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