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