hci_ldisc.c 12 KB

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