initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / bluetooth / hidp / core.c
blobee652ae9c34217d08bc8ceedf7b580b24d293c80
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/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>
39 #include <net/sock.h>
41 #include <linux/input.h>
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/l2cap.h>
46 #include "hidp.h"
48 #ifndef CONFIG_BT_HIDP_DEBUG
49 #undef BT_DBG
50 #define BT_DBG(D...)
51 #endif
53 #define VERSION "1.0"
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;
80 struct list_head *p;
82 BT_DBG("");
84 list_for_each(p, &hidp_session_list) {
85 session = list_entry(p, struct hidp_session, list);
86 if (!bacmp(bdaddr, &session->bdaddr))
87 return session;
89 return NULL;
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;
111 ci->vendor = 0x0000;
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);
122 else
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;
130 struct sk_buff *skb;
131 unsigned char newleds;
133 BT_DBG("session %p hid %p data %p size %d", session, device, data, size);
135 if (type != EV_LED)
136 return -1;
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)
145 return 0;
147 session->leds = newleds;
149 if (!(skb = alloc_skb(3, GFP_ATOMIC))) {
150 BT_ERR("Can't allocate memory for new frame");
151 return -ENOMEM;
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);
162 return 0;
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);
182 else
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);
189 else
190 BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
194 memcpy(keys, udata, 8);
195 break;
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]);
207 if (size > 3)
208 input_report_rel(dev, REL_WHEEL, sdata[3]);
209 break;
212 input_sync(dev);
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)
237 struct sk_buff *skb;
239 BT_DBG("session %p", session);
241 if (!(skb = alloc_skb(1, GFP_ATOMIC))) {
242 BT_ERR("Can't allocate memory for message");
243 return;
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)
255 __u8 hdr;
257 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
259 hdr = skb->data[0];
260 skb_pull(skb, 1);
262 if (hdr == 0xa1) {
263 hidp_set_timer(session);
265 if (session->input)
266 hidp_input_report(session, skb);
267 } else {
268 BT_DBG("Unsupported protocol header 0x%02x", hdr);
271 kfree_skb(skb);
272 return 0;
275 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
277 struct kvec iv = { data, len };
278 struct msghdr msg;
280 BT_DBG("sock %p data %p len %d", sock, data, len);
282 if (!len)
283 return 0;
285 memset(&msg, 0, sizeof(msg));
287 return kernel_sendmsg(sock, &msg, &iv, 1, len);
290 static int hidp_process_transmit(struct hidp_session *session)
292 struct sk_buff *skb;
294 BT_DBG("session %p", session);
296 while ((skb = skb_dequeue(&session->ctrl_transmit))) {
297 if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
298 skb_queue_head(&session->ctrl_transmit, skb);
299 break;
302 hidp_set_timer(session);
303 kfree_skb(skb);
306 while ((skb = skb_dequeue(&session->intr_transmit))) {
307 if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
308 skb_queue_head(&session->intr_transmit, skb);
309 break;
312 hidp_set_timer(session);
313 kfree_skb(skb);
316 return skb_queue_len(&session->ctrl_transmit) +
317 skb_queue_len(&session->intr_transmit);
320 static int hidp_session(void *arg)
322 struct hidp_session *session = arg;
323 struct sock *ctrl_sk = session->ctrl_sock->sk;
324 struct sock *intr_sk = session->intr_sock->sk;
325 struct sk_buff *skb;
326 int vendor = 0x0000, product = 0x0000;
327 wait_queue_t ctrl_wait, intr_wait;
328 unsigned long timeo = HZ;
330 BT_DBG("session %p", session);
332 if (session->input) {
333 vendor = session->input->id.vendor;
334 product = session->input->id.product;
337 daemonize("khidpd_%04x%04x", vendor, product);
338 set_user_nice(current, -15);
339 current->flags |= PF_NOFREEZE;
341 init_waitqueue_entry(&ctrl_wait, current);
342 init_waitqueue_entry(&intr_wait, current);
343 add_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
344 add_wait_queue(intr_sk->sk_sleep, &intr_wait);
345 while (!atomic_read(&session->terminate)) {
346 set_current_state(TASK_INTERRUPTIBLE);
348 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
349 break;
351 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
352 skb_orphan(skb);
353 hidp_recv_frame(session, skb);
356 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
357 skb_orphan(skb);
358 hidp_recv_frame(session, skb);
361 hidp_process_transmit(session);
363 schedule();
365 set_current_state(TASK_RUNNING);
366 remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
367 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
369 down_write(&hidp_session_sem);
371 hidp_del_timer(session);
373 if (intr_sk->sk_state != BT_CONNECTED) {
374 init_waitqueue_entry(&ctrl_wait, current);
375 add_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
376 while (timeo && ctrl_sk->sk_state != BT_CLOSED) {
377 set_current_state(TASK_INTERRUPTIBLE);
378 timeo = schedule_timeout(timeo);
380 set_current_state(TASK_RUNNING);
381 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
382 timeo = HZ;
385 fput(session->ctrl_sock->file);
387 init_waitqueue_entry(&intr_wait, current);
388 add_wait_queue(intr_sk->sk_sleep, &intr_wait);
389 while (timeo && intr_sk->sk_state != BT_CLOSED) {
390 set_current_state(TASK_INTERRUPTIBLE);
391 timeo = schedule_timeout(timeo);
393 set_current_state(TASK_RUNNING);
394 remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
396 fput(session->intr_sock->file);
398 __hidp_unlink_session(session);
400 if (session->input) {
401 input_unregister_device(session->input);
402 kfree(session->input);
405 up_write(&hidp_session_sem);
407 kfree(session);
408 return 0;
411 static inline void hidp_setup_input(struct hidp_session *session, struct hidp_connadd_req *req)
413 struct input_dev *input = session->input;
414 int i;
416 input->private = session;
418 input->id.bustype = BUS_BLUETOOTH;
419 input->id.vendor = req->vendor;
420 input->id.product = req->product;
421 input->id.version = req->version;
423 if (req->subclass & 0x40) {
424 set_bit(EV_KEY, input->evbit);
425 set_bit(EV_LED, input->evbit);
426 set_bit(EV_REP, input->evbit);
428 set_bit(LED_NUML, input->ledbit);
429 set_bit(LED_CAPSL, input->ledbit);
430 set_bit(LED_SCROLLL, input->ledbit);
431 set_bit(LED_COMPOSE, input->ledbit);
432 set_bit(LED_KANA, input->ledbit);
434 for (i = 0; i < sizeof(hidp_keycode); i++)
435 set_bit(hidp_keycode[i], input->keybit);
436 clear_bit(0, input->keybit);
439 if (req->subclass & 0x80) {
440 input->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
441 input->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
442 input->relbit[0] = BIT(REL_X) | BIT(REL_Y);
443 input->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
444 input->relbit[0] |= BIT(REL_WHEEL);
447 input->event = hidp_input_event;
449 input_register_device(input);
452 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
454 struct hidp_session *session, *s;
455 int err;
457 BT_DBG("");
459 if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) ||
460 bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst))
461 return -ENOTUNIQ;
463 session = kmalloc(sizeof(struct hidp_session), GFP_KERNEL);
464 if (!session)
465 return -ENOMEM;
466 memset(session, 0, sizeof(struct hidp_session));
468 session->input = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
469 if (!session->input) {
470 kfree(session);
471 return -ENOMEM;
473 memset(session->input, 0, sizeof(struct input_dev));
475 down_write(&hidp_session_sem);
477 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
478 if (s && s->state == BT_CONNECTED) {
479 err = -EEXIST;
480 goto failed;
483 bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
485 session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->omtu, l2cap_pi(ctrl_sock->sk)->imtu);
486 session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->omtu, l2cap_pi(intr_sock->sk)->imtu);
488 BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
490 session->ctrl_sock = ctrl_sock;
491 session->intr_sock = intr_sock;
492 session->state = BT_CONNECTED;
494 init_timer(&session->timer);
496 session->timer.function = hidp_idle_timeout;
497 session->timer.data = (unsigned long) session;
499 skb_queue_head_init(&session->ctrl_transmit);
500 skb_queue_head_init(&session->intr_transmit);
502 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
503 session->idle_to = req->idle_to;
505 if (session->input)
506 hidp_setup_input(session, req);
508 __hidp_link_session(session);
510 hidp_set_timer(session);
512 err = kernel_thread(hidp_session, session, CLONE_KERNEL);
513 if (err < 0)
514 goto unlink;
516 if (session->input) {
517 hidp_send_message(session, 0x70);
518 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
520 session->leds = 0xff;
521 hidp_input_event(session->input, EV_LED, 0, 0);
524 up_write(&hidp_session_sem);
525 return 0;
527 unlink:
528 hidp_del_timer(session);
530 __hidp_unlink_session(session);
532 if (session->input)
533 input_unregister_device(session->input);
535 failed:
536 up_write(&hidp_session_sem);
538 if (session->input)
539 kfree(session->input);
541 kfree(session);
542 return err;
545 int hidp_del_connection(struct hidp_conndel_req *req)
547 struct hidp_session *session;
548 int err = 0;
550 BT_DBG("");
552 down_read(&hidp_session_sem);
554 session = __hidp_get_session(&req->bdaddr);
555 if (session) {
556 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
557 hidp_send_message(session, 0x15);
558 } else {
559 /* Flush the transmit queues */
560 skb_queue_purge(&session->ctrl_transmit);
561 skb_queue_purge(&session->intr_transmit);
563 /* Kill session thread */
564 atomic_inc(&session->terminate);
565 hidp_schedule(session);
567 } else
568 err = -ENOENT;
570 up_read(&hidp_session_sem);
571 return err;
574 int hidp_get_connlist(struct hidp_connlist_req *req)
576 struct list_head *p;
577 int err = 0, n = 0;
579 BT_DBG("");
581 down_read(&hidp_session_sem);
583 list_for_each(p, &hidp_session_list) {
584 struct hidp_session *session;
585 struct hidp_conninfo ci;
587 session = list_entry(p, struct hidp_session, list);
589 __hidp_copy_session(session, &ci);
591 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
592 err = -EFAULT;
593 break;
596 if (++n >= req->cnum)
597 break;
599 req->ci++;
601 req->cnum = n;
603 up_read(&hidp_session_sem);
604 return err;
607 int hidp_get_conninfo(struct hidp_conninfo *ci)
609 struct hidp_session *session;
610 int err = 0;
612 down_read(&hidp_session_sem);
614 session = __hidp_get_session(&ci->bdaddr);
615 if (session)
616 __hidp_copy_session(session, ci);
617 else
618 err = -ENOENT;
620 up_read(&hidp_session_sem);
621 return err;
624 static int __init hidp_init(void)
626 l2cap_load();
628 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
630 return hidp_init_sockets();
633 static void __exit hidp_exit(void)
635 hidp_cleanup_sockets();
638 module_init(hidp_init);
639 module_exit(hidp_exit);
641 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
642 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
643 MODULE_VERSION(VERSION);
644 MODULE_LICENSE("GPL");
645 MODULE_ALIAS("bt-proto-6");