blob: 3f6ef0630a8e64e5a25b51bdb27f1b97394d6a65 [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 Herrmann3764eaa2013-04-06 20:28:43 +020071static inline void hidp_schedule(struct hidp_session *session)
72{
73 struct sock *ctrl_sk = session->ctrl_sock->sk;
74 struct sock *intr_sk = session->intr_sock->sk;
75
76 wake_up_interruptible(sk_sleep(ctrl_sk));
77 wake_up_interruptible(sk_sleep(intr_sk));
78}
79
David Herrmann52051852013-04-06 20:28:47 +020080static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Vasiliy Kulikovd31dbf62010-10-30 18:26:31 +040082 memset(ci, 0, sizeof(*ci));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 bacpy(&ci->bdaddr, &session->bdaddr);
84
85 ci->flags = session->flags;
David Herrmanndcc07642013-04-06 20:28:40 +020086 ci->state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 ci->vendor = 0x0000;
89 ci->product = 0x0000;
90 ci->version = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (session->input) {
93 ci->vendor = session->input->id.vendor;
94 ci->product = session->input->id.product;
95 ci->version = session->input->id.version;
96 if (session->input->name)
97 strncpy(ci->name, session->input->name, 128);
98 else
99 strncpy(ci->name, "HID Boot Device", 128);
100 }
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100101
102 if (session->hid) {
103 ci->vendor = session->hid->vendor;
104 ci->product = session->hid->product;
105 ci->version = session->hid->version;
106 strncpy(ci->name, session->hid->name, 128);
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Andrew Morton91f5cca2008-02-05 03:07:58 -0800110static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
111 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned char newleds;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100114 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100116 BT_DBG("session %p type %d code %d value %d", session, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (type != EV_LED)
119 return -1;
120
121 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
122 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
123 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
124 (!!test_bit(LED_CAPSL, dev->led) << 1) |
125 (!!test_bit(LED_NUML, dev->led));
126
127 if (session->leds == newleds)
128 return 0;
129
130 session->leds = newleds;
131
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200132 skb = alloc_skb(3, GFP_ATOMIC);
133 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 BT_ERR("Can't allocate memory for new frame");
135 return -ENOMEM;
136 }
137
138 *skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
139 *skb_put(skb, 1) = 0x01;
140 *skb_put(skb, 1) = newleds;
141
142 skb_queue_tail(&session->intr_transmit, skb);
143
144 hidp_schedule(session);
145
146 return 0;
147}
148
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100149static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
150{
Marcel Holtmann5be39462007-05-09 09:15:30 +0200151 struct hid_device *hid = input_get_drvdata(dev);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100152 struct hidp_session *session = hid->driver_data;
153
154 return hidp_queue_event(session, dev, type, code, value);
155}
156
157static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
158{
Marcel Holtmann5be39462007-05-09 09:15:30 +0200159 struct hidp_session *session = input_get_drvdata(dev);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100160
161 return hidp_queue_event(session, dev, type, code, value);
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
165{
166 struct input_dev *dev = session->input;
167 unsigned char *keys = session->keys;
168 unsigned char *udata = skb->data + 1;
169 signed char *sdata = skb->data + 1;
170 int i, size = skb->len - 1;
171
172 switch (skb->data[0]) {
173 case 0x01: /* Keyboard report */
174 for (i = 0; i < 8; i++)
175 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
176
177 /* If all the key codes have been set to 0x01, it means
178 * too many keys were pressed at the same time. */
179 if (!memcmp(udata + 2, hidp_mkeyspat, 6))
180 break;
181
182 for (i = 2; i < 8; i++) {
183 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
184 if (hidp_keycode[keys[i]])
185 input_report_key(dev, hidp_keycode[keys[i]], 0);
186 else
187 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
188 }
189
190 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
191 if (hidp_keycode[udata[i]])
192 input_report_key(dev, hidp_keycode[udata[i]], 1);
193 else
194 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
195 }
196 }
197
198 memcpy(keys, udata, 8);
199 break;
200
201 case 0x02: /* Mouse report */
202 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
203 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
204 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
205 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
206 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
207
208 input_report_rel(dev, REL_X, sdata[1]);
209 input_report_rel(dev, REL_Y, sdata[2]);
210
211 if (size > 3)
212 input_report_rel(dev, REL_WHEEL, sdata[3]);
213 break;
214 }
215
216 input_sync(dev);
217}
218
Bastien Nocera6bf82682010-01-20 12:00:42 +0000219static int __hidp_send_ctrl_message(struct hidp_session *session,
Gustavo Padovan1d0c4da2012-06-09 19:22:42 -0300220 unsigned char hdr, unsigned char *data,
221 int size)
Bastien Nocera6bf82682010-01-20 12:00:42 +0000222{
223 struct sk_buff *skb;
224
225 BT_DBG("session %p data %p size %d", session, data, size);
226
David Herrmann794d1752011-08-26 14:06:02 +0200227 if (atomic_read(&session->terminate))
228 return -EIO;
229
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200230 skb = alloc_skb(size + 1, GFP_ATOMIC);
231 if (!skb) {
Bastien Nocera6bf82682010-01-20 12:00:42 +0000232 BT_ERR("Can't allocate memory for new frame");
233 return -ENOMEM;
234 }
235
236 *skb_put(skb, 1) = hdr;
237 if (data && size > 0)
238 memcpy(skb_put(skb, size), data, size);
239
240 skb_queue_tail(&session->ctrl_transmit, skb);
241
242 return 0;
243}
244
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300245static int hidp_send_ctrl_message(struct hidp_session *session,
Bastien Nocera6bf82682010-01-20 12:00:42 +0000246 unsigned char hdr, unsigned char *data, int size)
247{
248 int err;
249
250 err = __hidp_send_ctrl_message(session, hdr, data, size);
251
252 hidp_schedule(session);
253
254 return err;
255}
256
Andrew Morton91f5cca2008-02-05 03:07:58 -0800257static int hidp_queue_report(struct hidp_session *session,
258 unsigned char *data, int size)
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100259{
260 struct sk_buff *skb;
261
Dave Young6792b5e2007-10-20 14:15:39 +0200262 BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100263
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200264 skb = alloc_skb(size + 1, GFP_ATOMIC);
265 if (!skb) {
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100266 BT_ERR("Can't allocate memory for new frame");
267 return -ENOMEM;
268 }
269
270 *skb_put(skb, 1) = 0xa2;
271 if (size > 0)
272 memcpy(skb_put(skb, size), data, size);
273
274 skb_queue_tail(&session->intr_transmit, skb);
275
276 hidp_schedule(session);
277
278 return 0;
279}
280
281static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
282{
283 unsigned char buf[32];
284 int rsize;
285
286 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
287 if (rsize > sizeof(buf))
288 return -EIO;
289
290 hid_output_report(report, buf);
291
292 return hidp_queue_report(session, buf, rsize);
293}
294
Alan Ott0ff17312011-01-18 03:04:40 -0500295static int hidp_get_raw_report(struct hid_device *hid,
296 unsigned char report_number,
297 unsigned char *data, size_t count,
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100298 unsigned char report_type)
Jiri Kosina2da31932009-11-26 16:20:56 +0100299{
Alan Ott0ff17312011-01-18 03:04:40 -0500300 struct hidp_session *session = hid->driver_data;
301 struct sk_buff *skb;
302 size_t len;
303 int numbered_reports = hid->report_enum[report_type].numbered;
David Herrmann794d1752011-08-26 14:06:02 +0200304 int ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500305
Karl Reltonfd86c9b2013-02-20 18:16:19 +0000306 if (atomic_read(&session->terminate))
307 return -EIO;
308
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100309 switch (report_type) {
310 case HID_FEATURE_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500311 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
312 break;
313 case HID_INPUT_REPORT:
314 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100315 break;
316 case HID_OUTPUT_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500317 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100318 break;
319 default:
320 return -EINVAL;
321 }
322
Alan Ott0ff17312011-01-18 03:04:40 -0500323 if (mutex_lock_interruptible(&session->report_mutex))
324 return -ERESTARTSYS;
325
326 /* Set up our wait, and send the report request to the device. */
327 session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK;
328 session->waiting_report_number = numbered_reports ? report_number : -1;
329 set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
330 data[0] = report_number;
David Herrmann794d1752011-08-26 14:06:02 +0200331 ret = hidp_send_ctrl_message(hid->driver_data, report_type, data, 1);
332 if (ret)
333 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500334
335 /* Wait for the return of the report. The returned report
336 gets put in session->report_return. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200337 while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
338 !atomic_read(&session->terminate)) {
Alan Ott0ff17312011-01-18 03:04:40 -0500339 int res;
340
341 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200342 !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)
343 || atomic_read(&session->terminate),
Alan Ott0ff17312011-01-18 03:04:40 -0500344 5*HZ);
345 if (res == 0) {
346 /* timeout */
David Herrmann794d1752011-08-26 14:06:02 +0200347 ret = -EIO;
348 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500349 }
350 if (res < 0) {
351 /* signal */
David Herrmann794d1752011-08-26 14:06:02 +0200352 ret = -ERESTARTSYS;
353 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500354 }
355 }
356
357 skb = session->report_return;
358 if (skb) {
359 len = skb->len < count ? skb->len : count;
360 memcpy(data, skb->data, len);
361
362 kfree_skb(skb);
363 session->report_return = NULL;
364 } else {
365 /* Device returned a HANDSHAKE, indicating protocol error. */
366 len = -EIO;
367 }
368
369 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
370 mutex_unlock(&session->report_mutex);
371
372 return len;
373
David Herrmann794d1752011-08-26 14:06:02 +0200374err:
Alan Ott0ff17312011-01-18 03:04:40 -0500375 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
376 mutex_unlock(&session->report_mutex);
David Herrmann794d1752011-08-26 14:06:02 +0200377 return ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
Jiri Kosina2da31932009-11-26 16:20:56 +0100381 unsigned char report_type)
382{
Alan Ott08254112011-01-18 03:04:38 -0500383 struct hidp_session *session = hid->driver_data;
384 int ret;
385
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100386 switch (report_type) {
387 case HID_FEATURE_REPORT:
388 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
389 break;
390 case HID_OUTPUT_REPORT:
Antonio Ospite97e1efb2011-02-20 18:26:46 +0100391 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100392 break;
393 default:
394 return -EINVAL;
395 }
396
Alan Ott08254112011-01-18 03:04:38 -0500397 if (mutex_lock_interruptible(&session->report_mutex))
398 return -ERESTARTSYS;
399
400 /* Set up our wait, and send the report request to the device. */
401 set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
David Herrmann794d1752011-08-26 14:06:02 +0200402 ret = hidp_send_ctrl_message(hid->driver_data, report_type, data,
403 count);
404 if (ret)
Alan Ott08254112011-01-18 03:04:38 -0500405 goto err;
Alan Ott08254112011-01-18 03:04:38 -0500406
407 /* Wait for the ACK from the device. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200408 while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) &&
409 !atomic_read(&session->terminate)) {
Alan Ott08254112011-01-18 03:04:38 -0500410 int res;
411
412 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200413 !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)
414 || atomic_read(&session->terminate),
Alan Ott08254112011-01-18 03:04:38 -0500415 10*HZ);
416 if (res == 0) {
417 /* timeout */
418 ret = -EIO;
419 goto err;
420 }
421 if (res < 0) {
422 /* signal */
423 ret = -ERESTARTSYS;
424 goto err;
425 }
426 }
427
428 if (!session->output_report_success) {
429 ret = -EIO;
430 goto err;
431 }
432
433 ret = count;
434
435err:
436 clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
437 mutex_unlock(&session->report_mutex);
438 return ret;
Jiri Kosina2da31932009-11-26 16:20:56 +0100439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void hidp_idle_timeout(unsigned long arg)
442{
443 struct hidp_session *session = (struct hidp_session *) arg;
444
David Herrmann52051852013-04-06 20:28:47 +0200445 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Andrew Morton91f5cca2008-02-05 03:07:58 -0800448static void hidp_set_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 if (session->idle_to > 0)
451 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
452}
453
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300454static void hidp_del_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 if (session->idle_to > 0)
457 del_timer(&session->timer);
458}
459
Andrew Morton91f5cca2008-02-05 03:07:58 -0800460static void hidp_process_handshake(struct hidp_session *session,
461 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 BT_DBG("session %p param 0x%02x", session, param);
Alan Ott08254112011-01-18 03:04:38 -0500464 session->output_report_success = 0; /* default condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 switch (param) {
467 case HIDP_HSHK_SUCCESSFUL:
468 /* FIXME: Call into SET_ GET_ handlers here */
Alan Ott08254112011-01-18 03:04:38 -0500469 session->output_report_success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471
472 case HIDP_HSHK_NOT_READY:
473 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
474 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
475 case HIDP_HSHK_ERR_INVALID_PARAMETER:
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300476 if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags))
Alan Ott0ff17312011-01-18 03:04:40 -0500477 wake_up_interruptible(&session->report_queue);
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* FIXME: Call into SET_ GET_ handlers here */
480 break;
481
482 case HIDP_HSHK_ERR_UNKNOWN:
483 break;
484
485 case HIDP_HSHK_ERR_FATAL:
486 /* Device requests a reboot, as this is the only way this error
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900487 * can be recovered. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 __hidp_send_ctrl_message(session,
489 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
490 break;
491
492 default:
493 __hidp_send_ctrl_message(session,
494 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
495 break;
496 }
Alan Ott08254112011-01-18 03:04:38 -0500497
498 /* Wake up the waiting thread. */
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300499 if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags))
Alan Ott08254112011-01-18 03:04:38 -0500500 wake_up_interruptible(&session->report_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Andrew Morton91f5cca2008-02-05 03:07:58 -0800503static void hidp_process_hid_control(struct hidp_session *session,
504 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 BT_DBG("session %p param 0x%02x", session, param);
507
Dave Youngeff001e2008-02-05 03:07:14 -0800508 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Flush the transmit queues */
510 skb_queue_purge(&session->ctrl_transmit);
511 skb_queue_purge(&session->intr_transmit);
512
David Herrmann52051852013-04-06 20:28:47 +0200513 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515}
516
Alan Ott0ff17312011-01-18 03:04:40 -0500517/* Returns true if the passed-in skb should be freed by the caller. */
518static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800519 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Alan Ott0ff17312011-01-18 03:04:40 -0500521 int done_with_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
523
524 switch (param) {
525 case HIDP_DATA_RTYPE_INPUT:
526 hidp_set_timer(session);
527
528 if (session->input)
529 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100530
531 if (session->hid)
532 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case HIDP_DATA_RTYPE_OTHER:
536 case HIDP_DATA_RTYPE_OUPUT:
537 case HIDP_DATA_RTYPE_FEATURE:
538 break;
539
540 default:
541 __hidp_send_ctrl_message(session,
542 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
543 }
Alan Ott0ff17312011-01-18 03:04:40 -0500544
545 if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
546 param == session->waiting_report_type) {
547 if (session->waiting_report_number < 0 ||
548 session->waiting_report_number == skb->data[0]) {
549 /* hidp_get_raw_report() is waiting on this report. */
550 session->report_return = skb;
551 done_with_skb = 0;
552 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
553 wake_up_interruptible(&session->report_queue);
554 }
555 }
556
557 return done_with_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Andrew Morton91f5cca2008-02-05 03:07:58 -0800560static void hidp_recv_ctrl_frame(struct hidp_session *session,
561 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 unsigned char hdr, type, param;
Alan Ott0ff17312011-01-18 03:04:40 -0500564 int free_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
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 type = hdr & HIDP_HEADER_TRANS_MASK;
572 param = hdr & HIDP_HEADER_PARAM_MASK;
573
574 switch (type) {
575 case HIDP_TRANS_HANDSHAKE:
576 hidp_process_handshake(session, param);
577 break;
578
579 case HIDP_TRANS_HID_CONTROL:
580 hidp_process_hid_control(session, param);
581 break;
582
583 case HIDP_TRANS_DATA:
Alan Ott0ff17312011-01-18 03:04:40 -0500584 free_skb = hidp_process_data(session, skb, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586
587 default:
588 __hidp_send_ctrl_message(session,
589 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
590 break;
591 }
592
Alan Ott0ff17312011-01-18 03:04:40 -0500593 if (free_skb)
594 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Andrew Morton91f5cca2008-02-05 03:07:58 -0800597static void hidp_recv_intr_frame(struct hidp_session *session,
598 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 unsigned char hdr;
601
602 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
603
604 hdr = skb->data[0];
605 skb_pull(skb, 1);
606
607 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
608 hidp_set_timer(session);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (session->input)
611 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100612
613 if (session->hid) {
614 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
615 BT_DBG("report len %d", skb->len);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
618 BT_DBG("Unsupported protocol header 0x%02x", hdr);
619 }
620
621 kfree_skb(skb);
622}
623
624static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
625{
626 struct kvec iv = { data, len };
627 struct msghdr msg;
628
629 BT_DBG("sock %p data %p len %d", sock, data, len);
630
631 if (!len)
632 return 0;
633
634 memset(&msg, 0, sizeof(msg));
635
636 return kernel_sendmsg(sock, &msg, &iv, 1, len);
637}
638
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300639static void hidp_process_intr_transmit(struct hidp_session *session)
640{
641 struct sk_buff *skb;
David Herrmann2df01202013-04-06 20:28:48 +0200642 int ret;
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300643
644 BT_DBG("session %p", session);
645
646 while ((skb = skb_dequeue(&session->intr_transmit))) {
David Herrmann2df01202013-04-06 20:28:48 +0200647 ret = hidp_send_frame(session->intr_sock, skb->data, skb->len);
648 if (ret == -EAGAIN) {
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300649 skb_queue_head(&session->intr_transmit, skb);
650 break;
David Herrmann2df01202013-04-06 20:28:48 +0200651 } else if (ret < 0) {
652 hidp_session_terminate(session);
653 kfree_skb(skb);
654 break;
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300655 }
656
657 hidp_set_timer(session);
658 kfree_skb(skb);
659 }
660}
661
662static void hidp_process_ctrl_transmit(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct sk_buff *skb;
David Herrmann2df01202013-04-06 20:28:48 +0200665 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 BT_DBG("session %p", session);
668
669 while ((skb = skb_dequeue(&session->ctrl_transmit))) {
David Herrmann2df01202013-04-06 20:28:48 +0200670 ret = hidp_send_frame(session->ctrl_sock, skb->data, skb->len);
671 if (ret == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 skb_queue_head(&session->ctrl_transmit, skb);
673 break;
David Herrmann2df01202013-04-06 20:28:48 +0200674 } else if (ret < 0) {
675 hidp_session_terminate(session);
676 kfree_skb(skb);
677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 hidp_set_timer(session);
681 kfree_skb(skb);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Andrew Morton91f5cca2008-02-05 03:07:58 -0800685static int hidp_setup_input(struct hidp_session *session,
686 struct hidp_connadd_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jiri Slabyc500c972008-05-16 11:49:16 +0200688 struct input_dev *input;
Gustavo F. Padovan3415a5f2011-10-06 21:17:32 -0300689 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jiri Slabyc500c972008-05-16 11:49:16 +0200691 input = input_allocate_device();
692 if (!input)
693 return -ENOMEM;
694
695 session->input = input;
696
Marcel Holtmann5be39462007-05-09 09:15:30 +0200697 input_set_drvdata(input, session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dmitry Torokhov34abf912005-09-15 02:01:40 -0500699 input->name = "Bluetooth HID Boot Protocol Device";
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 input->id.bustype = BUS_BLUETOOTH;
702 input->id.vendor = req->vendor;
703 input->id.product = req->product;
704 input->id.version = req->version;
705
706 if (req->subclass & 0x40) {
707 set_bit(EV_KEY, input->evbit);
708 set_bit(EV_LED, input->evbit);
709 set_bit(EV_REP, input->evbit);
710
711 set_bit(LED_NUML, input->ledbit);
712 set_bit(LED_CAPSL, input->ledbit);
713 set_bit(LED_SCROLLL, input->ledbit);
714 set_bit(LED_COMPOSE, input->ledbit);
715 set_bit(LED_KANA, input->ledbit);
716
717 for (i = 0; i < sizeof(hidp_keycode); i++)
718 set_bit(hidp_keycode[i], input->keybit);
719 clear_bit(0, input->keybit);
720 }
721
722 if (req->subclass & 0x80) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700723 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
724 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
725 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
726 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
727 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
728 BIT_MASK(BTN_EXTRA);
729 input->relbit[0] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
David Herrmann52051852013-04-06 20:28:47 +0200732 input->dev.parent = &session->conn->hcon->dev;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 input->event = hidp_input_event;
735
Marcel Holtmannedad6382009-08-22 14:22:15 -0700736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Marcel Holtmannf5ffd462007-02-17 23:58:53 +0100739static int hidp_open(struct hid_device *hid)
740{
741 return 0;
742}
743
744static void hidp_close(struct hid_device *hid)
745{
746}
747
Jiri Slabyc500c972008-05-16 11:49:16 +0200748static int hidp_parse(struct hid_device *hid)
749{
750 struct hidp_session *session = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200751
Michael Poole15c697c2010-02-05 12:23:43 -0500752 return hid_parse_report(session->hid, session->rd_data,
753 session->rd_size);
Jiri Slabyc500c972008-05-16 11:49:16 +0200754}
755
756static int hidp_start(struct hid_device *hid)
757{
758 struct hidp_session *session = hid->driver_data;
759 struct hid_report *report;
760
David Herrmann142c69c2011-08-26 13:27:12 +0200761 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
762 return 0;
763
Jiri Slabyc500c972008-05-16 11:49:16 +0200764 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
765 report_list, list)
766 hidp_send_report(session, report);
767
768 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
769 report_list, list)
770 hidp_send_report(session, report);
771
Jiri Slabyc500c972008-05-16 11:49:16 +0200772 return 0;
773}
774
775static void hidp_stop(struct hid_device *hid)
776{
777 struct hidp_session *session = hid->driver_data;
778
779 skb_queue_purge(&session->ctrl_transmit);
780 skb_queue_purge(&session->intr_transmit);
781
Jiri Slabyc500c972008-05-16 11:49:16 +0200782 hid->claimed = 0;
783}
784
785static struct hid_ll_driver hidp_hid_driver = {
786 .parse = hidp_parse,
787 .start = hidp_start,
788 .stop = hidp_stop,
789 .open = hidp_open,
790 .close = hidp_close,
791 .hidinput_input_event = hidp_hidinput_event,
792};
793
Alan Ott0f69dca2011-01-18 03:04:37 -0500794/* This function sets up the hid device. It does not add it
795 to the HID system. That is done in hidp_add_connection(). */
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200796static int hidp_setup_hid(struct hidp_session *session,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800797 struct hidp_connadd_req *req)
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100798{
Jiri Slabyc500c972008-05-16 11:49:16 +0200799 struct hid_device *hid;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700800 int err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100801
Michael Poole15c697c2010-02-05 12:23:43 -0500802 session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
803 if (!session->rd_data)
804 return -ENOMEM;
805
806 if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
807 err = -EFAULT;
808 goto fault;
809 }
810 session->rd_size = req->rd_size;
811
Jiri Slabyc500c972008-05-16 11:49:16 +0200812 hid = hid_allocate_device();
Michael Poole15c697c2010-02-05 12:23:43 -0500813 if (IS_ERR(hid)) {
814 err = PTR_ERR(hid);
815 goto fault;
816 }
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100817
Jiri Slabyc500c972008-05-16 11:49:16 +0200818 session->hid = hid;
Michael Poole15c697c2010-02-05 12:23:43 -0500819
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100820 hid->driver_data = session;
821
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100822 hid->bus = BUS_BLUETOOTH;
823 hid->vendor = req->vendor;
824 hid->product = req->product;
825 hid->version = req->version;
Jiri Slabyc500c972008-05-16 11:49:16 +0200826 hid->country = req->country;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100827
Anderson Lizardo0a9ab9b2013-01-06 18:28:53 -0400828 strncpy(hid->name, req->name, sizeof(req->name) - 1);
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300829
830 snprintf(hid->phys, sizeof(hid->phys), "%pMR",
831 &bt_sk(session->ctrl_sock->sk)->src);
832
833 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
834 &bt_sk(session->ctrl_sock->sk)->dst);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100835
David Herrmann52051852013-04-06 20:28:47 +0200836 hid->dev.parent = &session->conn->hcon->dev;
Jiri Slabyc500c972008-05-16 11:49:16 +0200837 hid->ll_driver = &hidp_hid_driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200838
Alan Ott0ff17312011-01-18 03:04:40 -0500839 hid->hid_get_raw_report = hidp_get_raw_report;
Jiri Kosina2da31932009-11-26 16:20:56 +0100840 hid->hid_output_raw_report = hidp_output_raw_report;
841
Lamarque V. Souza4529eef2012-12-06 12:39:55 -0200842 /* True if device is blacklisted in drivers/hid/hid-core.c */
843 if (hid_ignore(hid)) {
844 hid_destroy_device(session->hid);
845 session->hid = NULL;
846 return -ENODEV;
847 }
848
Jiri Slabyc500c972008-05-16 11:49:16 +0200849 return 0;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700850
Michael Poole15c697c2010-02-05 12:23:43 -0500851fault:
852 kfree(session->rd_data);
853 session->rd_data = NULL;
854
Marcel Holtmannedad6382009-08-22 14:22:15 -0700855 return err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100856}
857
David Herrmannb4f34d82013-04-06 20:28:46 +0200858/* initialize session devices */
859static int hidp_session_dev_init(struct hidp_session *session,
860 struct hidp_connadd_req *req)
861{
862 int ret;
863
864 if (req->rd_size > 0) {
865 ret = hidp_setup_hid(session, req);
866 if (ret && ret != -ENODEV)
867 return ret;
868 }
869
870 if (!session->hid) {
871 ret = hidp_setup_input(session, req);
872 if (ret < 0)
873 return ret;
874 }
875
876 return 0;
877}
878
879/* destroy session devices */
880static void hidp_session_dev_destroy(struct hidp_session *session)
881{
882 if (session->hid)
883 put_device(&session->hid->dev);
884 else if (session->input)
885 input_put_device(session->input);
886
887 kfree(session->rd_data);
888 session->rd_data = NULL;
889}
890
891/* add HID/input devices to their underlying bus systems */
892static int hidp_session_dev_add(struct hidp_session *session)
893{
894 int ret;
895
896 /* Both HID and input systems drop a ref-count when unregistering the
897 * device but they don't take a ref-count when registering them. Work
898 * around this by explicitly taking a refcount during registration
899 * which is dropped automatically by unregistering the devices. */
900
901 if (session->hid) {
902 ret = hid_add_device(session->hid);
903 if (ret)
904 return ret;
905 get_device(&session->hid->dev);
906 } else if (session->input) {
907 ret = input_register_device(session->input);
908 if (ret)
909 return ret;
910 input_get_device(session->input);
911 }
912
913 return 0;
914}
915
916/* remove HID/input devices from their bus systems */
917static void hidp_session_dev_del(struct hidp_session *session)
918{
919 if (session->hid)
920 hid_destroy_device(session->hid);
921 else if (session->input)
922 input_unregister_device(session->input);
923}
924
925/*
926 * Create new session object
927 * Allocate session object, initialize static fields, copy input data into the
928 * object and take a reference to all sub-objects.
929 * This returns 0 on success and puts a pointer to the new session object in
930 * \out. Otherwise, an error code is returned.
931 * The new session object has an initial ref-count of 1.
932 */
933static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr,
934 struct socket *ctrl_sock,
935 struct socket *intr_sock,
936 struct hidp_connadd_req *req,
937 struct l2cap_conn *conn)
938{
939 struct hidp_session *session;
940 int ret;
941 struct bt_sock *ctrl, *intr;
942
943 ctrl = bt_sk(ctrl_sock->sk);
944 intr = bt_sk(intr_sock->sk);
945
946 session = kzalloc(sizeof(*session), GFP_KERNEL);
947 if (!session)
948 return -ENOMEM;
949
950 /* object and runtime management */
951 kref_init(&session->ref);
952 atomic_set(&session->state, HIDP_SESSION_IDLING);
953 init_waitqueue_head(&session->state_queue);
954 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
955
956 /* connection management */
957 bacpy(&session->bdaddr, bdaddr);
958 session->conn = conn;
959 session->user.probe = hidp_session_probe;
960 session->user.remove = hidp_session_remove;
961 session->ctrl_sock = ctrl_sock;
962 session->intr_sock = intr_sock;
963 skb_queue_head_init(&session->ctrl_transmit);
964 skb_queue_head_init(&session->intr_transmit);
965 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl)->chan->omtu,
966 l2cap_pi(ctrl)->chan->imtu);
967 session->intr_mtu = min_t(uint, l2cap_pi(intr)->chan->omtu,
968 l2cap_pi(intr)->chan->imtu);
969 session->idle_to = req->idle_to;
970
971 /* device management */
972 setup_timer(&session->timer, hidp_idle_timeout,
973 (unsigned long)session);
974
975 /* session data */
976 mutex_init(&session->report_mutex);
977 init_waitqueue_head(&session->report_queue);
978
979 ret = hidp_session_dev_init(session, req);
980 if (ret)
981 goto err_free;
982
983 l2cap_conn_get(session->conn);
984 get_file(session->intr_sock->file);
985 get_file(session->ctrl_sock->file);
986 *out = session;
987 return 0;
988
989err_free:
990 kfree(session);
991 return ret;
992}
993
994/* increase ref-count of the given session by one */
995static void hidp_session_get(struct hidp_session *session)
996{
997 kref_get(&session->ref);
998}
999
1000/* release callback */
1001static void session_free(struct kref *ref)
1002{
1003 struct hidp_session *session = container_of(ref, struct hidp_session,
1004 ref);
1005
1006 hidp_session_dev_destroy(session);
1007 skb_queue_purge(&session->ctrl_transmit);
1008 skb_queue_purge(&session->intr_transmit);
1009 fput(session->intr_sock->file);
1010 fput(session->ctrl_sock->file);
1011 l2cap_conn_put(session->conn);
1012 kfree(session);
1013}
1014
1015/* decrease ref-count of the given session by one */
1016static void hidp_session_put(struct hidp_session *session)
1017{
1018 kref_put(&session->ref, session_free);
1019}
1020
1021/*
1022 * Search the list of active sessions for a session with target address
1023 * \bdaddr. You must hold at least a read-lock on \hidp_session_sem. As long as
1024 * you do not release this lock, the session objects cannot vanish and you can
1025 * safely take a reference to the session yourself.
1026 */
1027static struct hidp_session *__hidp_session_find(const bdaddr_t *bdaddr)
1028{
1029 struct hidp_session *session;
1030
1031 list_for_each_entry(session, &hidp_session_list, list) {
1032 if (!bacmp(bdaddr, &session->bdaddr))
1033 return session;
1034 }
1035
1036 return NULL;
1037}
1038
1039/*
1040 * Same as __hidp_session_find() but no locks must be held. This also takes a
1041 * reference of the returned session (if non-NULL) so you must drop this
1042 * reference if you no longer use the object.
1043 */
1044static struct hidp_session *hidp_session_find(const bdaddr_t *bdaddr)
1045{
1046 struct hidp_session *session;
1047
1048 down_read(&hidp_session_sem);
1049
1050 session = __hidp_session_find(bdaddr);
1051 if (session)
1052 hidp_session_get(session);
1053
1054 up_read(&hidp_session_sem);
1055
1056 return session;
1057}
1058
1059/*
1060 * Start session synchronously
1061 * This starts a session thread and waits until initialization
1062 * is done or returns an error if it couldn't be started.
1063 * If this returns 0 the session thread is up and running. You must call
1064 * hipd_session_stop_sync() before deleting any runtime resources.
1065 */
1066static int hidp_session_start_sync(struct hidp_session *session)
1067{
1068 unsigned int vendor, product;
1069
1070 if (session->hid) {
1071 vendor = session->hid->vendor;
1072 product = session->hid->product;
1073 } else if (session->input) {
1074 vendor = session->input->id.vendor;
1075 product = session->input->id.product;
1076 } else {
1077 vendor = 0x0000;
1078 product = 0x0000;
1079 }
1080
1081 session->task = kthread_run(hidp_session_thread, session,
1082 "khidpd_%04x%04x", vendor, product);
1083 if (IS_ERR(session->task))
1084 return PTR_ERR(session->task);
1085
1086 while (atomic_read(&session->state) <= HIDP_SESSION_IDLING)
1087 wait_event(session->state_queue,
1088 atomic_read(&session->state) > HIDP_SESSION_IDLING);
1089
1090 return 0;
1091}
1092
1093/*
1094 * Terminate session thread
1095 * Wake up session thread and notify it to stop. This is asynchronous and
1096 * returns immediately. Call this whenever a runtime error occurs and you want
1097 * the session to stop.
1098 * Note: wake_up_process() performs any necessary memory-barriers for us.
1099 */
1100static void hidp_session_terminate(struct hidp_session *session)
1101{
1102 atomic_inc(&session->terminate);
1103 wake_up_process(session->task);
1104}
1105
1106/*
1107 * Probe HIDP session
1108 * This is called from the l2cap_conn core when our l2cap_user object is bound
1109 * to the hci-connection. We get the session via the \user object and can now
1110 * start the session thread, register the HID/input devices and link it into
1111 * the global session list.
1112 * The global session-list owns its own reference to the session object so you
1113 * can drop your own reference after registering the l2cap_user object.
1114 */
1115static int hidp_session_probe(struct l2cap_conn *conn,
1116 struct l2cap_user *user)
1117{
1118 struct hidp_session *session = container_of(user,
1119 struct hidp_session,
1120 user);
1121 struct hidp_session *s;
1122 int ret;
1123
1124 down_write(&hidp_session_sem);
1125
1126 /* check that no other session for this device exists */
1127 s = __hidp_session_find(&session->bdaddr);
1128 if (s) {
1129 ret = -EEXIST;
1130 goto out_unlock;
1131 }
1132
1133 ret = hidp_session_start_sync(session);
1134 if (ret)
1135 goto out_unlock;
1136
1137 ret = hidp_session_dev_add(session);
1138 if (ret)
1139 goto out_stop;
1140
1141 hidp_session_get(session);
1142 list_add(&session->list, &hidp_session_list);
1143 ret = 0;
1144 goto out_unlock;
1145
1146out_stop:
1147 hidp_session_terminate(session);
1148out_unlock:
1149 up_write(&hidp_session_sem);
1150 return ret;
1151}
1152
1153/*
1154 * Remove HIDP session
1155 * Called from the l2cap_conn core when either we explicitly unregistered
1156 * the l2cap_user object or if the underlying connection is shut down.
1157 * We signal the hidp-session thread to shut down, unregister the HID/input
1158 * devices and unlink the session from the global list.
1159 * This drops the reference to the session that is owned by the global
1160 * session-list.
1161 * Note: We _must_ not synchronosly wait for the session-thread to shut down.
1162 * This is, because the session-thread might be waiting for an HCI lock that is
1163 * held while we are called. Therefore, we only unregister the devices and
1164 * notify the session-thread to terminate. The thread itself owns a reference
1165 * to the session object so it can safely shut down.
1166 */
1167static void hidp_session_remove(struct l2cap_conn *conn,
1168 struct l2cap_user *user)
1169{
1170 struct hidp_session *session = container_of(user,
1171 struct hidp_session,
1172 user);
1173
1174 down_write(&hidp_session_sem);
1175
1176 hidp_session_terminate(session);
1177 hidp_session_dev_del(session);
1178 list_del(&session->list);
1179
1180 up_write(&hidp_session_sem);
1181
1182 hidp_session_put(session);
1183}
1184
1185/*
1186 * Session Worker
1187 * This performs the actual main-loop of the HIDP worker. We first check
1188 * whether the underlying connection is still alive, then parse all pending
1189 * messages and finally send all outstanding messages.
1190 */
1191static void hidp_session_run(struct hidp_session *session)
1192{
1193 struct sock *ctrl_sk = session->ctrl_sock->sk;
1194 struct sock *intr_sk = session->intr_sock->sk;
1195 struct sk_buff *skb;
1196
1197 for (;;) {
1198 /*
1199 * This thread can be woken up two ways:
1200 * - You call hidp_session_terminate() which sets the
1201 * session->terminate flag and wakes this thread up.
1202 * - Via modifying the socket state of ctrl/intr_sock. This
1203 * thread is woken up by ->sk_state_changed().
1204 *
1205 * Note: set_current_state() performs any necessary
1206 * memory-barriers for us.
1207 */
1208 set_current_state(TASK_INTERRUPTIBLE);
1209
1210 if (atomic_read(&session->terminate))
1211 break;
1212
1213 if (ctrl_sk->sk_state != BT_CONNECTED ||
1214 intr_sk->sk_state != BT_CONNECTED)
1215 break;
1216
1217 /* parse incoming intr-skbs */
1218 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
1219 skb_orphan(skb);
1220 if (!skb_linearize(skb))
1221 hidp_recv_intr_frame(session, skb);
1222 else
1223 kfree_skb(skb);
1224 }
1225
1226 /* send pending intr-skbs */
1227 hidp_process_intr_transmit(session);
1228
1229 /* parse incoming ctrl-skbs */
1230 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
1231 skb_orphan(skb);
1232 if (!skb_linearize(skb))
1233 hidp_recv_ctrl_frame(session, skb);
1234 else
1235 kfree_skb(skb);
1236 }
1237
1238 /* send pending ctrl-skbs */
1239 hidp_process_ctrl_transmit(session);
1240
1241 schedule();
1242 }
1243
1244 atomic_inc(&session->terminate);
1245 set_current_state(TASK_RUNNING);
1246}
1247
1248/*
1249 * HIDP session thread
1250 * This thread runs the I/O for a single HIDP session. Startup is synchronous
1251 * which allows us to take references to ourself here instead of doing that in
1252 * the caller.
1253 * When we are ready to run we notify the caller and call hidp_session_run().
1254 */
1255static int hidp_session_thread(void *arg)
1256{
1257 struct hidp_session *session = arg;
1258 wait_queue_t ctrl_wait, intr_wait;
1259
1260 BT_DBG("session %p", session);
1261
1262 /* initialize runtime environment */
1263 hidp_session_get(session);
1264 __module_get(THIS_MODULE);
1265 set_user_nice(current, -15);
1266 hidp_set_timer(session);
1267
1268 init_waitqueue_entry(&ctrl_wait, current);
1269 init_waitqueue_entry(&intr_wait, current);
1270 add_wait_queue(sk_sleep(session->ctrl_sock->sk), &ctrl_wait);
1271 add_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1272 /* This memory barrier is paired with wq_has_sleeper(). See
1273 * sock_poll_wait() for more information why this is needed. */
1274 smp_mb();
1275
1276 /* notify synchronous startup that we're ready */
1277 atomic_inc(&session->state);
1278 wake_up(&session->state_queue);
1279
1280 /* run session */
1281 hidp_session_run(session);
1282
1283 /* cleanup runtime environment */
1284 remove_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1285 remove_wait_queue(sk_sleep(session->intr_sock->sk), &ctrl_wait);
1286 wake_up_interruptible(&session->report_queue);
1287 hidp_del_timer(session);
1288
1289 /*
1290 * If we stopped ourself due to any internal signal, we should try to
1291 * unregister our own session here to avoid having it linger until the
1292 * parent l2cap_conn dies or user-space cleans it up.
1293 * This does not deadlock as we don't do any synchronous shutdown.
1294 * Instead, this call has the same semantics as if user-space tried to
1295 * delete the session.
1296 */
1297 l2cap_unregister_user(session->conn, &session->user);
1298 hidp_session_put(session);
1299
1300 module_put_and_exit(0);
1301 return 0;
1302}
1303
1304static int hidp_verify_sockets(struct socket *ctrl_sock,
1305 struct socket *intr_sock)
1306{
1307 struct bt_sock *ctrl, *intr;
1308 struct hidp_session *session;
1309
1310 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
1311 return -EINVAL;
1312
1313 ctrl = bt_sk(ctrl_sock->sk);
1314 intr = bt_sk(intr_sock->sk);
1315
1316 if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst))
1317 return -ENOTUNIQ;
1318 if (ctrl->sk.sk_state != BT_CONNECTED ||
1319 intr->sk.sk_state != BT_CONNECTED)
1320 return -EBADFD;
1321
1322 /* early session check, we check again during session registration */
1323 session = hidp_session_find(&ctrl->dst);
1324 if (session) {
1325 hidp_session_put(session);
1326 return -EEXIST;
1327 }
1328
1329 return 0;
1330}
1331
1332int hidp_connection_add(struct hidp_connadd_req *req,
1333 struct socket *ctrl_sock,
1334 struct socket *intr_sock)
1335{
1336 struct hidp_session *session;
1337 struct l2cap_conn *conn;
1338 struct l2cap_chan *chan = l2cap_pi(ctrl_sock->sk)->chan;
1339 int ret;
1340
1341 ret = hidp_verify_sockets(ctrl_sock, intr_sock);
1342 if (ret)
1343 return ret;
1344
1345 conn = NULL;
1346 l2cap_chan_lock(chan);
1347 if (chan->conn) {
1348 l2cap_conn_get(chan->conn);
1349 conn = chan->conn;
1350 }
1351 l2cap_chan_unlock(chan);
1352
1353 if (!conn)
1354 return -EBADFD;
1355
1356 ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock,
1357 intr_sock, req, conn);
1358 if (ret)
1359 goto out_conn;
1360
1361 ret = l2cap_register_user(conn, &session->user);
1362 if (ret)
1363 goto out_session;
1364
1365 ret = 0;
1366
1367out_session:
1368 hidp_session_put(session);
1369out_conn:
1370 l2cap_conn_put(conn);
1371 return ret;
1372}
1373
1374int hidp_connection_del(struct hidp_conndel_req *req)
1375{
1376 struct hidp_session *session;
1377
1378 session = hidp_session_find(&req->bdaddr);
1379 if (!session)
1380 return -ENOENT;
1381
1382 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG))
1383 hidp_send_ctrl_message(session,
1384 HIDP_TRANS_HID_CONTROL |
1385 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG,
1386 NULL, 0);
1387 else
1388 l2cap_unregister_user(session->conn, &session->user);
1389
1390 hidp_session_put(session);
1391
1392 return 0;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395int hidp_get_connlist(struct hidp_connlist_req *req)
1396{
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001397 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int err = 0, n = 0;
1399
1400 BT_DBG("");
1401
1402 down_read(&hidp_session_sem);
1403
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001404 list_for_each_entry(session, &hidp_session_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct hidp_conninfo ci;
1406
David Herrmann52051852013-04-06 20:28:47 +02001407 hidp_copy_session(session, &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
1410 err = -EFAULT;
1411 break;
1412 }
1413
1414 if (++n >= req->cnum)
1415 break;
1416
1417 req->ci++;
1418 }
1419 req->cnum = n;
1420
1421 up_read(&hidp_session_sem);
1422 return err;
1423}
1424
1425int hidp_get_conninfo(struct hidp_conninfo *ci)
1426{
1427 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
David Herrmann52051852013-04-06 20:28:47 +02001429 session = hidp_session_find(&ci->bdaddr);
1430 if (session) {
1431 hidp_copy_session(session, ci);
1432 hidp_session_put(session);
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
David Herrmann52051852013-04-06 20:28:47 +02001435 return session ? 0 : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438static int __init hidp_init(void)
1439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1441
Henrik Rydberg8215d552012-04-23 12:07:07 +02001442 return hidp_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445static void __exit hidp_exit(void)
1446{
1447 hidp_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
1450module_init(hidp_init);
1451module_exit(hidp_exit);
1452
1453MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
David Herrmannb4f34d82013-04-06 20:28:46 +02001454MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1456MODULE_VERSION(VERSION);
1457MODULE_LICENSE("GPL");
1458MODULE_ALIAS("bt-proto-6");