ati_remote2.c 24 KB

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