From 287c91dfbd55defbb2080d34cc6b386f7968be2d Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 17 Mar 2011 12:17:30 +0100 Subject: bluetooth: Add SCO parameters support for socket Adds ability to setup SCO connection parameters through socket option. ST-Ericsson Linux next: Not tested, ER 256277 ST-Ericsson ID: 256277 ST-Ericsson FOSS-OUT ID: STETL-FOSS-OUT-10137 Change-Id: If859341019fd9f7cc7c0efe95750779e84fac801 Signed-off-by: Andrzej Kaczmarek Reviewed-on: http://gerrit.lud.stericsson.com/gerrit/30136 Reviewed-by: Par-Gunnar HJALMDAHL Tested-by: Par-Gunnar HJALMDAHL Reviewed-by: Srinidhi KASAGAR Signed-off-by: Robert Marklund --- include/net/bluetooth/bluetooth.h | 12 +++++++ include/net/bluetooth/hci.h | 17 ++++++++++ include/net/bluetooth/hci_core.h | 9 +++-- include/net/bluetooth/sco.h | 4 ++- net/bluetooth/hci_conn.c | 47 +++++++++++++++++++------- net/bluetooth/hci_event.c | 6 ++-- net/bluetooth/l2cap_core.c | 4 +-- net/bluetooth/lib.c | 2 ++ net/bluetooth/mgmt.c | 2 +- net/bluetooth/sco.c | 70 ++++++++++++++++++++++++++++++++++++++- 10 files changed, 151 insertions(+), 22 deletions(-) diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h index e727555d4ee..496ba079e05 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -84,6 +84,18 @@ int bt_printk(const char *level, const char *fmt, ...); #define BT_ERR(fmt, arg...) bt_printk(KERN_ERR, pr_fmt(fmt), ##arg) #define BT_DBG(fmt, arg...) pr_debug(fmt "\n", ##arg) +#define BT_SCO_PARAMETERS 8 +struct bt_sco_parameters { + __u32 tx_bandwidth; + __u32 rx_bandwidth; + __u16 max_latency; + __u16 voice_setting; + __u8 retrans_effort; + __u16 pkt_type; +} __packed; + +#define BT_NO_AUTORETRY 9 + /* Connection and socket states */ enum { BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index be30aabe7b8..a1b00e10019 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -159,6 +159,8 @@ enum { #define SCO_ESCO_MASK (ESCO_HV1 | ESCO_HV2 | ESCO_HV3) #define EDR_ESCO_MASK (ESCO_2EV3 | ESCO_3EV3 | ESCO_2EV5 | ESCO_3EV5) +#define ALL_ESCO_MASK (SCO_ESCO_MASK | ESCO_EV3 | ESCO_EV4 | ESCO_EV5 | \ + EDR_ESCO_MASK) /* ACL flags */ #define ACL_START_NO_FLUSH 0x00 @@ -410,6 +412,21 @@ struct hci_cp_setup_sync_conn { __le16 pkt_type; } __packed; +/* Air coding format types */ +#define HCI_SYNC_AIR_CODING_CVSD 0x00 +#define HCI_SYNC_AIR_CODING_ULAW 0x01 +#define HCI_SYNC_AIR_CODING_ALAW 0x02 +#define HCI_SYNC_AIR_CODING_TRANSPARENT 0x03 + +/* Max latency constants */ +#define HCI_SYNC_MAX_LATENCY_DONTCARE 0xffff + +/* Retransmission effort constants */ +#define HCI_SYNC_RETRANS_EFFORT_NO 0x00 +#define HCI_SYNC_RETRANS_EFFORT_POWER 0x01 +#define HCI_SYNC_RETRANS_EFFORT_QUALITY 0x02 +#define HCI_SYNC_RETRANS_EFFORT_DONTCARE 0xff + #define HCI_OP_ACCEPT_SYNC_CONN_REQ 0x0429 struct hci_cp_accept_sync_conn_req { bdaddr_t bdaddr; diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 8f441b8b296..4f6a6cea32e 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -250,6 +250,7 @@ struct hci_conn { __u8 type; __u8 out; __u8 attempt; + __u8 no_autoretry; __u8 dev_class[3]; __u8 features[8]; __u8 ssp_mode; @@ -290,6 +291,8 @@ struct hci_conn { void *l2cap_data; void *sco_data; + struct bt_sco_parameters *sco_parameters; + struct hci_conn *link; void (*connect_cfm_cb) (struct hci_conn *conn, u8 status); @@ -446,13 +449,15 @@ void hci_add_sco(struct hci_conn *conn, __u16 handle); void hci_setup_sync(struct hci_conn *conn, __u16 handle); void hci_sco_setup(struct hci_conn *conn, __u8 status); -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, + bdaddr_t *dst); int hci_conn_del(struct hci_conn *conn); void hci_conn_hash_flush(struct hci_dev *hdev); void hci_conn_check_pending(struct hci_dev *hdev); struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, - __u8 sec_level, __u8 auth_type); + __u8 sec_level, __u8 auth_type, + struct bt_sco_parameters *sco_parameters); int hci_conn_check_link_mode(struct hci_conn *conn); int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level); int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type); diff --git a/include/net/bluetooth/sco.h b/include/net/bluetooth/sco.h index 1e35c43657c..e6b5a94f7e8 100644 --- a/include/net/bluetooth/sco.h +++ b/include/net/bluetooth/sco.h @@ -72,7 +72,9 @@ struct sco_conn { struct sco_pinfo { struct bt_sock bt; - __u32 flags; + struct bt_sco_parameters param; + __u8 no_autoretry; + struct sco_conn *conn; }; diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index ea7f031f3b0..f170bbbdcf0 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -44,6 +44,7 @@ #include #include +#include static void hci_le_connect(struct hci_conn *conn) { @@ -147,16 +148,21 @@ void hci_add_sco(struct hci_conn *conn, __u16 handle) { struct hci_dev *hdev = conn->hdev; struct hci_cp_add_sco cp; + struct bt_sco_parameters *p = conn->sco_parameters; + __u16 pkt_type; BT_DBG("%p", conn); + /* HCI_Add_SCO_Connection uses shifted bitmask for packet type */ + pkt_type = (p->pkt_type << 5) & conn->pkt_type; + conn->state = BT_CONNECT; conn->out = 1; conn->attempt++; cp.handle = cpu_to_le16(handle); - cp.pkt_type = cpu_to_le16(conn->pkt_type); + cp.pkt_type = cpu_to_le16(pkt_type); hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); } @@ -165,22 +171,35 @@ void hci_setup_sync(struct hci_conn *conn, __u16 handle) { struct hci_dev *hdev = conn->hdev; struct hci_cp_setup_sync_conn cp; + struct bt_sco_parameters *p = conn->sco_parameters; + __u16 voice_setting; + __u16 pkt_type; BT_DBG("%p", conn); + /* + * Combine voice setting using device parameters and air coding + * format set by user. + */ + voice_setting = (hdev->voice_setting & 0xfffc) | + (p->voice_setting & 0x0003); + + /* Bits for EDR packets have inverted logic in BT spec. */ + pkt_type = (p->pkt_type & conn->pkt_type) ^ EDR_ESCO_MASK; + conn->state = BT_CONNECT; conn->out = 1; conn->attempt++; cp.handle = cpu_to_le16(handle); - cp.pkt_type = cpu_to_le16(conn->pkt_type); - cp.tx_bandwidth = cpu_to_le32(0x00001f40); - cp.rx_bandwidth = cpu_to_le32(0x00001f40); - cp.max_latency = cpu_to_le16(0xffff); - cp.voice_setting = cpu_to_le16(hdev->voice_setting); - cp.retrans_effort = 0xff; + cp.tx_bandwidth = cpu_to_le32(p->tx_bandwidth); + cp.rx_bandwidth = cpu_to_le32(p->rx_bandwidth); + cp.max_latency = cpu_to_le16(p->max_latency); + cp.voice_setting = cpu_to_le16(voice_setting); + cp.retrans_effort = p->retrans_effort; + cp.pkt_type = cpu_to_le16(pkt_type); hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); } @@ -333,7 +352,8 @@ static void hci_conn_auto_accept(unsigned long arg) hci_dev_unlock(hdev); } -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, + bdaddr_t *dst) { struct hci_conn *conn; @@ -362,13 +382,12 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) break; case SCO_LINK: if (lmp_esco_capable(hdev)) - conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | - (hdev->esco_type & EDR_ESCO_MASK); + conn->pkt_type = hdev->esco_type & SCO_ESCO_MASK; else conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; break; case ESCO_LINK: - conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; + conn->pkt_type = hdev->esco_type; break; } @@ -492,7 +511,9 @@ EXPORT_SYMBOL(hci_get_route); /* Create SCO, ACL or LE connection. * Device _must_ be locked */ -struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type) +struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, + __u8 sec_level, __u8 auth_type, + struct bt_sco_parameters *sco_parameters) { struct hci_conn *acl; struct hci_conn *sco; @@ -557,6 +578,8 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 hci_conn_hold(sco); + sco->sco_parameters = sco_parameters; + if (acl->state == BT_CONNECTED && (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { acl->power_save = 1; diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 7ef4eb4435f..d7e7ffecc46 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -1486,6 +1486,7 @@ static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *sk conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr); if (!conn) { + /* pkt_type not yet used for incoming connections */ conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr); if (!conn) { BT_ERR("No memory for new connection"); @@ -2483,9 +2484,8 @@ static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_bu case 0x1c: /* SCO interval rejected */ case 0x1a: /* Unsupported Remote Feature */ case 0x1f: /* Unspecified error */ - if (conn->out && conn->attempt < 2) { - conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | - (hdev->esco_type & EDR_ESCO_MASK); + if (conn->out && !conn->no_autoretry && conn->attempt < 2) { + conn->pkt_type = hdev->esco_type & SCO_ESCO_MASK; hci_setup_sync(conn, conn->link->handle); goto unlock; } diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index b3bdb482bbe..bd39d4af608 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -1109,10 +1109,10 @@ int l2cap_chan_connect(struct l2cap_chan *chan) if (chan->dcid == L2CAP_CID_LE_DATA) hcon = hci_connect(hdev, LE_LINK, dst, - chan->sec_level, auth_type); + chan->sec_level, auth_type, NULL); else hcon = hci_connect(hdev, ACL_LINK, dst, - chan->sec_level, auth_type); + chan->sec_level, auth_type, NULL); if (IS_ERR(hcon)) { err = PTR_ERR(hcon); diff --git a/net/bluetooth/lib.c b/net/bluetooth/lib.c index 86a6bed229d..f9b2dde4c0a 100644 --- a/net/bluetooth/lib.c +++ b/net/bluetooth/lib.c @@ -136,6 +136,8 @@ int bt_to_errno(__u16 code) return EPROTONOSUPPORT; case 0x1b: + case 0x1c: + case 0x1d: return ECONNREFUSED; case 0x19: diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 53e109eb043..9d8a5bd20a6 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -1372,7 +1372,7 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) auth_type = HCI_AT_DEDICATED_BONDING_MITM; } - conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, auth_type); + conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, auth_type, NULL); if (IS_ERR(conn)) { err = PTR_ERR(conn); goto unlock; diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index a324b009e34..d65ec5f7327 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -178,6 +178,7 @@ static int sco_connect(struct sock *sk) { bdaddr_t *src = &bt_sk(sk)->src; bdaddr_t *dst = &bt_sk(sk)->dst; + struct bt_sco_parameters *param = &sco_pi(sk)->param; struct sco_conn *conn; struct hci_conn *hcon; struct hci_dev *hdev; @@ -196,12 +197,15 @@ static int sco_connect(struct sock *sk) else type = SCO_LINK; - hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); + hcon = hci_connect(hdev, type, dst, + BT_SECURITY_LOW, HCI_AT_NO_BONDING, param); if (IS_ERR(hcon)) { err = PTR_ERR(hcon); goto done; } + hcon->no_autoretry = sco_pi(sk)->no_autoretry; + conn = sco_conn_add(hcon, 0); if (!conn) { hci_conn_put(hcon); @@ -402,12 +406,26 @@ static void sco_sock_close(struct sock *sk) static void sco_sock_init(struct sock *sk, struct sock *parent) { + struct sco_pinfo *pi = sco_pi(sk); + BT_DBG("sk %p", sk); if (parent) { sk->sk_type = parent->sk_type; security_sk_clone(parent, sk); } + + pi->param.tx_bandwidth = 8000; + pi->param.rx_bandwidth = 8000; + pi->param.max_latency = HCI_SYNC_MAX_LATENCY_DONTCARE; + + /* Only Air Coding Format matters here, other data will be + * overriden by device settings during connection setup. + */ + pi->param.voice_setting = HCI_SYNC_AIR_CODING_CVSD; + + pi->param.retrans_effort = HCI_SYNC_RETRANS_EFFORT_DONTCARE; + pi->param.pkt_type = ALL_ESCO_MASK; } static struct proto sco_proto = { @@ -661,13 +679,45 @@ static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) { struct sock *sk = sock->sk; + int len; int err = 0; + struct bt_sco_parameters *param; + u32 opt; BT_DBG("sk %p", sk); + if (level != SOL_BLUETOOTH) + return -ENOPROTOOPT; + lock_sock(sk); switch (optname) { + case BT_SCO_PARAMETERS: + /* We do not support changing SCO parameters during + * connection. + */ + if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { + err = -EBUSY; + break; + } + + param = &sco_pi(sk)->param; + + len = min_t(unsigned int, sizeof(*param), optlen); + if (copy_from_user((char *) param, optval, len)) + err = -EFAULT; + + break; + + case BT_NO_AUTORETRY: + if (get_user(opt, (u32 __user *) optval)) { + err = -EFAULT; + break; + } + + sco_pi(sk)->no_autoretry = opt; + break; + default: err = -ENOPROTOOPT; break; @@ -737,18 +787,36 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char { struct sock *sk = sock->sk; int len, err = 0; + struct bt_sco_parameters *params; BT_DBG("sk %p", sk); if (level == SOL_SCO) return sco_sock_getsockopt_old(sock, optname, optval, optlen); + if (level != SOL_BLUETOOTH) + return -ENOPROTOOPT; + if (get_user(len, optlen)) return -EFAULT; lock_sock(sk); switch (optname) { + case BT_SCO_PARAMETERS: + params = &sco_pi(sk)->param; + + len = min_t(unsigned int, len, sizeof(*params)); + if (copy_to_user(optval, (char *) params, len)) + err = -EFAULT; + + break; + + case BT_NO_AUTORETRY: + if (put_user(sco_pi(sk)->no_autoretry, (u32 __user *) optval)) + err = -EFAULT; + break; + default: err = -ENOPROTOOPT; break; -- cgit v1.2.3