hci_ldisc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.    BlueZ - Bluetooth protocol stack for Linux
  3.    Copyright (C) 2000-2001 Qualcomm Incorporated
  4.    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License version 2 as
  7.    published by the Free Software Foundation;
  8.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9.    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10.    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11.    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12.    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
  13.    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
  14.    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
  15.    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16.    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
  17.    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
  18.    SOFTWARE IS DISCLAIMED.
  19. */
  20. /*
  21.  * BlueZ HCI UART driver.
  22.  *
  23.  * $Id: hci_ldisc.c,v 1.2 2002/04/17 17:37:20 maxk Exp $    
  24.  */
  25. #define VERSION "2.0"
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/version.h>
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/types.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/poll.h>
  37. #include <linux/slab.h>
  38. #include <linux/tty.h>
  39. #include <linux/errno.h>
  40. #include <linux/string.h>
  41. #include <linux/signal.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/skbuff.h>
  44. #include <net/bluetooth/bluetooth.h>
  45. #include <net/bluetooth/hci_core.h>
  46. #include "hci_uart.h"
  47. #ifndef HCI_UART_DEBUG
  48. #undef  BT_DBG
  49. #define BT_DBG( A... )
  50. #undef  BT_DMP
  51. #define BT_DMP( A... )
  52. #endif
  53. static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  54. int hci_uart_register_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 -EEXIST;
  60. hup[p->id] = p;
  61. return 0;
  62. }
  63. int hci_uart_unregister_proto(struct hci_uart_proto *p)
  64. {
  65. if (p->id >= HCI_UART_MAX_PROTO)
  66. return -EINVAL;
  67. if (!hup[p->id])
  68. return -EINVAL;
  69. hup[p->id] = NULL;
  70. return 0;
  71. }
  72. static struct hci_uart_proto *n_hci_get_proto(unsigned int id)
  73. {
  74. if (id >= HCI_UART_MAX_PROTO)
  75. return NULL;
  76. return hup[id];
  77. }
  78. /* ------- Interface to HCI layer ------ */
  79. /* Initialize device */
  80. static int n_hci_open(struct hci_dev *hdev)
  81. {
  82. BT_DBG("%s %p", hdev->name, hdev);
  83. /* Nothing to do for UART driver */
  84. set_bit(HCI_RUNNING, &hdev->flags);
  85. return 0;
  86. }
  87. /* Reset device */
  88. static int n_hci_flush(struct hci_dev *hdev)
  89. {
  90. struct n_hci *n_hci  = (struct n_hci *) hdev->driver_data;
  91. struct tty_struct *tty = n_hci->tty;
  92. BT_DBG("hdev %p tty %p", hdev, tty);
  93. /* Drop TX queue */
  94. skb_queue_purge(&n_hci->txq);
  95. /* Flush any pending characters in the driver and discipline. */
  96. if (tty->ldisc.flush_buffer)
  97. tty->ldisc.flush_buffer(tty);
  98. if (tty->driver.flush_buffer)
  99. tty->driver.flush_buffer(tty);
  100. if (n_hci->proto->flush)
  101. n_hci->proto->flush(n_hci);
  102. return 0;
  103. }
  104. /* Close device */
  105. static int n_hci_close(struct hci_dev *hdev)
  106. {
  107. BT_DBG("hdev %p", hdev);
  108. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  109. return 0;
  110. n_hci_flush(hdev);
  111. return 0;
  112. }
  113. static int n_hci_tx_wakeup(struct n_hci *n_hci)
  114. {
  115. struct hci_dev *hdev = &n_hci->hdev;
  116. if (test_and_set_bit(N_HCI_SENDING, &n_hci->tx_state)) {
  117. set_bit(N_HCI_TX_WAKEUP, &n_hci->tx_state);
  118. return 0;
  119. }
  120. BT_DBG("");
  121. do {
  122. register struct sk_buff *skb;
  123. register int len;
  124. clear_bit(N_HCI_TX_WAKEUP, &n_hci->tx_state);
  125. if (!(skb = skb_dequeue(&n_hci->txq)))
  126. break;
  127. len = n_hci->proto->send(n_hci, skb->data, skb->len);
  128. n_hci->hdev.stat.byte_tx += len;
  129. if (len == skb->len) {
  130. /* Complete frame was sent */
  131. switch (skb->pkt_type) {
  132. case HCI_COMMAND_PKT:
  133. hdev->stat.cmd_tx++;
  134. break;
  135. case HCI_ACLDATA_PKT:
  136. hdev->stat.acl_tx++;
  137. break;
  138. case HCI_SCODATA_PKT:
  139. hdev->stat.cmd_tx++;
  140. break;
  141. };
  142. kfree_skb(skb);
  143. } else {
  144. /* Subtract sent part and requeue  */
  145. skb_pull(skb, len);
  146. skb_queue_head(&n_hci->txq, skb);
  147. }
  148. } while (test_bit(N_HCI_TX_WAKEUP, &n_hci->tx_state));
  149. clear_bit(N_HCI_SENDING, &n_hci->tx_state);
  150. return 0;
  151. }
  152. /* Send frames from HCI layer */
  153. static int n_hci_send_frame(struct sk_buff *skb)
  154. {
  155. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  156. struct tty_struct *tty;
  157. struct n_hci *n_hci;
  158. if (!hdev) {
  159. BT_ERR("Frame for uknown device (hdev=NULL)");
  160. return -ENODEV;
  161. }
  162. if (!test_bit(HCI_RUNNING, &hdev->flags))
  163. return -EBUSY;
  164. n_hci = (struct n_hci *) hdev->driver_data;
  165. tty = n_hci->tty;
  166. BT_DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
  167. if (n_hci->proto->preq) {
  168. skb = n_hci->proto->preq(n_hci, skb);
  169. if (!skb)
  170. return 0;
  171. }
  172. skb_queue_tail(&n_hci->txq, skb);
  173. n_hci_tx_wakeup(n_hci);
  174. return 0;
  175. }
  176. static void n_hci_destruct(struct hci_dev *hdev)
  177. {
  178. struct n_hci *n_hci;
  179. if (!hdev) return;
  180. BT_DBG("%s", hdev->name);
  181. n_hci = (struct n_hci *) hdev->driver_data;
  182. kfree(n_hci);
  183. MOD_DEC_USE_COUNT;
  184. }
  185. /* ------ LDISC part ------ */
  186. /* n_hci_tty_open
  187.  * 
  188.  *     Called when line discipline changed to N_HCI.
  189.  *
  190.  * Arguments:
  191.  *     tty    pointer to tty info structure
  192.  * Return Value:    
  193.  *     0 if success, otherwise error code
  194.  */
  195. static int n_hci_tty_open(struct tty_struct *tty)
  196. {
  197. struct n_hci *n_hci = (void *)tty->disc_data;
  198. BT_DBG("tty %p", tty);
  199. if (n_hci)
  200. return -EEXIST;
  201. if (!(n_hci = kmalloc(sizeof(struct n_hci), GFP_KERNEL))) {
  202. BT_ERR("Can't allocate controll structure");
  203. return -ENFILE;
  204. }
  205. memset(n_hci, 0, sizeof(struct n_hci));
  206. tty->disc_data = n_hci;
  207. n_hci->tty = tty;
  208. spin_lock_init(&n_hci->rx_lock);
  209. skb_queue_head_init(&n_hci->txq);
  210. /* Flush any pending characters in the driver and line discipline */
  211. if (tty->ldisc.flush_buffer)
  212. tty->ldisc.flush_buffer(tty);
  213. if (tty->driver.flush_buffer)
  214. tty->driver.flush_buffer(tty);
  215. MOD_INC_USE_COUNT;
  216. return 0;
  217. }
  218. /* n_hci_tty_close()
  219.  *
  220.  *    Called when the line discipline is changed to something
  221.  *    else, the tty is closed, or the tty detects a hangup.
  222.  */
  223. static void n_hci_tty_close(struct tty_struct *tty)
  224. {
  225. struct n_hci *n_hci = (void *)tty->disc_data;
  226. BT_DBG("tty %p", tty);
  227. /* Detach from the tty */
  228. tty->disc_data = NULL;
  229. if (n_hci) {
  230. struct hci_dev *hdev = &n_hci->hdev;
  231. n_hci_close(hdev);
  232. if (test_and_clear_bit(N_HCI_PROTO_SET, &n_hci->flags)) {
  233. n_hci->proto->close(n_hci);
  234. hci_unregister_dev(hdev);
  235. }
  236. MOD_DEC_USE_COUNT;
  237. }
  238. }
  239. /* n_hci_tty_wakeup()
  240.  *
  241.  *    Callback for transmit wakeup. Called when low level
  242.  *    device driver can accept more send data.
  243.  *
  244.  * Arguments:        tty    pointer to associated tty instance data
  245.  * Return Value:    None
  246.  */
  247. static void n_hci_tty_wakeup( struct tty_struct *tty )
  248. {
  249. struct n_hci *n_hci = (void *)tty->disc_data;
  250. BT_DBG("");
  251. if (!n_hci)
  252. return;
  253. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  254. if (tty != n_hci->tty)
  255. return;
  256. n_hci_tx_wakeup(n_hci);
  257. }
  258. /* n_hci_tty_room()
  259.  * 
  260.  *    Callback function from tty driver. Return the amount of 
  261.  *    space left in the receiver's buffer to decide if remote
  262.  *    transmitter is to be throttled.
  263.  *
  264.  * Arguments:        tty    pointer to associated tty instance data
  265.  * Return Value:    number of bytes left in receive buffer
  266.  */
  267. static int n_hci_tty_room (struct tty_struct *tty)
  268. {
  269. return 65536;
  270. }
  271. /* n_hci_tty_receive()
  272.  * 
  273.  *     Called by tty low level driver when receive data is
  274.  *     available.
  275.  *     
  276.  * Arguments:  tty          pointer to tty isntance data
  277.  *             data         pointer to received data
  278.  *             flags        pointer to flags for data
  279.  *             count        count of received data in bytes
  280.  *     
  281.  * Return Value:    None
  282.  */
  283. static void n_hci_tty_receive(struct tty_struct *tty, const __u8 * data, char *flags, int count)
  284. {
  285. struct n_hci *n_hci = (void *)tty->disc_data;
  286. if (!n_hci || tty != n_hci->tty)
  287. return;
  288. if (!test_bit(N_HCI_PROTO_SET, &n_hci->flags))
  289. return;
  290. spin_lock(&n_hci->rx_lock);
  291. n_hci->proto->recv(n_hci, (void *) data, count);
  292. n_hci->hdev.stat.byte_rx += count;
  293. spin_unlock(&n_hci->rx_lock);
  294. if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle)
  295. tty->driver.unthrottle(tty);
  296. }
  297. static int n_hci_register_dev(struct n_hci *n_hci)
  298. {
  299. struct hci_dev *hdev;
  300. BT_DBG("");
  301. /* Initialize and register HCI device */
  302. hdev = &n_hci->hdev;
  303. hdev->type = HCI_UART;
  304. hdev->driver_data = n_hci;
  305. hdev->open  = n_hci_open;
  306. hdev->close = n_hci_close;
  307. hdev->flush = n_hci_flush;
  308. hdev->send  = n_hci_send_frame;
  309. hdev->destruct = n_hci_destruct;
  310. if (hci_register_dev(hdev) < 0) {
  311. BT_ERR("Can't register HCI device %s", hdev->name);
  312. return -ENODEV;
  313. }
  314. MOD_INC_USE_COUNT;
  315. return 0;
  316. }
  317. static int n_hci_set_proto(struct n_hci *n_hci, int id)
  318. {
  319. struct hci_uart_proto *p;
  320. int err;
  321. p = n_hci_get_proto(id);
  322. if (!p)
  323. return -EPROTONOSUPPORT;
  324. err = p->open(n_hci);
  325. if (err)
  326. return err;
  327. n_hci->proto = p;
  328. err = n_hci_register_dev(n_hci);
  329. if (err) {
  330. p->close(n_hci);
  331. return err;
  332. }
  333. return 0;
  334. }
  335. /* n_hci_tty_ioctl()
  336.  *
  337.  *    Process IOCTL system call for the tty device.
  338.  *
  339.  * Arguments:
  340.  *
  341.  *    tty        pointer to tty instance data
  342.  *    file       pointer to open file object for device
  343.  *    cmd        IOCTL command code
  344.  *    arg        argument for IOCTL call (cmd dependent)
  345.  *
  346.  * Return Value:    Command dependent
  347.  */
  348. static int n_hci_tty_ioctl(struct tty_struct *tty, struct file * file,
  349.                             unsigned int cmd, unsigned long arg)
  350. {
  351. struct n_hci *n_hci = (void *)tty->disc_data;
  352. int err = 0;
  353. BT_DBG("");
  354. /* Verify the status of the device */
  355. if (!n_hci)
  356. return -EBADF;
  357. switch (cmd) {
  358. case HCIUARTSETPROTO:
  359. if (!test_and_set_bit(N_HCI_PROTO_SET, &n_hci->flags)) {
  360. err = n_hci_set_proto(n_hci, arg);
  361. if (err) {
  362. clear_bit(N_HCI_PROTO_SET, &n_hci->flags);
  363. return err;
  364. }
  365. tty->low_latency = 1;
  366. } else
  367. return -EBUSY;
  368. case HCIUARTGETPROTO:
  369. if (test_bit(N_HCI_PROTO_SET, &n_hci->flags))
  370. return n_hci->proto->id;
  371. return -EUNATCH;
  372. default:
  373. err = n_tty_ioctl(tty, file, cmd, arg);
  374. break;
  375. };
  376. return err;
  377. }
  378. /*
  379.  * We don't provide read/write/poll interface for user space.
  380.  */
  381. static ssize_t n_hci_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr)
  382. {
  383. return 0;
  384. }
  385. static ssize_t n_hci_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
  386. {
  387. return 0;
  388. }
  389. static unsigned int n_hci_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
  390. {
  391. return 0;
  392. }
  393. #ifdef CONFIG_BLUEZ_HCIUART_H4
  394. int h4_init(void);
  395. int h4_deinit(void);
  396. #endif
  397. int __init n_hci_init(void)
  398. {
  399. static struct tty_ldisc n_hci_ldisc;
  400. int err;
  401. BT_INFO("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", 
  402. VERSION);
  403. BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
  404. /* Register the tty discipline */
  405. memset(&n_hci_ldisc, 0, sizeof (n_hci_ldisc));
  406. n_hci_ldisc.magic       = TTY_LDISC_MAGIC;
  407. n_hci_ldisc.name        = "n_hci";
  408. n_hci_ldisc.open        = n_hci_tty_open;
  409. n_hci_ldisc.close       = n_hci_tty_close;
  410. n_hci_ldisc.read        = n_hci_tty_read;
  411. n_hci_ldisc.write       = n_hci_tty_write;
  412. n_hci_ldisc.ioctl       = n_hci_tty_ioctl;
  413. n_hci_ldisc.poll        = n_hci_tty_poll;
  414. n_hci_ldisc.receive_room= n_hci_tty_room;
  415. n_hci_ldisc.receive_buf = n_hci_tty_receive;
  416. n_hci_ldisc.write_wakeup= n_hci_tty_wakeup;
  417. if ((err = tty_register_ldisc(N_HCI, &n_hci_ldisc))) {
  418. BT_ERR("Can't register HCI line discipline (%d)", err);
  419. return err;
  420. }
  421. #ifdef CONFIG_BLUEZ_HCIUART_H4
  422. h4_init();
  423. #endif
  424. return 0;
  425. }
  426. void n_hci_cleanup(void)
  427. {
  428. int err;
  429. #ifdef CONFIG_BLUEZ_HCIUART_H4
  430. h4_deinit();
  431. #endif
  432. /* Release tty registration of line discipline */
  433. if ((err = tty_register_ldisc(N_HCI, NULL)))
  434. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  435. }
  436. module_init(n_hci_init);
  437. module_exit(n_hci_cleanup);
  438. MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
  439. MODULE_DESCRIPTION("BlueZ HCI UART driver ver " VERSION);
  440. MODULE_LICENSE("GPL");