hci_ll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Texas Instruments' Bluetooth HCILL UART protocol
  3. *
  4. * HCILL (HCI Low Level) is a Texas Instruments' power management
  5. * protocol extension to H4.
  6. *
  7. * Copyright (C) 2007 Texas Instruments, Inc.
  8. *
  9. * Written by Ohad Ben-Cohen <ohad@bencohen.org>
  10. *
  11. * Acknowledgements:
  12. * This file is based on hci_h4.c, which was written
  13. * by Maxim Krasnyansky and Marcel Holtmann.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2
  17. * as published by the Free Software Foundation
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include <linux/types.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/poll.h>
  38. #include <linux/slab.h>
  39. #include <linux/tty.h>
  40. #include <linux/errno.h>
  41. #include <linux/string.h>
  42. #include <linux/signal.h>
  43. #include <linux/ioctl.h>
  44. #include <linux/skbuff.h>
  45. #include <net/bluetooth/bluetooth.h>
  46. #include <net/bluetooth/hci_core.h>
  47. #include "hci_uart.h"
  48. /* HCILL commands */
  49. #define HCILL_GO_TO_SLEEP_IND 0x30
  50. #define HCILL_GO_TO_SLEEP_ACK 0x31
  51. #define HCILL_WAKE_UP_IND 0x32
  52. #define HCILL_WAKE_UP_ACK 0x33
  53. /* HCILL receiver States */
  54. #define HCILL_W4_PACKET_TYPE 0
  55. #define HCILL_W4_EVENT_HDR 1
  56. #define HCILL_W4_ACL_HDR 2
  57. #define HCILL_W4_SCO_HDR 3
  58. #define HCILL_W4_DATA 4
  59. /* HCILL states */
  60. enum hcill_states_e {
  61. HCILL_ASLEEP,
  62. HCILL_ASLEEP_TO_AWAKE,
  63. HCILL_AWAKE,
  64. HCILL_AWAKE_TO_ASLEEP
  65. };
  66. struct hcill_cmd {
  67. u8 cmd;
  68. } __packed;
  69. struct ll_struct {
  70. unsigned long rx_state;
  71. unsigned long rx_count;
  72. struct sk_buff *rx_skb;
  73. struct sk_buff_head txq;
  74. spinlock_t hcill_lock; /* HCILL state lock */
  75. unsigned long hcill_state; /* HCILL power state */
  76. struct sk_buff_head tx_wait_q; /* HCILL wait queue */
  77. };
  78. /*
  79. * Builds and sends an HCILL command packet.
  80. * These are very simple packets with only 1 cmd byte
  81. */
  82. static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
  83. {
  84. int err = 0;
  85. struct sk_buff *skb = NULL;
  86. struct ll_struct *ll = hu->priv;
  87. struct hcill_cmd *hcill_packet;
  88. BT_DBG("hu %p cmd 0x%x", hu, cmd);
  89. /* allocate packet */
  90. skb = bt_skb_alloc(1, GFP_ATOMIC);
  91. if (!skb) {
  92. BT_ERR("cannot allocate memory for HCILL packet");
  93. err = -ENOMEM;
  94. goto out;
  95. }
  96. /* prepare packet */
  97. hcill_packet = (struct hcill_cmd *) skb_put(skb, 1);
  98. hcill_packet->cmd = cmd;
  99. /* send packet */
  100. skb_queue_tail(&ll->txq, skb);
  101. out:
  102. return err;
  103. }
  104. /* Initialize protocol */
  105. static int ll_open(struct hci_uart *hu)
  106. {
  107. struct ll_struct *ll;
  108. BT_DBG("hu %p", hu);
  109. ll = kzalloc(sizeof(*ll), GFP_KERNEL);
  110. if (!ll)
  111. return -ENOMEM;
  112. skb_queue_head_init(&ll->txq);
  113. skb_queue_head_init(&ll->tx_wait_q);
  114. spin_lock_init(&ll->hcill_lock);
  115. ll->hcill_state = HCILL_AWAKE;
  116. hu->priv = ll;
  117. return 0;
  118. }
  119. /* Flush protocol data */
  120. static int ll_flush(struct hci_uart *hu)
  121. {
  122. struct ll_struct *ll = hu->priv;
  123. BT_DBG("hu %p", hu);
  124. skb_queue_purge(&ll->tx_wait_q);
  125. skb_queue_purge(&ll->txq);
  126. return 0;
  127. }
  128. /* Close protocol */
  129. static int ll_close(struct hci_uart *hu)
  130. {
  131. struct ll_struct *ll = hu->priv;
  132. BT_DBG("hu %p", hu);
  133. skb_queue_purge(&ll->tx_wait_q);
  134. skb_queue_purge(&ll->txq);
  135. kfree_skb(ll->rx_skb);
  136. hu->priv = NULL;
  137. kfree(ll);
  138. return 0;
  139. }
  140. /*
  141. * internal function, which does common work of the device wake up process:
  142. * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
  143. * 2. changes internal state to HCILL_AWAKE.
  144. * Note: assumes that hcill_lock spinlock is taken,
  145. * shouldn't be called otherwise!
  146. */
  147. static void __ll_do_awake(struct ll_struct *ll)
  148. {
  149. struct sk_buff *skb = NULL;
  150. while ((skb = skb_dequeue(&ll->tx_wait_q)))
  151. skb_queue_tail(&ll->txq, skb);
  152. ll->hcill_state = HCILL_AWAKE;
  153. }
  154. /*
  155. * Called upon a wake-up-indication from the device
  156. */
  157. static void ll_device_want_to_wakeup(struct hci_uart *hu)
  158. {
  159. unsigned long flags;
  160. struct ll_struct *ll = hu->priv;
  161. BT_DBG("hu %p", hu);
  162. /* lock hcill state */
  163. spin_lock_irqsave(&ll->hcill_lock, flags);
  164. switch (ll->hcill_state) {
  165. case HCILL_ASLEEP_TO_AWAKE:
  166. /*
  167. * This state means that both the host and the BRF chip
  168. * have simultaneously sent a wake-up-indication packet.
  169. * Traditionally, in this case, receiving a wake-up-indication
  170. * was enough and an additional wake-up-ack wasn't needed.
  171. * This has changed with the BRF6350, which does require an
  172. * explicit wake-up-ack. Other BRF versions, which do not
  173. * require an explicit ack here, do accept it, thus it is
  174. * perfectly safe to always send one.
  175. */
  176. BT_DBG("dual wake-up-indication");
  177. /* deliberate fall-through - do not add break */
  178. case HCILL_ASLEEP:
  179. /* acknowledge device wake up */
  180. if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
  181. BT_ERR("cannot acknowledge device wake up");
  182. goto out;
  183. }
  184. break;
  185. default:
  186. /* any other state is illegal */
  187. BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
  188. break;
  189. }
  190. /* send pending packets and change state to HCILL_AWAKE */
  191. __ll_do_awake(ll);
  192. out:
  193. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  194. /* actually send the packets */
  195. hci_uart_tx_wakeup(hu);
  196. }
  197. /*
  198. * Called upon a sleep-indication from the device
  199. */
  200. static void ll_device_want_to_sleep(struct hci_uart *hu)
  201. {
  202. unsigned long flags;
  203. struct ll_struct *ll = hu->priv;
  204. BT_DBG("hu %p", hu);
  205. /* lock hcill state */
  206. spin_lock_irqsave(&ll->hcill_lock, flags);
  207. /* sanity check */
  208. if (ll->hcill_state != HCILL_AWAKE)
  209. BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
  210. /* acknowledge device sleep */
  211. if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
  212. BT_ERR("cannot acknowledge device sleep");
  213. goto out;
  214. }
  215. /* update state */
  216. ll->hcill_state = HCILL_ASLEEP;
  217. out:
  218. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  219. /* actually send the sleep ack packet */
  220. hci_uart_tx_wakeup(hu);
  221. }
  222. /*
  223. * Called upon wake-up-acknowledgement from the device
  224. */
  225. static void ll_device_woke_up(struct hci_uart *hu)
  226. {
  227. unsigned long flags;
  228. struct ll_struct *ll = hu->priv;
  229. BT_DBG("hu %p", hu);
  230. /* lock hcill state */
  231. spin_lock_irqsave(&ll->hcill_lock, flags);
  232. /* sanity check */
  233. if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
  234. BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
  235. /* send pending packets and change state to HCILL_AWAKE */
  236. __ll_do_awake(ll);
  237. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  238. /* actually send the packets */
  239. hci_uart_tx_wakeup(hu);
  240. }
  241. /* Enqueue frame for transmittion (padding, crc, etc) */
  242. /* may be called from two simultaneous tasklets */
  243. static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  244. {
  245. unsigned long flags = 0;
  246. struct ll_struct *ll = hu->priv;
  247. BT_DBG("hu %p skb %p", hu, skb);
  248. /* Prepend skb with frame type */
  249. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  250. /* lock hcill state */
  251. spin_lock_irqsave(&ll->hcill_lock, flags);
  252. /* act according to current state */
  253. switch (ll->hcill_state) {
  254. case HCILL_AWAKE:
  255. BT_DBG("device awake, sending normally");
  256. skb_queue_tail(&ll->txq, skb);
  257. break;
  258. case HCILL_ASLEEP:
  259. BT_DBG("device asleep, waking up and queueing packet");
  260. /* save packet for later */
  261. skb_queue_tail(&ll->tx_wait_q, skb);
  262. /* awake device */
  263. if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
  264. BT_ERR("cannot wake up device");
  265. break;
  266. }
  267. ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
  268. break;
  269. case HCILL_ASLEEP_TO_AWAKE:
  270. BT_DBG("device waking up, queueing packet");
  271. /* transient state; just keep packet for later */
  272. skb_queue_tail(&ll->tx_wait_q, skb);
  273. break;
  274. default:
  275. BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
  276. kfree_skb(skb);
  277. break;
  278. }
  279. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  280. return 0;
  281. }
  282. static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
  283. {
  284. int room = skb_tailroom(ll->rx_skb);
  285. BT_DBG("len %d room %d", len, room);
  286. if (!len) {
  287. hci_recv_frame(hdev, ll->rx_skb);
  288. } else if (len > room) {
  289. BT_ERR("Data length is too large");
  290. kfree_skb(ll->rx_skb);
  291. } else {
  292. ll->rx_state = HCILL_W4_DATA;
  293. ll->rx_count = len;
  294. return len;
  295. }
  296. ll->rx_state = HCILL_W4_PACKET_TYPE;
  297. ll->rx_skb = NULL;
  298. ll->rx_count = 0;
  299. return 0;
  300. }
  301. /* Recv data */
  302. static int ll_recv(struct hci_uart *hu, const void *data, int count)
  303. {
  304. struct ll_struct *ll = hu->priv;
  305. const char *ptr;
  306. struct hci_event_hdr *eh;
  307. struct hci_acl_hdr *ah;
  308. struct hci_sco_hdr *sh;
  309. int len, type, dlen;
  310. BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
  311. ptr = data;
  312. while (count) {
  313. if (ll->rx_count) {
  314. len = min_t(unsigned int, ll->rx_count, count);
  315. memcpy(skb_put(ll->rx_skb, len), ptr, len);
  316. ll->rx_count -= len; count -= len; ptr += len;
  317. if (ll->rx_count)
  318. continue;
  319. switch (ll->rx_state) {
  320. case HCILL_W4_DATA:
  321. BT_DBG("Complete data");
  322. hci_recv_frame(hu->hdev, ll->rx_skb);
  323. ll->rx_state = HCILL_W4_PACKET_TYPE;
  324. ll->rx_skb = NULL;
  325. continue;
  326. case HCILL_W4_EVENT_HDR:
  327. eh = hci_event_hdr(ll->rx_skb);
  328. BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
  329. ll_check_data_len(hu->hdev, ll, eh->plen);
  330. continue;
  331. case HCILL_W4_ACL_HDR:
  332. ah = hci_acl_hdr(ll->rx_skb);
  333. dlen = __le16_to_cpu(ah->dlen);
  334. BT_DBG("ACL header: dlen %d", dlen);
  335. ll_check_data_len(hu->hdev, ll, dlen);
  336. continue;
  337. case HCILL_W4_SCO_HDR:
  338. sh = hci_sco_hdr(ll->rx_skb);
  339. BT_DBG("SCO header: dlen %d", sh->dlen);
  340. ll_check_data_len(hu->hdev, ll, sh->dlen);
  341. continue;
  342. }
  343. }
  344. /* HCILL_W4_PACKET_TYPE */
  345. switch (*ptr) {
  346. case HCI_EVENT_PKT:
  347. BT_DBG("Event packet");
  348. ll->rx_state = HCILL_W4_EVENT_HDR;
  349. ll->rx_count = HCI_EVENT_HDR_SIZE;
  350. type = HCI_EVENT_PKT;
  351. break;
  352. case HCI_ACLDATA_PKT:
  353. BT_DBG("ACL packet");
  354. ll->rx_state = HCILL_W4_ACL_HDR;
  355. ll->rx_count = HCI_ACL_HDR_SIZE;
  356. type = HCI_ACLDATA_PKT;
  357. break;
  358. case HCI_SCODATA_PKT:
  359. BT_DBG("SCO packet");
  360. ll->rx_state = HCILL_W4_SCO_HDR;
  361. ll->rx_count = HCI_SCO_HDR_SIZE;
  362. type = HCI_SCODATA_PKT;
  363. break;
  364. /* HCILL signals */
  365. case HCILL_GO_TO_SLEEP_IND:
  366. BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
  367. ll_device_want_to_sleep(hu);
  368. ptr++; count--;
  369. continue;
  370. case HCILL_GO_TO_SLEEP_ACK:
  371. /* shouldn't happen */
  372. BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
  373. ptr++; count--;
  374. continue;
  375. case HCILL_WAKE_UP_IND:
  376. BT_DBG("HCILL_WAKE_UP_IND packet");
  377. ll_device_want_to_wakeup(hu);
  378. ptr++; count--;
  379. continue;
  380. case HCILL_WAKE_UP_ACK:
  381. BT_DBG("HCILL_WAKE_UP_ACK packet");
  382. ll_device_woke_up(hu);
  383. ptr++; count--;
  384. continue;
  385. default:
  386. BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
  387. hu->hdev->stat.err_rx++;
  388. ptr++; count--;
  389. continue;
  390. }
  391. ptr++; count--;
  392. /* Allocate packet */
  393. ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  394. if (!ll->rx_skb) {
  395. BT_ERR("Can't allocate mem for new packet");
  396. ll->rx_state = HCILL_W4_PACKET_TYPE;
  397. ll->rx_count = 0;
  398. return -ENOMEM;
  399. }
  400. bt_cb(ll->rx_skb)->pkt_type = type;
  401. }
  402. return count;
  403. }
  404. static struct sk_buff *ll_dequeue(struct hci_uart *hu)
  405. {
  406. struct ll_struct *ll = hu->priv;
  407. return skb_dequeue(&ll->txq);
  408. }
  409. static const struct hci_uart_proto llp = {
  410. .id = HCI_UART_LL,
  411. .name = "LL",
  412. .open = ll_open,
  413. .close = ll_close,
  414. .recv = ll_recv,
  415. .enqueue = ll_enqueue,
  416. .dequeue = ll_dequeue,
  417. .flush = ll_flush,
  418. };
  419. int __init ll_init(void)
  420. {
  421. return hci_uart_register_proto(&llp);
  422. }
  423. int __exit ll_deinit(void)
  424. {
  425. return hci_uart_unregister_proto(&llp);
  426. }