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 */
157 * Fields for deferring free_imon_context().
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".
165 * Use a flag for telling display_open()/vfd_write()/lcd_write() that
166 * imon_disconnect() was already called.
170 * We need to wait for RCU grace period in order to allow
171 * display_open() to safely check ->disconnected and increment ->users.
176 #define TOUCH_TIMEOUT (HZ/30)
178 /* vfd character device file operations */
179 static const struct file_operations vfd_fops = {
180 .owner = THIS_MODULE,
181 .open = display_open,
183 .release = display_close,
184 .llseek = noop_llseek,
187 /* lcd character device file operations */
188 static const struct file_operations lcd_fops = {
189 .owner = THIS_MODULE,
190 .open = display_open,
192 .release = display_close,
193 .llseek = noop_llseek,
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,
210 static struct usb_class_driver imon_vfd_class = {
213 .minor_base = DISPLAY_MINOR_BASE,
216 static struct usb_class_driver imon_lcd_class = {
219 .minor_base = DISPLAY_MINOR_BASE,
222 /* imon receiver front panel/knob key table */
223 static const struct imon_usb_dev_descr imon_default_table = {
224 .flags = IMON_NO_FLAGS,
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 },
272 static const struct imon_usb_dev_descr imon_OEM_VFD = {
273 .flags = IMON_NEED_20MS_PKT_DELAY,
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 },
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,
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 },
338 /* imon ultrabay front panel key table */
339 static const struct imon_usb_dev_descr ultrabay_table = {
340 .flags = IMON_SUPPRESS_REPEATED_KEYS,
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 },
365 * USB Device ID for iMON USB Control Boards
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.
375 static const struct usb_device_id imon_usb_id_table[] = {
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)
382 { USB_DEVICE(0x15c2, 0xffdc),
383 .driver_info = (unsigned long)&imon_default_table },
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.
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},
450 /* USB Device data */
451 static struct usb_driver imon_driver = {
454 .disconnect = imon_disconnect,
455 .suspend = imon_suspend,
456 .resume = imon_resume,
457 .id_table = imon_usb_id_table,
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);
468 module_param(debug, bool, S_IRUGO | S_IWUSR);
469 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
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)");
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).");
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.
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)");
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)");
494 static void free_imon_context(struct imon_context *ictx)
496 struct device *dev = ictx->dev;
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);
505 dev_dbg(dev, "%s: iMON context freed\n", __func__);
509 * Called when the Display device (e.g. /dev/lcd0)
510 * is opened by the application.
512 static int display_open(struct inode *inode, struct file *file)
514 struct usb_interface *interface;
515 struct imon_context *ictx = NULL;
519 subminor = iminor(inode);
520 interface = usb_find_interface(&imon_driver, subminor);
522 pr_err("could not find interface for minor %d\n", subminor);
528 ictx = usb_get_intfdata(interface);
529 if (!ictx || ictx->disconnected || !refcount_inc_not_zero(&ictx->users)) {
531 pr_err("no context found for minor %d\n", subminor);
537 mutex_lock(&ictx->lock);
539 if (!ictx->display_supported) {
540 pr_err("display not supported by device\n");
542 } else if (ictx->display_isopen) {
543 pr_err("display port is already open\n");
546 ictx->display_isopen = true;
547 file->private_data = ictx;
548 dev_dbg(ictx->dev, "display port opened\n");
551 mutex_unlock(&ictx->lock);
553 if (retval && refcount_dec_and_test(&ictx->users))
554 free_imon_context(ictx);
561 * Called when the display device (e.g. /dev/lcd0)
562 * is closed by the application.
564 static int display_close(struct inode *inode, struct file *file)
566 struct imon_context *ictx = file->private_data;
569 mutex_lock(&ictx->lock);
571 if (!ictx->display_supported) {
572 pr_err("display not supported by device\n");
574 } else if (!ictx->display_isopen) {
575 pr_err("display is not open\n");
578 ictx->display_isopen = false;
579 dev_dbg(ictx->dev, "display port closed\n");
582 mutex_unlock(&ictx->lock);
583 if (refcount_dec_and_test(&ictx->users))
584 free_imon_context(ictx);
589 * Sends a packet to the device -- this function must be called with
590 * ictx->lock held, or its unlock/lock sequence while waiting for tx
591 * to complete can/will lead to a deadlock.
593 static int send_packet(struct imon_context *ictx)
596 unsigned long timeout;
599 struct usb_ctrlrequest *control_req = NULL;
601 /* Check if we need to use control or interrupt urb */
602 if (!ictx->tx_control) {
603 pipe = usb_sndintpipe(ictx->usbdev_intf0,
604 ictx->tx_endpoint->bEndpointAddress);
605 interval = ictx->tx_endpoint->bInterval;
607 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
609 sizeof(ictx->usb_tx_buf),
610 usb_tx_callback, ictx, interval);
612 ictx->tx_urb->actual_length = 0;
614 /* fill request into kmalloc'ed space: */
615 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
616 if (control_req == NULL)
619 /* setup packet is '21 09 0200 0001 0008' */
620 control_req->bRequestType = 0x21;
621 control_req->bRequest = 0x09;
622 control_req->wValue = cpu_to_le16(0x0200);
623 control_req->wIndex = cpu_to_le16(0x0001);
624 control_req->wLength = cpu_to_le16(0x0008);
626 /* control pipe is endpoint 0x00 */
627 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
629 /* build the control urb */
630 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
631 pipe, (unsigned char *)control_req,
633 sizeof(ictx->usb_tx_buf),
634 usb_tx_callback, ictx);
635 ictx->tx_urb->actual_length = 0;
638 reinit_completion(&ictx->tx.finished);
639 ictx->tx.busy = true;
640 smp_rmb(); /* ensure later readers know we're busy */
642 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
644 ictx->tx.busy = false;
645 smp_rmb(); /* ensure later readers know we're not busy */
646 pr_err_ratelimited("error submitting urb(%d)\n", retval);
648 /* Wait for transmission to complete (or abort) */
649 mutex_unlock(&ictx->lock);
650 retval = wait_for_completion_interruptible(
653 usb_kill_urb(ictx->tx_urb);
654 pr_err_ratelimited("task interrupted\n");
656 mutex_lock(&ictx->lock);
658 retval = ictx->tx.status;
660 pr_err_ratelimited("packet tx failed (%d)\n", retval);
666 * Induce a mandatory delay before returning, as otherwise,
667 * send_packet can get called so rapidly as to overwhelm the device,
668 * particularly on faster systems and/or those with quirky usb.
670 timeout = msecs_to_jiffies(ictx->send_packet_delay);
671 set_current_state(TASK_INTERRUPTIBLE);
672 schedule_timeout(timeout);
678 * Sends an associate packet to the iMON 2.4G.
680 * This might not be such a good idea, since it has an id collision with
681 * some versions of the "IR & VFD" combo. The only way to determine if it
682 * is an RF version is to look at the product description string. (Which
683 * we currently do not fetch).
685 static int send_associate_24g(struct imon_context *ictx)
688 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
689 0x00, 0x00, 0x00, 0x20 };
692 pr_err("no context for device\n");
696 if (!ictx->dev_present_intf0) {
697 pr_err("no iMON device present\n");
701 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
702 retval = send_packet(ictx);
708 * Sends packets to setup and show clock on iMON display
710 * Arguments: year - last 2 digits of year, month - 1..12,
711 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
712 * hour - 0..23, minute - 0..59, second - 0..59
714 static int send_set_imon_clock(struct imon_context *ictx,
715 unsigned int year, unsigned int month,
716 unsigned int day, unsigned int dow,
717 unsigned int hour, unsigned int minute,
720 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
725 pr_err("no context for device\n");
729 switch (ictx->display_type) {
730 case IMON_DISPLAY_TYPE_LCD:
731 clock_enable_pkt[0][0] = 0x80;
732 clock_enable_pkt[0][1] = year;
733 clock_enable_pkt[0][2] = month-1;
734 clock_enable_pkt[0][3] = day;
735 clock_enable_pkt[0][4] = hour;
736 clock_enable_pkt[0][5] = minute;
737 clock_enable_pkt[0][6] = second;
739 clock_enable_pkt[1][0] = 0x80;
740 clock_enable_pkt[1][1] = 0;
741 clock_enable_pkt[1][2] = 0;
742 clock_enable_pkt[1][3] = 0;
743 clock_enable_pkt[1][4] = 0;
744 clock_enable_pkt[1][5] = 0;
745 clock_enable_pkt[1][6] = 0;
747 if (ictx->product == 0xffdc) {
748 clock_enable_pkt[0][7] = 0x50;
749 clock_enable_pkt[1][7] = 0x51;
751 clock_enable_pkt[0][7] = 0x88;
752 clock_enable_pkt[1][7] = 0x8a;
757 case IMON_DISPLAY_TYPE_VFD:
758 clock_enable_pkt[0][0] = year;
759 clock_enable_pkt[0][1] = month-1;
760 clock_enable_pkt[0][2] = day;
761 clock_enable_pkt[0][3] = dow;
762 clock_enable_pkt[0][4] = hour;
763 clock_enable_pkt[0][5] = minute;
764 clock_enable_pkt[0][6] = second;
765 clock_enable_pkt[0][7] = 0x40;
767 clock_enable_pkt[1][0] = 0;
768 clock_enable_pkt[1][1] = 0;
769 clock_enable_pkt[1][2] = 1;
770 clock_enable_pkt[1][3] = 0;
771 clock_enable_pkt[1][4] = 0;
772 clock_enable_pkt[1][5] = 0;
773 clock_enable_pkt[1][6] = 0;
774 clock_enable_pkt[1][7] = 0x42;
782 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
783 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
784 retval = send_packet(ictx);
786 pr_err("send_packet failed for packet %d\n", i);
795 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
797 static ssize_t associate_remote_show(struct device *d,
798 struct device_attribute *attr,
801 struct imon_context *ictx = dev_get_drvdata(d);
806 mutex_lock(&ictx->lock);
807 if (ictx->rf_isassociating)
808 strscpy(buf, "associating\n", PAGE_SIZE);
810 strscpy(buf, "closed\n", PAGE_SIZE);
812 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");
813 mutex_unlock(&ictx->lock);
817 static ssize_t associate_remote_store(struct device *d,
818 struct device_attribute *attr,
819 const char *buf, size_t count)
821 struct imon_context *ictx;
823 ictx = dev_get_drvdata(d);
828 mutex_lock(&ictx->lock);
829 ictx->rf_isassociating = true;
830 send_associate_24g(ictx);
831 mutex_unlock(&ictx->lock);
837 * sysfs functions to control internal imon clock
839 static ssize_t imon_clock_show(struct device *d,
840 struct device_attribute *attr, char *buf)
842 struct imon_context *ictx = dev_get_drvdata(d);
848 mutex_lock(&ictx->lock);
850 if (!ictx->display_supported) {
851 len = snprintf(buf, PAGE_SIZE, "Not supported.");
853 len = snprintf(buf, PAGE_SIZE,
854 "To set the clock on your iMON display:\n"
855 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
856 "%s", ictx->display_isopen ?
857 "\nNOTE: imon device must be closed\n" : "");
860 mutex_unlock(&ictx->lock);
865 static ssize_t imon_clock_store(struct device *d,
866 struct device_attribute *attr,
867 const char *buf, size_t count)
869 struct imon_context *ictx = dev_get_drvdata(d);
871 unsigned int year, month, day, dow, hour, minute, second;
876 mutex_lock(&ictx->lock);
878 if (!ictx->display_supported) {
881 } else if (ictx->display_isopen) {
886 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
887 &hour, &minute, &second) != 7) {
892 if ((month < 1 || month > 12) ||
893 (day < 1 || day > 31) || (dow > 6) ||
894 (hour > 23) || (minute > 59) || (second > 59)) {
899 retval = send_set_imon_clock(ictx, year, month, day, dow,
900 hour, minute, second);
906 mutex_unlock(&ictx->lock);
912 static DEVICE_ATTR_RW(imon_clock);
913 static DEVICE_ATTR_RW(associate_remote);
915 static struct attribute *imon_display_sysfs_entries[] = {
916 &dev_attr_imon_clock.attr,
920 static const struct attribute_group imon_display_attr_group = {
921 .attrs = imon_display_sysfs_entries
924 static struct attribute *imon_rf_sysfs_entries[] = {
925 &dev_attr_associate_remote.attr,
929 static const struct attribute_group imon_rf_attr_group = {
930 .attrs = imon_rf_sysfs_entries
934 * Writes data to the VFD. The iMON VFD is 2x16 characters
935 * and requires data in 5 consecutive USB interrupt packets,
936 * each packet but the last carrying 7 bytes.
938 * I don't know if the VFD board supports features such as
939 * scrolling, clearing rows, blanking, etc. so at
940 * the caller must provide a full screen of data. If fewer
941 * than 32 bytes are provided spaces will be appended to
942 * generate a full screen.
944 static ssize_t vfd_write(struct file *file, const char __user *buf,
945 size_t n_bytes, loff_t *pos)
951 struct imon_context *ictx = file->private_data;
952 static const unsigned char vfd_packet6[] = {
953 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
955 if (ictx->disconnected)
958 mutex_lock(&ictx->lock);
960 if (!ictx->dev_present_intf0) {
961 pr_err_ratelimited("no iMON device present\n");
966 if (n_bytes <= 0 || n_bytes > 32) {
967 pr_err_ratelimited("invalid payload size\n");
972 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
977 /* Pad with spaces */
978 for (i = n_bytes; i < 32; ++i)
979 ictx->tx.data_buf[i] = ' ';
981 for (i = 32; i < 35; ++i)
982 ictx->tx.data_buf[i] = 0xFF;
988 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
989 ictx->usb_tx_buf[7] = (unsigned char) seq;
991 retval = send_packet(ictx);
993 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
1000 } while (offset < 35);
1002 /* Send packet #6 */
1003 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
1004 ictx->usb_tx_buf[7] = (unsigned char) seq;
1005 retval = send_packet(ictx);
1007 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
1010 mutex_unlock(&ictx->lock);
1012 return (!retval) ? n_bytes : retval;
1016 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
1017 * packets. We accept data as 16 hexadecimal digits, followed by a
1018 * newline (to make it easy to drive the device from a command-line
1019 * -- even though the actual binary data is a bit complicated).
1021 * The device itself is not a "traditional" text-mode display. It's
1022 * actually a 16x96 pixel bitmap display. That means if you want to
1023 * display text, you've got to have your own "font" and translate the
1024 * text into bitmaps for display. This is really flexible (you can
1025 * display whatever diacritics you need, and so on), but it's also
1026 * a lot more complicated than most LCDs...
1028 static ssize_t lcd_write(struct file *file, const char __user *buf,
1029 size_t n_bytes, loff_t *pos)
1032 struct imon_context *ictx = file->private_data;
1034 if (ictx->disconnected)
1037 mutex_lock(&ictx->lock);
1039 if (!ictx->display_supported) {
1040 pr_err_ratelimited("no iMON display present\n");
1046 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1052 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1057 retval = send_packet(ictx);
1059 pr_err_ratelimited("send packet failed!\n");
1062 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1063 __func__, (int) n_bytes);
1066 mutex_unlock(&ictx->lock);
1067 return (!retval) ? n_bytes : retval;
1071 * Callback function for USB core API: transmit data
1073 static void usb_tx_callback(struct urb *urb)
1075 struct imon_context *ictx;
1079 ictx = (struct imon_context *)urb->context;
1083 ictx->tx.status = urb->status;
1085 /* notify waiters that write has finished */
1086 ictx->tx.busy = false;
1087 smp_rmb(); /* ensure later readers know we're not busy */
1088 complete(&ictx->tx.finished);
1092 * report touchscreen input
1094 static void imon_touch_display_timeout(struct timer_list *t)
1096 struct imon_context *ictx = from_timer(ictx, t, ttimer);
1098 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
1101 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1102 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1103 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1104 input_sync(ictx->touch);
1108 * iMON IR receivers support two different signal sets -- those used by
1109 * the iMON remotes, and those used by the Windows MCE remotes (which is
1110 * really just RC-6), but only one or the other at a time, as the signals
1111 * are decoded onboard the receiver.
1113 * This function gets called two different ways, one way is from
1114 * rc_register_device, for initial protocol selection/setup, and the other is
1115 * via a userspace-initiated protocol change request, either by direct sysfs
1116 * prodding or by something like ir-keytable. In the rc_register_device case,
1117 * the imon context lock is already held, but when initiated from userspace,
1118 * it is not, so we must acquire it prior to calling send_packet, which
1119 * requires that the lock is held.
1121 static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
1124 struct imon_context *ictx = rc->priv;
1125 struct device *dev = ictx->dev;
1126 bool unlock = false;
1127 unsigned char ir_proto_packet[] = {
1128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1130 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
1131 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
1133 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
1134 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1135 ir_proto_packet[0] = 0x01;
1136 *rc_proto = RC_PROTO_BIT_RC6_MCE;
1137 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
1138 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1140 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1141 /* ir_proto_packet[0] = 0x00; // already the default */
1142 *rc_proto = RC_PROTO_BIT_IMON;
1144 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
1146 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1147 /* ir_proto_packet[0] = 0x00; // already the default */
1148 *rc_proto = RC_PROTO_BIT_IMON;
1151 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1153 if (!mutex_is_locked(&ictx->lock)) {
1155 mutex_lock(&ictx->lock);
1158 retval = send_packet(ictx);
1162 ictx->rc_proto = *rc_proto;
1163 ictx->pad_mouse = false;
1167 mutex_unlock(&ictx->lock);
1173 * The directional pad behaves a bit differently, depending on whether this is
1174 * one of the older ffdc devices or a newer device. Newer devices appear to
1175 * have a higher resolution matrix for more precise mouse movement, but it
1176 * makes things overly sensitive in keyboard mode, so we do some interesting
1177 * contortions to make it less touchy. Older devices run through the same
1178 * routine with shorter timeout and a smaller threshold.
1180 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1183 static ktime_t prev_time;
1184 static ktime_t hit_time;
1185 static int x, y, prev_result, hits;
1187 long msec, msec_hit;
1190 msec = ktime_ms_delta(ct, prev_time);
1191 msec_hit = ktime_ms_delta(ct, hit_time);
1204 if (abs(x) > threshold || abs(y) > threshold) {
1205 if (abs(y) > abs(x))
1206 result = (y > 0) ? 0x7F : 0x80;
1208 result = (x > 0) ? 0x7F00 : 0x8000;
1213 if (result == prev_result) {
1219 y = 17 * threshold / 30;
1222 y -= 17 * threshold / 30;
1225 x = 17 * threshold / 30;
1228 x -= 17 * threshold / 30;
1233 if (hits == 2 && msec_hit < timeout) {
1238 prev_result = result;
1247 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1251 bool is_release_code = false;
1253 /* Look for the initial press of a button */
1254 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1255 ictx->rc_toggle = 0x0;
1256 ictx->rc_scancode = scancode;
1258 /* Look for the release of a button */
1259 if (keycode == KEY_RESERVED) {
1260 release = scancode & ~0x4000;
1261 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1262 if (keycode != KEY_RESERVED)
1263 is_release_code = true;
1266 ictx->release_code = is_release_code;
1271 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1275 #define MCE_KEY_MASK 0x7000
1276 #define MCE_TOGGLE_BIT 0x8000
1279 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1280 * (the toggle bit flipping between alternating key presses), while
1281 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1282 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1283 * but we can't or them into all codes, as some keys are decoded in
1284 * a different way w/o the same use of the toggle bit...
1286 if (scancode & 0x80000000)
1287 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1289 ictx->rc_scancode = scancode;
1290 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1292 /* not used in mce mode, but make sure we know its false */
1293 ictx->release_code = false;
1298 static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
1300 const struct imon_panel_key_table *key_table;
1301 u32 keycode = KEY_RESERVED;
1304 key_table = ictx->dev_descr->key_table;
1306 for (i = 0; key_table[i].hw_code != 0; i++) {
1307 if (key_table[i].hw_code == (code | 0xffee)) {
1308 keycode = key_table[i].keycode;
1312 ictx->release_code = false;
1316 static bool imon_mouse_event(struct imon_context *ictx,
1317 unsigned char *buf, int len)
1319 signed char rel_x = 0x00, rel_y = 0x00;
1321 bool mouse_input = true;
1323 unsigned long flags;
1325 spin_lock_irqsave(&ictx->kc_lock, flags);
1327 /* newer iMON device PAD or mouse button */
1328 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1332 /* 0xffdc iMON PAD or mouse button input */
1333 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1334 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1335 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1336 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1339 rel_x = rel_x + rel_x / 2;
1340 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1341 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1344 rel_y = rel_y + rel_y / 2;
1346 /* some ffdc devices decode mouse buttons differently... */
1347 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1349 /* ch+/- buttons, which we use for an emulated scroll wheel */
1350 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1352 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1355 mouse_input = false;
1357 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1360 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1363 input_report_rel(ictx->idev, REL_WHEEL, dir);
1364 } else if (rel_x || rel_y) {
1365 input_report_rel(ictx->idev, REL_X, rel_x);
1366 input_report_rel(ictx->idev, REL_Y, rel_y);
1368 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1369 input_report_key(ictx->idev, BTN_RIGHT,
1370 buf[1] >> right_shift & 0x1);
1372 input_sync(ictx->idev);
1373 spin_lock_irqsave(&ictx->kc_lock, flags);
1374 ictx->last_keycode = ictx->kc;
1375 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1381 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1383 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1384 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1385 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1386 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1387 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1388 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1389 input_sync(ictx->touch);
1392 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1395 signed char rel_x = 0x00, rel_y = 0x00;
1396 u16 timeout, threshold;
1397 u32 scancode = KEY_RESERVED;
1398 unsigned long flags;
1401 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1402 * contain a position coordinate (x,y), with each component ranging
1403 * from -14 to 14. We want to down-sample this to only 4 discrete values
1404 * for up/down/left/right arrow keys. Also, when you get too close to
1405 * diagonals, it has a tendency to jump back and forth, so lets try to
1406 * ignore when they get too close.
1408 if (ictx->product != 0xffdc) {
1409 /* first, pad to 8 bytes so it conforms with everything else */
1410 buf[5] = buf[6] = buf[7] = 0;
1411 timeout = 500; /* in msecs */
1412 /* (2*threshold) x (2*threshold) square */
1413 threshold = pad_thresh ? pad_thresh : 28;
1417 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1418 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1419 dir = stabilize((int)rel_x, (int)rel_y,
1420 timeout, threshold);
1422 spin_lock_irqsave(&ictx->kc_lock,
1424 ictx->kc = KEY_UNKNOWN;
1425 spin_unlock_irqrestore(&ictx->kc_lock,
1429 buf[2] = dir & 0xFF;
1430 buf[3] = (dir >> 8) & 0xFF;
1431 scancode = be32_to_cpu(*((__be32 *)buf));
1435 * Hack alert: instead of using keycodes, we have
1436 * to use hard-coded scancodes here...
1438 if (abs(rel_y) > abs(rel_x)) {
1439 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1442 scancode = 0x01007f00; /* KEY_DOWN */
1444 scancode = 0x01008000; /* KEY_UP */
1447 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1449 scancode = 0x0100007f; /* KEY_RIGHT */
1451 scancode = 0x01000080; /* KEY_LEFT */
1456 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1457 * device (15c2:ffdc). The remote generates various codes from
1458 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1459 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1460 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1461 * reversed endianness. Extract direction from buffer, rotate endianness,
1462 * adjust sign and feed the values into stabilize(). The resulting codes
1463 * will be 0x01008000, 0x01007F00, which match the newer devices.
1466 timeout = 10; /* in msecs */
1467 /* (2*threshold) x (2*threshold) square */
1468 threshold = pad_thresh ? pad_thresh : 15;
1471 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1472 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1476 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1477 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1482 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1484 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
1485 dir = stabilize((int)rel_x, (int)rel_y,
1486 timeout, threshold);
1488 spin_lock_irqsave(&ictx->kc_lock, flags);
1489 ictx->kc = KEY_UNKNOWN;
1490 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1493 buf[2] = dir & 0xFF;
1494 buf[3] = (dir >> 8) & 0xFF;
1495 scancode = be32_to_cpu(*((__be32 *)buf));
1498 * Hack alert: instead of using keycodes, we have
1499 * to use hard-coded scancodes here...
1501 if (abs(rel_y) > abs(rel_x)) {
1502 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1505 scancode = 0x01007f00; /* KEY_DOWN */
1507 scancode = 0x01008000; /* KEY_UP */
1510 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1512 scancode = 0x0100007f; /* KEY_RIGHT */
1514 scancode = 0x01000080; /* KEY_LEFT */
1520 spin_lock_irqsave(&ictx->kc_lock, flags);
1521 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1522 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1527 * figure out if these is a press or a release. We don't actually
1528 * care about repeats, as those will be auto-generated within the IR
1529 * subsystem for repeating scancodes.
1531 static int imon_parse_press_type(struct imon_context *ictx,
1532 unsigned char *buf, u8 ktype)
1535 unsigned long flags;
1537 spin_lock_irqsave(&ictx->kc_lock, flags);
1539 /* key release of 0x02XXXXXX key */
1540 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1541 ictx->kc = ictx->last_keycode;
1543 /* mouse button release on (some) 0xffdc devices */
1544 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1545 buf[2] == 0x81 && buf[3] == 0xb7)
1546 ictx->kc = ictx->last_keycode;
1548 /* mouse button release on (some other) 0xffdc devices */
1549 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1550 buf[2] == 0x81 && buf[3] == 0xb7)
1551 ictx->kc = ictx->last_keycode;
1553 /* mce-specific button handling, no keyup events */
1554 else if (ktype == IMON_KEY_MCE) {
1555 ictx->rc_toggle = buf[2];
1558 /* incoherent or irrelevant data */
1559 } else if (ictx->kc == KEY_RESERVED)
1560 press_type = -EINVAL;
1562 /* key release of 0xXXXXXXb7 key */
1563 else if (ictx->release_code)
1566 /* this is a button press */
1570 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1576 * Process the incoming packet
1578 static void imon_incoming_packet(struct imon_context *ictx,
1579 struct urb *urb, int intf)
1581 int len = urb->actual_length;
1582 unsigned char *buf = urb->transfer_buffer;
1583 struct device *dev = ictx->dev;
1584 unsigned long flags;
1589 static ktime_t prev_time;
1592 /* filter out junk data on the older 0xffdc imon devices */
1593 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1596 /* Figure out what key was pressed */
1597 if (len == 8 && buf[7] == 0xee) {
1598 scancode = be64_to_cpu(*((__be64 *)buf));
1599 ktype = IMON_KEY_PANEL;
1600 kc = imon_panel_key_lookup(ictx, scancode);
1601 ictx->release_code = false;
1603 scancode = be32_to_cpu(*((__be32 *)buf));
1604 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
1605 ktype = IMON_KEY_IMON;
1607 ktype = IMON_KEY_MCE;
1608 kc = imon_mce_key_lookup(ictx, scancode);
1610 ktype = IMON_KEY_IMON;
1611 kc = imon_remote_key_lookup(ictx, scancode);
1615 spin_lock_irqsave(&ictx->kc_lock, flags);
1616 /* keyboard/mouse mode toggle button */
1617 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1618 ictx->last_keycode = kc;
1620 ictx->pad_mouse = !ictx->pad_mouse;
1621 dev_dbg(dev, "toggling to %s mode\n",
1622 ictx->pad_mouse ? "mouse" : "keyboard");
1623 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1626 ictx->pad_mouse = false;
1627 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1632 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1634 /* send touchscreen events through input subsystem if touchpad data */
1635 if (ictx->touch && len == 8 && buf[7] == 0x86) {
1636 imon_touch_event(ictx, buf);
1639 /* look for mouse events with pad in mouse mode */
1640 } else if (ictx->pad_mouse) {
1641 if (imon_mouse_event(ictx, buf, len))
1645 /* Now for some special handling to convert pad input to arrow keys */
1646 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1647 ((len == 8) && (buf[0] & 0x40) &&
1648 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1650 imon_pad_to_keys(ictx, buf);
1654 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1658 press_type = imon_parse_press_type(ictx, buf, ktype);
1660 goto not_input_data;
1662 if (ktype != IMON_KEY_PANEL) {
1663 if (press_type == 0)
1664 rc_keyup(ictx->rdev);
1666 enum rc_proto proto;
1668 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1669 proto = RC_PROTO_RC6_MCE;
1670 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1671 proto = RC_PROTO_IMON;
1675 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1678 spin_lock_irqsave(&ictx->kc_lock, flags);
1679 ictx->last_keycode = ictx->kc;
1680 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1685 /* Only panel type events left to process now */
1686 spin_lock_irqsave(&ictx->kc_lock, flags);
1689 /* KEY repeats from knob and panel that need to be suppressed */
1690 if (ictx->kc == KEY_MUTE ||
1691 ictx->dev_descr->flags & IMON_SUPPRESS_REPEATED_KEYS) {
1692 if (ictx->kc == ictx->last_keycode &&
1693 ktime_ms_delta(t, prev_time) < ictx->idev->rep[REP_DELAY]) {
1694 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1702 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1704 input_report_key(ictx->idev, kc, press_type);
1705 input_sync(ictx->idev);
1707 /* panel keys don't generate a release */
1708 input_report_key(ictx->idev, kc, 0);
1709 input_sync(ictx->idev);
1711 spin_lock_irqsave(&ictx->kc_lock, flags);
1712 ictx->last_keycode = kc;
1713 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1719 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1720 __func__, len, intf);
1724 /* iMON 2.4G associate frame */
1725 if (buf[0] == 0x00 &&
1726 buf[2] == 0xFF && /* REFID */
1729 buf[5] == 0xFF && /* iMON 2.4G */
1730 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1731 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1732 dev_warn(dev, "%s: remote associated refid=%02X\n",
1734 ictx->rf_isassociating = false;
1739 * Callback function for USB core API: receive data
1741 static void usb_rx_callback_intf0(struct urb *urb)
1743 struct imon_context *ictx;
1749 ictx = (struct imon_context *)urb->context;
1754 * if we get a callback before we're done configuring the hardware, we
1755 * can't yet process the data, as there's nowhere to send it, but we
1756 * still need to submit a new rx URB to avoid wedging the hardware
1758 if (!ictx->dev_present_intf0)
1761 switch (urb->status) {
1762 case -ENOENT: /* usbcore unlink successful! */
1765 case -ESHUTDOWN: /* transport endpoint was shut down */
1769 imon_incoming_packet(ictx, urb, intfnum);
1773 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1774 __func__, urb->status);
1779 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1782 static void usb_rx_callback_intf1(struct urb *urb)
1784 struct imon_context *ictx;
1790 ictx = (struct imon_context *)urb->context;
1795 * if we get a callback before we're done configuring the hardware, we
1796 * can't yet process the data, as there's nowhere to send it, but we
1797 * still need to submit a new rx URB to avoid wedging the hardware
1799 if (!ictx->dev_present_intf1)
1802 switch (urb->status) {
1803 case -ENOENT: /* usbcore unlink successful! */
1806 case -ESHUTDOWN: /* transport endpoint was shut down */
1810 imon_incoming_packet(ictx, urb, intfnum);
1814 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1815 __func__, urb->status);
1820 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1824 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1825 * devices, and all of them constantly spew interrupts, even when there
1826 * is no actual data to report. However, byte 6 of this buffer looks like
1827 * its unique across device variants, so we're trying to key off that to
1828 * figure out which display type (if any) and what IR protocol the device
1829 * actually supports. These devices have their IR protocol hard-coded into
1830 * their firmware, they can't be changed on the fly like the newer hardware.
1832 static void imon_get_ffdc_type(struct imon_context *ictx)
1834 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1835 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1836 u64 allowed_protos = RC_PROTO_BIT_IMON;
1838 switch (ffdc_cfg_byte) {
1839 /* iMON Knob, no display, iMON IR + vol knob */
1841 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1842 ictx->display_supported = false;
1844 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1846 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1847 ictx->display_supported = false;
1848 ictx->rf_device = true;
1850 /* iMON VFD, no IR (does have vol knob tho) */
1852 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1853 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1855 /* iMON VFD, iMON IR */
1859 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1860 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1862 /* iMON VFD, MCE IR */
1865 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1866 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1867 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1869 /* iMON VFD, iMON or MCE IR */
1871 dev_info(ictx->dev, "0xffdc iMON VFD, iMON or MCE IR");
1872 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1873 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1875 /* iMON LCD, MCE IR */
1877 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1878 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1879 allowed_protos = RC_PROTO_BIT_RC6_MCE;
1881 /* no display, iMON IR */
1883 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1884 ictx->display_supported = false;
1886 /* Soundgraph iMON UltraBay */
1888 dev_info(ictx->dev, "0xffdc iMON UltraBay, LCD + IR");
1889 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1890 allowed_protos = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1891 ictx->dev_descr = &ultrabay_table;
1895 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
1896 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1898 * We don't know which one it is, allow user to set the
1899 * RC6 one from userspace if IMON wasn't correct.
1901 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1905 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1907 ictx->display_type = detected_display_type;
1908 ictx->rc_proto = allowed_protos;
1911 static void imon_set_display_type(struct imon_context *ictx)
1913 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1916 * Try to auto-detect the type of display if the user hasn't set
1917 * it by hand via the display_type modparam. Default is VFD.
1920 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1921 switch (ictx->product) {
1923 /* set in imon_get_ffdc_type() */
1924 configured_display_type = ictx->display_type;
1928 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1933 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1939 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1940 ictx->display_supported = false;
1945 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1949 configured_display_type = display_type;
1950 if (display_type == IMON_DISPLAY_TYPE_NONE)
1951 ictx->display_supported = false;
1953 ictx->display_supported = true;
1954 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1955 __func__, display_type);
1958 ictx->display_type = configured_display_type;
1961 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1963 struct rc_dev *rdev;
1965 static const unsigned char fp_packet[] = {
1966 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
1968 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
1970 dev_err(ictx->dev, "remote control dev allocation failed\n");
1974 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1975 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1976 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1977 sizeof(ictx->phys_rdev));
1978 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1980 rdev->device_name = ictx->name_rdev;
1981 rdev->input_phys = ictx->phys_rdev;
1982 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1983 rdev->dev.parent = ictx->dev;
1986 /* iMON PAD or MCE */
1987 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1988 rdev->change_protocol = imon_ir_change_protocol;
1989 rdev->driver_name = MOD_NAME;
1991 /* Enable front-panel buttons and/or knobs */
1992 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1993 ret = send_packet(ictx);
1994 /* Not fatal, but warn about it */
1996 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1998 if (ictx->product == 0xffdc) {
1999 imon_get_ffdc_type(ictx);
2000 rdev->allowed_protocols = ictx->rc_proto;
2003 imon_set_display_type(ictx);
2005 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
2006 rdev->map_name = RC_MAP_IMON_MCE;
2008 rdev->map_name = RC_MAP_IMON_PAD;
2010 ret = rc_register_device(rdev);
2012 dev_err(ictx->dev, "remote input dev register failed\n");
2019 rc_free_device(rdev);
2023 static struct input_dev *imon_init_idev(struct imon_context *ictx)
2025 const struct imon_panel_key_table *key_table;
2026 struct input_dev *idev;
2029 key_table = ictx->dev_descr->key_table;
2031 idev = input_allocate_device();
2035 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
2036 "iMON Panel, Knob and Mouse(%04x:%04x)",
2037 ictx->vendor, ictx->product);
2038 idev->name = ictx->name_idev;
2040 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2041 sizeof(ictx->phys_idev));
2042 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
2043 idev->phys = ictx->phys_idev;
2045 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
2047 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2048 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2049 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2050 BIT_MASK(REL_WHEEL);
2052 /* panel and/or knob code support */
2053 for (i = 0; key_table[i].hw_code != 0; i++) {
2054 u32 kc = key_table[i].keycode;
2055 __set_bit(kc, idev->keybit);
2058 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2059 idev->dev.parent = ictx->dev;
2060 input_set_drvdata(idev, ictx);
2062 ret = input_register_device(idev);
2064 dev_err(ictx->dev, "input dev register failed\n");
2071 input_free_device(idev);
2075 static struct input_dev *imon_init_touch(struct imon_context *ictx)
2077 struct input_dev *touch;
2080 touch = input_allocate_device();
2082 goto touch_alloc_failed;
2084 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2085 "iMON USB Touchscreen (%04x:%04x)",
2086 ictx->vendor, ictx->product);
2087 touch->name = ictx->name_touch;
2089 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2090 sizeof(ictx->phys_touch));
2091 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
2092 touch->phys = ictx->phys_touch;
2095 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2096 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2097 BIT_MASK(BTN_TOUCH);
2098 input_set_abs_params(touch, ABS_X,
2100 input_set_abs_params(touch, ABS_Y,
2103 input_set_drvdata(touch, ictx);
2105 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2106 touch->dev.parent = ictx->dev;
2107 ret = input_register_device(touch);
2109 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2110 goto touch_register_failed;
2115 touch_register_failed:
2116 input_free_device(touch);
2122 static bool imon_find_endpoints(struct imon_context *ictx,
2123 struct usb_host_interface *iface_desc)
2125 struct usb_endpoint_descriptor *ep;
2126 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2127 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2128 int ifnum = iface_desc->desc.bInterfaceNumber;
2129 int num_endpts = iface_desc->desc.bNumEndpoints;
2130 int i, ep_dir, ep_type;
2131 bool ir_ep_found = false;
2132 bool display_ep_found = false;
2133 bool tx_control = false;
2136 * Scan the endpoint list and set:
2137 * first input endpoint = IR endpoint
2138 * first output endpoint = display endpoint
2140 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2141 ep = &iface_desc->endpoint[i].desc;
2142 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2143 ep_type = usb_endpoint_type(ep);
2145 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2146 ep_type == USB_ENDPOINT_XFER_INT) {
2150 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2152 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2153 ep_type == USB_ENDPOINT_XFER_INT) {
2155 display_ep_found = true;
2156 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2161 ictx->rx_endpoint_intf0 = rx_endpoint;
2163 * tx is used to send characters to lcd/vfd, associate RF
2164 * remotes, set IR protocol, and maybe more...
2166 ictx->tx_endpoint = tx_endpoint;
2168 ictx->rx_endpoint_intf1 = rx_endpoint;
2172 * If we didn't find a display endpoint, this is probably one of the
2173 * newer iMON devices that use control urb instead of interrupt
2175 if (!display_ep_found) {
2177 display_ep_found = true;
2178 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2183 * Some iMON receivers have no display. Unfortunately, it seems
2184 * that SoundGraph recycles device IDs between devices both with
2187 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2188 display_ep_found = false;
2189 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2193 * iMON Touch devices have a VGA touchscreen, but no "display", as
2194 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2196 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2197 display_ep_found = false;
2198 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2201 /* Input endpoint is mandatory */
2203 pr_err("no valid input (IR) endpoint found\n");
2205 ictx->tx_control = tx_control;
2207 if (display_ep_found)
2208 ictx->display_supported = true;
2214 static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2215 const struct usb_device_id *id)
2217 struct imon_context *ictx;
2220 struct device *dev = &intf->dev;
2221 struct usb_host_interface *iface_desc;
2224 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
2228 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2230 goto rx_urb_alloc_failed;
2231 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2233 goto tx_urb_alloc_failed;
2235 mutex_init(&ictx->lock);
2236 spin_lock_init(&ictx->kc_lock);
2238 mutex_lock(&ictx->lock);
2241 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2242 ictx->rx_urb_intf0 = rx_urb;
2243 ictx->tx_urb = tx_urb;
2244 ictx->rf_device = false;
2246 init_completion(&ictx->tx.finished);
2248 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2249 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2251 /* save drive info for later accessing the panel/knob key table */
2252 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
2253 /* default send_packet delay is 5ms but some devices need more */
2254 ictx->send_packet_delay = ictx->dev_descr->flags &
2255 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
2258 iface_desc = intf->cur_altsetting;
2259 if (!imon_find_endpoints(ictx, iface_desc)) {
2260 goto find_endpoint_failed;
2263 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2264 usb_rcvintpipe(ictx->usbdev_intf0,
2265 ictx->rx_endpoint_intf0->bEndpointAddress),
2266 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2267 usb_rx_callback_intf0, ictx,
2268 ictx->rx_endpoint_intf0->bInterval);
2270 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2272 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2273 goto urb_submit_failed;
2276 ictx->idev = imon_init_idev(ictx);
2278 dev_err(dev, "%s: input device setup failed\n", __func__);
2279 goto idev_setup_failed;
2282 ictx->rdev = imon_init_rdev(ictx);
2284 dev_err(dev, "%s: rc device setup failed\n", __func__);
2285 goto rdev_setup_failed;
2288 ictx->dev_present_intf0 = true;
2290 mutex_unlock(&ictx->lock);
2294 input_unregister_device(ictx->idev);
2296 usb_kill_urb(ictx->rx_urb_intf0);
2298 find_endpoint_failed:
2299 usb_put_dev(ictx->usbdev_intf0);
2300 mutex_unlock(&ictx->lock);
2301 usb_free_urb(tx_urb);
2302 tx_urb_alloc_failed:
2303 usb_free_urb(rx_urb);
2304 rx_urb_alloc_failed:
2307 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2312 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2313 struct imon_context *ictx)
2316 struct usb_host_interface *iface_desc;
2319 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2321 goto rx_urb_alloc_failed;
2323 mutex_lock(&ictx->lock);
2325 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2326 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
2329 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2330 ictx->rx_urb_intf1 = rx_urb;
2333 iface_desc = intf->cur_altsetting;
2334 if (!imon_find_endpoints(ictx, iface_desc))
2335 goto find_endpoint_failed;
2337 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2338 ictx->touch = imon_init_touch(ictx);
2340 goto touch_setup_failed;
2344 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2345 usb_rcvintpipe(ictx->usbdev_intf1,
2346 ictx->rx_endpoint_intf1->bEndpointAddress),
2347 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2348 usb_rx_callback_intf1, ictx,
2349 ictx->rx_endpoint_intf1->bInterval);
2351 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2354 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2355 goto urb_submit_failed;
2358 ictx->dev_present_intf1 = true;
2360 mutex_unlock(&ictx->lock);
2365 input_unregister_device(ictx->touch);
2367 find_endpoint_failed:
2368 usb_put_dev(ictx->usbdev_intf1);
2369 ictx->usbdev_intf1 = NULL;
2370 mutex_unlock(&ictx->lock);
2371 usb_free_urb(rx_urb);
2372 ictx->rx_urb_intf1 = NULL;
2373 rx_urb_alloc_failed:
2374 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
2379 static void imon_init_display(struct imon_context *ictx,
2380 struct usb_interface *intf)
2384 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2386 /* set up sysfs entry for built-in clock */
2387 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2389 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2392 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2393 ret = usb_register_dev(intf, &imon_lcd_class);
2395 ret = usb_register_dev(intf, &imon_vfd_class);
2397 /* Not a fatal error, so ignore */
2398 dev_info(ictx->dev, "could not get a minor number for display\n");
2403 * Callback function for USB core API: Probe
2405 static int imon_probe(struct usb_interface *interface,
2406 const struct usb_device_id *id)
2408 struct usb_device *usbdev = NULL;
2409 struct usb_host_interface *iface_desc = NULL;
2410 struct usb_interface *first_if;
2411 struct device *dev = &interface->dev;
2412 int ifnum, sysfs_err;
2414 struct imon_context *ictx = NULL;
2415 u16 vendor, product;
2417 usbdev = usb_get_dev(interface_to_usbdev(interface));
2418 iface_desc = interface->cur_altsetting;
2419 ifnum = iface_desc->desc.bInterfaceNumber;
2420 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2421 product = le16_to_cpu(usbdev->descriptor.idProduct);
2423 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2424 __func__, vendor, product, ifnum);
2426 first_if = usb_ifnum_to_if(usbdev, 0);
2433 ictx = imon_init_intf0(interface, id);
2435 pr_err("failed to initialize context!\n");
2439 refcount_set(&ictx->users, 1);
2442 /* this is the secondary interface on the device */
2443 struct imon_context *first_if_ctx = usb_get_intfdata(first_if);
2445 /* fail early if first intf failed to register */
2446 if (!first_if_ctx) {
2451 ictx = imon_init_intf1(interface, first_if_ctx);
2453 pr_err("failed to attach to context!\n");
2457 refcount_inc(&ictx->users);
2461 usb_set_intfdata(interface, ictx);
2464 if (product == 0xffdc && ictx->rf_device) {
2465 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2466 &imon_rf_attr_group);
2468 pr_err("Could not create RF sysfs entries(%d)\n",
2472 if (ictx->display_supported)
2473 imon_init_display(ictx, interface);
2476 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2477 vendor, product, ifnum,
2478 usbdev->bus->busnum, usbdev->devnum);
2480 usb_put_dev(usbdev);
2485 usb_put_dev(usbdev);
2486 dev_err(dev, "unable to register, err %d\n", ret);
2492 * Callback function for USB core API: disconnect
2494 static void imon_disconnect(struct usb_interface *interface)
2496 struct imon_context *ictx;
2500 ictx = usb_get_intfdata(interface);
2501 ictx->disconnected = true;
2503 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2506 * sysfs_remove_group is safe to call even if sysfs_create_group
2507 * hasn't been called
2509 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2510 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2512 usb_set_intfdata(interface, NULL);
2514 /* Abort ongoing write */
2515 if (ictx->tx.busy) {
2516 usb_kill_urb(ictx->tx_urb);
2517 complete(&ictx->tx.finished);
2521 ictx->dev_present_intf0 = false;
2522 usb_kill_urb(ictx->rx_urb_intf0);
2523 input_unregister_device(ictx->idev);
2524 rc_unregister_device(ictx->rdev);
2525 if (ictx->display_supported) {
2526 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2527 usb_deregister_dev(interface, &imon_lcd_class);
2528 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
2529 usb_deregister_dev(interface, &imon_vfd_class);
2531 usb_put_dev(ictx->usbdev_intf0);
2533 ictx->dev_present_intf1 = false;
2534 usb_kill_urb(ictx->rx_urb_intf1);
2535 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2536 del_timer_sync(&ictx->ttimer);
2537 input_unregister_device(ictx->touch);
2539 usb_put_dev(ictx->usbdev_intf1);
2542 if (refcount_dec_and_test(&ictx->users))
2543 free_imon_context(ictx);
2545 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2549 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2551 struct imon_context *ictx = usb_get_intfdata(intf);
2552 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2555 usb_kill_urb(ictx->rx_urb_intf0);
2557 usb_kill_urb(ictx->rx_urb_intf1);
2562 static int imon_resume(struct usb_interface *intf)
2565 struct imon_context *ictx = usb_get_intfdata(intf);
2566 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2569 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2570 usb_rcvintpipe(ictx->usbdev_intf0,
2571 ictx->rx_endpoint_intf0->bEndpointAddress),
2572 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2573 usb_rx_callback_intf0, ictx,
2574 ictx->rx_endpoint_intf0->bInterval);
2576 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_NOIO);
2579 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2580 usb_rcvintpipe(ictx->usbdev_intf1,
2581 ictx->rx_endpoint_intf1->bEndpointAddress),
2582 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2583 usb_rx_callback_intf1, ictx,
2584 ictx->rx_endpoint_intf1->bInterval);
2586 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_NOIO);
2592 module_usb_driver(imon_driver);