aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/bluetooth/bpa10x.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/bluetooth/bpa10x.c')
-rw-r--r--drivers/bluetooth/bpa10x.c624
1 files changed, 262 insertions, 362 deletions
diff --git a/drivers/bluetooth/bpa10x.c b/drivers/bluetooth/bpa10x.c
index e8ebd5d3de86..1375b5345a0a 100644
--- a/drivers/bluetooth/bpa10x.c
+++ b/drivers/bluetooth/bpa10x.c
@@ -2,7 +2,7 @@
2 * 2 *
3 * Digianswer Bluetooth USB driver 3 * Digianswer Bluetooth USB driver
4 * 4 *
5 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> 5 * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
6 * 6 *
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
@@ -21,13 +21,14 @@
21 * 21 *
22 */ 22 */
23 23
24#include <linux/module.h>
25
26#include <linux/kernel.h> 24#include <linux/kernel.h>
25#include <linux/module.h>
27#include <linux/init.h> 26#include <linux/init.h>
28#include <linux/slab.h> 27#include <linux/slab.h>
29#include <linux/types.h> 28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/errno.h> 30#include <linux/errno.h>
31#include <linux/skbuff.h>
31 32
32#include <linux/usb.h> 33#include <linux/usb.h>
33 34
@@ -39,7 +40,7 @@
39#define BT_DBG(D...) 40#define BT_DBG(D...)
40#endif 41#endif
41 42
42#define VERSION "0.8" 43#define VERSION "0.9"
43 44
44static int ignore = 0; 45static int ignore = 0;
45 46
@@ -52,393 +53,285 @@ static struct usb_device_id bpa10x_table[] = {
52 53
53MODULE_DEVICE_TABLE(usb, bpa10x_table); 54MODULE_DEVICE_TABLE(usb, bpa10x_table);
54 55
55#define BPA10X_CMD_EP 0x00
56#define BPA10X_EVT_EP 0x81
57#define BPA10X_TX_EP 0x02
58#define BPA10X_RX_EP 0x82
59
60#define BPA10X_CMD_BUF_SIZE 252
61#define BPA10X_EVT_BUF_SIZE 16
62#define BPA10X_TX_BUF_SIZE 384
63#define BPA10X_RX_BUF_SIZE 384
64
65struct bpa10x_data { 56struct bpa10x_data {
66 struct hci_dev *hdev; 57 struct hci_dev *hdev;
67 struct usb_device *udev; 58 struct usb_device *udev;
68 59
69 rwlock_t lock; 60 struct usb_anchor tx_anchor;
61 struct usb_anchor rx_anchor;
70 62
71 struct sk_buff_head cmd_queue; 63 struct sk_buff *rx_skb[2];
72 struct urb *cmd_urb;
73 struct urb *evt_urb;
74 struct sk_buff *evt_skb;
75 unsigned int evt_len;
76
77 struct sk_buff_head tx_queue;
78 struct urb *tx_urb;
79 struct urb *rx_urb;
80}; 64};
81 65
82#define HCI_VENDOR_HDR_SIZE 5 66#define HCI_VENDOR_HDR_SIZE 5
83 67
84struct hci_vendor_hdr { 68struct hci_vendor_hdr {
85 __u8 type; 69 __u8 type;
86 __le16 snum; 70 __le16 snum;
87 __le16 dlen; 71 __le16 dlen;
88} __attribute__ ((packed)); 72} __attribute__ ((packed));
89 73
90static void bpa10x_recv_bulk(struct bpa10x_data *data, unsigned char *buf, int count) 74static int bpa10x_recv(struct hci_dev *hdev, int queue, void *buf, int count)
91{ 75{
92 struct hci_acl_hdr *ah; 76 struct bpa10x_data *data = hdev->driver_data;
93 struct hci_sco_hdr *sh; 77
94 struct hci_vendor_hdr *vh; 78 BT_DBG("%s queue %d buffer %p count %d", hdev->name,
95 struct sk_buff *skb; 79 queue, buf, count);
96 int len; 80
81 if (queue < 0 || queue > 1)
82 return -EILSEQ;
83
84 hdev->stat.byte_rx += count;
97 85
98 while (count) { 86 while (count) {
99 switch (*buf++) { 87 struct sk_buff *skb = data->rx_skb[queue];
100 case HCI_ACLDATA_PKT: 88 struct { __u8 type; int expect; } *scb;
101 ah = (struct hci_acl_hdr *) buf; 89 int type, len = 0;
102 len = HCI_ACL_HDR_SIZE + __le16_to_cpu(ah->dlen);
103 skb = bt_skb_alloc(len, GFP_ATOMIC);
104 if (skb) {
105 memcpy(skb_put(skb, len), buf, len);
106 skb->dev = (void *) data->hdev;
107 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
108 hci_recv_frame(skb);
109 }
110 break;
111 90
112 case HCI_SCODATA_PKT: 91 if (!skb) {
113 sh = (struct hci_sco_hdr *) buf; 92 /* Start of the frame */
114 len = HCI_SCO_HDR_SIZE + sh->dlen; 93
115 skb = bt_skb_alloc(len, GFP_ATOMIC); 94 type = *((__u8 *) buf);
116 if (skb) { 95 count--; buf++;
117 memcpy(skb_put(skb, len), buf, len); 96
118 skb->dev = (void *) data->hdev; 97 switch (type) {
119 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 98 case HCI_EVENT_PKT:
120 hci_recv_frame(skb); 99 if (count >= HCI_EVENT_HDR_SIZE) {
100 struct hci_event_hdr *h = buf;
101 len = HCI_EVENT_HDR_SIZE + h->plen;
102 } else
103 return -EILSEQ;
104 break;
105
106 case HCI_ACLDATA_PKT:
107 if (count >= HCI_ACL_HDR_SIZE) {
108 struct hci_acl_hdr *h = buf;
109 len = HCI_ACL_HDR_SIZE +
110 __le16_to_cpu(h->dlen);
111 } else
112 return -EILSEQ;
113 break;
114
115 case HCI_SCODATA_PKT:
116 if (count >= HCI_SCO_HDR_SIZE) {
117 struct hci_sco_hdr *h = buf;
118 len = HCI_SCO_HDR_SIZE + h->dlen;
119 } else
120 return -EILSEQ;
121 break;
122
123 case HCI_VENDOR_PKT:
124 if (count >= HCI_VENDOR_HDR_SIZE) {
125 struct hci_vendor_hdr *h = buf;
126 len = HCI_VENDOR_HDR_SIZE +
127 __le16_to_cpu(h->dlen);
128 } else
129 return -EILSEQ;
130 break;
121 } 131 }
122 break;
123 132
124 case HCI_VENDOR_PKT:
125 vh = (struct hci_vendor_hdr *) buf;
126 len = HCI_VENDOR_HDR_SIZE + __le16_to_cpu(vh->dlen);
127 skb = bt_skb_alloc(len, GFP_ATOMIC); 133 skb = bt_skb_alloc(len, GFP_ATOMIC);
128 if (skb) { 134 if (!skb) {
129 memcpy(skb_put(skb, len), buf, len); 135 BT_ERR("%s no memory for packet", hdev->name);
130 skb->dev = (void *) data->hdev; 136 return -ENOMEM;
131 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
132 hci_recv_frame(skb);
133 } 137 }
134 break;
135
136 default:
137 len = count - 1;
138 break;
139 }
140 138
141 buf += len; 139 skb->dev = (void *) hdev;
142 count -= (len + 1);
143 }
144}
145
146static int bpa10x_recv_event(struct bpa10x_data *data, unsigned char *buf, int size)
147{
148 BT_DBG("data %p buf %p size %d", data, buf, size);
149 140
150 if (data->evt_skb) { 141 data->rx_skb[queue] = skb;
151 struct sk_buff *skb = data->evt_skb;
152 142
153 memcpy(skb_put(skb, size), buf, size); 143 scb = (void *) skb->cb;
144 scb->type = type;
145 scb->expect = len;
146 } else {
147 /* Continuation */
154 148
155 if (skb->len == data->evt_len) { 149 scb = (void *) skb->cb;
156 data->evt_skb = NULL; 150 len = scb->expect;
157 data->evt_len = 0;
158 hci_recv_frame(skb);
159 }
160 } else {
161 struct sk_buff *skb;
162 struct hci_event_hdr *hdr;
163 unsigned char pkt_type;
164 int pkt_len = 0;
165
166 if (size < HCI_EVENT_HDR_SIZE + 1) {
167 BT_ERR("%s event packet block with size %d is too short",
168 data->hdev->name, size);
169 return -EILSEQ;
170 } 151 }
171 152
172 pkt_type = *buf++; 153 len = min(len, count);
173 size--;
174
175 if (pkt_type != HCI_EVENT_PKT) {
176 BT_ERR("%s unexpected event packet start byte 0x%02x",
177 data->hdev->name, pkt_type);
178 return -EPROTO;
179 }
180 154
181 hdr = (struct hci_event_hdr *) buf; 155 memcpy(skb_put(skb, len), buf, len);
182 pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
183 156
184 skb = bt_skb_alloc(pkt_len, GFP_ATOMIC); 157 scb->expect -= len;
185 if (!skb) {
186 BT_ERR("%s no memory for new event packet",
187 data->hdev->name);
188 return -ENOMEM;
189 }
190 158
191 skb->dev = (void *) data->hdev; 159 if (scb->expect == 0) {
192 bt_cb(skb)->pkt_type = pkt_type; 160 /* Complete frame */
193 161
194 memcpy(skb_put(skb, size), buf, size); 162 data->rx_skb[queue] = NULL;
195 163
196 if (pkt_len == size) { 164 bt_cb(skb)->pkt_type = scb->type;
197 hci_recv_frame(skb); 165 hci_recv_frame(skb);
198 } else {
199 data->evt_skb = skb;
200 data->evt_len = pkt_len;
201 } 166 }
167
168 count -= len; buf += len;
202 } 169 }
203 170
204 return 0; 171 return 0;
205} 172}
206 173
207static void bpa10x_wakeup(struct bpa10x_data *data) 174static void bpa10x_tx_complete(struct urb *urb)
208{ 175{
209 struct urb *urb; 176 struct sk_buff *skb = urb->context;
210 struct sk_buff *skb; 177 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
211 int err;
212 178
213 BT_DBG("data %p", data); 179 BT_DBG("%s urb %p status %d count %d", hdev->name,
180 urb, urb->status, urb->actual_length);
214 181
215 urb = data->cmd_urb; 182 if (!test_bit(HCI_RUNNING, &hdev->flags))
216 if (urb->status == -EINPROGRESS) 183 goto done;
217 skb = NULL; 184
185 if (!urb->status)
186 hdev->stat.byte_tx += urb->transfer_buffer_length;
218 else 187 else
219 skb = skb_dequeue(&data->cmd_queue); 188 hdev->stat.err_tx++;
220 189
221 if (skb) { 190done:
222 struct usb_ctrlrequest *cr; 191 kfree(urb->setup_packet);
223 192
224 if (skb->len > BPA10X_CMD_BUF_SIZE) { 193 kfree_skb(skb);
225 BT_ERR("%s command packet with size %d is too big", 194}
226 data->hdev->name, skb->len); 195
227 kfree_skb(skb); 196static void bpa10x_rx_complete(struct urb *urb)
228 return; 197{
229 } 198 struct hci_dev *hdev = urb->context;
199 struct bpa10x_data *data = hdev->driver_data;
200 int err;
230 201
231 cr = (struct usb_ctrlrequest *) urb->setup_packet; 202 BT_DBG("%s urb %p status %d count %d", hdev->name,
232 cr->wLength = __cpu_to_le16(skb->len); 203 urb, urb->status, urb->actual_length);
233 204
234 skb_copy_from_linear_data(skb, urb->transfer_buffer, skb->len); 205 if (!test_bit(HCI_RUNNING, &hdev->flags))
235 urb->transfer_buffer_length = skb->len; 206 return;
236 207
237 err = usb_submit_urb(urb, GFP_ATOMIC); 208 if (urb->status == 0) {
238 if (err < 0 && err != -ENODEV) { 209 if (bpa10x_recv(hdev, usb_pipebulk(urb->pipe),
239 BT_ERR("%s submit failed for command urb %p with error %d", 210 urb->transfer_buffer,
240 data->hdev->name, urb, err); 211 urb->actual_length) < 0) {
241 skb_queue_head(&data->cmd_queue, skb); 212 BT_ERR("%s corrupted event packet", hdev->name);
242 } else 213 hdev->stat.err_rx++;
243 kfree_skb(skb); 214 }
244 } 215 }
245 216
246 urb = data->tx_urb; 217 usb_anchor_urb(urb, &data->rx_anchor);
247 if (urb->status == -EINPROGRESS) 218
248 skb = NULL; 219 err = usb_submit_urb(urb, GFP_ATOMIC);
249 else 220 if (err < 0) {
250 skb = skb_dequeue(&data->tx_queue); 221 BT_ERR("%s urb %p failed to resubmit (%d)",
251 222 hdev->name, urb, -err);
252 if (skb) { 223 usb_unanchor_urb(urb);
253 skb_copy_from_linear_data(skb, urb->transfer_buffer, skb->len);
254 urb->transfer_buffer_length = skb->len;
255
256 err = usb_submit_urb(urb, GFP_ATOMIC);
257 if (err < 0 && err != -ENODEV) {
258 BT_ERR("%s submit failed for command urb %p with error %d",
259 data->hdev->name, urb, err);
260 skb_queue_head(&data->tx_queue, skb);
261 } else
262 kfree_skb(skb);
263 } 224 }
264} 225}
265 226
266static void bpa10x_complete(struct urb *urb) 227static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
267{ 228{
268 struct bpa10x_data *data = urb->context; 229 struct bpa10x_data *data = hdev->driver_data;
269 unsigned char *buf = urb->transfer_buffer; 230 struct urb *urb;
270 int err, count = urb->actual_length; 231 unsigned char *buf;
232 unsigned int pipe;
233 int err, size = 16;
271 234
272 BT_DBG("data %p urb %p buf %p count %d", data, urb, buf, count); 235 BT_DBG("%s", hdev->name);
273 236
274 read_lock(&data->lock); 237 urb = usb_alloc_urb(0, GFP_KERNEL);
238 if (!urb)
239 return -ENOMEM;
275 240
276 if (!test_bit(HCI_RUNNING, &data->hdev->flags)) 241 buf = kmalloc(size, GFP_KERNEL);
277 goto unlock; 242 if (!buf) {
243 usb_free_urb(urb);
244 return -ENOMEM;
245 }
278 246
279 if (urb->status < 0 || !count) 247 pipe = usb_rcvintpipe(data->udev, 0x81);
280 goto resubmit;
281 248
282 if (usb_pipein(urb->pipe)) { 249 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
283 data->hdev->stat.byte_rx += count; 250 bpa10x_rx_complete, hdev, 1);
284 251
285 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) 252 urb->transfer_flags |= URB_FREE_BUFFER;
286 bpa10x_recv_event(data, buf, count);
287 253
288 if (usb_pipetype(urb->pipe) == PIPE_BULK) 254 usb_anchor_urb(urb, &data->rx_anchor);
289 bpa10x_recv_bulk(data, buf, count);
290 } else {
291 data->hdev->stat.byte_tx += count;
292 255
293 bpa10x_wakeup(data); 256 err = usb_submit_urb(urb, GFP_KERNEL);
257 if (err < 0) {
258 BT_ERR("%s urb %p submission failed (%d)",
259 hdev->name, urb, -err);
260 usb_unanchor_urb(urb);
261 kfree(buf);
294 } 262 }
295 263
296resubmit: 264 usb_free_urb(urb);
297 if (usb_pipein(urb->pipe)) {
298 err = usb_submit_urb(urb, GFP_ATOMIC);
299 if (err < 0 && err != -ENODEV) {
300 BT_ERR("%s urb %p type %d resubmit status %d",
301 data->hdev->name, urb, usb_pipetype(urb->pipe), err);
302 }
303 }
304 265
305unlock: 266 return err;
306 read_unlock(&data->lock);
307} 267}
308 268
309static inline struct urb *bpa10x_alloc_urb(struct usb_device *udev, unsigned int pipe, 269static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
310 size_t size, gfp_t flags, void *data)
311{ 270{
271 struct bpa10x_data *data = hdev->driver_data;
312 struct urb *urb; 272 struct urb *urb;
313 struct usb_ctrlrequest *cr;
314 unsigned char *buf; 273 unsigned char *buf;
274 unsigned int pipe;
275 int err, size = 64;
315 276
316 BT_DBG("udev %p data %p", udev, data); 277 BT_DBG("%s", hdev->name);
317 278
318 urb = usb_alloc_urb(0, flags); 279 urb = usb_alloc_urb(0, GFP_KERNEL);
319 if (!urb) 280 if (!urb)
320 return NULL; 281 return -ENOMEM;
321 282
322 buf = kmalloc(size, flags); 283 buf = kmalloc(size, GFP_KERNEL);
323 if (!buf) { 284 if (!buf) {
324 usb_free_urb(urb); 285 usb_free_urb(urb);
325 return NULL; 286 return -ENOMEM;
326 } 287 }
327 288
328 switch (usb_pipetype(pipe)) { 289 pipe = usb_rcvbulkpipe(data->udev, 0x82);
329 case PIPE_CONTROL:
330 cr = kmalloc(sizeof(*cr), flags);
331 if (!cr) {
332 kfree(buf);
333 usb_free_urb(urb);
334 return NULL;
335 }
336 290
337 cr->bRequestType = USB_TYPE_VENDOR; 291 usb_fill_bulk_urb(urb, data->udev, pipe,
338 cr->bRequest = 0; 292 buf, size, bpa10x_rx_complete, hdev);
339 cr->wIndex = 0;
340 cr->wValue = 0;
341 cr->wLength = __cpu_to_le16(0);
342 293
343 usb_fill_control_urb(urb, udev, pipe, (void *) cr, buf, 0, bpa10x_complete, data); 294 urb->transfer_flags |= URB_FREE_BUFFER;
344 break;
345 295
346 case PIPE_INTERRUPT: 296 usb_anchor_urb(urb, &data->rx_anchor);
347 usb_fill_int_urb(urb, udev, pipe, buf, size, bpa10x_complete, data, 1);
348 break;
349 297
350 case PIPE_BULK: 298 err = usb_submit_urb(urb, GFP_KERNEL);
351 usb_fill_bulk_urb(urb, udev, pipe, buf, size, bpa10x_complete, data); 299 if (err < 0) {
352 break; 300 BT_ERR("%s urb %p submission failed (%d)",
353 301 hdev->name, urb, -err);
354 default: 302 usb_unanchor_urb(urb);
355 kfree(buf); 303 kfree(buf);
356 usb_free_urb(urb);
357 return NULL;
358 } 304 }
359 305
360 return urb;
361}
362
363static inline void bpa10x_free_urb(struct urb *urb)
364{
365 BT_DBG("urb %p", urb);
366
367 if (!urb)
368 return;
369
370 kfree(urb->setup_packet);
371 kfree(urb->transfer_buffer);
372
373 usb_free_urb(urb); 306 usb_free_urb(urb);
307
308 return err;
374} 309}
375 310
376static int bpa10x_open(struct hci_dev *hdev) 311static int bpa10x_open(struct hci_dev *hdev)
377{ 312{
378 struct bpa10x_data *data = hdev->driver_data; 313 struct bpa10x_data *data = hdev->driver_data;
379 struct usb_device *udev = data->udev;
380 unsigned long flags;
381 int err; 314 int err;
382 315
383 BT_DBG("hdev %p data %p", hdev, data); 316 BT_DBG("%s", hdev->name);
384 317
385 if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) 318 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
386 return 0; 319 return 0;
387 320
388 data->cmd_urb = bpa10x_alloc_urb(udev, usb_sndctrlpipe(udev, BPA10X_CMD_EP), 321 err = bpa10x_submit_intr_urb(hdev);
389 BPA10X_CMD_BUF_SIZE, GFP_KERNEL, data); 322 if (err < 0)
390 if (!data->cmd_urb) { 323 goto error;
391 err = -ENOMEM;
392 goto done;
393 }
394
395 data->evt_urb = bpa10x_alloc_urb(udev, usb_rcvintpipe(udev, BPA10X_EVT_EP),
396 BPA10X_EVT_BUF_SIZE, GFP_KERNEL, data);
397 if (!data->evt_urb) {
398 bpa10x_free_urb(data->cmd_urb);
399 err = -ENOMEM;
400 goto done;
401 }
402
403 data->rx_urb = bpa10x_alloc_urb(udev, usb_rcvbulkpipe(udev, BPA10X_RX_EP),
404 BPA10X_RX_BUF_SIZE, GFP_KERNEL, data);
405 if (!data->rx_urb) {
406 bpa10x_free_urb(data->evt_urb);
407 bpa10x_free_urb(data->cmd_urb);
408 err = -ENOMEM;
409 goto done;
410 }
411
412 data->tx_urb = bpa10x_alloc_urb(udev, usb_sndbulkpipe(udev, BPA10X_TX_EP),
413 BPA10X_TX_BUF_SIZE, GFP_KERNEL, data);
414 if (!data->rx_urb) {
415 bpa10x_free_urb(data->rx_urb);
416 bpa10x_free_urb(data->evt_urb);
417 bpa10x_free_urb(data->cmd_urb);
418 err = -ENOMEM;
419 goto done;
420 }
421 324
422 write_lock_irqsave(&data->lock, flags); 325 err = bpa10x_submit_bulk_urb(hdev);
326 if (err < 0)
327 goto error;
423 328
424 err = usb_submit_urb(data->evt_urb, GFP_ATOMIC); 329 return 0;
425 if (err < 0) {
426 BT_ERR("%s submit failed for event urb %p with error %d",
427 data->hdev->name, data->evt_urb, err);
428 } else {
429 err = usb_submit_urb(data->rx_urb, GFP_ATOMIC);
430 if (err < 0) {
431 BT_ERR("%s submit failed for rx urb %p with error %d",
432 data->hdev->name, data->evt_urb, err);
433 usb_kill_urb(data->evt_urb);
434 }
435 }
436 330
437 write_unlock_irqrestore(&data->lock, flags); 331error:
332 usb_kill_anchored_urbs(&data->rx_anchor);
438 333
439done: 334 clear_bit(HCI_RUNNING, &hdev->flags);
440 if (err < 0)
441 clear_bit(HCI_RUNNING, &hdev->flags);
442 335
443 return err; 336 return err;
444} 337}
@@ -446,27 +339,13 @@ done:
446static int bpa10x_close(struct hci_dev *hdev) 339static int bpa10x_close(struct hci_dev *hdev)
447{ 340{
448 struct bpa10x_data *data = hdev->driver_data; 341 struct bpa10x_data *data = hdev->driver_data;
449 unsigned long flags;
450 342
451 BT_DBG("hdev %p data %p", hdev, data); 343 BT_DBG("%s", hdev->name);
452 344
453 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) 345 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
454 return 0; 346 return 0;
455 347
456 write_lock_irqsave(&data->lock, flags); 348 usb_kill_anchored_urbs(&data->rx_anchor);
457
458 skb_queue_purge(&data->cmd_queue);
459 usb_kill_urb(data->cmd_urb);
460 usb_kill_urb(data->evt_urb);
461 usb_kill_urb(data->rx_urb);
462 usb_kill_urb(data->tx_urb);
463
464 write_unlock_irqrestore(&data->lock, flags);
465
466 bpa10x_free_urb(data->cmd_urb);
467 bpa10x_free_urb(data->evt_urb);
468 bpa10x_free_urb(data->rx_urb);
469 bpa10x_free_urb(data->tx_urb);
470 349
471 return 0; 350 return 0;
472} 351}
@@ -475,9 +354,9 @@ static int bpa10x_flush(struct hci_dev *hdev)
475{ 354{
476 struct bpa10x_data *data = hdev->driver_data; 355 struct bpa10x_data *data = hdev->driver_data;
477 356
478 BT_DBG("hdev %p data %p", hdev, data); 357 BT_DBG("%s", hdev->name);
479 358
480 skb_queue_purge(&data->cmd_queue); 359 usb_kill_anchored_urbs(&data->tx_anchor);
481 360
482 return 0; 361 return 0;
483} 362}
@@ -485,45 +364,78 @@ static int bpa10x_flush(struct hci_dev *hdev)
485static int bpa10x_send_frame(struct sk_buff *skb) 364static int bpa10x_send_frame(struct sk_buff *skb)
486{ 365{
487 struct hci_dev *hdev = (struct hci_dev *) skb->dev; 366 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
488 struct bpa10x_data *data; 367 struct bpa10x_data *data = hdev->driver_data;
489 368 struct usb_ctrlrequest *dr;
490 BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, bt_cb(skb)->pkt_type, skb->len); 369 struct urb *urb;
370 unsigned int pipe;
371 int err;
491 372
492 if (!hdev) { 373 BT_DBG("%s", hdev->name);
493 BT_ERR("Frame for unknown HCI device");
494 return -ENODEV;
495 }
496 374
497 if (!test_bit(HCI_RUNNING, &hdev->flags)) 375 if (!test_bit(HCI_RUNNING, &hdev->flags))
498 return -EBUSY; 376 return -EBUSY;
499 377
500 data = hdev->driver_data; 378 urb = usb_alloc_urb(0, GFP_ATOMIC);
379 if (!urb)
380 return -ENOMEM;
501 381
502 /* Prepend skb with frame type */ 382 /* Prepend skb with frame type */
503 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); 383 *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
504 384
505 switch (bt_cb(skb)->pkt_type) { 385 switch (bt_cb(skb)->pkt_type) {
506 case HCI_COMMAND_PKT: 386 case HCI_COMMAND_PKT:
387 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
388 if (!dr) {
389 usb_free_urb(urb);
390 return -ENOMEM;
391 }
392
393 dr->bRequestType = USB_TYPE_VENDOR;
394 dr->bRequest = 0;
395 dr->wIndex = 0;
396 dr->wValue = 0;
397 dr->wLength = __cpu_to_le16(skb->len);
398
399 pipe = usb_sndctrlpipe(data->udev, 0x00);
400
401 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
402 skb->data, skb->len, bpa10x_tx_complete, skb);
403
507 hdev->stat.cmd_tx++; 404 hdev->stat.cmd_tx++;
508 skb_queue_tail(&data->cmd_queue, skb);
509 break; 405 break;
510 406
511 case HCI_ACLDATA_PKT: 407 case HCI_ACLDATA_PKT:
408 pipe = usb_sndbulkpipe(data->udev, 0x02);
409
410 usb_fill_bulk_urb(urb, data->udev, pipe,
411 skb->data, skb->len, bpa10x_tx_complete, skb);
412
512 hdev->stat.acl_tx++; 413 hdev->stat.acl_tx++;
513 skb_queue_tail(&data->tx_queue, skb);
514 break; 414 break;
515 415
516 case HCI_SCODATA_PKT: 416 case HCI_SCODATA_PKT:
417 pipe = usb_sndbulkpipe(data->udev, 0x02);
418
419 usb_fill_bulk_urb(urb, data->udev, pipe,
420 skb->data, skb->len, bpa10x_tx_complete, skb);
421
517 hdev->stat.sco_tx++; 422 hdev->stat.sco_tx++;
518 skb_queue_tail(&data->tx_queue, skb);
519 break; 423 break;
520 };
521 424
522 read_lock(&data->lock); 425 default:
426 return -EILSEQ;
427 }
428
429 usb_anchor_urb(urb, &data->tx_anchor);
523 430
524 bpa10x_wakeup(data); 431 err = usb_submit_urb(urb, GFP_ATOMIC);
432 if (err < 0) {
433 BT_ERR("%s urb %p submission failed", hdev->name, urb);
434 kfree(urb->setup_packet);
435 usb_unanchor_urb(urb);
436 }
525 437
526 read_unlock(&data->lock); 438 usb_free_urb(urb);
527 439
528 return 0; 440 return 0;
529} 441}
@@ -532,16 +444,17 @@ static void bpa10x_destruct(struct hci_dev *hdev)
532{ 444{
533 struct bpa10x_data *data = hdev->driver_data; 445 struct bpa10x_data *data = hdev->driver_data;
534 446
535 BT_DBG("hdev %p data %p", hdev, data); 447 BT_DBG("%s", hdev->name);
536 448
449 kfree(data->rx_skb[0]);
450 kfree(data->rx_skb[1]);
537 kfree(data); 451 kfree(data);
538} 452}
539 453
540static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id) 454static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
541{ 455{
542 struct usb_device *udev = interface_to_usbdev(intf);
543 struct hci_dev *hdev;
544 struct bpa10x_data *data; 456 struct bpa10x_data *data;
457 struct hci_dev *hdev;
545 int err; 458 int err;
546 459
547 BT_DBG("intf %p id %p", intf, id); 460 BT_DBG("intf %p id %p", intf, id);
@@ -549,48 +462,43 @@ static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *
549 if (ignore) 462 if (ignore)
550 return -ENODEV; 463 return -ENODEV;
551 464
552 if (intf->cur_altsetting->desc.bInterfaceNumber > 0) 465 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
553 return -ENODEV; 466 return -ENODEV;
554 467
555 data = kzalloc(sizeof(*data), GFP_KERNEL); 468 data = kzalloc(sizeof(*data), GFP_KERNEL);
556 if (!data) { 469 if (!data)
557 BT_ERR("Can't allocate data structure");
558 return -ENOMEM; 470 return -ENOMEM;
559 }
560
561 data->udev = udev;
562 471
563 rwlock_init(&data->lock); 472 data->udev = interface_to_usbdev(intf);
564 473
565 skb_queue_head_init(&data->cmd_queue); 474 init_usb_anchor(&data->tx_anchor);
566 skb_queue_head_init(&data->tx_queue); 475 init_usb_anchor(&data->rx_anchor);
567 476
568 hdev = hci_alloc_dev(); 477 hdev = hci_alloc_dev();
569 if (!hdev) { 478 if (!hdev) {
570 BT_ERR("Can't allocate HCI device");
571 kfree(data); 479 kfree(data);
572 return -ENOMEM; 480 return -ENOMEM;
573 } 481 }
574 482
575 data->hdev = hdev;
576
577 hdev->type = HCI_USB; 483 hdev->type = HCI_USB;
578 hdev->driver_data = data; 484 hdev->driver_data = data;
485
486 data->hdev = hdev;
487
579 SET_HCIDEV_DEV(hdev, &intf->dev); 488 SET_HCIDEV_DEV(hdev, &intf->dev);
580 489
581 hdev->open = bpa10x_open; 490 hdev->open = bpa10x_open;
582 hdev->close = bpa10x_close; 491 hdev->close = bpa10x_close;
583 hdev->flush = bpa10x_flush; 492 hdev->flush = bpa10x_flush;
584 hdev->send = bpa10x_send_frame; 493 hdev->send = bpa10x_send_frame;
585 hdev->destruct = bpa10x_destruct; 494 hdev->destruct = bpa10x_destruct;
586 495
587 hdev->owner = THIS_MODULE; 496 hdev->owner = THIS_MODULE;
588 497
589 err = hci_register_dev(hdev); 498 err = hci_register_dev(hdev);
590 if (err < 0) { 499 if (err < 0) {
591 BT_ERR("Can't register HCI device");
592 kfree(data);
593 hci_free_dev(hdev); 500 hci_free_dev(hdev);
501 kfree(data);
594 return err; 502 return err;
595 } 503 }
596 504
@@ -602,19 +510,17 @@ static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *
602static void bpa10x_disconnect(struct usb_interface *intf) 510static void bpa10x_disconnect(struct usb_interface *intf)
603{ 511{
604 struct bpa10x_data *data = usb_get_intfdata(intf); 512 struct bpa10x_data *data = usb_get_intfdata(intf);
605 struct hci_dev *hdev = data->hdev;
606 513
607 BT_DBG("intf %p", intf); 514 BT_DBG("intf %p", intf);
608 515
609 if (!hdev) 516 if (!data)
610 return; 517 return;
611 518
612 usb_set_intfdata(intf, NULL); 519 usb_set_intfdata(intf, NULL);
613 520
614 if (hci_unregister_dev(hdev) < 0) 521 hci_unregister_dev(data->hdev);
615 BT_ERR("Can't unregister HCI device %s", hdev->name);
616 522
617 hci_free_dev(hdev); 523 hci_free_dev(data->hdev);
618} 524}
619 525
620static struct usb_driver bpa10x_driver = { 526static struct usb_driver bpa10x_driver = {
@@ -626,15 +532,9 @@ static struct usb_driver bpa10x_driver = {
626 532
627static int __init bpa10x_init(void) 533static int __init bpa10x_init(void)
628{ 534{
629 int err;
630
631 BT_INFO("Digianswer Bluetooth USB driver ver %s", VERSION); 535 BT_INFO("Digianswer Bluetooth USB driver ver %s", VERSION);
632 536
633 err = usb_register(&bpa10x_driver); 537 return usb_register(&bpa10x_driver);
634 if (err < 0)
635 BT_ERR("Failed to register USB driver");
636
637 return err;
638} 538}
639 539
640static void __exit bpa10x_exit(void) 540static void __exit bpa10x_exit(void)