hci_ll.c 12 KB

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