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