0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <linux/types.h>
0015 #include <linux/sched.h>
0016 #include <linux/errno.h>
0017 #include <linux/skbuff.h>
0018
0019 #include <linux/mmc/host.h>
0020 #include <linux/mmc/sdio_ids.h>
0021 #include <linux/mmc/sdio_func.h>
0022
0023 #include <net/bluetooth/bluetooth.h>
0024 #include <net/bluetooth/hci_core.h>
0025
0026 #define VERSION "0.1"
0027
0028 static const struct sdio_device_id btsdio_table[] = {
0029
0030 { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_A) },
0031
0032
0033 { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_B) },
0034
0035
0036 { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_AMP) },
0037
0038 { }
0039 };
0040
0041 MODULE_DEVICE_TABLE(sdio, btsdio_table);
0042
0043 struct btsdio_data {
0044 struct hci_dev *hdev;
0045 struct sdio_func *func;
0046
0047 struct work_struct work;
0048
0049 struct sk_buff_head txq;
0050 };
0051
0052 #define REG_RDAT 0x00
0053 #define REG_TDAT 0x00
0054 #define REG_PC_RRT 0x10
0055 #define REG_PC_WRT 0x11
0056 #define REG_RTC_STAT 0x12
0057 #define REG_RTC_SET 0x12
0058 #define REG_INTRD 0x13
0059 #define REG_CL_INTRD 0x13
0060 #define REG_EN_INTRD 0x14
0061 #define REG_MD_STAT 0x20
0062 #define REG_MD_SET 0x20
0063
0064 static int btsdio_tx_packet(struct btsdio_data *data, struct sk_buff *skb)
0065 {
0066 int err;
0067
0068 BT_DBG("%s", data->hdev->name);
0069
0070
0071 skb_push(skb, 4);
0072 skb->data[0] = (skb->len & 0x0000ff);
0073 skb->data[1] = (skb->len & 0x00ff00) >> 8;
0074 skb->data[2] = (skb->len & 0xff0000) >> 16;
0075 skb->data[3] = hci_skb_pkt_type(skb);
0076
0077 err = sdio_writesb(data->func, REG_TDAT, skb->data, skb->len);
0078 if (err < 0) {
0079 skb_pull(skb, 4);
0080 sdio_writeb(data->func, 0x01, REG_PC_WRT, NULL);
0081 return err;
0082 }
0083
0084 data->hdev->stat.byte_tx += skb->len;
0085
0086 kfree_skb(skb);
0087
0088 return 0;
0089 }
0090
0091 static void btsdio_work(struct work_struct *work)
0092 {
0093 struct btsdio_data *data = container_of(work, struct btsdio_data, work);
0094 struct sk_buff *skb;
0095 int err;
0096
0097 BT_DBG("%s", data->hdev->name);
0098
0099 sdio_claim_host(data->func);
0100
0101 while ((skb = skb_dequeue(&data->txq))) {
0102 err = btsdio_tx_packet(data, skb);
0103 if (err < 0) {
0104 data->hdev->stat.err_tx++;
0105 skb_queue_head(&data->txq, skb);
0106 break;
0107 }
0108 }
0109
0110 sdio_release_host(data->func);
0111 }
0112
0113 static int btsdio_rx_packet(struct btsdio_data *data)
0114 {
0115 u8 hdr[4] __attribute__ ((aligned(4)));
0116 struct sk_buff *skb;
0117 int err, len;
0118
0119 BT_DBG("%s", data->hdev->name);
0120
0121 err = sdio_readsb(data->func, hdr, REG_RDAT, 4);
0122 if (err < 0)
0123 return err;
0124
0125 len = hdr[0] | (hdr[1] << 8) | (hdr[2] << 16);
0126 if (len < 4 || len > 65543)
0127 return -EILSEQ;
0128
0129 skb = bt_skb_alloc(len - 4, GFP_KERNEL);
0130 if (!skb) {
0131
0132
0133
0134
0135 return -ENOMEM;
0136 }
0137
0138 skb_put(skb, len - 4);
0139
0140 err = sdio_readsb(data->func, skb->data, REG_RDAT, len - 4);
0141 if (err < 0) {
0142 kfree_skb(skb);
0143 return err;
0144 }
0145
0146 data->hdev->stat.byte_rx += len;
0147
0148 switch (hdr[3]) {
0149 case HCI_EVENT_PKT:
0150 case HCI_ACLDATA_PKT:
0151 case HCI_SCODATA_PKT:
0152 case HCI_ISODATA_PKT:
0153 hci_skb_pkt_type(skb) = hdr[3];
0154 err = hci_recv_frame(data->hdev, skb);
0155 if (err < 0)
0156 return err;
0157 break;
0158 default:
0159 kfree_skb(skb);
0160 return -EINVAL;
0161 }
0162
0163 sdio_writeb(data->func, 0x00, REG_PC_RRT, NULL);
0164
0165 return 0;
0166 }
0167
0168 static void btsdio_interrupt(struct sdio_func *func)
0169 {
0170 struct btsdio_data *data = sdio_get_drvdata(func);
0171 int intrd;
0172
0173 BT_DBG("%s", data->hdev->name);
0174
0175 intrd = sdio_readb(func, REG_INTRD, NULL);
0176 if (intrd & 0x01) {
0177 sdio_writeb(func, 0x01, REG_CL_INTRD, NULL);
0178
0179 if (btsdio_rx_packet(data) < 0) {
0180 data->hdev->stat.err_rx++;
0181 sdio_writeb(data->func, 0x01, REG_PC_RRT, NULL);
0182 }
0183 }
0184 }
0185
0186 static int btsdio_open(struct hci_dev *hdev)
0187 {
0188 struct btsdio_data *data = hci_get_drvdata(hdev);
0189 int err;
0190
0191 BT_DBG("%s", hdev->name);
0192
0193 sdio_claim_host(data->func);
0194
0195 err = sdio_enable_func(data->func);
0196 if (err < 0)
0197 goto release;
0198
0199 err = sdio_claim_irq(data->func, btsdio_interrupt);
0200 if (err < 0) {
0201 sdio_disable_func(data->func);
0202 goto release;
0203 }
0204
0205 if (data->func->class == SDIO_CLASS_BT_B)
0206 sdio_writeb(data->func, 0x00, REG_MD_SET, NULL);
0207
0208 sdio_writeb(data->func, 0x01, REG_EN_INTRD, NULL);
0209
0210 release:
0211 sdio_release_host(data->func);
0212
0213 return err;
0214 }
0215
0216 static int btsdio_close(struct hci_dev *hdev)
0217 {
0218 struct btsdio_data *data = hci_get_drvdata(hdev);
0219
0220 BT_DBG("%s", hdev->name);
0221
0222 sdio_claim_host(data->func);
0223
0224 sdio_writeb(data->func, 0x00, REG_EN_INTRD, NULL);
0225
0226 sdio_release_irq(data->func);
0227 sdio_disable_func(data->func);
0228
0229 sdio_release_host(data->func);
0230
0231 return 0;
0232 }
0233
0234 static int btsdio_flush(struct hci_dev *hdev)
0235 {
0236 struct btsdio_data *data = hci_get_drvdata(hdev);
0237
0238 BT_DBG("%s", hdev->name);
0239
0240 skb_queue_purge(&data->txq);
0241
0242 return 0;
0243 }
0244
0245 static int btsdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
0246 {
0247 struct btsdio_data *data = hci_get_drvdata(hdev);
0248
0249 BT_DBG("%s", hdev->name);
0250
0251 switch (hci_skb_pkt_type(skb)) {
0252 case HCI_COMMAND_PKT:
0253 hdev->stat.cmd_tx++;
0254 break;
0255
0256 case HCI_ACLDATA_PKT:
0257 hdev->stat.acl_tx++;
0258 break;
0259
0260 case HCI_SCODATA_PKT:
0261 hdev->stat.sco_tx++;
0262 break;
0263
0264 default:
0265 return -EILSEQ;
0266 }
0267
0268 skb_queue_tail(&data->txq, skb);
0269
0270 schedule_work(&data->work);
0271
0272 return 0;
0273 }
0274
0275 static int btsdio_probe(struct sdio_func *func,
0276 const struct sdio_device_id *id)
0277 {
0278 struct btsdio_data *data;
0279 struct hci_dev *hdev;
0280 struct sdio_func_tuple *tuple = func->tuples;
0281 int err;
0282
0283 BT_DBG("func %p id %p class 0x%04x", func, id, func->class);
0284
0285 while (tuple) {
0286 BT_DBG("code 0x%x size %d", tuple->code, tuple->size);
0287 tuple = tuple->next;
0288 }
0289
0290
0291
0292
0293 if (func->vendor == SDIO_VENDOR_ID_BROADCOM &&
0294 !mmc_card_is_removable(func->card->host)) {
0295 switch (func->device) {
0296 case SDIO_DEVICE_ID_BROADCOM_43341:
0297 case SDIO_DEVICE_ID_BROADCOM_43430:
0298 case SDIO_DEVICE_ID_BROADCOM_4345:
0299 case SDIO_DEVICE_ID_BROADCOM_43455:
0300 case SDIO_DEVICE_ID_BROADCOM_4356:
0301 return -ENODEV;
0302 }
0303 }
0304
0305 data = devm_kzalloc(&func->dev, sizeof(*data), GFP_KERNEL);
0306 if (!data)
0307 return -ENOMEM;
0308
0309 data->func = func;
0310
0311 INIT_WORK(&data->work, btsdio_work);
0312
0313 skb_queue_head_init(&data->txq);
0314
0315 hdev = hci_alloc_dev();
0316 if (!hdev)
0317 return -ENOMEM;
0318
0319 hdev->bus = HCI_SDIO;
0320 hci_set_drvdata(hdev, data);
0321
0322 if (id->class == SDIO_CLASS_BT_AMP)
0323 hdev->dev_type = HCI_AMP;
0324 else
0325 hdev->dev_type = HCI_PRIMARY;
0326
0327 data->hdev = hdev;
0328
0329 SET_HCIDEV_DEV(hdev, &func->dev);
0330
0331 hdev->open = btsdio_open;
0332 hdev->close = btsdio_close;
0333 hdev->flush = btsdio_flush;
0334 hdev->send = btsdio_send_frame;
0335
0336 if (func->vendor == 0x0104 && func->device == 0x00c5)
0337 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
0338
0339 err = hci_register_dev(hdev);
0340 if (err < 0) {
0341 hci_free_dev(hdev);
0342 return err;
0343 }
0344
0345 sdio_set_drvdata(func, data);
0346
0347 return 0;
0348 }
0349
0350 static void btsdio_remove(struct sdio_func *func)
0351 {
0352 struct btsdio_data *data = sdio_get_drvdata(func);
0353 struct hci_dev *hdev;
0354
0355 BT_DBG("func %p", func);
0356
0357 if (!data)
0358 return;
0359
0360 hdev = data->hdev;
0361
0362 sdio_set_drvdata(func, NULL);
0363
0364 hci_unregister_dev(hdev);
0365
0366 hci_free_dev(hdev);
0367 }
0368
0369 static struct sdio_driver btsdio_driver = {
0370 .name = "btsdio",
0371 .probe = btsdio_probe,
0372 .remove = btsdio_remove,
0373 .id_table = btsdio_table,
0374 };
0375
0376 module_sdio_driver(btsdio_driver);
0377
0378 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
0379 MODULE_DESCRIPTION("Generic Bluetooth SDIO driver ver " VERSION);
0380 MODULE_VERSION(VERSION);
0381 MODULE_LICENSE("GPL");