ati_remote2.c 23 KB

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