summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb2/core/usb2_sw_transfer.c
blob: 23f89bafc228470d636d11d2735645fc6708c08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* $FreeBSD$ */
/*-
 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <dev/usb2/include/usb2_mfunc.h>
#include <dev/usb2/include/usb2_standard.h>
#include <dev/usb2/include/usb2_error.h>
#include <dev/usb2/include/usb2_defs.h>

#define	USB_DEBUG_VAR usb2_debug

#include <dev/usb2/core/usb2_core.h>
#include <dev/usb2/core/usb2_process.h>
#include <dev/usb2/core/usb2_busdma.h>
#include <dev/usb2/core/usb2_transfer.h>
#include <dev/usb2/core/usb2_sw_transfer.h>
#include <dev/usb2/core/usb2_device.h>
#include <dev/usb2/core/usb2_debug.h>

#include <dev/usb2/controller/usb2_controller.h>
#include <dev/usb2/controller/usb2_bus.h>

/*------------------------------------------------------------------------*
 *	usb2_sw_transfer - factored out code
 *
 * This function is basically used for the Virtual Root HUB, and can
 * emulate control, bulk and interrupt endpoints. Data is exchanged
 * using the "std->ptr" and "std->len" fields, that allows kernel
 * virtual memory to be transferred. All state is kept in the
 * structure pointed to by the "std" argument passed to this
 * function. The "func" argument points to a function that is called
 * back in the various states, so that the application using this
 * function can get a chance to select the outcome. The "func"
 * function is allowed to sleep, exiting all mutexes. If this function
 * will sleep the "enter" and "start" methods must be marked
 * non-cancelable, hence there is no extra cancelled checking in this
 * function.
 *------------------------------------------------------------------------*/
void
usb2_sw_transfer(struct usb2_sw_transfer *std,
    usb2_sw_transfer_func_t *func)
{
	struct usb2_xfer *xfer;
	uint32_t len;
	uint8_t shortpkt = 0;

	xfer = std->xfer;
	if (xfer == NULL) {
		/* the transfer is gone */
		DPRINTF("xfer gone\n");
		return;
	}
	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);

	std->xfer = NULL;

	/* check for control transfer */
	if (xfer->flags_int.control_xfr) {
		/* check if we are transferring the SETUP packet */
		if (xfer->flags_int.control_hdr) {

			/* copy out the USB request */

			if (xfer->frlengths[0] == sizeof(std->req)) {
				usb2_copy_out(xfer->frbuffers, 0,
				    &std->req, sizeof(std->req));
			} else {
				std->err = USB_ERR_INVAL;
				goto done;
			}

			xfer->aframes = 1;

			std->err = 0;
			std->state = USB_SW_TR_SETUP;

			(func) (xfer, std);

			if (std->err) {
				goto done;
			}
		} else {
			/* skip the first frame in this case */
			xfer->aframes = 1;
		}
	}
	std->err = 0;
	std->state = USB_SW_TR_PRE_DATA;

	(func) (xfer, std);

	if (std->err) {
		goto done;
	}
	/* Transfer data. Iterate accross all frames. */
	while (xfer->aframes != xfer->nframes) {

		len = xfer->frlengths[xfer->aframes];

		if (len > std->len) {
			len = std->len;
			shortpkt = 1;
		}
		if (len > 0) {
			if ((xfer->endpoint & (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN) {
				usb2_copy_in(xfer->frbuffers + xfer->aframes, 0,
				    std->ptr, len);
			} else {
				usb2_copy_out(xfer->frbuffers + xfer->aframes, 0,
				    std->ptr, len);
			}
		}
		std->ptr += len;
		std->len -= len;
		xfer->frlengths[xfer->aframes] = len;
		xfer->aframes++;

		if (shortpkt) {
			break;
		}
	}

	std->err = 0;
	std->state = USB_SW_TR_POST_DATA;

	(func) (xfer, std);

	if (std->err) {
		goto done;
	}
	/* check if the control transfer is complete */
	if (xfer->flags_int.control_xfr &&
	    !xfer->flags_int.control_act) {

		std->err = 0;
		std->state = USB_SW_TR_STATUS;

		(func) (xfer, std);

		if (std->err) {
			goto done;
		}
	}
done:
	DPRINTF("done err=%s\n", usb2_errstr(std->err));
	std->state = USB_SW_TR_PRE_CALLBACK;
	(func) (xfer, std);
}
OpenPOWER on IntegriCloud