imon: add conditional locking in change_protocol
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / rc / imon.c
blobeee39570f23db711af66f272be242981c2d1e39c
1 /*
2 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
4 * Copyright(C) 2010 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 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
38 #include <linux/input.h>
39 #include <linux/usb.h>
40 #include <linux/usb/input.h>
41 #include <media/rc-core.h>
43 #include <linux/time.h>
44 #include <linux/timer.h>
46 #define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
47 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
48 #define MOD_NAME "imon"
49 #define MOD_VERSION "0.9.3"
51 #define DISPLAY_MINOR_BASE 144
52 #define DEVICE_NAME "lcd%d"
54 #define BUF_CHUNK_SIZE 8
55 #define BUF_SIZE 128
57 #define BIT_DURATION 250 /* each bit received is 250us */
59 #define IMON_CLOCK_ENABLE_PACKETS 2
61 /*** P R O T O T Y P E S ***/
63 /* USB Callback prototypes */
64 static int imon_probe(struct usb_interface *interface,
65 const struct usb_device_id *id);
66 static void imon_disconnect(struct usb_interface *interface);
67 static void usb_rx_callback_intf0(struct urb *urb);
68 static void usb_rx_callback_intf1(struct urb *urb);
69 static void usb_tx_callback(struct urb *urb);
71 /* suspend/resume support */
72 static int imon_resume(struct usb_interface *intf);
73 static int imon_suspend(struct usb_interface *intf, pm_message_t message);
75 /* Display file_operations function prototypes */
76 static int display_open(struct inode *inode, struct file *file);
77 static int display_close(struct inode *inode, struct file *file);
79 /* VFD write operation */
80 static ssize_t vfd_write(struct file *file, const char *buf,
81 size_t n_bytes, loff_t *pos);
83 /* LCD file_operations override function prototypes */
84 static ssize_t lcd_write(struct file *file, const char *buf,
85 size_t n_bytes, loff_t *pos);
87 /*** G L O B A L S ***/
89 struct imon_context {
90 struct device *dev;
91 /* Newer devices have two interfaces */
92 struct usb_device *usbdev_intf0;
93 struct usb_device *usbdev_intf1;
95 bool display_supported; /* not all controllers do */
96 bool display_isopen; /* display port has been opened */
97 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
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;
110 struct urb *tx_urb;
111 bool tx_control;
112 unsigned char usb_rx_buf[8];
113 unsigned char usb_tx_buf[8];
115 struct tx_t {
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 */
120 } tx;
122 u16 vendor; /* usb vendor ID */
123 u16 product; /* usb product ID */
125 struct rc_dev *rdev; /* rc-core device for remote */
126 struct input_dev *idev; /* input device for panel & IR mouse */
127 struct input_dev *touch; /* input device for touchscreen */
129 spinlock_t kc_lock; /* make sure we get keycodes right */
130 u32 kc; /* current input keycode */
131 u32 last_keycode; /* last reported input keycode */
132 u32 rc_scancode; /* the computed remote scancode */
133 u8 rc_toggle; /* the computed remote toggle bit */
134 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
135 bool release_code; /* some keys send a release code */
137 u8 display_type; /* store the display type */
138 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
140 char name_rdev[128]; /* rc input device name */
141 char phys_rdev[64]; /* rc input device phys path */
143 char name_idev[128]; /* input device name */
144 char phys_idev[64]; /* input device phys path */
146 char name_touch[128]; /* touch screen name */
147 char phys_touch[64]; /* touch screen phys path */
148 struct timer_list ttimer; /* touch screen timer */
149 int touch_x; /* x coordinate on touchscreen */
150 int touch_y; /* y coordinate on touchscreen */
153 #define TOUCH_TIMEOUT (HZ/30)
155 /* vfd character device file operations */
156 static const struct file_operations vfd_fops = {
157 .owner = THIS_MODULE,
158 .open = &display_open,
159 .write = &vfd_write,
160 .release = &display_close,
161 .llseek = noop_llseek,
164 /* lcd character device file operations */
165 static const struct file_operations lcd_fops = {
166 .owner = THIS_MODULE,
167 .open = &display_open,
168 .write = &lcd_write,
169 .release = &display_close,
170 .llseek = noop_llseek,
173 enum {
174 IMON_DISPLAY_TYPE_AUTO = 0,
175 IMON_DISPLAY_TYPE_VFD = 1,
176 IMON_DISPLAY_TYPE_LCD = 2,
177 IMON_DISPLAY_TYPE_VGA = 3,
178 IMON_DISPLAY_TYPE_NONE = 4,
181 enum {
182 IMON_KEY_IMON = 0,
183 IMON_KEY_MCE = 1,
184 IMON_KEY_PANEL = 2,
188 * USB Device ID for iMON USB Control Boards
190 * The Windows drivers contain 6 different inf files, more or less one for
191 * each new device until the 0x0034-0x0046 devices, which all use the same
192 * driver. Some of the devices in the 34-46 range haven't been definitively
193 * identified yet. Early devices have either a TriGem Computer, Inc. or a
194 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
195 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
196 * the ffdc and later devices, which do onboard decoding.
198 static struct usb_device_id imon_usb_id_table[] = {
200 * Several devices with this same device ID, all use iMON_PAD.inf
201 * SoundGraph iMON PAD (IR & VFD)
202 * SoundGraph iMON PAD (IR & LCD)
203 * SoundGraph iMON Knob (IR only)
205 { USB_DEVICE(0x15c2, 0xffdc) },
208 * Newer devices, all driven by the latest iMON Windows driver, full
209 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
210 * Need user input to fill in details on unknown devices.
212 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
213 { USB_DEVICE(0x15c2, 0x0034) },
214 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
215 { USB_DEVICE(0x15c2, 0x0035) },
216 /* SoundGraph iMON OEM VFD (IR & VFD) */
217 { USB_DEVICE(0x15c2, 0x0036) },
218 /* device specifics unknown */
219 { USB_DEVICE(0x15c2, 0x0037) },
220 /* SoundGraph iMON OEM LCD (IR & LCD) */
221 { USB_DEVICE(0x15c2, 0x0038) },
222 /* SoundGraph iMON UltraBay (IR & LCD) */
223 { USB_DEVICE(0x15c2, 0x0039) },
224 /* device specifics unknown */
225 { USB_DEVICE(0x15c2, 0x003a) },
226 /* device specifics unknown */
227 { USB_DEVICE(0x15c2, 0x003b) },
228 /* SoundGraph iMON OEM Inside (IR only) */
229 { USB_DEVICE(0x15c2, 0x003c) },
230 /* device specifics unknown */
231 { USB_DEVICE(0x15c2, 0x003d) },
232 /* device specifics unknown */
233 { USB_DEVICE(0x15c2, 0x003e) },
234 /* device specifics unknown */
235 { USB_DEVICE(0x15c2, 0x003f) },
236 /* device specifics unknown */
237 { USB_DEVICE(0x15c2, 0x0040) },
238 /* SoundGraph iMON MINI (IR only) */
239 { USB_DEVICE(0x15c2, 0x0041) },
240 /* Antec Veris Multimedia Station EZ External (IR only) */
241 { USB_DEVICE(0x15c2, 0x0042) },
242 /* Antec Veris Multimedia Station Basic Internal (IR only) */
243 { USB_DEVICE(0x15c2, 0x0043) },
244 /* Antec Veris Multimedia Station Elite (IR & VFD) */
245 { USB_DEVICE(0x15c2, 0x0044) },
246 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
247 { USB_DEVICE(0x15c2, 0x0045) },
248 /* device specifics unknown */
249 { USB_DEVICE(0x15c2, 0x0046) },
253 /* USB Device data */
254 static struct usb_driver imon_driver = {
255 .name = MOD_NAME,
256 .probe = imon_probe,
257 .disconnect = imon_disconnect,
258 .suspend = imon_suspend,
259 .resume = imon_resume,
260 .id_table = imon_usb_id_table,
263 static struct usb_class_driver imon_vfd_class = {
264 .name = DEVICE_NAME,
265 .fops = &vfd_fops,
266 .minor_base = DISPLAY_MINOR_BASE,
269 static struct usb_class_driver imon_lcd_class = {
270 .name = DEVICE_NAME,
271 .fops = &lcd_fops,
272 .minor_base = DISPLAY_MINOR_BASE,
275 /* imon receiver front panel/knob key table */
276 static const struct {
277 u64 hw_code;
278 u32 keycode;
279 } imon_panel_key_table[] = {
280 { 0x000000000f00ffeell, KEY_PROG1 }, /* Go */
281 { 0x000000001f00ffeell, KEY_AUDIO },
282 { 0x000000002000ffeell, KEY_VIDEO },
283 { 0x000000002100ffeell, KEY_CAMERA },
284 { 0x000000002700ffeell, KEY_DVD },
285 { 0x000000002300ffeell, KEY_TV },
286 { 0x000000000500ffeell, KEY_PREVIOUS },
287 { 0x000000000700ffeell, KEY_REWIND },
288 { 0x000000000400ffeell, KEY_STOP },
289 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
290 { 0x000000000800ffeell, KEY_FASTFORWARD },
291 { 0x000000000600ffeell, KEY_NEXT },
292 { 0x000000010000ffeell, KEY_RIGHT },
293 { 0x000001000000ffeell, KEY_LEFT },
294 { 0x000000003d00ffeell, KEY_SELECT },
295 { 0x000100000000ffeell, KEY_VOLUMEUP },
296 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
297 { 0x000000000100ffeell, KEY_MUTE },
298 /* 0xffdc iMON MCE VFD */
299 { 0x00010000ffffffeell, KEY_VOLUMEUP },
300 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
301 /* iMON Knob values */
302 { 0x000100ffffffffeell, KEY_VOLUMEUP },
303 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
304 { 0x000008ffffffffeell, KEY_MUTE },
307 /* to prevent races between open() and disconnect(), probing, etc */
308 static DEFINE_MUTEX(driver_lock);
310 /* Module bookkeeping bits */
311 MODULE_AUTHOR(MOD_AUTHOR);
312 MODULE_DESCRIPTION(MOD_DESC);
313 MODULE_VERSION(MOD_VERSION);
314 MODULE_LICENSE("GPL");
315 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
317 static bool debug;
318 module_param(debug, bool, S_IRUGO | S_IWUSR);
319 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
321 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
322 static int display_type;
323 module_param(display_type, int, S_IRUGO);
324 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
325 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
327 static int pad_stabilize = 1;
328 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
329 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
330 "presses in arrow key mode. 0=disable, 1=enable (default).");
333 * In certain use cases, mouse mode isn't really helpful, and could actually
334 * cause confusion, so allow disabling it when the IR device is open.
336 static bool nomouse;
337 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
338 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
339 "open. 0=don't disable, 1=disable. (default: don't disable)");
341 /* threshold at which a pad push registers as an arrow key in kbd mode */
342 static int pad_thresh;
343 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
344 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
345 "arrow key in kbd mode (default: 28)");
348 static void free_imon_context(struct imon_context *ictx)
350 struct device *dev = ictx->dev;
352 usb_free_urb(ictx->tx_urb);
353 usb_free_urb(ictx->rx_urb_intf0);
354 usb_free_urb(ictx->rx_urb_intf1);
355 kfree(ictx);
357 dev_dbg(dev, "%s: iMON context freed\n", __func__);
361 * Called when the Display device (e.g. /dev/lcd0)
362 * is opened by the application.
364 static int display_open(struct inode *inode, struct file *file)
366 struct usb_interface *interface;
367 struct imon_context *ictx = NULL;
368 int subminor;
369 int retval = 0;
371 /* prevent races with disconnect */
372 mutex_lock(&driver_lock);
374 subminor = iminor(inode);
375 interface = usb_find_interface(&imon_driver, subminor);
376 if (!interface) {
377 pr_err("could not find interface for minor %d\n", subminor);
378 retval = -ENODEV;
379 goto exit;
381 ictx = usb_get_intfdata(interface);
383 if (!ictx) {
384 pr_err("no context found for minor %d\n", subminor);
385 retval = -ENODEV;
386 goto exit;
389 mutex_lock(&ictx->lock);
391 if (!ictx->display_supported) {
392 pr_err("display not supported by device\n");
393 retval = -ENODEV;
394 } else if (ictx->display_isopen) {
395 pr_err("display port is already open\n");
396 retval = -EBUSY;
397 } else {
398 ictx->display_isopen = true;
399 file->private_data = ictx;
400 dev_dbg(ictx->dev, "display port opened\n");
403 mutex_unlock(&ictx->lock);
405 exit:
406 mutex_unlock(&driver_lock);
407 return retval;
411 * Called when the display device (e.g. /dev/lcd0)
412 * is closed by the application.
414 static int display_close(struct inode *inode, struct file *file)
416 struct imon_context *ictx = NULL;
417 int retval = 0;
419 ictx = file->private_data;
421 if (!ictx) {
422 pr_err("no context for device\n");
423 return -ENODEV;
426 mutex_lock(&ictx->lock);
428 if (!ictx->display_supported) {
429 pr_err("display not supported by device\n");
430 retval = -ENODEV;
431 } else if (!ictx->display_isopen) {
432 pr_err("display is not open\n");
433 retval = -EIO;
434 } else {
435 ictx->display_isopen = false;
436 dev_dbg(ictx->dev, "display port closed\n");
437 if (!ictx->dev_present_intf0) {
439 * Device disconnected before close and IR port is not
440 * open. If IR port is open, context will be deleted by
441 * ir_close.
443 mutex_unlock(&ictx->lock);
444 free_imon_context(ictx);
445 return retval;
449 mutex_unlock(&ictx->lock);
450 return retval;
454 * Sends a packet to the device -- this function must be called with
455 * ictx->lock held, or its unlock/lock sequence while waiting for tx
456 * to complete can/will lead to a deadlock.
458 static int send_packet(struct imon_context *ictx)
460 unsigned int pipe;
461 unsigned long timeout;
462 int interval = 0;
463 int retval = 0;
464 struct usb_ctrlrequest *control_req = NULL;
466 /* Check if we need to use control or interrupt urb */
467 if (!ictx->tx_control) {
468 pipe = usb_sndintpipe(ictx->usbdev_intf0,
469 ictx->tx_endpoint->bEndpointAddress);
470 interval = ictx->tx_endpoint->bInterval;
472 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
473 ictx->usb_tx_buf,
474 sizeof(ictx->usb_tx_buf),
475 usb_tx_callback, ictx, interval);
477 ictx->tx_urb->actual_length = 0;
478 } else {
479 /* fill request into kmalloc'ed space: */
480 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
481 GFP_KERNEL);
482 if (control_req == NULL)
483 return -ENOMEM;
485 /* setup packet is '21 09 0200 0001 0008' */
486 control_req->bRequestType = 0x21;
487 control_req->bRequest = 0x09;
488 control_req->wValue = cpu_to_le16(0x0200);
489 control_req->wIndex = cpu_to_le16(0x0001);
490 control_req->wLength = cpu_to_le16(0x0008);
492 /* control pipe is endpoint 0x00 */
493 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
495 /* build the control urb */
496 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
497 pipe, (unsigned char *)control_req,
498 ictx->usb_tx_buf,
499 sizeof(ictx->usb_tx_buf),
500 usb_tx_callback, ictx);
501 ictx->tx_urb->actual_length = 0;
504 init_completion(&ictx->tx.finished);
505 ictx->tx.busy = true;
506 smp_rmb(); /* ensure later readers know we're busy */
508 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
509 if (retval) {
510 ictx->tx.busy = false;
511 smp_rmb(); /* ensure later readers know we're not busy */
512 pr_err("error submitting urb(%d)\n", retval);
513 } else {
514 /* Wait for transmission to complete (or abort) */
515 mutex_unlock(&ictx->lock);
516 retval = wait_for_completion_interruptible(
517 &ictx->tx.finished);
518 if (retval)
519 pr_err("task interrupted\n");
520 mutex_lock(&ictx->lock);
522 retval = ictx->tx.status;
523 if (retval)
524 pr_err("packet tx failed (%d)\n", retval);
527 kfree(control_req);
530 * Induce a mandatory 5ms delay before returning, as otherwise,
531 * send_packet can get called so rapidly as to overwhelm the device,
532 * particularly on faster systems and/or those with quirky usb.
534 timeout = msecs_to_jiffies(5);
535 set_current_state(TASK_UNINTERRUPTIBLE);
536 schedule_timeout(timeout);
538 return retval;
542 * Sends an associate packet to the iMON 2.4G.
544 * This might not be such a good idea, since it has an id collision with
545 * some versions of the "IR & VFD" combo. The only way to determine if it
546 * is an RF version is to look at the product description string. (Which
547 * we currently do not fetch).
549 static int send_associate_24g(struct imon_context *ictx)
551 int retval;
552 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
553 0x00, 0x00, 0x00, 0x20 };
555 if (!ictx) {
556 pr_err("no context for device\n");
557 return -ENODEV;
560 if (!ictx->dev_present_intf0) {
561 pr_err("no iMON device present\n");
562 return -ENODEV;
565 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
566 retval = send_packet(ictx);
568 return retval;
572 * Sends packets to setup and show clock on iMON display
574 * Arguments: year - last 2 digits of year, month - 1..12,
575 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
576 * hour - 0..23, minute - 0..59, second - 0..59
578 static int send_set_imon_clock(struct imon_context *ictx,
579 unsigned int year, unsigned int month,
580 unsigned int day, unsigned int dow,
581 unsigned int hour, unsigned int minute,
582 unsigned int second)
584 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
585 int retval = 0;
586 int i;
588 if (!ictx) {
589 pr_err("no context for device\n");
590 return -ENODEV;
593 switch (ictx->display_type) {
594 case IMON_DISPLAY_TYPE_LCD:
595 clock_enable_pkt[0][0] = 0x80;
596 clock_enable_pkt[0][1] = year;
597 clock_enable_pkt[0][2] = month-1;
598 clock_enable_pkt[0][3] = day;
599 clock_enable_pkt[0][4] = hour;
600 clock_enable_pkt[0][5] = minute;
601 clock_enable_pkt[0][6] = second;
603 clock_enable_pkt[1][0] = 0x80;
604 clock_enable_pkt[1][1] = 0;
605 clock_enable_pkt[1][2] = 0;
606 clock_enable_pkt[1][3] = 0;
607 clock_enable_pkt[1][4] = 0;
608 clock_enable_pkt[1][5] = 0;
609 clock_enable_pkt[1][6] = 0;
611 if (ictx->product == 0xffdc) {
612 clock_enable_pkt[0][7] = 0x50;
613 clock_enable_pkt[1][7] = 0x51;
614 } else {
615 clock_enable_pkt[0][7] = 0x88;
616 clock_enable_pkt[1][7] = 0x8a;
619 break;
621 case IMON_DISPLAY_TYPE_VFD:
622 clock_enable_pkt[0][0] = year;
623 clock_enable_pkt[0][1] = month-1;
624 clock_enable_pkt[0][2] = day;
625 clock_enable_pkt[0][3] = dow;
626 clock_enable_pkt[0][4] = hour;
627 clock_enable_pkt[0][5] = minute;
628 clock_enable_pkt[0][6] = second;
629 clock_enable_pkt[0][7] = 0x40;
631 clock_enable_pkt[1][0] = 0;
632 clock_enable_pkt[1][1] = 0;
633 clock_enable_pkt[1][2] = 1;
634 clock_enable_pkt[1][3] = 0;
635 clock_enable_pkt[1][4] = 0;
636 clock_enable_pkt[1][5] = 0;
637 clock_enable_pkt[1][6] = 0;
638 clock_enable_pkt[1][7] = 0x42;
640 break;
642 default:
643 return -ENODEV;
646 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
647 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
648 retval = send_packet(ictx);
649 if (retval) {
650 pr_err("send_packet failed for packet %d\n", i);
651 break;
655 return retval;
659 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
661 static ssize_t show_associate_remote(struct device *d,
662 struct device_attribute *attr,
663 char *buf)
665 struct imon_context *ictx = dev_get_drvdata(d);
667 if (!ictx)
668 return -ENODEV;
670 mutex_lock(&ictx->lock);
671 if (ictx->rf_isassociating)
672 strcpy(buf, "associating\n");
673 else
674 strcpy(buf, "closed\n");
676 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
677 "instructions on how to associate your iMON 2.4G DT/LT "
678 "remote\n");
679 mutex_unlock(&ictx->lock);
680 return strlen(buf);
683 static ssize_t store_associate_remote(struct device *d,
684 struct device_attribute *attr,
685 const char *buf, size_t count)
687 struct imon_context *ictx;
689 ictx = dev_get_drvdata(d);
691 if (!ictx)
692 return -ENODEV;
694 mutex_lock(&ictx->lock);
695 ictx->rf_isassociating = true;
696 send_associate_24g(ictx);
697 mutex_unlock(&ictx->lock);
699 return count;
703 * sysfs functions to control internal imon clock
705 static ssize_t show_imon_clock(struct device *d,
706 struct device_attribute *attr, char *buf)
708 struct imon_context *ictx = dev_get_drvdata(d);
709 size_t len;
711 if (!ictx)
712 return -ENODEV;
714 mutex_lock(&ictx->lock);
716 if (!ictx->display_supported) {
717 len = snprintf(buf, PAGE_SIZE, "Not supported.");
718 } else {
719 len = snprintf(buf, PAGE_SIZE,
720 "To set the clock on your iMON display:\n"
721 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
722 "%s", ictx->display_isopen ?
723 "\nNOTE: imon device must be closed\n" : "");
726 mutex_unlock(&ictx->lock);
728 return len;
731 static ssize_t store_imon_clock(struct device *d,
732 struct device_attribute *attr,
733 const char *buf, size_t count)
735 struct imon_context *ictx = dev_get_drvdata(d);
736 ssize_t retval;
737 unsigned int year, month, day, dow, hour, minute, second;
739 if (!ictx)
740 return -ENODEV;
742 mutex_lock(&ictx->lock);
744 if (!ictx->display_supported) {
745 retval = -ENODEV;
746 goto exit;
747 } else if (ictx->display_isopen) {
748 retval = -EBUSY;
749 goto exit;
752 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
753 &hour, &minute, &second) != 7) {
754 retval = -EINVAL;
755 goto exit;
758 if ((month < 1 || month > 12) ||
759 (day < 1 || day > 31) || (dow > 6) ||
760 (hour > 23) || (minute > 59) || (second > 59)) {
761 retval = -EINVAL;
762 goto exit;
765 retval = send_set_imon_clock(ictx, year, month, day, dow,
766 hour, minute, second);
767 if (retval)
768 goto exit;
770 retval = count;
771 exit:
772 mutex_unlock(&ictx->lock);
774 return retval;
778 static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
779 store_imon_clock);
781 static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
782 store_associate_remote);
784 static struct attribute *imon_display_sysfs_entries[] = {
785 &dev_attr_imon_clock.attr,
786 NULL
789 static struct attribute_group imon_display_attr_group = {
790 .attrs = imon_display_sysfs_entries
793 static struct attribute *imon_rf_sysfs_entries[] = {
794 &dev_attr_associate_remote.attr,
795 NULL
798 static struct attribute_group imon_rf_attr_group = {
799 .attrs = imon_rf_sysfs_entries
803 * Writes data to the VFD. The iMON VFD is 2x16 characters
804 * and requires data in 5 consecutive USB interrupt packets,
805 * each packet but the last carrying 7 bytes.
807 * I don't know if the VFD board supports features such as
808 * scrolling, clearing rows, blanking, etc. so at
809 * the caller must provide a full screen of data. If fewer
810 * than 32 bytes are provided spaces will be appended to
811 * generate a full screen.
813 static ssize_t vfd_write(struct file *file, const char *buf,
814 size_t n_bytes, loff_t *pos)
816 int i;
817 int offset;
818 int seq;
819 int retval = 0;
820 struct imon_context *ictx;
821 const unsigned char vfd_packet6[] = {
822 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
824 ictx = file->private_data;
825 if (!ictx) {
826 pr_err("no context for device\n");
827 return -ENODEV;
830 mutex_lock(&ictx->lock);
832 if (!ictx->dev_present_intf0) {
833 pr_err("no iMON device present\n");
834 retval = -ENODEV;
835 goto exit;
838 if (n_bytes <= 0 || n_bytes > 32) {
839 pr_err("invalid payload size\n");
840 retval = -EINVAL;
841 goto exit;
844 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
845 retval = -EFAULT;
846 goto exit;
849 /* Pad with spaces */
850 for (i = n_bytes; i < 32; ++i)
851 ictx->tx.data_buf[i] = ' ';
853 for (i = 32; i < 35; ++i)
854 ictx->tx.data_buf[i] = 0xFF;
856 offset = 0;
857 seq = 0;
859 do {
860 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
861 ictx->usb_tx_buf[7] = (unsigned char) seq;
863 retval = send_packet(ictx);
864 if (retval) {
865 pr_err("send packet failed for packet #%d\n", seq / 2);
866 goto exit;
867 } else {
868 seq += 2;
869 offset += 7;
872 } while (offset < 35);
874 /* Send packet #6 */
875 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
876 ictx->usb_tx_buf[7] = (unsigned char) seq;
877 retval = send_packet(ictx);
878 if (retval)
879 pr_err("send packet failed for packet #%d\n", seq / 2);
881 exit:
882 mutex_unlock(&ictx->lock);
884 return (!retval) ? n_bytes : retval;
888 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
889 * packets. We accept data as 16 hexadecimal digits, followed by a
890 * newline (to make it easy to drive the device from a command-line
891 * -- even though the actual binary data is a bit complicated).
893 * The device itself is not a "traditional" text-mode display. It's
894 * actually a 16x96 pixel bitmap display. That means if you want to
895 * display text, you've got to have your own "font" and translate the
896 * text into bitmaps for display. This is really flexible (you can
897 * display whatever diacritics you need, and so on), but it's also
898 * a lot more complicated than most LCDs...
900 static ssize_t lcd_write(struct file *file, const char *buf,
901 size_t n_bytes, loff_t *pos)
903 int retval = 0;
904 struct imon_context *ictx;
906 ictx = file->private_data;
907 if (!ictx) {
908 pr_err("no context for device\n");
909 return -ENODEV;
912 mutex_lock(&ictx->lock);
914 if (!ictx->display_supported) {
915 pr_err("no iMON display present\n");
916 retval = -ENODEV;
917 goto exit;
920 if (n_bytes != 8) {
921 pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
922 retval = -EINVAL;
923 goto exit;
926 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
927 retval = -EFAULT;
928 goto exit;
931 retval = send_packet(ictx);
932 if (retval) {
933 pr_err("send packet failed!\n");
934 goto exit;
935 } else {
936 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
937 __func__, (int) n_bytes);
939 exit:
940 mutex_unlock(&ictx->lock);
941 return (!retval) ? n_bytes : retval;
945 * Callback function for USB core API: transmit data
947 static void usb_tx_callback(struct urb *urb)
949 struct imon_context *ictx;
951 if (!urb)
952 return;
953 ictx = (struct imon_context *)urb->context;
954 if (!ictx)
955 return;
957 ictx->tx.status = urb->status;
959 /* notify waiters that write has finished */
960 ictx->tx.busy = false;
961 smp_rmb(); /* ensure later readers know we're not busy */
962 complete(&ictx->tx.finished);
966 * report touchscreen input
968 static void imon_touch_display_timeout(unsigned long data)
970 struct imon_context *ictx = (struct imon_context *)data;
972 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
973 return;
975 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
976 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
977 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
978 input_sync(ictx->touch);
982 * iMON IR receivers support two different signal sets -- those used by
983 * the iMON remotes, and those used by the Windows MCE remotes (which is
984 * really just RC-6), but only one or the other at a time, as the signals
985 * are decoded onboard the receiver.
987 * This function gets called two different ways, one way is from
988 * rc_register_device, for initial protocol selection/setup, and the other is
989 * via a userspace-initiated protocol change request, either by direct sysfs
990 * prodding or by something like ir-keytable. In the rc_register_device case,
991 * the imon context lock is already held, but when initiated from userspace,
992 * it is not, so we must acquire it prior to calling send_packet, which
993 * requires that the lock is held.
995 static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
997 int retval;
998 struct imon_context *ictx = rc->priv;
999 struct device *dev = ictx->dev;
1000 bool unlock = false;
1001 unsigned char ir_proto_packet[] = {
1002 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1004 if (rc_type && !(rc_type & rc->allowed_protos))
1005 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1006 "this device does not support\n");
1008 switch (rc_type) {
1009 case RC_TYPE_RC6:
1010 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1011 ir_proto_packet[0] = 0x01;
1012 break;
1013 case RC_TYPE_UNKNOWN:
1014 case RC_TYPE_OTHER:
1015 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1016 if (!pad_stabilize)
1017 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1018 /* ir_proto_packet[0] = 0x00; // already the default */
1019 rc_type = RC_TYPE_OTHER;
1020 break;
1021 default:
1022 dev_warn(dev, "Unsupported IR protocol specified, overriding "
1023 "to iMON IR protocol\n");
1024 if (!pad_stabilize)
1025 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1026 /* ir_proto_packet[0] = 0x00; // already the default */
1027 rc_type = RC_TYPE_OTHER;
1028 break;
1031 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1033 if (!mutex_is_locked(&ictx->lock)) {
1034 unlock = true;
1035 mutex_lock(&ictx->lock);
1038 retval = send_packet(ictx);
1039 if (retval)
1040 goto out;
1042 ictx->rc_type = rc_type;
1043 ictx->pad_mouse = false;
1045 out:
1046 if (unlock)
1047 mutex_unlock(&ictx->lock);
1049 return retval;
1052 static inline int tv2int(const struct timeval *a, const struct timeval *b)
1054 int usecs = 0;
1055 int sec = 0;
1057 if (b->tv_usec > a->tv_usec) {
1058 usecs = 1000000;
1059 sec--;
1062 usecs += a->tv_usec - b->tv_usec;
1064 sec += a->tv_sec - b->tv_sec;
1065 sec *= 1000;
1066 usecs /= 1000;
1067 sec += usecs;
1069 if (sec < 0)
1070 sec = 1000;
1072 return sec;
1076 * The directional pad behaves a bit differently, depending on whether this is
1077 * one of the older ffdc devices or a newer device. Newer devices appear to
1078 * have a higher resolution matrix for more precise mouse movement, but it
1079 * makes things overly sensitive in keyboard mode, so we do some interesting
1080 * contortions to make it less touchy. Older devices run through the same
1081 * routine with shorter timeout and a smaller threshold.
1083 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1085 struct timeval ct;
1086 static struct timeval prev_time = {0, 0};
1087 static struct timeval hit_time = {0, 0};
1088 static int x, y, prev_result, hits;
1089 int result = 0;
1090 int msec, msec_hit;
1092 do_gettimeofday(&ct);
1093 msec = tv2int(&ct, &prev_time);
1094 msec_hit = tv2int(&ct, &hit_time);
1096 if (msec > 100) {
1097 x = 0;
1098 y = 0;
1099 hits = 0;
1102 x += a;
1103 y += b;
1105 prev_time = ct;
1107 if (abs(x) > threshold || abs(y) > threshold) {
1108 if (abs(y) > abs(x))
1109 result = (y > 0) ? 0x7F : 0x80;
1110 else
1111 result = (x > 0) ? 0x7F00 : 0x8000;
1113 x = 0;
1114 y = 0;
1116 if (result == prev_result) {
1117 hits++;
1119 if (hits > 3) {
1120 switch (result) {
1121 case 0x7F:
1122 y = 17 * threshold / 30;
1123 break;
1124 case 0x80:
1125 y -= 17 * threshold / 30;
1126 break;
1127 case 0x7F00:
1128 x = 17 * threshold / 30;
1129 break;
1130 case 0x8000:
1131 x -= 17 * threshold / 30;
1132 break;
1136 if (hits == 2 && msec_hit < timeout) {
1137 result = 0;
1138 hits = 1;
1140 } else {
1141 prev_result = result;
1142 hits = 1;
1143 hit_time = ct;
1147 return result;
1150 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1152 u32 keycode;
1153 u32 release;
1154 bool is_release_code = false;
1156 /* Look for the initial press of a button */
1157 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1158 ictx->rc_toggle = 0x0;
1159 ictx->rc_scancode = scancode;
1161 /* Look for the release of a button */
1162 if (keycode == KEY_RESERVED) {
1163 release = scancode & ~0x4000;
1164 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1165 if (keycode != KEY_RESERVED)
1166 is_release_code = true;
1169 ictx->release_code = is_release_code;
1171 return keycode;
1174 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1176 u32 keycode;
1178 #define MCE_KEY_MASK 0x7000
1179 #define MCE_TOGGLE_BIT 0x8000
1182 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1183 * (the toggle bit flipping between alternating key presses), while
1184 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1185 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1186 * but we can't or them into all codes, as some keys are decoded in
1187 * a different way w/o the same use of the toggle bit...
1189 if (scancode & 0x80000000)
1190 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1192 ictx->rc_scancode = scancode;
1193 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1195 /* not used in mce mode, but make sure we know its false */
1196 ictx->release_code = false;
1198 return keycode;
1201 static u32 imon_panel_key_lookup(u64 code)
1203 int i;
1204 u32 keycode = KEY_RESERVED;
1206 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1207 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1208 keycode = imon_panel_key_table[i].keycode;
1209 break;
1213 return 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;
1220 u8 right_shift = 1;
1221 bool mouse_input = true;
1222 int dir = 0;
1223 unsigned long flags;
1225 spin_lock_irqsave(&ictx->kc_lock, flags);
1227 /* newer iMON device PAD or mouse button */
1228 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1229 rel_x = buf[2];
1230 rel_y = buf[3];
1231 right_shift = 1;
1232 /* 0xffdc iMON PAD or mouse button input */
1233 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1234 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1235 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1236 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1237 if (buf[0] & 0x02)
1238 rel_x |= ~0x0f;
1239 rel_x = rel_x + rel_x / 2;
1240 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1241 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1242 if (buf[0] & 0x01)
1243 rel_y |= ~0x0f;
1244 rel_y = rel_y + rel_y / 2;
1245 right_shift = 2;
1246 /* some ffdc devices decode mouse buttons differently... */
1247 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1248 right_shift = 2;
1249 /* ch+/- buttons, which we use for an emulated scroll wheel */
1250 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1251 dir = 1;
1252 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1253 dir = -1;
1254 } else
1255 mouse_input = false;
1257 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1259 if (mouse_input) {
1260 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1262 if (dir) {
1263 input_report_rel(ictx->idev, REL_WHEEL, dir);
1264 } else if (rel_x || rel_y) {
1265 input_report_rel(ictx->idev, REL_X, rel_x);
1266 input_report_rel(ictx->idev, REL_Y, rel_y);
1267 } else {
1268 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1269 input_report_key(ictx->idev, BTN_RIGHT,
1270 buf[1] >> right_shift & 0x1);
1272 input_sync(ictx->idev);
1273 spin_lock_irqsave(&ictx->kc_lock, flags);
1274 ictx->last_keycode = ictx->kc;
1275 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1278 return mouse_input;
1281 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1283 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1284 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1285 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1286 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1287 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1288 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1289 input_sync(ictx->touch);
1292 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1294 int dir = 0;
1295 char rel_x = 0x00, rel_y = 0x00;
1296 u16 timeout, threshold;
1297 u32 scancode = KEY_RESERVED;
1298 unsigned long flags;
1301 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1302 * contain a position coordinate (x,y), with each component ranging
1303 * from -14 to 14. We want to down-sample this to only 4 discrete values
1304 * for up/down/left/right arrow keys. Also, when you get too close to
1305 * diagonals, it has a tendancy to jump back and forth, so lets try to
1306 * ignore when they get too close.
1308 if (ictx->product != 0xffdc) {
1309 /* first, pad to 8 bytes so it conforms with everything else */
1310 buf[5] = buf[6] = buf[7] = 0;
1311 timeout = 500; /* in msecs */
1312 /* (2*threshold) x (2*threshold) square */
1313 threshold = pad_thresh ? pad_thresh : 28;
1314 rel_x = buf[2];
1315 rel_y = buf[3];
1317 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1318 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1319 dir = stabilize((int)rel_x, (int)rel_y,
1320 timeout, threshold);
1321 if (!dir) {
1322 spin_lock_irqsave(&ictx->kc_lock,
1323 flags);
1324 ictx->kc = KEY_UNKNOWN;
1325 spin_unlock_irqrestore(&ictx->kc_lock,
1326 flags);
1327 return;
1329 buf[2] = dir & 0xFF;
1330 buf[3] = (dir >> 8) & 0xFF;
1331 scancode = be32_to_cpu(*((u32 *)buf));
1333 } else {
1335 * Hack alert: instead of using keycodes, we have
1336 * to use hard-coded scancodes here...
1338 if (abs(rel_y) > abs(rel_x)) {
1339 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1340 buf[3] = 0;
1341 if (rel_y > 0)
1342 scancode = 0x01007f00; /* KEY_DOWN */
1343 else
1344 scancode = 0x01008000; /* KEY_UP */
1345 } else {
1346 buf[2] = 0;
1347 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1348 if (rel_x > 0)
1349 scancode = 0x0100007f; /* KEY_RIGHT */
1350 else
1351 scancode = 0x01000080; /* KEY_LEFT */
1356 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1357 * device (15c2:ffdc). The remote generates various codes from
1358 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1359 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1360 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1361 * reversed endianess. Extract direction from buffer, rotate endianess,
1362 * adjust sign and feed the values into stabilize(). The resulting codes
1363 * will be 0x01008000, 0x01007F00, which match the newer devices.
1365 } else {
1366 timeout = 10; /* in msecs */
1367 /* (2*threshold) x (2*threshold) square */
1368 threshold = pad_thresh ? pad_thresh : 15;
1370 /* buf[1] is x */
1371 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1372 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1373 if (buf[0] & 0x02)
1374 rel_x |= ~0x10+1;
1375 /* buf[2] is y */
1376 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1377 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1378 if (buf[0] & 0x01)
1379 rel_y |= ~0x10+1;
1381 buf[0] = 0x01;
1382 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1384 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1385 dir = stabilize((int)rel_x, (int)rel_y,
1386 timeout, threshold);
1387 if (!dir) {
1388 spin_lock_irqsave(&ictx->kc_lock, flags);
1389 ictx->kc = KEY_UNKNOWN;
1390 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1391 return;
1393 buf[2] = dir & 0xFF;
1394 buf[3] = (dir >> 8) & 0xFF;
1395 scancode = be32_to_cpu(*((u32 *)buf));
1396 } else {
1398 * Hack alert: instead of using keycodes, we have
1399 * to use hard-coded scancodes here...
1401 if (abs(rel_y) > abs(rel_x)) {
1402 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1403 buf[3] = 0;
1404 if (rel_y > 0)
1405 scancode = 0x01007f00; /* KEY_DOWN */
1406 else
1407 scancode = 0x01008000; /* KEY_UP */
1408 } else {
1409 buf[2] = 0;
1410 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1411 if (rel_x > 0)
1412 scancode = 0x0100007f; /* KEY_RIGHT */
1413 else
1414 scancode = 0x01000080; /* KEY_LEFT */
1419 if (scancode) {
1420 spin_lock_irqsave(&ictx->kc_lock, flags);
1421 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1422 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1427 * figure out if these is a press or a release. We don't actually
1428 * care about repeats, as those will be auto-generated within the IR
1429 * subsystem for repeating scancodes.
1431 static int imon_parse_press_type(struct imon_context *ictx,
1432 unsigned char *buf, u8 ktype)
1434 int press_type = 0;
1435 unsigned long flags;
1437 spin_lock_irqsave(&ictx->kc_lock, flags);
1439 /* key release of 0x02XXXXXX key */
1440 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1441 ictx->kc = ictx->last_keycode;
1443 /* mouse button release on (some) 0xffdc devices */
1444 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1445 buf[2] == 0x81 && buf[3] == 0xb7)
1446 ictx->kc = ictx->last_keycode;
1448 /* mouse button release on (some other) 0xffdc devices */
1449 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1450 buf[2] == 0x81 && buf[3] == 0xb7)
1451 ictx->kc = ictx->last_keycode;
1453 /* mce-specific button handling, no keyup events */
1454 else if (ktype == IMON_KEY_MCE) {
1455 ictx->rc_toggle = buf[2];
1456 press_type = 1;
1458 /* incoherent or irrelevant data */
1459 } else if (ictx->kc == KEY_RESERVED)
1460 press_type = -EINVAL;
1462 /* key release of 0xXXXXXXb7 key */
1463 else if (ictx->release_code)
1464 press_type = 0;
1466 /* this is a button press */
1467 else
1468 press_type = 1;
1470 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1472 return press_type;
1476 * Process the incoming packet
1478 static void imon_incoming_packet(struct imon_context *ictx,
1479 struct urb *urb, int intf)
1481 int len = urb->actual_length;
1482 unsigned char *buf = urb->transfer_buffer;
1483 struct device *dev = ictx->dev;
1484 unsigned long flags;
1485 u32 kc;
1486 bool norelease = false;
1487 int i;
1488 u64 scancode;
1489 int press_type = 0;
1490 int msec;
1491 struct timeval t;
1492 static struct timeval prev_time = { 0, 0 };
1493 u8 ktype;
1495 /* filter out junk data on the older 0xffdc imon devices */
1496 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1497 return;
1499 /* Figure out what key was pressed */
1500 if (len == 8 && buf[7] == 0xee) {
1501 scancode = be64_to_cpu(*((u64 *)buf));
1502 ktype = IMON_KEY_PANEL;
1503 kc = imon_panel_key_lookup(scancode);
1504 } else {
1505 scancode = be32_to_cpu(*((u32 *)buf));
1506 if (ictx->rc_type == RC_TYPE_RC6) {
1507 ktype = IMON_KEY_IMON;
1508 if (buf[0] == 0x80)
1509 ktype = IMON_KEY_MCE;
1510 kc = imon_mce_key_lookup(ictx, scancode);
1511 } else {
1512 ktype = IMON_KEY_IMON;
1513 kc = imon_remote_key_lookup(ictx, scancode);
1517 spin_lock_irqsave(&ictx->kc_lock, flags);
1518 /* keyboard/mouse mode toggle button */
1519 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1520 ictx->last_keycode = kc;
1521 if (!nomouse) {
1522 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1523 dev_dbg(dev, "toggling to %s mode\n",
1524 ictx->pad_mouse ? "mouse" : "keyboard");
1525 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1526 return;
1527 } else {
1528 ictx->pad_mouse = false;
1529 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1533 ictx->kc = kc;
1534 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1536 /* send touchscreen events through input subsystem if touchpad data */
1537 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1538 buf[7] == 0x86) {
1539 imon_touch_event(ictx, buf);
1540 return;
1542 /* look for mouse events with pad in mouse mode */
1543 } else if (ictx->pad_mouse) {
1544 if (imon_mouse_event(ictx, buf, len))
1545 return;
1548 /* Now for some special handling to convert pad input to arrow keys */
1549 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1550 ((len == 8) && (buf[0] & 0x40) &&
1551 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1552 len = 8;
1553 imon_pad_to_keys(ictx, buf);
1554 norelease = true;
1557 if (debug) {
1558 printk(KERN_INFO "intf%d decoded packet: ", intf);
1559 for (i = 0; i < len; ++i)
1560 printk("%02x ", buf[i]);
1561 printk("\n");
1564 press_type = imon_parse_press_type(ictx, buf, ktype);
1565 if (press_type < 0)
1566 goto not_input_data;
1568 spin_lock_irqsave(&ictx->kc_lock, flags);
1569 if (ictx->kc == KEY_UNKNOWN)
1570 goto unknown_key;
1571 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1573 if (ktype != IMON_KEY_PANEL) {
1574 if (press_type == 0)
1575 rc_keyup(ictx->rdev);
1576 else {
1577 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
1578 spin_lock_irqsave(&ictx->kc_lock, flags);
1579 ictx->last_keycode = ictx->kc;
1580 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1582 return;
1585 /* Only panel type events left to process now */
1586 spin_lock_irqsave(&ictx->kc_lock, flags);
1588 /* KEY_MUTE repeats from knob need to be suppressed */
1589 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
1590 do_gettimeofday(&t);
1591 msec = tv2int(&t, &prev_time);
1592 prev_time = t;
1593 if (msec < ictx->idev->rep[REP_DELAY]) {
1594 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1595 return;
1598 kc = ictx->kc;
1600 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1602 input_report_key(ictx->idev, kc, press_type);
1603 input_sync(ictx->idev);
1605 /* panel keys don't generate a release */
1606 input_report_key(ictx->idev, kc, 0);
1607 input_sync(ictx->idev);
1609 ictx->last_keycode = kc;
1611 return;
1613 unknown_key:
1614 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1615 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
1616 (long long)scancode);
1617 return;
1619 not_input_data:
1620 if (len != 8) {
1621 dev_warn(dev, "imon %s: invalid incoming packet "
1622 "size (len = %d, intf%d)\n", __func__, len, intf);
1623 return;
1626 /* iMON 2.4G associate frame */
1627 if (buf[0] == 0x00 &&
1628 buf[2] == 0xFF && /* REFID */
1629 buf[3] == 0xFF &&
1630 buf[4] == 0xFF &&
1631 buf[5] == 0xFF && /* iMON 2.4G */
1632 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1633 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1634 dev_warn(dev, "%s: remote associated refid=%02X\n",
1635 __func__, buf[1]);
1636 ictx->rf_isassociating = false;
1641 * Callback function for USB core API: receive data
1643 static void usb_rx_callback_intf0(struct urb *urb)
1645 struct imon_context *ictx;
1646 int intfnum = 0;
1648 if (!urb)
1649 return;
1651 ictx = (struct imon_context *)urb->context;
1652 if (!ictx)
1653 return;
1655 switch (urb->status) {
1656 case -ENOENT: /* usbcore unlink successful! */
1657 return;
1659 case -ESHUTDOWN: /* transport endpoint was shut down */
1660 break;
1662 case 0:
1663 imon_incoming_packet(ictx, urb, intfnum);
1664 break;
1666 default:
1667 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1668 __func__, urb->status);
1669 break;
1672 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1675 static void usb_rx_callback_intf1(struct urb *urb)
1677 struct imon_context *ictx;
1678 int intfnum = 1;
1680 if (!urb)
1681 return;
1683 ictx = (struct imon_context *)urb->context;
1684 if (!ictx)
1685 return;
1687 switch (urb->status) {
1688 case -ENOENT: /* usbcore unlink successful! */
1689 return;
1691 case -ESHUTDOWN: /* transport endpoint was shut down */
1692 break;
1694 case 0:
1695 imon_incoming_packet(ictx, urb, intfnum);
1696 break;
1698 default:
1699 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1700 __func__, urb->status);
1701 break;
1704 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1708 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1709 * devices, and all of them constantly spew interrupts, even when there
1710 * is no actual data to report. However, byte 6 of this buffer looks like
1711 * its unique across device variants, so we're trying to key off that to
1712 * figure out which display type (if any) and what IR protocol the device
1713 * actually supports. These devices have their IR protocol hard-coded into
1714 * their firmware, they can't be changed on the fly like the newer hardware.
1716 static void imon_get_ffdc_type(struct imon_context *ictx)
1718 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1719 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1720 u64 allowed_protos = RC_TYPE_OTHER;
1722 switch (ffdc_cfg_byte) {
1723 /* iMON Knob, no display, iMON IR + vol knob */
1724 case 0x21:
1725 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1726 ictx->display_supported = false;
1727 break;
1728 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1729 case 0x4e:
1730 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1731 ictx->display_supported = false;
1732 ictx->rf_device = true;
1733 break;
1734 /* iMON VFD, no IR (does have vol knob tho) */
1735 case 0x35:
1736 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1737 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1738 break;
1739 /* iMON VFD, iMON IR */
1740 case 0x24:
1741 case 0x85:
1742 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1743 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1744 break;
1745 /* iMON VFD, MCE IR */
1746 case 0x9e:
1747 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1748 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1749 allowed_protos = RC_TYPE_RC6;
1750 break;
1751 /* iMON LCD, MCE IR */
1752 case 0x9f:
1753 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1754 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1755 allowed_protos = RC_TYPE_RC6;
1756 break;
1757 default:
1758 dev_info(ictx->dev, "Unknown 0xffdc device, "
1759 "defaulting to VFD and iMON IR");
1760 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1761 break;
1764 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1766 ictx->display_type = detected_display_type;
1767 ictx->rc_type = allowed_protos;
1770 static void imon_set_display_type(struct imon_context *ictx)
1772 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1775 * Try to auto-detect the type of display if the user hasn't set
1776 * it by hand via the display_type modparam. Default is VFD.
1779 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1780 switch (ictx->product) {
1781 case 0xffdc:
1782 /* set in imon_get_ffdc_type() */
1783 configured_display_type = ictx->display_type;
1784 break;
1785 case 0x0034:
1786 case 0x0035:
1787 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1788 break;
1789 case 0x0038:
1790 case 0x0039:
1791 case 0x0045:
1792 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1793 break;
1794 case 0x003c:
1795 case 0x0041:
1796 case 0x0042:
1797 case 0x0043:
1798 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1799 ictx->display_supported = false;
1800 break;
1801 case 0x0036:
1802 case 0x0044:
1803 default:
1804 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1805 break;
1807 } else {
1808 configured_display_type = display_type;
1809 if (display_type == IMON_DISPLAY_TYPE_NONE)
1810 ictx->display_supported = false;
1811 else
1812 ictx->display_supported = true;
1813 dev_info(ictx->dev, "%s: overriding display type to %d via "
1814 "modparam\n", __func__, display_type);
1817 ictx->display_type = configured_display_type;
1820 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1822 struct rc_dev *rdev;
1823 int ret;
1824 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1825 0x00, 0x00, 0x00, 0x88 };
1827 rdev = rc_allocate_device();
1828 if (!rdev) {
1829 dev_err(ictx->dev, "remote control dev allocation failed\n");
1830 goto out;
1833 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1834 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1835 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1836 sizeof(ictx->phys_rdev));
1837 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1839 rdev->input_name = ictx->name_rdev;
1840 rdev->input_phys = ictx->phys_rdev;
1841 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1842 rdev->dev.parent = ictx->dev;
1844 rdev->priv = ictx;
1845 rdev->driver_type = RC_DRIVER_SCANCODE;
1846 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
1847 rdev->change_protocol = imon_ir_change_protocol;
1848 rdev->driver_name = MOD_NAME;
1850 /* Enable front-panel buttons and/or knobs */
1851 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1852 ret = send_packet(ictx);
1853 /* Not fatal, but warn about it */
1854 if (ret)
1855 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1857 if (ictx->product == 0xffdc) {
1858 imon_get_ffdc_type(ictx);
1859 rdev->allowed_protos = ictx->rc_type;
1862 imon_set_display_type(ictx);
1864 if (ictx->rc_type == RC_TYPE_RC6)
1865 rdev->map_name = RC_MAP_IMON_MCE;
1866 else
1867 rdev->map_name = RC_MAP_IMON_PAD;
1869 ret = rc_register_device(rdev);
1870 if (ret < 0) {
1871 dev_err(ictx->dev, "remote input dev register failed\n");
1872 goto out;
1875 return rdev;
1877 out:
1878 rc_free_device(rdev);
1879 return NULL;
1882 static struct input_dev *imon_init_idev(struct imon_context *ictx)
1884 struct input_dev *idev;
1885 int ret, i;
1887 idev = input_allocate_device();
1888 if (!idev) {
1889 dev_err(ictx->dev, "input dev allocation failed\n");
1890 goto out;
1893 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1894 "iMON Panel, Knob and Mouse(%04x:%04x)",
1895 ictx->vendor, ictx->product);
1896 idev->name = ictx->name_idev;
1898 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1899 sizeof(ictx->phys_idev));
1900 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
1901 idev->phys = ictx->phys_idev;
1903 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
1905 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1906 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1907 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1908 BIT_MASK(REL_WHEEL);
1910 /* panel and/or knob code support */
1911 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1912 u32 kc = imon_panel_key_table[i].keycode;
1913 __set_bit(kc, idev->keybit);
1916 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1917 idev->dev.parent = ictx->dev;
1918 input_set_drvdata(idev, ictx);
1920 ret = input_register_device(idev);
1921 if (ret < 0) {
1922 dev_err(ictx->dev, "input dev register failed\n");
1923 goto out;
1926 return idev;
1928 out:
1929 input_free_device(idev);
1930 return NULL;
1933 static struct input_dev *imon_init_touch(struct imon_context *ictx)
1935 struct input_dev *touch;
1936 int ret;
1938 touch = input_allocate_device();
1939 if (!touch) {
1940 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1941 goto touch_alloc_failed;
1944 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1945 "iMON USB Touchscreen (%04x:%04x)",
1946 ictx->vendor, ictx->product);
1947 touch->name = ictx->name_touch;
1949 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1950 sizeof(ictx->phys_touch));
1951 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
1952 touch->phys = ictx->phys_touch;
1954 touch->evbit[0] =
1955 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1956 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1957 BIT_MASK(BTN_TOUCH);
1958 input_set_abs_params(touch, ABS_X,
1959 0x00, 0xfff, 0, 0);
1960 input_set_abs_params(touch, ABS_Y,
1961 0x00, 0xfff, 0, 0);
1963 input_set_drvdata(touch, ictx);
1965 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1966 touch->dev.parent = ictx->dev;
1967 ret = input_register_device(touch);
1968 if (ret < 0) {
1969 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1970 goto touch_register_failed;
1973 return touch;
1975 touch_register_failed:
1976 input_free_device(ictx->touch);
1978 touch_alloc_failed:
1979 return NULL;
1982 static bool imon_find_endpoints(struct imon_context *ictx,
1983 struct usb_host_interface *iface_desc)
1985 struct usb_endpoint_descriptor *ep;
1986 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1987 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1988 int ifnum = iface_desc->desc.bInterfaceNumber;
1989 int num_endpts = iface_desc->desc.bNumEndpoints;
1990 int i, ep_dir, ep_type;
1991 bool ir_ep_found = false;
1992 bool display_ep_found = false;
1993 bool tx_control = false;
1996 * Scan the endpoint list and set:
1997 * first input endpoint = IR endpoint
1998 * first output endpoint = display endpoint
2000 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2001 ep = &iface_desc->endpoint[i].desc;
2002 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2003 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2005 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2006 ep_type == USB_ENDPOINT_XFER_INT) {
2008 rx_endpoint = ep;
2009 ir_ep_found = true;
2010 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2012 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2013 ep_type == USB_ENDPOINT_XFER_INT) {
2014 tx_endpoint = ep;
2015 display_ep_found = true;
2016 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2020 if (ifnum == 0) {
2021 ictx->rx_endpoint_intf0 = rx_endpoint;
2023 * tx is used to send characters to lcd/vfd, associate RF
2024 * remotes, set IR protocol, and maybe more...
2026 ictx->tx_endpoint = tx_endpoint;
2027 } else {
2028 ictx->rx_endpoint_intf1 = rx_endpoint;
2032 * If we didn't find a display endpoint, this is probably one of the
2033 * newer iMON devices that use control urb instead of interrupt
2035 if (!display_ep_found) {
2036 tx_control = true;
2037 display_ep_found = true;
2038 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2039 "interface OUT endpoint\n", __func__);
2043 * Some iMON receivers have no display. Unfortunately, it seems
2044 * that SoundGraph recycles device IDs between devices both with
2045 * and without... :\
2047 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2048 display_ep_found = false;
2049 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2053 * iMON Touch devices have a VGA touchscreen, but no "display", as
2054 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2056 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2057 display_ep_found = false;
2058 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2061 /* Input endpoint is mandatory */
2062 if (!ir_ep_found)
2063 pr_err("no valid input (IR) endpoint found\n");
2065 ictx->tx_control = tx_control;
2067 if (display_ep_found)
2068 ictx->display_supported = true;
2070 return ir_ep_found;
2074 static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2076 struct imon_context *ictx;
2077 struct urb *rx_urb;
2078 struct urb *tx_urb;
2079 struct device *dev = &intf->dev;
2080 struct usb_host_interface *iface_desc;
2081 int ret = -ENOMEM;
2083 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2084 if (!ictx) {
2085 dev_err(dev, "%s: kzalloc failed for context", __func__);
2086 goto exit;
2088 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2089 if (!rx_urb) {
2090 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2091 goto rx_urb_alloc_failed;
2093 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2094 if (!tx_urb) {
2095 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2096 __func__);
2097 goto tx_urb_alloc_failed;
2100 mutex_init(&ictx->lock);
2101 spin_lock_init(&ictx->kc_lock);
2103 mutex_lock(&ictx->lock);
2105 ictx->dev = dev;
2106 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2107 ictx->dev_present_intf0 = true;
2108 ictx->rx_urb_intf0 = rx_urb;
2109 ictx->tx_urb = tx_urb;
2110 ictx->rf_device = false;
2112 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2113 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2115 ret = -ENODEV;
2116 iface_desc = intf->cur_altsetting;
2117 if (!imon_find_endpoints(ictx, iface_desc)) {
2118 goto find_endpoint_failed;
2121 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2122 usb_rcvintpipe(ictx->usbdev_intf0,
2123 ictx->rx_endpoint_intf0->bEndpointAddress),
2124 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2125 usb_rx_callback_intf0, ictx,
2126 ictx->rx_endpoint_intf0->bInterval);
2128 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2129 if (ret) {
2130 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2131 goto urb_submit_failed;
2134 ictx->idev = imon_init_idev(ictx);
2135 if (!ictx->idev) {
2136 dev_err(dev, "%s: input device setup failed\n", __func__);
2137 goto idev_setup_failed;
2140 ictx->rdev = imon_init_rdev(ictx);
2141 if (!ictx->rdev) {
2142 dev_err(dev, "%s: rc device setup failed\n", __func__);
2143 goto rdev_setup_failed;
2146 mutex_unlock(&ictx->lock);
2147 return ictx;
2149 rdev_setup_failed:
2150 input_unregister_device(ictx->idev);
2151 idev_setup_failed:
2152 usb_kill_urb(ictx->rx_urb_intf0);
2153 urb_submit_failed:
2154 find_endpoint_failed:
2155 mutex_unlock(&ictx->lock);
2156 usb_free_urb(tx_urb);
2157 tx_urb_alloc_failed:
2158 usb_free_urb(rx_urb);
2159 rx_urb_alloc_failed:
2160 kfree(ictx);
2161 exit:
2162 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2164 return NULL;
2167 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2168 struct imon_context *ictx)
2170 struct urb *rx_urb;
2171 struct usb_host_interface *iface_desc;
2172 int ret = -ENOMEM;
2174 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2175 if (!rx_urb) {
2176 pr_err("usb_alloc_urb failed for IR urb\n");
2177 goto rx_urb_alloc_failed;
2180 mutex_lock(&ictx->lock);
2182 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2183 init_timer(&ictx->ttimer);
2184 ictx->ttimer.data = (unsigned long)ictx;
2185 ictx->ttimer.function = imon_touch_display_timeout;
2188 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2189 ictx->dev_present_intf1 = true;
2190 ictx->rx_urb_intf1 = rx_urb;
2192 ret = -ENODEV;
2193 iface_desc = intf->cur_altsetting;
2194 if (!imon_find_endpoints(ictx, iface_desc))
2195 goto find_endpoint_failed;
2197 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2198 ictx->touch = imon_init_touch(ictx);
2199 if (!ictx->touch)
2200 goto touch_setup_failed;
2201 } else
2202 ictx->touch = NULL;
2204 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2205 usb_rcvintpipe(ictx->usbdev_intf1,
2206 ictx->rx_endpoint_intf1->bEndpointAddress),
2207 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2208 usb_rx_callback_intf1, ictx,
2209 ictx->rx_endpoint_intf1->bInterval);
2211 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2213 if (ret) {
2214 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2215 goto urb_submit_failed;
2218 mutex_unlock(&ictx->lock);
2219 return ictx;
2221 urb_submit_failed:
2222 if (ictx->touch)
2223 input_unregister_device(ictx->touch);
2224 touch_setup_failed:
2225 find_endpoint_failed:
2226 mutex_unlock(&ictx->lock);
2227 usb_free_urb(rx_urb);
2228 rx_urb_alloc_failed:
2229 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2231 return NULL;
2234 static void imon_init_display(struct imon_context *ictx,
2235 struct usb_interface *intf)
2237 int ret;
2239 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2241 /* set up sysfs entry for built-in clock */
2242 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2243 if (ret)
2244 dev_err(ictx->dev, "Could not create display sysfs "
2245 "entries(%d)", ret);
2247 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2248 ret = usb_register_dev(intf, &imon_lcd_class);
2249 else
2250 ret = usb_register_dev(intf, &imon_vfd_class);
2251 if (ret)
2252 /* Not a fatal error, so ignore */
2253 dev_info(ictx->dev, "could not get a minor number for "
2254 "display\n");
2259 * Callback function for USB core API: Probe
2261 static int __devinit imon_probe(struct usb_interface *interface,
2262 const struct usb_device_id *id)
2264 struct usb_device *usbdev = NULL;
2265 struct usb_host_interface *iface_desc = NULL;
2266 struct usb_interface *first_if;
2267 struct device *dev = &interface->dev;
2268 int ifnum, code_length, sysfs_err;
2269 int ret = 0;
2270 struct imon_context *ictx = NULL;
2271 struct imon_context *first_if_ctx = NULL;
2272 u16 vendor, product;
2274 code_length = BUF_CHUNK_SIZE * 8;
2276 usbdev = usb_get_dev(interface_to_usbdev(interface));
2277 iface_desc = interface->cur_altsetting;
2278 ifnum = iface_desc->desc.bInterfaceNumber;
2279 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2280 product = le16_to_cpu(usbdev->descriptor.idProduct);
2282 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2283 __func__, vendor, product, ifnum);
2285 /* prevent races probing devices w/multiple interfaces */
2286 mutex_lock(&driver_lock);
2288 first_if = usb_ifnum_to_if(usbdev, 0);
2289 first_if_ctx = usb_get_intfdata(first_if);
2291 if (ifnum == 0) {
2292 ictx = imon_init_intf0(interface);
2293 if (!ictx) {
2294 pr_err("failed to initialize context!\n");
2295 ret = -ENODEV;
2296 goto fail;
2299 } else {
2300 /* this is the secondary interface on the device */
2301 ictx = imon_init_intf1(interface, first_if_ctx);
2302 if (!ictx) {
2303 pr_err("failed to attach to context!\n");
2304 ret = -ENODEV;
2305 goto fail;
2310 usb_set_intfdata(interface, ictx);
2312 if (ifnum == 0) {
2313 mutex_lock(&ictx->lock);
2315 if (product == 0xffdc && ictx->rf_device) {
2316 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2317 &imon_rf_attr_group);
2318 if (sysfs_err)
2319 pr_err("Could not create RF sysfs entries(%d)\n",
2320 sysfs_err);
2323 if (ictx->display_supported)
2324 imon_init_display(ictx, interface);
2326 mutex_unlock(&ictx->lock);
2329 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2330 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2331 usbdev->bus->busnum, usbdev->devnum);
2333 mutex_unlock(&driver_lock);
2335 return 0;
2337 fail:
2338 mutex_unlock(&driver_lock);
2339 dev_err(dev, "unable to register, err %d\n", ret);
2341 return ret;
2345 * Callback function for USB core API: disconnect
2347 static void __devexit imon_disconnect(struct usb_interface *interface)
2349 struct imon_context *ictx;
2350 struct device *dev;
2351 int ifnum;
2353 /* prevent races with multi-interface device probing and display_open */
2354 mutex_lock(&driver_lock);
2356 ictx = usb_get_intfdata(interface);
2357 dev = ictx->dev;
2358 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2360 mutex_lock(&ictx->lock);
2363 * sysfs_remove_group is safe to call even if sysfs_create_group
2364 * hasn't been called
2366 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2367 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2369 usb_set_intfdata(interface, NULL);
2371 /* Abort ongoing write */
2372 if (ictx->tx.busy) {
2373 usb_kill_urb(ictx->tx_urb);
2374 complete_all(&ictx->tx.finished);
2377 if (ifnum == 0) {
2378 ictx->dev_present_intf0 = false;
2379 usb_kill_urb(ictx->rx_urb_intf0);
2380 input_unregister_device(ictx->idev);
2381 rc_unregister_device(ictx->rdev);
2382 if (ictx->display_supported) {
2383 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2384 usb_deregister_dev(interface, &imon_lcd_class);
2385 else
2386 usb_deregister_dev(interface, &imon_vfd_class);
2388 } else {
2389 ictx->dev_present_intf1 = false;
2390 usb_kill_urb(ictx->rx_urb_intf1);
2391 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2392 input_unregister_device(ictx->touch);
2395 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1) {
2396 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2397 del_timer_sync(&ictx->ttimer);
2398 mutex_unlock(&ictx->lock);
2399 if (!ictx->display_isopen)
2400 free_imon_context(ictx);
2401 } else
2402 mutex_unlock(&ictx->lock);
2404 mutex_unlock(&driver_lock);
2406 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2407 __func__, ifnum);
2410 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2412 struct imon_context *ictx = usb_get_intfdata(intf);
2413 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2415 if (ifnum == 0)
2416 usb_kill_urb(ictx->rx_urb_intf0);
2417 else
2418 usb_kill_urb(ictx->rx_urb_intf1);
2420 return 0;
2423 static int imon_resume(struct usb_interface *intf)
2425 int rc = 0;
2426 struct imon_context *ictx = usb_get_intfdata(intf);
2427 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2429 if (ifnum == 0) {
2430 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2431 usb_rcvintpipe(ictx->usbdev_intf0,
2432 ictx->rx_endpoint_intf0->bEndpointAddress),
2433 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2434 usb_rx_callback_intf0, ictx,
2435 ictx->rx_endpoint_intf0->bInterval);
2437 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2439 } else {
2440 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2441 usb_rcvintpipe(ictx->usbdev_intf1,
2442 ictx->rx_endpoint_intf1->bEndpointAddress),
2443 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2444 usb_rx_callback_intf1, ictx,
2445 ictx->rx_endpoint_intf1->bInterval);
2447 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2450 return rc;
2453 static int __init imon_init(void)
2455 int rc;
2457 rc = usb_register(&imon_driver);
2458 if (rc) {
2459 pr_err("usb register failed(%d)\n", rc);
2460 rc = -ENODEV;
2463 return rc;
2466 static void __exit imon_exit(void)
2468 usb_deregister(&imon_driver);
2471 module_init(imon_init);
2472 module_exit(imon_exit);