xref: /freebsd/sys/dev/usb/usb_transfer.h (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
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  * Definition of internal USB transfer states:
34  * ===========================================
35  *
36  * The main reason there are many USB states is that we are allowed to
37  * cancel USB transfers, then start the USB transfer again and that
38  * this state transaction cannot always be done in a single atomic
39  * operation without blocking the calling thread. One reason for this
40  * is that the USB hardware sometimes needs to wait for DMA
41  * controllers to finish which is done asynchronously and grows the
42  * statemachine.
43  *
44  * When extending the following statemachine there are basically two
45  * things you should think about: Which states should be executed or
46  * modified in case of USB transfer stop and which states should be
47  * executed or modified in case of USB transfer start. Also respect
48  * the "can_cancel_immed" flag which basically tells if you can go
49  * directly from a wait state to the cancelling states.
50  */
51 
52 enum {
53 	/* XFER start execute state */
54 
55 	/* USB_ST_SETUP = 0 (already defined) */
56 
57 	/* XFER transferred execute state */
58 
59 	/* USB_ST_TRANSFERRED = 1 (already defined) */
60 
61 	/* XFER error execute state */
62 
63 	/* USB_ST_ERROR = 2 (already defined) */
64 
65 	/* XFER restart after error execute state */
66 
67 	USB_ST_RESTART = 8,
68 
69 	/* XFER transfer idle state */
70 
71 	USB_ST_WAIT_SETUP,
72 
73 	/* Other XFER execute states */
74 
75 	USB_ST_PIPE_OPEN = 16,
76 	USB_ST_PIPE_OPEN_ERROR,
77 	USB_ST_PIPE_OPEN_RESTART,
78 
79 	USB_ST_BDMA_LOAD,
80 	USB_ST_BDMA_LOAD_ERROR,
81 	USB_ST_BDMA_LOAD_RESTART,
82 
83 	USB_ST_IVAL_DLY,
84 	USB_ST_IVAL_DLY_ERROR,
85 	USB_ST_IVAL_DLY_RESTART,
86 
87 	USB_ST_PIPE_STALL,
88 	USB_ST_PIPE_STALL_ERROR,
89 	USB_ST_PIPE_STALL_RESTART,
90 
91 	USB_ST_ENTER,
92 	USB_ST_ENTER_ERROR,
93 	USB_ST_ENTER_RESTART,
94 
95 	USB_ST_START,
96 	USB_ST_START_ERROR,
97 	USB_ST_START_RESTART,
98 
99 	USB_ST_PIPE_CLOSE,
100 	USB_ST_PIPE_CLOSE_ERROR,
101 	USB_ST_PIPE_CLOSE_RESTART,
102 
103 	USB_ST_BDMA_DLY,
104 	USB_ST_BDMA_DLY_ERROR,
105 	USB_ST_BDMA_DLY_RESTART,
106 
107 	/* XFER transfer wait states */
108 
109 	USB_ST_WAIT_PIPE_OPEN = 64,
110 	USB_ST_WAIT_PIPE_OPEN_ERROR,
111 	USB_ST_WAIT_PIPE_OPEN_RESTART,
112 
113 	USB_ST_WAIT_BDMA_LOAD,
114 	USB_ST_WAIT_BDMA_LOAD_ERROR,
115 	USB_ST_WAIT_BDMA_LOAD_RESTART,
116 
117 	USB_ST_WAIT_IVAL_DLY,
118 	USB_ST_WAIT_IVAL_DLY_ERROR,
119 	USB_ST_WAIT_IVAL_DLY_RESTART,
120 
121 	USB_ST_WAIT_PIPE_STALL,
122 	USB_ST_WAIT_PIPE_STALL_ERROR,
123 	USB_ST_WAIT_PIPE_STALL_RESTART,
124 
125 	USB_ST_WAIT_ENTER,
126 	USB_ST_WAIT_ENTER_ERROR,
127 	USB_ST_WAIT_ENTER_RESTART,
128 
129 	USB_ST_WAIT_START,
130 	USB_ST_WAIT_START_ERROR,
131 	USB_ST_WAIT_START_RESTART,
132 
133 	USB_ST_WAIT_PIPE_CLOSE,
134 	USB_ST_WAIT_PIPE_CLOSE_ERROR,
135 	USB_ST_WAIT_PIPE_CLOSE_RESTART,
136 
137 	USB_ST_WAIT_BDMA_DLY,
138 	USB_ST_WAIT_BDMA_DLY_ERROR,
139 	USB_ST_WAIT_BDMA_DLY_RESTART,
140 
141 	USB_ST_WAIT_TRANSFERRED,
142 	USB_ST_WAIT_TRANSFERRED_ERROR,
143 	USB_ST_WAIT_TRANSFERRED_RESTART,
144 };
145 
146 /*
147  * The following structure defines the messages that is used to signal
148  * the "done_p" USB process.
149  */
150 struct usb_done_msg {
151 	struct usb_proc_msg hdr;
152 	struct usb_xfer_root *xroot;
153 };
154 
155 #define	USB_DMATAG_TO_XROOT(dpt)				\
156   ((struct usb_xfer_root *)(					\
157    ((uint8_t *)(dpt)) -						\
158    ((uint8_t *)&((struct usb_xfer_root *)0)->dma_parent_tag)))
159 
160 /*
161  * The following structure is used to keep information about memory
162  * that should be automatically freed at the moment all USB transfers
163  * have been freed.
164  */
165 struct usb_xfer_root {
166 	struct usb_dma_parent_tag dma_parent_tag;
167 #if USB_HAVE_BUSDMA
168 	struct usb_xfer_queue dma_q;
169 #endif
170 	struct usb_xfer_queue done_q;
171 	struct usb_done_msg done_m[2];
172 	struct cv cv_drain;
173 
174 	struct usb_process *done_p;	/* pointer to callback process */
175 	void   *memory_base;
176 	struct mtx *xfer_mtx;	/* cannot be changed during operation */
177 #if USB_HAVE_BUSDMA
178 	struct usb_page_cache *dma_page_cache_start;
179 	struct usb_page_cache *dma_page_cache_end;
180 #endif
181 	struct usb_page_cache *xfer_page_cache_start;
182 	struct usb_page_cache *xfer_page_cache_end;
183 	struct usb_bus *bus;		/* pointer to USB bus (cached) */
184 	struct usb_device *udev;	/* pointer to USB device */
185 
186 	usb_size_t memory_size;
187 	usb_size_t setup_refcount;
188 #if USB_HAVE_BUSDMA
189 	usb_frcount_t dma_nframes;	/* number of page caches to load */
190 	usb_frcount_t dma_currframe;	/* currect page cache number */
191 	usb_frlength_t dma_frlength_0;	/* length of page cache zero */
192 	uint8_t	dma_error;		/* set if virtual memory could not be
193 					 * loaded */
194 #endif
195 	uint8_t	done_sleep;		/* set if done thread is sleeping */
196 };
197 
198 /*
199  * The following structure is used when setting up an array of USB
200  * transfers.
201  */
202 struct usb_setup_params {
203 	struct usb_dma_tag *dma_tag_p;
204 	struct usb_page *dma_page_ptr;
205 	struct usb_page_cache *dma_page_cache_ptr;	/* these will be
206 							 * auto-freed */
207 	struct usb_page_cache *xfer_page_cache_ptr;	/* these will not be
208 							 * auto-freed */
209 	struct usb_device *udev;
210 	struct usb_xfer *curr_xfer;
211 	const struct usb_config *curr_setup;
212 	const struct usb_pipe_methods *methods;
213 	void   *buf;
214 	usb_frlength_t *xfer_length_ptr;
215 
216 	usb_size_t size[7];
217 	usb_frlength_t bufsize;
218 	usb_frlength_t bufsize_max;
219 
220 	uint32_t hc_max_frame_size;
221 	uint16_t hc_max_packet_size;
222 	uint8_t	hc_max_packet_count;
223 	enum usb_dev_speed speed;
224 	uint8_t	dma_tag_max;
225 	usb_error_t err;
226 };
227 
228 /* function prototypes */
229 
230 uint8_t	usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
231 	    struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
232 	    usb_size_t count);
233 void	usb_dma_delay_done_cb(struct usb_xfer *);
234 void	usb_command_wrapper(struct usb_xfer_queue *pq,
235 	    struct usb_xfer *xfer);
236 void	usbd_pipe_enter(struct usb_xfer *xfer);
237 void	usbd_pipe_start(struct usb_xfer_queue *pq);
238 void	usbd_transfer_dequeue(struct usb_xfer *xfer);
239 void	usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error);
240 void	usbd_transfer_enqueue(struct usb_xfer_queue *pq,
241 	    struct usb_xfer *xfer);
242 void	usbd_transfer_setup_sub(struct usb_setup_params *parm);
243 void	usbd_ctrl_transfer_setup(struct usb_device *udev);
244 void	usbd_clear_stall_locked(struct usb_device *udev,
245 	    struct usb_endpoint *ep);
246 void	usbd_clear_data_toggle(struct usb_device *udev,
247 	    struct usb_endpoint *ep);
248 usb_callback_t usbd_do_request_callback;
249 usb_callback_t usb_handle_request_callback;
250 usb_callback_t usb_do_clear_stall_callback;
251 void	usbd_transfer_timeout_ms(struct usb_xfer *xfer,
252 	    void (*cb) (void *arg), usb_timeout_t ms);
253 usb_timeout_t usbd_get_dma_delay(struct usb_device *udev);
254 void	usbd_transfer_power_ref(struct usb_xfer *xfer, int val);
255 
256 #endif					/* _USB_TRANSFER_H_ */
257