1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4 
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10 
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 /*
26  * $Id: hci_core.h,v 1.5 2002/06/27 04:56:30 maxk Exp $
27  */
28 
29 #ifndef __HCI_CORE_H
30 #define __HCI_CORE_H
31 
32 #include <net/bluetooth/hci.h>
33 
34 /* HCI upper protocols */
35 #define HCI_PROTO_L2CAP	0
36 #define HCI_PROTO_SCO	1
37 
38 #define HCI_INIT_TIMEOUT (HZ * 10)
39 
40 /* HCI Core structures */
41 
42 struct inquiry_entry {
43 	struct inquiry_entry 	*next;
44 	__u32			timestamp;
45 	inquiry_info		info;
46 };
47 
48 struct inquiry_cache {
49 	spinlock_t 		lock;
50 	__u32			timestamp;
51 	struct inquiry_entry 	*list;
52 };
53 
54 struct conn_hash {
55 	struct list_head list;
56 	spinlock_t       lock;
57 	unsigned int     num;
58 };
59 
60 struct hci_dev {
61 	struct list_head list;
62 	spinlock_t	lock;
63 	atomic_t 	refcnt;
64 
65 	char		name[8];
66 	unsigned long	flags;
67 	__u16		id;
68 	__u8	 	type;
69 	bdaddr_t	bdaddr;
70 	__u8		features[8];
71 
72 	__u16		pkt_type;
73 	__u16		link_policy;
74 	__u16		link_mode;
75 
76 	unsigned long	quirks;
77 
78 	atomic_t 	cmd_cnt;
79 	unsigned int 	acl_cnt;
80 	unsigned int 	sco_cnt;
81 
82 	unsigned int	acl_mtu;
83 	unsigned int 	sco_mtu;
84 	unsigned int	acl_pkts;
85 	unsigned int	sco_pkts;
86 
87 	unsigned long   cmd_last_tx;
88 	unsigned long   acl_last_tx;
89 	unsigned long   sco_last_tx;
90 
91 	struct tasklet_struct 	cmd_task;
92 	struct tasklet_struct	rx_task;
93 	struct tasklet_struct 	tx_task;
94 
95 	struct sk_buff_head	rx_q;
96 	struct sk_buff_head 	raw_q;
97 	struct sk_buff_head 	cmd_q;
98 
99 	struct sk_buff     	*sent_cmd;
100 
101 	struct semaphore	req_lock;
102 	wait_queue_head_t	req_wait_q;
103 	__u32			req_status;
104 	__u32			req_result;
105 
106 	struct inquiry_cache 	inq_cache;
107 	struct conn_hash 	conn_hash;
108 
109 	struct hci_dev_stats 	stat;
110 
111 	void			*driver_data;
112 	void			*core_data;
113 
114 	atomic_t 		promisc;
115 
116 	int (*open)(struct hci_dev *hdev);
117 	int (*close)(struct hci_dev *hdev);
118 	int (*flush)(struct hci_dev *hdev);
119 	int (*send)(struct sk_buff *skb);
120 	void (*destruct)(struct hci_dev *hdev);
121 	int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
122 };
123 
124 struct hci_conn {
125 	struct list_head list;
126 
127 	atomic_t	 refcnt;
128 	spinlock_t	 lock;
129 
130 	bdaddr_t         dst;
131 	__u16            handle;
132 	__u16            state;
133 	__u8		 type;
134 	__u8		 out;
135 	__u32		 link_mode;
136 	unsigned long	 pend;
137 
138 	unsigned int	 sent;
139 
140 	struct sk_buff_head data_q;
141 
142 	struct timer_list timer;
143 
144 	struct hci_dev 	*hdev;
145 	void		*l2cap_data;
146 	void		*sco_data;
147 	void		*priv;
148 
149 	struct hci_conn *link;
150 };
151 
152 extern struct hci_proto *hci_proto[];
153 extern struct list_head hdev_list;
154 extern rwlock_t hdev_list_lock;
155 
156 /* ----- Inquiry cache ----- */
157 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   // 30 seconds
158 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   // 60 seconds
159 
160 #define inquiry_cache_lock(c)		spin_lock(&c->lock)
161 #define inquiry_cache_unlock(c)		spin_unlock(&c->lock)
162 #define inquiry_cache_lock_bh(c)	spin_lock_bh(&c->lock)
163 #define inquiry_cache_unlock_bh(c)	spin_unlock_bh(&c->lock)
164 
inquiry_cache_init(struct hci_dev * hdev)165 static inline void inquiry_cache_init(struct hci_dev *hdev)
166 {
167 	struct inquiry_cache *c = &hdev->inq_cache;
168 	spin_lock_init(&c->lock);
169 	c->list = NULL;
170 }
171 
inquiry_cache_empty(struct hci_dev * hdev)172 static inline int inquiry_cache_empty(struct hci_dev *hdev)
173 {
174 	struct inquiry_cache *c = &hdev->inq_cache;
175 	return (c->list == NULL);
176 }
177 
inquiry_cache_age(struct hci_dev * hdev)178 static inline long inquiry_cache_age(struct hci_dev *hdev)
179 {
180 	struct inquiry_cache *c = &hdev->inq_cache;
181 	return jiffies - c->timestamp;
182 }
183 
inquiry_entry_age(struct inquiry_entry * e)184 static inline long inquiry_entry_age(struct inquiry_entry *e)
185 {
186 	return jiffies - e->timestamp;
187 }
188 
189 struct inquiry_entry *inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
190 void inquiry_cache_update(struct hci_dev *hdev, inquiry_info *info);
191 void inquiry_cache_flush(struct hci_dev *hdev);
192 int  inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf);
193 
194 /* ----- HCI Connections ----- */
195 enum {
196 	HCI_CONN_AUTH_PEND,
197 	HCI_CONN_ENCRYPT_PEND
198 };
199 
200 #define hci_conn_lock(c)	spin_lock(&c->lock)
201 #define hci_conn_unlock(c)	spin_unlock(&c->lock)
202 #define hci_conn_lock_bh(c)	spin_lock_bh(&c->lock)
203 #define hci_conn_unlock_bh(c)	spin_unlock_bh(&c->lock)
204 
205 #define conn_hash_lock(d)	spin_lock(&d->conn_hash->lock)
206 #define conn_hash_unlock(d)	spin_unlock(&d->conn_hash->lock)
207 #define conn_hash_lock_bh(d)	spin_lock_bh(&d->conn_hash->lock)
208 #define conn_hash_unlock_bh(d)	spin_unlock_bh(&d->conn_hash->lock)
209 
conn_hash_init(struct hci_dev * hdev)210 static inline void conn_hash_init(struct hci_dev *hdev)
211 {
212 	struct conn_hash *h = &hdev->conn_hash;
213 	INIT_LIST_HEAD(&h->list);
214 	spin_lock_init(&h->lock);
215 	h->num = 0;
216 }
217 
conn_hash_add(struct hci_dev * hdev,struct hci_conn * c)218 static inline void conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
219 {
220 	struct conn_hash *h = &hdev->conn_hash;
221 	list_add(&c->list, &h->list);
222 	h->num++;
223 }
224 
conn_hash_del(struct hci_dev * hdev,struct hci_conn * c)225 static inline void conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
226 {
227 	struct conn_hash *h = &hdev->conn_hash;
228 	list_del(&c->list);
229 	h->num--;
230 }
231 
conn_hash_lookup_handle(struct hci_dev * hdev,__u16 handle)232 static inline struct hci_conn *conn_hash_lookup_handle(struct hci_dev *hdev,
233 	       				__u16 handle)
234 {
235 	register struct conn_hash *h = &hdev->conn_hash;
236 	register struct list_head *p;
237 	register struct hci_conn  *c;
238 
239 	list_for_each(p, &h->list) {
240 		c = list_entry(p, struct hci_conn, list);
241 		if (c->handle == handle)
242 			return c;
243 	}
244         return NULL;
245 }
246 
conn_hash_lookup_ba(struct hci_dev * hdev,__u8 type,bdaddr_t * ba)247 static inline struct hci_conn *conn_hash_lookup_ba(struct hci_dev *hdev,
248 					__u8 type, bdaddr_t *ba)
249 {
250 	register struct conn_hash *h = &hdev->conn_hash;
251 	register struct list_head *p;
252 	register struct hci_conn  *c;
253 
254 	list_for_each(p, &h->list) {
255 		c = list_entry(p, struct hci_conn, list);
256 		if (c->type == type && !bacmp(&c->dst, ba))
257 			return c;
258 	}
259         return NULL;
260 }
261 
262 void hci_acl_connect(struct hci_conn *conn);
263 void hci_acl_disconn(struct hci_conn *conn, __u8 reason);
264 void hci_add_sco(struct hci_conn *conn, __u16 handle);
265 
266 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
267 int    hci_conn_del(struct hci_conn *conn);
268 void   hci_conn_hash_flush(struct hci_dev *hdev);
269 
270 struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *src);
271 int hci_conn_auth(struct hci_conn *conn);
272 int hci_conn_encrypt(struct hci_conn *conn);
273 
hci_conn_set_timer(struct hci_conn * conn,long timeout)274 static inline void hci_conn_set_timer(struct hci_conn *conn, long timeout)
275 {
276 	mod_timer(&conn->timer, jiffies + timeout);
277 }
278 
hci_conn_del_timer(struct hci_conn * conn)279 static inline void hci_conn_del_timer(struct hci_conn *conn)
280 {
281 	del_timer(&conn->timer);
282 }
283 
hci_conn_hold(struct hci_conn * conn)284 static inline void hci_conn_hold(struct hci_conn *conn)
285 {
286 	atomic_inc(&conn->refcnt);
287 	hci_conn_del_timer(conn);
288 }
289 
hci_conn_put(struct hci_conn * conn)290 static inline void hci_conn_put(struct hci_conn *conn)
291 {
292 	if (atomic_dec_and_test(&conn->refcnt)) {
293 		if (conn->type == ACL_LINK) {
294 			unsigned long timeo = (conn->out) ?
295 				HCI_DISCONN_TIMEOUT : HCI_DISCONN_TIMEOUT * 2;
296 			hci_conn_set_timer(conn, timeo);
297 		} else
298 			hci_conn_set_timer(conn, HZ / 100);
299 	}
300 }
301 
302 /* ----- HCI Devices ----- */
hci_dev_put(struct hci_dev * d)303 static inline void hci_dev_put(struct hci_dev *d)
304 {
305 	if (atomic_dec_and_test(&d->refcnt))
306 		d->destruct(d);
307 }
308 #define hci_dev_hold(d)		atomic_inc(&d->refcnt)
309 
310 #define hci_dev_lock(d)		spin_lock(&d->lock)
311 #define hci_dev_unlock(d)	spin_unlock(&d->lock)
312 #define hci_dev_lock_bh(d)	spin_lock_bh(&d->lock)
313 #define hci_dev_unlock_bh(d)	spin_unlock_bh(&d->lock)
314 
315 struct hci_dev *hci_dev_get(int index);
316 struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
317 int hci_register_dev(struct hci_dev *hdev);
318 int hci_unregister_dev(struct hci_dev *hdev);
319 int hci_suspend_dev(struct hci_dev *hdev);
320 int hci_resume_dev(struct hci_dev *hdev);
321 int hci_dev_open(__u16 dev);
322 int hci_dev_close(__u16 dev);
323 int hci_dev_reset(__u16 dev);
324 int hci_dev_reset_stat(__u16 dev);
325 int hci_dev_cmd(unsigned int cmd, unsigned long arg);
326 int hci_get_dev_list(unsigned long arg);
327 int hci_get_dev_info(unsigned long arg);
328 int hci_get_conn_list(unsigned long arg);
329 int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg);
330 int hci_inquiry(unsigned long arg);
331 
332 int  hci_recv_frame(struct sk_buff *skb);
333 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
334 
335 /* ----- LMP capabilities ----- */
336 #define lmp_rswitch_capable(dev) (dev->features[0] & LMP_RSWITCH)
337 #define lmp_encrypt_capable(dev) (dev->features[0] & LMP_ENCRYPT)
338 
339 /* ----- HCI tasks ----- */
hci_sched_cmd(struct hci_dev * hdev)340 static inline void hci_sched_cmd(struct hci_dev *hdev)
341 {
342 	tasklet_schedule(&hdev->cmd_task);
343 }
344 
hci_sched_rx(struct hci_dev * hdev)345 static inline void hci_sched_rx(struct hci_dev *hdev)
346 {
347 	tasklet_schedule(&hdev->rx_task);
348 }
349 
hci_sched_tx(struct hci_dev * hdev)350 static inline void hci_sched_tx(struct hci_dev *hdev)
351 {
352 	tasklet_schedule(&hdev->tx_task);
353 }
354 
355 /* ----- HCI protocols ----- */
356 struct hci_proto {
357 	char 		*name;
358 	unsigned int 	id;
359 	unsigned long	flags;
360 
361 	void		*priv;
362 
363 	int (*connect_ind) 	(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type);
364 	int (*connect_cfm)	(struct hci_conn *conn, __u8 status);
365 	int (*disconn_ind)	(struct hci_conn *conn, __u8 reason);
366 	int (*recv_acldata)	(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
367 	int (*recv_scodata)	(struct hci_conn *conn, struct sk_buff *skb);
368 	int (*auth_cfm)		(struct hci_conn *conn, __u8 status);
369 	int (*encrypt_cfm)	(struct hci_conn *conn, __u8 status);
370 };
371 
hci_proto_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 type)372 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
373 {
374 	register struct hci_proto *hp;
375 	int mask = 0;
376 
377 	hp = hci_proto[HCI_PROTO_L2CAP];
378 	if (hp && hp->connect_ind)
379 		mask |= hp->connect_ind(hdev, bdaddr, type);
380 
381 	hp = hci_proto[HCI_PROTO_SCO];
382 	if (hp && hp->connect_ind)
383 		mask |= hp->connect_ind(hdev, bdaddr, type);
384 
385 	return mask;
386 }
387 
hci_proto_connect_cfm(struct hci_conn * conn,__u8 status)388 static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
389 {
390 	register struct hci_proto *hp;
391 
392 	hp = hci_proto[HCI_PROTO_L2CAP];
393 	if (hp && hp->connect_cfm)
394 		hp->connect_cfm(conn, status);
395 
396 	hp = hci_proto[HCI_PROTO_SCO];
397 	if (hp && hp->connect_cfm)
398 		hp->connect_cfm(conn, status);
399 }
400 
hci_proto_disconn_ind(struct hci_conn * conn,__u8 reason)401 static inline void hci_proto_disconn_ind(struct hci_conn *conn, __u8 reason)
402 {
403 	register struct hci_proto *hp;
404 
405 	hp = hci_proto[HCI_PROTO_L2CAP];
406 	if (hp && hp->disconn_ind)
407 		hp->disconn_ind(conn, reason);
408 
409 	hp = hci_proto[HCI_PROTO_SCO];
410 	if (hp && hp->disconn_ind)
411 		hp->disconn_ind(conn, reason);
412 }
413 
hci_proto_auth_cfm(struct hci_conn * conn,__u8 status)414 static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
415 {
416 	register struct hci_proto *hp;
417 
418 	hp = hci_proto[HCI_PROTO_L2CAP];
419 	if (hp && hp->auth_cfm)
420 		hp->auth_cfm(conn, status);
421 
422 	hp = hci_proto[HCI_PROTO_SCO];
423 	if (hp && hp->auth_cfm)
424 		hp->auth_cfm(conn, status);
425 }
426 
hci_proto_encrypt_cfm(struct hci_conn * conn,__u8 status)427 static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status)
428 {
429 	register struct hci_proto *hp;
430 
431 	hp = hci_proto[HCI_PROTO_L2CAP];
432 	if (hp && hp->encrypt_cfm)
433 		hp->encrypt_cfm(conn, status);
434 
435 	hp = hci_proto[HCI_PROTO_SCO];
436 	if (hp && hp->encrypt_cfm)
437 		hp->encrypt_cfm(conn, status);
438 }
439 
440 int hci_register_proto(struct hci_proto *hproto);
441 int hci_unregister_proto(struct hci_proto *hproto);
442 int hci_register_notifier(struct notifier_block *nb);
443 int hci_unregister_notifier(struct notifier_block *nb);
444 
445 int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void *param);
446 int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
447 int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
448 
449 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf);
450 
451 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
452 
453 /* ----- HCI Sockets ----- */
454 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
455 
456 /* HCI info for socket */
457 #define hci_pi(sk)	((struct hci_pinfo *) &sk->tp_pinfo)
458 struct hci_pinfo {
459 	struct hci_dev 	  *hdev;
460 	struct hci_filter filter;
461 	__u32             cmsg_mask;
462 };
463 
464 /* HCI security filter */
465 #define HCI_SFLT_MAX_OGF  5
466 
467 struct hci_sec_filter {
468 	__u32 type_mask;
469 	__u32 event_mask[2];
470 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
471 };
472 
473 /* ----- HCI requests ----- */
474 #define HCI_REQ_DONE	  0
475 #define HCI_REQ_PEND	  1
476 #define HCI_REQ_CANCELED  2
477 
478 #define hci_req_lock(d)		down(&d->req_lock)
479 #define hci_req_unlock(d)	up(&d->req_lock)
480 
481 void hci_req_complete(struct hci_dev *hdev, int result);
482 void hci_req_cancel(struct hci_dev *hdev, int err);
483 
484 #endif /* __HCI_CORE_H */
485