YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | HIDP implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org> |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 4 | Copyright (C) 2013 David Herrmann <dh.herrmann@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License version 2 as |
| 8 | published by the Free Software Foundation; |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 11 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 13 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 14 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 24 | #include <linux/kref.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/file.h> |
Szymon Janc | aabf6f8 | 2011-04-05 15:37:45 +0200 | [diff] [blame] | 27 | #include <linux/kthread.h> |
Marcel Holtmann | 364f635 | 2009-08-22 14:15:53 -0700 | [diff] [blame] | 28 | #include <linux/hidraw.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 31 | #include <net/bluetooth/hci_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
| 33 | |
| 34 | #include "hidp.h" |
| 35 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 36 | #define VERSION "1.2" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static DECLARE_RWSEM(hidp_session_sem); |
| 39 | static LIST_HEAD(hidp_session_list); |
| 40 | |
| 41 | static unsigned char hidp_keycode[256] = { |
Szymon Janc | 17f09a7 | 2011-03-21 14:20:01 +0100 | [diff] [blame] | 42 | 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, |
| 43 | 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, |
| 44 | 21, 44, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, |
| 45 | 14, 15, 57, 12, 13, 26, 27, 43, 43, 39, 40, 41, 51, 52, |
| 46 | 53, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 87, 88, |
| 47 | 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, 105, 108, 103, 69, |
| 48 | 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, 72, 73, |
| 49 | 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190, |
| 50 | 191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135, |
| 51 | 136, 113, 115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94, |
| 52 | 95, 0, 0, 0, 122, 123, 90, 91, 85, 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 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 57 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 58 | 29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115, |
| 59 | 114, 113, 150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; |
| 63 | |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 64 | static int hidp_session_probe(struct l2cap_conn *conn, |
| 65 | struct l2cap_user *user); |
| 66 | static void hidp_session_remove(struct l2cap_conn *conn, |
| 67 | struct l2cap_user *user); |
| 68 | static int hidp_session_thread(void *arg); |
| 69 | static void hidp_session_terminate(struct hidp_session *s); |
| 70 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 71 | static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Vasiliy Kulikov | d31dbf6 | 2010-10-30 18:26:31 +0400 | [diff] [blame] | 73 | memset(ci, 0, sizeof(*ci)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | bacpy(&ci->bdaddr, &session->bdaddr); |
| 75 | |
| 76 | ci->flags = session->flags; |
David Herrmann | dcc0764 | 2013-04-06 20:28:40 +0200 | [diff] [blame] | 77 | ci->state = BT_CONNECTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | ci->vendor = 0x0000; |
| 80 | ci->product = 0x0000; |
| 81 | ci->version = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | if (session->input) { |
| 84 | ci->vendor = session->input->id.vendor; |
| 85 | ci->product = session->input->id.product; |
| 86 | ci->version = session->input->id.version; |
| 87 | if (session->input->name) |
| 88 | strncpy(ci->name, session->input->name, 128); |
| 89 | else |
| 90 | strncpy(ci->name, "HID Boot Device", 128); |
| 91 | } |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 92 | |
| 93 | if (session->hid) { |
| 94 | ci->vendor = session->hid->vendor; |
| 95 | ci->product = session->hid->product; |
| 96 | ci->version = session->hid->version; |
| 97 | strncpy(ci->name, session->hid->name, 128); |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 101 | /* assemble skb, queue message on @transmit and wake up the session thread */ |
| 102 | static int hidp_send_message(struct hidp_session *session, struct socket *sock, |
| 103 | struct sk_buff_head *transmit, unsigned char hdr, |
| 104 | const unsigned char *data, int size) |
| 105 | { |
| 106 | struct sk_buff *skb; |
| 107 | struct sock *sk = sock->sk; |
| 108 | |
| 109 | BT_DBG("session %p data %p size %d", session, data, size); |
| 110 | |
| 111 | if (atomic_read(&session->terminate)) |
| 112 | return -EIO; |
| 113 | |
| 114 | skb = alloc_skb(size + 1, GFP_ATOMIC); |
| 115 | if (!skb) { |
| 116 | BT_ERR("Can't allocate memory for new frame"); |
| 117 | return -ENOMEM; |
| 118 | } |
| 119 | |
| 120 | *skb_put(skb, 1) = hdr; |
| 121 | if (data && size > 0) |
| 122 | memcpy(skb_put(skb, size), data, size); |
| 123 | |
| 124 | skb_queue_tail(transmit, skb); |
| 125 | wake_up_interruptible(sk_sleep(sk)); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int hidp_send_ctrl_message(struct hidp_session *session, |
| 131 | unsigned char hdr, const unsigned char *data, |
| 132 | int size) |
| 133 | { |
| 134 | return hidp_send_message(session, session->ctrl_sock, |
| 135 | &session->ctrl_transmit, hdr, data, size); |
| 136 | } |
| 137 | |
| 138 | static int hidp_send_intr_message(struct hidp_session *session, |
| 139 | unsigned char hdr, const unsigned char *data, |
| 140 | int size) |
| 141 | { |
| 142 | return hidp_send_message(session, session->intr_sock, |
| 143 | &session->intr_transmit, hdr, data, size); |
| 144 | } |
| 145 | |
David Herrmann | af87b3d | 2013-04-06 20:28:51 +0200 | [diff] [blame] | 146 | static int hidp_input_event(struct input_dev *dev, unsigned int type, |
| 147 | unsigned int code, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
David Herrmann | af87b3d | 2013-04-06 20:28:51 +0200 | [diff] [blame] | 149 | struct hidp_session *session = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | unsigned char newleds; |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 151 | unsigned char hdr, data[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
David Herrmann | af87b3d | 2013-04-06 20:28:51 +0200 | [diff] [blame] | 153 | BT_DBG("session %p type %d code %d value %d", |
| 154 | session, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (type != EV_LED) |
| 157 | return -1; |
| 158 | |
| 159 | newleds = (!!test_bit(LED_KANA, dev->led) << 3) | |
| 160 | (!!test_bit(LED_COMPOSE, dev->led) << 3) | |
| 161 | (!!test_bit(LED_SCROLLL, dev->led) << 2) | |
| 162 | (!!test_bit(LED_CAPSL, dev->led) << 1) | |
| 163 | (!!test_bit(LED_NUML, dev->led)); |
| 164 | |
| 165 | if (session->leds == newleds) |
| 166 | return 0; |
| 167 | |
| 168 | session->leds = newleds; |
| 169 | |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 170 | hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; |
| 171 | data[0] = 0x01; |
| 172 | data[1] = newleds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 174 | return hidp_send_intr_message(session, hdr, data, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb) |
| 178 | { |
| 179 | struct input_dev *dev = session->input; |
| 180 | unsigned char *keys = session->keys; |
| 181 | unsigned char *udata = skb->data + 1; |
| 182 | signed char *sdata = skb->data + 1; |
| 183 | int i, size = skb->len - 1; |
| 184 | |
| 185 | switch (skb->data[0]) { |
| 186 | case 0x01: /* Keyboard report */ |
| 187 | for (i = 0; i < 8; i++) |
| 188 | input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1); |
| 189 | |
| 190 | /* If all the key codes have been set to 0x01, it means |
| 191 | * too many keys were pressed at the same time. */ |
| 192 | if (!memcmp(udata + 2, hidp_mkeyspat, 6)) |
| 193 | break; |
| 194 | |
| 195 | for (i = 2; i < 8; i++) { |
| 196 | if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) { |
| 197 | if (hidp_keycode[keys[i]]) |
| 198 | input_report_key(dev, hidp_keycode[keys[i]], 0); |
| 199 | else |
| 200 | BT_ERR("Unknown key (scancode %#x) released.", keys[i]); |
| 201 | } |
| 202 | |
| 203 | if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) { |
| 204 | if (hidp_keycode[udata[i]]) |
| 205 | input_report_key(dev, hidp_keycode[udata[i]], 1); |
| 206 | else |
| 207 | BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | memcpy(keys, udata, 8); |
| 212 | break; |
| 213 | |
| 214 | case 0x02: /* Mouse report */ |
| 215 | input_report_key(dev, BTN_LEFT, sdata[0] & 0x01); |
| 216 | input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02); |
| 217 | input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04); |
| 218 | input_report_key(dev, BTN_SIDE, sdata[0] & 0x08); |
| 219 | input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10); |
| 220 | |
| 221 | input_report_rel(dev, REL_X, sdata[1]); |
| 222 | input_report_rel(dev, REL_Y, sdata[2]); |
| 223 | |
| 224 | if (size > 3) |
| 225 | input_report_rel(dev, REL_WHEEL, sdata[3]); |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | input_sync(dev); |
| 230 | } |
| 231 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 232 | static int hidp_send_report(struct hidp_session *session, struct hid_report *report) |
| 233 | { |
Jiri Kosina | 27ce405 | 2013-07-10 19:56:27 +0200 | [diff] [blame] | 234 | unsigned char hdr; |
| 235 | u8 *buf; |
| 236 | int rsize, ret; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 237 | |
Jiri Kosina | 27ce405 | 2013-07-10 19:56:27 +0200 | [diff] [blame] | 238 | buf = hid_alloc_report_buf(report, GFP_ATOMIC); |
| 239 | if (!buf) |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 240 | return -EIO; |
| 241 | |
| 242 | hid_output_report(report, buf); |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 243 | hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 244 | |
Jiri Kosina | bc197ee | 2013-07-22 17:11:44 +0200 | [diff] [blame^] | 245 | rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
Jiri Kosina | 27ce405 | 2013-07-10 19:56:27 +0200 | [diff] [blame] | 246 | ret = hidp_send_intr_message(session, hdr, buf, rsize); |
| 247 | |
| 248 | kfree(buf); |
| 249 | return ret; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 252 | static int hidp_get_raw_report(struct hid_device *hid, |
| 253 | unsigned char report_number, |
| 254 | unsigned char *data, size_t count, |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 255 | unsigned char report_type) |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 256 | { |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 257 | struct hidp_session *session = hid->driver_data; |
| 258 | struct sk_buff *skb; |
| 259 | size_t len; |
| 260 | int numbered_reports = hid->report_enum[report_type].numbered; |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 261 | int ret; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 262 | |
Karl Relton | fd86c9b | 2013-02-20 18:16:19 +0000 | [diff] [blame] | 263 | if (atomic_read(&session->terminate)) |
| 264 | return -EIO; |
| 265 | |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 266 | switch (report_type) { |
| 267 | case HID_FEATURE_REPORT: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 268 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE; |
| 269 | break; |
| 270 | case HID_INPUT_REPORT: |
| 271 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT; |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 272 | break; |
| 273 | case HID_OUTPUT_REPORT: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 274 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT; |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 275 | break; |
| 276 | default: |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 280 | if (mutex_lock_interruptible(&session->report_mutex)) |
| 281 | return -ERESTARTSYS; |
| 282 | |
| 283 | /* Set up our wait, and send the report request to the device. */ |
| 284 | session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK; |
| 285 | session->waiting_report_number = numbered_reports ? report_number : -1; |
| 286 | set_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 287 | data[0] = report_number; |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 288 | ret = hidp_send_ctrl_message(session, report_type, data, 1); |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 289 | if (ret) |
| 290 | goto err; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 291 | |
| 292 | /* Wait for the return of the report. The returned report |
| 293 | gets put in session->report_return. */ |
David Herrmann | e3492dc | 2013-04-06 20:28:41 +0200 | [diff] [blame] | 294 | while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) && |
| 295 | !atomic_read(&session->terminate)) { |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 296 | int res; |
| 297 | |
| 298 | res = wait_event_interruptible_timeout(session->report_queue, |
David Herrmann | e3492dc | 2013-04-06 20:28:41 +0200 | [diff] [blame] | 299 | !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) |
| 300 | || atomic_read(&session->terminate), |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 301 | 5*HZ); |
| 302 | if (res == 0) { |
| 303 | /* timeout */ |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 304 | ret = -EIO; |
| 305 | goto err; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 306 | } |
| 307 | if (res < 0) { |
| 308 | /* signal */ |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 309 | ret = -ERESTARTSYS; |
| 310 | goto err; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | skb = session->report_return; |
| 315 | if (skb) { |
| 316 | len = skb->len < count ? skb->len : count; |
| 317 | memcpy(data, skb->data, len); |
| 318 | |
| 319 | kfree_skb(skb); |
| 320 | session->report_return = NULL; |
| 321 | } else { |
| 322 | /* Device returned a HANDSHAKE, indicating protocol error. */ |
| 323 | len = -EIO; |
| 324 | } |
| 325 | |
| 326 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 327 | mutex_unlock(&session->report_mutex); |
| 328 | |
| 329 | return len; |
| 330 | |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 331 | err: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 332 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 333 | mutex_unlock(&session->report_mutex); |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 334 | return ret; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 335 | } |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count, |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 338 | unsigned char report_type) |
| 339 | { |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 340 | struct hidp_session *session = hid->driver_data; |
| 341 | int ret; |
| 342 | |
David Herrmann | e73dcfb | 2013-04-06 20:28:52 +0200 | [diff] [blame] | 343 | if (report_type == HID_OUTPUT_REPORT) { |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 344 | report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; |
David Herrmann | e73dcfb | 2013-04-06 20:28:52 +0200 | [diff] [blame] | 345 | return hidp_send_intr_message(session, report_type, |
| 346 | data, count); |
| 347 | } else if (report_type != HID_FEATURE_REPORT) { |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 348 | return -EINVAL; |
| 349 | } |
| 350 | |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 351 | if (mutex_lock_interruptible(&session->report_mutex)) |
| 352 | return -ERESTARTSYS; |
| 353 | |
| 354 | /* Set up our wait, and send the report request to the device. */ |
| 355 | set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); |
David Herrmann | e73dcfb | 2013-04-06 20:28:52 +0200 | [diff] [blame] | 356 | report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE; |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 357 | ret = hidp_send_ctrl_message(session, report_type, data, count); |
David Herrmann | 794d175 | 2011-08-26 14:06:02 +0200 | [diff] [blame] | 358 | if (ret) |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 359 | goto err; |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 360 | |
| 361 | /* Wait for the ACK from the device. */ |
David Herrmann | e3492dc | 2013-04-06 20:28:41 +0200 | [diff] [blame] | 362 | while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) && |
| 363 | !atomic_read(&session->terminate)) { |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 364 | int res; |
| 365 | |
| 366 | res = wait_event_interruptible_timeout(session->report_queue, |
David Herrmann | e3492dc | 2013-04-06 20:28:41 +0200 | [diff] [blame] | 367 | !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) |
| 368 | || atomic_read(&session->terminate), |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 369 | 10*HZ); |
| 370 | if (res == 0) { |
| 371 | /* timeout */ |
| 372 | ret = -EIO; |
| 373 | goto err; |
| 374 | } |
| 375 | if (res < 0) { |
| 376 | /* signal */ |
| 377 | ret = -ERESTARTSYS; |
| 378 | goto err; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!session->output_report_success) { |
| 383 | ret = -EIO; |
| 384 | goto err; |
| 385 | } |
| 386 | |
| 387 | ret = count; |
| 388 | |
| 389 | err: |
| 390 | clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); |
| 391 | mutex_unlock(&session->report_mutex); |
| 392 | return ret; |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | static void hidp_idle_timeout(unsigned long arg) |
| 396 | { |
| 397 | struct hidp_session *session = (struct hidp_session *) arg; |
| 398 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 399 | hidp_session_terminate(session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 402 | static void hidp_set_timer(struct hidp_session *session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | if (session->idle_to > 0) |
| 405 | mod_timer(&session->timer, jiffies + HZ * session->idle_to); |
| 406 | } |
| 407 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 408 | static void hidp_del_timer(struct hidp_session *session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
| 410 | if (session->idle_to > 0) |
| 411 | del_timer(&session->timer); |
| 412 | } |
| 413 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 414 | static void hidp_process_handshake(struct hidp_session *session, |
| 415 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
| 417 | BT_DBG("session %p param 0x%02x", session, param); |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 418 | session->output_report_success = 0; /* default condition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | switch (param) { |
| 421 | case HIDP_HSHK_SUCCESSFUL: |
| 422 | /* FIXME: Call into SET_ GET_ handlers here */ |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 423 | session->output_report_success = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | case HIDP_HSHK_NOT_READY: |
| 427 | case HIDP_HSHK_ERR_INVALID_REPORT_ID: |
| 428 | case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST: |
| 429 | case HIDP_HSHK_ERR_INVALID_PARAMETER: |
Gustavo F. Padovan | ab88f71 | 2011-10-06 22:05:37 -0300 | [diff] [blame] | 430 | if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags)) |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 431 | wake_up_interruptible(&session->report_queue); |
Gustavo F. Padovan | ab88f71 | 2011-10-06 22:05:37 -0300 | [diff] [blame] | 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | /* FIXME: Call into SET_ GET_ handlers here */ |
| 434 | break; |
| 435 | |
| 436 | case HIDP_HSHK_ERR_UNKNOWN: |
| 437 | break; |
| 438 | |
| 439 | case HIDP_HSHK_ERR_FATAL: |
| 440 | /* Device requests a reboot, as this is the only way this error |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 441 | * can be recovered. */ |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 442 | hidp_send_ctrl_message(session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0); |
| 444 | break; |
| 445 | |
| 446 | default: |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 447 | hidp_send_ctrl_message(session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0); |
| 449 | break; |
| 450 | } |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 451 | |
| 452 | /* Wake up the waiting thread. */ |
Gustavo F. Padovan | ab88f71 | 2011-10-06 22:05:37 -0300 | [diff] [blame] | 453 | if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)) |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 454 | wake_up_interruptible(&session->report_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 457 | static void hidp_process_hid_control(struct hidp_session *session, |
| 458 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | BT_DBG("session %p param 0x%02x", session, param); |
| 461 | |
Dave Young | eff001e | 2008-02-05 03:07:14 -0800 | [diff] [blame] | 462 | if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* Flush the transmit queues */ |
| 464 | skb_queue_purge(&session->ctrl_transmit); |
| 465 | skb_queue_purge(&session->intr_transmit); |
| 466 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 467 | hidp_session_terminate(session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 471 | /* Returns true if the passed-in skb should be freed by the caller. */ |
| 472 | static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb, |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 473 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 475 | int done_with_skb = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param); |
| 477 | |
| 478 | switch (param) { |
| 479 | case HIDP_DATA_RTYPE_INPUT: |
| 480 | hidp_set_timer(session); |
| 481 | |
| 482 | if (session->input) |
| 483 | hidp_input_report(session, skb); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 484 | |
| 485 | if (session->hid) |
| 486 | hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | break; |
| 488 | |
| 489 | case HIDP_DATA_RTYPE_OTHER: |
| 490 | case HIDP_DATA_RTYPE_OUPUT: |
| 491 | case HIDP_DATA_RTYPE_FEATURE: |
| 492 | break; |
| 493 | |
| 494 | default: |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 495 | hidp_send_ctrl_message(session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0); |
| 497 | } |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 498 | |
| 499 | if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) && |
| 500 | param == session->waiting_report_type) { |
| 501 | if (session->waiting_report_number < 0 || |
| 502 | session->waiting_report_number == skb->data[0]) { |
| 503 | /* hidp_get_raw_report() is waiting on this report. */ |
| 504 | session->report_return = skb; |
| 505 | done_with_skb = 0; |
| 506 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 507 | wake_up_interruptible(&session->report_queue); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return done_with_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 514 | static void hidp_recv_ctrl_frame(struct hidp_session *session, |
| 515 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | unsigned char hdr, type, param; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 518 | int free_skb = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | BT_DBG("session %p skb %p len %d", session, skb, skb->len); |
| 521 | |
| 522 | hdr = skb->data[0]; |
| 523 | skb_pull(skb, 1); |
| 524 | |
| 525 | type = hdr & HIDP_HEADER_TRANS_MASK; |
| 526 | param = hdr & HIDP_HEADER_PARAM_MASK; |
| 527 | |
| 528 | switch (type) { |
| 529 | case HIDP_TRANS_HANDSHAKE: |
| 530 | hidp_process_handshake(session, param); |
| 531 | break; |
| 532 | |
| 533 | case HIDP_TRANS_HID_CONTROL: |
| 534 | hidp_process_hid_control(session, param); |
| 535 | break; |
| 536 | |
| 537 | case HIDP_TRANS_DATA: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 538 | free_skb = hidp_process_data(session, skb, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | break; |
| 540 | |
| 541 | default: |
David Herrmann | 41edc0c | 2013-04-06 20:28:50 +0200 | [diff] [blame] | 542 | hidp_send_ctrl_message(session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0); |
| 544 | break; |
| 545 | } |
| 546 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 547 | if (free_skb) |
| 548 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 551 | static void hidp_recv_intr_frame(struct hidp_session *session, |
| 552 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
| 554 | unsigned char hdr; |
| 555 | |
| 556 | BT_DBG("session %p skb %p len %d", session, skb, skb->len); |
| 557 | |
| 558 | hdr = skb->data[0]; |
| 559 | skb_pull(skb, 1); |
| 560 | |
| 561 | if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) { |
| 562 | hidp_set_timer(session); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (session->input) |
| 565 | hidp_input_report(session, skb); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 566 | |
| 567 | if (session->hid) { |
| 568 | hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1); |
| 569 | BT_DBG("report len %d", skb->len); |
| 570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } else { |
| 572 | BT_DBG("Unsupported protocol header 0x%02x", hdr); |
| 573 | } |
| 574 | |
| 575 | kfree_skb(skb); |
| 576 | } |
| 577 | |
| 578 | static int hidp_send_frame(struct socket *sock, unsigned char *data, int len) |
| 579 | { |
| 580 | struct kvec iv = { data, len }; |
| 581 | struct msghdr msg; |
| 582 | |
| 583 | BT_DBG("sock %p data %p len %d", sock, data, len); |
| 584 | |
| 585 | if (!len) |
| 586 | return 0; |
| 587 | |
| 588 | memset(&msg, 0, sizeof(msg)); |
| 589 | |
| 590 | return kernel_sendmsg(sock, &msg, &iv, 1, len); |
| 591 | } |
| 592 | |
David Herrmann | 7350e6c | 2013-04-06 20:28:49 +0200 | [diff] [blame] | 593 | /* dequeue message from @transmit and send via @sock */ |
| 594 | static void hidp_process_transmit(struct hidp_session *session, |
| 595 | struct sk_buff_head *transmit, |
| 596 | struct socket *sock) |
Gustavo F. Padovan | 679344e | 2011-10-06 20:51:37 -0300 | [diff] [blame] | 597 | { |
| 598 | struct sk_buff *skb; |
David Herrmann | 2df0120 | 2013-04-06 20:28:48 +0200 | [diff] [blame] | 599 | int ret; |
Gustavo F. Padovan | 679344e | 2011-10-06 20:51:37 -0300 | [diff] [blame] | 600 | |
| 601 | BT_DBG("session %p", session); |
| 602 | |
David Herrmann | 7350e6c | 2013-04-06 20:28:49 +0200 | [diff] [blame] | 603 | while ((skb = skb_dequeue(transmit))) { |
| 604 | ret = hidp_send_frame(sock, skb->data, skb->len); |
David Herrmann | 2df0120 | 2013-04-06 20:28:48 +0200 | [diff] [blame] | 605 | if (ret == -EAGAIN) { |
David Herrmann | 7350e6c | 2013-04-06 20:28:49 +0200 | [diff] [blame] | 606 | skb_queue_head(transmit, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | break; |
David Herrmann | 2df0120 | 2013-04-06 20:28:48 +0200 | [diff] [blame] | 608 | } else if (ret < 0) { |
| 609 | hidp_session_terminate(session); |
| 610 | kfree_skb(skb); |
| 611 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | hidp_set_timer(session); |
| 615 | kfree_skb(skb); |
| 616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 619 | static int hidp_setup_input(struct hidp_session *session, |
| 620 | struct hidp_connadd_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 622 | struct input_dev *input; |
Gustavo F. Padovan | 3415a5f | 2011-10-06 21:17:32 -0300 | [diff] [blame] | 623 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 625 | input = input_allocate_device(); |
| 626 | if (!input) |
| 627 | return -ENOMEM; |
| 628 | |
| 629 | session->input = input; |
| 630 | |
Marcel Holtmann | 5be3946 | 2007-05-09 09:15:30 +0200 | [diff] [blame] | 631 | input_set_drvdata(input, session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Dmitry Torokhov | 34abf91 | 2005-09-15 02:01:40 -0500 | [diff] [blame] | 633 | input->name = "Bluetooth HID Boot Protocol Device"; |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | input->id.bustype = BUS_BLUETOOTH; |
| 636 | input->id.vendor = req->vendor; |
| 637 | input->id.product = req->product; |
| 638 | input->id.version = req->version; |
| 639 | |
| 640 | if (req->subclass & 0x40) { |
| 641 | set_bit(EV_KEY, input->evbit); |
| 642 | set_bit(EV_LED, input->evbit); |
| 643 | set_bit(EV_REP, input->evbit); |
| 644 | |
| 645 | set_bit(LED_NUML, input->ledbit); |
| 646 | set_bit(LED_CAPSL, input->ledbit); |
| 647 | set_bit(LED_SCROLLL, input->ledbit); |
| 648 | set_bit(LED_COMPOSE, input->ledbit); |
| 649 | set_bit(LED_KANA, input->ledbit); |
| 650 | |
| 651 | for (i = 0; i < sizeof(hidp_keycode); i++) |
| 652 | set_bit(hidp_keycode[i], input->keybit); |
| 653 | clear_bit(0, input->keybit); |
| 654 | } |
| 655 | |
| 656 | if (req->subclass & 0x80) { |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 657 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 658 | input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 659 | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE); |
| 660 | input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 661 | input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) | |
| 662 | BIT_MASK(BTN_EXTRA); |
| 663 | input->relbit[0] |= BIT_MASK(REL_WHEEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 666 | input->dev.parent = &session->conn->hcon->dev; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | input->event = hidp_input_event; |
| 669 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 670 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Marcel Holtmann | f5ffd46 | 2007-02-17 23:58:53 +0100 | [diff] [blame] | 673 | static int hidp_open(struct hid_device *hid) |
| 674 | { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static void hidp_close(struct hid_device *hid) |
| 679 | { |
| 680 | } |
| 681 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 682 | static int hidp_parse(struct hid_device *hid) |
| 683 | { |
| 684 | struct hidp_session *session = hid->driver_data; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 685 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 686 | return hid_parse_report(session->hid, session->rd_data, |
| 687 | session->rd_size); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static int hidp_start(struct hid_device *hid) |
| 691 | { |
| 692 | struct hidp_session *session = hid->driver_data; |
| 693 | struct hid_report *report; |
| 694 | |
David Herrmann | 142c69c | 2011-08-26 13:27:12 +0200 | [diff] [blame] | 695 | if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS) |
| 696 | return 0; |
| 697 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 698 | list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT]. |
| 699 | report_list, list) |
| 700 | hidp_send_report(session, report); |
| 701 | |
| 702 | list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT]. |
| 703 | report_list, list) |
| 704 | hidp_send_report(session, report); |
| 705 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static void hidp_stop(struct hid_device *hid) |
| 710 | { |
| 711 | struct hidp_session *session = hid->driver_data; |
| 712 | |
| 713 | skb_queue_purge(&session->ctrl_transmit); |
| 714 | skb_queue_purge(&session->intr_transmit); |
| 715 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 716 | hid->claimed = 0; |
| 717 | } |
| 718 | |
| 719 | static struct hid_ll_driver hidp_hid_driver = { |
| 720 | .parse = hidp_parse, |
| 721 | .start = hidp_start, |
| 722 | .stop = hidp_stop, |
| 723 | .open = hidp_open, |
| 724 | .close = hidp_close, |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 725 | }; |
| 726 | |
Alan Ott | 0f69dca | 2011-01-18 03:04:37 -0500 | [diff] [blame] | 727 | /* This function sets up the hid device. It does not add it |
| 728 | to the HID system. That is done in hidp_add_connection(). */ |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 729 | static int hidp_setup_hid(struct hidp_session *session, |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 730 | struct hidp_connadd_req *req) |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 731 | { |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 732 | struct hid_device *hid; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 733 | int err; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 734 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 735 | session->rd_data = kzalloc(req->rd_size, GFP_KERNEL); |
| 736 | if (!session->rd_data) |
| 737 | return -ENOMEM; |
| 738 | |
| 739 | if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) { |
| 740 | err = -EFAULT; |
| 741 | goto fault; |
| 742 | } |
| 743 | session->rd_size = req->rd_size; |
| 744 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 745 | hid = hid_allocate_device(); |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 746 | if (IS_ERR(hid)) { |
| 747 | err = PTR_ERR(hid); |
| 748 | goto fault; |
| 749 | } |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 750 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 751 | session->hid = hid; |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 752 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 753 | hid->driver_data = session; |
| 754 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 755 | hid->bus = BUS_BLUETOOTH; |
| 756 | hid->vendor = req->vendor; |
| 757 | hid->product = req->product; |
| 758 | hid->version = req->version; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 759 | hid->country = req->country; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 760 | |
Anderson Lizardo | 0a9ab9b | 2013-01-06 18:28:53 -0400 | [diff] [blame] | 761 | strncpy(hid->name, req->name, sizeof(req->name) - 1); |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 762 | |
| 763 | snprintf(hid->phys, sizeof(hid->phys), "%pMR", |
| 764 | &bt_sk(session->ctrl_sock->sk)->src); |
| 765 | |
| 766 | snprintf(hid->uniq, sizeof(hid->uniq), "%pMR", |
| 767 | &bt_sk(session->ctrl_sock->sk)->dst); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 768 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 769 | hid->dev.parent = &session->conn->hcon->dev; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 770 | hid->ll_driver = &hidp_hid_driver; |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 771 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 772 | hid->hid_get_raw_report = hidp_get_raw_report; |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 773 | hid->hid_output_raw_report = hidp_output_raw_report; |
| 774 | |
Lamarque V. Souza | 4529eef | 2012-12-06 12:39:55 -0200 | [diff] [blame] | 775 | /* True if device is blacklisted in drivers/hid/hid-core.c */ |
| 776 | if (hid_ignore(hid)) { |
| 777 | hid_destroy_device(session->hid); |
| 778 | session->hid = NULL; |
| 779 | return -ENODEV; |
| 780 | } |
| 781 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 782 | return 0; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 783 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 784 | fault: |
| 785 | kfree(session->rd_data); |
| 786 | session->rd_data = NULL; |
| 787 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 788 | return err; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 789 | } |
| 790 | |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 791 | /* initialize session devices */ |
| 792 | static int hidp_session_dev_init(struct hidp_session *session, |
| 793 | struct hidp_connadd_req *req) |
| 794 | { |
| 795 | int ret; |
| 796 | |
| 797 | if (req->rd_size > 0) { |
| 798 | ret = hidp_setup_hid(session, req); |
| 799 | if (ret && ret != -ENODEV) |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | if (!session->hid) { |
| 804 | ret = hidp_setup_input(session, req); |
| 805 | if (ret < 0) |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* destroy session devices */ |
| 813 | static void hidp_session_dev_destroy(struct hidp_session *session) |
| 814 | { |
| 815 | if (session->hid) |
| 816 | put_device(&session->hid->dev); |
| 817 | else if (session->input) |
| 818 | input_put_device(session->input); |
| 819 | |
| 820 | kfree(session->rd_data); |
| 821 | session->rd_data = NULL; |
| 822 | } |
| 823 | |
| 824 | /* add HID/input devices to their underlying bus systems */ |
| 825 | static int hidp_session_dev_add(struct hidp_session *session) |
| 826 | { |
| 827 | int ret; |
| 828 | |
| 829 | /* Both HID and input systems drop a ref-count when unregistering the |
| 830 | * device but they don't take a ref-count when registering them. Work |
| 831 | * around this by explicitly taking a refcount during registration |
| 832 | * which is dropped automatically by unregistering the devices. */ |
| 833 | |
| 834 | if (session->hid) { |
| 835 | ret = hid_add_device(session->hid); |
| 836 | if (ret) |
| 837 | return ret; |
| 838 | get_device(&session->hid->dev); |
| 839 | } else if (session->input) { |
| 840 | ret = input_register_device(session->input); |
| 841 | if (ret) |
| 842 | return ret; |
| 843 | input_get_device(session->input); |
| 844 | } |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* remove HID/input devices from their bus systems */ |
| 850 | static void hidp_session_dev_del(struct hidp_session *session) |
| 851 | { |
| 852 | if (session->hid) |
| 853 | hid_destroy_device(session->hid); |
| 854 | else if (session->input) |
| 855 | input_unregister_device(session->input); |
| 856 | } |
| 857 | |
| 858 | /* |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 859 | * Asynchronous device registration |
| 860 | * HID device drivers might want to perform I/O during initialization to |
| 861 | * detect device types. Therefore, call device registration in a separate |
| 862 | * worker so the HIDP thread can schedule I/O operations. |
| 863 | * Note that this must be called after the worker thread was initialized |
| 864 | * successfully. This will then add the devices and increase session state |
| 865 | * on success, otherwise it will terminate the session thread. |
| 866 | */ |
| 867 | static void hidp_session_dev_work(struct work_struct *work) |
| 868 | { |
| 869 | struct hidp_session *session = container_of(work, |
| 870 | struct hidp_session, |
| 871 | dev_init); |
| 872 | int ret; |
| 873 | |
| 874 | ret = hidp_session_dev_add(session); |
| 875 | if (!ret) |
| 876 | atomic_inc(&session->state); |
| 877 | else |
| 878 | hidp_session_terminate(session); |
| 879 | } |
| 880 | |
| 881 | /* |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 882 | * Create new session object |
| 883 | * Allocate session object, initialize static fields, copy input data into the |
| 884 | * object and take a reference to all sub-objects. |
| 885 | * This returns 0 on success and puts a pointer to the new session object in |
| 886 | * \out. Otherwise, an error code is returned. |
| 887 | * The new session object has an initial ref-count of 1. |
| 888 | */ |
| 889 | static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr, |
| 890 | struct socket *ctrl_sock, |
| 891 | struct socket *intr_sock, |
| 892 | struct hidp_connadd_req *req, |
| 893 | struct l2cap_conn *conn) |
| 894 | { |
| 895 | struct hidp_session *session; |
| 896 | int ret; |
| 897 | struct bt_sock *ctrl, *intr; |
| 898 | |
| 899 | ctrl = bt_sk(ctrl_sock->sk); |
| 900 | intr = bt_sk(intr_sock->sk); |
| 901 | |
| 902 | session = kzalloc(sizeof(*session), GFP_KERNEL); |
| 903 | if (!session) |
| 904 | return -ENOMEM; |
| 905 | |
| 906 | /* object and runtime management */ |
| 907 | kref_init(&session->ref); |
| 908 | atomic_set(&session->state, HIDP_SESSION_IDLING); |
| 909 | init_waitqueue_head(&session->state_queue); |
| 910 | session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID); |
| 911 | |
| 912 | /* connection management */ |
| 913 | bacpy(&session->bdaddr, bdaddr); |
| 914 | session->conn = conn; |
| 915 | session->user.probe = hidp_session_probe; |
| 916 | session->user.remove = hidp_session_remove; |
| 917 | session->ctrl_sock = ctrl_sock; |
| 918 | session->intr_sock = intr_sock; |
| 919 | skb_queue_head_init(&session->ctrl_transmit); |
| 920 | skb_queue_head_init(&session->intr_transmit); |
| 921 | session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl)->chan->omtu, |
| 922 | l2cap_pi(ctrl)->chan->imtu); |
| 923 | session->intr_mtu = min_t(uint, l2cap_pi(intr)->chan->omtu, |
| 924 | l2cap_pi(intr)->chan->imtu); |
| 925 | session->idle_to = req->idle_to; |
| 926 | |
| 927 | /* device management */ |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 928 | INIT_WORK(&session->dev_init, hidp_session_dev_work); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 929 | setup_timer(&session->timer, hidp_idle_timeout, |
| 930 | (unsigned long)session); |
| 931 | |
| 932 | /* session data */ |
| 933 | mutex_init(&session->report_mutex); |
| 934 | init_waitqueue_head(&session->report_queue); |
| 935 | |
| 936 | ret = hidp_session_dev_init(session, req); |
| 937 | if (ret) |
| 938 | goto err_free; |
| 939 | |
| 940 | l2cap_conn_get(session->conn); |
| 941 | get_file(session->intr_sock->file); |
| 942 | get_file(session->ctrl_sock->file); |
| 943 | *out = session; |
| 944 | return 0; |
| 945 | |
| 946 | err_free: |
| 947 | kfree(session); |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | /* increase ref-count of the given session by one */ |
| 952 | static void hidp_session_get(struct hidp_session *session) |
| 953 | { |
| 954 | kref_get(&session->ref); |
| 955 | } |
| 956 | |
| 957 | /* release callback */ |
| 958 | static void session_free(struct kref *ref) |
| 959 | { |
| 960 | struct hidp_session *session = container_of(ref, struct hidp_session, |
| 961 | ref); |
| 962 | |
| 963 | hidp_session_dev_destroy(session); |
| 964 | skb_queue_purge(&session->ctrl_transmit); |
| 965 | skb_queue_purge(&session->intr_transmit); |
| 966 | fput(session->intr_sock->file); |
| 967 | fput(session->ctrl_sock->file); |
| 968 | l2cap_conn_put(session->conn); |
| 969 | kfree(session); |
| 970 | } |
| 971 | |
| 972 | /* decrease ref-count of the given session by one */ |
| 973 | static void hidp_session_put(struct hidp_session *session) |
| 974 | { |
| 975 | kref_put(&session->ref, session_free); |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Search the list of active sessions for a session with target address |
| 980 | * \bdaddr. You must hold at least a read-lock on \hidp_session_sem. As long as |
| 981 | * you do not release this lock, the session objects cannot vanish and you can |
| 982 | * safely take a reference to the session yourself. |
| 983 | */ |
| 984 | static struct hidp_session *__hidp_session_find(const bdaddr_t *bdaddr) |
| 985 | { |
| 986 | struct hidp_session *session; |
| 987 | |
| 988 | list_for_each_entry(session, &hidp_session_list, list) { |
| 989 | if (!bacmp(bdaddr, &session->bdaddr)) |
| 990 | return session; |
| 991 | } |
| 992 | |
| 993 | return NULL; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Same as __hidp_session_find() but no locks must be held. This also takes a |
| 998 | * reference of the returned session (if non-NULL) so you must drop this |
| 999 | * reference if you no longer use the object. |
| 1000 | */ |
| 1001 | static struct hidp_session *hidp_session_find(const bdaddr_t *bdaddr) |
| 1002 | { |
| 1003 | struct hidp_session *session; |
| 1004 | |
| 1005 | down_read(&hidp_session_sem); |
| 1006 | |
| 1007 | session = __hidp_session_find(bdaddr); |
| 1008 | if (session) |
| 1009 | hidp_session_get(session); |
| 1010 | |
| 1011 | up_read(&hidp_session_sem); |
| 1012 | |
| 1013 | return session; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Start session synchronously |
| 1018 | * This starts a session thread and waits until initialization |
| 1019 | * is done or returns an error if it couldn't be started. |
| 1020 | * If this returns 0 the session thread is up and running. You must call |
| 1021 | * hipd_session_stop_sync() before deleting any runtime resources. |
| 1022 | */ |
| 1023 | static int hidp_session_start_sync(struct hidp_session *session) |
| 1024 | { |
| 1025 | unsigned int vendor, product; |
| 1026 | |
| 1027 | if (session->hid) { |
| 1028 | vendor = session->hid->vendor; |
| 1029 | product = session->hid->product; |
| 1030 | } else if (session->input) { |
| 1031 | vendor = session->input->id.vendor; |
| 1032 | product = session->input->id.product; |
| 1033 | } else { |
| 1034 | vendor = 0x0000; |
| 1035 | product = 0x0000; |
| 1036 | } |
| 1037 | |
| 1038 | session->task = kthread_run(hidp_session_thread, session, |
| 1039 | "khidpd_%04x%04x", vendor, product); |
| 1040 | if (IS_ERR(session->task)) |
| 1041 | return PTR_ERR(session->task); |
| 1042 | |
| 1043 | while (atomic_read(&session->state) <= HIDP_SESSION_IDLING) |
| 1044 | wait_event(session->state_queue, |
| 1045 | atomic_read(&session->state) > HIDP_SESSION_IDLING); |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Terminate session thread |
| 1052 | * Wake up session thread and notify it to stop. This is asynchronous and |
| 1053 | * returns immediately. Call this whenever a runtime error occurs and you want |
| 1054 | * the session to stop. |
| 1055 | * Note: wake_up_process() performs any necessary memory-barriers for us. |
| 1056 | */ |
| 1057 | static void hidp_session_terminate(struct hidp_session *session) |
| 1058 | { |
| 1059 | atomic_inc(&session->terminate); |
| 1060 | wake_up_process(session->task); |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Probe HIDP session |
| 1065 | * This is called from the l2cap_conn core when our l2cap_user object is bound |
| 1066 | * to the hci-connection. We get the session via the \user object and can now |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1067 | * start the session thread, link it into the global session list and |
| 1068 | * schedule HID/input device registration. |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1069 | * The global session-list owns its own reference to the session object so you |
| 1070 | * can drop your own reference after registering the l2cap_user object. |
| 1071 | */ |
| 1072 | static int hidp_session_probe(struct l2cap_conn *conn, |
| 1073 | struct l2cap_user *user) |
| 1074 | { |
| 1075 | struct hidp_session *session = container_of(user, |
| 1076 | struct hidp_session, |
| 1077 | user); |
| 1078 | struct hidp_session *s; |
| 1079 | int ret; |
| 1080 | |
| 1081 | down_write(&hidp_session_sem); |
| 1082 | |
| 1083 | /* check that no other session for this device exists */ |
| 1084 | s = __hidp_session_find(&session->bdaddr); |
| 1085 | if (s) { |
| 1086 | ret = -EEXIST; |
| 1087 | goto out_unlock; |
| 1088 | } |
| 1089 | |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1090 | if (session->input) { |
| 1091 | ret = hidp_session_dev_add(session); |
| 1092 | if (ret) |
| 1093 | goto out_unlock; |
| 1094 | } |
| 1095 | |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1096 | ret = hidp_session_start_sync(session); |
| 1097 | if (ret) |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1098 | goto out_del; |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1099 | |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1100 | /* HID device registration is async to allow I/O during probe */ |
| 1101 | if (session->input) |
| 1102 | atomic_inc(&session->state); |
| 1103 | else |
| 1104 | schedule_work(&session->dev_init); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1105 | |
| 1106 | hidp_session_get(session); |
| 1107 | list_add(&session->list, &hidp_session_list); |
| 1108 | ret = 0; |
| 1109 | goto out_unlock; |
| 1110 | |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1111 | out_del: |
| 1112 | if (session->input) |
| 1113 | hidp_session_dev_del(session); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1114 | out_unlock: |
| 1115 | up_write(&hidp_session_sem); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Remove HIDP session |
| 1121 | * Called from the l2cap_conn core when either we explicitly unregistered |
| 1122 | * the l2cap_user object or if the underlying connection is shut down. |
| 1123 | * We signal the hidp-session thread to shut down, unregister the HID/input |
| 1124 | * devices and unlink the session from the global list. |
| 1125 | * This drops the reference to the session that is owned by the global |
| 1126 | * session-list. |
| 1127 | * Note: We _must_ not synchronosly wait for the session-thread to shut down. |
| 1128 | * This is, because the session-thread might be waiting for an HCI lock that is |
| 1129 | * held while we are called. Therefore, we only unregister the devices and |
| 1130 | * notify the session-thread to terminate. The thread itself owns a reference |
| 1131 | * to the session object so it can safely shut down. |
| 1132 | */ |
| 1133 | static void hidp_session_remove(struct l2cap_conn *conn, |
| 1134 | struct l2cap_user *user) |
| 1135 | { |
| 1136 | struct hidp_session *session = container_of(user, |
| 1137 | struct hidp_session, |
| 1138 | user); |
| 1139 | |
| 1140 | down_write(&hidp_session_sem); |
| 1141 | |
| 1142 | hidp_session_terminate(session); |
David Herrmann | 4e713cd | 2013-05-23 13:10:25 +0200 | [diff] [blame] | 1143 | |
| 1144 | cancel_work_sync(&session->dev_init); |
| 1145 | if (session->input || |
| 1146 | atomic_read(&session->state) > HIDP_SESSION_PREPARING) |
| 1147 | hidp_session_dev_del(session); |
| 1148 | |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1149 | list_del(&session->list); |
| 1150 | |
| 1151 | up_write(&hidp_session_sem); |
| 1152 | |
| 1153 | hidp_session_put(session); |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Session Worker |
| 1158 | * This performs the actual main-loop of the HIDP worker. We first check |
| 1159 | * whether the underlying connection is still alive, then parse all pending |
| 1160 | * messages and finally send all outstanding messages. |
| 1161 | */ |
| 1162 | static void hidp_session_run(struct hidp_session *session) |
| 1163 | { |
| 1164 | struct sock *ctrl_sk = session->ctrl_sock->sk; |
| 1165 | struct sock *intr_sk = session->intr_sock->sk; |
| 1166 | struct sk_buff *skb; |
| 1167 | |
| 1168 | for (;;) { |
| 1169 | /* |
| 1170 | * This thread can be woken up two ways: |
| 1171 | * - You call hidp_session_terminate() which sets the |
| 1172 | * session->terminate flag and wakes this thread up. |
| 1173 | * - Via modifying the socket state of ctrl/intr_sock. This |
| 1174 | * thread is woken up by ->sk_state_changed(). |
| 1175 | * |
| 1176 | * Note: set_current_state() performs any necessary |
| 1177 | * memory-barriers for us. |
| 1178 | */ |
| 1179 | set_current_state(TASK_INTERRUPTIBLE); |
| 1180 | |
| 1181 | if (atomic_read(&session->terminate)) |
| 1182 | break; |
| 1183 | |
| 1184 | if (ctrl_sk->sk_state != BT_CONNECTED || |
| 1185 | intr_sk->sk_state != BT_CONNECTED) |
| 1186 | break; |
| 1187 | |
| 1188 | /* parse incoming intr-skbs */ |
| 1189 | while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) { |
| 1190 | skb_orphan(skb); |
| 1191 | if (!skb_linearize(skb)) |
| 1192 | hidp_recv_intr_frame(session, skb); |
| 1193 | else |
| 1194 | kfree_skb(skb); |
| 1195 | } |
| 1196 | |
| 1197 | /* send pending intr-skbs */ |
David Herrmann | 7350e6c | 2013-04-06 20:28:49 +0200 | [diff] [blame] | 1198 | hidp_process_transmit(session, &session->intr_transmit, |
| 1199 | session->intr_sock); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1200 | |
| 1201 | /* parse incoming ctrl-skbs */ |
| 1202 | while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) { |
| 1203 | skb_orphan(skb); |
| 1204 | if (!skb_linearize(skb)) |
| 1205 | hidp_recv_ctrl_frame(session, skb); |
| 1206 | else |
| 1207 | kfree_skb(skb); |
| 1208 | } |
| 1209 | |
| 1210 | /* send pending ctrl-skbs */ |
David Herrmann | 7350e6c | 2013-04-06 20:28:49 +0200 | [diff] [blame] | 1211 | hidp_process_transmit(session, &session->ctrl_transmit, |
| 1212 | session->ctrl_sock); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1213 | |
| 1214 | schedule(); |
| 1215 | } |
| 1216 | |
| 1217 | atomic_inc(&session->terminate); |
| 1218 | set_current_state(TASK_RUNNING); |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * HIDP session thread |
| 1223 | * This thread runs the I/O for a single HIDP session. Startup is synchronous |
| 1224 | * which allows us to take references to ourself here instead of doing that in |
| 1225 | * the caller. |
| 1226 | * When we are ready to run we notify the caller and call hidp_session_run(). |
| 1227 | */ |
| 1228 | static int hidp_session_thread(void *arg) |
| 1229 | { |
| 1230 | struct hidp_session *session = arg; |
| 1231 | wait_queue_t ctrl_wait, intr_wait; |
| 1232 | |
| 1233 | BT_DBG("session %p", session); |
| 1234 | |
| 1235 | /* initialize runtime environment */ |
| 1236 | hidp_session_get(session); |
| 1237 | __module_get(THIS_MODULE); |
| 1238 | set_user_nice(current, -15); |
| 1239 | hidp_set_timer(session); |
| 1240 | |
| 1241 | init_waitqueue_entry(&ctrl_wait, current); |
| 1242 | init_waitqueue_entry(&intr_wait, current); |
| 1243 | add_wait_queue(sk_sleep(session->ctrl_sock->sk), &ctrl_wait); |
| 1244 | add_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait); |
| 1245 | /* This memory barrier is paired with wq_has_sleeper(). See |
| 1246 | * sock_poll_wait() for more information why this is needed. */ |
| 1247 | smp_mb(); |
| 1248 | |
| 1249 | /* notify synchronous startup that we're ready */ |
| 1250 | atomic_inc(&session->state); |
| 1251 | wake_up(&session->state_queue); |
| 1252 | |
| 1253 | /* run session */ |
| 1254 | hidp_session_run(session); |
| 1255 | |
| 1256 | /* cleanup runtime environment */ |
| 1257 | remove_wait_queue(sk_sleep(session->intr_sock->sk), &intr_wait); |
| 1258 | remove_wait_queue(sk_sleep(session->intr_sock->sk), &ctrl_wait); |
| 1259 | wake_up_interruptible(&session->report_queue); |
| 1260 | hidp_del_timer(session); |
| 1261 | |
| 1262 | /* |
| 1263 | * If we stopped ourself due to any internal signal, we should try to |
| 1264 | * unregister our own session here to avoid having it linger until the |
| 1265 | * parent l2cap_conn dies or user-space cleans it up. |
| 1266 | * This does not deadlock as we don't do any synchronous shutdown. |
| 1267 | * Instead, this call has the same semantics as if user-space tried to |
| 1268 | * delete the session. |
| 1269 | */ |
| 1270 | l2cap_unregister_user(session->conn, &session->user); |
| 1271 | hidp_session_put(session); |
| 1272 | |
| 1273 | module_put_and_exit(0); |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int hidp_verify_sockets(struct socket *ctrl_sock, |
| 1278 | struct socket *intr_sock) |
| 1279 | { |
| 1280 | struct bt_sock *ctrl, *intr; |
| 1281 | struct hidp_session *session; |
| 1282 | |
| 1283 | if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock)) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | ctrl = bt_sk(ctrl_sock->sk); |
| 1287 | intr = bt_sk(intr_sock->sk); |
| 1288 | |
| 1289 | if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst)) |
| 1290 | return -ENOTUNIQ; |
| 1291 | if (ctrl->sk.sk_state != BT_CONNECTED || |
| 1292 | intr->sk.sk_state != BT_CONNECTED) |
| 1293 | return -EBADFD; |
| 1294 | |
| 1295 | /* early session check, we check again during session registration */ |
| 1296 | session = hidp_session_find(&ctrl->dst); |
| 1297 | if (session) { |
| 1298 | hidp_session_put(session); |
| 1299 | return -EEXIST; |
| 1300 | } |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | int hidp_connection_add(struct hidp_connadd_req *req, |
| 1306 | struct socket *ctrl_sock, |
| 1307 | struct socket *intr_sock) |
| 1308 | { |
| 1309 | struct hidp_session *session; |
| 1310 | struct l2cap_conn *conn; |
| 1311 | struct l2cap_chan *chan = l2cap_pi(ctrl_sock->sk)->chan; |
| 1312 | int ret; |
| 1313 | |
| 1314 | ret = hidp_verify_sockets(ctrl_sock, intr_sock); |
| 1315 | if (ret) |
| 1316 | return ret; |
| 1317 | |
| 1318 | conn = NULL; |
| 1319 | l2cap_chan_lock(chan); |
| 1320 | if (chan->conn) { |
| 1321 | l2cap_conn_get(chan->conn); |
| 1322 | conn = chan->conn; |
| 1323 | } |
| 1324 | l2cap_chan_unlock(chan); |
| 1325 | |
| 1326 | if (!conn) |
| 1327 | return -EBADFD; |
| 1328 | |
| 1329 | ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock, |
| 1330 | intr_sock, req, conn); |
| 1331 | if (ret) |
| 1332 | goto out_conn; |
| 1333 | |
| 1334 | ret = l2cap_register_user(conn, &session->user); |
| 1335 | if (ret) |
| 1336 | goto out_session; |
| 1337 | |
| 1338 | ret = 0; |
| 1339 | |
| 1340 | out_session: |
| 1341 | hidp_session_put(session); |
| 1342 | out_conn: |
| 1343 | l2cap_conn_put(conn); |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | int hidp_connection_del(struct hidp_conndel_req *req) |
| 1348 | { |
| 1349 | struct hidp_session *session; |
| 1350 | |
| 1351 | session = hidp_session_find(&req->bdaddr); |
| 1352 | if (!session) |
| 1353 | return -ENOENT; |
| 1354 | |
| 1355 | if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) |
| 1356 | hidp_send_ctrl_message(session, |
| 1357 | HIDP_TRANS_HID_CONTROL | |
| 1358 | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, |
| 1359 | NULL, 0); |
| 1360 | else |
| 1361 | l2cap_unregister_user(session->conn, &session->user); |
| 1362 | |
| 1363 | hidp_session_put(session); |
| 1364 | |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | int hidp_get_connlist(struct hidp_connlist_req *req) |
| 1369 | { |
Gustavo F. Padovan | cd11cdd | 2011-10-06 17:35:31 -0300 | [diff] [blame] | 1370 | struct hidp_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | int err = 0, n = 0; |
| 1372 | |
| 1373 | BT_DBG(""); |
| 1374 | |
| 1375 | down_read(&hidp_session_sem); |
| 1376 | |
Gustavo F. Padovan | cd11cdd | 2011-10-06 17:35:31 -0300 | [diff] [blame] | 1377 | list_for_each_entry(session, &hidp_session_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | struct hidp_conninfo ci; |
| 1379 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 1380 | hidp_copy_session(session, &ci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
| 1382 | if (copy_to_user(req->ci, &ci, sizeof(ci))) { |
| 1383 | err = -EFAULT; |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | if (++n >= req->cnum) |
| 1388 | break; |
| 1389 | |
| 1390 | req->ci++; |
| 1391 | } |
| 1392 | req->cnum = n; |
| 1393 | |
| 1394 | up_read(&hidp_session_sem); |
| 1395 | return err; |
| 1396 | } |
| 1397 | |
| 1398 | int hidp_get_conninfo(struct hidp_conninfo *ci) |
| 1399 | { |
| 1400 | struct hidp_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 1402 | session = hidp_session_find(&ci->bdaddr); |
| 1403 | if (session) { |
| 1404 | hidp_copy_session(session, ci); |
| 1405 | hidp_session_put(session); |
| 1406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
David Herrmann | 5205185 | 2013-04-06 20:28:47 +0200 | [diff] [blame] | 1408 | return session ? 0 : -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static int __init hidp_init(void) |
| 1412 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION); |
| 1414 | |
Henrik Rydberg | 8215d55 | 2012-04-23 12:07:07 +0200 | [diff] [blame] | 1415 | return hidp_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static void __exit hidp_exit(void) |
| 1419 | { |
| 1420 | hidp_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | module_init(hidp_init); |
| 1424 | module_exit(hidp_exit); |
| 1425 | |
| 1426 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
David Herrmann | b4f34d8 | 2013-04-06 20:28:46 +0200 | [diff] [blame] | 1427 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION); |
| 1429 | MODULE_VERSION(VERSION); |
| 1430 | MODULE_LICENSE("GPL"); |
| 1431 | MODULE_ALIAS("bt-proto-6"); |