|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (C) 2025 Google Corporation |
| 4 | + */ |
| 5 | + |
| 6 | +#include <linux/skbuff.h> |
| 7 | +#include <linux/types.h> |
| 8 | + |
| 9 | +#include <net/bluetooth/bluetooth.h> |
| 10 | +#include <net/bluetooth/hci.h> |
| 11 | +#include <net/bluetooth/hci_core.h> |
| 12 | +#include <net/bluetooth/hci_drv.h> |
| 13 | + |
| 14 | +int hci_drv_cmd_status(struct hci_dev *hdev, u16 cmd, u8 status) |
| 15 | +{ |
| 16 | + struct hci_drv_ev_hdr *hdr; |
| 17 | + struct hci_drv_ev_cmd_status *ev; |
| 18 | + struct sk_buff *skb; |
| 19 | + |
| 20 | + skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
| 21 | + if (!skb) |
| 22 | + return -ENOMEM; |
| 23 | + |
| 24 | + hdr = skb_put(skb, sizeof(*hdr)); |
| 25 | + hdr->opcode = __cpu_to_le16(HCI_DRV_EV_CMD_STATUS); |
| 26 | + hdr->len = __cpu_to_le16(sizeof(*ev)); |
| 27 | + |
| 28 | + ev = skb_put(skb, sizeof(*ev)); |
| 29 | + ev->opcode = __cpu_to_le16(cmd); |
| 30 | + ev->status = status; |
| 31 | + |
| 32 | + hci_skb_pkt_type(skb) = HCI_DRV_PKT; |
| 33 | + |
| 34 | + return hci_recv_frame(hdev, skb); |
| 35 | +} |
| 36 | +EXPORT_SYMBOL(hci_drv_cmd_status); |
| 37 | + |
| 38 | +int hci_drv_cmd_complete(struct hci_dev *hdev, u16 cmd, u8 status, void *rp, |
| 39 | + size_t rp_len) |
| 40 | +{ |
| 41 | + struct hci_drv_ev_hdr *hdr; |
| 42 | + struct hci_drv_ev_cmd_complete *ev; |
| 43 | + struct sk_buff *skb; |
| 44 | + |
| 45 | + skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
| 46 | + if (!skb) |
| 47 | + return -ENOMEM; |
| 48 | + |
| 49 | + hdr = skb_put(skb, sizeof(*hdr)); |
| 50 | + hdr->opcode = __cpu_to_le16(HCI_DRV_EV_CMD_COMPLETE); |
| 51 | + hdr->len = __cpu_to_le16(sizeof(*ev) + rp_len); |
| 52 | + |
| 53 | + ev = skb_put(skb, sizeof(*ev)); |
| 54 | + ev->opcode = __cpu_to_le16(cmd); |
| 55 | + ev->status = status; |
| 56 | + |
| 57 | + skb_put_data(skb, rp, rp_len); |
| 58 | + |
| 59 | + hci_skb_pkt_type(skb) = HCI_DRV_PKT; |
| 60 | + |
| 61 | + return hci_recv_frame(hdev, skb); |
| 62 | +} |
| 63 | +EXPORT_SYMBOL(hci_drv_cmd_complete); |
| 64 | + |
| 65 | +int hci_drv_process_cmd(struct hci_dev *hdev, struct sk_buff *skb) |
| 66 | +{ |
| 67 | + struct hci_drv_cmd_hdr *hdr; |
| 68 | + const struct hci_drv_handler *handler = NULL; |
| 69 | + u16 opcode, len, ogf, ocf; |
| 70 | + |
| 71 | + hdr = skb_pull_data(skb, sizeof(*hdr)); |
| 72 | + if (!hdr) |
| 73 | + return -EILSEQ; |
| 74 | + |
| 75 | + opcode = __le16_to_cpu(hdr->opcode); |
| 76 | + len = __le16_to_cpu(hdr->len); |
| 77 | + if (len != skb->len) |
| 78 | + return -EILSEQ; |
| 79 | + |
| 80 | + ogf = hci_opcode_ogf(opcode); |
| 81 | + ocf = hci_opcode_ocf(opcode); |
| 82 | + |
| 83 | + if (!hdev->hci_drv) |
| 84 | + return hci_drv_cmd_status(hdev, opcode, |
| 85 | + HCI_DRV_STATUS_UNKNOWN_COMMAND); |
| 86 | + |
| 87 | + if (ogf != HCI_DRV_OGF_DRIVER_SPECIFIC) { |
| 88 | + if (opcode < hdev->hci_drv->common_handler_count) |
| 89 | + handler = &hdev->hci_drv->common_handlers[opcode]; |
| 90 | + } else { |
| 91 | + if (ocf < hdev->hci_drv->specific_handler_count) |
| 92 | + handler = &hdev->hci_drv->specific_handlers[ocf]; |
| 93 | + } |
| 94 | + |
| 95 | + if (!handler || !handler->func) |
| 96 | + return hci_drv_cmd_status(hdev, opcode, |
| 97 | + HCI_DRV_STATUS_UNKNOWN_COMMAND); |
| 98 | + |
| 99 | + if (len != handler->data_len) |
| 100 | + return hci_drv_cmd_status(hdev, opcode, |
| 101 | + HCI_DRV_STATUS_INVALID_PARAMETERS); |
| 102 | + |
| 103 | + return handler->func(hdev, skb->data, len); |
| 104 | +} |
| 105 | +EXPORT_SYMBOL(hci_drv_process_cmd); |
0 commit comments