]> git.proxmox.com Git - mirror_qemu.git/blame - hw/bt/core.c
Merge remote-tracking branch 'remotes/huth/tags/pull-request-2018-01-22' into staging
[mirror_qemu.git] / hw / bt / core.c
CommitLineData
1ae26a18
AZ
1/*
2 * Convenience functions for bluetooth.
3 *
4 * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
fad6cb1a 16 * You should have received a copy of the GNU General Public License along
8167ee88 17 * with this program; if not, see <http://www.gnu.org/licenses/>.
1ae26a18
AZ
18 */
19
0430891c 20#include "qemu/osdep.h"
bf937a79 21#include "qemu/error-report.h"
1ae26a18 22#include "qemu-common.h"
dccfcd0e 23#include "sysemu/bt.h"
83c9f4ca 24#include "hw/bt.h"
1ae26a18
AZ
25
26/* Slave implementations can ignore this */
27static void bt_dummy_lmp_mode_change(struct bt_link_s *link)
28{
29}
30
31/* Slaves should never receive these PDUs */
32static void bt_dummy_lmp_connection_complete(struct bt_link_s *link)
33{
34 if (link->slave->reject_reason)
bf937a79 35 error_report("%s: stray LMP_not_accepted received, fixme", __func__);
1ae26a18 36 else
bf937a79 37 error_report("%s: stray LMP_accepted received, fixme", __func__);
1ae26a18
AZ
38 exit(-1);
39}
40
41static void bt_dummy_lmp_disconnect_master(struct bt_link_s *link)
42{
bf937a79 43 error_report("%s: stray LMP_detach received, fixme", __func__);
1ae26a18
AZ
44 exit(-1);
45}
46
47static void bt_dummy_lmp_acl_resp(struct bt_link_s *link,
48 const uint8_t *data, int start, int len)
49{
bf937a79 50 error_report("%s: stray ACL response PDU, fixme", __func__);
1ae26a18
AZ
51 exit(-1);
52}
53
54/* Slaves that don't hold any additional per link state can use these */
55static void bt_dummy_lmp_connection_request(struct bt_link_s *req)
56{
7267c094 57 struct bt_link_s *link = g_malloc0(sizeof(struct bt_link_s));
1ae26a18
AZ
58
59 link->slave = req->slave;
60 link->host = req->host;
61
62 req->host->reject_reason = 0;
63 req->host->lmp_connection_complete(link);
64}
65
66static void bt_dummy_lmp_disconnect_slave(struct bt_link_s *link)
67{
7267c094 68 g_free(link);
1ae26a18
AZ
69}
70
71static void bt_dummy_destroy(struct bt_device_s *device)
72{
73 bt_device_done(device);
7267c094 74 g_free(device);
1ae26a18
AZ
75}
76
77static int bt_dev_idx = 0;
78
79void bt_device_init(struct bt_device_s *dev, struct bt_scatternet_s *net)
80{
81 memset(dev, 0, sizeof(*dev));
82 dev->inquiry_scan = 1;
83 dev->page_scan = 1;
84
85 dev->bd_addr.b[0] = bt_dev_idx & 0xff;
86 dev->bd_addr.b[1] = bt_dev_idx >> 8;
87 dev->bd_addr.b[2] = 0xd0;
88 dev->bd_addr.b[3] = 0xba;
89 dev->bd_addr.b[4] = 0xbe;
90 dev->bd_addr.b[5] = 0xba;
91 bt_dev_idx ++;
92
93 /* Simple slave-only devices need to implement only .lmp_acl_data */
94 dev->lmp_connection_complete = bt_dummy_lmp_connection_complete;
95 dev->lmp_disconnect_master = bt_dummy_lmp_disconnect_master;
96 dev->lmp_acl_resp = bt_dummy_lmp_acl_resp;
97 dev->lmp_mode_change = bt_dummy_lmp_mode_change;
98 dev->lmp_connection_request = bt_dummy_lmp_connection_request;
99 dev->lmp_disconnect_slave = bt_dummy_lmp_disconnect_slave;
100
101 dev->handle_destroy = bt_dummy_destroy;
102
103 dev->net = net;
104 dev->next = net->slave;
105 net->slave = dev;
106}
107
108void bt_device_done(struct bt_device_s *dev)
109{
110 struct bt_device_s **p = &dev->net->slave;
111
112 while (*p && *p != dev)
113 p = &(*p)->next;
114 if (*p != dev) {
bf937a79
AF
115 error_report("%s: bad bt device \"%s\"", __func__,
116 dev->lmp_name ?: "(null)");
1ae26a18
AZ
117 exit(-1);
118 }
119
120 *p = dev->next;
121}
644e1a8a
MR
122
123static struct bt_vlan_s {
124 struct bt_scatternet_s net;
125 int id;
126 struct bt_vlan_s *next;
127} *first_bt_vlan;
128
129/* find or alloc a new bluetooth "VLAN" */
130struct bt_scatternet_s *qemu_find_bt_vlan(int id)
131{
132 struct bt_vlan_s **pvlan, *vlan;
133 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
134 if (vlan->id == id)
135 return &vlan->net;
136 }
137 vlan = g_malloc0(sizeof(struct bt_vlan_s));
138 vlan->id = id;
139 pvlan = &first_bt_vlan;
140 while (*pvlan != NULL)
141 pvlan = &(*pvlan)->next;
142 *pvlan = vlan;
143 return &vlan->net;
144}