1/*
2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef _BTHCI_TRANSPORT_H_
6#define _BTHCI_TRANSPORT_H_
7
8#include <bluetooth/HCI/btHCI.h>
9
10#include <util/DoublyLinkedList.h>
11
12#include <net_buffer.h>
13#include <Drivers.h>
14
15
16typedef enum {
17	ANCILLYANT = (1<<0),
18	RUNNING = (1<<1),
19	LEAVING = (1<<2),
20	SENDING = (1<<3),
21	PROCESSING = (1<<4)
22} bt_transport_status_t;
23
24
25typedef uint8 bt_stat_t;
26typedef struct bt_hci_statistics {
27	bt_stat_t acceptedTX;
28	bt_stat_t rejectedTX;
29	bt_stat_t successfulTX;
30	bt_stat_t errorTX;
31
32	bt_stat_t acceptedRX;
33	bt_stat_t rejectedRX;
34	bt_stat_t successfulRX;
35	bt_stat_t errorRX;
36
37	bt_stat_t commandTX;
38	bt_stat_t eventRX;
39	bt_stat_t aclTX;
40	bt_stat_t aclRX;
41	bt_stat_t scoTX;
42	bt_stat_t scoRX;
43	bt_stat_t escoTX;
44	bt_stat_t escoRX;
45
46	bt_stat_t bytesRX;
47	bt_stat_t bytesTX;
48} bt_hci_statistics;
49
50
51typedef struct bt_hci_device {
52	transport_type	kind;
53	char			realName[B_OS_NAME_LENGTH];
54} bt_hci_device;
55
56
57/* Hooks which drivers will have to provide.
58 * The structure is meant to be allocated in driver side and
59 * provided to the HCI where it will fill the remaining fields
60 */
61typedef struct bt_hci_transport_hooks {
62
63	// to be filled by driver
64	status_t	(*SendCommand)(hci_id hciId, void* command);
65	status_t	(*SendACL)(hci_id hciId, net_buffer* nbuf);
66	status_t	(*SendSCO)(hci_id hciId, net_buffer* nbuf);
67	status_t	(*SendESCO)(hci_id hciId, net_buffer* nbuf);
68
69	status_t	(*DeliverStatistics)(hci_id hciId, bt_hci_statistics* statistics);
70
71	transport_type kind;
72
73} bt_hci_transport_hooks;
74
75typedef struct bt_hci_device_information {
76
77	uint32	flags;
78	uint16	vendorId;
79	uint16	deviceId;
80	char	name[B_OS_NAME_LENGTH];
81
82} bt_hci_device_information;
83
84
85#ifdef __cplusplus
86
87struct bluetooth_device : DoublyLinkedListLinkImpl<bluetooth_device> {
88
89	net_buffer*	fBuffersRx[HCI_NUM_PACKET_TYPES];
90	size_t		fExpectedPacketSize[HCI_NUM_PACKET_TYPES];
91	hci_id		index;
92
93	uint16		supportedPacketTypes;
94	uint16		linkMode;
95	int			fd;
96
97	bt_hci_device_information*	info;
98	bt_hci_transport_hooks*		hooks;
99	uint16						mtu;
100
101};
102
103#else
104
105struct bluetooth_device;
106
107#endif
108
109
110#define BT_HCI_MODULE_NAME "bluetooth/hci/v1"
111
112// Possible definition of a bus manager
113typedef struct bt_hci_module_info {
114	module_info info;
115	// Registration in Stack
116	status_t			(*RegisterDriver)(bt_hci_transport_hooks* hooks,
117							bluetooth_device** device);
118	status_t			(*UnregisterDriver)(hci_id id);
119	bluetooth_device*	(*FindDeviceByID)(hci_id id);
120
121	// to be called from transport driver
122	status_t			(*PostTransportPacket)(hci_id hid, bt_packet_t type,
123							void* data, size_t count);
124
125	// To be called from upper layers
126	status_t		(*PostACL)(hci_id hciId, net_buffer* buffer);
127	status_t		(*PostSCO)(hci_id hciId, net_buffer* buffer);
128	status_t		(*PostESCO)(hci_id hciId, net_buffer* buffer);
129
130} bt_hci_module_info ;
131
132
133/* Here the transport driver have some flags that
134 * can be used to inform the upper layer about some
135 * special behaouvior to perform */
136
137#define BT_IGNORE_THIS_DEVICE	(1 << 0)
138#define BT_SCO_NOT_WORKING		(1 << 1)
139#define BT_WILL_NEED_A_RESET	(1 << 2)
140#define BT_DIGIANSWER			(1 << 4)
141
142// Mandatory IOCTLS
143#define BT_IOCTLS_OFFSET 3000
144
145enum {
146	ISSUE_BT_COMMAND = B_DEVICE_OP_CODES_END + BT_IOCTLS_OFFSET, // 12999
147	GET_STATS,
148	GET_NOTIFICATION_PORT,
149	GET_HCI_ID,
150	BT_UP
151};
152
153// To deprecate ...
154#define PACK_PORTCODE(type,hid,data) ((type & 0xFF) << 24 | (hid & 0xFF) << 16 | (data & 0xFFFF))
155#define GET_PORTCODE_TYPE(code) ((code & 0xFF000000) >> 24)
156#define GET_PORTCODE_HID(code) ((code & 0x00FF0000) >> 16)
157#define GET_PORTCODE_DATA(code) ((code & 0x0000FFFF))
158
159/*  Port drivers can use to send information (1 for all for
160	at moment refer to ioctl GET_NOTIFICATION_PORT)*/
161#define BT_USERLAND_PORT_NAME "BT Kernel-User Event"
162#define BT_RX_PORT_NAME "BT Kernel RX assembly"
163#define BLUETOOTH_CONNECTION_PORT "bluetooth connection port"
164#define BLUETOOTH_CONNECTION_SCHED_PORT "bluetooth con sched port"
165
166#endif // _BTHCI_TRANSPORT_H_
167