btsdio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/host.h>
  33. #include <linux/mmc/sdio_ids.h>
  34. #include <linux/mmc/sdio_func.h>
  35. #include <net/bluetooth/bluetooth.h>
  36. #include <net/bluetooth/hci_core.h>
  37. #define VERSION "0.1"
  38. static const struct sdio_device_id btsdio_table[] = {
  39. /* Generic Bluetooth Type-A SDIO device */
  40. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_A) },
  41. /* Generic Bluetooth Type-B SDIO device */
  42. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_B) },
  43. /* Generic Bluetooth AMP controller */
  44. { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_AMP) },
  45. { } /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(sdio, btsdio_table);
  48. struct btsdio_data {
  49. struct hci_dev *hdev;
  50. struct sdio_func *func;
  51. struct work_struct work;
  52. struct sk_buff_head txq;
  53. };
  54. #define REG_RDAT 0x00 /* Receiver Data */
  55. #define REG_TDAT 0x00 /* Transmitter Data */
  56. #define REG_PC_RRT 0x10 /* Read Packet Control */
  57. #define REG_PC_WRT 0x11 /* Write Packet Control */
  58. #define REG_RTC_STAT 0x12 /* Retry Control Status */
  59. #define REG_RTC_SET 0x12 /* Retry Control Set */
  60. #define REG_INTRD 0x13 /* Interrupt Indication */
  61. #define REG_CL_INTRD 0x13 /* Interrupt Clear */
  62. #define REG_EN_INTRD 0x14 /* Interrupt Enable */
  63. #define REG_MD_STAT 0x20 /* Bluetooth Mode Status */
  64. #define REG_MD_SET 0x20 /* Bluetooth Mode Set */
  65. static int btsdio_tx_packet(struct btsdio_data *data, struct sk_buff *skb)
  66. {
  67. int err;
  68. BT_DBG("%s", data->hdev->name);
  69. /* Prepend Type-A header */
  70. skb_push(skb, 4);
  71. skb->data[0] = (skb->len & 0x0000ff);
  72. skb->data[1] = (skb->len & 0x00ff00) >> 8;
  73. skb->data[2] = (skb->len & 0xff0000) >> 16;
  74. skb->data[3] = hci_skb_pkt_type(skb);
  75. err = sdio_writesb(data->func, REG_TDAT, skb->data, skb->len);
  76. if (err < 0) {
  77. skb_pull(skb, 4);
  78. sdio_writeb(data->func, 0x01, REG_PC_WRT, NULL);
  79. return err;
  80. }
  81. data->hdev->stat.byte_tx += skb->len;
  82. kfree_skb(skb);
  83. return 0;
  84. }
  85. static void btsdio_work(struct work_struct *work)
  86. {
  87. struct btsdio_data *data = container_of(work, struct btsdio_data, work);
  88. struct sk_buff *skb;
  89. int err;
  90. BT_DBG("%s", data->hdev->name);
  91. sdio_claim_host(data->func);
  92. while ((skb = skb_dequeue(&data->txq))) {
  93. err = btsdio_tx_packet(data, skb);
  94. if (err < 0) {
  95. data->hdev->stat.err_tx++;
  96. skb_queue_head(&data->txq, skb);
  97. break;
  98. }
  99. }
  100. sdio_release_host(data->func);
  101. }
  102. static int btsdio_rx_packet(struct btsdio_data *data)
  103. {
  104. u8 hdr[4] __attribute__ ((aligned(4)));
  105. struct sk_buff *skb;
  106. int err, len;
  107. BT_DBG("%s", data->hdev->name);
  108. err = sdio_readsb(data->func, hdr, REG_RDAT, 4);
  109. if (err < 0)
  110. return err;
  111. len = hdr[0] | (hdr[1] << 8) | (hdr[2] << 16);
  112. if (len < 4 || len > 65543)
  113. return -EILSEQ;
  114. skb = bt_skb_alloc(len - 4, GFP_KERNEL);
  115. if (!skb) {
  116. /* Out of memory. Prepare a read retry and just
  117. * return with the expectation that the next time
  118. * we're called we'll have more memory.
  119. */
  120. return -ENOMEM;
  121. }
  122. skb_put(skb, len - 4);
  123. err = sdio_readsb(data->func, skb->data, REG_RDAT, len - 4);
  124. if (err < 0) {
  125. kfree_skb(skb);
  126. return err;
  127. }
  128. data->hdev->stat.byte_rx += len;
  129. hci_skb_pkt_type(skb) = hdr[3];
  130. err = hci_recv_frame(data->hdev, skb);
  131. if (err < 0)
  132. return err;
  133. sdio_writeb(data->func, 0x00, REG_PC_RRT, NULL);
  134. return 0;
  135. }
  136. static void btsdio_interrupt(struct sdio_func *func)
  137. {
  138. struct btsdio_data *data = sdio_get_drvdata(func);
  139. int intrd;
  140. BT_DBG("%s", data->hdev->name);
  141. intrd = sdio_readb(func, REG_INTRD, NULL);
  142. if (intrd & 0x01) {
  143. sdio_writeb(func, 0x01, REG_CL_INTRD, NULL);
  144. if (btsdio_rx_packet(data) < 0) {
  145. data->hdev->stat.err_rx++;
  146. sdio_writeb(data->func, 0x01, REG_PC_RRT, NULL);
  147. }
  148. }
  149. }
  150. static int btsdio_open(struct hci_dev *hdev)
  151. {
  152. struct btsdio_data *data = hci_get_drvdata(hdev);
  153. int err;
  154. BT_DBG("%s", hdev->name);
  155. sdio_claim_host(data->func);
  156. err = sdio_enable_func(data->func);
  157. if (err < 0)
  158. goto release;
  159. err = sdio_claim_irq(data->func, btsdio_interrupt);
  160. if (err < 0) {
  161. sdio_disable_func(data->func);
  162. goto release;
  163. }
  164. if (data->func->class == SDIO_CLASS_BT_B)
  165. sdio_writeb(data->func, 0x00, REG_MD_SET, NULL);
  166. sdio_writeb(data->func, 0x01, REG_EN_INTRD, NULL);
  167. release:
  168. sdio_release_host(data->func);
  169. return err;
  170. }
  171. static int btsdio_close(struct hci_dev *hdev)
  172. {
  173. struct btsdio_data *data = hci_get_drvdata(hdev);
  174. BT_DBG("%s", hdev->name);
  175. sdio_claim_host(data->func);
  176. sdio_writeb(data->func, 0x00, REG_EN_INTRD, NULL);
  177. sdio_release_irq(data->func);
  178. sdio_disable_func(data->func);
  179. sdio_release_host(data->func);
  180. return 0;
  181. }
  182. static int btsdio_flush(struct hci_dev *hdev)
  183. {
  184. struct btsdio_data *data = hci_get_drvdata(hdev);
  185. BT_DBG("%s", hdev->name);
  186. skb_queue_purge(&data->txq);
  187. return 0;
  188. }
  189. static int btsdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  190. {
  191. struct btsdio_data *data = hci_get_drvdata(hdev);
  192. BT_DBG("%s", hdev->name);
  193. switch (hci_skb_pkt_type(skb)) {
  194. case HCI_COMMAND_PKT:
  195. hdev->stat.cmd_tx++;
  196. break;
  197. case HCI_ACLDATA_PKT:
  198. hdev->stat.acl_tx++;
  199. break;
  200. case HCI_SCODATA_PKT:
  201. hdev->stat.sco_tx++;
  202. break;
  203. default:
  204. return -EILSEQ;
  205. }
  206. skb_queue_tail(&data->txq, skb);
  207. schedule_work(&data->work);
  208. return 0;
  209. }
  210. static int btsdio_probe(struct sdio_func *func,
  211. const struct sdio_device_id *id)
  212. {
  213. struct btsdio_data *data;
  214. struct hci_dev *hdev;
  215. struct sdio_func_tuple *tuple = func->tuples;
  216. int err;
  217. BT_DBG("func %p id %p class 0x%04x", func, id, func->class);
  218. while (tuple) {
  219. BT_DBG("code 0x%x size %d", tuple->code, tuple->size);
  220. tuple = tuple->next;
  221. }
  222. /* BCM43341 devices soldered onto the PCB (non-removable) use an
  223. * uart connection for bluetooth, ignore the BT SDIO interface.
  224. */
  225. if (func->vendor == SDIO_VENDOR_ID_BROADCOM &&
  226. func->device == SDIO_DEVICE_ID_BROADCOM_43341 &&
  227. !mmc_card_is_removable(func->card->host))
  228. return -ENODEV;
  229. data = devm_kzalloc(&func->dev, sizeof(*data), GFP_KERNEL);
  230. if (!data)
  231. return -ENOMEM;
  232. data->func = func;
  233. INIT_WORK(&data->work, btsdio_work);
  234. skb_queue_head_init(&data->txq);
  235. hdev = hci_alloc_dev();
  236. if (!hdev)
  237. return -ENOMEM;
  238. hdev->bus = HCI_SDIO;
  239. hci_set_drvdata(hdev, data);
  240. if (id->class == SDIO_CLASS_BT_AMP)
  241. hdev->dev_type = HCI_AMP;
  242. else
  243. hdev->dev_type = HCI_PRIMARY;
  244. data->hdev = hdev;
  245. SET_HCIDEV_DEV(hdev, &func->dev);
  246. hdev->open = btsdio_open;
  247. hdev->close = btsdio_close;
  248. hdev->flush = btsdio_flush;
  249. hdev->send = btsdio_send_frame;
  250. if (func->vendor == 0x0104 && func->device == 0x00c5)
  251. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  252. err = hci_register_dev(hdev);
  253. if (err < 0) {
  254. hci_free_dev(hdev);
  255. return err;
  256. }
  257. sdio_set_drvdata(func, data);
  258. return 0;
  259. }
  260. static void btsdio_remove(struct sdio_func *func)
  261. {
  262. struct btsdio_data *data = sdio_get_drvdata(func);
  263. struct hci_dev *hdev;
  264. BT_DBG("func %p", func);
  265. if (!data)
  266. return;
  267. hdev = data->hdev;
  268. sdio_set_drvdata(func, NULL);
  269. hci_unregister_dev(hdev);
  270. hci_free_dev(hdev);
  271. }
  272. static struct sdio_driver btsdio_driver = {
  273. .name = "btsdio",
  274. .probe = btsdio_probe,
  275. .remove = btsdio_remove,
  276. .id_table = btsdio_table,
  277. };
  278. static int __init btsdio_init(void)
  279. {
  280. BT_INFO("Generic Bluetooth SDIO driver ver %s", VERSION);
  281. return sdio_register_driver(&btsdio_driver);
  282. }
  283. static void __exit btsdio_exit(void)
  284. {
  285. sdio_unregister_driver(&btsdio_driver);
  286. }
  287. module_init(btsdio_init);
  288. module_exit(btsdio_exit);
  289. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  290. MODULE_DESCRIPTION("Generic Bluetooth SDIO driver ver " VERSION);
  291. MODULE_VERSION(VERSION);
  292. MODULE_LICENSE("GPL");