1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
6 * Portions based on the original lirc_imon driver,
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
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>
27 #include <linux/input.h>
28 #include <linux/usb.h>
29 #include <linux/usb/input.h>
30 #include <media/rc-core.h>
32 #include <linux/timer.h>
35 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
36 #define MOD_NAME "imon"
37 #define MOD_VERSION "0.9.4"
39 #define DISPLAY_MINOR_BASE 144
40 #define DEVICE_NAME "lcd%d"
42 #define BUF_CHUNK_SIZE 8
45 #define BIT_DURATION 250 /* each bit received is 250us */
47 #define IMON_CLOCK_ENABLE_PACKETS 2
49 /*** P R O T O T Y P E S ***/
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);
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);
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);
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);
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);
75 /*** G L O B A L S ***/
77 struct imon_panel_key_table {
82 struct imon_usb_dev_descr {
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[];
92 /* Newer devices have two interfaces */
93 struct usb_device *usbdev_intf0;
94 struct usb_device *usbdev_intf1;
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 */
103 struct mutex lock; /* to lock this object */
104 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
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;
113 unsigned char usb_rx_buf[8];
114 unsigned char usb_tx_buf[8];
115 unsigned int send_packet_delay;
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 */
124 u16 vendor; /* usb vendor ID */
125 u16 product; /* usb product ID */
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 */
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 */
139 u8 display_type; /* store the display type */
140 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
142 char name_rdev[128]; /* rc input device name */
143 char phys_rdev[64]; /* rc input device phys path */
145 char name_idev[128]; /* input device name */
146 char phys_idev[64]; /* input device phys path */
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 */
158 #define TOUCH_TIMEOUT (HZ/30)
160 /* vfd character device file operations */
161 static const struct file_operations vfd_fops = {
162 .owner = THIS_MODULE,
163 .open = &display_open,
165 .release = &display_close,
166 .llseek = noop_llseek,
169 /* lcd character device file operations */
170 static const struct file_operations lcd_fops = {
171 .owner = THIS_MODULE,
172 .open = &display_open,
174 .release = &display_close,
175 .llseek = noop_llseek,
179 IMON_DISPLAY_TYPE_AUTO = 0,
180 IMON_DISPLAY_TYPE_VFD = 1,
181 IMON_DISPLAY_TYPE_LCD = 2,
182 IMON_DISPLAY_TYPE_VGA = 3,
183 IMON_DISPLAY_TYPE_NONE = 4,
192 static struct usb_class_driver imon_vfd_class = {
195 .minor_base = DISPLAY_MINOR_BASE,
198 static struct usb_class_driver imon_lcd_class = {
201 .minor_base = DISPLAY_MINOR_BASE,
204 /* imon receiver front panel/knob key table */
205 static const struct imon_usb_dev_descr imon_default_table = {
206 .flags = IMON_NO_FLAGS,
208 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
209 { 0x000000001200ffeell, KEY_UP },
210 { 0x000000001300ffeell, KEY_DOWN },
211 { 0x000000001400ffeell, KEY_LEFT },
212 { 0x000000001500ffeell, KEY_RIGHT },
213 { 0x000000001600ffeell, KEY_ENTER },
214 { 0x000000001700ffeell, KEY_ESC },
215 { 0x000000001f00ffeell, KEY_AUDIO },
216 { 0x000000002000ffeell, KEY_VIDEO },
217 { 0x000000002100ffeell, KEY_CAMERA },
218 { 0x000000002700ffeell, KEY_DVD },
219 { 0x000000002300ffeell, KEY_TV },
220 { 0x000000002b00ffeell, KEY_EXIT },
221 { 0x000000002c00ffeell, KEY_SELECT },
222 { 0x000000002d00ffeell, KEY_MENU },
223 { 0x000000000500ffeell, KEY_PREVIOUS },
224 { 0x000000000700ffeell, KEY_REWIND },
225 { 0x000000000400ffeell, KEY_STOP },
226 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
227 { 0x000000000800ffeell, KEY_FASTFORWARD },
228 { 0x000000000600ffeell, KEY_NEXT },
229 { 0x000000010000ffeell, KEY_RIGHT },
230 { 0x000001000000ffeell, KEY_LEFT },
231 { 0x000000003d00ffeell, KEY_SELECT },
232 { 0x000100000000ffeell, KEY_VOLUMEUP },
233 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
234 { 0x000000000100ffeell, KEY_MUTE },
235 /* 0xffdc iMON MCE VFD */
236 { 0x00010000ffffffeell, KEY_VOLUMEUP },
237 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
238 { 0x00000001ffffffeell, KEY_MUTE },
239 { 0x0000000fffffffeell, KEY_MEDIA },
240 { 0x00000012ffffffeell, KEY_UP },
241 { 0x00000013ffffffeell, KEY_DOWN },
242 { 0x00000014ffffffeell, KEY_LEFT },
243 { 0x00000015ffffffeell, KEY_RIGHT },
244 { 0x00000016ffffffeell, KEY_ENTER },
245 { 0x00000017ffffffeell, KEY_ESC },
246 /* iMON Knob values */
247 { 0x000100ffffffffeell, KEY_VOLUMEUP },
248 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
249 { 0x000008ffffffffeell, KEY_MUTE },
254 static const struct imon_usb_dev_descr imon_OEM_VFD = {
255 .flags = IMON_NEED_20MS_PKT_DELAY,
257 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
258 { 0x000000001200ffeell, KEY_UP },
259 { 0x000000001300ffeell, KEY_DOWN },
260 { 0x000000001400ffeell, KEY_LEFT },
261 { 0x000000001500ffeell, KEY_RIGHT },
262 { 0x000000001600ffeell, KEY_ENTER },
263 { 0x000000001700ffeell, KEY_ESC },
264 { 0x000000001f00ffeell, KEY_AUDIO },
265 { 0x000000002b00ffeell, KEY_EXIT },
266 { 0x000000002c00ffeell, KEY_SELECT },
267 { 0x000000002d00ffeell, KEY_MENU },
268 { 0x000000000500ffeell, KEY_PREVIOUS },
269 { 0x000000000700ffeell, KEY_REWIND },
270 { 0x000000000400ffeell, KEY_STOP },
271 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
272 { 0x000000000800ffeell, KEY_FASTFORWARD },
273 { 0x000000000600ffeell, KEY_NEXT },
274 { 0x000000010000ffeell, KEY_RIGHT },
275 { 0x000001000000ffeell, KEY_LEFT },
276 { 0x000000003d00ffeell, KEY_SELECT },
277 { 0x000100000000ffeell, KEY_VOLUMEUP },
278 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
279 { 0x000000000100ffeell, KEY_MUTE },
280 /* 0xffdc iMON MCE VFD */
281 { 0x00010000ffffffeell, KEY_VOLUMEUP },
282 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
283 { 0x00000001ffffffeell, KEY_MUTE },
284 { 0x0000000fffffffeell, KEY_MEDIA },
285 { 0x00000012ffffffeell, KEY_UP },
286 { 0x00000013ffffffeell, KEY_DOWN },
287 { 0x00000014ffffffeell, KEY_LEFT },
288 { 0x00000015ffffffeell, KEY_RIGHT },
289 { 0x00000016ffffffeell, KEY_ENTER },
290 { 0x00000017ffffffeell, KEY_ESC },
291 /* iMON Knob values */
292 { 0x000100ffffffffeell, KEY_VOLUMEUP },
293 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
294 { 0x000008ffffffffeell, KEY_MUTE },
299 /* imon receiver front panel/knob key table for DH102*/
300 static const struct imon_usb_dev_descr imon_DH102 = {
301 .flags = IMON_NO_FLAGS,
303 { 0x000100000000ffeell, KEY_VOLUMEUP },
304 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
305 { 0x000000010000ffeell, KEY_MUTE },
306 { 0x0000000f0000ffeell, KEY_MEDIA },
307 { 0x000000120000ffeell, KEY_UP },
308 { 0x000000130000ffeell, KEY_DOWN },
309 { 0x000000140000ffeell, KEY_LEFT },
310 { 0x000000150000ffeell, KEY_RIGHT },
311 { 0x000000160000ffeell, KEY_ENTER },
312 { 0x000000170000ffeell, KEY_ESC },
313 { 0x0000002b0000ffeell, KEY_EXIT },
314 { 0x0000002c0000ffeell, KEY_SELECT },
315 { 0x0000002d0000ffeell, KEY_MENU },
320 /* imon ultrabay front panel key table */
321 static const struct imon_usb_dev_descr ultrabay_table = {
322 .flags = IMON_SUPPRESS_REPEATED_KEYS,
324 { 0x0000000f0000ffeell, KEY_MEDIA }, /* Go */
325 { 0x000000000100ffeell, KEY_UP },
326 { 0x000000000001ffeell, KEY_DOWN },
327 { 0x000000160000ffeell, KEY_ENTER },
328 { 0x0000001f0000ffeell, KEY_AUDIO }, /* Music */
329 { 0x000000200000ffeell, KEY_VIDEO }, /* Movie */
330 { 0x000000210000ffeell, KEY_CAMERA }, /* Photo */
331 { 0x000000270000ffeell, KEY_DVD }, /* DVD */
332 { 0x000000230000ffeell, KEY_TV }, /* TV */
333 { 0x000000050000ffeell, KEY_PREVIOUS }, /* Previous */
334 { 0x000000070000ffeell, KEY_REWIND },
335 { 0x000000040000ffeell, KEY_STOP },
336 { 0x000000020000ffeell, KEY_PLAYPAUSE },
337 { 0x000000080000ffeell, KEY_FASTFORWARD },
338 { 0x000000060000ffeell, KEY_NEXT }, /* Next */
339 { 0x000100000000ffeell, KEY_VOLUMEUP },
340 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
341 { 0x000000010000ffeell, KEY_MUTE },
347 * USB Device ID for iMON USB Control Boards
349 * The Windows drivers contain 6 different inf files, more or less one for
350 * each new device until the 0x0034-0x0046 devices, which all use the same
351 * driver. Some of the devices in the 34-46 range haven't been definitively
352 * identified yet. Early devices have either a TriGem Computer, Inc. or a
353 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
354 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
355 * the ffdc and later devices, which do onboard decoding.
357 static const struct usb_device_id imon_usb_id_table[] = {
359 * Several devices with this same device ID, all use iMON_PAD.inf
360 * SoundGraph iMON PAD (IR & VFD)
361 * SoundGraph iMON PAD (IR & LCD)
362 * SoundGraph iMON Knob (IR only)
364 { USB_DEVICE(0x15c2, 0xffdc),
365 .driver_info = (unsigned long)&imon_default_table },
368 * Newer devices, all driven by the latest iMON Windows driver, full
369 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
370 * Need user input to fill in details on unknown devices.
372 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
373 { USB_DEVICE(0x15c2, 0x0034),
374 .driver_info = (unsigned long)&imon_DH102 },
375 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
376 { USB_DEVICE(0x15c2, 0x0035),
377 .driver_info = (unsigned long)&imon_default_table},
378 /* SoundGraph iMON OEM VFD (IR & VFD) */
379 { USB_DEVICE(0x15c2, 0x0036),
380 .driver_info = (unsigned long)&imon_OEM_VFD },
381 /* device specifics unknown */
382 { USB_DEVICE(0x15c2, 0x0037),
383 .driver_info = (unsigned long)&imon_default_table},
384 /* SoundGraph iMON OEM LCD (IR & LCD) */
385 { USB_DEVICE(0x15c2, 0x0038),
386 .driver_info = (unsigned long)&imon_default_table},
387 /* SoundGraph iMON UltraBay (IR & LCD) */
388 { USB_DEVICE(0x15c2, 0x0039),
389 .driver_info = (unsigned long)&imon_default_table},
390 /* device specifics unknown */
391 { USB_DEVICE(0x15c2, 0x003a),
392 .driver_info = (unsigned long)&imon_default_table},
393 /* device specifics unknown */
394 { USB_DEVICE(0x15c2, 0x003b),
395 .driver_info = (unsigned long)&imon_default_table},
396 /* SoundGraph iMON OEM Inside (IR only) */
397 { USB_DEVICE(0x15c2, 0x003c),
398 .driver_info = (unsigned long)&imon_default_table},
399 /* device specifics unknown */
400 { USB_DEVICE(0x15c2, 0x003d),
401 .driver_info = (unsigned long)&imon_default_table},
402 /* device specifics unknown */
403 { USB_DEVICE(0x15c2, 0x003e),
404 .driver_info = (unsigned long)&imon_default_table},
405 /* device specifics unknown */
406 { USB_DEVICE(0x15c2, 0x003f),
407 .driver_info = (unsigned long)&imon_default_table},
408 /* device specifics unknown */
409 { USB_DEVICE(0x15c2, 0x0040),
410 .driver_info = (unsigned long)&imon_default_table},
411 /* SoundGraph iMON MINI (IR only) */
412 { USB_DEVICE(0x15c2, 0x0041),
413 .driver_info = (unsigned long)&imon_default_table},
414 /* Antec Veris Multimedia Station EZ External (IR only) */
415 { USB_DEVICE(0x15c2, 0x0042),
416 .driver_info = (unsigned long)&imon_default_table},
417 /* Antec Veris Multimedia Station Basic Internal (IR only) */
418 { USB_DEVICE(0x15c2, 0x0043),
419 .driver_info = (unsigned long)&imon_default_table},
420 /* Antec Veris Multimedia Station Elite (IR & VFD) */
421 { USB_DEVICE(0x15c2, 0x0044),
422 .driver_info = (unsigned long)&imon_default_table},
423 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
424 { USB_DEVICE(0x15c2, 0x0045),
425 .driver_info = (unsigned long)&imon_default_table},
426 /* device specifics unknown */
427 { USB_DEVICE(0x15c2, 0x0046),
428 .driver_info = (unsigned long)&imon_default_table},
432 /* USB Device data */
433 static struct usb_driver imon_driver = {
436 .disconnect = imon_disconnect,
437 .suspend = imon_suspend,
438 .resume = imon_resume,
439 .id_table = imon_usb_id_table,
442 /* to prevent races between open() and disconnect(), probing, etc */
443 static DEFINE_MUTEX(driver_lock);
445 /* Module bookkeeping bits */
446 MODULE_AUTHOR(MOD_AUTHOR);
447 MODULE_DESCRIPTION(MOD_DESC);
448 MODULE_VERSION(MOD_VERSION);
449 MODULE_LICENSE("GPL");
450 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
453 module_param(debug, bool, S_IRUGO | S_IWUSR);
454 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
456 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
457 static int display_type;
458 module_param(display_type, int, S_IRUGO);
459 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, 1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
461 static int pad_stabilize = 1;
462 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
463 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD presses in arrow key mode. 0=disable, 1=enable (default).");
466 * In certain use cases, mouse mode isn't really helpful, and could actually
467 * cause confusion, so allow disabling it when the IR device is open.
470 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
471 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is open. 0=don't disable, 1=disable. (default: don't disable)");
473 /* threshold at which a pad push registers as an arrow key in kbd mode */
474 static int pad_thresh;
475 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
476 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an arrow key in kbd mode (default: 28)");
479 static void free_imon_context(struct imon_context *ictx)
481 struct device *dev = ictx->dev;
483 usb_free_urb(ictx->tx_urb);
484 usb_free_urb(ictx->rx_urb_intf0);
485 usb_free_urb(ictx->rx_urb_intf1);
488 dev_dbg(dev, "%s: iMON context freed\n", __func__);
492 * Called when the Display device (e.g. /dev/lcd0)
493 * is opened by the application.
495 static int display_open(struct inode *inode, struct file *file)
497 struct usb_interface *interface;
498 struct imon_context *ictx = NULL;
502 /* prevent races with disconnect */
503 mutex_lock(&driver_lock);
505 subminor = iminor(inode);
506 interface = usb_find_interface(&imon_driver, subminor);
508 pr_err("could not find interface for minor %d\n", subminor);
512 ictx = usb_get_intfdata(interface);
515 pr_err("no context found for minor %d\n", subminor);
520 mutex_lock(&ictx->lock);
522 if (!ictx->display_supported) {
523 pr_err("display not supported by device\n");
525 } else if (ictx->display_isopen) {
526 pr_err("display port is already open\n");
529 ictx->display_isopen = true;
530 file->private_data = ictx;
531 dev_dbg(ictx->dev, "display port opened\n");
534 mutex_unlock(&ictx->lock);
537 mutex_unlock(&driver_lock);
542 * Called when the display device (e.g. /dev/lcd0)
543 * is closed by the application.
545 static int display_close(struct inode *inode, struct file *file)
547 struct imon_context *ictx = NULL;
550 ictx = file->private_data;
553 pr_err("no context for device\n");
557 mutex_lock(&ictx->lock);
559 if (!ictx->display_supported) {
560 pr_err("display not supported by device\n");
562 } else if (!ictx->display_isopen) {
563 pr_err("display is not open\n");
566 ictx->display_isopen = false;
567 dev_dbg(ictx->dev, "display port closed\n");
570 mutex_unlock(&ictx->lock);
575 * Sends a packet to the device -- this function must be called with
576 * ictx->lock held, or its unlock/lock sequence while waiting for tx
577 * to complete can/will lead to a deadlock.
579 static int send_packet(struct imon_context *ictx)
582 unsigned long timeout;
585 struct usb_ctrlrequest *control_req = NULL;
587 /* Check if we need to use control or interrupt urb */
588 if (!ictx->tx_control) {
589 pipe = usb_sndintpipe(ictx->usbdev_intf0,
590 ictx->tx_endpoint->bEndpointAddress);
591 interval = ictx->tx_endpoint->bInterval;
593 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
595 sizeof(ictx->usb_tx_buf),
596 usb_tx_callback, ictx, interval);
598 ictx->tx_urb->actual_length = 0;
600 /* fill request into kmalloc'ed space: */
601 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
602 if (control_req == NULL)
605 /* setup packet is '21 09 0200 0001 0008' */
606 control_req->bRequestType = 0x21;
607 control_req->bRequest = 0x09;
608 control_req->wValue = cpu_to_le16(0x0200);
609 control_req->wIndex = cpu_to_le16(0x0001);
610 control_req->wLength = cpu_to_le16(0x0008);
612 /* control pipe is endpoint 0x00 */
613 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
615 /* build the control urb */
616 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
617 pipe, (unsigned char *)control_req,
619 sizeof(ictx->usb_tx_buf),
620 usb_tx_callback, ictx);
621 ictx->tx_urb->actual_length = 0;
624 reinit_completion(&ictx->tx.finished);
625 ictx->tx.busy = true;
626 smp_rmb(); /* ensure later readers know we're busy */
628 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
630 ictx->tx.busy = false;
631 smp_rmb(); /* ensure later readers know we're not busy */
632 pr_err_ratelimited("error submitting urb(%d)\n", retval);
634 /* Wait for transmission to complete (or abort) */
635 mutex_unlock(&ictx->lock);
636 retval = wait_for_completion_interruptible(
639 usb_kill_urb(ictx->tx_urb);
640 pr_err_ratelimited("task interrupted\n");
642 mutex_lock(&ictx->lock);
644 retval = ictx->tx.status;
646 pr_err_ratelimited("packet tx failed (%d)\n", retval);
652 * Induce a mandatory delay before returning, as otherwise,
653 * send_packet can get called so rapidly as to overwhelm the device,
654 * particularly on faster systems and/or those with quirky usb.
656 timeout = msecs_to_jiffies(ictx->send_packet_delay);
657 set_current_state(TASK_INTERRUPTIBLE);
658 schedule_timeout(timeout);
664 * Sends an associate packet to the iMON 2.4G.
666 * This might not be such a good idea, since it has an id collision with
667 * some versions of the "IR & VFD" combo. The only way to determine if it
668 * is an RF version is to look at the product description string. (Which
669 * we currently do not fetch).
671 static int send_associate_24g(struct imon_context *ictx)
674 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
675 0x00, 0x00, 0x00, 0x20 };
678 pr_err("no context for device\n");
682 if (!ictx->dev_present_intf0) {
683 pr_err("no iMON device present\n");
687 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
688 retval = send_packet(ictx);
694 * Sends packets to setup and show clock on iMON display
696 * Arguments: year - last 2 digits of year, month - 1..12,
697 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
698 * hour - 0..23, minute - 0..59, second - 0..59
700 static int send_set_imon_clock(struct imon_context *ictx,
701 unsigned int year, unsigned int month,
702 unsigned int day, unsigned int dow,
703 unsigned int hour, unsigned int minute,
706 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
711 pr_err("no context for device\n");
715 switch (ictx->display_type) {
716 case IMON_DISPLAY_TYPE_LCD:
717 clock_enable_pkt[0][0] = 0x80;
718 clock_enable_pkt[0][1] = year;
719 clock_enable_pkt[0][2] = month-1;
720 clock_enable_pkt[0][3] = day;
721 clock_enable_pkt[0][4] = hour;
722 clock_enable_pkt[0][5] = minute;
723 clock_enable_pkt[0][6] = second;
725 clock_enable_pkt[1][0] = 0x80;
726 clock_enable_pkt[1][1] = 0;
727 clock_enable_pkt[1][2] = 0;
728 clock_enable_pkt[1][3] = 0;
729 clock_enable_pkt[1][4] = 0;
730 clock_enable_pkt[1][5] = 0;
731 clock_enable_pkt[1][6] = 0;
733 if (ictx->product == 0xffdc) {
734 clock_enable_pkt[0][7] = 0x50;
735 clock_enable_pkt[1][7] = 0x51;
737 clock_enable_pkt[0][7] = 0x88;
738 clock_enable_pkt[1][7] = 0x8a;
743 case IMON_DISPLAY_TYPE_VFD:
744 clock_enable_pkt[0][0] = year;
745 clock_enable_pkt[0][1] = month-1;
746 clock_enable_pkt[0][2] = day;
747 clock_enable_pkt[0][3] = dow;
748 clock_enable_pkt[0][4] = hour;
749 clock_enable_pkt[0][5] = minute;
750 clock_enable_pkt[0][6] = second;
751 clock_enable_pkt[0][7] = 0x40;
753 clock_enable_pkt[1][0] = 0;
754 clock_enable_pkt[1][1] = 0;
755 clock_enable_pkt[1][2] = 1;
756 clock_enable_pkt[1][3] = 0;
757 clock_enable_pkt[1][4] = 0;
758 clock_enable_pkt[1][5] = 0;
759 clock_enable_pkt[1][6] = 0;
760 clock_enable_pkt[1][7] = 0x42;
768 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
769 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
770 retval = send_packet(ictx);
772 pr_err("send_packet failed for packet %d\n", i);
781 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
783 static ssize_t associate_remote_show(struct device *d,
784 struct device_attribute *attr,
787 struct imon_context *ictx = dev_get_drvdata(d);
792 mutex_lock(&ictx->lock);
793 if (ictx->rf_isassociating)
794 strscpy(buf, "associating\n", PAGE_SIZE);
796 strscpy(buf, "closed\n", PAGE_SIZE);
798 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");
799 mutex_unlock(&ictx->lock);
803 static ssize_t associate_remote_store(struct device *d,
804 struct device_attribute *attr,
805 const char *buf, size_t count)
807 struct imon_context *ictx;
809 ictx = dev_get_drvdata(d);
814 mutex_lock(&ictx->lock);
815 ictx->rf_isassociating = true;
816 send_associate_24g(ictx);
817 mutex_unlock(&ictx->lock);
823 * sysfs functions to control internal imon clock
825 static ssize_t imon_clock_show(struct device *d,
826 struct device_attribute *attr, char *buf)
828 struct imon_context *ictx = dev_get_drvdata(d);
834 mutex_lock(&ictx->lock);
836 if (!ictx->display_supported) {
837 len = snprintf(buf, PAGE_SIZE, "Not supported.");
839 len = snprintf(buf, PAGE_SIZE,
840 "To set the clock on your iMON display:\n"
841 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
842 "%s", ictx->display_isopen ?
843 "\nNOTE: imon device must be closed\n" : "");
846 mutex_unlock(&ictx->lock);
851 static ssize_t imon_clock_store(struct device *d,
852 struct device_attribute *attr,
853 const char *buf, size_t count)
855 struct imon_context *ictx = dev_get_drvdata(d);
857 unsigned int year, month, day, dow, hour, minute, second;
862 mutex_lock(&ictx->lock);
864 if (!ictx->display_supported) {
867 } else if (ictx->display_isopen) {
872 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
873 &hour, &minute, &second) != 7) {
878 if ((month < 1 || month > 12) ||
879 (day < 1 || day > 31) || (dow > 6) ||
880 (hour > 23) || (minute > 59) || (second > 59)) {
885 retval = send_set_imon_clock(ictx, year, month, day, dow,
886 hour, minute, second);
892 mutex_unlock(&ictx->lock);
898 static DEVICE_ATTR_RW(imon_clock);
899 static DEVICE_ATTR_RW(associate_remote);
901 static struct attribute *imon_display_sysfs_entries[] = {
902 &dev_attr_imon_clock.attr,
906 static const struct attribute_group imon_display_attr_group = {
907 .attrs = imon_display_sysfs_entries
910 static struct attribute *imon_rf_sysfs_entries[] = {
911 &dev_attr_associate_remote.attr,
915 static const struct attribute_group imon_rf_attr_group = {
916 .attrs = imon_rf_sysfs_entries
920 * Writes data to the VFD. The iMON VFD is 2x16 characters
921 * and requires data in 5 consecutive USB interrupt packets,
922 * each packet but the last carrying 7 bytes.
924 * I don't know if the VFD board supports features such as
925 * scrolling, clearing rows, blanking, etc. so at
926 * the caller must provide a full screen of data. If fewer
927 * than 32 bytes are provided spaces will be appended to
928 * generate a full screen.
930 static ssize_t vfd_write(struct file *file, const char __user *buf,
931 size_t n_bytes, loff_t *pos)
937 struct imon_context *ictx;
938 static const unsigned char vfd_packet6[] = {
939 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
941 ictx = file->private_data;
943 pr_err_ratelimited("no context for device\n");
947 mutex_lock(&ictx->lock);
949 if (!ictx->dev_present_intf0) {
950 pr_err_ratelimited("no iMON device present\n");
955 if (n_bytes <= 0 || n_bytes > 32) {
956 pr_err_ratelimited("invalid payload size\n");
961 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
966 /* Pad with spaces */
967 for (i = n_bytes; i < 32; ++i)
968 ictx->tx.data_buf[i] = ' ';
970 for (i = 32; i < 35; ++i)
971 ictx->tx.data_buf[i] = 0xFF;
977 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
978 ictx->usb_tx_buf[7] = (unsigned char) seq;
980 retval = send_packet(ictx);
982 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
989 } while (offset < 35);
992 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
993 ictx->usb_tx_buf[7] = (unsigned char) seq;
994 retval = send_packet(ictx);
996 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
999 mutex_unlock(&ictx->lock);
1001 return (!retval) ? n_bytes : retval;
1005 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
1006 * packets. We accept data as 16 hexadecimal digits, followed by a
1007 * newline (to make it easy to drive the device from a command-line
1008 * -- even though the actual binary data is a bit complicated).
1010 * The device itself is not a "traditional" text-mode display. It's
1011 * actually a 16x96 pixel bitmap display. That means if you want to
1012 * display text, you've got to have your own "font" and translate the
1013 * text into bitmaps for display. This is really flexible (you can
1014 * display whatever diacritics you need, and so on), but it's also
1015 * a lot more complicated than most LCDs...
1017 static ssize_t lcd_write(struct file *file, const char __user *buf,
1018 size_t n_bytes, loff_t *pos)
1021 struct imon_context *ictx;
1023 ictx = file->private_data;
1025 pr_err_ratelimited("no context for device\n");
1029 mutex_lock(&ictx->lock);
1031 if (!ictx->display_supported) {
1032 pr_err_ratelimited("no iMON display present\n");
1038 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1044 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1049 retval = send_packet(ictx);
1051 pr_err_ratelimited("send packet failed!\n");
1054 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1055 __func__, (int) n_bytes);
1058 mutex_unlock(&ictx->lock);
1059 return (!retval) ? n_bytes : retval;
1063 * Callback function for USB core API: transmit data
1065 static void usb_tx_callback(struct urb *urb)
1067 struct imon_context *ictx;
1071 ictx = (struct imon_context *)urb->context;
1075 ictx->tx.status = urb->status;
1077 /* notify waiters that write has finished */
1078 ictx->tx.busy = false;
1079 smp_rmb(); /* ensure later readers know we're not busy */
1080 complete(&ictx->tx.finished);
1084 * report touchscreen input
1086 static void imon_touch_display_timeout(struct timer_list *t)
1088 struct imon_context *ictx = from_timer(ictx, t, ttimer);
1090 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
1093 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1094 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1095 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1096 input_sync(ictx->touch);
1100 * iMON IR receivers support two different signal sets -- those used by
1101 * the iMON remotes, and those used by the Windows MCE remotes (which is
1102 * really just RC-6), but only one or the other at a time, as the signals
1103 * are decoded onboard the receiver.
1105 * This function gets called two different ways, one way is from
1106 * rc_register_device, for initial protocol selection/setup, and the other is
1107 * via a userspace-initiated protocol change request, either by direct sysfs
1108 * prodding or by something like ir-keytable. In the rc_register_device case,
1109 * the imon context lock is already held, but when initiated from userspace,
1110 * it is not, so we must acquire it prior to calling send_packet, which
1111 * requires that the lock is held.
1113 static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
1116 struct imon_context *ictx = rc->priv;
1117 struct device *dev = ictx->dev;
1118 bool unlock = false;
1119 unsigned char ir_proto_packet[] = {
1120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1122 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
1123 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
1125 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
1126 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1127 ir_proto_packet[0] = 0x01;
1128 *rc_proto = RC_PROTO_BIT_RC6_MCE;
1129 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
1130 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1132 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1133 /* ir_proto_packet[0] = 0x00; // already the default */
1134 *rc_proto = RC_PROTO_BIT_IMON;
1136 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
1138 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1139 /* ir_proto_packet[0] = 0x00; // already the default */
1140 *rc_proto = RC_PROTO_BIT_IMON;
1143 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1145 if (!mutex_is_locked(&ictx->lock)) {
1147 mutex_lock(&ictx->lock);
1150 retval = send_packet(ictx);
1154 ictx->rc_proto = *rc_proto;
1155 ictx->pad_mouse = false;
1159 mutex_unlock(&ictx->lock);
1165 * The directional pad behaves a bit differently, depending on whether this is
1166 * one of the older ffdc devices or a newer device. Newer devices appear to
1167 * have a higher resolution matrix for more precise mouse movement, but it
1168 * makes things overly sensitive in keyboard mode, so we do some interesting
1169 * contortions to make it less touchy. Older devices run through the same
1170 * routine with shorter timeout and a smaller threshold.
1172 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1175 static ktime_t prev_time;
1176 static ktime_t hit_time;
1177 static int x, y, prev_result, hits;
1179 long msec, msec_hit;
1182 msec = ktime_ms_delta(ct, prev_time);
1183 msec_hit = ktime_ms_delta(ct, hit_time);
1196 if (abs(x) > threshold || abs(y) > threshold) {
1197 if (abs(y) > abs(x))
1198 result = (y > 0) ? 0x7F : 0x80;
1200 result = (x > 0) ? 0x7F00 : 0x8000;
1205 if (result == prev_result) {
1211 y = 17 * threshold / 30;
1214 y -= 17 * threshold / 30;
1217 x = 17 * threshold / 30;
1220 x -= 17 * threshold / 30;
1225 if (hits == 2 && msec_hit < timeout) {
1230 prev_result = result;
1239 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1243 bool is_release_code = false;
1245 /* Look for the initial press of a button */
1246 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1247 ictx->rc_toggle = 0x0;
1248 ictx->rc_scancode = scancode;
1250 /* Look for the release of a button */
1251 if (keycode == KEY_RESERVED) {
1252 release = scancode & ~0x4000;
1253 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1254 if (keycode != KEY_RESERVED)
1255 is_release_code = true;
1258 ictx->release_code = is_release_code;
1263 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1267 #define MCE_KEY_MASK 0x7000
1268 #define MCE_TOGGLE_BIT 0x8000
1271 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1272 * (the toggle bit flipping between alternating key presses), while
1273 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1274 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1275 * but we can't or them into all codes, as some keys are decoded in
1276 * a different way w/o the same use of the toggle bit...
1278 if (scancode & 0x80000000)
1279 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1281 ictx->rc_scancode = scancode;
1282 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1284 /* not used in mce mode, but make sure we know its false */
1285 ictx->release_code = false;
1290 static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
1292 const struct imon_panel_key_table *key_table;
1293 u32 keycode = KEY_RESERVED;
1296 key_table = ictx->dev_descr->key_table;
1298 for (i = 0; key_table[i].hw_code != 0; i++) {
1299 if (key_table[i].hw_code == (code | 0xffee)) {
1300 keycode = key_table[i].keycode;
1304 ictx->release_code = false;
1308 static bool imon_mouse_event(struct imon_context *ictx,
1309 unsigned char *buf, int len)
1311 signed char rel_x = 0x00, rel_y = 0x00;
1313 bool mouse_input = true;
1315 unsigned long flags;
1317 spin_lock_irqsave(&ictx->kc_lock, flags);
1319 /* newer iMON device PAD or mouse button */
1320 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1324 /* 0xffdc iMON PAD or mouse button input */
1325 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1326 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1327 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1328 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1331 rel_x = rel_x + rel_x / 2;
1332 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1333 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1336 rel_y = rel_y + rel_y / 2;
1338 /* some ffdc devices decode mouse buttons differently... */
1339 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1341 /* ch+/- buttons, which we use for an emulated scroll wheel */
1342 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1344 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1347 mouse_input = false;
1349 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1352 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1355 input_report_rel(ictx->idev, REL_WHEEL, dir);
1356 } else if (rel_x || rel_y) {
1357 input_report_rel(ictx->idev, REL_X, rel_x);
1358 input_report_rel(ictx->idev, REL_Y, rel_y);
1360 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1361 input_report_key(ictx->idev, BTN_RIGHT,
1362 buf[1] >> right_shift & 0x1);
1364 input_sync(ictx->idev);
1365 spin_lock_irqsave(&ictx->kc_lock, flags);
1366 ictx->last_keycode = ictx->kc;
1367 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1373 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1375 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1376 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1377 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1378 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1379 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1380 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1381 input_sync(ictx->touch);
1384 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1387 signed char rel_x = 0x00, rel_y = 0x00;
1388 u16 timeout, threshold;
1389 u32 scancode = KEY_RESERVED;
1390 unsigned long flags;
1393 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1394 * contain a position coordinate (x,y), with each component ranging
1395 * from -14 to 14. We want to down-sample this to only 4 discrete values
1396 * for up/down/left/right arrow keys. Also, when you get too close to
1397 * diagonals, it has a tendency to jump back and forth, so lets try to
1398 * ignore when they get too close.
1400 if (ictx->product != 0xffdc) {
1401 /* first, pad to 8 bytes so it conforms with everything else */
1402 buf[5] = buf[6] = buf[7] = 0;
1403 timeout = 500; /* in msecs */
1404 /* (2*threshold) x (2*threshold) square */
1405 threshold = pad_thresh ? pad_thresh : 28;
1409 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1410 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1411 dir = stabilize((int)rel_x, (int)rel_y,
1412 timeout, threshold);
1414 spin_lock_irqsave(&ictx->kc_lock,
1416 ictx->kc = KEY_UNKNOWN;
1417 spin_unlock_irqrestore(&ictx->kc_lock,
1421 buf[2] = dir & 0xFF;
1422 buf[3] = (dir >> 8) & 0xFF;
1423 scancode = be32_to_cpu(*((__be32 *)buf));
1427 * Hack alert: instead of using keycodes, we have
1428 * to use hard-coded scancodes here...
1430 if (abs(rel_y) > abs(rel_x)) {
1431 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1434 scancode = 0x01007f00; /* KEY_DOWN */
1436 scancode = 0x01008000; /* KEY_UP */
1439 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1441 scancode = 0x0100007f; /* KEY_RIGHT */
1443 scancode = 0x01000080; /* KEY_LEFT */
1448 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1449 * device (15c2:ffdc). The remote generates various codes from
1450 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1451 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1452 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1453 * reversed endianness. Extract direction from buffer, rotate endianness,
1454 * adjust sign and feed the values into stabilize(). The resulting codes
1455 * will be 0x01008000, 0x01007F00, which match the newer devices.
1458 timeout = 10; /* in msecs */
1459 /* (2*threshold) x (2*threshold) square */
1460 threshold = pad_thresh ? pad_thresh : 15;
1463 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1464 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1468 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1469 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1474 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1476 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1477 dir = stabilize((int)rel_x, (int)rel_y,
1478 timeout, threshold);
1480 spin_lock_irqsave(&ictx->kc_lock, flags);
1481 ictx->kc = KEY_UNKNOWN;
1482 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1485 buf[2] = dir & 0xFF;
1486 buf[3] = (dir >> 8) & 0xFF;
1487 scancode = be32_to_cpu(*((__be32 *)buf));
1490 * Hack alert: instead of using keycodes, we have
1491 * to use hard-coded scancodes here...
1493 if (abs(rel_y) > abs(rel_x)) {
1494 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1497 scancode = 0x01007f00; /* KEY_DOWN */
1499 scancode = 0x01008000; /* KEY_UP */
1502 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1504 scancode = 0x0100007f; /* KEY_RIGHT */
1506 scancode = 0x01000080; /* KEY_LEFT */
1512 spin_lock_irqsave(&ictx->kc_lock, flags);
1513 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1514 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1519 * figure out if these is a press or a release. We don't actually
1520 * care about repeats, as those will be auto-generated within the IR
1521 * subsystem for repeating scancodes.
1523 static int imon_parse_press_type(struct imon_context *ictx,
1524 unsigned char *buf, u8 ktype)
1527 unsigned long flags;
1529 spin_lock_irqsave(&ictx->kc_lock, flags);
1531 /* key release of 0x02XXXXXX key */
1532 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1533 ictx->kc = ictx->last_keycode;
1535 /* mouse button release on (some) 0xffdc devices */
1536 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1537 buf[2] == 0x81 && buf[3] == 0xb7)
1538 ictx->kc = ictx->last_keycode;
1540 /* mouse button release on (some other) 0xffdc devices */
1541 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1542 buf[2] == 0x81 && buf[3] == 0xb7)
1543 ictx->kc = ictx->last_keycode;
1545 /* mce-specific button handling, no keyup events */
1546 else if (ktype == IMON_KEY_MCE) {
1547 ictx->rc_toggle = buf[2];
1550 /* incoherent or irrelevant data */
1551 } else if (ictx->kc == KEY_RESERVED)
1552 press_type = -EINVAL;
1554 /* key release of 0xXXXXXXb7 key */
1555 else if (ictx->release_code)
1558 /* this is a button press */
1562 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1568 * Process the incoming packet
1570 static void imon_incoming_packet(struct imon_context *ictx,
1571 struct urb *urb, int intf)
1573 int len = urb->actual_length;
1574 unsigned char *buf = urb->transfer_buffer;
1575 struct device *dev = ictx->dev;
1576 unsigned long flags;
1581 static ktime_t prev_time;
1584 /* filter out junk data on the older 0xffdc imon devices */
1585 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1588 /* Figure out what key was pressed */
1589 if (len == 8 && buf[7] == 0xee) {
1590 scancode = be64_to_cpu(*((__be64 *)buf));
1591 ktype = IMON_KEY_PANEL;
1592 kc = imon_panel_key_lookup(ictx, scancode);
1593 ictx->release_code = false;
1595 scancode = be32_to_cpu(*((__be32 *)buf));
1596 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
1597 ktype = IMON_KEY_IMON;
1599 ktype = IMON_KEY_MCE;
1600 kc = imon_mce_key_lookup(ictx, scancode);
1602 ktype = IMON_KEY_IMON;
1603 kc = imon_remote_key_lookup(ictx, scancode);
1607 spin_lock_irqsave(&ictx->kc_lock, flags);
1608 /* keyboard/mouse mode toggle button */
1609 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1610 ictx->last_keycode = kc;
1612 ictx->pad_mouse = !ictx->pad_mouse;
1613 dev_dbg(dev, "toggling to %s mode\n",
1614 ictx->pad_mouse ? "mouse" : "keyboard");
1615 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1618 ictx->pad_mouse = false;
1619 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1624 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1626 /* send touchscreen events through input subsystem if touchpad data */
1627 if (ictx->touch && len == 8 && buf[7] == 0x86) {
1628 imon_touch_event(ictx, buf);
1631 /* look for mouse events with pad in mouse mode */
1632 } else if (ictx->pad_mouse) {
1633 if (imon_mouse_event(ictx, buf, len))
1637 /* Now for some special handling to convert pad input to arrow keys */
1638 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1639 ((len == 8) && (buf[0] & 0x40) &&
1640 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1642 imon_pad_to_keys(ictx, buf);
1646 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1650 press_type = imon_parse_press_type(ictx, buf, ktype);
1652 goto not_input_data;
1654 if (ktype != IMON_KEY_PANEL) {
1655 if (press_type == 0)
1656 rc_keyup(ictx->rdev);
1658 enum rc_proto proto;
1660 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1661 proto = RC_PROTO_RC6_MCE;
1662 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1663 proto = RC_PROTO_IMON;
1667 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1670 spin_lock_irqsave(&ictx->kc_lock, flags);
1671 ictx->last_keycode = ictx->kc;
1672 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1677 /* Only panel type events left to process now */
1678 spin_lock_irqsave(&ictx->kc_lock, flags);
1681 /* KEY repeats from knob and panel that need to be suppressed */
1682 if (ictx->kc == KEY_MUTE ||
1683 ictx->dev_descr->flags & IMON_SUPPRESS_REPEATED_KEYS) {
1684 if (ictx->kc == ictx->last_keycode &&
1685 ktime_ms_delta(t, prev_time) < ictx->idev->rep[REP_DELAY]) {
1686 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1694 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1696 input_report_key(ictx->idev, kc, press_type);
1697 input_sync(ictx->idev);
1699 /* panel keys don't generate a release */
1700 input_report_key(ictx->idev, kc, 0);
1701 input_sync(ictx->idev);
1703 spin_lock_irqsave(&ictx->kc_lock, flags);
1704 ictx->last_keycode = kc;
1705 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1711 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1712 __func__, len, intf);
1716 /* iMON 2.4G associate frame */
1717 if (buf[0] == 0x00 &&
1718 buf[2] == 0xFF && /* REFID */
1721 buf[5] == 0xFF && /* iMON 2.4G */
1722 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1723 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1724 dev_warn(dev, "%s: remote associated refid=%02X\n",
1726 ictx->rf_isassociating = false;
1731 * Callback function for USB core API: receive data
1733 static void usb_rx_callback_intf0(struct urb *urb)
1735 struct imon_context *ictx;
1741 ictx = (struct imon_context *)urb->context;
1746 * if we get a callback before we're done configuring the hardware, we
1747 * can't yet process the data, as there's nowhere to send it, but we
1748 * still need to submit a new rx URB to avoid wedging the hardware
1750 if (!ictx->dev_present_intf0)
1753 switch (urb->status) {
1754 case -ENOENT: /* usbcore unlink successful! */
1757 case -ESHUTDOWN: /* transport endpoint was shut down */
1761 imon_incoming_packet(ictx, urb, intfnum);
1765 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1766 __func__, urb->status);
1771 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1774 static void usb_rx_callback_intf1(struct urb *urb)
1776 struct imon_context *ictx;
1782 ictx = (struct imon_context *)urb->context;
1787 * if we get a callback before we're done configuring the hardware, we
1788 * can't yet process the data, as there's nowhere to send it, but we
1789 * still need to submit a new rx URB to avoid wedging the hardware
1791 if (!ictx->dev_present_intf1)
1794 switch (urb->status) {
1795 case -ENOENT: /* usbcore unlink successful! */
1798 case -ESHUTDOWN: /* transport endpoint was shut down */
1802 imon_incoming_packet(ictx, urb, intfnum);
1806 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1807 __func__, urb->status);
1812 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1816 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1817 * devices, and all of them constantly spew interrupts, even when there
1818 * is no actual data to report. However, byte 6 of this buffer looks like
1819 * its unique across device variants, so we're trying to key off that to
1820 * figure out which display type (if any) and what IR protocol the device
1821 * actually supports. These devices have their IR protocol hard-coded into
1822 * their firmware, they can't be changed on the fly like the newer hardware.
1824 static void imon_get_ffdc_type(struct imon_context *ictx)
1826 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1827 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1828 u64 allowed_protos = RC_PROTO_BIT_IMON;
1830 switch (ffdc_cfg_byte) {
1831 /* iMON Knob, no display, iMON IR + vol knob */
1833 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1834 ictx->display_supported = false;
1836 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1838 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1839 ictx->display_supported = false;
1840 ictx->rf_device = true;
1842 /* iMON VFD, no IR (does have vol knob tho) */
1844 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1845 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1847 /* iMON VFD, iMON IR */
1851 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1852 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1854 /* iMON VFD, MCE IR */
1857 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1858 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1859 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1861 /* iMON VFD, iMON or MCE IR */
1863 dev_info(ictx->dev, "0xffdc iMON VFD, iMON or MCE IR");
1864 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1865 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1867 /* iMON LCD, MCE IR */
1869 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1870 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1871 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1873 /* no display, iMON IR */
1875 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1876 ictx->display_supported = false;
1878 /* Soundgraph iMON UltraBay */
1880 dev_info(ictx->dev, "0xffdc iMON UltraBay, LCD + IR");
1881 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1882 allowed_protos = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1883 ictx->dev_descr = &ultrabay_table;
1887 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
1888 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1890 * We don't know which one it is, allow user to set the
1891 * RC6 one from userspace if IMON wasn't correct.
1893 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1897 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1899 ictx->display_type = detected_display_type;
1900 ictx->rc_proto = allowed_protos;
1903 static void imon_set_display_type(struct imon_context *ictx)
1905 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1908 * Try to auto-detect the type of display if the user hasn't set
1909 * it by hand via the display_type modparam. Default is VFD.
1912 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1913 switch (ictx->product) {
1915 /* set in imon_get_ffdc_type() */
1916 configured_display_type = ictx->display_type;
1920 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1925 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1931 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1932 ictx->display_supported = false;
1937 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1941 configured_display_type = display_type;
1942 if (display_type == IMON_DISPLAY_TYPE_NONE)
1943 ictx->display_supported = false;
1945 ictx->display_supported = true;
1946 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1947 __func__, display_type);
1950 ictx->display_type = configured_display_type;
1953 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1955 struct rc_dev *rdev;
1957 static const unsigned char fp_packet[] = {
1958 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
1960 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
1962 dev_err(ictx->dev, "remote control dev allocation failed\n");
1966 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1967 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1968 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1969 sizeof(ictx->phys_rdev));
1970 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1972 rdev->device_name = ictx->name_rdev;
1973 rdev->input_phys = ictx->phys_rdev;
1974 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1975 rdev->dev.parent = ictx->dev;
1978 /* iMON PAD or MCE */
1979 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1980 rdev->change_protocol = imon_ir_change_protocol;
1981 rdev->driver_name = MOD_NAME;
1983 /* Enable front-panel buttons and/or knobs */
1984 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1985 ret = send_packet(ictx);
1986 /* Not fatal, but warn about it */
1988 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1990 if (ictx->product == 0xffdc) {
1991 imon_get_ffdc_type(ictx);
1992 rdev->allowed_protocols = ictx->rc_proto;
1995 imon_set_display_type(ictx);
1997 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1998 rdev->map_name = RC_MAP_IMON_MCE;
2000 rdev->map_name = RC_MAP_IMON_PAD;
2002 ret = rc_register_device(rdev);
2004 dev_err(ictx->dev, "remote input dev register failed\n");
2011 rc_free_device(rdev);
2015 static struct input_dev *imon_init_idev(struct imon_context *ictx)
2017 const struct imon_panel_key_table *key_table;
2018 struct input_dev *idev;
2021 key_table = ictx->dev_descr->key_table;
2023 idev = input_allocate_device();
2027 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
2028 "iMON Panel, Knob and Mouse(%04x:%04x)",
2029 ictx->vendor, ictx->product);
2030 idev->name = ictx->name_idev;
2032 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2033 sizeof(ictx->phys_idev));
2034 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
2035 idev->phys = ictx->phys_idev;
2037 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
2039 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2040 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2041 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2042 BIT_MASK(REL_WHEEL);
2044 /* panel and/or knob code support */
2045 for (i = 0; key_table[i].hw_code != 0; i++) {
2046 u32 kc = key_table[i].keycode;
2047 __set_bit(kc, idev->keybit);
2050 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2051 idev->dev.parent = ictx->dev;
2052 input_set_drvdata(idev, ictx);
2054 ret = input_register_device(idev);
2056 dev_err(ictx->dev, "input dev register failed\n");
2063 input_free_device(idev);
2067 static struct input_dev *imon_init_touch(struct imon_context *ictx)
2069 struct input_dev *touch;
2072 touch = input_allocate_device();
2074 goto touch_alloc_failed;
2076 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2077 "iMON USB Touchscreen (%04x:%04x)",
2078 ictx->vendor, ictx->product);
2079 touch->name = ictx->name_touch;
2081 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2082 sizeof(ictx->phys_touch));
2083 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
2084 touch->phys = ictx->phys_touch;
2087 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2088 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2089 BIT_MASK(BTN_TOUCH);
2090 input_set_abs_params(touch, ABS_X,
2092 input_set_abs_params(touch, ABS_Y,
2095 input_set_drvdata(touch, ictx);
2097 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2098 touch->dev.parent = ictx->dev;
2099 ret = input_register_device(touch);
2101 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2102 goto touch_register_failed;
2107 touch_register_failed:
2108 input_free_device(touch);
2114 static bool imon_find_endpoints(struct imon_context *ictx,
2115 struct usb_host_interface *iface_desc)
2117 struct usb_endpoint_descriptor *ep;
2118 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2119 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2120 int ifnum = iface_desc->desc.bInterfaceNumber;
2121 int num_endpts = iface_desc->desc.bNumEndpoints;
2122 int i, ep_dir, ep_type;
2123 bool ir_ep_found = false;
2124 bool display_ep_found = false;
2125 bool tx_control = false;
2128 * Scan the endpoint list and set:
2129 * first input endpoint = IR endpoint
2130 * first output endpoint = display endpoint
2132 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2133 ep = &iface_desc->endpoint[i].desc;
2134 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2135 ep_type = usb_endpoint_type(ep);
2137 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2138 ep_type == USB_ENDPOINT_XFER_INT) {
2142 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2144 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2145 ep_type == USB_ENDPOINT_XFER_INT) {
2147 display_ep_found = true;
2148 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2153 ictx->rx_endpoint_intf0 = rx_endpoint;
2155 * tx is used to send characters to lcd/vfd, associate RF
2156 * remotes, set IR protocol, and maybe more...
2158 ictx->tx_endpoint = tx_endpoint;
2160 ictx->rx_endpoint_intf1 = rx_endpoint;
2164 * If we didn't find a display endpoint, this is probably one of the
2165 * newer iMON devices that use control urb instead of interrupt
2167 if (!display_ep_found) {
2169 display_ep_found = true;
2170 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2175 * Some iMON receivers have no display. Unfortunately, it seems
2176 * that SoundGraph recycles device IDs between devices both with
2179 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2180 display_ep_found = false;
2181 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2185 * iMON Touch devices have a VGA touchscreen, but no "display", as
2186 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2188 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2189 display_ep_found = false;
2190 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2193 /* Input endpoint is mandatory */
2195 pr_err("no valid input (IR) endpoint found\n");
2197 ictx->tx_control = tx_control;
2199 if (display_ep_found)
2200 ictx->display_supported = true;
2206 static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2207 const struct usb_device_id *id)
2209 struct imon_context *ictx;
2212 struct device *dev = &intf->dev;
2213 struct usb_host_interface *iface_desc;
2216 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
2220 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2222 goto rx_urb_alloc_failed;
2223 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2225 goto tx_urb_alloc_failed;
2227 mutex_init(&ictx->lock);
2228 spin_lock_init(&ictx->kc_lock);
2230 mutex_lock(&ictx->lock);
2233 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2234 ictx->rx_urb_intf0 = rx_urb;
2235 ictx->tx_urb = tx_urb;
2236 ictx->rf_device = false;
2238 init_completion(&ictx->tx.finished);
2240 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2241 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2243 /* save drive info for later accessing the panel/knob key table */
2244 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
2245 /* default send_packet delay is 5ms but some devices need more */
2246 ictx->send_packet_delay = ictx->dev_descr->flags &
2247 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
2250 iface_desc = intf->cur_altsetting;
2251 if (!imon_find_endpoints(ictx, iface_desc)) {
2252 goto find_endpoint_failed;
2255 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2256 usb_rcvintpipe(ictx->usbdev_intf0,
2257 ictx->rx_endpoint_intf0->bEndpointAddress),
2258 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2259 usb_rx_callback_intf0, ictx,
2260 ictx->rx_endpoint_intf0->bInterval);
2262 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2264 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2265 goto urb_submit_failed;
2268 ictx->idev = imon_init_idev(ictx);
2270 dev_err(dev, "%s: input device setup failed\n", __func__);
2271 goto idev_setup_failed;
2274 ictx->rdev = imon_init_rdev(ictx);
2276 dev_err(dev, "%s: rc device setup failed\n", __func__);
2277 goto rdev_setup_failed;
2280 ictx->dev_present_intf0 = true;
2282 mutex_unlock(&ictx->lock);
2286 input_unregister_device(ictx->idev);
2288 usb_kill_urb(ictx->rx_urb_intf0);
2290 find_endpoint_failed:
2291 usb_put_dev(ictx->usbdev_intf0);
2292 mutex_unlock(&ictx->lock);
2293 usb_free_urb(tx_urb);
2294 tx_urb_alloc_failed:
2295 usb_free_urb(rx_urb);
2296 rx_urb_alloc_failed:
2299 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2304 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2305 struct imon_context *ictx)
2308 struct usb_host_interface *iface_desc;
2311 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2313 goto rx_urb_alloc_failed;
2315 mutex_lock(&ictx->lock);
2317 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2318 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
2321 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2322 ictx->rx_urb_intf1 = rx_urb;
2325 iface_desc = intf->cur_altsetting;
2326 if (!imon_find_endpoints(ictx, iface_desc))
2327 goto find_endpoint_failed;
2329 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2330 ictx->touch = imon_init_touch(ictx);
2332 goto touch_setup_failed;
2336 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2337 usb_rcvintpipe(ictx->usbdev_intf1,
2338 ictx->rx_endpoint_intf1->bEndpointAddress),
2339 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2340 usb_rx_callback_intf1, ictx,
2341 ictx->rx_endpoint_intf1->bInterval);
2343 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2346 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2347 goto urb_submit_failed;
2350 ictx->dev_present_intf1 = true;
2352 mutex_unlock(&ictx->lock);
2357 input_unregister_device(ictx->touch);
2359 find_endpoint_failed:
2360 usb_put_dev(ictx->usbdev_intf1);
2361 mutex_unlock(&ictx->lock);
2362 usb_free_urb(rx_urb);
2363 rx_urb_alloc_failed:
2364 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
2369 static void imon_init_display(struct imon_context *ictx,
2370 struct usb_interface *intf)
2374 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2376 /* set up sysfs entry for built-in clock */
2377 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2379 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2382 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2383 ret = usb_register_dev(intf, &imon_lcd_class);
2385 ret = usb_register_dev(intf, &imon_vfd_class);
2387 /* Not a fatal error, so ignore */
2388 dev_info(ictx->dev, "could not get a minor number for display\n");
2393 * Callback function for USB core API: Probe
2395 static int imon_probe(struct usb_interface *interface,
2396 const struct usb_device_id *id)
2398 struct usb_device *usbdev = NULL;
2399 struct usb_host_interface *iface_desc = NULL;
2400 struct usb_interface *first_if;
2401 struct device *dev = &interface->dev;
2402 int ifnum, sysfs_err;
2404 struct imon_context *ictx = NULL;
2405 struct imon_context *first_if_ctx = NULL;
2406 u16 vendor, product;
2408 usbdev = usb_get_dev(interface_to_usbdev(interface));
2409 iface_desc = interface->cur_altsetting;
2410 ifnum = iface_desc->desc.bInterfaceNumber;
2411 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2412 product = le16_to_cpu(usbdev->descriptor.idProduct);
2414 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2415 __func__, vendor, product, ifnum);
2417 /* prevent races probing devices w/multiple interfaces */
2418 mutex_lock(&driver_lock);
2420 first_if = usb_ifnum_to_if(usbdev, 0);
2426 first_if_ctx = usb_get_intfdata(first_if);
2429 ictx = imon_init_intf0(interface, id);
2431 pr_err("failed to initialize context!\n");
2437 /* this is the secondary interface on the device */
2439 /* fail early if first intf failed to register */
2440 if (!first_if_ctx) {
2445 ictx = imon_init_intf1(interface, first_if_ctx);
2447 pr_err("failed to attach to context!\n");
2454 usb_set_intfdata(interface, ictx);
2457 mutex_lock(&ictx->lock);
2459 if (product == 0xffdc && ictx->rf_device) {
2460 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2461 &imon_rf_attr_group);
2463 pr_err("Could not create RF sysfs entries(%d)\n",
2467 if (ictx->display_supported)
2468 imon_init_display(ictx, interface);
2470 mutex_unlock(&ictx->lock);
2473 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2474 vendor, product, ifnum,
2475 usbdev->bus->busnum, usbdev->devnum);
2477 mutex_unlock(&driver_lock);
2478 usb_put_dev(usbdev);
2483 mutex_unlock(&driver_lock);
2484 usb_put_dev(usbdev);
2485 dev_err(dev, "unable to register, err %d\n", ret);
2491 * Callback function for USB core API: disconnect
2493 static void imon_disconnect(struct usb_interface *interface)
2495 struct imon_context *ictx;
2499 /* prevent races with multi-interface device probing and display_open */
2500 mutex_lock(&driver_lock);
2502 ictx = usb_get_intfdata(interface);
2504 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2507 * sysfs_remove_group is safe to call even if sysfs_create_group
2508 * hasn't been called
2510 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2511 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2513 usb_set_intfdata(interface, NULL);
2515 /* Abort ongoing write */
2516 if (ictx->tx.busy) {
2517 usb_kill_urb(ictx->tx_urb);
2518 complete(&ictx->tx.finished);
2522 ictx->dev_present_intf0 = false;
2523 usb_kill_urb(ictx->rx_urb_intf0);
2524 usb_put_dev(ictx->usbdev_intf0);
2525 input_unregister_device(ictx->idev);
2526 rc_unregister_device(ictx->rdev);
2527 if (ictx->display_supported) {
2528 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2529 usb_deregister_dev(interface, &imon_lcd_class);
2530 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
2531 usb_deregister_dev(interface, &imon_vfd_class);
2534 ictx->dev_present_intf1 = false;
2535 usb_kill_urb(ictx->rx_urb_intf1);
2536 usb_put_dev(ictx->usbdev_intf1);
2537 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2538 input_unregister_device(ictx->touch);
2539 del_timer_sync(&ictx->ttimer);
2543 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2544 free_imon_context(ictx);
2546 mutex_unlock(&driver_lock);
2548 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2552 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2554 struct imon_context *ictx = usb_get_intfdata(intf);
2555 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2558 usb_kill_urb(ictx->rx_urb_intf0);
2560 usb_kill_urb(ictx->rx_urb_intf1);
2565 static int imon_resume(struct usb_interface *intf)
2568 struct imon_context *ictx = usb_get_intfdata(intf);
2569 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2572 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2573 usb_rcvintpipe(ictx->usbdev_intf0,
2574 ictx->rx_endpoint_intf0->bEndpointAddress),
2575 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2576 usb_rx_callback_intf0, ictx,
2577 ictx->rx_endpoint_intf0->bInterval);
2579 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2582 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2583 usb_rcvintpipe(ictx->usbdev_intf1,
2584 ictx->rx_endpoint_intf1->bEndpointAddress),
2585 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2586 usb_rx_callback_intf1, ictx,
2587 ictx->rx_endpoint_intf1->bInterval);
2589 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2595 module_usb_driver(imon_driver);