blob: bc37a1800166067af660aa108d2f528c8628a260 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Jiri Kosina078328d2013-06-13 12:03:49 +02002 * HID driver for Sony / PS2 / PS3 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
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
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020011 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 */
19
Jiri Kosina078328d2013-06-13 12:03:49 +020020/* NOTE: in order for the Sony PS3 BD Remote Control to be found by
21 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
22 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
23 *
24 * There will be no PIN request from the device.
25 */
26
Jiri Slabybd28ce02008-06-25 23:47:04 +020027#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020031#include <linux/usb.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020032#include <linux/leds.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020033
34#include "hid-ids.h"
35
Antonio Ospite816651a2010-05-03 22:15:55 +020036#define VAIO_RDESC_CONSTANT (1 << 0)
37#define SIXAXIS_CONTROLLER_USB (1 << 1)
38#define SIXAXIS_CONTROLLER_BT (1 << 2)
Colin Leitnerf04d5142013-05-27 23:41:05 +020039#define BUZZ_CONTROLLER (1 << 3)
Jiri Kosina078328d2013-06-13 12:03:49 +020040#define PS3REMOTE (1 << 4)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020041
Simon Wood61ab44b2011-06-10 12:00:26 +020042static const u8 sixaxis_rdesc_fixup[] = {
43 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
44 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
45 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
46};
47
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020048static const u8 sixaxis_rdesc_fixup2[] = {
49 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
50 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
51 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
52 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
53 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
54 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
55 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
56 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
57 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
58 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
59 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
60 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
61 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
62 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
63 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
64 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
65 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
66 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
67 0xb1, 0x02, 0xc0, 0xc0,
68};
69
Jiri Kosina078328d2013-06-13 12:03:49 +020070static __u8 ps3remote_rdesc[] = {
71 0x05, 0x01, /* GUsagePage Generic Desktop */
72 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
73 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
74
75 /* Use collection 1 for joypad buttons */
76 0xA1, 0x02, /* MCollection Logical (interrelated data) */
77
78 /* Ignore the 1st byte, maybe it is used for a controller
79 * number but it's not needed for correct operation */
80 0x75, 0x08, /* GReportSize 0x08 [8] */
81 0x95, 0x01, /* GReportCount 0x01 [1] */
82 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
83
84 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
85 * buttons multiple keypresses are allowed */
86 0x05, 0x09, /* GUsagePage Button */
87 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
88 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
89 0x14, /* GLogicalMinimum [0] */
90 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
91 0x75, 0x01, /* GReportSize 0x01 [1] */
92 0x95, 0x18, /* GReportCount 0x18 [24] */
93 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
94
95 0xC0, /* MEndCollection */
96
97 /* Use collection 2 for remote control buttons */
98 0xA1, 0x02, /* MCollection Logical (interrelated data) */
99
100 /* 5th byte is used for remote control buttons */
101 0x05, 0x09, /* GUsagePage Button */
102 0x18, /* LUsageMinimum [No button pressed] */
103 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
104 0x14, /* GLogicalMinimum [0] */
105 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
106 0x75, 0x08, /* GReportSize 0x08 [8] */
107 0x95, 0x01, /* GReportCount 0x01 [1] */
108 0x80, /* MInput */
109
110 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
111 * 0xff and 11th is for press indication */
112 0x75, 0x08, /* GReportSize 0x08 [8] */
113 0x95, 0x06, /* GReportCount 0x06 [6] */
114 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
115
116 /* 12th byte is for battery strength */
117 0x05, 0x06, /* GUsagePage Generic Device Controls */
118 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
119 0x14, /* GLogicalMinimum [0] */
120 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
121 0x75, 0x08, /* GReportSize 0x08 [8] */
122 0x95, 0x01, /* GReportCount 0x01 [1] */
123 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
124
125 0xC0, /* MEndCollection */
126
127 0xC0 /* MEndCollection [Game Pad] */
128};
129
130static const unsigned int ps3remote_keymap_joypad_buttons[] = {
131 [0x01] = KEY_SELECT,
132 [0x02] = BTN_THUMBL, /* L3 */
133 [0x03] = BTN_THUMBR, /* R3 */
134 [0x04] = BTN_START,
135 [0x05] = KEY_UP,
136 [0x06] = KEY_RIGHT,
137 [0x07] = KEY_DOWN,
138 [0x08] = KEY_LEFT,
139 [0x09] = BTN_TL2, /* L2 */
140 [0x0a] = BTN_TR2, /* R2 */
141 [0x0b] = BTN_TL, /* L1 */
142 [0x0c] = BTN_TR, /* R1 */
143 [0x0d] = KEY_OPTION, /* options/triangle */
144 [0x0e] = KEY_BACK, /* back/circle */
145 [0x0f] = BTN_0, /* cross */
146 [0x10] = KEY_SCREEN, /* view/square */
147 [0x11] = KEY_HOMEPAGE, /* PS button */
148 [0x14] = KEY_ENTER,
149};
150static const unsigned int ps3remote_keymap_remote_buttons[] = {
151 [0x00] = KEY_1,
152 [0x01] = KEY_2,
153 [0x02] = KEY_3,
154 [0x03] = KEY_4,
155 [0x04] = KEY_5,
156 [0x05] = KEY_6,
157 [0x06] = KEY_7,
158 [0x07] = KEY_8,
159 [0x08] = KEY_9,
160 [0x09] = KEY_0,
161 [0x0e] = KEY_ESC, /* return */
162 [0x0f] = KEY_CLEAR,
163 [0x16] = KEY_EJECTCD,
164 [0x1a] = KEY_MENU, /* top menu */
165 [0x28] = KEY_TIME,
166 [0x30] = KEY_PREVIOUS,
167 [0x31] = KEY_NEXT,
168 [0x32] = KEY_PLAY,
169 [0x33] = KEY_REWIND, /* scan back */
170 [0x34] = KEY_FORWARD, /* scan forward */
171 [0x38] = KEY_STOP,
172 [0x39] = KEY_PAUSE,
173 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
174 [0x60] = KEY_FRAMEBACK, /* slow/step back */
175 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
176 [0x63] = KEY_SUBTITLE,
177 [0x64] = KEY_AUDIO,
178 [0x65] = KEY_ANGLE,
179 [0x70] = KEY_INFO, /* display */
180 [0x80] = KEY_BLUE,
181 [0x81] = KEY_RED,
182 [0x82] = KEY_GREEN,
183 [0x83] = KEY_YELLOW,
184};
185
Colin Leitnerf04d5142013-05-27 23:41:05 +0200186static const unsigned int buzz_keymap[] = {
187 /* The controller has 4 remote buzzers, each with one LED and 5
188 * buttons.
189 *
190 * We use the mapping chosen by the controller, which is:
191 *
192 * Key Offset
193 * -------------------
194 * Buzz 1
195 * Blue 5
196 * Orange 4
197 * Green 3
198 * Yellow 2
199 *
200 * So, for example, the orange button on the third buzzer is mapped to
201 * BTN_TRIGGER_HAPPY14
202 */
203 [ 1] = BTN_TRIGGER_HAPPY1,
204 [ 2] = BTN_TRIGGER_HAPPY2,
205 [ 3] = BTN_TRIGGER_HAPPY3,
206 [ 4] = BTN_TRIGGER_HAPPY4,
207 [ 5] = BTN_TRIGGER_HAPPY5,
208 [ 6] = BTN_TRIGGER_HAPPY6,
209 [ 7] = BTN_TRIGGER_HAPPY7,
210 [ 8] = BTN_TRIGGER_HAPPY8,
211 [ 9] = BTN_TRIGGER_HAPPY9,
212 [10] = BTN_TRIGGER_HAPPY10,
213 [11] = BTN_TRIGGER_HAPPY11,
214 [12] = BTN_TRIGGER_HAPPY12,
215 [13] = BTN_TRIGGER_HAPPY13,
216 [14] = BTN_TRIGGER_HAPPY14,
217 [15] = BTN_TRIGGER_HAPPY15,
218 [16] = BTN_TRIGGER_HAPPY16,
219 [17] = BTN_TRIGGER_HAPPY17,
220 [18] = BTN_TRIGGER_HAPPY18,
221 [19] = BTN_TRIGGER_HAPPY19,
222 [20] = BTN_TRIGGER_HAPPY20,
223};
224
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200225struct sony_sc {
226 unsigned long quirks;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200227
228 void *extra;
229};
230
231struct buzz_extra {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200232 int led_state;
233 struct led_classdev *leds[4];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200234};
235
Jiri Kosina078328d2013-06-13 12:03:49 +0200236static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
237 unsigned int *rsize)
238{
239 *rsize = sizeof(ps3remote_rdesc);
240 return ps3remote_rdesc;
241}
242
243static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
244 struct hid_field *field, struct hid_usage *usage,
245 unsigned long **bit, int *max)
246{
247 unsigned int key = usage->hid & HID_USAGE;
248
249 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
250 return -1;
251
252 switch (usage->collection_index) {
253 case 1:
254 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
255 return -1;
256
257 key = ps3remote_keymap_joypad_buttons[key];
258 if (!key)
259 return -1;
260 break;
261 case 2:
262 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
263 return -1;
264
265 key = ps3remote_keymap_remote_buttons[key];
266 if (!key)
267 return -1;
268 break;
269 default:
270 return -1;
271 }
272
273 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
274 return 1;
275}
276
277
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200278/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400279static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
280 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200281{
282 struct sony_sc *sc = hid_get_drvdata(hdev);
283
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900284 /*
285 * Some Sony RF receivers wrongly declare the mouse pointer as a
286 * a constant non-data variable.
287 */
288 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
289 /* usage page: generic desktop controls */
290 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
291 /* usage: mouse */
292 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
293 /* input (usage page for x,y axes): constant, variable, relative */
294 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +0900295 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900296 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200297 rdesc[55] = 0x06;
298 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200299
300 /* The HID descriptor exposed over BT has a trailing zero byte */
301 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
302 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
303 rdesc[83] == 0x75) {
304 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
305 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
306 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200307 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
308 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
309 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
310 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
311 *rsize = sizeof(sixaxis_rdesc_fixup2);
312 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200313 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200314
315 if (sc->quirks & PS3REMOTE)
316 return ps3remote_fixup(hdev, rdesc, rsize);
317
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400318 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200319}
320
Simon Woodc9e4d872011-06-10 12:00:27 +0200321static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
322 __u8 *rd, int size)
323{
324 struct sony_sc *sc = hid_get_drvdata(hdev);
325
326 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
327 * has to be BYTE_SWAPPED before passing up to joystick interface
328 */
329 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
330 rd[0] == 0x01 && size == 49) {
331 swap(rd[41], rd[42]);
332 swap(rd[43], rd[44]);
333 swap(rd[45], rd[46]);
334 swap(rd[47], rd[48]);
335 }
336
337 return 0;
338}
339
Colin Leitnerf04d5142013-05-27 23:41:05 +0200340static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
341 struct hid_field *field, struct hid_usage *usage,
342 unsigned long **bit, int *max)
343{
344 struct sony_sc *sc = hid_get_drvdata(hdev);
345
346 if (sc->quirks & BUZZ_CONTROLLER) {
347 unsigned int key = usage->hid & HID_USAGE;
348
349 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
350 return -1;
351
352 switch (usage->collection_index) {
353 case 1:
354 if (key >= ARRAY_SIZE(buzz_keymap))
355 return -1;
356
357 key = buzz_keymap[key];
358 if (!key)
359 return -1;
360 break;
361 default:
362 return -1;
363 }
364
365 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
366 return 1;
367 }
368
Jiri Kosina078328d2013-06-13 12:03:49 +0200369 if (sc->quirks & PS3REMOTE)
370 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
371
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200372 /* Let hid-core decide for the others */
373 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200374}
375
Antonio Ospite5710fab2011-02-20 18:26:45 +0100376/*
377 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
378 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
379 * so we need to override that forcing HID Output Reports on the Control EP.
380 *
381 * There is also another issue about HID Output Reports via USB, the Sixaxis
382 * does not want the report_id as part of the data packet, so we have to
383 * discard buf[0] when sending the actual control message, even for numbered
384 * reports, humpf!
385 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200386static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
387 size_t count, unsigned char report_type)
388{
389 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
390 struct usb_device *dev = interface_to_usbdev(intf);
391 struct usb_host_interface *interface = intf->cur_altsetting;
392 int report_id = buf[0];
393 int ret;
394
Antonio Ospite5710fab2011-02-20 18:26:45 +0100395 if (report_type == HID_OUTPUT_REPORT) {
396 /* Don't send the Report ID */
397 buf++;
398 count--;
399 }
400
Antonio Ospite569b10a2010-10-19 16:13:10 +0200401 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
402 HID_REQ_SET_REPORT,
403 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
404 ((report_type + 1) << 8) | report_id,
405 interface->desc.bInterfaceNumber, buf, count,
406 USB_CTRL_SET_TIMEOUT);
407
Antonio Ospite5710fab2011-02-20 18:26:45 +0100408 /* Count also the Report ID, in case of an Output report. */
409 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
410 ret++;
411
Antonio Ospite569b10a2010-10-19 16:13:10 +0200412 return ret;
413}
414
Jiri Slabybd28ce02008-06-25 23:47:04 +0200415/*
416 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
417 * to "operational". Without this, the ps3 controller will not report any
418 * events.
419 */
Antonio Ospite816651a2010-05-03 22:15:55 +0200420static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +0200421{
Jiri Slabybd28ce02008-06-25 23:47:04 +0200422 int ret;
423 char *buf = kmalloc(18, GFP_KERNEL);
424
425 if (!buf)
426 return -ENOMEM;
427
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +0200428 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
429
Jiri Slabybd28ce02008-06-25 23:47:04 +0200430 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -0800431 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +0200432
433 kfree(buf);
434
435 return ret;
436}
437
Antonio Ospite816651a2010-05-03 22:15:55 +0200438static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000439{
Antonio Ospitefddb33f2010-05-03 17:19:03 +0200440 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000441 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
442}
443
Colin Leitnerf04d5142013-05-27 23:41:05 +0200444static void buzz_set_leds(struct hid_device *hdev, int leds)
445{
446 struct list_head *report_list =
447 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
448 struct hid_report *report = list_entry(report_list->next,
449 struct hid_report, list);
450 __s32 *value = report->field[0]->value;
451
452 value[0] = 0x00;
453 value[1] = (leds & 1) ? 0xff : 0x00;
454 value[2] = (leds & 2) ? 0xff : 0x00;
455 value[3] = (leds & 4) ? 0xff : 0x00;
456 value[4] = (leds & 8) ? 0xff : 0x00;
457 value[5] = 0x00;
458 value[6] = 0x00;
459 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
460}
461
462static void buzz_led_set_brightness(struct led_classdev *led,
463 enum led_brightness value)
464{
465 struct device *dev = led->dev->parent;
466 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
467 struct sony_sc *drv_data;
468 struct buzz_extra *buzz;
469
470 int n;
471
472 drv_data = hid_get_drvdata(hdev);
473 if (!drv_data || !drv_data->extra) {
474 hid_err(hdev, "No device data\n");
475 return;
476 }
477 buzz = drv_data->extra;
478
479 for (n = 0; n < 4; n++) {
480 if (led == buzz->leds[n]) {
481 int on = !! (buzz->led_state & (1 << n));
482 if (value == LED_OFF && on) {
483 buzz->led_state &= ~(1 << n);
484 buzz_set_leds(hdev, buzz->led_state);
485 } else if (value != LED_OFF && !on) {
486 buzz->led_state |= (1 << n);
487 buzz_set_leds(hdev, buzz->led_state);
488 }
489 break;
490 }
491 }
492}
493
494static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
495{
496 struct device *dev = led->dev->parent;
497 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
498 struct sony_sc *drv_data;
499 struct buzz_extra *buzz;
500
501 int n;
502 int on = 0;
503
504 drv_data = hid_get_drvdata(hdev);
505 if (!drv_data || !drv_data->extra) {
506 hid_err(hdev, "No device data\n");
507 return LED_OFF;
508 }
509 buzz = drv_data->extra;
510
511 for (n = 0; n < 4; n++) {
512 if (led == buzz->leds[n]) {
513 on = !! (buzz->led_state & (1 << n));
514 break;
515 }
516 }
517
518 return on ? LED_FULL : LED_OFF;
519}
Colin Leitnerf04d5142013-05-27 23:41:05 +0200520
521static int buzz_init(struct hid_device *hdev)
522{
523 struct sony_sc *drv_data;
524 struct buzz_extra *buzz;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200525 int n, ret = 0;
526 struct led_classdev *led;
527 size_t name_sz;
528 char *name;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200529
530 drv_data = hid_get_drvdata(hdev);
531 BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
532
Kees Cook9446edb2013-09-11 21:56:52 +0200533 /* Validate expected report characteristics. */
534 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
535 return -ENODEV;
536
Colin Leitnerf04d5142013-05-27 23:41:05 +0200537 buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
538 if (!buzz) {
539 hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
540 return -ENOMEM;
541 }
542 drv_data->extra = buzz;
543
544 /* Clear LEDs as we have no way of reading their initial state. This is
545 * only relevant if the driver is loaded after somebody actively set the
546 * LEDs to on */
547 buzz_set_leds(hdev, 0x00);
548
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200549 name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200550
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200551 for (n = 0; n < 4; n++) {
552 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
553 if (!led) {
554 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
555 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200556 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200557
558 name = (void *)(&led[1]);
559 snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
560 led->name = name;
561 led->brightness = 0;
562 led->max_brightness = 1;
563 led->brightness_get = buzz_led_get_brightness;
564 led->brightness_set = buzz_led_set_brightness;
565
566 if (led_classdev_register(&hdev->dev, led)) {
567 hid_err(hdev, "Failed to register LED %d\n", n);
568 kfree(led);
569 goto error_leds;
570 }
571
572 buzz->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200573 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200574
575 return ret;
576
Colin Leitnerf04d5142013-05-27 23:41:05 +0200577error_leds:
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200578 for (n = 0; n < 4; n++) {
579 led = buzz->leds[n];
580 buzz->leds[n] = NULL;
581 if (!led)
582 continue;
583 led_classdev_unregister(led);
584 kfree(led);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200585 }
586
587 kfree(drv_data->extra);
588 drv_data->extra = NULL;
589 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200590}
591
592static void buzz_remove(struct hid_device *hdev)
593{
594 struct sony_sc *drv_data;
595 struct buzz_extra *buzz;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200596 struct led_classdev *led;
597 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200598
599 drv_data = hid_get_drvdata(hdev);
600 BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
601
602 buzz = drv_data->extra;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200603
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200604 for (n = 0; n < 4; n++) {
605 led = buzz->leds[n];
606 buzz->leds[n] = NULL;
607 if (!led)
608 continue;
609 led_classdev_unregister(led);
610 kfree(led);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200611 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200612
613 kfree(drv_data->extra);
614 drv_data->extra = NULL;
615}
616
Jiri Slabybd28ce02008-06-25 23:47:04 +0200617static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
618{
619 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200620 unsigned long quirks = id->driver_data;
621 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200622 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200623
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200624 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200625 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800626 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200627 return -ENOMEM;
628 }
629
630 sc->quirks = quirks;
631 hid_set_drvdata(hdev, sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +0200632
Jiri Slabybd28ce02008-06-25 23:47:04 +0200633 ret = hid_parse(hdev);
634 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800635 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200636 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +0200637 }
638
Colin Leitnerf04d5142013-05-27 23:41:05 +0200639 if (sc->quirks & VAIO_RDESC_CONSTANT)
640 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
641 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
642 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
643 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
644 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
645
646 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +0200647 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800648 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200649 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +0200650 }
651
Antonio Ospite569b10a2010-10-19 16:13:10 +0200652 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
653 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +0200654 ret = sixaxis_set_operational_usb(hdev);
Antonio Ospite569b10a2010-10-19 16:13:10 +0200655 }
Antonio Ospite816651a2010-05-03 22:15:55 +0200656 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
657 ret = sixaxis_set_operational_bt(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200658 else if (sc->quirks & BUZZ_CONTROLLER)
659 ret = buzz_init(hdev);
Antonio Ospite816651a2010-05-03 22:15:55 +0200660 else
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000661 ret = 0;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000662
Jiri Kosina4dfdc462008-12-30 00:49:59 +0100663 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +0200664 goto err_stop;
665
666 return 0;
667err_stop:
668 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +0200669 return ret;
670}
671
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200672static void sony_remove(struct hid_device *hdev)
673{
Colin Leitnerf04d5142013-05-27 23:41:05 +0200674 struct sony_sc *sc = hid_get_drvdata(hdev);
675
676 if (sc->quirks & BUZZ_CONTROLLER)
677 buzz_remove(hdev);
678
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200679 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200680}
681
Jiri Slabybd28ce02008-06-25 23:47:04 +0200682static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +0200683 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
684 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +0200685 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
686 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +0200687 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
688 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200689 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
690 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +0900691 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
692 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +0200693 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
694 * Logitech joystick from the device descriptor. */
695 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
696 .driver_data = BUZZ_CONTROLLER },
697 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
698 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +0200699 /* PS3 BD Remote Control */
700 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
701 .driver_data = PS3REMOTE },
702 /* Logitech Harmony Adapter for PS3 */
703 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
704 .driver_data = PS3REMOTE },
Jiri Slabybd28ce02008-06-25 23:47:04 +0200705 { }
706};
707MODULE_DEVICE_TABLE(hid, sony_devices);
708
709static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200710 .name = "sony",
711 .id_table = sony_devices,
712 .input_mapping = sony_mapping,
713 .probe = sony_probe,
714 .remove = sony_remove,
715 .report_fixup = sony_report_fixup,
716 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +0200717};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700718module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +0200719
Jiri Slabybd28ce02008-06-25 23:47:04 +0200720MODULE_LICENSE("GPL");