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