hci_ldisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. #undef BT_DMP
  50. #define BT_DMP( A... )
  51. #endif
  52. static int reset = 0;
  53. static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  54. int hci_uart_register_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 -EEXIST;
  60. hup[p->id] = p;
  61. return 0;
  62. }
  63. int hci_uart_unregister_proto(struct hci_uart_proto *p)
  64. {
  65. if (p->id >= HCI_UART_MAX_PROTO)
  66. return -EINVAL;
  67. if (!hup[p->id])
  68. return -EINVAL;
  69. hup[p->id] = NULL;
  70. return 0;
  71. }
  72. static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  73. {
  74. if (id >= HCI_UART_MAX_PROTO)
  75. return NULL;
  76. return hup[id];
  77. }
  78. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  79. {
  80. struct hci_dev *hdev = hu->hdev;
  81. /* Update HCI stat counters */
  82. switch (pkt_type) {
  83. case HCI_COMMAND_PKT:
  84. hdev->stat.cmd_tx++;
  85. break;
  86. case HCI_ACLDATA_PKT:
  87. hdev->stat.acl_tx++;
  88. break;
  89. case HCI_SCODATA_PKT:
  90. hdev->stat.cmd_tx++;
  91. break;
  92. }
  93. }
  94. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  95. {
  96. struct sk_buff *skb = hu->tx_skb;
  97. if (!skb)
  98. skb = hu->proto->dequeue(hu);
  99. else
  100. hu->tx_skb = NULL;
  101. return skb;
  102. }
  103. int hci_uart_tx_wakeup(struct hci_uart *hu)
  104. {
  105. struct tty_struct *tty = hu->tty;
  106. struct hci_dev *hdev = hu->hdev;
  107. struct sk_buff *skb;
  108. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  109. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  110. return 0;
  111. }
  112. BT_DBG("");
  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->driver->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, 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. return 0;
  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. if (tty->driver->flush_buffer)
  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. 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 tty_struct *tty;
  173. struct hci_uart *hu;
  174. if (!hdev) {
  175. BT_ERR("Frame for uknown 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. tty = hu->tty;
  182. BT_DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
  183. hu->proto->enqueue(hu, skb);
  184. hci_uart_tx_wakeup(hu);
  185. return 0;
  186. }
  187. static void hci_uart_destruct(struct hci_dev *hdev)
  188. {
  189. struct hci_uart *hu;
  190. if (!hdev) return;
  191. BT_DBG("%s", hdev->name);
  192. hu = (struct hci_uart *) hdev->driver_data;
  193. kfree(hu);
  194. }
  195. /* ------ LDISC part ------ */
  196. /* hci_uart_tty_open
  197. *
  198. * Called when line discipline changed to HCI_UART.
  199. *
  200. * Arguments:
  201. * tty pointer to tty info structure
  202. * Return Value:
  203. * 0 if success, otherwise error code
  204. */
  205. static int hci_uart_tty_open(struct tty_struct *tty)
  206. {
  207. struct hci_uart *hu = (void *) tty->disc_data;
  208. BT_DBG("tty %p", tty);
  209. if (hu)
  210. return -EEXIST;
  211. if (!(hu = kmalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  212. BT_ERR("Can't allocate controll structure");
  213. return -ENFILE;
  214. }
  215. memset(hu, 0, sizeof(struct hci_uart));
  216. tty->disc_data = hu;
  217. hu->tty = tty;
  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.flush_buffer)
  223. tty->ldisc.flush_buffer(tty);
  224. if (tty->driver->flush_buffer)
  225. tty->driver->flush_buffer(tty);
  226. return 0;
  227. }
  228. /* hci_uart_tty_close()
  229. *
  230. * Called when the line discipline is changed to something
  231. * else, the tty is closed, or the tty detects a hangup.
  232. */
  233. static void hci_uart_tty_close(struct tty_struct *tty)
  234. {
  235. struct hci_uart *hu = (void *)tty->disc_data;
  236. BT_DBG("tty %p", tty);
  237. /* Detach from the tty */
  238. tty->disc_data = NULL;
  239. if (hu) {
  240. struct hci_dev *hdev = hu->hdev;
  241. hci_uart_close(hdev);
  242. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  243. hu->proto->close(hu);
  244. hci_unregister_dev(hdev);
  245. hci_free_dev(hdev);
  246. }
  247. }
  248. }
  249. /* hci_uart_tty_wakeup()
  250. *
  251. * Callback for transmit wakeup. Called when low level
  252. * device driver can accept more send data.
  253. *
  254. * Arguments: tty pointer to associated tty instance data
  255. * Return Value: None
  256. */
  257. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  258. {
  259. struct hci_uart *hu = (void *)tty->disc_data;
  260. BT_DBG("");
  261. if (!hu)
  262. return;
  263. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  264. if (tty != hu->tty)
  265. return;
  266. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  267. hci_uart_tx_wakeup(hu);
  268. }
  269. /* hci_uart_tty_room()
  270. *
  271. * Callback function from tty driver. Return the amount of
  272. * space left in the receiver's buffer to decide if remote
  273. * transmitter is to be throttled.
  274. *
  275. * Arguments: tty pointer to associated tty instance data
  276. * Return Value: number of bytes left in receive buffer
  277. */
  278. static int hci_uart_tty_room (struct tty_struct *tty)
  279. {
  280. return 65536;
  281. }
  282. /* hci_uart_tty_receive()
  283. *
  284. * Called by tty low level driver when receive data is
  285. * available.
  286. *
  287. * Arguments: tty pointer to tty isntance data
  288. * data pointer to received data
  289. * flags pointer to flags for data
  290. * count count of received data in bytes
  291. *
  292. * Return Value: None
  293. */
  294. static void hci_uart_tty_receive(struct tty_struct *tty, const __u8 *data, char *flags, int count)
  295. {
  296. struct hci_uart *hu = (void *)tty->disc_data;
  297. if (!hu || tty != hu->tty)
  298. return;
  299. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  300. return;
  301. spin_lock(&hu->rx_lock);
  302. hu->proto->recv(hu, (void *) data, count);
  303. hu->hdev->stat.byte_rx += count;
  304. spin_unlock(&hu->rx_lock);
  305. if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver->unthrottle)
  306. tty->driver->unthrottle(tty);
  307. }
  308. static int hci_uart_register_dev(struct hci_uart *hu)
  309. {
  310. struct hci_dev *hdev;
  311. BT_DBG("");
  312. /* Initialize and register HCI device */
  313. hdev = hci_alloc_dev();
  314. if (!hdev) {
  315. BT_ERR("Can't allocate HCI device");
  316. return -ENOMEM;
  317. }
  318. hu->hdev = hdev;
  319. hdev->type = HCI_UART;
  320. hdev->driver_data = hu;
  321. hdev->open = hci_uart_open;
  322. hdev->close = hci_uart_close;
  323. hdev->flush = hci_uart_flush;
  324. hdev->send = hci_uart_send_frame;
  325. hdev->destruct = hci_uart_destruct;
  326. hdev->owner = THIS_MODULE;
  327. if (reset)
  328. set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
  329. if (hci_register_dev(hdev) < 0) {
  330. BT_ERR("Can't register HCI device");
  331. hci_free_dev(hdev);
  332. return -ENODEV;
  333. }
  334. return 0;
  335. }
  336. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  337. {
  338. struct hci_uart_proto *p;
  339. int err;
  340. p = hci_uart_get_proto(id);
  341. if (!p)
  342. return -EPROTONOSUPPORT;
  343. err = p->open(hu);
  344. if (err)
  345. return err;
  346. hu->proto = p;
  347. err = hci_uart_register_dev(hu);
  348. if (err) {
  349. p->close(hu);
  350. return err;
  351. }
  352. return 0;
  353. }
  354. /* hci_uart_tty_ioctl()
  355. *
  356. * Process IOCTL system call for the tty device.
  357. *
  358. * Arguments:
  359. *
  360. * tty pointer to tty instance data
  361. * file pointer to open file object for device
  362. * cmd IOCTL command code
  363. * arg argument for IOCTL call (cmd dependent)
  364. *
  365. * Return Value: Command dependent
  366. */
  367. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
  368. unsigned int cmd, unsigned long arg)
  369. {
  370. struct hci_uart *hu = (void *)tty->disc_data;
  371. int err = 0;
  372. BT_DBG("");
  373. /* Verify the status of the device */
  374. if (!hu)
  375. return -EBADF;
  376. switch (cmd) {
  377. case HCIUARTSETPROTO:
  378. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  379. err = hci_uart_set_proto(hu, arg);
  380. if (err) {
  381. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  382. return err;
  383. }
  384. tty->low_latency = 1;
  385. } else
  386. return -EBUSY;
  387. case HCIUARTGETPROTO:
  388. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  389. return hu->proto->id;
  390. return -EUNATCH;
  391. default:
  392. err = n_tty_ioctl(tty, file, cmd, arg);
  393. break;
  394. };
  395. return err;
  396. }
  397. /*
  398. * We don't provide read/write/poll interface for user space.
  399. */
  400. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file, unsigned char __user *buf, size_t nr)
  401. {
  402. return 0;
  403. }
  404. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
  405. {
  406. return 0;
  407. }
  408. static unsigned int hci_uart_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
  409. {
  410. return 0;
  411. }
  412. #ifdef CONFIG_BT_HCIUART_H4
  413. int h4_init(void);
  414. int h4_deinit(void);
  415. #endif
  416. #ifdef CONFIG_BT_HCIUART_BCSP
  417. int bcsp_init(void);
  418. int bcsp_deinit(void);
  419. #endif
  420. static int __init hci_uart_init(void)
  421. {
  422. static struct tty_ldisc hci_uart_ldisc;
  423. int err;
  424. BT_INFO("HCI UART driver ver %s", VERSION);
  425. /* Register the tty discipline */
  426. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  427. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  428. hci_uart_ldisc.name = "n_hci";
  429. hci_uart_ldisc.open = hci_uart_tty_open;
  430. hci_uart_ldisc.close = hci_uart_tty_close;
  431. hci_uart_ldisc.read = hci_uart_tty_read;
  432. hci_uart_ldisc.write = hci_uart_tty_write;
  433. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  434. hci_uart_ldisc.poll = hci_uart_tty_poll;
  435. hci_uart_ldisc.receive_room= hci_uart_tty_room;
  436. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  437. hci_uart_ldisc.write_wakeup= hci_uart_tty_wakeup;
  438. hci_uart_ldisc.owner = THIS_MODULE;
  439. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  440. BT_ERR("HCI line discipline registration failed. (%d)", err);
  441. return err;
  442. }
  443. #ifdef CONFIG_BT_HCIUART_H4
  444. h4_init();
  445. #endif
  446. #ifdef CONFIG_BT_HCIUART_BCSP
  447. bcsp_init();
  448. #endif
  449. return 0;
  450. }
  451. static void __exit hci_uart_exit(void)
  452. {
  453. int err;
  454. #ifdef CONFIG_BT_HCIUART_H4
  455. h4_deinit();
  456. #endif
  457. #ifdef CONFIG_BT_HCIUART_BCSP
  458. bcsp_deinit();
  459. #endif
  460. /* Release tty registration of line discipline */
  461. if ((err = tty_unregister_ldisc(N_HCI)))
  462. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  463. }
  464. module_init(hci_uart_init);
  465. module_exit(hci_uart_exit);
  466. module_param(reset, bool, 0644);
  467. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  468. MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
  469. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  470. MODULE_VERSION(VERSION);
  471. MODULE_LICENSE("GPL");
  472. MODULE_ALIAS_LDISC(N_HCI);