hci_ldisc.c 13 KB

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