hci_ldisc.c 13 KB

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