blob: 624fd33d708626fc7ce0095332f2791a910ce7a8 [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];
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300115 unsigned int send_packet_delay;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300116
117 struct tx_t {
118 unsigned char data_buf[35]; /* user data buffer */
119 struct completion finished; /* wait for write to finish */
120 bool busy; /* write in progress */
121 int status; /* status of tx completion */
122 } tx;
123
124 u16 vendor; /* usb vendor ID */
125 u16 product; /* usb product ID */
126
David Härdemand8b4b582010-10-29 16:08:23 -0300127 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300128 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300129 struct input_dev *touch; /* input device for touchscreen */
130
Jarod Wilson693508d2010-09-15 15:56:03 -0300131 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300132 u32 kc; /* current input keycode */
133 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300134 u32 rc_scancode; /* the computed remote scancode */
135 u8 rc_toggle; /* the computed remote toggle bit */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300136 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300137 bool release_code; /* some keys send a release code */
138
139 u8 display_type; /* store the display type */
140 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
141
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300142 char name_rdev[128]; /* rc input device name */
143 char phys_rdev[64]; /* rc input device phys path */
144
Jarod Wilson21677cf2010-04-16 18:29:02 -0300145 char name_idev[128]; /* input device name */
146 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300147
148 char name_touch[128]; /* touch screen name */
149 char phys_touch[64]; /* touch screen phys path */
150 struct timer_list ttimer; /* touch screen timer */
151 int touch_x; /* x coordinate on touchscreen */
152 int touch_y; /* y coordinate on touchscreen */
153};
154
155#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300156
157/* vfd character device file operations */
158static const struct file_operations vfd_fops = {
159 .owner = THIS_MODULE,
160 .open = &display_open,
161 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200162 .release = &display_close,
163 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300164};
165
166/* lcd character device file operations */
167static const struct file_operations lcd_fops = {
168 .owner = THIS_MODULE,
169 .open = &display_open,
170 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200171 .release = &display_close,
172 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300173};
174
175enum {
176 IMON_DISPLAY_TYPE_AUTO = 0,
177 IMON_DISPLAY_TYPE_VFD = 1,
178 IMON_DISPLAY_TYPE_LCD = 2,
179 IMON_DISPLAY_TYPE_VGA = 3,
180 IMON_DISPLAY_TYPE_NONE = 4,
181};
182
183enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300184 IMON_KEY_IMON = 0,
185 IMON_KEY_MCE = 1,
186 IMON_KEY_PANEL = 2,
187};
188
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300189enum {
190 IMON_NEED_20MS_PKT_DELAY = 1
191};
192
Jarod Wilson21677cf2010-04-16 18:29:02 -0300193/*
194 * USB Device ID for iMON USB Control Boards
195 *
196 * The Windows drivers contain 6 different inf files, more or less one for
197 * each new device until the 0x0034-0x0046 devices, which all use the same
198 * driver. Some of the devices in the 34-46 range haven't been definitively
199 * identified yet. Early devices have either a TriGem Computer, Inc. or a
200 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
201 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
202 * the ffdc and later devices, which do onboard decoding.
203 */
204static struct usb_device_id imon_usb_id_table[] = {
205 /*
206 * Several devices with this same device ID, all use iMON_PAD.inf
207 * SoundGraph iMON PAD (IR & VFD)
208 * SoundGraph iMON PAD (IR & LCD)
209 * SoundGraph iMON Knob (IR only)
210 */
211 { USB_DEVICE(0x15c2, 0xffdc) },
212
213 /*
214 * Newer devices, all driven by the latest iMON Windows driver, full
215 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
216 * Need user input to fill in details on unknown devices.
217 */
218 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
219 { USB_DEVICE(0x15c2, 0x0034) },
220 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
221 { USB_DEVICE(0x15c2, 0x0035) },
222 /* SoundGraph iMON OEM VFD (IR & VFD) */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300223 { USB_DEVICE(0x15c2, 0x0036), .driver_info = IMON_NEED_20MS_PKT_DELAY },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300224 /* device specifics unknown */
225 { USB_DEVICE(0x15c2, 0x0037) },
226 /* SoundGraph iMON OEM LCD (IR & LCD) */
227 { USB_DEVICE(0x15c2, 0x0038) },
228 /* SoundGraph iMON UltraBay (IR & LCD) */
229 { USB_DEVICE(0x15c2, 0x0039) },
230 /* device specifics unknown */
231 { USB_DEVICE(0x15c2, 0x003a) },
232 /* device specifics unknown */
233 { USB_DEVICE(0x15c2, 0x003b) },
234 /* SoundGraph iMON OEM Inside (IR only) */
235 { USB_DEVICE(0x15c2, 0x003c) },
236 /* device specifics unknown */
237 { USB_DEVICE(0x15c2, 0x003d) },
238 /* device specifics unknown */
239 { USB_DEVICE(0x15c2, 0x003e) },
240 /* device specifics unknown */
241 { USB_DEVICE(0x15c2, 0x003f) },
242 /* device specifics unknown */
243 { USB_DEVICE(0x15c2, 0x0040) },
244 /* SoundGraph iMON MINI (IR only) */
245 { USB_DEVICE(0x15c2, 0x0041) },
246 /* Antec Veris Multimedia Station EZ External (IR only) */
247 { USB_DEVICE(0x15c2, 0x0042) },
248 /* Antec Veris Multimedia Station Basic Internal (IR only) */
249 { USB_DEVICE(0x15c2, 0x0043) },
250 /* Antec Veris Multimedia Station Elite (IR & VFD) */
251 { USB_DEVICE(0x15c2, 0x0044) },
252 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
253 { USB_DEVICE(0x15c2, 0x0045) },
254 /* device specifics unknown */
255 { USB_DEVICE(0x15c2, 0x0046) },
256 {}
257};
258
259/* USB Device data */
260static struct usb_driver imon_driver = {
261 .name = MOD_NAME,
262 .probe = imon_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800263 .disconnect = imon_disconnect,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300264 .suspend = imon_suspend,
265 .resume = imon_resume,
266 .id_table = imon_usb_id_table,
267};
268
269static struct usb_class_driver imon_vfd_class = {
270 .name = DEVICE_NAME,
271 .fops = &vfd_fops,
272 .minor_base = DISPLAY_MINOR_BASE,
273};
274
275static struct usb_class_driver imon_lcd_class = {
276 .name = DEVICE_NAME,
277 .fops = &lcd_fops,
278 .minor_base = DISPLAY_MINOR_BASE,
279};
280
281/* imon receiver front panel/knob key table */
282static const struct {
283 u64 hw_code;
284 u32 keycode;
285} imon_panel_key_table[] = {
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300286 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
287 { 0x000000001200ffeell, KEY_UP },
288 { 0x000000001300ffeell, KEY_DOWN },
289 { 0x000000001400ffeell, KEY_LEFT },
290 { 0x000000001500ffeell, KEY_RIGHT },
291 { 0x000000001600ffeell, KEY_ENTER },
292 { 0x000000001700ffeell, KEY_ESC },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300293 { 0x000000001f00ffeell, KEY_AUDIO },
294 { 0x000000002000ffeell, KEY_VIDEO },
295 { 0x000000002100ffeell, KEY_CAMERA },
296 { 0x000000002700ffeell, KEY_DVD },
297 { 0x000000002300ffeell, KEY_TV },
Jarod Wilson53a5fd42011-02-01 16:27:05 -0300298 { 0x000000002b00ffeell, KEY_EXIT },
299 { 0x000000002c00ffeell, KEY_SELECT },
300 { 0x000000002d00ffeell, KEY_MENU },
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300301 { 0x000000000500ffeell, KEY_PREVIOUS },
302 { 0x000000000700ffeell, KEY_REWIND },
303 { 0x000000000400ffeell, KEY_STOP },
304 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
305 { 0x000000000800ffeell, KEY_FASTFORWARD },
306 { 0x000000000600ffeell, KEY_NEXT },
307 { 0x000000010000ffeell, KEY_RIGHT },
308 { 0x000001000000ffeell, KEY_LEFT },
309 { 0x000000003d00ffeell, KEY_SELECT },
310 { 0x000100000000ffeell, KEY_VOLUMEUP },
311 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
312 { 0x000000000100ffeell, KEY_MUTE },
Jarod Wilson04292fc2010-09-15 00:28:41 -0300313 /* 0xffdc iMON MCE VFD */
314 { 0x00010000ffffffeell, KEY_VOLUMEUP },
315 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
Jarod Wilson443b3912011-06-04 14:00:54 -0300316 { 0x00000001ffffffeell, KEY_MUTE },
317 { 0x0000000fffffffeell, KEY_MEDIA },
318 { 0x00000012ffffffeell, KEY_UP },
319 { 0x00000013ffffffeell, KEY_DOWN },
320 { 0x00000014ffffffeell, KEY_LEFT },
321 { 0x00000015ffffffeell, KEY_RIGHT },
322 { 0x00000016ffffffeell, KEY_ENTER },
323 { 0x00000017ffffffeell, KEY_ESC },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300324 /* iMON Knob values */
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -0300325 { 0x000100ffffffffeell, KEY_VOLUMEUP },
326 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
327 { 0x000008ffffffffeell, KEY_MUTE },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300328};
329
330/* to prevent races between open() and disconnect(), probing, etc */
331static DEFINE_MUTEX(driver_lock);
332
333/* Module bookkeeping bits */
334MODULE_AUTHOR(MOD_AUTHOR);
335MODULE_DESCRIPTION(MOD_DESC);
336MODULE_VERSION(MOD_VERSION);
337MODULE_LICENSE("GPL");
338MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
339
340static bool debug;
341module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300342MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300343
344/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
345static int display_type;
346module_param(display_type, int, S_IRUGO);
347MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
348 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
349
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300350static int pad_stabilize = 1;
351module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
352MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
353 "presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300354
355/*
356 * In certain use cases, mouse mode isn't really helpful, and could actually
357 * cause confusion, so allow disabling it when the IR device is open.
358 */
359static bool nomouse;
360module_param(nomouse, bool, S_IRUGO | S_IWUSR);
361MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
362 "open. 0=don't disable, 1=disable. (default: don't disable)");
363
364/* threshold at which a pad push registers as an arrow key in kbd mode */
365static int pad_thresh;
366module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
367MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
368 "arrow key in kbd mode (default: 28)");
369
370
371static void free_imon_context(struct imon_context *ictx)
372{
373 struct device *dev = ictx->dev;
374
375 usb_free_urb(ictx->tx_urb);
376 usb_free_urb(ictx->rx_urb_intf0);
377 usb_free_urb(ictx->rx_urb_intf1);
378 kfree(ictx);
379
380 dev_dbg(dev, "%s: iMON context freed\n", __func__);
381}
382
383/**
384 * Called when the Display device (e.g. /dev/lcd0)
385 * is opened by the application.
386 */
387static int display_open(struct inode *inode, struct file *file)
388{
389 struct usb_interface *interface;
390 struct imon_context *ictx = NULL;
391 int subminor;
392 int retval = 0;
393
394 /* prevent races with disconnect */
395 mutex_lock(&driver_lock);
396
397 subminor = iminor(inode);
398 interface = usb_find_interface(&imon_driver, subminor);
399 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300400 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300401 retval = -ENODEV;
402 goto exit;
403 }
404 ictx = usb_get_intfdata(interface);
405
406 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300407 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300408 retval = -ENODEV;
409 goto exit;
410 }
411
412 mutex_lock(&ictx->lock);
413
414 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300415 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300416 retval = -ENODEV;
417 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300418 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300419 retval = -EBUSY;
420 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300421 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300422 file->private_data = ictx;
423 dev_dbg(ictx->dev, "display port opened\n");
424 }
425
426 mutex_unlock(&ictx->lock);
427
428exit:
429 mutex_unlock(&driver_lock);
430 return retval;
431}
432
433/**
434 * Called when the display device (e.g. /dev/lcd0)
435 * is closed by the application.
436 */
437static int display_close(struct inode *inode, struct file *file)
438{
439 struct imon_context *ictx = NULL;
440 int retval = 0;
441
Joe Perchesabf84382010-07-12 17:50:03 -0300442 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300443
444 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300445 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300446 return -ENODEV;
447 }
448
449 mutex_lock(&ictx->lock);
450
451 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300452 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300453 retval = -ENODEV;
454 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300455 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300456 retval = -EIO;
457 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300458 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300459 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300460 }
461
462 mutex_unlock(&ictx->lock);
463 return retval;
464}
465
466/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300467 * Sends a packet to the device -- this function must be called with
468 * ictx->lock held, or its unlock/lock sequence while waiting for tx
469 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300470 */
471static int send_packet(struct imon_context *ictx)
472{
473 unsigned int pipe;
474 unsigned long timeout;
475 int interval = 0;
476 int retval = 0;
477 struct usb_ctrlrequest *control_req = NULL;
478
479 /* Check if we need to use control or interrupt urb */
480 if (!ictx->tx_control) {
481 pipe = usb_sndintpipe(ictx->usbdev_intf0,
482 ictx->tx_endpoint->bEndpointAddress);
483 interval = ictx->tx_endpoint->bInterval;
484
485 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
486 ictx->usb_tx_buf,
487 sizeof(ictx->usb_tx_buf),
488 usb_tx_callback, ictx, interval);
489
490 ictx->tx_urb->actual_length = 0;
491 } else {
492 /* fill request into kmalloc'ed space: */
493 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
494 GFP_KERNEL);
495 if (control_req == NULL)
496 return -ENOMEM;
497
498 /* setup packet is '21 09 0200 0001 0008' */
499 control_req->bRequestType = 0x21;
500 control_req->bRequest = 0x09;
501 control_req->wValue = cpu_to_le16(0x0200);
502 control_req->wIndex = cpu_to_le16(0x0001);
503 control_req->wLength = cpu_to_le16(0x0008);
504
505 /* control pipe is endpoint 0x00 */
506 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
507
508 /* build the control urb */
509 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
510 pipe, (unsigned char *)control_req,
511 ictx->usb_tx_buf,
512 sizeof(ictx->usb_tx_buf),
513 usb_tx_callback, ictx);
514 ictx->tx_urb->actual_length = 0;
515 }
516
517 init_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300518 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300519 smp_rmb(); /* ensure later readers know we're busy */
520
521 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
522 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300523 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300524 smp_rmb(); /* ensure later readers know we're not busy */
Jarod Wilson47a09b02011-07-14 14:20:46 -0300525 pr_err_ratelimited("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300526 } else {
527 /* Wait for transmission to complete (or abort) */
528 mutex_unlock(&ictx->lock);
529 retval = wait_for_completion_interruptible(
530 &ictx->tx.finished);
531 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300532 pr_err_ratelimited("task interrupted\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300533 mutex_lock(&ictx->lock);
534
535 retval = ictx->tx.status;
536 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300537 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300538 }
539
540 kfree(control_req);
541
542 /*
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300543 * Induce a mandatory delay before returning, as otherwise,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300544 * send_packet can get called so rapidly as to overwhelm the device,
545 * particularly on faster systems and/or those with quirky usb.
546 */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300547 timeout = msecs_to_jiffies(ictx->send_packet_delay);
548 set_current_state(TASK_INTERRUPTIBLE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300549 schedule_timeout(timeout);
550
551 return retval;
552}
553
554/**
555 * Sends an associate packet to the iMON 2.4G.
556 *
557 * This might not be such a good idea, since it has an id collision with
558 * some versions of the "IR & VFD" combo. The only way to determine if it
559 * is an RF version is to look at the product description string. (Which
560 * we currently do not fetch).
561 */
562static int send_associate_24g(struct imon_context *ictx)
563{
564 int retval;
565 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
566 0x00, 0x00, 0x00, 0x20 };
567
568 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300569 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300570 return -ENODEV;
571 }
572
573 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300574 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300575 return -ENODEV;
576 }
577
578 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
579 retval = send_packet(ictx);
580
581 return retval;
582}
583
584/**
585 * Sends packets to setup and show clock on iMON display
586 *
587 * Arguments: year - last 2 digits of year, month - 1..12,
588 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
589 * hour - 0..23, minute - 0..59, second - 0..59
590 */
591static int send_set_imon_clock(struct imon_context *ictx,
592 unsigned int year, unsigned int month,
593 unsigned int day, unsigned int dow,
594 unsigned int hour, unsigned int minute,
595 unsigned int second)
596{
597 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
598 int retval = 0;
599 int i;
600
601 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300602 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300603 return -ENODEV;
604 }
605
606 switch (ictx->display_type) {
607 case IMON_DISPLAY_TYPE_LCD:
608 clock_enable_pkt[0][0] = 0x80;
609 clock_enable_pkt[0][1] = year;
610 clock_enable_pkt[0][2] = month-1;
611 clock_enable_pkt[0][3] = day;
612 clock_enable_pkt[0][4] = hour;
613 clock_enable_pkt[0][5] = minute;
614 clock_enable_pkt[0][6] = second;
615
616 clock_enable_pkt[1][0] = 0x80;
617 clock_enable_pkt[1][1] = 0;
618 clock_enable_pkt[1][2] = 0;
619 clock_enable_pkt[1][3] = 0;
620 clock_enable_pkt[1][4] = 0;
621 clock_enable_pkt[1][5] = 0;
622 clock_enable_pkt[1][6] = 0;
623
624 if (ictx->product == 0xffdc) {
625 clock_enable_pkt[0][7] = 0x50;
626 clock_enable_pkt[1][7] = 0x51;
627 } else {
628 clock_enable_pkt[0][7] = 0x88;
629 clock_enable_pkt[1][7] = 0x8a;
630 }
631
632 break;
633
634 case IMON_DISPLAY_TYPE_VFD:
635 clock_enable_pkt[0][0] = year;
636 clock_enable_pkt[0][1] = month-1;
637 clock_enable_pkt[0][2] = day;
638 clock_enable_pkt[0][3] = dow;
639 clock_enable_pkt[0][4] = hour;
640 clock_enable_pkt[0][5] = minute;
641 clock_enable_pkt[0][6] = second;
642 clock_enable_pkt[0][7] = 0x40;
643
644 clock_enable_pkt[1][0] = 0;
645 clock_enable_pkt[1][1] = 0;
646 clock_enable_pkt[1][2] = 1;
647 clock_enable_pkt[1][3] = 0;
648 clock_enable_pkt[1][4] = 0;
649 clock_enable_pkt[1][5] = 0;
650 clock_enable_pkt[1][6] = 0;
651 clock_enable_pkt[1][7] = 0x42;
652
653 break;
654
655 default:
656 return -ENODEV;
657 }
658
659 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
660 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
661 retval = send_packet(ictx);
662 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300663 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300664 break;
665 }
666 }
667
668 return retval;
669}
670
671/**
672 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
673 */
674static ssize_t show_associate_remote(struct device *d,
675 struct device_attribute *attr,
676 char *buf)
677{
678 struct imon_context *ictx = dev_get_drvdata(d);
679
680 if (!ictx)
681 return -ENODEV;
682
683 mutex_lock(&ictx->lock);
684 if (ictx->rf_isassociating)
685 strcpy(buf, "associating\n");
686 else
687 strcpy(buf, "closed\n");
688
689 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
690 "instructions on how to associate your iMON 2.4G DT/LT "
691 "remote\n");
692 mutex_unlock(&ictx->lock);
693 return strlen(buf);
694}
695
696static ssize_t store_associate_remote(struct device *d,
697 struct device_attribute *attr,
698 const char *buf, size_t count)
699{
700 struct imon_context *ictx;
701
702 ictx = dev_get_drvdata(d);
703
704 if (!ictx)
705 return -ENODEV;
706
707 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300708 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300709 send_associate_24g(ictx);
710 mutex_unlock(&ictx->lock);
711
712 return count;
713}
714
715/**
716 * sysfs functions to control internal imon clock
717 */
718static ssize_t show_imon_clock(struct device *d,
719 struct device_attribute *attr, char *buf)
720{
721 struct imon_context *ictx = dev_get_drvdata(d);
722 size_t len;
723
724 if (!ictx)
725 return -ENODEV;
726
727 mutex_lock(&ictx->lock);
728
729 if (!ictx->display_supported) {
730 len = snprintf(buf, PAGE_SIZE, "Not supported.");
731 } else {
732 len = snprintf(buf, PAGE_SIZE,
733 "To set the clock on your iMON display:\n"
734 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
735 "%s", ictx->display_isopen ?
736 "\nNOTE: imon device must be closed\n" : "");
737 }
738
739 mutex_unlock(&ictx->lock);
740
741 return len;
742}
743
744static ssize_t store_imon_clock(struct device *d,
745 struct device_attribute *attr,
746 const char *buf, size_t count)
747{
748 struct imon_context *ictx = dev_get_drvdata(d);
749 ssize_t retval;
750 unsigned int year, month, day, dow, hour, minute, second;
751
752 if (!ictx)
753 return -ENODEV;
754
755 mutex_lock(&ictx->lock);
756
757 if (!ictx->display_supported) {
758 retval = -ENODEV;
759 goto exit;
760 } else if (ictx->display_isopen) {
761 retval = -EBUSY;
762 goto exit;
763 }
764
765 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
766 &hour, &minute, &second) != 7) {
767 retval = -EINVAL;
768 goto exit;
769 }
770
771 if ((month < 1 || month > 12) ||
772 (day < 1 || day > 31) || (dow > 6) ||
773 (hour > 23) || (minute > 59) || (second > 59)) {
774 retval = -EINVAL;
775 goto exit;
776 }
777
778 retval = send_set_imon_clock(ictx, year, month, day, dow,
779 hour, minute, second);
780 if (retval)
781 goto exit;
782
783 retval = count;
784exit:
785 mutex_unlock(&ictx->lock);
786
787 return retval;
788}
789
790
791static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
792 store_imon_clock);
793
794static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
795 store_associate_remote);
796
797static struct attribute *imon_display_sysfs_entries[] = {
798 &dev_attr_imon_clock.attr,
799 NULL
800};
801
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300802static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300803 .attrs = imon_display_sysfs_entries
804};
805
806static struct attribute *imon_rf_sysfs_entries[] = {
807 &dev_attr_associate_remote.attr,
808 NULL
809};
810
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300811static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300812 .attrs = imon_rf_sysfs_entries
813};
814
815/**
816 * Writes data to the VFD. The iMON VFD is 2x16 characters
817 * and requires data in 5 consecutive USB interrupt packets,
818 * each packet but the last carrying 7 bytes.
819 *
820 * I don't know if the VFD board supports features such as
821 * scrolling, clearing rows, blanking, etc. so at
822 * the caller must provide a full screen of data. If fewer
823 * than 32 bytes are provided spaces will be appended to
824 * generate a full screen.
825 */
826static ssize_t vfd_write(struct file *file, const char *buf,
827 size_t n_bytes, loff_t *pos)
828{
829 int i;
830 int offset;
831 int seq;
832 int retval = 0;
833 struct imon_context *ictx;
834 const unsigned char vfd_packet6[] = {
835 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
836
Joe Perchesabf84382010-07-12 17:50:03 -0300837 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300838 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300839 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300840 return -ENODEV;
841 }
842
843 mutex_lock(&ictx->lock);
844
845 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300846 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300847 retval = -ENODEV;
848 goto exit;
849 }
850
851 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300852 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300853 retval = -EINVAL;
854 goto exit;
855 }
856
857 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
858 retval = -EFAULT;
859 goto exit;
860 }
861
862 /* Pad with spaces */
863 for (i = n_bytes; i < 32; ++i)
864 ictx->tx.data_buf[i] = ' ';
865
866 for (i = 32; i < 35; ++i)
867 ictx->tx.data_buf[i] = 0xFF;
868
869 offset = 0;
870 seq = 0;
871
872 do {
873 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
874 ictx->usb_tx_buf[7] = (unsigned char) seq;
875
876 retval = send_packet(ictx);
877 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300878 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300879 goto exit;
880 } else {
881 seq += 2;
882 offset += 7;
883 }
884
885 } while (offset < 35);
886
887 /* Send packet #6 */
888 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
889 ictx->usb_tx_buf[7] = (unsigned char) seq;
890 retval = send_packet(ictx);
891 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300892 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300893
894exit:
895 mutex_unlock(&ictx->lock);
896
897 return (!retval) ? n_bytes : retval;
898}
899
900/**
901 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
902 * packets. We accept data as 16 hexadecimal digits, followed by a
903 * newline (to make it easy to drive the device from a command-line
904 * -- even though the actual binary data is a bit complicated).
905 *
906 * The device itself is not a "traditional" text-mode display. It's
907 * actually a 16x96 pixel bitmap display. That means if you want to
908 * display text, you've got to have your own "font" and translate the
909 * text into bitmaps for display. This is really flexible (you can
910 * display whatever diacritics you need, and so on), but it's also
911 * a lot more complicated than most LCDs...
912 */
913static ssize_t lcd_write(struct file *file, const char *buf,
914 size_t n_bytes, loff_t *pos)
915{
916 int retval = 0;
917 struct imon_context *ictx;
918
Joe Perchesabf84382010-07-12 17:50:03 -0300919 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300920 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300921 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300922 return -ENODEV;
923 }
924
925 mutex_lock(&ictx->lock);
926
927 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300928 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300929 retval = -ENODEV;
930 goto exit;
931 }
932
933 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300934 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
935 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300936 retval = -EINVAL;
937 goto exit;
938 }
939
940 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
941 retval = -EFAULT;
942 goto exit;
943 }
944
945 retval = send_packet(ictx);
946 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300947 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300948 goto exit;
949 } else {
950 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
951 __func__, (int) n_bytes);
952 }
953exit:
954 mutex_unlock(&ictx->lock);
955 return (!retval) ? n_bytes : retval;
956}
957
958/**
959 * Callback function for USB core API: transmit data
960 */
961static void usb_tx_callback(struct urb *urb)
962{
963 struct imon_context *ictx;
964
965 if (!urb)
966 return;
967 ictx = (struct imon_context *)urb->context;
968 if (!ictx)
969 return;
970
971 ictx->tx.status = urb->status;
972
973 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300974 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300975 smp_rmb(); /* ensure later readers know we're not busy */
976 complete(&ictx->tx.finished);
977}
978
979/**
Jarod Wilson21677cf2010-04-16 18:29:02 -0300980 * report touchscreen input
981 */
982static void imon_touch_display_timeout(unsigned long data)
983{
984 struct imon_context *ictx = (struct imon_context *)data;
985
Dan Carpenterf03900d2010-05-04 08:37:33 -0300986 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300987 return;
988
989 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
990 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
991 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
992 input_sync(ictx->touch);
993}
994
995/**
996 * iMON IR receivers support two different signal sets -- those used by
997 * the iMON remotes, and those used by the Windows MCE remotes (which is
998 * really just RC-6), but only one or the other at a time, as the signals
999 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -03001000 *
1001 * This function gets called two different ways, one way is from
1002 * rc_register_device, for initial protocol selection/setup, and the other is
1003 * via a userspace-initiated protocol change request, either by direct sysfs
1004 * prodding or by something like ir-keytable. In the rc_register_device case,
1005 * the imon context lock is already held, but when initiated from userspace,
1006 * it is not, so we must acquire it prior to calling send_packet, which
1007 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001008 */
David Härdemanc003ab12012-10-11 19:11:54 -03001009static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001010{
1011 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001012 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001013 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001014 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001015 unsigned char ir_proto_packet[] = {
1016 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1017
David Härdemanc003ab12012-10-11 19:11:54 -03001018 if (*rc_type && !(*rc_type & rc->allowed_protos))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001019 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1020 "this device does not support\n");
1021
David Härdemanc003ab12012-10-11 19:11:54 -03001022 if (*rc_type & RC_BIT_RC6_MCE) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001023 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1024 ir_proto_packet[0] = 0x01;
David Härdemanc003ab12012-10-11 19:11:54 -03001025 *rc_type = RC_BIT_RC6_MCE;
1026 } else if (*rc_type & RC_BIT_OTHER) {
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001027 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001028 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001029 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001030 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001031 *rc_type = RC_BIT_OTHER;
1032 } else {
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001033 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001034 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001035 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001036 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001037 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001038 *rc_type = RC_BIT_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001039 }
1040
1041 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1042
Jarod Wilson23ef7102011-04-27 19:01:44 -03001043 if (!mutex_is_locked(&ictx->lock)) {
1044 unlock = true;
1045 mutex_lock(&ictx->lock);
1046 }
1047
Jarod Wilson21677cf2010-04-16 18:29:02 -03001048 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001049 if (retval)
1050 goto out;
1051
David Härdemanc003ab12012-10-11 19:11:54 -03001052 ictx->rc_type = *rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001053 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001054
1055out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001056 if (unlock)
1057 mutex_unlock(&ictx->lock);
1058
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001059 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001060}
1061
1062static inline int tv2int(const struct timeval *a, const struct timeval *b)
1063{
1064 int usecs = 0;
1065 int sec = 0;
1066
1067 if (b->tv_usec > a->tv_usec) {
1068 usecs = 1000000;
1069 sec--;
1070 }
1071
1072 usecs += a->tv_usec - b->tv_usec;
1073
1074 sec += a->tv_sec - b->tv_sec;
1075 sec *= 1000;
1076 usecs /= 1000;
1077 sec += usecs;
1078
1079 if (sec < 0)
1080 sec = 1000;
1081
1082 return sec;
1083}
1084
1085/**
1086 * The directional pad behaves a bit differently, depending on whether this is
1087 * one of the older ffdc devices or a newer device. Newer devices appear to
1088 * have a higher resolution matrix for more precise mouse movement, but it
1089 * makes things overly sensitive in keyboard mode, so we do some interesting
1090 * contortions to make it less touchy. Older devices run through the same
1091 * routine with shorter timeout and a smaller threshold.
1092 */
1093static int stabilize(int a, int b, u16 timeout, u16 threshold)
1094{
1095 struct timeval ct;
1096 static struct timeval prev_time = {0, 0};
1097 static struct timeval hit_time = {0, 0};
1098 static int x, y, prev_result, hits;
1099 int result = 0;
1100 int msec, msec_hit;
1101
1102 do_gettimeofday(&ct);
1103 msec = tv2int(&ct, &prev_time);
1104 msec_hit = tv2int(&ct, &hit_time);
1105
1106 if (msec > 100) {
1107 x = 0;
1108 y = 0;
1109 hits = 0;
1110 }
1111
1112 x += a;
1113 y += b;
1114
1115 prev_time = ct;
1116
1117 if (abs(x) > threshold || abs(y) > threshold) {
1118 if (abs(y) > abs(x))
1119 result = (y > 0) ? 0x7F : 0x80;
1120 else
1121 result = (x > 0) ? 0x7F00 : 0x8000;
1122
1123 x = 0;
1124 y = 0;
1125
1126 if (result == prev_result) {
1127 hits++;
1128
1129 if (hits > 3) {
1130 switch (result) {
1131 case 0x7F:
1132 y = 17 * threshold / 30;
1133 break;
1134 case 0x80:
1135 y -= 17 * threshold / 30;
1136 break;
1137 case 0x7F00:
1138 x = 17 * threshold / 30;
1139 break;
1140 case 0x8000:
1141 x -= 17 * threshold / 30;
1142 break;
1143 }
1144 }
1145
1146 if (hits == 2 && msec_hit < timeout) {
1147 result = 0;
1148 hits = 1;
1149 }
1150 } else {
1151 prev_result = result;
1152 hits = 1;
1153 hit_time = ct;
1154 }
1155 }
1156
1157 return result;
1158}
1159
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001160static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001161{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001162 u32 keycode;
1163 u32 release;
1164 bool is_release_code = false;
1165
1166 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001167 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001168 ictx->rc_toggle = 0x0;
1169 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001170
1171 /* Look for the release of a button */
1172 if (keycode == KEY_RESERVED) {
1173 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001174 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001175 if (keycode != KEY_RESERVED)
1176 is_release_code = true;
1177 }
1178
1179 ictx->release_code = is_release_code;
1180
1181 return keycode;
1182}
1183
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001184static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001185{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001186 u32 keycode;
1187
1188#define MCE_KEY_MASK 0x7000
1189#define MCE_TOGGLE_BIT 0x8000
1190
1191 /*
1192 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1193 * (the toggle bit flipping between alternating key presses), while
1194 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1195 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1196 * but we can't or them into all codes, as some keys are decoded in
1197 * a different way w/o the same use of the toggle bit...
1198 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001199 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001200 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1201
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001202 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001203 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001204
1205 /* not used in mce mode, but make sure we know its false */
1206 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001207
1208 return keycode;
1209}
1210
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001211static u32 imon_panel_key_lookup(u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001212{
1213 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001214 u32 keycode = KEY_RESERVED;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001215
Jarod Wilson083e4722010-05-04 16:17:05 -03001216 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1217 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1218 keycode = imon_panel_key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001219 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001220 }
1221 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001222
1223 return keycode;
1224}
1225
1226static bool imon_mouse_event(struct imon_context *ictx,
1227 unsigned char *buf, int len)
1228{
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001229 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001230 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001231 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001232 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001233 unsigned long flags;
1234
1235 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001236
1237 /* newer iMON device PAD or mouse button */
1238 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1239 rel_x = buf[2];
1240 rel_y = buf[3];
1241 right_shift = 1;
1242 /* 0xffdc iMON PAD or mouse button input */
1243 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1244 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1245 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1246 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1247 if (buf[0] & 0x02)
1248 rel_x |= ~0x0f;
1249 rel_x = rel_x + rel_x / 2;
1250 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1251 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1252 if (buf[0] & 0x01)
1253 rel_y |= ~0x0f;
1254 rel_y = rel_y + rel_y / 2;
1255 right_shift = 2;
1256 /* some ffdc devices decode mouse buttons differently... */
1257 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1258 right_shift = 2;
1259 /* ch+/- buttons, which we use for an emulated scroll wheel */
1260 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1261 dir = 1;
1262 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1263 dir = -1;
1264 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001265 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001266
Jarod Wilson693508d2010-09-15 15:56:03 -03001267 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1268
Jarod Wilson21677cf2010-04-16 18:29:02 -03001269 if (mouse_input) {
1270 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1271
1272 if (dir) {
1273 input_report_rel(ictx->idev, REL_WHEEL, dir);
1274 } else if (rel_x || rel_y) {
1275 input_report_rel(ictx->idev, REL_X, rel_x);
1276 input_report_rel(ictx->idev, REL_Y, rel_y);
1277 } else {
1278 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1279 input_report_key(ictx->idev, BTN_RIGHT,
1280 buf[1] >> right_shift & 0x1);
1281 }
1282 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001283 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001284 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001285 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001286 }
1287
1288 return mouse_input;
1289}
1290
1291static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1292{
1293 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1294 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1295 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1296 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1297 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1298 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1299 input_sync(ictx->touch);
1300}
1301
1302static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1303{
1304 int dir = 0;
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001305 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001306 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001307 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001308 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001309
1310 /*
1311 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1312 * contain a position coordinate (x,y), with each component ranging
1313 * from -14 to 14. We want to down-sample this to only 4 discrete values
1314 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001315 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001316 * ignore when they get too close.
1317 */
1318 if (ictx->product != 0xffdc) {
1319 /* first, pad to 8 bytes so it conforms with everything else */
1320 buf[5] = buf[6] = buf[7] = 0;
1321 timeout = 500; /* in msecs */
1322 /* (2*threshold) x (2*threshold) square */
1323 threshold = pad_thresh ? pad_thresh : 28;
1324 rel_x = buf[2];
1325 rel_y = buf[3];
1326
David Härdemanc003ab12012-10-11 19:11:54 -03001327 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001328 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1329 dir = stabilize((int)rel_x, (int)rel_y,
1330 timeout, threshold);
1331 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001332 spin_lock_irqsave(&ictx->kc_lock,
1333 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001334 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001335 spin_unlock_irqrestore(&ictx->kc_lock,
1336 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001337 return;
1338 }
1339 buf[2] = dir & 0xFF;
1340 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001341 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001342 }
1343 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001344 /*
1345 * Hack alert: instead of using keycodes, we have
1346 * to use hard-coded scancodes here...
1347 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001348 if (abs(rel_y) > abs(rel_x)) {
1349 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1350 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001351 if (rel_y > 0)
1352 scancode = 0x01007f00; /* KEY_DOWN */
1353 else
1354 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001355 } else {
1356 buf[2] = 0;
1357 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001358 if (rel_x > 0)
1359 scancode = 0x0100007f; /* KEY_RIGHT */
1360 else
1361 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001362 }
1363 }
1364
1365 /*
1366 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1367 * device (15c2:ffdc). The remote generates various codes from
1368 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1369 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1370 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1371 * reversed endianess. Extract direction from buffer, rotate endianess,
1372 * adjust sign and feed the values into stabilize(). The resulting codes
1373 * will be 0x01008000, 0x01007F00, which match the newer devices.
1374 */
1375 } else {
1376 timeout = 10; /* in msecs */
1377 /* (2*threshold) x (2*threshold) square */
1378 threshold = pad_thresh ? pad_thresh : 15;
1379
1380 /* buf[1] is x */
1381 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1382 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1383 if (buf[0] & 0x02)
1384 rel_x |= ~0x10+1;
1385 /* buf[2] is y */
1386 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1387 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1388 if (buf[0] & 0x01)
1389 rel_y |= ~0x10+1;
1390
1391 buf[0] = 0x01;
1392 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1393
David Härdemanc003ab12012-10-11 19:11:54 -03001394 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001395 dir = stabilize((int)rel_x, (int)rel_y,
1396 timeout, threshold);
1397 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001398 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001399 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001400 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001401 return;
1402 }
1403 buf[2] = dir & 0xFF;
1404 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001405 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001406 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001407 /*
1408 * Hack alert: instead of using keycodes, we have
1409 * to use hard-coded scancodes here...
1410 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001411 if (abs(rel_y) > abs(rel_x)) {
1412 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1413 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001414 if (rel_y > 0)
1415 scancode = 0x01007f00; /* KEY_DOWN */
1416 else
1417 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001418 } else {
1419 buf[2] = 0;
1420 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001421 if (rel_x > 0)
1422 scancode = 0x0100007f; /* KEY_RIGHT */
1423 else
1424 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001425 }
1426 }
1427 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001428
Jarod Wilson693508d2010-09-15 15:56:03 -03001429 if (scancode) {
1430 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001431 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001432 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1433 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001434}
1435
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001436/**
1437 * figure out if these is a press or a release. We don't actually
1438 * care about repeats, as those will be auto-generated within the IR
1439 * subsystem for repeating scancodes.
1440 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001441static int imon_parse_press_type(struct imon_context *ictx,
1442 unsigned char *buf, u8 ktype)
1443{
1444 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001445 unsigned long flags;
1446
1447 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001448
1449 /* key release of 0x02XXXXXX key */
1450 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1451 ictx->kc = ictx->last_keycode;
1452
1453 /* mouse button release on (some) 0xffdc devices */
1454 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1455 buf[2] == 0x81 && buf[3] == 0xb7)
1456 ictx->kc = ictx->last_keycode;
1457
1458 /* mouse button release on (some other) 0xffdc devices */
1459 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1460 buf[2] == 0x81 && buf[3] == 0xb7)
1461 ictx->kc = ictx->last_keycode;
1462
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001463 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001464 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001465 ictx->rc_toggle = buf[2];
1466 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001467
1468 /* incoherent or irrelevant data */
1469 } else if (ictx->kc == KEY_RESERVED)
1470 press_type = -EINVAL;
1471
1472 /* key release of 0xXXXXXXb7 key */
1473 else if (ictx->release_code)
1474 press_type = 0;
1475
1476 /* this is a button press */
1477 else
1478 press_type = 1;
1479
Jarod Wilson693508d2010-09-15 15:56:03 -03001480 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1481
Jarod Wilson21677cf2010-04-16 18:29:02 -03001482 return press_type;
1483}
1484
1485/**
1486 * Process the incoming packet
1487 */
1488static void imon_incoming_packet(struct imon_context *ictx,
1489 struct urb *urb, int intf)
1490{
1491 int len = urb->actual_length;
1492 unsigned char *buf = urb->transfer_buffer;
1493 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001494 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001495 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001496 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001497 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001498 int press_type = 0;
1499 int msec;
1500 struct timeval t;
1501 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001502 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001503
Jarod Wilson21677cf2010-04-16 18:29:02 -03001504 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001505 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001506 return;
1507
1508 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001509 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001510 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001511 ktype = IMON_KEY_PANEL;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001512 kc = imon_panel_key_lookup(scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001513 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001514 scancode = be32_to_cpu(*((u32 *)buf));
David Härdemanc003ab12012-10-11 19:11:54 -03001515 if (ictx->rc_type == RC_BIT_RC6_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001516 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001517 if (buf[0] == 0x80)
1518 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001519 kc = imon_mce_key_lookup(ictx, scancode);
1520 } else {
1521 ktype = IMON_KEY_IMON;
1522 kc = imon_remote_key_lookup(ictx, scancode);
1523 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001524 }
1525
Jarod Wilson693508d2010-09-15 15:56:03 -03001526 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001527 /* keyboard/mouse mode toggle button */
1528 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1529 ictx->last_keycode = kc;
1530 if (!nomouse) {
1531 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1532 dev_dbg(dev, "toggling to %s mode\n",
1533 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001534 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001535 return;
1536 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001537 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001538 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1539 }
1540 }
1541
1542 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001543 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001544
1545 /* send touchscreen events through input subsystem if touchpad data */
1546 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1547 buf[7] == 0x86) {
1548 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001549 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001550
1551 /* look for mouse events with pad in mouse mode */
1552 } else if (ictx->pad_mouse) {
1553 if (imon_mouse_event(ictx, buf, len))
1554 return;
1555 }
1556
1557 /* Now for some special handling to convert pad input to arrow keys */
1558 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1559 ((len == 8) && (buf[0] & 0x40) &&
1560 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1561 len = 8;
1562 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001563 }
1564
1565 if (debug) {
1566 printk(KERN_INFO "intf%d decoded packet: ", intf);
1567 for (i = 0; i < len; ++i)
1568 printk("%02x ", buf[i]);
1569 printk("\n");
1570 }
1571
1572 press_type = imon_parse_press_type(ictx, buf, ktype);
1573 if (press_type < 0)
1574 goto not_input_data;
1575
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001576 if (ktype != IMON_KEY_PANEL) {
1577 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001578 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001579 else {
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001580 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001581 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001582 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001583 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001584 }
1585 return;
1586 }
1587
1588 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001589 spin_lock_irqsave(&ictx->kc_lock, flags);
1590
Jarod Wilson94215cc2011-06-04 14:14:41 -03001591 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001592 /* KEY_MUTE repeats from knob need to be suppressed */
1593 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001594 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001595 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001596 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001597 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001598 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001599 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001600 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001601 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001602
Jarod Wilson693508d2010-09-15 15:56:03 -03001603 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001604
Jarod Wilson428cc762010-10-23 16:42:20 -03001605 input_report_key(ictx->idev, kc, press_type);
1606 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001607
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001608 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001609 input_report_key(ictx->idev, kc, 0);
1610 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001611
Jarod Wilson94215cc2011-06-04 14:14:41 -03001612 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001613 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001614 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001615
1616 return;
1617
Jarod Wilson21677cf2010-04-16 18:29:02 -03001618not_input_data:
1619 if (len != 8) {
1620 dev_warn(dev, "imon %s: invalid incoming packet "
1621 "size (len = %d, intf%d)\n", __func__, len, intf);
1622 return;
1623 }
1624
1625 /* iMON 2.4G associate frame */
1626 if (buf[0] == 0x00 &&
1627 buf[2] == 0xFF && /* REFID */
1628 buf[3] == 0xFF &&
1629 buf[4] == 0xFF &&
1630 buf[5] == 0xFF && /* iMON 2.4G */
1631 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1632 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1633 dev_warn(dev, "%s: remote associated refid=%02X\n",
1634 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001635 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001636 }
1637}
1638
1639/**
1640 * Callback function for USB core API: receive data
1641 */
1642static void usb_rx_callback_intf0(struct urb *urb)
1643{
1644 struct imon_context *ictx;
1645 int intfnum = 0;
1646
1647 if (!urb)
1648 return;
1649
1650 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001651 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001652 return;
1653
Jarod Wilson8791d632012-01-26 12:04:11 -03001654 /*
1655 * if we get a callback before we're done configuring the hardware, we
1656 * can't yet process the data, as there's nowhere to send it, but we
1657 * still need to submit a new rx URB to avoid wedging the hardware
1658 */
1659 if (!ictx->dev_present_intf0)
1660 goto out;
1661
Jarod Wilson21677cf2010-04-16 18:29:02 -03001662 switch (urb->status) {
1663 case -ENOENT: /* usbcore unlink successful! */
1664 return;
1665
1666 case -ESHUTDOWN: /* transport endpoint was shut down */
1667 break;
1668
1669 case 0:
1670 imon_incoming_packet(ictx, urb, intfnum);
1671 break;
1672
1673 default:
1674 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1675 __func__, urb->status);
1676 break;
1677 }
1678
Jarod Wilson8791d632012-01-26 12:04:11 -03001679out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001680 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1681}
1682
1683static void usb_rx_callback_intf1(struct urb *urb)
1684{
1685 struct imon_context *ictx;
1686 int intfnum = 1;
1687
1688 if (!urb)
1689 return;
1690
1691 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001692 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001693 return;
1694
Jarod Wilson8791d632012-01-26 12:04:11 -03001695 /*
1696 * if we get a callback before we're done configuring the hardware, we
1697 * can't yet process the data, as there's nowhere to send it, but we
1698 * still need to submit a new rx URB to avoid wedging the hardware
1699 */
1700 if (!ictx->dev_present_intf1)
1701 goto out;
1702
Jarod Wilson21677cf2010-04-16 18:29:02 -03001703 switch (urb->status) {
1704 case -ENOENT: /* usbcore unlink successful! */
1705 return;
1706
1707 case -ESHUTDOWN: /* transport endpoint was shut down */
1708 break;
1709
1710 case 0:
1711 imon_incoming_packet(ictx, urb, intfnum);
1712 break;
1713
1714 default:
1715 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1716 __func__, urb->status);
1717 break;
1718 }
1719
Jarod Wilson8791d632012-01-26 12:04:11 -03001720out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001721 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1722}
1723
Jarod Wilson04292fc2010-09-15 00:28:41 -03001724/*
1725 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1726 * devices, and all of them constantly spew interrupts, even when there
1727 * is no actual data to report. However, byte 6 of this buffer looks like
1728 * its unique across device variants, so we're trying to key off that to
1729 * figure out which display type (if any) and what IR protocol the device
1730 * actually supports. These devices have their IR protocol hard-coded into
1731 * their firmware, they can't be changed on the fly like the newer hardware.
1732 */
1733static void imon_get_ffdc_type(struct imon_context *ictx)
1734{
1735 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1736 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
David Härdemanc003ab12012-10-11 19:11:54 -03001737 u64 allowed_protos = RC_BIT_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001738
1739 switch (ffdc_cfg_byte) {
1740 /* iMON Knob, no display, iMON IR + vol knob */
1741 case 0x21:
1742 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1743 ictx->display_supported = false;
1744 break;
1745 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1746 case 0x4e:
1747 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1748 ictx->display_supported = false;
1749 ictx->rf_device = true;
1750 break;
1751 /* iMON VFD, no IR (does have vol knob tho) */
1752 case 0x35:
1753 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1754 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1755 break;
1756 /* iMON VFD, iMON IR */
1757 case 0x24:
1758 case 0x85:
1759 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1760 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1761 break;
1762 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001763 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001764 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001765 case 0x9e:
1766 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1767 detected_display_type = IMON_DISPLAY_TYPE_VFD;
David Härdemanc003ab12012-10-11 19:11:54 -03001768 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001769 break;
1770 /* iMON LCD, MCE IR */
1771 case 0x9f:
1772 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1773 detected_display_type = IMON_DISPLAY_TYPE_LCD;
David Härdemanc003ab12012-10-11 19:11:54 -03001774 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001775 break;
1776 default:
1777 dev_info(ictx->dev, "Unknown 0xffdc device, "
1778 "defaulting to VFD and iMON IR");
1779 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Jarod Wilson372b4242011-06-20 00:07:13 -03001780 /* We don't know which one it is, allow user to set the
1781 * RC6 one from userspace if OTHER wasn't correct. */
David Härdemanc003ab12012-10-11 19:11:54 -03001782 allowed_protos |= RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001783 break;
1784 }
1785
1786 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1787
1788 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001789 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001790}
1791
1792static void imon_set_display_type(struct imon_context *ictx)
1793{
1794 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1795
1796 /*
1797 * Try to auto-detect the type of display if the user hasn't set
1798 * it by hand via the display_type modparam. Default is VFD.
1799 */
1800
1801 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1802 switch (ictx->product) {
1803 case 0xffdc:
1804 /* set in imon_get_ffdc_type() */
1805 configured_display_type = ictx->display_type;
1806 break;
1807 case 0x0034:
1808 case 0x0035:
1809 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1810 break;
1811 case 0x0038:
1812 case 0x0039:
1813 case 0x0045:
1814 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1815 break;
1816 case 0x003c:
1817 case 0x0041:
1818 case 0x0042:
1819 case 0x0043:
1820 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1821 ictx->display_supported = false;
1822 break;
1823 case 0x0036:
1824 case 0x0044:
1825 default:
1826 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1827 break;
1828 }
1829 } else {
1830 configured_display_type = display_type;
1831 if (display_type == IMON_DISPLAY_TYPE_NONE)
1832 ictx->display_supported = false;
1833 else
1834 ictx->display_supported = true;
1835 dev_info(ictx->dev, "%s: overriding display type to %d via "
1836 "modparam\n", __func__, display_type);
1837 }
1838
1839 ictx->display_type = configured_display_type;
1840}
1841
David Härdemand8b4b582010-10-29 16:08:23 -03001842static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001843{
David Härdemand8b4b582010-10-29 16:08:23 -03001844 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001845 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001846 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1847 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001848
David Härdemand8b4b582010-10-29 16:08:23 -03001849 rdev = rc_allocate_device();
1850 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001851 dev_err(ictx->dev, "remote control dev allocation failed\n");
1852 goto out;
1853 }
1854
1855 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1856 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1857 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1858 sizeof(ictx->phys_rdev));
1859 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1860
David Härdemand8b4b582010-10-29 16:08:23 -03001861 rdev->input_name = ictx->name_rdev;
1862 rdev->input_phys = ictx->phys_rdev;
1863 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001864 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001865
David Härdemand8b4b582010-10-29 16:08:23 -03001866 rdev->priv = ictx;
1867 rdev->driver_type = RC_DRIVER_SCANCODE;
David Härdemanc003ab12012-10-11 19:11:54 -03001868 rdev->allowed_protos = RC_BIT_OTHER | RC_BIT_RC6_MCE; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001869 rdev->change_protocol = imon_ir_change_protocol;
1870 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001871
Jarod Wilson04292fc2010-09-15 00:28:41 -03001872 /* Enable front-panel buttons and/or knobs */
1873 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1874 ret = send_packet(ictx);
1875 /* Not fatal, but warn about it */
1876 if (ret)
1877 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1878
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001879 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001880 imon_get_ffdc_type(ictx);
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001881 rdev->allowed_protos = ictx->rc_type;
1882 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001883
1884 imon_set_display_type(ictx);
1885
David Härdemanc003ab12012-10-11 19:11:54 -03001886 if (ictx->rc_type == RC_BIT_RC6_MCE)
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001887 rdev->map_name = RC_MAP_IMON_MCE;
1888 else
1889 rdev->map_name = RC_MAP_IMON_PAD;
1890
David Härdemand8b4b582010-10-29 16:08:23 -03001891 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001892 if (ret < 0) {
1893 dev_err(ictx->dev, "remote input dev register failed\n");
1894 goto out;
1895 }
1896
1897 return rdev;
1898
1899out:
David Härdemand8b4b582010-10-29 16:08:23 -03001900 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001901 return NULL;
1902}
1903
Jarod Wilson21677cf2010-04-16 18:29:02 -03001904static struct input_dev *imon_init_idev(struct imon_context *ictx)
1905{
1906 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001907 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001908
1909 idev = input_allocate_device();
1910 if (!idev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001911 dev_err(ictx->dev, "input dev allocation failed\n");
1912 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001913 }
1914
Jarod Wilson21677cf2010-04-16 18:29:02 -03001915 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001916 "iMON Panel, Knob and Mouse(%04x:%04x)",
1917 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001918 idev->name = ictx->name_idev;
1919
1920 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1921 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001922 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001923 idev->phys = ictx->phys_idev;
1924
Jarod Wilsondb190fc2010-04-30 16:06:12 -03001925 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001926
1927 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1928 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1929 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1930 BIT_MASK(REL_WHEEL);
1931
1932 /* panel and/or knob code support */
1933 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1934 u32 kc = imon_panel_key_table[i].keycode;
1935 __set_bit(kc, idev->keybit);
1936 }
1937
Jarod Wilson21677cf2010-04-16 18:29:02 -03001938 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1939 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001940 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001941
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001942 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001943 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001944 dev_err(ictx->dev, "input dev register failed\n");
1945 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001946 }
1947
1948 return idev;
1949
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001950out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001951 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001952 return NULL;
1953}
1954
1955static struct input_dev *imon_init_touch(struct imon_context *ictx)
1956{
1957 struct input_dev *touch;
1958 int ret;
1959
1960 touch = input_allocate_device();
1961 if (!touch) {
1962 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1963 goto touch_alloc_failed;
1964 }
1965
1966 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1967 "iMON USB Touchscreen (%04x:%04x)",
1968 ictx->vendor, ictx->product);
1969 touch->name = ictx->name_touch;
1970
1971 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1972 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001973 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001974 touch->phys = ictx->phys_touch;
1975
1976 touch->evbit[0] =
1977 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1978 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1979 BIT_MASK(BTN_TOUCH);
1980 input_set_abs_params(touch, ABS_X,
1981 0x00, 0xfff, 0, 0);
1982 input_set_abs_params(touch, ABS_Y,
1983 0x00, 0xfff, 0, 0);
1984
1985 input_set_drvdata(touch, ictx);
1986
1987 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1988 touch->dev.parent = ictx->dev;
1989 ret = input_register_device(touch);
1990 if (ret < 0) {
1991 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1992 goto touch_register_failed;
1993 }
1994
1995 return touch;
1996
1997touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03001998 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001999
2000touch_alloc_failed:
2001 return NULL;
2002}
2003
2004static bool imon_find_endpoints(struct imon_context *ictx,
2005 struct usb_host_interface *iface_desc)
2006{
2007 struct usb_endpoint_descriptor *ep;
2008 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2009 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2010 int ifnum = iface_desc->desc.bInterfaceNumber;
2011 int num_endpts = iface_desc->desc.bNumEndpoints;
2012 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002013 bool ir_ep_found = false;
2014 bool display_ep_found = false;
2015 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002016
2017 /*
2018 * Scan the endpoint list and set:
2019 * first input endpoint = IR endpoint
2020 * first output endpoint = display endpoint
2021 */
2022 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2023 ep = &iface_desc->endpoint[i].desc;
2024 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2025 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2026
2027 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2028 ep_type == USB_ENDPOINT_XFER_INT) {
2029
2030 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002031 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002032 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2033
2034 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2035 ep_type == USB_ENDPOINT_XFER_INT) {
2036 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002037 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002038 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2039 }
2040 }
2041
2042 if (ifnum == 0) {
2043 ictx->rx_endpoint_intf0 = rx_endpoint;
2044 /*
2045 * tx is used to send characters to lcd/vfd, associate RF
2046 * remotes, set IR protocol, and maybe more...
2047 */
2048 ictx->tx_endpoint = tx_endpoint;
2049 } else {
2050 ictx->rx_endpoint_intf1 = rx_endpoint;
2051 }
2052
2053 /*
2054 * If we didn't find a display endpoint, this is probably one of the
2055 * newer iMON devices that use control urb instead of interrupt
2056 */
2057 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002058 tx_control = true;
2059 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002060 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2061 "interface OUT endpoint\n", __func__);
2062 }
2063
2064 /*
2065 * Some iMON receivers have no display. Unfortunately, it seems
2066 * that SoundGraph recycles device IDs between devices both with
2067 * and without... :\
2068 */
2069 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002070 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002071 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2072 }
2073
2074 /*
2075 * iMON Touch devices have a VGA touchscreen, but no "display", as
2076 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2077 */
2078 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002079 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002080 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2081 }
2082
2083 /* Input endpoint is mandatory */
2084 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002085 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002086
2087 ictx->tx_control = tx_control;
2088
2089 if (display_ep_found)
2090 ictx->display_supported = true;
2091
2092 return ir_ep_found;
2093
2094}
2095
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002096static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2097 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002098{
2099 struct imon_context *ictx;
2100 struct urb *rx_urb;
2101 struct urb *tx_urb;
2102 struct device *dev = &intf->dev;
2103 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002104 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002105
2106 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2107 if (!ictx) {
2108 dev_err(dev, "%s: kzalloc failed for context", __func__);
2109 goto exit;
2110 }
2111 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2112 if (!rx_urb) {
2113 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2114 goto rx_urb_alloc_failed;
2115 }
2116 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2117 if (!tx_urb) {
2118 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2119 __func__);
2120 goto tx_urb_alloc_failed;
2121 }
2122
2123 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002124 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002125
2126 mutex_lock(&ictx->lock);
2127
2128 ictx->dev = dev;
2129 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002130 ictx->rx_urb_intf0 = rx_urb;
2131 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002132 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002133
2134 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2135 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2136
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002137 /* default send_packet delay is 5ms but some devices need more */
2138 ictx->send_packet_delay = id->driver_info & IMON_NEED_20MS_PKT_DELAY ?
2139 20 : 5;
2140
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002141 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002142 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002143 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002144 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002145 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002146
Jarod Wilson21677cf2010-04-16 18:29:02 -03002147 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2148 usb_rcvintpipe(ictx->usbdev_intf0,
2149 ictx->rx_endpoint_intf0->bEndpointAddress),
2150 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2151 usb_rx_callback_intf0, ictx,
2152 ictx->rx_endpoint_intf0->bInterval);
2153
2154 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2155 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002156 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002157 goto urb_submit_failed;
2158 }
2159
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002160 ictx->idev = imon_init_idev(ictx);
2161 if (!ictx->idev) {
2162 dev_err(dev, "%s: input device setup failed\n", __func__);
2163 goto idev_setup_failed;
2164 }
2165
2166 ictx->rdev = imon_init_rdev(ictx);
2167 if (!ictx->rdev) {
2168 dev_err(dev, "%s: rc device setup failed\n", __func__);
2169 goto rdev_setup_failed;
2170 }
2171
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002172 ictx->dev_present_intf0 = true;
2173
Jarod Wilson23ef7102011-04-27 19:01:44 -03002174 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002175 return ictx;
2176
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002177rdev_setup_failed:
2178 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002179idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002180 usb_kill_urb(ictx->rx_urb_intf0);
2181urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002182find_endpoint_failed:
2183 mutex_unlock(&ictx->lock);
2184 usb_free_urb(tx_urb);
2185tx_urb_alloc_failed:
2186 usb_free_urb(rx_urb);
2187rx_urb_alloc_failed:
2188 kfree(ictx);
2189exit:
2190 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2191
2192 return NULL;
2193}
2194
2195static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2196 struct imon_context *ictx)
2197{
2198 struct urb *rx_urb;
2199 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002200 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002201
2202 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2203 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002204 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002205 goto rx_urb_alloc_failed;
2206 }
2207
2208 mutex_lock(&ictx->lock);
2209
2210 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2211 init_timer(&ictx->ttimer);
2212 ictx->ttimer.data = (unsigned long)ictx;
2213 ictx->ttimer.function = imon_touch_display_timeout;
2214 }
2215
2216 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002217 ictx->rx_urb_intf1 = rx_urb;
2218
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002219 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002220 iface_desc = intf->cur_altsetting;
2221 if (!imon_find_endpoints(ictx, iface_desc))
2222 goto find_endpoint_failed;
2223
2224 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2225 ictx->touch = imon_init_touch(ictx);
2226 if (!ictx->touch)
2227 goto touch_setup_failed;
2228 } else
2229 ictx->touch = NULL;
2230
2231 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2232 usb_rcvintpipe(ictx->usbdev_intf1,
2233 ictx->rx_endpoint_intf1->bEndpointAddress),
2234 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2235 usb_rx_callback_intf1, ictx,
2236 ictx->rx_endpoint_intf1->bInterval);
2237
2238 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2239
2240 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002241 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002242 goto urb_submit_failed;
2243 }
2244
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002245 ictx->dev_present_intf1 = true;
2246
Jarod Wilson23ef7102011-04-27 19:01:44 -03002247 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002248 return ictx;
2249
2250urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002251 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002252 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002253touch_setup_failed:
2254find_endpoint_failed:
2255 mutex_unlock(&ictx->lock);
2256 usb_free_urb(rx_urb);
2257rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002258 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002259
2260 return NULL;
2261}
2262
Jarod Wilson21677cf2010-04-16 18:29:02 -03002263static void imon_init_display(struct imon_context *ictx,
2264 struct usb_interface *intf)
2265{
2266 int ret;
2267
2268 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2269
2270 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002271 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002272 if (ret)
2273 dev_err(ictx->dev, "Could not create display sysfs "
2274 "entries(%d)", ret);
2275
2276 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2277 ret = usb_register_dev(intf, &imon_lcd_class);
2278 else
2279 ret = usb_register_dev(intf, &imon_vfd_class);
2280 if (ret)
2281 /* Not a fatal error, so ignore */
2282 dev_info(ictx->dev, "could not get a minor number for "
2283 "display\n");
2284
2285}
2286
2287/**
2288 * Callback function for USB core API: Probe
2289 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002290static int imon_probe(struct usb_interface *interface,
2291 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002292{
2293 struct usb_device *usbdev = NULL;
2294 struct usb_host_interface *iface_desc = NULL;
2295 struct usb_interface *first_if;
2296 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002297 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002298 int ret = 0;
2299 struct imon_context *ictx = NULL;
2300 struct imon_context *first_if_ctx = NULL;
2301 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002302
Jarod Wilson21677cf2010-04-16 18:29:02 -03002303 usbdev = usb_get_dev(interface_to_usbdev(interface));
2304 iface_desc = interface->cur_altsetting;
2305 ifnum = iface_desc->desc.bInterfaceNumber;
2306 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2307 product = le16_to_cpu(usbdev->descriptor.idProduct);
2308
2309 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2310 __func__, vendor, product, ifnum);
2311
2312 /* prevent races probing devices w/multiple interfaces */
2313 mutex_lock(&driver_lock);
2314
2315 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002316 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002317
2318 if (ifnum == 0) {
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002319 ictx = imon_init_intf0(interface, id);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002320 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002321 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002322 ret = -ENODEV;
2323 goto fail;
2324 }
2325
Jarod Wilson21677cf2010-04-16 18:29:02 -03002326 } else {
2327 /* this is the secondary interface on the device */
2328 ictx = imon_init_intf1(interface, first_if_ctx);
2329 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002330 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002331 ret = -ENODEV;
2332 goto fail;
2333 }
2334
2335 }
2336
2337 usb_set_intfdata(interface, ictx);
2338
2339 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002340 mutex_lock(&ictx->lock);
2341
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002342 if (product == 0xffdc && ictx->rf_device) {
2343 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002344 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002345 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002346 pr_err("Could not create RF sysfs entries(%d)\n",
2347 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002348 }
2349
Jarod Wilson21677cf2010-04-16 18:29:02 -03002350 if (ictx->display_supported)
2351 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002352
2353 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002354 }
2355
Jarod Wilson21677cf2010-04-16 18:29:02 -03002356 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2357 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2358 usbdev->bus->busnum, usbdev->devnum);
2359
Jarod Wilson21677cf2010-04-16 18:29:02 -03002360 mutex_unlock(&driver_lock);
2361
2362 return 0;
2363
2364fail:
2365 mutex_unlock(&driver_lock);
2366 dev_err(dev, "unable to register, err %d\n", ret);
2367
2368 return ret;
2369}
2370
2371/**
2372 * Callback function for USB core API: disconnect
2373 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002374static void imon_disconnect(struct usb_interface *interface)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002375{
2376 struct imon_context *ictx;
2377 struct device *dev;
2378 int ifnum;
2379
2380 /* prevent races with multi-interface device probing and display_open */
2381 mutex_lock(&driver_lock);
2382
2383 ictx = usb_get_intfdata(interface);
2384 dev = ictx->dev;
2385 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2386
Jarod Wilson21677cf2010-04-16 18:29:02 -03002387 /*
2388 * sysfs_remove_group is safe to call even if sysfs_create_group
2389 * hasn't been called
2390 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002391 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2392 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002393
2394 usb_set_intfdata(interface, NULL);
2395
2396 /* Abort ongoing write */
2397 if (ictx->tx.busy) {
2398 usb_kill_urb(ictx->tx_urb);
2399 complete_all(&ictx->tx.finished);
2400 }
2401
2402 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002403 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002404 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002405 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002406 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002407 if (ictx->display_supported) {
2408 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2409 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002410 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002411 usb_deregister_dev(interface, &imon_vfd_class);
2412 }
2413 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002414 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002415 usb_kill_urb(ictx->rx_urb_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002416 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002417 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002418 del_timer_sync(&ictx->ttimer);
2419 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002420 }
2421
Jarod Wilson76a2d212011-04-28 18:20:58 -03002422 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2423 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002424
2425 mutex_unlock(&driver_lock);
2426
2427 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2428 __func__, ifnum);
2429}
2430
2431static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2432{
2433 struct imon_context *ictx = usb_get_intfdata(intf);
2434 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2435
2436 if (ifnum == 0)
2437 usb_kill_urb(ictx->rx_urb_intf0);
2438 else
2439 usb_kill_urb(ictx->rx_urb_intf1);
2440
2441 return 0;
2442}
2443
2444static int imon_resume(struct usb_interface *intf)
2445{
2446 int rc = 0;
2447 struct imon_context *ictx = usb_get_intfdata(intf);
2448 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2449
2450 if (ifnum == 0) {
2451 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2452 usb_rcvintpipe(ictx->usbdev_intf0,
2453 ictx->rx_endpoint_intf0->bEndpointAddress),
2454 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2455 usb_rx_callback_intf0, ictx,
2456 ictx->rx_endpoint_intf0->bInterval);
2457
2458 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2459
2460 } else {
2461 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2462 usb_rcvintpipe(ictx->usbdev_intf1,
2463 ictx->rx_endpoint_intf1->bEndpointAddress),
2464 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2465 usb_rx_callback_intf1, ictx,
2466 ictx->rx_endpoint_intf1->bInterval);
2467
2468 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2469 }
2470
2471 return rc;
2472}
2473
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08002474module_usb_driver(imon_driver);