fed up with those stupid warnings
[mmotm.git] / net / bluetooth / hidp / core.c
blob115b3584dc037285cf85d7c276c7c1c9341470a0
1 /*
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>
39 #include <net/sock.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>
49 #include "hidp.h"
51 #define VERSION "1.2"
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 /* HIDP device quirks */
78 enum {
79 HIDP_QUIRK_USE_INPUT_SESSION
82 struct quirk_id {
83 __u16 vendor;
84 __u16 product;
85 unsigned long quirks;
88 static const struct quirk_id hidp_quirks[] = {
90 /* Lamarque: Acer Bluetooth Optical Rechargeable Mouse
91 * does not work properly with hid session since 2.6.27. */
92 .vendor = 0x0458,
93 .product = 0x0058,
94 .quirks = 1 << HIDP_QUIRK_USE_INPUT_SESSION
98 static int quirk_test_bit(__u16 vendor, __u16 product, int quirk)
100 int i;
102 for (i = 0; i < ARRAY_SIZE(hidp_quirks); i++) {
103 const struct quirk_id *q = hidp_quirks + i;
105 if (q->vendor == vendor && q->product == product)
106 return test_bit(quirk, &q->quirks);
109 return 0;
112 static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)
114 struct hidp_session *session;
115 struct list_head *p;
117 BT_DBG("");
119 list_for_each(p, &hidp_session_list) {
120 session = list_entry(p, struct hidp_session, list);
121 if (!bacmp(bdaddr, &session->bdaddr))
122 return session;
124 return NULL;
127 static void __hidp_link_session(struct hidp_session *session)
129 __module_get(THIS_MODULE);
130 list_add(&session->list, &hidp_session_list);
132 hci_conn_hold_device(session->conn);
135 static void __hidp_unlink_session(struct hidp_session *session)
137 hci_conn_put_device(session->conn);
139 list_del(&session->list);
140 module_put(THIS_MODULE);
143 static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
145 bacpy(&ci->bdaddr, &session->bdaddr);
147 ci->flags = session->flags;
148 ci->state = session->state;
150 ci->vendor = 0x0000;
151 ci->product = 0x0000;
152 ci->version = 0x0000;
153 memset(ci->name, 0, 128);
155 if (session->input) {
156 ci->vendor = session->input->id.vendor;
157 ci->product = session->input->id.product;
158 ci->version = session->input->id.version;
159 if (session->input->name)
160 strncpy(ci->name, session->input->name, 128);
161 else
162 strncpy(ci->name, "HID Boot Device", 128);
165 if (session->hid) {
166 ci->vendor = session->hid->vendor;
167 ci->product = session->hid->product;
168 ci->version = session->hid->version;
169 strncpy(ci->name, session->hid->name, 128);
173 static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
174 unsigned int type, unsigned int code, int value)
176 unsigned char newleds;
177 struct sk_buff *skb;
179 BT_DBG("session %p type %d code %d value %d", session, type, code, value);
181 if (type != EV_LED)
182 return -1;
184 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
185 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
186 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
187 (!!test_bit(LED_CAPSL, dev->led) << 1) |
188 (!!test_bit(LED_NUML, dev->led));
190 if (session->leds == newleds)
191 return 0;
193 session->leds = newleds;
195 if (!(skb = alloc_skb(3, GFP_ATOMIC))) {
196 BT_ERR("Can't allocate memory for new frame");
197 return -ENOMEM;
200 *skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
201 *skb_put(skb, 1) = 0x01;
202 *skb_put(skb, 1) = newleds;
204 skb_queue_tail(&session->intr_transmit, skb);
206 hidp_schedule(session);
208 return 0;
211 static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
213 struct hid_device *hid = input_get_drvdata(dev);
214 struct hidp_session *session = hid->driver_data;
216 return hidp_queue_event(session, dev, type, code, value);
219 static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
221 struct hidp_session *session = input_get_drvdata(dev);
223 return hidp_queue_event(session, dev, type, code, value);
226 static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
228 struct input_dev *dev = session->input;
229 unsigned char *keys = session->keys;
230 unsigned char *udata = skb->data + 1;
231 signed char *sdata = skb->data + 1;
232 int i, size = skb->len - 1;
234 switch (skb->data[0]) {
235 case 0x01: /* Keyboard report */
236 for (i = 0; i < 8; i++)
237 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
239 /* If all the key codes have been set to 0x01, it means
240 * too many keys were pressed at the same time. */
241 if (!memcmp(udata + 2, hidp_mkeyspat, 6))
242 break;
244 for (i = 2; i < 8; i++) {
245 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
246 if (hidp_keycode[keys[i]])
247 input_report_key(dev, hidp_keycode[keys[i]], 0);
248 else
249 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
252 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
253 if (hidp_keycode[udata[i]])
254 input_report_key(dev, hidp_keycode[udata[i]], 1);
255 else
256 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
260 memcpy(keys, udata, 8);
261 break;
263 case 0x02: /* Mouse report */
264 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
265 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
266 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
267 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
268 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
270 input_report_rel(dev, REL_X, sdata[1]);
271 input_report_rel(dev, REL_Y, sdata[2]);
273 if (size > 3)
274 input_report_rel(dev, REL_WHEEL, sdata[3]);
275 break;
278 input_sync(dev);
281 static int hidp_queue_report(struct hidp_session *session,
282 unsigned char *data, int size)
284 struct sk_buff *skb;
286 BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size);
288 if (!(skb = alloc_skb(size + 1, GFP_ATOMIC))) {
289 BT_ERR("Can't allocate memory for new frame");
290 return -ENOMEM;
293 *skb_put(skb, 1) = 0xa2;
294 if (size > 0)
295 memcpy(skb_put(skb, size), data, size);
297 skb_queue_tail(&session->intr_transmit, skb);
299 hidp_schedule(session);
301 return 0;
304 static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
306 unsigned char buf[32];
307 int rsize;
309 rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
310 if (rsize > sizeof(buf))
311 return -EIO;
313 hid_output_report(report, buf);
315 return hidp_queue_report(session, buf, rsize);
318 static void hidp_idle_timeout(unsigned long arg)
320 struct hidp_session *session = (struct hidp_session *) arg;
322 atomic_inc(&session->terminate);
323 hidp_schedule(session);
326 static void hidp_set_timer(struct hidp_session *session)
328 if (session->idle_to > 0)
329 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
332 static inline void hidp_del_timer(struct hidp_session *session)
334 if (session->idle_to > 0)
335 del_timer(&session->timer);
338 static int __hidp_send_ctrl_message(struct hidp_session *session,
339 unsigned char hdr, unsigned char *data, int size)
341 struct sk_buff *skb;
343 BT_DBG("session %p data %p size %d", session, data, size);
345 if (!(skb = alloc_skb(size + 1, GFP_ATOMIC))) {
346 BT_ERR("Can't allocate memory for new frame");
347 return -ENOMEM;
350 *skb_put(skb, 1) = hdr;
351 if (data && size > 0)
352 memcpy(skb_put(skb, size), data, size);
354 skb_queue_tail(&session->ctrl_transmit, skb);
356 return 0;
359 static inline int hidp_send_ctrl_message(struct hidp_session *session,
360 unsigned char hdr, unsigned char *data, int size)
362 int err;
364 err = __hidp_send_ctrl_message(session, hdr, data, size);
366 hidp_schedule(session);
368 return err;
371 static void hidp_process_handshake(struct hidp_session *session,
372 unsigned char param)
374 BT_DBG("session %p param 0x%02x", session, param);
376 switch (param) {
377 case HIDP_HSHK_SUCCESSFUL:
378 /* FIXME: Call into SET_ GET_ handlers here */
379 break;
381 case HIDP_HSHK_NOT_READY:
382 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
383 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
384 case HIDP_HSHK_ERR_INVALID_PARAMETER:
385 /* FIXME: Call into SET_ GET_ handlers here */
386 break;
388 case HIDP_HSHK_ERR_UNKNOWN:
389 break;
391 case HIDP_HSHK_ERR_FATAL:
392 /* Device requests a reboot, as this is the only way this error
393 * can be recovered. */
394 __hidp_send_ctrl_message(session,
395 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
396 break;
398 default:
399 __hidp_send_ctrl_message(session,
400 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
401 break;
405 static void hidp_process_hid_control(struct hidp_session *session,
406 unsigned char param)
408 BT_DBG("session %p param 0x%02x", session, param);
410 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
411 /* Flush the transmit queues */
412 skb_queue_purge(&session->ctrl_transmit);
413 skb_queue_purge(&session->intr_transmit);
415 /* Kill session thread */
416 atomic_inc(&session->terminate);
417 hidp_schedule(session);
421 static void hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
422 unsigned char param)
424 BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);
426 switch (param) {
427 case HIDP_DATA_RTYPE_INPUT:
428 hidp_set_timer(session);
430 if (session->input)
431 hidp_input_report(session, skb);
433 if (session->hid)
434 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
436 break;
438 case HIDP_DATA_RTYPE_OTHER:
439 case HIDP_DATA_RTYPE_OUPUT:
440 case HIDP_DATA_RTYPE_FEATURE:
441 break;
443 default:
444 __hidp_send_ctrl_message(session,
445 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
449 static void hidp_recv_ctrl_frame(struct hidp_session *session,
450 struct sk_buff *skb)
452 unsigned char hdr, type, param;
454 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
456 hdr = skb->data[0];
457 skb_pull(skb, 1);
459 type = hdr & HIDP_HEADER_TRANS_MASK;
460 param = hdr & HIDP_HEADER_PARAM_MASK;
462 switch (type) {
463 case HIDP_TRANS_HANDSHAKE:
464 hidp_process_handshake(session, param);
465 break;
467 case HIDP_TRANS_HID_CONTROL:
468 hidp_process_hid_control(session, param);
469 break;
471 case HIDP_TRANS_DATA:
472 hidp_process_data(session, skb, param);
473 break;
475 default:
476 __hidp_send_ctrl_message(session,
477 HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
478 break;
481 kfree_skb(skb);
484 static void hidp_recv_intr_frame(struct hidp_session *session,
485 struct sk_buff *skb)
487 unsigned char hdr;
489 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
491 hdr = skb->data[0];
492 skb_pull(skb, 1);
494 if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
495 hidp_set_timer(session);
497 if (session->input)
498 hidp_input_report(session, skb);
500 if (session->hid) {
501 hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
502 BT_DBG("report len %d", skb->len);
504 } else {
505 BT_DBG("Unsupported protocol header 0x%02x", hdr);
508 kfree_skb(skb);
511 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
513 struct kvec iv = { data, len };
514 struct msghdr msg;
516 BT_DBG("sock %p data %p len %d", sock, data, len);
518 if (!len)
519 return 0;
521 memset(&msg, 0, sizeof(msg));
523 return kernel_sendmsg(sock, &msg, &iv, 1, len);
526 static void hidp_process_transmit(struct hidp_session *session)
528 struct sk_buff *skb;
530 BT_DBG("session %p", session);
532 while ((skb = skb_dequeue(&session->ctrl_transmit))) {
533 if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
534 skb_queue_head(&session->ctrl_transmit, skb);
535 break;
538 hidp_set_timer(session);
539 kfree_skb(skb);
542 while ((skb = skb_dequeue(&session->intr_transmit))) {
543 if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
544 skb_queue_head(&session->intr_transmit, skb);
545 break;
548 hidp_set_timer(session);
549 kfree_skb(skb);
553 static int hidp_session(void *arg)
555 struct hidp_session *session = arg;
556 struct sock *ctrl_sk = session->ctrl_sock->sk;
557 struct sock *intr_sk = session->intr_sock->sk;
558 struct sk_buff *skb;
559 int vendor = 0x0000, product = 0x0000;
560 wait_queue_t ctrl_wait, intr_wait;
562 BT_DBG("session %p", session);
564 if (session->input) {
565 vendor = session->input->id.vendor;
566 product = session->input->id.product;
569 if (session->hid) {
570 vendor = session->hid->vendor;
571 product = session->hid->product;
574 daemonize("khidpd_%04x%04x", vendor, product);
575 set_user_nice(current, -15);
577 init_waitqueue_entry(&ctrl_wait, current);
578 init_waitqueue_entry(&intr_wait, current);
579 add_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
580 add_wait_queue(intr_sk->sk_sleep, &intr_wait);
581 while (!atomic_read(&session->terminate)) {
582 set_current_state(TASK_INTERRUPTIBLE);
584 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
585 break;
587 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
588 skb_orphan(skb);
589 hidp_recv_ctrl_frame(session, skb);
592 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
593 skb_orphan(skb);
594 hidp_recv_intr_frame(session, skb);
597 hidp_process_transmit(session);
599 schedule();
601 set_current_state(TASK_RUNNING);
602 remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
603 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
605 down_write(&hidp_session_sem);
607 hidp_del_timer(session);
609 if (session->input) {
610 input_unregister_device(session->input);
611 session->input = NULL;
614 if (session->hid) {
615 hid_destroy_device(session->hid);
616 session->hid = NULL;
619 /* Wakeup user-space polling for socket errors */
620 session->intr_sock->sk->sk_err = EUNATCH;
621 session->ctrl_sock->sk->sk_err = EUNATCH;
623 hidp_schedule(session);
625 fput(session->intr_sock->file);
627 wait_event_timeout(*(ctrl_sk->sk_sleep),
628 (ctrl_sk->sk_state == BT_CLOSED), msecs_to_jiffies(500));
630 fput(session->ctrl_sock->file);
632 __hidp_unlink_session(session);
634 up_write(&hidp_session_sem);
636 kfree(session);
637 return 0;
640 static struct device *hidp_get_device(struct hidp_session *session)
642 bdaddr_t *src = &bt_sk(session->ctrl_sock->sk)->src;
643 bdaddr_t *dst = &bt_sk(session->ctrl_sock->sk)->dst;
644 struct device *device = NULL;
645 struct hci_dev *hdev;
647 hdev = hci_get_route(dst, src);
648 if (!hdev)
649 return NULL;
651 session->conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
652 if (session->conn)
653 device = &session->conn->dev;
655 hci_dev_put(hdev);
657 return device;
660 static int hidp_setup_input(struct hidp_session *session,
661 struct hidp_connadd_req *req)
663 struct input_dev *input;
664 int err, i;
666 input = input_allocate_device();
667 if (!input)
668 return -ENOMEM;
670 session->input = input;
672 input_set_drvdata(input, session);
674 input->name = "Bluetooth HID Boot Protocol Device";
676 input->id.bustype = BUS_BLUETOOTH;
677 input->id.vendor = req->vendor;
678 input->id.product = req->product;
679 input->id.version = req->version;
681 if (req->subclass & 0x40) {
682 set_bit(EV_KEY, input->evbit);
683 set_bit(EV_LED, input->evbit);
684 set_bit(EV_REP, input->evbit);
686 set_bit(LED_NUML, input->ledbit);
687 set_bit(LED_CAPSL, input->ledbit);
688 set_bit(LED_SCROLLL, input->ledbit);
689 set_bit(LED_COMPOSE, input->ledbit);
690 set_bit(LED_KANA, input->ledbit);
692 for (i = 0; i < sizeof(hidp_keycode); i++)
693 set_bit(hidp_keycode[i], input->keybit);
694 clear_bit(0, input->keybit);
697 if (req->subclass & 0x80) {
698 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
699 input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
700 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
701 input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
702 input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
703 BIT_MASK(BTN_EXTRA);
704 input->relbit[0] |= BIT_MASK(REL_WHEEL);
707 input->dev.parent = hidp_get_device(session);
709 input->event = hidp_input_event;
711 err = input_register_device(input);
712 if (err < 0) {
713 hci_conn_put_device(session->conn);
714 return err;
717 return 0;
720 static int hidp_open(struct hid_device *hid)
722 return 0;
725 static void hidp_close(struct hid_device *hid)
729 static int hidp_parse(struct hid_device *hid)
731 struct hidp_session *session = hid->driver_data;
732 struct hidp_connadd_req *req = session->req;
733 unsigned char *buf;
734 int ret;
736 buf = kmalloc(req->rd_size, GFP_KERNEL);
737 if (!buf)
738 return -ENOMEM;
740 if (copy_from_user(buf, req->rd_data, req->rd_size)) {
741 kfree(buf);
742 return -EFAULT;
745 ret = hid_parse_report(session->hid, buf, req->rd_size);
747 kfree(buf);
749 if (ret)
750 return ret;
752 session->req = NULL;
754 return 0;
757 static int hidp_start(struct hid_device *hid)
759 struct hidp_session *session = hid->driver_data;
760 struct hid_report *report;
762 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
763 report_list, list)
764 hidp_send_report(session, report);
766 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
767 report_list, list)
768 hidp_send_report(session, report);
770 return 0;
773 static void hidp_stop(struct hid_device *hid)
775 struct hidp_session *session = hid->driver_data;
777 skb_queue_purge(&session->ctrl_transmit);
778 skb_queue_purge(&session->intr_transmit);
780 hid->claimed = 0;
783 static struct hid_ll_driver hidp_hid_driver = {
784 .parse = hidp_parse,
785 .start = hidp_start,
786 .stop = hidp_stop,
787 .open = hidp_open,
788 .close = hidp_close,
789 .hidinput_input_event = hidp_hidinput_event,
792 static int hidp_setup_hid(struct hidp_session *session,
793 struct hidp_connadd_req *req)
795 struct hid_device *hid;
796 bdaddr_t src, dst;
797 int err;
799 hid = hid_allocate_device();
800 if (IS_ERR(hid))
801 return PTR_ERR(session->hid);
803 session->hid = hid;
804 session->req = req;
805 hid->driver_data = session;
807 baswap(&src, &bt_sk(session->ctrl_sock->sk)->src);
808 baswap(&dst, &bt_sk(session->ctrl_sock->sk)->dst);
810 hid->bus = BUS_BLUETOOTH;
811 hid->vendor = req->vendor;
812 hid->product = req->product;
813 hid->version = req->version;
814 hid->country = req->country;
816 strncpy(hid->name, req->name, 128);
817 strncpy(hid->phys, batostr(&src), 64);
818 strncpy(hid->uniq, batostr(&dst), 64);
820 hid->dev.parent = hidp_get_device(session);
821 hid->ll_driver = &hidp_hid_driver;
823 err = hid_add_device(hid);
824 if (err < 0)
825 goto failed;
827 return 0;
829 failed:
830 hid_destroy_device(hid);
831 session->hid = NULL;
833 return err;
836 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
838 struct hidp_session *session, *s;
839 int err;
841 BT_DBG("");
843 if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) ||
844 bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst))
845 return -ENOTUNIQ;
847 session = kzalloc(sizeof(struct hidp_session), GFP_KERNEL);
848 if (!session)
849 return -ENOMEM;
851 BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size);
853 down_write(&hidp_session_sem);
855 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
856 if (s && s->state == BT_CONNECTED) {
857 err = -EEXIST;
858 goto failed;
861 bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
863 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->omtu, l2cap_pi(ctrl_sock->sk)->imtu);
864 session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->omtu, l2cap_pi(intr_sock->sk)->imtu);
866 BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
868 session->ctrl_sock = ctrl_sock;
869 session->intr_sock = intr_sock;
870 session->state = BT_CONNECTED;
872 setup_timer(&session->timer, hidp_idle_timeout, (unsigned long)session);
874 skb_queue_head_init(&session->ctrl_transmit);
875 skb_queue_head_init(&session->intr_transmit);
877 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
878 session->idle_to = req->idle_to;
880 if (req->rd_size > 0 &&
881 !quirk_test_bit(req->vendor, req->product,
882 HIDP_QUIRK_USE_INPUT_SESSION)) {
883 err = hidp_setup_hid(session, req);
884 if (err && err != -ENODEV)
885 goto purge;
888 if (!session->hid) {
889 err = hidp_setup_input(session, req);
890 if (err < 0)
891 goto purge;
894 __hidp_link_session(session);
896 hidp_set_timer(session);
898 err = kernel_thread(hidp_session, session, CLONE_KERNEL);
899 if (err < 0)
900 goto unlink;
902 if (session->input) {
903 hidp_send_ctrl_message(session,
904 HIDP_TRANS_SET_PROTOCOL | HIDP_PROTO_BOOT, NULL, 0);
905 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
907 session->leds = 0xff;
908 hidp_input_event(session->input, EV_LED, 0, 0);
911 up_write(&hidp_session_sem);
912 return 0;
914 unlink:
915 hidp_del_timer(session);
917 __hidp_unlink_session(session);
919 if (session->input) {
920 input_unregister_device(session->input);
921 session->input = NULL;
924 if (session->hid) {
925 hid_destroy_device(session->hid);
926 session->hid = NULL;
929 purge:
930 skb_queue_purge(&session->ctrl_transmit);
931 skb_queue_purge(&session->intr_transmit);
933 failed:
934 up_write(&hidp_session_sem);
936 input_free_device(session->input);
937 kfree(session);
938 return err;
941 int hidp_del_connection(struct hidp_conndel_req *req)
943 struct hidp_session *session;
944 int err = 0;
946 BT_DBG("");
948 down_read(&hidp_session_sem);
950 session = __hidp_get_session(&req->bdaddr);
951 if (session) {
952 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
953 hidp_send_ctrl_message(session,
954 HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, NULL, 0);
955 } else {
956 /* Flush the transmit queues */
957 skb_queue_purge(&session->ctrl_transmit);
958 skb_queue_purge(&session->intr_transmit);
960 /* Wakeup user-space polling for socket errors */
961 session->intr_sock->sk->sk_err = EUNATCH;
962 session->ctrl_sock->sk->sk_err = EUNATCH;
964 /* Kill session thread */
965 atomic_inc(&session->terminate);
966 hidp_schedule(session);
968 } else
969 err = -ENOENT;
971 up_read(&hidp_session_sem);
972 return err;
975 int hidp_get_connlist(struct hidp_connlist_req *req)
977 struct list_head *p;
978 int err = 0, n = 0;
980 BT_DBG("");
982 down_read(&hidp_session_sem);
984 list_for_each(p, &hidp_session_list) {
985 struct hidp_session *session;
986 struct hidp_conninfo ci;
988 session = list_entry(p, struct hidp_session, list);
990 __hidp_copy_session(session, &ci);
992 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
993 err = -EFAULT;
994 break;
997 if (++n >= req->cnum)
998 break;
1000 req->ci++;
1002 req->cnum = n;
1004 up_read(&hidp_session_sem);
1005 return err;
1008 int hidp_get_conninfo(struct hidp_conninfo *ci)
1010 struct hidp_session *session;
1011 int err = 0;
1013 down_read(&hidp_session_sem);
1015 session = __hidp_get_session(&ci->bdaddr);
1016 if (session)
1017 __hidp_copy_session(session, ci);
1018 else
1019 err = -ENOENT;
1021 up_read(&hidp_session_sem);
1022 return err;
1025 static const struct hid_device_id hidp_table[] = {
1026 { HID_BLUETOOTH_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1030 static struct hid_driver hidp_driver = {
1031 .name = "generic-bluetooth",
1032 .id_table = hidp_table,
1035 static int __init hidp_init(void)
1037 int ret;
1039 l2cap_load();
1041 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
1043 ret = hid_register_driver(&hidp_driver);
1044 if (ret)
1045 goto err;
1047 ret = hidp_init_sockets();
1048 if (ret)
1049 goto err_drv;
1051 return 0;
1052 err_drv:
1053 hid_unregister_driver(&hidp_driver);
1054 err:
1055 return ret;
1058 static void __exit hidp_exit(void)
1060 hidp_cleanup_sockets();
1061 hid_unregister_driver(&hidp_driver);
1064 module_init(hidp_init);
1065 module_exit(hidp_exit);
1067 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1068 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
1069 MODULE_VERSION(VERSION);
1070 MODULE_LICENSE("GPL");
1071 MODULE_ALIAS("bt-proto-6");