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