FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / media / rc / imon.c
blobf714e1a22c9206cd2e679cfc087817b0cb8e4c79
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.2"
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_MEDIA }, /* Go */
281 { 0x000000001200ffeell, KEY_UP },
282 { 0x000000001300ffeell, KEY_DOWN },
283 { 0x000000001400ffeell, KEY_LEFT },
284 { 0x000000001500ffeell, KEY_RIGHT },
285 { 0x000000001600ffeell, KEY_ENTER },
286 { 0x000000001700ffeell, KEY_ESC },
287 { 0x000000001f00ffeell, KEY_AUDIO },
288 { 0x000000002000ffeell, KEY_VIDEO },
289 { 0x000000002100ffeell, KEY_CAMERA },
290 { 0x000000002700ffeell, KEY_DVD },
291 { 0x000000002300ffeell, KEY_TV },
292 { 0x000000002b00ffeell, KEY_EXIT },
293 { 0x000000002c00ffeell, KEY_SELECT },
294 { 0x000000002d00ffeell, KEY_MENU },
295 { 0x000000000500ffeell, KEY_PREVIOUS },
296 { 0x000000000700ffeell, KEY_REWIND },
297 { 0x000000000400ffeell, KEY_STOP },
298 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
299 { 0x000000000800ffeell, KEY_FASTFORWARD },
300 { 0x000000000600ffeell, KEY_NEXT },
301 { 0x000000010000ffeell, KEY_RIGHT },
302 { 0x000001000000ffeell, KEY_LEFT },
303 { 0x000000003d00ffeell, KEY_SELECT },
304 { 0x000100000000ffeell, KEY_VOLUMEUP },
305 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
306 { 0x000000000100ffeell, KEY_MUTE },
307 /* 0xffdc iMON MCE VFD */
308 { 0x00010000ffffffeell, KEY_VOLUMEUP },
309 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
310 /* iMON Knob values */
311 { 0x000100ffffffffeell, KEY_VOLUMEUP },
312 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
313 { 0x000008ffffffffeell, KEY_MUTE },
316 /* to prevent races between open() and disconnect(), probing, etc */
317 static DEFINE_MUTEX(driver_lock);
319 /* Module bookkeeping bits */
320 MODULE_AUTHOR(MOD_AUTHOR);
321 MODULE_DESCRIPTION(MOD_DESC);
322 MODULE_VERSION(MOD_VERSION);
323 MODULE_LICENSE("GPL");
324 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
326 static bool debug;
327 module_param(debug, bool, S_IRUGO | S_IWUSR);
328 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
330 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
331 static int display_type;
332 module_param(display_type, int, S_IRUGO);
333 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
334 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
336 static int pad_stabilize = 1;
337 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
338 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
339 "presses in arrow key mode. 0=disable, 1=enable (default).");
342 * In certain use cases, mouse mode isn't really helpful, and could actually
343 * cause confusion, so allow disabling it when the IR device is open.
345 static bool nomouse;
346 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
347 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
348 "open. 0=don't disable, 1=disable. (default: don't disable)");
350 /* threshold at which a pad push registers as an arrow key in kbd mode */
351 static int pad_thresh;
352 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
353 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
354 "arrow key in kbd mode (default: 28)");
357 static void free_imon_context(struct imon_context *ictx)
359 struct device *dev = ictx->dev;
361 usb_free_urb(ictx->tx_urb);
362 usb_free_urb(ictx->rx_urb_intf0);
363 usb_free_urb(ictx->rx_urb_intf1);
364 kfree(ictx);
366 dev_dbg(dev, "%s: iMON context freed\n", __func__);
370 * Called when the Display device (e.g. /dev/lcd0)
371 * is opened by the application.
373 static int display_open(struct inode *inode, struct file *file)
375 struct usb_interface *interface;
376 struct imon_context *ictx = NULL;
377 int subminor;
378 int retval = 0;
380 /* prevent races with disconnect */
381 mutex_lock(&driver_lock);
383 subminor = iminor(inode);
384 interface = usb_find_interface(&imon_driver, subminor);
385 if (!interface) {
386 pr_err("could not find interface for minor %d\n", subminor);
387 retval = -ENODEV;
388 goto exit;
390 ictx = usb_get_intfdata(interface);
392 if (!ictx) {
393 pr_err("no context found for minor %d\n", subminor);
394 retval = -ENODEV;
395 goto exit;
398 mutex_lock(&ictx->lock);
400 if (!ictx->display_supported) {
401 pr_err("display not supported by device\n");
402 retval = -ENODEV;
403 } else if (ictx->display_isopen) {
404 pr_err("display port is already open\n");
405 retval = -EBUSY;
406 } else {
407 ictx->display_isopen = true;
408 file->private_data = ictx;
409 dev_dbg(ictx->dev, "display port opened\n");
412 mutex_unlock(&ictx->lock);
414 exit:
415 mutex_unlock(&driver_lock);
416 return retval;
420 * Called when the display device (e.g. /dev/lcd0)
421 * is closed by the application.
423 static int display_close(struct inode *inode, struct file *file)
425 struct imon_context *ictx = NULL;
426 int retval = 0;
428 ictx = file->private_data;
430 if (!ictx) {
431 pr_err("no context for device\n");
432 return -ENODEV;
435 mutex_lock(&ictx->lock);
437 if (!ictx->display_supported) {
438 pr_err("display not supported by device\n");
439 retval = -ENODEV;
440 } else if (!ictx->display_isopen) {
441 pr_err("display is not open\n");
442 retval = -EIO;
443 } else {
444 ictx->display_isopen = false;
445 dev_dbg(ictx->dev, "display port closed\n");
446 if (!ictx->dev_present_intf0) {
448 * Device disconnected before close and IR port is not
449 * open. If IR port is open, context will be deleted by
450 * ir_close.
452 mutex_unlock(&ictx->lock);
453 free_imon_context(ictx);
454 return retval;
458 mutex_unlock(&ictx->lock);
459 return retval;
463 * Sends a packet to the device -- this function must be called
464 * with ictx->lock held.
466 static int send_packet(struct imon_context *ictx)
468 unsigned int pipe;
469 unsigned long timeout;
470 int interval = 0;
471 int retval = 0;
472 struct usb_ctrlrequest *control_req = NULL;
474 /* Check if we need to use control or interrupt urb */
475 if (!ictx->tx_control) {
476 pipe = usb_sndintpipe(ictx->usbdev_intf0,
477 ictx->tx_endpoint->bEndpointAddress);
478 interval = ictx->tx_endpoint->bInterval;
480 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
481 ictx->usb_tx_buf,
482 sizeof(ictx->usb_tx_buf),
483 usb_tx_callback, ictx, interval);
485 ictx->tx_urb->actual_length = 0;
486 } else {
487 /* fill request into kmalloc'ed space: */
488 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
489 GFP_KERNEL);
490 if (control_req == NULL)
491 return -ENOMEM;
493 /* setup packet is '21 09 0200 0001 0008' */
494 control_req->bRequestType = 0x21;
495 control_req->bRequest = 0x09;
496 control_req->wValue = cpu_to_le16(0x0200);
497 control_req->wIndex = cpu_to_le16(0x0001);
498 control_req->wLength = cpu_to_le16(0x0008);
500 /* control pipe is endpoint 0x00 */
501 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
503 /* build the control urb */
504 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
505 pipe, (unsigned char *)control_req,
506 ictx->usb_tx_buf,
507 sizeof(ictx->usb_tx_buf),
508 usb_tx_callback, ictx);
509 ictx->tx_urb->actual_length = 0;
512 init_completion(&ictx->tx.finished);
513 ictx->tx.busy = true;
514 smp_rmb(); /* ensure later readers know we're busy */
516 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
517 if (retval) {
518 ictx->tx.busy = false;
519 smp_rmb(); /* ensure later readers know we're not busy */
520 pr_err("error submitting urb(%d)\n", retval);
521 } else {
522 /* Wait for transmission to complete (or abort) */
523 mutex_unlock(&ictx->lock);
524 retval = wait_for_completion_interruptible(
525 &ictx->tx.finished);
526 if (retval)
527 pr_err("task interrupted\n");
528 mutex_lock(&ictx->lock);
530 retval = ictx->tx.status;
531 if (retval)
532 pr_err("packet tx failed (%d)\n", retval);
535 kfree(control_req);
538 * Induce a mandatory 5ms delay before returning, as otherwise,
539 * send_packet can get called so rapidly as to overwhelm the device,
540 * particularly on faster systems and/or those with quirky usb.
542 timeout = msecs_to_jiffies(5);
543 set_current_state(TASK_UNINTERRUPTIBLE);
544 schedule_timeout(timeout);
546 return retval;
550 * Sends an associate packet to the iMON 2.4G.
552 * This might not be such a good idea, since it has an id collision with
553 * some versions of the "IR & VFD" combo. The only way to determine if it
554 * is an RF version is to look at the product description string. (Which
555 * we currently do not fetch).
557 static int send_associate_24g(struct imon_context *ictx)
559 int retval;
560 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
561 0x00, 0x00, 0x00, 0x20 };
563 if (!ictx) {
564 pr_err("no context for device\n");
565 return -ENODEV;
568 if (!ictx->dev_present_intf0) {
569 pr_err("no iMON device present\n");
570 return -ENODEV;
573 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
574 retval = send_packet(ictx);
576 return retval;
580 * Sends packets to setup and show clock on iMON display
582 * Arguments: year - last 2 digits of year, month - 1..12,
583 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
584 * hour - 0..23, minute - 0..59, second - 0..59
586 static int send_set_imon_clock(struct imon_context *ictx,
587 unsigned int year, unsigned int month,
588 unsigned int day, unsigned int dow,
589 unsigned int hour, unsigned int minute,
590 unsigned int second)
592 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
593 int retval = 0;
594 int i;
596 if (!ictx) {
597 pr_err("no context for device\n");
598 return -ENODEV;
601 switch (ictx->display_type) {
602 case IMON_DISPLAY_TYPE_LCD:
603 clock_enable_pkt[0][0] = 0x80;
604 clock_enable_pkt[0][1] = year;
605 clock_enable_pkt[0][2] = month-1;
606 clock_enable_pkt[0][3] = day;
607 clock_enable_pkt[0][4] = hour;
608 clock_enable_pkt[0][5] = minute;
609 clock_enable_pkt[0][6] = second;
611 clock_enable_pkt[1][0] = 0x80;
612 clock_enable_pkt[1][1] = 0;
613 clock_enable_pkt[1][2] = 0;
614 clock_enable_pkt[1][3] = 0;
615 clock_enable_pkt[1][4] = 0;
616 clock_enable_pkt[1][5] = 0;
617 clock_enable_pkt[1][6] = 0;
619 if (ictx->product == 0xffdc) {
620 clock_enable_pkt[0][7] = 0x50;
621 clock_enable_pkt[1][7] = 0x51;
622 } else {
623 clock_enable_pkt[0][7] = 0x88;
624 clock_enable_pkt[1][7] = 0x8a;
627 break;
629 case IMON_DISPLAY_TYPE_VFD:
630 clock_enable_pkt[0][0] = year;
631 clock_enable_pkt[0][1] = month-1;
632 clock_enable_pkt[0][2] = day;
633 clock_enable_pkt[0][3] = dow;
634 clock_enable_pkt[0][4] = hour;
635 clock_enable_pkt[0][5] = minute;
636 clock_enable_pkt[0][6] = second;
637 clock_enable_pkt[0][7] = 0x40;
639 clock_enable_pkt[1][0] = 0;
640 clock_enable_pkt[1][1] = 0;
641 clock_enable_pkt[1][2] = 1;
642 clock_enable_pkt[1][3] = 0;
643 clock_enable_pkt[1][4] = 0;
644 clock_enable_pkt[1][5] = 0;
645 clock_enable_pkt[1][6] = 0;
646 clock_enable_pkt[1][7] = 0x42;
648 break;
650 default:
651 return -ENODEV;
654 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
655 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
656 retval = send_packet(ictx);
657 if (retval) {
658 pr_err("send_packet failed for packet %d\n", i);
659 break;
663 return retval;
667 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
669 static ssize_t show_associate_remote(struct device *d,
670 struct device_attribute *attr,
671 char *buf)
673 struct imon_context *ictx = dev_get_drvdata(d);
675 if (!ictx)
676 return -ENODEV;
678 mutex_lock(&ictx->lock);
679 if (ictx->rf_isassociating)
680 strcpy(buf, "associating\n");
681 else
682 strcpy(buf, "closed\n");
684 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
685 "instructions on how to associate your iMON 2.4G DT/LT "
686 "remote\n");
687 mutex_unlock(&ictx->lock);
688 return strlen(buf);
691 static ssize_t store_associate_remote(struct device *d,
692 struct device_attribute *attr,
693 const char *buf, size_t count)
695 struct imon_context *ictx;
697 ictx = dev_get_drvdata(d);
699 if (!ictx)
700 return -ENODEV;
702 mutex_lock(&ictx->lock);
703 ictx->rf_isassociating = true;
704 send_associate_24g(ictx);
705 mutex_unlock(&ictx->lock);
707 return count;
711 * sysfs functions to control internal imon clock
713 static ssize_t show_imon_clock(struct device *d,
714 struct device_attribute *attr, char *buf)
716 struct imon_context *ictx = dev_get_drvdata(d);
717 size_t len;
719 if (!ictx)
720 return -ENODEV;
722 mutex_lock(&ictx->lock);
724 if (!ictx->display_supported) {
725 len = snprintf(buf, PAGE_SIZE, "Not supported.");
726 } else {
727 len = snprintf(buf, PAGE_SIZE,
728 "To set the clock on your iMON display:\n"
729 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
730 "%s", ictx->display_isopen ?
731 "\nNOTE: imon device must be closed\n" : "");
734 mutex_unlock(&ictx->lock);
736 return len;
739 static ssize_t store_imon_clock(struct device *d,
740 struct device_attribute *attr,
741 const char *buf, size_t count)
743 struct imon_context *ictx = dev_get_drvdata(d);
744 ssize_t retval;
745 unsigned int year, month, day, dow, hour, minute, second;
747 if (!ictx)
748 return -ENODEV;
750 mutex_lock(&ictx->lock);
752 if (!ictx->display_supported) {
753 retval = -ENODEV;
754 goto exit;
755 } else if (ictx->display_isopen) {
756 retval = -EBUSY;
757 goto exit;
760 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
761 &hour, &minute, &second) != 7) {
762 retval = -EINVAL;
763 goto exit;
766 if ((month < 1 || month > 12) ||
767 (day < 1 || day > 31) || (dow > 6) ||
768 (hour > 23) || (minute > 59) || (second > 59)) {
769 retval = -EINVAL;
770 goto exit;
773 retval = send_set_imon_clock(ictx, year, month, day, dow,
774 hour, minute, second);
775 if (retval)
776 goto exit;
778 retval = count;
779 exit:
780 mutex_unlock(&ictx->lock);
782 return retval;
786 static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
787 store_imon_clock);
789 static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
790 store_associate_remote);
792 static struct attribute *imon_display_sysfs_entries[] = {
793 &dev_attr_imon_clock.attr,
794 NULL
797 static struct attribute_group imon_display_attr_group = {
798 .attrs = imon_display_sysfs_entries
801 static struct attribute *imon_rf_sysfs_entries[] = {
802 &dev_attr_associate_remote.attr,
803 NULL
806 static struct attribute_group imon_rf_attr_group = {
807 .attrs = imon_rf_sysfs_entries
811 * Writes data to the VFD. The iMON VFD is 2x16 characters
812 * and requires data in 5 consecutive USB interrupt packets,
813 * each packet but the last carrying 7 bytes.
815 * I don't know if the VFD board supports features such as
816 * scrolling, clearing rows, blanking, etc. so at
817 * the caller must provide a full screen of data. If fewer
818 * than 32 bytes are provided spaces will be appended to
819 * generate a full screen.
821 static ssize_t vfd_write(struct file *file, const char *buf,
822 size_t n_bytes, loff_t *pos)
824 int i;
825 int offset;
826 int seq;
827 int retval = 0;
828 struct imon_context *ictx;
829 const unsigned char vfd_packet6[] = {
830 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
832 ictx = file->private_data;
833 if (!ictx) {
834 pr_err("no context for device\n");
835 return -ENODEV;
838 mutex_lock(&ictx->lock);
840 if (!ictx->dev_present_intf0) {
841 pr_err("no iMON device present\n");
842 retval = -ENODEV;
843 goto exit;
846 if (n_bytes <= 0 || n_bytes > 32) {
847 pr_err("invalid payload size\n");
848 retval = -EINVAL;
849 goto exit;
852 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
853 retval = -EFAULT;
854 goto exit;
857 /* Pad with spaces */
858 for (i = n_bytes; i < 32; ++i)
859 ictx->tx.data_buf[i] = ' ';
861 for (i = 32; i < 35; ++i)
862 ictx->tx.data_buf[i] = 0xFF;
864 offset = 0;
865 seq = 0;
867 do {
868 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
869 ictx->usb_tx_buf[7] = (unsigned char) seq;
871 retval = send_packet(ictx);
872 if (retval) {
873 pr_err("send packet failed for packet #%d\n", seq / 2);
874 goto exit;
875 } else {
876 seq += 2;
877 offset += 7;
880 } while (offset < 35);
882 /* Send packet #6 */
883 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
884 ictx->usb_tx_buf[7] = (unsigned char) seq;
885 retval = send_packet(ictx);
886 if (retval)
887 pr_err("send packet failed for packet #%d\n", seq / 2);
889 exit:
890 mutex_unlock(&ictx->lock);
892 return (!retval) ? n_bytes : retval;
896 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
897 * packets. We accept data as 16 hexadecimal digits, followed by a
898 * newline (to make it easy to drive the device from a command-line
899 * -- even though the actual binary data is a bit complicated).
901 * The device itself is not a "traditional" text-mode display. It's
902 * actually a 16x96 pixel bitmap display. That means if you want to
903 * display text, you've got to have your own "font" and translate the
904 * text into bitmaps for display. This is really flexible (you can
905 * display whatever diacritics you need, and so on), but it's also
906 * a lot more complicated than most LCDs...
908 static ssize_t lcd_write(struct file *file, const char *buf,
909 size_t n_bytes, loff_t *pos)
911 int retval = 0;
912 struct imon_context *ictx;
914 ictx = file->private_data;
915 if (!ictx) {
916 pr_err("no context for device\n");
917 return -ENODEV;
920 mutex_lock(&ictx->lock);
922 if (!ictx->display_supported) {
923 pr_err("no iMON display present\n");
924 retval = -ENODEV;
925 goto exit;
928 if (n_bytes != 8) {
929 pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
930 retval = -EINVAL;
931 goto exit;
934 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
935 retval = -EFAULT;
936 goto exit;
939 retval = send_packet(ictx);
940 if (retval) {
941 pr_err("send packet failed!\n");
942 goto exit;
943 } else {
944 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
945 __func__, (int) n_bytes);
947 exit:
948 mutex_unlock(&ictx->lock);
949 return (!retval) ? n_bytes : retval;
953 * Callback function for USB core API: transmit data
955 static void usb_tx_callback(struct urb *urb)
957 struct imon_context *ictx;
959 if (!urb)
960 return;
961 ictx = (struct imon_context *)urb->context;
962 if (!ictx)
963 return;
965 ictx->tx.status = urb->status;
967 /* notify waiters that write has finished */
968 ictx->tx.busy = false;
969 smp_rmb(); /* ensure later readers know we're not busy */
970 complete(&ictx->tx.finished);
974 * report touchscreen input
976 static void imon_touch_display_timeout(unsigned long data)
978 struct imon_context *ictx = (struct imon_context *)data;
980 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
981 return;
983 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
984 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
985 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
986 input_sync(ictx->touch);
990 * iMON IR receivers support two different signal sets -- those used by
991 * the iMON remotes, and those used by the Windows MCE remotes (which is
992 * really just RC-6), but only one or the other at a time, as the signals
993 * are decoded onboard the receiver.
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 unsigned char ir_proto_packet[] = {
1001 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1003 if (rc_type && !(rc_type & rc->allowed_protos))
1004 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1005 "this device does not support\n");
1007 switch (rc_type) {
1008 case RC_TYPE_RC6:
1009 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1010 ir_proto_packet[0] = 0x01;
1011 break;
1012 case RC_TYPE_UNKNOWN:
1013 case RC_TYPE_OTHER:
1014 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
1015 if (!pad_stabilize)
1016 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1017 /* ir_proto_packet[0] = 0x00; // already the default */
1018 rc_type = RC_TYPE_OTHER;
1019 break;
1020 default:
1021 dev_warn(dev, "Unsupported IR protocol specified, overriding "
1022 "to iMON IR protocol\n");
1023 if (!pad_stabilize)
1024 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1025 /* ir_proto_packet[0] = 0x00; // already the default */
1026 rc_type = RC_TYPE_OTHER;
1027 break;
1030 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1032 retval = send_packet(ictx);
1033 if (retval)
1034 goto out;
1036 ictx->rc_type = rc_type;
1037 ictx->pad_mouse = false;
1039 out:
1040 return retval;
1043 static inline int tv2int(const struct timeval *a, const struct timeval *b)
1045 int usecs = 0;
1046 int sec = 0;
1048 if (b->tv_usec > a->tv_usec) {
1049 usecs = 1000000;
1050 sec--;
1053 usecs += a->tv_usec - b->tv_usec;
1055 sec += a->tv_sec - b->tv_sec;
1056 sec *= 1000;
1057 usecs /= 1000;
1058 sec += usecs;
1060 if (sec < 0)
1061 sec = 1000;
1063 return sec;
1067 * The directional pad behaves a bit differently, depending on whether this is
1068 * one of the older ffdc devices or a newer device. Newer devices appear to
1069 * have a higher resolution matrix for more precise mouse movement, but it
1070 * makes things overly sensitive in keyboard mode, so we do some interesting
1071 * contortions to make it less touchy. Older devices run through the same
1072 * routine with shorter timeout and a smaller threshold.
1074 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1076 struct timeval ct;
1077 static struct timeval prev_time = {0, 0};
1078 static struct timeval hit_time = {0, 0};
1079 static int x, y, prev_result, hits;
1080 int result = 0;
1081 int msec, msec_hit;
1083 do_gettimeofday(&ct);
1084 msec = tv2int(&ct, &prev_time);
1085 msec_hit = tv2int(&ct, &hit_time);
1087 if (msec > 100) {
1088 x = 0;
1089 y = 0;
1090 hits = 0;
1093 x += a;
1094 y += b;
1096 prev_time = ct;
1098 if (abs(x) > threshold || abs(y) > threshold) {
1099 if (abs(y) > abs(x))
1100 result = (y > 0) ? 0x7F : 0x80;
1101 else
1102 result = (x > 0) ? 0x7F00 : 0x8000;
1104 x = 0;
1105 y = 0;
1107 if (result == prev_result) {
1108 hits++;
1110 if (hits > 3) {
1111 switch (result) {
1112 case 0x7F:
1113 y = 17 * threshold / 30;
1114 break;
1115 case 0x80:
1116 y -= 17 * threshold / 30;
1117 break;
1118 case 0x7F00:
1119 x = 17 * threshold / 30;
1120 break;
1121 case 0x8000:
1122 x -= 17 * threshold / 30;
1123 break;
1127 if (hits == 2 && msec_hit < timeout) {
1128 result = 0;
1129 hits = 1;
1131 } else {
1132 prev_result = result;
1133 hits = 1;
1134 hit_time = ct;
1138 return result;
1141 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1143 u32 keycode;
1144 u32 release;
1145 bool is_release_code = false;
1147 /* Look for the initial press of a button */
1148 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1149 ictx->rc_toggle = 0x0;
1150 ictx->rc_scancode = scancode;
1152 /* Look for the release of a button */
1153 if (keycode == KEY_RESERVED) {
1154 release = scancode & ~0x4000;
1155 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1156 if (keycode != KEY_RESERVED)
1157 is_release_code = true;
1160 ictx->release_code = is_release_code;
1162 return keycode;
1165 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1167 u32 keycode;
1169 #define MCE_KEY_MASK 0x7000
1170 #define MCE_TOGGLE_BIT 0x8000
1173 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1174 * (the toggle bit flipping between alternating key presses), while
1175 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1176 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1177 * but we can't or them into all codes, as some keys are decoded in
1178 * a different way w/o the same use of the toggle bit...
1180 if (scancode & 0x80000000)
1181 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1183 ictx->rc_scancode = scancode;
1184 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1186 /* not used in mce mode, but make sure we know its false */
1187 ictx->release_code = false;
1189 return keycode;
1192 static u32 imon_panel_key_lookup(u64 code)
1194 int i;
1195 u32 keycode = KEY_RESERVED;
1197 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1198 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1199 keycode = imon_panel_key_table[i].keycode;
1200 break;
1204 return keycode;
1207 static bool imon_mouse_event(struct imon_context *ictx,
1208 unsigned char *buf, int len)
1210 char rel_x = 0x00, rel_y = 0x00;
1211 u8 right_shift = 1;
1212 bool mouse_input = true;
1213 int dir = 0;
1214 unsigned long flags;
1216 spin_lock_irqsave(&ictx->kc_lock, flags);
1218 /* newer iMON device PAD or mouse button */
1219 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1220 rel_x = buf[2];
1221 rel_y = buf[3];
1222 right_shift = 1;
1223 /* 0xffdc iMON PAD or mouse button input */
1224 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1225 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1226 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1227 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1228 if (buf[0] & 0x02)
1229 rel_x |= ~0x0f;
1230 rel_x = rel_x + rel_x / 2;
1231 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1232 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1233 if (buf[0] & 0x01)
1234 rel_y |= ~0x0f;
1235 rel_y = rel_y + rel_y / 2;
1236 right_shift = 2;
1237 /* some ffdc devices decode mouse buttons differently... */
1238 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1239 right_shift = 2;
1240 /* ch+/- buttons, which we use for an emulated scroll wheel */
1241 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1242 dir = 1;
1243 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1244 dir = -1;
1245 } else
1246 mouse_input = false;
1248 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1250 if (mouse_input) {
1251 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1253 if (dir) {
1254 input_report_rel(ictx->idev, REL_WHEEL, dir);
1255 } else if (rel_x || rel_y) {
1256 input_report_rel(ictx->idev, REL_X, rel_x);
1257 input_report_rel(ictx->idev, REL_Y, rel_y);
1258 } else {
1259 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1260 input_report_key(ictx->idev, BTN_RIGHT,
1261 buf[1] >> right_shift & 0x1);
1263 input_sync(ictx->idev);
1264 spin_lock_irqsave(&ictx->kc_lock, flags);
1265 ictx->last_keycode = ictx->kc;
1266 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1269 return mouse_input;
1272 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1274 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1275 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1276 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1277 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1278 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1279 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1280 input_sync(ictx->touch);
1283 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1285 int dir = 0;
1286 char rel_x = 0x00, rel_y = 0x00;
1287 u16 timeout, threshold;
1288 u32 scancode = KEY_RESERVED;
1289 unsigned long flags;
1292 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1293 * contain a position coordinate (x,y), with each component ranging
1294 * from -14 to 14. We want to down-sample this to only 4 discrete values
1295 * for up/down/left/right arrow keys. Also, when you get too close to
1296 * diagonals, it has a tendancy to jump back and forth, so lets try to
1297 * ignore when they get too close.
1299 if (ictx->product != 0xffdc) {
1300 /* first, pad to 8 bytes so it conforms with everything else */
1301 buf[5] = buf[6] = buf[7] = 0;
1302 timeout = 500; /* in msecs */
1303 /* (2*threshold) x (2*threshold) square */
1304 threshold = pad_thresh ? pad_thresh : 28;
1305 rel_x = buf[2];
1306 rel_y = buf[3];
1308 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1309 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1310 dir = stabilize((int)rel_x, (int)rel_y,
1311 timeout, threshold);
1312 if (!dir) {
1313 spin_lock_irqsave(&ictx->kc_lock,
1314 flags);
1315 ictx->kc = KEY_UNKNOWN;
1316 spin_unlock_irqrestore(&ictx->kc_lock,
1317 flags);
1318 return;
1320 buf[2] = dir & 0xFF;
1321 buf[3] = (dir >> 8) & 0xFF;
1322 scancode = be32_to_cpu(*((u32 *)buf));
1324 } else {
1326 * Hack alert: instead of using keycodes, we have
1327 * to use hard-coded scancodes here...
1329 if (abs(rel_y) > abs(rel_x)) {
1330 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1331 buf[3] = 0;
1332 if (rel_y > 0)
1333 scancode = 0x01007f00; /* KEY_DOWN */
1334 else
1335 scancode = 0x01008000; /* KEY_UP */
1336 } else {
1337 buf[2] = 0;
1338 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1339 if (rel_x > 0)
1340 scancode = 0x0100007f; /* KEY_RIGHT */
1341 else
1342 scancode = 0x01000080; /* KEY_LEFT */
1347 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1348 * device (15c2:ffdc). The remote generates various codes from
1349 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1350 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1351 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1352 * reversed endianess. Extract direction from buffer, rotate endianess,
1353 * adjust sign and feed the values into stabilize(). The resulting codes
1354 * will be 0x01008000, 0x01007F00, which match the newer devices.
1356 } else {
1357 timeout = 10; /* in msecs */
1358 /* (2*threshold) x (2*threshold) square */
1359 threshold = pad_thresh ? pad_thresh : 15;
1361 /* buf[1] is x */
1362 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1363 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1364 if (buf[0] & 0x02)
1365 rel_x |= ~0x10+1;
1366 /* buf[2] is y */
1367 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1368 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1369 if (buf[0] & 0x01)
1370 rel_y |= ~0x10+1;
1372 buf[0] = 0x01;
1373 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1375 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1376 dir = stabilize((int)rel_x, (int)rel_y,
1377 timeout, threshold);
1378 if (!dir) {
1379 spin_lock_irqsave(&ictx->kc_lock, flags);
1380 ictx->kc = KEY_UNKNOWN;
1381 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1382 return;
1384 buf[2] = dir & 0xFF;
1385 buf[3] = (dir >> 8) & 0xFF;
1386 scancode = be32_to_cpu(*((u32 *)buf));
1387 } else {
1389 * Hack alert: instead of using keycodes, we have
1390 * to use hard-coded scancodes here...
1392 if (abs(rel_y) > abs(rel_x)) {
1393 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1394 buf[3] = 0;
1395 if (rel_y > 0)
1396 scancode = 0x01007f00; /* KEY_DOWN */
1397 else
1398 scancode = 0x01008000; /* KEY_UP */
1399 } else {
1400 buf[2] = 0;
1401 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1402 if (rel_x > 0)
1403 scancode = 0x0100007f; /* KEY_RIGHT */
1404 else
1405 scancode = 0x01000080; /* KEY_LEFT */
1410 if (scancode) {
1411 spin_lock_irqsave(&ictx->kc_lock, flags);
1412 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1413 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1418 * figure out if these is a press or a release. We don't actually
1419 * care about repeats, as those will be auto-generated within the IR
1420 * subsystem for repeating scancodes.
1422 static int imon_parse_press_type(struct imon_context *ictx,
1423 unsigned char *buf, u8 ktype)
1425 int press_type = 0;
1426 unsigned long flags;
1428 spin_lock_irqsave(&ictx->kc_lock, flags);
1430 /* key release of 0x02XXXXXX key */
1431 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1432 ictx->kc = ictx->last_keycode;
1434 /* mouse button release on (some) 0xffdc devices */
1435 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1436 buf[2] == 0x81 && buf[3] == 0xb7)
1437 ictx->kc = ictx->last_keycode;
1439 /* mouse button release on (some other) 0xffdc devices */
1440 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1441 buf[2] == 0x81 && buf[3] == 0xb7)
1442 ictx->kc = ictx->last_keycode;
1444 /* mce-specific button handling, no keyup events */
1445 else if (ktype == IMON_KEY_MCE) {
1446 ictx->rc_toggle = buf[2];
1447 press_type = 1;
1449 /* incoherent or irrelevant data */
1450 } else if (ictx->kc == KEY_RESERVED)
1451 press_type = -EINVAL;
1453 /* key release of 0xXXXXXXb7 key */
1454 else if (ictx->release_code)
1455 press_type = 0;
1457 /* this is a button press */
1458 else
1459 press_type = 1;
1461 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1463 return press_type;
1467 * Process the incoming packet
1469 static void imon_incoming_packet(struct imon_context *ictx,
1470 struct urb *urb, int intf)
1472 int len = urb->actual_length;
1473 unsigned char *buf = urb->transfer_buffer;
1474 struct device *dev = ictx->dev;
1475 unsigned long flags;
1476 u32 kc;
1477 bool norelease = false;
1478 int i;
1479 u64 scancode;
1480 int press_type = 0;
1481 int msec;
1482 struct timeval t;
1483 static struct timeval prev_time = { 0, 0 };
1484 u8 ktype;
1486 /* filter out junk data on the older 0xffdc imon devices */
1487 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1488 return;
1490 /* Figure out what key was pressed */
1491 if (len == 8 && buf[7] == 0xee) {
1492 scancode = be64_to_cpu(*((u64 *)buf));
1493 ktype = IMON_KEY_PANEL;
1494 kc = imon_panel_key_lookup(scancode);
1495 } else {
1496 scancode = be32_to_cpu(*((u32 *)buf));
1497 if (ictx->rc_type == RC_TYPE_RC6) {
1498 ktype = IMON_KEY_IMON;
1499 if (buf[0] == 0x80)
1500 ktype = IMON_KEY_MCE;
1501 kc = imon_mce_key_lookup(ictx, scancode);
1502 } else {
1503 ktype = IMON_KEY_IMON;
1504 kc = imon_remote_key_lookup(ictx, scancode);
1508 spin_lock_irqsave(&ictx->kc_lock, flags);
1509 /* keyboard/mouse mode toggle button */
1510 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1511 ictx->last_keycode = kc;
1512 if (!nomouse) {
1513 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1514 dev_dbg(dev, "toggling to %s mode\n",
1515 ictx->pad_mouse ? "mouse" : "keyboard");
1516 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1517 return;
1518 } else {
1519 ictx->pad_mouse = false;
1520 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1524 ictx->kc = kc;
1525 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1527 /* send touchscreen events through input subsystem if touchpad data */
1528 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1529 buf[7] == 0x86) {
1530 imon_touch_event(ictx, buf);
1531 return;
1533 /* look for mouse events with pad in mouse mode */
1534 } else if (ictx->pad_mouse) {
1535 if (imon_mouse_event(ictx, buf, len))
1536 return;
1539 /* Now for some special handling to convert pad input to arrow keys */
1540 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1541 ((len == 8) && (buf[0] & 0x40) &&
1542 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1543 len = 8;
1544 imon_pad_to_keys(ictx, buf);
1545 norelease = true;
1548 if (debug) {
1549 printk(KERN_INFO "intf%d decoded packet: ", intf);
1550 for (i = 0; i < len; ++i)
1551 printk("%02x ", buf[i]);
1552 printk("\n");
1555 press_type = imon_parse_press_type(ictx, buf, ktype);
1556 if (press_type < 0)
1557 goto not_input_data;
1559 spin_lock_irqsave(&ictx->kc_lock, flags);
1560 if (ictx->kc == KEY_UNKNOWN)
1561 goto unknown_key;
1562 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1564 if (ktype != IMON_KEY_PANEL) {
1565 if (press_type == 0)
1566 rc_keyup(ictx->rdev);
1567 else {
1568 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
1569 spin_lock_irqsave(&ictx->kc_lock, flags);
1570 ictx->last_keycode = ictx->kc;
1571 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1573 return;
1576 /* Only panel type events left to process now */
1577 spin_lock_irqsave(&ictx->kc_lock, flags);
1579 /* KEY_MUTE repeats from knob need to be suppressed */
1580 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
1581 do_gettimeofday(&t);
1582 msec = tv2int(&t, &prev_time);
1583 prev_time = t;
1584 if (msec < ictx->idev->rep[REP_DELAY]) {
1585 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1586 return;
1589 kc = ictx->kc;
1591 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1593 input_report_key(ictx->idev, kc, press_type);
1594 input_sync(ictx->idev);
1596 /* panel keys don't generate a release */
1597 input_report_key(ictx->idev, kc, 0);
1598 input_sync(ictx->idev);
1600 ictx->last_keycode = kc;
1602 return;
1604 unknown_key:
1605 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1606 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
1607 (long long)scancode);
1608 return;
1610 not_input_data:
1611 if (len != 8) {
1612 dev_warn(dev, "imon %s: invalid incoming packet "
1613 "size (len = %d, intf%d)\n", __func__, len, intf);
1614 return;
1617 /* iMON 2.4G associate frame */
1618 if (buf[0] == 0x00 &&
1619 buf[2] == 0xFF && /* REFID */
1620 buf[3] == 0xFF &&
1621 buf[4] == 0xFF &&
1622 buf[5] == 0xFF && /* iMON 2.4G */
1623 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1624 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1625 dev_warn(dev, "%s: remote associated refid=%02X\n",
1626 __func__, buf[1]);
1627 ictx->rf_isassociating = false;
1632 * Callback function for USB core API: receive data
1634 static void usb_rx_callback_intf0(struct urb *urb)
1636 struct imon_context *ictx;
1637 int intfnum = 0;
1639 if (!urb)
1640 return;
1642 ictx = (struct imon_context *)urb->context;
1643 if (!ictx)
1644 return;
1646 switch (urb->status) {
1647 case -ENOENT: /* usbcore unlink successful! */
1648 return;
1650 case -ESHUTDOWN: /* transport endpoint was shut down */
1651 break;
1653 case 0:
1654 imon_incoming_packet(ictx, urb, intfnum);
1655 break;
1657 default:
1658 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1659 __func__, urb->status);
1660 break;
1663 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1666 static void usb_rx_callback_intf1(struct urb *urb)
1668 struct imon_context *ictx;
1669 int intfnum = 1;
1671 if (!urb)
1672 return;
1674 ictx = (struct imon_context *)urb->context;
1675 if (!ictx)
1676 return;
1678 switch (urb->status) {
1679 case -ENOENT: /* usbcore unlink successful! */
1680 return;
1682 case -ESHUTDOWN: /* transport endpoint was shut down */
1683 break;
1685 case 0:
1686 imon_incoming_packet(ictx, urb, intfnum);
1687 break;
1689 default:
1690 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1691 __func__, urb->status);
1692 break;
1695 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1699 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1700 * devices, and all of them constantly spew interrupts, even when there
1701 * is no actual data to report. However, byte 6 of this buffer looks like
1702 * its unique across device variants, so we're trying to key off that to
1703 * figure out which display type (if any) and what IR protocol the device
1704 * actually supports. These devices have their IR protocol hard-coded into
1705 * their firmware, they can't be changed on the fly like the newer hardware.
1707 static void imon_get_ffdc_type(struct imon_context *ictx)
1709 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1710 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1711 u64 allowed_protos = RC_TYPE_OTHER;
1713 switch (ffdc_cfg_byte) {
1714 /* iMON Knob, no display, iMON IR + vol knob */
1715 case 0x21:
1716 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1717 ictx->display_supported = false;
1718 break;
1719 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1720 case 0x4e:
1721 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1722 ictx->display_supported = false;
1723 ictx->rf_device = true;
1724 break;
1725 /* iMON VFD, no IR (does have vol knob tho) */
1726 case 0x35:
1727 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1728 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1729 break;
1730 /* iMON VFD, iMON IR */
1731 case 0x24:
1732 case 0x85:
1733 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1734 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1735 break;
1736 /* iMON VFD, MCE IR */
1737 case 0x9e:
1738 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1739 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1740 allowed_protos = RC_TYPE_RC6;
1741 break;
1742 /* iMON LCD, MCE IR */
1743 case 0x9f:
1744 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1745 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1746 allowed_protos = RC_TYPE_RC6;
1747 break;
1748 default:
1749 dev_info(ictx->dev, "Unknown 0xffdc device, "
1750 "defaulting to VFD and iMON IR");
1751 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1752 break;
1755 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1757 ictx->display_type = detected_display_type;
1758 ictx->rc_type = allowed_protos;
1761 static void imon_set_display_type(struct imon_context *ictx)
1763 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1766 * Try to auto-detect the type of display if the user hasn't set
1767 * it by hand via the display_type modparam. Default is VFD.
1770 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1771 switch (ictx->product) {
1772 case 0xffdc:
1773 /* set in imon_get_ffdc_type() */
1774 configured_display_type = ictx->display_type;
1775 break;
1776 case 0x0034:
1777 case 0x0035:
1778 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1779 break;
1780 case 0x0038:
1781 case 0x0039:
1782 case 0x0045:
1783 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1784 break;
1785 case 0x003c:
1786 case 0x0041:
1787 case 0x0042:
1788 case 0x0043:
1789 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1790 ictx->display_supported = false;
1791 break;
1792 case 0x0036:
1793 case 0x0044:
1794 default:
1795 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1796 break;
1798 } else {
1799 configured_display_type = display_type;
1800 if (display_type == IMON_DISPLAY_TYPE_NONE)
1801 ictx->display_supported = false;
1802 else
1803 ictx->display_supported = true;
1804 dev_info(ictx->dev, "%s: overriding display type to %d via "
1805 "modparam\n", __func__, display_type);
1808 ictx->display_type = configured_display_type;
1811 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1813 struct rc_dev *rdev;
1814 int ret;
1815 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1816 0x00, 0x00, 0x00, 0x88 };
1818 rdev = rc_allocate_device();
1819 if (!rdev) {
1820 dev_err(ictx->dev, "remote control dev allocation failed\n");
1821 goto out;
1824 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1825 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1826 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1827 sizeof(ictx->phys_rdev));
1828 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1830 rdev->input_name = ictx->name_rdev;
1831 rdev->input_phys = ictx->phys_rdev;
1832 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1833 rdev->dev.parent = ictx->dev;
1835 rdev->priv = ictx;
1836 rdev->driver_type = RC_DRIVER_SCANCODE;
1837 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
1838 rdev->change_protocol = imon_ir_change_protocol;
1839 rdev->driver_name = MOD_NAME;
1841 /* Enable front-panel buttons and/or knobs */
1842 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1843 ret = send_packet(ictx);
1844 /* Not fatal, but warn about it */
1845 if (ret)
1846 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1848 if (ictx->product == 0xffdc) {
1849 imon_get_ffdc_type(ictx);
1850 rdev->allowed_protos = ictx->rc_type;
1853 imon_set_display_type(ictx);
1855 if (ictx->rc_type == RC_TYPE_RC6)
1856 rdev->map_name = RC_MAP_IMON_MCE;
1857 else
1858 rdev->map_name = RC_MAP_IMON_PAD;
1860 ret = rc_register_device(rdev);
1861 if (ret < 0) {
1862 dev_err(ictx->dev, "remote input dev register failed\n");
1863 goto out;
1866 return rdev;
1868 out:
1869 rc_free_device(rdev);
1870 return NULL;
1873 static struct input_dev *imon_init_idev(struct imon_context *ictx)
1875 struct input_dev *idev;
1876 int ret, i;
1878 idev = input_allocate_device();
1879 if (!idev) {
1880 dev_err(ictx->dev, "input dev allocation failed\n");
1881 goto out;
1884 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1885 "iMON Panel, Knob and Mouse(%04x:%04x)",
1886 ictx->vendor, ictx->product);
1887 idev->name = ictx->name_idev;
1889 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1890 sizeof(ictx->phys_idev));
1891 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
1892 idev->phys = ictx->phys_idev;
1894 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
1896 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1897 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1898 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1899 BIT_MASK(REL_WHEEL);
1901 /* panel and/or knob code support */
1902 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1903 u32 kc = imon_panel_key_table[i].keycode;
1904 __set_bit(kc, idev->keybit);
1907 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1908 idev->dev.parent = ictx->dev;
1909 input_set_drvdata(idev, ictx);
1911 ret = input_register_device(idev);
1912 if (ret < 0) {
1913 dev_err(ictx->dev, "input dev register failed\n");
1914 goto out;
1917 return idev;
1919 out:
1920 input_free_device(idev);
1921 return NULL;
1924 static struct input_dev *imon_init_touch(struct imon_context *ictx)
1926 struct input_dev *touch;
1927 int ret;
1929 touch = input_allocate_device();
1930 if (!touch) {
1931 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1932 goto touch_alloc_failed;
1935 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1936 "iMON USB Touchscreen (%04x:%04x)",
1937 ictx->vendor, ictx->product);
1938 touch->name = ictx->name_touch;
1940 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1941 sizeof(ictx->phys_touch));
1942 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
1943 touch->phys = ictx->phys_touch;
1945 touch->evbit[0] =
1946 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1947 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1948 BIT_MASK(BTN_TOUCH);
1949 input_set_abs_params(touch, ABS_X,
1950 0x00, 0xfff, 0, 0);
1951 input_set_abs_params(touch, ABS_Y,
1952 0x00, 0xfff, 0, 0);
1954 input_set_drvdata(touch, ictx);
1956 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1957 touch->dev.parent = ictx->dev;
1958 ret = input_register_device(touch);
1959 if (ret < 0) {
1960 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1961 goto touch_register_failed;
1964 return touch;
1966 touch_register_failed:
1967 input_free_device(ictx->touch);
1969 touch_alloc_failed:
1970 return NULL;
1973 static bool imon_find_endpoints(struct imon_context *ictx,
1974 struct usb_host_interface *iface_desc)
1976 struct usb_endpoint_descriptor *ep;
1977 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1978 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1979 int ifnum = iface_desc->desc.bInterfaceNumber;
1980 int num_endpts = iface_desc->desc.bNumEndpoints;
1981 int i, ep_dir, ep_type;
1982 bool ir_ep_found = false;
1983 bool display_ep_found = false;
1984 bool tx_control = false;
1987 * Scan the endpoint list and set:
1988 * first input endpoint = IR endpoint
1989 * first output endpoint = display endpoint
1991 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
1992 ep = &iface_desc->endpoint[i].desc;
1993 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1994 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1996 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
1997 ep_type == USB_ENDPOINT_XFER_INT) {
1999 rx_endpoint = ep;
2000 ir_ep_found = true;
2001 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2003 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2004 ep_type == USB_ENDPOINT_XFER_INT) {
2005 tx_endpoint = ep;
2006 display_ep_found = true;
2007 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2011 if (ifnum == 0) {
2012 ictx->rx_endpoint_intf0 = rx_endpoint;
2014 * tx is used to send characters to lcd/vfd, associate RF
2015 * remotes, set IR protocol, and maybe more...
2017 ictx->tx_endpoint = tx_endpoint;
2018 } else {
2019 ictx->rx_endpoint_intf1 = rx_endpoint;
2023 * If we didn't find a display endpoint, this is probably one of the
2024 * newer iMON devices that use control urb instead of interrupt
2026 if (!display_ep_found) {
2027 tx_control = true;
2028 display_ep_found = true;
2029 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2030 "interface OUT endpoint\n", __func__);
2034 * Some iMON receivers have no display. Unfortunately, it seems
2035 * that SoundGraph recycles device IDs between devices both with
2036 * and without... :\
2038 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2039 display_ep_found = false;
2040 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2044 * iMON Touch devices have a VGA touchscreen, but no "display", as
2045 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2047 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2048 display_ep_found = false;
2049 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2052 /* Input endpoint is mandatory */
2053 if (!ir_ep_found)
2054 pr_err("no valid input (IR) endpoint found\n");
2056 ictx->tx_control = tx_control;
2058 if (display_ep_found)
2059 ictx->display_supported = true;
2061 return ir_ep_found;
2065 static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2067 struct imon_context *ictx;
2068 struct urb *rx_urb;
2069 struct urb *tx_urb;
2070 struct device *dev = &intf->dev;
2071 struct usb_host_interface *iface_desc;
2072 int ret = -ENOMEM;
2074 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2075 if (!ictx) {
2076 dev_err(dev, "%s: kzalloc failed for context", __func__);
2077 goto exit;
2079 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2080 if (!rx_urb) {
2081 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2082 goto rx_urb_alloc_failed;
2084 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2085 if (!tx_urb) {
2086 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2087 __func__);
2088 goto tx_urb_alloc_failed;
2091 mutex_init(&ictx->lock);
2092 spin_lock_init(&ictx->kc_lock);
2094 mutex_lock(&ictx->lock);
2096 ictx->dev = dev;
2097 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2098 ictx->dev_present_intf0 = true;
2099 ictx->rx_urb_intf0 = rx_urb;
2100 ictx->tx_urb = tx_urb;
2101 ictx->rf_device = false;
2103 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2104 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2106 ret = -ENODEV;
2107 iface_desc = intf->cur_altsetting;
2108 if (!imon_find_endpoints(ictx, iface_desc)) {
2109 goto find_endpoint_failed;
2112 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2113 usb_rcvintpipe(ictx->usbdev_intf0,
2114 ictx->rx_endpoint_intf0->bEndpointAddress),
2115 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2116 usb_rx_callback_intf0, ictx,
2117 ictx->rx_endpoint_intf0->bInterval);
2119 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2120 if (ret) {
2121 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2122 goto urb_submit_failed;
2125 ictx->idev = imon_init_idev(ictx);
2126 if (!ictx->idev) {
2127 dev_err(dev, "%s: input device setup failed\n", __func__);
2128 goto idev_setup_failed;
2131 ictx->rdev = imon_init_rdev(ictx);
2132 if (!ictx->rdev) {
2133 dev_err(dev, "%s: rc device setup failed\n", __func__);
2134 goto rdev_setup_failed;
2137 return ictx;
2139 rdev_setup_failed:
2140 input_unregister_device(ictx->idev);
2141 idev_setup_failed:
2142 usb_kill_urb(ictx->rx_urb_intf0);
2143 urb_submit_failed:
2144 find_endpoint_failed:
2145 mutex_unlock(&ictx->lock);
2146 usb_free_urb(tx_urb);
2147 tx_urb_alloc_failed:
2148 usb_free_urb(rx_urb);
2149 rx_urb_alloc_failed:
2150 kfree(ictx);
2151 exit:
2152 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2154 return NULL;
2157 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2158 struct imon_context *ictx)
2160 struct urb *rx_urb;
2161 struct usb_host_interface *iface_desc;
2162 int ret = -ENOMEM;
2164 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2165 if (!rx_urb) {
2166 pr_err("usb_alloc_urb failed for IR urb\n");
2167 goto rx_urb_alloc_failed;
2170 mutex_lock(&ictx->lock);
2172 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2173 init_timer(&ictx->ttimer);
2174 ictx->ttimer.data = (unsigned long)ictx;
2175 ictx->ttimer.function = imon_touch_display_timeout;
2178 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2179 ictx->dev_present_intf1 = true;
2180 ictx->rx_urb_intf1 = rx_urb;
2182 ret = -ENODEV;
2183 iface_desc = intf->cur_altsetting;
2184 if (!imon_find_endpoints(ictx, iface_desc))
2185 goto find_endpoint_failed;
2187 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2188 ictx->touch = imon_init_touch(ictx);
2189 if (!ictx->touch)
2190 goto touch_setup_failed;
2191 } else
2192 ictx->touch = NULL;
2194 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2195 usb_rcvintpipe(ictx->usbdev_intf1,
2196 ictx->rx_endpoint_intf1->bEndpointAddress),
2197 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2198 usb_rx_callback_intf1, ictx,
2199 ictx->rx_endpoint_intf1->bInterval);
2201 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2203 if (ret) {
2204 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2205 goto urb_submit_failed;
2208 return ictx;
2210 urb_submit_failed:
2211 if (ictx->touch)
2212 input_unregister_device(ictx->touch);
2213 touch_setup_failed:
2214 find_endpoint_failed:
2215 mutex_unlock(&ictx->lock);
2216 usb_free_urb(rx_urb);
2217 rx_urb_alloc_failed:
2218 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2220 return NULL;
2223 static void imon_init_display(struct imon_context *ictx,
2224 struct usb_interface *intf)
2226 int ret;
2228 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2230 /* set up sysfs entry for built-in clock */
2231 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2232 if (ret)
2233 dev_err(ictx->dev, "Could not create display sysfs "
2234 "entries(%d)", ret);
2236 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2237 ret = usb_register_dev(intf, &imon_lcd_class);
2238 else
2239 ret = usb_register_dev(intf, &imon_vfd_class);
2240 if (ret)
2241 /* Not a fatal error, so ignore */
2242 dev_info(ictx->dev, "could not get a minor number for "
2243 "display\n");
2248 * Callback function for USB core API: Probe
2250 static int __devinit imon_probe(struct usb_interface *interface,
2251 const struct usb_device_id *id)
2253 struct usb_device *usbdev = NULL;
2254 struct usb_host_interface *iface_desc = NULL;
2255 struct usb_interface *first_if;
2256 struct device *dev = &interface->dev;
2257 int ifnum, code_length, sysfs_err;
2258 int ret = 0;
2259 struct imon_context *ictx = NULL;
2260 struct imon_context *first_if_ctx = NULL;
2261 u16 vendor, product;
2263 code_length = BUF_CHUNK_SIZE * 8;
2265 usbdev = usb_get_dev(interface_to_usbdev(interface));
2266 iface_desc = interface->cur_altsetting;
2267 ifnum = iface_desc->desc.bInterfaceNumber;
2268 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2269 product = le16_to_cpu(usbdev->descriptor.idProduct);
2271 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2272 __func__, vendor, product, ifnum);
2274 /* prevent races probing devices w/multiple interfaces */
2275 mutex_lock(&driver_lock);
2277 first_if = usb_ifnum_to_if(usbdev, 0);
2278 first_if_ctx = usb_get_intfdata(first_if);
2280 if (ifnum == 0) {
2281 ictx = imon_init_intf0(interface);
2282 if (!ictx) {
2283 pr_err("failed to initialize context!\n");
2284 ret = -ENODEV;
2285 goto fail;
2288 } else {
2289 /* this is the secondary interface on the device */
2290 ictx = imon_init_intf1(interface, first_if_ctx);
2291 if (!ictx) {
2292 pr_err("failed to attach to context!\n");
2293 ret = -ENODEV;
2294 goto fail;
2299 usb_set_intfdata(interface, ictx);
2301 if (ifnum == 0) {
2302 if (product == 0xffdc && ictx->rf_device) {
2303 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2304 &imon_rf_attr_group);
2305 if (sysfs_err)
2306 pr_err("Could not create RF sysfs entries(%d)\n",
2307 sysfs_err);
2310 if (ictx->display_supported)
2311 imon_init_display(ictx, interface);
2314 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2315 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2316 usbdev->bus->busnum, usbdev->devnum);
2318 mutex_unlock(&ictx->lock);
2319 mutex_unlock(&driver_lock);
2321 return 0;
2323 fail:
2324 mutex_unlock(&driver_lock);
2325 dev_err(dev, "unable to register, err %d\n", ret);
2327 return ret;
2331 * Callback function for USB core API: disconnect
2333 static void __devexit imon_disconnect(struct usb_interface *interface)
2335 struct imon_context *ictx;
2336 struct device *dev;
2337 int ifnum;
2339 /* prevent races with multi-interface device probing and display_open */
2340 mutex_lock(&driver_lock);
2342 ictx = usb_get_intfdata(interface);
2343 dev = ictx->dev;
2344 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2346 mutex_lock(&ictx->lock);
2349 * sysfs_remove_group is safe to call even if sysfs_create_group
2350 * hasn't been called
2352 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2353 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2355 usb_set_intfdata(interface, NULL);
2357 /* Abort ongoing write */
2358 if (ictx->tx.busy) {
2359 usb_kill_urb(ictx->tx_urb);
2360 complete_all(&ictx->tx.finished);
2363 if (ifnum == 0) {
2364 ictx->dev_present_intf0 = false;
2365 usb_kill_urb(ictx->rx_urb_intf0);
2366 input_unregister_device(ictx->idev);
2367 rc_unregister_device(ictx->rdev);
2368 if (ictx->display_supported) {
2369 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2370 usb_deregister_dev(interface, &imon_lcd_class);
2371 else
2372 usb_deregister_dev(interface, &imon_vfd_class);
2374 } else {
2375 ictx->dev_present_intf1 = false;
2376 usb_kill_urb(ictx->rx_urb_intf1);
2377 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2378 input_unregister_device(ictx->touch);
2381 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1) {
2382 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA)
2383 del_timer_sync(&ictx->ttimer);
2384 mutex_unlock(&ictx->lock);
2385 if (!ictx->display_isopen)
2386 free_imon_context(ictx);
2387 } else
2388 mutex_unlock(&ictx->lock);
2390 mutex_unlock(&driver_lock);
2392 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2393 __func__, ifnum);
2396 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2398 struct imon_context *ictx = usb_get_intfdata(intf);
2399 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2401 if (ifnum == 0)
2402 usb_kill_urb(ictx->rx_urb_intf0);
2403 else
2404 usb_kill_urb(ictx->rx_urb_intf1);
2406 return 0;
2409 static int imon_resume(struct usb_interface *intf)
2411 int rc = 0;
2412 struct imon_context *ictx = usb_get_intfdata(intf);
2413 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2415 if (ifnum == 0) {
2416 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2417 usb_rcvintpipe(ictx->usbdev_intf0,
2418 ictx->rx_endpoint_intf0->bEndpointAddress),
2419 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2420 usb_rx_callback_intf0, ictx,
2421 ictx->rx_endpoint_intf0->bInterval);
2423 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2425 } else {
2426 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2427 usb_rcvintpipe(ictx->usbdev_intf1,
2428 ictx->rx_endpoint_intf1->bEndpointAddress),
2429 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2430 usb_rx_callback_intf1, ictx,
2431 ictx->rx_endpoint_intf1->bInterval);
2433 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2436 return rc;
2439 static int __init imon_init(void)
2441 int rc;
2443 rc = usb_register(&imon_driver);
2444 if (rc) {
2445 pr_err("usb register failed(%d)\n", rc);
2446 rc = -ENODEV;
2449 return rc;
2452 static void __exit imon_exit(void)
2454 usb_deregister(&imon_driver);
2457 module_init(imon_init);
2458 module_exit(imon_exit);