This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / bluetooth / hidp / core.c
1 /* 
2    HIDP implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
4
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;
8
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.
17
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.
21 */
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25
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>
40
41 #include <linux/input.h>
42
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/l2cap.h>
45
46 #include "hidp.h"
47
48 #ifndef CONFIG_BT_HIDP_DEBUG
49 #undef  BT_DBG
50 #define BT_DBG(D...)
51 #endif
52
53 #define VERSION "1.0"
54
55 static DECLARE_RWSEM(hidp_session_sem);
56 static LIST_HEAD(hidp_session_list);
57
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
75 };
76
77 static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)
78 {
79         struct hidp_session *session;
80         struct list_head *p;
81
82         BT_DBG("");
83
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;
88         }
89         return NULL;
90 }
91
92 static void __hidp_link_session(struct hidp_session *session)
93 {
94         __module_get(THIS_MODULE);
95         list_add(&session->list, &hidp_session_list);
96 }
97
98 static void __hidp_unlink_session(struct hidp_session *session)
99 {
100         list_del(&session->list);
101         module_put(THIS_MODULE);
102 }
103
104 static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
105 {
106         bacpy(&ci->bdaddr, &session->bdaddr);
107
108         ci->flags = session->flags;
109         ci->state = session->state;
110
111         ci->vendor  = 0x0000;
112         ci->product = 0x0000;
113         ci->version = 0x0000;
114         memset(ci->name, 0, 128);
115
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);
124         }
125 }
126
127 static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
128 {
129         struct hidp_session *session = dev->private;
130         struct sk_buff *skb;
131         unsigned char newleds;
132
133         BT_DBG("session %p hid %p data %p size %d", session, device, data, size);
134
135         if (type != EV_LED)
136                 return -1;
137
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));
143
144         if (session->leds == newleds)
145                 return 0;
146
147         session->leds = newleds;
148
149         if (!(skb = alloc_skb(3, GFP_ATOMIC))) {
150                 BT_ERR("Can't allocate memory for new frame");
151                 return -ENOMEM;
152         }
153
154         *skb_put(skb, 1) = 0xa2;
155         *skb_put(skb, 1) = 0x01;
156         *skb_put(skb, 1) = newleds;
157
158         skb_queue_tail(&session->intr_transmit, skb);
159
160         hidp_schedule(session);
161
162         return 0;
163 }
164
165 static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
166 {
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;
172
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);
177
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]);
184                         }
185
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]);
191                         }
192                 }
193
194                 memcpy(keys, udata, 8);
195                 break;
196
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);
203
204                 input_report_rel(dev, REL_X, sdata[1]);
205                 input_report_rel(dev, REL_Y, sdata[2]);
206
207                 if (size > 3)
208                         input_report_rel(dev, REL_WHEEL, sdata[3]);
209                 break;
210         }
211
212         input_sync(dev);
213 }
214
215 static void hidp_idle_timeout(unsigned long arg)
216 {
217         struct hidp_session *session = (struct hidp_session *) arg;
218
219         atomic_inc(&session->terminate);
220         hidp_schedule(session);
221 }
222
223 static inline void hidp_set_timer(struct hidp_session *session)
224 {
225         if (session->idle_to > 0)
226                 mod_timer(&session->timer, jiffies + HZ * session->idle_to);
227 }
228
229 static inline void hidp_del_timer(struct hidp_session *session)
230 {
231         if (session->idle_to > 0)
232                 del_timer(&session->timer);
233 }
234
235 static inline void hidp_send_message(struct hidp_session *session, unsigned char hdr)
236 {
237         struct sk_buff *skb;
238
239         BT_DBG("session %p", session);
240
241         if (!(skb = alloc_skb(1, GFP_ATOMIC))) {
242                 BT_ERR("Can't allocate memory for message");
243                 return;
244         }
245
246         *skb_put(skb, 1) = hdr;
247
248         skb_queue_tail(&session->ctrl_transmit, skb);
249
250         hidp_schedule(session);
251 }
252
253 static inline int hidp_recv_frame(struct hidp_session *session, struct sk_buff *skb)
254 {
255         __u8 hdr;
256
257         BT_DBG("session %p skb %p len %d", session, skb, skb->len);
258
259         hdr = skb->data[0];
260         skb_pull(skb, 1);
261
262         if (hdr == 0xa1) {
263                 hidp_set_timer(session);
264
265                 if (session->input)
266                         hidp_input_report(session, skb);
267         } else {
268                 BT_DBG("Unsupported protocol header 0x%02x", hdr);
269         }
270
271         kfree_skb(skb);
272         return 0;
273 }
274
275 static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
276 {
277         struct iovec iv = { data, len };
278         struct msghdr msg;
279
280         BT_DBG("sock %p data %p len %d", sock, data, len);
281
282         if (!len)
283                 return 0;
284
285         memset(&msg, 0, sizeof(msg));
286         msg.msg_iovlen = 1;
287         msg.msg_iov = &iv;
288
289         return sock_sendmsg(sock, &msg, len);
290 }
291
292 static int hidp_process_transmit(struct hidp_session *session)
293 {
294         struct sk_buff *skb;
295
296         BT_DBG("session %p", session);
297
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);
301                         break;
302                 }
303
304                 hidp_set_timer(session);
305                 kfree_skb(skb);
306         }
307
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);
311                         break;
312                 }
313
314                 hidp_set_timer(session);
315                 kfree_skb(skb);
316         }
317
318         return skb_queue_len(&session->ctrl_transmit) +
319                                 skb_queue_len(&session->intr_transmit);
320 }
321
322 static int hidp_session(void *arg)
323 {
324         struct hidp_session *session = arg;
325         struct sock *ctrl_sk = session->ctrl_sock->sk;
326         struct sock *intr_sk = session->intr_sock->sk;
327         struct sk_buff *skb;
328         int vendor = 0x0000, product = 0x0000;
329         wait_queue_t ctrl_wait, intr_wait;
330         unsigned long timeo = HZ;
331
332         BT_DBG("session %p", session);
333
334         if (session->input) {
335                 vendor  = session->input->id.vendor;
336                 product = session->input->id.product;
337         }
338
339         daemonize("khidpd_%04x%04x", vendor, product);
340         set_user_nice(current, -15);
341         current->flags |= PF_NOFREEZE;
342
343         set_fs(KERNEL_DS);
344
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);
351
352                 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
353                         break;
354
355                 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
356                         skb_orphan(skb);
357                         hidp_recv_frame(session, skb);
358                 }
359
360                 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
361                         skb_orphan(skb);
362                         hidp_recv_frame(session, skb);
363                 }
364
365                 hidp_process_transmit(session);
366
367                 schedule();
368         }
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);
372
373         down_write(&hidp_session_sem);
374
375         hidp_del_timer(session);
376
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);
383                 }
384                 set_current_state(TASK_RUNNING);
385                 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
386                 timeo = HZ;
387         }
388
389         fput(session->ctrl_sock->file);
390
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);
396         }
397         set_current_state(TASK_RUNNING);
398         remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
399
400         fput(session->intr_sock->file);
401
402         __hidp_unlink_session(session);
403
404         if (session->input) {
405                 input_unregister_device(session->input);
406                 kfree(session->input);
407         }
408
409         up_write(&hidp_session_sem);
410
411         kfree(session);
412         return 0;
413 }
414
415 static inline void hidp_setup_input(struct hidp_session *session, struct hidp_connadd_req *req)
416 {
417         struct input_dev *input = session->input;
418         int i;
419
420         input->private = session;
421
422         input->id.bustype = BUS_BLUETOOTH;
423         input->id.vendor  = req->vendor;
424         input->id.product = req->product;
425         input->id.version = req->version;
426
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);
431
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);
437
438                 for (i = 0; i < sizeof(hidp_keycode); i++)
439                         set_bit(hidp_keycode[i], input->keybit);
440                 clear_bit(0, input->keybit);
441         }
442
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);
449         }
450
451         input->event = hidp_input_event;
452
453         input_register_device(input);
454 }
455
456 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
457 {
458         struct hidp_session *session, *s;
459         int err;
460
461         BT_DBG("");
462
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))
465                 return -ENOTUNIQ;
466
467         session = kmalloc(sizeof(struct hidp_session), GFP_KERNEL);
468         if (!session) 
469                 return -ENOMEM;
470         memset(session, 0, sizeof(struct hidp_session));
471
472         session->input = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
473         if (!session->input) {
474                 kfree(session);
475                 return -ENOMEM;
476         }
477         memset(session->input, 0, sizeof(struct input_dev));
478
479         down_write(&hidp_session_sem);
480
481         s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
482         if (s && s->state == BT_CONNECTED) {
483                 err = -EEXIST;
484                 goto failed;
485         }
486
487         bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
488
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);
491
492         BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
493
494         session->ctrl_sock = ctrl_sock;
495         session->intr_sock = intr_sock;
496         session->state     = BT_CONNECTED;
497
498         init_timer(&session->timer);
499
500         session->timer.function = hidp_idle_timeout;
501         session->timer.data     = (unsigned long) session;
502
503         skb_queue_head_init(&session->ctrl_transmit);
504         skb_queue_head_init(&session->intr_transmit);
505
506         session->flags   = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
507         session->idle_to = req->idle_to;
508
509         if (session->input)
510                 hidp_setup_input(session, req);
511
512         __hidp_link_session(session);
513
514         hidp_set_timer(session);
515
516         err = kernel_thread(hidp_session, session, CLONE_KERNEL);
517         if (err < 0)
518                 goto unlink;
519
520         if (session->input) {
521                 hidp_send_message(session, 0x70);
522                 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
523
524                 session->leds = 0xff;
525                 hidp_input_event(session->input, EV_LED, 0, 0);
526         }
527
528         up_write(&hidp_session_sem);
529         return 0;
530
531 unlink:
532         hidp_del_timer(session);
533
534         __hidp_unlink_session(session);
535
536         if (session->input)
537                 input_unregister_device(session->input);
538
539 failed:
540         up_write(&hidp_session_sem);
541
542         if (session->input)
543                 kfree(session->input);
544
545         kfree(session);
546         return err;
547 }
548
549 int hidp_del_connection(struct hidp_conndel_req *req)
550 {
551         struct hidp_session *session;
552         int err = 0;
553
554         BT_DBG("");
555
556         down_read(&hidp_session_sem);
557
558         session = __hidp_get_session(&req->bdaddr);
559         if (session) {
560                 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
561                         hidp_send_message(session, 0x15);
562                 } else {
563                         /* Flush the transmit queues */
564                         skb_queue_purge(&session->ctrl_transmit);
565                         skb_queue_purge(&session->intr_transmit);
566
567                         /* Kill session thread */
568                         atomic_inc(&session->terminate);
569                         hidp_schedule(session);
570                 }
571         } else
572                 err = -ENOENT;
573
574         up_read(&hidp_session_sem);
575         return err;
576 }
577
578 int hidp_get_connlist(struct hidp_connlist_req *req)
579 {
580         struct list_head *p;
581         int err = 0, n = 0;
582
583         BT_DBG("");
584
585         down_read(&hidp_session_sem);
586
587         list_for_each(p, &hidp_session_list) {
588                 struct hidp_session *session;
589                 struct hidp_conninfo ci;
590
591                 session = list_entry(p, struct hidp_session, list);
592
593                 __hidp_copy_session(session, &ci);
594
595                 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
596                         err = -EFAULT;
597                         break;
598                 }
599
600                 if (++n >= req->cnum)
601                         break;
602
603                 req->ci++;
604         }
605         req->cnum = n;
606
607         up_read(&hidp_session_sem);
608         return err;
609 }
610
611 int hidp_get_conninfo(struct hidp_conninfo *ci)
612 {
613         struct hidp_session *session;
614         int err = 0;
615
616         down_read(&hidp_session_sem);
617
618         session = __hidp_get_session(&ci->bdaddr);
619         if (session)
620                 __hidp_copy_session(session, ci);
621         else
622                 err = -ENOENT;
623
624         up_read(&hidp_session_sem);
625         return err;
626 }
627
628 static int __init hidp_init(void)
629 {
630         l2cap_load();
631
632         BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
633
634         return hidp_init_sockets();
635 }
636
637 static void __exit hidp_exit(void)
638 {
639         hidp_cleanup_sockets();
640 }
641
642 module_init(hidp_init);
643 module_exit(hidp_exit);
644
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");