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