hci_ldisc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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/version.h>
  41. #include <net/bluetooth/bluetooth.h>
  42. #include <net/bluetooth/hci_core.h>
  43. #include "hci_uart.h"
  44. #define NEW_TX_SCHED_POLICY
  45. #if WOBT_NOTIFY
  46. #include <linux/suspend.h>
  47. #endif
  48. #ifdef BTCOEX
  49. #include "rtk_coex.h"
  50. #endif
  51. #define VERSION "2.2.0c90be4.20211102-175223"
  52. #if HCI_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
  53. #define GET_DRV_DATA(x) hci_get_drvdata(x)
  54. #else
  55. #define GET_DRV_DATA(x) (struct hci_uart *)(x->driver_data)
  56. #endif
  57. #define SEMWAIT_TIMEOUT 50
  58. #if WOBT_NOTIFY
  59. struct hci_rsp_read_local {
  60. __u8 status;
  61. __u8 hci_ver;
  62. __le16 hci_rev;
  63. __u8 lmp_ver;
  64. __le16 manufacturer;
  65. __le16 lmp_subver;
  66. } __packed;
  67. #endif
  68. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  69. static int reset = 0;
  70. #endif
  71. static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
  72. static int hci_uart_flush(struct hci_dev *hdev);
  73. int hci_uart_register_proto(struct hci_uart_proto *p)
  74. {
  75. if (p->id >= HCI_UART_MAX_PROTO)
  76. return -EINVAL;
  77. if (hup[p->id])
  78. return -EEXIST;
  79. hup[p->id] = p;
  80. return 0;
  81. }
  82. int hci_uart_unregister_proto(struct hci_uart_proto *p)
  83. {
  84. if (p->id >= HCI_UART_MAX_PROTO)
  85. return -EINVAL;
  86. if (!hup[p->id])
  87. return -EINVAL;
  88. hup[p->id] = NULL;
  89. return 0;
  90. }
  91. static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
  92. {
  93. if (id >= HCI_UART_MAX_PROTO)
  94. return NULL;
  95. return hup[id];
  96. }
  97. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  98. {
  99. struct hci_dev *hdev = hu->hdev;
  100. /* Update HCI stat counters */
  101. switch (pkt_type) {
  102. case HCI_COMMAND_PKT:
  103. hdev->stat.cmd_tx++;
  104. break;
  105. case HCI_ACLDATA_PKT:
  106. hdev->stat.acl_tx++;
  107. break;
  108. case HCI_SCODATA_PKT:
  109. hdev->stat.sco_tx++;
  110. break;
  111. }
  112. }
  113. static inline void hci_proto_read_lock(struct hci_uart *hu)
  114. {
  115. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  116. percpu_down_read(&hu->proto_lock);
  117. #else
  118. down_read(&hu->proto_lock);
  119. #endif
  120. }
  121. static inline int hci_proto_read_trylock(struct hci_uart *hu)
  122. {
  123. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  124. return percpu_down_read_trylock(&hu->proto_lock);
  125. #else
  126. return down_read_trylock(&hu->proto_lock);
  127. #endif
  128. }
  129. static inline void hci_proto_read_unlock(struct hci_uart *hu)
  130. {
  131. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  132. percpu_up_read(&hu->proto_lock);
  133. #else
  134. up_read(&hu->proto_lock);
  135. #endif
  136. }
  137. static inline void hci_proto_write_lock(struct hci_uart *hu)
  138. {
  139. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  140. percpu_down_write(&hu->proto_lock);
  141. #else
  142. down_write(&hu->proto_lock);
  143. #endif
  144. }
  145. static inline void hci_proto_write_unlock(struct hci_uart *hu)
  146. {
  147. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  148. percpu_up_write(&hu->proto_lock);
  149. #else
  150. up_write(&hu->proto_lock);
  151. #endif
  152. }
  153. static inline int hci_proto_init_rwlock(struct hci_uart *hu)
  154. {
  155. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  156. return percpu_init_rwsem(&hu->proto_lock);
  157. #else
  158. init_rwsem(&hu->proto_lock);
  159. return 0;
  160. #endif
  161. }
  162. static inline void hci_proto_free_rwlock(struct hci_uart *hu)
  163. {
  164. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  165. percpu_free_rwsem(&hu->proto_lock);
  166. #endif
  167. }
  168. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  169. {
  170. struct sk_buff *skb = hu->tx_skb;
  171. if (!skb) {
  172. hci_proto_read_lock(hu);
  173. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  174. skb = hu->proto->dequeue(hu);
  175. hci_proto_read_unlock(hu);
  176. } else {
  177. hu->tx_skb = NULL;
  178. }
  179. return skb;
  180. }
  181. /* This may be called in an IRQ context */
  182. int hci_uart_tx_wakeup(struct hci_uart *hu)
  183. {
  184. /* If acquiring lock fails we assume the tty is being closed because
  185. * that is the only time the write lock is acquired. If, however,
  186. * at some point in the future the write lock is also acquired in
  187. * other situations, then this must be revisited.
  188. */
  189. if (!hci_proto_read_trylock(hu))
  190. return 0;
  191. /* proto_lock is locked */
  192. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  193. goto no_schedule;
  194. #ifdef NEW_TX_SCHED_POLICY
  195. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  196. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
  197. goto no_schedule;
  198. #else
  199. if (in_interrupt() || in_atomic()) {
  200. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  201. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  202. goto no_schedule;
  203. }
  204. } else {
  205. /* NOTE: proto_lock can't be spin lock, because it may
  206. * schedule here. Schedule is not allowed while atomic
  207. */
  208. if (down_timeout(&hu->tx_sem,
  209. msecs_to_jiffies(SEMWAIT_TIMEOUT)) == -ETIME) {
  210. pr_warn("%s: Something went wrong with wait\n",
  211. __func__);
  212. goto no_schedule;
  213. }
  214. /* semaphore is locked */
  215. if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
  216. set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  217. up(&hu->tx_sem);
  218. goto no_schedule;
  219. }
  220. up(&hu->tx_sem);
  221. }
  222. #endif
  223. BT_DBG("");
  224. schedule_work(&hu->write_work);
  225. no_schedule:
  226. hci_proto_read_unlock(hu);
  227. return 0;
  228. }
  229. static void hci_uart_write_work(struct work_struct *work)
  230. {
  231. struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
  232. struct tty_struct *tty = hu->tty;
  233. struct hci_dev *hdev = hu->hdev;
  234. struct sk_buff *skb;
  235. /* REVISIT: should we cope with bad skbs or ->write() returning
  236. * and error value ?
  237. */
  238. restart:
  239. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  240. while ((skb = hci_uart_dequeue(hu))) {
  241. int len;
  242. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  243. len = tty->ops->write(tty, skb->data, skb->len);
  244. hdev->stat.byte_tx += len;
  245. skb_pull(skb, len);
  246. if (skb->len) {
  247. hu->tx_skb = skb;
  248. break;
  249. }
  250. hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
  251. kfree_skb(skb);
  252. }
  253. #ifdef NEW_TX_SCHED_POLICY
  254. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  255. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
  256. goto restart;
  257. #else
  258. if (down_timeout(&hu->tx_sem, msecs_to_jiffies(SEMWAIT_TIMEOUT))) {
  259. pr_warn("%s: Something went wrong with wait\n", __func__);
  260. goto restart;
  261. }
  262. /* semaphore is locked */
  263. if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)) {
  264. up(&hu->tx_sem);
  265. goto restart;
  266. }
  267. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  268. up(&hu->tx_sem);
  269. #endif
  270. return;
  271. }
  272. /* ------- Interface to HCI layer ------ */
  273. /* Initialize device */
  274. static int hci_uart_open(struct hci_dev *hdev)
  275. {
  276. BT_DBG("%s %p", hdev->name, hdev);
  277. /* Undo clearing this from hci_uart_close() */
  278. hdev->flush = hci_uart_flush;
  279. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
  280. set_bit(HCI_RUNNING, &hdev->flags);
  281. #endif
  282. #ifdef BTCOEX
  283. rtk_btcoex_open(hdev);
  284. #endif
  285. return 0;
  286. }
  287. /* static void hci_flush_sync(struct hci_dev *hdev)
  288. * {
  289. * #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
  290. * u8 buf[2] = { 0, 0 };
  291. * struct sk_buff *skb;
  292. *
  293. * BT_INFO("hci flush sync");
  294. *
  295. * set_bit(HCI_INIT, &hdev->flags);
  296. * skb = __hci_cmd_sync(hdev, 0xfc19, 2, buf, msecs_to_jiffies(2000));
  297. * clear_bit(HCI_INIT, &hdev->flags);
  298. *
  299. * if (IS_ERR(skb)) {
  300. * BT_ERR("command 0xfc19 tx failed (%ld)\n", PTR_ERR(skb));
  301. * return;
  302. * }
  303. *
  304. * if (skb->len == 1)
  305. * BT_INFO("hci flush sync status %u", skb->data[0]);
  306. *
  307. * kfree_skb(skb);
  308. * #endif
  309. * }
  310. */
  311. static int __hci_uart_flush(struct hci_dev *hdev, u8 sync)
  312. {
  313. struct hci_uart *hu = GET_DRV_DATA(hdev); //(struct hci_uart *) hdev->driver_data;
  314. struct tty_struct *tty = hu->tty;
  315. BT_INFO("%s: hdev %p tty %p", __func__, hdev, tty);
  316. /* Make sure all HCI packets has been transmitted */
  317. /* if (sync && test_bit(HCI_RUNNING, &hdev->flags))
  318. * hci_flush_sync(hdev);
  319. */
  320. if (hu->tx_skb) {
  321. kfree_skb(hu->tx_skb);
  322. hu->tx_skb = NULL;
  323. }
  324. /* Flush any pending characters in the driver and discipline. */
  325. /* tty_ldisc_flush(tty);
  326. * tty_driver_flush_buffer(tty);
  327. */
  328. /* Don't flush the tty. Sometime, the hdev is closed abnormally.
  329. * There may be cmd complete event in rx buf or the sent ack in tx buf.
  330. * tty flush will result in hciX: command 0xXXXX tx timeout
  331. */
  332. tty_wait_until_sent(tty, msecs_to_jiffies(500));
  333. hci_proto_read_lock(hu);
  334. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  335. hu->proto->flush(hu);
  336. hci_proto_read_unlock(hu);
  337. return 0;
  338. }
  339. /* Reset device */
  340. static int hci_uart_flush(struct hci_dev *hdev)
  341. {
  342. return __hci_uart_flush(hdev, 1);
  343. }
  344. /* Close device */
  345. static int hci_uart_close(struct hci_dev *hdev)
  346. {
  347. BT_INFO("%s: hdev %p", __func__, hdev);
  348. /* When in kernel 4.4.0 and greater, the HCI_RUNNING bit is
  349. * cleared in hci_dev_do_close(). */
  350. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
  351. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  352. return 0;
  353. #else
  354. if (test_bit(HCI_RUNNING, &hdev->flags))
  355. BT_ERR("HCI_RUNNING is not cleared before.");
  356. #endif
  357. if (test_bit(HCI_RUNNING, &hdev->flags))
  358. __hci_uart_flush(hdev, 0);
  359. else
  360. __hci_uart_flush(hdev, 1);
  361. hdev->flush = NULL;
  362. #ifdef BTCOEX
  363. rtk_btcoex_close();
  364. #endif
  365. return 0;
  366. }
  367. /* Send frames from HCI layer */
  368. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
  369. int hci_uart_send_frame(struct sk_buff *skb)
  370. #else
  371. int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  372. #endif
  373. {
  374. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
  375. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  376. #endif
  377. struct hci_uart *hu;
  378. if (!hdev) {
  379. BT_ERR("Frame for unknown device (hdev=NULL)");
  380. return -ENODEV;
  381. }
  382. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
  383. if (!test_bit(HCI_RUNNING, &hdev->flags))
  384. return -EBUSY;
  385. #endif
  386. hu = GET_DRV_DATA(hdev); //(struct hci_uart *) hdev->driver_data;
  387. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
  388. skb->len);
  389. #ifdef BTCOEX
  390. if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)
  391. rtk_btcoex_parse_cmd(skb->data, skb->len);
  392. if (bt_cb(skb)->pkt_type == HCI_ACLDATA_PKT)
  393. rtk_btcoex_parse_l2cap_data_tx(skb->data, skb->len);
  394. #endif
  395. hci_proto_read_lock(hu);
  396. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  397. hci_proto_read_unlock(hu);
  398. return -EUNATCH;
  399. }
  400. hu->proto->enqueue(hu, skb);
  401. hci_proto_read_unlock(hu);
  402. hci_uart_tx_wakeup(hu);
  403. return 0;
  404. }
  405. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  406. static void hci_uart_destruct(struct hci_dev *hdev)
  407. {
  408. if (!hdev)
  409. return;
  410. BT_DBG("%s", hdev->name);
  411. kfree(hdev->driver_data);
  412. }
  413. #endif
  414. #if WOBT_NOTIFY
  415. static int hci_uart_async_send(struct hci_uart *hu, u16 opcode,
  416. u32 plen, const void *param)
  417. {
  418. int len = HCI_COMMAND_HDR_SIZE + plen;
  419. struct hci_command_hdr *hdr;
  420. struct sk_buff *skb;
  421. skb = bt_skb_alloc(len, GFP_ATOMIC);
  422. if (!skb)
  423. return -ENOMEM;
  424. hdr = (struct hci_command_hdr *)skb_put(skb, HCI_COMMAND_HDR_SIZE);
  425. hdr->opcode = cpu_to_le16(opcode);
  426. hdr->plen = plen;
  427. if (plen)
  428. memcpy(skb_put(skb, plen), param, plen);
  429. BT_INFO("rtl: skb len %d", skb->len);
  430. bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
  431. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
  432. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
  433. bt_cb(skb)->opcode = opcode;
  434. #else
  435. bt_cb(skb)->hci.opcode = opcode;
  436. #endif
  437. #endif
  438. /* Stand-alone HCI commands must be flagged as
  439. * single-command requests.
  440. */
  441. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
  442. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
  443. bt_cb(skb)->req.start = true;
  444. #else
  445. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
  446. bt_cb(skb)->hci.req_start = true;
  447. #else
  448. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  449. #endif
  450. #endif /* 4.4.0 */
  451. #endif /* 3.10.0 */
  452. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
  453. hci_uart_send_frame(skb);
  454. #else
  455. hci_uart_send_frame(hu->hdev, skb);
  456. #endif
  457. /* hci_proto_read_lock(hu);
  458. * if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  459. * hci_proto_read_unlock(hu);
  460. * BT_ERR("rtl send: proto not ready");
  461. * return -EUNATCH;
  462. * }
  463. * hu->proto->enqueue(hu, skb);
  464. * hci_proto_read_unlock(hu);
  465. * hci_uart_tx_wakeup(hu);
  466. */
  467. return 0;
  468. }
  469. static int rtl_read_local_version(struct hci_dev *hdev, u8 *hci_ver,
  470. u16 *hci_rev, u16 *lmp_subver)
  471. {
  472. struct hci_rsp_read_local *ver;
  473. struct sk_buff *skb;
  474. skb = __hci_cmd_sync(hdev, 0x1001, 0, NULL, HCI_INIT_TIMEOUT);
  475. if (IS_ERR(skb)) {
  476. BT_ERR("rtl: Could not read lmp subversion");
  477. return PTR_ERR(skb);
  478. }
  479. if (skb->len != sizeof(struct hci_rsp_read_local)) {
  480. BT_ERR("%s: rtl: Local version length mismatch", hdev->name);
  481. kfree_skb(skb);
  482. return -EIO;
  483. }
  484. ver = (struct hci_rsp_read_local *)skb->data;
  485. *hci_ver = ver->hci_ver;
  486. *hci_rev = le16_to_cpu(ver->hci_rev);
  487. *lmp_subver = le16_to_cpu(ver->lmp_subver);
  488. kfree_skb(skb);
  489. return 0;
  490. }
  491. #if RTKBT_TV_POWERON_WHITELIST
  492. static int rtkbt_lookup_le_device_poweron_whitelist(struct hci_uart *hu)
  493. {
  494. struct hci_conn_params *p;
  495. u8 *params;
  496. int result = 0;
  497. hci_dev_lock(hu->hdev);
  498. list_for_each_entry(p, &hu->hdev->le_conn_params, list) {
  499. #if 0 // for debug message
  500. BT_INFO("%s(): auto_connect = %d", __FUNCTION__, p->auto_connect);
  501. BT_INFO("%s(): addr_type = 0x%02x", __FUNCTION__, p->addr_type);
  502. BT_INFO("%s(): addr=%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
  503. p->addr.b[5], p->addr.b[4], p->addr.b[3],
  504. p->addr.b[2], p->addr.b[1], p->addr.b[0]);
  505. #endif
  506. if ( p->auto_connect == HCI_AUTO_CONN_ALWAYS &&
  507. p->addr_type == ADDR_LE_DEV_PUBLIC ) {
  508. BT_INFO("%s(): Set RTKBT LE Power-on Whitelist for "
  509. "%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
  510. p->addr.b[5], p->addr.b[4], p->addr.b[3],
  511. p->addr.b[2], p->addr.b[1], p->addr.b[0]);
  512. params = kzalloc(8, GFP_ATOMIC);
  513. if (!params) {
  514. BT_ERR("Can't allocate memory for params");
  515. return -ENOMEM;
  516. }
  517. params[0] = 0x00;
  518. params[1] = p->addr.b[0];
  519. params[2] = p->addr.b[1];
  520. params[3] = p->addr.b[2];
  521. params[4] = p->addr.b[3];
  522. params[5] = p->addr.b[4];
  523. params[6] = p->addr.b[5];
  524. result = hci_uart_async_send(hu, 0xfc7b, 7, params);
  525. if (result)
  526. BT_ERR("rtl: Command failed for power-on whitelist");
  527. msleep(500);
  528. kfree(params);
  529. }
  530. }
  531. hci_dev_unlock(hu->hdev);
  532. return result;
  533. }
  534. #endif
  535. static int rtkbt_notify_suspend(struct hci_uart *hu)
  536. {
  537. struct hci_conn *conn;
  538. struct sk_buff *rx_skb;
  539. u8 params_suspend_notify[1] = { 0x01 };
  540. u8 event_params[6] = { 0x05, 0x04, 0x00, 0x10, 0x00, 0x13 };
  541. int result = 0;
  542. result = hci_uart_async_send(hu, 0xfc28, 1, params_suspend_notify);
  543. if (result)
  544. BT_ERR("Realtek suspend h5-bt failed");
  545. msleep(500);
  546. hci_dev_lock(hu->hdev);
  547. conn = hci_conn_hash_lookup_state(hu->hdev, LE_LINK, BT_CONNECTED);
  548. if (conn && (conn->state == BT_CONNECTED)){
  549. rx_skb = alloc_skb(6, GFP_ATOMIC);
  550. if (!rx_skb)
  551. return -1;
  552. event_params[3] = (u8)(conn->handle);
  553. event_params[4] = (u8)(conn->handle >> 8);
  554. hci_skb_pkt_type(rx_skb) = HCI_EVENT_PKT;
  555. skb_put_data(rx_skb, event_params, 6);
  556. BT_INFO("Send Disconnect Complete EVENT to upper stack");
  557. hci_recv_frame(hu->hdev, rx_skb);
  558. }
  559. hci_dev_unlock(hu->hdev);
  560. return result;
  561. }
  562. static void le_scan_disable(struct hci_uart *hu)
  563. {
  564. #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
  565. if (use_ext_scan(hu->hdev)) {
  566. u8 ext_enable_cp[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  567. hci_uart_async_send(hu, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 6, ext_enable_cp);
  568. } else {
  569. u8 enable_cp[2] = {0x00, 0x00};
  570. hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
  571. }
  572. #else
  573. u8 enable_cp[2] = {0x00, 0x00};
  574. hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
  575. #endif
  576. return;
  577. }
  578. static void le_scan_restart(struct hci_uart *hu)
  579. {
  580. int result;
  581. #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
  582. if (use_ext_scan(hu->hdev)) {
  583. u8 ext_enable_cp[6] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
  584. BT_INFO("LE Extended Scan Restart...");
  585. le_scan_disable(hu);
  586. result = hci_uart_async_send(hu, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 6, ext_enable_cp);
  587. if (result)
  588. BT_ERR("LE Extended Scan Restart: Failed");
  589. } else {
  590. u8 enable_cp[2] = {0x01, 0x01};
  591. BT_INFO("LE Scan Restart...");
  592. le_scan_disable(hu);
  593. result = hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
  594. if (result)
  595. BT_ERR("LE Scan Restart: Failed");
  596. }
  597. #else
  598. u8 enable_cp[2] = {0x01, 0x01};
  599. BT_INFO("LE Scan Restart");
  600. le_scan_disable(hu);
  601. result = hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
  602. if (result)
  603. BT_ERR("LE Scan Restart: Failed");
  604. #endif
  605. return;
  606. }
  607. static bool le_aoto_conn_always_exist(struct hci_uart *hu)
  608. {
  609. struct hci_conn_params *p;
  610. bool ret = false;
  611. hci_dev_lock(hu->hdev);
  612. list_for_each_entry(p, &hu->hdev->le_conn_params, list) {
  613. if ( p->auto_connect == HCI_AUTO_CONN_ALWAYS &&
  614. p->addr_type == ADDR_LE_DEV_PUBLIC ) {
  615. ret = true;
  616. }
  617. }
  618. hci_dev_unlock(hu->hdev);
  619. return ret;
  620. }
  621. static int hci_uart_pm_notifier(struct notifier_block *b, unsigned long v, void *d)
  622. {
  623. int result;
  624. struct hci_uart *hu = container_of(b, struct hci_uart, pm_notify_block);
  625. u8 hci_ver = 0;
  626. u16 hci_rev = 0;
  627. u16 lmp_subver = 0;
  628. #if WOBT_NOTIFY_BG_SCAN_LE_WHITELIST_ONLY
  629. u8 params_bg_scan[5] = { 0x60, 0x01, 0x10, 0x00, 0x01 };
  630. #endif
  631. BT_INFO("%s: %lu", __func__, v);
  632. switch (v) {
  633. case PM_SUSPEND_PREPARE:
  634. BT_INFO("rtl: bt suspending");
  635. #if WOBT_NOTIFY_BG_SCAN_LE_WHITELIST_ONLY
  636. /* Send set back ground scan parameters to Controller for power-on mode */
  637. result = hci_uart_async_send(hu, 0xfc7a, 5, params_bg_scan);
  638. if (result)
  639. BT_ERR("Realtek bg-scan h5-bt failed");
  640. /* FIXME: Ensure the above vendor command is sent to Controller
  641. * and we received the h5 ack from Controller
  642. * */
  643. msleep(500);
  644. #endif
  645. #if RTKBT_TV_POWERON_WHITELIST
  646. result = rtkbt_lookup_le_device_poweron_whitelist(hu);
  647. if (result < 0) {
  648. BT_ERR("rtkbt_lookup_le_device_poweron_whitelist error: %d", result);
  649. }
  650. #endif
  651. result = rtkbt_notify_suspend(hu);
  652. if (result < 0) {
  653. BT_ERR("rtkbt_notify_suspend error: %d", result);
  654. }
  655. break;
  656. case PM_POST_SUSPEND:
  657. result = rtl_read_local_version(hu->hdev, &hci_ver, &hci_rev,
  658. &lmp_subver);
  659. if (result)
  660. break;
  661. BT_INFO("rtl resume: hci ver %u, hci rev %04x, lmp subver %04x",
  662. hci_ver, hci_rev, lmp_subver);
  663. if (le_aoto_conn_always_exist(hu))
  664. le_scan_restart(hu);
  665. break;
  666. default:
  667. BT_INFO("Caught msg %lu other than SUSPEND_PREPARE", v);
  668. break;
  669. }
  670. return 0;
  671. }
  672. #endif
  673. /* ------ LDISC part ------ */
  674. /* hci_uart_tty_open
  675. *
  676. * Called when line discipline changed to HCI_UART.
  677. *
  678. * Arguments:
  679. * tty pointer to tty info structure
  680. * Return Value:
  681. * 0 if success, otherwise error code
  682. */
  683. static int hci_uart_tty_open(struct tty_struct *tty)
  684. {
  685. struct hci_uart *hu = (void *)tty->disc_data;
  686. BT_DBG("tty %p", tty);
  687. /* But nothing ensures disc_data to be NULL. And since ld->ops->open
  688. * shall be called only once, we do not need the check at all.
  689. * So remove it.
  690. *
  691. * Note that this is not an issue now, but n_tty will start using the
  692. * disc_data pointer and this invalid 'if' would trigger then rendering
  693. * TTYs over BT unusable.
  694. */
  695. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
  696. /* FIXME: This btw is bogus, nothing requires the old ldisc to clear
  697. * the pointer
  698. */
  699. if (hu)
  700. return -EEXIST;
  701. #endif
  702. /* Error if the tty has no write op instead of leaving an exploitable
  703. * hole
  704. */
  705. if (tty->ops->write == NULL)
  706. return -EOPNOTSUPP;
  707. if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
  708. BT_ERR("Can't allocate control structure");
  709. return -ENFILE;
  710. }
  711. tty->disc_data = hu;
  712. hu->tty = tty;
  713. tty->receive_room = 65536;
  714. INIT_WORK(&hu->write_work, hci_uart_write_work);
  715. hci_proto_init_rwlock(hu);
  716. sema_init(&hu->tx_sem, 1);
  717. /* Flush any pending characters in the driver and line discipline. */
  718. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  719. open path is before the ldisc is referencable */
  720. if (tty->ldisc->ops->flush_buffer)
  721. tty->ldisc->ops->flush_buffer(tty);
  722. tty_driver_flush_buffer(tty);
  723. #if WOBT_NOTIFY
  724. hu->pm_notify_block.notifier_call = hci_uart_pm_notifier;
  725. register_pm_notifier(&hu->pm_notify_block);
  726. #endif
  727. return 0;
  728. }
  729. /* hci_uart_tty_close()
  730. *
  731. * Called when the line discipline is changed to something
  732. * else, the tty is closed, or the tty detects a hangup.
  733. */
  734. static void hci_uart_tty_close(struct tty_struct *tty)
  735. {
  736. struct hci_uart *hu = (void *)tty->disc_data;
  737. struct hci_dev *hdev;
  738. BT_INFO("%s: tty %p", __func__, tty);
  739. /* Detach from the tty */
  740. tty->disc_data = NULL;
  741. if (!hu)
  742. return;
  743. hdev = hu->hdev;
  744. if (hdev)
  745. hci_uart_close(hdev);
  746. if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  747. hci_proto_write_lock(hu);
  748. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  749. hci_proto_write_unlock(hu);
  750. cancel_work_sync(&hu->write_work);
  751. if (hdev) {
  752. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  753. hci_unregister_dev(hdev);
  754. hci_free_dev(hdev);
  755. }
  756. hu->proto->close(hu);
  757. }
  758. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  759. hci_proto_free_rwlock(hu);
  760. #if WOBT_NOTIFY
  761. unregister_pm_notifier(&hu->pm_notify_block);
  762. #endif
  763. kfree(hu);
  764. }
  765. /* hci_uart_tty_wakeup()
  766. *
  767. * Callback for transmit wakeup. Called when low level
  768. * device driver can accept more send data.
  769. *
  770. * Arguments: tty pointer to associated tty instance data
  771. * Return Value: None
  772. */
  773. static void hci_uart_tty_wakeup(struct tty_struct *tty)
  774. {
  775. struct hci_uart *hu = (void *)tty->disc_data;
  776. BT_DBG("");
  777. if (!hu)
  778. return;
  779. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  780. if (tty != hu->tty)
  781. return;
  782. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  783. hci_uart_tx_wakeup(hu);
  784. }
  785. /* hci_uart_tty_receive()
  786. *
  787. * Called by tty low level driver when receive data is
  788. * available.
  789. *
  790. * Arguments: tty pointer to tty isntance data
  791. * data pointer to received data
  792. * flags pointer to flags for data
  793. * count count of received data in bytes
  794. *
  795. * Return Value: None
  796. */
  797. static void hci_uart_tty_receive(struct tty_struct *tty, const u8 * data,
  798. char *flags, int count)
  799. {
  800. struct hci_uart *hu = (void *)tty->disc_data;
  801. int (*proto_receive)(struct hci_uart *hu, void *data, int len);
  802. if (!hu || tty != hu->tty)
  803. return;
  804. hci_proto_read_lock(hu);
  805. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  806. hci_proto_read_unlock(hu);
  807. return;
  808. }
  809. proto_receive = hu->proto->recv;
  810. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  811. proto_receive(hu, (void *)data, count);
  812. hci_proto_read_unlock(hu);
  813. #else
  814. hci_proto_read_unlock(hu);
  815. /* It does not need a lock here as it is already protected by a mutex in
  816. * tty caller
  817. */
  818. proto_receive(hu, (void *)data, count);
  819. #endif
  820. if (hu->hdev)
  821. hu->hdev->stat.byte_rx += count;
  822. tty_unthrottle(tty);
  823. }
  824. static int hci_uart_register_dev(struct hci_uart *hu)
  825. {
  826. struct hci_dev *hdev;
  827. BT_INFO("hci_uart_register_dev");
  828. /* Initialize and register HCI device */
  829. hdev = hci_alloc_dev();
  830. if (!hdev) {
  831. BT_ERR("Can't allocate HCI device");
  832. return -ENOMEM;
  833. }
  834. hu->hdev = hdev;
  835. #if HCI_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
  836. hdev->bus = HCI_UART;
  837. #else
  838. hdev->type = HCI_UART;
  839. #endif
  840. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  841. hci_set_drvdata(hdev, hu);
  842. #else
  843. hdev->driver_data = hu;
  844. #endif
  845. hdev->open = hci_uart_open;
  846. hdev->close = hci_uart_close;
  847. hdev->flush = hci_uart_flush;
  848. hdev->send = hci_uart_send_frame;
  849. /* NOTE: No hdev->setup setting for Realtek BTUART because
  850. * the download procedure is done with rtk_hciattach in userspace
  851. * before this function called in hci_uart_set_proto()
  852. */
  853. SET_HCIDEV_DEV(hdev, hu->tty->dev);
  854. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  855. hdev->destruct = hci_uart_destruct;
  856. hdev->owner = THIS_MODULE;
  857. #endif
  858. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  859. if (!reset)
  860. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  861. #endif
  862. #if HCI_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
  863. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  864. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  865. #endif
  866. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
  867. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  868. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  869. #endif
  870. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  871. if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
  872. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
  873. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  874. #else
  875. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  876. #endif
  877. #endif
  878. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  879. if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  880. hdev->dev_type = HCI_AMP;
  881. else
  882. #if HCI_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
  883. hdev->dev_type = HCI_BREDR;
  884. #else
  885. hdev->dev_type = HCI_PRIMARY;
  886. #endif
  887. #endif
  888. #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
  889. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  890. #endif
  891. if (hci_register_dev(hdev) < 0) {
  892. BT_ERR("Can't register HCI device");
  893. hci_free_dev(hdev);
  894. return -ENODEV;
  895. }
  896. set_bit(HCI_UART_REGISTERED, &hu->flags);
  897. #ifdef BTCOEX
  898. rtk_btcoex_probe(hdev);
  899. #endif
  900. return 0;
  901. }
  902. static int hci_uart_set_proto(struct hci_uart *hu, int id)
  903. {
  904. struct hci_uart_proto *p;
  905. int err;
  906. p = hci_uart_get_proto(id);
  907. if (!p)
  908. return -EPROTONOSUPPORT;
  909. err = p->open(hu);
  910. if (err)
  911. return err;
  912. hu->proto = p;
  913. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  914. /* Initialize and register HCI dev */
  915. err = hci_uart_register_dev(hu);
  916. if (err) {
  917. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  918. p->close(hu);
  919. return err;
  920. }
  921. return 0;
  922. }
  923. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
  924. static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
  925. {
  926. /* TODO: Add HCI_UART_INIT_PENDING, HCI_UART_VND_DETECT check */
  927. unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
  928. BIT(HCI_UART_RESET_ON_INIT) |
  929. BIT(HCI_UART_CREATE_AMP) |
  930. BIT(HCI_UART_EXT_CONFIG);
  931. if (flags & ~valid_flags)
  932. return -EINVAL;
  933. hu->hdev_flags = flags;
  934. return 0;
  935. }
  936. #endif
  937. /* hci_uart_tty_ioctl()
  938. *
  939. * Process IOCTL system call for the tty device.
  940. *
  941. * Arguments:
  942. *
  943. * tty pointer to tty instance data
  944. * file pointer to open file object for device
  945. * cmd IOCTL command code
  946. * arg argument for IOCTL call (cmd dependent)
  947. *
  948. * Return Value: Command dependent
  949. */
  950. static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
  951. unsigned int cmd, unsigned long arg)
  952. {
  953. struct hci_uart *hu = (void *)tty->disc_data;
  954. int err = 0;
  955. BT_DBG("");
  956. /* Verify the status of the device */
  957. if (!hu)
  958. return -EBADF;
  959. switch (cmd) {
  960. case HCIUARTSETPROTO:
  961. if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
  962. err = hci_uart_set_proto(hu, arg);
  963. if (err) {
  964. clear_bit(HCI_UART_PROTO_SET, &hu->flags);
  965. return err;
  966. }
  967. } else
  968. return -EBUSY;
  969. break;
  970. case HCIUARTGETPROTO:
  971. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  972. return hu->proto->id;
  973. return -EUNATCH;
  974. case HCIUARTGETDEVICE:
  975. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  976. return hu->hdev->id;
  977. return -EUNATCH;
  978. case HCIUARTSETFLAGS:
  979. if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
  980. return -EBUSY;
  981. #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
  982. err = hci_uart_set_flags(hu, arg);
  983. if (err)
  984. return err;
  985. #else
  986. hu->hdev_flags = arg;
  987. #endif
  988. break;
  989. case HCIUARTGETFLAGS:
  990. return hu->hdev_flags;
  991. default:
  992. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  993. break;
  994. };
  995. return err;
  996. }
  997. /*
  998. * We don't provide read/write/poll interface for user space.
  999. */
  1000. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 20) && \
  1001. ((LINUX_VERSION_CODE < KERNEL_VERSION(5, 11, 0)) || \
  1002. (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 3)))
  1003. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  1004. unsigned char *buf, size_t nr,
  1005. void **cookie, unsigned long offset)
  1006. #else
  1007. static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
  1008. unsigned char __user * buf, size_t nr)
  1009. #endif
  1010. {
  1011. return 0;
  1012. }
  1013. static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
  1014. const unsigned char *data, size_t count)
  1015. {
  1016. return 0;
  1017. }
  1018. static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
  1019. struct file *filp, poll_table * wait)
  1020. {
  1021. return 0;
  1022. }
  1023. static struct tty_ldisc_ops hci_uart_ldisc = {
  1024. .owner = THIS_MODULE,
  1025. .magic = TTY_LDISC_MAGIC,
  1026. .name = "n_hci",
  1027. .open = hci_uart_tty_open,
  1028. .close = hci_uart_tty_close,
  1029. .read = hci_uart_tty_read,
  1030. .write = hci_uart_tty_write,
  1031. .ioctl = hci_uart_tty_ioctl,
  1032. #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  1033. .compat_ioctl = hci_uart_tty_ioctl,
  1034. #endif
  1035. .poll = hci_uart_tty_poll,
  1036. .receive_buf = hci_uart_tty_receive,
  1037. .write_wakeup = hci_uart_tty_wakeup,
  1038. };
  1039. static int __init hci_uart_init(void)
  1040. {
  1041. int err;
  1042. BT_INFO("HCI UART driver ver %s", VERSION);
  1043. /* Register the tty discipline */
  1044. if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
  1045. BT_ERR("HCI line discipline registration failed. (%d)", err);
  1046. return err;
  1047. }
  1048. #ifdef CONFIG_BT_HCIUART_H4
  1049. h4_init();
  1050. #endif
  1051. /* Add realtek h5 support */
  1052. h5_init();
  1053. #ifdef BTCOEX
  1054. rtk_btcoex_init();
  1055. #endif
  1056. return 0;
  1057. }
  1058. static void __exit hci_uart_exit(void)
  1059. {
  1060. int err;
  1061. #ifdef CONFIG_BT_HCIUART_H4
  1062. h4_deinit();
  1063. #endif
  1064. h5_deinit();
  1065. /* Release tty registration of line discipline */
  1066. if ((err = tty_unregister_ldisc(N_HCI)))
  1067. BT_ERR("Can't unregister HCI line discipline (%d)", err);
  1068. #ifdef BTCOEX
  1069. rtk_btcoex_exit();
  1070. #endif
  1071. }
  1072. module_init(hci_uart_init);
  1073. module_exit(hci_uart_exit);
  1074. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  1075. module_param(reset, bool, 0644);
  1076. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  1077. #endif
  1078. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  1079. MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
  1080. MODULE_VERSION(VERSION);
  1081. MODULE_LICENSE("GPL");
  1082. MODULE_ALIAS_LDISC(N_HCI);