OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [bluetooth/] [hci_usb.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
   HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
3
   Copyright (C) 2000-2001 Qualcomm Incorporated
4
   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
5
 
6
   Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License version 2 as
10
   published by the Free Software Foundation;
11
 
12
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 
21
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23
   SOFTWARE IS DISCLAIMED.
24
*/
25
 
26
/*
27
 * Based on original USB Bluetooth driver for Linux kernel
28
 *    Copyright (c) 2000 Greg Kroah-Hartman        <greg@kroah.com>
29
 *    Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
30
 *
31
 * $Id: hci_usb.c,v 1.1.1.1 2004-04-15 02:29:46 phoenix Exp $
32
 */
33
#define VERSION "2.5"
34
 
35
#include <linux/config.h>
36
#include <linux/module.h>
37
 
38
#include <linux/version.h>
39
#include <linux/kernel.h>
40
#include <linux/init.h>
41
#include <linux/sched.h>
42
#include <linux/unistd.h>
43
#include <linux/types.h>
44
#include <linux/interrupt.h>
45
 
46
#include <linux/slab.h>
47
#include <linux/errno.h>
48
#include <linux/string.h>
49
#include <linux/skbuff.h>
50
 
51
#include <linux/usb.h>
52
 
53
#include <net/bluetooth/bluetooth.h>
54
#include <net/bluetooth/hci_core.h>
55
 
56
#include "hci_usb.h"
57
 
58
#ifndef HCI_USB_DEBUG
59
#undef  BT_DBG
60
#define BT_DBG( A... )
61
#undef  BT_DMP
62
#define BT_DMP( A... )
63
#endif
64
 
65
#ifndef CONFIG_BLUEZ_HCIUSB_ZERO_PACKET
66
#undef  USB_ZERO_PACKET
67
#define USB_ZERO_PACKET 0
68
#endif
69
 
70
static struct usb_driver hci_usb_driver;
71
 
72
static struct usb_device_id bluetooth_ids[] = {
73
        /* Generic Bluetooth USB device */
74
        { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
75
 
76
        /* AVM BlueFRITZ! USB v2.0 */
77
        { USB_DEVICE(0x057c, 0x3800) },
78
 
79
        /* Ericsson with non-standard id */
80
        { USB_DEVICE(0x0bdb, 0x1002) },
81
 
82
        /* ALPS Module with non-standard id */
83
        { USB_DEVICE(0x044e, 0x3002) },
84
 
85
        /* Bluetooth Ultraport Module from IBM */
86
        { USB_DEVICE(0x04bf, 0x030a) },
87
 
88
        { }     /* Terminating entry */
89
};
90
 
91
MODULE_DEVICE_TABLE (usb, bluetooth_ids);
92
 
93
static struct usb_device_id blacklist_ids[] = {
94
        /* Broadcom BCM2033 without firmware */
95
        { USB_DEVICE(0x0a5c, 0x2033), driver_info: HCI_IGNORE },
96
 
97
        /* Broadcom BCM2035 */
98
        { USB_DEVICE(0x0a5c, 0x200a), driver_info: HCI_RESET },
99
 
100
        /* Digianswer device */
101
        { USB_DEVICE(0x08fd, 0x0001), driver_info: HCI_DIGIANSWER },
102
 
103
        { }     /* Terminating entry */
104
};
105
 
106
struct _urb *_urb_alloc(int isoc, int gfp)
107
{
108
        struct _urb *_urb = kmalloc(sizeof(struct _urb) +
109
                                sizeof(struct iso_packet_descriptor) * isoc, gfp);
110
        if (_urb) {
111
                memset(_urb, 0, sizeof(*_urb));
112
                spin_lock_init(&_urb->urb.lock);
113
        }
114
        return _urb;
115
}
116
 
117
struct _urb *_urb_dequeue(struct _urb_queue *q)
118
{
119
        struct _urb *_urb = NULL;
120
        unsigned long flags;
121
        spin_lock_irqsave(&q->lock, flags);
122
        {
123
                struct list_head *head = &q->head;
124
                struct list_head *next = head->next;
125
                if (next != head) {
126
                        _urb = list_entry(next, struct _urb, list);
127
                        list_del(next); _urb->queue = NULL;
128
                }
129
        }
130
        spin_unlock_irqrestore(&q->lock, flags);
131
        return _urb;
132
}
133
 
134
static void hci_usb_rx_complete(struct urb *urb);
135
static void hci_usb_tx_complete(struct urb *urb);
136
 
137
#define __pending_tx(husb, type)  (&husb->pending_tx[type-1])
138
#define __pending_q(husb, type)   (&husb->pending_q[type-1])
139
#define __completed_q(husb, type) (&husb->completed_q[type-1])
140
#define __transmit_q(husb, type)  (&husb->transmit_q[type-1])
141
#define __reassembly(husb, type)  (husb->reassembly[type-1])
142
 
143
static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
144
{
145
        return _urb_dequeue(__completed_q(husb, type));
146
}
147
 
148
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
149
static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
150
{
151
        int offset = 0, i;
152
 
153
        BT_DBG("len %d mtu %d", len, mtu);
154
 
155
        for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
156
                urb->iso_frame_desc[i].offset = offset;
157
                urb->iso_frame_desc[i].length = mtu;
158
                BT_DBG("desc %d offset %d len %d", i, offset, mtu);
159
        }
160
        if (len && i < HCI_MAX_ISOC_FRAMES) {
161
                urb->iso_frame_desc[i].offset = offset;
162
                urb->iso_frame_desc[i].length = len;
163
                BT_DBG("desc %d offset %d len %d", i, offset, len);
164
                i++;
165
        }
166
        urb->number_of_packets = i;
167
}
168
#endif
169
 
170
static int hci_usb_intr_rx_submit(struct hci_usb *husb)
171
{
172
        struct _urb *_urb;
173
        struct urb *urb;
174
        int err, pipe, interval, size;
175
        void *buf;
176
 
177
        BT_DBG("%s", husb->hdev.name);
178
 
179
        size = husb->intr_in_ep->wMaxPacketSize;
180
 
181
        buf = kmalloc(size, GFP_ATOMIC);
182
        if (!buf)
183
                return -ENOMEM;
184
 
185
        _urb = _urb_alloc(0, GFP_ATOMIC);
186
        if (!_urb) {
187
                kfree(buf);
188
                return -ENOMEM;
189
        }
190
        _urb->type = HCI_EVENT_PKT;
191
        _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
192
 
193
        urb = &_urb->urb;
194
        pipe     = usb_rcvintpipe(husb->udev, husb->intr_in_ep->bEndpointAddress);
195
        interval = husb->intr_in_ep->bInterval;
196
        FILL_INT_URB(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
197
 
198
        err = usb_submit_urb(urb);
199
        if (err) {
200
                BT_ERR("%s intr rx submit failed urb %p err %d",
201
                                husb->hdev.name, urb, err);
202
                _urb_unlink(_urb);
203
                _urb_free(_urb);
204
                kfree(buf);
205
        }
206
        return err;
207
}
208
 
209
static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
210
{
211
        struct _urb *_urb;
212
        struct urb *urb;
213
        int err, pipe, size = HCI_MAX_FRAME_SIZE;
214
        void *buf;
215
 
216
        buf = kmalloc(size, GFP_ATOMIC);
217
        if (!buf)
218
                return -ENOMEM;
219
 
220
        _urb = _urb_alloc(0, GFP_ATOMIC);
221
        if (!_urb) {
222
                kfree(buf);
223
                return -ENOMEM;
224
        }
225
        _urb->type = HCI_ACLDATA_PKT;
226
        _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
227
 
228
        urb  = &_urb->urb;
229
        pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->bEndpointAddress);
230
        FILL_BULK_URB(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
231
        urb->transfer_flags = USB_QUEUE_BULK;
232
 
233
        BT_DBG("%s urb %p", husb->hdev.name, urb);
234
 
235
        err = usb_submit_urb(urb);
236
        if (err) {
237
                BT_ERR("%s bulk rx submit failed urb %p err %d",
238
                                husb->hdev.name, urb, err);
239
                _urb_unlink(_urb);
240
                _urb_free(_urb);
241
                kfree(buf);
242
        }
243
        return err;
244
}
245
 
246
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
247
static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
248
{
249
        struct _urb *_urb;
250
        struct urb *urb;
251
        int err, mtu, size;
252
        void *buf;
253
 
254
        mtu  = husb->isoc_in_ep->wMaxPacketSize;
255
        size = mtu * HCI_MAX_ISOC_FRAMES;
256
 
257
        buf = kmalloc(size, GFP_ATOMIC);
258
        if (!buf)
259
                return -ENOMEM;
260
 
261
        _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
262
        if (!_urb) {
263
                kfree(buf);
264
                return -ENOMEM;
265
        }
266
        _urb->type = HCI_SCODATA_PKT;
267
        _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
268
 
269
        urb = &_urb->urb;
270
 
271
        urb->context  = husb;
272
        urb->dev      = husb->udev;
273
        urb->pipe     = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->bEndpointAddress);
274
        urb->complete = hci_usb_rx_complete;
275
 
276
        urb->transfer_buffer_length = size;
277
        urb->transfer_buffer = buf;
278
        urb->transfer_flags  = USB_ISO_ASAP;
279
 
280
        __fill_isoc_desc(urb, size, mtu);
281
 
282
        BT_DBG("%s urb %p", husb->hdev.name, urb);
283
 
284
        err = usb_submit_urb(urb);
285
        if (err) {
286
                BT_ERR("%s isoc rx submit failed urb %p err %d",
287
                                husb->hdev.name, urb, err);
288
                _urb_unlink(_urb);
289
                _urb_free(_urb);
290
                kfree(buf);
291
        }
292
        return err;
293
}
294
#endif
295
 
296
/* Initialize device */
297
static int hci_usb_open(struct hci_dev *hdev)
298
{
299
        struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
300
        int i, err;
301
        unsigned long flags;
302
 
303
        BT_DBG("%s", hdev->name);
304
 
305
        if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
306
                return 0;
307
 
308
        MOD_INC_USE_COUNT;
309
 
310
        write_lock_irqsave(&husb->completion_lock, flags);
311
 
312
        err = hci_usb_intr_rx_submit(husb);
313
        if (!err) {
314
                for (i = 0; i < HCI_MAX_BULK_RX; i++)
315
                        hci_usb_bulk_rx_submit(husb);
316
 
317
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
318
                if (husb->isoc_iface)
319
                        for (i = 0; i < HCI_MAX_ISOC_RX; i++)
320
                                hci_usb_isoc_rx_submit(husb);
321
#endif
322
        } else {
323
                clear_bit(HCI_RUNNING, &hdev->flags);
324
                MOD_DEC_USE_COUNT;
325
        }
326
 
327
        write_unlock_irqrestore(&husb->completion_lock, flags);
328
        return err;
329
}
330
 
331
/* Reset device */
332
static int hci_usb_flush(struct hci_dev *hdev)
333
{
334
        struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
335
        int i;
336
 
337
        BT_DBG("%s", hdev->name);
338
 
339
        for (i=0; i < 4; i++)
340
                skb_queue_purge(&husb->transmit_q[i]);
341
        return 0;
342
}
343
 
344
static void hci_usb_unlink_urbs(struct hci_usb *husb)
345
{
346
        int i;
347
 
348
        BT_DBG("%s", husb->hdev.name);
349
 
350
        for (i=0; i < 4; i++) {
351
                struct _urb *_urb;
352
                struct urb *urb;
353
 
354
                /* Kill pending requests */
355
                while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
356
                        urb = &_urb->urb;
357
                        BT_DBG("%s unlinking _urb %p type %d urb %p",
358
                                        husb->hdev.name, _urb, _urb->type, urb);
359
                        usb_unlink_urb(urb);
360
                        _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
361
                }
362
 
363
                /* Release completed requests */
364
                while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
365
                        urb = &_urb->urb;
366
                        BT_DBG("%s freeing _urb %p type %d urb %p",
367
                                        husb->hdev.name, _urb, _urb->type, urb);
368
                        if (urb->setup_packet)
369
                                kfree(urb->setup_packet);
370
                        if (urb->transfer_buffer)
371
                                kfree(urb->transfer_buffer);
372
                        _urb_free(_urb);
373
                }
374
 
375
                /* Release reassembly buffers */
376
                if (husb->reassembly[i]) {
377
                        kfree_skb(husb->reassembly[i]);
378
                        husb->reassembly[i] = NULL;
379
                }
380
        }
381
}
382
 
383
/* Close device */
384
static int hci_usb_close(struct hci_dev *hdev)
385
{
386
        struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
387
        unsigned long flags;
388
 
389
        if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
390
                return 0;
391
 
392
        BT_DBG("%s", hdev->name);
393
 
394
        write_lock_irqsave(&husb->completion_lock, flags);
395
 
396
        hci_usb_unlink_urbs(husb);
397
        hci_usb_flush(hdev);
398
 
399
        write_unlock_irqrestore(&husb->completion_lock, flags);
400
 
401
        MOD_DEC_USE_COUNT;
402
        return 0;
403
}
404
 
405
static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
406
{
407
        struct urb *urb = &_urb->urb;
408
        int err;
409
 
410
        BT_DBG("%s urb %p type %d", husb->hdev.name, urb, _urb->type);
411
 
412
        _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
413
        err = usb_submit_urb(urb);
414
        if (err) {
415
                BT_ERR("%s tx submit failed urb %p type %d err %d",
416
                                husb->hdev.name, urb, _urb->type, err);
417
                _urb_unlink(_urb);
418
                _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
419
        } else
420
                atomic_inc(__pending_tx(husb, _urb->type));
421
 
422
        return err;
423
}
424
 
425
static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
426
{
427
        struct _urb *_urb = __get_completed(husb, skb->pkt_type);
428
        struct usb_ctrlrequest *dr;
429
        struct urb *urb;
430
 
431
        if (!_urb) {
432
                _urb = _urb_alloc(0, GFP_ATOMIC);
433
                if (!_urb)
434
                        return -ENOMEM;
435
                _urb->type = skb->pkt_type;
436
 
437
                dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
438
                if (!dr) {
439
                        _urb_free(_urb);
440
                        return -ENOMEM;
441
                }
442
        } else
443
                dr = (void *) _urb->urb.setup_packet;
444
 
445
        dr->bRequestType = husb->ctrl_req;
446
        dr->bRequest = 0;
447
        dr->wIndex   = 0;
448
        dr->wValue   = 0;
449
        dr->wLength  = __cpu_to_le16(skb->len);
450
 
451
        urb = &_urb->urb;
452
        FILL_CONTROL_URB(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
453
                (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
454
 
455
        BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
456
 
457
        _urb->priv = skb;
458
        return __tx_submit(husb, _urb);
459
}
460
 
461
static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
462
{
463
        struct _urb *_urb = __get_completed(husb, skb->pkt_type);
464
        struct urb *urb;
465
        int pipe;
466
 
467
        if (!_urb) {
468
                _urb = _urb_alloc(0, GFP_ATOMIC);
469
                if (!_urb)
470
                        return -ENOMEM;
471
                _urb->type = skb->pkt_type;
472
        }
473
 
474
        urb  = &_urb->urb;
475
        pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->bEndpointAddress);
476
        FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
477
                        hci_usb_tx_complete, husb);
478
        urb->transfer_flags = USB_QUEUE_BULK | USB_ZERO_PACKET;
479
 
480
        BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
481
 
482
        _urb->priv = skb;
483
        return __tx_submit(husb, _urb);
484
}
485
 
486
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
487
static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
488
{
489
        struct _urb *_urb = __get_completed(husb, skb->pkt_type);
490
        struct urb *urb;
491
 
492
        if (!_urb) {
493
                _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
494
                if (!_urb)
495
                        return -ENOMEM;
496
                _urb->type = skb->pkt_type;
497
        }
498
 
499
        BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
500
 
501
        urb = &_urb->urb;
502
 
503
        urb->context  = husb;
504
        urb->dev      = husb->udev;
505
        urb->pipe     = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->bEndpointAddress);
506
        urb->complete = hci_usb_tx_complete;
507
        urb->transfer_flags = USB_ISO_ASAP;
508
 
509
        urb->transfer_buffer = skb->data;
510
        urb->transfer_buffer_length = skb->len;
511
 
512
        __fill_isoc_desc(urb, skb->len, husb->isoc_out_ep->wMaxPacketSize);
513
 
514
        _urb->priv = skb;
515
        return __tx_submit(husb, _urb);
516
}
517
#endif
518
 
519
static void hci_usb_tx_process(struct hci_usb *husb)
520
{
521
        struct sk_buff_head *q;
522
        struct sk_buff *skb;
523
 
524
        BT_DBG("%s", husb->hdev.name);
525
 
526
        do {
527
                clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
528
 
529
                /* Process command queue */
530
                q = __transmit_q(husb, HCI_COMMAND_PKT);
531
                if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
532
                                (skb = skb_dequeue(q))) {
533
                        if (hci_usb_send_ctrl(husb, skb) < 0)
534
                                skb_queue_head(q, skb);
535
                }
536
 
537
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
538
                /* Process SCO queue */
539
                q = __transmit_q(husb, HCI_SCODATA_PKT);
540
                if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
541
                                (skb = skb_dequeue(q))) {
542
                        if (hci_usb_send_isoc(husb, skb) < 0)
543
                                skb_queue_head(q, skb);
544
                }
545
#endif
546
 
547
                /* Process ACL queue */
548
                q = __transmit_q(husb, HCI_ACLDATA_PKT);
549
                while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
550
                                (skb = skb_dequeue(q))) {
551
                        if (hci_usb_send_bulk(husb, skb) < 0) {
552
                                skb_queue_head(q, skb);
553
                                break;
554
                        }
555
                }
556
        } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
557
}
558
 
559
static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
560
{
561
        /* Serialize TX queue processing to avoid data reordering */
562
        if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
563
                hci_usb_tx_process(husb);
564
                clear_bit(HCI_USB_TX_PROCESS, &husb->state);
565
        } else
566
                set_bit(HCI_USB_TX_WAKEUP, &husb->state);
567
}
568
 
569
/* Send frames from HCI layer */
570
static int hci_usb_send_frame(struct sk_buff *skb)
571
{
572
        struct hci_dev *hdev = (struct hci_dev *) skb->dev;
573
        struct hci_usb *husb;
574
 
575
        if (!hdev) {
576
                BT_ERR("frame for uknown device (hdev=NULL)");
577
                return -ENODEV;
578
        }
579
 
580
        if (!test_bit(HCI_RUNNING, &hdev->flags))
581
                return -EBUSY;
582
 
583
        BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
584
 
585
        husb = (struct hci_usb *) hdev->driver_data;
586
 
587
        switch (skb->pkt_type) {
588
        case HCI_COMMAND_PKT:
589
                hdev->stat.cmd_tx++;
590
                break;
591
 
592
        case HCI_ACLDATA_PKT:
593
                hdev->stat.acl_tx++;
594
                break;
595
 
596
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
597
        case HCI_SCODATA_PKT:
598
                hdev->stat.sco_tx++;
599
                break;
600
#endif
601
 
602
        default:
603
                kfree_skb(skb);
604
                return 0;
605
        }
606
 
607
        read_lock(&husb->completion_lock);
608
 
609
        skb_queue_tail(__transmit_q(husb, skb->pkt_type), skb);
610
        hci_usb_tx_wakeup(husb);
611
 
612
        read_unlock(&husb->completion_lock);
613
        return 0;
614
}
615
 
616
static inline int __recv_frame(struct hci_usb *husb, int type, void *data, int count)
617
{
618
        BT_DBG("%s type %d data %p count %d", husb->hdev.name, type, data, count);
619
 
620
        husb->hdev.stat.byte_rx += count;
621
 
622
        while (count) {
623
                struct sk_buff *skb = __reassembly(husb, type);
624
                struct { int expect; } *scb;
625
                int len = 0;
626
 
627
                if (!skb) {
628
                        /* Start of the frame */
629
 
630
                        switch (type) {
631
                        case HCI_EVENT_PKT:
632
                                if (count >= HCI_EVENT_HDR_SIZE) {
633
                                        hci_event_hdr *h = data;
634
                                        len = HCI_EVENT_HDR_SIZE + h->plen;
635
                                } else
636
                                        return -EILSEQ;
637
                                break;
638
 
639
                        case HCI_ACLDATA_PKT:
640
                                if (count >= HCI_ACL_HDR_SIZE) {
641
                                        hci_acl_hdr *h = data;
642
                                        len = HCI_ACL_HDR_SIZE + __le16_to_cpu(h->dlen);
643
                                } else
644
                                        return -EILSEQ;
645
                                break;
646
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
647
                        case HCI_SCODATA_PKT:
648
                                if (count >= HCI_SCO_HDR_SIZE) {
649
                                        hci_sco_hdr *h = data;
650
                                        len = HCI_SCO_HDR_SIZE + h->dlen;
651
                                } else
652
                                        return -EILSEQ;
653
                                break;
654
#endif
655
                        }
656
                        BT_DBG("new packet len %d", len);
657
 
658
                        skb = bluez_skb_alloc(len, GFP_ATOMIC);
659
                        if (!skb) {
660
                                BT_ERR("%s no memory for the packet", husb->hdev.name);
661
                                return -ENOMEM;
662
                        }
663
                        skb->dev = (void *) &husb->hdev;
664
                        skb->pkt_type = type;
665
 
666
                        __reassembly(husb, type) = skb;
667
 
668
                        scb = (void *) skb->cb;
669
                        scb->expect = len;
670
                } else {
671
                        /* Continuation */
672
                        scb = (void *) skb->cb;
673
                        len = scb->expect;
674
                }
675
 
676
                len = min(len, count);
677
 
678
                memcpy(skb_put(skb, len), data, len);
679
 
680
                scb->expect -= len;
681
                if (!scb->expect) {
682
                        /* Complete frame */
683
                        __reassembly(husb, type) = NULL;
684
                        hci_recv_frame(skb);
685
                }
686
 
687
                count -= len; data += len;
688
        }
689
        return 0;
690
}
691
 
692
static void hci_usb_rx_complete(struct urb *urb)
693
{
694
        struct _urb *_urb = container_of(urb, struct _urb, urb);
695
        struct hci_usb *husb = (void *) urb->context;
696
        struct hci_dev *hdev = &husb->hdev;
697
        int    err, count = urb->actual_length;
698
 
699
        BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
700
                        _urb->type, urb->status, count, urb->transfer_flags);
701
 
702
        if (!test_bit(HCI_RUNNING, &hdev->flags))
703
                return;
704
 
705
        read_lock(&husb->completion_lock);
706
 
707
        if (urb->status || !count)
708
                goto resubmit;
709
 
710
        if (_urb->type == HCI_SCODATA_PKT) {
711
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
712
                int i;
713
                for (i=0; i < urb->number_of_packets; i++) {
714
                        BT_DBG("desc %d status %d offset %d len %d", i,
715
                                        urb->iso_frame_desc[i].status,
716
                                        urb->iso_frame_desc[i].offset,
717
                                        urb->iso_frame_desc[i].actual_length);
718
 
719
                        if (!urb->iso_frame_desc[i].status)
720
                                __recv_frame(husb, _urb->type,
721
                                        urb->transfer_buffer + urb->iso_frame_desc[i].offset,
722
                                        urb->iso_frame_desc[i].actual_length);
723
                }
724
#else
725
                ;
726
#endif
727
        } else {
728
                err = __recv_frame(husb, _urb->type, urb->transfer_buffer, count);
729
                if (err < 0) {
730
                        BT_ERR("%s corrupted packet: type %d count %d",
731
                                        husb->hdev.name, _urb->type, count);
732
                        hdev->stat.err_rx++;
733
                }
734
        }
735
 
736
resubmit:
737
        if (_urb->type != HCI_EVENT_PKT) {
738
                urb->dev = husb->udev;
739
                err      = usb_submit_urb(urb);
740
                BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
741
                                _urb->type, err);
742
        }
743
        read_unlock(&husb->completion_lock);
744
}
745
 
746
static void hci_usb_tx_complete(struct urb *urb)
747
{
748
        struct _urb *_urb = container_of(urb, struct _urb, urb);
749
        struct hci_usb *husb = (void *) urb->context;
750
        struct hci_dev *hdev = &husb->hdev;
751
 
752
        BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
753
                        urb->status, urb->transfer_flags);
754
 
755
        atomic_dec(__pending_tx(husb, _urb->type));
756
 
757
        urb->transfer_buffer = NULL;
758
        kfree_skb((struct sk_buff *) _urb->priv);
759
 
760
        if (!test_bit(HCI_RUNNING, &hdev->flags))
761
                return;
762
 
763
        if (!urb->status)
764
                hdev->stat.byte_tx += urb->transfer_buffer_length;
765
        else
766
                hdev->stat.err_tx++;
767
 
768
        read_lock(&husb->completion_lock);
769
 
770
        _urb_unlink(_urb);
771
        _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
772
 
773
        hci_usb_tx_wakeup(husb);
774
 
775
        read_unlock(&husb->completion_lock);
776
}
777
 
778
static void hci_usb_destruct(struct hci_dev *hdev)
779
{
780
        struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
781
 
782
        BT_DBG("%s", hdev->name);
783
 
784
        kfree(husb);
785
}
786
 
787
static void *hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
788
{
789
        struct usb_endpoint_descriptor *bulk_out_ep[HCI_MAX_IFACE_NUM];
790
        struct usb_endpoint_descriptor *isoc_out_ep[HCI_MAX_IFACE_NUM];
791
        struct usb_endpoint_descriptor *bulk_in_ep[HCI_MAX_IFACE_NUM];
792
        struct usb_endpoint_descriptor *isoc_in_ep[HCI_MAX_IFACE_NUM];
793
        struct usb_endpoint_descriptor *intr_in_ep[HCI_MAX_IFACE_NUM];
794
        struct usb_interface_descriptor *uif;
795
        struct usb_endpoint_descriptor *ep;
796
        struct usb_interface *iface, *isoc_iface;
797
        struct hci_usb *husb;
798
        struct hci_dev *hdev;
799
        int i, a, e, size, ifn, isoc_ifnum, isoc_alts;
800
 
801
        BT_DBG("udev %p ifnum %d", udev, ifnum);
802
 
803
        iface = &udev->actconfig->interface[0];
804
 
805
        if (!id->driver_info) {
806
                const struct usb_device_id *match;
807
                match = usb_match_id(udev, iface, blacklist_ids);
808
                if (match)
809
                        id = match;
810
        }
811
 
812
        if (id->driver_info & HCI_IGNORE)
813
                return NULL;
814
 
815
        /* Check number of endpoints */
816
        if (udev->actconfig->interface[ifnum].altsetting[0].bNumEndpoints < 3)
817
                return NULL;
818
 
819
        memset(bulk_out_ep, 0, sizeof(bulk_out_ep));
820
        memset(isoc_out_ep, 0, sizeof(isoc_out_ep));
821
        memset(bulk_in_ep,  0, sizeof(bulk_in_ep));
822
        memset(isoc_in_ep,  0, sizeof(isoc_in_ep));
823
        memset(intr_in_ep,  0, sizeof(intr_in_ep));
824
 
825
        size = 0;
826
        isoc_iface = NULL;
827
        isoc_alts  = isoc_ifnum = 0;
828
 
829
        /* Find endpoints that we need */
830
 
831
        ifn = MIN(udev->actconfig->bNumInterfaces, HCI_MAX_IFACE_NUM);
832
        for (i = 0; i < ifn; i++) {
833
                iface = &udev->actconfig->interface[i];
834
                for (a = 0; a < iface->num_altsetting; a++) {
835
                        uif = &iface->altsetting[a];
836
                        for (e = 0; e < uif->bNumEndpoints; e++) {
837
                                ep = &uif->endpoint[e];
838
 
839
                                switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
840
                                case USB_ENDPOINT_XFER_INT:
841
                                        if (ep->bEndpointAddress & USB_DIR_IN)
842
                                                intr_in_ep[i] = ep;
843
                                        break;
844
 
845
                                case USB_ENDPOINT_XFER_BULK:
846
                                        if (ep->bEndpointAddress & USB_DIR_IN)
847
                                                bulk_in_ep[i]  = ep;
848
                                        else
849
                                                bulk_out_ep[i] = ep;
850
                                        break;
851
 
852
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
853
                                case USB_ENDPOINT_XFER_ISOC:
854
                                        if (ep->wMaxPacketSize < size || a > 2)
855
                                                break;
856
                                        size = ep->wMaxPacketSize;
857
 
858
                                        isoc_iface = iface;
859
                                        isoc_alts  = a;
860
                                        isoc_ifnum = i;
861
 
862
                                        if (ep->bEndpointAddress & USB_DIR_IN)
863
                                                isoc_in_ep[i]  = ep;
864
                                        else
865
                                                isoc_out_ep[i] = ep;
866
                                        break;
867
#endif
868
                                }
869
                        }
870
                }
871
        }
872
 
873
        if (!bulk_in_ep[0] || !bulk_out_ep[0] || !intr_in_ep[0]) {
874
                BT_DBG("Bulk endpoints not found");
875
                goto done;
876
        }
877
 
878
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
879
        if (!isoc_in_ep[1] || !isoc_out_ep[1]) {
880
                BT_DBG("Isoc endpoints not found");
881
                isoc_iface = NULL;
882
        }
883
#endif
884
 
885
        if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
886
                BT_ERR("Can't allocate: control structure");
887
                goto done;
888
        }
889
 
890
        memset(husb, 0, sizeof(struct hci_usb));
891
 
892
        husb->udev = udev;
893
        husb->bulk_out_ep = bulk_out_ep[0];
894
        husb->bulk_in_ep  = bulk_in_ep[0];
895
        husb->intr_in_ep  = intr_in_ep[0];
896
 
897
        if (id->driver_info & HCI_DIGIANSWER)
898
                husb->ctrl_req = HCI_DIGI_REQ;
899
        else
900
                husb->ctrl_req = HCI_CTRL_REQ;
901
 
902
#ifdef CONFIG_BLUEZ_HCIUSB_SCO
903
        if (isoc_iface) {
904
                BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
905
                if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
906
                        BT_ERR("Can't set isoc interface settings");
907
                        isoc_iface = NULL;
908
                }
909
                usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb);
910
                husb->isoc_iface  = isoc_iface;
911
                husb->isoc_in_ep  = isoc_in_ep[isoc_ifnum];
912
                husb->isoc_out_ep = isoc_out_ep[isoc_ifnum];
913
        }
914
#endif
915
 
916
        husb->completion_lock = RW_LOCK_UNLOCKED;
917
 
918
        for (i = 0; i < 4; i++) {
919
                skb_queue_head_init(&husb->transmit_q[i]);
920
                _urb_queue_init(&husb->pending_q[i]);
921
                _urb_queue_init(&husb->completed_q[i]);
922
        }
923
 
924
        /* Initialize and register HCI device */
925
        hdev = &husb->hdev;
926
 
927
        hdev->type  = HCI_USB;
928
        hdev->driver_data = husb;
929
 
930
        hdev->open  = hci_usb_open;
931
        hdev->close = hci_usb_close;
932
        hdev->flush = hci_usb_flush;
933
        hdev->send  = hci_usb_send_frame;
934
        hdev->destruct = hci_usb_destruct;
935
 
936
        if (id->driver_info & HCI_RESET)
937
                set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
938
 
939
        if (hci_register_dev(hdev) < 0) {
940
                BT_ERR("Can't register HCI device");
941
                goto probe_error;
942
        }
943
 
944
        return husb;
945
 
946
probe_error:
947
        kfree(husb);
948
 
949
done:
950
        return NULL;
951
}
952
 
953
static void hci_usb_disconnect(struct usb_device *udev, void *ptr)
954
{
955
        struct hci_usb *husb = (struct hci_usb *) ptr;
956
        struct hci_dev *hdev = &husb->hdev;
957
 
958
        if (!husb)
959
                return;
960
 
961
        BT_DBG("%s", hdev->name);
962
 
963
        hci_usb_close(hdev);
964
 
965
        if (husb->isoc_iface)
966
                usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
967
 
968
        if (hci_unregister_dev(hdev) < 0)
969
                BT_ERR("Can't unregister HCI device %s", hdev->name);
970
}
971
 
972
static struct usb_driver hci_usb_driver = {
973
        name:           "hci_usb",
974
        probe:          hci_usb_probe,
975
        disconnect:     hci_usb_disconnect,
976
        id_table:       bluetooth_ids,
977
};
978
 
979
int hci_usb_init(void)
980
{
981
        int err;
982
 
983
        BT_INFO("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",
984
                VERSION);
985
        BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
986
 
987
        if ((err = usb_register(&hci_usb_driver)) < 0)
988
                BT_ERR("Failed to register HCI USB driver");
989
 
990
        return err;
991
}
992
 
993
void hci_usb_cleanup(void)
994
{
995
        usb_deregister(&hci_usb_driver);
996
}
997
 
998
module_init(hci_usb_init);
999
module_exit(hci_usb_cleanup);
1000
 
1001
MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1002
MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
1003
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.