netbsd: Transition to use new transfer completion API
[platform/upstream/libusb.git] / libusb / os / netbsd_usb.c
1 /*
2  * Copyright © 2011 Martin Pieuchot <mpi@openbsd.org>
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <sys/time.h>
22 #include <sys/types.h>
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <dev/usb/usb.h>
32
33 #include "libusbi.h"
34
35 struct device_priv {
36         char devnode[16];
37         int fd;
38
39         unsigned char *cdesc;                   /* active config descriptor */
40         usb_device_descriptor_t ddesc;          /* usb device descriptor */
41 };
42
43 struct handle_priv {
44         int endpoints[USB_MAX_ENDPOINTS];
45 };
46
47 /*
48  * Backend functions
49  */
50 static int netbsd_get_device_list(struct libusb_context *,
51     struct discovered_devs **);
52 static int netbsd_open(struct libusb_device_handle *);
53 static void netbsd_close(struct libusb_device_handle *);
54
55 static int netbsd_get_device_descriptor(struct libusb_device *, unsigned char *,
56     int *);
57 static int netbsd_get_active_config_descriptor(struct libusb_device *,
58     unsigned char *, size_t, int *);
59 static int netbsd_get_config_descriptor(struct libusb_device *, uint8_t,
60     unsigned char *, size_t, int *);
61
62 static int netbsd_get_configuration(struct libusb_device_handle *, int *);
63 static int netbsd_set_configuration(struct libusb_device_handle *, int);
64
65 static int netbsd_claim_interface(struct libusb_device_handle *, int);
66 static int netbsd_release_interface(struct libusb_device_handle *, int);
67
68 static int netbsd_set_interface_altsetting(struct libusb_device_handle *, int,
69     int);
70 static int netbsd_clear_halt(struct libusb_device_handle *, unsigned char);
71 static int netbsd_reset_device(struct libusb_device_handle *);
72 static void netbsd_destroy_device(struct libusb_device *);
73
74 static int netbsd_submit_transfer(struct usbi_transfer *);
75 static int netbsd_cancel_transfer(struct usbi_transfer *);
76 static void netbsd_clear_transfer_priv(struct usbi_transfer *);
77 static int netbsd_handle_transfer_completion(struct usbi_transfer *);
78 static int netbsd_clock_gettime(int, struct timespec *);
79
80 /*
81  * Private functions
82  */
83 static int _errno_to_libusb(int);
84 static int _cache_active_config_descriptor(struct libusb_device *, int);
85 static int _sync_control_transfer(struct usbi_transfer *);
86 static int _sync_gen_transfer(struct usbi_transfer *);
87 static int _access_endpoint(struct libusb_transfer *);
88
89 const struct usbi_os_backend netbsd_backend = {
90         "Synchronous NetBSD backend",
91         0,
92         NULL,                           /* init() */
93         NULL,                           /* exit() */
94         netbsd_get_device_list,
95         NULL,                           /* hotplug_poll */
96         netbsd_open,
97         netbsd_close,
98
99         netbsd_get_device_descriptor,
100         netbsd_get_active_config_descriptor,
101         netbsd_get_config_descriptor,
102         NULL,                           /* get_config_descriptor_by_value() */
103
104         netbsd_get_configuration,
105         netbsd_set_configuration,
106
107         netbsd_claim_interface,
108         netbsd_release_interface,
109
110         netbsd_set_interface_altsetting,
111         netbsd_clear_halt,
112         netbsd_reset_device,
113
114         NULL,                           /* alloc_streams */
115         NULL,                           /* free_streams */
116
117         NULL,                           /* kernel_driver_active() */
118         NULL,                           /* detach_kernel_driver() */
119         NULL,                           /* attach_kernel_driver() */
120
121         netbsd_destroy_device,
122
123         netbsd_submit_transfer,
124         netbsd_cancel_transfer,
125         netbsd_clear_transfer_priv,
126
127         NULL,                           /* handle_events() */
128         netbsd_handle_transfer_completion,
129
130         netbsd_clock_gettime,
131         sizeof(struct device_priv),
132         sizeof(struct handle_priv),
133         0,                              /* transfer_priv_size */
134         0,                              /* add_iso_packet_size */
135 };
136
137 int
138 netbsd_get_device_list(struct libusb_context * ctx,
139         struct discovered_devs **discdevs)
140 {
141         struct libusb_device *dev;
142         struct device_priv *dpriv;
143         struct usb_device_info di;
144         unsigned long session_id;
145         char devnode[16];
146         int fd, err, i;
147
148         usbi_dbg("");
149
150         /* Only ugen(4) is supported */
151         for (i = 0; i < USB_MAX_DEVICES; i++) {
152                 /* Control endpoint is always .00 */
153                 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
154
155                 if ((fd = open(devnode, O_RDONLY)) < 0) {
156                         if (errno != ENOENT && errno != ENXIO)
157                                 usbi_err(ctx, "could not open %s", devnode);
158                         continue;
159                 }
160
161                 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
162                         continue;
163
164                 session_id = (di.udi_bus << 8 | di.udi_addr);
165                 dev = usbi_get_device_by_session_id(ctx, session_id);
166
167                 if (dev == NULL) {
168                         dev = usbi_alloc_device(ctx, session_id);
169                         if (dev == NULL)
170                                 return (LIBUSB_ERROR_NO_MEM);
171
172                         dev->bus_number = di.udi_bus;
173                         dev->device_address = di.udi_addr;
174                         dev->speed = di.udi_speed;
175
176                         dpriv = (struct device_priv *)dev->os_priv;
177                         strlcpy(dpriv->devnode, devnode, sizeof(devnode));
178                         dpriv->fd = -1;
179
180                         if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
181                                 err = errno;
182                                 goto error;
183                         }
184
185                         dpriv->cdesc = NULL;
186                         if (_cache_active_config_descriptor(dev, fd)) {
187                                 err = errno;
188                                 goto error;
189                         }
190
191                         if ((err = usbi_sanitize_device(dev)))
192                                 goto error;
193                 }
194                 close(fd);
195
196                 if (discovered_devs_append(*discdevs, dev) == NULL)
197                         return (LIBUSB_ERROR_NO_MEM);
198
199                 libusb_unref_device(dev);
200         }
201
202         return (LIBUSB_SUCCESS);
203
204 error:
205         close(fd);
206         libusb_unref_device(dev);
207         return _errno_to_libusb(err);
208 }
209
210 int
211 netbsd_open(struct libusb_device_handle *handle)
212 {
213         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
214         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
215
216         dpriv->fd = open(dpriv->devnode, O_RDWR);
217         if (dpriv->fd < 0) {
218                 dpriv->fd = open(dpriv->devnode, O_RDONLY);
219                 if (dpriv->fd < 0)
220                         return _errno_to_libusb(errno);
221         }
222
223         usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
224
225         return (LIBUSB_SUCCESS);
226 }
227
228 void
229 netbsd_close(struct libusb_device_handle *handle)
230 {
231         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
232         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
233
234         usbi_dbg("close: fd %d", dpriv->fd);
235
236         close(dpriv->fd);
237         dpriv->fd = -1;
238 }
239
240 int
241 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
242     int *host_endian)
243 {
244         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
245
246         usbi_dbg("");
247
248         memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
249
250         *host_endian = 0;
251
252         return (LIBUSB_SUCCESS);
253 }
254
255 int
256 netbsd_get_active_config_descriptor(struct libusb_device *dev,
257     unsigned char *buf, size_t len, int *host_endian)
258 {
259         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
260         usb_config_descriptor_t *ucd;
261
262         ucd = (usb_config_descriptor_t *) dpriv->cdesc;
263         len = MIN(len, UGETW(ucd->wTotalLength));
264
265         usbi_dbg("len %d", len);
266
267         memcpy(buf, dpriv->cdesc, len);
268
269         *host_endian = 0;
270
271         return len;
272 }
273
274 int
275 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
276     unsigned char *buf, size_t len, int *host_endian)
277 {
278         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
279         struct usb_full_desc ufd;
280         int fd, err;
281
282         usbi_dbg("index %d, len %d", idx, len);
283
284         /* A config descriptor may be requested before opening the device */
285         if (dpriv->fd >= 0) {
286                 fd = dpriv->fd;
287         } else {
288                 fd = open(dpriv->devnode, O_RDONLY);
289                 if (fd < 0)
290                         return _errno_to_libusb(errno);
291         }
292
293         ufd.ufd_config_index = idx;
294         ufd.ufd_size = len;
295         ufd.ufd_data = buf;
296
297         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
298                 err = errno;
299                 if (dpriv->fd < 0)
300                         close(fd);
301                 return _errno_to_libusb(err);
302         }
303
304         if (dpriv->fd < 0)
305                 close(fd);
306
307         *host_endian = 0;
308
309         return len;
310 }
311
312 int
313 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
314 {
315         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
316
317         usbi_dbg("");
318
319         if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
320                 return _errno_to_libusb(errno);
321
322         usbi_dbg("configuration %d", *config);
323
324         return (LIBUSB_SUCCESS);
325 }
326
327 int
328 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
329 {
330         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
331
332         usbi_dbg("configuration %d", config);
333
334         if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
335                 return _errno_to_libusb(errno);
336
337         return _cache_active_config_descriptor(handle->dev, dpriv->fd);
338 }
339
340 int
341 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
342 {
343         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
344         int i;
345
346         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
347                 hpriv->endpoints[i] = -1;
348
349         return (LIBUSB_SUCCESS);
350 }
351
352 int
353 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
354 {
355         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
356         int i;
357
358         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
359                 if (hpriv->endpoints[i] >= 0)
360                         close(hpriv->endpoints[i]);
361
362         return (LIBUSB_SUCCESS);
363 }
364
365 int
366 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
367     int altsetting)
368 {
369         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
370         struct usb_alt_interface intf;
371
372         usbi_dbg("iface %d, setting %d", iface, altsetting);
373
374         memset(&intf, 0, sizeof(intf));
375
376         intf.uai_interface_index = iface;
377         intf.uai_alt_no = altsetting;
378
379         if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
380                 return _errno_to_libusb(errno);
381
382         return (LIBUSB_SUCCESS);
383 }
384
385 int
386 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
387 {
388         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
389         struct usb_ctl_request req;
390
391         usbi_dbg("");
392
393         req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
394         req.ucr_request.bRequest = UR_CLEAR_FEATURE;
395         USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
396         USETW(req.ucr_request.wIndex, endpoint);
397         USETW(req.ucr_request.wLength, 0);
398
399         if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
400                 return _errno_to_libusb(errno);
401
402         return (LIBUSB_SUCCESS);
403 }
404
405 int
406 netbsd_reset_device(struct libusb_device_handle *handle)
407 {
408         usbi_dbg("");
409
410         return (LIBUSB_ERROR_NOT_SUPPORTED);
411 }
412
413 void
414 netbsd_destroy_device(struct libusb_device *dev)
415 {
416         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
417
418         usbi_dbg("");
419
420         free(dpriv->cdesc);
421 }
422
423 int
424 netbsd_submit_transfer(struct usbi_transfer *itransfer)
425 {
426         struct libusb_transfer *transfer;
427         struct handle_priv *hpriv;
428         int err = 0;
429
430         usbi_dbg("");
431
432         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
433         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
434
435         switch (transfer->type) {
436         case LIBUSB_TRANSFER_TYPE_CONTROL:
437                 err = _sync_control_transfer(itransfer);
438                 break;
439         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
440                 if (IS_XFEROUT(transfer)) {
441                         /* Isochronous write is not supported */
442                         err = LIBUSB_ERROR_NOT_SUPPORTED;
443                         break;
444                 }
445                 err = _sync_gen_transfer(itransfer);
446                 break;
447         case LIBUSB_TRANSFER_TYPE_BULK:
448         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
449                 if (IS_XFEROUT(transfer) &&
450                     transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
451                         err = LIBUSB_ERROR_NOT_SUPPORTED;
452                         break;
453                 }
454                 err = _sync_gen_transfer(itransfer);
455                 break;
456         case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
457                 err = LIBUSB_ERROR_NOT_SUPPORTED;
458                 break;
459         }
460
461         if (err)
462                 return (err);
463
464         usbi_signal_transfer_completion(itransfer);
465
466         return (LIBUSB_SUCCESS);
467 }
468
469 int
470 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
471 {
472         usbi_dbg("");
473
474         return (LIBUSB_ERROR_NOT_SUPPORTED);
475 }
476
477 void
478 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
479 {
480         usbi_dbg("");
481
482         /* Nothing to do */
483 }
484
485 int
486 netbsd_handle_transfer_completion(struct usbi_transfer *itransfer)
487 {
488         return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
489 }
490
491 int
492 netbsd_clock_gettime(int clkid, struct timespec *tp)
493 {
494         usbi_dbg("clock %d", clkid);
495
496         if (clkid == USBI_CLOCK_REALTIME)
497                 return clock_gettime(CLOCK_REALTIME, tp);
498
499         if (clkid == USBI_CLOCK_MONOTONIC)
500                 return clock_gettime(CLOCK_MONOTONIC, tp);
501
502         return (LIBUSB_ERROR_INVALID_PARAM);
503 }
504
505 int
506 _errno_to_libusb(int err)
507 {
508         switch (err) {
509         case EIO:
510                 return (LIBUSB_ERROR_IO);
511         case EACCES:
512                 return (LIBUSB_ERROR_ACCESS);
513         case ENOENT:
514                 return (LIBUSB_ERROR_NO_DEVICE);
515         case ENOMEM:
516                 return (LIBUSB_ERROR_NO_MEM);
517         }
518
519         usbi_dbg("error: %s", strerror(err));
520
521         return (LIBUSB_ERROR_OTHER);
522 }
523
524 int
525 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
526 {
527         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
528         struct usb_config_desc ucd;
529         struct usb_full_desc ufd;
530         unsigned char* buf;
531         int len;
532
533         usbi_dbg("fd %d", fd);
534
535         ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
536
537         if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
538                 return _errno_to_libusb(errno);
539
540         usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
541
542         len = UGETW(ucd.ucd_desc.wTotalLength);
543         buf = malloc(len);
544         if (buf == NULL)
545                 return (LIBUSB_ERROR_NO_MEM);
546
547         ufd.ufd_config_index = ucd.ucd_config_index;
548         ufd.ufd_size = len;
549         ufd.ufd_data = buf;
550
551         usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
552
553         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
554                 free(buf);
555                 return _errno_to_libusb(errno);
556         }
557
558         if (dpriv->cdesc)
559                 free(dpriv->cdesc);
560         dpriv->cdesc = buf;
561
562         return (0);
563 }
564
565 int
566 _sync_control_transfer(struct usbi_transfer *itransfer)
567 {
568         struct libusb_transfer *transfer;
569         struct libusb_control_setup *setup;
570         struct device_priv *dpriv;
571         struct usb_ctl_request req;
572
573         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
574         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
575         setup = (struct libusb_control_setup *)transfer->buffer;
576
577         usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
578             setup->bmRequestType, setup->bRequest,
579             libusb_le16_to_cpu(setup->wValue),
580             libusb_le16_to_cpu(setup->wIndex),
581             libusb_le16_to_cpu(setup->wLength), transfer->timeout);
582
583         req.ucr_request.bmRequestType = setup->bmRequestType;
584         req.ucr_request.bRequest = setup->bRequest;
585         /* Don't use USETW, libusb already deals with the endianness */
586         (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
587         (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
588         (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
589         req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
590
591         if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
592                 req.ucr_flags = USBD_SHORT_XFER_OK;
593
594         if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
595                 return _errno_to_libusb(errno);
596
597         if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
598                 return _errno_to_libusb(errno);
599
600         itransfer->transferred = req.ucr_actlen;
601
602         usbi_dbg("transferred %d", itransfer->transferred);
603
604         return (0);
605 }
606
607 int
608 _access_endpoint(struct libusb_transfer *transfer)
609 {
610         struct handle_priv *hpriv;
611         struct device_priv *dpriv;
612         char *s, devnode[16];
613         int fd, endpt;
614         mode_t mode;
615
616         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
617         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
618
619         endpt = UE_GET_ADDR(transfer->endpoint);
620         mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
621
622         usbi_dbg("endpoint %d mode %d", endpt, mode);
623
624         if (hpriv->endpoints[endpt] < 0) {
625                 /* Pick the right node given the control one */
626                 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
627                 s = strchr(devnode, '.');
628                 snprintf(s, 4, ".%02d", endpt);
629
630                 /* We may need to read/write to the same endpoint later. */
631                 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
632                         if ((fd = open(devnode, mode)) < 0)
633                                 return (-1);
634
635                 hpriv->endpoints[endpt] = fd;
636         }
637
638         return (hpriv->endpoints[endpt]);
639 }
640
641 int
642 _sync_gen_transfer(struct usbi_transfer *itransfer)
643 {
644         struct libusb_transfer *transfer;
645         int fd, nr = 1;
646
647         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
648
649         /*
650          * Bulk, Interrupt or Isochronous transfer depends on the
651          * endpoint and thus the node to open.
652          */
653         if ((fd = _access_endpoint(transfer)) < 0)
654                 return _errno_to_libusb(errno);
655
656         if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
657                 return _errno_to_libusb(errno);
658
659         if (IS_XFERIN(transfer)) {
660                 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
661                         if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
662                                 return _errno_to_libusb(errno);
663
664                 nr = read(fd, transfer->buffer, transfer->length);
665         } else {
666                 nr = write(fd, transfer->buffer, transfer->length);
667         }
668
669         if (nr < 0)
670                 return _errno_to_libusb(errno);
671
672         itransfer->transferred = nr;
673
674         return (0);
675 }