1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 by Karl Kurbjun
11 * Portions Copyright (C) 2007 by Catalin Patulea
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 /*#define LOGF_ENABLE*/
39 /*******************************************************************************
40 * These are the driver specific defines.
41 ******************************************************************************/
43 /* This define is primarily intended for testing, using HISPEED all the time
44 * should be acceptable since the device should down-train if the host does not
49 /* Right now sending blocks till the full transfer has completed. The driver
50 * will work without USB_TRAN_BLOCK set, but it is more than 50% slower.
51 * The driver is more "Proper" without USB_TRAN_BLOCK defined so if you start
52 * having freezeups or trouble using USB undefine this option.
54 #define USB_TRAN_BLOCK
56 /*******************************************************************************
57 * The following functions are all helpers which should not be called directly
58 * from the USB stack. They should only be called by eachother, or the USB
59 * stack visible functions.
60 ******************************************************************************/
62 static volatile unsigned short * pipe_ctrl_addr(int pipe
);
63 static void pipe_handshake(int pipe
, int handshake
);
64 static void pipe_c_select (int pipe
, bool dir
);
65 #if !defined(USB_TRAN_BLOCK)
66 static int pipe_buffer_size (int pipe
);
68 static int pipe_maxpack_size (int pipe
);
69 static void control_received(void);
70 static void transfer_complete(int endpoint
);
71 static int mxx_transmit_receive(int endpoint
);
72 static int mxx_queue(int endpoint
, void * ptr
, int length
, bool send
,
75 struct M66591_epstat
{
76 unsigned char dir
; /* endpoint direction */
77 char *buf
; /* user buffer to store data */
78 int length
; /* how match data will fit */
79 volatile int count
; /* actual data count */
80 bool waiting
; /* is there data to transfer? */
81 bool busy
; /* has the pipe been requested for use? */
84 static struct M66591_epstat M66591_eps
[USB_NUM_ENDPOINTS
];
86 /* This function is used to return the control address for each pipe */
87 static volatile unsigned short * pipe_ctrl_addr(int pipe
) {
89 return &M66591_DCPCTRL
;
91 return &M66591_PIPECTRL1
+ (pipe
-1);
95 static void pipe_init(int pipe
) {
96 volatile unsigned short *pipe_cfg
;
97 pipe_cfg
= pipe_ctrl_addr(pipe
);
99 *pipe_cfg
|= 1<<9; /* ACLR */
100 *pipe_cfg
&= ~(1<<9); /* Force de-assertion */
101 *pipe_cfg
|= 1<<8; /* SQCLR */
104 /* This function sets the pipe/endpoint handshake */
105 static void pipe_handshake(int pipe
, int handshake
) {
108 if(handshake
== PIPE_SHAKE_STALL
) {
109 if( *(pipe_ctrl_addr(pipe
)) & 0x03 ) {
110 *(pipe_ctrl_addr(pipe
)) = 0x03;
112 *(pipe_ctrl_addr(pipe
)) = 0x02;
115 *(pipe_ctrl_addr(pipe
)) = handshake
;
119 /* This function chooses the pipe desired and waits the required time before
120 * warites/reads are valid */
121 static void pipe_c_select (int pipe
, bool dir
) {
122 M66591_CPORT_CTRL0
= pipe
| (1<<10) | (dir
<<5);
124 // Wait for the Pipe to be valid;
128 #if !defined(USB_TRAN_BLOCK)
129 /* This returns the maximum buffer size of each pipe. On this device the size
132 static int pipe_buffer_size (int pipe
) {
138 if(M66591_PIPE_CFGWND
& (1<<9) )
154 /* This function returns the maximum packet size for each endpoint/pipe. The
155 * max packet size is dependent on whether the device is running High or Full
158 static int pipe_maxpack_size (int pipe
) {
159 if( (M66591_HSFS
& 0xFF) == 0x03 ) { /* Device is running Highspeed */
162 /* DCP max packet size is configurable */
163 return M66591_DCP_MXPKSZ
;
175 } else { /* Device is running Full speed */
178 /* DCP max packet size is configurable */
179 return M66591_DCP_MXPKSZ
;
194 /* This is a helper function that is only called from the interupt handler. It
195 * copies the control packet information from the PHY and notifies the stack.
197 static void control_received(void) {
198 /* copy setup data from packet */
199 static struct usb_ctrlrequest temp
;
201 memcpy(&temp
, (unsigned char*)&M66591_USB_REQ0
, 8);
203 logf("mxx: bReqType=0x%02x bReq=0x%02x wVal=0x%04x"
204 " wIdx=0x%04x wLen=0x%04x",
205 temp
.bRequestType
, temp
.bRequest
, temp
.wValue
,
206 temp
.wIndex
, temp
.wLength
);
208 /* acknowledge packet recieved (clear valid) */
209 M66591_INTSTAT_MAIN
&= ~(1<<3);
211 usb_core_control_request(&temp
);
214 /* This is a helper function, it is used to notife the stack that a transfer is
217 static void transfer_complete(int endpoint
) {
218 M66591_INTCFG_EMP
&= ~(1 << endpoint
);
219 logf("mxx: ep %d transfer complete", endpoint
);
220 int temp
=M66591_eps
[endpoint
].dir
? USB_DIR_IN
: USB_DIR_OUT
;
221 usb_core_transfer_complete(endpoint
, temp
, 0,
222 M66591_eps
[endpoint
].count
);
225 /* This is the main transmit routine that is typically called from the interrupt
226 * handler (the queue function calls it in some situations)
228 static int mxx_transmit_receive(int endpoint
) {
229 logf("mxx: do start");
231 /* Only the lower 15 bits of the endpoint correlate to the pipe number.
232 * For example pipe 2 will corelate to endpoint 0x82, so the upper bits
233 * need to be masked out.
237 int i
; /* Used as a loop counter */
238 int length
; /* Used in transfers to determine the amount to send/receive */
240 bool send
=M66591_eps
[endpoint
].dir
;
242 /* This is used as the internal buffer pointer */
243 unsigned short *ptrs
;
245 /* Choose the pipe that data is being transfered on */
246 pipe_c_select(endpoint
, send
);
248 /* Check to see if the endpoint is ready and give it some time to become
249 * ready. If it runs out of time exit out as an error.
252 while (!(M66591_CPORT_CTRL1
&(1<<13))) {
254 logf("mxx: FIFO %d not ready", endpoint
);
261 int maxpack
=pipe_maxpack_size(endpoint
);
262 #if defined(USB_TRAN_BLOCK)
263 length
= M66591_eps
[endpoint
].length
;
265 int bufsize
=pipe_buffer_size(endpoint
);
266 length
=MIN(M66591_eps
[endpoint
].length
- M66591_eps
[endpoint
].count
,
270 /* Calculate the position in the buffer, all transfers should be 2-byte
271 * aligned till the last packet or short packet.
273 ptrs
= (unsigned short *)(M66591_eps
[endpoint
].buf
274 + M66591_eps
[endpoint
].count
);
276 /* Check if the buffer is alligned */
277 if( LIKELY(((int)ptrs
) & 0x01) == 0 )
279 /* Start sending data in 16-bit words (fast) */
280 for (i
= 0; i
< (length
>>1); i
++) {
281 #if defined(USB_TRAN_BLOCK)
282 /* This wait is dangerous in the event that something happens
283 * to the PHY pipe where it never becomes ready again, should
284 * probably add a timeout, and ideally completely remove.
286 while(!(M66591_CPORT_CTRL1
&(1<<13))){};
289 M66591_CPORT
= *ptrs
++;
290 M66591_eps
[endpoint
].count
+=2;
293 /* If the length is odd, send the last byte after setting the byte
297 /* Unset MBW (8-bit transfer) */
298 M66591_CPORT_CTRL0
&= ~(1<<10);
299 M66591_CPORT
= *((unsigned char *)ptrs
);
300 M66591_eps
[endpoint
].count
++;
305 /* The buffer is mis-aligned - data needs to be organized first.
306 * This is slower than the above method.
309 unsigned char *ptrc
= (unsigned char*)ptrs
;
311 /* Start sending data in 16-bit words */
312 for (i
= 0; i
< (length
>>1); i
++) {
313 #if defined(USB_TRAN_BLOCK)
314 /* This wait is dangerous in the event that something happens
315 * to the PHY pipe where it never becomes ready again, should
316 * probably add a timeout, and ideally completely remove.
318 while(!(M66591_CPORT_CTRL1
&(1<<13))){};
321 /* These are mis-aligned accesses so the data nees to be
324 sbuf
= (*(ptrc
+1) << 8) | *ptrc
;
328 M66591_eps
[endpoint
].count
+=2;
331 /* If the length is odd, send the last byte after setting the byte
335 /* Unset MBW (8-bit transfer) */
336 M66591_CPORT_CTRL0
&= ~(1<<10);
337 M66591_CPORT
= *ptrc
;
338 M66591_eps
[endpoint
].count
++;
342 /* If the transfer is complete set up interrupts to notify when FIFO is
343 * EMPTY, disable READY and let the handler know that there is nothing
344 * left to transfer on this pipe.
346 if(M66591_eps
[endpoint
].count
== M66591_eps
[endpoint
].length
) {
347 /* Enable Empty flag */
348 M66591_INTCFG_EMP
|= 1 << endpoint
;
349 /* Disable ready flag */
350 M66591_INTCFG_RDY
&= ~(1 << endpoint
);
351 /* Nothing left to transfer */
352 M66591_eps
[endpoint
].waiting
=false;
354 /* There is still data to transfer, make sure READY is enabled */
355 M66591_INTCFG_RDY
|= 1 << endpoint
;
358 /* Set BVAL if length is not a multiple of the maximum packet size */
359 if( (length
== 0) || (length
% maxpack
!= 0) ) {
360 logf("mxx: do set BVAL");
361 M66591_CPORT_CTRL1
|= (1<<15) | ((length
== 0) << 14);
364 /* Read data from FIFO */
366 /* Read the number of bytes that the PHY received */
367 int receive_length
=M66591_CPORT_CTRL1
& 0x03FF;
369 /* The number of bytes to actually read is either what's left of the
370 * amount requested, or the amount that the PHY received. Choose the
371 * smaller of the two.
373 length
= MIN(M66591_eps
[endpoint
].length
- M66591_eps
[endpoint
].count
,
376 /* If the length is zero, just clear the buffer as specified in the
377 * datasheet. Otherwise read in the data (in 16-bit pieces */
379 /* Set the BCLR bit */
380 M66591_CPORT_CTRL1
|= 1<<14;
382 /* Set the position in the buffer */
383 ptrs
= (unsigned short *)(M66591_eps
[endpoint
].buf
384 + M66591_eps
[endpoint
].count
);
386 /* Read in the data (buffer size should be even). The PHY cannot
387 * switch from 16-bit mode to 8-bit mode on an OUT buffer.
389 for (i
= 0; i
< ((length
+1)>>1); i
++) {
390 *ptrs
++ = M66591_CPORT
;
391 M66591_eps
[endpoint
].count
+=2;
395 /* If the length was odd subtract 1 from the count */
396 M66591_eps
[endpoint
].count
-= (length
&0x01);
398 /* If the requested size of data was received, or the data received was
399 * less than the maximum packet size end the transfer.
401 if( (M66591_eps
[endpoint
].count
== M66591_eps
[endpoint
].length
)
402 || (length
% pipe_maxpack_size(endpoint
)) ) {
404 /* If the host tries to send anything else the FIFO is not ready/
407 pipe_handshake(endpoint
, PIPE_SHAKE_NAK
);
408 /* Tell the interrupt handler that transfer is complete. */
409 M66591_eps
[endpoint
].waiting
=false;
411 M66591_INTCFG_RDY
&= ~(1 << endpoint
);
413 /* Let the stack know that the transfer is complete */
415 transfer_complete(endpoint
);
419 logf("mxx: do done ep %d %s len: %d cnt: %d", endpoint
,
420 send
? "out" : "in", length
, M66591_eps
[endpoint
].count
);
425 /* This function is used to start transfers. It is a helper function for the
426 * usb_drv_send_nonblocking, usb_drv_send, and usb_drv_receive functions.
428 * The functionality for wait needs to be added. Currently the driver is
429 * always used in a blocking mode(USB_TRAN_BLOCK) so it is not required.
431 static int mxx_queue(int endpoint
, void * ptr
, int length
, bool send
,
434 #if defined(USB_TRAN_BLOCK) && !defined(LOGF_ENABLE)
439 int flags
= disable_irq_save();
441 /* Only the lower 15 bits of the endpoint correlate to the pipe number.
442 * For example pipe 2 will corelate to endpoint 0x82, so the upper bits
443 * need to be masked out.
447 /* Initialize the enpoint status registers used for the transfer */
448 M66591_eps
[endpoint
].buf
=ptr
;
449 M66591_eps
[endpoint
].length
=length
;
450 M66591_eps
[endpoint
].count
=0;
451 M66591_eps
[endpoint
].dir
=send
;
452 M66591_eps
[endpoint
].waiting
=true;
454 logf("mxx: queue ep %d %s, len: %d, wait: %d",
455 endpoint
, send
? "out" : "in", length
, wait
);
457 /* Pick the pipe that communications are happening on */
458 pipe_c_select(endpoint
, send
);
460 /* All transfers start with a BUF handshake */
461 pipe_handshake(endpoint
, PIPE_SHAKE_BUF
);
463 /* This USB PHY takes care of control completion packets by setting the
464 * CCPL bit in EP0 (endpoint 0, or DCP). If the control state is "write no
465 * data tranfer" then we just need to set the CCPL bit (hopefully)
466 * regardless of what the stack said to send.
468 int control_state
= (M66591_INTSTAT_MAIN
& 0x07);
469 if(endpoint
==0 && control_state
==CTRL_WTND
) {
470 logf("mxx: queue ep 0 ctls: 5, set ccpl");
473 M66591_DCPCTRL
|= 1<<2;
475 /* This is the standard case for transmitting data */
477 /* If the pipe is not ready don't try and send right away; instead
478 * just set the READY interrupt so that the handler can initiate
481 if((M66591_CPORT_CTRL1
&(1<<13))) {
482 mxx_transmit_receive(endpoint
);
484 M66591_INTCFG_RDY
|= 1 << endpoint
;
488 transfer_complete(endpoint
);
491 /* When receiving data, just enable the ready interrupt, the PHY
492 * will trigger it and then the reads can start.
494 M66591_INTCFG_RDY
|= 1 << endpoint
;
503 /*******************************************************************************
504 * This is the interrupt handler for this driver. It should be called from the
505 * target interrupt handler routine (eg. GPIO3 on M:Robe 500).
506 ******************************************************************************/
507 void USB_DEVICE(void) __attribute__ ((section(".icode")));
508 void USB_DEVICE(void) {
509 int pipe_restore
=M66591_CPORT_CTRL0
;
510 logf("\nmxx: INT BEGIN tick: %d", (int) current_tick
);
512 logf("mxx: sMAIN0: 0x%04x, sRDY: 0x%04x",
513 M66591_INTSTAT_MAIN
, M66591_INTSTAT_RDY
);
514 logf("mxx: sNRDY: 0x%04x, sEMP: 0x%04x",
515 M66591_INTSTAT_NRDY
, M66591_INTSTAT_EMP
);
517 /* VBUS (connected) interrupt */
518 while ( M66591_INTSTAT_MAIN
& (1<<15) ) {
519 M66591_INTSTAT_MAIN
&= ~(1<<15);
521 /* If device is not clocked, interrupt flag must be set manually */
522 if ( !(M66591_TRN_CTRL
& (1<<10)) ) {
523 M66591_INTSTAT_MAIN
|= (1<<15);
527 /* Resume interrupt: This is not used. Extra logic needs to be added similar
528 * to the VBUS interrupt incase the PHY clock is not running.
530 if(M66591_INTSTAT_MAIN
& (1<<14)) {
531 M66591_INTSTAT_MAIN
&= ~(1<<14);
535 /* Device state transition interrupt: Not used, but useful for debugging */
536 if(M66591_INTSTAT_MAIN
& (1<<12)) {
537 M66591_INTSTAT_MAIN
&= ~(1<<12);
538 logf("mxx: DEV state CHANGE=%d",
539 ((M66591_INTSTAT_MAIN
& (0x07<<4)) >> 4) );
542 /* Control transfer stage interrupt */
543 if(M66591_INTSTAT_MAIN
& (1<<11)) {
544 M66591_INTSTAT_MAIN
&= ~(1<<11);
545 int control_state
= (M66591_INTSTAT_MAIN
& 0x07);
547 logf("mxx: CTRT with CTSQ=%d", control_state
);
549 switch ( control_state
) {
551 transfer_complete(0);
556 /* If data is not valid stop */
557 if(!(M66591_INTSTAT_MAIN
& (1<<3)) ) {
558 logf("mxx: CTRT interrupt but VALID is false");
565 pipe_handshake(0, PIPE_SHAKE_BUF
);
566 M66591_DCPCTRL
|= 1<<2; /* Set CCPL */
569 logf("mxx: CTRT with unknown CTSQ");
574 /* FIFO EMPTY interrupt: when this happens the transfer should be complete.
575 * When the interrupt occurs notify the stack.
577 if(M66591_INTSTAT_MAIN
& (1<<10)) {
579 logf("mxx: INT EMPTY: 0x%04x", M66591_INTSTAT_EMP
);
581 for(i
=0; i
<USB_NUM_ENDPOINTS
; i
++) {
582 if(M66591_INTSTAT_EMP
&(1<<i
)) {
583 /* Clear the empty flag */
584 M66591_INTSTAT_EMP
=~(1<<i
);
585 /* Notify the stack */
586 transfer_complete(i
);
591 /* FIFO NOT READY interrupt: This is not used, but included incase the
592 * interrupt is endabled.
594 if(M66591_INTSTAT_MAIN
& (1<<9)) {
595 logf("mxx: INT NOT READY: 0x%04x", M66591_INTSTAT_NRDY
);
596 M66591_INTSTAT_NRDY
= 0;
599 /* FIFO READY interrupt: This just initiates transfers if they are needed */
600 if(M66591_INTSTAT_MAIN
& (1<<8)) {
602 logf("mxx: INT READY: 0x%04x", M66591_INTSTAT_RDY
);
604 for(i
=0; i
<USB_NUM_ENDPOINTS
; i
++) {
605 /* Was this endpoint ready and waiting */
606 if(M66591_INTSTAT_RDY
&(1<<i
) && M66591_eps
[i
].waiting
) {
607 /* Clear the ready flag */
608 M66591_INTSTAT_RDY
=~(1<<i
);
609 /* It was ready and waiting so start a transfer */
610 mxx_transmit_receive(i
);
615 /* Make sure that the INTStatus register is completely cleared. */
616 M66591_INTSTAT_MAIN
= 0;
618 /* Restore the pipe state before the interrupt occured */
619 M66591_CPORT_CTRL0
=pipe_restore
;
620 logf("mxx: INT END\n");
623 /*******************************************************************************
624 * The following functions are all called by and visible to the USB stack.
625 ******************************************************************************/
627 /* The M55691 handles this automatically, nothing to do */
628 void usb_drv_set_address(int address
) {
632 /* This function sets the standard test modes, it is not required, but might as
633 * well implement it since the hardware supports it
635 void usb_drv_set_test_mode(int mode
) {
636 /* This sets the test bits and assumes that mode is from 0 to 0x04 */
637 M66591_TESTMODE
&= 0x0007;
638 M66591_TESTMODE
|= mode
;
641 /* Request an unused endpoint */
642 int usb_drv_request_endpoint(int type
, int dir
) {
646 if (type
== USB_ENDPOINT_XFER_BULK
) {
647 /* Enable double buffer mode (only used for ep 1 and 2) */
648 pipecfg
|= 1<<9 | 1<<8;
650 /* Bulk endpoints must be between 1 and 4 inclusive */
653 while(M66591_eps
[ep
].busy
&& ep
++<5);
655 /* If this reached 5 the endpoints were all busy */
657 logf("mxx: ep %d busy", ep
);
660 } else if (type
== USB_ENDPOINT_XFER_INT
) {
665 while(M66591_eps
[ep
].busy
&& ep
++<7);
667 /* If this reached 7 the endpoints were all busy */
669 logf("mxx: ep %d busy", ep
);
673 /* Not a supported type */
677 if (dir
== USB_DIR_IN
) {
681 M66591_eps
[ep
].busy
= true;
682 M66591_eps
[ep
].dir
= dir
;
684 M66591_PIPE_CFGSEL
=ep
;
686 /* Enable pipe (15) */
689 pipe_handshake(ep
, PIPE_SHAKE_NAK
);
691 /* Setup the flags */
692 M66591_PIPE_CFGWND
=pipecfg
;
696 logf("mxx: ep req ep#: %d config: 0x%04x", ep
, M66591_PIPE_CFGWND
);
701 /* Used by stack to tell the helper functions that the pipe is not in use */
702 void usb_drv_release_endpoint(int ep
) {
706 if (ep
< 1 || ep
> USB_NUM_ENDPOINTS
|| M66591_eps
[ep
].busy
== false)
709 flags
= disable_irq_save();
711 logf("mxx: ep %d release", ep
);
713 M66591_eps
[ep
].busy
= false;
714 M66591_eps
[ep
].dir
= -1;
719 /* Periodically called to check if a cable was plugged into the device */
720 inline int usb_detect(void)
722 if(M66591_INTSTAT_MAIN
&(1<<7))
725 return USB_EXTRACTED
;
728 void usb_enable(bool on
) {
729 logf("mxx: %s: %s", __FUNCTION__
, on
? "true" : "false");
736 /* This is where the driver stuff starts */
737 void usb_drv_init(void) {
738 logf("mxx: Device Init");
740 M66591_PIN_CFG1
= 0x8000; /* Drive Current: 3.3V setting */
741 M66591_PIN_CFG2
= 0x0000;
743 M66591_TRN_CTRL
= 0x8000; /* External 48 MHz clock */
744 M66591_TRN_CTRL
|=0x0001;
746 M66591_INTCFG_MAIN
|=0x8000; /* Enable VBUS interrupt */
749 /* fully enable driver */
750 void usb_attach(void) {
753 /* Reset Endpoint states */
754 for(i
=0; i
<USB_NUM_ENDPOINTS
; i
++) {
755 M66591_eps
[i
].dir
= -1;
756 M66591_eps
[i
].buf
= (char *) 0;
757 M66591_eps
[i
].length
= 0;
758 M66591_eps
[i
].count
= 0;
759 M66591_eps
[i
].waiting
= false;
760 M66591_eps
[i
].busy
= false;
763 /* Issue a h/w reset */
767 /* USB Attach Process: This follows the flow diagram in the M66591GP
768 * Reference Manual Rev 1.00, p. 77 */
772 M66591_TRN_CTRL
|= 1<<7;
775 M66591_TRN_CTRL
&= ~(1<<7);
778 /* Enable oscillation buffer XCKE */
779 M66591_TRN_CTRL
|= (1<<13);
783 /* Enable reference clock, PLL RCKE */
784 M66591_TRN_CTRL
|= (3<<11);
788 /* Enable internal clock supply SCKE */
789 M66591_TRN_CTRL
|= (1<<10);
791 /* Disable PIPE ready interrupts */
792 M66591_INTCFG_RDY
= 0;
794 /* Disable PIPE not-ready interrupts */
795 M66591_INTCFG_NRDY
= 0;
797 /* Disable PIPE empyt/size error interrupts */
798 M66591_INTCFG_EMP
= 0;
800 /* Enable all interrupts except NOT READY, RESUME, and VBUS */
801 M66591_INTCFG_MAIN
= 0x1DFF;
803 pipe_c_select(0, false);
805 /* Enable continuous transfer mode on the DCP */
806 M66591_DCP_CNTMD
|= (1<<8);
808 /* Set the threshold that the PHY will automatically transmit from EP0 */
809 M66591_DCP_CTRLEN
= 256;
811 pipe_handshake(0, PIPE_SHAKE_NAK
);
813 /* Set the Max packet size to 64 */
814 M66591_DCP_MXPKSZ
= 64;
816 /* Attach notification to PC (D+ pull-up) */
817 M66591_TRN_CTRL
|= (1<<4);
819 logf("mxx: attached");
822 void usb_drv_exit(void) {
823 /* USB Detach Process: This follows the flow diagram in the M66591GP
824 * Reference Manual Rev 1.00, p. 78.
827 /* Detach notification to PC (disable D+ pull-up) */
828 M66591_TRN_CTRL
&= ~(1<<4);
831 M66591_TRN_CTRL
&= ~0x01;
833 /* Disable internal clock supply */
834 M66591_TRN_CTRL
&= ~(1<<10);
838 M66591_TRN_CTRL
&= ~(1<<11);
841 /* Disable internal reference clock */
842 M66591_TRN_CTRL
&= ~(1<<12);
845 /* Disable oscillation buffer, reenable USB operation */
846 M66591_TRN_CTRL
&= ~(1<<13);
848 M66591_TRN_CTRL
|= 0x01;
850 logf("mxx: detached");
853 /* This function begins a transmit (on an IN endpoint), it should not block
854 * so the actual transmit is done in the interrupt handler.
856 int usb_drv_send_nonblocking(int endpoint
, void* ptr
, int length
)
858 return mxx_queue(endpoint
, ptr
, length
, true, false);
861 /* This function begins a transmit (on an IN endpoint), it does not block
862 * so the actual transmit is done in the interrupt handler.
864 int usb_drv_send(int endpoint
, void* ptr
, int length
)
866 return mxx_queue(endpoint
, ptr
, length
, true, true);
869 /* This function begins a receive (on an OUT endpoint), it should not block
870 * so the actual receive is done in the interrupt handler.
872 int usb_drv_recv(int endpoint
, void* ptr
, int length
)
874 return mxx_queue(endpoint
, ptr
, length
, false, false);
877 /* This function checks the reset handshake speed status
878 * (Fullspeed or Highspeed)
880 int usb_drv_port_speed(void)
882 int handshake
= (M66591_HSFS
& 0xFF);
884 if( handshake
== 0x02) {
885 return 0; /* Handshook at Full-Speed */
886 } else if( handshake
== 0x03) {
887 return 1; /* Handshook at Hi-Speed */
889 return -1; /* Error, handshake may not be complete */
893 /* This function checks if the endpoint is stalled (error). I am not sure what
894 * the "in" variable is intended for.
896 bool usb_drv_stalled(int endpoint
,bool in
)
900 bool stalled
= (*(pipe_ctrl_addr(endpoint
)) & (0x02)) ? true : false;
902 logf("mxx: stall?: %s ep: %d", stalled
? "true" : "false", endpoint
);
911 /* This function stalls/unstalls the endpoint. Stalls only happen on error so
912 * if the endpoint is functioning properly this should not be called. I am
913 * not sure what the "in" variable is intended for.
915 void usb_drv_stall(int endpoint
, bool stall
,bool in
)
919 logf("mxx: stall - ep: %d", endpoint
);
922 /* Stall the pipe (host needs to intervene/error) */
923 pipe_handshake(endpoint
, PIPE_SHAKE_STALL
);
925 /* Setting this to a NAK, not sure if it is appropriate */
926 pipe_handshake(endpoint
, PIPE_SHAKE_NAK
);
930 /* !!!!!!!!!!This function is likely incomplete!!!!!!!!!!!!!! */
931 void usb_drv_cancel_all_transfers(void)
936 logf("mxx: %s", __func__
);
938 flags
= disable_irq_save();
939 for (endpoint
= 0; endpoint
< USB_NUM_ENDPOINTS
; endpoint
++) {
940 if (M66591_eps
[endpoint
].buf
) {
941 M66591_eps
[endpoint
].buf
= NULL
;