btsdio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. *
  3. * Generic Bluetooth SDIO driver
  4. *
  5. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  6. * Copyright (C) 2007 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/mmc/sdio_ids.h>
  33. #include <linux/mmc/sdio_func.h>
  34. #include <net/bluetooth/bluetooth.h>
  35. #include <net/bluetooth/hci_core.h>
  36. #define VERSION "0.1"
  37. static const struct sdio_device_id btsdio_table[] = {
  38. /* Generic Bluetooth Type-A SDIO device */
  39. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_A) },
  40. /* Generic Bluetooth Type-B SDIO device */
  41. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_B) },
  42. /* Generic Bluetooth AMP controller */
  43. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_AMP) },
  44. { } /* Terminating entry */
  45. };
  46. MODULE_DEVICE_TABLE(sdio, btsdio_table);
  47. struct btsdio_data {
  48. struct hci_dev *hdev;
  49. struct sdio_func *func;
  50. struct work_struct work;
  51. struct sk_buff_head txq;
  52. };
  53. #define REG_RDAT 0x00 /* Receiver Data */
  54. #define REG_TDAT 0x00 /* Transmitter Data */
  55. #define REG_PC_RRT 0x10 /* Read Packet Control */
  56. #define REG_PC_WRT 0x11 /* Write Packet Control */
  57. #define REG_RTC_STAT 0x12 /* Retry Control Status */
  58. #define REG_RTC_SET 0x12 /* Retry Control Set */
  59. #define REG_INTRD 0x13 /* Interrupt Indication */
  60. #define REG_CL_INTRD 0x13 /* Interrupt Clear */
  61. #define REG_EN_INTRD 0x14 /* Interrupt Enable */
  62. #define REG_MD_STAT 0x20 /* Bluetooth Mode Status */
  63. static int btsdio_tx_packet(struct btsdio_data *data, struct sk_buff *skb)
  64. {
  65. int err;
  66. BT_DBG("%s", data->hdev->name);
  67. /* Prepend Type-A header */
  68. skb_push(skb, 4);
  69. skb->data[0] = (skb->len & 0x0000ff);
  70. skb->data[1] = (skb->len & 0x00ff00) >> 8;
  71. skb->data[2] = (skb->len & 0xff0000) >> 16;
  72. skb->data[3] = bt_cb(skb)->pkt_type;
  73. err = sdio_writesb(data->func, REG_TDAT, skb->data, skb->len);
  74. if (err < 0) {
  75. skb_pull(skb, 4);
  76. sdio_writeb(data->func, 0x01, REG_PC_WRT, NULL);
  77. return err;
  78. }
  79. data->hdev->stat.byte_tx += skb->len;
  80. kfree_skb(skb);
  81. return 0;
  82. }
  83. static void btsdio_work(struct work_struct *work)
  84. {
  85. struct btsdio_data *data = container_of(work, struct btsdio_data, work);
  86. struct sk_buff *skb;
  87. int err;
  88. BT_DBG("%s", data->hdev->name);
  89. sdio_claim_host(data->func);
  90. while ((skb = skb_dequeue(&data->txq))) {
  91. err = btsdio_tx_packet(data, skb);
  92. if (err < 0) {
  93. data->hdev->stat.err_tx++;
  94. skb_queue_head(&data->txq, skb);
  95. break;
  96. }
  97. }
  98. sdio_release_host(data->func);
  99. }
  100. static int btsdio_rx_packet(struct btsdio_data *data)
  101. {
  102. u8 hdr[4] __attribute__ ((aligned(4)));
  103. struct sk_buff *skb;
  104. int err, len;
  105. BT_DBG("%s", data->hdev->name);
  106. err = sdio_readsb(data->func, hdr, REG_RDAT, 4);
  107. if (err < 0)
  108. return err;
  109. len = hdr[0] | (hdr[1] << 8) | (hdr[2] << 16);
  110. if (len < 4 || len > 65543)
  111. return -EILSEQ;
  112. skb = bt_skb_alloc(len - 4, GFP_KERNEL);
  113. if (!skb) {
  114. /* Out of memory. Prepare a read retry and just
  115. * return with the expectation that the next time
  116. * we're called we'll have more memory. */
  117. return -ENOMEM;
  118. }
  119. skb_put(skb, len - 4);
  120. err = sdio_readsb(data->func, skb->data, REG_RDAT, len - 4);
  121. if (err < 0) {
  122. kfree_skb(skb);
  123. return err;
  124. }
  125. data->hdev->stat.byte_rx += len;
  126. skb->dev = (void *) data->hdev;
  127. bt_cb(skb)->pkt_type = hdr[3];
  128. err = hci_recv_frame(skb);
  129. if (err < 0)
  130. return err;
  131. sdio_writeb(data->func, 0x00, REG_PC_RRT, NULL);
  132. return 0;
  133. }
  134. static void btsdio_interrupt(struct sdio_func *func)
  135. {
  136. struct btsdio_data *data = sdio_get_drvdata(func);
  137. int intrd;
  138. BT_DBG("%s", data->hdev->name);
  139. intrd = sdio_readb(func, REG_INTRD, NULL);
  140. if (intrd & 0x01) {
  141. sdio_writeb(func, 0x01, REG_CL_INTRD, NULL);
  142. if (btsdio_rx_packet(data) < 0) {
  143. data->hdev->stat.err_rx++;
  144. sdio_writeb(data->func, 0x01, REG_PC_RRT, NULL);
  145. }
  146. }
  147. }
  148. static int btsdio_open(struct hci_dev *hdev)
  149. {
  150. struct btsdio_data *data = hdev->driver_data;
  151. int err;
  152. BT_DBG("%s", hdev->name);
  153. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  154. return 0;
  155. sdio_claim_host(data->func);
  156. err = sdio_enable_func(data->func);
  157. if (err < 0) {
  158. clear_bit(HCI_RUNNING, &hdev->flags);
  159. goto release;
  160. }
  161. err = sdio_claim_irq(data->func, btsdio_interrupt);
  162. if (err < 0) {
  163. sdio_disable_func(data->func);
  164. clear_bit(HCI_RUNNING, &hdev->flags);
  165. goto release;
  166. }
  167. if (data->func->class == SDIO_CLASS_BT_B)
  168. sdio_writeb(data->func, 0x00, REG_MD_STAT, NULL);
  169. sdio_writeb(data->func, 0x01, REG_EN_INTRD, NULL);
  170. release:
  171. sdio_release_host(data->func);
  172. return err;
  173. }
  174. static int btsdio_close(struct hci_dev *hdev)
  175. {
  176. struct btsdio_data *data = hdev->driver_data;
  177. BT_DBG("%s", hdev->name);
  178. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  179. return 0;
  180. sdio_claim_host(data->func);
  181. sdio_writeb(data->func, 0x00, REG_EN_INTRD, NULL);
  182. sdio_release_irq(data->func);
  183. sdio_disable_func(data->func);
  184. sdio_release_host(data->func);
  185. return 0;
  186. }
  187. static int btsdio_flush(struct hci_dev *hdev)
  188. {
  189. struct btsdio_data *data = hdev->driver_data;
  190. BT_DBG("%s", hdev->name);
  191. skb_queue_purge(&data->txq);
  192. return 0;
  193. }
  194. static int btsdio_send_frame(struct sk_buff *skb)
  195. {
  196. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  197. struct btsdio_data *data = hdev->driver_data;
  198. BT_DBG("%s", hdev->name);
  199. if (!test_bit(HCI_RUNNING, &hdev->flags))
  200. return -EBUSY;
  201. switch (bt_cb(skb)->pkt_type) {
  202. case HCI_COMMAND_PKT:
  203. hdev->stat.cmd_tx++;
  204. break;
  205. case HCI_ACLDATA_PKT:
  206. hdev->stat.acl_tx++;
  207. break;
  208. case HCI_SCODATA_PKT:
  209. hdev->stat.sco_tx++;
  210. break;
  211. default:
  212. return -EILSEQ;
  213. }
  214. skb_queue_tail(&data->txq, skb);
  215. schedule_work(&data->work);
  216. return 0;
  217. }
  218. static void btsdio_destruct(struct hci_dev *hdev)
  219. {
  220. struct btsdio_data *data = hdev->driver_data;
  221. BT_DBG("%s", hdev->name);
  222. kfree(data);
  223. }
  224. static int btsdio_probe(struct sdio_func *func,
  225. const struct sdio_device_id *id)
  226. {
  227. struct btsdio_data *data;
  228. struct hci_dev *hdev;
  229. struct sdio_func_tuple *tuple = func->tuples;
  230. int err;
  231. BT_DBG("func %p id %p class 0x%04x", func, id, func->class);
  232. while (tuple) {
  233. BT_DBG("code 0x%x size %d", tuple->code, tuple->size);
  234. tuple = tuple->next;
  235. }
  236. data = kzalloc(sizeof(*data), GFP_KERNEL);
  237. if (!data)
  238. return -ENOMEM;
  239. data->func = func;
  240. INIT_WORK(&data->work, btsdio_work);
  241. skb_queue_head_init(&data->txq);
  242. hdev = hci_alloc_dev();
  243. if (!hdev) {
  244. kfree(data);
  245. return -ENOMEM;
  246. }
  247. hdev->bus = HCI_SDIO;
  248. hdev->driver_data = data;
  249. if (id->class == SDIO_CLASS_BT_AMP)
  250. hdev->dev_type = HCI_AMP;
  251. else
  252. hdev->dev_type = HCI_BREDR;
  253. data->hdev = hdev;
  254. SET_HCIDEV_DEV(hdev, &func->dev);
  255. hdev->open = btsdio_open;
  256. hdev->close = btsdio_close;
  257. hdev->flush = btsdio_flush;
  258. hdev->send = btsdio_send_frame;
  259. hdev->destruct = btsdio_destruct;
  260. hdev->owner = THIS_MODULE;
  261. err = hci_register_dev(hdev);
  262. if (err < 0) {
  263. hci_free_dev(hdev);
  264. kfree(data);
  265. return err;
  266. }
  267. sdio_set_drvdata(func, data);
  268. return 0;
  269. }
  270. static void btsdio_remove(struct sdio_func *func)
  271. {
  272. struct btsdio_data *data = sdio_get_drvdata(func);
  273. struct hci_dev *hdev;
  274. BT_DBG("func %p", func);
  275. if (!data)
  276. return;
  277. hdev = data->hdev;
  278. sdio_set_drvdata(func, NULL);
  279. hci_unregister_dev(hdev);
  280. hci_free_dev(hdev);
  281. }
  282. static struct sdio_driver btsdio_driver = {
  283. .name = "btsdio",
  284. .probe = btsdio_probe,
  285. .remove = btsdio_remove,
  286. .id_table = btsdio_table,
  287. };
  288. static int __init btsdio_init(void)
  289. {
  290. BT_INFO("Generic Bluetooth SDIO driver ver %s", VERSION);
  291. return sdio_register_driver(&btsdio_driver);
  292. }
  293. static void __exit btsdio_exit(void)
  294. {
  295. sdio_unregister_driver(&btsdio_driver);
  296. }
  297. module_init(btsdio_init);
  298. module_exit(btsdio_exit);
  299. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  300. MODULE_DESCRIPTION("Generic Bluetooth SDIO driver ver " VERSION);
  301. MODULE_VERSION(VERSION);
  302. MODULE_LICENSE("GPL");