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