2 HIDP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/freezer.h>
32 #include <linux/fcntl.h>
33 #include <linux/skbuff.h>
34 #include <linux/socket.h>
35 #include <linux/ioctl.h>
36 #include <linux/file.h>
37 #include <linux/init.h>
38 #include <linux/wait.h>
41 #include <linux/input.h>
42 #include <linux/hid.h>
43 #include <linux/hidraw.h>
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_core.h>
47 #include <net/bluetooth/l2cap.h>
53 static DECLARE_RWSEM(hidp_session_sem);
54 static LIST_HEAD(hidp_session_list);
56 static unsigned char hidp_keycode[256] = {
57 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
58 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
59 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
60 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
61 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
62 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
63 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
64 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
65 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
66 122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
72 150,158,159,128,136,177,178,176,142,152,173,140
75 static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
77 static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)
79 struct hidp_session *session;
84 list_for_each(p, &hidp_session_list) {
85 session = list_entry(p, struct hidp_session, list);
86 if (!bacmp(bdaddr, &session->bdaddr))
92 static void __hidp_link_session(struct hidp_session *session)
94 __module_get(THIS_MODULE);
95 list_add(&session->list, &hidp_session_list);
97 hci_conn_hold_device(session->conn);
100 static void __hidp_unlink_session(struct hidp_session *session)
102 hci_conn_put_device(session->conn);
104 list_del(&session->list);
105 module_put(THIS_MODULE);
108 static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
110 memset(ci, 0, sizeof(*ci));
111 bacpy(&ci->bdaddr, &session->bdaddr);
113 ci->flags = session->flags;
114 ci->state = session->state;
117 ci->product = 0x0000;
118 ci->version = 0x0000;
120 if (session->input) {
121 ci->vendor = session->input->id.vendor;
122 ci->product = session->input->id.product;
123 ci->version = session->input->id.version;
124 if (session->input->name)
125 strncpy(ci->name, session->input->name, 128);
127 strncpy(ci->name, "HID Boot Device", 128);
131 ci->vendor = session->hid->vendor;
132 ci->product = session->hid->product;
133 ci->version = session->hid->version;
134 strncpy(ci->name, session->hid->name, 128);
138 static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
139 unsigned int type, unsigned int code, int value)
141 unsigned char newleds;
144 BT_DBG("session %p type %d code %d value %d", session, type, code, value);
149 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
150 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
151 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
152 (!!test_bit(LED_CAPSL, dev->led) << 1) |
153 (!!test_bit(LED_NUML, dev->led));
155 if (session->leds == newleds)
158 session->leds = newleds;
160 skb = alloc_skb(3, GFP_ATOMIC);
162 BT_ERR("Can't allocate memory for new frame");
166 *skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
167 *skb_put(skb, 1) = 0x01;
168 *skb_put(skb, 1) = newleds;
170 skb_queue_tail(&session->intr_transmit, skb);
172 hidp_schedule(session);
177 static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
179 struct hid_device *hid = input_get_drvdata(dev);
180 struct hidp_session *session = hid->driver_data;
182 return hidp_queue_event(session, dev, type, code, value);
185 static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
187 struct hidp_session *session = input_get_drvdata(dev);
189 return hidp_queue_event(session, dev, type, code, value);
192 static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
194 struct input_dev *dev = session->input;
195 unsigned char *keys = session->keys;
196 unsigned char *udata = skb->data + 1;
197 signed char *sdata = skb->data + 1;
198 int i, size = skb->len - 1;
200 switch (skb->data[0]) {
201 case 0x01: /* Keyboard report */
202 for (i = 0; i < 8; i++)
203 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
205 /* If all the key codes have been set to 0x01, it means
206 * too many keys were pressed at the same time. */
207 if (!memcmp(udata + 2, hidp_mkeyspat, 6))
210 for (i = 2; i < 8; i++) {
211 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
212 if (hidp_keycode[keys[i]])
213 input_report_key(dev, hidp_keycode[keys[i]], 0);
215 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
218 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
219 if (hidp_keycode[udata[i]])
220 input_report_key(dev, hidp_keycode[udata[i]], 1);
222 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
226 memcpy(keys, udata, 8);
229 case 0x02: /* Mouse report */
230 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
231 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
232 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
233 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
234 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
236 input_report_rel(dev, REL_X, sdata[1]);
237 input_report_rel(dev, REL_Y, sdata[2]);
240 input_report_rel(dev, REL_WHEEL, sdata[3]);
247 static int __hidp_send_ctrl_message(struct hidp_session *session,
248 unsigned char hdr, unsigned char *data, int size)
252 BT_DBG("session %p data %p size %d", session, data, size);
254 skb = alloc_skb(size + 1, GFP_ATOMIC);
256 BT_ERR("Can't allocate memory for new frame");
260 *skb_put(skb, 1) = hdr;
261 if (data && size > 0)
262 memcpy(skb_put(skb, size), data, size);
264 skb_queue_tail(&session->ctrl_transmit, skb);
269 static inline int hidp_send_ctrl_message(struct hidp_session *session,
270 unsigned char hdr, unsigned char *data, int size)
274 err = __hidp_send_ctrl_message(session, hdr, data, size);
276 hidp_schedule(session);
281 static int hidp_queue_report(struct hidp_session *session,
282 unsigned char *data, int size)
286 BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size);
288 skb = alloc_skb(size + 1, GFP_ATOMIC);
290 BT_ERR("Can't allocate memory for new frame");
294 *skb_put(skb, 1) = 0xa2;
296 memcpy(skb_put(skb, size), data, size);
298 skb_queue_tail(&session->intr_transmit, skb);
300 hidp_schedule(session);
305 static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
307 unsigned char buf[32];
310 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
311 if (rsize > sizeof(buf))
314 hid_output_report(report, buf);
316 return hidp_queue_report(session, buf, rsize);
319 static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
320 unsigned char report_type)
322 switch (report_type) {
323 case HID_FEATURE_REPORT:
324 report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
326 case HID_OUTPUT_REPORT:
327 report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
333 if (hidp_send_ctrl_message(hid->driver_data, report_type,
339 static void hidp_idle_timeout(unsigned long arg)
341 struct hidp_session *session = (struct hidp_session *) arg;
343 atomic_inc(&session->terminate);
344 hidp_schedule(session);
347 static void hidp_set_timer(struct hidp_session *session)
349 if (session->idle_to > 0)
350 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
353 static inline void hidp_del_timer(struct hidp_session *session)
355 if (session->idle_to > 0)
356 del_timer(&session->timer);
359 static void hidp_process_handshake(struct hidp_session *session,
362 BT_DBG("session %p param 0x%02x", session, param);
365 case HIDP_HSHK_SUCCESSFUL:
366 /* FIXME: Call into SET_ GET_ handlers here */
369 case HIDP_HSHK_NOT_READY:
370 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
371 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
372 case HIDP_HSHK_ERR_INVALID_PARAMETER:
373 /* FIXME: Call into SET_ GET_ handlers here */
376 case HIDP_HSHK_ERR_UNKNOWN:
379 case HIDP_HSHK_ERR_FATAL:
380 /* Device requests a reboot, as this is the only way this error
381 * can be recovered. */
382 __hidp_send_ctrl_message(session,
383 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
387 __hidp_send_ctrl_message(session,
388 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
393 static void hidp_process_hid_control(struct hidp_session *session,
396 BT_DBG("session %p param 0x%02x", session, param);
398 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
399 /* Flush the transmit queues */
400 skb_queue_purge(&session->ctrl_transmit);
401 skb_queue_purge(&session->intr_transmit);
403 /* Kill session thread */
404 atomic_inc(&session->terminate);
405 hidp_schedule(session);
409 static void hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
412 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
415 case HIDP_DATA_RTYPE_INPUT:
416 hidp_set_timer(session);
419 hidp_input_report(session, skb);
422 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
426 case HIDP_DATA_RTYPE_OTHER:
427 case HIDP_DATA_RTYPE_OUPUT:
428 case HIDP_DATA_RTYPE_FEATURE:
432 __hidp_send_ctrl_message(session,
433 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
437 static void hidp_recv_ctrl_frame(struct hidp_session *session,
440 unsigned char hdr, type, param;
442 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
447 type = hdr & HIDP_HEADER_TRANS_MASK;
448 param = hdr & HIDP_HEADER_PARAM_MASK;
451 case HIDP_TRANS_HANDSHAKE:
452 hidp_process_handshake(session, param);
455 case HIDP_TRANS_HID_CONTROL:
456 hidp_process_hid_control(session, param);
459 case HIDP_TRANS_DATA:
460 hidp_process_data(session, skb, param);
464 __hidp_send_ctrl_message(session,
465 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
472 static void hidp_recv_intr_frame(struct hidp_session *session,
477 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
482 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
483 hidp_set_timer(session);
486 hidp_input_report(session, skb);
489 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
490 BT_DBG("report len %d", skb->len);
493 BT_DBG("Unsupported protocol header 0x%02x", hdr);
499 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
501 struct kvec iv = { data, len };
504 BT_DBG("sock %p data %p len %d", sock, data, len);
509 memset(&msg, 0, sizeof(msg));
511 return kernel_sendmsg(sock, &msg, &iv, 1, len);
514 static void hidp_process_transmit(struct hidp_session *session)
518 BT_DBG("session %p", session);
520 while ((skb = skb_dequeue(&session->ctrl_transmit))) {
521 if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
522 skb_queue_head(&session->ctrl_transmit, skb);
526 hidp_set_timer(session);
530 while ((skb = skb_dequeue(&session->intr_transmit))) {
531 if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
532 skb_queue_head(&session->intr_transmit, skb);
536 hidp_set_timer(session);
541 static int hidp_session(void *arg)
543 struct hidp_session *session = arg;
544 struct sock *ctrl_sk = session->ctrl_sock->sk;
545 struct sock *intr_sk = session->intr_sock->sk;
547 int vendor = 0x0000, product = 0x0000;
548 wait_queue_t ctrl_wait, intr_wait;
550 BT_DBG("session %p", session);
552 if (session->input) {
553 vendor = session->input->id.vendor;
554 product = session->input->id.product;
558 vendor = session->hid->vendor;
559 product = session->hid->product;
562 daemonize("khidpd_%04x%04x", vendor, product);
563 set_user_nice(current, -15);
565 init_waitqueue_entry(&ctrl_wait, current);
566 init_waitqueue_entry(&intr_wait, current);
567 add_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait);
568 add_wait_queue(sk_sleep(intr_sk), &intr_wait);
569 while (!atomic_read(&session->terminate)) {
570 set_current_state(TASK_INTERRUPTIBLE);
572 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
575 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
577 hidp_recv_ctrl_frame(session, skb);
580 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
582 hidp_recv_intr_frame(session, skb);
585 hidp_process_transmit(session);
589 set_current_state(TASK_RUNNING);
590 remove_wait_queue(sk_sleep(intr_sk), &intr_wait);
591 remove_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait);
593 down_write(&hidp_session_sem);
595 hidp_del_timer(session);
597 if (session->input) {
598 input_unregister_device(session->input);
599 session->input = NULL;
603 hid_destroy_device(session->hid);
607 /* Wakeup user-space polling for socket errors */
608 session->intr_sock->sk->sk_err = EUNATCH;
609 session->ctrl_sock->sk->sk_err = EUNATCH;
611 hidp_schedule(session);
613 fput(session->intr_sock->file);
615 wait_event_timeout(*(sk_sleep(ctrl_sk)),
616 (ctrl_sk->sk_state == BT_CLOSED), msecs_to_jiffies(500));
618 fput(session->ctrl_sock->file);
620 __hidp_unlink_session(session);
622 up_write(&hidp_session_sem);
628 static struct device *hidp_get_device(struct hidp_session *session)
630 bdaddr_t *src = &bt_sk(session->ctrl_sock->sk)->src;
631 bdaddr_t *dst = &bt_sk(session->ctrl_sock->sk)->dst;
632 struct device *device = NULL;
633 struct hci_dev *hdev;
635 hdev = hci_get_route(dst, src);
639 session->conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
641 device = &session->conn->dev;
648 static int hidp_setup_input(struct hidp_session *session,
649 struct hidp_connadd_req *req)
651 struct input_dev *input;
654 input = input_allocate_device();
658 session->input = input;
660 input_set_drvdata(input, session);
662 input->name = "Bluetooth HID Boot Protocol Device";
664 input->id.bustype = BUS_BLUETOOTH;
665 input->id.vendor = req->vendor;
666 input->id.product = req->product;
667 input->id.version = req->version;
669 if (req->subclass & 0x40) {
670 set_bit(EV_KEY, input->evbit);
671 set_bit(EV_LED, input->evbit);
672 set_bit(EV_REP, input->evbit);
674 set_bit(LED_NUML, input->ledbit);
675 set_bit(LED_CAPSL, input->ledbit);
676 set_bit(LED_SCROLLL, input->ledbit);
677 set_bit(LED_COMPOSE, input->ledbit);
678 set_bit(LED_KANA, input->ledbit);
680 for (i = 0; i < sizeof(hidp_keycode); i++)
681 set_bit(hidp_keycode[i], input->keybit);
682 clear_bit(0, input->keybit);
685 if (req->subclass & 0x80) {
686 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
687 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
688 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
689 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
690 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
692 input->relbit[0] |= BIT_MASK(REL_WHEEL);
695 input->dev.parent = hidp_get_device(session);
697 input->event = hidp_input_event;
699 err = input_register_device(input);
701 hci_conn_put_device(session->conn);
708 static int hidp_open(struct hid_device *hid)
713 static void hidp_close(struct hid_device *hid)
717 static int hidp_parse(struct hid_device *hid)
719 struct hidp_session *session = hid->driver_data;
721 return hid_parse_report(session->hid, session->rd_data,
725 static int hidp_start(struct hid_device *hid)
727 struct hidp_session *session = hid->driver_data;
728 struct hid_report *report;
730 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
732 hidp_send_report(session, report);
734 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
736 hidp_send_report(session, report);
741 static void hidp_stop(struct hid_device *hid)
743 struct hidp_session *session = hid->driver_data;
745 skb_queue_purge(&session->ctrl_transmit);
746 skb_queue_purge(&session->intr_transmit);
751 static struct hid_ll_driver hidp_hid_driver = {
757 .hidinput_input_event = hidp_hidinput_event,
760 static int hidp_setup_hid(struct hidp_session *session,
761 struct hidp_connadd_req *req)
763 struct hid_device *hid;
766 session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
767 if (!session->rd_data)
770 if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
774 session->rd_size = req->rd_size;
776 hid = hid_allocate_device();
784 hid->driver_data = session;
786 hid->bus = BUS_BLUETOOTH;
787 hid->vendor = req->vendor;
788 hid->product = req->product;
789 hid->version = req->version;
790 hid->country = req->country;
792 strncpy(hid->name, req->name, 128);
793 strncpy(hid->phys, batostr(&bt_sk(session->ctrl_sock->sk)->src), 64);
794 strncpy(hid->uniq, batostr(&bt_sk(session->ctrl_sock->sk)->dst), 64);
796 hid->dev.parent = hidp_get_device(session);
797 hid->ll_driver = &hidp_hid_driver;
799 hid->hid_output_raw_report = hidp_output_raw_report;
801 err = hid_add_device(hid);
808 hid_destroy_device(hid);
812 kfree(session->rd_data);
813 session->rd_data = NULL;
818 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
820 struct hidp_session *session, *s;
825 if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) ||
826 bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst))
829 session = kzalloc(sizeof(struct hidp_session), GFP_KERNEL);
833 BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size);
835 down_write(&hidp_session_sem);
837 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
838 if (s && s->state == BT_CONNECTED) {
843 bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
845 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->omtu, l2cap_pi(ctrl_sock->sk)->imtu);
846 session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->omtu, l2cap_pi(intr_sock->sk)->imtu);
848 BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
850 session->ctrl_sock = ctrl_sock;
851 session->intr_sock = intr_sock;
852 session->state = BT_CONNECTED;
854 setup_timer(&session->timer, hidp_idle_timeout, (unsigned long)session);
856 skb_queue_head_init(&session->ctrl_transmit);
857 skb_queue_head_init(&session->intr_transmit);
859 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
860 session->idle_to = req->idle_to;
862 if (req->rd_size > 0) {
863 err = hidp_setup_hid(session, req);
864 if (err && err != -ENODEV)
869 err = hidp_setup_input(session, req);
874 __hidp_link_session(session);
876 hidp_set_timer(session);
878 err = kernel_thread(hidp_session, session, CLONE_KERNEL);
882 if (session->input) {
883 hidp_send_ctrl_message(session,
884 HIDP_TRANS_SET_PROTOCOL | HIDP_PROTO_BOOT, NULL, 0);
885 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
887 session->leds = 0xff;
888 hidp_input_event(session->input, EV_LED, 0, 0);
891 up_write(&hidp_session_sem);
895 hidp_del_timer(session);
897 __hidp_unlink_session(session);
899 if (session->input) {
900 input_unregister_device(session->input);
901 session->input = NULL;
905 hid_destroy_device(session->hid);
909 kfree(session->rd_data);
910 session->rd_data = NULL;
913 skb_queue_purge(&session->ctrl_transmit);
914 skb_queue_purge(&session->intr_transmit);
917 up_write(&hidp_session_sem);
919 input_free_device(session->input);
924 int hidp_del_connection(struct hidp_conndel_req *req)
926 struct hidp_session *session;
931 down_read(&hidp_session_sem);
933 session = __hidp_get_session(&req->bdaddr);
935 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
936 hidp_send_ctrl_message(session,
937 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, NULL, 0);
939 /* Flush the transmit queues */
940 skb_queue_purge(&session->ctrl_transmit);
941 skb_queue_purge(&session->intr_transmit);
943 /* Wakeup user-space polling for socket errors */
944 session->intr_sock->sk->sk_err = EUNATCH;
945 session->ctrl_sock->sk->sk_err = EUNATCH;
947 /* Kill session thread */
948 atomic_inc(&session->terminate);
949 hidp_schedule(session);
954 up_read(&hidp_session_sem);
958 int hidp_get_connlist(struct hidp_connlist_req *req)
965 down_read(&hidp_session_sem);
967 list_for_each(p, &hidp_session_list) {
968 struct hidp_session *session;
969 struct hidp_conninfo ci;
971 session = list_entry(p, struct hidp_session, list);
973 __hidp_copy_session(session, &ci);
975 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
980 if (++n >= req->cnum)
987 up_read(&hidp_session_sem);
991 int hidp_get_conninfo(struct hidp_conninfo *ci)
993 struct hidp_session *session;
996 down_read(&hidp_session_sem);
998 session = __hidp_get_session(&ci->bdaddr);
1000 __hidp_copy_session(session, ci);
1004 up_read(&hidp_session_sem);
1008 static const struct hid_device_id hidp_table[] = {
1009 { HID_BLUETOOTH_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1013 static struct hid_driver hidp_driver = {
1014 .name = "generic-bluetooth",
1015 .id_table = hidp_table,
1018 static int __init hidp_init(void)
1022 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1024 ret = hid_register_driver(&hidp_driver);
1028 ret = hidp_init_sockets();
1034 hid_unregister_driver(&hidp_driver);
1039 static void __exit hidp_exit(void)
1041 hidp_cleanup_sockets();
1042 hid_unregister_driver(&hidp_driver);
1045 module_init(hidp_init);
1046 module_exit(hidp_exit);
1048 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1049 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1050 MODULE_VERSION(VERSION);
1051 MODULE_LICENSE("GPL");
1052 MODULE_ALIAS("bt-proto-6");