VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 kvec 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
287         return kernel_sendmsg(sock, &msg, &iv, 1, len);
288 }
289
290 static int hidp_process_transmit(struct hidp_session *session)
291 {
292         struct sk_buff *skb;
293
294         BT_DBG("session %p", session);
295
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;
300                 }
301
302                 hidp_set_timer(session);
303                 kfree_skb(skb);
304         }
305
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;
310                 }
311
312                 hidp_set_timer(session);
313                 kfree_skb(skb);
314         }
315
316         return skb_queue_len(&session->ctrl_transmit) +
317                                 skb_queue_len(&session->intr_transmit);
318 }
319
320 static int hidp_session(void *arg)
321 {
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;
329
330         BT_DBG("session %p", session);
331
332         if (session->input) {
333                 vendor  = session->input->id.vendor;
334                 product = session->input->id.product;
335         }
336
337         daemonize("khidpd_%04x%04x", vendor, product);
338         set_user_nice(current, -15);
339         current->flags |= PF_NOFREEZE;
340
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);
347
348                 if (ctrl_sk->sk_state != BT_CONNECTED || intr_sk->sk_state != BT_CONNECTED)
349                         break;
350
351                 while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
352                         skb_orphan(skb);
353                         hidp_recv_frame(session, skb);
354                 }
355
356                 while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
357                         skb_orphan(skb);
358                         hidp_recv_frame(session, skb);
359                 }
360
361                 hidp_process_transmit(session);
362
363                 schedule();
364         }
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);
368
369         down_write(&hidp_session_sem);
370
371         hidp_del_timer(session);
372
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);
379                 }
380                 set_current_state(TASK_RUNNING);
381                 remove_wait_queue(ctrl_sk->sk_sleep, &ctrl_wait);
382                 timeo = HZ;
383         }
384
385         fput(session->ctrl_sock->file);
386
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);
392         }
393         set_current_state(TASK_RUNNING);
394         remove_wait_queue(intr_sk->sk_sleep, &intr_wait);
395
396         fput(session->intr_sock->file);
397
398         __hidp_unlink_session(session);
399
400         if (session->input) {
401                 input_unregister_device(session->input);
402                 kfree(session->input);
403         }
404
405         up_write(&hidp_session_sem);
406
407         kfree(session);
408         return 0;
409 }
410
411 static inline void hidp_setup_input(struct hidp_session *session, struct hidp_connadd_req *req)
412 {
413         struct input_dev *input = session->input;
414         int i;
415
416         input->private = session;
417
418         input->id.bustype = BUS_BLUETOOTH;
419         input->id.vendor  = req->vendor;
420         input->id.product = req->product;
421         input->id.version = req->version;
422
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);
427
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);
433
434                 for (i = 0; i < sizeof(hidp_keycode); i++)
435                         set_bit(hidp_keycode[i], input->keybit);
436                 clear_bit(0, input->keybit);
437         }
438
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);
445         }
446
447         input->event = hidp_input_event;
448
449         input_register_device(input);
450 }
451
452 int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
453 {
454         struct hidp_session *session, *s;
455         int err;
456
457         BT_DBG("");
458
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;
462
463         session = kmalloc(sizeof(struct hidp_session), GFP_KERNEL);
464         if (!session) 
465                 return -ENOMEM;
466         memset(session, 0, sizeof(struct hidp_session));
467
468         session->input = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
469         if (!session->input) {
470                 kfree(session);
471                 return -ENOMEM;
472         }
473         memset(session->input, 0, sizeof(struct input_dev));
474
475         down_write(&hidp_session_sem);
476
477         s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
478         if (s && s->state == BT_CONNECTED) {
479                 err = -EEXIST;
480                 goto failed;
481         }
482
483         bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
484
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);
487
488         BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
489
490         session->ctrl_sock = ctrl_sock;
491         session->intr_sock = intr_sock;
492         session->state     = BT_CONNECTED;
493
494         init_timer(&session->timer);
495
496         session->timer.function = hidp_idle_timeout;
497         session->timer.data     = (unsigned long) session;
498
499         skb_queue_head_init(&session->ctrl_transmit);
500         skb_queue_head_init(&session->intr_transmit);
501
502         session->flags   = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
503         session->idle_to = req->idle_to;
504
505         if (session->input)
506                 hidp_setup_input(session, req);
507
508         __hidp_link_session(session);
509
510         hidp_set_timer(session);
511
512         err = kernel_thread(hidp_session, session, CLONE_KERNEL);
513         if (err < 0)
514                 goto unlink;
515
516         if (session->input) {
517                 hidp_send_message(session, 0x70);
518                 session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
519
520                 session->leds = 0xff;
521                 hidp_input_event(session->input, EV_LED, 0, 0);
522         }
523
524         up_write(&hidp_session_sem);
525         return 0;
526
527 unlink:
528         hidp_del_timer(session);
529
530         __hidp_unlink_session(session);
531
532         if (session->input)
533                 input_unregister_device(session->input);
534
535 failed:
536         up_write(&hidp_session_sem);
537
538         if (session->input)
539                 kfree(session->input);
540
541         kfree(session);
542         return err;
543 }
544
545 int hidp_del_connection(struct hidp_conndel_req *req)
546 {
547         struct hidp_session *session;
548         int err = 0;
549
550         BT_DBG("");
551
552         down_read(&hidp_session_sem);
553
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);
562
563                         /* Kill session thread */
564                         atomic_inc(&session->terminate);
565                         hidp_schedule(session);
566                 }
567         } else
568                 err = -ENOENT;
569
570         up_read(&hidp_session_sem);
571         return err;
572 }
573
574 int hidp_get_connlist(struct hidp_connlist_req *req)
575 {
576         struct list_head *p;
577         int err = 0, n = 0;
578
579         BT_DBG("");
580
581         down_read(&hidp_session_sem);
582
583         list_for_each(p, &hidp_session_list) {
584                 struct hidp_session *session;
585                 struct hidp_conninfo ci;
586
587                 session = list_entry(p, struct hidp_session, list);
588
589                 __hidp_copy_session(session, &ci);
590
591                 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
592                         err = -EFAULT;
593                         break;
594                 }
595
596                 if (++n >= req->cnum)
597                         break;
598
599                 req->ci++;
600         }
601         req->cnum = n;
602
603         up_read(&hidp_session_sem);
604         return err;
605 }
606
607 int hidp_get_conninfo(struct hidp_conninfo *ci)
608 {
609         struct hidp_session *session;
610         int err = 0;
611
612         down_read(&hidp_session_sem);
613
614         session = __hidp_get_session(&ci->bdaddr);
615         if (session)
616                 __hidp_copy_session(session, ci);
617         else
618                 err = -ENOENT;
619
620         up_read(&hidp_session_sem);
621         return err;
622 }
623
624 static int __init hidp_init(void)
625 {
626         l2cap_load();
627
628         BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
629
630         return hidp_init_sockets();
631 }
632
633 static void __exit hidp_exit(void)
634 {
635         hidp_cleanup_sockets();
636 }
637
638 module_init(hidp_init);
639 module_exit(hidp_exit);
640
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");