2 * HIDPP protocol for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech (c)
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Should you need to contact me, the author, you can do so by e-mail send
22 * your message to Benjamin Tissoires <benjamin.tissoires at gmail com>
27 #include <linux/device.h>
28 #include <linux/hid.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include "hid-logitech-hidpp.h"
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
36 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
38 #define MAX_INIT_RETRY 5
40 enum delayed_work_type {
44 static void hidpp_print_raw_event(const char *header, u8 *data, int size)
47 unsigned char log[96];
48 unsigned char tmpstr[60];
50 snprintf(log, sizeof(tmpstr), "%s (size=%d)", header, size);
52 for (i = 0; i < size; i++) {
53 snprintf(tmpstr, sizeof(tmpstr), " %02x", data[i]);
54 strlcat(log, tmpstr, sizeof(log));
60 static int __hidpp_send_report(struct hid_device *hdev,
61 struct hidpp_report *hidpp_rept)
65 if (!hdev->hid_output_raw_report) {
66 dev_err(&hdev->dev, "%s:"
67 "hid_output_raw_report is null\n", __func__);
71 hidpp_print_raw_event("sending ", (u8 *)hidpp_rept,
72 HIDPP_REPORT_LONG_LENGTH);
73 sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) hidpp_rept,
74 sizeof(struct hidpp_report),
77 return (sent_bytes < 0) ? sent_bytes : 0;
80 static int hidpp_send_message_sync(struct hidpp_device *hidpp_dev,
81 struct hidpp_report *message,
82 struct hidpp_report *response)
86 mutex_lock(&hidpp_dev->send_mutex);
88 hidpp_dev->send_receive_buf = response;
89 hidpp_dev->answer_available = false;
91 /* So that we can later validate the answer when it arrives
92 * in hidpp_raw_event */
95 ret = __hidpp_send_report(hidpp_dev->hid_dev, message);
98 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
99 memset(response, 0, sizeof(struct hidpp_report));
103 if (!wait_event_timeout(hidpp_dev->wait, hidpp_dev->answer_available, 10*HZ)) {
104 dbg_hid("%s:timeout waiting for response\n", __func__);
105 memset(response, 0, sizeof(struct hidpp_report));
109 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
110 response->fap.feature_index == HIDPP_ERROR) {
111 ret = response->fap.params[0];
112 dbg_hid("__hidpp_send_report got hidpp error %d\n", ret);
117 mutex_unlock(&hidpp_dev->send_mutex);
122 int hidpp_send_fap_command_sync(struct hidpp_device *hidpp_dev,
123 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
124 struct hidpp_report *response)
126 struct hidpp_report message;
128 if (param_count > sizeof(message.rap.params))
131 memset(&message, 0, sizeof(message));
132 message.report_id = REPORT_ID_HIDPP_LONG;
133 message.fap.feature_index = feat_index;
134 message.fap.funcindex_clientid = funcindex_clientid;
135 memcpy(&message.fap.params, params, param_count);
137 return hidpp_send_message_sync(hidpp_dev, &message, response);
139 EXPORT_SYMBOL_GPL(hidpp_send_fap_command_sync);
141 int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
142 u8 report_id, u8 sub_id, u8 reg_address, u8 *params,
144 struct hidpp_report *response)
146 struct hidpp_report message;
148 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
149 (report_id != REPORT_ID_HIDPP_LONG))
152 if (param_count > sizeof(message.rap.params))
155 memset(&message, 0, sizeof(message));
156 message.report_id = report_id;
157 message.rap.sub_id = sub_id;
158 message.rap.reg_address = reg_address;
159 memcpy(&message.rap.params, params, param_count);
161 return hidpp_send_message_sync(hidpp_dev, &message, response);
163 EXPORT_SYMBOL_GPL(hidpp_send_rap_command_sync);
165 int hidpp_get_hidpp2_feature_index(struct hidpp_device *hidpp_dev,
170 struct hidpp_report response;
174 params[0] = feature_id >> 8;
175 params[1] = feature_id & 0xff;
176 ret = hidpp_send_hidpp2_sync(hidpp_dev,
177 REPORT_ID_HIDPP_SHORT,
186 *feature_idx = response.rap.params[0];
189 EXPORT_SYMBOL_GPL(hidpp_get_hidpp2_feature_index);
191 static void schedule_delayed_hidpp_init(struct hidpp_device *hidpp_dev)
193 enum delayed_work_type work_type = HIDPP_INIT;
195 kfifo_in(&hidpp_dev->delayed_work_fifo, &work_type,
196 sizeof(enum delayed_work_type));
198 if (schedule_work(&hidpp_dev->work) == 0) {
199 dbg_hid("%s: did not schedule the work item,"
200 " was already queued\n",
205 void hidpp_delayed_init(struct hidpp_device *hidpp_device)
207 struct hid_device *hdev = hidpp_device->hid_dev;
210 dbg_hid("%s: hdev:%p\n", __func__, hdev);
212 if (hidpp_device->initialized)
215 if (down_trylock(&hidpp_device->hid_dev->driver_lock)) {
216 if (hidpp_device->init_retry < MAX_INIT_RETRY) {
217 dbg_hid("%s: we need to reschedule the work item."
218 "Semaphore still held on device\n", __func__);
219 schedule_delayed_hidpp_init(hidpp_device);
220 hidpp_device->init_retry++;
222 dbg_hid("%s: giving up initialization now.", __func__);
223 hidpp_device->init_retry = 0;
227 up(&hidpp_device->hid_dev->driver_lock);
229 if (hidpp_device->device_init)
230 ret = hidpp_device->device_init(hidpp_device);
233 hidpp_device->initialized = true;
235 EXPORT_SYMBOL_GPL(hidpp_delayed_init);
237 static void delayed_work_cb(struct work_struct *work)
239 struct hidpp_device *hidpp_device =
240 container_of(work, struct hidpp_device, work);
243 enum delayed_work_type work_type;
245 dbg_hid("%s\n", __func__);
247 spin_lock_irqsave(&hidpp_device->lock, flags);
249 count = kfifo_out(&hidpp_device->delayed_work_fifo, &work_type,
250 sizeof(enum delayed_work_type));
252 if (count != sizeof(enum delayed_work_type)) {
253 dev_err(&hidpp_device->hid_dev->dev, "%s: workitem triggered without "
254 "notifications available\n", __func__);
255 spin_unlock_irqrestore(&hidpp_device->lock, flags);
259 if (!kfifo_is_empty(&hidpp_device->delayed_work_fifo)) {
260 if (schedule_work(&hidpp_device->work) == 0) {
261 dbg_hid("%s: did not schedule the work item, was "
262 "already queued\n", __func__);
266 spin_unlock_irqrestore(&hidpp_device->lock, flags);
270 hidpp_delayed_init(hidpp_device);
273 dbg_hid("%s: unexpected report type\n", __func__);
277 int hidpp_init(struct hidpp_device *hidpp_dev, struct hid_device *hid_dev)
279 if (hidpp_dev->initialized)
282 hidpp_dev->init_retry = 0;
283 hidpp_dev->hid_dev = hid_dev;
284 hidpp_dev->initialized = 1;
286 INIT_WORK(&hidpp_dev->work, delayed_work_cb);
287 mutex_init(&hidpp_dev->send_mutex);
288 init_waitqueue_head(&hidpp_dev->wait);
290 spin_lock_init(&hidpp_dev->lock);
291 if (kfifo_alloc(&hidpp_dev->delayed_work_fifo,
292 4 * sizeof(struct hidpp_report),
294 dev_err(&hidpp_dev->hid_dev->dev,
295 "%s:failed allocating delayed_work_fifo\n", __func__);
296 mutex_destroy(&hidpp_dev->send_mutex);
302 EXPORT_SYMBOL_GPL(hidpp_init);
304 void hidpp_connect_change(struct hidpp_device *hidpp_dev, bool connected)
306 if ((!hidpp_dev->initialized) && (connected))
307 hidpp_delayed_init(hidpp_dev);
309 if (hidpp_dev->connect_change)
310 hidpp_dev->connect_change(hidpp_dev, connected);
312 EXPORT_SYMBOL_GPL(hidpp_connect_change);
314 void hidpp_remove(struct hidpp_device *hidpp_dev)
316 dbg_hid("%s\n", __func__);
317 cancel_work_sync(&hidpp_dev->work);
318 mutex_destroy(&hidpp_dev->send_mutex);
319 kfifo_free(&hidpp_dev->delayed_work_fifo);
320 hidpp_dev->initialized = false;
321 hidpp_dev->hid_dev = NULL;
323 EXPORT_SYMBOL_GPL(hidpp_remove);
325 int hidpp_raw_event(struct hid_device *hdev, struct hid_report *hid_report,
328 struct hidpp_device *hidpp_dev = hid_get_drvdata(hdev);
329 struct hidpp_report *report = (struct hidpp_report *)data;
330 struct hidpp_report *question = hidpp_dev->send_receive_buf;
331 struct hidpp_report *answer = hidpp_dev->send_receive_buf;
333 dbg_hid("%s\n", __func__);
335 hidpp_print_raw_event("hidpp_raw_event", data, size);
337 if ((report->report_id != REPORT_ID_HIDPP_LONG) &&
338 (report->report_id != REPORT_ID_HIDPP_SHORT)) {
339 dbg_hid("hid-logitech-hidpp.c:%s: ignore report_id:%d\n",
340 __func__, report->report_id);
344 /* If the mutex is locked then we have a pending answer from a
345 * previoulsly sent command
347 if (unlikely(mutex_is_locked(&hidpp_dev->send_mutex))) {
348 /* Check for a correct hidpp20 answer */
349 bool correct_answer =
350 report->fap.feature_index == question->fap.feature_index &&
351 report->fap.funcindex_clientid == question->fap.funcindex_clientid;
352 dbg_hid("%s mutex is locked, waiting for reply\n", __func__);
354 /* Check for a "correct" hidpp10 error message, this means the
355 * device is hidpp10 and does not support the command sent */
356 correct_answer = correct_answer ||
357 (report->fap.feature_index == HIDPP_ERROR &&
358 report->fap.funcindex_clientid == question->fap.feature_index &&
359 report->fap.params[0] == question->fap.funcindex_clientid);
361 if (correct_answer) {
362 hidpp_print_raw_event("answer", data, size);
364 hidpp_dev->answer_available = true;
365 wake_up(&hidpp_dev->wait);
366 /* This was an answer to a command that this driver sent
367 * we return 1 to hid-core to avoid forwarding the command
368 * upstream as it has been treated by the driver */
374 if (hidpp_dev->raw_event != NULL) {
375 return hidpp_dev->raw_event(hidpp_dev, report);
378 hidpp_print_raw_event("event not treated", data, size);
382 EXPORT_SYMBOL_GPL(hidpp_raw_event);