hci_core.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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