hci_ldisc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 int 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.cmd_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. struct tty_struct *tty = hu->tty;
  98. struct hci_dev *hdev = hu->hdev;
  99. struct sk_buff *skb;
  100. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  101. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  102. return 0;
  103. }
  104. BT_DBG("");
  105. restart:
  106. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  107. while ((skb = hci_uart_dequeue(hu))) {
  108. int len;
  109. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  110. len = tty->ops->write(tty, skb->data, skb->len);
  111. hdev->stat.byte_tx += len;
  112. skb_pull(skb, len);
  113. if (skb->len) {
  114. hu->tx_skb = skb;
  115. break;
  116. }
  117. hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
  118. kfree_skb(skb);
  119. }
  120. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  121. goto restart;
  122. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  123. return 0;
  124. }
  125. /* ------- Interface to HCI layer ------ */
  126. /* Initialize device */
  127. static int hci_uart_open(struct hci_dev *hdev)
  128. {
  129. BT_DBG("%s %p", hdev->name, hdev);
  130. /* Nothing to do for UART driver */
  131. set_bit(HCI_RUNNING, &hdev->flags);
  132. return 0;
  133. }
  134. /* Reset device */
  135. static int hci_uart_flush(struct hci_dev *hdev)
  136. {
  137. struct hci_uart *hu = (struct hci_uart *) hdev->driver_data;
  138. struct tty_struct *tty = hu->tty;
  139. BT_DBG("hdev %p tty %p", hdev, tty);
  140. if (hu->tx_skb) {
  141. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  142. }
  143. /* Flush any pending characters in the driver and discipline. */
  144. tty_ldisc_flush(tty);
  145. tty_driver_flush_buffer(tty);
  146. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  147. hu->proto->flush(hu);
  148. return 0;
  149. }
  150. /* Close device */
  151. static int hci_uart_close(struct hci_dev *hdev)
  152. {
  153. BT_DBG("hdev %p", hdev);
  154. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  155. return 0;
  156. hci_uart_flush(hdev);
  157. hdev->flush = NULL;
  158. return 0;
  159. }
  160. /* Send frames from HCI layer */
  161. static int hci_uart_send_frame(struct sk_buff *skb)
  162. {
  163. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  164. struct tty_struct *tty;
  165. struct hci_uart *hu;
  166. if (!hdev) {
  167. BT_ERR("Frame for uknown device (hdev=NULL)");
  168. return -ENODEV;
  169. }
  170. if (!test_bit(HCI_RUNNING, &hdev->flags))
  171. return -EBUSY;
  172. hu = (struct hci_uart *) hdev->driver_data;
  173. tty = hu->tty;
  174. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
  175. hu->proto->enqueue(hu, skb);
  176. hci_uart_tx_wakeup(hu);
  177. return 0;
  178. }
  179. static void hci_uart_destruct(struct hci_dev *hdev)
  180. {
  181. if (!hdev)
  182. return;
  183. BT_DBG("%s", hdev->name);
  184. kfree(hdev->driver_data);
  185. }
  186. /* ------ LDISC part ------ */
  187. /* hci_uart_tty_open
  188. *
  189. * Called when line discipline changed to HCI_UART.
  190. *
  191. * Arguments:
  192. * tty pointer to tty info structure
  193. * Return Value:
  194. * 0 if success, otherwise error code
  195. */
  196. static int hci_uart_tty_open(struct tty_struct *tty)
  197. {
  198. struct hci_uart *hu = (void *) tty->disc_data;
  199. BT_DBG("tty %p", tty);
  200. if (hu)
  201. return -EEXIST;
  202. if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  203. BT_ERR("Can't allocate control structure");
  204. return -ENFILE;
  205. }
  206. tty->disc_data = hu;
  207. hu->tty = tty;
  208. tty->receive_room = 65536;
  209. spin_lock_init(&hu->rx_lock);
  210. /* Flush any pending characters in the driver and line discipline. */
  211. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  212. open path is before the ldisc is referencable */
  213. if (tty->ldisc->ops->flush_buffer)
  214. tty->ldisc->ops->flush_buffer(tty);
  215. tty_driver_flush_buffer(tty);
  216. return 0;
  217. }
  218. /* hci_uart_tty_close()
  219. *
  220. * Called when the line discipline is changed to something
  221. * else, the tty is closed, or the tty detects a hangup.
  222. */
  223. static void hci_uart_tty_close(struct tty_struct *tty)
  224. {
  225. struct hci_uart *hu = (void *)tty->disc_data;
  226. BT_DBG("tty %p", tty);
  227. /* Detach from the tty */
  228. tty->disc_data = NULL;
  229. if (hu) {
  230. struct hci_dev *hdev = hu->hdev;
  231. if (hdev)
  232. hci_uart_close(hdev);
  233. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  234. hu->proto->close(hu);
  235. hci_unregister_dev(hdev);
  236. hci_free_dev(hdev);
  237. }
  238. }
  239. }
  240. /* hci_uart_tty_wakeup()
  241. *
  242. * Callback for transmit wakeup. Called when low level
  243. * device driver can accept more send data.
  244. *
  245. * Arguments: tty pointer to associated tty instance data
  246. * Return Value: None
  247. */
  248. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  249. {
  250. struct hci_uart *hu = (void *)tty->disc_data;
  251. BT_DBG("");
  252. if (!hu)
  253. return;
  254. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  255. if (tty != hu->tty)
  256. return;
  257. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  258. hci_uart_tx_wakeup(hu);
  259. }
  260. /* hci_uart_tty_receive()
  261. *
  262. * Called by tty low level driver when receive data is
  263. * available.
  264. *
  265. * Arguments: tty pointer to tty isntance data
  266. * data pointer to received data
  267. * flags pointer to flags for data
  268. * count count of received data in bytes
  269. *
  270. * Return Value: None
  271. */
  272. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  273. {
  274. struct hci_uart *hu = (void *)tty->disc_data;
  275. if (!hu || tty != hu->tty)
  276. return;
  277. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  278. return;
  279. spin_lock(&hu->rx_lock);
  280. hu->proto->recv(hu, (void *) data, count);
  281. hu->hdev->stat.byte_rx += count;
  282. spin_unlock(&hu->rx_lock);
  283. tty_unthrottle(tty);
  284. }
  285. static int hci_uart_register_dev(struct hci_uart *hu)
  286. {
  287. struct hci_dev *hdev;
  288. BT_DBG("");
  289. /* Initialize and register HCI device */
  290. hdev = hci_alloc_dev();
  291. if (!hdev) {
  292. BT_ERR("Can't allocate HCI device");
  293. return -ENOMEM;
  294. }
  295. hu->hdev = hdev;
  296. hdev->type = HCI_UART;
  297. hdev->driver_data = hu;
  298. hdev->open = hci_uart_open;
  299. hdev->close = hci_uart_close;
  300. hdev->flush = hci_uart_flush;
  301. hdev->send = hci_uart_send_frame;
  302. hdev->destruct = hci_uart_destruct;
  303. hdev->owner = THIS_MODULE;
  304. if (!reset)
  305. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  306. if (hci_register_dev(hdev) < 0) {
  307. BT_ERR("Can't register HCI device");
  308. hci_free_dev(hdev);
  309. return -ENODEV;
  310. }
  311. return 0;
  312. }
  313. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  314. {
  315. struct hci_uart_proto *p;
  316. int err;
  317. p = hci_uart_get_proto(id);
  318. if (!p)
  319. return -EPROTONOSUPPORT;
  320. err = p->open(hu);
  321. if (err)
  322. return err;
  323. hu->proto = p;
  324. err = hci_uart_register_dev(hu);
  325. if (err) {
  326. p->close(hu);
  327. return err;
  328. }
  329. return 0;
  330. }
  331. /* hci_uart_tty_ioctl()
  332. *
  333. * Process IOCTL system call for the tty device.
  334. *
  335. * Arguments:
  336. *
  337. * tty pointer to tty instance data
  338. * file pointer to open file object for device
  339. * cmd IOCTL command code
  340. * arg argument for IOCTL call (cmd dependent)
  341. *
  342. * Return Value: Command dependent
  343. */
  344. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
  345. unsigned int cmd, unsigned long arg)
  346. {
  347. struct hci_uart *hu = (void *)tty->disc_data;
  348. int err = 0;
  349. BT_DBG("");
  350. /* Verify the status of the device */
  351. if (!hu)
  352. return -EBADF;
  353. switch (cmd) {
  354. case HCIUARTSETPROTO:
  355. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  356. err = hci_uart_set_proto(hu, arg);
  357. if (err) {
  358. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  359. return err;
  360. }
  361. } else
  362. return -EBUSY;
  363. break;
  364. case HCIUARTGETPROTO:
  365. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  366. return hu->proto->id;
  367. return -EUNATCH;
  368. case HCIUARTGETDEVICE:
  369. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  370. return hu->hdev->id;
  371. return -EUNATCH;
  372. default:
  373. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  374. break;
  375. };
  376. return err;
  377. }
  378. /*
  379. * We don't provide read/write/poll interface for user space.
  380. */
  381. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  382. unsigned char __user *buf, size_t nr)
  383. {
  384. return 0;
  385. }
  386. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  387. const unsigned char *data, size_t count)
  388. {
  389. return 0;
  390. }
  391. static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
  392. struct file *filp, poll_table *wait)
  393. {
  394. return 0;
  395. }
  396. static int __init hci_uart_init(void)
  397. {
  398. static struct tty_ldisc_ops hci_uart_ldisc;
  399. int err;
  400. BT_INFO("HCI UART driver ver %s", VERSION);
  401. /* Register the tty discipline */
  402. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  403. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  404. hci_uart_ldisc.name = "n_hci";
  405. hci_uart_ldisc.open = hci_uart_tty_open;
  406. hci_uart_ldisc.close = hci_uart_tty_close;
  407. hci_uart_ldisc.read = hci_uart_tty_read;
  408. hci_uart_ldisc.write = hci_uart_tty_write;
  409. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  410. hci_uart_ldisc.poll = hci_uart_tty_poll;
  411. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  412. hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
  413. hci_uart_ldisc.owner = THIS_MODULE;
  414. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  415. BT_ERR("HCI line discipline registration failed. (%d)", err);
  416. return err;
  417. }
  418. #ifdef CONFIG_BT_HCIUART_H4
  419. h4_init();
  420. #endif
  421. #ifdef CONFIG_BT_HCIUART_BCSP
  422. bcsp_init();
  423. #endif
  424. #ifdef CONFIG_BT_HCIUART_LL
  425. ll_init();
  426. #endif
  427. return 0;
  428. }
  429. static void __exit hci_uart_exit(void)
  430. {
  431. int err;
  432. #ifdef CONFIG_BT_HCIUART_H4
  433. h4_deinit();
  434. #endif
  435. #ifdef CONFIG_BT_HCIUART_BCSP
  436. bcsp_deinit();
  437. #endif
  438. #ifdef CONFIG_BT_HCIUART_LL
  439. ll_deinit();
  440. #endif
  441. /* Release tty registration of line discipline */
  442. if ((err = tty_unregister_ldisc(N_HCI)))
  443. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  444. }
  445. module_init(hci_uart_init);
  446. module_exit(hci_uart_exit);
  447. module_param(reset, bool, 0644);
  448. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  449. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  450. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  451. MODULE_VERSION(VERSION);
  452. MODULE_LICENSE("GPL");
  453. MODULE_ALIAS_LDISC(N_HCI);