hci_ldisc.c 13 KB

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