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. int scancode, int *keycode)
  392. {
  393. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  394. int index, mode;
  395. mode = scancode >> 8;
  396. if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
  397. return -EINVAL;
  398. index = ati_remote2_lookup(scancode & 0xFF);
  399. if (index < 0)
  400. return -EINVAL;
  401. *keycode = ar2->keycode[mode][index];
  402. return 0;
  403. }
  404. static int ati_remote2_setkeycode(struct input_dev *idev, int scancode, int keycode)
  405. {
  406. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  407. int index, mode, old_keycode;
  408. mode = scancode >> 8;
  409. if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
  410. return -EINVAL;
  411. index = ati_remote2_lookup(scancode & 0xFF);
  412. if (index < 0)
  413. return -EINVAL;
  414. if (keycode < KEY_RESERVED || keycode > KEY_MAX)
  415. return -EINVAL;
  416. old_keycode = ar2->keycode[mode][index];
  417. ar2->keycode[mode][index] = keycode;
  418. set_bit(keycode, idev->keybit);
  419. for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
  420. for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
  421. if (ar2->keycode[mode][index] == old_keycode)
  422. return 0;
  423. }
  424. }
  425. clear_bit(old_keycode, idev->keybit);
  426. return 0;
  427. }
  428. static int ati_remote2_input_init(struct ati_remote2 *ar2)
  429. {
  430. struct input_dev *idev;
  431. int index, mode, retval;
  432. idev = input_allocate_device();
  433. if (!idev)
  434. return -ENOMEM;
  435. ar2->idev = idev;
  436. input_set_drvdata(idev, ar2);
  437. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
  438. idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  439. BIT_MASK(BTN_RIGHT);
  440. idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  441. for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
  442. for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
  443. ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
  444. set_bit(ar2->keycode[mode][index], idev->keybit);
  445. }
  446. }
  447. /* AUX1-AUX4 and PC generate the same scancode. */
  448. index = ati_remote2_lookup(0x3f);
  449. ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
  450. ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
  451. ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
  452. ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
  453. ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
  454. set_bit(KEY_PROG1, idev->keybit);
  455. set_bit(KEY_PROG2, idev->keybit);
  456. set_bit(KEY_PROG3, idev->keybit);
  457. set_bit(KEY_PROG4, idev->keybit);
  458. set_bit(KEY_PC, idev->keybit);
  459. idev->rep[REP_DELAY] = 250;
  460. idev->rep[REP_PERIOD] = 33;
  461. idev->open = ati_remote2_open;
  462. idev->close = ati_remote2_close;
  463. idev->getkeycode = ati_remote2_getkeycode;
  464. idev->setkeycode = ati_remote2_setkeycode;
  465. idev->name = ar2->name;
  466. idev->phys = ar2->phys;
  467. usb_to_input_id(ar2->udev, &idev->id);
  468. idev->dev.parent = &ar2->udev->dev;
  469. retval = input_register_device(idev);
  470. if (retval)
  471. input_free_device(idev);
  472. return retval;
  473. }
  474. static int ati_remote2_urb_init(struct ati_remote2 *ar2)
  475. {
  476. struct usb_device *udev = ar2->udev;
  477. int i, pipe, maxp;
  478. for (i = 0; i < 2; i++) {
  479. ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
  480. if (!ar2->buf[i])
  481. return -ENOMEM;
  482. ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  483. if (!ar2->urb[i])
  484. return -ENOMEM;
  485. pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
  486. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  487. maxp = maxp > 4 ? 4 : maxp;
  488. usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
  489. i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
  490. ar2, ar2->ep[i]->bInterval);
  491. ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
  492. ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  493. }
  494. return 0;
  495. }
  496. static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
  497. {
  498. int i;
  499. for (i = 0; i < 2; i++) {
  500. usb_free_urb(ar2->urb[i]);
  501. usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
  502. }
  503. }
  504. static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
  505. {
  506. int r, i, channel;
  507. /*
  508. * Configure receiver to only accept input from remote "channel"
  509. * channel == 0 -> Accept input from any remote channel
  510. * channel == 1 -> Only accept input from remote channel 1
  511. * channel == 2 -> Only accept input from remote channel 2
  512. * ...
  513. * channel == 16 -> Only accept input from remote channel 16
  514. */
  515. channel = 0;
  516. for (i = 0; i < 16; i++) {
  517. if ((1 << i) & ch_mask) {
  518. if (!(~(1 << i) & ch_mask))
  519. channel = i + 1;
  520. break;
  521. }
  522. }
  523. r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
  524. 0x20,
  525. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  526. channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  527. if (r) {
  528. dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
  529. __func__, r);
  530. return r;
  531. }
  532. return 0;
  533. }
  534. static ssize_t ati_remote2_show_channel_mask(struct device *dev,
  535. struct device_attribute *attr,
  536. char *buf)
  537. {
  538. struct usb_device *udev = to_usb_device(dev);
  539. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  540. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  541. return sprintf(buf, "0x%04x\n", ar2->channel_mask);
  542. }
  543. static ssize_t ati_remote2_store_channel_mask(struct device *dev,
  544. struct device_attribute *attr,
  545. const char *buf, size_t count)
  546. {
  547. struct usb_device *udev = to_usb_device(dev);
  548. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  549. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  550. unsigned long mask;
  551. int r;
  552. if (strict_strtoul(buf, 0, &mask))
  553. return -EINVAL;
  554. if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
  555. return -EINVAL;
  556. r = usb_autopm_get_interface(ar2->intf[0]);
  557. if (r) {
  558. dev_err(&ar2->intf[0]->dev,
  559. "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
  560. return r;
  561. }
  562. mutex_lock(&ati_remote2_mutex);
  563. if (mask != ar2->channel_mask && !ati_remote2_setup(ar2, mask))
  564. ar2->channel_mask = mask;
  565. mutex_unlock(&ati_remote2_mutex);
  566. usb_autopm_put_interface(ar2->intf[0]);
  567. return count;
  568. }
  569. static ssize_t ati_remote2_show_mode_mask(struct device *dev,
  570. struct device_attribute *attr,
  571. char *buf)
  572. {
  573. struct usb_device *udev = to_usb_device(dev);
  574. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  575. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  576. return sprintf(buf, "0x%02x\n", ar2->mode_mask);
  577. }
  578. static ssize_t ati_remote2_store_mode_mask(struct device *dev,
  579. struct device_attribute *attr,
  580. const char *buf, size_t count)
  581. {
  582. struct usb_device *udev = to_usb_device(dev);
  583. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  584. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  585. unsigned long mask;
  586. if (strict_strtoul(buf, 0, &mask))
  587. return -EINVAL;
  588. if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
  589. return -EINVAL;
  590. ar2->mode_mask = mask;
  591. return count;
  592. }
  593. static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
  594. ati_remote2_store_channel_mask);
  595. static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
  596. ati_remote2_store_mode_mask);
  597. static struct attribute *ati_remote2_attrs[] = {
  598. &dev_attr_channel_mask.attr,
  599. &dev_attr_mode_mask.attr,
  600. NULL,
  601. };
  602. static struct attribute_group ati_remote2_attr_group = {
  603. .attrs = ati_remote2_attrs,
  604. };
  605. static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
  606. {
  607. struct usb_device *udev = interface_to_usbdev(interface);
  608. struct usb_host_interface *alt = interface->cur_altsetting;
  609. struct ati_remote2 *ar2;
  610. int r;
  611. if (alt->desc.bInterfaceNumber)
  612. return -ENODEV;
  613. ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
  614. if (!ar2)
  615. return -ENOMEM;
  616. ar2->udev = udev;
  617. ar2->intf[0] = interface;
  618. ar2->ep[0] = &alt->endpoint[0].desc;
  619. ar2->intf[1] = usb_ifnum_to_if(udev, 1);
  620. r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
  621. if (r)
  622. goto fail1;
  623. alt = ar2->intf[1]->cur_altsetting;
  624. ar2->ep[1] = &alt->endpoint[0].desc;
  625. r = ati_remote2_urb_init(ar2);
  626. if (r)
  627. goto fail2;
  628. ar2->channel_mask = channel_mask;
  629. ar2->mode_mask = mode_mask;
  630. r = ati_remote2_setup(ar2, ar2->channel_mask);
  631. if (r)
  632. goto fail2;
  633. usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
  634. strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
  635. strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
  636. r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
  637. if (r)
  638. goto fail2;
  639. r = ati_remote2_input_init(ar2);
  640. if (r)
  641. goto fail3;
  642. usb_set_intfdata(interface, ar2);
  643. interface->needs_remote_wakeup = 1;
  644. return 0;
  645. fail3:
  646. sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
  647. fail2:
  648. ati_remote2_urb_cleanup(ar2);
  649. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  650. fail1:
  651. kfree(ar2);
  652. return r;
  653. }
  654. static void ati_remote2_disconnect(struct usb_interface *interface)
  655. {
  656. struct ati_remote2 *ar2;
  657. struct usb_host_interface *alt = interface->cur_altsetting;
  658. if (alt->desc.bInterfaceNumber)
  659. return;
  660. ar2 = usb_get_intfdata(interface);
  661. usb_set_intfdata(interface, NULL);
  662. input_unregister_device(ar2->idev);
  663. sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
  664. ati_remote2_urb_cleanup(ar2);
  665. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  666. kfree(ar2);
  667. }
  668. static int ati_remote2_suspend(struct usb_interface *interface,
  669. pm_message_t message)
  670. {
  671. struct ati_remote2 *ar2;
  672. struct usb_host_interface *alt = interface->cur_altsetting;
  673. if (alt->desc.bInterfaceNumber)
  674. return 0;
  675. ar2 = usb_get_intfdata(interface);
  676. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  677. mutex_lock(&ati_remote2_mutex);
  678. if (ar2->flags & ATI_REMOTE2_OPENED)
  679. ati_remote2_kill_urbs(ar2);
  680. ar2->flags |= ATI_REMOTE2_SUSPENDED;
  681. mutex_unlock(&ati_remote2_mutex);
  682. return 0;
  683. }
  684. static int ati_remote2_resume(struct usb_interface *interface)
  685. {
  686. struct ati_remote2 *ar2;
  687. struct usb_host_interface *alt = interface->cur_altsetting;
  688. int r = 0;
  689. if (alt->desc.bInterfaceNumber)
  690. return 0;
  691. ar2 = usb_get_intfdata(interface);
  692. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  693. mutex_lock(&ati_remote2_mutex);
  694. if (ar2->flags & ATI_REMOTE2_OPENED)
  695. r = ati_remote2_submit_urbs(ar2);
  696. if (!r)
  697. ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
  698. mutex_unlock(&ati_remote2_mutex);
  699. return r;
  700. }
  701. static int ati_remote2_reset_resume(struct usb_interface *interface)
  702. {
  703. struct ati_remote2 *ar2;
  704. struct usb_host_interface *alt = interface->cur_altsetting;
  705. int r = 0;
  706. if (alt->desc.bInterfaceNumber)
  707. return 0;
  708. ar2 = usb_get_intfdata(interface);
  709. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  710. mutex_lock(&ati_remote2_mutex);
  711. r = ati_remote2_setup(ar2, ar2->channel_mask);
  712. if (r)
  713. goto out;
  714. if (ar2->flags & ATI_REMOTE2_OPENED)
  715. r = ati_remote2_submit_urbs(ar2);
  716. if (!r)
  717. ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
  718. out:
  719. mutex_unlock(&ati_remote2_mutex);
  720. return r;
  721. }
  722. static int ati_remote2_pre_reset(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 0;
  728. ar2 = usb_get_intfdata(interface);
  729. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  730. mutex_lock(&ati_remote2_mutex);
  731. if (ar2->flags == ATI_REMOTE2_OPENED)
  732. ati_remote2_kill_urbs(ar2);
  733. return 0;
  734. }
  735. static int ati_remote2_post_reset(struct usb_interface *interface)
  736. {
  737. struct ati_remote2 *ar2;
  738. struct usb_host_interface *alt = interface->cur_altsetting;
  739. int r = 0;
  740. if (alt->desc.bInterfaceNumber)
  741. return 0;
  742. ar2 = usb_get_intfdata(interface);
  743. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  744. if (ar2->flags == ATI_REMOTE2_OPENED)
  745. r = ati_remote2_submit_urbs(ar2);
  746. mutex_unlock(&ati_remote2_mutex);
  747. return r;
  748. }
  749. static int __init ati_remote2_init(void)
  750. {
  751. int r;
  752. r = usb_register(&ati_remote2_driver);
  753. if (r)
  754. printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
  755. else
  756. printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
  757. return r;
  758. }
  759. static void __exit ati_remote2_exit(void)
  760. {
  761. usb_deregister(&ati_remote2_driver);
  762. }
  763. module_init(ati_remote2_init);
  764. module_exit(ati_remote2_exit);