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