hid-sony.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. #ifdef CONFIG_SONY_FF
  207. struct work_struct rumble_worker;
  208. struct hid_device *hdev;
  209. __u8 left;
  210. __u8 right;
  211. #endif
  212. void *extra;
  213. };
  214. struct buzz_extra {
  215. int led_state;
  216. struct led_classdev *leds[4];
  217. };
  218. static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
  219. unsigned int *rsize)
  220. {
  221. *rsize = sizeof(ps3remote_rdesc);
  222. return ps3remote_rdesc;
  223. }
  224. static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
  225. struct hid_field *field, struct hid_usage *usage,
  226. unsigned long **bit, int *max)
  227. {
  228. unsigned int key = usage->hid & HID_USAGE;
  229. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  230. return -1;
  231. switch (usage->collection_index) {
  232. case 1:
  233. if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
  234. return -1;
  235. key = ps3remote_keymap_joypad_buttons[key];
  236. if (!key)
  237. return -1;
  238. break;
  239. case 2:
  240. if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
  241. return -1;
  242. key = ps3remote_keymap_remote_buttons[key];
  243. if (!key)
  244. return -1;
  245. break;
  246. default:
  247. return -1;
  248. }
  249. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  250. return 1;
  251. }
  252. /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
  253. static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  254. unsigned int *rsize)
  255. {
  256. struct sony_sc *sc = hid_get_drvdata(hdev);
  257. /*
  258. * Some Sony RF receivers wrongly declare the mouse pointer as a
  259. * a constant non-data variable.
  260. */
  261. if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  262. /* usage page: generic desktop controls */
  263. /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  264. /* usage: mouse */
  265. rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  266. /* input (usage page for x,y axes): constant, variable, relative */
  267. rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  268. hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  269. /* input: data, variable, relative */
  270. rdesc[55] = 0x06;
  271. }
  272. /* The HID descriptor exposed over BT has a trailing zero byte */
  273. if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
  274. ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
  275. rdesc[83] == 0x75) {
  276. hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
  277. memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
  278. sizeof(sixaxis_rdesc_fixup));
  279. } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
  280. *rsize > sizeof(sixaxis_rdesc_fixup2)) {
  281. hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
  282. *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
  283. *rsize = sizeof(sixaxis_rdesc_fixup2);
  284. memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
  285. }
  286. if (sc->quirks & PS3REMOTE)
  287. return ps3remote_fixup(hdev, rdesc, rsize);
  288. return rdesc;
  289. }
  290. static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  291. __u8 *rd, int size)
  292. {
  293. struct sony_sc *sc = hid_get_drvdata(hdev);
  294. /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
  295. * has to be BYTE_SWAPPED before passing up to joystick interface
  296. */
  297. if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
  298. rd[0] == 0x01 && size == 49) {
  299. swap(rd[41], rd[42]);
  300. swap(rd[43], rd[44]);
  301. swap(rd[45], rd[46]);
  302. swap(rd[47], rd[48]);
  303. }
  304. return 0;
  305. }
  306. static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
  307. struct hid_field *field, struct hid_usage *usage,
  308. unsigned long **bit, int *max)
  309. {
  310. struct sony_sc *sc = hid_get_drvdata(hdev);
  311. if (sc->quirks & BUZZ_CONTROLLER) {
  312. unsigned int key = usage->hid & HID_USAGE;
  313. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  314. return -1;
  315. switch (usage->collection_index) {
  316. case 1:
  317. if (key >= ARRAY_SIZE(buzz_keymap))
  318. return -1;
  319. key = buzz_keymap[key];
  320. if (!key)
  321. return -1;
  322. break;
  323. default:
  324. return -1;
  325. }
  326. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  327. return 1;
  328. }
  329. if (sc->quirks & PS3REMOTE)
  330. return ps3remote_mapping(hdev, hi, field, usage, bit, max);
  331. /* Let hid-core decide for the others */
  332. return 0;
  333. }
  334. /*
  335. * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
  336. * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
  337. * so we need to override that forcing HID Output Reports on the Control EP.
  338. *
  339. * There is also another issue about HID Output Reports via USB, the Sixaxis
  340. * does not want the report_id as part of the data packet, so we have to
  341. * discard buf[0] when sending the actual control message, even for numbered
  342. * reports, humpf!
  343. */
  344. static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
  345. size_t count, unsigned char report_type)
  346. {
  347. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  348. struct usb_device *dev = interface_to_usbdev(intf);
  349. struct usb_host_interface *interface = intf->cur_altsetting;
  350. int report_id = buf[0];
  351. int ret;
  352. if (report_type == HID_OUTPUT_REPORT) {
  353. /* Don't send the Report ID */
  354. buf++;
  355. count--;
  356. }
  357. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  358. HID_REQ_SET_REPORT,
  359. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  360. ((report_type + 1) << 8) | report_id,
  361. interface->desc.bInterfaceNumber, buf, count,
  362. USB_CTRL_SET_TIMEOUT);
  363. /* Count also the Report ID, in case of an Output report. */
  364. if (ret > 0 && report_type == HID_OUTPUT_REPORT)
  365. ret++;
  366. return ret;
  367. }
  368. /*
  369. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  370. * to "operational". Without this, the ps3 controller will not report any
  371. * events.
  372. */
  373. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  374. {
  375. int ret;
  376. char *buf = kmalloc(18, GFP_KERNEL);
  377. if (!buf)
  378. return -ENOMEM;
  379. ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
  380. if (ret < 0)
  381. hid_err(hdev, "can't set operational mode\n");
  382. kfree(buf);
  383. return ret;
  384. }
  385. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  386. {
  387. unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  388. return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  389. }
  390. static void buzz_set_leds(struct hid_device *hdev, int leds)
  391. {
  392. struct list_head *report_list =
  393. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  394. struct hid_report *report = list_entry(report_list->next,
  395. struct hid_report, list);
  396. __s32 *value = report->field[0]->value;
  397. value[0] = 0x00;
  398. value[1] = (leds & 1) ? 0xff : 0x00;
  399. value[2] = (leds & 2) ? 0xff : 0x00;
  400. value[3] = (leds & 4) ? 0xff : 0x00;
  401. value[4] = (leds & 8) ? 0xff : 0x00;
  402. value[5] = 0x00;
  403. value[6] = 0x00;
  404. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  405. }
  406. static void buzz_led_set_brightness(struct led_classdev *led,
  407. enum led_brightness value)
  408. {
  409. struct device *dev = led->dev->parent;
  410. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  411. struct sony_sc *drv_data;
  412. struct buzz_extra *buzz;
  413. int n;
  414. drv_data = hid_get_drvdata(hdev);
  415. if (!drv_data || !drv_data->extra) {
  416. hid_err(hdev, "No device data\n");
  417. return;
  418. }
  419. buzz = drv_data->extra;
  420. for (n = 0; n < 4; n++) {
  421. if (led == buzz->leds[n]) {
  422. int on = !! (buzz->led_state & (1 << n));
  423. if (value == LED_OFF && on) {
  424. buzz->led_state &= ~(1 << n);
  425. buzz_set_leds(hdev, buzz->led_state);
  426. } else if (value != LED_OFF && !on) {
  427. buzz->led_state |= (1 << n);
  428. buzz_set_leds(hdev, buzz->led_state);
  429. }
  430. break;
  431. }
  432. }
  433. }
  434. static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
  435. {
  436. struct device *dev = led->dev->parent;
  437. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  438. struct sony_sc *drv_data;
  439. struct buzz_extra *buzz;
  440. int n;
  441. int on = 0;
  442. drv_data = hid_get_drvdata(hdev);
  443. if (!drv_data || !drv_data->extra) {
  444. hid_err(hdev, "No device data\n");
  445. return LED_OFF;
  446. }
  447. buzz = drv_data->extra;
  448. for (n = 0; n < 4; n++) {
  449. if (led == buzz->leds[n]) {
  450. on = !! (buzz->led_state & (1 << n));
  451. break;
  452. }
  453. }
  454. return on ? LED_FULL : LED_OFF;
  455. }
  456. static int buzz_init(struct hid_device *hdev)
  457. {
  458. struct sony_sc *drv_data;
  459. struct buzz_extra *buzz;
  460. int n, ret = 0;
  461. struct led_classdev *led;
  462. size_t name_sz;
  463. char *name;
  464. drv_data = hid_get_drvdata(hdev);
  465. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  466. /* Validate expected report characteristics. */
  467. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
  468. return -ENODEV;
  469. buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
  470. if (!buzz) {
  471. hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
  472. return -ENOMEM;
  473. }
  474. drv_data->extra = buzz;
  475. /* Clear LEDs as we have no way of reading their initial state. This is
  476. * only relevant if the driver is loaded after somebody actively set the
  477. * LEDs to on */
  478. buzz_set_leds(hdev, 0x00);
  479. name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
  480. for (n = 0; n < 4; n++) {
  481. led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  482. if (!led) {
  483. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  484. goto error_leds;
  485. }
  486. name = (void *)(&led[1]);
  487. snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
  488. led->name = name;
  489. led->brightness = 0;
  490. led->max_brightness = 1;
  491. led->brightness_get = buzz_led_get_brightness;
  492. led->brightness_set = buzz_led_set_brightness;
  493. if (led_classdev_register(&hdev->dev, led)) {
  494. hid_err(hdev, "Failed to register LED %d\n", n);
  495. kfree(led);
  496. goto error_leds;
  497. }
  498. buzz->leds[n] = led;
  499. }
  500. return ret;
  501. error_leds:
  502. for (n = 0; n < 4; n++) {
  503. led = buzz->leds[n];
  504. buzz->leds[n] = NULL;
  505. if (!led)
  506. continue;
  507. led_classdev_unregister(led);
  508. kfree(led);
  509. }
  510. kfree(drv_data->extra);
  511. drv_data->extra = NULL;
  512. return ret;
  513. }
  514. static void buzz_remove(struct hid_device *hdev)
  515. {
  516. struct sony_sc *drv_data;
  517. struct buzz_extra *buzz;
  518. struct led_classdev *led;
  519. int n;
  520. drv_data = hid_get_drvdata(hdev);
  521. BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
  522. buzz = drv_data->extra;
  523. for (n = 0; n < 4; n++) {
  524. led = buzz->leds[n];
  525. buzz->leds[n] = NULL;
  526. if (!led)
  527. continue;
  528. led_classdev_unregister(led);
  529. kfree(led);
  530. }
  531. kfree(drv_data->extra);
  532. drv_data->extra = NULL;
  533. }
  534. #ifdef CONFIG_SONY_FF
  535. static void sony_rumble_worker(struct work_struct *work)
  536. {
  537. struct sony_sc *sc = container_of(work, struct sony_sc, rumble_worker);
  538. unsigned char buf[] = {
  539. 0x01,
  540. 0x00, 0xff, 0x00, 0xff, 0x00,
  541. 0x00, 0x00, 0x00, 0x00, 0x03,
  542. 0xff, 0x27, 0x10, 0x00, 0x32,
  543. 0xff, 0x27, 0x10, 0x00, 0x32,
  544. 0xff, 0x27, 0x10, 0x00, 0x32,
  545. 0xff, 0x27, 0x10, 0x00, 0x32,
  546. 0x00, 0x00, 0x00, 0x00, 0x00
  547. };
  548. buf[3] = sc->right;
  549. buf[5] = sc->left;
  550. sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
  551. HID_OUTPUT_REPORT);
  552. }
  553. static int sony_play_effect(struct input_dev *dev, void *data,
  554. struct ff_effect *effect)
  555. {
  556. struct hid_device *hid = input_get_drvdata(dev);
  557. struct sony_sc *sc = hid_get_drvdata(hid);
  558. if (effect->type != FF_RUMBLE)
  559. return 0;
  560. sc->left = effect->u.rumble.strong_magnitude / 256;
  561. sc->right = effect->u.rumble.weak_magnitude ? 1 : 0;
  562. schedule_work(&sc->rumble_worker);
  563. return 0;
  564. }
  565. static int sony_init_ff(struct hid_device *hdev)
  566. {
  567. struct hid_input *hidinput = list_entry(hdev->inputs.next,
  568. struct hid_input, list);
  569. struct input_dev *input_dev = hidinput->input;
  570. struct sony_sc *sc = hid_get_drvdata(hdev);
  571. sc->hdev = hdev;
  572. INIT_WORK(&sc->rumble_worker, sony_rumble_worker);
  573. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  574. return input_ff_create_memless(input_dev, NULL, sony_play_effect);
  575. }
  576. static void sony_destroy_ff(struct hid_device *hdev)
  577. {
  578. struct sony_sc *sc = hid_get_drvdata(hdev);
  579. cancel_work_sync(&sc->rumble_worker);
  580. }
  581. #else
  582. static int sony_init_ff(struct hid_device *hdev)
  583. {
  584. return 0;
  585. }
  586. static void sony_destroy_ff(struct hid_device *hdev)
  587. {
  588. }
  589. #endif
  590. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  591. {
  592. int ret;
  593. unsigned long quirks = id->driver_data;
  594. struct sony_sc *sc;
  595. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  596. sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
  597. if (sc == NULL) {
  598. hid_err(hdev, "can't alloc sony descriptor\n");
  599. return -ENOMEM;
  600. }
  601. sc->quirks = quirks;
  602. hid_set_drvdata(hdev, sc);
  603. ret = hid_parse(hdev);
  604. if (ret) {
  605. hid_err(hdev, "parse failed\n");
  606. return ret;
  607. }
  608. if (sc->quirks & VAIO_RDESC_CONSTANT)
  609. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  610. else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
  611. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  612. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  613. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  614. ret = hid_hw_start(hdev, connect_mask);
  615. if (ret) {
  616. hid_err(hdev, "hw start failed\n");
  617. return ret;
  618. }
  619. if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  620. hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
  621. ret = sixaxis_set_operational_usb(hdev);
  622. }
  623. else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
  624. ret = sixaxis_set_operational_bt(hdev);
  625. else if (sc->quirks & BUZZ_CONTROLLER)
  626. ret = buzz_init(hdev);
  627. else
  628. ret = 0;
  629. if (ret < 0)
  630. goto err_stop;
  631. ret = sony_init_ff(hdev);
  632. if (ret < 0)
  633. goto err_stop;
  634. return 0;
  635. err_stop:
  636. hid_hw_stop(hdev);
  637. return ret;
  638. }
  639. static void sony_remove(struct hid_device *hdev)
  640. {
  641. struct sony_sc *sc = hid_get_drvdata(hdev);
  642. if (sc->quirks & BUZZ_CONTROLLER)
  643. buzz_remove(hdev);
  644. sony_destroy_ff(hdev);
  645. hid_hw_stop(hdev);
  646. }
  647. static const struct hid_device_id sony_devices[] = {
  648. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  649. .driver_data = SIXAXIS_CONTROLLER_USB },
  650. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  651. .driver_data = SIXAXIS_CONTROLLER_USB },
  652. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  653. .driver_data = SIXAXIS_CONTROLLER_BT },
  654. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  655. .driver_data = VAIO_RDESC_CONSTANT },
  656. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  657. .driver_data = VAIO_RDESC_CONSTANT },
  658. /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  659. * Logitech joystick from the device descriptor. */
  660. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  661. .driver_data = BUZZ_CONTROLLER },
  662. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  663. .driver_data = BUZZ_CONTROLLER },
  664. /* PS3 BD Remote Control */
  665. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  666. .driver_data = PS3REMOTE },
  667. /* Logitech Harmony Adapter for PS3 */
  668. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  669. .driver_data = PS3REMOTE },
  670. { }
  671. };
  672. MODULE_DEVICE_TABLE(hid, sony_devices);
  673. static struct hid_driver sony_driver = {
  674. .name = "sony",
  675. .id_table = sony_devices,
  676. .input_mapping = sony_mapping,
  677. .probe = sony_probe,
  678. .remove = sony_remove,
  679. .report_fixup = sony_report_fixup,
  680. .raw_event = sony_raw_event
  681. };
  682. module_hid_driver(sony_driver);
  683. MODULE_LICENSE("GPL");