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