summaryrefslogtreecommitdiffstats
path: root/libusb/bsd.c
blob: e63486732b42e721407f07d8f949699006f9ee36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * (Free|Open|Net)BSD USB support
 *
 * Derived from Linux version by Richard Tobin.
 *
 * $Id: bsd.c,v 1.33 2006/03/04 01:16:10 jerdfelt Exp $
 * $Name:  $
 *
 * This library is covered by the LGPL, read LICENSE for details.
 */

/*
 * Note: I don't have a clue what I'm doing.  I just looked at the
 * man pages and source to try and find things that did the same as
 * the Linux version. -- Richard
 *
 * johnjen@reynoldsnet.org - minor fixes with debug mode output. Consistent brace
 * use as well as indenting. More error messages put in to test for failure
 * modes with /dev/ permissions (when it happens). Note: I, like Richard, have
 * no clue what I'm doing. Patches to increase/fix functionality happily
 * accepted!
 */

/* dirkx@webweaving.org - minor changes to make things actually work
 * 	for both read and write.
 */

#if defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
#define __FreeBSD_kernel__ __FreeBSD__
#endif

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#include <dev/usb/usb.h>

#include "usbi.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_OLD_DEV_USB_USB_H
/*
 * It appears some of the BSD's (OpenBSD atleast) have switched over to a
 * new naming convention, so we setup some macro's for backward
 * compability with older versions --jerdfelt
 */

/* struct usb_ctl_request */
#define ucr_addr		addr
#define ucr_request		request
#define ucr_data		data
#define ucr_flags		flags
#define ucr_actlen		actlen

/* struct usb_alt_interface */
#define uai_config_index	config_index
#define uai_interface_index	interface_index
#define uai_alt_no		alt_no

/* struct usb_config_desc */
#define ucd_config_index	config_index
#define ucd_desc		desc

/* struct usb_interface_desc */
#define uid_config_index	config_index
#define uid_interface_index	interface_index
#define uid_alt_index		alt_index
#define uid_desc		desc

/* struct usb_endpoint_desc */
#define ued_config_index	config_index
#define ued_interface_index	interface_index
#define ued_alt_index		alt_index
#define ued_endpoint_index	endpoint_index
#define ued_desc		desc

/* struct usb_full_desc */
#define ufd_config_index	config_index
#define ufd_size		size
#define ufd_data		data

/* struct usb_string_desc */
#define usd_string_index	string_index
#define usd_language_id		language_id
#define usd_desc		desc

/* struct usb_ctl_report_desc */
#define ucrd_size		size
#define ucrd_data		data

/* struct usb_device_info */
#define udi_bus			bus
#define udi_addr		addr
#define udi_cookie		cookie
#define udi_product		product
#define udi_vendor		vendor
#define udi_release		release
#define udi_productNo		productNo
#define udi_vendorNo		vendorNo
#define udi_releaseNo		releaseNo
#define udi_class		class
#define udi_subclass		subclass
#define udi_protocol		protocol
#define udi_config		config
#define udi_lowspeed		lowspeed
#define udi_power		power
#define udi_nports		nports
#define udi_devnames		devnames
#define udi_ports		ports

/* struct usb_ctl_report */
#define ucr_report		report
#define ucr_data		data

/* struct usb_device_stats */
#define uds_requests		requests
#endif

static int ensure_ep_open(usb_dev_handle *dev, int ep, int mode);

#define MAX_CONTROLLERS 10

/* This records the file descriptors for the endpoints.  It doesn't seem
   to work to re-open them for each read (as well as being inefficient). */

struct bsd_usb_dev_handle_info {
    int ep_fd[USB_MAX_ENDPOINTS];
};

int usb_os_open(usb_dev_handle *dev)
{
  int i;
  struct bsd_usb_dev_handle_info *info;
  char ctlpath[PATH_MAX + 1];

  info = malloc(sizeof(struct bsd_usb_dev_handle_info));
  if (!info)
    USB_ERROR(-ENOMEM);
  dev->impl_info = info;

#ifdef __FreeBSD_kernel__
  snprintf(ctlpath, PATH_MAX, "%s", dev->device->filename);
#else
  snprintf(ctlpath, PATH_MAX, "%s.00", dev->device->filename);
#endif
  dev->fd = open(ctlpath, O_RDWR);
  if (dev->fd < 0) {
    dev->fd = open(ctlpath, O_RDONLY);
    if (dev->fd < 0) {
      free(info);
      USB_ERROR_STR(-errno, "failed to open %s: %s",
                    ctlpath, strerror(errno));
    }
  }

  /* Mark the endpoints as not yet open */
  for (i = 0; i < USB_MAX_ENDPOINTS; i++)
    info->ep_fd[i] = -1;

  return 0;
}

int usb_os_close(usb_dev_handle *dev)
{
  struct bsd_usb_dev_handle_info *info = dev->impl_info;
  int i;

  /* Close any open endpoints */

  for (i = 0; i < USB_MAX_ENDPOINTS; i++)
    if (info->ep_fd[i] >= 0) {
      if (usb_debug >= 2)
        fprintf(stderr, "usb_os_close: closing endpoint %d\n", info->ep_fd[i]);
      close(info->ep_fd[i]);
    }

  free(info);

  if (dev->fd <= 0)
    return 0;

  if (close(dev->fd) == -1)
    /* Failing trying to close a file really isn't an error, so return 0 */
    USB_ERROR_STR(0, "tried to close device fd %d: %s", dev->fd,
                  strerror(errno));

  return 0;
}

int usb_set_configuration(usb_dev_handle *dev, int configuration)
{
  int ret;

  ret = ioctl(dev->fd, USB_SET_CONFIG, &configuration);
  if (ret < 0)
    USB_ERROR_STR(-errno, "could not set config %d: %s", configuration,
                  strerror(errno));

  dev->config = configuration;

  return 0;
}

int usb_claim_interface(usb_dev_handle *dev, int interface)
{
  /* BSD doesn't have the corresponding ioctl.  It seems to
     be sufficient to open the relevant endpoints as needed. */

  dev->interface = interface;

  return 0;
}

int usb_release_interface(usb_dev_handle *dev, int interface)
{
  /* See above */
  return 0;
}

int usb_set_altinterface(usb_dev_handle *dev, int alternate)
{
  int ret;
  struct usb_alt_interface intf;

  if (dev->interface < 0)
    USB_ERROR(-EINVAL);

  intf.uai_interface_index = dev->interface;
  intf.uai_alt_no = alternate;

  ret = ioctl(dev->fd, USB_SET_ALTINTERFACE, &intf);
  if (ret < 0)
    USB_ERROR_STR(-errno, "could not set alt intf %d/%d: %s",
                  dev->interface, alternate, strerror(errno));

  dev->altsetting = alternate;

  return 0;
}

static int ensure_ep_open(usb_dev_handle *dev, int ep, int mode)
{
  struct bsd_usb_dev_handle_info *info = dev->impl_info;
  int fd;
  char buf[20];

  /* Get the address for this endpoint; we could check
   * the mode against the direction; but we've done that
   * already in the usb_bulk_read/write.
   */
  ep = UE_GET_ADDR(ep);

  if (info->ep_fd[ep] < 0) {
#ifdef __FreeBSD_kernel__
    snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->device->filename, ep);
#else
    snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->device->filename, ep);
#endif
    /* Try to open it O_RDWR first for those devices which have in and out
     * endpoints with the same address (eg 0x02 and 0x82)
     */
    fd = open(buf, O_RDWR);
    if (fd < 0 && errno == ENXIO)
      fd = open(buf, mode);
    if (fd < 0)
      USB_ERROR_STR(-errno, "can't open %s for bulk read: %s",
                    buf, strerror(errno));
    info->ep_fd[ep] = fd;
  }

  return info->ep_fd[ep];
}

int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                   int timeout)
{
  int fd, ret;

  /* Ensure the endpoint address is correct */
  ep &= ~USB_ENDPOINT_IN;

  fd = ensure_ep_open(dev, ep, O_WRONLY);
  if (fd < 0) {
    if (usb_debug >= 2) {
#ifdef __FreeBSD_kernel__
      fprintf (stderr, "usb_bulk_write: got negative open file descriptor for endpoint %d\n", UE_GET_ADDR(ep));
#else
      fprintf (stderr, "usb_bulk_write: got negative open file descriptor for endpoint %02d\n", UE_GET_ADDR(ep));
#endif
    }
    return fd;
  }

  ret = ioctl(fd, USB_SET_TIMEOUT, &timeout);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting timeout: %s",
                  strerror(errno));

  ret = write(fd, bytes, size);
  if (ret < 0)
#ifdef __FreeBSD_kernel__
    USB_ERROR_STR(-errno, "error writing to bulk endpoint %s.%d: %s",
                  dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#else
    USB_ERROR_STR(-errno, "error writing to bulk endpoint %s.%02d: %s",
                dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif

  return size;
}

int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                  int timeout)
{
  int fd, ret, one = 1;

  /* Ensure the endpoint address is correct */
  ep |= USB_ENDPOINT_IN;

  fd = ensure_ep_open(dev, ep, O_RDONLY);
  if (fd < 0) {
    if (usb_debug >= 2) {
#ifdef __FreeBSD_kernel__
      fprintf (stderr, "usb_bulk_read: got negative open file descriptor for endpoint %d\n", UE_GET_ADDR(ep));
#else
      fprintf (stderr, "usb_bulk_read: got negative open file descriptor for endpoint %02d\n", UE_GET_ADDR(ep));
#endif
    }
    return fd;
  }

  ret = ioctl(fd, USB_SET_TIMEOUT, &timeout);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting timeout: %s", strerror(errno));

  ret = ioctl(fd, USB_SET_SHORT_XFER, &one);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting short xfer: %s", strerror(errno));

  ret = read(fd, bytes, size);
  if (ret < 0)
#ifdef __FreeBSD_kernel__
    USB_ERROR_STR(-errno, "error reading from bulk endpoint %s.%d: %s",
                  dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#else
    USB_ERROR_STR(-errno, "error reading from bulk endpoint %s.%02d: %s",
                dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif

  return ret;
}

int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                        int timeout)
{
  int fd, ret, sent = 0;

  /* Ensure the endpoint address is correct */
  ep &= ~USB_ENDPOINT_IN;

  fd = ensure_ep_open(dev, ep, O_WRONLY);
  if (fd < 0) {
    if (usb_debug >= 2) {
#ifdef __FreeBSD_kernel__
      fprintf (stderr, "usb_interrupt_write: got negative open file descriptor for endpoint %d\n", UE_GET_ADDR(ep));
#else
      fprintf (stderr, "usb_interrupt_write: got negative open file descriptor for endpoint %02d\n", UE_GET_ADDR(ep));
#endif
    }
    return fd;
  }

  ret = ioctl(fd, USB_SET_TIMEOUT, &timeout);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting timeout: %s",
                  strerror(errno));

  do {
    ret = write(fd, bytes+sent, size-sent);
    if (ret < 0)
#ifdef __FreeBSD_kernel__
      USB_ERROR_STR(-errno, "error writing to interrupt endpoint %s.%d: %s",
                    dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#else
      USB_ERROR_STR(-errno, "error writing to interrupt endpoint %s.%02d: %s",
                  dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif

    sent += ret;
  } while (ret > 0 && sent < size);

  return sent;
}

int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                       int timeout)
{
  int fd, ret, retrieved = 0, one = 1;

  /* Ensure the endpoint address is correct */
  ep |= USB_ENDPOINT_IN;

  fd = ensure_ep_open(dev, ep, O_RDONLY);
  if (fd < 0) {
    if (usb_debug >= 2) {
#ifdef __FreeBSD_kernel__
      fprintf (stderr, "usb_interrupt_read: got negative open file descriptor for endpoint %d\n", UE_GET_ADDR(ep));
#else
      fprintf (stderr, "usb_interrupt_read: got negative open file descriptor for endpoint %02d\n", UE_GET_ADDR(ep));
#endif
    }
    return fd;
  }

  ret = ioctl(fd, USB_SET_TIMEOUT, &timeout);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting timeout: %s", strerror(errno));

  ret = ioctl(fd, USB_SET_SHORT_XFER, &one);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error setting short xfer: %s", strerror(errno));

  do {
    ret = read(fd, bytes+retrieved, size-retrieved);
    if (ret < 0)
#ifdef __FreeBSD_kernel__
      USB_ERROR_STR(-errno, "error reading from interrupt endpoint %s.%d: %s",
                    dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#else
      USB_ERROR_STR(-errno, "error reading from interrupt endpoint %s.%02d: %s",
                  dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif
    retrieved += ret;
  } while (ret > 0 && retrieved < size);

  return retrieved;
}

int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
                     int value, int index, char *bytes, int size, int timeout)
{
  struct usb_ctl_request req;
  int ret;

  if (usb_debug >= 3)
    fprintf(stderr, "usb_control_msg: %d %d %d %d %p %d %d\n",
            requesttype, request, value, index, bytes, size, timeout);

  req.ucr_request.bmRequestType = requesttype;
  req.ucr_request.bRequest = request;
  USETW(req.ucr_request.wValue, value);
  USETW(req.ucr_request.wIndex, index);
  USETW(req.ucr_request.wLength, size);

  req.ucr_data = bytes;
  req.ucr_flags = USBD_SHORT_XFER_OK;

  ret = ioctl(dev->fd, USB_SET_TIMEOUT, &timeout);
#if (__NetBSD__ || __OpenBSD__)
  if (ret < 0 && errno != EINVAL)
#else
  if (ret < 0)
#endif
    USB_ERROR_STR(-errno, "error setting timeout: %s",
                  strerror(errno));

  ret = ioctl(dev->fd, USB_DO_REQUEST, &req);
  if (ret < 0)
    USB_ERROR_STR(-errno, "error sending control message: %s",
                  strerror(errno));

  return UGETW(req.ucr_request.wLength);
}

int usb_os_find_busses(struct usb_bus **busses)
{
  struct usb_bus *fbus = NULL;
  int controller;
  int fd;
  char buf[20];

  for (controller = 0; controller < MAX_CONTROLLERS; controller++) {
    struct usb_bus *bus;

    snprintf(buf, sizeof(buf) - 1, "/dev/usb%d", controller);
    fd = open(buf, O_RDWR);
    if (fd < 0) {
      if (usb_debug >= 2)
        if (errno != ENXIO && errno != ENOENT)
          fprintf(stderr, "usb_os_find_busses: can't open %s: %s\n",
                  buf, strerror(errno));
      continue;
    }
    close(fd);

    bus = malloc(sizeof(*bus));
    if (!bus)
      USB_ERROR(-ENOMEM);

    memset((void *)bus, 0, sizeof(*bus));

    strncpy(bus->dirname, buf, sizeof(bus->dirname) - 1);
    bus->dirname[sizeof(bus->dirname) - 1] = 0;

    LIST_ADD(fbus, bus);

    if (usb_debug >= 2)
      fprintf(stderr, "usb_os_find_busses: Found %s\n", bus->dirname);
  }

  *busses = fbus;

  return 0;
}

int usb_os_find_devices(struct usb_bus *bus, struct usb_device **devices)
{
  struct usb_device *fdev = NULL;
  int cfd, dfd;
  int device;

  cfd = open(bus->dirname, O_RDONLY);
  if (cfd < 0)
    USB_ERROR_STR(-errno, "couldn't open(%s): %s", bus->dirname,
                  strerror(errno));

  for (device = 1; device < USB_MAX_DEVICES; device++) {
    struct usb_device_info di;
    struct usb_device *dev;
    unsigned char device_desc[DEVICE_DESC_LENGTH];
    char buf[20];

    di.udi_addr = device;
    if (ioctl(cfd, USB_DEVICEINFO, &di) < 0)
      continue;

    /* There's a device; is it one we should mess with? */

    if (strncmp(di.udi_devnames[0], "ugen", 4) != 0)
      /* best not to play with things we don't understand */
      continue;

#ifdef __FreeBSD_kernel__
    snprintf(buf, sizeof(buf) - 1, "/dev/%s", di.udi_devnames[0]);
#else
    snprintf(buf, sizeof(buf) - 1, "/dev/%s.00", di.udi_devnames[0]);
#endif

    /* Open its control endpoint */
    dfd = open(buf, O_RDONLY);
    if (dfd < 0) {
      if (usb_debug >= 2)
        fprintf(stderr, "usb_os_find_devices: couldn't open device %s: %s\n",
                buf, strerror(errno));
      continue;
    }

    dev = malloc(sizeof(*dev));
    if (!dev)
      USB_ERROR(-ENOMEM);

    memset((void *)dev, 0, sizeof(*dev));

    dev->bus = bus;

    /* we need to report the device name as /dev/ugenx NOT /dev/ugenx.00
     * This seemed easier than having 2 variables...
     */
#if (__NetBSD__ || __OpenBSD__)
    snprintf(buf, sizeof(buf) - 1, "/dev/%s", di.udi_devnames[0]);
#endif

    strncpy(dev->filename, buf, sizeof(dev->filename) - 1);
    dev->filename[sizeof(dev->filename) - 1] = 0;

    if (ioctl(dfd, USB_GET_DEVICE_DESC, device_desc) < 0)
      USB_ERROR_STR(-errno, "couldn't get device descriptor for %s: %s",
                    buf, strerror(errno));

    close(dfd);

    usb_parse_descriptor(device_desc, "bbwbbbbwwwbbbb", &dev->descriptor);

    LIST_ADD(fdev, dev);

    if (usb_debug >= 2)
      fprintf(stderr, "usb_os_find_devices: Found %s on %s\n",
              dev->filename, bus->dirname);
  }

  close(cfd);

  *devices = fdev;

  return 0;
}

int usb_os_determine_children(struct usb_bus *bus)
{
  /* Nothing yet */
  return 0;
}

void usb_os_init(void)
{
  /* nothing */
}

int usb_resetep(usb_dev_handle *dev, unsigned int ep)
{
  /* Not yet done, because I haven't needed it. */

  USB_ERROR_STR(-ENOSYS, "usb_resetep called, unimplemented on BSD");
}

int usb_clear_halt(usb_dev_handle *dev, unsigned int ep)
{
  /* Not yet done, because I haven't needed it. */

  USB_ERROR_STR(-ENOSYS, "usb_clear_halt called, unimplemented on BSD");
}

int usb_reset(usb_dev_handle *dev)
{
  /* Not yet done, because I haven't needed it. */

  USB_ERROR_STR(-ENOSYS, "usb_reset called, unimplemented on BSD");
}