hci_ldisc.c 12 KB

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