core.c 39 KB

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