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