core.c 39 KB

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