ati_remote2.c 23 KB

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