blob: 7136582a857375465802185fb877af914e11a517 [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 Wilson443b3912011-06-04 14:00:54 -0300310 { 0x00000001ffffffeell, KEY_MUTE },
311 { 0x0000000fffffffeell, KEY_MEDIA },
312 { 0x00000012ffffffeell, KEY_UP },
313 { 0x00000013ffffffeell, KEY_DOWN },
314 { 0x00000014ffffffeell, KEY_LEFT },
315 { 0x00000015ffffffeell, KEY_RIGHT },
316 { 0x00000016ffffffeell, KEY_ENTER },
317 { 0x00000017ffffffeell, KEY_ESC },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300318 /* iMON Knob values */
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300319 { 0x000100ffffffffeell, KEY_VOLUMEUP },
320 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
321 { 0x000008ffffffffeell, KEY_MUTE },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300322};
323
324/* to prevent races between open() and disconnect(), probing, etc */
325static DEFINE_MUTEX(driver_lock);
326
327/* Module bookkeeping bits */
328MODULE_AUTHOR(MOD_AUTHOR);
329MODULE_DESCRIPTION(MOD_DESC);
330MODULE_VERSION(MOD_VERSION);
331MODULE_LICENSE("GPL");
332MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
333
334static bool debug;
335module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300336MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300337
338/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
339static int display_type;
340module_param(display_type, int, S_IRUGO);
341MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
342 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
343
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300344static int pad_stabilize = 1;
345module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
346MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
347 "presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300348
349/*
350 * In certain use cases, mouse mode isn't really helpful, and could actually
351 * cause confusion, so allow disabling it when the IR device is open.
352 */
353static bool nomouse;
354module_param(nomouse, bool, S_IRUGO | S_IWUSR);
355MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
356 "open. 0=don't disable, 1=disable. (default: don't disable)");
357
358/* threshold at which a pad push registers as an arrow key in kbd mode */
359static int pad_thresh;
360module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
361MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
362 "arrow key in kbd mode (default: 28)");
363
364
365static void free_imon_context(struct imon_context *ictx)
366{
367 struct device *dev = ictx->dev;
368
369 usb_free_urb(ictx->tx_urb);
370 usb_free_urb(ictx->rx_urb_intf0);
371 usb_free_urb(ictx->rx_urb_intf1);
372 kfree(ictx);
373
374 dev_dbg(dev, "%s: iMON context freed\n", __func__);
375}
376
377/**
378 * Called when the Display device (e.g. /dev/lcd0)
379 * is opened by the application.
380 */
381static int display_open(struct inode *inode, struct file *file)
382{
383 struct usb_interface *interface;
384 struct imon_context *ictx = NULL;
385 int subminor;
386 int retval = 0;
387
388 /* prevent races with disconnect */
389 mutex_lock(&driver_lock);
390
391 subminor = iminor(inode);
392 interface = usb_find_interface(&imon_driver, subminor);
393 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300394 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300395 retval = -ENODEV;
396 goto exit;
397 }
398 ictx = usb_get_intfdata(interface);
399
400 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300401 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300402 retval = -ENODEV;
403 goto exit;
404 }
405
406 mutex_lock(&ictx->lock);
407
408 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300409 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300410 retval = -ENODEV;
411 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300412 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300413 retval = -EBUSY;
414 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300415 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300416 file->private_data = ictx;
417 dev_dbg(ictx->dev, "display port opened\n");
418 }
419
420 mutex_unlock(&ictx->lock);
421
422exit:
423 mutex_unlock(&driver_lock);
424 return retval;
425}
426
427/**
428 * Called when the display device (e.g. /dev/lcd0)
429 * is closed by the application.
430 */
431static int display_close(struct inode *inode, struct file *file)
432{
433 struct imon_context *ictx = NULL;
434 int retval = 0;
435
Joe Perchesabf84382010-07-12 17:50:03 -0300436 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300437
438 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300439 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300440 return -ENODEV;
441 }
442
443 mutex_lock(&ictx->lock);
444
445 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300446 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300447 retval = -ENODEV;
448 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300449 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300450 retval = -EIO;
451 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300452 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300453 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300454 }
455
456 mutex_unlock(&ictx->lock);
457 return retval;
458}
459
460/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300461 * Sends a packet to the device -- this function must be called with
462 * ictx->lock held, or its unlock/lock sequence while waiting for tx
463 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300464 */
465static int send_packet(struct imon_context *ictx)
466{
467 unsigned int pipe;
468 unsigned long timeout;
469 int interval = 0;
470 int retval = 0;
471 struct usb_ctrlrequest *control_req = NULL;
472
473 /* Check if we need to use control or interrupt urb */
474 if (!ictx->tx_control) {
475 pipe = usb_sndintpipe(ictx->usbdev_intf0,
476 ictx->tx_endpoint->bEndpointAddress);
477 interval = ictx->tx_endpoint->bInterval;
478
479 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
480 ictx->usb_tx_buf,
481 sizeof(ictx->usb_tx_buf),
482 usb_tx_callback, ictx, interval);
483
484 ictx->tx_urb->actual_length = 0;
485 } else {
486 /* fill request into kmalloc'ed space: */
487 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
488 GFP_KERNEL);
489 if (control_req == NULL)
490 return -ENOMEM;
491
492 /* setup packet is '21 09 0200 0001 0008' */
493 control_req->bRequestType = 0x21;
494 control_req->bRequest = 0x09;
495 control_req->wValue = cpu_to_le16(0x0200);
496 control_req->wIndex = cpu_to_le16(0x0001);
497 control_req->wLength = cpu_to_le16(0x0008);
498
499 /* control pipe is endpoint 0x00 */
500 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
501
502 /* build the control urb */
503 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
504 pipe, (unsigned char *)control_req,
505 ictx->usb_tx_buf,
506 sizeof(ictx->usb_tx_buf),
507 usb_tx_callback, ictx);
508 ictx->tx_urb->actual_length = 0;
509 }
510
511 init_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300512 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300513 smp_rmb(); /* ensure later readers know we're busy */
514
515 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
516 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300517 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300518 smp_rmb(); /* ensure later readers know we're not busy */
Joe Perchese2302502010-06-20 04:20:46 -0300519 pr_err("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300520 } else {
521 /* Wait for transmission to complete (or abort) */
522 mutex_unlock(&ictx->lock);
523 retval = wait_for_completion_interruptible(
524 &ictx->tx.finished);
525 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300526 pr_err("task interrupted\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300527 mutex_lock(&ictx->lock);
528
529 retval = ictx->tx.status;
530 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300531 pr_err("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300532 }
533
534 kfree(control_req);
535
536 /*
537 * Induce a mandatory 5ms delay before returning, as otherwise,
538 * send_packet can get called so rapidly as to overwhelm the device,
539 * particularly on faster systems and/or those with quirky usb.
540 */
541 timeout = msecs_to_jiffies(5);
542 set_current_state(TASK_UNINTERRUPTIBLE);
543 schedule_timeout(timeout);
544
545 return retval;
546}
547
548/**
549 * Sends an associate packet to the iMON 2.4G.
550 *
551 * This might not be such a good idea, since it has an id collision with
552 * some versions of the "IR & VFD" combo. The only way to determine if it
553 * is an RF version is to look at the product description string. (Which
554 * we currently do not fetch).
555 */
556static int send_associate_24g(struct imon_context *ictx)
557{
558 int retval;
559 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
560 0x00, 0x00, 0x00, 0x20 };
561
562 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300563 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300564 return -ENODEV;
565 }
566
567 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300568 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300569 return -ENODEV;
570 }
571
572 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
573 retval = send_packet(ictx);
574
575 return retval;
576}
577
578/**
579 * Sends packets to setup and show clock on iMON display
580 *
581 * Arguments: year - last 2 digits of year, month - 1..12,
582 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
583 * hour - 0..23, minute - 0..59, second - 0..59
584 */
585static int send_set_imon_clock(struct imon_context *ictx,
586 unsigned int year, unsigned int month,
587 unsigned int day, unsigned int dow,
588 unsigned int hour, unsigned int minute,
589 unsigned int second)
590{
591 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
592 int retval = 0;
593 int i;
594
595 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300596 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300597 return -ENODEV;
598 }
599
600 switch (ictx->display_type) {
601 case IMON_DISPLAY_TYPE_LCD:
602 clock_enable_pkt[0][0] = 0x80;
603 clock_enable_pkt[0][1] = year;
604 clock_enable_pkt[0][2] = month-1;
605 clock_enable_pkt[0][3] = day;
606 clock_enable_pkt[0][4] = hour;
607 clock_enable_pkt[0][5] = minute;
608 clock_enable_pkt[0][6] = second;
609
610 clock_enable_pkt[1][0] = 0x80;
611 clock_enable_pkt[1][1] = 0;
612 clock_enable_pkt[1][2] = 0;
613 clock_enable_pkt[1][3] = 0;
614 clock_enable_pkt[1][4] = 0;
615 clock_enable_pkt[1][5] = 0;
616 clock_enable_pkt[1][6] = 0;
617
618 if (ictx->product == 0xffdc) {
619 clock_enable_pkt[0][7] = 0x50;
620 clock_enable_pkt[1][7] = 0x51;
621 } else {
622 clock_enable_pkt[0][7] = 0x88;
623 clock_enable_pkt[1][7] = 0x8a;
624 }
625
626 break;
627
628 case IMON_DISPLAY_TYPE_VFD:
629 clock_enable_pkt[0][0] = year;
630 clock_enable_pkt[0][1] = month-1;
631 clock_enable_pkt[0][2] = day;
632 clock_enable_pkt[0][3] = dow;
633 clock_enable_pkt[0][4] = hour;
634 clock_enable_pkt[0][5] = minute;
635 clock_enable_pkt[0][6] = second;
636 clock_enable_pkt[0][7] = 0x40;
637
638 clock_enable_pkt[1][0] = 0;
639 clock_enable_pkt[1][1] = 0;
640 clock_enable_pkt[1][2] = 1;
641 clock_enable_pkt[1][3] = 0;
642 clock_enable_pkt[1][4] = 0;
643 clock_enable_pkt[1][5] = 0;
644 clock_enable_pkt[1][6] = 0;
645 clock_enable_pkt[1][7] = 0x42;
646
647 break;
648
649 default:
650 return -ENODEV;
651 }
652
653 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
654 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
655 retval = send_packet(ictx);
656 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300657 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300658 break;
659 }
660 }
661
662 return retval;
663}
664
665/**
666 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
667 */
668static ssize_t show_associate_remote(struct device *d,
669 struct device_attribute *attr,
670 char *buf)
671{
672 struct imon_context *ictx = dev_get_drvdata(d);
673
674 if (!ictx)
675 return -ENODEV;
676
677 mutex_lock(&ictx->lock);
678 if (ictx->rf_isassociating)
679 strcpy(buf, "associating\n");
680 else
681 strcpy(buf, "closed\n");
682
683 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
684 "instructions on how to associate your iMON 2.4G DT/LT "
685 "remote\n");
686 mutex_unlock(&ictx->lock);
687 return strlen(buf);
688}
689
690static ssize_t store_associate_remote(struct device *d,
691 struct device_attribute *attr,
692 const char *buf, size_t count)
693{
694 struct imon_context *ictx;
695
696 ictx = dev_get_drvdata(d);
697
698 if (!ictx)
699 return -ENODEV;
700
701 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300702 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300703 send_associate_24g(ictx);
704 mutex_unlock(&ictx->lock);
705
706 return count;
707}
708
709/**
710 * sysfs functions to control internal imon clock
711 */
712static ssize_t show_imon_clock(struct device *d,
713 struct device_attribute *attr, char *buf)
714{
715 struct imon_context *ictx = dev_get_drvdata(d);
716 size_t len;
717
718 if (!ictx)
719 return -ENODEV;
720
721 mutex_lock(&ictx->lock);
722
723 if (!ictx->display_supported) {
724 len = snprintf(buf, PAGE_SIZE, "Not supported.");
725 } else {
726 len = snprintf(buf, PAGE_SIZE,
727 "To set the clock on your iMON display:\n"
728 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
729 "%s", ictx->display_isopen ?
730 "\nNOTE: imon device must be closed\n" : "");
731 }
732
733 mutex_unlock(&ictx->lock);
734
735 return len;
736}
737
738static ssize_t store_imon_clock(struct device *d,
739 struct device_attribute *attr,
740 const char *buf, size_t count)
741{
742 struct imon_context *ictx = dev_get_drvdata(d);
743 ssize_t retval;
744 unsigned int year, month, day, dow, hour, minute, second;
745
746 if (!ictx)
747 return -ENODEV;
748
749 mutex_lock(&ictx->lock);
750
751 if (!ictx->display_supported) {
752 retval = -ENODEV;
753 goto exit;
754 } else if (ictx->display_isopen) {
755 retval = -EBUSY;
756 goto exit;
757 }
758
759 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
760 &hour, &minute, &second) != 7) {
761 retval = -EINVAL;
762 goto exit;
763 }
764
765 if ((month < 1 || month > 12) ||
766 (day < 1 || day > 31) || (dow > 6) ||
767 (hour > 23) || (minute > 59) || (second > 59)) {
768 retval = -EINVAL;
769 goto exit;
770 }
771
772 retval = send_set_imon_clock(ictx, year, month, day, dow,
773 hour, minute, second);
774 if (retval)
775 goto exit;
776
777 retval = count;
778exit:
779 mutex_unlock(&ictx->lock);
780
781 return retval;
782}
783
784
785static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
786 store_imon_clock);
787
788static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
789 store_associate_remote);
790
791static struct attribute *imon_display_sysfs_entries[] = {
792 &dev_attr_imon_clock.attr,
793 NULL
794};
795
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300796static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300797 .attrs = imon_display_sysfs_entries
798};
799
800static struct attribute *imon_rf_sysfs_entries[] = {
801 &dev_attr_associate_remote.attr,
802 NULL
803};
804
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300805static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300806 .attrs = imon_rf_sysfs_entries
807};
808
809/**
810 * Writes data to the VFD. The iMON VFD is 2x16 characters
811 * and requires data in 5 consecutive USB interrupt packets,
812 * each packet but the last carrying 7 bytes.
813 *
814 * I don't know if the VFD board supports features such as
815 * scrolling, clearing rows, blanking, etc. so at
816 * the caller must provide a full screen of data. If fewer
817 * than 32 bytes are provided spaces will be appended to
818 * generate a full screen.
819 */
820static ssize_t vfd_write(struct file *file, const char *buf,
821 size_t n_bytes, loff_t *pos)
822{
823 int i;
824 int offset;
825 int seq;
826 int retval = 0;
827 struct imon_context *ictx;
828 const unsigned char vfd_packet6[] = {
829 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
830
Joe Perchesabf84382010-07-12 17:50:03 -0300831 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300832 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300833 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300834 return -ENODEV;
835 }
836
837 mutex_lock(&ictx->lock);
838
839 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300840 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300841 retval = -ENODEV;
842 goto exit;
843 }
844
845 if (n_bytes <= 0 || n_bytes > 32) {
Joe Perchese2302502010-06-20 04:20:46 -0300846 pr_err("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300847 retval = -EINVAL;
848 goto exit;
849 }
850
851 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
852 retval = -EFAULT;
853 goto exit;
854 }
855
856 /* Pad with spaces */
857 for (i = n_bytes; i < 32; ++i)
858 ictx->tx.data_buf[i] = ' ';
859
860 for (i = 32; i < 35; ++i)
861 ictx->tx.data_buf[i] = 0xFF;
862
863 offset = 0;
864 seq = 0;
865
866 do {
867 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
868 ictx->usb_tx_buf[7] = (unsigned char) seq;
869
870 retval = send_packet(ictx);
871 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300872 pr_err("send packet failed for packet #%d\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300873 goto exit;
874 } else {
875 seq += 2;
876 offset += 7;
877 }
878
879 } while (offset < 35);
880
881 /* Send packet #6 */
882 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
883 ictx->usb_tx_buf[7] = (unsigned char) seq;
884 retval = send_packet(ictx);
885 if (retval)
Joe Perchese2302502010-06-20 04:20:46 -0300886 pr_err("send packet failed for packet #%d\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300887
888exit:
889 mutex_unlock(&ictx->lock);
890
891 return (!retval) ? n_bytes : retval;
892}
893
894/**
895 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
896 * packets. We accept data as 16 hexadecimal digits, followed by a
897 * newline (to make it easy to drive the device from a command-line
898 * -- even though the actual binary data is a bit complicated).
899 *
900 * The device itself is not a "traditional" text-mode display. It's
901 * actually a 16x96 pixel bitmap display. That means if you want to
902 * display text, you've got to have your own "font" and translate the
903 * text into bitmaps for display. This is really flexible (you can
904 * display whatever diacritics you need, and so on), but it's also
905 * a lot more complicated than most LCDs...
906 */
907static ssize_t lcd_write(struct file *file, const char *buf,
908 size_t n_bytes, loff_t *pos)
909{
910 int retval = 0;
911 struct imon_context *ictx;
912
Joe Perchesabf84382010-07-12 17:50:03 -0300913 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300914 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300915 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300916 return -ENODEV;
917 }
918
919 mutex_lock(&ictx->lock);
920
921 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300922 pr_err("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300923 retval = -ENODEV;
924 goto exit;
925 }
926
927 if (n_bytes != 8) {
Joe Perchese2302502010-06-20 04:20:46 -0300928 pr_err("invalid payload size: %d (expected 8)\n", (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300929 retval = -EINVAL;
930 goto exit;
931 }
932
933 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
934 retval = -EFAULT;
935 goto exit;
936 }
937
938 retval = send_packet(ictx);
939 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300940 pr_err("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300941 goto exit;
942 } else {
943 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
944 __func__, (int) n_bytes);
945 }
946exit:
947 mutex_unlock(&ictx->lock);
948 return (!retval) ? n_bytes : retval;
949}
950
951/**
952 * Callback function for USB core API: transmit data
953 */
954static void usb_tx_callback(struct urb *urb)
955{
956 struct imon_context *ictx;
957
958 if (!urb)
959 return;
960 ictx = (struct imon_context *)urb->context;
961 if (!ictx)
962 return;
963
964 ictx->tx.status = urb->status;
965
966 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300967 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300968 smp_rmb(); /* ensure later readers know we're not busy */
969 complete(&ictx->tx.finished);
970}
971
972/**
Jarod Wilson21677cf2010-04-16 18:29:02 -0300973 * report touchscreen input
974 */
975static void imon_touch_display_timeout(unsigned long data)
976{
977 struct imon_context *ictx = (struct imon_context *)data;
978
Dan Carpenterf03900d2010-05-04 08:37:33 -0300979 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300980 return;
981
982 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
983 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
984 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
985 input_sync(ictx->touch);
986}
987
988/**
989 * iMON IR receivers support two different signal sets -- those used by
990 * the iMON remotes, and those used by the Windows MCE remotes (which is
991 * really just RC-6), but only one or the other at a time, as the signals
992 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -0300993 *
994 * This function gets called two different ways, one way is from
995 * rc_register_device, for initial protocol selection/setup, and the other is
996 * via a userspace-initiated protocol change request, either by direct sysfs
997 * prodding or by something like ir-keytable. In the rc_register_device case,
998 * the imon context lock is already held, but when initiated from userspace,
999 * it is not, so we must acquire it prior to calling send_packet, which
1000 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001001 */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001002static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001003{
1004 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001005 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001006 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001007 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001008 unsigned char ir_proto_packet[] = {
1009 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1010
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001011 if (rc_type && !(rc_type & rc->allowed_protos))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001012 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1013 "this device does not support\n");
1014
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001015 switch (rc_type) {
1016 case RC_TYPE_RC6:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001017 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1018 ir_proto_packet[0] = 0x01;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001019 break;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001020 case RC_TYPE_UNKNOWN:
1021 case RC_TYPE_OTHER:
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001022 dev_dbg(dev, "Configuring IR receiver for iMON 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 Wilson21677cf2010-04-16 18:29:02 -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 default:
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001029 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001030 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001031 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001032 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001033 /* ir_proto_packet[0] = 0x00; // already the default */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001034 rc_type = RC_TYPE_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001035 break;
1036 }
1037
1038 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1039
Jarod Wilson23ef7102011-04-27 19:01:44 -03001040 if (!mutex_is_locked(&ictx->lock)) {
1041 unlock = true;
1042 mutex_lock(&ictx->lock);
1043 }
1044
Jarod Wilson21677cf2010-04-16 18:29:02 -03001045 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001046 if (retval)
1047 goto out;
1048
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001049 ictx->rc_type = rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001050 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001051
1052out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001053 if (unlock)
1054 mutex_unlock(&ictx->lock);
1055
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001056 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001057}
1058
1059static inline int tv2int(const struct timeval *a, const struct timeval *b)
1060{
1061 int usecs = 0;
1062 int sec = 0;
1063
1064 if (b->tv_usec > a->tv_usec) {
1065 usecs = 1000000;
1066 sec--;
1067 }
1068
1069 usecs += a->tv_usec - b->tv_usec;
1070
1071 sec += a->tv_sec - b->tv_sec;
1072 sec *= 1000;
1073 usecs /= 1000;
1074 sec += usecs;
1075
1076 if (sec < 0)
1077 sec = 1000;
1078
1079 return sec;
1080}
1081
1082/**
1083 * The directional pad behaves a bit differently, depending on whether this is
1084 * one of the older ffdc devices or a newer device. Newer devices appear to
1085 * have a higher resolution matrix for more precise mouse movement, but it
1086 * makes things overly sensitive in keyboard mode, so we do some interesting
1087 * contortions to make it less touchy. Older devices run through the same
1088 * routine with shorter timeout and a smaller threshold.
1089 */
1090static int stabilize(int a, int b, u16 timeout, u16 threshold)
1091{
1092 struct timeval ct;
1093 static struct timeval prev_time = {0, 0};
1094 static struct timeval hit_time = {0, 0};
1095 static int x, y, prev_result, hits;
1096 int result = 0;
1097 int msec, msec_hit;
1098
1099 do_gettimeofday(&ct);
1100 msec = tv2int(&ct, &prev_time);
1101 msec_hit = tv2int(&ct, &hit_time);
1102
1103 if (msec > 100) {
1104 x = 0;
1105 y = 0;
1106 hits = 0;
1107 }
1108
1109 x += a;
1110 y += b;
1111
1112 prev_time = ct;
1113
1114 if (abs(x) > threshold || abs(y) > threshold) {
1115 if (abs(y) > abs(x))
1116 result = (y > 0) ? 0x7F : 0x80;
1117 else
1118 result = (x > 0) ? 0x7F00 : 0x8000;
1119
1120 x = 0;
1121 y = 0;
1122
1123 if (result == prev_result) {
1124 hits++;
1125
1126 if (hits > 3) {
1127 switch (result) {
1128 case 0x7F:
1129 y = 17 * threshold / 30;
1130 break;
1131 case 0x80:
1132 y -= 17 * threshold / 30;
1133 break;
1134 case 0x7F00:
1135 x = 17 * threshold / 30;
1136 break;
1137 case 0x8000:
1138 x -= 17 * threshold / 30;
1139 break;
1140 }
1141 }
1142
1143 if (hits == 2 && msec_hit < timeout) {
1144 result = 0;
1145 hits = 1;
1146 }
1147 } else {
1148 prev_result = result;
1149 hits = 1;
1150 hit_time = ct;
1151 }
1152 }
1153
1154 return result;
1155}
1156
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001157static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001158{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001159 u32 keycode;
1160 u32 release;
1161 bool is_release_code = false;
1162
1163 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001164 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001165 ictx->rc_toggle = 0x0;
1166 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001167
1168 /* Look for the release of a button */
1169 if (keycode == KEY_RESERVED) {
1170 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001171 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001172 if (keycode != KEY_RESERVED)
1173 is_release_code = true;
1174 }
1175
1176 ictx->release_code = is_release_code;
1177
1178 return keycode;
1179}
1180
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001181static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001182{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001183 u32 keycode;
1184
1185#define MCE_KEY_MASK 0x7000
1186#define MCE_TOGGLE_BIT 0x8000
1187
1188 /*
1189 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1190 * (the toggle bit flipping between alternating key presses), while
1191 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1192 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1193 * but we can't or them into all codes, as some keys are decoded in
1194 * a different way w/o the same use of the toggle bit...
1195 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001196 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001197 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1198
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001199 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001200 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001201
1202 /* not used in mce mode, but make sure we know its false */
1203 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001204
1205 return keycode;
1206}
1207
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001208static u32 imon_panel_key_lookup(u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001209{
1210 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001211 u32 keycode = KEY_RESERVED;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001212
Jarod Wilson083e4722010-05-04 16:17:05 -03001213 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1214 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1215 keycode = imon_panel_key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001216 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001217 }
1218 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001219
1220 return keycode;
1221}
1222
1223static bool imon_mouse_event(struct imon_context *ictx,
1224 unsigned char *buf, int len)
1225{
1226 char rel_x = 0x00, rel_y = 0x00;
1227 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001228 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001229 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001230 unsigned long flags;
1231
1232 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001233
1234 /* newer iMON device PAD or mouse button */
1235 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1236 rel_x = buf[2];
1237 rel_y = buf[3];
1238 right_shift = 1;
1239 /* 0xffdc iMON PAD or mouse button input */
1240 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1241 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1242 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1243 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1244 if (buf[0] & 0x02)
1245 rel_x |= ~0x0f;
1246 rel_x = rel_x + rel_x / 2;
1247 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1248 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1249 if (buf[0] & 0x01)
1250 rel_y |= ~0x0f;
1251 rel_y = rel_y + rel_y / 2;
1252 right_shift = 2;
1253 /* some ffdc devices decode mouse buttons differently... */
1254 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1255 right_shift = 2;
1256 /* ch+/- buttons, which we use for an emulated scroll wheel */
1257 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1258 dir = 1;
1259 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1260 dir = -1;
1261 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001262 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001263
Jarod Wilson693508d2010-09-15 15:56:03 -03001264 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1265
Jarod Wilson21677cf2010-04-16 18:29:02 -03001266 if (mouse_input) {
1267 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1268
1269 if (dir) {
1270 input_report_rel(ictx->idev, REL_WHEEL, dir);
1271 } else if (rel_x || rel_y) {
1272 input_report_rel(ictx->idev, REL_X, rel_x);
1273 input_report_rel(ictx->idev, REL_Y, rel_y);
1274 } else {
1275 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1276 input_report_key(ictx->idev, BTN_RIGHT,
1277 buf[1] >> right_shift & 0x1);
1278 }
1279 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001280 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001281 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001282 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001283 }
1284
1285 return mouse_input;
1286}
1287
1288static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1289{
1290 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1291 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1292 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1293 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1294 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1295 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1296 input_sync(ictx->touch);
1297}
1298
1299static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1300{
1301 int dir = 0;
1302 char rel_x = 0x00, rel_y = 0x00;
1303 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001304 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001305 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001306
1307 /*
1308 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1309 * contain a position coordinate (x,y), with each component ranging
1310 * from -14 to 14. We want to down-sample this to only 4 discrete values
1311 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001312 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001313 * ignore when they get too close.
1314 */
1315 if (ictx->product != 0xffdc) {
1316 /* first, pad to 8 bytes so it conforms with everything else */
1317 buf[5] = buf[6] = buf[7] = 0;
1318 timeout = 500; /* in msecs */
1319 /* (2*threshold) x (2*threshold) square */
1320 threshold = pad_thresh ? pad_thresh : 28;
1321 rel_x = buf[2];
1322 rel_y = buf[3];
1323
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001324 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001325 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1326 dir = stabilize((int)rel_x, (int)rel_y,
1327 timeout, threshold);
1328 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001329 spin_lock_irqsave(&ictx->kc_lock,
1330 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001331 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001332 spin_unlock_irqrestore(&ictx->kc_lock,
1333 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001334 return;
1335 }
1336 buf[2] = dir & 0xFF;
1337 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001338 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001339 }
1340 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001341 /*
1342 * Hack alert: instead of using keycodes, we have
1343 * to use hard-coded scancodes here...
1344 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001345 if (abs(rel_y) > abs(rel_x)) {
1346 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1347 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001348 if (rel_y > 0)
1349 scancode = 0x01007f00; /* KEY_DOWN */
1350 else
1351 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001352 } else {
1353 buf[2] = 0;
1354 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001355 if (rel_x > 0)
1356 scancode = 0x0100007f; /* KEY_RIGHT */
1357 else
1358 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001359 }
1360 }
1361
1362 /*
1363 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1364 * device (15c2:ffdc). The remote generates various codes from
1365 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1366 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1367 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1368 * reversed endianess. Extract direction from buffer, rotate endianess,
1369 * adjust sign and feed the values into stabilize(). The resulting codes
1370 * will be 0x01008000, 0x01007F00, which match the newer devices.
1371 */
1372 } else {
1373 timeout = 10; /* in msecs */
1374 /* (2*threshold) x (2*threshold) square */
1375 threshold = pad_thresh ? pad_thresh : 15;
1376
1377 /* buf[1] is x */
1378 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1379 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1380 if (buf[0] & 0x02)
1381 rel_x |= ~0x10+1;
1382 /* buf[2] is y */
1383 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1384 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1385 if (buf[0] & 0x01)
1386 rel_y |= ~0x10+1;
1387
1388 buf[0] = 0x01;
1389 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1390
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001391 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001392 dir = stabilize((int)rel_x, (int)rel_y,
1393 timeout, threshold);
1394 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001395 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001396 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001397 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001398 return;
1399 }
1400 buf[2] = dir & 0xFF;
1401 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001402 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001403 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001404 /*
1405 * Hack alert: instead of using keycodes, we have
1406 * to use hard-coded scancodes here...
1407 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001408 if (abs(rel_y) > abs(rel_x)) {
1409 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1410 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001411 if (rel_y > 0)
1412 scancode = 0x01007f00; /* KEY_DOWN */
1413 else
1414 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001415 } else {
1416 buf[2] = 0;
1417 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001418 if (rel_x > 0)
1419 scancode = 0x0100007f; /* KEY_RIGHT */
1420 else
1421 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001422 }
1423 }
1424 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001425
Jarod Wilson693508d2010-09-15 15:56:03 -03001426 if (scancode) {
1427 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001428 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001429 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1430 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001431}
1432
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001433/**
1434 * figure out if these is a press or a release. We don't actually
1435 * care about repeats, as those will be auto-generated within the IR
1436 * subsystem for repeating scancodes.
1437 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001438static int imon_parse_press_type(struct imon_context *ictx,
1439 unsigned char *buf, u8 ktype)
1440{
1441 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001442 unsigned long flags;
1443
1444 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001445
1446 /* key release of 0x02XXXXXX key */
1447 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1448 ictx->kc = ictx->last_keycode;
1449
1450 /* mouse button release on (some) 0xffdc devices */
1451 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1452 buf[2] == 0x81 && buf[3] == 0xb7)
1453 ictx->kc = ictx->last_keycode;
1454
1455 /* mouse button release on (some other) 0xffdc devices */
1456 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1457 buf[2] == 0x81 && buf[3] == 0xb7)
1458 ictx->kc = ictx->last_keycode;
1459
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001460 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001461 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001462 ictx->rc_toggle = buf[2];
1463 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001464
1465 /* incoherent or irrelevant data */
1466 } else if (ictx->kc == KEY_RESERVED)
1467 press_type = -EINVAL;
1468
1469 /* key release of 0xXXXXXXb7 key */
1470 else if (ictx->release_code)
1471 press_type = 0;
1472
1473 /* this is a button press */
1474 else
1475 press_type = 1;
1476
Jarod Wilson693508d2010-09-15 15:56:03 -03001477 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1478
Jarod Wilson21677cf2010-04-16 18:29:02 -03001479 return press_type;
1480}
1481
1482/**
1483 * Process the incoming packet
1484 */
1485static void imon_incoming_packet(struct imon_context *ictx,
1486 struct urb *urb, int intf)
1487{
1488 int len = urb->actual_length;
1489 unsigned char *buf = urb->transfer_buffer;
1490 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001491 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001492 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001493 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001494 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001495 int press_type = 0;
1496 int msec;
1497 struct timeval t;
1498 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001499 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001500
Jarod Wilson21677cf2010-04-16 18:29:02 -03001501 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001502 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001503 return;
1504
1505 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001506 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001507 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001508 ktype = IMON_KEY_PANEL;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001509 kc = imon_panel_key_lookup(scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001510 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001511 scancode = be32_to_cpu(*((u32 *)buf));
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001512 if (ictx->rc_type == RC_TYPE_RC6) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001513 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001514 if (buf[0] == 0x80)
1515 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001516 kc = imon_mce_key_lookup(ictx, scancode);
1517 } else {
1518 ktype = IMON_KEY_IMON;
1519 kc = imon_remote_key_lookup(ictx, scancode);
1520 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001521 }
1522
Jarod Wilson693508d2010-09-15 15:56:03 -03001523 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001524 /* keyboard/mouse mode toggle button */
1525 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1526 ictx->last_keycode = kc;
1527 if (!nomouse) {
1528 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1529 dev_dbg(dev, "toggling to %s mode\n",
1530 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001531 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001532 return;
1533 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001534 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001535 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1536 }
1537 }
1538
1539 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001540 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001541
1542 /* send touchscreen events through input subsystem if touchpad data */
1543 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1544 buf[7] == 0x86) {
1545 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001546 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001547
1548 /* look for mouse events with pad in mouse mode */
1549 } else if (ictx->pad_mouse) {
1550 if (imon_mouse_event(ictx, buf, len))
1551 return;
1552 }
1553
1554 /* Now for some special handling to convert pad input to arrow keys */
1555 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1556 ((len == 8) && (buf[0] & 0x40) &&
1557 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1558 len = 8;
1559 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001560 }
1561
1562 if (debug) {
1563 printk(KERN_INFO "intf%d decoded packet: ", intf);
1564 for (i = 0; i < len; ++i)
1565 printk("%02x ", buf[i]);
1566 printk("\n");
1567 }
1568
1569 press_type = imon_parse_press_type(ictx, buf, ktype);
1570 if (press_type < 0)
1571 goto not_input_data;
1572
Jarod Wilson693508d2010-09-15 15:56:03 -03001573 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001574 if (ictx->kc == KEY_UNKNOWN)
1575 goto unknown_key;
Jarod Wilson693508d2010-09-15 15:56:03 -03001576 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001577
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001578 if (ktype != IMON_KEY_PANEL) {
1579 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001580 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001581 else {
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001582 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001583 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001584 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001585 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001586 }
1587 return;
1588 }
1589
1590 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001591 spin_lock_irqsave(&ictx->kc_lock, flags);
1592
Jarod Wilson94215cc2011-06-04 14:14:41 -03001593 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001594 /* KEY_MUTE repeats from knob need to be suppressed */
1595 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001596 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001597 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001598 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001599 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001600 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001601 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001602 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001603 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001604
Jarod Wilson693508d2010-09-15 15:56:03 -03001605 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001606
Jarod Wilson428cc762010-10-23 16:42:20 -03001607 input_report_key(ictx->idev, kc, press_type);
1608 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001609
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001610 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001611 input_report_key(ictx->idev, kc, 0);
1612 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001613
Jarod Wilson94215cc2011-06-04 14:14:41 -03001614 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001615 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001616 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001617
1618 return;
1619
1620unknown_key:
Jarod Wilson693508d2010-09-15 15:56:03 -03001621 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001622 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001623 (long long)scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001624 return;
1625
1626not_input_data:
1627 if (len != 8) {
1628 dev_warn(dev, "imon %s: invalid incoming packet "
1629 "size (len = %d, intf%d)\n", __func__, len, intf);
1630 return;
1631 }
1632
1633 /* iMON 2.4G associate frame */
1634 if (buf[0] == 0x00 &&
1635 buf[2] == 0xFF && /* REFID */
1636 buf[3] == 0xFF &&
1637 buf[4] == 0xFF &&
1638 buf[5] == 0xFF && /* iMON 2.4G */
1639 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1640 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1641 dev_warn(dev, "%s: remote associated refid=%02X\n",
1642 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001643 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001644 }
1645}
1646
1647/**
1648 * Callback function for USB core API: receive data
1649 */
1650static void usb_rx_callback_intf0(struct urb *urb)
1651{
1652 struct imon_context *ictx;
1653 int intfnum = 0;
1654
1655 if (!urb)
1656 return;
1657
1658 ictx = (struct imon_context *)urb->context;
1659 if (!ictx)
1660 return;
1661
1662 switch (urb->status) {
1663 case -ENOENT: /* usbcore unlink successful! */
1664 return;
1665
1666 case -ESHUTDOWN: /* transport endpoint was shut down */
1667 break;
1668
1669 case 0:
1670 imon_incoming_packet(ictx, urb, intfnum);
1671 break;
1672
1673 default:
1674 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1675 __func__, urb->status);
1676 break;
1677 }
1678
1679 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1680}
1681
1682static void usb_rx_callback_intf1(struct urb *urb)
1683{
1684 struct imon_context *ictx;
1685 int intfnum = 1;
1686
1687 if (!urb)
1688 return;
1689
1690 ictx = (struct imon_context *)urb->context;
1691 if (!ictx)
1692 return;
1693
1694 switch (urb->status) {
1695 case -ENOENT: /* usbcore unlink successful! */
1696 return;
1697
1698 case -ESHUTDOWN: /* transport endpoint was shut down */
1699 break;
1700
1701 case 0:
1702 imon_incoming_packet(ictx, urb, intfnum);
1703 break;
1704
1705 default:
1706 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1707 __func__, urb->status);
1708 break;
1709 }
1710
1711 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1712}
1713
Jarod Wilson04292fc2010-09-15 00:28:41 -03001714/*
1715 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1716 * devices, and all of them constantly spew interrupts, even when there
1717 * is no actual data to report. However, byte 6 of this buffer looks like
1718 * its unique across device variants, so we're trying to key off that to
1719 * figure out which display type (if any) and what IR protocol the device
1720 * actually supports. These devices have their IR protocol hard-coded into
1721 * their firmware, they can't be changed on the fly like the newer hardware.
1722 */
1723static void imon_get_ffdc_type(struct imon_context *ictx)
1724{
1725 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1726 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001727 u64 allowed_protos = RC_TYPE_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001728
1729 switch (ffdc_cfg_byte) {
1730 /* iMON Knob, no display, iMON IR + vol knob */
1731 case 0x21:
1732 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1733 ictx->display_supported = false;
1734 break;
1735 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1736 case 0x4e:
1737 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1738 ictx->display_supported = false;
1739 ictx->rf_device = true;
1740 break;
1741 /* iMON VFD, no IR (does have vol knob tho) */
1742 case 0x35:
1743 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1744 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1745 break;
1746 /* iMON VFD, iMON IR */
1747 case 0x24:
1748 case 0x85:
1749 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1750 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1751 break;
1752 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001753 case 0x46:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001754 case 0x9e:
1755 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1756 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001757 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001758 break;
1759 /* iMON LCD, MCE IR */
1760 case 0x9f:
1761 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1762 detected_display_type = IMON_DISPLAY_TYPE_LCD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001763 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001764 break;
1765 default:
1766 dev_info(ictx->dev, "Unknown 0xffdc device, "
1767 "defaulting to VFD and iMON IR");
1768 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1769 break;
1770 }
1771
1772 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1773
1774 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001775 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001776}
1777
1778static void imon_set_display_type(struct imon_context *ictx)
1779{
1780 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1781
1782 /*
1783 * Try to auto-detect the type of display if the user hasn't set
1784 * it by hand via the display_type modparam. Default is VFD.
1785 */
1786
1787 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1788 switch (ictx->product) {
1789 case 0xffdc:
1790 /* set in imon_get_ffdc_type() */
1791 configured_display_type = ictx->display_type;
1792 break;
1793 case 0x0034:
1794 case 0x0035:
1795 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1796 break;
1797 case 0x0038:
1798 case 0x0039:
1799 case 0x0045:
1800 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1801 break;
1802 case 0x003c:
1803 case 0x0041:
1804 case 0x0042:
1805 case 0x0043:
1806 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1807 ictx->display_supported = false;
1808 break;
1809 case 0x0036:
1810 case 0x0044:
1811 default:
1812 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1813 break;
1814 }
1815 } else {
1816 configured_display_type = display_type;
1817 if (display_type == IMON_DISPLAY_TYPE_NONE)
1818 ictx->display_supported = false;
1819 else
1820 ictx->display_supported = true;
1821 dev_info(ictx->dev, "%s: overriding display type to %d via "
1822 "modparam\n", __func__, display_type);
1823 }
1824
1825 ictx->display_type = configured_display_type;
1826}
1827
David Härdemand8b4b582010-10-29 16:08:23 -03001828static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001829{
David Härdemand8b4b582010-10-29 16:08:23 -03001830 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001831 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001832 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1833 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001834
David Härdemand8b4b582010-10-29 16:08:23 -03001835 rdev = rc_allocate_device();
1836 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001837 dev_err(ictx->dev, "remote control dev allocation failed\n");
1838 goto out;
1839 }
1840
1841 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1842 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1843 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1844 sizeof(ictx->phys_rdev));
1845 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1846
David Härdemand8b4b582010-10-29 16:08:23 -03001847 rdev->input_name = ictx->name_rdev;
1848 rdev->input_phys = ictx->phys_rdev;
1849 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001850 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001851
David Härdemand8b4b582010-10-29 16:08:23 -03001852 rdev->priv = ictx;
1853 rdev->driver_type = RC_DRIVER_SCANCODE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001854 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001855 rdev->change_protocol = imon_ir_change_protocol;
1856 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001857
Jarod Wilson04292fc2010-09-15 00:28:41 -03001858 /* Enable front-panel buttons and/or knobs */
1859 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1860 ret = send_packet(ictx);
1861 /* Not fatal, but warn about it */
1862 if (ret)
1863 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1864
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001865 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001866 imon_get_ffdc_type(ictx);
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001867 rdev->allowed_protos = ictx->rc_type;
1868 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001869
1870 imon_set_display_type(ictx);
1871
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001872 if (ictx->rc_type == RC_TYPE_RC6)
1873 rdev->map_name = RC_MAP_IMON_MCE;
1874 else
1875 rdev->map_name = RC_MAP_IMON_PAD;
1876
David Härdemand8b4b582010-10-29 16:08:23 -03001877 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001878 if (ret < 0) {
1879 dev_err(ictx->dev, "remote input dev register failed\n");
1880 goto out;
1881 }
1882
1883 return rdev;
1884
1885out:
David Härdemand8b4b582010-10-29 16:08:23 -03001886 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001887 return NULL;
1888}
1889
Jarod Wilson21677cf2010-04-16 18:29:02 -03001890static struct input_dev *imon_init_idev(struct imon_context *ictx)
1891{
1892 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001893 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001894
1895 idev = input_allocate_device();
1896 if (!idev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001897 dev_err(ictx->dev, "input dev allocation failed\n");
1898 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001899 }
1900
Jarod Wilson21677cf2010-04-16 18:29:02 -03001901 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001902 "iMON Panel, Knob and Mouse(%04x:%04x)",
1903 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001904 idev->name = ictx->name_idev;
1905
1906 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1907 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001908 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001909 idev->phys = ictx->phys_idev;
1910
Jarod Wilsondb190fc2010-04-30 16:06:12 -03001911 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001912
1913 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1914 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1915 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1916 BIT_MASK(REL_WHEEL);
1917
1918 /* panel and/or knob code support */
1919 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1920 u32 kc = imon_panel_key_table[i].keycode;
1921 __set_bit(kc, idev->keybit);
1922 }
1923
Jarod Wilson21677cf2010-04-16 18:29:02 -03001924 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1925 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001926 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001927
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001928 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001929 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001930 dev_err(ictx->dev, "input dev register failed\n");
1931 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001932 }
1933
1934 return idev;
1935
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001936out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001937 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001938 return NULL;
1939}
1940
1941static struct input_dev *imon_init_touch(struct imon_context *ictx)
1942{
1943 struct input_dev *touch;
1944 int ret;
1945
1946 touch = input_allocate_device();
1947 if (!touch) {
1948 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1949 goto touch_alloc_failed;
1950 }
1951
1952 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1953 "iMON USB Touchscreen (%04x:%04x)",
1954 ictx->vendor, ictx->product);
1955 touch->name = ictx->name_touch;
1956
1957 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1958 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001959 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001960 touch->phys = ictx->phys_touch;
1961
1962 touch->evbit[0] =
1963 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1964 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1965 BIT_MASK(BTN_TOUCH);
1966 input_set_abs_params(touch, ABS_X,
1967 0x00, 0xfff, 0, 0);
1968 input_set_abs_params(touch, ABS_Y,
1969 0x00, 0xfff, 0, 0);
1970
1971 input_set_drvdata(touch, ictx);
1972
1973 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1974 touch->dev.parent = ictx->dev;
1975 ret = input_register_device(touch);
1976 if (ret < 0) {
1977 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1978 goto touch_register_failed;
1979 }
1980
1981 return touch;
1982
1983touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03001984 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001985
1986touch_alloc_failed:
1987 return NULL;
1988}
1989
1990static bool imon_find_endpoints(struct imon_context *ictx,
1991 struct usb_host_interface *iface_desc)
1992{
1993 struct usb_endpoint_descriptor *ep;
1994 struct usb_endpoint_descriptor *rx_endpoint = NULL;
1995 struct usb_endpoint_descriptor *tx_endpoint = NULL;
1996 int ifnum = iface_desc->desc.bInterfaceNumber;
1997 int num_endpts = iface_desc->desc.bNumEndpoints;
1998 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001999 bool ir_ep_found = false;
2000 bool display_ep_found = false;
2001 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002002
2003 /*
2004 * Scan the endpoint list and set:
2005 * first input endpoint = IR endpoint
2006 * first output endpoint = display endpoint
2007 */
2008 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2009 ep = &iface_desc->endpoint[i].desc;
2010 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2011 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2012
2013 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2014 ep_type == USB_ENDPOINT_XFER_INT) {
2015
2016 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002017 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002018 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2019
2020 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2021 ep_type == USB_ENDPOINT_XFER_INT) {
2022 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002023 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002024 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2025 }
2026 }
2027
2028 if (ifnum == 0) {
2029 ictx->rx_endpoint_intf0 = rx_endpoint;
2030 /*
2031 * tx is used to send characters to lcd/vfd, associate RF
2032 * remotes, set IR protocol, and maybe more...
2033 */
2034 ictx->tx_endpoint = tx_endpoint;
2035 } else {
2036 ictx->rx_endpoint_intf1 = rx_endpoint;
2037 }
2038
2039 /*
2040 * If we didn't find a display endpoint, this is probably one of the
2041 * newer iMON devices that use control urb instead of interrupt
2042 */
2043 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002044 tx_control = true;
2045 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002046 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2047 "interface OUT endpoint\n", __func__);
2048 }
2049
2050 /*
2051 * Some iMON receivers have no display. Unfortunately, it seems
2052 * that SoundGraph recycles device IDs between devices both with
2053 * and without... :\
2054 */
2055 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002056 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002057 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2058 }
2059
2060 /*
2061 * iMON Touch devices have a VGA touchscreen, but no "display", as
2062 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2063 */
2064 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002065 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002066 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2067 }
2068
2069 /* Input endpoint is mandatory */
2070 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002071 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002072
2073 ictx->tx_control = tx_control;
2074
2075 if (display_ep_found)
2076 ictx->display_supported = true;
2077
2078 return ir_ep_found;
2079
2080}
2081
2082static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2083{
2084 struct imon_context *ictx;
2085 struct urb *rx_urb;
2086 struct urb *tx_urb;
2087 struct device *dev = &intf->dev;
2088 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002089 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002090
2091 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2092 if (!ictx) {
2093 dev_err(dev, "%s: kzalloc failed for context", __func__);
2094 goto exit;
2095 }
2096 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2097 if (!rx_urb) {
2098 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2099 goto rx_urb_alloc_failed;
2100 }
2101 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2102 if (!tx_urb) {
2103 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2104 __func__);
2105 goto tx_urb_alloc_failed;
2106 }
2107
2108 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002109 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002110
2111 mutex_lock(&ictx->lock);
2112
2113 ictx->dev = dev;
2114 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002115 ictx->dev_present_intf0 = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002116 ictx->rx_urb_intf0 = rx_urb;
2117 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002118 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002119
2120 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2121 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2122
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002123 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002124 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002125 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002126 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002127 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002128
Jarod Wilson21677cf2010-04-16 18:29:02 -03002129 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2130 usb_rcvintpipe(ictx->usbdev_intf0,
2131 ictx->rx_endpoint_intf0->bEndpointAddress),
2132 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2133 usb_rx_callback_intf0, ictx,
2134 ictx->rx_endpoint_intf0->bInterval);
2135
2136 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2137 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002138 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002139 goto urb_submit_failed;
2140 }
2141
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002142 ictx->idev = imon_init_idev(ictx);
2143 if (!ictx->idev) {
2144 dev_err(dev, "%s: input device setup failed\n", __func__);
2145 goto idev_setup_failed;
2146 }
2147
2148 ictx->rdev = imon_init_rdev(ictx);
2149 if (!ictx->rdev) {
2150 dev_err(dev, "%s: rc device setup failed\n", __func__);
2151 goto rdev_setup_failed;
2152 }
2153
Jarod Wilson23ef7102011-04-27 19:01:44 -03002154 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002155 return ictx;
2156
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002157rdev_setup_failed:
2158 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002159idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002160 usb_kill_urb(ictx->rx_urb_intf0);
2161urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002162find_endpoint_failed:
2163 mutex_unlock(&ictx->lock);
2164 usb_free_urb(tx_urb);
2165tx_urb_alloc_failed:
2166 usb_free_urb(rx_urb);
2167rx_urb_alloc_failed:
2168 kfree(ictx);
2169exit:
2170 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2171
2172 return NULL;
2173}
2174
2175static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2176 struct imon_context *ictx)
2177{
2178 struct urb *rx_urb;
2179 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002180 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002181
2182 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2183 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002184 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002185 goto rx_urb_alloc_failed;
2186 }
2187
2188 mutex_lock(&ictx->lock);
2189
2190 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2191 init_timer(&ictx->ttimer);
2192 ictx->ttimer.data = (unsigned long)ictx;
2193 ictx->ttimer.function = imon_touch_display_timeout;
2194 }
2195
2196 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002197 ictx->dev_present_intf1 = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002198 ictx->rx_urb_intf1 = rx_urb;
2199
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002200 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002201 iface_desc = intf->cur_altsetting;
2202 if (!imon_find_endpoints(ictx, iface_desc))
2203 goto find_endpoint_failed;
2204
2205 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2206 ictx->touch = imon_init_touch(ictx);
2207 if (!ictx->touch)
2208 goto touch_setup_failed;
2209 } else
2210 ictx->touch = NULL;
2211
2212 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2213 usb_rcvintpipe(ictx->usbdev_intf1,
2214 ictx->rx_endpoint_intf1->bEndpointAddress),
2215 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2216 usb_rx_callback_intf1, ictx,
2217 ictx->rx_endpoint_intf1->bInterval);
2218
2219 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2220
2221 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002222 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002223 goto urb_submit_failed;
2224 }
2225
Jarod Wilson23ef7102011-04-27 19:01:44 -03002226 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002227 return ictx;
2228
2229urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002230 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002231 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002232touch_setup_failed:
2233find_endpoint_failed:
2234 mutex_unlock(&ictx->lock);
2235 usb_free_urb(rx_urb);
2236rx_urb_alloc_failed:
2237 dev_err(ictx->dev, "unable to initialize intf0, err %d\n", ret);
2238
2239 return NULL;
2240}
2241
Jarod Wilson21677cf2010-04-16 18:29:02 -03002242static void imon_init_display(struct imon_context *ictx,
2243 struct usb_interface *intf)
2244{
2245 int ret;
2246
2247 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2248
2249 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002250 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002251 if (ret)
2252 dev_err(ictx->dev, "Could not create display sysfs "
2253 "entries(%d)", ret);
2254
2255 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2256 ret = usb_register_dev(intf, &imon_lcd_class);
2257 else
2258 ret = usb_register_dev(intf, &imon_vfd_class);
2259 if (ret)
2260 /* Not a fatal error, so ignore */
2261 dev_info(ictx->dev, "could not get a minor number for "
2262 "display\n");
2263
2264}
2265
2266/**
2267 * Callback function for USB core API: Probe
2268 */
2269static int __devinit imon_probe(struct usb_interface *interface,
2270 const struct usb_device_id *id)
2271{
2272 struct usb_device *usbdev = NULL;
2273 struct usb_host_interface *iface_desc = NULL;
2274 struct usb_interface *first_if;
2275 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002276 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002277 int ret = 0;
2278 struct imon_context *ictx = NULL;
2279 struct imon_context *first_if_ctx = NULL;
2280 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002281
Jarod Wilson21677cf2010-04-16 18:29:02 -03002282 usbdev = usb_get_dev(interface_to_usbdev(interface));
2283 iface_desc = interface->cur_altsetting;
2284 ifnum = iface_desc->desc.bInterfaceNumber;
2285 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2286 product = le16_to_cpu(usbdev->descriptor.idProduct);
2287
2288 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2289 __func__, vendor, product, ifnum);
2290
2291 /* prevent races probing devices w/multiple interfaces */
2292 mutex_lock(&driver_lock);
2293
2294 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002295 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002296
2297 if (ifnum == 0) {
2298 ictx = imon_init_intf0(interface);
2299 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002300 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002301 ret = -ENODEV;
2302 goto fail;
2303 }
2304
Jarod Wilson21677cf2010-04-16 18:29:02 -03002305 } else {
2306 /* this is the secondary interface on the device */
2307 ictx = imon_init_intf1(interface, first_if_ctx);
2308 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002309 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002310 ret = -ENODEV;
2311 goto fail;
2312 }
2313
2314 }
2315
2316 usb_set_intfdata(interface, ictx);
2317
2318 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002319 mutex_lock(&ictx->lock);
2320
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002321 if (product == 0xffdc && ictx->rf_device) {
2322 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002323 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002324 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002325 pr_err("Could not create RF sysfs entries(%d)\n",
2326 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002327 }
2328
Jarod Wilson21677cf2010-04-16 18:29:02 -03002329 if (ictx->display_supported)
2330 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002331
2332 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002333 }
2334
Jarod Wilson21677cf2010-04-16 18:29:02 -03002335 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2336 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2337 usbdev->bus->busnum, usbdev->devnum);
2338
Jarod Wilson21677cf2010-04-16 18:29:02 -03002339 mutex_unlock(&driver_lock);
2340
2341 return 0;
2342
2343fail:
2344 mutex_unlock(&driver_lock);
2345 dev_err(dev, "unable to register, err %d\n", ret);
2346
2347 return ret;
2348}
2349
2350/**
2351 * Callback function for USB core API: disconnect
2352 */
2353static void __devexit imon_disconnect(struct usb_interface *interface)
2354{
2355 struct imon_context *ictx;
2356 struct device *dev;
2357 int ifnum;
2358
2359 /* prevent races with multi-interface device probing and display_open */
2360 mutex_lock(&driver_lock);
2361
2362 ictx = usb_get_intfdata(interface);
2363 dev = ictx->dev;
2364 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2365
Jarod Wilson21677cf2010-04-16 18:29:02 -03002366 /*
2367 * sysfs_remove_group is safe to call even if sysfs_create_group
2368 * hasn't been called
2369 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002370 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2371 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002372
2373 usb_set_intfdata(interface, NULL);
2374
2375 /* Abort ongoing write */
2376 if (ictx->tx.busy) {
2377 usb_kill_urb(ictx->tx_urb);
2378 complete_all(&ictx->tx.finished);
2379 }
2380
2381 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002382 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002383 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002384 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002385 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002386 if (ictx->display_supported) {
2387 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2388 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002389 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002390 usb_deregister_dev(interface, &imon_vfd_class);
2391 }
2392 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002393 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002394 usb_kill_urb(ictx->rx_urb_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002395 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002396 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002397 del_timer_sync(&ictx->ttimer);
2398 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002399 }
2400
Jarod Wilson76a2d212011-04-28 18:20:58 -03002401 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2402 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002403
2404 mutex_unlock(&driver_lock);
2405
2406 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2407 __func__, ifnum);
2408}
2409
2410static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2411{
2412 struct imon_context *ictx = usb_get_intfdata(intf);
2413 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2414
2415 if (ifnum == 0)
2416 usb_kill_urb(ictx->rx_urb_intf0);
2417 else
2418 usb_kill_urb(ictx->rx_urb_intf1);
2419
2420 return 0;
2421}
2422
2423static int imon_resume(struct usb_interface *intf)
2424{
2425 int rc = 0;
2426 struct imon_context *ictx = usb_get_intfdata(intf);
2427 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2428
2429 if (ifnum == 0) {
2430 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2431 usb_rcvintpipe(ictx->usbdev_intf0,
2432 ictx->rx_endpoint_intf0->bEndpointAddress),
2433 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2434 usb_rx_callback_intf0, ictx,
2435 ictx->rx_endpoint_intf0->bInterval);
2436
2437 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2438
2439 } else {
2440 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2441 usb_rcvintpipe(ictx->usbdev_intf1,
2442 ictx->rx_endpoint_intf1->bEndpointAddress),
2443 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2444 usb_rx_callback_intf1, ictx,
2445 ictx->rx_endpoint_intf1->bInterval);
2446
2447 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2448 }
2449
2450 return rc;
2451}
2452
2453static int __init imon_init(void)
2454{
2455 int rc;
2456
2457 rc = usb_register(&imon_driver);
2458 if (rc) {
Joe Perchese2302502010-06-20 04:20:46 -03002459 pr_err("usb register failed(%d)\n", rc);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002460 rc = -ENODEV;
2461 }
2462
2463 return rc;
2464}
2465
2466static void __exit imon_exit(void)
2467{
2468 usb_deregister(&imon_driver);
2469}
2470
2471module_init(imon_init);
2472module_exit(imon_exit);