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