2 * Copyright © 2011 Martin Pieuchot <mpi@openbsd.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <sys/types.h>
29 #include <dev/usb/usb.h>
38 unsigned char *cdesc
; /* active config descriptor */
39 usb_device_descriptor_t ddesc
; /* usb device descriptor */
43 int pipe
[2]; /* for event notification */
44 int endpoints
[USB_MAX_ENDPOINTS
];
50 static int obsd_get_device_list(struct libusb_context
*,
51 struct discovered_devs
**);
52 static int obsd_open(struct libusb_device_handle
*);
53 static void obsd_close(struct libusb_device_handle
*);
55 static int obsd_get_device_descriptor(struct libusb_device
*, unsigned char *,
57 static int obsd_get_active_config_descriptor(struct libusb_device
*,
58 unsigned char *, size_t, int *);
59 static int obsd_get_config_descriptor(struct libusb_device
*, uint8_t,
60 unsigned char *, size_t, int *);
62 static int obsd_get_configuration(struct libusb_device_handle
*, int *);
63 static int obsd_set_configuration(struct libusb_device_handle
*, int);
65 static int obsd_claim_interface(struct libusb_device_handle
*, int);
66 static int obsd_release_interface(struct libusb_device_handle
*, int);
68 static int obsd_set_interface_altsetting(struct libusb_device_handle
*, int,
70 static int obsd_clear_halt(struct libusb_device_handle
*, unsigned char);
71 static int obsd_reset_device(struct libusb_device_handle
*);
72 static void obsd_destroy_device(struct libusb_device
*);
74 static int obsd_submit_transfer(struct usbi_transfer
*);
75 static int obsd_cancel_transfer(struct usbi_transfer
*);
76 static void obsd_clear_transfer_priv(struct usbi_transfer
*);
77 static int obsd_handle_events(struct libusb_context
*ctx
, struct pollfd
*,
79 static int obsd_clock_gettime(int, struct timespec
*);
84 static int _errno_to_libusb(int);
85 static int _cache_active_config_descriptor(struct libusb_device
*, int);
86 static int _sync_control_transfer(struct usbi_transfer
*);
87 static int _sync_gen_transfer(struct usbi_transfer
*);
88 static int _access_endpoint(struct libusb_transfer
*);
90 const struct usbi_os_backend openbsd_backend
= {
91 "Synchronous OpenBSD backend",
96 NULL
, /* hotplug_poll */
100 obsd_get_device_descriptor
,
101 obsd_get_active_config_descriptor
,
102 obsd_get_config_descriptor
,
103 NULL
, /* get_config_descriptor_by_value() */
105 obsd_get_configuration
,
106 obsd_set_configuration
,
108 obsd_claim_interface
,
109 obsd_release_interface
,
111 obsd_set_interface_altsetting
,
115 NULL
, /* kernel_driver_active() */
116 NULL
, /* detach_kernel_driver() */
117 NULL
, /* attach_kernel_driver() */
121 obsd_submit_transfer
,
122 obsd_cancel_transfer
,
123 obsd_clear_transfer_priv
,
128 sizeof(struct device_priv
),
129 sizeof(struct handle_priv
),
130 0, /* transfer_priv_size */
131 0, /* add_iso_packet_size */
135 obsd_get_device_list(struct libusb_context
* ctx
,
136 struct discovered_devs
**discdevs
)
138 struct libusb_device
*dev
;
139 struct device_priv
*dpriv
;
140 struct usb_device_info di
;
141 unsigned long session_id
;
147 /* Only ugen(4) is supported */
148 for (i
= 0; i
< USB_MAX_DEVICES
; i
++) {
149 /* Control endpoint is always .00 */
150 snprintf(devnode
, sizeof(devnode
), "/dev/ugen%d.00", i
);
152 if ((fd
= open(devnode
, O_RDONLY
)) < 0) {
153 if (errno
!= ENOENT
&& errno
!= ENXIO
)
154 usbi_err(ctx
, "could not open %s", devnode
);
158 if (ioctl(fd
, USB_GET_DEVICEINFO
, &di
) < 0)
161 session_id
= (di
.udi_bus
<< 8 | di
.udi_addr
);
162 dev
= usbi_get_device_by_session_id(ctx
, session_id
);
165 dev
= libusb_ref_device(dev
);
167 dev
= usbi_alloc_device(ctx
, session_id
);
169 return (LIBUSB_ERROR_NO_MEM
);
171 dev
->bus_number
= di
.udi_bus
;
172 dev
->device_address
= di
.udi_addr
;
173 dev
->speed
= di
.udi_speed
;
175 dpriv
= (struct device_priv
*)dev
->os_priv
;
176 strlcpy(dpriv
->devnode
, devnode
, sizeof(devnode
));
179 if (ioctl(fd
, USB_GET_DEVICE_DESC
, &dpriv
->ddesc
) < 0) {
185 if (_cache_active_config_descriptor(dev
, fd
)) {
190 if ((err
= usbi_sanitize_device(dev
)))
195 if (discovered_devs_append(*discdevs
, dev
) == NULL
)
196 return (LIBUSB_ERROR_NO_MEM
);
198 libusb_unref_device(dev
);
201 return (LIBUSB_SUCCESS
);
205 libusb_unref_device(dev
);
206 return _errno_to_libusb(err
);
210 obsd_open(struct libusb_device_handle
*handle
)
212 struct handle_priv
*hpriv
= (struct handle_priv
*)handle
->os_priv
;
213 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
215 dpriv
->fd
= open(dpriv
->devnode
, O_RDWR
);
217 dpriv
->fd
= open(dpriv
->devnode
, O_RDONLY
);
219 return _errno_to_libusb(errno
);
222 usbi_dbg("open %s: fd %d", dpriv
->devnode
, dpriv
->fd
);
224 if (pipe(hpriv
->pipe
) < 0)
225 return _errno_to_libusb(errno
);
227 return usbi_add_pollfd(HANDLE_CTX(handle
), hpriv
->pipe
[0], POLLIN
);
231 obsd_close(struct libusb_device_handle
*handle
)
233 struct handle_priv
*hpriv
= (struct handle_priv
*)handle
->os_priv
;
234 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
236 usbi_dbg("close: fd %d", dpriv
->fd
);
241 usbi_remove_pollfd(HANDLE_CTX(handle
), hpriv
->pipe
[0]);
243 close(hpriv
->pipe
[0]);
244 close(hpriv
->pipe
[1]);
248 obsd_get_device_descriptor(struct libusb_device
*dev
, unsigned char *buf
,
251 struct device_priv
*dpriv
= (struct device_priv
*)dev
->os_priv
;
255 memcpy(buf
, &dpriv
->ddesc
, DEVICE_DESC_LENGTH
);
259 return (LIBUSB_SUCCESS
);
263 obsd_get_active_config_descriptor(struct libusb_device
*dev
,
264 unsigned char *buf
, size_t len
, int *host_endian
)
266 struct device_priv
*dpriv
= (struct device_priv
*)dev
->os_priv
;
267 usb_config_descriptor_t
*ucd
;
269 ucd
= (usb_config_descriptor_t
*) dpriv
->cdesc
;
270 len
= MIN(len
, UGETW(ucd
->wTotalLength
));
272 usbi_dbg("len %d", len
);
274 memcpy(buf
, dpriv
->cdesc
, len
);
282 obsd_get_config_descriptor(struct libusb_device
*dev
, uint8_t idx
,
283 unsigned char *buf
, size_t len
, int *host_endian
)
285 struct device_priv
*dpriv
= (struct device_priv
*)dev
->os_priv
;
286 struct usb_full_desc ufd
;
289 usbi_dbg("index %d, len %d", idx
, len
);
291 /* A config descriptor may be requested before opening the device */
292 if (dpriv
->fd
>= 0) {
295 fd
= open(dpriv
->devnode
, O_RDONLY
);
297 return _errno_to_libusb(errno
);
300 ufd
.ufd_config_index
= idx
;
304 if ((ioctl(fd
, USB_GET_FULL_DESC
, &ufd
)) < 0) {
308 return _errno_to_libusb(err
);
320 obsd_get_configuration(struct libusb_device_handle
*handle
, int *config
)
322 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
326 if (ioctl(dpriv
->fd
, USB_GET_CONFIG
, config
) < 0)
327 return _errno_to_libusb(errno
);
329 usbi_dbg("configuration %d", *config
);
331 return (LIBUSB_SUCCESS
);
335 obsd_set_configuration(struct libusb_device_handle
*handle
, int config
)
337 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
339 usbi_dbg("configuration %d", config
);
341 if (ioctl(dpriv
->fd
, USB_SET_CONFIG
, &config
) < 0)
342 return _errno_to_libusb(errno
);
344 return _cache_active_config_descriptor(handle
->dev
, dpriv
->fd
);
348 obsd_claim_interface(struct libusb_device_handle
*handle
, int iface
)
350 struct handle_priv
*hpriv
= (struct handle_priv
*)handle
->os_priv
;
353 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
354 hpriv
->endpoints
[i
] = -1;
356 return (LIBUSB_SUCCESS
);
360 obsd_release_interface(struct libusb_device_handle
*handle
, int iface
)
362 struct handle_priv
*hpriv
= (struct handle_priv
*)handle
->os_priv
;
365 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
366 if (hpriv
->endpoints
[i
] >= 0)
367 close(hpriv
->endpoints
[i
]);
369 return (LIBUSB_SUCCESS
);
373 obsd_set_interface_altsetting(struct libusb_device_handle
*handle
, int iface
,
376 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
377 struct usb_alt_interface intf
;
379 usbi_dbg("iface %d, setting %d", iface
, altsetting
);
381 memset(&intf
, 0, sizeof(intf
));
383 intf
.uai_interface_index
= iface
;
384 intf
.uai_alt_no
= altsetting
;
386 if (ioctl(dpriv
->fd
, USB_SET_ALTINTERFACE
, &intf
) < 0)
387 return _errno_to_libusb(errno
);
389 return (LIBUSB_SUCCESS
);
393 obsd_clear_halt(struct libusb_device_handle
*handle
, unsigned char endpoint
)
395 struct device_priv
*dpriv
= (struct device_priv
*)handle
->dev
->os_priv
;
396 struct usb_ctl_request req
;
400 req
.ucr_request
.bmRequestType
= UT_WRITE_ENDPOINT
;
401 req
.ucr_request
.bRequest
= UR_CLEAR_FEATURE
;
402 USETW(req
.ucr_request
.wValue
, UF_ENDPOINT_HALT
);
403 USETW(req
.ucr_request
.wIndex
, endpoint
);
404 USETW(req
.ucr_request
.wLength
, 0);
406 if (ioctl(dpriv
->fd
, USB_DO_REQUEST
, &req
) < 0)
407 return _errno_to_libusb(errno
);
409 return (LIBUSB_SUCCESS
);
413 obsd_reset_device(struct libusb_device_handle
*handle
)
417 return (LIBUSB_ERROR_NOT_SUPPORTED
);
421 obsd_destroy_device(struct libusb_device
*dev
)
423 struct device_priv
*dpriv
= (struct device_priv
*)dev
->os_priv
;
431 obsd_submit_transfer(struct usbi_transfer
*itransfer
)
433 struct libusb_transfer
*transfer
;
434 struct handle_priv
*hpriv
;
439 transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
440 hpriv
= (struct handle_priv
*)transfer
->dev_handle
->os_priv
;
442 switch (transfer
->type
) {
443 case LIBUSB_TRANSFER_TYPE_CONTROL
:
444 err
= _sync_control_transfer(itransfer
);
446 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
:
447 if (IS_XFEROUT(transfer
)) {
448 /* Isochronous write is not supported */
449 err
= LIBUSB_ERROR_NOT_SUPPORTED
;
452 err
= _sync_gen_transfer(itransfer
);
454 case LIBUSB_TRANSFER_TYPE_BULK
:
455 case LIBUSB_TRANSFER_TYPE_INTERRUPT
:
456 if (IS_XFEROUT(transfer
) &&
457 transfer
->flags
& LIBUSB_TRANSFER_ADD_ZERO_PACKET
) {
458 err
= LIBUSB_ERROR_NOT_SUPPORTED
;
461 err
= _sync_gen_transfer(itransfer
);
468 if (write(hpriv
->pipe
[1], &itransfer
, sizeof(itransfer
)) < 0)
469 return _errno_to_libusb(errno
);
471 return (LIBUSB_SUCCESS
);
475 obsd_cancel_transfer(struct usbi_transfer
*itransfer
)
479 return (LIBUSB_ERROR_NOT_SUPPORTED
);
483 obsd_clear_transfer_priv(struct usbi_transfer
*itransfer
)
491 obsd_handle_events(struct libusb_context
*ctx
, struct pollfd
*fds
, nfds_t nfds
,
494 struct libusb_device_handle
*handle
;
495 struct handle_priv
*hpriv
= NULL
;
496 struct usbi_transfer
*itransfer
;
497 struct pollfd
*pollfd
;
502 pthread_mutex_lock(&ctx
->open_devs_lock
);
503 for (i
= 0; i
< nfds
&& num_ready
> 0; i
++) {
506 if (!pollfd
->revents
)
511 list_for_each_entry(handle
, &ctx
->open_devs
, list
,
512 struct libusb_device_handle
) {
513 hpriv
= (struct handle_priv
*)handle
->os_priv
;
515 if (hpriv
->pipe
[0] == pollfd
->fd
)
522 usbi_dbg("fd %d is not an event pipe!", pollfd
->fd
);
527 if (pollfd
->revents
& POLLERR
) {
528 usbi_remove_pollfd(HANDLE_CTX(handle
), hpriv
->pipe
[0]);
529 usbi_handle_disconnect(handle
);
533 if (read(hpriv
->pipe
[0], &itransfer
, sizeof(itransfer
)) < 0) {
538 if ((err
= usbi_handle_transfer_completion(itransfer
,
539 LIBUSB_TRANSFER_COMPLETED
)))
542 pthread_mutex_unlock(&ctx
->open_devs_lock
);
545 return _errno_to_libusb(err
);
547 return (LIBUSB_SUCCESS
);
551 obsd_clock_gettime(int clkid
, struct timespec
*tp
)
553 usbi_dbg("clock %d", clkid
);
555 if (clkid
== USBI_CLOCK_REALTIME
)
556 return clock_gettime(CLOCK_REALTIME
, tp
);
558 if (clkid
== USBI_CLOCK_MONOTONIC
)
559 return clock_gettime(CLOCK_MONOTONIC
, tp
);
561 return (LIBUSB_ERROR_INVALID_PARAM
);
565 _errno_to_libusb(int err
)
569 return (LIBUSB_ERROR_IO
);
571 return (LIBUSB_ERROR_ACCESS
);
573 return (LIBUSB_ERROR_NO_DEVICE
);
575 return (LIBUSB_ERROR_NO_MEM
);
578 usbi_dbg("error: %s", strerror(err
));
580 return (LIBUSB_ERROR_OTHER
);
584 _cache_active_config_descriptor(struct libusb_device
*dev
, int fd
)
586 struct device_priv
*dpriv
= (struct device_priv
*)dev
->os_priv
;
587 struct usb_config_desc ucd
;
588 struct usb_full_desc ufd
;
592 usbi_dbg("fd %d", fd
);
594 ucd
.ucd_config_index
= USB_CURRENT_CONFIG_INDEX
;
596 if ((ioctl(fd
, USB_GET_CONFIG_DESC
, &ucd
)) < 0)
597 return _errno_to_libusb(errno
);
599 usbi_dbg("active bLength %d", ucd
.ucd_desc
.bLength
);
601 len
= UGETW(ucd
.ucd_desc
.wTotalLength
);
604 return (LIBUSB_ERROR_NO_MEM
);
606 ufd
.ufd_config_index
= ucd
.ucd_config_index
;
610 usbi_dbg("index %d, len %d", ufd
.ufd_config_index
, len
);
612 if ((ioctl(fd
, USB_GET_FULL_DESC
, &ufd
)) < 0) {
614 return _errno_to_libusb(errno
);
625 _sync_control_transfer(struct usbi_transfer
*itransfer
)
627 struct libusb_transfer
*transfer
;
628 struct libusb_control_setup
*setup
;
629 struct device_priv
*dpriv
;
630 struct usb_ctl_request req
;
632 transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
633 dpriv
= (struct device_priv
*)transfer
->dev_handle
->dev
->os_priv
;
634 setup
= (struct libusb_control_setup
*)transfer
->buffer
;
636 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
637 setup
->bmRequestType
, setup
->bRequest
,
638 libusb_le16_to_cpu(setup
->wValue
),
639 libusb_le16_to_cpu(setup
->wIndex
),
640 libusb_le16_to_cpu(setup
->wLength
), transfer
->timeout
);
642 req
.ucr_request
.bmRequestType
= setup
->bmRequestType
;
643 req
.ucr_request
.bRequest
= setup
->bRequest
;
644 /* Don't use USETW, libusbx already deals with the endianness */
645 (*(uint16_t *)req
.ucr_request
.wValue
) = setup
->wValue
;
646 (*(uint16_t *)req
.ucr_request
.wIndex
) = setup
->wIndex
;
647 (*(uint16_t *)req
.ucr_request
.wLength
) = setup
->wLength
;
648 req
.ucr_data
= transfer
->buffer
+ LIBUSB_CONTROL_SETUP_SIZE
;
650 if ((transfer
->flags
& LIBUSB_TRANSFER_SHORT_NOT_OK
) == 0)
651 req
.ucr_flags
= USBD_SHORT_XFER_OK
;
653 if ((ioctl(dpriv
->fd
, USB_SET_TIMEOUT
, &transfer
->timeout
)) < 0)
654 return _errno_to_libusb(errno
);
656 if ((ioctl(dpriv
->fd
, USB_DO_REQUEST
, &req
)) < 0)
657 return _errno_to_libusb(errno
);
659 itransfer
->transferred
= req
.ucr_actlen
;
661 usbi_dbg("transferred %d", itransfer
->transferred
);
667 _access_endpoint(struct libusb_transfer
*transfer
)
669 struct handle_priv
*hpriv
;
670 struct device_priv
*dpriv
;
671 char *s
, devnode
[16];
675 hpriv
= (struct handle_priv
*)transfer
->dev_handle
->os_priv
;
676 dpriv
= (struct device_priv
*)transfer
->dev_handle
->dev
->os_priv
;
678 endpt
= UE_GET_ADDR(transfer
->endpoint
);
679 mode
= IS_XFERIN(transfer
) ? O_RDONLY
: O_WRONLY
;
681 usbi_dbg("endpoint %d mode %d", endpt
, mode
);
683 if (hpriv
->endpoints
[endpt
] < 0) {
684 /* Pick the right node given the control one */
685 strlcpy(devnode
, dpriv
->devnode
, sizeof(devnode
));
686 s
= strchr(devnode
, '.');
687 snprintf(s
, 4, ".%02d", endpt
);
689 /* We may need to read/write to the same endpoint later. */
690 if (((fd
= open(devnode
, O_RDWR
)) < 0) && (errno
== ENXIO
))
691 if ((fd
= open(devnode
, mode
)) < 0)
694 hpriv
->endpoints
[endpt
] = fd
;
697 return (hpriv
->endpoints
[endpt
]);
701 _sync_gen_transfer(struct usbi_transfer
*itransfer
)
703 struct libusb_transfer
*transfer
;
706 transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
709 * Bulk, Interrupt or Isochronous transfer depends on the
710 * endpoint and thus the node to open.
712 if ((fd
= _access_endpoint(transfer
)) < 0)
713 return _errno_to_libusb(errno
);
715 if ((ioctl(fd
, USB_SET_TIMEOUT
, &transfer
->timeout
)) < 0)
716 return _errno_to_libusb(errno
);
718 if (IS_XFERIN(transfer
)) {
719 if ((transfer
->flags
& LIBUSB_TRANSFER_SHORT_NOT_OK
) == 0)
720 if ((ioctl(fd
, USB_SET_SHORT_XFER
, &nr
)) < 0)
721 return _errno_to_libusb(errno
);
723 nr
= read(fd
, transfer
->buffer
, transfer
->length
);
725 nr
= write(fd
, transfer
->buffer
, transfer
->length
);
729 return _errno_to_libusb(errno
);
731 itransfer
->transferred
= nr
;