blob: 5fcc0389d929fafb23449ea175332ceec82e1474 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 HIDP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
David Herrmannb4f34d82013-04-06 20:28:46 +02004 Copyright (C) 2013 David Herrmann <dh.herrmann@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
9
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090014 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090019 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 SOFTWARE IS DISCLAIMED.
22*/
23
David Herrmannb4f34d82013-04-06 20:28:46 +020024#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/file.h>
Szymon Jancaabf6f82011-04-05 15:37:45 +020027#include <linux/kthread.h>
Marcel Holtmann364f6352009-08-22 14:15:53 -070028#include <linux/hidraw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/bluetooth/bluetooth.h>
Marcel Holtmann0a85b962006-07-06 13:09:02 +020031#include <net/bluetooth/hci_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/bluetooth/l2cap.h>
33
34#include "hidp.h"
35
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +010036#define VERSION "1.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static DECLARE_RWSEM(hidp_session_sem);
39static LIST_HEAD(hidp_session_list);
40
41static unsigned char hidp_keycode[256] = {
Szymon Janc17f09a72011-03-21 14:20:01 +010042 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36,
43 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45,
44 21, 44, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 28, 1,
45 14, 15, 57, 12, 13, 26, 27, 43, 43, 39, 40, 41, 51, 52,
46 53, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 87, 88,
47 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, 105, 108, 103, 69,
48 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, 72, 73,
49 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190,
50 191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135,
51 136, 113, 115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94,
52 95, 0, 0, 0, 122, 123, 90, 91, 85, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115,
59 114, 113, 150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
63
David Herrmannb4f34d82013-04-06 20:28:46 +020064static int hidp_session_probe(struct l2cap_conn *conn,
65 struct l2cap_user *user);
66static void hidp_session_remove(struct l2cap_conn *conn,
67 struct l2cap_user *user);
68static int hidp_session_thread(void *arg);
69static void hidp_session_terminate(struct hidp_session *s);
70
David Herrmann52051852013-04-06 20:28:47 +020071static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Vasiliy Kulikovd31dbf62010-10-30 18:26:31 +040073 memset(ci, 0, sizeof(*ci));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 bacpy(&ci->bdaddr, &session->bdaddr);
75
76 ci->flags = session->flags;
David Herrmanndcc07642013-04-06 20:28:40 +020077 ci->state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 ci->vendor = 0x0000;
80 ci->product = 0x0000;
81 ci->version = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (session->input) {
84 ci->vendor = session->input->id.vendor;
85 ci->product = session->input->id.product;
86 ci->version = session->input->id.version;
87 if (session->input->name)
88 strncpy(ci->name, session->input->name, 128);
89 else
90 strncpy(ci->name, "HID Boot Device", 128);
91 }
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +010092
93 if (session->hid) {
94 ci->vendor = session->hid->vendor;
95 ci->product = session->hid->product;
96 ci->version = session->hid->version;
97 strncpy(ci->name, session->hid->name, 128);
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
David Herrmann41edc0c2013-04-06 20:28:50 +0200101/* assemble skb, queue message on @transmit and wake up the session thread */
102static int hidp_send_message(struct hidp_session *session, struct socket *sock,
103 struct sk_buff_head *transmit, unsigned char hdr,
104 const unsigned char *data, int size)
105{
106 struct sk_buff *skb;
107 struct sock *sk = sock->sk;
108
109 BT_DBG("session %p data %p size %d", session, data, size);
110
111 if (atomic_read(&session->terminate))
112 return -EIO;
113
114 skb = alloc_skb(size + 1, GFP_ATOMIC);
115 if (!skb) {
116 BT_ERR("Can't allocate memory for new frame");
117 return -ENOMEM;
118 }
119
120 *skb_put(skb, 1) = hdr;
121 if (data && size > 0)
122 memcpy(skb_put(skb, size), data, size);
123
124 skb_queue_tail(transmit, skb);
125 wake_up_interruptible(sk_sleep(sk));
126
127 return 0;
128}
129
130static int hidp_send_ctrl_message(struct hidp_session *session,
131 unsigned char hdr, const unsigned char *data,
132 int size)
133{
134 return hidp_send_message(session, session->ctrl_sock,
135 &session->ctrl_transmit, hdr, data, size);
136}
137
138static int hidp_send_intr_message(struct hidp_session *session,
139 unsigned char hdr, const unsigned char *data,
140 int size)
141{
142 return hidp_send_message(session, session->intr_sock,
143 &session->intr_transmit, hdr, data, size);
144}
145
Andrew Morton91f5cca2008-02-05 03:07:58 -0800146static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
147 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 unsigned char newleds;
David Herrmann41edc0c2013-04-06 20:28:50 +0200150 unsigned char hdr, data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100152 BT_DBG("session %p type %d code %d value %d", session, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (type != EV_LED)
155 return -1;
156
157 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
158 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
159 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
160 (!!test_bit(LED_CAPSL, dev->led) << 1) |
161 (!!test_bit(LED_NUML, dev->led));
162
163 if (session->leds == newleds)
164 return 0;
165
166 session->leds = newleds;
167
David Herrmann41edc0c2013-04-06 20:28:50 +0200168 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
169 data[0] = 0x01;
170 data[1] = newleds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Herrmann41edc0c2013-04-06 20:28:50 +0200172 return hidp_send_intr_message(session, hdr, data, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100175static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
176{
Marcel Holtmann5be39462007-05-09 09:15:30 +0200177 struct hid_device *hid = input_get_drvdata(dev);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100178 struct hidp_session *session = hid->driver_data;
179
180 return hidp_queue_event(session, dev, type, code, value);
181}
182
183static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
184{
Marcel Holtmann5be39462007-05-09 09:15:30 +0200185 struct hidp_session *session = input_get_drvdata(dev);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100186
187 return hidp_queue_event(session, dev, type, code, value);
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
191{
192 struct input_dev *dev = session->input;
193 unsigned char *keys = session->keys;
194 unsigned char *udata = skb->data + 1;
195 signed char *sdata = skb->data + 1;
196 int i, size = skb->len - 1;
197
198 switch (skb->data[0]) {
199 case 0x01: /* Keyboard report */
200 for (i = 0; i < 8; i++)
201 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
202
203 /* If all the key codes have been set to 0x01, it means
204 * too many keys were pressed at the same time. */
205 if (!memcmp(udata + 2, hidp_mkeyspat, 6))
206 break;
207
208 for (i = 2; i < 8; i++) {
209 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
210 if (hidp_keycode[keys[i]])
211 input_report_key(dev, hidp_keycode[keys[i]], 0);
212 else
213 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
214 }
215
216 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
217 if (hidp_keycode[udata[i]])
218 input_report_key(dev, hidp_keycode[udata[i]], 1);
219 else
220 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
221 }
222 }
223
224 memcpy(keys, udata, 8);
225 break;
226
227 case 0x02: /* Mouse report */
228 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
229 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
230 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
231 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
232 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
233
234 input_report_rel(dev, REL_X, sdata[1]);
235 input_report_rel(dev, REL_Y, sdata[2]);
236
237 if (size > 3)
238 input_report_rel(dev, REL_WHEEL, sdata[3]);
239 break;
240 }
241
242 input_sync(dev);
243}
244
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100245static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
246{
David Herrmann41edc0c2013-04-06 20:28:50 +0200247 unsigned char buf[32], hdr;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100248 int rsize;
249
250 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
251 if (rsize > sizeof(buf))
252 return -EIO;
253
254 hid_output_report(report, buf);
David Herrmann41edc0c2013-04-06 20:28:50 +0200255 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100256
David Herrmann41edc0c2013-04-06 20:28:50 +0200257 return hidp_send_intr_message(session, hdr, buf, rsize);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100258}
259
Alan Ott0ff17312011-01-18 03:04:40 -0500260static int hidp_get_raw_report(struct hid_device *hid,
261 unsigned char report_number,
262 unsigned char *data, size_t count,
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100263 unsigned char report_type)
Jiri Kosina2da31932009-11-26 16:20:56 +0100264{
Alan Ott0ff17312011-01-18 03:04:40 -0500265 struct hidp_session *session = hid->driver_data;
266 struct sk_buff *skb;
267 size_t len;
268 int numbered_reports = hid->report_enum[report_type].numbered;
David Herrmann794d1752011-08-26 14:06:02 +0200269 int ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500270
Karl Reltonfd86c9b2013-02-20 18:16:19 +0000271 if (atomic_read(&session->terminate))
272 return -EIO;
273
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100274 switch (report_type) {
275 case HID_FEATURE_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500276 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
277 break;
278 case HID_INPUT_REPORT:
279 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100280 break;
281 case HID_OUTPUT_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500282 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100283 break;
284 default:
285 return -EINVAL;
286 }
287
Alan Ott0ff17312011-01-18 03:04:40 -0500288 if (mutex_lock_interruptible(&session->report_mutex))
289 return -ERESTARTSYS;
290
291 /* Set up our wait, and send the report request to the device. */
292 session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK;
293 session->waiting_report_number = numbered_reports ? report_number : -1;
294 set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
295 data[0] = report_number;
David Herrmann41edc0c2013-04-06 20:28:50 +0200296 ret = hidp_send_ctrl_message(session, report_type, data, 1);
David Herrmann794d1752011-08-26 14:06:02 +0200297 if (ret)
298 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500299
300 /* Wait for the return of the report. The returned report
301 gets put in session->report_return. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200302 while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
303 !atomic_read(&session->terminate)) {
Alan Ott0ff17312011-01-18 03:04:40 -0500304 int res;
305
306 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200307 !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)
308 || atomic_read(&session->terminate),
Alan Ott0ff17312011-01-18 03:04:40 -0500309 5*HZ);
310 if (res == 0) {
311 /* timeout */
David Herrmann794d1752011-08-26 14:06:02 +0200312 ret = -EIO;
313 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500314 }
315 if (res < 0) {
316 /* signal */
David Herrmann794d1752011-08-26 14:06:02 +0200317 ret = -ERESTARTSYS;
318 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500319 }
320 }
321
322 skb = session->report_return;
323 if (skb) {
324 len = skb->len < count ? skb->len : count;
325 memcpy(data, skb->data, len);
326
327 kfree_skb(skb);
328 session->report_return = NULL;
329 } else {
330 /* Device returned a HANDSHAKE, indicating protocol error. */
331 len = -EIO;
332 }
333
334 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
335 mutex_unlock(&session->report_mutex);
336
337 return len;
338
David Herrmann794d1752011-08-26 14:06:02 +0200339err:
Alan Ott0ff17312011-01-18 03:04:40 -0500340 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
341 mutex_unlock(&session->report_mutex);
David Herrmann794d1752011-08-26 14:06:02 +0200342 return ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
Jiri Kosina2da31932009-11-26 16:20:56 +0100346 unsigned char report_type)
347{
Alan Ott08254112011-01-18 03:04:38 -0500348 struct hidp_session *session = hid->driver_data;
349 int ret;
350
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100351 switch (report_type) {
352 case HID_FEATURE_REPORT:
353 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
354 break;
355 case HID_OUTPUT_REPORT:
David Herrmann41edc0c2013-04-06 20:28:50 +0200356 report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100357 break;
358 default:
359 return -EINVAL;
360 }
361
Alan Ott08254112011-01-18 03:04:38 -0500362 if (mutex_lock_interruptible(&session->report_mutex))
363 return -ERESTARTSYS;
364
365 /* Set up our wait, and send the report request to the device. */
366 set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
David Herrmann41edc0c2013-04-06 20:28:50 +0200367 ret = hidp_send_ctrl_message(session, report_type, data, count);
David Herrmann794d1752011-08-26 14:06:02 +0200368 if (ret)
Alan Ott08254112011-01-18 03:04:38 -0500369 goto err;
Alan Ott08254112011-01-18 03:04:38 -0500370
371 /* Wait for the ACK from the device. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200372 while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) &&
373 !atomic_read(&session->terminate)) {
Alan Ott08254112011-01-18 03:04:38 -0500374 int res;
375
376 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200377 !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)
378 || atomic_read(&session->terminate),
Alan Ott08254112011-01-18 03:04:38 -0500379 10*HZ);
380 if (res == 0) {
381 /* timeout */
382 ret = -EIO;
383 goto err;
384 }
385 if (res < 0) {
386 /* signal */
387 ret = -ERESTARTSYS;
388 goto err;
389 }
390 }
391
392 if (!session->output_report_success) {
393 ret = -EIO;
394 goto err;
395 }
396
397 ret = count;
398
399err:
400 clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
401 mutex_unlock(&session->report_mutex);
402 return ret;
Jiri Kosina2da31932009-11-26 16:20:56 +0100403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void hidp_idle_timeout(unsigned long arg)
406{
407 struct hidp_session *session = (struct hidp_session *) arg;
408
David Herrmann52051852013-04-06 20:28:47 +0200409 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Andrew Morton91f5cca2008-02-05 03:07:58 -0800412static void hidp_set_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 if (session->idle_to > 0)
415 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
416}
417
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300418static void hidp_del_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 if (session->idle_to > 0)
421 del_timer(&session->timer);
422}
423
Andrew Morton91f5cca2008-02-05 03:07:58 -0800424static void hidp_process_handshake(struct hidp_session *session,
425 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 BT_DBG("session %p param 0x%02x", session, param);
Alan Ott08254112011-01-18 03:04:38 -0500428 session->output_report_success = 0; /* default condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 switch (param) {
431 case HIDP_HSHK_SUCCESSFUL:
432 /* FIXME: Call into SET_ GET_ handlers here */
Alan Ott08254112011-01-18 03:04:38 -0500433 session->output_report_success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
435
436 case HIDP_HSHK_NOT_READY:
437 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
438 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
439 case HIDP_HSHK_ERR_INVALID_PARAMETER:
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300440 if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags))
Alan Ott0ff17312011-01-18 03:04:40 -0500441 wake_up_interruptible(&session->report_queue);
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* FIXME: Call into SET_ GET_ handlers here */
444 break;
445
446 case HIDP_HSHK_ERR_UNKNOWN:
447 break;
448
449 case HIDP_HSHK_ERR_FATAL:
450 /* Device requests a reboot, as this is the only way this error
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900451 * can be recovered. */
David Herrmann41edc0c2013-04-06 20:28:50 +0200452 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
454 break;
455
456 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200457 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
459 break;
460 }
Alan Ott08254112011-01-18 03:04:38 -0500461
462 /* Wake up the waiting thread. */
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300463 if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags))
Alan Ott08254112011-01-18 03:04:38 -0500464 wake_up_interruptible(&session->report_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Andrew Morton91f5cca2008-02-05 03:07:58 -0800467static void hidp_process_hid_control(struct hidp_session *session,
468 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 BT_DBG("session %p param 0x%02x", session, param);
471
Dave Youngeff001e2008-02-05 03:07:14 -0800472 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* Flush the transmit queues */
474 skb_queue_purge(&session->ctrl_transmit);
475 skb_queue_purge(&session->intr_transmit);
476
David Herrmann52051852013-04-06 20:28:47 +0200477 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479}
480
Alan Ott0ff17312011-01-18 03:04:40 -0500481/* Returns true if the passed-in skb should be freed by the caller. */
482static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800483 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Alan Ott0ff17312011-01-18 03:04:40 -0500485 int done_with_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
487
488 switch (param) {
489 case HIDP_DATA_RTYPE_INPUT:
490 hidp_set_timer(session);
491
492 if (session->input)
493 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100494
495 if (session->hid)
496 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498
499 case HIDP_DATA_RTYPE_OTHER:
500 case HIDP_DATA_RTYPE_OUPUT:
501 case HIDP_DATA_RTYPE_FEATURE:
502 break;
503
504 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200505 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
507 }
Alan Ott0ff17312011-01-18 03:04:40 -0500508
509 if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
510 param == session->waiting_report_type) {
511 if (session->waiting_report_number < 0 ||
512 session->waiting_report_number == skb->data[0]) {
513 /* hidp_get_raw_report() is waiting on this report. */
514 session->report_return = skb;
515 done_with_skb = 0;
516 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
517 wake_up_interruptible(&session->report_queue);
518 }
519 }
520
521 return done_with_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Andrew Morton91f5cca2008-02-05 03:07:58 -0800524static void hidp_recv_ctrl_frame(struct hidp_session *session,
525 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 unsigned char hdr, type, param;
Alan Ott0ff17312011-01-18 03:04:40 -0500528 int free_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
531
532 hdr = skb->data[0];
533 skb_pull(skb, 1);
534
535 type = hdr & HIDP_HEADER_TRANS_MASK;
536 param = hdr & HIDP_HEADER_PARAM_MASK;
537
538 switch (type) {
539 case HIDP_TRANS_HANDSHAKE:
540 hidp_process_handshake(session, param);
541 break;
542
543 case HIDP_TRANS_HID_CONTROL:
544 hidp_process_hid_control(session, param);
545 break;
546
547 case HIDP_TRANS_DATA:
Alan Ott0ff17312011-01-18 03:04:40 -0500548 free_skb = hidp_process_data(session, skb, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550
551 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200552 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
554 break;
555 }
556
Alan Ott0ff17312011-01-18 03:04:40 -0500557 if (free_skb)
558 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Andrew Morton91f5cca2008-02-05 03:07:58 -0800561static void hidp_recv_intr_frame(struct hidp_session *session,
562 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 unsigned char hdr;
565
566 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
567
568 hdr = skb->data[0];
569 skb_pull(skb, 1);
570
571 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
572 hidp_set_timer(session);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (session->input)
575 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100576
577 if (session->hid) {
578 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
579 BT_DBG("report len %d", skb->len);
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 } else {
582 BT_DBG("Unsupported protocol header 0x%02x", hdr);
583 }
584
585 kfree_skb(skb);
586}
587
588static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
589{
590 struct kvec iv = { data, len };
591 struct msghdr msg;
592
593 BT_DBG("sock %p data %p len %d", sock, data, len);
594
595 if (!len)
596 return 0;
597
598 memset(&msg, 0, sizeof(msg));
599
600 return kernel_sendmsg(sock, &msg, &iv, 1, len);
601}
602
David Herrmann7350e6c2013-04-06 20:28:49 +0200603/* dequeue message from @transmit and send via @sock */
604static void hidp_process_transmit(struct hidp_session *session,
605 struct sk_buff_head *transmit,
606 struct socket *sock)
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300607{
608 struct sk_buff *skb;
David Herrmann2df01202013-04-06 20:28:48 +0200609 int ret;
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300610
611 BT_DBG("session %p", session);
612
David Herrmann7350e6c2013-04-06 20:28:49 +0200613 while ((skb = skb_dequeue(transmit))) {
614 ret = hidp_send_frame(sock, skb->data, skb->len);
David Herrmann2df01202013-04-06 20:28:48 +0200615 if (ret == -EAGAIN) {
David Herrmann7350e6c2013-04-06 20:28:49 +0200616 skb_queue_head(transmit, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
David Herrmann2df01202013-04-06 20:28:48 +0200618 } else if (ret < 0) {
619 hidp_session_terminate(session);
620 kfree_skb(skb);
621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 hidp_set_timer(session);
625 kfree_skb(skb);
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Andrew Morton91f5cca2008-02-05 03:07:58 -0800629static int hidp_setup_input(struct hidp_session *session,
630 struct hidp_connadd_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jiri Slabyc500c972008-05-16 11:49:16 +0200632 struct input_dev *input;
Gustavo F. Padovan3415a5f2011-10-06 21:17:32 -0300633 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jiri Slabyc500c972008-05-16 11:49:16 +0200635 input = input_allocate_device();
636 if (!input)
637 return -ENOMEM;
638
639 session->input = input;
640
Marcel Holtmann5be39462007-05-09 09:15:30 +0200641 input_set_drvdata(input, session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dmitry Torokhov34abf912005-09-15 02:01:40 -0500643 input->name = "Bluetooth HID Boot Protocol Device";
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 input->id.bustype = BUS_BLUETOOTH;
646 input->id.vendor = req->vendor;
647 input->id.product = req->product;
648 input->id.version = req->version;
649
650 if (req->subclass & 0x40) {
651 set_bit(EV_KEY, input->evbit);
652 set_bit(EV_LED, input->evbit);
653 set_bit(EV_REP, input->evbit);
654
655 set_bit(LED_NUML, input->ledbit);
656 set_bit(LED_CAPSL, input->ledbit);
657 set_bit(LED_SCROLLL, input->ledbit);
658 set_bit(LED_COMPOSE, input->ledbit);
659 set_bit(LED_KANA, input->ledbit);
660
661 for (i = 0; i < sizeof(hidp_keycode); i++)
662 set_bit(hidp_keycode[i], input->keybit);
663 clear_bit(0, input->keybit);
664 }
665
666 if (req->subclass & 0x80) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700667 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
668 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
669 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
670 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
671 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
672 BIT_MASK(BTN_EXTRA);
673 input->relbit[0] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
David Herrmann52051852013-04-06 20:28:47 +0200676 input->dev.parent = &session->conn->hcon->dev;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 input->event = hidp_input_event;
679
Marcel Holtmannedad6382009-08-22 14:22:15 -0700680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Marcel Holtmannf5ffd462007-02-17 23:58:53 +0100683static int hidp_open(struct hid_device *hid)
684{
685 return 0;
686}
687
688static void hidp_close(struct hid_device *hid)
689{
690}
691
Jiri Slabyc500c972008-05-16 11:49:16 +0200692static int hidp_parse(struct hid_device *hid)
693{
694 struct hidp_session *session = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200695
Michael Poole15c697c2010-02-05 12:23:43 -0500696 return hid_parse_report(session->hid, session->rd_data,
697 session->rd_size);
Jiri Slabyc500c972008-05-16 11:49:16 +0200698}
699
700static int hidp_start(struct hid_device *hid)
701{
702 struct hidp_session *session = hid->driver_data;
703 struct hid_report *report;
704
David Herrmann142c69c2011-08-26 13:27:12 +0200705 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
706 return 0;
707
Jiri Slabyc500c972008-05-16 11:49:16 +0200708 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
709 report_list, list)
710 hidp_send_report(session, report);
711
712 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
713 report_list, list)
714 hidp_send_report(session, report);
715
Jiri Slabyc500c972008-05-16 11:49:16 +0200716 return 0;
717}
718
719static void hidp_stop(struct hid_device *hid)
720{
721 struct hidp_session *session = hid->driver_data;
722
723 skb_queue_purge(&session->ctrl_transmit);
724 skb_queue_purge(&session->intr_transmit);
725
Jiri Slabyc500c972008-05-16 11:49:16 +0200726 hid->claimed = 0;
727}
728
729static struct hid_ll_driver hidp_hid_driver = {
730 .parse = hidp_parse,
731 .start = hidp_start,
732 .stop = hidp_stop,
733 .open = hidp_open,
734 .close = hidp_close,
735 .hidinput_input_event = hidp_hidinput_event,
736};
737
Alan Ott0f69dca2011-01-18 03:04:37 -0500738/* This function sets up the hid device. It does not add it
739 to the HID system. That is done in hidp_add_connection(). */
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200740static int hidp_setup_hid(struct hidp_session *session,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800741 struct hidp_connadd_req *req)
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100742{
Jiri Slabyc500c972008-05-16 11:49:16 +0200743 struct hid_device *hid;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700744 int err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100745
Michael Poole15c697c2010-02-05 12:23:43 -0500746 session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
747 if (!session->rd_data)
748 return -ENOMEM;
749
750 if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
751 err = -EFAULT;
752 goto fault;
753 }
754 session->rd_size = req->rd_size;
755
Jiri Slabyc500c972008-05-16 11:49:16 +0200756 hid = hid_allocate_device();
Michael Poole15c697c2010-02-05 12:23:43 -0500757 if (IS_ERR(hid)) {
758 err = PTR_ERR(hid);
759 goto fault;
760 }
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100761
Jiri Slabyc500c972008-05-16 11:49:16 +0200762 session->hid = hid;
Michael Poole15c697c2010-02-05 12:23:43 -0500763
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100764 hid->driver_data = session;
765
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100766 hid->bus = BUS_BLUETOOTH;
767 hid->vendor = req->vendor;
768 hid->product = req->product;
769 hid->version = req->version;
Jiri Slabyc500c972008-05-16 11:49:16 +0200770 hid->country = req->country;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100771
Anderson Lizardo0a9ab9b2013-01-06 18:28:53 -0400772 strncpy(hid->name, req->name, sizeof(req->name) - 1);
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300773
774 snprintf(hid->phys, sizeof(hid->phys), "%pMR",
775 &bt_sk(session->ctrl_sock->sk)->src);
776
777 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
778 &bt_sk(session->ctrl_sock->sk)->dst);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100779
David Herrmann52051852013-04-06 20:28:47 +0200780 hid->dev.parent = &session->conn->hcon->dev;
Jiri Slabyc500c972008-05-16 11:49:16 +0200781 hid->ll_driver = &hidp_hid_driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200782
Alan Ott0ff17312011-01-18 03:04:40 -0500783 hid->hid_get_raw_report = hidp_get_raw_report;
Jiri Kosina2da31932009-11-26 16:20:56 +0100784 hid->hid_output_raw_report = hidp_output_raw_report;
785
Lamarque V. Souza4529eef2012-12-06 12:39:55 -0200786 /* True if device is blacklisted in drivers/hid/hid-core.c */
787 if (hid_ignore(hid)) {
788 hid_destroy_device(session->hid);
789 session->hid = NULL;
790 return -ENODEV;
791 }
792
Jiri Slabyc500c972008-05-16 11:49:16 +0200793 return 0;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700794
Michael Poole15c697c2010-02-05 12:23:43 -0500795fault:
796 kfree(session->rd_data);
797 session->rd_data = NULL;
798
Marcel Holtmannedad6382009-08-22 14:22:15 -0700799 return err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100800}
801
David Herrmannb4f34d82013-04-06 20:28:46 +0200802/* initialize session devices */
803static int hidp_session_dev_init(struct hidp_session *session,
804 struct hidp_connadd_req *req)
805{
806 int ret;
807
808 if (req->rd_size > 0) {
809 ret = hidp_setup_hid(session, req);
810 if (ret && ret != -ENODEV)
811 return ret;
812 }
813
814 if (!session->hid) {
815 ret = hidp_setup_input(session, req);
816 if (ret < 0)
817 return ret;
818 }
819
820 return 0;
821}
822
823/* destroy session devices */
824static void hidp_session_dev_destroy(struct hidp_session *session)
825{
826 if (session->hid)
827 put_device(&session->hid->dev);
828 else if (session->input)
829 input_put_device(session->input);
830
831 kfree(session->rd_data);
832 session->rd_data = NULL;
833}
834
835/* add HID/input devices to their underlying bus systems */
836static int hidp_session_dev_add(struct hidp_session *session)
837{
838 int ret;
839
840 /* Both HID and input systems drop a ref-count when unregistering the
841 * device but they don't take a ref-count when registering them. Work
842 * around this by explicitly taking a refcount during registration
843 * which is dropped automatically by unregistering the devices. */
844
845 if (session->hid) {
846 ret = hid_add_device(session->hid);
847 if (ret)
848 return ret;
849 get_device(&session->hid->dev);
850 } else if (session->input) {
851 ret = input_register_device(session->input);
852 if (ret)
853 return ret;
854 input_get_device(session->input);
855 }
856
857 return 0;
858}
859
860/* remove HID/input devices from their bus systems */
861static void hidp_session_dev_del(struct hidp_session *session)
862{
863 if (session->hid)
864 hid_destroy_device(session->hid);
865 else if (session->input)
866 input_unregister_device(session->input);
867}
868
869/*
870 * Create new session object
871 * Allocate session object, initialize static fields, copy input data into the
872 * object and take a reference to all sub-objects.
873 * This returns 0 on success and puts a pointer to the new session object in
874 * \out. Otherwise, an error code is returned.
875 * The new session object has an initial ref-count of 1.
876 */
877static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr,
878 struct socket *ctrl_sock,
879 struct socket *intr_sock,
880 struct hidp_connadd_req *req,
881 struct l2cap_conn *conn)
882{
883 struct hidp_session *session;
884 int ret;
885 struct bt_sock *ctrl, *intr;
886
887 ctrl = bt_sk(ctrl_sock->sk);
888 intr = bt_sk(intr_sock->sk);
889
890 session = kzalloc(sizeof(*session), GFP_KERNEL);
891 if (!session)
892 return -ENOMEM;
893
894 /* object and runtime management */
895 kref_init(&session->ref);
896 atomic_set(&session->state, HIDP_SESSION_IDLING);
897 init_waitqueue_head(&session->state_queue);
898 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
899
900 /* connection management */
901 bacpy(&session->bdaddr, bdaddr);
902 session->conn = conn;
903 session->user.probe = hidp_session_probe;
904 session->user.remove = hidp_session_remove;
905 session->ctrl_sock = ctrl_sock;
906 session->intr_sock = intr_sock;
907 skb_queue_head_init(&session->ctrl_transmit);
908 skb_queue_head_init(&session->intr_transmit);
909 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl)->chan->omtu,
910 l2cap_pi(ctrl)->chan->imtu);
911 session->intr_mtu = min_t(uint, l2cap_pi(intr)->chan->omtu,
912 l2cap_pi(intr)->chan->imtu);
913 session->idle_to = req->idle_to;
914
915 /* device management */
916 setup_timer(&session->timer, hidp_idle_timeout,
917 (unsigned long)session);
918
919 /* session data */
920 mutex_init(&session->report_mutex);
921 init_waitqueue_head(&session->report_queue);
922
923 ret = hidp_session_dev_init(session, req);
924 if (ret)
925 goto err_free;
926
927 l2cap_conn_get(session->conn);
928 get_file(session->intr_sock->file);
929 get_file(session->ctrl_sock->file);
930 *out = session;
931 return 0;
932
933err_free:
934 kfree(session);
935 return ret;
936}
937
938/* increase ref-count of the given session by one */
939static void hidp_session_get(struct hidp_session *session)
940{
941 kref_get(&session->ref);
942}
943
944/* release callback */
945static void session_free(struct kref *ref)
946{
947 struct hidp_session *session = container_of(ref, struct hidp_session,
948 ref);
949
950 hidp_session_dev_destroy(session);
951 skb_queue_purge(&session->ctrl_transmit);
952 skb_queue_purge(&session->intr_transmit);
953 fput(session->intr_sock->file);
954 fput(session->ctrl_sock->file);
955 l2cap_conn_put(session->conn);
956 kfree(session);
957}
958
959/* decrease ref-count of the given session by one */
960static void hidp_session_put(struct hidp_session *session)
961{
962 kref_put(&session->ref, session_free);
963}
964
965/*
966 * Search the list of active sessions for a session with target address
967 * \bdaddr. You must hold at least a read-lock on \hidp_session_sem. As long as
968 * you do not release this lock, the session objects cannot vanish and you can
969 * safely take a reference to the session yourself.
970 */
971static struct hidp_session *__hidp_session_find(const bdaddr_t *bdaddr)
972{
973 struct hidp_session *session;
974
975 list_for_each_entry(session, &hidp_session_list, list) {
976 if (!bacmp(bdaddr, &session->bdaddr))
977 return session;
978 }
979
980 return NULL;
981}
982
983/*
984 * Same as __hidp_session_find() but no locks must be held. This also takes a
985 * reference of the returned session (if non-NULL) so you must drop this
986 * reference if you no longer use the object.
987 */
988static struct hidp_session *hidp_session_find(const bdaddr_t *bdaddr)
989{
990 struct hidp_session *session;
991
992 down_read(&hidp_session_sem);
993
994 session = __hidp_session_find(bdaddr);
995 if (session)
996 hidp_session_get(session);
997
998 up_read(&hidp_session_sem);
999
1000 return session;
1001}
1002
1003/*
1004 * Start session synchronously
1005 * This starts a session thread and waits until initialization
1006 * is done or returns an error if it couldn't be started.
1007 * If this returns 0 the session thread is up and running. You must call
1008 * hipd_session_stop_sync() before deleting any runtime resources.
1009 */
1010static int hidp_session_start_sync(struct hidp_session *session)
1011{
1012 unsigned int vendor, product;
1013
1014 if (session->hid) {
1015 vendor = session->hid->vendor;
1016 product = session->hid->product;
1017 } else if (session->input) {
1018 vendor = session->input->id.vendor;
1019 product = session->input->id.product;
1020 } else {
1021 vendor = 0x0000;
1022 product = 0x0000;
1023 }
1024
1025 session->task = kthread_run(hidp_session_thread, session,
1026 "khidpd_%04x%04x", vendor, product);
1027 if (IS_ERR(session->task))
1028 return PTR_ERR(session->task);
1029
1030 while (atomic_read(&session->state) <= HIDP_SESSION_IDLING)
1031 wait_event(session->state_queue,
1032 atomic_read(&session->state) > HIDP_SESSION_IDLING);
1033
1034 return 0;
1035}
1036
1037/*
1038 * Terminate session thread
1039 * Wake up session thread and notify it to stop. This is asynchronous and
1040 * returns immediately. Call this whenever a runtime error occurs and you want
1041 * the session to stop.
1042 * Note: wake_up_process() performs any necessary memory-barriers for us.
1043 */
1044static void hidp_session_terminate(struct hidp_session *session)
1045{
1046 atomic_inc(&session->terminate);
1047 wake_up_process(session->task);
1048}
1049
1050/*
1051 * Probe HIDP session
1052 * This is called from the l2cap_conn core when our l2cap_user object is bound
1053 * to the hci-connection. We get the session via the \user object and can now
1054 * start the session thread, register the HID/input devices and link it into
1055 * the global session list.
1056 * The global session-list owns its own reference to the session object so you
1057 * can drop your own reference after registering the l2cap_user object.
1058 */
1059static int hidp_session_probe(struct l2cap_conn *conn,
1060 struct l2cap_user *user)
1061{
1062 struct hidp_session *session = container_of(user,
1063 struct hidp_session,
1064 user);
1065 struct hidp_session *s;
1066 int ret;
1067
1068 down_write(&hidp_session_sem);
1069
1070 /* check that no other session for this device exists */
1071 s = __hidp_session_find(&session->bdaddr);
1072 if (s) {
1073 ret = -EEXIST;
1074 goto out_unlock;
1075 }
1076
1077 ret = hidp_session_start_sync(session);
1078 if (ret)
1079 goto out_unlock;
1080
1081 ret = hidp_session_dev_add(session);
1082 if (ret)
1083 goto out_stop;
1084
1085 hidp_session_get(session);
1086 list_add(&session->list, &hidp_session_list);
1087 ret = 0;
1088 goto out_unlock;
1089
1090out_stop:
1091 hidp_session_terminate(session);
1092out_unlock:
1093 up_write(&hidp_session_sem);
1094 return ret;
1095}
1096
1097/*
1098 * Remove HIDP session
1099 * Called from the l2cap_conn core when either we explicitly unregistered
1100 * the l2cap_user object or if the underlying connection is shut down.
1101 * We signal the hidp-session thread to shut down, unregister the HID/input
1102 * devices and unlink the session from the global list.
1103 * This drops the reference to the session that is owned by the global
1104 * session-list.
1105 * Note: We _must_ not synchronosly wait for the session-thread to shut down.
1106 * This is, because the session-thread might be waiting for an HCI lock that is
1107 * held while we are called. Therefore, we only unregister the devices and
1108 * notify the session-thread to terminate. The thread itself owns a reference
1109 * to the session object so it can safely shut down.
1110 */
1111static void hidp_session_remove(struct l2cap_conn *conn,
1112 struct l2cap_user *user)
1113{
1114 struct hidp_session *session = container_of(user,
1115 struct hidp_session,
1116 user);
1117
1118 down_write(&hidp_session_sem);
1119
1120 hidp_session_terminate(session);
1121 hidp_session_dev_del(session);
1122 list_del(&session->list);
1123
1124 up_write(&hidp_session_sem);
1125
1126 hidp_session_put(session);
1127}
1128
1129/*
1130 * Session Worker
1131 * This performs the actual main-loop of the HIDP worker. We first check
1132 * whether the underlying connection is still alive, then parse all pending
1133 * messages and finally send all outstanding messages.
1134 */
1135static void hidp_session_run(struct hidp_session *session)
1136{
1137 struct sock *ctrl_sk = session->ctrl_sock->sk;
1138 struct sock *intr_sk = session->intr_sock->sk;
1139 struct sk_buff *skb;
1140
1141 for (;;) {
1142 /*
1143 * This thread can be woken up two ways:
1144 * - You call hidp_session_terminate() which sets the
1145 * session->terminate flag and wakes this thread up.
1146 * - Via modifying the socket state of ctrl/intr_sock. This
1147 * thread is woken up by ->sk_state_changed().
1148 *
1149 * Note: set_current_state() performs any necessary
1150 * memory-barriers for us.
1151 */
1152 set_current_state(TASK_INTERRUPTIBLE);
1153
1154 if (atomic_read(&session->terminate))
1155 break;
1156
1157 if (ctrl_sk->sk_state != BT_CONNECTED ||
1158 intr_sk->sk_state != BT_CONNECTED)
1159 break;
1160
1161 /* parse incoming intr-skbs */
1162 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
1163 skb_orphan(skb);
1164 if (!skb_linearize(skb))
1165 hidp_recv_intr_frame(session, skb);
1166 else
1167 kfree_skb(skb);
1168 }
1169
1170 /* send pending intr-skbs */
David Herrmann7350e6c2013-04-06 20:28:49 +02001171 hidp_process_transmit(session, &session->intr_transmit,
1172 session->intr_sock);
David Herrmannb4f34d82013-04-06 20:28:46 +02001173
1174 /* parse incoming ctrl-skbs */
1175 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
1176 skb_orphan(skb);
1177 if (!skb_linearize(skb))
1178 hidp_recv_ctrl_frame(session, skb);
1179 else
1180 kfree_skb(skb);
1181 }
1182
1183 /* send pending ctrl-skbs */
David Herrmann7350e6c2013-04-06 20:28:49 +02001184 hidp_process_transmit(session, &session->ctrl_transmit,
1185 session->ctrl_sock);
David Herrmannb4f34d82013-04-06 20:28:46 +02001186
1187 schedule();
1188 }
1189
1190 atomic_inc(&session->terminate);
1191 set_current_state(TASK_RUNNING);
1192}
1193
1194/*
1195 * HIDP session thread
1196 * This thread runs the I/O for a single HIDP session. Startup is synchronous
1197 * which allows us to take references to ourself here instead of doing that in
1198 * the caller.
1199 * When we are ready to run we notify the caller and call hidp_session_run().
1200 */
1201static int hidp_session_thread(void *arg)
1202{
1203 struct hidp_session *session = arg;
1204 wait_queue_t ctrl_wait, intr_wait;
1205
1206 BT_DBG("session %p", session);
1207
1208 /* initialize runtime environment */
1209 hidp_session_get(session);
1210 __module_get(THIS_MODULE);
1211 set_user_nice(current, -15);
1212 hidp_set_timer(session);
1213
1214 init_waitqueue_entry(&ctrl_wait, current);
1215 init_waitqueue_entry(&intr_wait, current);
1216 add_wait_queue(sk_sleep(session->ctrl_sock->sk), &ctrl_wait);
1217 add_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1218 /* This memory barrier is paired with wq_has_sleeper(). See
1219 * sock_poll_wait() for more information why this is needed. */
1220 smp_mb();
1221
1222 /* notify synchronous startup that we're ready */
1223 atomic_inc(&session->state);
1224 wake_up(&session->state_queue);
1225
1226 /* run session */
1227 hidp_session_run(session);
1228
1229 /* cleanup runtime environment */
1230 remove_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1231 remove_wait_queue(sk_sleep(session->intr_sock->sk), &ctrl_wait);
1232 wake_up_interruptible(&session->report_queue);
1233 hidp_del_timer(session);
1234
1235 /*
1236 * If we stopped ourself due to any internal signal, we should try to
1237 * unregister our own session here to avoid having it linger until the
1238 * parent l2cap_conn dies or user-space cleans it up.
1239 * This does not deadlock as we don't do any synchronous shutdown.
1240 * Instead, this call has the same semantics as if user-space tried to
1241 * delete the session.
1242 */
1243 l2cap_unregister_user(session->conn, &session->user);
1244 hidp_session_put(session);
1245
1246 module_put_and_exit(0);
1247 return 0;
1248}
1249
1250static int hidp_verify_sockets(struct socket *ctrl_sock,
1251 struct socket *intr_sock)
1252{
1253 struct bt_sock *ctrl, *intr;
1254 struct hidp_session *session;
1255
1256 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
1257 return -EINVAL;
1258
1259 ctrl = bt_sk(ctrl_sock->sk);
1260 intr = bt_sk(intr_sock->sk);
1261
1262 if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst))
1263 return -ENOTUNIQ;
1264 if (ctrl->sk.sk_state != BT_CONNECTED ||
1265 intr->sk.sk_state != BT_CONNECTED)
1266 return -EBADFD;
1267
1268 /* early session check, we check again during session registration */
1269 session = hidp_session_find(&ctrl->dst);
1270 if (session) {
1271 hidp_session_put(session);
1272 return -EEXIST;
1273 }
1274
1275 return 0;
1276}
1277
1278int hidp_connection_add(struct hidp_connadd_req *req,
1279 struct socket *ctrl_sock,
1280 struct socket *intr_sock)
1281{
1282 struct hidp_session *session;
1283 struct l2cap_conn *conn;
1284 struct l2cap_chan *chan = l2cap_pi(ctrl_sock->sk)->chan;
1285 int ret;
1286
1287 ret = hidp_verify_sockets(ctrl_sock, intr_sock);
1288 if (ret)
1289 return ret;
1290
1291 conn = NULL;
1292 l2cap_chan_lock(chan);
1293 if (chan->conn) {
1294 l2cap_conn_get(chan->conn);
1295 conn = chan->conn;
1296 }
1297 l2cap_chan_unlock(chan);
1298
1299 if (!conn)
1300 return -EBADFD;
1301
1302 ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock,
1303 intr_sock, req, conn);
1304 if (ret)
1305 goto out_conn;
1306
1307 ret = l2cap_register_user(conn, &session->user);
1308 if (ret)
1309 goto out_session;
1310
1311 ret = 0;
1312
1313out_session:
1314 hidp_session_put(session);
1315out_conn:
1316 l2cap_conn_put(conn);
1317 return ret;
1318}
1319
1320int hidp_connection_del(struct hidp_conndel_req *req)
1321{
1322 struct hidp_session *session;
1323
1324 session = hidp_session_find(&req->bdaddr);
1325 if (!session)
1326 return -ENOENT;
1327
1328 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG))
1329 hidp_send_ctrl_message(session,
1330 HIDP_TRANS_HID_CONTROL |
1331 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG,
1332 NULL, 0);
1333 else
1334 l2cap_unregister_user(session->conn, &session->user);
1335
1336 hidp_session_put(session);
1337
1338 return 0;
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341int hidp_get_connlist(struct hidp_connlist_req *req)
1342{
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001343 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int err = 0, n = 0;
1345
1346 BT_DBG("");
1347
1348 down_read(&hidp_session_sem);
1349
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001350 list_for_each_entry(session, &hidp_session_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct hidp_conninfo ci;
1352
David Herrmann52051852013-04-06 20:28:47 +02001353 hidp_copy_session(session, &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
1356 err = -EFAULT;
1357 break;
1358 }
1359
1360 if (++n >= req->cnum)
1361 break;
1362
1363 req->ci++;
1364 }
1365 req->cnum = n;
1366
1367 up_read(&hidp_session_sem);
1368 return err;
1369}
1370
1371int hidp_get_conninfo(struct hidp_conninfo *ci)
1372{
1373 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Herrmann52051852013-04-06 20:28:47 +02001375 session = hidp_session_find(&ci->bdaddr);
1376 if (session) {
1377 hidp_copy_session(session, ci);
1378 hidp_session_put(session);
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
David Herrmann52051852013-04-06 20:28:47 +02001381 return session ? 0 : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384static int __init hidp_init(void)
1385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1387
Henrik Rydberg8215d552012-04-23 12:07:07 +02001388 return hidp_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391static void __exit hidp_exit(void)
1392{
1393 hidp_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396module_init(hidp_init);
1397module_exit(hidp_exit);
1398
1399MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
David Herrmannb4f34d82013-04-06 20:28:46 +02001400MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1402MODULE_VERSION(VERSION);
1403MODULE_LICENSE("GPL");
1404MODULE_ALIAS("bt-proto-6");