]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/libs4c/usb/base/usb/usb.h
Merge branch 'master' into can-usb1
[lincan.git] / embedded / libs4c / usb / base / usb / usb.h
1 /**************************************************************/
2 /***   Module : USB module - header file                    ***/
3 /***   Author : Roman Bartosinski (C) 28.04.2002            ***/
4 /***   Modify : 08.08.2002                                  ***/
5 /***   Rewrite: 05.09.2002                                  ***/
6 /**************************************************************/
7
8 #ifndef _USB_MODULE_
9   #define _USB_MODULE_
10
11   #include "usb_spec.h"
12   #include "usb_devdes.h"
13   
14 #if defined(SDCC) || defined(__KEIL__) || defined(__C51__)
15   /*8051 special handling*/
16   #define REENTRANT_SIGN reentrant
17 #else
18   #define xdata           /*nothing*/
19   #define REENTRANT_SIGN  /*nothing*/
20 #endif
21
22 #define USB_WITH_CB_FNC
23 //#define USB_WITH_UDEV_FNC
24
25 #ifdef USB_WITH_UDEV_FNC
26   #define USB_UDEV_REENTRANT_SIGN REENTRANT_SIGN
27 #else /*USB_WITH_UDEV_FNC*/
28   #define USB_UDEV_REENTRANT_SIGN /*nothing*/
29 #endif /*USB_WITH_UDEV_FNC*/
30
31 /* control endpoint */
32   #define MAX_CONTROL_XFER_DATA_SIZE 8
33
34   struct usb_ep_t;
35   
36   typedef void endfnc_t( struct  usb_ep_t *ep) REENTRANT_SIGN;
37   
38   #define USB_NEXT_PKT_SEND 0
39   #define USB_NEXT_PKT_REC  1
40
41   #define USB_COMPLETE_OK   0
42   #define USB_COMPLETE_FAIL -1
43
44   typedef struct usb_ep_t {
45     struct usb_device_t *udev;      /* pointer to parent device */
46     unsigned short max_packet_size; /* max. size of endpoint package, e.g. PDI_EP0_PACKET_SIZE */
47     unsigned char *ptr;             /* pointer to current transmitted data */
48     unsigned int  size;             /* full size of current transmitted data */
49     unsigned int  actual;           /* transmitted data size */
50     endfnc_t *efnc;                 /* ??? */
51     unsigned char flags;            /* endpoint flags & state - idle,receiving, transmitting  ??? HERE ??? */
52     unsigned char epnum;            /* endpoint number (index) - endpoint0 must be set to 0 */
53     unsigned short event_mask;      /* event(interrupt) mask for this endpoint, e.g. PDI_INT_EP1_IN for pdiusbd1x */
54    #ifdef USB_WITH_CB_FNC
55     int (*next_pkt_fnc)(struct usb_ep_t *ep, int len, int codeval) REENTRANT_SIGN;
56     int (*complete_fnc)(struct usb_ep_t *ep, int status) REENTRANT_SIGN;
57     long user_data;
58    #endif /*USB_WITH_CB_FNC*/
59   } usb_ep_t;
60
61
62 /* Vendor & Class functions */
63 /*
64  #ifdef USB_WITH_CB_FNC
65   typedef int usb_vendor_extension_fnc_t(usb_ep_t *ep, USB_DEVICE_REQUEST *dreq);
66   extern xdata usb_vendor_extension_fnc_t USBVendorRequestCBFnc;
67
68   typedef int usb_class_extension_fnc_t(usb_ep_t *ep, USB_DEVICE_REQUEST *dreq);
69   extern xdata usb_class_extension_fnc_t USBClassRequestCBFnc;
70  #else //USB_WITH_CB_FNC
71   char USBVendorRequest( USB_DEVICE_REQUEST *dr);
72   char USBClassRequest( USB_DEVICE_REQUEST *dr);
73  #endif //USB_WITH_CB_FNC
74 */
75
76  
77 /* USB device */  
78   typedef struct usb_device_t {
79     unsigned char id;               /* device ID ??? */
80     unsigned char flags;            /* usb device flags + endpoint0 events */
81     unsigned char ep_events;        /* one bit for each endpoint (without ep0) event,(bit0 for udev->ep[0], bit1 for udev->ep[1], ...)*/
82     unsigned char configuration;    /* current configuration */
83 //    unsigned char interface;        /* current interface */
84 //    unsigned char altinterface;     /* current alternative interface */
85
86     //int (stdreq[13])( struct usb_device_t *udev) REENTRANT_SIGN;    /* pointer to array of standard request processing functions */
87     int (*vendor_fnc)( struct usb_device_t *udev) REENTRANT_SIGN;     /* pointer to vendor request processing function */
88     int (*class_fnc)( struct usb_device_t *udev) REENTRANT_SIGN;      /* pointer to class request processing function */
89
90     const USB_DEVICE_DESCRIPTORS_TABLE *devdes_table;
91
92    #ifdef USB_WITH_UDEV_FNC
93     int (*init)( struct usb_device_t *udev) REENTRANT_SIGN;              /* function for hw specific part of initialize usb device */
94     int (*set_addr)( struct usb_device_t *udev, unsigned char addr) REENTRANT_SIGN;  /* set device address */
95     int (*set_configuration)( struct usb_device_t *udev, unsigned char iCfg) REENTRANT_SIGN;  /* set device configuration */
96     int (*connect)( struct usb_device_t *udev) REENTRANT_SIGN;           /* function for hw specific part of connecting device to usb */
97     int (*disconnect)( struct usb_device_t *udev) REENTRANT_SIGN;        /* function for hw specific part of disconnecting device to usb */
98     void (*ack_setup)( struct usb_device_t *udev) REENTRANT_SIGN;        /* function for hw specific part of control response acknowledge */
99     void (*ack_control_setup)( struct usb_device_t *udev) REENTRANT_SIGN;        /* function for hw specific part of control response acknowledge */
100     int (*check_events)( struct usb_device_t *udev) REENTRANT_SIGN;      /* hw specific part of function for checking events */
101     void (*stall)( usb_ep_t *ep) REENTRANT_SIGN;                         /* hw specific function to stall endpoint */
102     void (*unstall)( usb_ep_t *ep) REENTRANT_SIGN;                       /* hw specific function to unstall endpoint */
103     int (*read_endpoint)( usb_ep_t *ep, void *ptr, int size) REENTRANT_SIGN;
104     int (*write_endpoint)( usb_ep_t *ep, const void *ptr, int size) REENTRANT_SIGN;
105    #endif /*USB_WITH_UDEV_FNC*/
106     
107 //    USB_DEVICE_REQUEST *request;   /* current usb request - only if there is a valid usb request in processing */
108     USB_DEVICE_REQUEST request;    /* usb device request */
109     
110     unsigned char cntep;           /* number of device endpoints in ep array without EP0 */
111     usb_ep_t ep0;                  /* endpoint 0 */
112     usb_ep_t *ep;                 /* others endpoints in array */
113   } usb_device_t;
114
115
116 /* endpoint flags */
117   /* endpoint state */
118   #define USB_STATE_IDLE       0x00
119   #define USB_STATE_TRANSMIT   0x01
120   #define USB_STATE_RECEIVE    0x02
121   #define USB_STATE_MASK       0x03
122
123 /* usb_device flags */
124   #define USB_FLAG_CONFIGURED   0x01
125   #define USB_FLAG_BUS_RESET    0x02
126   #define USB_FLAG_SUSPEND      0x04
127   #define USB_FLAG_SETUP        0x08 // setup_packet
128   #define USB_FLAG_REMOTE_WAKE  0x10
129   
130   #define USB_FLAG_EVENT_RX0  0x40
131   #define USB_FLAG_EVENT_TX0  0x80
132   
133
134
135 /* device functions - inline ??? */
136   int usb_init( usb_device_t *udev);
137   int usb_connect( usb_device_t *udev);
138   int usb_disconnect( usb_device_t *udev);
139   void usb_stall( usb_ep_t *ep);
140   void usb_unstall( usb_ep_t *ep);
141
142   #define usb_stall_ep0( udev) \
143     do { \
144       usb_stall( &(udev->ep0)); \
145     } while(0)
146
147 /* check usb events(interrupts) */
148   int usb_check_events( usb_device_t *udev);
149 /* response to standard constrol requests */
150   int usb_control_response( usb_device_t *udev);  
151 /* send control data */
152   void usb_send_control_data( usb_device_t *udev, unsigned char *pData, unsigned short len);
153   void usb_set_control_endfnc( usb_device_t *udev, endfnc_t *efnc);// REENTRANT_SIGN;
154   void usb_ack_setup( usb_ep_t *ep);
155
156   
157 /* Standard requests functions */
158 //  typedef int (*usb_stdreq_fnc_t)( usb_device_t *udev) REENTRANT_SIGN;
159 //  extern xdata usb_stdreq_fnc_t usb_standard_requests[13];
160   
161
162 #ifdef USB_WITH_UDEV_FNC
163
164   #define usb_udev_is_fnc(_M_udev, _M_fnc) (_M_udev->_M_fnc)
165   
166   #define usb_udev_init(_M_udev) (_M_udev->init(_M_udev))
167   #define usb_udev_set_addr(_M_udev, _M_addr) (_M_udev->set_addr(_M_udev, _M_addr))
168   #define usb_udev_set_configuration(_M_udev, _M_iCfg) (_M_udev->set_configuration(_M_udev, _M_iCfg))
169   #define usb_udev_connect(_M_udev) (_M_udev->connect(_M_udev))
170   #define usb_udev_disconnect(_M_udev) (_M_udev->disconnect(_M_udev))
171   #define usb_udev_ack_setup(_M_udev) (_M_udev->ack_setup(_M_udev))
172   #define usb_udev_ack_control_setup(_M_udev) (_M_udev->ack_control_setup(_M_udev))
173   #define usb_udev_check_events(_M_udev) (_M_udev->check_events(_M_udev))
174
175   #define usb_udev_stall(_M_ep) ((_M_ep)->udev->stall(_M_ep))
176   #define usb_udev_unstall(_M_ep) ((_M_ep)->udev->unstall(_M_ep))
177
178   #define usb_udev_read_endpoint(_M_ep, _M_ptr, _M_size) \
179         ((_M_ep)->udev->read_endpoint(_M_ep, _M_ptr, _M_size))
180
181   #define usb_udev_write_endpoint(_M_ep, _M_ptr, _M_size) \
182         ((_M_ep)->udev->write_endpoint(_M_ep, _M_ptr, _M_size))
183
184 #else /*USB_WITH_UDEV_FNC*/
185
186   #define USB_PDI_DIRECT_FNC
187   #include "pdi.h"
188
189 #endif /*USB_WITH_UDEV_FNC*/
190  
191 #endif