blob: 0785a24af8fc4acca86e11b74b372b8d9af29dcd [file] [log] [blame]
Jarod Wilson21677cf2010-04-16 18:29:02 -03001/*
2 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
3 *
Jarod Wilson693508d2010-09-15 15:56:03 -03004 * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson21677cf2010-04-16 18:29:02 -03005 * Portions based on the original lirc_imon driver,
6 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
7 *
8 * Huge thanks to R. Geoff Newbury for invaluable debugging on the
9 * 0xffdc iMON devices, and for sending me one to hack on, without
10 * which the support for them wouldn't be nearly as good. Thanks
11 * also to the numerous 0xffdc device owners that tested auto-config
12 * support for me and provided debug dumps from their devices.
13 *
14 * imon is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
Joe Perchese2302502010-06-20 04:20:46 -030029#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
30
Jarod Wilson21677cf2010-04-16 18:29:02 -030031#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h>
Jarod Wilson47a09b02011-07-14 14:20:46 -030037#include <linux/ratelimit.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030038
39#include <linux/input.h>
40#include <linux/usb.h>
41#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030042#include <media/rc-core.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030043
44#include <linux/time.h>
45#include <linux/timer.h>
46
47#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
48#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
49#define MOD_NAME "imon"
Jarod Wilson8791d632012-01-26 12:04:11 -030050#define MOD_VERSION "0.9.4"
Jarod Wilson21677cf2010-04-16 18:29:02 -030051
52#define DISPLAY_MINOR_BASE 144
53#define DEVICE_NAME "lcd%d"
54
55#define BUF_CHUNK_SIZE 8
56#define BUF_SIZE 128
57
58#define BIT_DURATION 250 /* each bit received is 250us */
59
60#define IMON_CLOCK_ENABLE_PACKETS 2
Jarod Wilson21677cf2010-04-16 18:29:02 -030061
62/*** P R O T O T Y P E S ***/
63
64/* USB Callback prototypes */
65static int imon_probe(struct usb_interface *interface,
66 const struct usb_device_id *id);
67static void imon_disconnect(struct usb_interface *interface);
68static void usb_rx_callback_intf0(struct urb *urb);
69static void usb_rx_callback_intf1(struct urb *urb);
70static void usb_tx_callback(struct urb *urb);
71
72/* suspend/resume support */
73static int imon_resume(struct usb_interface *intf);
74static int imon_suspend(struct usb_interface *intf, pm_message_t message);
75
76/* Display file_operations function prototypes */
77static int display_open(struct inode *inode, struct file *file);
78static int display_close(struct inode *inode, struct file *file);
79
80/* VFD write operation */
David Härdemand6740d82014-04-03 20:34:53 -030081static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -030082 size_t n_bytes, loff_t *pos);
83
84/* LCD file_operations override function prototypes */
David Härdemand6740d82014-04-03 20:34:53 -030085static ssize_t lcd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -030086 size_t n_bytes, loff_t *pos);
87
88/*** G L O B A L S ***/
89
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -030090struct imon_panel_key_table {
91 u64 hw_code;
92 u32 keycode;
93};
94
95struct imon_usb_dev_descr {
96 __u16 flags;
97#define IMON_NO_FLAGS 0
98#define IMON_NEED_20MS_PKT_DELAY 1
99 struct imon_panel_key_table key_table[];
100};
101
Jarod Wilson21677cf2010-04-16 18:29:02 -0300102struct imon_context {
103 struct device *dev;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300104 /* Newer devices have two interfaces */
105 struct usb_device *usbdev_intf0;
106 struct usb_device *usbdev_intf1;
107
108 bool display_supported; /* not all controllers do */
109 bool display_isopen; /* display port has been opened */
Jarod Wilsonbbe46902010-05-24 12:02:05 -0300110 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300111 bool rf_isassociating; /* RF remote associating */
112 bool dev_present_intf0; /* USB device presence, interface 0 */
113 bool dev_present_intf1; /* USB device presence, interface 1 */
114
115 struct mutex lock; /* to lock this object */
116 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
117
118 struct usb_endpoint_descriptor *rx_endpoint_intf0;
119 struct usb_endpoint_descriptor *rx_endpoint_intf1;
120 struct usb_endpoint_descriptor *tx_endpoint;
121 struct urb *rx_urb_intf0;
122 struct urb *rx_urb_intf1;
123 struct urb *tx_urb;
124 bool tx_control;
125 unsigned char usb_rx_buf[8];
126 unsigned char usb_tx_buf[8];
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300127 unsigned int send_packet_delay;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300128
129 struct tx_t {
130 unsigned char data_buf[35]; /* user data buffer */
131 struct completion finished; /* wait for write to finish */
132 bool busy; /* write in progress */
133 int status; /* status of tx completion */
134 } tx;
135
136 u16 vendor; /* usb vendor ID */
137 u16 product; /* usb product ID */
138
David Härdemand8b4b582010-10-29 16:08:23 -0300139 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300140 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300141 struct input_dev *touch; /* input device for touchscreen */
142
Jarod Wilson693508d2010-09-15 15:56:03 -0300143 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300144 u32 kc; /* current input keycode */
145 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300146 u32 rc_scancode; /* the computed remote scancode */
147 u8 rc_toggle; /* the computed remote toggle bit */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300148 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300149 bool release_code; /* some keys send a release code */
150
151 u8 display_type; /* store the display type */
152 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
153
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300154 char name_rdev[128]; /* rc input device name */
155 char phys_rdev[64]; /* rc input device phys path */
156
Jarod Wilson21677cf2010-04-16 18:29:02 -0300157 char name_idev[128]; /* input device name */
158 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300159
160 char name_touch[128]; /* touch screen name */
161 char phys_touch[64]; /* touch screen phys path */
162 struct timer_list ttimer; /* touch screen timer */
163 int touch_x; /* x coordinate on touchscreen */
164 int touch_y; /* y coordinate on touchscreen */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300165 struct imon_usb_dev_descr *dev_descr; /* device description with key
166 table for front panels */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300167};
168
169#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300170
171/* vfd character device file operations */
172static const struct file_operations vfd_fops = {
173 .owner = THIS_MODULE,
174 .open = &display_open,
175 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200176 .release = &display_close,
177 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300178};
179
180/* lcd character device file operations */
181static const struct file_operations lcd_fops = {
182 .owner = THIS_MODULE,
183 .open = &display_open,
184 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200185 .release = &display_close,
186 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300187};
188
189enum {
190 IMON_DISPLAY_TYPE_AUTO = 0,
191 IMON_DISPLAY_TYPE_VFD = 1,
192 IMON_DISPLAY_TYPE_LCD = 2,
193 IMON_DISPLAY_TYPE_VGA = 3,
194 IMON_DISPLAY_TYPE_NONE = 4,
195};
196
197enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300198 IMON_KEY_IMON = 0,
199 IMON_KEY_MCE = 1,
200 IMON_KEY_PANEL = 2,
201};
202
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300203static struct usb_class_driver imon_vfd_class = {
204 .name = DEVICE_NAME,
205 .fops = &vfd_fops,
206 .minor_base = DISPLAY_MINOR_BASE,
207};
208
209static struct usb_class_driver imon_lcd_class = {
210 .name = DEVICE_NAME,
211 .fops = &lcd_fops,
212 .minor_base = DISPLAY_MINOR_BASE,
213};
214
215/* imon receiver front panel/knob key table */
216static const struct imon_usb_dev_descr imon_default_table = {
217 .flags = IMON_NO_FLAGS,
218 .key_table = {
219 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
220 { 0x000000001200ffeell, KEY_UP },
221 { 0x000000001300ffeell, KEY_DOWN },
222 { 0x000000001400ffeell, KEY_LEFT },
223 { 0x000000001500ffeell, KEY_RIGHT },
224 { 0x000000001600ffeell, KEY_ENTER },
225 { 0x000000001700ffeell, KEY_ESC },
226 { 0x000000001f00ffeell, KEY_AUDIO },
227 { 0x000000002000ffeell, KEY_VIDEO },
228 { 0x000000002100ffeell, KEY_CAMERA },
229 { 0x000000002700ffeell, KEY_DVD },
230 { 0x000000002300ffeell, KEY_TV },
231 { 0x000000002b00ffeell, KEY_EXIT },
232 { 0x000000002c00ffeell, KEY_SELECT },
233 { 0x000000002d00ffeell, KEY_MENU },
234 { 0x000000000500ffeell, KEY_PREVIOUS },
235 { 0x000000000700ffeell, KEY_REWIND },
236 { 0x000000000400ffeell, KEY_STOP },
237 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
238 { 0x000000000800ffeell, KEY_FASTFORWARD },
239 { 0x000000000600ffeell, KEY_NEXT },
240 { 0x000000010000ffeell, KEY_RIGHT },
241 { 0x000001000000ffeell, KEY_LEFT },
242 { 0x000000003d00ffeell, KEY_SELECT },
243 { 0x000100000000ffeell, KEY_VOLUMEUP },
244 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
245 { 0x000000000100ffeell, KEY_MUTE },
246 /* 0xffdc iMON MCE VFD */
247 { 0x00010000ffffffeell, KEY_VOLUMEUP },
248 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
249 { 0x00000001ffffffeell, KEY_MUTE },
250 { 0x0000000fffffffeell, KEY_MEDIA },
251 { 0x00000012ffffffeell, KEY_UP },
252 { 0x00000013ffffffeell, KEY_DOWN },
253 { 0x00000014ffffffeell, KEY_LEFT },
254 { 0x00000015ffffffeell, KEY_RIGHT },
255 { 0x00000016ffffffeell, KEY_ENTER },
256 { 0x00000017ffffffeell, KEY_ESC },
257 /* iMON Knob values */
258 { 0x000100ffffffffeell, KEY_VOLUMEUP },
259 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
260 { 0x000008ffffffffeell, KEY_MUTE },
261 { 0, KEY_RESERVED },
262 }
263};
264
265static const struct imon_usb_dev_descr imon_OEM_VFD = {
266 .flags = IMON_NEED_20MS_PKT_DELAY,
267 .key_table = {
268 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
269 { 0x000000001200ffeell, KEY_UP },
270 { 0x000000001300ffeell, KEY_DOWN },
271 { 0x000000001400ffeell, KEY_LEFT },
272 { 0x000000001500ffeell, KEY_RIGHT },
273 { 0x000000001600ffeell, KEY_ENTER },
274 { 0x000000001700ffeell, KEY_ESC },
275 { 0x000000001f00ffeell, KEY_AUDIO },
276 { 0x000000002b00ffeell, KEY_EXIT },
277 { 0x000000002c00ffeell, KEY_SELECT },
278 { 0x000000002d00ffeell, KEY_MENU },
279 { 0x000000000500ffeell, KEY_PREVIOUS },
280 { 0x000000000700ffeell, KEY_REWIND },
281 { 0x000000000400ffeell, KEY_STOP },
282 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
283 { 0x000000000800ffeell, KEY_FASTFORWARD },
284 { 0x000000000600ffeell, KEY_NEXT },
285 { 0x000000010000ffeell, KEY_RIGHT },
286 { 0x000001000000ffeell, KEY_LEFT },
287 { 0x000000003d00ffeell, KEY_SELECT },
288 { 0x000100000000ffeell, KEY_VOLUMEUP },
289 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
290 { 0x000000000100ffeell, KEY_MUTE },
291 /* 0xffdc iMON MCE VFD */
292 { 0x00010000ffffffeell, KEY_VOLUMEUP },
293 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
294 { 0x00000001ffffffeell, KEY_MUTE },
295 { 0x0000000fffffffeell, KEY_MEDIA },
296 { 0x00000012ffffffeell, KEY_UP },
297 { 0x00000013ffffffeell, KEY_DOWN },
298 { 0x00000014ffffffeell, KEY_LEFT },
299 { 0x00000015ffffffeell, KEY_RIGHT },
300 { 0x00000016ffffffeell, KEY_ENTER },
301 { 0x00000017ffffffeell, KEY_ESC },
302 /* iMON Knob values */
303 { 0x000100ffffffffeell, KEY_VOLUMEUP },
304 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
305 { 0x000008ffffffffeell, KEY_MUTE },
306 { 0, KEY_RESERVED },
307 }
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300308};
309
Ulrich Eckhardt7b5fc072014-07-26 14:59:07 -0300310/* imon receiver front panel/knob key table for DH102*/
311static const struct imon_usb_dev_descr imon_DH102 = {
312 .flags = IMON_NO_FLAGS,
313 .key_table = {
314 { 0x000100000000ffeell, KEY_VOLUMEUP },
315 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
316 { 0x000000010000ffeell, KEY_MUTE },
317 { 0x0000000f0000ffeell, KEY_MEDIA },
318 { 0x000000120000ffeell, KEY_UP },
319 { 0x000000130000ffeell, KEY_DOWN },
320 { 0x000000140000ffeell, KEY_LEFT },
321 { 0x000000150000ffeell, KEY_RIGHT },
322 { 0x000000160000ffeell, KEY_ENTER },
323 { 0x000000170000ffeell, KEY_ESC },
324 { 0x0000002b0000ffeell, KEY_EXIT },
325 { 0x0000002c0000ffeell, KEY_SELECT },
326 { 0x0000002d0000ffeell, KEY_MENU },
327 { 0, KEY_RESERVED }
328 }
329};
330
Jarod Wilson21677cf2010-04-16 18:29:02 -0300331/*
332 * USB Device ID for iMON USB Control Boards
333 *
334 * The Windows drivers contain 6 different inf files, more or less one for
335 * each new device until the 0x0034-0x0046 devices, which all use the same
336 * driver. Some of the devices in the 34-46 range haven't been definitively
337 * identified yet. Early devices have either a TriGem Computer, Inc. or a
338 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
339 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
340 * the ffdc and later devices, which do onboard decoding.
341 */
342static struct usb_device_id imon_usb_id_table[] = {
343 /*
344 * Several devices with this same device ID, all use iMON_PAD.inf
345 * SoundGraph iMON PAD (IR & VFD)
346 * SoundGraph iMON PAD (IR & LCD)
347 * SoundGraph iMON Knob (IR only)
348 */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300349 { USB_DEVICE(0x15c2, 0xffdc),
350 .driver_info = (unsigned long)&imon_default_table },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300351
352 /*
353 * Newer devices, all driven by the latest iMON Windows driver, full
354 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
355 * Need user input to fill in details on unknown devices.
356 */
357 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300358 { USB_DEVICE(0x15c2, 0x0034),
Ulrich Eckhardt7b5fc072014-07-26 14:59:07 -0300359 .driver_info = (unsigned long)&imon_DH102 },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300360 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300361 { USB_DEVICE(0x15c2, 0x0035),
362 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300363 /* SoundGraph iMON OEM VFD (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300364 { USB_DEVICE(0x15c2, 0x0036),
365 .driver_info = (unsigned long)&imon_OEM_VFD },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300366 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300367 { USB_DEVICE(0x15c2, 0x0037),
368 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300369 /* SoundGraph iMON OEM LCD (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300370 { USB_DEVICE(0x15c2, 0x0038),
371 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300372 /* SoundGraph iMON UltraBay (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300373 { USB_DEVICE(0x15c2, 0x0039),
374 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300375 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300376 { USB_DEVICE(0x15c2, 0x003a),
377 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300378 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300379 { USB_DEVICE(0x15c2, 0x003b),
380 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300381 /* SoundGraph iMON OEM Inside (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300382 { USB_DEVICE(0x15c2, 0x003c),
383 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300384 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300385 { USB_DEVICE(0x15c2, 0x003d),
386 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300387 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300388 { USB_DEVICE(0x15c2, 0x003e),
389 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300390 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300391 { USB_DEVICE(0x15c2, 0x003f),
392 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300393 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300394 { USB_DEVICE(0x15c2, 0x0040),
395 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300396 /* SoundGraph iMON MINI (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300397 { USB_DEVICE(0x15c2, 0x0041),
398 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300399 /* Antec Veris Multimedia Station EZ External (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300400 { USB_DEVICE(0x15c2, 0x0042),
401 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300402 /* Antec Veris Multimedia Station Basic Internal (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300403 { USB_DEVICE(0x15c2, 0x0043),
404 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300405 /* Antec Veris Multimedia Station Elite (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300406 { USB_DEVICE(0x15c2, 0x0044),
407 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300408 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300409 { USB_DEVICE(0x15c2, 0x0045),
410 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300411 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300412 { USB_DEVICE(0x15c2, 0x0046),
413 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300414 {}
415};
416
417/* USB Device data */
418static struct usb_driver imon_driver = {
419 .name = MOD_NAME,
420 .probe = imon_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800421 .disconnect = imon_disconnect,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300422 .suspend = imon_suspend,
423 .resume = imon_resume,
424 .id_table = imon_usb_id_table,
425};
426
Jarod Wilson21677cf2010-04-16 18:29:02 -0300427/* to prevent races between open() and disconnect(), probing, etc */
428static DEFINE_MUTEX(driver_lock);
429
430/* Module bookkeeping bits */
431MODULE_AUTHOR(MOD_AUTHOR);
432MODULE_DESCRIPTION(MOD_DESC);
433MODULE_VERSION(MOD_VERSION);
434MODULE_LICENSE("GPL");
435MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
436
437static bool debug;
438module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300439MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300440
441/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
442static int display_type;
443module_param(display_type, int, S_IRUGO);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200444MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, 1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300445
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300446static int pad_stabilize = 1;
447module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200448MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300449
450/*
451 * In certain use cases, mouse mode isn't really helpful, and could actually
452 * cause confusion, so allow disabling it when the IR device is open.
453 */
454static bool nomouse;
455module_param(nomouse, bool, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200456MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is open. 0=don't disable, 1=disable. (default: don't disable)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300457
458/* threshold at which a pad push registers as an arrow key in kbd mode */
459static int pad_thresh;
460module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200461MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an arrow key in kbd mode (default: 28)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300462
463
464static void free_imon_context(struct imon_context *ictx)
465{
466 struct device *dev = ictx->dev;
467
468 usb_free_urb(ictx->tx_urb);
469 usb_free_urb(ictx->rx_urb_intf0);
470 usb_free_urb(ictx->rx_urb_intf1);
471 kfree(ictx);
472
473 dev_dbg(dev, "%s: iMON context freed\n", __func__);
474}
475
476/**
477 * Called when the Display device (e.g. /dev/lcd0)
478 * is opened by the application.
479 */
480static int display_open(struct inode *inode, struct file *file)
481{
482 struct usb_interface *interface;
483 struct imon_context *ictx = NULL;
484 int subminor;
485 int retval = 0;
486
487 /* prevent races with disconnect */
488 mutex_lock(&driver_lock);
489
490 subminor = iminor(inode);
491 interface = usb_find_interface(&imon_driver, subminor);
492 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300493 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300494 retval = -ENODEV;
495 goto exit;
496 }
497 ictx = usb_get_intfdata(interface);
498
499 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300500 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300501 retval = -ENODEV;
502 goto exit;
503 }
504
505 mutex_lock(&ictx->lock);
506
507 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300508 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300509 retval = -ENODEV;
510 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300511 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300512 retval = -EBUSY;
513 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300514 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300515 file->private_data = ictx;
516 dev_dbg(ictx->dev, "display port opened\n");
517 }
518
519 mutex_unlock(&ictx->lock);
520
521exit:
522 mutex_unlock(&driver_lock);
523 return retval;
524}
525
526/**
527 * Called when the display device (e.g. /dev/lcd0)
528 * is closed by the application.
529 */
530static int display_close(struct inode *inode, struct file *file)
531{
532 struct imon_context *ictx = NULL;
533 int retval = 0;
534
Joe Perchesabf84382010-07-12 17:50:03 -0300535 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300536
537 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300538 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300539 return -ENODEV;
540 }
541
542 mutex_lock(&ictx->lock);
543
544 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300545 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300546 retval = -ENODEV;
547 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300548 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300549 retval = -EIO;
550 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300551 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300552 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300553 }
554
555 mutex_unlock(&ictx->lock);
556 return retval;
557}
558
559/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300560 * Sends a packet to the device -- this function must be called with
561 * ictx->lock held, or its unlock/lock sequence while waiting for tx
562 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300563 */
564static int send_packet(struct imon_context *ictx)
565{
566 unsigned int pipe;
567 unsigned long timeout;
568 int interval = 0;
569 int retval = 0;
570 struct usb_ctrlrequest *control_req = NULL;
571
572 /* Check if we need to use control or interrupt urb */
573 if (!ictx->tx_control) {
574 pipe = usb_sndintpipe(ictx->usbdev_intf0,
575 ictx->tx_endpoint->bEndpointAddress);
576 interval = ictx->tx_endpoint->bInterval;
577
578 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
579 ictx->usb_tx_buf,
580 sizeof(ictx->usb_tx_buf),
581 usb_tx_callback, ictx, interval);
582
583 ictx->tx_urb->actual_length = 0;
584 } else {
585 /* fill request into kmalloc'ed space: */
586 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
587 GFP_KERNEL);
588 if (control_req == NULL)
589 return -ENOMEM;
590
591 /* setup packet is '21 09 0200 0001 0008' */
592 control_req->bRequestType = 0x21;
593 control_req->bRequest = 0x09;
594 control_req->wValue = cpu_to_le16(0x0200);
595 control_req->wIndex = cpu_to_le16(0x0001);
596 control_req->wLength = cpu_to_le16(0x0008);
597
598 /* control pipe is endpoint 0x00 */
599 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
600
601 /* build the control urb */
602 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
603 pipe, (unsigned char *)control_req,
604 ictx->usb_tx_buf,
605 sizeof(ictx->usb_tx_buf),
606 usb_tx_callback, ictx);
607 ictx->tx_urb->actual_length = 0;
608 }
609
Daniel Wagner7c073ff2016-09-16 08:18:21 -0300610 reinit_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300611 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300612 smp_rmb(); /* ensure later readers know we're busy */
613
614 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
615 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300616 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300617 smp_rmb(); /* ensure later readers know we're not busy */
Jarod Wilson47a09b02011-07-14 14:20:46 -0300618 pr_err_ratelimited("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300619 } else {
620 /* Wait for transmission to complete (or abort) */
621 mutex_unlock(&ictx->lock);
622 retval = wait_for_completion_interruptible(
623 &ictx->tx.finished);
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300624 if (retval) {
625 usb_kill_urb(ictx->tx_urb);
Jarod Wilson47a09b02011-07-14 14:20:46 -0300626 pr_err_ratelimited("task interrupted\n");
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300627 }
Jarod Wilson21677cf2010-04-16 18:29:02 -0300628 mutex_lock(&ictx->lock);
629
630 retval = ictx->tx.status;
631 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300632 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300633 }
634
635 kfree(control_req);
636
637 /*
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300638 * Induce a mandatory delay before returning, as otherwise,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300639 * send_packet can get called so rapidly as to overwhelm the device,
640 * particularly on faster systems and/or those with quirky usb.
641 */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300642 timeout = msecs_to_jiffies(ictx->send_packet_delay);
643 set_current_state(TASK_INTERRUPTIBLE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300644 schedule_timeout(timeout);
645
646 return retval;
647}
648
649/**
650 * Sends an associate packet to the iMON 2.4G.
651 *
652 * This might not be such a good idea, since it has an id collision with
653 * some versions of the "IR & VFD" combo. The only way to determine if it
654 * is an RF version is to look at the product description string. (Which
655 * we currently do not fetch).
656 */
657static int send_associate_24g(struct imon_context *ictx)
658{
659 int retval;
660 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
661 0x00, 0x00, 0x00, 0x20 };
662
663 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300664 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300665 return -ENODEV;
666 }
667
668 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300669 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300670 return -ENODEV;
671 }
672
673 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
674 retval = send_packet(ictx);
675
676 return retval;
677}
678
679/**
680 * Sends packets to setup and show clock on iMON display
681 *
682 * Arguments: year - last 2 digits of year, month - 1..12,
683 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
684 * hour - 0..23, minute - 0..59, second - 0..59
685 */
686static int send_set_imon_clock(struct imon_context *ictx,
687 unsigned int year, unsigned int month,
688 unsigned int day, unsigned int dow,
689 unsigned int hour, unsigned int minute,
690 unsigned int second)
691{
692 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
693 int retval = 0;
694 int i;
695
696 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300697 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300698 return -ENODEV;
699 }
700
701 switch (ictx->display_type) {
702 case IMON_DISPLAY_TYPE_LCD:
703 clock_enable_pkt[0][0] = 0x80;
704 clock_enable_pkt[0][1] = year;
705 clock_enable_pkt[0][2] = month-1;
706 clock_enable_pkt[0][3] = day;
707 clock_enable_pkt[0][4] = hour;
708 clock_enable_pkt[0][5] = minute;
709 clock_enable_pkt[0][6] = second;
710
711 clock_enable_pkt[1][0] = 0x80;
712 clock_enable_pkt[1][1] = 0;
713 clock_enable_pkt[1][2] = 0;
714 clock_enable_pkt[1][3] = 0;
715 clock_enable_pkt[1][4] = 0;
716 clock_enable_pkt[1][5] = 0;
717 clock_enable_pkt[1][6] = 0;
718
719 if (ictx->product == 0xffdc) {
720 clock_enable_pkt[0][7] = 0x50;
721 clock_enable_pkt[1][7] = 0x51;
722 } else {
723 clock_enable_pkt[0][7] = 0x88;
724 clock_enable_pkt[1][7] = 0x8a;
725 }
726
727 break;
728
729 case IMON_DISPLAY_TYPE_VFD:
730 clock_enable_pkt[0][0] = year;
731 clock_enable_pkt[0][1] = month-1;
732 clock_enable_pkt[0][2] = day;
733 clock_enable_pkt[0][3] = dow;
734 clock_enable_pkt[0][4] = hour;
735 clock_enable_pkt[0][5] = minute;
736 clock_enable_pkt[0][6] = second;
737 clock_enable_pkt[0][7] = 0x40;
738
739 clock_enable_pkt[1][0] = 0;
740 clock_enable_pkt[1][1] = 0;
741 clock_enable_pkt[1][2] = 1;
742 clock_enable_pkt[1][3] = 0;
743 clock_enable_pkt[1][4] = 0;
744 clock_enable_pkt[1][5] = 0;
745 clock_enable_pkt[1][6] = 0;
746 clock_enable_pkt[1][7] = 0x42;
747
748 break;
749
750 default:
751 return -ENODEV;
752 }
753
754 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
755 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
756 retval = send_packet(ictx);
757 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300758 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300759 break;
760 }
761 }
762
763 return retval;
764}
765
766/**
767 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
768 */
769static ssize_t show_associate_remote(struct device *d,
770 struct device_attribute *attr,
771 char *buf)
772{
773 struct imon_context *ictx = dev_get_drvdata(d);
774
775 if (!ictx)
776 return -ENODEV;
777
778 mutex_lock(&ictx->lock);
779 if (ictx->rf_isassociating)
780 strcpy(buf, "associating\n");
781 else
782 strcpy(buf, "closed\n");
783
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200784 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");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300785 mutex_unlock(&ictx->lock);
786 return strlen(buf);
787}
788
789static ssize_t store_associate_remote(struct device *d,
790 struct device_attribute *attr,
791 const char *buf, size_t count)
792{
793 struct imon_context *ictx;
794
795 ictx = dev_get_drvdata(d);
796
797 if (!ictx)
798 return -ENODEV;
799
800 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300801 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300802 send_associate_24g(ictx);
803 mutex_unlock(&ictx->lock);
804
805 return count;
806}
807
808/**
809 * sysfs functions to control internal imon clock
810 */
811static ssize_t show_imon_clock(struct device *d,
812 struct device_attribute *attr, char *buf)
813{
814 struct imon_context *ictx = dev_get_drvdata(d);
815 size_t len;
816
817 if (!ictx)
818 return -ENODEV;
819
820 mutex_lock(&ictx->lock);
821
822 if (!ictx->display_supported) {
823 len = snprintf(buf, PAGE_SIZE, "Not supported.");
824 } else {
825 len = snprintf(buf, PAGE_SIZE,
826 "To set the clock on your iMON display:\n"
827 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
828 "%s", ictx->display_isopen ?
829 "\nNOTE: imon device must be closed\n" : "");
830 }
831
832 mutex_unlock(&ictx->lock);
833
834 return len;
835}
836
837static ssize_t store_imon_clock(struct device *d,
838 struct device_attribute *attr,
839 const char *buf, size_t count)
840{
841 struct imon_context *ictx = dev_get_drvdata(d);
842 ssize_t retval;
843 unsigned int year, month, day, dow, hour, minute, second;
844
845 if (!ictx)
846 return -ENODEV;
847
848 mutex_lock(&ictx->lock);
849
850 if (!ictx->display_supported) {
851 retval = -ENODEV;
852 goto exit;
853 } else if (ictx->display_isopen) {
854 retval = -EBUSY;
855 goto exit;
856 }
857
858 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
859 &hour, &minute, &second) != 7) {
860 retval = -EINVAL;
861 goto exit;
862 }
863
864 if ((month < 1 || month > 12) ||
865 (day < 1 || day > 31) || (dow > 6) ||
866 (hour > 23) || (minute > 59) || (second > 59)) {
867 retval = -EINVAL;
868 goto exit;
869 }
870
871 retval = send_set_imon_clock(ictx, year, month, day, dow,
872 hour, minute, second);
873 if (retval)
874 goto exit;
875
876 retval = count;
877exit:
878 mutex_unlock(&ictx->lock);
879
880 return retval;
881}
882
883
884static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
885 store_imon_clock);
886
887static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
888 store_associate_remote);
889
890static struct attribute *imon_display_sysfs_entries[] = {
891 &dev_attr_imon_clock.attr,
892 NULL
893};
894
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300895static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300896 .attrs = imon_display_sysfs_entries
897};
898
899static struct attribute *imon_rf_sysfs_entries[] = {
900 &dev_attr_associate_remote.attr,
901 NULL
902};
903
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300904static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300905 .attrs = imon_rf_sysfs_entries
906};
907
908/**
909 * Writes data to the VFD. The iMON VFD is 2x16 characters
910 * and requires data in 5 consecutive USB interrupt packets,
911 * each packet but the last carrying 7 bytes.
912 *
913 * I don't know if the VFD board supports features such as
914 * scrolling, clearing rows, blanking, etc. so at
915 * the caller must provide a full screen of data. If fewer
916 * than 32 bytes are provided spaces will be appended to
917 * generate a full screen.
918 */
David Härdemand6740d82014-04-03 20:34:53 -0300919static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300920 size_t n_bytes, loff_t *pos)
921{
922 int i;
923 int offset;
924 int seq;
925 int retval = 0;
926 struct imon_context *ictx;
927 const unsigned char vfd_packet6[] = {
928 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
929
Joe Perchesabf84382010-07-12 17:50:03 -0300930 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300931 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300932 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300933 return -ENODEV;
934 }
935
936 mutex_lock(&ictx->lock);
937
938 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300939 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300940 retval = -ENODEV;
941 goto exit;
942 }
943
944 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300945 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300946 retval = -EINVAL;
947 goto exit;
948 }
949
950 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
951 retval = -EFAULT;
952 goto exit;
953 }
954
955 /* Pad with spaces */
956 for (i = n_bytes; i < 32; ++i)
957 ictx->tx.data_buf[i] = ' ';
958
959 for (i = 32; i < 35; ++i)
960 ictx->tx.data_buf[i] = 0xFF;
961
962 offset = 0;
963 seq = 0;
964
965 do {
966 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
967 ictx->usb_tx_buf[7] = (unsigned char) seq;
968
969 retval = send_packet(ictx);
970 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300971 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300972 goto exit;
973 } else {
974 seq += 2;
975 offset += 7;
976 }
977
978 } while (offset < 35);
979
980 /* Send packet #6 */
981 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
982 ictx->usb_tx_buf[7] = (unsigned char) seq;
983 retval = send_packet(ictx);
984 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300985 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300986
987exit:
988 mutex_unlock(&ictx->lock);
989
990 return (!retval) ? n_bytes : retval;
991}
992
993/**
994 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
995 * packets. We accept data as 16 hexadecimal digits, followed by a
996 * newline (to make it easy to drive the device from a command-line
997 * -- even though the actual binary data is a bit complicated).
998 *
999 * The device itself is not a "traditional" text-mode display. It's
1000 * actually a 16x96 pixel bitmap display. That means if you want to
1001 * display text, you've got to have your own "font" and translate the
1002 * text into bitmaps for display. This is really flexible (you can
1003 * display whatever diacritics you need, and so on), but it's also
1004 * a lot more complicated than most LCDs...
1005 */
David Härdemand6740d82014-04-03 20:34:53 -03001006static ssize_t lcd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001007 size_t n_bytes, loff_t *pos)
1008{
1009 int retval = 0;
1010 struct imon_context *ictx;
1011
Joe Perchesabf84382010-07-12 17:50:03 -03001012 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001013 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001014 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001015 return -ENODEV;
1016 }
1017
1018 mutex_lock(&ictx->lock);
1019
1020 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001021 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001022 retval = -ENODEV;
1023 goto exit;
1024 }
1025
1026 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001027 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1028 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001029 retval = -EINVAL;
1030 goto exit;
1031 }
1032
1033 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1034 retval = -EFAULT;
1035 goto exit;
1036 }
1037
1038 retval = send_packet(ictx);
1039 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001040 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001041 goto exit;
1042 } else {
1043 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1044 __func__, (int) n_bytes);
1045 }
1046exit:
1047 mutex_unlock(&ictx->lock);
1048 return (!retval) ? n_bytes : retval;
1049}
1050
1051/**
1052 * Callback function for USB core API: transmit data
1053 */
1054static void usb_tx_callback(struct urb *urb)
1055{
1056 struct imon_context *ictx;
1057
1058 if (!urb)
1059 return;
1060 ictx = (struct imon_context *)urb->context;
1061 if (!ictx)
1062 return;
1063
1064 ictx->tx.status = urb->status;
1065
1066 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001067 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001068 smp_rmb(); /* ensure later readers know we're not busy */
1069 complete(&ictx->tx.finished);
1070}
1071
1072/**
Jarod Wilson21677cf2010-04-16 18:29:02 -03001073 * report touchscreen input
1074 */
1075static void imon_touch_display_timeout(unsigned long data)
1076{
1077 struct imon_context *ictx = (struct imon_context *)data;
1078
Dan Carpenterf03900d2010-05-04 08:37:33 -03001079 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001080 return;
1081
1082 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1083 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1084 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1085 input_sync(ictx->touch);
1086}
1087
1088/**
1089 * iMON IR receivers support two different signal sets -- those used by
1090 * the iMON remotes, and those used by the Windows MCE remotes (which is
1091 * really just RC-6), but only one or the other at a time, as the signals
1092 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -03001093 *
1094 * This function gets called two different ways, one way is from
1095 * rc_register_device, for initial protocol selection/setup, and the other is
1096 * via a userspace-initiated protocol change request, either by direct sysfs
1097 * prodding or by something like ir-keytable. In the rc_register_device case,
1098 * the imon context lock is already held, but when initiated from userspace,
1099 * it is not, so we must acquire it prior to calling send_packet, which
1100 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001101 */
David Härdemanc003ab12012-10-11 19:11:54 -03001102static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001103{
1104 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001105 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001106 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001107 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001108 unsigned char ir_proto_packet[] = {
1109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1110
David Härdemanc5540fb2014-04-03 20:32:21 -03001111 if (*rc_type && !(*rc_type & rc->allowed_protocols))
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001112 dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001113
David Härdemanc003ab12012-10-11 19:11:54 -03001114 if (*rc_type & RC_BIT_RC6_MCE) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001115 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1116 ir_proto_packet[0] = 0x01;
David Härdemanc003ab12012-10-11 19:11:54 -03001117 *rc_type = RC_BIT_RC6_MCE;
1118 } else if (*rc_type & RC_BIT_OTHER) {
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001119 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001120 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001121 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001122 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001123 *rc_type = RC_BIT_OTHER;
1124 } else {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001125 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001126 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001127 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001128 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001129 *rc_type = RC_BIT_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001130 }
1131
1132 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1133
Jarod Wilson23ef7102011-04-27 19:01:44 -03001134 if (!mutex_is_locked(&ictx->lock)) {
1135 unlock = true;
1136 mutex_lock(&ictx->lock);
1137 }
1138
Jarod Wilson21677cf2010-04-16 18:29:02 -03001139 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001140 if (retval)
1141 goto out;
1142
David Härdemanc003ab12012-10-11 19:11:54 -03001143 ictx->rc_type = *rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001144 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001145
1146out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001147 if (unlock)
1148 mutex_unlock(&ictx->lock);
1149
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001150 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001151}
1152
1153static inline int tv2int(const struct timeval *a, const struct timeval *b)
1154{
1155 int usecs = 0;
1156 int sec = 0;
1157
1158 if (b->tv_usec > a->tv_usec) {
1159 usecs = 1000000;
1160 sec--;
1161 }
1162
1163 usecs += a->tv_usec - b->tv_usec;
1164
1165 sec += a->tv_sec - b->tv_sec;
1166 sec *= 1000;
1167 usecs /= 1000;
1168 sec += usecs;
1169
1170 if (sec < 0)
1171 sec = 1000;
1172
1173 return sec;
1174}
1175
1176/**
1177 * The directional pad behaves a bit differently, depending on whether this is
1178 * one of the older ffdc devices or a newer device. Newer devices appear to
1179 * have a higher resolution matrix for more precise mouse movement, but it
1180 * makes things overly sensitive in keyboard mode, so we do some interesting
1181 * contortions to make it less touchy. Older devices run through the same
1182 * routine with shorter timeout and a smaller threshold.
1183 */
1184static int stabilize(int a, int b, u16 timeout, u16 threshold)
1185{
1186 struct timeval ct;
1187 static struct timeval prev_time = {0, 0};
1188 static struct timeval hit_time = {0, 0};
1189 static int x, y, prev_result, hits;
1190 int result = 0;
1191 int msec, msec_hit;
1192
1193 do_gettimeofday(&ct);
1194 msec = tv2int(&ct, &prev_time);
1195 msec_hit = tv2int(&ct, &hit_time);
1196
1197 if (msec > 100) {
1198 x = 0;
1199 y = 0;
1200 hits = 0;
1201 }
1202
1203 x += a;
1204 y += b;
1205
1206 prev_time = ct;
1207
1208 if (abs(x) > threshold || abs(y) > threshold) {
1209 if (abs(y) > abs(x))
1210 result = (y > 0) ? 0x7F : 0x80;
1211 else
1212 result = (x > 0) ? 0x7F00 : 0x8000;
1213
1214 x = 0;
1215 y = 0;
1216
1217 if (result == prev_result) {
1218 hits++;
1219
1220 if (hits > 3) {
1221 switch (result) {
1222 case 0x7F:
1223 y = 17 * threshold / 30;
1224 break;
1225 case 0x80:
1226 y -= 17 * threshold / 30;
1227 break;
1228 case 0x7F00:
1229 x = 17 * threshold / 30;
1230 break;
1231 case 0x8000:
1232 x -= 17 * threshold / 30;
1233 break;
1234 }
1235 }
1236
1237 if (hits == 2 && msec_hit < timeout) {
1238 result = 0;
1239 hits = 1;
1240 }
1241 } else {
1242 prev_result = result;
1243 hits = 1;
1244 hit_time = ct;
1245 }
1246 }
1247
1248 return result;
1249}
1250
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001251static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001252{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001253 u32 keycode;
1254 u32 release;
1255 bool is_release_code = false;
1256
1257 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001258 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001259 ictx->rc_toggle = 0x0;
1260 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001261
1262 /* Look for the release of a button */
1263 if (keycode == KEY_RESERVED) {
1264 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001265 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001266 if (keycode != KEY_RESERVED)
1267 is_release_code = true;
1268 }
1269
1270 ictx->release_code = is_release_code;
1271
1272 return keycode;
1273}
1274
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001275static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001276{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001277 u32 keycode;
1278
1279#define MCE_KEY_MASK 0x7000
1280#define MCE_TOGGLE_BIT 0x8000
1281
1282 /*
1283 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1284 * (the toggle bit flipping between alternating key presses), while
1285 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1286 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1287 * but we can't or them into all codes, as some keys are decoded in
1288 * a different way w/o the same use of the toggle bit...
1289 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001290 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001291 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1292
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001293 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001294 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001295
1296 /* not used in mce mode, but make sure we know its false */
1297 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001298
1299 return keycode;
1300}
1301
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001302static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001303{
1304 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001305 u32 keycode = KEY_RESERVED;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001306 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001307
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001308 for (i = 0; key_table[i].hw_code != 0; i++) {
1309 if (key_table[i].hw_code == (code | 0xffee)) {
1310 keycode = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001311 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001312 }
1313 }
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001314 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001315 return keycode;
1316}
1317
1318static bool imon_mouse_event(struct imon_context *ictx,
1319 unsigned char *buf, int len)
1320{
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001321 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001322 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001323 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001324 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001325 unsigned long flags;
1326
1327 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001328
1329 /* newer iMON device PAD or mouse button */
1330 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1331 rel_x = buf[2];
1332 rel_y = buf[3];
1333 right_shift = 1;
1334 /* 0xffdc iMON PAD or mouse button input */
1335 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1336 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1337 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1338 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1339 if (buf[0] & 0x02)
1340 rel_x |= ~0x0f;
1341 rel_x = rel_x + rel_x / 2;
1342 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1343 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1344 if (buf[0] & 0x01)
1345 rel_y |= ~0x0f;
1346 rel_y = rel_y + rel_y / 2;
1347 right_shift = 2;
1348 /* some ffdc devices decode mouse buttons differently... */
1349 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1350 right_shift = 2;
1351 /* ch+/- buttons, which we use for an emulated scroll wheel */
1352 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1353 dir = 1;
1354 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1355 dir = -1;
1356 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001357 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001358
Jarod Wilson693508d2010-09-15 15:56:03 -03001359 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1360
Jarod Wilson21677cf2010-04-16 18:29:02 -03001361 if (mouse_input) {
1362 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1363
1364 if (dir) {
1365 input_report_rel(ictx->idev, REL_WHEEL, dir);
1366 } else if (rel_x || rel_y) {
1367 input_report_rel(ictx->idev, REL_X, rel_x);
1368 input_report_rel(ictx->idev, REL_Y, rel_y);
1369 } else {
1370 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1371 input_report_key(ictx->idev, BTN_RIGHT,
1372 buf[1] >> right_shift & 0x1);
1373 }
1374 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001375 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001376 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001377 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001378 }
1379
1380 return mouse_input;
1381}
1382
1383static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1384{
1385 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1386 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1387 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1388 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1389 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1390 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1391 input_sync(ictx->touch);
1392}
1393
1394static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1395{
1396 int dir = 0;
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001397 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001398 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001399 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001400 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001401
1402 /*
1403 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1404 * contain a position coordinate (x,y), with each component ranging
1405 * from -14 to 14. We want to down-sample this to only 4 discrete values
1406 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001407 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001408 * ignore when they get too close.
1409 */
1410 if (ictx->product != 0xffdc) {
1411 /* first, pad to 8 bytes so it conforms with everything else */
1412 buf[5] = buf[6] = buf[7] = 0;
1413 timeout = 500; /* in msecs */
1414 /* (2*threshold) x (2*threshold) square */
1415 threshold = pad_thresh ? pad_thresh : 28;
1416 rel_x = buf[2];
1417 rel_y = buf[3];
1418
David Härdemanc003ab12012-10-11 19:11:54 -03001419 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001420 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1421 dir = stabilize((int)rel_x, (int)rel_y,
1422 timeout, threshold);
1423 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001424 spin_lock_irqsave(&ictx->kc_lock,
1425 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001426 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001427 spin_unlock_irqrestore(&ictx->kc_lock,
1428 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001429 return;
1430 }
1431 buf[2] = dir & 0xFF;
1432 buf[3] = (dir >> 8) & 0xFF;
Hans Verkuild778d252014-08-20 19:34:33 -03001433 scancode = be32_to_cpu(*((__be32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001434 }
1435 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001436 /*
1437 * Hack alert: instead of using keycodes, we have
1438 * to use hard-coded scancodes here...
1439 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001440 if (abs(rel_y) > abs(rel_x)) {
1441 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1442 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001443 if (rel_y > 0)
1444 scancode = 0x01007f00; /* KEY_DOWN */
1445 else
1446 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001447 } else {
1448 buf[2] = 0;
1449 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001450 if (rel_x > 0)
1451 scancode = 0x0100007f; /* KEY_RIGHT */
1452 else
1453 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001454 }
1455 }
1456
1457 /*
1458 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1459 * device (15c2:ffdc). The remote generates various codes from
1460 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1461 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1462 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001463 * reversed endianness. Extract direction from buffer, rotate endianness,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001464 * adjust sign and feed the values into stabilize(). The resulting codes
1465 * will be 0x01008000, 0x01007F00, which match the newer devices.
1466 */
1467 } else {
1468 timeout = 10; /* in msecs */
1469 /* (2*threshold) x (2*threshold) square */
1470 threshold = pad_thresh ? pad_thresh : 15;
1471
1472 /* buf[1] is x */
1473 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1474 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1475 if (buf[0] & 0x02)
1476 rel_x |= ~0x10+1;
1477 /* buf[2] is y */
1478 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1479 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1480 if (buf[0] & 0x01)
1481 rel_y |= ~0x10+1;
1482
1483 buf[0] = 0x01;
1484 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1485
David Härdemanc003ab12012-10-11 19:11:54 -03001486 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001487 dir = stabilize((int)rel_x, (int)rel_y,
1488 timeout, threshold);
1489 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001490 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001491 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001492 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001493 return;
1494 }
1495 buf[2] = dir & 0xFF;
1496 buf[3] = (dir >> 8) & 0xFF;
Hans Verkuild778d252014-08-20 19:34:33 -03001497 scancode = be32_to_cpu(*((__be32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001498 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001499 /*
1500 * Hack alert: instead of using keycodes, we have
1501 * to use hard-coded scancodes here...
1502 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001503 if (abs(rel_y) > abs(rel_x)) {
1504 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1505 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001506 if (rel_y > 0)
1507 scancode = 0x01007f00; /* KEY_DOWN */
1508 else
1509 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001510 } else {
1511 buf[2] = 0;
1512 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001513 if (rel_x > 0)
1514 scancode = 0x0100007f; /* KEY_RIGHT */
1515 else
1516 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001517 }
1518 }
1519 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001520
Jarod Wilson693508d2010-09-15 15:56:03 -03001521 if (scancode) {
1522 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001523 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001524 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1525 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001526}
1527
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001528/**
1529 * figure out if these is a press or a release. We don't actually
1530 * care about repeats, as those will be auto-generated within the IR
1531 * subsystem for repeating scancodes.
1532 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001533static int imon_parse_press_type(struct imon_context *ictx,
1534 unsigned char *buf, u8 ktype)
1535{
1536 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001537 unsigned long flags;
1538
1539 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001540
1541 /* key release of 0x02XXXXXX key */
1542 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1543 ictx->kc = ictx->last_keycode;
1544
1545 /* mouse button release on (some) 0xffdc devices */
1546 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1547 buf[2] == 0x81 && buf[3] == 0xb7)
1548 ictx->kc = ictx->last_keycode;
1549
1550 /* mouse button release on (some other) 0xffdc devices */
1551 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1552 buf[2] == 0x81 && buf[3] == 0xb7)
1553 ictx->kc = ictx->last_keycode;
1554
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001555 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001556 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001557 ictx->rc_toggle = buf[2];
1558 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001559
1560 /* incoherent or irrelevant data */
1561 } else if (ictx->kc == KEY_RESERVED)
1562 press_type = -EINVAL;
1563
1564 /* key release of 0xXXXXXXb7 key */
1565 else if (ictx->release_code)
1566 press_type = 0;
1567
1568 /* this is a button press */
1569 else
1570 press_type = 1;
1571
Jarod Wilson693508d2010-09-15 15:56:03 -03001572 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1573
Jarod Wilson21677cf2010-04-16 18:29:02 -03001574 return press_type;
1575}
1576
1577/**
1578 * Process the incoming packet
1579 */
1580static void imon_incoming_packet(struct imon_context *ictx,
1581 struct urb *urb, int intf)
1582{
1583 int len = urb->actual_length;
1584 unsigned char *buf = urb->transfer_buffer;
1585 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001586 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001587 u32 kc;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001588 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001589 int press_type = 0;
1590 int msec;
1591 struct timeval t;
1592 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001593 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001594
Jarod Wilson21677cf2010-04-16 18:29:02 -03001595 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001596 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001597 return;
1598
1599 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001600 if (len == 8 && buf[7] == 0xee) {
Hans Verkuild778d252014-08-20 19:34:33 -03001601 scancode = be64_to_cpu(*((__be64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001602 ktype = IMON_KEY_PANEL;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001603 kc = imon_panel_key_lookup(ictx, scancode);
Ulrich Eckhardt6ddc2be2014-07-26 15:01:12 -03001604 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001605 } else {
Hans Verkuild778d252014-08-20 19:34:33 -03001606 scancode = be32_to_cpu(*((__be32 *)buf));
David Härdemanc003ab12012-10-11 19:11:54 -03001607 if (ictx->rc_type == RC_BIT_RC6_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001608 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001609 if (buf[0] == 0x80)
1610 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001611 kc = imon_mce_key_lookup(ictx, scancode);
1612 } else {
1613 ktype = IMON_KEY_IMON;
1614 kc = imon_remote_key_lookup(ictx, scancode);
1615 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001616 }
1617
Jarod Wilson693508d2010-09-15 15:56:03 -03001618 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001619 /* keyboard/mouse mode toggle button */
1620 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1621 ictx->last_keycode = kc;
1622 if (!nomouse) {
1623 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1624 dev_dbg(dev, "toggling to %s mode\n",
1625 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001626 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001627 return;
1628 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001629 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001630 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1631 }
1632 }
1633
1634 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001635 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001636
1637 /* send touchscreen events through input subsystem if touchpad data */
1638 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1639 buf[7] == 0x86) {
1640 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001641 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001642
1643 /* look for mouse events with pad in mouse mode */
1644 } else if (ictx->pad_mouse) {
1645 if (imon_mouse_event(ictx, buf, len))
1646 return;
1647 }
1648
1649 /* Now for some special handling to convert pad input to arrow keys */
1650 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1651 ((len == 8) && (buf[0] & 0x40) &&
1652 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1653 len = 8;
1654 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001655 }
1656
1657 if (debug) {
Mauro Carvalho Chehab832d40c2016-10-14 07:48:35 -03001658 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1659 intf, len, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001660 }
1661
1662 press_type = imon_parse_press_type(ictx, buf, ktype);
1663 if (press_type < 0)
1664 goto not_input_data;
1665
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001666 if (ktype != IMON_KEY_PANEL) {
1667 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001668 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001669 else {
Ulrich Eckhardtd358aef2014-10-10 13:27:32 -03001670 if (ictx->rc_type == RC_BIT_RC6_MCE ||
1671 ictx->rc_type == RC_BIT_OTHER)
David Härdeman120703f2014-04-03 20:31:30 -03001672 rc_keydown(ictx->rdev,
1673 ictx->rc_type == RC_BIT_RC6_MCE ? RC_TYPE_RC6_MCE : RC_TYPE_OTHER,
1674 ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001675 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001676 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001677 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001678 }
1679 return;
1680 }
1681
1682 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001683 spin_lock_irqsave(&ictx->kc_lock, flags);
1684
Jarod Wilson94215cc2011-06-04 14:14:41 -03001685 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001686 /* KEY_MUTE repeats from knob need to be suppressed */
1687 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001688 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001689 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001690 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001691 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001692 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001693 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001694 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001695 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001696
Jarod Wilson693508d2010-09-15 15:56:03 -03001697 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001698
Jarod Wilson428cc762010-10-23 16:42:20 -03001699 input_report_key(ictx->idev, kc, press_type);
1700 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001701
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001702 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001703 input_report_key(ictx->idev, kc, 0);
1704 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001705
Jarod Wilson94215cc2011-06-04 14:14:41 -03001706 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001707 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001708 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001709
1710 return;
1711
Jarod Wilson21677cf2010-04-16 18:29:02 -03001712not_input_data:
1713 if (len != 8) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001714 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1715 __func__, len, intf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001716 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]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001729 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001730 }
1731}
1732
1733/**
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;
Jarod Wilson8791d632012-01-26 12:04:11 -03001745 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001746 return;
1747
Jarod Wilson8791d632012-01-26 12:04:11 -03001748 /*
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
Jarod Wilson21677cf2010-04-16 18:29:02 -03001756 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:
1764 imon_incoming_packet(ictx, urb, intfnum);
1765 break;
1766
1767 default:
1768 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1769 __func__, urb->status);
1770 break;
1771 }
1772
Jarod Wilson8791d632012-01-26 12:04:11 -03001773out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001774 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;
Jarod Wilson8791d632012-01-26 12:04:11 -03001786 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001787 return;
1788
Jarod Wilson8791d632012-01-26 12:04:11 -03001789 /*
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
Jarod Wilson21677cf2010-04-16 18:29:02 -03001797 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:
1805 imon_incoming_packet(ictx, urb, intfnum);
1806 break;
1807
1808 default:
1809 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1810 __func__, urb->status);
1811 break;
1812 }
1813
Jarod Wilson8791d632012-01-26 12:04:11 -03001814out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001815 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1816}
1817
Jarod Wilson04292fc2010-09-15 00:28:41 -03001818/*
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;
David Härdemanc003ab12012-10-11 19:11:54 -03001831 u64 allowed_protos = RC_BIT_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001832
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:
1852 case 0x85:
1853 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1854 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1855 break;
1856 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001857 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001858 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001859 case 0x9e:
1860 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1861 detected_display_type = IMON_DISPLAY_TYPE_VFD;
David Härdemanc003ab12012-10-11 19:11:54 -03001862 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001863 break;
1864 /* iMON LCD, MCE IR */
1865 case 0x9f:
1866 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1867 detected_display_type = IMON_DISPLAY_TYPE_LCD;
David Härdemanc003ab12012-10-11 19:11:54 -03001868 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001869 break;
1870 default:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001871 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
Jarod Wilson04292fc2010-09-15 00:28:41 -03001872 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Jarod Wilson372b4242011-06-20 00:07:13 -03001873 /* We don't know which one it is, allow user to set the
1874 * RC6 one from userspace if OTHER wasn't correct. */
David Härdemanc003ab12012-10-11 19:11:54 -03001875 allowed_protos |= RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001876 break;
1877 }
1878
1879 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1880
1881 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001882 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001883}
1884
1885static void imon_set_display_type(struct imon_context *ictx)
1886{
1887 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1888
1889 /*
1890 * Try to auto-detect the type of display if the user hasn't set
1891 * it by hand via the display_type modparam. Default is VFD.
1892 */
1893
1894 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1895 switch (ictx->product) {
1896 case 0xffdc:
1897 /* set in imon_get_ffdc_type() */
1898 configured_display_type = ictx->display_type;
1899 break;
1900 case 0x0034:
1901 case 0x0035:
1902 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1903 break;
1904 case 0x0038:
1905 case 0x0039:
1906 case 0x0045:
1907 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1908 break;
1909 case 0x003c:
1910 case 0x0041:
1911 case 0x0042:
1912 case 0x0043:
1913 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1914 ictx->display_supported = false;
1915 break;
1916 case 0x0036:
1917 case 0x0044:
1918 default:
1919 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1920 break;
1921 }
1922 } else {
1923 configured_display_type = display_type;
1924 if (display_type == IMON_DISPLAY_TYPE_NONE)
1925 ictx->display_supported = false;
1926 else
1927 ictx->display_supported = true;
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001928 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1929 __func__, display_type);
Jarod Wilson04292fc2010-09-15 00:28:41 -03001930 }
1931
1932 ictx->display_type = configured_display_type;
1933}
1934
David Härdemand8b4b582010-10-29 16:08:23 -03001935static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001936{
David Härdemand8b4b582010-10-29 16:08:23 -03001937 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001938 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001939 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1940 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001941
David Härdemand8b4b582010-10-29 16:08:23 -03001942 rdev = rc_allocate_device();
1943 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001944 dev_err(ictx->dev, "remote control dev allocation failed\n");
1945 goto out;
1946 }
1947
1948 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1949 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1950 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1951 sizeof(ictx->phys_rdev));
1952 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1953
David Härdemand8b4b582010-10-29 16:08:23 -03001954 rdev->input_name = ictx->name_rdev;
1955 rdev->input_phys = ictx->phys_rdev;
1956 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001957 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001958
David Härdemand8b4b582010-10-29 16:08:23 -03001959 rdev->priv = ictx;
1960 rdev->driver_type = RC_DRIVER_SCANCODE;
David Härdemanc5540fb2014-04-03 20:32:21 -03001961 rdev->allowed_protocols = RC_BIT_OTHER | RC_BIT_RC6_MCE; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001962 rdev->change_protocol = imon_ir_change_protocol;
1963 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001964
Jarod Wilson04292fc2010-09-15 00:28:41 -03001965 /* Enable front-panel buttons and/or knobs */
1966 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1967 ret = send_packet(ictx);
1968 /* Not fatal, but warn about it */
1969 if (ret)
1970 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1971
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001972 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001973 imon_get_ffdc_type(ictx);
David Härdemanc5540fb2014-04-03 20:32:21 -03001974 rdev->allowed_protocols = ictx->rc_type;
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001975 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001976
1977 imon_set_display_type(ictx);
1978
David Härdemanc003ab12012-10-11 19:11:54 -03001979 if (ictx->rc_type == RC_BIT_RC6_MCE)
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001980 rdev->map_name = RC_MAP_IMON_MCE;
1981 else
1982 rdev->map_name = RC_MAP_IMON_PAD;
1983
David Härdemand8b4b582010-10-29 16:08:23 -03001984 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001985 if (ret < 0) {
1986 dev_err(ictx->dev, "remote input dev register failed\n");
1987 goto out;
1988 }
1989
1990 return rdev;
1991
1992out:
David Härdemand8b4b582010-10-29 16:08:23 -03001993 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001994 return NULL;
1995}
1996
Jarod Wilson21677cf2010-04-16 18:29:02 -03001997static struct input_dev *imon_init_idev(struct imon_context *ictx)
1998{
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001999 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002000 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002001 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002002
2003 idev = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03002004 if (!idev)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002005 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002006
Jarod Wilson21677cf2010-04-16 18:29:02 -03002007 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002008 "iMON Panel, Knob and Mouse(%04x:%04x)",
2009 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002010 idev->name = ictx->name_idev;
2011
2012 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2013 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002014 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002015 idev->phys = ictx->phys_idev;
2016
Jarod Wilsondb190fc2010-04-30 16:06:12 -03002017 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002018
2019 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2020 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2021 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2022 BIT_MASK(REL_WHEEL);
2023
2024 /* panel and/or knob code support */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002025 for (i = 0; key_table[i].hw_code != 0; i++) {
2026 u32 kc = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002027 __set_bit(kc, idev->keybit);
2028 }
2029
Jarod Wilson21677cf2010-04-16 18:29:02 -03002030 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2031 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002032 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002033
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002034 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002035 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002036 dev_err(ictx->dev, "input dev register failed\n");
2037 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002038 }
2039
2040 return idev;
2041
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002042out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002043 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002044 return NULL;
2045}
2046
2047static struct input_dev *imon_init_touch(struct imon_context *ictx)
2048{
2049 struct input_dev *touch;
2050 int ret;
2051
2052 touch = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03002053 if (!touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002054 goto touch_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002055
2056 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2057 "iMON USB Touchscreen (%04x:%04x)",
2058 ictx->vendor, ictx->product);
2059 touch->name = ictx->name_touch;
2060
2061 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2062 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002063 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002064 touch->phys = ictx->phys_touch;
2065
2066 touch->evbit[0] =
2067 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2068 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2069 BIT_MASK(BTN_TOUCH);
2070 input_set_abs_params(touch, ABS_X,
2071 0x00, 0xfff, 0, 0);
2072 input_set_abs_params(touch, ABS_Y,
2073 0x00, 0xfff, 0, 0);
2074
2075 input_set_drvdata(touch, ictx);
2076
2077 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2078 touch->dev.parent = ictx->dev;
2079 ret = input_register_device(touch);
2080 if (ret < 0) {
2081 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2082 goto touch_register_failed;
2083 }
2084
2085 return touch;
2086
2087touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03002088 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002089
2090touch_alloc_failed:
2091 return NULL;
2092}
2093
2094static bool imon_find_endpoints(struct imon_context *ictx,
2095 struct usb_host_interface *iface_desc)
2096{
2097 struct usb_endpoint_descriptor *ep;
2098 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2099 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2100 int ifnum = iface_desc->desc.bInterfaceNumber;
2101 int num_endpts = iface_desc->desc.bNumEndpoints;
2102 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002103 bool ir_ep_found = false;
2104 bool display_ep_found = false;
2105 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002106
2107 /*
2108 * Scan the endpoint list and set:
2109 * first input endpoint = IR endpoint
2110 * first output endpoint = display endpoint
2111 */
2112 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2113 ep = &iface_desc->endpoint[i].desc;
2114 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
Himangi Saraogi9408d8f2014-08-15 13:18:53 -03002115 ep_type = usb_endpoint_type(ep);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002116
2117 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2118 ep_type == USB_ENDPOINT_XFER_INT) {
2119
2120 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002121 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002122 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2123
2124 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2125 ep_type == USB_ENDPOINT_XFER_INT) {
2126 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002127 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002128 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2129 }
2130 }
2131
2132 if (ifnum == 0) {
2133 ictx->rx_endpoint_intf0 = rx_endpoint;
2134 /*
2135 * tx is used to send characters to lcd/vfd, associate RF
2136 * remotes, set IR protocol, and maybe more...
2137 */
2138 ictx->tx_endpoint = tx_endpoint;
2139 } else {
2140 ictx->rx_endpoint_intf1 = rx_endpoint;
2141 }
2142
2143 /*
2144 * If we didn't find a display endpoint, this is probably one of the
2145 * newer iMON devices that use control urb instead of interrupt
2146 */
2147 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002148 tx_control = true;
2149 display_ep_found = true;
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002150 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2151 __func__);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002152 }
2153
2154 /*
2155 * Some iMON receivers have no display. Unfortunately, it seems
2156 * that SoundGraph recycles device IDs between devices both with
2157 * and without... :\
2158 */
2159 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002160 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002161 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2162 }
2163
2164 /*
2165 * iMON Touch devices have a VGA touchscreen, but no "display", as
2166 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2167 */
2168 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002169 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002170 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2171 }
2172
2173 /* Input endpoint is mandatory */
2174 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002175 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002176
2177 ictx->tx_control = tx_control;
2178
2179 if (display_ep_found)
2180 ictx->display_supported = true;
2181
2182 return ir_ep_found;
2183
2184}
2185
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002186static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2187 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002188{
2189 struct imon_context *ictx;
2190 struct urb *rx_urb;
2191 struct urb *tx_urb;
2192 struct device *dev = &intf->dev;
2193 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002194 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002195
2196 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2197 if (!ictx) {
2198 dev_err(dev, "%s: kzalloc failed for context", __func__);
2199 goto exit;
2200 }
2201 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002202 if (!rx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002203 goto rx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002204 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002205 if (!tx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002206 goto tx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002207
2208 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002209 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002210
2211 mutex_lock(&ictx->lock);
2212
2213 ictx->dev = dev;
2214 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002215 ictx->rx_urb_intf0 = rx_urb;
2216 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002217 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002218
Daniel Wagner7c073ff2016-09-16 08:18:21 -03002219 init_completion(&ictx->tx.finished);
2220
Jarod Wilson21677cf2010-04-16 18:29:02 -03002221 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2222 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2223
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002224 /* save drive info for later accessing the panel/knob key table */
2225 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002226 /* default send_packet delay is 5ms but some devices need more */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002227 ictx->send_packet_delay = ictx->dev_descr->flags &
2228 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002229
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002230 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002231 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002232 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002233 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002234 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002235
Jarod Wilson21677cf2010-04-16 18:29:02 -03002236 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2237 usb_rcvintpipe(ictx->usbdev_intf0,
2238 ictx->rx_endpoint_intf0->bEndpointAddress),
2239 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2240 usb_rx_callback_intf0, ictx,
2241 ictx->rx_endpoint_intf0->bInterval);
2242
2243 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2244 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002245 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002246 goto urb_submit_failed;
2247 }
2248
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002249 ictx->idev = imon_init_idev(ictx);
2250 if (!ictx->idev) {
2251 dev_err(dev, "%s: input device setup failed\n", __func__);
2252 goto idev_setup_failed;
2253 }
2254
2255 ictx->rdev = imon_init_rdev(ictx);
2256 if (!ictx->rdev) {
2257 dev_err(dev, "%s: rc device setup failed\n", __func__);
2258 goto rdev_setup_failed;
2259 }
2260
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002261 ictx->dev_present_intf0 = true;
2262
Jarod Wilson23ef7102011-04-27 19:01:44 -03002263 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002264 return ictx;
2265
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002266rdev_setup_failed:
2267 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002268idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002269 usb_kill_urb(ictx->rx_urb_intf0);
2270urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002271find_endpoint_failed:
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002272 usb_put_dev(ictx->usbdev_intf0);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002273 mutex_unlock(&ictx->lock);
2274 usb_free_urb(tx_urb);
2275tx_urb_alloc_failed:
2276 usb_free_urb(rx_urb);
2277rx_urb_alloc_failed:
2278 kfree(ictx);
2279exit:
2280 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2281
2282 return NULL;
2283}
2284
2285static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2286 struct imon_context *ictx)
2287{
2288 struct urb *rx_urb;
2289 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002290 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002291
2292 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002293 if (!rx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002294 goto rx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002295
2296 mutex_lock(&ictx->lock);
2297
2298 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2299 init_timer(&ictx->ttimer);
2300 ictx->ttimer.data = (unsigned long)ictx;
2301 ictx->ttimer.function = imon_touch_display_timeout;
2302 }
2303
2304 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002305 ictx->rx_urb_intf1 = rx_urb;
2306
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002307 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002308 iface_desc = intf->cur_altsetting;
2309 if (!imon_find_endpoints(ictx, iface_desc))
2310 goto find_endpoint_failed;
2311
2312 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2313 ictx->touch = imon_init_touch(ictx);
2314 if (!ictx->touch)
2315 goto touch_setup_failed;
2316 } else
2317 ictx->touch = NULL;
2318
2319 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2320 usb_rcvintpipe(ictx->usbdev_intf1,
2321 ictx->rx_endpoint_intf1->bEndpointAddress),
2322 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2323 usb_rx_callback_intf1, ictx,
2324 ictx->rx_endpoint_intf1->bInterval);
2325
2326 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2327
2328 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002329 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002330 goto urb_submit_failed;
2331 }
2332
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002333 ictx->dev_present_intf1 = true;
2334
Jarod Wilson23ef7102011-04-27 19:01:44 -03002335 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002336 return ictx;
2337
2338urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002339 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002340 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002341touch_setup_failed:
2342find_endpoint_failed:
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002343 usb_put_dev(ictx->usbdev_intf1);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002344 mutex_unlock(&ictx->lock);
2345 usb_free_urb(rx_urb);
2346rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002347 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002348
2349 return NULL;
2350}
2351
Jarod Wilson21677cf2010-04-16 18:29:02 -03002352static void imon_init_display(struct imon_context *ictx,
2353 struct usb_interface *intf)
2354{
2355 int ret;
2356
2357 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2358
2359 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002360 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002361 if (ret)
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002362 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2363 ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002364
2365 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2366 ret = usb_register_dev(intf, &imon_lcd_class);
2367 else
2368 ret = usb_register_dev(intf, &imon_vfd_class);
2369 if (ret)
2370 /* Not a fatal error, so ignore */
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002371 dev_info(ictx->dev, "could not get a minor number for display\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002372
2373}
2374
2375/**
2376 * Callback function for USB core API: Probe
2377 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002378static int imon_probe(struct usb_interface *interface,
2379 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002380{
2381 struct usb_device *usbdev = NULL;
2382 struct usb_host_interface *iface_desc = NULL;
2383 struct usb_interface *first_if;
2384 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002385 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002386 int ret = 0;
2387 struct imon_context *ictx = NULL;
2388 struct imon_context *first_if_ctx = NULL;
2389 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002390
Jarod Wilson21677cf2010-04-16 18:29:02 -03002391 usbdev = usb_get_dev(interface_to_usbdev(interface));
2392 iface_desc = interface->cur_altsetting;
2393 ifnum = iface_desc->desc.bInterfaceNumber;
2394 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2395 product = le16_to_cpu(usbdev->descriptor.idProduct);
2396
2397 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2398 __func__, vendor, product, ifnum);
2399
2400 /* prevent races probing devices w/multiple interfaces */
2401 mutex_lock(&driver_lock);
2402
2403 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002404 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002405
2406 if (ifnum == 0) {
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002407 ictx = imon_init_intf0(interface, id);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002408 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002409 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002410 ret = -ENODEV;
2411 goto fail;
2412 }
2413
Jarod Wilson21677cf2010-04-16 18:29:02 -03002414 } else {
Kevin Baradon7d54ba02013-04-22 16:09:45 -03002415 /* this is the secondary interface on the device */
2416
2417 /* fail early if first intf failed to register */
2418 if (!first_if_ctx) {
2419 ret = -ENODEV;
2420 goto fail;
2421 }
2422
Jarod Wilson21677cf2010-04-16 18:29:02 -03002423 ictx = imon_init_intf1(interface, first_if_ctx);
2424 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002425 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002426 ret = -ENODEV;
2427 goto fail;
2428 }
2429
2430 }
2431
2432 usb_set_intfdata(interface, ictx);
2433
2434 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002435 mutex_lock(&ictx->lock);
2436
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002437 if (product == 0xffdc && ictx->rf_device) {
2438 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002439 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002440 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002441 pr_err("Could not create RF sysfs entries(%d)\n",
2442 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002443 }
2444
Jarod Wilson21677cf2010-04-16 18:29:02 -03002445 if (ictx->display_supported)
2446 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002447
2448 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002449 }
2450
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002451 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2452 vendor, product, ifnum,
Jarod Wilson21677cf2010-04-16 18:29:02 -03002453 usbdev->bus->busnum, usbdev->devnum);
2454
Jarod Wilson21677cf2010-04-16 18:29:02 -03002455 mutex_unlock(&driver_lock);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002456 usb_put_dev(usbdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002457
2458 return 0;
2459
2460fail:
2461 mutex_unlock(&driver_lock);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002462 usb_put_dev(usbdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002463 dev_err(dev, "unable to register, err %d\n", ret);
2464
2465 return ret;
2466}
2467
2468/**
2469 * Callback function for USB core API: disconnect
2470 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002471static void imon_disconnect(struct usb_interface *interface)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002472{
2473 struct imon_context *ictx;
2474 struct device *dev;
2475 int ifnum;
2476
2477 /* prevent races with multi-interface device probing and display_open */
2478 mutex_lock(&driver_lock);
2479
2480 ictx = usb_get_intfdata(interface);
2481 dev = ictx->dev;
2482 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2483
Jarod Wilson21677cf2010-04-16 18:29:02 -03002484 /*
2485 * sysfs_remove_group is safe to call even if sysfs_create_group
2486 * hasn't been called
2487 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002488 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2489 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002490
2491 usb_set_intfdata(interface, NULL);
2492
2493 /* Abort ongoing write */
2494 if (ictx->tx.busy) {
2495 usb_kill_urb(ictx->tx_urb);
Daniel Wagner7c073ff2016-09-16 08:18:21 -03002496 complete(&ictx->tx.finished);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002497 }
2498
2499 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002500 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002501 usb_kill_urb(ictx->rx_urb_intf0);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002502 usb_put_dev(ictx->usbdev_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002503 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002504 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002505 if (ictx->display_supported) {
2506 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2507 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002508 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002509 usb_deregister_dev(interface, &imon_vfd_class);
2510 }
2511 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002512 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002513 usb_kill_urb(ictx->rx_urb_intf1);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002514 usb_put_dev(ictx->usbdev_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002515 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002516 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002517 del_timer_sync(&ictx->ttimer);
2518 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002519 }
2520
Jarod Wilson76a2d212011-04-28 18:20:58 -03002521 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2522 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002523
2524 mutex_unlock(&driver_lock);
2525
2526 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2527 __func__, ifnum);
2528}
2529
2530static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2531{
2532 struct imon_context *ictx = usb_get_intfdata(intf);
2533 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2534
2535 if (ifnum == 0)
2536 usb_kill_urb(ictx->rx_urb_intf0);
2537 else
2538 usb_kill_urb(ictx->rx_urb_intf1);
2539
2540 return 0;
2541}
2542
2543static int imon_resume(struct usb_interface *intf)
2544{
2545 int rc = 0;
2546 struct imon_context *ictx = usb_get_intfdata(intf);
2547 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2548
2549 if (ifnum == 0) {
2550 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2551 usb_rcvintpipe(ictx->usbdev_intf0,
2552 ictx->rx_endpoint_intf0->bEndpointAddress),
2553 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2554 usb_rx_callback_intf0, ictx,
2555 ictx->rx_endpoint_intf0->bInterval);
2556
2557 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2558
2559 } else {
2560 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2561 usb_rcvintpipe(ictx->usbdev_intf1,
2562 ictx->rx_endpoint_intf1->bEndpointAddress),
2563 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2564 usb_rx_callback_intf1, ictx,
2565 ictx->rx_endpoint_intf1->bInterval);
2566
2567 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2568 }
2569
2570 return rc;
2571}
2572
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08002573module_usb_driver(imon_driver);