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>
39 #include <linux/mutex.h>
42 #include <linux/input.h>
43 #include <linux/hid.h>
44 #include <linux/hidraw.h>
46 #include <net/bluetooth/bluetooth.h>
47 #include <net/bluetooth/hci_core.h>
48 #include <net/bluetooth/l2cap.h>
54 static DECLARE_RWSEM(hidp_session_sem
);
55 static LIST_HEAD(hidp_session_list
);
57 static unsigned char hidp_keycode
[256] = {
58 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
59 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
60 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
61 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
62 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
63 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
64 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
65 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
66 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
67 122,123, 90, 91, 85, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
73 150,158,159,128,136,177,178,176,142,152,173,140
76 static unsigned char hidp_mkeyspat
[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
78 static struct hidp_session
*__hidp_get_session(bdaddr_t
*bdaddr
)
80 struct hidp_session
*session
;
85 list_for_each(p
, &hidp_session_list
) {
86 session
= list_entry(p
, struct hidp_session
, list
);
87 if (!bacmp(bdaddr
, &session
->bdaddr
))
93 static void __hidp_link_session(struct hidp_session
*session
)
95 __module_get(THIS_MODULE
);
96 list_add(&session
->list
, &hidp_session_list
);
98 hci_conn_hold_device(session
->conn
);
101 static void __hidp_unlink_session(struct hidp_session
*session
)
103 hci_conn_put_device(session
->conn
);
105 list_del(&session
->list
);
106 module_put(THIS_MODULE
);
109 static void __hidp_copy_session(struct hidp_session
*session
, struct hidp_conninfo
*ci
)
111 memset(ci
, 0, sizeof(*ci
));
112 bacpy(&ci
->bdaddr
, &session
->bdaddr
);
114 ci
->flags
= session
->flags
;
115 ci
->state
= session
->state
;
118 ci
->product
= 0x0000;
119 ci
->version
= 0x0000;
121 if (session
->input
) {
122 ci
->vendor
= session
->input
->id
.vendor
;
123 ci
->product
= session
->input
->id
.product
;
124 ci
->version
= session
->input
->id
.version
;
125 if (session
->input
->name
)
126 strncpy(ci
->name
, session
->input
->name
, 128);
128 strncpy(ci
->name
, "HID Boot Device", 128);
132 ci
->vendor
= session
->hid
->vendor
;
133 ci
->product
= session
->hid
->product
;
134 ci
->version
= session
->hid
->version
;
135 strncpy(ci
->name
, session
->hid
->name
, 128);
139 static int hidp_queue_event(struct hidp_session
*session
, struct input_dev
*dev
,
140 unsigned int type
, unsigned int code
, int value
)
142 unsigned char newleds
;
145 BT_DBG("session %p type %d code %d value %d", session
, type
, code
, value
);
150 newleds
= (!!test_bit(LED_KANA
, dev
->led
) << 3) |
151 (!!test_bit(LED_COMPOSE
, dev
->led
) << 3) |
152 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) |
153 (!!test_bit(LED_CAPSL
, dev
->led
) << 1) |
154 (!!test_bit(LED_NUML
, dev
->led
));
156 if (session
->leds
== newleds
)
159 session
->leds
= newleds
;
161 skb
= alloc_skb(3, GFP_ATOMIC
);
163 BT_ERR("Can't allocate memory for new frame");
167 *skb_put(skb
, 1) = HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_OUPUT
;
168 *skb_put(skb
, 1) = 0x01;
169 *skb_put(skb
, 1) = newleds
;
171 skb_queue_tail(&session
->intr_transmit
, skb
);
173 hidp_schedule(session
);
178 static int hidp_hidinput_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
180 struct hid_device
*hid
= input_get_drvdata(dev
);
181 struct hidp_session
*session
= hid
->driver_data
;
183 return hidp_queue_event(session
, dev
, type
, code
, value
);
186 static int hidp_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
188 struct hidp_session
*session
= input_get_drvdata(dev
);
190 return hidp_queue_event(session
, dev
, type
, code
, value
);
193 static void hidp_input_report(struct hidp_session
*session
, struct sk_buff
*skb
)
195 struct input_dev
*dev
= session
->input
;
196 unsigned char *keys
= session
->keys
;
197 unsigned char *udata
= skb
->data
+ 1;
198 signed char *sdata
= skb
->data
+ 1;
199 int i
, size
= skb
->len
- 1;
201 switch (skb
->data
[0]) {
202 case 0x01: /* Keyboard report */
203 for (i
= 0; i
< 8; i
++)
204 input_report_key(dev
, hidp_keycode
[i
+ 224], (udata
[0] >> i
) & 1);
206 /* If all the key codes have been set to 0x01, it means
207 * too many keys were pressed at the same time. */
208 if (!memcmp(udata
+ 2, hidp_mkeyspat
, 6))
211 for (i
= 2; i
< 8; i
++) {
212 if (keys
[i
] > 3 && memscan(udata
+ 2, keys
[i
], 6) == udata
+ 8) {
213 if (hidp_keycode
[keys
[i
]])
214 input_report_key(dev
, hidp_keycode
[keys
[i
]], 0);
216 BT_ERR("Unknown key (scancode %#x) released.", keys
[i
]);
219 if (udata
[i
] > 3 && memscan(keys
+ 2, udata
[i
], 6) == keys
+ 8) {
220 if (hidp_keycode
[udata
[i
]])
221 input_report_key(dev
, hidp_keycode
[udata
[i
]], 1);
223 BT_ERR("Unknown key (scancode %#x) pressed.", udata
[i
]);
227 memcpy(keys
, udata
, 8);
230 case 0x02: /* Mouse report */
231 input_report_key(dev
, BTN_LEFT
, sdata
[0] & 0x01);
232 input_report_key(dev
, BTN_RIGHT
, sdata
[0] & 0x02);
233 input_report_key(dev
, BTN_MIDDLE
, sdata
[0] & 0x04);
234 input_report_key(dev
, BTN_SIDE
, sdata
[0] & 0x08);
235 input_report_key(dev
, BTN_EXTRA
, sdata
[0] & 0x10);
237 input_report_rel(dev
, REL_X
, sdata
[1]);
238 input_report_rel(dev
, REL_Y
, sdata
[2]);
241 input_report_rel(dev
, REL_WHEEL
, sdata
[3]);
248 static int __hidp_send_ctrl_message(struct hidp_session
*session
,
249 unsigned char hdr
, unsigned char *data
, int size
)
253 BT_DBG("session %p data %p size %d", session
, data
, size
);
255 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
257 BT_ERR("Can't allocate memory for new frame");
261 *skb_put(skb
, 1) = hdr
;
262 if (data
&& size
> 0)
263 memcpy(skb_put(skb
, size
), data
, size
);
265 skb_queue_tail(&session
->ctrl_transmit
, skb
);
270 static inline int hidp_send_ctrl_message(struct hidp_session
*session
,
271 unsigned char hdr
, unsigned char *data
, int size
)
275 err
= __hidp_send_ctrl_message(session
, hdr
, data
, size
);
277 hidp_schedule(session
);
282 static int hidp_queue_report(struct hidp_session
*session
,
283 unsigned char *data
, int size
)
287 BT_DBG("session %p hid %p data %p size %d", session
, session
->hid
, data
, size
);
289 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
291 BT_ERR("Can't allocate memory for new frame");
295 *skb_put(skb
, 1) = 0xa2;
297 memcpy(skb_put(skb
, size
), data
, size
);
299 skb_queue_tail(&session
->intr_transmit
, skb
);
301 hidp_schedule(session
);
306 static int hidp_send_report(struct hidp_session
*session
, struct hid_report
*report
)
308 unsigned char buf
[32];
311 rsize
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
312 if (rsize
> sizeof(buf
))
315 hid_output_report(report
, buf
);
317 return hidp_queue_report(session
, buf
, rsize
);
320 static int hidp_get_raw_report(struct hid_device
*hid
,
321 unsigned char report_number
,
322 unsigned char *data
, size_t count
,
323 unsigned char report_type
)
325 struct hidp_session
*session
= hid
->driver_data
;
328 int numbered_reports
= hid
->report_enum
[report_type
].numbered
;
330 switch (report_type
) {
331 case HID_FEATURE_REPORT
:
332 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
334 case HID_INPUT_REPORT
:
335 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_INPUT
;
337 case HID_OUTPUT_REPORT
:
338 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
344 if (mutex_lock_interruptible(&session
->report_mutex
))
347 /* Set up our wait, and send the report request to the device. */
348 session
->waiting_report_type
= report_type
& HIDP_DATA_RTYPE_MASK
;
349 session
->waiting_report_number
= numbered_reports
? report_number
: -1;
350 set_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
351 data
[0] = report_number
;
352 if (hidp_send_ctrl_message(hid
->driver_data
, report_type
, data
, 1))
355 /* Wait for the return of the report. The returned report
356 gets put in session->report_return. */
357 while (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
)) {
360 res
= wait_event_interruptible_timeout(session
->report_queue
,
361 !test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
),
373 skb
= session
->report_return
;
375 len
= skb
->len
< count
? skb
->len
: count
;
376 memcpy(data
, skb
->data
, len
);
379 session
->report_return
= NULL
;
381 /* Device returned a HANDSHAKE, indicating protocol error. */
385 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
386 mutex_unlock(&session
->report_mutex
);
391 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
392 mutex_unlock(&session
->report_mutex
);
395 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
396 mutex_unlock(&session
->report_mutex
);
400 static int hidp_output_raw_report(struct hid_device
*hid
, unsigned char *data
, size_t count
,
401 unsigned char report_type
)
403 struct hidp_session
*session
= hid
->driver_data
;
406 switch (report_type
) {
407 case HID_FEATURE_REPORT
:
408 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
410 case HID_OUTPUT_REPORT
:
411 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
417 if (mutex_lock_interruptible(&session
->report_mutex
))
420 /* Set up our wait, and send the report request to the device. */
421 set_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
422 if (hidp_send_ctrl_message(hid
->driver_data
, report_type
,
428 /* Wait for the ACK from the device. */
429 while (test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
)) {
432 res
= wait_event_interruptible_timeout(session
->report_queue
,
433 !test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
),
447 if (!session
->output_report_success
) {
455 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
456 mutex_unlock(&session
->report_mutex
);
460 static void hidp_idle_timeout(unsigned long arg
)
462 struct hidp_session
*session
= (struct hidp_session
*) arg
;
464 atomic_inc(&session
->terminate
);
465 hidp_schedule(session
);
468 static void hidp_set_timer(struct hidp_session
*session
)
470 if (session
->idle_to
> 0)
471 mod_timer(&session
->timer
, jiffies
+ HZ
* session
->idle_to
);
474 static inline void hidp_del_timer(struct hidp_session
*session
)
476 if (session
->idle_to
> 0)
477 del_timer(&session
->timer
);
480 static void hidp_process_handshake(struct hidp_session
*session
,
483 BT_DBG("session %p param 0x%02x", session
, param
);
484 session
->output_report_success
= 0; /* default condition */
487 case HIDP_HSHK_SUCCESSFUL
:
488 /* FIXME: Call into SET_ GET_ handlers here */
489 session
->output_report_success
= 1;
492 case HIDP_HSHK_NOT_READY
:
493 case HIDP_HSHK_ERR_INVALID_REPORT_ID
:
494 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
:
495 case HIDP_HSHK_ERR_INVALID_PARAMETER
:
496 if (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
)) {
497 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
498 wake_up_interruptible(&session
->report_queue
);
500 /* FIXME: Call into SET_ GET_ handlers here */
503 case HIDP_HSHK_ERR_UNKNOWN
:
506 case HIDP_HSHK_ERR_FATAL
:
507 /* Device requests a reboot, as this is the only way this error
508 * can be recovered. */
509 __hidp_send_ctrl_message(session
,
510 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_SOFT_RESET
, NULL
, 0);
514 __hidp_send_ctrl_message(session
,
515 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
519 /* Wake up the waiting thread. */
520 if (test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
)) {
521 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
522 wake_up_interruptible(&session
->report_queue
);
526 static void hidp_process_hid_control(struct hidp_session
*session
,
529 BT_DBG("session %p param 0x%02x", session
, param
);
531 if (param
== HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
) {
532 /* Flush the transmit queues */
533 skb_queue_purge(&session
->ctrl_transmit
);
534 skb_queue_purge(&session
->intr_transmit
);
536 /* Kill session thread */
537 atomic_inc(&session
->terminate
);
538 hidp_schedule(session
);
542 /* Returns true if the passed-in skb should be freed by the caller. */
543 static int hidp_process_data(struct hidp_session
*session
, struct sk_buff
*skb
,
546 int done_with_skb
= 1;
547 BT_DBG("session %p skb %p len %d param 0x%02x", session
, skb
, skb
->len
, param
);
550 case HIDP_DATA_RTYPE_INPUT
:
551 hidp_set_timer(session
);
554 hidp_input_report(session
, skb
);
557 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 0);
560 case HIDP_DATA_RTYPE_OTHER
:
561 case HIDP_DATA_RTYPE_OUPUT
:
562 case HIDP_DATA_RTYPE_FEATURE
:
566 __hidp_send_ctrl_message(session
,
567 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
570 if (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
) &&
571 param
== session
->waiting_report_type
) {
572 if (session
->waiting_report_number
< 0 ||
573 session
->waiting_report_number
== skb
->data
[0]) {
574 /* hidp_get_raw_report() is waiting on this report. */
575 session
->report_return
= skb
;
577 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
578 wake_up_interruptible(&session
->report_queue
);
582 return done_with_skb
;
585 static void hidp_recv_ctrl_frame(struct hidp_session
*session
,
588 unsigned char hdr
, type
, param
;
591 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
596 type
= hdr
& HIDP_HEADER_TRANS_MASK
;
597 param
= hdr
& HIDP_HEADER_PARAM_MASK
;
600 case HIDP_TRANS_HANDSHAKE
:
601 hidp_process_handshake(session
, param
);
604 case HIDP_TRANS_HID_CONTROL
:
605 hidp_process_hid_control(session
, param
);
608 case HIDP_TRANS_DATA
:
609 free_skb
= hidp_process_data(session
, skb
, param
);
613 __hidp_send_ctrl_message(session
,
614 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
, NULL
, 0);
622 static void hidp_recv_intr_frame(struct hidp_session
*session
,
627 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
632 if (hdr
== (HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_INPUT
)) {
633 hidp_set_timer(session
);
636 hidp_input_report(session
, skb
);
639 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 1);
640 BT_DBG("report len %d", skb
->len
);
643 BT_DBG("Unsupported protocol header 0x%02x", hdr
);
649 static int hidp_send_frame(struct socket
*sock
, unsigned char *data
, int len
)
651 struct kvec iv
= { data
, len
};
654 BT_DBG("sock %p data %p len %d", sock
, data
, len
);
659 memset(&msg
, 0, sizeof(msg
));
661 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
664 static void hidp_process_transmit(struct hidp_session
*session
)
668 BT_DBG("session %p", session
);
670 while ((skb
= skb_dequeue(&session
->ctrl_transmit
))) {
671 if (hidp_send_frame(session
->ctrl_sock
, skb
->data
, skb
->len
) < 0) {
672 skb_queue_head(&session
->ctrl_transmit
, skb
);
676 hidp_set_timer(session
);
680 while ((skb
= skb_dequeue(&session
->intr_transmit
))) {
681 if (hidp_send_frame(session
->intr_sock
, skb
->data
, skb
->len
) < 0) {
682 skb_queue_head(&session
->intr_transmit
, skb
);
686 hidp_set_timer(session
);
691 static int hidp_session(void *arg
)
693 struct hidp_session
*session
= arg
;
694 struct sock
*ctrl_sk
= session
->ctrl_sock
->sk
;
695 struct sock
*intr_sk
= session
->intr_sock
->sk
;
697 int vendor
= 0x0000, product
= 0x0000;
698 wait_queue_t ctrl_wait
, intr_wait
;
700 BT_DBG("session %p", session
);
702 if (session
->input
) {
703 vendor
= session
->input
->id
.vendor
;
704 product
= session
->input
->id
.product
;
708 vendor
= session
->hid
->vendor
;
709 product
= session
->hid
->product
;
712 daemonize("khidpd_%04x%04x", vendor
, product
);
713 set_user_nice(current
, -15);
715 init_waitqueue_entry(&ctrl_wait
, current
);
716 init_waitqueue_entry(&intr_wait
, current
);
717 add_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
718 add_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
719 session
->waiting_for_startup
= 0;
720 wake_up_interruptible(&session
->startup_queue
);
721 while (!atomic_read(&session
->terminate
)) {
722 set_current_state(TASK_INTERRUPTIBLE
);
724 if (ctrl_sk
->sk_state
!= BT_CONNECTED
|| intr_sk
->sk_state
!= BT_CONNECTED
)
727 while ((skb
= skb_dequeue(&ctrl_sk
->sk_receive_queue
))) {
729 hidp_recv_ctrl_frame(session
, skb
);
732 while ((skb
= skb_dequeue(&intr_sk
->sk_receive_queue
))) {
734 hidp_recv_intr_frame(session
, skb
);
737 hidp_process_transmit(session
);
741 set_current_state(TASK_RUNNING
);
742 remove_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
743 remove_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
745 down_write(&hidp_session_sem
);
747 hidp_del_timer(session
);
749 if (session
->input
) {
750 input_unregister_device(session
->input
);
751 session
->input
= NULL
;
755 hid_destroy_device(session
->hid
);
759 /* Wakeup user-space polling for socket errors */
760 session
->intr_sock
->sk
->sk_err
= EUNATCH
;
761 session
->ctrl_sock
->sk
->sk_err
= EUNATCH
;
763 hidp_schedule(session
);
765 fput(session
->intr_sock
->file
);
767 wait_event_timeout(*(sk_sleep(ctrl_sk
)),
768 (ctrl_sk
->sk_state
== BT_CLOSED
), msecs_to_jiffies(500));
770 fput(session
->ctrl_sock
->file
);
772 __hidp_unlink_session(session
);
774 up_write(&hidp_session_sem
);
780 static struct device
*hidp_get_device(struct hidp_session
*session
)
782 bdaddr_t
*src
= &bt_sk(session
->ctrl_sock
->sk
)->src
;
783 bdaddr_t
*dst
= &bt_sk(session
->ctrl_sock
->sk
)->dst
;
784 struct device
*device
= NULL
;
785 struct hci_dev
*hdev
;
787 hdev
= hci_get_route(dst
, src
);
791 session
->conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
793 device
= &session
->conn
->dev
;
800 static int hidp_setup_input(struct hidp_session
*session
,
801 struct hidp_connadd_req
*req
)
803 struct input_dev
*input
;
806 input
= input_allocate_device();
810 session
->input
= input
;
812 input_set_drvdata(input
, session
);
814 input
->name
= "Bluetooth HID Boot Protocol Device";
816 input
->id
.bustype
= BUS_BLUETOOTH
;
817 input
->id
.vendor
= req
->vendor
;
818 input
->id
.product
= req
->product
;
819 input
->id
.version
= req
->version
;
821 if (req
->subclass
& 0x40) {
822 set_bit(EV_KEY
, input
->evbit
);
823 set_bit(EV_LED
, input
->evbit
);
824 set_bit(EV_REP
, input
->evbit
);
826 set_bit(LED_NUML
, input
->ledbit
);
827 set_bit(LED_CAPSL
, input
->ledbit
);
828 set_bit(LED_SCROLLL
, input
->ledbit
);
829 set_bit(LED_COMPOSE
, input
->ledbit
);
830 set_bit(LED_KANA
, input
->ledbit
);
832 for (i
= 0; i
< sizeof(hidp_keycode
); i
++)
833 set_bit(hidp_keycode
[i
], input
->keybit
);
834 clear_bit(0, input
->keybit
);
837 if (req
->subclass
& 0x80) {
838 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
839 input
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
840 BIT_MASK(BTN_RIGHT
) | BIT_MASK(BTN_MIDDLE
);
841 input
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
842 input
->keybit
[BIT_WORD(BTN_MOUSE
)] |= BIT_MASK(BTN_SIDE
) |
844 input
->relbit
[0] |= BIT_MASK(REL_WHEEL
);
847 input
->dev
.parent
= hidp_get_device(session
);
849 input
->event
= hidp_input_event
;
851 err
= input_register_device(input
);
853 hci_conn_put_device(session
->conn
);
860 static int hidp_open(struct hid_device
*hid
)
865 static void hidp_close(struct hid_device
*hid
)
869 static int hidp_parse(struct hid_device
*hid
)
871 struct hidp_session
*session
= hid
->driver_data
;
873 return hid_parse_report(session
->hid
, session
->rd_data
,
877 static int hidp_start(struct hid_device
*hid
)
879 struct hidp_session
*session
= hid
->driver_data
;
880 struct hid_report
*report
;
882 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].
884 hidp_send_report(session
, report
);
886 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].
888 hidp_send_report(session
, report
);
893 static void hidp_stop(struct hid_device
*hid
)
895 struct hidp_session
*session
= hid
->driver_data
;
897 skb_queue_purge(&session
->ctrl_transmit
);
898 skb_queue_purge(&session
->intr_transmit
);
903 static struct hid_ll_driver hidp_hid_driver
= {
909 .hidinput_input_event
= hidp_hidinput_event
,
912 /* This function sets up the hid device. It does not add it
913 to the HID system. That is done in hidp_add_connection(). */
914 static int hidp_setup_hid(struct hidp_session
*session
,
915 struct hidp_connadd_req
*req
)
917 struct hid_device
*hid
;
920 session
->rd_data
= kzalloc(req
->rd_size
, GFP_KERNEL
);
921 if (!session
->rd_data
)
924 if (copy_from_user(session
->rd_data
, req
->rd_data
, req
->rd_size
)) {
928 session
->rd_size
= req
->rd_size
;
930 hid
= hid_allocate_device();
938 hid
->driver_data
= session
;
940 hid
->bus
= BUS_BLUETOOTH
;
941 hid
->vendor
= req
->vendor
;
942 hid
->product
= req
->product
;
943 hid
->version
= req
->version
;
944 hid
->country
= req
->country
;
946 strncpy(hid
->name
, req
->name
, 128);
947 strncpy(hid
->phys
, batostr(&bt_sk(session
->ctrl_sock
->sk
)->src
), 64);
948 strncpy(hid
->uniq
, batostr(&bt_sk(session
->ctrl_sock
->sk
)->dst
), 64);
950 hid
->dev
.parent
= hidp_get_device(session
);
951 hid
->ll_driver
= &hidp_hid_driver
;
953 hid
->hid_get_raw_report
= hidp_get_raw_report
;
954 hid
->hid_output_raw_report
= hidp_output_raw_report
;
959 kfree(session
->rd_data
);
960 session
->rd_data
= NULL
;
965 int hidp_add_connection(struct hidp_connadd_req
*req
, struct socket
*ctrl_sock
, struct socket
*intr_sock
)
967 struct hidp_session
*session
, *s
;
972 if (bacmp(&bt_sk(ctrl_sock
->sk
)->src
, &bt_sk(intr_sock
->sk
)->src
) ||
973 bacmp(&bt_sk(ctrl_sock
->sk
)->dst
, &bt_sk(intr_sock
->sk
)->dst
))
976 session
= kzalloc(sizeof(struct hidp_session
), GFP_KERNEL
);
980 BT_DBG("rd_data %p rd_size %d", req
->rd_data
, req
->rd_size
);
982 down_write(&hidp_session_sem
);
984 s
= __hidp_get_session(&bt_sk(ctrl_sock
->sk
)->dst
);
985 if (s
&& s
->state
== BT_CONNECTED
) {
990 bacpy(&session
->bdaddr
, &bt_sk(ctrl_sock
->sk
)->dst
);
992 session
->ctrl_mtu
= min_t(uint
, l2cap_pi(ctrl_sock
->sk
)->omtu
, l2cap_pi(ctrl_sock
->sk
)->imtu
);
993 session
->intr_mtu
= min_t(uint
, l2cap_pi(intr_sock
->sk
)->omtu
, l2cap_pi(intr_sock
->sk
)->imtu
);
995 BT_DBG("ctrl mtu %d intr mtu %d", session
->ctrl_mtu
, session
->intr_mtu
);
997 session
->ctrl_sock
= ctrl_sock
;
998 session
->intr_sock
= intr_sock
;
999 session
->state
= BT_CONNECTED
;
1001 setup_timer(&session
->timer
, hidp_idle_timeout
, (unsigned long)session
);
1003 skb_queue_head_init(&session
->ctrl_transmit
);
1004 skb_queue_head_init(&session
->intr_transmit
);
1006 mutex_init(&session
->report_mutex
);
1007 init_waitqueue_head(&session
->report_queue
);
1008 init_waitqueue_head(&session
->startup_queue
);
1009 session
->waiting_for_startup
= 1;
1010 session
->flags
= req
->flags
& (1 << HIDP_BLUETOOTH_VENDOR_ID
);
1011 session
->idle_to
= req
->idle_to
;
1013 if (req
->rd_size
> 0) {
1014 err
= hidp_setup_hid(session
, req
);
1015 if (err
&& err
!= -ENODEV
)
1019 if (!session
->hid
) {
1020 err
= hidp_setup_input(session
, req
);
1025 __hidp_link_session(session
);
1027 hidp_set_timer(session
);
1029 err
= kernel_thread(hidp_session
, session
, CLONE_KERNEL
);
1032 while (session
->waiting_for_startup
) {
1033 wait_event_interruptible(session
->startup_queue
,
1034 !session
->waiting_for_startup
);
1037 err
= hid_add_device(session
->hid
);
1039 goto err_add_device
;
1041 if (session
->input
) {
1042 hidp_send_ctrl_message(session
,
1043 HIDP_TRANS_SET_PROTOCOL
| HIDP_PROTO_BOOT
, NULL
, 0);
1044 session
->flags
|= (1 << HIDP_BOOT_PROTOCOL_MODE
);
1046 session
->leds
= 0xff;
1047 hidp_input_event(session
->input
, EV_LED
, 0, 0);
1050 up_write(&hidp_session_sem
);
1054 hid_destroy_device(session
->hid
);
1055 session
->hid
= NULL
;
1056 atomic_inc(&session
->terminate
);
1057 hidp_schedule(session
);
1060 hidp_del_timer(session
);
1062 __hidp_unlink_session(session
);
1064 if (session
->input
) {
1065 input_unregister_device(session
->input
);
1066 session
->input
= NULL
;
1070 hid_destroy_device(session
->hid
);
1071 session
->hid
= NULL
;
1074 kfree(session
->rd_data
);
1075 session
->rd_data
= NULL
;
1078 skb_queue_purge(&session
->ctrl_transmit
);
1079 skb_queue_purge(&session
->intr_transmit
);
1082 up_write(&hidp_session_sem
);
1084 input_free_device(session
->input
);
1089 int hidp_del_connection(struct hidp_conndel_req
*req
)
1091 struct hidp_session
*session
;
1096 down_read(&hidp_session_sem
);
1098 session
= __hidp_get_session(&req
->bdaddr
);
1100 if (req
->flags
& (1 << HIDP_VIRTUAL_CABLE_UNPLUG
)) {
1101 hidp_send_ctrl_message(session
,
1102 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
, NULL
, 0);
1104 /* Flush the transmit queues */
1105 skb_queue_purge(&session
->ctrl_transmit
);
1106 skb_queue_purge(&session
->intr_transmit
);
1108 /* Wakeup user-space polling for socket errors */
1109 session
->intr_sock
->sk
->sk_err
= EUNATCH
;
1110 session
->ctrl_sock
->sk
->sk_err
= EUNATCH
;
1112 /* Kill session thread */
1113 atomic_inc(&session
->terminate
);
1114 hidp_schedule(session
);
1119 up_read(&hidp_session_sem
);
1123 int hidp_get_connlist(struct hidp_connlist_req
*req
)
1125 struct list_head
*p
;
1130 down_read(&hidp_session_sem
);
1132 list_for_each(p
, &hidp_session_list
) {
1133 struct hidp_session
*session
;
1134 struct hidp_conninfo ci
;
1136 session
= list_entry(p
, struct hidp_session
, list
);
1138 __hidp_copy_session(session
, &ci
);
1140 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
1145 if (++n
>= req
->cnum
)
1152 up_read(&hidp_session_sem
);
1156 int hidp_get_conninfo(struct hidp_conninfo
*ci
)
1158 struct hidp_session
*session
;
1161 down_read(&hidp_session_sem
);
1163 session
= __hidp_get_session(&ci
->bdaddr
);
1165 __hidp_copy_session(session
, ci
);
1169 up_read(&hidp_session_sem
);
1173 static const struct hid_device_id hidp_table
[] = {
1174 { HID_BLUETOOTH_DEVICE(HID_ANY_ID
, HID_ANY_ID
) },
1178 static struct hid_driver hidp_driver
= {
1179 .name
= "generic-bluetooth",
1180 .id_table
= hidp_table
,
1183 static int __init
hidp_init(void)
1187 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION
);
1189 ret
= hid_register_driver(&hidp_driver
);
1193 ret
= hidp_init_sockets();
1199 hid_unregister_driver(&hidp_driver
);
1204 static void __exit
hidp_exit(void)
1206 hidp_cleanup_sockets();
1207 hid_unregister_driver(&hidp_driver
);
1210 module_init(hidp_init
);
1211 module_exit(hidp_exit
);
1213 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1214 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION
);
1215 MODULE_VERSION(VERSION
);
1216 MODULE_LICENSE("GPL");
1217 MODULE_ALIAS("bt-proto-6");