ati_remote2.c 23 KB

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