diff options
author | Daniel Thompson <daniel.thompson@linaro.org> | 2017-07-30 08:09:38 +0100 |
---|---|---|
committer | Daniel Thompson <daniel.thompson@linaro.org> | 2017-07-31 16:50:12 +0100 |
commit | d056581fc634ee92304c865df251b864db2d92de (patch) | |
tree | a727a02aade1ab112e30bedf8139e6c8fffc332b | |
parent | 725bd2b87aa294f42c91b8ed73915e9ee9539c6b (diff) |
net: bluetooth: RDA support
-rw-r--r-- | net/bluetooth/af_bluetooth.c | 34 | ||||
-rw-r--r-- | net/bluetooth/amp.c | 2 | ||||
-rw-r--r-- | net/bluetooth/hci_conn.c | 57 | ||||
-rw-r--r-- | net/bluetooth/hci_core.c | 262 | ||||
-rw-r--r-- | net/bluetooth/hci_event.c | 20 | ||||
-rw-r--r-- | net/bluetooth/l2cap_core.c | 4 | ||||
-rw-r--r-- | net/bluetooth/mgmt.c | 4 |
7 files changed, 360 insertions, 23 deletions
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c index 6629cdc134dc..f7c36826f3f4 100644 --- a/net/bluetooth/af_bluetooth.c +++ b/net/bluetooth/af_bluetooth.c @@ -30,6 +30,11 @@ #include <net/bluetooth/bluetooth.h> #include <linux/proc_fs.h> +#ifndef CONFIG_BT_SOCK_DEBUG +#undef BT_DBG +#define BT_DBG(D...) +#endif + #define VERSION "2.16" /* Bluetooth sockets */ @@ -103,11 +108,40 @@ void bt_sock_unregister(int proto) } EXPORT_SYMBOL(bt_sock_unregister); +#ifdef CONFIG_PARANOID_NETWORK +static inline int current_has_bt_admin(void) +{ + return !current_euid(); +} + +static inline int current_has_bt(void) +{ + return current_has_bt_admin(); +} +# else +static inline int current_has_bt_admin(void) +{ + return 1; +} + +static inline int current_has_bt(void) +{ + return 1; +} +#endif + static int bt_sock_create(struct net *net, struct socket *sock, int proto, int kern) { int err; + if (proto == BTPROTO_RFCOMM || proto == BTPROTO_SCO || + proto == BTPROTO_L2CAP) { + if (!current_has_bt()) + return -EPERM; + } else if (!current_has_bt_admin()) + return -EPERM; + if (net != &init_net) return -EAFNOSUPPORT; diff --git a/net/bluetooth/amp.c b/net/bluetooth/amp.c index d459ed43c779..a3f3380c2095 100644 --- a/net/bluetooth/amp.c +++ b/net/bluetooth/amp.c @@ -113,7 +113,7 @@ struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr, bdaddr_t *dst = mgr->l2cap_conn->dst; struct hci_conn *hcon; - hcon = hci_conn_add(hdev, AMP_LINK, dst); + hcon = hci_conn_add(hdev, AMP_LINK, 0, dst); if (!hcon) return NULL; diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 8e7290aea8f8..c9b2d5011fe4 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -354,7 +354,8 @@ static void hci_conn_auto_accept(unsigned long arg) &conn->dst); } -struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) +struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, + __u16 pkt_type, bdaddr_t *dst) { struct hci_conn *conn; @@ -382,14 +383,22 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; break; case SCO_LINK: - if (lmp_esco_capable(hdev)) - conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | - (hdev->esco_type & EDR_ESCO_MASK); - else - conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; - break; + if (!pkt_type) + pkt_type = SCO_ESCO_MASK; case ESCO_LINK: - conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; + if (!pkt_type) + pkt_type = ALL_ESCO_MASK; + if (lmp_esco_capable(hdev)) { + /* HCI Setup Synchronous Connection Command uses + reverse logic on the EDR_ESCO_MASK bits */ + conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) & + hdev->esco_type; + } else { + /* Legacy HCI Add Sco Connection Command uses a + shifted bitmask */ + conn->pkt_type = (pkt_type << 5) & hdev->pkt_type & + SCO_PTYPE_MASK; + } break; } @@ -520,7 +529,7 @@ static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, if (le) return ERR_PTR(-EBUSY); - le = hci_conn_add(hdev, LE_LINK, dst); + le = hci_conn_add(hdev, LE_LINK, 0, dst); if (!le) return ERR_PTR(-ENOMEM); @@ -543,7 +552,7 @@ static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); if (!acl) { - acl = hci_conn_add(hdev, ACL_LINK, dst); + acl = hci_conn_add(hdev, ACL_LINK, 0, dst); if (!acl) return ERR_PTR(-ENOMEM); } @@ -561,7 +570,8 @@ static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, } static struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, - bdaddr_t *dst, u8 sec_level, u8 auth_type) + __u16 pkt_type, bdaddr_t *dst, + u8 sec_level, u8 auth_type) { struct hci_conn *acl; struct hci_conn *sco; @@ -572,7 +582,7 @@ static struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, sco = hci_conn_hash_lookup_ba(hdev, type, dst); if (!sco) { - sco = hci_conn_add(hdev, type, dst); + sco = hci_conn_add(hdev, type, pkt_type, dst); if (!sco) { hci_conn_drop(acl); return ERR_PTR(-ENOMEM); @@ -602,7 +612,8 @@ static struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, } /* Create SCO, ACL or LE connection. */ -struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, +struct hci_conn *hci_connect(struct hci_dev *hdev, int type, + __u16 pkt_type, bdaddr_t *dst, __u8 dst_type, __u8 sec_level, __u8 auth_type) { BT_DBG("%s dst %pMR type 0x%x", hdev->name, dst, type); @@ -614,7 +625,7 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, return hci_connect_acl(hdev, dst, sec_level, auth_type); case SCO_LINK: case ESCO_LINK: - return hci_connect_sco(hdev, type, dst, sec_level, auth_type); + return hci_connect_sco(hdev, type, pkt_type, dst, sec_level, auth_type); } return ERR_PTR(-EINVAL); @@ -883,6 +894,15 @@ int hci_get_conn_list(void __user *arg) (ci + n)->out = c->out; (ci + n)->state = c->state; (ci + n)->link_mode = c->link_mode; + if (c->type == SCO_LINK) { + (ci + n)->mtu = hdev->sco_mtu; + (ci + n)->cnt = hdev->sco_cnt; + (ci + n)->pkts = hdev->sco_pkts; + } else { + (ci + n)->mtu = hdev->acl_mtu; + (ci + n)->cnt = hdev->acl_cnt; + (ci + n)->pkts = hdev->acl_pkts; + } if (++n >= req.conn_num) break; } @@ -919,6 +939,15 @@ int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) ci.out = conn->out; ci.state = conn->state; ci.link_mode = conn->link_mode; + if (req.type == SCO_LINK) { + ci.mtu = hdev->sco_mtu; + ci.cnt = hdev->sco_cnt; + ci.pkts = hdev->sco_pkts; + } else { + ci.mtu = hdev->acl_mtu; + ci.cnt = hdev->acl_cnt; + ci.pkts = hdev->acl_pkts; + } } hci_dev_unlock(hdev); diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 7c88f5f83598..44ab823be060 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -33,6 +33,268 @@ #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> +#ifdef CONFIG_BT_RANDADDR +#define USE_MAC_FROM_RDA_NVRAM +#ifdef USE_MAC_FROM_RDA_NVRAM +#include <plat/md_sys.h> +struct bt_mac_info { + u16 activated; + u16 NAP; + u8 UAP; + u32 LAP; +}; +#define BT_MAC_ACTIVATED_FLAG 0x1d0e +#endif /*USE_MAC_FROM_RDA_NVRAM*/ +#define BT_NVRAM_FILE_NAME "/data/misc/bluetooth/BTMAC" +#endif + +#ifdef CONFIG_BT_RANDADDR + +#ifndef USE_MAC_FROM_RDA_NVRAM +static int nvram_read(char *filename, char *buf, ssize_t len, int offset) +{ + struct file *fd; + int retLen = -1; + + mm_segment_t old_fs = get_fs(); + set_fs(KERNEL_DS); + + fd = filp_open(filename, O_RDWR|O_CREAT, 0666); + + if(IS_ERR(fd)) { + printk("[bt][nvram_read] : failed to open fd = %d !!\n" ,(int)fd); + return -1; + } + + do{ + if ((fd->f_op == NULL) || (fd->f_op->read == NULL)){ + printk("[bt][nvram_read] : file can not be read!!\n"); + break; + } + + if (fd->f_pos != offset) { + if (fd->f_op->llseek){ + if(fd->f_op->llseek(fd, offset, 0) != offset){ + printk("[bt][nvram_read] : failed to seek!!\n"); + break; + } + } else { + fd->f_pos = offset; + } + } + + retLen = fd->f_op->read(fd, + buf, + len, + &fd->f_pos); + }while(false); + + filp_close(fd, NULL); + set_fs(old_fs); + printk("bt nv_read success \n"); + return retLen; +} + +static int nvram_write(char *filename, char *buf, ssize_t len, int offset) +{ + struct file *fd; + int retLen = -1; + + mm_segment_t old_fs = get_fs(); + set_fs(KERNEL_DS); + + fd = filp_open(filename, O_RDWR|O_CREAT, 0666); + + if(IS_ERR(fd)){ + printk("[bt][nvram_write] : failed to open!! fd = %d \n", (int)fd); + return -1; + } + + do{ + if ((fd->f_op == NULL) || (fd->f_op->write == NULL)){ + printk("[bt][nvram_write] : file can not be write!!\n"); + break; + } /* End of if */ + + if (fd->f_pos != offset){ + if (fd->f_op->llseek){ + if(fd->f_op->llseek(fd, offset, 0) != offset){ + printk("[rda5890][nvram_write] : failed to seek!!\n"); + break; + } + } else { + fd->f_pos = offset; + } + } + + retLen = fd->f_op->write(fd, + buf, + len, + &fd->f_pos); + }while(false); + + filp_close(fd, NULL); + set_fs(old_fs); + printk("bt nv_write success \n"); + return retLen; +} +#endif /*USE_MAC_FROM_RDA_NVRAM*/ + +#ifdef USE_MAC_FROM_RDA_NVRAM +int bt_read_mac_from_nvram(char *buf) +{ + int ret; + struct msys_device *bt_msys = NULL; + struct bt_mac_info bt_info; + struct client_cmd cmd_set; + int retry=3; + + bt_msys = rda_msys_alloc_device(); + if (!bt_msys) { + printk(KERN_ERR "nvram: can not allocate bt_msys device\n"); + ret = -ENOMEM; + goto err_handle_sys; + } + + bt_msys->module = SYS_GEN_MOD; + bt_msys->name = "rda-bt"; + rda_msys_register_device(bt_msys); + + memset(&bt_info, sizeof(bt_info), 0); + cmd_set.pmsys_dev = bt_msys; + cmd_set.mod_id = SYS_GEN_MOD; + cmd_set.mesg_id = SYS_GEN_CMD_GET_BT_INFO; + cmd_set.pdata = NULL; + cmd_set.data_size = 0; + cmd_set.pout_data = &bt_info; + cmd_set.out_size = sizeof(bt_info); + + while (retry--) { + ret = rda_msys_send_cmd(&cmd_set); + if (ret) { + printk(KERN_ERR "nvram:can not get bt mac from nvram \n"); + ret = -EBUSY; + } else { + break; + } + } + + if (ret == -EBUSY) { + goto err_handle_cmd; + } + + if (bt_info.activated != BT_MAC_ACTIVATED_FLAG) { + printk(KERN_ERR "nvram:get invalid bt mac address from nvram\n"); + ret = -EINVAL; + goto err_invalid_mac; + } + + buf[0] = (bt_info.NAP >> 8) & 0xff; + buf[1] = bt_info.NAP & 0xff; + buf[2] = bt_info.UAP; + buf[3] = (bt_info.LAP >> 16) & 0xff; + buf[4] = (bt_info.LAP >> 8) & 0xff; + buf[5] = bt_info.LAP & 0xff; + + printk(KERN_ERR "nvram:get bt mac address [%02x:%02x:%02x:%02x:%02x:%02x] from nvram success.\n", + buf[0], buf[1], buf[2], + buf[3], buf[4], buf[5]); + ret = 0; /*success*/ + +err_invalid_mac: +err_handle_cmd: + rda_msys_unregister_device(bt_msys); + rda_msys_free_device(bt_msys); +err_handle_sys: + return ret; +} + +int bt_write_mac_to_nvram(const char *buf) +{ + int ret; + struct msys_device *bt_msys = NULL; + struct bt_mac_info bt_info; + struct client_cmd cmd_set; + + bt_msys = rda_msys_alloc_device(); + if (!bt_msys) { + printk(KERN_ERR "nvram: can not allocate bt_msys device\n"); + ret = -ENOMEM; + goto err_handle_sys; + } + + bt_msys->module = SYS_GEN_MOD; + bt_msys->name = "rda-bt"; + rda_msys_register_device(bt_msys); + + memset(&bt_info, sizeof(bt_info), 0); + bt_info.activated = BT_MAC_ACTIVATED_FLAG; + bt_info.NAP = 0x0000 | (buf[0]|0x0000)<<8 | (buf[1]|0x0000); + bt_info.UAP = buf[2]; + bt_info.LAP = 0x00000000 | (buf[3]|0x00000000)<<16 | (buf[4]|0x00000000)<<8 | (buf[5]|0x00000000); + + cmd_set.pmsys_dev = bt_msys; + cmd_set.mod_id = SYS_GEN_MOD; + cmd_set.mesg_id = SYS_GEN_CMD_SET_BT_INFO; + cmd_set.pdata = &bt_info; + cmd_set.data_size = sizeof(bt_info); + cmd_set.pout_data = NULL; + cmd_set.out_size = 0; + + ret = rda_msys_send_cmd(&cmd_set); + if (ret) { + printk(KERN_ERR "nvram:can not set bt mac to nvram \n"); + ret = -EBUSY; + goto err_handle_cmd; + } + + printk(KERN_ERR "nvram:set bt mac address [%02x:%02x:%02x:%02x:%02x:%02x] to nvram success.\n", + buf[0], buf[1], buf[2], + buf[3], buf[4], buf[5]); + ret = 0; /*success*/ + +err_handle_cmd: + rda_msys_unregister_device(bt_msys); + rda_msys_free_device(bt_msys); +err_handle_sys: + return ret; +} + +#endif /*USE_MAC_FROM_RDA_NVRAM*/ + +void bt_get_random_address(char *buf) +{ + static unsigned char has_create_addr_success = 0; + static __u8 address[] = {0x00, 0x00, 0x00, 0x00, 0x90,0x59}; + + if(!has_create_addr_success) + { +#ifdef USE_MAC_FROM_RDA_NVRAM + int ret; + ret = bt_read_mac_from_nvram(address); + if (ret) { + printk(KERN_ERR "nvram:get a random bt address\n"); + get_random_bytes(address, 6); + if (ret == -EINVAL) + bt_write_mac_to_nvram(address); + } +#else + if(nvram_read(BT_NVRAM_FILE_NAME, address, 6, 0) != 6) + { + get_random_bytes(address, 6); + nvram_write(BT_NVRAM_FILE_NAME, address, 6, 0); + } +#endif /* USE_MAC_FROM_RDA_NVRAM */ + has_create_addr_success = 1; + } + + memcpy(buf, address, sizeof(address)); +} + +EXPORT_SYMBOL(bt_get_random_address); + +#endif + static void hci_rx_work(struct work_struct *work); static void hci_cmd_work(struct work_struct *work); static void hci_tx_work(struct work_struct *work); diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 5daf7ab26710..1526fb232b3f 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -1119,7 +1119,7 @@ static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status) } } else { if (!conn) { - conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr); + conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr); if (conn) { conn->out = true; conn->link_mode |= HCI_LM_MASTER; @@ -1748,6 +1748,15 @@ unlock: hci_conn_check_pending(hdev); } +static inline bool is_sco_active(struct hci_dev *hdev) +{ + if (hci_conn_hash_lookup_state(hdev, SCO_LINK, BT_CONNECTED) || + (hci_conn_hash_lookup_state(hdev, ESCO_LINK, + BT_CONNECTED))) + return true; + return false; +} + static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) { struct hci_ev_conn_request *ev = (void *) skb->data; @@ -1775,7 +1784,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr); if (!conn) { - conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr); + /* pkt_type not yet used for incoming connections */ + conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr); if (!conn) { BT_ERR("No memory for new connection"); hci_dev_unlock(hdev); @@ -1794,7 +1804,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) bacpy(&cp.bdaddr, &ev->bdaddr); - if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER)) + if (lmp_rswitch_capable(hdev) && ((mask & HCI_LM_MASTER) + || is_sco_active(hdev))) cp.role = 0x00; /* Become master */ else cp.role = 0x01; /* Remain slave */ @@ -2963,6 +2974,7 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev, hci_conn_add_sysfs(conn); break; + case 0x10: /* Connection Accept Timeout */ case 0x11: /* Unsupported Feature or Parameter Value */ case 0x1c: /* SCO interval rejected */ case 0x1a: /* Unsupported Remote Feature */ @@ -3540,7 +3552,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); if (!conn) { - conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr); + conn = hci_conn_add(hdev, LE_LINK, 0, &ev->bdaddr); if (!conn) { BT_ERR("No memory for new connection"); goto unlock; diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 68843a28a7af..c11a28bae844 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -1793,10 +1793,10 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, auth_type = l2cap_get_auth_type(chan); if (chan->dcid == L2CAP_CID_LE_DATA) - hcon = hci_connect(hdev, LE_LINK, dst, dst_type, + hcon = hci_connect(hdev, LE_LINK, 0, dst, dst_type, chan->sec_level, auth_type); else - hcon = hci_connect(hdev, ACL_LINK, dst, dst_type, + hcon = hci_connect(hdev, ACL_LINK, 0, dst, dst_type, chan->sec_level, auth_type); if (IS_ERR(hcon)) { diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 8208a13a9837..3e574540b2c2 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -2205,10 +2205,10 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, auth_type = HCI_AT_DEDICATED_BONDING_MITM; if (cp->addr.type == BDADDR_BREDR) - conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, + conn = hci_connect(hdev, ACL_LINK, 0, &cp->addr.bdaddr, cp->addr.type, sec_level, auth_type); else - conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, + conn = hci_connect(hdev, LE_LINK, 0, &cp->addr.bdaddr, cp->addr.type, sec_level, auth_type); if (IS_ERR(conn)) { |