xref: /freebsd/sys/dev/usb/usb_transfer.h (revision e0a69b51ac09dc0376611274903ba683e2d1ace7)
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 
2702ac6454SAndrew Thompson #ifndef _USB2_TRANSFER_H_
2802ac6454SAndrew Thompson #define	_USB2_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 
70e0a69b51SAndrew Thompson 	size_t memory_size;
71e0a69b51SAndrew Thompson 	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 
100e0a69b51SAndrew Thompson 	size_t size[7];
101e0a69b51SAndrew Thompson 	usb_frlength_t bufsize;
102e0a69b51SAndrew Thompson 	usb_frlength_t bufsize_max;
10302ac6454SAndrew Thompson 
104578d0effSAndrew Thompson 	uint16_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 
114760bc48eSAndrew Thompson uint8_t	usb2_transfer_setup_sub_malloc(struct usb_setup_params *parm,
115e0a69b51SAndrew Thompson 	    struct usb_page_cache **ppc, size_t size, size_t align,
116e0a69b51SAndrew Thompson 	    size_t count);
117760bc48eSAndrew Thompson void	usb2_command_wrapper(struct usb_xfer_queue *pq,
118760bc48eSAndrew Thompson 	    struct usb_xfer *xfer);
119760bc48eSAndrew Thompson void	usb2_pipe_enter(struct usb_xfer *xfer);
120760bc48eSAndrew Thompson void	usb2_pipe_start(struct usb_xfer_queue *pq);
121760bc48eSAndrew Thompson void	usb2_transfer_dequeue(struct usb_xfer *xfer);
122e0a69b51SAndrew Thompson void	usb2_transfer_done(struct usb_xfer *xfer, usb_error_t error);
123760bc48eSAndrew Thompson void	usb2_transfer_enqueue(struct usb_xfer_queue *pq,
124760bc48eSAndrew Thompson 	    struct usb_xfer *xfer);
125760bc48eSAndrew Thompson void	usb2_transfer_setup_sub(struct usb_setup_params *parm);
126760bc48eSAndrew Thompson void	usb2_default_transfer_setup(struct usb_device *udev);
127760bc48eSAndrew Thompson void	usb2_clear_data_toggle(struct usb_device *udev,
128760bc48eSAndrew Thompson 	    struct usb_pipe *pipe);
129760bc48eSAndrew Thompson void	usb2_do_poll(struct usb_xfer **ppxfer, uint16_t max);
130e0a69b51SAndrew Thompson usb_callback_t usb2_do_request_callback;
131e0a69b51SAndrew Thompson usb_callback_t usb2_handle_request_callback;
132e0a69b51SAndrew Thompson usb_callback_t usb2_do_clear_stall_callback;
133760bc48eSAndrew Thompson void	usb2_transfer_timeout_ms(struct usb_xfer *xfer,
134e0a69b51SAndrew Thompson 	    void (*cb) (void *arg), usb_timeout_t ms);
135e0a69b51SAndrew Thompson usb_timeout_t usb2_get_dma_delay(struct usb_bus *bus);
136760bc48eSAndrew Thompson void	usb2_transfer_power_ref(struct usb_xfer *xfer, int val);
13702ac6454SAndrew Thompson 
13802ac6454SAndrew Thompson #endif					/* _USB2_TRANSFER_H_ */
139