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/config.h>
24 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/major.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/poll.h>
33 #include <linux/fcntl.h>
34 #include <linux/skbuff.h>
35 #include <linux/socket.h>
36 #include <linux/ioctl.h>
37 #include <linux/file.h>
38 #include <linux/init.h>
41 #include <linux/input.h>
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/l2cap.h>
48 #ifndef CONFIG_BT_HIDP_DEBUG
55 static DECLARE_RWSEM(hidp_session_sem);
56 static LIST_HEAD(hidp_session_list);
58 static unsigned char hidp_keycode[256] = {
59 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
60 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
61 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
62 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
63 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
64 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
65 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
66 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
67 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
68 122,123, 90, 91, 85, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
74 150,158,159,128,136,177,178,176,142,152,173,140
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);
98 static void __hidp_unlink_session(struct hidp_session *session)
100 list_del(&session->list);
101 module_put(THIS_MODULE);
104 static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
106 bacpy(&ci->bdaddr, &session->bdaddr);
108 ci->flags = session->flags;
109 ci->state = session->state;
112 ci->product = 0x0000;
113 ci->version = 0x0000;
114 memset(ci->name, 0, 128);
116 if (session->input) {
117 ci->vendor = session->input->id.vendor;
118 ci->product = session->input->id.product;
119 ci->version = session->input->id.version;
120 if (session->input->name)
121 strncpy(ci->name, session->input->name, 128);
123 strncpy(ci->name, "HID Boot Device", 128);
127 static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
129 struct hidp_session *session = dev->private;
131 unsigned char newleds;
133 BT_DBG("session %p hid %p data %p size %d", session, device, data, size);
138 newleds = (!!test_bit(LED_KANA, dev->led) << 3) |
139 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
140 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
141 (!!test_bit(LED_CAPSL, dev->led) << 1) |
142 (!!test_bit(LED_NUML, dev->led));
144 if (session->leds == newleds)
147 session->leds = newleds;
149 if (!(skb = alloc_skb(3, GFP_ATOMIC))) {
150 BT_ERR("Can't allocate memory for new frame");
154 *skb_put(skb, 1) = 0xa2;
155 *skb_put(skb, 1) = 0x01;
156 *skb_put(skb, 1) = newleds;
158 skb_queue_tail(&session->intr_transmit, skb);
160 hidp_schedule(session);
165 static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
167 struct input_dev *dev = session->input;
168 unsigned char *keys = session->keys;
169 unsigned char *udata = skb->data + 1;
170 signed char *sdata = skb->data + 1;
171 int i, size = skb->len - 1;
173 switch (skb->data[0]) {
174 case 0x01: /* Keyboard report */
175 for (i = 0; i < 8; i++)
176 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
178 for (i = 2; i < 8; i++) {
179 if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
180 if (hidp_keycode[keys[i]])
181 input_report_key(dev, hidp_keycode[keys[i]], 0);
183 BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
186 if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
187 if (hidp_keycode[udata[i]])
188 input_report_key(dev, hidp_keycode[udata[i]], 1);
190 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
194 memcpy(keys, udata, 8);
197 case 0x02: /* Mouse report */
198 input_report_key(dev, BTN_LEFT, sdata[0] & 0x01);
199 input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02);
200 input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
201 input_report_key(dev, BTN_SIDE, sdata[0] & 0x08);
202 input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10);
204 input_report_rel(dev, REL_X, sdata[1]);
205 input_report_rel(dev, REL_Y, sdata[2]);
208 input_report_rel(dev, REL_WHEEL, sdata[3]);
215 static void hidp_idle_timeout(unsigned long arg)
217 struct hidp_session *session = (struct hidp_session *) arg;
219 atomic_inc(&session->terminate);
220 hidp_schedule(session);
223 static inline void hidp_set_timer(struct hidp_session *session)
225 if (session->idle_to > 0)
226 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
229 static inline void hidp_del_timer(struct hidp_session *session)
231 if (session->idle_to > 0)
232 del_timer(&session->timer);
235 static inline void hidp_send_message(struct hidp_session *session, unsigned char hdr)
239 BT_DBG("session %p", session);
241 if (!(skb = alloc_skb(1, GFP_ATOMIC))) {
242 BT_ERR("Can't allocate memory for message");
246 *skb_put(skb, 1) = hdr;
248 skb_queue_tail(&session->ctrl_transmit, skb);
250 hidp_schedule(session);
253 static inline int hidp_recv_frame(struct hidp_session *session, struct sk_buff *skb)
257 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
263 hidp_set_timer(session);
266 hidp_input_report(session, skb);
268 BT_DBG("Unsupported protocol header 0x%02x", hdr);
275 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
277 struct iovec iv = { data, len };
280 BT_DBG("sock %p data %p len %d", sock, data, len);
285 memset(&msg, 0, sizeof(msg));
289 return sock_sendmsg(sock, &msg, len);
292 static int hidp_process_transmit(struct hidp_session *session)
296 BT_DBG("session %p", session);
298 while ((skb = skb_dequeue(&session->ctrl_transmit))) {
299 if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
300 skb_queue_head(&session->ctrl_transmit, skb);
304 hidp_set_timer(session);
308 while ((skb = skb_dequeue(&session->intr_transmit))) {
309 if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
310 skb_queue_head(&session->intr_transmit, skb);
314 hidp_set_timer(session);
318 return skb_queue_len(&session->ctrl_transmit) +
319 skb_queue_len(&session->intr_transmit);
322 static int hidp_session(void *arg)
324 struct hidp_session *session = arg;
325 struct sock *ctrl_sk = session->ctrl_sock->sk;
326 struct sock *intr_sk = session->intr_sock->sk;
328 int vendor = 0x0000, product = 0x0000;
329 wait_queue_t ctrl_wait, intr_wait;
330 unsigned long timeo = HZ;
332 BT_DBG("session %p", session);
334 if (session->input) {
335 vendor = session->input->id.vendor;
336 product = session->input->id.product;
339 daemonize("khidpd_%04x%04x", vendor, product);
340 set_user_nice(current, -15);
341 current->flags |= PF_NOFREEZE;
345 init_waitqueue_entry(&ctrl_wait, current);
346 init_waitqueue_entry(&intr_wait, current);
347 add_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
348 add_wait_queue(intr_sk->sk_sleep, &intr_wait);
349 while (!atomic_read(&session->terminate)) {
350 set_current_state(TASK_INTERRUPTIBLE);
352 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
355 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
357 hidp_recv_frame(session, skb);
360 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
362 hidp_recv_frame(session, skb);
365 hidp_process_transmit(session);
369 set_current_state(TASK_RUNNING);
370 remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
371 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
373 down_write(&hidp_session_sem);
375 hidp_del_timer(session);
377 if (intr_sk->sk_state != BT_CONNECTED) {
378 init_waitqueue_entry(&ctrl_wait, current);
379 add_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
380 while (timeo && ctrl_sk->sk_state != BT_CLOSED) {
381 set_current_state(TASK_INTERRUPTIBLE);
382 timeo = schedule_timeout(timeo);
384 set_current_state(TASK_RUNNING);
385 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
389 fput(session->ctrl_sock->file);
391 init_waitqueue_entry(&intr_wait, current);
392 add_wait_queue(intr_sk->sk_sleep, &intr_wait);
393 while (timeo && intr_sk->sk_state != BT_CLOSED) {
394 set_current_state(TASK_INTERRUPTIBLE);
395 timeo = schedule_timeout(timeo);
397 set_current_state(TASK_RUNNING);
398 remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
400 fput(session->intr_sock->file);
402 __hidp_unlink_session(session);
404 if (session->input) {
405 input_unregister_device(session->input);
406 kfree(session->input);
409 up_write(&hidp_session_sem);
415 static inline void hidp_setup_input(struct hidp_session *session, struct hidp_connadd_req *req)
417 struct input_dev *input = session->input;
420 input->private = session;
422 input->id.bustype = BUS_BLUETOOTH;
423 input->id.vendor = req->vendor;
424 input->id.product = req->product;
425 input->id.version = req->version;
427 if (req->subclass & 0x40) {
428 set_bit(EV_KEY, input->evbit);
429 set_bit(EV_LED, input->evbit);
430 set_bit(EV_REP, input->evbit);
432 set_bit(LED_NUML, input->ledbit);
433 set_bit(LED_CAPSL, input->ledbit);
434 set_bit(LED_SCROLLL, input->ledbit);
435 set_bit(LED_COMPOSE, input->ledbit);
436 set_bit(LED_KANA, input->ledbit);
438 for (i = 0; i < sizeof(hidp_keycode); i++)
439 set_bit(hidp_keycode[i], input->keybit);
440 clear_bit(0, input->keybit);
443 if (req->subclass & 0x80) {
444 input->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
445 input->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
446 input->relbit[0] = BIT(REL_X) | BIT(REL_Y);
447 input->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
448 input->relbit[0] |= BIT(REL_WHEEL);
451 input->event = hidp_input_event;
453 input_register_device(input);
456 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
458 struct hidp_session *session, *s;
463 if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) ||
464 bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst))
467 session = kmalloc(sizeof(struct hidp_session), GFP_KERNEL);
470 memset(session, 0, sizeof(struct hidp_session));
472 session->input = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
473 if (!session->input) {
477 memset(session->input, 0, sizeof(struct input_dev));
479 down_write(&hidp_session_sem);
481 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
482 if (s && s->state == BT_CONNECTED) {
487 bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
489 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->omtu, l2cap_pi(ctrl_sock->sk)->imtu);
490 session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->omtu, l2cap_pi(intr_sock->sk)->imtu);
492 BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
494 session->ctrl_sock = ctrl_sock;
495 session->intr_sock = intr_sock;
496 session->state = BT_CONNECTED;
498 init_timer(&session->timer);
500 session->timer.function = hidp_idle_timeout;
501 session->timer.data = (unsigned long) session;
503 skb_queue_head_init(&session->ctrl_transmit);
504 skb_queue_head_init(&session->intr_transmit);
506 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
507 session->idle_to = req->idle_to;
510 hidp_setup_input(session, req);
512 __hidp_link_session(session);
514 hidp_set_timer(session);
516 err = kernel_thread(hidp_session, session, CLONE_KERNEL);
520 if (session->input) {
521 hidp_send_message(session, 0x70);
522 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
524 session->leds = 0xff;
525 hidp_input_event(session->input, EV_LED, 0, 0);
528 up_write(&hidp_session_sem);
532 hidp_del_timer(session);
534 __hidp_unlink_session(session);
537 input_unregister_device(session->input);
540 up_write(&hidp_session_sem);
543 kfree(session->input);
549 int hidp_del_connection(struct hidp_conndel_req *req)
551 struct hidp_session *session;
556 down_read(&hidp_session_sem);
558 session = __hidp_get_session(&req->bdaddr);
560 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
561 hidp_send_message(session, 0x15);
563 /* Flush the transmit queues */
564 skb_queue_purge(&session->ctrl_transmit);
565 skb_queue_purge(&session->intr_transmit);
567 /* Kill session thread */
568 atomic_inc(&session->terminate);
569 hidp_schedule(session);
574 up_read(&hidp_session_sem);
578 int hidp_get_connlist(struct hidp_connlist_req *req)
585 down_read(&hidp_session_sem);
587 list_for_each(p, &hidp_session_list) {
588 struct hidp_session *session;
589 struct hidp_conninfo ci;
591 session = list_entry(p, struct hidp_session, list);
593 __hidp_copy_session(session, &ci);
595 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
600 if (++n >= req->cnum)
607 up_read(&hidp_session_sem);
611 int hidp_get_conninfo(struct hidp_conninfo *ci)
613 struct hidp_session *session;
616 down_read(&hidp_session_sem);
618 session = __hidp_get_session(&ci->bdaddr);
620 __hidp_copy_session(session, ci);
624 up_read(&hidp_session_sem);
628 static int __init hidp_init(void)
632 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
634 return hidp_init_sockets();
637 static void __exit hidp_exit(void)
639 hidp_cleanup_sockets();
642 module_init(hidp_init);
643 module_exit(hidp_exit);
645 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
646 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
647 MODULE_VERSION(VERSION);
648 MODULE_LICENSE("GPL");
649 MODULE_ALIAS("bt-proto-6");