summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/target/mips/ingenic_jz47xx/usb-jz4760.c251
1 files changed, 108 insertions, 143 deletions
diff --git a/firmware/target/mips/ingenic_jz47xx/usb-jz4760.c b/firmware/target/mips/ingenic_jz47xx/usb-jz4760.c
index 474d45edee..8ff6d4bc1e 100644
--- a/firmware/target/mips/ingenic_jz47xx/usb-jz4760.c
+++ b/firmware/target/mips/ingenic_jz47xx/usb-jz4760.c
@@ -54,16 +54,14 @@
54 OUT = HOST->DEV, (ie we recv) 54 OUT = HOST->DEV, (ie we recv)
55*/ 55*/
56 56
57enum ep_type 57enum ep_type {
58{
59 ep_control, 58 ep_control,
60 ep_bulk, 59 ep_bulk,
61 ep_interrupt, 60 ep_interrupt,
62 ep_isochronous 61 ep_isochronous
63}; 62};
64 63
65struct usb_endpoint 64struct usb_endpoint {
66{
67 const enum ep_type type; 65 const enum ep_type type;
68 const long fifo_addr; 66 const long fifo_addr;
69 unsigned short fifo_size; 67 unsigned short fifo_size;
@@ -76,8 +74,7 @@ struct usb_endpoint
76 74
77 volatile void *buf; 75 volatile void *buf;
78 volatile size_t length; 76 volatile size_t length;
79 union 77 union {
80 {
81 volatile size_t sent; 78 volatile size_t sent;
82 volatile size_t received; 79 volatile size_t received;
83 }; 80 };
@@ -94,8 +91,7 @@ struct usb_endpoint
94#define short_not_ok 1 /* only works for mass storage.. */ 91#define short_not_ok 1 /* only works for mass storage.. */
95#define ep_doublebuf(__ep) 0 92#define ep_doublebuf(__ep) 0
96 93
97static union 94static union {
98{
99 int buf[64 / sizeof(int)]; 95 int buf[64 / sizeof(int)];
100 struct usb_ctrlrequest request; 96 struct usb_ctrlrequest request;
101} ep0_rx; 97} ep0_rx;
@@ -106,7 +102,7 @@ static volatile bool ep0_data_requested = false;
106static struct usb_endpoint endpoints[] = 102static struct usb_endpoint endpoints[] =
107{ 103{
108 EP_INIT(ep_control, USB_FIFO_EP(0), 64, NULL), 104 EP_INIT(ep_control, USB_FIFO_EP(0), 64, NULL),
109 EP_INIT(ep_control, USB_FIFO_EP(0), 64, ep0_rx.buf), 105 EP_INIT(ep_control, USB_FIFO_EP(0), 64, NULL),
110 EP_INIT(ep_bulk, USB_FIFO_EP(1), 512, NULL), 106 EP_INIT(ep_bulk, USB_FIFO_EP(1), 512, NULL),
111 EP_INIT(ep_bulk, USB_FIFO_EP(1), 512, NULL), 107 EP_INIT(ep_bulk, USB_FIFO_EP(1), 512, NULL),
112 EP_INIT(ep_interrupt, USB_FIFO_EP(2), 512, NULL), 108 EP_INIT(ep_interrupt, USB_FIFO_EP(2), 512, NULL),
@@ -127,19 +123,14 @@ static void readFIFO(struct usb_endpoint *ep, unsigned int size)
127 register unsigned int s = size >> 2; 123 register unsigned int s = size >> 2;
128 register unsigned int x; 124 register unsigned int x;
129 125
130 if(size > 0) 126 if(size > 0) {
131 { 127 if (((unsigned int)ptr & 3) == 0) {
132 if( ((unsigned int)ptr & 3) == 0 ) 128 while(s--) {
133 {
134 while(s--)
135 *ptr32++ = REG32(ep->fifo_addr); 129 *ptr32++ = REG32(ep->fifo_addr);
136 130 }
137 ptr = (unsigned char*)ptr32; 131 ptr = (unsigned char*)ptr32;
138 } 132 } else {
139 else 133 while(s--) {
140 {
141 while(s--)
142 {
143 x = REG32(ep->fifo_addr); 134 x = REG32(ep->fifo_addr);
144 *ptr++ = x & 0xFF; x >>= 8; 135 *ptr++ = x & 0xFF; x >>= 8;
145 *ptr++ = x & 0xFF; x >>= 8; 136 *ptr++ = x & 0xFF; x >>= 8;
@@ -147,10 +138,10 @@ static void readFIFO(struct usb_endpoint *ep, unsigned int size)
147 *ptr++ = x; 138 *ptr++ = x;
148 } 139 }
149 } 140 }
150
151 s = size & 3; 141 s = size & 3;
152 while(s--) 142 while(s--) {
153 *ptr++ = REG8(ep->fifo_addr); 143 *ptr++ = REG8(ep->fifo_addr);
144 }
154 } 145 }
155} 146}
156 147
@@ -163,18 +154,14 @@ static void writeFIFO(struct usb_endpoint *ep, size_t size)
163 register size_t s = size >> 2; 154 register size_t s = size >> 2;
164 register unsigned int x; 155 register unsigned int x;
165 156
166 if(size > 0) 157 if (size > 0) {
167 { 158 if (((unsigned int)d8 & 3) == 0) {
168 if( ((unsigned int)d8 & 3) == 0 ) 159 while (s--) {
169 {
170 while (s--)
171 REG32(ep->fifo_addr) = *d32++; 160 REG32(ep->fifo_addr) = *d32++;
161 }
172 d8 = (unsigned char *)d32; 162 d8 = (unsigned char *)d32;
173 } 163 } else {
174 else 164 while (s--) {
175 {
176 while (s--)
177 {
178 x = (unsigned int)(*d8++) & 0xff; 165 x = (unsigned int)(*d8++) & 0xff;
179 x |= ((unsigned int)(*d8++) & 0xff) << 8; 166 x |= ((unsigned int)(*d8++) & 0xff) << 8;
180 x |= ((unsigned int)(*d8++) & 0xff) << 16; 167 x |= ((unsigned int)(*d8++) & 0xff) << 16;
@@ -182,11 +169,9 @@ static void writeFIFO(struct usb_endpoint *ep, size_t size)
182 REG32(ep->fifo_addr) = x; 169 REG32(ep->fifo_addr) = x;
183 } 170 }
184 } 171 }
185 172 s = size & 3;
186 if( (s = size & 3) ) 173 while (s--) {
187 { 174 REG8(ep->fifo_addr) = *d8++;
188 while (s--)
189 REG8(ep->fifo_addr) = *d8++;
190 } 175 }
191 } 176 }
192} 177}
@@ -195,18 +180,17 @@ static void flushFIFO(struct usb_endpoint *ep)
195{ 180{
196 logf("%s(%d)", __func__, EP_NUMBER(ep)); 181 logf("%s(%d)", __func__, EP_NUMBER(ep));
197 182
198 switch (ep->type) 183 switch (ep->type) {
199 { 184 case ep_control:
200 case ep_control:
201 break; 185 break;
202 186 case ep_bulk:
203 case ep_bulk: 187 case ep_interrupt:
204 case ep_interrupt: 188 case ep_isochronous:
205 case ep_isochronous: 189 if (EP_IS_IN(ep)) {
206 if(EP_IS_IN(ep)) 190 REG_USB_INCSR |= (USB_INCSR_FF | USB_INCSR_CDT);
207 REG_USB_INCSR |= (USB_INCSR_FF | USB_INCSR_CDT); 191 } else {
208 else 192 REG_USB_OUTCSR |= (USB_OUTCSR_FF | USB_OUTCSR_CDT);
209 REG_USB_OUTCSR |= (USB_OUTCSR_FF | USB_OUTCSR_CDT); 193 }
210 break; 194 break;
211 } 195 }
212} 196}
@@ -217,8 +201,9 @@ static inline void ep_transfer_completed(struct usb_endpoint* ep)
217 ep->length = 0; 201 ep->length = 0;
218 ep->buf = NULL; 202 ep->buf = NULL;
219 ep->busy = false; 203 ep->busy = false;
220 if(ep->wait) 204 if (ep->wait) {
221 semaphore_release(&ep->complete); 205 semaphore_release(&ep->complete);
206 }
222} 207}
223 208
224static void EP0_send(void) 209static void EP0_send(void)
@@ -232,27 +217,26 @@ static void EP0_send(void)
232 217
233 logf("%s(): 0x%x %d %d", __func__, csr0, ep->sent, ep->length); 218 logf("%s(): 0x%x %d %d", __func__, csr0, ep->sent, ep->length);
234 219
235 if(ep->sent == 0) 220 if (ep->sent == 0) {
236 {
237 length = MIN(ep->length, ep->fifo_size); 221 length = MIN(ep->length, ep->fifo_size);
238 REG_USB_CSR0 = (csr0 | USB_CSR0_FLUSHFIFO); 222 REG_USB_CSR0 = (csr0 | USB_CSR0_FLUSHFIFO);
239 } 223 } else {
240 else
241 length = MIN(EP_BUF_LEFT(ep), ep->fifo_size); 224 length = MIN(EP_BUF_LEFT(ep), ep->fifo_size);
225 }
242 226
243 writeFIFO(ep, length); 227 writeFIFO(ep, length);
244 ep->sent += length; 228 ep->sent += length;
245 229
246 if(ep->sent >= ep->length) 230 if (ep->sent >= ep->length) {
247 {
248 REG_USB_CSR0 = (csr0 | USB_CSR0_INPKTRDY | USB_CSR0_DATAEND); /* Set data end! */ 231 REG_USB_CSR0 = (csr0 | USB_CSR0_INPKTRDY | USB_CSR0_DATAEND); /* Set data end! */
249 if (!ep->wait) 232 if (!ep->wait) {
250 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, 0, ep->sent); 233 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, 0, ep->sent);
234 }
251 ep->rc = 0; 235 ep->rc = 0;
252 ep_transfer_completed(ep); 236 ep_transfer_completed(ep);
253 } 237 } else {
254 else
255 REG_USB_CSR0 = (csr0 | USB_CSR0_INPKTRDY); 238 REG_USB_CSR0 = (csr0 | USB_CSR0_INPKTRDY);
239 }
256} 240}
257 241
258static void EP0_handler(void) 242static void EP0_handler(void)
@@ -270,8 +254,7 @@ static void EP0_handler(void)
270 /* Check for SentStall: 254 /* Check for SentStall:
271 This bit is set when a STALL handshake is transmitted. The CPU should clear this bit. 255 This bit is set when a STALL handshake is transmitted. The CPU should clear this bit.
272 */ 256 */
273 if(csr0 & USB_CSR0_SENTSTALL) 257 if (csr0 & USB_CSR0_SENTSTALL) {
274 {
275 REG_USB_CSR0 = csr0 & ~USB_CSR0_SENTSTALL; 258 REG_USB_CSR0 = csr0 & ~USB_CSR0_SENTSTALL;
276 return; 259 return;
277 } 260 }
@@ -281,68 +264,62 @@ static void EP0_handler(void)
281 An interrupt will be generated and the FIFO flushed at this time. 264 An interrupt will be generated and the FIFO flushed at this time.
282 The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit. 265 The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit.
283 */ 266 */
284 if(csr0 & USB_CSR0_SETUPEND) 267 if (csr0 & USB_CSR0_SETUPEND) {
285 {
286 csr0 |= USB_CSR0_SVDSETUPEND; 268 csr0 |= USB_CSR0_SVDSETUPEND;
287 REG_USB_CSR0 = csr0; 269 REG_USB_CSR0 = csr0;
288 ep0_data_supplied = false; 270 ep0_data_supplied = false;
289 ep0_data_requested = false; 271 ep0_data_requested = false;
290 if (ep_send->busy) 272 if (ep_send->busy) {
291 {
292 if (!ep_send->wait) 273 if (!ep_send->wait)
293 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, -1, 0); 274 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, -1, 0);
294 ep_transfer_completed(ep_send); 275 ep_transfer_completed(ep_send);
295 } 276 }
296 if (ep_recv->busy) 277 if (ep_recv->busy) {
297 {
298 usb_core_transfer_complete(EP_CONTROL, USB_DIR_OUT, -1, 0); 278 usb_core_transfer_complete(EP_CONTROL, USB_DIR_OUT, -1, 0);
299 ep_transfer_completed(ep_recv); 279 ep_transfer_completed(ep_recv);
300 } 280 }
301 } 281 }
302 282
303 /* Call relevant routines for endpoint 0 state */ 283 /* Call relevant routines for endpoint 0 state */
304 if(csr0 & USB_CSR0_OUTPKTRDY) /* There is a packet in the fifo */ 284 if (csr0 & USB_CSR0_OUTPKTRDY) { /* There is a packet in the fifo */
305 { 285 if (ep_send->busy) {
306 if (ep_send->busy) 286 if (!ep_send->wait) {
307 {
308 if (!ep_send->wait)
309 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, -1, 0); 287 usb_core_transfer_complete(EP_CONTROL, USB_DIR_IN, -1, 0);
288 }
310 ep_transfer_completed(ep_send); 289 ep_transfer_completed(ep_send);
311 } 290 }
312 if (ep_recv->busy && ep_recv->buf && ep_recv->length) 291 if (ep_recv->busy && ep_recv->buf && ep_recv->length) {
313 {
314 unsigned int size = REG_USB_COUNT0; 292 unsigned int size = REG_USB_COUNT0;
315 readFIFO(ep_recv, size); 293 readFIFO(ep_recv, size);
316 ep_recv->received += size; 294 ep_recv->received += size;
317 if (size < ep_recv->fifo_size || ep_recv->received >= ep_recv->length) 295 if (size < ep_recv->fifo_size || ep_recv->received >= ep_recv->length) {
318 {
319 REG_USB_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND; /* Set data end! */ 296 REG_USB_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND; /* Set data end! */
320 usb_core_transfer_complete(EP_CONTROL, USB_DIR_OUT, 0, ep_recv->received); 297 usb_core_transfer_complete(EP_CONTROL, USB_DIR_OUT, 0, ep_recv->received);
321 ep_transfer_completed(ep_recv); 298 ep_transfer_completed(ep_recv);
299 } else {
300 REG_USB_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY; /* clear OUTPKTRDY bit */
322 } 301 }
323 else REG_USB_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY; /* clear OUTPKTRDY bit */ 302 } else if (!ep0_data_supplied) {
324 }
325 else if (!ep0_data_supplied)
326 {
327 ep_recv->buf = ep0_rx.buf; 303 ep_recv->buf = ep0_rx.buf;
328 readFIFO(ep_recv, REG_USB_COUNT0); 304 readFIFO(ep_recv, REG_USB_COUNT0);
329 csr0 |= USB_CSR0_SVDOUTPKTRDY; 305 csr0 |= USB_CSR0_SVDOUTPKTRDY;
330 if (!ep0_rx.request.wLength) 306 if (!ep0_rx.request.wLength) {
331 {
332 csr0 |= USB_CSR0_DATAEND; /* Set data end! */ 307 csr0 |= USB_CSR0_DATAEND; /* Set data end! */
333 ep0_data_requested = false; 308 ep0_data_requested = false;
334 ep0_data_supplied = false; 309 ep0_data_supplied = false;
335 } 310 } else if (ep0_rx.request.bRequestType & USB_DIR_IN) {
336 else if (ep0_rx.request.bRequestType & USB_DIR_IN)
337 ep0_data_requested = true; 311 ep0_data_requested = true;
338 else ep0_data_supplied = true; 312 } else {
313 ep0_data_supplied = true;
314 }
339 REG_USB_CSR0 = csr0; 315 REG_USB_CSR0 = csr0;
340 usb_core_legacy_control_request(&ep0_rx.request); 316 usb_core_legacy_control_request(&ep0_rx.request);
341 ep_transfer_completed(ep_recv); 317 ep_transfer_completed(ep_recv);
342 } 318 }
343 } 319 }
344 else if (ep_send->busy) 320 else if (ep_send->busy) {
345 EP0_send(); 321 EP0_send();
322 }
346} 323}
347 324
348/* Does new work */ 325/* Does new work */
@@ -429,7 +406,7 @@ static void EPIN_send(unsigned int endpoint)
429#endif 406#endif
430 407
431 /* Non-DMA code */ 408 /* Non-DMA code */
432 if(ep->sent == 0) 409 if (ep->sent == 0)
433 length = MIN(ep->length, ep->fifo_size); 410 length = MIN(ep->length, ep->fifo_size);
434 else 411 else
435 length = MIN(EP_BUF_LEFT(ep), ep->fifo_size); 412 length = MIN(EP_BUF_LEFT(ep), ep->fifo_size);
@@ -494,7 +471,7 @@ static void EPIN_complete(unsigned int endpoint)
494 471
495 logf("EP%d: %d -> %d", endpoint, ep->sent, ep->length); 472 logf("EP%d: %d -> %d", endpoint, ep->sent, ep->length);
496 473
497 if(ep->sent >= ep->length) { 474 if (ep->sent >= ep->length) {
498 if (!ep->wait) 475 if (!ep->wait)
499 usb_core_transfer_complete(endpoint, USB_DIR_IN, 0, ep->sent); 476 usb_core_transfer_complete(endpoint, USB_DIR_IN, 0, ep->sent);
500 ep->rc = 0; 477 ep->rc = 0;
@@ -516,9 +493,9 @@ static void EPOUT_handler(unsigned int endpoint)
516 } 493 }
517 494
518 select_endpoint(endpoint); 495 select_endpoint(endpoint);
519 while((csr = REG_USB_OUTCSR) & (USB_OUTCSR_SENTSTALL|USB_OUTCSR_OUTPKTRDY)) { 496 while ((csr = REG_USB_OUTCSR) & (USB_OUTCSR_SENTSTALL|USB_OUTCSR_OUTPKTRDY)) {
520 logf("%s(%d): 0x%x", __func__, endpoint, csr); 497 logf("%s(%d): 0x%x", __func__, endpoint, csr);
521 if(csr & USB_OUTCSR_SENTSTALL) { 498 if (csr & USB_OUTCSR_SENTSTALL) {
522 logf("stall sent, flushing fifo.."); 499 logf("stall sent, flushing fifo..");
523 flushFIFO(ep); 500 flushFIFO(ep);
524 REG_USB_OUTCSR = csr & ~USB_OUTCSR_SENTSTALL; 501 REG_USB_OUTCSR = csr & ~USB_OUTCSR_SENTSTALL;
@@ -593,7 +570,7 @@ static void EPOUT_handler(unsigned int endpoint)
593 570
594 logf("received: %d max length: %d", ep->received, ep->length); 571 logf("received: %d max length: %d", ep->received, ep->length);
595 572
596 if(size < ep->fifo_size || ep->received >= ep->length) { 573 if (size < ep->fifo_size || ep->received >= ep->length) {
597 usb_core_transfer_complete(endpoint, USB_DIR_OUT, 0, ep->received); 574 usb_core_transfer_complete(endpoint, USB_DIR_OUT, 0, ep->received);
598 ep_transfer_completed(ep); 575 ep_transfer_completed(ep);
599 logf("receive transfer_complete"); 576 logf("receive transfer_complete");
@@ -613,8 +590,7 @@ static void EPOUT_ready(unsigned int endpoint)
613 select_endpoint(endpoint); 590 select_endpoint(endpoint);
614 csr = REG_USB_OUTCSR; 591 csr = REG_USB_OUTCSR;
615 592
616 if(!ep->busy) 593 if (!ep->busy) {
617 {
618 logf("Entered EPOUT handler without work!"); 594 logf("Entered EPOUT handler without work!");
619 return; 595 return;
620 } 596 }
@@ -717,15 +693,15 @@ static void setup_endpoint(struct usb_endpoint *ep)
717 693
718 select_endpoint(endpoint); 694 select_endpoint(endpoint);
719 695
720 if (ep->busy) 696 if (ep->busy) {
721 { 697 if (EP_IS_IN(ep)) {
722 if(EP_IS_IN(ep))
723 {
724 if (ep->wait) 698 if (ep->wait)
725 semaphore_release(&ep->complete); 699 semaphore_release(&ep->complete);
726 else usb_core_transfer_complete(endpoint, USB_DIR_IN, -1, 0); 700 else
701 usb_core_transfer_complete(endpoint, USB_DIR_IN, -1, 0);
702 } else {
703 usb_core_transfer_complete(endpoint, USB_DIR_OUT, -1, 0);
727 } 704 }
728 else usb_core_transfer_complete(endpoint, USB_DIR_OUT, -1, 0);
729 } 705 }
730 706
731 ep->busy = false; 707 ep->busy = false;
@@ -733,17 +709,16 @@ static void setup_endpoint(struct usb_endpoint *ep)
733 ep->sent = 0; 709 ep->sent = 0;
734 ep->length = 0; 710 ep->length = 0;
735 711
736 if(ep->type != ep_control) 712 if (ep->type != ep_control)
737 ep->fifo_size = usb_drv_port_speed() ? 512 : 64; 713 ep->fifo_size = usb_drv_port_speed() ? 512 : 64;
738 714
739 ep->config = REG_USB_CONFIGDATA; 715 ep->config = REG_USB_CONFIGDATA;
740 716
741 if(EP_IS_IN(ep)) 717 if(EP_IS_IN(ep)) {
742 {
743 csr = (USB_INCSR_FF | USB_INCSR_CDT); 718 csr = (USB_INCSR_FF | USB_INCSR_CDT);
744 csrh = USB_INCSRH_MODE; 719 csrh = USB_INCSRH_MODE;
745 720
746 if(ep->type == ep_interrupt) 721 if (ep->type == ep_interrupt)
747 csrh |= USB_INCSRH_FRCDATATOG; 722 csrh |= USB_INCSRH_FRCDATATOG;
748 723
749 REG_USB_INMAXP = ep->fifo_size; 724 REG_USB_INMAXP = ep->fifo_size;
@@ -754,9 +729,7 @@ static void setup_endpoint(struct usb_endpoint *ep)
754 729
755 if (ep->allocated) 730 if (ep->allocated)
756 REG_USB_INTRINE |= USB_INTR_EP(EP_NUMBER2(ep)); 731 REG_USB_INTRINE |= USB_INTR_EP(EP_NUMBER2(ep));
757 } 732 } else {
758 else
759 {
760 csr = (USB_OUTCSR_FF | USB_OUTCSR_CDT); 733 csr = (USB_OUTCSR_FF | USB_OUTCSR_CDT);
761 csrh = 0; 734 csrh = 0;
762 735
@@ -813,8 +786,7 @@ static void udc_reset(void)
813 endpoints[0].config = REG_USB_CONFIGDATA; 786 endpoints[0].config = REG_USB_CONFIGDATA;
814 endpoints[1].config = REG_USB_CONFIGDATA; 787 endpoints[1].config = REG_USB_CONFIGDATA;
815 788
816 if (endpoints[0].busy) 789 if (endpoints[0].busy) {
817 {
818 if (endpoints[0].wait) 790 if (endpoints[0].wait)
819 semaphore_release(&endpoints[0].complete); 791 semaphore_release(&endpoints[0].complete);
820 else 792 else
@@ -837,7 +809,7 @@ static void udc_reset(void)
837 endpoints[1].allocated = true; 809 endpoints[1].allocated = true;
838 810
839 /* Reset other endpoints */ 811 /* Reset other endpoints */
840 for(i=2; i<TOTAL_EP(); i++) 812 for (i=2; i<TOTAL_EP(); i++)
841 setup_endpoint(&endpoints[i]); 813 setup_endpoint(&endpoints[i]);
842 814
843 ep0_data_supplied = false; 815 ep0_data_supplied = false;
@@ -864,26 +836,26 @@ void OTG(void)
864 logf("IRQ %x %x %x %x", intrUSB, intrIn, intrOut, intrDMA); 836 logf("IRQ %x %x %x %x", intrUSB, intrIn, intrOut, intrDMA);
865 837
866 /* EPIN & EPOUT are all handled in DMA */ 838 /* EPIN & EPOUT are all handled in DMA */
867 if(intrIn & USB_INTR_EP(0)) 839 if (intrIn & USB_INTR_EP(0))
868 EP0_handler(); 840 EP0_handler();
869 if(intrIn & USB_INTR_EP(1)) 841 if (intrIn & USB_INTR_EP(1))
870 EPIN_complete(1); 842 EPIN_complete(1);
871 if(intrIn & USB_INTR_EP(2)) 843 if (intrIn & USB_INTR_EP(2))
872 EPIN_complete(2); 844 EPIN_complete(2);
873 if(intrOut & USB_INTR_EP(1)) 845 if (intrOut & USB_INTR_EP(1))
874 EPOUT_ready(1); 846 EPOUT_ready(1);
875 if(intrOut & USB_INTR_EP(2)) 847 if (intrOut & USB_INTR_EP(2))
876 EPOUT_ready(2); 848 EPOUT_ready(2);
877 if(intrUSB & USB_INTR_RESET) 849 if (intrUSB & USB_INTR_RESET)
878 udc_reset(); 850 udc_reset();
879 if(intrUSB & USB_INTR_SUSPEND) 851 if (intrUSB & USB_INTR_SUSPEND)
880 logf("USB suspend"); 852 logf("USB suspend");
881 if(intrUSB & USB_INTR_RESUME) 853 if (intrUSB & USB_INTR_RESUME)
882 logf("USB resume"); 854 logf("USB resume");
883#ifdef USE_USB_DMA 855#ifdef USE_USB_DMA
884 if(intrDMA & (1<<USB_INTR_DMA_BULKIN)) 856 if (intrDMA & (1<<USB_INTR_DMA_BULKIN))
885 EPDMA_handler(USB_INTR_DMA_BULKIN); 857 EPDMA_handler(USB_INTR_DMA_BULKIN);
886 if(intrDMA & (1<<USB_INTR_DMA_BULKOUT)) 858 if (intrDMA & (1<<USB_INTR_DMA_BULKOUT))
887 EPDMA_handler(USB_INTR_DMA_BULKOUT); 859 EPDMA_handler(USB_INTR_DMA_BULKOUT);
888#endif 860#endif
889} 861}
@@ -896,11 +868,10 @@ bool usb_drv_stalled(int endpoint, bool in)
896 868
897 select_endpoint(endpoint); 869 select_endpoint(endpoint);
898 870
899 if(endpoint == EP_CONTROL) 871 if (endpoint == EP_CONTROL) {
900 return (REG_USB_CSR0 & USB_CSR0_SENDSTALL) != 0; 872 return (REG_USB_CSR0 & USB_CSR0_SENDSTALL) != 0;
901 else 873 } else {
902 { 874 if (in)
903 if(in)
904 return (REG_USB_INCSR & USB_INCSR_SENDSTALL) != 0; 875 return (REG_USB_INCSR & USB_INCSR_SENDSTALL) != 0;
905 else 876 else
906 return (REG_USB_OUTCSR & USB_OUTCSR_SENDSTALL) != 0; 877 return (REG_USB_OUTCSR & USB_OUTCSR_SENDSTALL) != 0;
@@ -915,24 +886,18 @@ void usb_drv_stall(int endpoint, bool stall, bool in)
915 886
916 select_endpoint(endpoint); 887 select_endpoint(endpoint);
917 888
918 if(endpoint == EP_CONTROL) 889 if(endpoint == EP_CONTROL) {
919 {
920 if(stall) 890 if(stall)
921 REG_USB_CSR0 |= USB_CSR0_SENDSTALL; 891 REG_USB_CSR0 |= USB_CSR0_SENDSTALL;
922 else 892 else
923 REG_USB_CSR0 &= ~USB_CSR0_SENDSTALL; 893 REG_USB_CSR0 &= ~USB_CSR0_SENDSTALL;
924 } 894 } else {
925 else 895 if(in) {
926 {
927 if(in)
928 {
929 if(stall) 896 if(stall)
930 REG_USB_INCSR |= USB_INCSR_SENDSTALL; 897 REG_USB_INCSR |= USB_INCSR_SENDSTALL;
931 else 898 else
932 REG_USB_INCSR = (REG_USB_INCSR & ~USB_INCSR_SENDSTALL) | USB_INCSR_CDT; 899 REG_USB_INCSR = (REG_USB_INCSR & ~USB_INCSR_SENDSTALL) | USB_INCSR_CDT;
933 } 900 } else {
934 else
935 {
936 if(stall) 901 if(stall)
937 REG_USB_OUTCSR |= USB_OUTCSR_SENDSTALL; 902 REG_USB_OUTCSR |= USB_OUTCSR_SENDSTALL;
938 else 903 else
@@ -965,7 +930,7 @@ void usb_init_device(void)
965 930
966 system_enable_irq(IRQ_OTG); 931 system_enable_irq(IRQ_OTG);
967 932
968 for(unsigned i=0; i<TOTAL_EP(); i++) 933 for (unsigned i=0; i<TOTAL_EP(); i++)
969 semaphore_init(&endpoints[i].complete, 1, 0); 934 semaphore_init(&endpoints[i].complete, 1, 0);
970} 935}
971 936
@@ -979,7 +944,7 @@ static int usb_oneshot_callback(struct timeout *tmo)
979 * and post appropriate event. */ 944 * and post appropriate event. */
980 usb_status_event(state); 945 usb_status_event(state);
981 946
982 if(state == USB_EXTRACTED) 947 if (state == USB_EXTRACTED)
983 __gpio_as_irq_rise_edge(PIN_USB_DET); 948 __gpio_as_irq_rise_edge(PIN_USB_DET);
984 else 949 else
985 __gpio_as_irq_fall_edge(PIN_USB_DET); 950 __gpio_as_irq_fall_edge(PIN_USB_DET);
@@ -996,7 +961,7 @@ void GPIO_USB_DET(void)
996 961
997void usb_enable(bool on) 962void usb_enable(bool on)
998{ 963{
999 if(on) 964 if (on)
1000 usb_core_init(); 965 usb_core_init();
1001 else 966 else
1002 usb_core_exit(); 967 usb_core_exit();
@@ -1070,7 +1035,7 @@ static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length
1070 ep->sent = 0; 1035 ep->sent = 0;
1071 ep->length = length; 1036 ep->length = length;
1072 ep->busy = true; 1037 ep->busy = true;
1073 if(blocking) { 1038 if (blocking) {
1074 ep->rc = -1; 1039 ep->rc = -1;
1075 ep->wait = true; 1040 ep->wait = true;
1076 } else { 1041 } else {
@@ -1085,7 +1050,7 @@ static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length
1085 1050
1086 restore_irq(flags); 1051 restore_irq(flags);
1087 1052
1088 if(blocking) { 1053 if (blocking) {
1089 semaphore_wait(&ep->complete, HZ); 1054 semaphore_wait(&ep->complete, HZ);
1090 ep->wait = false; 1055 ep->wait = false;
1091 } 1056 }
@@ -1097,8 +1062,7 @@ int usb_drv_send_nonblocking(int endpoint, void* ptr, int length)
1097 1062
1098 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length); 1063 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
1099 1064
1100 if (ep->allocated) 1065 if (ep->allocated) {
1101 {
1102 usb_drv_send_internal(ep, ptr, length, false); 1066 usb_drv_send_internal(ep, ptr, length, false);
1103 return 0; 1067 return 0;
1104 } 1068 }
@@ -1112,8 +1076,7 @@ int usb_drv_send(int endpoint, void* ptr, int length)
1112 1076
1113 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length); 1077 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
1114 1078
1115 if (ep->allocated) 1079 if (ep->allocated) {
1116 {
1117 usb_drv_send_internal(ep, ptr, length, true); 1080 usb_drv_send_internal(ep, ptr, length, true);
1118 return ep->rc; 1081 return ep->rc;
1119 } 1082 }
@@ -1188,7 +1151,8 @@ void usb_drv_cancel_all_transfers(void)
1188{ 1151{
1189 logf("%s()", __func__); 1152 logf("%s()", __func__);
1190 1153
1191 unsigned int i, flags = disable_irq_save(); 1154 unsigned int i;
1155 unsigned int flags = disable_irq_save();
1192 1156
1193#ifdef USE_USB_DMA 1157#ifdef USE_USB_DMA
1194 /* Disable DMA */ 1158 /* Disable DMA */
@@ -1204,7 +1168,8 @@ void usb_drv_cancel_all_transfers(void)
1204 usb_core_transfer_complete(i >> 1, USB_DIR_OUT, -1, 0); 1168 usb_core_transfer_complete(i >> 1, USB_DIR_OUT, -1, 0);
1205 else if (endpoints[i].wait) 1169 else if (endpoints[i].wait)
1206 semaphore_release(&endpoints[i].complete); 1170 semaphore_release(&endpoints[i].complete);
1207 else usb_core_transfer_complete(i >> 1, USB_DIR_IN, -1, 0); 1171 else
1172 usb_core_transfer_complete(i >> 1, USB_DIR_IN, -1, 0);
1208 } 1173 }
1209 1174
1210 if(i != 1) /* ep0 out needs special handling */ 1175 if(i != 1) /* ep0 out needs special handling */