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>
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>
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 */
79 HIDP_QUIRK_USE_INPUT_SESSION
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. */
94 .quirks
= 1 << HIDP_QUIRK_USE_INPUT_SESSION
98 static int quirk_test_bit(__u16 vendor
, __u16 product
, int quirk
)
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
);
112 static struct hidp_session
*__hidp_get_session(bdaddr_t
*bdaddr
)
114 struct hidp_session
*session
;
119 list_for_each(p
, &hidp_session_list
) {
120 session
= list_entry(p
, struct hidp_session
, list
);
121 if (!bacmp(bdaddr
, &session
->bdaddr
))
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
;
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);
162 strncpy(ci
->name
, "HID Boot Device", 128);
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
;
179 BT_DBG("session %p type %d code %d value %d", session
, type
, code
, value
);
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
)
193 session
->leds
= newleds
;
195 if (!(skb
= alloc_skb(3, GFP_ATOMIC
))) {
196 BT_ERR("Can't allocate memory for new frame");
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
);
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))
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);
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);
256 BT_ERR("Unknown key (scancode %#x) pressed.", udata
[i
]);
260 memcpy(keys
, udata
, 8);
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]);
274 input_report_rel(dev
, REL_WHEEL
, sdata
[3]);
281 static int hidp_queue_report(struct hidp_session
*session
,
282 unsigned char *data
, int size
)
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");
293 *skb_put(skb
, 1) = 0xa2;
295 memcpy(skb_put(skb
, size
), data
, size
);
297 skb_queue_tail(&session
->intr_transmit
, skb
);
299 hidp_schedule(session
);
304 static int hidp_send_report(struct hidp_session
*session
, struct hid_report
*report
)
306 unsigned char buf
[32];
309 rsize
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
310 if (rsize
> sizeof(buf
))
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
)
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");
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
);
359 static inline int hidp_send_ctrl_message(struct hidp_session
*session
,
360 unsigned char hdr
, unsigned char *data
, int size
)
364 err
= __hidp_send_ctrl_message(session
, hdr
, data
, size
);
366 hidp_schedule(session
);
371 static void hidp_process_handshake(struct hidp_session
*session
,
374 BT_DBG("session %p param 0x%02x", session
, param
);
377 case HIDP_HSHK_SUCCESSFUL
:
378 /* FIXME: Call into SET_ GET_ handlers here */
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 */
388 case HIDP_HSHK_ERR_UNKNOWN
:
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);
399 __hidp_send_ctrl_message(session
,
400 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
405 static void hidp_process_hid_control(struct hidp_session
*session
,
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
,
424 BT_DBG("session %p skb %p len %d param 0x%02x", session
, skb
, skb
->len
, param
);
427 case HIDP_DATA_RTYPE_INPUT
:
428 hidp_set_timer(session
);
431 hidp_input_report(session
, skb
);
434 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 0);
438 case HIDP_DATA_RTYPE_OTHER
:
439 case HIDP_DATA_RTYPE_OUPUT
:
440 case HIDP_DATA_RTYPE_FEATURE
:
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
,
452 unsigned char hdr
, type
, param
;
454 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
459 type
= hdr
& HIDP_HEADER_TRANS_MASK
;
460 param
= hdr
& HIDP_HEADER_PARAM_MASK
;
463 case HIDP_TRANS_HANDSHAKE
:
464 hidp_process_handshake(session
, param
);
467 case HIDP_TRANS_HID_CONTROL
:
468 hidp_process_hid_control(session
, param
);
471 case HIDP_TRANS_DATA
:
472 hidp_process_data(session
, skb
, param
);
476 __hidp_send_ctrl_message(session
,
477 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
, NULL
, 0);
484 static void hidp_recv_intr_frame(struct hidp_session
*session
,
489 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
494 if (hdr
== (HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_INPUT
)) {
495 hidp_set_timer(session
);
498 hidp_input_report(session
, skb
);
501 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 1);
502 BT_DBG("report len %d", skb
->len
);
505 BT_DBG("Unsupported protocol header 0x%02x", hdr
);
511 static int hidp_send_frame(struct socket
*sock
, unsigned char *data
, int len
)
513 struct kvec iv
= { data
, len
};
516 BT_DBG("sock %p data %p len %d", sock
, data
, len
);
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
)
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
);
538 hidp_set_timer(session
);
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
);
548 hidp_set_timer(session
);
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
;
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
;
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
)
587 while ((skb
= skb_dequeue(&ctrl_sk
->sk_receive_queue
))) {
589 hidp_recv_ctrl_frame(session
, skb
);
592 while ((skb
= skb_dequeue(&intr_sk
->sk_receive_queue
))) {
594 hidp_recv_intr_frame(session
, skb
);
597 hidp_process_transmit(session
);
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
;
615 hid_destroy_device(session
->hid
);
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
);
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
);
651 session
->conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
653 device
= &session
->conn
->dev
;
660 static int hidp_setup_input(struct hidp_session
*session
,
661 struct hidp_connadd_req
*req
)
663 struct input_dev
*input
;
666 input
= input_allocate_device();
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
) |
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
);
713 hci_conn_put_device(session
->conn
);
720 static int hidp_open(struct hid_device
*hid
)
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
;
736 buf
= kmalloc(req
->rd_size
, GFP_KERNEL
);
740 if (copy_from_user(buf
, req
->rd_data
, req
->rd_size
)) {
745 ret
= hid_parse_report(session
->hid
, buf
, req
->rd_size
);
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
].
764 hidp_send_report(session
, report
);
766 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].
768 hidp_send_report(session
, report
);
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
);
783 static struct hid_ll_driver hidp_hid_driver
= {
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
;
799 hid
= hid_allocate_device();
801 return PTR_ERR(session
->hid
);
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
);
830 hid_destroy_device(hid
);
836 int hidp_add_connection(struct hidp_connadd_req
*req
, struct socket
*ctrl_sock
, struct socket
*intr_sock
)
838 struct hidp_session
*session
, *s
;
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
))
847 session
= kzalloc(sizeof(struct hidp_session
), GFP_KERNEL
);
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
) {
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
)
889 err
= hidp_setup_input(session
, req
);
894 __hidp_link_session(session
);
896 hidp_set_timer(session
);
898 err
= kernel_thread(hidp_session
, session
, CLONE_KERNEL
);
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
);
915 hidp_del_timer(session
);
917 __hidp_unlink_session(session
);
919 if (session
->input
) {
920 input_unregister_device(session
->input
);
921 session
->input
= NULL
;
925 hid_destroy_device(session
->hid
);
930 skb_queue_purge(&session
->ctrl_transmit
);
931 skb_queue_purge(&session
->intr_transmit
);
934 up_write(&hidp_session_sem
);
936 input_free_device(session
->input
);
941 int hidp_del_connection(struct hidp_conndel_req
*req
)
943 struct hidp_session
*session
;
948 down_read(&hidp_session_sem
);
950 session
= __hidp_get_session(&req
->bdaddr
);
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);
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
);
971 up_read(&hidp_session_sem
);
975 int hidp_get_connlist(struct hidp_connlist_req
*req
)
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
))) {
997 if (++n
>= req
->cnum
)
1004 up_read(&hidp_session_sem
);
1008 int hidp_get_conninfo(struct hidp_conninfo
*ci
)
1010 struct hidp_session
*session
;
1013 down_read(&hidp_session_sem
);
1015 session
= __hidp_get_session(&ci
->bdaddr
);
1017 __hidp_copy_session(session
, ci
);
1021 up_read(&hidp_session_sem
);
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)
1041 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION
);
1043 ret
= hid_register_driver(&hidp_driver
);
1047 ret
= hidp_init_sockets();
1053 hid_unregister_driver(&hidp_driver
);
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");