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