Added new calls to libusb
[AROS.git] / rom / usb / vusbhc / vusbhci_bridge.c
blob374fce8adfacf8fa395f1ead419473c0e81be541
1 /*
2 Copyright © 2015, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Virtual USB host controller
6 Lang: English
7 */
9 #ifdef DEBUG
10 #undef DEBUG
11 #endif
12 #define DEBUG 1
14 #include <aros/debug.h>
16 #include <proto/exec.h>
17 #include <proto/hostlib.h>
19 #include <devices/usb.h>
20 #include <devices/usb_hub.h>
22 #include "vusbhci_device.h"
23 #include "vusbhci_bridge.h"
25 APTR HostLibBase;
26 struct libusb_func libusb_func;
28 static void *libusbhandle;
30 static libusb_device_handle *dev_handle = NULL;
32 void ctrl_callback_handler(struct libusb_transfer *transfer) {
34 struct IOUsbHWReq *ioreq = transfer->user_data;
35 struct VUSBHCIUnit *unit = (struct VUSBHCIUnit *) ioreq->iouh_Req.io_Unit;
37 mybug_unit(-1, ("ctrl_callback_handler!\n\n"));
40 int hotplug_callback_event_handler(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) {
42 struct VUSBHCIBase *VUSBHCIBase = (struct VUSBHCIBase *)user_data;
43 struct VUSBHCIUnit *unit = VUSBHCIBase->usbunit200;
45 struct libusb_device_descriptor desc;
46 int rc, speed;
48 mybug_unit(-1, ("Hotplug callback event!\n"));
50 switch(event) {
52 case LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED:
53 mybug_unit(-1, ("- Device attached\n"));
55 if(unit->allocated && (!dev_handle)) {
56 rc = LIBUSBCALL(libusb_get_device_descriptor, dev, &desc);
57 if (LIBUSB_SUCCESS != rc) {
58 mybug_unit(-1, ("Failed to read device descriptor\n"));
59 return 0;
60 } else {
61 mybug_unit(-1, ("Device attach: %04x:%04x\n", desc.idVendor, desc.idProduct));
63 rc = LIBUSBCALL(libusb_open, dev, &dev_handle);
64 if(dev_handle) {
65 LIBUSBCALL(libusb_set_auto_detach_kernel_driver, dev_handle, 1);
66 LIBUSBCALL(libusb_set_configuration, dev_handle, 1);
67 LIBUSBCALL(libusb_claim_interface, dev_handle, 0);
69 speed = LIBUSBCALL(libusb_get_device_speed, dev);
70 switch(speed) {
71 case LIBUSB_SPEED_LOW:
72 unit->roothub.portstatus.wPortStatus |= AROS_WORD2LE(UPSF_PORT_LOW_SPEED);
73 break;
74 case LIBUSB_SPEED_FULL:
75 unit->roothub.portstatus.wPortStatus &= ~(AROS_WORD2LE(UPSF_PORT_HIGH_SPEED)|AROS_WORD2LE(UPSF_PORT_LOW_SPEED));
76 break;
77 case LIBUSB_SPEED_HIGH:
78 unit->roothub.portstatus.wPortStatus |= AROS_WORD2LE(UPSF_PORT_HIGH_SPEED);
79 break;
80 //case LIBUSB_SPEED_SUPER:
81 //break;
84 unit->roothub.portstatus.wPortStatus |= AROS_WORD2LE(UPSF_PORT_CONNECTION);
85 unit->roothub.portstatus.wPortChange |= AROS_WORD2LE(UPSF_PORT_CONNECTION);
87 uhwCheckRootHubChanges(unit);
88 } else {
89 if(rc == LIBUSB_ERROR_ACCESS) {
90 mybug_unit(-1, ("libusb_open, access error, try running as superuser\n\n"));
95 break;
97 case LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT:
98 mybug_unit(-1, (" - Device detached\n"));
100 if(unit->allocated) {
102 unit->roothub.portstatus.wPortStatus &= ~UPSF_PORT_CONNECTION;
103 unit->roothub.portstatus.wPortChange |= UPSF_PORT_CONNECTION;
105 uhwCheckRootHubChanges(unit);
108 if(dev_handle != NULL) {
109 LIBUSBCALL(libusb_close, dev_handle);
110 dev_handle = NULL;
113 break;
115 default:
116 mybug_unit(-1, (" - Unknown event arrived\n"));
117 break;
121 return 0;
124 void *hostlib_load_so(const char *sofile, const char **names, int nfuncs, void **funcptr) {
125 void *handle;
126 char *err;
127 int i;
129 if ((handle = HostLib_Open(sofile, &err)) == NULL) {
130 (bug("[LIBUSB] failed to open '%s': %s\n", sofile, err));
131 return NULL;
132 }else{
133 bug("[LIBUSB] opened '%s'\n", sofile);
136 for (i = 0; i < nfuncs; i++) {
137 funcptr[i] = HostLib_GetPointer(handle, names[i], &err);
138 if (err != NULL) {
139 bug("[LIBUSB] failed to get symbol '%s' (%s)\n", names[i], err);
140 HostLib_Close(handle, NULL);
141 return NULL;
142 }else{
143 bug("[LIBUSB] managed to get symbol '%s'\n", names[i]);
147 return handle;
150 BOOL libusb_bridge_init(struct VUSBHCIBase *VUSBHCIBase) {
152 int rc;
154 HostLibBase = OpenResource("hostlib.resource");
156 if (!HostLibBase)
157 return FALSE;
159 libusbhandle = hostlib_load_so("libusb.so", libusb_func_names, LIBUSB_NUM_FUNCS, (void **)&libusb_func);
161 if (!libusbhandle)
162 return FALSE;
164 if(!LIBUSBCALL(libusb_init, NULL)) {
165 LIBUSBCALL(libusb_set_debug, NULL, 1);
166 bug("[LIBUSB] Checking hotplug support of libusb\n");
167 if (LIBUSBCALL(libusb_has_capability, LIBUSB_CAP_HAS_HOTPLUG)) {
168 bug("[LIBUSB] - Hotplug supported\n");
170 rc = (LIBUSBCALL(libusb_hotplug_register_callback,
171 NULL,
172 (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED|LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
174 LIBUSB_HOTPLUG_MATCH_ANY,
175 LIBUSB_HOTPLUG_MATCH_ANY,
176 LIBUSB_HOTPLUG_MATCH_ANY,
177 hotplug_callback_event_handler,
178 (void *)VUSBHCIBase,
179 NULL)
182 if(rc == LIBUSB_SUCCESS) {
183 bug("[LIBUSB] - Hotplug callback installed rc = %d\n", rc);
184 return TRUE;
187 bug("[LIBUSB] - Hotplug callback installation failure! rc = %d\n", rc);
189 } else {
190 bug("[LIBUSB] - Hotplug not supported, failing...\n");
191 LIBUSBCALL(libusb_exit, NULL);
193 libusb_bridge_cleanup();
196 return FALSE;
199 VOID libusb_bridge_cleanup() {
200 HostLib_Close(libusbhandle, NULL);
203 void call_libusb_event_handler() {
204 LIBUSBCALL(libusb_handle_events, NULL);
208 FIXME: libusb expects buffer to precede with enough space for setup data (8 bytes or LIBUSB_CONTROL_SETUP_SIZE)
209 - Copy buffer need to be used
211 int do_libusb_ctrl_transfer(struct IOUsbHWReq *ioreq) {
212 struct VUSBHCIUnit *unit = (struct VUSBHCIUnit *) ioreq->iouh_Req.io_Unit;
214 int rc;
216 UWORD bmRequestType = (ioreq->iouh_SetupData.bmRequestType);
217 UWORD bRequest = (ioreq->iouh_SetupData.bRequest);
218 UWORD wValue = (ioreq->iouh_SetupData.wValue);
219 UWORD wIndex = (ioreq->iouh_SetupData.wIndex);
220 UWORD wLength = (ioreq->iouh_SetupData.wLength);
222 switch(((ULONG)ioreq->iouh_SetupData.bmRequestType<<16)|((ULONG)ioreq->iouh_SetupData.bRequest)) {
223 case ((((URTF_OUT|URTF_STANDARD|URTF_DEVICE))<<16)|(USR_SET_ADDRESS)):
224 mybug_unit(-1, ("Filtering out SET_ADDRESS\n\n"));
225 ioreq->iouh_Actual = ioreq->iouh_Length;
226 return UHIOERR_NO_ERROR;
227 break;
230 mybug_unit(-1, ("wLength %d\n", wLength));
231 mybug_unit(-1, ("ioreq->iouh_Length %d\n", ioreq->iouh_Length));
233 rc = LIBUSBCALL(libusb_control_transfer, dev_handle, bmRequestType, bRequest, wValue, wIndex, ioreq->iouh_Data, ioreq->iouh_Length, 10);
235 if(rc<0) {
236 rc = 0;
239 ioreq->iouh_Actual = rc;
241 mybug_unit(-1, ("Done!\n\n"));
242 return UHIOERR_NO_ERROR;
246 FIXME: libusb expects buffer to precede with enough space for setup data (8 bytes or LIBUSB_CONTROL_SETUP_SIZE)
247 - Copy buffer need to be used
249 int do_libusb_intr_transfer(struct IOUsbHWReq *ioreq) {
250 struct VUSBHCIUnit *unit = (struct VUSBHCIUnit *) ioreq->iouh_Req.io_Unit;
252 int rc;
254 UWORD wLength = AROS_WORD2LE(ioreq->iouh_SetupData.wLength);
256 mybug_unit(-1, ("wLength %d\n", wLength));
257 mybug_unit(-1, ("ioreq->iouh_Length %d\n", ioreq->iouh_Length));
259 rc = LIBUSBCALL(libusb_interrupt_transfer, dev_handle, ioreq->iouh_Endpoint, ioreq->iouh_Data, wLength, &ioreq->iouh_Actual, 10);
260 mybug_unit(-1, ("libusb_interrupt_transfer rc = %d\n\n", rc));
262 mybug_unit(-1, ("Done!\n\n"));
263 return UHIOERR_NO_ERROR;
266 int do_libusb_bulk_transfer(struct IOUsbHWReq *ioreq) {
267 struct VUSBHCIUnit *unit = (struct VUSBHCIUnit *) ioreq->iouh_Req.io_Unit;
269 int rc, transferred = 0, i;
270 APTR buffer;
271 UBYTE endpoint = ioreq->iouh_Endpoint;
273 UWORD wLength = AROS_WORD2LE(ioreq->iouh_SetupData.wLength);
275 mybug_unit(-1, ("wLength %d\n", wLength));
276 mybug_unit(-1, ("ioreq->iouh_Length %d\n", ioreq->iouh_Length));
278 // if( (ioreq->iouh_SetupData.bmRequestType && URTF_IN) ) {
280 This is a hack for massstorage with EP1 being IN and EP2 being OUT, will mess everuthing else
281 for some reason above code tries always OUT transfer, check why
283 if(endpoint == 1) {
284 mybug_unit(-1, ("ioreq->iouh_Endpoint %d (IN)\n", endpoint));
285 rc = LIBUSBCALL(libusb_bulk_transfer, dev_handle, (endpoint|LIBUSB_ENDPOINT_IN), (UBYTE *)ioreq->iouh_Data, ioreq->iouh_Length, &transferred, 0);
287 buffer = ioreq->iouh_Data;
289 mybug_unit(-1, ("Bulk data buffer in:\n"));
290 for(i = 0;i < ioreq->iouh_Length; i++) {
291 if(i%8 == 0)
292 bug("\n");
294 bug("%02x ", *(UBYTE *)buffer++ );
296 bug("\n\n");
297 } else {
298 /* LIBUSB_ENDPOINT_OUT = 0*/
299 mybug_unit(-1, ("ioreq->iouh_Endpoint %d (OUT)\n", endpoint));
301 buffer = ioreq->iouh_Data;
303 mybug_unit(-1, ("Bulk data buffer out:\n"));
304 for(i = 0;i < ioreq->iouh_Length; i++) {
305 if(i%8 == 0)
306 bug("\n");
308 bug("%02x ", *(UBYTE *)buffer++ );
310 bug("\n\n");
312 rc = LIBUSBCALL(libusb_bulk_transfer, dev_handle, (endpoint|LIBUSB_ENDPOINT_OUT), (UBYTE *)ioreq->iouh_Data, ioreq->iouh_Length, &transferred, 0);
315 mybug_unit(-1, ("libusb_bulk_transfer rc = %d, transferred %d\n", rc, transferred));
318 0 on success (and populates transferred)
319 LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates transferred)
320 LIBUSB_ERROR_PIPE if the endpoint halted
321 LIBUSB_ERROR_OVERFLOW if the device offered more data, see Packets and overflows
322 LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
323 another LIBUSB_ERROR code on other failures
325 if(rc<0) {
326 rc = 0;
327 } else {
328 if(transferred) {
329 ioreq->iouh_Actual = transferred;
333 mybug_unit(-1, ("Done!\n\n"));
334 return UHIOERR_NO_ERROR;
337 int do_libusb_isoc_transfer(struct IOUsbHWReq *ioreq) {
338 struct VUSBHCIUnit *unit = (struct VUSBHCIUnit *) ioreq->iouh_Req.io_Unit;
340 //UWORD bmRequestType = (ioreq->iouh_SetupData.bmRequestType) & (URTF_STANDARD | URTF_CLASS | URTF_VENDOR);
341 //UWORD bmRequestDirection = (ioreq->iouh_SetupData.bmRequestType) & (URTF_IN | URTF_OUT);
342 //UWORD bmRequestRecipient = (ioreq->iouh_SetupData.bmRequestType) & (URTF_DEVICE | URTF_INTERFACE | URTF_ENDPOINT | URTF_OTHER);
344 //UWORD bRequest = (ioreq->iouh_SetupData.bRequest);
345 //UWORD wValue = AROS_WORD2LE(ioreq->iouh_SetupData.wValue);
346 //UWORD wIndex = AROS_WORD2LE(ioreq->iouh_SetupData.wIndex);
347 //UWORD wLength = AROS_WORD2LE(ioreq->iouh_SetupData.wLength);
349 mybug_unit(-1, ("Done!\n\n"));
350 return 0;