misc: Fixed a couple of typos in comments
[simavr] / simavr / sim / avr_usb.c
1 /* vim: set sts=4:sw=4:ts=4:noexpandtab
2         avr_usb.c
3
4         Copyright 2012 Torbjorn Tyridal <ttyridal@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /* TODO correct reset values */
23 /* TODO generate sofi every 1ms (when connected) */
24 /* TODO otg support? */
25 /* TODO drop bitfields? */
26 /* TODO thread safe ioctls */
27 /* TODO dual-bank endpoint buffers */
28 /* TODO actually pay attention to endpoint memory allocation ? buggy endpoint configuration doesn't matter in the simulator now. */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 #include "avr_usb.h"
35
36 enum usb_regs
37 {
38         usbcon = 0,
39         udcon = 8,
40         udint = 9,
41         udien = 10,
42         udaddr = 11,
43         udfnuml = 12,
44         udfnumh = 13,
45         udmfn = 14,
46 //     _res=15,
47         ueintx = 16,
48         uenum = 17,
49         uerst = 18,
50         ueconx = 19,
51         uecfg0x = 20,
52         uecfg1x = 21,
53         uesta0x = 22,
54         uesta1x = 23,
55         ueienx = 24,
56         uedatx = 25,
57         uebclx = 26,
58 //     _res2=27,
59         ueint = 28,
60         otgtcon = 29,
61 };
62
63 union _ueintx {
64         struct {
65                 uint8_t txini :1;
66                 uint8_t stalledi :1;
67                 uint8_t rxouti :1;
68                 uint8_t rxstpi :1;
69                 uint8_t nakouti :1;
70                 uint8_t rwal :1;
71                 uint8_t nakini :1;
72                 uint8_t fifocon :1;
73         };
74         uint8_t v;
75 };
76
77 struct _epstate {
78         union _ueintx ueintx;
79         uint8_t dummy1;
80         uint8_t dummy2;
81         union {
82                 struct {
83                         uint8_t epen :1;
84                         uint8_t res :2;
85                         uint8_t rstdt :1;
86                         uint8_t stallrqc :1;
87                         uint8_t stallrq :1;
88                 };
89                 uint8_t v;
90         } ueconx;
91         union {
92                 struct {
93                         uint8_t epdir :1;
94                         uint8_t res :5;
95                         uint8_t eptype :2;
96                 };
97                 uint8_t v;
98         } uecfg0x;
99         union {
100                 struct {
101                         uint8_t res :1;
102                         uint8_t alloc :1;
103                         uint8_t epbk1 :2;
104                         uint8_t epsize :3;
105                         uint8_t res2 :1;
106                 };
107                 uint8_t v;
108         } uecfg1x;
109         union {
110                 struct {
111                         uint8_t nbusybk :2;
112                         uint8_t dtseq :2;
113                         uint8_t res :1;
114                         uint8_t underfi :1;
115                         uint8_t overfi :1;
116                         uint8_t cfgok :1;
117                 };
118                 uint8_t v;
119         } uesta0x;
120         union {
121                 struct {
122                         uint8_t curbk :2;
123                         uint8_t ctrldir :1;
124                         uint8_t res :5;
125                 };
126                 uint8_t v;
127         } uesta1x;
128         union {
129                 struct {
130                         uint8_t txine :1;
131                         uint8_t stallede :1;
132                         uint8_t rxoute :1;
133                         uint8_t rxstpe :1;
134                         uint8_t nakoute :1;
135                         uint8_t res :1;
136                         uint8_t nakine :1;
137                         uint8_t flerre :1;
138                 };
139                 uint8_t v;
140         } ueienx;
141
142         struct {
143                 uint8_t bytes[64];
144                 uint8_t tail;
145         } bank[2];
146         uint8_t current_bank;
147         int setup_is_read;
148 };
149
150 struct usb_internal_state {
151         struct _epstate ep_state[5];
152         avr_int_vector_t com_vect;
153         avr_int_vector_t gen_vect;
154 };
155
156 const uint8_t num_endpoints = 5;//sizeof (struct usb_internal_state.ep_state) / sizeof (struct usb_internal_state.ep_state[0]);
157
158 static uint8_t
159 current_ep_to_cpu(
160                 avr_usb_t * p)
161 {
162         return p->io.avr->data[p->r_usbcon + uenum];
163 }
164
165 static struct _epstate *
166 get_epstate(
167                 avr_usb_t * p,
168                 uint8_t ep)
169 {
170         assert(ep < num_endpoints);
171         return &p->state->ep_state[ep];
172 }
173
174
175 enum epints {
176         txini = 0,
177         stalledi = 1,
178         rxouti = 2,
179         rxstpi = 3,
180         nakouti = 4,
181         nakini = 6,
182         overfi = 10,
183         underfi = 11,
184 };
185
186 static void
187 raise_ep_interrupt(
188         struct avr_t * avr,
189         avr_usb_t * p,
190         uint8_t ep,
191         enum epints irq)
192 {
193         struct _epstate * epstate = get_epstate(p, ep);
194         assert(ep < num_endpoints);
195         avr->data[p->r_usbcon + ueint] |= 1 << ep;
196         switch (irq) {
197                 case txini:
198                 case stalledi:
199                 case rxouti:
200                 case nakouti:
201                 case nakini:
202                         epstate->ueintx.v |= 1 << irq;
203                         if (epstate->ueienx.v & (1 << irq))
204                                 avr_raise_interrupt(avr, &p->state->com_vect);
205                         break;
206                 case rxstpi:
207                         epstate->ueintx.v |= 1 << irq;
208                         if (epstate->ueienx.v & (1 << irq))
209                                 avr_raise_interrupt(avr, &p->state->com_vect);
210                         break;
211                 case overfi:
212                         epstate->uesta0x.overfi = 1;
213                         if (epstate->ueienx.flerre)
214                                 avr_raise_interrupt(avr, &p->state->com_vect);
215                         break;
216                 case underfi:
217                         epstate->uesta0x.underfi = 1;
218                         if (epstate->ueienx.flerre)
219                                 avr_raise_interrupt(avr, &p->state->com_vect);
220                         break;
221                 default:
222                         assert(0);
223         }
224 }
225
226 enum usbints {
227         suspi = 0, sofi = 2, eorsti = 3, wakeupi = 4, eorsmi = 5, uprsmi = 6
228 };
229 static void
230 raise_usb_interrupt(
231                 avr_usb_t * p,
232                 enum usbints irq)
233 {
234         uint8_t * Rudien = &p->io.avr->data[p->r_usbcon + udien];
235         uint8_t * Rudint = &p->io.avr->data[p->r_usbcon + udint];
236
237         switch (irq) {
238                 case uprsmi:
239                 case eorsmi:
240                 case wakeupi:
241                 case eorsti:
242                 case sofi:
243                 case suspi:
244                         *Rudint |= 1 << irq;
245                         if (*Rudien & (1 << irq))
246                                 avr_raise_interrupt(p->io.avr, &p->state->gen_vect);
247                         break;
248                 default:
249                         assert(0);
250         }
251
252 }
253
254 static void
255 reset_endpoints(
256                 struct avr_t * avr,
257                 avr_usb_t * p)
258 {
259         memset(&p->state->ep_state[1], 0,
260                 sizeof p->state->ep_state - sizeof p->state->ep_state[0]);
261 }
262
263 static int
264 ep_fifo_empty(
265                 struct _epstate * epstate)
266 {
267         return epstate->bank[epstate->current_bank].tail == 0;
268 }
269
270 static int
271 ep_fifo_full(
272                 struct _epstate * epstate)
273 {
274         return epstate->bank[epstate->current_bank].tail >=
275                                         (8 << epstate->uecfg1x.epsize);
276 }
277
278 static uint8_t
279 ep_fifo_size(
280                 struct _epstate * epstate)
281 {
282         assert(epstate->ueconx.epen);
283         return (8 << epstate->uecfg1x.epsize);
284 }
285
286 static uint8_t
287 ep_fifo_count(
288                 struct _epstate * epstate)
289 {
290         return epstate->bank[epstate->current_bank].tail;
291 }
292
293 static int
294 ep_fifo_cpu_readbyte(
295                 struct _epstate * epstate)
296 {
297         uint8_t i, j;
298         uint8_t v = epstate->bank[epstate->current_bank].bytes[0];
299
300         if (!epstate->ueconx.epen) {
301                 printf("WARNING! Adding bytes to non configured endpoint\n");
302                 return -1;
303         }
304
305         if (ep_fifo_empty(epstate))
306                 return -2;
307
308         for (i = 0, j = ep_fifo_count(epstate) - 1; i < j; i++)
309                 epstate->bank[epstate->current_bank].bytes[i] =
310                         epstate->bank[epstate->current_bank].bytes[i + 1];
311         epstate->bank[epstate->current_bank].tail--;
312         return v;
313 }
314
315 static int
316 ep_fifo_cpu_writebyte(
317                 struct _epstate * epstate,
318                 uint8_t v)
319 {
320         if (!epstate->ueconx.epen) {
321                 printf("WARNING! Adding bytes to non configured endpoint\n");
322                 return -1;
323         }
324         if (ep_fifo_full(epstate))
325                 return -2;
326
327         epstate->bank[epstate->current_bank].bytes[epstate->bank[epstate->current_bank].tail++] = v;
328         return 0;
329 }
330
331 static int
332 ep_fifo_usb_read(
333                 struct _epstate * epstate,
334                 uint8_t * buf)
335 {
336         if (!epstate->ueconx.epen) {
337                 printf("WARNING! Reading from non configured endpoint\n");
338                 return -1;
339         }
340         if (epstate->ueintx.txini) {
341                 return AVR_IOCTL_USB_NAK;
342         }
343         if (epstate->ueintx.fifocon && epstate->uecfg0x.eptype != 0) {
344                 return AVR_IOCTL_USB_NAK;
345         }
346
347         int ret = epstate->bank[epstate->current_bank].tail;
348         memcpy(buf, epstate->bank[epstate->current_bank].bytes,
349                 epstate->bank[epstate->current_bank].tail);
350         epstate->bank[epstate->current_bank].tail = 0;
351         return ret;
352 }
353
354 static int
355 ep_fifo_usb_write(
356         struct _epstate * epstate,
357         uint8_t * buf,
358         uint8_t len)
359 {
360         if (!epstate->ueconx.epen) {
361                 printf("WARNING! Adding bytes to non configured endpoint\n");
362                 return -1;
363         }
364
365         if (epstate->ueintx.rxouti) {
366                 return AVR_IOCTL_USB_NAK;
367         }
368         if (epstate->ueintx.fifocon && epstate->uecfg0x.eptype != 0) {
369                 return AVR_IOCTL_USB_NAK;
370         }
371
372         if (len > ep_fifo_size(epstate)) {
373                 printf("EP OVERFI\n");
374                 len = sizeof epstate->bank[epstate->current_bank].bytes;
375         }memcpy(epstate->bank[epstate->current_bank].bytes, buf, len);
376         epstate->bank[epstate->current_bank].tail = len;
377
378         return 0;
379 }
380
381 static uint8_t
382 avr_usb_ep_read_bytecount(
383         struct avr_t * avr,
384         avr_io_addr_t addr,
385         void * param)
386 {
387         avr_usb_t * p = (avr_usb_t *) param;
388         return ep_fifo_count(get_epstate(p, current_ep_to_cpu(p)));
389 }
390
391 static void
392 avr_usb_udaddr_write(
393         struct avr_t * avr,
394         avr_io_addr_t addr,
395         uint8_t v,
396         void * param)
397 {
398         if (v & 0x80)
399                 printf("Activate address %d\n", v & 0x7f);
400         avr_core_watch_write(avr, addr, v);
401 }
402
403 static void
404 avr_usb_udcon_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
405 {
406         avr_usb_t * p = (avr_usb_t *)param;
407
408         if(avr->data[addr]&1 && !(v&1))
409                 avr_raise_irq(p->io.irq + USB_IRQ_ATTACH, !(v&1));
410         avr_core_watch_write(avr, addr, v);
411 }
412
413 static void
414 avr_usb_uenum_write(
415         struct avr_t * avr,
416         avr_io_addr_t addr,
417         uint8_t v,
418         void * param)
419 {
420         assert(v < num_endpoints);
421         avr_core_watch_write(avr, addr, v);
422 }
423
424 static uint8_t
425 avr_usb_ep_read_ueintx(
426         struct avr_t * avr,
427         avr_io_addr_t addr,
428         void * param)
429 {
430         avr_usb_t * p = (avr_usb_t *) param;
431         uint8_t ep = current_ep_to_cpu(p);
432
433         if (p->state->ep_state[ep].uecfg0x.epdir)
434                 p->state->ep_state[ep].ueintx.rwal = !ep_fifo_full(get_epstate(p, ep));
435         else
436                 p->state->ep_state[ep].ueintx.rwal = !ep_fifo_empty(get_epstate(p, ep));
437
438         return p->state->ep_state[ep].ueintx.v;
439 }
440
441 static void
442 avr_usb_ep_write_ueintx(
443         struct avr_t * avr,
444         avr_io_addr_t addr,
445         uint8_t v,
446         void * param)
447 {
448         avr_usb_t * p = (avr_usb_t *) param;
449         uint8_t ep = current_ep_to_cpu(p);
450
451         union _ueintx * newstate = (union _ueintx*) &v;
452         union _ueintx * curstate = &p->state->ep_state[ep].ueintx;
453
454         if (curstate->rxouti & !newstate->rxouti)
455                 curstate->rxouti = 0;
456         if (curstate->txini & !newstate->txini)
457                 curstate->txini = 0;
458         if (curstate->rxstpi & !newstate->rxstpi) {
459                 curstate->txini = 1;
460                 curstate->rxouti = 0;
461                 curstate->rxstpi = 0;
462         }
463         if (curstate->fifocon & !newstate->fifocon)
464                 curstate->fifocon = 0;
465         if (curstate->nakini & !newstate->nakini)
466                 curstate->nakini = 0;
467         if (curstate->nakouti & !newstate->nakouti)
468                 curstate->nakouti = 0;
469         if (curstate->stalledi & !newstate->stalledi)
470                 curstate->stalledi = 0;
471         if (curstate->rwal & !newstate->rwal)
472                 printf("pointless change of ueintx.rwal\n");
473
474         if ((curstate->v & 0xdf) == 0)
475                 avr->data[p->r_usbcon + ueint] &= 0xff ^ (1 << ep); // mark ep0 interrupt
476 }
477
478 static uint8_t
479 avr_usb_ep_read(
480         struct avr_t * avr,
481         avr_io_addr_t addr,
482         void * param)
483 {
484         avr_usb_t * p = (avr_usb_t *) param;
485         uint8_t laddr = addr - p->r_usbcon;
486         uint8_t v;
487         struct _epstate * epstate = get_epstate(p, current_ep_to_cpu(p));
488
489         switch(laddr) {
490                 case ueconx:  v = epstate->ueconx.v; break;
491                 case uecfg0x: v = epstate->uecfg0x.v; break;
492                 case uecfg1x: v = epstate->uecfg1x.v; break;
493                 case uesta0x: v = epstate->uesta0x.v; break;
494                 case uesta1x: v = epstate->uesta1x.v; break;
495                 case ueienx:  v = epstate->ueienx.v; break;
496                 default:assert(0);
497         }
498         return v;
499 }
500
501 static void
502 avr_usb_ep_write(
503         struct avr_t * avr,
504         avr_io_addr_t addr,
505         uint8_t v,
506         void * param)
507 {
508         avr_usb_t * p = (avr_usb_t *) param;
509         struct _epstate * epstate = get_epstate(p, current_ep_to_cpu(p));
510         uint8_t laddr = addr - p->r_usbcon;
511
512         switch (laddr) {
513                 case ueconx:
514                         if (v & 1 << 4)
515                                 epstate->ueconx.stallrq = 0;
516                         if (v & 1 << 5)
517                                 epstate->ueconx.stallrq = 1;
518                         epstate->ueconx.epen = (v & 1) != 0;
519                         break;
520                 case uecfg0x:
521                         epstate->uecfg0x.v = v;
522                         epstate->uesta0x.cfgok = 0;
523                         break;
524                 case uecfg1x:
525                         epstate->uecfg1x.v = v;
526                         epstate->uesta0x.cfgok = epstate->uecfg1x.alloc;
527                         if (epstate->uecfg0x.eptype == 0)
528                                 epstate->ueintx.txini = 1;
529                         else if (epstate->uecfg0x.epdir) {
530                                 epstate->ueintx.txini = 1;
531                                 epstate->ueintx.rwal = 1;
532                                 epstate->ueintx.fifocon = 1;
533                         } else
534                                 epstate->ueintx.rxouti = 0;
535                         avr_core_watch_write(avr, p->r_usbcon + uesta0x,
536                                 epstate->uesta0x.v);
537                         break;
538                 case uesta0x:
539                         v = (epstate->uesta0x.v & 0x9f) + (v & (0x60 & epstate->uesta0x.v));
540                         epstate->uesta0x.v = v;
541                         break;
542                 case ueienx:
543                         epstate->ueienx.v = v;
544                         break;
545                 default:
546                         assert(0);
547         }
548 }
549
550 static uint8_t
551 avr_usb_ep_read_data(
552         struct avr_t * avr,
553         avr_io_addr_t addr,
554         void * param)
555 {
556         avr_usb_t * p = (avr_usb_t *) param;
557         int ret = ep_fifo_cpu_readbyte(get_epstate(p, current_ep_to_cpu(p)));
558
559         if (ret < 0) {
560                 if (ret == -2)
561                         raise_ep_interrupt(avr, p, current_ep_to_cpu(p), underfi);
562                 return 0;
563         } else
564                 return (uint8_t) ret;
565 }
566
567 static void
568 avr_usb_ep_write_data(
569         struct avr_t * avr,
570         avr_io_addr_t addr,
571         uint8_t v,
572         void * param)
573 {
574         avr_usb_t * p = (avr_usb_t *) param;
575         int ret = ep_fifo_cpu_writebyte(get_epstate(p, current_ep_to_cpu(p)), v);
576         if (ret == 0)
577                 return;
578
579         if (ret == -2)
580                 raise_ep_interrupt(avr, p, current_ep_to_cpu(p), overfi);
581 }
582
583 static void
584 avr_usb_pll_write(
585         struct avr_t * avr,
586         avr_io_addr_t addr,
587         uint8_t v,
588         void * param)
589 {
590         v |= (v >> 1) & 1;
591         avr_core_watch_write(avr, addr, v);
592 }
593
594
595 avr_cycle_count_t
596 sof_generator(
597         struct avr_t * avr,
598         avr_cycle_count_t when,
599         void * param)
600 {
601         avr_usb_t * p = (avr_usb_t *) param;
602         //stop sof generation if detached
603         if (avr->data[p->r_usbcon + udcon] & 1)
604                 return 0;
605         else {
606                 raise_usb_interrupt(p, sofi);
607                 return when;
608         }
609 }
610
611 static int
612 avr_usb_ioctl(
613                 struct avr_io_t * io,
614                 uint32_t ctl,
615                 void * io_param)
616 {
617         avr_usb_t * p = (avr_usb_t *) io;
618         struct avr_io_usb * d = (struct avr_io_usb*) io_param;
619         struct _epstate * epstate = 0;
620         int ret;
621         uint8_t ep;
622
623         switch (ctl) {
624                 case AVR_IOCTL_USB_READ:
625                         ep = d->pipe & 0x7f;
626                         epstate = get_epstate(p, ep);
627
628                         if (epstate->ueconx.stallrq) {
629                                 raise_ep_interrupt(io->avr, p, 0, stalledi);
630                                 return AVR_IOCTL_USB_STALL;
631                         }
632                         if (ep && !epstate->uecfg0x.epdir)
633                                 printf("Reading from IN endpoint from host??\n");
634
635                         ret = ep_fifo_usb_read(epstate, d->buf);
636                         if (ret < 0) {
637                                 // is this correct? It makes the cdc example work.
638                                 // Linux stops polling the data ep if we send naks,but
639                                 // according to usb spec nak'ing should be ok.
640                                 if (epstate->uecfg0x.eptype == 2) {
641                                         d->sz = 0;
642                                         return 0;
643                                 } else
644                                         return ret;
645                         }
646                         d->sz = ret;
647                         ret = 0;
648                         epstate->ueintx.fifocon = 1;
649                         raise_ep_interrupt(io->avr, p, ep, txini);
650                         return ret;
651                 case AVR_IOCTL_USB_WRITE:
652                         ep = d->pipe & 0x7f;
653                         epstate = get_epstate(p, ep);
654
655                         if (ep && epstate->uecfg0x.epdir)
656                                 printf("Writing to IN endpoint from host??\n");
657
658                         if (epstate->ueconx.stallrq) {
659                                 raise_ep_interrupt(io->avr, p, 0, stalledi);
660                                 return AVR_IOCTL_USB_STALL;
661                         }
662
663                         ret = ep_fifo_usb_write(epstate, d->buf, d->sz);
664                         if (ret < 0)
665                                 return ret;
666
667                         epstate->ueintx.fifocon = 1;
668                         raise_ep_interrupt(io->avr, p, ep, rxouti);
669                         return 0;
670                 case AVR_IOCTL_USB_SETUP:
671                         ep = d->pipe & 0x7f;
672                         epstate = get_epstate(p, ep);
673
674                         epstate->ueconx.stallrq = 0;
675                         // teensy actually depends on this (fails to ack rxouti on usb
676                         // control read status stage) even if the datasheet clearly states
677                         // that one should do so.
678                         epstate->ueintx.rxouti = 0;
679
680                         ret = ep_fifo_usb_write(epstate, d->buf, d->sz);
681                         if (ret < 0)
682                                 return ret;
683                         raise_ep_interrupt(io->avr, p, ep, rxstpi);
684
685                         return 0;
686                 case AVR_IOCTL_USB_RESET:
687                         printf("__USB_RESET__\n");
688                         reset_endpoints(io->avr, p);
689                         raise_usb_interrupt(p, eorsti);
690                         if (0)
691                                 avr_cycle_timer_register_usec(io->avr, 1000, sof_generator, p);
692                         return 0;
693                 default:
694                         return -1;
695         }
696 }
697
698 void
699 avr_usb_reset(
700                 struct avr_io_t *io)
701 {
702         avr_usb_t * p = (avr_usb_t *) io;
703         uint8_t i;
704
705         memset(p->state->ep_state, 0, sizeof p->state->ep_state);
706
707         for (i = 0; i < otgtcon; i++)
708                 p->io.avr->data[p->r_usbcon + i] = 0;
709
710         p->io.avr->data[p->r_usbcon] = 0x20;
711         p->io.avr->data[p->r_usbcon + udcon] = 1;
712
713         printf("%s\n", __FUNCTION__);
714 }
715
716 static const char * irq_names[USB_IRQ_COUNT] = {
717         [USB_IRQ_ATTACH] = ">attach",
718 };
719
720 static void
721 avr_usb_dealloc(
722                 struct avr_io_t * port)
723 {
724         avr_usb_t * p = (avr_usb_t *) port;
725         free(p->state);
726 }
727
728 static  avr_io_t        _io = {
729         .kind = "usb",
730         .reset = avr_usb_reset,
731         .irq_names = irq_names,
732         .ioctl = avr_usb_ioctl,
733         .dealloc = avr_usb_dealloc,
734 };
735
736 static void
737 register_io_ep_readwrite(
738                 avr_t * avr,
739                 avr_usb_t * p,
740                 uint8_t laddr)
741 {
742         avr_register_io_write(avr, p->r_usbcon + laddr, avr_usb_ep_write, p);
743         avr_register_io_read(avr, p->r_usbcon + laddr, avr_usb_ep_read, p);
744 }
745
746 static void
747 register_vectors(
748                 avr_t * avr,
749                 avr_usb_t * p)
750 {
751         // usb interrupts are multiplexed into just two vectors.
752         // we therefore need fake bits for enable & raise
753
754         // use usbe as fake enable bit
755         p->state->com_vect.enable = (avr_regbit_t)AVR_IO_REGBIT(p->r_usbcon, 7);
756         p->state->gen_vect.enable = (avr_regbit_t)AVR_IO_REGBIT(p->r_usbcon, 7);
757
758 //      // use reserved/unused bits in usbsta as fake raised bits
759 //      p->state->com_vect.raised = (avr_regbit_t)AVR_IO_REGBIT(p->r_usbcon+1,7);
760 //      p->state->gen_vect.raised = (avr_regbit_t)AVR_IO_REGBIT(p->r_usbcon+1,6);
761
762         p->state->com_vect.vector = p->usb_com_vect;
763         p->state->gen_vect.vector = p->usb_gen_vect;
764
765         avr_register_vector(avr, &p->state->com_vect);
766         avr_register_vector(avr, &p->state->gen_vect);
767 }
768
769 void avr_usb_init(avr_t * avr, avr_usb_t * p)
770 {
771         p->io = _io;
772
773         p->state = calloc(1, sizeof *p->state);
774
775         avr_register_io(avr, &p->io);
776         register_vectors(avr, p);
777         // allocate this module's IRQ
778         avr_io_setirqs(&p->io, AVR_IOCTL_USB_GETIRQ(), USB_IRQ_COUNT, NULL);
779
780         avr_register_io_write(avr, p->r_usbcon + udaddr, avr_usb_udaddr_write, p);
781         avr_register_io_write(avr, p->r_usbcon + udcon, avr_usb_udcon_write, p);
782         avr_register_io_write(avr, p->r_usbcon + uenum, avr_usb_uenum_write, p);
783
784         avr_register_io_read(avr, p->r_usbcon + uedatx, avr_usb_ep_read_data, p);
785         avr_register_io_write(avr, p->r_usbcon + uedatx, avr_usb_ep_write_data, p);
786         avr_register_io_read(avr, p->r_usbcon + uebclx, avr_usb_ep_read_bytecount, p); //ro
787
788         avr_register_io_read(avr, p->r_usbcon + ueintx, avr_usb_ep_read_ueintx, p);
789         avr_register_io_write(avr, p->r_usbcon + ueintx, avr_usb_ep_write_ueintx, p);
790
791         register_io_ep_readwrite(avr, p, ueconx);
792         register_io_ep_readwrite(avr, p, uecfg0x);
793         register_io_ep_readwrite(avr, p, uecfg1x);
794         register_io_ep_readwrite(avr, p, uesta0x);
795         register_io_ep_readwrite(avr, p, uesta1x);
796         register_io_ep_readwrite(avr, p, ueienx);
797
798         avr_register_io_write(avr, p->r_pllcsr, avr_usb_pll_write, p);
799 }
800