23 #include <linux/module.h>
26 #include <linux/hidraw.h>
39 static unsigned char hidp_keycode[256] = {
40 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36,
41 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45,
42 21, 44, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 28, 1,
43 14, 15, 57, 12, 13, 26, 27, 43, 43, 39, 40, 41, 51, 52,
44 53, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 87, 88,
45 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, 105, 108, 103, 69,
46 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, 72, 73,
47 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190,
48 191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135,
49 136, 113, 115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94,
50 95, 0, 0, 0, 122, 123, 90, 91, 85, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115,
57 114, 113, 150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140
60 static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
69 if (!bacmp(bdaddr, &session->
bdaddr))
76 static void __hidp_link_session(
struct hidp_session *session)
78 list_add(&session->
list, &hidp_session_list);
81 static void __hidp_unlink_session(
struct hidp_session *session)
90 memset(ci, 0,
sizeof(*ci));
100 if (session->
input) {
104 if (session->
input->name)
118 static int hidp_queue_event(
struct hidp_session *session,
struct input_dev *
dev,
121 unsigned char newleds;
124 BT_DBG(
"session %p type %d code %d value %d", session, type, code, value);
135 if (session->
leds == newleds)
138 session->
leds = newleds;
142 BT_ERR(
"Can't allocate memory for new frame");
152 hidp_schedule(session);
157 static int hidp_hidinput_event(
struct input_dev *dev,
unsigned int type,
unsigned int code,
int value)
159 struct hid_device *hid = input_get_drvdata(dev);
162 return hidp_queue_event(session, dev, type, code, value);
165 static int hidp_input_event(
struct input_dev *dev,
unsigned int type,
unsigned int code,
int value)
169 return hidp_queue_event(session, dev, type, code, value);
174 struct input_dev *dev = session->
input;
175 unsigned char *
keys = session->
keys;
176 unsigned char *udata = skb->
data + 1;
180 switch (skb->
data[0]) {
182 for (i = 0; i < 8; i++)
183 input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
187 if (!
memcmp(udata + 2, hidp_mkeyspat, 6))
190 for (i = 2; i < 8; i++) {
191 if (keys[i] > 3 &&
memscan(udata + 2, keys[i], 6) == udata + 8) {
192 if (hidp_keycode[keys[i]])
193 input_report_key(dev, hidp_keycode[keys[i]], 0);
195 BT_ERR(
"Unknown key (scancode %#x) released.", keys[i]);
198 if (udata[i] > 3 &&
memscan(keys + 2, udata[i], 6) == keys + 8) {
199 if (hidp_keycode[udata[i]])
200 input_report_key(dev, hidp_keycode[udata[i]], 1);
202 BT_ERR(
"Unknown key (scancode %#x) pressed.", udata[i]);
210 input_report_key(dev,
BTN_LEFT, sdata[0] & 0x01);
211 input_report_key(dev,
BTN_RIGHT, sdata[0] & 0x02);
212 input_report_key(dev,
BTN_MIDDLE, sdata[0] & 0x04);
213 input_report_key(dev,
BTN_SIDE, sdata[0] & 0x08);
214 input_report_key(dev,
BTN_EXTRA, sdata[0] & 0x10);
216 input_report_rel(dev,
REL_X, sdata[1]);
217 input_report_rel(dev,
REL_Y, sdata[2]);
220 input_report_rel(dev,
REL_WHEEL, sdata[3]);
227 static int __hidp_send_ctrl_message(
struct hidp_session *session,
228 unsigned char hdr,
unsigned char *
data,
233 BT_DBG(
"session %p data %p size %d", session, data, size);
240 BT_ERR(
"Can't allocate memory for new frame");
245 if (data && size > 0)
253 static int hidp_send_ctrl_message(
struct hidp_session *session,
254 unsigned char hdr,
unsigned char *data,
int size)
258 err = __hidp_send_ctrl_message(session, hdr, data, size);
260 hidp_schedule(session);
265 static int hidp_queue_report(
struct hidp_session *session,
266 unsigned char *data,
int size)
270 BT_DBG(
"session %p hid %p data %p size %d", session, session->
hid, data, size);
274 BT_ERR(
"Can't allocate memory for new frame");
284 hidp_schedule(session);
291 unsigned char buf[32];
294 rsize = ((report->
size - 1) >> 3) + 1 + (report->
id > 0);
295 if (rsize >
sizeof(buf))
300 return hidp_queue_report(session, buf, rsize);
303 static int hidp_get_raw_report(
struct hid_device *hid,
305 unsigned char *data,
size_t count,
306 unsigned char report_type)
311 int numbered_reports = hid->
report_enum[report_type].numbered;
314 switch (report_type) {
336 ret = hidp_send_ctrl_message(hid->
driver_data, report_type, data, 1);
383 static int hidp_output_raw_report(
struct hid_device *hid,
unsigned char *data,
size_t count,
384 unsigned char report_type)
389 switch (report_type) {
405 ret = hidp_send_ctrl_message(hid->
driver_data, report_type, data,
442 static void hidp_idle_timeout(
unsigned long arg)
450 static void hidp_set_timer(
struct hidp_session *session)
456 static void hidp_del_timer(
struct hidp_session *session)
462 static void hidp_process_handshake(
struct hidp_session *session,
465 BT_DBG(
"session %p param 0x%02x", session, param);
490 __hidp_send_ctrl_message(session,
495 __hidp_send_ctrl_message(session,
505 static void hidp_process_hid_control(
struct hidp_session *session,
508 BT_DBG(
"session %p param 0x%02x", session, param);
524 int done_with_skb = 1;
525 BT_DBG(
"session %p skb %p len %d param 0x%02x", session, skb, skb->
len, param);
529 hidp_set_timer(session);
532 hidp_input_report(session, skb);
544 __hidp_send_ctrl_message(session,
560 return done_with_skb;
563 static void hidp_recv_ctrl_frame(
struct hidp_session *session,
569 BT_DBG(
"session %p skb %p len %d", session, skb, skb->
len);
579 hidp_process_handshake(session, param);
583 hidp_process_hid_control(session, param);
587 free_skb = hidp_process_data(session, skb, param);
591 __hidp_send_ctrl_message(session,
600 static void hidp_recv_intr_frame(
struct hidp_session *session,
605 BT_DBG(
"session %p skb %p len %d", session, skb, skb->
len);
611 hidp_set_timer(session);
614 hidp_input_report(session, skb);
621 BT_DBG(
"Unsupported protocol header 0x%02x", hdr);
627 static int hidp_send_frame(
struct socket *
sock,
unsigned char *data,
int len)
632 BT_DBG(
"sock %p data %p len %d", sock, data, len);
642 static void hidp_process_intr_transmit(
struct hidp_session *session)
646 BT_DBG(
"session %p", session);
654 hidp_set_timer(session);
659 static void hidp_process_ctrl_transmit(
struct hidp_session *session)
663 BT_DBG(
"session %p", session);
671 hidp_set_timer(session);
679 struct sock *ctrl_sk = session->
ctrl_sock->sk;
680 struct sock *intr_sk = session->
intr_sock->sk;
684 BT_DBG(
"session %p", session);
689 init_waitqueue_entry(&ctrl_wait,
current);
690 init_waitqueue_entry(&intr_wait,
current);
703 if (!skb_linearize(skb))
704 hidp_recv_intr_frame(session, skb);
709 hidp_process_intr_transmit(session);
713 if (!skb_linearize(skb))
714 hidp_recv_ctrl_frame(session, skb);
719 hidp_process_ctrl_transmit(session);
734 hidp_del_timer(session);
736 if (session->
input) {
737 input_unregister_device(session->
input);
750 hidp_schedule(session);
759 __hidp_unlink_session(session);
781 conn = hci_conn_hash_lookup_ba(hdev,
ACL_LINK, dst);
791 static int hidp_setup_input(
struct hidp_session *session,
794 struct input_dev *
input;
797 input = input_allocate_device();
803 input_set_drvdata(input, session);
805 input->name =
"Bluetooth HID Boot Protocol Device";
808 input->id.vendor = req->
vendor;
809 input->id.product = req->
product;
810 input->id.version = req->
version;
823 for (i = 0; i <
sizeof(hidp_keycode); i++)
824 set_bit(hidp_keycode[i], input->keybit);
838 input->dev.parent = &session->
conn->dev;
840 input->event = hidp_input_event;
850 static void hidp_close(
struct hid_device *hid)
872 hidp_send_report(session, report);
876 hidp_send_report(session, report);
897 .hidinput_input_event = hidp_hidinput_event,
938 hid->
dev.parent = &session->
conn->dev;
961 if (bacmp(&
bt_sk(ctrl_sock->
sk)->src, &
bt_sk(intr_sock->
sk)->src) ||
969 s = __hidp_get_session(&
bt_sk(ctrl_sock->
sk)->dst);
994 session->
conn = hidp_get_connection(session);
995 if (!session->
conn) {
1002 skb_queue_head_init(&session->ctrl_transmit);
1003 skb_queue_head_init(&session->intr_transmit);
1008 session->waiting_for_startup = 1;
1010 session->idle_to = req->
idle_to;
1012 __hidp_link_session(session);
1015 err = hidp_setup_hid(session, req);
1020 if (!session->hid) {
1021 err = hidp_setup_input(session, req);
1026 hidp_set_timer(session);
1029 vendor = session->hid->vendor;
1030 product = session->hid->product;
1031 }
else if (session->input) {
1032 vendor = session->input->id.vendor;
1033 product = session->input->id.product;
1041 if (IS_ERR(session->task)) {
1042 err = PTR_ERR(session->task);
1046 while (session->waiting_for_startup) {
1048 !session->waiting_for_startup);
1054 err = input_register_device(session->input);
1063 if (session->input) {
1064 hidp_send_ctrl_message(session,
1068 session->leds = 0xff;
1069 hidp_input_event(session->input,
EV_LED, 0, 0);
1076 hidp_del_timer(session);
1078 if (session->input) {
1079 input_unregister_device(session->input);
1080 session->input =
NULL;
1085 session->hid =
NULL;
1088 kfree(session->rd_data);
1089 session->rd_data =
NULL;
1092 __hidp_unlink_session(session);
1113 session = __hidp_get_session(&req->
bdaddr);
1116 hidp_send_ctrl_message(session,
1145 __hidp_copy_session(session, &ci);
1152 if (++
n >= req->
cnum)
1170 session = __hidp_get_session(&ci->
bdaddr);
1172 __hidp_copy_session(session, ci);
1180 static int __init hidp_init(
void)
1187 static void __exit hidp_exit(
void)