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>
40 #include <linux/kthread.h>
43 #include <linux/input.h>
44 #include <linux/hid.h>
45 #include <linux/hidraw.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49 #include <net/bluetooth/l2cap.h>
55 static DECLARE_RWSEM(hidp_session_sem
);
56 static LIST_HEAD(hidp_session_list
);
58 static unsigned char hidp_keycode
[256] = {
59 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36,
60 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45,
61 21, 44, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 28, 1,
62 14, 15, 57, 12, 13, 26, 27, 43, 43, 39, 40, 41, 51, 52,
63 53, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 87, 88,
64 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, 105, 108, 103, 69,
65 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, 72, 73,
66 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190,
67 191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135,
68 136, 113, 115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94,
69 95, 0, 0, 0, 122, 123, 90, 91, 85, 0, 0, 0, 0, 0,
70 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,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115,
76 114, 113, 150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140
79 static unsigned char hidp_mkeyspat
[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
81 static struct hidp_session
*__hidp_get_session(bdaddr_t
*bdaddr
)
83 struct hidp_session
*session
;
87 list_for_each_entry(session
, &hidp_session_list
, list
) {
88 if (!bacmp(bdaddr
, &session
->bdaddr
))
94 static void __hidp_link_session(struct hidp_session
*session
)
96 __module_get(THIS_MODULE
);
97 list_add(&session
->list
, &hidp_session_list
);
99 hci_conn_hold_device(session
->conn
);
102 static void __hidp_unlink_session(struct hidp_session
*session
)
104 hci_conn_put_device(session
->conn
);
106 list_del(&session
->list
);
107 module_put(THIS_MODULE
);
110 static void __hidp_copy_session(struct hidp_session
*session
, struct hidp_conninfo
*ci
)
112 memset(ci
, 0, sizeof(*ci
));
113 bacpy(&ci
->bdaddr
, &session
->bdaddr
);
115 ci
->flags
= session
->flags
;
116 ci
->state
= session
->state
;
119 ci
->product
= 0x0000;
120 ci
->version
= 0x0000;
122 if (session
->input
) {
123 ci
->vendor
= session
->input
->id
.vendor
;
124 ci
->product
= session
->input
->id
.product
;
125 ci
->version
= session
->input
->id
.version
;
126 if (session
->input
->name
)
127 strncpy(ci
->name
, session
->input
->name
, 128);
129 strncpy(ci
->name
, "HID Boot Device", 128);
133 ci
->vendor
= session
->hid
->vendor
;
134 ci
->product
= session
->hid
->product
;
135 ci
->version
= session
->hid
->version
;
136 strncpy(ci
->name
, session
->hid
->name
, 128);
140 static int hidp_queue_event(struct hidp_session
*session
, struct input_dev
*dev
,
141 unsigned int type
, unsigned int code
, int value
)
143 unsigned char newleds
;
146 BT_DBG("session %p type %d code %d value %d", session
, type
, code
, value
);
151 newleds
= (!!test_bit(LED_KANA
, dev
->led
) << 3) |
152 (!!test_bit(LED_COMPOSE
, dev
->led
) << 3) |
153 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) |
154 (!!test_bit(LED_CAPSL
, dev
->led
) << 1) |
155 (!!test_bit(LED_NUML
, dev
->led
));
157 if (session
->leds
== newleds
)
160 session
->leds
= newleds
;
162 skb
= alloc_skb(3, GFP_ATOMIC
);
164 BT_ERR("Can't allocate memory for new frame");
168 *skb_put(skb
, 1) = HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_OUPUT
;
169 *skb_put(skb
, 1) = 0x01;
170 *skb_put(skb
, 1) = newleds
;
172 skb_queue_tail(&session
->intr_transmit
, skb
);
174 hidp_schedule(session
);
179 static int hidp_hidinput_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
181 struct hid_device
*hid
= input_get_drvdata(dev
);
182 struct hidp_session
*session
= hid
->driver_data
;
184 return hidp_queue_event(session
, dev
, type
, code
, value
);
187 static int hidp_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
189 struct hidp_session
*session
= input_get_drvdata(dev
);
191 return hidp_queue_event(session
, dev
, type
, code
, value
);
194 static void hidp_input_report(struct hidp_session
*session
, struct sk_buff
*skb
)
196 struct input_dev
*dev
= session
->input
;
197 unsigned char *keys
= session
->keys
;
198 unsigned char *udata
= skb
->data
+ 1;
199 signed char *sdata
= skb
->data
+ 1;
200 int i
, size
= skb
->len
- 1;
202 switch (skb
->data
[0]) {
203 case 0x01: /* Keyboard report */
204 for (i
= 0; i
< 8; i
++)
205 input_report_key(dev
, hidp_keycode
[i
+ 224], (udata
[0] >> i
) & 1);
207 /* If all the key codes have been set to 0x01, it means
208 * too many keys were pressed at the same time. */
209 if (!memcmp(udata
+ 2, hidp_mkeyspat
, 6))
212 for (i
= 2; i
< 8; i
++) {
213 if (keys
[i
] > 3 && memscan(udata
+ 2, keys
[i
], 6) == udata
+ 8) {
214 if (hidp_keycode
[keys
[i
]])
215 input_report_key(dev
, hidp_keycode
[keys
[i
]], 0);
217 BT_ERR("Unknown key (scancode %#x) released.", keys
[i
]);
220 if (udata
[i
] > 3 && memscan(keys
+ 2, udata
[i
], 6) == keys
+ 8) {
221 if (hidp_keycode
[udata
[i
]])
222 input_report_key(dev
, hidp_keycode
[udata
[i
]], 1);
224 BT_ERR("Unknown key (scancode %#x) pressed.", udata
[i
]);
228 memcpy(keys
, udata
, 8);
231 case 0x02: /* Mouse report */
232 input_report_key(dev
, BTN_LEFT
, sdata
[0] & 0x01);
233 input_report_key(dev
, BTN_RIGHT
, sdata
[0] & 0x02);
234 input_report_key(dev
, BTN_MIDDLE
, sdata
[0] & 0x04);
235 input_report_key(dev
, BTN_SIDE
, sdata
[0] & 0x08);
236 input_report_key(dev
, BTN_EXTRA
, sdata
[0] & 0x10);
238 input_report_rel(dev
, REL_X
, sdata
[1]);
239 input_report_rel(dev
, REL_Y
, sdata
[2]);
242 input_report_rel(dev
, REL_WHEEL
, sdata
[3]);
249 static int __hidp_send_ctrl_message(struct hidp_session
*session
,
250 unsigned char hdr
, unsigned char *data
, int size
)
254 BT_DBG("session %p data %p size %d", session
, data
, size
);
256 if (atomic_read(&session
->terminate
))
259 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
261 BT_ERR("Can't allocate memory for new frame");
265 *skb_put(skb
, 1) = hdr
;
266 if (data
&& size
> 0)
267 memcpy(skb_put(skb
, size
), data
, size
);
269 skb_queue_tail(&session
->ctrl_transmit
, skb
);
274 static inline int hidp_send_ctrl_message(struct hidp_session
*session
,
275 unsigned char hdr
, unsigned char *data
, int size
)
279 err
= __hidp_send_ctrl_message(session
, hdr
, data
, size
);
281 hidp_schedule(session
);
286 static int hidp_queue_report(struct hidp_session
*session
,
287 unsigned char *data
, int size
)
291 BT_DBG("session %p hid %p data %p size %d", session
, session
->hid
, data
, size
);
293 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
295 BT_ERR("Can't allocate memory for new frame");
299 *skb_put(skb
, 1) = 0xa2;
301 memcpy(skb_put(skb
, size
), data
, size
);
303 skb_queue_tail(&session
->intr_transmit
, skb
);
305 hidp_schedule(session
);
310 static int hidp_send_report(struct hidp_session
*session
, struct hid_report
*report
)
312 unsigned char buf
[32];
315 rsize
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
316 if (rsize
> sizeof(buf
))
319 hid_output_report(report
, buf
);
321 return hidp_queue_report(session
, buf
, rsize
);
324 static int hidp_get_raw_report(struct hid_device
*hid
,
325 unsigned char report_number
,
326 unsigned char *data
, size_t count
,
327 unsigned char report_type
)
329 struct hidp_session
*session
= hid
->driver_data
;
332 int numbered_reports
= hid
->report_enum
[report_type
].numbered
;
335 switch (report_type
) {
336 case HID_FEATURE_REPORT
:
337 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
339 case HID_INPUT_REPORT
:
340 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_INPUT
;
342 case HID_OUTPUT_REPORT
:
343 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
349 if (mutex_lock_interruptible(&session
->report_mutex
))
352 /* Set up our wait, and send the report request to the device. */
353 session
->waiting_report_type
= report_type
& HIDP_DATA_RTYPE_MASK
;
354 session
->waiting_report_number
= numbered_reports
? report_number
: -1;
355 set_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
356 data
[0] = report_number
;
357 ret
= hidp_send_ctrl_message(hid
->driver_data
, report_type
, data
, 1);
361 /* Wait for the return of the report. The returned report
362 gets put in session->report_return. */
363 while (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
)) {
366 res
= wait_event_interruptible_timeout(session
->report_queue
,
367 !test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
),
381 skb
= session
->report_return
;
383 len
= skb
->len
< count
? skb
->len
: count
;
384 memcpy(data
, skb
->data
, len
);
387 session
->report_return
= NULL
;
389 /* Device returned a HANDSHAKE, indicating protocol error. */
393 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
394 mutex_unlock(&session
->report_mutex
);
399 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
400 mutex_unlock(&session
->report_mutex
);
404 static int hidp_output_raw_report(struct hid_device
*hid
, unsigned char *data
, size_t count
,
405 unsigned char report_type
)
407 struct hidp_session
*session
= hid
->driver_data
;
410 switch (report_type
) {
411 case HID_FEATURE_REPORT
:
412 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
414 case HID_OUTPUT_REPORT
:
415 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
421 if (mutex_lock_interruptible(&session
->report_mutex
))
424 /* Set up our wait, and send the report request to the device. */
425 set_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
426 ret
= hidp_send_ctrl_message(hid
->driver_data
, report_type
, data
,
431 /* Wait for the ACK from the device. */
432 while (test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
)) {
435 res
= wait_event_interruptible_timeout(session
->report_queue
,
436 !test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
),
450 if (!session
->output_report_success
) {
458 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
459 mutex_unlock(&session
->report_mutex
);
463 static void hidp_idle_timeout(unsigned long arg
)
465 struct hidp_session
*session
= (struct hidp_session
*) arg
;
467 atomic_inc(&session
->terminate
);
468 wake_up_process(session
->task
);
471 static void hidp_set_timer(struct hidp_session
*session
)
473 if (session
->idle_to
> 0)
474 mod_timer(&session
->timer
, jiffies
+ HZ
* session
->idle_to
);
477 static inline void hidp_del_timer(struct hidp_session
*session
)
479 if (session
->idle_to
> 0)
480 del_timer(&session
->timer
);
483 static void hidp_process_handshake(struct hidp_session
*session
,
486 BT_DBG("session %p param 0x%02x", session
, param
);
487 session
->output_report_success
= 0; /* default condition */
490 case HIDP_HSHK_SUCCESSFUL
:
491 /* FIXME: Call into SET_ GET_ handlers here */
492 session
->output_report_success
= 1;
495 case HIDP_HSHK_NOT_READY
:
496 case HIDP_HSHK_ERR_INVALID_REPORT_ID
:
497 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
:
498 case HIDP_HSHK_ERR_INVALID_PARAMETER
:
499 if (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
)) {
500 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
501 wake_up_interruptible(&session
->report_queue
);
503 /* FIXME: Call into SET_ GET_ handlers here */
506 case HIDP_HSHK_ERR_UNKNOWN
:
509 case HIDP_HSHK_ERR_FATAL
:
510 /* Device requests a reboot, as this is the only way this error
511 * can be recovered. */
512 __hidp_send_ctrl_message(session
,
513 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_SOFT_RESET
, NULL
, 0);
517 __hidp_send_ctrl_message(session
,
518 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
522 /* Wake up the waiting thread. */
523 if (test_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
)) {
524 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
525 wake_up_interruptible(&session
->report_queue
);
529 static void hidp_process_hid_control(struct hidp_session
*session
,
532 BT_DBG("session %p param 0x%02x", session
, param
);
534 if (param
== HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
) {
535 /* Flush the transmit queues */
536 skb_queue_purge(&session
->ctrl_transmit
);
537 skb_queue_purge(&session
->intr_transmit
);
539 atomic_inc(&session
->terminate
);
540 wake_up_process(current
);
544 /* Returns true if the passed-in skb should be freed by the caller. */
545 static int hidp_process_data(struct hidp_session
*session
, struct sk_buff
*skb
,
548 int done_with_skb
= 1;
549 BT_DBG("session %p skb %p len %d param 0x%02x", session
, skb
, skb
->len
, param
);
552 case HIDP_DATA_RTYPE_INPUT
:
553 hidp_set_timer(session
);
556 hidp_input_report(session
, skb
);
559 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 0);
562 case HIDP_DATA_RTYPE_OTHER
:
563 case HIDP_DATA_RTYPE_OUPUT
:
564 case HIDP_DATA_RTYPE_FEATURE
:
568 __hidp_send_ctrl_message(session
,
569 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
572 if (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
) &&
573 param
== session
->waiting_report_type
) {
574 if (session
->waiting_report_number
< 0 ||
575 session
->waiting_report_number
== skb
->data
[0]) {
576 /* hidp_get_raw_report() is waiting on this report. */
577 session
->report_return
= skb
;
579 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
580 wake_up_interruptible(&session
->report_queue
);
584 return done_with_skb
;
587 static void hidp_recv_ctrl_frame(struct hidp_session
*session
,
590 unsigned char hdr
, type
, param
;
593 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
598 type
= hdr
& HIDP_HEADER_TRANS_MASK
;
599 param
= hdr
& HIDP_HEADER_PARAM_MASK
;
602 case HIDP_TRANS_HANDSHAKE
:
603 hidp_process_handshake(session
, param
);
606 case HIDP_TRANS_HID_CONTROL
:
607 hidp_process_hid_control(session
, param
);
610 case HIDP_TRANS_DATA
:
611 free_skb
= hidp_process_data(session
, skb
, param
);
615 __hidp_send_ctrl_message(session
,
616 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
, NULL
, 0);
624 static void hidp_recv_intr_frame(struct hidp_session
*session
,
629 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
634 if (hdr
== (HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_INPUT
)) {
635 hidp_set_timer(session
);
638 hidp_input_report(session
, skb
);
641 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 1);
642 BT_DBG("report len %d", skb
->len
);
645 BT_DBG("Unsupported protocol header 0x%02x", hdr
);
651 static int hidp_send_frame(struct socket
*sock
, unsigned char *data
, int len
)
653 struct kvec iv
= { data
, len
};
656 BT_DBG("sock %p data %p len %d", sock
, data
, len
);
661 memset(&msg
, 0, sizeof(msg
));
663 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
666 static void hidp_process_transmit(struct hidp_session
*session
)
670 BT_DBG("session %p", session
);
672 while ((skb
= skb_dequeue(&session
->ctrl_transmit
))) {
673 if (hidp_send_frame(session
->ctrl_sock
, skb
->data
, skb
->len
) < 0) {
674 skb_queue_head(&session
->ctrl_transmit
, skb
);
678 hidp_set_timer(session
);
682 while ((skb
= skb_dequeue(&session
->intr_transmit
))) {
683 if (hidp_send_frame(session
->intr_sock
, skb
->data
, skb
->len
) < 0) {
684 skb_queue_head(&session
->intr_transmit
, skb
);
688 hidp_set_timer(session
);
693 static int hidp_session(void *arg
)
695 struct hidp_session
*session
= arg
;
696 struct sock
*ctrl_sk
= session
->ctrl_sock
->sk
;
697 struct sock
*intr_sk
= session
->intr_sock
->sk
;
699 wait_queue_t ctrl_wait
, intr_wait
;
701 BT_DBG("session %p", session
);
703 set_user_nice(current
, -15);
705 init_waitqueue_entry(&ctrl_wait
, current
);
706 init_waitqueue_entry(&intr_wait
, current
);
707 add_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
708 add_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
709 session
->waiting_for_startup
= 0;
710 wake_up_interruptible(&session
->startup_queue
);
711 set_current_state(TASK_INTERRUPTIBLE
);
712 while (!atomic_read(&session
->terminate
)) {
713 if (ctrl_sk
->sk_state
!= BT_CONNECTED
||
714 intr_sk
->sk_state
!= BT_CONNECTED
)
717 while ((skb
= skb_dequeue(&intr_sk
->sk_receive_queue
))) {
719 if (!skb_linearize(skb
))
720 hidp_recv_intr_frame(session
, skb
);
725 while ((skb
= skb_dequeue(&ctrl_sk
->sk_receive_queue
))) {
727 if (!skb_linearize(skb
))
728 hidp_recv_ctrl_frame(session
, skb
);
733 hidp_process_transmit(session
);
736 set_current_state(TASK_INTERRUPTIBLE
);
738 set_current_state(TASK_RUNNING
);
739 remove_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
740 remove_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
742 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
743 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
744 wake_up_interruptible(&session
->report_queue
);
746 down_write(&hidp_session_sem
);
748 hidp_del_timer(session
);
750 if (session
->input
) {
751 input_unregister_device(session
->input
);
752 session
->input
= NULL
;
756 hid_destroy_device(session
->hid
);
760 /* Wakeup user-space polling for socket errors */
761 session
->intr_sock
->sk
->sk_err
= EUNATCH
;
762 session
->ctrl_sock
->sk
->sk_err
= EUNATCH
;
764 hidp_schedule(session
);
766 fput(session
->intr_sock
->file
);
768 wait_event_timeout(*(sk_sleep(ctrl_sk
)),
769 (ctrl_sk
->sk_state
== BT_CLOSED
), msecs_to_jiffies(500));
771 fput(session
->ctrl_sock
->file
);
773 __hidp_unlink_session(session
);
775 up_write(&hidp_session_sem
);
777 kfree(session
->rd_data
);
782 static struct device
*hidp_get_device(struct hidp_session
*session
)
784 bdaddr_t
*src
= &bt_sk(session
->ctrl_sock
->sk
)->src
;
785 bdaddr_t
*dst
= &bt_sk(session
->ctrl_sock
->sk
)->dst
;
786 struct device
*device
= NULL
;
787 struct hci_dev
*hdev
;
789 hdev
= hci_get_route(dst
, src
);
793 session
->conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
795 device
= &session
->conn
->dev
;
802 static int hidp_setup_input(struct hidp_session
*session
,
803 struct hidp_connadd_req
*req
)
805 struct input_dev
*input
;
808 input
= input_allocate_device();
812 session
->input
= input
;
814 input_set_drvdata(input
, session
);
816 input
->name
= "Bluetooth HID Boot Protocol Device";
818 input
->id
.bustype
= BUS_BLUETOOTH
;
819 input
->id
.vendor
= req
->vendor
;
820 input
->id
.product
= req
->product
;
821 input
->id
.version
= req
->version
;
823 if (req
->subclass
& 0x40) {
824 set_bit(EV_KEY
, input
->evbit
);
825 set_bit(EV_LED
, input
->evbit
);
826 set_bit(EV_REP
, input
->evbit
);
828 set_bit(LED_NUML
, input
->ledbit
);
829 set_bit(LED_CAPSL
, input
->ledbit
);
830 set_bit(LED_SCROLLL
, input
->ledbit
);
831 set_bit(LED_COMPOSE
, input
->ledbit
);
832 set_bit(LED_KANA
, input
->ledbit
);
834 for (i
= 0; i
< sizeof(hidp_keycode
); i
++)
835 set_bit(hidp_keycode
[i
], input
->keybit
);
836 clear_bit(0, input
->keybit
);
839 if (req
->subclass
& 0x80) {
840 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
841 input
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
842 BIT_MASK(BTN_RIGHT
) | BIT_MASK(BTN_MIDDLE
);
843 input
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
844 input
->keybit
[BIT_WORD(BTN_MOUSE
)] |= BIT_MASK(BTN_SIDE
) |
846 input
->relbit
[0] |= BIT_MASK(REL_WHEEL
);
849 input
->dev
.parent
= hidp_get_device(session
);
851 input
->event
= hidp_input_event
;
853 err
= input_register_device(input
);
855 input_free_device(input
);
856 session
->input
= NULL
;
863 static int hidp_open(struct hid_device
*hid
)
868 static void hidp_close(struct hid_device
*hid
)
872 static int hidp_parse(struct hid_device
*hid
)
874 struct hidp_session
*session
= hid
->driver_data
;
876 return hid_parse_report(session
->hid
, session
->rd_data
,
880 static int hidp_start(struct hid_device
*hid
)
882 struct hidp_session
*session
= hid
->driver_data
;
883 struct hid_report
*report
;
885 if (hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
)
888 list_for_each_entry(report
, &hid
->report_enum
[HID_INPUT_REPORT
].
890 hidp_send_report(session
, report
);
892 list_for_each_entry(report
, &hid
->report_enum
[HID_FEATURE_REPORT
].
894 hidp_send_report(session
, report
);
899 static void hidp_stop(struct hid_device
*hid
)
901 struct hidp_session
*session
= hid
->driver_data
;
903 skb_queue_purge(&session
->ctrl_transmit
);
904 skb_queue_purge(&session
->intr_transmit
);
909 static struct hid_ll_driver hidp_hid_driver
= {
915 .hidinput_input_event
= hidp_hidinput_event
,
918 /* This function sets up the hid device. It does not add it
919 to the HID system. That is done in hidp_add_connection(). */
920 static int hidp_setup_hid(struct hidp_session
*session
,
921 struct hidp_connadd_req
*req
)
923 struct hid_device
*hid
;
926 session
->rd_data
= kzalloc(req
->rd_size
, GFP_KERNEL
);
927 if (!session
->rd_data
)
930 if (copy_from_user(session
->rd_data
, req
->rd_data
, req
->rd_size
)) {
934 session
->rd_size
= req
->rd_size
;
936 hid
= hid_allocate_device();
944 hid
->driver_data
= session
;
946 hid
->bus
= BUS_BLUETOOTH
;
947 hid
->vendor
= req
->vendor
;
948 hid
->product
= req
->product
;
949 hid
->version
= req
->version
;
950 hid
->country
= req
->country
;
952 strncpy(hid
->name
, req
->name
, 128);
953 strncpy(hid
->phys
, batostr(&bt_sk(session
->ctrl_sock
->sk
)->src
), 64);
954 strncpy(hid
->uniq
, batostr(&bt_sk(session
->ctrl_sock
->sk
)->dst
), 64);
956 hid
->dev
.parent
= hidp_get_device(session
);
957 hid
->ll_driver
= &hidp_hid_driver
;
959 hid
->hid_get_raw_report
= hidp_get_raw_report
;
960 hid
->hid_output_raw_report
= hidp_output_raw_report
;
965 kfree(session
->rd_data
);
966 session
->rd_data
= NULL
;
971 int hidp_add_connection(struct hidp_connadd_req
*req
, struct socket
*ctrl_sock
, struct socket
*intr_sock
)
973 struct hidp_session
*session
, *s
;
979 if (bacmp(&bt_sk(ctrl_sock
->sk
)->src
, &bt_sk(intr_sock
->sk
)->src
) ||
980 bacmp(&bt_sk(ctrl_sock
->sk
)->dst
, &bt_sk(intr_sock
->sk
)->dst
))
983 session
= kzalloc(sizeof(struct hidp_session
), GFP_KERNEL
);
987 BT_DBG("rd_data %p rd_size %d", req
->rd_data
, req
->rd_size
);
989 down_write(&hidp_session_sem
);
991 s
= __hidp_get_session(&bt_sk(ctrl_sock
->sk
)->dst
);
992 if (s
&& s
->state
== BT_CONNECTED
) {
997 bacpy(&session
->bdaddr
, &bt_sk(ctrl_sock
->sk
)->dst
);
999 session
->ctrl_mtu
= min_t(uint
, l2cap_pi(ctrl_sock
->sk
)->chan
->omtu
,
1000 l2cap_pi(ctrl_sock
->sk
)->chan
->imtu
);
1001 session
->intr_mtu
= min_t(uint
, l2cap_pi(intr_sock
->sk
)->chan
->omtu
,
1002 l2cap_pi(intr_sock
->sk
)->chan
->imtu
);
1004 BT_DBG("ctrl mtu %d intr mtu %d", session
->ctrl_mtu
, session
->intr_mtu
);
1006 session
->ctrl_sock
= ctrl_sock
;
1007 session
->intr_sock
= intr_sock
;
1008 session
->state
= BT_CONNECTED
;
1010 setup_timer(&session
->timer
, hidp_idle_timeout
, (unsigned long)session
);
1012 skb_queue_head_init(&session
->ctrl_transmit
);
1013 skb_queue_head_init(&session
->intr_transmit
);
1015 mutex_init(&session
->report_mutex
);
1016 init_waitqueue_head(&session
->report_queue
);
1017 init_waitqueue_head(&session
->startup_queue
);
1018 session
->waiting_for_startup
= 1;
1019 session
->flags
= req
->flags
& (1 << HIDP_BLUETOOTH_VENDOR_ID
);
1020 session
->idle_to
= req
->idle_to
;
1022 if (req
->rd_size
> 0) {
1023 err
= hidp_setup_hid(session
, req
);
1024 if (err
&& err
!= -ENODEV
)
1028 if (!session
->hid
) {
1029 err
= hidp_setup_input(session
, req
);
1034 __hidp_link_session(session
);
1036 hidp_set_timer(session
);
1039 vendor
= session
->hid
->vendor
;
1040 product
= session
->hid
->product
;
1041 } else if (session
->input
) {
1042 vendor
= session
->input
->id
.vendor
;
1043 product
= session
->input
->id
.product
;
1049 session
->task
= kthread_run(hidp_session
, session
, "khidpd_%04x%04x",
1051 if (IS_ERR(session
->task
)) {
1052 err
= PTR_ERR(session
->task
);
1056 while (session
->waiting_for_startup
) {
1057 wait_event_interruptible(session
->startup_queue
,
1058 !session
->waiting_for_startup
);
1061 err
= hid_add_device(session
->hid
);
1063 atomic_inc(&session
->terminate
);
1064 wake_up_process(session
->task
);
1065 up_write(&hidp_session_sem
);
1069 if (session
->input
) {
1070 hidp_send_ctrl_message(session
,
1071 HIDP_TRANS_SET_PROTOCOL
| HIDP_PROTO_BOOT
, NULL
, 0);
1072 session
->flags
|= (1 << HIDP_BOOT_PROTOCOL_MODE
);
1074 session
->leds
= 0xff;
1075 hidp_input_event(session
->input
, EV_LED
, 0, 0);
1078 up_write(&hidp_session_sem
);
1082 hidp_del_timer(session
);
1084 __hidp_unlink_session(session
);
1086 if (session
->input
) {
1087 input_unregister_device(session
->input
);
1088 session
->input
= NULL
;
1092 hid_destroy_device(session
->hid
);
1093 session
->hid
= NULL
;
1096 kfree(session
->rd_data
);
1097 session
->rd_data
= NULL
;
1100 skb_queue_purge(&session
->ctrl_transmit
);
1101 skb_queue_purge(&session
->intr_transmit
);
1104 up_write(&hidp_session_sem
);
1110 int hidp_del_connection(struct hidp_conndel_req
*req
)
1112 struct hidp_session
*session
;
1117 down_read(&hidp_session_sem
);
1119 session
= __hidp_get_session(&req
->bdaddr
);
1121 if (req
->flags
& (1 << HIDP_VIRTUAL_CABLE_UNPLUG
)) {
1122 hidp_send_ctrl_message(session
,
1123 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
, NULL
, 0);
1125 /* Flush the transmit queues */
1126 skb_queue_purge(&session
->ctrl_transmit
);
1127 skb_queue_purge(&session
->intr_transmit
);
1129 atomic_inc(&session
->terminate
);
1130 wake_up_process(session
->task
);
1135 up_read(&hidp_session_sem
);
1139 int hidp_get_connlist(struct hidp_connlist_req
*req
)
1141 struct hidp_session
*session
;
1146 down_read(&hidp_session_sem
);
1148 list_for_each_entry(session
, &hidp_session_list
, list
) {
1149 struct hidp_conninfo ci
;
1151 __hidp_copy_session(session
, &ci
);
1153 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
1158 if (++n
>= req
->cnum
)
1165 up_read(&hidp_session_sem
);
1169 int hidp_get_conninfo(struct hidp_conninfo
*ci
)
1171 struct hidp_session
*session
;
1174 down_read(&hidp_session_sem
);
1176 session
= __hidp_get_session(&ci
->bdaddr
);
1178 __hidp_copy_session(session
, ci
);
1182 up_read(&hidp_session_sem
);
1186 static const struct hid_device_id hidp_table
[] = {
1187 { HID_BLUETOOTH_DEVICE(HID_ANY_ID
, HID_ANY_ID
) },
1191 static struct hid_driver hidp_driver
= {
1192 .name
= "generic-bluetooth",
1193 .id_table
= hidp_table
,
1196 static int __init
hidp_init(void)
1200 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION
);
1202 ret
= hid_register_driver(&hidp_driver
);
1206 ret
= hidp_init_sockets();
1212 hid_unregister_driver(&hidp_driver
);
1217 static void __exit
hidp_exit(void)
1219 hidp_cleanup_sockets();
1220 hid_unregister_driver(&hidp_driver
);
1223 module_init(hidp_init
);
1224 module_exit(hidp_exit
);
1226 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1227 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION
);
1228 MODULE_VERSION(VERSION
);
1229 MODULE_LICENSE("GPL");
1230 MODULE_ALIAS("bt-proto-6");