Skip to content

Commit 00398e1

Browse files
Alain Michaudholtmann
authored andcommitted
Bluetooth: Add support for BT_PKT_STATUS CMSG data for SCO connections
This change adds support for reporting the BT_PKT_STATUS to the socket CMSG data to allow the implementation of a packet loss correction on erroneous data received on the SCO socket. The patch was partially developed by Marcel Holtmann and validated by Hsin-yu Chao. Signed-off-by: Alain Michaud <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent e660b35 commit 00398e1

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

include/net/bluetooth/bluetooth.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ struct bt_voice {
147147
#define BT_MODE_LE_FLOWCTL 0x03
148148
#define BT_MODE_EXT_FLOWCTL 0x04
149149

150+
#define BT_PKT_STATUS 16
151+
152+
#define BT_SCM_PKT_STATUS 0x03
153+
150154
__printf(1, 2)
151155
void bt_info(const char *fmt, ...);
152156
__printf(1, 2)
@@ -286,6 +290,7 @@ struct bt_sock {
286290
struct sock *parent;
287291
unsigned long flags;
288292
void (*skb_msg_name)(struct sk_buff *, void *, int *);
293+
void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
289294
};
290295

291296
enum {
@@ -335,6 +340,10 @@ struct l2cap_ctrl {
335340
struct l2cap_chan *chan;
336341
};
337342

343+
struct sco_ctrl {
344+
u8 pkt_status;
345+
};
346+
338347
struct hci_dev;
339348

340349
typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
@@ -361,6 +370,7 @@ struct bt_skb_cb {
361370
u8 incoming:1;
362371
union {
363372
struct l2cap_ctrl l2cap;
373+
struct sco_ctrl sco;
364374
struct hci_ctrl hci;
365375
};
366376
};

include/net/bluetooth/sco.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ struct sco_conninfo {
4646
__u8 dev_class[3];
4747
};
4848

49+
#define SCO_CMSG_PKT_STATUS 0x01
50+
4951
#endif /* __SCO_H */

net/bluetooth/af_bluetooth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
286286
if (msg->msg_name && bt_sk(sk)->skb_msg_name)
287287
bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
288288
&msg->msg_namelen);
289+
290+
if (bt_sk(sk)->skb_put_cmsg)
291+
bt_sk(sk)->skb_put_cmsg(skb, msg, sk);
289292
}
290293

291294
skb_free_datagram(sk, skb);

net/bluetooth/hci_core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,7 @@ static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
45544554

45554555
if (conn) {
45564556
/* Send to upper protocol */
4557+
bt_cb(skb)->sco.pkt_status = flags & 0x03;
45574558
sco_recv_scodata(conn, skb);
45584559
return;
45594560
} else {

net/bluetooth/sco.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct sco_pinfo {
6666
bdaddr_t dst;
6767
__u32 flags;
6868
__u16 setting;
69+
__u8 cmsg_mask;
6970
struct sco_conn *conn;
7071
};
7172

@@ -449,6 +450,15 @@ static void sco_sock_close(struct sock *sk)
449450
sco_sock_kill(sk);
450451
}
451452

453+
static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg,
454+
struct sock *sk)
455+
{
456+
if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS)
457+
put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS,
458+
sizeof(bt_cb(skb)->sco.pkt_status),
459+
&bt_cb(skb)->sco.pkt_status);
460+
}
461+
452462
static void sco_sock_init(struct sock *sk, struct sock *parent)
453463
{
454464
BT_DBG("sk %p", sk);
@@ -457,6 +467,8 @@ static void sco_sock_init(struct sock *sk, struct sock *parent)
457467
sk->sk_type = parent->sk_type;
458468
bt_sk(sk)->flags = bt_sk(parent)->flags;
459469
security_sk_clone(parent, sk);
470+
} else {
471+
bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg;
460472
}
461473
}
462474

@@ -846,6 +858,18 @@ static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
846858
sco_pi(sk)->setting = voice.setting;
847859
break;
848860

861+
case BT_PKT_STATUS:
862+
if (get_user(opt, (u32 __user *)optval)) {
863+
err = -EFAULT;
864+
break;
865+
}
866+
867+
if (opt)
868+
sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
869+
else
870+
sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
871+
break;
872+
849873
default:
850874
err = -ENOPROTOOPT;
851875
break;
@@ -923,6 +947,7 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
923947
int len, err = 0;
924948
struct bt_voice voice;
925949
u32 phys;
950+
int pkt_status;
926951

927952
BT_DBG("sk %p", sk);
928953

@@ -969,6 +994,13 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
969994
err = -EFAULT;
970995
break;
971996

997+
case BT_PKT_STATUS:
998+
pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
999+
1000+
if (put_user(pkt_status, (int __user *)optval))
1001+
err = -EFAULT;
1002+
break;
1003+
9721004
default:
9731005
err = -ENOPROTOOPT;
9741006
break;

0 commit comments

Comments
 (0)