PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/bluetooth/hci_ldisc.c

https://github.com/VorkTeam/vorkKernel-DESIRE
C | 585 lines | 364 code | 126 blank | 95 comment | 47 complexity | cdf8a9818b9046cf89382bf74082abe4 MD5 | raw file
  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 unknown 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. /* FIXME: This btw is bogus, nothing requires the old ldisc to clear
  201. the pointer */
  202. if (hu)
  203. return -EEXIST;
  204. /* Error if the tty has no write op instead of leaving an exploitable
  205. hole */
  206. if (tty->ops->write == NULL)
  207. return -EOPNOTSUPP;
  208. if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  209. BT_ERR("Can't allocate control structure");
  210. return -ENFILE;
  211. }
  212. tty->disc_data = hu;
  213. hu->tty = tty;
  214. tty->receive_room = 65536;
  215. spin_lock_init(&hu->rx_lock);
  216. /* Flush any pending characters in the driver and line discipline. */
  217. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  218. open path is before the ldisc is referencable */
  219. if (tty->ldisc->ops->flush_buffer)
  220. tty->ldisc->ops->flush_buffer(tty);
  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. if (hdev)
  238. hci_uart_close(hdev);
  239. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  240. hu->proto->close(hu);
  241. hci_unregister_dev(hdev);
  242. hci_free_dev(hdev);
  243. }
  244. }
  245. }
  246. /* hci_uart_tty_wakeup()
  247. *
  248. * Callback for transmit wakeup. Called when low level
  249. * device driver can accept more send data.
  250. *
  251. * Arguments: tty pointer to associated tty instance data
  252. * Return Value: None
  253. */
  254. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  255. {
  256. struct hci_uart *hu = (void *)tty->disc_data;
  257. BT_DBG("");
  258. if (!hu)
  259. return;
  260. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  261. if (tty != hu->tty)
  262. return;
  263. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  264. hci_uart_tx_wakeup(hu);
  265. }
  266. /* hci_uart_tty_receive()
  267. *
  268. * Called by tty low level driver when receive data is
  269. * available.
  270. *
  271. * Arguments: tty pointer to tty isntance data
  272. * data pointer to received data
  273. * flags pointer to flags for data
  274. * count count of received data in bytes
  275. *
  276. * Return Value: None
  277. */
  278. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  279. {
  280. struct hci_uart *hu = (void *)tty->disc_data;
  281. if (!hu || tty != hu->tty)
  282. return;
  283. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  284. return;
  285. spin_lock(&hu->rx_lock);
  286. hu->proto->recv(hu, (void *) data, count);
  287. hu->hdev->stat.byte_rx += count;
  288. spin_unlock(&hu->rx_lock);
  289. tty_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->bus = 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_NO_RESET, &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. } else
  368. return -EBUSY;
  369. break;
  370. case HCIUARTGETPROTO:
  371. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  372. return hu->proto->id;
  373. return -EUNATCH;
  374. case HCIUARTGETDEVICE:
  375. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  376. return hu->hdev->id;
  377. return -EUNATCH;
  378. default:
  379. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  380. break;
  381. };
  382. return err;
  383. }
  384. /*
  385. * We don't provide read/write/poll interface for user space.
  386. */
  387. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  388. unsigned char __user *buf, size_t nr)
  389. {
  390. return 0;
  391. }
  392. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  393. const unsigned char *data, size_t count)
  394. {
  395. return 0;
  396. }
  397. static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
  398. struct file *filp, poll_table *wait)
  399. {
  400. return 0;
  401. }
  402. static int __init hci_uart_init(void)
  403. {
  404. static struct tty_ldisc_ops hci_uart_ldisc;
  405. int err;
  406. BT_INFO("HCI UART driver ver %s", VERSION);
  407. /* Register the tty discipline */
  408. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  409. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  410. hci_uart_ldisc.name = "n_hci";
  411. hci_uart_ldisc.open = hci_uart_tty_open;
  412. hci_uart_ldisc.close = hci_uart_tty_close;
  413. hci_uart_ldisc.read = hci_uart_tty_read;
  414. hci_uart_ldisc.write = hci_uart_tty_write;
  415. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  416. hci_uart_ldisc.poll = hci_uart_tty_poll;
  417. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  418. hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
  419. hci_uart_ldisc.owner = THIS_MODULE;
  420. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  421. BT_ERR("HCI line discipline registration failed. (%d)", err);
  422. return err;
  423. }
  424. #ifdef CONFIG_BT_HCIUART_H4
  425. h4_init();
  426. #endif
  427. #ifdef CONFIG_BT_HCIUART_BCSP
  428. bcsp_init();
  429. #endif
  430. #ifdef CONFIG_BT_HCIUART_LL
  431. ll_init();
  432. #endif
  433. return 0;
  434. }
  435. static void __exit hci_uart_exit(void)
  436. {
  437. int err;
  438. #ifdef CONFIG_BT_HCIUART_H4
  439. h4_deinit();
  440. #endif
  441. #ifdef CONFIG_BT_HCIUART_BCSP
  442. bcsp_deinit();
  443. #endif
  444. #ifdef CONFIG_BT_HCIUART_LL
  445. ll_deinit();
  446. #endif
  447. /* Release tty registration of line discipline */
  448. if ((err = tty_unregister_ldisc(N_HCI)))
  449. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  450. }
  451. module_init(hci_uart_init);
  452. module_exit(hci_uart_exit);
  453. module_param(reset, bool, 0644);
  454. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  455. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  456. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  457. MODULE_VERSION(VERSION);
  458. MODULE_LICENSE("GPL");
  459. MODULE_ALIAS_LDISC(N_HCI);