1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2014 Intel Corporation
7 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
27 #include "lib/bluetooth.h"
30 #include "src/shared/io.h"
31 #include "monitor/bt.h"
35 #define DEBUGFS_PATH "/sys/kernel/debug/bluetooth"
36 #define DEVCORE_PATH "/sys/class/devcoredump"
45 static void vhci_destroy(void *user_data)
47 struct vhci *vhci = user_data;
49 btdev_destroy(vhci->btdev);
55 static void vhci_write_callback(const struct iovec *iov, int iovlen,
58 struct vhci *vhci = user_data;
61 written = io_send(vhci->io, iov, iovlen);
66 static bool vhci_read_callback(struct io *io, void *user_data)
68 struct vhci *vhci = user_data;
69 int fd = io_get_fd(vhci->io);
70 unsigned char buf[4096];
73 len = read(fd, buf, sizeof(buf));
77 btdev_receive_h4(vhci->btdev, buf, len);
82 bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
83 void *user_data, vhci_destroy_func_t destroy)
88 return btdev_set_debug(vhci->btdev, callback, user_data, destroy);
91 struct vhci_create_req {
94 } __attribute__((packed));
96 struct vhci_create_rsp {
100 } __attribute__((packed));
102 struct vhci *vhci_open(uint8_t type)
105 struct vhci_create_req req;
106 struct vhci_create_rsp rsp;
109 fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
113 memset(&req, 0, sizeof(req));
114 req.pkt_type = HCI_VENDOR_PKT;
118 req.opcode = HCI_AMP;
121 req.opcode = HCI_PRIMARY;
125 if (write(fd, &req, sizeof(req)) < 0) {
130 memset(&rsp, 0, sizeof(rsp));
132 if (read(fd, &rsp, sizeof(rsp)) < 0) {
137 vhci = malloc(sizeof(*vhci));
143 memset(vhci, 0, sizeof(*vhci));
145 vhci->index = rsp.index;
146 vhci->io = io_new(fd);
148 io_set_close_on_destroy(vhci->io, true);
150 vhci->btdev = btdev_create(type, rsp.index);
156 btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci);
158 if (!io_set_read_handler(vhci->io, vhci_read_callback, vhci, NULL)) {
166 void vhci_close(struct vhci *vhci)
174 bool vhci_pause_input(struct vhci *vhci, bool paused)
177 return io_set_read_handler(vhci->io, NULL, NULL, NULL);
179 return io_set_read_handler(vhci->io, vhci_read_callback, vhci,
183 struct btdev *vhci_get_btdev(struct vhci *vhci)
191 static int vhci_debugfs_write(struct vhci *vhci, char *option, const void *data,
201 memset(path, 0, sizeof(path));
202 sprintf(path, DEBUGFS_PATH "/hci%d/%s", vhci->index, option);
204 fd = open(path, O_RDWR);
208 n = write(fd, data, len);
219 int vhci_set_force_suspend(struct vhci *vhci, bool enable)
223 val = (enable) ? 'Y' : 'N';
225 return vhci_debugfs_write(vhci, "force_suspend", &val, sizeof(val));
228 int vhci_set_force_wakeup(struct vhci *vhci, bool enable)
232 val = (enable) ? 'Y' : 'N';
234 return vhci_debugfs_write(vhci, "force_wakeup", &val, sizeof(val));
237 int vhci_set_msft_opcode(struct vhci *vhci, uint16_t opcode)
242 snprintf(val, sizeof(val), "0x%4x", opcode);
244 err = vhci_debugfs_write(vhci, "msft_opcode", &val, sizeof(val));
248 return btdev_set_msft_opcode(vhci->btdev, opcode);
251 int vhci_set_aosp_capable(struct vhci *vhci, bool enable)
255 val = (enable) ? 'Y' : 'N';
257 return vhci_debugfs_write(vhci, "aosp_capable", &val, sizeof(val));
260 int vhci_set_emu_opcode(struct vhci *vhci, uint16_t opcode)
262 return btdev_set_emu_opcode(vhci->btdev, opcode);
265 int vhci_set_force_static_address(struct vhci *vhci, bool enable)
269 val = (enable) ? 'Y' : 'N';
271 return vhci_debugfs_write(vhci, "force_static_address", &val,
275 int vhci_force_devcd(struct vhci *vhci, const void *data, size_t len)
277 return vhci_debugfs_write(vhci, "force_devcoredump", data, len);
280 int vhci_read_devcd(struct vhci *vhci, void *buf, size_t size)
283 struct dirent *entry;
284 char filename[PATH_MAX];
288 dir = opendir(DEVCORE_PATH);
292 while ((entry = readdir(dir)) != NULL) {
293 if (strstr(entry->d_name, "devcd"))
302 sprintf(filename, DEVCORE_PATH "/%s/data", entry->d_name);
303 fd = open(filename, O_RDWR);
309 ret = read(fd, buf, size);
315 /* Once the devcoredump is read, write anything to it to mark it for
318 if (write(fd, "0", 1) < 0) {