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> |
| 4 | |
| 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; |
| 8 | |
| 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/sched.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/poll.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 31 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Szymon Janc | aabf6f8 | 2011-04-05 15:37:45 +0200 | [diff] [blame] | 40 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/sock.h> |
| 42 | |
| 43 | #include <linux/input.h> |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 44 | #include <linux/hid.h> |
Marcel Holtmann | 364f635 | 2009-08-22 14:15:53 -0700 | [diff] [blame] | 45 | #include <linux/hidraw.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include <net/bluetooth/bluetooth.h> |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 48 | #include <net/bluetooth/hci_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/bluetooth/l2cap.h> |
| 50 | |
| 51 | #include "hidp.h" |
| 52 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 53 | #define VERSION "1.2" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | static DECLARE_RWSEM(hidp_session_sem); |
| 56 | static LIST_HEAD(hidp_session_list); |
| 57 | |
| 58 | static unsigned char hidp_keycode[256] = { |
Szymon Janc | 17f09a7 | 2011-03-21 14:20:01 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; |
| 80 | |
| 81 | static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr) |
| 82 | { |
| 83 | struct hidp_session *session; |
| 84 | struct list_head *p; |
| 85 | |
| 86 | BT_DBG(""); |
| 87 | |
| 88 | list_for_each(p, &hidp_session_list) { |
| 89 | session = list_entry(p, struct hidp_session, list); |
| 90 | if (!bacmp(bdaddr, &session->bdaddr)) |
| 91 | return session; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static void __hidp_link_session(struct hidp_session *session) |
| 97 | { |
| 98 | __module_get(THIS_MODULE); |
| 99 | list_add(&session->list, &hidp_session_list); |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 100 | |
| 101 | hci_conn_hold_device(session->conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void __hidp_unlink_session(struct hidp_session *session) |
| 105 | { |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 106 | hci_conn_put_device(session->conn); |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | list_del(&session->list); |
| 109 | module_put(THIS_MODULE); |
| 110 | } |
| 111 | |
| 112 | static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci) |
| 113 | { |
Vasiliy Kulikov | d31dbf6 | 2010-10-30 18:26:31 +0400 | [diff] [blame] | 114 | memset(ci, 0, sizeof(*ci)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | bacpy(&ci->bdaddr, &session->bdaddr); |
| 116 | |
| 117 | ci->flags = session->flags; |
| 118 | ci->state = session->state; |
| 119 | |
| 120 | ci->vendor = 0x0000; |
| 121 | ci->product = 0x0000; |
| 122 | ci->version = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | if (session->input) { |
| 125 | ci->vendor = session->input->id.vendor; |
| 126 | ci->product = session->input->id.product; |
| 127 | ci->version = session->input->id.version; |
| 128 | if (session->input->name) |
| 129 | strncpy(ci->name, session->input->name, 128); |
| 130 | else |
| 131 | strncpy(ci->name, "HID Boot Device", 128); |
| 132 | } |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 133 | |
| 134 | if (session->hid) { |
| 135 | ci->vendor = session->hid->vendor; |
| 136 | ci->product = session->hid->product; |
| 137 | ci->version = session->hid->version; |
| 138 | strncpy(ci->name, session->hid->name, 128); |
| 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 142 | static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev, |
| 143 | unsigned int type, unsigned int code, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | unsigned char newleds; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 146 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 148 | BT_DBG("session %p type %d code %d value %d", session, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | if (type != EV_LED) |
| 151 | return -1; |
| 152 | |
| 153 | newleds = (!!test_bit(LED_KANA, dev->led) << 3) | |
| 154 | (!!test_bit(LED_COMPOSE, dev->led) << 3) | |
| 155 | (!!test_bit(LED_SCROLLL, dev->led) << 2) | |
| 156 | (!!test_bit(LED_CAPSL, dev->led) << 1) | |
| 157 | (!!test_bit(LED_NUML, dev->led)); |
| 158 | |
| 159 | if (session->leds == newleds) |
| 160 | return 0; |
| 161 | |
| 162 | session->leds = newleds; |
| 163 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 164 | skb = alloc_skb(3, GFP_ATOMIC); |
| 165 | if (!skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | BT_ERR("Can't allocate memory for new frame"); |
| 167 | return -ENOMEM; |
| 168 | } |
| 169 | |
| 170 | *skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; |
| 171 | *skb_put(skb, 1) = 0x01; |
| 172 | *skb_put(skb, 1) = newleds; |
| 173 | |
| 174 | skb_queue_tail(&session->intr_transmit, skb); |
| 175 | |
| 176 | hidp_schedule(session); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 181 | static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 182 | { |
Marcel Holtmann | 5be3946 | 2007-05-09 09:15:30 +0200 | [diff] [blame] | 183 | struct hid_device *hid = input_get_drvdata(dev); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 184 | struct hidp_session *session = hid->driver_data; |
| 185 | |
| 186 | return hidp_queue_event(session, dev, type, code, value); |
| 187 | } |
| 188 | |
| 189 | static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 190 | { |
Marcel Holtmann | 5be3946 | 2007-05-09 09:15:30 +0200 | [diff] [blame] | 191 | struct hidp_session *session = input_get_drvdata(dev); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 192 | |
| 193 | return hidp_queue_event(session, dev, type, code, value); |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb) |
| 197 | { |
| 198 | struct input_dev *dev = session->input; |
| 199 | unsigned char *keys = session->keys; |
| 200 | unsigned char *udata = skb->data + 1; |
| 201 | signed char *sdata = skb->data + 1; |
| 202 | int i, size = skb->len - 1; |
| 203 | |
| 204 | switch (skb->data[0]) { |
| 205 | case 0x01: /* Keyboard report */ |
| 206 | for (i = 0; i < 8; i++) |
| 207 | input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1); |
| 208 | |
| 209 | /* If all the key codes have been set to 0x01, it means |
| 210 | * too many keys were pressed at the same time. */ |
| 211 | if (!memcmp(udata + 2, hidp_mkeyspat, 6)) |
| 212 | break; |
| 213 | |
| 214 | for (i = 2; i < 8; i++) { |
| 215 | if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) { |
| 216 | if (hidp_keycode[keys[i]]) |
| 217 | input_report_key(dev, hidp_keycode[keys[i]], 0); |
| 218 | else |
| 219 | BT_ERR("Unknown key (scancode %#x) released.", keys[i]); |
| 220 | } |
| 221 | |
| 222 | if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) { |
| 223 | if (hidp_keycode[udata[i]]) |
| 224 | input_report_key(dev, hidp_keycode[udata[i]], 1); |
| 225 | else |
| 226 | BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | memcpy(keys, udata, 8); |
| 231 | break; |
| 232 | |
| 233 | case 0x02: /* Mouse report */ |
| 234 | input_report_key(dev, BTN_LEFT, sdata[0] & 0x01); |
| 235 | input_report_key(dev, BTN_RIGHT, sdata[0] & 0x02); |
| 236 | input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04); |
| 237 | input_report_key(dev, BTN_SIDE, sdata[0] & 0x08); |
| 238 | input_report_key(dev, BTN_EXTRA, sdata[0] & 0x10); |
| 239 | |
| 240 | input_report_rel(dev, REL_X, sdata[1]); |
| 241 | input_report_rel(dev, REL_Y, sdata[2]); |
| 242 | |
| 243 | if (size > 3) |
| 244 | input_report_rel(dev, REL_WHEEL, sdata[3]); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | input_sync(dev); |
| 249 | } |
| 250 | |
Bastien Nocera | 6bf8268 | 2010-01-20 12:00:42 +0000 | [diff] [blame] | 251 | static int __hidp_send_ctrl_message(struct hidp_session *session, |
| 252 | unsigned char hdr, unsigned char *data, int size) |
| 253 | { |
| 254 | struct sk_buff *skb; |
| 255 | |
| 256 | BT_DBG("session %p data %p size %d", session, data, size); |
| 257 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 258 | skb = alloc_skb(size + 1, GFP_ATOMIC); |
| 259 | if (!skb) { |
Bastien Nocera | 6bf8268 | 2010-01-20 12:00:42 +0000 | [diff] [blame] | 260 | BT_ERR("Can't allocate memory for new frame"); |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
| 264 | *skb_put(skb, 1) = hdr; |
| 265 | if (data && size > 0) |
| 266 | memcpy(skb_put(skb, size), data, size); |
| 267 | |
| 268 | skb_queue_tail(&session->ctrl_transmit, skb); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static inline int hidp_send_ctrl_message(struct hidp_session *session, |
| 274 | unsigned char hdr, unsigned char *data, int size) |
| 275 | { |
| 276 | int err; |
| 277 | |
| 278 | err = __hidp_send_ctrl_message(session, hdr, data, size); |
| 279 | |
| 280 | hidp_schedule(session); |
| 281 | |
| 282 | return err; |
| 283 | } |
| 284 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 285 | static int hidp_queue_report(struct hidp_session *session, |
| 286 | unsigned char *data, int size) |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 287 | { |
| 288 | struct sk_buff *skb; |
| 289 | |
Dave Young | 6792b5e | 2007-10-20 14:15:39 +0200 | [diff] [blame] | 290 | BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 291 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 292 | skb = alloc_skb(size + 1, GFP_ATOMIC); |
| 293 | if (!skb) { |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 294 | BT_ERR("Can't allocate memory for new frame"); |
| 295 | return -ENOMEM; |
| 296 | } |
| 297 | |
| 298 | *skb_put(skb, 1) = 0xa2; |
| 299 | if (size > 0) |
| 300 | memcpy(skb_put(skb, size), data, size); |
| 301 | |
| 302 | skb_queue_tail(&session->intr_transmit, skb); |
| 303 | |
| 304 | hidp_schedule(session); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int hidp_send_report(struct hidp_session *session, struct hid_report *report) |
| 310 | { |
| 311 | unsigned char buf[32]; |
| 312 | int rsize; |
| 313 | |
| 314 | rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
| 315 | if (rsize > sizeof(buf)) |
| 316 | return -EIO; |
| 317 | |
| 318 | hid_output_report(report, buf); |
| 319 | |
| 320 | return hidp_queue_report(session, buf, rsize); |
| 321 | } |
| 322 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 323 | static int hidp_get_raw_report(struct hid_device *hid, |
| 324 | unsigned char report_number, |
| 325 | unsigned char *data, size_t count, |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 326 | unsigned char report_type) |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 327 | { |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 328 | struct hidp_session *session = hid->driver_data; |
| 329 | struct sk_buff *skb; |
| 330 | size_t len; |
| 331 | int numbered_reports = hid->report_enum[report_type].numbered; |
| 332 | |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 333 | switch (report_type) { |
| 334 | case HID_FEATURE_REPORT: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 335 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE; |
| 336 | break; |
| 337 | case HID_INPUT_REPORT: |
| 338 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT; |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 339 | break; |
| 340 | case HID_OUTPUT_REPORT: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 341 | report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT; |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 347 | if (mutex_lock_interruptible(&session->report_mutex)) |
| 348 | return -ERESTARTSYS; |
| 349 | |
| 350 | /* Set up our wait, and send the report request to the device. */ |
| 351 | session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK; |
| 352 | session->waiting_report_number = numbered_reports ? report_number : -1; |
| 353 | set_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 354 | data[0] = report_number; |
| 355 | if (hidp_send_ctrl_message(hid->driver_data, report_type, data, 1)) |
| 356 | goto err_eio; |
| 357 | |
| 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)) { |
| 361 | int res; |
| 362 | |
| 363 | res = wait_event_interruptible_timeout(session->report_queue, |
| 364 | !test_bit(HIDP_WAITING_FOR_RETURN, &session->flags), |
| 365 | 5*HZ); |
| 366 | if (res == 0) { |
| 367 | /* timeout */ |
| 368 | goto err_eio; |
| 369 | } |
| 370 | if (res < 0) { |
| 371 | /* signal */ |
| 372 | goto err_restartsys; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | skb = session->report_return; |
| 377 | if (skb) { |
| 378 | len = skb->len < count ? skb->len : count; |
| 379 | memcpy(data, skb->data, len); |
| 380 | |
| 381 | kfree_skb(skb); |
| 382 | session->report_return = NULL; |
| 383 | } else { |
| 384 | /* Device returned a HANDSHAKE, indicating protocol error. */ |
| 385 | len = -EIO; |
| 386 | } |
| 387 | |
| 388 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 389 | mutex_unlock(&session->report_mutex); |
| 390 | |
| 391 | return len; |
| 392 | |
| 393 | err_restartsys: |
| 394 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 395 | mutex_unlock(&session->report_mutex); |
| 396 | return -ERESTARTSYS; |
| 397 | err_eio: |
| 398 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 399 | mutex_unlock(&session->report_mutex); |
| 400 | return -EIO; |
| 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | 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] | 404 | unsigned char report_type) |
| 405 | { |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 406 | struct hidp_session *session = hid->driver_data; |
| 407 | int ret; |
| 408 | |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 409 | switch (report_type) { |
| 410 | case HID_FEATURE_REPORT: |
| 411 | report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE; |
| 412 | break; |
| 413 | case HID_OUTPUT_REPORT: |
Antonio Ospite | 97e1efb | 2011-02-20 18:26:46 +0100 | [diff] [blame] | 414 | report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_OUPUT; |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 415 | break; |
| 416 | default: |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 420 | if (mutex_lock_interruptible(&session->report_mutex)) |
| 421 | return -ERESTARTSYS; |
| 422 | |
| 423 | /* Set up our wait, and send the report request to the device. */ |
| 424 | set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 425 | if (hidp_send_ctrl_message(hid->driver_data, report_type, |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 426 | data, count)) { |
| 427 | ret = -ENOMEM; |
| 428 | goto err; |
| 429 | } |
| 430 | |
| 431 | /* Wait for the ACK from the device. */ |
| 432 | while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)) { |
| 433 | int res; |
| 434 | |
| 435 | res = wait_event_interruptible_timeout(session->report_queue, |
| 436 | !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags), |
| 437 | 10*HZ); |
| 438 | if (res == 0) { |
| 439 | /* timeout */ |
| 440 | ret = -EIO; |
| 441 | goto err; |
| 442 | } |
| 443 | if (res < 0) { |
| 444 | /* signal */ |
| 445 | ret = -ERESTARTSYS; |
| 446 | goto err; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (!session->output_report_success) { |
| 451 | ret = -EIO; |
| 452 | goto err; |
| 453 | } |
| 454 | |
| 455 | ret = count; |
| 456 | |
| 457 | err: |
| 458 | clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); |
| 459 | mutex_unlock(&session->report_mutex); |
| 460 | return ret; |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | static void hidp_idle_timeout(unsigned long arg) |
| 464 | { |
| 465 | struct hidp_session *session = (struct hidp_session *) arg; |
| 466 | |
Peter Hurley | 7bb59df | 2011-06-30 13:53:53 -0400 | [diff] [blame] | 467 | atomic_inc(&session->terminate); |
| 468 | wake_up_process(session->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 471 | static void hidp_set_timer(struct hidp_session *session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | if (session->idle_to > 0) |
| 474 | mod_timer(&session->timer, jiffies + HZ * session->idle_to); |
| 475 | } |
| 476 | |
| 477 | static inline void hidp_del_timer(struct hidp_session *session) |
| 478 | { |
| 479 | if (session->idle_to > 0) |
| 480 | del_timer(&session->timer); |
| 481 | } |
| 482 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 483 | static void hidp_process_handshake(struct hidp_session *session, |
| 484 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | BT_DBG("session %p param 0x%02x", session, param); |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 487 | session->output_report_success = 0; /* default condition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | switch (param) { |
| 490 | case HIDP_HSHK_SUCCESSFUL: |
| 491 | /* FIXME: Call into SET_ GET_ handlers here */ |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 492 | session->output_report_success = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | break; |
| 494 | |
| 495 | case HIDP_HSHK_NOT_READY: |
| 496 | case HIDP_HSHK_ERR_INVALID_REPORT_ID: |
| 497 | case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST: |
| 498 | case HIDP_HSHK_ERR_INVALID_PARAMETER: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 499 | if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)) { |
| 500 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 501 | wake_up_interruptible(&session->report_queue); |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* FIXME: Call into SET_ GET_ handlers here */ |
| 504 | break; |
| 505 | |
| 506 | case HIDP_HSHK_ERR_UNKNOWN: |
| 507 | break; |
| 508 | |
| 509 | case HIDP_HSHK_ERR_FATAL: |
| 510 | /* Device requests a reboot, as this is the only way this error |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 511 | * can be recovered. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | __hidp_send_ctrl_message(session, |
| 513 | HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0); |
| 514 | break; |
| 515 | |
| 516 | default: |
| 517 | __hidp_send_ctrl_message(session, |
| 518 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0); |
| 519 | break; |
| 520 | } |
Alan Ott | 0825411 | 2011-01-18 03:04:38 -0500 | [diff] [blame] | 521 | |
| 522 | /* Wake up the waiting thread. */ |
| 523 | if (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)) { |
| 524 | clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); |
| 525 | wake_up_interruptible(&session->report_queue); |
| 526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 529 | static void hidp_process_hid_control(struct hidp_session *session, |
| 530 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
| 532 | BT_DBG("session %p param 0x%02x", session, param); |
| 533 | |
Dave Young | eff001e | 2008-02-05 03:07:14 -0800 | [diff] [blame] | 534 | if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /* Flush the transmit queues */ |
| 536 | skb_queue_purge(&session->ctrl_transmit); |
| 537 | skb_queue_purge(&session->intr_transmit); |
| 538 | |
Peter Hurley | 7bb59df | 2011-06-30 13:53:53 -0400 | [diff] [blame] | 539 | atomic_inc(&session->terminate); |
| 540 | wake_up_process(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 544 | /* Returns true if the passed-in skb should be freed by the caller. */ |
| 545 | static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb, |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 546 | unsigned char param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 548 | int done_with_skb = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param); |
| 550 | |
| 551 | switch (param) { |
| 552 | case HIDP_DATA_RTYPE_INPUT: |
| 553 | hidp_set_timer(session); |
| 554 | |
| 555 | if (session->input) |
| 556 | hidp_input_report(session, skb); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 557 | |
| 558 | if (session->hid) |
| 559 | 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] | 560 | break; |
| 561 | |
| 562 | case HIDP_DATA_RTYPE_OTHER: |
| 563 | case HIDP_DATA_RTYPE_OUPUT: |
| 564 | case HIDP_DATA_RTYPE_FEATURE: |
| 565 | break; |
| 566 | |
| 567 | default: |
| 568 | __hidp_send_ctrl_message(session, |
| 569 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0); |
| 570 | } |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 571 | |
| 572 | if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) && |
| 573 | param == session->waiting_report_type) { |
| 574 | if (session->waiting_report_number < 0 || |
| 575 | session->waiting_report_number == skb->data[0]) { |
| 576 | /* hidp_get_raw_report() is waiting on this report. */ |
| 577 | session->report_return = skb; |
| 578 | done_with_skb = 0; |
| 579 | clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags); |
| 580 | wake_up_interruptible(&session->report_queue); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return done_with_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 587 | static void hidp_recv_ctrl_frame(struct hidp_session *session, |
| 588 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
| 590 | unsigned char hdr, type, param; |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 591 | int free_skb = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | BT_DBG("session %p skb %p len %d", session, skb, skb->len); |
| 594 | |
| 595 | hdr = skb->data[0]; |
| 596 | skb_pull(skb, 1); |
| 597 | |
| 598 | type = hdr & HIDP_HEADER_TRANS_MASK; |
| 599 | param = hdr & HIDP_HEADER_PARAM_MASK; |
| 600 | |
| 601 | switch (type) { |
| 602 | case HIDP_TRANS_HANDSHAKE: |
| 603 | hidp_process_handshake(session, param); |
| 604 | break; |
| 605 | |
| 606 | case HIDP_TRANS_HID_CONTROL: |
| 607 | hidp_process_hid_control(session, param); |
| 608 | break; |
| 609 | |
| 610 | case HIDP_TRANS_DATA: |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 611 | free_skb = hidp_process_data(session, skb, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | default: |
| 615 | __hidp_send_ctrl_message(session, |
| 616 | HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0); |
| 617 | break; |
| 618 | } |
| 619 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 620 | if (free_skb) |
| 621 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 624 | static void hidp_recv_intr_frame(struct hidp_session *session, |
| 625 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | unsigned char hdr; |
| 628 | |
| 629 | BT_DBG("session %p skb %p len %d", session, skb, skb->len); |
| 630 | |
| 631 | hdr = skb->data[0]; |
| 632 | skb_pull(skb, 1); |
| 633 | |
| 634 | if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) { |
| 635 | hidp_set_timer(session); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (session->input) |
| 638 | hidp_input_report(session, skb); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 639 | |
| 640 | if (session->hid) { |
| 641 | hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1); |
| 642 | BT_DBG("report len %d", skb->len); |
| 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } else { |
| 645 | BT_DBG("Unsupported protocol header 0x%02x", hdr); |
| 646 | } |
| 647 | |
| 648 | kfree_skb(skb); |
| 649 | } |
| 650 | |
| 651 | static int hidp_send_frame(struct socket *sock, unsigned char *data, int len) |
| 652 | { |
| 653 | struct kvec iv = { data, len }; |
| 654 | struct msghdr msg; |
| 655 | |
| 656 | BT_DBG("sock %p data %p len %d", sock, data, len); |
| 657 | |
| 658 | if (!len) |
| 659 | return 0; |
| 660 | |
| 661 | memset(&msg, 0, sizeof(msg)); |
| 662 | |
| 663 | return kernel_sendmsg(sock, &msg, &iv, 1, len); |
| 664 | } |
| 665 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 666 | static void hidp_process_transmit(struct hidp_session *session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | struct sk_buff *skb; |
| 669 | |
| 670 | BT_DBG("session %p", session); |
| 671 | |
| 672 | while ((skb = skb_dequeue(&session->ctrl_transmit))) { |
| 673 | if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) { |
| 674 | skb_queue_head(&session->ctrl_transmit, skb); |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | hidp_set_timer(session); |
| 679 | kfree_skb(skb); |
| 680 | } |
| 681 | |
| 682 | while ((skb = skb_dequeue(&session->intr_transmit))) { |
| 683 | if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) { |
| 684 | skb_queue_head(&session->intr_transmit, skb); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | hidp_set_timer(session); |
| 689 | kfree_skb(skb); |
| 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static int hidp_session(void *arg) |
| 694 | { |
| 695 | struct hidp_session *session = arg; |
| 696 | struct sock *ctrl_sk = session->ctrl_sock->sk; |
| 697 | struct sock *intr_sk = session->intr_sock->sk; |
| 698 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | wait_queue_t ctrl_wait, intr_wait; |
| 700 | |
| 701 | BT_DBG("session %p", session); |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | set_user_nice(current, -15); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | init_waitqueue_entry(&ctrl_wait, current); |
| 706 | init_waitqueue_entry(&intr_wait, current); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 707 | add_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait); |
| 708 | add_wait_queue(sk_sleep(intr_sk), &intr_wait); |
Alan Ott | 0f69dca | 2011-01-18 03:04:37 -0500 | [diff] [blame] | 709 | session->waiting_for_startup = 0; |
| 710 | wake_up_interruptible(&session->startup_queue); |
Peter Hurley | 7bb59df | 2011-06-30 13:53:53 -0400 | [diff] [blame] | 711 | set_current_state(TASK_INTERRUPTIBLE); |
| 712 | while (!atomic_read(&session->terminate)) { |
Szymon Janc | 17f09a7 | 2011-03-21 14:20:01 +0100 | [diff] [blame] | 713 | if (ctrl_sk->sk_state != BT_CONNECTED || |
| 714 | intr_sk->sk_state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | break; |
| 716 | |
| 717 | while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) { |
| 718 | skb_orphan(skb); |
| 719 | hidp_recv_ctrl_frame(session, skb); |
| 720 | } |
| 721 | |
| 722 | while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) { |
| 723 | skb_orphan(skb); |
| 724 | hidp_recv_intr_frame(session, skb); |
| 725 | } |
| 726 | |
| 727 | hidp_process_transmit(session); |
| 728 | |
| 729 | schedule(); |
Peter Hurley | 7bb59df | 2011-06-30 13:53:53 -0400 | [diff] [blame] | 730 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 733 | remove_wait_queue(sk_sleep(intr_sk), &intr_wait); |
| 734 | remove_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | down_write(&hidp_session_sem); |
| 737 | |
| 738 | hidp_del_timer(session); |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (session->input) { |
| 741 | input_unregister_device(session->input); |
Dmitry Torokhov | 34abf91 | 2005-09-15 02:01:40 -0500 | [diff] [blame] | 742 | session->input = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 745 | if (session->hid) { |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 746 | hid_destroy_device(session->hid); |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 747 | session->hid = NULL; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Marcel Holtmann | ec8dab3 | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 750 | /* Wakeup user-space polling for socket errors */ |
| 751 | session->intr_sock->sk->sk_err = EUNATCH; |
| 752 | session->ctrl_sock->sk->sk_err = EUNATCH; |
| 753 | |
| 754 | hidp_schedule(session); |
| 755 | |
David Woodhouse | 1c39858 | 2007-07-07 14:58:39 -0400 | [diff] [blame] | 756 | fput(session->intr_sock->file); |
| 757 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 758 | wait_event_timeout(*(sk_sleep(ctrl_sk)), |
David Woodhouse | 1c39858 | 2007-07-07 14:58:39 -0400 | [diff] [blame] | 759 | (ctrl_sk->sk_state == BT_CLOSED), msecs_to_jiffies(500)); |
| 760 | |
| 761 | fput(session->ctrl_sock->file); |
| 762 | |
| 763 | __hidp_unlink_session(session); |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | up_write(&hidp_session_sem); |
| 766 | |
Peter Hurley | 1c97e94 | 2011-08-05 10:51:34 -0400 | [diff] [blame] | 767 | kfree(session->rd_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | kfree(session); |
| 769 | return 0; |
| 770 | } |
| 771 | |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 772 | static struct device *hidp_get_device(struct hidp_session *session) |
| 773 | { |
| 774 | bdaddr_t *src = &bt_sk(session->ctrl_sock->sk)->src; |
| 775 | bdaddr_t *dst = &bt_sk(session->ctrl_sock->sk)->dst; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 776 | struct device *device = NULL; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 777 | struct hci_dev *hdev; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 778 | |
| 779 | hdev = hci_get_route(dst, src); |
| 780 | if (!hdev) |
| 781 | return NULL; |
| 782 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 783 | session->conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 784 | if (session->conn) |
| 785 | device = &session->conn->dev; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 786 | |
| 787 | hci_dev_put(hdev); |
| 788 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 789 | return device; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 792 | static int hidp_setup_input(struct hidp_session *session, |
| 793 | struct hidp_connadd_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 795 | struct input_dev *input; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 796 | int err, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 798 | input = input_allocate_device(); |
| 799 | if (!input) |
| 800 | return -ENOMEM; |
| 801 | |
| 802 | session->input = input; |
| 803 | |
Marcel Holtmann | 5be3946 | 2007-05-09 09:15:30 +0200 | [diff] [blame] | 804 | input_set_drvdata(input, session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Dmitry Torokhov | 34abf91 | 2005-09-15 02:01:40 -0500 | [diff] [blame] | 806 | input->name = "Bluetooth HID Boot Protocol Device"; |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | input->id.bustype = BUS_BLUETOOTH; |
| 809 | input->id.vendor = req->vendor; |
| 810 | input->id.product = req->product; |
| 811 | input->id.version = req->version; |
| 812 | |
| 813 | if (req->subclass & 0x40) { |
| 814 | set_bit(EV_KEY, input->evbit); |
| 815 | set_bit(EV_LED, input->evbit); |
| 816 | set_bit(EV_REP, input->evbit); |
| 817 | |
| 818 | set_bit(LED_NUML, input->ledbit); |
| 819 | set_bit(LED_CAPSL, input->ledbit); |
| 820 | set_bit(LED_SCROLLL, input->ledbit); |
| 821 | set_bit(LED_COMPOSE, input->ledbit); |
| 822 | set_bit(LED_KANA, input->ledbit); |
| 823 | |
| 824 | for (i = 0; i < sizeof(hidp_keycode); i++) |
| 825 | set_bit(hidp_keycode[i], input->keybit); |
| 826 | clear_bit(0, input->keybit); |
| 827 | } |
| 828 | |
| 829 | if (req->subclass & 0x80) { |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 830 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 831 | input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 832 | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE); |
| 833 | input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 834 | input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) | |
| 835 | BIT_MASK(BTN_EXTRA); |
| 836 | input->relbit[0] |= BIT_MASK(REL_WHEEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Marcel Holtmann | 5be3946 | 2007-05-09 09:15:30 +0200 | [diff] [blame] | 839 | input->dev.parent = hidp_get_device(session); |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | input->event = hidp_input_event; |
| 842 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 843 | err = input_register_device(input); |
| 844 | if (err < 0) { |
Peter Hurley | 615aedd | 2011-08-05 10:51:50 -0400 | [diff] [blame] | 845 | input_free_device(input); |
| 846 | session->input = NULL; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 847 | return err; |
| 848 | } |
| 849 | |
| 850 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Marcel Holtmann | f5ffd46 | 2007-02-17 23:58:53 +0100 | [diff] [blame] | 853 | static int hidp_open(struct hid_device *hid) |
| 854 | { |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static void hidp_close(struct hid_device *hid) |
| 859 | { |
| 860 | } |
| 861 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 862 | static int hidp_parse(struct hid_device *hid) |
| 863 | { |
| 864 | struct hidp_session *session = hid->driver_data; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 865 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 866 | return hid_parse_report(session->hid, session->rd_data, |
| 867 | session->rd_size); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static int hidp_start(struct hid_device *hid) |
| 871 | { |
| 872 | struct hidp_session *session = hid->driver_data; |
| 873 | struct hid_report *report; |
| 874 | |
David Herrmann | 142c69c | 2011-08-26 13:27:12 +0200 | [diff] [blame] | 875 | if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS) |
| 876 | return 0; |
| 877 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 878 | list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT]. |
| 879 | report_list, list) |
| 880 | hidp_send_report(session, report); |
| 881 | |
| 882 | list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT]. |
| 883 | report_list, list) |
| 884 | hidp_send_report(session, report); |
| 885 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | static void hidp_stop(struct hid_device *hid) |
| 890 | { |
| 891 | struct hidp_session *session = hid->driver_data; |
| 892 | |
| 893 | skb_queue_purge(&session->ctrl_transmit); |
| 894 | skb_queue_purge(&session->intr_transmit); |
| 895 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 896 | hid->claimed = 0; |
| 897 | } |
| 898 | |
| 899 | static struct hid_ll_driver hidp_hid_driver = { |
| 900 | .parse = hidp_parse, |
| 901 | .start = hidp_start, |
| 902 | .stop = hidp_stop, |
| 903 | .open = hidp_open, |
| 904 | .close = hidp_close, |
| 905 | .hidinput_input_event = hidp_hidinput_event, |
| 906 | }; |
| 907 | |
Alan Ott | 0f69dca | 2011-01-18 03:04:37 -0500 | [diff] [blame] | 908 | /* This function sets up the hid device. It does not add it |
| 909 | to the HID system. That is done in hidp_add_connection(). */ |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 910 | static int hidp_setup_hid(struct hidp_session *session, |
Andrew Morton | 91f5cca | 2008-02-05 03:07:58 -0800 | [diff] [blame] | 911 | struct hidp_connadd_req *req) |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 912 | { |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 913 | struct hid_device *hid; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 914 | int err; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 915 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 916 | session->rd_data = kzalloc(req->rd_size, GFP_KERNEL); |
| 917 | if (!session->rd_data) |
| 918 | return -ENOMEM; |
| 919 | |
| 920 | if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) { |
| 921 | err = -EFAULT; |
| 922 | goto fault; |
| 923 | } |
| 924 | session->rd_size = req->rd_size; |
| 925 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 926 | hid = hid_allocate_device(); |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 927 | if (IS_ERR(hid)) { |
| 928 | err = PTR_ERR(hid); |
| 929 | goto fault; |
| 930 | } |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 931 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 932 | session->hid = hid; |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 933 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 934 | hid->driver_data = session; |
| 935 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 936 | hid->bus = BUS_BLUETOOTH; |
| 937 | hid->vendor = req->vendor; |
| 938 | hid->product = req->product; |
| 939 | hid->version = req->version; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 940 | hid->country = req->country; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 941 | |
| 942 | strncpy(hid->name, req->name, 128); |
Gustavo F. Padovan | d6b2eb2 | 2010-09-03 18:29:46 -0300 | [diff] [blame] | 943 | strncpy(hid->phys, batostr(&bt_sk(session->ctrl_sock->sk)->src), 64); |
| 944 | strncpy(hid->uniq, batostr(&bt_sk(session->ctrl_sock->sk)->dst), 64); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 945 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 946 | hid->dev.parent = hidp_get_device(session); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 947 | hid->ll_driver = &hidp_hid_driver; |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 948 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 949 | hid->hid_get_raw_report = hidp_get_raw_report; |
Jiri Kosina | 2da3193 | 2009-11-26 16:20:56 +0100 | [diff] [blame] | 950 | hid->hid_output_raw_report = hidp_output_raw_report; |
| 951 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 952 | return 0; |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 953 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 954 | fault: |
| 955 | kfree(session->rd_data); |
| 956 | session->rd_data = NULL; |
| 957 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 958 | return err; |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 959 | } |
| 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock) |
| 962 | { |
| 963 | struct hidp_session *session, *s; |
Szymon Janc | aabf6f8 | 2011-04-05 15:37:45 +0200 | [diff] [blame] | 964 | int vendor, product; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | int err; |
| 966 | |
| 967 | BT_DBG(""); |
| 968 | |
| 969 | if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) || |
| 970 | bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst)) |
| 971 | return -ENOTUNIQ; |
| 972 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 973 | session = kzalloc(sizeof(struct hidp_session), GFP_KERNEL); |
Dmitry Torokhov | 34abf91 | 2005-09-15 02:01:40 -0500 | [diff] [blame] | 974 | if (!session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 977 | BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size); |
| 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | down_write(&hidp_session_sem); |
| 980 | |
| 981 | s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst); |
| 982 | if (s && s->state == BT_CONNECTED) { |
| 983 | err = -EEXIST; |
| 984 | goto failed; |
| 985 | } |
| 986 | |
| 987 | bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst); |
| 988 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 989 | session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->chan->omtu, |
| 990 | l2cap_pi(ctrl_sock->sk)->chan->imtu); |
| 991 | session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->chan->omtu, |
| 992 | l2cap_pi(intr_sock->sk)->chan->imtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu); |
| 995 | |
| 996 | session->ctrl_sock = ctrl_sock; |
| 997 | session->intr_sock = intr_sock; |
| 998 | session->state = BT_CONNECTED; |
| 999 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1000 | setup_timer(&session->timer, hidp_idle_timeout, (unsigned long)session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | skb_queue_head_init(&session->ctrl_transmit); |
| 1003 | skb_queue_head_init(&session->intr_transmit); |
| 1004 | |
Alan Ott | 0ff1731 | 2011-01-18 03:04:40 -0500 | [diff] [blame] | 1005 | mutex_init(&session->report_mutex); |
| 1006 | init_waitqueue_head(&session->report_queue); |
Alan Ott | 0f69dca | 2011-01-18 03:04:37 -0500 | [diff] [blame] | 1007 | init_waitqueue_head(&session->startup_queue); |
| 1008 | session->waiting_for_startup = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID); |
| 1010 | session->idle_to = req->idle_to; |
| 1011 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1012 | if (req->rd_size > 0) { |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1013 | err = hidp_setup_hid(session, req); |
Jiri Slaby | d458a9d | 2008-05-16 11:49:20 +0200 | [diff] [blame] | 1014 | if (err && err != -ENODEV) |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1015 | goto purge; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | if (!session->hid) { |
| 1019 | err = hidp_setup_input(session, req); |
| 1020 | if (err < 0) |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1021 | goto purge; |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1022 | } |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | __hidp_link_session(session); |
| 1025 | |
| 1026 | hidp_set_timer(session); |
| 1027 | |
Szymon Janc | aabf6f8 | 2011-04-05 15:37:45 +0200 | [diff] [blame] | 1028 | if (session->hid) { |
| 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; |
| 1034 | } else { |
| 1035 | vendor = 0x0000; |
| 1036 | product = 0x0000; |
| 1037 | } |
| 1038 | |
| 1039 | session->task = kthread_run(hidp_session, session, "khidpd_%04x%04x", |
| 1040 | vendor, product); |
| 1041 | if (IS_ERR(session->task)) { |
| 1042 | err = PTR_ERR(session->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | goto unlink; |
Szymon Janc | aabf6f8 | 2011-04-05 15:37:45 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Alan Ott | 0f69dca | 2011-01-18 03:04:37 -0500 | [diff] [blame] | 1046 | while (session->waiting_for_startup) { |
| 1047 | wait_event_interruptible(session->startup_queue, |
| 1048 | !session->waiting_for_startup); |
| 1049 | } |
| 1050 | |
| 1051 | err = hid_add_device(session->hid); |
Peter Hurley | e9d5cb5 | 2011-08-05 10:51:26 -0400 | [diff] [blame] | 1052 | if (err < 0) { |
| 1053 | atomic_inc(&session->terminate); |
| 1054 | wake_up_process(session->task); |
| 1055 | up_write(&hidp_session_sem); |
| 1056 | return err; |
| 1057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (session->input) { |
| 1060 | hidp_send_ctrl_message(session, |
| 1061 | HIDP_TRANS_SET_PROTOCOL | HIDP_PROTO_BOOT, NULL, 0); |
| 1062 | session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE); |
| 1063 | |
| 1064 | session->leds = 0xff; |
| 1065 | hidp_input_event(session->input, EV_LED, 0, 0); |
| 1066 | } |
| 1067 | |
| 1068 | up_write(&hidp_session_sem); |
| 1069 | return 0; |
| 1070 | |
| 1071 | unlink: |
| 1072 | hidp_del_timer(session); |
| 1073 | |
| 1074 | __hidp_unlink_session(session); |
| 1075 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1076 | if (session->input) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | input_unregister_device(session->input); |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1078 | session->input = NULL; |
| 1079 | } |
| 1080 | |
| 1081 | if (session->hid) { |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1082 | hid_destroy_device(session->hid); |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1083 | session->hid = NULL; |
| 1084 | } |
| 1085 | |
Michael Poole | 15c697c | 2010-02-05 12:23:43 -0500 | [diff] [blame] | 1086 | kfree(session->rd_data); |
| 1087 | session->rd_data = NULL; |
| 1088 | |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1089 | purge: |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1090 | skb_queue_purge(&session->ctrl_transmit); |
| 1091 | skb_queue_purge(&session->intr_transmit); |
Marcel Holtmann | edad638 | 2009-08-22 14:22:15 -0700 | [diff] [blame] | 1092 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1093 | failed: |
| 1094 | up_write(&hidp_session_sem); |
Marcel Holtmann | e1aaadd | 2007-02-17 23:58:49 +0100 | [diff] [blame] | 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | kfree(session); |
| 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | int hidp_del_connection(struct hidp_conndel_req *req) |
| 1101 | { |
| 1102 | struct hidp_session *session; |
| 1103 | int err = 0; |
| 1104 | |
| 1105 | BT_DBG(""); |
| 1106 | |
| 1107 | down_read(&hidp_session_sem); |
| 1108 | |
| 1109 | session = __hidp_get_session(&req->bdaddr); |
| 1110 | if (session) { |
| 1111 | if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) { |
| 1112 | hidp_send_ctrl_message(session, |
| 1113 | HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, NULL, 0); |
| 1114 | } else { |
| 1115 | /* Flush the transmit queues */ |
| 1116 | skb_queue_purge(&session->ctrl_transmit); |
| 1117 | skb_queue_purge(&session->intr_transmit); |
| 1118 | |
Peter Hurley | 7bb59df | 2011-06-30 13:53:53 -0400 | [diff] [blame] | 1119 | atomic_inc(&session->terminate); |
| 1120 | wake_up_process(session->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | } else |
| 1123 | err = -ENOENT; |
| 1124 | |
| 1125 | up_read(&hidp_session_sem); |
| 1126 | return err; |
| 1127 | } |
| 1128 | |
| 1129 | int hidp_get_connlist(struct hidp_connlist_req *req) |
| 1130 | { |
| 1131 | struct list_head *p; |
| 1132 | int err = 0, n = 0; |
| 1133 | |
| 1134 | BT_DBG(""); |
| 1135 | |
| 1136 | down_read(&hidp_session_sem); |
| 1137 | |
| 1138 | list_for_each(p, &hidp_session_list) { |
| 1139 | struct hidp_session *session; |
| 1140 | struct hidp_conninfo ci; |
| 1141 | |
| 1142 | session = list_entry(p, struct hidp_session, list); |
| 1143 | |
| 1144 | __hidp_copy_session(session, &ci); |
| 1145 | |
| 1146 | if (copy_to_user(req->ci, &ci, sizeof(ci))) { |
| 1147 | err = -EFAULT; |
| 1148 | break; |
| 1149 | } |
| 1150 | |
| 1151 | if (++n >= req->cnum) |
| 1152 | break; |
| 1153 | |
| 1154 | req->ci++; |
| 1155 | } |
| 1156 | req->cnum = n; |
| 1157 | |
| 1158 | up_read(&hidp_session_sem); |
| 1159 | return err; |
| 1160 | } |
| 1161 | |
| 1162 | int hidp_get_conninfo(struct hidp_conninfo *ci) |
| 1163 | { |
| 1164 | struct hidp_session *session; |
| 1165 | int err = 0; |
| 1166 | |
| 1167 | down_read(&hidp_session_sem); |
| 1168 | |
| 1169 | session = __hidp_get_session(&ci->bdaddr); |
| 1170 | if (session) |
| 1171 | __hidp_copy_session(session, ci); |
| 1172 | else |
| 1173 | err = -ENOENT; |
| 1174 | |
| 1175 | up_read(&hidp_session_sem); |
| 1176 | return err; |
| 1177 | } |
| 1178 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1179 | static const struct hid_device_id hidp_table[] = { |
| 1180 | { HID_BLUETOOTH_DEVICE(HID_ANY_ID, HID_ANY_ID) }, |
| 1181 | { } |
| 1182 | }; |
| 1183 | |
| 1184 | static struct hid_driver hidp_driver = { |
| 1185 | .name = "generic-bluetooth", |
| 1186 | .id_table = hidp_table, |
| 1187 | }; |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | static int __init hidp_init(void) |
| 1190 | { |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1191 | int ret; |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION); |
| 1194 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1195 | ret = hid_register_driver(&hidp_driver); |
| 1196 | if (ret) |
| 1197 | goto err; |
| 1198 | |
| 1199 | ret = hidp_init_sockets(); |
| 1200 | if (ret) |
| 1201 | goto err_drv; |
| 1202 | |
| 1203 | return 0; |
| 1204 | err_drv: |
| 1205 | hid_unregister_driver(&hidp_driver); |
| 1206 | err: |
| 1207 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | static void __exit hidp_exit(void) |
| 1211 | { |
| 1212 | hidp_cleanup_sockets(); |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1213 | hid_unregister_driver(&hidp_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | module_init(hidp_init); |
| 1217 | module_exit(hidp_exit); |
| 1218 | |
| 1219 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 1220 | MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION); |
| 1221 | MODULE_VERSION(VERSION); |
| 1222 | MODULE_LICENSE("GPL"); |
| 1223 | MODULE_ALIAS("bt-proto-6"); |