Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide
[linux-2.6-block.git] / drivers / media / rc / imon.c
CommitLineData
3cfa958b 1// SPDX-License-Identifier: GPL-2.0-or-later
21677cfc
JW
2/*
3 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
4 *
693508df 5 * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
21677cfc
JW
6 * Portions based on the original lirc_imon driver,
7 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
8 *
9 * Huge thanks to R. Geoff Newbury for invaluable debugging on the
10 * 0xffdc iMON devices, and for sending me one to hack on, without
11 * which the support for them wouldn't be nearly as good. Thanks
12 * also to the numerous 0xffdc device owners that tested auto-config
13 * support for me and provided debug dumps from their devices.
21677cfc
JW
14 */
15
e2302501
JP
16#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
17
21677cfc
JW
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
1edba648 21#include <linux/ktime.h>
21677cfc
JW
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/uaccess.h>
47a09b08 25#include <linux/ratelimit.h>
21677cfc
JW
26
27#include <linux/input.h>
28#include <linux/usb.h>
29#include <linux/usb/input.h>
6bda9644 30#include <media/rc-core.h>
21677cfc 31
21677cfc
JW
32#include <linux/timer.h>
33
34#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
35#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
36#define MOD_NAME "imon"
8791d63a 37#define MOD_VERSION "0.9.4"
21677cfc
JW
38
39#define DISPLAY_MINOR_BASE 144
40#define DEVICE_NAME "lcd%d"
41
42#define BUF_CHUNK_SIZE 8
43#define BUF_SIZE 128
44
45#define BIT_DURATION 250 /* each bit received is 250us */
46
47#define IMON_CLOCK_ENABLE_PACKETS 2
21677cfc
JW
48
49/*** P R O T O T Y P E S ***/
50
51/* USB Callback prototypes */
52static int imon_probe(struct usb_interface *interface,
53 const struct usb_device_id *id);
54static void imon_disconnect(struct usb_interface *interface);
55static void usb_rx_callback_intf0(struct urb *urb);
56static void usb_rx_callback_intf1(struct urb *urb);
57static void usb_tx_callback(struct urb *urb);
58
59/* suspend/resume support */
60static int imon_resume(struct usb_interface *intf);
61static int imon_suspend(struct usb_interface *intf, pm_message_t message);
62
63/* Display file_operations function prototypes */
64static int display_open(struct inode *inode, struct file *file);
65static int display_close(struct inode *inode, struct file *file);
66
67/* VFD write operation */
d6740d86 68static ssize_t vfd_write(struct file *file, const char __user *buf,
21677cfc
JW
69 size_t n_bytes, loff_t *pos);
70
71/* LCD file_operations override function prototypes */
d6740d86 72static ssize_t lcd_write(struct file *file, const char __user *buf,
21677cfc
JW
73 size_t n_bytes, loff_t *pos);
74
75/*** G L O B A L S ***/
76
0d8053f2
UE
77struct imon_panel_key_table {
78 u64 hw_code;
79 u32 keycode;
80};
81
82struct imon_usb_dev_descr {
83 __u16 flags;
84#define IMON_NO_FLAGS 0
85#define IMON_NEED_20MS_PKT_DELAY 1
86 struct imon_panel_key_table key_table[];
87};
88
21677cfc
JW
89struct imon_context {
90 struct device *dev;
21677cfc
JW
91 /* Newer devices have two interfaces */
92 struct usb_device *usbdev_intf0;
93 struct usb_device *usbdev_intf1;
94
95 bool display_supported; /* not all controllers do */
96 bool display_isopen; /* display port has been opened */
bbe4690f 97 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
21677cfc
JW
98 bool rf_isassociating; /* RF remote associating */
99 bool dev_present_intf0; /* USB device presence, interface 0 */
100 bool dev_present_intf1; /* USB device presence, interface 1 */
101
102 struct mutex lock; /* to lock this object */
103 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
104
105 struct usb_endpoint_descriptor *rx_endpoint_intf0;
106 struct usb_endpoint_descriptor *rx_endpoint_intf1;
107 struct usb_endpoint_descriptor *tx_endpoint;
108 struct urb *rx_urb_intf0;
109 struct urb *rx_urb_intf1;
110 struct urb *tx_urb;
111 bool tx_control;
112 unsigned char usb_rx_buf[8];
113 unsigned char usb_tx_buf[8];
26ccbec4 114 unsigned int send_packet_delay;
21677cfc
JW
115
116 struct tx_t {
117 unsigned char data_buf[35]; /* user data buffer */
118 struct completion finished; /* wait for write to finish */
119 bool busy; /* write in progress */
120 int status; /* status of tx completion */
121 } tx;
122
123 u16 vendor; /* usb vendor ID */
124 u16 product; /* usb product ID */
125
d8b4b582 126 struct rc_dev *rdev; /* rc-core device for remote */
eaf2bcc9 127 struct input_dev *idev; /* input device for panel & IR mouse */
21677cfc
JW
128 struct input_dev *touch; /* input device for touchscreen */
129
693508df 130 spinlock_t kc_lock; /* make sure we get keycodes right */
21677cfc
JW
131 u32 kc; /* current input keycode */
132 u32 last_keycode; /* last reported input keycode */
eaf2bcc9
DH
133 u32 rc_scancode; /* the computed remote scancode */
134 u8 rc_toggle; /* the computed remote toggle bit */
6d741bfe 135 u64 rc_proto; /* iMON or MCE (RC6) IR protocol? */
21677cfc
JW
136 bool release_code; /* some keys send a release code */
137
138 u8 display_type; /* store the display type */
139 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
140
eaf2bcc9
DH
141 char name_rdev[128]; /* rc input device name */
142 char phys_rdev[64]; /* rc input device phys path */
143
21677cfc
JW
144 char name_idev[128]; /* input device name */
145 char phys_idev[64]; /* input device phys path */
21677cfc
JW
146
147 char name_touch[128]; /* touch screen name */
148 char phys_touch[64]; /* touch screen phys path */
149 struct timer_list ttimer; /* touch screen timer */
150 int touch_x; /* x coordinate on touchscreen */
151 int touch_y; /* y coordinate on touchscreen */
0d8053f2
UE
152 struct imon_usb_dev_descr *dev_descr; /* device description with key
153 table for front panels */
21677cfc
JW
154};
155
156#define TOUCH_TIMEOUT (HZ/30)
21677cfc
JW
157
158/* vfd character device file operations */
159static const struct file_operations vfd_fops = {
160 .owner = THIS_MODULE,
161 .open = &display_open,
162 .write = &vfd_write,
6038f373
AB
163 .release = &display_close,
164 .llseek = noop_llseek,
21677cfc
JW
165};
166
167/* lcd character device file operations */
168static const struct file_operations lcd_fops = {
169 .owner = THIS_MODULE,
170 .open = &display_open,
171 .write = &lcd_write,
6038f373
AB
172 .release = &display_close,
173 .llseek = noop_llseek,
21677cfc
JW
174};
175
176enum {
177 IMON_DISPLAY_TYPE_AUTO = 0,
178 IMON_DISPLAY_TYPE_VFD = 1,
179 IMON_DISPLAY_TYPE_LCD = 2,
180 IMON_DISPLAY_TYPE_VGA = 3,
181 IMON_DISPLAY_TYPE_NONE = 4,
182};
183
21677cfc
JW
184enum {
185 IMON_KEY_IMON = 0,
186 IMON_KEY_MCE = 1,
187 IMON_KEY_PANEL = 2,
188};
189
0d8053f2
UE
190static struct usb_class_driver imon_vfd_class = {
191 .name = DEVICE_NAME,
192 .fops = &vfd_fops,
193 .minor_base = DISPLAY_MINOR_BASE,
194};
195
196static struct usb_class_driver imon_lcd_class = {
197 .name = DEVICE_NAME,
198 .fops = &lcd_fops,
199 .minor_base = DISPLAY_MINOR_BASE,
200};
201
202/* imon receiver front panel/knob key table */
203static const struct imon_usb_dev_descr imon_default_table = {
204 .flags = IMON_NO_FLAGS,
205 .key_table = {
206 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
207 { 0x000000001200ffeell, KEY_UP },
208 { 0x000000001300ffeell, KEY_DOWN },
209 { 0x000000001400ffeell, KEY_LEFT },
210 { 0x000000001500ffeell, KEY_RIGHT },
211 { 0x000000001600ffeell, KEY_ENTER },
212 { 0x000000001700ffeell, KEY_ESC },
213 { 0x000000001f00ffeell, KEY_AUDIO },
214 { 0x000000002000ffeell, KEY_VIDEO },
215 { 0x000000002100ffeell, KEY_CAMERA },
216 { 0x000000002700ffeell, KEY_DVD },
217 { 0x000000002300ffeell, KEY_TV },
218 { 0x000000002b00ffeell, KEY_EXIT },
219 { 0x000000002c00ffeell, KEY_SELECT },
220 { 0x000000002d00ffeell, KEY_MENU },
221 { 0x000000000500ffeell, KEY_PREVIOUS },
222 { 0x000000000700ffeell, KEY_REWIND },
223 { 0x000000000400ffeell, KEY_STOP },
224 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
225 { 0x000000000800ffeell, KEY_FASTFORWARD },
226 { 0x000000000600ffeell, KEY_NEXT },
227 { 0x000000010000ffeell, KEY_RIGHT },
228 { 0x000001000000ffeell, KEY_LEFT },
229 { 0x000000003d00ffeell, KEY_SELECT },
230 { 0x000100000000ffeell, KEY_VOLUMEUP },
231 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
232 { 0x000000000100ffeell, KEY_MUTE },
233 /* 0xffdc iMON MCE VFD */
234 { 0x00010000ffffffeell, KEY_VOLUMEUP },
235 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
236 { 0x00000001ffffffeell, KEY_MUTE },
237 { 0x0000000fffffffeell, KEY_MEDIA },
238 { 0x00000012ffffffeell, KEY_UP },
239 { 0x00000013ffffffeell, KEY_DOWN },
240 { 0x00000014ffffffeell, KEY_LEFT },
241 { 0x00000015ffffffeell, KEY_RIGHT },
242 { 0x00000016ffffffeell, KEY_ENTER },
243 { 0x00000017ffffffeell, KEY_ESC },
244 /* iMON Knob values */
245 { 0x000100ffffffffeell, KEY_VOLUMEUP },
246 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
247 { 0x000008ffffffffeell, KEY_MUTE },
248 { 0, KEY_RESERVED },
249 }
250};
251
252static const struct imon_usb_dev_descr imon_OEM_VFD = {
253 .flags = IMON_NEED_20MS_PKT_DELAY,
254 .key_table = {
255 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
256 { 0x000000001200ffeell, KEY_UP },
257 { 0x000000001300ffeell, KEY_DOWN },
258 { 0x000000001400ffeell, KEY_LEFT },
259 { 0x000000001500ffeell, KEY_RIGHT },
260 { 0x000000001600ffeell, KEY_ENTER },
261 { 0x000000001700ffeell, KEY_ESC },
262 { 0x000000001f00ffeell, KEY_AUDIO },
263 { 0x000000002b00ffeell, KEY_EXIT },
264 { 0x000000002c00ffeell, KEY_SELECT },
265 { 0x000000002d00ffeell, KEY_MENU },
266 { 0x000000000500ffeell, KEY_PREVIOUS },
267 { 0x000000000700ffeell, KEY_REWIND },
268 { 0x000000000400ffeell, KEY_STOP },
269 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
270 { 0x000000000800ffeell, KEY_FASTFORWARD },
271 { 0x000000000600ffeell, KEY_NEXT },
272 { 0x000000010000ffeell, KEY_RIGHT },
273 { 0x000001000000ffeell, KEY_LEFT },
274 { 0x000000003d00ffeell, KEY_SELECT },
275 { 0x000100000000ffeell, KEY_VOLUMEUP },
276 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
277 { 0x000000000100ffeell, KEY_MUTE },
278 /* 0xffdc iMON MCE VFD */
279 { 0x00010000ffffffeell, KEY_VOLUMEUP },
280 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
281 { 0x00000001ffffffeell, KEY_MUTE },
282 { 0x0000000fffffffeell, KEY_MEDIA },
283 { 0x00000012ffffffeell, KEY_UP },
284 { 0x00000013ffffffeell, KEY_DOWN },
285 { 0x00000014ffffffeell, KEY_LEFT },
286 { 0x00000015ffffffeell, KEY_RIGHT },
287 { 0x00000016ffffffeell, KEY_ENTER },
288 { 0x00000017ffffffeell, KEY_ESC },
289 /* iMON Knob values */
290 { 0x000100ffffffffeell, KEY_VOLUMEUP },
291 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
292 { 0x000008ffffffffeell, KEY_MUTE },
293 { 0, KEY_RESERVED },
294 }
26ccbec4
KB
295};
296
7b5fc071
UE
297/* imon receiver front panel/knob key table for DH102*/
298static const struct imon_usb_dev_descr imon_DH102 = {
299 .flags = IMON_NO_FLAGS,
300 .key_table = {
301 { 0x000100000000ffeell, KEY_VOLUMEUP },
302 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
303 { 0x000000010000ffeell, KEY_MUTE },
304 { 0x0000000f0000ffeell, KEY_MEDIA },
305 { 0x000000120000ffeell, KEY_UP },
306 { 0x000000130000ffeell, KEY_DOWN },
307 { 0x000000140000ffeell, KEY_LEFT },
308 { 0x000000150000ffeell, KEY_RIGHT },
309 { 0x000000160000ffeell, KEY_ENTER },
310 { 0x000000170000ffeell, KEY_ESC },
311 { 0x0000002b0000ffeell, KEY_EXIT },
312 { 0x0000002c0000ffeell, KEY_SELECT },
313 { 0x0000002d0000ffeell, KEY_MENU },
314 { 0, KEY_RESERVED }
315 }
316};
317
21677cfc
JW
318/*
319 * USB Device ID for iMON USB Control Boards
320 *
321 * The Windows drivers contain 6 different inf files, more or less one for
322 * each new device until the 0x0034-0x0046 devices, which all use the same
323 * driver. Some of the devices in the 34-46 range haven't been definitively
324 * identified yet. Early devices have either a TriGem Computer, Inc. or a
325 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
326 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
327 * the ffdc and later devices, which do onboard decoding.
328 */
5fad16b5 329static const struct usb_device_id imon_usb_id_table[] = {
21677cfc
JW
330 /*
331 * Several devices with this same device ID, all use iMON_PAD.inf
332 * SoundGraph iMON PAD (IR & VFD)
333 * SoundGraph iMON PAD (IR & LCD)
334 * SoundGraph iMON Knob (IR only)
335 */
0d8053f2
UE
336 { USB_DEVICE(0x15c2, 0xffdc),
337 .driver_info = (unsigned long)&imon_default_table },
21677cfc
JW
338
339 /*
340 * Newer devices, all driven by the latest iMON Windows driver, full
341 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
342 * Need user input to fill in details on unknown devices.
343 */
344 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
0d8053f2 345 { USB_DEVICE(0x15c2, 0x0034),
7b5fc071 346 .driver_info = (unsigned long)&imon_DH102 },
21677cfc 347 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
0d8053f2
UE
348 { USB_DEVICE(0x15c2, 0x0035),
349 .driver_info = (unsigned long)&imon_default_table},
21677cfc 350 /* SoundGraph iMON OEM VFD (IR & VFD) */
0d8053f2
UE
351 { USB_DEVICE(0x15c2, 0x0036),
352 .driver_info = (unsigned long)&imon_OEM_VFD },
21677cfc 353 /* device specifics unknown */
0d8053f2
UE
354 { USB_DEVICE(0x15c2, 0x0037),
355 .driver_info = (unsigned long)&imon_default_table},
21677cfc 356 /* SoundGraph iMON OEM LCD (IR & LCD) */
0d8053f2
UE
357 { USB_DEVICE(0x15c2, 0x0038),
358 .driver_info = (unsigned long)&imon_default_table},
21677cfc 359 /* SoundGraph iMON UltraBay (IR & LCD) */
0d8053f2
UE
360 { USB_DEVICE(0x15c2, 0x0039),
361 .driver_info = (unsigned long)&imon_default_table},
21677cfc 362 /* device specifics unknown */
0d8053f2
UE
363 { USB_DEVICE(0x15c2, 0x003a),
364 .driver_info = (unsigned long)&imon_default_table},
21677cfc 365 /* device specifics unknown */
0d8053f2
UE
366 { USB_DEVICE(0x15c2, 0x003b),
367 .driver_info = (unsigned long)&imon_default_table},
21677cfc 368 /* SoundGraph iMON OEM Inside (IR only) */
0d8053f2
UE
369 { USB_DEVICE(0x15c2, 0x003c),
370 .driver_info = (unsigned long)&imon_default_table},
21677cfc 371 /* device specifics unknown */
0d8053f2
UE
372 { USB_DEVICE(0x15c2, 0x003d),
373 .driver_info = (unsigned long)&imon_default_table},
21677cfc 374 /* device specifics unknown */
0d8053f2
UE
375 { USB_DEVICE(0x15c2, 0x003e),
376 .driver_info = (unsigned long)&imon_default_table},
21677cfc 377 /* device specifics unknown */
0d8053f2
UE
378 { USB_DEVICE(0x15c2, 0x003f),
379 .driver_info = (unsigned long)&imon_default_table},
21677cfc 380 /* device specifics unknown */
0d8053f2
UE
381 { USB_DEVICE(0x15c2, 0x0040),
382 .driver_info = (unsigned long)&imon_default_table},
21677cfc 383 /* SoundGraph iMON MINI (IR only) */
0d8053f2
UE
384 { USB_DEVICE(0x15c2, 0x0041),
385 .driver_info = (unsigned long)&imon_default_table},
21677cfc 386 /* Antec Veris Multimedia Station EZ External (IR only) */
0d8053f2
UE
387 { USB_DEVICE(0x15c2, 0x0042),
388 .driver_info = (unsigned long)&imon_default_table},
21677cfc 389 /* Antec Veris Multimedia Station Basic Internal (IR only) */
0d8053f2
UE
390 { USB_DEVICE(0x15c2, 0x0043),
391 .driver_info = (unsigned long)&imon_default_table},
21677cfc 392 /* Antec Veris Multimedia Station Elite (IR & VFD) */
0d8053f2
UE
393 { USB_DEVICE(0x15c2, 0x0044),
394 .driver_info = (unsigned long)&imon_default_table},
21677cfc 395 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
0d8053f2
UE
396 { USB_DEVICE(0x15c2, 0x0045),
397 .driver_info = (unsigned long)&imon_default_table},
21677cfc 398 /* device specifics unknown */
0d8053f2
UE
399 { USB_DEVICE(0x15c2, 0x0046),
400 .driver_info = (unsigned long)&imon_default_table},
21677cfc
JW
401 {}
402};
403
404/* USB Device data */
405static struct usb_driver imon_driver = {
406 .name = MOD_NAME,
407 .probe = imon_probe,
4c62e976 408 .disconnect = imon_disconnect,
21677cfc
JW
409 .suspend = imon_suspend,
410 .resume = imon_resume,
411 .id_table = imon_usb_id_table,
412};
413
21677cfc
JW
414/* to prevent races between open() and disconnect(), probing, etc */
415static DEFINE_MUTEX(driver_lock);
416
417/* Module bookkeeping bits */
418MODULE_AUTHOR(MOD_AUTHOR);
419MODULE_DESCRIPTION(MOD_DESC);
420MODULE_VERSION(MOD_VERSION);
421MODULE_LICENSE("GPL");
422MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
423
424static bool debug;
425module_param(debug, bool, S_IRUGO | S_IWUSR);
6aa209e4 426MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
21677cfc
JW
427
428/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
429static int display_type;
430module_param(display_type, int, S_IRUGO);
25ec587c 431MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, 1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
21677cfc 432
6718e8ad
JW
433static int pad_stabilize = 1;
434module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
25ec587c 435MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD presses in arrow key mode. 0=disable, 1=enable (default).");
21677cfc
JW
436
437/*
438 * In certain use cases, mouse mode isn't really helpful, and could actually
439 * cause confusion, so allow disabling it when the IR device is open.
440 */
441static bool nomouse;
442module_param(nomouse, bool, S_IRUGO | S_IWUSR);
25ec587c 443MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is open. 0=don't disable, 1=disable. (default: don't disable)");
21677cfc
JW
444
445/* threshold at which a pad push registers as an arrow key in kbd mode */
446static int pad_thresh;
447module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
25ec587c 448MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an arrow key in kbd mode (default: 28)");
21677cfc
JW
449
450
451static void free_imon_context(struct imon_context *ictx)
452{
453 struct device *dev = ictx->dev;
454
455 usb_free_urb(ictx->tx_urb);
456 usb_free_urb(ictx->rx_urb_intf0);
457 usb_free_urb(ictx->rx_urb_intf1);
458 kfree(ictx);
459
460 dev_dbg(dev, "%s: iMON context freed\n", __func__);
461}
462
255940e6 463/*
21677cfc
JW
464 * Called when the Display device (e.g. /dev/lcd0)
465 * is opened by the application.
466 */
467static int display_open(struct inode *inode, struct file *file)
468{
469 struct usb_interface *interface;
470 struct imon_context *ictx = NULL;
471 int subminor;
472 int retval = 0;
473
474 /* prevent races with disconnect */
475 mutex_lock(&driver_lock);
476
477 subminor = iminor(inode);
478 interface = usb_find_interface(&imon_driver, subminor);
479 if (!interface) {
e2302501 480 pr_err("could not find interface for minor %d\n", subminor);
21677cfc
JW
481 retval = -ENODEV;
482 goto exit;
483 }
484 ictx = usb_get_intfdata(interface);
485
486 if (!ictx) {
e2302501 487 pr_err("no context found for minor %d\n", subminor);
21677cfc
JW
488 retval = -ENODEV;
489 goto exit;
490 }
491
492 mutex_lock(&ictx->lock);
493
494 if (!ictx->display_supported) {
e2302501 495 pr_err("display not supported by device\n");
21677cfc
JW
496 retval = -ENODEV;
497 } else if (ictx->display_isopen) {
e2302501 498 pr_err("display port is already open\n");
21677cfc
JW
499 retval = -EBUSY;
500 } else {
f789bf40 501 ictx->display_isopen = true;
21677cfc
JW
502 file->private_data = ictx;
503 dev_dbg(ictx->dev, "display port opened\n");
504 }
505
506 mutex_unlock(&ictx->lock);
507
508exit:
509 mutex_unlock(&driver_lock);
510 return retval;
511}
512
255940e6 513/*
21677cfc
JW
514 * Called when the display device (e.g. /dev/lcd0)
515 * is closed by the application.
516 */
517static int display_close(struct inode *inode, struct file *file)
518{
519 struct imon_context *ictx = NULL;
520 int retval = 0;
521
abf84383 522 ictx = file->private_data;
21677cfc
JW
523
524 if (!ictx) {
e2302501 525 pr_err("no context for device\n");
21677cfc
JW
526 return -ENODEV;
527 }
528
529 mutex_lock(&ictx->lock);
530
531 if (!ictx->display_supported) {
e2302501 532 pr_err("display not supported by device\n");
21677cfc
JW
533 retval = -ENODEV;
534 } else if (!ictx->display_isopen) {
e2302501 535 pr_err("display is not open\n");
21677cfc
JW
536 retval = -EIO;
537 } else {
f789bf40 538 ictx->display_isopen = false;
21677cfc 539 dev_dbg(ictx->dev, "display port closed\n");
21677cfc
JW
540 }
541
542 mutex_unlock(&ictx->lock);
543 return retval;
544}
545
255940e6 546/*
23ef710e
JW
547 * Sends a packet to the device -- this function must be called with
548 * ictx->lock held, or its unlock/lock sequence while waiting for tx
549 * to complete can/will lead to a deadlock.
21677cfc
JW
550 */
551static int send_packet(struct imon_context *ictx)
552{
553 unsigned int pipe;
554 unsigned long timeout;
555 int interval = 0;
556 int retval = 0;
557 struct usb_ctrlrequest *control_req = NULL;
558
559 /* Check if we need to use control or interrupt urb */
560 if (!ictx->tx_control) {
561 pipe = usb_sndintpipe(ictx->usbdev_intf0,
562 ictx->tx_endpoint->bEndpointAddress);
563 interval = ictx->tx_endpoint->bInterval;
564
565 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
566 ictx->usb_tx_buf,
567 sizeof(ictx->usb_tx_buf),
568 usb_tx_callback, ictx, interval);
569
570 ictx->tx_urb->actual_length = 0;
571 } else {
572 /* fill request into kmalloc'ed space: */
a8c779eb 573 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
21677cfc
JW
574 if (control_req == NULL)
575 return -ENOMEM;
576
577 /* setup packet is '21 09 0200 0001 0008' */
578 control_req->bRequestType = 0x21;
579 control_req->bRequest = 0x09;
580 control_req->wValue = cpu_to_le16(0x0200);
581 control_req->wIndex = cpu_to_le16(0x0001);
582 control_req->wLength = cpu_to_le16(0x0008);
583
584 /* control pipe is endpoint 0x00 */
585 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
586
587 /* build the control urb */
588 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
589 pipe, (unsigned char *)control_req,
590 ictx->usb_tx_buf,
591 sizeof(ictx->usb_tx_buf),
592 usb_tx_callback, ictx);
593 ictx->tx_urb->actual_length = 0;
594 }
595
7c073fff 596 reinit_completion(&ictx->tx.finished);
f789bf40 597 ictx->tx.busy = true;
21677cfc
JW
598 smp_rmb(); /* ensure later readers know we're busy */
599
600 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
601 if (retval) {
f789bf40 602 ictx->tx.busy = false;
21677cfc 603 smp_rmb(); /* ensure later readers know we're not busy */
47a09b08 604 pr_err_ratelimited("error submitting urb(%d)\n", retval);
21677cfc
JW
605 } else {
606 /* Wait for transmission to complete (or abort) */
607 mutex_unlock(&ictx->lock);
608 retval = wait_for_completion_interruptible(
609 &ictx->tx.finished);
5f3f254f
KB
610 if (retval) {
611 usb_kill_urb(ictx->tx_urb);
47a09b08 612 pr_err_ratelimited("task interrupted\n");
5f3f254f 613 }
21677cfc
JW
614 mutex_lock(&ictx->lock);
615
616 retval = ictx->tx.status;
617 if (retval)
47a09b08 618 pr_err_ratelimited("packet tx failed (%d)\n", retval);
21677cfc
JW
619 }
620
621 kfree(control_req);
622
623 /*
26ccbec4 624 * Induce a mandatory delay before returning, as otherwise,
21677cfc
JW
625 * send_packet can get called so rapidly as to overwhelm the device,
626 * particularly on faster systems and/or those with quirky usb.
627 */
26ccbec4
KB
628 timeout = msecs_to_jiffies(ictx->send_packet_delay);
629 set_current_state(TASK_INTERRUPTIBLE);
21677cfc
JW
630 schedule_timeout(timeout);
631
632 return retval;
633}
634
255940e6 635/*
21677cfc
JW
636 * Sends an associate packet to the iMON 2.4G.
637 *
638 * This might not be such a good idea, since it has an id collision with
639 * some versions of the "IR & VFD" combo. The only way to determine if it
640 * is an RF version is to look at the product description string. (Which
641 * we currently do not fetch).
642 */
643static int send_associate_24g(struct imon_context *ictx)
644{
645 int retval;
646 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
647 0x00, 0x00, 0x00, 0x20 };
648
649 if (!ictx) {
e2302501 650 pr_err("no context for device\n");
21677cfc
JW
651 return -ENODEV;
652 }
653
654 if (!ictx->dev_present_intf0) {
e2302501 655 pr_err("no iMON device present\n");
21677cfc
JW
656 return -ENODEV;
657 }
658
659 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
660 retval = send_packet(ictx);
661
662 return retval;
663}
664
255940e6 665/*
21677cfc
JW
666 * Sends packets to setup and show clock on iMON display
667 *
668 * Arguments: year - last 2 digits of year, month - 1..12,
669 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
670 * hour - 0..23, minute - 0..59, second - 0..59
671 */
672static int send_set_imon_clock(struct imon_context *ictx,
673 unsigned int year, unsigned int month,
674 unsigned int day, unsigned int dow,
675 unsigned int hour, unsigned int minute,
676 unsigned int second)
677{
678 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
679 int retval = 0;
680 int i;
681
682 if (!ictx) {
e2302501 683 pr_err("no context for device\n");
21677cfc
JW
684 return -ENODEV;
685 }
686
687 switch (ictx->display_type) {
688 case IMON_DISPLAY_TYPE_LCD:
689 clock_enable_pkt[0][0] = 0x80;
690 clock_enable_pkt[0][1] = year;
691 clock_enable_pkt[0][2] = month-1;
692 clock_enable_pkt[0][3] = day;
693 clock_enable_pkt[0][4] = hour;
694 clock_enable_pkt[0][5] = minute;
695 clock_enable_pkt[0][6] = second;
696
697 clock_enable_pkt[1][0] = 0x80;
698 clock_enable_pkt[1][1] = 0;
699 clock_enable_pkt[1][2] = 0;
700 clock_enable_pkt[1][3] = 0;
701 clock_enable_pkt[1][4] = 0;
702 clock_enable_pkt[1][5] = 0;
703 clock_enable_pkt[1][6] = 0;
704
705 if (ictx->product == 0xffdc) {
706 clock_enable_pkt[0][7] = 0x50;
707 clock_enable_pkt[1][7] = 0x51;
708 } else {
709 clock_enable_pkt[0][7] = 0x88;
710 clock_enable_pkt[1][7] = 0x8a;
711 }
712
713 break;
714
715 case IMON_DISPLAY_TYPE_VFD:
716 clock_enable_pkt[0][0] = year;
717 clock_enable_pkt[0][1] = month-1;
718 clock_enable_pkt[0][2] = day;
719 clock_enable_pkt[0][3] = dow;
720 clock_enable_pkt[0][4] = hour;
721 clock_enable_pkt[0][5] = minute;
722 clock_enable_pkt[0][6] = second;
723 clock_enable_pkt[0][7] = 0x40;
724
725 clock_enable_pkt[1][0] = 0;
726 clock_enable_pkt[1][1] = 0;
727 clock_enable_pkt[1][2] = 1;
728 clock_enable_pkt[1][3] = 0;
729 clock_enable_pkt[1][4] = 0;
730 clock_enable_pkt[1][5] = 0;
731 clock_enable_pkt[1][6] = 0;
732 clock_enable_pkt[1][7] = 0x42;
733
734 break;
735
736 default:
737 return -ENODEV;
738 }
739
740 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
741 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
742 retval = send_packet(ictx);
743 if (retval) {
e2302501 744 pr_err("send_packet failed for packet %d\n", i);
21677cfc
JW
745 break;
746 }
747 }
748
749 return retval;
750}
751
255940e6 752/*
21677cfc
JW
753 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
754 */
755static ssize_t show_associate_remote(struct device *d,
756 struct device_attribute *attr,
757 char *buf)
758{
759 struct imon_context *ictx = dev_get_drvdata(d);
760
761 if (!ictx)
762 return -ENODEV;
763
764 mutex_lock(&ictx->lock);
765 if (ictx->rf_isassociating)
2396e282 766 strscpy(buf, "associating\n", PAGE_SIZE);
21677cfc 767 else
2396e282 768 strscpy(buf, "closed\n", PAGE_SIZE);
21677cfc 769
25ec587c 770 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for instructions on how to associate your iMON 2.4G DT/LT remote\n");
21677cfc
JW
771 mutex_unlock(&ictx->lock);
772 return strlen(buf);
773}
774
775static ssize_t store_associate_remote(struct device *d,
776 struct device_attribute *attr,
777 const char *buf, size_t count)
778{
779 struct imon_context *ictx;
780
781 ictx = dev_get_drvdata(d);
782
783 if (!ictx)
784 return -ENODEV;
785
786 mutex_lock(&ictx->lock);
f789bf40 787 ictx->rf_isassociating = true;
21677cfc
JW
788 send_associate_24g(ictx);
789 mutex_unlock(&ictx->lock);
790
791 return count;
792}
793
255940e6 794/*
21677cfc
JW
795 * sysfs functions to control internal imon clock
796 */
797static ssize_t show_imon_clock(struct device *d,
798 struct device_attribute *attr, char *buf)
799{
800 struct imon_context *ictx = dev_get_drvdata(d);
801 size_t len;
802
803 if (!ictx)
804 return -ENODEV;
805
806 mutex_lock(&ictx->lock);
807
808 if (!ictx->display_supported) {
809 len = snprintf(buf, PAGE_SIZE, "Not supported.");
810 } else {
811 len = snprintf(buf, PAGE_SIZE,
812 "To set the clock on your iMON display:\n"
813 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
814 "%s", ictx->display_isopen ?
815 "\nNOTE: imon device must be closed\n" : "");
816 }
817
818 mutex_unlock(&ictx->lock);
819
820 return len;
821}
822
823static ssize_t store_imon_clock(struct device *d,
824 struct device_attribute *attr,
825 const char *buf, size_t count)
826{
827 struct imon_context *ictx = dev_get_drvdata(d);
828 ssize_t retval;
829 unsigned int year, month, day, dow, hour, minute, second;
830
831 if (!ictx)
832 return -ENODEV;
833
834 mutex_lock(&ictx->lock);
835
836 if (!ictx->display_supported) {
837 retval = -ENODEV;
838 goto exit;
839 } else if (ictx->display_isopen) {
840 retval = -EBUSY;
841 goto exit;
842 }
843
844 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
845 &hour, &minute, &second) != 7) {
846 retval = -EINVAL;
847 goto exit;
848 }
849
850 if ((month < 1 || month > 12) ||
851 (day < 1 || day > 31) || (dow > 6) ||
852 (hour > 23) || (minute > 59) || (second > 59)) {
853 retval = -EINVAL;
854 goto exit;
855 }
856
857 retval = send_set_imon_clock(ictx, year, month, day, dow,
858 hour, minute, second);
859 if (retval)
860 goto exit;
861
862 retval = count;
863exit:
864 mutex_unlock(&ictx->lock);
865
866 return retval;
867}
868
869
870static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
871 store_imon_clock);
872
873static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
874 store_associate_remote);
875
876static struct attribute *imon_display_sysfs_entries[] = {
877 &dev_attr_imon_clock.attr,
878 NULL
879};
880
d9a77b98 881static const struct attribute_group imon_display_attr_group = {
21677cfc
JW
882 .attrs = imon_display_sysfs_entries
883};
884
885static struct attribute *imon_rf_sysfs_entries[] = {
886 &dev_attr_associate_remote.attr,
887 NULL
888};
889
d9a77b98 890static const struct attribute_group imon_rf_attr_group = {
21677cfc
JW
891 .attrs = imon_rf_sysfs_entries
892};
893
255940e6 894/*
21677cfc
JW
895 * Writes data to the VFD. The iMON VFD is 2x16 characters
896 * and requires data in 5 consecutive USB interrupt packets,
897 * each packet but the last carrying 7 bytes.
898 *
899 * I don't know if the VFD board supports features such as
900 * scrolling, clearing rows, blanking, etc. so at
901 * the caller must provide a full screen of data. If fewer
902 * than 32 bytes are provided spaces will be appended to
903 * generate a full screen.
904 */
d6740d86 905static ssize_t vfd_write(struct file *file, const char __user *buf,
21677cfc
JW
906 size_t n_bytes, loff_t *pos)
907{
908 int i;
909 int offset;
910 int seq;
911 int retval = 0;
912 struct imon_context *ictx;
c25895c7 913 static const unsigned char vfd_packet6[] = {
21677cfc
JW
914 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
915
abf84383 916 ictx = file->private_data;
21677cfc 917 if (!ictx) {
47a09b08 918 pr_err_ratelimited("no context for device\n");
21677cfc
JW
919 return -ENODEV;
920 }
921
922 mutex_lock(&ictx->lock);
923
924 if (!ictx->dev_present_intf0) {
47a09b08 925 pr_err_ratelimited("no iMON device present\n");
21677cfc
JW
926 retval = -ENODEV;
927 goto exit;
928 }
929
930 if (n_bytes <= 0 || n_bytes > 32) {
47a09b08 931 pr_err_ratelimited("invalid payload size\n");
21677cfc
JW
932 retval = -EINVAL;
933 goto exit;
934 }
935
936 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
937 retval = -EFAULT;
938 goto exit;
939 }
940
941 /* Pad with spaces */
942 for (i = n_bytes; i < 32; ++i)
943 ictx->tx.data_buf[i] = ' ';
944
945 for (i = 32; i < 35; ++i)
946 ictx->tx.data_buf[i] = 0xFF;
947
948 offset = 0;
949 seq = 0;
950
951 do {
952 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
953 ictx->usb_tx_buf[7] = (unsigned char) seq;
954
955 retval = send_packet(ictx);
956 if (retval) {
47a09b08 957 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
21677cfc
JW
958 goto exit;
959 } else {
960 seq += 2;
961 offset += 7;
962 }
963
964 } while (offset < 35);
965
966 /* Send packet #6 */
967 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
968 ictx->usb_tx_buf[7] = (unsigned char) seq;
969 retval = send_packet(ictx);
970 if (retval)
47a09b08 971 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
21677cfc
JW
972
973exit:
974 mutex_unlock(&ictx->lock);
975
976 return (!retval) ? n_bytes : retval;
977}
978
255940e6 979/*
21677cfc
JW
980 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
981 * packets. We accept data as 16 hexadecimal digits, followed by a
982 * newline (to make it easy to drive the device from a command-line
983 * -- even though the actual binary data is a bit complicated).
984 *
985 * The device itself is not a "traditional" text-mode display. It's
986 * actually a 16x96 pixel bitmap display. That means if you want to
987 * display text, you've got to have your own "font" and translate the
988 * text into bitmaps for display. This is really flexible (you can
989 * display whatever diacritics you need, and so on), but it's also
990 * a lot more complicated than most LCDs...
991 */
d6740d86 992static ssize_t lcd_write(struct file *file, const char __user *buf,
21677cfc
JW
993 size_t n_bytes, loff_t *pos)
994{
995 int retval = 0;
996 struct imon_context *ictx;
997
abf84383 998 ictx = file->private_data;
21677cfc 999 if (!ictx) {
47a09b08 1000 pr_err_ratelimited("no context for device\n");
21677cfc
JW
1001 return -ENODEV;
1002 }
1003
1004 mutex_lock(&ictx->lock);
1005
1006 if (!ictx->display_supported) {
47a09b08 1007 pr_err_ratelimited("no iMON display present\n");
21677cfc
JW
1008 retval = -ENODEV;
1009 goto exit;
1010 }
1011
1012 if (n_bytes != 8) {
47a09b08
JW
1013 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1014 (int)n_bytes);
21677cfc
JW
1015 retval = -EINVAL;
1016 goto exit;
1017 }
1018
1019 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1020 retval = -EFAULT;
1021 goto exit;
1022 }
1023
1024 retval = send_packet(ictx);
1025 if (retval) {
47a09b08 1026 pr_err_ratelimited("send packet failed!\n");
21677cfc
JW
1027 goto exit;
1028 } else {
1029 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1030 __func__, (int) n_bytes);
1031 }
1032exit:
1033 mutex_unlock(&ictx->lock);
1034 return (!retval) ? n_bytes : retval;
1035}
1036
255940e6 1037/*
21677cfc
JW
1038 * Callback function for USB core API: transmit data
1039 */
1040static void usb_tx_callback(struct urb *urb)
1041{
1042 struct imon_context *ictx;
1043
1044 if (!urb)
1045 return;
1046 ictx = (struct imon_context *)urb->context;
1047 if (!ictx)
1048 return;
1049
1050 ictx->tx.status = urb->status;
1051
1052 /* notify waiters that write has finished */
f789bf40 1053 ictx->tx.busy = false;
21677cfc
JW
1054 smp_rmb(); /* ensure later readers know we're not busy */
1055 complete(&ictx->tx.finished);
1056}
1057
255940e6 1058/*
21677cfc
JW
1059 * report touchscreen input
1060 */
b17ec78a 1061static void imon_touch_display_timeout(struct timer_list *t)
21677cfc 1062{
b17ec78a 1063 struct imon_context *ictx = from_timer(ictx, t, ttimer);
21677cfc 1064
f03900d6 1065 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
21677cfc
JW
1066 return;
1067
1068 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1069 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1070 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1071 input_sync(ictx->touch);
1072}
1073
255940e6 1074/*
21677cfc
JW
1075 * iMON IR receivers support two different signal sets -- those used by
1076 * the iMON remotes, and those used by the Windows MCE remotes (which is
1077 * really just RC-6), but only one or the other at a time, as the signals
1078 * are decoded onboard the receiver.
23ef710e
JW
1079 *
1080 * This function gets called two different ways, one way is from
1081 * rc_register_device, for initial protocol selection/setup, and the other is
1082 * via a userspace-initiated protocol change request, either by direct sysfs
1083 * prodding or by something like ir-keytable. In the rc_register_device case,
1084 * the imon context lock is already held, but when initiated from userspace,
1085 * it is not, so we must acquire it prior to calling send_packet, which
1086 * requires that the lock is held.
21677cfc 1087 */
6d741bfe 1088static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
21677cfc
JW
1089{
1090 int retval;
d8b4b582 1091 struct imon_context *ictx = rc->priv;
21677cfc 1092 struct device *dev = ictx->dev;
23ef710e 1093 bool unlock = false;
21677cfc
JW
1094 unsigned char ir_proto_packet[] = {
1095 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1096
6d741bfe 1097 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
25ec587c 1098 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
21677cfc 1099
6d741bfe 1100 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
21677cfc
JW
1101 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1102 ir_proto_packet[0] = 0x01;
6d741bfe 1103 *rc_proto = RC_PROTO_BIT_RC6_MCE;
2525fdcb 1104 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
666a9ed8 1105 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
76f1ef42 1106 if (!pad_stabilize)
666a9ed8 1107 dev_dbg(dev, "PAD stabilize functionality disabled\n");
21677cfc 1108 /* ir_proto_packet[0] = 0x00; // already the default */
2525fdcb 1109 *rc_proto = RC_PROTO_BIT_IMON;
c003ab1b 1110 } else {
25ec587c 1111 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
76f1ef42 1112 if (!pad_stabilize)
666a9ed8 1113 dev_dbg(dev, "PAD stabilize functionality disabled\n");
6718e8ad 1114 /* ir_proto_packet[0] = 0x00; // already the default */
2525fdcb 1115 *rc_proto = RC_PROTO_BIT_IMON;
21677cfc
JW
1116 }
1117
1118 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1119
23ef710e
JW
1120 if (!mutex_is_locked(&ictx->lock)) {
1121 unlock = true;
1122 mutex_lock(&ictx->lock);
1123 }
1124
21677cfc 1125 retval = send_packet(ictx);
6718e8ad
JW
1126 if (retval)
1127 goto out;
1128
6d741bfe 1129 ictx->rc_proto = *rc_proto;
76f1ef42 1130 ictx->pad_mouse = false;
6718e8ad
JW
1131
1132out:
23ef710e
JW
1133 if (unlock)
1134 mutex_unlock(&ictx->lock);
1135
6718e8ad 1136 return retval;
21677cfc
JW
1137}
1138
255940e6 1139/*
21677cfc
JW
1140 * The directional pad behaves a bit differently, depending on whether this is
1141 * one of the older ffdc devices or a newer device. Newer devices appear to
1142 * have a higher resolution matrix for more precise mouse movement, but it
1143 * makes things overly sensitive in keyboard mode, so we do some interesting
1144 * contortions to make it less touchy. Older devices run through the same
1145 * routine with shorter timeout and a smaller threshold.
1146 */
1147static int stabilize(int a, int b, u16 timeout, u16 threshold)
1148{
1edba648
CZ
1149 ktime_t ct;
1150 static ktime_t prev_time;
1151 static ktime_t hit_time;
21677cfc
JW
1152 static int x, y, prev_result, hits;
1153 int result = 0;
1edba648 1154 long msec, msec_hit;
21677cfc 1155
1edba648
CZ
1156 ct = ktime_get();
1157 msec = ktime_ms_delta(ct, prev_time);
1158 msec_hit = ktime_ms_delta(ct, hit_time);
21677cfc
JW
1159
1160 if (msec > 100) {
1161 x = 0;
1162 y = 0;
1163 hits = 0;
1164 }
1165
1166 x += a;
1167 y += b;
1168
1169 prev_time = ct;
1170
1171 if (abs(x) > threshold || abs(y) > threshold) {
1172 if (abs(y) > abs(x))
1173 result = (y > 0) ? 0x7F : 0x80;
1174 else
1175 result = (x > 0) ? 0x7F00 : 0x8000;
1176
1177 x = 0;
1178 y = 0;
1179
1180 if (result == prev_result) {
1181 hits++;
1182
1183 if (hits > 3) {
1184 switch (result) {
1185 case 0x7F:
1186 y = 17 * threshold / 30;
1187 break;
1188 case 0x80:
1189 y -= 17 * threshold / 30;
1190 break;
1191 case 0x7F00:
1192 x = 17 * threshold / 30;
1193 break;
1194 case 0x8000:
1195 x -= 17 * threshold / 30;
1196 break;
1197 }
1198 }
1199
1200 if (hits == 2 && msec_hit < timeout) {
1201 result = 0;
1202 hits = 1;
1203 }
1204 } else {
1205 prev_result = result;
1206 hits = 1;
1207 hit_time = ct;
1208 }
1209 }
1210
1211 return result;
1212}
1213
eaf2bcc9 1214static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
21677cfc 1215{
21677cfc
JW
1216 u32 keycode;
1217 u32 release;
1218 bool is_release_code = false;
1219
1220 /* Look for the initial press of a button */
ca86674b 1221 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
eaf2bcc9
DH
1222 ictx->rc_toggle = 0x0;
1223 ictx->rc_scancode = scancode;
21677cfc
JW
1224
1225 /* Look for the release of a button */
1226 if (keycode == KEY_RESERVED) {
1227 release = scancode & ~0x4000;
ca86674b 1228 keycode = rc_g_keycode_from_table(ictx->rdev, release);
21677cfc
JW
1229 if (keycode != KEY_RESERVED)
1230 is_release_code = true;
1231 }
1232
1233 ictx->release_code = is_release_code;
1234
1235 return keycode;
1236}
1237
eaf2bcc9 1238static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
21677cfc 1239{
21677cfc
JW
1240 u32 keycode;
1241
1242#define MCE_KEY_MASK 0x7000
1243#define MCE_TOGGLE_BIT 0x8000
1244
1245 /*
1246 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1247 * (the toggle bit flipping between alternating key presses), while
1248 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1249 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1250 * but we can't or them into all codes, as some keys are decoded in
1251 * a different way w/o the same use of the toggle bit...
1252 */
eaf2bcc9 1253 if (scancode & 0x80000000)
21677cfc
JW
1254 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1255
eaf2bcc9 1256 ictx->rc_scancode = scancode;
ca86674b 1257 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
eaf2bcc9
DH
1258
1259 /* not used in mce mode, but make sure we know its false */
1260 ictx->release_code = false;
21677cfc
JW
1261
1262 return keycode;
1263}
1264
0d8053f2 1265static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
21677cfc
JW
1266{
1267 int i;
083e4721 1268 u32 keycode = KEY_RESERVED;
0d8053f2 1269 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
21677cfc 1270
0d8053f2
UE
1271 for (i = 0; key_table[i].hw_code != 0; i++) {
1272 if (key_table[i].hw_code == (code | 0xffee)) {
1273 keycode = key_table[i].keycode;
21677cfc 1274 break;
083e4721
JW
1275 }
1276 }
0d8053f2 1277 ictx->release_code = false;
21677cfc
JW
1278 return keycode;
1279}
1280
1281static bool imon_mouse_event(struct imon_context *ictx,
1282 unsigned char *buf, int len)
1283{
24dec5da 1284 signed char rel_x = 0x00, rel_y = 0x00;
21677cfc 1285 u8 right_shift = 1;
f789bf40 1286 bool mouse_input = true;
21677cfc 1287 int dir = 0;
693508df
JW
1288 unsigned long flags;
1289
1290 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1291
1292 /* newer iMON device PAD or mouse button */
1293 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1294 rel_x = buf[2];
1295 rel_y = buf[3];
1296 right_shift = 1;
1297 /* 0xffdc iMON PAD or mouse button input */
1298 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1299 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1300 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1301 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1302 if (buf[0] & 0x02)
1303 rel_x |= ~0x0f;
1304 rel_x = rel_x + rel_x / 2;
1305 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1306 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1307 if (buf[0] & 0x01)
1308 rel_y |= ~0x0f;
1309 rel_y = rel_y + rel_y / 2;
1310 right_shift = 2;
1311 /* some ffdc devices decode mouse buttons differently... */
1312 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1313 right_shift = 2;
1314 /* ch+/- buttons, which we use for an emulated scroll wheel */
1315 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1316 dir = 1;
1317 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1318 dir = -1;
1319 } else
f789bf40 1320 mouse_input = false;
21677cfc 1321
693508df
JW
1322 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1323
21677cfc
JW
1324 if (mouse_input) {
1325 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1326
1327 if (dir) {
1328 input_report_rel(ictx->idev, REL_WHEEL, dir);
1329 } else if (rel_x || rel_y) {
1330 input_report_rel(ictx->idev, REL_X, rel_x);
1331 input_report_rel(ictx->idev, REL_Y, rel_y);
1332 } else {
1333 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1334 input_report_key(ictx->idev, BTN_RIGHT,
1335 buf[1] >> right_shift & 0x1);
1336 }
1337 input_sync(ictx->idev);
693508df 1338 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc 1339 ictx->last_keycode = ictx->kc;
693508df 1340 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1341 }
1342
1343 return mouse_input;
1344}
1345
1346static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1347{
1348 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1349 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1350 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1351 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1352 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1353 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1354 input_sync(ictx->touch);
1355}
1356
1357static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1358{
1359 int dir = 0;
24dec5da 1360 signed char rel_x = 0x00, rel_y = 0x00;
21677cfc 1361 u16 timeout, threshold;
eaf2bcc9 1362 u32 scancode = KEY_RESERVED;
693508df 1363 unsigned long flags;
21677cfc
JW
1364
1365 /*
1366 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1367 * contain a position coordinate (x,y), with each component ranging
1368 * from -14 to 14. We want to down-sample this to only 4 discrete values
1369 * for up/down/left/right arrow keys. Also, when you get too close to
25985edc 1370 * diagonals, it has a tendency to jump back and forth, so lets try to
21677cfc
JW
1371 * ignore when they get too close.
1372 */
1373 if (ictx->product != 0xffdc) {
1374 /* first, pad to 8 bytes so it conforms with everything else */
1375 buf[5] = buf[6] = buf[7] = 0;
1376 timeout = 500; /* in msecs */
1377 /* (2*threshold) x (2*threshold) square */
1378 threshold = pad_thresh ? pad_thresh : 28;
1379 rel_x = buf[2];
1380 rel_y = buf[3];
1381
2525fdcb 1382 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
21677cfc
JW
1383 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1384 dir = stabilize((int)rel_x, (int)rel_y,
1385 timeout, threshold);
1386 if (!dir) {
693508df
JW
1387 spin_lock_irqsave(&ictx->kc_lock,
1388 flags);
21677cfc 1389 ictx->kc = KEY_UNKNOWN;
693508df
JW
1390 spin_unlock_irqrestore(&ictx->kc_lock,
1391 flags);
21677cfc
JW
1392 return;
1393 }
1394 buf[2] = dir & 0xFF;
1395 buf[3] = (dir >> 8) & 0xFF;
d778d258 1396 scancode = be32_to_cpu(*((__be32 *)buf));
21677cfc
JW
1397 }
1398 } else {
eaf2bcc9
DH
1399 /*
1400 * Hack alert: instead of using keycodes, we have
1401 * to use hard-coded scancodes here...
1402 */
21677cfc
JW
1403 if (abs(rel_y) > abs(rel_x)) {
1404 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1405 buf[3] = 0;
eaf2bcc9
DH
1406 if (rel_y > 0)
1407 scancode = 0x01007f00; /* KEY_DOWN */
1408 else
1409 scancode = 0x01008000; /* KEY_UP */
21677cfc
JW
1410 } else {
1411 buf[2] = 0;
1412 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
eaf2bcc9
DH
1413 if (rel_x > 0)
1414 scancode = 0x0100007f; /* KEY_RIGHT */
1415 else
1416 scancode = 0x01000080; /* KEY_LEFT */
21677cfc
JW
1417 }
1418 }
1419
1420 /*
1421 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1422 * device (15c2:ffdc). The remote generates various codes from
1423 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1424 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1425 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
39c1cb2b 1426 * reversed endianness. Extract direction from buffer, rotate endianness,
21677cfc
JW
1427 * adjust sign and feed the values into stabilize(). The resulting codes
1428 * will be 0x01008000, 0x01007F00, which match the newer devices.
1429 */
1430 } else {
1431 timeout = 10; /* in msecs */
1432 /* (2*threshold) x (2*threshold) square */
1433 threshold = pad_thresh ? pad_thresh : 15;
1434
1435 /* buf[1] is x */
1436 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1437 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1438 if (buf[0] & 0x02)
1439 rel_x |= ~0x10+1;
1440 /* buf[2] is y */
1441 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1442 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1443 if (buf[0] & 0x01)
1444 rel_y |= ~0x10+1;
1445
1446 buf[0] = 0x01;
1447 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1448
2525fdcb 1449 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
21677cfc
JW
1450 dir = stabilize((int)rel_x, (int)rel_y,
1451 timeout, threshold);
1452 if (!dir) {
693508df 1453 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc 1454 ictx->kc = KEY_UNKNOWN;
693508df 1455 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1456 return;
1457 }
1458 buf[2] = dir & 0xFF;
1459 buf[3] = (dir >> 8) & 0xFF;
d778d258 1460 scancode = be32_to_cpu(*((__be32 *)buf));
21677cfc 1461 } else {
eaf2bcc9
DH
1462 /*
1463 * Hack alert: instead of using keycodes, we have
1464 * to use hard-coded scancodes here...
1465 */
21677cfc
JW
1466 if (abs(rel_y) > abs(rel_x)) {
1467 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1468 buf[3] = 0;
eaf2bcc9
DH
1469 if (rel_y > 0)
1470 scancode = 0x01007f00; /* KEY_DOWN */
1471 else
1472 scancode = 0x01008000; /* KEY_UP */
21677cfc
JW
1473 } else {
1474 buf[2] = 0;
1475 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
eaf2bcc9
DH
1476 if (rel_x > 0)
1477 scancode = 0x0100007f; /* KEY_RIGHT */
1478 else
1479 scancode = 0x01000080; /* KEY_LEFT */
21677cfc
JW
1480 }
1481 }
1482 }
eaf2bcc9 1483
693508df
JW
1484 if (scancode) {
1485 spin_lock_irqsave(&ictx->kc_lock, flags);
eaf2bcc9 1486 ictx->kc = imon_remote_key_lookup(ictx, scancode);
693508df
JW
1487 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1488 }
21677cfc
JW
1489}
1490
255940e6 1491/*
eaf2bcc9
DH
1492 * figure out if these is a press or a release. We don't actually
1493 * care about repeats, as those will be auto-generated within the IR
1494 * subsystem for repeating scancodes.
1495 */
21677cfc
JW
1496static int imon_parse_press_type(struct imon_context *ictx,
1497 unsigned char *buf, u8 ktype)
1498{
1499 int press_type = 0;
693508df
JW
1500 unsigned long flags;
1501
1502 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1503
1504 /* key release of 0x02XXXXXX key */
1505 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1506 ictx->kc = ictx->last_keycode;
1507
1508 /* mouse button release on (some) 0xffdc devices */
1509 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1510 buf[2] == 0x81 && buf[3] == 0xb7)
1511 ictx->kc = ictx->last_keycode;
1512
1513 /* mouse button release on (some other) 0xffdc devices */
1514 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1515 buf[2] == 0x81 && buf[3] == 0xb7)
1516 ictx->kc = ictx->last_keycode;
1517
eaf2bcc9 1518 /* mce-specific button handling, no keyup events */
21677cfc 1519 else if (ktype == IMON_KEY_MCE) {
eaf2bcc9
DH
1520 ictx->rc_toggle = buf[2];
1521 press_type = 1;
21677cfc
JW
1522
1523 /* incoherent or irrelevant data */
1524 } else if (ictx->kc == KEY_RESERVED)
1525 press_type = -EINVAL;
1526
1527 /* key release of 0xXXXXXXb7 key */
1528 else if (ictx->release_code)
1529 press_type = 0;
1530
1531 /* this is a button press */
1532 else
1533 press_type = 1;
1534
693508df
JW
1535 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1536
21677cfc
JW
1537 return press_type;
1538}
1539
255940e6 1540/*
21677cfc
JW
1541 * Process the incoming packet
1542 */
1b450f21 1543static void imon_incoming_packet(struct imon_context *ictx,
21677cfc
JW
1544 struct urb *urb, int intf)
1545{
1546 int len = urb->actual_length;
1547 unsigned char *buf = urb->transfer_buffer;
1548 struct device *dev = ictx->dev;
693508df 1549 unsigned long flags;
21677cfc 1550 u32 kc;
eaf2bcc9 1551 u64 scancode;
21677cfc 1552 int press_type = 0;
1edba648
CZ
1553 long msec;
1554 ktime_t t;
1555 static ktime_t prev_time;
eaf2bcc9 1556 u8 ktype;
21677cfc 1557
21677cfc 1558 /* filter out junk data on the older 0xffdc imon devices */
bbe4690f 1559 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
21677cfc
JW
1560 return;
1561
1562 /* Figure out what key was pressed */
21677cfc 1563 if (len == 8 && buf[7] == 0xee) {
d778d258 1564 scancode = be64_to_cpu(*((__be64 *)buf));
21677cfc 1565 ktype = IMON_KEY_PANEL;
0d8053f2 1566 kc = imon_panel_key_lookup(ictx, scancode);
6ddc2be5 1567 ictx->release_code = false;
21677cfc 1568 } else {
d778d258 1569 scancode = be32_to_cpu(*((__be32 *)buf));
6d741bfe 1570 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
eaf2bcc9 1571 ktype = IMON_KEY_IMON;
21677cfc
JW
1572 if (buf[0] == 0x80)
1573 ktype = IMON_KEY_MCE;
eaf2bcc9
DH
1574 kc = imon_mce_key_lookup(ictx, scancode);
1575 } else {
1576 ktype = IMON_KEY_IMON;
1577 kc = imon_remote_key_lookup(ictx, scancode);
1578 }
21677cfc
JW
1579 }
1580
693508df 1581 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1582 /* keyboard/mouse mode toggle button */
1583 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1584 ictx->last_keycode = kc;
1585 if (!nomouse) {
bd7e31bb 1586 ictx->pad_mouse = !ictx->pad_mouse;
21677cfc
JW
1587 dev_dbg(dev, "toggling to %s mode\n",
1588 ictx->pad_mouse ? "mouse" : "keyboard");
693508df 1589 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1590 return;
1591 } else {
76f1ef42 1592 ictx->pad_mouse = false;
21677cfc
JW
1593 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1594 }
1595 }
1596
1597 ictx->kc = kc;
693508df 1598 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1599
1600 /* send touchscreen events through input subsystem if touchpad data */
1601 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1602 buf[7] == 0x86) {
1603 imon_touch_event(ictx, buf);
eaf2bcc9 1604 return;
21677cfc
JW
1605
1606 /* look for mouse events with pad in mouse mode */
1607 } else if (ictx->pad_mouse) {
1608 if (imon_mouse_event(ictx, buf, len))
1609 return;
1610 }
1611
1612 /* Now for some special handling to convert pad input to arrow keys */
1613 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1614 ((len == 8) && (buf[0] & 0x40) &&
1615 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1616 len = 8;
1617 imon_pad_to_keys(ictx, buf);
21677cfc
JW
1618 }
1619
1620 if (debug) {
832d40c0
MCC
1621 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1622 intf, len, buf);
21677cfc
JW
1623 }
1624
1625 press_type = imon_parse_press_type(ictx, buf, ktype);
1626 if (press_type < 0)
1627 goto not_input_data;
1628
eaf2bcc9
DH
1629 if (ktype != IMON_KEY_PANEL) {
1630 if (press_type == 0)
ca86674b 1631 rc_keyup(ictx->rdev);
eaf2bcc9 1632 else {
2525fdcb
SY
1633 enum rc_proto proto;
1634
1635 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1636 proto = RC_PROTO_RC6_MCE;
1637 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1638 proto = RC_PROTO_IMON;
1639 else
1640 return;
1641
1642 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1643 ictx->rc_toggle);
1644
693508df 1645 spin_lock_irqsave(&ictx->kc_lock, flags);
eaf2bcc9 1646 ictx->last_keycode = ictx->kc;
693508df 1647 spin_unlock_irqrestore(&ictx->kc_lock, flags);
eaf2bcc9
DH
1648 }
1649 return;
1650 }
21677cfc 1651
eaf2bcc9 1652 /* Only panel type events left to process now */
693508df
JW
1653 spin_lock_irqsave(&ictx->kc_lock, flags);
1654
1edba648 1655 t = ktime_get();
eaf2bcc9
DH
1656 /* KEY_MUTE repeats from knob need to be suppressed */
1657 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
1edba648 1658 msec = ktime_ms_delta(t, prev_time);
428cc763 1659 if (msec < ictx->idev->rep[REP_DELAY]) {
693508df 1660 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc 1661 return;
693508df 1662 }
21677cfc 1663 }
94215ccd 1664 prev_time = t;
693508df 1665 kc = ictx->kc;
21677cfc 1666
693508df 1667 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc 1668
428cc763
JW
1669 input_report_key(ictx->idev, kc, press_type);
1670 input_sync(ictx->idev);
21677cfc 1671
eaf2bcc9 1672 /* panel keys don't generate a release */
428cc763
JW
1673 input_report_key(ictx->idev, kc, 0);
1674 input_sync(ictx->idev);
21677cfc 1675
94215ccd 1676 spin_lock_irqsave(&ictx->kc_lock, flags);
693508df 1677 ictx->last_keycode = kc;
94215ccd 1678 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1679
1680 return;
1681
21677cfc
JW
1682not_input_data:
1683 if (len != 8) {
25ec587c
MCC
1684 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1685 __func__, len, intf);
21677cfc
JW
1686 return;
1687 }
1688
1689 /* iMON 2.4G associate frame */
1690 if (buf[0] == 0x00 &&
1691 buf[2] == 0xFF && /* REFID */
1692 buf[3] == 0xFF &&
1693 buf[4] == 0xFF &&
1694 buf[5] == 0xFF && /* iMON 2.4G */
1695 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1696 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1697 dev_warn(dev, "%s: remote associated refid=%02X\n",
1698 __func__, buf[1]);
f789bf40 1699 ictx->rf_isassociating = false;
21677cfc
JW
1700 }
1701}
1702
255940e6 1703/*
21677cfc
JW
1704 * Callback function for USB core API: receive data
1705 */
1706static void usb_rx_callback_intf0(struct urb *urb)
1707{
1708 struct imon_context *ictx;
1709 int intfnum = 0;
1710
1711 if (!urb)
1712 return;
1713
1714 ictx = (struct imon_context *)urb->context;
8791d63a 1715 if (!ictx)
21677cfc
JW
1716 return;
1717
8791d63a
JW
1718 /*
1719 * if we get a callback before we're done configuring the hardware, we
1720 * can't yet process the data, as there's nowhere to send it, but we
1721 * still need to submit a new rx URB to avoid wedging the hardware
1722 */
1723 if (!ictx->dev_present_intf0)
1724 goto out;
1725
21677cfc
JW
1726 switch (urb->status) {
1727 case -ENOENT: /* usbcore unlink successful! */
1728 return;
1729
1730 case -ESHUTDOWN: /* transport endpoint was shut down */
1731 break;
1732
1733 case 0:
1b450f21 1734 imon_incoming_packet(ictx, urb, intfnum);
21677cfc
JW
1735 break;
1736
1737 default:
1738 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1739 __func__, urb->status);
1740 break;
1741 }
1742
8791d63a 1743out:
21677cfc
JW
1744 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1745}
1746
1747static void usb_rx_callback_intf1(struct urb *urb)
1748{
1749 struct imon_context *ictx;
1750 int intfnum = 1;
1751
1752 if (!urb)
1753 return;
1754
1755 ictx = (struct imon_context *)urb->context;
8791d63a 1756 if (!ictx)
21677cfc
JW
1757 return;
1758
8791d63a
JW
1759 /*
1760 * if we get a callback before we're done configuring the hardware, we
1761 * can't yet process the data, as there's nowhere to send it, but we
1762 * still need to submit a new rx URB to avoid wedging the hardware
1763 */
1764 if (!ictx->dev_present_intf1)
1765 goto out;
1766
21677cfc
JW
1767 switch (urb->status) {
1768 case -ENOENT: /* usbcore unlink successful! */
1769 return;
1770
1771 case -ESHUTDOWN: /* transport endpoint was shut down */
1772 break;
1773
1774 case 0:
1b450f21 1775 imon_incoming_packet(ictx, urb, intfnum);
21677cfc
JW
1776 break;
1777
1778 default:
1779 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1780 __func__, urb->status);
1781 break;
1782 }
1783
8791d63a 1784out:
21677cfc
JW
1785 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1786}
1787
04292fc0
JW
1788/*
1789 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1790 * devices, and all of them constantly spew interrupts, even when there
1791 * is no actual data to report. However, byte 6 of this buffer looks like
1792 * its unique across device variants, so we're trying to key off that to
1793 * figure out which display type (if any) and what IR protocol the device
1794 * actually supports. These devices have their IR protocol hard-coded into
1795 * their firmware, they can't be changed on the fly like the newer hardware.
1796 */
1797static void imon_get_ffdc_type(struct imon_context *ictx)
1798{
1799 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1800 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
2525fdcb 1801 u64 allowed_protos = RC_PROTO_BIT_IMON;
04292fc0
JW
1802
1803 switch (ffdc_cfg_byte) {
1804 /* iMON Knob, no display, iMON IR + vol knob */
1805 case 0x21:
1806 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1807 ictx->display_supported = false;
1808 break;
1809 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1810 case 0x4e:
1811 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1812 ictx->display_supported = false;
1813 ictx->rf_device = true;
1814 break;
1815 /* iMON VFD, no IR (does have vol knob tho) */
1816 case 0x35:
1817 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1818 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1819 break;
1820 /* iMON VFD, iMON IR */
1821 case 0x24:
6d4a36d1 1822 case 0x30:
04292fc0
JW
1823 case 0x85:
1824 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1825 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1826 break;
1827 /* iMON VFD, MCE IR */
443b3919 1828 case 0x46:
842071c9 1829 case 0x7e:
04292fc0
JW
1830 case 0x9e:
1831 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1832 detected_display_type = IMON_DISPLAY_TYPE_VFD;
6d741bfe 1833 allowed_protos = RC_PROTO_BIT_RC6_MCE;
04292fc0
JW
1834 break;
1835 /* iMON LCD, MCE IR */
1836 case 0x9f:
1837 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1838 detected_display_type = IMON_DISPLAY_TYPE_LCD;
6d741bfe 1839 allowed_protos = RC_PROTO_BIT_RC6_MCE;
04292fc0 1840 break;
6a489f76
SY
1841 /* no display, iMON IR */
1842 case 0x26:
1843 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1844 ictx->display_supported = false;
1845 break;
04292fc0 1846 default:
25ec587c 1847 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
04292fc0 1848 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2525fdcb
SY
1849 /*
1850 * We don't know which one it is, allow user to set the
1851 * RC6 one from userspace if IMON wasn't correct.
1852 */
6d741bfe 1853 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
04292fc0
JW
1854 break;
1855 }
1856
1857 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1858
1859 ictx->display_type = detected_display_type;
6d741bfe 1860 ictx->rc_proto = allowed_protos;
04292fc0
JW
1861}
1862
1863static void imon_set_display_type(struct imon_context *ictx)
1864{
1865 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1866
1867 /*
1868 * Try to auto-detect the type of display if the user hasn't set
1869 * it by hand via the display_type modparam. Default is VFD.
1870 */
1871
1872 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1873 switch (ictx->product) {
1874 case 0xffdc:
1875 /* set in imon_get_ffdc_type() */
1876 configured_display_type = ictx->display_type;
1877 break;
1878 case 0x0034:
1879 case 0x0035:
1880 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1881 break;
1882 case 0x0038:
1883 case 0x0039:
1884 case 0x0045:
1885 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1886 break;
1887 case 0x003c:
1888 case 0x0041:
1889 case 0x0042:
1890 case 0x0043:
1891 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1892 ictx->display_supported = false;
1893 break;
1894 case 0x0036:
1895 case 0x0044:
1896 default:
1897 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1898 break;
1899 }
1900 } else {
1901 configured_display_type = display_type;
1902 if (display_type == IMON_DISPLAY_TYPE_NONE)
1903 ictx->display_supported = false;
1904 else
1905 ictx->display_supported = true;
25ec587c
MCC
1906 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1907 __func__, display_type);
04292fc0
JW
1908 }
1909
1910 ictx->display_type = configured_display_type;
1911}
1912
d8b4b582 1913static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
eaf2bcc9 1914{
d8b4b582 1915 struct rc_dev *rdev;
eaf2bcc9 1916 int ret;
c25895c7
CIK
1917 static const unsigned char fp_packet[] = {
1918 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
eaf2bcc9 1919
1b450f21 1920 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
d8b4b582 1921 if (!rdev) {
eaf2bcc9
DH
1922 dev_err(ictx->dev, "remote control dev allocation failed\n");
1923 goto out;
1924 }
1925
1926 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1927 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1928 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1929 sizeof(ictx->phys_rdev));
1930 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1931
518f4b26 1932 rdev->device_name = ictx->name_rdev;
d8b4b582
DH
1933 rdev->input_phys = ictx->phys_rdev;
1934 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
eaf2bcc9 1935 rdev->dev.parent = ictx->dev;
eaf2bcc9 1936
d8b4b582 1937 rdev->priv = ictx;
1b450f21 1938 /* iMON PAD or MCE */
2525fdcb 1939 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
d8b4b582
DH
1940 rdev->change_protocol = imon_ir_change_protocol;
1941 rdev->driver_name = MOD_NAME;
eaf2bcc9 1942
04292fc0
JW
1943 /* Enable front-panel buttons and/or knobs */
1944 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1945 ret = send_packet(ictx);
1946 /* Not fatal, but warn about it */
1947 if (ret)
1948 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1949
7d2edfc2 1950 if (ictx->product == 0xffdc) {
04292fc0 1951 imon_get_ffdc_type(ictx);
6d741bfe 1952 rdev->allowed_protocols = ictx->rc_proto;
7d2edfc2 1953 }
04292fc0
JW
1954
1955 imon_set_display_type(ictx);
1956
1b450f21 1957 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
7d2edfc2
JW
1958 rdev->map_name = RC_MAP_IMON_MCE;
1959 else
1960 rdev->map_name = RC_MAP_IMON_PAD;
1961
d8b4b582 1962 ret = rc_register_device(rdev);
eaf2bcc9
DH
1963 if (ret < 0) {
1964 dev_err(ictx->dev, "remote input dev register failed\n");
1965 goto out;
1966 }
1967
1968 return rdev;
1969
1970out:
d8b4b582 1971 rc_free_device(rdev);
eaf2bcc9
DH
1972 return NULL;
1973}
1974
21677cfc
JW
1975static struct input_dev *imon_init_idev(struct imon_context *ictx)
1976{
0d8053f2 1977 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
21677cfc 1978 struct input_dev *idev;
21677cfc 1979 int ret, i;
21677cfc
JW
1980
1981 idev = input_allocate_device();
da4a7339 1982 if (!idev)
eaf2bcc9 1983 goto out;
21677cfc 1984
21677cfc 1985 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
eaf2bcc9
DH
1986 "iMON Panel, Knob and Mouse(%04x:%04x)",
1987 ictx->vendor, ictx->product);
21677cfc
JW
1988 idev->name = ictx->name_idev;
1989
1990 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1991 sizeof(ictx->phys_idev));
eaf2bcc9 1992 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
21677cfc
JW
1993 idev->phys = ictx->phys_idev;
1994
db190fc1 1995 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
21677cfc
JW
1996
1997 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1998 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1999 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2000 BIT_MASK(REL_WHEEL);
2001
2002 /* panel and/or knob code support */
0d8053f2
UE
2003 for (i = 0; key_table[i].hw_code != 0; i++) {
2004 u32 kc = key_table[i].keycode;
21677cfc
JW
2005 __set_bit(kc, idev->keybit);
2006 }
2007
21677cfc
JW
2008 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2009 idev->dev.parent = ictx->dev;
eaf2bcc9 2010 input_set_drvdata(idev, ictx);
21677cfc 2011
eaf2bcc9 2012 ret = input_register_device(idev);
21677cfc 2013 if (ret < 0) {
eaf2bcc9
DH
2014 dev_err(ictx->dev, "input dev register failed\n");
2015 goto out;
21677cfc
JW
2016 }
2017
2018 return idev;
2019
eaf2bcc9 2020out:
21677cfc 2021 input_free_device(idev);
21677cfc
JW
2022 return NULL;
2023}
2024
2025static struct input_dev *imon_init_touch(struct imon_context *ictx)
2026{
2027 struct input_dev *touch;
2028 int ret;
2029
2030 touch = input_allocate_device();
da4a7339 2031 if (!touch)
21677cfc 2032 goto touch_alloc_failed;
21677cfc
JW
2033
2034 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2035 "iMON USB Touchscreen (%04x:%04x)",
2036 ictx->vendor, ictx->product);
2037 touch->name = ictx->name_touch;
2038
2039 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2040 sizeof(ictx->phys_touch));
eaf2bcc9 2041 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
21677cfc
JW
2042 touch->phys = ictx->phys_touch;
2043
2044 touch->evbit[0] =
2045 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2046 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2047 BIT_MASK(BTN_TOUCH);
2048 input_set_abs_params(touch, ABS_X,
2049 0x00, 0xfff, 0, 0);
2050 input_set_abs_params(touch, ABS_Y,
2051 0x00, 0xfff, 0, 0);
2052
2053 input_set_drvdata(touch, ictx);
2054
2055 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2056 touch->dev.parent = ictx->dev;
2057 ret = input_register_device(touch);
2058 if (ret < 0) {
2059 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2060 goto touch_register_failed;
2061 }
2062
2063 return touch;
2064
2065touch_register_failed:
aeb35ebc 2066 input_free_device(touch);
21677cfc
JW
2067
2068touch_alloc_failed:
2069 return NULL;
2070}
2071
2072static bool imon_find_endpoints(struct imon_context *ictx,
2073 struct usb_host_interface *iface_desc)
2074{
2075 struct usb_endpoint_descriptor *ep;
2076 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2077 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2078 int ifnum = iface_desc->desc.bInterfaceNumber;
2079 int num_endpts = iface_desc->desc.bNumEndpoints;
2080 int i, ep_dir, ep_type;
f789bf40
JW
2081 bool ir_ep_found = false;
2082 bool display_ep_found = false;
2083 bool tx_control = false;
21677cfc
JW
2084
2085 /*
2086 * Scan the endpoint list and set:
2087 * first input endpoint = IR endpoint
2088 * first output endpoint = display endpoint
2089 */
2090 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2091 ep = &iface_desc->endpoint[i].desc;
2092 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
9408d8f0 2093 ep_type = usb_endpoint_type(ep);
21677cfc
JW
2094
2095 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2096 ep_type == USB_ENDPOINT_XFER_INT) {
2097
2098 rx_endpoint = ep;
f789bf40 2099 ir_ep_found = true;
21677cfc
JW
2100 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2101
2102 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2103 ep_type == USB_ENDPOINT_XFER_INT) {
2104 tx_endpoint = ep;
f789bf40 2105 display_ep_found = true;
21677cfc
JW
2106 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2107 }
2108 }
2109
2110 if (ifnum == 0) {
2111 ictx->rx_endpoint_intf0 = rx_endpoint;
2112 /*
2113 * tx is used to send characters to lcd/vfd, associate RF
2114 * remotes, set IR protocol, and maybe more...
2115 */
2116 ictx->tx_endpoint = tx_endpoint;
2117 } else {
2118 ictx->rx_endpoint_intf1 = rx_endpoint;
2119 }
2120
2121 /*
2122 * If we didn't find a display endpoint, this is probably one of the
2123 * newer iMON devices that use control urb instead of interrupt
2124 */
2125 if (!display_ep_found) {
f789bf40
JW
2126 tx_control = true;
2127 display_ep_found = true;
25ec587c
MCC
2128 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2129 __func__);
21677cfc
JW
2130 }
2131
2132 /*
2133 * Some iMON receivers have no display. Unfortunately, it seems
2134 * that SoundGraph recycles device IDs between devices both with
2135 * and without... :\
2136 */
2137 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
f789bf40 2138 display_ep_found = false;
21677cfc
JW
2139 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2140 }
2141
2142 /*
2143 * iMON Touch devices have a VGA touchscreen, but no "display", as
2144 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2145 */
2146 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
f789bf40 2147 display_ep_found = false;
21677cfc
JW
2148 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2149 }
2150
2151 /* Input endpoint is mandatory */
2152 if (!ir_ep_found)
e2302501 2153 pr_err("no valid input (IR) endpoint found\n");
21677cfc
JW
2154
2155 ictx->tx_control = tx_control;
2156
2157 if (display_ep_found)
2158 ictx->display_supported = true;
2159
2160 return ir_ep_found;
2161
2162}
2163
f7d141b3
KB
2164static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2165 const struct usb_device_id *id)
21677cfc
JW
2166{
2167 struct imon_context *ictx;
2168 struct urb *rx_urb;
2169 struct urb *tx_urb;
2170 struct device *dev = &intf->dev;
2171 struct usb_host_interface *iface_desc;
1f71baef 2172 int ret = -ENOMEM;
21677cfc 2173
a8c779eb 2174 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
3e70b256 2175 if (!ictx)
21677cfc 2176 goto exit;
3e70b256 2177
21677cfc 2178 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2179 if (!rx_urb)
21677cfc 2180 goto rx_urb_alloc_failed;
21677cfc 2181 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2182 if (!tx_urb)
21677cfc 2183 goto tx_urb_alloc_failed;
21677cfc
JW
2184
2185 mutex_init(&ictx->lock);
693508df 2186 spin_lock_init(&ictx->kc_lock);
21677cfc
JW
2187
2188 mutex_lock(&ictx->lock);
2189
2190 ictx->dev = dev;
2191 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
21677cfc
JW
2192 ictx->rx_urb_intf0 = rx_urb;
2193 ictx->tx_urb = tx_urb;
bbe4690f 2194 ictx->rf_device = false;
21677cfc 2195
7c073fff
DW
2196 init_completion(&ictx->tx.finished);
2197
21677cfc
JW
2198 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2199 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2200
0d8053f2
UE
2201 /* save drive info for later accessing the panel/knob key table */
2202 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
f7d141b3 2203 /* default send_packet delay is 5ms but some devices need more */
0d8053f2
UE
2204 ictx->send_packet_delay = ictx->dev_descr->flags &
2205 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
f7d141b3 2206
1f71baef 2207 ret = -ENODEV;
21677cfc 2208 iface_desc = intf->cur_altsetting;
1f71baef 2209 if (!imon_find_endpoints(ictx, iface_desc)) {
21677cfc 2210 goto find_endpoint_failed;
1f71baef 2211 }
21677cfc 2212
21677cfc
JW
2213 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2214 usb_rcvintpipe(ictx->usbdev_intf0,
2215 ictx->rx_endpoint_intf0->bEndpointAddress),
2216 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2217 usb_rx_callback_intf0, ictx,
2218 ictx->rx_endpoint_intf0->bInterval);
2219
2220 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2221 if (ret) {
e2302501 2222 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
21677cfc
JW
2223 goto urb_submit_failed;
2224 }
2225
9ad77eb5
JW
2226 ictx->idev = imon_init_idev(ictx);
2227 if (!ictx->idev) {
2228 dev_err(dev, "%s: input device setup failed\n", __func__);
2229 goto idev_setup_failed;
2230 }
2231
2232 ictx->rdev = imon_init_rdev(ictx);
2233 if (!ictx->rdev) {
2234 dev_err(dev, "%s: rc device setup failed\n", __func__);
2235 goto rdev_setup_failed;
2236 }
2237
6f6b90c9
JW
2238 ictx->dev_present_intf0 = true;
2239
23ef710e 2240 mutex_unlock(&ictx->lock);
21677cfc
JW
2241 return ictx;
2242
eaf2bcc9
DH
2243rdev_setup_failed:
2244 input_unregister_device(ictx->idev);
21677cfc 2245idev_setup_failed:
9ad77eb5
JW
2246 usb_kill_urb(ictx->rx_urb_intf0);
2247urb_submit_failed:
21677cfc 2248find_endpoint_failed:
e87cb470 2249 usb_put_dev(ictx->usbdev_intf0);
21677cfc
JW
2250 mutex_unlock(&ictx->lock);
2251 usb_free_urb(tx_urb);
2252tx_urb_alloc_failed:
2253 usb_free_urb(rx_urb);
2254rx_urb_alloc_failed:
2255 kfree(ictx);
2256exit:
2257 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2258
2259 return NULL;
2260}
2261
2262static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2263 struct imon_context *ictx)
2264{
2265 struct urb *rx_urb;
2266 struct usb_host_interface *iface_desc;
1f71baef 2267 int ret = -ENOMEM;
21677cfc
JW
2268
2269 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2270 if (!rx_urb)
21677cfc 2271 goto rx_urb_alloc_failed;
21677cfc
JW
2272
2273 mutex_lock(&ictx->lock);
2274
2275 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
b17ec78a 2276 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
21677cfc
JW
2277 }
2278
2279 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
21677cfc
JW
2280 ictx->rx_urb_intf1 = rx_urb;
2281
1f71baef 2282 ret = -ENODEV;
21677cfc
JW
2283 iface_desc = intf->cur_altsetting;
2284 if (!imon_find_endpoints(ictx, iface_desc))
2285 goto find_endpoint_failed;
2286
2287 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2288 ictx->touch = imon_init_touch(ictx);
2289 if (!ictx->touch)
2290 goto touch_setup_failed;
2291 } else
2292 ictx->touch = NULL;
2293
2294 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2295 usb_rcvintpipe(ictx->usbdev_intf1,
2296 ictx->rx_endpoint_intf1->bEndpointAddress),
2297 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2298 usb_rx_callback_intf1, ictx,
2299 ictx->rx_endpoint_intf1->bInterval);
2300
2301 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2302
2303 if (ret) {
e2302501 2304 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
21677cfc
JW
2305 goto urb_submit_failed;
2306 }
2307
6f6b90c9
JW
2308 ictx->dev_present_intf1 = true;
2309
23ef710e 2310 mutex_unlock(&ictx->lock);
21677cfc
JW
2311 return ictx;
2312
2313urb_submit_failed:
20cd1959 2314 if (ictx->touch)
21677cfc 2315 input_unregister_device(ictx->touch);
21677cfc
JW
2316touch_setup_failed:
2317find_endpoint_failed:
e87cb470 2318 usb_put_dev(ictx->usbdev_intf1);
21677cfc
JW
2319 mutex_unlock(&ictx->lock);
2320 usb_free_urb(rx_urb);
2321rx_urb_alloc_failed:
8791d63a 2322 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
21677cfc
JW
2323
2324 return NULL;
2325}
2326
21677cfc
JW
2327static void imon_init_display(struct imon_context *ictx,
2328 struct usb_interface *intf)
2329{
2330 int ret;
2331
2332 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2333
2334 /* set up sysfs entry for built-in clock */
6aa209e4 2335 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
21677cfc 2336 if (ret)
25ec587c
MCC
2337 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2338 ret);
21677cfc
JW
2339
2340 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2341 ret = usb_register_dev(intf, &imon_lcd_class);
2342 else
2343 ret = usb_register_dev(intf, &imon_vfd_class);
2344 if (ret)
2345 /* Not a fatal error, so ignore */
25ec587c 2346 dev_info(ictx->dev, "could not get a minor number for display\n");
21677cfc
JW
2347
2348}
2349
255940e6 2350/*
21677cfc
JW
2351 * Callback function for USB core API: Probe
2352 */
4c62e976
GKH
2353static int imon_probe(struct usb_interface *interface,
2354 const struct usb_device_id *id)
21677cfc
JW
2355{
2356 struct usb_device *usbdev = NULL;
2357 struct usb_host_interface *iface_desc = NULL;
2358 struct usb_interface *first_if;
2359 struct device *dev = &interface->dev;
76a2d21d 2360 int ifnum, sysfs_err;
21677cfc
JW
2361 int ret = 0;
2362 struct imon_context *ictx = NULL;
2363 struct imon_context *first_if_ctx = NULL;
2364 u16 vendor, product;
21677cfc 2365
21677cfc
JW
2366 usbdev = usb_get_dev(interface_to_usbdev(interface));
2367 iface_desc = interface->cur_altsetting;
2368 ifnum = iface_desc->desc.bInterfaceNumber;
2369 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2370 product = le16_to_cpu(usbdev->descriptor.idProduct);
2371
2372 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2373 __func__, vendor, product, ifnum);
2374
2375 /* prevent races probing devices w/multiple interfaces */
2376 mutex_lock(&driver_lock);
2377
2378 first_if = usb_ifnum_to_if(usbdev, 0);
58fd55e8
AY
2379 if (!first_if) {
2380 ret = -ENODEV;
2381 goto fail;
2382 }
2383
8350e155 2384 first_if_ctx = usb_get_intfdata(first_if);
21677cfc
JW
2385
2386 if (ifnum == 0) {
f7d141b3 2387 ictx = imon_init_intf0(interface, id);
21677cfc 2388 if (!ictx) {
e2302501 2389 pr_err("failed to initialize context!\n");
21677cfc
JW
2390 ret = -ENODEV;
2391 goto fail;
2392 }
2393
21677cfc 2394 } else {
7d54ba0e
KB
2395 /* this is the secondary interface on the device */
2396
2397 /* fail early if first intf failed to register */
2398 if (!first_if_ctx) {
2399 ret = -ENODEV;
2400 goto fail;
2401 }
2402
21677cfc
JW
2403 ictx = imon_init_intf1(interface, first_if_ctx);
2404 if (!ictx) {
e2302501 2405 pr_err("failed to attach to context!\n");
21677cfc
JW
2406 ret = -ENODEV;
2407 goto fail;
2408 }
2409
2410 }
2411
2412 usb_set_intfdata(interface, ictx);
2413
2414 if (ifnum == 0) {
23ef710e
JW
2415 mutex_lock(&ictx->lock);
2416
bbe4690f
JW
2417 if (product == 0xffdc && ictx->rf_device) {
2418 sysfs_err = sysfs_create_group(&interface->dev.kobj,
6aa209e4 2419 &imon_rf_attr_group);
bbe4690f 2420 if (sysfs_err)
e2302501
JP
2421 pr_err("Could not create RF sysfs entries(%d)\n",
2422 sysfs_err);
bbe4690f
JW
2423 }
2424
21677cfc
JW
2425 if (ictx->display_supported)
2426 imon_init_display(ictx, interface);
23ef710e
JW
2427
2428 mutex_unlock(&ictx->lock);
21677cfc
JW
2429 }
2430
25ec587c
MCC
2431 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2432 vendor, product, ifnum,
21677cfc
JW
2433 usbdev->bus->busnum, usbdev->devnum);
2434
21677cfc 2435 mutex_unlock(&driver_lock);
e87cb470 2436 usb_put_dev(usbdev);
21677cfc
JW
2437
2438 return 0;
2439
2440fail:
2441 mutex_unlock(&driver_lock);
e87cb470 2442 usb_put_dev(usbdev);
21677cfc
JW
2443 dev_err(dev, "unable to register, err %d\n", ret);
2444
2445 return ret;
2446}
2447
255940e6 2448/*
21677cfc
JW
2449 * Callback function for USB core API: disconnect
2450 */
4c62e976 2451static void imon_disconnect(struct usb_interface *interface)
21677cfc
JW
2452{
2453 struct imon_context *ictx;
2454 struct device *dev;
2455 int ifnum;
2456
2457 /* prevent races with multi-interface device probing and display_open */
2458 mutex_lock(&driver_lock);
2459
2460 ictx = usb_get_intfdata(interface);
2461 dev = ictx->dev;
2462 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2463
21677cfc
JW
2464 /*
2465 * sysfs_remove_group is safe to call even if sysfs_create_group
2466 * hasn't been called
2467 */
6aa209e4
JW
2468 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2469 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
21677cfc
JW
2470
2471 usb_set_intfdata(interface, NULL);
2472
2473 /* Abort ongoing write */
2474 if (ictx->tx.busy) {
2475 usb_kill_urb(ictx->tx_urb);
7c073fff 2476 complete(&ictx->tx.finished);
21677cfc
JW
2477 }
2478
2479 if (ifnum == 0) {
f789bf40 2480 ictx->dev_present_intf0 = false;
21677cfc 2481 usb_kill_urb(ictx->rx_urb_intf0);
e87cb470 2482 usb_put_dev(ictx->usbdev_intf0);
eaf2bcc9 2483 input_unregister_device(ictx->idev);
d8b4b582 2484 rc_unregister_device(ictx->rdev);
21677cfc
JW
2485 if (ictx->display_supported) {
2486 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2487 usb_deregister_dev(interface, &imon_lcd_class);
76a2d21d 2488 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
21677cfc
JW
2489 usb_deregister_dev(interface, &imon_vfd_class);
2490 }
2491 } else {
f789bf40 2492 ictx->dev_present_intf1 = false;
21677cfc 2493 usb_kill_urb(ictx->rx_urb_intf1);
e87cb470 2494 usb_put_dev(ictx->usbdev_intf1);
76a2d21d 2495 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
21677cfc 2496 input_unregister_device(ictx->touch);
76a2d21d
JW
2497 del_timer_sync(&ictx->ttimer);
2498 }
21677cfc
JW
2499 }
2500
76a2d21d
JW
2501 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2502 free_imon_context(ictx);
21677cfc
JW
2503
2504 mutex_unlock(&driver_lock);
2505
2506 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2507 __func__, ifnum);
2508}
2509
2510static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2511{
2512 struct imon_context *ictx = usb_get_intfdata(intf);
2513 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2514
2515 if (ifnum == 0)
2516 usb_kill_urb(ictx->rx_urb_intf0);
2517 else
2518 usb_kill_urb(ictx->rx_urb_intf1);
2519
2520 return 0;
2521}
2522
2523static int imon_resume(struct usb_interface *intf)
2524{
2525 int rc = 0;
2526 struct imon_context *ictx = usb_get_intfdata(intf);
2527 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2528
2529 if (ifnum == 0) {
2530 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2531 usb_rcvintpipe(ictx->usbdev_intf0,
2532 ictx->rx_endpoint_intf0->bEndpointAddress),
2533 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2534 usb_rx_callback_intf0, ictx,
2535 ictx->rx_endpoint_intf0->bInterval);
2536
2537 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2538
2539 } else {
2540 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2541 usb_rcvintpipe(ictx->usbdev_intf1,
2542 ictx->rx_endpoint_intf1->bEndpointAddress),
2543 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2544 usb_rx_callback_intf1, ictx,
2545 ictx->rx_endpoint_intf1->bInterval);
2546
2547 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2548 }
2549
2550 return rc;
2551}
2552
ecb3b2b3 2553module_usb_driver(imon_driver);