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