hci_conn.c (a86ddbffa6ed05bc2465a545a96627b6e776c019) hci_conn.c (26afbd826ee326e63a334c37fd45e82e50a615ec)
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

--- 111 unchanged lines hidden (view full) ---

120
121 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
122 hci_remove_link_key(hdev, &conn->dst);
123
124 hci_chan_list_flush(conn);
125
126 hci_conn_hash_del(hdev, conn);
127
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

--- 111 unchanged lines hidden (view full) ---

120
121 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
122 hci_remove_link_key(hdev, &conn->dst);
123
124 hci_chan_list_flush(conn);
125
126 hci_conn_hash_del(hdev, conn);
127
128 if (conn->cleanup)
129 conn->cleanup(conn);
130
128 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
129 switch (conn->setting & SCO_AIRMODE_MASK) {
130 case SCO_AIRMODE_CVSD:
131 case SCO_AIRMODE_TRANSP:
132 if (hdev->notify)
133 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
134 break;
135 }

--- 581 unchanged lines hidden (view full) ---

717 if (conn->role == HCI_ROLE_MASTER)
718 conn->out = true;
719
720 switch (type) {
721 case ACL_LINK:
722 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
723 break;
724 case LE_LINK:
131 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
132 switch (conn->setting & SCO_AIRMODE_MASK) {
133 case SCO_AIRMODE_CVSD:
134 case SCO_AIRMODE_TRANSP:
135 if (hdev->notify)
136 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
137 break;
138 }

--- 581 unchanged lines hidden (view full) ---

720 if (conn->role == HCI_ROLE_MASTER)
721 conn->out = true;
722
723 switch (type) {
724 case ACL_LINK:
725 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
726 break;
727 case LE_LINK:
728 case ISO_LINK:
725 /* conn->src should reflect the local identity address */
726 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
727 break;
728 case SCO_LINK:
729 if (lmp_esco_capable(hdev))
730 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
731 (hdev->esco_type & EDR_ESCO_MASK);
732 else

--- 494 unchanged lines hidden (view full) ---

1227 }
1228
1229 hci_sco_setup(acl, 0x00);
1230 }
1231
1232 return sco;
1233}
1234
729 /* conn->src should reflect the local identity address */
730 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
731 break;
732 case SCO_LINK:
733 if (lmp_esco_capable(hdev))
734 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
735 (hdev->esco_type & EDR_ESCO_MASK);
736 else

--- 494 unchanged lines hidden (view full) ---

1231 }
1232
1233 hci_sco_setup(acl, 0x00);
1234 }
1235
1236 return sco;
1237}
1238
1239struct iso_list_data {
1240 u8 cig;
1241 u8 cis;
1242 int count;
1243 struct {
1244 struct hci_cp_le_set_cig_params cp;
1245 struct hci_cis_params cis[0x11];
1246 } pdu;
1247};
1248
1249static void cis_add(struct iso_list_data *d, struct bt_iso_qos *qos)
1250{
1251 struct hci_cis_params *cis = &d->pdu.cis[d->pdu.cp.num_cis];
1252
1253 cis->cis_id = qos->cis;
1254 cis->c_sdu = cpu_to_le16(qos->out.sdu);
1255 cis->p_sdu = cpu_to_le16(qos->in.sdu);
1256 cis->c_phy = qos->out.phy;
1257 cis->p_phy = qos->in.phy;
1258 cis->c_rtn = qos->out.rtn;
1259 cis->p_rtn = qos->in.rtn;
1260
1261 d->pdu.cp.num_cis++;
1262}
1263
1264static void cis_list(struct hci_conn *conn, void *data)
1265{
1266 struct iso_list_data *d = data;
1267
1268 if (d->cig != conn->iso_qos.cig || d->cis == BT_ISO_QOS_CIS_UNSET ||
1269 d->cis != conn->iso_qos.cis)
1270 return;
1271
1272 d->count++;
1273
1274 if (d->pdu.cp.cig_id == BT_ISO_QOS_CIG_UNSET ||
1275 d->count >= ARRAY_SIZE(d->pdu.cis))
1276 return;
1277
1278 cis_add(d, &conn->iso_qos);
1279}
1280
1281static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
1282{
1283 struct hci_dev *hdev = conn->hdev;
1284 struct iso_list_data data;
1285
1286 memset(&data, 0, sizeof(data));
1287
1288 /* Allocate a CIG if not set */
1289 if (qos->cig == BT_ISO_QOS_CIG_UNSET) {
1290 for (data.cig = 0x00; data.cig < 0xff; data.cig++) {
1291 data.count = 0;
1292 data.cis = 0xff;
1293
1294 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK,
1295 BT_BOUND, &data);
1296 if (data.count)
1297 continue;
1298
1299 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK,
1300 BT_CONNECTED, &data);
1301 if (!data.count)
1302 break;
1303 }
1304
1305 if (data.cig == 0xff)
1306 return false;
1307
1308 /* Update CIG */
1309 qos->cig = data.cig;
1310 }
1311
1312 data.pdu.cp.cig_id = qos->cig;
1313 hci_cpu_to_le24(qos->out.interval, data.pdu.cp.c_interval);
1314 hci_cpu_to_le24(qos->in.interval, data.pdu.cp.p_interval);
1315 data.pdu.cp.sca = qos->sca;
1316 data.pdu.cp.packing = qos->packing;
1317 data.pdu.cp.framing = qos->framing;
1318 data.pdu.cp.c_latency = cpu_to_le16(qos->out.latency);
1319 data.pdu.cp.p_latency = cpu_to_le16(qos->in.latency);
1320
1321 if (qos->cis != BT_ISO_QOS_CIS_UNSET) {
1322 data.count = 0;
1323 data.cig = qos->cig;
1324 data.cis = qos->cis;
1325
1326 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, BT_BOUND,
1327 &data);
1328 if (data.count)
1329 return false;
1330
1331 cis_add(&data, qos);
1332 }
1333
1334 /* Reprogram all CIS(s) with the same CIG */
1335 for (data.cig = qos->cig, data.cis = 0x00; data.cis < 0x11;
1336 data.cis++) {
1337 data.count = 0;
1338
1339 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, BT_BOUND,
1340 &data);
1341 if (data.count)
1342 continue;
1343
1344 /* Allocate a CIS if not set */
1345 if (qos->cis == BT_ISO_QOS_CIS_UNSET) {
1346 /* Update CIS */
1347 qos->cis = data.cis;
1348 cis_add(&data, qos);
1349 }
1350 }
1351
1352 if (qos->cis == BT_ISO_QOS_CIS_UNSET || !data.pdu.cp.num_cis)
1353 return false;
1354
1355 if (hci_send_cmd(hdev, HCI_OP_LE_SET_CIG_PARAMS,
1356 sizeof(data.pdu.cp) +
1357 (data.pdu.cp.num_cis * sizeof(*data.pdu.cis)),
1358 &data.pdu) < 0)
1359 return false;
1360
1361 return true;
1362}
1363
1364static void find_cis(struct hci_conn *conn, void *data)
1365{
1366 struct iso_list_data *d = data;
1367
1368 /* Ignore broadcast */
1369 if (!bacmp(&conn->dst, BDADDR_ANY))
1370 return;
1371
1372 d->count++;
1373}
1374
1375static int remove_cig_sync(struct hci_dev *hdev, void *data)
1376{
1377 u8 handle = PTR_ERR(data);
1378
1379 return hci_le_remove_cig_sync(hdev, handle);
1380}
1381
1382int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
1383{
1384 bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
1385
1386 return hci_cmd_sync_queue(hdev, remove_cig_sync, ERR_PTR(handle), NULL);
1387}
1388
1389static void cis_cleanup(struct hci_conn *conn)
1390{
1391 struct hci_dev *hdev = conn->hdev;
1392 struct iso_list_data d;
1393
1394 memset(&d, 0, sizeof(d));
1395 d.cig = conn->iso_qos.cig;
1396
1397 /* Check if ISO connection is a CIS and remove CIG if there are
1398 * no other connections using it.
1399 */
1400 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d);
1401 if (d.count)
1402 return;
1403
1404 hci_le_remove_cig(hdev, conn->iso_qos.cig);
1405}
1406
1407struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1408 __u8 dst_type, struct bt_iso_qos *qos)
1409{
1410 struct hci_conn *cis;
1411
1412 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type);
1413 if (!cis) {
1414 cis = hci_conn_add(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
1415 if (!cis)
1416 return ERR_PTR(-ENOMEM);
1417 cis->cleanup = cis_cleanup;
1418 }
1419
1420 if (cis->state == BT_CONNECTED)
1421 return cis;
1422
1423 /* Check if CIS has been set and the settings matches */
1424 if (cis->state == BT_BOUND &&
1425 !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
1426 return cis;
1427
1428 /* Update LINK PHYs according to QoS preference */
1429 cis->le_tx_phy = qos->out.phy;
1430 cis->le_rx_phy = qos->in.phy;
1431
1432 /* If output interval is not set use the input interval as it cannot be
1433 * 0x000000.
1434 */
1435 if (!qos->out.interval)
1436 qos->out.interval = qos->in.interval;
1437
1438 /* If input interval is not set use the output interval as it cannot be
1439 * 0x000000.
1440 */
1441 if (!qos->in.interval)
1442 qos->in.interval = qos->out.interval;
1443
1444 /* If output latency is not set use the input latency as it cannot be
1445 * 0x0000.
1446 */
1447 if (!qos->out.latency)
1448 qos->out.latency = qos->in.latency;
1449
1450 /* If input latency is not set use the output latency as it cannot be
1451 * 0x0000.
1452 */
1453 if (!qos->in.latency)
1454 qos->in.latency = qos->out.latency;
1455
1456 /* Mirror PHYs that are disabled as SDU will be set to 0 */
1457 if (!qos->in.phy)
1458 qos->in.phy = qos->out.phy;
1459
1460 if (!qos->out.phy)
1461 qos->out.phy = qos->in.phy;
1462
1463 if (!hci_le_set_cig_params(cis, qos)) {
1464 hci_conn_drop(cis);
1465 return ERR_PTR(-EINVAL);
1466 }
1467
1468 cis->iso_qos = *qos;
1469 cis->state = BT_BOUND;
1470
1471 return cis;
1472}
1473
1474bool hci_iso_setup_path(struct hci_conn *conn)
1475{
1476 struct hci_dev *hdev = conn->hdev;
1477 struct hci_cp_le_setup_iso_path cmd;
1478
1479 memset(&cmd, 0, sizeof(cmd));
1480
1481 if (conn->iso_qos.out.sdu) {
1482 cmd.handle = cpu_to_le16(conn->handle);
1483 cmd.direction = 0x00; /* Input (Host to Controller) */
1484 cmd.path = 0x00; /* HCI path if enabled */
1485 cmd.codec = 0x03; /* Transparent Data */
1486
1487 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1488 &cmd) < 0)
1489 return false;
1490 }
1491
1492 if (conn->iso_qos.in.sdu) {
1493 cmd.handle = cpu_to_le16(conn->handle);
1494 cmd.direction = 0x01; /* Output (Controller to Host) */
1495 cmd.path = 0x00; /* HCI path if enabled */
1496 cmd.codec = 0x03; /* Transparent Data */
1497
1498 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1499 &cmd) < 0)
1500 return false;
1501 }
1502
1503 return true;
1504}
1505
1506static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
1507{
1508 struct {
1509 struct hci_cp_le_create_cis cp;
1510 struct hci_cis cis[0x1f];
1511 } cmd;
1512 struct hci_conn *conn = data;
1513 u8 cig;
1514
1515 memset(&cmd, 0, sizeof(cmd));
1516 cmd.cis[0].acl_handle = cpu_to_le16(conn->link->handle);
1517 cmd.cis[0].cis_handle = cpu_to_le16(conn->handle);
1518 cmd.cp.num_cis++;
1519 cig = conn->iso_qos.cig;
1520
1521 hci_dev_lock(hdev);
1522
1523 rcu_read_lock();
1524
1525 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
1526 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
1527
1528 if (conn == data || conn->type != ISO_LINK ||
1529 conn->state == BT_CONNECTED || conn->iso_qos.cig != cig)
1530 continue;
1531
1532 /* Check if all CIS(s) belonging to a CIG are ready */
1533 if (conn->link->state != BT_CONNECTED ||
1534 conn->state != BT_CONNECT) {
1535 cmd.cp.num_cis = 0;
1536 break;
1537 }
1538
1539 /* Group all CIS with state BT_CONNECT since the spec don't
1540 * allow to send them individually:
1541 *
1542 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
1543 * page 2566:
1544 *
1545 * If the Host issues this command before all the
1546 * HCI_LE_CIS_Established events from the previous use of the
1547 * command have been generated, the Controller shall return the
1548 * error code Command Disallowed (0x0C).
1549 */
1550 cis->acl_handle = cpu_to_le16(conn->link->handle);
1551 cis->cis_handle = cpu_to_le16(conn->handle);
1552 cmd.cp.num_cis++;
1553 }
1554
1555 rcu_read_unlock();
1556
1557 hci_dev_unlock(hdev);
1558
1559 if (!cmd.cp.num_cis)
1560 return 0;
1561
1562 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_CIS, sizeof(cmd.cp) +
1563 sizeof(cmd.cis[0]) * cmd.cp.num_cis, &cmd);
1564}
1565
1566int hci_le_create_cis(struct hci_conn *conn)
1567{
1568 struct hci_conn *cis;
1569 struct hci_dev *hdev = conn->hdev;
1570 int err;
1571
1572 switch (conn->type) {
1573 case LE_LINK:
1574 if (!conn->link || conn->state != BT_CONNECTED)
1575 return -EINVAL;
1576 cis = conn->link;
1577 break;
1578 case ISO_LINK:
1579 cis = conn;
1580 break;
1581 default:
1582 return -EINVAL;
1583 }
1584
1585 if (cis->state == BT_CONNECT)
1586 return 0;
1587
1588 /* Queue Create CIS */
1589 err = hci_cmd_sync_queue(hdev, hci_create_cis_sync, cis, NULL);
1590 if (err)
1591 return err;
1592
1593 cis->state = BT_CONNECT;
1594
1595 return 0;
1596}
1597
1598static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
1599 struct bt_iso_io_qos *qos, __u8 phy)
1600{
1601 /* Only set MTU if PHY is enabled */
1602 if (!qos->sdu && qos->phy) {
1603 if (hdev->iso_mtu > 0)
1604 qos->sdu = hdev->iso_mtu;
1605 else if (hdev->le_mtu > 0)
1606 qos->sdu = hdev->le_mtu;
1607 else
1608 qos->sdu = hdev->acl_mtu;
1609 }
1610
1611 /* Use the same PHY as ACL if set to any */
1612 if (qos->phy == BT_ISO_PHY_ANY)
1613 qos->phy = phy;
1614
1615 /* Use LE ACL connection interval if not set */
1616 if (!qos->interval)
1617 /* ACL interval unit in 1.25 ms to us */
1618 qos->interval = conn->le_conn_interval * 1250;
1619
1620 /* Use LE ACL connection latency if not set */
1621 if (!qos->latency)
1622 qos->latency = conn->le_conn_latency;
1623}
1624
1625struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
1626 __u8 dst_type, struct bt_iso_qos *qos)
1627{
1628 struct hci_conn *le;
1629 struct hci_conn *cis;
1630
1631 /* Convert from ISO socket address type to HCI address type */
1632 if (dst_type == BDADDR_LE_PUBLIC)
1633 dst_type = ADDR_LE_DEV_PUBLIC;
1634 else
1635 dst_type = ADDR_LE_DEV_RANDOM;
1636
1637 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
1638 le = hci_connect_le(hdev, dst, dst_type, false,
1639 BT_SECURITY_LOW,
1640 HCI_LE_CONN_TIMEOUT,
1641 HCI_ROLE_SLAVE);
1642 else
1643 le = hci_connect_le_scan(hdev, dst, dst_type,
1644 BT_SECURITY_LOW,
1645 HCI_LE_CONN_TIMEOUT,
1646 CONN_REASON_ISO_CONNECT);
1647 if (IS_ERR(le))
1648 return le;
1649
1650 hci_iso_qos_setup(hdev, le, &qos->out,
1651 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys);
1652 hci_iso_qos_setup(hdev, le, &qos->in,
1653 le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys);
1654
1655 cis = hci_bind_cis(hdev, dst, dst_type, qos);
1656 if (IS_ERR(cis)) {
1657 hci_conn_drop(le);
1658 return cis;
1659 }
1660
1661 le->link = cis;
1662 cis->link = le;
1663
1664 hci_conn_hold(cis);
1665
1666 /* If LE is already connected and CIS handle is already set proceed to
1667 * Create CIS immediately.
1668 */
1669 if (le->state == BT_CONNECTED && cis->handle != HCI_CONN_HANDLE_UNSET)
1670 hci_le_create_cis(le);
1671
1672 return cis;
1673}
1674
1235/* Check link security requirement */
1236int hci_conn_check_link_mode(struct hci_conn *conn)
1237{
1238 BT_DBG("hcon %p", conn);
1239
1240 /* In Secure Connections Only mode, it is required that Secure
1241 * Connections is used and the link is encrypted with AES-CCM
1242 * using a P-256 authenticated combination key.

--- 568 unchanged lines hidden ---
1675/* Check link security requirement */
1676int hci_conn_check_link_mode(struct hci_conn *conn)
1677{
1678 BT_DBG("hcon %p", conn);
1679
1680 /* In Secure Connections Only mode, it is required that Secure
1681 * Connections is used and the link is encrypted with AES-CCM
1682 * using a P-256 authenticated combination key.

--- 568 unchanged lines hidden ---