1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ati_remote2 - ATI/Philips USB RF remote driver 4 * 5 * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi> 6 * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk> 7 */ 8 9 #include <linux/usb/input.h> 10 #include <linux/slab.h> 11 #include <linux/module.h> 12 13 #define DRIVER_DESC "ATI/Philips USB RF remote driver" 14 15 MODULE_DESCRIPTION(DRIVER_DESC); 16 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); 17 MODULE_LICENSE("GPL"); 18 19 /* 20 * ATI Remote Wonder II Channel Configuration 21 * 22 * The remote control can be assigned one of sixteen "channels" in order to facilitate 23 * the use of multiple remote controls within range of each other. 24 * A remote's "channel" may be altered by pressing and holding the "PC" button for 25 * approximately 3 seconds, after which the button will slowly flash the count of the 26 * currently configured "channel", using the numeric keypad enter a number between 1 and 27 * 16 and then press the "PC" button again, the button will slowly flash the count of the 28 * newly configured "channel". 29 */ 30 31 enum { 32 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF, 33 ATI_REMOTE2_MAX_MODE_MASK = 0x1F, 34 }; 35 36 static int ati_remote2_set_mask(const char *val, 37 const struct kernel_param *kp, 38 unsigned int max) 39 { 40 unsigned int mask; 41 int ret; 42 43 if (!val) 44 return -EINVAL; 45 46 ret = kstrtouint(val, 0, &mask); 47 if (ret) 48 return ret; 49 50 if (mask & ~max) 51 return -EINVAL; 52 53 *(unsigned int *)kp->arg = mask; 54 55 return 0; 56 } 57 58 static int ati_remote2_set_channel_mask(const char *val, 59 const struct kernel_param *kp) 60 { 61 pr_debug("%s()\n", __func__); 62 63 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK); 64 } 65 66 static int ati_remote2_get_channel_mask(char *buffer, 67 const struct kernel_param *kp) 68 { 69 pr_debug("%s()\n", __func__); 70 71 return sprintf(buffer, "0x%04x\n", *(unsigned int *)kp->arg); 72 } 73 74 static int ati_remote2_set_mode_mask(const char *val, 75 const struct kernel_param *kp) 76 { 77 pr_debug("%s()\n", __func__); 78 79 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK); 80 } 81 82 static int ati_remote2_get_mode_mask(char *buffer, 83 const struct kernel_param *kp) 84 { 85 pr_debug("%s()\n", __func__); 86 87 return sprintf(buffer, "0x%02x\n", *(unsigned int *)kp->arg); 88 } 89 90 static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK; 91 #define param_check_channel_mask(name, p) __param_check(name, p, unsigned int) 92 static const struct kernel_param_ops param_ops_channel_mask = { 93 .set = ati_remote2_set_channel_mask, 94 .get = ati_remote2_get_channel_mask, 95 }; 96 module_param(channel_mask, channel_mask, 0644); 97 MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>"); 98 99 static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK; 100 #define param_check_mode_mask(name, p) __param_check(name, p, unsigned int) 101 static const struct kernel_param_ops param_ops_mode_mask = { 102 .set = ati_remote2_set_mode_mask, 103 .get = ati_remote2_get_mode_mask, 104 }; 105 module_param(mode_mask, mode_mask, 0644); 106 MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>"); 107 108 static const struct usb_device_id ati_remote2_id_table[] = { 109 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */ 110 { } 111 }; 112 MODULE_DEVICE_TABLE(usb, ati_remote2_id_table); 113 114 static DEFINE_MUTEX(ati_remote2_mutex); 115 116 enum { 117 ATI_REMOTE2_OPENED = 0x1, 118 ATI_REMOTE2_SUSPENDED = 0x2, 119 }; 120 121 enum { 122 ATI_REMOTE2_AUX1, 123 ATI_REMOTE2_AUX2, 124 ATI_REMOTE2_AUX3, 125 ATI_REMOTE2_AUX4, 126 ATI_REMOTE2_PC, 127 ATI_REMOTE2_MODES, 128 }; 129 130 static const struct { 131 u8 hw_code; 132 u16 keycode; 133 } ati_remote2_key_table[] = { 134 { 0x00, KEY_0 }, 135 { 0x01, KEY_1 }, 136 { 0x02, KEY_2 }, 137 { 0x03, KEY_3 }, 138 { 0x04, KEY_4 }, 139 { 0x05, KEY_5 }, 140 { 0x06, KEY_6 }, 141 { 0x07, KEY_7 }, 142 { 0x08, KEY_8 }, 143 { 0x09, KEY_9 }, 144 { 0x0c, KEY_POWER }, 145 { 0x0d, KEY_MUTE }, 146 { 0x10, KEY_VOLUMEUP }, 147 { 0x11, KEY_VOLUMEDOWN }, 148 { 0x20, KEY_CHANNELUP }, 149 { 0x21, KEY_CHANNELDOWN }, 150 { 0x28, KEY_FORWARD }, 151 { 0x29, KEY_REWIND }, 152 { 0x2c, KEY_PLAY }, 153 { 0x30, KEY_PAUSE }, 154 { 0x31, KEY_STOP }, 155 { 0x37, KEY_RECORD }, 156 { 0x38, KEY_DVD }, 157 { 0x39, KEY_TV }, 158 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */ 159 { 0x54, KEY_MENU }, 160 { 0x58, KEY_UP }, 161 { 0x59, KEY_DOWN }, 162 { 0x5a, KEY_LEFT }, 163 { 0x5b, KEY_RIGHT }, 164 { 0x5c, KEY_OK }, 165 { 0x78, KEY_A }, 166 { 0x79, KEY_B }, 167 { 0x7a, KEY_C }, 168 { 0x7b, KEY_D }, 169 { 0x7c, KEY_E }, 170 { 0x7d, KEY_F }, 171 { 0x82, KEY_ENTER }, 172 { 0x8e, KEY_VENDOR }, 173 { 0x96, KEY_COFFEE }, 174 { 0xa9, BTN_LEFT }, 175 { 0xaa, BTN_RIGHT }, 176 { 0xbe, KEY_QUESTION }, 177 { 0xd0, KEY_EDIT }, 178 { 0xd5, KEY_FRONT }, 179 { 0xf9, KEY_INFO }, 180 }; 181 182 struct ati_remote2 { 183 struct input_dev *idev; 184 struct usb_device *udev; 185 186 struct usb_interface *intf[2]; 187 struct usb_endpoint_descriptor *ep[2]; 188 struct urb *urb[2]; 189 void *buf[2]; 190 dma_addr_t buf_dma[2]; 191 192 unsigned long jiffies; 193 int mode; 194 195 char name[64]; 196 char phys[64]; 197 198 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */ 199 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)]; 200 201 unsigned int flags; 202 203 unsigned int channel_mask; 204 unsigned int mode_mask; 205 }; 206 207 static struct usb_driver ati_remote2_driver; 208 209 static int ati_remote2_submit_urbs(struct ati_remote2 *ar2) 210 { 211 int r; 212 213 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL); 214 if (r) { 215 dev_err(&ar2->intf[0]->dev, 216 "%s(): usb_submit_urb() = %d\n", __func__, r); 217 return r; 218 } 219 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL); 220 if (r) { 221 usb_kill_urb(ar2->urb[0]); 222 dev_err(&ar2->intf[1]->dev, 223 "%s(): usb_submit_urb() = %d\n", __func__, r); 224 return r; 225 } 226 227 return 0; 228 } 229 230 static void ati_remote2_kill_urbs(struct ati_remote2 *ar2) 231 { 232 usb_kill_urb(ar2->urb[1]); 233 usb_kill_urb(ar2->urb[0]); 234 } 235 236 static int ati_remote2_open(struct input_dev *idev) 237 { 238 struct ati_remote2 *ar2 = input_get_drvdata(idev); 239 int r; 240 241 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 242 243 r = usb_autopm_get_interface(ar2->intf[0]); 244 if (r) { 245 dev_err(&ar2->intf[0]->dev, 246 "%s(): usb_autopm_get_interface() = %d\n", __func__, r); 247 goto fail1; 248 } 249 250 mutex_lock(&ati_remote2_mutex); 251 252 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) { 253 r = ati_remote2_submit_urbs(ar2); 254 if (r) 255 goto fail2; 256 } 257 258 ar2->flags |= ATI_REMOTE2_OPENED; 259 260 mutex_unlock(&ati_remote2_mutex); 261 262 usb_autopm_put_interface(ar2->intf[0]); 263 264 return 0; 265 266 fail2: 267 mutex_unlock(&ati_remote2_mutex); 268 usb_autopm_put_interface(ar2->intf[0]); 269 fail1: 270 return r; 271 } 272 273 static void ati_remote2_close(struct input_dev *idev) 274 { 275 struct ati_remote2 *ar2 = input_get_drvdata(idev); 276 277 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 278 279 mutex_lock(&ati_remote2_mutex); 280 281 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) 282 ati_remote2_kill_urbs(ar2); 283 284 ar2->flags &= ~ATI_REMOTE2_OPENED; 285 286 mutex_unlock(&ati_remote2_mutex); 287 } 288 289 static void ati_remote2_input_mouse(struct ati_remote2 *ar2) 290 { 291 struct input_dev *idev = ar2->idev; 292 u8 *data = ar2->buf[0]; 293 int channel, mode; 294 295 channel = data[0] >> 4; 296 297 if (!((1 << channel) & ar2->channel_mask)) 298 return; 299 300 mode = data[0] & 0x0F; 301 302 if (mode > ATI_REMOTE2_PC) { 303 dev_err(&ar2->intf[0]->dev, 304 "Unknown mode byte (%02x %02x %02x %02x)\n", 305 data[3], data[2], data[1], data[0]); 306 return; 307 } 308 309 if (!((1 << mode) & ar2->mode_mask)) 310 return; 311 312 input_event(idev, EV_REL, REL_X, (s8) data[1]); 313 input_event(idev, EV_REL, REL_Y, (s8) data[2]); 314 input_sync(idev); 315 } 316 317 static int ati_remote2_lookup(unsigned int hw_code) 318 { 319 int i; 320 321 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++) 322 if (ati_remote2_key_table[i].hw_code == hw_code) 323 return i; 324 325 return -1; 326 } 327 328 static void ati_remote2_input_key(struct ati_remote2 *ar2) 329 { 330 struct input_dev *idev = ar2->idev; 331 u8 *data = ar2->buf[1]; 332 int channel, mode, hw_code, index; 333 334 channel = data[0] >> 4; 335 336 if (!((1 << channel) & ar2->channel_mask)) 337 return; 338 339 mode = data[0] & 0x0F; 340 341 if (mode > ATI_REMOTE2_PC) { 342 dev_err(&ar2->intf[1]->dev, 343 "Unknown mode byte (%02x %02x %02x %02x)\n", 344 data[3], data[2], data[1], data[0]); 345 return; 346 } 347 348 hw_code = data[2]; 349 if (hw_code == 0x3f) { 350 /* 351 * For some incomprehensible reason the mouse pad generates 352 * events which look identical to the events from the last 353 * pressed mode key. Naturally we don't want to generate key 354 * events for the mouse pad so we filter out any subsequent 355 * events from the same mode key. 356 */ 357 if (ar2->mode == mode) 358 return; 359 360 if (data[1] == 0) 361 ar2->mode = mode; 362 } 363 364 if (!((1 << mode) & ar2->mode_mask)) 365 return; 366 367 index = ati_remote2_lookup(hw_code); 368 if (index < 0) { 369 dev_err(&ar2->intf[1]->dev, 370 "Unknown code byte (%02x %02x %02x %02x)\n", 371 data[3], data[2], data[1], data[0]); 372 return; 373 } 374 375 switch (data[1]) { 376 case 0: /* release */ 377 break; 378 case 1: /* press */ 379 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]); 380 break; 381 case 2: /* repeat */ 382 383 /* No repeat for mouse buttons. */ 384 if (ar2->keycode[mode][index] == BTN_LEFT || 385 ar2->keycode[mode][index] == BTN_RIGHT) 386 return; 387 388 if (!time_after_eq(jiffies, ar2->jiffies)) 389 return; 390 391 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]); 392 break; 393 default: 394 dev_err(&ar2->intf[1]->dev, 395 "Unknown state byte (%02x %02x %02x %02x)\n", 396 data[3], data[2], data[1], data[0]); 397 return; 398 } 399 400 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]); 401 input_sync(idev); 402 } 403 404 static void ati_remote2_complete_mouse(struct urb *urb) 405 { 406 struct ati_remote2 *ar2 = urb->context; 407 int r; 408 409 switch (urb->status) { 410 case 0: 411 usb_mark_last_busy(ar2->udev); 412 ati_remote2_input_mouse(ar2); 413 break; 414 case -ENOENT: 415 case -EILSEQ: 416 case -ECONNRESET: 417 case -ESHUTDOWN: 418 dev_dbg(&ar2->intf[0]->dev, 419 "%s(): urb status = %d\n", __func__, urb->status); 420 return; 421 default: 422 usb_mark_last_busy(ar2->udev); 423 dev_err(&ar2->intf[0]->dev, 424 "%s(): urb status = %d\n", __func__, urb->status); 425 } 426 427 r = usb_submit_urb(urb, GFP_ATOMIC); 428 if (r) 429 dev_err(&ar2->intf[0]->dev, 430 "%s(): usb_submit_urb() = %d\n", __func__, r); 431 } 432 433 static void ati_remote2_complete_key(struct urb *urb) 434 { 435 struct ati_remote2 *ar2 = urb->context; 436 int r; 437 438 switch (urb->status) { 439 case 0: 440 usb_mark_last_busy(ar2->udev); 441 ati_remote2_input_key(ar2); 442 break; 443 case -ENOENT: 444 case -EILSEQ: 445 case -ECONNRESET: 446 case -ESHUTDOWN: 447 dev_dbg(&ar2->intf[1]->dev, 448 "%s(): urb status = %d\n", __func__, urb->status); 449 return; 450 default: 451 usb_mark_last_busy(ar2->udev); 452 dev_err(&ar2->intf[1]->dev, 453 "%s(): urb status = %d\n", __func__, urb->status); 454 } 455 456 r = usb_submit_urb(urb, GFP_ATOMIC); 457 if (r) 458 dev_err(&ar2->intf[1]->dev, 459 "%s(): usb_submit_urb() = %d\n", __func__, r); 460 } 461 462 static int ati_remote2_getkeycode(struct input_dev *idev, 463 struct input_keymap_entry *ke) 464 { 465 struct ati_remote2 *ar2 = input_get_drvdata(idev); 466 unsigned int mode; 467 int offset; 468 unsigned int index; 469 unsigned int scancode; 470 471 if (ke->flags & INPUT_KEYMAP_BY_INDEX) { 472 index = ke->index; 473 if (index >= ATI_REMOTE2_MODES * 474 ARRAY_SIZE(ati_remote2_key_table)) 475 return -EINVAL; 476 477 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table); 478 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table); 479 scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code; 480 } else { 481 if (input_scancode_to_scalar(ke, &scancode)) 482 return -EINVAL; 483 484 mode = scancode >> 8; 485 if (mode > ATI_REMOTE2_PC) 486 return -EINVAL; 487 488 offset = ati_remote2_lookup(scancode & 0xff); 489 if (offset < 0) 490 return -EINVAL; 491 492 index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset; 493 } 494 495 ke->keycode = ar2->keycode[mode][offset]; 496 ke->len = sizeof(scancode); 497 memcpy(&ke->scancode, &scancode, sizeof(scancode)); 498 ke->index = index; 499 500 return 0; 501 } 502 503 static int ati_remote2_setkeycode(struct input_dev *idev, 504 const struct input_keymap_entry *ke, 505 unsigned int *old_keycode) 506 { 507 struct ati_remote2 *ar2 = input_get_drvdata(idev); 508 unsigned int mode; 509 int offset; 510 unsigned int index; 511 unsigned int scancode; 512 513 if (ke->flags & INPUT_KEYMAP_BY_INDEX) { 514 if (ke->index >= ATI_REMOTE2_MODES * 515 ARRAY_SIZE(ati_remote2_key_table)) 516 return -EINVAL; 517 518 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table); 519 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table); 520 } else { 521 if (input_scancode_to_scalar(ke, &scancode)) 522 return -EINVAL; 523 524 mode = scancode >> 8; 525 if (mode > ATI_REMOTE2_PC) 526 return -EINVAL; 527 528 offset = ati_remote2_lookup(scancode & 0xff); 529 if (offset < 0) 530 return -EINVAL; 531 } 532 533 *old_keycode = ar2->keycode[mode][offset]; 534 ar2->keycode[mode][offset] = ke->keycode; 535 __set_bit(ke->keycode, idev->keybit); 536 537 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { 538 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { 539 if (ar2->keycode[mode][index] == *old_keycode) 540 return 0; 541 } 542 } 543 544 __clear_bit(*old_keycode, idev->keybit); 545 546 return 0; 547 } 548 549 static int ati_remote2_input_init(struct ati_remote2 *ar2) 550 { 551 struct input_dev *idev; 552 int index, mode, retval; 553 554 idev = input_allocate_device(); 555 if (!idev) 556 return -ENOMEM; 557 558 ar2->idev = idev; 559 input_set_drvdata(idev, ar2); 560 561 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL); 562 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | 563 BIT_MASK(BTN_RIGHT); 564 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); 565 566 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { 567 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { 568 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode; 569 __set_bit(ar2->keycode[mode][index], idev->keybit); 570 } 571 } 572 573 /* AUX1-AUX4 and PC generate the same scancode. */ 574 index = ati_remote2_lookup(0x3f); 575 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1; 576 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2; 577 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3; 578 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4; 579 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC; 580 __set_bit(KEY_PROG1, idev->keybit); 581 __set_bit(KEY_PROG2, idev->keybit); 582 __set_bit(KEY_PROG3, idev->keybit); 583 __set_bit(KEY_PROG4, idev->keybit); 584 __set_bit(KEY_PC, idev->keybit); 585 586 idev->rep[REP_DELAY] = 250; 587 idev->rep[REP_PERIOD] = 33; 588 589 idev->open = ati_remote2_open; 590 idev->close = ati_remote2_close; 591 592 idev->getkeycode = ati_remote2_getkeycode; 593 idev->setkeycode = ati_remote2_setkeycode; 594 595 idev->name = ar2->name; 596 idev->phys = ar2->phys; 597 598 usb_to_input_id(ar2->udev, &idev->id); 599 idev->dev.parent = &ar2->udev->dev; 600 601 retval = input_register_device(idev); 602 if (retval) 603 input_free_device(idev); 604 605 return retval; 606 } 607 608 static int ati_remote2_urb_init(struct ati_remote2 *ar2) 609 { 610 struct usb_device *udev = ar2->udev; 611 int i, pipe, maxp; 612 613 for (i = 0; i < 2; i++) { 614 ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]); 615 if (!ar2->buf[i]) 616 return -ENOMEM; 617 618 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL); 619 if (!ar2->urb[i]) 620 return -ENOMEM; 621 622 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress); 623 maxp = usb_maxpacket(udev, pipe); 624 maxp = maxp > 4 ? 4 : maxp; 625 626 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp, 627 i ? ati_remote2_complete_key : ati_remote2_complete_mouse, 628 ar2, ar2->ep[i]->bInterval); 629 ar2->urb[i]->transfer_dma = ar2->buf_dma[i]; 630 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 631 } 632 633 return 0; 634 } 635 636 static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2) 637 { 638 int i; 639 640 for (i = 0; i < 2; i++) { 641 usb_free_urb(ar2->urb[i]); 642 usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]); 643 } 644 } 645 646 static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask) 647 { 648 int r, i, channel; 649 650 /* 651 * Configure receiver to only accept input from remote "channel" 652 * channel == 0 -> Accept input from any remote channel 653 * channel == 1 -> Only accept input from remote channel 1 654 * channel == 2 -> Only accept input from remote channel 2 655 * ... 656 * channel == 16 -> Only accept input from remote channel 16 657 */ 658 659 channel = 0; 660 for (i = 0; i < 16; i++) { 661 if ((1 << i) & ch_mask) { 662 if (!(~(1 << i) & ch_mask)) 663 channel = i + 1; 664 break; 665 } 666 } 667 668 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0), 669 0x20, 670 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 671 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT); 672 if (r) { 673 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n", 674 __func__, r); 675 return r; 676 } 677 678 return 0; 679 } 680 681 static ssize_t ati_remote2_show_channel_mask(struct device *dev, 682 struct device_attribute *attr, 683 char *buf) 684 { 685 struct usb_device *udev = to_usb_device(dev); 686 struct usb_interface *intf = usb_ifnum_to_if(udev, 0); 687 struct ati_remote2 *ar2 = usb_get_intfdata(intf); 688 689 return sprintf(buf, "0x%04x\n", ar2->channel_mask); 690 } 691 692 static ssize_t ati_remote2_store_channel_mask(struct device *dev, 693 struct device_attribute *attr, 694 const char *buf, size_t count) 695 { 696 struct usb_device *udev = to_usb_device(dev); 697 struct usb_interface *intf = usb_ifnum_to_if(udev, 0); 698 struct ati_remote2 *ar2 = usb_get_intfdata(intf); 699 unsigned int mask; 700 int r; 701 702 r = kstrtouint(buf, 0, &mask); 703 if (r) 704 return r; 705 706 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK) 707 return -EINVAL; 708 709 r = usb_autopm_get_interface(ar2->intf[0]); 710 if (r) { 711 dev_err(&ar2->intf[0]->dev, 712 "%s(): usb_autopm_get_interface() = %d\n", __func__, r); 713 return r; 714 } 715 716 mutex_lock(&ati_remote2_mutex); 717 718 if (mask != ar2->channel_mask) { 719 r = ati_remote2_setup(ar2, mask); 720 if (!r) 721 ar2->channel_mask = mask; 722 } 723 724 mutex_unlock(&ati_remote2_mutex); 725 726 usb_autopm_put_interface(ar2->intf[0]); 727 728 return r ? r : count; 729 } 730 731 static ssize_t ati_remote2_show_mode_mask(struct device *dev, 732 struct device_attribute *attr, 733 char *buf) 734 { 735 struct usb_device *udev = to_usb_device(dev); 736 struct usb_interface *intf = usb_ifnum_to_if(udev, 0); 737 struct ati_remote2 *ar2 = usb_get_intfdata(intf); 738 739 return sprintf(buf, "0x%02x\n", ar2->mode_mask); 740 } 741 742 static ssize_t ati_remote2_store_mode_mask(struct device *dev, 743 struct device_attribute *attr, 744 const char *buf, size_t count) 745 { 746 struct usb_device *udev = to_usb_device(dev); 747 struct usb_interface *intf = usb_ifnum_to_if(udev, 0); 748 struct ati_remote2 *ar2 = usb_get_intfdata(intf); 749 unsigned int mask; 750 int err; 751 752 err = kstrtouint(buf, 0, &mask); 753 if (err) 754 return err; 755 756 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK) 757 return -EINVAL; 758 759 ar2->mode_mask = mask; 760 761 return count; 762 } 763 764 static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask, 765 ati_remote2_store_channel_mask); 766 767 static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask, 768 ati_remote2_store_mode_mask); 769 770 static struct attribute *ati_remote2_attrs[] = { 771 &dev_attr_channel_mask.attr, 772 &dev_attr_mode_mask.attr, 773 NULL, 774 }; 775 ATTRIBUTE_GROUPS(ati_remote2); 776 777 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id) 778 { 779 struct usb_device *udev = interface_to_usbdev(interface); 780 struct usb_host_interface *alt = interface->cur_altsetting; 781 struct ati_remote2 *ar2; 782 int r; 783 784 if (alt->desc.bInterfaceNumber) 785 return -ENODEV; 786 787 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL); 788 if (!ar2) 789 return -ENOMEM; 790 791 ar2->udev = udev; 792 793 /* Sanity check, first interface must have an endpoint */ 794 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) { 795 dev_err(&interface->dev, 796 "%s(): interface 0 must have an endpoint\n", __func__); 797 r = -ENODEV; 798 goto fail1; 799 } 800 ar2->intf[0] = interface; 801 ar2->ep[0] = &alt->endpoint[0].desc; 802 803 /* Sanity check, the device must have two interfaces */ 804 ar2->intf[1] = usb_ifnum_to_if(udev, 1); 805 if ((udev->actconfig->desc.bNumInterfaces < 2) || !ar2->intf[1]) { 806 dev_err(&interface->dev, "%s(): need 2 interfaces, found %d\n", 807 __func__, udev->actconfig->desc.bNumInterfaces); 808 r = -ENODEV; 809 goto fail1; 810 } 811 812 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2); 813 if (r) 814 goto fail1; 815 816 /* Sanity check, second interface must have an endpoint */ 817 alt = ar2->intf[1]->cur_altsetting; 818 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) { 819 dev_err(&interface->dev, 820 "%s(): interface 1 must have an endpoint\n", __func__); 821 r = -ENODEV; 822 goto fail2; 823 } 824 ar2->ep[1] = &alt->endpoint[0].desc; 825 826 r = ati_remote2_urb_init(ar2); 827 if (r) 828 goto fail3; 829 830 ar2->channel_mask = channel_mask; 831 ar2->mode_mask = mode_mask; 832 833 r = ati_remote2_setup(ar2, ar2->channel_mask); 834 if (r) 835 goto fail3; 836 837 usb_make_path(udev, ar2->phys, sizeof(ar2->phys)); 838 strlcat(ar2->phys, "/input0", sizeof(ar2->phys)); 839 840 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name)); 841 842 r = ati_remote2_input_init(ar2); 843 if (r) 844 goto fail3; 845 846 usb_set_intfdata(interface, ar2); 847 848 interface->needs_remote_wakeup = 1; 849 850 return 0; 851 852 fail3: 853 ati_remote2_urb_cleanup(ar2); 854 fail2: 855 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); 856 fail1: 857 kfree(ar2); 858 859 return r; 860 } 861 862 static void ati_remote2_disconnect(struct usb_interface *interface) 863 { 864 struct ati_remote2 *ar2; 865 struct usb_host_interface *alt = interface->cur_altsetting; 866 867 if (alt->desc.bInterfaceNumber) 868 return; 869 870 ar2 = usb_get_intfdata(interface); 871 usb_set_intfdata(interface, NULL); 872 873 input_unregister_device(ar2->idev); 874 875 ati_remote2_urb_cleanup(ar2); 876 877 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); 878 879 kfree(ar2); 880 } 881 882 static int ati_remote2_suspend(struct usb_interface *interface, 883 pm_message_t message) 884 { 885 struct ati_remote2 *ar2; 886 struct usb_host_interface *alt = interface->cur_altsetting; 887 888 if (alt->desc.bInterfaceNumber) 889 return 0; 890 891 ar2 = usb_get_intfdata(interface); 892 893 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 894 895 mutex_lock(&ati_remote2_mutex); 896 897 if (ar2->flags & ATI_REMOTE2_OPENED) 898 ati_remote2_kill_urbs(ar2); 899 900 ar2->flags |= ATI_REMOTE2_SUSPENDED; 901 902 mutex_unlock(&ati_remote2_mutex); 903 904 return 0; 905 } 906 907 static int ati_remote2_resume(struct usb_interface *interface) 908 { 909 struct ati_remote2 *ar2; 910 struct usb_host_interface *alt = interface->cur_altsetting; 911 int r = 0; 912 913 if (alt->desc.bInterfaceNumber) 914 return 0; 915 916 ar2 = usb_get_intfdata(interface); 917 918 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 919 920 mutex_lock(&ati_remote2_mutex); 921 922 if (ar2->flags & ATI_REMOTE2_OPENED) 923 r = ati_remote2_submit_urbs(ar2); 924 925 if (!r) 926 ar2->flags &= ~ATI_REMOTE2_SUSPENDED; 927 928 mutex_unlock(&ati_remote2_mutex); 929 930 return r; 931 } 932 933 static int ati_remote2_reset_resume(struct usb_interface *interface) 934 { 935 struct ati_remote2 *ar2; 936 struct usb_host_interface *alt = interface->cur_altsetting; 937 int r = 0; 938 939 if (alt->desc.bInterfaceNumber) 940 return 0; 941 942 ar2 = usb_get_intfdata(interface); 943 944 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 945 946 mutex_lock(&ati_remote2_mutex); 947 948 r = ati_remote2_setup(ar2, ar2->channel_mask); 949 if (r) 950 goto out; 951 952 if (ar2->flags & ATI_REMOTE2_OPENED) 953 r = ati_remote2_submit_urbs(ar2); 954 955 if (!r) 956 ar2->flags &= ~ATI_REMOTE2_SUSPENDED; 957 958 out: 959 mutex_unlock(&ati_remote2_mutex); 960 961 return r; 962 } 963 964 static int ati_remote2_pre_reset(struct usb_interface *interface) 965 { 966 struct ati_remote2 *ar2; 967 struct usb_host_interface *alt = interface->cur_altsetting; 968 969 if (alt->desc.bInterfaceNumber) 970 return 0; 971 972 ar2 = usb_get_intfdata(interface); 973 974 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 975 976 mutex_lock(&ati_remote2_mutex); 977 978 if (ar2->flags == ATI_REMOTE2_OPENED) 979 ati_remote2_kill_urbs(ar2); 980 981 return 0; 982 } 983 984 static int ati_remote2_post_reset(struct usb_interface *interface) 985 { 986 struct ati_remote2 *ar2; 987 struct usb_host_interface *alt = interface->cur_altsetting; 988 int r = 0; 989 990 if (alt->desc.bInterfaceNumber) 991 return 0; 992 993 ar2 = usb_get_intfdata(interface); 994 995 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 996 997 if (ar2->flags == ATI_REMOTE2_OPENED) 998 r = ati_remote2_submit_urbs(ar2); 999 1000 mutex_unlock(&ati_remote2_mutex); 1001 1002 return r; 1003 } 1004 1005 static struct usb_driver ati_remote2_driver = { 1006 .name = "ati_remote2", 1007 .probe = ati_remote2_probe, 1008 .disconnect = ati_remote2_disconnect, 1009 .dev_groups = ati_remote2_groups, 1010 .id_table = ati_remote2_id_table, 1011 .suspend = ati_remote2_suspend, 1012 .resume = ati_remote2_resume, 1013 .reset_resume = ati_remote2_reset_resume, 1014 .pre_reset = ati_remote2_pre_reset, 1015 .post_reset = ati_remote2_post_reset, 1016 .supports_autosuspend = 1, 1017 }; 1018 1019 module_usb_driver(ati_remote2_driver); 1020