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