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