102ac6454SAndrew Thompson /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 402ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 502ac6454SAndrew Thompson * 602ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 702ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 802ac6454SAndrew Thompson * are met: 902ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1002ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1102ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1302ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1402ac6454SAndrew Thompson * 1502ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1602ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1702ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1802ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1902ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2002ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2102ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2202ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2302ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2402ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2502ac6454SAndrew Thompson * SUCH DAMAGE. 2602ac6454SAndrew Thompson */ 2702ac6454SAndrew Thompson 2875973647SAndrew Thompson #ifndef _USB_TRANSFER_H_ 2975973647SAndrew Thompson #define _USB_TRANSFER_H_ 3002ac6454SAndrew Thompson 3102ac6454SAndrew Thompson /* 3265788daeSHans Petter Selasky * A few words about USB transfer states: 3365788daeSHans Petter Selasky * ====================================== 349c7e90baSHans Petter Selasky * 3565788daeSHans Petter Selasky * USB transfers can have multiple states, because they can be 3665788daeSHans Petter Selasky * cancelled and started again and this cannot always be done 3765788daeSHans Petter Selasky * atomically under a mutex. One reason for this is that the USB 3865788daeSHans Petter Selasky * hardware sometimes needs to wait for DMA controllers to finish 3965788daeSHans Petter Selasky * which is done asynchronously and grows the statemachine. 409c7e90baSHans Petter Selasky */ 419c7e90baSHans Petter Selasky 429c7e90baSHans Petter Selasky /* 4302ac6454SAndrew Thompson * The following structure defines the messages that is used to signal 4402ac6454SAndrew Thompson * the "done_p" USB process. 4502ac6454SAndrew Thompson */ 46760bc48eSAndrew Thompson struct usb_done_msg { 47760bc48eSAndrew Thompson struct usb_proc_msg hdr; 48760bc48eSAndrew Thompson struct usb_xfer_root *xroot; 4902ac6454SAndrew Thompson }; 5002ac6454SAndrew Thompson 51bdc081c6SAndrew Thompson #define USB_DMATAG_TO_XROOT(dpt) \ 5252ab576dSJohn Baldwin __containerof(dpt, struct usb_xfer_root, dma_parent_tag) 53bdc081c6SAndrew Thompson 5402ac6454SAndrew Thompson /* 5502ac6454SAndrew Thompson * The following structure is used to keep information about memory 5602ac6454SAndrew Thompson * that should be automatically freed at the moment all USB transfers 5702ac6454SAndrew Thompson * have been freed. 5802ac6454SAndrew Thompson */ 59760bc48eSAndrew Thompson struct usb_xfer_root { 60760bc48eSAndrew Thompson struct usb_dma_parent_tag dma_parent_tag; 61bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 62760bc48eSAndrew Thompson struct usb_xfer_queue dma_q; 63bdc081c6SAndrew Thompson #endif 64760bc48eSAndrew Thompson struct usb_xfer_queue done_q; 65760bc48eSAndrew Thompson struct usb_done_msg done_m[2]; 6602ac6454SAndrew Thompson struct cv cv_drain; 6702ac6454SAndrew Thompson 68760bc48eSAndrew Thompson struct usb_process *done_p; /* pointer to callback process */ 6902ac6454SAndrew Thompson void *memory_base; 7002ac6454SAndrew Thompson struct mtx *xfer_mtx; /* cannot be changed during operation */ 71bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 72760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_start; 73760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_end; 74bdc081c6SAndrew Thompson #endif 75760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_start; 76760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_end; 77760bc48eSAndrew Thompson struct usb_bus *bus; /* pointer to USB bus (cached) */ 78760bc48eSAndrew Thompson struct usb_device *udev; /* pointer to USB device */ 7902ac6454SAndrew Thompson 80f9cb546cSAndrew Thompson usb_size_t memory_size; 81f9cb546cSAndrew Thompson usb_size_t setup_refcount; 82578d0effSAndrew Thompson #if USB_HAVE_BUSDMA 83e0a69b51SAndrew Thompson usb_frcount_t dma_nframes; /* number of page caches to load */ 84e0a69b51SAndrew Thompson usb_frcount_t dma_currframe; /* currect page cache number */ 85e0a69b51SAndrew Thompson usb_frlength_t dma_frlength_0; /* length of page cache zero */ 8602ac6454SAndrew Thompson uint8_t dma_error; /* set if virtual memory could not be 8702ac6454SAndrew Thompson * loaded */ 88578d0effSAndrew Thompson #endif 8902ac6454SAndrew Thompson uint8_t done_sleep; /* set if done thread is sleeping */ 9002ac6454SAndrew Thompson }; 9102ac6454SAndrew Thompson 9202ac6454SAndrew Thompson /* 9302ac6454SAndrew Thompson * The following structure is used when setting up an array of USB 9402ac6454SAndrew Thompson * transfers. 9502ac6454SAndrew Thompson */ 96760bc48eSAndrew Thompson struct usb_setup_params { 97760bc48eSAndrew Thompson struct usb_dma_tag *dma_tag_p; 98760bc48eSAndrew Thompson struct usb_page *dma_page_ptr; 99760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_ptr; /* these will be 10002ac6454SAndrew Thompson * auto-freed */ 101760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_ptr; /* these will not be 10202ac6454SAndrew Thompson * auto-freed */ 103760bc48eSAndrew Thompson struct usb_device *udev; 104760bc48eSAndrew Thompson struct usb_xfer *curr_xfer; 105760bc48eSAndrew Thompson const struct usb_config *curr_setup; 106760bc48eSAndrew Thompson const struct usb_pipe_methods *methods; 10702ac6454SAndrew Thompson void *buf; 108e0a69b51SAndrew Thompson usb_frlength_t *xfer_length_ptr; 10902ac6454SAndrew Thompson 110f9cb546cSAndrew Thompson usb_size_t size[7]; 111e0a69b51SAndrew Thompson usb_frlength_t bufsize; 112e0a69b51SAndrew Thompson usb_frlength_t bufsize_max; 11302ac6454SAndrew Thompson 114963169b4SHans Petter Selasky uint32_t hc_max_frame_size; 11502ac6454SAndrew Thompson uint16_t hc_max_packet_size; 11602ac6454SAndrew Thompson uint8_t hc_max_packet_count; 1178d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 11802ac6454SAndrew Thompson uint8_t dma_tag_max; 119e0a69b51SAndrew Thompson usb_error_t err; 12002ac6454SAndrew Thompson }; 12102ac6454SAndrew Thompson 12202ac6454SAndrew Thompson /* function prototypes */ 12302ac6454SAndrew Thompson 124a593f6b8SAndrew Thompson uint8_t usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 125f9cb546cSAndrew Thompson struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 126f9cb546cSAndrew Thompson usb_size_t count); 127963169b4SHans Petter Selasky void usb_dma_delay_done_cb(struct usb_xfer *); 128a593f6b8SAndrew Thompson void usb_command_wrapper(struct usb_xfer_queue *pq, 129760bc48eSAndrew Thompson struct usb_xfer *xfer); 130a593f6b8SAndrew Thompson void usbd_pipe_enter(struct usb_xfer *xfer); 131a593f6b8SAndrew Thompson void usbd_pipe_start(struct usb_xfer_queue *pq); 132a593f6b8SAndrew Thompson void usbd_transfer_dequeue(struct usb_xfer *xfer); 133a593f6b8SAndrew Thompson void usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error); 134a593f6b8SAndrew Thompson void usbd_transfer_enqueue(struct usb_xfer_queue *pq, 135760bc48eSAndrew Thompson struct usb_xfer *xfer); 136a593f6b8SAndrew Thompson void usbd_transfer_setup_sub(struct usb_setup_params *parm); 1375b3bb704SAndrew Thompson void usbd_ctrl_transfer_setup(struct usb_device *udev); 138963169b4SHans Petter Selasky void usbd_clear_stall_locked(struct usb_device *udev, 139963169b4SHans Petter Selasky struct usb_endpoint *ep); 140a593f6b8SAndrew Thompson void usbd_clear_data_toggle(struct usb_device *udev, 141ae60fdfbSAndrew Thompson struct usb_endpoint *ep); 142a593f6b8SAndrew Thompson usb_callback_t usbd_do_request_callback; 143a593f6b8SAndrew Thompson usb_callback_t usb_handle_request_callback; 144a593f6b8SAndrew Thompson usb_callback_t usb_do_clear_stall_callback; 145a593f6b8SAndrew Thompson void usbd_transfer_timeout_ms(struct usb_xfer *xfer, 146e0a69b51SAndrew Thompson void (*cb) (void *arg), usb_timeout_t ms); 147527aa7b2SAndrew Thompson usb_timeout_t usbd_get_dma_delay(struct usb_device *udev); 148a593f6b8SAndrew Thompson void usbd_transfer_power_ref(struct usb_xfer *xfer, int val); 1498fc2a3c4SHans Petter Selasky uint8_t usbd_xfer_get_isochronous_start_frame(struct usb_xfer *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t *); 15002ac6454SAndrew Thompson 15175973647SAndrew Thompson #endif /* _USB_TRANSFER_H_ */ 152