xref: /freebsd/sys/dev/usb/usb_transfer.h (revision b1f9167f94059fd55c630891d359bcff987bd7eb)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _USB_TRANSFER_H_
28 #define	_USB_TRANSFER_H_
29 
30 /*
31  * Definition of internal USB transfer states:
32  * ===========================================
33  *
34  * The main reason there are many USB states is that we are allowed to
35  * cancel USB transfers, then start the USB transfer again and that
36  * this state transaction cannot always be done in a single atomic
37  * operation without blocking the calling thread. One reason for this
38  * is that the USB hardware sometimes needs to wait for DMA
39  * controllers to finish which is done asynchronously and grows the
40  * statemachine.
41  *
42  * When extending the following statemachine there are basically two
43  * things you should think about: Which states should be executed or
44  * modified in case of USB transfer stop and which states should be
45  * executed or modified in case of USB transfer start. Also respect
46  * the "can_cancel_immed" flag which basically tells if you can go
47  * directly from a wait state to the cancelling states.
48  */
49 
50 enum {
51 	/* XFER start execute state */
52 
53 	/* USB_ST_SETUP = 0 (already defined) */
54 
55 	/* XFER transferred execute state */
56 
57 	/* USB_ST_TRANSFERRED = 1 (already defined) */
58 
59 	/* XFER error execute state */
60 
61 	/* USB_ST_ERROR = 2 (already defined) */
62 
63 	/* XFER restart after error execute state */
64 
65 	USB_ST_RESTART = 8,
66 
67 	/* XFER transfer idle state */
68 
69 	USB_ST_WAIT_SETUP,
70 
71 	/* Other XFER execute states */
72 
73 	USB_ST_PIPE_OPEN = 16,
74 	USB_ST_PIPE_OPEN_ERROR,
75 	USB_ST_PIPE_OPEN_RESTART,
76 
77 	USB_ST_BDMA_LOAD,
78 	USB_ST_BDMA_LOAD_ERROR,
79 	USB_ST_BDMA_LOAD_RESTART,
80 
81 	USB_ST_IVAL_DLY,
82 	USB_ST_IVAL_DLY_ERROR,
83 	USB_ST_IVAL_DLY_RESTART,
84 
85 	USB_ST_PIPE_STALL,
86 	USB_ST_PIPE_STALL_ERROR,
87 	USB_ST_PIPE_STALL_RESTART,
88 
89 	USB_ST_ENTER,
90 	USB_ST_ENTER_ERROR,
91 	USB_ST_ENTER_RESTART,
92 
93 	USB_ST_START,
94 	USB_ST_START_ERROR,
95 	USB_ST_START_RESTART,
96 
97 	USB_ST_PIPE_CLOSE,
98 	USB_ST_PIPE_CLOSE_ERROR,
99 	USB_ST_PIPE_CLOSE_RESTART,
100 
101 	USB_ST_BDMA_DLY,
102 	USB_ST_BDMA_DLY_ERROR,
103 	USB_ST_BDMA_DLY_RESTART,
104 
105 	/* XFER transfer wait states */
106 
107 	USB_ST_WAIT_PIPE_OPEN = 64,
108 	USB_ST_WAIT_PIPE_OPEN_ERROR,
109 	USB_ST_WAIT_PIPE_OPEN_RESTART,
110 
111 	USB_ST_WAIT_BDMA_LOAD,
112 	USB_ST_WAIT_BDMA_LOAD_ERROR,
113 	USB_ST_WAIT_BDMA_LOAD_RESTART,
114 
115 	USB_ST_WAIT_IVAL_DLY,
116 	USB_ST_WAIT_IVAL_DLY_ERROR,
117 	USB_ST_WAIT_IVAL_DLY_RESTART,
118 
119 	USB_ST_WAIT_PIPE_STALL,
120 	USB_ST_WAIT_PIPE_STALL_ERROR,
121 	USB_ST_WAIT_PIPE_STALL_RESTART,
122 
123 	USB_ST_WAIT_ENTER,
124 	USB_ST_WAIT_ENTER_ERROR,
125 	USB_ST_WAIT_ENTER_RESTART,
126 
127 	USB_ST_WAIT_START,
128 	USB_ST_WAIT_START_ERROR,
129 	USB_ST_WAIT_START_RESTART,
130 
131 	USB_ST_WAIT_PIPE_CLOSE,
132 	USB_ST_WAIT_PIPE_CLOSE_ERROR,
133 	USB_ST_WAIT_PIPE_CLOSE_RESTART,
134 
135 	USB_ST_WAIT_BDMA_DLY,
136 	USB_ST_WAIT_BDMA_DLY_ERROR,
137 	USB_ST_WAIT_BDMA_DLY_RESTART,
138 
139 	USB_ST_WAIT_TRANSFERRED,
140 	USB_ST_WAIT_TRANSFERRED_ERROR,
141 	USB_ST_WAIT_TRANSFERRED_RESTART,
142 };
143 
144 /*
145  * The following structure defines the messages that is used to signal
146  * the "done_p" USB process.
147  */
148 struct usb_done_msg {
149 	struct usb_proc_msg hdr;
150 	struct usb_xfer_root *xroot;
151 };
152 
153 #define	USB_DMATAG_TO_XROOT(dpt)				\
154   ((struct usb_xfer_root *)(					\
155    ((uint8_t *)(dpt)) -						\
156    ((uint8_t *)&((struct usb_xfer_root *)0)->dma_parent_tag)))
157 
158 /*
159  * The following structure is used to keep information about memory
160  * that should be automatically freed at the moment all USB transfers
161  * have been freed.
162  */
163 struct usb_xfer_root {
164 	struct usb_dma_parent_tag dma_parent_tag;
165 #if USB_HAVE_BUSDMA
166 	struct usb_xfer_queue dma_q;
167 #endif
168 	struct usb_xfer_queue done_q;
169 	struct usb_done_msg done_m[2];
170 	struct cv cv_drain;
171 
172 	struct usb_process *done_p;	/* pointer to callback process */
173 	void   *memory_base;
174 	struct mtx *xfer_mtx;	/* cannot be changed during operation */
175 #if USB_HAVE_BUSDMA
176 	struct usb_page_cache *dma_page_cache_start;
177 	struct usb_page_cache *dma_page_cache_end;
178 #endif
179 	struct usb_page_cache *xfer_page_cache_start;
180 	struct usb_page_cache *xfer_page_cache_end;
181 	struct usb_bus *bus;		/* pointer to USB bus (cached) */
182 	struct usb_device *udev;	/* pointer to USB device */
183 
184 	usb_size_t memory_size;
185 	usb_size_t setup_refcount;
186 #if USB_HAVE_BUSDMA
187 	usb_frcount_t dma_nframes;	/* number of page caches to load */
188 	usb_frcount_t dma_currframe;	/* currect page cache number */
189 	usb_frlength_t dma_frlength_0;	/* length of page cache zero */
190 	uint8_t	dma_error;		/* set if virtual memory could not be
191 					 * loaded */
192 #endif
193 	uint8_t	done_sleep;		/* set if done thread is sleeping */
194 };
195 
196 /*
197  * The following structure is used when setting up an array of USB
198  * transfers.
199  */
200 struct usb_setup_params {
201 	struct usb_dma_tag *dma_tag_p;
202 	struct usb_page *dma_page_ptr;
203 	struct usb_page_cache *dma_page_cache_ptr;	/* these will be
204 							 * auto-freed */
205 	struct usb_page_cache *xfer_page_cache_ptr;	/* these will not be
206 							 * auto-freed */
207 	struct usb_device *udev;
208 	struct usb_xfer *curr_xfer;
209 	const struct usb_config *curr_setup;
210 	const struct usb_pipe_methods *methods;
211 	void   *buf;
212 	usb_frlength_t *xfer_length_ptr;
213 
214 	usb_size_t size[7];
215 	usb_frlength_t bufsize;
216 	usb_frlength_t bufsize_max;
217 
218 	uint32_t hc_max_frame_size;
219 	uint16_t hc_max_packet_size;
220 	uint8_t	hc_max_packet_count;
221 	enum usb_dev_speed speed;
222 	uint8_t	dma_tag_max;
223 	usb_error_t err;
224 };
225 
226 /* function prototypes */
227 
228 uint8_t	usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
229 	    struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
230 	    usb_size_t count);
231 void	usb_dma_delay_done_cb(struct usb_xfer *);
232 void	usb_command_wrapper(struct usb_xfer_queue *pq,
233 	    struct usb_xfer *xfer);
234 void	usbd_pipe_enter(struct usb_xfer *xfer);
235 void	usbd_pipe_start(struct usb_xfer_queue *pq);
236 void	usbd_transfer_dequeue(struct usb_xfer *xfer);
237 void	usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error);
238 void	usbd_transfer_enqueue(struct usb_xfer_queue *pq,
239 	    struct usb_xfer *xfer);
240 void	usbd_transfer_setup_sub(struct usb_setup_params *parm);
241 void	usbd_ctrl_transfer_setup(struct usb_device *udev);
242 void	usbd_clear_stall_locked(struct usb_device *udev,
243 	    struct usb_endpoint *ep);
244 void	usbd_clear_data_toggle(struct usb_device *udev,
245 	    struct usb_endpoint *ep);
246 usb_callback_t usbd_do_request_callback;
247 usb_callback_t usb_handle_request_callback;
248 usb_callback_t usb_do_clear_stall_callback;
249 void	usbd_transfer_timeout_ms(struct usb_xfer *xfer,
250 	    void (*cb) (void *arg), usb_timeout_t ms);
251 usb_timeout_t usbd_get_dma_delay(struct usb_device *udev);
252 void	usbd_transfer_power_ref(struct usb_xfer *xfer, int val);
253 
254 #endif					/* _USB_TRANSFER_H_ */
255