blob: 3fc759537cc6386c77964fbb2c848de1a4bef01f [file] [log] [blame]
Jarod Wilson21677cf2010-04-16 18:29:02 -03001/*
2 * imon.c: input and display driver for SoundGraph iMON IR/VFD/LCD
3 *
Jarod Wilson693508d2010-09-15 15:56:03 -03004 * Copyright(C) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson21677cf2010-04-16 18:29:02 -03005 * Portions based on the original lirc_imon driver,
6 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
7 *
8 * Huge thanks to R. Geoff Newbury for invaluable debugging on the
9 * 0xffdc iMON devices, and for sending me one to hack on, without
10 * which the support for them wouldn't be nearly as good. Thanks
11 * also to the numerous 0xffdc device owners that tested auto-config
12 * support for me and provided debug dumps from their devices.
13 *
14 * imon is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
Joe Perchese2302502010-06-20 04:20:46 -030029#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
30
Jarod Wilson21677cf2010-04-16 18:29:02 -030031#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h>
Jarod Wilson47a09b02011-07-14 14:20:46 -030037#include <linux/ratelimit.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030038
39#include <linux/input.h>
40#include <linux/usb.h>
41#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030042#include <media/rc-core.h>
Jarod Wilson21677cf2010-04-16 18:29:02 -030043
44#include <linux/time.h>
45#include <linux/timer.h>
46
47#define MOD_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
48#define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
49#define MOD_NAME "imon"
Jarod Wilson8791d632012-01-26 12:04:11 -030050#define MOD_VERSION "0.9.4"
Jarod Wilson21677cf2010-04-16 18:29:02 -030051
52#define DISPLAY_MINOR_BASE 144
53#define DEVICE_NAME "lcd%d"
54
55#define BUF_CHUNK_SIZE 8
56#define BUF_SIZE 128
57
58#define BIT_DURATION 250 /* each bit received is 250us */
59
60#define IMON_CLOCK_ENABLE_PACKETS 2
Jarod Wilson21677cf2010-04-16 18:29:02 -030061
62/*** P R O T O T Y P E S ***/
63
64/* USB Callback prototypes */
65static int imon_probe(struct usb_interface *interface,
66 const struct usb_device_id *id);
67static void imon_disconnect(struct usb_interface *interface);
68static void usb_rx_callback_intf0(struct urb *urb);
69static void usb_rx_callback_intf1(struct urb *urb);
70static void usb_tx_callback(struct urb *urb);
71
72/* suspend/resume support */
73static int imon_resume(struct usb_interface *intf);
74static int imon_suspend(struct usb_interface *intf, pm_message_t message);
75
76/* Display file_operations function prototypes */
77static int display_open(struct inode *inode, struct file *file);
78static int display_close(struct inode *inode, struct file *file);
79
80/* VFD write operation */
David Härdemand6740d82014-04-03 20:34:53 -030081static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -030082 size_t n_bytes, loff_t *pos);
83
84/* LCD file_operations override function prototypes */
David Härdemand6740d82014-04-03 20:34:53 -030085static ssize_t lcd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -030086 size_t n_bytes, loff_t *pos);
87
88/*** G L O B A L S ***/
89
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -030090struct imon_panel_key_table {
91 u64 hw_code;
92 u32 keycode;
93};
94
95struct imon_usb_dev_descr {
96 __u16 flags;
97#define IMON_NO_FLAGS 0
98#define IMON_NEED_20MS_PKT_DELAY 1
99 struct imon_panel_key_table key_table[];
100};
101
Jarod Wilson21677cf2010-04-16 18:29:02 -0300102struct imon_context {
103 struct device *dev;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300104 /* Newer devices have two interfaces */
105 struct usb_device *usbdev_intf0;
106 struct usb_device *usbdev_intf1;
107
108 bool display_supported; /* not all controllers do */
109 bool display_isopen; /* display port has been opened */
Jarod Wilsonbbe46902010-05-24 12:02:05 -0300110 bool rf_device; /* true if iMON 2.4G LT/DT RF device */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300111 bool rf_isassociating; /* RF remote associating */
112 bool dev_present_intf0; /* USB device presence, interface 0 */
113 bool dev_present_intf1; /* USB device presence, interface 1 */
114
115 struct mutex lock; /* to lock this object */
116 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
117
118 struct usb_endpoint_descriptor *rx_endpoint_intf0;
119 struct usb_endpoint_descriptor *rx_endpoint_intf1;
120 struct usb_endpoint_descriptor *tx_endpoint;
121 struct urb *rx_urb_intf0;
122 struct urb *rx_urb_intf1;
123 struct urb *tx_urb;
124 bool tx_control;
125 unsigned char usb_rx_buf[8];
126 unsigned char usb_tx_buf[8];
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300127 unsigned int send_packet_delay;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300128
129 struct tx_t {
130 unsigned char data_buf[35]; /* user data buffer */
131 struct completion finished; /* wait for write to finish */
132 bool busy; /* write in progress */
133 int status; /* status of tx completion */
134 } tx;
135
136 u16 vendor; /* usb vendor ID */
137 u16 product; /* usb product ID */
138
David Härdemand8b4b582010-10-29 16:08:23 -0300139 struct rc_dev *rdev; /* rc-core device for remote */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300140 struct input_dev *idev; /* input device for panel & IR mouse */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300141 struct input_dev *touch; /* input device for touchscreen */
142
Jarod Wilson693508d2010-09-15 15:56:03 -0300143 spinlock_t kc_lock; /* make sure we get keycodes right */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300144 u32 kc; /* current input keycode */
145 u32 last_keycode; /* last reported input keycode */
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300146 u32 rc_scancode; /* the computed remote scancode */
147 u8 rc_toggle; /* the computed remote toggle bit */
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300148 u64 rc_type; /* iMON or MCE (RC6) IR protocol? */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300149 bool release_code; /* some keys send a release code */
150
151 u8 display_type; /* store the display type */
152 bool pad_mouse; /* toggle kbd(0)/mouse(1) mode */
153
David Härdemaneaf2bcc2010-09-15 15:42:07 -0300154 char name_rdev[128]; /* rc input device name */
155 char phys_rdev[64]; /* rc input device phys path */
156
Jarod Wilson21677cf2010-04-16 18:29:02 -0300157 char name_idev[128]; /* input device name */
158 char phys_idev[64]; /* input device phys path */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300159
160 char name_touch[128]; /* touch screen name */
161 char phys_touch[64]; /* touch screen phys path */
162 struct timer_list ttimer; /* touch screen timer */
163 int touch_x; /* x coordinate on touchscreen */
164 int touch_y; /* y coordinate on touchscreen */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300165 struct imon_usb_dev_descr *dev_descr; /* device description with key
166 table for front panels */
Jarod Wilson21677cf2010-04-16 18:29:02 -0300167};
168
169#define TOUCH_TIMEOUT (HZ/30)
Jarod Wilson21677cf2010-04-16 18:29:02 -0300170
171/* vfd character device file operations */
172static const struct file_operations vfd_fops = {
173 .owner = THIS_MODULE,
174 .open = &display_open,
175 .write = &vfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200176 .release = &display_close,
177 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300178};
179
180/* lcd character device file operations */
181static const struct file_operations lcd_fops = {
182 .owner = THIS_MODULE,
183 .open = &display_open,
184 .write = &lcd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200185 .release = &display_close,
186 .llseek = noop_llseek,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300187};
188
189enum {
190 IMON_DISPLAY_TYPE_AUTO = 0,
191 IMON_DISPLAY_TYPE_VFD = 1,
192 IMON_DISPLAY_TYPE_LCD = 2,
193 IMON_DISPLAY_TYPE_VGA = 3,
194 IMON_DISPLAY_TYPE_NONE = 4,
195};
196
197enum {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300198 IMON_KEY_IMON = 0,
199 IMON_KEY_MCE = 1,
200 IMON_KEY_PANEL = 2,
201};
202
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300203static struct usb_class_driver imon_vfd_class = {
204 .name = DEVICE_NAME,
205 .fops = &vfd_fops,
206 .minor_base = DISPLAY_MINOR_BASE,
207};
208
209static struct usb_class_driver imon_lcd_class = {
210 .name = DEVICE_NAME,
211 .fops = &lcd_fops,
212 .minor_base = DISPLAY_MINOR_BASE,
213};
214
215/* imon receiver front panel/knob key table */
216static const struct imon_usb_dev_descr imon_default_table = {
217 .flags = IMON_NO_FLAGS,
218 .key_table = {
219 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
220 { 0x000000001200ffeell, KEY_UP },
221 { 0x000000001300ffeell, KEY_DOWN },
222 { 0x000000001400ffeell, KEY_LEFT },
223 { 0x000000001500ffeell, KEY_RIGHT },
224 { 0x000000001600ffeell, KEY_ENTER },
225 { 0x000000001700ffeell, KEY_ESC },
226 { 0x000000001f00ffeell, KEY_AUDIO },
227 { 0x000000002000ffeell, KEY_VIDEO },
228 { 0x000000002100ffeell, KEY_CAMERA },
229 { 0x000000002700ffeell, KEY_DVD },
230 { 0x000000002300ffeell, KEY_TV },
231 { 0x000000002b00ffeell, KEY_EXIT },
232 { 0x000000002c00ffeell, KEY_SELECT },
233 { 0x000000002d00ffeell, KEY_MENU },
234 { 0x000000000500ffeell, KEY_PREVIOUS },
235 { 0x000000000700ffeell, KEY_REWIND },
236 { 0x000000000400ffeell, KEY_STOP },
237 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
238 { 0x000000000800ffeell, KEY_FASTFORWARD },
239 { 0x000000000600ffeell, KEY_NEXT },
240 { 0x000000010000ffeell, KEY_RIGHT },
241 { 0x000001000000ffeell, KEY_LEFT },
242 { 0x000000003d00ffeell, KEY_SELECT },
243 { 0x000100000000ffeell, KEY_VOLUMEUP },
244 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
245 { 0x000000000100ffeell, KEY_MUTE },
246 /* 0xffdc iMON MCE VFD */
247 { 0x00010000ffffffeell, KEY_VOLUMEUP },
248 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
249 { 0x00000001ffffffeell, KEY_MUTE },
250 { 0x0000000fffffffeell, KEY_MEDIA },
251 { 0x00000012ffffffeell, KEY_UP },
252 { 0x00000013ffffffeell, KEY_DOWN },
253 { 0x00000014ffffffeell, KEY_LEFT },
254 { 0x00000015ffffffeell, KEY_RIGHT },
255 { 0x00000016ffffffeell, KEY_ENTER },
256 { 0x00000017ffffffeell, KEY_ESC },
257 /* iMON Knob values */
258 { 0x000100ffffffffeell, KEY_VOLUMEUP },
259 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
260 { 0x000008ffffffffeell, KEY_MUTE },
261 { 0, KEY_RESERVED },
262 }
263};
264
265static const struct imon_usb_dev_descr imon_OEM_VFD = {
266 .flags = IMON_NEED_20MS_PKT_DELAY,
267 .key_table = {
268 { 0x000000000f00ffeell, KEY_MEDIA }, /* Go */
269 { 0x000000001200ffeell, KEY_UP },
270 { 0x000000001300ffeell, KEY_DOWN },
271 { 0x000000001400ffeell, KEY_LEFT },
272 { 0x000000001500ffeell, KEY_RIGHT },
273 { 0x000000001600ffeell, KEY_ENTER },
274 { 0x000000001700ffeell, KEY_ESC },
275 { 0x000000001f00ffeell, KEY_AUDIO },
276 { 0x000000002b00ffeell, KEY_EXIT },
277 { 0x000000002c00ffeell, KEY_SELECT },
278 { 0x000000002d00ffeell, KEY_MENU },
279 { 0x000000000500ffeell, KEY_PREVIOUS },
280 { 0x000000000700ffeell, KEY_REWIND },
281 { 0x000000000400ffeell, KEY_STOP },
282 { 0x000000003c00ffeell, KEY_PLAYPAUSE },
283 { 0x000000000800ffeell, KEY_FASTFORWARD },
284 { 0x000000000600ffeell, KEY_NEXT },
285 { 0x000000010000ffeell, KEY_RIGHT },
286 { 0x000001000000ffeell, KEY_LEFT },
287 { 0x000000003d00ffeell, KEY_SELECT },
288 { 0x000100000000ffeell, KEY_VOLUMEUP },
289 { 0x010000000000ffeell, KEY_VOLUMEDOWN },
290 { 0x000000000100ffeell, KEY_MUTE },
291 /* 0xffdc iMON MCE VFD */
292 { 0x00010000ffffffeell, KEY_VOLUMEUP },
293 { 0x01000000ffffffeell, KEY_VOLUMEDOWN },
294 { 0x00000001ffffffeell, KEY_MUTE },
295 { 0x0000000fffffffeell, KEY_MEDIA },
296 { 0x00000012ffffffeell, KEY_UP },
297 { 0x00000013ffffffeell, KEY_DOWN },
298 { 0x00000014ffffffeell, KEY_LEFT },
299 { 0x00000015ffffffeell, KEY_RIGHT },
300 { 0x00000016ffffffeell, KEY_ENTER },
301 { 0x00000017ffffffeell, KEY_ESC },
302 /* iMON Knob values */
303 { 0x000100ffffffffeell, KEY_VOLUMEUP },
304 { 0x010000ffffffffeell, KEY_VOLUMEDOWN },
305 { 0x000008ffffffffeell, KEY_MUTE },
306 { 0, KEY_RESERVED },
307 }
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300308};
309
Jarod Wilson21677cf2010-04-16 18:29:02 -0300310/*
311 * USB Device ID for iMON USB Control Boards
312 *
313 * The Windows drivers contain 6 different inf files, more or less one for
314 * each new device until the 0x0034-0x0046 devices, which all use the same
315 * driver. Some of the devices in the 34-46 range haven't been definitively
316 * identified yet. Early devices have either a TriGem Computer, Inc. or a
317 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
318 * devices use the SoundGraph vendor ID (0x15c2). This driver only supports
319 * the ffdc and later devices, which do onboard decoding.
320 */
321static struct usb_device_id imon_usb_id_table[] = {
322 /*
323 * Several devices with this same device ID, all use iMON_PAD.inf
324 * SoundGraph iMON PAD (IR & VFD)
325 * SoundGraph iMON PAD (IR & LCD)
326 * SoundGraph iMON Knob (IR only)
327 */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300328 { USB_DEVICE(0x15c2, 0xffdc),
329 .driver_info = (unsigned long)&imon_default_table },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300330
331 /*
332 * Newer devices, all driven by the latest iMON Windows driver, full
333 * list of device IDs extracted via 'strings Setup/data1.hdr |grep 15c2'
334 * Need user input to fill in details on unknown devices.
335 */
336 /* SoundGraph iMON OEM Touch LCD (IR & 7" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300337 { USB_DEVICE(0x15c2, 0x0034),
338 .driver_info = (unsigned long)&imon_default_table },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300339 /* SoundGraph iMON OEM Touch LCD (IR & 4.3" VGA LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300340 { USB_DEVICE(0x15c2, 0x0035),
341 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300342 /* SoundGraph iMON OEM VFD (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300343 { USB_DEVICE(0x15c2, 0x0036),
344 .driver_info = (unsigned long)&imon_OEM_VFD },
Jarod Wilson21677cf2010-04-16 18:29:02 -0300345 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300346 { USB_DEVICE(0x15c2, 0x0037),
347 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300348 /* SoundGraph iMON OEM LCD (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300349 { USB_DEVICE(0x15c2, 0x0038),
350 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300351 /* SoundGraph iMON UltraBay (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300352 { USB_DEVICE(0x15c2, 0x0039),
353 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300354 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300355 { USB_DEVICE(0x15c2, 0x003a),
356 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300357 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300358 { USB_DEVICE(0x15c2, 0x003b),
359 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300360 /* SoundGraph iMON OEM Inside (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300361 { USB_DEVICE(0x15c2, 0x003c),
362 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300363 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300364 { USB_DEVICE(0x15c2, 0x003d),
365 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300366 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300367 { USB_DEVICE(0x15c2, 0x003e),
368 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300369 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300370 { USB_DEVICE(0x15c2, 0x003f),
371 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300372 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300373 { USB_DEVICE(0x15c2, 0x0040),
374 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300375 /* SoundGraph iMON MINI (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300376 { USB_DEVICE(0x15c2, 0x0041),
377 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300378 /* Antec Veris Multimedia Station EZ External (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300379 { USB_DEVICE(0x15c2, 0x0042),
380 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300381 /* Antec Veris Multimedia Station Basic Internal (IR only) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300382 { USB_DEVICE(0x15c2, 0x0043),
383 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300384 /* Antec Veris Multimedia Station Elite (IR & VFD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300385 { USB_DEVICE(0x15c2, 0x0044),
386 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300387 /* Antec Veris Multimedia Station Premiere (IR & LCD) */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300388 { USB_DEVICE(0x15c2, 0x0045),
389 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300390 /* device specifics unknown */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -0300391 { USB_DEVICE(0x15c2, 0x0046),
392 .driver_info = (unsigned long)&imon_default_table},
Jarod Wilson21677cf2010-04-16 18:29:02 -0300393 {}
394};
395
396/* USB Device data */
397static struct usb_driver imon_driver = {
398 .name = MOD_NAME,
399 .probe = imon_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800400 .disconnect = imon_disconnect,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300401 .suspend = imon_suspend,
402 .resume = imon_resume,
403 .id_table = imon_usb_id_table,
404};
405
Jarod Wilson21677cf2010-04-16 18:29:02 -0300406/* to prevent races between open() and disconnect(), probing, etc */
407static DEFINE_MUTEX(driver_lock);
408
409/* Module bookkeeping bits */
410MODULE_AUTHOR(MOD_AUTHOR);
411MODULE_DESCRIPTION(MOD_DESC);
412MODULE_VERSION(MOD_VERSION);
413MODULE_LICENSE("GPL");
414MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
415
416static bool debug;
417module_param(debug, bool, S_IRUGO | S_IWUSR);
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300418MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300419
420/* lcd, vfd, vga or none? should be auto-detected, but can be overridden... */
421static int display_type;
422module_param(display_type, int, S_IRUGO);
423MODULE_PARM_DESC(display_type, "Type of attached display. 0=autodetect, "
424 "1=vfd, 2=lcd, 3=vga, 4=none (default: autodetect)");
425
Jarod Wilson6718e8a2010-04-23 02:27:11 -0300426static int pad_stabilize = 1;
427module_param(pad_stabilize, int, S_IRUGO | S_IWUSR);
428MODULE_PARM_DESC(pad_stabilize, "Apply stabilization algorithm to iMON PAD "
429 "presses in arrow key mode. 0=disable, 1=enable (default).");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300430
431/*
432 * In certain use cases, mouse mode isn't really helpful, and could actually
433 * cause confusion, so allow disabling it when the IR device is open.
434 */
435static bool nomouse;
436module_param(nomouse, bool, S_IRUGO | S_IWUSR);
437MODULE_PARM_DESC(nomouse, "Disable mouse input device mode when IR device is "
438 "open. 0=don't disable, 1=disable. (default: don't disable)");
439
440/* threshold at which a pad push registers as an arrow key in kbd mode */
441static int pad_thresh;
442module_param(pad_thresh, int, S_IRUGO | S_IWUSR);
443MODULE_PARM_DESC(pad_thresh, "Threshold at which a pad push registers as an "
444 "arrow key in kbd mode (default: 28)");
445
446
447static void free_imon_context(struct imon_context *ictx)
448{
449 struct device *dev = ictx->dev;
450
451 usb_free_urb(ictx->tx_urb);
452 usb_free_urb(ictx->rx_urb_intf0);
453 usb_free_urb(ictx->rx_urb_intf1);
454 kfree(ictx);
455
456 dev_dbg(dev, "%s: iMON context freed\n", __func__);
457}
458
459/**
460 * Called when the Display device (e.g. /dev/lcd0)
461 * is opened by the application.
462 */
463static int display_open(struct inode *inode, struct file *file)
464{
465 struct usb_interface *interface;
466 struct imon_context *ictx = NULL;
467 int subminor;
468 int retval = 0;
469
470 /* prevent races with disconnect */
471 mutex_lock(&driver_lock);
472
473 subminor = iminor(inode);
474 interface = usb_find_interface(&imon_driver, subminor);
475 if (!interface) {
Joe Perchese2302502010-06-20 04:20:46 -0300476 pr_err("could not find interface for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300477 retval = -ENODEV;
478 goto exit;
479 }
480 ictx = usb_get_intfdata(interface);
481
482 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300483 pr_err("no context found for minor %d\n", subminor);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300484 retval = -ENODEV;
485 goto exit;
486 }
487
488 mutex_lock(&ictx->lock);
489
490 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300491 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300492 retval = -ENODEV;
493 } else if (ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300494 pr_err("display port is already open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300495 retval = -EBUSY;
496 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300497 ictx->display_isopen = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300498 file->private_data = ictx;
499 dev_dbg(ictx->dev, "display port opened\n");
500 }
501
502 mutex_unlock(&ictx->lock);
503
504exit:
505 mutex_unlock(&driver_lock);
506 return retval;
507}
508
509/**
510 * Called when the display device (e.g. /dev/lcd0)
511 * is closed by the application.
512 */
513static int display_close(struct inode *inode, struct file *file)
514{
515 struct imon_context *ictx = NULL;
516 int retval = 0;
517
Joe Perchesabf84382010-07-12 17:50:03 -0300518 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300519
520 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300521 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300522 return -ENODEV;
523 }
524
525 mutex_lock(&ictx->lock);
526
527 if (!ictx->display_supported) {
Joe Perchese2302502010-06-20 04:20:46 -0300528 pr_err("display not supported by device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300529 retval = -ENODEV;
530 } else if (!ictx->display_isopen) {
Joe Perchese2302502010-06-20 04:20:46 -0300531 pr_err("display is not open\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300532 retval = -EIO;
533 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300534 ictx->display_isopen = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300535 dev_dbg(ictx->dev, "display port closed\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300536 }
537
538 mutex_unlock(&ictx->lock);
539 return retval;
540}
541
542/**
Jarod Wilson23ef7102011-04-27 19:01:44 -0300543 * Sends a packet to the device -- this function must be called with
544 * ictx->lock held, or its unlock/lock sequence while waiting for tx
545 * to complete can/will lead to a deadlock.
Jarod Wilson21677cf2010-04-16 18:29:02 -0300546 */
547static int send_packet(struct imon_context *ictx)
548{
549 unsigned int pipe;
550 unsigned long timeout;
551 int interval = 0;
552 int retval = 0;
553 struct usb_ctrlrequest *control_req = NULL;
554
555 /* Check if we need to use control or interrupt urb */
556 if (!ictx->tx_control) {
557 pipe = usb_sndintpipe(ictx->usbdev_intf0,
558 ictx->tx_endpoint->bEndpointAddress);
559 interval = ictx->tx_endpoint->bInterval;
560
561 usb_fill_int_urb(ictx->tx_urb, ictx->usbdev_intf0, pipe,
562 ictx->usb_tx_buf,
563 sizeof(ictx->usb_tx_buf),
564 usb_tx_callback, ictx, interval);
565
566 ictx->tx_urb->actual_length = 0;
567 } else {
568 /* fill request into kmalloc'ed space: */
569 control_req = kmalloc(sizeof(struct usb_ctrlrequest),
570 GFP_KERNEL);
571 if (control_req == NULL)
572 return -ENOMEM;
573
574 /* setup packet is '21 09 0200 0001 0008' */
575 control_req->bRequestType = 0x21;
576 control_req->bRequest = 0x09;
577 control_req->wValue = cpu_to_le16(0x0200);
578 control_req->wIndex = cpu_to_le16(0x0001);
579 control_req->wLength = cpu_to_le16(0x0008);
580
581 /* control pipe is endpoint 0x00 */
582 pipe = usb_sndctrlpipe(ictx->usbdev_intf0, 0);
583
584 /* build the control urb */
585 usb_fill_control_urb(ictx->tx_urb, ictx->usbdev_intf0,
586 pipe, (unsigned char *)control_req,
587 ictx->usb_tx_buf,
588 sizeof(ictx->usb_tx_buf),
589 usb_tx_callback, ictx);
590 ictx->tx_urb->actual_length = 0;
591 }
592
593 init_completion(&ictx->tx.finished);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300594 ictx->tx.busy = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300595 smp_rmb(); /* ensure later readers know we're busy */
596
597 retval = usb_submit_urb(ictx->tx_urb, GFP_KERNEL);
598 if (retval) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300599 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300600 smp_rmb(); /* ensure later readers know we're not busy */
Jarod Wilson47a09b02011-07-14 14:20:46 -0300601 pr_err_ratelimited("error submitting urb(%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300602 } else {
603 /* Wait for transmission to complete (or abort) */
604 mutex_unlock(&ictx->lock);
605 retval = wait_for_completion_interruptible(
606 &ictx->tx.finished);
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300607 if (retval) {
608 usb_kill_urb(ictx->tx_urb);
Jarod Wilson47a09b02011-07-14 14:20:46 -0300609 pr_err_ratelimited("task interrupted\n");
Kevin Baradon5f3f2542013-04-22 16:09:46 -0300610 }
Jarod Wilson21677cf2010-04-16 18:29:02 -0300611 mutex_lock(&ictx->lock);
612
613 retval = ictx->tx.status;
614 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300615 pr_err_ratelimited("packet tx failed (%d)\n", retval);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300616 }
617
618 kfree(control_req);
619
620 /*
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300621 * Induce a mandatory delay before returning, as otherwise,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300622 * send_packet can get called so rapidly as to overwhelm the device,
623 * particularly on faster systems and/or those with quirky usb.
624 */
Kevin Baradon26ccbec2013-02-18 14:42:47 -0300625 timeout = msecs_to_jiffies(ictx->send_packet_delay);
626 set_current_state(TASK_INTERRUPTIBLE);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300627 schedule_timeout(timeout);
628
629 return retval;
630}
631
632/**
633 * Sends an associate packet to the iMON 2.4G.
634 *
635 * This might not be such a good idea, since it has an id collision with
636 * some versions of the "IR & VFD" combo. The only way to determine if it
637 * is an RF version is to look at the product description string. (Which
638 * we currently do not fetch).
639 */
640static int send_associate_24g(struct imon_context *ictx)
641{
642 int retval;
643 const unsigned char packet[8] = { 0x01, 0x00, 0x00, 0x00,
644 0x00, 0x00, 0x00, 0x20 };
645
646 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300647 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300648 return -ENODEV;
649 }
650
651 if (!ictx->dev_present_intf0) {
Joe Perchese2302502010-06-20 04:20:46 -0300652 pr_err("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300653 return -ENODEV;
654 }
655
656 memcpy(ictx->usb_tx_buf, packet, sizeof(packet));
657 retval = send_packet(ictx);
658
659 return retval;
660}
661
662/**
663 * Sends packets to setup and show clock on iMON display
664 *
665 * Arguments: year - last 2 digits of year, month - 1..12,
666 * day - 1..31, dow - day of the week (0-Sun...6-Sat),
667 * hour - 0..23, minute - 0..59, second - 0..59
668 */
669static int send_set_imon_clock(struct imon_context *ictx,
670 unsigned int year, unsigned int month,
671 unsigned int day, unsigned int dow,
672 unsigned int hour, unsigned int minute,
673 unsigned int second)
674{
675 unsigned char clock_enable_pkt[IMON_CLOCK_ENABLE_PACKETS][8];
676 int retval = 0;
677 int i;
678
679 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -0300680 pr_err("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300681 return -ENODEV;
682 }
683
684 switch (ictx->display_type) {
685 case IMON_DISPLAY_TYPE_LCD:
686 clock_enable_pkt[0][0] = 0x80;
687 clock_enable_pkt[0][1] = year;
688 clock_enable_pkt[0][2] = month-1;
689 clock_enable_pkt[0][3] = day;
690 clock_enable_pkt[0][4] = hour;
691 clock_enable_pkt[0][5] = minute;
692 clock_enable_pkt[0][6] = second;
693
694 clock_enable_pkt[1][0] = 0x80;
695 clock_enable_pkt[1][1] = 0;
696 clock_enable_pkt[1][2] = 0;
697 clock_enable_pkt[1][3] = 0;
698 clock_enable_pkt[1][4] = 0;
699 clock_enable_pkt[1][5] = 0;
700 clock_enable_pkt[1][6] = 0;
701
702 if (ictx->product == 0xffdc) {
703 clock_enable_pkt[0][7] = 0x50;
704 clock_enable_pkt[1][7] = 0x51;
705 } else {
706 clock_enable_pkt[0][7] = 0x88;
707 clock_enable_pkt[1][7] = 0x8a;
708 }
709
710 break;
711
712 case IMON_DISPLAY_TYPE_VFD:
713 clock_enable_pkt[0][0] = year;
714 clock_enable_pkt[0][1] = month-1;
715 clock_enable_pkt[0][2] = day;
716 clock_enable_pkt[0][3] = dow;
717 clock_enable_pkt[0][4] = hour;
718 clock_enable_pkt[0][5] = minute;
719 clock_enable_pkt[0][6] = second;
720 clock_enable_pkt[0][7] = 0x40;
721
722 clock_enable_pkt[1][0] = 0;
723 clock_enable_pkt[1][1] = 0;
724 clock_enable_pkt[1][2] = 1;
725 clock_enable_pkt[1][3] = 0;
726 clock_enable_pkt[1][4] = 0;
727 clock_enable_pkt[1][5] = 0;
728 clock_enable_pkt[1][6] = 0;
729 clock_enable_pkt[1][7] = 0x42;
730
731 break;
732
733 default:
734 return -ENODEV;
735 }
736
737 for (i = 0; i < IMON_CLOCK_ENABLE_PACKETS; i++) {
738 memcpy(ictx->usb_tx_buf, clock_enable_pkt[i], 8);
739 retval = send_packet(ictx);
740 if (retval) {
Joe Perchese2302502010-06-20 04:20:46 -0300741 pr_err("send_packet failed for packet %d\n", i);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300742 break;
743 }
744 }
745
746 return retval;
747}
748
749/**
750 * These are the sysfs functions to handle the association on the iMON 2.4G LT.
751 */
752static ssize_t show_associate_remote(struct device *d,
753 struct device_attribute *attr,
754 char *buf)
755{
756 struct imon_context *ictx = dev_get_drvdata(d);
757
758 if (!ictx)
759 return -ENODEV;
760
761 mutex_lock(&ictx->lock);
762 if (ictx->rf_isassociating)
763 strcpy(buf, "associating\n");
764 else
765 strcpy(buf, "closed\n");
766
767 dev_info(d, "Visit http://www.lirc.org/html/imon-24g.html for "
768 "instructions on how to associate your iMON 2.4G DT/LT "
769 "remote\n");
770 mutex_unlock(&ictx->lock);
771 return strlen(buf);
772}
773
774static ssize_t store_associate_remote(struct device *d,
775 struct device_attribute *attr,
776 const char *buf, size_t count)
777{
778 struct imon_context *ictx;
779
780 ictx = dev_get_drvdata(d);
781
782 if (!ictx)
783 return -ENODEV;
784
785 mutex_lock(&ictx->lock);
Jarod Wilsonf789bf42010-05-24 12:00:31 -0300786 ictx->rf_isassociating = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300787 send_associate_24g(ictx);
788 mutex_unlock(&ictx->lock);
789
790 return count;
791}
792
793/**
794 * sysfs functions to control internal imon clock
795 */
796static ssize_t show_imon_clock(struct device *d,
797 struct device_attribute *attr, char *buf)
798{
799 struct imon_context *ictx = dev_get_drvdata(d);
800 size_t len;
801
802 if (!ictx)
803 return -ENODEV;
804
805 mutex_lock(&ictx->lock);
806
807 if (!ictx->display_supported) {
808 len = snprintf(buf, PAGE_SIZE, "Not supported.");
809 } else {
810 len = snprintf(buf, PAGE_SIZE,
811 "To set the clock on your iMON display:\n"
812 "# date \"+%%y %%m %%d %%w %%H %%M %%S\" > imon_clock\n"
813 "%s", ictx->display_isopen ?
814 "\nNOTE: imon device must be closed\n" : "");
815 }
816
817 mutex_unlock(&ictx->lock);
818
819 return len;
820}
821
822static ssize_t store_imon_clock(struct device *d,
823 struct device_attribute *attr,
824 const char *buf, size_t count)
825{
826 struct imon_context *ictx = dev_get_drvdata(d);
827 ssize_t retval;
828 unsigned int year, month, day, dow, hour, minute, second;
829
830 if (!ictx)
831 return -ENODEV;
832
833 mutex_lock(&ictx->lock);
834
835 if (!ictx->display_supported) {
836 retval = -ENODEV;
837 goto exit;
838 } else if (ictx->display_isopen) {
839 retval = -EBUSY;
840 goto exit;
841 }
842
843 if (sscanf(buf, "%u %u %u %u %u %u %u", &year, &month, &day, &dow,
844 &hour, &minute, &second) != 7) {
845 retval = -EINVAL;
846 goto exit;
847 }
848
849 if ((month < 1 || month > 12) ||
850 (day < 1 || day > 31) || (dow > 6) ||
851 (hour > 23) || (minute > 59) || (second > 59)) {
852 retval = -EINVAL;
853 goto exit;
854 }
855
856 retval = send_set_imon_clock(ictx, year, month, day, dow,
857 hour, minute, second);
858 if (retval)
859 goto exit;
860
861 retval = count;
862exit:
863 mutex_unlock(&ictx->lock);
864
865 return retval;
866}
867
868
869static DEVICE_ATTR(imon_clock, S_IWUSR | S_IRUGO, show_imon_clock,
870 store_imon_clock);
871
872static DEVICE_ATTR(associate_remote, S_IWUSR | S_IRUGO, show_associate_remote,
873 store_associate_remote);
874
875static struct attribute *imon_display_sysfs_entries[] = {
876 &dev_attr_imon_clock.attr,
877 NULL
878};
879
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300880static struct attribute_group imon_display_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300881 .attrs = imon_display_sysfs_entries
882};
883
884static struct attribute *imon_rf_sysfs_entries[] = {
885 &dev_attr_associate_remote.attr,
886 NULL
887};
888
Jarod Wilson6aa209e2010-10-23 16:42:51 -0300889static struct attribute_group imon_rf_attr_group = {
Jarod Wilson21677cf2010-04-16 18:29:02 -0300890 .attrs = imon_rf_sysfs_entries
891};
892
893/**
894 * Writes data to the VFD. The iMON VFD is 2x16 characters
895 * and requires data in 5 consecutive USB interrupt packets,
896 * each packet but the last carrying 7 bytes.
897 *
898 * I don't know if the VFD board supports features such as
899 * scrolling, clearing rows, blanking, etc. so at
900 * the caller must provide a full screen of data. If fewer
901 * than 32 bytes are provided spaces will be appended to
902 * generate a full screen.
903 */
David Härdemand6740d82014-04-03 20:34:53 -0300904static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300905 size_t n_bytes, loff_t *pos)
906{
907 int i;
908 int offset;
909 int seq;
910 int retval = 0;
911 struct imon_context *ictx;
912 const unsigned char vfd_packet6[] = {
913 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
914
Joe Perchesabf84382010-07-12 17:50:03 -0300915 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300916 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300917 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300918 return -ENODEV;
919 }
920
921 mutex_lock(&ictx->lock);
922
923 if (!ictx->dev_present_intf0) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300924 pr_err_ratelimited("no iMON device present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300925 retval = -ENODEV;
926 goto exit;
927 }
928
929 if (n_bytes <= 0 || n_bytes > 32) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300930 pr_err_ratelimited("invalid payload size\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -0300931 retval = -EINVAL;
932 goto exit;
933 }
934
935 if (copy_from_user(ictx->tx.data_buf, buf, n_bytes)) {
936 retval = -EFAULT;
937 goto exit;
938 }
939
940 /* Pad with spaces */
941 for (i = n_bytes; i < 32; ++i)
942 ictx->tx.data_buf[i] = ' ';
943
944 for (i = 32; i < 35; ++i)
945 ictx->tx.data_buf[i] = 0xFF;
946
947 offset = 0;
948 seq = 0;
949
950 do {
951 memcpy(ictx->usb_tx_buf, ictx->tx.data_buf + offset, 7);
952 ictx->usb_tx_buf[7] = (unsigned char) seq;
953
954 retval = send_packet(ictx);
955 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300956 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300957 goto exit;
958 } else {
959 seq += 2;
960 offset += 7;
961 }
962
963 } while (offset < 35);
964
965 /* Send packet #6 */
966 memcpy(ictx->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
967 ictx->usb_tx_buf[7] = (unsigned char) seq;
968 retval = send_packet(ictx);
969 if (retval)
Jarod Wilson47a09b02011-07-14 14:20:46 -0300970 pr_err_ratelimited("send packet #%d failed\n", seq / 2);
Jarod Wilson21677cf2010-04-16 18:29:02 -0300971
972exit:
973 mutex_unlock(&ictx->lock);
974
975 return (!retval) ? n_bytes : retval;
976}
977
978/**
979 * Writes data to the LCD. The iMON OEM LCD screen expects 8-byte
980 * packets. We accept data as 16 hexadecimal digits, followed by a
981 * newline (to make it easy to drive the device from a command-line
982 * -- even though the actual binary data is a bit complicated).
983 *
984 * The device itself is not a "traditional" text-mode display. It's
985 * actually a 16x96 pixel bitmap display. That means if you want to
986 * display text, you've got to have your own "font" and translate the
987 * text into bitmaps for display. This is really flexible (you can
988 * display whatever diacritics you need, and so on), but it's also
989 * a lot more complicated than most LCDs...
990 */
David Härdemand6740d82014-04-03 20:34:53 -0300991static ssize_t lcd_write(struct file *file, const char __user *buf,
Jarod Wilson21677cf2010-04-16 18:29:02 -0300992 size_t n_bytes, loff_t *pos)
993{
994 int retval = 0;
995 struct imon_context *ictx;
996
Joe Perchesabf84382010-07-12 17:50:03 -0300997 ictx = file->private_data;
Jarod Wilson21677cf2010-04-16 18:29:02 -0300998 if (!ictx) {
Jarod Wilson47a09b02011-07-14 14:20:46 -0300999 pr_err_ratelimited("no context for device\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001000 return -ENODEV;
1001 }
1002
1003 mutex_lock(&ictx->lock);
1004
1005 if (!ictx->display_supported) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001006 pr_err_ratelimited("no iMON display present\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001007 retval = -ENODEV;
1008 goto exit;
1009 }
1010
1011 if (n_bytes != 8) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001012 pr_err_ratelimited("invalid payload size: %d (expected 8)\n",
1013 (int)n_bytes);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001014 retval = -EINVAL;
1015 goto exit;
1016 }
1017
1018 if (copy_from_user(ictx->usb_tx_buf, buf, 8)) {
1019 retval = -EFAULT;
1020 goto exit;
1021 }
1022
1023 retval = send_packet(ictx);
1024 if (retval) {
Jarod Wilson47a09b02011-07-14 14:20:46 -03001025 pr_err_ratelimited("send packet failed!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001026 goto exit;
1027 } else {
1028 dev_dbg(ictx->dev, "%s: write %d bytes to LCD\n",
1029 __func__, (int) n_bytes);
1030 }
1031exit:
1032 mutex_unlock(&ictx->lock);
1033 return (!retval) ? n_bytes : retval;
1034}
1035
1036/**
1037 * Callback function for USB core API: transmit data
1038 */
1039static void usb_tx_callback(struct urb *urb)
1040{
1041 struct imon_context *ictx;
1042
1043 if (!urb)
1044 return;
1045 ictx = (struct imon_context *)urb->context;
1046 if (!ictx)
1047 return;
1048
1049 ictx->tx.status = urb->status;
1050
1051 /* notify waiters that write has finished */
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001052 ictx->tx.busy = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001053 smp_rmb(); /* ensure later readers know we're not busy */
1054 complete(&ictx->tx.finished);
1055}
1056
1057/**
Jarod Wilson21677cf2010-04-16 18:29:02 -03001058 * report touchscreen input
1059 */
1060static void imon_touch_display_timeout(unsigned long data)
1061{
1062 struct imon_context *ictx = (struct imon_context *)data;
1063
Dan Carpenterf03900d2010-05-04 08:37:33 -03001064 if (ictx->display_type != IMON_DISPLAY_TYPE_VGA)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001065 return;
1066
1067 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1068 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1069 input_report_key(ictx->touch, BTN_TOUCH, 0x00);
1070 input_sync(ictx->touch);
1071}
1072
1073/**
1074 * iMON IR receivers support two different signal sets -- those used by
1075 * the iMON remotes, and those used by the Windows MCE remotes (which is
1076 * really just RC-6), but only one or the other at a time, as the signals
1077 * are decoded onboard the receiver.
Jarod Wilson23ef7102011-04-27 19:01:44 -03001078 *
1079 * This function gets called two different ways, one way is from
1080 * rc_register_device, for initial protocol selection/setup, and the other is
1081 * via a userspace-initiated protocol change request, either by direct sysfs
1082 * prodding or by something like ir-keytable. In the rc_register_device case,
1083 * the imon context lock is already held, but when initiated from userspace,
1084 * it is not, so we must acquire it prior to calling send_packet, which
1085 * requires that the lock is held.
Jarod Wilson21677cf2010-04-16 18:29:02 -03001086 */
David Härdemanc003ab12012-10-11 19:11:54 -03001087static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001088{
1089 int retval;
David Härdemand8b4b582010-10-29 16:08:23 -03001090 struct imon_context *ictx = rc->priv;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001091 struct device *dev = ictx->dev;
Jarod Wilson23ef7102011-04-27 19:01:44 -03001092 bool unlock = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001093 unsigned char ir_proto_packet[] = {
1094 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
1095
David Härdemanc5540fb2014-04-03 20:32:21 -03001096 if (*rc_type && !(*rc_type & rc->allowed_protocols))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001097 dev_warn(dev, "Looks like you're trying to use an IR protocol "
1098 "this device does not support\n");
1099
David Härdemanc003ab12012-10-11 19:11:54 -03001100 if (*rc_type & RC_BIT_RC6_MCE) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001101 dev_dbg(dev, "Configuring IR receiver for MCE protocol\n");
1102 ir_proto_packet[0] = 0x01;
David Härdemanc003ab12012-10-11 19:11:54 -03001103 *rc_type = RC_BIT_RC6_MCE;
1104 } else if (*rc_type & RC_BIT_OTHER) {
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001105 dev_dbg(dev, "Configuring IR receiver for iMON protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001106 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001107 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03001108 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001109 *rc_type = RC_BIT_OTHER;
1110 } else {
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001111 dev_warn(dev, "Unsupported IR protocol specified, overriding "
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001112 "to iMON IR protocol\n");
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001113 if (!pad_stabilize)
Jarod Wilson666a9ed2010-04-28 14:37:29 -03001114 dev_dbg(dev, "PAD stabilize functionality disabled\n");
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001115 /* ir_proto_packet[0] = 0x00; // already the default */
David Härdemanc003ab12012-10-11 19:11:54 -03001116 *rc_type = RC_BIT_OTHER;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001117 }
1118
1119 memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet));
1120
Jarod Wilson23ef7102011-04-27 19:01:44 -03001121 if (!mutex_is_locked(&ictx->lock)) {
1122 unlock = true;
1123 mutex_lock(&ictx->lock);
1124 }
1125
Jarod Wilson21677cf2010-04-16 18:29:02 -03001126 retval = send_packet(ictx);
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001127 if (retval)
1128 goto out;
1129
David Härdemanc003ab12012-10-11 19:11:54 -03001130 ictx->rc_type = *rc_type;
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001131 ictx->pad_mouse = false;
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001132
1133out:
Jarod Wilson23ef7102011-04-27 19:01:44 -03001134 if (unlock)
1135 mutex_unlock(&ictx->lock);
1136
Jarod Wilson6718e8a2010-04-23 02:27:11 -03001137 return retval;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001138}
1139
1140static inline int tv2int(const struct timeval *a, const struct timeval *b)
1141{
1142 int usecs = 0;
1143 int sec = 0;
1144
1145 if (b->tv_usec > a->tv_usec) {
1146 usecs = 1000000;
1147 sec--;
1148 }
1149
1150 usecs += a->tv_usec - b->tv_usec;
1151
1152 sec += a->tv_sec - b->tv_sec;
1153 sec *= 1000;
1154 usecs /= 1000;
1155 sec += usecs;
1156
1157 if (sec < 0)
1158 sec = 1000;
1159
1160 return sec;
1161}
1162
1163/**
1164 * The directional pad behaves a bit differently, depending on whether this is
1165 * one of the older ffdc devices or a newer device. Newer devices appear to
1166 * have a higher resolution matrix for more precise mouse movement, but it
1167 * makes things overly sensitive in keyboard mode, so we do some interesting
1168 * contortions to make it less touchy. Older devices run through the same
1169 * routine with shorter timeout and a smaller threshold.
1170 */
1171static int stabilize(int a, int b, u16 timeout, u16 threshold)
1172{
1173 struct timeval ct;
1174 static struct timeval prev_time = {0, 0};
1175 static struct timeval hit_time = {0, 0};
1176 static int x, y, prev_result, hits;
1177 int result = 0;
1178 int msec, msec_hit;
1179
1180 do_gettimeofday(&ct);
1181 msec = tv2int(&ct, &prev_time);
1182 msec_hit = tv2int(&ct, &hit_time);
1183
1184 if (msec > 100) {
1185 x = 0;
1186 y = 0;
1187 hits = 0;
1188 }
1189
1190 x += a;
1191 y += b;
1192
1193 prev_time = ct;
1194
1195 if (abs(x) > threshold || abs(y) > threshold) {
1196 if (abs(y) > abs(x))
1197 result = (y > 0) ? 0x7F : 0x80;
1198 else
1199 result = (x > 0) ? 0x7F00 : 0x8000;
1200
1201 x = 0;
1202 y = 0;
1203
1204 if (result == prev_result) {
1205 hits++;
1206
1207 if (hits > 3) {
1208 switch (result) {
1209 case 0x7F:
1210 y = 17 * threshold / 30;
1211 break;
1212 case 0x80:
1213 y -= 17 * threshold / 30;
1214 break;
1215 case 0x7F00:
1216 x = 17 * threshold / 30;
1217 break;
1218 case 0x8000:
1219 x -= 17 * threshold / 30;
1220 break;
1221 }
1222 }
1223
1224 if (hits == 2 && msec_hit < timeout) {
1225 result = 0;
1226 hits = 1;
1227 }
1228 } else {
1229 prev_result = result;
1230 hits = 1;
1231 hit_time = ct;
1232 }
1233 }
1234
1235 return result;
1236}
1237
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001238static u32 imon_remote_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001239{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001240 u32 keycode;
1241 u32 release;
1242 bool is_release_code = false;
1243
1244 /* Look for the initial press of a button */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001245 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001246 ictx->rc_toggle = 0x0;
1247 ictx->rc_scancode = scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001248
1249 /* Look for the release of a button */
1250 if (keycode == KEY_RESERVED) {
1251 release = scancode & ~0x4000;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001252 keycode = rc_g_keycode_from_table(ictx->rdev, release);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001253 if (keycode != KEY_RESERVED)
1254 is_release_code = true;
1255 }
1256
1257 ictx->release_code = is_release_code;
1258
1259 return keycode;
1260}
1261
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001262static u32 imon_mce_key_lookup(struct imon_context *ictx, u32 scancode)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001263{
Jarod Wilson21677cf2010-04-16 18:29:02 -03001264 u32 keycode;
1265
1266#define MCE_KEY_MASK 0x7000
1267#define MCE_TOGGLE_BIT 0x8000
1268
1269 /*
1270 * On some receivers, mce keys decode to 0x8000f04xx and 0x8000f84xx
1271 * (the toggle bit flipping between alternating key presses), while
1272 * on other receivers, we see 0x8000f74xx and 0x8000ff4xx. To keep
1273 * the table trim, we always or in the bits to look up 0x8000ff4xx,
1274 * but we can't or them into all codes, as some keys are decoded in
1275 * a different way w/o the same use of the toggle bit...
1276 */
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001277 if (scancode & 0x80000000)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001278 scancode = scancode | MCE_KEY_MASK | MCE_TOGGLE_BIT;
1279
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001280 ictx->rc_scancode = scancode;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001281 keycode = rc_g_keycode_from_table(ictx->rdev, scancode);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001282
1283 /* not used in mce mode, but make sure we know its false */
1284 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001285
1286 return keycode;
1287}
1288
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001289static u32 imon_panel_key_lookup(struct imon_context *ictx, u64 code)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001290{
1291 int i;
Jarod Wilson083e4722010-05-04 16:17:05 -03001292 u32 keycode = KEY_RESERVED;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001293 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001294
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001295 for (i = 0; key_table[i].hw_code != 0; i++) {
1296 if (key_table[i].hw_code == (code | 0xffee)) {
1297 keycode = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001298 break;
Jarod Wilson083e4722010-05-04 16:17:05 -03001299 }
1300 }
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001301 ictx->release_code = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001302 return keycode;
1303}
1304
1305static bool imon_mouse_event(struct imon_context *ictx,
1306 unsigned char *buf, int len)
1307{
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001308 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001309 u8 right_shift = 1;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001310 bool mouse_input = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001311 int dir = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001312 unsigned long flags;
1313
1314 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001315
1316 /* newer iMON device PAD or mouse button */
1317 if (ictx->product != 0xffdc && (buf[0] & 0x01) && len == 5) {
1318 rel_x = buf[2];
1319 rel_y = buf[3];
1320 right_shift = 1;
1321 /* 0xffdc iMON PAD or mouse button input */
1322 } else if (ictx->product == 0xffdc && (buf[0] & 0x40) &&
1323 !((buf[1] & 0x01) || ((buf[1] >> 2) & 0x01))) {
1324 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1325 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1326 if (buf[0] & 0x02)
1327 rel_x |= ~0x0f;
1328 rel_x = rel_x + rel_x / 2;
1329 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1330 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1331 if (buf[0] & 0x01)
1332 rel_y |= ~0x0f;
1333 rel_y = rel_y + rel_y / 2;
1334 right_shift = 2;
1335 /* some ffdc devices decode mouse buttons differently... */
1336 } else if (ictx->product == 0xffdc && (buf[0] == 0x68)) {
1337 right_shift = 2;
1338 /* ch+/- buttons, which we use for an emulated scroll wheel */
1339 } else if (ictx->kc == KEY_CHANNELUP && (buf[2] & 0x40) != 0x40) {
1340 dir = 1;
1341 } else if (ictx->kc == KEY_CHANNELDOWN && (buf[2] & 0x40) != 0x40) {
1342 dir = -1;
1343 } else
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001344 mouse_input = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001345
Jarod Wilson693508d2010-09-15 15:56:03 -03001346 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1347
Jarod Wilson21677cf2010-04-16 18:29:02 -03001348 if (mouse_input) {
1349 dev_dbg(ictx->dev, "sending mouse data via input subsystem\n");
1350
1351 if (dir) {
1352 input_report_rel(ictx->idev, REL_WHEEL, dir);
1353 } else if (rel_x || rel_y) {
1354 input_report_rel(ictx->idev, REL_X, rel_x);
1355 input_report_rel(ictx->idev, REL_Y, rel_y);
1356 } else {
1357 input_report_key(ictx->idev, BTN_LEFT, buf[1] & 0x1);
1358 input_report_key(ictx->idev, BTN_RIGHT,
1359 buf[1] >> right_shift & 0x1);
1360 }
1361 input_sync(ictx->idev);
Jarod Wilson693508d2010-09-15 15:56:03 -03001362 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001363 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001364 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001365 }
1366
1367 return mouse_input;
1368}
1369
1370static void imon_touch_event(struct imon_context *ictx, unsigned char *buf)
1371{
1372 mod_timer(&ictx->ttimer, jiffies + TOUCH_TIMEOUT);
1373 ictx->touch_x = (buf[0] << 4) | (buf[1] >> 4);
1374 ictx->touch_y = 0xfff - ((buf[2] << 4) | (buf[1] & 0xf));
1375 input_report_abs(ictx->touch, ABS_X, ictx->touch_x);
1376 input_report_abs(ictx->touch, ABS_Y, ictx->touch_y);
1377 input_report_key(ictx->touch, BTN_TOUCH, 0x01);
1378 input_sync(ictx->touch);
1379}
1380
1381static void imon_pad_to_keys(struct imon_context *ictx, unsigned char *buf)
1382{
1383 int dir = 0;
Alexandre Lissy24dec5d2012-09-02 15:35:20 -03001384 signed char rel_x = 0x00, rel_y = 0x00;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001385 u16 timeout, threshold;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001386 u32 scancode = KEY_RESERVED;
Jarod Wilson693508d2010-09-15 15:56:03 -03001387 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001388
1389 /*
1390 * The imon directional pad functions more like a touchpad. Bytes 3 & 4
1391 * contain a position coordinate (x,y), with each component ranging
1392 * from -14 to 14. We want to down-sample this to only 4 discrete values
1393 * for up/down/left/right arrow keys. Also, when you get too close to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001394 * diagonals, it has a tendency to jump back and forth, so lets try to
Jarod Wilson21677cf2010-04-16 18:29:02 -03001395 * ignore when they get too close.
1396 */
1397 if (ictx->product != 0xffdc) {
1398 /* first, pad to 8 bytes so it conforms with everything else */
1399 buf[5] = buf[6] = buf[7] = 0;
1400 timeout = 500; /* in msecs */
1401 /* (2*threshold) x (2*threshold) square */
1402 threshold = pad_thresh ? pad_thresh : 28;
1403 rel_x = buf[2];
1404 rel_y = buf[3];
1405
David Härdemanc003ab12012-10-11 19:11:54 -03001406 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001407 if ((buf[1] == 0) && ((rel_x != 0) || (rel_y != 0))) {
1408 dir = stabilize((int)rel_x, (int)rel_y,
1409 timeout, threshold);
1410 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001411 spin_lock_irqsave(&ictx->kc_lock,
1412 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001413 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001414 spin_unlock_irqrestore(&ictx->kc_lock,
1415 flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001416 return;
1417 }
1418 buf[2] = dir & 0xFF;
1419 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001420 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001421 }
1422 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001423 /*
1424 * Hack alert: instead of using keycodes, we have
1425 * to use hard-coded scancodes here...
1426 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001427 if (abs(rel_y) > abs(rel_x)) {
1428 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1429 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001430 if (rel_y > 0)
1431 scancode = 0x01007f00; /* KEY_DOWN */
1432 else
1433 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001434 } else {
1435 buf[2] = 0;
1436 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001437 if (rel_x > 0)
1438 scancode = 0x0100007f; /* KEY_RIGHT */
1439 else
1440 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001441 }
1442 }
1443
1444 /*
1445 * Handle on-board decoded pad events for e.g. older VFD/iMON-Pad
1446 * device (15c2:ffdc). The remote generates various codes from
1447 * 0x68nnnnB7 to 0x6AnnnnB7, the left mouse button generates
1448 * 0x688301b7 and the right one 0x688481b7. All other keys generate
1449 * 0x2nnnnnnn. Position coordinate is encoded in buf[1] and buf[2] with
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001450 * reversed endianness. Extract direction from buffer, rotate endianness,
Jarod Wilson21677cf2010-04-16 18:29:02 -03001451 * adjust sign and feed the values into stabilize(). The resulting codes
1452 * will be 0x01008000, 0x01007F00, which match the newer devices.
1453 */
1454 } else {
1455 timeout = 10; /* in msecs */
1456 /* (2*threshold) x (2*threshold) square */
1457 threshold = pad_thresh ? pad_thresh : 15;
1458
1459 /* buf[1] is x */
1460 rel_x = (buf[1] & 0x08) | (buf[1] & 0x10) >> 2 |
1461 (buf[1] & 0x20) >> 4 | (buf[1] & 0x40) >> 6;
1462 if (buf[0] & 0x02)
1463 rel_x |= ~0x10+1;
1464 /* buf[2] is y */
1465 rel_y = (buf[2] & 0x08) | (buf[2] & 0x10) >> 2 |
1466 (buf[2] & 0x20) >> 4 | (buf[2] & 0x40) >> 6;
1467 if (buf[0] & 0x01)
1468 rel_y |= ~0x10+1;
1469
1470 buf[0] = 0x01;
1471 buf[1] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
1472
David Härdemanc003ab12012-10-11 19:11:54 -03001473 if (ictx->rc_type == RC_BIT_OTHER && pad_stabilize) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001474 dir = stabilize((int)rel_x, (int)rel_y,
1475 timeout, threshold);
1476 if (!dir) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001477 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001478 ictx->kc = KEY_UNKNOWN;
Jarod Wilson693508d2010-09-15 15:56:03 -03001479 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001480 return;
1481 }
1482 buf[2] = dir & 0xFF;
1483 buf[3] = (dir >> 8) & 0xFF;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001484 scancode = be32_to_cpu(*((u32 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001485 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001486 /*
1487 * Hack alert: instead of using keycodes, we have
1488 * to use hard-coded scancodes here...
1489 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001490 if (abs(rel_y) > abs(rel_x)) {
1491 buf[2] = (rel_y > 0) ? 0x7F : 0x80;
1492 buf[3] = 0;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001493 if (rel_y > 0)
1494 scancode = 0x01007f00; /* KEY_DOWN */
1495 else
1496 scancode = 0x01008000; /* KEY_UP */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001497 } else {
1498 buf[2] = 0;
1499 buf[3] = (rel_x > 0) ? 0x7F : 0x80;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001500 if (rel_x > 0)
1501 scancode = 0x0100007f; /* KEY_RIGHT */
1502 else
1503 scancode = 0x01000080; /* KEY_LEFT */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001504 }
1505 }
1506 }
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001507
Jarod Wilson693508d2010-09-15 15:56:03 -03001508 if (scancode) {
1509 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001510 ictx->kc = imon_remote_key_lookup(ictx, scancode);
Jarod Wilson693508d2010-09-15 15:56:03 -03001511 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1512 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001513}
1514
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001515/**
1516 * figure out if these is a press or a release. We don't actually
1517 * care about repeats, as those will be auto-generated within the IR
1518 * subsystem for repeating scancodes.
1519 */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001520static int imon_parse_press_type(struct imon_context *ictx,
1521 unsigned char *buf, u8 ktype)
1522{
1523 int press_type = 0;
Jarod Wilson693508d2010-09-15 15:56:03 -03001524 unsigned long flags;
1525
1526 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001527
1528 /* key release of 0x02XXXXXX key */
1529 if (ictx->kc == KEY_RESERVED && buf[0] == 0x02 && buf[3] == 0x00)
1530 ictx->kc = ictx->last_keycode;
1531
1532 /* mouse button release on (some) 0xffdc devices */
1533 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x68 && buf[1] == 0x82 &&
1534 buf[2] == 0x81 && buf[3] == 0xb7)
1535 ictx->kc = ictx->last_keycode;
1536
1537 /* mouse button release on (some other) 0xffdc devices */
1538 else if (ictx->kc == KEY_RESERVED && buf[0] == 0x01 && buf[1] == 0x00 &&
1539 buf[2] == 0x81 && buf[3] == 0xb7)
1540 ictx->kc = ictx->last_keycode;
1541
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001542 /* mce-specific button handling, no keyup events */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001543 else if (ktype == IMON_KEY_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001544 ictx->rc_toggle = buf[2];
1545 press_type = 1;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001546
1547 /* incoherent or irrelevant data */
1548 } else if (ictx->kc == KEY_RESERVED)
1549 press_type = -EINVAL;
1550
1551 /* key release of 0xXXXXXXb7 key */
1552 else if (ictx->release_code)
1553 press_type = 0;
1554
1555 /* this is a button press */
1556 else
1557 press_type = 1;
1558
Jarod Wilson693508d2010-09-15 15:56:03 -03001559 spin_unlock_irqrestore(&ictx->kc_lock, flags);
1560
Jarod Wilson21677cf2010-04-16 18:29:02 -03001561 return press_type;
1562}
1563
1564/**
1565 * Process the incoming packet
1566 */
1567static void imon_incoming_packet(struct imon_context *ictx,
1568 struct urb *urb, int intf)
1569{
1570 int len = urb->actual_length;
1571 unsigned char *buf = urb->transfer_buffer;
1572 struct device *dev = ictx->dev;
Jarod Wilson693508d2010-09-15 15:56:03 -03001573 unsigned long flags;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001574 u32 kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001575 int i;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001576 u64 scancode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001577 int press_type = 0;
1578 int msec;
1579 struct timeval t;
1580 static struct timeval prev_time = { 0, 0 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001581 u8 ktype;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001582
Jarod Wilson21677cf2010-04-16 18:29:02 -03001583 /* filter out junk data on the older 0xffdc imon devices */
Jarod Wilsonbbe46902010-05-24 12:02:05 -03001584 if ((buf[0] == 0xff) && (buf[1] == 0xff) && (buf[2] == 0xff))
Jarod Wilson21677cf2010-04-16 18:29:02 -03001585 return;
1586
1587 /* Figure out what key was pressed */
Jarod Wilson21677cf2010-04-16 18:29:02 -03001588 if (len == 8 && buf[7] == 0xee) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001589 scancode = be64_to_cpu(*((u64 *)buf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03001590 ktype = IMON_KEY_PANEL;
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001591 kc = imon_panel_key_lookup(ictx, scancode);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001592 } else {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001593 scancode = be32_to_cpu(*((u32 *)buf));
David Härdemanc003ab12012-10-11 19:11:54 -03001594 if (ictx->rc_type == RC_BIT_RC6_MCE) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001595 ktype = IMON_KEY_IMON;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001596 if (buf[0] == 0x80)
1597 ktype = IMON_KEY_MCE;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001598 kc = imon_mce_key_lookup(ictx, scancode);
1599 } else {
1600 ktype = IMON_KEY_IMON;
1601 kc = imon_remote_key_lookup(ictx, scancode);
1602 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001603 }
1604
Jarod Wilson693508d2010-09-15 15:56:03 -03001605 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001606 /* keyboard/mouse mode toggle button */
1607 if (kc == KEY_KEYBOARD && !ictx->release_code) {
1608 ictx->last_keycode = kc;
1609 if (!nomouse) {
1610 ictx->pad_mouse = ~(ictx->pad_mouse) & 0x1;
1611 dev_dbg(dev, "toggling to %s mode\n",
1612 ictx->pad_mouse ? "mouse" : "keyboard");
Jarod Wilson693508d2010-09-15 15:56:03 -03001613 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001614 return;
1615 } else {
Jarod Wilson76f1ef42011-01-06 16:59:35 -03001616 ictx->pad_mouse = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001617 dev_dbg(dev, "mouse mode disabled, passing key value\n");
1618 }
1619 }
1620
1621 ictx->kc = kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001622 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001623
1624 /* send touchscreen events through input subsystem if touchpad data */
1625 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA && len == 8 &&
1626 buf[7] == 0x86) {
1627 imon_touch_event(ictx, buf);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001628 return;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001629
1630 /* look for mouse events with pad in mouse mode */
1631 } else if (ictx->pad_mouse) {
1632 if (imon_mouse_event(ictx, buf, len))
1633 return;
1634 }
1635
1636 /* Now for some special handling to convert pad input to arrow keys */
1637 if (((len == 5) && (buf[0] == 0x01) && (buf[4] == 0x00)) ||
1638 ((len == 8) && (buf[0] & 0x40) &&
1639 !(buf[1] & 0x1 || buf[1] >> 2 & 0x1))) {
1640 len = 8;
1641 imon_pad_to_keys(ictx, buf);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001642 }
1643
1644 if (debug) {
1645 printk(KERN_INFO "intf%d decoded packet: ", intf);
1646 for (i = 0; i < len; ++i)
1647 printk("%02x ", buf[i]);
1648 printk("\n");
1649 }
1650
1651 press_type = imon_parse_press_type(ictx, buf, ktype);
1652 if (press_type < 0)
1653 goto not_input_data;
1654
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001655 if (ktype != IMON_KEY_PANEL) {
1656 if (press_type == 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001657 rc_keyup(ictx->rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001658 else {
David Härdeman120703f2014-04-03 20:31:30 -03001659 if (ictx->rc_type == RC_BIT_RC6_MCE)
1660 rc_keydown(ictx->rdev,
1661 ictx->rc_type == RC_BIT_RC6_MCE ? RC_TYPE_RC6_MCE : RC_TYPE_OTHER,
1662 ictx->rc_scancode, ictx->rc_toggle);
Jarod Wilson693508d2010-09-15 15:56:03 -03001663 spin_lock_irqsave(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001664 ictx->last_keycode = ictx->kc;
Jarod Wilson693508d2010-09-15 15:56:03 -03001665 spin_unlock_irqrestore(&ictx->kc_lock, flags);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001666 }
1667 return;
1668 }
1669
1670 /* Only panel type events left to process now */
Jarod Wilson693508d2010-09-15 15:56:03 -03001671 spin_lock_irqsave(&ictx->kc_lock, flags);
1672
Jarod Wilson94215cc2011-06-04 14:14:41 -03001673 do_gettimeofday(&t);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001674 /* KEY_MUTE repeats from knob need to be suppressed */
1675 if (ictx->kc == KEY_MUTE && ictx->kc == ictx->last_keycode) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03001676 msec = tv2int(&t, &prev_time);
Jarod Wilson428cc762010-10-23 16:42:20 -03001677 if (msec < ictx->idev->rep[REP_DELAY]) {
Jarod Wilson693508d2010-09-15 15:56:03 -03001678 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001679 return;
Jarod Wilson693508d2010-09-15 15:56:03 -03001680 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03001681 }
Jarod Wilson94215cc2011-06-04 14:14:41 -03001682 prev_time = t;
Jarod Wilson693508d2010-09-15 15:56:03 -03001683 kc = ictx->kc;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001684
Jarod Wilson693508d2010-09-15 15:56:03 -03001685 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001686
Jarod Wilson428cc762010-10-23 16:42:20 -03001687 input_report_key(ictx->idev, kc, press_type);
1688 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001689
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001690 /* panel keys don't generate a release */
Jarod Wilson428cc762010-10-23 16:42:20 -03001691 input_report_key(ictx->idev, kc, 0);
1692 input_sync(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001693
Jarod Wilson94215cc2011-06-04 14:14:41 -03001694 spin_lock_irqsave(&ictx->kc_lock, flags);
Jarod Wilson693508d2010-09-15 15:56:03 -03001695 ictx->last_keycode = kc;
Jarod Wilson94215cc2011-06-04 14:14:41 -03001696 spin_unlock_irqrestore(&ictx->kc_lock, flags);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001697
1698 return;
1699
Jarod Wilson21677cf2010-04-16 18:29:02 -03001700not_input_data:
1701 if (len != 8) {
1702 dev_warn(dev, "imon %s: invalid incoming packet "
1703 "size (len = %d, intf%d)\n", __func__, len, intf);
1704 return;
1705 }
1706
1707 /* iMON 2.4G associate frame */
1708 if (buf[0] == 0x00 &&
1709 buf[2] == 0xFF && /* REFID */
1710 buf[3] == 0xFF &&
1711 buf[4] == 0xFF &&
1712 buf[5] == 0xFF && /* iMON 2.4G */
1713 ((buf[6] == 0x4E && buf[7] == 0xDF) || /* LT */
1714 (buf[6] == 0x5E && buf[7] == 0xDF))) { /* DT */
1715 dev_warn(dev, "%s: remote associated refid=%02X\n",
1716 __func__, buf[1]);
Jarod Wilsonf789bf42010-05-24 12:00:31 -03001717 ictx->rf_isassociating = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001718 }
1719}
1720
1721/**
1722 * Callback function for USB core API: receive data
1723 */
1724static void usb_rx_callback_intf0(struct urb *urb)
1725{
1726 struct imon_context *ictx;
1727 int intfnum = 0;
1728
1729 if (!urb)
1730 return;
1731
1732 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001733 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001734 return;
1735
Jarod Wilson8791d632012-01-26 12:04:11 -03001736 /*
1737 * if we get a callback before we're done configuring the hardware, we
1738 * can't yet process the data, as there's nowhere to send it, but we
1739 * still need to submit a new rx URB to avoid wedging the hardware
1740 */
1741 if (!ictx->dev_present_intf0)
1742 goto out;
1743
Jarod Wilson21677cf2010-04-16 18:29:02 -03001744 switch (urb->status) {
1745 case -ENOENT: /* usbcore unlink successful! */
1746 return;
1747
1748 case -ESHUTDOWN: /* transport endpoint was shut down */
1749 break;
1750
1751 case 0:
1752 imon_incoming_packet(ictx, urb, intfnum);
1753 break;
1754
1755 default:
1756 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1757 __func__, urb->status);
1758 break;
1759 }
1760
Jarod Wilson8791d632012-01-26 12:04:11 -03001761out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001762 usb_submit_urb(ictx->rx_urb_intf0, GFP_ATOMIC);
1763}
1764
1765static void usb_rx_callback_intf1(struct urb *urb)
1766{
1767 struct imon_context *ictx;
1768 int intfnum = 1;
1769
1770 if (!urb)
1771 return;
1772
1773 ictx = (struct imon_context *)urb->context;
Jarod Wilson8791d632012-01-26 12:04:11 -03001774 if (!ictx)
Jarod Wilson21677cf2010-04-16 18:29:02 -03001775 return;
1776
Jarod Wilson8791d632012-01-26 12:04:11 -03001777 /*
1778 * if we get a callback before we're done configuring the hardware, we
1779 * can't yet process the data, as there's nowhere to send it, but we
1780 * still need to submit a new rx URB to avoid wedging the hardware
1781 */
1782 if (!ictx->dev_present_intf1)
1783 goto out;
1784
Jarod Wilson21677cf2010-04-16 18:29:02 -03001785 switch (urb->status) {
1786 case -ENOENT: /* usbcore unlink successful! */
1787 return;
1788
1789 case -ESHUTDOWN: /* transport endpoint was shut down */
1790 break;
1791
1792 case 0:
1793 imon_incoming_packet(ictx, urb, intfnum);
1794 break;
1795
1796 default:
1797 dev_warn(ictx->dev, "imon %s: status(%d): ignored\n",
1798 __func__, urb->status);
1799 break;
1800 }
1801
Jarod Wilson8791d632012-01-26 12:04:11 -03001802out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03001803 usb_submit_urb(ictx->rx_urb_intf1, GFP_ATOMIC);
1804}
1805
Jarod Wilson04292fc2010-09-15 00:28:41 -03001806/*
1807 * The 0x15c2:0xffdc device ID was used for umpteen different imon
1808 * devices, and all of them constantly spew interrupts, even when there
1809 * is no actual data to report. However, byte 6 of this buffer looks like
1810 * its unique across device variants, so we're trying to key off that to
1811 * figure out which display type (if any) and what IR protocol the device
1812 * actually supports. These devices have their IR protocol hard-coded into
1813 * their firmware, they can't be changed on the fly like the newer hardware.
1814 */
1815static void imon_get_ffdc_type(struct imon_context *ictx)
1816{
1817 u8 ffdc_cfg_byte = ictx->usb_rx_buf[6];
1818 u8 detected_display_type = IMON_DISPLAY_TYPE_NONE;
David Härdemanc003ab12012-10-11 19:11:54 -03001819 u64 allowed_protos = RC_BIT_OTHER;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001820
1821 switch (ffdc_cfg_byte) {
1822 /* iMON Knob, no display, iMON IR + vol knob */
1823 case 0x21:
1824 dev_info(ictx->dev, "0xffdc iMON Knob, iMON IR");
1825 ictx->display_supported = false;
1826 break;
1827 /* iMON 2.4G LT (usb stick), no display, iMON RF */
1828 case 0x4e:
1829 dev_info(ictx->dev, "0xffdc iMON 2.4G LT, iMON RF");
1830 ictx->display_supported = false;
1831 ictx->rf_device = true;
1832 break;
1833 /* iMON VFD, no IR (does have vol knob tho) */
1834 case 0x35:
1835 dev_info(ictx->dev, "0xffdc iMON VFD + knob, no IR");
1836 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1837 break;
1838 /* iMON VFD, iMON IR */
1839 case 0x24:
1840 case 0x85:
1841 dev_info(ictx->dev, "0xffdc iMON VFD, iMON IR");
1842 detected_display_type = IMON_DISPLAY_TYPE_VFD;
1843 break;
1844 /* iMON VFD, MCE IR */
Jarod Wilson443b3912011-06-04 14:00:54 -03001845 case 0x46:
Jarod Wilson842071c2011-06-20 00:04:05 -03001846 case 0x7e:
Jarod Wilson04292fc2010-09-15 00:28:41 -03001847 case 0x9e:
1848 dev_info(ictx->dev, "0xffdc iMON VFD, MCE IR");
1849 detected_display_type = IMON_DISPLAY_TYPE_VFD;
David Härdemanc003ab12012-10-11 19:11:54 -03001850 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001851 break;
1852 /* iMON LCD, MCE IR */
1853 case 0x9f:
1854 dev_info(ictx->dev, "0xffdc iMON LCD, MCE IR");
1855 detected_display_type = IMON_DISPLAY_TYPE_LCD;
David Härdemanc003ab12012-10-11 19:11:54 -03001856 allowed_protos = RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001857 break;
1858 default:
1859 dev_info(ictx->dev, "Unknown 0xffdc device, "
1860 "defaulting to VFD and iMON IR");
1861 detected_display_type = IMON_DISPLAY_TYPE_VFD;
Jarod Wilson372b4242011-06-20 00:07:13 -03001862 /* We don't know which one it is, allow user to set the
1863 * RC6 one from userspace if OTHER wasn't correct. */
David Härdemanc003ab12012-10-11 19:11:54 -03001864 allowed_protos |= RC_BIT_RC6_MCE;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001865 break;
1866 }
1867
1868 printk(KERN_CONT " (id 0x%02x)\n", ffdc_cfg_byte);
1869
1870 ictx->display_type = detected_display_type;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001871 ictx->rc_type = allowed_protos;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001872}
1873
1874static void imon_set_display_type(struct imon_context *ictx)
1875{
1876 u8 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1877
1878 /*
1879 * Try to auto-detect the type of display if the user hasn't set
1880 * it by hand via the display_type modparam. Default is VFD.
1881 */
1882
1883 if (display_type == IMON_DISPLAY_TYPE_AUTO) {
1884 switch (ictx->product) {
1885 case 0xffdc:
1886 /* set in imon_get_ffdc_type() */
1887 configured_display_type = ictx->display_type;
1888 break;
1889 case 0x0034:
1890 case 0x0035:
1891 configured_display_type = IMON_DISPLAY_TYPE_VGA;
1892 break;
1893 case 0x0038:
1894 case 0x0039:
1895 case 0x0045:
1896 configured_display_type = IMON_DISPLAY_TYPE_LCD;
1897 break;
1898 case 0x003c:
1899 case 0x0041:
1900 case 0x0042:
1901 case 0x0043:
1902 configured_display_type = IMON_DISPLAY_TYPE_NONE;
1903 ictx->display_supported = false;
1904 break;
1905 case 0x0036:
1906 case 0x0044:
1907 default:
1908 configured_display_type = IMON_DISPLAY_TYPE_VFD;
1909 break;
1910 }
1911 } else {
1912 configured_display_type = display_type;
1913 if (display_type == IMON_DISPLAY_TYPE_NONE)
1914 ictx->display_supported = false;
1915 else
1916 ictx->display_supported = true;
1917 dev_info(ictx->dev, "%s: overriding display type to %d via "
1918 "modparam\n", __func__, display_type);
1919 }
1920
1921 ictx->display_type = configured_display_type;
1922}
1923
David Härdemand8b4b582010-10-29 16:08:23 -03001924static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001925{
David Härdemand8b4b582010-10-29 16:08:23 -03001926 struct rc_dev *rdev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001927 int ret;
Jarod Wilson04292fc2010-09-15 00:28:41 -03001928 const unsigned char fp_packet[] = { 0x40, 0x00, 0x00, 0x00,
1929 0x00, 0x00, 0x00, 0x88 };
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001930
David Härdemand8b4b582010-10-29 16:08:23 -03001931 rdev = rc_allocate_device();
1932 if (!rdev) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001933 dev_err(ictx->dev, "remote control dev allocation failed\n");
1934 goto out;
1935 }
1936
1937 snprintf(ictx->name_rdev, sizeof(ictx->name_rdev),
1938 "iMON Remote (%04x:%04x)", ictx->vendor, ictx->product);
1939 usb_make_path(ictx->usbdev_intf0, ictx->phys_rdev,
1940 sizeof(ictx->phys_rdev));
1941 strlcat(ictx->phys_rdev, "/input0", sizeof(ictx->phys_rdev));
1942
David Härdemand8b4b582010-10-29 16:08:23 -03001943 rdev->input_name = ictx->name_rdev;
1944 rdev->input_phys = ictx->phys_rdev;
1945 usb_to_input_id(ictx->usbdev_intf0, &rdev->input_id);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001946 rdev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001947
David Härdemand8b4b582010-10-29 16:08:23 -03001948 rdev->priv = ictx;
1949 rdev->driver_type = RC_DRIVER_SCANCODE;
David Härdemanc5540fb2014-04-03 20:32:21 -03001950 rdev->allowed_protocols = RC_BIT_OTHER | RC_BIT_RC6_MCE; /* iMON PAD or MCE */
David Härdemand8b4b582010-10-29 16:08:23 -03001951 rdev->change_protocol = imon_ir_change_protocol;
1952 rdev->driver_name = MOD_NAME;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001953
Jarod Wilson04292fc2010-09-15 00:28:41 -03001954 /* Enable front-panel buttons and/or knobs */
1955 memcpy(ictx->usb_tx_buf, &fp_packet, sizeof(fp_packet));
1956 ret = send_packet(ictx);
1957 /* Not fatal, but warn about it */
1958 if (ret)
1959 dev_info(ictx->dev, "panel buttons/knobs setup failed\n");
1960
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001961 if (ictx->product == 0xffdc) {
Jarod Wilson04292fc2010-09-15 00:28:41 -03001962 imon_get_ffdc_type(ictx);
David Härdemanc5540fb2014-04-03 20:32:21 -03001963 rdev->allowed_protocols = ictx->rc_type;
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001964 }
Jarod Wilson04292fc2010-09-15 00:28:41 -03001965
1966 imon_set_display_type(ictx);
1967
David Härdemanc003ab12012-10-11 19:11:54 -03001968 if (ictx->rc_type == RC_BIT_RC6_MCE)
Jarod Wilson7d2edfc2011-01-06 16:57:14 -03001969 rdev->map_name = RC_MAP_IMON_MCE;
1970 else
1971 rdev->map_name = RC_MAP_IMON_PAD;
1972
David Härdemand8b4b582010-10-29 16:08:23 -03001973 ret = rc_register_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001974 if (ret < 0) {
1975 dev_err(ictx->dev, "remote input dev register failed\n");
1976 goto out;
1977 }
1978
1979 return rdev;
1980
1981out:
David Härdemand8b4b582010-10-29 16:08:23 -03001982 rc_free_device(rdev);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001983 return NULL;
1984}
1985
Jarod Wilson21677cf2010-04-16 18:29:02 -03001986static struct input_dev *imon_init_idev(struct imon_context *ictx)
1987{
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03001988 struct imon_panel_key_table *key_table = ictx->dev_descr->key_table;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001989 struct input_dev *idev;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001990 int ret, i;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001991
1992 idev = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03001993 if (!idev)
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001994 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03001995
Jarod Wilson21677cf2010-04-16 18:29:02 -03001996 snprintf(ictx->name_idev, sizeof(ictx->name_idev),
David Härdemaneaf2bcc2010-09-15 15:42:07 -03001997 "iMON Panel, Knob and Mouse(%04x:%04x)",
1998 ictx->vendor, ictx->product);
Jarod Wilson21677cf2010-04-16 18:29:02 -03001999 idev->name = ictx->name_idev;
2000
2001 usb_make_path(ictx->usbdev_intf0, ictx->phys_idev,
2002 sizeof(ictx->phys_idev));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002003 strlcat(ictx->phys_idev, "/input1", sizeof(ictx->phys_idev));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002004 idev->phys = ictx->phys_idev;
2005
Jarod Wilsondb190fc2010-04-30 16:06:12 -03002006 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002007
2008 idev->keybit[BIT_WORD(BTN_MOUSE)] =
2009 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2010 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
2011 BIT_MASK(REL_WHEEL);
2012
2013 /* panel and/or knob code support */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002014 for (i = 0; key_table[i].hw_code != 0; i++) {
2015 u32 kc = key_table[i].keycode;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002016 __set_bit(kc, idev->keybit);
2017 }
2018
Jarod Wilson21677cf2010-04-16 18:29:02 -03002019 usb_to_input_id(ictx->usbdev_intf0, &idev->id);
2020 idev->dev.parent = ictx->dev;
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002021 input_set_drvdata(idev, ictx);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002022
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002023 ret = input_register_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002024 if (ret < 0) {
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002025 dev_err(ictx->dev, "input dev register failed\n");
2026 goto out;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002027 }
2028
2029 return idev;
2030
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002031out:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002032 input_free_device(idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002033 return NULL;
2034}
2035
2036static struct input_dev *imon_init_touch(struct imon_context *ictx)
2037{
2038 struct input_dev *touch;
2039 int ret;
2040
2041 touch = input_allocate_device();
Joe Perchesda4a7332013-10-23 16:14:51 -03002042 if (!touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002043 goto touch_alloc_failed;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002044
2045 snprintf(ictx->name_touch, sizeof(ictx->name_touch),
2046 "iMON USB Touchscreen (%04x:%04x)",
2047 ictx->vendor, ictx->product);
2048 touch->name = ictx->name_touch;
2049
2050 usb_make_path(ictx->usbdev_intf1, ictx->phys_touch,
2051 sizeof(ictx->phys_touch));
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002052 strlcat(ictx->phys_touch, "/input2", sizeof(ictx->phys_touch));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002053 touch->phys = ictx->phys_touch;
2054
2055 touch->evbit[0] =
2056 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2057 touch->keybit[BIT_WORD(BTN_TOUCH)] =
2058 BIT_MASK(BTN_TOUCH);
2059 input_set_abs_params(touch, ABS_X,
2060 0x00, 0xfff, 0, 0);
2061 input_set_abs_params(touch, ABS_Y,
2062 0x00, 0xfff, 0, 0);
2063
2064 input_set_drvdata(touch, ictx);
2065
2066 usb_to_input_id(ictx->usbdev_intf1, &touch->id);
2067 touch->dev.parent = ictx->dev;
2068 ret = input_register_device(touch);
2069 if (ret < 0) {
2070 dev_info(ictx->dev, "touchscreen input dev register failed\n");
2071 goto touch_register_failed;
2072 }
2073
2074 return touch;
2075
2076touch_register_failed:
Julia Lawallaeb35eb2011-05-20 15:31:59 -03002077 input_free_device(touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002078
2079touch_alloc_failed:
2080 return NULL;
2081}
2082
2083static bool imon_find_endpoints(struct imon_context *ictx,
2084 struct usb_host_interface *iface_desc)
2085{
2086 struct usb_endpoint_descriptor *ep;
2087 struct usb_endpoint_descriptor *rx_endpoint = NULL;
2088 struct usb_endpoint_descriptor *tx_endpoint = NULL;
2089 int ifnum = iface_desc->desc.bInterfaceNumber;
2090 int num_endpts = iface_desc->desc.bNumEndpoints;
2091 int i, ep_dir, ep_type;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002092 bool ir_ep_found = false;
2093 bool display_ep_found = false;
2094 bool tx_control = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002095
2096 /*
2097 * Scan the endpoint list and set:
2098 * first input endpoint = IR endpoint
2099 * first output endpoint = display endpoint
2100 */
2101 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
2102 ep = &iface_desc->endpoint[i].desc;
2103 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2104 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2105
2106 if (!ir_ep_found && ep_dir == USB_DIR_IN &&
2107 ep_type == USB_ENDPOINT_XFER_INT) {
2108
2109 rx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002110 ir_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002111 dev_dbg(ictx->dev, "%s: found IR endpoint\n", __func__);
2112
2113 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
2114 ep_type == USB_ENDPOINT_XFER_INT) {
2115 tx_endpoint = ep;
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002116 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002117 dev_dbg(ictx->dev, "%s: found display endpoint\n", __func__);
2118 }
2119 }
2120
2121 if (ifnum == 0) {
2122 ictx->rx_endpoint_intf0 = rx_endpoint;
2123 /*
2124 * tx is used to send characters to lcd/vfd, associate RF
2125 * remotes, set IR protocol, and maybe more...
2126 */
2127 ictx->tx_endpoint = tx_endpoint;
2128 } else {
2129 ictx->rx_endpoint_intf1 = rx_endpoint;
2130 }
2131
2132 /*
2133 * If we didn't find a display endpoint, this is probably one of the
2134 * newer iMON devices that use control urb instead of interrupt
2135 */
2136 if (!display_ep_found) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002137 tx_control = true;
2138 display_ep_found = true;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002139 dev_dbg(ictx->dev, "%s: device uses control endpoint, not "
2140 "interface OUT endpoint\n", __func__);
2141 }
2142
2143 /*
2144 * Some iMON receivers have no display. Unfortunately, it seems
2145 * that SoundGraph recycles device IDs between devices both with
2146 * and without... :\
2147 */
2148 if (ictx->display_type == IMON_DISPLAY_TYPE_NONE) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002149 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002150 dev_dbg(ictx->dev, "%s: device has no display\n", __func__);
2151 }
2152
2153 /*
2154 * iMON Touch devices have a VGA touchscreen, but no "display", as
2155 * that refers to e.g. /dev/lcd0 (a character device LCD or VFD).
2156 */
2157 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002158 display_ep_found = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002159 dev_dbg(ictx->dev, "%s: iMON Touch device found\n", __func__);
2160 }
2161
2162 /* Input endpoint is mandatory */
2163 if (!ir_ep_found)
Joe Perchese2302502010-06-20 04:20:46 -03002164 pr_err("no valid input (IR) endpoint found\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002165
2166 ictx->tx_control = tx_control;
2167
2168 if (display_ep_found)
2169 ictx->display_supported = true;
2170
2171 return ir_ep_found;
2172
2173}
2174
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002175static struct imon_context *imon_init_intf0(struct usb_interface *intf,
2176 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002177{
2178 struct imon_context *ictx;
2179 struct urb *rx_urb;
2180 struct urb *tx_urb;
2181 struct device *dev = &intf->dev;
2182 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002183 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002184
2185 ictx = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
2186 if (!ictx) {
2187 dev_err(dev, "%s: kzalloc failed for context", __func__);
2188 goto exit;
2189 }
2190 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2191 if (!rx_urb) {
2192 dev_err(dev, "%s: usb_alloc_urb failed for IR urb", __func__);
2193 goto rx_urb_alloc_failed;
2194 }
2195 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2196 if (!tx_urb) {
2197 dev_err(dev, "%s: usb_alloc_urb failed for display urb",
2198 __func__);
2199 goto tx_urb_alloc_failed;
2200 }
2201
2202 mutex_init(&ictx->lock);
Jarod Wilson693508d2010-09-15 15:56:03 -03002203 spin_lock_init(&ictx->kc_lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002204
2205 mutex_lock(&ictx->lock);
2206
2207 ictx->dev = dev;
2208 ictx->usbdev_intf0 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002209 ictx->rx_urb_intf0 = rx_urb;
2210 ictx->tx_urb = tx_urb;
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002211 ictx->rf_device = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002212
2213 ictx->vendor = le16_to_cpu(ictx->usbdev_intf0->descriptor.idVendor);
2214 ictx->product = le16_to_cpu(ictx->usbdev_intf0->descriptor.idProduct);
2215
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002216 /* save drive info for later accessing the panel/knob key table */
2217 ictx->dev_descr = (struct imon_usb_dev_descr *)id->driver_info;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002218 /* default send_packet delay is 5ms but some devices need more */
Ulrich Eckhardt0d8053f2014-07-26 14:56:01 -03002219 ictx->send_packet_delay = ictx->dev_descr->flags &
2220 IMON_NEED_20MS_PKT_DELAY ? 20 : 5;
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002221
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002222 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002223 iface_desc = intf->cur_altsetting;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002224 if (!imon_find_endpoints(ictx, iface_desc)) {
Jarod Wilson21677cf2010-04-16 18:29:02 -03002225 goto find_endpoint_failed;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002226 }
Jarod Wilson21677cf2010-04-16 18:29:02 -03002227
Jarod Wilson21677cf2010-04-16 18:29:02 -03002228 usb_fill_int_urb(ictx->rx_urb_intf0, ictx->usbdev_intf0,
2229 usb_rcvintpipe(ictx->usbdev_intf0,
2230 ictx->rx_endpoint_intf0->bEndpointAddress),
2231 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2232 usb_rx_callback_intf0, ictx,
2233 ictx->rx_endpoint_intf0->bInterval);
2234
2235 ret = usb_submit_urb(ictx->rx_urb_intf0, GFP_KERNEL);
2236 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002237 pr_err("usb_submit_urb failed for intf0 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002238 goto urb_submit_failed;
2239 }
2240
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002241 ictx->idev = imon_init_idev(ictx);
2242 if (!ictx->idev) {
2243 dev_err(dev, "%s: input device setup failed\n", __func__);
2244 goto idev_setup_failed;
2245 }
2246
2247 ictx->rdev = imon_init_rdev(ictx);
2248 if (!ictx->rdev) {
2249 dev_err(dev, "%s: rc device setup failed\n", __func__);
2250 goto rdev_setup_failed;
2251 }
2252
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002253 ictx->dev_present_intf0 = true;
2254
Jarod Wilson23ef7102011-04-27 19:01:44 -03002255 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002256 return ictx;
2257
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002258rdev_setup_failed:
2259 input_unregister_device(ictx->idev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002260idev_setup_failed:
Jarod Wilson9ad77eb2011-01-06 16:59:34 -03002261 usb_kill_urb(ictx->rx_urb_intf0);
2262urb_submit_failed:
Jarod Wilson21677cf2010-04-16 18:29:02 -03002263find_endpoint_failed:
2264 mutex_unlock(&ictx->lock);
2265 usb_free_urb(tx_urb);
2266tx_urb_alloc_failed:
2267 usb_free_urb(rx_urb);
2268rx_urb_alloc_failed:
2269 kfree(ictx);
2270exit:
2271 dev_err(dev, "unable to initialize intf0, err %d\n", ret);
2272
2273 return NULL;
2274}
2275
2276static struct imon_context *imon_init_intf1(struct usb_interface *intf,
2277 struct imon_context *ictx)
2278{
2279 struct urb *rx_urb;
2280 struct usb_host_interface *iface_desc;
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002281 int ret = -ENOMEM;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002282
2283 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2284 if (!rx_urb) {
Joe Perchese2302502010-06-20 04:20:46 -03002285 pr_err("usb_alloc_urb failed for IR urb\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002286 goto rx_urb_alloc_failed;
2287 }
2288
2289 mutex_lock(&ictx->lock);
2290
2291 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2292 init_timer(&ictx->ttimer);
2293 ictx->ttimer.data = (unsigned long)ictx;
2294 ictx->ttimer.function = imon_touch_display_timeout;
2295 }
2296
2297 ictx->usbdev_intf1 = usb_get_dev(interface_to_usbdev(intf));
Jarod Wilson21677cf2010-04-16 18:29:02 -03002298 ictx->rx_urb_intf1 = rx_urb;
2299
Mauro Carvalho Chehab1f71bae2010-04-20 19:11:30 -03002300 ret = -ENODEV;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002301 iface_desc = intf->cur_altsetting;
2302 if (!imon_find_endpoints(ictx, iface_desc))
2303 goto find_endpoint_failed;
2304
2305 if (ictx->display_type == IMON_DISPLAY_TYPE_VGA) {
2306 ictx->touch = imon_init_touch(ictx);
2307 if (!ictx->touch)
2308 goto touch_setup_failed;
2309 } else
2310 ictx->touch = NULL;
2311
2312 usb_fill_int_urb(ictx->rx_urb_intf1, ictx->usbdev_intf1,
2313 usb_rcvintpipe(ictx->usbdev_intf1,
2314 ictx->rx_endpoint_intf1->bEndpointAddress),
2315 ictx->usb_rx_buf, sizeof(ictx->usb_rx_buf),
2316 usb_rx_callback_intf1, ictx,
2317 ictx->rx_endpoint_intf1->bInterval);
2318
2319 ret = usb_submit_urb(ictx->rx_urb_intf1, GFP_KERNEL);
2320
2321 if (ret) {
Joe Perchese2302502010-06-20 04:20:46 -03002322 pr_err("usb_submit_urb failed for intf1 (%d)\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002323 goto urb_submit_failed;
2324 }
2325
Jarod Wilson6f6b90c2011-07-19 12:12:47 -03002326 ictx->dev_present_intf1 = true;
2327
Jarod Wilson23ef7102011-04-27 19:01:44 -03002328 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002329 return ictx;
2330
2331urb_submit_failed:
Jarod Wilson20cd1952010-07-26 11:11:36 -03002332 if (ictx->touch)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002333 input_unregister_device(ictx->touch);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002334touch_setup_failed:
2335find_endpoint_failed:
2336 mutex_unlock(&ictx->lock);
2337 usb_free_urb(rx_urb);
2338rx_urb_alloc_failed:
Jarod Wilson8791d632012-01-26 12:04:11 -03002339 dev_err(ictx->dev, "unable to initialize intf1, err %d\n", ret);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002340
2341 return NULL;
2342}
2343
Jarod Wilson21677cf2010-04-16 18:29:02 -03002344static void imon_init_display(struct imon_context *ictx,
2345 struct usb_interface *intf)
2346{
2347 int ret;
2348
2349 dev_dbg(ictx->dev, "Registering iMON display with sysfs\n");
2350
2351 /* set up sysfs entry for built-in clock */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002352 ret = sysfs_create_group(&intf->dev.kobj, &imon_display_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002353 if (ret)
2354 dev_err(ictx->dev, "Could not create display sysfs "
2355 "entries(%d)", ret);
2356
2357 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2358 ret = usb_register_dev(intf, &imon_lcd_class);
2359 else
2360 ret = usb_register_dev(intf, &imon_vfd_class);
2361 if (ret)
2362 /* Not a fatal error, so ignore */
2363 dev_info(ictx->dev, "could not get a minor number for "
2364 "display\n");
2365
2366}
2367
2368/**
2369 * Callback function for USB core API: Probe
2370 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002371static int imon_probe(struct usb_interface *interface,
2372 const struct usb_device_id *id)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002373{
2374 struct usb_device *usbdev = NULL;
2375 struct usb_host_interface *iface_desc = NULL;
2376 struct usb_interface *first_if;
2377 struct device *dev = &interface->dev;
Jarod Wilson76a2d212011-04-28 18:20:58 -03002378 int ifnum, sysfs_err;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002379 int ret = 0;
2380 struct imon_context *ictx = NULL;
2381 struct imon_context *first_if_ctx = NULL;
2382 u16 vendor, product;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002383
Jarod Wilson21677cf2010-04-16 18:29:02 -03002384 usbdev = usb_get_dev(interface_to_usbdev(interface));
2385 iface_desc = interface->cur_altsetting;
2386 ifnum = iface_desc->desc.bInterfaceNumber;
2387 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
2388 product = le16_to_cpu(usbdev->descriptor.idProduct);
2389
2390 dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
2391 __func__, vendor, product, ifnum);
2392
2393 /* prevent races probing devices w/multiple interfaces */
2394 mutex_lock(&driver_lock);
2395
2396 first_if = usb_ifnum_to_if(usbdev, 0);
Joe Perches8350e152010-11-30 18:42:07 -03002397 first_if_ctx = usb_get_intfdata(first_if);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002398
2399 if (ifnum == 0) {
Kevin Baradonf7d141b2013-04-22 16:09:44 -03002400 ictx = imon_init_intf0(interface, id);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002401 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002402 pr_err("failed to initialize context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002403 ret = -ENODEV;
2404 goto fail;
2405 }
2406
Jarod Wilson21677cf2010-04-16 18:29:02 -03002407 } else {
Kevin Baradon7d54ba02013-04-22 16:09:45 -03002408 /* this is the secondary interface on the device */
2409
2410 /* fail early if first intf failed to register */
2411 if (!first_if_ctx) {
2412 ret = -ENODEV;
2413 goto fail;
2414 }
2415
Jarod Wilson21677cf2010-04-16 18:29:02 -03002416 ictx = imon_init_intf1(interface, first_if_ctx);
2417 if (!ictx) {
Joe Perchese2302502010-06-20 04:20:46 -03002418 pr_err("failed to attach to context!\n");
Jarod Wilson21677cf2010-04-16 18:29:02 -03002419 ret = -ENODEV;
2420 goto fail;
2421 }
2422
2423 }
2424
2425 usb_set_intfdata(interface, ictx);
2426
2427 if (ifnum == 0) {
Jarod Wilson23ef7102011-04-27 19:01:44 -03002428 mutex_lock(&ictx->lock);
2429
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002430 if (product == 0xffdc && ictx->rf_device) {
2431 sysfs_err = sysfs_create_group(&interface->dev.kobj,
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002432 &imon_rf_attr_group);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002433 if (sysfs_err)
Joe Perchese2302502010-06-20 04:20:46 -03002434 pr_err("Could not create RF sysfs entries(%d)\n",
2435 sysfs_err);
Jarod Wilsonbbe46902010-05-24 12:02:05 -03002436 }
2437
Jarod Wilson21677cf2010-04-16 18:29:02 -03002438 if (ictx->display_supported)
2439 imon_init_display(ictx, interface);
Jarod Wilson23ef7102011-04-27 19:01:44 -03002440
2441 mutex_unlock(&ictx->lock);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002442 }
2443
Jarod Wilson21677cf2010-04-16 18:29:02 -03002444 dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
2445 "usb<%d:%d> initialized\n", vendor, product, ifnum,
2446 usbdev->bus->busnum, usbdev->devnum);
2447
Jarod Wilson21677cf2010-04-16 18:29:02 -03002448 mutex_unlock(&driver_lock);
2449
2450 return 0;
2451
2452fail:
2453 mutex_unlock(&driver_lock);
2454 dev_err(dev, "unable to register, err %d\n", ret);
2455
2456 return ret;
2457}
2458
2459/**
2460 * Callback function for USB core API: disconnect
2461 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002462static void imon_disconnect(struct usb_interface *interface)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002463{
2464 struct imon_context *ictx;
2465 struct device *dev;
2466 int ifnum;
2467
2468 /* prevent races with multi-interface device probing and display_open */
2469 mutex_lock(&driver_lock);
2470
2471 ictx = usb_get_intfdata(interface);
2472 dev = ictx->dev;
2473 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
2474
Jarod Wilson21677cf2010-04-16 18:29:02 -03002475 /*
2476 * sysfs_remove_group is safe to call even if sysfs_create_group
2477 * hasn't been called
2478 */
Jarod Wilson6aa209e2010-10-23 16:42:51 -03002479 sysfs_remove_group(&interface->dev.kobj, &imon_display_attr_group);
2480 sysfs_remove_group(&interface->dev.kobj, &imon_rf_attr_group);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002481
2482 usb_set_intfdata(interface, NULL);
2483
2484 /* Abort ongoing write */
2485 if (ictx->tx.busy) {
2486 usb_kill_urb(ictx->tx_urb);
2487 complete_all(&ictx->tx.finished);
2488 }
2489
2490 if (ifnum == 0) {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002491 ictx->dev_present_intf0 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002492 usb_kill_urb(ictx->rx_urb_intf0);
David Härdemaneaf2bcc2010-09-15 15:42:07 -03002493 input_unregister_device(ictx->idev);
David Härdemand8b4b582010-10-29 16:08:23 -03002494 rc_unregister_device(ictx->rdev);
Jarod Wilson21677cf2010-04-16 18:29:02 -03002495 if (ictx->display_supported) {
2496 if (ictx->display_type == IMON_DISPLAY_TYPE_LCD)
2497 usb_deregister_dev(interface, &imon_lcd_class);
Jarod Wilson76a2d212011-04-28 18:20:58 -03002498 else if (ictx->display_type == IMON_DISPLAY_TYPE_VFD)
Jarod Wilson21677cf2010-04-16 18:29:02 -03002499 usb_deregister_dev(interface, &imon_vfd_class);
2500 }
2501 } else {
Jarod Wilsonf789bf42010-05-24 12:00:31 -03002502 ictx->dev_present_intf1 = false;
Jarod Wilson21677cf2010-04-16 18:29:02 -03002503 usb_kill_urb(ictx->rx_urb_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);