Blame view

drivers/bluetooth/btsdio.c 8.06 KB
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  /*
   *
   *  Generic Bluetooth SDIO driver
   *
   *  Copyright (C) 2007  Cambridge Silicon Radio Ltd.
   *  Copyright (C) 2007  Marcel Holtmann <marcel@holtmann.org>
   *
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/types.h>
  #include <linux/sched.h>
  #include <linux/errno.h>
  #include <linux/skbuff.h>
  
  #include <linux/mmc/sdio_ids.h>
  #include <linux/mmc/sdio_func.h>
  
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
39
40
41
42
43
44
45
46
  #define VERSION "0.1"
  
  static const struct sdio_device_id btsdio_table[] = {
  	/* Generic Bluetooth Type-A SDIO device */
  	{ SDIO_DEVICE_CLASS(SDIO_CLASS_BT_A) },
  
  	/* Generic Bluetooth Type-B SDIO device */
  	{ SDIO_DEVICE_CLASS(SDIO_CLASS_BT_B) },
fb3d8eb47   David Vrabel   Bluetooth: Suppor...
47
48
  	/* Generic Bluetooth AMP controller */
  	{ SDIO_DEVICE_CLASS(SDIO_CLASS_BT_AMP) },
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  	{ }	/* Terminating entry */
  };
  
  MODULE_DEVICE_TABLE(sdio, btsdio_table);
  
  struct btsdio_data {
  	struct hci_dev   *hdev;
  	struct sdio_func *func;
  
  	struct work_struct work;
  
  	struct sk_buff_head txq;
  };
  
  #define REG_RDAT     0x00	/* Receiver Data */
  #define REG_TDAT     0x00	/* Transmitter Data */
  #define REG_PC_RRT   0x10	/* Read Packet Control */
  #define REG_PC_WRT   0x11	/* Write Packet Control */
  #define REG_RTC_STAT 0x12	/* Retry Control Status */
  #define REG_RTC_SET  0x12	/* Retry Control Set */
  #define REG_INTRD    0x13	/* Interrupt Indication */
  #define REG_CL_INTRD 0x13	/* Interrupt Clear */
  #define REG_EN_INTRD 0x14	/* Interrupt Enable */
  #define REG_MD_STAT  0x20	/* Bluetooth Mode Status */
bc7219709   Marcel Holtmann   Bluetooth: Use MD...
73
  #define REG_MD_SET   0x20	/* Bluetooth Mode Set */
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
74
75
76
77
78
79
80
81
82
83
84
85
  
  static int btsdio_tx_packet(struct btsdio_data *data, struct sk_buff *skb)
  {
  	int err;
  
  	BT_DBG("%s", data->hdev->name);
  
  	/* Prepend Type-A header */
  	skb_push(skb, 4);
  	skb->data[0] = (skb->len & 0x0000ff);
  	skb->data[1] = (skb->len & 0x00ff00) >> 8;
  	skb->data[2] = (skb->len & 0xff0000) >> 16;
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
86
  	skb->data[3] = hci_skb_pkt_type(skb);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
87
88
89
  
  	err = sdio_writesb(data->func, REG_TDAT, skb->data, skb->len);
  	if (err < 0) {
7644d63d1   Tomas Winkler   Bluetooth: Fix TX...
90
  		skb_pull(skb, 4);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  		sdio_writeb(data->func, 0x01, REG_PC_WRT, NULL);
  		return err;
  	}
  
  	data->hdev->stat.byte_tx += skb->len;
  
  	kfree_skb(skb);
  
  	return 0;
  }
  
  static void btsdio_work(struct work_struct *work)
  {
  	struct btsdio_data *data = container_of(work, struct btsdio_data, work);
  	struct sk_buff *skb;
  	int err;
  
  	BT_DBG("%s", data->hdev->name);
  
  	sdio_claim_host(data->func);
  
  	while ((skb = skb_dequeue(&data->txq))) {
  		err = btsdio_tx_packet(data, skb);
  		if (err < 0) {
  			data->hdev->stat.err_tx++;
  			skb_queue_head(&data->txq, skb);
  			break;
  		}
  	}
  
  	sdio_release_host(data->func);
  }
  
  static int btsdio_rx_packet(struct btsdio_data *data)
  {
  	u8 hdr[4] __attribute__ ((aligned(4)));
  	struct sk_buff *skb;
  	int err, len;
  
  	BT_DBG("%s", data->hdev->name);
  
  	err = sdio_readsb(data->func, hdr, REG_RDAT, 4);
  	if (err < 0)
  		return err;
  
  	len = hdr[0] | (hdr[1] << 8) | (hdr[2] << 16);
  	if (len < 4 || len > 65543)
  		return -EILSEQ;
  
  	skb = bt_skb_alloc(len - 4, GFP_KERNEL);
  	if (!skb) {
  		/* Out of memory. Prepare a read retry and just
  		 * return with the expectation that the next time
  		 * we're called we'll have more memory. */
  		return -ENOMEM;
  	}
  
  	skb_put(skb, len - 4);
  
  	err = sdio_readsb(data->func, skb->data, REG_RDAT, len - 4);
  	if (err < 0) {
cbfd24a75   Sergio Luis   btsdio: free sk_b...
152
  		kfree_skb(skb);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
153
154
155
156
  		return err;
  	}
  
  	data->hdev->stat.byte_rx += len;
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
157
  	hci_skb_pkt_type(skb) = hdr[3];
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
158

e1a261706   Marcel Holtmann   Bluetooth: Provid...
159
  	err = hci_recv_frame(data->hdev, skb);
2fa993423   Adrian Bunk   drivers/bluetooth...
160
  	if (err < 0)
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
161
  		return err;
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  
  	sdio_writeb(data->func, 0x00, REG_PC_RRT, NULL);
  
  	return 0;
  }
  
  static void btsdio_interrupt(struct sdio_func *func)
  {
  	struct btsdio_data *data = sdio_get_drvdata(func);
  	int intrd;
  
  	BT_DBG("%s", data->hdev->name);
  
  	intrd = sdio_readb(func, REG_INTRD, NULL);
  	if (intrd & 0x01) {
  		sdio_writeb(func, 0x01, REG_CL_INTRD, NULL);
  
  		if (btsdio_rx_packet(data) < 0) {
  			data->hdev->stat.err_rx++;
  			sdio_writeb(data->func, 0x01, REG_PC_RRT, NULL);
  		}
  	}
  }
  
  static int btsdio_open(struct hci_dev *hdev)
  {
155961e80   David Herrmann   Bluetooth: Remove...
188
  	struct btsdio_data *data = hci_get_drvdata(hdev);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
189
190
191
  	int err;
  
  	BT_DBG("%s", hdev->name);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
192
193
194
  	sdio_claim_host(data->func);
  
  	err = sdio_enable_func(data->func);
e9ca8bf15   Marcel Holtmann   Bluetooth: Move h...
195
  	if (err < 0)
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
196
  		goto release;
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
197
198
199
200
  
  	err = sdio_claim_irq(data->func, btsdio_interrupt);
  	if (err < 0) {
  		sdio_disable_func(data->func);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
201
202
203
204
  		goto release;
  	}
  
  	if (data->func->class == SDIO_CLASS_BT_B)
bc7219709   Marcel Holtmann   Bluetooth: Use MD...
205
  		sdio_writeb(data->func, 0x00, REG_MD_SET, NULL);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
206
207
208
209
210
211
212
213
214
215
216
  
  	sdio_writeb(data->func, 0x01, REG_EN_INTRD, NULL);
  
  release:
  	sdio_release_host(data->func);
  
  	return err;
  }
  
  static int btsdio_close(struct hci_dev *hdev)
  {
155961e80   David Herrmann   Bluetooth: Remove...
217
  	struct btsdio_data *data = hci_get_drvdata(hdev);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
218
219
  
  	BT_DBG("%s", hdev->name);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  	sdio_claim_host(data->func);
  
  	sdio_writeb(data->func, 0x00, REG_EN_INTRD, NULL);
  
  	sdio_release_irq(data->func);
  	sdio_disable_func(data->func);
  
  	sdio_release_host(data->func);
  
  	return 0;
  }
  
  static int btsdio_flush(struct hci_dev *hdev)
  {
155961e80   David Herrmann   Bluetooth: Remove...
234
  	struct btsdio_data *data = hci_get_drvdata(hdev);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
235
236
237
238
239
240
241
  
  	BT_DBG("%s", hdev->name);
  
  	skb_queue_purge(&data->txq);
  
  	return 0;
  }
7bd8f09f6   Marcel Holtmann   Bluetooth: Add hd...
242
  static int btsdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
243
  {
155961e80   David Herrmann   Bluetooth: Remove...
244
  	struct btsdio_data *data = hci_get_drvdata(hdev);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
245
246
  
  	BT_DBG("%s", hdev->name);
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
247
  	switch (hci_skb_pkt_type(skb)) {
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  	case HCI_COMMAND_PKT:
  		hdev->stat.cmd_tx++;
  		break;
  
  	case HCI_ACLDATA_PKT:
  		hdev->stat.acl_tx++;
  		break;
  
  	case HCI_SCODATA_PKT:
  		hdev->stat.sco_tx++;
  		break;
  
  	default:
  		return -EILSEQ;
  	}
  
  	skb_queue_tail(&data->txq, skb);
  
  	schedule_work(&data->work);
  
  	return 0;
  }
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  static int btsdio_probe(struct sdio_func *func,
  				const struct sdio_device_id *id)
  {
  	struct btsdio_data *data;
  	struct hci_dev *hdev;
  	struct sdio_func_tuple *tuple = func->tuples;
  	int err;
  
  	BT_DBG("func %p id %p class 0x%04x", func, id, func->class);
  
  	while (tuple) {
  		BT_DBG("code 0x%x size %d", tuple->code, tuple->size);
  		tuple = tuple->next;
  	}
3a382772b   Sachin Kamat   Bluetooth: Use de...
284
  	data = devm_kzalloc(&func->dev, sizeof(*data), GFP_KERNEL);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
285
286
287
288
289
290
291
292
293
294
  	if (!data)
  		return -ENOMEM;
  
  	data->func = func;
  
  	INIT_WORK(&data->work, btsdio_work);
  
  	skb_queue_head_init(&data->txq);
  
  	hdev = hci_alloc_dev();
3a382772b   Sachin Kamat   Bluetooth: Use de...
295
  	if (!hdev)
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
296
  		return -ENOMEM;
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
297

c13854cef   Marcel Holtmann   Bluetooth: Conver...
298
  	hdev->bus = HCI_SDIO;
155961e80   David Herrmann   Bluetooth: Remove...
299
  	hci_set_drvdata(hdev, data);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
300

fb3d8eb47   David Vrabel   Bluetooth: Suppor...
301
302
303
304
  	if (id->class == SDIO_CLASS_BT_AMP)
  		hdev->dev_type = HCI_AMP;
  	else
  		hdev->dev_type = HCI_BREDR;
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
305
306
307
308
309
310
311
312
  	data->hdev = hdev;
  
  	SET_HCIDEV_DEV(hdev, &func->dev);
  
  	hdev->open     = btsdio_open;
  	hdev->close    = btsdio_close;
  	hdev->flush    = btsdio_flush;
  	hdev->send     = btsdio_send_frame;
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
313

de8b58289   Marcel Holtmann   Bluetooth: Set HC...
314
315
  	if (func->vendor == 0x0104 && func->device == 0x00c5)
  		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
316
317
318
  	err = hci_register_dev(hdev);
  	if (err < 0) {
  		hci_free_dev(hdev);
ddbaf13e3   Marcel Holtmann   [Bluetooth] Add g...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  		return err;
  	}
  
  	sdio_set_drvdata(func, data);
  
  	return 0;
  }
  
  static void btsdio_remove(struct sdio_func *func)
  {
  	struct btsdio_data *data = sdio_get_drvdata(func);
  	struct hci_dev *hdev;
  
  	BT_DBG("func %p", func);
  
  	if (!data)
  		return;
  
  	hdev = data->hdev;
  
  	sdio_set_drvdata(func, NULL);
  
  	hci_unregister_dev(hdev);
  
  	hci_free_dev(hdev);
  }
  
  static struct sdio_driver btsdio_driver = {
  	.name		= "btsdio",
  	.probe		= btsdio_probe,
  	.remove		= btsdio_remove,
  	.id_table	= btsdio_table,
  };
  
  static int __init btsdio_init(void)
  {
  	BT_INFO("Generic Bluetooth SDIO driver ver %s", VERSION);
  
  	return sdio_register_driver(&btsdio_driver);
  }
  
  static void __exit btsdio_exit(void)
  {
  	sdio_unregister_driver(&btsdio_driver);
  }
  
  module_init(btsdio_init);
  module_exit(btsdio_exit);
  
  MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  MODULE_DESCRIPTION("Generic Bluetooth SDIO driver ver " VERSION);
  MODULE_VERSION(VERSION);
  MODULE_LICENSE("GPL");