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