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
))
95 static void __hidp_link_session(struct hidp_session
*session
)
97 list_add(&session
->list
, &hidp_session_list
);
100 static void __hidp_unlink_session(struct hidp_session
*session
)
102 hci_conn_put_device(session
->conn
);
104 list_del(&session
->list
);
107 static void __hidp_copy_session(struct hidp_session
*session
, struct hidp_conninfo
*ci
)
109 memset(ci
, 0, sizeof(*ci
));
110 bacpy(&ci
->bdaddr
, &session
->bdaddr
);
112 ci
->flags
= session
->flags
;
113 ci
->state
= session
->state
;
116 ci
->product
= 0x0000;
117 ci
->version
= 0x0000;
119 if (session
->input
) {
120 ci
->vendor
= session
->input
->id
.vendor
;
121 ci
->product
= session
->input
->id
.product
;
122 ci
->version
= session
->input
->id
.version
;
123 if (session
->input
->name
)
124 strncpy(ci
->name
, session
->input
->name
, 128);
126 strncpy(ci
->name
, "HID Boot Device", 128);
130 ci
->vendor
= session
->hid
->vendor
;
131 ci
->product
= session
->hid
->product
;
132 ci
->version
= session
->hid
->version
;
133 strncpy(ci
->name
, session
->hid
->name
, 128);
137 static int hidp_queue_event(struct hidp_session
*session
, struct input_dev
*dev
,
138 unsigned int type
, unsigned int code
, int value
)
140 unsigned char newleds
;
143 BT_DBG("session %p type %d code %d value %d", session
, type
, code
, value
);
148 newleds
= (!!test_bit(LED_KANA
, dev
->led
) << 3) |
149 (!!test_bit(LED_COMPOSE
, dev
->led
) << 3) |
150 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) |
151 (!!test_bit(LED_CAPSL
, dev
->led
) << 1) |
152 (!!test_bit(LED_NUML
, dev
->led
));
154 if (session
->leds
== newleds
)
157 session
->leds
= newleds
;
159 skb
= alloc_skb(3, GFP_ATOMIC
);
161 BT_ERR("Can't allocate memory for new frame");
165 *skb_put(skb
, 1) = HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_OUPUT
;
166 *skb_put(skb
, 1) = 0x01;
167 *skb_put(skb
, 1) = newleds
;
169 skb_queue_tail(&session
->intr_transmit
, skb
);
171 hidp_schedule(session
);
176 static int hidp_hidinput_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
178 struct hid_device
*hid
= input_get_drvdata(dev
);
179 struct hidp_session
*session
= hid
->driver_data
;
181 return hidp_queue_event(session
, dev
, type
, code
, value
);
184 static int hidp_input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
186 struct hidp_session
*session
= input_get_drvdata(dev
);
188 return hidp_queue_event(session
, dev
, type
, code
, value
);
191 static void hidp_input_report(struct hidp_session
*session
, struct sk_buff
*skb
)
193 struct input_dev
*dev
= session
->input
;
194 unsigned char *keys
= session
->keys
;
195 unsigned char *udata
= skb
->data
+ 1;
196 signed char *sdata
= skb
->data
+ 1;
197 int i
, size
= skb
->len
- 1;
199 switch (skb
->data
[0]) {
200 case 0x01: /* Keyboard report */
201 for (i
= 0; i
< 8; i
++)
202 input_report_key(dev
, hidp_keycode
[i
+ 224], (udata
[0] >> i
) & 1);
204 /* If all the key codes have been set to 0x01, it means
205 * too many keys were pressed at the same time. */
206 if (!memcmp(udata
+ 2, hidp_mkeyspat
, 6))
209 for (i
= 2; i
< 8; i
++) {
210 if (keys
[i
] > 3 && memscan(udata
+ 2, keys
[i
], 6) == udata
+ 8) {
211 if (hidp_keycode
[keys
[i
]])
212 input_report_key(dev
, hidp_keycode
[keys
[i
]], 0);
214 BT_ERR("Unknown key (scancode %#x) released.", keys
[i
]);
217 if (udata
[i
] > 3 && memscan(keys
+ 2, udata
[i
], 6) == keys
+ 8) {
218 if (hidp_keycode
[udata
[i
]])
219 input_report_key(dev
, hidp_keycode
[udata
[i
]], 1);
221 BT_ERR("Unknown key (scancode %#x) pressed.", udata
[i
]);
225 memcpy(keys
, udata
, 8);
228 case 0x02: /* Mouse report */
229 input_report_key(dev
, BTN_LEFT
, sdata
[0] & 0x01);
230 input_report_key(dev
, BTN_RIGHT
, sdata
[0] & 0x02);
231 input_report_key(dev
, BTN_MIDDLE
, sdata
[0] & 0x04);
232 input_report_key(dev
, BTN_SIDE
, sdata
[0] & 0x08);
233 input_report_key(dev
, BTN_EXTRA
, sdata
[0] & 0x10);
235 input_report_rel(dev
, REL_X
, sdata
[1]);
236 input_report_rel(dev
, REL_Y
, sdata
[2]);
239 input_report_rel(dev
, REL_WHEEL
, sdata
[3]);
246 static int __hidp_send_ctrl_message(struct hidp_session
*session
,
247 unsigned char hdr
, unsigned char *data
, int size
)
251 BT_DBG("session %p data %p size %d", session
, data
, size
);
253 if (atomic_read(&session
->terminate
))
256 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
258 BT_ERR("Can't allocate memory for new frame");
262 *skb_put(skb
, 1) = hdr
;
263 if (data
&& size
> 0)
264 memcpy(skb_put(skb
, size
), data
, size
);
266 skb_queue_tail(&session
->ctrl_transmit
, skb
);
271 static inline int hidp_send_ctrl_message(struct hidp_session
*session
,
272 unsigned char hdr
, unsigned char *data
, int size
)
276 err
= __hidp_send_ctrl_message(session
, hdr
, data
, size
);
278 hidp_schedule(session
);
283 static int hidp_queue_report(struct hidp_session
*session
,
284 unsigned char *data
, int size
)
288 BT_DBG("session %p hid %p data %p size %d", session
, session
->hid
, data
, size
);
290 skb
= alloc_skb(size
+ 1, GFP_ATOMIC
);
292 BT_ERR("Can't allocate memory for new frame");
296 *skb_put(skb
, 1) = 0xa2;
298 memcpy(skb_put(skb
, size
), data
, size
);
300 skb_queue_tail(&session
->intr_transmit
, skb
);
302 hidp_schedule(session
);
307 static int hidp_send_report(struct hidp_session
*session
, struct hid_report
*report
)
309 unsigned char buf
[32];
312 rsize
= ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
313 if (rsize
> sizeof(buf
))
316 hid_output_report(report
, buf
);
318 return hidp_queue_report(session
, buf
, rsize
);
321 static int hidp_get_raw_report(struct hid_device
*hid
,
322 unsigned char report_number
,
323 unsigned char *data
, size_t count
,
324 unsigned char report_type
)
326 struct hidp_session
*session
= hid
->driver_data
;
329 int numbered_reports
= hid
->report_enum
[report_type
].numbered
;
332 switch (report_type
) {
333 case HID_FEATURE_REPORT
:
334 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
336 case HID_INPUT_REPORT
:
337 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_INPUT
;
339 case HID_OUTPUT_REPORT
:
340 report_type
= HIDP_TRANS_GET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
346 if (mutex_lock_interruptible(&session
->report_mutex
))
349 /* Set up our wait, and send the report request to the device. */
350 session
->waiting_report_type
= report_type
& HIDP_DATA_RTYPE_MASK
;
351 session
->waiting_report_number
= numbered_reports
? report_number
: -1;
352 set_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
353 data
[0] = report_number
;
354 ret
= hidp_send_ctrl_message(hid
->driver_data
, report_type
, data
, 1);
358 /* Wait for the return of the report. The returned report
359 gets put in session->report_return. */
360 while (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
)) {
363 res
= wait_event_interruptible_timeout(session
->report_queue
,
364 !test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
),
378 skb
= session
->report_return
;
380 len
= skb
->len
< count
? skb
->len
: count
;
381 memcpy(data
, skb
->data
, len
);
384 session
->report_return
= NULL
;
386 /* Device returned a HANDSHAKE, indicating protocol error. */
390 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
391 mutex_unlock(&session
->report_mutex
);
396 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
397 mutex_unlock(&session
->report_mutex
);
401 static int hidp_output_raw_report(struct hid_device
*hid
, unsigned char *data
, size_t count
,
402 unsigned char report_type
)
404 struct hidp_session
*session
= hid
->driver_data
;
407 switch (report_type
) {
408 case HID_FEATURE_REPORT
:
409 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_FEATURE
;
411 case HID_OUTPUT_REPORT
:
412 report_type
= HIDP_TRANS_SET_REPORT
| HIDP_DATA_RTYPE_OUPUT
;
418 if (mutex_lock_interruptible(&session
->report_mutex
))
421 /* Set up our wait, and send the report request to the device. */
422 set_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
423 ret
= hidp_send_ctrl_message(hid
->driver_data
, report_type
, data
,
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 wake_up_process(session
->task
);
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_and_clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
))
497 wake_up_interruptible(&session
->report_queue
);
499 /* FIXME: Call into SET_ GET_ handlers here */
502 case HIDP_HSHK_ERR_UNKNOWN
:
505 case HIDP_HSHK_ERR_FATAL
:
506 /* Device requests a reboot, as this is the only way this error
507 * can be recovered. */
508 __hidp_send_ctrl_message(session
,
509 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_SOFT_RESET
, NULL
, 0);
513 __hidp_send_ctrl_message(session
,
514 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
518 /* Wake up the waiting thread. */
519 if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
))
520 wake_up_interruptible(&session
->report_queue
);
523 static void hidp_process_hid_control(struct hidp_session
*session
,
526 BT_DBG("session %p param 0x%02x", session
, param
);
528 if (param
== HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
) {
529 /* Flush the transmit queues */
530 skb_queue_purge(&session
->ctrl_transmit
);
531 skb_queue_purge(&session
->intr_transmit
);
533 atomic_inc(&session
->terminate
);
534 wake_up_process(current
);
538 /* Returns true if the passed-in skb should be freed by the caller. */
539 static int hidp_process_data(struct hidp_session
*session
, struct sk_buff
*skb
,
542 int done_with_skb
= 1;
543 BT_DBG("session %p skb %p len %d param 0x%02x", session
, skb
, skb
->len
, param
);
546 case HIDP_DATA_RTYPE_INPUT
:
547 hidp_set_timer(session
);
550 hidp_input_report(session
, skb
);
553 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 0);
556 case HIDP_DATA_RTYPE_OTHER
:
557 case HIDP_DATA_RTYPE_OUPUT
:
558 case HIDP_DATA_RTYPE_FEATURE
:
562 __hidp_send_ctrl_message(session
,
563 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_INVALID_PARAMETER
, NULL
, 0);
566 if (test_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
) &&
567 param
== session
->waiting_report_type
) {
568 if (session
->waiting_report_number
< 0 ||
569 session
->waiting_report_number
== skb
->data
[0]) {
570 /* hidp_get_raw_report() is waiting on this report. */
571 session
->report_return
= skb
;
573 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
574 wake_up_interruptible(&session
->report_queue
);
578 return done_with_skb
;
581 static void hidp_recv_ctrl_frame(struct hidp_session
*session
,
584 unsigned char hdr
, type
, param
;
587 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
592 type
= hdr
& HIDP_HEADER_TRANS_MASK
;
593 param
= hdr
& HIDP_HEADER_PARAM_MASK
;
596 case HIDP_TRANS_HANDSHAKE
:
597 hidp_process_handshake(session
, param
);
600 case HIDP_TRANS_HID_CONTROL
:
601 hidp_process_hid_control(session
, param
);
604 case HIDP_TRANS_DATA
:
605 free_skb
= hidp_process_data(session
, skb
, param
);
609 __hidp_send_ctrl_message(session
,
610 HIDP_TRANS_HANDSHAKE
| HIDP_HSHK_ERR_UNSUPPORTED_REQUEST
, NULL
, 0);
618 static void hidp_recv_intr_frame(struct hidp_session
*session
,
623 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
628 if (hdr
== (HIDP_TRANS_DATA
| HIDP_DATA_RTYPE_INPUT
)) {
629 hidp_set_timer(session
);
632 hidp_input_report(session
, skb
);
635 hid_input_report(session
->hid
, HID_INPUT_REPORT
, skb
->data
, skb
->len
, 1);
636 BT_DBG("report len %d", skb
->len
);
639 BT_DBG("Unsupported protocol header 0x%02x", hdr
);
645 static int hidp_send_frame(struct socket
*sock
, unsigned char *data
, int len
)
647 struct kvec iv
= { data
, len
};
650 BT_DBG("sock %p data %p len %d", sock
, data
, len
);
655 memset(&msg
, 0, sizeof(msg
));
657 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
660 static void hidp_process_intr_transmit(struct hidp_session
*session
)
664 BT_DBG("session %p", session
);
666 while ((skb
= skb_dequeue(&session
->intr_transmit
))) {
667 if (hidp_send_frame(session
->intr_sock
, skb
->data
, skb
->len
) < 0) {
668 skb_queue_head(&session
->intr_transmit
, skb
);
672 hidp_set_timer(session
);
677 static void hidp_process_ctrl_transmit(struct hidp_session
*session
)
681 BT_DBG("session %p", session
);
683 while ((skb
= skb_dequeue(&session
->ctrl_transmit
))) {
684 if (hidp_send_frame(session
->ctrl_sock
, skb
->data
, skb
->len
) < 0) {
685 skb_queue_head(&session
->ctrl_transmit
, skb
);
689 hidp_set_timer(session
);
694 static int hidp_session(void *arg
)
696 struct hidp_session
*session
= arg
;
697 struct sock
*ctrl_sk
= session
->ctrl_sock
->sk
;
698 struct sock
*intr_sk
= session
->intr_sock
->sk
;
700 wait_queue_t ctrl_wait
, intr_wait
;
702 BT_DBG("session %p", session
);
704 __module_get(THIS_MODULE
);
705 set_user_nice(current
, -15);
707 init_waitqueue_entry(&ctrl_wait
, current
);
708 init_waitqueue_entry(&intr_wait
, current
);
709 add_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
710 add_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
711 session
->waiting_for_startup
= 0;
712 wake_up_interruptible(&session
->startup_queue
);
713 set_current_state(TASK_INTERRUPTIBLE
);
714 while (!atomic_read(&session
->terminate
)) {
715 if (ctrl_sk
->sk_state
!= BT_CONNECTED
||
716 intr_sk
->sk_state
!= BT_CONNECTED
)
719 while ((skb
= skb_dequeue(&intr_sk
->sk_receive_queue
))) {
721 if (!skb_linearize(skb
))
722 hidp_recv_intr_frame(session
, skb
);
727 hidp_process_intr_transmit(session
);
729 while ((skb
= skb_dequeue(&ctrl_sk
->sk_receive_queue
))) {
731 if (!skb_linearize(skb
))
732 hidp_recv_ctrl_frame(session
, skb
);
737 hidp_process_ctrl_transmit(session
);
740 set_current_state(TASK_INTERRUPTIBLE
);
742 set_current_state(TASK_RUNNING
);
743 remove_wait_queue(sk_sleep(intr_sk
), &intr_wait
);
744 remove_wait_queue(sk_sleep(ctrl_sk
), &ctrl_wait
);
746 clear_bit(HIDP_WAITING_FOR_SEND_ACK
, &session
->flags
);
747 clear_bit(HIDP_WAITING_FOR_RETURN
, &session
->flags
);
748 wake_up_interruptible(&session
->report_queue
);
750 down_write(&hidp_session_sem
);
752 hidp_del_timer(session
);
754 if (session
->input
) {
755 input_unregister_device(session
->input
);
756 session
->input
= NULL
;
760 hid_destroy_device(session
->hid
);
764 /* Wakeup user-space polling for socket errors */
765 session
->intr_sock
->sk
->sk_err
= EUNATCH
;
766 session
->ctrl_sock
->sk
->sk_err
= EUNATCH
;
768 hidp_schedule(session
);
770 fput(session
->intr_sock
->file
);
772 wait_event_timeout(*(sk_sleep(ctrl_sk
)),
773 (ctrl_sk
->sk_state
== BT_CLOSED
), msecs_to_jiffies(500));
775 fput(session
->ctrl_sock
->file
);
777 __hidp_unlink_session(session
);
779 up_write(&hidp_session_sem
);
781 kfree(session
->rd_data
);
783 module_put_and_exit(0);
787 static struct hci_conn
*hidp_get_connection(struct hidp_session
*session
)
789 bdaddr_t
*src
= &bt_sk(session
->ctrl_sock
->sk
)->src
;
790 bdaddr_t
*dst
= &bt_sk(session
->ctrl_sock
->sk
)->dst
;
791 struct hci_conn
*conn
;
792 struct hci_dev
*hdev
;
794 hdev
= hci_get_route(dst
, src
);
799 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
801 hci_conn_hold_device(conn
);
802 hci_dev_unlock(hdev
);
809 static int hidp_setup_input(struct hidp_session
*session
,
810 struct hidp_connadd_req
*req
)
812 struct input_dev
*input
;
815 input
= input_allocate_device();
819 session
->input
= input
;
821 input_set_drvdata(input
, session
);
823 input
->name
= "Bluetooth HID Boot Protocol Device";
825 input
->id
.bustype
= BUS_BLUETOOTH
;
826 input
->id
.vendor
= req
->vendor
;
827 input
->id
.product
= req
->product
;
828 input
->id
.version
= req
->version
;
830 if (req
->subclass
& 0x40) {
831 set_bit(EV_KEY
, input
->evbit
);
832 set_bit(EV_LED
, input
->evbit
);
833 set_bit(EV_REP
, input
->evbit
);
835 set_bit(LED_NUML
, input
->ledbit
);
836 set_bit(LED_CAPSL
, input
->ledbit
);
837 set_bit(LED_SCROLLL
, input
->ledbit
);
838 set_bit(LED_COMPOSE
, input
->ledbit
);
839 set_bit(LED_KANA
, input
->ledbit
);
841 for (i
= 0; i
< sizeof(hidp_keycode
); i
++)
842 set_bit(hidp_keycode
[i
], input
->keybit
);
843 clear_bit(0, input
->keybit
);
846 if (req
->subclass
& 0x80) {
847 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
848 input
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
849 BIT_MASK(BTN_RIGHT
) | BIT_MASK(BTN_MIDDLE
);
850 input
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
851 input
->keybit
[BIT_WORD(BTN_MOUSE
)] |= BIT_MASK(BTN_SIDE
) |
853 input
->relbit
[0] |= BIT_MASK(REL_WHEEL
);
856 input
->dev
.parent
= &session
->conn
->dev
;
858 input
->event
= hidp_input_event
;
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
= &session
->conn
->dev
;
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 BT_DBG("rd_data %p rd_size %d", req
->rd_data
, req
->rd_size
);
985 down_write(&hidp_session_sem
);
987 s
= __hidp_get_session(&bt_sk(ctrl_sock
->sk
)->dst
);
988 if (s
&& s
->state
== BT_CONNECTED
) {
989 up_write(&hidp_session_sem
);
993 session
= kzalloc(sizeof(struct hidp_session
), GFP_KERNEL
);
995 up_write(&hidp_session_sem
);
999 bacpy(&session
->bdaddr
, &bt_sk(ctrl_sock
->sk
)->dst
);
1001 session
->ctrl_mtu
= min_t(uint
, l2cap_pi(ctrl_sock
->sk
)->chan
->omtu
,
1002 l2cap_pi(ctrl_sock
->sk
)->chan
->imtu
);
1003 session
->intr_mtu
= min_t(uint
, l2cap_pi(intr_sock
->sk
)->chan
->omtu
,
1004 l2cap_pi(intr_sock
->sk
)->chan
->imtu
);
1006 BT_DBG("ctrl mtu %d intr mtu %d", session
->ctrl_mtu
, session
->intr_mtu
);
1008 session
->ctrl_sock
= ctrl_sock
;
1009 session
->intr_sock
= intr_sock
;
1010 session
->state
= BT_CONNECTED
;
1012 session
->conn
= hidp_get_connection(session
);
1013 if (!session
->conn
) {
1018 setup_timer(&session
->timer
, hidp_idle_timeout
, (unsigned long)session
);
1020 skb_queue_head_init(&session
->ctrl_transmit
);
1021 skb_queue_head_init(&session
->intr_transmit
);
1023 mutex_init(&session
->report_mutex
);
1024 init_waitqueue_head(&session
->report_queue
);
1025 init_waitqueue_head(&session
->startup_queue
);
1026 session
->waiting_for_startup
= 1;
1027 session
->flags
= req
->flags
& (1 << HIDP_BLUETOOTH_VENDOR_ID
);
1028 session
->idle_to
= req
->idle_to
;
1030 __hidp_link_session(session
);
1032 if (req
->rd_size
> 0) {
1033 err
= hidp_setup_hid(session
, req
);
1038 if (!session
->hid
) {
1039 err
= hidp_setup_input(session
, req
);
1044 hidp_set_timer(session
);
1047 vendor
= session
->hid
->vendor
;
1048 product
= session
->hid
->product
;
1049 } else if (session
->input
) {
1050 vendor
= session
->input
->id
.vendor
;
1051 product
= session
->input
->id
.product
;
1057 session
->task
= kthread_run(hidp_session
, session
, "khidpd_%04x%04x",
1059 if (IS_ERR(session
->task
)) {
1060 err
= PTR_ERR(session
->task
);
1064 while (session
->waiting_for_startup
) {
1065 wait_event_interruptible(session
->startup_queue
,
1066 !session
->waiting_for_startup
);
1070 err
= hid_add_device(session
->hid
);
1072 err
= input_register_device(session
->input
);
1075 atomic_inc(&session
->terminate
);
1076 wake_up_process(session
->task
);
1077 up_write(&hidp_session_sem
);
1081 if (session
->input
) {
1082 hidp_send_ctrl_message(session
,
1083 HIDP_TRANS_SET_PROTOCOL
| HIDP_PROTO_BOOT
, NULL
, 0);
1084 session
->flags
|= (1 << HIDP_BOOT_PROTOCOL_MODE
);
1086 session
->leds
= 0xff;
1087 hidp_input_event(session
->input
, EV_LED
, 0, 0);
1090 up_write(&hidp_session_sem
);
1094 hidp_del_timer(session
);
1096 if (session
->input
) {
1097 input_unregister_device(session
->input
);
1098 session
->input
= NULL
;
1102 hid_destroy_device(session
->hid
);
1103 session
->hid
= NULL
;
1106 kfree(session
->rd_data
);
1107 session
->rd_data
= NULL
;
1110 __hidp_unlink_session(session
);
1112 skb_queue_purge(&session
->ctrl_transmit
);
1113 skb_queue_purge(&session
->intr_transmit
);
1116 up_write(&hidp_session_sem
);
1122 int hidp_del_connection(struct hidp_conndel_req
*req
)
1124 struct hidp_session
*session
;
1129 down_read(&hidp_session_sem
);
1131 session
= __hidp_get_session(&req
->bdaddr
);
1133 if (req
->flags
& (1 << HIDP_VIRTUAL_CABLE_UNPLUG
)) {
1134 hidp_send_ctrl_message(session
,
1135 HIDP_TRANS_HID_CONTROL
| HIDP_CTRL_VIRTUAL_CABLE_UNPLUG
, NULL
, 0);
1137 /* Flush the transmit queues */
1138 skb_queue_purge(&session
->ctrl_transmit
);
1139 skb_queue_purge(&session
->intr_transmit
);
1141 atomic_inc(&session
->terminate
);
1142 wake_up_process(session
->task
);
1147 up_read(&hidp_session_sem
);
1151 int hidp_get_connlist(struct hidp_connlist_req
*req
)
1153 struct hidp_session
*session
;
1158 down_read(&hidp_session_sem
);
1160 list_for_each_entry(session
, &hidp_session_list
, list
) {
1161 struct hidp_conninfo ci
;
1163 __hidp_copy_session(session
, &ci
);
1165 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
1170 if (++n
>= req
->cnum
)
1177 up_read(&hidp_session_sem
);
1181 int hidp_get_conninfo(struct hidp_conninfo
*ci
)
1183 struct hidp_session
*session
;
1186 down_read(&hidp_session_sem
);
1188 session
= __hidp_get_session(&ci
->bdaddr
);
1190 __hidp_copy_session(session
, ci
);
1194 up_read(&hidp_session_sem
);
1198 static const struct hid_device_id hidp_table
[] = {
1199 { HID_BLUETOOTH_DEVICE(HID_ANY_ID
, HID_ANY_ID
) },
1203 static struct hid_driver hidp_driver
= {
1204 .name
= "generic-bluetooth",
1205 .id_table
= hidp_table
,
1208 static int __init
hidp_init(void)
1212 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION
);
1214 ret
= hid_register_driver(&hidp_driver
);
1218 ret
= hidp_init_sockets();
1224 hid_unregister_driver(&hidp_driver
);
1229 static void __exit
hidp_exit(void)
1231 hidp_cleanup_sockets();
1232 hid_unregister_driver(&hidp_driver
);
1235 module_init(hidp_init
);
1236 module_exit(hidp_exit
);
1238 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1239 MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION
);
1240 MODULE_VERSION(VERSION
);
1241 MODULE_LICENSE("GPL");
1242 MODULE_ALIAS("bt-proto-6");