]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBHost/USBHost/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/host/usb0_host_drv_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBHost / USBHost / TARGET_RENESAS / TARGET_RZ_A1H / usb0 / src / host / usb0_host_drv_api.c
1 /*******************************************************************************
2 * DISCLAIMER
3 * This software is supplied by Renesas Electronics Corporation and is only
4 * intended for use with Renesas products. No other uses are authorized. This
5 * software is owned by Renesas Electronics Corporation and is protected under
6 * all applicable laws, including copyright laws.
7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
16 * Renesas reserves the right, without notice, to make changes to this software
17 * and to discontinue the availability of this software. By using this software,
18 * you agree to the additional terms and conditions found by accessing the
19 * following link:
20 * http://www.renesas.com/disclaimer
21 * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
22 *******************************************************************************/
23 /*******************************************************************************
24 * File Name    : usb0_host_drv_api.c
25 * $Rev: 1116 $
26 * $Date:: 2014-07-09 16:29:19 +0900#$
27 * Device(s)    : RZ/A1H
28 * Tool-Chain   :
29 * OS           : None
30 * H/W Platform :
31 * Description  : RZ/A1H R7S72100 USB Sample Program
32 * Operation    :
33 * Limitations  :
34 *******************************************************************************/
35
36
37 /*******************************************************************************
38 Includes   <System Includes> , "Project Includes"
39 *******************************************************************************/
40 #include "usb0_host.h"
41 #include "dev_drv.h"
42
43
44 /*******************************************************************************
45 Typedef definitions
46 *******************************************************************************/
47
48
49 /*******************************************************************************
50 Macro definitions
51 *******************************************************************************/
52
53
54 /*******************************************************************************
55 Imported global variables and functions (from other files)
56 *******************************************************************************/
57
58
59 /*******************************************************************************
60 Exported global variables and functions (to be accessed by other files)
61 *******************************************************************************/
62 static void usb0_host_resetEP(USB_HOST_CFG_PIPETBL_t *tbl);
63
64
65 /*******************************************************************************
66 Private global variables and functions
67 *******************************************************************************/
68
69
70 /*******************************************************************************
71 * Function Name: usb0_api_host_init
72 * Description  : Initializes USB module in the USB host mode.
73 *              : USB connection is executed when executing this function in
74 *              : the states that USB device isconnected to the USB port.
75 * Arguments    : uint8_t int_level  : USB Module interrupt level
76 *              : USBU16  mode       : USB_HOST_HIGH_SPEED
77 *                                   : USB_HOST_FULL_SPEED
78 *              : uint16_t clockmode : USB Clock mode
79 * Return Value : USB detach or attach
80 *              :  USB_HOST_ATTACH
81 *              :  USB_HOST_DETACH
82 *******************************************************************************/
83 uint16_t usb0_api_host_init (uint8_t int_level, uint16_t mode, uint16_t clockmode)
84 {
85     uint16_t         connect;
86     volatile uint8_t dummy_buf;
87
88     CPG.STBCR7 &= 0xfd;                         /*The clock of USB0 modules is permitted */
89     dummy_buf   = CPG.STBCR7;                   /* (Dummy read) */
90
91     g_usb0_host_SupportUsbDeviceSpeed = mode;
92
93     usb0_host_setting_interrupt(int_level);
94     usb0_host_reset_module(clockmode);
95
96     g_usb0_host_bchg_flag   = USB_HOST_NO;
97     g_usb0_host_detach_flag = USB_HOST_NO;
98     g_usb0_host_attach_flag = USB_HOST_NO;
99
100     g_usb0_host_driver_state = USB_HOST_DRV_DETACHED;
101     g_usb0_host_default_max_packet[USB_HOST_DEVICE_0] = 64;
102
103     usb0_host_InitModule();
104
105     connect = usb0_host_CheckAttach();
106
107     if (connect == USB_HOST_ATTACH)
108     {
109         g_usb0_host_attach_flag = USB_HOST_YES;
110     }
111     else
112     {
113         usb0_host_UsbDetach2();
114     }
115
116     return connect;
117 }
118
119 #if(1) /* ohci_wrapp */
120 #else
121 /*******************************************************************************
122 * Function Name: usb0_api_host_enumeration
123 * Description  : Initializes USB module in the USB host mode.
124 *              : USB connection is executed when executing this function in
125 *              : the states that USB device isconnected to the USB port.
126 * Arguments    : uint16_t devadr : device address
127 * Return Value : DEVDRV_USBH_DETACH_ERR       : device detach
128 *              : DEVDRV_SUCCESS               : device enumeration success
129 *              : DEVDRV_ERROR                 : device enumeration error
130 *******************************************************************************/
131 int32_t usb0_api_host_enumeration (uint16_t devadr)
132 {
133     int32_t  ret;
134     uint16_t driver_sts;
135
136     g_usb0_host_setUsbAddress = devadr;
137
138     while (1)
139     {
140         driver_sts = usb0_api_host_GetUsbDeviceState();
141
142         if (driver_sts == USB_HOST_DRV_DETACHED)
143         {
144             ret = DEVDRV_USBH_DETACH_ERR;
145             break;
146         }
147         else if (driver_sts == USB_HOST_DRV_CONFIGURED)
148         {
149             ret = DEVDRV_SUCCESS;
150             break;
151         }
152         else if (driver_sts == USB_HOST_DRV_STALL)
153         {
154             ret = DEVDRV_ERROR;
155             break;
156         }
157         else if (driver_sts == USB_HOST_DRV_NORES)
158         {
159             ret = DEVDRV_ERROR;
160             break;
161         }
162         else
163         {
164             /* Do Nothing */
165         }
166     }
167
168     if (driver_sts == USB_HOST_DRV_NORES)
169     {
170         while (1)
171         {
172             driver_sts = usb0_api_host_GetUsbDeviceState();
173
174             if (driver_sts == USB_HOST_DRV_DETACHED)
175             {
176                 break;
177             }
178         }
179     }
180
181     return ret;
182 }
183
184 /*******************************************************************************
185 * Function Name: usb0_api_host_detach
186 * Description  : USB detach routine
187 * Arguments    : none
188 * Return Value : USB_HOST_DETACH : USB detach
189 *              : USB_HOST_ATTACH : USB attach
190 *              : DEVDRV_ERROR    : error
191 *******************************************************************************/
192 int32_t usb0_api_host_detach (void)
193 {
194     int32_t  ret;
195     uint16_t driver_sts;
196
197     while (1)
198     {
199         driver_sts = usb0_api_host_GetUsbDeviceState();
200
201         if (driver_sts == USB_HOST_DRV_DETACHED)
202         {
203             ret = USB_HOST_DETACH;
204             break;
205         }
206         else if (driver_sts == USB_HOST_DRV_CONFIGURED)
207         {
208             ret = USB_HOST_ATTACH;
209             break;
210         }
211         else if (driver_sts == USB_HOST_DRV_STALL)
212         {
213             ret = DEVDRV_ERROR;
214             break;
215         }
216         else if (driver_sts == USB_HOST_DRV_NORES)
217         {
218             ret = DEVDRV_ERROR;
219             break;
220         }
221         else
222         {
223             /* Do Nothing */
224         }
225     }
226
227     if (driver_sts == USB_HOST_DRV_NORES)
228     {
229         while (1)
230         {
231             driver_sts = usb0_api_host_GetUsbDeviceState();
232
233             if (driver_sts == USB_HOST_DRV_DETACHED)
234             {
235                 break;
236             }
237         }
238     }
239
240     return ret;
241 }
242
243 /*******************************************************************************
244 * Function Name: usb0_api_host_data_in
245 * Description  : Executes USB transfer as data-in in the argument specified pipe.
246 * Arguments    : uint16_t devadr       ; device address
247 *              : uint16_t Pipe         ; Pipe Number
248 *              : uint32_t Size         ; Data Size
249 *              : uint8_t  *data_buf    ; Data data_buf Address
250 * Return Value : DEVDRV_SUCCESS       ; success
251 *              : DEVDRV_ERROR         ; error
252 *******************************************************************************/
253 int32_t usb0_api_host_data_in (uint16_t devadr, uint16_t Pipe, uint32_t Size, uint8_t * data_buf)
254 {
255     int32_t ret;
256
257     if (Pipe == USB_HOST_PIPE0)
258     {
259         return DEVDRV_ERROR;
260     }
261
262     if (RZA_IO_RegRead_16(&g_usb0_host_pipemaxp[Pipe], USB_PIPEMAXP_DEVSEL_SHIFT, USB_PIPEMAXP_DEVSEL) != devadr)
263     {
264         return DEVDRV_ERROR;
265     }
266
267     if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg[Pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 1)
268     {
269         return DEVDRV_ERROR;
270     }
271
272     if (g_usb0_host_pipe_status[Pipe] == USB_HOST_PIPE_IDLE)
273     {
274         usb0_host_start_receive_transfer(Pipe, Size, data_buf);
275     }
276     else
277     {
278         return DEVDRV_ERROR;              /* Now pipe is busy */
279     }
280
281     /* waiting for completing routine           */
282     do
283     {
284         if (g_usb0_host_detach_flag == USB_HOST_YES)
285         {
286             break;
287         }
288
289         if ((g_usb0_host_pipe_status[Pipe] != USB_HOST_PIPE_IDLE) && (g_usb0_host_pipe_status[Pipe] != USB_HOST_PIPE_WAIT))
290         {
291             break;
292         }
293
294     } while (1);
295
296     if (g_usb0_host_detach_flag == USB_HOST_YES)
297     {
298         return DEVDRV_USBH_DETACH_ERR;
299     }
300
301     switch (g_usb0_host_pipe_status[Pipe])
302     {
303         case USB_HOST_PIPE_DONE:
304             ret = DEVDRV_SUCCESS;
305         break;
306
307         case USB_HOST_PIPE_STALL:
308             ret = DEVDRV_USBH_STALL;
309         break;
310
311         case USB_HOST_PIPE_NORES:
312             ret = DEVDRV_USBH_COM_ERR;
313         break;
314
315         default:
316             ret = DEVDRV_ERROR;
317         break;
318     }
319
320     usb0_host_stop_transfer(Pipe);
321
322     g_usb0_host_pipe_status[Pipe] = USB_HOST_PIPE_IDLE;
323
324     return ret;
325 }
326
327 /*******************************************************************************
328 * Function Name: usb0_api_host_data_out
329 * Description  : Executes USB transfer as data-out in the argument specified pipe.
330 * Arguments    : uint16_t devadr       ; device address
331 *              : uint16_t Pipe         ; Pipe Number
332 *              : uint32_t Size         ; Data Size
333 *              : uint8_t  *data_buf    ; Data data_buf Address
334 * Return Value : DEVDRV_SUCCESS       ; success
335 *              : DEVDRV_ERROR         ; error
336 *******************************************************************************/
337 int32_t usb0_api_host_data_out (uint16_t devadr, uint16_t Pipe, uint32_t Size, uint8_t * data_buf)
338 {
339     int32_t ret;
340
341     if (Pipe == USB_HOST_PIPE0)
342     {
343         return DEVDRV_ERROR;
344     }
345
346     if (RZA_IO_RegRead_16(&g_usb0_host_pipemaxp[Pipe], USB_PIPEMAXP_DEVSEL_SHIFT, USB_PIPEMAXP_DEVSEL) != devadr)
347     {
348         return DEVDRV_ERROR;
349     }
350
351     if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg[Pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0)
352     {
353         return DEVDRV_ERROR;
354     }
355
356     if (g_usb0_host_pipe_status[Pipe] == USB_HOST_PIPE_IDLE)
357     {
358         usb0_host_start_send_transfer(Pipe, Size, data_buf);
359     }
360     else
361     {
362         return DEVDRV_ERROR;              /* Now pipe is busy */
363     }
364
365     /* waiting for completing routine           */
366     do
367     {
368         if (g_usb0_host_detach_flag == USB_HOST_YES)
369         {
370             break;
371         }
372
373         if ((g_usb0_host_pipe_status[Pipe] != USB_HOST_PIPE_IDLE) && (g_usb0_host_pipe_status[Pipe] != USB_HOST_PIPE_WAIT))
374         {
375             break;
376         }
377
378     } while (1);
379
380     if (g_usb0_host_detach_flag == USB_HOST_YES)
381     {
382         return DEVDRV_USBH_DETACH_ERR;
383     }
384
385     switch (g_usb0_host_pipe_status[Pipe])
386     {
387         case USB_HOST_PIPE_DONE:
388             ret = DEVDRV_SUCCESS;
389         break;
390
391         case USB_HOST_PIPE_STALL:
392             ret = DEVDRV_USBH_STALL;
393         break;
394
395         case USB_HOST_PIPE_NORES:
396             ret = DEVDRV_USBH_COM_ERR;
397         break;
398
399         default:
400             ret = DEVDRV_ERROR;
401         break;
402     }
403
404     usb0_host_stop_transfer(Pipe);
405
406     g_usb0_host_pipe_status[Pipe] = USB_HOST_PIPE_IDLE;
407
408     return ret;
409 }
410
411 /*******************************************************************************
412 * Function Name: usb0_api_host_control_transfer
413 * Description  : Executes USB control transfer.
414 * Arguments    : uint16_t devadr       ; device address
415 *              : uint16_t Req          ; bmRequestType & bRequest
416 *              : uint16_t Val          ; wValue
417 *              : uint16_t Indx         ; wIndex
418 *              : uint16_t Len          ; wLength
419 *              : uint8_t  *buf         ; Buffer
420 * Return Value : DEVDRV_SUCCESS           ; success
421 *              : DEVDRV_USBH_DETACH_ERR   ; device detach
422 *              : DEVDRV_USBH_CTRL_COM_ERR ; device no response
423 *              : DEVDRV_USBH_STALL        ; STALL
424 *              : DEVDRV_ERROR             ; error
425 *******************************************************************************/
426 int32_t usb0_api_host_control_transfer (uint16_t devadr, uint16_t Req, uint16_t Val, uint16_t Indx,
427                                                      uint16_t Len, uint8_t * Buf)
428 {
429     int32_t  ret;
430
431     do
432     {
433         ret = usb0_host_CtrlTransStart(devadr, Req, Val, Indx, Len, Buf);
434
435         if (ret == DEVDRV_SUCCESS)
436         {
437             if (g_usb0_host_detach_flag == USB_HOST_YES)
438             {
439                 break;
440             }
441
442             if ((g_usb0_host_pipe_status[USB_HOST_PIPE0] != USB_HOST_PIPE_IDLE)
443                 && (g_usb0_host_pipe_status[USB_HOST_PIPE0] != USB_HOST_PIPE_WAIT))
444             {
445                 break;
446             }
447         }
448         else
449         {
450             return DEVDRV_ERROR;
451         }
452     } while (1);
453
454     if (g_usb0_host_detach_flag == USB_HOST_YES)
455     {
456         return DEVDRV_USBH_DETACH_ERR;
457     }
458
459     switch (g_usb0_host_pipe_status[USB_HOST_PIPE0])
460     {
461         case USB_HOST_PIPE_DONE:
462             ret = DEVDRV_SUCCESS;
463         break;
464
465         case USB_HOST_PIPE_STALL:
466             ret = DEVDRV_USBH_STALL;
467         break;
468
469         case USB_HOST_PIPE_NORES:
470             ret = DEVDRV_USBH_CTRL_COM_ERR;
471         break;
472
473         default:
474             ret = DEVDRV_ERROR;
475         break;
476     }
477
478     g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_IDLE;
479
480     return ret;
481 }
482
483 /*******************************************************************************
484 * Function Name: usb0_api_host_set_endpoint
485 * Description  : Sets end point on the information specified in the argument.
486 * Arguments    : uint16_t                devadr           ; device address
487 *              : uint8_t                *configdescriptor ; device configration descriptor
488 *              : USB_HOST_CFG_PIPETBL_t *user_table       ; pipe table
489 * Return Value : DEVDRV_SUCCESS       ; success
490 *              : DEVDRV_ERROR         ; error
491 *******************************************************************************/
492 int32_t usb0_api_host_set_endpoint (uint16_t devadr, USB_HOST_CFG_PIPETBL_t * user_table, uint8_t * configdescriptor)
493 {
494     uint16_t                ret;
495     uint32_t                end_point;
496     uint32_t                offset;
497     uint32_t                totalLength;
498     USB_HOST_CFG_PIPETBL_t * pipe_table;
499
500     /*  End Point Search */
501     end_point   = 0;
502     offset      = configdescriptor[0];
503     totalLength = (uint16_t)(configdescriptor[2] + ((uint16_t)configdescriptor[3] << 8));
504
505     do
506     {
507         if (configdescriptor[offset + 1] == USB_HOST_ENDPOINT_DESC)
508         {
509             pipe_table = &user_table[end_point];
510
511             if (pipe_table->pipe_number == 0xffff)
512             {
513                 break;
514             }
515
516             ret = usb0_api_host_SetEndpointTable(devadr, pipe_table, (uint8_t *)&configdescriptor[offset]);
517
518             if ((ret != USB_HOST_PIPE_IN) && (ret != USB_HOST_PIPE_OUT))
519             {
520                 return DEVDRV_ERROR;
521             }
522
523             ++end_point;
524         }
525
526         /* Next End Point Search */
527         offset += configdescriptor[offset];
528
529     } while (offset < totalLength);
530
531     return DEVDRV_SUCCESS;
532 }
533
534 /*******************************************************************************
535 * Function Name: usb0_api_host_clear_endpoint
536 * Description  : Clears the pipe definition table specified in the argument.
537 * Arguments    : USB_HOST_CFG_PIPETBL_t *user_table : pipe table
538 * Return Value : DEVDRV_SUCCESS       ; success
539 *              : DEVDRV_ERROR         ; error
540 *******************************************************************************/
541 int32_t usb0_api_host_clear_endpoint (USB_HOST_CFG_PIPETBL_t * user_table)
542 {
543     uint16_t pipe;
544
545     for (pipe = USB_HOST_PIPE0; pipe <= USB_HOST_MAX_PIPE_NO; ++pipe)
546     {
547         if (user_table->pipe_number == 0xffff)
548         {
549             break;
550         }
551         user_table->pipe_cfg         &= (USB_HOST_DBLBFIELD | USB_HOST_CNTMDFIELD);
552         user_table->pipe_max_pktsize  = 0;
553         user_table->pipe_cycle        = 0;
554
555         user_table++;
556     }
557
558     return DEVDRV_SUCCESS;
559 }
560
561 /*******************************************************************************
562 * Function Name: usb0_api_host_clear_endpoint_pipe
563 * Description  : Clears the pipe definition table specified in the argument.
564 * Arguments    : uint16_t pipe_sel                  : Pipe Number
565 *              : USB_HOST_CFG_PIPETBL_t *user_table : pipe table
566 * Return Value : DEVDRV_SUCCESS       ; success
567 *              : DEVDRV_ERROR         ; error
568 *******************************************************************************/
569 int32_t usb0_api_host_clear_endpoint_pipe (uint16_t pipe_sel, USB_HOST_CFG_PIPETBL_t * user_table)
570 {
571     uint16_t pipe;
572
573     for (pipe = USB_HOST_PIPE0; pipe <= USB_HOST_MAX_PIPE_NO; ++pipe)
574     {
575         if (user_table->pipe_number == 0xffff)
576         {
577             break;
578         }
579
580         if (user_table->pipe_number == pipe_sel)
581         {
582             user_table->pipe_cfg         &= (USB_HOST_DBLBFIELD | USB_HOST_CNTMDFIELD);
583             user_table->pipe_max_pktsize  = 0;
584             user_table->pipe_cycle        = 0;
585             break;
586         }
587
588         user_table++;
589     }
590
591     return DEVDRV_SUCCESS;
592 }
593 #endif
594
595 /*******************************************************************************
596 * Function Name: usb0_api_host_SetEndpointTable
597 * Description  : Sets the end point on the information specified by the argument.
598 * Arguments    : uint16_t devadr                    : device address
599 *              : USB_HOST_CFG_PIPETBL_t *user_table : pipe table
600 *              : uint8_t                *Table      : Endpoint descriptor
601 * Return Value : USB_HOST_DIR_H_IN           ; IN endpoint
602 *              : USB_HOST_DIR_H_OUT          ; OUT endpoint
603 *              : USB_END_POINT_ERROR         ; error
604 *******************************************************************************/
605 uint16_t usb0_api_host_SetEndpointTable (uint16_t devadr, USB_HOST_CFG_PIPETBL_t * user_table, uint8_t * Table)
606 {
607     uint16_t PipeCfg;
608     uint16_t PipeMaxp;
609     uint16_t pipe_number;
610     uint16_t ret;
611     uint16_t ret_flag = 0;                                          // avoid warning.
612
613     pipe_number = user_table->pipe_number;
614
615     if (Table[1] != USB_HOST_ENDPOINT_DESC)
616     {
617         return USB_END_POINT_ERROR;
618     }
619
620     switch (Table[3] & USB_HOST_EP_TYPE)
621     {
622         case USB_HOST_EP_CNTRL:
623             ret_flag =  USB_END_POINT_ERROR;
624         break;
625
626         case USB_HOST_EP_ISO:
627             if ((pipe_number != USB_HOST_PIPE1) && (pipe_number != USB_HOST_PIPE2))
628             {
629                 return USB_END_POINT_ERROR;
630             }
631
632             PipeCfg = USB_HOST_ISO;
633         break;
634
635         case USB_HOST_EP_BULK:
636             if ((pipe_number < USB_HOST_PIPE1) || (pipe_number > USB_HOST_PIPE5))
637             {
638                 return USB_END_POINT_ERROR;
639             }
640
641             PipeCfg = USB_HOST_BULK;
642         break;
643
644         case USB_HOST_EP_INT:
645             if ((pipe_number < USB_HOST_PIPE6) || (pipe_number > USB_HOST_PIPE9))
646             {
647                 return USB_END_POINT_ERROR;
648             }
649
650             PipeCfg = USB_HOST_INTERRUPT;
651         break;
652
653         default:
654             ret_flag = USB_END_POINT_ERROR;
655         break;
656     }
657
658     if (ret_flag == USB_END_POINT_ERROR)
659     {
660         return ret_flag;
661     }
662
663     /* Set pipe configuration table */
664     if ((Table[2] & USB_HOST_EP_DIR_MASK) == USB_HOST_EP_IN)        /* IN(receive) */
665     {
666         if (PipeCfg == USB_HOST_ISO)
667         {
668             /* Transfer Type is ISO*/
669             PipeCfg |= USB_HOST_DIR_H_IN;
670
671             switch (user_table->fifo_port)
672             {
673                 case USB_HOST_CUSE:
674                 case USB_HOST_D0USE:
675                 case USB_HOST_D1USE:
676                 case USB_HOST_D0DMA:
677                 case USB_HOST_D1DMA:
678                     PipeCfg |= (uint16_t)(user_table->pipe_cfg & USB_HOST_DBLBFIELD);
679                 break;
680
681                 default:
682                     ret_flag = USB_END_POINT_ERROR;
683                 break;
684             }
685
686             if (ret_flag == USB_END_POINT_ERROR)
687             {
688                 return ret_flag;
689             }
690         }
691         else
692         {
693             /* Transfer Type is BULK or INT */
694             PipeCfg |= (USB_HOST_SHTNAKON | USB_HOST_DIR_H_IN);             /* Compulsory SHTNAK */
695
696             switch (user_table->fifo_port)
697             {
698                 case USB_HOST_CUSE:
699                 case USB_HOST_D0USE:
700                 case USB_HOST_D1USE:
701                     PipeCfg |= (uint16_t)(user_table->pipe_cfg & (USB_HOST_DBLBFIELD | USB_HOST_CNTMDFIELD));
702                 break;
703
704                 case USB_HOST_D0DMA:
705                 case USB_HOST_D1DMA:
706                     PipeCfg |= (uint16_t)(user_table->pipe_cfg & (USB_HOST_DBLBFIELD | USB_HOST_CNTMDFIELD));
707 #ifdef  __USB_DMA_BFRE_ENABLE__
708                     /* this routine cannnot be perfomred if read operation is executed in buffer size */
709                     PipeCfg |= USB_HOST_BFREON;
710 #endif
711                 break;
712
713                 default:
714                     ret_flag = USB_END_POINT_ERROR;
715                 break;
716             }
717
718             if (ret_flag == USB_END_POINT_ERROR)
719             {
720                 return ret_flag;
721             }
722         }
723         ret = USB_HOST_PIPE_IN;
724     }
725     else                                                            /* OUT(send)    */
726     {
727         if (PipeCfg == USB_HOST_ISO)
728         {
729             /* Transfer Type is ISO*/
730             PipeCfg |= (uint16_t)(user_table->pipe_cfg & USB_HOST_DBLBFIELD);
731         }
732         else
733         {
734             /* Transfer Type is BULK or INT */
735             PipeCfg |= (uint16_t)(user_table->pipe_cfg & (USB_HOST_DBLBFIELD | USB_HOST_CNTMDFIELD));
736         }
737         PipeCfg |= USB_HOST_DIR_H_OUT;
738         ret = USB_HOST_PIPE_OUT;
739     }
740
741     switch (user_table->fifo_port)
742     {
743         case USB_HOST_CUSE:
744             g_usb0_host_PipeTbl[pipe_number] = (uint16_t)USB_HOST_CFIFO_USE;
745         break;
746
747         case USB_HOST_D0USE:
748             g_usb0_host_PipeTbl[pipe_number] = (uint16_t)USB_HOST_D0FIFO_USE;
749         break;
750
751         case USB_HOST_D1USE:
752             g_usb0_host_PipeTbl[pipe_number] = (uint16_t)USB_HOST_D1FIFO_USE;
753         break;
754
755         case USB_HOST_D0DMA:
756             g_usb0_host_PipeTbl[pipe_number] = (uint16_t)USB_HOST_D0FIFO_DMA;
757         break;
758
759         case USB_HOST_D1DMA:
760             g_usb0_host_PipeTbl[pipe_number] = (uint16_t)USB_HOST_D1FIFO_DMA;
761         break;
762
763         default:
764             ret_flag = USB_END_POINT_ERROR;
765         break;
766     }
767
768     if (ret_flag == USB_END_POINT_ERROR)
769     {
770         return ret_flag;
771     }
772
773     /* Endpoint number set              */
774     PipeCfg  |= (uint16_t)(Table[2] & USB_HOST_EP_NUM_MASK);
775     g_usb0_host_PipeTbl[pipe_number] |= (uint16_t)(Table[2] & USB_HOST_EP_NUM_MASK);
776
777     /* Max packet size set              */
778     PipeMaxp  = (uint16_t)((uint16_t)Table[4] | (uint16_t)((uint16_t)Table[5] << 8));
779
780     if (PipeMaxp == 0u)
781     {
782         return USB_END_POINT_ERROR;
783     }
784
785     /* Set device address               */
786     PipeMaxp |= (uint16_t)(devadr << 12);
787
788     user_table->pipe_cfg         = PipeCfg;
789     user_table->pipe_max_pktsize = PipeMaxp;
790
791     usb0_host_resetEP(user_table);
792
793     return ret;
794 }
795
796 /*******************************************************************************
797 * Function Name: usb0_host_resetEP
798 * Description  : Sets the end point on the information specified by the argument.
799 * Arguments    : USB_HOST_CFG_PIPETBL_t *tbl : pipe table
800 * Return Value : none
801 *******************************************************************************/
802 static void usb0_host_resetEP (USB_HOST_CFG_PIPETBL_t * tbl)
803 {
804
805     uint16_t pipe;
806
807     /* Host pipe */
808     /* The pipe number of pipe definition table is obtained */
809     pipe = (uint16_t)(tbl->pipe_number & USB_HOST_BITCURPIPE);  /* Pipe Number */
810
811     /* FIFO port access pipe is set to initial value */
812     /* The connection with FIFO should be cut before setting the pipe */
813     if (RZA_IO_RegRead_16(&USB200.CFIFOSEL,
814                             USB_CFIFOSEL_CURPIPE_SHIFT,
815                             USB_CFIFOSEL_CURPIPE) == pipe)
816     {
817         usb0_host_change_fifo_port(USB_HOST_PIPE0, USB_HOST_CUSE, USB_HOST_NO, USB_HOST_BITMBW_16);
818     }
819
820     if (RZA_IO_RegRead_16(&USB200.D0FIFOSEL,
821                             USB_DnFIFOSEL_CURPIPE_SHIFT,
822                             USB_DnFIFOSEL_CURPIPE) == pipe)
823     {
824         usb0_host_change_fifo_port(USB_HOST_PIPE0, USB_HOST_D0USE, USB_HOST_NO, USB_HOST_BITMBW_16);
825     }
826
827     if (RZA_IO_RegRead_16(&USB200.D1FIFOSEL,
828                             USB_DnFIFOSEL_CURPIPE_SHIFT,
829                             USB_DnFIFOSEL_CURPIPE) == pipe)
830     {
831         usb0_host_change_fifo_port(USB_HOST_PIPE0, USB_HOST_D1USE, USB_HOST_NO, USB_HOST_BITMBW_16);
832     }
833
834     /* Interrupt of pipe set is disabled */
835     usb0_host_disable_brdy_int(pipe);
836     usb0_host_disable_nrdy_int(pipe);
837     usb0_host_disable_bemp_int(pipe);
838
839     /* Pipe to set is set to NAK */
840     usb0_host_set_pid_nak(pipe);
841
842     /* Pipe is set */
843     USB200.PIPESEL  = pipe;
844
845     USB200.PIPECFG  = tbl->pipe_cfg;
846     USB200.PIPEBUF  = tbl->pipe_buf;
847     USB200.PIPEMAXP = tbl->pipe_max_pktsize;
848     USB200.PIPEPERI = tbl->pipe_cycle;
849
850     g_usb0_host_pipecfg[pipe]  = tbl->pipe_cfg;
851     g_usb0_host_pipebuf[pipe]  = tbl->pipe_buf;
852     g_usb0_host_pipemaxp[pipe] = tbl->pipe_max_pktsize;
853     g_usb0_host_pipeperi[pipe] = tbl->pipe_cycle;
854
855     /* Sequence bit clear */
856     usb0_host_set_sqclr(pipe);
857
858     usb0_host_aclrm(pipe);
859     usb0_host_set_csclr(pipe);
860
861     /* Pipe window selection is set to unused */
862     USB200.PIPESEL = USB_HOST_PIPE0;
863
864 }
865
866 #if(1) /* ohci_wrapp */
867 #else
868 /*******************************************************************************
869 * Function Name: usb0_api_host_data_count
870 * Description  : Get g_usb0_host_data_count[pipe]
871 * Arguments    : uint16_t pipe        ; Pipe Number
872 *              : uint32_t *data_count ; return g_usb0_data_count[pipe]
873 * Return Value : DEVDRV_SUCCESS    ; success
874 *              : DEVDRV_ERROR      ; error
875 *******************************************************************************/
876 int32_t usb0_api_host_data_count (uint16_t pipe, uint32_t * data_count)
877 {
878     if (pipe > USB_HOST_MAX_PIPE_NO)
879     {
880         return DEVDRV_ERROR;
881     }
882
883     *data_count = g_usb0_host_PipeDataSize[pipe];
884
885     return DEVDRV_SUCCESS;
886 }
887 #endif
888
889 /* End of File */