ati_remote2.c 23 KB

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