blob: 7f26fdf2e54e4a9498264f7b92155fb146887d9c [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>
Jarod Wilson47a09b02011-07-14 14:20:46 -030037#include <linux/ratelimit.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030038
39#include <linux/input.h>
40#include <linux/usb.h>
41#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030042#include <media/rc-core.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030043
44#include <linux/time.h>
45#include <linux/timer.h>
46
47#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
48#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
49#define MOD_NAME "imon"
Jarod Wilson8791d632012-01-26 12:04:11 -030050#define MOD_VERSION "0.9.4"
Jarod Wilson21677cf2010-04-16 18:29:02 -030051
52#define DISPLAY_MINOR_BASE 144
53#define DEVICE_NAME "lcd%d"
54
55#define BUF_CHUNK_SIZE 8
56#define BUF_SIZE 128
57
58#define BIT_DURATION 250 /* each bit received is 250us */
59
60#define IMON_CLOCK_ENABLE_PACKETS 2
Jarod Wilson21677cf2010-04-16 18:29:02 -030061
62/*** P R O T O T Y P E S ***/
63
64/* USB Callback prototypes */
65static int imon_probe(struct usb_interface *interface,
66 const struct usb_device_id *id);
67static void imon_disconnect(struct usb_interface *interface);
68static void usb_rx_callback_intf0(struct urb *urb);
69static void usb_rx_callback_intf1(struct urb *urb);
70static void usb_tx_callback(struct urb *urb);
71
72/* suspend/resume support */
73static int imon_resume(struct usb_interface *intf);
74static int imon_suspend(struct usb_interface *intf, pm_message_t message);
75
76/* Display file_operations function prototypes */
77static int display_open(struct inode *inode, struct file *file);
78static int display_close(struct inode *inode, struct file *file);
79
80/* VFD write operation */
81static ssize_t vfd_write(struct file *file, const char *buf,
82 size_t n_bytes, loff_t *pos);
83
84/* LCD file_operations override function prototypes */
85static ssize_t lcd_write(struct file *file, const char *buf,
86 size_t n_bytes, loff_t *pos);
87
88/*** G L O B A L S ***/
89
90struct imon_context {
91 struct device *dev;
Jarod Wilson21677cf2010-04-16 18:29:02 -030092 /* Newer devices have two interfaces */
93 struct usb_device *usbdev_intf0;
94 struct usb_device *usbdev_intf1;
95
96 bool display_supported; /* not all controllers do */
97 bool display_isopen; /* display port has been opened */
Jarod Wilsonbbe46902010-05-24 12:02:05 -030098 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
Jarod Wilson21677cf2010-04-16 18:29:02 -030099 bool rf_isassociating; /* RF remote associating */
100 bool dev_present_intf0; /* USB device presence, interface 0 */
101 bool dev_present_intf1; /* USB device presence, interface 1 */
102
103 struct mutex lock; /* to lock this object */
104 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
105
106 struct usb_endpoint_descriptor *rx_endpoint_intf0;
107 struct usb_endpoint_descriptor *rx_endpoint_intf1;
108 struct usb_endpoint_descriptor *tx_endpoint;
109 struct urb *rx_urb_intf0;
110 struct urb *rx_urb_intf1;
111 struct urb *tx_urb;
112 bool tx_control;
113 unsigned char usb_rx_buf[8];
114 unsigned char usb_tx_buf[8];
115
116 struct tx_t {
117 unsigned char data_buf[35]; /* user data buffer */
118 struct completion finished; /* wait for write to finish */
119 bool busy; /* write in progress */
120 int status; /* status of tx completion */
121 } tx;
122
123 u16 vendor; /* usb vendor ID */
124 u16 product; /* usb product ID */
125
David Härdemand8b4b582010-10-29 16:08:23 -0300126 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300127 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300128 struct input_dev *touch; /* input device for touchscreen */
129
Jarod Wilson693508d2010-09-15 15:56:03 -0300130 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300131 u32 kc; /* current input keycode */
132 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300133 u32 rc_scancode; /* the computed remote scancode */
134 u8 rc_toggle; /* the computed remote toggle bit */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300135 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300136 bool release_code; /* some keys send a release code */
137
138 u8 display_type; /* store the display type */
139 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
140
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300141 char name_rdev[128]; /* rc input device name */
142 char phys_rdev[64]; /* rc input device phys path */
143
Jarod Wilson21677cf2010-04-16 18:29:02 -0300144 char name_idev[128]; /* input device name */
145 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300146
147 char name_touch[128]; /* touch screen name */
148 char phys_touch[64]; /* touch screen phys path */
149 struct timer_list ttimer; /* touch screen timer */
150 int touch_x; /* x coordinate on touchscreen */
151 int touch_y; /* y coordinate on touchscreen */
152};
153
154#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300155
156/* vfd character device file operations */
157static const struct file_operations vfd_fops = {
158 .owner = THIS_MODULE,
159 .open = &display_open,
160 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200161 .release = &display_close,
162 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300163};
164
165/* lcd character device file operations */
166static const struct file_operations lcd_fops = {
167 .owner = THIS_MODULE,
168 .open = &display_open,
169 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200170 .release = &display_close,
171 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300172};
173
174enum {
175 IMON_DISPLAY_TYPE_AUTO = 0,
176 IMON_DISPLAY_TYPE_VFD = 1,
177 IMON_DISPLAY_TYPE_LCD = 2,
178 IMON_DISPLAY_TYPE_VGA = 3,
179 IMON_DISPLAY_TYPE_NONE = 4,
180};
181
182enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300183 IMON_KEY_IMON = 0,
184 IMON_KEY_MCE = 1,
185 IMON_KEY_PANEL = 2,
186};
187
188/*
189 * USB Device ID for iMON USB Control Boards
190 *
191 * The Windows drivers contain 6 different inf files, more or less one for
192 * each new device until the 0x0034-0x0046 devices, which all use the same
193 * driver. Some of the devices in the 34-46 range haven't been definitively
194 * identified yet. Early devices have either a TriGem Computer, Inc. or a
195 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
196 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
197 * the ffdc and later devices, which do onboard decoding.
198 */
199static struct usb_device_id imon_usb_id_table[] = {
200 /*
201 * Several devices with this same device ID, all use iMON_PAD.inf
202 * SoundGraph iMON PAD (IR & VFD)
203 * SoundGraph iMON PAD (IR & LCD)
204 * SoundGraph iMON Knob (IR only)
205 */
206 { USB_DEVICE(0x15c2, 0xffdc) },
207
208 /*
209 * Newer devices, all driven by the latest iMON Windows driver, full
210 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
211 * Need user input to fill in details on unknown devices.
212 */
213 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
214 { USB_DEVICE(0x15c2, 0x0034) },
215 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
216 { USB_DEVICE(0x15c2, 0x0035) },
217 /* SoundGraph iMON OEM VFD (IR & VFD) */
218 { USB_DEVICE(0x15c2, 0x0036) },
219 /* device specifics unknown */
220 { USB_DEVICE(0x15c2, 0x0037) },
221 /* SoundGraph iMON OEM LCD (IR & LCD) */
222 { USB_DEVICE(0x15c2, 0x0038) },
223 /* SoundGraph iMON UltraBay (IR & LCD) */
224 { USB_DEVICE(0x15c2, 0x0039) },
225 /* device specifics unknown */
226 { USB_DEVICE(0x15c2, 0x003a) },
227 /* device specifics unknown */
228 { USB_DEVICE(0x15c2, 0x003b) },
229 /* SoundGraph iMON OEM Inside (IR only) */
230 { USB_DEVICE(0x15c2, 0x003c) },
231 /* device specifics unknown */
232 { USB_DEVICE(0x15c2, 0x003d) },
233 /* device specifics unknown */
234 { USB_DEVICE(0x15c2, 0x003e) },
235 /* device specifics unknown */
236 { USB_DEVICE(0x15c2, 0x003f) },
237 /* device specifics unknown */
238 { USB_DEVICE(0x15c2, 0x0040) },
239 /* SoundGraph iMON MINI (IR only) */
240 { USB_DEVICE(0x15c2, 0x0041) },
241 /* Antec Veris Multimedia Station EZ External (IR only) */
242 { USB_DEVICE(0x15c2, 0x0042) },
243 /* Antec Veris Multimedia Station Basic Internal (IR only) */
244 { USB_DEVICE(0x15c2, 0x0043) },
245 /* Antec Veris Multimedia Station Elite (IR & VFD) */
246 { USB_DEVICE(0x15c2, 0x0044) },
247 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
248 { USB_DEVICE(0x15c2, 0x0045) },
249 /* device specifics unknown */
250 { USB_DEVICE(0x15c2, 0x0046) },
251 {}
252};
253
254/* USB Device data */
255static struct usb_driver imon_driver = {
256 .name = MOD_NAME,
257 .probe = imon_probe,
258 .disconnect = imon_disconnect,
259 .suspend = imon_suspend,
260 .resume = imon_resume,
261 .id_table = imon_usb_id_table,
262};
263
264static struct usb_class_driver imon_vfd_class = {
265 .name = DEVICE_NAME,
266 .fops = &vfd_fops,
267 .minor_base = DISPLAY_MINOR_BASE,
268};
269
270static struct usb_class_driver imon_lcd_class = {
271 .name = DEVICE_NAME,
272 .fops = &lcd_fops,
273 .minor_base = DISPLAY_MINOR_BASE,
274};
275
276/* imon receiver front panel/knob key table */
277static const struct {
278 u64 hw_code;
279 u32 keycode;
280} imon_panel_key_table[] = {
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300281 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
282 { 0x000000001200ffeell, KEY_UP },
283 { 0x000000001300ffeell, KEY_DOWN },
284 { 0x000000001400ffeell, KEY_LEFT },
285 { 0x000000001500ffeell, KEY_RIGHT },
286 { 0x000000001600ffeell, KEY_ENTER },
287 { 0x000000001700ffeell, KEY_ESC },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300288 { 0x000000001f00ffeell, KEY_AUDIO },
289 { 0x000000002000ffeell, KEY_VIDEO },
290 { 0x000000002100ffeell, KEY_CAMERA },
291 { 0x000000002700ffeell, KEY_DVD },
292 { 0x000000002300ffeell, KEY_TV },
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300293 { 0x000000002b00ffeell, KEY_EXIT },
294 { 0x000000002c00ffeell, KEY_SELECT },
295 { 0x000000002d00ffeell, KEY_MENU },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300296 { 0x000000000500ffeell, KEY_PREVIOUS },
297 { 0x000000000700ffeell, KEY_REWIND },
298 { 0x000000000400ffeell, KEY_STOP },
299 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
300 { 0x000000000800ffeell, KEY_FASTFORWARD },
301 { 0x000000000600ffeell, KEY_NEXT },
302 { 0x000000010000ffeell, KEY_RIGHT },
303 { 0x000001000000ffeell, KEY_LEFT },
304 { 0x000000003d00ffeell, KEY_SELECT },
305 { 0x000100000000ffeell, KEY_VOLUMEUP },
306 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
307 { 0x000000000100ffeell, KEY_MUTE },
Jarod Wilson04292fc2010-09-15 00:28:41 -0300308 /* 0xffdc iMON MCE VFD */
309 { 0x00010000ffffffeell, KEY_VOLUMEUP },
310 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
Jarod Wilson443b3912011-06-04 14:00:54 -0300311 { 0x00000001ffffffeell, KEY_MUTE },
312 { 0x0000000fffffffeell, KEY_MEDIA },
313 { 0x00000012ffffffeell, KEY_UP },
314 { 0x00000013ffffffeell, KEY_DOWN },
315 { 0x00000014ffffffeell, KEY_LEFT },
316 { 0x00000015ffffffeell, KEY_RIGHT },
317 { 0x00000016ffffffeell, KEY_ENTER },
318 { 0x00000017ffffffeell, KEY_ESC },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300319 /* iMON Knob values */
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300320 { 0x000100ffffffffeell, KEY_VOLUMEUP },
321 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
322 { 0x000008ffffffffeell, KEY_MUTE },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300323};
324
325/* to prevent races between open() and disconnect(), probing, etc */
326static DEFINE_MUTEX(driver_lock);
327
328/* Module bookkeeping bits */
329MODULE_AUTHOR(MOD_AUTHOR);
330MODULE_DESCRIPTION(MOD_DESC);
331MODULE_VERSION(MOD_VERSION);
332MODULE_LICENSE("GPL");
333MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
334
335static bool debug;
336module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300337MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300338
339/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
340static int display_type;
341module_param(display_type, int, S_IRUGO);
342MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
343 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
344
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300345static int pad_stabilize = 1;
346module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
347MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
348 "presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300349
350/*
351 * In certain use cases, mouse mode isn't really helpful, and could actually
352 * cause confusion, so allow disabling it when the IR device is open.
353 */
354static bool nomouse;
355module_param(nomouse, bool, S_IRUGO | S_IWUSR);
356MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
357 "open. 0=don't disable, 1=disable. (default: don't disable)");
358
359/* threshold at which a pad push registers as an arrow key in kbd mode */
360static int pad_thresh;
361module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
362MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
363 "arrow key in kbd mode (default: 28)");
364
365
366static void free_imon_context(struct imon_context *ictx)
367{
368 struct device *dev = ictx->dev;
369
370 usb_free_urb(ictx->tx_urb);
371 usb_free_urb(ictx->rx_urb_intf0);
372 usb_free_urb(ictx->rx_urb_intf1);
373 kfree(ictx);
374
375 dev_dbg(dev, "%s: iMON context freed\n", __func__);
376}
377
378/**
379 * Called when the Display device (e.g. /dev/lcd0)
380 * is opened by the application.
381 */
382static int display_open(struct inode *inode, struct file *file)
383{
384 struct usb_interface *interface;
385 struct imon_context *ictx = NULL;
386 int subminor;
387 int retval = 0;
388
389 /* prevent races with disconnect */
390 mutex_lock(&driver_lock);
391
392 subminor = iminor(inode);
393 interface = usb_find_interface(&imon_driver, subminor);
394 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300395 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300396 retval = -ENODEV;
397 goto exit;
398 }
399 ictx = usb_get_intfdata(interface);
400
401 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300402 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300403 retval = -ENODEV;
404 goto exit;
405 }
406
407 mutex_lock(&ictx->lock);
408
409 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300410 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300411 retval = -ENODEV;
412 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300413 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300414 retval = -EBUSY;
415 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300416 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300417 file->private_data = ictx;
418 dev_dbg(ictx->dev, "display port opened\n");
419 }
420
421 mutex_unlock(&ictx->lock);
422
423exit:
424 mutex_unlock(&driver_lock);
425 return retval;
426}
427
428/**
429 * Called when the display device (e.g. /dev/lcd0)
430 * is closed by the application.
431 */
432static int display_close(struct inode *inode, struct file *file)
433{
434 struct imon_context *ictx = NULL;
435 int retval = 0;
436
Joe Perchesabf84382010-07-12 17:50:03 -0300437 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300438
439 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300440 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300441 return -ENODEV;
442 }
443
444 mutex_lock(&ictx->lock);
445
446 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300447 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300448 retval = -ENODEV;
449 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300450 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300451 retval = -EIO;
452 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300453 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300454 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300455 }
456
457 mutex_unlock(&ictx->lock);
458 return retval;
459}
460
461/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300462 * Sends a packet to the device -- this function must be called with
463 * ictx->lock held, or its unlock/lock sequence while waiting for tx
464 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300465 */
466static int send_packet(struct imon_context *ictx)
467{
468 unsigned int pipe;
469 unsigned long timeout;
470 int interval = 0;
471 int retval = 0;
472 struct usb_ctrlrequest *control_req = NULL;
473
474 /* Check if we need to use control or interrupt urb */
475 if (!ictx->tx_control) {
476 pipe = usb_sndintpipe(ictx->usbdev_intf0,
477 ictx->tx_endpoint->bEndpointAddress);
478 interval = ictx->tx_endpoint->bInterval;
479
480 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
481 ictx->usb_tx_buf,
482 sizeof(ictx->usb_tx_buf),
483 usb_tx_callback, ictx, interval);
484
485 ictx->tx_urb->actual_length = 0;
486 } else {
487 /* fill request into kmalloc'ed space: */
488 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
489 GFP_KERNEL);
490 if (control_req == NULL)
491 return -ENOMEM;
492
493 /* setup packet is '21 09 0200 0001 0008' */
494 control_req->bRequestType = 0x21;
495 control_req->bRequest = 0x09;
496 control_req->wValue = cpu_to_le16(0x0200);
497 control_req->wIndex = cpu_to_le16(0x0001);
498 control_req->wLength = cpu_to_le16(0x0008);
499
500 /* control pipe is endpoint 0x00 */
501 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
502
503 /* build the control urb */
504 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
505 pipe, (unsigned char *)control_req,
506 ictx->usb_tx_buf,
507 sizeof(ictx->usb_tx_buf),
508 usb_tx_callback, ictx);
509 ictx->tx_urb->actual_length = 0;
510 }
511
512 init_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300513 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300514 smp_rmb(); /* ensure later readers know we're busy */
515
516 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
517 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300518 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300519 smp_rmb(); /* ensure later readers know we're not busy */
Jarod Wilson47a09b02011-07-14 14:20:46 -0300520 pr_err_ratelimited("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300521 } else {
522 /* Wait for transmission to complete (or abort) */
523 mutex_unlock(&ictx->lock);
524 retval = wait_for_completion_interruptible(
525 &ictx->tx.finished);
526 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300527 pr_err_ratelimited("task interrupted\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300528 mutex_lock(&ictx->lock);
529
530 retval = ictx->tx.status;
531 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300532 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300533 }
534
535 kfree(control_req);
536
537 /*
538 * Induce a mandatory 5ms delay before returning, as otherwise,
539 * send_packet can get called so rapidly as to overwhelm the device,
540 * particularly on faster systems and/or those with quirky usb.
541 */
542 timeout = msecs_to_jiffies(5);
543 set_current_state(TASK_UNINTERRUPTIBLE);
544 schedule_timeout(timeout);
545
546 return retval;
547}
548
549/**
550 * Sends an associate packet to the iMON 2.4G.
551 *
552 * This might not be such a good idea, since it has an id collision with
553 * some versions of the "IR & VFD" combo. The only way to determine if it
554 * is an RF version is to look at the product description string. (Which
555 * we currently do not fetch).
556 */
557static int send_associate_24g(struct imon_context *ictx)
558{
559 int retval;
560 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
561 0x00, 0x00, 0x00, 0x20 };
562
563 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300564 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300565 return -ENODEV;
566 }
567
568 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300569 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300570 return -ENODEV;
571 }
572
573 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
574 retval = send_packet(ictx);
575
576 return retval;
577}
578
579/**
580 * Sends packets to setup and show clock on iMON display
581 *
582 * Arguments: year - last 2 digits of year, month - 1..12,
583 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
584 * hour - 0..23, minute - 0..59, second - 0..59
585 */
586static int send_set_imon_clock(struct imon_context *ictx,
587 unsigned int year, unsigned int month,
588 unsigned int day, unsigned int dow,
589 unsigned int hour, unsigned int minute,
590 unsigned int second)
591{
592 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
593 int retval = 0;
594 int i;
595
596 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300597 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300598 return -ENODEV;
599 }
600
601 switch (ictx->display_type) {
602 case IMON_DISPLAY_TYPE_LCD:
603 clock_enable_pkt[0][0] = 0x80;
604 clock_enable_pkt[0][1] = year;
605 clock_enable_pkt[0][2] = month-1;
606 clock_enable_pkt[0][3] = day;
607 clock_enable_pkt[0][4] = hour;
608 clock_enable_pkt[0][5] = minute;
609 clock_enable_pkt[0][6] = second;
610
611 clock_enable_pkt[1][0] = 0x80;
612 clock_enable_pkt[1][1] = 0;
613 clock_enable_pkt[1][2] = 0;
614 clock_enable_pkt[1][3] = 0;
615 clock_enable_pkt[1][4] = 0;
616 clock_enable_pkt[1][5] = 0;
617 clock_enable_pkt[1][6] = 0;
618
619 if (ictx->product == 0xffdc) {
620 clock_enable_pkt[0][7] = 0x50;
621 clock_enable_pkt[1][7] = 0x51;
622 } else {
623 clock_enable_pkt[0][7] = 0x88;
624 clock_enable_pkt[1][7] = 0x8a;
625 }
626
627 break;
628
629 case IMON_DISPLAY_TYPE_VFD:
630 clock_enable_pkt[0][0] = year;
631 clock_enable_pkt[0][1] = month-1;
632 clock_enable_pkt[0][2] = day;
633 clock_enable_pkt[0][3] = dow;
634 clock_enable_pkt[0][4] = hour;
635 clock_enable_pkt[0][5] = minute;
636 clock_enable_pkt[0][6] = second;
637 clock_enable_pkt[0][7] = 0x40;
638
639 clock_enable_pkt[1][0] = 0;
640 clock_enable_pkt[1][1] = 0;
641 clock_enable_pkt[1][2] = 1;
642 clock_enable_pkt[1][3] = 0;
643 clock_enable_pkt[1][4] = 0;
644 clock_enable_pkt[1][5] = 0;
645 clock_enable_pkt[1][6] = 0;
646 clock_enable_pkt[1][7] = 0x42;
647
648 break;
649
650 default:
651 return -ENODEV;
652 }
653
654 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
655 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
656 retval = send_packet(ictx);
657 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300658 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300659 break;
660 }
661 }
662
663 return retval;
664}
665
666/**
667 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
668 */
669static ssize_t show_associate_remote(struct device *d,
670 struct device_attribute *attr,
671 char *buf)
672{
673 struct imon_context *ictx = dev_get_drvdata(d);
674
675 if (!ictx)
676 return -ENODEV;
677
678 mutex_lock(&ictx->lock);
679 if (ictx->rf_isassociating)
680 strcpy(buf, "associating\n");
681 else
682 strcpy(buf, "closed\n");
683
684 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
685 "instructions on how to associate your iMON 2.4G DT/LT "
686 "remote\n");
687 mutex_unlock(&ictx->lock);
688 return strlen(buf);
689}
690
691static ssize_t store_associate_remote(struct device *d,
692 struct device_attribute *attr,
693 const char *buf, size_t count)
694{
695 struct imon_context *ictx;
696
697 ictx = dev_get_drvdata(d);
698
699 if (!ictx)
700 return -ENODEV;
701
702 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300703 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300704 send_associate_24g(ictx);
705 mutex_unlock(&ictx->lock);
706
707 return count;
708}
709
710/**
711 * sysfs functions to control internal imon clock
712 */
713static ssize_t show_imon_clock(struct device *d,
714 struct device_attribute *attr, char *buf)
715{
716 struct imon_context *ictx = dev_get_drvdata(d);
717 size_t len;
718
719 if (!ictx)
720 return -ENODEV;
721
722 mutex_lock(&ictx->lock);
723
724 if (!ictx->display_supported) {
725 len = snprintf(buf, PAGE_SIZE, "Not supported.");
726 } else {
727 len = snprintf(buf, PAGE_SIZE,
728 "To set the clock on your iMON display:\n"
729 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
730 "%s", ictx->display_isopen ?
731 "\nNOTE: imon device must be closed\n" : "");
732 }
733
734 mutex_unlock(&ictx->lock);
735
736 return len;
737}
738
739static ssize_t store_imon_clock(struct device *d,
740 struct device_attribute *attr,
741 const char *buf, size_t count)
742{
743 struct imon_context *ictx = dev_get_drvdata(d);
744 ssize_t retval;
745 unsigned int year, month, day, dow, hour, minute, second;
746
747 if (!ictx)
748 return -ENODEV;
749
750 mutex_lock(&ictx->lock);
751
752 if (!ictx->display_supported) {
753 retval = -ENODEV;
754 goto exit;
755 } else if (ictx->display_isopen) {
756 retval = -EBUSY;
757 goto exit;
758 }
759
760 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
761 &hour, &minute, &second) != 7) {
762 retval = -EINVAL;
763 goto exit;
764 }
765
766 if ((month < 1 || month > 12) ||
767 (day < 1 || day > 31) || (dow > 6) ||
768 (hour > 23) || (minute > 59) || (second > 59)) {
769 retval = -EINVAL;
770 goto exit;
771 }
772
773 retval = send_set_imon_clock(ictx, year, month, day, dow,
774 hour, minute, second);
775 if (retval)
776 goto exit;
777
778 retval = count;
779exit:
780 mutex_unlock(&ictx->lock);
781
782 return retval;
783}
784
785
786static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
787 store_imon_clock);
788
789static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
790 store_associate_remote);
791
792static struct attribute *imon_display_sysfs_entries[] = {
793 &dev_attr_imon_clock.attr,
794 NULL
795};
796
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300797static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300798 .attrs = imon_display_sysfs_entries
799};
800
801static struct attribute *imon_rf_sysfs_entries[] = {
802 &dev_attr_associate_remote.attr,
803 NULL
804};
805
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300806static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300807 .attrs = imon_rf_sysfs_entries
808};
809
810/**
811 * Writes data to the VFD. The iMON VFD is 2x16 characters
812 * and requires data in 5 consecutive USB interrupt packets,
813 * each packet but the last carrying 7 bytes.
814 *
815 * I don't know if the VFD board supports features such as
816 * scrolling, clearing rows, blanking, etc. so at
817 * the caller must provide a full screen of data. If fewer
818 * than 32 bytes are provided spaces will be appended to
819 * generate a full screen.
820 */
821static ssize_t vfd_write(struct file *file, const char *buf,
822 size_t n_bytes, loff_t *pos)
823{
824 int i;
825 int offset;
826 int seq;
827 int retval = 0;
828 struct imon_context *ictx;
829 const unsigned char vfd_packet6[] = {
830 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
831
Joe Perchesabf84382010-07-12 17:50:03 -0300832 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300833 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300834 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300835 return -ENODEV;
836 }
837
838 mutex_lock(&ictx->lock);
839
840 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300841 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300842 retval = -ENODEV;
843 goto exit;
844 }
845
846 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300847 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300848 retval = -EINVAL;
849 goto exit;
850 }
851
852 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
853 retval = -EFAULT;
854 goto exit;
855 }
856
857 /* Pad with spaces */
858 for (i = n_bytes; i < 32; ++i)
859 ictx->tx.data_buf[i] = ' ';
860
861 for (i = 32; i < 35; ++i)
862 ictx->tx.data_buf[i] = 0xFF;
863
864 offset = 0;
865 seq = 0;
866
867 do {
868 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
869 ictx->usb_tx_buf[7] = (unsigned char) seq;
870
871 retval = send_packet(ictx);
872 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300873 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300874 goto exit;
875 } else {
876 seq += 2;
877 offset += 7;
878 }
879
880 } while (offset < 35);
881
882 /* Send packet #6 */
883 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
884 ictx->usb_tx_buf[7] = (unsigned char) seq;
885 retval = send_packet(ictx);
886 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300887 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300888
889exit:
890 mutex_unlock(&ictx->lock);
891
892 return (!retval) ? n_bytes : retval;
893}
894
895/**
896 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
897 * packets. We accept data as 16 hexadecimal digits, followed by a
898 * newline (to make it easy to drive the device from a command-line
899 * -- even though the actual binary data is a bit complicated).
900 *
901 * The device itself is not a "traditional" text-mode display. It's
902 * actually a 16x96 pixel bitmap display. That means if you want to
903 * display text, you've got to have your own "font" and translate the
904 * text into bitmaps for display. This is really flexible (you can
905 * display whatever diacritics you need, and so on), but it's also
906 * a lot more complicated than most LCDs...
907 */
908static ssize_t lcd_write(struct file *file, const char *buf,
909 size_t n_bytes, loff_t *pos)
910{
911 int retval = 0;
912 struct imon_context *ictx;
913
Joe Perchesabf84382010-07-12 17:50:03 -0300914 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300915 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300916 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300917 return -ENODEV;
918 }
919
920 mutex_lock(&ictx->lock);
921
922 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300923 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300924 retval = -ENODEV;
925 goto exit;
926 }
927
928 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300929 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
930 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300931 retval = -EINVAL;
932 goto exit;
933 }
934
935 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
936 retval = -EFAULT;
937 goto exit;
938 }
939
940 retval = send_packet(ictx);
941 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300942 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300943 goto exit;
944 } else {
945 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
946 __func__, (int) n_bytes);
947 }
948exit:
949 mutex_unlock(&ictx->lock);
950 return (!retval) ? n_bytes : retval;
951}
952
953/**
954 * Callback function for USB core API: transmit data
955 */
956static void usb_tx_callback(struct urb *urb)
957{
958 struct imon_context *ictx;
959
960 if (!urb)
961 return;
962 ictx = (struct imon_context *)urb->context;
963 if (!ictx)
964 return;
965
966 ictx->tx.status = urb->status;
967
968 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300969 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300970 smp_rmb(); /* ensure later readers know we're not busy */
971 complete(&ictx->tx.finished);
972}
973
974/**
Jarod Wilson21677cf2010-04-16 18:29:02 -0300975 * report touchscreen input
976 */
977static void imon_touch_display_timeout(unsigned long data)
978{
979 struct imon_context *ictx = (struct imon_context *)data;
980
Dan Carpenterf03900d2010-05-04 08:37:33 -0300981 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300982 return;
983
984 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
985 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
986 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
987 input_sync(ictx->touch);
988}
989
990/**
991 * iMON IR receivers support two different signal sets -- those used by
992 * the iMON remotes, and those used by the Windows MCE remotes (which is
993 * really just RC-6), but only one or the other at a time, as the signals
994 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -0300995 *
996 * This function gets called two different ways, one way is from
997 * rc_register_device, for initial protocol selection/setup, and the other is
998 * via a userspace-initiated protocol change request, either by direct sysfs
999 * prodding or by something like ir-keytable. In the rc_register_device case,
1000 * the imon context lock is already held, but when initiated from userspace,
1001 * it is not, so we must acquire it prior to calling send_packet, which
1002 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001003 */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001004static int imon_ir_change_protocol(struct rc_dev *rc, u64 rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001005{
1006 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001007 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001008 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001009 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001010 unsigned char ir_proto_packet[] = {
1011 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1012
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001013 if (rc_type && !(rc_type & rc->allowed_protos))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001014 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1015 "this device does not support\n");
1016
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001017 switch (rc_type) {
1018 case RC_TYPE_RC6:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001019 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1020 ir_proto_packet[0] = 0x01;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001021 break;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001022 case RC_TYPE_UNKNOWN:
1023 case RC_TYPE_OTHER:
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001024 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001025 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001026 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001027 /* ir_proto_packet[0] = 0x00; // already the default */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001028 rc_type = RC_TYPE_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001029 break;
1030 default:
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001031 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001032 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001033 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001034 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001035 /* ir_proto_packet[0] = 0x00; // already the default */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001036 rc_type = RC_TYPE_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001037 break;
1038 }
1039
1040 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1041
Jarod Wilson23ef7102011-04-27 19:01:44 -03001042 if (!mutex_is_locked(&ictx->lock)) {
1043 unlock = true;
1044 mutex_lock(&ictx->lock);
1045 }
1046
Jarod Wilson21677cf2010-04-16 18:29:02 -03001047 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001048 if (retval)
1049 goto out;
1050
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001051 ictx->rc_type = rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001052 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001053
1054out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001055 if (unlock)
1056 mutex_unlock(&ictx->lock);
1057
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001058 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001059}
1060
1061static inline int tv2int(const struct timeval *a, const struct timeval *b)
1062{
1063 int usecs = 0;
1064 int sec = 0;
1065
1066 if (b->tv_usec > a->tv_usec) {
1067 usecs = 1000000;
1068 sec--;
1069 }
1070
1071 usecs += a->tv_usec - b->tv_usec;
1072
1073 sec += a->tv_sec - b->tv_sec;
1074 sec *= 1000;
1075 usecs /= 1000;
1076 sec += usecs;
1077
1078 if (sec < 0)
1079 sec = 1000;
1080
1081 return sec;
1082}
1083
1084/**
1085 * The directional pad behaves a bit differently, depending on whether this is
1086 * one of the older ffdc devices or a newer device. Newer devices appear to
1087 * have a higher resolution matrix for more precise mouse movement, but it
1088 * makes things overly sensitive in keyboard mode, so we do some interesting
1089 * contortions to make it less touchy. Older devices run through the same
1090 * routine with shorter timeout and a smaller threshold.
1091 */
1092static int stabilize(int a, int b, u16 timeout, u16 threshold)
1093{
1094 struct timeval ct;
1095 static struct timeval prev_time = {0, 0};
1096 static struct timeval hit_time = {0, 0};
1097 static int x, y, prev_result, hits;
1098 int result = 0;
1099 int msec, msec_hit;
1100
1101 do_gettimeofday(&ct);
1102 msec = tv2int(&ct, &prev_time);
1103 msec_hit = tv2int(&ct, &hit_time);
1104
1105 if (msec > 100) {
1106 x = 0;
1107 y = 0;
1108 hits = 0;
1109 }
1110
1111 x += a;
1112 y += b;
1113
1114 prev_time = ct;
1115
1116 if (abs(x) > threshold || abs(y) > threshold) {
1117 if (abs(y) > abs(x))
1118 result = (y > 0) ? 0x7F : 0x80;
1119 else
1120 result = (x > 0) ? 0x7F00 : 0x8000;
1121
1122 x = 0;
1123 y = 0;
1124
1125 if (result == prev_result) {
1126 hits++;
1127
1128 if (hits > 3) {
1129 switch (result) {
1130 case 0x7F:
1131 y = 17 * threshold / 30;
1132 break;
1133 case 0x80:
1134 y -= 17 * threshold / 30;
1135 break;
1136 case 0x7F00:
1137 x = 17 * threshold / 30;
1138 break;
1139 case 0x8000:
1140 x -= 17 * threshold / 30;
1141 break;
1142 }
1143 }
1144
1145 if (hits == 2 && msec_hit < timeout) {
1146 result = 0;
1147 hits = 1;
1148 }
1149 } else {
1150 prev_result = result;
1151 hits = 1;
1152 hit_time = ct;
1153 }
1154 }
1155
1156 return result;
1157}
1158
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001159static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001160{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001161 u32 keycode;
1162 u32 release;
1163 bool is_release_code = false;
1164
1165 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001166 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001167 ictx->rc_toggle = 0x0;
1168 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001169
1170 /* Look for the release of a button */
1171 if (keycode == KEY_RESERVED) {
1172 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001173 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001174 if (keycode != KEY_RESERVED)
1175 is_release_code = true;
1176 }
1177
1178 ictx->release_code = is_release_code;
1179
1180 return keycode;
1181}
1182
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001183static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001184{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001185 u32 keycode;
1186
1187#define MCE_KEY_MASK 0x7000
1188#define MCE_TOGGLE_BIT 0x8000
1189
1190 /*
1191 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1192 * (the toggle bit flipping between alternating key presses), while
1193 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1194 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1195 * but we can't or them into all codes, as some keys are decoded in
1196 * a different way w/o the same use of the toggle bit...
1197 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001198 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001199 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1200
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001201 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001202 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001203
1204 /* not used in mce mode, but make sure we know its false */
1205 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001206
1207 return keycode;
1208}
1209
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001210static u32 imon_panel_key_lookup(u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001211{
1212 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001213 u32 keycode = KEY_RESERVED;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001214
Jarod Wilson083e4722010-05-04 16:17:05 -03001215 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1216 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1217 keycode = imon_panel_key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001218 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001219 }
1220 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001221
1222 return keycode;
1223}
1224
1225static bool imon_mouse_event(struct imon_context *ictx,
1226 unsigned char *buf, int len)
1227{
1228 char rel_x = 0x00, rel_y = 0x00;
1229 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001230 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001231 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001232 unsigned long flags;
1233
1234 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001235
1236 /* newer iMON device PAD or mouse button */
1237 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1238 rel_x = buf[2];
1239 rel_y = buf[3];
1240 right_shift = 1;
1241 /* 0xffdc iMON PAD or mouse button input */
1242 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1243 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1244 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1245 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1246 if (buf[0] & 0x02)
1247 rel_x |= ~0x0f;
1248 rel_x = rel_x + rel_x / 2;
1249 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1250 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1251 if (buf[0] & 0x01)
1252 rel_y |= ~0x0f;
1253 rel_y = rel_y + rel_y / 2;
1254 right_shift = 2;
1255 /* some ffdc devices decode mouse buttons differently... */
1256 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1257 right_shift = 2;
1258 /* ch+/- buttons, which we use for an emulated scroll wheel */
1259 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1260 dir = 1;
1261 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1262 dir = -1;
1263 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001264 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001265
Jarod Wilson693508d2010-09-15 15:56:03 -03001266 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1267
Jarod Wilson21677cf2010-04-16 18:29:02 -03001268 if (mouse_input) {
1269 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1270
1271 if (dir) {
1272 input_report_rel(ictx->idev, REL_WHEEL, dir);
1273 } else if (rel_x || rel_y) {
1274 input_report_rel(ictx->idev, REL_X, rel_x);
1275 input_report_rel(ictx->idev, REL_Y, rel_y);
1276 } else {
1277 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1278 input_report_key(ictx->idev, BTN_RIGHT,
1279 buf[1] >> right_shift & 0x1);
1280 }
1281 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001282 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001283 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001284 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001285 }
1286
1287 return mouse_input;
1288}
1289
1290static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1291{
1292 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1293 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1294 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1295 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1296 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1297 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1298 input_sync(ictx->touch);
1299}
1300
1301static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1302{
1303 int dir = 0;
1304 char rel_x = 0x00, rel_y = 0x00;
1305 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001306 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001307 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001308
1309 /*
1310 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1311 * contain a position coordinate (x,y), with each component ranging
1312 * from -14 to 14. We want to down-sample this to only 4 discrete values
1313 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001314 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001315 * ignore when they get too close.
1316 */
1317 if (ictx->product != 0xffdc) {
1318 /* first, pad to 8 bytes so it conforms with everything else */
1319 buf[5] = buf[6] = buf[7] = 0;
1320 timeout = 500; /* in msecs */
1321 /* (2*threshold) x (2*threshold) square */
1322 threshold = pad_thresh ? pad_thresh : 28;
1323 rel_x = buf[2];
1324 rel_y = buf[3];
1325
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001326 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001327 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1328 dir = stabilize((int)rel_x, (int)rel_y,
1329 timeout, threshold);
1330 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001331 spin_lock_irqsave(&ictx->kc_lock,
1332 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001333 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001334 spin_unlock_irqrestore(&ictx->kc_lock,
1335 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001336 return;
1337 }
1338 buf[2] = dir & 0xFF;
1339 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001340 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001341 }
1342 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001343 /*
1344 * Hack alert: instead of using keycodes, we have
1345 * to use hard-coded scancodes here...
1346 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001347 if (abs(rel_y) > abs(rel_x)) {
1348 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1349 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001350 if (rel_y > 0)
1351 scancode = 0x01007f00; /* KEY_DOWN */
1352 else
1353 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001354 } else {
1355 buf[2] = 0;
1356 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001357 if (rel_x > 0)
1358 scancode = 0x0100007f; /* KEY_RIGHT */
1359 else
1360 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001361 }
1362 }
1363
1364 /*
1365 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1366 * device (15c2:ffdc). The remote generates various codes from
1367 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1368 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1369 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1370 * reversed endianess. Extract direction from buffer, rotate endianess,
1371 * adjust sign and feed the values into stabilize(). The resulting codes
1372 * will be 0x01008000, 0x01007F00, which match the newer devices.
1373 */
1374 } else {
1375 timeout = 10; /* in msecs */
1376 /* (2*threshold) x (2*threshold) square */
1377 threshold = pad_thresh ? pad_thresh : 15;
1378
1379 /* buf[1] is x */
1380 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1381 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1382 if (buf[0] & 0x02)
1383 rel_x |= ~0x10+1;
1384 /* buf[2] is y */
1385 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1386 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1387 if (buf[0] & 0x01)
1388 rel_y |= ~0x10+1;
1389
1390 buf[0] = 0x01;
1391 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1392
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001393 if (ictx->rc_type == RC_TYPE_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001394 dir = stabilize((int)rel_x, (int)rel_y,
1395 timeout, threshold);
1396 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001397 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001398 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001399 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001400 return;
1401 }
1402 buf[2] = dir & 0xFF;
1403 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001404 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001405 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001406 /*
1407 * Hack alert: instead of using keycodes, we have
1408 * to use hard-coded scancodes here...
1409 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001410 if (abs(rel_y) > abs(rel_x)) {
1411 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1412 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001413 if (rel_y > 0)
1414 scancode = 0x01007f00; /* KEY_DOWN */
1415 else
1416 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001417 } else {
1418 buf[2] = 0;
1419 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001420 if (rel_x > 0)
1421 scancode = 0x0100007f; /* KEY_RIGHT */
1422 else
1423 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001424 }
1425 }
1426 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001427
Jarod Wilson693508d2010-09-15 15:56:03 -03001428 if (scancode) {
1429 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001430 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001431 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1432 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001433}
1434
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001435/**
1436 * figure out if these is a press or a release. We don't actually
1437 * care about repeats, as those will be auto-generated within the IR
1438 * subsystem for repeating scancodes.
1439 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001440static int imon_parse_press_type(struct imon_context *ictx,
1441 unsigned char *buf, u8 ktype)
1442{
1443 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001444 unsigned long flags;
1445
1446 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001447
1448 /* key release of 0x02XXXXXX key */
1449 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1450 ictx->kc = ictx->last_keycode;
1451
1452 /* mouse button release on (some) 0xffdc devices */
1453 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1454 buf[2] == 0x81 && buf[3] == 0xb7)
1455 ictx->kc = ictx->last_keycode;
1456
1457 /* mouse button release on (some other) 0xffdc devices */
1458 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1459 buf[2] == 0x81 && buf[3] == 0xb7)
1460 ictx->kc = ictx->last_keycode;
1461
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001462 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001463 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001464 ictx->rc_toggle = buf[2];
1465 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001466
1467 /* incoherent or irrelevant data */
1468 } else if (ictx->kc == KEY_RESERVED)
1469 press_type = -EINVAL;
1470
1471 /* key release of 0xXXXXXXb7 key */
1472 else if (ictx->release_code)
1473 press_type = 0;
1474
1475 /* this is a button press */
1476 else
1477 press_type = 1;
1478
Jarod Wilson693508d2010-09-15 15:56:03 -03001479 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1480
Jarod Wilson21677cf2010-04-16 18:29:02 -03001481 return press_type;
1482}
1483
1484/**
1485 * Process the incoming packet
1486 */
1487static void imon_incoming_packet(struct imon_context *ictx,
1488 struct urb *urb, int intf)
1489{
1490 int len = urb->actual_length;
1491 unsigned char *buf = urb->transfer_buffer;
1492 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001493 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001494 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001495 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001496 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001497 int press_type = 0;
1498 int msec;
1499 struct timeval t;
1500 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001501 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001502
Jarod Wilson21677cf2010-04-16 18:29:02 -03001503 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001504 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001505 return;
1506
1507 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001508 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001509 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001510 ktype = IMON_KEY_PANEL;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001511 kc = imon_panel_key_lookup(scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001512 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001513 scancode = be32_to_cpu(*((u32 *)buf));
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001514 if (ictx->rc_type == RC_TYPE_RC6) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001515 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001516 if (buf[0] == 0x80)
1517 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001518 kc = imon_mce_key_lookup(ictx, scancode);
1519 } else {
1520 ktype = IMON_KEY_IMON;
1521 kc = imon_remote_key_lookup(ictx, scancode);
1522 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001523 }
1524
Jarod Wilson693508d2010-09-15 15:56:03 -03001525 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001526 /* keyboard/mouse mode toggle button */
1527 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1528 ictx->last_keycode = kc;
1529 if (!nomouse) {
1530 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1531 dev_dbg(dev, "toggling to %s mode\n",
1532 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001533 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001534 return;
1535 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001536 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001537 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1538 }
1539 }
1540
1541 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001542 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001543
1544 /* send touchscreen events through input subsystem if touchpad data */
1545 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1546 buf[7] == 0x86) {
1547 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001548 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001549
1550 /* look for mouse events with pad in mouse mode */
1551 } else if (ictx->pad_mouse) {
1552 if (imon_mouse_event(ictx, buf, len))
1553 return;
1554 }
1555
1556 /* Now for some special handling to convert pad input to arrow keys */
1557 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1558 ((len == 8) && (buf[0] & 0x40) &&
1559 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1560 len = 8;
1561 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001562 }
1563
1564 if (debug) {
1565 printk(KERN_INFO "intf%d decoded packet: ", intf);
1566 for (i = 0; i < len; ++i)
1567 printk("%02x ", buf[i]);
1568 printk("\n");
1569 }
1570
1571 press_type = imon_parse_press_type(ictx, buf, ktype);
1572 if (press_type < 0)
1573 goto not_input_data;
1574
Jarod Wilson693508d2010-09-15 15:56:03 -03001575 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001576 if (ictx->kc == KEY_UNKNOWN)
1577 goto unknown_key;
Jarod Wilson693508d2010-09-15 15:56:03 -03001578 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001579
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001580 if (ktype != IMON_KEY_PANEL) {
1581 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001582 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001583 else {
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001584 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001585 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001586 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001587 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001588 }
1589 return;
1590 }
1591
1592 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001593 spin_lock_irqsave(&ictx->kc_lock, flags);
1594
Jarod Wilson94215cc2011-06-04 14:14:41 -03001595 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001596 /* KEY_MUTE repeats from knob need to be suppressed */
1597 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001598 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001599 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001600 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001601 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001602 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001603 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001604 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001605 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001606
Jarod Wilson693508d2010-09-15 15:56:03 -03001607 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001608
Jarod Wilson428cc762010-10-23 16:42:20 -03001609 input_report_key(ictx->idev, kc, press_type);
1610 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001611
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001612 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001613 input_report_key(ictx->idev, kc, 0);
1614 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001615
Jarod Wilson94215cc2011-06-04 14:14:41 -03001616 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001617 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001618 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001619
1620 return;
1621
1622unknown_key:
Jarod Wilson693508d2010-09-15 15:56:03 -03001623 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001624 dev_info(dev, "%s: unknown keypress, code 0x%llx\n", __func__,
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001625 (long long)scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001626 return;
1627
1628not_input_data:
1629 if (len != 8) {
1630 dev_warn(dev, "imon %s: invalid incoming packet "
1631 "size (len = %d, intf%d)\n", __func__, len, intf);
1632 return;
1633 }
1634
1635 /* iMON 2.4G associate frame */
1636 if (buf[0] == 0x00 &&
1637 buf[2] == 0xFF && /* REFID */
1638 buf[3] == 0xFF &&
1639 buf[4] == 0xFF &&
1640 buf[5] == 0xFF && /* iMON 2.4G */
1641 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1642 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1643 dev_warn(dev, "%s: remote associated refid=%02X\n",
1644 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001645 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001646 }
1647}
1648
1649/**
1650 * Callback function for USB core API: receive data
1651 */
1652static void usb_rx_callback_intf0(struct urb *urb)
1653{
1654 struct imon_context *ictx;
1655 int intfnum = 0;
1656
1657 if (!urb)
1658 return;
1659
1660 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001661 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001662 return;
1663
Jarod Wilson8791d632012-01-26 12:04:11 -03001664 /*
1665 * if we get a callback before we're done configuring the hardware, we
1666 * can't yet process the data, as there's nowhere to send it, but we
1667 * still need to submit a new rx URB to avoid wedging the hardware
1668 */
1669 if (!ictx->dev_present_intf0)
1670 goto out;
1671
Jarod Wilson21677cf2010-04-16 18:29:02 -03001672 switch (urb->status) {
1673 case -ENOENT: /* usbcore unlink successful! */
1674 return;
1675
1676 case -ESHUTDOWN: /* transport endpoint was shut down */
1677 break;
1678
1679 case 0:
1680 imon_incoming_packet(ictx, urb, intfnum);
1681 break;
1682
1683 default:
1684 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1685 __func__, urb->status);
1686 break;
1687 }
1688
Jarod Wilson8791d632012-01-26 12:04:11 -03001689out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001690 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1691}
1692
1693static void usb_rx_callback_intf1(struct urb *urb)
1694{
1695 struct imon_context *ictx;
1696 int intfnum = 1;
1697
1698 if (!urb)
1699 return;
1700
1701 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001702 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001703 return;
1704
Jarod Wilson8791d632012-01-26 12:04:11 -03001705 /*
1706 * if we get a callback before we're done configuring the hardware, we
1707 * can't yet process the data, as there's nowhere to send it, but we
1708 * still need to submit a new rx URB to avoid wedging the hardware
1709 */
1710 if (!ictx->dev_present_intf1)
1711 goto out;
1712
Jarod Wilson21677cf2010-04-16 18:29:02 -03001713 switch (urb->status) {
1714 case -ENOENT: /* usbcore unlink successful! */
1715 return;
1716
1717 case -ESHUTDOWN: /* transport endpoint was shut down */
1718 break;
1719
1720 case 0:
1721 imon_incoming_packet(ictx, urb, intfnum);
1722 break;
1723
1724 default:
1725 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1726 __func__, urb->status);
1727 break;
1728 }
1729
Jarod Wilson8791d632012-01-26 12:04:11 -03001730out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001731 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1732}
1733
Jarod Wilson04292fc2010-09-15 00:28:41 -03001734/*
1735 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1736 * devices, and all of them constantly spew interrupts, even when there
1737 * is no actual data to report. However, byte 6 of this buffer looks like
1738 * its unique across device variants, so we're trying to key off that to
1739 * figure out which display type (if any) and what IR protocol the device
1740 * actually supports. These devices have their IR protocol hard-coded into
1741 * their firmware, they can't be changed on the fly like the newer hardware.
1742 */
1743static void imon_get_ffdc_type(struct imon_context *ictx)
1744{
1745 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1746 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001747 u64 allowed_protos = RC_TYPE_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001748
1749 switch (ffdc_cfg_byte) {
1750 /* iMON Knob, no display, iMON IR + vol knob */
1751 case 0x21:
1752 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1753 ictx->display_supported = false;
1754 break;
1755 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1756 case 0x4e:
1757 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1758 ictx->display_supported = false;
1759 ictx->rf_device = true;
1760 break;
1761 /* iMON VFD, no IR (does have vol knob tho) */
1762 case 0x35:
1763 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1764 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1765 break;
1766 /* iMON VFD, iMON IR */
1767 case 0x24:
1768 case 0x85:
1769 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1770 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1771 break;
1772 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001773 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001774 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001775 case 0x9e:
1776 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1777 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001778 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001779 break;
1780 /* iMON LCD, MCE IR */
1781 case 0x9f:
1782 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1783 detected_display_type = IMON_DISPLAY_TYPE_LCD;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001784 allowed_protos = RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001785 break;
1786 default:
1787 dev_info(ictx->dev, "Unknown 0xffdc device, "
1788 "defaulting to VFD and iMON IR");
1789 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Jarod Wilson372b4242011-06-20 00:07:13 -03001790 /* We don't know which one it is, allow user to set the
1791 * RC6 one from userspace if OTHER wasn't correct. */
1792 allowed_protos |= RC_TYPE_RC6;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001793 break;
1794 }
1795
1796 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1797
1798 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001799 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001800}
1801
1802static void imon_set_display_type(struct imon_context *ictx)
1803{
1804 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1805
1806 /*
1807 * Try to auto-detect the type of display if the user hasn't set
1808 * it by hand via the display_type modparam. Default is VFD.
1809 */
1810
1811 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1812 switch (ictx->product) {
1813 case 0xffdc:
1814 /* set in imon_get_ffdc_type() */
1815 configured_display_type = ictx->display_type;
1816 break;
1817 case 0x0034:
1818 case 0x0035:
1819 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1820 break;
1821 case 0x0038:
1822 case 0x0039:
1823 case 0x0045:
1824 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1825 break;
1826 case 0x003c:
1827 case 0x0041:
1828 case 0x0042:
1829 case 0x0043:
1830 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1831 ictx->display_supported = false;
1832 break;
1833 case 0x0036:
1834 case 0x0044:
1835 default:
1836 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1837 break;
1838 }
1839 } else {
1840 configured_display_type = display_type;
1841 if (display_type == IMON_DISPLAY_TYPE_NONE)
1842 ictx->display_supported = false;
1843 else
1844 ictx->display_supported = true;
1845 dev_info(ictx->dev, "%s: overriding display type to %d via "
1846 "modparam\n", __func__, display_type);
1847 }
1848
1849 ictx->display_type = configured_display_type;
1850}
1851
David Härdemand8b4b582010-10-29 16:08:23 -03001852static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001853{
David Härdemand8b4b582010-10-29 16:08:23 -03001854 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001855 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001856 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1857 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001858
David Härdemand8b4b582010-10-29 16:08:23 -03001859 rdev = rc_allocate_device();
1860 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001861 dev_err(ictx->dev, "remote control dev allocation failed\n");
1862 goto out;
1863 }
1864
1865 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1866 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1867 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1868 sizeof(ictx->phys_rdev));
1869 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1870
David Härdemand8b4b582010-10-29 16:08:23 -03001871 rdev->input_name = ictx->name_rdev;
1872 rdev->input_phys = ictx->phys_rdev;
1873 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001874 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001875
David Härdemand8b4b582010-10-29 16:08:23 -03001876 rdev->priv = ictx;
1877 rdev->driver_type = RC_DRIVER_SCANCODE;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001878 rdev->allowed_protos = RC_TYPE_OTHER | RC_TYPE_RC6; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001879 rdev->change_protocol = imon_ir_change_protocol;
1880 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001881
Jarod Wilson04292fc2010-09-15 00:28:41 -03001882 /* Enable front-panel buttons and/or knobs */
1883 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1884 ret = send_packet(ictx);
1885 /* Not fatal, but warn about it */
1886 if (ret)
1887 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1888
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001889 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001890 imon_get_ffdc_type(ictx);
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001891 rdev->allowed_protos = ictx->rc_type;
1892 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001893
1894 imon_set_display_type(ictx);
1895
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001896 if (ictx->rc_type == RC_TYPE_RC6)
1897 rdev->map_name = RC_MAP_IMON_MCE;
1898 else
1899 rdev->map_name = RC_MAP_IMON_PAD;
1900
David Härdemand8b4b582010-10-29 16:08:23 -03001901 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001902 if (ret < 0) {
1903 dev_err(ictx->dev, "remote input dev register failed\n");
1904 goto out;
1905 }
1906
1907 return rdev;
1908
1909out:
David Härdemand8b4b582010-10-29 16:08:23 -03001910 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001911 return NULL;
1912}
1913
Jarod Wilson21677cf2010-04-16 18:29:02 -03001914static struct input_dev *imon_init_idev(struct imon_context *ictx)
1915{
1916 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001917 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001918
1919 idev = input_allocate_device();
1920 if (!idev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001921 dev_err(ictx->dev, "input dev allocation failed\n");
1922 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001923 }
1924
Jarod Wilson21677cf2010-04-16 18:29:02 -03001925 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001926 "iMON Panel, Knob and Mouse(%04x:%04x)",
1927 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001928 idev->name = ictx->name_idev;
1929
1930 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1931 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001932 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001933 idev->phys = ictx->phys_idev;
1934
Jarod Wilsondb190fc2010-04-30 16:06:12 -03001935 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001936
1937 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1938 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1939 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1940 BIT_MASK(REL_WHEEL);
1941
1942 /* panel and/or knob code support */
1943 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1944 u32 kc = imon_panel_key_table[i].keycode;
1945 __set_bit(kc, idev->keybit);
1946 }
1947
Jarod Wilson21677cf2010-04-16 18:29:02 -03001948 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1949 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001950 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001951
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001952 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001953 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001954 dev_err(ictx->dev, "input dev register failed\n");
1955 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001956 }
1957
1958 return idev;
1959
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001960out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001961 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001962 return NULL;
1963}
1964
1965static struct input_dev *imon_init_touch(struct imon_context *ictx)
1966{
1967 struct input_dev *touch;
1968 int ret;
1969
1970 touch = input_allocate_device();
1971 if (!touch) {
1972 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1973 goto touch_alloc_failed;
1974 }
1975
1976 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1977 "iMON USB Touchscreen (%04x:%04x)",
1978 ictx->vendor, ictx->product);
1979 touch->name = ictx->name_touch;
1980
1981 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1982 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001983 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001984 touch->phys = ictx->phys_touch;
1985
1986 touch->evbit[0] =
1987 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1988 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1989 BIT_MASK(BTN_TOUCH);
1990 input_set_abs_params(touch, ABS_X,
1991 0x00, 0xfff, 0, 0);
1992 input_set_abs_params(touch, ABS_Y,
1993 0x00, 0xfff, 0, 0);
1994
1995 input_set_drvdata(touch, ictx);
1996
1997 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1998 touch->dev.parent = ictx->dev;
1999 ret = input_register_device(touch);
2000 if (ret < 0) {
2001 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2002 goto touch_register_failed;
2003 }
2004
2005 return touch;
2006
2007touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03002008 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002009
2010touch_alloc_failed:
2011 return NULL;
2012}
2013
2014static bool imon_find_endpoints(struct imon_context *ictx,
2015 struct usb_host_interface *iface_desc)
2016{
2017 struct usb_endpoint_descriptor *ep;
2018 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2019 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2020 int ifnum = iface_desc->desc.bInterfaceNumber;
2021 int num_endpts = iface_desc->desc.bNumEndpoints;
2022 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002023 bool ir_ep_found = false;
2024 bool display_ep_found = false;
2025 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002026
2027 /*
2028 * Scan the endpoint list and set:
2029 * first input endpoint = IR endpoint
2030 * first output endpoint = display endpoint
2031 */
2032 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2033 ep = &iface_desc->endpoint[i].desc;
2034 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2035 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2036
2037 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2038 ep_type == USB_ENDPOINT_XFER_INT) {
2039
2040 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002041 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002042 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2043
2044 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2045 ep_type == USB_ENDPOINT_XFER_INT) {
2046 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002047 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002048 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2049 }
2050 }
2051
2052 if (ifnum == 0) {
2053 ictx->rx_endpoint_intf0 = rx_endpoint;
2054 /*
2055 * tx is used to send characters to lcd/vfd, associate RF
2056 * remotes, set IR protocol, and maybe more...
2057 */
2058 ictx->tx_endpoint = tx_endpoint;
2059 } else {
2060 ictx->rx_endpoint_intf1 = rx_endpoint;
2061 }
2062
2063 /*
2064 * If we didn't find a display endpoint, this is probably one of the
2065 * newer iMON devices that use control urb instead of interrupt
2066 */
2067 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002068 tx_control = true;
2069 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002070 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2071 "interface OUT endpoint\n", __func__);
2072 }
2073
2074 /*
2075 * Some iMON receivers have no display. Unfortunately, it seems
2076 * that SoundGraph recycles device IDs between devices both with
2077 * and without... :\
2078 */
2079 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002080 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002081 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2082 }
2083
2084 /*
2085 * iMON Touch devices have a VGA touchscreen, but no "display", as
2086 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2087 */
2088 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002089 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002090 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2091 }
2092
2093 /* Input endpoint is mandatory */
2094 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002095 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002096
2097 ictx->tx_control = tx_control;
2098
2099 if (display_ep_found)
2100 ictx->display_supported = true;
2101
2102 return ir_ep_found;
2103
2104}
2105
2106static struct imon_context *imon_init_intf0(struct usb_interface *intf)
2107{
2108 struct imon_context *ictx;
2109 struct urb *rx_urb;
2110 struct urb *tx_urb;
2111 struct device *dev = &intf->dev;
2112 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002113 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002114
2115 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2116 if (!ictx) {
2117 dev_err(dev, "%s: kzalloc failed for context", __func__);
2118 goto exit;
2119 }
2120 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2121 if (!rx_urb) {
2122 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2123 goto rx_urb_alloc_failed;
2124 }
2125 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2126 if (!tx_urb) {
2127 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2128 __func__);
2129 goto tx_urb_alloc_failed;
2130 }
2131
2132 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002133 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002134
2135 mutex_lock(&ictx->lock);
2136
2137 ictx->dev = dev;
2138 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002139 ictx->rx_urb_intf0 = rx_urb;
2140 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002141 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002142
2143 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2144 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2145
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002146 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002147 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002148 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002149 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002150 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002151
Jarod Wilson21677cf2010-04-16 18:29:02 -03002152 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2153 usb_rcvintpipe(ictx->usbdev_intf0,
2154 ictx->rx_endpoint_intf0->bEndpointAddress),
2155 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2156 usb_rx_callback_intf0, ictx,
2157 ictx->rx_endpoint_intf0->bInterval);
2158
2159 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2160 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002161 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002162 goto urb_submit_failed;
2163 }
2164
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002165 ictx->idev = imon_init_idev(ictx);
2166 if (!ictx->idev) {
2167 dev_err(dev, "%s: input device setup failed\n", __func__);
2168 goto idev_setup_failed;
2169 }
2170
2171 ictx->rdev = imon_init_rdev(ictx);
2172 if (!ictx->rdev) {
2173 dev_err(dev, "%s: rc device setup failed\n", __func__);
2174 goto rdev_setup_failed;
2175 }
2176
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002177 ictx->dev_present_intf0 = true;
2178
Jarod Wilson23ef7102011-04-27 19:01:44 -03002179 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002180 return ictx;
2181
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002182rdev_setup_failed:
2183 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002184idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002185 usb_kill_urb(ictx->rx_urb_intf0);
2186urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002187find_endpoint_failed:
2188 mutex_unlock(&ictx->lock);
2189 usb_free_urb(tx_urb);
2190tx_urb_alloc_failed:
2191 usb_free_urb(rx_urb);
2192rx_urb_alloc_failed:
2193 kfree(ictx);
2194exit:
2195 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2196
2197 return NULL;
2198}
2199
2200static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2201 struct imon_context *ictx)
2202{
2203 struct urb *rx_urb;
2204 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002205 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002206
2207 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2208 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002209 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002210 goto rx_urb_alloc_failed;
2211 }
2212
2213 mutex_lock(&ictx->lock);
2214
2215 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2216 init_timer(&ictx->ttimer);
2217 ictx->ttimer.data = (unsigned long)ictx;
2218 ictx->ttimer.function = imon_touch_display_timeout;
2219 }
2220
2221 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002222 ictx->rx_urb_intf1 = rx_urb;
2223
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002224 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002225 iface_desc = intf->cur_altsetting;
2226 if (!imon_find_endpoints(ictx, iface_desc))
2227 goto find_endpoint_failed;
2228
2229 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2230 ictx->touch = imon_init_touch(ictx);
2231 if (!ictx->touch)
2232 goto touch_setup_failed;
2233 } else
2234 ictx->touch = NULL;
2235
2236 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2237 usb_rcvintpipe(ictx->usbdev_intf1,
2238 ictx->rx_endpoint_intf1->bEndpointAddress),
2239 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2240 usb_rx_callback_intf1, ictx,
2241 ictx->rx_endpoint_intf1->bInterval);
2242
2243 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2244
2245 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002246 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002247 goto urb_submit_failed;
2248 }
2249
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002250 ictx->dev_present_intf1 = true;
2251
Jarod Wilson23ef7102011-04-27 19:01:44 -03002252 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002253 return ictx;
2254
2255urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002256 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002257 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002258touch_setup_failed:
2259find_endpoint_failed:
2260 mutex_unlock(&ictx->lock);
2261 usb_free_urb(rx_urb);
2262rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002263 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002264
2265 return NULL;
2266}
2267
Jarod Wilson21677cf2010-04-16 18:29:02 -03002268static void imon_init_display(struct imon_context *ictx,
2269 struct usb_interface *intf)
2270{
2271 int ret;
2272
2273 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2274
2275 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002276 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002277 if (ret)
2278 dev_err(ictx->dev, "Could not create display sysfs "
2279 "entries(%d)", ret);
2280
2281 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2282 ret = usb_register_dev(intf, &imon_lcd_class);
2283 else
2284 ret = usb_register_dev(intf, &imon_vfd_class);
2285 if (ret)
2286 /* Not a fatal error, so ignore */
2287 dev_info(ictx->dev, "could not get a minor number for "
2288 "display\n");
2289
2290}
2291
2292/**
2293 * Callback function for USB core API: Probe
2294 */
2295static int __devinit imon_probe(struct usb_interface *interface,
2296 const struct usb_device_id *id)
2297{
2298 struct usb_device *usbdev = NULL;
2299 struct usb_host_interface *iface_desc = NULL;
2300 struct usb_interface *first_if;
2301 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002302 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002303 int ret = 0;
2304 struct imon_context *ictx = NULL;
2305 struct imon_context *first_if_ctx = NULL;
2306 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002307
Jarod Wilson21677cf2010-04-16 18:29:02 -03002308 usbdev = usb_get_dev(interface_to_usbdev(interface));
2309 iface_desc = interface->cur_altsetting;
2310 ifnum = iface_desc->desc.bInterfaceNumber;
2311 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2312 product = le16_to_cpu(usbdev->descriptor.idProduct);
2313
2314 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2315 __func__, vendor, product, ifnum);
2316
2317 /* prevent races probing devices w/multiple interfaces */
2318 mutex_lock(&driver_lock);
2319
2320 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002321 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002322
2323 if (ifnum == 0) {
2324 ictx = imon_init_intf0(interface);
2325 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002326 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002327 ret = -ENODEV;
2328 goto fail;
2329 }
2330
Jarod Wilson21677cf2010-04-16 18:29:02 -03002331 } else {
2332 /* this is the secondary interface on the device */
2333 ictx = imon_init_intf1(interface, first_if_ctx);
2334 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002335 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002336 ret = -ENODEV;
2337 goto fail;
2338 }
2339
2340 }
2341
2342 usb_set_intfdata(interface, ictx);
2343
2344 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002345 mutex_lock(&ictx->lock);
2346
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002347 if (product == 0xffdc && ictx->rf_device) {
2348 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002349 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002350 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002351 pr_err("Could not create RF sysfs entries(%d)\n",
2352 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002353 }
2354
Jarod Wilson21677cf2010-04-16 18:29:02 -03002355 if (ictx->display_supported)
2356 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002357
2358 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002359 }
2360
Jarod Wilson21677cf2010-04-16 18:29:02 -03002361 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2362 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2363 usbdev->bus->busnum, usbdev->devnum);
2364
Jarod Wilson21677cf2010-04-16 18:29:02 -03002365 mutex_unlock(&driver_lock);
2366
2367 return 0;
2368
2369fail:
2370 mutex_unlock(&driver_lock);
2371 dev_err(dev, "unable to register, err %d\n", ret);
2372
2373 return ret;
2374}
2375
2376/**
2377 * Callback function for USB core API: disconnect
2378 */
2379static void __devexit imon_disconnect(struct usb_interface *interface)
2380{
2381 struct imon_context *ictx;
2382 struct device *dev;
2383 int ifnum;
2384
2385 /* prevent races with multi-interface device probing and display_open */
2386 mutex_lock(&driver_lock);
2387
2388 ictx = usb_get_intfdata(interface);
2389 dev = ictx->dev;
2390 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2391
Jarod Wilson21677cf2010-04-16 18:29:02 -03002392 /*
2393 * sysfs_remove_group is safe to call even if sysfs_create_group
2394 * hasn't been called
2395 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002396 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2397 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002398
2399 usb_set_intfdata(interface, NULL);
2400
2401 /* Abort ongoing write */
2402 if (ictx->tx.busy) {
2403 usb_kill_urb(ictx->tx_urb);
2404 complete_all(&ictx->tx.finished);
2405 }
2406
2407 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002408 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002409 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002410 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002411 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002412 if (ictx->display_supported) {
2413 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2414 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002415 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002416 usb_deregister_dev(interface, &imon_vfd_class);
2417 }
2418 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002419 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002420 usb_kill_urb(ictx->rx_urb_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002421 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002422 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002423 del_timer_sync(&ictx->ttimer);
2424 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002425 }
2426
Jarod Wilson76a2d212011-04-28 18:20:58 -03002427 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2428 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002429
2430 mutex_unlock(&driver_lock);
2431
2432 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2433 __func__, ifnum);
2434}
2435
2436static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2437{
2438 struct imon_context *ictx = usb_get_intfdata(intf);
2439 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2440
2441 if (ifnum == 0)
2442 usb_kill_urb(ictx->rx_urb_intf0);
2443 else
2444 usb_kill_urb(ictx->rx_urb_intf1);
2445
2446 return 0;
2447}
2448
2449static int imon_resume(struct usb_interface *intf)
2450{
2451 int rc = 0;
2452 struct imon_context *ictx = usb_get_intfdata(intf);
2453 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2454
2455 if (ifnum == 0) {
2456 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2457 usb_rcvintpipe(ictx->usbdev_intf0,
2458 ictx->rx_endpoint_intf0->bEndpointAddress),
2459 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2460 usb_rx_callback_intf0, ictx,
2461 ictx->rx_endpoint_intf0->bInterval);
2462
2463 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2464
2465 } else {
2466 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2467 usb_rcvintpipe(ictx->usbdev_intf1,
2468 ictx->rx_endpoint_intf1->bEndpointAddress),
2469 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2470 usb_rx_callback_intf1, ictx,
2471 ictx->rx_endpoint_intf1->bInterval);
2472
2473 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2474 }
2475
2476 return rc;
2477}
2478
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08002479module_usb_driver(imon_driver);