3 * Generic Bluetooth SDIO driver
5 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 * Copyright (C) 2007 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/skbuff.h>
34 #include <linux/mmc/sdio_ids.h>
35 #include <linux/mmc/sdio_func.h>
37 #include <net/bluetooth/bluetooth.h>
38 #include <net/bluetooth/hci_core.h>
42 static const struct sdio_device_id btsdio_table
[] = {
43 /* Generic Bluetooth Type-A SDIO device */
44 { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_A
) },
46 /* Generic Bluetooth Type-B SDIO device */
47 { SDIO_DEVICE_CLASS(SDIO_CLASS_BT_B
) },
49 { } /* Terminating entry */
52 MODULE_DEVICE_TABLE(sdio
, btsdio_table
);
56 struct sdio_func
*func
;
58 struct work_struct work
;
60 struct sk_buff_head txq
;
63 #define REG_RDAT 0x00 /* Receiver Data */
64 #define REG_TDAT 0x00 /* Transmitter Data */
65 #define REG_PC_RRT 0x10 /* Read Packet Control */
66 #define REG_PC_WRT 0x11 /* Write Packet Control */
67 #define REG_RTC_STAT 0x12 /* Retry Control Status */
68 #define REG_RTC_SET 0x12 /* Retry Control Set */
69 #define REG_INTRD 0x13 /* Interrupt Indication */
70 #define REG_CL_INTRD 0x13 /* Interrupt Clear */
71 #define REG_EN_INTRD 0x14 /* Interrupt Enable */
72 #define REG_MD_STAT 0x20 /* Bluetooth Mode Status */
74 static int btsdio_tx_packet(struct btsdio_data
*data
, struct sk_buff
*skb
)
78 BT_DBG("%s", data
->hdev
->name
);
80 /* Prepend Type-A header */
82 skb
->data
[0] = (skb
->len
& 0x0000ff);
83 skb
->data
[1] = (skb
->len
& 0x00ff00) >> 8;
84 skb
->data
[2] = (skb
->len
& 0xff0000) >> 16;
85 skb
->data
[3] = bt_cb(skb
)->pkt_type
;
87 err
= sdio_writesb(data
->func
, REG_TDAT
, skb
->data
, skb
->len
);
90 sdio_writeb(data
->func
, 0x01, REG_PC_WRT
, NULL
);
94 data
->hdev
->stat
.byte_tx
+= skb
->len
;
101 static void btsdio_work(struct work_struct
*work
)
103 struct btsdio_data
*data
= container_of(work
, struct btsdio_data
, work
);
107 BT_DBG("%s", data
->hdev
->name
);
109 sdio_claim_host(data
->func
);
111 while ((skb
= skb_dequeue(&data
->txq
))) {
112 err
= btsdio_tx_packet(data
, skb
);
114 data
->hdev
->stat
.err_tx
++;
115 skb_queue_head(&data
->txq
, skb
);
120 sdio_release_host(data
->func
);
123 static int btsdio_rx_packet(struct btsdio_data
*data
)
125 u8 hdr
[4] __attribute__ ((aligned(4)));
129 BT_DBG("%s", data
->hdev
->name
);
131 err
= sdio_readsb(data
->func
, hdr
, REG_RDAT
, 4);
135 len
= hdr
[0] | (hdr
[1] << 8) | (hdr
[2] << 16);
136 if (len
< 4 || len
> 65543)
139 skb
= bt_skb_alloc(len
- 4, GFP_KERNEL
);
141 /* Out of memory. Prepare a read retry and just
142 * return with the expectation that the next time
143 * we're called we'll have more memory. */
147 skb_put(skb
, len
- 4);
149 err
= sdio_readsb(data
->func
, skb
->data
, REG_RDAT
, len
- 4);
155 data
->hdev
->stat
.byte_rx
+= len
;
157 skb
->dev
= (void *) data
->hdev
;
158 bt_cb(skb
)->pkt_type
= hdr
[3];
160 err
= hci_recv_frame(skb
);
164 sdio_writeb(data
->func
, 0x00, REG_PC_RRT
, NULL
);
169 static void btsdio_interrupt(struct sdio_func
*func
)
171 struct btsdio_data
*data
= sdio_get_drvdata(func
);
174 BT_DBG("%s", data
->hdev
->name
);
176 intrd
= sdio_readb(func
, REG_INTRD
, NULL
);
178 sdio_writeb(func
, 0x01, REG_CL_INTRD
, NULL
);
180 if (btsdio_rx_packet(data
) < 0) {
181 data
->hdev
->stat
.err_rx
++;
182 sdio_writeb(data
->func
, 0x01, REG_PC_RRT
, NULL
);
187 static int btsdio_open(struct hci_dev
*hdev
)
189 struct btsdio_data
*data
= hdev
->driver_data
;
192 BT_DBG("%s", hdev
->name
);
194 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
197 sdio_claim_host(data
->func
);
199 err
= sdio_enable_func(data
->func
);
201 clear_bit(HCI_RUNNING
, &hdev
->flags
);
205 err
= sdio_claim_irq(data
->func
, btsdio_interrupt
);
207 sdio_disable_func(data
->func
);
208 clear_bit(HCI_RUNNING
, &hdev
->flags
);
212 if (data
->func
->class == SDIO_CLASS_BT_B
)
213 sdio_writeb(data
->func
, 0x00, REG_MD_STAT
, NULL
);
215 sdio_writeb(data
->func
, 0x01, REG_EN_INTRD
, NULL
);
218 sdio_release_host(data
->func
);
223 static int btsdio_close(struct hci_dev
*hdev
)
225 struct btsdio_data
*data
= hdev
->driver_data
;
227 BT_DBG("%s", hdev
->name
);
229 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
232 sdio_claim_host(data
->func
);
234 sdio_writeb(data
->func
, 0x00, REG_EN_INTRD
, NULL
);
236 sdio_release_irq(data
->func
);
237 sdio_disable_func(data
->func
);
239 sdio_release_host(data
->func
);
244 static int btsdio_flush(struct hci_dev
*hdev
)
246 struct btsdio_data
*data
= hdev
->driver_data
;
248 BT_DBG("%s", hdev
->name
);
250 skb_queue_purge(&data
->txq
);
255 static int btsdio_send_frame(struct sk_buff
*skb
)
257 struct hci_dev
*hdev
= (struct hci_dev
*) skb
->dev
;
258 struct btsdio_data
*data
= hdev
->driver_data
;
260 BT_DBG("%s", hdev
->name
);
262 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
265 switch (bt_cb(skb
)->pkt_type
) {
266 case HCI_COMMAND_PKT
:
270 case HCI_ACLDATA_PKT
:
274 case HCI_SCODATA_PKT
:
282 skb_queue_tail(&data
->txq
, skb
);
284 schedule_work(&data
->work
);
289 static void btsdio_destruct(struct hci_dev
*hdev
)
291 struct btsdio_data
*data
= hdev
->driver_data
;
293 BT_DBG("%s", hdev
->name
);
298 static int btsdio_probe(struct sdio_func
*func
,
299 const struct sdio_device_id
*id
)
301 struct btsdio_data
*data
;
302 struct hci_dev
*hdev
;
303 struct sdio_func_tuple
*tuple
= func
->tuples
;
306 BT_DBG("func %p id %p class 0x%04x", func
, id
, func
->class);
309 BT_DBG("code 0x%x size %d", tuple
->code
, tuple
->size
);
313 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
319 INIT_WORK(&data
->work
, btsdio_work
);
321 skb_queue_head_init(&data
->txq
);
323 hdev
= hci_alloc_dev();
329 hdev
->bus
= HCI_SDIO
;
330 hdev
->driver_data
= data
;
334 SET_HCIDEV_DEV(hdev
, &func
->dev
);
336 hdev
->open
= btsdio_open
;
337 hdev
->close
= btsdio_close
;
338 hdev
->flush
= btsdio_flush
;
339 hdev
->send
= btsdio_send_frame
;
340 hdev
->destruct
= btsdio_destruct
;
342 hdev
->owner
= THIS_MODULE
;
344 err
= hci_register_dev(hdev
);
351 sdio_set_drvdata(func
, data
);
356 static void btsdio_remove(struct sdio_func
*func
)
358 struct btsdio_data
*data
= sdio_get_drvdata(func
);
359 struct hci_dev
*hdev
;
361 BT_DBG("func %p", func
);
368 sdio_set_drvdata(func
, NULL
);
370 hci_unregister_dev(hdev
);
375 static struct sdio_driver btsdio_driver
= {
377 .probe
= btsdio_probe
,
378 .remove
= btsdio_remove
,
379 .id_table
= btsdio_table
,
382 static int __init
btsdio_init(void)
384 BT_INFO("Generic Bluetooth SDIO driver ver %s", VERSION
);
386 return sdio_register_driver(&btsdio_driver
);
389 static void __exit
btsdio_exit(void)
391 sdio_unregister_driver(&btsdio_driver
);
394 module_init(btsdio_init
);
395 module_exit(btsdio_exit
);
397 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
398 MODULE_DESCRIPTION("Generic Bluetooth SDIO driver ver " VERSION
);
399 MODULE_VERSION(VERSION
);
400 MODULE_LICENSE("GPL");