Bluetooth: Add H/W TX timeout error MGMT event
[platform/kernel/linux-starfive.git] / include / net / bluetooth / hci_core.h
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
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 #ifndef __HCI_CORE_H
26 #define __HCI_CORE_H
27
28 #include <linux/idr.h>
29 #include <linux/leds.h>
30 #include <linux/rculist.h>
31
32 #include <net/bluetooth/hci.h>
33 #include <net/bluetooth/hci_sync.h>
34 #include <net/bluetooth/hci_sock.h>
35
36 /* HCI priority */
37 #define HCI_PRIO_MAX    7
38
39 /* HCI maximum id value */
40 #define HCI_MAX_ID 10000
41
42 /* HCI Core structures */
43 struct inquiry_data {
44         bdaddr_t        bdaddr;
45         __u8            pscan_rep_mode;
46         __u8            pscan_period_mode;
47         __u8            pscan_mode;
48         __u8            dev_class[3];
49         __le16          clock_offset;
50         __s8            rssi;
51         __u8            ssp_mode;
52 };
53
54 struct inquiry_entry {
55         struct list_head        all;            /* inq_cache.all */
56         struct list_head        list;           /* unknown or resolve */
57         enum {
58                 NAME_NOT_KNOWN,
59                 NAME_NEEDED,
60                 NAME_PENDING,
61                 NAME_KNOWN,
62         } name_state;
63         __u32                   timestamp;
64         struct inquiry_data     data;
65 };
66
67 struct discovery_state {
68         int                     type;
69         enum {
70                 DISCOVERY_STOPPED,
71                 DISCOVERY_STARTING,
72                 DISCOVERY_FINDING,
73                 DISCOVERY_RESOLVING,
74                 DISCOVERY_STOPPING,
75         } state;
76         struct list_head        all;    /* All devices found during inquiry */
77         struct list_head        unknown;        /* Name state not known */
78         struct list_head        resolve;        /* Name needs to be resolved */
79         __u32                   timestamp;
80         bdaddr_t                last_adv_addr;
81         u8                      last_adv_addr_type;
82         s8                      last_adv_rssi;
83         u32                     last_adv_flags;
84         u8                      last_adv_data[HCI_MAX_AD_LENGTH];
85         u8                      last_adv_data_len;
86         bool                    report_invalid_rssi;
87         bool                    result_filtering;
88         bool                    limited;
89         s8                      rssi;
90         u16                     uuid_count;
91         u8                      (*uuids)[16];
92         unsigned long           scan_start;
93         unsigned long           scan_duration;
94         unsigned long           name_resolve_timeout;
95 };
96
97 #define SUSPEND_NOTIFIER_TIMEOUT        msecs_to_jiffies(2000) /* 2 seconds */
98
99 enum suspend_tasks {
100         SUSPEND_PAUSE_DISCOVERY,
101         SUSPEND_UNPAUSE_DISCOVERY,
102
103         SUSPEND_PAUSE_ADVERTISING,
104         SUSPEND_UNPAUSE_ADVERTISING,
105
106         SUSPEND_SCAN_DISABLE,
107         SUSPEND_SCAN_ENABLE,
108         SUSPEND_DISCONNECTING,
109
110         SUSPEND_POWERING_DOWN,
111
112         SUSPEND_PREPARE_NOTIFIER,
113
114         SUSPEND_SET_ADV_FILTER,
115         __SUSPEND_NUM_TASKS
116 };
117
118 enum suspended_state {
119         BT_RUNNING = 0,
120         BT_SUSPEND_DISCONNECT,
121         BT_SUSPEND_CONFIGURE_WAKE,
122 };
123
124 struct hci_conn_hash {
125         struct list_head list;
126         unsigned int     acl_num;
127         unsigned int     amp_num;
128         unsigned int     sco_num;
129         unsigned int     iso_num;
130         unsigned int     le_num;
131         unsigned int     le_num_peripheral;
132 };
133
134 struct bdaddr_list {
135         struct list_head list;
136         bdaddr_t bdaddr;
137         u8 bdaddr_type;
138 };
139
140 struct codec_list {
141         struct list_head list;
142         u8      id;
143         __u16   cid;
144         __u16   vid;
145         u8      transport;
146         u8      num_caps;
147         u32     len;
148         struct hci_codec_caps caps[];
149 };
150
151 struct bdaddr_list_with_irk {
152         struct list_head list;
153         bdaddr_t bdaddr;
154         u8 bdaddr_type;
155         u8 peer_irk[16];
156         u8 local_irk[16];
157 };
158
159 /* Bitmask of connection flags */
160 enum hci_conn_flags {
161         HCI_CONN_FLAG_REMOTE_WAKEUP = 1,
162         HCI_CONN_FLAG_DEVICE_PRIVACY = 2,
163 };
164 typedef u8 hci_conn_flags_t;
165
166 struct bdaddr_list_with_flags {
167         struct list_head list;
168         bdaddr_t bdaddr;
169         u8 bdaddr_type;
170         hci_conn_flags_t flags;
171 };
172
173 struct bt_uuid {
174         struct list_head list;
175         u8 uuid[16];
176         u8 size;
177         u8 svc_hint;
178 };
179
180 struct blocked_key {
181         struct list_head list;
182         struct rcu_head rcu;
183         u8 type;
184         u8 val[16];
185 };
186
187 struct smp_csrk {
188         bdaddr_t bdaddr;
189         u8 bdaddr_type;
190         u8 type;
191         u8 val[16];
192 };
193
194 struct smp_ltk {
195         struct list_head list;
196         struct rcu_head rcu;
197         bdaddr_t bdaddr;
198         u8 bdaddr_type;
199         u8 authenticated;
200         u8 type;
201         u8 enc_size;
202         __le16 ediv;
203         __le64 rand;
204         u8 val[16];
205 };
206
207 struct smp_irk {
208         struct list_head list;
209         struct rcu_head rcu;
210         bdaddr_t rpa;
211         bdaddr_t bdaddr;
212         u8 addr_type;
213         u8 val[16];
214 };
215
216 struct link_key {
217         struct list_head list;
218         struct rcu_head rcu;
219         bdaddr_t bdaddr;
220         u8 type;
221         u8 val[HCI_LINK_KEY_SIZE];
222         u8 pin_len;
223 };
224
225 struct oob_data {
226         struct list_head list;
227         bdaddr_t bdaddr;
228         u8 bdaddr_type;
229         u8 present;
230         u8 hash192[16];
231         u8 rand192[16];
232         u8 hash256[16];
233         u8 rand256[16];
234 };
235
236 struct adv_info {
237         struct list_head list;
238         bool    enabled;
239         bool    pending;
240         bool    periodic;
241         __u8    mesh;
242         __u8    instance;
243         __u32   flags;
244         __u16   timeout;
245         __u16   remaining_time;
246         __u16   duration;
247         __u16   adv_data_len;
248         __u8    adv_data[HCI_MAX_EXT_AD_LENGTH];
249         bool    adv_data_changed;
250         __u16   scan_rsp_len;
251         __u8    scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
252         bool    scan_rsp_changed;
253         __u16   per_adv_data_len;
254         __u8    per_adv_data[HCI_MAX_PER_AD_LENGTH];
255         __s8    tx_power;
256         __u32   min_interval;
257         __u32   max_interval;
258         bdaddr_t        random_addr;
259         bool            rpa_expired;
260         struct delayed_work     rpa_expired_cb;
261 };
262
263 #define HCI_MAX_ADV_INSTANCES           5
264 #define HCI_DEFAULT_ADV_DURATION        2
265
266 #define HCI_ADV_TX_POWER_NO_PREFERENCE 0x7F
267
268 #define DATA_CMP(_d1, _l1, _d2, _l2) \
269         (_l1 == _l2 ? memcmp(_d1, _d2, _l1) : _l1 - _l2)
270
271 #define ADV_DATA_CMP(_adv, _data, _len) \
272         DATA_CMP((_adv)->adv_data, (_adv)->adv_data_len, _data, _len)
273
274 #define SCAN_RSP_CMP(_adv, _data, _len) \
275         DATA_CMP((_adv)->scan_rsp_data, (_adv)->scan_rsp_len, _data, _len)
276
277 struct monitored_device {
278         struct list_head list;
279
280         bdaddr_t bdaddr;
281         __u8     addr_type;
282         __u16    handle;
283         bool     notified;
284 };
285
286 struct adv_pattern {
287         struct list_head list;
288         __u8 ad_type;
289         __u8 offset;
290         __u8 length;
291         __u8 value[HCI_MAX_AD_LENGTH];
292 };
293
294 struct adv_rssi_thresholds {
295         __s8 low_threshold;
296         __s8 high_threshold;
297         __u16 low_threshold_timeout;
298         __u16 high_threshold_timeout;
299         __u8 sampling_period;
300 };
301
302 struct adv_monitor {
303         struct list_head patterns;
304         struct adv_rssi_thresholds rssi;
305         __u16           handle;
306
307         enum {
308                 ADV_MONITOR_STATE_NOT_REGISTERED,
309                 ADV_MONITOR_STATE_REGISTERED,
310                 ADV_MONITOR_STATE_OFFLOADED
311         } state;
312 };
313
314 #define HCI_MIN_ADV_MONITOR_HANDLE              1
315 #define HCI_MAX_ADV_MONITOR_NUM_HANDLES         32
316 #define HCI_MAX_ADV_MONITOR_NUM_PATTERNS        16
317 #define HCI_ADV_MONITOR_EXT_NONE                1
318 #define HCI_ADV_MONITOR_EXT_MSFT                2
319
320 #define HCI_MAX_SHORT_NAME_LENGTH       10
321
322 #define HCI_CONN_HANDLE_UNSET           0xffff
323 #define HCI_CONN_HANDLE_MAX             0x0eff
324
325 /* Min encryption key size to match with SMP */
326 #define HCI_MIN_ENC_KEY_SIZE            7
327
328 /* Default LE RPA expiry time, 15 minutes */
329 #define HCI_DEFAULT_RPA_TIMEOUT         (15 * 60)
330
331 /* Default min/max age of connection information (1s/3s) */
332 #define DEFAULT_CONN_INFO_MIN_AGE       1000
333 #define DEFAULT_CONN_INFO_MAX_AGE       3000
334 /* Default authenticated payload timeout 30s */
335 #define DEFAULT_AUTH_PAYLOAD_TIMEOUT   0x0bb8
336
337 struct amp_assoc {
338         __u16   len;
339         __u16   offset;
340         __u16   rem_len;
341         __u16   len_so_far;
342         __u8    data[HCI_MAX_AMP_ASSOC_SIZE];
343 };
344
345 #define HCI_MAX_PAGES   3
346
347 #ifdef TIZEN_BT
348 #define HCI_MAX_EIR_MANUFACTURER_DATA_LENGTH    100
349 #endif
350
351 struct hci_dev {
352         struct list_head list;
353         struct mutex    lock;
354
355         char            name[8];
356         unsigned long   flags;
357         __u16           id;
358         __u8            bus;
359         __u8            dev_type;
360         bdaddr_t        bdaddr;
361         bdaddr_t        setup_addr;
362         bdaddr_t        public_addr;
363         bdaddr_t        random_addr;
364         bdaddr_t        static_addr;
365         __u8            adv_addr_type;
366         __u8            dev_name[HCI_MAX_NAME_LENGTH];
367         __u8            short_name[HCI_MAX_SHORT_NAME_LENGTH];
368         __u8            eir[HCI_MAX_EIR_LENGTH];
369         __u16           appearance;
370         __u8            dev_class[3];
371         __u8            major_class;
372         __u8            minor_class;
373         __u8            max_page;
374         __u8            features[HCI_MAX_PAGES][8];
375         __u8            le_features[8];
376         __u8            le_accept_list_size;
377         __u8            le_resolv_list_size;
378         __u8            le_num_of_adv_sets;
379         __u8            le_states[8];
380         __u8            mesh_ad_types[16];
381         __u8            mesh_send_ref;
382         __u8            commands[64];
383         __u8            hci_ver;
384         __u16           hci_rev;
385         __u8            lmp_ver;
386         __u16           manufacturer;
387         __u16           lmp_subver;
388         __u16           voice_setting;
389         __u8            num_iac;
390         __u16           stored_max_keys;
391         __u16           stored_num_keys;
392         __u8            io_capability;
393         __s8            inq_tx_power;
394         __u8            err_data_reporting;
395         __u16           page_scan_interval;
396         __u16           page_scan_window;
397         __u8            page_scan_type;
398         __u8            le_adv_channel_map;
399         __u16           le_adv_min_interval;
400         __u16           le_adv_max_interval;
401         __u8            le_scan_type;
402         __u16           le_scan_interval;
403         __u16           le_scan_window;
404         __u16           le_scan_int_suspend;
405         __u16           le_scan_window_suspend;
406         __u16           le_scan_int_discovery;
407         __u16           le_scan_window_discovery;
408         __u16           le_scan_int_adv_monitor;
409         __u16           le_scan_window_adv_monitor;
410         __u16           le_scan_int_connect;
411         __u16           le_scan_window_connect;
412         __u16           le_conn_min_interval;
413         __u16           le_conn_max_interval;
414         __u16           le_conn_latency;
415         __u16           le_supv_timeout;
416         __u16           le_def_tx_len;
417         __u16           le_def_tx_time;
418         __u16           le_max_tx_len;
419         __u16           le_max_tx_time;
420         __u16           le_max_rx_len;
421         __u16           le_max_rx_time;
422         __u8            le_max_key_size;
423         __u8            le_min_key_size;
424         __u16           discov_interleaved_timeout;
425         __u16           conn_info_min_age;
426         __u16           conn_info_max_age;
427         __u16           auth_payload_timeout;
428         __u8            min_enc_key_size;
429         __u8            max_enc_key_size;
430         __u8            pairing_opts;
431         __u8            ssp_debug_mode;
432         __u8            hw_error_code;
433         __u32           clock;
434         __u16           advmon_allowlist_duration;
435         __u16           advmon_no_filter_duration;
436         __u8            enable_advmon_interleave_scan;
437
438         __u16           devid_source;
439         __u16           devid_vendor;
440         __u16           devid_product;
441         __u16           devid_version;
442
443         __u8            def_page_scan_type;
444         __u16           def_page_scan_int;
445         __u16           def_page_scan_window;
446         __u8            def_inq_scan_type;
447         __u16           def_inq_scan_int;
448         __u16           def_inq_scan_window;
449         __u16           def_br_lsto;
450         __u16           def_page_timeout;
451         __u16           def_multi_adv_rotation_duration;
452         __u16           def_le_autoconnect_timeout;
453         __s8            min_le_tx_power;
454         __s8            max_le_tx_power;
455
456         __u16           pkt_type;
457         __u16           esco_type;
458         __u16           link_policy;
459         __u16           link_mode;
460
461         __u32           idle_timeout;
462         __u16           sniff_min_interval;
463         __u16           sniff_max_interval;
464
465         __u8            amp_status;
466         __u32           amp_total_bw;
467         __u32           amp_max_bw;
468         __u32           amp_min_latency;
469         __u32           amp_max_pdu;
470         __u8            amp_type;
471         __u16           amp_pal_cap;
472         __u16           amp_assoc_size;
473         __u32           amp_max_flush_to;
474         __u32           amp_be_flush_to;
475
476         struct amp_assoc        loc_assoc;
477
478         __u8            flow_ctl_mode;
479
480         unsigned int    auto_accept_delay;
481
482         unsigned long   quirks;
483
484         atomic_t        cmd_cnt;
485         unsigned int    acl_cnt;
486         unsigned int    sco_cnt;
487         unsigned int    le_cnt;
488         unsigned int    iso_cnt;
489
490         unsigned int    acl_mtu;
491         unsigned int    sco_mtu;
492         unsigned int    le_mtu;
493         unsigned int    iso_mtu;
494         unsigned int    acl_pkts;
495         unsigned int    sco_pkts;
496         unsigned int    le_pkts;
497         unsigned int    iso_pkts;
498
499         __u16           block_len;
500         __u16           block_mtu;
501         __u16           num_blocks;
502         __u16           block_cnt;
503
504         unsigned long   acl_last_tx;
505         unsigned long   sco_last_tx;
506         unsigned long   le_last_tx;
507
508         __u8            le_tx_def_phys;
509         __u8            le_rx_def_phys;
510
511         struct workqueue_struct *workqueue;
512         struct workqueue_struct *req_workqueue;
513
514         struct work_struct      power_on;
515         struct delayed_work     power_off;
516         struct work_struct      error_reset;
517         struct work_struct      cmd_sync_work;
518         struct list_head        cmd_sync_work_list;
519         struct mutex            cmd_sync_work_lock;
520         struct mutex            unregister_lock;
521         struct work_struct      cmd_sync_cancel_work;
522         struct work_struct      reenable_adv_work;
523
524         __u16                   discov_timeout;
525         struct delayed_work     discov_off;
526
527         struct delayed_work     service_cache;
528
529         struct delayed_work     cmd_timer;
530         struct delayed_work     ncmd_timer;
531
532         struct work_struct      rx_work;
533         struct work_struct      cmd_work;
534         struct work_struct      tx_work;
535
536         struct delayed_work     le_scan_disable;
537         struct delayed_work     le_scan_restart;
538
539         struct sk_buff_head     rx_q;
540         struct sk_buff_head     raw_q;
541         struct sk_buff_head     cmd_q;
542
543         struct sk_buff          *sent_cmd;
544         struct sk_buff          *recv_event;
545
546         struct mutex            req_lock;
547         wait_queue_head_t       req_wait_q;
548         __u32                   req_status;
549         __u32                   req_result;
550         struct sk_buff          *req_skb;
551
552         void                    *smp_data;
553         void                    *smp_bredr_data;
554
555         struct discovery_state  discovery;
556
557         int                     discovery_old_state;
558         bool                    discovery_paused;
559         int                     advertising_old_state;
560         bool                    advertising_paused;
561
562         struct notifier_block   suspend_notifier;
563         enum suspended_state    suspend_state_next;
564         enum suspended_state    suspend_state;
565         bool                    scanning_paused;
566         bool                    suspended;
567         u8                      wake_reason;
568         bdaddr_t                wake_addr;
569         u8                      wake_addr_type;
570 #ifdef TIZEN_BT
571         struct discovery_state  le_discovery;
572 #endif
573
574         struct hci_conn_hash    conn_hash;
575
576         struct list_head        mesh_pending;
577         struct list_head        mgmt_pending;
578         struct list_head        reject_list;
579         struct list_head        accept_list;
580         struct list_head        uuids;
581         struct list_head        link_keys;
582         struct list_head        long_term_keys;
583         struct list_head        identity_resolving_keys;
584         struct list_head        remote_oob_data;
585         struct list_head        le_accept_list;
586         struct list_head        le_resolv_list;
587         struct list_head        le_conn_params;
588         struct list_head        pend_le_conns;
589         struct list_head        pend_le_reports;
590         struct list_head        blocked_keys;
591         struct list_head        local_codecs;
592
593         struct hci_dev_stats    stat;
594
595         atomic_t                promisc;
596
597         const char              *hw_info;
598         const char              *fw_info;
599         struct dentry           *debugfs;
600
601         struct device           dev;
602
603         struct rfkill           *rfkill;
604
605         DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS);
606         hci_conn_flags_t        conn_flags;
607
608         __s8                    adv_tx_power;
609         __u8                    adv_data[HCI_MAX_EXT_AD_LENGTH];
610         __u8                    adv_data_len;
611         __u8                    scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
612         __u8                    scan_rsp_data_len;
613         __u8                    per_adv_data[HCI_MAX_PER_AD_LENGTH];
614         __u8                    per_adv_data_len;
615
616         struct list_head        adv_instances;
617         unsigned int            adv_instance_cnt;
618         __u8                    cur_adv_instance;
619         __u16                   adv_instance_timeout;
620         struct delayed_work     adv_instance_expire;
621
622         struct idr              adv_monitors_idr;
623         unsigned int            adv_monitors_cnt;
624
625         __u8                    irk[16];
626         __u32                   rpa_timeout;
627         struct delayed_work     rpa_expired;
628         bdaddr_t                rpa;
629
630         struct delayed_work     mesh_send_done;
631
632         enum {
633                 INTERLEAVE_SCAN_NONE,
634                 INTERLEAVE_SCAN_NO_FILTER,
635                 INTERLEAVE_SCAN_ALLOWLIST
636         } interleave_scan_state;
637
638         struct delayed_work     interleave_scan;
639
640         struct list_head        monitored_devices;
641         bool                    advmon_pend_notify;
642
643 #if IS_ENABLED(CONFIG_BT_LEDS)
644         struct led_trigger      *power_led;
645 #endif
646
647 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
648         __u16                   msft_opcode;
649         void                    *msft_data;
650         bool                    msft_curve_validity;
651 #endif
652
653 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
654         bool                    aosp_capable;
655         bool                    aosp_quality_report;
656 #endif
657
658 #ifdef TIZEN_BT
659         __u8                    adv_filter_policy;
660         __u8                    adv_type;
661         __u8                    manufacturer_len;
662         __u8                    manufacturer_data[HCI_MAX_EIR_MANUFACTURER_DATA_LENGTH];
663 #endif
664
665         int (*open)(struct hci_dev *hdev);
666         int (*close)(struct hci_dev *hdev);
667         int (*flush)(struct hci_dev *hdev);
668         int (*setup)(struct hci_dev *hdev);
669         int (*shutdown)(struct hci_dev *hdev);
670         int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
671         void (*notify)(struct hci_dev *hdev, unsigned int evt);
672         void (*hw_error)(struct hci_dev *hdev, u8 code);
673         int (*post_init)(struct hci_dev *hdev);
674         int (*set_diag)(struct hci_dev *hdev, bool enable);
675         int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
676         void (*cmd_timeout)(struct hci_dev *hdev);
677         bool (*wakeup)(struct hci_dev *hdev);
678         int (*set_quality_report)(struct hci_dev *hdev, bool enable);
679         int (*get_data_path_id)(struct hci_dev *hdev, __u8 *data_path);
680         int (*get_codec_config_data)(struct hci_dev *hdev, __u8 type,
681                                      struct bt_codec *codec, __u8 *vnd_len,
682                                      __u8 **vnd_data);
683 };
684
685 #define HCI_PHY_HANDLE(handle)  (handle & 0xff)
686
687 enum conn_reasons {
688         CONN_REASON_PAIR_DEVICE,
689         CONN_REASON_L2CAP_CHAN,
690         CONN_REASON_SCO_CONNECT,
691         CONN_REASON_ISO_CONNECT,
692 };
693
694 struct hci_conn {
695         struct list_head list;
696
697         atomic_t        refcnt;
698
699         bdaddr_t        dst;
700         __u8            dst_type;
701         bdaddr_t        src;
702         __u8            src_type;
703         bdaddr_t        init_addr;
704         __u8            init_addr_type;
705         bdaddr_t        resp_addr;
706         __u8            resp_addr_type;
707         __u8            adv_instance;
708         __u16           handle;
709         __u16           sync_handle;
710         __u16           state;
711         __u8            mode;
712         __u8            type;
713         __u8            role;
714         bool            out;
715         __u8            attempt;
716         __u8            dev_class[3];
717         __u8            features[HCI_MAX_PAGES][8];
718         __u16           pkt_type;
719         __u16           link_policy;
720         __u8            key_type;
721         __u8            auth_type;
722         __u8            sec_level;
723         __u8            pending_sec_level;
724         __u8            pin_length;
725         __u8            enc_key_size;
726         __u8            io_capability;
727         __u32           passkey_notify;
728         __u8            passkey_entered;
729         __u16           disc_timeout;
730         __u16           conn_timeout;
731         __u16           setting;
732         __u16           auth_payload_timeout;
733         __u16           le_conn_min_interval;
734         __u16           le_conn_max_interval;
735         __u16           le_conn_interval;
736         __u16           le_conn_latency;
737         __u16           le_supv_timeout;
738         __u8            le_adv_data[HCI_MAX_AD_LENGTH];
739         __u8            le_adv_data_len;
740         __u8            le_per_adv_data[HCI_MAX_PER_AD_LENGTH];
741         __u8            le_per_adv_data_len;
742         __u8            le_tx_phy;
743         __u8            le_rx_phy;
744         __s8            rssi;
745         __s8            tx_power;
746         __s8            max_tx_power;
747         struct bt_iso_qos iso_qos;
748         unsigned long   flags;
749
750         enum conn_reasons conn_reason;
751
752         __u32           clock;
753         __u16           clock_accuracy;
754
755         unsigned long   conn_info_timestamp;
756
757         __u8            remote_cap;
758         __u8            remote_auth;
759         __u8            remote_id;
760
761         unsigned int    sent;
762
763         struct sk_buff_head data_q;
764         struct list_head chan_list;
765
766         struct delayed_work disc_work;
767         struct delayed_work auto_accept_work;
768         struct delayed_work idle_work;
769         struct delayed_work le_conn_timeout;
770         struct work_struct  le_scan_cleanup;
771
772         struct device   dev;
773         struct dentry   *debugfs;
774
775         struct hci_dev  *hdev;
776         void            *l2cap_data;
777         void            *sco_data;
778         void            *iso_data;
779         struct amp_mgr  *amp_mgr;
780
781 #ifdef TIZEN_BT
782         bool            rssi_monitored;
783 #endif
784         struct hci_conn *link;
785         struct bt_codec codec;
786
787         void (*connect_cfm_cb)  (struct hci_conn *conn, u8 status);
788         void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
789         void (*disconn_cfm_cb)  (struct hci_conn *conn, u8 reason);
790
791         void (*cleanup)(struct hci_conn *conn);
792 };
793
794 struct hci_chan {
795         struct list_head list;
796         __u16 handle;
797         struct hci_conn *conn;
798         struct sk_buff_head data_q;
799         unsigned int    sent;
800         __u8            state;
801         bool            amp;
802 };
803
804 struct hci_conn_params {
805         struct list_head list;
806         struct list_head action;
807
808         bdaddr_t addr;
809         u8 addr_type;
810
811         u16 conn_min_interval;
812         u16 conn_max_interval;
813         u16 conn_latency;
814         u16 supervision_timeout;
815
816         enum {
817                 HCI_AUTO_CONN_DISABLED,
818                 HCI_AUTO_CONN_REPORT,
819                 HCI_AUTO_CONN_DIRECT,
820                 HCI_AUTO_CONN_ALWAYS,
821                 HCI_AUTO_CONN_LINK_LOSS,
822                 HCI_AUTO_CONN_EXPLICIT,
823         } auto_connect;
824
825         struct hci_conn *conn;
826         bool explicit_connect;
827         hci_conn_flags_t flags;
828         u8  privacy_mode;
829 };
830
831 extern struct list_head hci_dev_list;
832 extern struct list_head hci_cb_list;
833 extern rwlock_t hci_dev_list_lock;
834 extern struct mutex hci_cb_list_lock;
835
836 #define hci_dev_set_flag(hdev, nr)             set_bit((nr), (hdev)->dev_flags)
837 #define hci_dev_clear_flag(hdev, nr)           clear_bit((nr), (hdev)->dev_flags)
838 #define hci_dev_change_flag(hdev, nr)          change_bit((nr), (hdev)->dev_flags)
839 #define hci_dev_test_flag(hdev, nr)            test_bit((nr), (hdev)->dev_flags)
840 #define hci_dev_test_and_set_flag(hdev, nr)    test_and_set_bit((nr), (hdev)->dev_flags)
841 #define hci_dev_test_and_clear_flag(hdev, nr)  test_and_clear_bit((nr), (hdev)->dev_flags)
842 #define hci_dev_test_and_change_flag(hdev, nr) test_and_change_bit((nr), (hdev)->dev_flags)
843
844 #define hci_dev_clear_volatile_flags(hdev)                      \
845         do {                                                    \
846                 hci_dev_clear_flag(hdev, HCI_LE_SCAN);          \
847                 hci_dev_clear_flag(hdev, HCI_LE_ADV);           \
848                 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);\
849                 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);     \
850                 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);   \
851         } while (0)
852
853 #define hci_dev_le_state_simultaneous(hdev) \
854         (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && \
855          (hdev->le_states[4] & 0x08) && /* Central */ \
856          (hdev->le_states[4] & 0x40) && /* Peripheral */ \
857          (hdev->le_states[3] & 0x10))   /* Simultaneous */
858
859 /* ----- HCI interface to upper protocols ----- */
860 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
861 int l2cap_disconn_ind(struct hci_conn *hcon);
862 void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags);
863
864 #if IS_ENABLED(CONFIG_BT_BREDR)
865 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
866 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
867 #else
868 static inline int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
869                                   __u8 *flags)
870 {
871         return 0;
872 }
873
874 static inline void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
875 {
876 }
877 #endif
878
879 #if IS_ENABLED(CONFIG_BT_LE)
880 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
881 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags);
882 #else
883 static inline int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
884                                   __u8 *flags)
885 {
886         return 0;
887 }
888 static inline void iso_recv(struct hci_conn *hcon, struct sk_buff *skb,
889                             u16 flags)
890 {
891 }
892 #endif
893
894 /* ----- Inquiry cache ----- */
895 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
896 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
897
898 static inline void discovery_init(struct hci_dev *hdev)
899 {
900         hdev->discovery.state = DISCOVERY_STOPPED;
901         INIT_LIST_HEAD(&hdev->discovery.all);
902         INIT_LIST_HEAD(&hdev->discovery.unknown);
903         INIT_LIST_HEAD(&hdev->discovery.resolve);
904         hdev->discovery.report_invalid_rssi = true;
905         hdev->discovery.rssi = HCI_RSSI_INVALID;
906 }
907
908 static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
909 {
910         hdev->discovery.result_filtering = false;
911         hdev->discovery.report_invalid_rssi = true;
912         hdev->discovery.rssi = HCI_RSSI_INVALID;
913         hdev->discovery.uuid_count = 0;
914         kfree(hdev->discovery.uuids);
915         hdev->discovery.uuids = NULL;
916         hdev->discovery.scan_start = 0;
917         hdev->discovery.scan_duration = 0;
918 }
919
920 bool hci_discovery_active(struct hci_dev *hdev);
921
922 void hci_discovery_set_state(struct hci_dev *hdev, int state);
923
924 static inline int inquiry_cache_empty(struct hci_dev *hdev)
925 {
926         return list_empty(&hdev->discovery.all);
927 }
928
929 static inline long inquiry_cache_age(struct hci_dev *hdev)
930 {
931         struct discovery_state *c = &hdev->discovery;
932         return jiffies - c->timestamp;
933 }
934
935 static inline long inquiry_entry_age(struct inquiry_entry *e)
936 {
937         return jiffies - e->timestamp;
938 }
939
940 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
941                                                bdaddr_t *bdaddr);
942 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
943                                                        bdaddr_t *bdaddr);
944 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
945                                                        bdaddr_t *bdaddr,
946                                                        int state);
947 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
948                                       struct inquiry_entry *ie);
949 u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
950                              bool name_known);
951 void hci_inquiry_cache_flush(struct hci_dev *hdev);
952
953 /* ----- HCI Connections ----- */
954 enum {
955         HCI_CONN_AUTH_PEND,
956         HCI_CONN_REAUTH_PEND,
957         HCI_CONN_ENCRYPT_PEND,
958         HCI_CONN_RSWITCH_PEND,
959         HCI_CONN_MODE_CHANGE_PEND,
960         HCI_CONN_SCO_SETUP_PEND,
961         HCI_CONN_MGMT_CONNECTED,
962         HCI_CONN_SSP_ENABLED,
963         HCI_CONN_SC_ENABLED,
964         HCI_CONN_AES_CCM,
965         HCI_CONN_POWER_SAVE,
966         HCI_CONN_FLUSH_KEY,
967         HCI_CONN_ENCRYPT,
968         HCI_CONN_AUTH,
969         HCI_CONN_SECURE,
970         HCI_CONN_FIPS,
971         HCI_CONN_STK_ENCRYPT,
972         HCI_CONN_AUTH_INITIATOR,
973         HCI_CONN_DROP,
974         HCI_CONN_CANCEL,
975         HCI_CONN_PARAM_REMOVAL_PEND,
976         HCI_CONN_NEW_LINK_KEY,
977         HCI_CONN_SCANNING,
978         HCI_CONN_AUTH_FAILURE,
979         HCI_CONN_PER_ADV,
980 };
981
982 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
983 {
984         struct hci_dev *hdev = conn->hdev;
985         return hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
986                test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
987 }
988
989 static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
990 {
991         struct hci_dev *hdev = conn->hdev;
992         return hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
993                test_bit(HCI_CONN_SC_ENABLED, &conn->flags);
994 }
995
996 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
997 {
998         struct hci_conn_hash *h = &hdev->conn_hash;
999         list_add_rcu(&c->list, &h->list);
1000         switch (c->type) {
1001         case ACL_LINK:
1002                 h->acl_num++;
1003                 break;
1004         case AMP_LINK:
1005                 h->amp_num++;
1006                 break;
1007         case LE_LINK:
1008                 h->le_num++;
1009                 if (c->role == HCI_ROLE_SLAVE)
1010                         h->le_num_peripheral++;
1011                 break;
1012         case SCO_LINK:
1013         case ESCO_LINK:
1014                 h->sco_num++;
1015                 break;
1016         case ISO_LINK:
1017                 h->iso_num++;
1018                 break;
1019         }
1020 }
1021
1022 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
1023 {
1024         struct hci_conn_hash *h = &hdev->conn_hash;
1025
1026         list_del_rcu(&c->list);
1027         synchronize_rcu();
1028
1029         switch (c->type) {
1030         case ACL_LINK:
1031                 h->acl_num--;
1032                 break;
1033         case AMP_LINK:
1034                 h->amp_num--;
1035                 break;
1036         case LE_LINK:
1037                 h->le_num--;
1038                 if (c->role == HCI_ROLE_SLAVE)
1039                         h->le_num_peripheral--;
1040                 break;
1041         case SCO_LINK:
1042         case ESCO_LINK:
1043                 h->sco_num--;
1044                 break;
1045         case ISO_LINK:
1046                 h->iso_num--;
1047                 break;
1048         }
1049 }
1050
1051 static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
1052 {
1053         struct hci_conn_hash *h = &hdev->conn_hash;
1054         switch (type) {
1055         case ACL_LINK:
1056                 return h->acl_num;
1057         case AMP_LINK:
1058                 return h->amp_num;
1059         case LE_LINK:
1060                 return h->le_num;
1061         case SCO_LINK:
1062         case ESCO_LINK:
1063                 return h->sco_num;
1064         case ISO_LINK:
1065                 return h->iso_num;
1066         default:
1067                 return 0;
1068         }
1069 }
1070
1071 static inline unsigned int hci_conn_count(struct hci_dev *hdev)
1072 {
1073         struct hci_conn_hash *c = &hdev->conn_hash;
1074
1075         return c->acl_num + c->amp_num + c->sco_num + c->le_num + c->iso_num;
1076 }
1077
1078 static inline __u8 hci_conn_lookup_type(struct hci_dev *hdev, __u16 handle)
1079 {
1080         struct hci_conn_hash *h = &hdev->conn_hash;
1081         struct hci_conn *c;
1082         __u8 type = INVALID_LINK;
1083
1084         rcu_read_lock();
1085
1086         list_for_each_entry_rcu(c, &h->list, list) {
1087                 if (c->handle == handle) {
1088                         type = c->type;
1089                         break;
1090                 }
1091         }
1092
1093         rcu_read_unlock();
1094
1095         return type;
1096 }
1097
1098 static inline struct hci_conn *hci_conn_hash_lookup_bis(struct hci_dev *hdev,
1099                                                         bdaddr_t *ba,
1100                                                         __u8 big, __u8 bis)
1101 {
1102         struct hci_conn_hash *h = &hdev->conn_hash;
1103         struct hci_conn  *c;
1104
1105         rcu_read_lock();
1106
1107         list_for_each_entry_rcu(c, &h->list, list) {
1108                 if (bacmp(&c->dst, ba) || c->type != ISO_LINK)
1109                         continue;
1110
1111                 if (c->iso_qos.big == big && c->iso_qos.bis == bis) {
1112                         rcu_read_unlock();
1113                         return c;
1114                 }
1115         }
1116         rcu_read_unlock();
1117
1118         return NULL;
1119 }
1120
1121 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
1122                                                                 __u16 handle)
1123 {
1124         struct hci_conn_hash *h = &hdev->conn_hash;
1125         struct hci_conn  *c;
1126
1127         rcu_read_lock();
1128
1129         list_for_each_entry_rcu(c, &h->list, list) {
1130                 if (c->handle == handle) {
1131                         rcu_read_unlock();
1132                         return c;
1133                 }
1134         }
1135         rcu_read_unlock();
1136
1137         return NULL;
1138 }
1139
1140 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
1141                                                         __u8 type, bdaddr_t *ba)
1142 {
1143         struct hci_conn_hash *h = &hdev->conn_hash;
1144         struct hci_conn  *c;
1145
1146         rcu_read_lock();
1147
1148         list_for_each_entry_rcu(c, &h->list, list) {
1149                 if (c->type == type && !bacmp(&c->dst, ba)) {
1150                         rcu_read_unlock();
1151                         return c;
1152                 }
1153         }
1154
1155         rcu_read_unlock();
1156
1157         return NULL;
1158 }
1159
1160 static inline struct hci_conn *hci_conn_hash_lookup_le(struct hci_dev *hdev,
1161                                                        bdaddr_t *ba,
1162                                                        __u8 ba_type)
1163 {
1164         struct hci_conn_hash *h = &hdev->conn_hash;
1165         struct hci_conn  *c;
1166
1167         rcu_read_lock();
1168
1169         list_for_each_entry_rcu(c, &h->list, list) {
1170                 if (c->type != LE_LINK)
1171                        continue;
1172
1173                 if (ba_type == c->dst_type && !bacmp(&c->dst, ba)) {
1174                         rcu_read_unlock();
1175                         return c;
1176                 }
1177         }
1178
1179         rcu_read_unlock();
1180
1181         return NULL;
1182 }
1183
1184 static inline struct hci_conn *hci_conn_hash_lookup_cis(struct hci_dev *hdev,
1185                                                         bdaddr_t *ba,
1186                                                         __u8 ba_type)
1187 {
1188         struct hci_conn_hash *h = &hdev->conn_hash;
1189         struct hci_conn  *c;
1190
1191         rcu_read_lock();
1192
1193         list_for_each_entry_rcu(c, &h->list, list) {
1194                 if (c->type != ISO_LINK)
1195                         continue;
1196
1197                 if (ba_type == c->dst_type && !bacmp(&c->dst, ba)) {
1198                         rcu_read_unlock();
1199                         return c;
1200                 }
1201         }
1202
1203         rcu_read_unlock();
1204
1205         return NULL;
1206 }
1207
1208 static inline struct hci_conn *hci_conn_hash_lookup_cig(struct hci_dev *hdev,
1209                                                         __u8 handle)
1210 {
1211         struct hci_conn_hash *h = &hdev->conn_hash;
1212         struct hci_conn  *c;
1213
1214         rcu_read_lock();
1215
1216         list_for_each_entry_rcu(c, &h->list, list) {
1217                 if (c->type != ISO_LINK)
1218                         continue;
1219
1220                 if (handle == c->iso_qos.cig) {
1221                         rcu_read_unlock();
1222                         return c;
1223                 }
1224         }
1225
1226         rcu_read_unlock();
1227
1228         return NULL;
1229 }
1230
1231 static inline struct hci_conn *hci_conn_hash_lookup_big(struct hci_dev *hdev,
1232                                                         __u8 handle)
1233 {
1234         struct hci_conn_hash *h = &hdev->conn_hash;
1235         struct hci_conn  *c;
1236
1237         rcu_read_lock();
1238
1239         list_for_each_entry_rcu(c, &h->list, list) {
1240                 if (bacmp(&c->dst, BDADDR_ANY) || c->type != ISO_LINK)
1241                         continue;
1242
1243                 if (handle == c->iso_qos.big) {
1244                         rcu_read_unlock();
1245                         return c;
1246                 }
1247         }
1248
1249         rcu_read_unlock();
1250
1251         return NULL;
1252 }
1253
1254 static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
1255                                                         __u8 type, __u16 state)
1256 {
1257         struct hci_conn_hash *h = &hdev->conn_hash;
1258         struct hci_conn  *c;
1259
1260         rcu_read_lock();
1261
1262         list_for_each_entry_rcu(c, &h->list, list) {
1263                 if (c->type == type && c->state == state) {
1264                         rcu_read_unlock();
1265                         return c;
1266                 }
1267         }
1268
1269         rcu_read_unlock();
1270
1271         return NULL;
1272 }
1273
1274 typedef void (*hci_conn_func_t)(struct hci_conn *conn, void *data);
1275 static inline void hci_conn_hash_list_state(struct hci_dev *hdev,
1276                                             hci_conn_func_t func, __u8 type,
1277                                             __u16 state, void *data)
1278 {
1279         struct hci_conn_hash *h = &hdev->conn_hash;
1280         struct hci_conn  *c;
1281
1282         if (!func)
1283                 return;
1284
1285         rcu_read_lock();
1286
1287         list_for_each_entry_rcu(c, &h->list, list) {
1288                 if (c->type == type && c->state == state)
1289                         func(c, data);
1290         }
1291
1292         rcu_read_unlock();
1293 }
1294
1295 static inline struct hci_conn *hci_lookup_le_connect(struct hci_dev *hdev)
1296 {
1297         struct hci_conn_hash *h = &hdev->conn_hash;
1298         struct hci_conn  *c;
1299
1300         rcu_read_lock();
1301
1302         list_for_each_entry_rcu(c, &h->list, list) {
1303                 if (c->type == LE_LINK && c->state == BT_CONNECT &&
1304                     !test_bit(HCI_CONN_SCANNING, &c->flags)) {
1305                         rcu_read_unlock();
1306                         return c;
1307                 }
1308         }
1309
1310         rcu_read_unlock();
1311
1312         return NULL;
1313 }
1314
1315 #ifdef TIZEN_BT
1316 static inline bool hci_conn_rssi_state_set(struct hci_dev *hdev,
1317                                         __u8 type, bdaddr_t *ba, bool value)
1318 {
1319         struct hci_conn_hash *h = &hdev->conn_hash;
1320         struct hci_conn  *c;
1321         __u8 conn_type;
1322
1323         if (type == 0x01)
1324                 conn_type = LE_LINK;
1325         else
1326                 conn_type = ACL_LINK;
1327
1328         rcu_read_lock();
1329
1330         list_for_each_entry_rcu(c, &h->list, list) {
1331                 if (c->type == conn_type && !bacmp(&c->dst, ba)) {
1332                         c->rssi_monitored = value;
1333                         rcu_read_unlock();
1334                         return true;
1335                 }
1336         }
1337
1338         rcu_read_unlock();
1339         return false;
1340 }
1341
1342 static inline void hci_conn_rssi_unset_all(struct hci_dev *hdev,
1343                                         __u8 type)
1344 {
1345         struct hci_conn_hash *h = &hdev->conn_hash;
1346         struct hci_conn  *c;
1347         __u8 conn_type;
1348
1349         if (type == 0x01)
1350                 conn_type = LE_LINK;
1351         else
1352                 conn_type = ACL_LINK;
1353
1354         rcu_read_lock();
1355         list_for_each_entry_rcu(c, &h->list, list) {
1356                 if (c->type == conn_type)
1357                         c->rssi_monitored = false;
1358         }
1359         rcu_read_unlock();
1360 }
1361
1362 static inline int hci_conn_hash_lookup_rssi_count(struct hci_dev *hdev)
1363 {
1364         struct hci_conn_hash *h = &hdev->conn_hash;
1365         struct hci_conn  *c;
1366         int count = 0;
1367
1368         rcu_read_lock();
1369         list_for_each_entry_rcu(c, &h->list, list) {
1370                 if (c->rssi_monitored == true)
1371                         ++count;
1372         }
1373         rcu_read_unlock();
1374
1375         return count;
1376 }
1377
1378 bool hci_le_discovery_active(struct hci_dev *hdev);
1379 void hci_le_discovery_set_state(struct hci_dev *hdev, int state);
1380 #endif
1381
1382 int hci_disconnect(struct hci_conn *conn, __u8 reason);
1383 bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
1384 void hci_sco_setup(struct hci_conn *conn, __u8 status);
1385 bool hci_iso_setup_path(struct hci_conn *conn);
1386 int hci_le_create_cis(struct hci_conn *conn);
1387
1388 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1389                               u8 role);
1390 int hci_conn_del(struct hci_conn *conn);
1391 void hci_conn_hash_flush(struct hci_dev *hdev);
1392 void hci_conn_check_pending(struct hci_dev *hdev);
1393
1394 struct hci_chan *hci_chan_create(struct hci_conn *conn);
1395 void hci_chan_del(struct hci_chan *chan);
1396 void hci_chan_list_flush(struct hci_conn *conn);
1397 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
1398
1399 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1400                                      u8 dst_type, u8 sec_level,
1401                                      u16 conn_timeout,
1402                                      enum conn_reasons conn_reason);
1403 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1404                                 u8 dst_type, bool dst_resolved, u8 sec_level,
1405                                 u16 conn_timeout, u8 role);
1406 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1407                                  u8 sec_level, u8 auth_type,
1408                                  enum conn_reasons conn_reason);
1409 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1410                                  __u16 setting, struct bt_codec *codec);
1411 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1412                               __u8 dst_type, struct bt_iso_qos *qos);
1413 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
1414                                  __u8 dst_type, struct bt_iso_qos *qos);
1415 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
1416                                  __u8 dst_type, struct bt_iso_qos *qos,
1417                                  __u8 data_len, __u8 *data);
1418 int hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst, __u8 dst_type,
1419                        __u8 sid);
1420 int hci_le_big_create_sync(struct hci_dev *hdev, struct bt_iso_qos *qos,
1421                            __u16 sync_handle, __u8 num_bis, __u8 bis[]);
1422 int hci_conn_check_link_mode(struct hci_conn *conn);
1423 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
1424 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1425                       bool initiator);
1426 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
1427
1428 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
1429
1430 void hci_conn_failed(struct hci_conn *conn, u8 status);
1431
1432 /*
1433  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
1434  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
1435  * working or anything else. They just guarantee that the object is available
1436  * and can be dereferenced. So you can use its locks, local variables and any
1437  * other constant data.
1438  * Before accessing runtime data, you _must_ lock the object and then check that
1439  * it is still running. As soon as you release the locks, the connection might
1440  * get dropped, though.
1441  *
1442  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
1443  * how long the underlying connection is held. So every channel that runs on the
1444  * hci_conn object calls this to prevent the connection from disappearing. As
1445  * long as you hold a device, you must also guarantee that you have a valid
1446  * reference to the device via hci_conn_get() (or the initial reference from
1447  * hci_conn_add()).
1448  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
1449  * break because nobody cares for that. But this means, we cannot use
1450  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
1451  */
1452
1453 static inline struct hci_conn *hci_conn_get(struct hci_conn *conn)
1454 {
1455         get_device(&conn->dev);
1456         return conn;
1457 }
1458
1459 static inline void hci_conn_put(struct hci_conn *conn)
1460 {
1461         put_device(&conn->dev);
1462 }
1463
1464 static inline void hci_conn_hold(struct hci_conn *conn)
1465 {
1466         BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1467
1468         atomic_inc(&conn->refcnt);
1469         cancel_delayed_work(&conn->disc_work);
1470 }
1471
1472 static inline void hci_conn_drop(struct hci_conn *conn)
1473 {
1474         BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1475
1476         if (atomic_dec_and_test(&conn->refcnt)) {
1477                 unsigned long timeo;
1478
1479                 switch (conn->type) {
1480                 case ACL_LINK:
1481                 case LE_LINK:
1482                         cancel_delayed_work(&conn->idle_work);
1483                         if (conn->state == BT_CONNECTED) {
1484                                 timeo = conn->disc_timeout;
1485                                 if (!conn->out)
1486                                         timeo *= 2;
1487                         } else {
1488                                 timeo = 0;
1489                         }
1490                         break;
1491
1492                 case AMP_LINK:
1493                         timeo = conn->disc_timeout;
1494                         break;
1495
1496                 default:
1497                         timeo = 0;
1498                         break;
1499                 }
1500
1501                 cancel_delayed_work(&conn->disc_work);
1502                 queue_delayed_work(conn->hdev->workqueue,
1503                                    &conn->disc_work, timeo);
1504         }
1505 }
1506
1507 /* ----- HCI Devices ----- */
1508 static inline void hci_dev_put(struct hci_dev *d)
1509 {
1510         BT_DBG("%s orig refcnt %d", d->name,
1511                kref_read(&d->dev.kobj.kref));
1512
1513         put_device(&d->dev);
1514 }
1515
1516 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
1517 {
1518         BT_DBG("%s orig refcnt %d", d->name,
1519                kref_read(&d->dev.kobj.kref));
1520
1521         get_device(&d->dev);
1522         return d;
1523 }
1524
1525 #define hci_dev_lock(d)         mutex_lock(&d->lock)
1526 #define hci_dev_unlock(d)       mutex_unlock(&d->lock)
1527
1528 #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
1529 #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
1530
1531 static inline void *hci_get_drvdata(struct hci_dev *hdev)
1532 {
1533         return dev_get_drvdata(&hdev->dev);
1534 }
1535
1536 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
1537 {
1538         dev_set_drvdata(&hdev->dev, data);
1539 }
1540
1541 static inline void *hci_get_priv(struct hci_dev *hdev)
1542 {
1543         return (char *)hdev + sizeof(*hdev);
1544 }
1545
1546 struct hci_dev *hci_dev_get(int index);
1547 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, u8 src_type);
1548
1549 struct hci_dev *hci_alloc_dev_priv(int sizeof_priv);
1550
1551 static inline struct hci_dev *hci_alloc_dev(void)
1552 {
1553         return hci_alloc_dev_priv(0);
1554 }
1555
1556 void hci_free_dev(struct hci_dev *hdev);
1557 int hci_register_dev(struct hci_dev *hdev);
1558 void hci_unregister_dev(struct hci_dev *hdev);
1559 void hci_release_dev(struct hci_dev *hdev);
1560 int hci_register_suspend_notifier(struct hci_dev *hdev);
1561 int hci_unregister_suspend_notifier(struct hci_dev *hdev);
1562 int hci_suspend_dev(struct hci_dev *hdev);
1563 int hci_resume_dev(struct hci_dev *hdev);
1564 int hci_reset_dev(struct hci_dev *hdev);
1565 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
1566 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb);
1567 __printf(2, 3) void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...);
1568 __printf(2, 3) void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...);
1569
1570 static inline void hci_set_msft_opcode(struct hci_dev *hdev, __u16 opcode)
1571 {
1572 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
1573         hdev->msft_opcode = opcode;
1574 #endif
1575 }
1576
1577 static inline void hci_set_aosp_capable(struct hci_dev *hdev)
1578 {
1579 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
1580         hdev->aosp_capable = true;
1581 #endif
1582 }
1583
1584 int hci_dev_open(__u16 dev);
1585 int hci_dev_close(__u16 dev);
1586 int hci_dev_do_close(struct hci_dev *hdev);
1587 int hci_dev_reset(__u16 dev);
1588 int hci_dev_reset_stat(__u16 dev);
1589 int hci_dev_cmd(unsigned int cmd, void __user *arg);
1590 int hci_get_dev_list(void __user *arg);
1591 int hci_get_dev_info(void __user *arg);
1592 int hci_get_conn_list(void __user *arg);
1593 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
1594 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
1595 int hci_inquiry(void __user *arg);
1596
1597 struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *list,
1598                                            bdaddr_t *bdaddr, u8 type);
1599 struct bdaddr_list_with_irk *hci_bdaddr_list_lookup_with_irk(
1600                                     struct list_head *list, bdaddr_t *bdaddr,
1601                                     u8 type);
1602 struct bdaddr_list_with_flags *
1603 hci_bdaddr_list_lookup_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1604                                   u8 type);
1605 int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1606 int hci_bdaddr_list_add_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1607                                  u8 type, u8 *peer_irk, u8 *local_irk);
1608 int hci_bdaddr_list_add_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1609                                    u8 type, u32 flags);
1610 int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1611 int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1612                                  u8 type);
1613 int hci_bdaddr_list_del_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1614                                    u8 type);
1615 void hci_bdaddr_list_clear(struct list_head *list);
1616
1617 struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
1618                                                bdaddr_t *addr, u8 addr_type);
1619 struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
1620                                             bdaddr_t *addr, u8 addr_type);
1621 void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
1622 void hci_conn_params_clear_disabled(struct hci_dev *hdev);
1623
1624 struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
1625                                                   bdaddr_t *addr,
1626                                                   u8 addr_type);
1627
1628 void hci_uuids_clear(struct hci_dev *hdev);
1629
1630 void hci_link_keys_clear(struct hci_dev *hdev);
1631 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1632 struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
1633                                   bdaddr_t *bdaddr, u8 *val, u8 type,
1634                                   u8 pin_len, bool *persistent);
1635 struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1636                             u8 addr_type, u8 type, u8 authenticated,
1637                             u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
1638 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1639                              u8 addr_type, u8 role);
1640 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
1641 void hci_smp_ltks_clear(struct hci_dev *hdev);
1642 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1643
1644 struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
1645 struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1646                                      u8 addr_type);
1647 struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1648                             u8 addr_type, u8 val[16], bdaddr_t *rpa);
1649 void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
1650 bool hci_is_blocked_key(struct hci_dev *hdev, u8 type, u8 val[16]);
1651 void hci_blocked_keys_clear(struct hci_dev *hdev);
1652 void hci_smp_irks_clear(struct hci_dev *hdev);
1653
1654 bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1655
1656 void hci_remote_oob_data_clear(struct hci_dev *hdev);
1657 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1658                                           bdaddr_t *bdaddr, u8 bdaddr_type);
1659 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1660                             u8 bdaddr_type, u8 *hash192, u8 *rand192,
1661                             u8 *hash256, u8 *rand256);
1662 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1663                                u8 bdaddr_type);
1664
1665 void hci_adv_instances_clear(struct hci_dev *hdev);
1666 struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance);
1667 struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance);
1668 struct adv_info *hci_add_adv_instance(struct hci_dev *hdev, u8 instance,
1669                                       u32 flags, u16 adv_data_len, u8 *adv_data,
1670                                       u16 scan_rsp_len, u8 *scan_rsp_data,
1671                                       u16 timeout, u16 duration, s8 tx_power,
1672                                       u32 min_interval, u32 max_interval,
1673                                       u8 mesh_handle);
1674 struct adv_info *hci_add_per_instance(struct hci_dev *hdev, u8 instance,
1675                                       u32 flags, u8 data_len, u8 *data,
1676                                       u32 min_interval, u32 max_interval);
1677 int hci_set_adv_instance_data(struct hci_dev *hdev, u8 instance,
1678                          u16 adv_data_len, u8 *adv_data,
1679                          u16 scan_rsp_len, u8 *scan_rsp_data);
1680 int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
1681 void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
1682 u32 hci_adv_instance_flags(struct hci_dev *hdev, u8 instance);
1683 bool hci_adv_instance_is_scannable(struct hci_dev *hdev, u8 instance);
1684
1685 void hci_adv_monitors_clear(struct hci_dev *hdev);
1686 void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1687 int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1688 int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle);
1689 int hci_remove_all_adv_monitor(struct hci_dev *hdev);
1690 bool hci_is_adv_monitoring(struct hci_dev *hdev);
1691 int hci_get_adv_monitor_offload_ext(struct hci_dev *hdev);
1692
1693 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
1694
1695 void hci_init_sysfs(struct hci_dev *hdev);
1696 void hci_conn_init_sysfs(struct hci_conn *conn);
1697 void hci_conn_add_sysfs(struct hci_conn *conn);
1698 void hci_conn_del_sysfs(struct hci_conn *conn);
1699
1700 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
1701
1702 /* ----- LMP capabilities ----- */
1703 #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
1704 #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
1705 #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
1706 #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
1707 #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
1708 #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
1709 #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
1710 #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
1711 #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
1712 #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
1713 #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
1714 #define lmp_esco_2m_capable(dev)   ((dev)->features[0][5] & LMP_EDR_ESCO_2M)
1715 #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
1716 #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
1717 #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
1718 #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
1719 #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
1720 #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
1721 #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
1722 #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
1723 #define lmp_edr_2m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_2M)
1724 #define lmp_edr_3m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_3M)
1725 #define lmp_edr_3slot_capable(dev) ((dev)->features[0][4] & LMP_EDR_3SLOT)
1726 #define lmp_edr_5slot_capable(dev) ((dev)->features[0][5] & LMP_EDR_5SLOT)
1727
1728 /* ----- Extended LMP capabilities ----- */
1729 #define lmp_cpb_central_capable(dev) ((dev)->features[2][0] & LMP_CPB_CENTRAL)
1730 #define lmp_cpb_peripheral_capable(dev) ((dev)->features[2][0] & LMP_CPB_PERIPHERAL)
1731 #define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
1732 #define lmp_sync_scan_capable(dev)  ((dev)->features[2][0] & LMP_SYNC_SCAN)
1733 #define lmp_sc_capable(dev)         ((dev)->features[2][1] & LMP_SC)
1734 #define lmp_ping_capable(dev)       ((dev)->features[2][1] & LMP_PING)
1735
1736 /* ----- Host capabilities ----- */
1737 #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
1738 #define lmp_host_sc_capable(dev)   ((dev)->features[1][0] & LMP_HOST_SC)
1739 #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
1740 #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
1741
1742 #define hdev_is_powered(dev)   (test_bit(HCI_UP, &(dev)->flags) && \
1743                                 !hci_dev_test_flag(dev, HCI_AUTO_OFF))
1744 #define bredr_sc_enabled(dev)  (lmp_sc_capable(dev) && \
1745                                 hci_dev_test_flag(dev, HCI_SC_ENABLED))
1746 #define rpa_valid(dev)         (bacmp(&dev->rpa, BDADDR_ANY) && \
1747                                 !hci_dev_test_flag(dev, HCI_RPA_EXPIRED))
1748 #define adv_rpa_valid(adv)     (bacmp(&adv->random_addr, BDADDR_ANY) && \
1749                                 !adv->rpa_expired)
1750
1751 #define scan_1m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_1M) || \
1752                       ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_1M))
1753
1754 #define scan_2m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_2M) || \
1755                       ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_2M))
1756
1757 #define scan_coded(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_CODED) || \
1758                          ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_CODED))
1759
1760 #define ll_privacy_capable(dev) ((dev)->le_features[0] & HCI_LE_LL_PRIVACY)
1761
1762 /* Use LL Privacy based address resolution if supported */
1763 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
1764                              hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
1765
1766 #define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
1767                                    (hdev->commands[39] & 0x04))
1768
1769 /* Use enhanced synchronous connection if command is supported and its quirk
1770  * has not been set.
1771  */
1772 #define enhanced_sync_conn_capable(dev) \
1773         (((dev)->commands[29] & 0x08) && \
1774          !test_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &(dev)->quirks))
1775
1776 /* Use ext scanning if set ext scan param and ext scan enable is supported */
1777 #define use_ext_scan(dev) (((dev)->commands[37] & 0x20) && \
1778                            ((dev)->commands[37] & 0x40) && \
1779                            !test_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &(dev)->quirks))
1780
1781 /* Use ext create connection if command is supported */
1782 #define use_ext_conn(dev) ((dev)->commands[37] & 0x80)
1783
1784 /* Extended advertising support */
1785 #define ext_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_EXT_ADV))
1786
1787 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 1789:
1788  *
1789  * C24: Mandatory if the LE Controller supports Connection State and either
1790  * LE Feature (LL Privacy) or LE Feature (Extended Advertising) is supported
1791  */
1792 #define use_enhanced_conn_complete(dev) (ll_privacy_capable(dev) || \
1793                                          ext_adv_capable(dev))
1794
1795 /* Periodic advertising support */
1796 #define per_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_PERIODIC_ADV))
1797
1798 /* CIS Master/Slave and BIS support */
1799 #define iso_capable(dev) (cis_capable(dev) || bis_capable(dev))
1800 #define cis_capable(dev) \
1801         (cis_central_capable(dev) || cis_peripheral_capable(dev))
1802 #define cis_central_capable(dev) \
1803         ((dev)->le_features[3] & HCI_LE_CIS_CENTRAL)
1804 #define cis_peripheral_capable(dev) \
1805         ((dev)->le_features[3] & HCI_LE_CIS_PERIPHERAL)
1806 #define bis_capable(dev) ((dev)->le_features[3] & HCI_LE_ISO_BROADCASTER)
1807
1808 #define mws_transport_config_capable(dev) (((dev)->commands[30] & 0x08) && \
1809         (!test_bit(HCI_QUIRK_BROKEN_MWS_TRANSPORT_CONFIG, &(dev)->quirks)))
1810
1811 /* ----- HCI protocols ----- */
1812 #define HCI_PROTO_DEFER             0x01
1813
1814 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
1815                                         __u8 type, __u8 *flags)
1816 {
1817         switch (type) {
1818         case ACL_LINK:
1819                 return l2cap_connect_ind(hdev, bdaddr);
1820
1821         case SCO_LINK:
1822         case ESCO_LINK:
1823                 return sco_connect_ind(hdev, bdaddr, flags);
1824
1825         case ISO_LINK:
1826                 return iso_connect_ind(hdev, bdaddr, flags);
1827
1828         default:
1829                 BT_ERR("unknown link type %d", type);
1830                 return -EINVAL;
1831         }
1832 }
1833
1834 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
1835 {
1836         if (conn->type != ACL_LINK && conn->type != LE_LINK)
1837                 return HCI_ERROR_REMOTE_USER_TERM;
1838
1839         return l2cap_disconn_ind(conn);
1840 }
1841
1842 /* ----- HCI callbacks ----- */
1843 struct hci_cb {
1844         struct list_head list;
1845
1846         char *name;
1847
1848         void (*connect_cfm)     (struct hci_conn *conn, __u8 status);
1849         void (*disconn_cfm)     (struct hci_conn *conn, __u8 status);
1850         void (*security_cfm)    (struct hci_conn *conn, __u8 status,
1851                                                                 __u8 encrypt);
1852         void (*key_change_cfm)  (struct hci_conn *conn, __u8 status);
1853         void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
1854 };
1855
1856 static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
1857 {
1858         struct hci_cb *cb;
1859
1860         mutex_lock(&hci_cb_list_lock);
1861         list_for_each_entry(cb, &hci_cb_list, list) {
1862                 if (cb->connect_cfm)
1863                         cb->connect_cfm(conn, status);
1864         }
1865         mutex_unlock(&hci_cb_list_lock);
1866
1867         if (conn->connect_cfm_cb)
1868                 conn->connect_cfm_cb(conn, status);
1869 }
1870
1871 static inline void hci_disconn_cfm(struct hci_conn *conn, __u8 reason)
1872 {
1873         struct hci_cb *cb;
1874
1875         mutex_lock(&hci_cb_list_lock);
1876         list_for_each_entry(cb, &hci_cb_list, list) {
1877                 if (cb->disconn_cfm)
1878                         cb->disconn_cfm(conn, reason);
1879         }
1880         mutex_unlock(&hci_cb_list_lock);
1881
1882         if (conn->disconn_cfm_cb)
1883                 conn->disconn_cfm_cb(conn, reason);
1884 }
1885
1886 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
1887 {
1888         struct hci_cb *cb;
1889         __u8 encrypt;
1890
1891         if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
1892                 return;
1893
1894         encrypt = test_bit(HCI_CONN_ENCRYPT, &conn->flags) ? 0x01 : 0x00;
1895
1896         mutex_lock(&hci_cb_list_lock);
1897         list_for_each_entry(cb, &hci_cb_list, list) {
1898                 if (cb->security_cfm)
1899                         cb->security_cfm(conn, status, encrypt);
1900         }
1901         mutex_unlock(&hci_cb_list_lock);
1902
1903         if (conn->security_cfm_cb)
1904                 conn->security_cfm_cb(conn, status);
1905 }
1906
1907 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
1908 {
1909         struct hci_cb *cb;
1910         __u8 encrypt;
1911
1912         if (conn->state == BT_CONFIG) {
1913                 if (!status)
1914                         conn->state = BT_CONNECTED;
1915
1916                 hci_connect_cfm(conn, status);
1917                 hci_conn_drop(conn);
1918                 return;
1919         }
1920
1921         if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1922                 encrypt = 0x00;
1923         else if (test_bit(HCI_CONN_AES_CCM, &conn->flags))
1924                 encrypt = 0x02;
1925         else
1926                 encrypt = 0x01;
1927
1928         if (!status) {
1929                 if (conn->sec_level == BT_SECURITY_SDP)
1930                         conn->sec_level = BT_SECURITY_LOW;
1931
1932                 if (conn->pending_sec_level > conn->sec_level)
1933                         conn->sec_level = conn->pending_sec_level;
1934         }
1935
1936         mutex_lock(&hci_cb_list_lock);
1937         list_for_each_entry(cb, &hci_cb_list, list) {
1938                 if (cb->security_cfm)
1939                         cb->security_cfm(conn, status, encrypt);
1940         }
1941         mutex_unlock(&hci_cb_list_lock);
1942
1943         if (conn->security_cfm_cb)
1944                 conn->security_cfm_cb(conn, status);
1945 }
1946
1947 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
1948 {
1949         struct hci_cb *cb;
1950
1951         mutex_lock(&hci_cb_list_lock);
1952         list_for_each_entry(cb, &hci_cb_list, list) {
1953                 if (cb->key_change_cfm)
1954                         cb->key_change_cfm(conn, status);
1955         }
1956         mutex_unlock(&hci_cb_list_lock);
1957 }
1958
1959 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
1960                                                                 __u8 role)
1961 {
1962         struct hci_cb *cb;
1963
1964         mutex_lock(&hci_cb_list_lock);
1965         list_for_each_entry(cb, &hci_cb_list, list) {
1966                 if (cb->role_switch_cfm)
1967                         cb->role_switch_cfm(conn, status, role);
1968         }
1969         mutex_unlock(&hci_cb_list_lock);
1970 }
1971
1972 static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
1973 {
1974         if (addr_type != ADDR_LE_DEV_RANDOM)
1975                 return false;
1976
1977         if ((bdaddr->b[5] & 0xc0) == 0x40)
1978                return true;
1979
1980         return false;
1981 }
1982
1983 static inline bool hci_is_identity_address(bdaddr_t *addr, u8 addr_type)
1984 {
1985         if (addr_type == ADDR_LE_DEV_PUBLIC)
1986                 return true;
1987
1988         /* Check for Random Static address type */
1989         if ((addr->b[5] & 0xc0) == 0xc0)
1990                 return true;
1991
1992         return false;
1993 }
1994
1995 static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
1996                                           bdaddr_t *bdaddr, u8 addr_type)
1997 {
1998         if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
1999                 return NULL;
2000
2001         return hci_find_irk_by_rpa(hdev, bdaddr);
2002 }
2003
2004 static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
2005                                         u16 to_multiplier)
2006 {
2007         u16 max_latency;
2008
2009         if (min > max || min < 6 || max > 3200)
2010                 return -EINVAL;
2011
2012         if (to_multiplier < 10 || to_multiplier > 3200)
2013                 return -EINVAL;
2014
2015         if (max >= to_multiplier * 8)
2016                 return -EINVAL;
2017
2018         max_latency = (to_multiplier * 4 / max) - 1;
2019         if (latency > 499 || latency > max_latency)
2020                 return -EINVAL;
2021
2022         return 0;
2023 }
2024
2025 int hci_register_cb(struct hci_cb *hcb);
2026 int hci_unregister_cb(struct hci_cb *hcb);
2027
2028 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
2029                    const void *param);
2030
2031 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
2032                  const void *param);
2033 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
2034 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
2035 void hci_send_iso(struct hci_conn *conn, struct sk_buff *skb);
2036
2037 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
2038 void *hci_recv_event_data(struct hci_dev *hdev, __u8 event);
2039
2040 u32 hci_conn_get_phy(struct hci_conn *conn);
2041
2042 /* ----- HCI Sockets ----- */
2043 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
2044 void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
2045                          int flag, struct sock *skip_sk);
2046 void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
2047 void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
2048                                  void *data, u16 data_len, ktime_t tstamp,
2049                                  int flag, struct sock *skip_sk);
2050
2051 void hci_sock_dev_event(struct hci_dev *hdev, int event);
2052
2053 #define HCI_MGMT_VAR_LEN        BIT(0)
2054 #define HCI_MGMT_NO_HDEV        BIT(1)
2055 #define HCI_MGMT_UNTRUSTED      BIT(2)
2056 #define HCI_MGMT_UNCONFIGURED   BIT(3)
2057 #define HCI_MGMT_HDEV_OPTIONAL  BIT(4)
2058
2059 struct hci_mgmt_handler {
2060         int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
2061                      u16 data_len);
2062         size_t data_len;
2063         unsigned long flags;
2064 };
2065
2066 struct hci_mgmt_chan {
2067         struct list_head list;
2068         unsigned short channel;
2069         size_t handler_count;
2070         const struct hci_mgmt_handler *handlers;
2071 #ifdef TIZEN_BT
2072         size_t tizen_handler_count;
2073         const struct hci_mgmt_handler *tizen_handlers;
2074 #endif
2075         void (*hdev_init) (struct sock *sk, struct hci_dev *hdev);
2076 };
2077
2078 int hci_mgmt_chan_register(struct hci_mgmt_chan *c);
2079 void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c);
2080
2081 /* Management interface */
2082 #define DISCOV_TYPE_BREDR               (BIT(BDADDR_BREDR))
2083 #define DISCOV_TYPE_LE                  (BIT(BDADDR_LE_PUBLIC) | \
2084                                          BIT(BDADDR_LE_RANDOM))
2085 #define DISCOV_TYPE_INTERLEAVED         (BIT(BDADDR_BREDR) | \
2086                                          BIT(BDADDR_LE_PUBLIC) | \
2087                                          BIT(BDADDR_LE_RANDOM))
2088
2089 /* These LE scan and inquiry parameters were chosen according to LE General
2090  * Discovery Procedure specification.
2091  */
2092 #define DISCOV_LE_SCAN_WIN              0x12
2093 #define DISCOV_LE_SCAN_INT              0x12
2094 #define DISCOV_LE_TIMEOUT               10240   /* msec */
2095 #define DISCOV_INTERLEAVED_TIMEOUT      5120    /* msec */
2096 #define DISCOV_INTERLEAVED_INQUIRY_LEN  0x04
2097 #define DISCOV_BREDR_INQUIRY_LEN        0x08
2098 #define DISCOV_LE_RESTART_DELAY         msecs_to_jiffies(200)   /* msec */
2099 #define DISCOV_LE_FAST_ADV_INT_MIN      0x00A0  /* 100 msec */
2100 #define DISCOV_LE_FAST_ADV_INT_MAX      0x00F0  /* 150 msec */
2101 #define DISCOV_LE_PER_ADV_INT_MIN       0x00A0  /* 200 msec */
2102 #define DISCOV_LE_PER_ADV_INT_MAX       0x00A0  /* 200 msec */
2103 #define DISCOV_LE_ADV_MESH_MIN          0x00A0  /* 100 msec */
2104 #define DISCOV_LE_ADV_MESH_MAX          0x00A0  /* 100 msec */
2105 #define INTERVAL_TO_MS(x)               (((x) * 10) / 0x10)
2106
2107 #define NAME_RESOLVE_DURATION           msecs_to_jiffies(10240) /* 10.24 sec */
2108
2109 void mgmt_fill_version_info(void *ver);
2110 int mgmt_new_settings(struct hci_dev *hdev);
2111 void mgmt_index_added(struct hci_dev *hdev);
2112 void mgmt_index_removed(struct hci_dev *hdev);
2113 void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
2114 void mgmt_power_on(struct hci_dev *hdev, int err);
2115 void __mgmt_power_off(struct hci_dev *hdev);
2116 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
2117                        bool persistent);
2118 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
2119                            u8 *name, u8 name_len);
2120 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
2121                               u8 link_type, u8 addr_type, u8 reason,
2122                               bool mgmt_connected);
2123 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
2124                             u8 link_type, u8 addr_type, u8 status);
2125 void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2126                          u8 addr_type, u8 status);
2127 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
2128 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2129                                   u8 status);
2130 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2131                                       u8 status);
2132 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2133                               u8 link_type, u8 addr_type, u32 value,
2134                               u8 confirm_hint);
2135 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2136                                      u8 link_type, u8 addr_type, u8 status);
2137 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2138                                          u8 link_type, u8 addr_type, u8 status);
2139 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2140                               u8 link_type, u8 addr_type);
2141 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2142                                      u8 link_type, u8 addr_type, u8 status);
2143 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2144                                          u8 link_type, u8 addr_type, u8 status);
2145 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
2146                              u8 link_type, u8 addr_type, u32 passkey,
2147                              u8 entered);
2148 void mgmt_auth_failed(struct hci_conn *conn, u8 status);
2149 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
2150 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
2151                                     u8 status);
2152 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
2153 void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status);
2154 void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status);
2155 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2156                        u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
2157                        u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
2158                        u64 instant);
2159 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2160                       u8 addr_type, s8 rssi, u8 *name, u8 name_len);
2161 void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
2162 void mgmt_suspending(struct hci_dev *hdev, u8 state);
2163 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
2164                    u8 addr_type);
2165 bool mgmt_powering_down(struct hci_dev *hdev);
2166 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
2167 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent);
2168 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
2169                    bool persistent);
2170 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
2171                          u8 bdaddr_type, u8 store_hint, u16 min_interval,
2172                          u16 max_interval, u16 latency, u16 timeout);
2173 void mgmt_smp_complete(struct hci_conn *conn, bool complete);
2174 bool mgmt_get_connectable(struct hci_dev *hdev);
2175 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev);
2176 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev,
2177                             u8 instance);
2178 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
2179                               u8 instance);
2180 void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle);
2181 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
2182 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
2183                                   bdaddr_t *bdaddr, u8 addr_type);
2184
2185 #ifdef TIZEN_BT
2186 void mgmt_rssi_enable_success(struct sock *sk, struct hci_dev *hdev,
2187                 void *data, struct hci_cc_rsp_enable_rssi *rp, int success);
2188 void mgmt_rssi_disable_success(struct sock *sk, struct hci_dev *hdev,
2189                 void *data, struct hci_cc_rsp_enable_rssi *rp, int success);
2190 int mgmt_set_rssi_threshold(struct sock *sk, struct hci_dev *hdev,
2191                 void *data, u16 len);
2192 void mgmt_rssi_alert_evt(struct hci_dev *hdev, struct sk_buff *skb);
2193 void mgmt_raw_rssi_response(struct hci_dev *hdev,
2194                 struct hci_cc_rp_get_raw_rssi *rp, int success);
2195 void mgmt_enable_rssi_cc(struct hci_dev *hdev, void *response, u8 status);
2196 int mgmt_device_name_update(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *name,
2197                 u8 name_len);
2198 void mgmt_le_discovering(struct hci_dev *hdev, u8 discovering);
2199 int mgmt_le_conn_updated(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type,
2200                 u8 dst_type, u16 conn_interval, u16 conn_latency,
2201                 u16 supervision_timeout);
2202 int mgmt_le_conn_update_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
2203                 u8 link_type, u8 addr_type, u8 status);
2204 void mgmt_hardware_error(struct hci_dev *hdev, u8 err_code);
2205 void mgmt_tx_timeout_error(struct hci_dev *hdev);
2206 #endif
2207
2208 int hci_abort_conn(struct hci_conn *conn, u8 reason);
2209 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
2210                       u16 to_multiplier);
2211 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
2212                       __u8 ltk[16], __u8 key_size);
2213
2214 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2215                                u8 *bdaddr_type);
2216
2217 #define SCO_AIRMODE_MASK       0x0003
2218 #define SCO_AIRMODE_CVSD       0x0000
2219 #define SCO_AIRMODE_TRANSP     0x0003
2220
2221 #define LOCAL_CODEC_ACL_MASK    BIT(0)
2222 #define LOCAL_CODEC_SCO_MASK    BIT(1)
2223
2224 #define TRANSPORT_TYPE_MAX      0x04
2225
2226 #endif /* __HCI_CORE_H */