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