1 /*- 2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #ifndef _USB_TRANSFER_H_ 27 #define _USB_TRANSFER_H_ 28 29 /* 30 * Definition of internal USB transfer states: 31 * =========================================== 32 * 33 * The main reason there are many USB states is that we are allowed to 34 * cancel USB transfers, then start the USB transfer again and that 35 * this state transaction cannot always be done in a single atomic 36 * operation without blocking the calling thread. One reason for this 37 * is that the USB hardware sometimes needs to wait for DMA 38 * controllers to finish which is done asynchronously and grows the 39 * statemachine. 40 * 41 * When extending the following statemachine there are basically two 42 * things you should think about: Which states should be executed or 43 * modified in case of USB transfer stop and which states should be 44 * executed or modified in case of USB transfer start. Also respect 45 * the "can_cancel_immed" flag which basically tells if you can go 46 * directly from a wait state to the cancelling states. 47 */ 48 49 enum { 50 /* XFER start execute state */ 51 52 /* USB_ST_SETUP = 0 (already defined) */ 53 54 /* XFER transferred execute state */ 55 56 /* USB_ST_TRANSFERRED = 1 (already defined) */ 57 58 /* XFER error execute state */ 59 60 /* USB_ST_ERROR = 2 (already defined) */ 61 62 /* XFER restart after error execute state */ 63 64 USB_ST_RESTART = 8, 65 66 /* XFER transfer idle state */ 67 68 USB_ST_WAIT_SETUP, 69 70 /* Other XFER execute states */ 71 72 USB_ST_PIPE_OPEN = 16, 73 USB_ST_PIPE_OPEN_ERROR, 74 USB_ST_PIPE_OPEN_RESTART, 75 76 USB_ST_BDMA_LOAD, 77 USB_ST_BDMA_LOAD_ERROR, 78 USB_ST_BDMA_LOAD_RESTART, 79 80 USB_ST_IVAL_DLY, 81 USB_ST_IVAL_DLY_ERROR, 82 USB_ST_IVAL_DLY_RESTART, 83 84 USB_ST_PIPE_STALL, 85 USB_ST_PIPE_STALL_ERROR, 86 USB_ST_PIPE_STALL_RESTART, 87 88 USB_ST_ENTER, 89 USB_ST_ENTER_ERROR, 90 USB_ST_ENTER_RESTART, 91 92 USB_ST_START, 93 USB_ST_START_ERROR, 94 USB_ST_START_RESTART, 95 96 USB_ST_PIPE_CLOSE, 97 USB_ST_PIPE_CLOSE_ERROR, 98 USB_ST_PIPE_CLOSE_RESTART, 99 100 USB_ST_BDMA_DLY, 101 USB_ST_BDMA_DLY_ERROR, 102 USB_ST_BDMA_DLY_RESTART, 103 104 /* XFER transfer wait states */ 105 106 USB_ST_WAIT_PIPE_OPEN = 64, 107 USB_ST_WAIT_PIPE_OPEN_ERROR, 108 USB_ST_WAIT_PIPE_OPEN_RESTART, 109 110 USB_ST_WAIT_BDMA_LOAD, 111 USB_ST_WAIT_BDMA_LOAD_ERROR, 112 USB_ST_WAIT_BDMA_LOAD_RESTART, 113 114 USB_ST_WAIT_IVAL_DLY, 115 USB_ST_WAIT_IVAL_DLY_ERROR, 116 USB_ST_WAIT_IVAL_DLY_RESTART, 117 118 USB_ST_WAIT_PIPE_STALL, 119 USB_ST_WAIT_PIPE_STALL_ERROR, 120 USB_ST_WAIT_PIPE_STALL_RESTART, 121 122 USB_ST_WAIT_ENTER, 123 USB_ST_WAIT_ENTER_ERROR, 124 USB_ST_WAIT_ENTER_RESTART, 125 126 USB_ST_WAIT_START, 127 USB_ST_WAIT_START_ERROR, 128 USB_ST_WAIT_START_RESTART, 129 130 USB_ST_WAIT_PIPE_CLOSE, 131 USB_ST_WAIT_PIPE_CLOSE_ERROR, 132 USB_ST_WAIT_PIPE_CLOSE_RESTART, 133 134 USB_ST_WAIT_BDMA_DLY, 135 USB_ST_WAIT_BDMA_DLY_ERROR, 136 USB_ST_WAIT_BDMA_DLY_RESTART, 137 138 USB_ST_WAIT_TRANSFERRED, 139 USB_ST_WAIT_TRANSFERRED_ERROR, 140 USB_ST_WAIT_TRANSFERRED_RESTART, 141 }; 142 143 /* 144 * The following structure defines the messages that is used to signal 145 * the "done_p" USB process. 146 */ 147 struct usb_done_msg { 148 struct usb_proc_msg hdr; 149 struct usb_xfer_root *xroot; 150 }; 151 152 #define USB_DMATAG_TO_XROOT(dpt) \ 153 ((struct usb_xfer_root *)( \ 154 ((uint8_t *)(dpt)) - \ 155 ((uint8_t *)&((struct usb_xfer_root *)0)->dma_parent_tag))) 156 157 /* 158 * The following structure is used to keep information about memory 159 * that should be automatically freed at the moment all USB transfers 160 * have been freed. 161 */ 162 struct usb_xfer_root { 163 struct usb_dma_parent_tag dma_parent_tag; 164 #if USB_HAVE_BUSDMA 165 struct usb_xfer_queue dma_q; 166 #endif 167 struct usb_xfer_queue done_q; 168 struct usb_done_msg done_m[2]; 169 struct cv cv_drain; 170 171 struct usb_process *done_p; /* pointer to callback process */ 172 void *memory_base; 173 struct lock *xfer_lock; /* cannot be changed during operation */ 174 #if USB_HAVE_BUSDMA 175 struct usb_page_cache *dma_page_cache_start; 176 struct usb_page_cache *dma_page_cache_end; 177 #endif 178 struct usb_page_cache *xfer_page_cache_start; 179 struct usb_page_cache *xfer_page_cache_end; 180 struct usb_bus *bus; /* pointer to USB bus (cached) */ 181 struct usb_device *udev; /* pointer to USB device */ 182 183 usb_size_t memory_size; 184 usb_size_t setup_refcount; 185 #if USB_HAVE_BUSDMA 186 usb_frcount_t dma_nframes; /* number of page caches to load */ 187 usb_frcount_t dma_currframe; /* currect page cache number */ 188 usb_frlength_t dma_frlength_0; /* length of page cache zero */ 189 uint8_t dma_error; /* set if virtual memory could not be 190 * loaded */ 191 #endif 192 uint8_t done_sleep; /* set if done thread is sleeping */ 193 }; 194 195 /* 196 * The following structure is used when setting up an array of USB 197 * transfers. 198 */ 199 struct usb_setup_params { 200 struct usb_dma_tag *dma_tag_p; 201 struct usb_page *dma_page_ptr; 202 struct usb_page_cache *dma_page_cache_ptr; /* these will be 203 * auto-freed */ 204 struct usb_page_cache *xfer_page_cache_ptr; /* these will not be 205 * auto-freed */ 206 struct usb_device *udev; 207 struct usb_xfer *curr_xfer; 208 const struct usb_config *curr_setup; 209 const struct usb_pipe_methods *methods; 210 void *buf; 211 usb_frlength_t *xfer_length_ptr; 212 213 usb_size_t size[7]; 214 usb_frlength_t bufsize; 215 usb_frlength_t bufsize_max; 216 217 uint32_t hc_max_frame_size; 218 uint16_t hc_max_packet_size; 219 uint8_t hc_max_packet_count; 220 enum usb_dev_speed speed; 221 uint8_t dma_tag_max; 222 usb_error_t err; 223 }; 224 225 /* function prototypes */ 226 227 uint8_t usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 228 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 229 usb_size_t count); 230 void usb_dma_delay_done_cb(struct usb_xfer *); 231 void usb_command_wrapper(struct usb_xfer_queue *pq, 232 struct usb_xfer *xfer); 233 void usbd_pipe_enter(struct usb_xfer *xfer); 234 void usbd_pipe_start(struct usb_xfer_queue *pq); 235 void usbd_transfer_dequeue(struct usb_xfer *xfer); 236 void usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error); 237 void usbd_transfer_enqueue(struct usb_xfer_queue *pq, 238 struct usb_xfer *xfer); 239 void usbd_transfer_setup_sub(struct usb_setup_params *parm); 240 void usbd_ctrl_transfer_setup(struct usb_device *udev); 241 void usbd_clear_stall_locked(struct usb_device *udev, 242 struct usb_endpoint *ep); 243 void usbd_clear_data_toggle(struct usb_device *udev, 244 struct usb_endpoint *ep); 245 usb_callback_t usbd_do_request_callback; 246 usb_callback_t usb_handle_request_callback; 247 usb_callback_t usb_do_clear_stall_callback; 248 void usbd_transfer_timeout_ms(struct usb_xfer *xfer, 249 void (*cb) (void *arg), usb_timeout_t ms); 250 usb_timeout_t usbd_get_dma_delay(struct usb_device *udev); 251 void usbd_transfer_power_ref(struct usb_xfer *xfer, int val); 252 253 #endif /* _USB_TRANSFER_H_ */ 254