1/*
2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 *
4 * All rights reserved. Distributed under the terms of the MIT License.
5 *
6 */
7#ifndef _H2GENERIC_H_
8#define _H2GENERIC_H_
9
10
11#include <net_buffer.h>
12#include <net_device.h>
13
14#include <OS.h>
15#include <USB3.h>
16
17#include <util/list.h>
18#include <bluetooth/HCI/btHCI.h>
19#include <bluetooth/HCI/btHCI_transport.h>
20
21#include <btCoreData.h>
22
23#include "snet_buffer.h"
24
25
26// USB definitions for the generic device move to h2cfg
27#define UDCLASS_WIRELESS	0xe0
28#define UDSUBCLASS_RF		0x01
29#define UDPROTO_BLUETOOTH	0x01
30
31#define BLUETOOTH_DEVICE_TRANSPORT	"h2"
32#define BLUETOOTH_DEVICE_NAME "generic"
33#include "h2cfg.h"
34
35#define USB_TYPE_CLASS			(0x01 << 5)  /// Check if it is in some other header
36#define USB_TYPE_VENDOR			(0x02 << 5)
37
38#define TOUCH(x) ((void)(x))
39
40// Expecting nobody is gonna have 16 USB-BT dongles connected in their system
41#define MAX_BT_GENERIC_USB_DEVICES	16
42
43extern usb_module_info* usb;
44extern bt_hci_module_info* hci;
45extern struct bt_hci_module_info* btDevices;
46extern struct net_buffer_module_info* nb;
47extern struct bluetooth_core_data_module_info* btCoreData;
48
49#define MAX_COMMAND_WINDOW 1
50#define MAX_ACL_OUT_WINDOW 4
51#define MAX_ACL_IN_WINDOW  1
52
53#define MAX_NUM_QUEUED_PACKETS 1
54#define NUM_BUFFERS 1
55
56typedef struct bt_usb_dev bt_usb_dev;
57
58struct bt_usb_dev {
59	usb_device		dev;          /* opaque handle */
60	hci_id					hdev; /* HCI device id*/
61	bluetooth_device*		ndev;
62
63	char			name[B_OS_NAME_LENGTH];
64	bool			connected;    /* is the device plugged into the USB? */
65	int32			open_count;   /* number of clients of the device */
66	int32			num;          /* instance number of the device */
67
68	sem_id			lock;         /* synchronize access to the device */
69	sem_id			cmd_complete; /* To synchronize completitions */
70
71	size_t			actual_len;   /* length of data returned by command */
72	status_t		cmd_status;   /* result of command */
73
74	uint8				ctrl_req;
75	uint8				driver_info;
76	uint32				state;
77
78	bt_hci_statistics	stat;
79
80	const	usb_endpoint_info*	bulk_in_ep;
81			uint16				max_packet_size_bulk_in;
82	const	usb_endpoint_info*	bulk_out_ep;
83			uint16				max_packet_size_bulk_out;
84	const	usb_endpoint_info*	intr_in_ep;
85			uint16				max_packet_size_intr_in;
86
87#ifdef BLUETOOTH_SUPPORTS_SCO
88	const usb_endpoint_info	*iso_in_ep;
89	const usb_endpoint_info	*iso_out_ep;
90#endif
91
92	/* This so called rooms, are for dumping the USB RX frames
93	 * and try to reuse the allocations. see util submodule
94	 */
95	struct list eventRoom;
96	struct list aclRoom;
97
98	// Tx buffers: net_buffers for BT_ACL and snet_buffers for BT_COMMAND
99	// in the same array
100	struct list	nbuffersTx[BT_DRIVER_TXCOVERAGE];
101	uint32		nbuffersPendingTx[BT_DRIVER_TXCOVERAGE];
102
103	// Rx buffer
104	net_buffer*		nbufferRx[BT_DRIVER_RXCOVERAGE];
105	snet_buffer*	eventRx;
106
107	// for who ever needs preallocated buffers
108	struct list		snetBufferRecycleTrash;
109
110};
111
112bt_usb_dev* fetch_device(bt_usb_dev* dev, hci_id hid);
113
114
115static inline uint32
116TEST_AND_SET(uint32 *byte, uint32 bit_mask)
117{
118	uint32 val = (*byte&bit_mask)!=0;
119	*byte |= bit_mask;
120	return val;
121}
122
123
124static inline uint32
125TEST_AND_CLEAR(uint32* byte, uint32 bit_mask)
126{
127	uint32 val = (*byte&bit_mask)!=0;
128	*byte &= ~bit_mask;
129	return val;
130}
131
132
133#endif
134