hci_ldisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.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. /*
  21. * Bluetooth HCI UART driver.
  22. *
  23. * $Id: hci_ldisc.c,v 1.5 2002/10/02 18:37:20 maxk Exp $
  24. */
  25. #define VERSION "2.1"
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/sched.h>
  31. #include <linux/types.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/ptrace.h>
  35. #include <linux/poll.h>
  36. #include <linux/slab.h>
  37. #include <linux/tty.h>
  38. #include <linux/errno.h>
  39. #include <linux/string.h>
  40. #include <linux/signal.h>
  41. #include <linux/ioctl.h>
  42. #include <linux/skbuff.h>
  43. #include <net/bluetooth/bluetooth.h>
  44. #include <net/bluetooth/hci_core.h>
  45. #include "hci_uart.h"
  46. #ifndef CONFIG_BT_HCIUART_DEBUG
  47. #undef BT_DBG
  48. #define BT_DBG( A... )
  49. #endif
  50. static int reset = 0;
  51. static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  52. int hci_uart_register_proto(struct hci_uart_proto *p)
  53. {
  54. if (p->id >= HCI_UART_MAX_PROTO)
  55. return -EINVAL;
  56. if (hup[p->id])
  57. return -EEXIST;
  58. hup[p->id] = p;
  59. return 0;
  60. }
  61. int hci_uart_unregister_proto(struct hci_uart_proto *p)
  62. {
  63. if (p->id >= HCI_UART_MAX_PROTO)
  64. return -EINVAL;
  65. if (!hup[p->id])
  66. return -EINVAL;
  67. hup[p->id] = NULL;
  68. return 0;
  69. }
  70. static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  71. {
  72. if (id >= HCI_UART_MAX_PROTO)
  73. return NULL;
  74. return hup[id];
  75. }
  76. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  77. {
  78. struct hci_dev *hdev = hu->hdev;
  79. /* Update HCI stat counters */
  80. switch (pkt_type) {
  81. case HCI_COMMAND_PKT:
  82. hdev->stat.cmd_tx++;
  83. break;
  84. case HCI_ACLDATA_PKT:
  85. hdev->stat.acl_tx++;
  86. break;
  87. case HCI_SCODATA_PKT:
  88. hdev->stat.cmd_tx++;
  89. break;
  90. }
  91. }
  92. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  93. {
  94. struct sk_buff *skb = hu->tx_skb;
  95. if (!skb)
  96. skb = hu->proto->dequeue(hu);
  97. else
  98. hu->tx_skb = NULL;
  99. return skb;
  100. }
  101. int hci_uart_tx_wakeup(struct hci_uart *hu)
  102. {
  103. struct tty_struct *tty = hu->tty;
  104. struct hci_dev *hdev = hu->hdev;
  105. struct sk_buff *skb;
  106. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  107. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  108. return 0;
  109. }
  110. BT_DBG("");
  111. restart:
  112. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  113. while ((skb = hci_uart_dequeue(hu))) {
  114. int len;
  115. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  116. len = tty->driver->write(tty, skb->data, skb->len);
  117. hdev->stat.byte_tx += len;
  118. skb_pull(skb, len);
  119. if (skb->len) {
  120. hu->tx_skb = skb;
  121. break;
  122. }
  123. hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
  124. kfree_skb(skb);
  125. }
  126. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  127. goto restart;
  128. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  129. return 0;
  130. }
  131. /* ------- Interface to HCI layer ------ */
  132. /* Initialize device */
  133. static int hci_uart_open(struct hci_dev *hdev)
  134. {
  135. BT_DBG("%s %p", hdev->name, hdev);
  136. /* Nothing to do for UART driver */
  137. set_bit(HCI_RUNNING, &hdev->flags);
  138. return 0;
  139. }
  140. /* Reset device */
  141. static int hci_uart_flush(struct hci_dev *hdev)
  142. {
  143. struct hci_uart *hu = (struct hci_uart *) hdev->driver_data;
  144. struct tty_struct *tty = hu->tty;
  145. BT_DBG("hdev %p tty %p", hdev, tty);
  146. if (hu->tx_skb) {
  147. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  148. }
  149. /* Flush any pending characters in the driver and discipline. */
  150. tty_ldisc_flush(tty);
  151. if (tty->driver->flush_buffer)
  152. tty->driver->flush_buffer(tty);
  153. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  154. hu->proto->flush(hu);
  155. return 0;
  156. }
  157. /* Close device */
  158. static int hci_uart_close(struct hci_dev *hdev)
  159. {
  160. BT_DBG("hdev %p", hdev);
  161. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  162. return 0;
  163. hci_uart_flush(hdev);
  164. return 0;
  165. }
  166. /* Send frames from HCI layer */
  167. static int hci_uart_send_frame(struct sk_buff *skb)
  168. {
  169. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  170. struct tty_struct *tty;
  171. struct hci_uart *hu;
  172. if (!hdev) {
  173. BT_ERR("Frame for uknown device (hdev=NULL)");
  174. return -ENODEV;
  175. }
  176. if (!test_bit(HCI_RUNNING, &hdev->flags))
  177. return -EBUSY;
  178. hu = (struct hci_uart *) hdev->driver_data;
  179. tty = hu->tty;
  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. static void hci_uart_destruct(struct hci_dev *hdev)
  186. {
  187. struct hci_uart *hu;
  188. if (!hdev) return;
  189. BT_DBG("%s", hdev->name);
  190. hu = (struct hci_uart *) hdev->driver_data;
  191. kfree(hu);
  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. if (hu)
  208. return -EEXIST;
  209. if (!(hu = kmalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  210. BT_ERR("Can't allocate controll structure");
  211. return -ENFILE;
  212. }
  213. memset(hu, 0, sizeof(struct hci_uart));
  214. tty->disc_data = hu;
  215. hu->tty = tty;
  216. spin_lock_init(&hu->rx_lock);
  217. /* Flush any pending characters in the driver and line discipline. */
  218. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  219. open path is before the ldisc is referencable */
  220. if (tty->ldisc.flush_buffer)
  221. tty->ldisc.flush_buffer(tty);
  222. if (tty->driver->flush_buffer)
  223. tty->driver->flush_buffer(tty);
  224. return 0;
  225. }
  226. /* hci_uart_tty_close()
  227. *
  228. * Called when the line discipline is changed to something
  229. * else, the tty is closed, or the tty detects a hangup.
  230. */
  231. static void hci_uart_tty_close(struct tty_struct *tty)
  232. {
  233. struct hci_uart *hu = (void *)tty->disc_data;
  234. BT_DBG("tty %p", tty);
  235. /* Detach from the tty */
  236. tty->disc_data = NULL;
  237. if (hu) {
  238. struct hci_dev *hdev = hu->hdev;
  239. hci_uart_close(hdev);
  240. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  241. hu->proto->close(hu);
  242. hci_unregister_dev(hdev);
  243. hci_free_dev(hdev);
  244. }
  245. }
  246. }
  247. /* hci_uart_tty_wakeup()
  248. *
  249. * Callback for transmit wakeup. Called when low level
  250. * device driver can accept more send data.
  251. *
  252. * Arguments: tty pointer to associated tty instance data
  253. * Return Value: None
  254. */
  255. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  256. {
  257. struct hci_uart *hu = (void *)tty->disc_data;
  258. BT_DBG("");
  259. if (!hu)
  260. return;
  261. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  262. if (tty != hu->tty)
  263. return;
  264. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  265. hci_uart_tx_wakeup(hu);
  266. }
  267. /* hci_uart_tty_room()
  268. *
  269. * Callback function from tty driver. Return the amount of
  270. * space left in the receiver's buffer to decide if remote
  271. * transmitter is to be throttled.
  272. *
  273. * Arguments: tty pointer to associated tty instance data
  274. * Return Value: number of bytes left in receive buffer
  275. */
  276. static int hci_uart_tty_room (struct tty_struct *tty)
  277. {
  278. return 65536;
  279. }
  280. /* hci_uart_tty_receive()
  281. *
  282. * Called by tty low level driver when receive data is
  283. * available.
  284. *
  285. * Arguments: tty pointer to tty isntance data
  286. * data pointer to received data
  287. * flags pointer to flags for data
  288. * count count of received data in bytes
  289. *
  290. * Return Value: None
  291. */
  292. static void hci_uart_tty_receive(struct tty_struct *tty, const __u8 *data, char *flags, int count)
  293. {
  294. struct hci_uart *hu = (void *)tty->disc_data;
  295. if (!hu || tty != hu->tty)
  296. return;
  297. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  298. return;
  299. spin_lock(&hu->rx_lock);
  300. hu->proto->recv(hu, (void *) data, count);
  301. hu->hdev->stat.byte_rx += count;
  302. spin_unlock(&hu->rx_lock);
  303. if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver->unthrottle)
  304. tty->driver->unthrottle(tty);
  305. }
  306. static int hci_uart_register_dev(struct hci_uart *hu)
  307. {
  308. struct hci_dev *hdev;
  309. BT_DBG("");
  310. /* Initialize and register HCI device */
  311. hdev = hci_alloc_dev();
  312. if (!hdev) {
  313. BT_ERR("Can't allocate HCI device");
  314. return -ENOMEM;
  315. }
  316. hu->hdev = hdev;
  317. hdev->type = HCI_UART;
  318. hdev->driver_data = hu;
  319. hdev->open = hci_uart_open;
  320. hdev->close = hci_uart_close;
  321. hdev->flush = hci_uart_flush;
  322. hdev->send = hci_uart_send_frame;
  323. hdev->destruct = hci_uart_destruct;
  324. hdev->owner = THIS_MODULE;
  325. if (reset)
  326. set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
  327. if (hci_register_dev(hdev) < 0) {
  328. BT_ERR("Can't register HCI device");
  329. hci_free_dev(hdev);
  330. return -ENODEV;
  331. }
  332. return 0;
  333. }
  334. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  335. {
  336. struct hci_uart_proto *p;
  337. int err;
  338. p = hci_uart_get_proto(id);
  339. if (!p)
  340. return -EPROTONOSUPPORT;
  341. err = p->open(hu);
  342. if (err)
  343. return err;
  344. hu->proto = p;
  345. err = hci_uart_register_dev(hu);
  346. if (err) {
  347. p->close(hu);
  348. return err;
  349. }
  350. return 0;
  351. }
  352. /* hci_uart_tty_ioctl()
  353. *
  354. * Process IOCTL system call for the tty device.
  355. *
  356. * Arguments:
  357. *
  358. * tty pointer to tty instance data
  359. * file pointer to open file object for device
  360. * cmd IOCTL command code
  361. * arg argument for IOCTL call (cmd dependent)
  362. *
  363. * Return Value: Command dependent
  364. */
  365. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
  366. unsigned int cmd, unsigned long arg)
  367. {
  368. struct hci_uart *hu = (void *)tty->disc_data;
  369. int err = 0;
  370. BT_DBG("");
  371. /* Verify the status of the device */
  372. if (!hu)
  373. return -EBADF;
  374. switch (cmd) {
  375. case HCIUARTSETPROTO:
  376. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  377. err = hci_uart_set_proto(hu, arg);
  378. if (err) {
  379. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  380. return err;
  381. }
  382. tty->low_latency = 1;
  383. } else
  384. return -EBUSY;
  385. case HCIUARTGETPROTO:
  386. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  387. return hu->proto->id;
  388. return -EUNATCH;
  389. default:
  390. err = n_tty_ioctl(tty, file, cmd, arg);
  391. break;
  392. };
  393. return err;
  394. }
  395. /*
  396. * We don't provide read/write/poll interface for user space.
  397. */
  398. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file, unsigned char __user *buf, size_t nr)
  399. {
  400. return 0;
  401. }
  402. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
  403. {
  404. return 0;
  405. }
  406. static unsigned int hci_uart_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
  407. {
  408. return 0;
  409. }
  410. #ifdef CONFIG_BT_HCIUART_H4
  411. int h4_init(void);
  412. int h4_deinit(void);
  413. #endif
  414. #ifdef CONFIG_BT_HCIUART_BCSP
  415. int bcsp_init(void);
  416. int bcsp_deinit(void);
  417. #endif
  418. static int __init hci_uart_init(void)
  419. {
  420. static struct tty_ldisc hci_uart_ldisc;
  421. int err;
  422. BT_INFO("HCI UART driver ver %s", VERSION);
  423. /* Register the tty discipline */
  424. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  425. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  426. hci_uart_ldisc.name = "n_hci";
  427. hci_uart_ldisc.open = hci_uart_tty_open;
  428. hci_uart_ldisc.close = hci_uart_tty_close;
  429. hci_uart_ldisc.read = hci_uart_tty_read;
  430. hci_uart_ldisc.write = hci_uart_tty_write;
  431. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  432. hci_uart_ldisc.poll = hci_uart_tty_poll;
  433. hci_uart_ldisc.receive_room= hci_uart_tty_room;
  434. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  435. hci_uart_ldisc.write_wakeup= hci_uart_tty_wakeup;
  436. hci_uart_ldisc.owner = THIS_MODULE;
  437. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  438. BT_ERR("HCI line discipline registration failed. (%d)", err);
  439. return err;
  440. }
  441. #ifdef CONFIG_BT_HCIUART_H4
  442. h4_init();
  443. #endif
  444. #ifdef CONFIG_BT_HCIUART_BCSP
  445. bcsp_init();
  446. #endif
  447. return 0;
  448. }
  449. static void __exit hci_uart_exit(void)
  450. {
  451. int err;
  452. #ifdef CONFIG_BT_HCIUART_H4
  453. h4_deinit();
  454. #endif
  455. #ifdef CONFIG_BT_HCIUART_BCSP
  456. bcsp_deinit();
  457. #endif
  458. /* Release tty registration of line discipline */
  459. if ((err = tty_unregister_ldisc(N_HCI)))
  460. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  461. }
  462. module_init(hci_uart_init);
  463. module_exit(hci_uart_exit);
  464. module_param(reset, bool, 0644);
  465. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  466. MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
  467. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  468. MODULE_VERSION(VERSION);
  469. MODULE_LICENSE("GPL");
  470. MODULE_ALIAS_LDISC(N_HCI);