hid-sony.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. return -1;
  326. }
  327. /*
  328. * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
  329. * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
  330. * so we need to override that forcing HID Output Reports on the Control EP.
  331. *
  332. * There is also another issue about HID Output Reports via USB, the Sixaxis
  333. * does not want the report_id as part of the data packet, so we have to
  334. * discard buf[0] when sending the actual control message, even for numbered
  335. * reports, humpf!
  336. */
  337. static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
  338. size_t count, unsigned char report_type)
  339. {
  340. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  341. struct usb_device *dev = interface_to_usbdev(intf);
  342. struct usb_host_interface *interface = intf->cur_altsetting;
  343. int report_id = buf[0];
  344. int ret;
  345. if (report_type == HID_OUTPUT_REPORT) {
  346. /* Don't send the Report ID */
  347. buf++;
  348. count--;
  349. }
  350. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  351. HID_REQ_SET_REPORT,
  352. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  353. ((report_type + 1) << 8) | report_id,
  354. interface->desc.bInterfaceNumber, buf, count,
  355. USB_CTRL_SET_TIMEOUT);
  356. /* Count also the Report ID, in case of an Output report. */
  357. if (ret > 0 && report_type == HID_OUTPUT_REPORT)
  358. ret++;
  359. return ret;
  360. }
  361. /*
  362. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  363. * to "operational". Without this, the ps3 controller will not report any
  364. * events.
  365. */
  366. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  367. {
  368. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  369. struct usb_device *dev = interface_to_usbdev(intf);
  370. __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  371. int ret;
  372. char *buf = kmalloc(18, GFP_KERNEL);
  373. if (!buf)
  374. return -ENOMEM;
  375. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  376. HID_REQ_GET_REPORT,
  377. USB_DIR_IN | USB_TYPE_CLASS |
  378. USB_RECIP_INTERFACE,
  379. (3 << 8) | 0xf2, ifnum, buf, 17,
  380. USB_CTRL_GET_TIMEOUT);
  381. if (ret < 0)
  382. hid_err(hdev, "can't set operational mode\n");
  383. kfree(buf);
  384. return ret;
  385. }
  386. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  387. {
  388. unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  389. return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  390. }
  391. static void buzz_set_leds(struct hid_device *hdev, int leds)
  392. {
  393. struct list_head *report_list =
  394. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  395. struct hid_report *report = list_entry(report_list->next,
  396. struct hid_report, list);
  397. __s32 *value = report->field[0]->value;
  398. value[0] = 0x00;
  399. value[1] = (leds & 1) ? 0xff : 0x00;
  400. value[2] = (leds & 2) ? 0xff : 0x00;
  401. value[3] = (leds & 4) ? 0xff : 0x00;
  402. value[4] = (leds & 8) ? 0xff : 0x00;
  403. value[5] = 0x00;
  404. value[6] = 0x00;
  405. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  406. }
  407. static void buzz_led_set_brightness(struct led_classdev *led,
  408. enum led_brightness value)
  409. {
  410. struct device *dev = led->dev->parent;
  411. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  412. struct sony_sc *drv_data;
  413. struct buzz_extra *buzz;
  414. int n;
  415. drv_data = hid_get_drvdata(hdev);
  416. if (!drv_data || !drv_data->extra) {
  417. hid_err(hdev, "No device data\n");
  418. return;
  419. }
  420. buzz = drv_data->extra;
  421. for (n = 0; n < 4; n++) {
  422. if (led == buzz->leds[n]) {
  423. int on = !! (buzz->led_state & (1 << n));
  424. if (value == LED_OFF && on) {
  425. buzz->led_state &= ~(1 << n);
  426. buzz_set_leds(hdev, buzz->led_state);
  427. } else if (value != LED_OFF && !on) {
  428. buzz->led_state |= (1 << n);
  429. buzz_set_leds(hdev, buzz->led_state);
  430. }
  431. break;
  432. }
  433. }
  434. }
  435. static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
  436. {
  437. struct device *dev = led->dev->parent;
  438. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  439. struct sony_sc *drv_data;
  440. struct buzz_extra *buzz;
  441. int n;
  442. int on = 0;
  443. drv_data = hid_get_drvdata(hdev);
  444. if (!drv_data || !drv_data->extra) {
  445. hid_err(hdev, "No device data\n");
  446. return LED_OFF;
  447. }
  448. buzz = drv_data->extra;
  449. for (n = 0; n < 4; n++) {
  450. if (led == buzz->leds[n]) {
  451. on = !! (buzz->led_state & (1 << n));
  452. break;
  453. }
  454. }
  455. return on ? LED_FULL : LED_OFF;
  456. }
  457. static int buzz_init(struct hid_device *hdev)
  458. {
  459. struct sony_sc *drv_data;
  460. struct buzz_extra *buzz;
  461. int n, ret = 0;
  462. struct led_classdev *led;
  463. size_t name_sz;
  464. char *name;
  465. drv_data = hid_get_drvdata(hdev);
  466. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  467. buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
  468. if (!buzz) {
  469. hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
  470. return -ENOMEM;
  471. }
  472. drv_data->extra = buzz;
  473. /* Clear LEDs as we have no way of reading their initial state. This is
  474. * only relevant if the driver is loaded after somebody actively set the
  475. * LEDs to on */
  476. buzz_set_leds(hdev, 0x00);
  477. name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
  478. for (n = 0; n < 4; n++) {
  479. led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  480. if (!led) {
  481. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  482. goto error_leds;
  483. }
  484. name = (void *)(&led[1]);
  485. snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
  486. led->name = name;
  487. led->brightness = 0;
  488. led->max_brightness = 1;
  489. led->brightness_get = buzz_led_get_brightness;
  490. led->brightness_set = buzz_led_set_brightness;
  491. if (led_classdev_register(&hdev->dev, led)) {
  492. hid_err(hdev, "Failed to register LED %d\n", n);
  493. kfree(led);
  494. goto error_leds;
  495. }
  496. buzz->leds[n] = led;
  497. }
  498. return ret;
  499. error_leds:
  500. for (n = 0; n < 4; n++) {
  501. led = buzz->leds[n];
  502. buzz->leds[n] = NULL;
  503. if (!led)
  504. continue;
  505. led_classdev_unregister(led);
  506. kfree(led);
  507. }
  508. kfree(drv_data->extra);
  509. drv_data->extra = NULL;
  510. return ret;
  511. }
  512. static void buzz_remove(struct hid_device *hdev)
  513. {
  514. struct sony_sc *drv_data;
  515. struct buzz_extra *buzz;
  516. struct led_classdev *led;
  517. int n;
  518. drv_data = hid_get_drvdata(hdev);
  519. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  520. buzz = drv_data->extra;
  521. for (n = 0; n < 4; n++) {
  522. led = buzz->leds[n];
  523. buzz->leds[n] = NULL;
  524. if (!led)
  525. continue;
  526. led_classdev_unregister(led);
  527. kfree(led);
  528. }
  529. kfree(drv_data->extra);
  530. drv_data->extra = NULL;
  531. }
  532. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  533. {
  534. int ret;
  535. unsigned long quirks = id->driver_data;
  536. struct sony_sc *sc;
  537. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  538. sc = kzalloc(sizeof(*sc), GFP_KERNEL);
  539. if (sc == NULL) {
  540. hid_err(hdev, "can't alloc sony descriptor\n");
  541. return -ENOMEM;
  542. }
  543. sc->quirks = quirks;
  544. hid_set_drvdata(hdev, sc);
  545. ret = hid_parse(hdev);
  546. if (ret) {
  547. hid_err(hdev, "parse failed\n");
  548. goto err_free;
  549. }
  550. if (sc->quirks & VAIO_RDESC_CONSTANT)
  551. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  552. else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
  553. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  554. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  555. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  556. ret = hid_hw_start(hdev, connect_mask);
  557. if (ret) {
  558. hid_err(hdev, "hw start failed\n");
  559. goto err_free;
  560. }
  561. if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  562. hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
  563. ret = sixaxis_set_operational_usb(hdev);
  564. }
  565. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  566. ret = sixaxis_set_operational_bt(hdev);
  567. else if (sc->quirks & BUZZ_CONTROLLER)
  568. ret = buzz_init(hdev);
  569. else
  570. ret = 0;
  571. if (ret < 0)
  572. goto err_stop;
  573. return 0;
  574. err_stop:
  575. hid_hw_stop(hdev);
  576. err_free:
  577. kfree(sc);
  578. return ret;
  579. }
  580. static void sony_remove(struct hid_device *hdev)
  581. {
  582. struct sony_sc *sc = hid_get_drvdata(hdev);
  583. if (sc->quirks & BUZZ_CONTROLLER)
  584. buzz_remove(hdev);
  585. hid_hw_stop(hdev);
  586. kfree(sc);
  587. }
  588. static const struct hid_device_id sony_devices[] = {
  589. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  590. .driver_data = SIXAXIS_CONTROLLER_USB },
  591. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  592. .driver_data = SIXAXIS_CONTROLLER_USB },
  593. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  594. .driver_data = SIXAXIS_CONTROLLER_BT },
  595. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  596. .driver_data = VAIO_RDESC_CONSTANT },
  597. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  598. .driver_data = VAIO_RDESC_CONSTANT },
  599. /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  600. * Logitech joystick from the device descriptor. */
  601. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  602. .driver_data = BUZZ_CONTROLLER },
  603. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  604. .driver_data = BUZZ_CONTROLLER },
  605. /* PS3 BD Remote Control */
  606. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  607. .driver_data = PS3REMOTE },
  608. /* Logitech Harmony Adapter for PS3 */
  609. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  610. .driver_data = PS3REMOTE },
  611. { }
  612. };
  613. MODULE_DEVICE_TABLE(hid, sony_devices);
  614. static struct hid_driver sony_driver = {
  615. .name = "sony",
  616. .id_table = sony_devices,
  617. .input_mapping = sony_mapping,
  618. .probe = sony_probe,
  619. .remove = sony_remove,
  620. .report_fixup = sony_report_fixup,
  621. .raw_event = sony_raw_event
  622. };
  623. module_hid_driver(sony_driver);
  624. MODULE_LICENSE("GPL");