usb: dwc3: ep0: add handling for unaligned OUT transfers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / rc / imon.c
blob6bc35eeb653bf0cbc3ff8a82766481e862d9b198
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_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 { 0x00000001ffffffeell, KEY_MUTE },
311 { 0x0000000fffffffeell, KEY_MEDIA },
312 { 0x00000012ffffffeell, KEY_UP },
313 { 0x00000013ffffffeell, KEY_DOWN },
314 { 0x00000014ffffffeell, KEY_LEFT },
315 { 0x00000015ffffffeell, KEY_RIGHT },
316 { 0x00000016ffffffeell, KEY_ENTER },
317 { 0x00000017ffffffeell, KEY_ESC },
318 /* iMON Knob values */
319 { 0x000100ffffffffeell, KEY_VOLUMEUP },
320 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
321 { 0x000008ffffffffeell, KEY_MUTE },
324 /* to prevent races between open() and disconnect(), probing, etc */
325 static DEFINE_MUTEX(driver_lock);
327 /* Module bookkeeping bits */
328 MODULE_AUTHOR(MOD_AUTHOR);
329 MODULE_DESCRIPTION(MOD_DESC);
330 MODULE_VERSION(MOD_VERSION);
331 MODULE_LICENSE("GPL");
332 MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
334 static bool debug;
335 module_param(debug, bool, S_IRUGO | S_IWUSR);
336 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
338 /* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
339 static int display_type;
340 module_param(display_type, int, S_IRUGO);
341 MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
342 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
344 static int pad_stabilize = 1;
345 module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
346 MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
347 "presses in arrow key mode. 0=disable, 1=enable (default).");
350 * In certain use cases, mouse mode isn't really helpful, and could actually
351 * cause confusion, so allow disabling it when the IR device is open.
353 static bool nomouse;
354 module_param(nomouse, bool, S_IRUGO | S_IWUSR);
355 MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
356 "open. 0=don't disable, 1=disable. (default: don't disable)");
358 /* threshold at which a pad push registers as an arrow key in kbd mode */
359 static int pad_thresh;
360 module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
361 MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
362 "arrow key in kbd mode (default: 28)");
365 static void free_imon_context(struct imon_context *ictx)
367 struct device *dev = ictx->dev;
369 usb_free_urb(ictx->tx_urb);
370 usb_free_urb(ictx->rx_urb_intf0);
371 usb_free_urb(ictx->rx_urb_intf1);
372 kfree(ictx);
374 dev_dbg(dev, "%s: iMON context freed\n", __func__);
378 * Called when the Display device (e.g. /dev/lcd0)
379 * is opened by the application.
381 static int display_open(struct inode *inode, struct file *file)
383 struct usb_interface *interface;
384 struct imon_context *ictx = NULL;
385 int subminor;
386 int retval = 0;
388 /* prevent races with disconnect */
389 mutex_lock(&driver_lock);
391 subminor = iminor(inode);
392 interface = usb_find_interface(&imon_driver, subminor);
393 if (!interface) {
394 pr_err("could not find interface for minor %d\n", subminor);
395 retval = -ENODEV;
396 goto exit;
398 ictx = usb_get_intfdata(interface);
400 if (!ictx) {
401 pr_err("no context found for minor %d\n", subminor);
402 retval = -ENODEV;
403 goto exit;
406 mutex_lock(&ictx->lock);
408 if (!ictx->display_supported) {
409 pr_err("display not supported by device\n");
410 retval = -ENODEV;
411 } else if (ictx->display_isopen) {
412 pr_err("display port is already open\n");
413 retval = -EBUSY;
414 } else {
415 ictx->display_isopen = true;
416 file->private_data = ictx;
417 dev_dbg(ictx->dev, "display port opened\n");
420 mutex_unlock(&ictx->lock);
422 exit:
423 mutex_unlock(&driver_lock);
424 return retval;
428 * Called when the display device (e.g. /dev/lcd0)
429 * is closed by the application.
431 static int display_close(struct inode *inode, struct file *file)
433 struct imon_context *ictx = NULL;
434 int retval = 0;
436 ictx = file->private_data;
438 if (!ictx) {
439 pr_err("no context for device\n");
440 return -ENODEV;
443 mutex_lock(&ictx->lock);
445 if (!ictx->display_supported) {
446 pr_err("display not supported by device\n");
447 retval = -ENODEV;
448 } else if (!ictx->display_isopen) {
449 pr_err("display is not open\n");
450 retval = -EIO;
451 } else {
452 ictx->display_isopen = false;
453 dev_dbg(ictx->dev, "display port closed\n");
456 mutex_unlock(&ictx->lock);
457 return retval;
461 * Sends a packet to the device -- this function must be called with
462 * ictx->lock held, or its unlock/lock sequence while waiting for tx
463 * to complete can/will lead to a deadlock.
465 static int send_packet(struct imon_context *ictx)
467 unsigned int pipe;
468 unsigned long timeout;
469 int interval = 0;
470 int retval = 0;
471 struct usb_ctrlrequest *control_req = NULL;
473 /* Check if we need to use control or interrupt urb */
474 if (!ictx->tx_control) {
475 pipe = usb_sndintpipe(ictx->usbdev_intf0,
476 ictx->tx_endpoint->bEndpointAddress);
477 interval = ictx->tx_endpoint->bInterval;
479 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
480 ictx->usb_tx_buf,
481 sizeof(ictx->usb_tx_buf),
482 usb_tx_callback, ictx, interval);
484 ictx->tx_urb->actual_length = 0;
485 } else {
486 /* fill request into kmalloc'ed space: */
487 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
488 GFP_KERNEL);
489 if (control_req == NULL)
490 return -ENOMEM;
492 /* setup packet is '21 09 0200 0001 0008' */
493 control_req->bRequestType = 0x21;
494 control_req->bRequest = 0x09;
495 control_req->wValue = cpu_to_le16(0x0200);
496 control_req->wIndex = cpu_to_le16(0x0001);
497 control_req->wLength = cpu_to_le16(0x0008);
499 /* control pipe is endpoint 0x00 */
500 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
502 /* build the control urb */
503 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
504 pipe, (unsigned char *)control_req,
505 ictx->usb_tx_buf,
506 sizeof(ictx->usb_tx_buf),
507 usb_tx_callback, ictx);
508 ictx->tx_urb->actual_length = 0;
511 init_completion(&ictx->tx.finished);
512 ictx->tx.busy = true;
513 smp_rmb(); /* ensure later readers know we're busy */
515 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
516 if (retval) {
517 ictx->tx.busy = false;
518 smp_rmb(); /* ensure later readers know we're not busy */
519 pr_err("error submitting urb(%d)\n", retval);
520 } else {
521 /* Wait for transmission to complete (or abort) */
522 mutex_unlock(&ictx->lock);
523 retval = wait_for_completion_interruptible(
524 &ictx->tx.finished);
525 if (retval)
526 pr_err("task interrupted\n");
527 mutex_lock(&ictx->lock);
529 retval = ictx->tx.status;
530 if (retval)
531 pr_err("packet tx failed (%d)\n", retval);
534 kfree(control_req);
537 * Induce a mandatory 5ms delay before returning, as otherwise,
538 * send_packet can get called so rapidly as to overwhelm the device,
539 * particularly on faster systems and/or those with quirky usb.
541 timeout = msecs_to_jiffies(5);
542 set_current_state(TASK_UNINTERRUPTIBLE);
543 schedule_timeout(timeout);
545 return retval;
549 * Sends an associate packet to the iMON 2.4G.
551 * This might not be such a good idea, since it has an id collision with
552 * some versions of the "IR & VFD" combo. The only way to determine if it
553 * is an RF version is to look at the product description string. (Which
554 * we currently do not fetch).
556 static int send_associate_24g(struct imon_context *ictx)
558 int retval;
559 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
560 0x00, 0x00, 0x00, 0x20 };
562 if (!ictx) {
563 pr_err("no context for device\n");
564 return -ENODEV;
567 if (!ictx->dev_present_intf0) {
568 pr_err("no iMON device present\n");
569 return -ENODEV;
572 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
573 retval = send_packet(ictx);
575 return retval;
579 * Sends packets to setup and show clock on iMON display
581 * Arguments: year - last 2 digits of year, month - 1..12,
582 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
583 * hour - 0..23, minute - 0..59, second - 0..59
585 static int send_set_imon_clock(struct imon_context *ictx,
586 unsigned int year, unsigned int month,
587 unsigned int day, unsigned int dow,
588 unsigned int hour, unsigned int minute,
589 unsigned int second)
591 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
592 int retval = 0;
593 int i;
595 if (!ictx) {
596 pr_err("no context for device\n");
597 return -ENODEV;
600 switch (ictx->display_type) {
601 case IMON_DISPLAY_TYPE_LCD:
602 clock_enable_pkt[0][0] = 0x80;
603 clock_enable_pkt[0][1] = year;
604 clock_enable_pkt[0][2] = month-1;
605 clock_enable_pkt[0][3] = day;
606 clock_enable_pkt[0][4] = hour;
607 clock_enable_pkt[0][5] = minute;
608 clock_enable_pkt[0][6] = second;
610 clock_enable_pkt[1][0] = 0x80;
611 clock_enable_pkt[1][1] = 0;
612 clock_enable_pkt[1][2] = 0;
613 clock_enable_pkt[1][3] = 0;
614 clock_enable_pkt[1][4] = 0;
615 clock_enable_pkt[1][5] = 0;
616 clock_enable_pkt[1][6] = 0;
618 if (ictx->product == 0xffdc) {
619 clock_enable_pkt[0][7] = 0x50;
620 clock_enable_pkt[1][7] = 0x51;
621 } else {
622 clock_enable_pkt[0][7] = 0x88;
623 clock_enable_pkt[1][7] = 0x8a;
626 break;
628 case IMON_DISPLAY_TYPE_VFD:
629 clock_enable_pkt[0][0] = year;
630 clock_enable_pkt[0][1] = month-1;
631 clock_enable_pkt[0][2] = day;
632 clock_enable_pkt[0][3] = dow;
633 clock_enable_pkt[0][4] = hour;
634 clock_enable_pkt[0][5] = minute;
635 clock_enable_pkt[0][6] = second;
636 clock_enable_pkt[0][7] = 0x40;
638 clock_enable_pkt[1][0] = 0;
639 clock_enable_pkt[1][1] = 0;
640 clock_enable_pkt[1][2] = 1;
641 clock_enable_pkt[1][3] = 0;
642 clock_enable_pkt[1][4] = 0;
643 clock_enable_pkt[1][5] = 0;
644 clock_enable_pkt[1][6] = 0;
645 clock_enable_pkt[1][7] = 0x42;
647 break;
649 default:
650 return -ENODEV;
653 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
654 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
655 retval = send_packet(ictx);
656 if (retval) {
657 pr_err("send_packet failed for packet %d\n", i);
658 break;
662 return retval;
666 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
668 static ssize_t show_associate_remote(struct device *d,
669 struct device_attribute *attr,
670 char *buf)
672 struct imon_context *ictx = dev_get_drvdata(d);
674 if (!ictx)
675 return -ENODEV;
677 mutex_lock(&ictx->lock);
678 if (ictx->rf_isassociating)
679 strcpy(buf, "associating\n");
680 else
681 strcpy(buf, "closed\n");
683 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
684 "instructions on how to associate your iMON 2.4G DT/LT "
685 "remote\n");
686 mutex_unlock(&ictx->lock);
687 return strlen(buf);
690 static ssize_t store_associate_remote(struct device *d,
691 struct device_attribute *attr,
692 const char *buf, size_t count)
694 struct imon_context *ictx;
696 ictx = dev_get_drvdata(d);
698 if (!ictx)
699 return -ENODEV;
701 mutex_lock(&ictx->lock);
702 ictx->rf_isassociating = true;
703 send_associate_24g(ictx);
704 mutex_unlock(&ictx->lock);
706 return count;
710 * sysfs functions to control internal imon clock
712 static ssize_t show_imon_clock(struct device *d,
713 struct device_attribute *attr, char *buf)
715 struct imon_context *ictx = dev_get_drvdata(d);
716 size_t len;
718 if (!ictx)
719 return -ENODEV;
721 mutex_lock(&ictx->lock);
723 if (!ictx->display_supported) {
724 len = snprintf(buf, PAGE_SIZE, "Not supported.");
725 } else {
726 len = snprintf(buf, PAGE_SIZE,
727 "To set the clock on your iMON display:\n"
728 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
729 "%s", ictx->display_isopen ?
730 "\nNOTE: imon device must be closed\n" : "");
733 mutex_unlock(&ictx->lock);
735 return len;
738 static ssize_t store_imon_clock(struct device *d,
739 struct device_attribute *attr,
740 const char *buf, size_t count)
742 struct imon_context *ictx = dev_get_drvdata(d);
743 ssize_t retval;
744 unsigned int year, month, day, dow, hour, minute, second;
746 if (!ictx)
747 return -ENODEV;
749 mutex_lock(&ictx->lock);
751 if (!ictx->display_supported) {
752 retval = -ENODEV;
753 goto exit;
754 } else if (ictx->display_isopen) {
755 retval = -EBUSY;
756 goto exit;
759 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
760 &hour, &minute, &second) != 7) {
761 retval = -EINVAL;
762 goto exit;
765 if ((month < 1 || month > 12) ||
766 (day < 1 || day > 31) || (dow > 6) ||
767 (hour > 23) || (minute > 59) || (second > 59)) {
768 retval = -EINVAL;
769 goto exit;
772 retval = send_set_imon_clock(ictx, year, month, day, dow,
773 hour, minute, second);
774 if (retval)
775 goto exit;
777 retval = count;
778 exit:
779 mutex_unlock(&ictx->lock);
781 return retval;
785 static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
786 store_imon_clock);
788 static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
789 store_associate_remote);
791 static struct attribute *imon_display_sysfs_entries[] = {
792 &dev_attr_imon_clock.attr,
793 NULL
796 static struct attribute_group imon_display_attr_group = {
797 .attrs = imon_display_sysfs_entries
800 static struct attribute *imon_rf_sysfs_entries[] = {
801 &dev_attr_associate_remote.attr,
802 NULL
805 static struct attribute_group imon_rf_attr_group = {
806 .attrs = imon_rf_sysfs_entries
810 * Writes data to the VFD. The iMON VFD is 2x16 characters
811 * and requires data in 5 consecutive USB interrupt packets,
812 * each packet but the last carrying 7 bytes.
814 * I don't know if the VFD board supports features such as
815 * scrolling, clearing rows, blanking, etc. so at
816 * the caller must provide a full screen of data. If fewer
817 * than 32 bytes are provided spaces will be appended to
818 * generate a full screen.
820 static ssize_t vfd_write(struct file *file, const char *buf,
821 size_t n_bytes, loff_t *pos)
823 int i;
824 int offset;
825 int seq;
826 int retval = 0;
827 struct imon_context *ictx;
828 const unsigned char vfd_packet6[] = {
829 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
831 ictx = file->private_data;
832 if (!ictx) {
833 pr_err("no context for device\n");
834 return -ENODEV;
837 mutex_lock(&ictx->lock);
839 if (!ictx->dev_present_intf0) {
840 pr_err("no iMON device present\n");
841 retval = -ENODEV;
842 goto exit;
845 if (n_bytes <= 0 || n_bytes > 32) {
846 pr_err("invalid payload size\n");
847 retval = -EINVAL;
848 goto exit;
851 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
852 retval = -EFAULT;
853 goto exit;
856 /* Pad with spaces */
857 for (i = n_bytes; i < 32; ++i)
858 ictx->tx.data_buf[i] = ' ';
860 for (i = 32; i < 35; ++i)
861 ictx->tx.data_buf[i] = 0xFF;
863 offset = 0;
864 seq = 0;
866 do {
867 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
868 ictx->usb_tx_buf[7] = (unsigned char) seq;
870 retval = send_packet(ictx);
871 if (retval) {
872 pr_err("send packet failed for packet #%d\n", seq / 2);
873 goto exit;
874 } else {
875 seq += 2;
876 offset += 7;
879 } while (offset < 35);
881 /* Send packet #6 */
882 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
883 ictx->usb_tx_buf[7] = (unsigned char) seq;
884 retval = send_packet(ictx);
885 if (retval)
886 pr_err("send packet failed for packet #%d\n", seq / 2);
888 exit:
889 mutex_unlock(&ictx->lock);
891 return (!retval) ? n_bytes : retval;
895 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
896 * packets. We accept data as 16 hexadecimal digits, followed by a
897 * newline (to make it easy to drive the device from a command-line
898 * -- even though the actual binary data is a bit complicated).
900 * The device itself is not a "traditional" text-mode display. It's
901 * actually a 16x96 pixel bitmap display. That means if you want to
902 * display text, you've got to have your own "font" and translate the
903 * text into bitmaps for display. This is really flexible (you can
904 * display whatever diacritics you need, and so on), but it's also
905 * a lot more complicated than most LCDs...
907 static ssize_t lcd_write(struct file *file, const char *buf,
908 size_t n_bytes, loff_t *pos)
910 int retval = 0;
911 struct imon_context *ictx;
913 ictx = file->private_data;
914 if (!ictx) {
915 pr_err("no context for device\n");
916 return -ENODEV;
919 mutex_lock(&ictx->lock);
921 if (!ictx->display_supported) {
922 pr_err("no iMON display present\n");
923 retval = -ENODEV;
924 goto exit;
927 if (n_bytes != 8) {
928 pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
929 retval = -EINVAL;
930 goto exit;
933 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
934 retval = -EFAULT;
935 goto exit;
938 retval = send_packet(ictx);
939 if (retval) {
940 pr_err("send packet failed!\n");
941 goto exit;
942 } else {
943 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
944 __func__, (int) n_bytes);
946 exit:
947 mutex_unlock(&ictx->lock);
948 return (!retval) ? n_bytes : retval;
952 * Callback function for USB core API: transmit data
954 static void usb_tx_callback(struct urb *urb)
956 struct imon_context *ictx;
958 if (!urb)
959 return;
960 ictx = (struct imon_context *)urb->context;
961 if (!ictx)
962 return;
964 ictx->tx.status = urb->status;
966 /* notify waiters that write has finished */
967 ictx->tx.busy = false;
968 smp_rmb(); /* ensure later readers know we're not busy */
969 complete(&ictx->tx.finished);
973 * report touchscreen input
975 static void imon_touch_display_timeout(unsigned long data)
977 struct imon_context *ictx = (struct imon_context *)data;
979 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
980 return;
982 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
983 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
984 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
985 input_sync(ictx->touch);
989 * iMON IR receivers support two different signal sets -- those used by
990 * the iMON remotes, and those used by the Windows MCE remotes (which is
991 * really just RC-6), but only one or the other at a time, as the signals
992 * are decoded onboard the receiver.
994 * This function gets called two different ways, one way is from
995 * rc_register_device, for initial protocol selection/setup, and the other is
996 * via a userspace-initiated protocol change request, either by direct sysfs
997 * prodding or by something like ir-keytable. In the rc_register_device case,
998 * the imon context lock is already held, but when initiated from userspace,
999 * it is not, so we must acquire it prior to calling send_packet, which
1000 * requires that the lock is held.
1002 static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
1004 int retval;
1005 struct imon_context *ictx = rc->priv;
1006 struct device *dev = ictx->dev;
1007 bool unlock = false;
1008 unsigned char ir_proto_packet[] = {
1009 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1011 if (rc_type && !(rc_type & rc->allowed_protos))
1012 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1013 "this device does not support\n");
1015 switch (rc_type) {
1016 case RC_TYPE_RC6:
1017 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1018 ir_proto_packet[0] = 0x01;
1019 break;
1020 case RC_TYPE_UNKNOWN:
1021 case RC_TYPE_OTHER:
1022 dev_dbg(dev, "Configuring IR receiver for iMON 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;
1028 default:
1029 dev_warn(dev, "Unsupported IR protocol specified, overriding "
1030 "to iMON IR protocol\n");
1031 if (!pad_stabilize)
1032 dev_dbg(dev, "PAD stabilize functionality disabled\n");
1033 /* ir_proto_packet[0] = 0x00; // already the default */
1034 rc_type = RC_TYPE_OTHER;
1035 break;
1038 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1040 if (!mutex_is_locked(&ictx->lock)) {
1041 unlock = true;
1042 mutex_lock(&ictx->lock);
1045 retval = send_packet(ictx);
1046 if (retval)
1047 goto out;
1049 ictx->rc_type = rc_type;
1050 ictx->pad_mouse = false;
1052 out:
1053 if (unlock)
1054 mutex_unlock(&ictx->lock);
1056 return retval;
1059 static inline int tv2int(const struct timeval *a, const struct timeval *b)
1061 int usecs = 0;
1062 int sec = 0;
1064 if (b->tv_usec > a->tv_usec) {
1065 usecs = 1000000;
1066 sec--;
1069 usecs += a->tv_usec - b->tv_usec;
1071 sec += a->tv_sec - b->tv_sec;
1072 sec *= 1000;
1073 usecs /= 1000;
1074 sec += usecs;
1076 if (sec < 0)
1077 sec = 1000;
1079 return sec;
1083 * The directional pad behaves a bit differently, depending on whether this is
1084 * one of the older ffdc devices or a newer device. Newer devices appear to
1085 * have a higher resolution matrix for more precise mouse movement, but it
1086 * makes things overly sensitive in keyboard mode, so we do some interesting
1087 * contortions to make it less touchy. Older devices run through the same
1088 * routine with shorter timeout and a smaller threshold.
1090 static int stabilize(int a, int b, u16 timeout, u16 threshold)
1092 struct timeval ct;
1093 static struct timeval prev_time = {0, 0};
1094 static struct timeval hit_time = {0, 0};
1095 static int x, y, prev_result, hits;
1096 int result = 0;
1097 int msec, msec_hit;
1099 do_gettimeofday(&ct);
1100 msec = tv2int(&ct, &prev_time);
1101 msec_hit = tv2int(&ct, &hit_time);
1103 if (msec > 100) {
1104 x = 0;
1105 y = 0;
1106 hits = 0;
1109 x += a;
1110 y += b;
1112 prev_time = ct;
1114 if (abs(x) > threshold || abs(y) > threshold) {
1115 if (abs(y) > abs(x))
1116 result = (y > 0) ? 0x7F : 0x80;
1117 else
1118 result = (x > 0) ? 0x7F00 : 0x8000;
1120 x = 0;
1121 y = 0;
1123 if (result == prev_result) {
1124 hits++;
1126 if (hits > 3) {
1127 switch (result) {
1128 case 0x7F:
1129 y = 17 * threshold / 30;
1130 break;
1131 case 0x80:
1132 y -= 17 * threshold / 30;
1133 break;
1134 case 0x7F00:
1135 x = 17 * threshold / 30;
1136 break;
1137 case 0x8000:
1138 x -= 17 * threshold / 30;
1139 break;
1143 if (hits == 2 && msec_hit < timeout) {
1144 result = 0;
1145 hits = 1;
1147 } else {
1148 prev_result = result;
1149 hits = 1;
1150 hit_time = ct;
1154 return result;
1157 static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
1159 u32 keycode;
1160 u32 release;
1161 bool is_release_code = false;
1163 /* Look for the initial press of a button */
1164 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1165 ictx->rc_toggle = 0x0;
1166 ictx->rc_scancode = scancode;
1168 /* Look for the release of a button */
1169 if (keycode == KEY_RESERVED) {
1170 release = scancode & ~0x4000;
1171 keycode = rc_g_keycode_from_table(ictx->rdev, release);
1172 if (keycode != KEY_RESERVED)
1173 is_release_code = true;
1176 ictx->release_code = is_release_code;
1178 return keycode;
1181 static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
1183 u32 keycode;
1185 #define MCE_KEY_MASK 0x7000
1186 #define MCE_TOGGLE_BIT 0x8000
1189 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1190 * (the toggle bit flipping between alternating key presses), while
1191 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1192 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1193 * but we can't or them into all codes, as some keys are decoded in
1194 * a different way w/o the same use of the toggle bit...
1196 if (scancode & 0x80000000)
1197 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1199 ictx->rc_scancode = scancode;
1200 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
1202 /* not used in mce mode, but make sure we know its false */
1203 ictx->release_code = false;
1205 return keycode;
1208 static u32 imon_panel_key_lookup(u64 code)
1210 int i;
1211 u32 keycode = KEY_RESERVED;
1213 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1214 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1215 keycode = imon_panel_key_table[i].keycode;
1216 break;
1220 return keycode;
1223 static bool imon_mouse_event(struct imon_context *ictx,
1224 unsigned char *buf, int len)
1226 char rel_x = 0x00, rel_y = 0x00;
1227 u8 right_shift = 1;
1228 bool mouse_input = true;
1229 int dir = 0;
1230 unsigned long flags;
1232 spin_lock_irqsave(&ictx->kc_lock, flags);
1234 /* newer iMON device PAD or mouse button */
1235 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1236 rel_x = buf[2];
1237 rel_y = buf[3];
1238 right_shift = 1;
1239 /* 0xffdc iMON PAD or mouse button input */
1240 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1241 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1242 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1243 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1244 if (buf[0] & 0x02)
1245 rel_x |= ~0x0f;
1246 rel_x = rel_x + rel_x / 2;
1247 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1248 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1249 if (buf[0] & 0x01)
1250 rel_y |= ~0x0f;
1251 rel_y = rel_y + rel_y / 2;
1252 right_shift = 2;
1253 /* some ffdc devices decode mouse buttons differently... */
1254 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1255 right_shift = 2;
1256 /* ch+/- buttons, which we use for an emulated scroll wheel */
1257 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1258 dir = 1;
1259 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1260 dir = -1;
1261 } else
1262 mouse_input = false;
1264 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1266 if (mouse_input) {
1267 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1269 if (dir) {
1270 input_report_rel(ictx->idev, REL_WHEEL, dir);
1271 } else if (rel_x || rel_y) {
1272 input_report_rel(ictx->idev, REL_X, rel_x);
1273 input_report_rel(ictx->idev, REL_Y, rel_y);
1274 } else {
1275 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1276 input_report_key(ictx->idev, BTN_RIGHT,
1277 buf[1] >> right_shift & 0x1);
1279 input_sync(ictx->idev);
1280 spin_lock_irqsave(&ictx->kc_lock, flags);
1281 ictx->last_keycode = ictx->kc;
1282 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1285 return mouse_input;
1288 static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1290 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1291 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1292 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1293 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1294 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1295 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1296 input_sync(ictx->touch);
1299 static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1301 int dir = 0;
1302 char rel_x = 0x00, rel_y = 0x00;
1303 u16 timeout, threshold;
1304 u32 scancode = KEY_RESERVED;
1305 unsigned long flags;
1308 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1309 * contain a position coordinate (x,y), with each component ranging
1310 * from -14 to 14. We want to down-sample this to only 4 discrete values
1311 * for up/down/left/right arrow keys. Also, when you get too close to
1312 * diagonals, it has a tendency to jump back and forth, so lets try to
1313 * ignore when they get too close.
1315 if (ictx->product != 0xffdc) {
1316 /* first, pad to 8 bytes so it conforms with everything else */
1317 buf[5] = buf[6] = buf[7] = 0;
1318 timeout = 500; /* in msecs */
1319 /* (2*threshold) x (2*threshold) square */
1320 threshold = pad_thresh ? pad_thresh : 28;
1321 rel_x = buf[2];
1322 rel_y = buf[3];
1324 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1325 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1326 dir = stabilize((int)rel_x, (int)rel_y,
1327 timeout, threshold);
1328 if (!dir) {
1329 spin_lock_irqsave(&ictx->kc_lock,
1330 flags);
1331 ictx->kc = KEY_UNKNOWN;
1332 spin_unlock_irqrestore(&ictx->kc_lock,
1333 flags);
1334 return;
1336 buf[2] = dir & 0xFF;
1337 buf[3] = (dir >> 8) & 0xFF;
1338 scancode = be32_to_cpu(*((u32 *)buf));
1340 } else {
1342 * Hack alert: instead of using keycodes, we have
1343 * to use hard-coded scancodes here...
1345 if (abs(rel_y) > abs(rel_x)) {
1346 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1347 buf[3] = 0;
1348 if (rel_y > 0)
1349 scancode = 0x01007f00; /* KEY_DOWN */
1350 else
1351 scancode = 0x01008000; /* KEY_UP */
1352 } else {
1353 buf[2] = 0;
1354 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1355 if (rel_x > 0)
1356 scancode = 0x0100007f; /* KEY_RIGHT */
1357 else
1358 scancode = 0x01000080; /* KEY_LEFT */
1363 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1364 * device (15c2:ffdc). The remote generates various codes from
1365 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1366 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1367 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1368 * reversed endianess. Extract direction from buffer, rotate endianess,
1369 * adjust sign and feed the values into stabilize(). The resulting codes
1370 * will be 0x01008000, 0x01007F00, which match the newer devices.
1372 } else {
1373 timeout = 10; /* in msecs */
1374 /* (2*threshold) x (2*threshold) square */
1375 threshold = pad_thresh ? pad_thresh : 15;
1377 /* buf[1] is x */
1378 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1379 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1380 if (buf[0] & 0x02)
1381 rel_x |= ~0x10+1;
1382 /* buf[2] is y */
1383 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1384 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1385 if (buf[0] & 0x01)
1386 rel_y |= ~0x10+1;
1388 buf[0] = 0x01;
1389 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1391 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
1392 dir = stabilize((int)rel_x, (int)rel_y,
1393 timeout, threshold);
1394 if (!dir) {
1395 spin_lock_irqsave(&ictx->kc_lock, flags);
1396 ictx->kc = KEY_UNKNOWN;
1397 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1398 return;
1400 buf[2] = dir & 0xFF;
1401 buf[3] = (dir >> 8) & 0xFF;
1402 scancode = be32_to_cpu(*((u32 *)buf));
1403 } else {
1405 * Hack alert: instead of using keycodes, we have
1406 * to use hard-coded scancodes here...
1408 if (abs(rel_y) > abs(rel_x)) {
1409 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1410 buf[3] = 0;
1411 if (rel_y > 0)
1412 scancode = 0x01007f00; /* KEY_DOWN */
1413 else
1414 scancode = 0x01008000; /* KEY_UP */
1415 } else {
1416 buf[2] = 0;
1417 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
1418 if (rel_x > 0)
1419 scancode = 0x0100007f; /* KEY_RIGHT */
1420 else
1421 scancode = 0x01000080; /* KEY_LEFT */
1426 if (scancode) {
1427 spin_lock_irqsave(&ictx->kc_lock, flags);
1428 ictx->kc = imon_remote_key_lookup(ictx, scancode);
1429 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1434 * figure out if these is a press or a release. We don't actually
1435 * care about repeats, as those will be auto-generated within the IR
1436 * subsystem for repeating scancodes.
1438 static int imon_parse_press_type(struct imon_context *ictx,
1439 unsigned char *buf, u8 ktype)
1441 int press_type = 0;
1442 unsigned long flags;
1444 spin_lock_irqsave(&ictx->kc_lock, flags);
1446 /* key release of 0x02XXXXXX key */
1447 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1448 ictx->kc = ictx->last_keycode;
1450 /* mouse button release on (some) 0xffdc devices */
1451 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1452 buf[2] == 0x81 && buf[3] == 0xb7)
1453 ictx->kc = ictx->last_keycode;
1455 /* mouse button release on (some other) 0xffdc devices */
1456 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1457 buf[2] == 0x81 && buf[3] == 0xb7)
1458 ictx->kc = ictx->last_keycode;
1460 /* mce-specific button handling, no keyup events */
1461 else if (ktype == IMON_KEY_MCE) {
1462 ictx->rc_toggle = buf[2];
1463 press_type = 1;
1465 /* incoherent or irrelevant data */
1466 } else if (ictx->kc == KEY_RESERVED)
1467 press_type = -EINVAL;
1469 /* key release of 0xXXXXXXb7 key */
1470 else if (ictx->release_code)
1471 press_type = 0;
1473 /* this is a button press */
1474 else
1475 press_type = 1;
1477 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1479 return press_type;
1483 * Process the incoming packet
1485 static void imon_incoming_packet(struct imon_context *ictx,
1486 struct urb *urb, int intf)
1488 int len = urb->actual_length;
1489 unsigned char *buf = urb->transfer_buffer;
1490 struct device *dev = ictx->dev;
1491 unsigned long flags;
1492 u32 kc;
1493 int i;
1494 u64 scancode;
1495 int press_type = 0;
1496 int msec;
1497 struct timeval t;
1498 static struct timeval prev_time = { 0, 0 };
1499 u8 ktype;
1501 /* filter out junk data on the older 0xffdc imon devices */
1502 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
1503 return;
1505 /* Figure out what key was pressed */
1506 if (len == 8 && buf[7] == 0xee) {
1507 scancode = be64_to_cpu(*((u64 *)buf));
1508 ktype = IMON_KEY_PANEL;
1509 kc = imon_panel_key_lookup(scancode);
1510 } else {
1511 scancode = be32_to_cpu(*((u32 *)buf));
1512 if (ictx->rc_type == RC_TYPE_RC6) {
1513 ktype = IMON_KEY_IMON;
1514 if (buf[0] == 0x80)
1515 ktype = IMON_KEY_MCE;
1516 kc = imon_mce_key_lookup(ictx, scancode);
1517 } else {
1518 ktype = IMON_KEY_IMON;
1519 kc = imon_remote_key_lookup(ictx, scancode);
1523 spin_lock_irqsave(&ictx->kc_lock, flags);
1524 /* keyboard/mouse mode toggle button */
1525 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1526 ictx->last_keycode = kc;
1527 if (!nomouse) {
1528 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1529 dev_dbg(dev, "toggling to %s mode\n",
1530 ictx->pad_mouse ? "mouse" : "keyboard");
1531 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1532 return;
1533 } else {
1534 ictx->pad_mouse = false;
1535 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1539 ictx->kc = kc;
1540 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1542 /* send touchscreen events through input subsystem if touchpad data */
1543 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1544 buf[7] == 0x86) {
1545 imon_touch_event(ictx, buf);
1546 return;
1548 /* look for mouse events with pad in mouse mode */
1549 } else if (ictx->pad_mouse) {
1550 if (imon_mouse_event(ictx, buf, len))
1551 return;
1554 /* Now for some special handling to convert pad input to arrow keys */
1555 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1556 ((len == 8) && (buf[0] & 0x40) &&
1557 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1558 len = 8;
1559 imon_pad_to_keys(ictx, buf);
1562 if (debug) {
1563 printk(KERN_INFO "intf%d decoded packet: ", intf);
1564 for (i = 0; i < len; ++i)
1565 printk("%02x ", buf[i]);
1566 printk("\n");
1569 press_type = imon_parse_press_type(ictx, buf, ktype);
1570 if (press_type < 0)
1571 goto not_input_data;
1573 spin_lock_irqsave(&ictx->kc_lock, flags);
1574 if (ictx->kc == KEY_UNKNOWN)
1575 goto unknown_key;
1576 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1578 if (ktype != IMON_KEY_PANEL) {
1579 if (press_type == 0)
1580 rc_keyup(ictx->rdev);
1581 else {
1582 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
1583 spin_lock_irqsave(&ictx->kc_lock, flags);
1584 ictx->last_keycode = ictx->kc;
1585 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1587 return;
1590 /* Only panel type events left to process now */
1591 spin_lock_irqsave(&ictx->kc_lock, flags);
1593 do_gettimeofday(&t);
1594 /* KEY_MUTE repeats from knob need to be suppressed */
1595 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
1596 msec = tv2int(&t, &prev_time);
1597 if (msec < ictx->idev->rep[REP_DELAY]) {
1598 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1599 return;
1602 prev_time = t;
1603 kc = ictx->kc;
1605 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1607 input_report_key(ictx->idev, kc, press_type);
1608 input_sync(ictx->idev);
1610 /* panel keys don't generate a release */
1611 input_report_key(ictx->idev, kc, 0);
1612 input_sync(ictx->idev);
1614 spin_lock_irqsave(&ictx->kc_lock, flags);
1615 ictx->last_keycode = kc;
1616 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1618 return;
1620 unknown_key:
1621 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1622 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
1623 (long long)scancode);
1624 return;
1626 not_input_data:
1627 if (len != 8) {
1628 dev_warn(dev, "imon %s: invalid incoming packet "
1629 "size (len = %d, intf%d)\n", __func__, len, intf);
1630 return;
1633 /* iMON 2.4G associate frame */
1634 if (buf[0] == 0x00 &&
1635 buf[2] == 0xFF && /* REFID */
1636 buf[3] == 0xFF &&
1637 buf[4] == 0xFF &&
1638 buf[5] == 0xFF && /* iMON 2.4G */
1639 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1640 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1641 dev_warn(dev, "%s: remote associated refid=%02X\n",
1642 __func__, buf[1]);
1643 ictx->rf_isassociating = false;
1648 * Callback function for USB core API: receive data
1650 static void usb_rx_callback_intf0(struct urb *urb)
1652 struct imon_context *ictx;
1653 int intfnum = 0;
1655 if (!urb)
1656 return;
1658 ictx = (struct imon_context *)urb->context;
1659 if (!ictx)
1660 return;
1662 switch (urb->status) {
1663 case -ENOENT: /* usbcore unlink successful! */
1664 return;
1666 case -ESHUTDOWN: /* transport endpoint was shut down */
1667 break;
1669 case 0:
1670 imon_incoming_packet(ictx, urb, intfnum);
1671 break;
1673 default:
1674 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1675 __func__, urb->status);
1676 break;
1679 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1682 static void usb_rx_callback_intf1(struct urb *urb)
1684 struct imon_context *ictx;
1685 int intfnum = 1;
1687 if (!urb)
1688 return;
1690 ictx = (struct imon_context *)urb->context;
1691 if (!ictx)
1692 return;
1694 switch (urb->status) {
1695 case -ENOENT: /* usbcore unlink successful! */
1696 return;
1698 case -ESHUTDOWN: /* transport endpoint was shut down */
1699 break;
1701 case 0:
1702 imon_incoming_packet(ictx, urb, intfnum);
1703 break;
1705 default:
1706 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1707 __func__, urb->status);
1708 break;
1711 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1715 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1716 * devices, and all of them constantly spew interrupts, even when there
1717 * is no actual data to report. However, byte 6 of this buffer looks like
1718 * its unique across device variants, so we're trying to key off that to
1719 * figure out which display type (if any) and what IR protocol the device
1720 * actually supports. These devices have their IR protocol hard-coded into
1721 * their firmware, they can't be changed on the fly like the newer hardware.
1723 static void imon_get_ffdc_type(struct imon_context *ictx)
1725 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1726 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
1727 u64 allowed_protos = RC_TYPE_OTHER;
1729 switch (ffdc_cfg_byte) {
1730 /* iMON Knob, no display, iMON IR + vol knob */
1731 case 0x21:
1732 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1733 ictx->display_supported = false;
1734 break;
1735 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1736 case 0x4e:
1737 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1738 ictx->display_supported = false;
1739 ictx->rf_device = true;
1740 break;
1741 /* iMON VFD, no IR (does have vol knob tho) */
1742 case 0x35:
1743 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1744 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1745 break;
1746 /* iMON VFD, iMON IR */
1747 case 0x24:
1748 case 0x85:
1749 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1750 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1751 break;
1752 /* iMON VFD, MCE IR */
1753 case 0x46:
1754 case 0x7e:
1755 case 0x9e:
1756 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1757 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1758 allowed_protos = RC_TYPE_RC6;
1759 break;
1760 /* iMON LCD, MCE IR */
1761 case 0x9f:
1762 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1763 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1764 allowed_protos = RC_TYPE_RC6;
1765 break;
1766 default:
1767 dev_info(ictx->dev, "Unknown 0xffdc device, "
1768 "defaulting to VFD and iMON IR");
1769 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1770 /* We don't know which one it is, allow user to set the
1771 * RC6 one from userspace if OTHER wasn't correct. */
1772 allowed_protos |= RC_TYPE_RC6;
1773 break;
1776 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1778 ictx->display_type = detected_display_type;
1779 ictx->rc_type = allowed_protos;
1782 static void imon_set_display_type(struct imon_context *ictx)
1784 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1787 * Try to auto-detect the type of display if the user hasn't set
1788 * it by hand via the display_type modparam. Default is VFD.
1791 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1792 switch (ictx->product) {
1793 case 0xffdc:
1794 /* set in imon_get_ffdc_type() */
1795 configured_display_type = ictx->display_type;
1796 break;
1797 case 0x0034:
1798 case 0x0035:
1799 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1800 break;
1801 case 0x0038:
1802 case 0x0039:
1803 case 0x0045:
1804 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1805 break;
1806 case 0x003c:
1807 case 0x0041:
1808 case 0x0042:
1809 case 0x0043:
1810 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1811 ictx->display_supported = false;
1812 break;
1813 case 0x0036:
1814 case 0x0044:
1815 default:
1816 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1817 break;
1819 } else {
1820 configured_display_type = display_type;
1821 if (display_type == IMON_DISPLAY_TYPE_NONE)
1822 ictx->display_supported = false;
1823 else
1824 ictx->display_supported = true;
1825 dev_info(ictx->dev, "%s: overriding display type to %d via "
1826 "modparam\n", __func__, display_type);
1829 ictx->display_type = configured_display_type;
1832 static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
1834 struct rc_dev *rdev;
1835 int ret;
1836 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1837 0x00, 0x00, 0x00, 0x88 };
1839 rdev = rc_allocate_device();
1840 if (!rdev) {
1841 dev_err(ictx->dev, "remote control dev allocation failed\n");
1842 goto out;
1845 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1846 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1847 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1848 sizeof(ictx->phys_rdev));
1849 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1851 rdev->input_name = ictx->name_rdev;
1852 rdev->input_phys = ictx->phys_rdev;
1853 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
1854 rdev->dev.parent = ictx->dev;
1856 rdev->priv = ictx;
1857 rdev->driver_type = RC_DRIVER_SCANCODE;
1858 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
1859 rdev->change_protocol = imon_ir_change_protocol;
1860 rdev->driver_name = MOD_NAME;
1862 /* Enable front-panel buttons and/or knobs */
1863 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1864 ret = send_packet(ictx);
1865 /* Not fatal, but warn about it */
1866 if (ret)
1867 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1869 if (ictx->product == 0xffdc) {
1870 imon_get_ffdc_type(ictx);
1871 rdev->allowed_protos = ictx->rc_type;
1874 imon_set_display_type(ictx);
1876 if (ictx->rc_type == RC_TYPE_RC6)
1877 rdev->map_name = RC_MAP_IMON_MCE;
1878 else
1879 rdev->map_name = RC_MAP_IMON_PAD;
1881 ret = rc_register_device(rdev);
1882 if (ret < 0) {
1883 dev_err(ictx->dev, "remote input dev register failed\n");
1884 goto out;
1887 return rdev;
1889 out:
1890 rc_free_device(rdev);
1891 return NULL;
1894 static struct input_dev *imon_init_idev(struct imon_context *ictx)
1896 struct input_dev *idev;
1897 int ret, i;
1899 idev = input_allocate_device();
1900 if (!idev) {
1901 dev_err(ictx->dev, "input dev allocation failed\n");
1902 goto out;
1905 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
1906 "iMON Panel, Knob and Mouse(%04x:%04x)",
1907 ictx->vendor, ictx->product);
1908 idev->name = ictx->name_idev;
1910 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1911 sizeof(ictx->phys_idev));
1912 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
1913 idev->phys = ictx->phys_idev;
1915 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
1917 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1918 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1919 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1920 BIT_MASK(REL_WHEEL);
1922 /* panel and/or knob code support */
1923 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1924 u32 kc = imon_panel_key_table[i].keycode;
1925 __set_bit(kc, idev->keybit);
1928 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1929 idev->dev.parent = ictx->dev;
1930 input_set_drvdata(idev, ictx);
1932 ret = input_register_device(idev);
1933 if (ret < 0) {
1934 dev_err(ictx->dev, "input dev register failed\n");
1935 goto out;
1938 return idev;
1940 out:
1941 input_free_device(idev);
1942 return NULL;
1945 static struct input_dev *imon_init_touch(struct imon_context *ictx)
1947 struct input_dev *touch;
1948 int ret;
1950 touch = input_allocate_device();
1951 if (!touch) {
1952 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1953 goto touch_alloc_failed;
1956 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1957 "iMON USB Touchscreen (%04x:%04x)",
1958 ictx->vendor, ictx->product);
1959 touch->name = ictx->name_touch;
1961 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1962 sizeof(ictx->phys_touch));
1963 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
1964 touch->phys = ictx->phys_touch;
1966 touch->evbit[0] =
1967 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1968 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1969 BIT_MASK(BTN_TOUCH);
1970 input_set_abs_params(touch, ABS_X,
1971 0x00, 0xfff, 0, 0);
1972 input_set_abs_params(touch, ABS_Y,
1973 0x00, 0xfff, 0, 0);
1975 input_set_drvdata(touch, ictx);
1977 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1978 touch->dev.parent = ictx->dev;
1979 ret = input_register_device(touch);
1980 if (ret < 0) {
1981 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1982 goto touch_register_failed;
1985 return touch;
1987 touch_register_failed:
1988 input_free_device(touch);
1990 touch_alloc_failed:
1991 return NULL;
1994 static bool imon_find_endpoints(struct imon_context *ictx,
1995 struct usb_host_interface *iface_desc)
1997 struct usb_endpoint_descriptor *ep;
1998 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1999 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2000 int ifnum = iface_desc->desc.bInterfaceNumber;
2001 int num_endpts = iface_desc->desc.bNumEndpoints;
2002 int i, ep_dir, ep_type;
2003 bool ir_ep_found = false;
2004 bool display_ep_found = false;
2005 bool tx_control = false;
2008 * Scan the endpoint list and set:
2009 * first input endpoint = IR endpoint
2010 * first output endpoint = display endpoint
2012 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2013 ep = &iface_desc->endpoint[i].desc;
2014 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2015 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2017 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2018 ep_type == USB_ENDPOINT_XFER_INT) {
2020 rx_endpoint = ep;
2021 ir_ep_found = true;
2022 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2024 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2025 ep_type == USB_ENDPOINT_XFER_INT) {
2026 tx_endpoint = ep;
2027 display_ep_found = true;
2028 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2032 if (ifnum == 0) {
2033 ictx->rx_endpoint_intf0 = rx_endpoint;
2035 * tx is used to send characters to lcd/vfd, associate RF
2036 * remotes, set IR protocol, and maybe more...
2038 ictx->tx_endpoint = tx_endpoint;
2039 } else {
2040 ictx->rx_endpoint_intf1 = rx_endpoint;
2044 * If we didn't find a display endpoint, this is probably one of the
2045 * newer iMON devices that use control urb instead of interrupt
2047 if (!display_ep_found) {
2048 tx_control = true;
2049 display_ep_found = true;
2050 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2051 "interface OUT endpoint\n", __func__);
2055 * Some iMON receivers have no display. Unfortunately, it seems
2056 * that SoundGraph recycles device IDs between devices both with
2057 * and without... :\
2059 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
2060 display_ep_found = false;
2061 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2065 * iMON Touch devices have a VGA touchscreen, but no "display", as
2066 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2068 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2069 display_ep_found = false;
2070 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2073 /* Input endpoint is mandatory */
2074 if (!ir_ep_found)
2075 pr_err("no valid input (IR) endpoint found\n");
2077 ictx->tx_control = tx_control;
2079 if (display_ep_found)
2080 ictx->display_supported = true;
2082 return ir_ep_found;
2086 static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2088 struct imon_context *ictx;
2089 struct urb *rx_urb;
2090 struct urb *tx_urb;
2091 struct device *dev = &intf->dev;
2092 struct usb_host_interface *iface_desc;
2093 int ret = -ENOMEM;
2095 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2096 if (!ictx) {
2097 dev_err(dev, "%s: kzalloc failed for context", __func__);
2098 goto exit;
2100 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2101 if (!rx_urb) {
2102 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2103 goto rx_urb_alloc_failed;
2105 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2106 if (!tx_urb) {
2107 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2108 __func__);
2109 goto tx_urb_alloc_failed;
2112 mutex_init(&ictx->lock);
2113 spin_lock_init(&ictx->kc_lock);
2115 mutex_lock(&ictx->lock);
2117 ictx->dev = dev;
2118 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
2119 ictx->dev_present_intf0 = true;
2120 ictx->rx_urb_intf0 = rx_urb;
2121 ictx->tx_urb = tx_urb;
2122 ictx->rf_device = false;
2124 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2125 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2127 ret = -ENODEV;
2128 iface_desc = intf->cur_altsetting;
2129 if (!imon_find_endpoints(ictx, iface_desc)) {
2130 goto find_endpoint_failed;
2133 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2134 usb_rcvintpipe(ictx->usbdev_intf0,
2135 ictx->rx_endpoint_intf0->bEndpointAddress),
2136 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2137 usb_rx_callback_intf0, ictx,
2138 ictx->rx_endpoint_intf0->bInterval);
2140 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2141 if (ret) {
2142 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
2143 goto urb_submit_failed;
2146 ictx->idev = imon_init_idev(ictx);
2147 if (!ictx->idev) {
2148 dev_err(dev, "%s: input device setup failed\n", __func__);
2149 goto idev_setup_failed;
2152 ictx->rdev = imon_init_rdev(ictx);
2153 if (!ictx->rdev) {
2154 dev_err(dev, "%s: rc device setup failed\n", __func__);
2155 goto rdev_setup_failed;
2158 mutex_unlock(&ictx->lock);
2159 return ictx;
2161 rdev_setup_failed:
2162 input_unregister_device(ictx->idev);
2163 idev_setup_failed:
2164 usb_kill_urb(ictx->rx_urb_intf0);
2165 urb_submit_failed:
2166 find_endpoint_failed:
2167 mutex_unlock(&ictx->lock);
2168 usb_free_urb(tx_urb);
2169 tx_urb_alloc_failed:
2170 usb_free_urb(rx_urb);
2171 rx_urb_alloc_failed:
2172 kfree(ictx);
2173 exit:
2174 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2176 return NULL;
2179 static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2180 struct imon_context *ictx)
2182 struct urb *rx_urb;
2183 struct usb_host_interface *iface_desc;
2184 int ret = -ENOMEM;
2186 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2187 if (!rx_urb) {
2188 pr_err("usb_alloc_urb failed for IR urb\n");
2189 goto rx_urb_alloc_failed;
2192 mutex_lock(&ictx->lock);
2194 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2195 init_timer(&ictx->ttimer);
2196 ictx->ttimer.data = (unsigned long)ictx;
2197 ictx->ttimer.function = imon_touch_display_timeout;
2200 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
2201 ictx->dev_present_intf1 = true;
2202 ictx->rx_urb_intf1 = rx_urb;
2204 ret = -ENODEV;
2205 iface_desc = intf->cur_altsetting;
2206 if (!imon_find_endpoints(ictx, iface_desc))
2207 goto find_endpoint_failed;
2209 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2210 ictx->touch = imon_init_touch(ictx);
2211 if (!ictx->touch)
2212 goto touch_setup_failed;
2213 } else
2214 ictx->touch = NULL;
2216 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2217 usb_rcvintpipe(ictx->usbdev_intf1,
2218 ictx->rx_endpoint_intf1->bEndpointAddress),
2219 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2220 usb_rx_callback_intf1, ictx,
2221 ictx->rx_endpoint_intf1->bInterval);
2223 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2225 if (ret) {
2226 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
2227 goto urb_submit_failed;
2230 mutex_unlock(&ictx->lock);
2231 return ictx;
2233 urb_submit_failed:
2234 if (ictx->touch)
2235 input_unregister_device(ictx->touch);
2236 touch_setup_failed:
2237 find_endpoint_failed:
2238 mutex_unlock(&ictx->lock);
2239 usb_free_urb(rx_urb);
2240 rx_urb_alloc_failed:
2241 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2243 return NULL;
2246 static void imon_init_display(struct imon_context *ictx,
2247 struct usb_interface *intf)
2249 int ret;
2251 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2253 /* set up sysfs entry for built-in clock */
2254 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
2255 if (ret)
2256 dev_err(ictx->dev, "Could not create display sysfs "
2257 "entries(%d)", ret);
2259 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2260 ret = usb_register_dev(intf, &imon_lcd_class);
2261 else
2262 ret = usb_register_dev(intf, &imon_vfd_class);
2263 if (ret)
2264 /* Not a fatal error, so ignore */
2265 dev_info(ictx->dev, "could not get a minor number for "
2266 "display\n");
2271 * Callback function for USB core API: Probe
2273 static int __devinit imon_probe(struct usb_interface *interface,
2274 const struct usb_device_id *id)
2276 struct usb_device *usbdev = NULL;
2277 struct usb_host_interface *iface_desc = NULL;
2278 struct usb_interface *first_if;
2279 struct device *dev = &interface->dev;
2280 int ifnum, sysfs_err;
2281 int ret = 0;
2282 struct imon_context *ictx = NULL;
2283 struct imon_context *first_if_ctx = NULL;
2284 u16 vendor, product;
2286 usbdev = usb_get_dev(interface_to_usbdev(interface));
2287 iface_desc = interface->cur_altsetting;
2288 ifnum = iface_desc->desc.bInterfaceNumber;
2289 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2290 product = le16_to_cpu(usbdev->descriptor.idProduct);
2292 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2293 __func__, vendor, product, ifnum);
2295 /* prevent races probing devices w/multiple interfaces */
2296 mutex_lock(&driver_lock);
2298 first_if = usb_ifnum_to_if(usbdev, 0);
2299 first_if_ctx = usb_get_intfdata(first_if);
2301 if (ifnum == 0) {
2302 ictx = imon_init_intf0(interface);
2303 if (!ictx) {
2304 pr_err("failed to initialize context!\n");
2305 ret = -ENODEV;
2306 goto fail;
2309 } else {
2310 /* this is the secondary interface on the device */
2311 ictx = imon_init_intf1(interface, first_if_ctx);
2312 if (!ictx) {
2313 pr_err("failed to attach to context!\n");
2314 ret = -ENODEV;
2315 goto fail;
2320 usb_set_intfdata(interface, ictx);
2322 if (ifnum == 0) {
2323 mutex_lock(&ictx->lock);
2325 if (product == 0xffdc && ictx->rf_device) {
2326 sysfs_err = sysfs_create_group(&interface->dev.kobj,
2327 &imon_rf_attr_group);
2328 if (sysfs_err)
2329 pr_err("Could not create RF sysfs entries(%d)\n",
2330 sysfs_err);
2333 if (ictx->display_supported)
2334 imon_init_display(ictx, interface);
2336 mutex_unlock(&ictx->lock);
2339 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2340 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2341 usbdev->bus->busnum, usbdev->devnum);
2343 mutex_unlock(&driver_lock);
2345 return 0;
2347 fail:
2348 mutex_unlock(&driver_lock);
2349 dev_err(dev, "unable to register, err %d\n", ret);
2351 return ret;
2355 * Callback function for USB core API: disconnect
2357 static void __devexit imon_disconnect(struct usb_interface *interface)
2359 struct imon_context *ictx;
2360 struct device *dev;
2361 int ifnum;
2363 /* prevent races with multi-interface device probing and display_open */
2364 mutex_lock(&driver_lock);
2366 ictx = usb_get_intfdata(interface);
2367 dev = ictx->dev;
2368 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2371 * sysfs_remove_group is safe to call even if sysfs_create_group
2372 * hasn't been called
2374 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2375 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
2377 usb_set_intfdata(interface, NULL);
2379 /* Abort ongoing write */
2380 if (ictx->tx.busy) {
2381 usb_kill_urb(ictx->tx_urb);
2382 complete_all(&ictx->tx.finished);
2385 if (ifnum == 0) {
2386 ictx->dev_present_intf0 = false;
2387 usb_kill_urb(ictx->rx_urb_intf0);
2388 input_unregister_device(ictx->idev);
2389 rc_unregister_device(ictx->rdev);
2390 if (ictx->display_supported) {
2391 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2392 usb_deregister_dev(interface, &imon_lcd_class);
2393 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
2394 usb_deregister_dev(interface, &imon_vfd_class);
2396 } else {
2397 ictx->dev_present_intf1 = false;
2398 usb_kill_urb(ictx->rx_urb_intf1);
2399 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2400 input_unregister_device(ictx->touch);
2401 del_timer_sync(&ictx->ttimer);
2405 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2406 free_imon_context(ictx);
2408 mutex_unlock(&driver_lock);
2410 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2411 __func__, ifnum);
2414 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2416 struct imon_context *ictx = usb_get_intfdata(intf);
2417 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2419 if (ifnum == 0)
2420 usb_kill_urb(ictx->rx_urb_intf0);
2421 else
2422 usb_kill_urb(ictx->rx_urb_intf1);
2424 return 0;
2427 static int imon_resume(struct usb_interface *intf)
2429 int rc = 0;
2430 struct imon_context *ictx = usb_get_intfdata(intf);
2431 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2433 if (ifnum == 0) {
2434 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2435 usb_rcvintpipe(ictx->usbdev_intf0,
2436 ictx->rx_endpoint_intf0->bEndpointAddress),
2437 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2438 usb_rx_callback_intf0, ictx,
2439 ictx->rx_endpoint_intf0->bInterval);
2441 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2443 } else {
2444 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2445 usb_rcvintpipe(ictx->usbdev_intf1,
2446 ictx->rx_endpoint_intf1->bEndpointAddress),
2447 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2448 usb_rx_callback_intf1, ictx,
2449 ictx->rx_endpoint_intf1->bInterval);
2451 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2454 return rc;
2457 static int __init imon_init(void)
2459 int rc;
2461 rc = usb_register(&imon_driver);
2462 if (rc) {
2463 pr_err("usb register failed(%d)\n", rc);
2464 rc = -ENODEV;
2467 return rc;
2470 static void __exit imon_exit(void)
2472 usb_deregister(&imon_driver);
2475 module_init(imon_init);
2476 module_exit(imon_exit);