V4L/DVB: imon: split mouse events to a separate input dev
[linux-2.6/btrfs-unstable.git] / drivers / media / IR / imon.c
blobd36fe7239782b001229495f369b64e9a292ef9ae
1 /*
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.2"
49 #define DISPLAY_MINOR_BASE 144
50 #define DEVICE_NAME "lcd%d"
52 #define BUF_CHUNK_SIZE 8
53 #define BUF_SIZE 128
55 #define BIT_DURATION 250 /* each bit received is 250us */
57 #define IMON_CLOCK_ENABLE_PACKETS 2
59 /*** P R O T O T Y P E S ***/
61 /* USB Callback prototypes */
62 static int imon_probe(struct usb_interface *interface,
63 const struct usb_device_id *id);
64 static void imon_disconnect(struct usb_interface *interface);
65 static void usb_rx_callback_intf0(struct urb *urb);
66 static void usb_rx_callback_intf1(struct urb *urb);
67 static void usb_tx_callback(struct urb *urb);
69 /* suspend/resume support */
70 static int imon_resume(struct usb_interface *intf);
71 static int imon_suspend(struct usb_interface *intf, pm_message_t message);
73 /* Display file_operations function prototypes */
74 static int display_open(struct inode *inode, struct file *file);
75 static int display_close(struct inode *inode, struct file *file);
77 /* VFD write operation */
78 static ssize_t vfd_write(struct file *file, const char *buf,
79 size_t n_bytes, loff_t *pos);
81 /* LCD file_operations override function prototypes */
82 static ssize_t lcd_write(struct file *file, const char *buf,
83 size_t n_bytes, loff_t *pos);
85 /*** G L O B A L S ***/
87 struct imon_context {
88 struct device *dev;
89 struct ir_dev_props *props;
90 /* Newer devices have two interfaces */
91 struct usb_device *usbdev_intf0;
92 struct usb_device *usbdev_intf1;
94 bool display_supported; /* not all controllers do */
95 bool display_isopen; /* display port has been opened */
96 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
97 bool rf_isassociating; /* RF remote associating */
98 bool dev_present_intf0; /* USB device presence, interface 0 */
99 bool dev_present_intf1; /* USB device presence, interface 1 */
101 struct mutex lock; /* to lock this object */
102 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
104 struct usb_endpoint_descriptor *rx_endpoint_intf0;
105 struct usb_endpoint_descriptor *rx_endpoint_intf1;
106 struct usb_endpoint_descriptor *tx_endpoint;
107 struct urb *rx_urb_intf0;
108 struct urb *rx_urb_intf1;
109 struct urb *tx_urb;
110 bool tx_control;
111 unsigned char usb_rx_buf[8];
112 unsigned char usb_tx_buf[8];
114 struct tx_t {
115 unsigned char data_buf[35]; /* user data buffer */
116 struct completion finished; /* wait for write to finish */
117 bool busy; /* write in progress */
118 int status; /* status of tx completion */
119 } tx;
121 u16 vendor; /* usb vendor ID */
122 u16 product; /* usb product ID */
124 struct input_dev *rdev; /* input device for remote */
125 struct input_dev *idev; /* input device for panel & IR mouse */
126 struct input_dev *touch; /* input device for touchscreen */
128 u32 kc; /* current input keycode */
129 u32 last_keycode; /* last reported input keycode */
130 u32 rc_scancode; /* the computed remote scancode */
131 u8 rc_toggle; /* the computed remote toggle bit */
132 u64 ir_type; /* iMON or MCE (RC6) IR protocol? */
133 bool release_code; /* some keys send a release code */
135 u8 display_type; /* store the display type */
136 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
138 char name_rdev[128]; /* rc input device name */
139 char phys_rdev[64]; /* rc input device phys path */
141 char name_idev[128]; /* input device name */
142 char phys_idev[64]; /* input device phys path */
144 char name_touch[128]; /* touch screen name */
145 char phys_touch[64]; /* touch screen phys path */
146 struct timer_list ttimer; /* touch screen timer */
147 int touch_x; /* x coordinate on touchscreen */
148 int touch_y; /* y coordinate on touchscreen */
151 #define TOUCH_TIMEOUT (HZ/30)
153 /* vfd character device file operations */
154 static const struct file_operations vfd_fops = {
155 .owner = THIS_MODULE,
156 .open = &display_open,
157 .write = &vfd_write,
158 .release = &display_close
161 /* lcd character device file operations */
162 static const struct file_operations lcd_fops = {
163 .owner = THIS_MODULE,
164 .open = &display_open,
165 .write = &lcd_write,
166 .release = &display_close
169 enum {
170 IMON_DISPLAY_TYPE_AUTO = 0,
171 IMON_DISPLAY_TYPE_VFD = 1,
172 IMON_DISPLAY_TYPE_LCD = 2,
173 IMON_DISPLAY_TYPE_VGA = 3,
174 IMON_DISPLAY_TYPE_NONE = 4,
177 enum {
178 IMON_KEY_IMON = 0,
179 IMON_KEY_MCE = 1,
180 IMON_KEY_PANEL = 2,
184 * USB Device ID for iMON USB Control Boards
186 * The Windows drivers contain 6 different inf files, more or less one for
187 * each new device until the 0x0034-0x0046 devices, which all use the same
188 * driver. Some of the devices in the 34-46 range haven't been definitively
189 * identified yet. Early devices have either a TriGem Computer, Inc. or a
190 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
191 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
192 * the ffdc and later devices, which do onboard decoding.
194 static struct usb_device_id imon_usb_id_table[] = {
196 * Several devices with this same device ID, all use iMON_PAD.inf
197 * SoundGraph iMON PAD (IR & VFD)
198 * SoundGraph iMON PAD (IR & LCD)
199 * SoundGraph iMON Knob (IR only)
201 { USB_DEVICE(0x15c2, 0xffdc) },
204 * Newer devices, all driven by the latest iMON Windows driver, full
205 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
206 * Need user input to fill in details on unknown devices.
208 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
209 { USB_DEVICE(0x15c2, 0x0034) },
210 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
211 { USB_DEVICE(0x15c2, 0x0035) },
212 /* SoundGraph iMON OEM VFD (IR & VFD) */
213 { USB_DEVICE(0x15c2, 0x0036) },
214 /* device specifics unknown */
215 { USB_DEVICE(0x15c2, 0x0037) },
216 /* SoundGraph iMON OEM LCD (IR & LCD) */
217 { USB_DEVICE(0x15c2, 0x0038) },
218 /* SoundGraph iMON UltraBay (IR & LCD) */
219 { USB_DEVICE(0x15c2, 0x0039) },
220 /* device specifics unknown */
221 { USB_DEVICE(0x15c2, 0x003a) },
222 /* device specifics unknown */
223 { USB_DEVICE(0x15c2, 0x003b) },
224 /* SoundGraph iMON OEM Inside (IR only) */
225 { USB_DEVICE(0x15c2, 0x003c) },
226 /* device specifics unknown */
227 { USB_DEVICE(0x15c2, 0x003d) },
228 /* device specifics unknown */
229 { USB_DEVICE(0x15c2, 0x003e) },
230 /* device specifics unknown */
231 { USB_DEVICE(0x15c2, 0x003f) },
232 /* device specifics unknown */
233 { USB_DEVICE(0x15c2, 0x0040) },
234 /* SoundGraph iMON MINI (IR only) */
235 { USB_DEVICE(0x15c2, 0x0041) },
236 /* Antec Veris Multimedia Station EZ External (IR only) */
237 { USB_DEVICE(0x15c2, 0x0042) },
238 /* Antec Veris Multimedia Station Basic Internal (IR only) */
239 { USB_DEVICE(0x15c2, 0x0043) },
240 /* Antec Veris Multimedia Station Elite (IR & VFD) */
241 { USB_DEVICE(0x15c2, 0x0044) },
242 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
243 { USB_DEVICE(0x15c2, 0x0045) },
244 /* device specifics unknown */
245 { USB_DEVICE(0x15c2, 0x0046) },
249 /* USB Device data */
250 static struct usb_driver imon_driver = {
251 .name = MOD_NAME,
252 .probe = imon_probe,
253 .disconnect = imon_disconnect,
254 .suspend = imon_suspend,
255 .resume = imon_resume,
256 .id_table = imon_usb_id_table,
259 static struct usb_class_driver imon_vfd_class = {
260 .name = DEVICE_NAME,
261 .fops = &vfd_fops,
262 .minor_base = DISPLAY_MINOR_BASE,
265 static struct usb_class_driver imon_lcd_class = {
266 .name = DEVICE_NAME,
267 .fops = &lcd_fops,
268 .minor_base = DISPLAY_MINOR_BASE,
271 /* imon receiver front panel/knob key table */
272 static const struct {
273 u64 hw_code;
274 u32 keycode;
275 } imon_panel_key_table[] = {
276 { 0x000000000f00ffeell, KEY_PROG1 }, /* Go */
277 { 0x000000001f00ffeell, KEY_AUDIO },
278 { 0x000000002000ffeell, KEY_VIDEO },
279 { 0x000000002100ffeell, KEY_CAMERA },
280 { 0x000000002700ffeell, KEY_DVD },
281 { 0x000000002300ffeell, KEY_TV },
282 { 0x000000000500ffeell, KEY_PREVIOUS },
283 { 0x000000000700ffeell, KEY_REWIND },
284 { 0x000000000400ffeell, KEY_STOP },
285 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
286 { 0x000000000800ffeell, KEY_FASTFORWARD },
287 { 0x000000000600ffeell, KEY_NEXT },
288 { 0x000000010000ffeell, KEY_RIGHT },
289 { 0x000001000000ffeell, KEY_LEFT },
290 { 0x000000003d00ffeell, KEY_SELECT },
291 { 0x000100000000ffeell, KEY_VOLUMEUP },
292 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
293 { 0x000000000100ffeell, KEY_MUTE },
294 /* iMON Knob values */
295 { 0x000100ffffffffeell, KEY_VOLUMEUP },
296 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
297 { 0x000008ffffffffeell, KEY_MUTE },
300 /* to prevent races between open() and disconnect(), probing, etc */
301 static DEFINE_MUTEX(driver_lock);
303 /* Module bookkeeping bits */
304 MODULE_AUTHOR(MOD_AUTHOR);
305 MODULE_DESCRIPTION(MOD_DESC);
306 MODULE_VERSION(MOD_VERSION);
307 MODULE_LICENSE("GPL");
308 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
310 static bool debug;
311 module_param(debug, bool, S_IRUGO | S_IWUSR);
312 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes(default: no)");
314 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
315 static int display_type;
316 module_param(display_type, int, S_IRUGO);
317 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
318 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
320 static int pad_stabilize = 1;
321 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
322 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
323 "presses in arrow key mode. 0=disable, 1=enable (default).");
326 * In certain use cases, mouse mode isn't really helpful, and could actually
327 * cause confusion, so allow disabling it when the IR device is open.
329 static bool nomouse;
330 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
331 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
332 "open. 0=don't disable, 1=disable. (default: don't disable)");
334 /* threshold at which a pad push registers as an arrow key in kbd mode */
335 static int pad_thresh;
336 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
337 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
338 "arrow key in kbd mode (default: 28)");
341 static void free_imon_context(struct imon_context *ictx)
343 struct device *dev = ictx->dev;
345 usb_free_urb(ictx->tx_urb);
346 usb_free_urb(ictx->rx_urb_intf0);
347 usb_free_urb(ictx->rx_urb_intf1);
348 kfree(ictx);
350 dev_dbg(dev, "%s: iMON context freed\n", __func__);
354 * Called when the Display device (e.g. /dev/lcd0)
355 * is opened by the application.
357 static int display_open(struct inode *inode, struct file *file)
359 struct usb_interface *interface;
360 struct imon_context *ictx = NULL;
361 int subminor;
362 int retval = 0;
364 /* prevent races with disconnect */
365 mutex_lock(&driver_lock);
367 subminor = iminor(inode);
368 interface = usb_find_interface(&imon_driver, subminor);
369 if (!interface) {
370 err("%s: could not find interface for minor %d",
371 __func__, subminor);
372 retval = -ENODEV;
373 goto exit;
375 ictx = usb_get_intfdata(interface);
377 if (!ictx) {
378 err("%s: no context found for minor %d", __func__, subminor);
379 retval = -ENODEV;
380 goto exit;
383 mutex_lock(&ictx->lock);
385 if (!ictx->display_supported) {
386 err("%s: display not supported by device", __func__);
387 retval = -ENODEV;
388 } else if (ictx->display_isopen) {
389 err("%s: display port is already open", __func__);
390 retval = -EBUSY;
391 } else {
392 ictx->display_isopen = true;
393 file->private_data = ictx;
394 dev_dbg(ictx->dev, "display port opened\n");
397 mutex_unlock(&ictx->lock);
399 exit:
400 mutex_unlock(&driver_lock);
401 return retval;
405 * Called when the display device (e.g. /dev/lcd0)
406 * is closed by the application.
408 static int display_close(struct inode *inode, struct file *file)
410 struct imon_context *ictx = NULL;
411 int retval = 0;
413 ictx = file->private_data;
415 if (!ictx) {
416 err("%s: no context for device", __func__);
417 return -ENODEV;
420 mutex_lock(&ictx->lock);
422 if (!ictx->display_supported) {
423 err("%s: display not supported by device", __func__);
424 retval = -ENODEV;
425 } else if (!ictx->display_isopen) {
426 err("%s: display is not open", __func__);
427 retval = -EIO;
428 } else {
429 ictx->display_isopen = false;
430 dev_dbg(ictx->dev, "display port closed\n");
431 if (!ictx->dev_present_intf0) {
433 * Device disconnected before close and IR port is not
434 * open. If IR port is open, context will be deleted by
435 * ir_close.
437 mutex_unlock(&ictx->lock);
438 free_imon_context(ictx);
439 return retval;
443 mutex_unlock(&ictx->lock);
444 return retval;
448 * Sends a packet to the device -- this function must be called
449 * with ictx->lock held.
451 static int send_packet(struct imon_context *ictx)
453 unsigned int pipe;
454 unsigned long timeout;
455 int interval = 0;
456 int retval = 0;
457 struct usb_ctrlrequest *control_req = NULL;
459 /* Check if we need to use control or interrupt urb */
460 if (!ictx->tx_control) {
461 pipe = usb_sndintpipe(ictx->usbdev_intf0,
462 ictx->tx_endpoint->bEndpointAddress);
463 interval = ictx->tx_endpoint->bInterval;
465 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
466 ictx->usb_tx_buf,
467 sizeof(ictx->usb_tx_buf),
468 usb_tx_callback, ictx, interval);
470 ictx->tx_urb->actual_length = 0;
471 } else {
472 /* fill request into kmalloc'ed space: */
473 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
474 GFP_KERNEL);
475 if (control_req == NULL)
476 return -ENOMEM;
478 /* setup packet is '21 09 0200 0001 0008' */
479 control_req->bRequestType = 0x21;
480 control_req->bRequest = 0x09;
481 control_req->wValue = cpu_to_le16(0x0200);
482 control_req->wIndex = cpu_to_le16(0x0001);
483 control_req->wLength = cpu_to_le16(0x0008);
485 /* control pipe is endpoint 0x00 */
486 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
488 /* build the control urb */
489 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
490 pipe, (unsigned char *)control_req,
491 ictx->usb_tx_buf,
492 sizeof(ictx->usb_tx_buf),
493 usb_tx_callback, ictx);
494 ictx->tx_urb->actual_length = 0;
497 init_completion(&ictx->tx.finished);
498 ictx->tx.busy = true;
499 smp_rmb(); /* ensure later readers know we're busy */
501 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
502 if (retval) {
503 ictx->tx.busy = false;
504 smp_rmb(); /* ensure later readers know we're not busy */
505 err("%s: error submitting urb(%d)", __func__, retval);
506 } else {
507 /* Wait for transmission to complete (or abort) */
508 mutex_unlock(&ictx->lock);
509 retval = wait_for_completion_interruptible(
510 &ictx->tx.finished);
511 if (retval)
512 err("%s: task interrupted", __func__);
513 mutex_lock(&ictx->lock);
515 retval = ictx->tx.status;
516 if (retval)
517 err("%s: packet tx failed (%d)", __func__, retval);
520 kfree(control_req);
523 * Induce a mandatory 5ms delay before returning, as otherwise,
524 * send_packet can get called so rapidly as to overwhelm the device,
525 * particularly on faster systems and/or those with quirky usb.
527 timeout = msecs_to_jiffies(5);
528 set_current_state(TASK_UNINTERRUPTIBLE);
529 schedule_timeout(timeout);
531 return retval;
535 * Sends an associate packet to the iMON 2.4G.
537 * This might not be such a good idea, since it has an id collision with
538 * some versions of the "IR & VFD" combo. The only way to determine if it
539 * is an RF version is to look at the product description string. (Which
540 * we currently do not fetch).
542 static int send_associate_24g(struct imon_context *ictx)
544 int retval;
545 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
546 0x00, 0x00, 0x00, 0x20 };
548 if (!ictx) {
549 err("%s: no context for device", __func__);
550 return -ENODEV;
553 if (!ictx->dev_present_intf0) {
554 err("%s: no iMON device present", __func__);
555 return -ENODEV;
558 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
559 retval = send_packet(ictx);
561 return retval;
565 * Sends packets to setup and show clock on iMON display
567 * Arguments: year - last 2 digits of year, month - 1..12,
568 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
569 * hour - 0..23, minute - 0..59, second - 0..59
571 static int send_set_imon_clock(struct imon_context *ictx,
572 unsigned int year, unsigned int month,
573 unsigned int day, unsigned int dow,
574 unsigned int hour, unsigned int minute,
575 unsigned int second)
577 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
578 int retval = 0;
579 int i;
581 if (!ictx) {
582 err("%s: no context for device", __func__);
583 return -ENODEV;
586 switch (ictx->display_type) {
587 case IMON_DISPLAY_TYPE_LCD:
588 clock_enable_pkt[0][0] = 0x80;
589 clock_enable_pkt[0][1] = year;
590 clock_enable_pkt[0][2] = month-1;
591 clock_enable_pkt[0][3] = day;
592 clock_enable_pkt[0][4] = hour;
593 clock_enable_pkt[0][5] = minute;
594 clock_enable_pkt[0][6] = second;
596 clock_enable_pkt[1][0] = 0x80;
597 clock_enable_pkt[1][1] = 0;
598 clock_enable_pkt[1][2] = 0;
599 clock_enable_pkt[1][3] = 0;
600 clock_enable_pkt[1][4] = 0;
601 clock_enable_pkt[1][5] = 0;
602 clock_enable_pkt[1][6] = 0;
604 if (ictx->product == 0xffdc) {
605 clock_enable_pkt[0][7] = 0x50;
606 clock_enable_pkt[1][7] = 0x51;
607 } else {
608 clock_enable_pkt[0][7] = 0x88;
609 clock_enable_pkt[1][7] = 0x8a;
612 break;
614 case IMON_DISPLAY_TYPE_VFD:
615 clock_enable_pkt[0][0] = year;
616 clock_enable_pkt[0][1] = month-1;
617 clock_enable_pkt[0][2] = day;
618 clock_enable_pkt[0][3] = dow;
619 clock_enable_pkt[0][4] = hour;
620 clock_enable_pkt[0][5] = minute;
621 clock_enable_pkt[0][6] = second;
622 clock_enable_pkt[0][7] = 0x40;
624 clock_enable_pkt[1][0] = 0;
625 clock_enable_pkt[1][1] = 0;
626 clock_enable_pkt[1][2] = 1;
627 clock_enable_pkt[1][3] = 0;
628 clock_enable_pkt[1][4] = 0;
629 clock_enable_pkt[1][5] = 0;
630 clock_enable_pkt[1][6] = 0;
631 clock_enable_pkt[1][7] = 0x42;
633 break;
635 default:
636 return -ENODEV;
639 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
640 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
641 retval = send_packet(ictx);
642 if (retval) {
643 err("%s: send_packet failed for packet %d",
644 __func__, i);
645 break;
649 return retval;
653 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
655 static ssize_t show_associate_remote(struct device *d,
656 struct device_attribute *attr,
657 char *buf)
659 struct imon_context *ictx = dev_get_drvdata(d);
661 if (!ictx)
662 return -ENODEV;
664 mutex_lock(&ictx->lock);
665 if (ictx->rf_isassociating)
666 strcpy(buf, "associating\n");
667 else
668 strcpy(buf, "closed\n");
670 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
671 "instructions on how to associate your iMON 2.4G DT/LT "
672 "remote\n");
673 mutex_unlock(&ictx->lock);
674 return strlen(buf);
677 static ssize_t store_associate_remote(struct device *d,
678 struct device_attribute *attr,
679 const char *buf, size_t count)
681 struct imon_context *ictx;
683 ictx = dev_get_drvdata(d);
685 if (!ictx)
686 return -ENODEV;
688 mutex_lock(&ictx->lock);
689 ictx->rf_isassociating = true;
690 send_associate_24g(ictx);
691 mutex_unlock(&ictx->lock);
693 return count;
697 * sysfs functions to control internal imon clock
699 static ssize_t show_imon_clock(struct device *d,
700 struct device_attribute *attr, char *buf)
702 struct imon_context *ictx = dev_get_drvdata(d);
703 size_t len;
705 if (!ictx)
706 return -ENODEV;
708 mutex_lock(&ictx->lock);
710 if (!ictx->display_supported) {
711 len = snprintf(buf, PAGE_SIZE, "Not supported.");
712 } else {
713 len = snprintf(buf, PAGE_SIZE,
714 "To set the clock on your iMON display:\n"
715 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
716 "%s", ictx->display_isopen ?
717 "\nNOTE: imon device must be closed\n" : "");
720 mutex_unlock(&ictx->lock);
722 return len;
725 static ssize_t store_imon_clock(struct device *d,
726 struct device_attribute *attr,
727 const char *buf, size_t count)
729 struct imon_context *ictx = dev_get_drvdata(d);
730 ssize_t retval;
731 unsigned int year, month, day, dow, hour, minute, second;
733 if (!ictx)
734 return -ENODEV;
736 mutex_lock(&ictx->lock);
738 if (!ictx->display_supported) {
739 retval = -ENODEV;
740 goto exit;
741 } else if (ictx->display_isopen) {
742 retval = -EBUSY;
743 goto exit;
746 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
747 &hour, &minute, &second) != 7) {
748 retval = -EINVAL;
749 goto exit;
752 if ((month < 1 || month > 12) ||
753 (day < 1 || day > 31) || (dow > 6) ||
754 (hour > 23) || (minute > 59) || (second > 59)) {
755 retval = -EINVAL;
756 goto exit;
759 retval = send_set_imon_clock(ictx, year, month, day, dow,
760 hour, minute, second);
761 if (retval)
762 goto exit;
764 retval = count;
765 exit:
766 mutex_unlock(&ictx->lock);
768 return retval;
772 static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
773 store_imon_clock);
775 static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
776 store_associate_remote);
778 static struct attribute *imon_display_sysfs_entries[] = {
779 &dev_attr_imon_clock.attr,
780 NULL
783 static struct attribute_group imon_display_attribute_group = {
784 .attrs = imon_display_sysfs_entries
787 static struct attribute *imon_rf_sysfs_entries[] = {
788 &dev_attr_associate_remote.attr,
789 NULL
792 static struct attribute_group imon_rf_attribute_group = {
793 .attrs = imon_rf_sysfs_entries
797 * Writes data to the VFD. The iMON VFD is 2x16 characters
798 * and requires data in 5 consecutive USB interrupt packets,
799 * each packet but the last carrying 7 bytes.
801 * I don't know if the VFD board supports features such as
802 * scrolling, clearing rows, blanking, etc. so at
803 * the caller must provide a full screen of data. If fewer
804 * than 32 bytes are provided spaces will be appended to
805 * generate a full screen.
807 static ssize_t vfd_write(struct file *file, const char *buf,
808 size_t n_bytes, loff_t *pos)
810 int i;
811 int offset;
812 int seq;
813 int retval = 0;
814 struct imon_context *ictx;
815 const unsigned char vfd_packet6[] = {
816 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
818 ictx = file->private_data;
819 if (!ictx) {
820 err("%s: no context for device", __func__);
821 return -ENODEV;
824 mutex_lock(&ictx->lock);
826 if (!ictx->dev_present_intf0) {
827 err("%s: no iMON device present", __func__);
828 retval = -ENODEV;
829 goto exit;
832 if (n_bytes <= 0 || n_bytes > 32) {
833 err("%s: invalid payload size", __func__);
834 retval = -EINVAL;
835 goto exit;
838 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
839 retval = -EFAULT;
840 goto exit;
843 /* Pad with spaces */
844 for (i = n_bytes; i < 32; ++i)
845 ictx->tx.data_buf[i] = ' ';
847 for (i = 32; i < 35; ++i)
848 ictx->tx.data_buf[i] = 0xFF;
850 offset = 0;
851 seq = 0;
853 do {
854 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
855 ictx->usb_tx_buf[7] = (unsigned char) seq;
857 retval = send_packet(ictx);
858 if (retval) {
859 err("%s: send packet failed for packet #%d",
860 __func__, seq/2);
861 goto exit;
862 } else {
863 seq += 2;
864 offset += 7;
867 } while (offset < 35);
869 /* Send packet #6 */
870 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
871 ictx->usb_tx_buf[7] = (unsigned char) seq;
872 retval = send_packet(ictx);
873 if (retval)
874 err("%s: send packet failed for packet #%d",
875 __func__, seq / 2);
877 exit:
878 mutex_unlock(&ictx->lock);
880 return (!retval) ? n_bytes : retval;
884 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
885 * packets. We accept data as 16 hexadecimal digits, followed by a
886 * newline (to make it easy to drive the device from a command-line
887 * -- even though the actual binary data is a bit complicated).
889 * The device itself is not a "traditional" text-mode display. It's
890 * actually a 16x96 pixel bitmap display. That means if you want to
891 * display text, you've got to have your own "font" and translate the
892 * text into bitmaps for display. This is really flexible (you can
893 * display whatever diacritics you need, and so on), but it's also
894 * a lot more complicated than most LCDs...
896 static ssize_t lcd_write(struct file *file, const char *buf,
897 size_t n_bytes, loff_t *pos)
899 int retval = 0;
900 struct imon_context *ictx;
902 ictx = file->private_data;
903 if (!ictx) {
904 err("%s: no context for device", __func__);
905 return -ENODEV;
908 mutex_lock(&ictx->lock);
910 if (!ictx->display_supported) {
911 err("%s: no iMON display present", __func__);
912 retval = -ENODEV;
913 goto exit;
916 if (n_bytes != 8) {
917 err("%s: invalid payload size: %d (expecting 8)",
918 __func__, (int) n_bytes);
919 retval = -EINVAL;
920 goto exit;
923 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
924 retval = -EFAULT;
925 goto exit;
928 retval = send_packet(ictx);
929 if (retval) {
930 err("%s: send packet failed!", __func__);
931 goto exit;
932 } else {
933 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
934 __func__, (int) n_bytes);
936 exit:
937 mutex_unlock(&ictx->lock);
938 return (!retval) ? n_bytes : retval;
942 * Callback function for USB core API: transmit data
944 static void usb_tx_callback(struct urb *urb)
946 struct imon_context *ictx;
948 if (!urb)
949 return;
950 ictx = (struct imon_context *)urb->context;
951 if (!ictx)
952 return;
954 ictx->tx.status = urb->status;
956 /* notify waiters that write has finished */
957 ictx->tx.busy = false;
958 smp_rmb(); /* ensure later readers know we're not busy */
959 complete(&ictx->tx.finished);
963 * report touchscreen input
965 static void imon_touch_display_timeout(unsigned long data)
967 struct imon_context *ictx = (struct imon_context *)data;
969 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
970 return;
972 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
973 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
974 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
975 input_sync(ictx->touch);
979 * iMON IR receivers support two different signal sets -- those used by
980 * the iMON remotes, and those used by the Windows MCE remotes (which is
981 * really just RC-6), but only one or the other at a time, as the signals
982 * are decoded onboard the receiver.
984 int imon_ir_change_protocol(void *priv, u64 ir_type)
986 int retval;
987 struct imon_context *ictx = priv;
988 struct device *dev = ictx->dev;
989 bool pad_mouse;
990 unsigned char ir_proto_packet[] = {
991 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
993 if (ir_type && !(ir_type & ictx->props->allowed_protos))
994 dev_warn(dev, "Looks like you're trying to use an IR protocol "
995 "this device does not support\n");
997 switch (ir_type) {
998 case IR_TYPE_RC6:
999 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1000 ir_proto_packet[0] = 0x01;
1001 pad_mouse = false;
1002 break;
1003 case IR_TYPE_UNKNOWN:
1004 case IR_TYPE_OTHER:
1005 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1006 if (pad_stabilize)
1007 pad_mouse = true;
1008 else {
1009 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1010 pad_mouse = false;
1012 /* ir_proto_packet[0] = 0x00; // already the default */
1013 ir_type = IR_TYPE_OTHER;
1014 break;
1015 default:
1016 dev_warn(dev, "Unsupported IR protocol specified, overriding "
1017 "to iMON IR protocol\n");
1018 if (pad_stabilize)
1019 pad_mouse = true;
1020 else {
1021 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1022 pad_mouse = false;
1024 /* ir_proto_packet[0] = 0x00; // already the default */
1025 ir_type = IR_TYPE_OTHER;
1026 break;
1029 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1031 retval = send_packet(ictx);
1032 if (retval)
1033 goto out;
1035 ictx->ir_type = ir_type;
1036 ictx->pad_mouse = pad_mouse;
1038 out:
1039 return retval;
1042 static inline int tv2int(const struct timeval *a, const struct timeval *b)
1044 int usecs = 0;
1045 int sec = 0;
1047 if (b->tv_usec > a->tv_usec) {
1048 usecs = 1000000;
1049 sec--;
1052 usecs += a->tv_usec - b->tv_usec;
1054 sec += a->tv_sec - b->tv_sec;
1055 sec *= 1000;
1056 usecs /= 1000;
1057 sec += usecs;
1059 if (sec < 0)
1060 sec = 1000;
1062 return sec;
1066 * The directional pad behaves a bit differently, depending on whether this is
1067 * one of the older ffdc devices or a newer device. Newer devices appear to
1068 * have a higher resolution matrix for more precise mouse movement, but it
1069 * makes things overly sensitive in keyboard mode, so we do some interesting
1070 * contortions to make it less touchy. Older devices run through the same
1071 * routine with shorter timeout and a smaller threshold.
1073 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1075 struct timeval ct;
1076 static struct timeval prev_time = {0, 0};
1077 static struct timeval hit_time = {0, 0};
1078 static int x, y, prev_result, hits;
1079 int result = 0;
1080 int msec, msec_hit;
1082 do_gettimeofday(&ct);
1083 msec = tv2int(&ct, &prev_time);
1084 msec_hit = tv2int(&ct, &hit_time);
1086 if (msec > 100) {
1087 x = 0;
1088 y = 0;
1089 hits = 0;
1092 x += a;
1093 y += b;
1095 prev_time = ct;
1097 if (abs(x) > threshold || abs(y) > threshold) {
1098 if (abs(y) > abs(x))
1099 result = (y > 0) ? 0x7F : 0x80;
1100 else
1101 result = (x > 0) ? 0x7F00 : 0x8000;
1103 x = 0;
1104 y = 0;
1106 if (result == prev_result) {
1107 hits++;
1109 if (hits > 3) {
1110 switch (result) {
1111 case 0x7F:
1112 y = 17 * threshold / 30;
1113 break;
1114 case 0x80:
1115 y -= 17 * threshold / 30;
1116 break;
1117 case 0x7F00:
1118 x = 17 * threshold / 30;
1119 break;
1120 case 0x8000:
1121 x -= 17 * threshold / 30;
1122 break;
1126 if (hits == 2 && msec_hit < timeout) {
1127 result = 0;
1128 hits = 1;
1130 } else {
1131 prev_result = result;
1132 hits = 1;
1133 hit_time = ct;
1137 return result;
1140 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1142 u32 keycode;
1143 u32 release;
1144 bool is_release_code = false;
1146 /* Look for the initial press of a button */
1147 keycode = ir_g_keycode_from_table(ictx->rdev, scancode);
1148 ictx->rc_toggle = 0x0;
1149 ictx->rc_scancode = scancode;
1151 /* Look for the release of a button */
1152 if (keycode == KEY_RESERVED) {
1153 release = scancode & ~0x4000;
1154 keycode = ir_g_keycode_from_table(ictx->rdev, release);
1155 if (keycode != KEY_RESERVED)
1156 is_release_code = true;
1159 ictx->release_code = is_release_code;
1161 return keycode;
1164 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1166 u32 keycode;
1168 #define MCE_KEY_MASK 0x7000
1169 #define MCE_TOGGLE_BIT 0x8000
1172 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1173 * (the toggle bit flipping between alternating key presses), while
1174 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1175 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1176 * but we can't or them into all codes, as some keys are decoded in
1177 * a different way w/o the same use of the toggle bit...
1179 if (scancode & 0x80000000)
1180 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1182 ictx->rc_scancode = scancode;
1183 keycode = ir_g_keycode_from_table(ictx->rdev, scancode);
1185 /* not used in mce mode, but make sure we know its false */
1186 ictx->release_code = false;
1188 return keycode;
1191 static u32 imon_panel_key_lookup(u64 code)
1193 int i;
1194 u32 keycode = KEY_RESERVED;
1196 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1197 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1198 keycode = imon_panel_key_table[i].keycode;
1199 break;
1203 return keycode;
1206 static bool imon_mouse_event(struct imon_context *ictx,
1207 unsigned char *buf, int len)
1209 char rel_x = 0x00, rel_y = 0x00;
1210 u8 right_shift = 1;
1211 bool mouse_input = true;
1212 int dir = 0;
1214 /* newer iMON device PAD or mouse button */
1215 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1216 rel_x = buf[2];
1217 rel_y = buf[3];
1218 right_shift = 1;
1219 /* 0xffdc iMON PAD or mouse button input */
1220 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1221 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1222 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1223 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1224 if (buf[0] & 0x02)
1225 rel_x |= ~0x0f;
1226 rel_x = rel_x + rel_x / 2;
1227 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1228 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1229 if (buf[0] & 0x01)
1230 rel_y |= ~0x0f;
1231 rel_y = rel_y + rel_y / 2;
1232 right_shift = 2;
1233 /* some ffdc devices decode mouse buttons differently... */
1234 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1235 right_shift = 2;
1236 /* ch+/- buttons, which we use for an emulated scroll wheel */
1237 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1238 dir = 1;
1239 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1240 dir = -1;
1241 } else
1242 mouse_input = false;
1244 if (mouse_input) {
1245 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1247 if (dir) {
1248 input_report_rel(ictx->idev, REL_WHEEL, dir);
1249 } else if (rel_x || rel_y) {
1250 input_report_rel(ictx->idev, REL_X, rel_x);
1251 input_report_rel(ictx->idev, REL_Y, rel_y);
1252 } else {
1253 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1254 input_report_key(ictx->idev, BTN_RIGHT,
1255 buf[1] >> right_shift & 0x1);
1257 input_sync(ictx->idev);
1258 ictx->last_keycode = ictx->kc;
1261 return mouse_input;
1264 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1266 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1267 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1268 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1269 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1270 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1271 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1272 input_sync(ictx->touch);
1275 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1277 int dir = 0;
1278 char rel_x = 0x00, rel_y = 0x00;
1279 u16 timeout, threshold;
1280 u32 scancode = KEY_RESERVED;
1283 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1284 * contain a position coordinate (x,y), with each component ranging
1285 * from -14 to 14. We want to down-sample this to only 4 discrete values
1286 * for up/down/left/right arrow keys. Also, when you get too close to
1287 * diagonals, it has a tendancy to jump back and forth, so lets try to
1288 * ignore when they get too close.
1290 if (ictx->product != 0xffdc) {
1291 /* first, pad to 8 bytes so it conforms with everything else */
1292 buf[5] = buf[6] = buf[7] = 0;
1293 timeout = 500; /* in msecs */
1294 /* (2*threshold) x (2*threshold) square */
1295 threshold = pad_thresh ? pad_thresh : 28;
1296 rel_x = buf[2];
1297 rel_y = buf[3];
1299 if (ictx->ir_type == IR_TYPE_OTHER && pad_stabilize) {
1300 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1301 dir = stabilize((int)rel_x, (int)rel_y,
1302 timeout, threshold);
1303 if (!dir) {
1304 ictx->kc = KEY_UNKNOWN;
1305 return;
1307 buf[2] = dir & 0xFF;
1308 buf[3] = (dir >> 8) & 0xFF;
1309 scancode = be32_to_cpu(*((u32 *)buf));
1311 } else {
1313 * Hack alert: instead of using keycodes, we have
1314 * to use hard-coded scancodes here...
1316 if (abs(rel_y) > abs(rel_x)) {
1317 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1318 buf[3] = 0;
1319 if (rel_y > 0)
1320 scancode = 0x01007f00; /* KEY_DOWN */
1321 else
1322 scancode = 0x01008000; /* KEY_UP */
1323 } else {
1324 buf[2] = 0;
1325 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1326 if (rel_x > 0)
1327 scancode = 0x0100007f; /* KEY_RIGHT */
1328 else
1329 scancode = 0x01000080; /* KEY_LEFT */
1334 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1335 * device (15c2:ffdc). The remote generates various codes from
1336 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1337 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1338 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1339 * reversed endianess. Extract direction from buffer, rotate endianess,
1340 * adjust sign and feed the values into stabilize(). The resulting codes
1341 * will be 0x01008000, 0x01007F00, which match the newer devices.
1343 } else {
1344 timeout = 10; /* in msecs */
1345 /* (2*threshold) x (2*threshold) square */
1346 threshold = pad_thresh ? pad_thresh : 15;
1348 /* buf[1] is x */
1349 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1350 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1351 if (buf[0] & 0x02)
1352 rel_x |= ~0x10+1;
1353 /* buf[2] is y */
1354 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1355 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1356 if (buf[0] & 0x01)
1357 rel_y |= ~0x10+1;
1359 buf[0] = 0x01;
1360 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1362 if (ictx->ir_type == IR_TYPE_OTHER && pad_stabilize) {
1363 dir = stabilize((int)rel_x, (int)rel_y,
1364 timeout, threshold);
1365 if (!dir) {
1366 ictx->kc = KEY_UNKNOWN;
1367 return;
1369 buf[2] = dir & 0xFF;
1370 buf[3] = (dir >> 8) & 0xFF;
1371 scancode = be32_to_cpu(*((u32 *)buf));
1372 } else {
1374 * Hack alert: instead of using keycodes, we have
1375 * to use hard-coded scancodes here...
1377 if (abs(rel_y) > abs(rel_x)) {
1378 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1379 buf[3] = 0;
1380 if (rel_y > 0)
1381 scancode = 0x01007f00; /* KEY_DOWN */
1382 else
1383 scancode = 0x01008000; /* KEY_UP */
1384 } else {
1385 buf[2] = 0;
1386 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1387 if (rel_x > 0)
1388 scancode = 0x0100007f; /* KEY_RIGHT */
1389 else
1390 scancode = 0x01000080; /* KEY_LEFT */
1395 if (scancode)
1396 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1400 * figure out if these is a press or a release. We don't actually
1401 * care about repeats, as those will be auto-generated within the IR
1402 * subsystem for repeating scancodes.
1404 static int imon_parse_press_type(struct imon_context *ictx,
1405 unsigned char *buf, u8 ktype)
1407 int press_type = 0;
1409 /* key release of 0x02XXXXXX key */
1410 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1411 ictx->kc = ictx->last_keycode;
1413 /* mouse button release on (some) 0xffdc devices */
1414 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1415 buf[2] == 0x81 && buf[3] == 0xb7)
1416 ictx->kc = ictx->last_keycode;
1418 /* mouse button release on (some other) 0xffdc devices */
1419 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1420 buf[2] == 0x81 && buf[3] == 0xb7)
1421 ictx->kc = ictx->last_keycode;
1423 /* mce-specific button handling, no keyup events */
1424 else if (ktype == IMON_KEY_MCE) {
1425 ictx->rc_toggle = buf[2];
1426 press_type = 1;
1428 /* incoherent or irrelevant data */
1429 } else if (ictx->kc == KEY_RESERVED)
1430 press_type = -EINVAL;
1432 /* key release of 0xXXXXXXb7 key */
1433 else if (ictx->release_code)
1434 press_type = 0;
1436 /* this is a button press */
1437 else
1438 press_type = 1;
1440 return press_type;
1444 * Process the incoming packet
1446 static void imon_incoming_packet(struct imon_context *ictx,
1447 struct urb *urb, int intf)
1449 int len = urb->actual_length;
1450 unsigned char *buf = urb->transfer_buffer;
1451 struct device *dev = ictx->dev;
1452 u32 kc;
1453 bool norelease = false;
1454 int i;
1455 u64 scancode;
1456 struct input_dev *idev = NULL;
1457 struct ir_input_dev *irdev = NULL;
1458 int press_type = 0;
1459 int msec;
1460 struct timeval t;
1461 static struct timeval prev_time = { 0, 0 };
1462 u8 ktype;
1464 idev = ictx->idev;
1465 irdev = input_get_drvdata(idev);
1467 /* filter out junk data on the older 0xffdc imon devices */
1468 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1469 return;
1471 /* Figure out what key was pressed */
1472 if (len == 8 && buf[7] == 0xee) {
1473 scancode = be64_to_cpu(*((u64 *)buf));
1474 ktype = IMON_KEY_PANEL;
1475 kc = imon_panel_key_lookup(scancode);
1476 } else {
1477 scancode = be32_to_cpu(*((u32 *)buf));
1478 if (ictx->ir_type == IR_TYPE_RC6) {
1479 ktype = IMON_KEY_IMON;
1480 if (buf[0] == 0x80)
1481 ktype = IMON_KEY_MCE;
1482 kc = imon_mce_key_lookup(ictx, scancode);
1483 } else {
1484 ktype = IMON_KEY_IMON;
1485 kc = imon_remote_key_lookup(ictx, scancode);
1489 /* keyboard/mouse mode toggle button */
1490 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1491 ictx->last_keycode = kc;
1492 if (!nomouse) {
1493 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1494 dev_dbg(dev, "toggling to %s mode\n",
1495 ictx->pad_mouse ? "mouse" : "keyboard");
1496 return;
1497 } else {
1498 ictx->pad_mouse = 0;
1499 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1503 ictx->kc = kc;
1505 /* send touchscreen events through input subsystem if touchpad data */
1506 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1507 buf[7] == 0x86) {
1508 imon_touch_event(ictx, buf);
1509 return;
1511 /* look for mouse events with pad in mouse mode */
1512 } else if (ictx->pad_mouse) {
1513 if (imon_mouse_event(ictx, buf, len))
1514 return;
1517 /* Now for some special handling to convert pad input to arrow keys */
1518 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1519 ((len == 8) && (buf[0] & 0x40) &&
1520 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1521 len = 8;
1522 imon_pad_to_keys(ictx, buf);
1523 norelease = true;
1526 if (debug) {
1527 printk(KERN_INFO "intf%d decoded packet: ", intf);
1528 for (i = 0; i < len; ++i)
1529 printk("%02x ", buf[i]);
1530 printk("\n");
1533 press_type = imon_parse_press_type(ictx, buf, ktype);
1534 if (press_type < 0)
1535 goto not_input_data;
1537 if (ictx->kc == KEY_UNKNOWN)
1538 goto unknown_key;
1540 if (ktype != IMON_KEY_PANEL) {
1541 if (press_type == 0)
1542 ir_keyup(irdev);
1543 else {
1544 ir_keydown(ictx->rdev, ictx->rc_scancode,
1545 ictx->rc_toggle);
1546 ictx->last_keycode = ictx->kc;
1548 return;
1551 /* Only panel type events left to process now */
1552 /* KEY_MUTE repeats from knob need to be suppressed */
1553 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
1554 do_gettimeofday(&t);
1555 msec = tv2int(&t, &prev_time);
1556 prev_time = t;
1557 if (msec < idev->rep[REP_DELAY])
1558 return;
1561 input_report_key(idev, ictx->kc, press_type);
1562 input_sync(idev);
1564 /* panel keys don't generate a release */
1565 input_report_key(idev, ictx->kc, 0);
1566 input_sync(idev);
1568 ictx->last_keycode = ictx->kc;
1570 return;
1572 unknown_key:
1573 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
1574 (long long)scancode);
1575 return;
1577 not_input_data:
1578 if (len != 8) {
1579 dev_warn(dev, "imon %s: invalid incoming packet "
1580 "size (len = %d, intf%d)\n", __func__, len, intf);
1581 return;
1584 /* iMON 2.4G associate frame */
1585 if (buf[0] == 0x00 &&
1586 buf[2] == 0xFF && /* REFID */
1587 buf[3] == 0xFF &&
1588 buf[4] == 0xFF &&
1589 buf[5] == 0xFF && /* iMON 2.4G */
1590 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1591 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1592 dev_warn(dev, "%s: remote associated refid=%02X\n",
1593 __func__, buf[1]);
1594 ictx->rf_isassociating = false;
1599 * Callback function for USB core API: receive data
1601 static void usb_rx_callback_intf0(struct urb *urb)
1603 struct imon_context *ictx;
1604 int intfnum = 0;
1606 if (!urb)
1607 return;
1609 ictx = (struct imon_context *)urb->context;
1610 if (!ictx)
1611 return;
1613 switch (urb->status) {
1614 case -ENOENT: /* usbcore unlink successful! */
1615 return;
1617 case -ESHUTDOWN: /* transport endpoint was shut down */
1618 break;
1620 case 0:
1621 imon_incoming_packet(ictx, urb, intfnum);
1622 break;
1624 default:
1625 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1626 __func__, urb->status);
1627 break;
1630 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1633 static void usb_rx_callback_intf1(struct urb *urb)
1635 struct imon_context *ictx;
1636 int intfnum = 1;
1638 if (!urb)
1639 return;
1641 ictx = (struct imon_context *)urb->context;
1642 if (!ictx)
1643 return;
1645 switch (urb->status) {
1646 case -ENOENT: /* usbcore unlink successful! */
1647 return;
1649 case -ESHUTDOWN: /* transport endpoint was shut down */
1650 break;
1652 case 0:
1653 imon_incoming_packet(ictx, urb, intfnum);
1654 break;
1656 default:
1657 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1658 __func__, urb->status);
1659 break;
1662 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1665 static struct input_dev *imon_init_rdev(struct imon_context *ictx)
1667 struct input_dev *rdev;
1668 struct ir_dev_props *props;
1669 int ret;
1671 rdev = input_allocate_device();
1672 props = kzalloc(sizeof(*props), GFP_KERNEL);
1673 if (!rdev || !props) {
1674 dev_err(ictx->dev, "remote control dev allocation failed\n");
1675 goto out;
1678 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1679 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1680 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1681 sizeof(ictx->phys_rdev));
1682 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1684 rdev->name = ictx->name_rdev;
1685 rdev->phys = ictx->phys_rdev;
1686 usb_to_input_id(ictx->usbdev_intf0, &rdev->id);
1687 rdev->dev.parent = ictx->dev;
1688 rdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
1689 input_set_drvdata(rdev, ictx);
1691 props->priv = ictx;
1692 props->driver_type = RC_DRIVER_SCANCODE;
1693 props->allowed_protos = IR_TYPE_OTHER | IR_TYPE_RC6; /* iMON PAD or MCE */
1694 props->change_protocol = imon_ir_change_protocol;
1695 ictx->props = props;
1697 ret = ir_input_register(rdev, RC_MAP_IMON_PAD, props, MOD_NAME);
1698 if (ret < 0) {
1699 dev_err(ictx->dev, "remote input dev register failed\n");
1700 goto out;
1703 return rdev;
1705 out:
1706 kfree(props);
1707 input_free_device(rdev);
1708 return NULL;
1711 static struct input_dev *imon_init_idev(struct imon_context *ictx)
1713 struct input_dev *idev;
1714 int ret, i;
1716 idev = input_allocate_device();
1717 if (!idev) {
1718 dev_err(ictx->dev, "input dev allocation failed\n");
1719 goto out;
1722 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1723 "iMON Panel, Knob and Mouse(%04x:%04x)",
1724 ictx->vendor, ictx->product);
1725 idev->name = ictx->name_idev;
1727 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1728 sizeof(ictx->phys_idev));
1729 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
1730 idev->phys = ictx->phys_idev;
1732 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
1734 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1735 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1736 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1737 BIT_MASK(REL_WHEEL);
1739 /* panel and/or knob code support */
1740 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1741 u32 kc = imon_panel_key_table[i].keycode;
1742 __set_bit(kc, idev->keybit);
1745 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1746 idev->dev.parent = ictx->dev;
1747 input_set_drvdata(idev, ictx);
1749 ret = input_register_device(idev);
1750 if (ret < 0) {
1751 dev_err(ictx->dev, "input dev register failed\n");
1752 goto out;
1755 return idev;
1757 out:
1758 input_free_device(idev);
1759 return NULL;
1762 static struct input_dev *imon_init_touch(struct imon_context *ictx)
1764 struct input_dev *touch;
1765 int ret;
1767 touch = input_allocate_device();
1768 if (!touch) {
1769 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1770 goto touch_alloc_failed;
1773 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1774 "iMON USB Touchscreen (%04x:%04x)",
1775 ictx->vendor, ictx->product);
1776 touch->name = ictx->name_touch;
1778 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1779 sizeof(ictx->phys_touch));
1780 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
1781 touch->phys = ictx->phys_touch;
1783 touch->evbit[0] =
1784 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1785 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1786 BIT_MASK(BTN_TOUCH);
1787 input_set_abs_params(touch, ABS_X,
1788 0x00, 0xfff, 0, 0);
1789 input_set_abs_params(touch, ABS_Y,
1790 0x00, 0xfff, 0, 0);
1792 input_set_drvdata(touch, ictx);
1794 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1795 touch->dev.parent = ictx->dev;
1796 ret = input_register_device(touch);
1797 if (ret < 0) {
1798 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1799 goto touch_register_failed;
1802 return touch;
1804 touch_register_failed:
1805 input_free_device(ictx->touch);
1807 touch_alloc_failed:
1808 return NULL;
1811 static bool imon_find_endpoints(struct imon_context *ictx,
1812 struct usb_host_interface *iface_desc)
1814 struct usb_endpoint_descriptor *ep;
1815 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1816 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1817 int ifnum = iface_desc->desc.bInterfaceNumber;
1818 int num_endpts = iface_desc->desc.bNumEndpoints;
1819 int i, ep_dir, ep_type;
1820 bool ir_ep_found = false;
1821 bool display_ep_found = false;
1822 bool tx_control = false;
1825 * Scan the endpoint list and set:
1826 * first input endpoint = IR endpoint
1827 * first output endpoint = display endpoint
1829 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
1830 ep = &iface_desc->endpoint[i].desc;
1831 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1832 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1834 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
1835 ep_type == USB_ENDPOINT_XFER_INT) {
1837 rx_endpoint = ep;
1838 ir_ep_found = true;
1839 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
1841 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
1842 ep_type == USB_ENDPOINT_XFER_INT) {
1843 tx_endpoint = ep;
1844 display_ep_found = true;
1845 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
1849 if (ifnum == 0) {
1850 ictx->rx_endpoint_intf0 = rx_endpoint;
1852 * tx is used to send characters to lcd/vfd, associate RF
1853 * remotes, set IR protocol, and maybe more...
1855 ictx->tx_endpoint = tx_endpoint;
1856 } else {
1857 ictx->rx_endpoint_intf1 = rx_endpoint;
1861 * If we didn't find a display endpoint, this is probably one of the
1862 * newer iMON devices that use control urb instead of interrupt
1864 if (!display_ep_found) {
1865 tx_control = true;
1866 display_ep_found = true;
1867 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
1868 "interface OUT endpoint\n", __func__);
1872 * Some iMON receivers have no display. Unfortunately, it seems
1873 * that SoundGraph recycles device IDs between devices both with
1874 * and without... :\
1876 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
1877 display_ep_found = false;
1878 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
1882 * iMON Touch devices have a VGA touchscreen, but no "display", as
1883 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
1885 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
1886 display_ep_found = false;
1887 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
1890 /* Input endpoint is mandatory */
1891 if (!ir_ep_found)
1892 err("%s: no valid input (IR) endpoint found.", __func__);
1894 ictx->tx_control = tx_control;
1896 if (display_ep_found)
1897 ictx->display_supported = true;
1899 return ir_ep_found;
1903 static struct imon_context *imon_init_intf0(struct usb_interface *intf)
1905 struct imon_context *ictx;
1906 struct urb *rx_urb;
1907 struct urb *tx_urb;
1908 struct device *dev = &intf->dev;
1909 struct usb_host_interface *iface_desc;
1910 int ret = -ENOMEM;
1912 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
1913 if (!ictx) {
1914 dev_err(dev, "%s: kzalloc failed for context", __func__);
1915 goto exit;
1917 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1918 if (!rx_urb) {
1919 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
1920 goto rx_urb_alloc_failed;
1922 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1923 if (!tx_urb) {
1924 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
1925 __func__);
1926 goto tx_urb_alloc_failed;
1929 mutex_init(&ictx->lock);
1931 mutex_lock(&ictx->lock);
1933 ictx->dev = dev;
1934 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
1935 ictx->dev_present_intf0 = true;
1936 ictx->rx_urb_intf0 = rx_urb;
1937 ictx->tx_urb = tx_urb;
1938 ictx->rf_device = false;
1940 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
1941 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
1943 ret = -ENODEV;
1944 iface_desc = intf->cur_altsetting;
1945 if (!imon_find_endpoints(ictx, iface_desc)) {
1946 goto find_endpoint_failed;
1949 ictx->idev = imon_init_idev(ictx);
1950 if (!ictx->idev) {
1951 dev_err(dev, "%s: input device setup failed\n", __func__);
1952 goto idev_setup_failed;
1955 ictx->rdev = imon_init_rdev(ictx);
1956 if (!ictx->rdev) {
1957 dev_err(dev, "%s: rc device setup failed\n", __func__);
1958 goto rdev_setup_failed;
1961 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
1962 usb_rcvintpipe(ictx->usbdev_intf0,
1963 ictx->rx_endpoint_intf0->bEndpointAddress),
1964 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
1965 usb_rx_callback_intf0, ictx,
1966 ictx->rx_endpoint_intf0->bInterval);
1968 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
1969 if (ret) {
1970 err("%s: usb_submit_urb failed for intf0 (%d)",
1971 __func__, ret);
1972 goto urb_submit_failed;
1975 return ictx;
1977 urb_submit_failed:
1978 ir_input_unregister(ictx->rdev);
1979 rdev_setup_failed:
1980 input_unregister_device(ictx->idev);
1981 idev_setup_failed:
1982 find_endpoint_failed:
1983 mutex_unlock(&ictx->lock);
1984 usb_free_urb(tx_urb);
1985 tx_urb_alloc_failed:
1986 usb_free_urb(rx_urb);
1987 rx_urb_alloc_failed:
1988 kfree(ictx);
1989 exit:
1990 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
1992 return NULL;
1995 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
1996 struct imon_context *ictx)
1998 struct urb *rx_urb;
1999 struct usb_host_interface *iface_desc;
2000 int ret = -ENOMEM;
2002 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2003 if (!rx_urb) {
2004 err("%s: usb_alloc_urb failed for IR urb", __func__);
2005 goto rx_urb_alloc_failed;
2008 mutex_lock(&ictx->lock);
2010 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2011 init_timer(&ictx->ttimer);
2012 ictx->ttimer.data = (unsigned long)ictx;
2013 ictx->ttimer.function = imon_touch_display_timeout;
2016 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2017 ictx->dev_present_intf1 = true;
2018 ictx->rx_urb_intf1 = rx_urb;
2020 ret = -ENODEV;
2021 iface_desc = intf->cur_altsetting;
2022 if (!imon_find_endpoints(ictx, iface_desc))
2023 goto find_endpoint_failed;
2025 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2026 ictx->touch = imon_init_touch(ictx);
2027 if (!ictx->touch)
2028 goto touch_setup_failed;
2029 } else
2030 ictx->touch = NULL;
2032 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2033 usb_rcvintpipe(ictx->usbdev_intf1,
2034 ictx->rx_endpoint_intf1->bEndpointAddress),
2035 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2036 usb_rx_callback_intf1, ictx,
2037 ictx->rx_endpoint_intf1->bInterval);
2039 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2041 if (ret) {
2042 err("%s: usb_submit_urb failed for intf1 (%d)",
2043 __func__, ret);
2044 goto urb_submit_failed;
2047 return ictx;
2049 urb_submit_failed:
2050 if (ictx->touch)
2051 input_unregister_device(ictx->touch);
2052 touch_setup_failed:
2053 find_endpoint_failed:
2054 mutex_unlock(&ictx->lock);
2055 usb_free_urb(rx_urb);
2056 rx_urb_alloc_failed:
2057 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2059 return NULL;
2063 * The 0x15c2:0xffdc device ID was used for umpteen different imon
2064 * devices, and all of them constantly spew interrupts, even when there
2065 * is no actual data to report. However, byte 6 of this buffer looks like
2066 * its unique across device variants, so we're trying to key off that to
2067 * figure out which display type (if any) and what IR protocol the device
2068 * actually supports. These devices have their IR protocol hard-coded into
2069 * their firmware, they can't be changed on the fly like the newer hardware.
2071 static void imon_get_ffdc_type(struct imon_context *ictx)
2073 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
2074 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
2075 u64 allowed_protos = IR_TYPE_OTHER;
2077 switch (ffdc_cfg_byte) {
2078 /* iMON Knob, no display, iMON IR + vol knob */
2079 case 0x21:
2080 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
2081 ictx->display_supported = false;
2082 break;
2083 /* iMON 2.4G LT (usb stick), no display, iMON RF */
2084 case 0x4e:
2085 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
2086 ictx->display_supported = false;
2087 ictx->rf_device = true;
2088 break;
2089 /* iMON VFD, no IR (does have vol knob tho) */
2090 case 0x35:
2091 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
2092 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2093 break;
2094 /* iMON VFD, iMON IR */
2095 case 0x24:
2096 case 0x85:
2097 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
2098 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2099 break;
2100 /* iMON LCD, MCE IR */
2101 case 0x9e:
2102 case 0x9f:
2103 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
2104 detected_display_type = IMON_DISPLAY_TYPE_LCD;
2105 allowed_protos = IR_TYPE_RC6;
2106 break;
2107 default:
2108 dev_info(ictx->dev, "Unknown 0xffdc device, "
2109 "defaulting to VFD and iMON IR");
2110 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2111 break;
2114 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
2116 ictx->display_type = detected_display_type;
2117 ictx->props->allowed_protos = allowed_protos;
2118 ictx->ir_type = allowed_protos;
2121 static void imon_set_display_type(struct imon_context *ictx,
2122 struct usb_interface *intf)
2124 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
2127 * Try to auto-detect the type of display if the user hasn't set
2128 * it by hand via the display_type modparam. Default is VFD.
2131 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
2132 switch (ictx->product) {
2133 case 0xffdc:
2134 /* set in imon_get_ffdc_type() */
2135 configured_display_type = ictx->display_type;
2136 break;
2137 case 0x0034:
2138 case 0x0035:
2139 configured_display_type = IMON_DISPLAY_TYPE_VGA;
2140 break;
2141 case 0x0038:
2142 case 0x0039:
2143 case 0x0045:
2144 configured_display_type = IMON_DISPLAY_TYPE_LCD;
2145 break;
2146 case 0x003c:
2147 case 0x0041:
2148 case 0x0042:
2149 case 0x0043:
2150 configured_display_type = IMON_DISPLAY_TYPE_NONE;
2151 ictx->display_supported = false;
2152 break;
2153 case 0x0036:
2154 case 0x0044:
2155 default:
2156 configured_display_type = IMON_DISPLAY_TYPE_VFD;
2157 break;
2159 } else {
2160 configured_display_type = display_type;
2161 if (display_type == IMON_DISPLAY_TYPE_NONE)
2162 ictx->display_supported = false;
2163 else
2164 ictx->display_supported = true;
2165 dev_info(ictx->dev, "%s: overriding display type to %d via "
2166 "modparam\n", __func__, display_type);
2169 ictx->display_type = configured_display_type;
2172 static void imon_init_display(struct imon_context *ictx,
2173 struct usb_interface *intf)
2175 int ret;
2177 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2179 /* set up sysfs entry for built-in clock */
2180 ret = sysfs_create_group(&intf->dev.kobj,
2181 &imon_display_attribute_group);
2182 if (ret)
2183 dev_err(ictx->dev, "Could not create display sysfs "
2184 "entries(%d)", ret);
2186 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2187 ret = usb_register_dev(intf, &imon_lcd_class);
2188 else
2189 ret = usb_register_dev(intf, &imon_vfd_class);
2190 if (ret)
2191 /* Not a fatal error, so ignore */
2192 dev_info(ictx->dev, "could not get a minor number for "
2193 "display\n");
2198 * Callback function for USB core API: Probe
2200 static int __devinit imon_probe(struct usb_interface *interface,
2201 const struct usb_device_id *id)
2203 struct usb_device *usbdev = NULL;
2204 struct usb_host_interface *iface_desc = NULL;
2205 struct usb_interface *first_if;
2206 struct device *dev = &interface->dev;
2207 int ifnum, code_length, sysfs_err;
2208 int ret = 0;
2209 struct imon_context *ictx = NULL;
2210 struct imon_context *first_if_ctx = NULL;
2211 u16 vendor, product;
2212 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
2213 0x00, 0x00, 0x00, 0x88 };
2215 code_length = BUF_CHUNK_SIZE * 8;
2217 usbdev = usb_get_dev(interface_to_usbdev(interface));
2218 iface_desc = interface->cur_altsetting;
2219 ifnum = iface_desc->desc.bInterfaceNumber;
2220 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2221 product = le16_to_cpu(usbdev->descriptor.idProduct);
2223 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2224 __func__, vendor, product, ifnum);
2226 /* prevent races probing devices w/multiple interfaces */
2227 mutex_lock(&driver_lock);
2229 first_if = usb_ifnum_to_if(usbdev, 0);
2230 first_if_ctx = (struct imon_context *)usb_get_intfdata(first_if);
2232 if (ifnum == 0) {
2233 ictx = imon_init_intf0(interface);
2234 if (!ictx) {
2235 err("%s: failed to initialize context!\n", __func__);
2236 ret = -ENODEV;
2237 goto fail;
2240 } else {
2241 /* this is the secondary interface on the device */
2242 ictx = imon_init_intf1(interface, first_if_ctx);
2243 if (!ictx) {
2244 err("%s: failed to attach to context!\n", __func__);
2245 ret = -ENODEV;
2246 goto fail;
2251 usb_set_intfdata(interface, ictx);
2253 if (ifnum == 0) {
2254 /* Enable front-panel buttons and/or knobs */
2255 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
2256 ret = send_packet(ictx);
2257 /* Not fatal, but warn about it */
2258 if (ret)
2259 dev_info(dev, "failed to enable panel buttons "
2260 "and/or knobs\n");
2262 if (product == 0xffdc)
2263 imon_get_ffdc_type(ictx);
2265 imon_set_display_type(ictx, interface);
2267 if (product == 0xffdc && ictx->rf_device) {
2268 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2269 &imon_rf_attribute_group);
2270 if (sysfs_err)
2271 err("%s: Could not create RF sysfs entries(%d)",
2272 __func__, sysfs_err);
2275 if (ictx->display_supported)
2276 imon_init_display(ictx, interface);
2279 /* set IR protocol/remote type */
2280 ret = imon_ir_change_protocol(ictx, ictx->ir_type);
2281 if (ret) {
2282 dev_warn(dev, "%s: failed to set IR protocol, falling back "
2283 "to standard iMON protocol mode\n", __func__);
2284 ictx->ir_type = IR_TYPE_OTHER;
2287 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2288 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2289 usbdev->bus->busnum, usbdev->devnum);
2291 mutex_unlock(&ictx->lock);
2292 mutex_unlock(&driver_lock);
2294 return 0;
2296 fail:
2297 mutex_unlock(&driver_lock);
2298 dev_err(dev, "unable to register, err %d\n", ret);
2300 return ret;
2304 * Callback function for USB core API: disconnect
2306 static void __devexit imon_disconnect(struct usb_interface *interface)
2308 struct imon_context *ictx;
2309 struct device *dev;
2310 int ifnum;
2312 /* prevent races with multi-interface device probing and display_open */
2313 mutex_lock(&driver_lock);
2315 ictx = usb_get_intfdata(interface);
2316 dev = ictx->dev;
2317 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2319 mutex_lock(&ictx->lock);
2322 * sysfs_remove_group is safe to call even if sysfs_create_group
2323 * hasn't been called
2325 sysfs_remove_group(&interface->dev.kobj,
2326 &imon_display_attribute_group);
2327 sysfs_remove_group(&interface->dev.kobj,
2328 &imon_rf_attribute_group);
2330 usb_set_intfdata(interface, NULL);
2332 /* Abort ongoing write */
2333 if (ictx->tx.busy) {
2334 usb_kill_urb(ictx->tx_urb);
2335 complete_all(&ictx->tx.finished);
2338 if (ifnum == 0) {
2339 ictx->dev_present_intf0 = false;
2340 usb_kill_urb(ictx->rx_urb_intf0);
2341 input_unregister_device(ictx->idev);
2342 ir_input_unregister(ictx->rdev);
2343 if (ictx->display_supported) {
2344 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2345 usb_deregister_dev(interface, &imon_lcd_class);
2346 else
2347 usb_deregister_dev(interface, &imon_vfd_class);
2349 } else {
2350 ictx->dev_present_intf1 = false;
2351 usb_kill_urb(ictx->rx_urb_intf1);
2352 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2353 input_unregister_device(ictx->touch);
2356 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1) {
2357 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2358 del_timer_sync(&ictx->ttimer);
2359 mutex_unlock(&ictx->lock);
2360 if (!ictx->display_isopen)
2361 free_imon_context(ictx);
2362 } else
2363 mutex_unlock(&ictx->lock);
2365 mutex_unlock(&driver_lock);
2367 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2368 __func__, ifnum);
2371 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2373 struct imon_context *ictx = usb_get_intfdata(intf);
2374 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2376 if (ifnum == 0)
2377 usb_kill_urb(ictx->rx_urb_intf0);
2378 else
2379 usb_kill_urb(ictx->rx_urb_intf1);
2381 return 0;
2384 static int imon_resume(struct usb_interface *intf)
2386 int rc = 0;
2387 struct imon_context *ictx = usb_get_intfdata(intf);
2388 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2390 if (ifnum == 0) {
2391 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2392 usb_rcvintpipe(ictx->usbdev_intf0,
2393 ictx->rx_endpoint_intf0->bEndpointAddress),
2394 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2395 usb_rx_callback_intf0, ictx,
2396 ictx->rx_endpoint_intf0->bInterval);
2398 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2400 } else {
2401 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2402 usb_rcvintpipe(ictx->usbdev_intf1,
2403 ictx->rx_endpoint_intf1->bEndpointAddress),
2404 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2405 usb_rx_callback_intf1, ictx,
2406 ictx->rx_endpoint_intf1->bInterval);
2408 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2411 return rc;
2414 static int __init imon_init(void)
2416 int rc;
2418 rc = usb_register(&imon_driver);
2419 if (rc) {
2420 err("%s: usb register failed(%d)", __func__, rc);
2421 rc = -ENODEV;
2424 return rc;
2427 static void __exit imon_exit(void)
2429 usb_deregister(&imon_driver);
2432 module_init(imon_init);
2433 module_exit(imon_exit);