VM: restore >4k secondary cache functionality
[minix.git] / lib / libusb / usb.c
blob0b3ffcdaaaf426056b26ec7053f53c8b6fac5ccb
1 #include <minix/config.h>
2 #include <minix/const.h>
3 #include <minix/usb.h>
4 #include <minix/com.h>
5 #include <minix/safecopies.h>
6 #include <minix/sysutil.h>
7 #include <minix/ds.h>
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <string.h>
12 static struct usb_urb * pending_urbs = NULL;
13 static endpoint_t hcd_ep;
15 static void _usb_urb_complete(struct usb_driver *ud, long urb_id);
17 /*****************************************************************************
18 * usb_send_urb *
19 ****************************************************************************/
20 int usb_send_urb(struct usb_urb* urb)
22 message msg;
23 int res;
24 cp_grant_id_t gid;
25 if (urb == NULL) {
26 return EINVAL;
29 if (hcd_ep == 0) {
30 return EINVAL;
33 /* setup grant */
34 gid = cpf_grant_direct(hcd_ep,(vir_bytes) &urb->dev_id,
35 urb->urb_size - sizeof(void*),CPF_WRITE|CPF_READ);
37 if (gid == -1) {
38 printf("usb_send_urb: grant failed: "
39 "cpf_grant_direct(%d,%p,%d)\n", hcd_ep, urb, urb->urb_size);
40 return EINVAL;
43 urb->gid = gid;
45 /* prepare message */
46 msg.m_type = USB_RQ_SEND_URB;
47 msg.USB_GRANT_ID = gid;
48 msg.USB_GRANT_SIZE = urb->urb_size-sizeof(void*);
50 /* send message */
51 res = sendrec(hcd_ep, &msg);
53 if (res != 0) {
54 panic("usb_send_urb: could not talk to hcd: %d", res);
57 if (msg.m_type != USB_REPLY) {
58 panic("usb_send_urb: got illegal response from hcd: %d", msg.m_type);
61 if (msg.USB_RESULT != 0) {
62 panic("usb_send_urb: hcd could not enqueue URB: %d", msg.USB_RESULT);
65 /* everything ok, add urb to pending_urbs */
66 urb->urb_id = msg.USB_URB_ID;
67 urb->next = pending_urbs;
68 pending_urbs = urb;
70 /* done. */
72 /* (The HCD will send us a message when the URB is completed.) */
74 return res;
77 /*****************************************************************************
78 * usb_cancle_urb *
79 ****************************************************************************/
80 int usb_cancle_urb(struct usb_urb* urb)
82 int res;
83 message msg;
85 if (urb == NULL) {
86 panic("usb_send_urb: urb == NULL!");
89 if (urb->urb_id == USB_INVALID_URB_ID) {
90 return EINVAL;
93 /* prepare message */
94 msg.m_type = USB_RQ_CANCEL_URB;
95 msg.USB_URB_ID = urb->urb_id;
97 /* send message */
98 res = sendrec(hcd_ep, &msg);
100 if (res != 0) {
101 panic("usb_cancle_urb: could not talk to hcd: %d", res);
104 if (msg.m_type != USB_REPLY) {
105 panic("usb_cancle_urb: got illegal response from hcd: %d", msg.m_type);
108 if (msg.USB_RESULT != 0) {
109 panic("usb_cancle_urb: got illegal response from hcd: %d", msg.m_type);
112 res = msg.USB_RESULT;
114 /* done. */
115 return res;
118 /*****************************************************************************
119 * usb_init *
120 ****************************************************************************/
121 int usb_init(char *name)
123 int res;
124 message msg;
126 /* get the endpoint of the HCD */
127 res = ds_retrieve_label_endpt("usbd", &hcd_ep);
129 if (res != 0) {
130 panic("usb_init: ds_retrieve_label_endpt failed for 'usb': %d", res);
133 msg.m_type = USB_RQ_INIT;
135 strncpy(msg.USB_RB_INIT_NAME, name, M3_LONG_STRING);
137 res = sendrec(hcd_ep, &msg);
139 if (res != 0) {
140 panic("usb_init: can't talk to USB: %d", res);
143 if (msg.m_type != USB_REPLY) {
144 panic("usb_init: bad reply from USB: %d", msg.m_type);
147 if (msg.USB_RESULT != 0 ) {
148 panic("usb_init: init failed: %d", msg.USB_RESULT);
151 return 0;
154 /*****************************************************************************
155 * _usb_urb_complete *
156 ****************************************************************************/
157 static void _usb_urb_complete(struct usb_driver *ud, long urb_id)
159 /* find the corresponding URB in the urb_pending list. */
160 struct usb_urb * urb = NULL;
161 if (pending_urbs != NULL) {
162 if (pending_urbs->urb_id == urb_id) {
163 urb = pending_urbs;
164 pending_urbs = urb->next;
165 } else {
166 struct usb_urb *u = pending_urbs;
167 while (u->next) {
168 if (u->next->urb_id == urb_id) {
169 urb = u->next;
170 u->next = u->next->next;
171 urb->next = NULL;
172 break;
174 u = u->next;
179 /* Did we find a URB? */
180 if (urb != NULL) {
181 /* revoke grant */
182 cpf_revoke(urb->gid);
183 /* call completion handler */
184 #if 0
185 dump_urb(urb);
186 #endif
187 ud->urb_completion(urb);
188 } else {
189 printf("WARN: _usb_urb_complete: did not find URB with ID %ld", urb_id);
193 /*****************************************************************************
194 * usb_handle_msg *
195 ****************************************************************************/
196 int usb_handle_msg(struct usb_driver *ud, message *msg)
199 * we expect kind of messages:
200 * - new usb device
201 * - removed device
202 * - URB completed
204 * NOTE: the hcd driver doesn't expect replies for these messages.
207 if (!ud) {
208 return -1;
211 switch(msg->m_type) {
212 case USB_COMPLETE_URB:
213 _usb_urb_complete(ud, msg->USB_URB_ID);
214 return 0;
215 case USB_ANNOUCE_DEV:
216 ud->connect_device(msg->USB_DEV_ID, msg->USB_INTERFACES);
217 return 0;
218 case USB_WITHDRAW_DEV:
219 ud->disconnect_device(msg->USB_DEV_ID);
220 return 0;
221 default:
222 panic("usb_handle_msg: bogus message from USB");