ati_remote2.c 19 KB

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