blob: 441dadbf6a89f3a47444b463d5f6b2c5bf35017a [file] [log] [blame]
Ron Shaffer04fafe42010-05-28 11:53:45 -04001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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
Ron Shaffer04fafe42010-05-28 11:53:45 -040015 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
Ron Shaffer04fafe42010-05-28 11:53:45 -040020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25#ifndef __HCI_CORE_H
26#define __HCI_CORE_H
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/bluetooth/hci.h>
29
30/* HCI upper protocols */
31#define HCI_PROTO_L2CAP 0
32#define HCI_PROTO_SCO 1
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* HCI Core structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct inquiry_data {
36 bdaddr_t bdaddr;
37 __u8 pscan_rep_mode;
38 __u8 pscan_period_mode;
39 __u8 pscan_mode;
40 __u8 dev_class[3];
Marcel Holtmann1ebb9252005-11-08 09:57:21 -080041 __le16 clock_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 __s8 rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +020043 __u8 ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46struct inquiry_entry {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020047 struct inquiry_entry *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 __u32 timestamp;
49 struct inquiry_data data;
50};
51
52struct inquiry_cache {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020053 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 __u32 timestamp;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020055 struct inquiry_entry *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58struct hci_conn_hash {
59 struct list_head list;
60 spinlock_t lock;
61 unsigned int acl_num;
62 unsigned int sco_num;
Ville Tervofcd89c02011-02-10 22:38:47 -030063 unsigned int le_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Johan Hedbergf0358562010-05-18 13:20:32 +020066struct bdaddr_list {
67 struct list_head list;
68 bdaddr_t bdaddr;
69};
Johan Hedberg2aeb9a12011-01-04 12:08:51 +020070
71struct bt_uuid {
72 struct list_head list;
73 u8 uuid[16];
Johan Hedberg1aff6f02011-01-13 21:56:52 +020074 u8 svc_hint;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +020075};
76
Johan Hedberg55ed8ca12011-01-17 14:41:05 +020077struct link_key {
78 struct list_head list;
79 bdaddr_t bdaddr;
80 u8 type;
81 u8 val[16];
82 u8 pin_len;
83};
84
Suraj Sumangalacd4c5392010-07-14 13:02:16 +053085#define NUM_REASSEMBLY 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct hci_dev {
87 struct list_head list;
88 spinlock_t lock;
89 atomic_t refcnt;
90
91 char name[8];
92 unsigned long flags;
93 __u16 id;
Marcel Holtmannc13854c2010-02-08 15:27:07 +010094 __u8 bus;
Marcel Holtmann943da252010-02-13 02:28:41 +010095 __u8 dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 bdaddr_t bdaddr;
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097 __u8 dev_name[248];
98 __u8 dev_class[3];
Johan Hedberg1aff6f02011-01-13 21:56:52 +020099 __u8 major_class;
100 __u8 minor_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 __u8 features[8];
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 __u8 commands[64];
Marcel Holtmann333140b2008-07-14 20:13:48 +0200103 __u8 ssp_mode;
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200104 __u8 hci_ver;
105 __u16 hci_rev;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200106 __u8 lmp_ver;
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200107 __u16 manufacturer;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200108 __le16 lmp_subver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 __u16 voice_setting;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200110 __u8 io_capability;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 __u16 pkt_type;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200113 __u16 esco_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 __u16 link_policy;
115 __u16 link_mode;
116
Marcel Holtmann04837f62006-07-03 10:02:33 +0200117 __u32 idle_timeout;
118 __u16 sniff_min_interval;
119 __u16 sniff_max_interval;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long quirks;
122
123 atomic_t cmd_cnt;
124 unsigned int acl_cnt;
125 unsigned int sco_cnt;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300126 unsigned int le_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 unsigned int acl_mtu;
129 unsigned int sco_mtu;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300130 unsigned int le_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned int acl_pkts;
132 unsigned int sco_pkts;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300133 unsigned int le_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long acl_last_tx;
136 unsigned long sco_last_tx;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300137 unsigned long le_last_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100139 struct workqueue_struct *workqueue;
140
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200141 struct work_struct power_on;
142 struct work_struct power_off;
143 struct timer_list off_timer;
144
Ville Tervo6bd32322011-02-16 16:32:41 +0200145 struct timer_list cmd_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct tasklet_struct cmd_task;
147 struct tasklet_struct rx_task;
148 struct tasklet_struct tx_task;
149
150 struct sk_buff_head rx_q;
151 struct sk_buff_head raw_q;
152 struct sk_buff_head cmd_q;
153
154 struct sk_buff *sent_cmd;
Suraj Sumangalacd4c5392010-07-14 13:02:16 +0530155 struct sk_buff *reassembly[NUM_REASSEMBLY];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +0000157 struct mutex req_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 wait_queue_head_t req_wait_q;
159 __u32 req_status;
160 __u32 req_result;
Johan Hedberga5040ef2011-01-10 13:28:59 +0200161
162 __u16 init_last_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 struct inquiry_cache inq_cache;
165 struct hci_conn_hash conn_hash;
David Millerea4bd8b2010-07-30 21:54:49 -0700166 struct list_head blacklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200168 struct list_head uuids;
169
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200170 struct list_head link_keys;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct hci_dev_stats stat;
173
174 struct sk_buff_head driver_init;
175
176 void *driver_data;
177 void *core_data;
178
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200179 atomic_t promisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Marcel Holtmannca325f62010-02-08 16:22:31 +0100181 struct dentry *debugfs;
182
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200183 struct device *parent;
184 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200186 struct rfkill *rfkill;
187
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200188 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 int (*open)(struct hci_dev *hdev);
191 int (*close)(struct hci_dev *hdev);
192 int (*flush)(struct hci_dev *hdev);
193 int (*send)(struct sk_buff *skb);
194 void (*destruct)(struct hci_dev *hdev);
195 void (*notify)(struct hci_dev *hdev, unsigned int evt);
196 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
197};
198
199struct hci_conn {
200 struct list_head list;
201
Szymon Jancadc42662011-02-17 16:42:00 +0100202 atomic_t refcnt;
203 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Szymon Jancadc42662011-02-17 16:42:00 +0100205 bdaddr_t dst;
206 __u16 handle;
207 __u16 state;
208 __u8 mode;
209 __u8 type;
210 __u8 out;
211 __u8 attempt;
212 __u8 dev_class[3];
213 __u8 features[8];
214 __u8 ssp_mode;
215 __u16 interval;
216 __u16 pkt_type;
217 __u16 link_policy;
218 __u32 link_mode;
219 __u8 auth_type;
220 __u8 sec_level;
221 __u8 pending_sec_level;
222 __u8 pin_length;
223 __u8 io_capability;
224 __u8 power_save;
225 __u16 disc_timeout;
226 unsigned long pend;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200227
Johan Hedberg03b555e2011-01-04 15:40:05 +0200228 __u8 remote_cap;
229 __u8 remote_oob;
230 __u8 remote_auth;
231
Szymon Jancadc42662011-02-17 16:42:00 +0100232 unsigned int sent;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct sk_buff_head data_q;
235
Marcel Holtmann04837f62006-07-03 10:02:33 +0200236 struct timer_list disc_timer;
237 struct timer_list idle_timer;
238
Roger Quadrosf3784d82009-04-23 14:50:54 +0300239 struct work_struct work_add;
240 struct work_struct work_del;
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200241
242 struct device dev;
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700243 atomic_t devref;
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct hci_dev *hdev;
246 void *l2cap_data;
247 void *sco_data;
248 void *priv;
249
250 struct hci_conn *link;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300251
252 void (*connect_cfm_cb) (struct hci_conn *conn, u8 status);
253 void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
254 void (*disconn_cfm_cb) (struct hci_conn *conn, u8 reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
257extern struct hci_proto *hci_proto[];
258extern struct list_head hci_dev_list;
259extern struct list_head hci_cb_list;
260extern rwlock_t hci_dev_list_lock;
261extern rwlock_t hci_cb_list_lock;
262
263/* ----- Inquiry cache ----- */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200264#define INQUIRY_CACHE_AGE_MAX (HZ*30) /* 30 seconds */
265#define INQUIRY_ENTRY_AGE_MAX (HZ*60) /* 60 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267#define inquiry_cache_lock(c) spin_lock(&c->lock)
268#define inquiry_cache_unlock(c) spin_unlock(&c->lock)
269#define inquiry_cache_lock_bh(c) spin_lock_bh(&c->lock)
270#define inquiry_cache_unlock_bh(c) spin_unlock_bh(&c->lock)
271
272static inline void inquiry_cache_init(struct hci_dev *hdev)
273{
274 struct inquiry_cache *c = &hdev->inq_cache;
275 spin_lock_init(&c->lock);
276 c->list = NULL;
277}
278
279static inline int inquiry_cache_empty(struct hci_dev *hdev)
280{
281 struct inquiry_cache *c = &hdev->inq_cache;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000282 return c->list == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static inline long inquiry_cache_age(struct hci_dev *hdev)
286{
287 struct inquiry_cache *c = &hdev->inq_cache;
288 return jiffies - c->timestamp;
289}
290
291static inline long inquiry_entry_age(struct inquiry_entry *e)
292{
293 return jiffies - e->timestamp;
294}
295
296struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
297void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data);
298
299/* ----- HCI Connections ----- */
300enum {
301 HCI_CONN_AUTH_PEND,
302 HCI_CONN_ENCRYPT_PEND,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200303 HCI_CONN_RSWITCH_PEND,
304 HCI_CONN_MODE_CHANGE_PEND,
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400305 HCI_CONN_SCO_SETUP_PEND,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
308static inline void hci_conn_hash_init(struct hci_dev *hdev)
309{
310 struct hci_conn_hash *h = &hdev->conn_hash;
311 INIT_LIST_HEAD(&h->list);
312 spin_lock_init(&h->lock);
313 h->acl_num = 0;
314 h->sco_num = 0;
315}
316
317static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
318{
319 struct hci_conn_hash *h = &hdev->conn_hash;
320 list_add(&c->list, &h->list);
Ville Tervofcd89c02011-02-10 22:38:47 -0300321 switch (c->type) {
322 case ACL_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 h->acl_num++;
Ville Tervofcd89c02011-02-10 22:38:47 -0300324 break;
325 case LE_LINK:
326 h->le_num++;
327 break;
328 case SCO_LINK:
329 case ESCO_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 h->sco_num++;
Ville Tervofcd89c02011-02-10 22:38:47 -0300331 break;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
336{
337 struct hci_conn_hash *h = &hdev->conn_hash;
338 list_del(&c->list);
Ville Tervofcd89c02011-02-10 22:38:47 -0300339 switch (c->type) {
340 case ACL_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 h->acl_num--;
Ville Tervofcd89c02011-02-10 22:38:47 -0300342 break;
343 case LE_LINK:
344 h->le_num--;
345 break;
346 case SCO_LINK:
347 case ESCO_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 h->sco_num--;
Ville Tervofcd89c02011-02-10 22:38:47 -0300349 break;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100354 __u16 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct hci_conn_hash *h = &hdev->conn_hash;
357 struct list_head *p;
358 struct hci_conn *c;
359
360 list_for_each(p, &h->list) {
361 c = list_entry(p, struct hci_conn, list);
362 if (c->handle == handle)
363 return c;
364 }
365 return NULL;
366}
367
368static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100369 __u8 type, bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct hci_conn_hash *h = &hdev->conn_hash;
372 struct list_head *p;
373 struct hci_conn *c;
374
375 list_for_each(p, &h->list) {
376 c = list_entry(p, struct hci_conn, list);
377 if (c->type == type && !bacmp(&c->dst, ba))
378 return c;
379 }
380 return NULL;
381}
382
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200383static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100384 __u8 type, __u16 state)
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200385{
386 struct hci_conn_hash *h = &hdev->conn_hash;
387 struct list_head *p;
388 struct hci_conn *c;
389
390 list_for_each(p, &h->list) {
391 c = list_entry(p, struct hci_conn, list);
392 if (c->type == type && c->state == state)
393 return c;
394 }
395 return NULL;
396}
397
398void hci_acl_connect(struct hci_conn *conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399void hci_acl_disconn(struct hci_conn *conn, __u8 reason);
400void hci_add_sco(struct hci_conn *conn, __u16 handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200401void hci_setup_sync(struct hci_conn *conn, __u16 handle);
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400402void hci_sco_setup(struct hci_conn *conn, __u8 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200405int hci_conn_del(struct hci_conn *conn);
406void hci_conn_hash_flush(struct hci_dev *hdev);
407void hci_conn_check_pending(struct hci_dev *hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100409struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200410int hci_conn_check_link_mode(struct hci_conn *conn);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100411int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412int hci_conn_change_link_key(struct hci_conn *conn);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100413int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Marcel Holtmann04837f62006-07-03 10:02:33 +0200415void hci_conn_enter_active_mode(struct hci_conn *conn);
416void hci_conn_enter_sniff_mode(struct hci_conn *conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700418void hci_conn_hold_device(struct hci_conn *conn);
419void hci_conn_put_device(struct hci_conn *conn);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static inline void hci_conn_hold(struct hci_conn *conn)
422{
423 atomic_inc(&conn->refcnt);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200424 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static inline void hci_conn_put(struct hci_conn *conn)
428{
429 if (atomic_dec_and_test(&conn->refcnt)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +0200430 unsigned long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (conn->type == ACL_LINK) {
Marcel Holtmann04837f62006-07-03 10:02:33 +0200432 del_timer(&conn->idle_timer);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200433 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200434 timeo = msecs_to_jiffies(conn->disc_timeout);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200435 if (!conn->out)
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200436 timeo *= 2;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200437 } else
438 timeo = msecs_to_jiffies(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 } else
Marcel Holtmann04837f62006-07-03 10:02:33 +0200440 timeo = msecs_to_jiffies(10);
441 mod_timer(&conn->disc_timer, jiffies + timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* ----- HCI Devices ----- */
446static inline void __hci_dev_put(struct hci_dev *d)
447{
448 if (atomic_dec_and_test(&d->refcnt))
449 d->destruct(d);
450}
451
452static inline void hci_dev_put(struct hci_dev *d)
Ron Shaffer04fafe42010-05-28 11:53:45 -0400453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 __hci_dev_put(d);
455 module_put(d->owner);
456}
457
458static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
459{
460 atomic_inc(&d->refcnt);
461 return d;
462}
463
464static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
465{
466 if (try_module_get(d->owner))
467 return __hci_dev_hold(d);
468 return NULL;
469}
470
471#define hci_dev_lock(d) spin_lock(&d->lock)
472#define hci_dev_unlock(d) spin_unlock(&d->lock)
473#define hci_dev_lock_bh(d) spin_lock_bh(&d->lock)
474#define hci_dev_unlock_bh(d) spin_unlock_bh(&d->lock)
475
476struct hci_dev *hci_dev_get(int index);
477struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
478
479struct hci_dev *hci_alloc_dev(void);
480void hci_free_dev(struct hci_dev *hdev);
481int hci_register_dev(struct hci_dev *hdev);
482int hci_unregister_dev(struct hci_dev *hdev);
483int hci_suspend_dev(struct hci_dev *hdev);
484int hci_resume_dev(struct hci_dev *hdev);
485int hci_dev_open(__u16 dev);
486int hci_dev_close(__u16 dev);
487int hci_dev_reset(__u16 dev);
488int hci_dev_reset_stat(__u16 dev);
489int hci_dev_cmd(unsigned int cmd, void __user *arg);
490int hci_get_dev_list(void __user *arg);
491int hci_get_dev_info(void __user *arg);
492int hci_get_conn_list(void __user *arg);
493int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200494int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int hci_inquiry(void __user *arg);
496
Johan Hedbergf0358562010-05-18 13:20:32 +0200497struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
498int hci_blacklist_clear(struct hci_dev *hdev);
499
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200500int hci_uuids_clear(struct hci_dev *hdev);
501
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200502int hci_link_keys_clear(struct hci_dev *hdev);
503struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
504int hci_add_link_key(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr,
505 u8 *key, u8 type, u8 pin_len);
506int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
507
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200508void hci_del_off_timer(struct hci_dev *hdev);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
511
Marcel Holtmann76bca882009-11-18 00:40:39 +0100512int hci_recv_frame(struct sk_buff *skb);
Marcel Holtmannef222012007-07-11 06:42:04 +0200513int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
Suraj Sumangala99811512010-07-14 13:02:19 +0530514int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
Marcel Holtmannef222012007-07-11 06:42:04 +0200515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516int hci_register_sysfs(struct hci_dev *hdev);
517void hci_unregister_sysfs(struct hci_dev *hdev);
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700518void hci_conn_init_sysfs(struct hci_conn *conn);
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200519void hci_conn_add_sysfs(struct hci_conn *conn);
520void hci_conn_del_sysfs(struct hci_conn *conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200522#define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524/* ----- LMP capabilities ----- */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200525#define lmp_rswitch_capable(dev) ((dev)->features[0] & LMP_RSWITCH)
526#define lmp_encrypt_capable(dev) ((dev)->features[0] & LMP_ENCRYPT)
527#define lmp_sniff_capable(dev) ((dev)->features[0] & LMP_SNIFF)
528#define lmp_sniffsubr_capable(dev) ((dev)->features[5] & LMP_SNIFF_SUBR)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200529#define lmp_esco_capable(dev) ((dev)->features[3] & LMP_ESCO)
Marcel Holtmann769be972008-07-14 20:13:49 +0200530#define lmp_ssp_capable(dev) ((dev)->features[6] & LMP_SIMPLE_PAIR)
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200531#define lmp_no_flush_capable(dev) ((dev)->features[6] & LMP_NO_FLUSH)
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300532#define lmp_le_capable(dev) ((dev)->features[4] & LMP_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534/* ----- HCI protocols ----- */
535struct hci_proto {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100536 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned int id;
538 unsigned long flags;
539
540 void *priv;
541
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100542 int (*connect_ind) (struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int (*connect_cfm) (struct hci_conn *conn, __u8 status);
Marcel Holtmann2950f212009-02-12 14:02:50 +0100544 int (*disconn_ind) (struct hci_conn *conn);
545 int (*disconn_cfm) (struct hci_conn *conn, __u8 reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int (*recv_acldata) (struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
547 int (*recv_scodata) (struct hci_conn *conn, struct sk_buff *skb);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100548 int (*security_cfm) (struct hci_conn *conn, __u8 status, __u8 encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549};
550
551static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
552{
553 register struct hci_proto *hp;
554 int mask = 0;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 hp = hci_proto[HCI_PROTO_L2CAP];
557 if (hp && hp->connect_ind)
558 mask |= hp->connect_ind(hdev, bdaddr, type);
559
560 hp = hci_proto[HCI_PROTO_SCO];
561 if (hp && hp->connect_ind)
562 mask |= hp->connect_ind(hdev, bdaddr, type);
563
564 return mask;
565}
566
567static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
568{
569 register struct hci_proto *hp;
570
571 hp = hci_proto[HCI_PROTO_L2CAP];
572 if (hp && hp->connect_cfm)
573 hp->connect_cfm(conn, status);
574
575 hp = hci_proto[HCI_PROTO_SCO];
576 if (hp && hp->connect_cfm)
577 hp->connect_cfm(conn, status);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300578
579 if (conn->connect_cfm_cb)
580 conn->connect_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Marcel Holtmann2950f212009-02-12 14:02:50 +0100583static inline int hci_proto_disconn_ind(struct hci_conn *conn)
584{
585 register struct hci_proto *hp;
586 int reason = 0x13;
587
588 hp = hci_proto[HCI_PROTO_L2CAP];
589 if (hp && hp->disconn_ind)
590 reason = hp->disconn_ind(conn);
591
592 hp = hci_proto[HCI_PROTO_SCO];
593 if (hp && hp->disconn_ind)
594 reason = hp->disconn_ind(conn);
595
596 return reason;
597}
598
599static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 register struct hci_proto *hp;
602
603 hp = hci_proto[HCI_PROTO_L2CAP];
Marcel Holtmann2950f212009-02-12 14:02:50 +0100604 if (hp && hp->disconn_cfm)
605 hp->disconn_cfm(conn, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 hp = hci_proto[HCI_PROTO_SCO];
Marcel Holtmann2950f212009-02-12 14:02:50 +0100608 if (hp && hp->disconn_cfm)
609 hp->disconn_cfm(conn, reason);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300610
611 if (conn->disconn_cfm_cb)
612 conn->disconn_cfm_cb(conn, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
616{
617 register struct hci_proto *hp;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100618 __u8 encrypt;
619
620 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
621 return;
622
623 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 hp = hci_proto[HCI_PROTO_L2CAP];
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100626 if (hp && hp->security_cfm)
627 hp->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 hp = hci_proto[HCI_PROTO_SCO];
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100630 if (hp && hp->security_cfm)
631 hp->security_cfm(conn, status, encrypt);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300632
633 if (conn->security_cfm_cb)
634 conn->security_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Marcel Holtmann9719f8a2008-07-14 20:13:45 +0200637static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 register struct hci_proto *hp;
640
641 hp = hci_proto[HCI_PROTO_L2CAP];
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100642 if (hp && hp->security_cfm)
643 hp->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 hp = hci_proto[HCI_PROTO_SCO];
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100646 if (hp && hp->security_cfm)
647 hp->security_cfm(conn, status, encrypt);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300648
649 if (conn->security_cfm_cb)
650 conn->security_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653int hci_register_proto(struct hci_proto *hproto);
654int hci_unregister_proto(struct hci_proto *hproto);
655
656/* ----- HCI callbacks ----- */
657struct hci_cb {
658 struct list_head list;
659
660 char *name;
661
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100662 void (*security_cfm) (struct hci_conn *conn, __u8 status, __u8 encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 void (*key_change_cfm) (struct hci_conn *conn, __u8 status);
664 void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
665};
666
667static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
668{
669 struct list_head *p;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100670 __u8 encrypt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 hci_proto_auth_cfm(conn, status);
673
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100674 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
675 return;
676
677 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 read_lock_bh(&hci_cb_list_lock);
680 list_for_each(p, &hci_cb_list) {
681 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100682 if (cb->security_cfm)
683 cb->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 read_unlock_bh(&hci_cb_list_lock);
686}
687
688static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
689{
690 struct list_head *p;
691
Marcel Holtmann435fef22009-02-09 03:55:28 +0100692 if (conn->sec_level == BT_SECURITY_SDP)
693 conn->sec_level = BT_SECURITY_LOW;
694
Marcel Holtmann9719f8a2008-07-14 20:13:45 +0200695 hci_proto_encrypt_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 read_lock_bh(&hci_cb_list_lock);
698 list_for_each(p, &hci_cb_list) {
699 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100700 if (cb->security_cfm)
701 cb->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 read_unlock_bh(&hci_cb_list_lock);
704}
705
706static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
707{
708 struct list_head *p;
709
710 read_lock_bh(&hci_cb_list_lock);
711 list_for_each(p, &hci_cb_list) {
712 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
713 if (cb->key_change_cfm)
714 cb->key_change_cfm(conn, status);
715 }
716 read_unlock_bh(&hci_cb_list_lock);
717}
718
719static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status, __u8 role)
720{
721 struct list_head *p;
722
723 read_lock_bh(&hci_cb_list_lock);
724 list_for_each(p, &hci_cb_list) {
725 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
726 if (cb->role_switch_cfm)
727 cb->role_switch_cfm(conn, status, role);
728 }
729 read_unlock_bh(&hci_cb_list_lock);
730}
731
732int hci_register_cb(struct hci_cb *hcb);
733int hci_unregister_cb(struct hci_cb *hcb);
734
735int hci_register_notifier(struct notifier_block *nb);
736int hci_unregister_notifier(struct notifier_block *nb);
737
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200738int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300739void hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300740void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200742void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
745
746/* ----- HCI Sockets ----- */
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200747void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb,
748 struct sock *skip_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Johan Hedberg03811012010-12-08 00:21:06 +0200750/* Management interface */
751int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
Johan Hedbergc71e97b2010-12-13 21:07:07 +0200752int mgmt_index_added(u16 index);
753int mgmt_index_removed(u16 index);
Johan Hedberg5add6af2010-12-16 10:00:37 +0200754int mgmt_powered(u16 index, u8 powered);
Johan Hedberg73f22f62010-12-29 16:00:25 +0200755int mgmt_discoverable(u16 index, u8 discoverable);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200756int mgmt_connectable(u16 index, u8 connectable);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200757int mgmt_new_key(u16 index, struct link_key *key, u8 old_key_type);
Johan Hedbergf7520542011-01-20 12:34:39 +0200758int mgmt_connected(u16 index, bdaddr_t *bdaddr);
759int mgmt_disconnected(u16 index, bdaddr_t *bdaddr);
Johan Hedberg8962ee72011-01-20 12:40:27 +0200760int mgmt_disconnect_failed(u16 index);
Johan Hedberg17d5c042011-01-22 06:09:08 +0200761int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200762int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr);
763int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
764int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300765int mgmt_user_confirm_request(u16 index, bdaddr_t *bdaddr, __le32 value);
766int mgmt_user_confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
767int mgmt_user_confirm_neg_reply_complete(u16 index, bdaddr_t *bdaddr,
768 u8 status);
Johan Hedberg2a611692011-02-19 12:06:00 -0300769int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status);
Johan Hedberg03811012010-12-08 00:21:06 +0200770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/* HCI info for socket */
772#define hci_pi(sk) ((struct hci_pinfo *) sk)
773
774struct hci_pinfo {
775 struct bt_sock bt;
776 struct hci_dev *hdev;
777 struct hci_filter filter;
778 __u32 cmsg_mask;
Johan Hedbergc02178d2010-12-08 00:21:05 +0200779 unsigned short channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780};
781
782/* HCI security filter */
783#define HCI_SFLT_MAX_OGF 5
784
785struct hci_sec_filter {
786 __u32 type_mask;
787 __u32 event_mask[2];
788 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
789};
790
791/* ----- HCI requests ----- */
792#define HCI_REQ_DONE 0
793#define HCI_REQ_PEND 1
794#define HCI_REQ_CANCELED 2
795
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +0000796#define hci_req_lock(d) mutex_lock(&d->req_lock)
797#define hci_req_unlock(d) mutex_unlock(&d->req_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Johan Hedberg23bb5762010-12-21 23:01:27 +0200799void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200801void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
802 u16 latency, u16 to_multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#endif /* __HCI_CORE_H */