blob: 989d2554ec728beab95a204f532a028c812f4975 [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.
Jarod Wilson21677cf2010-04-16 18:29:02 -030023 */
24
Joe Perchese2302502010-06-20 04:20:46 -030025#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
26
Jarod Wilson21677cf2010-04-16 18:29:02 -030027#include <linux/errno.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
Chunyan Zhang1edba642017-11-06 09:06:10 -050030#include <linux/ktime.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030031#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/uaccess.h>
Jarod Wilson47a09b02011-07-14 14:20:46 -030034#include <linux/ratelimit.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030035
36#include <linux/input.h>
37#include <linux/usb.h>
38#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030039#include <media/rc-core.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030040
Jarod Wilson21677cf2010-04-16 18:29:02 -030041#include <linux/timer.h>
42
43#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
44#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
45#define MOD_NAME "imon"
Jarod Wilson8791d632012-01-26 12:04:11 -030046#define MOD_VERSION "0.9.4"
Jarod Wilson21677cf2010-04-16 18:29:02 -030047
48#define DISPLAY_MINOR_BASE 144
49#define DEVICE_NAME "lcd%d"
50
51#define BUF_CHUNK_SIZE 8
52#define BUF_SIZE 128
53
54#define BIT_DURATION 250 /* each bit received is 250us */
55
56#define IMON_CLOCK_ENABLE_PACKETS 2
Jarod Wilson21677cf2010-04-16 18:29:02 -030057
58/*** P R O T O T Y P E S ***/
59
60/* USB Callback prototypes */
61static int imon_probe(struct usb_interface *interface,
62 const struct usb_device_id *id);
63static void imon_disconnect(struct usb_interface *interface);
64static void usb_rx_callback_intf0(struct urb *urb);
65static void usb_rx_callback_intf1(struct urb *urb);
66static void usb_tx_callback(struct urb *urb);
67
68/* suspend/resume support */
69static int imon_resume(struct usb_interface *intf);
70static int imon_suspend(struct usb_interface *intf, pm_message_t message);
71
72/* Display file_operations function prototypes */
73static int display_open(struct inode *inode, struct file *file);
74static int display_close(struct inode *inode, struct file *file);
75
76/* VFD write operation */
David Härdemand6740d82014-04-03 20:34:53 -030077static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -030078 size_t n_bytes, loff_t *pos);
79
80/* LCD file_operations override function prototypes */
David Härdemand6740d82014-04-03 20:34:53 -030081static ssize_t lcd_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/*** G L O B A L S ***/
85
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -030086struct imon_panel_key_table {
87 u64 hw_code;
88 u32 keycode;
89};
90
91struct imon_usb_dev_descr {
92 __u16 flags;
93#define IMON_NO_FLAGS 0
94#define IMON_NEED_20MS_PKT_DELAY 1
95 struct imon_panel_key_table key_table[];
96};
97
Jarod Wilson21677cf2010-04-16 18:29:02 -030098struct imon_context {
99 struct device *dev;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300100 /* Newer devices have two interfaces */
101 struct usb_device *usbdev_intf0;
102 struct usb_device *usbdev_intf1;
103
104 bool display_supported; /* not all controllers do */
105 bool display_isopen; /* display port has been opened */
Jarod Wilsonbbe46902010-05-24 12:02:05 -0300106 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300107 bool rf_isassociating; /* RF remote associating */
108 bool dev_present_intf0; /* USB device presence, interface 0 */
109 bool dev_present_intf1; /* USB device presence, interface 1 */
110
111 struct mutex lock; /* to lock this object */
112 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
113
114 struct usb_endpoint_descriptor *rx_endpoint_intf0;
115 struct usb_endpoint_descriptor *rx_endpoint_intf1;
116 struct usb_endpoint_descriptor *tx_endpoint;
117 struct urb *rx_urb_intf0;
118 struct urb *rx_urb_intf1;
119 struct urb *tx_urb;
120 bool tx_control;
121 unsigned char usb_rx_buf[8];
122 unsigned char usb_tx_buf[8];
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300123 unsigned int send_packet_delay;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300124
125 struct tx_t {
126 unsigned char data_buf[35]; /* user data buffer */
127 struct completion finished; /* wait for write to finish */
128 bool busy; /* write in progress */
129 int status; /* status of tx completion */
130 } tx;
131
132 u16 vendor; /* usb vendor ID */
133 u16 product; /* usb product ID */
134
David Härdemand8b4b582010-10-29 16:08:23 -0300135 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300136 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300137 struct input_dev *touch; /* input device for touchscreen */
138
Jarod Wilson693508d2010-09-15 15:56:03 -0300139 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300140 u32 kc; /* current input keycode */
141 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300142 u32 rc_scancode; /* the computed remote scancode */
143 u8 rc_toggle; /* the computed remote toggle bit */
Sean Young6d741bf2017-08-07 16:20:58 -0400144 u64 rc_proto; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300145 bool release_code; /* some keys send a release code */
146
147 u8 display_type; /* store the display type */
148 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
149
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300150 char name_rdev[128]; /* rc input device name */
151 char phys_rdev[64]; /* rc input device phys path */
152
Jarod Wilson21677cf2010-04-16 18:29:02 -0300153 char name_idev[128]; /* input device name */
154 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300155
156 char name_touch[128]; /* touch screen name */
157 char phys_touch[64]; /* touch screen phys path */
158 struct timer_list ttimer; /* touch screen timer */
159 int touch_x; /* x coordinate on touchscreen */
160 int touch_y; /* y coordinate on touchscreen */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300161 struct imon_usb_dev_descr *dev_descr; /* device description with key
162 table for front panels */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300163};
164
165#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300166
167/* vfd character device file operations */
168static const struct file_operations vfd_fops = {
169 .owner = THIS_MODULE,
170 .open = &display_open,
171 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200172 .release = &display_close,
173 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300174};
175
176/* lcd character device file operations */
177static const struct file_operations lcd_fops = {
178 .owner = THIS_MODULE,
179 .open = &display_open,
180 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200181 .release = &display_close,
182 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300183};
184
185enum {
186 IMON_DISPLAY_TYPE_AUTO = 0,
187 IMON_DISPLAY_TYPE_VFD = 1,
188 IMON_DISPLAY_TYPE_LCD = 2,
189 IMON_DISPLAY_TYPE_VGA = 3,
190 IMON_DISPLAY_TYPE_NONE = 4,
191};
192
193enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300194 IMON_KEY_IMON = 0,
195 IMON_KEY_MCE = 1,
196 IMON_KEY_PANEL = 2,
197};
198
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300199static struct usb_class_driver imon_vfd_class = {
200 .name = DEVICE_NAME,
201 .fops = &vfd_fops,
202 .minor_base = DISPLAY_MINOR_BASE,
203};
204
205static struct usb_class_driver imon_lcd_class = {
206 .name = DEVICE_NAME,
207 .fops = &lcd_fops,
208 .minor_base = DISPLAY_MINOR_BASE,
209};
210
211/* imon receiver front panel/knob key table */
212static const struct imon_usb_dev_descr imon_default_table = {
213 .flags = IMON_NO_FLAGS,
214 .key_table = {
215 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
216 { 0x000000001200ffeell, KEY_UP },
217 { 0x000000001300ffeell, KEY_DOWN },
218 { 0x000000001400ffeell, KEY_LEFT },
219 { 0x000000001500ffeell, KEY_RIGHT },
220 { 0x000000001600ffeell, KEY_ENTER },
221 { 0x000000001700ffeell, KEY_ESC },
222 { 0x000000001f00ffeell, KEY_AUDIO },
223 { 0x000000002000ffeell, KEY_VIDEO },
224 { 0x000000002100ffeell, KEY_CAMERA },
225 { 0x000000002700ffeell, KEY_DVD },
226 { 0x000000002300ffeell, KEY_TV },
227 { 0x000000002b00ffeell, KEY_EXIT },
228 { 0x000000002c00ffeell, KEY_SELECT },
229 { 0x000000002d00ffeell, KEY_MENU },
230 { 0x000000000500ffeell, KEY_PREVIOUS },
231 { 0x000000000700ffeell, KEY_REWIND },
232 { 0x000000000400ffeell, KEY_STOP },
233 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
234 { 0x000000000800ffeell, KEY_FASTFORWARD },
235 { 0x000000000600ffeell, KEY_NEXT },
236 { 0x000000010000ffeell, KEY_RIGHT },
237 { 0x000001000000ffeell, KEY_LEFT },
238 { 0x000000003d00ffeell, KEY_SELECT },
239 { 0x000100000000ffeell, KEY_VOLUMEUP },
240 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
241 { 0x000000000100ffeell, KEY_MUTE },
242 /* 0xffdc iMON MCE VFD */
243 { 0x00010000ffffffeell, KEY_VOLUMEUP },
244 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
245 { 0x00000001ffffffeell, KEY_MUTE },
246 { 0x0000000fffffffeell, KEY_MEDIA },
247 { 0x00000012ffffffeell, KEY_UP },
248 { 0x00000013ffffffeell, KEY_DOWN },
249 { 0x00000014ffffffeell, KEY_LEFT },
250 { 0x00000015ffffffeell, KEY_RIGHT },
251 { 0x00000016ffffffeell, KEY_ENTER },
252 { 0x00000017ffffffeell, KEY_ESC },
253 /* iMON Knob values */
254 { 0x000100ffffffffeell, KEY_VOLUMEUP },
255 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
256 { 0x000008ffffffffeell, KEY_MUTE },
257 { 0, KEY_RESERVED },
258 }
259};
260
261static const struct imon_usb_dev_descr imon_OEM_VFD = {
262 .flags = IMON_NEED_20MS_PKT_DELAY,
263 .key_table = {
264 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
265 { 0x000000001200ffeell, KEY_UP },
266 { 0x000000001300ffeell, KEY_DOWN },
267 { 0x000000001400ffeell, KEY_LEFT },
268 { 0x000000001500ffeell, KEY_RIGHT },
269 { 0x000000001600ffeell, KEY_ENTER },
270 { 0x000000001700ffeell, KEY_ESC },
271 { 0x000000001f00ffeell, KEY_AUDIO },
272 { 0x000000002b00ffeell, KEY_EXIT },
273 { 0x000000002c00ffeell, KEY_SELECT },
274 { 0x000000002d00ffeell, KEY_MENU },
275 { 0x000000000500ffeell, KEY_PREVIOUS },
276 { 0x000000000700ffeell, KEY_REWIND },
277 { 0x000000000400ffeell, KEY_STOP },
278 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
279 { 0x000000000800ffeell, KEY_FASTFORWARD },
280 { 0x000000000600ffeell, KEY_NEXT },
281 { 0x000000010000ffeell, KEY_RIGHT },
282 { 0x000001000000ffeell, KEY_LEFT },
283 { 0x000000003d00ffeell, KEY_SELECT },
284 { 0x000100000000ffeell, KEY_VOLUMEUP },
285 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
286 { 0x000000000100ffeell, KEY_MUTE },
287 /* 0xffdc iMON MCE VFD */
288 { 0x00010000ffffffeell, KEY_VOLUMEUP },
289 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
290 { 0x00000001ffffffeell, KEY_MUTE },
291 { 0x0000000fffffffeell, KEY_MEDIA },
292 { 0x00000012ffffffeell, KEY_UP },
293 { 0x00000013ffffffeell, KEY_DOWN },
294 { 0x00000014ffffffeell, KEY_LEFT },
295 { 0x00000015ffffffeell, KEY_RIGHT },
296 { 0x00000016ffffffeell, KEY_ENTER },
297 { 0x00000017ffffffeell, KEY_ESC },
298 /* iMON Knob values */
299 { 0x000100ffffffffeell, KEY_VOLUMEUP },
300 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
301 { 0x000008ffffffffeell, KEY_MUTE },
302 { 0, KEY_RESERVED },
303 }
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300304};
305
Ulrich Eckhardt7b5fc072014-07-26 14:59:07 -0300306/* imon receiver front panel/knob key table for DH102*/
307static const struct imon_usb_dev_descr imon_DH102 = {
308 .flags = IMON_NO_FLAGS,
309 .key_table = {
310 { 0x000100000000ffeell, KEY_VOLUMEUP },
311 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
312 { 0x000000010000ffeell, KEY_MUTE },
313 { 0x0000000f0000ffeell, KEY_MEDIA },
314 { 0x000000120000ffeell, KEY_UP },
315 { 0x000000130000ffeell, KEY_DOWN },
316 { 0x000000140000ffeell, KEY_LEFT },
317 { 0x000000150000ffeell, KEY_RIGHT },
318 { 0x000000160000ffeell, KEY_ENTER },
319 { 0x000000170000ffeell, KEY_ESC },
320 { 0x0000002b0000ffeell, KEY_EXIT },
321 { 0x0000002c0000ffeell, KEY_SELECT },
322 { 0x0000002d0000ffeell, KEY_MENU },
323 { 0, KEY_RESERVED }
324 }
325};
326
Jarod Wilson21677cf2010-04-16 18:29:02 -0300327/*
328 * USB Device ID for iMON USB Control Boards
329 *
330 * The Windows drivers contain 6 different inf files, more or less one for
331 * each new device until the 0x0034-0x0046 devices, which all use the same
332 * driver. Some of the devices in the 34-46 range haven't been definitively
333 * identified yet. Early devices have either a TriGem Computer, Inc. or a
334 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
335 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
336 * the ffdc and later devices, which do onboard decoding.
337 */
Arvind Yadav5fad16b2017-08-13 05:54:44 -0300338static const struct usb_device_id imon_usb_id_table[] = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300339 /*
340 * Several devices with this same device ID, all use iMON_PAD.inf
341 * SoundGraph iMON PAD (IR & VFD)
342 * SoundGraph iMON PAD (IR & LCD)
343 * SoundGraph iMON Knob (IR only)
344 */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300345 { USB_DEVICE(0x15c2, 0xffdc),
346 .driver_info = (unsigned long)&imon_default_table },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300347
348 /*
349 * Newer devices, all driven by the latest iMON Windows driver, full
350 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
351 * Need user input to fill in details on unknown devices.
352 */
353 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300354 { USB_DEVICE(0x15c2, 0x0034),
Ulrich Eckhardt7b5fc072014-07-26 14:59:07 -0300355 .driver_info = (unsigned long)&imon_DH102 },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300356 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300357 { USB_DEVICE(0x15c2, 0x0035),
358 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300359 /* SoundGraph iMON OEM VFD (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300360 { USB_DEVICE(0x15c2, 0x0036),
361 .driver_info = (unsigned long)&imon_OEM_VFD },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300362 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300363 { USB_DEVICE(0x15c2, 0x0037),
364 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300365 /* SoundGraph iMON OEM LCD (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300366 { USB_DEVICE(0x15c2, 0x0038),
367 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300368 /* SoundGraph iMON UltraBay (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300369 { USB_DEVICE(0x15c2, 0x0039),
370 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300371 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300372 { USB_DEVICE(0x15c2, 0x003a),
373 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300374 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300375 { USB_DEVICE(0x15c2, 0x003b),
376 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300377 /* SoundGraph iMON OEM Inside (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300378 { USB_DEVICE(0x15c2, 0x003c),
379 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300380 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300381 { USB_DEVICE(0x15c2, 0x003d),
382 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300383 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300384 { USB_DEVICE(0x15c2, 0x003e),
385 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300386 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300387 { USB_DEVICE(0x15c2, 0x003f),
388 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300389 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300390 { USB_DEVICE(0x15c2, 0x0040),
391 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300392 /* SoundGraph iMON MINI (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300393 { USB_DEVICE(0x15c2, 0x0041),
394 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300395 /* Antec Veris Multimedia Station EZ External (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300396 { USB_DEVICE(0x15c2, 0x0042),
397 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300398 /* Antec Veris Multimedia Station Basic Internal (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300399 { USB_DEVICE(0x15c2, 0x0043),
400 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300401 /* Antec Veris Multimedia Station Elite (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300402 { USB_DEVICE(0x15c2, 0x0044),
403 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300404 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300405 { USB_DEVICE(0x15c2, 0x0045),
406 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300407 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300408 { USB_DEVICE(0x15c2, 0x0046),
409 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300410 {}
411};
412
413/* USB Device data */
414static struct usb_driver imon_driver = {
415 .name = MOD_NAME,
416 .probe = imon_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800417 .disconnect = imon_disconnect,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300418 .suspend = imon_suspend,
419 .resume = imon_resume,
420 .id_table = imon_usb_id_table,
421};
422
Jarod Wilson21677cf2010-04-16 18:29:02 -0300423/* to prevent races between open() and disconnect(), probing, etc */
424static DEFINE_MUTEX(driver_lock);
425
426/* Module bookkeeping bits */
427MODULE_AUTHOR(MOD_AUTHOR);
428MODULE_DESCRIPTION(MOD_DESC);
429MODULE_VERSION(MOD_VERSION);
430MODULE_LICENSE("GPL");
431MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
432
433static bool debug;
434module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300435MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300436
437/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
438static int display_type;
439module_param(display_type, int, S_IRUGO);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200440MODULE_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 -0300441
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300442static int pad_stabilize = 1;
443module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200444MODULE_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 -0300445
446/*
447 * In certain use cases, mouse mode isn't really helpful, and could actually
448 * cause confusion, so allow disabling it when the IR device is open.
449 */
450static bool nomouse;
451module_param(nomouse, bool, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200452MODULE_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 -0300453
454/* threshold at which a pad push registers as an arrow key in kbd mode */
455static int pad_thresh;
456module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200457MODULE_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 -0300458
459
460static void free_imon_context(struct imon_context *ictx)
461{
462 struct device *dev = ictx->dev;
463
464 usb_free_urb(ictx->tx_urb);
465 usb_free_urb(ictx->rx_urb_intf0);
466 usb_free_urb(ictx->rx_urb_intf1);
467 kfree(ictx);
468
469 dev_dbg(dev, "%s: iMON context freed\n", __func__);
470}
471
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500472/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300473 * Called when the Display device (e.g. /dev/lcd0)
474 * is opened by the application.
475 */
476static int display_open(struct inode *inode, struct file *file)
477{
478 struct usb_interface *interface;
479 struct imon_context *ictx = NULL;
480 int subminor;
481 int retval = 0;
482
483 /* prevent races with disconnect */
484 mutex_lock(&driver_lock);
485
486 subminor = iminor(inode);
487 interface = usb_find_interface(&imon_driver, subminor);
488 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300489 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300490 retval = -ENODEV;
491 goto exit;
492 }
493 ictx = usb_get_intfdata(interface);
494
495 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300496 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300497 retval = -ENODEV;
498 goto exit;
499 }
500
501 mutex_lock(&ictx->lock);
502
503 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300504 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300505 retval = -ENODEV;
506 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300507 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300508 retval = -EBUSY;
509 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300510 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300511 file->private_data = ictx;
512 dev_dbg(ictx->dev, "display port opened\n");
513 }
514
515 mutex_unlock(&ictx->lock);
516
517exit:
518 mutex_unlock(&driver_lock);
519 return retval;
520}
521
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500522/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300523 * Called when the display device (e.g. /dev/lcd0)
524 * is closed by the application.
525 */
526static int display_close(struct inode *inode, struct file *file)
527{
528 struct imon_context *ictx = NULL;
529 int retval = 0;
530
Joe Perchesabf84382010-07-12 17:50:03 -0300531 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300532
533 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300534 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300535 return -ENODEV;
536 }
537
538 mutex_lock(&ictx->lock);
539
540 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300541 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300542 retval = -ENODEV;
543 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300544 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300545 retval = -EIO;
546 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300547 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300548 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300549 }
550
551 mutex_unlock(&ictx->lock);
552 return retval;
553}
554
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500555/*
Jarod Wilson23ef7102011-04-27 19:01:44 -0300556 * Sends a packet to the device -- this function must be called with
557 * ictx->lock held, or its unlock/lock sequence while waiting for tx
558 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300559 */
560static int send_packet(struct imon_context *ictx)
561{
562 unsigned int pipe;
563 unsigned long timeout;
564 int interval = 0;
565 int retval = 0;
566 struct usb_ctrlrequest *control_req = NULL;
567
568 /* Check if we need to use control or interrupt urb */
569 if (!ictx->tx_control) {
570 pipe = usb_sndintpipe(ictx->usbdev_intf0,
571 ictx->tx_endpoint->bEndpointAddress);
572 interval = ictx->tx_endpoint->bInterval;
573
574 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
575 ictx->usb_tx_buf,
576 sizeof(ictx->usb_tx_buf),
577 usb_tx_callback, ictx, interval);
578
579 ictx->tx_urb->actual_length = 0;
580 } else {
581 /* fill request into kmalloc'ed space: */
Markus Elfringa8c779e2017-08-29 07:45:59 -0300582 control_req = kmalloc(sizeof(*control_req), GFP_KERNEL);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300583 if (control_req == NULL)
584 return -ENOMEM;
585
586 /* setup packet is '21 09 0200 0001 0008' */
587 control_req->bRequestType = 0x21;
588 control_req->bRequest = 0x09;
589 control_req->wValue = cpu_to_le16(0x0200);
590 control_req->wIndex = cpu_to_le16(0x0001);
591 control_req->wLength = cpu_to_le16(0x0008);
592
593 /* control pipe is endpoint 0x00 */
594 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
595
596 /* build the control urb */
597 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
598 pipe, (unsigned char *)control_req,
599 ictx->usb_tx_buf,
600 sizeof(ictx->usb_tx_buf),
601 usb_tx_callback, ictx);
602 ictx->tx_urb->actual_length = 0;
603 }
604
Daniel Wagner7c073ff2016-09-16 08:18:21 -0300605 reinit_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300606 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300607 smp_rmb(); /* ensure later readers know we're busy */
608
609 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
610 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300611 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300612 smp_rmb(); /* ensure later readers know we're not busy */
Jarod Wilson47a09b02011-07-14 14:20:46 -0300613 pr_err_ratelimited("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300614 } else {
615 /* Wait for transmission to complete (or abort) */
616 mutex_unlock(&ictx->lock);
617 retval = wait_for_completion_interruptible(
618 &ictx->tx.finished);
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300619 if (retval) {
620 usb_kill_urb(ictx->tx_urb);
Jarod Wilson47a09b02011-07-14 14:20:46 -0300621 pr_err_ratelimited("task interrupted\n");
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300622 }
Jarod Wilson21677cf2010-04-16 18:29:02 -0300623 mutex_lock(&ictx->lock);
624
625 retval = ictx->tx.status;
626 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300627 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300628 }
629
630 kfree(control_req);
631
632 /*
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300633 * Induce a mandatory delay before returning, as otherwise,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300634 * send_packet can get called so rapidly as to overwhelm the device,
635 * particularly on faster systems and/or those with quirky usb.
636 */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300637 timeout = msecs_to_jiffies(ictx->send_packet_delay);
638 set_current_state(TASK_INTERRUPTIBLE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300639 schedule_timeout(timeout);
640
641 return retval;
642}
643
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500644/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300645 * Sends an associate packet to the iMON 2.4G.
646 *
647 * This might not be such a good idea, since it has an id collision with
648 * some versions of the "IR & VFD" combo. The only way to determine if it
649 * is an RF version is to look at the product description string. (Which
650 * we currently do not fetch).
651 */
652static int send_associate_24g(struct imon_context *ictx)
653{
654 int retval;
655 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
656 0x00, 0x00, 0x00, 0x20 };
657
658 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300659 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300660 return -ENODEV;
661 }
662
663 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300664 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300665 return -ENODEV;
666 }
667
668 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
669 retval = send_packet(ictx);
670
671 return retval;
672}
673
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500674/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300675 * Sends packets to setup and show clock on iMON display
676 *
677 * Arguments: year - last 2 digits of year, month - 1..12,
678 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
679 * hour - 0..23, minute - 0..59, second - 0..59
680 */
681static int send_set_imon_clock(struct imon_context *ictx,
682 unsigned int year, unsigned int month,
683 unsigned int day, unsigned int dow,
684 unsigned int hour, unsigned int minute,
685 unsigned int second)
686{
687 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
688 int retval = 0;
689 int i;
690
691 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300692 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300693 return -ENODEV;
694 }
695
696 switch (ictx->display_type) {
697 case IMON_DISPLAY_TYPE_LCD:
698 clock_enable_pkt[0][0] = 0x80;
699 clock_enable_pkt[0][1] = year;
700 clock_enable_pkt[0][2] = month-1;
701 clock_enable_pkt[0][3] = day;
702 clock_enable_pkt[0][4] = hour;
703 clock_enable_pkt[0][5] = minute;
704 clock_enable_pkt[0][6] = second;
705
706 clock_enable_pkt[1][0] = 0x80;
707 clock_enable_pkt[1][1] = 0;
708 clock_enable_pkt[1][2] = 0;
709 clock_enable_pkt[1][3] = 0;
710 clock_enable_pkt[1][4] = 0;
711 clock_enable_pkt[1][5] = 0;
712 clock_enable_pkt[1][6] = 0;
713
714 if (ictx->product == 0xffdc) {
715 clock_enable_pkt[0][7] = 0x50;
716 clock_enable_pkt[1][7] = 0x51;
717 } else {
718 clock_enable_pkt[0][7] = 0x88;
719 clock_enable_pkt[1][7] = 0x8a;
720 }
721
722 break;
723
724 case IMON_DISPLAY_TYPE_VFD:
725 clock_enable_pkt[0][0] = year;
726 clock_enable_pkt[0][1] = month-1;
727 clock_enable_pkt[0][2] = day;
728 clock_enable_pkt[0][3] = dow;
729 clock_enable_pkt[0][4] = hour;
730 clock_enable_pkt[0][5] = minute;
731 clock_enable_pkt[0][6] = second;
732 clock_enable_pkt[0][7] = 0x40;
733
734 clock_enable_pkt[1][0] = 0;
735 clock_enable_pkt[1][1] = 0;
736 clock_enable_pkt[1][2] = 1;
737 clock_enable_pkt[1][3] = 0;
738 clock_enable_pkt[1][4] = 0;
739 clock_enable_pkt[1][5] = 0;
740 clock_enable_pkt[1][6] = 0;
741 clock_enable_pkt[1][7] = 0x42;
742
743 break;
744
745 default:
746 return -ENODEV;
747 }
748
749 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
750 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
751 retval = send_packet(ictx);
752 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300753 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300754 break;
755 }
756 }
757
758 return retval;
759}
760
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500761/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300762 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
763 */
764static ssize_t show_associate_remote(struct device *d,
765 struct device_attribute *attr,
766 char *buf)
767{
768 struct imon_context *ictx = dev_get_drvdata(d);
769
770 if (!ictx)
771 return -ENODEV;
772
773 mutex_lock(&ictx->lock);
774 if (ictx->rf_isassociating)
Mauro Carvalho Chehab2396e282018-09-10 16:14:15 -0400775 strscpy(buf, "associating\n", PAGE_SIZE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300776 else
Mauro Carvalho Chehab2396e282018-09-10 16:14:15 -0400777 strscpy(buf, "closed\n", PAGE_SIZE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300778
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200779 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 -0300780 mutex_unlock(&ictx->lock);
781 return strlen(buf);
782}
783
784static ssize_t store_associate_remote(struct device *d,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
787{
788 struct imon_context *ictx;
789
790 ictx = dev_get_drvdata(d);
791
792 if (!ictx)
793 return -ENODEV;
794
795 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300796 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300797 send_associate_24g(ictx);
798 mutex_unlock(&ictx->lock);
799
800 return count;
801}
802
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500803/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300804 * sysfs functions to control internal imon clock
805 */
806static ssize_t show_imon_clock(struct device *d,
807 struct device_attribute *attr, char *buf)
808{
809 struct imon_context *ictx = dev_get_drvdata(d);
810 size_t len;
811
812 if (!ictx)
813 return -ENODEV;
814
815 mutex_lock(&ictx->lock);
816
817 if (!ictx->display_supported) {
818 len = snprintf(buf, PAGE_SIZE, "Not supported.");
819 } else {
820 len = snprintf(buf, PAGE_SIZE,
821 "To set the clock on your iMON display:\n"
822 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
823 "%s", ictx->display_isopen ?
824 "\nNOTE: imon device must be closed\n" : "");
825 }
826
827 mutex_unlock(&ictx->lock);
828
829 return len;
830}
831
832static ssize_t store_imon_clock(struct device *d,
833 struct device_attribute *attr,
834 const char *buf, size_t count)
835{
836 struct imon_context *ictx = dev_get_drvdata(d);
837 ssize_t retval;
838 unsigned int year, month, day, dow, hour, minute, second;
839
840 if (!ictx)
841 return -ENODEV;
842
843 mutex_lock(&ictx->lock);
844
845 if (!ictx->display_supported) {
846 retval = -ENODEV;
847 goto exit;
848 } else if (ictx->display_isopen) {
849 retval = -EBUSY;
850 goto exit;
851 }
852
853 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
854 &hour, &minute, &second) != 7) {
855 retval = -EINVAL;
856 goto exit;
857 }
858
859 if ((month < 1 || month > 12) ||
860 (day < 1 || day > 31) || (dow > 6) ||
861 (hour > 23) || (minute > 59) || (second > 59)) {
862 retval = -EINVAL;
863 goto exit;
864 }
865
866 retval = send_set_imon_clock(ictx, year, month, day, dow,
867 hour, minute, second);
868 if (retval)
869 goto exit;
870
871 retval = count;
872exit:
873 mutex_unlock(&ictx->lock);
874
875 return retval;
876}
877
878
879static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
880 store_imon_clock);
881
882static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
883 store_associate_remote);
884
885static struct attribute *imon_display_sysfs_entries[] = {
886 &dev_attr_imon_clock.attr,
887 NULL
888};
889
Arvind Yadavd9a77b92017-07-07 04:15:12 -0400890static const struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300891 .attrs = imon_display_sysfs_entries
892};
893
894static struct attribute *imon_rf_sysfs_entries[] = {
895 &dev_attr_associate_remote.attr,
896 NULL
897};
898
Arvind Yadavd9a77b92017-07-07 04:15:12 -0400899static const struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300900 .attrs = imon_rf_sysfs_entries
901};
902
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500903/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300904 * Writes data to the VFD. The iMON VFD is 2x16 characters
905 * and requires data in 5 consecutive USB interrupt packets,
906 * each packet but the last carrying 7 bytes.
907 *
908 * I don't know if the VFD board supports features such as
909 * scrolling, clearing rows, blanking, etc. so at
910 * the caller must provide a full screen of data. If fewer
911 * than 32 bytes are provided spaces will be appended to
912 * generate a full screen.
913 */
David Härdemand6740d82014-04-03 20:34:53 -0300914static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300915 size_t n_bytes, loff_t *pos)
916{
917 int i;
918 int offset;
919 int seq;
920 int retval = 0;
921 struct imon_context *ictx;
Colin Ian Kingc25895c2017-09-05 09:07:50 -0300922 static const unsigned char vfd_packet6[] = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300923 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
924
Joe Perchesabf84382010-07-12 17:50:03 -0300925 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300926 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300927 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300928 return -ENODEV;
929 }
930
931 mutex_lock(&ictx->lock);
932
933 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300934 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300935 retval = -ENODEV;
936 goto exit;
937 }
938
939 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300940 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300941 retval = -EINVAL;
942 goto exit;
943 }
944
945 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
946 retval = -EFAULT;
947 goto exit;
948 }
949
950 /* Pad with spaces */
951 for (i = n_bytes; i < 32; ++i)
952 ictx->tx.data_buf[i] = ' ';
953
954 for (i = 32; i < 35; ++i)
955 ictx->tx.data_buf[i] = 0xFF;
956
957 offset = 0;
958 seq = 0;
959
960 do {
961 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
962 ictx->usb_tx_buf[7] = (unsigned char) seq;
963
964 retval = send_packet(ictx);
965 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300966 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300967 goto exit;
968 } else {
969 seq += 2;
970 offset += 7;
971 }
972
973 } while (offset < 35);
974
975 /* Send packet #6 */
976 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
977 ictx->usb_tx_buf[7] = (unsigned char) seq;
978 retval = send_packet(ictx);
979 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300980 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300981
982exit:
983 mutex_unlock(&ictx->lock);
984
985 return (!retval) ? n_bytes : retval;
986}
987
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -0500988/*
Jarod Wilson21677cf2010-04-16 18:29:02 -0300989 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
990 * packets. We accept data as 16 hexadecimal digits, followed by a
991 * newline (to make it easy to drive the device from a command-line
992 * -- even though the actual binary data is a bit complicated).
993 *
994 * The device itself is not a "traditional" text-mode display. It's
995 * actually a 16x96 pixel bitmap display. That means if you want to
996 * display text, you've got to have your own "font" and translate the
997 * text into bitmaps for display. This is really flexible (you can
998 * display whatever diacritics you need, and so on), but it's also
999 * a lot more complicated than most LCDs...
1000 */
David Härdemand6740d82014-04-03 20:34:53 -03001001static ssize_t lcd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001002 size_t n_bytes, loff_t *pos)
1003{
1004 int retval = 0;
1005 struct imon_context *ictx;
1006
Joe Perchesabf84382010-07-12 17:50:03 -03001007 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001008 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001009 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001010 return -ENODEV;
1011 }
1012
1013 mutex_lock(&ictx->lock);
1014
1015 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001016 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001017 retval = -ENODEV;
1018 goto exit;
1019 }
1020
1021 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001022 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1023 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001024 retval = -EINVAL;
1025 goto exit;
1026 }
1027
1028 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1029 retval = -EFAULT;
1030 goto exit;
1031 }
1032
1033 retval = send_packet(ictx);
1034 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001035 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001036 goto exit;
1037 } else {
1038 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1039 __func__, (int) n_bytes);
1040 }
1041exit:
1042 mutex_unlock(&ictx->lock);
1043 return (!retval) ? n_bytes : retval;
1044}
1045
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001046/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001047 * Callback function for USB core API: transmit data
1048 */
1049static void usb_tx_callback(struct urb *urb)
1050{
1051 struct imon_context *ictx;
1052
1053 if (!urb)
1054 return;
1055 ictx = (struct imon_context *)urb->context;
1056 if (!ictx)
1057 return;
1058
1059 ictx->tx.status = urb->status;
1060
1061 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001062 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001063 smp_rmb(); /* ensure later readers know we're not busy */
1064 complete(&ictx->tx.finished);
1065}
1066
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001067/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001068 * report touchscreen input
1069 */
Kees Cookb17ec782017-10-24 11:23:14 -04001070static void imon_touch_display_timeout(struct timer_list *t)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001071{
Kees Cookb17ec782017-10-24 11:23:14 -04001072 struct imon_context *ictx = from_timer(ictx, t, ttimer);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001073
Dan Carpenterf03900d2010-05-04 08:37:33 -03001074 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001075 return;
1076
1077 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1078 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1079 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1080 input_sync(ictx->touch);
1081}
1082
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001083/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001084 * iMON IR receivers support two different signal sets -- those used by
1085 * the iMON remotes, and those used by the Windows MCE remotes (which is
1086 * really just RC-6), but only one or the other at a time, as the signals
1087 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -03001088 *
1089 * This function gets called two different ways, one way is from
1090 * rc_register_device, for initial protocol selection/setup, and the other is
1091 * via a userspace-initiated protocol change request, either by direct sysfs
1092 * prodding or by something like ir-keytable. In the rc_register_device case,
1093 * the imon context lock is already held, but when initiated from userspace,
1094 * it is not, so we must acquire it prior to calling send_packet, which
1095 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001096 */
Sean Young6d741bf2017-08-07 16:20:58 -04001097static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001098{
1099 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001100 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001101 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001102 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001103 unsigned char ir_proto_packet[] = {
1104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1105
Sean Young6d741bf2017-08-07 16:20:58 -04001106 if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001107 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 -03001108
Sean Young6d741bf2017-08-07 16:20:58 -04001109 if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001110 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1111 ir_proto_packet[0] = 0x01;
Sean Young6d741bf2017-08-07 16:20:58 -04001112 *rc_proto = RC_PROTO_BIT_RC6_MCE;
Sean Young2525fdcb2018-03-07 05:55:38 -05001113 } else if (*rc_proto & RC_PROTO_BIT_IMON) {
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001114 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001115 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001116 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001117 /* ir_proto_packet[0] = 0x00; // already the default */
Sean Young2525fdcb2018-03-07 05:55:38 -05001118 *rc_proto = RC_PROTO_BIT_IMON;
David Härdemanc003ab12012-10-11 19:11:54 -03001119 } else {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001120 dev_warn(dev, "Unsupported IR protocol specified, overriding to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001121 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001122 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001123 /* ir_proto_packet[0] = 0x00; // already the default */
Sean Young2525fdcb2018-03-07 05:55:38 -05001124 *rc_proto = RC_PROTO_BIT_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001125 }
1126
1127 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1128
Jarod Wilson23ef7102011-04-27 19:01:44 -03001129 if (!mutex_is_locked(&ictx->lock)) {
1130 unlock = true;
1131 mutex_lock(&ictx->lock);
1132 }
1133
Jarod Wilson21677cf2010-04-16 18:29:02 -03001134 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001135 if (retval)
1136 goto out;
1137
Sean Young6d741bf2017-08-07 16:20:58 -04001138 ictx->rc_proto = *rc_proto;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001139 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001140
1141out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001142 if (unlock)
1143 mutex_unlock(&ictx->lock);
1144
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001145 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001146}
1147
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001148/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001149 * The directional pad behaves a bit differently, depending on whether this is
1150 * one of the older ffdc devices or a newer device. Newer devices appear to
1151 * have a higher resolution matrix for more precise mouse movement, but it
1152 * makes things overly sensitive in keyboard mode, so we do some interesting
1153 * contortions to make it less touchy. Older devices run through the same
1154 * routine with shorter timeout and a smaller threshold.
1155 */
1156static int stabilize(int a, int b, u16 timeout, u16 threshold)
1157{
Chunyan Zhang1edba642017-11-06 09:06:10 -05001158 ktime_t ct;
1159 static ktime_t prev_time;
1160 static ktime_t hit_time;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001161 static int x, y, prev_result, hits;
1162 int result = 0;
Chunyan Zhang1edba642017-11-06 09:06:10 -05001163 long msec, msec_hit;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001164
Chunyan Zhang1edba642017-11-06 09:06:10 -05001165 ct = ktime_get();
1166 msec = ktime_ms_delta(ct, prev_time);
1167 msec_hit = ktime_ms_delta(ct, hit_time);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001168
1169 if (msec > 100) {
1170 x = 0;
1171 y = 0;
1172 hits = 0;
1173 }
1174
1175 x += a;
1176 y += b;
1177
1178 prev_time = ct;
1179
1180 if (abs(x) > threshold || abs(y) > threshold) {
1181 if (abs(y) > abs(x))
1182 result = (y > 0) ? 0x7F : 0x80;
1183 else
1184 result = (x > 0) ? 0x7F00 : 0x8000;
1185
1186 x = 0;
1187 y = 0;
1188
1189 if (result == prev_result) {
1190 hits++;
1191
1192 if (hits > 3) {
1193 switch (result) {
1194 case 0x7F:
1195 y = 17 * threshold / 30;
1196 break;
1197 case 0x80:
1198 y -= 17 * threshold / 30;
1199 break;
1200 case 0x7F00:
1201 x = 17 * threshold / 30;
1202 break;
1203 case 0x8000:
1204 x -= 17 * threshold / 30;
1205 break;
1206 }
1207 }
1208
1209 if (hits == 2 && msec_hit < timeout) {
1210 result = 0;
1211 hits = 1;
1212 }
1213 } else {
1214 prev_result = result;
1215 hits = 1;
1216 hit_time = ct;
1217 }
1218 }
1219
1220 return result;
1221}
1222
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001223static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001224{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001225 u32 keycode;
1226 u32 release;
1227 bool is_release_code = false;
1228
1229 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001230 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001231 ictx->rc_toggle = 0x0;
1232 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001233
1234 /* Look for the release of a button */
1235 if (keycode == KEY_RESERVED) {
1236 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001237 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001238 if (keycode != KEY_RESERVED)
1239 is_release_code = true;
1240 }
1241
1242 ictx->release_code = is_release_code;
1243
1244 return keycode;
1245}
1246
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001247static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001248{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001249 u32 keycode;
1250
1251#define MCE_KEY_MASK 0x7000
1252#define MCE_TOGGLE_BIT 0x8000
1253
1254 /*
1255 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1256 * (the toggle bit flipping between alternating key presses), while
1257 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1258 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1259 * but we can't or them into all codes, as some keys are decoded in
1260 * a different way w/o the same use of the toggle bit...
1261 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001262 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001263 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1264
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001265 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001266 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001267
1268 /* not used in mce mode, but make sure we know its false */
1269 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001270
1271 return keycode;
1272}
1273
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001274static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001275{
1276 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001277 u32 keycode = KEY_RESERVED;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001278 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001279
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001280 for (i = 0; key_table[i].hw_code != 0; i++) {
1281 if (key_table[i].hw_code == (code | 0xffee)) {
1282 keycode = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001283 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001284 }
1285 }
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001286 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001287 return keycode;
1288}
1289
1290static bool imon_mouse_event(struct imon_context *ictx,
1291 unsigned char *buf, int len)
1292{
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001293 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001294 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001295 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001296 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001297 unsigned long flags;
1298
1299 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001300
1301 /* newer iMON device PAD or mouse button */
1302 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1303 rel_x = buf[2];
1304 rel_y = buf[3];
1305 right_shift = 1;
1306 /* 0xffdc iMON PAD or mouse button input */
1307 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1308 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1309 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1310 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1311 if (buf[0] & 0x02)
1312 rel_x |= ~0x0f;
1313 rel_x = rel_x + rel_x / 2;
1314 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1315 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1316 if (buf[0] & 0x01)
1317 rel_y |= ~0x0f;
1318 rel_y = rel_y + rel_y / 2;
1319 right_shift = 2;
1320 /* some ffdc devices decode mouse buttons differently... */
1321 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1322 right_shift = 2;
1323 /* ch+/- buttons, which we use for an emulated scroll wheel */
1324 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1325 dir = 1;
1326 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1327 dir = -1;
1328 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001329 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001330
Jarod Wilson693508d2010-09-15 15:56:03 -03001331 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1332
Jarod Wilson21677cf2010-04-16 18:29:02 -03001333 if (mouse_input) {
1334 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1335
1336 if (dir) {
1337 input_report_rel(ictx->idev, REL_WHEEL, dir);
1338 } else if (rel_x || rel_y) {
1339 input_report_rel(ictx->idev, REL_X, rel_x);
1340 input_report_rel(ictx->idev, REL_Y, rel_y);
1341 } else {
1342 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1343 input_report_key(ictx->idev, BTN_RIGHT,
1344 buf[1] >> right_shift & 0x1);
1345 }
1346 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001347 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001348 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001349 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001350 }
1351
1352 return mouse_input;
1353}
1354
1355static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1356{
1357 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1358 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1359 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1360 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1361 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1362 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1363 input_sync(ictx->touch);
1364}
1365
1366static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1367{
1368 int dir = 0;
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001369 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001370 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001371 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001372 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001373
1374 /*
1375 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1376 * contain a position coordinate (x,y), with each component ranging
1377 * from -14 to 14. We want to down-sample this to only 4 discrete values
1378 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001379 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001380 * ignore when they get too close.
1381 */
1382 if (ictx->product != 0xffdc) {
1383 /* first, pad to 8 bytes so it conforms with everything else */
1384 buf[5] = buf[6] = buf[7] = 0;
1385 timeout = 500; /* in msecs */
1386 /* (2*threshold) x (2*threshold) square */
1387 threshold = pad_thresh ? pad_thresh : 28;
1388 rel_x = buf[2];
1389 rel_y = buf[3];
1390
Sean Young2525fdcb2018-03-07 05:55:38 -05001391 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001392 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1393 dir = stabilize((int)rel_x, (int)rel_y,
1394 timeout, threshold);
1395 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001396 spin_lock_irqsave(&ictx->kc_lock,
1397 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001398 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001399 spin_unlock_irqrestore(&ictx->kc_lock,
1400 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001401 return;
1402 }
1403 buf[2] = dir & 0xFF;
1404 buf[3] = (dir >> 8) & 0xFF;
Hans Verkuild778d252014-08-20 19:34:33 -03001405 scancode = be32_to_cpu(*((__be32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001406 }
1407 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001408 /*
1409 * Hack alert: instead of using keycodes, we have
1410 * to use hard-coded scancodes here...
1411 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001412 if (abs(rel_y) > abs(rel_x)) {
1413 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1414 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001415 if (rel_y > 0)
1416 scancode = 0x01007f00; /* KEY_DOWN */
1417 else
1418 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001419 } else {
1420 buf[2] = 0;
1421 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001422 if (rel_x > 0)
1423 scancode = 0x0100007f; /* KEY_RIGHT */
1424 else
1425 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001426 }
1427 }
1428
1429 /*
1430 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1431 * device (15c2:ffdc). The remote generates various codes from
1432 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1433 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1434 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001435 * reversed endianness. Extract direction from buffer, rotate endianness,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001436 * adjust sign and feed the values into stabilize(). The resulting codes
1437 * will be 0x01008000, 0x01007F00, which match the newer devices.
1438 */
1439 } else {
1440 timeout = 10; /* in msecs */
1441 /* (2*threshold) x (2*threshold) square */
1442 threshold = pad_thresh ? pad_thresh : 15;
1443
1444 /* buf[1] is x */
1445 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1446 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1447 if (buf[0] & 0x02)
1448 rel_x |= ~0x10+1;
1449 /* buf[2] is y */
1450 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1451 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1452 if (buf[0] & 0x01)
1453 rel_y |= ~0x10+1;
1454
1455 buf[0] = 0x01;
1456 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1457
Sean Young2525fdcb2018-03-07 05:55:38 -05001458 if (ictx->rc_proto == RC_PROTO_BIT_IMON && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001459 dir = stabilize((int)rel_x, (int)rel_y,
1460 timeout, threshold);
1461 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001462 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001463 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001464 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001465 return;
1466 }
1467 buf[2] = dir & 0xFF;
1468 buf[3] = (dir >> 8) & 0xFF;
Hans Verkuild778d252014-08-20 19:34:33 -03001469 scancode = be32_to_cpu(*((__be32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001470 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001471 /*
1472 * Hack alert: instead of using keycodes, we have
1473 * to use hard-coded scancodes here...
1474 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001475 if (abs(rel_y) > abs(rel_x)) {
1476 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1477 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001478 if (rel_y > 0)
1479 scancode = 0x01007f00; /* KEY_DOWN */
1480 else
1481 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001482 } else {
1483 buf[2] = 0;
1484 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001485 if (rel_x > 0)
1486 scancode = 0x0100007f; /* KEY_RIGHT */
1487 else
1488 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001489 }
1490 }
1491 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001492
Jarod Wilson693508d2010-09-15 15:56:03 -03001493 if (scancode) {
1494 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001495 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001496 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1497 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001498}
1499
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001500/*
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001501 * figure out if these is a press or a release. We don't actually
1502 * care about repeats, as those will be auto-generated within the IR
1503 * subsystem for repeating scancodes.
1504 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001505static int imon_parse_press_type(struct imon_context *ictx,
1506 unsigned char *buf, u8 ktype)
1507{
1508 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001509 unsigned long flags;
1510
1511 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001512
1513 /* key release of 0x02XXXXXX key */
1514 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1515 ictx->kc = ictx->last_keycode;
1516
1517 /* mouse button release on (some) 0xffdc devices */
1518 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1519 buf[2] == 0x81 && buf[3] == 0xb7)
1520 ictx->kc = ictx->last_keycode;
1521
1522 /* mouse button release on (some other) 0xffdc devices */
1523 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1524 buf[2] == 0x81 && buf[3] == 0xb7)
1525 ictx->kc = ictx->last_keycode;
1526
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001527 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001528 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001529 ictx->rc_toggle = buf[2];
1530 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001531
1532 /* incoherent or irrelevant data */
1533 } else if (ictx->kc == KEY_RESERVED)
1534 press_type = -EINVAL;
1535
1536 /* key release of 0xXXXXXXb7 key */
1537 else if (ictx->release_code)
1538 press_type = 0;
1539
1540 /* this is a button press */
1541 else
1542 press_type = 1;
1543
Jarod Wilson693508d2010-09-15 15:56:03 -03001544 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1545
Jarod Wilson21677cf2010-04-16 18:29:02 -03001546 return press_type;
1547}
1548
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001549/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001550 * Process the incoming packet
1551 */
Sean Young1b450f22018-01-06 07:24:50 -05001552static void imon_incoming_packet(struct imon_context *ictx,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001553 struct urb *urb, int intf)
1554{
1555 int len = urb->actual_length;
1556 unsigned char *buf = urb->transfer_buffer;
1557 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001558 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001559 u32 kc;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001560 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001561 int press_type = 0;
Chunyan Zhang1edba642017-11-06 09:06:10 -05001562 long msec;
1563 ktime_t t;
1564 static ktime_t prev_time;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001565 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001566
Jarod Wilson21677cf2010-04-16 18:29:02 -03001567 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001568 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001569 return;
1570
1571 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001572 if (len == 8 && buf[7] == 0xee) {
Hans Verkuild778d252014-08-20 19:34:33 -03001573 scancode = be64_to_cpu(*((__be64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001574 ktype = IMON_KEY_PANEL;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001575 kc = imon_panel_key_lookup(ictx, scancode);
Ulrich Eckhardt6ddc2be2014-07-26 15:01:12 -03001576 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001577 } else {
Hans Verkuild778d252014-08-20 19:34:33 -03001578 scancode = be32_to_cpu(*((__be32 *)buf));
Sean Young6d741bf2017-08-07 16:20:58 -04001579 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001580 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001581 if (buf[0] == 0x80)
1582 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001583 kc = imon_mce_key_lookup(ictx, scancode);
1584 } else {
1585 ktype = IMON_KEY_IMON;
1586 kc = imon_remote_key_lookup(ictx, scancode);
1587 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001588 }
1589
Jarod Wilson693508d2010-09-15 15:56:03 -03001590 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001591 /* keyboard/mouse mode toggle button */
1592 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1593 ictx->last_keycode = kc;
1594 if (!nomouse) {
Arnd Bergmannbd7e31b2017-05-11 08:46:44 -03001595 ictx->pad_mouse = !ictx->pad_mouse;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001596 dev_dbg(dev, "toggling to %s mode\n",
1597 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001598 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001599 return;
1600 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001601 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001602 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1603 }
1604 }
1605
1606 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001607 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001608
1609 /* send touchscreen events through input subsystem if touchpad data */
1610 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1611 buf[7] == 0x86) {
1612 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001613 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001614
1615 /* look for mouse events with pad in mouse mode */
1616 } else if (ictx->pad_mouse) {
1617 if (imon_mouse_event(ictx, buf, len))
1618 return;
1619 }
1620
1621 /* Now for some special handling to convert pad input to arrow keys */
1622 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1623 ((len == 8) && (buf[0] & 0x40) &&
1624 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1625 len = 8;
1626 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001627 }
1628
1629 if (debug) {
Mauro Carvalho Chehab832d40c2016-10-14 07:48:35 -03001630 printk(KERN_INFO "intf%d decoded packet: %*ph\n",
1631 intf, len, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001632 }
1633
1634 press_type = imon_parse_press_type(ictx, buf, ktype);
1635 if (press_type < 0)
1636 goto not_input_data;
1637
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001638 if (ktype != IMON_KEY_PANEL) {
1639 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001640 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001641 else {
Sean Young2525fdcb2018-03-07 05:55:38 -05001642 enum rc_proto proto;
1643
1644 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
1645 proto = RC_PROTO_RC6_MCE;
1646 else if (ictx->rc_proto == RC_PROTO_BIT_IMON)
1647 proto = RC_PROTO_IMON;
1648 else
1649 return;
1650
1651 rc_keydown(ictx->rdev, proto, ictx->rc_scancode,
1652 ictx->rc_toggle);
1653
Jarod Wilson693508d2010-09-15 15:56:03 -03001654 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001655 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001656 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001657 }
1658 return;
1659 }
1660
1661 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001662 spin_lock_irqsave(&ictx->kc_lock, flags);
1663
Chunyan Zhang1edba642017-11-06 09:06:10 -05001664 t = ktime_get();
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001665 /* KEY_MUTE repeats from knob need to be suppressed */
1666 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Chunyan Zhang1edba642017-11-06 09:06:10 -05001667 msec = ktime_ms_delta(t, prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001668 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001669 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001670 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001671 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001672 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001673 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001674 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001675
Jarod Wilson693508d2010-09-15 15:56:03 -03001676 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001677
Jarod Wilson428cc762010-10-23 16:42:20 -03001678 input_report_key(ictx->idev, kc, press_type);
1679 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001680
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001681 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001682 input_report_key(ictx->idev, kc, 0);
1683 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001684
Jarod Wilson94215cc2011-06-04 14:14:41 -03001685 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001686 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001687 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001688
1689 return;
1690
Jarod Wilson21677cf2010-04-16 18:29:02 -03001691not_input_data:
1692 if (len != 8) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001693 dev_warn(dev, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
1694 __func__, len, intf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001695 return;
1696 }
1697
1698 /* iMON 2.4G associate frame */
1699 if (buf[0] == 0x00 &&
1700 buf[2] == 0xFF && /* REFID */
1701 buf[3] == 0xFF &&
1702 buf[4] == 0xFF &&
1703 buf[5] == 0xFF && /* iMON 2.4G */
1704 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1705 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1706 dev_warn(dev, "%s: remote associated refid=%02X\n",
1707 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001708 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001709 }
1710}
1711
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05001712/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03001713 * Callback function for USB core API: receive data
1714 */
1715static void usb_rx_callback_intf0(struct urb *urb)
1716{
1717 struct imon_context *ictx;
1718 int intfnum = 0;
1719
1720 if (!urb)
1721 return;
1722
1723 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001724 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001725 return;
1726
Jarod Wilson8791d632012-01-26 12:04:11 -03001727 /*
1728 * if we get a callback before we're done configuring the hardware, we
1729 * can't yet process the data, as there's nowhere to send it, but we
1730 * still need to submit a new rx URB to avoid wedging the hardware
1731 */
1732 if (!ictx->dev_present_intf0)
1733 goto out;
1734
Jarod Wilson21677cf2010-04-16 18:29:02 -03001735 switch (urb->status) {
1736 case -ENOENT: /* usbcore unlink successful! */
1737 return;
1738
1739 case -ESHUTDOWN: /* transport endpoint was shut down */
1740 break;
1741
1742 case 0:
Sean Young1b450f22018-01-06 07:24:50 -05001743 imon_incoming_packet(ictx, urb, intfnum);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001744 break;
1745
1746 default:
1747 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1748 __func__, urb->status);
1749 break;
1750 }
1751
Jarod Wilson8791d632012-01-26 12:04:11 -03001752out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001753 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1754}
1755
1756static void usb_rx_callback_intf1(struct urb *urb)
1757{
1758 struct imon_context *ictx;
1759 int intfnum = 1;
1760
1761 if (!urb)
1762 return;
1763
1764 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001765 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001766 return;
1767
Jarod Wilson8791d632012-01-26 12:04:11 -03001768 /*
1769 * if we get a callback before we're done configuring the hardware, we
1770 * can't yet process the data, as there's nowhere to send it, but we
1771 * still need to submit a new rx URB to avoid wedging the hardware
1772 */
1773 if (!ictx->dev_present_intf1)
1774 goto out;
1775
Jarod Wilson21677cf2010-04-16 18:29:02 -03001776 switch (urb->status) {
1777 case -ENOENT: /* usbcore unlink successful! */
1778 return;
1779
1780 case -ESHUTDOWN: /* transport endpoint was shut down */
1781 break;
1782
1783 case 0:
Sean Young1b450f22018-01-06 07:24:50 -05001784 imon_incoming_packet(ictx, urb, intfnum);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001785 break;
1786
1787 default:
1788 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1789 __func__, urb->status);
1790 break;
1791 }
1792
Jarod Wilson8791d632012-01-26 12:04:11 -03001793out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001794 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1795}
1796
Jarod Wilson04292fc2010-09-15 00:28:41 -03001797/*
1798 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1799 * devices, and all of them constantly spew interrupts, even when there
1800 * is no actual data to report. However, byte 6 of this buffer looks like
1801 * its unique across device variants, so we're trying to key off that to
1802 * figure out which display type (if any) and what IR protocol the device
1803 * actually supports. These devices have their IR protocol hard-coded into
1804 * their firmware, they can't be changed on the fly like the newer hardware.
1805 */
1806static void imon_get_ffdc_type(struct imon_context *ictx)
1807{
1808 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1809 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
Sean Young2525fdcb2018-03-07 05:55:38 -05001810 u64 allowed_protos = RC_PROTO_BIT_IMON;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001811
1812 switch (ffdc_cfg_byte) {
1813 /* iMON Knob, no display, iMON IR + vol knob */
1814 case 0x21:
1815 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1816 ictx->display_supported = false;
1817 break;
1818 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1819 case 0x4e:
1820 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1821 ictx->display_supported = false;
1822 ictx->rf_device = true;
1823 break;
1824 /* iMON VFD, no IR (does have vol knob tho) */
1825 case 0x35:
1826 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1827 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1828 break;
1829 /* iMON VFD, iMON IR */
1830 case 0x24:
Sean Young6d4a36d2017-11-21 15:51:39 -05001831 case 0x30:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001832 case 0x85:
1833 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1834 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1835 break;
1836 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001837 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001838 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001839 case 0x9e:
1840 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1841 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Sean Young6d741bf2017-08-07 16:20:58 -04001842 allowed_protos = RC_PROTO_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001843 break;
1844 /* iMON LCD, MCE IR */
1845 case 0x9f:
1846 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1847 detected_display_type = IMON_DISPLAY_TYPE_LCD;
Sean Young6d741bf2017-08-07 16:20:58 -04001848 allowed_protos = RC_PROTO_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001849 break;
Sean Young6a489f72017-12-02 06:10:34 -05001850 /* no display, iMON IR */
1851 case 0x26:
1852 dev_info(ictx->dev, "0xffdc iMON Inside, iMON IR");
1853 ictx->display_supported = false;
1854 break;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001855 default:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001856 dev_info(ictx->dev, "Unknown 0xffdc device, defaulting to VFD and iMON IR");
Jarod Wilson04292fc2010-09-15 00:28:41 -03001857 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Sean Young2525fdcb2018-03-07 05:55:38 -05001858 /*
1859 * We don't know which one it is, allow user to set the
1860 * RC6 one from userspace if IMON wasn't correct.
1861 */
Sean Young6d741bf2017-08-07 16:20:58 -04001862 allowed_protos |= RC_PROTO_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001863 break;
1864 }
1865
1866 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1867
1868 ictx->display_type = detected_display_type;
Sean Young6d741bf2017-08-07 16:20:58 -04001869 ictx->rc_proto = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001870}
1871
1872static void imon_set_display_type(struct imon_context *ictx)
1873{
1874 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1875
1876 /*
1877 * Try to auto-detect the type of display if the user hasn't set
1878 * it by hand via the display_type modparam. Default is VFD.
1879 */
1880
1881 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1882 switch (ictx->product) {
1883 case 0xffdc:
1884 /* set in imon_get_ffdc_type() */
1885 configured_display_type = ictx->display_type;
1886 break;
1887 case 0x0034:
1888 case 0x0035:
1889 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1890 break;
1891 case 0x0038:
1892 case 0x0039:
1893 case 0x0045:
1894 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1895 break;
1896 case 0x003c:
1897 case 0x0041:
1898 case 0x0042:
1899 case 0x0043:
1900 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1901 ictx->display_supported = false;
1902 break;
1903 case 0x0036:
1904 case 0x0044:
1905 default:
1906 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1907 break;
1908 }
1909 } else {
1910 configured_display_type = display_type;
1911 if (display_type == IMON_DISPLAY_TYPE_NONE)
1912 ictx->display_supported = false;
1913 else
1914 ictx->display_supported = true;
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02001915 dev_info(ictx->dev, "%s: overriding display type to %d via modparam\n",
1916 __func__, display_type);
Jarod Wilson04292fc2010-09-15 00:28:41 -03001917 }
1918
1919 ictx->display_type = configured_display_type;
1920}
1921
David Härdemand8b4b582010-10-29 16:08:23 -03001922static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001923{
David Härdemand8b4b582010-10-29 16:08:23 -03001924 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001925 int ret;
Colin Ian Kingc25895c2017-09-05 09:07:50 -03001926 static const unsigned char fp_packet[] = {
1927 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001928
Sean Young1b450f22018-01-06 07:24:50 -05001929 rdev = rc_allocate_device(RC_DRIVER_SCANCODE);
David Härdemand8b4b582010-10-29 16:08:23 -03001930 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001931 dev_err(ictx->dev, "remote control dev allocation failed\n");
1932 goto out;
1933 }
1934
1935 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1936 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1937 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1938 sizeof(ictx->phys_rdev));
1939 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1940
Sean Young518f4b22017-07-01 12:13:19 -04001941 rdev->device_name = ictx->name_rdev;
David Härdemand8b4b582010-10-29 16:08:23 -03001942 rdev->input_phys = ictx->phys_rdev;
1943 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001944 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001945
David Härdemand8b4b582010-10-29 16:08:23 -03001946 rdev->priv = ictx;
Sean Young1b450f22018-01-06 07:24:50 -05001947 /* iMON PAD or MCE */
Sean Young2525fdcb2018-03-07 05:55:38 -05001948 rdev->allowed_protocols = RC_PROTO_BIT_IMON | RC_PROTO_BIT_RC6_MCE;
David Härdemand8b4b582010-10-29 16:08:23 -03001949 rdev->change_protocol = imon_ir_change_protocol;
1950 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001951
Jarod Wilson04292fc2010-09-15 00:28:41 -03001952 /* Enable front-panel buttons and/or knobs */
1953 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1954 ret = send_packet(ictx);
1955 /* Not fatal, but warn about it */
1956 if (ret)
1957 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1958
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001959 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001960 imon_get_ffdc_type(ictx);
Sean Young6d741bf2017-08-07 16:20:58 -04001961 rdev->allowed_protocols = ictx->rc_proto;
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001962 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001963
1964 imon_set_display_type(ictx);
1965
Sean Young1b450f22018-01-06 07:24:50 -05001966 if (ictx->rc_proto == RC_PROTO_BIT_RC6_MCE)
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001967 rdev->map_name = RC_MAP_IMON_MCE;
1968 else
1969 rdev->map_name = RC_MAP_IMON_PAD;
1970
David Härdemand8b4b582010-10-29 16:08:23 -03001971 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001972 if (ret < 0) {
1973 dev_err(ictx->dev, "remote input dev register failed\n");
1974 goto out;
1975 }
1976
1977 return rdev;
1978
1979out:
David Härdemand8b4b582010-10-29 16:08:23 -03001980 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001981 return NULL;
1982}
1983
Jarod Wilson21677cf2010-04-16 18:29:02 -03001984static struct input_dev *imon_init_idev(struct imon_context *ictx)
1985{
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001986 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001987 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001988 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001989
1990 idev = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03001991 if (!idev)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001992 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001993
Jarod Wilson21677cf2010-04-16 18:29:02 -03001994 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001995 "iMON Panel, Knob and Mouse(%04x:%04x)",
1996 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001997 idev->name = ictx->name_idev;
1998
1999 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2000 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002001 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002002 idev->phys = ictx->phys_idev;
2003
Jarod Wilsondb190fc2010-04-30 16:06:12 -03002004 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002005
2006 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2007 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2008 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2009 BIT_MASK(REL_WHEEL);
2010
2011 /* panel and/or knob code support */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002012 for (i = 0; key_table[i].hw_code != 0; i++) {
2013 u32 kc = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002014 __set_bit(kc, idev->keybit);
2015 }
2016
Jarod Wilson21677cf2010-04-16 18:29:02 -03002017 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2018 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002019 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002020
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002021 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002022 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002023 dev_err(ictx->dev, "input dev register failed\n");
2024 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002025 }
2026
2027 return idev;
2028
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002029out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002030 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002031 return NULL;
2032}
2033
2034static struct input_dev *imon_init_touch(struct imon_context *ictx)
2035{
2036 struct input_dev *touch;
2037 int ret;
2038
2039 touch = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03002040 if (!touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002041 goto touch_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002042
2043 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2044 "iMON USB Touchscreen (%04x:%04x)",
2045 ictx->vendor, ictx->product);
2046 touch->name = ictx->name_touch;
2047
2048 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2049 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002050 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002051 touch->phys = ictx->phys_touch;
2052
2053 touch->evbit[0] =
2054 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2055 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2056 BIT_MASK(BTN_TOUCH);
2057 input_set_abs_params(touch, ABS_X,
2058 0x00, 0xfff, 0, 0);
2059 input_set_abs_params(touch, ABS_Y,
2060 0x00, 0xfff, 0, 0);
2061
2062 input_set_drvdata(touch, ictx);
2063
2064 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2065 touch->dev.parent = ictx->dev;
2066 ret = input_register_device(touch);
2067 if (ret < 0) {
2068 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2069 goto touch_register_failed;
2070 }
2071
2072 return touch;
2073
2074touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03002075 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002076
2077touch_alloc_failed:
2078 return NULL;
2079}
2080
2081static bool imon_find_endpoints(struct imon_context *ictx,
2082 struct usb_host_interface *iface_desc)
2083{
2084 struct usb_endpoint_descriptor *ep;
2085 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2086 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2087 int ifnum = iface_desc->desc.bInterfaceNumber;
2088 int num_endpts = iface_desc->desc.bNumEndpoints;
2089 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002090 bool ir_ep_found = false;
2091 bool display_ep_found = false;
2092 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002093
2094 /*
2095 * Scan the endpoint list and set:
2096 * first input endpoint = IR endpoint
2097 * first output endpoint = display endpoint
2098 */
2099 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2100 ep = &iface_desc->endpoint[i].desc;
2101 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
Himangi Saraogi9408d8f2014-08-15 13:18:53 -03002102 ep_type = usb_endpoint_type(ep);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002103
2104 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2105 ep_type == USB_ENDPOINT_XFER_INT) {
2106
2107 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002108 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002109 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2110
2111 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2112 ep_type == USB_ENDPOINT_XFER_INT) {
2113 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002114 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002115 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2116 }
2117 }
2118
2119 if (ifnum == 0) {
2120 ictx->rx_endpoint_intf0 = rx_endpoint;
2121 /*
2122 * tx is used to send characters to lcd/vfd, associate RF
2123 * remotes, set IR protocol, and maybe more...
2124 */
2125 ictx->tx_endpoint = tx_endpoint;
2126 } else {
2127 ictx->rx_endpoint_intf1 = rx_endpoint;
2128 }
2129
2130 /*
2131 * If we didn't find a display endpoint, this is probably one of the
2132 * newer iMON devices that use control urb instead of interrupt
2133 */
2134 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002135 tx_control = true;
2136 display_ep_found = true;
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002137 dev_dbg(ictx->dev, "%s: device uses control endpoint, not interface OUT endpoint\n",
2138 __func__);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002139 }
2140
2141 /*
2142 * Some iMON receivers have no display. Unfortunately, it seems
2143 * that SoundGraph recycles device IDs between devices both with
2144 * and without... :\
2145 */
2146 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002147 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002148 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2149 }
2150
2151 /*
2152 * iMON Touch devices have a VGA touchscreen, but no "display", as
2153 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2154 */
2155 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002156 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002157 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2158 }
2159
2160 /* Input endpoint is mandatory */
2161 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002162 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002163
2164 ictx->tx_control = tx_control;
2165
2166 if (display_ep_found)
2167 ictx->display_supported = true;
2168
2169 return ir_ep_found;
2170
2171}
2172
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002173static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2174 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002175{
2176 struct imon_context *ictx;
2177 struct urb *rx_urb;
2178 struct urb *tx_urb;
2179 struct device *dev = &intf->dev;
2180 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002181 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002182
Markus Elfringa8c779e2017-08-29 07:45:59 -03002183 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL);
Markus Elfring3e70b252017-08-29 07:40:07 -03002184 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002185 goto exit;
Markus Elfring3e70b252017-08-29 07:40:07 -03002186
Jarod Wilson21677cf2010-04-16 18:29:02 -03002187 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002188 if (!rx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002189 goto rx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002190 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002191 if (!tx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002192 goto tx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002193
2194 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002195 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002196
2197 mutex_lock(&ictx->lock);
2198
2199 ictx->dev = dev;
2200 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002201 ictx->rx_urb_intf0 = rx_urb;
2202 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002203 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002204
Daniel Wagner7c073ff2016-09-16 08:18:21 -03002205 init_completion(&ictx->tx.finished);
2206
Jarod Wilson21677cf2010-04-16 18:29:02 -03002207 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2208 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2209
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002210 /* save drive info for later accessing the panel/knob key table */
2211 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002212 /* default send_packet delay is 5ms but some devices need more */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002213 ictx->send_packet_delay = ictx->dev_descr->flags &
2214 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002215
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002216 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002217 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002218 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002219 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002220 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002221
Jarod Wilson21677cf2010-04-16 18:29:02 -03002222 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2223 usb_rcvintpipe(ictx->usbdev_intf0,
2224 ictx->rx_endpoint_intf0->bEndpointAddress),
2225 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2226 usb_rx_callback_intf0, ictx,
2227 ictx->rx_endpoint_intf0->bInterval);
2228
2229 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2230 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002231 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002232 goto urb_submit_failed;
2233 }
2234
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002235 ictx->idev = imon_init_idev(ictx);
2236 if (!ictx->idev) {
2237 dev_err(dev, "%s: input device setup failed\n", __func__);
2238 goto idev_setup_failed;
2239 }
2240
2241 ictx->rdev = imon_init_rdev(ictx);
2242 if (!ictx->rdev) {
2243 dev_err(dev, "%s: rc device setup failed\n", __func__);
2244 goto rdev_setup_failed;
2245 }
2246
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002247 ictx->dev_present_intf0 = true;
2248
Jarod Wilson23ef7102011-04-27 19:01:44 -03002249 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002250 return ictx;
2251
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002252rdev_setup_failed:
2253 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002254idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002255 usb_kill_urb(ictx->rx_urb_intf0);
2256urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002257find_endpoint_failed:
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002258 usb_put_dev(ictx->usbdev_intf0);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002259 mutex_unlock(&ictx->lock);
2260 usb_free_urb(tx_urb);
2261tx_urb_alloc_failed:
2262 usb_free_urb(rx_urb);
2263rx_urb_alloc_failed:
2264 kfree(ictx);
2265exit:
2266 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2267
2268 return NULL;
2269}
2270
2271static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2272 struct imon_context *ictx)
2273{
2274 struct urb *rx_urb;
2275 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002276 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002277
2278 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang15241912016-08-11 18:03:39 -03002279 if (!rx_urb)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002280 goto rx_urb_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002281
2282 mutex_lock(&ictx->lock);
2283
2284 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Kees Cookb17ec782017-10-24 11:23:14 -04002285 timer_setup(&ictx->ttimer, imon_touch_display_timeout, 0);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002286 }
2287
2288 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002289 ictx->rx_urb_intf1 = rx_urb;
2290
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002291 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002292 iface_desc = intf->cur_altsetting;
2293 if (!imon_find_endpoints(ictx, iface_desc))
2294 goto find_endpoint_failed;
2295
2296 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2297 ictx->touch = imon_init_touch(ictx);
2298 if (!ictx->touch)
2299 goto touch_setup_failed;
2300 } else
2301 ictx->touch = NULL;
2302
2303 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2304 usb_rcvintpipe(ictx->usbdev_intf1,
2305 ictx->rx_endpoint_intf1->bEndpointAddress),
2306 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2307 usb_rx_callback_intf1, ictx,
2308 ictx->rx_endpoint_intf1->bInterval);
2309
2310 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2311
2312 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002313 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002314 goto urb_submit_failed;
2315 }
2316
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002317 ictx->dev_present_intf1 = true;
2318
Jarod Wilson23ef7102011-04-27 19:01:44 -03002319 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002320 return ictx;
2321
2322urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002323 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002324 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002325touch_setup_failed:
2326find_endpoint_failed:
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002327 usb_put_dev(ictx->usbdev_intf1);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002328 mutex_unlock(&ictx->lock);
2329 usb_free_urb(rx_urb);
2330rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002331 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002332
2333 return NULL;
2334}
2335
Jarod Wilson21677cf2010-04-16 18:29:02 -03002336static void imon_init_display(struct imon_context *ictx,
2337 struct usb_interface *intf)
2338{
2339 int ret;
2340
2341 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2342
2343 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002344 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002345 if (ret)
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002346 dev_err(ictx->dev, "Could not create display sysfs entries(%d)",
2347 ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002348
2349 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2350 ret = usb_register_dev(intf, &imon_lcd_class);
2351 else
2352 ret = usb_register_dev(intf, &imon_vfd_class);
2353 if (ret)
2354 /* Not a fatal error, so ignore */
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002355 dev_info(ictx->dev, "could not get a minor number for display\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002356
2357}
2358
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05002359/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03002360 * Callback function for USB core API: Probe
2361 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002362static int imon_probe(struct usb_interface *interface,
2363 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002364{
2365 struct usb_device *usbdev = NULL;
2366 struct usb_host_interface *iface_desc = NULL;
2367 struct usb_interface *first_if;
2368 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002369 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002370 int ret = 0;
2371 struct imon_context *ictx = NULL;
2372 struct imon_context *first_if_ctx = NULL;
2373 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002374
Jarod Wilson21677cf2010-04-16 18:29:02 -03002375 usbdev = usb_get_dev(interface_to_usbdev(interface));
2376 iface_desc = interface->cur_altsetting;
2377 ifnum = iface_desc->desc.bInterfaceNumber;
2378 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2379 product = le16_to_cpu(usbdev->descriptor.idProduct);
2380
2381 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2382 __func__, vendor, product, ifnum);
2383
2384 /* prevent races probing devices w/multiple interfaces */
2385 mutex_lock(&driver_lock);
2386
2387 first_if = usb_ifnum_to_if(usbdev, 0);
Arvind Yadav58fd55e2017-10-09 20:14:48 +02002388 if (!first_if) {
2389 ret = -ENODEV;
2390 goto fail;
2391 }
2392
Joe Perches8350e152010-11-30 18:42:07 -03002393 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002394
2395 if (ifnum == 0) {
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002396 ictx = imon_init_intf0(interface, id);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002397 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002398 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002399 ret = -ENODEV;
2400 goto fail;
2401 }
2402
Jarod Wilson21677cf2010-04-16 18:29:02 -03002403 } else {
Kevin Baradon7d54ba02013-04-22 16:09:45 -03002404 /* this is the secondary interface on the device */
2405
2406 /* fail early if first intf failed to register */
2407 if (!first_if_ctx) {
2408 ret = -ENODEV;
2409 goto fail;
2410 }
2411
Jarod Wilson21677cf2010-04-16 18:29:02 -03002412 ictx = imon_init_intf1(interface, first_if_ctx);
2413 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002414 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002415 ret = -ENODEV;
2416 goto fail;
2417 }
2418
2419 }
2420
2421 usb_set_intfdata(interface, ictx);
2422
2423 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002424 mutex_lock(&ictx->lock);
2425
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002426 if (product == 0xffdc && ictx->rf_device) {
2427 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002428 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002429 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002430 pr_err("Could not create RF sysfs entries(%d)\n",
2431 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002432 }
2433
Jarod Wilson21677cf2010-04-16 18:29:02 -03002434 if (ictx->display_supported)
2435 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002436
2437 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002438 }
2439
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -02002440 dev_info(dev, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
2441 vendor, product, ifnum,
Jarod Wilson21677cf2010-04-16 18:29:02 -03002442 usbdev->bus->busnum, usbdev->devnum);
2443
Jarod Wilson21677cf2010-04-16 18:29:02 -03002444 mutex_unlock(&driver_lock);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002445 usb_put_dev(usbdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002446
2447 return 0;
2448
2449fail:
2450 mutex_unlock(&driver_lock);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002451 usb_put_dev(usbdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002452 dev_err(dev, "unable to register, err %d\n", ret);
2453
2454 return ret;
2455}
2456
Mauro Carvalho Chehab255940e2017-11-27 10:27:54 -05002457/*
Jarod Wilson21677cf2010-04-16 18:29:02 -03002458 * Callback function for USB core API: disconnect
2459 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002460static void imon_disconnect(struct usb_interface *interface)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002461{
2462 struct imon_context *ictx;
2463 struct device *dev;
2464 int ifnum;
2465
2466 /* prevent races with multi-interface device probing and display_open */
2467 mutex_lock(&driver_lock);
2468
2469 ictx = usb_get_intfdata(interface);
2470 dev = ictx->dev;
2471 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2472
Jarod Wilson21677cf2010-04-16 18:29:02 -03002473 /*
2474 * sysfs_remove_group is safe to call even if sysfs_create_group
2475 * hasn't been called
2476 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002477 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2478 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002479
2480 usb_set_intfdata(interface, NULL);
2481
2482 /* Abort ongoing write */
2483 if (ictx->tx.busy) {
2484 usb_kill_urb(ictx->tx_urb);
Daniel Wagner7c073ff2016-09-16 08:18:21 -03002485 complete(&ictx->tx.finished);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002486 }
2487
2488 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002489 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002490 usb_kill_urb(ictx->rx_urb_intf0);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002491 usb_put_dev(ictx->usbdev_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002492 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002493 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002494 if (ictx->display_supported) {
2495 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2496 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002497 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002498 usb_deregister_dev(interface, &imon_vfd_class);
2499 }
2500 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002501 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002502 usb_kill_urb(ictx->rx_urb_intf1);
Alexey Khoroshilove87cb472014-09-15 18:36:15 -03002503 usb_put_dev(ictx->usbdev_intf1);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002504 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002505 input_unregister_device(ictx->touch);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002506 del_timer_sync(&ictx->ttimer);
2507 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002508 }
2509
Jarod Wilson76a2d212011-04-28 18:20:58 -03002510 if (!ictx->dev_present_intf0 && !ictx->dev_present_intf1)
2511 free_imon_context(ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002512
2513 mutex_unlock(&driver_lock);
2514
2515 dev_dbg(dev, "%s: iMON device (intf%d) disconnected\n",
2516 __func__, ifnum);
2517}
2518
2519static int imon_suspend(struct usb_interface *intf, pm_message_t message)
2520{
2521 struct imon_context *ictx = usb_get_intfdata(intf);
2522 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2523
2524 if (ifnum == 0)
2525 usb_kill_urb(ictx->rx_urb_intf0);
2526 else
2527 usb_kill_urb(ictx->rx_urb_intf1);
2528
2529 return 0;
2530}
2531
2532static int imon_resume(struct usb_interface *intf)
2533{
2534 int rc = 0;
2535 struct imon_context *ictx = usb_get_intfdata(intf);
2536 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2537
2538 if (ifnum == 0) {
2539 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2540 usb_rcvintpipe(ictx->usbdev_intf0,
2541 ictx->rx_endpoint_intf0->bEndpointAddress),
2542 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2543 usb_rx_callback_intf0, ictx,
2544 ictx->rx_endpoint_intf0->bInterval);
2545
2546 rc = usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
2547
2548 } else {
2549 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2550 usb_rcvintpipe(ictx->usbdev_intf1,
2551 ictx->rx_endpoint_intf1->bEndpointAddress),
2552 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2553 usb_rx_callback_intf1, ictx,
2554 ictx->rx_endpoint_intf1->bInterval);
2555
2556 rc = usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
2557 }
2558
2559 return rc;
2560}
2561
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08002562module_usb_driver(imon_driver);