ati_remote2.c 24 KB

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