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