core.c 37 KB

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