102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 302ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 2775973647SAndrew Thompson #ifndef _USB_TRANSFER_H_ 2875973647SAndrew Thompson #define _USB_TRANSFER_H_ 2902ac6454SAndrew Thompson 3002ac6454SAndrew Thompson /* 3102ac6454SAndrew Thompson * The following structure defines the messages that is used to signal 3202ac6454SAndrew Thompson * the "done_p" USB process. 3302ac6454SAndrew Thompson */ 34760bc48eSAndrew Thompson struct usb_done_msg { 35760bc48eSAndrew Thompson struct usb_proc_msg hdr; 36760bc48eSAndrew Thompson struct usb_xfer_root *xroot; 3702ac6454SAndrew Thompson }; 3802ac6454SAndrew Thompson 39bdc081c6SAndrew Thompson #define USB_DMATAG_TO_XROOT(dpt) \ 40760bc48eSAndrew Thompson ((struct usb_xfer_root *)( \ 41bdc081c6SAndrew Thompson ((uint8_t *)(dpt)) - \ 42760bc48eSAndrew Thompson ((uint8_t *)&((struct usb_xfer_root *)0)->dma_parent_tag))) 43bdc081c6SAndrew Thompson 4402ac6454SAndrew Thompson /* 4502ac6454SAndrew Thompson * The following structure is used to keep information about memory 4602ac6454SAndrew Thompson * that should be automatically freed at the moment all USB transfers 4702ac6454SAndrew Thompson * have been freed. 4802ac6454SAndrew Thompson */ 49760bc48eSAndrew Thompson struct usb_xfer_root { 50760bc48eSAndrew Thompson struct usb_dma_parent_tag dma_parent_tag; 51bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 52760bc48eSAndrew Thompson struct usb_xfer_queue dma_q; 53bdc081c6SAndrew Thompson #endif 54760bc48eSAndrew Thompson struct usb_xfer_queue done_q; 55760bc48eSAndrew Thompson struct usb_done_msg done_m[2]; 5602ac6454SAndrew Thompson struct cv cv_drain; 5702ac6454SAndrew Thompson 58760bc48eSAndrew Thompson struct usb_process *done_p; /* pointer to callback process */ 5902ac6454SAndrew Thompson void *memory_base; 6002ac6454SAndrew Thompson struct mtx *xfer_mtx; /* cannot be changed during operation */ 61bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 62760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_start; 63760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_end; 64bdc081c6SAndrew Thompson #endif 65760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_start; 66760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_end; 67760bc48eSAndrew Thompson struct usb_bus *bus; /* pointer to USB bus (cached) */ 68760bc48eSAndrew Thompson struct usb_device *udev; /* pointer to USB device */ 6902ac6454SAndrew Thompson 70f9cb546cSAndrew Thompson usb_size_t memory_size; 71f9cb546cSAndrew Thompson usb_size_t setup_refcount; 72578d0effSAndrew Thompson #if USB_HAVE_BUSDMA 73e0a69b51SAndrew Thompson usb_frcount_t dma_nframes; /* number of page caches to load */ 74e0a69b51SAndrew Thompson usb_frcount_t dma_currframe; /* currect page cache number */ 75e0a69b51SAndrew Thompson usb_frlength_t dma_frlength_0; /* length of page cache zero */ 7602ac6454SAndrew Thompson uint8_t dma_error; /* set if virtual memory could not be 7702ac6454SAndrew Thompson * loaded */ 78578d0effSAndrew Thompson #endif 7902ac6454SAndrew Thompson uint8_t done_sleep; /* set if done thread is sleeping */ 8002ac6454SAndrew Thompson }; 8102ac6454SAndrew Thompson 8202ac6454SAndrew Thompson /* 8302ac6454SAndrew Thompson * The following structure is used when setting up an array of USB 8402ac6454SAndrew Thompson * transfers. 8502ac6454SAndrew Thompson */ 86760bc48eSAndrew Thompson struct usb_setup_params { 87760bc48eSAndrew Thompson struct usb_dma_tag *dma_tag_p; 88760bc48eSAndrew Thompson struct usb_page *dma_page_ptr; 89760bc48eSAndrew Thompson struct usb_page_cache *dma_page_cache_ptr; /* these will be 9002ac6454SAndrew Thompson * auto-freed */ 91760bc48eSAndrew Thompson struct usb_page_cache *xfer_page_cache_ptr; /* these will not be 9202ac6454SAndrew Thompson * auto-freed */ 93760bc48eSAndrew Thompson struct usb_device *udev; 94760bc48eSAndrew Thompson struct usb_xfer *curr_xfer; 95760bc48eSAndrew Thompson const struct usb_config *curr_setup; 96760bc48eSAndrew Thompson const struct usb_pipe_methods *methods; 9702ac6454SAndrew Thompson void *buf; 98e0a69b51SAndrew Thompson usb_frlength_t *xfer_length_ptr; 9902ac6454SAndrew Thompson 100f9cb546cSAndrew Thompson usb_size_t size[7]; 101e0a69b51SAndrew Thompson usb_frlength_t bufsize; 102e0a69b51SAndrew Thompson usb_frlength_t bufsize_max; 10302ac6454SAndrew Thompson 104*963169b4SHans Petter Selasky uint32_t hc_max_frame_size; 10502ac6454SAndrew Thompson uint16_t hc_max_packet_size; 10602ac6454SAndrew Thompson uint8_t hc_max_packet_count; 1078d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 10802ac6454SAndrew Thompson uint8_t dma_tag_max; 109e0a69b51SAndrew Thompson usb_error_t err; 11002ac6454SAndrew Thompson }; 11102ac6454SAndrew Thompson 11202ac6454SAndrew Thompson /* function prototypes */ 11302ac6454SAndrew Thompson 114a593f6b8SAndrew Thompson uint8_t usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 115f9cb546cSAndrew Thompson struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 116f9cb546cSAndrew Thompson usb_size_t count); 117*963169b4SHans Petter Selasky void usb_dma_delay_done_cb(struct usb_xfer *); 118a593f6b8SAndrew Thompson void usb_command_wrapper(struct usb_xfer_queue *pq, 119760bc48eSAndrew Thompson struct usb_xfer *xfer); 120a593f6b8SAndrew Thompson void usbd_pipe_enter(struct usb_xfer *xfer); 121a593f6b8SAndrew Thompson void usbd_pipe_start(struct usb_xfer_queue *pq); 122a593f6b8SAndrew Thompson void usbd_transfer_dequeue(struct usb_xfer *xfer); 123a593f6b8SAndrew Thompson void usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error); 124a593f6b8SAndrew Thompson void usbd_transfer_enqueue(struct usb_xfer_queue *pq, 125760bc48eSAndrew Thompson struct usb_xfer *xfer); 126a593f6b8SAndrew Thompson void usbd_transfer_setup_sub(struct usb_setup_params *parm); 1275b3bb704SAndrew Thompson void usbd_ctrl_transfer_setup(struct usb_device *udev); 128*963169b4SHans Petter Selasky void usbd_clear_stall_locked(struct usb_device *udev, 129*963169b4SHans Petter Selasky struct usb_endpoint *ep); 130a593f6b8SAndrew Thompson void usbd_clear_data_toggle(struct usb_device *udev, 131ae60fdfbSAndrew Thompson struct usb_endpoint *ep); 132a593f6b8SAndrew Thompson usb_callback_t usbd_do_request_callback; 133a593f6b8SAndrew Thompson usb_callback_t usb_handle_request_callback; 134a593f6b8SAndrew Thompson usb_callback_t usb_do_clear_stall_callback; 135a593f6b8SAndrew Thompson void usbd_transfer_timeout_ms(struct usb_xfer *xfer, 136e0a69b51SAndrew Thompson void (*cb) (void *arg), usb_timeout_t ms); 137527aa7b2SAndrew Thompson usb_timeout_t usbd_get_dma_delay(struct usb_device *udev); 138a593f6b8SAndrew Thompson void usbd_transfer_power_ref(struct usb_xfer *xfer, int val); 13902ac6454SAndrew Thompson 14075973647SAndrew Thompson #endif /* _USB_TRANSFER_H_ */ 141