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