blob: 13a0a059094724db1e8a1094031f03e61a7668d3 [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
David Herrmannaf87b3d2013-04-06 20:28:51 +0200146static int hidp_input_event(struct input_dev *dev, unsigned int type,
147 unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
David Herrmannaf87b3d2013-04-06 20:28:51 +0200149 struct hidp_session *session = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned char newleds;
David Herrmann41edc0c2013-04-06 20:28:50 +0200151 unsigned char hdr, data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Herrmannaf87b3d2013-04-06 20:28:51 +0200153 BT_DBG("session %p type %d code %d value %d",
154 session, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (type != EV_LED)
157 return -1;
158
159 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
160 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
161 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
162 (!!test_bit(LED_CAPSL, dev->led) << 1) |
163 (!!test_bit(LED_NUML, dev->led));
164
165 if (session->leds == newleds)
166 return 0;
167
168 session->leds = newleds;
169
David Herrmann41edc0c2013-04-06 20:28:50 +0200170 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
171 data[0] = 0x01;
172 data[1] = newleds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Herrmann41edc0c2013-04-06 20:28:50 +0200174 return hidp_send_intr_message(session, hdr, data, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
178{
179 struct input_dev *dev = session->input;
180 unsigned char *keys = session->keys;
181 unsigned char *udata = skb->data + 1;
182 signed char *sdata = skb->data + 1;
183 int i, size = skb->len - 1;
184
185 switch (skb->data[0]) {
186 case 0x01: /* Keyboard report */
187 for (i = 0; i < 8; i++)
188 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
189
190 /* If all the key codes have been set to 0x01, it means
191 * too many keys were pressed at the same time. */
192 if (!memcmp(udata + 2, hidp_mkeyspat, 6))
193 break;
194
195 for (i = 2; i < 8; i++) {
196 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
197 if (hidp_keycode[keys[i]])
198 input_report_key(dev, hidp_keycode[keys[i]], 0);
199 else
200 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
201 }
202
203 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
204 if (hidp_keycode[udata[i]])
205 input_report_key(dev, hidp_keycode[udata[i]], 1);
206 else
207 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
208 }
209 }
210
211 memcpy(keys, udata, 8);
212 break;
213
214 case 0x02: /* Mouse report */
215 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
216 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
217 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
218 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
219 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
220
221 input_report_rel(dev, REL_X, sdata[1]);
222 input_report_rel(dev, REL_Y, sdata[2]);
223
224 if (size > 3)
225 input_report_rel(dev, REL_WHEEL, sdata[3]);
226 break;
227 }
228
229 input_sync(dev);
230}
231
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100232static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
233{
David Herrmann41edc0c2013-04-06 20:28:50 +0200234 unsigned char buf[32], hdr;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100235 int rsize;
236
237 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
238 if (rsize > sizeof(buf))
239 return -EIO;
240
241 hid_output_report(report, buf);
David Herrmann41edc0c2013-04-06 20:28:50 +0200242 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100243
David Herrmann41edc0c2013-04-06 20:28:50 +0200244 return hidp_send_intr_message(session, hdr, buf, rsize);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100245}
246
Alan Ott0ff17312011-01-18 03:04:40 -0500247static int hidp_get_raw_report(struct hid_device *hid,
248 unsigned char report_number,
249 unsigned char *data, size_t count,
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100250 unsigned char report_type)
Jiri Kosina2da31932009-11-26 16:20:56 +0100251{
Alan Ott0ff17312011-01-18 03:04:40 -0500252 struct hidp_session *session = hid->driver_data;
253 struct sk_buff *skb;
254 size_t len;
255 int numbered_reports = hid->report_enum[report_type].numbered;
David Herrmann794d1752011-08-26 14:06:02 +0200256 int ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500257
Karl Reltonfd86c9b2013-02-20 18:16:19 +0000258 if (atomic_read(&session->terminate))
259 return -EIO;
260
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100261 switch (report_type) {
262 case HID_FEATURE_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500263 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
264 break;
265 case HID_INPUT_REPORT:
266 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100267 break;
268 case HID_OUTPUT_REPORT:
Alan Ott0ff17312011-01-18 03:04:40 -0500269 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100270 break;
271 default:
272 return -EINVAL;
273 }
274
Alan Ott0ff17312011-01-18 03:04:40 -0500275 if (mutex_lock_interruptible(&session->report_mutex))
276 return -ERESTARTSYS;
277
278 /* Set up our wait, and send the report request to the device. */
279 session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK;
280 session->waiting_report_number = numbered_reports ? report_number : -1;
281 set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
282 data[0] = report_number;
David Herrmann41edc0c2013-04-06 20:28:50 +0200283 ret = hidp_send_ctrl_message(session, report_type, data, 1);
David Herrmann794d1752011-08-26 14:06:02 +0200284 if (ret)
285 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500286
287 /* Wait for the return of the report. The returned report
288 gets put in session->report_return. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200289 while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
290 !atomic_read(&session->terminate)) {
Alan Ott0ff17312011-01-18 03:04:40 -0500291 int res;
292
293 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200294 !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)
295 || atomic_read(&session->terminate),
Alan Ott0ff17312011-01-18 03:04:40 -0500296 5*HZ);
297 if (res == 0) {
298 /* timeout */
David Herrmann794d1752011-08-26 14:06:02 +0200299 ret = -EIO;
300 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500301 }
302 if (res < 0) {
303 /* signal */
David Herrmann794d1752011-08-26 14:06:02 +0200304 ret = -ERESTARTSYS;
305 goto err;
Alan Ott0ff17312011-01-18 03:04:40 -0500306 }
307 }
308
309 skb = session->report_return;
310 if (skb) {
311 len = skb->len < count ? skb->len : count;
312 memcpy(data, skb->data, len);
313
314 kfree_skb(skb);
315 session->report_return = NULL;
316 } else {
317 /* Device returned a HANDSHAKE, indicating protocol error. */
318 len = -EIO;
319 }
320
321 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
322 mutex_unlock(&session->report_mutex);
323
324 return len;
325
David Herrmann794d1752011-08-26 14:06:02 +0200326err:
Alan Ott0ff17312011-01-18 03:04:40 -0500327 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
328 mutex_unlock(&session->report_mutex);
David Herrmann794d1752011-08-26 14:06:02 +0200329 return ret;
Alan Ott0ff17312011-01-18 03:04:40 -0500330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
Jiri Kosina2da31932009-11-26 16:20:56 +0100333 unsigned char report_type)
334{
Alan Ott08254112011-01-18 03:04:38 -0500335 struct hidp_session *session = hid->driver_data;
336 int ret;
337
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100338 switch (report_type) {
339 case HID_FEATURE_REPORT:
340 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
341 break;
342 case HID_OUTPUT_REPORT:
David Herrmann41edc0c2013-04-06 20:28:50 +0200343 report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100344 break;
345 default:
346 return -EINVAL;
347 }
348
Alan Ott08254112011-01-18 03:04:38 -0500349 if (mutex_lock_interruptible(&session->report_mutex))
350 return -ERESTARTSYS;
351
352 /* Set up our wait, and send the report request to the device. */
353 set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
David Herrmann41edc0c2013-04-06 20:28:50 +0200354 ret = hidp_send_ctrl_message(session, report_type, data, count);
David Herrmann794d1752011-08-26 14:06:02 +0200355 if (ret)
Alan Ott08254112011-01-18 03:04:38 -0500356 goto err;
Alan Ott08254112011-01-18 03:04:38 -0500357
358 /* Wait for the ACK from the device. */
David Herrmanne3492dc2013-04-06 20:28:41 +0200359 while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) &&
360 !atomic_read(&session->terminate)) {
Alan Ott08254112011-01-18 03:04:38 -0500361 int res;
362
363 res = wait_event_interruptible_timeout(session->report_queue,
David Herrmanne3492dc2013-04-06 20:28:41 +0200364 !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)
365 || atomic_read(&session->terminate),
Alan Ott08254112011-01-18 03:04:38 -0500366 10*HZ);
367 if (res == 0) {
368 /* timeout */
369 ret = -EIO;
370 goto err;
371 }
372 if (res < 0) {
373 /* signal */
374 ret = -ERESTARTSYS;
375 goto err;
376 }
377 }
378
379 if (!session->output_report_success) {
380 ret = -EIO;
381 goto err;
382 }
383
384 ret = count;
385
386err:
387 clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
388 mutex_unlock(&session->report_mutex);
389 return ret;
Jiri Kosina2da31932009-11-26 16:20:56 +0100390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static void hidp_idle_timeout(unsigned long arg)
393{
394 struct hidp_session *session = (struct hidp_session *) arg;
395
David Herrmann52051852013-04-06 20:28:47 +0200396 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Andrew Morton91f5cca2008-02-05 03:07:58 -0800399static void hidp_set_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 if (session->idle_to > 0)
402 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
403}
404
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300405static void hidp_del_timer(struct hidp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 if (session->idle_to > 0)
408 del_timer(&session->timer);
409}
410
Andrew Morton91f5cca2008-02-05 03:07:58 -0800411static void hidp_process_handshake(struct hidp_session *session,
412 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 BT_DBG("session %p param 0x%02x", session, param);
Alan Ott08254112011-01-18 03:04:38 -0500415 session->output_report_success = 0; /* default condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 switch (param) {
418 case HIDP_HSHK_SUCCESSFUL:
419 /* FIXME: Call into SET_ GET_ handlers here */
Alan Ott08254112011-01-18 03:04:38 -0500420 session->output_report_success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422
423 case HIDP_HSHK_NOT_READY:
424 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
425 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
426 case HIDP_HSHK_ERR_INVALID_PARAMETER:
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300427 if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags))
Alan Ott0ff17312011-01-18 03:04:40 -0500428 wake_up_interruptible(&session->report_queue);
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* FIXME: Call into SET_ GET_ handlers here */
431 break;
432
433 case HIDP_HSHK_ERR_UNKNOWN:
434 break;
435
436 case HIDP_HSHK_ERR_FATAL:
437 /* Device requests a reboot, as this is the only way this error
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900438 * can be recovered. */
David Herrmann41edc0c2013-04-06 20:28:50 +0200439 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
441 break;
442
443 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200444 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
446 break;
447 }
Alan Ott08254112011-01-18 03:04:38 -0500448
449 /* Wake up the waiting thread. */
Gustavo F. Padovanab88f712011-10-06 22:05:37 -0300450 if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags))
Alan Ott08254112011-01-18 03:04:38 -0500451 wake_up_interruptible(&session->report_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Andrew Morton91f5cca2008-02-05 03:07:58 -0800454static void hidp_process_hid_control(struct hidp_session *session,
455 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 BT_DBG("session %p param 0x%02x", session, param);
458
Dave Youngeff001e2008-02-05 03:07:14 -0800459 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Flush the transmit queues */
461 skb_queue_purge(&session->ctrl_transmit);
462 skb_queue_purge(&session->intr_transmit);
463
David Herrmann52051852013-04-06 20:28:47 +0200464 hidp_session_terminate(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466}
467
Alan Ott0ff17312011-01-18 03:04:40 -0500468/* Returns true if the passed-in skb should be freed by the caller. */
469static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800470 unsigned char param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Alan Ott0ff17312011-01-18 03:04:40 -0500472 int done_with_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
474
475 switch (param) {
476 case HIDP_DATA_RTYPE_INPUT:
477 hidp_set_timer(session);
478
479 if (session->input)
480 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100481
482 if (session->hid)
483 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485
486 case HIDP_DATA_RTYPE_OTHER:
487 case HIDP_DATA_RTYPE_OUPUT:
488 case HIDP_DATA_RTYPE_FEATURE:
489 break;
490
491 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200492 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
494 }
Alan Ott0ff17312011-01-18 03:04:40 -0500495
496 if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
497 param == session->waiting_report_type) {
498 if (session->waiting_report_number < 0 ||
499 session->waiting_report_number == skb->data[0]) {
500 /* hidp_get_raw_report() is waiting on this report. */
501 session->report_return = skb;
502 done_with_skb = 0;
503 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
504 wake_up_interruptible(&session->report_queue);
505 }
506 }
507
508 return done_with_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Andrew Morton91f5cca2008-02-05 03:07:58 -0800511static void hidp_recv_ctrl_frame(struct hidp_session *session,
512 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned char hdr, type, param;
Alan Ott0ff17312011-01-18 03:04:40 -0500515 int free_skb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
518
519 hdr = skb->data[0];
520 skb_pull(skb, 1);
521
522 type = hdr & HIDP_HEADER_TRANS_MASK;
523 param = hdr & HIDP_HEADER_PARAM_MASK;
524
525 switch (type) {
526 case HIDP_TRANS_HANDSHAKE:
527 hidp_process_handshake(session, param);
528 break;
529
530 case HIDP_TRANS_HID_CONTROL:
531 hidp_process_hid_control(session, param);
532 break;
533
534 case HIDP_TRANS_DATA:
Alan Ott0ff17312011-01-18 03:04:40 -0500535 free_skb = hidp_process_data(session, skb, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537
538 default:
David Herrmann41edc0c2013-04-06 20:28:50 +0200539 hidp_send_ctrl_message(session,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
541 break;
542 }
543
Alan Ott0ff17312011-01-18 03:04:40 -0500544 if (free_skb)
545 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Andrew Morton91f5cca2008-02-05 03:07:58 -0800548static void hidp_recv_intr_frame(struct hidp_session *session,
549 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned char hdr;
552
553 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
554
555 hdr = skb->data[0];
556 skb_pull(skb, 1);
557
558 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
559 hidp_set_timer(session);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (session->input)
562 hidp_input_report(session, skb);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100563
564 if (session->hid) {
565 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
566 BT_DBG("report len %d", skb->len);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 } else {
569 BT_DBG("Unsupported protocol header 0x%02x", hdr);
570 }
571
572 kfree_skb(skb);
573}
574
575static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
576{
577 struct kvec iv = { data, len };
578 struct msghdr msg;
579
580 BT_DBG("sock %p data %p len %d", sock, data, len);
581
582 if (!len)
583 return 0;
584
585 memset(&msg, 0, sizeof(msg));
586
587 return kernel_sendmsg(sock, &msg, &iv, 1, len);
588}
589
David Herrmann7350e6c2013-04-06 20:28:49 +0200590/* dequeue message from @transmit and send via @sock */
591static void hidp_process_transmit(struct hidp_session *session,
592 struct sk_buff_head *transmit,
593 struct socket *sock)
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300594{
595 struct sk_buff *skb;
David Herrmann2df01202013-04-06 20:28:48 +0200596 int ret;
Gustavo F. Padovan679344e2011-10-06 20:51:37 -0300597
598 BT_DBG("session %p", session);
599
David Herrmann7350e6c2013-04-06 20:28:49 +0200600 while ((skb = skb_dequeue(transmit))) {
601 ret = hidp_send_frame(sock, skb->data, skb->len);
David Herrmann2df01202013-04-06 20:28:48 +0200602 if (ret == -EAGAIN) {
David Herrmann7350e6c2013-04-06 20:28:49 +0200603 skb_queue_head(transmit, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
David Herrmann2df01202013-04-06 20:28:48 +0200605 } else if (ret < 0) {
606 hidp_session_terminate(session);
607 kfree_skb(skb);
608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 hidp_set_timer(session);
612 kfree_skb(skb);
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Andrew Morton91f5cca2008-02-05 03:07:58 -0800616static int hidp_setup_input(struct hidp_session *session,
617 struct hidp_connadd_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Jiri Slabyc500c972008-05-16 11:49:16 +0200619 struct input_dev *input;
Gustavo F. Padovan3415a5f2011-10-06 21:17:32 -0300620 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jiri Slabyc500c972008-05-16 11:49:16 +0200622 input = input_allocate_device();
623 if (!input)
624 return -ENOMEM;
625
626 session->input = input;
627
Marcel Holtmann5be39462007-05-09 09:15:30 +0200628 input_set_drvdata(input, session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dmitry Torokhov34abf912005-09-15 02:01:40 -0500630 input->name = "Bluetooth HID Boot Protocol Device";
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 input->id.bustype = BUS_BLUETOOTH;
633 input->id.vendor = req->vendor;
634 input->id.product = req->product;
635 input->id.version = req->version;
636
637 if (req->subclass & 0x40) {
638 set_bit(EV_KEY, input->evbit);
639 set_bit(EV_LED, input->evbit);
640 set_bit(EV_REP, input->evbit);
641
642 set_bit(LED_NUML, input->ledbit);
643 set_bit(LED_CAPSL, input->ledbit);
644 set_bit(LED_SCROLLL, input->ledbit);
645 set_bit(LED_COMPOSE, input->ledbit);
646 set_bit(LED_KANA, input->ledbit);
647
648 for (i = 0; i < sizeof(hidp_keycode); i++)
649 set_bit(hidp_keycode[i], input->keybit);
650 clear_bit(0, input->keybit);
651 }
652
653 if (req->subclass & 0x80) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700654 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
655 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
656 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
657 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
658 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
659 BIT_MASK(BTN_EXTRA);
660 input->relbit[0] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
David Herrmann52051852013-04-06 20:28:47 +0200663 input->dev.parent = &session->conn->hcon->dev;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 input->event = hidp_input_event;
666
Marcel Holtmannedad6382009-08-22 14:22:15 -0700667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Marcel Holtmannf5ffd462007-02-17 23:58:53 +0100670static int hidp_open(struct hid_device *hid)
671{
672 return 0;
673}
674
675static void hidp_close(struct hid_device *hid)
676{
677}
678
Jiri Slabyc500c972008-05-16 11:49:16 +0200679static int hidp_parse(struct hid_device *hid)
680{
681 struct hidp_session *session = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200682
Michael Poole15c697c2010-02-05 12:23:43 -0500683 return hid_parse_report(session->hid, session->rd_data,
684 session->rd_size);
Jiri Slabyc500c972008-05-16 11:49:16 +0200685}
686
687static int hidp_start(struct hid_device *hid)
688{
689 struct hidp_session *session = hid->driver_data;
690 struct hid_report *report;
691
David Herrmann142c69c2011-08-26 13:27:12 +0200692 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
693 return 0;
694
Jiri Slabyc500c972008-05-16 11:49:16 +0200695 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
696 report_list, list)
697 hidp_send_report(session, report);
698
699 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
700 report_list, list)
701 hidp_send_report(session, report);
702
Jiri Slabyc500c972008-05-16 11:49:16 +0200703 return 0;
704}
705
706static void hidp_stop(struct hid_device *hid)
707{
708 struct hidp_session *session = hid->driver_data;
709
710 skb_queue_purge(&session->ctrl_transmit);
711 skb_queue_purge(&session->intr_transmit);
712
Jiri Slabyc500c972008-05-16 11:49:16 +0200713 hid->claimed = 0;
714}
715
716static struct hid_ll_driver hidp_hid_driver = {
717 .parse = hidp_parse,
718 .start = hidp_start,
719 .stop = hidp_stop,
720 .open = hidp_open,
721 .close = hidp_close,
Jiri Slabyc500c972008-05-16 11:49:16 +0200722};
723
Alan Ott0f69dca2011-01-18 03:04:37 -0500724/* This function sets up the hid device. It does not add it
725 to the HID system. That is done in hidp_add_connection(). */
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200726static int hidp_setup_hid(struct hidp_session *session,
Andrew Morton91f5cca2008-02-05 03:07:58 -0800727 struct hidp_connadd_req *req)
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100728{
Jiri Slabyc500c972008-05-16 11:49:16 +0200729 struct hid_device *hid;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700730 int err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100731
Michael Poole15c697c2010-02-05 12:23:43 -0500732 session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
733 if (!session->rd_data)
734 return -ENOMEM;
735
736 if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
737 err = -EFAULT;
738 goto fault;
739 }
740 session->rd_size = req->rd_size;
741
Jiri Slabyc500c972008-05-16 11:49:16 +0200742 hid = hid_allocate_device();
Michael Poole15c697c2010-02-05 12:23:43 -0500743 if (IS_ERR(hid)) {
744 err = PTR_ERR(hid);
745 goto fault;
746 }
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100747
Jiri Slabyc500c972008-05-16 11:49:16 +0200748 session->hid = hid;
Michael Poole15c697c2010-02-05 12:23:43 -0500749
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100750 hid->driver_data = session;
751
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100752 hid->bus = BUS_BLUETOOTH;
753 hid->vendor = req->vendor;
754 hid->product = req->product;
755 hid->version = req->version;
Jiri Slabyc500c972008-05-16 11:49:16 +0200756 hid->country = req->country;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100757
Anderson Lizardo0a9ab9b2013-01-06 18:28:53 -0400758 strncpy(hid->name, req->name, sizeof(req->name) - 1);
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300759
760 snprintf(hid->phys, sizeof(hid->phys), "%pMR",
761 &bt_sk(session->ctrl_sock->sk)->src);
762
763 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
764 &bt_sk(session->ctrl_sock->sk)->dst);
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100765
David Herrmann52051852013-04-06 20:28:47 +0200766 hid->dev.parent = &session->conn->hcon->dev;
Jiri Slabyc500c972008-05-16 11:49:16 +0200767 hid->ll_driver = &hidp_hid_driver;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200768
Alan Ott0ff17312011-01-18 03:04:40 -0500769 hid->hid_get_raw_report = hidp_get_raw_report;
Jiri Kosina2da31932009-11-26 16:20:56 +0100770 hid->hid_output_raw_report = hidp_output_raw_report;
771
Lamarque V. Souza4529eef2012-12-06 12:39:55 -0200772 /* True if device is blacklisted in drivers/hid/hid-core.c */
773 if (hid_ignore(hid)) {
774 hid_destroy_device(session->hid);
775 session->hid = NULL;
776 return -ENODEV;
777 }
778
Jiri Slabyc500c972008-05-16 11:49:16 +0200779 return 0;
Marcel Holtmannedad6382009-08-22 14:22:15 -0700780
Michael Poole15c697c2010-02-05 12:23:43 -0500781fault:
782 kfree(session->rd_data);
783 session->rd_data = NULL;
784
Marcel Holtmannedad6382009-08-22 14:22:15 -0700785 return err;
Marcel Holtmanne1aaadd2007-02-17 23:58:49 +0100786}
787
David Herrmannb4f34d82013-04-06 20:28:46 +0200788/* initialize session devices */
789static int hidp_session_dev_init(struct hidp_session *session,
790 struct hidp_connadd_req *req)
791{
792 int ret;
793
794 if (req->rd_size > 0) {
795 ret = hidp_setup_hid(session, req);
796 if (ret && ret != -ENODEV)
797 return ret;
798 }
799
800 if (!session->hid) {
801 ret = hidp_setup_input(session, req);
802 if (ret < 0)
803 return ret;
804 }
805
806 return 0;
807}
808
809/* destroy session devices */
810static void hidp_session_dev_destroy(struct hidp_session *session)
811{
812 if (session->hid)
813 put_device(&session->hid->dev);
814 else if (session->input)
815 input_put_device(session->input);
816
817 kfree(session->rd_data);
818 session->rd_data = NULL;
819}
820
821/* add HID/input devices to their underlying bus systems */
822static int hidp_session_dev_add(struct hidp_session *session)
823{
824 int ret;
825
826 /* Both HID and input systems drop a ref-count when unregistering the
827 * device but they don't take a ref-count when registering them. Work
828 * around this by explicitly taking a refcount during registration
829 * which is dropped automatically by unregistering the devices. */
830
831 if (session->hid) {
832 ret = hid_add_device(session->hid);
833 if (ret)
834 return ret;
835 get_device(&session->hid->dev);
836 } else if (session->input) {
837 ret = input_register_device(session->input);
838 if (ret)
839 return ret;
840 input_get_device(session->input);
841 }
842
843 return 0;
844}
845
846/* remove HID/input devices from their bus systems */
847static void hidp_session_dev_del(struct hidp_session *session)
848{
849 if (session->hid)
850 hid_destroy_device(session->hid);
851 else if (session->input)
852 input_unregister_device(session->input);
853}
854
855/*
856 * Create new session object
857 * Allocate session object, initialize static fields, copy input data into the
858 * object and take a reference to all sub-objects.
859 * This returns 0 on success and puts a pointer to the new session object in
860 * \out. Otherwise, an error code is returned.
861 * The new session object has an initial ref-count of 1.
862 */
863static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr,
864 struct socket *ctrl_sock,
865 struct socket *intr_sock,
866 struct hidp_connadd_req *req,
867 struct l2cap_conn *conn)
868{
869 struct hidp_session *session;
870 int ret;
871 struct bt_sock *ctrl, *intr;
872
873 ctrl = bt_sk(ctrl_sock->sk);
874 intr = bt_sk(intr_sock->sk);
875
876 session = kzalloc(sizeof(*session), GFP_KERNEL);
877 if (!session)
878 return -ENOMEM;
879
880 /* object and runtime management */
881 kref_init(&session->ref);
882 atomic_set(&session->state, HIDP_SESSION_IDLING);
883 init_waitqueue_head(&session->state_queue);
884 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
885
886 /* connection management */
887 bacpy(&session->bdaddr, bdaddr);
888 session->conn = conn;
889 session->user.probe = hidp_session_probe;
890 session->user.remove = hidp_session_remove;
891 session->ctrl_sock = ctrl_sock;
892 session->intr_sock = intr_sock;
893 skb_queue_head_init(&session->ctrl_transmit);
894 skb_queue_head_init(&session->intr_transmit);
895 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl)->chan->omtu,
896 l2cap_pi(ctrl)->chan->imtu);
897 session->intr_mtu = min_t(uint, l2cap_pi(intr)->chan->omtu,
898 l2cap_pi(intr)->chan->imtu);
899 session->idle_to = req->idle_to;
900
901 /* device management */
902 setup_timer(&session->timer, hidp_idle_timeout,
903 (unsigned long)session);
904
905 /* session data */
906 mutex_init(&session->report_mutex);
907 init_waitqueue_head(&session->report_queue);
908
909 ret = hidp_session_dev_init(session, req);
910 if (ret)
911 goto err_free;
912
913 l2cap_conn_get(session->conn);
914 get_file(session->intr_sock->file);
915 get_file(session->ctrl_sock->file);
916 *out = session;
917 return 0;
918
919err_free:
920 kfree(session);
921 return ret;
922}
923
924/* increase ref-count of the given session by one */
925static void hidp_session_get(struct hidp_session *session)
926{
927 kref_get(&session->ref);
928}
929
930/* release callback */
931static void session_free(struct kref *ref)
932{
933 struct hidp_session *session = container_of(ref, struct hidp_session,
934 ref);
935
936 hidp_session_dev_destroy(session);
937 skb_queue_purge(&session->ctrl_transmit);
938 skb_queue_purge(&session->intr_transmit);
939 fput(session->intr_sock->file);
940 fput(session->ctrl_sock->file);
941 l2cap_conn_put(session->conn);
942 kfree(session);
943}
944
945/* decrease ref-count of the given session by one */
946static void hidp_session_put(struct hidp_session *session)
947{
948 kref_put(&session->ref, session_free);
949}
950
951/*
952 * Search the list of active sessions for a session with target address
953 * \bdaddr. You must hold at least a read-lock on \hidp_session_sem. As long as
954 * you do not release this lock, the session objects cannot vanish and you can
955 * safely take a reference to the session yourself.
956 */
957static struct hidp_session *__hidp_session_find(const bdaddr_t *bdaddr)
958{
959 struct hidp_session *session;
960
961 list_for_each_entry(session, &hidp_session_list, list) {
962 if (!bacmp(bdaddr, &session->bdaddr))
963 return session;
964 }
965
966 return NULL;
967}
968
969/*
970 * Same as __hidp_session_find() but no locks must be held. This also takes a
971 * reference of the returned session (if non-NULL) so you must drop this
972 * reference if you no longer use the object.
973 */
974static struct hidp_session *hidp_session_find(const bdaddr_t *bdaddr)
975{
976 struct hidp_session *session;
977
978 down_read(&hidp_session_sem);
979
980 session = __hidp_session_find(bdaddr);
981 if (session)
982 hidp_session_get(session);
983
984 up_read(&hidp_session_sem);
985
986 return session;
987}
988
989/*
990 * Start session synchronously
991 * This starts a session thread and waits until initialization
992 * is done or returns an error if it couldn't be started.
993 * If this returns 0 the session thread is up and running. You must call
994 * hipd_session_stop_sync() before deleting any runtime resources.
995 */
996static int hidp_session_start_sync(struct hidp_session *session)
997{
998 unsigned int vendor, product;
999
1000 if (session->hid) {
1001 vendor = session->hid->vendor;
1002 product = session->hid->product;
1003 } else if (session->input) {
1004 vendor = session->input->id.vendor;
1005 product = session->input->id.product;
1006 } else {
1007 vendor = 0x0000;
1008 product = 0x0000;
1009 }
1010
1011 session->task = kthread_run(hidp_session_thread, session,
1012 "khidpd_%04x%04x", vendor, product);
1013 if (IS_ERR(session->task))
1014 return PTR_ERR(session->task);
1015
1016 while (atomic_read(&session->state) <= HIDP_SESSION_IDLING)
1017 wait_event(session->state_queue,
1018 atomic_read(&session->state) > HIDP_SESSION_IDLING);
1019
1020 return 0;
1021}
1022
1023/*
1024 * Terminate session thread
1025 * Wake up session thread and notify it to stop. This is asynchronous and
1026 * returns immediately. Call this whenever a runtime error occurs and you want
1027 * the session to stop.
1028 * Note: wake_up_process() performs any necessary memory-barriers for us.
1029 */
1030static void hidp_session_terminate(struct hidp_session *session)
1031{
1032 atomic_inc(&session->terminate);
1033 wake_up_process(session->task);
1034}
1035
1036/*
1037 * Probe HIDP session
1038 * This is called from the l2cap_conn core when our l2cap_user object is bound
1039 * to the hci-connection. We get the session via the \user object and can now
1040 * start the session thread, register the HID/input devices and link it into
1041 * the global session list.
1042 * The global session-list owns its own reference to the session object so you
1043 * can drop your own reference after registering the l2cap_user object.
1044 */
1045static int hidp_session_probe(struct l2cap_conn *conn,
1046 struct l2cap_user *user)
1047{
1048 struct hidp_session *session = container_of(user,
1049 struct hidp_session,
1050 user);
1051 struct hidp_session *s;
1052 int ret;
1053
1054 down_write(&hidp_session_sem);
1055
1056 /* check that no other session for this device exists */
1057 s = __hidp_session_find(&session->bdaddr);
1058 if (s) {
1059 ret = -EEXIST;
1060 goto out_unlock;
1061 }
1062
1063 ret = hidp_session_start_sync(session);
1064 if (ret)
1065 goto out_unlock;
1066
1067 ret = hidp_session_dev_add(session);
1068 if (ret)
1069 goto out_stop;
1070
1071 hidp_session_get(session);
1072 list_add(&session->list, &hidp_session_list);
1073 ret = 0;
1074 goto out_unlock;
1075
1076out_stop:
1077 hidp_session_terminate(session);
1078out_unlock:
1079 up_write(&hidp_session_sem);
1080 return ret;
1081}
1082
1083/*
1084 * Remove HIDP session
1085 * Called from the l2cap_conn core when either we explicitly unregistered
1086 * the l2cap_user object or if the underlying connection is shut down.
1087 * We signal the hidp-session thread to shut down, unregister the HID/input
1088 * devices and unlink the session from the global list.
1089 * This drops the reference to the session that is owned by the global
1090 * session-list.
1091 * Note: We _must_ not synchronosly wait for the session-thread to shut down.
1092 * This is, because the session-thread might be waiting for an HCI lock that is
1093 * held while we are called. Therefore, we only unregister the devices and
1094 * notify the session-thread to terminate. The thread itself owns a reference
1095 * to the session object so it can safely shut down.
1096 */
1097static void hidp_session_remove(struct l2cap_conn *conn,
1098 struct l2cap_user *user)
1099{
1100 struct hidp_session *session = container_of(user,
1101 struct hidp_session,
1102 user);
1103
1104 down_write(&hidp_session_sem);
1105
1106 hidp_session_terminate(session);
1107 hidp_session_dev_del(session);
1108 list_del(&session->list);
1109
1110 up_write(&hidp_session_sem);
1111
1112 hidp_session_put(session);
1113}
1114
1115/*
1116 * Session Worker
1117 * This performs the actual main-loop of the HIDP worker. We first check
1118 * whether the underlying connection is still alive, then parse all pending
1119 * messages and finally send all outstanding messages.
1120 */
1121static void hidp_session_run(struct hidp_session *session)
1122{
1123 struct sock *ctrl_sk = session->ctrl_sock->sk;
1124 struct sock *intr_sk = session->intr_sock->sk;
1125 struct sk_buff *skb;
1126
1127 for (;;) {
1128 /*
1129 * This thread can be woken up two ways:
1130 * - You call hidp_session_terminate() which sets the
1131 * session->terminate flag and wakes this thread up.
1132 * - Via modifying the socket state of ctrl/intr_sock. This
1133 * thread is woken up by ->sk_state_changed().
1134 *
1135 * Note: set_current_state() performs any necessary
1136 * memory-barriers for us.
1137 */
1138 set_current_state(TASK_INTERRUPTIBLE);
1139
1140 if (atomic_read(&session->terminate))
1141 break;
1142
1143 if (ctrl_sk->sk_state != BT_CONNECTED ||
1144 intr_sk->sk_state != BT_CONNECTED)
1145 break;
1146
1147 /* parse incoming intr-skbs */
1148 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
1149 skb_orphan(skb);
1150 if (!skb_linearize(skb))
1151 hidp_recv_intr_frame(session, skb);
1152 else
1153 kfree_skb(skb);
1154 }
1155
1156 /* send pending intr-skbs */
David Herrmann7350e6c2013-04-06 20:28:49 +02001157 hidp_process_transmit(session, &session->intr_transmit,
1158 session->intr_sock);
David Herrmannb4f34d82013-04-06 20:28:46 +02001159
1160 /* parse incoming ctrl-skbs */
1161 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
1162 skb_orphan(skb);
1163 if (!skb_linearize(skb))
1164 hidp_recv_ctrl_frame(session, skb);
1165 else
1166 kfree_skb(skb);
1167 }
1168
1169 /* send pending ctrl-skbs */
David Herrmann7350e6c2013-04-06 20:28:49 +02001170 hidp_process_transmit(session, &session->ctrl_transmit,
1171 session->ctrl_sock);
David Herrmannb4f34d82013-04-06 20:28:46 +02001172
1173 schedule();
1174 }
1175
1176 atomic_inc(&session->terminate);
1177 set_current_state(TASK_RUNNING);
1178}
1179
1180/*
1181 * HIDP session thread
1182 * This thread runs the I/O for a single HIDP session. Startup is synchronous
1183 * which allows us to take references to ourself here instead of doing that in
1184 * the caller.
1185 * When we are ready to run we notify the caller and call hidp_session_run().
1186 */
1187static int hidp_session_thread(void *arg)
1188{
1189 struct hidp_session *session = arg;
1190 wait_queue_t ctrl_wait, intr_wait;
1191
1192 BT_DBG("session %p", session);
1193
1194 /* initialize runtime environment */
1195 hidp_session_get(session);
1196 __module_get(THIS_MODULE);
1197 set_user_nice(current, -15);
1198 hidp_set_timer(session);
1199
1200 init_waitqueue_entry(&ctrl_wait, current);
1201 init_waitqueue_entry(&intr_wait, current);
1202 add_wait_queue(sk_sleep(session->ctrl_sock->sk), &ctrl_wait);
1203 add_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1204 /* This memory barrier is paired with wq_has_sleeper(). See
1205 * sock_poll_wait() for more information why this is needed. */
1206 smp_mb();
1207
1208 /* notify synchronous startup that we're ready */
1209 atomic_inc(&session->state);
1210 wake_up(&session->state_queue);
1211
1212 /* run session */
1213 hidp_session_run(session);
1214
1215 /* cleanup runtime environment */
1216 remove_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1217 remove_wait_queue(sk_sleep(session->intr_sock->sk), &ctrl_wait);
1218 wake_up_interruptible(&session->report_queue);
1219 hidp_del_timer(session);
1220
1221 /*
1222 * If we stopped ourself due to any internal signal, we should try to
1223 * unregister our own session here to avoid having it linger until the
1224 * parent l2cap_conn dies or user-space cleans it up.
1225 * This does not deadlock as we don't do any synchronous shutdown.
1226 * Instead, this call has the same semantics as if user-space tried to
1227 * delete the session.
1228 */
1229 l2cap_unregister_user(session->conn, &session->user);
1230 hidp_session_put(session);
1231
1232 module_put_and_exit(0);
1233 return 0;
1234}
1235
1236static int hidp_verify_sockets(struct socket *ctrl_sock,
1237 struct socket *intr_sock)
1238{
1239 struct bt_sock *ctrl, *intr;
1240 struct hidp_session *session;
1241
1242 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
1243 return -EINVAL;
1244
1245 ctrl = bt_sk(ctrl_sock->sk);
1246 intr = bt_sk(intr_sock->sk);
1247
1248 if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst))
1249 return -ENOTUNIQ;
1250 if (ctrl->sk.sk_state != BT_CONNECTED ||
1251 intr->sk.sk_state != BT_CONNECTED)
1252 return -EBADFD;
1253
1254 /* early session check, we check again during session registration */
1255 session = hidp_session_find(&ctrl->dst);
1256 if (session) {
1257 hidp_session_put(session);
1258 return -EEXIST;
1259 }
1260
1261 return 0;
1262}
1263
1264int hidp_connection_add(struct hidp_connadd_req *req,
1265 struct socket *ctrl_sock,
1266 struct socket *intr_sock)
1267{
1268 struct hidp_session *session;
1269 struct l2cap_conn *conn;
1270 struct l2cap_chan *chan = l2cap_pi(ctrl_sock->sk)->chan;
1271 int ret;
1272
1273 ret = hidp_verify_sockets(ctrl_sock, intr_sock);
1274 if (ret)
1275 return ret;
1276
1277 conn = NULL;
1278 l2cap_chan_lock(chan);
1279 if (chan->conn) {
1280 l2cap_conn_get(chan->conn);
1281 conn = chan->conn;
1282 }
1283 l2cap_chan_unlock(chan);
1284
1285 if (!conn)
1286 return -EBADFD;
1287
1288 ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock,
1289 intr_sock, req, conn);
1290 if (ret)
1291 goto out_conn;
1292
1293 ret = l2cap_register_user(conn, &session->user);
1294 if (ret)
1295 goto out_session;
1296
1297 ret = 0;
1298
1299out_session:
1300 hidp_session_put(session);
1301out_conn:
1302 l2cap_conn_put(conn);
1303 return ret;
1304}
1305
1306int hidp_connection_del(struct hidp_conndel_req *req)
1307{
1308 struct hidp_session *session;
1309
1310 session = hidp_session_find(&req->bdaddr);
1311 if (!session)
1312 return -ENOENT;
1313
1314 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG))
1315 hidp_send_ctrl_message(session,
1316 HIDP_TRANS_HID_CONTROL |
1317 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG,
1318 NULL, 0);
1319 else
1320 l2cap_unregister_user(session->conn, &session->user);
1321
1322 hidp_session_put(session);
1323
1324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327int hidp_get_connlist(struct hidp_connlist_req *req)
1328{
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001329 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err = 0, n = 0;
1331
1332 BT_DBG("");
1333
1334 down_read(&hidp_session_sem);
1335
Gustavo F. Padovancd11cdd2011-10-06 17:35:31 -03001336 list_for_each_entry(session, &hidp_session_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct hidp_conninfo ci;
1338
David Herrmann52051852013-04-06 20:28:47 +02001339 hidp_copy_session(session, &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
1342 err = -EFAULT;
1343 break;
1344 }
1345
1346 if (++n >= req->cnum)
1347 break;
1348
1349 req->ci++;
1350 }
1351 req->cnum = n;
1352
1353 up_read(&hidp_session_sem);
1354 return err;
1355}
1356
1357int hidp_get_conninfo(struct hidp_conninfo *ci)
1358{
1359 struct hidp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
David Herrmann52051852013-04-06 20:28:47 +02001361 session = hidp_session_find(&ci->bdaddr);
1362 if (session) {
1363 hidp_copy_session(session, ci);
1364 hidp_session_put(session);
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
David Herrmann52051852013-04-06 20:28:47 +02001367 return session ? 0 : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370static int __init hidp_init(void)
1371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1373
Henrik Rydberg8215d552012-04-23 12:07:07 +02001374 return hidp_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377static void __exit hidp_exit(void)
1378{
1379 hidp_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382module_init(hidp_init);
1383module_exit(hidp_exit);
1384
1385MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
David Herrmannb4f34d82013-04-06 20:28:46 +02001386MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1388MODULE_VERSION(VERSION);
1389MODULE_LICENSE("GPL");
1390MODULE_ALIAS("bt-proto-6");