2 * HID driver for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech
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.
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.
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
25 #include <linux/device.h>
26 #include <linux/hid.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <linux/kfifo.h>
30 #include <asm/unaligned.h>
33 #define DJ_MAX_PAIRED_DEVICES 6
34 #define DJ_MAX_NUMBER_NOTIFICATIONS 8
35 #define DJ_RECEIVER_INDEX 0
36 #define DJ_DEVICE_INDEX_MIN 1
37 #define DJ_DEVICE_INDEX_MAX 6
39 #define DJREPORT_SHORT_LENGTH 15
40 #define DJREPORT_LONG_LENGTH 32
42 #define REPORT_ID_DJ_SHORT 0x20
43 #define REPORT_ID_DJ_LONG 0x21
45 #define REPORT_TYPE_RFREPORT_LAST 0x1F
47 /* Command Switch to DJ mode */
48 #define REPORT_TYPE_CMD_SWITCH 0x80
49 #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
50 #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
51 #define TIMEOUT_NO_KEEPALIVE 0x00
53 /* Command to Get the list of Paired devices */
54 #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
56 /* Device Paired Notification */
57 #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
58 #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
59 #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
60 #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
61 #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
62 #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
63 #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
65 /* Device Un-Paired Notification */
66 #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
69 /* Connection Status Notification */
70 #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
71 #define CONNECTION_STATUS_PARAM_STATUS 0x00
72 #define STATUS_LINKLOSS 0x01
74 /* Error Notification */
75 #define REPORT_TYPE_NOTIF_ERROR 0x7F
76 #define NOTIF_ERROR_PARAM_ETYPE 0x00
77 #define ETYPE_KEEPALIVE_TIMEOUT 0x01
79 /* supported DJ HID && RF report types */
80 #define REPORT_TYPE_KEYBOARD 0x01
81 #define REPORT_TYPE_MOUSE 0x02
82 #define REPORT_TYPE_CONSUMER_CONTROL 0x03
83 #define REPORT_TYPE_SYSTEM_CONTROL 0x04
84 #define REPORT_TYPE_MEDIA_CENTER 0x08
85 #define REPORT_TYPE_LEDS 0x0E
87 /* RF Report types bitfield */
88 #define STD_KEYBOARD 0x00000002
89 #define STD_MOUSE 0x00000004
90 #define MULTIMEDIA 0x00000008
91 #define POWER_KEYS 0x00000010
92 #define MEDIA_CENTER 0x00000100
93 #define KBD_LEDS 0x00004000
99 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
102 struct dj_receiver_dev {
103 struct hid_device *hdev;
104 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
105 DJ_DEVICE_INDEX_MIN];
106 struct work_struct work;
107 struct kfifo notif_fifo;
109 bool querying_devices;
113 struct hid_device *hdev;
114 struct dj_receiver_dev *dj_receiver_dev;
115 u32 reports_supported;
119 /* Keyboard descriptor (1) */
120 static const char kbd_descriptor[] = {
121 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
122 0x09, 0x06, /* USAGE (Keyboard) */
123 0xA1, 0x01, /* COLLECTION (Application) */
124 0x85, 0x01, /* REPORT_ID (1) */
125 0x95, 0x08, /* REPORT_COUNT (8) */
126 0x75, 0x01, /* REPORT_SIZE (1) */
127 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
128 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
129 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
130 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
131 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
132 0x81, 0x02, /* INPUT (Data,Var,Abs) */
133 0x95, 0x06, /* REPORT_COUNT (6) */
134 0x75, 0x08, /* REPORT_SIZE (8) */
135 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
136 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
137 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
138 0x19, 0x00, /* USAGE_MINIMUM (no event) */
139 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
140 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
141 0x85, 0x0e, /* REPORT_ID (14) */
142 0x05, 0x08, /* USAGE PAGE (LED page) */
143 0x95, 0x05, /* REPORT COUNT (5) */
144 0x75, 0x01, /* REPORT SIZE (1) */
145 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
146 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
147 0x19, 0x01, /* USAGE MINIMUM (1) */
148 0x29, 0x05, /* USAGE MAXIMUM (5) */
149 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
150 0x95, 0x01, /* REPORT COUNT (1) */
151 0x75, 0x03, /* REPORT SIZE (3) */
152 0x91, 0x01, /* OUTPUT (Constant) */
156 /* Mouse descriptor (2) */
157 static const char mse_descriptor[] = {
158 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
159 0x09, 0x02, /* USAGE (Mouse) */
160 0xA1, 0x01, /* COLLECTION (Application) */
161 0x85, 0x02, /* REPORT_ID = 2 */
162 0x09, 0x01, /* USAGE (pointer) */
163 0xA1, 0x00, /* COLLECTION (physical) */
164 0x05, 0x09, /* USAGE_PAGE (buttons) */
165 0x19, 0x01, /* USAGE_MIN (1) */
166 0x29, 0x10, /* USAGE_MAX (16) */
167 0x15, 0x00, /* LOGICAL_MIN (0) */
168 0x25, 0x01, /* LOGICAL_MAX (1) */
169 0x95, 0x10, /* REPORT_COUNT (16) */
170 0x75, 0x01, /* REPORT_SIZE (1) */
171 0x81, 0x02, /* INPUT (data var abs) */
172 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
173 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
174 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
175 0x75, 0x0C, /* REPORT_SIZE (12) */
176 0x95, 0x02, /* REPORT_COUNT (2) */
177 0x09, 0x30, /* USAGE (X) */
178 0x09, 0x31, /* USAGE (Y) */
179 0x81, 0x06, /* INPUT */
180 0x15, 0x81, /* LOGICAL_MIN (-127) */
181 0x25, 0x7F, /* LOGICAL_MAX (127) */
182 0x75, 0x08, /* REPORT_SIZE (8) */
183 0x95, 0x01, /* REPORT_COUNT (1) */
184 0x09, 0x38, /* USAGE (wheel) */
185 0x81, 0x06, /* INPUT */
186 0x05, 0x0C, /* USAGE_PAGE(consumer) */
187 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
188 0x95, 0x01, /* REPORT_COUNT (1) */
189 0x81, 0x06, /* INPUT */
190 0xC0, /* END_COLLECTION */
191 0xC0, /* END_COLLECTION */
194 /* Consumer Control descriptor (3) */
195 static const char consumer_descriptor[] = {
196 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
197 0x09, 0x01, /* USAGE (Consumer Control) */
198 0xA1, 0x01, /* COLLECTION (Application) */
199 0x85, 0x03, /* REPORT_ID = 3 */
200 0x75, 0x10, /* REPORT_SIZE (16) */
201 0x95, 0x02, /* REPORT_COUNT (2) */
202 0x15, 0x01, /* LOGICAL_MIN (1) */
203 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
204 0x19, 0x01, /* USAGE_MIN (1) */
205 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
206 0x81, 0x00, /* INPUT (Data Ary Abs) */
207 0xC0, /* END_COLLECTION */
210 /* System control descriptor (4) */
211 static const char syscontrol_descriptor[] = {
212 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
213 0x09, 0x80, /* USAGE (System Control) */
214 0xA1, 0x01, /* COLLECTION (Application) */
215 0x85, 0x04, /* REPORT_ID = 4 */
216 0x75, 0x02, /* REPORT_SIZE (2) */
217 0x95, 0x01, /* REPORT_COUNT (1) */
218 0x15, 0x01, /* LOGICAL_MIN (1) */
219 0x25, 0x03, /* LOGICAL_MAX (3) */
220 0x09, 0x82, /* USAGE (System Sleep) */
221 0x09, 0x81, /* USAGE (System Power Down) */
222 0x09, 0x83, /* USAGE (System Wake Up) */
223 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
224 0x75, 0x06, /* REPORT_SIZE (6) */
225 0x81, 0x03, /* INPUT (Cnst Var Abs) */
226 0xC0, /* END_COLLECTION */
229 /* Media descriptor (8) */
230 static const char media_descriptor[] = {
231 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
232 0x09, 0x88, /* Usage 0x0088 */
233 0xa1, 0x01, /* BeginCollection */
234 0x85, 0x08, /* Report ID 8 */
235 0x19, 0x01, /* Usage Min 0x0001 */
236 0x29, 0xff, /* Usage Max 0x00ff */
237 0x15, 0x01, /* Logical Min 1 */
238 0x26, 0xff, 0x00, /* Logical Max 255 */
239 0x75, 0x08, /* Report Size 8 */
240 0x95, 0x01, /* Report Count 1 */
241 0x81, 0x00, /* Input */
242 0xc0, /* EndCollection */
245 /* Maximum size of all defined hid reports in bytes (including report id) */
246 #define MAX_REPORT_SIZE 8
248 /* Make sure all descriptors are present here */
249 #define MAX_RDESC_SIZE \
250 (sizeof(kbd_descriptor) + \
251 sizeof(mse_descriptor) + \
252 sizeof(consumer_descriptor) + \
253 sizeof(syscontrol_descriptor) + \
254 sizeof(media_descriptor))
256 /* Number of possible hid report types that can be created by this driver.
258 * Right now, RF report types have the same report types (or report id's)
259 * than the hid report created from those RF reports. In the future
260 * this doesnt have to be true.
262 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
263 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
264 * reports and consumer control, etc. If a new RF report is created, it doesn't
265 * has to have the same report id as its corresponding hid report, so an
266 * translation may have to take place for future report types.
268 #define NUMBER_OF_HID_REPORTS 32
269 static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
270 [1] = 8, /* Standard keyboard */
271 [2] = 8, /* Standard mouse */
272 [3] = 5, /* Consumer control */
273 [4] = 2, /* System control */
274 [8] = 2, /* Media Center */
278 #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
280 static struct hid_ll_driver logi_dj_ll_driver;
282 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
284 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
285 struct dj_report *dj_report)
287 /* Called in delayed work context */
288 struct dj_device *dj_dev;
291 spin_lock_irqsave(&djrcv_dev->lock, flags);
292 dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
293 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
294 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
296 if (dj_dev != NULL) {
297 hid_destroy_device(dj_dev->hdev);
300 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
305 static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
306 struct dj_report *dj_report)
308 /* Called in delayed work context */
309 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
310 struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
311 struct usb_device *usbdev = interface_to_usbdev(intf);
312 struct hid_device *dj_hiddev;
313 struct dj_device *dj_dev;
315 /* Device index goes from 1 to 6, we need 3 bytes to store the
316 * semicolon, the index, and a null terminator
318 unsigned char tmpstr[3];
320 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
321 SPFUNCTION_DEVICE_LIST_EMPTY) {
322 dbg_hid("%s: device list is empty\n", __func__);
323 djrcv_dev->querying_devices = false;
327 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
328 /* The device is already known. No need to reallocate it. */
329 dbg_hid("%s: device is already known\n", __func__);
333 dj_hiddev = hid_allocate_device();
334 if (IS_ERR(dj_hiddev)) {
335 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
340 dj_hiddev->ll_driver = &logi_dj_ll_driver;
342 dj_hiddev->dev.parent = &djrcv_hdev->dev;
343 dj_hiddev->bus = BUS_USB;
344 dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
346 (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
348 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
349 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
350 "Logitech Unifying Device. Wireless PID:%04x",
353 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
355 usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
356 snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
357 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
359 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
362 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
364 goto dj_device_allocate_fail;
367 dj_dev->reports_supported = get_unaligned_le32(
368 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
369 dj_dev->hdev = dj_hiddev;
370 dj_dev->dj_receiver_dev = djrcv_dev;
371 dj_dev->device_index = dj_report->device_index;
372 dj_hiddev->driver_data = dj_dev;
374 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
376 if (hid_add_device(dj_hiddev)) {
377 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
379 goto hid_add_device_fail;
385 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
387 dj_device_allocate_fail:
388 hid_destroy_device(dj_hiddev);
391 static void delayedwork_callback(struct work_struct *work)
393 struct dj_receiver_dev *djrcv_dev =
394 container_of(work, struct dj_receiver_dev, work);
396 struct dj_report dj_report;
401 dbg_hid("%s\n", __func__);
403 spin_lock_irqsave(&djrcv_dev->lock, flags);
405 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
406 sizeof(struct dj_report));
408 if (count != sizeof(struct dj_report)) {
409 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
410 "notifications available\n", __func__);
411 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
415 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
416 if (schedule_work(&djrcv_dev->work) == 0) {
417 dbg_hid("%s: did not schedule the work item, was "
418 "already queued\n", __func__);
422 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
424 switch (dj_report.report_type) {
425 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
426 logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
428 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
429 logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
432 /* A normal report (i. e. not belonging to a pair/unpair notification)
433 * arriving here, means that the report arrived but we did not have a
434 * paired dj_device associated to the report's device_index, this
435 * means that the original "device paired" notification corresponding
436 * to this dj_device never arrived to this driver. The reason is that
437 * hid-core discards all packets coming from a device while probe() is
439 if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
440 /* ok, we don't know the device, just re-ask the
441 * receiver for the list of connected devices. */
442 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
444 /* everything went fine, so just leave */
447 dev_err(&djrcv_dev->hdev->dev,
448 "%s:logi_dj_recv_query_paired_devices "
449 "error:%d\n", __func__, retval);
451 dbg_hid("%s: unexpected report type\n", __func__);
455 static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
456 struct dj_report *dj_report)
458 /* We are called from atomic context (tasklet && djrcv->lock held) */
460 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
462 if (schedule_work(&djrcv_dev->work) == 0) {
463 dbg_hid("%s: did not schedule the work item, was already "
464 "queued\n", __func__);
468 static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
469 struct dj_report *dj_report)
471 /* We are called from atomic context (tasklet && djrcv->lock held) */
473 u8 reportbuffer[MAX_REPORT_SIZE];
474 struct dj_device *djdev;
476 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
478 memset(reportbuffer, 0, sizeof(reportbuffer));
480 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
481 if (djdev->reports_supported & (1 << i)) {
483 if (hid_input_report(djdev->hdev,
486 hid_reportid_size_map[i], 1)) {
487 dbg_hid("hid_input_report error sending null "
494 static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
495 struct dj_report *dj_report)
497 /* We are called from atomic context (tasklet && djrcv->lock held) */
498 struct dj_device *dj_device;
500 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
502 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
503 (hid_reportid_size_map[dj_report->report_type] == 0)) {
504 dbg_hid("invalid report type:%x\n", dj_report->report_type);
508 if (hid_input_report(dj_device->hdev,
509 HID_INPUT_REPORT, &dj_report->report_type,
510 hid_reportid_size_map[dj_report->report_type], 1)) {
511 dbg_hid("hid_input_report error\n");
516 static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
517 struct dj_report *dj_report)
519 struct hid_device *hdev = djrcv_dev->hdev;
520 struct hid_report *report;
521 struct hid_report_enum *output_report_enum;
522 u8 *data = (u8 *)(&dj_report->device_index);
525 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
526 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
529 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
533 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
534 report->field[0]->value[i] = data[i];
536 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
541 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
543 struct dj_report *dj_report;
546 /* no need to protect djrcv_dev->querying_devices */
547 if (djrcv_dev->querying_devices)
550 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
553 dj_report->report_id = REPORT_ID_DJ_SHORT;
554 dj_report->device_index = 0xFF;
555 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
556 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
562 static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
565 struct dj_report *dj_report;
568 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
571 dj_report->report_id = REPORT_ID_DJ_SHORT;
572 dj_report->device_index = 0xFF;
573 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
574 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
575 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
576 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
580 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
581 * processing the "switch-to-dj" command while we send an other command.
582 * 50 msec should gives enough time to the receiver to be ready.
590 static int logi_dj_ll_open(struct hid_device *hid)
592 dbg_hid("%s:%s\n", __func__, hid->phys);
597 static void logi_dj_ll_close(struct hid_device *hid)
599 dbg_hid("%s:%s\n", __func__, hid->phys);
602 static int logi_dj_ll_raw_request(struct hid_device *hid,
603 unsigned char reportnum, __u8 *buf,
604 size_t count, unsigned char report_type,
607 struct dj_device *djdev = hid->driver_data;
608 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
612 if (buf[0] != REPORT_TYPE_LEDS)
615 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
619 if (count > DJREPORT_SHORT_LENGTH - 2)
620 count = DJREPORT_SHORT_LENGTH - 2;
622 out_buf[0] = REPORT_ID_DJ_SHORT;
623 out_buf[1] = djdev->device_index;
624 memcpy(out_buf + 2, buf, count);
626 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
627 DJREPORT_SHORT_LENGTH, report_type, reqtype);
633 static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
635 memcpy(rdesc + *rsize, data, size);
639 static int logi_dj_ll_parse(struct hid_device *hid)
641 struct dj_device *djdev = hid->driver_data;
642 unsigned int rsize = 0;
646 dbg_hid("%s\n", __func__);
648 djdev->hdev->version = 0x0111;
649 djdev->hdev->country = 0x00;
651 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
655 if (djdev->reports_supported & STD_KEYBOARD) {
656 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
657 __func__, djdev->reports_supported);
658 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
661 if (djdev->reports_supported & STD_MOUSE) {
662 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
663 "%x\n", __func__, djdev->reports_supported);
664 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
667 if (djdev->reports_supported & MULTIMEDIA) {
668 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
669 __func__, djdev->reports_supported);
670 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
673 if (djdev->reports_supported & POWER_KEYS) {
674 dbg_hid("%s: sending a power keys report descriptor: %x\n",
675 __func__, djdev->reports_supported);
676 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
679 if (djdev->reports_supported & MEDIA_CENTER) {
680 dbg_hid("%s: sending a media center report descriptor: %x\n",
681 __func__, djdev->reports_supported);
682 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
685 if (djdev->reports_supported & KBD_LEDS) {
686 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
687 __func__, djdev->reports_supported);
690 retval = hid_parse_report(hid, rdesc, rsize);
696 static int logi_dj_ll_start(struct hid_device *hid)
698 dbg_hid("%s\n", __func__);
702 static void logi_dj_ll_stop(struct hid_device *hid)
704 dbg_hid("%s\n", __func__);
708 static struct hid_ll_driver logi_dj_ll_driver = {
709 .parse = logi_dj_ll_parse,
710 .start = logi_dj_ll_start,
711 .stop = logi_dj_ll_stop,
712 .open = logi_dj_ll_open,
713 .close = logi_dj_ll_close,
714 .raw_request = logi_dj_ll_raw_request,
718 static int logi_dj_raw_event(struct hid_device *hdev,
719 struct hid_report *report, u8 *data,
722 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
723 struct dj_report *dj_report = (struct dj_report *) data;
726 dbg_hid("%s, size:%d\n", __func__, size);
728 /* Here we receive all data coming from iface 2, there are 4 cases:
730 * 1) Data should continue its normal processing i.e. data does not
731 * come from the DJ collection, in which case we do nothing and
732 * return 0, so hid-core can continue normal processing (will forward
733 * to associated hidraw device)
735 * 2) Data is from DJ collection, and is intended for this driver i. e.
736 * data contains arrival, departure, etc notifications, in which case
737 * we queue them for delayed processing by the work queue. We return 1
738 * to hid-core as no further processing is required from it.
740 * 3) Data is from DJ collection, and informs a connection change,
741 * if the change means rf link loss, then we must send a null report
742 * to the upper layer to discard potentially pressed keys that may be
743 * repeated forever by the input layer. Return 1 to hid-core as no
744 * further processing is required.
746 * 4) Data is from DJ collection and is an actual input event from
747 * a paired DJ device in which case we forward it to the correct hid
748 * device (via hid_input_report() ) and return 1 so hid-core does not do
749 * anything else with it.
753 if (data[0] != REPORT_ID_DJ_SHORT)
756 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
757 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
759 * Device index is wrong, bail out.
760 * This driver can ignore safely the receiver notifications,
761 * so ignore those reports too.
763 if (dj_report->device_index != DJ_RECEIVER_INDEX)
764 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
765 __func__, dj_report->device_index);
769 spin_lock_irqsave(&djrcv_dev->lock, flags);
771 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
772 /* received an event for an unknown device, bail out */
773 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
777 switch (dj_report->report_type) {
778 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
779 /* pairing notifications are handled above the switch */
781 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
782 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
784 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
785 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
787 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
791 logi_dj_recv_forward_report(djrcv_dev, dj_report);
795 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
800 static int logi_dj_probe(struct hid_device *hdev,
801 const struct hid_device_id *id)
803 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
804 struct dj_receiver_dev *djrcv_dev;
807 dbg_hid("%s called for ifnum %d\n", __func__,
808 intf->cur_altsetting->desc.bInterfaceNumber);
810 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
811 * any hid_device for them */
812 if (intf->cur_altsetting->desc.bInterfaceNumber !=
813 LOGITECH_DJ_INTERFACE_NUMBER) {
814 dbg_hid("%s: ignoring ifnum %d\n", __func__,
815 intf->cur_altsetting->desc.bInterfaceNumber);
819 /* Treat interface 2 */
821 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
824 "%s:failed allocating dj_receiver_dev\n", __func__);
827 djrcv_dev->hdev = hdev;
828 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
829 spin_lock_init(&djrcv_dev->lock);
830 if (kfifo_alloc(&djrcv_dev->notif_fifo,
831 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
834 "%s:failed allocating notif_fifo\n", __func__);
838 hid_set_drvdata(hdev, djrcv_dev);
840 /* Call to usbhid to fetch the HID descriptors of interface 2 and
841 * subsequently call to the hid/hid-core to parse the fetched
842 * descriptors, this will in turn create the hidraw and hiddev nodes
843 * for interface 2 of the receiver */
844 retval = hid_parse(hdev);
847 "%s:parse of interface 2 failed\n", __func__);
851 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
852 0, DJREPORT_SHORT_LENGTH - 1)) {
857 /* Starts the usb device and connects to upper interfaces hiddev and
859 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
862 "%s:hid_hw_start returned error\n", __func__);
863 goto hid_hw_start_fail;
866 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
869 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
871 goto switch_to_dj_mode_fail;
874 /* This is enabling the polling urb on the IN endpoint */
875 retval = hid_hw_open(hdev);
877 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
882 /* Allow incoming packets to arrive: */
883 hid_device_io_start(hdev);
885 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
887 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
888 "error:%d\n", __func__, retval);
889 goto logi_dj_recv_query_paired_devices_failed;
894 logi_dj_recv_query_paired_devices_failed:
898 switch_to_dj_mode_fail:
903 kfifo_free(&djrcv_dev->notif_fifo);
905 hid_set_drvdata(hdev, NULL);
911 static int logi_dj_reset_resume(struct hid_device *hdev)
914 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
916 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
919 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
927 static void logi_dj_remove(struct hid_device *hdev)
929 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
930 struct dj_device *dj_dev;
933 dbg_hid("%s\n", __func__);
935 cancel_work_sync(&djrcv_dev->work);
940 /* I suppose that at this point the only context that can access
941 * the djrecv_data is this thread as the work item is guaranteed to
942 * have finished and no more raw_event callbacks should arrive after
943 * the remove callback was triggered so no locks are put around the
945 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
946 dj_dev = djrcv_dev->paired_dj_devices[i];
947 if (dj_dev != NULL) {
948 hid_destroy_device(dj_dev->hdev);
950 djrcv_dev->paired_dj_devices[i] = NULL;
954 kfifo_free(&djrcv_dev->notif_fifo);
956 hid_set_drvdata(hdev, NULL);
959 static const struct hid_device_id logi_dj_receivers[] = {
960 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
961 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
962 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
963 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
967 MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
969 static struct hid_driver logi_djreceiver_driver = {
970 .name = "logitech-djreceiver",
971 .id_table = logi_dj_receivers,
972 .probe = logi_dj_probe,
973 .remove = logi_dj_remove,
974 .raw_event = logi_dj_raw_event,
976 .reset_resume = logi_dj_reset_resume,
980 module_hid_driver(logi_djreceiver_driver);
982 MODULE_LICENSE("GPL");
983 MODULE_AUTHOR("Logitech");
984 MODULE_AUTHOR("Nestor Lopez Casado");
985 MODULE_AUTHOR("nlopezcasad@logitech.com");