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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#include <minix/config.h>
#include <minix/const.h>
#include <minix/usb.h>
#include <minix/com.h>
#include <minix/safecopies.h>
#include <minix/sysutil.h>
#include <minix/ds.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static struct usb_urb * pending_urbs = NULL;
static endpoint_t hcd_ep;
static void _usb_urb_complete(struct usb_driver *ud, unsigned int urb_id);
/*****************************************************************************
* usb_send_urb *
****************************************************************************/
int usb_send_urb(struct usb_urb* urb)
{
message msg;
int res;
cp_grant_id_t gid;
if (urb == NULL) {
return EINVAL;
}
if (hcd_ep == 0) {
return EINVAL;
}
/* setup grant */
gid = cpf_grant_direct(hcd_ep,(vir_bytes) &urb->dev_id,
urb->urb_size - sizeof(void*),CPF_WRITE|CPF_READ);
if (gid == -1) {
printf("usb_send_urb: grant failed: "
"cpf_grant_direct(%d,%p,%d)\n", hcd_ep, urb, urb->urb_size);
return EINVAL;
}
urb->gid = gid;
/* prepare message */
msg.m_type = USB_RQ_SEND_URB;
msg.USB_GRANT_ID = gid;
msg.USB_GRANT_SIZE = urb->urb_size-sizeof(void*);
/* send message */
res = ipc_sendrec(hcd_ep, &msg);
if (res != 0) {
panic("usb_send_urb: could not talk to hcd: %d", res);
}
if (msg.m_type != USB_REPLY) {
panic("usb_send_urb: got illegal response from hcd: %d", msg.m_type);
}
if (msg.USB_RESULT != 0) {
panic("usb_send_urb: hcd could not enqueue URB: %ld", msg.USB_RESULT);
}
/* everything ok, add urb to pending_urbs */
urb->urb_id = msg.USB_URB_ID;
urb->next = pending_urbs;
pending_urbs = urb;
/* done. */
/* (The HCD will send us a message when the URB is completed.) */
return res;
}
/*****************************************************************************
* usb_cancle_urb *
****************************************************************************/
int usb_cancle_urb(struct usb_urb* urb)
{
int res;
message msg;
if (urb == NULL) {
panic("usb_send_urb: urb == NULL!");
}
if (urb->urb_id == USB_INVALID_URB_ID) {
return EINVAL;
}
/* prepare message */
msg.m_type = USB_RQ_CANCEL_URB;
msg.USB_URB_ID = urb->urb_id;
/* send message */
res = ipc_sendrec(hcd_ep, &msg);
if (res != 0) {
panic("usb_cancle_urb: could not talk to hcd: %d", res);
}
if (msg.m_type != USB_REPLY) {
panic("usb_cancle_urb: got illegal response from hcd: %d", msg.m_type);
}
if (msg.USB_RESULT != 0) {
panic("usb_cancle_urb: got illegal response from hcd: %d", msg.m_type);
}
res = msg.USB_RESULT;
/* done. */
return res;
}
/*****************************************************************************
* usb_init *
****************************************************************************/
int usb_init(char *name)
{
int res;
message msg;
/* get the endpoint of the HCD */
res = ds_retrieve_label_endpt("usbd", &hcd_ep);
if (res != 0) {
panic("usb_init: ds_retrieve_label_endpt failed for 'usb': %d", res);
}
msg.m_type = USB_RQ_INIT;
strncpy(msg.USB_RB_INIT_NAME, name, M_PATH_STRING_MAX);
res = ipc_sendrec(hcd_ep, &msg);
if (res != 0) {
panic("usb_init: can't talk to USB: %d", res);
}
if (msg.m_type != USB_REPLY) {
panic("usb_init: bad reply from USB: %d", msg.m_type);
}
if (msg.USB_RESULT != 0 ) {
panic("usb_init: init failed: %ld", msg.USB_RESULT);
}
return 0;
}
/*****************************************************************************
* _usb_urb_complete *
****************************************************************************/
static void _usb_urb_complete(struct usb_driver *ud, unsigned int urb_id)
{
/* find the corresponding URB in the urb_pending list. */
struct usb_urb * urb = NULL;
if (pending_urbs != NULL) {
if (pending_urbs->urb_id == urb_id) {
urb = pending_urbs;
pending_urbs = urb->next;
} else {
struct usb_urb *u = pending_urbs;
while (u->next) {
if (u->next->urb_id == urb_id) {
urb = u->next;
u->next = u->next->next;
urb->next = NULL;
break;
}
u = u->next;
}
}
}
/* Did we find a URB? */
if (urb != NULL) {
/* revoke grant */
cpf_revoke(urb->gid);
/* call completion handler */
#if 0
dump_urb(urb);
#endif
ud->urb_completion(urb);
} else {
printf("WARN: _usb_urb_complete: did not find URB with ID %u",
urb_id);
}
}
/*****************************************************************************
* usb_handle_msg *
****************************************************************************/
int usb_handle_msg(struct usb_driver *ud, message *msg)
{
/*
* we expect kind of messages:
* - new usb device
* - removed device
* - URB completed
*
* NOTE: the hcd driver doesn't expect replies for these messages.
*/
if (!ud) {
return -1;
}
switch(msg->m_type) {
case USB_COMPLETE_URB:
_usb_urb_complete(ud, (unsigned int)msg->USB_URB_ID);
return 0;
case USB_ANNOUCE_DEV:
ud->connect_device(msg->USB_DEV_ID, msg->USB_INTERFACES);
return 0;
case USB_WITHDRAW_DEV:
ud->disconnect_device(msg->USB_DEV_ID);
return 0;
default:
panic("usb_handle_msg: bogus message from USB");
}
}
/*****************************************************************************
* usb_send_info *
*****************************************************************************/
int
usb_send_info(long info_type, long info_value)
{
int res;
message msg;
/* Prepare message */
msg.m_type = USB_RQ_SEND_INFO;
msg.USB_INFO_TYPE = info_type;
msg.USB_INFO_VALUE = info_value;
/* Send/receive message */
res = ipc_sendrec(hcd_ep, &msg);
if (res != 0)
panic("usb_send_info: could not talk to HCD: %d", res);
if (msg.m_type != USB_REPLY)
panic("usb_send_info: got illegal response from HCD: %d", msg.m_type);
if (msg.USB_RESULT != 0)
panic("usb_send_info: got illegal response from HCD: %d", msg.m_type);
return msg.USB_RESULT;
}
|