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