2 HIDP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
4 Copyright (C) 2013 David Herrmann <dh.herrmann@gmail.com>
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;
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
14 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
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
24 #include <linux/kref.h>
25 #include <linux/module.h>
26 #include <linux/file.h>
27 #include <linux/kthread.h>
28 #include <linux/hidraw.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 #include <net/bluetooth/l2cap.h>
38 static DECLARE_RWSEM(hidp_session_sem);
39 static LIST_HEAD(hidp_session_list);
41 static unsigned char hidp_keycode[256] = {
42 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
62 static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
64 static int hidp_session_probe(struct l2cap_conn *conn,
65 struct l2cap_user *user);
66 static void hidp_session_remove(struct l2cap_conn *conn,
67 struct l2cap_user *user);
68 static int hidp_session_thread(void *arg);
69 static void hidp_session_terminate(struct hidp_session *s);
71 static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
73 memset(ci, 0, sizeof(*ci));
74 bacpy(&ci->bdaddr, &session->bdaddr);
76 ci->flags = session->flags;
77 ci->state = BT_CONNECTED;
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);
90 strncpy(ci->name, "HID Boot Device", 128);
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);
101 /* assemble skb, queue message on @transmit and wake up the session thread */
102 static 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)
107 struct sock *sk = sock->sk;
109 BT_DBG("session %p data %p size %d", session, data, size);
111 if (atomic_read(&session->terminate))
114 skb = alloc_skb(size + 1, GFP_ATOMIC);
116 BT_ERR("Can't allocate memory for new frame");
120 *skb_put(skb, 1) = hdr;
121 if (data && size > 0)
122 memcpy(skb_put(skb, size), data, size);
124 skb_queue_tail(transmit, skb);
125 wake_up_interruptible(sk_sleep(sk));
130 static int hidp_send_ctrl_message(struct hidp_session *session,
131 unsigned char hdr, const unsigned char *data,
134 return hidp_send_message(session, session->ctrl_sock,
135 &session->ctrl_transmit, hdr, data, size);
138 static int hidp_send_intr_message(struct hidp_session *session,
139 unsigned char hdr, const unsigned char *data,
142 return hidp_send_message(session, session->intr_sock,
143 &session->intr_transmit, hdr, data, size);
146 static int hidp_input_event(struct input_dev *dev, unsigned int type,
147 unsigned int code, int value)
149 struct hidp_session *session = input_get_drvdata(dev);
150 unsigned char newleds;
151 unsigned char hdr, data[2];
153 BT_DBG("session %p type %d code %d value %d",
154 session, type, code, value);
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));
165 if (session->leds == newleds)
168 session->leds = newleds;
170 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
174 return hidp_send_intr_message(session, hdr, data, 2);
177 static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
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;
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);
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))
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);
200 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
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);
207 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
211 memcpy(keys, udata, 8);
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);
221 input_report_rel(dev, REL_X, sdata[1]);
222 input_report_rel(dev, REL_Y, sdata[2]);
225 input_report_rel(dev, REL_WHEEL, sdata[3]);
232 static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
238 buf = hid_alloc_report_buf(report, GFP_ATOMIC);
242 hid_output_report(report, buf);
243 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
245 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
246 ret = hidp_send_intr_message(session, hdr, buf, rsize);
252 static int hidp_get_raw_report(struct hid_device *hid,
253 unsigned char report_number,
254 unsigned char *data, size_t count,
255 unsigned char report_type)
257 struct hidp_session *session = hid->driver_data;
260 int numbered_reports = hid->report_enum[report_type].numbered;
263 if (atomic_read(&session->terminate))
266 switch (report_type) {
267 case HID_FEATURE_REPORT:
268 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
270 case HID_INPUT_REPORT:
271 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
273 case HID_OUTPUT_REPORT:
274 report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
280 if (mutex_lock_interruptible(&session->report_mutex))
283 /* Set up our wait, and send the report request to the device. */
284 session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK;
285 session->waiting_report_number = numbered_reports ? report_number : -1;
286 set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
287 data[0] = report_number;
288 ret = hidp_send_ctrl_message(session, report_type, data, 1);
292 /* Wait for the return of the report. The returned report
293 gets put in session->report_return. */
294 while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
295 !atomic_read(&session->terminate)) {
298 res = wait_event_interruptible_timeout(session->report_queue,
299 !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)
300 || atomic_read(&session->terminate),
314 skb = session->report_return;
316 len = skb->len < count ? skb->len : count;
317 memcpy(data, skb->data, len);
320 session->report_return = NULL;
322 /* Device returned a HANDSHAKE, indicating protocol error. */
326 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
327 mutex_unlock(&session->report_mutex);
332 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
333 mutex_unlock(&session->report_mutex);
337 static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
338 unsigned char report_type)
340 struct hidp_session *session = hid->driver_data;
343 if (report_type == HID_OUTPUT_REPORT) {
344 report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
345 return hidp_send_intr_message(session, report_type,
347 } else if (report_type != HID_FEATURE_REPORT) {
351 if (mutex_lock_interruptible(&session->report_mutex))
354 /* Set up our wait, and send the report request to the device. */
355 set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
356 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
357 ret = hidp_send_ctrl_message(session, report_type, data, count);
361 /* Wait for the ACK from the device. */
362 while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) &&
363 !atomic_read(&session->terminate)) {
366 res = wait_event_interruptible_timeout(session->report_queue,
367 !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)
368 || atomic_read(&session->terminate),
382 if (!session->output_report_success) {
390 clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
391 mutex_unlock(&session->report_mutex);
395 static void hidp_idle_timeout(unsigned long arg)
397 struct hidp_session *session = (struct hidp_session *) arg;
399 hidp_session_terminate(session);
402 static void hidp_set_timer(struct hidp_session *session)
404 if (session->idle_to > 0)
405 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
408 static void hidp_del_timer(struct hidp_session *session)
410 if (session->idle_to > 0)
411 del_timer(&session->timer);
414 static void hidp_process_handshake(struct hidp_session *session,
417 BT_DBG("session %p param 0x%02x", session, param);
418 session->output_report_success = 0; /* default condition */
421 case HIDP_HSHK_SUCCESSFUL:
422 /* FIXME: Call into SET_ GET_ handlers here */
423 session->output_report_success = 1;
426 case HIDP_HSHK_NOT_READY:
427 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
428 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
429 case HIDP_HSHK_ERR_INVALID_PARAMETER:
430 if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags))
431 wake_up_interruptible(&session->report_queue);
433 /* FIXME: Call into SET_ GET_ handlers here */
436 case HIDP_HSHK_ERR_UNKNOWN:
439 case HIDP_HSHK_ERR_FATAL:
440 /* Device requests a reboot, as this is the only way this error
441 * can be recovered. */
442 hidp_send_ctrl_message(session,
443 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
447 hidp_send_ctrl_message(session,
448 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
452 /* Wake up the waiting thread. */
453 if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags))
454 wake_up_interruptible(&session->report_queue);
457 static void hidp_process_hid_control(struct hidp_session *session,
460 BT_DBG("session %p param 0x%02x", session, param);
462 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
463 /* Flush the transmit queues */
464 skb_queue_purge(&session->ctrl_transmit);
465 skb_queue_purge(&session->intr_transmit);
467 hidp_session_terminate(session);
471 /* Returns true if the passed-in skb should be freed by the caller. */
472 static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
475 int done_with_skb = 1;
476 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
479 case HIDP_DATA_RTYPE_INPUT:
480 hidp_set_timer(session);
483 hidp_input_report(session, skb);
486 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
489 case HIDP_DATA_RTYPE_OTHER:
490 case HIDP_DATA_RTYPE_OUPUT:
491 case HIDP_DATA_RTYPE_FEATURE:
495 hidp_send_ctrl_message(session,
496 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
499 if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
500 param == session->waiting_report_type) {
501 if (session->waiting_report_number < 0 ||
502 session->waiting_report_number == skb->data[0]) {
503 /* hidp_get_raw_report() is waiting on this report. */
504 session->report_return = skb;
506 clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
507 wake_up_interruptible(&session->report_queue);
511 return done_with_skb;
514 static void hidp_recv_ctrl_frame(struct hidp_session *session,
517 unsigned char hdr, type, param;
520 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
525 type = hdr & HIDP_HEADER_TRANS_MASK;
526 param = hdr & HIDP_HEADER_PARAM_MASK;
529 case HIDP_TRANS_HANDSHAKE:
530 hidp_process_handshake(session, param);
533 case HIDP_TRANS_HID_CONTROL:
534 hidp_process_hid_control(session, param);
537 case HIDP_TRANS_DATA:
538 free_skb = hidp_process_data(session, skb, param);
542 hidp_send_ctrl_message(session,
543 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
551 static void hidp_recv_intr_frame(struct hidp_session *session,
556 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
561 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
562 hidp_set_timer(session);
565 hidp_input_report(session, skb);
568 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
569 BT_DBG("report len %d", skb->len);
572 BT_DBG("Unsupported protocol header 0x%02x", hdr);
578 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
580 struct kvec iv = { data, len };
583 BT_DBG("sock %p data %p len %d", sock, data, len);
588 memset(&msg, 0, sizeof(msg));
590 return kernel_sendmsg(sock, &msg, &iv, 1, len);
593 /* dequeue message from @transmit and send via @sock */
594 static void hidp_process_transmit(struct hidp_session *session,
595 struct sk_buff_head *transmit,
601 BT_DBG("session %p", session);
603 while ((skb = skb_dequeue(transmit))) {
604 ret = hidp_send_frame(sock, skb->data, skb->len);
605 if (ret == -EAGAIN) {
606 skb_queue_head(transmit, skb);
608 } else if (ret < 0) {
609 hidp_session_terminate(session);
614 hidp_set_timer(session);
619 static int hidp_setup_input(struct hidp_session *session,
620 struct hidp_connadd_req *req)
622 struct input_dev *input;
625 input = input_allocate_device();
629 session->input = input;
631 input_set_drvdata(input, session);
633 input->name = "Bluetooth HID Boot Protocol Device";
635 input->id.bustype = BUS_BLUETOOTH;
636 input->id.vendor = req->vendor;
637 input->id.product = req->product;
638 input->id.version = req->version;
640 if (req->subclass & 0x40) {
641 set_bit(EV_KEY, input->evbit);
642 set_bit(EV_LED, input->evbit);
643 set_bit(EV_REP, input->evbit);
645 set_bit(LED_NUML, input->ledbit);
646 set_bit(LED_CAPSL, input->ledbit);
647 set_bit(LED_SCROLLL, input->ledbit);
648 set_bit(LED_COMPOSE, input->ledbit);
649 set_bit(LED_KANA, input->ledbit);
651 for (i = 0; i < sizeof(hidp_keycode); i++)
652 set_bit(hidp_keycode[i], input->keybit);
653 clear_bit(0, input->keybit);
656 if (req->subclass & 0x80) {
657 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
658 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
659 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
660 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
661 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
663 input->relbit[0] |= BIT_MASK(REL_WHEEL);
666 input->dev.parent = &session->conn->hcon->dev;
668 input->event = hidp_input_event;
673 static int hidp_open(struct hid_device *hid)
678 static void hidp_close(struct hid_device *hid)
682 static int hidp_parse(struct hid_device *hid)
684 struct hidp_session *session = hid->driver_data;
686 return hid_parse_report(session->hid, session->rd_data,
690 static int hidp_start(struct hid_device *hid)
692 struct hidp_session *session = hid->driver_data;
693 struct hid_report *report;
695 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
698 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
700 hidp_send_report(session, report);
702 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
704 hidp_send_report(session, report);
709 static void hidp_stop(struct hid_device *hid)
711 struct hidp_session *session = hid->driver_data;
713 skb_queue_purge(&session->ctrl_transmit);
714 skb_queue_purge(&session->intr_transmit);
719 static struct hid_ll_driver hidp_hid_driver = {
727 /* This function sets up the hid device. It does not add it
728 to the HID system. That is done in hidp_add_connection(). */
729 static int hidp_setup_hid(struct hidp_session *session,
730 struct hidp_connadd_req *req)
732 struct hid_device *hid;
735 session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
736 if (!session->rd_data)
739 if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
743 session->rd_size = req->rd_size;
745 hid = hid_allocate_device();
753 hid->driver_data = session;
755 hid->bus = BUS_BLUETOOTH;
756 hid->vendor = req->vendor;
757 hid->product = req->product;
758 hid->version = req->version;
759 hid->country = req->country;
761 strncpy(hid->name, req->name, sizeof(req->name) - 1);
763 snprintf(hid->phys, sizeof(hid->phys), "%pMR",
764 &bt_sk(session->ctrl_sock->sk)->src);
766 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
767 &bt_sk(session->ctrl_sock->sk)->dst);
769 hid->dev.parent = &session->conn->hcon->dev;
770 hid->ll_driver = &hidp_hid_driver;
772 hid->hid_get_raw_report = hidp_get_raw_report;
773 hid->hid_output_raw_report = hidp_output_raw_report;
775 /* True if device is blacklisted in drivers/hid/hid-core.c */
776 if (hid_ignore(hid)) {
777 hid_destroy_device(session->hid);
785 kfree(session->rd_data);
786 session->rd_data = NULL;
791 /* initialize session devices */
792 static int hidp_session_dev_init(struct hidp_session *session,
793 struct hidp_connadd_req *req)
797 if (req->rd_size > 0) {
798 ret = hidp_setup_hid(session, req);
799 if (ret && ret != -ENODEV)
804 ret = hidp_setup_input(session, req);
812 /* destroy session devices */
813 static void hidp_session_dev_destroy(struct hidp_session *session)
816 put_device(&session->hid->dev);
817 else if (session->input)
818 input_put_device(session->input);
820 kfree(session->rd_data);
821 session->rd_data = NULL;
824 /* add HID/input devices to their underlying bus systems */
825 static int hidp_session_dev_add(struct hidp_session *session)
829 /* Both HID and input systems drop a ref-count when unregistering the
830 * device but they don't take a ref-count when registering them. Work
831 * around this by explicitly taking a refcount during registration
832 * which is dropped automatically by unregistering the devices. */
835 ret = hid_add_device(session->hid);
838 get_device(&session->hid->dev);
839 } else if (session->input) {
840 ret = input_register_device(session->input);
843 input_get_device(session->input);
849 /* remove HID/input devices from their bus systems */
850 static void hidp_session_dev_del(struct hidp_session *session)
853 hid_destroy_device(session->hid);
854 else if (session->input)
855 input_unregister_device(session->input);
859 * Create new session object
860 * Allocate session object, initialize static fields, copy input data into the
861 * object and take a reference to all sub-objects.
862 * This returns 0 on success and puts a pointer to the new session object in
863 * \out. Otherwise, an error code is returned.
864 * The new session object has an initial ref-count of 1.
866 static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr,
867 struct socket *ctrl_sock,
868 struct socket *intr_sock,
869 struct hidp_connadd_req *req,
870 struct l2cap_conn *conn)
872 struct hidp_session *session;
874 struct bt_sock *ctrl, *intr;
876 ctrl = bt_sk(ctrl_sock->sk);
877 intr = bt_sk(intr_sock->sk);
879 session = kzalloc(sizeof(*session), GFP_KERNEL);
883 /* object and runtime management */
884 kref_init(&session->ref);
885 atomic_set(&session->state, HIDP_SESSION_IDLING);
886 init_waitqueue_head(&session->state_queue);
887 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
889 /* connection management */
890 bacpy(&session->bdaddr, bdaddr);
891 session->conn = conn;
892 session->user.probe = hidp_session_probe;
893 session->user.remove = hidp_session_remove;
894 session->ctrl_sock = ctrl_sock;
895 session->intr_sock = intr_sock;
896 skb_queue_head_init(&session->ctrl_transmit);
897 skb_queue_head_init(&session->intr_transmit);
898 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl)->chan->omtu,
899 l2cap_pi(ctrl)->chan->imtu);
900 session->intr_mtu = min_t(uint, l2cap_pi(intr)->chan->omtu,
901 l2cap_pi(intr)->chan->imtu);
902 session->idle_to = req->idle_to;
904 /* device management */
905 setup_timer(&session->timer, hidp_idle_timeout,
906 (unsigned long)session);
909 mutex_init(&session->report_mutex);
910 init_waitqueue_head(&session->report_queue);
912 ret = hidp_session_dev_init(session, req);
916 l2cap_conn_get(session->conn);
917 get_file(session->intr_sock->file);
918 get_file(session->ctrl_sock->file);
927 /* increase ref-count of the given session by one */
928 static void hidp_session_get(struct hidp_session *session)
930 kref_get(&session->ref);
933 /* release callback */
934 static void session_free(struct kref *ref)
936 struct hidp_session *session = container_of(ref, struct hidp_session,
939 hidp_session_dev_destroy(session);
940 skb_queue_purge(&session->ctrl_transmit);
941 skb_queue_purge(&session->intr_transmit);
942 fput(session->intr_sock->file);
943 fput(session->ctrl_sock->file);
944 l2cap_conn_put(session->conn);
948 /* decrease ref-count of the given session by one */
949 static void hidp_session_put(struct hidp_session *session)
951 kref_put(&session->ref, session_free);
955 * Search the list of active sessions for a session with target address
956 * \bdaddr. You must hold at least a read-lock on \hidp_session_sem. As long as
957 * you do not release this lock, the session objects cannot vanish and you can
958 * safely take a reference to the session yourself.
960 static struct hidp_session *__hidp_session_find(const bdaddr_t *bdaddr)
962 struct hidp_session *session;
964 list_for_each_entry(session, &hidp_session_list, list) {
965 if (!bacmp(bdaddr, &session->bdaddr))
973 * Same as __hidp_session_find() but no locks must be held. This also takes a
974 * reference of the returned session (if non-NULL) so you must drop this
975 * reference if you no longer use the object.
977 static struct hidp_session *hidp_session_find(const bdaddr_t *bdaddr)
979 struct hidp_session *session;
981 down_read(&hidp_session_sem);
983 session = __hidp_session_find(bdaddr);
985 hidp_session_get(session);
987 up_read(&hidp_session_sem);
993 * Start session synchronously
994 * This starts a session thread and waits until initialization
995 * is done or returns an error if it couldn't be started.
996 * If this returns 0 the session thread is up and running. You must call
997 * hipd_session_stop_sync() before deleting any runtime resources.
999 static int hidp_session_start_sync(struct hidp_session *session)
1001 unsigned int vendor, product;
1004 vendor = session->hid->vendor;
1005 product = session->hid->product;
1006 } else if (session->input) {
1007 vendor = session->input->id.vendor;
1008 product = session->input->id.product;
1014 session->task = kthread_run(hidp_session_thread, session,
1015 "khidpd_%04x%04x", vendor, product);
1016 if (IS_ERR(session->task))
1017 return PTR_ERR(session->task);
1019 while (atomic_read(&session->state) <= HIDP_SESSION_IDLING)
1020 wait_event(session->state_queue,
1021 atomic_read(&session->state) > HIDP_SESSION_IDLING);
1027 * Terminate session thread
1028 * Wake up session thread and notify it to stop. This is asynchronous and
1029 * returns immediately. Call this whenever a runtime error occurs and you want
1030 * the session to stop.
1031 * Note: wake_up_process() performs any necessary memory-barriers for us.
1033 static void hidp_session_terminate(struct hidp_session *session)
1035 atomic_inc(&session->terminate);
1036 wake_up_process(session->task);
1040 * Probe HIDP session
1041 * This is called from the l2cap_conn core when our l2cap_user object is bound
1042 * to the hci-connection. We get the session via the \user object and can now
1043 * start the session thread, register the HID/input devices and link it into
1044 * the global session list.
1045 * The global session-list owns its own reference to the session object so you
1046 * can drop your own reference after registering the l2cap_user object.
1048 static int hidp_session_probe(struct l2cap_conn *conn,
1049 struct l2cap_user *user)
1051 struct hidp_session *session = container_of(user,
1052 struct hidp_session,
1054 struct hidp_session *s;
1057 down_write(&hidp_session_sem);
1059 /* check that no other session for this device exists */
1060 s = __hidp_session_find(&session->bdaddr);
1066 ret = hidp_session_start_sync(session);
1070 ret = hidp_session_dev_add(session);
1074 hidp_session_get(session);
1075 list_add(&session->list, &hidp_session_list);
1080 hidp_session_terminate(session);
1082 up_write(&hidp_session_sem);
1087 * Remove HIDP session
1088 * Called from the l2cap_conn core when either we explicitly unregistered
1089 * the l2cap_user object or if the underlying connection is shut down.
1090 * We signal the hidp-session thread to shut down, unregister the HID/input
1091 * devices and unlink the session from the global list.
1092 * This drops the reference to the session that is owned by the global
1094 * Note: We _must_ not synchronosly wait for the session-thread to shut down.
1095 * This is, because the session-thread might be waiting for an HCI lock that is
1096 * held while we are called. Therefore, we only unregister the devices and
1097 * notify the session-thread to terminate. The thread itself owns a reference
1098 * to the session object so it can safely shut down.
1100 static void hidp_session_remove(struct l2cap_conn *conn,
1101 struct l2cap_user *user)
1103 struct hidp_session *session = container_of(user,
1104 struct hidp_session,
1107 down_write(&hidp_session_sem);
1109 hidp_session_terminate(session);
1110 hidp_session_dev_del(session);
1111 list_del(&session->list);
1113 up_write(&hidp_session_sem);
1115 hidp_session_put(session);
1120 * This performs the actual main-loop of the HIDP worker. We first check
1121 * whether the underlying connection is still alive, then parse all pending
1122 * messages and finally send all outstanding messages.
1124 static void hidp_session_run(struct hidp_session *session)
1126 struct sock *ctrl_sk = session->ctrl_sock->sk;
1127 struct sock *intr_sk = session->intr_sock->sk;
1128 struct sk_buff *skb;
1132 * This thread can be woken up two ways:
1133 * - You call hidp_session_terminate() which sets the
1134 * session->terminate flag and wakes this thread up.
1135 * - Via modifying the socket state of ctrl/intr_sock. This
1136 * thread is woken up by ->sk_state_changed().
1138 * Note: set_current_state() performs any necessary
1139 * memory-barriers for us.
1141 set_current_state(TASK_INTERRUPTIBLE);
1143 if (atomic_read(&session->terminate))
1146 if (ctrl_sk->sk_state != BT_CONNECTED ||
1147 intr_sk->sk_state != BT_CONNECTED)
1150 /* parse incoming intr-skbs */
1151 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
1153 if (!skb_linearize(skb))
1154 hidp_recv_intr_frame(session, skb);
1159 /* send pending intr-skbs */
1160 hidp_process_transmit(session, &session->intr_transmit,
1161 session->intr_sock);
1163 /* parse incoming ctrl-skbs */
1164 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
1166 if (!skb_linearize(skb))
1167 hidp_recv_ctrl_frame(session, skb);
1172 /* send pending ctrl-skbs */
1173 hidp_process_transmit(session, &session->ctrl_transmit,
1174 session->ctrl_sock);
1179 atomic_inc(&session->terminate);
1180 set_current_state(TASK_RUNNING);
1184 * HIDP session thread
1185 * This thread runs the I/O for a single HIDP session. Startup is synchronous
1186 * which allows us to take references to ourself here instead of doing that in
1188 * When we are ready to run we notify the caller and call hidp_session_run().
1190 static int hidp_session_thread(void *arg)
1192 struct hidp_session *session = arg;
1193 wait_queue_t ctrl_wait, intr_wait;
1195 BT_DBG("session %p", session);
1197 /* initialize runtime environment */
1198 hidp_session_get(session);
1199 __module_get(THIS_MODULE);
1200 set_user_nice(current, -15);
1201 hidp_set_timer(session);
1203 init_waitqueue_entry(&ctrl_wait, current);
1204 init_waitqueue_entry(&intr_wait, current);
1205 add_wait_queue(sk_sleep(session->ctrl_sock->sk), &ctrl_wait);
1206 add_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1207 /* This memory barrier is paired with wq_has_sleeper(). See
1208 * sock_poll_wait() for more information why this is needed. */
1211 /* notify synchronous startup that we're ready */
1212 atomic_inc(&session->state);
1213 wake_up(&session->state_queue);
1216 hidp_session_run(session);
1218 /* cleanup runtime environment */
1219 remove_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait);
1220 remove_wait_queue(sk_sleep(session->intr_sock->sk), &ctrl_wait);
1221 wake_up_interruptible(&session->report_queue);
1222 hidp_del_timer(session);
1225 * If we stopped ourself due to any internal signal, we should try to
1226 * unregister our own session here to avoid having it linger until the
1227 * parent l2cap_conn dies or user-space cleans it up.
1228 * This does not deadlock as we don't do any synchronous shutdown.
1229 * Instead, this call has the same semantics as if user-space tried to
1230 * delete the session.
1232 l2cap_unregister_user(session->conn, &session->user);
1233 hidp_session_put(session);
1235 module_put_and_exit(0);
1239 static int hidp_verify_sockets(struct socket *ctrl_sock,
1240 struct socket *intr_sock)
1242 struct bt_sock *ctrl, *intr;
1243 struct hidp_session *session;
1245 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
1248 ctrl = bt_sk(ctrl_sock->sk);
1249 intr = bt_sk(intr_sock->sk);
1251 if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst))
1253 if (ctrl->sk.sk_state != BT_CONNECTED ||
1254 intr->sk.sk_state != BT_CONNECTED)
1257 /* early session check, we check again during session registration */
1258 session = hidp_session_find(&ctrl->dst);
1260 hidp_session_put(session);
1267 int hidp_connection_add(struct hidp_connadd_req *req,
1268 struct socket *ctrl_sock,
1269 struct socket *intr_sock)
1271 struct hidp_session *session;
1272 struct l2cap_conn *conn;
1273 struct l2cap_chan *chan = l2cap_pi(ctrl_sock->sk)->chan;
1276 ret = hidp_verify_sockets(ctrl_sock, intr_sock);
1281 l2cap_chan_lock(chan);
1283 l2cap_conn_get(chan->conn);
1286 l2cap_chan_unlock(chan);
1291 ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock,
1292 intr_sock, req, conn);
1296 ret = l2cap_register_user(conn, &session->user);
1303 hidp_session_put(session);
1305 l2cap_conn_put(conn);
1309 int hidp_connection_del(struct hidp_conndel_req *req)
1311 struct hidp_session *session;
1313 session = hidp_session_find(&req->bdaddr);
1317 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG))
1318 hidp_send_ctrl_message(session,
1319 HIDP_TRANS_HID_CONTROL |
1320 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG,
1323 l2cap_unregister_user(session->conn, &session->user);
1325 hidp_session_put(session);
1330 int hidp_get_connlist(struct hidp_connlist_req *req)
1332 struct hidp_session *session;
1337 down_read(&hidp_session_sem);
1339 list_for_each_entry(session, &hidp_session_list, list) {
1340 struct hidp_conninfo ci;
1342 hidp_copy_session(session, &ci);
1344 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
1349 if (++n >= req->cnum)
1356 up_read(&hidp_session_sem);
1360 int hidp_get_conninfo(struct hidp_conninfo *ci)
1362 struct hidp_session *session;
1364 session = hidp_session_find(&ci->bdaddr);
1366 hidp_copy_session(session, ci);
1367 hidp_session_put(session);
1370 return session ? 0 : -ENOENT;
1373 static int __init hidp_init(void)
1375 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1377 return hidp_init_sockets();
1380 static void __exit hidp_exit(void)
1382 hidp_cleanup_sockets();
1385 module_init(hidp_init);
1386 module_exit(hidp_exit);
1388 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1389 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1390 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1391 MODULE_VERSION(VERSION);
1392 MODULE_LICENSE("GPL");
1393 MODULE_ALIAS("bt-proto-6");