blob: 72e3fa652481671cff04e2c06a1478d21fb0ffb6 [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);
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300531 if (retval) {
532 usb_kill_urb(ictx->tx_urb);
Jarod Wilson47a09b02011-07-14 14:20:46 -0300533 pr_err_ratelimited("task interrupted\n");
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300534 }
Jarod Wilson21677cf2010-04-16 18:29:02 -0300535 mutex_lock(&ictx->lock);
536
537 retval = ictx->tx.status;
538 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300539 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300540 }
541
542 kfree(control_req);
543
544 /*
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300545 * Induce a mandatory delay before returning, as otherwise,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300546 * send_packet can get called so rapidly as to overwhelm the device,
547 * particularly on faster systems and/or those with quirky usb.
548 */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300549 timeout = msecs_to_jiffies(ictx->send_packet_delay);
550 set_current_state(TASK_INTERRUPTIBLE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300551 schedule_timeout(timeout);
552
553 return retval;
554}
555
556/**
557 * Sends an associate packet to the iMON 2.4G.
558 *
559 * This might not be such a good idea, since it has an id collision with
560 * some versions of the "IR & VFD" combo. The only way to determine if it
561 * is an RF version is to look at the product description string. (Which
562 * we currently do not fetch).
563 */
564static int send_associate_24g(struct imon_context *ictx)
565{
566 int retval;
567 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
568 0x00, 0x00, 0x00, 0x20 };
569
570 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300571 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300572 return -ENODEV;
573 }
574
575 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300576 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300577 return -ENODEV;
578 }
579
580 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
581 retval = send_packet(ictx);
582
583 return retval;
584}
585
586/**
587 * Sends packets to setup and show clock on iMON display
588 *
589 * Arguments: year - last 2 digits of year, month - 1..12,
590 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
591 * hour - 0..23, minute - 0..59, second - 0..59
592 */
593static int send_set_imon_clock(struct imon_context *ictx,
594 unsigned int year, unsigned int month,
595 unsigned int day, unsigned int dow,
596 unsigned int hour, unsigned int minute,
597 unsigned int second)
598{
599 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
600 int retval = 0;
601 int i;
602
603 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300604 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300605 return -ENODEV;
606 }
607
608 switch (ictx->display_type) {
609 case IMON_DISPLAY_TYPE_LCD:
610 clock_enable_pkt[0][0] = 0x80;
611 clock_enable_pkt[0][1] = year;
612 clock_enable_pkt[0][2] = month-1;
613 clock_enable_pkt[0][3] = day;
614 clock_enable_pkt[0][4] = hour;
615 clock_enable_pkt[0][5] = minute;
616 clock_enable_pkt[0][6] = second;
617
618 clock_enable_pkt[1][0] = 0x80;
619 clock_enable_pkt[1][1] = 0;
620 clock_enable_pkt[1][2] = 0;
621 clock_enable_pkt[1][3] = 0;
622 clock_enable_pkt[1][4] = 0;
623 clock_enable_pkt[1][5] = 0;
624 clock_enable_pkt[1][6] = 0;
625
626 if (ictx->product == 0xffdc) {
627 clock_enable_pkt[0][7] = 0x50;
628 clock_enable_pkt[1][7] = 0x51;
629 } else {
630 clock_enable_pkt[0][7] = 0x88;
631 clock_enable_pkt[1][7] = 0x8a;
632 }
633
634 break;
635
636 case IMON_DISPLAY_TYPE_VFD:
637 clock_enable_pkt[0][0] = year;
638 clock_enable_pkt[0][1] = month-1;
639 clock_enable_pkt[0][2] = day;
640 clock_enable_pkt[0][3] = dow;
641 clock_enable_pkt[0][4] = hour;
642 clock_enable_pkt[0][5] = minute;
643 clock_enable_pkt[0][6] = second;
644 clock_enable_pkt[0][7] = 0x40;
645
646 clock_enable_pkt[1][0] = 0;
647 clock_enable_pkt[1][1] = 0;
648 clock_enable_pkt[1][2] = 1;
649 clock_enable_pkt[1][3] = 0;
650 clock_enable_pkt[1][4] = 0;
651 clock_enable_pkt[1][5] = 0;
652 clock_enable_pkt[1][6] = 0;
653 clock_enable_pkt[1][7] = 0x42;
654
655 break;
656
657 default:
658 return -ENODEV;
659 }
660
661 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
662 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
663 retval = send_packet(ictx);
664 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300665 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300666 break;
667 }
668 }
669
670 return retval;
671}
672
673/**
674 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
675 */
676static ssize_t show_associate_remote(struct device *d,
677 struct device_attribute *attr,
678 char *buf)
679{
680 struct imon_context *ictx = dev_get_drvdata(d);
681
682 if (!ictx)
683 return -ENODEV;
684
685 mutex_lock(&ictx->lock);
686 if (ictx->rf_isassociating)
687 strcpy(buf, "associating\n");
688 else
689 strcpy(buf, "closed\n");
690
691 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
692 "instructions on how to associate your iMON 2.4G DT/LT "
693 "remote\n");
694 mutex_unlock(&ictx->lock);
695 return strlen(buf);
696}
697
698static ssize_t store_associate_remote(struct device *d,
699 struct device_attribute *attr,
700 const char *buf, size_t count)
701{
702 struct imon_context *ictx;
703
704 ictx = dev_get_drvdata(d);
705
706 if (!ictx)
707 return -ENODEV;
708
709 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300710 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300711 send_associate_24g(ictx);
712 mutex_unlock(&ictx->lock);
713
714 return count;
715}
716
717/**
718 * sysfs functions to control internal imon clock
719 */
720static ssize_t show_imon_clock(struct device *d,
721 struct device_attribute *attr, char *buf)
722{
723 struct imon_context *ictx = dev_get_drvdata(d);
724 size_t len;
725
726 if (!ictx)
727 return -ENODEV;
728
729 mutex_lock(&ictx->lock);
730
731 if (!ictx->display_supported) {
732 len = snprintf(buf, PAGE_SIZE, "Not supported.");
733 } else {
734 len = snprintf(buf, PAGE_SIZE,
735 "To set the clock on your iMON display:\n"
736 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
737 "%s", ictx->display_isopen ?
738 "\nNOTE: imon device must be closed\n" : "");
739 }
740
741 mutex_unlock(&ictx->lock);
742
743 return len;
744}
745
746static ssize_t store_imon_clock(struct device *d,
747 struct device_attribute *attr,
748 const char *buf, size_t count)
749{
750 struct imon_context *ictx = dev_get_drvdata(d);
751 ssize_t retval;
752 unsigned int year, month, day, dow, hour, minute, second;
753
754 if (!ictx)
755 return -ENODEV;
756
757 mutex_lock(&ictx->lock);
758
759 if (!ictx->display_supported) {
760 retval = -ENODEV;
761 goto exit;
762 } else if (ictx->display_isopen) {
763 retval = -EBUSY;
764 goto exit;
765 }
766
767 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
768 &hour, &minute, &second) != 7) {
769 retval = -EINVAL;
770 goto exit;
771 }
772
773 if ((month < 1 || month > 12) ||
774 (day < 1 || day > 31) || (dow > 6) ||
775 (hour > 23) || (minute > 59) || (second > 59)) {
776 retval = -EINVAL;
777 goto exit;
778 }
779
780 retval = send_set_imon_clock(ictx, year, month, day, dow,
781 hour, minute, second);
782 if (retval)
783 goto exit;
784
785 retval = count;
786exit:
787 mutex_unlock(&ictx->lock);
788
789 return retval;
790}
791
792
793static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
794 store_imon_clock);
795
796static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
797 store_associate_remote);
798
799static struct attribute *imon_display_sysfs_entries[] = {
800 &dev_attr_imon_clock.attr,
801 NULL
802};
803
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300804static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300805 .attrs = imon_display_sysfs_entries
806};
807
808static struct attribute *imon_rf_sysfs_entries[] = {
809 &dev_attr_associate_remote.attr,
810 NULL
811};
812
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300813static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300814 .attrs = imon_rf_sysfs_entries
815};
816
817/**
818 * Writes data to the VFD. The iMON VFD is 2x16 characters
819 * and requires data in 5 consecutive USB interrupt packets,
820 * each packet but the last carrying 7 bytes.
821 *
822 * I don't know if the VFD board supports features such as
823 * scrolling, clearing rows, blanking, etc. so at
824 * the caller must provide a full screen of data. If fewer
825 * than 32 bytes are provided spaces will be appended to
826 * generate a full screen.
827 */
828static ssize_t vfd_write(struct file *file, const char *buf,
829 size_t n_bytes, loff_t *pos)
830{
831 int i;
832 int offset;
833 int seq;
834 int retval = 0;
835 struct imon_context *ictx;
836 const unsigned char vfd_packet6[] = {
837 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
838
Joe Perchesabf84382010-07-12 17:50:03 -0300839 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300840 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300841 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300842 return -ENODEV;
843 }
844
845 mutex_lock(&ictx->lock);
846
847 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300848 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300849 retval = -ENODEV;
850 goto exit;
851 }
852
853 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300854 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300855 retval = -EINVAL;
856 goto exit;
857 }
858
859 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
860 retval = -EFAULT;
861 goto exit;
862 }
863
864 /* Pad with spaces */
865 for (i = n_bytes; i < 32; ++i)
866 ictx->tx.data_buf[i] = ' ';
867
868 for (i = 32; i < 35; ++i)
869 ictx->tx.data_buf[i] = 0xFF;
870
871 offset = 0;
872 seq = 0;
873
874 do {
875 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
876 ictx->usb_tx_buf[7] = (unsigned char) seq;
877
878 retval = send_packet(ictx);
879 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300880 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300881 goto exit;
882 } else {
883 seq += 2;
884 offset += 7;
885 }
886
887 } while (offset < 35);
888
889 /* Send packet #6 */
890 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
891 ictx->usb_tx_buf[7] = (unsigned char) seq;
892 retval = send_packet(ictx);
893 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300894 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300895
896exit:
897 mutex_unlock(&ictx->lock);
898
899 return (!retval) ? n_bytes : retval;
900}
901
902/**
903 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
904 * packets. We accept data as 16 hexadecimal digits, followed by a
905 * newline (to make it easy to drive the device from a command-line
906 * -- even though the actual binary data is a bit complicated).
907 *
908 * The device itself is not a "traditional" text-mode display. It's
909 * actually a 16x96 pixel bitmap display. That means if you want to
910 * display text, you've got to have your own "font" and translate the
911 * text into bitmaps for display. This is really flexible (you can
912 * display whatever diacritics you need, and so on), but it's also
913 * a lot more complicated than most LCDs...
914 */
915static ssize_t lcd_write(struct file *file, const char *buf,
916 size_t n_bytes, loff_t *pos)
917{
918 int retval = 0;
919 struct imon_context *ictx;
920
Joe Perchesabf84382010-07-12 17:50:03 -0300921 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300922 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300923 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300924 return -ENODEV;
925 }
926
927 mutex_lock(&ictx->lock);
928
929 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300930 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300931 retval = -ENODEV;
932 goto exit;
933 }
934
935 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300936 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
937 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300938 retval = -EINVAL;
939 goto exit;
940 }
941
942 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
943 retval = -EFAULT;
944 goto exit;
945 }
946
947 retval = send_packet(ictx);
948 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300949 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300950 goto exit;
951 } else {
952 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
953 __func__, (int) n_bytes);
954 }
955exit:
956 mutex_unlock(&ictx->lock);
957 return (!retval) ? n_bytes : retval;
958}
959
960/**
961 * Callback function for USB core API: transmit data
962 */
963static void usb_tx_callback(struct urb *urb)
964{
965 struct imon_context *ictx;
966
967 if (!urb)
968 return;
969 ictx = (struct imon_context *)urb->context;
970 if (!ictx)
971 return;
972
973 ictx->tx.status = urb->status;
974
975 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300976 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300977 smp_rmb(); /* ensure later readers know we're not busy */
978 complete(&ictx->tx.finished);
979}
980
981/**
Jarod Wilson21677cf2010-04-16 18:29:02 -0300982 * report touchscreen input
983 */
984static void imon_touch_display_timeout(unsigned long data)
985{
986 struct imon_context *ictx = (struct imon_context *)data;
987
Dan Carpenterf03900d2010-05-04 08:37:33 -0300988 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300989 return;
990
991 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
992 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
993 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
994 input_sync(ictx->touch);
995}
996
997/**
998 * iMON IR receivers support two different signal sets -- those used by
999 * the iMON remotes, and those used by the Windows MCE remotes (which is
1000 * really just RC-6), but only one or the other at a time, as the signals
1001 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -03001002 *
1003 * This function gets called two different ways, one way is from
1004 * rc_register_device, for initial protocol selection/setup, and the other is
1005 * via a userspace-initiated protocol change request, either by direct sysfs
1006 * prodding or by something like ir-keytable. In the rc_register_device case,
1007 * the imon context lock is already held, but when initiated from userspace,
1008 * it is not, so we must acquire it prior to calling send_packet, which
1009 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001010 */
David Härdemanc003ab12012-10-11 19:11:54 -03001011static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001012{
1013 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001014 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001015 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001016 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001017 unsigned char ir_proto_packet[] = {
1018 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1019
David Härdemanc003ab12012-10-11 19:11:54 -03001020 if (*rc_type && !(*rc_type & rc->allowed_protos))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001021 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1022 "this device does not support\n");
1023
David Härdemanc003ab12012-10-11 19:11:54 -03001024 if (*rc_type & RC_BIT_RC6_MCE) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001025 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1026 ir_proto_packet[0] = 0x01;
David Härdemanc003ab12012-10-11 19:11:54 -03001027 *rc_type = RC_BIT_RC6_MCE;
1028 } else if (*rc_type & RC_BIT_OTHER) {
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001029 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001030 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001031 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001032 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001033 *rc_type = RC_BIT_OTHER;
1034 } else {
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001035 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001036 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001037 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001038 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001039 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001040 *rc_type = RC_BIT_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001041 }
1042
1043 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1044
Jarod Wilson23ef7102011-04-27 19:01:44 -03001045 if (!mutex_is_locked(&ictx->lock)) {
1046 unlock = true;
1047 mutex_lock(&ictx->lock);
1048 }
1049
Jarod Wilson21677cf2010-04-16 18:29:02 -03001050 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001051 if (retval)
1052 goto out;
1053
David Härdemanc003ab12012-10-11 19:11:54 -03001054 ictx->rc_type = *rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001055 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001056
1057out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001058 if (unlock)
1059 mutex_unlock(&ictx->lock);
1060
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001061 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001062}
1063
1064static inline int tv2int(const struct timeval *a, const struct timeval *b)
1065{
1066 int usecs = 0;
1067 int sec = 0;
1068
1069 if (b->tv_usec > a->tv_usec) {
1070 usecs = 1000000;
1071 sec--;
1072 }
1073
1074 usecs += a->tv_usec - b->tv_usec;
1075
1076 sec += a->tv_sec - b->tv_sec;
1077 sec *= 1000;
1078 usecs /= 1000;
1079 sec += usecs;
1080
1081 if (sec < 0)
1082 sec = 1000;
1083
1084 return sec;
1085}
1086
1087/**
1088 * The directional pad behaves a bit differently, depending on whether this is
1089 * one of the older ffdc devices or a newer device. Newer devices appear to
1090 * have a higher resolution matrix for more precise mouse movement, but it
1091 * makes things overly sensitive in keyboard mode, so we do some interesting
1092 * contortions to make it less touchy. Older devices run through the same
1093 * routine with shorter timeout and a smaller threshold.
1094 */
1095static int stabilize(int a, int b, u16 timeout, u16 threshold)
1096{
1097 struct timeval ct;
1098 static struct timeval prev_time = {0, 0};
1099 static struct timeval hit_time = {0, 0};
1100 static int x, y, prev_result, hits;
1101 int result = 0;
1102 int msec, msec_hit;
1103
1104 do_gettimeofday(&ct);
1105 msec = tv2int(&ct, &prev_time);
1106 msec_hit = tv2int(&ct, &hit_time);
1107
1108 if (msec > 100) {
1109 x = 0;
1110 y = 0;
1111 hits = 0;
1112 }
1113
1114 x += a;
1115 y += b;
1116
1117 prev_time = ct;
1118
1119 if (abs(x) > threshold || abs(y) > threshold) {
1120 if (abs(y) > abs(x))
1121 result = (y > 0) ? 0x7F : 0x80;
1122 else
1123 result = (x > 0) ? 0x7F00 : 0x8000;
1124
1125 x = 0;
1126 y = 0;
1127
1128 if (result == prev_result) {
1129 hits++;
1130
1131 if (hits > 3) {
1132 switch (result) {
1133 case 0x7F:
1134 y = 17 * threshold / 30;
1135 break;
1136 case 0x80:
1137 y -= 17 * threshold / 30;
1138 break;
1139 case 0x7F00:
1140 x = 17 * threshold / 30;
1141 break;
1142 case 0x8000:
1143 x -= 17 * threshold / 30;
1144 break;
1145 }
1146 }
1147
1148 if (hits == 2 && msec_hit < timeout) {
1149 result = 0;
1150 hits = 1;
1151 }
1152 } else {
1153 prev_result = result;
1154 hits = 1;
1155 hit_time = ct;
1156 }
1157 }
1158
1159 return result;
1160}
1161
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001162static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001163{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001164 u32 keycode;
1165 u32 release;
1166 bool is_release_code = false;
1167
1168 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001169 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001170 ictx->rc_toggle = 0x0;
1171 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001172
1173 /* Look for the release of a button */
1174 if (keycode == KEY_RESERVED) {
1175 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001176 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001177 if (keycode != KEY_RESERVED)
1178 is_release_code = true;
1179 }
1180
1181 ictx->release_code = is_release_code;
1182
1183 return keycode;
1184}
1185
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001186static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001187{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001188 u32 keycode;
1189
1190#define MCE_KEY_MASK 0x7000
1191#define MCE_TOGGLE_BIT 0x8000
1192
1193 /*
1194 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1195 * (the toggle bit flipping between alternating key presses), while
1196 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1197 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1198 * but we can't or them into all codes, as some keys are decoded in
1199 * a different way w/o the same use of the toggle bit...
1200 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001201 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001202 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1203
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001204 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001205 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001206
1207 /* not used in mce mode, but make sure we know its false */
1208 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001209
1210 return keycode;
1211}
1212
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001213static u32 imon_panel_key_lookup(u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001214{
1215 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001216 u32 keycode = KEY_RESERVED;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001217
Jarod Wilson083e4722010-05-04 16:17:05 -03001218 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1219 if (imon_panel_key_table[i].hw_code == (code | 0xffee)) {
1220 keycode = imon_panel_key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001221 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001222 }
1223 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001224
1225 return keycode;
1226}
1227
1228static bool imon_mouse_event(struct imon_context *ictx,
1229 unsigned char *buf, int len)
1230{
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001231 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001232 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001233 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001234 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001235 unsigned long flags;
1236
1237 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001238
1239 /* newer iMON device PAD or mouse button */
1240 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1241 rel_x = buf[2];
1242 rel_y = buf[3];
1243 right_shift = 1;
1244 /* 0xffdc iMON PAD or mouse button input */
1245 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1246 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1247 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1248 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1249 if (buf[0] & 0x02)
1250 rel_x |= ~0x0f;
1251 rel_x = rel_x + rel_x / 2;
1252 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1253 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1254 if (buf[0] & 0x01)
1255 rel_y |= ~0x0f;
1256 rel_y = rel_y + rel_y / 2;
1257 right_shift = 2;
1258 /* some ffdc devices decode mouse buttons differently... */
1259 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1260 right_shift = 2;
1261 /* ch+/- buttons, which we use for an emulated scroll wheel */
1262 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1263 dir = 1;
1264 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1265 dir = -1;
1266 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001267 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001268
Jarod Wilson693508d2010-09-15 15:56:03 -03001269 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1270
Jarod Wilson21677cf2010-04-16 18:29:02 -03001271 if (mouse_input) {
1272 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1273
1274 if (dir) {
1275 input_report_rel(ictx->idev, REL_WHEEL, dir);
1276 } else if (rel_x || rel_y) {
1277 input_report_rel(ictx->idev, REL_X, rel_x);
1278 input_report_rel(ictx->idev, REL_Y, rel_y);
1279 } else {
1280 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1281 input_report_key(ictx->idev, BTN_RIGHT,
1282 buf[1] >> right_shift & 0x1);
1283 }
1284 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001285 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001286 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001287 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001288 }
1289
1290 return mouse_input;
1291}
1292
1293static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1294{
1295 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1296 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1297 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1298 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1299 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1300 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1301 input_sync(ictx->touch);
1302}
1303
1304static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1305{
1306 int dir = 0;
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001307 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001308 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001309 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001310 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001311
1312 /*
1313 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1314 * contain a position coordinate (x,y), with each component ranging
1315 * from -14 to 14. We want to down-sample this to only 4 discrete values
1316 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001317 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001318 * ignore when they get too close.
1319 */
1320 if (ictx->product != 0xffdc) {
1321 /* first, pad to 8 bytes so it conforms with everything else */
1322 buf[5] = buf[6] = buf[7] = 0;
1323 timeout = 500; /* in msecs */
1324 /* (2*threshold) x (2*threshold) square */
1325 threshold = pad_thresh ? pad_thresh : 28;
1326 rel_x = buf[2];
1327 rel_y = buf[3];
1328
David Härdemanc003ab12012-10-11 19:11:54 -03001329 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001330 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1331 dir = stabilize((int)rel_x, (int)rel_y,
1332 timeout, threshold);
1333 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001334 spin_lock_irqsave(&ictx->kc_lock,
1335 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001336 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001337 spin_unlock_irqrestore(&ictx->kc_lock,
1338 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001339 return;
1340 }
1341 buf[2] = dir & 0xFF;
1342 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001343 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001344 }
1345 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001346 /*
1347 * Hack alert: instead of using keycodes, we have
1348 * to use hard-coded scancodes here...
1349 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001350 if (abs(rel_y) > abs(rel_x)) {
1351 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1352 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001353 if (rel_y > 0)
1354 scancode = 0x01007f00; /* KEY_DOWN */
1355 else
1356 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001357 } else {
1358 buf[2] = 0;
1359 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001360 if (rel_x > 0)
1361 scancode = 0x0100007f; /* KEY_RIGHT */
1362 else
1363 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001364 }
1365 }
1366
1367 /*
1368 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1369 * device (15c2:ffdc). The remote generates various codes from
1370 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1371 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1372 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
1373 * reversed endianess. Extract direction from buffer, rotate endianess,
1374 * adjust sign and feed the values into stabilize(). The resulting codes
1375 * will be 0x01008000, 0x01007F00, which match the newer devices.
1376 */
1377 } else {
1378 timeout = 10; /* in msecs */
1379 /* (2*threshold) x (2*threshold) square */
1380 threshold = pad_thresh ? pad_thresh : 15;
1381
1382 /* buf[1] is x */
1383 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1384 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1385 if (buf[0] & 0x02)
1386 rel_x |= ~0x10+1;
1387 /* buf[2] is y */
1388 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1389 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1390 if (buf[0] & 0x01)
1391 rel_y |= ~0x10+1;
1392
1393 buf[0] = 0x01;
1394 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1395
David Härdemanc003ab12012-10-11 19:11:54 -03001396 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001397 dir = stabilize((int)rel_x, (int)rel_y,
1398 timeout, threshold);
1399 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001400 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001401 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001402 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001403 return;
1404 }
1405 buf[2] = dir & 0xFF;
1406 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001407 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001408 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001409 /*
1410 * Hack alert: instead of using keycodes, we have
1411 * to use hard-coded scancodes here...
1412 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001413 if (abs(rel_y) > abs(rel_x)) {
1414 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1415 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001416 if (rel_y > 0)
1417 scancode = 0x01007f00; /* KEY_DOWN */
1418 else
1419 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001420 } else {
1421 buf[2] = 0;
1422 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001423 if (rel_x > 0)
1424 scancode = 0x0100007f; /* KEY_RIGHT */
1425 else
1426 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001427 }
1428 }
1429 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001430
Jarod Wilson693508d2010-09-15 15:56:03 -03001431 if (scancode) {
1432 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001433 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001434 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1435 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001436}
1437
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001438/**
1439 * figure out if these is a press or a release. We don't actually
1440 * care about repeats, as those will be auto-generated within the IR
1441 * subsystem for repeating scancodes.
1442 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001443static int imon_parse_press_type(struct imon_context *ictx,
1444 unsigned char *buf, u8 ktype)
1445{
1446 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001447 unsigned long flags;
1448
1449 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001450
1451 /* key release of 0x02XXXXXX key */
1452 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1453 ictx->kc = ictx->last_keycode;
1454
1455 /* mouse button release on (some) 0xffdc devices */
1456 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1457 buf[2] == 0x81 && buf[3] == 0xb7)
1458 ictx->kc = ictx->last_keycode;
1459
1460 /* mouse button release on (some other) 0xffdc devices */
1461 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1462 buf[2] == 0x81 && buf[3] == 0xb7)
1463 ictx->kc = ictx->last_keycode;
1464
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001465 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001466 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001467 ictx->rc_toggle = buf[2];
1468 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001469
1470 /* incoherent or irrelevant data */
1471 } else if (ictx->kc == KEY_RESERVED)
1472 press_type = -EINVAL;
1473
1474 /* key release of 0xXXXXXXb7 key */
1475 else if (ictx->release_code)
1476 press_type = 0;
1477
1478 /* this is a button press */
1479 else
1480 press_type = 1;
1481
Jarod Wilson693508d2010-09-15 15:56:03 -03001482 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1483
Jarod Wilson21677cf2010-04-16 18:29:02 -03001484 return press_type;
1485}
1486
1487/**
1488 * Process the incoming packet
1489 */
1490static void imon_incoming_packet(struct imon_context *ictx,
1491 struct urb *urb, int intf)
1492{
1493 int len = urb->actual_length;
1494 unsigned char *buf = urb->transfer_buffer;
1495 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001496 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001497 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001498 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001499 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001500 int press_type = 0;
1501 int msec;
1502 struct timeval t;
1503 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001504 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001505
Jarod Wilson21677cf2010-04-16 18:29:02 -03001506 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001507 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001508 return;
1509
1510 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001511 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001512 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001513 ktype = IMON_KEY_PANEL;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001514 kc = imon_panel_key_lookup(scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001515 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001516 scancode = be32_to_cpu(*((u32 *)buf));
David Härdemanc003ab12012-10-11 19:11:54 -03001517 if (ictx->rc_type == RC_BIT_RC6_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001518 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001519 if (buf[0] == 0x80)
1520 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001521 kc = imon_mce_key_lookup(ictx, scancode);
1522 } else {
1523 ktype = IMON_KEY_IMON;
1524 kc = imon_remote_key_lookup(ictx, scancode);
1525 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001526 }
1527
Jarod Wilson693508d2010-09-15 15:56:03 -03001528 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001529 /* keyboard/mouse mode toggle button */
1530 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1531 ictx->last_keycode = kc;
1532 if (!nomouse) {
1533 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1534 dev_dbg(dev, "toggling to %s mode\n",
1535 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001536 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001537 return;
1538 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001539 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001540 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1541 }
1542 }
1543
1544 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001545 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001546
1547 /* send touchscreen events through input subsystem if touchpad data */
1548 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1549 buf[7] == 0x86) {
1550 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001551 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001552
1553 /* look for mouse events with pad in mouse mode */
1554 } else if (ictx->pad_mouse) {
1555 if (imon_mouse_event(ictx, buf, len))
1556 return;
1557 }
1558
1559 /* Now for some special handling to convert pad input to arrow keys */
1560 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1561 ((len == 8) && (buf[0] & 0x40) &&
1562 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1563 len = 8;
1564 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001565 }
1566
1567 if (debug) {
1568 printk(KERN_INFO "intf%d decoded packet: ", intf);
1569 for (i = 0; i < len; ++i)
1570 printk("%02x ", buf[i]);
1571 printk("\n");
1572 }
1573
1574 press_type = imon_parse_press_type(ictx, buf, ktype);
1575 if (press_type < 0)
1576 goto not_input_data;
1577
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001578 if (ktype != IMON_KEY_PANEL) {
1579 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001580 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001581 else {
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001582 rc_keydown(ictx->rdev, ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001583 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001584 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001585 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001586 }
1587 return;
1588 }
1589
1590 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001591 spin_lock_irqsave(&ictx->kc_lock, flags);
1592
Jarod Wilson94215cc2011-06-04 14:14:41 -03001593 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001594 /* KEY_MUTE repeats from knob need to be suppressed */
1595 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001596 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001597 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001598 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001599 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001600 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001601 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001602 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001603 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001604
Jarod Wilson693508d2010-09-15 15:56:03 -03001605 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001606
Jarod Wilson428cc762010-10-23 16:42:20 -03001607 input_report_key(ictx->idev, kc, press_type);
1608 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001609
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001610 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001611 input_report_key(ictx->idev, kc, 0);
1612 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001613
Jarod Wilson94215cc2011-06-04 14:14:41 -03001614 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001615 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001616 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001617
1618 return;
1619
Jarod Wilson21677cf2010-04-16 18:29:02 -03001620not_input_data:
1621 if (len != 8) {
1622 dev_warn(dev, "imon %s: invalid incoming packet "
1623 "size (len = %d, intf%d)\n", __func__, len, intf);
1624 return;
1625 }
1626
1627 /* iMON 2.4G associate frame */
1628 if (buf[0] == 0x00 &&
1629 buf[2] == 0xFF && /* REFID */
1630 buf[3] == 0xFF &&
1631 buf[4] == 0xFF &&
1632 buf[5] == 0xFF && /* iMON 2.4G */
1633 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1634 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1635 dev_warn(dev, "%s: remote associated refid=%02X\n",
1636 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001637 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001638 }
1639}
1640
1641/**
1642 * Callback function for USB core API: receive data
1643 */
1644static void usb_rx_callback_intf0(struct urb *urb)
1645{
1646 struct imon_context *ictx;
1647 int intfnum = 0;
1648
1649 if (!urb)
1650 return;
1651
1652 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001653 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001654 return;
1655
Jarod Wilson8791d632012-01-26 12:04:11 -03001656 /*
1657 * if we get a callback before we're done configuring the hardware, we
1658 * can't yet process the data, as there's nowhere to send it, but we
1659 * still need to submit a new rx URB to avoid wedging the hardware
1660 */
1661 if (!ictx->dev_present_intf0)
1662 goto out;
1663
Jarod Wilson21677cf2010-04-16 18:29:02 -03001664 switch (urb->status) {
1665 case -ENOENT: /* usbcore unlink successful! */
1666 return;
1667
1668 case -ESHUTDOWN: /* transport endpoint was shut down */
1669 break;
1670
1671 case 0:
1672 imon_incoming_packet(ictx, urb, intfnum);
1673 break;
1674
1675 default:
1676 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1677 __func__, urb->status);
1678 break;
1679 }
1680
Jarod Wilson8791d632012-01-26 12:04:11 -03001681out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001682 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1683}
1684
1685static void usb_rx_callback_intf1(struct urb *urb)
1686{
1687 struct imon_context *ictx;
1688 int intfnum = 1;
1689
1690 if (!urb)
1691 return;
1692
1693 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001694 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001695 return;
1696
Jarod Wilson8791d632012-01-26 12:04:11 -03001697 /*
1698 * if we get a callback before we're done configuring the hardware, we
1699 * can't yet process the data, as there's nowhere to send it, but we
1700 * still need to submit a new rx URB to avoid wedging the hardware
1701 */
1702 if (!ictx->dev_present_intf1)
1703 goto out;
1704
Jarod Wilson21677cf2010-04-16 18:29:02 -03001705 switch (urb->status) {
1706 case -ENOENT: /* usbcore unlink successful! */
1707 return;
1708
1709 case -ESHUTDOWN: /* transport endpoint was shut down */
1710 break;
1711
1712 case 0:
1713 imon_incoming_packet(ictx, urb, intfnum);
1714 break;
1715
1716 default:
1717 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1718 __func__, urb->status);
1719 break;
1720 }
1721
Jarod Wilson8791d632012-01-26 12:04:11 -03001722out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001723 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1724}
1725
Jarod Wilson04292fc2010-09-15 00:28:41 -03001726/*
1727 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1728 * devices, and all of them constantly spew interrupts, even when there
1729 * is no actual data to report. However, byte 6 of this buffer looks like
1730 * its unique across device variants, so we're trying to key off that to
1731 * figure out which display type (if any) and what IR protocol the device
1732 * actually supports. These devices have their IR protocol hard-coded into
1733 * their firmware, they can't be changed on the fly like the newer hardware.
1734 */
1735static void imon_get_ffdc_type(struct imon_context *ictx)
1736{
1737 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1738 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
David Härdemanc003ab12012-10-11 19:11:54 -03001739 u64 allowed_protos = RC_BIT_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001740
1741 switch (ffdc_cfg_byte) {
1742 /* iMON Knob, no display, iMON IR + vol knob */
1743 case 0x21:
1744 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1745 ictx->display_supported = false;
1746 break;
1747 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1748 case 0x4e:
1749 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1750 ictx->display_supported = false;
1751 ictx->rf_device = true;
1752 break;
1753 /* iMON VFD, no IR (does have vol knob tho) */
1754 case 0x35:
1755 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1756 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1757 break;
1758 /* iMON VFD, iMON IR */
1759 case 0x24:
1760 case 0x85:
1761 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1762 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1763 break;
1764 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001765 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001766 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001767 case 0x9e:
1768 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1769 detected_display_type = IMON_DISPLAY_TYPE_VFD;
David Härdemanc003ab12012-10-11 19:11:54 -03001770 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001771 break;
1772 /* iMON LCD, MCE IR */
1773 case 0x9f:
1774 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1775 detected_display_type = IMON_DISPLAY_TYPE_LCD;
David Härdemanc003ab12012-10-11 19:11:54 -03001776 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001777 break;
1778 default:
1779 dev_info(ictx->dev, "Unknown 0xffdc device, "
1780 "defaulting to VFD and iMON IR");
1781 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Jarod Wilson372b4242011-06-20 00:07:13 -03001782 /* We don't know which one it is, allow user to set the
1783 * RC6 one from userspace if OTHER wasn't correct. */
David Härdemanc003ab12012-10-11 19:11:54 -03001784 allowed_protos |= RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001785 break;
1786 }
1787
1788 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1789
1790 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001791 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001792}
1793
1794static void imon_set_display_type(struct imon_context *ictx)
1795{
1796 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1797
1798 /*
1799 * Try to auto-detect the type of display if the user hasn't set
1800 * it by hand via the display_type modparam. Default is VFD.
1801 */
1802
1803 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1804 switch (ictx->product) {
1805 case 0xffdc:
1806 /* set in imon_get_ffdc_type() */
1807 configured_display_type = ictx->display_type;
1808 break;
1809 case 0x0034:
1810 case 0x0035:
1811 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1812 break;
1813 case 0x0038:
1814 case 0x0039:
1815 case 0x0045:
1816 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1817 break;
1818 case 0x003c:
1819 case 0x0041:
1820 case 0x0042:
1821 case 0x0043:
1822 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1823 ictx->display_supported = false;
1824 break;
1825 case 0x0036:
1826 case 0x0044:
1827 default:
1828 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1829 break;
1830 }
1831 } else {
1832 configured_display_type = display_type;
1833 if (display_type == IMON_DISPLAY_TYPE_NONE)
1834 ictx->display_supported = false;
1835 else
1836 ictx->display_supported = true;
1837 dev_info(ictx->dev, "%s: overriding display type to %d via "
1838 "modparam\n", __func__, display_type);
1839 }
1840
1841 ictx->display_type = configured_display_type;
1842}
1843
David Härdemand8b4b582010-10-29 16:08:23 -03001844static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001845{
David Härdemand8b4b582010-10-29 16:08:23 -03001846 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001847 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001848 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1849 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001850
David Härdemand8b4b582010-10-29 16:08:23 -03001851 rdev = rc_allocate_device();
1852 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001853 dev_err(ictx->dev, "remote control dev allocation failed\n");
1854 goto out;
1855 }
1856
1857 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1858 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1859 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1860 sizeof(ictx->phys_rdev));
1861 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1862
David Härdemand8b4b582010-10-29 16:08:23 -03001863 rdev->input_name = ictx->name_rdev;
1864 rdev->input_phys = ictx->phys_rdev;
1865 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001866 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001867
David Härdemand8b4b582010-10-29 16:08:23 -03001868 rdev->priv = ictx;
1869 rdev->driver_type = RC_DRIVER_SCANCODE;
David Härdemanc003ab12012-10-11 19:11:54 -03001870 rdev->allowed_protos = RC_BIT_OTHER | RC_BIT_RC6_MCE; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001871 rdev->change_protocol = imon_ir_change_protocol;
1872 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001873
Jarod Wilson04292fc2010-09-15 00:28:41 -03001874 /* Enable front-panel buttons and/or knobs */
1875 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1876 ret = send_packet(ictx);
1877 /* Not fatal, but warn about it */
1878 if (ret)
1879 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1880
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001881 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001882 imon_get_ffdc_type(ictx);
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001883 rdev->allowed_protos = ictx->rc_type;
1884 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001885
1886 imon_set_display_type(ictx);
1887
David Härdemanc003ab12012-10-11 19:11:54 -03001888 if (ictx->rc_type == RC_BIT_RC6_MCE)
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001889 rdev->map_name = RC_MAP_IMON_MCE;
1890 else
1891 rdev->map_name = RC_MAP_IMON_PAD;
1892
David Härdemand8b4b582010-10-29 16:08:23 -03001893 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001894 if (ret < 0) {
1895 dev_err(ictx->dev, "remote input dev register failed\n");
1896 goto out;
1897 }
1898
1899 return rdev;
1900
1901out:
David Härdemand8b4b582010-10-29 16:08:23 -03001902 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001903 return NULL;
1904}
1905
Jarod Wilson21677cf2010-04-16 18:29:02 -03001906static struct input_dev *imon_init_idev(struct imon_context *ictx)
1907{
1908 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001909 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001910
1911 idev = input_allocate_device();
1912 if (!idev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001913 dev_err(ictx->dev, "input dev allocation failed\n");
1914 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001915 }
1916
Jarod Wilson21677cf2010-04-16 18:29:02 -03001917 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001918 "iMON Panel, Knob and Mouse(%04x:%04x)",
1919 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001920 idev->name = ictx->name_idev;
1921
1922 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
1923 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001924 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001925 idev->phys = ictx->phys_idev;
1926
Jarod Wilsondb190fc2010-04-30 16:06:12 -03001927 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001928
1929 idev->keybit[BIT_WORD(BTN_MOUSE)] =
1930 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1931 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
1932 BIT_MASK(REL_WHEEL);
1933
1934 /* panel and/or knob code support */
1935 for (i = 0; i < ARRAY_SIZE(imon_panel_key_table); i++) {
1936 u32 kc = imon_panel_key_table[i].keycode;
1937 __set_bit(kc, idev->keybit);
1938 }
1939
Jarod Wilson21677cf2010-04-16 18:29:02 -03001940 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
1941 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001942 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001943
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001944 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001945 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001946 dev_err(ictx->dev, "input dev register failed\n");
1947 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001948 }
1949
1950 return idev;
1951
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001952out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001953 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001954 return NULL;
1955}
1956
1957static struct input_dev *imon_init_touch(struct imon_context *ictx)
1958{
1959 struct input_dev *touch;
1960 int ret;
1961
1962 touch = input_allocate_device();
1963 if (!touch) {
1964 dev_err(ictx->dev, "touchscreen input dev allocation failed\n");
1965 goto touch_alloc_failed;
1966 }
1967
1968 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
1969 "iMON USB Touchscreen (%04x:%04x)",
1970 ictx->vendor, ictx->product);
1971 touch->name = ictx->name_touch;
1972
1973 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
1974 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001975 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001976 touch->phys = ictx->phys_touch;
1977
1978 touch->evbit[0] =
1979 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1980 touch->keybit[BIT_WORD(BTN_TOUCH)] =
1981 BIT_MASK(BTN_TOUCH);
1982 input_set_abs_params(touch, ABS_X,
1983 0x00, 0xfff, 0, 0);
1984 input_set_abs_params(touch, ABS_Y,
1985 0x00, 0xfff, 0, 0);
1986
1987 input_set_drvdata(touch, ictx);
1988
1989 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
1990 touch->dev.parent = ictx->dev;
1991 ret = input_register_device(touch);
1992 if (ret < 0) {
1993 dev_info(ictx->dev, "touchscreen input dev register failed\n");
1994 goto touch_register_failed;
1995 }
1996
1997 return touch;
1998
1999touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03002000 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002001
2002touch_alloc_failed:
2003 return NULL;
2004}
2005
2006static bool imon_find_endpoints(struct imon_context *ictx,
2007 struct usb_host_interface *iface_desc)
2008{
2009 struct usb_endpoint_descriptor *ep;
2010 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2011 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2012 int ifnum = iface_desc->desc.bInterfaceNumber;
2013 int num_endpts = iface_desc->desc.bNumEndpoints;
2014 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002015 bool ir_ep_found = false;
2016 bool display_ep_found = false;
2017 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002018
2019 /*
2020 * Scan the endpoint list and set:
2021 * first input endpoint = IR endpoint
2022 * first output endpoint = display endpoint
2023 */
2024 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2025 ep = &iface_desc->endpoint[i].desc;
2026 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2027 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2028
2029 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2030 ep_type == USB_ENDPOINT_XFER_INT) {
2031
2032 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002033 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002034 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2035
2036 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2037 ep_type == USB_ENDPOINT_XFER_INT) {
2038 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002039 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002040 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2041 }
2042 }
2043
2044 if (ifnum == 0) {
2045 ictx->rx_endpoint_intf0 = rx_endpoint;
2046 /*
2047 * tx is used to send characters to lcd/vfd, associate RF
2048 * remotes, set IR protocol, and maybe more...
2049 */
2050 ictx->tx_endpoint = tx_endpoint;
2051 } else {
2052 ictx->rx_endpoint_intf1 = rx_endpoint;
2053 }
2054
2055 /*
2056 * If we didn't find a display endpoint, this is probably one of the
2057 * newer iMON devices that use control urb instead of interrupt
2058 */
2059 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002060 tx_control = true;
2061 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002062 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2063 "interface OUT endpoint\n", __func__);
2064 }
2065
2066 /*
2067 * Some iMON receivers have no display. Unfortunately, it seems
2068 * that SoundGraph recycles device IDs between devices both with
2069 * and without... :\
2070 */
2071 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002072 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002073 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2074 }
2075
2076 /*
2077 * iMON Touch devices have a VGA touchscreen, but no "display", as
2078 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2079 */
2080 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002081 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002082 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2083 }
2084
2085 /* Input endpoint is mandatory */
2086 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002087 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002088
2089 ictx->tx_control = tx_control;
2090
2091 if (display_ep_found)
2092 ictx->display_supported = true;
2093
2094 return ir_ep_found;
2095
2096}
2097
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002098static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2099 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002100{
2101 struct imon_context *ictx;
2102 struct urb *rx_urb;
2103 struct urb *tx_urb;
2104 struct device *dev = &intf->dev;
2105 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002106 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002107
2108 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2109 if (!ictx) {
2110 dev_err(dev, "%s: kzalloc failed for context", __func__);
2111 goto exit;
2112 }
2113 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2114 if (!rx_urb) {
2115 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2116 goto rx_urb_alloc_failed;
2117 }
2118 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2119 if (!tx_urb) {
2120 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2121 __func__);
2122 goto tx_urb_alloc_failed;
2123 }
2124
2125 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002126 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002127
2128 mutex_lock(&ictx->lock);
2129
2130 ictx->dev = dev;
2131 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002132 ictx->rx_urb_intf0 = rx_urb;
2133 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002134 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002135
2136 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2137 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2138
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002139 /* default send_packet delay is 5ms but some devices need more */
2140 ictx->send_packet_delay = id->driver_info & IMON_NEED_20MS_PKT_DELAY ?
2141 20 : 5;
2142
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002143 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002144 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002145 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002146 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002147 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002148
Jarod Wilson21677cf2010-04-16 18:29:02 -03002149 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2150 usb_rcvintpipe(ictx->usbdev_intf0,
2151 ictx->rx_endpoint_intf0->bEndpointAddress),
2152 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2153 usb_rx_callback_intf0, ictx,
2154 ictx->rx_endpoint_intf0->bInterval);
2155
2156 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2157 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002158 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002159 goto urb_submit_failed;
2160 }
2161
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002162 ictx->idev = imon_init_idev(ictx);
2163 if (!ictx->idev) {
2164 dev_err(dev, "%s: input device setup failed\n", __func__);
2165 goto idev_setup_failed;
2166 }
2167
2168 ictx->rdev = imon_init_rdev(ictx);
2169 if (!ictx->rdev) {
2170 dev_err(dev, "%s: rc device setup failed\n", __func__);
2171 goto rdev_setup_failed;
2172 }
2173
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002174 ictx->dev_present_intf0 = true;
2175
Jarod Wilson23ef7102011-04-27 19:01:44 -03002176 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002177 return ictx;
2178
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002179rdev_setup_failed:
2180 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002181idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002182 usb_kill_urb(ictx->rx_urb_intf0);
2183urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002184find_endpoint_failed:
2185 mutex_unlock(&ictx->lock);
2186 usb_free_urb(tx_urb);
2187tx_urb_alloc_failed:
2188 usb_free_urb(rx_urb);
2189rx_urb_alloc_failed:
2190 kfree(ictx);
2191exit:
2192 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2193
2194 return NULL;
2195}
2196
2197static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2198 struct imon_context *ictx)
2199{
2200 struct urb *rx_urb;
2201 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002202 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002203
2204 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2205 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002206 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002207 goto rx_urb_alloc_failed;
2208 }
2209
2210 mutex_lock(&ictx->lock);
2211
2212 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2213 init_timer(&ictx->ttimer);
2214 ictx->ttimer.data = (unsigned long)ictx;
2215 ictx->ttimer.function = imon_touch_display_timeout;
2216 }
2217
2218 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002219 ictx->rx_urb_intf1 = rx_urb;
2220
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002221 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002222 iface_desc = intf->cur_altsetting;
2223 if (!imon_find_endpoints(ictx, iface_desc))
2224 goto find_endpoint_failed;
2225
2226 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2227 ictx->touch = imon_init_touch(ictx);
2228 if (!ictx->touch)
2229 goto touch_setup_failed;
2230 } else
2231 ictx->touch = NULL;
2232
2233 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2234 usb_rcvintpipe(ictx->usbdev_intf1,
2235 ictx->rx_endpoint_intf1->bEndpointAddress),
2236 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2237 usb_rx_callback_intf1, ictx,
2238 ictx->rx_endpoint_intf1->bInterval);
2239
2240 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2241
2242 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002243 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002244 goto urb_submit_failed;
2245 }
2246
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002247 ictx->dev_present_intf1 = true;
2248
Jarod Wilson23ef7102011-04-27 19:01:44 -03002249 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002250 return ictx;
2251
2252urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002253 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002254 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002255touch_setup_failed:
2256find_endpoint_failed:
2257 mutex_unlock(&ictx->lock);
2258 usb_free_urb(rx_urb);
2259rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002260 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002261
2262 return NULL;
2263}
2264
Jarod Wilson21677cf2010-04-16 18:29:02 -03002265static void imon_init_display(struct imon_context *ictx,
2266 struct usb_interface *intf)
2267{
2268 int ret;
2269
2270 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2271
2272 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002273 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002274 if (ret)
2275 dev_err(ictx->dev, "Could not create display sysfs "
2276 "entries(%d)", ret);
2277
2278 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2279 ret = usb_register_dev(intf, &imon_lcd_class);
2280 else
2281 ret = usb_register_dev(intf, &imon_vfd_class);
2282 if (ret)
2283 /* Not a fatal error, so ignore */
2284 dev_info(ictx->dev, "could not get a minor number for "
2285 "display\n");
2286
2287}
2288
2289/**
2290 * Callback function for USB core API: Probe
2291 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002292static int imon_probe(struct usb_interface *interface,
2293 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002294{
2295 struct usb_device *usbdev = NULL;
2296 struct usb_host_interface *iface_desc = NULL;
2297 struct usb_interface *first_if;
2298 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002299 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002300 int ret = 0;
2301 struct imon_context *ictx = NULL;
2302 struct imon_context *first_if_ctx = NULL;
2303 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002304
Jarod Wilson21677cf2010-04-16 18:29:02 -03002305 usbdev = usb_get_dev(interface_to_usbdev(interface));
2306 iface_desc = interface->cur_altsetting;
2307 ifnum = iface_desc->desc.bInterfaceNumber;
2308 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2309 product = le16_to_cpu(usbdev->descriptor.idProduct);
2310
2311 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2312 __func__, vendor, product, ifnum);
2313
2314 /* prevent races probing devices w/multiple interfaces */
2315 mutex_lock(&driver_lock);
2316
2317 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002318 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002319
2320 if (ifnum == 0) {
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002321 ictx = imon_init_intf0(interface, id);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002322 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002323 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002324 ret = -ENODEV;
2325 goto fail;
2326 }
2327
Jarod Wilson21677cf2010-04-16 18:29:02 -03002328 } else {
Kevin Baradon7d54ba02013-04-22 16:09:45 -03002329 /* this is the secondary interface on the device */
2330
2331 /* fail early if first intf failed to register */
2332 if (!first_if_ctx) {
2333 ret = -ENODEV;
2334 goto fail;
2335 }
2336
Jarod Wilson21677cf2010-04-16 18:29:02 -03002337 ictx = imon_init_intf1(interface, first_if_ctx);
2338 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002339 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002340 ret = -ENODEV;
2341 goto fail;
2342 }
2343
2344 }
2345
2346 usb_set_intfdata(interface, ictx);
2347
2348 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002349 mutex_lock(&ictx->lock);
2350
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002351 if (product == 0xffdc && ictx->rf_device) {
2352 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002353 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002354 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002355 pr_err("Could not create RF sysfs entries(%d)\n",
2356 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002357 }
2358
Jarod Wilson21677cf2010-04-16 18:29:02 -03002359 if (ictx->display_supported)
2360 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002361
2362 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002363 }
2364
Jarod Wilson21677cf2010-04-16 18:29:02 -03002365 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2366 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2367 usbdev->bus->busnum, usbdev->devnum);
2368
Jarod Wilson21677cf2010-04-16 18:29:02 -03002369 mutex_unlock(&driver_lock);
2370
2371 return 0;
2372
2373fail:
2374 mutex_unlock(&driver_lock);
2375 dev_err(dev, "unable to register, err %d\n", ret);
2376
2377 return ret;
2378}
2379
2380/**
2381 * Callback function for USB core API: disconnect
2382 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002383static void imon_disconnect(struct usb_interface *interface)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002384{
2385 struct imon_context *ictx;
2386 struct device *dev;
2387 int ifnum;
2388
2389 /* prevent races with multi-interface device probing and display_open */
2390 mutex_lock(&driver_lock);
2391
2392 ictx = usb_get_intfdata(interface);
2393 dev = ictx->dev;
2394 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2395
Jarod Wilson21677cf2010-04-16 18:29:02 -03002396 /*
2397 * sysfs_remove_group is safe to call even if sysfs_create_group
2398 * hasn't been called
2399 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002400 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2401 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002402
2403 usb_set_intfdata(interface, NULL);
2404
2405 /* Abort ongoing write */
2406 if (ictx->tx.busy) {
2407 usb_kill_urb(ictx->tx_urb);
2408 complete_all(&ictx->tx.finished);
2409 }
2410
2411 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002412 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002413 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002414 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002415 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002416 if (ictx->display_supported) {
2417 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2418 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002419 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002420 usb_deregister_dev(interface, &imon_vfd_class);
2421 }
2422 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002423 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002424 usb_kill_urb(ictx->rx_urb_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002425 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002426 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002427 del_timer_sync(&ictx->ttimer);
2428 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002429 }
2430
Jarod Wilson76a2d212011-04-28 18:20:58 -03002431 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2432 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002433
2434 mutex_unlock(&driver_lock);
2435
2436 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2437 __func__, ifnum);
2438}
2439
2440static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2441{
2442 struct imon_context *ictx = usb_get_intfdata(intf);
2443 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2444
2445 if (ifnum == 0)
2446 usb_kill_urb(ictx->rx_urb_intf0);
2447 else
2448 usb_kill_urb(ictx->rx_urb_intf1);
2449
2450 return 0;
2451}
2452
2453static int imon_resume(struct usb_interface *intf)
2454{
2455 int rc = 0;
2456 struct imon_context *ictx = usb_get_intfdata(intf);
2457 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2458
2459 if (ifnum == 0) {
2460 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2461 usb_rcvintpipe(ictx->usbdev_intf0,
2462 ictx->rx_endpoint_intf0->bEndpointAddress),
2463 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2464 usb_rx_callback_intf0, ictx,
2465 ictx->rx_endpoint_intf0->bInterval);
2466
2467 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2468
2469 } else {
2470 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2471 usb_rcvintpipe(ictx->usbdev_intf1,
2472 ictx->rx_endpoint_intf1->bEndpointAddress),
2473 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2474 usb_rx_callback_intf1, ictx,
2475 ictx->rx_endpoint_intf1->bInterval);
2476
2477 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2478 }
2479
2480 return rc;
2481}
2482
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08002483module_usb_driver(imon_driver);