| 1 |
/* |
| 2 |
|
| 3 |
Copyright (c) 2002 Finger Lakes Instrumentation (FLI), L.L.C. |
| 4 |
All rights reserved. |
| 5 |
|
| 6 |
Redistribution and use in source and binary forms, with or without |
| 7 |
modification, are permitted provided that the following conditions |
| 8 |
are met: |
| 9 |
|
| 10 |
Redistributions of source code must retain the above copyright |
| 11 |
notice, this list of conditions and the following disclaimer. |
| 12 |
|
| 13 |
Redistributions in binary form must reproduce the above |
| 14 |
copyright notice, this list of conditions and the following |
| 15 |
disclaimer in the documentation and/or other materials |
| 16 |
provided with the distribution. |
| 17 |
|
| 18 |
Neither the name of Finger Lakes Instrumentation (FLI), LLC |
| 19 |
nor the names of its contributors may be used to endorse or |
| 20 |
promote products derived from this software without specific |
| 21 |
prior written permission. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 |
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 33 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 |
POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
====================================================================== |
| 37 |
|
| 38 |
Finger Lakes Instrumentation, L.L.C. (FLI) |
| 39 |
web: http://www.fli-cam.com |
| 40 |
email: [email protected] |
| 41 |
|
| 42 |
*/ |
| 43 |
|
| 44 |
#include <sys/types.h> |
| 45 |
#include <dev/usb/usb.h> |
| 46 |
#include <sys/ioctl.h> |
| 47 |
|
| 48 |
#include <stdio.h> |
| 49 |
#include <unistd.h> |
| 50 |
#include <errno.h> |
| 51 |
#include <fcntl.h> |
| 52 |
#include <string.h> |
| 53 |
|
| 54 |
#include "libfli-libfli.h" |
| 55 |
#include "libfli-sys.h" |
| 56 |
#include "libfli-usb.h" |
| 57 |
|
| 58 |
long bsd_usb_connect(flidev_t dev, fli_unixio_t *io, char *name) |
| 59 |
{ |
| 60 |
usb_device_descriptor_t usbdesc; |
| 61 |
int tmp, ep; |
| 62 |
char tmpname[PATH_MAX]; |
| 63 |
|
| 64 |
if (ioctl(io->fd, USB_GET_DEVICE_DESC, &usbdesc) == -1) |
| 65 |
{ |
| 66 |
debug(FLIDEBUG_FAIL, "%s: Could not read descriptor: %s", |
| 67 |
__PRETTY_FUNCTION__, strerror(errno)); |
| 68 |
return -EIO; |
| 69 |
} |
| 70 |
|
| 71 |
if (UGETW(usbdesc.idVendor) != FLIUSB_VENDORID) |
| 72 |
{ |
| 73 |
debug(FLIDEBUG_INFO, "%s: Not a FLI device!", __PRETTY_FUNCTION__); |
| 74 |
return -ENODEV; |
| 75 |
} |
| 76 |
|
| 77 |
switch (UGETW(usbdesc.idProduct)) |
| 78 |
{ |
| 79 |
/* These are valid product IDs */ |
| 80 |
case FLIUSB_CAM_ID: |
| 81 |
case FLIUSB_FOCUSER_ID: |
| 82 |
case FLIUSB_FILTER_ID: |
| 83 |
case FLIUSB_PROLINE_ID: |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
/* Anything else is unknown */ |
| 88 |
return -ENODEV; |
| 89 |
} |
| 90 |
|
| 91 |
DEVICE->devinfo.devid = UGETW(usbdesc.idProduct); |
| 92 |
DEVICE->devinfo.fwrev = UGETW(usbdesc.bcdDevice); |
| 93 |
|
| 94 |
switch (DEVICE->devinfo.devid) |
| 95 |
{ |
| 96 |
case FLIUSB_CAM_ID: |
| 97 |
case FLIUSB_FOCUSER_ID: |
| 98 |
case FLIUSB_FILTER_ID: |
| 99 |
ep = 0x02; |
| 100 |
break; |
| 101 |
|
| 102 |
case FLIUSB_PROLINE_ID: |
| 103 |
ep = 0x01; |
| 104 |
break; |
| 105 |
|
| 106 |
default: |
| 107 |
debug(FLIDEBUG_FAIL, "Unknown device type."); /* This shouldn't happen */ |
| 108 |
return -ENODEV; |
| 109 |
} |
| 110 |
|
| 111 |
if (snprintf(tmpname, sizeof(tmpname), "%s.%d", name, ep) >= sizeof(tmpname)) |
| 112 |
return -EOVERFLOW; |
| 113 |
|
| 114 |
if ((tmp = open(tmpname, O_RDWR)) == -1) |
| 115 |
{ |
| 116 |
debug(FLIDEBUG_FAIL, "%s: open(%s) failed: %s", |
| 117 |
__PRETTY_FUNCTION__, tmpname, strerror(errno)); |
| 118 |
return -errno; |
| 119 |
} |
| 120 |
close(io->fd); |
| 121 |
io->fd = tmp; |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
long bsd_usb_disconnect(flidev_t dev) |
| 127 |
{ |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
long bsd_bulkwrite(flidev_t dev, void *buf, long *wlen) |
| 132 |
{ |
| 133 |
fli_unixio_t *io; |
| 134 |
long org_wlen = *wlen; |
| 135 |
int to; |
| 136 |
|
| 137 |
io = DEVICE->io_data; |
| 138 |
to = DEVICE->io_timeout; |
| 139 |
|
| 140 |
if (ioctl(io->fd, USB_SET_TIMEOUT, &to) == -1) |
| 141 |
return -errno; |
| 142 |
|
| 143 |
*wlen = write(io->fd, buf, *wlen); |
| 144 |
|
| 145 |
if (*wlen != org_wlen) |
| 146 |
return -errno; |
| 147 |
else |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
long bsd_bulkread(flidev_t dev, void *buf, long *rlen) |
| 152 |
{ |
| 153 |
fli_unixio_t *io; |
| 154 |
long org_rlen = *rlen; |
| 155 |
int to; |
| 156 |
|
| 157 |
io = DEVICE->io_data; |
| 158 |
to = DEVICE->io_timeout; |
| 159 |
|
| 160 |
if (ioctl(io->fd, USB_SET_TIMEOUT, &to) == -1) |
| 161 |
return -errno; |
| 162 |
|
| 163 |
*rlen = read(io->fd, buf, *rlen); |
| 164 |
|
| 165 |
if (*rlen != org_rlen) |
| 166 |
return -errno; |
| 167 |
else |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
long bsd_bulktransfer(flidev_t dev, int ep, void *buf, long *len) |
| 172 |
{ |
| 173 |
char name[PATH_MAX]; |
| 174 |
long orglen = *len; |
| 175 |
int fd, to, err; |
| 176 |
|
| 177 |
if (snprintf(name, sizeof(name), "%s.%d", DEVICE->name, ep) >= sizeof(name)) |
| 178 |
return -EOVERFLOW; |
| 179 |
|
| 180 |
if ((fd = open(name, O_RDWR)) == -1) |
| 181 |
{ |
| 182 |
debug(FLIDEBUG_FAIL, "%s: open(%s) failed: %s", |
| 183 |
__PRETTY_FUNCTION__, name, strerror(errno)); |
| 184 |
return -errno; |
| 185 |
} |
| 186 |
|
| 187 |
to = DEVICE->io_timeout; |
| 188 |
if (ioctl(fd, USB_SET_TIMEOUT, &to) == -1) |
| 189 |
{ |
| 190 |
err = -errno; |
| 191 |
goto done; |
| 192 |
} |
| 193 |
|
| 194 |
if (ep & UE_DIR_IN) |
| 195 |
*len = read(fd, buf, *len); |
| 196 |
else |
| 197 |
*len = write(fd, buf, *len); |
| 198 |
|
| 199 |
if (*len != orglen) |
| 200 |
err = -errno; |
| 201 |
else |
| 202 |
err = 0; |
| 203 |
|
| 204 |
done: |
| 205 |
|
| 206 |
close(fd); |
| 207 |
return err; |
| 208 |
} |