root / hw / bt-hci-csr.c @ 505597e4
History | View | Annotate | Download (12 kB)
1 | 58a26b47 | balrog | /*
|
---|---|---|---|
2 | 58a26b47 | balrog | * Bluetooth serial HCI transport.
|
3 | 58a26b47 | balrog | * CSR41814 HCI with H4p vendor extensions.
|
4 | 58a26b47 | balrog | *
|
5 | 58a26b47 | balrog | * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
|
6 | 58a26b47 | balrog | *
|
7 | 58a26b47 | balrog | * This program is free software; you can redistribute it and/or
|
8 | 58a26b47 | balrog | * modify it under the terms of the GNU General Public License as
|
9 | 58a26b47 | balrog | * published by the Free Software Foundation; either version 2 or
|
10 | 58a26b47 | balrog | * (at your option) version 3 of the License.
|
11 | 58a26b47 | balrog | *
|
12 | 58a26b47 | balrog | * This program is distributed in the hope that it will be useful,
|
13 | 58a26b47 | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 58a26b47 | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | 58a26b47 | balrog | * GNU General Public License for more details.
|
16 | 58a26b47 | balrog | *
|
17 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
18 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 | 58a26b47 | balrog | */
|
20 | 58a26b47 | balrog | |
21 | 58a26b47 | balrog | #include "qemu-common.h" |
22 | 58a26b47 | balrog | #include "qemu-char.h" |
23 | 58a26b47 | balrog | #include "qemu-timer.h" |
24 | 58a26b47 | balrog | #include "irq.h" |
25 | 58a26b47 | balrog | #include "net.h" |
26 | 58a26b47 | balrog | #include "bt.h" |
27 | 58a26b47 | balrog | |
28 | 58a26b47 | balrog | struct csrhci_s {
|
29 | 58a26b47 | balrog | int enable;
|
30 | 58a26b47 | balrog | qemu_irq *pins; |
31 | 58a26b47 | balrog | int pin_state;
|
32 | 58a26b47 | balrog | int modem_state;
|
33 | 58a26b47 | balrog | CharDriverState chr; |
34 | 58a26b47 | balrog | #define FIFO_LEN 4096 |
35 | 58a26b47 | balrog | int out_start;
|
36 | 58a26b47 | balrog | int out_len;
|
37 | 58a26b47 | balrog | int out_size;
|
38 | 58a26b47 | balrog | uint8_t outfifo[FIFO_LEN * 2];
|
39 | 58a26b47 | balrog | uint8_t inpkt[FIFO_LEN]; |
40 | 58a26b47 | balrog | int in_len;
|
41 | 58a26b47 | balrog | int in_hdr;
|
42 | 58a26b47 | balrog | int in_data;
|
43 | 58a26b47 | balrog | QEMUTimer *out_tm; |
44 | 58a26b47 | balrog | int64_t baud_delay; |
45 | 58a26b47 | balrog | |
46 | c227f099 | Anthony Liguori | bdaddr_t bd_addr; |
47 | 58a26b47 | balrog | struct HCIInfo *hci;
|
48 | 58a26b47 | balrog | }; |
49 | 58a26b47 | balrog | |
50 | 58a26b47 | balrog | /* H4+ packet types */
|
51 | 58a26b47 | balrog | enum {
|
52 | 58a26b47 | balrog | H4_CMD_PKT = 1,
|
53 | 58a26b47 | balrog | H4_ACL_PKT = 2,
|
54 | 58a26b47 | balrog | H4_SCO_PKT = 3,
|
55 | 58a26b47 | balrog | H4_EVT_PKT = 4,
|
56 | 58a26b47 | balrog | H4_NEG_PKT = 6,
|
57 | 58a26b47 | balrog | H4_ALIVE_PKT = 7,
|
58 | 58a26b47 | balrog | }; |
59 | 58a26b47 | balrog | |
60 | 58a26b47 | balrog | /* CSR41814 negotiation start magic packet */
|
61 | 58a26b47 | balrog | static const uint8_t csrhci_neg_packet[] = { |
62 | 58a26b47 | balrog | H4_NEG_PKT, 10,
|
63 | 58a26b47 | balrog | 0x00, 0xa0, 0x01, 0x00, 0x00, |
64 | 58a26b47 | balrog | 0x4c, 0x00, 0x96, 0x00, 0x00, |
65 | 58a26b47 | balrog | }; |
66 | 58a26b47 | balrog | |
67 | 58a26b47 | balrog | /* CSR41814 vendor-specific command OCFs */
|
68 | 58a26b47 | balrog | enum {
|
69 | 58a26b47 | balrog | OCF_CSR_SEND_FIRMWARE = 0x000,
|
70 | 58a26b47 | balrog | }; |
71 | 58a26b47 | balrog | |
72 | 58a26b47 | balrog | static inline void csrhci_fifo_wake(struct csrhci_s *s) |
73 | 58a26b47 | balrog | { |
74 | 58a26b47 | balrog | if (!s->enable || !s->out_len)
|
75 | 58a26b47 | balrog | return;
|
76 | 58a26b47 | balrog | |
77 | 58a26b47 | balrog | /* XXX: Should wait for s->modem_state & CHR_TIOCM_RTS? */
|
78 | 58a26b47 | balrog | if (s->chr.chr_can_read && s->chr.chr_can_read(s->chr.handler_opaque) &&
|
79 | 58a26b47 | balrog | s->chr.chr_read) { |
80 | 58a26b47 | balrog | s->chr.chr_read(s->chr.handler_opaque, |
81 | 58a26b47 | balrog | s->outfifo + s->out_start ++, 1);
|
82 | 58a26b47 | balrog | s->out_len --; |
83 | 58a26b47 | balrog | if (s->out_start >= s->out_size) {
|
84 | 58a26b47 | balrog | s->out_start = 0;
|
85 | 58a26b47 | balrog | s->out_size = FIFO_LEN; |
86 | 58a26b47 | balrog | } |
87 | 58a26b47 | balrog | } |
88 | 58a26b47 | balrog | |
89 | 58a26b47 | balrog | if (s->out_len)
|
90 | 74475455 | Paolo Bonzini | qemu_mod_timer(s->out_tm, qemu_get_clock_ns(vm_clock) + s->baud_delay); |
91 | 58a26b47 | balrog | } |
92 | 58a26b47 | balrog | |
93 | 58a26b47 | balrog | #define csrhci_out_packetz(s, len) memset(csrhci_out_packet(s, len), 0, len) |
94 | 58a26b47 | balrog | static uint8_t *csrhci_out_packet(struct csrhci_s *s, int len) |
95 | 58a26b47 | balrog | { |
96 | 58a26b47 | balrog | int off = s->out_start + s->out_len;
|
97 | 58a26b47 | balrog | |
98 | 58a26b47 | balrog | /* TODO: do the padding here, i.e. align len */
|
99 | 58a26b47 | balrog | s->out_len += len; |
100 | 58a26b47 | balrog | |
101 | 58a26b47 | balrog | if (off < FIFO_LEN) {
|
102 | 58a26b47 | balrog | if (off + len > FIFO_LEN && (s->out_size = off + len) > FIFO_LEN * 2) { |
103 | 58a26b47 | balrog | fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
|
104 | 58a26b47 | balrog | exit(-1);
|
105 | 58a26b47 | balrog | } |
106 | 58a26b47 | balrog | return s->outfifo + off;
|
107 | 58a26b47 | balrog | } |
108 | 58a26b47 | balrog | |
109 | 58a26b47 | balrog | if (s->out_len > s->out_size) {
|
110 | 58a26b47 | balrog | fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
|
111 | 58a26b47 | balrog | exit(-1);
|
112 | 58a26b47 | balrog | } |
113 | 58a26b47 | balrog | |
114 | 58a26b47 | balrog | return s->outfifo + off - s->out_size;
|
115 | 58a26b47 | balrog | } |
116 | 58a26b47 | balrog | |
117 | 58a26b47 | balrog | static inline uint8_t *csrhci_out_packet_csr(struct csrhci_s *s, |
118 | 58a26b47 | balrog | int type, int len) |
119 | 58a26b47 | balrog | { |
120 | 58a26b47 | balrog | uint8_t *ret = csrhci_out_packetz(s, len + 2);
|
121 | 58a26b47 | balrog | |
122 | 58a26b47 | balrog | *ret ++ = type; |
123 | 58a26b47 | balrog | *ret ++ = len; |
124 | 58a26b47 | balrog | |
125 | 58a26b47 | balrog | return ret;
|
126 | 58a26b47 | balrog | } |
127 | 58a26b47 | balrog | |
128 | 58a26b47 | balrog | static inline uint8_t *csrhci_out_packet_event(struct csrhci_s *s, |
129 | 58a26b47 | balrog | int evt, int len) |
130 | 58a26b47 | balrog | { |
131 | 58a26b47 | balrog | uint8_t *ret = csrhci_out_packetz(s, |
132 | 58a26b47 | balrog | len + 1 + sizeof(struct hci_event_hdr)); |
133 | 58a26b47 | balrog | |
134 | 58a26b47 | balrog | *ret ++ = H4_EVT_PKT; |
135 | 58a26b47 | balrog | ((struct hci_event_hdr *) ret)->evt = evt;
|
136 | 58a26b47 | balrog | ((struct hci_event_hdr *) ret)->plen = len;
|
137 | 58a26b47 | balrog | |
138 | 58a26b47 | balrog | return ret + sizeof(struct hci_event_hdr); |
139 | 58a26b47 | balrog | } |
140 | 58a26b47 | balrog | |
141 | 58a26b47 | balrog | static void csrhci_in_packet_vendor(struct csrhci_s *s, int ocf, |
142 | 58a26b47 | balrog | uint8_t *data, int len)
|
143 | 58a26b47 | balrog | { |
144 | 58a26b47 | balrog | int offset;
|
145 | 58a26b47 | balrog | uint8_t *rpkt; |
146 | 58a26b47 | balrog | |
147 | 58a26b47 | balrog | switch (ocf) {
|
148 | 58a26b47 | balrog | case OCF_CSR_SEND_FIRMWARE:
|
149 | 58a26b47 | balrog | /* Check if this is the bd_address packet */
|
150 | 58a26b47 | balrog | if (len >= 18 + 8 && data[12] == 0x01 && data[13] == 0x00) { |
151 | 58a26b47 | balrog | offset = 18;
|
152 | 58a26b47 | balrog | s->bd_addr.b[0] = data[offset + 7]; /* Beyond cmd packet end(!?) */ |
153 | 58a26b47 | balrog | s->bd_addr.b[1] = data[offset + 6]; |
154 | 58a26b47 | balrog | s->bd_addr.b[2] = data[offset + 4]; |
155 | 58a26b47 | balrog | s->bd_addr.b[3] = data[offset + 0]; |
156 | 58a26b47 | balrog | s->bd_addr.b[4] = data[offset + 3]; |
157 | 58a26b47 | balrog | s->bd_addr.b[5] = data[offset + 2]; |
158 | 58a26b47 | balrog | |
159 | 58a26b47 | balrog | s->hci->bdaddr_set(s->hci, s->bd_addr.b); |
160 | 58a26b47 | balrog | fprintf(stderr, "%s: bd_address loaded from firmware: "
|
161 | 58a26b47 | balrog | "%02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
|
162 | 58a26b47 | balrog | s->bd_addr.b[0], s->bd_addr.b[1], s->bd_addr.b[2], |
163 | 58a26b47 | balrog | s->bd_addr.b[3], s->bd_addr.b[4], s->bd_addr.b[5]); |
164 | 58a26b47 | balrog | } |
165 | 58a26b47 | balrog | |
166 | 58a26b47 | balrog | rpkt = csrhci_out_packet_event(s, EVT_VENDOR, 11);
|
167 | 58a26b47 | balrog | /* Status bytes: no error */
|
168 | 58a26b47 | balrog | rpkt[9] = 0x00; |
169 | 58a26b47 | balrog | rpkt[10] = 0x00; |
170 | 58a26b47 | balrog | break;
|
171 | 58a26b47 | balrog | |
172 | 58a26b47 | balrog | default:
|
173 | 58a26b47 | balrog | fprintf(stderr, "%s: got a bad CMD packet\n", __FUNCTION__);
|
174 | 58a26b47 | balrog | return;
|
175 | 58a26b47 | balrog | } |
176 | 58a26b47 | balrog | |
177 | 58a26b47 | balrog | csrhci_fifo_wake(s); |
178 | 58a26b47 | balrog | } |
179 | 58a26b47 | balrog | |
180 | 58a26b47 | balrog | static void csrhci_in_packet(struct csrhci_s *s, uint8_t *pkt) |
181 | 58a26b47 | balrog | { |
182 | 58a26b47 | balrog | uint8_t *rpkt; |
183 | 58a26b47 | balrog | int opc;
|
184 | 58a26b47 | balrog | |
185 | 58a26b47 | balrog | switch (*pkt ++) {
|
186 | 58a26b47 | balrog | case H4_CMD_PKT:
|
187 | 58a26b47 | balrog | opc = le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode);
|
188 | 58a26b47 | balrog | if (cmd_opcode_ogf(opc) == OGF_VENDOR_CMD) {
|
189 | 58a26b47 | balrog | csrhci_in_packet_vendor(s, cmd_opcode_ocf(opc), |
190 | 58a26b47 | balrog | pkt + sizeof(struct hci_command_hdr), |
191 | 58a26b47 | balrog | s->in_len - sizeof(struct hci_command_hdr) - 1); |
192 | 58a26b47 | balrog | return;
|
193 | 58a26b47 | balrog | } |
194 | 58a26b47 | balrog | |
195 | 58a26b47 | balrog | /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,
|
196 | 58a26b47 | balrog | * we need to send it to the HCI layer and then add our supported
|
197 | 58a26b47 | balrog | * commands to the returned mask (such as OGF_VENDOR_CMD). With
|
198 | 58a26b47 | balrog | * bt-hci.c we could just have hooks for this kind of commands but
|
199 | 58a26b47 | balrog | * we can't with bt-host.c. */
|
200 | 58a26b47 | balrog | |
201 | 58a26b47 | balrog | s->hci->cmd_send(s->hci, pkt, s->in_len - 1);
|
202 | 58a26b47 | balrog | break;
|
203 | 58a26b47 | balrog | |
204 | 58a26b47 | balrog | case H4_EVT_PKT:
|
205 | 58a26b47 | balrog | goto bad_pkt;
|
206 | 58a26b47 | balrog | |
207 | 58a26b47 | balrog | case H4_ACL_PKT:
|
208 | 58a26b47 | balrog | s->hci->acl_send(s->hci, pkt, s->in_len - 1);
|
209 | 58a26b47 | balrog | break;
|
210 | 58a26b47 | balrog | |
211 | 58a26b47 | balrog | case H4_SCO_PKT:
|
212 | 58a26b47 | balrog | s->hci->sco_send(s->hci, pkt, s->in_len - 1);
|
213 | 58a26b47 | balrog | break;
|
214 | 58a26b47 | balrog | |
215 | 58a26b47 | balrog | case H4_NEG_PKT:
|
216 | 58a26b47 | balrog | if (s->in_hdr != sizeof(csrhci_neg_packet) || |
217 | 58a26b47 | balrog | memcmp(pkt - 1, csrhci_neg_packet, s->in_hdr)) {
|
218 | 58a26b47 | balrog | fprintf(stderr, "%s: got a bad NEG packet\n", __FUNCTION__);
|
219 | 58a26b47 | balrog | return;
|
220 | 58a26b47 | balrog | } |
221 | 58a26b47 | balrog | pkt += 2;
|
222 | 58a26b47 | balrog | |
223 | 58a26b47 | balrog | rpkt = csrhci_out_packet_csr(s, H4_NEG_PKT, 10);
|
224 | 58a26b47 | balrog | |
225 | 58a26b47 | balrog | *rpkt ++ = 0x20; /* Operational settings negotation Ok */ |
226 | 58a26b47 | balrog | memcpy(rpkt, pkt, 7); rpkt += 7; |
227 | 58a26b47 | balrog | *rpkt ++ = 0xff;
|
228 | 7300c079 | Blue Swirl | *rpkt = 0xff;
|
229 | 58a26b47 | balrog | break;
|
230 | 58a26b47 | balrog | |
231 | 58a26b47 | balrog | case H4_ALIVE_PKT:
|
232 | 58a26b47 | balrog | if (s->in_hdr != 4 || pkt[1] != 0x55 || pkt[2] != 0x00) { |
233 | 58a26b47 | balrog | fprintf(stderr, "%s: got a bad ALIVE packet\n", __FUNCTION__);
|
234 | 58a26b47 | balrog | return;
|
235 | 58a26b47 | balrog | } |
236 | 58a26b47 | balrog | |
237 | 58a26b47 | balrog | rpkt = csrhci_out_packet_csr(s, H4_ALIVE_PKT, 2);
|
238 | 58a26b47 | balrog | |
239 | 58a26b47 | balrog | *rpkt ++ = 0xcc;
|
240 | 7300c079 | Blue Swirl | *rpkt = 0x00;
|
241 | 58a26b47 | balrog | break;
|
242 | 58a26b47 | balrog | |
243 | 58a26b47 | balrog | default:
|
244 | 58a26b47 | balrog | bad_pkt:
|
245 | 58a26b47 | balrog | /* TODO: error out */
|
246 | 58a26b47 | balrog | fprintf(stderr, "%s: got a bad packet\n", __FUNCTION__);
|
247 | 58a26b47 | balrog | break;
|
248 | 58a26b47 | balrog | } |
249 | 58a26b47 | balrog | |
250 | 58a26b47 | balrog | csrhci_fifo_wake(s); |
251 | 58a26b47 | balrog | } |
252 | 58a26b47 | balrog | |
253 | 58a26b47 | balrog | static int csrhci_header_len(const uint8_t *pkt) |
254 | 58a26b47 | balrog | { |
255 | 58a26b47 | balrog | switch (pkt[0]) { |
256 | 58a26b47 | balrog | case H4_CMD_PKT:
|
257 | 58a26b47 | balrog | return HCI_COMMAND_HDR_SIZE;
|
258 | 58a26b47 | balrog | case H4_EVT_PKT:
|
259 | 58a26b47 | balrog | return HCI_EVENT_HDR_SIZE;
|
260 | 58a26b47 | balrog | case H4_ACL_PKT:
|
261 | 58a26b47 | balrog | return HCI_ACL_HDR_SIZE;
|
262 | 58a26b47 | balrog | case H4_SCO_PKT:
|
263 | 58a26b47 | balrog | return HCI_SCO_HDR_SIZE;
|
264 | 58a26b47 | balrog | case H4_NEG_PKT:
|
265 | 58a26b47 | balrog | return pkt[1] + 1; |
266 | 58a26b47 | balrog | case H4_ALIVE_PKT:
|
267 | 58a26b47 | balrog | return 3; |
268 | 58a26b47 | balrog | } |
269 | 58a26b47 | balrog | |
270 | 58a26b47 | balrog | exit(-1);
|
271 | 58a26b47 | balrog | } |
272 | 58a26b47 | balrog | |
273 | 58a26b47 | balrog | static int csrhci_data_len(const uint8_t *pkt) |
274 | 58a26b47 | balrog | { |
275 | 58a26b47 | balrog | switch (*pkt ++) {
|
276 | 58a26b47 | balrog | case H4_CMD_PKT:
|
277 | 58a26b47 | balrog | /* It seems that vendor-specific command packets for H4+ are all
|
278 | 58a26b47 | balrog | * one byte longer than indicated in the standard header. */
|
279 | 58a26b47 | balrog | if (le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode) == 0xfc00) |
280 | 58a26b47 | balrog | return (((struct hci_command_hdr *) pkt)->plen + 1) & ~1; |
281 | 58a26b47 | balrog | |
282 | 58a26b47 | balrog | return ((struct hci_command_hdr *) pkt)->plen; |
283 | 58a26b47 | balrog | case H4_EVT_PKT:
|
284 | 58a26b47 | balrog | return ((struct hci_event_hdr *) pkt)->plen; |
285 | 58a26b47 | balrog | case H4_ACL_PKT:
|
286 | 58a26b47 | balrog | return le16_to_cpu(((struct hci_acl_hdr *) pkt)->dlen); |
287 | 58a26b47 | balrog | case H4_SCO_PKT:
|
288 | 58a26b47 | balrog | return ((struct hci_sco_hdr *) pkt)->dlen; |
289 | 58a26b47 | balrog | case H4_NEG_PKT:
|
290 | 58a26b47 | balrog | case H4_ALIVE_PKT:
|
291 | 58a26b47 | balrog | return 0; |
292 | 58a26b47 | balrog | } |
293 | 58a26b47 | balrog | |
294 | 58a26b47 | balrog | exit(-1);
|
295 | 58a26b47 | balrog | } |
296 | 58a26b47 | balrog | |
297 | 58a26b47 | balrog | static int csrhci_write(struct CharDriverState *chr, |
298 | 58a26b47 | balrog | const uint8_t *buf, int len) |
299 | 58a26b47 | balrog | { |
300 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) chr->opaque; |
301 | 58a26b47 | balrog | int plen = s->in_len;
|
302 | 58a26b47 | balrog | |
303 | 58a26b47 | balrog | if (!s->enable)
|
304 | 58a26b47 | balrog | return 0; |
305 | 58a26b47 | balrog | |
306 | 58a26b47 | balrog | s->in_len += len; |
307 | 58a26b47 | balrog | memcpy(s->inpkt + plen, buf, len); |
308 | 58a26b47 | balrog | |
309 | 58a26b47 | balrog | while (1) { |
310 | 58a26b47 | balrog | if (s->in_len >= 2 && plen < 2) |
311 | 58a26b47 | balrog | s->in_hdr = csrhci_header_len(s->inpkt) + 1;
|
312 | 58a26b47 | balrog | |
313 | 58a26b47 | balrog | if (s->in_len >= s->in_hdr && plen < s->in_hdr)
|
314 | 58a26b47 | balrog | s->in_data = csrhci_data_len(s->inpkt) + s->in_hdr; |
315 | 58a26b47 | balrog | |
316 | 58a26b47 | balrog | if (s->in_len >= s->in_data) {
|
317 | 58a26b47 | balrog | csrhci_in_packet(s, s->inpkt); |
318 | 58a26b47 | balrog | |
319 | 58a26b47 | balrog | memmove(s->inpkt, s->inpkt + s->in_len, s->in_len - s->in_data); |
320 | 58a26b47 | balrog | s->in_len -= s->in_data; |
321 | 58a26b47 | balrog | s->in_hdr = INT_MAX; |
322 | 58a26b47 | balrog | s->in_data = INT_MAX; |
323 | 58a26b47 | balrog | plen = 0;
|
324 | 58a26b47 | balrog | } else
|
325 | 58a26b47 | balrog | break;
|
326 | 58a26b47 | balrog | } |
327 | 58a26b47 | balrog | |
328 | 58a26b47 | balrog | return len;
|
329 | 58a26b47 | balrog | } |
330 | 58a26b47 | balrog | |
331 | 58a26b47 | balrog | static void csrhci_out_hci_packet_event(void *opaque, |
332 | 58a26b47 | balrog | const uint8_t *data, int len) |
333 | 58a26b47 | balrog | { |
334 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) opaque; |
335 | 58a26b47 | balrog | uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */ |
336 | 58a26b47 | balrog | |
337 | 58a26b47 | balrog | *pkt ++ = H4_EVT_PKT; |
338 | 58a26b47 | balrog | memcpy(pkt, data, len); |
339 | 58a26b47 | balrog | |
340 | 58a26b47 | balrog | csrhci_fifo_wake(s); |
341 | 58a26b47 | balrog | } |
342 | 58a26b47 | balrog | |
343 | 58a26b47 | balrog | static void csrhci_out_hci_packet_acl(void *opaque, |
344 | 58a26b47 | balrog | const uint8_t *data, int len) |
345 | 58a26b47 | balrog | { |
346 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) opaque; |
347 | 58a26b47 | balrog | uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */ |
348 | 58a26b47 | balrog | |
349 | 58a26b47 | balrog | *pkt ++ = H4_ACL_PKT; |
350 | 58a26b47 | balrog | pkt[len & ~1] = 0; |
351 | 58a26b47 | balrog | memcpy(pkt, data, len); |
352 | 58a26b47 | balrog | |
353 | 58a26b47 | balrog | csrhci_fifo_wake(s); |
354 | 58a26b47 | balrog | } |
355 | 58a26b47 | balrog | |
356 | 58a26b47 | balrog | static int csrhci_ioctl(struct CharDriverState *chr, int cmd, void *arg) |
357 | 58a26b47 | balrog | { |
358 | 58a26b47 | balrog | QEMUSerialSetParams *ssp; |
359 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) chr->opaque; |
360 | 58a26b47 | balrog | int prev_state = s->modem_state;
|
361 | 58a26b47 | balrog | |
362 | 58a26b47 | balrog | switch (cmd) {
|
363 | 58a26b47 | balrog | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
364 | 58a26b47 | balrog | ssp = (QEMUSerialSetParams *) arg; |
365 | 6ee093c9 | Juan Quintela | s->baud_delay = get_ticks_per_sec() / ssp->speed; |
366 | 58a26b47 | balrog | /* Moments later... (but shorter than 100ms) */
|
367 | 58a26b47 | balrog | s->modem_state |= CHR_TIOCM_CTS; |
368 | 58a26b47 | balrog | break;
|
369 | 58a26b47 | balrog | |
370 | 58a26b47 | balrog | case CHR_IOCTL_SERIAL_GET_TIOCM:
|
371 | 58a26b47 | balrog | *(int *) arg = s->modem_state;
|
372 | 58a26b47 | balrog | break;
|
373 | 58a26b47 | balrog | |
374 | 58a26b47 | balrog | case CHR_IOCTL_SERIAL_SET_TIOCM:
|
375 | 58a26b47 | balrog | s->modem_state = *(int *) arg;
|
376 | 58a26b47 | balrog | if (~s->modem_state & prev_state & CHR_TIOCM_RTS)
|
377 | 58a26b47 | balrog | s->modem_state &= ~CHR_TIOCM_CTS; |
378 | 58a26b47 | balrog | break;
|
379 | 58a26b47 | balrog | |
380 | 58a26b47 | balrog | default:
|
381 | 58a26b47 | balrog | return -ENOTSUP;
|
382 | 58a26b47 | balrog | } |
383 | 58a26b47 | balrog | return 0; |
384 | 58a26b47 | balrog | } |
385 | 58a26b47 | balrog | |
386 | 58a26b47 | balrog | static void csrhci_reset(struct csrhci_s *s) |
387 | 58a26b47 | balrog | { |
388 | 58a26b47 | balrog | s->out_len = 0;
|
389 | 58a26b47 | balrog | s->out_size = FIFO_LEN; |
390 | 58a26b47 | balrog | s->in_len = 0;
|
391 | 6ee093c9 | Juan Quintela | s->baud_delay = get_ticks_per_sec(); |
392 | 58a26b47 | balrog | s->enable = 0;
|
393 | 58a26b47 | balrog | s->in_hdr = INT_MAX; |
394 | 58a26b47 | balrog | s->in_data = INT_MAX; |
395 | 58a26b47 | balrog | |
396 | 58a26b47 | balrog | s->modem_state = 0;
|
397 | 58a26b47 | balrog | /* After a while... (but sooner than 10ms) */
|
398 | 58a26b47 | balrog | s->modem_state |= CHR_TIOCM_CTS; |
399 | 58a26b47 | balrog | |
400 | c227f099 | Anthony Liguori | memset(&s->bd_addr, 0, sizeof(bdaddr_t)); |
401 | 58a26b47 | balrog | } |
402 | 58a26b47 | balrog | |
403 | 58a26b47 | balrog | static void csrhci_out_tick(void *opaque) |
404 | 58a26b47 | balrog | { |
405 | 58a26b47 | balrog | csrhci_fifo_wake((struct csrhci_s *) opaque);
|
406 | 58a26b47 | balrog | } |
407 | 58a26b47 | balrog | |
408 | 58a26b47 | balrog | static void csrhci_pins(void *opaque, int line, int level) |
409 | 58a26b47 | balrog | { |
410 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) opaque; |
411 | 58a26b47 | balrog | int state = s->pin_state;
|
412 | 58a26b47 | balrog | |
413 | 58a26b47 | balrog | s->pin_state &= ~(1 << line);
|
414 | 58a26b47 | balrog | s->pin_state |= (!!level) << line; |
415 | 58a26b47 | balrog | |
416 | 58a26b47 | balrog | if ((state & ~s->pin_state) & (1 << csrhci_pin_reset)) { |
417 | 58a26b47 | balrog | /* TODO: Disappear from lower layers */
|
418 | 58a26b47 | balrog | csrhci_reset(s); |
419 | 58a26b47 | balrog | } |
420 | 58a26b47 | balrog | |
421 | 58a26b47 | balrog | if (s->pin_state == 3 && state != 3) { |
422 | 58a26b47 | balrog | s->enable = 1;
|
423 | 58a26b47 | balrog | /* TODO: Wake lower layers up */
|
424 | 58a26b47 | balrog | } |
425 | 58a26b47 | balrog | } |
426 | 58a26b47 | balrog | |
427 | 58a26b47 | balrog | qemu_irq *csrhci_pins_get(CharDriverState *chr) |
428 | 58a26b47 | balrog | { |
429 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) chr->opaque; |
430 | 58a26b47 | balrog | |
431 | 58a26b47 | balrog | return s->pins;
|
432 | 58a26b47 | balrog | } |
433 | 58a26b47 | balrog | |
434 | 58a26b47 | balrog | CharDriverState *uart_hci_init(qemu_irq wakeup) |
435 | 58a26b47 | balrog | { |
436 | 58a26b47 | balrog | struct csrhci_s *s = (struct csrhci_s *) |
437 | 58a26b47 | balrog | qemu_mallocz(sizeof(struct csrhci_s)); |
438 | 58a26b47 | balrog | |
439 | 58a26b47 | balrog | s->chr.opaque = s; |
440 | 58a26b47 | balrog | s->chr.chr_write = csrhci_write; |
441 | 58a26b47 | balrog | s->chr.chr_ioctl = csrhci_ioctl; |
442 | 58a26b47 | balrog | |
443 | 58a26b47 | balrog | s->hci = qemu_next_hci(); |
444 | 58a26b47 | balrog | s->hci->opaque = s; |
445 | 58a26b47 | balrog | s->hci->evt_recv = csrhci_out_hci_packet_event; |
446 | 58a26b47 | balrog | s->hci->acl_recv = csrhci_out_hci_packet_acl; |
447 | 58a26b47 | balrog | |
448 | 74475455 | Paolo Bonzini | s->out_tm = qemu_new_timer_ns(vm_clock, csrhci_out_tick, s); |
449 | 58a26b47 | balrog | s->pins = qemu_allocate_irqs(csrhci_pins, s, __csrhci_pins); |
450 | 58a26b47 | balrog | csrhci_reset(s); |
451 | 58a26b47 | balrog | |
452 | 58a26b47 | balrog | return &s->chr;
|
453 | 58a26b47 | balrog | } |