hci_ldisc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth HCI UART driver
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/signal.h>
  23. #include <linux/ioctl.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/firmware.h>
  26. #include <linux/serdev.h>
  27. #include <net/bluetooth/bluetooth.h>
  28. #include <net/bluetooth/hci_core.h>
  29. #include "btintel.h"
  30. #include "btbcm.h"
  31. #include "hci_uart.h"
  32. #define VERSION "2.3"
  33. static const struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  34. int hci_uart_register_proto(const struct hci_uart_proto *p)
  35. {
  36. if (p->id >= HCI_UART_MAX_PROTO)
  37. return -EINVAL;
  38. if (hup[p->id])
  39. return -EEXIST;
  40. hup[p->id] = p;
  41. BT_INFO("HCI UART protocol %s registered", p->name);
  42. return 0;
  43. }
  44. int hci_uart_unregister_proto(const struct hci_uart_proto *p)
  45. {
  46. if (p->id >= HCI_UART_MAX_PROTO)
  47. return -EINVAL;
  48. if (!hup[p->id])
  49. return -EINVAL;
  50. hup[p->id] = NULL;
  51. return 0;
  52. }
  53. static const struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  54. {
  55. if (id >= HCI_UART_MAX_PROTO)
  56. return NULL;
  57. return hup[id];
  58. }
  59. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  60. {
  61. struct hci_dev *hdev = hu->hdev;
  62. /* Update HCI stat counters */
  63. switch (pkt_type) {
  64. case HCI_COMMAND_PKT:
  65. hdev->stat.cmd_tx++;
  66. break;
  67. case HCI_ACLDATA_PKT:
  68. hdev->stat.acl_tx++;
  69. break;
  70. case HCI_SCODATA_PKT:
  71. hdev->stat.sco_tx++;
  72. break;
  73. }
  74. }
  75. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  76. {
  77. struct sk_buff *skb = hu->tx_skb;
  78. if (!skb) {
  79. percpu_down_read(&hu->proto_lock);
  80. if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
  81. test_bit(HCI_UART_PROTO_INIT, &hu->flags))
  82. skb = hu->proto->dequeue(hu);
  83. percpu_up_read(&hu->proto_lock);
  84. } else {
  85. hu->tx_skb = NULL;
  86. }
  87. return skb;
  88. }
  89. int hci_uart_tx_wakeup(struct hci_uart *hu)
  90. {
  91. /* This may be called in an IRQ context, so we can't sleep. Therefore
  92. * we try to acquire the lock only, and if that fails we assume the
  93. * tty is being closed because that is the only time the write lock is
  94. * acquired. If, however, at some point in the future the write lock
  95. * is also acquired in other situations, then this must be revisited.
  96. */
  97. if (!percpu_down_read_trylock(&hu->proto_lock))
  98. return 0;
  99. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
  100. !test_bit(HCI_UART_PROTO_INIT, &hu->flags))
  101. goto no_schedule;
  102. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  103. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
  104. goto no_schedule;
  105. BT_DBG("");
  106. schedule_work(&hu->write_work);
  107. no_schedule:
  108. percpu_up_read(&hu->proto_lock);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(hci_uart_tx_wakeup);
  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. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  137. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  138. goto restart;
  139. wake_up_bit(&hu->tx_state, HCI_UART_SENDING);
  140. }
  141. void hci_uart_init_work(struct work_struct *work)
  142. {
  143. struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
  144. int err;
  145. struct hci_dev *hdev;
  146. if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  147. return;
  148. err = hci_register_dev(hu->hdev);
  149. if (err < 0) {
  150. BT_ERR("Can't register HCI device");
  151. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  152. hu->proto->close(hu);
  153. hdev = hu->hdev;
  154. hu->hdev = NULL;
  155. hci_free_dev(hdev);
  156. return;
  157. }
  158. set_bit(HCI_UART_REGISTERED, &hu->flags);
  159. }
  160. int hci_uart_init_ready(struct hci_uart *hu)
  161. {
  162. if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  163. return -EALREADY;
  164. schedule_work(&hu->init_ready);
  165. return 0;
  166. }
  167. int hci_uart_wait_until_sent(struct hci_uart *hu)
  168. {
  169. return wait_on_bit_timeout(&hu->tx_state, HCI_UART_SENDING,
  170. TASK_INTERRUPTIBLE,
  171. msecs_to_jiffies(2000));
  172. }
  173. /* ------- Interface to HCI layer ------ */
  174. /* Reset device */
  175. static int hci_uart_flush(struct hci_dev *hdev)
  176. {
  177. struct hci_uart *hu = hci_get_drvdata(hdev);
  178. struct tty_struct *tty = hu->tty;
  179. BT_DBG("hdev %p tty %p", hdev, tty);
  180. if (hu->tx_skb) {
  181. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  182. }
  183. /* Flush any pending characters in the driver and discipline. */
  184. tty_ldisc_flush(tty);
  185. tty_driver_flush_buffer(tty);
  186. percpu_down_read(&hu->proto_lock);
  187. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  188. hu->proto->flush(hu);
  189. percpu_up_read(&hu->proto_lock);
  190. return 0;
  191. }
  192. /* Initialize device */
  193. static int hci_uart_open(struct hci_dev *hdev)
  194. {
  195. BT_DBG("%s %p", hdev->name, hdev);
  196. /* Undo clearing this from hci_uart_close() */
  197. hdev->flush = hci_uart_flush;
  198. return 0;
  199. }
  200. /* Close device */
  201. static int hci_uart_close(struct hci_dev *hdev)
  202. {
  203. BT_DBG("hdev %p", hdev);
  204. hci_uart_flush(hdev);
  205. hdev->flush = NULL;
  206. return 0;
  207. }
  208. /* Send frames from HCI layer */
  209. static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  210. {
  211. struct hci_uart *hu = hci_get_drvdata(hdev);
  212. BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
  213. skb->len);
  214. percpu_down_read(&hu->proto_lock);
  215. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
  216. !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
  217. percpu_up_read(&hu->proto_lock);
  218. return -EUNATCH;
  219. }
  220. hu->proto->enqueue(hu, skb);
  221. percpu_up_read(&hu->proto_lock);
  222. hci_uart_tx_wakeup(hu);
  223. return 0;
  224. }
  225. /* Check the underlying device or tty has flow control support */
  226. bool hci_uart_has_flow_control(struct hci_uart *hu)
  227. {
  228. /* serdev nodes check if the needed operations are present */
  229. if (hu->serdev)
  230. return true;
  231. if (hu->tty->driver->ops->tiocmget && hu->tty->driver->ops->tiocmset)
  232. return true;
  233. return false;
  234. }
  235. /* Flow control or un-flow control the device */
  236. void hci_uart_set_flow_control(struct hci_uart *hu, bool enable)
  237. {
  238. struct tty_struct *tty = hu->tty;
  239. struct ktermios ktermios;
  240. int status;
  241. unsigned int set = 0;
  242. unsigned int clear = 0;
  243. if (hu->serdev) {
  244. serdev_device_set_flow_control(hu->serdev, !enable);
  245. serdev_device_set_rts(hu->serdev, !enable);
  246. return;
  247. }
  248. if (enable) {
  249. /* Disable hardware flow control */
  250. ktermios = tty->termios;
  251. ktermios.c_cflag &= ~CRTSCTS;
  252. tty_set_termios(tty, &ktermios);
  253. BT_DBG("Disabling hardware flow control: %s",
  254. (tty->termios.c_cflag & CRTSCTS) ? "failed" : "success");
  255. /* Clear RTS to prevent the device from sending */
  256. /* Most UARTs need OUT2 to enable interrupts */
  257. status = tty->driver->ops->tiocmget(tty);
  258. BT_DBG("Current tiocm 0x%x", status);
  259. set &= ~(TIOCM_OUT2 | TIOCM_RTS);
  260. clear = ~set;
  261. set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  262. TIOCM_OUT2 | TIOCM_LOOP;
  263. clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  264. TIOCM_OUT2 | TIOCM_LOOP;
  265. status = tty->driver->ops->tiocmset(tty, set, clear);
  266. BT_DBG("Clearing RTS: %s", status ? "failed" : "success");
  267. } else {
  268. /* Set RTS to allow the device to send again */
  269. status = tty->driver->ops->tiocmget(tty);
  270. BT_DBG("Current tiocm 0x%x", status);
  271. set |= (TIOCM_OUT2 | TIOCM_RTS);
  272. clear = ~set;
  273. set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  274. TIOCM_OUT2 | TIOCM_LOOP;
  275. clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
  276. TIOCM_OUT2 | TIOCM_LOOP;
  277. status = tty->driver->ops->tiocmset(tty, set, clear);
  278. BT_DBG("Setting RTS: %s", status ? "failed" : "success");
  279. /* Re-enable hardware flow control */
  280. ktermios = tty->termios;
  281. ktermios.c_cflag |= CRTSCTS;
  282. tty_set_termios(tty, &ktermios);
  283. BT_DBG("Enabling hardware flow control: %s",
  284. !(tty->termios.c_cflag & CRTSCTS) ? "failed" : "success");
  285. }
  286. }
  287. void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
  288. unsigned int oper_speed)
  289. {
  290. hu->init_speed = init_speed;
  291. hu->oper_speed = oper_speed;
  292. }
  293. void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed)
  294. {
  295. struct tty_struct *tty = hu->tty;
  296. struct ktermios ktermios;
  297. ktermios = tty->termios;
  298. ktermios.c_cflag &= ~CBAUD;
  299. tty_termios_encode_baud_rate(&ktermios, speed, speed);
  300. /* tty_set_termios() return not checked as it is always 0 */
  301. tty_set_termios(tty, &ktermios);
  302. BT_DBG("%s: New tty speeds: %d/%d", hu->hdev->name,
  303. tty->termios.c_ispeed, tty->termios.c_ospeed);
  304. }
  305. static int hci_uart_setup(struct hci_dev *hdev)
  306. {
  307. struct hci_uart *hu = hci_get_drvdata(hdev);
  308. struct hci_rp_read_local_version *ver;
  309. struct sk_buff *skb;
  310. unsigned int speed;
  311. int err;
  312. /* Init speed if any */
  313. if (hu->init_speed)
  314. speed = hu->init_speed;
  315. else if (hu->proto->init_speed)
  316. speed = hu->proto->init_speed;
  317. else
  318. speed = 0;
  319. if (speed)
  320. hci_uart_set_baudrate(hu, speed);
  321. /* Operational speed if any */
  322. if (hu->oper_speed)
  323. speed = hu->oper_speed;
  324. else if (hu->proto->oper_speed)
  325. speed = hu->proto->oper_speed;
  326. else
  327. speed = 0;
  328. if (hu->proto->set_baudrate && speed) {
  329. err = hu->proto->set_baudrate(hu, speed);
  330. if (!err)
  331. hci_uart_set_baudrate(hu, speed);
  332. }
  333. if (hu->proto->setup)
  334. return hu->proto->setup(hu);
  335. if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
  336. return 0;
  337. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  338. HCI_INIT_TIMEOUT);
  339. if (IS_ERR(skb)) {
  340. BT_ERR("%s: Reading local version information failed (%ld)",
  341. hdev->name, PTR_ERR(skb));
  342. return 0;
  343. }
  344. if (skb->len != sizeof(*ver)) {
  345. BT_ERR("%s: Event length mismatch for version information",
  346. hdev->name);
  347. goto done;
  348. }
  349. ver = (struct hci_rp_read_local_version *)skb->data;
  350. switch (le16_to_cpu(ver->manufacturer)) {
  351. #ifdef CONFIG_BT_HCIUART_INTEL
  352. case 2:
  353. hdev->set_bdaddr = btintel_set_bdaddr;
  354. btintel_check_bdaddr(hdev);
  355. break;
  356. #endif
  357. #ifdef CONFIG_BT_HCIUART_BCM
  358. case 15:
  359. hdev->set_bdaddr = btbcm_set_bdaddr;
  360. btbcm_check_bdaddr(hdev);
  361. break;
  362. #endif
  363. default:
  364. break;
  365. }
  366. done:
  367. kfree_skb(skb);
  368. return 0;
  369. }
  370. /* ------ LDISC part ------ */
  371. /* hci_uart_tty_open
  372. *
  373. * Called when line discipline changed to HCI_UART.
  374. *
  375. * Arguments:
  376. * tty pointer to tty info structure
  377. * Return Value:
  378. * 0 if success, otherwise error code
  379. */
  380. static int hci_uart_tty_open(struct tty_struct *tty)
  381. {
  382. struct hci_uart *hu;
  383. BT_DBG("tty %p", tty);
  384. if (!capable(CAP_NET_ADMIN))
  385. return -EPERM;
  386. /* Error if the tty has no write op instead of leaving an exploitable
  387. * hole
  388. */
  389. if (tty->ops->write == NULL)
  390. return -EOPNOTSUPP;
  391. hu = kzalloc_obj(*hu);
  392. if (!hu) {
  393. BT_ERR("Can't allocate control structure");
  394. return -ENFILE;
  395. }
  396. if (percpu_init_rwsem(&hu->proto_lock)) {
  397. BT_ERR("Can't allocate semaphore structure");
  398. kfree(hu);
  399. return -ENOMEM;
  400. }
  401. tty->disc_data = hu;
  402. hu->tty = tty;
  403. tty->receive_room = 65536;
  404. /* disable alignment support by default */
  405. hu->alignment = 1;
  406. hu->padding = 0;
  407. /* Use serial port speed as oper_speed */
  408. hu->oper_speed = tty->termios.c_ospeed;
  409. INIT_WORK(&hu->init_ready, hci_uart_init_work);
  410. INIT_WORK(&hu->write_work, hci_uart_write_work);
  411. /* Flush any pending characters in the driver */
  412. tty_driver_flush_buffer(tty);
  413. return 0;
  414. }
  415. /* hci_uart_tty_close()
  416. *
  417. * Called when the line discipline is changed to something
  418. * else, the tty is closed, or the tty detects a hangup.
  419. */
  420. static void hci_uart_tty_close(struct tty_struct *tty)
  421. {
  422. struct hci_uart *hu = tty->disc_data;
  423. struct hci_dev *hdev;
  424. BT_DBG("tty %p", tty);
  425. /* Detach from the tty */
  426. tty->disc_data = NULL;
  427. if (!hu)
  428. return;
  429. hdev = hu->hdev;
  430. if (hdev)
  431. hci_uart_close(hdev);
  432. if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  433. percpu_down_write(&hu->proto_lock);
  434. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  435. percpu_up_write(&hu->proto_lock);
  436. cancel_work_sync(&hu->init_ready);
  437. cancel_work_sync(&hu->write_work);
  438. if (hdev) {
  439. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  440. hci_unregister_dev(hdev);
  441. hci_free_dev(hdev);
  442. }
  443. hu->proto->close(hu);
  444. }
  445. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  446. percpu_free_rwsem(&hu->proto_lock);
  447. kfree(hu);
  448. }
  449. /* hci_uart_tty_wakeup()
  450. *
  451. * Callback for transmit wakeup. Called when low level
  452. * device driver can accept more send data.
  453. *
  454. * Arguments: tty pointer to associated tty instance data
  455. * Return Value: None
  456. */
  457. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  458. {
  459. struct hci_uart *hu = tty->disc_data;
  460. BT_DBG("");
  461. if (!hu)
  462. return;
  463. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  464. if (tty != hu->tty)
  465. return;
  466. if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
  467. test_bit(HCI_UART_PROTO_INIT, &hu->flags))
  468. hci_uart_tx_wakeup(hu);
  469. }
  470. /* hci_uart_tty_receive()
  471. *
  472. * Called by tty low level driver when receive data is
  473. * available.
  474. *
  475. * Arguments: tty pointer to tty instance data
  476. * data pointer to received data
  477. * flags pointer to flags for data
  478. * count count of received data in bytes
  479. *
  480. * Return Value: None
  481. */
  482. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
  483. const u8 *flags, size_t count)
  484. {
  485. struct hci_uart *hu = tty->disc_data;
  486. if (!hu || tty != hu->tty)
  487. return;
  488. percpu_down_read(&hu->proto_lock);
  489. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
  490. !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
  491. percpu_up_read(&hu->proto_lock);
  492. return;
  493. }
  494. /* It does not need a lock here as it is already protected by a mutex in
  495. * tty caller
  496. */
  497. hu->proto->recv(hu, data, count);
  498. percpu_up_read(&hu->proto_lock);
  499. if (hu->hdev)
  500. hu->hdev->stat.byte_rx += count;
  501. tty_unthrottle(tty);
  502. }
  503. static int hci_uart_register_dev(struct hci_uart *hu)
  504. {
  505. struct hci_dev *hdev;
  506. int err;
  507. BT_DBG("");
  508. /* Initialize and register HCI device */
  509. hdev = hci_alloc_dev();
  510. if (!hdev) {
  511. BT_ERR("Can't allocate HCI device");
  512. return -ENOMEM;
  513. }
  514. hu->hdev = hdev;
  515. hdev->bus = HCI_UART;
  516. hci_set_drvdata(hdev, hu);
  517. /* Only when vendor specific setup callback is provided, consider
  518. * the manufacturer information valid. This avoids filling in the
  519. * value for Ericsson when nothing is specified.
  520. */
  521. if (hu->proto->setup)
  522. hdev->manufacturer = hu->proto->manufacturer;
  523. hdev->open = hci_uart_open;
  524. hdev->close = hci_uart_close;
  525. hdev->flush = hci_uart_flush;
  526. hdev->send = hci_uart_send_frame;
  527. hdev->setup = hci_uart_setup;
  528. SET_HCIDEV_DEV(hdev, hu->tty->dev);
  529. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  530. hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
  531. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  532. hci_set_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG);
  533. if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
  534. hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
  535. /* Only call open() for the protocol after hdev is fully initialized as
  536. * open() (or a timer/workqueue it starts) may attempt to reference it.
  537. */
  538. err = hu->proto->open(hu);
  539. if (err) {
  540. hu->hdev = NULL;
  541. hci_free_dev(hdev);
  542. return err;
  543. }
  544. set_bit(HCI_UART_PROTO_INIT, &hu->flags);
  545. if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  546. return 0;
  547. if (hci_register_dev(hdev) < 0) {
  548. BT_ERR("Can't register HCI device");
  549. hu->proto->close(hu);
  550. hu->hdev = NULL;
  551. hci_free_dev(hdev);
  552. return -ENODEV;
  553. }
  554. set_bit(HCI_UART_REGISTERED, &hu->flags);
  555. return 0;
  556. }
  557. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  558. {
  559. const struct hci_uart_proto *p;
  560. int err;
  561. p = hci_uart_get_proto(id);
  562. if (!p)
  563. return -EPROTONOSUPPORT;
  564. hu->proto = p;
  565. err = hci_uart_register_dev(hu);
  566. if (err) {
  567. return err;
  568. }
  569. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  570. clear_bit(HCI_UART_PROTO_INIT, &hu->flags);
  571. return 0;
  572. }
  573. static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
  574. {
  575. unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
  576. BIT(HCI_UART_RESET_ON_INIT) |
  577. BIT(HCI_UART_INIT_PENDING) |
  578. BIT(HCI_UART_EXT_CONFIG) |
  579. BIT(HCI_UART_VND_DETECT);
  580. if (flags & ~valid_flags)
  581. return -EINVAL;
  582. hu->hdev_flags = flags;
  583. return 0;
  584. }
  585. /* hci_uart_tty_ioctl()
  586. *
  587. * Process IOCTL system call for the tty device.
  588. *
  589. * Arguments:
  590. *
  591. * tty pointer to tty instance data
  592. * cmd IOCTL command code
  593. * arg argument for IOCTL call (cmd dependent)
  594. *
  595. * Return Value: Command dependent
  596. */
  597. static int hci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  598. unsigned long arg)
  599. {
  600. struct hci_uart *hu = tty->disc_data;
  601. int err = 0;
  602. BT_DBG("");
  603. /* Verify the status of the device */
  604. if (!hu)
  605. return -EBADF;
  606. switch (cmd) {
  607. case HCIUARTSETPROTO:
  608. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  609. err = hci_uart_set_proto(hu, arg);
  610. if (err)
  611. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  612. } else
  613. err = -EBUSY;
  614. break;
  615. case HCIUARTGETPROTO:
  616. if (test_bit(HCI_UART_PROTO_SET, &hu->flags) &&
  617. test_bit(HCI_UART_PROTO_READY, &hu->flags))
  618. err = hu->proto->id;
  619. else
  620. err = -EUNATCH;
  621. break;
  622. case HCIUARTGETDEVICE:
  623. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  624. err = hu->hdev->id;
  625. else
  626. err = -EUNATCH;
  627. break;
  628. case HCIUARTSETFLAGS:
  629. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  630. err = -EBUSY;
  631. else
  632. err = hci_uart_set_flags(hu, arg);
  633. break;
  634. case HCIUARTGETFLAGS:
  635. err = hu->hdev_flags;
  636. break;
  637. default:
  638. err = n_tty_ioctl_helper(tty, cmd, arg);
  639. break;
  640. }
  641. return err;
  642. }
  643. /*
  644. * We don't provide read/write/poll interface for user space.
  645. */
  646. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  647. u8 *buf, size_t nr, void **cookie,
  648. unsigned long offset)
  649. {
  650. return 0;
  651. }
  652. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  653. const u8 *data, size_t count)
  654. {
  655. return 0;
  656. }
  657. static struct tty_ldisc_ops hci_uart_ldisc = {
  658. .owner = THIS_MODULE,
  659. .num = N_HCI,
  660. .name = "n_hci",
  661. .open = hci_uart_tty_open,
  662. .close = hci_uart_tty_close,
  663. .read = hci_uart_tty_read,
  664. .write = hci_uart_tty_write,
  665. .ioctl = hci_uart_tty_ioctl,
  666. .compat_ioctl = hci_uart_tty_ioctl,
  667. .receive_buf = hci_uart_tty_receive,
  668. .write_wakeup = hci_uart_tty_wakeup,
  669. };
  670. static int __init hci_uart_init(void)
  671. {
  672. int err;
  673. BT_INFO("HCI UART driver ver %s", VERSION);
  674. /* Register the tty discipline */
  675. err = tty_register_ldisc(&hci_uart_ldisc);
  676. if (err) {
  677. BT_ERR("HCI line discipline registration failed. (%d)", err);
  678. return err;
  679. }
  680. #ifdef CONFIG_BT_HCIUART_H4
  681. h4_init();
  682. #endif
  683. #ifdef CONFIG_BT_HCIUART_BCSP
  684. bcsp_init();
  685. #endif
  686. #ifdef CONFIG_BT_HCIUART_LL
  687. ll_init();
  688. #endif
  689. #ifdef CONFIG_BT_HCIUART_ATH3K
  690. ath_init();
  691. #endif
  692. #ifdef CONFIG_BT_HCIUART_3WIRE
  693. h5_init();
  694. #endif
  695. #ifdef CONFIG_BT_HCIUART_INTEL
  696. intel_init();
  697. #endif
  698. #ifdef CONFIG_BT_HCIUART_BCM
  699. bcm_init();
  700. #endif
  701. #ifdef CONFIG_BT_HCIUART_QCA
  702. qca_init();
  703. #endif
  704. #ifdef CONFIG_BT_HCIUART_AG6XX
  705. ag6xx_init();
  706. #endif
  707. #ifdef CONFIG_BT_HCIUART_MRVL
  708. mrvl_init();
  709. #endif
  710. #ifdef CONFIG_BT_HCIUART_AML
  711. aml_init();
  712. #endif
  713. return 0;
  714. }
  715. static void __exit hci_uart_exit(void)
  716. {
  717. #ifdef CONFIG_BT_HCIUART_H4
  718. h4_deinit();
  719. #endif
  720. #ifdef CONFIG_BT_HCIUART_BCSP
  721. bcsp_deinit();
  722. #endif
  723. #ifdef CONFIG_BT_HCIUART_LL
  724. ll_deinit();
  725. #endif
  726. #ifdef CONFIG_BT_HCIUART_ATH3K
  727. ath_deinit();
  728. #endif
  729. #ifdef CONFIG_BT_HCIUART_3WIRE
  730. h5_deinit();
  731. #endif
  732. #ifdef CONFIG_BT_HCIUART_INTEL
  733. intel_deinit();
  734. #endif
  735. #ifdef CONFIG_BT_HCIUART_BCM
  736. bcm_deinit();
  737. #endif
  738. #ifdef CONFIG_BT_HCIUART_QCA
  739. qca_deinit();
  740. #endif
  741. #ifdef CONFIG_BT_HCIUART_AG6XX
  742. ag6xx_deinit();
  743. #endif
  744. #ifdef CONFIG_BT_HCIUART_MRVL
  745. mrvl_deinit();
  746. #endif
  747. #ifdef CONFIG_BT_HCIUART_AML
  748. aml_deinit();
  749. #endif
  750. tty_unregister_ldisc(&hci_uart_ldisc);
  751. }
  752. module_init(hci_uart_init);
  753. module_exit(hci_uart_exit);
  754. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  755. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  756. MODULE_VERSION(VERSION);
  757. MODULE_LICENSE("GPL");
  758. MODULE_ALIAS_LDISC(N_HCI);