hci_ldisc.c 19 KB

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