hci_ldisc.c 20 KB

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