- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

# drivers/usb/usb_lib.c

Go to the documentation of this file.

    00001
    00043 #include <assert.h>
    00044 #include <mempool.h>
    00045 #include <physmem.h>
    00046 #include <util.h>
    00047 #include <usb/request.h>
    00048
    00049 #include <app/config_usb.h>
    00050
    00051 static struct mem_pool  usb_request_pool;
    00052
    00062 void usb_req_init(struct usb_request *req)
    00063 {
    00064         slist_init(&req->buf_list);
    00065         req->flags = 0;
    00066         req->bytes_xfered = 0;
    00067 }
    00068
    00075 struct usb_request *usb_req_alloc(void)
    00076 {
    00077         struct usb_request      *req;
    00078
    00079         req = mem_pool_alloc(&usb_request_pool);
    00080         if (req)
    00081                 usb_req_init(req);
    00082
    00083         return req;
    00084 }
    00085
    00091 void usb_req_free(struct usb_request *req)
    00092 {
    00093         // dbg_printf("free req %p\n", req);
    00094         mem_pool_free(&usb_request_pool, req);
    00095 }
    00096
    00107 struct usb_request *usb_req_alloc_single_tx(const void *data, size_t len)
    00108 {
    00109         struct usb_request      *req;
    00110         struct buffer           *buf;
    00111
    00112         buf = buffer_alloc();
    00113         if (!buf)
    00114                 return NULL;
    00115         req = usb_req_alloc();
    00116         if (!req) {
    00117                 buffer_free(buf);
    00118                 return NULL;
    00119         }
    00120
    00121         buffer_init_tx(buf, data, len);
    00122         usb_req_add_buffer(req, buf);
    00123
    00124         return req;
    00125 }
    00126
    00137 struct usb_request *usb_req_alloc_single_rx(void *data, size_t len)
    00138 {
    00139         struct usb_request      *req;
    00140         struct buffer           *buf;
    00141
    00142         buf = buffer_alloc();
    00143         if (!buf)
    00144                 return NULL;
    00145         req = usb_req_alloc();
    00146         if (!req) {
    00147                 buffer_free(buf);
    00148                 return NULL;
    00149         }
    00150
    00151         buffer_init_rx(buf, data, len);
    00152         usb_req_add_buffer(req, buf);
    00153
    00154         return req;
    00155 }
    00156
    00162 extern void usb_req_free_all(struct usb_request *req)
    00163 {
    00164         struct buffer   *buf;
    00165         struct buffer   *next;
    00166
    00167         usb_req_for_each_buffer_safe(req, buf, next) {
    00168                 dbg_printf("req %p buf %p next %p\n", req, buf, next);
    00169                 buffer_free(buf);
    00170         }
    00171         usb_req_free(req);
    00172 }
    00173
    00179 void usb_init(void)
    00180 {
    00181         mem_pool_init_physmem(&usb_request_pool, &cpu_sram_pool,
    00182                         CONFIG_USB_NR_REQUESTS,
    00183                         sizeof(struct usb_request), 2);
    00184 }