hci_ldisc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 <linux/firmware.h>
  41. #include <net/bluetooth/bluetooth.h>
  42. #include <net/bluetooth/hci_core.h>
  43. #include "btintel.h"
  44. #include "btbcm.h"
  45. #include "hci_uart.h"
  46. #define VERSION "2.3"
  47. static const struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  48. int hci_uart_register_proto(const struct hci_uart_proto *p)
  49. {
  50. if (p->id >= HCI_UART_MAX_PROTO)
  51. return -EINVAL;
  52. if (hup[p->id])
  53. return -EEXIST;
  54. hup[p->id] = p;
  55. BT_INFO("HCI UART protocol %s registered", p->name);
  56. return 0;
  57. }
  58. int hci_uart_unregister_proto(const struct hci_uart_proto *p)
  59. {
  60. if (p->id >= HCI_UART_MAX_PROTO)
  61. return -EINVAL;
  62. if (!hup[p->id])
  63. return -EINVAL;
  64. hup[p->id] = NULL;
  65. return 0;
  66. }
  67. static const struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  68. {
  69. if (id >= HCI_UART_MAX_PROTO)
  70. return NULL;
  71. return hup[id];
  72. }
  73. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  74. {
  75. struct hci_dev *hdev = hu->hdev;
  76. /* Update HCI stat counters */
  77. switch (pkt_type) {
  78. case HCI_COMMAND_PKT:
  79. hdev->stat.cmd_tx++;
  80. break;
  81. case HCI_ACLDATA_PKT:
  82. hdev->stat.acl_tx++;
  83. break;
  84. case HCI_SCODATA_PKT:
  85. hdev->stat.sco_tx++;
  86. break;
  87. }
  88. }
  89. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  90. {
  91. struct sk_buff *skb = hu->tx_skb;
  92. if (!skb)
  93. skb = hu->proto->dequeue(hu);
  94. else
  95. hu->tx_skb = NULL;
  96. return skb;
  97. }
  98. int hci_uart_tx_wakeup(struct hci_uart *hu)
  99. {
  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. schedule_work(&hu->write_work);
  106. return 0;
  107. }
  108. static void hci_uart_write_work(struct work_struct *work)
  109. {
  110. struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
  111. struct tty_struct *tty = hu->tty;
  112. struct hci_dev *hdev = hu->hdev;
  113. struct sk_buff *skb;
  114. /* REVISIT: should we cope with bad skbs or ->write() returning
  115. * and error value ?
  116. */
  117. restart:
  118. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  119. while ((skb = hci_uart_dequeue(hu))) {
  120. int len;
  121. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  122. len = tty->ops->write(tty, skb->data, skb->len);
  123. hdev->stat.byte_tx += len;
  124. skb_pull(skb, len);
  125. if (skb->len) {
  126. hu->tx_skb = skb;
  127. break;
  128. }
  129. hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
  130. kfree_skb(skb);
  131. }
  132. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  133. goto restart;
  134. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  135. }
  136. static void hci_uart_init_work(struct work_struct *work)
  137. {
  138. struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
  139. int err;
  140. if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  141. return;
  142. err = hci_register_dev(hu->hdev);
  143. if (err < 0) {
  144. BT_ERR("Can't register HCI device");
  145. hci_free_dev(hu->hdev);
  146. hu->hdev = NULL;
  147. hu->proto->close(hu);
  148. }
  149. set_bit(HCI_UART_REGISTERED, &hu->flags);
  150. }
  151. int hci_uart_init_ready(struct hci_uart *hu)
  152. {
  153. if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  154. return -EALREADY;
  155. schedule_work(&hu->init_ready);
  156. return 0;
  157. }
  158. /* ------- Interface to HCI layer ------ */
  159. /* Initialize device */
  160. static int hci_uart_open(struct hci_dev *hdev)
  161. {
  162. BT_DBG("%s %p", hdev->name, hdev);
  163. /* Nothing to do for UART driver */
  164. set_bit(HCI_RUNNING, &hdev->flags);
  165. return 0;
  166. }
  167. /* Reset device */
  168. static int hci_uart_flush(struct hci_dev *hdev)
  169. {
  170. struct hci_uart *hu = hci_get_drvdata(hdev);
  171. struct tty_struct *tty = hu->tty;
  172. BT_DBG("hdev %p tty %p", hdev, tty);
  173. if (hu->tx_skb) {
  174. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  175. }
  176. /* Flush any pending characters in the driver and discipline. */
  177. tty_ldisc_flush(tty);
  178. tty_driver_flush_buffer(tty);
  179. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  180. hu->proto->flush(hu);
  181. return 0;
  182. }
  183. /* Close device */
  184. static int hci_uart_close(struct hci_dev *hdev)
  185. {
  186. BT_DBG("hdev %p", hdev);
  187. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  188. return 0;
  189. hci_uart_flush(hdev);
  190. hdev->flush = NULL;
  191. return 0;
  192. }
  193. /* Send frames from HCI layer */
  194. static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  195. {
  196. struct hci_uart *hu = hci_get_drvdata(hdev);
  197. if (!test_bit(HCI_RUNNING, &hdev->flags))
  198. return -EBUSY;
  199. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
  200. hu->proto->enqueue(hu, skb);
  201. hci_uart_tx_wakeup(hu);
  202. return 0;
  203. }
  204. /* Flow control or un-flow control the device */
  205. void hci_uart_set_flow_control(struct hci_uart *hu, bool enable)
  206. {
  207. struct tty_struct *tty = hu->tty;
  208. struct ktermios ktermios;
  209. int status;
  210. unsigned int set = 0;
  211. unsigned int clear = 0;
  212. if (enable) {
  213. /* Disable hardware flow control */
  214. ktermios = tty->termios;
  215. ktermios.c_cflag &= ~CRTSCTS;
  216. status = tty_set_termios(tty, &ktermios);
  217. BT_DBG("Disabling hardware flow control: %s",
  218. status ? "failed" : "success");
  219. /* Clear RTS to prevent the device from sending */
  220. /* Most UARTs need OUT2 to enable interrupts */
  221. status = tty->driver->ops->tiocmget(tty);
  222. BT_DBG("Current tiocm 0x%x", status);
  223. set &= ~(TIOCM_OUT2 | TIOCM_RTS);
  224. clear = ~set;
  225. set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  226. TIOCM_OUT2 | TIOCM_LOOP;
  227. clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  228. TIOCM_OUT2 | TIOCM_LOOP;
  229. status = tty->driver->ops->tiocmset(tty, set, clear);
  230. BT_DBG("Clearing RTS: %s", status ? "failed" : "success");
  231. } else {
  232. /* Set RTS to allow the device to send again */
  233. status = tty->driver->ops->tiocmget(tty);
  234. BT_DBG("Current tiocm 0x%x", status);
  235. set |= (TIOCM_OUT2 | TIOCM_RTS);
  236. clear = ~set;
  237. set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  238. TIOCM_OUT2 | TIOCM_LOOP;
  239. clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  240. TIOCM_OUT2 | TIOCM_LOOP;
  241. status = tty->driver->ops->tiocmset(tty, set, clear);
  242. BT_DBG("Setting RTS: %s", status ? "failed" : "success");
  243. /* Re-enable hardware flow control */
  244. ktermios = tty->termios;
  245. ktermios.c_cflag |= CRTSCTS;
  246. status = tty_set_termios(tty, &ktermios);
  247. BT_DBG("Enabling hardware flow control: %s",
  248. status ? "failed" : "success");
  249. }
  250. }
  251. void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
  252. unsigned int oper_speed)
  253. {
  254. hu->init_speed = init_speed;
  255. hu->oper_speed = oper_speed;
  256. }
  257. void hci_uart_init_tty(struct hci_uart *hu)
  258. {
  259. struct tty_struct *tty = hu->tty;
  260. struct ktermios ktermios;
  261. /* Bring the UART into a known 8 bits no parity hw fc state */
  262. ktermios = tty->termios;
  263. ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
  264. INLCR | IGNCR | ICRNL | IXON);
  265. ktermios.c_oflag &= ~OPOST;
  266. ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  267. ktermios.c_cflag &= ~(CSIZE | PARENB);
  268. ktermios.c_cflag |= CS8;
  269. ktermios.c_cflag |= CRTSCTS;
  270. /* tty_set_termios() return not checked as it is always 0 */
  271. tty_set_termios(tty, &ktermios);
  272. }
  273. void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed)
  274. {
  275. struct tty_struct *tty = hu->tty;
  276. struct ktermios ktermios;
  277. ktermios = tty->termios;
  278. ktermios.c_cflag &= ~CBAUD;
  279. tty_termios_encode_baud_rate(&ktermios, speed, speed);
  280. /* tty_set_termios() return not checked as it is always 0 */
  281. tty_set_termios(tty, &ktermios);
  282. BT_DBG("%s: New tty speeds: %d/%d", hu->hdev->name,
  283. tty->termios.c_ispeed, tty->termios.c_ospeed);
  284. }
  285. static int hci_uart_setup(struct hci_dev *hdev)
  286. {
  287. struct hci_uart *hu = hci_get_drvdata(hdev);
  288. struct hci_rp_read_local_version *ver;
  289. struct sk_buff *skb;
  290. unsigned int speed;
  291. int err;
  292. /* Init speed if any */
  293. if (hu->init_speed)
  294. speed = hu->init_speed;
  295. else if (hu->proto->init_speed)
  296. speed = hu->proto->init_speed;
  297. else
  298. speed = 0;
  299. if (speed)
  300. hci_uart_set_baudrate(hu, speed);
  301. /* Operational speed if any */
  302. if (hu->oper_speed)
  303. speed = hu->oper_speed;
  304. else if (hu->proto->oper_speed)
  305. speed = hu->proto->oper_speed;
  306. else
  307. speed = 0;
  308. if (hu->proto->set_baudrate && speed) {
  309. err = hu->proto->set_baudrate(hu, speed);
  310. if (!err)
  311. hci_uart_set_baudrate(hu, speed);
  312. }
  313. if (hu->proto->setup)
  314. return hu->proto->setup(hu);
  315. if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
  316. return 0;
  317. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  318. HCI_INIT_TIMEOUT);
  319. if (IS_ERR(skb)) {
  320. BT_ERR("%s: Reading local version information failed (%ld)",
  321. hdev->name, PTR_ERR(skb));
  322. return 0;
  323. }
  324. if (skb->len != sizeof(*ver)) {
  325. BT_ERR("%s: Event length mismatch for version information",
  326. hdev->name);
  327. goto done;
  328. }
  329. ver = (struct hci_rp_read_local_version *)skb->data;
  330. switch (le16_to_cpu(ver->manufacturer)) {
  331. #ifdef CONFIG_BT_HCIUART_INTEL
  332. case 2:
  333. hdev->set_bdaddr = btintel_set_bdaddr;
  334. btintel_check_bdaddr(hdev);
  335. break;
  336. #endif
  337. #ifdef CONFIG_BT_HCIUART_BCM
  338. case 15:
  339. hdev->set_bdaddr = btbcm_set_bdaddr;
  340. btbcm_check_bdaddr(hdev);
  341. break;
  342. #endif
  343. }
  344. done:
  345. kfree_skb(skb);
  346. return 0;
  347. }
  348. /* ------ LDISC part ------ */
  349. /* hci_uart_tty_open
  350. *
  351. * Called when line discipline changed to HCI_UART.
  352. *
  353. * Arguments:
  354. * tty pointer to tty info structure
  355. * Return Value:
  356. * 0 if success, otherwise error code
  357. */
  358. static int hci_uart_tty_open(struct tty_struct *tty)
  359. {
  360. struct hci_uart *hu;
  361. BT_DBG("tty %p", tty);
  362. /* Error if the tty has no write op instead of leaving an exploitable
  363. hole */
  364. if (tty->ops->write == NULL)
  365. return -EOPNOTSUPP;
  366. hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
  367. if (!hu) {
  368. BT_ERR("Can't allocate control structure");
  369. return -ENFILE;
  370. }
  371. tty->disc_data = hu;
  372. hu->tty = tty;
  373. tty->receive_room = 65536;
  374. INIT_WORK(&hu->init_ready, hci_uart_init_work);
  375. INIT_WORK(&hu->write_work, hci_uart_write_work);
  376. spin_lock_init(&hu->rx_lock);
  377. /* Flush any pending characters in the driver and line discipline. */
  378. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  379. open path is before the ldisc is referencable */
  380. if (tty->ldisc->ops->flush_buffer)
  381. tty->ldisc->ops->flush_buffer(tty);
  382. tty_driver_flush_buffer(tty);
  383. return 0;
  384. }
  385. /* hci_uart_tty_close()
  386. *
  387. * Called when the line discipline is changed to something
  388. * else, the tty is closed, or the tty detects a hangup.
  389. */
  390. static void hci_uart_tty_close(struct tty_struct *tty)
  391. {
  392. struct hci_uart *hu = tty->disc_data;
  393. struct hci_dev *hdev;
  394. BT_DBG("tty %p", tty);
  395. /* Detach from the tty */
  396. tty->disc_data = NULL;
  397. if (!hu)
  398. return;
  399. hdev = hu->hdev;
  400. if (hdev)
  401. hci_uart_close(hdev);
  402. cancel_work_sync(&hu->write_work);
  403. if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  404. if (hdev) {
  405. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  406. hci_unregister_dev(hdev);
  407. hci_free_dev(hdev);
  408. }
  409. hu->proto->close(hu);
  410. }
  411. kfree(hu);
  412. }
  413. /* hci_uart_tty_wakeup()
  414. *
  415. * Callback for transmit wakeup. Called when low level
  416. * device driver can accept more send data.
  417. *
  418. * Arguments: tty pointer to associated tty instance data
  419. * Return Value: None
  420. */
  421. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  422. {
  423. struct hci_uart *hu = tty->disc_data;
  424. BT_DBG("");
  425. if (!hu)
  426. return;
  427. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  428. if (tty != hu->tty)
  429. return;
  430. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  431. hci_uart_tx_wakeup(hu);
  432. }
  433. /* hci_uart_tty_receive()
  434. *
  435. * Called by tty low level driver when receive data is
  436. * available.
  437. *
  438. * Arguments: tty pointer to tty isntance data
  439. * data pointer to received data
  440. * flags pointer to flags for data
  441. * count count of received data in bytes
  442. *
  443. * Return Value: None
  444. */
  445. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
  446. char *flags, int count)
  447. {
  448. struct hci_uart *hu = tty->disc_data;
  449. if (!hu || tty != hu->tty)
  450. return;
  451. if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
  452. return;
  453. spin_lock(&hu->rx_lock);
  454. hu->proto->recv(hu, data, count);
  455. if (hu->hdev)
  456. hu->hdev->stat.byte_rx += count;
  457. spin_unlock(&hu->rx_lock);
  458. tty_unthrottle(tty);
  459. }
  460. static int hci_uart_register_dev(struct hci_uart *hu)
  461. {
  462. struct hci_dev *hdev;
  463. BT_DBG("");
  464. /* Initialize and register HCI device */
  465. hdev = hci_alloc_dev();
  466. if (!hdev) {
  467. BT_ERR("Can't allocate HCI device");
  468. return -ENOMEM;
  469. }
  470. hu->hdev = hdev;
  471. hdev->bus = HCI_UART;
  472. hci_set_drvdata(hdev, hu);
  473. hdev->open = hci_uart_open;
  474. hdev->close = hci_uart_close;
  475. hdev->flush = hci_uart_flush;
  476. hdev->send = hci_uart_send_frame;
  477. hdev->setup = hci_uart_setup;
  478. SET_HCIDEV_DEV(hdev, hu->tty->dev);
  479. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  480. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  481. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  482. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  483. if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
  484. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  485. if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  486. hdev->dev_type = HCI_AMP;
  487. else
  488. hdev->dev_type = HCI_BREDR;
  489. if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  490. return 0;
  491. if (hci_register_dev(hdev) < 0) {
  492. BT_ERR("Can't register HCI device");
  493. hci_free_dev(hdev);
  494. return -ENODEV;
  495. }
  496. set_bit(HCI_UART_REGISTERED, &hu->flags);
  497. return 0;
  498. }
  499. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  500. {
  501. const struct hci_uart_proto *p;
  502. int err;
  503. p = hci_uart_get_proto(id);
  504. if (!p)
  505. return -EPROTONOSUPPORT;
  506. err = p->open(hu);
  507. if (err)
  508. return err;
  509. hu->proto = p;
  510. err = hci_uart_register_dev(hu);
  511. if (err) {
  512. p->close(hu);
  513. return err;
  514. }
  515. return 0;
  516. }
  517. static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
  518. {
  519. unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
  520. BIT(HCI_UART_RESET_ON_INIT) |
  521. BIT(HCI_UART_CREATE_AMP) |
  522. BIT(HCI_UART_INIT_PENDING) |
  523. BIT(HCI_UART_EXT_CONFIG) |
  524. BIT(HCI_UART_VND_DETECT);
  525. if (flags & ~valid_flags)
  526. return -EINVAL;
  527. hu->hdev_flags = flags;
  528. return 0;
  529. }
  530. /* hci_uart_tty_ioctl()
  531. *
  532. * Process IOCTL system call for the tty device.
  533. *
  534. * Arguments:
  535. *
  536. * tty pointer to tty instance data
  537. * file pointer to open file object for device
  538. * cmd IOCTL command code
  539. * arg argument for IOCTL call (cmd dependent)
  540. *
  541. * Return Value: Command dependent
  542. */
  543. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
  544. unsigned int cmd, unsigned long arg)
  545. {
  546. struct hci_uart *hu = tty->disc_data;
  547. int err = 0;
  548. BT_DBG("");
  549. /* Verify the status of the device */
  550. if (!hu)
  551. return -EBADF;
  552. switch (cmd) {
  553. case HCIUARTSETPROTO:
  554. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  555. err = hci_uart_set_proto(hu, arg);
  556. if (err) {
  557. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  558. return err;
  559. }
  560. } else
  561. return -EBUSY;
  562. break;
  563. case HCIUARTGETPROTO:
  564. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  565. return hu->proto->id;
  566. return -EUNATCH;
  567. case HCIUARTGETDEVICE:
  568. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  569. return hu->hdev->id;
  570. return -EUNATCH;
  571. case HCIUARTSETFLAGS:
  572. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  573. return -EBUSY;
  574. err = hci_uart_set_flags(hu, arg);
  575. if (err)
  576. return err;
  577. break;
  578. case HCIUARTGETFLAGS:
  579. return hu->hdev_flags;
  580. default:
  581. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  582. break;
  583. }
  584. return err;
  585. }
  586. /*
  587. * We don't provide read/write/poll interface for user space.
  588. */
  589. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  590. unsigned char __user *buf, size_t nr)
  591. {
  592. return 0;
  593. }
  594. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  595. const unsigned char *data, size_t count)
  596. {
  597. return 0;
  598. }
  599. static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
  600. struct file *filp, poll_table *wait)
  601. {
  602. return 0;
  603. }
  604. static int __init hci_uart_init(void)
  605. {
  606. static struct tty_ldisc_ops hci_uart_ldisc;
  607. int err;
  608. BT_INFO("HCI UART driver ver %s", VERSION);
  609. /* Register the tty discipline */
  610. memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
  611. hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
  612. hci_uart_ldisc.name = "n_hci";
  613. hci_uart_ldisc.open = hci_uart_tty_open;
  614. hci_uart_ldisc.close = hci_uart_tty_close;
  615. hci_uart_ldisc.read = hci_uart_tty_read;
  616. hci_uart_ldisc.write = hci_uart_tty_write;
  617. hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
  618. hci_uart_ldisc.poll = hci_uart_tty_poll;
  619. hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
  620. hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
  621. hci_uart_ldisc.owner = THIS_MODULE;
  622. err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
  623. if (err) {
  624. BT_ERR("HCI line discipline registration failed. (%d)", err);
  625. return err;
  626. }
  627. #ifdef CONFIG_BT_HCIUART_H4
  628. h4_init();
  629. #endif
  630. #ifdef CONFIG_BT_HCIUART_BCSP
  631. bcsp_init();
  632. #endif
  633. #ifdef CONFIG_BT_HCIUART_LL
  634. ll_init();
  635. #endif
  636. #ifdef CONFIG_BT_HCIUART_ATH3K
  637. ath_init();
  638. #endif
  639. #ifdef CONFIG_BT_HCIUART_3WIRE
  640. h5_init();
  641. #endif
  642. #ifdef CONFIG_BT_HCIUART_BCM
  643. bcm_init();
  644. #endif
  645. return 0;
  646. }
  647. static void __exit hci_uart_exit(void)
  648. {
  649. int err;
  650. #ifdef CONFIG_BT_HCIUART_H4
  651. h4_deinit();
  652. #endif
  653. #ifdef CONFIG_BT_HCIUART_BCSP
  654. bcsp_deinit();
  655. #endif
  656. #ifdef CONFIG_BT_HCIUART_LL
  657. ll_deinit();
  658. #endif
  659. #ifdef CONFIG_BT_HCIUART_ATH3K
  660. ath_deinit();
  661. #endif
  662. #ifdef CONFIG_BT_HCIUART_3WIRE
  663. h5_deinit();
  664. #endif
  665. #ifdef CONFIG_BT_HCIUART_BCM
  666. bcm_deinit();
  667. #endif
  668. /* Release tty registration of line discipline */
  669. err = tty_unregister_ldisc(N_HCI);
  670. if (err)
  671. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  672. }
  673. module_init(hci_uart_init);
  674. module_exit(hci_uart_exit);
  675. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  676. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  677. MODULE_VERSION(VERSION);
  678. MODULE_LICENSE("GPL");
  679. MODULE_ALIAS_LDISC(N_HCI);