blob: 3f3c70716268d2e40faf343a0d1320791a265924 [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 *
Jarod Wilson693508d2010-09-15 15:56:03 -03004 * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson21677cf2010-04-16 18:29:02 -03005 * 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
Joe Perchese2302502010-06-20 04:20:46 -030029#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
30
Jarod Wilson21677cf2010-04-16 18:29:02 -030031#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h>
37
38#include <linux/input.h>
39#include <linux/usb.h>
40#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030041#include <media/rc-core.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030042
43#include <linux/time.h>
44#include <linux/timer.h>
45
46#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
47#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
48#define MOD_NAME "imon"
Jarod Wilson23ef7102011-04-27 19:01:44 -030049#define MOD_VERSION "0.9.3"
Jarod Wilson21677cf2010-04-16 18:29:02 -030050
51#define DISPLAY_MINOR_BASE 144
52#define DEVICE_NAME "lcd%d"
53
54#define BUF_CHUNK_SIZE 8
55#define BUF_SIZE 128
56
57#define BIT_DURATION 250 /* each bit received is 250us */
58
59#define IMON_CLOCK_ENABLE_PACKETS 2
Jarod Wilson21677cf2010-04-16 18:29:02 -030060
61/*** P R O T O T Y P E S ***/
62
63/* USB Callback prototypes */
64static int imon_probe(struct usb_interface *interface,
65 const struct usb_device_id *id);
66static void imon_disconnect(struct usb_interface *interface);
67static void usb_rx_callback_intf0(struct urb *urb);
68static void usb_rx_callback_intf1(struct urb *urb);
69static void usb_tx_callback(struct urb *urb);
70
71/* suspend/resume support */
72static int imon_resume(struct usb_interface *intf);
73static int imon_suspend(struct usb_interface *intf, pm_message_t message);
74
75/* Display file_operations function prototypes */
76static int display_open(struct inode *inode, struct file *file);
77static int display_close(struct inode *inode, struct file *file);
78
79/* VFD write operation */
80static ssize_t vfd_write(struct file *file, const char *buf,
81 size_t n_bytes, loff_t *pos);
82
83/* LCD file_operations override function prototypes */
84static ssize_t lcd_write(struct file *file, const char *buf,
85 size_t n_bytes, loff_t *pos);
86
87/*** G L O B A L S ***/
88
89struct imon_context {
90 struct device *dev;
Jarod Wilson21677cf2010-04-16 18:29:02 -030091 /* Newer devices have two interfaces */
92 struct usb_device *usbdev_intf0;
93 struct usb_device *usbdev_intf1;
94
95 bool display_supported; /* not all controllers do */
96 bool display_isopen; /* display port has been opened */
Jarod Wilsonbbe46902010-05-24 12:02:05 -030097 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
Jarod Wilson21677cf2010-04-16 18:29:02 -030098 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
David Härdemand8b4b582010-10-29 16:08:23 -0300125 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300126 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300127 struct input_dev *touch; /* input device for touchscreen */
128
Jarod Wilson693508d2010-09-15 15:56:03 -0300129 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300130 u32 kc; /* current input keycode */
131 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300132 u32 rc_scancode; /* the computed remote scancode */
133 u8 rc_toggle; /* the computed remote toggle bit */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300134 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300135 bool release_code; /* some keys send a release code */
136
137 u8 display_type; /* store the display type */
138 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
139
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300140 char name_rdev[128]; /* rc input device name */
141 char phys_rdev[64]; /* rc input device phys path */
142
Jarod Wilson21677cf2010-04-16 18:29:02 -0300143 char name_idev[128]; /* input device name */
144 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300145
146 char name_touch[128]; /* touch screen name */
147 char phys_touch[64]; /* touch screen phys path */
148 struct timer_list ttimer; /* touch screen timer */
149 int touch_x; /* x coordinate on touchscreen */
150 int touch_y; /* y coordinate on touchscreen */
151};
152
153#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300154
155/* vfd character device file operations */
156static const struct file_operations vfd_fops = {
157 .owner = THIS_MODULE,
158 .open = &display_open,
159 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200160 .release = &display_close,
161 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300162};
163
164/* lcd character device file operations */
165static const struct file_operations lcd_fops = {
166 .owner = THIS_MODULE,
167 .open = &display_open,
168 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200169 .release = &display_close,
170 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300171};
172
173enum {
174 IMON_DISPLAY_TYPE_AUTO = 0,
175 IMON_DISPLAY_TYPE_VFD = 1,
176 IMON_DISPLAY_TYPE_LCD = 2,
177 IMON_DISPLAY_TYPE_VGA = 3,
178 IMON_DISPLAY_TYPE_NONE = 4,
179};
180
181enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300182 IMON_KEY_IMON = 0,
183 IMON_KEY_MCE = 1,
184 IMON_KEY_PANEL = 2,
185};
186
187/*
188 * USB Device ID for iMON USB Control Boards
189 *
190 * The Windows drivers contain 6 different inf files, more or less one for
191 * each new device until the 0x0034-0x0046 devices, which all use the same
192 * driver. Some of the devices in the 34-46 range haven't been definitively
193 * identified yet. Early devices have either a TriGem Computer, Inc. or a
194 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
195 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
196 * the ffdc and later devices, which do onboard decoding.
197 */
198static struct usb_device_id imon_usb_id_table[] = {
199 /*
200 * Several devices with this same device ID, all use iMON_PAD.inf
201 * SoundGraph iMON PAD (IR & VFD)
202 * SoundGraph iMON PAD (IR & LCD)
203 * SoundGraph iMON Knob (IR only)
204 */
205 { USB_DEVICE(0x15c2, 0xffdc) },
206
207 /*
208 * Newer devices, all driven by the latest iMON Windows driver, full
209 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
210 * Need user input to fill in details on unknown devices.
211 */
212 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
213 { USB_DEVICE(0x15c2, 0x0034) },
214 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
215 { USB_DEVICE(0x15c2, 0x0035) },
216 /* SoundGraph iMON OEM VFD (IR & VFD) */
217 { USB_DEVICE(0x15c2, 0x0036) },
218 /* device specifics unknown */
219 { USB_DEVICE(0x15c2, 0x0037) },
220 /* SoundGraph iMON OEM LCD (IR & LCD) */
221 { USB_DEVICE(0x15c2, 0x0038) },
222 /* SoundGraph iMON UltraBay (IR & LCD) */
223 { USB_DEVICE(0x15c2, 0x0039) },
224 /* device specifics unknown */
225 { USB_DEVICE(0x15c2, 0x003a) },
226 /* device specifics unknown */
227 { USB_DEVICE(0x15c2, 0x003b) },
228 /* SoundGraph iMON OEM Inside (IR only) */
229 { USB_DEVICE(0x15c2, 0x003c) },
230 /* device specifics unknown */
231 { USB_DEVICE(0x15c2, 0x003d) },
232 /* device specifics unknown */
233 { USB_DEVICE(0x15c2, 0x003e) },
234 /* device specifics unknown */
235 { USB_DEVICE(0x15c2, 0x003f) },
236 /* device specifics unknown */
237 { USB_DEVICE(0x15c2, 0x0040) },
238 /* SoundGraph iMON MINI (IR only) */
239 { USB_DEVICE(0x15c2, 0x0041) },
240 /* Antec Veris Multimedia Station EZ External (IR only) */
241 { USB_DEVICE(0x15c2, 0x0042) },
242 /* Antec Veris Multimedia Station Basic Internal (IR only) */
243 { USB_DEVICE(0x15c2, 0x0043) },
244 /* Antec Veris Multimedia Station Elite (IR & VFD) */
245 { USB_DEVICE(0x15c2, 0x0044) },
246 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
247 { USB_DEVICE(0x15c2, 0x0045) },
248 /* device specifics unknown */
249 { USB_DEVICE(0x15c2, 0x0046) },
250 {}
251};
252
253/* USB Device data */
254static struct usb_driver imon_driver = {
255 .name = MOD_NAME,
256 .probe = imon_probe,
257 .disconnect = imon_disconnect,
258 .suspend = imon_suspend,
259 .resume = imon_resume,
260 .id_table = imon_usb_id_table,
261};
262
263static struct usb_class_driver imon_vfd_class = {
264 .name = DEVICE_NAME,
265 .fops = &vfd_fops,
266 .minor_base = DISPLAY_MINOR_BASE,
267};
268
269static struct usb_class_driver imon_lcd_class = {
270 .name = DEVICE_NAME,
271 .fops = &lcd_fops,
272 .minor_base = DISPLAY_MINOR_BASE,
273};
274
275/* imon receiver front panel/knob key table */
276static const struct {
277 u64 hw_code;
278 u32 keycode;
279} imon_panel_key_table[] = {
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300280 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
281 { 0x000000001200ffeell, KEY_UP },
282 { 0x000000001300ffeell, KEY_DOWN },
283 { 0x000000001400ffeell, KEY_LEFT },
284 { 0x000000001500ffeell, KEY_RIGHT },
285 { 0x000000001600ffeell, KEY_ENTER },
286 { 0x000000001700ffeell, KEY_ESC },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300287 { 0x000000001f00ffeell, KEY_AUDIO },
288 { 0x000000002000ffeell, KEY_VIDEO },
289 { 0x000000002100ffeell, KEY_CAMERA },
290 { 0x000000002700ffeell, KEY_DVD },
291 { 0x000000002300ffeell, KEY_TV },
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300292 { 0x000000002b00ffeell, KEY_EXIT },
293 { 0x000000002c00ffeell, KEY_SELECT },
294 { 0x000000002d00ffeell, KEY_MENU },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300295 { 0x000000000500ffeell, KEY_PREVIOUS },
296 { 0x000000000700ffeell, KEY_REWIND },
297 { 0x000000000400ffeell, KEY_STOP },
298 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
299 { 0x000000000800ffeell, KEY_FASTFORWARD },
300 { 0x000000000600ffeell, KEY_NEXT },
301 { 0x000000010000ffeell, KEY_RIGHT },
302 { 0x000001000000ffeell, KEY_LEFT },
303 { 0x000000003d00ffeell, KEY_SELECT },
304 { 0x000100000000ffeell, KEY_VOLUMEUP },
305 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
306 { 0x000000000100ffeell, KEY_MUTE },
Jarod Wilson04292fc2010-09-15 00:28:41 -0300307 /* 0xffdc iMON MCE VFD */
308 { 0x00010000ffffffeell, KEY_VOLUMEUP },
309 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300310 /* iMON Knob values */
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300311 { 0x000100ffffffffeell, KEY_VOLUMEUP },
312 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
313 { 0x000008ffffffffeell, KEY_MUTE },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300314};
315
316/* to prevent races between open() and disconnect(), probing, etc */
317static DEFINE_MUTEX(driver_lock);
318
319/* Module bookkeeping bits */
320MODULE_AUTHOR(MOD_AUTHOR);
321MODULE_DESCRIPTION(MOD_DESC);
322MODULE_VERSION(MOD_VERSION);
323MODULE_LICENSE("GPL");
324MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
325
326static bool debug;
327module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300328MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300329
330/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
331static int display_type;
332module_param(display_type, int, S_IRUGO);
333MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
334 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
335
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300336static int pad_stabilize = 1;
337module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
338MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
339 "presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300340
341/*
342 * In certain use cases, mouse mode isn't really helpful, and could actually
343 * cause confusion, so allow disabling it when the IR device is open.
344 */
345static bool nomouse;
346module_param(nomouse, bool, S_IRUGO | S_IWUSR);
347MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
348 "open. 0=don't disable, 1=disable. (default: don't disable)");
349
350/* threshold at which a pad push registers as an arrow key in kbd mode */
351static int pad_thresh;
352module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
353MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
354 "arrow key in kbd mode (default: 28)");
355
356
357static void free_imon_context(struct imon_context *ictx)
358{
359 struct device *dev = ictx->dev;
360
361 usb_free_urb(ictx->tx_urb);
362 usb_free_urb(ictx->rx_urb_intf0);
363 usb_free_urb(ictx->rx_urb_intf1);
364 kfree(ictx);
365
366 dev_dbg(dev, "%s: iMON context freed\n", __func__);
367}
368
369/**
370 * Called when the Display device (e.g. /dev/lcd0)
371 * is opened by the application.
372 */
373static int display_open(struct inode *inode, struct file *file)
374{
375 struct usb_interface *interface;
376 struct imon_context *ictx = NULL;
377 int subminor;
378 int retval = 0;
379
380 /* prevent races with disconnect */
381 mutex_lock(&driver_lock);
382
383 subminor = iminor(inode);
384 interface = usb_find_interface(&imon_driver, subminor);
385 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300386 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300387 retval = -ENODEV;
388 goto exit;
389 }
390 ictx = usb_get_intfdata(interface);
391
392 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300393 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300394 retval = -ENODEV;
395 goto exit;
396 }
397
398 mutex_lock(&ictx->lock);
399
400 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300401 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300402 retval = -ENODEV;
403 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300404 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300405 retval = -EBUSY;
406 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300407 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300408 file->private_data = ictx;
409 dev_dbg(ictx->dev, "display port opened\n");
410 }
411
412 mutex_unlock(&ictx->lock);
413
414exit:
415 mutex_unlock(&driver_lock);
416 return retval;
417}
418
419/**
420 * Called when the display device (e.g. /dev/lcd0)
421 * is closed by the application.
422 */
423static int display_close(struct inode *inode, struct file *file)
424{
425 struct imon_context *ictx = NULL;
426 int retval = 0;
427
Joe Perchesabf84382010-07-12 17:50:03 -0300428 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300429
430 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300431 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300432 return -ENODEV;
433 }
434
435 mutex_lock(&ictx->lock);
436
437 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300438 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300439 retval = -ENODEV;
440 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300441 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300442 retval = -EIO;
443 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300444 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300445 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300446 }
447
448 mutex_unlock(&ictx->lock);
449 return retval;
450}
451
452/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300453 * Sends a packet to the device -- this function must be called with
454 * ictx->lock held, or its unlock/lock sequence while waiting for tx
455 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300456 */
457static int send_packet(struct imon_context *ictx)
458{
459 unsigned int pipe;
460 unsigned long timeout;
461 int interval = 0;
462 int retval = 0;
463 struct usb_ctrlrequest *control_req = NULL;
464
465 /* Check if we need to use control or interrupt urb */
466 if (!ictx->tx_control) {
467 pipe = usb_sndintpipe(ictx->usbdev_intf0,
468 ictx->tx_endpoint->bEndpointAddress);
469 interval = ictx->tx_endpoint->bInterval;
470
471 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
472 ictx->usb_tx_buf,
473 sizeof(ictx->usb_tx_buf),
474 usb_tx_callback, ictx, interval);
475
476 ictx->tx_urb->actual_length = 0;
477 } else {
478 /* fill request into kmalloc'ed space: */
479 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
480 GFP_KERNEL);
481 if (control_req == NULL)
482 return -ENOMEM;
483
484 /* setup packet is '21 09 0200 0001 0008' */
485 control_req->bRequestType = 0x21;
486 control_req->bRequest = 0x09;
487 control_req->wValue = cpu_to_le16(0x0200);
488 control_req->wIndex = cpu_to_le16(0x0001);
489 control_req->wLength = cpu_to_le16(0x0008);
490
491 /* control pipe is endpoint 0x00 */
492 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
493
494 /* build the control urb */
495 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
496 pipe, (unsigned char *)control_req,
497 ictx->usb_tx_buf,
498 sizeof(ictx->usb_tx_buf),
499 usb_tx_callback, ictx);
500 ictx->tx_urb->actual_length = 0;
501 }
502
503 init_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300504 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300505 smp_rmb(); /* ensure later readers know we're busy */
506
507 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
508 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300509 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300510 smp_rmb(); /* ensure later readers know we're not busy */
Joe Perchese2302502010-06-20 04:20:46 -0300511 pr_err("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300512 } else {
513 /* Wait for transmission to complete (or abort) */
514 mutex_unlock(&ictx->lock);
515 retval = wait_for_completion_interruptible(
516 &ictx->tx.finished);
517 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300518 pr_err("task interrupted\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300519 mutex_lock(&ictx->lock);
520
521 retval = ictx->tx.status;
522 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300523 pr_err("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300524 }
525
526 kfree(control_req);
527
528 /*
529 * Induce a mandatory 5ms delay before returning, as otherwise,
530 * send_packet can get called so rapidly as to overwhelm the device,
531 * particularly on faster systems and/or those with quirky usb.
532 */
533 timeout = msecs_to_jiffies(5);
534 set_current_state(TASK_UNINTERRUPTIBLE);
535 schedule_timeout(timeout);
536
537 return retval;
538}
539
540/**
541 * Sends an associate packet to the iMON 2.4G.
542 *
543 * This might not be such a good idea, since it has an id collision with
544 * some versions of the "IR & VFD" combo. The only way to determine if it
545 * is an RF version is to look at the product description string. (Which
546 * we currently do not fetch).
547 */
548static int send_associate_24g(struct imon_context *ictx)
549{
550 int retval;
551 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
552 0x00, 0x00, 0x00, 0x20 };
553
554 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300555 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300556 return -ENODEV;
557 }
558
559 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300560 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300561 return -ENODEV;
562 }
563
564 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
565 retval = send_packet(ictx);
566
567 return retval;
568}
569
570/**
571 * Sends packets to setup and show clock on iMON display
572 *
573 * Arguments: year - last 2 digits of year, month - 1..12,
574 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
575 * hour - 0..23, minute - 0..59, second - 0..59
576 */
577static int send_set_imon_clock(struct imon_context *ictx,
578 unsigned int year, unsigned int month,
579 unsigned int day, unsigned int dow,
580 unsigned int hour, unsigned int minute,
581 unsigned int second)
582{
583 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
584 int retval = 0;
585 int i;
586
587 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300588 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300589 return -ENODEV;
590 }
591
592 switch (ictx->display_type) {
593 case IMON_DISPLAY_TYPE_LCD:
594 clock_enable_pkt[0][0] = 0x80;
595 clock_enable_pkt[0][1] = year;
596 clock_enable_pkt[0][2] = month-1;
597 clock_enable_pkt[0][3] = day;
598 clock_enable_pkt[0][4] = hour;
599 clock_enable_pkt[0][5] = minute;
600 clock_enable_pkt[0][6] = second;
601
602 clock_enable_pkt[1][0] = 0x80;
603 clock_enable_pkt[1][1] = 0;
604 clock_enable_pkt[1][2] = 0;
605 clock_enable_pkt[1][3] = 0;
606 clock_enable_pkt[1][4] = 0;
607 clock_enable_pkt[1][5] = 0;
608 clock_enable_pkt[1][6] = 0;
609
610 if (ictx->product == 0xffdc) {
611 clock_enable_pkt[0][7] = 0x50;
612 clock_enable_pkt[1][7] = 0x51;
613 } else {
614 clock_enable_pkt[0][7] = 0x88;
615 clock_enable_pkt[1][7] = 0x8a;
616 }
617
618 break;
619
620 case IMON_DISPLAY_TYPE_VFD:
621 clock_enable_pkt[0][0] = year;
622 clock_enable_pkt[0][1] = month-1;
623 clock_enable_pkt[0][2] = day;
624 clock_enable_pkt[0][3] = dow;
625 clock_enable_pkt[0][4] = hour;
626 clock_enable_pkt[0][5] = minute;
627 clock_enable_pkt[0][6] = second;
628 clock_enable_pkt[0][7] = 0x40;
629
630 clock_enable_pkt[1][0] = 0;
631 clock_enable_pkt[1][1] = 0;
632 clock_enable_pkt[1][2] = 1;
633 clock_enable_pkt[1][3] = 0;
634 clock_enable_pkt[1][4] = 0;
635 clock_enable_pkt[1][5] = 0;
636 clock_enable_pkt[1][6] = 0;
637 clock_enable_pkt[1][7] = 0x42;
638
639 break;
640
641 default:
642 return -ENODEV;
643 }
644
645 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
646 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
647 retval = send_packet(ictx);
648 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300649 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300650 break;
651 }
652 }
653
654 return retval;
655}
656
657/**
658 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
659 */
660static ssize_t show_associate_remote(struct device *d,
661 struct device_attribute *attr,
662 char *buf)
663{
664 struct imon_context *ictx = dev_get_drvdata(d);
665
666 if (!ictx)
667 return -ENODEV;
668
669 mutex_lock(&ictx->lock);
670 if (ictx->rf_isassociating)
671 strcpy(buf, "associating\n");
672 else
673 strcpy(buf, "closed\n");
674
675 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
676 "instructions on how to associate your iMON 2.4G DT/LT "
677 "remote\n");
678 mutex_unlock(&ictx->lock);
679 return strlen(buf);
680}
681
682static ssize_t store_associate_remote(struct device *d,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
685{
686 struct imon_context *ictx;
687
688 ictx = dev_get_drvdata(d);
689
690 if (!ictx)
691 return -ENODEV;
692
693 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300694 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300695 send_associate_24g(ictx);
696 mutex_unlock(&ictx->lock);
697
698 return count;
699}
700
701/**
702 * sysfs functions to control internal imon clock
703 */
704static ssize_t show_imon_clock(struct device *d,
705 struct device_attribute *attr, char *buf)
706{
707 struct imon_context *ictx = dev_get_drvdata(d);
708 size_t len;
709
710 if (!ictx)
711 return -ENODEV;
712
713 mutex_lock(&ictx->lock);
714
715 if (!ictx->display_supported) {
716 len = snprintf(buf, PAGE_SIZE, "Not supported.");
717 } else {
718 len = snprintf(buf, PAGE_SIZE,
719 "To set the clock on your iMON display:\n"
720 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
721 "%s", ictx->display_isopen ?
722 "\nNOTE: imon device must be closed\n" : "");
723 }
724
725 mutex_unlock(&ictx->lock);
726
727 return len;
728}
729
730static ssize_t store_imon_clock(struct device *d,
731 struct device_attribute *attr,
732 const char *buf, size_t count)
733{
734 struct imon_context *ictx = dev_get_drvdata(d);
735 ssize_t retval;
736 unsigned int year, month, day, dow, hour, minute, second;
737
738 if (!ictx)
739 return -ENODEV;
740
741 mutex_lock(&ictx->lock);
742
743 if (!ictx->display_supported) {
744 retval = -ENODEV;
745 goto exit;
746 } else if (ictx->display_isopen) {
747 retval = -EBUSY;
748 goto exit;
749 }
750
751 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
752 &hour, &minute, &second) != 7) {
753 retval = -EINVAL;
754 goto exit;
755 }
756
757 if ((month < 1 || month > 12) ||
758 (day < 1 || day > 31) || (dow > 6) ||
759 (hour > 23) || (minute > 59) || (second > 59)) {
760 retval = -EINVAL;
761 goto exit;
762 }
763
764 retval = send_set_imon_clock(ictx, year, month, day, dow,
765 hour, minute, second);
766 if (retval)
767 goto exit;
768
769 retval = count;
770exit:
771 mutex_unlock(&ictx->lock);
772
773 return retval;
774}
775
776
777static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
778 store_imon_clock);
779
780static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
781 store_associate_remote);
782
783static struct attribute *imon_display_sysfs_entries[] = {
784 &dev_attr_imon_clock.attr,
785 NULL
786};
787
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300788static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300789 .attrs = imon_display_sysfs_entries
790};
791
792static struct attribute *imon_rf_sysfs_entries[] = {
793 &dev_attr_associate_remote.attr,
794 NULL
795};
796
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300797static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300798 .attrs = imon_rf_sysfs_entries
799};
800
801/**
802 * Writes data to the VFD. The iMON VFD is 2x16 characters
803 * and requires data in 5 consecutive USB interrupt packets,
804 * each packet but the last carrying 7 bytes.
805 *
806 * I don't know if the VFD board supports features such as
807 * scrolling, clearing rows, blanking, etc. so at
808 * the caller must provide a full screen of data. If fewer
809 * than 32 bytes are provided spaces will be appended to
810 * generate a full screen.
811 */
812static ssize_t vfd_write(struct file *file, const char *buf,
813 size_t n_bytes, loff_t *pos)
814{
815 int i;
816 int offset;
817 int seq;
818 int retval = 0;
819 struct imon_context *ictx;
820 const unsigned char vfd_packet6[] = {
821 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
822
Joe Perchesabf84382010-07-12 17:50:03 -0300823 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300824 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300825 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300826 return -ENODEV;
827 }
828
829 mutex_lock(&ictx->lock);
830
831 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300832 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300833 retval = -ENODEV;
834 goto exit;
835 }
836
837 if (n_bytes <= 0 || n_bytes > 32) {
Joe Perchese2302502010-06-20 04:20:46 -0300838 pr_err("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300839 retval = -EINVAL;
840 goto exit;
841 }
842
843 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
844 retval = -EFAULT;
845 goto exit;
846 }
847
848 /* Pad with spaces */
849 for (i = n_bytes; i < 32; ++i)
850 ictx->tx.data_buf[i] = ' ';
851
852 for (i = 32; i < 35; ++i)
853 ictx->tx.data_buf[i] = 0xFF;
854
855 offset = 0;
856 seq = 0;
857
858 do {
859 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
860 ictx->usb_tx_buf[7] = (unsigned char) seq;
861
862 retval = send_packet(ictx);
863 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300864 pr_err("send packet failed for packet #%d\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300865 goto exit;
866 } else {
867 seq += 2;
868 offset += 7;
869 }
870
871 } while (offset < 35);
872
873 /* Send packet #6 */
874 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
875 ictx->usb_tx_buf[7] = (unsigned char) seq;
876 retval = send_packet(ictx);
877 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300878 pr_err("send packet failed for packet #%d\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300879
880exit:
881 mutex_unlock(&ictx->lock);
882
883 return (!retval) ? n_bytes : retval;
884}
885
886/**
887 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
888 * packets. We accept data as 16 hexadecimal digits, followed by a
889 * newline (to make it easy to drive the device from a command-line
890 * -- even though the actual binary data is a bit complicated).
891 *
892 * The device itself is not a "traditional" text-mode display. It's
893 * actually a 16x96 pixel bitmap display. That means if you want to
894 * display text, you've got to have your own "font" and translate the
895 * text into bitmaps for display. This is really flexible (you can
896 * display whatever diacritics you need, and so on), but it's also
897 * a lot more complicated than most LCDs...
898 */
899static ssize_t lcd_write(struct file *file, const char *buf,
900 size_t n_bytes, loff_t *pos)
901{
902 int retval = 0;
903 struct imon_context *ictx;
904
Joe Perchesabf84382010-07-12 17:50:03 -0300905 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300906 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300907 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300908 return -ENODEV;
909 }
910
911 mutex_lock(&ictx->lock);
912
913 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300914 pr_err("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300915 retval = -ENODEV;
916 goto exit;
917 }
918
919 if (n_bytes != 8) {
Joe Perchese2302502010-06-20 04:20:46 -0300920 pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300921 retval = -EINVAL;
922 goto exit;
923 }
924
925 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
926 retval = -EFAULT;
927 goto exit;
928 }
929
930 retval = send_packet(ictx);
931 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300932 pr_err("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300933 goto exit;
934 } else {
935 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
936 __func__, (int) n_bytes);
937 }
938exit:
939 mutex_unlock(&ictx->lock);
940 return (!retval) ? n_bytes : retval;
941}
942
943/**
944 * Callback function for USB core API: transmit data
945 */
946static void usb_tx_callback(struct urb *urb)
947{
948 struct imon_context *ictx;
949
950 if (!urb)
951 return;
952 ictx = (struct imon_context *)urb->context;
953 if (!ictx)
954 return;
955
956 ictx->tx.status = urb->status;
957
958 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300959 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300960 smp_rmb(); /* ensure later readers know we're not busy */
961 complete(&ictx->tx.finished);
962}
963
964/**
Jarod Wilson21677cf2010-04-16 18:29:02 -0300965 * report touchscreen input
966 */
967static void imon_touch_display_timeout(unsigned long data)
968{
969 struct imon_context *ictx = (struct imon_context *)data;
970
Dan Carpenterf03900d2010-05-04 08:37:33 -0300971 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300972 return;
973
974 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
975 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
976 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
977 input_sync(ictx->touch);
978}
979
980/**
981 * iMON IR receivers support two different signal sets -- those used by
982 * the iMON remotes, and those used by the Windows MCE remotes (which is
983 * really just RC-6), but only one or the other at a time, as the signals
984 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -0300985 *
986 * This function gets called two different ways, one way is from
987 * rc_register_device, for initial protocol selection/setup, and the other is
988 * via a userspace-initiated protocol change request, either by direct sysfs
989 * prodding or by something like ir-keytable. In the rc_register_device case,
990 * the imon context lock is already held, but when initiated from userspace,
991 * it is not, so we must acquire it prior to calling send_packet, which
992 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300993 */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300994static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300995{
996 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -0300997 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300998 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -0300999 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001000 unsigned char ir_proto_packet[] = {
1001 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1002
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001003 if (rc_type && !(rc_type & rc->allowed_protos))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001004 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1005 "this device does not support\n");
1006
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001007 switch (rc_type) {
1008 case RC_TYPE_RC6:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001009 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1010 ir_proto_packet[0] = 0x01;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001011 break;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001012 case RC_TYPE_UNKNOWN:
1013 case RC_TYPE_OTHER:
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001014 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001015 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001016 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001017 /* ir_proto_packet[0] = 0x00; // already the default */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001018 rc_type = RC_TYPE_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001019 break;
1020 default:
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001021 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001022 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001023 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001024 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001025 /* ir_proto_packet[0] = 0x00; // already the default */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001026 rc_type = RC_TYPE_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001027 break;
1028 }
1029
1030 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1031
Jarod Wilson23ef7102011-04-27 19:01:44 -03001032 if (!mutex_is_locked(&ictx->lock)) {
1033 unlock = true;
1034 mutex_lock(&ictx->lock);
1035 }
1036
Jarod Wilson21677cf2010-04-16 18:29:02 -03001037 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001038 if (retval)
1039 goto out;
1040
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001041 ictx->rc_type = rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001042 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001043
1044out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001045 if (unlock)
1046 mutex_unlock(&ictx->lock);
1047
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001048 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001049}
1050
1051static inline int tv2int(const struct timeval *a, const struct timeval *b)
1052{
1053 int usecs = 0;
1054 int sec = 0;
1055
1056 if (b->tv_usec > a->tv_usec) {
1057 usecs = 1000000;
1058 sec--;
1059 }
1060
1061 usecs += a->tv_usec - b->tv_usec;
1062
1063 sec += a->tv_sec - b->tv_sec;
1064 sec *= 1000;
1065 usecs /= 1000;
1066 sec += usecs;
1067
1068 if (sec < 0)
1069 sec = 1000;
1070
1071 return sec;
1072}
1073
1074/**
1075 * The directional pad behaves a bit differently, depending on whether this is
1076 * one of the older ffdc devices or a newer device. Newer devices appear to
1077 * have a higher resolution matrix for more precise mouse movement, but it
1078 * makes things overly sensitive in keyboard mode, so we do some interesting
1079 * contortions to make it less touchy. Older devices run through the same
1080 * routine with shorter timeout and a smaller threshold.
1081 */
1082static int stabilize(int a, int b, u16 timeout, u16 threshold)
1083{
1084 struct timeval ct;
1085 static struct timeval prev_time = {0, 0};
1086 static struct timeval hit_time = {0, 0};
1087 static int x, y, prev_result, hits;
1088 int result = 0;
1089 int msec, msec_hit;
1090
1091 do_gettimeofday(&ct);
1092 msec = tv2int(&ct, &prev_time);
1093 msec_hit = tv2int(&ct, &hit_time);
1094
1095 if (msec > 100) {
1096 x = 0;
1097 y = 0;
1098 hits = 0;
1099 }
1100
1101 x += a;
1102 y += b;
1103
1104 prev_time = ct;
1105
1106 if (abs(x) > threshold || abs(y) > threshold) {
1107 if (abs(y) > abs(x))
1108 result = (y > 0) ? 0x7F : 0x80;
1109 else
1110 result = (x > 0) ? 0x7F00 : 0x8000;
1111
1112 x = 0;
1113 y = 0;
1114
1115 if (result == prev_result) {
1116 hits++;
1117
1118 if (hits > 3) {
1119 switch (result) {
1120 case 0x7F:
1121 y = 17 * threshold / 30;
1122 break;
1123 case 0x80:
1124 y -= 17 * threshold / 30;
1125 break;
1126 case 0x7F00:
1127 x = 17 * threshold / 30;
1128 break;
1129 case 0x8000:
1130 x -= 17 * threshold / 30;
1131 break;
1132 }
1133 }
1134
1135 if (hits == 2 && msec_hit < timeout) {
1136 result = 0;
1137 hits = 1;
1138 }
1139 } else {
1140 prev_result = result;
1141 hits = 1;
1142 hit_time = ct;
1143 }
1144 }
1145
1146 return result;
1147}
1148
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001149static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001150{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001151 u32 keycode;
1152 u32 release;
1153 bool is_release_code = false;
1154
1155 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001156 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001157 ictx->rc_toggle = 0x0;
1158 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001159
1160 /* Look for the release of a button */
1161 if (keycode == KEY_RESERVED) {
1162 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001163 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001164 if (keycode != KEY_RESERVED)
1165 is_release_code = true;
1166 }
1167
1168 ictx->release_code = is_release_code;
1169
1170 return keycode;
1171}
1172
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001173static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001174{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001175 u32 keycode;
1176
1177#define MCE_KEY_MASK 0x7000
1178#define MCE_TOGGLE_BIT 0x8000
1179
1180 /*
1181 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1182 * (the toggle bit flipping between alternating key presses), while
1183 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1184 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1185 * but we can't or them into all codes, as some keys are decoded in
1186 * a different way w/o the same use of the toggle bit...
1187 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001188 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001189 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1190
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001191 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001192 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001193
1194 /* not used in mce mode, but make sure we know its false */
1195 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001196
1197 return keycode;
1198}
1199
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001200static u32 imon_panel_key_lookup(u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001201{
1202 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001203 u32 keycode = KEY_RESERVED;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001204
Jarod Wilson083e4722010-05-04 16:17:05 -03001205 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1206 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1207 keycode = imon_panel_key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001208 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001209 }
1210 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001211
1212 return keycode;
1213}
1214
1215static bool imon_mouse_event(struct imon_context *ictx,
1216 unsigned char *buf, int len)
1217{
1218 char rel_x = 0x00, rel_y = 0x00;
1219 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001220 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001221 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001222 unsigned long flags;
1223
1224 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001225
1226 /* newer iMON device PAD or mouse button */
1227 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1228 rel_x = buf[2];
1229 rel_y = buf[3];
1230 right_shift = 1;
1231 /* 0xffdc iMON PAD or mouse button input */
1232 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1233 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1234 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1235 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1236 if (buf[0] & 0x02)
1237 rel_x |= ~0x0f;
1238 rel_x = rel_x + rel_x / 2;
1239 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1240 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1241 if (buf[0] & 0x01)
1242 rel_y |= ~0x0f;
1243 rel_y = rel_y + rel_y / 2;
1244 right_shift = 2;
1245 /* some ffdc devices decode mouse buttons differently... */
1246 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1247 right_shift = 2;
1248 /* ch+/- buttons, which we use for an emulated scroll wheel */
1249 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1250 dir = 1;
1251 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1252 dir = -1;
1253 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001254 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001255
Jarod Wilson693508d2010-09-15 15:56:03 -03001256 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1257
Jarod Wilson21677cf2010-04-16 18:29:02 -03001258 if (mouse_input) {
1259 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1260
1261 if (dir) {
1262 input_report_rel(ictx->idev, REL_WHEEL, dir);
1263 } else if (rel_x || rel_y) {
1264 input_report_rel(ictx->idev, REL_X, rel_x);
1265 input_report_rel(ictx->idev, REL_Y, rel_y);
1266 } else {
1267 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1268 input_report_key(ictx->idev, BTN_RIGHT,
1269 buf[1] >> right_shift & 0x1);
1270 }
1271 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001272 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001273 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001274 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001275 }
1276
1277 return mouse_input;
1278}
1279
1280static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1281{
1282 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1283 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1284 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1285 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1286 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1287 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1288 input_sync(ictx->touch);
1289}
1290
1291static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1292{
1293 int dir = 0;
1294 char rel_x = 0x00, rel_y = 0x00;
1295 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001296 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001297 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001298
1299 /*
1300 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1301 * contain a position coordinate (x,y), with each component ranging
1302 * from -14 to 14. We want to down-sample this to only 4 discrete values
1303 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001304 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001305 * ignore when they get too close.
1306 */
1307 if (ictx->product != 0xffdc) {
1308 /* first, pad to 8 bytes so it conforms with everything else */
1309 buf[5] = buf[6] = buf[7] = 0;
1310 timeout = 500; /* in msecs */
1311 /* (2*threshold) x (2*threshold) square */
1312 threshold = pad_thresh ? pad_thresh : 28;
1313 rel_x = buf[2];
1314 rel_y = buf[3];
1315
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001316 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001317 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1318 dir = stabilize((int)rel_x, (int)rel_y,
1319 timeout, threshold);
1320 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001321 spin_lock_irqsave(&ictx->kc_lock,
1322 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001323 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001324 spin_unlock_irqrestore(&ictx->kc_lock,
1325 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001326 return;
1327 }
1328 buf[2] = dir & 0xFF;
1329 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001330 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001331 }
1332 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001333 /*
1334 * Hack alert: instead of using keycodes, we have
1335 * to use hard-coded scancodes here...
1336 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001337 if (abs(rel_y) > abs(rel_x)) {
1338 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1339 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001340 if (rel_y > 0)
1341 scancode = 0x01007f00; /* KEY_DOWN */
1342 else
1343 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001344 } else {
1345 buf[2] = 0;
1346 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001347 if (rel_x > 0)
1348 scancode = 0x0100007f; /* KEY_RIGHT */
1349 else
1350 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001351 }
1352 }
1353
1354 /*
1355 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1356 * device (15c2:ffdc). The remote generates various codes from
1357 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1358 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1359 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1360 * reversed endianess. Extract direction from buffer, rotate endianess,
1361 * adjust sign and feed the values into stabilize(). The resulting codes
1362 * will be 0x01008000, 0x01007F00, which match the newer devices.
1363 */
1364 } else {
1365 timeout = 10; /* in msecs */
1366 /* (2*threshold) x (2*threshold) square */
1367 threshold = pad_thresh ? pad_thresh : 15;
1368
1369 /* buf[1] is x */
1370 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1371 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1372 if (buf[0] & 0x02)
1373 rel_x |= ~0x10+1;
1374 /* buf[2] is y */
1375 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1376 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1377 if (buf[0] & 0x01)
1378 rel_y |= ~0x10+1;
1379
1380 buf[0] = 0x01;
1381 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1382
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001383 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001384 dir = stabilize((int)rel_x, (int)rel_y,
1385 timeout, threshold);
1386 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001387 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001388 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001389 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001390 return;
1391 }
1392 buf[2] = dir & 0xFF;
1393 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001394 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001395 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001396 /*
1397 * Hack alert: instead of using keycodes, we have
1398 * to use hard-coded scancodes here...
1399 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001400 if (abs(rel_y) > abs(rel_x)) {
1401 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1402 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001403 if (rel_y > 0)
1404 scancode = 0x01007f00; /* KEY_DOWN */
1405 else
1406 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001407 } else {
1408 buf[2] = 0;
1409 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001410 if (rel_x > 0)
1411 scancode = 0x0100007f; /* KEY_RIGHT */
1412 else
1413 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001414 }
1415 }
1416 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001417
Jarod Wilson693508d2010-09-15 15:56:03 -03001418 if (scancode) {
1419 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001420 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001421 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1422 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001423}
1424
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001425/**
1426 * figure out if these is a press or a release. We don't actually
1427 * care about repeats, as those will be auto-generated within the IR
1428 * subsystem for repeating scancodes.
1429 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001430static int imon_parse_press_type(struct imon_context *ictx,
1431 unsigned char *buf, u8 ktype)
1432{
1433 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001434 unsigned long flags;
1435
1436 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001437
1438 /* key release of 0x02XXXXXX key */
1439 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1440 ictx->kc = ictx->last_keycode;
1441
1442 /* mouse button release on (some) 0xffdc devices */
1443 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1444 buf[2] == 0x81 && buf[3] == 0xb7)
1445 ictx->kc = ictx->last_keycode;
1446
1447 /* mouse button release on (some other) 0xffdc devices */
1448 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1449 buf[2] == 0x81 && buf[3] == 0xb7)
1450 ictx->kc = ictx->last_keycode;
1451
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001452 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001453 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001454 ictx->rc_toggle = buf[2];
1455 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001456
1457 /* incoherent or irrelevant data */
1458 } else if (ictx->kc == KEY_RESERVED)
1459 press_type = -EINVAL;
1460
1461 /* key release of 0xXXXXXXb7 key */
1462 else if (ictx->release_code)
1463 press_type = 0;
1464
1465 /* this is a button press */
1466 else
1467 press_type = 1;
1468
Jarod Wilson693508d2010-09-15 15:56:03 -03001469 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1470
Jarod Wilson21677cf2010-04-16 18:29:02 -03001471 return press_type;
1472}
1473
1474/**
1475 * Process the incoming packet
1476 */
1477static void imon_incoming_packet(struct imon_context *ictx,
1478 struct urb *urb, int intf)
1479{
1480 int len = urb->actual_length;
1481 unsigned char *buf = urb->transfer_buffer;
1482 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001483 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001484 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001485 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001486 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001487 int press_type = 0;
1488 int msec;
1489 struct timeval t;
1490 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001491 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001492
Jarod Wilson21677cf2010-04-16 18:29:02 -03001493 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001494 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001495 return;
1496
1497 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001498 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001499 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001500 ktype = IMON_KEY_PANEL;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001501 kc = imon_panel_key_lookup(scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001502 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001503 scancode = be32_to_cpu(*((u32 *)buf));
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001504 if (ictx->rc_type == RC_TYPE_RC6) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001505 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001506 if (buf[0] == 0x80)
1507 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001508 kc = imon_mce_key_lookup(ictx, scancode);
1509 } else {
1510 ktype = IMON_KEY_IMON;
1511 kc = imon_remote_key_lookup(ictx, scancode);
1512 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001513 }
1514
Jarod Wilson693508d2010-09-15 15:56:03 -03001515 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001516 /* keyboard/mouse mode toggle button */
1517 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1518 ictx->last_keycode = kc;
1519 if (!nomouse) {
1520 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1521 dev_dbg(dev, "toggling to %s mode\n",
1522 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001523 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001524 return;
1525 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001526 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001527 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1528 }
1529 }
1530
1531 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001532 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001533
1534 /* send touchscreen events through input subsystem if touchpad data */
1535 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1536 buf[7] == 0x86) {
1537 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001538 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001539
1540 /* look for mouse events with pad in mouse mode */
1541 } else if (ictx->pad_mouse) {
1542 if (imon_mouse_event(ictx, buf, len))
1543 return;
1544 }
1545
1546 /* Now for some special handling to convert pad input to arrow keys */
1547 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1548 ((len == 8) && (buf[0] & 0x40) &&
1549 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1550 len = 8;
1551 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001552 }
1553
1554 if (debug) {
1555 printk(KERN_INFO "intf%d decoded packet: ", intf);
1556 for (i = 0; i < len; ++i)
1557 printk("%02x ", buf[i]);
1558 printk("\n");
1559 }
1560
1561 press_type = imon_parse_press_type(ictx, buf, ktype);
1562 if (press_type < 0)
1563 goto not_input_data;
1564
Jarod Wilson693508d2010-09-15 15:56:03 -03001565 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001566 if (ictx->kc == KEY_UNKNOWN)
1567 goto unknown_key;
Jarod Wilson693508d2010-09-15 15:56:03 -03001568 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001569
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001570 if (ktype != IMON_KEY_PANEL) {
1571 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001572 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001573 else {
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001574 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001575 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001576 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001577 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001578 }
1579 return;
1580 }
1581
1582 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001583 spin_lock_irqsave(&ictx->kc_lock, flags);
1584
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001585 /* KEY_MUTE repeats from knob need to be suppressed */
1586 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001587 do_gettimeofday(&t);
1588 msec = tv2int(&t, &prev_time);
1589 prev_time = t;
Jarod Wilson428cc762010-10-23 16:42:20 -03001590 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001591 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001592 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001593 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001594 }
Jarod Wilson693508d2010-09-15 15:56:03 -03001595 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001596
Jarod Wilson693508d2010-09-15 15:56:03 -03001597 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001598
Jarod Wilson428cc762010-10-23 16:42:20 -03001599 input_report_key(ictx->idev, kc, press_type);
1600 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001601
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001602 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001603 input_report_key(ictx->idev, kc, 0);
1604 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001605
Jarod Wilson693508d2010-09-15 15:56:03 -03001606 ictx->last_keycode = kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001607
1608 return;
1609
1610unknown_key:
Jarod Wilson693508d2010-09-15 15:56:03 -03001611 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001612 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001613 (long long)scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001614 return;
1615
1616not_input_data:
1617 if (len != 8) {
1618 dev_warn(dev, "imon %s: invalid incoming packet "
1619 "size (len = %d, intf%d)\n", __func__, len, intf);
1620 return;
1621 }
1622
1623 /* iMON 2.4G associate frame */
1624 if (buf[0] == 0x00 &&
1625 buf[2] == 0xFF && /* REFID */
1626 buf[3] == 0xFF &&
1627 buf[4] == 0xFF &&
1628 buf[5] == 0xFF && /* iMON 2.4G */
1629 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1630 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1631 dev_warn(dev, "%s: remote associated refid=%02X\n",
1632 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001633 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001634 }
1635}
1636
1637/**
1638 * Callback function for USB core API: receive data
1639 */
1640static void usb_rx_callback_intf0(struct urb *urb)
1641{
1642 struct imon_context *ictx;
1643 int intfnum = 0;
1644
1645 if (!urb)
1646 return;
1647
1648 ictx = (struct imon_context *)urb->context;
1649 if (!ictx)
1650 return;
1651
1652 switch (urb->status) {
1653 case -ENOENT: /* usbcore unlink successful! */
1654 return;
1655
1656 case -ESHUTDOWN: /* transport endpoint was shut down */
1657 break;
1658
1659 case 0:
1660 imon_incoming_packet(ictx, urb, intfnum);
1661 break;
1662
1663 default:
1664 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1665 __func__, urb->status);
1666 break;
1667 }
1668
1669 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1670}
1671
1672static void usb_rx_callback_intf1(struct urb *urb)
1673{
1674 struct imon_context *ictx;
1675 int intfnum = 1;
1676
1677 if (!urb)
1678 return;
1679
1680 ictx = (struct imon_context *)urb->context;
1681 if (!ictx)
1682 return;
1683
1684 switch (urb->status) {
1685 case -ENOENT: /* usbcore unlink successful! */
1686 return;
1687
1688 case -ESHUTDOWN: /* transport endpoint was shut down */
1689 break;
1690
1691 case 0:
1692 imon_incoming_packet(ictx, urb, intfnum);
1693 break;
1694
1695 default:
1696 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1697 __func__, urb->status);
1698 break;
1699 }
1700
1701 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1702}
1703
Jarod Wilson04292fc2010-09-15 00:28:41 -03001704/*
1705 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1706 * devices, and all of them constantly spew interrupts, even when there
1707 * is no actual data to report. However, byte 6 of this buffer looks like
1708 * its unique across device variants, so we're trying to key off that to
1709 * figure out which display type (if any) and what IR protocol the device
1710 * actually supports. These devices have their IR protocol hard-coded into
1711 * their firmware, they can't be changed on the fly like the newer hardware.
1712 */
1713static void imon_get_ffdc_type(struct imon_context *ictx)
1714{
1715 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1716 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001717 u64 allowed_protos = RC_TYPE_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001718
1719 switch (ffdc_cfg_byte) {
1720 /* iMON Knob, no display, iMON IR + vol knob */
1721 case 0x21:
1722 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1723 ictx->display_supported = false;
1724 break;
1725 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1726 case 0x4e:
1727 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1728 ictx->display_supported = false;
1729 ictx->rf_device = true;
1730 break;
1731 /* iMON VFD, no IR (does have vol knob tho) */
1732 case 0x35:
1733 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1734 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1735 break;
1736 /* iMON VFD, iMON IR */
1737 case 0x24:
1738 case 0x85:
1739 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1740 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1741 break;
1742 /* iMON VFD, MCE IR */
1743 case 0x9e:
1744 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1745 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001746 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001747 break;
1748 /* iMON LCD, MCE IR */
1749 case 0x9f:
1750 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1751 detected_display_type = IMON_DISPLAY_TYPE_LCD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001752 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001753 break;
1754 default:
1755 dev_info(ictx->dev, "Unknown 0xffdc device, "
1756 "defaulting to VFD and iMON IR");
1757 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1758 break;
1759 }
1760
1761 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1762
1763 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001764 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001765}
1766
1767static void imon_set_display_type(struct imon_context *ictx)
1768{
1769 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1770
1771 /*
1772 * Try to auto-detect the type of display if the user hasn't set
1773 * it by hand via the display_type modparam. Default is VFD.
1774 */
1775
1776 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1777 switch (ictx->product) {
1778 case 0xffdc:
1779 /* set in imon_get_ffdc_type() */
1780 configured_display_type = ictx->display_type;
1781 break;
1782 case 0x0034:
1783 case 0x0035:
1784 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1785 break;
1786 case 0x0038:
1787 case 0x0039:
1788 case 0x0045:
1789 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1790 break;
1791 case 0x003c:
1792 case 0x0041:
1793 case 0x0042:
1794 case 0x0043:
1795 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1796 ictx->display_supported = false;
1797 break;
1798 case 0x0036:
1799 case 0x0044:
1800 default:
1801 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1802 break;
1803 }
1804 } else {
1805 configured_display_type = display_type;
1806 if (display_type == IMON_DISPLAY_TYPE_NONE)
1807 ictx->display_supported = false;
1808 else
1809 ictx->display_supported = true;
1810 dev_info(ictx->dev, "%s: overriding display type to %d via "
1811 "modparam\n", __func__, display_type);
1812 }
1813
1814 ictx->display_type = configured_display_type;
1815}
1816
David Härdemand8b4b582010-10-29 16:08:23 -03001817static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001818{
David Härdemand8b4b582010-10-29 16:08:23 -03001819 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001820 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001821 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1822 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001823
David Härdemand8b4b582010-10-29 16:08:23 -03001824 rdev = rc_allocate_device();
1825 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001826 dev_err(ictx->dev, "remote control dev allocation failed\n");
1827 goto out;
1828 }
1829
1830 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1831 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1832 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1833 sizeof(ictx->phys_rdev));
1834 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1835
David Härdemand8b4b582010-10-29 16:08:23 -03001836 rdev->input_name = ictx->name_rdev;
1837 rdev->input_phys = ictx->phys_rdev;
1838 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001839 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001840
David Härdemand8b4b582010-10-29 16:08:23 -03001841 rdev->priv = ictx;
1842 rdev->driver_type = RC_DRIVER_SCANCODE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001843 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001844 rdev->change_protocol = imon_ir_change_protocol;
1845 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001846
Jarod Wilson04292fc2010-09-15 00:28:41 -03001847 /* Enable front-panel buttons and/or knobs */
1848 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1849 ret = send_packet(ictx);
1850 /* Not fatal, but warn about it */
1851 if (ret)
1852 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1853
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001854 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001855 imon_get_ffdc_type(ictx);
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001856 rdev->allowed_protos = ictx->rc_type;
1857 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001858
1859 imon_set_display_type(ictx);
1860
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001861 if (ictx->rc_type == RC_TYPE_RC6)
1862 rdev->map_name = RC_MAP_IMON_MCE;
1863 else
1864 rdev->map_name = RC_MAP_IMON_PAD;
1865
David Härdemand8b4b582010-10-29 16:08:23 -03001866 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001867 if (ret < 0) {
1868 dev_err(ictx->dev, "remote input dev register failed\n");
1869 goto out;
1870 }
1871
1872 return rdev;
1873
1874out:
David Härdemand8b4b582010-10-29 16:08:23 -03001875 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001876 return NULL;
1877}
1878
Jarod Wilson21677cf2010-04-16 18:29:02 -03001879static struct input_dev *imon_init_idev(struct imon_context *ictx)
1880{
1881 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001882 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001883
1884 idev = input_allocate_device();
1885 if (!idev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001886 dev_err(ictx->dev, "input dev allocation failed\n");
1887 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001888 }
1889
Jarod Wilson21677cf2010-04-16 18:29:02 -03001890 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001891 "iMON Panel, Knob and Mouse(%04x:%04x)",
1892 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001893 idev->name = ictx->name_idev;
1894
1895 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1896 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001897 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001898 idev->phys = ictx->phys_idev;
1899
Jarod Wilsondb190fc2010-04-30 16:06:12 -03001900 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001901
1902 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1903 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1904 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1905 BIT_MASK(REL_WHEEL);
1906
1907 /* panel and/or knob code support */
1908 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1909 u32 kc = imon_panel_key_table[i].keycode;
1910 __set_bit(kc, idev->keybit);
1911 }
1912
Jarod Wilson21677cf2010-04-16 18:29:02 -03001913 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1914 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001915 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001916
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001917 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001918 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001919 dev_err(ictx->dev, "input dev register failed\n");
1920 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001921 }
1922
1923 return idev;
1924
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001925out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001926 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001927 return NULL;
1928}
1929
1930static struct input_dev *imon_init_touch(struct imon_context *ictx)
1931{
1932 struct input_dev *touch;
1933 int ret;
1934
1935 touch = input_allocate_device();
1936 if (!touch) {
1937 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1938 goto touch_alloc_failed;
1939 }
1940
1941 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1942 "iMON USB Touchscreen (%04x:%04x)",
1943 ictx->vendor, ictx->product);
1944 touch->name = ictx->name_touch;
1945
1946 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1947 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001948 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001949 touch->phys = ictx->phys_touch;
1950
1951 touch->evbit[0] =
1952 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1953 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1954 BIT_MASK(BTN_TOUCH);
1955 input_set_abs_params(touch, ABS_X,
1956 0x00, 0xfff, 0, 0);
1957 input_set_abs_params(touch, ABS_Y,
1958 0x00, 0xfff, 0, 0);
1959
1960 input_set_drvdata(touch, ictx);
1961
1962 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1963 touch->dev.parent = ictx->dev;
1964 ret = input_register_device(touch);
1965 if (ret < 0) {
1966 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1967 goto touch_register_failed;
1968 }
1969
1970 return touch;
1971
1972touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03001973 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001974
1975touch_alloc_failed:
1976 return NULL;
1977}
1978
1979static bool imon_find_endpoints(struct imon_context *ictx,
1980 struct usb_host_interface *iface_desc)
1981{
1982 struct usb_endpoint_descriptor *ep;
1983 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1984 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1985 int ifnum = iface_desc->desc.bInterfaceNumber;
1986 int num_endpts = iface_desc->desc.bNumEndpoints;
1987 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001988 bool ir_ep_found = false;
1989 bool display_ep_found = false;
1990 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001991
1992 /*
1993 * Scan the endpoint list and set:
1994 * first input endpoint = IR endpoint
1995 * first output endpoint = display endpoint
1996 */
1997 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
1998 ep = &iface_desc->endpoint[i].desc;
1999 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2000 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2001
2002 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2003 ep_type == USB_ENDPOINT_XFER_INT) {
2004
2005 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002006 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002007 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2008
2009 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2010 ep_type == USB_ENDPOINT_XFER_INT) {
2011 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002012 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002013 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2014 }
2015 }
2016
2017 if (ifnum == 0) {
2018 ictx->rx_endpoint_intf0 = rx_endpoint;
2019 /*
2020 * tx is used to send characters to lcd/vfd, associate RF
2021 * remotes, set IR protocol, and maybe more...
2022 */
2023 ictx->tx_endpoint = tx_endpoint;
2024 } else {
2025 ictx->rx_endpoint_intf1 = rx_endpoint;
2026 }
2027
2028 /*
2029 * If we didn't find a display endpoint, this is probably one of the
2030 * newer iMON devices that use control urb instead of interrupt
2031 */
2032 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002033 tx_control = true;
2034 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002035 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2036 "interface OUT endpoint\n", __func__);
2037 }
2038
2039 /*
2040 * Some iMON receivers have no display. Unfortunately, it seems
2041 * that SoundGraph recycles device IDs between devices both with
2042 * and without... :\
2043 */
2044 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002045 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002046 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2047 }
2048
2049 /*
2050 * iMON Touch devices have a VGA touchscreen, but no "display", as
2051 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2052 */
2053 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002054 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002055 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2056 }
2057
2058 /* Input endpoint is mandatory */
2059 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002060 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002061
2062 ictx->tx_control = tx_control;
2063
2064 if (display_ep_found)
2065 ictx->display_supported = true;
2066
2067 return ir_ep_found;
2068
2069}
2070
2071static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2072{
2073 struct imon_context *ictx;
2074 struct urb *rx_urb;
2075 struct urb *tx_urb;
2076 struct device *dev = &intf->dev;
2077 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002078 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002079
2080 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2081 if (!ictx) {
2082 dev_err(dev, "%s: kzalloc failed for context", __func__);
2083 goto exit;
2084 }
2085 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2086 if (!rx_urb) {
2087 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2088 goto rx_urb_alloc_failed;
2089 }
2090 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2091 if (!tx_urb) {
2092 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2093 __func__);
2094 goto tx_urb_alloc_failed;
2095 }
2096
2097 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002098 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002099
2100 mutex_lock(&ictx->lock);
2101
2102 ictx->dev = dev;
2103 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002104 ictx->dev_present_intf0 = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002105 ictx->rx_urb_intf0 = rx_urb;
2106 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002107 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002108
2109 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2110 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2111
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002112 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002113 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002114 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002115 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002116 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002117
Jarod Wilson21677cf2010-04-16 18:29:02 -03002118 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2119 usb_rcvintpipe(ictx->usbdev_intf0,
2120 ictx->rx_endpoint_intf0->bEndpointAddress),
2121 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2122 usb_rx_callback_intf0, ictx,
2123 ictx->rx_endpoint_intf0->bInterval);
2124
2125 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2126 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002127 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002128 goto urb_submit_failed;
2129 }
2130
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002131 ictx->idev = imon_init_idev(ictx);
2132 if (!ictx->idev) {
2133 dev_err(dev, "%s: input device setup failed\n", __func__);
2134 goto idev_setup_failed;
2135 }
2136
2137 ictx->rdev = imon_init_rdev(ictx);
2138 if (!ictx->rdev) {
2139 dev_err(dev, "%s: rc device setup failed\n", __func__);
2140 goto rdev_setup_failed;
2141 }
2142
Jarod Wilson23ef7102011-04-27 19:01:44 -03002143 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002144 return ictx;
2145
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002146rdev_setup_failed:
2147 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002148idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002149 usb_kill_urb(ictx->rx_urb_intf0);
2150urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002151find_endpoint_failed:
2152 mutex_unlock(&ictx->lock);
2153 usb_free_urb(tx_urb);
2154tx_urb_alloc_failed:
2155 usb_free_urb(rx_urb);
2156rx_urb_alloc_failed:
2157 kfree(ictx);
2158exit:
2159 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2160
2161 return NULL;
2162}
2163
2164static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2165 struct imon_context *ictx)
2166{
2167 struct urb *rx_urb;
2168 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002169 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002170
2171 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2172 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002173 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002174 goto rx_urb_alloc_failed;
2175 }
2176
2177 mutex_lock(&ictx->lock);
2178
2179 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2180 init_timer(&ictx->ttimer);
2181 ictx->ttimer.data = (unsigned long)ictx;
2182 ictx->ttimer.function = imon_touch_display_timeout;
2183 }
2184
2185 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002186 ictx->dev_present_intf1 = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002187 ictx->rx_urb_intf1 = rx_urb;
2188
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002189 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002190 iface_desc = intf->cur_altsetting;
2191 if (!imon_find_endpoints(ictx, iface_desc))
2192 goto find_endpoint_failed;
2193
2194 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2195 ictx->touch = imon_init_touch(ictx);
2196 if (!ictx->touch)
2197 goto touch_setup_failed;
2198 } else
2199 ictx->touch = NULL;
2200
2201 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2202 usb_rcvintpipe(ictx->usbdev_intf1,
2203 ictx->rx_endpoint_intf1->bEndpointAddress),
2204 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2205 usb_rx_callback_intf1, ictx,
2206 ictx->rx_endpoint_intf1->bInterval);
2207
2208 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2209
2210 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002211 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002212 goto urb_submit_failed;
2213 }
2214
Jarod Wilson23ef7102011-04-27 19:01:44 -03002215 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002216 return ictx;
2217
2218urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002219 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002220 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002221touch_setup_failed:
2222find_endpoint_failed:
2223 mutex_unlock(&ictx->lock);
2224 usb_free_urb(rx_urb);
2225rx_urb_alloc_failed:
2226 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2227
2228 return NULL;
2229}
2230
Jarod Wilson21677cf2010-04-16 18:29:02 -03002231static void imon_init_display(struct imon_context *ictx,
2232 struct usb_interface *intf)
2233{
2234 int ret;
2235
2236 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2237
2238 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002239 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002240 if (ret)
2241 dev_err(ictx->dev, "Could not create display sysfs "
2242 "entries(%d)", ret);
2243
2244 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2245 ret = usb_register_dev(intf, &imon_lcd_class);
2246 else
2247 ret = usb_register_dev(intf, &imon_vfd_class);
2248 if (ret)
2249 /* Not a fatal error, so ignore */
2250 dev_info(ictx->dev, "could not get a minor number for "
2251 "display\n");
2252
2253}
2254
2255/**
2256 * Callback function for USB core API: Probe
2257 */
2258static int __devinit imon_probe(struct usb_interface *interface,
2259 const struct usb_device_id *id)
2260{
2261 struct usb_device *usbdev = NULL;
2262 struct usb_host_interface *iface_desc = NULL;
2263 struct usb_interface *first_if;
2264 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002265 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002266 int ret = 0;
2267 struct imon_context *ictx = NULL;
2268 struct imon_context *first_if_ctx = NULL;
2269 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002270
Jarod Wilson21677cf2010-04-16 18:29:02 -03002271 usbdev = usb_get_dev(interface_to_usbdev(interface));
2272 iface_desc = interface->cur_altsetting;
2273 ifnum = iface_desc->desc.bInterfaceNumber;
2274 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2275 product = le16_to_cpu(usbdev->descriptor.idProduct);
2276
2277 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2278 __func__, vendor, product, ifnum);
2279
2280 /* prevent races probing devices w/multiple interfaces */
2281 mutex_lock(&driver_lock);
2282
2283 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002284 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002285
2286 if (ifnum == 0) {
2287 ictx = imon_init_intf0(interface);
2288 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002289 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002290 ret = -ENODEV;
2291 goto fail;
2292 }
2293
Jarod Wilson21677cf2010-04-16 18:29:02 -03002294 } else {
2295 /* this is the secondary interface on the device */
2296 ictx = imon_init_intf1(interface, first_if_ctx);
2297 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002298 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002299 ret = -ENODEV;
2300 goto fail;
2301 }
2302
2303 }
2304
2305 usb_set_intfdata(interface, ictx);
2306
2307 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002308 mutex_lock(&ictx->lock);
2309
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002310 if (product == 0xffdc && ictx->rf_device) {
2311 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002312 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002313 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002314 pr_err("Could not create RF sysfs entries(%d)\n",
2315 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002316 }
2317
Jarod Wilson21677cf2010-04-16 18:29:02 -03002318 if (ictx->display_supported)
2319 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002320
2321 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002322 }
2323
Jarod Wilson21677cf2010-04-16 18:29:02 -03002324 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2325 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2326 usbdev->bus->busnum, usbdev->devnum);
2327
Jarod Wilson21677cf2010-04-16 18:29:02 -03002328 mutex_unlock(&driver_lock);
2329
2330 return 0;
2331
2332fail:
2333 mutex_unlock(&driver_lock);
2334 dev_err(dev, "unable to register, err %d\n", ret);
2335
2336 return ret;
2337}
2338
2339/**
2340 * Callback function for USB core API: disconnect
2341 */
2342static void __devexit imon_disconnect(struct usb_interface *interface)
2343{
2344 struct imon_context *ictx;
2345 struct device *dev;
2346 int ifnum;
2347
2348 /* prevent races with multi-interface device probing and display_open */
2349 mutex_lock(&driver_lock);
2350
2351 ictx = usb_get_intfdata(interface);
2352 dev = ictx->dev;
2353 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2354
Jarod Wilson21677cf2010-04-16 18:29:02 -03002355 /*
2356 * sysfs_remove_group is safe to call even if sysfs_create_group
2357 * hasn't been called
2358 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002359 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2360 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002361
2362 usb_set_intfdata(interface, NULL);
2363
2364 /* Abort ongoing write */
2365 if (ictx->tx.busy) {
2366 usb_kill_urb(ictx->tx_urb);
2367 complete_all(&ictx->tx.finished);
2368 }
2369
2370 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002371 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002372 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002373 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002374 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002375 if (ictx->display_supported) {
2376 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2377 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002378 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002379 usb_deregister_dev(interface, &imon_vfd_class);
2380 }
2381 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002382 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002383 usb_kill_urb(ictx->rx_urb_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002384 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002385 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002386 del_timer_sync(&ictx->ttimer);
2387 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002388 }
2389
Jarod Wilson76a2d212011-04-28 18:20:58 -03002390 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2391 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002392
2393 mutex_unlock(&driver_lock);
2394
2395 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2396 __func__, ifnum);
2397}
2398
2399static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2400{
2401 struct imon_context *ictx = usb_get_intfdata(intf);
2402 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2403
2404 if (ifnum == 0)
2405 usb_kill_urb(ictx->rx_urb_intf0);
2406 else
2407 usb_kill_urb(ictx->rx_urb_intf1);
2408
2409 return 0;
2410}
2411
2412static int imon_resume(struct usb_interface *intf)
2413{
2414 int rc = 0;
2415 struct imon_context *ictx = usb_get_intfdata(intf);
2416 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2417
2418 if (ifnum == 0) {
2419 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2420 usb_rcvintpipe(ictx->usbdev_intf0,
2421 ictx->rx_endpoint_intf0->bEndpointAddress),
2422 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2423 usb_rx_callback_intf0, ictx,
2424 ictx->rx_endpoint_intf0->bInterval);
2425
2426 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2427
2428 } else {
2429 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2430 usb_rcvintpipe(ictx->usbdev_intf1,
2431 ictx->rx_endpoint_intf1->bEndpointAddress),
2432 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2433 usb_rx_callback_intf1, ictx,
2434 ictx->rx_endpoint_intf1->bInterval);
2435
2436 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2437 }
2438
2439 return rc;
2440}
2441
2442static int __init imon_init(void)
2443{
2444 int rc;
2445
2446 rc = usb_register(&imon_driver);
2447 if (rc) {
Joe Perchese2302502010-06-20 04:20:46 -03002448 pr_err("usb register failed(%d)\n", rc);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002449 rc = -ENODEV;
2450 }
2451
2452 return rc;
2453}
2454
2455static void __exit imon_exit(void)
2456{
2457 usb_deregister(&imon_driver);
2458}
2459
2460module_init(imon_init);
2461module_exit(imon_exit);