hci_ldisc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. *
  3. * Bluetooth HCI UART driver
  4. *
  5. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  6. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  7. * Copyright (c) 2000-2001, 2010-2012, The Linux Foundation. All rights reserved.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/poll.h>
  33. #include <linux/slab.h>
  34. #include <linux/tty.h>
  35. #include <linux/errno.h>
  36. #include <linux/string.h>
  37. #include <linux/signal.h>
  38. #include <linux/ioctl.h>
  39. #include <linux/skbuff.h>
  40. #include <net/bluetooth/bluetooth.h>
  41. #include <net/bluetooth/hci_core.h>
  42. #include "hci_uart.h"
  43. #define VERSION "2.2"
  44. static bool reset = 0;
  45. static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  46. static void hci_uart_tty_wakeup_action(unsigned long data);
  47. int hci_uart_register_proto(struct hci_uart_proto *p)
  48. {
  49. if (p->id >= HCI_UART_MAX_PROTO)
  50. return -EINVAL;
  51. if (hup[p->id])
  52. return -EEXIST;
  53. hup[p->id] = p;
  54. return 0;
  55. }
  56. int hci_uart_unregister_proto(struct hci_uart_proto *p)
  57. {
  58. if (p->id >= HCI_UART_MAX_PROTO)
  59. return -EINVAL;
  60. if (!hup[p->id])
  61. return -EINVAL;
  62. hup[p->id] = NULL;
  63. return 0;
  64. }
  65. static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  66. {
  67. if (id >= HCI_UART_MAX_PROTO)
  68. return NULL;
  69. return hup[id];
  70. }
  71. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  72. {
  73. struct hci_dev *hdev = hu->hdev;
  74. /* Update HCI stat counters */
  75. switch (pkt_type) {
  76. case HCI_COMMAND_PKT:
  77. hdev->stat.cmd_tx++;
  78. break;
  79. case HCI_ACLDATA_PKT:
  80. hdev->stat.acl_tx++;
  81. break;
  82. case HCI_SCODATA_PKT:
  83. hdev->stat.sco_tx++;
  84. break;
  85. }
  86. }
  87. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  88. {
  89. struct sk_buff *skb = hu->tx_skb;
  90. if (!skb)
  91. skb = hu->proto->dequeue(hu);
  92. else
  93. hu->tx_skb = NULL;
  94. return skb;
  95. }
  96. int hci_uart_tx_wakeup(struct hci_uart *hu)
  97. {
  98. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  99. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  100. return 0;
  101. }
  102. BT_DBG("");
  103. schedule_work(&hu->write_work);
  104. return 0;
  105. }
  106. static void hci_uart_write_work(struct work_struct *work)
  107. {
  108. struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
  109. struct tty_struct *tty = hu->tty;
  110. struct hci_dev *hdev = hu->hdev;
  111. struct sk_buff *skb;
  112. /* REVISIT: should we cope with bad skbs or ->write() returning
  113. * and error value ?
  114. */
  115. restart:
  116. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  117. while ((skb = hci_uart_dequeue(hu))) {
  118. int len;
  119. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  120. len = tty->ops->write(tty, skb->data, skb->len);
  121. hdev->stat.byte_tx += len;
  122. skb_pull(skb, len);
  123. if (skb->len) {
  124. hu->tx_skb = skb;
  125. break;
  126. }
  127. hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
  128. kfree_skb(skb);
  129. }
  130. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  131. goto restart;
  132. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  133. }
  134. /* ------- Interface to HCI layer ------ */
  135. /* Initialize device */
  136. static int hci_uart_open(struct hci_dev *hdev)
  137. {
  138. BT_DBG("%s %p", hdev->name, hdev);
  139. /* Nothing to do for UART driver */
  140. set_bit(HCI_RUNNING, &hdev->flags);
  141. return 0;
  142. }
  143. /* Reset device */
  144. static int hci_uart_flush(struct hci_dev *hdev)
  145. {
  146. struct hci_uart *hu = (struct hci_uart *) hdev->driver_data;
  147. struct tty_struct *tty = hu->tty;
  148. BT_DBG("hdev %p tty %p", hdev, tty);
  149. if (hu->tx_skb) {
  150. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  151. }
  152. /* Flush any pending characters in the driver and discipline. */
  153. tty_ldisc_flush(tty);
  154. tty_driver_flush_buffer(tty);
  155. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  156. hu->proto->flush(hu);
  157. return 0;
  158. }
  159. /* Close device */
  160. static int hci_uart_close(struct hci_dev *hdev)
  161. {
  162. BT_DBG("hdev %p", hdev);
  163. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  164. return 0;
  165. hci_uart_flush(hdev);
  166. hdev->flush = NULL;
  167. return 0;
  168. }
  169. /* Send frames from HCI layer */
  170. static int hci_uart_send_frame(struct sk_buff *skb)
  171. {
  172. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  173. struct hci_uart *hu;
  174. if (!hdev) {
  175. BT_ERR("Frame for unknown device (hdev=NULL)");
  176. return -ENODEV;
  177. }
  178. if (!test_bit(HCI_RUNNING, &hdev->flags))
  179. return -EBUSY;
  180. hu = (struct hci_uart *) hdev->driver_data;
  181. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
  182. hu->proto->enqueue(hu, skb);
  183. hci_uart_tx_wakeup(hu);
  184. return 0;
  185. }
  186. static void hci_uart_destruct(struct hci_dev *hdev)
  187. {
  188. if (!hdev)
  189. return;
  190. BT_DBG("%s", hdev->name);
  191. kfree(hdev->driver_data);
  192. }
  193. /* ------ LDISC part ------ */
  194. /* hci_uart_tty_open
  195. *
  196. * Called when line discipline changed to HCI_UART.
  197. *
  198. * Arguments:
  199. * tty pointer to tty info structure
  200. * Return Value:
  201. * 0 if success, otherwise error code
  202. */
  203. static int hci_uart_tty_open(struct tty_struct *tty)
  204. {
  205. struct hci_uart *hu = (void *) tty->disc_data;
  206. BT_DBG("tty %p", tty);
  207. /* FIXME: This btw is bogus, nothing requires the old ldisc to clear
  208. the pointer */
  209. if (hu)
  210. return -EEXIST;
  211. /* Error if the tty has no write op instead of leaving an exploitable
  212. hole */
  213. if (tty->ops->write == NULL)
  214. return -EOPNOTSUPP;
  215. if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  216. BT_ERR("Can't allocate control structure");
  217. return -ENFILE;
  218. }
  219. tty->disc_data = hu;
  220. hu->tty = tty;
  221. tty->receive_room = 65536;
  222. INIT_WORK(&hu->write_work, hci_uart_write_work);
  223. spin_lock_init(&hu->rx_lock);
  224. tasklet_init(&hu->tty_wakeup_task, hci_uart_tty_wakeup_action,
  225. (unsigned long)hu);
  226. /* Flush any pending characters in the driver and line discipline. */
  227. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  228. open path is before the ldisc is referencable */
  229. if (tty->ldisc->ops->flush_buffer)
  230. tty->ldisc->ops->flush_buffer(tty);
  231. tty_driver_flush_buffer(tty);
  232. return 0;
  233. }
  234. /* hci_uart_tty_close()
  235. *
  236. * Called when the line discipline is changed to something
  237. * else, the tty is closed, or the tty detects a hangup.
  238. */
  239. static void hci_uart_tty_close(struct tty_struct *tty)
  240. {
  241. struct hci_uart *hu = (void *)tty->disc_data;
  242. BT_DBG("tty %p", tty);
  243. /* Detach from the tty */
  244. tty->disc_data = NULL;
  245. if (hu) {
  246. struct hci_dev *hdev = hu->hdev;
  247. if (hdev)
  248. hci_uart_close(hdev);
  249. tasklet_kill(&hu->tty_wakeup_task);
  250. cancel_work_sync(&hu->write_work);
  251. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  252. hu->proto->close(hu);
  253. if (hdev) {
  254. hci_unregister_dev(hdev);
  255. hci_free_dev(hdev);
  256. }
  257. }
  258. }
  259. }
  260. /* hci_uart_tty_wakeup()
  261. *
  262. * Callback for transmit wakeup. Called when low level
  263. * device driver can accept more send data.
  264. * This callback gets called from the isr context so
  265. * schedule the send data operation to tasklet.
  266. *
  267. * Arguments: tty pointer to associated tty instance data
  268. * Return Value: None
  269. */
  270. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  271. {
  272. struct hci_uart *hu = (void *)tty->disc_data;
  273. tasklet_schedule(&hu->tty_wakeup_task);
  274. }
  275. /* hci_uart_tty_wakeup_action()
  276. *
  277. * Scheduled action to transmit data when low level device
  278. * driver can accept more data.
  279. */
  280. static void hci_uart_tty_wakeup_action(unsigned long data)
  281. {
  282. struct hci_uart *hu = (struct hci_uart *)data;
  283. struct tty_struct *tty;
  284. BT_DBG("");
  285. if (!hu)
  286. return;
  287. tty = hu->tty;
  288. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  289. if (tty != hu->tty)
  290. return;
  291. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  292. hci_uart_tx_wakeup(hu);
  293. }
  294. /* hci_uart_tty_receive()
  295. *
  296. * Called by tty low level driver when receive data is
  297. * available.
  298. *
  299. * Arguments: tty pointer to tty isntance data
  300. * data pointer to received data
  301. * flags pointer to flags for data
  302. * count count of received data in bytes
  303. *
  304. * Return Value: None
  305. */
  306. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  307. {
  308. int ret;
  309. struct hci_uart *hu = (void *)tty->disc_data;
  310. if (!hu || tty != hu->tty)
  311. return;
  312. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  313. return;
  314. spin_lock(&hu->rx_lock);
  315. ret = hu->proto->recv(hu, (void *) data, count);
  316. if (ret > 0)
  317. hu->hdev->stat.byte_rx += count;
  318. spin_unlock(&hu->rx_lock);
  319. tty_unthrottle(tty);
  320. }
  321. static int hci_uart_register_dev(struct hci_uart *hu)
  322. {
  323. struct hci_dev *hdev;
  324. BT_DBG("");
  325. /* Initialize and register HCI device */
  326. hdev = hci_alloc_dev();
  327. if (!hdev) {
  328. BT_ERR("Can't allocate HCI device");
  329. return -ENOMEM;
  330. }
  331. hu->hdev = hdev;
  332. hdev->bus = HCI_UART;
  333. hdev->driver_data = hu;
  334. hdev->open = hci_uart_open;
  335. hdev->close = hci_uart_close;
  336. hdev->flush = hci_uart_flush;
  337. hdev->send = hci_uart_send_frame;
  338. hdev->destruct = hci_uart_destruct;
  339. hdev->parent = hu->tty->dev;
  340. hdev->owner = THIS_MODULE;
  341. if (!reset)
  342. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  343. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  344. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  345. if (hci_register_dev(hdev) < 0) {
  346. BT_ERR("Can't register HCI device");
  347. hci_free_dev(hdev);
  348. return -ENODEV;
  349. }
  350. return 0;
  351. }
  352. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  353. {
  354. struct hci_uart_proto *p;
  355. int err;
  356. p = hci_uart_get_proto(id);
  357. if (!p)
  358. return -EPROTONOSUPPORT;
  359. err = p->open(hu);
  360. if (err)
  361. return err;
  362. hu->proto = p;
  363. err = hci_uart_register_dev(hu);
  364. if (err) {
  365. p->close(hu);
  366. return err;
  367. }
  368. return 0;
  369. }
  370. /* hci_uart_tty_ioctl()
  371. *
  372. * Process IOCTL system call for the tty device.
  373. *
  374. * Arguments:
  375. *
  376. * tty pointer to tty instance data
  377. * file pointer to open file object for device
  378. * cmd IOCTL command code
  379. * arg argument for IOCTL call (cmd dependent)
  380. *
  381. * Return Value: Command dependent
  382. */
  383. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
  384. unsigned int cmd, unsigned long arg)
  385. {
  386. struct hci_uart *hu = (void *)tty->disc_data;
  387. int err = 0;
  388. BT_DBG("");
  389. /* Verify the status of the device */
  390. if (!hu)
  391. return -EBADF;
  392. switch (cmd) {
  393. case HCIUARTSETPROTO:
  394. if (!test_and_set_bit(HCI_UART_PROTO_SET_IN_PROGRESS,
  395. &hu->flags) && !test_bit(HCI_UART_PROTO_SET,
  396. &hu->flags)) {
  397. err = hci_uart_set_proto(hu, arg);
  398. if (err) {
  399. clear_bit(HCI_UART_PROTO_SET_IN_PROGRESS,
  400. &hu->flags);
  401. return err;
  402. } else {
  403. set_bit(HCI_UART_PROTO_SET, &hu->flags);
  404. clear_bit(HCI_UART_PROTO_SET_IN_PROGRESS,
  405. &hu->flags);
  406. }
  407. } else
  408. return -EBUSY;
  409. break;
  410. case HCIUARTGETPROTO:
  411. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  412. return hu->proto->id;
  413. return -EUNATCH;
  414. case HCIUARTGETDEVICE:
  415. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  416. return hu->hdev->id;
  417. return -EUNATCH;
  418. case HCIUARTSETFLAGS:
  419. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  420. return -EBUSY;
  421. hu->hdev_flags = arg;
  422. break;
  423. case HCIUARTGETFLAGS:
  424. return hu->hdev_flags;
  425. default:
  426. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  427. break;
  428. };
  429. return err;
  430. }
  431. /*
  432. * We don't provide read/write/poll interface for user space.
  433. */
  434. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  435. unsigned char __user *buf, size_t nr)
  436. {
  437. return 0;
  438. }
  439. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  440. const unsigned char *data, size_t count)
  441. {
  442. return 0;
  443. }
  444. static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
  445. struct file *filp, poll_table *wait)
  446. {
  447. return 0;
  448. }
  449. static int __init hci_uart_init(void)
  450. {
  451. static struct tty_ldisc_ops hci_uart_ldisc;
  452. int err;
  453. BT_INFO("HCI UART driver ver %s", VERSION);
  454. /* Register the tty discipline */
  455. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  456. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  457. hci_uart_ldisc.name = "n_hci";
  458. hci_uart_ldisc.open = hci_uart_tty_open;
  459. hci_uart_ldisc.close = hci_uart_tty_close;
  460. hci_uart_ldisc.read = hci_uart_tty_read;
  461. hci_uart_ldisc.write = hci_uart_tty_write;
  462. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  463. hci_uart_ldisc.poll = hci_uart_tty_poll;
  464. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  465. hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
  466. hci_uart_ldisc.owner = THIS_MODULE;
  467. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  468. BT_ERR("HCI line discipline registration failed. (%d)", err);
  469. return err;
  470. }
  471. #ifdef CONFIG_BT_HCIUART_H4
  472. h4_init();
  473. #endif
  474. #ifdef CONFIG_BT_HCIUART_BCSP
  475. bcsp_init();
  476. #endif
  477. #ifdef CONFIG_BT_HCIUART_LL
  478. ll_init();
  479. #endif
  480. #ifdef CONFIG_BT_HCIUART_ATH3K
  481. ath_init();
  482. #endif
  483. #ifdef CONFIG_BT_HCIUART_IBS
  484. ibs_init();
  485. #endif
  486. return 0;
  487. }
  488. static void __exit hci_uart_exit(void)
  489. {
  490. int err;
  491. #ifdef CONFIG_BT_HCIUART_H4
  492. h4_deinit();
  493. #endif
  494. #ifdef CONFIG_BT_HCIUART_BCSP
  495. bcsp_deinit();
  496. #endif
  497. #ifdef CONFIG_BT_HCIUART_LL
  498. ll_deinit();
  499. #endif
  500. #ifdef CONFIG_BT_HCIUART_ATH3K
  501. ath_deinit();
  502. #endif
  503. #ifdef CONFIG_BT_HCIUART_IBS
  504. ibs_deinit();
  505. #endif
  506. /* Release tty registration of line discipline */
  507. if ((err = tty_unregister_ldisc(N_HCI)))
  508. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  509. }
  510. module_init(hci_uart_init);
  511. module_exit(hci_uart_exit);
  512. module_param(reset, bool, 0644);
  513. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  514. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  515. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  516. MODULE_VERSION(VERSION);
  517. MODULE_LICENSE("GPL");
  518. MODULE_ALIAS_LDISC(N_HCI);