ati_remote2.c 24 KB

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