hci_ll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. } __attribute__((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. skb->dev = (void *) hu->hdev;
  100. /* send packet */
  101. skb_queue_tail(&ll->txq, skb);
  102. out:
  103. return err;
  104. }
  105. /* Initialize protocol */
  106. static int ll_open(struct hci_uart *hu)
  107. {
  108. struct ll_struct *ll;
  109. BT_DBG("hu %p", hu);
  110. ll = kzalloc(sizeof(*ll), GFP_ATOMIC);
  111. if (!ll)
  112. return -ENOMEM;
  113. skb_queue_head_init(&ll->txq);
  114. skb_queue_head_init(&ll->tx_wait_q);
  115. spin_lock_init(&ll->hcill_lock);
  116. ll->hcill_state = HCILL_AWAKE;
  117. hu->priv = ll;
  118. return 0;
  119. }
  120. /* Flush protocol data */
  121. static int ll_flush(struct hci_uart *hu)
  122. {
  123. struct ll_struct *ll = hu->priv;
  124. BT_DBG("hu %p", hu);
  125. skb_queue_purge(&ll->tx_wait_q);
  126. skb_queue_purge(&ll->txq);
  127. return 0;
  128. }
  129. /* Close protocol */
  130. static int ll_close(struct hci_uart *hu)
  131. {
  132. struct ll_struct *ll = hu->priv;
  133. BT_DBG("hu %p", hu);
  134. skb_queue_purge(&ll->tx_wait_q);
  135. skb_queue_purge(&ll->txq);
  136. if (ll->rx_skb)
  137. kfree_skb(ll->rx_skb);
  138. hu->priv = NULL;
  139. kfree(ll);
  140. return 0;
  141. }
  142. /*
  143. * internal function, which does common work of the device wake up process:
  144. * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
  145. * 2. changes internal state to HCILL_AWAKE.
  146. * Note: assumes that hcill_lock spinlock is taken,
  147. * shouldn't be called otherwise!
  148. */
  149. static void __ll_do_awake(struct ll_struct *ll)
  150. {
  151. struct sk_buff *skb = NULL;
  152. while ((skb = skb_dequeue(&ll->tx_wait_q)))
  153. skb_queue_tail(&ll->txq, skb);
  154. ll->hcill_state = HCILL_AWAKE;
  155. }
  156. /*
  157. * Called upon a wake-up-indication from the device
  158. */
  159. static void ll_device_want_to_wakeup(struct hci_uart *hu)
  160. {
  161. unsigned long flags;
  162. struct ll_struct *ll = hu->priv;
  163. BT_DBG("hu %p", hu);
  164. /* lock hcill state */
  165. spin_lock_irqsave(&ll->hcill_lock, flags);
  166. switch (ll->hcill_state) {
  167. case HCILL_ASLEEP_TO_AWAKE:
  168. /*
  169. * This state means that both the host and the BRF chip
  170. * have simultaneously sent a wake-up-indication packet.
  171. * Traditionaly, in this case, receiving a wake-up-indication
  172. * was enough and an additional wake-up-ack wasn't needed.
  173. * This has changed with the BRF6350, which does require an
  174. * explicit wake-up-ack. Other BRF versions, which do not
  175. * require an explicit ack here, do accept it, thus it is
  176. * perfectly safe to always send one.
  177. */
  178. BT_DBG("dual wake-up-indication");
  179. /* deliberate fall-through - do not add break */
  180. case HCILL_ASLEEP:
  181. /* acknowledge device wake up */
  182. if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
  183. BT_ERR("cannot acknowledge device wake up");
  184. goto out;
  185. }
  186. break;
  187. default:
  188. /* any other state is illegal */
  189. BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
  190. break;
  191. }
  192. /* send pending packets and change state to HCILL_AWAKE */
  193. __ll_do_awake(ll);
  194. out:
  195. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  196. /* actually send the packets */
  197. hci_uart_tx_wakeup(hu);
  198. }
  199. /*
  200. * Called upon a sleep-indication from the device
  201. */
  202. static void ll_device_want_to_sleep(struct hci_uart *hu)
  203. {
  204. unsigned long flags;
  205. struct ll_struct *ll = hu->priv;
  206. BT_DBG("hu %p", hu);
  207. /* lock hcill state */
  208. spin_lock_irqsave(&ll->hcill_lock, flags);
  209. /* sanity check */
  210. if (ll->hcill_state != HCILL_AWAKE)
  211. BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
  212. /* acknowledge device sleep */
  213. if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
  214. BT_ERR("cannot acknowledge device sleep");
  215. goto out;
  216. }
  217. /* update state */
  218. ll->hcill_state = HCILL_ASLEEP;
  219. out:
  220. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  221. /* actually send the sleep ack packet */
  222. hci_uart_tx_wakeup(hu);
  223. }
  224. /*
  225. * Called upon wake-up-acknowledgement from the device
  226. */
  227. static void ll_device_woke_up(struct hci_uart *hu)
  228. {
  229. unsigned long flags;
  230. struct ll_struct *ll = hu->priv;
  231. BT_DBG("hu %p", hu);
  232. /* lock hcill state */
  233. spin_lock_irqsave(&ll->hcill_lock, flags);
  234. /* sanity check */
  235. if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
  236. BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
  237. /* send pending packets and change state to HCILL_AWAKE */
  238. __ll_do_awake(ll);
  239. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  240. /* actually send the packets */
  241. hci_uart_tx_wakeup(hu);
  242. }
  243. /* Enqueue frame for transmittion (padding, crc, etc) */
  244. /* may be called from two simultaneous tasklets */
  245. static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  246. {
  247. unsigned long flags = 0;
  248. struct ll_struct *ll = hu->priv;
  249. BT_DBG("hu %p skb %p", hu, skb);
  250. /* Prepend skb with frame type */
  251. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  252. /* lock hcill state */
  253. spin_lock_irqsave(&ll->hcill_lock, flags);
  254. /* act according to current state */
  255. switch (ll->hcill_state) {
  256. case HCILL_AWAKE:
  257. BT_DBG("device awake, sending normally");
  258. skb_queue_tail(&ll->txq, skb);
  259. break;
  260. case HCILL_ASLEEP:
  261. BT_DBG("device asleep, waking up and queueing packet");
  262. /* save packet for later */
  263. skb_queue_tail(&ll->tx_wait_q, skb);
  264. /* awake device */
  265. if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
  266. BT_ERR("cannot wake up device");
  267. break;
  268. }
  269. ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
  270. break;
  271. case HCILL_ASLEEP_TO_AWAKE:
  272. BT_DBG("device waking up, queueing packet");
  273. /* transient state; just keep packet for later */
  274. skb_queue_tail(&ll->tx_wait_q, skb);
  275. break;
  276. default:
  277. BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
  278. kfree_skb(skb);
  279. break;
  280. }
  281. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  282. return 0;
  283. }
  284. static inline int ll_check_data_len(struct ll_struct *ll, int len)
  285. {
  286. register int room = skb_tailroom(ll->rx_skb);
  287. BT_DBG("len %d room %d", len, room);
  288. if (!len) {
  289. hci_recv_frame(ll->rx_skb);
  290. } else if (len > room) {
  291. BT_ERR("Data length is too large");
  292. kfree_skb(ll->rx_skb);
  293. } else {
  294. ll->rx_state = HCILL_W4_DATA;
  295. ll->rx_count = len;
  296. return len;
  297. }
  298. ll->rx_state = HCILL_W4_PACKET_TYPE;
  299. ll->rx_skb = NULL;
  300. ll->rx_count = 0;
  301. return 0;
  302. }
  303. /* Recv data */
  304. static int ll_recv(struct hci_uart *hu, void *data, int count)
  305. {
  306. struct ll_struct *ll = hu->priv;
  307. register char *ptr;
  308. struct hci_event_hdr *eh;
  309. struct hci_acl_hdr *ah;
  310. struct hci_sco_hdr *sh;
  311. register int len, type, dlen;
  312. BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
  313. ptr = data;
  314. while (count) {
  315. if (ll->rx_count) {
  316. len = min_t(unsigned int, ll->rx_count, count);
  317. memcpy(skb_put(ll->rx_skb, len), ptr, len);
  318. ll->rx_count -= len; count -= len; ptr += len;
  319. if (ll->rx_count)
  320. continue;
  321. switch (ll->rx_state) {
  322. case HCILL_W4_DATA:
  323. BT_DBG("Complete data");
  324. hci_recv_frame(ll->rx_skb);
  325. ll->rx_state = HCILL_W4_PACKET_TYPE;
  326. ll->rx_skb = NULL;
  327. continue;
  328. case HCILL_W4_EVENT_HDR:
  329. eh = (struct hci_event_hdr *) ll->rx_skb->data;
  330. BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
  331. ll_check_data_len(ll, eh->plen);
  332. continue;
  333. case HCILL_W4_ACL_HDR:
  334. ah = (struct hci_acl_hdr *) ll->rx_skb->data;
  335. dlen = __le16_to_cpu(ah->dlen);
  336. BT_DBG("ACL header: dlen %d", dlen);
  337. ll_check_data_len(ll, dlen);
  338. continue;
  339. case HCILL_W4_SCO_HDR:
  340. sh = (struct hci_sco_hdr *) ll->rx_skb->data;
  341. BT_DBG("SCO header: dlen %d", sh->dlen);
  342. ll_check_data_len(ll, sh->dlen);
  343. continue;
  344. }
  345. }
  346. /* HCILL_W4_PACKET_TYPE */
  347. switch (*ptr) {
  348. case HCI_EVENT_PKT:
  349. BT_DBG("Event packet");
  350. ll->rx_state = HCILL_W4_EVENT_HDR;
  351. ll->rx_count = HCI_EVENT_HDR_SIZE;
  352. type = HCI_EVENT_PKT;
  353. break;
  354. case HCI_ACLDATA_PKT:
  355. BT_DBG("ACL packet");
  356. ll->rx_state = HCILL_W4_ACL_HDR;
  357. ll->rx_count = HCI_ACL_HDR_SIZE;
  358. type = HCI_ACLDATA_PKT;
  359. break;
  360. case HCI_SCODATA_PKT:
  361. BT_DBG("SCO packet");
  362. ll->rx_state = HCILL_W4_SCO_HDR;
  363. ll->rx_count = HCI_SCO_HDR_SIZE;
  364. type = HCI_SCODATA_PKT;
  365. break;
  366. /* HCILL signals */
  367. case HCILL_GO_TO_SLEEP_IND:
  368. BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
  369. ll_device_want_to_sleep(hu);
  370. ptr++; count--;
  371. continue;
  372. case HCILL_GO_TO_SLEEP_ACK:
  373. /* shouldn't happen */
  374. BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
  375. ptr++; count--;
  376. continue;
  377. case HCILL_WAKE_UP_IND:
  378. BT_DBG("HCILL_WAKE_UP_IND packet");
  379. ll_device_want_to_wakeup(hu);
  380. ptr++; count--;
  381. continue;
  382. case HCILL_WAKE_UP_ACK:
  383. BT_DBG("HCILL_WAKE_UP_ACK packet");
  384. ll_device_woke_up(hu);
  385. ptr++; count--;
  386. continue;
  387. default:
  388. BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
  389. hu->hdev->stat.err_rx++;
  390. ptr++; count--;
  391. continue;
  392. };
  393. ptr++; count--;
  394. /* Allocate packet */
  395. ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  396. if (!ll->rx_skb) {
  397. BT_ERR("Can't allocate mem for new packet");
  398. ll->rx_state = HCILL_W4_PACKET_TYPE;
  399. ll->rx_count = 0;
  400. return 0;
  401. }
  402. ll->rx_skb->dev = (void *) hu->hdev;
  403. bt_cb(ll->rx_skb)->pkt_type = type;
  404. }
  405. return count;
  406. }
  407. static struct sk_buff *ll_dequeue(struct hci_uart *hu)
  408. {
  409. struct ll_struct *ll = hu->priv;
  410. return skb_dequeue(&ll->txq);
  411. }
  412. static struct hci_uart_proto llp = {
  413. .id = HCI_UART_LL,
  414. .open = ll_open,
  415. .close = ll_close,
  416. .recv = ll_recv,
  417. .enqueue = ll_enqueue,
  418. .dequeue = ll_dequeue,
  419. .flush = ll_flush,
  420. };
  421. int ll_init(void)
  422. {
  423. int err = hci_uart_register_proto(&llp);
  424. if (!err)
  425. BT_INFO("HCILL protocol initialized");
  426. else
  427. BT_ERR("HCILL protocol registration failed");
  428. return err;
  429. }
  430. int ll_deinit(void)
  431. {
  432. return hci_uart_unregister_proto(&llp);
  433. }