hci_ldisc.c 15 KB

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