2 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
4 * Copyright(C) 2009 Jarod Wilson <jarod@wilsonet.com>
5 * Portions based on the original lirc_imon driver,
6 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
8 * Huge thanks to R. Geoff Newbury for invaluable debugging on the
9 * 0xffdc iMON devices, and for sending me one to hack on, without
10 * which the support for them wouldn't be nearly as good. Thanks
11 * also to the numerous 0xffdc device owners that tested auto-config
12 * support for me and provided debug dumps from their devices.
14 * imon is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
36 #include <linux/input.h>
37 #include <linux/usb.h>
38 #include <linux/usb/input.h>
39 #include <media/ir-core.h>
41 #include <linux/time.h>
42 #include <linux/timer.h>
44 #define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
45 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
46 #define MOD_NAME "imon"
47 #define MOD_VERSION "0.9.1"
49 #define DISPLAY_MINOR_BASE 144
50 #define DEVICE_NAME "lcd%d"
52 #define BUF_CHUNK_SIZE 8
55 #define BIT_DURATION 250 /* each bit received is 250us */
57 #define IMON_CLOCK_ENABLE_PACKETS 2
58 #define IMON_KEY_RELEASE_OFFSET 1000
60 /*** P R O T O T Y P E S ***/
62 /* USB Callback prototypes */
63 static int imon_probe(struct usb_interface *interface,
64 const struct usb_device_id *id);
65 static void imon_disconnect(struct usb_interface *interface);
66 static void usb_rx_callback_intf0(struct urb *urb);
67 static void usb_rx_callback_intf1(struct urb *urb);
68 static void usb_tx_callback(struct urb *urb);
70 /* suspend/resume support */
71 static int imon_resume(struct usb_interface *intf);
72 static int imon_suspend(struct usb_interface *intf, pm_message_t message);
74 /* Display file_operations function prototypes */
75 static int display_open(struct inode *inode, struct file *file);
76 static int display_close(struct inode *inode, struct file *file);
78 /* VFD write operation */
79 static ssize_t vfd_write(struct file *file, const char *buf,
80 size_t n_bytes, loff_t *pos);
82 /* LCD file_operations override function prototypes */
83 static ssize_t lcd_write(struct file *file, const char *buf,
84 size_t n_bytes, loff_t *pos);
86 /*** G L O B A L S ***/
90 struct ir_dev_props *props;
91 struct ir_input_dev *ir;
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_isassociating; /* RF remote associating */
99 bool dev_present_intf0; /* USB device presence, interface 0 */
100 bool dev_present_intf1; /* USB device presence, interface 1 */
102 struct mutex lock; /* to lock this object */
103 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
105 struct usb_endpoint_descriptor *rx_endpoint_intf0;
106 struct usb_endpoint_descriptor *rx_endpoint_intf1;
107 struct usb_endpoint_descriptor *tx_endpoint;
108 struct urb *rx_urb_intf0;
109 struct urb *rx_urb_intf1;
112 unsigned char usb_rx_buf[8];
113 unsigned char usb_tx_buf[8];
116 unsigned char data_buf[35]; /* user data buffer */
117 struct completion finished; /* wait for write to finish */
118 bool busy; /* write in progress */
119 int status; /* status of tx completion */
122 u16 vendor; /* usb vendor ID */
123 u16 product; /* usb product ID */
125 struct input_dev *idev; /* input device for remote */
126 struct input_dev *touch; /* input device for touchscreen */
128 u32 kc; /* current input keycode */
129 u32 last_keycode; /* last reported input keycode */
130 u64 ir_type; /* iMON or MCE (RC6) IR protocol? */
131 u8 mce_toggle_bit; /* last mce toggle bit */
132 bool release_code; /* some keys send a release code */
134 u8 display_type; /* store the display type */
135 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
137 char name_idev[128]; /* input device name */
138 char phys_idev[64]; /* input device phys path */
139 struct timer_list itimer; /* input device timer, need for rc6 */
141 char name_touch[128]; /* touch screen name */
142 char phys_touch[64]; /* touch screen phys path */
143 struct timer_list ttimer; /* touch screen timer */
144 int touch_x; /* x coordinate on touchscreen */
145 int touch_y; /* y coordinate on touchscreen */
148 #define TOUCH_TIMEOUT (HZ/30)
149 #define MCE_TIMEOUT_MS 200
151 /* vfd character device file operations */
152 static const struct file_operations vfd_fops = {
153 .owner = THIS_MODULE,
154 .open = &display_open,
156 .release = &display_close
159 /* lcd character device file operations */
160 static const struct file_operations lcd_fops = {
161 .owner = THIS_MODULE,
162 .open = &display_open,
164 .release = &display_close
168 IMON_DISPLAY_TYPE_AUTO = 0,
169 IMON_DISPLAY_TYPE_VFD = 1,
170 IMON_DISPLAY_TYPE_LCD = 2,
171 IMON_DISPLAY_TYPE_VGA = 3,
172 IMON_DISPLAY_TYPE_NONE = 4,
182 * USB Device ID for iMON USB Control Boards
184 * The Windows drivers contain 6 different inf files, more or less one for
185 * each new device until the 0x0034-0x0046 devices, which all use the same
186 * driver. Some of the devices in the 34-46 range haven't been definitively
187 * identified yet. Early devices have either a TriGem Computer, Inc. or a
188 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
189 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
190 * the ffdc and later devices, which do onboard decoding.
192 static struct usb_device_id imon_usb_id_table[] = {
194 * Several devices with this same device ID, all use iMON_PAD.inf
195 * SoundGraph iMON PAD (IR & VFD)
196 * SoundGraph iMON PAD (IR & LCD)
197 * SoundGraph iMON Knob (IR only)
199 { USB_DEVICE(0x15c2, 0xffdc) },
202 * Newer devices, all driven by the latest iMON Windows driver, full
203 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
204 * Need user input to fill in details on unknown devices.
206 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
207 { USB_DEVICE(0x15c2, 0x0034) },
208 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
209 { USB_DEVICE(0x15c2, 0x0035) },
210 /* SoundGraph iMON OEM VFD (IR & VFD) */
211 { USB_DEVICE(0x15c2, 0x0036) },
212 /* device specifics unknown */
213 { USB_DEVICE(0x15c2, 0x0037) },
214 /* SoundGraph iMON OEM LCD (IR & LCD) */
215 { USB_DEVICE(0x15c2, 0x0038) },
216 /* SoundGraph iMON UltraBay (IR & LCD) */
217 { USB_DEVICE(0x15c2, 0x0039) },
218 /* device specifics unknown */
219 { USB_DEVICE(0x15c2, 0x003a) },
220 /* device specifics unknown */
221 { USB_DEVICE(0x15c2, 0x003b) },
222 /* SoundGraph iMON OEM Inside (IR only) */
223 { USB_DEVICE(0x15c2, 0x003c) },
224 /* device specifics unknown */
225 { USB_DEVICE(0x15c2, 0x003d) },
226 /* device specifics unknown */
227 { USB_DEVICE(0x15c2, 0x003e) },
228 /* device specifics unknown */
229 { USB_DEVICE(0x15c2, 0x003f) },
230 /* device specifics unknown */
231 { USB_DEVICE(0x15c2, 0x0040) },
232 /* SoundGraph iMON MINI (IR only) */
233 { USB_DEVICE(0x15c2, 0x0041) },
234 /* Antec Veris Multimedia Station EZ External (IR only) */
235 { USB_DEVICE(0x15c2, 0x0042) },
236 /* Antec Veris Multimedia Station Basic Internal (IR only) */
237 { USB_DEVICE(0x15c2, 0x0043) },
238 /* Antec Veris Multimedia Station Elite (IR & VFD) */
239 { USB_DEVICE(0x15c2, 0x0044) },
240 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
241 { USB_DEVICE(0x15c2, 0x0045) },
242 /* device specifics unknown */
243 { USB_DEVICE(0x15c2, 0x0046) },
247 /* USB Device data */
248 static struct usb_driver imon_driver = {
251 .disconnect = imon_disconnect,
252 .suspend = imon_suspend,
253 .resume = imon_resume,
254 .id_table = imon_usb_id_table,
257 static struct usb_class_driver imon_vfd_class = {
260 .minor_base = DISPLAY_MINOR_BASE,
263 static struct usb_class_driver imon_lcd_class = {
266 .minor_base = DISPLAY_MINOR_BASE,
269 /* imon receiver front panel/knob key table */
270 static const struct {
273 } imon_panel_key_table[] = {
274 { 0x000000000f00ffeell, KEY_PROG1 }, /* Go */
275 { 0x000000001f00ffeell, KEY_AUDIO },
276 { 0x000000002000ffeell, KEY_VIDEO },
277 { 0x000000002100ffeell, KEY_CAMERA },
278 { 0x000000002700ffeell, KEY_DVD },
279 { 0x000000002300ffeell, KEY_TV },
280 { 0x000000000500ffeell, KEY_PREVIOUS },
281 { 0x000000000700ffeell, KEY_REWIND },
282 { 0x000000000400ffeell, KEY_STOP },
283 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
284 { 0x000000000800ffeell, KEY_FASTFORWARD },
285 { 0x000000000600ffeell, KEY_NEXT },
286 { 0x000000010000ffeell, KEY_RIGHT },
287 { 0x000001000000ffeell, KEY_LEFT },
288 { 0x000000003d00ffeell, KEY_SELECT },
289 { 0x000100000000ffeell, KEY_VOLUMEUP },
290 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
291 { 0x000000000100ffeell, KEY_MUTE },
292 /* iMON Knob values */
293 { 0x000100ffffffffeell, KEY_VOLUMEUP },
294 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
295 { 0x000008ffffffffeell, KEY_MUTE },
298 /* to prevent races between open() and disconnect(), probing, etc */
299 static DEFINE_MUTEX(driver_lock);
301 /* Module bookkeeping bits */
302 MODULE_AUTHOR(MOD_AUTHOR);
303 MODULE_DESCRIPTION(MOD_DESC);
304 MODULE_VERSION(MOD_VERSION);
305 MODULE_LICENSE("GPL");
306 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
309 module_param(debug, bool, S_IRUGO | S_IWUSR);
310 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes(default: no)");
312 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
313 static int display_type;
314 module_param(display_type, int, S_IRUGO);
315 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
316 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
318 static int pad_stabilize = 1;
319 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
320 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
321 "presses in arrow key mode. 0=disable, 1=enable (default).");
324 * In certain use cases, mouse mode isn't really helpful, and could actually
325 * cause confusion, so allow disabling it when the IR device is open.
328 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
329 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
330 "open. 0=don't disable, 1=disable. (default: don't disable)");
332 /* threshold at which a pad push registers as an arrow key in kbd mode */
333 static int pad_thresh;
334 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
335 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
336 "arrow key in kbd mode (default: 28)");
339 static void free_imon_context(struct imon_context *ictx)
341 struct device *dev = ictx->dev;
343 usb_free_urb(ictx->tx_urb);
344 usb_free_urb(ictx->rx_urb_intf0);
345 usb_free_urb(ictx->rx_urb_intf1);
348 dev_dbg(dev, "%s: iMON context freed\n", __func__);
352 * Called when the Display device (e.g. /dev/lcd0)
353 * is opened by the application.
355 static int display_open(struct inode *inode, struct file *file)
357 struct usb_interface *interface;
358 struct imon_context *ictx = NULL;
362 /* prevent races with disconnect */
363 mutex_lock(&driver_lock);
365 subminor = iminor(inode);
366 interface = usb_find_interface(&imon_driver, subminor);
368 err("%s: could not find interface for minor %d",
373 ictx = usb_get_intfdata(interface);
376 err("%s: no context found for minor %d", __func__, subminor);
381 mutex_lock(&ictx->lock);
383 if (!ictx->display_supported) {
384 err("%s: display not supported by device", __func__);
386 } else if (ictx->display_isopen) {
387 err("%s: display port is already open", __func__);
390 ictx->display_isopen = 1;
391 file->private_data = ictx;
392 dev_dbg(ictx->dev, "display port opened\n");
395 mutex_unlock(&ictx->lock);
398 mutex_unlock(&driver_lock);
403 * Called when the display device (e.g. /dev/lcd0)
404 * is closed by the application.
406 static int display_close(struct inode *inode, struct file *file)
408 struct imon_context *ictx = NULL;
411 ictx = (struct imon_context *)file->private_data;
414 err("%s: no context for device", __func__);
418 mutex_lock(&ictx->lock);
420 if (!ictx->display_supported) {
421 err("%s: display not supported by device", __func__);
423 } else if (!ictx->display_isopen) {
424 err("%s: display is not open", __func__);
427 ictx->display_isopen = 0;
428 dev_dbg(ictx->dev, "display port closed\n");
429 if (!ictx->dev_present_intf0) {
431 * Device disconnected before close and IR port is not
432 * open. If IR port is open, context will be deleted by
435 mutex_unlock(&ictx->lock);
436 free_imon_context(ictx);
441 mutex_unlock(&ictx->lock);
446 * Sends a packet to the device -- this function must be called
447 * with ictx->lock held.
449 static int send_packet(struct imon_context *ictx)
452 unsigned long timeout;
455 struct usb_ctrlrequest *control_req = NULL;
457 /* Check if we need to use control or interrupt urb */
458 if (!ictx->tx_control) {
459 pipe = usb_sndintpipe(ictx->usbdev_intf0,
460 ictx->tx_endpoint->bEndpointAddress);
461 interval = ictx->tx_endpoint->bInterval;
463 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
465 sizeof(ictx->usb_tx_buf),
466 usb_tx_callback, ictx, interval);
468 ictx->tx_urb->actual_length = 0;
470 /* fill request into kmalloc'ed space: */
471 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
473 if (control_req == NULL)
476 /* setup packet is '21 09 0200 0001 0008' */
477 control_req->bRequestType = 0x21;
478 control_req->bRequest = 0x09;
479 control_req->wValue = cpu_to_le16(0x0200);
480 control_req->wIndex = cpu_to_le16(0x0001);
481 control_req->wLength = cpu_to_le16(0x0008);
483 /* control pipe is endpoint 0x00 */
484 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
486 /* build the control urb */
487 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
488 pipe, (unsigned char *)control_req,
490 sizeof(ictx->usb_tx_buf),
491 usb_tx_callback, ictx);
492 ictx->tx_urb->actual_length = 0;
495 init_completion(&ictx->tx.finished);
497 smp_rmb(); /* ensure later readers know we're busy */
499 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
502 smp_rmb(); /* ensure later readers know we're not busy */
503 err("%s: error submitting urb(%d)", __func__, retval);
505 /* Wait for transmission to complete (or abort) */
506 mutex_unlock(&ictx->lock);
507 retval = wait_for_completion_interruptible(
510 err("%s: task interrupted", __func__);
511 mutex_lock(&ictx->lock);
513 retval = ictx->tx.status;
515 err("%s: packet tx failed (%d)", __func__, retval);
521 * Induce a mandatory 5ms delay before returning, as otherwise,
522 * send_packet can get called so rapidly as to overwhelm the device,
523 * particularly on faster systems and/or those with quirky usb.
525 timeout = msecs_to_jiffies(5);
526 set_current_state(TASK_UNINTERRUPTIBLE);
527 schedule_timeout(timeout);
533 * Sends an associate packet to the iMON 2.4G.
535 * This might not be such a good idea, since it has an id collision with
536 * some versions of the "IR & VFD" combo. The only way to determine if it
537 * is an RF version is to look at the product description string. (Which
538 * we currently do not fetch).
540 static int send_associate_24g(struct imon_context *ictx)
543 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
544 0x00, 0x00, 0x00, 0x20 };
547 err("%s: no context for device", __func__);
551 if (!ictx->dev_present_intf0) {
552 err("%s: no iMON device present", __func__);
556 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
557 retval = send_packet(ictx);
563 * Sends packets to setup and show clock on iMON display
565 * Arguments: year - last 2 digits of year, month - 1..12,
566 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
567 * hour - 0..23, minute - 0..59, second - 0..59
569 static int send_set_imon_clock(struct imon_context *ictx,
570 unsigned int year, unsigned int month,
571 unsigned int day, unsigned int dow,
572 unsigned int hour, unsigned int minute,
575 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
580 err("%s: no context for device", __func__);
584 switch (ictx->display_type) {
585 case IMON_DISPLAY_TYPE_LCD:
586 clock_enable_pkt[0][0] = 0x80;
587 clock_enable_pkt[0][1] = year;
588 clock_enable_pkt[0][2] = month-1;
589 clock_enable_pkt[0][3] = day;
590 clock_enable_pkt[0][4] = hour;
591 clock_enable_pkt[0][5] = minute;
592 clock_enable_pkt[0][6] = second;
594 clock_enable_pkt[1][0] = 0x80;
595 clock_enable_pkt[1][1] = 0;
596 clock_enable_pkt[1][2] = 0;
597 clock_enable_pkt[1][3] = 0;
598 clock_enable_pkt[1][4] = 0;
599 clock_enable_pkt[1][5] = 0;
600 clock_enable_pkt[1][6] = 0;
602 if (ictx->product == 0xffdc) {
603 clock_enable_pkt[0][7] = 0x50;
604 clock_enable_pkt[1][7] = 0x51;
606 clock_enable_pkt[0][7] = 0x88;
607 clock_enable_pkt[1][7] = 0x8a;
612 case IMON_DISPLAY_TYPE_VFD:
613 clock_enable_pkt[0][0] = year;
614 clock_enable_pkt[0][1] = month-1;
615 clock_enable_pkt[0][2] = day;
616 clock_enable_pkt[0][3] = dow;
617 clock_enable_pkt[0][4] = hour;
618 clock_enable_pkt[0][5] = minute;
619 clock_enable_pkt[0][6] = second;
620 clock_enable_pkt[0][7] = 0x40;
622 clock_enable_pkt[1][0] = 0;
623 clock_enable_pkt[1][1] = 0;
624 clock_enable_pkt[1][2] = 1;
625 clock_enable_pkt[1][3] = 0;
626 clock_enable_pkt[1][4] = 0;
627 clock_enable_pkt[1][5] = 0;
628 clock_enable_pkt[1][6] = 0;
629 clock_enable_pkt[1][7] = 0x42;
637 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
638 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
639 retval = send_packet(ictx);
641 err("%s: send_packet failed for packet %d",
651 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
653 static ssize_t show_associate_remote(struct device *d,
654 struct device_attribute *attr,
657 struct imon_context *ictx = dev_get_drvdata(d);
662 mutex_lock(&ictx->lock);
663 if (ictx->rf_isassociating)
664 strcpy(buf, "associating\n");
666 strcpy(buf, "closed\n");
668 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
669 "instructions on how to associate your iMON 2.4G DT/LT "
671 mutex_unlock(&ictx->lock);
675 static ssize_t store_associate_remote(struct device *d,
676 struct device_attribute *attr,
677 const char *buf, size_t count)
679 struct imon_context *ictx;
681 ictx = dev_get_drvdata(d);
686 mutex_lock(&ictx->lock);
687 ictx->rf_isassociating = 1;
688 send_associate_24g(ictx);
689 mutex_unlock(&ictx->lock);
695 * sysfs functions to control internal imon clock
697 static ssize_t show_imon_clock(struct device *d,
698 struct device_attribute *attr, char *buf)
700 struct imon_context *ictx = dev_get_drvdata(d);
706 mutex_lock(&ictx->lock);
708 if (!ictx->display_supported) {
709 len = snprintf(buf, PAGE_SIZE, "Not supported.");
711 len = snprintf(buf, PAGE_SIZE,
712 "To set the clock on your iMON display:\n"
713 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
714 "%s", ictx->display_isopen ?
715 "\nNOTE: imon device must be closed\n" : "");
718 mutex_unlock(&ictx->lock);
723 static ssize_t store_imon_clock(struct device *d,
724 struct device_attribute *attr,
725 const char *buf, size_t count)
727 struct imon_context *ictx = dev_get_drvdata(d);
729 unsigned int year, month, day, dow, hour, minute, second;
734 mutex_lock(&ictx->lock);
736 if (!ictx->display_supported) {
739 } else if (ictx->display_isopen) {
744 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
745 &hour, &minute, &second) != 7) {
750 if ((month < 1 || month > 12) ||
751 (day < 1 || day > 31) || (dow > 6) ||
752 (hour > 23) || (minute > 59) || (second > 59)) {
757 retval = send_set_imon_clock(ictx, year, month, day, dow,
758 hour, minute, second);
764 mutex_unlock(&ictx->lock);
770 static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
773 static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
774 store_associate_remote);
776 static struct attribute *imon_display_sysfs_entries[] = {
777 &dev_attr_imon_clock.attr,
781 static struct attribute_group imon_display_attribute_group = {
782 .attrs = imon_display_sysfs_entries
785 static struct attribute *imon_rf_sysfs_entries[] = {
786 &dev_attr_associate_remote.attr,
790 static struct attribute_group imon_rf_attribute_group = {
791 .attrs = imon_rf_sysfs_entries
795 * Writes data to the VFD. The iMON VFD is 2x16 characters
796 * and requires data in 5 consecutive USB interrupt packets,
797 * each packet but the last carrying 7 bytes.
799 * I don't know if the VFD board supports features such as
800 * scrolling, clearing rows, blanking, etc. so at
801 * the caller must provide a full screen of data. If fewer
802 * than 32 bytes are provided spaces will be appended to
803 * generate a full screen.
805 static ssize_t vfd_write(struct file *file, const char *buf,
806 size_t n_bytes, loff_t *pos)
812 struct imon_context *ictx;
813 const unsigned char vfd_packet6[] = {
814 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
816 ictx = (struct imon_context *)file->private_data;
818 err("%s: no context for device", __func__);
822 mutex_lock(&ictx->lock);
824 if (!ictx->dev_present_intf0) {
825 err("%s: no iMON device present", __func__);
830 if (n_bytes <= 0 || n_bytes > 32) {
831 err("%s: invalid payload size", __func__);
836 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
841 /* Pad with spaces */
842 for (i = n_bytes; i < 32; ++i)
843 ictx->tx.data_buf[i] = ' ';
845 for (i = 32; i < 35; ++i)
846 ictx->tx.data_buf[i] = 0xFF;
852 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
853 ictx->usb_tx_buf[7] = (unsigned char) seq;
855 retval = send_packet(ictx);
857 err("%s: send packet failed for packet #%d",
865 } while (offset < 35);
868 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
869 ictx->usb_tx_buf[7] = (unsigned char) seq;
870 retval = send_packet(ictx);
872 err("%s: send packet failed for packet #%d",
876 mutex_unlock(&ictx->lock);
878 return (!retval) ? n_bytes : retval;
882 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
883 * packets. We accept data as 16 hexadecimal digits, followed by a
884 * newline (to make it easy to drive the device from a command-line
885 * -- even though the actual binary data is a bit complicated).
887 * The device itself is not a "traditional" text-mode display. It's
888 * actually a 16x96 pixel bitmap display. That means if you want to
889 * display text, you've got to have your own "font" and translate the
890 * text into bitmaps for display. This is really flexible (you can
891 * display whatever diacritics you need, and so on), but it's also
892 * a lot more complicated than most LCDs...
894 static ssize_t lcd_write(struct file *file, const char *buf,
895 size_t n_bytes, loff_t *pos)
898 struct imon_context *ictx;
900 ictx = (struct imon_context *)file->private_data;
902 err("%s: no context for device", __func__);
906 mutex_lock(&ictx->lock);
908 if (!ictx->display_supported) {
909 err("%s: no iMON display present", __func__);
915 err("%s: invalid payload size: %d (expecting 8)",
916 __func__, (int) n_bytes);
921 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
926 retval = send_packet(ictx);
928 err("%s: send packet failed!", __func__);
931 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
932 __func__, (int) n_bytes);
935 mutex_unlock(&ictx->lock);
936 return (!retval) ? n_bytes : retval;
940 * Callback function for USB core API: transmit data
942 static void usb_tx_callback(struct urb *urb)
944 struct imon_context *ictx;
948 ictx = (struct imon_context *)urb->context;
952 ictx->tx.status = urb->status;
954 /* notify waiters that write has finished */
956 smp_rmb(); /* ensure later readers know we're not busy */
957 complete(&ictx->tx.finished);
961 * mce/rc6 keypresses have no distinct release code, use timer
963 static void imon_mce_timeout(unsigned long data)
965 struct imon_context *ictx = (struct imon_context *)data;
967 input_report_key(ictx->idev, ictx->last_keycode, 0);
968 input_sync(ictx->idev);
972 * report touchscreen input
974 static void imon_touch_display_timeout(unsigned long data)
976 struct imon_context *ictx = (struct imon_context *)data;
978 if (!ictx->display_type == IMON_DISPLAY_TYPE_VGA)
981 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
982 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
983 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
984 input_sync(ictx->touch);
988 * iMON IR receivers support two different signal sets -- those used by
989 * the iMON remotes, and those used by the Windows MCE remotes (which is
990 * really just RC-6), but only one or the other at a time, as the signals
991 * are decoded onboard the receiver.
993 int imon_ir_change_protocol(void *priv, u64 ir_type)
996 struct imon_context *ictx = priv;
997 struct device *dev = ictx->dev;
999 unsigned char ir_proto_packet[] = {
1000 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1002 if (!(ir_type & ictx->props->allowed_protos))
1003 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1004 "this device does not support\n");
1008 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1009 ir_proto_packet[0] = 0x01;
1011 init_timer(&ictx->itimer);
1012 ictx->itimer.data = (unsigned long)ictx;
1013 ictx->itimer.function = imon_mce_timeout;
1015 case IR_TYPE_UNKNOWN:
1017 dev_dbg(dev, "Configuring IR receiver for iMON protocol");
1018 if (pad_stabilize) {
1019 printk(KERN_CONT "\n");
1022 printk(KERN_CONT " (without PAD stabilization)\n");
1025 /* ir_proto_packet[0] = 0x00; // already the default */
1026 ir_type = IR_TYPE_OTHER;
1029 dev_warn(dev, "Unsupported IR protocol specified, overriding "
1030 "to iMON IR protocol");
1031 if (pad_stabilize) {
1032 printk(KERN_CONT "\n");
1035 printk(KERN_CONT " (without PAD stabilization)\n");
1038 /* ir_proto_packet[0] = 0x00; // already the default */
1039 ir_type = IR_TYPE_OTHER;
1043 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1045 retval = send_packet(ictx);
1049 ictx->ir_type = ir_type;
1050 ictx->pad_mouse = pad_mouse;
1056 static inline int tv2int(const struct timeval *a, const struct timeval *b)
1061 if (b->tv_usec > a->tv_usec) {
1066 usecs += a->tv_usec - b->tv_usec;
1068 sec += a->tv_sec - b->tv_sec;
1080 * The directional pad behaves a bit differently, depending on whether this is
1081 * one of the older ffdc devices or a newer device. Newer devices appear to
1082 * have a higher resolution matrix for more precise mouse movement, but it
1083 * makes things overly sensitive in keyboard mode, so we do some interesting
1084 * contortions to make it less touchy. Older devices run through the same
1085 * routine with shorter timeout and a smaller threshold.
1087 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1090 static struct timeval prev_time = {0, 0};
1091 static struct timeval hit_time = {0, 0};
1092 static int x, y, prev_result, hits;
1096 do_gettimeofday(&ct);
1097 msec = tv2int(&ct, &prev_time);
1098 msec_hit = tv2int(&ct, &hit_time);
1111 if (abs(x) > threshold || abs(y) > threshold) {
1112 if (abs(y) > abs(x))
1113 result = (y > 0) ? 0x7F : 0x80;
1115 result = (x > 0) ? 0x7F00 : 0x8000;
1120 if (result == prev_result) {
1126 y = 17 * threshold / 30;
1129 y -= 17 * threshold / 30;
1132 x = 17 * threshold / 30;
1135 x -= 17 * threshold / 30;
1140 if (hits == 2 && msec_hit < timeout) {
1145 prev_result = result;
1154 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 hw_code)
1156 u32 scancode = be32_to_cpu(hw_code);
1159 bool is_release_code = false;
1161 /* Look for the initial press of a button */
1162 keycode = ir_g_keycode_from_table(ictx->idev, scancode);
1164 /* Look for the release of a button */
1165 if (keycode == KEY_RESERVED) {
1166 release = scancode & ~0x4000;
1167 keycode = ir_g_keycode_from_table(ictx->idev, release);
1168 if (keycode != KEY_RESERVED)
1169 is_release_code = true;
1172 ictx->release_code = is_release_code;
1177 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 hw_code)
1179 u32 scancode = be32_to_cpu(hw_code);
1182 #define MCE_KEY_MASK 0x7000
1183 #define MCE_TOGGLE_BIT 0x8000
1186 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1187 * (the toggle bit flipping between alternating key presses), while
1188 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1189 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1190 * but we can't or them into all codes, as some keys are decoded in
1191 * a different way w/o the same use of the toggle bit...
1193 if ((scancode >> 24) & 0x80)
1194 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1196 keycode = ir_g_keycode_from_table(ictx->idev, scancode);
1201 static u32 imon_panel_key_lookup(u64 hw_code)
1204 u64 code = be64_to_cpu(hw_code);
1207 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++)
1208 if (imon_panel_key_table[i].hw_code == (code | 0xffee))
1211 keycode = imon_panel_key_table[i % IMON_KEY_RELEASE_OFFSET].keycode;
1216 static bool imon_mouse_event(struct imon_context *ictx,
1217 unsigned char *buf, int len)
1219 char rel_x = 0x00, rel_y = 0x00;
1221 bool mouse_input = 1;
1224 /* newer iMON device PAD or mouse button */
1225 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1229 /* 0xffdc iMON PAD or mouse button input */
1230 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1231 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1232 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1233 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1236 rel_x = rel_x + rel_x / 2;
1237 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1238 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1241 rel_y = rel_y + rel_y / 2;
1243 /* some ffdc devices decode mouse buttons differently... */
1244 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1246 /* ch+/- buttons, which we use for an emulated scroll wheel */
1247 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1249 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1255 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1258 input_report_rel(ictx->idev, REL_WHEEL, dir);
1259 } else if (rel_x || rel_y) {
1260 input_report_rel(ictx->idev, REL_X, rel_x);
1261 input_report_rel(ictx->idev, REL_Y, rel_y);
1263 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1264 input_report_key(ictx->idev, BTN_RIGHT,
1265 buf[1] >> right_shift & 0x1);
1267 input_sync(ictx->idev);
1268 ictx->last_keycode = ictx->kc;
1274 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1276 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1277 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1278 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1279 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1280 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1281 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1282 input_sync(ictx->touch);
1285 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1288 char rel_x = 0x00, rel_y = 0x00;
1289 u16 timeout, threshold;
1294 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1295 * contain a position coordinate (x,y), with each component ranging
1296 * from -14 to 14. We want to down-sample this to only 4 discrete values
1297 * for up/down/left/right arrow keys. Also, when you get too close to
1298 * diagonals, it has a tendancy to jump back and forth, so lets try to
1299 * ignore when they get too close.
1301 if (ictx->product != 0xffdc) {
1302 /* first, pad to 8 bytes so it conforms with everything else */
1303 buf[5] = buf[6] = buf[7] = 0;
1304 timeout = 500; /* in msecs */
1305 /* (2*threshold) x (2*threshold) square */
1306 threshold = pad_thresh ? pad_thresh : 28;
1310 if (ictx->ir_type == IR_TYPE_OTHER && pad_stabilize) {
1311 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1312 dir = stabilize((int)rel_x, (int)rel_y,
1313 timeout, threshold);
1315 ictx->kc = KEY_UNKNOWN;
1318 buf[2] = dir & 0xFF;
1319 buf[3] = (dir >> 8) & 0xFF;
1320 memcpy(&temp_key, buf, sizeof(temp_key));
1321 remote_key = (u32) (le64_to_cpu(temp_key)
1323 ictx->kc = imon_remote_key_lookup(ictx,
1327 if (abs(rel_y) > abs(rel_x)) {
1328 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1330 ictx->kc = (rel_y > 0) ? KEY_DOWN : KEY_UP;
1333 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1334 ictx->kc = (rel_x > 0) ? KEY_RIGHT : KEY_LEFT;
1339 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1340 * device (15c2:ffdc). The remote generates various codes from
1341 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1342 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1343 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1344 * reversed endianess. Extract direction from buffer, rotate endianess,
1345 * adjust sign and feed the values into stabilize(). The resulting codes
1346 * will be 0x01008000, 0x01007F00, which match the newer devices.
1349 timeout = 10; /* in msecs */
1350 /* (2*threshold) x (2*threshold) square */
1351 threshold = pad_thresh ? pad_thresh : 15;
1354 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1355 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1359 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1360 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1365 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1367 if (ictx->ir_type == IR_TYPE_OTHER && pad_stabilize) {
1368 dir = stabilize((int)rel_x, (int)rel_y,
1369 timeout, threshold);
1371 ictx->kc = KEY_UNKNOWN;
1374 buf[2] = dir & 0xFF;
1375 buf[3] = (dir >> 8) & 0xFF;
1376 memcpy(&temp_key, buf, sizeof(temp_key));
1377 remote_key = (u32) (le64_to_cpu(temp_key) & 0xffffffff);
1378 ictx->kc = imon_remote_key_lookup(ictx, remote_key);
1380 if (abs(rel_y) > abs(rel_x)) {
1381 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1383 ictx->kc = (rel_y > 0) ? KEY_DOWN : KEY_UP;
1386 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1387 ictx->kc = (rel_x > 0) ? KEY_RIGHT : KEY_LEFT;
1393 static int imon_parse_press_type(struct imon_context *ictx,
1394 unsigned char *buf, u8 ktype)
1398 /* key release of 0x02XXXXXX key */
1399 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1400 ictx->kc = ictx->last_keycode;
1402 /* mouse button release on (some) 0xffdc devices */
1403 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1404 buf[2] == 0x81 && buf[3] == 0xb7)
1405 ictx->kc = ictx->last_keycode;
1407 /* mouse button release on (some other) 0xffdc devices */
1408 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1409 buf[2] == 0x81 && buf[3] == 0xb7)
1410 ictx->kc = ictx->last_keycode;
1412 /* mce-specific button handling */
1413 else if (ktype == IMON_KEY_MCE) {
1415 if (ictx->kc != ictx->last_keycode
1416 || buf[2] != ictx->mce_toggle_bit) {
1417 ictx->last_keycode = ictx->kc;
1418 ictx->mce_toggle_bit = buf[2];
1420 mod_timer(&ictx->itimer,
1421 jiffies + msecs_to_jiffies(MCE_TIMEOUT_MS));
1425 mod_timer(&ictx->itimer,
1426 jiffies + msecs_to_jiffies(MCE_TIMEOUT_MS));
1429 /* incoherent or irrelevant data */
1430 } else if (ictx->kc == KEY_RESERVED)
1431 press_type = -EINVAL;
1433 /* key release of 0xXXXXXXb7 key */
1434 else if (ictx->release_code)
1437 /* this is a button press */
1445 * Process the incoming packet
1447 static void imon_incoming_packet(struct imon_context *ictx,
1448 struct urb *urb, int intf)
1450 int len = urb->actual_length;
1451 unsigned char *buf = urb->transfer_buffer;
1452 struct device *dev = ictx->dev;
1459 struct input_dev *idev = NULL;
1463 static struct timeval prev_time = { 0, 0 };
1464 u8 ktype = IMON_KEY_IMON;
1468 /* filter out junk data on the older 0xffdc imon devices */
1469 if ((buf[0] == 0xff) && (buf[7] == 0xff))
1472 /* Figure out what key was pressed */
1473 memcpy(&temp_key, buf, sizeof(temp_key));
1474 if (len == 8 && buf[7] == 0xee) {
1475 ktype = IMON_KEY_PANEL;
1476 panel_key = le64_to_cpu(temp_key);
1477 kc = imon_panel_key_lookup(panel_key);
1479 remote_key = (u32) (le64_to_cpu(temp_key) & 0xffffffff);
1480 if (ictx->ir_type == IR_TYPE_RC6) {
1482 ktype = IMON_KEY_MCE;
1483 kc = imon_mce_key_lookup(ictx, remote_key);
1485 kc = imon_remote_key_lookup(ictx, remote_key);
1488 /* keyboard/mouse mode toggle button */
1489 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1490 ictx->last_keycode = kc;
1492 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1493 dev_dbg(dev, "toggling to %s mode\n",
1494 ictx->pad_mouse ? "mouse" : "keyboard");
1497 ictx->pad_mouse = 0;
1498 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1504 /* send touchscreen events through input subsystem if touchpad data */
1505 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1507 imon_touch_event(ictx, buf);
1509 /* look for mouse events with pad in mouse mode */
1510 } else if (ictx->pad_mouse) {
1511 if (imon_mouse_event(ictx, buf, len))
1515 /* Now for some special handling to convert pad input to arrow keys */
1516 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1517 ((len == 8) && (buf[0] & 0x40) &&
1518 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1520 imon_pad_to_keys(ictx, buf);
1525 printk(KERN_INFO "intf%d decoded packet: ", intf);
1526 for (i = 0; i < len; ++i)
1527 printk("%02x ", buf[i]);
1531 press_type = imon_parse_press_type(ictx, buf, ktype);
1533 goto not_input_data;
1535 if (ictx->kc == KEY_UNKNOWN)
1538 /* KEY_MUTE repeats from MCE and knob need to be suppressed */
1539 if ((ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode)
1540 && (buf[7] == 0xee || ktype == IMON_KEY_MCE)) {
1541 do_gettimeofday(&t);
1542 msec = tv2int(&t, &prev_time);
1548 input_report_key(idev, ictx->kc, press_type);
1551 /* panel keys and some remote keys don't generate a release */
1552 if (panel_key || norelease) {
1553 input_report_key(idev, ictx->kc, 0);
1557 ictx->last_keycode = ictx->kc;
1562 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
1563 (panel_key ? be64_to_cpu(panel_key) :
1564 be32_to_cpu(remote_key)));
1569 dev_warn(dev, "imon %s: invalid incoming packet "
1570 "size (len = %d, intf%d)\n", __func__, len, intf);
1574 /* iMON 2.4G associate frame */
1575 if (buf[0] == 0x00 &&
1576 buf[2] == 0xFF && /* REFID */
1579 buf[5] == 0xFF && /* iMON 2.4G */
1580 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1581 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1582 dev_warn(dev, "%s: remote associated refid=%02X\n",
1584 ictx->rf_isassociating = 0;
1589 * Callback function for USB core API: receive data
1591 static void usb_rx_callback_intf0(struct urb *urb)
1593 struct imon_context *ictx;
1599 ictx = (struct imon_context *)urb->context;
1603 switch (urb->status) {
1604 case -ENOENT: /* usbcore unlink successful! */
1607 case -ESHUTDOWN: /* transport endpoint was shut down */
1611 imon_incoming_packet(ictx, urb, intfnum);
1615 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1616 __func__, urb->status);
1620 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1623 static void usb_rx_callback_intf1(struct urb *urb)
1625 struct imon_context *ictx;
1631 ictx = (struct imon_context *)urb->context;
1635 switch (urb->status) {
1636 case -ENOENT: /* usbcore unlink successful! */
1639 case -ESHUTDOWN: /* transport endpoint was shut down */
1643 imon_incoming_packet(ictx, urb, intfnum);
1647 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1648 __func__, urb->status);
1652 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1655 static struct input_dev *imon_init_idev(struct imon_context *ictx)
1657 struct input_dev *idev;
1658 struct ir_dev_props *props;
1659 struct ir_input_dev *ir;
1662 idev = input_allocate_device();
1664 dev_err(ictx->dev, "remote input dev allocation failed\n");
1665 goto idev_alloc_failed;
1668 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
1670 dev_err(ictx->dev, "remote ir dev props allocation failed\n");
1671 goto props_alloc_failed;
1674 ir = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
1676 dev_err(ictx->dev, "remote ir input dev allocation failed\n");
1677 goto ir_dev_alloc_failed;
1680 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1681 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1682 idev->name = ictx->name_idev;
1684 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1685 sizeof(ictx->phys_idev));
1686 strlcat(ictx->phys_idev, "/input0", sizeof(ictx->phys_idev));
1687 idev->phys = ictx->phys_idev;
1689 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1691 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1692 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1693 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1694 BIT_MASK(REL_WHEEL);
1696 /* panel and/or knob code support */
1697 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1698 u32 kc = imon_panel_key_table[i].keycode;
1699 __set_bit(kc, idev->keybit);
1703 props->driver_type = RC_DRIVER_SCANCODE;
1704 /* IR_TYPE_OTHER maps to iMON PAD remote, IR_TYPE_RC6 to MCE remote */
1705 props->allowed_protos = IR_TYPE_OTHER | IR_TYPE_RC6;
1706 props->change_protocol = imon_ir_change_protocol;
1707 ictx->props = props;
1710 memcpy(&ir->dev, ictx->dev, sizeof(struct device));
1712 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1713 idev->dev.parent = ictx->dev;
1715 input_set_drvdata(idev, ir);
1717 ret = ir_input_register(idev, RC_MAP_IMON_PAD, props, MOD_NAME);
1719 dev_err(ictx->dev, "remote input dev register failed\n");
1720 goto idev_register_failed;
1725 idev_register_failed:
1727 ir_dev_alloc_failed:
1730 input_free_device(idev);
1736 static struct input_dev *imon_init_touch(struct imon_context *ictx)
1738 struct input_dev *touch;
1741 touch = input_allocate_device();
1743 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1744 goto touch_alloc_failed;
1747 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1748 "iMON USB Touchscreen (%04x:%04x)",
1749 ictx->vendor, ictx->product);
1750 touch->name = ictx->name_touch;
1752 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1753 sizeof(ictx->phys_touch));
1754 strlcat(ictx->phys_touch, "/input1", sizeof(ictx->phys_touch));
1755 touch->phys = ictx->phys_touch;
1758 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1759 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1760 BIT_MASK(BTN_TOUCH);
1761 input_set_abs_params(touch, ABS_X,
1763 input_set_abs_params(touch, ABS_Y,
1766 input_set_drvdata(touch, ictx);
1768 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1769 touch->dev.parent = ictx->dev;
1770 ret = input_register_device(touch);
1772 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1773 goto touch_register_failed;
1778 touch_register_failed:
1779 input_free_device(ictx->touch);
1780 mutex_unlock(&ictx->lock);
1786 static bool imon_find_endpoints(struct imon_context *ictx,
1787 struct usb_host_interface *iface_desc)
1789 struct usb_endpoint_descriptor *ep;
1790 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1791 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1792 int ifnum = iface_desc->desc.bInterfaceNumber;
1793 int num_endpts = iface_desc->desc.bNumEndpoints;
1794 int i, ep_dir, ep_type;
1795 bool ir_ep_found = 0;
1796 bool display_ep_found = 0;
1797 bool tx_control = 0;
1800 * Scan the endpoint list and set:
1801 * first input endpoint = IR endpoint
1802 * first output endpoint = display endpoint
1804 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
1805 ep = &iface_desc->endpoint[i].desc;
1806 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1807 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1809 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
1810 ep_type == USB_ENDPOINT_XFER_INT) {
1814 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
1816 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
1817 ep_type == USB_ENDPOINT_XFER_INT) {
1819 display_ep_found = 1;
1820 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
1825 ictx->rx_endpoint_intf0 = rx_endpoint;
1827 * tx is used to send characters to lcd/vfd, associate RF
1828 * remotes, set IR protocol, and maybe more...
1830 ictx->tx_endpoint = tx_endpoint;
1832 ictx->rx_endpoint_intf1 = rx_endpoint;
1836 * If we didn't find a display endpoint, this is probably one of the
1837 * newer iMON devices that use control urb instead of interrupt
1839 if (!display_ep_found) {
1841 display_ep_found = 1;
1842 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
1843 "interface OUT endpoint\n", __func__);
1847 * Some iMON receivers have no display. Unfortunately, it seems
1848 * that SoundGraph recycles device IDs between devices both with
1851 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
1852 display_ep_found = 0;
1853 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
1857 * iMON Touch devices have a VGA touchscreen, but no "display", as
1858 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
1860 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
1861 display_ep_found = 0;
1862 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
1865 /* Input endpoint is mandatory */
1867 err("%s: no valid input (IR) endpoint found.", __func__);
1869 ictx->tx_control = tx_control;
1871 if (display_ep_found)
1872 ictx->display_supported = true;
1878 static struct imon_context *imon_init_intf0(struct usb_interface *intf)
1880 struct imon_context *ictx;
1883 struct device *dev = &intf->dev;
1884 struct usb_host_interface *iface_desc;
1887 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
1889 dev_err(dev, "%s: kzalloc failed for context", __func__);
1892 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1894 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
1895 goto rx_urb_alloc_failed;
1897 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1899 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
1901 goto tx_urb_alloc_failed;
1904 mutex_init(&ictx->lock);
1906 mutex_lock(&ictx->lock);
1909 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
1910 ictx->dev_present_intf0 = 1;
1911 ictx->rx_urb_intf0 = rx_urb;
1912 ictx->tx_urb = tx_urb;
1914 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
1915 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
1918 iface_desc = intf->cur_altsetting;
1919 if (!imon_find_endpoints(ictx, iface_desc)) {
1920 goto find_endpoint_failed;
1923 ictx->idev = imon_init_idev(ictx);
1925 dev_err(dev, "%s: input device setup failed\n", __func__);
1926 goto idev_setup_failed;
1929 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
1930 usb_rcvintpipe(ictx->usbdev_intf0,
1931 ictx->rx_endpoint_intf0->bEndpointAddress),
1932 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
1933 usb_rx_callback_intf0, ictx,
1934 ictx->rx_endpoint_intf0->bInterval);
1936 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
1938 err("%s: usb_submit_urb failed for intf0 (%d)",
1940 goto urb_submit_failed;
1946 input_unregister_device(ictx->idev);
1947 input_free_device(ictx->idev);
1949 find_endpoint_failed:
1950 mutex_unlock(&ictx->lock);
1951 usb_free_urb(tx_urb);
1952 tx_urb_alloc_failed:
1953 usb_free_urb(rx_urb);
1954 rx_urb_alloc_failed:
1957 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
1962 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
1963 struct imon_context *ictx)
1966 struct usb_host_interface *iface_desc;
1969 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1971 err("%s: usb_alloc_urb failed for IR urb", __func__);
1972 goto rx_urb_alloc_failed;
1975 mutex_lock(&ictx->lock);
1977 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
1978 init_timer(&ictx->ttimer);
1979 ictx->ttimer.data = (unsigned long)ictx;
1980 ictx->ttimer.function = imon_touch_display_timeout;
1983 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
1984 ictx->dev_present_intf1 = 1;
1985 ictx->rx_urb_intf1 = rx_urb;
1988 iface_desc = intf->cur_altsetting;
1989 if (!imon_find_endpoints(ictx, iface_desc))
1990 goto find_endpoint_failed;
1992 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
1993 ictx->touch = imon_init_touch(ictx);
1995 goto touch_setup_failed;
1999 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2000 usb_rcvintpipe(ictx->usbdev_intf1,
2001 ictx->rx_endpoint_intf1->bEndpointAddress),
2002 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2003 usb_rx_callback_intf1, ictx,
2004 ictx->rx_endpoint_intf1->bInterval);
2006 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2009 err("%s: usb_submit_urb failed for intf1 (%d)",
2011 goto urb_submit_failed;
2018 input_unregister_device(ictx->touch);
2019 input_free_device(ictx->touch);
2022 find_endpoint_failed:
2023 mutex_unlock(&ictx->lock);
2024 usb_free_urb(rx_urb);
2025 rx_urb_alloc_failed:
2026 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2032 * The 0x15c2:0xffdc device ID was used for umpteen different imon
2033 * devices, and all of them constantly spew interrupts, even when there
2034 * is no actual data to report. However, byte 6 of this buffer looks like
2035 * its unique across device variants, so we're trying to key off that to
2036 * figure out which display type (if any) and what IR protocol the device
2037 * actually supports. These devices have their IR protocol hard-coded into
2038 * their firmware, they can't be changed on the fly like the newer hardware.
2040 static void imon_get_ffdc_type(struct imon_context *ictx)
2042 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
2043 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
2044 u64 allowed_protos = IR_TYPE_OTHER;
2046 switch (ffdc_cfg_byte) {
2047 /* iMON Knob, no display, iMON IR + vol knob */
2049 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
2050 ictx->display_supported = false;
2052 /* iMON VFD, no IR (does have vol knob tho) */
2054 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
2055 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2057 /* iMON VFD, iMON IR */
2060 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
2061 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2063 /* iMON LCD, MCE IR */
2065 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
2066 detected_display_type = IMON_DISPLAY_TYPE_LCD;
2067 allowed_protos = IR_TYPE_RC6;
2070 dev_info(ictx->dev, "Unknown 0xffdc device, "
2071 "defaulting to VFD and iMON IR");
2072 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2076 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
2078 ictx->display_type = detected_display_type;
2079 ictx->props->allowed_protos = allowed_protos;
2080 ictx->ir_type = allowed_protos;
2083 static void imon_set_display_type(struct imon_context *ictx,
2084 struct usb_interface *intf)
2086 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
2089 * Try to auto-detect the type of display if the user hasn't set
2090 * it by hand via the display_type modparam. Default is VFD.
2093 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
2094 switch (ictx->product) {
2096 /* set in imon_get_ffdc_type() */
2097 configured_display_type = ictx->display_type;
2101 configured_display_type = IMON_DISPLAY_TYPE_VGA;
2106 configured_display_type = IMON_DISPLAY_TYPE_LCD;
2112 configured_display_type = IMON_DISPLAY_TYPE_NONE;
2113 ictx->display_supported = false;
2118 configured_display_type = IMON_DISPLAY_TYPE_VFD;
2122 configured_display_type = display_type;
2123 if (display_type == IMON_DISPLAY_TYPE_NONE)
2124 ictx->display_supported = false;
2126 ictx->display_supported = true;
2127 dev_info(ictx->dev, "%s: overriding display type to %d via "
2128 "modparam\n", __func__, display_type);
2131 ictx->display_type = configured_display_type;
2134 static void imon_init_display(struct imon_context *ictx,
2135 struct usb_interface *intf)
2139 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2141 /* set up sysfs entry for built-in clock */
2142 ret = sysfs_create_group(&intf->dev.kobj,
2143 &imon_display_attribute_group);
2145 dev_err(ictx->dev, "Could not create display sysfs "
2146 "entries(%d)", ret);
2148 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2149 ret = usb_register_dev(intf, &imon_lcd_class);
2151 ret = usb_register_dev(intf, &imon_vfd_class);
2153 /* Not a fatal error, so ignore */
2154 dev_info(ictx->dev, "could not get a minor number for "
2160 * Callback function for USB core API: Probe
2162 static int __devinit imon_probe(struct usb_interface *interface,
2163 const struct usb_device_id *id)
2165 struct usb_device *usbdev = NULL;
2166 struct usb_host_interface *iface_desc = NULL;
2167 struct usb_interface *first_if;
2168 struct device *dev = &interface->dev;
2169 int ifnum, code_length, sysfs_err;
2171 struct imon_context *ictx = NULL;
2172 struct imon_context *first_if_ctx = NULL;
2173 u16 vendor, product;
2174 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
2175 0x00, 0x00, 0x00, 0x88 };
2177 code_length = BUF_CHUNK_SIZE * 8;
2179 usbdev = usb_get_dev(interface_to_usbdev(interface));
2180 iface_desc = interface->cur_altsetting;
2181 ifnum = iface_desc->desc.bInterfaceNumber;
2182 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2183 product = le16_to_cpu(usbdev->descriptor.idProduct);
2185 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2186 __func__, vendor, product, ifnum);
2188 /* prevent races probing devices w/multiple interfaces */
2189 mutex_lock(&driver_lock);
2191 first_if = usb_ifnum_to_if(usbdev, 0);
2192 first_if_ctx = (struct imon_context *)usb_get_intfdata(first_if);
2195 ictx = imon_init_intf0(interface);
2197 err("%s: failed to initialize context!\n", __func__);
2202 if (product == 0xffdc) {
2203 /* RF products *also* use 0xffdc... sigh... */
2204 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2205 &imon_rf_attribute_group);
2207 err("%s: Could not create RF sysfs entries(%d)",
2208 __func__, sysfs_err);
2212 /* this is the secondary interface on the device */
2213 ictx = imon_init_intf1(interface, first_if_ctx);
2215 err("%s: failed to attach to context!\n", __func__);
2222 usb_set_intfdata(interface, ictx);
2225 /* Enable front-panel buttons and/or knobs */
2226 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
2227 ret = send_packet(ictx);
2228 /* Not fatal, but warn about it */
2230 dev_info(dev, "failed to enable panel buttons "
2233 if (product == 0xffdc)
2234 imon_get_ffdc_type(ictx);
2236 imon_set_display_type(ictx, interface);
2238 if (ictx->display_supported)
2239 imon_init_display(ictx, interface);
2242 /* set IR protocol/remote type */
2243 ret = imon_ir_change_protocol(ictx, ictx->ir_type);
2245 dev_warn(dev, "%s: failed to set IR protocol, falling back "
2246 "to standard iMON protocol mode\n", __func__);
2247 ictx->ir_type = IR_TYPE_OTHER;
2250 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2251 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2252 usbdev->bus->busnum, usbdev->devnum);
2254 mutex_unlock(&ictx->lock);
2255 mutex_unlock(&driver_lock);
2260 mutex_unlock(&driver_lock);
2261 dev_err(dev, "unable to register, err %d\n", ret);
2267 * Callback function for USB core API: disconnect
2269 static void __devexit imon_disconnect(struct usb_interface *interface)
2271 struct imon_context *ictx;
2275 /* prevent races with multi-interface device probing and display_open */
2276 mutex_lock(&driver_lock);
2278 ictx = usb_get_intfdata(interface);
2280 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2282 mutex_lock(&ictx->lock);
2285 * sysfs_remove_group is safe to call even if sysfs_create_group
2286 * hasn't been called
2288 sysfs_remove_group(&interface->dev.kobj,
2289 &imon_display_attribute_group);
2290 sysfs_remove_group(&interface->dev.kobj,
2291 &imon_rf_attribute_group);
2293 usb_set_intfdata(interface, NULL);
2295 /* Abort ongoing write */
2296 if (ictx->tx.busy) {
2297 usb_kill_urb(ictx->tx_urb);
2298 complete_all(&ictx->tx.finished);
2302 ictx->dev_present_intf0 = 0;
2303 usb_kill_urb(ictx->rx_urb_intf0);
2304 input_unregister_device(ictx->idev);
2305 if (ictx->display_supported) {
2306 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2307 usb_deregister_dev(interface, &imon_lcd_class);
2309 usb_deregister_dev(interface, &imon_vfd_class);
2312 ictx->dev_present_intf1 = 0;
2313 usb_kill_urb(ictx->rx_urb_intf1);
2314 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2315 input_unregister_device(ictx->touch);
2318 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1) {
2319 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2320 del_timer_sync(&ictx->ttimer);
2321 mutex_unlock(&ictx->lock);
2322 if (!ictx->display_isopen)
2323 free_imon_context(ictx);
2325 if (ictx->ir_type == IR_TYPE_RC6)
2326 del_timer_sync(&ictx->itimer);
2327 mutex_unlock(&ictx->lock);
2330 mutex_unlock(&driver_lock);
2332 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2336 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2338 struct imon_context *ictx = usb_get_intfdata(intf);
2339 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2342 usb_kill_urb(ictx->rx_urb_intf0);
2344 usb_kill_urb(ictx->rx_urb_intf1);
2349 static int imon_resume(struct usb_interface *intf)
2352 struct imon_context *ictx = usb_get_intfdata(intf);
2353 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2356 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2357 usb_rcvintpipe(ictx->usbdev_intf0,
2358 ictx->rx_endpoint_intf0->bEndpointAddress),
2359 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2360 usb_rx_callback_intf0, ictx,
2361 ictx->rx_endpoint_intf0->bInterval);
2363 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2366 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2367 usb_rcvintpipe(ictx->usbdev_intf1,
2368 ictx->rx_endpoint_intf1->bEndpointAddress),
2369 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2370 usb_rx_callback_intf1, ictx,
2371 ictx->rx_endpoint_intf1->bInterval);
2373 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2379 static int __init imon_init(void)
2383 rc = usb_register(&imon_driver);
2385 err("%s: usb register failed(%d)", __func__, rc);
2392 static void __exit imon_exit(void)
2394 usb_deregister(&imon_driver);
2397 module_init(imon_init);
2398 module_exit(imon_exit);