Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / drivers / media / rc / imon.c
CommitLineData
3cfa958b 1// SPDX-License-Identifier: GPL-2.0-or-later
21677cfc
JW
2/*
3 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
4 *
693508df 5 * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
21677cfc
JW
6 * Portions based on the original lirc_imon driver,
7 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
8 *
9 * Huge thanks to R. Geoff Newbury for invaluable debugging on the
10 * 0xffdc iMON devices, and for sending me one to hack on, without
11 * which the support for them wouldn't be nearly as good. Thanks
12 * also to the numerous 0xffdc device owners that tested auto-config
13 * support for me and provided debug dumps from their devices.
21677cfc
JW
14 */
15
e2302501
JP
16#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
17
21677cfc
JW
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
1edba648 21#include <linux/ktime.h>
21677cfc
JW
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/uaccess.h>
47a09b08 25#include <linux/ratelimit.h>
21677cfc
JW
26
27#include <linux/input.h>
28#include <linux/usb.h>
29#include <linux/usb/input.h>
6bda9644 30#include <media/rc-core.h>
21677cfc 31
21677cfc
JW
32#include <linux/timer.h>
33
34#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
35#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
36#define MOD_NAME "imon"
8791d63a 37#define MOD_VERSION "0.9.4"
21677cfc
JW
38
39#define DISPLAY_MINOR_BASE 144
40#define DEVICE_NAME "lcd%d"
41
42#define BUF_CHUNK_SIZE 8
43#define BUF_SIZE 128
44
45#define BIT_DURATION 250 /* each bit received is 250us */
46
47#define IMON_CLOCK_ENABLE_PACKETS 2
21677cfc
JW
48
49/*** P R O T O T Y P E S ***/
50
51/* USB Callback prototypes */
52static int imon_probe(struct usb_interface *interface,
53 const struct usb_device_id *id);
54static void imon_disconnect(struct usb_interface *interface);
55static void usb_rx_callback_intf0(struct urb *urb);
56static void usb_rx_callback_intf1(struct urb *urb);
57static void usb_tx_callback(struct urb *urb);
58
59/* suspend/resume support */
60static int imon_resume(struct usb_interface *intf);
61static int imon_suspend(struct usb_interface *intf, pm_message_t message);
62
63/* Display file_operations function prototypes */
64static int display_open(struct inode *inode, struct file *file);
65static int display_close(struct inode *inode, struct file *file);
66
67/* VFD write operation */
d6740d86 68static ssize_t vfd_write(struct file *file, const char __user *buf,
21677cfc
JW
69 size_t n_bytes, loff_t *pos);
70
71/* LCD file_operations override function prototypes */
d6740d86 72static ssize_t lcd_write(struct file *file, const char __user *buf,
21677cfc
JW
73 size_t n_bytes, loff_t *pos);
74
75/*** G L O B A L S ***/
76
0d8053f2
UE
77struct imon_panel_key_table {
78 u64 hw_code;
79 u32 keycode;
80};
81
82struct imon_usb_dev_descr {
83 __u16 flags;
84#define IMON_NO_FLAGS 0
85#define IMON_NEED_20MS_PKT_DELAY 1
cf330691 86#define IMON_SUPPRESS_REPEATED_KEYS 2
0d8053f2
UE
87 struct imon_panel_key_table key_table[];
88};
89
21677cfc
JW
90struct imon_context {
91 struct device *dev;
21677cfc
JW
92 /* 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 */
bbe4690f 98 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
21677cfc
JW
99 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];
26ccbec4 115 unsigned int send_packet_delay;
21677cfc
JW
116
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
d8b4b582 127 struct rc_dev *rdev; /* rc-core device for remote */
eaf2bcc9 128 struct input_dev *idev; /* input device for panel & IR mouse */
21677cfc
JW
129 struct input_dev *touch; /* input device for touchscreen */
130
693508df 131 spinlock_t kc_lock; /* make sure we get keycodes right */
21677cfc
JW
132 u32 kc; /* current input keycode */
133 u32 last_keycode; /* last reported input keycode */
eaf2bcc9
DH
134 u32 rc_scancode; /* the computed remote scancode */
135 u8 rc_toggle; /* the computed remote toggle bit */
6d741bfe 136 u64 rc_proto; /* iMON or MCE (RC6) IR protocol? */
21677cfc
JW
137 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
eaf2bcc9
DH
142 char name_rdev[128]; /* rc input device name */
143 char phys_rdev[64]; /* rc input device phys path */
144
21677cfc
JW
145 char name_idev[128]; /* input device name */
146 char phys_idev[64]; /* input device phys path */
21677cfc
JW
147
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 */
cf330691
FG
153 const struct imon_usb_dev_descr *dev_descr;
154 /* device description with key */
155 /* table for front panels */
db264d4c
TH
156 /*
157 * Fields for deferring free_imon_context().
158 *
159 * Since reference to "struct imon_context" is stored into
160 * "struct file"->private_data, we need to remember
161 * how many file descriptors might access this "struct imon_context".
162 */
163 refcount_t users;
164 /*
165 * Use a flag for telling display_open()/vfd_write()/lcd_write() that
166 * imon_disconnect() was already called.
167 */
168 bool disconnected;
169 /*
170 * We need to wait for RCU grace period in order to allow
171 * display_open() to safely check ->disconnected and increment ->users.
172 */
173 struct rcu_head rcu;
21677cfc
JW
174};
175
176#define TOUCH_TIMEOUT (HZ/30)
21677cfc
JW
177
178/* vfd character device file operations */
179static const struct file_operations vfd_fops = {
180 .owner = THIS_MODULE,
db264d4c
TH
181 .open = display_open,
182 .write = vfd_write,
183 .release = display_close,
6038f373 184 .llseek = noop_llseek,
21677cfc
JW
185};
186
187/* lcd character device file operations */
188static const struct file_operations lcd_fops = {
189 .owner = THIS_MODULE,
db264d4c
TH
190 .open = display_open,
191 .write = lcd_write,
192 .release = display_close,
6038f373 193 .llseek = noop_llseek,
21677cfc
JW
194};
195
196enum {
197 IMON_DISPLAY_TYPE_AUTO = 0,
198 IMON_DISPLAY_TYPE_VFD = 1,
199 IMON_DISPLAY_TYPE_LCD = 2,
200 IMON_DISPLAY_TYPE_VGA = 3,
201 IMON_DISPLAY_TYPE_NONE = 4,
202};
203
21677cfc
JW
204enum {
205 IMON_KEY_IMON = 0,
206 IMON_KEY_MCE = 1,
207 IMON_KEY_PANEL = 2,
208};
209
0d8053f2
UE
210static struct usb_class_driver imon_vfd_class = {
211 .name = DEVICE_NAME,
212 .fops = &vfd_fops,
213 .minor_base = DISPLAY_MINOR_BASE,
214};
215
216static struct usb_class_driver imon_lcd_class = {
217 .name = DEVICE_NAME,
218 .fops = &lcd_fops,
219 .minor_base = DISPLAY_MINOR_BASE,
220};
221
222/* imon receiver front panel/knob key table */
223static const struct imon_usb_dev_descr imon_default_table = {
224 .flags = IMON_NO_FLAGS,
225 .key_table = {
226 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
227 { 0x000000001200ffeell, KEY_UP },
228 { 0x000000001300ffeell, KEY_DOWN },
229 { 0x000000001400ffeell, KEY_LEFT },
230 { 0x000000001500ffeell, KEY_RIGHT },
231 { 0x000000001600ffeell, KEY_ENTER },
232 { 0x000000001700ffeell, KEY_ESC },
233 { 0x000000001f00ffeell, KEY_AUDIO },
234 { 0x000000002000ffeell, KEY_VIDEO },
235 { 0x000000002100ffeell, KEY_CAMERA },
236 { 0x000000002700ffeell, KEY_DVD },
237 { 0x000000002300ffeell, KEY_TV },
238 { 0x000000002b00ffeell, KEY_EXIT },
239 { 0x000000002c00ffeell, KEY_SELECT },
240 { 0x000000002d00ffeell, KEY_MENU },
241 { 0x000000000500ffeell, KEY_PREVIOUS },
242 { 0x000000000700ffeell, KEY_REWIND },
243 { 0x000000000400ffeell, KEY_STOP },
244 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
245 { 0x000000000800ffeell, KEY_FASTFORWARD },
246 { 0x000000000600ffeell, KEY_NEXT },
247 { 0x000000010000ffeell, KEY_RIGHT },
248 { 0x000001000000ffeell, KEY_LEFT },
249 { 0x000000003d00ffeell, KEY_SELECT },
250 { 0x000100000000ffeell, KEY_VOLUMEUP },
251 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
252 { 0x000000000100ffeell, KEY_MUTE },
253 /* 0xffdc iMON MCE VFD */
254 { 0x00010000ffffffeell, KEY_VOLUMEUP },
255 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
256 { 0x00000001ffffffeell, KEY_MUTE },
257 { 0x0000000fffffffeell, KEY_MEDIA },
258 { 0x00000012ffffffeell, KEY_UP },
259 { 0x00000013ffffffeell, KEY_DOWN },
260 { 0x00000014ffffffeell, KEY_LEFT },
261 { 0x00000015ffffffeell, KEY_RIGHT },
262 { 0x00000016ffffffeell, KEY_ENTER },
263 { 0x00000017ffffffeell, KEY_ESC },
264 /* iMON Knob values */
265 { 0x000100ffffffffeell, KEY_VOLUMEUP },
266 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
267 { 0x000008ffffffffeell, KEY_MUTE },
268 { 0, KEY_RESERVED },
269 }
270};
271
272static const struct imon_usb_dev_descr imon_OEM_VFD = {
273 .flags = IMON_NEED_20MS_PKT_DELAY,
274 .key_table = {
275 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
276 { 0x000000001200ffeell, KEY_UP },
277 { 0x000000001300ffeell, KEY_DOWN },
278 { 0x000000001400ffeell, KEY_LEFT },
279 { 0x000000001500ffeell, KEY_RIGHT },
280 { 0x000000001600ffeell, KEY_ENTER },
281 { 0x000000001700ffeell, KEY_ESC },
282 { 0x000000001f00ffeell, KEY_AUDIO },
283 { 0x000000002b00ffeell, KEY_EXIT },
284 { 0x000000002c00ffeell, KEY_SELECT },
285 { 0x000000002d00ffeell, KEY_MENU },
286 { 0x000000000500ffeell, KEY_PREVIOUS },
287 { 0x000000000700ffeell, KEY_REWIND },
288 { 0x000000000400ffeell, KEY_STOP },
289 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
290 { 0x000000000800ffeell, KEY_FASTFORWARD },
291 { 0x000000000600ffeell, KEY_NEXT },
292 { 0x000000010000ffeell, KEY_RIGHT },
293 { 0x000001000000ffeell, KEY_LEFT },
294 { 0x000000003d00ffeell, KEY_SELECT },
295 { 0x000100000000ffeell, KEY_VOLUMEUP },
296 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
297 { 0x000000000100ffeell, KEY_MUTE },
298 /* 0xffdc iMON MCE VFD */
299 { 0x00010000ffffffeell, KEY_VOLUMEUP },
300 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
301 { 0x00000001ffffffeell, KEY_MUTE },
302 { 0x0000000fffffffeell, KEY_MEDIA },
303 { 0x00000012ffffffeell, KEY_UP },
304 { 0x00000013ffffffeell, KEY_DOWN },
305 { 0x00000014ffffffeell, KEY_LEFT },
306 { 0x00000015ffffffeell, KEY_RIGHT },
307 { 0x00000016ffffffeell, KEY_ENTER },
308 { 0x00000017ffffffeell, KEY_ESC },
309 /* iMON Knob values */
310 { 0x000100ffffffffeell, KEY_VOLUMEUP },
311 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
312 { 0x000008ffffffffeell, KEY_MUTE },
313 { 0, KEY_RESERVED },
314 }
26ccbec4
KB
315};
316
7b5fc071
UE
317/* imon receiver front panel/knob key table for DH102*/
318static const struct imon_usb_dev_descr imon_DH102 = {
319 .flags = IMON_NO_FLAGS,
320 .key_table = {
321 { 0x000100000000ffeell, KEY_VOLUMEUP },
322 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
323 { 0x000000010000ffeell, KEY_MUTE },
324 { 0x0000000f0000ffeell, KEY_MEDIA },
325 { 0x000000120000ffeell, KEY_UP },
326 { 0x000000130000ffeell, KEY_DOWN },
327 { 0x000000140000ffeell, KEY_LEFT },
328 { 0x000000150000ffeell, KEY_RIGHT },
329 { 0x000000160000ffeell, KEY_ENTER },
330 { 0x000000170000ffeell, KEY_ESC },
331 { 0x0000002b0000ffeell, KEY_EXIT },
332 { 0x0000002c0000ffeell, KEY_SELECT },
333 { 0x0000002d0000ffeell, KEY_MENU },
334 { 0, KEY_RESERVED }
335 }
336};
337
cf330691
FG
338/* imon ultrabay front panel key table */
339static const struct imon_usb_dev_descr ultrabay_table = {
340 .flags = IMON_SUPPRESS_REPEATED_KEYS,
341 .key_table = {
342 { 0x0000000f0000ffeell, KEY_MEDIA }, /* Go */
343 { 0x000000000100ffeell, KEY_UP },
344 { 0x000000000001ffeell, KEY_DOWN },
345 { 0x000000160000ffeell, KEY_ENTER },
346 { 0x0000001f0000ffeell, KEY_AUDIO }, /* Music */
347 { 0x000000200000ffeell, KEY_VIDEO }, /* Movie */
348 { 0x000000210000ffeell, KEY_CAMERA }, /* Photo */
349 { 0x000000270000ffeell, KEY_DVD }, /* DVD */
350 { 0x000000230000ffeell, KEY_TV }, /* TV */
351 { 0x000000050000ffeell, KEY_PREVIOUS }, /* Previous */
352 { 0x000000070000ffeell, KEY_REWIND },
353 { 0x000000040000ffeell, KEY_STOP },
354 { 0x000000020000ffeell, KEY_PLAYPAUSE },
355 { 0x000000080000ffeell, KEY_FASTFORWARD },
356 { 0x000000060000ffeell, KEY_NEXT }, /* Next */
357 { 0x000100000000ffeell, KEY_VOLUMEUP },
358 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
359 { 0x000000010000ffeell, KEY_MUTE },
360 { 0, KEY_RESERVED },
361 }
362};
363
21677cfc
JW
364/*
365 * USB Device ID for iMON USB Control Boards
366 *
367 * The Windows drivers contain 6 different inf files, more or less one for
368 * each new device until the 0x0034-0x0046 devices, which all use the same
369 * driver. Some of the devices in the 34-46 range haven't been definitively
370 * identified yet. Early devices have either a TriGem Computer, Inc. or a
371 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
372 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
373 * the ffdc and later devices, which do onboard decoding.
374 */
5fad16b5 375static const struct usb_device_id imon_usb_id_table[] = {
21677cfc
JW
376 /*
377 * Several devices with this same device ID, all use iMON_PAD.inf
378 * SoundGraph iMON PAD (IR & VFD)
379 * SoundGraph iMON PAD (IR & LCD)
380 * SoundGraph iMON Knob (IR only)
381 */
0d8053f2
UE
382 { USB_DEVICE(0x15c2, 0xffdc),
383 .driver_info = (unsigned long)&imon_default_table },
21677cfc
JW
384
385 /*
386 * Newer devices, all driven by the latest iMON Windows driver, full
387 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
388 * Need user input to fill in details on unknown devices.
389 */
390 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
0d8053f2 391 { USB_DEVICE(0x15c2, 0x0034),
7b5fc071 392 .driver_info = (unsigned long)&imon_DH102 },
21677cfc 393 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
0d8053f2
UE
394 { USB_DEVICE(0x15c2, 0x0035),
395 .driver_info = (unsigned long)&imon_default_table},
21677cfc 396 /* SoundGraph iMON OEM VFD (IR & VFD) */
0d8053f2
UE
397 { USB_DEVICE(0x15c2, 0x0036),
398 .driver_info = (unsigned long)&imon_OEM_VFD },
21677cfc 399 /* device specifics unknown */
0d8053f2
UE
400 { USB_DEVICE(0x15c2, 0x0037),
401 .driver_info = (unsigned long)&imon_default_table},
21677cfc 402 /* SoundGraph iMON OEM LCD (IR & LCD) */
0d8053f2
UE
403 { USB_DEVICE(0x15c2, 0x0038),
404 .driver_info = (unsigned long)&imon_default_table},
21677cfc 405 /* SoundGraph iMON UltraBay (IR & LCD) */
0d8053f2
UE
406 { USB_DEVICE(0x15c2, 0x0039),
407 .driver_info = (unsigned long)&imon_default_table},
21677cfc 408 /* device specifics unknown */
0d8053f2
UE
409 { USB_DEVICE(0x15c2, 0x003a),
410 .driver_info = (unsigned long)&imon_default_table},
21677cfc 411 /* device specifics unknown */
0d8053f2
UE
412 { USB_DEVICE(0x15c2, 0x003b),
413 .driver_info = (unsigned long)&imon_default_table},
21677cfc 414 /* SoundGraph iMON OEM Inside (IR only) */
0d8053f2
UE
415 { USB_DEVICE(0x15c2, 0x003c),
416 .driver_info = (unsigned long)&imon_default_table},
21677cfc 417 /* device specifics unknown */
0d8053f2
UE
418 { USB_DEVICE(0x15c2, 0x003d),
419 .driver_info = (unsigned long)&imon_default_table},
21677cfc 420 /* device specifics unknown */
0d8053f2
UE
421 { USB_DEVICE(0x15c2, 0x003e),
422 .driver_info = (unsigned long)&imon_default_table},
21677cfc 423 /* device specifics unknown */
0d8053f2
UE
424 { USB_DEVICE(0x15c2, 0x003f),
425 .driver_info = (unsigned long)&imon_default_table},
21677cfc 426 /* device specifics unknown */
0d8053f2
UE
427 { USB_DEVICE(0x15c2, 0x0040),
428 .driver_info = (unsigned long)&imon_default_table},
21677cfc 429 /* SoundGraph iMON MINI (IR only) */
0d8053f2
UE
430 { USB_DEVICE(0x15c2, 0x0041),
431 .driver_info = (unsigned long)&imon_default_table},
21677cfc 432 /* Antec Veris Multimedia Station EZ External (IR only) */
0d8053f2
UE
433 { USB_DEVICE(0x15c2, 0x0042),
434 .driver_info = (unsigned long)&imon_default_table},
21677cfc 435 /* Antec Veris Multimedia Station Basic Internal (IR only) */
0d8053f2
UE
436 { USB_DEVICE(0x15c2, 0x0043),
437 .driver_info = (unsigned long)&imon_default_table},
21677cfc 438 /* Antec Veris Multimedia Station Elite (IR & VFD) */
0d8053f2
UE
439 { USB_DEVICE(0x15c2, 0x0044),
440 .driver_info = (unsigned long)&imon_default_table},
21677cfc 441 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
0d8053f2
UE
442 { USB_DEVICE(0x15c2, 0x0045),
443 .driver_info = (unsigned long)&imon_default_table},
21677cfc 444 /* device specifics unknown */
0d8053f2
UE
445 { USB_DEVICE(0x15c2, 0x0046),
446 .driver_info = (unsigned long)&imon_default_table},
21677cfc
JW
447 {}
448};
449
450/* USB Device data */
451static struct usb_driver imon_driver = {
452 .name = MOD_NAME,
453 .probe = imon_probe,
4c62e976 454 .disconnect = imon_disconnect,
21677cfc
JW
455 .suspend = imon_suspend,
456 .resume = imon_resume,
457 .id_table = imon_usb_id_table,
458};
459
21677cfc
JW
460/* Module bookkeeping bits */
461MODULE_AUTHOR(MOD_AUTHOR);
462MODULE_DESCRIPTION(MOD_DESC);
463MODULE_VERSION(MOD_VERSION);
464MODULE_LICENSE("GPL");
465MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
466
467static bool debug;
468module_param(debug, bool, S_IRUGO | S_IWUSR);
6aa209e4 469MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
21677cfc
JW
470
471/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
472static int display_type;
473module_param(display_type, int, S_IRUGO);
25ec587c 474MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, 1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
21677cfc 475
6718e8ad
JW
476static int pad_stabilize = 1;
477module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
25ec587c 478MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD presses in arrow key mode. 0=disable, 1=enable (default).");
21677cfc
JW
479
480/*
481 * In certain use cases, mouse mode isn't really helpful, and could actually
482 * cause confusion, so allow disabling it when the IR device is open.
483 */
484static bool nomouse;
485module_param(nomouse, bool, S_IRUGO | S_IWUSR);
25ec587c 486MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is open. 0=don't disable, 1=disable. (default: don't disable)");
21677cfc
JW
487
488/* threshold at which a pad push registers as an arrow key in kbd mode */
489static int pad_thresh;
490module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
25ec587c 491MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an arrow key in kbd mode (default: 28)");
21677cfc
JW
492
493
494static void free_imon_context(struct imon_context *ictx)
495{
496 struct device *dev = ictx->dev;
497
498 usb_free_urb(ictx->tx_urb);
db264d4c 499 WARN_ON(ictx->dev_present_intf0);
21677cfc 500 usb_free_urb(ictx->rx_urb_intf0);
db264d4c 501 WARN_ON(ictx->dev_present_intf1);
21677cfc 502 usb_free_urb(ictx->rx_urb_intf1);
db264d4c 503 kfree_rcu(ictx, rcu);
21677cfc
JW
504
505 dev_dbg(dev, "%s: iMON context freed\n", __func__);
506}
507
255940e6 508/*
21677cfc
JW
509 * Called when the Display device (e.g. /dev/lcd0)
510 * is opened by the application.
511 */
512static int display_open(struct inode *inode, struct file *file)
513{
514 struct usb_interface *interface;
515 struct imon_context *ictx = NULL;
516 int subminor;
517 int retval = 0;
518
21677cfc
JW
519 subminor = iminor(inode);
520 interface = usb_find_interface(&imon_driver, subminor);
521 if (!interface) {
e2302501 522 pr_err("could not find interface for minor %d\n", subminor);
21677cfc
JW
523 retval = -ENODEV;
524 goto exit;
525 }
21677cfc 526
db264d4c
TH
527 rcu_read_lock();
528 ictx = usb_get_intfdata(interface);
529 if (!ictx || ictx->disconnected || !refcount_inc_not_zero(&ictx->users)) {
530 rcu_read_unlock();
e2302501 531 pr_err("no context found for minor %d\n", subminor);
21677cfc
JW
532 retval = -ENODEV;
533 goto exit;
534 }
db264d4c 535 rcu_read_unlock();
21677cfc
JW
536
537 mutex_lock(&ictx->lock);
538
539 if (!ictx->display_supported) {
e2302501 540 pr_err("display not supported by device\n");
21677cfc
JW
541 retval = -ENODEV;
542 } else if (ictx->display_isopen) {
e2302501 543 pr_err("display port is already open\n");
21677cfc
JW
544 retval = -EBUSY;
545 } else {
f789bf40 546 ictx->display_isopen = true;
21677cfc
JW
547 file->private_data = ictx;
548 dev_dbg(ictx->dev, "display port opened\n");
549 }
550
551 mutex_unlock(&ictx->lock);
552
db264d4c
TH
553 if (retval && refcount_dec_and_test(&ictx->users))
554 free_imon_context(ictx);
555
21677cfc 556exit:
21677cfc
JW
557 return retval;
558}
559
255940e6 560/*
21677cfc
JW
561 * Called when the display device (e.g. /dev/lcd0)
562 * is closed by the application.
563 */
564static int display_close(struct inode *inode, struct file *file)
565{
db264d4c 566 struct imon_context *ictx = file->private_data;
21677cfc
JW
567 int retval = 0;
568
21677cfc
JW
569 mutex_lock(&ictx->lock);
570
571 if (!ictx->display_supported) {
e2302501 572 pr_err("display not supported by device\n");
21677cfc
JW
573 retval = -ENODEV;
574 } else if (!ictx->display_isopen) {
e2302501 575 pr_err("display is not open\n");
21677cfc
JW
576 retval = -EIO;
577 } else {
f789bf40 578 ictx->display_isopen = false;
21677cfc 579 dev_dbg(ictx->dev, "display port closed\n");
21677cfc
JW
580 }
581
582 mutex_unlock(&ictx->lock);
db264d4c
TH
583 if (refcount_dec_and_test(&ictx->users))
584 free_imon_context(ictx);
21677cfc
JW
585 return retval;
586}
587
255940e6 588/*
23ef710e
JW
589 * Sends a packet to the device -- this function must be called with
590 * ictx->lock held, or its unlock/lock sequence while waiting for tx
591 * to complete can/will lead to a deadlock.
21677cfc
JW
592 */
593static int send_packet(struct imon_context *ictx)
594{
595 unsigned int pipe;
596 unsigned long timeout;
597 int interval = 0;
598 int retval = 0;
599 struct usb_ctrlrequest *control_req = NULL;
600
601 /* Check if we need to use control or interrupt urb */
602 if (!ictx->tx_control) {
603 pipe = usb_sndintpipe(ictx->usbdev_intf0,
604 ictx->tx_endpoint->bEndpointAddress);
605 interval = ictx->tx_endpoint->bInterval;
606
607 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
608 ictx->usb_tx_buf,
609 sizeof(ictx->usb_tx_buf),
610 usb_tx_callback, ictx, interval);
611
612 ictx->tx_urb->actual_length = 0;
613 } else {
614 /* fill request into kmalloc'ed space: */
a8c779eb 615 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
21677cfc
JW
616 if (control_req == NULL)
617 return -ENOMEM;
618
619 /* setup packet is '21 09 0200 0001 0008' */
620 control_req->bRequestType = 0x21;
621 control_req->bRequest = 0x09;
622 control_req->wValue = cpu_to_le16(0x0200);
623 control_req->wIndex = cpu_to_le16(0x0001);
624 control_req->wLength = cpu_to_le16(0x0008);
625
626 /* control pipe is endpoint 0x00 */
627 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
628
629 /* build the control urb */
630 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
631 pipe, (unsigned char *)control_req,
632 ictx->usb_tx_buf,
633 sizeof(ictx->usb_tx_buf),
634 usb_tx_callback, ictx);
635 ictx->tx_urb->actual_length = 0;
636 }
637
7c073fff 638 reinit_completion(&ictx->tx.finished);
f789bf40 639 ictx->tx.busy = true;
21677cfc
JW
640 smp_rmb(); /* ensure later readers know we're busy */
641
642 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
643 if (retval) {
f789bf40 644 ictx->tx.busy = false;
21677cfc 645 smp_rmb(); /* ensure later readers know we're not busy */
47a09b08 646 pr_err_ratelimited("error submitting urb(%d)\n", retval);
21677cfc
JW
647 } else {
648 /* Wait for transmission to complete (or abort) */
21677cfc
JW
649 retval = wait_for_completion_interruptible(
650 &ictx->tx.finished);
5f3f254f
KB
651 if (retval) {
652 usb_kill_urb(ictx->tx_urb);
47a09b08 653 pr_err_ratelimited("task interrupted\n");
5f3f254f 654 }
21677cfc 655
813ceef0 656 ictx->tx.busy = false;
21677cfc
JW
657 retval = ictx->tx.status;
658 if (retval)
47a09b08 659 pr_err_ratelimited("packet tx failed (%d)\n", retval);
21677cfc
JW
660 }
661
662 kfree(control_req);
663
664 /*
26ccbec4 665 * Induce a mandatory delay before returning, as otherwise,
21677cfc
JW
666 * send_packet can get called so rapidly as to overwhelm the device,
667 * particularly on faster systems and/or those with quirky usb.
668 */
26ccbec4
KB
669 timeout = msecs_to_jiffies(ictx->send_packet_delay);
670 set_current_state(TASK_INTERRUPTIBLE);
21677cfc
JW
671 schedule_timeout(timeout);
672
673 return retval;
674}
675
255940e6 676/*
21677cfc
JW
677 * Sends an associate packet to the iMON 2.4G.
678 *
679 * This might not be such a good idea, since it has an id collision with
680 * some versions of the "IR & VFD" combo. The only way to determine if it
681 * is an RF version is to look at the product description string. (Which
682 * we currently do not fetch).
683 */
684static int send_associate_24g(struct imon_context *ictx)
685{
21677cfc
JW
686 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
687 0x00, 0x00, 0x00, 0x20 };
688
689 if (!ictx) {
e2302501 690 pr_err("no context for device\n");
21677cfc
JW
691 return -ENODEV;
692 }
693
694 if (!ictx->dev_present_intf0) {
e2302501 695 pr_err("no iMON device present\n");
21677cfc
JW
696 return -ENODEV;
697 }
698
699 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
21677cfc 700
2dfe2c4f 701 return send_packet(ictx);
21677cfc
JW
702}
703
255940e6 704/*
21677cfc
JW
705 * Sends packets to setup and show clock on iMON display
706 *
707 * Arguments: year - last 2 digits of year, month - 1..12,
708 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
709 * hour - 0..23, minute - 0..59, second - 0..59
710 */
711static int send_set_imon_clock(struct imon_context *ictx,
712 unsigned int year, unsigned int month,
713 unsigned int day, unsigned int dow,
714 unsigned int hour, unsigned int minute,
715 unsigned int second)
716{
717 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
718 int retval = 0;
719 int i;
720
721 if (!ictx) {
e2302501 722 pr_err("no context for device\n");
21677cfc
JW
723 return -ENODEV;
724 }
725
726 switch (ictx->display_type) {
727 case IMON_DISPLAY_TYPE_LCD:
728 clock_enable_pkt[0][0] = 0x80;
729 clock_enable_pkt[0][1] = year;
730 clock_enable_pkt[0][2] = month-1;
731 clock_enable_pkt[0][3] = day;
732 clock_enable_pkt[0][4] = hour;
733 clock_enable_pkt[0][5] = minute;
734 clock_enable_pkt[0][6] = second;
735
736 clock_enable_pkt[1][0] = 0x80;
737 clock_enable_pkt[1][1] = 0;
738 clock_enable_pkt[1][2] = 0;
739 clock_enable_pkt[1][3] = 0;
740 clock_enable_pkt[1][4] = 0;
741 clock_enable_pkt[1][5] = 0;
742 clock_enable_pkt[1][6] = 0;
743
744 if (ictx->product == 0xffdc) {
745 clock_enable_pkt[0][7] = 0x50;
746 clock_enable_pkt[1][7] = 0x51;
747 } else {
748 clock_enable_pkt[0][7] = 0x88;
749 clock_enable_pkt[1][7] = 0x8a;
750 }
751
752 break;
753
754 case IMON_DISPLAY_TYPE_VFD:
755 clock_enable_pkt[0][0] = year;
756 clock_enable_pkt[0][1] = month-1;
757 clock_enable_pkt[0][2] = day;
758 clock_enable_pkt[0][3] = dow;
759 clock_enable_pkt[0][4] = hour;
760 clock_enable_pkt[0][5] = minute;
761 clock_enable_pkt[0][6] = second;
762 clock_enable_pkt[0][7] = 0x40;
763
764 clock_enable_pkt[1][0] = 0;
765 clock_enable_pkt[1][1] = 0;
766 clock_enable_pkt[1][2] = 1;
767 clock_enable_pkt[1][3] = 0;
768 clock_enable_pkt[1][4] = 0;
769 clock_enable_pkt[1][5] = 0;
770 clock_enable_pkt[1][6] = 0;
771 clock_enable_pkt[1][7] = 0x42;
772
773 break;
774
775 default:
776 return -ENODEV;
777 }
778
779 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
780 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
781 retval = send_packet(ictx);
782 if (retval) {
e2302501 783 pr_err("send_packet failed for packet %d\n", i);
21677cfc
JW
784 break;
785 }
786 }
787
788 return retval;
789}
790
255940e6 791/*
21677cfc
JW
792 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
793 */
f1d9f315 794static ssize_t associate_remote_show(struct device *d,
21677cfc
JW
795 struct device_attribute *attr,
796 char *buf)
797{
798 struct imon_context *ictx = dev_get_drvdata(d);
799
800 if (!ictx)
801 return -ENODEV;
802
803 mutex_lock(&ictx->lock);
804 if (ictx->rf_isassociating)
2396e282 805 strscpy(buf, "associating\n", PAGE_SIZE);
21677cfc 806 else
2396e282 807 strscpy(buf, "closed\n", PAGE_SIZE);
21677cfc 808
276e2ee0 809 dev_info(d, "Visit https://www.lirc.org/html/imon-24g.html for instructions on how to associate your iMON 2.4G DT/LT remote\n");
21677cfc
JW
810 mutex_unlock(&ictx->lock);
811 return strlen(buf);
812}
813
f1d9f315 814static ssize_t associate_remote_store(struct device *d,
21677cfc
JW
815 struct device_attribute *attr,
816 const char *buf, size_t count)
817{
818 struct imon_context *ictx;
819
820 ictx = dev_get_drvdata(d);
821
822 if (!ictx)
823 return -ENODEV;
824
825 mutex_lock(&ictx->lock);
f789bf40 826 ictx->rf_isassociating = true;
21677cfc
JW
827 send_associate_24g(ictx);
828 mutex_unlock(&ictx->lock);
829
830 return count;
831}
832
255940e6 833/*
21677cfc
JW
834 * sysfs functions to control internal imon clock
835 */
f1d9f315 836static ssize_t imon_clock_show(struct device *d,
21677cfc
JW
837 struct device_attribute *attr, char *buf)
838{
839 struct imon_context *ictx = dev_get_drvdata(d);
840 size_t len;
841
842 if (!ictx)
843 return -ENODEV;
844
845 mutex_lock(&ictx->lock);
846
847 if (!ictx->display_supported) {
cc4cce95 848 len = sysfs_emit(buf, "Not supported.");
21677cfc 849 } else {
cc4cce95
LZ
850 len = sysfs_emit(buf,
851 "To set the clock on your iMON display:\n"
852 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
853 "%s", ictx->display_isopen ?
854 "\nNOTE: imon device must be closed\n" : "");
21677cfc
JW
855 }
856
857 mutex_unlock(&ictx->lock);
858
859 return len;
860}
861
f1d9f315 862static ssize_t imon_clock_store(struct device *d,
21677cfc
JW
863 struct device_attribute *attr,
864 const char *buf, size_t count)
865{
866 struct imon_context *ictx = dev_get_drvdata(d);
867 ssize_t retval;
868 unsigned int year, month, day, dow, hour, minute, second;
869
870 if (!ictx)
871 return -ENODEV;
872
873 mutex_lock(&ictx->lock);
874
875 if (!ictx->display_supported) {
876 retval = -ENODEV;
877 goto exit;
878 } else if (ictx->display_isopen) {
879 retval = -EBUSY;
880 goto exit;
881 }
882
883 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
884 &hour, &minute, &second) != 7) {
885 retval = -EINVAL;
886 goto exit;
887 }
888
889 if ((month < 1 || month > 12) ||
890 (day < 1 || day > 31) || (dow > 6) ||
891 (hour > 23) || (minute > 59) || (second > 59)) {
892 retval = -EINVAL;
893 goto exit;
894 }
895
896 retval = send_set_imon_clock(ictx, year, month, day, dow,
897 hour, minute, second);
898 if (retval)
899 goto exit;
900
901 retval = count;
902exit:
903 mutex_unlock(&ictx->lock);
904
905 return retval;
906}
907
908
f1d9f315
ZL
909static DEVICE_ATTR_RW(imon_clock);
910static DEVICE_ATTR_RW(associate_remote);
21677cfc
JW
911
912static struct attribute *imon_display_sysfs_entries[] = {
913 &dev_attr_imon_clock.attr,
914 NULL
915};
916
d9a77b98 917static const struct attribute_group imon_display_attr_group = {
21677cfc
JW
918 .attrs = imon_display_sysfs_entries
919};
920
921static struct attribute *imon_rf_sysfs_entries[] = {
922 &dev_attr_associate_remote.attr,
923 NULL
924};
925
d9a77b98 926static const struct attribute_group imon_rf_attr_group = {
21677cfc
JW
927 .attrs = imon_rf_sysfs_entries
928};
929
255940e6 930/*
21677cfc
JW
931 * Writes data to the VFD. The iMON VFD is 2x16 characters
932 * and requires data in 5 consecutive USB interrupt packets,
933 * each packet but the last carrying 7 bytes.
934 *
935 * I don't know if the VFD board supports features such as
936 * scrolling, clearing rows, blanking, etc. so at
937 * the caller must provide a full screen of data. If fewer
938 * than 32 bytes are provided spaces will be appended to
939 * generate a full screen.
940 */
d6740d86 941static ssize_t vfd_write(struct file *file, const char __user *buf,
21677cfc
JW
942 size_t n_bytes, loff_t *pos)
943{
944 int i;
945 int offset;
946 int seq;
947 int retval = 0;
db264d4c 948 struct imon_context *ictx = file->private_data;
c25895c7 949 static const unsigned char vfd_packet6[] = {
21677cfc
JW
950 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
951
db264d4c 952 if (ictx->disconnected)
21677cfc 953 return -ENODEV;
21677cfc 954
813ceef0
GM
955 if (mutex_lock_interruptible(&ictx->lock))
956 return -ERESTARTSYS;
21677cfc
JW
957
958 if (!ictx->dev_present_intf0) {
47a09b08 959 pr_err_ratelimited("no iMON device present\n");
21677cfc
JW
960 retval = -ENODEV;
961 goto exit;
962 }
963
964 if (n_bytes <= 0 || n_bytes > 32) {
47a09b08 965 pr_err_ratelimited("invalid payload size\n");
21677cfc
JW
966 retval = -EINVAL;
967 goto exit;
968 }
969
970 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
971 retval = -EFAULT;
972 goto exit;
973 }
974
975 /* Pad with spaces */
976 for (i = n_bytes; i < 32; ++i)
977 ictx->tx.data_buf[i] = ' ';
978
979 for (i = 32; i < 35; ++i)
980 ictx->tx.data_buf[i] = 0xFF;
981
982 offset = 0;
983 seq = 0;
984
985 do {
986 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
987 ictx->usb_tx_buf[7] = (unsigned char) seq;
988
989 retval = send_packet(ictx);
990 if (retval) {
47a09b08 991 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
21677cfc
JW
992 goto exit;
993 } else {
994 seq += 2;
995 offset += 7;
996 }
997
998 } while (offset < 35);
999
1000 /* Send packet #6 */
1001 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
1002 ictx->usb_tx_buf[7] = (unsigned char) seq;
1003 retval = send_packet(ictx);
1004 if (retval)
47a09b08 1005 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
21677cfc
JW
1006
1007exit:
1008 mutex_unlock(&ictx->lock);
1009
1010 return (!retval) ? n_bytes : retval;
1011}
1012
255940e6 1013/*
21677cfc
JW
1014 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
1015 * packets. We accept data as 16 hexadecimal digits, followed by a
1016 * newline (to make it easy to drive the device from a command-line
1017 * -- even though the actual binary data is a bit complicated).
1018 *
1019 * The device itself is not a "traditional" text-mode display. It's
1020 * actually a 16x96 pixel bitmap display. That means if you want to
1021 * display text, you've got to have your own "font" and translate the
1022 * text into bitmaps for display. This is really flexible (you can
1023 * display whatever diacritics you need, and so on), but it's also
1024 * a lot more complicated than most LCDs...
1025 */
d6740d86 1026static ssize_t lcd_write(struct file *file, const char __user *buf,
21677cfc
JW
1027 size_t n_bytes, loff_t *pos)
1028{
1029 int retval = 0;
db264d4c 1030 struct imon_context *ictx = file->private_data;
21677cfc 1031
db264d4c 1032 if (ictx->disconnected)
21677cfc 1033 return -ENODEV;
21677cfc
JW
1034
1035 mutex_lock(&ictx->lock);
1036
1037 if (!ictx->display_supported) {
47a09b08 1038 pr_err_ratelimited("no iMON display present\n");
21677cfc
JW
1039 retval = -ENODEV;
1040 goto exit;
1041 }
1042
1043 if (n_bytes != 8) {
47a09b08
JW
1044 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1045 (int)n_bytes);
21677cfc
JW
1046 retval = -EINVAL;
1047 goto exit;
1048 }
1049
1050 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1051 retval = -EFAULT;
1052 goto exit;
1053 }
1054
1055 retval = send_packet(ictx);
1056 if (retval) {
47a09b08 1057 pr_err_ratelimited("send packet failed!\n");
21677cfc
JW
1058 goto exit;
1059 } else {
1060 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1061 __func__, (int) n_bytes);
1062 }
1063exit:
1064 mutex_unlock(&ictx->lock);
1065 return (!retval) ? n_bytes : retval;
1066}
1067
255940e6 1068/*
21677cfc
JW
1069 * Callback function for USB core API: transmit data
1070 */
1071static void usb_tx_callback(struct urb *urb)
1072{
1073 struct imon_context *ictx;
1074
1075 if (!urb)
1076 return;
1077 ictx = (struct imon_context *)urb->context;
1078 if (!ictx)
1079 return;
1080
1081 ictx->tx.status = urb->status;
1082
1083 /* notify waiters that write has finished */
f789bf40 1084 ictx->tx.busy = false;
21677cfc
JW
1085 smp_rmb(); /* ensure later readers know we're not busy */
1086 complete(&ictx->tx.finished);
1087}
1088
255940e6 1089/*
21677cfc
JW
1090 * report touchscreen input
1091 */
b17ec78a 1092static void imon_touch_display_timeout(struct timer_list *t)
21677cfc 1093{
b17ec78a 1094 struct imon_context *ictx = from_timer(ictx, t, ttimer);
21677cfc 1095
f03900d6 1096 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
21677cfc
JW
1097 return;
1098
1099 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1100 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1101 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1102 input_sync(ictx->touch);
1103}
1104
255940e6 1105/*
21677cfc
JW
1106 * iMON IR receivers support two different signal sets -- those used by
1107 * the iMON remotes, and those used by the Windows MCE remotes (which is
1108 * really just RC-6), but only one or the other at a time, as the signals
1109 * are decoded onboard the receiver.
23ef710e
JW
1110 *
1111 * This function gets called two different ways, one way is from
1112 * rc_register_device, for initial protocol selection/setup, and the other is
1113 * via a userspace-initiated protocol change request, either by direct sysfs
1114 * prodding or by something like ir-keytable. In the rc_register_device case,
1115 * the imon context lock is already held, but when initiated from userspace,
1116 * it is not, so we must acquire it prior to calling send_packet, which
1117 * requires that the lock is held.
21677cfc 1118 */
6d741bfe 1119static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
21677cfc
JW
1120{
1121 int retval;
d8b4b582 1122 struct imon_context *ictx = rc->priv;
21677cfc 1123 struct device *dev = ictx->dev;
23ef710e 1124 bool unlock = false;
21677cfc
JW
1125 unsigned char ir_proto_packet[] = {
1126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1127
6d741bfe 1128 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
25ec587c 1129 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
21677cfc 1130
6d741bfe 1131 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
21677cfc
JW
1132 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1133 ir_proto_packet[0] = 0x01;
6d741bfe 1134 *rc_proto = RC_PROTO_BIT_RC6_MCE;
2525fdcb 1135 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
666a9ed8 1136 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
76f1ef42 1137 if (!pad_stabilize)
666a9ed8 1138 dev_dbg(dev, "PAD stabilize functionality disabled\n");
21677cfc 1139 /* ir_proto_packet[0] = 0x00; // already the default */
2525fdcb 1140 *rc_proto = RC_PROTO_BIT_IMON;
c003ab1b 1141 } else {
25ec587c 1142 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
76f1ef42 1143 if (!pad_stabilize)
666a9ed8 1144 dev_dbg(dev, "PAD stabilize functionality disabled\n");
6718e8ad 1145 /* ir_proto_packet[0] = 0x00; // already the default */
2525fdcb 1146 *rc_proto = RC_PROTO_BIT_IMON;
21677cfc
JW
1147 }
1148
1149 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1150
24147897 1151 unlock = mutex_trylock(&ictx->lock);
23ef710e 1152
21677cfc 1153 retval = send_packet(ictx);
6718e8ad
JW
1154 if (retval)
1155 goto out;
1156
6d741bfe 1157 ictx->rc_proto = *rc_proto;
76f1ef42 1158 ictx->pad_mouse = false;
6718e8ad
JW
1159
1160out:
23ef710e
JW
1161 if (unlock)
1162 mutex_unlock(&ictx->lock);
1163
6718e8ad 1164 return retval;
21677cfc
JW
1165}
1166
255940e6 1167/*
21677cfc
JW
1168 * The directional pad behaves a bit differently, depending on whether this is
1169 * one of the older ffdc devices or a newer device. Newer devices appear to
1170 * have a higher resolution matrix for more precise mouse movement, but it
1171 * makes things overly sensitive in keyboard mode, so we do some interesting
1172 * contortions to make it less touchy. Older devices run through the same
1173 * routine with shorter timeout and a smaller threshold.
1174 */
1175static int stabilize(int a, int b, u16 timeout, u16 threshold)
1176{
1edba648
CZ
1177 ktime_t ct;
1178 static ktime_t prev_time;
1179 static ktime_t hit_time;
21677cfc
JW
1180 static int x, y, prev_result, hits;
1181 int result = 0;
1edba648 1182 long msec, msec_hit;
21677cfc 1183
1edba648
CZ
1184 ct = ktime_get();
1185 msec = ktime_ms_delta(ct, prev_time);
1186 msec_hit = ktime_ms_delta(ct, hit_time);
21677cfc
JW
1187
1188 if (msec > 100) {
1189 x = 0;
1190 y = 0;
1191 hits = 0;
1192 }
1193
1194 x += a;
1195 y += b;
1196
1197 prev_time = ct;
1198
1199 if (abs(x) > threshold || abs(y) > threshold) {
1200 if (abs(y) > abs(x))
1201 result = (y > 0) ? 0x7F : 0x80;
1202 else
1203 result = (x > 0) ? 0x7F00 : 0x8000;
1204
1205 x = 0;
1206 y = 0;
1207
1208 if (result == prev_result) {
1209 hits++;
1210
1211 if (hits > 3) {
1212 switch (result) {
1213 case 0x7F:
1214 y = 17 * threshold / 30;
1215 break;
1216 case 0x80:
1217 y -= 17 * threshold / 30;
1218 break;
1219 case 0x7F00:
1220 x = 17 * threshold / 30;
1221 break;
1222 case 0x8000:
1223 x -= 17 * threshold / 30;
1224 break;
1225 }
1226 }
1227
1228 if (hits == 2 && msec_hit < timeout) {
1229 result = 0;
1230 hits = 1;
1231 }
1232 } else {
1233 prev_result = result;
1234 hits = 1;
1235 hit_time = ct;
1236 }
1237 }
1238
1239 return result;
1240}
1241
eaf2bcc9 1242static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
21677cfc 1243{
21677cfc
JW
1244 u32 keycode;
1245 u32 release;
1246 bool is_release_code = false;
1247
1248 /* Look for the initial press of a button */
ca86674b 1249 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
eaf2bcc9
DH
1250 ictx->rc_toggle = 0x0;
1251 ictx->rc_scancode = scancode;
21677cfc
JW
1252
1253 /* Look for the release of a button */
1254 if (keycode == KEY_RESERVED) {
1255 release = scancode & ~0x4000;
ca86674b 1256 keycode = rc_g_keycode_from_table(ictx->rdev, release);
21677cfc
JW
1257 if (keycode != KEY_RESERVED)
1258 is_release_code = true;
1259 }
1260
1261 ictx->release_code = is_release_code;
1262
1263 return keycode;
1264}
1265
eaf2bcc9 1266static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
21677cfc 1267{
21677cfc
JW
1268 u32 keycode;
1269
1270#define MCE_KEY_MASK 0x7000
1271#define MCE_TOGGLE_BIT 0x8000
1272
1273 /*
1274 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1275 * (the toggle bit flipping between alternating key presses), while
1276 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1277 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1278 * but we can't or them into all codes, as some keys are decoded in
1279 * a different way w/o the same use of the toggle bit...
1280 */
eaf2bcc9 1281 if (scancode & 0x80000000)
21677cfc
JW
1282 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1283
eaf2bcc9 1284 ictx->rc_scancode = scancode;
ca86674b 1285 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
eaf2bcc9
DH
1286
1287 /* not used in mce mode, but make sure we know its false */
1288 ictx->release_code = false;
21677cfc
JW
1289
1290 return keycode;
1291}
1292
0d8053f2 1293static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
21677cfc 1294{
cf330691 1295 const struct imon_panel_key_table *key_table;
083e4721 1296 u32 keycode = KEY_RESERVED;
cf330691
FG
1297 int i;
1298
1299 key_table = ictx->dev_descr->key_table;
21677cfc 1300
0d8053f2
UE
1301 for (i = 0; key_table[i].hw_code != 0; i++) {
1302 if (key_table[i].hw_code == (code | 0xffee)) {
1303 keycode = key_table[i].keycode;
21677cfc 1304 break;
083e4721
JW
1305 }
1306 }
0d8053f2 1307 ictx->release_code = false;
21677cfc
JW
1308 return keycode;
1309}
1310
1311static bool imon_mouse_event(struct imon_context *ictx,
1312 unsigned char *buf, int len)
1313{
24dec5da 1314 signed char rel_x = 0x00, rel_y = 0x00;
21677cfc 1315 u8 right_shift = 1;
f789bf40 1316 bool mouse_input = true;
21677cfc 1317 int dir = 0;
693508df
JW
1318 unsigned long flags;
1319
1320 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1321
1322 /* newer iMON device PAD or mouse button */
1323 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1324 rel_x = buf[2];
1325 rel_y = buf[3];
1326 right_shift = 1;
1327 /* 0xffdc iMON PAD or mouse button input */
1328 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1329 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1330 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1331 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1332 if (buf[0] & 0x02)
1333 rel_x |= ~0x0f;
1334 rel_x = rel_x + rel_x / 2;
1335 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1336 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1337 if (buf[0] & 0x01)
1338 rel_y |= ~0x0f;
1339 rel_y = rel_y + rel_y / 2;
1340 right_shift = 2;
1341 /* some ffdc devices decode mouse buttons differently... */
1342 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1343 right_shift = 2;
1344 /* ch+/- buttons, which we use for an emulated scroll wheel */
1345 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1346 dir = 1;
1347 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1348 dir = -1;
1349 } else
f789bf40 1350 mouse_input = false;
21677cfc 1351
693508df
JW
1352 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1353
21677cfc
JW
1354 if (mouse_input) {
1355 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1356
1357 if (dir) {
1358 input_report_rel(ictx->idev, REL_WHEEL, dir);
1359 } else if (rel_x || rel_y) {
1360 input_report_rel(ictx->idev, REL_X, rel_x);
1361 input_report_rel(ictx->idev, REL_Y, rel_y);
1362 } else {
1363 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1364 input_report_key(ictx->idev, BTN_RIGHT,
1365 buf[1] >> right_shift & 0x1);
1366 }
1367 input_sync(ictx->idev);
693508df 1368 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc 1369 ictx->last_keycode = ictx->kc;
693508df 1370 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1371 }
1372
1373 return mouse_input;
1374}
1375
1376static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1377{
1378 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1379 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1380 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1381 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1382 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1383 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1384 input_sync(ictx->touch);
1385}
1386
1387static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1388{
1389 int dir = 0;
24dec5da 1390 signed char rel_x = 0x00, rel_y = 0x00;
21677cfc 1391 u16 timeout, threshold;
eaf2bcc9 1392 u32 scancode = KEY_RESERVED;
693508df 1393 unsigned long flags;
21677cfc
JW
1394
1395 /*
1396 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1397 * contain a position coordinate (x,y), with each component ranging
1398 * from -14 to 14. We want to down-sample this to only 4 discrete values
1399 * for up/down/left/right arrow keys. Also, when you get too close to
25985edc 1400 * diagonals, it has a tendency to jump back and forth, so lets try to
21677cfc
JW
1401 * ignore when they get too close.
1402 */
1403 if (ictx->product != 0xffdc) {
1404 /* first, pad to 8 bytes so it conforms with everything else */
1405 buf[5] = buf[6] = buf[7] = 0;
1406 timeout = 500; /* in msecs */
1407 /* (2*threshold) x (2*threshold) square */
1408 threshold = pad_thresh ? pad_thresh : 28;
1409 rel_x = buf[2];
1410 rel_y = buf[3];
1411
2525fdcb 1412 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
21677cfc
JW
1413 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1414 dir = stabilize((int)rel_x, (int)rel_y,
1415 timeout, threshold);
1416 if (!dir) {
693508df
JW
1417 spin_lock_irqsave(&ictx->kc_lock,
1418 flags);
21677cfc 1419 ictx->kc = KEY_UNKNOWN;
693508df
JW
1420 spin_unlock_irqrestore(&ictx->kc_lock,
1421 flags);
21677cfc
JW
1422 return;
1423 }
1424 buf[2] = dir & 0xFF;
1425 buf[3] = (dir >> 8) & 0xFF;
d778d258 1426 scancode = be32_to_cpu(*((__be32 *)buf));
21677cfc
JW
1427 }
1428 } else {
eaf2bcc9
DH
1429 /*
1430 * Hack alert: instead of using keycodes, we have
1431 * to use hard-coded scancodes here...
1432 */
21677cfc
JW
1433 if (abs(rel_y) > abs(rel_x)) {
1434 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1435 buf[3] = 0;
eaf2bcc9
DH
1436 if (rel_y > 0)
1437 scancode = 0x01007f00; /* KEY_DOWN */
1438 else
1439 scancode = 0x01008000; /* KEY_UP */
21677cfc
JW
1440 } else {
1441 buf[2] = 0;
1442 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
eaf2bcc9
DH
1443 if (rel_x > 0)
1444 scancode = 0x0100007f; /* KEY_RIGHT */
1445 else
1446 scancode = 0x01000080; /* KEY_LEFT */
21677cfc
JW
1447 }
1448 }
1449
1450 /*
1451 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1452 * device (15c2:ffdc). The remote generates various codes from
1453 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1454 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1455 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
39c1cb2b 1456 * reversed endianness. Extract direction from buffer, rotate endianness,
21677cfc
JW
1457 * adjust sign and feed the values into stabilize(). The resulting codes
1458 * will be 0x01008000, 0x01007F00, which match the newer devices.
1459 */
1460 } else {
1461 timeout = 10; /* in msecs */
1462 /* (2*threshold) x (2*threshold) square */
1463 threshold = pad_thresh ? pad_thresh : 15;
1464
1465 /* buf[1] is x */
1466 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1467 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1468 if (buf[0] & 0x02)
1469 rel_x |= ~0x10+1;
1470 /* buf[2] is y */
1471 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1472 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1473 if (buf[0] & 0x01)
1474 rel_y |= ~0x10+1;
1475
1476 buf[0] = 0x01;
1477 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1478
2525fdcb 1479 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
21677cfc
JW
1480 dir = stabilize((int)rel_x, (int)rel_y,
1481 timeout, threshold);
1482 if (!dir) {
693508df 1483 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc 1484 ictx->kc = KEY_UNKNOWN;
693508df 1485 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1486 return;
1487 }
1488 buf[2] = dir & 0xFF;
1489 buf[3] = (dir >> 8) & 0xFF;
d778d258 1490 scancode = be32_to_cpu(*((__be32 *)buf));
21677cfc 1491 } else {
eaf2bcc9
DH
1492 /*
1493 * Hack alert: instead of using keycodes, we have
1494 * to use hard-coded scancodes here...
1495 */
21677cfc
JW
1496 if (abs(rel_y) > abs(rel_x)) {
1497 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1498 buf[3] = 0;
eaf2bcc9
DH
1499 if (rel_y > 0)
1500 scancode = 0x01007f00; /* KEY_DOWN */
1501 else
1502 scancode = 0x01008000; /* KEY_UP */
21677cfc
JW
1503 } else {
1504 buf[2] = 0;
1505 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
eaf2bcc9
DH
1506 if (rel_x > 0)
1507 scancode = 0x0100007f; /* KEY_RIGHT */
1508 else
1509 scancode = 0x01000080; /* KEY_LEFT */
21677cfc
JW
1510 }
1511 }
1512 }
eaf2bcc9 1513
693508df
JW
1514 if (scancode) {
1515 spin_lock_irqsave(&ictx->kc_lock, flags);
eaf2bcc9 1516 ictx->kc = imon_remote_key_lookup(ictx, scancode);
693508df
JW
1517 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1518 }
21677cfc
JW
1519}
1520
255940e6 1521/*
eaf2bcc9
DH
1522 * figure out if these is a press or a release. We don't actually
1523 * care about repeats, as those will be auto-generated within the IR
1524 * subsystem for repeating scancodes.
1525 */
21677cfc
JW
1526static int imon_parse_press_type(struct imon_context *ictx,
1527 unsigned char *buf, u8 ktype)
1528{
1529 int press_type = 0;
693508df
JW
1530 unsigned long flags;
1531
1532 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1533
1534 /* key release of 0x02XXXXXX key */
1535 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1536 ictx->kc = ictx->last_keycode;
1537
1538 /* mouse button release on (some) 0xffdc devices */
1539 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1540 buf[2] == 0x81 && buf[3] == 0xb7)
1541 ictx->kc = ictx->last_keycode;
1542
1543 /* mouse button release on (some other) 0xffdc devices */
1544 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1545 buf[2] == 0x81 && buf[3] == 0xb7)
1546 ictx->kc = ictx->last_keycode;
1547
eaf2bcc9 1548 /* mce-specific button handling, no keyup events */
21677cfc 1549 else if (ktype == IMON_KEY_MCE) {
eaf2bcc9
DH
1550 ictx->rc_toggle = buf[2];
1551 press_type = 1;
21677cfc
JW
1552
1553 /* incoherent or irrelevant data */
1554 } else if (ictx->kc == KEY_RESERVED)
1555 press_type = -EINVAL;
1556
1557 /* key release of 0xXXXXXXb7 key */
1558 else if (ictx->release_code)
1559 press_type = 0;
1560
1561 /* this is a button press */
1562 else
1563 press_type = 1;
1564
693508df
JW
1565 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1566
21677cfc
JW
1567 return press_type;
1568}
1569
255940e6 1570/*
21677cfc
JW
1571 * Process the incoming packet
1572 */
1b450f21 1573static void imon_incoming_packet(struct imon_context *ictx,
21677cfc
JW
1574 struct urb *urb, int intf)
1575{
1576 int len = urb->actual_length;
1577 unsigned char *buf = urb->transfer_buffer;
1578 struct device *dev = ictx->dev;
693508df 1579 unsigned long flags;
21677cfc 1580 u32 kc;
eaf2bcc9 1581 u64 scancode;
21677cfc 1582 int press_type = 0;
1edba648
CZ
1583 ktime_t t;
1584 static ktime_t prev_time;
eaf2bcc9 1585 u8 ktype;
21677cfc 1586
21677cfc 1587 /* filter out junk data on the older 0xffdc imon devices */
bbe4690f 1588 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
21677cfc
JW
1589 return;
1590
1591 /* Figure out what key was pressed */
21677cfc 1592 if (len == 8 && buf[7] == 0xee) {
d778d258 1593 scancode = be64_to_cpu(*((__be64 *)buf));
21677cfc 1594 ktype = IMON_KEY_PANEL;
0d8053f2 1595 kc = imon_panel_key_lookup(ictx, scancode);
6ddc2be5 1596 ictx->release_code = false;
21677cfc 1597 } else {
d778d258 1598 scancode = be32_to_cpu(*((__be32 *)buf));
6d741bfe 1599 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
eaf2bcc9 1600 ktype = IMON_KEY_IMON;
21677cfc
JW
1601 if (buf[0] == 0x80)
1602 ktype = IMON_KEY_MCE;
eaf2bcc9
DH
1603 kc = imon_mce_key_lookup(ictx, scancode);
1604 } else {
1605 ktype = IMON_KEY_IMON;
1606 kc = imon_remote_key_lookup(ictx, scancode);
1607 }
21677cfc
JW
1608 }
1609
693508df 1610 spin_lock_irqsave(&ictx->kc_lock, flags);
21677cfc
JW
1611 /* keyboard/mouse mode toggle button */
1612 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1613 ictx->last_keycode = kc;
1614 if (!nomouse) {
bd7e31bb 1615 ictx->pad_mouse = !ictx->pad_mouse;
21677cfc
JW
1616 dev_dbg(dev, "toggling to %s mode\n",
1617 ictx->pad_mouse ? "mouse" : "keyboard");
693508df 1618 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1619 return;
1620 } else {
76f1ef42 1621 ictx->pad_mouse = false;
21677cfc
JW
1622 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1623 }
1624 }
1625
1626 ictx->kc = kc;
693508df 1627 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1628
1629 /* send touchscreen events through input subsystem if touchpad data */
f3f5ba42 1630 if (ictx->touch && len == 8 && buf[7] == 0x86) {
21677cfc 1631 imon_touch_event(ictx, buf);
eaf2bcc9 1632 return;
21677cfc
JW
1633
1634 /* look for mouse events with pad in mouse mode */
1635 } else if (ictx->pad_mouse) {
1636 if (imon_mouse_event(ictx, buf, len))
1637 return;
1638 }
1639
1640 /* Now for some special handling to convert pad input to arrow keys */
1641 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1642 ((len == 8) && (buf[0] & 0x40) &&
1643 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1644 len = 8;
1645 imon_pad_to_keys(ictx, buf);
21677cfc
JW
1646 }
1647
1648 if (debug) {
832d40c0
MCC
1649 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1650 intf, len, buf);
21677cfc
JW
1651 }
1652
1653 press_type = imon_parse_press_type(ictx, buf, ktype);
1654 if (press_type < 0)
1655 goto not_input_data;
1656
eaf2bcc9
DH
1657 if (ktype != IMON_KEY_PANEL) {
1658 if (press_type == 0)
ca86674b 1659 rc_keyup(ictx->rdev);
eaf2bcc9 1660 else {
2525fdcb
SY
1661 enum rc_proto proto;
1662
1663 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1664 proto = RC_PROTO_RC6_MCE;
1665 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1666 proto = RC_PROTO_IMON;
1667 else
1668 return;
1669
1670 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1671 ictx->rc_toggle);
1672
693508df 1673 spin_lock_irqsave(&ictx->kc_lock, flags);
eaf2bcc9 1674 ictx->last_keycode = ictx->kc;
693508df 1675 spin_unlock_irqrestore(&ictx->kc_lock, flags);
eaf2bcc9
DH
1676 }
1677 return;
1678 }
21677cfc 1679
eaf2bcc9 1680 /* Only panel type events left to process now */
693508df
JW
1681 spin_lock_irqsave(&ictx->kc_lock, flags);
1682
1edba648 1683 t = ktime_get();
cf330691
FG
1684 /* KEY repeats from knob and panel that need to be suppressed */
1685 if (ictx->kc == KEY_MUTE ||
1686 ictx->dev_descr->flags & IMON_SUPPRESS_REPEATED_KEYS) {
1687 if (ictx->kc == ictx->last_keycode &&
1688 ktime_ms_delta(t, prev_time) < ictx->idev->rep[REP_DELAY]) {
693508df 1689 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc 1690 return;
693508df 1691 }
21677cfc 1692 }
cf330691 1693
94215ccd 1694 prev_time = t;
693508df 1695 kc = ictx->kc;
21677cfc 1696
693508df 1697 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc 1698
428cc763
JW
1699 input_report_key(ictx->idev, kc, press_type);
1700 input_sync(ictx->idev);
21677cfc 1701
eaf2bcc9 1702 /* panel keys don't generate a release */
428cc763
JW
1703 input_report_key(ictx->idev, kc, 0);
1704 input_sync(ictx->idev);
21677cfc 1705
94215ccd 1706 spin_lock_irqsave(&ictx->kc_lock, flags);
693508df 1707 ictx->last_keycode = kc;
94215ccd 1708 spin_unlock_irqrestore(&ictx->kc_lock, flags);
21677cfc
JW
1709
1710 return;
1711
21677cfc
JW
1712not_input_data:
1713 if (len != 8) {
25ec587c
MCC
1714 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1715 __func__, len, intf);
21677cfc
JW
1716 return;
1717 }
1718
1719 /* iMON 2.4G associate frame */
1720 if (buf[0] == 0x00 &&
1721 buf[2] == 0xFF && /* REFID */
1722 buf[3] == 0xFF &&
1723 buf[4] == 0xFF &&
1724 buf[5] == 0xFF && /* iMON 2.4G */
1725 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1726 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1727 dev_warn(dev, "%s: remote associated refid=%02X\n",
1728 __func__, buf[1]);
f789bf40 1729 ictx->rf_isassociating = false;
21677cfc
JW
1730 }
1731}
1732
255940e6 1733/*
21677cfc
JW
1734 * Callback function for USB core API: receive data
1735 */
1736static void usb_rx_callback_intf0(struct urb *urb)
1737{
1738 struct imon_context *ictx;
1739 int intfnum = 0;
1740
1741 if (!urb)
1742 return;
1743
1744 ictx = (struct imon_context *)urb->context;
8791d63a 1745 if (!ictx)
21677cfc
JW
1746 return;
1747
8791d63a
JW
1748 /*
1749 * if we get a callback before we're done configuring the hardware, we
1750 * can't yet process the data, as there's nowhere to send it, but we
1751 * still need to submit a new rx URB to avoid wedging the hardware
1752 */
1753 if (!ictx->dev_present_intf0)
1754 goto out;
1755
21677cfc
JW
1756 switch (urb->status) {
1757 case -ENOENT: /* usbcore unlink successful! */
1758 return;
1759
1760 case -ESHUTDOWN: /* transport endpoint was shut down */
1761 break;
1762
1763 case 0:
1b450f21 1764 imon_incoming_packet(ictx, urb, intfnum);
21677cfc
JW
1765 break;
1766
1767 default:
1768 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1769 __func__, urb->status);
1770 break;
1771 }
1772
8791d63a 1773out:
21677cfc
JW
1774 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1775}
1776
1777static void usb_rx_callback_intf1(struct urb *urb)
1778{
1779 struct imon_context *ictx;
1780 int intfnum = 1;
1781
1782 if (!urb)
1783 return;
1784
1785 ictx = (struct imon_context *)urb->context;
8791d63a 1786 if (!ictx)
21677cfc
JW
1787 return;
1788
8791d63a
JW
1789 /*
1790 * if we get a callback before we're done configuring the hardware, we
1791 * can't yet process the data, as there's nowhere to send it, but we
1792 * still need to submit a new rx URB to avoid wedging the hardware
1793 */
1794 if (!ictx->dev_present_intf1)
1795 goto out;
1796
21677cfc
JW
1797 switch (urb->status) {
1798 case -ENOENT: /* usbcore unlink successful! */
1799 return;
1800
1801 case -ESHUTDOWN: /* transport endpoint was shut down */
1802 break;
1803
1804 case 0:
1b450f21 1805 imon_incoming_packet(ictx, urb, intfnum);
21677cfc
JW
1806 break;
1807
1808 default:
1809 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1810 __func__, urb->status);
1811 break;
1812 }
1813
8791d63a 1814out:
21677cfc
JW
1815 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1816}
1817
04292fc0
JW
1818/*
1819 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1820 * devices, and all of them constantly spew interrupts, even when there
1821 * is no actual data to report. However, byte 6 of this buffer looks like
1822 * its unique across device variants, so we're trying to key off that to
1823 * figure out which display type (if any) and what IR protocol the device
1824 * actually supports. These devices have their IR protocol hard-coded into
1825 * their firmware, they can't be changed on the fly like the newer hardware.
1826 */
1827static void imon_get_ffdc_type(struct imon_context *ictx)
1828{
1829 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1830 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
2525fdcb 1831 u64 allowed_protos = RC_PROTO_BIT_IMON;
04292fc0
JW
1832
1833 switch (ffdc_cfg_byte) {
1834 /* iMON Knob, no display, iMON IR + vol knob */
1835 case 0x21:
1836 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1837 ictx->display_supported = false;
1838 break;
1839 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1840 case 0x4e:
1841 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1842 ictx->display_supported = false;
1843 ictx->rf_device = true;
1844 break;
1845 /* iMON VFD, no IR (does have vol knob tho) */
1846 case 0x35:
1847 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1848 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1849 break;
1850 /* iMON VFD, iMON IR */
1851 case 0x24:
6d4a36d1 1852 case 0x30:
04292fc0
JW
1853 case 0x85:
1854 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1855 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1856 break;
1857 /* iMON VFD, MCE IR */
443b3919 1858 case 0x46:
04292fc0
JW
1859 case 0x9e:
1860 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1861 detected_display_type = IMON_DISPLAY_TYPE_VFD;
6d741bfe 1862 allowed_protos = RC_PROTO_BIT_RC6_MCE;
04292fc0 1863 break;
b20a6e29
DR
1864 /* iMON VFD, iMON or MCE IR */
1865 case 0x7e:
1866 dev_info(ictx->dev, "0xffdc iMON VFD, iMON or MCE IR");
1867 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1868 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
1869 break;
04292fc0
JW
1870 /* iMON LCD, MCE IR */
1871 case 0x9f:
1872 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1873 detected_display_type = IMON_DISPLAY_TYPE_LCD;
6d741bfe 1874 allowed_protos = RC_PROTO_BIT_RC6_MCE;
04292fc0 1875 break;
6a489f76
SY
1876 /* no display, iMON IR */
1877 case 0x26:
1878 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1879 ictx->display_supported = false;
1880 break;
cf330691
FG
1881 /* Soundgraph iMON UltraBay */
1882 case 0x98:
1883 dev_info(ictx->dev, "0xffdc iMON UltraBay, LCD + IR");
1884 detected_display_type = IMON_DISPLAY_TYPE_LCD;
1885 allowed_protos = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
1886 ictx->dev_descr = &ultrabay_table;
1887 break;
1888
04292fc0 1889 default:
25ec587c 1890 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
04292fc0 1891 detected_display_type = IMON_DISPLAY_TYPE_VFD;
2525fdcb
SY
1892 /*
1893 * We don't know which one it is, allow user to set the
1894 * RC6 one from userspace if IMON wasn't correct.
1895 */
6d741bfe 1896 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
04292fc0
JW
1897 break;
1898 }
1899
1900 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1901
1902 ictx->display_type = detected_display_type;
6d741bfe 1903 ictx->rc_proto = allowed_protos;
04292fc0
JW
1904}
1905
1906static void imon_set_display_type(struct imon_context *ictx)
1907{
1908 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1909
1910 /*
1911 * Try to auto-detect the type of display if the user hasn't set
1912 * it by hand via the display_type modparam. Default is VFD.
1913 */
1914
1915 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1916 switch (ictx->product) {
1917 case 0xffdc:
1918 /* set in imon_get_ffdc_type() */
1919 configured_display_type = ictx->display_type;
1920 break;
1921 case 0x0034:
1922 case 0x0035:
1923 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1924 break;
1925 case 0x0038:
1926 case 0x0039:
1927 case 0x0045:
1928 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1929 break;
1930 case 0x003c:
1931 case 0x0041:
1932 case 0x0042:
1933 case 0x0043:
1934 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1935 ictx->display_supported = false;
1936 break;
1937 case 0x0036:
1938 case 0x0044:
1939 default:
1940 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1941 break;
1942 }
1943 } else {
1944 configured_display_type = display_type;
1945 if (display_type == IMON_DISPLAY_TYPE_NONE)
1946 ictx->display_supported = false;
1947 else
1948 ictx->display_supported = true;
25ec587c
MCC
1949 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1950 __func__, display_type);
04292fc0
JW
1951 }
1952
1953 ictx->display_type = configured_display_type;
1954}
1955
d8b4b582 1956static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
eaf2bcc9 1957{
d8b4b582 1958 struct rc_dev *rdev;
eaf2bcc9 1959 int ret;
c25895c7
CIK
1960 static const unsigned char fp_packet[] = {
1961 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
eaf2bcc9 1962
1b450f21 1963 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
d8b4b582 1964 if (!rdev) {
eaf2bcc9
DH
1965 dev_err(ictx->dev, "remote control dev allocation failed\n");
1966 goto out;
1967 }
1968
1969 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1970 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1971 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1972 sizeof(ictx->phys_rdev));
1973 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1974
518f4b26 1975 rdev->device_name = ictx->name_rdev;
d8b4b582
DH
1976 rdev->input_phys = ictx->phys_rdev;
1977 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
eaf2bcc9 1978 rdev->dev.parent = ictx->dev;
eaf2bcc9 1979
d8b4b582 1980 rdev->priv = ictx;
1b450f21 1981 /* iMON PAD or MCE */
2525fdcb 1982 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
d8b4b582
DH
1983 rdev->change_protocol = imon_ir_change_protocol;
1984 rdev->driver_name = MOD_NAME;
eaf2bcc9 1985
04292fc0
JW
1986 /* Enable front-panel buttons and/or knobs */
1987 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1988 ret = send_packet(ictx);
1989 /* Not fatal, but warn about it */
1990 if (ret)
1991 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1992
7d2edfc2 1993 if (ictx->product == 0xffdc) {
04292fc0 1994 imon_get_ffdc_type(ictx);
6d741bfe 1995 rdev->allowed_protocols = ictx->rc_proto;
7d2edfc2 1996 }
04292fc0
JW
1997
1998 imon_set_display_type(ictx);
1999
1b450f21 2000 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
7d2edfc2
JW
2001 rdev->map_name = RC_MAP_IMON_MCE;
2002 else
2003 rdev->map_name = RC_MAP_IMON_PAD;
2004
d8b4b582 2005 ret = rc_register_device(rdev);
eaf2bcc9
DH
2006 if (ret < 0) {
2007 dev_err(ictx->dev, "remote input dev register failed\n");
2008 goto out;
2009 }
2010
2011 return rdev;
2012
2013out:
d8b4b582 2014 rc_free_device(rdev);
eaf2bcc9
DH
2015 return NULL;
2016}
2017
21677cfc
JW
2018static struct input_dev *imon_init_idev(struct imon_context *ictx)
2019{
cf330691 2020 const struct imon_panel_key_table *key_table;
21677cfc 2021 struct input_dev *idev;
21677cfc 2022 int ret, i;
21677cfc 2023
cf330691
FG
2024 key_table = ictx->dev_descr->key_table;
2025
21677cfc 2026 idev = input_allocate_device();
da4a7339 2027 if (!idev)
eaf2bcc9 2028 goto out;
21677cfc 2029
21677cfc 2030 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
eaf2bcc9
DH
2031 "iMON Panel, Knob and Mouse(%04x:%04x)",
2032 ictx->vendor, ictx->product);
21677cfc
JW
2033 idev->name = ictx->name_idev;
2034
2035 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2036 sizeof(ictx->phys_idev));
eaf2bcc9 2037 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
21677cfc
JW
2038 idev->phys = ictx->phys_idev;
2039
db190fc1 2040 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
21677cfc
JW
2041
2042 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2043 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2044 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2045 BIT_MASK(REL_WHEEL);
2046
2047 /* panel and/or knob code support */
0d8053f2
UE
2048 for (i = 0; key_table[i].hw_code != 0; i++) {
2049 u32 kc = key_table[i].keycode;
21677cfc
JW
2050 __set_bit(kc, idev->keybit);
2051 }
2052
21677cfc
JW
2053 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2054 idev->dev.parent = ictx->dev;
eaf2bcc9 2055 input_set_drvdata(idev, ictx);
21677cfc 2056
eaf2bcc9 2057 ret = input_register_device(idev);
21677cfc 2058 if (ret < 0) {
eaf2bcc9
DH
2059 dev_err(ictx->dev, "input dev register failed\n");
2060 goto out;
21677cfc
JW
2061 }
2062
2063 return idev;
2064
eaf2bcc9 2065out:
21677cfc 2066 input_free_device(idev);
21677cfc
JW
2067 return NULL;
2068}
2069
2070static struct input_dev *imon_init_touch(struct imon_context *ictx)
2071{
2072 struct input_dev *touch;
2073 int ret;
2074
2075 touch = input_allocate_device();
da4a7339 2076 if (!touch)
21677cfc 2077 goto touch_alloc_failed;
21677cfc
JW
2078
2079 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2080 "iMON USB Touchscreen (%04x:%04x)",
2081 ictx->vendor, ictx->product);
2082 touch->name = ictx->name_touch;
2083
2084 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2085 sizeof(ictx->phys_touch));
eaf2bcc9 2086 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
21677cfc
JW
2087 touch->phys = ictx->phys_touch;
2088
2089 touch->evbit[0] =
2090 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2091 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2092 BIT_MASK(BTN_TOUCH);
2093 input_set_abs_params(touch, ABS_X,
2094 0x00, 0xfff, 0, 0);
2095 input_set_abs_params(touch, ABS_Y,
2096 0x00, 0xfff, 0, 0);
2097
2098 input_set_drvdata(touch, ictx);
2099
2100 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2101 touch->dev.parent = ictx->dev;
2102 ret = input_register_device(touch);
2103 if (ret < 0) {
2104 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2105 goto touch_register_failed;
2106 }
2107
2108 return touch;
2109
2110touch_register_failed:
aeb35ebc 2111 input_free_device(touch);
21677cfc
JW
2112
2113touch_alloc_failed:
2114 return NULL;
2115}
2116
2117static bool imon_find_endpoints(struct imon_context *ictx,
2118 struct usb_host_interface *iface_desc)
2119{
2120 struct usb_endpoint_descriptor *ep;
2121 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2122 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2123 int ifnum = iface_desc->desc.bInterfaceNumber;
2124 int num_endpts = iface_desc->desc.bNumEndpoints;
2125 int i, ep_dir, ep_type;
f789bf40
JW
2126 bool ir_ep_found = false;
2127 bool display_ep_found = false;
2128 bool tx_control = false;
21677cfc
JW
2129
2130 /*
2131 * Scan the endpoint list and set:
2132 * first input endpoint = IR endpoint
2133 * first output endpoint = display endpoint
2134 */
2135 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2136 ep = &iface_desc->endpoint[i].desc;
2137 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
9408d8f0 2138 ep_type = usb_endpoint_type(ep);
21677cfc
JW
2139
2140 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2141 ep_type == USB_ENDPOINT_XFER_INT) {
2142
2143 rx_endpoint = ep;
f789bf40 2144 ir_ep_found = true;
21677cfc
JW
2145 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2146
2147 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2148 ep_type == USB_ENDPOINT_XFER_INT) {
2149 tx_endpoint = ep;
f789bf40 2150 display_ep_found = true;
21677cfc
JW
2151 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2152 }
2153 }
2154
2155 if (ifnum == 0) {
2156 ictx->rx_endpoint_intf0 = rx_endpoint;
2157 /*
2158 * tx is used to send characters to lcd/vfd, associate RF
2159 * remotes, set IR protocol, and maybe more...
2160 */
2161 ictx->tx_endpoint = tx_endpoint;
2162 } else {
2163 ictx->rx_endpoint_intf1 = rx_endpoint;
2164 }
2165
2166 /*
2167 * If we didn't find a display endpoint, this is probably one of the
2168 * newer iMON devices that use control urb instead of interrupt
2169 */
2170 if (!display_ep_found) {
f789bf40
JW
2171 tx_control = true;
2172 display_ep_found = true;
25ec587c
MCC
2173 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2174 __func__);
21677cfc
JW
2175 }
2176
2177 /*
2178 * Some iMON receivers have no display. Unfortunately, it seems
2179 * that SoundGraph recycles device IDs between devices both with
2180 * and without... :\
2181 */
2182 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
f789bf40 2183 display_ep_found = false;
21677cfc
JW
2184 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2185 }
2186
2187 /*
2188 * iMON Touch devices have a VGA touchscreen, but no "display", as
2189 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2190 */
2191 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
f789bf40 2192 display_ep_found = false;
21677cfc
JW
2193 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2194 }
2195
2196 /* Input endpoint is mandatory */
2197 if (!ir_ep_found)
e2302501 2198 pr_err("no valid input (IR) endpoint found\n");
21677cfc
JW
2199
2200 ictx->tx_control = tx_control;
2201
2202 if (display_ep_found)
2203 ictx->display_supported = true;
2204
2205 return ir_ep_found;
2206
2207}
2208
f7d141b3
KB
2209static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2210 const struct usb_device_id *id)
21677cfc
JW
2211{
2212 struct imon_context *ictx;
2213 struct urb *rx_urb;
2214 struct urb *tx_urb;
2215 struct device *dev = &intf->dev;
2216 struct usb_host_interface *iface_desc;
1f71baef 2217 int ret = -ENOMEM;
21677cfc 2218
a8c779eb 2219 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
3e70b256 2220 if (!ictx)
21677cfc 2221 goto exit;
3e70b256 2222
21677cfc 2223 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2224 if (!rx_urb)
21677cfc 2225 goto rx_urb_alloc_failed;
21677cfc 2226 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2227 if (!tx_urb)
21677cfc 2228 goto tx_urb_alloc_failed;
21677cfc
JW
2229
2230 mutex_init(&ictx->lock);
693508df 2231 spin_lock_init(&ictx->kc_lock);
21677cfc
JW
2232
2233 mutex_lock(&ictx->lock);
2234
2235 ictx->dev = dev;
2236 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
21677cfc
JW
2237 ictx->rx_urb_intf0 = rx_urb;
2238 ictx->tx_urb = tx_urb;
bbe4690f 2239 ictx->rf_device = false;
21677cfc 2240
7c073fff
DW
2241 init_completion(&ictx->tx.finished);
2242
21677cfc
JW
2243 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2244 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2245
0d8053f2
UE
2246 /* save drive info for later accessing the panel/knob key table */
2247 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
f7d141b3 2248 /* default send_packet delay is 5ms but some devices need more */
0d8053f2
UE
2249 ictx->send_packet_delay = ictx->dev_descr->flags &
2250 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
f7d141b3 2251
1f71baef 2252 ret = -ENODEV;
21677cfc 2253 iface_desc = intf->cur_altsetting;
1f71baef 2254 if (!imon_find_endpoints(ictx, iface_desc)) {
21677cfc 2255 goto find_endpoint_failed;
1f71baef 2256 }
21677cfc 2257
21677cfc
JW
2258 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2259 usb_rcvintpipe(ictx->usbdev_intf0,
2260 ictx->rx_endpoint_intf0->bEndpointAddress),
2261 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2262 usb_rx_callback_intf0, ictx,
2263 ictx->rx_endpoint_intf0->bInterval);
2264
2265 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2266 if (ret) {
e2302501 2267 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
21677cfc
JW
2268 goto urb_submit_failed;
2269 }
2270
9ad77eb5
JW
2271 ictx->idev = imon_init_idev(ictx);
2272 if (!ictx->idev) {
2273 dev_err(dev, "%s: input device setup failed\n", __func__);
2274 goto idev_setup_failed;
2275 }
2276
2277 ictx->rdev = imon_init_rdev(ictx);
2278 if (!ictx->rdev) {
2279 dev_err(dev, "%s: rc device setup failed\n", __func__);
2280 goto rdev_setup_failed;
2281 }
2282
6f6b90c9
JW
2283 ictx->dev_present_intf0 = true;
2284
23ef710e 2285 mutex_unlock(&ictx->lock);
21677cfc
JW
2286 return ictx;
2287
eaf2bcc9
DH
2288rdev_setup_failed:
2289 input_unregister_device(ictx->idev);
21677cfc 2290idev_setup_failed:
9ad77eb5
JW
2291 usb_kill_urb(ictx->rx_urb_intf0);
2292urb_submit_failed:
21677cfc 2293find_endpoint_failed:
e87cb470 2294 usb_put_dev(ictx->usbdev_intf0);
21677cfc
JW
2295 mutex_unlock(&ictx->lock);
2296 usb_free_urb(tx_urb);
2297tx_urb_alloc_failed:
2298 usb_free_urb(rx_urb);
2299rx_urb_alloc_failed:
2300 kfree(ictx);
2301exit:
2302 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2303
2304 return NULL;
2305}
2306
2307static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2308 struct imon_context *ictx)
2309{
2310 struct urb *rx_urb;
2311 struct usb_host_interface *iface_desc;
1f71baef 2312 int ret = -ENOMEM;
21677cfc
JW
2313
2314 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
15241919 2315 if (!rx_urb)
21677cfc 2316 goto rx_urb_alloc_failed;
21677cfc
JW
2317
2318 mutex_lock(&ictx->lock);
2319
2320 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
b17ec78a 2321 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
21677cfc
JW
2322 }
2323
2324 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
21677cfc
JW
2325 ictx->rx_urb_intf1 = rx_urb;
2326
1f71baef 2327 ret = -ENODEV;
21677cfc
JW
2328 iface_desc = intf->cur_altsetting;
2329 if (!imon_find_endpoints(ictx, iface_desc))
2330 goto find_endpoint_failed;
2331
2332 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2333 ictx->touch = imon_init_touch(ictx);
2334 if (!ictx->touch)
2335 goto touch_setup_failed;
2336 } else
2337 ictx->touch = NULL;
2338
2339 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2340 usb_rcvintpipe(ictx->usbdev_intf1,
2341 ictx->rx_endpoint_intf1->bEndpointAddress),
2342 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2343 usb_rx_callback_intf1, ictx,
2344 ictx->rx_endpoint_intf1->bInterval);
2345
2346 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2347
2348 if (ret) {
e2302501 2349 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
21677cfc
JW
2350 goto urb_submit_failed;
2351 }
2352
6f6b90c9
JW
2353 ictx->dev_present_intf1 = true;
2354
23ef710e 2355 mutex_unlock(&ictx->lock);
21677cfc
JW
2356 return ictx;
2357
2358urb_submit_failed:
20cd1959 2359 if (ictx->touch)
21677cfc 2360 input_unregister_device(ictx->touch);
21677cfc
JW
2361touch_setup_failed:
2362find_endpoint_failed:
e87cb470 2363 usb_put_dev(ictx->usbdev_intf1);
c9458c6f 2364 ictx->usbdev_intf1 = NULL;
21677cfc
JW
2365 mutex_unlock(&ictx->lock);
2366 usb_free_urb(rx_urb);
c9458c6f 2367 ictx->rx_urb_intf1 = NULL;
21677cfc 2368rx_urb_alloc_failed:
8791d63a 2369 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
21677cfc
JW
2370
2371 return NULL;
2372}
2373
21677cfc
JW
2374static void imon_init_display(struct imon_context *ictx,
2375 struct usb_interface *intf)
2376{
2377 int ret;
2378
2379 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2380
2381 /* set up sysfs entry for built-in clock */
6aa209e4 2382 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
21677cfc 2383 if (ret)
25ec587c
MCC
2384 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2385 ret);
21677cfc
JW
2386
2387 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2388 ret = usb_register_dev(intf, &imon_lcd_class);
2389 else
2390 ret = usb_register_dev(intf, &imon_vfd_class);
2391 if (ret)
2392 /* Not a fatal error, so ignore */
25ec587c 2393 dev_info(ictx->dev, "could not get a minor number for display\n");
21677cfc
JW
2394
2395}
2396
255940e6 2397/*
21677cfc
JW
2398 * Callback function for USB core API: Probe
2399 */
4c62e976
GKH
2400static int imon_probe(struct usb_interface *interface,
2401 const struct usb_device_id *id)
21677cfc
JW
2402{
2403 struct usb_device *usbdev = NULL;
2404 struct usb_host_interface *iface_desc = NULL;
2405 struct usb_interface *first_if;
2406 struct device *dev = &interface->dev;
76a2d21d 2407 int ifnum, sysfs_err;
21677cfc
JW
2408 int ret = 0;
2409 struct imon_context *ictx = NULL;
21677cfc 2410 u16 vendor, product;
21677cfc 2411
21677cfc
JW
2412 usbdev = usb_get_dev(interface_to_usbdev(interface));
2413 iface_desc = interface->cur_altsetting;
2414 ifnum = iface_desc->desc.bInterfaceNumber;
2415 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2416 product = le16_to_cpu(usbdev->descriptor.idProduct);
2417
2418 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2419 __func__, vendor, product, ifnum);
2420
21677cfc 2421 first_if = usb_ifnum_to_if(usbdev, 0);
58fd55e8
AY
2422 if (!first_if) {
2423 ret = -ENODEV;
2424 goto fail;
2425 }
2426
a1766a4f
TI
2427 if (first_if->dev.driver != interface->dev.driver) {
2428 dev_err(&interface->dev, "inconsistent driver matching\n");
2429 ret = -EINVAL;
2430 goto fail;
2431 }
2432
21677cfc 2433 if (ifnum == 0) {
f7d141b3 2434 ictx = imon_init_intf0(interface, id);
21677cfc 2435 if (!ictx) {
e2302501 2436 pr_err("failed to initialize context!\n");
21677cfc
JW
2437 ret = -ENODEV;
2438 goto fail;
2439 }
db264d4c 2440 refcount_set(&ictx->users, 1);
21677cfc 2441
21677cfc 2442 } else {
7d54ba0e 2443 /* this is the secondary interface on the device */
db264d4c 2444 struct imon_context *first_if_ctx = usb_get_intfdata(first_if);
7d54ba0e
KB
2445
2446 /* fail early if first intf failed to register */
2447 if (!first_if_ctx) {
2448 ret = -ENODEV;
2449 goto fail;
2450 }
2451
21677cfc
JW
2452 ictx = imon_init_intf1(interface, first_if_ctx);
2453 if (!ictx) {
e2302501 2454 pr_err("failed to attach to context!\n");
21677cfc
JW
2455 ret = -ENODEV;
2456 goto fail;
2457 }
db264d4c 2458 refcount_inc(&ictx->users);
21677cfc
JW
2459
2460 }
2461
2462 usb_set_intfdata(interface, ictx);
2463
2464 if (ifnum == 0) {
bbe4690f
JW
2465 if (product == 0xffdc && ictx->rf_device) {
2466 sysfs_err = sysfs_create_group(&interface->dev.kobj,
6aa209e4 2467 &imon_rf_attr_group);
bbe4690f 2468 if (sysfs_err)
e2302501
JP
2469 pr_err("Could not create RF sysfs entries(%d)\n",
2470 sysfs_err);
bbe4690f
JW
2471 }
2472
21677cfc
JW
2473 if (ictx->display_supported)
2474 imon_init_display(ictx, interface);
2475 }
2476
25ec587c
MCC
2477 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2478 vendor, product, ifnum,
21677cfc
JW
2479 usbdev->bus->busnum, usbdev->devnum);
2480
e87cb470 2481 usb_put_dev(usbdev);
21677cfc
JW
2482
2483 return 0;
2484
2485fail:
e87cb470 2486 usb_put_dev(usbdev);
21677cfc
JW
2487 dev_err(dev, "unable to register, err %d\n", ret);
2488
2489 return ret;
2490}
2491
255940e6 2492/*
21677cfc
JW
2493 * Callback function for USB core API: disconnect
2494 */
4c62e976 2495static void imon_disconnect(struct usb_interface *interface)
21677cfc
JW
2496{
2497 struct imon_context *ictx;
2498 struct device *dev;
2499 int ifnum;
2500
21677cfc 2501 ictx = usb_get_intfdata(interface);
db264d4c 2502 ictx->disconnected = true;
21677cfc
JW
2503 dev = ictx->dev;
2504 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2505
21677cfc
JW
2506 /*
2507 * sysfs_remove_group is safe to call even if sysfs_create_group
2508 * hasn't been called
2509 */
6aa209e4
JW
2510 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2511 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
21677cfc
JW
2512
2513 usb_set_intfdata(interface, NULL);
2514
2515 /* Abort ongoing write */
2516 if (ictx->tx.busy) {
2517 usb_kill_urb(ictx->tx_urb);
7c073fff 2518 complete(&ictx->tx.finished);
21677cfc
JW
2519 }
2520
2521 if (ifnum == 0) {
f789bf40 2522 ictx->dev_present_intf0 = false;
21677cfc 2523 usb_kill_urb(ictx->rx_urb_intf0);
eaf2bcc9 2524 input_unregister_device(ictx->idev);
d8b4b582 2525 rc_unregister_device(ictx->rdev);
21677cfc
JW
2526 if (ictx->display_supported) {
2527 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2528 usb_deregister_dev(interface, &imon_lcd_class);
76a2d21d 2529 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
21677cfc
JW
2530 usb_deregister_dev(interface, &imon_vfd_class);
2531 }
af2aa3c4 2532 usb_put_dev(ictx->usbdev_intf0);
21677cfc 2533 } else {
f789bf40 2534 ictx->dev_present_intf1 = false;
21677cfc 2535 usb_kill_urb(ictx->rx_urb_intf1);
76a2d21d 2536 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
76a2d21d 2537 del_timer_sync(&ictx->ttimer);
07af64dd 2538 input_unregister_device(ictx->touch);
76a2d21d 2539 }
af2aa3c4 2540 usb_put_dev(ictx->usbdev_intf1);
21677cfc
JW
2541 }
2542
db264d4c 2543 if (refcount_dec_and_test(&ictx->users))
76a2d21d 2544 free_imon_context(ictx);
21677cfc 2545
21677cfc
JW
2546 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2547 __func__, ifnum);
2548}
2549
2550static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2551{
2552 struct imon_context *ictx = usb_get_intfdata(intf);
2553 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2554
2555 if (ifnum == 0)
2556 usb_kill_urb(ictx->rx_urb_intf0);
2557 else
2558 usb_kill_urb(ictx->rx_urb_intf1);
2559
2560 return 0;
2561}
2562
2563static int imon_resume(struct usb_interface *intf)
2564{
2565 int rc = 0;
2566 struct imon_context *ictx = usb_get_intfdata(intf);
2567 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2568
2569 if (ifnum == 0) {
2570 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2571 usb_rcvintpipe(ictx->usbdev_intf0,
2572 ictx->rx_endpoint_intf0->bEndpointAddress),
2573 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2574 usb_rx_callback_intf0, ictx,
2575 ictx->rx_endpoint_intf0->bInterval);
2576
a43617a5 2577 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_NOIO);
21677cfc
JW
2578
2579 } else {
2580 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2581 usb_rcvintpipe(ictx->usbdev_intf1,
2582 ictx->rx_endpoint_intf1->bEndpointAddress),
2583 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2584 usb_rx_callback_intf1, ictx,
2585 ictx->rx_endpoint_intf1->bInterval);
2586
a43617a5 2587 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_NOIO);
21677cfc
JW
2588 }
2589
2590 return rc;
2591}
2592
ecb3b2b3 2593module_usb_driver(imon_driver);