ati_remote2.c 21 KB

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