ati_remote2.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. return r;
  207. }
  208. scoped_guard(mutex, &ati_remote2_mutex) {
  209. if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
  210. r = ati_remote2_submit_urbs(ar2);
  211. if (r)
  212. break;
  213. }
  214. ar2->flags |= ATI_REMOTE2_OPENED;
  215. }
  216. usb_autopm_put_interface(ar2->intf[0]);
  217. return r;
  218. }
  219. static void ati_remote2_close(struct input_dev *idev)
  220. {
  221. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  222. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  223. guard(mutex)(&ati_remote2_mutex);
  224. if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
  225. ati_remote2_kill_urbs(ar2);
  226. ar2->flags &= ~ATI_REMOTE2_OPENED;
  227. }
  228. static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
  229. {
  230. struct input_dev *idev = ar2->idev;
  231. u8 *data = ar2->buf[0];
  232. int channel, mode;
  233. channel = data[0] >> 4;
  234. if (!((1 << channel) & ar2->channel_mask))
  235. return;
  236. mode = data[0] & 0x0F;
  237. if (mode > ATI_REMOTE2_PC) {
  238. dev_err(&ar2->intf[0]->dev,
  239. "Unknown mode byte (%02x %02x %02x %02x)\n",
  240. data[3], data[2], data[1], data[0]);
  241. return;
  242. }
  243. if (!((1 << mode) & ar2->mode_mask))
  244. return;
  245. input_event(idev, EV_REL, REL_X, (s8) data[1]);
  246. input_event(idev, EV_REL, REL_Y, (s8) data[2]);
  247. input_sync(idev);
  248. }
  249. static int ati_remote2_lookup(unsigned int hw_code)
  250. {
  251. int i;
  252. for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
  253. if (ati_remote2_key_table[i].hw_code == hw_code)
  254. return i;
  255. return -1;
  256. }
  257. static void ati_remote2_input_key(struct ati_remote2 *ar2)
  258. {
  259. struct input_dev *idev = ar2->idev;
  260. u8 *data = ar2->buf[1];
  261. int channel, mode, hw_code, index;
  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[1]->dev,
  268. "Unknown mode byte (%02x %02x %02x %02x)\n",
  269. data[3], data[2], data[1], data[0]);
  270. return;
  271. }
  272. hw_code = data[2];
  273. if (hw_code == 0x3f) {
  274. /*
  275. * For some incomprehensible reason the mouse pad generates
  276. * events which look identical to the events from the last
  277. * pressed mode key. Naturally we don't want to generate key
  278. * events for the mouse pad so we filter out any subsequent
  279. * events from the same mode key.
  280. */
  281. if (ar2->mode == mode)
  282. return;
  283. if (data[1] == 0)
  284. ar2->mode = mode;
  285. }
  286. if (!((1 << mode) & ar2->mode_mask))
  287. return;
  288. index = ati_remote2_lookup(hw_code);
  289. if (index < 0) {
  290. dev_err(&ar2->intf[1]->dev,
  291. "Unknown code byte (%02x %02x %02x %02x)\n",
  292. data[3], data[2], data[1], data[0]);
  293. return;
  294. }
  295. switch (data[1]) {
  296. case 0: /* release */
  297. break;
  298. case 1: /* press */
  299. ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
  300. break;
  301. case 2: /* repeat */
  302. /* No repeat for mouse buttons. */
  303. if (ar2->keycode[mode][index] == BTN_LEFT ||
  304. ar2->keycode[mode][index] == BTN_RIGHT)
  305. return;
  306. if (!time_after_eq(jiffies, ar2->jiffies))
  307. return;
  308. ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
  309. break;
  310. default:
  311. dev_err(&ar2->intf[1]->dev,
  312. "Unknown state byte (%02x %02x %02x %02x)\n",
  313. data[3], data[2], data[1], data[0]);
  314. return;
  315. }
  316. input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
  317. input_sync(idev);
  318. }
  319. static void ati_remote2_complete_mouse(struct urb *urb)
  320. {
  321. struct ati_remote2 *ar2 = urb->context;
  322. int r;
  323. switch (urb->status) {
  324. case 0:
  325. usb_mark_last_busy(ar2->udev);
  326. ati_remote2_input_mouse(ar2);
  327. break;
  328. case -ENOENT:
  329. case -EILSEQ:
  330. case -ECONNRESET:
  331. case -ESHUTDOWN:
  332. dev_dbg(&ar2->intf[0]->dev,
  333. "%s(): urb status = %d\n", __func__, urb->status);
  334. return;
  335. default:
  336. usb_mark_last_busy(ar2->udev);
  337. dev_err(&ar2->intf[0]->dev,
  338. "%s(): urb status = %d\n", __func__, urb->status);
  339. }
  340. r = usb_submit_urb(urb, GFP_ATOMIC);
  341. if (r)
  342. dev_err(&ar2->intf[0]->dev,
  343. "%s(): usb_submit_urb() = %d\n", __func__, r);
  344. }
  345. static void ati_remote2_complete_key(struct urb *urb)
  346. {
  347. struct ati_remote2 *ar2 = urb->context;
  348. int r;
  349. switch (urb->status) {
  350. case 0:
  351. usb_mark_last_busy(ar2->udev);
  352. ati_remote2_input_key(ar2);
  353. break;
  354. case -ENOENT:
  355. case -EILSEQ:
  356. case -ECONNRESET:
  357. case -ESHUTDOWN:
  358. dev_dbg(&ar2->intf[1]->dev,
  359. "%s(): urb status = %d\n", __func__, urb->status);
  360. return;
  361. default:
  362. usb_mark_last_busy(ar2->udev);
  363. dev_err(&ar2->intf[1]->dev,
  364. "%s(): urb status = %d\n", __func__, urb->status);
  365. }
  366. r = usb_submit_urb(urb, GFP_ATOMIC);
  367. if (r)
  368. dev_err(&ar2->intf[1]->dev,
  369. "%s(): usb_submit_urb() = %d\n", __func__, r);
  370. }
  371. static int ati_remote2_getkeycode(struct input_dev *idev,
  372. struct input_keymap_entry *ke)
  373. {
  374. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  375. unsigned int mode;
  376. int offset;
  377. unsigned int index;
  378. unsigned int scancode;
  379. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  380. index = ke->index;
  381. if (index >= ATI_REMOTE2_MODES *
  382. ARRAY_SIZE(ati_remote2_key_table))
  383. return -EINVAL;
  384. mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
  385. offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
  386. scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code;
  387. } else {
  388. if (input_scancode_to_scalar(ke, &scancode))
  389. return -EINVAL;
  390. mode = scancode >> 8;
  391. if (mode > ATI_REMOTE2_PC)
  392. return -EINVAL;
  393. offset = ati_remote2_lookup(scancode & 0xff);
  394. if (offset < 0)
  395. return -EINVAL;
  396. index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset;
  397. }
  398. ke->keycode = ar2->keycode[mode][offset];
  399. ke->len = sizeof(scancode);
  400. memcpy(&ke->scancode, &scancode, sizeof(scancode));
  401. ke->index = index;
  402. return 0;
  403. }
  404. static int ati_remote2_setkeycode(struct input_dev *idev,
  405. const struct input_keymap_entry *ke,
  406. unsigned int *old_keycode)
  407. {
  408. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  409. unsigned int mode;
  410. int offset;
  411. unsigned int index;
  412. unsigned int scancode;
  413. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  414. if (ke->index >= ATI_REMOTE2_MODES *
  415. ARRAY_SIZE(ati_remote2_key_table))
  416. return -EINVAL;
  417. mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
  418. offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
  419. } else {
  420. if (input_scancode_to_scalar(ke, &scancode))
  421. return -EINVAL;
  422. mode = scancode >> 8;
  423. if (mode > ATI_REMOTE2_PC)
  424. return -EINVAL;
  425. offset = ati_remote2_lookup(scancode & 0xff);
  426. if (offset < 0)
  427. return -EINVAL;
  428. }
  429. *old_keycode = ar2->keycode[mode][offset];
  430. ar2->keycode[mode][offset] = ke->keycode;
  431. __set_bit(ke->keycode, idev->keybit);
  432. for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
  433. for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
  434. if (ar2->keycode[mode][index] == *old_keycode)
  435. return 0;
  436. }
  437. }
  438. __clear_bit(*old_keycode, idev->keybit);
  439. return 0;
  440. }
  441. static int ati_remote2_input_init(struct ati_remote2 *ar2)
  442. {
  443. struct input_dev *idev;
  444. int index, mode, retval;
  445. idev = input_allocate_device();
  446. if (!idev)
  447. return -ENOMEM;
  448. ar2->idev = idev;
  449. input_set_drvdata(idev, ar2);
  450. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
  451. idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  452. BIT_MASK(BTN_RIGHT);
  453. idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  454. for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
  455. for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
  456. ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
  457. __set_bit(ar2->keycode[mode][index], idev->keybit);
  458. }
  459. }
  460. /* AUX1-AUX4 and PC generate the same scancode. */
  461. index = ati_remote2_lookup(0x3f);
  462. ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
  463. ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
  464. ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
  465. ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
  466. ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
  467. __set_bit(KEY_PROG1, idev->keybit);
  468. __set_bit(KEY_PROG2, idev->keybit);
  469. __set_bit(KEY_PROG3, idev->keybit);
  470. __set_bit(KEY_PROG4, idev->keybit);
  471. __set_bit(KEY_PC, idev->keybit);
  472. idev->rep[REP_DELAY] = 250;
  473. idev->rep[REP_PERIOD] = 33;
  474. idev->open = ati_remote2_open;
  475. idev->close = ati_remote2_close;
  476. idev->getkeycode = ati_remote2_getkeycode;
  477. idev->setkeycode = ati_remote2_setkeycode;
  478. idev->name = ar2->name;
  479. idev->phys = ar2->phys;
  480. usb_to_input_id(ar2->udev, &idev->id);
  481. idev->dev.parent = &ar2->udev->dev;
  482. retval = input_register_device(idev);
  483. if (retval)
  484. input_free_device(idev);
  485. return retval;
  486. }
  487. static int ati_remote2_urb_init(struct ati_remote2 *ar2)
  488. {
  489. struct usb_device *udev = ar2->udev;
  490. int i, pipe, maxp;
  491. for (i = 0; i < 2; i++) {
  492. ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
  493. if (!ar2->buf[i])
  494. return -ENOMEM;
  495. ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  496. if (!ar2->urb[i])
  497. return -ENOMEM;
  498. pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
  499. maxp = usb_maxpacket(udev, pipe);
  500. maxp = maxp > 4 ? 4 : maxp;
  501. usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
  502. i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
  503. ar2, ar2->ep[i]->bInterval);
  504. ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
  505. ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  506. }
  507. return 0;
  508. }
  509. static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
  510. {
  511. int i;
  512. for (i = 0; i < 2; i++) {
  513. usb_free_urb(ar2->urb[i]);
  514. usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
  515. }
  516. }
  517. static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
  518. {
  519. int r, i, channel;
  520. /*
  521. * Configure receiver to only accept input from remote "channel"
  522. * channel == 0 -> Accept input from any remote channel
  523. * channel == 1 -> Only accept input from remote channel 1
  524. * channel == 2 -> Only accept input from remote channel 2
  525. * ...
  526. * channel == 16 -> Only accept input from remote channel 16
  527. */
  528. channel = 0;
  529. for (i = 0; i < 16; i++) {
  530. if ((1 << i) & ch_mask) {
  531. if (!(~(1 << i) & ch_mask))
  532. channel = i + 1;
  533. break;
  534. }
  535. }
  536. r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
  537. 0x20,
  538. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  539. channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  540. if (r) {
  541. dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
  542. __func__, r);
  543. return r;
  544. }
  545. return 0;
  546. }
  547. static ssize_t ati_remote2_show_channel_mask(struct device *dev,
  548. struct device_attribute *attr,
  549. char *buf)
  550. {
  551. struct usb_device *udev = to_usb_device(dev);
  552. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  553. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  554. return sprintf(buf, "0x%04x\n", ar2->channel_mask);
  555. }
  556. static ssize_t ati_remote2_store_channel_mask(struct device *dev,
  557. struct device_attribute *attr,
  558. const char *buf, size_t count)
  559. {
  560. struct usb_device *udev = to_usb_device(dev);
  561. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  562. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  563. unsigned int mask;
  564. int r;
  565. r = kstrtouint(buf, 0, &mask);
  566. if (r)
  567. return r;
  568. if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
  569. return -EINVAL;
  570. r = usb_autopm_get_interface(ar2->intf[0]);
  571. if (r) {
  572. dev_err(&ar2->intf[0]->dev,
  573. "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
  574. return r;
  575. }
  576. scoped_guard(mutex, &ati_remote2_mutex) {
  577. if (mask != ar2->channel_mask) {
  578. r = ati_remote2_setup(ar2, mask);
  579. if (!r)
  580. ar2->channel_mask = mask;
  581. }
  582. }
  583. usb_autopm_put_interface(ar2->intf[0]);
  584. return r ? r : count;
  585. }
  586. static ssize_t ati_remote2_show_mode_mask(struct device *dev,
  587. struct device_attribute *attr,
  588. char *buf)
  589. {
  590. struct usb_device *udev = to_usb_device(dev);
  591. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  592. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  593. return sprintf(buf, "0x%02x\n", ar2->mode_mask);
  594. }
  595. static ssize_t ati_remote2_store_mode_mask(struct device *dev,
  596. struct device_attribute *attr,
  597. const char *buf, size_t count)
  598. {
  599. struct usb_device *udev = to_usb_device(dev);
  600. struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
  601. struct ati_remote2 *ar2 = usb_get_intfdata(intf);
  602. unsigned int mask;
  603. int err;
  604. err = kstrtouint(buf, 0, &mask);
  605. if (err)
  606. return err;
  607. if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
  608. return -EINVAL;
  609. ar2->mode_mask = mask;
  610. return count;
  611. }
  612. static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
  613. ati_remote2_store_channel_mask);
  614. static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
  615. ati_remote2_store_mode_mask);
  616. static struct attribute *ati_remote2_attrs[] = {
  617. &dev_attr_channel_mask.attr,
  618. &dev_attr_mode_mask.attr,
  619. NULL,
  620. };
  621. ATTRIBUTE_GROUPS(ati_remote2);
  622. static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
  623. {
  624. struct usb_device *udev = interface_to_usbdev(interface);
  625. struct usb_host_interface *alt = interface->cur_altsetting;
  626. struct ati_remote2 *ar2;
  627. int r;
  628. if (alt->desc.bInterfaceNumber)
  629. return -ENODEV;
  630. ar2 = kzalloc_obj(struct ati_remote2);
  631. if (!ar2)
  632. return -ENOMEM;
  633. ar2->udev = udev;
  634. /* Sanity check, first interface must have an endpoint */
  635. if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
  636. dev_err(&interface->dev,
  637. "%s(): interface 0 must have an endpoint\n", __func__);
  638. r = -ENODEV;
  639. goto fail1;
  640. }
  641. ar2->intf[0] = interface;
  642. ar2->ep[0] = &alt->endpoint[0].desc;
  643. /* Sanity check, the device must have two interfaces */
  644. ar2->intf[1] = usb_ifnum_to_if(udev, 1);
  645. if ((udev->actconfig->desc.bNumInterfaces < 2) || !ar2->intf[1]) {
  646. dev_err(&interface->dev, "%s(): need 2 interfaces, found %d\n",
  647. __func__, udev->actconfig->desc.bNumInterfaces);
  648. r = -ENODEV;
  649. goto fail1;
  650. }
  651. r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
  652. if (r)
  653. goto fail1;
  654. /* Sanity check, second interface must have an endpoint */
  655. alt = ar2->intf[1]->cur_altsetting;
  656. if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
  657. dev_err(&interface->dev,
  658. "%s(): interface 1 must have an endpoint\n", __func__);
  659. r = -ENODEV;
  660. goto fail2;
  661. }
  662. ar2->ep[1] = &alt->endpoint[0].desc;
  663. r = ati_remote2_urb_init(ar2);
  664. if (r)
  665. goto fail3;
  666. ar2->channel_mask = channel_mask;
  667. ar2->mode_mask = mode_mask;
  668. r = ati_remote2_setup(ar2, ar2->channel_mask);
  669. if (r)
  670. goto fail3;
  671. usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
  672. strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
  673. strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
  674. r = ati_remote2_input_init(ar2);
  675. if (r)
  676. goto fail3;
  677. usb_set_intfdata(interface, ar2);
  678. interface->needs_remote_wakeup = 1;
  679. return 0;
  680. fail3:
  681. ati_remote2_urb_cleanup(ar2);
  682. fail2:
  683. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  684. fail1:
  685. kfree(ar2);
  686. return r;
  687. }
  688. static void ati_remote2_disconnect(struct usb_interface *interface)
  689. {
  690. struct ati_remote2 *ar2;
  691. struct usb_host_interface *alt = interface->cur_altsetting;
  692. if (alt->desc.bInterfaceNumber)
  693. return;
  694. ar2 = usb_get_intfdata(interface);
  695. usb_set_intfdata(interface, NULL);
  696. input_unregister_device(ar2->idev);
  697. ati_remote2_urb_cleanup(ar2);
  698. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  699. kfree(ar2);
  700. }
  701. static int ati_remote2_suspend(struct usb_interface *interface,
  702. pm_message_t message)
  703. {
  704. struct ati_remote2 *ar2;
  705. struct usb_host_interface *alt = interface->cur_altsetting;
  706. if (alt->desc.bInterfaceNumber)
  707. return 0;
  708. ar2 = usb_get_intfdata(interface);
  709. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  710. guard(mutex)(&ati_remote2_mutex);
  711. if (ar2->flags & ATI_REMOTE2_OPENED)
  712. ati_remote2_kill_urbs(ar2);
  713. ar2->flags |= ATI_REMOTE2_SUSPENDED;
  714. return 0;
  715. }
  716. static int ati_remote2_resume(struct usb_interface *interface)
  717. {
  718. struct ati_remote2 *ar2;
  719. struct usb_host_interface *alt = interface->cur_altsetting;
  720. int r = 0;
  721. if (alt->desc.bInterfaceNumber)
  722. return 0;
  723. ar2 = usb_get_intfdata(interface);
  724. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  725. guard(mutex)(&ati_remote2_mutex);
  726. if (ar2->flags & ATI_REMOTE2_OPENED)
  727. r = ati_remote2_submit_urbs(ar2);
  728. if (!r)
  729. ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
  730. return r;
  731. }
  732. static int ati_remote2_reset_resume(struct usb_interface *interface)
  733. {
  734. struct ati_remote2 *ar2;
  735. struct usb_host_interface *alt = interface->cur_altsetting;
  736. int r = 0;
  737. if (alt->desc.bInterfaceNumber)
  738. return 0;
  739. ar2 = usb_get_intfdata(interface);
  740. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  741. guard(mutex)(&ati_remote2_mutex);
  742. r = ati_remote2_setup(ar2, ar2->channel_mask);
  743. if (r)
  744. return r;
  745. if (ar2->flags & ATI_REMOTE2_OPENED)
  746. r = ati_remote2_submit_urbs(ar2);
  747. if (!r)
  748. ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
  749. return r;
  750. }
  751. static int ati_remote2_pre_reset(struct usb_interface *interface)
  752. {
  753. struct ati_remote2 *ar2;
  754. struct usb_host_interface *alt = interface->cur_altsetting;
  755. if (alt->desc.bInterfaceNumber)
  756. return 0;
  757. ar2 = usb_get_intfdata(interface);
  758. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  759. mutex_lock(&ati_remote2_mutex);
  760. if (ar2->flags == ATI_REMOTE2_OPENED)
  761. ati_remote2_kill_urbs(ar2);
  762. return 0;
  763. }
  764. static int ati_remote2_post_reset(struct usb_interface *interface)
  765. {
  766. struct ati_remote2 *ar2;
  767. struct usb_host_interface *alt = interface->cur_altsetting;
  768. int r = 0;
  769. if (alt->desc.bInterfaceNumber)
  770. return 0;
  771. ar2 = usb_get_intfdata(interface);
  772. dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
  773. if (ar2->flags == ATI_REMOTE2_OPENED)
  774. r = ati_remote2_submit_urbs(ar2);
  775. mutex_unlock(&ati_remote2_mutex);
  776. return r;
  777. }
  778. static struct usb_driver ati_remote2_driver = {
  779. .name = "ati_remote2",
  780. .probe = ati_remote2_probe,
  781. .disconnect = ati_remote2_disconnect,
  782. .dev_groups = ati_remote2_groups,
  783. .id_table = ati_remote2_id_table,
  784. .suspend = ati_remote2_suspend,
  785. .resume = ati_remote2_resume,
  786. .reset_resume = ati_remote2_reset_resume,
  787. .pre_reset = ati_remote2_pre_reset,
  788. .post_reset = ati_remote2_post_reset,
  789. .supports_autosuspend = 1,
  790. };
  791. module_usb_driver(ati_remote2_driver);