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