xref: /freebsd/sys/dev/usb/usb_transfer.h (revision 31ba4ce8898f9dfa5e7f054fdbc26e50a599a6e3)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _USB_TRANSFER_H_
30 #define	_USB_TRANSFER_H_
31 
32 /*
33  * A few words about USB transfer states:
34  * ======================================
35  *
36  * USB transfers can have multiple states, because they can be
37  * cancelled and started again and this cannot always be done
38  * atomically under a mutex. One reason for this is that the USB
39  * hardware sometimes needs to wait for DMA controllers to finish
40  * which is done asynchronously and grows the statemachine.
41  */
42 
43 /*
44  * The following structure defines the messages that is used to signal
45  * the "done_p" USB process.
46  */
47 struct usb_done_msg {
48 	struct usb_proc_msg hdr;
49 	struct usb_xfer_root *xroot;
50 };
51 
52 #define	USB_DMATAG_TO_XROOT(dpt)				\
53 	__containerof(dpt, struct usb_xfer_root, dma_parent_tag)
54 
55 /*
56  * The following structure is used to keep information about memory
57  * that should be automatically freed at the moment all USB transfers
58  * have been freed.
59  */
60 struct usb_xfer_root {
61 	struct usb_dma_parent_tag dma_parent_tag;
62 #if USB_HAVE_BUSDMA
63 	struct usb_xfer_queue dma_q;
64 #endif
65 	struct usb_xfer_queue done_q;
66 	struct usb_done_msg done_m[2];
67 	struct cv cv_drain;
68 
69 	struct usb_process *done_p;	/* pointer to callback process */
70 	void   *memory_base;
71 	struct mtx *xfer_mtx;	/* cannot be changed during operation */
72 #if USB_HAVE_BUSDMA
73 	struct usb_page_cache *dma_page_cache_start;
74 	struct usb_page_cache *dma_page_cache_end;
75 #endif
76 	struct usb_page_cache *xfer_page_cache_start;
77 	struct usb_page_cache *xfer_page_cache_end;
78 	struct usb_bus *bus;		/* pointer to USB bus (cached) */
79 	struct usb_device *udev;	/* pointer to USB device */
80 
81 	usb_size_t memory_size;
82 	usb_size_t setup_refcount;
83 #if USB_HAVE_BUSDMA
84 	usb_frcount_t dma_nframes;	/* number of page caches to load */
85 	usb_frcount_t dma_currframe;	/* currect page cache number */
86 	usb_frlength_t dma_frlength_0;	/* length of page cache zero */
87 	uint8_t	dma_error;		/* set if virtual memory could not be
88 					 * loaded */
89 #endif
90 	uint8_t	done_sleep;		/* set if done thread is sleeping */
91 };
92 
93 /*
94  * The following structure is used when setting up an array of USB
95  * transfers.
96  */
97 struct usb_setup_params {
98 	struct usb_dma_tag *dma_tag_p;
99 	struct usb_page *dma_page_ptr;
100 	struct usb_page_cache *dma_page_cache_ptr;	/* these will be
101 							 * auto-freed */
102 	struct usb_page_cache *xfer_page_cache_ptr;	/* these will not be
103 							 * auto-freed */
104 	struct usb_device *udev;
105 	struct usb_xfer *curr_xfer;
106 	const struct usb_config *curr_setup;
107 	const struct usb_pipe_methods *methods;
108 	void   *buf;
109 	usb_frlength_t *xfer_length_ptr;
110 
111 	usb_size_t size[7];
112 	usb_frlength_t bufsize;
113 	usb_frlength_t bufsize_max;
114 
115 	uint32_t hc_max_frame_size;
116 	uint16_t hc_max_packet_size;
117 	uint8_t	hc_max_packet_count;
118 	enum usb_dev_speed speed;
119 	uint8_t	dma_tag_max;
120 	usb_error_t err;
121 };
122 
123 /* function prototypes */
124 
125 uint8_t	usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
126 	    struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
127 	    usb_size_t count);
128 void	usb_dma_delay_done_cb(struct usb_xfer *);
129 void	usb_command_wrapper(struct usb_xfer_queue *pq,
130 	    struct usb_xfer *xfer);
131 void	usbd_pipe_enter(struct usb_xfer *xfer);
132 void	usbd_pipe_start(struct usb_xfer_queue *pq);
133 void	usbd_transfer_dequeue(struct usb_xfer *xfer);
134 void	usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error);
135 void	usbd_transfer_enqueue(struct usb_xfer_queue *pq,
136 	    struct usb_xfer *xfer);
137 void	usbd_transfer_setup_sub(struct usb_setup_params *parm);
138 void	usbd_ctrl_transfer_setup(struct usb_device *udev);
139 void	usbd_clear_stall_locked(struct usb_device *udev,
140 	    struct usb_endpoint *ep);
141 void	usbd_clear_data_toggle(struct usb_device *udev,
142 	    struct usb_endpoint *ep);
143 usb_callback_t usbd_do_request_callback;
144 usb_callback_t usb_handle_request_callback;
145 usb_callback_t usb_do_clear_stall_callback;
146 void	usbd_transfer_timeout_ms(struct usb_xfer *xfer,
147 	    void (*cb) (void *arg), usb_timeout_t ms);
148 usb_timeout_t usbd_get_dma_delay(struct usb_device *udev);
149 void	usbd_transfer_power_ref(struct usb_xfer *xfer, int val);
150 uint8_t	usbd_xfer_get_isochronous_start_frame(struct usb_xfer *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t *);
151 
152 #endif					/* _USB_TRANSFER_H_ */
153