]>
Commit | Line | Data |
---|---|---|
534a7b8e NLC |
1 | /* |
2 | * HID driver for Logitech Unifying receivers | |
3 | * | |
4 | * Copyright (c) 2011 Logitech | |
5 | */ | |
6 | ||
7 | /* | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | ||
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | ||
25 | #include <linux/device.h> | |
26 | #include <linux/hid.h> | |
27 | #include <linux/module.h> | |
8cb3746a | 28 | #include <linux/kfifo.h> |
82c0beb8 | 29 | #include <linux/delay.h> |
44d27f7d | 30 | #include <asm/unaligned.h> |
534a7b8e | 31 | #include "hid-ids.h" |
8cb3746a BT |
32 | |
33 | #define DJ_MAX_PAIRED_DEVICES 6 | |
4fcad95a | 34 | #define DJ_MAX_NUMBER_NOTIFS 8 |
8cb3746a BT |
35 | #define DJ_RECEIVER_INDEX 0 |
36 | #define DJ_DEVICE_INDEX_MIN 1 | |
37 | #define DJ_DEVICE_INDEX_MAX 6 | |
38 | ||
39 | #define DJREPORT_SHORT_LENGTH 15 | |
40 | #define DJREPORT_LONG_LENGTH 32 | |
41 | ||
42 | #define REPORT_ID_DJ_SHORT 0x20 | |
43 | #define REPORT_ID_DJ_LONG 0x21 | |
44 | ||
925f0f3e BT |
45 | #define REPORT_ID_HIDPP_SHORT 0x10 |
46 | #define REPORT_ID_HIDPP_LONG 0x11 | |
47 | ||
48 | #define HIDPP_REPORT_SHORT_LENGTH 7 | |
49 | #define HIDPP_REPORT_LONG_LENGTH 20 | |
50 | ||
51 | #define HIDPP_RECEIVER_INDEX 0xff | |
52 | ||
53 | #define REPORT_TYPE_RFREPORT_FIRST 0x01 | |
8cb3746a BT |
54 | #define REPORT_TYPE_RFREPORT_LAST 0x1F |
55 | ||
56 | /* Command Switch to DJ mode */ | |
57 | #define REPORT_TYPE_CMD_SWITCH 0x80 | |
58 | #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00 | |
59 | #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01 | |
60 | #define TIMEOUT_NO_KEEPALIVE 0x00 | |
61 | ||
62 | /* Command to Get the list of Paired devices */ | |
63 | #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81 | |
64 | ||
65 | /* Device Paired Notification */ | |
66 | #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41 | |
67 | #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01 | |
68 | #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02 | |
69 | #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00 | |
70 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01 | |
71 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02 | |
72 | #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03 | |
73 | ||
74 | /* Device Un-Paired Notification */ | |
75 | #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40 | |
76 | ||
8cb3746a BT |
77 | /* Connection Status Notification */ |
78 | #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42 | |
79 | #define CONNECTION_STATUS_PARAM_STATUS 0x00 | |
80 | #define STATUS_LINKLOSS 0x01 | |
81 | ||
82 | /* Error Notification */ | |
83 | #define REPORT_TYPE_NOTIF_ERROR 0x7F | |
84 | #define NOTIF_ERROR_PARAM_ETYPE 0x00 | |
85 | #define ETYPE_KEEPALIVE_TIMEOUT 0x01 | |
86 | ||
87 | /* supported DJ HID && RF report types */ | |
88 | #define REPORT_TYPE_KEYBOARD 0x01 | |
89 | #define REPORT_TYPE_MOUSE 0x02 | |
90 | #define REPORT_TYPE_CONSUMER_CONTROL 0x03 | |
91 | #define REPORT_TYPE_SYSTEM_CONTROL 0x04 | |
92 | #define REPORT_TYPE_MEDIA_CENTER 0x08 | |
93 | #define REPORT_TYPE_LEDS 0x0E | |
94 | ||
95 | /* RF Report types bitfield */ | |
a17dd1f2 BT |
96 | #define STD_KEYBOARD BIT(1) |
97 | #define STD_MOUSE BIT(2) | |
98 | #define MULTIMEDIA BIT(3) | |
99 | #define POWER_KEYS BIT(4) | |
100 | #define MEDIA_CENTER BIT(8) | |
101 | #define KBD_LEDS BIT(14) | |
8cb3746a | 102 | |
c0340412 BT |
103 | #define HIDPP_GET_LONG_REGISTER 0x83 |
104 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 | |
105 | #define HIDPP_PAIRING_INFORMATION 0x20 | |
106 | ||
8cb3746a BT |
107 | struct dj_report { |
108 | u8 report_id; | |
109 | u8 device_index; | |
110 | u8 report_type; | |
111 | u8 report_params[DJREPORT_SHORT_LENGTH - 3]; | |
112 | }; | |
113 | ||
7bb56a5f BT |
114 | struct hidpp_event { |
115 | u8 report_id; | |
116 | u8 device_index; | |
117 | u8 sub_id; | |
118 | u8 params[HIDPP_REPORT_LONG_LENGTH - 3U]; | |
119 | } __packed; | |
120 | ||
8cb3746a BT |
121 | struct dj_receiver_dev { |
122 | struct hid_device *hdev; | |
123 | struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES + | |
124 | DJ_DEVICE_INDEX_MIN]; | |
125 | struct work_struct work; | |
126 | struct kfifo notif_fifo; | |
127 | spinlock_t lock; | |
8cb3746a BT |
128 | }; |
129 | ||
130 | struct dj_device { | |
131 | struct hid_device *hdev; | |
132 | struct dj_receiver_dev *dj_receiver_dev; | |
133 | u32 reports_supported; | |
134 | u8 device_index; | |
135 | }; | |
534a7b8e | 136 | |
4fcad95a BT |
137 | #define WORKITEM_TYPE_EMPTY 0 |
138 | #define WORKITEM_TYPE_PAIRED 1 | |
139 | #define WORKITEM_TYPE_UNPAIRED 2 | |
140 | #define WORKITEM_TYPE_UNKNOWN 255 | |
141 | ||
142 | struct dj_workitem { | |
143 | u8 type; /* WORKITEM_TYPE_* */ | |
144 | u8 device_index; | |
145 | u8 quad_id_msb; | |
146 | u8 quad_id_lsb; | |
147 | u32 reports_supported; | |
148 | }; | |
149 | ||
534a7b8e NLC |
150 | /* Keyboard descriptor (1) */ |
151 | static const char kbd_descriptor[] = { | |
152 | 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */ | |
153 | 0x09, 0x06, /* USAGE (Keyboard) */ | |
154 | 0xA1, 0x01, /* COLLECTION (Application) */ | |
155 | 0x85, 0x01, /* REPORT_ID (1) */ | |
156 | 0x95, 0x08, /* REPORT_COUNT (8) */ | |
157 | 0x75, 0x01, /* REPORT_SIZE (1) */ | |
158 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ | |
159 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ | |
160 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ | |
161 | 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */ | |
162 | 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */ | |
163 | 0x81, 0x02, /* INPUT (Data,Var,Abs) */ | |
534a7b8e NLC |
164 | 0x95, 0x06, /* REPORT_COUNT (6) */ |
165 | 0x75, 0x08, /* REPORT_SIZE (8) */ | |
166 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ | |
167 | 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */ | |
168 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ | |
169 | 0x19, 0x00, /* USAGE_MINIMUM (no event) */ | |
170 | 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */ | |
171 | 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ | |
0e40d356 BT |
172 | 0x85, 0x0e, /* REPORT_ID (14) */ |
173 | 0x05, 0x08, /* USAGE PAGE (LED page) */ | |
174 | 0x95, 0x05, /* REPORT COUNT (5) */ | |
175 | 0x75, 0x01, /* REPORT SIZE (1) */ | |
176 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ | |
177 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ | |
178 | 0x19, 0x01, /* USAGE MINIMUM (1) */ | |
179 | 0x29, 0x05, /* USAGE MAXIMUM (5) */ | |
180 | 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ | |
181 | 0x95, 0x01, /* REPORT COUNT (1) */ | |
182 | 0x75, 0x03, /* REPORT SIZE (3) */ | |
183 | 0x91, 0x01, /* OUTPUT (Constant) */ | |
534a7b8e NLC |
184 | 0xC0 |
185 | }; | |
186 | ||
187 | /* Mouse descriptor (2) */ | |
188 | static const char mse_descriptor[] = { | |
189 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ | |
190 | 0x09, 0x02, /* USAGE (Mouse) */ | |
191 | 0xA1, 0x01, /* COLLECTION (Application) */ | |
192 | 0x85, 0x02, /* REPORT_ID = 2 */ | |
193 | 0x09, 0x01, /* USAGE (pointer) */ | |
194 | 0xA1, 0x00, /* COLLECTION (physical) */ | |
195 | 0x05, 0x09, /* USAGE_PAGE (buttons) */ | |
196 | 0x19, 0x01, /* USAGE_MIN (1) */ | |
197 | 0x29, 0x10, /* USAGE_MAX (16) */ | |
198 | 0x15, 0x00, /* LOGICAL_MIN (0) */ | |
199 | 0x25, 0x01, /* LOGICAL_MAX (1) */ | |
200 | 0x95, 0x10, /* REPORT_COUNT (16) */ | |
201 | 0x75, 0x01, /* REPORT_SIZE (1) */ | |
202 | 0x81, 0x02, /* INPUT (data var abs) */ | |
203 | 0x05, 0x01, /* USAGE_PAGE (generic desktop) */ | |
204 | 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */ | |
205 | 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */ | |
206 | 0x75, 0x0C, /* REPORT_SIZE (12) */ | |
207 | 0x95, 0x02, /* REPORT_COUNT (2) */ | |
208 | 0x09, 0x30, /* USAGE (X) */ | |
209 | 0x09, 0x31, /* USAGE (Y) */ | |
210 | 0x81, 0x06, /* INPUT */ | |
211 | 0x15, 0x81, /* LOGICAL_MIN (-127) */ | |
212 | 0x25, 0x7F, /* LOGICAL_MAX (127) */ | |
213 | 0x75, 0x08, /* REPORT_SIZE (8) */ | |
214 | 0x95, 0x01, /* REPORT_COUNT (1) */ | |
215 | 0x09, 0x38, /* USAGE (wheel) */ | |
216 | 0x81, 0x06, /* INPUT */ | |
217 | 0x05, 0x0C, /* USAGE_PAGE(consumer) */ | |
218 | 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */ | |
219 | 0x95, 0x01, /* REPORT_COUNT (1) */ | |
220 | 0x81, 0x06, /* INPUT */ | |
221 | 0xC0, /* END_COLLECTION */ | |
222 | 0xC0, /* END_COLLECTION */ | |
223 | }; | |
224 | ||
225 | /* Consumer Control descriptor (3) */ | |
226 | static const char consumer_descriptor[] = { | |
227 | 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */ | |
228 | 0x09, 0x01, /* USAGE (Consumer Control) */ | |
229 | 0xA1, 0x01, /* COLLECTION (Application) */ | |
230 | 0x85, 0x03, /* REPORT_ID = 3 */ | |
231 | 0x75, 0x10, /* REPORT_SIZE (16) */ | |
232 | 0x95, 0x02, /* REPORT_COUNT (2) */ | |
233 | 0x15, 0x01, /* LOGICAL_MIN (1) */ | |
234 | 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */ | |
235 | 0x19, 0x01, /* USAGE_MIN (1) */ | |
236 | 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */ | |
237 | 0x81, 0x00, /* INPUT (Data Ary Abs) */ | |
238 | 0xC0, /* END_COLLECTION */ | |
239 | }; /* */ | |
240 | ||
241 | /* System control descriptor (4) */ | |
242 | static const char syscontrol_descriptor[] = { | |
243 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ | |
244 | 0x09, 0x80, /* USAGE (System Control) */ | |
245 | 0xA1, 0x01, /* COLLECTION (Application) */ | |
246 | 0x85, 0x04, /* REPORT_ID = 4 */ | |
247 | 0x75, 0x02, /* REPORT_SIZE (2) */ | |
248 | 0x95, 0x01, /* REPORT_COUNT (1) */ | |
249 | 0x15, 0x01, /* LOGICAL_MIN (1) */ | |
250 | 0x25, 0x03, /* LOGICAL_MAX (3) */ | |
251 | 0x09, 0x82, /* USAGE (System Sleep) */ | |
252 | 0x09, 0x81, /* USAGE (System Power Down) */ | |
253 | 0x09, 0x83, /* USAGE (System Wake Up) */ | |
254 | 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ | |
255 | 0x75, 0x06, /* REPORT_SIZE (6) */ | |
256 | 0x81, 0x03, /* INPUT (Cnst Var Abs) */ | |
257 | 0xC0, /* END_COLLECTION */ | |
258 | }; | |
259 | ||
260 | /* Media descriptor (8) */ | |
261 | static const char media_descriptor[] = { | |
262 | 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */ | |
263 | 0x09, 0x88, /* Usage 0x0088 */ | |
264 | 0xa1, 0x01, /* BeginCollection */ | |
265 | 0x85, 0x08, /* Report ID 8 */ | |
266 | 0x19, 0x01, /* Usage Min 0x0001 */ | |
267 | 0x29, 0xff, /* Usage Max 0x00ff */ | |
268 | 0x15, 0x01, /* Logical Min 1 */ | |
269 | 0x26, 0xff, 0x00, /* Logical Max 255 */ | |
270 | 0x75, 0x08, /* Report Size 8 */ | |
271 | 0x95, 0x01, /* Report Count 1 */ | |
272 | 0x81, 0x00, /* Input */ | |
273 | 0xc0, /* EndCollection */ | |
274 | }; /* */ | |
275 | ||
925f0f3e BT |
276 | /* HIDPP descriptor */ |
277 | static const char hidpp_descriptor[] = { | |
278 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ | |
279 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ | |
280 | 0xa1, 0x01, /* Collection (Application) */ | |
281 | 0x85, 0x10, /* Report ID (16) */ | |
282 | 0x75, 0x08, /* Report Size (8) */ | |
283 | 0x95, 0x06, /* Report Count (6) */ | |
284 | 0x15, 0x00, /* Logical Minimum (0) */ | |
285 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ | |
286 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ | |
287 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ | |
288 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ | |
289 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ | |
290 | 0xc0, /* End Collection */ | |
291 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ | |
292 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ | |
293 | 0xa1, 0x01, /* Collection (Application) */ | |
294 | 0x85, 0x11, /* Report ID (17) */ | |
295 | 0x75, 0x08, /* Report Size (8) */ | |
296 | 0x95, 0x13, /* Report Count (19) */ | |
297 | 0x15, 0x00, /* Logical Minimum (0) */ | |
298 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ | |
299 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ | |
300 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ | |
301 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ | |
302 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ | |
303 | 0xc0, /* End Collection */ | |
304 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ | |
305 | 0x09, 0x04, /* Usage (Vendor Usage 0x04) */ | |
306 | 0xa1, 0x01, /* Collection (Application) */ | |
307 | 0x85, 0x20, /* Report ID (32) */ | |
308 | 0x75, 0x08, /* Report Size (8) */ | |
309 | 0x95, 0x0e, /* Report Count (14) */ | |
310 | 0x15, 0x00, /* Logical Minimum (0) */ | |
311 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ | |
312 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ | |
313 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ | |
314 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ | |
315 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ | |
316 | 0x85, 0x21, /* Report ID (33) */ | |
317 | 0x95, 0x1f, /* Report Count (31) */ | |
318 | 0x15, 0x00, /* Logical Minimum (0) */ | |
319 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ | |
320 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ | |
321 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ | |
322 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ | |
323 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ | |
324 | 0xc0, /* End Collection */ | |
325 | }; | |
326 | ||
534a7b8e NLC |
327 | /* Maximum size of all defined hid reports in bytes (including report id) */ |
328 | #define MAX_REPORT_SIZE 8 | |
329 | ||
2a039bf5 HR |
330 | /* Make sure all descriptors are present here */ |
331 | #define MAX_RDESC_SIZE \ | |
332 | (sizeof(kbd_descriptor) + \ | |
333 | sizeof(mse_descriptor) + \ | |
334 | sizeof(consumer_descriptor) + \ | |
335 | sizeof(syscontrol_descriptor) + \ | |
925f0f3e BT |
336 | sizeof(media_descriptor) + \ |
337 | sizeof(hidpp_descriptor)) | |
2a039bf5 | 338 | |
534a7b8e NLC |
339 | /* Number of possible hid report types that can be created by this driver. |
340 | * | |
341 | * Right now, RF report types have the same report types (or report id's) | |
342 | * than the hid report created from those RF reports. In the future | |
343 | * this doesnt have to be true. | |
344 | * | |
345 | * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds | |
346 | * to hid report id 0x01, this is standard keyboard. Same thing applies to mice | |
347 | * reports and consumer control, etc. If a new RF report is created, it doesn't | |
348 | * has to have the same report id as its corresponding hid report, so an | |
349 | * translation may have to take place for future report types. | |
350 | */ | |
351 | #define NUMBER_OF_HID_REPORTS 32 | |
352 | static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = { | |
353 | [1] = 8, /* Standard keyboard */ | |
354 | [2] = 8, /* Standard mouse */ | |
355 | [3] = 5, /* Consumer control */ | |
356 | [4] = 2, /* System control */ | |
357 | [8] = 2, /* Media Center */ | |
358 | }; | |
359 | ||
360 | ||
361 | #define LOGITECH_DJ_INTERFACE_NUMBER 0x02 | |
362 | ||
534a7b8e NLC |
363 | static struct hid_ll_driver logi_dj_ll_driver; |
364 | ||
c63e0e37 | 365 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev); |
534a7b8e NLC |
366 | |
367 | static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev, | |
4fcad95a | 368 | struct dj_workitem *workitem) |
534a7b8e NLC |
369 | { |
370 | /* Called in delayed work context */ | |
371 | struct dj_device *dj_dev; | |
372 | unsigned long flags; | |
373 | ||
374 | spin_lock_irqsave(&djrcv_dev->lock, flags); | |
4fcad95a BT |
375 | dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index]; |
376 | djrcv_dev->paired_dj_devices[workitem->device_index] = NULL; | |
534a7b8e NLC |
377 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
378 | ||
379 | if (dj_dev != NULL) { | |
380 | hid_destroy_device(dj_dev->hdev); | |
381 | kfree(dj_dev); | |
382 | } else { | |
383 | dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n", | |
384 | __func__); | |
385 | } | |
386 | } | |
387 | ||
388 | static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev, | |
4fcad95a | 389 | struct dj_workitem *workitem) |
534a7b8e NLC |
390 | { |
391 | /* Called in delayed work context */ | |
392 | struct hid_device *djrcv_hdev = djrcv_dev->hdev; | |
534a7b8e NLC |
393 | struct hid_device *dj_hiddev; |
394 | struct dj_device *dj_dev; | |
4fcad95a | 395 | u8 device_index = workitem->device_index; |
534a7b8e NLC |
396 | |
397 | /* Device index goes from 1 to 6, we need 3 bytes to store the | |
398 | * semicolon, the index, and a null terminator | |
399 | */ | |
400 | unsigned char tmpstr[3]; | |
401 | ||
4fcad95a | 402 | if (djrcv_dev->paired_dj_devices[device_index]) { |
c63e0e37 NLC |
403 | /* The device is already known. No need to reallocate it. */ |
404 | dbg_hid("%s: device is already known\n", __func__); | |
405 | return; | |
406 | } | |
407 | ||
534a7b8e NLC |
408 | dj_hiddev = hid_allocate_device(); |
409 | if (IS_ERR(dj_hiddev)) { | |
410 | dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n", | |
411 | __func__); | |
412 | return; | |
413 | } | |
414 | ||
415 | dj_hiddev->ll_driver = &logi_dj_ll_driver; | |
534a7b8e NLC |
416 | |
417 | dj_hiddev->dev.parent = &djrcv_hdev->dev; | |
418 | dj_hiddev->bus = BUS_USB; | |
82c0beb8 | 419 | dj_hiddev->vendor = djrcv_hdev->vendor; |
4fcad95a BT |
420 | dj_hiddev->product = (workitem->quad_id_msb << 8) | |
421 | workitem->quad_id_lsb; | |
534a7b8e | 422 | snprintf(dj_hiddev->name, sizeof(dj_hiddev->name), |
d610274b BT |
423 | "Logitech Unifying Device. Wireless PID:%04x", |
424 | dj_hiddev->product); | |
425 | ||
426 | dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE; | |
534a7b8e | 427 | |
82c0beb8 | 428 | memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys)); |
4fcad95a | 429 | snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index); |
534a7b8e NLC |
430 | strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys)); |
431 | ||
432 | dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL); | |
433 | ||
434 | if (!dj_dev) { | |
435 | dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n", | |
436 | __func__); | |
437 | goto dj_device_allocate_fail; | |
438 | } | |
439 | ||
4fcad95a | 440 | dj_dev->reports_supported = workitem->reports_supported; |
534a7b8e NLC |
441 | dj_dev->hdev = dj_hiddev; |
442 | dj_dev->dj_receiver_dev = djrcv_dev; | |
4fcad95a | 443 | dj_dev->device_index = device_index; |
534a7b8e NLC |
444 | dj_hiddev->driver_data = dj_dev; |
445 | ||
4fcad95a | 446 | djrcv_dev->paired_dj_devices[device_index] = dj_dev; |
534a7b8e NLC |
447 | |
448 | if (hid_add_device(dj_hiddev)) { | |
449 | dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n", | |
450 | __func__); | |
451 | goto hid_add_device_fail; | |
452 | } | |
453 | ||
454 | return; | |
455 | ||
456 | hid_add_device_fail: | |
4fcad95a | 457 | djrcv_dev->paired_dj_devices[device_index] = NULL; |
534a7b8e NLC |
458 | kfree(dj_dev); |
459 | dj_device_allocate_fail: | |
460 | hid_destroy_device(dj_hiddev); | |
461 | } | |
462 | ||
463 | static void delayedwork_callback(struct work_struct *work) | |
464 | { | |
465 | struct dj_receiver_dev *djrcv_dev = | |
466 | container_of(work, struct dj_receiver_dev, work); | |
467 | ||
4fcad95a | 468 | struct dj_workitem workitem; |
534a7b8e NLC |
469 | unsigned long flags; |
470 | int count; | |
c63e0e37 | 471 | int retval; |
534a7b8e NLC |
472 | |
473 | dbg_hid("%s\n", __func__); | |
474 | ||
475 | spin_lock_irqsave(&djrcv_dev->lock, flags); | |
476 | ||
4fcad95a | 477 | count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
534a7b8e | 478 | |
4fcad95a | 479 | if (count != sizeof(workitem)) { |
534a7b8e NLC |
480 | dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without " |
481 | "notifications available\n", __func__); | |
482 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); | |
483 | return; | |
484 | } | |
485 | ||
486 | if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) { | |
487 | if (schedule_work(&djrcv_dev->work) == 0) { | |
488 | dbg_hid("%s: did not schedule the work item, was " | |
489 | "already queued\n", __func__); | |
490 | } | |
491 | } | |
492 | ||
493 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); | |
494 | ||
4fcad95a BT |
495 | switch (workitem.type) { |
496 | case WORKITEM_TYPE_PAIRED: | |
497 | logi_dj_recv_add_djhid_device(djrcv_dev, &workitem); | |
534a7b8e | 498 | break; |
4fcad95a BT |
499 | case WORKITEM_TYPE_UNPAIRED: |
500 | logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem); | |
501 | break; | |
502 | case WORKITEM_TYPE_UNKNOWN: | |
503 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); | |
504 | if (retval) { | |
505 | dev_err(&djrcv_dev->hdev->dev, | |
506 | "%s: logi_dj_recv_query_paired_devices error: %d\n", | |
507 | __func__, retval); | |
508 | } | |
509 | break; | |
510 | case WORKITEM_TYPE_EMPTY: | |
511 | dbg_hid("%s: device list is empty\n", __func__); | |
512 | break; | |
513 | } | |
514 | } | |
515 | ||
516 | static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev, | |
517 | struct dj_report *dj_report) | |
518 | { | |
519 | /* We are called from atomic context (tasklet && djrcv->lock held) */ | |
520 | struct dj_workitem workitem = { | |
521 | .device_index = dj_report->device_index, | |
522 | }; | |
523 | ||
524 | switch (dj_report->report_type) { | |
525 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: | |
526 | workitem.type = WORKITEM_TYPE_PAIRED; | |
527 | if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] & | |
528 | SPFUNCTION_DEVICE_LIST_EMPTY) { | |
529 | workitem.type = WORKITEM_TYPE_EMPTY; | |
530 | break; | |
531 | } | |
532 | /* fall-through */ | |
534a7b8e | 533 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
4fcad95a BT |
534 | workitem.quad_id_msb = |
535 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]; | |
536 | workitem.quad_id_lsb = | |
537 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]; | |
538 | workitem.reports_supported = get_unaligned_le32( | |
539 | dj_report->report_params + | |
540 | DEVICE_PAIRED_RF_REPORT_TYPE); | |
541 | if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED) | |
542 | workitem.type = WORKITEM_TYPE_UNPAIRED; | |
534a7b8e NLC |
543 | break; |
544 | default: | |
c63e0e37 NLC |
545 | /* A normal report (i. e. not belonging to a pair/unpair notification) |
546 | * arriving here, means that the report arrived but we did not have a | |
547 | * paired dj_device associated to the report's device_index, this | |
548 | * means that the original "device paired" notification corresponding | |
549 | * to this dj_device never arrived to this driver. The reason is that | |
550 | * hid-core discards all packets coming from a device while probe() is | |
551 | * executing. */ | |
4fcad95a BT |
552 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
553 | /* | |
554 | * ok, we don't know the device, just re-ask the | |
555 | * receiver for the list of connected devices in | |
556 | * the delayed work callback. | |
557 | */ | |
558 | workitem.type = WORKITEM_TYPE_UNKNOWN; | |
c63e0e37 | 559 | } |
534a7b8e | 560 | } |
534a7b8e | 561 | |
4fcad95a | 562 | kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
534a7b8e NLC |
563 | |
564 | if (schedule_work(&djrcv_dev->work) == 0) { | |
565 | dbg_hid("%s: did not schedule the work item, was already " | |
566 | "queued\n", __func__); | |
567 | } | |
568 | } | |
569 | ||
570 | static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev, | |
571 | struct dj_report *dj_report) | |
572 | { | |
573 | /* We are called from atomic context (tasklet && djrcv->lock held) */ | |
574 | unsigned int i; | |
575 | u8 reportbuffer[MAX_REPORT_SIZE]; | |
576 | struct dj_device *djdev; | |
577 | ||
578 | djdev = djrcv_dev->paired_dj_devices[dj_report->device_index]; | |
579 | ||
534a7b8e NLC |
580 | memset(reportbuffer, 0, sizeof(reportbuffer)); |
581 | ||
582 | for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) { | |
583 | if (djdev->reports_supported & (1 << i)) { | |
584 | reportbuffer[0] = i; | |
585 | if (hid_input_report(djdev->hdev, | |
586 | HID_INPUT_REPORT, | |
587 | reportbuffer, | |
588 | hid_reportid_size_map[i], 1)) { | |
589 | dbg_hid("hid_input_report error sending null " | |
590 | "report\n"); | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
83898234 BT |
596 | static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev, |
597 | struct dj_report *dj_report) | |
534a7b8e NLC |
598 | { |
599 | /* We are called from atomic context (tasklet && djrcv->lock held) */ | |
600 | struct dj_device *dj_device; | |
601 | ||
602 | dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index]; | |
603 | ||
534a7b8e NLC |
604 | if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) || |
605 | (hid_reportid_size_map[dj_report->report_type] == 0)) { | |
606 | dbg_hid("invalid report type:%x\n", dj_report->report_type); | |
607 | return; | |
608 | } | |
609 | ||
610 | if (hid_input_report(dj_device->hdev, | |
611 | HID_INPUT_REPORT, &dj_report->report_type, | |
612 | hid_reportid_size_map[dj_report->report_type], 1)) { | |
613 | dbg_hid("hid_input_report error\n"); | |
614 | } | |
615 | } | |
616 | ||
83898234 BT |
617 | static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data, |
618 | int size) | |
925f0f3e BT |
619 | { |
620 | /* We are called from atomic context (tasklet && djrcv->lock held) */ | |
621 | if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1)) | |
622 | dbg_hid("hid_input_report error\n"); | |
623 | } | |
534a7b8e NLC |
624 | |
625 | static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, | |
626 | struct dj_report *dj_report) | |
627 | { | |
628 | struct hid_device *hdev = djrcv_dev->hdev; | |
dcd9006b BT |
629 | struct hid_report *report; |
630 | struct hid_report_enum *output_report_enum; | |
631 | u8 *data = (u8 *)(&dj_report->device_index); | |
297502ab | 632 | unsigned int i; |
534a7b8e | 633 | |
dcd9006b BT |
634 | output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; |
635 | report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT]; | |
636 | ||
637 | if (!report) { | |
638 | dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__); | |
534a7b8e NLC |
639 | return -ENODEV; |
640 | } | |
641 | ||
297502ab | 642 | for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++) |
dcd9006b BT |
643 | report->field[0]->value[i] = data[i]; |
644 | ||
83a44ac8 | 645 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
534a7b8e | 646 | |
dcd9006b | 647 | return 0; |
534a7b8e NLC |
648 | } |
649 | ||
650 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev) | |
651 | { | |
d8dc3494 MD |
652 | struct dj_report *dj_report; |
653 | int retval; | |
534a7b8e | 654 | |
8a55ade7 | 655 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
d8dc3494 MD |
656 | if (!dj_report) |
657 | return -ENOMEM; | |
658 | dj_report->report_id = REPORT_ID_DJ_SHORT; | |
659 | dj_report->device_index = 0xFF; | |
660 | dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES; | |
661 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); | |
662 | kfree(dj_report); | |
663 | return retval; | |
534a7b8e NLC |
664 | } |
665 | ||
c63e0e37 | 666 | |
534a7b8e NLC |
667 | static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev, |
668 | unsigned timeout) | |
669 | { | |
6a9ddc89 | 670 | struct hid_device *hdev = djrcv_dev->hdev; |
d8dc3494 | 671 | struct dj_report *dj_report; |
6a9ddc89 | 672 | u8 *buf; |
d8dc3494 | 673 | int retval; |
534a7b8e | 674 | |
8a55ade7 | 675 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
d8dc3494 MD |
676 | if (!dj_report) |
677 | return -ENOMEM; | |
678 | dj_report->report_id = REPORT_ID_DJ_SHORT; | |
679 | dj_report->device_index = 0xFF; | |
680 | dj_report->report_type = REPORT_TYPE_CMD_SWITCH; | |
681 | dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F; | |
682 | dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout; | |
683 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); | |
42c22dbf BT |
684 | |
685 | /* | |
686 | * Ugly sleep to work around a USB 3.0 bug when the receiver is still | |
687 | * processing the "switch-to-dj" command while we send an other command. | |
688 | * 50 msec should gives enough time to the receiver to be ready. | |
689 | */ | |
690 | msleep(50); | |
691 | ||
6a9ddc89 BT |
692 | /* |
693 | * Magical bits to set up hidpp notifications when the dj devices | |
694 | * are connected/disconnected. | |
695 | * | |
696 | * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller | |
697 | * than DJREPORT_SHORT_LENGTH. | |
698 | */ | |
699 | buf = (u8 *)dj_report; | |
700 | ||
701 | memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH); | |
702 | ||
703 | buf[0] = REPORT_ID_HIDPP_SHORT; | |
704 | buf[1] = 0xFF; | |
705 | buf[2] = 0x80; | |
706 | buf[3] = 0x00; | |
707 | buf[4] = 0x00; | |
708 | buf[5] = 0x09; | |
709 | buf[6] = 0x00; | |
710 | ||
711 | hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf, | |
712 | HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT, | |
713 | HID_REQ_SET_REPORT); | |
714 | ||
715 | kfree(dj_report); | |
d8dc3494 | 716 | return retval; |
534a7b8e NLC |
717 | } |
718 | ||
719 | ||
720 | static int logi_dj_ll_open(struct hid_device *hid) | |
721 | { | |
722 | dbg_hid("%s:%s\n", __func__, hid->phys); | |
723 | return 0; | |
724 | ||
725 | } | |
726 | ||
727 | static void logi_dj_ll_close(struct hid_device *hid) | |
728 | { | |
729 | dbg_hid("%s:%s\n", __func__, hid->phys); | |
730 | } | |
731 | ||
8dba3026 BT |
732 | /* |
733 | * Register 0xB5 is "pairing information". It is solely intended for the | |
734 | * receiver, so do not overwrite the device index. | |
735 | */ | |
c0340412 BT |
736 | static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT, |
737 | HIDPP_RECEIVER_INDEX, | |
738 | HIDPP_GET_LONG_REGISTER, | |
739 | HIDPP_REG_PAIRING_INFORMATION }; | |
740 | static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG, | |
741 | HIDPP_RECEIVER_INDEX, | |
742 | HIDPP_GET_LONG_REGISTER, | |
743 | HIDPP_REG_PAIRING_INFORMATION }; | |
33797820 | 744 | |
bd27e202 BT |
745 | static int logi_dj_ll_raw_request(struct hid_device *hid, |
746 | unsigned char reportnum, __u8 *buf, | |
747 | size_t count, unsigned char report_type, | |
748 | int reqtype) | |
534a7b8e | 749 | { |
0e40d356 BT |
750 | struct dj_device *djdev = hid->driver_data; |
751 | struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev; | |
752 | u8 *out_buf; | |
753 | int ret; | |
534a7b8e | 754 | |
925f0f3e BT |
755 | if ((buf[0] == REPORT_ID_HIDPP_SHORT) || |
756 | (buf[0] == REPORT_ID_HIDPP_LONG)) { | |
757 | if (count < 2) | |
758 | return -EINVAL; | |
759 | ||
33797820 BT |
760 | /* special case where we should not overwrite |
761 | * the device_index */ | |
8dba3026 BT |
762 | if (count == 7 && !memcmp(buf, unifying_pairing_query, |
763 | sizeof(unifying_pairing_query))) | |
764 | buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1); | |
33797820 BT |
765 | else |
766 | buf[1] = djdev->device_index; | |
925f0f3e BT |
767 | return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf, |
768 | count, report_type, reqtype); | |
769 | } | |
770 | ||
0e40d356 BT |
771 | if (buf[0] != REPORT_TYPE_LEDS) |
772 | return -EINVAL; | |
773 | ||
774 | out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC); | |
775 | if (!out_buf) | |
776 | return -ENOMEM; | |
777 | ||
51217e69 | 778 | if (count > DJREPORT_SHORT_LENGTH - 2) |
0e40d356 BT |
779 | count = DJREPORT_SHORT_LENGTH - 2; |
780 | ||
781 | out_buf[0] = REPORT_ID_DJ_SHORT; | |
782 | out_buf[1] = djdev->device_index; | |
783 | memcpy(out_buf + 2, buf, count); | |
784 | ||
0e40d356 | 785 | ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf, |
bd27e202 | 786 | DJREPORT_SHORT_LENGTH, report_type, reqtype); |
0e40d356 BT |
787 | |
788 | kfree(out_buf); | |
789 | return ret; | |
534a7b8e NLC |
790 | } |
791 | ||
6d603326 | 792 | static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size) |
2a039bf5 | 793 | { |
6d603326 | 794 | memcpy(rdesc + *rsize, data, size); |
2a039bf5 HR |
795 | *rsize += size; |
796 | } | |
797 | ||
534a7b8e NLC |
798 | static int logi_dj_ll_parse(struct hid_device *hid) |
799 | { | |
800 | struct dj_device *djdev = hid->driver_data; | |
2a039bf5 HR |
801 | unsigned int rsize = 0; |
802 | char *rdesc; | |
534a7b8e NLC |
803 | int retval; |
804 | ||
805 | dbg_hid("%s\n", __func__); | |
806 | ||
807 | djdev->hdev->version = 0x0111; | |
808 | djdev->hdev->country = 0x00; | |
809 | ||
2a039bf5 HR |
810 | rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL); |
811 | if (!rdesc) | |
812 | return -ENOMEM; | |
813 | ||
534a7b8e NLC |
814 | if (djdev->reports_supported & STD_KEYBOARD) { |
815 | dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n", | |
816 | __func__, djdev->reports_supported); | |
6d603326 | 817 | rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor)); |
534a7b8e NLC |
818 | } |
819 | ||
820 | if (djdev->reports_supported & STD_MOUSE) { | |
821 | dbg_hid("%s: sending a mouse descriptor, reports_supported: " | |
822 | "%x\n", __func__, djdev->reports_supported); | |
6d603326 | 823 | rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor)); |
534a7b8e NLC |
824 | } |
825 | ||
826 | if (djdev->reports_supported & MULTIMEDIA) { | |
827 | dbg_hid("%s: sending a multimedia report descriptor: %x\n", | |
828 | __func__, djdev->reports_supported); | |
6d603326 | 829 | rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor)); |
534a7b8e NLC |
830 | } |
831 | ||
832 | if (djdev->reports_supported & POWER_KEYS) { | |
833 | dbg_hid("%s: sending a power keys report descriptor: %x\n", | |
834 | __func__, djdev->reports_supported); | |
6d603326 | 835 | rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor)); |
534a7b8e NLC |
836 | } |
837 | ||
838 | if (djdev->reports_supported & MEDIA_CENTER) { | |
839 | dbg_hid("%s: sending a media center report descriptor: %x\n", | |
840 | __func__, djdev->reports_supported); | |
6d603326 | 841 | rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor)); |
534a7b8e NLC |
842 | } |
843 | ||
844 | if (djdev->reports_supported & KBD_LEDS) { | |
845 | dbg_hid("%s: need to send kbd leds report descriptor: %x\n", | |
846 | __func__, djdev->reports_supported); | |
847 | } | |
848 | ||
925f0f3e BT |
849 | rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor)); |
850 | ||
2a039bf5 HR |
851 | retval = hid_parse_report(hid, rdesc, rsize); |
852 | kfree(rdesc); | |
853 | ||
854 | return retval; | |
534a7b8e NLC |
855 | } |
856 | ||
534a7b8e NLC |
857 | static int logi_dj_ll_start(struct hid_device *hid) |
858 | { | |
859 | dbg_hid("%s\n", __func__); | |
860 | return 0; | |
861 | } | |
862 | ||
863 | static void logi_dj_ll_stop(struct hid_device *hid) | |
864 | { | |
865 | dbg_hid("%s\n", __func__); | |
866 | } | |
867 | ||
868 | ||
869 | static struct hid_ll_driver logi_dj_ll_driver = { | |
870 | .parse = logi_dj_ll_parse, | |
871 | .start = logi_dj_ll_start, | |
872 | .stop = logi_dj_ll_stop, | |
873 | .open = logi_dj_ll_open, | |
874 | .close = logi_dj_ll_close, | |
bd27e202 | 875 | .raw_request = logi_dj_ll_raw_request, |
534a7b8e NLC |
876 | }; |
877 | ||
925f0f3e | 878 | static int logi_dj_dj_event(struct hid_device *hdev, |
534a7b8e NLC |
879 | struct hid_report *report, u8 *data, |
880 | int size) | |
881 | { | |
882 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); | |
883 | struct dj_report *dj_report = (struct dj_report *) data; | |
884 | unsigned long flags; | |
534a7b8e | 885 | |
925f0f3e BT |
886 | /* |
887 | * Here we receive all data coming from iface 2, there are 3 cases: | |
534a7b8e | 888 | * |
925f0f3e BT |
889 | * 1) Data is intended for this driver i. e. data contains arrival, |
890 | * departure, etc notifications, in which case we queue them for delayed | |
891 | * processing by the work queue. We return 1 to hid-core as no further | |
892 | * processing is required from it. | |
534a7b8e | 893 | * |
925f0f3e BT |
894 | * 2) Data informs a connection change, if the change means rf link |
895 | * loss, then we must send a null report to the upper layer to discard | |
896 | * potentially pressed keys that may be repeated forever by the input | |
897 | * layer. Return 1 to hid-core as no further processing is required. | |
534a7b8e | 898 | * |
925f0f3e BT |
899 | * 3) Data is an actual input event from a paired DJ device in which |
900 | * case we forward it to the correct hid device (via hid_input_report() | |
901 | * ) and return 1 so hid-core does not anything else with it. | |
534a7b8e | 902 | */ |
5abfe85c | 903 | |
ad3e14d7 JK |
904 | if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) || |
905 | (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) { | |
5abfe85c BT |
906 | /* |
907 | * Device index is wrong, bail out. | |
908 | * This driver can ignore safely the receiver notifications, | |
909 | * so ignore those reports too. | |
910 | */ | |
911 | if (dj_report->device_index != DJ_RECEIVER_INDEX) | |
912 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", | |
ad3e14d7 JK |
913 | __func__, dj_report->device_index); |
914 | return false; | |
915 | } | |
534a7b8e NLC |
916 | |
917 | spin_lock_irqsave(&djrcv_dev->lock, flags); | |
368d4e59 BT |
918 | |
919 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { | |
920 | /* received an event for an unknown device, bail out */ | |
921 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); | |
922 | goto out; | |
923 | } | |
924 | ||
5abfe85c BT |
925 | switch (dj_report->report_type) { |
926 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: | |
368d4e59 BT |
927 | /* pairing notifications are handled above the switch */ |
928 | break; | |
5abfe85c BT |
929 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
930 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); | |
931 | break; | |
932 | case REPORT_TYPE_NOTIF_CONNECTION_STATUS: | |
933 | if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] == | |
934 | STATUS_LINKLOSS) { | |
935 | logi_dj_recv_forward_null_report(djrcv_dev, dj_report); | |
534a7b8e | 936 | } |
5abfe85c BT |
937 | break; |
938 | default: | |
83898234 | 939 | logi_dj_recv_forward_dj(djrcv_dev, dj_report); |
534a7b8e | 940 | } |
368d4e59 BT |
941 | |
942 | out: | |
534a7b8e NLC |
943 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
944 | ||
5abfe85c | 945 | return true; |
534a7b8e NLC |
946 | } |
947 | ||
925f0f3e BT |
948 | static int logi_dj_hidpp_event(struct hid_device *hdev, |
949 | struct hid_report *report, u8 *data, | |
950 | int size) | |
951 | { | |
952 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); | |
7bb56a5f | 953 | struct hidpp_event *hidpp_report = (struct hidpp_event *) data; |
925f0f3e | 954 | unsigned long flags; |
7bb56a5f | 955 | u8 device_index = hidpp_report->device_index; |
925f0f3e | 956 | |
33797820 BT |
957 | if (device_index == HIDPP_RECEIVER_INDEX) { |
958 | /* special case were the device wants to know its unifying | |
959 | * name */ | |
960 | if (size == HIDPP_REPORT_LONG_LENGTH && | |
8dba3026 BT |
961 | !memcmp(data, unifying_pairing_answer, |
962 | sizeof(unifying_pairing_answer))) | |
33797820 BT |
963 | device_index = (data[4] & 0x0F) + 1; |
964 | else | |
965 | return false; | |
966 | } | |
925f0f3e BT |
967 | |
968 | /* | |
969 | * Data is from the HID++ collection, in this case, we forward the | |
970 | * data to the corresponding child dj device and return 0 to hid-core | |
971 | * so he data also goes to the hidraw device of the receiver. This | |
972 | * allows a user space application to implement the full HID++ routing | |
973 | * via the receiver. | |
974 | */ | |
975 | ||
976 | if ((device_index < DJ_DEVICE_INDEX_MIN) || | |
977 | (device_index > DJ_DEVICE_INDEX_MAX)) { | |
978 | /* | |
979 | * Device index is wrong, bail out. | |
980 | * This driver can ignore safely the receiver notifications, | |
981 | * so ignore those reports too. | |
982 | */ | |
983 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", | |
7bb56a5f | 984 | __func__, hidpp_report->device_index); |
925f0f3e BT |
985 | return false; |
986 | } | |
987 | ||
988 | spin_lock_irqsave(&djrcv_dev->lock, flags); | |
989 | ||
990 | if (!djrcv_dev->paired_dj_devices[device_index]) | |
991 | /* received an event for an unknown device, bail out */ | |
992 | goto out; | |
993 | ||
83898234 BT |
994 | logi_dj_recv_forward_report(djrcv_dev->paired_dj_devices[device_index], |
995 | data, size); | |
925f0f3e BT |
996 | |
997 | out: | |
998 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); | |
999 | ||
1000 | return false; | |
1001 | } | |
1002 | ||
1003 | static int logi_dj_raw_event(struct hid_device *hdev, | |
1004 | struct hid_report *report, u8 *data, | |
1005 | int size) | |
1006 | { | |
1007 | dbg_hid("%s, size:%d\n", __func__, size); | |
1008 | ||
1009 | switch (data[0]) { | |
1010 | case REPORT_ID_DJ_SHORT: | |
f254ae93 PW |
1011 | if (size != DJREPORT_SHORT_LENGTH) { |
1012 | dev_err(&hdev->dev, "DJ report of bad size (%d)", size); | |
1013 | return false; | |
1014 | } | |
925f0f3e BT |
1015 | return logi_dj_dj_event(hdev, report, data, size); |
1016 | case REPORT_ID_HIDPP_SHORT: | |
f254ae93 PW |
1017 | if (size != HIDPP_REPORT_SHORT_LENGTH) { |
1018 | dev_err(&hdev->dev, | |
1019 | "Short HID++ report of bad size (%d)", size); | |
1020 | return false; | |
1021 | } | |
1022 | return logi_dj_hidpp_event(hdev, report, data, size); | |
925f0f3e | 1023 | case REPORT_ID_HIDPP_LONG: |
f254ae93 PW |
1024 | if (size != HIDPP_REPORT_LONG_LENGTH) { |
1025 | dev_err(&hdev->dev, | |
1026 | "Long HID++ report of bad size (%d)", size); | |
1027 | return false; | |
1028 | } | |
925f0f3e BT |
1029 | return logi_dj_hidpp_event(hdev, report, data, size); |
1030 | } | |
1031 | ||
1032 | return false; | |
1033 | } | |
1034 | ||
534a7b8e NLC |
1035 | static int logi_dj_probe(struct hid_device *hdev, |
1036 | const struct hid_device_id *id) | |
1037 | { | |
82c0beb8 BT |
1038 | struct hid_report_enum *rep_enum; |
1039 | struct hid_report *rep; | |
534a7b8e | 1040 | struct dj_receiver_dev *djrcv_dev; |
82c0beb8 | 1041 | bool has_hidpp = false; |
534a7b8e NLC |
1042 | int retval; |
1043 | ||
82c0beb8 BT |
1044 | /* |
1045 | * Call to usbhid to fetch the HID descriptors of the current | |
1046 | * interface subsequently call to the hid/hid-core to parse the | |
1047 | * fetched descriptors. | |
1048 | */ | |
1049 | retval = hid_parse(hdev); | |
1050 | if (retval) { | |
1051 | dev_err(&hdev->dev, | |
1052 | "%s:parse failed\n", __func__); | |
1053 | return retval; | |
1054 | } | |
534a7b8e | 1055 | |
82c0beb8 BT |
1056 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; |
1057 | ||
1058 | /* | |
1059 | * Check for the HID++ application. | |
1060 | * Note: we should theoretically check for HID++ and DJ | |
1061 | * collections, but this will do. | |
1062 | */ | |
1063 | list_for_each_entry(rep, &rep_enum->report_list, list) { | |
1064 | if (rep->application == 0xff000001) | |
1065 | has_hidpp = true; | |
534a7b8e NLC |
1066 | } |
1067 | ||
82c0beb8 BT |
1068 | /* |
1069 | * Ignore interfaces without DJ/HID++ collection, they will not carry | |
1070 | * any data, dont create any hid_device for them. | |
1071 | */ | |
1072 | if (!has_hidpp) | |
1073 | return -ENODEV; | |
1074 | ||
1075 | /* Treat DJ/HID++ interface */ | |
534a7b8e NLC |
1076 | |
1077 | djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL); | |
1078 | if (!djrcv_dev) { | |
1079 | dev_err(&hdev->dev, | |
1080 | "%s:failed allocating dj_receiver_dev\n", __func__); | |
1081 | return -ENOMEM; | |
1082 | } | |
1083 | djrcv_dev->hdev = hdev; | |
1084 | INIT_WORK(&djrcv_dev->work, delayedwork_callback); | |
1085 | spin_lock_init(&djrcv_dev->lock); | |
1086 | if (kfifo_alloc(&djrcv_dev->notif_fifo, | |
4fcad95a | 1087 | DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem), |
534a7b8e NLC |
1088 | GFP_KERNEL)) { |
1089 | dev_err(&hdev->dev, | |
1090 | "%s:failed allocating notif_fifo\n", __func__); | |
1091 | kfree(djrcv_dev); | |
1092 | return -ENOMEM; | |
1093 | } | |
1094 | hid_set_drvdata(hdev, djrcv_dev); | |
1095 | ||
297502ab | 1096 | |
534a7b8e NLC |
1097 | /* Starts the usb device and connects to upper interfaces hiddev and |
1098 | * hidraw */ | |
1099 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
1100 | if (retval) { | |
1101 | dev_err(&hdev->dev, | |
1102 | "%s:hid_hw_start returned error\n", __func__); | |
1103 | goto hid_hw_start_fail; | |
1104 | } | |
1105 | ||
1106 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); | |
1107 | if (retval < 0) { | |
1108 | dev_err(&hdev->dev, | |
1109 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", | |
1110 | __func__, retval); | |
1111 | goto switch_to_dj_mode_fail; | |
1112 | } | |
1113 | ||
1114 | /* This is enabling the polling urb on the IN endpoint */ | |
ddf7540e | 1115 | retval = hid_hw_open(hdev); |
534a7b8e | 1116 | if (retval < 0) { |
ddf7540e BT |
1117 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", |
1118 | __func__, retval); | |
534a7b8e NLC |
1119 | goto llopen_failed; |
1120 | } | |
1121 | ||
a9dd22b7 AR |
1122 | /* Allow incoming packets to arrive: */ |
1123 | hid_device_io_start(hdev); | |
1124 | ||
534a7b8e NLC |
1125 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
1126 | if (retval < 0) { | |
1127 | dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices " | |
1128 | "error:%d\n", __func__, retval); | |
1129 | goto logi_dj_recv_query_paired_devices_failed; | |
1130 | } | |
1131 | ||
1132 | return retval; | |
1133 | ||
1134 | logi_dj_recv_query_paired_devices_failed: | |
ddf7540e | 1135 | hid_hw_close(hdev); |
534a7b8e NLC |
1136 | |
1137 | llopen_failed: | |
1138 | switch_to_dj_mode_fail: | |
1139 | hid_hw_stop(hdev); | |
1140 | ||
1141 | hid_hw_start_fail: | |
534a7b8e NLC |
1142 | kfifo_free(&djrcv_dev->notif_fifo); |
1143 | kfree(djrcv_dev); | |
534a7b8e NLC |
1144 | return retval; |
1145 | ||
1146 | } | |
1147 | ||
1148 | #ifdef CONFIG_PM | |
1149 | static int logi_dj_reset_resume(struct hid_device *hdev) | |
1150 | { | |
1151 | int retval; | |
1152 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); | |
1153 | ||
1154 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); | |
1155 | if (retval < 0) { | |
1156 | dev_err(&hdev->dev, | |
1157 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", | |
1158 | __func__, retval); | |
1159 | } | |
1160 | ||
1161 | return 0; | |
1162 | } | |
1163 | #endif | |
1164 | ||
1165 | static void logi_dj_remove(struct hid_device *hdev) | |
1166 | { | |
1167 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); | |
1168 | struct dj_device *dj_dev; | |
1169 | int i; | |
1170 | ||
1171 | dbg_hid("%s\n", __func__); | |
1172 | ||
1173 | cancel_work_sync(&djrcv_dev->work); | |
1174 | ||
ddf7540e | 1175 | hid_hw_close(hdev); |
534a7b8e NLC |
1176 | hid_hw_stop(hdev); |
1177 | ||
1178 | /* I suppose that at this point the only context that can access | |
1179 | * the djrecv_data is this thread as the work item is guaranteed to | |
1180 | * have finished and no more raw_event callbacks should arrive after | |
1181 | * the remove callback was triggered so no locks are put around the | |
1182 | * code below */ | |
844580ff | 1183 | for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) { |
534a7b8e NLC |
1184 | dj_dev = djrcv_dev->paired_dj_devices[i]; |
1185 | if (dj_dev != NULL) { | |
1186 | hid_destroy_device(dj_dev->hdev); | |
1187 | kfree(dj_dev); | |
1188 | djrcv_dev->paired_dj_devices[i] = NULL; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | kfifo_free(&djrcv_dev->notif_fifo); | |
1193 | kfree(djrcv_dev); | |
534a7b8e NLC |
1194 | } |
1195 | ||
534a7b8e NLC |
1196 | static const struct hid_device_id logi_dj_receivers[] = { |
1197 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, | |
1198 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)}, | |
1199 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, | |
1200 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)}, | |
1201 | {} | |
1202 | }; | |
1203 | ||
1204 | MODULE_DEVICE_TABLE(hid, logi_dj_receivers); | |
1205 | ||
1206 | static struct hid_driver logi_djreceiver_driver = { | |
1207 | .name = "logitech-djreceiver", | |
1208 | .id_table = logi_dj_receivers, | |
1209 | .probe = logi_dj_probe, | |
1210 | .remove = logi_dj_remove, | |
1211 | .raw_event = logi_dj_raw_event, | |
1212 | #ifdef CONFIG_PM | |
1213 | .reset_resume = logi_dj_reset_resume, | |
1214 | #endif | |
1215 | }; | |
1216 | ||
ab94e562 | 1217 | module_hid_driver(logi_djreceiver_driver); |
534a7b8e | 1218 | |
534a7b8e NLC |
1219 | MODULE_LICENSE("GPL"); |
1220 | MODULE_AUTHOR("Logitech"); | |
1221 | MODULE_AUTHOR("Nestor Lopez Casado"); | |
1222 | MODULE_AUTHOR("nlopezcasad@logitech.com"); |