hid-sony.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * HID driver for Sony / PS2 / PS3 BD devices.
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. * Copyright (c) 2008 Jiri Slaby
  8. * Copyright (c) 2012 David Dillow <dave@thedillows.org>
  9. * Copyright (c) 2006-2013 Jiri Kosina
  10. * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. */
  18. /* NOTE: in order for the Sony PS3 BD Remote Control to be found by
  19. * a Bluetooth host, the key combination Start+Enter has to be kept pressed
  20. * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
  21. *
  22. * There will be no PIN request from the device.
  23. */
  24. #include <linux/device.h>
  25. #include <linux/hid.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb.h>
  29. #include <linux/leds.h>
  30. #include "hid-ids.h"
  31. #define VAIO_RDESC_CONSTANT (1 << 0)
  32. #define SIXAXIS_CONTROLLER_USB (1 << 1)
  33. #define SIXAXIS_CONTROLLER_BT (1 << 2)
  34. #define BUZZ_CONTROLLER (1 << 3)
  35. #define PS3REMOTE (1 << 4)
  36. static const u8 sixaxis_rdesc_fixup[] = {
  37. 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
  38. 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
  39. 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
  40. };
  41. static const u8 sixaxis_rdesc_fixup2[] = {
  42. 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
  43. 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
  44. 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
  45. 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
  46. 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
  47. 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
  48. 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
  49. 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
  50. 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
  51. 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
  52. 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
  53. 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
  54. 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
  55. 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
  56. 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
  57. 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
  58. 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
  59. 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
  60. 0xb1, 0x02, 0xc0, 0xc0,
  61. };
  62. static __u8 ps3remote_rdesc[] = {
  63. 0x05, 0x01, /* GUsagePage Generic Desktop */
  64. 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
  65. 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
  66. /* Use collection 1 for joypad buttons */
  67. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  68. /* Ignore the 1st byte, maybe it is used for a controller
  69. * number but it's not needed for correct operation */
  70. 0x75, 0x08, /* GReportSize 0x08 [8] */
  71. 0x95, 0x01, /* GReportCount 0x01 [1] */
  72. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  73. /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
  74. * buttons multiple keypresses are allowed */
  75. 0x05, 0x09, /* GUsagePage Button */
  76. 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
  77. 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
  78. 0x14, /* GLogicalMinimum [0] */
  79. 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
  80. 0x75, 0x01, /* GReportSize 0x01 [1] */
  81. 0x95, 0x18, /* GReportCount 0x18 [24] */
  82. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  83. 0xC0, /* MEndCollection */
  84. /* Use collection 2 for remote control buttons */
  85. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  86. /* 5th byte is used for remote control buttons */
  87. 0x05, 0x09, /* GUsagePage Button */
  88. 0x18, /* LUsageMinimum [No button pressed] */
  89. 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
  90. 0x14, /* GLogicalMinimum [0] */
  91. 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
  92. 0x75, 0x08, /* GReportSize 0x08 [8] */
  93. 0x95, 0x01, /* GReportCount 0x01 [1] */
  94. 0x80, /* MInput */
  95. /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
  96. * 0xff and 11th is for press indication */
  97. 0x75, 0x08, /* GReportSize 0x08 [8] */
  98. 0x95, 0x06, /* GReportCount 0x06 [6] */
  99. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  100. /* 12th byte is for battery strength */
  101. 0x05, 0x06, /* GUsagePage Generic Device Controls */
  102. 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
  103. 0x14, /* GLogicalMinimum [0] */
  104. 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
  105. 0x75, 0x08, /* GReportSize 0x08 [8] */
  106. 0x95, 0x01, /* GReportCount 0x01 [1] */
  107. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  108. 0xC0, /* MEndCollection */
  109. 0xC0 /* MEndCollection [Game Pad] */
  110. };
  111. static const unsigned int ps3remote_keymap_joypad_buttons[] = {
  112. [0x01] = KEY_SELECT,
  113. [0x02] = BTN_THUMBL, /* L3 */
  114. [0x03] = BTN_THUMBR, /* R3 */
  115. [0x04] = BTN_START,
  116. [0x05] = KEY_UP,
  117. [0x06] = KEY_RIGHT,
  118. [0x07] = KEY_DOWN,
  119. [0x08] = KEY_LEFT,
  120. [0x09] = BTN_TL2, /* L2 */
  121. [0x0a] = BTN_TR2, /* R2 */
  122. [0x0b] = BTN_TL, /* L1 */
  123. [0x0c] = BTN_TR, /* R1 */
  124. [0x0d] = KEY_OPTION, /* options/triangle */
  125. [0x0e] = KEY_BACK, /* back/circle */
  126. [0x0f] = BTN_0, /* cross */
  127. [0x10] = KEY_SCREEN, /* view/square */
  128. [0x11] = KEY_HOMEPAGE, /* PS button */
  129. [0x14] = KEY_ENTER,
  130. };
  131. static const unsigned int ps3remote_keymap_remote_buttons[] = {
  132. [0x00] = KEY_1,
  133. [0x01] = KEY_2,
  134. [0x02] = KEY_3,
  135. [0x03] = KEY_4,
  136. [0x04] = KEY_5,
  137. [0x05] = KEY_6,
  138. [0x06] = KEY_7,
  139. [0x07] = KEY_8,
  140. [0x08] = KEY_9,
  141. [0x09] = KEY_0,
  142. [0x0e] = KEY_ESC, /* return */
  143. [0x0f] = KEY_CLEAR,
  144. [0x16] = KEY_EJECTCD,
  145. [0x1a] = KEY_MENU, /* top menu */
  146. [0x28] = KEY_TIME,
  147. [0x30] = KEY_PREVIOUS,
  148. [0x31] = KEY_NEXT,
  149. [0x32] = KEY_PLAY,
  150. [0x33] = KEY_REWIND, /* scan back */
  151. [0x34] = KEY_FORWARD, /* scan forward */
  152. [0x38] = KEY_STOP,
  153. [0x39] = KEY_PAUSE,
  154. [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
  155. [0x60] = KEY_FRAMEBACK, /* slow/step back */
  156. [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
  157. [0x63] = KEY_SUBTITLE,
  158. [0x64] = KEY_AUDIO,
  159. [0x65] = KEY_ANGLE,
  160. [0x70] = KEY_INFO, /* display */
  161. [0x80] = KEY_BLUE,
  162. [0x81] = KEY_RED,
  163. [0x82] = KEY_GREEN,
  164. [0x83] = KEY_YELLOW,
  165. };
  166. static const unsigned int buzz_keymap[] = {
  167. /* The controller has 4 remote buzzers, each with one LED and 5
  168. * buttons.
  169. *
  170. * We use the mapping chosen by the controller, which is:
  171. *
  172. * Key Offset
  173. * -------------------
  174. * Buzz 1
  175. * Blue 5
  176. * Orange 4
  177. * Green 3
  178. * Yellow 2
  179. *
  180. * So, for example, the orange button on the third buzzer is mapped to
  181. * BTN_TRIGGER_HAPPY14
  182. */
  183. [ 1] = BTN_TRIGGER_HAPPY1,
  184. [ 2] = BTN_TRIGGER_HAPPY2,
  185. [ 3] = BTN_TRIGGER_HAPPY3,
  186. [ 4] = BTN_TRIGGER_HAPPY4,
  187. [ 5] = BTN_TRIGGER_HAPPY5,
  188. [ 6] = BTN_TRIGGER_HAPPY6,
  189. [ 7] = BTN_TRIGGER_HAPPY7,
  190. [ 8] = BTN_TRIGGER_HAPPY8,
  191. [ 9] = BTN_TRIGGER_HAPPY9,
  192. [10] = BTN_TRIGGER_HAPPY10,
  193. [11] = BTN_TRIGGER_HAPPY11,
  194. [12] = BTN_TRIGGER_HAPPY12,
  195. [13] = BTN_TRIGGER_HAPPY13,
  196. [14] = BTN_TRIGGER_HAPPY14,
  197. [15] = BTN_TRIGGER_HAPPY15,
  198. [16] = BTN_TRIGGER_HAPPY16,
  199. [17] = BTN_TRIGGER_HAPPY17,
  200. [18] = BTN_TRIGGER_HAPPY18,
  201. [19] = BTN_TRIGGER_HAPPY19,
  202. [20] = BTN_TRIGGER_HAPPY20,
  203. };
  204. struct sony_sc {
  205. unsigned long quirks;
  206. void *extra;
  207. };
  208. struct buzz_extra {
  209. int led_state;
  210. struct led_classdev *leds[4];
  211. };
  212. static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
  213. unsigned int *rsize)
  214. {
  215. *rsize = sizeof(ps3remote_rdesc);
  216. return ps3remote_rdesc;
  217. }
  218. static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
  219. struct hid_field *field, struct hid_usage *usage,
  220. unsigned long **bit, int *max)
  221. {
  222. unsigned int key = usage->hid & HID_USAGE;
  223. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  224. return -1;
  225. switch (usage->collection_index) {
  226. case 1:
  227. if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
  228. return -1;
  229. key = ps3remote_keymap_joypad_buttons[key];
  230. if (!key)
  231. return -1;
  232. break;
  233. case 2:
  234. if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
  235. return -1;
  236. key = ps3remote_keymap_remote_buttons[key];
  237. if (!key)
  238. return -1;
  239. break;
  240. default:
  241. return -1;
  242. }
  243. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  244. return 1;
  245. }
  246. /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
  247. static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  248. unsigned int *rsize)
  249. {
  250. struct sony_sc *sc = hid_get_drvdata(hdev);
  251. /*
  252. * Some Sony RF receivers wrongly declare the mouse pointer as a
  253. * a constant non-data variable.
  254. */
  255. if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  256. /* usage page: generic desktop controls */
  257. /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  258. /* usage: mouse */
  259. rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  260. /* input (usage page for x,y axes): constant, variable, relative */
  261. rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  262. hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  263. /* input: data, variable, relative */
  264. rdesc[55] = 0x06;
  265. }
  266. /* The HID descriptor exposed over BT has a trailing zero byte */
  267. if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
  268. ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
  269. rdesc[83] == 0x75) {
  270. hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
  271. memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
  272. sizeof(sixaxis_rdesc_fixup));
  273. } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
  274. *rsize > sizeof(sixaxis_rdesc_fixup2)) {
  275. hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
  276. *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
  277. *rsize = sizeof(sixaxis_rdesc_fixup2);
  278. memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
  279. }
  280. if (sc->quirks & PS3REMOTE)
  281. return ps3remote_fixup(hdev, rdesc, rsize);
  282. return rdesc;
  283. }
  284. static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  285. __u8 *rd, int size)
  286. {
  287. struct sony_sc *sc = hid_get_drvdata(hdev);
  288. /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
  289. * has to be BYTE_SWAPPED before passing up to joystick interface
  290. */
  291. if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
  292. rd[0] == 0x01 && size == 49) {
  293. swap(rd[41], rd[42]);
  294. swap(rd[43], rd[44]);
  295. swap(rd[45], rd[46]);
  296. swap(rd[47], rd[48]);
  297. }
  298. return 0;
  299. }
  300. static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
  301. struct hid_field *field, struct hid_usage *usage,
  302. unsigned long **bit, int *max)
  303. {
  304. struct sony_sc *sc = hid_get_drvdata(hdev);
  305. if (sc->quirks & BUZZ_CONTROLLER) {
  306. unsigned int key = usage->hid & HID_USAGE;
  307. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  308. return -1;
  309. switch (usage->collection_index) {
  310. case 1:
  311. if (key >= ARRAY_SIZE(buzz_keymap))
  312. return -1;
  313. key = buzz_keymap[key];
  314. if (!key)
  315. return -1;
  316. break;
  317. default:
  318. return -1;
  319. }
  320. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  321. return 1;
  322. }
  323. if (sc->quirks & PS3REMOTE)
  324. return ps3remote_mapping(hdev, hi, field, usage, bit, max);
  325. /* Let hid-core decide for the others */
  326. return 0;
  327. }
  328. /*
  329. * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
  330. * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
  331. * so we need to override that forcing HID Output Reports on the Control EP.
  332. *
  333. * There is also another issue about HID Output Reports via USB, the Sixaxis
  334. * does not want the report_id as part of the data packet, so we have to
  335. * discard buf[0] when sending the actual control message, even for numbered
  336. * reports, humpf!
  337. */
  338. static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
  339. size_t count, unsigned char report_type)
  340. {
  341. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  342. struct usb_device *dev = interface_to_usbdev(intf);
  343. struct usb_host_interface *interface = intf->cur_altsetting;
  344. int report_id = buf[0];
  345. int ret;
  346. if (report_type == HID_OUTPUT_REPORT) {
  347. /* Don't send the Report ID */
  348. buf++;
  349. count--;
  350. }
  351. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  352. HID_REQ_SET_REPORT,
  353. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  354. ((report_type + 1) << 8) | report_id,
  355. interface->desc.bInterfaceNumber, buf, count,
  356. USB_CTRL_SET_TIMEOUT);
  357. /* Count also the Report ID, in case of an Output report. */
  358. if (ret > 0 && report_type == HID_OUTPUT_REPORT)
  359. ret++;
  360. return ret;
  361. }
  362. /*
  363. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  364. * to "operational". Without this, the ps3 controller will not report any
  365. * events.
  366. */
  367. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  368. {
  369. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  370. struct usb_device *dev = interface_to_usbdev(intf);
  371. __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  372. int ret;
  373. char *buf = kmalloc(18, GFP_KERNEL);
  374. if (!buf)
  375. return -ENOMEM;
  376. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  377. HID_REQ_GET_REPORT,
  378. USB_DIR_IN | USB_TYPE_CLASS |
  379. USB_RECIP_INTERFACE,
  380. (3 << 8) | 0xf2, ifnum, buf, 17,
  381. USB_CTRL_GET_TIMEOUT);
  382. if (ret < 0)
  383. hid_err(hdev, "can't set operational mode\n");
  384. kfree(buf);
  385. return ret;
  386. }
  387. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  388. {
  389. unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  390. return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  391. }
  392. static void buzz_set_leds(struct hid_device *hdev, int leds)
  393. {
  394. struct list_head *report_list =
  395. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  396. struct hid_report *report = list_entry(report_list->next,
  397. struct hid_report, list);
  398. __s32 *value = report->field[0]->value;
  399. value[0] = 0x00;
  400. value[1] = (leds & 1) ? 0xff : 0x00;
  401. value[2] = (leds & 2) ? 0xff : 0x00;
  402. value[3] = (leds & 4) ? 0xff : 0x00;
  403. value[4] = (leds & 8) ? 0xff : 0x00;
  404. value[5] = 0x00;
  405. value[6] = 0x00;
  406. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  407. }
  408. static void buzz_led_set_brightness(struct led_classdev *led,
  409. enum led_brightness value)
  410. {
  411. struct device *dev = led->dev->parent;
  412. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  413. struct sony_sc *drv_data;
  414. struct buzz_extra *buzz;
  415. int n;
  416. drv_data = hid_get_drvdata(hdev);
  417. if (!drv_data || !drv_data->extra) {
  418. hid_err(hdev, "No device data\n");
  419. return;
  420. }
  421. buzz = drv_data->extra;
  422. for (n = 0; n < 4; n++) {
  423. if (led == buzz->leds[n]) {
  424. int on = !! (buzz->led_state & (1 << n));
  425. if (value == LED_OFF && on) {
  426. buzz->led_state &= ~(1 << n);
  427. buzz_set_leds(hdev, buzz->led_state);
  428. } else if (value != LED_OFF && !on) {
  429. buzz->led_state |= (1 << n);
  430. buzz_set_leds(hdev, buzz->led_state);
  431. }
  432. break;
  433. }
  434. }
  435. }
  436. static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
  437. {
  438. struct device *dev = led->dev->parent;
  439. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  440. struct sony_sc *drv_data;
  441. struct buzz_extra *buzz;
  442. int n;
  443. int on = 0;
  444. drv_data = hid_get_drvdata(hdev);
  445. if (!drv_data || !drv_data->extra) {
  446. hid_err(hdev, "No device data\n");
  447. return LED_OFF;
  448. }
  449. buzz = drv_data->extra;
  450. for (n = 0; n < 4; n++) {
  451. if (led == buzz->leds[n]) {
  452. on = !! (buzz->led_state & (1 << n));
  453. break;
  454. }
  455. }
  456. return on ? LED_FULL : LED_OFF;
  457. }
  458. static int buzz_init(struct hid_device *hdev)
  459. {
  460. struct sony_sc *drv_data;
  461. struct buzz_extra *buzz;
  462. int n, ret = 0;
  463. struct led_classdev *led;
  464. size_t name_sz;
  465. char *name;
  466. drv_data = hid_get_drvdata(hdev);
  467. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  468. buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
  469. if (!buzz) {
  470. hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
  471. return -ENOMEM;
  472. }
  473. drv_data->extra = buzz;
  474. /* Clear LEDs as we have no way of reading their initial state. This is
  475. * only relevant if the driver is loaded after somebody actively set the
  476. * LEDs to on */
  477. buzz_set_leds(hdev, 0x00);
  478. name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
  479. for (n = 0; n < 4; n++) {
  480. led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  481. if (!led) {
  482. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  483. goto error_leds;
  484. }
  485. name = (void *)(&led[1]);
  486. snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
  487. led->name = name;
  488. led->brightness = 0;
  489. led->max_brightness = 1;
  490. led->brightness_get = buzz_led_get_brightness;
  491. led->brightness_set = buzz_led_set_brightness;
  492. if (led_classdev_register(&hdev->dev, led)) {
  493. hid_err(hdev, "Failed to register LED %d\n", n);
  494. kfree(led);
  495. goto error_leds;
  496. }
  497. buzz->leds[n] = led;
  498. }
  499. return ret;
  500. error_leds:
  501. for (n = 0; n < 4; n++) {
  502. led = buzz->leds[n];
  503. buzz->leds[n] = NULL;
  504. if (!led)
  505. continue;
  506. led_classdev_unregister(led);
  507. kfree(led);
  508. }
  509. kfree(drv_data->extra);
  510. drv_data->extra = NULL;
  511. return ret;
  512. }
  513. static void buzz_remove(struct hid_device *hdev)
  514. {
  515. struct sony_sc *drv_data;
  516. struct buzz_extra *buzz;
  517. struct led_classdev *led;
  518. int n;
  519. drv_data = hid_get_drvdata(hdev);
  520. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  521. buzz = drv_data->extra;
  522. for (n = 0; n < 4; n++) {
  523. led = buzz->leds[n];
  524. buzz->leds[n] = NULL;
  525. if (!led)
  526. continue;
  527. led_classdev_unregister(led);
  528. kfree(led);
  529. }
  530. kfree(drv_data->extra);
  531. drv_data->extra = NULL;
  532. }
  533. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  534. {
  535. int ret;
  536. unsigned long quirks = id->driver_data;
  537. struct sony_sc *sc;
  538. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  539. sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
  540. if (sc == NULL) {
  541. hid_err(hdev, "can't alloc sony descriptor\n");
  542. return -ENOMEM;
  543. }
  544. sc->quirks = quirks;
  545. hid_set_drvdata(hdev, sc);
  546. ret = hid_parse(hdev);
  547. if (ret) {
  548. hid_err(hdev, "parse failed\n");
  549. return ret;
  550. }
  551. if (sc->quirks & VAIO_RDESC_CONSTANT)
  552. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  553. else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
  554. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  555. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  556. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  557. ret = hid_hw_start(hdev, connect_mask);
  558. if (ret) {
  559. hid_err(hdev, "hw start failed\n");
  560. return ret;
  561. }
  562. if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  563. hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
  564. ret = sixaxis_set_operational_usb(hdev);
  565. }
  566. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  567. ret = sixaxis_set_operational_bt(hdev);
  568. else if (sc->quirks & BUZZ_CONTROLLER)
  569. ret = buzz_init(hdev);
  570. else
  571. ret = 0;
  572. if (ret < 0)
  573. goto err_stop;
  574. return 0;
  575. err_stop:
  576. hid_hw_stop(hdev);
  577. return ret;
  578. }
  579. static void sony_remove(struct hid_device *hdev)
  580. {
  581. struct sony_sc *sc = hid_get_drvdata(hdev);
  582. if (sc->quirks & BUZZ_CONTROLLER)
  583. buzz_remove(hdev);
  584. hid_hw_stop(hdev);
  585. }
  586. static const struct hid_device_id sony_devices[] = {
  587. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  588. .driver_data = SIXAXIS_CONTROLLER_USB },
  589. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  590. .driver_data = SIXAXIS_CONTROLLER_USB },
  591. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  592. .driver_data = SIXAXIS_CONTROLLER_BT },
  593. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  594. .driver_data = VAIO_RDESC_CONSTANT },
  595. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  596. .driver_data = VAIO_RDESC_CONSTANT },
  597. /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  598. * Logitech joystick from the device descriptor. */
  599. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  600. .driver_data = BUZZ_CONTROLLER },
  601. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  602. .driver_data = BUZZ_CONTROLLER },
  603. /* PS3 BD Remote Control */
  604. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  605. .driver_data = PS3REMOTE },
  606. /* Logitech Harmony Adapter for PS3 */
  607. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  608. .driver_data = PS3REMOTE },
  609. { }
  610. };
  611. MODULE_DEVICE_TABLE(hid, sony_devices);
  612. static struct hid_driver sony_driver = {
  613. .name = "sony",
  614. .id_table = sony_devices,
  615. .input_mapping = sony_mapping,
  616. .probe = sony_probe,
  617. .remove = sony_remove,
  618. .report_fixup = sony_report_fixup,
  619. .raw_event = sony_raw_event
  620. };
  621. module_hid_driver(sony_driver);
  622. MODULE_LICENSE("GPL");