ati_remote2.c 24 KB

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