1184610Salfred/* $FreeBSD$ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27194230Sthompsa#ifndef _USB_TRANSFER_H_
28194230Sthompsa#define	_USB_TRANSFER_H_
29184610Salfred
30184610Salfred/*
31228056Shselasky * Definition of internal USB transfer states:
32228056Shselasky * ===========================================
33228056Shselasky *
34228056Shselasky * The main reason there are many USB states is that we are allowed to
35228056Shselasky * cancel USB transfers, then start the USB transfer again and that
36228056Shselasky * this state transaction cannot always be done in a single atomic
37228056Shselasky * operation without blocking the calling thread. One reason for this
38228056Shselasky * is that the USB hardware sometimes needs to wait for DMA
39228056Shselasky * controllers to finish which is done asynchronously and grows the
40228056Shselasky * statemachine.
41228056Shselasky *
42228056Shselasky * When extending the following statemachine there are basically two
43228056Shselasky * things you should think about: Which states should be executed or
44228056Shselasky * modified in case of USB transfer stop and which states should be
45228056Shselasky * executed or modified in case of USB transfer start. Also respect
46228056Shselasky * the "can_cancel_immed" flag which basically tells if you can go
47228056Shselasky * directly from a wait state to the cancelling states.
48228056Shselasky */
49228056Shselasky
50228056Shselaskyenum {
51228056Shselasky	/* XFER start execute state */
52228056Shselasky
53228056Shselasky	/* USB_ST_SETUP = 0 (already defined) */
54228056Shselasky
55228056Shselasky	/* XFER transferred execute state */
56228056Shselasky
57228056Shselasky	/* USB_ST_TRANSFERRED = 1 (already defined) */
58228056Shselasky
59228056Shselasky	/* XFER error execute state */
60228056Shselasky
61228056Shselasky	/* USB_ST_ERROR = 2 (already defined) */
62228056Shselasky
63228056Shselasky	/* XFER restart after error execute state */
64228056Shselasky
65228056Shselasky	USB_ST_RESTART = 8,
66228056Shselasky
67228056Shselasky	/* XFER transfer idle state */
68228056Shselasky
69228056Shselasky	USB_ST_WAIT_SETUP,
70228056Shselasky
71228056Shselasky	/* Other XFER execute states */
72228056Shselasky
73228056Shselasky	USB_ST_PIPE_OPEN = 16,
74228056Shselasky	USB_ST_PIPE_OPEN_ERROR,
75228056Shselasky	USB_ST_PIPE_OPEN_RESTART,
76228056Shselasky
77228056Shselasky	USB_ST_BDMA_LOAD,
78228056Shselasky	USB_ST_BDMA_LOAD_ERROR,
79228056Shselasky	USB_ST_BDMA_LOAD_RESTART,
80228056Shselasky
81228056Shselasky	USB_ST_IVAL_DLY,
82228056Shselasky	USB_ST_IVAL_DLY_ERROR,
83228056Shselasky	USB_ST_IVAL_DLY_RESTART,
84228056Shselasky
85228056Shselasky	USB_ST_PIPE_STALL,
86228056Shselasky	USB_ST_PIPE_STALL_ERROR,
87228056Shselasky	USB_ST_PIPE_STALL_RESTART,
88228056Shselasky
89228056Shselasky	USB_ST_ENTER,
90228056Shselasky	USB_ST_ENTER_ERROR,
91228056Shselasky	USB_ST_ENTER_RESTART,
92228056Shselasky
93228056Shselasky	USB_ST_START,
94228056Shselasky	USB_ST_START_ERROR,
95228056Shselasky	USB_ST_START_RESTART,
96228056Shselasky
97228056Shselasky	USB_ST_PIPE_CLOSE,
98228056Shselasky	USB_ST_PIPE_CLOSE_ERROR,
99228056Shselasky	USB_ST_PIPE_CLOSE_RESTART,
100228056Shselasky
101228056Shselasky	USB_ST_BDMA_DLY,
102228056Shselasky	USB_ST_BDMA_DLY_ERROR,
103228056Shselasky	USB_ST_BDMA_DLY_RESTART,
104228056Shselasky
105228056Shselasky	/* XFER transfer wait states */
106228056Shselasky
107228056Shselasky	USB_ST_WAIT_PIPE_OPEN = 64,
108228056Shselasky	USB_ST_WAIT_PIPE_OPEN_ERROR,
109228056Shselasky	USB_ST_WAIT_PIPE_OPEN_RESTART,
110228056Shselasky
111228056Shselasky	USB_ST_WAIT_BDMA_LOAD,
112228056Shselasky	USB_ST_WAIT_BDMA_LOAD_ERROR,
113228056Shselasky	USB_ST_WAIT_BDMA_LOAD_RESTART,
114228056Shselasky
115228056Shselasky	USB_ST_WAIT_IVAL_DLY,
116228056Shselasky	USB_ST_WAIT_IVAL_DLY_ERROR,
117228056Shselasky	USB_ST_WAIT_IVAL_DLY_RESTART,
118228056Shselasky
119228056Shselasky	USB_ST_WAIT_PIPE_STALL,
120228056Shselasky	USB_ST_WAIT_PIPE_STALL_ERROR,
121228056Shselasky	USB_ST_WAIT_PIPE_STALL_RESTART,
122228056Shselasky
123228056Shselasky	USB_ST_WAIT_ENTER,
124228056Shselasky	USB_ST_WAIT_ENTER_ERROR,
125228056Shselasky	USB_ST_WAIT_ENTER_RESTART,
126228056Shselasky
127228056Shselasky	USB_ST_WAIT_START,
128228056Shselasky	USB_ST_WAIT_START_ERROR,
129228056Shselasky	USB_ST_WAIT_START_RESTART,
130228056Shselasky
131228056Shselasky	USB_ST_WAIT_PIPE_CLOSE,
132228056Shselasky	USB_ST_WAIT_PIPE_CLOSE_ERROR,
133228056Shselasky	USB_ST_WAIT_PIPE_CLOSE_RESTART,
134228056Shselasky
135228056Shselasky	USB_ST_WAIT_BDMA_DLY,
136228056Shselasky	USB_ST_WAIT_BDMA_DLY_ERROR,
137228056Shselasky	USB_ST_WAIT_BDMA_DLY_RESTART,
138228056Shselasky
139228056Shselasky	USB_ST_WAIT_TRANSFERRED,
140228056Shselasky	USB_ST_WAIT_TRANSFERRED_ERROR,
141228056Shselasky	USB_ST_WAIT_TRANSFERRED_RESTART,
142228056Shselasky};
143228056Shselasky
144228056Shselasky/*
145184610Salfred * The following structure defines the messages that is used to signal
146184610Salfred * the "done_p" USB process.
147184610Salfred */
148192984Sthompsastruct usb_done_msg {
149192984Sthompsa	struct usb_proc_msg hdr;
150192984Sthompsa	struct usb_xfer_root *xroot;
151184610Salfred};
152184610Salfred
153190180Sthompsa#define	USB_DMATAG_TO_XROOT(dpt)				\
154192984Sthompsa  ((struct usb_xfer_root *)(					\
155190180Sthompsa   ((uint8_t *)(dpt)) -						\
156192984Sthompsa   ((uint8_t *)&((struct usb_xfer_root *)0)->dma_parent_tag)))
157190180Sthompsa
158184610Salfred/*
159184610Salfred * The following structure is used to keep information about memory
160184610Salfred * that should be automatically freed at the moment all USB transfers
161184610Salfred * have been freed.
162184610Salfred */
163192984Sthompsastruct usb_xfer_root {
164192984Sthompsa	struct usb_dma_parent_tag dma_parent_tag;
165190180Sthompsa#if USB_HAVE_BUSDMA
166192984Sthompsa	struct usb_xfer_queue dma_q;
167190180Sthompsa#endif
168192984Sthompsa	struct usb_xfer_queue done_q;
169192984Sthompsa	struct usb_done_msg done_m[2];
170184610Salfred	struct cv cv_drain;
171184610Salfred
172192984Sthompsa	struct usb_process *done_p;	/* pointer to callback process */
173184610Salfred	void   *memory_base;
174187173Sthompsa	struct mtx *xfer_mtx;	/* cannot be changed during operation */
175190180Sthompsa#if USB_HAVE_BUSDMA
176192984Sthompsa	struct usb_page_cache *dma_page_cache_start;
177192984Sthompsa	struct usb_page_cache *dma_page_cache_end;
178190180Sthompsa#endif
179192984Sthompsa	struct usb_page_cache *xfer_page_cache_start;
180192984Sthompsa	struct usb_page_cache *xfer_page_cache_end;
181192984Sthompsa	struct usb_bus *bus;		/* pointer to USB bus (cached) */
182192984Sthompsa	struct usb_device *udev;	/* pointer to USB device */
183184610Salfred
184193074Sthompsa	usb_size_t memory_size;
185193074Sthompsa	usb_size_t setup_refcount;
186190181Sthompsa#if USB_HAVE_BUSDMA
187193045Sthompsa	usb_frcount_t dma_nframes;	/* number of page caches to load */
188193045Sthompsa	usb_frcount_t dma_currframe;	/* currect page cache number */
189193045Sthompsa	usb_frlength_t dma_frlength_0;	/* length of page cache zero */
190184610Salfred	uint8_t	dma_error;		/* set if virtual memory could not be
191184610Salfred					 * loaded */
192190181Sthompsa#endif
193184610Salfred	uint8_t	done_sleep;		/* set if done thread is sleeping */
194184610Salfred};
195184610Salfred
196184610Salfred/*
197184610Salfred * The following structure is used when setting up an array of USB
198184610Salfred * transfers.
199184610Salfred */
200192984Sthompsastruct usb_setup_params {
201192984Sthompsa	struct usb_dma_tag *dma_tag_p;
202192984Sthompsa	struct usb_page *dma_page_ptr;
203192984Sthompsa	struct usb_page_cache *dma_page_cache_ptr;	/* these will be
204184610Salfred							 * auto-freed */
205192984Sthompsa	struct usb_page_cache *xfer_page_cache_ptr;	/* these will not be
206184610Salfred							 * auto-freed */
207192984Sthompsa	struct usb_device *udev;
208192984Sthompsa	struct usb_xfer *curr_xfer;
209192984Sthompsa	const struct usb_config *curr_setup;
210192984Sthompsa	const struct usb_pipe_methods *methods;
211184610Salfred	void   *buf;
212193045Sthompsa	usb_frlength_t *xfer_length_ptr;
213184610Salfred
214193074Sthompsa	usb_size_t size[7];
215193045Sthompsa	usb_frlength_t bufsize;
216193045Sthompsa	usb_frlength_t bufsize_max;
217184610Salfred
218213435Shselasky	uint32_t hc_max_frame_size;
219184610Salfred	uint16_t hc_max_packet_size;
220184610Salfred	uint8_t	hc_max_packet_count;
221192500Sthompsa	enum usb_dev_speed speed;
222184610Salfred	uint8_t	dma_tag_max;
223193045Sthompsa	usb_error_t err;
224184610Salfred};
225184610Salfred
226184610Salfred/* function prototypes */
227184610Salfred
228194228Sthompsauint8_t	usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
229193074Sthompsa	    struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
230193074Sthompsa	    usb_size_t count);
231213435Shselaskyvoid	usb_dma_delay_done_cb(struct usb_xfer *);
232194228Sthompsavoid	usb_command_wrapper(struct usb_xfer_queue *pq,
233192984Sthompsa	    struct usb_xfer *xfer);
234194228Sthompsavoid	usbd_pipe_enter(struct usb_xfer *xfer);
235194228Sthompsavoid	usbd_pipe_start(struct usb_xfer_queue *pq);
236194228Sthompsavoid	usbd_transfer_dequeue(struct usb_xfer *xfer);
237194228Sthompsavoid	usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error);
238194228Sthompsavoid	usbd_transfer_enqueue(struct usb_xfer_queue *pq,
239192984Sthompsa	    struct usb_xfer *xfer);
240194228Sthompsavoid	usbd_transfer_setup_sub(struct usb_setup_params *parm);
241207080Sthompsavoid	usbd_ctrl_transfer_setup(struct usb_device *udev);
242213435Shselaskyvoid	usbd_clear_stall_locked(struct usb_device *udev,
243213435Shselasky	    struct usb_endpoint *ep);
244194228Sthompsavoid	usbd_clear_data_toggle(struct usb_device *udev,
245193644Sthompsa	    struct usb_endpoint *ep);
246194228Sthompsausb_callback_t usbd_do_request_callback;
247194228Sthompsausb_callback_t usb_handle_request_callback;
248194228Sthompsausb_callback_t usb_do_clear_stall_callback;
249194228Sthompsavoid	usbd_transfer_timeout_ms(struct usb_xfer *xfer,
250193045Sthompsa	    void (*cb) (void *arg), usb_timeout_t ms);
251212134Sthompsausb_timeout_t usbd_get_dma_delay(struct usb_device *udev);
252194228Sthompsavoid	usbd_transfer_power_ref(struct usb_xfer *xfer, int val);
253184610Salfred
254194230Sthompsa#endif					/* _USB_TRANSFER_H_ */
255