2 * Haiku Backend for libusb
3 * Copyright © 2014 Akshay Jaggi <akshay1994.leo@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "haiku_usb.h"
33 haiku_init(struct libusb_context *ctx)
35 if (atomic_add(&gInitCount, 1) == 0)
36 return gUsbRoster.Start();
37 return LIBUSB_SUCCESS;
43 if (atomic_add(&gInitCount, -1) == 1)
48 haiku_open(struct libusb_device_handle *dev_handle)
50 USBDevice *dev = *((USBDevice **)dev_handle->dev->os_priv);
51 USBDeviceHandle *handle = new(std::nothrow) USBDeviceHandle(dev);
53 return LIBUSB_ERROR_NO_MEM;
54 if (handle->InitCheck() == false) {
56 return LIBUSB_ERROR_NO_DEVICE;
58 *((USBDeviceHandle **)dev_handle->os_priv) = handle;
59 return LIBUSB_SUCCESS;
63 haiku_close(struct libusb_device_handle *dev_handle)
65 USBDeviceHandle *handle = *((USBDeviceHandle **)dev_handle->os_priv);
69 *((USBDeviceHandle **)dev_handle->os_priv) = NULL;
73 haiku_get_device_descriptor(struct libusb_device *device, unsigned char *buffer, int *host_endian)
75 USBDevice *dev = *((USBDevice **)device->os_priv);
76 memcpy(buffer, dev->Descriptor(), DEVICE_DESC_LENGTH);
78 return LIBUSB_SUCCESS;
82 haiku_get_active_config_descriptor(struct libusb_device *device, unsigned char *buffer, size_t len, int *host_endian)
84 USBDevice *dev = *((USBDevice **)device->os_priv);
85 const usb_configuration_descriptor *act_config = dev->ActiveConfiguration();
86 if (len > act_config->total_length)
87 return LIBUSB_ERROR_OVERFLOW;
88 memcpy(buffer, act_config, len);
90 return LIBUSB_SUCCESS;
94 haiku_get_config_descriptor(struct libusb_device *device, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
96 USBDevice *dev = *((USBDevice **)device->os_priv);
97 const usb_configuration_descriptor *config = dev->ConfigurationDescriptor(config_index);
99 usbi_err(DEVICE_CTX(device), "failed getting configuration descriptor");
100 return LIBUSB_ERROR_INVALID_PARAM;
102 if (len > config->total_length)
103 len = config->total_length;
104 memcpy(buffer, config, len);
110 haiku_set_configuration(struct libusb_device_handle *dev_handle, int config)
112 USBDeviceHandle *handle= *((USBDeviceHandle **)dev_handle->os_priv);
113 return handle->SetConfiguration(config);
117 haiku_claim_interface(struct libusb_device_handle *dev_handle, int interface_number)
119 USBDeviceHandle *handle = *((USBDeviceHandle **)dev_handle->os_priv);
120 return handle->ClaimInterface(interface_number);
124 haiku_set_altsetting(struct libusb_device_handle *dev_handle, int interface_number, int altsetting)
126 USBDeviceHandle *handle = *((USBDeviceHandle **)dev_handle->os_priv);
127 return handle->SetAltSetting(interface_number, altsetting);
131 haiku_release_interface(struct libusb_device_handle *dev_handle, int interface_number)
133 USBDeviceHandle *handle = *((USBDeviceHandle **)dev_handle->os_priv);
134 haiku_set_altsetting(dev_handle,interface_number, 0);
135 return handle->ReleaseInterface(interface_number);
139 haiku_submit_transfer(struct usbi_transfer *itransfer)
141 struct libusb_transfer *fLibusbTransfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
142 USBDeviceHandle *fDeviceHandle = *((USBDeviceHandle **)fLibusbTransfer->dev_handle->os_priv);
143 return fDeviceHandle->SubmitTransfer(itransfer);
147 haiku_cancel_transfer(struct usbi_transfer *itransfer)
149 struct libusb_transfer *fLibusbTransfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
150 USBDeviceHandle *fDeviceHandle = *((USBDeviceHandle **)fLibusbTransfer->dev_handle->os_priv);
151 return fDeviceHandle->CancelTransfer(*((USBTransfer **)usbi_transfer_get_os_priv(itransfer)));
155 haiku_clear_transfer_priv(struct usbi_transfer *itransfer)
157 USBTransfer *transfer = *((USBTransfer **)usbi_transfer_get_os_priv(itransfer));
159 *((USBTransfer **)usbi_transfer_get_os_priv(itransfer)) = NULL;
163 haiku_handle_transfer_completion(struct usbi_transfer *itransfer)
165 USBTransfer *transfer = *((USBTransfer **)usbi_transfer_get_os_priv(itransfer));
167 usbi_mutex_lock(&itransfer->lock);
168 if (transfer->IsCancelled()) {
170 *((USBTransfer **)usbi_transfer_get_os_priv(itransfer)) = NULL;
171 usbi_mutex_unlock(&itransfer->lock);
172 if (itransfer->transferred < 0)
173 itransfer->transferred = 0;
174 return usbi_handle_transfer_cancellation(itransfer);
176 libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
177 if (itransfer->transferred < 0) {
178 usbi_err(ITRANSFER_CTX(itransfer), "error in transfer");
179 status = LIBUSB_TRANSFER_ERROR;
180 itransfer->transferred = 0;
183 *((USBTransfer **)usbi_transfer_get_os_priv(itransfer)) = NULL;
184 usbi_mutex_unlock(&itransfer->lock);
185 return usbi_handle_transfer_completion(itransfer, status);
189 haiku_clock_gettime(int clkid, struct timespec *tp)
191 if (clkid == USBI_CLOCK_REALTIME)
192 return clock_gettime(CLOCK_REALTIME, tp);
193 if (clkid == USBI_CLOCK_MONOTONIC)
194 return clock_gettime(CLOCK_MONOTONIC, tp);
195 return LIBUSB_ERROR_INVALID_PARAM;
198 const struct usbi_os_backend usbi_backend = {
199 /*.name =*/ "Haiku usbfs",
201 /*.init =*/ haiku_init,
202 /*.exit =*/ haiku_exit,
203 /*.get_device_list =*/ NULL,
204 /*.hotplug_poll =*/ NULL,
205 /*.open =*/ haiku_open,
206 /*.close =*/ haiku_close,
207 /*.get_device_descriptor =*/ haiku_get_device_descriptor,
208 /*.get_active_config_descriptor =*/ haiku_get_active_config_descriptor,
209 /*.get_config_descriptor =*/ haiku_get_config_descriptor,
210 /*.get_config_descriptor_by_value =*/ NULL,
213 /*.get_configuration =*/ NULL,
214 /*.set_configuration =*/ haiku_set_configuration,
215 /*.claim_interface =*/ haiku_claim_interface,
216 /*.release_interface =*/ haiku_release_interface,
218 /*.set_interface_altsetting =*/ haiku_set_altsetting,
219 /*.clear_halt =*/ NULL,
220 /*.reset_device =*/ NULL,
222 /*.alloc_streams =*/ NULL,
223 /*.free_streams =*/ NULL,
225 /*.dev_mem_alloc =*/ NULL,
226 /*.dev_mem_free =*/ NULL,
228 /*.kernel_driver_active =*/ NULL,
229 /*.detach_kernel_driver =*/ NULL,
230 /*.attach_kernel_driver =*/ NULL,
232 /*.destroy_device =*/ NULL,
234 /*.submit_transfer =*/ haiku_submit_transfer,
235 /*.cancel_transfer =*/ haiku_cancel_transfer,
236 /*.clear_transfer_priv =*/ haiku_clear_transfer_priv,
238 /*.handle_events =*/ NULL,
239 /*.handle_transfer_completion =*/ haiku_handle_transfer_completion,
241 /*.clock_gettime =*/ haiku_clock_gettime,
243 #ifdef USBI_TIMERFD_AVAILABLE
244 /*.get_timerfd_clockid =*/ NULL,
247 /*.context_priv_size=*/ 0,
248 /*.device_priv_size =*/ sizeof(USBDevice *),
249 /*.device_handle_priv_size =*/ sizeof(USBDeviceHandle *),
250 /*.transfer_priv_size =*/ sizeof(USBTransfer *),