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