btsdio.c 8.3 KB

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