hci_ldisc.c 19 KB

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