| 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 <linux/version.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <sys/types.h> |
| 47 |
#include <fcntl.h> |
| 48 |
|
| 49 |
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)) |
| 50 |
#include <linux/usb/ch9.h> |
| 51 |
#else |
| 52 |
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)) |
| 53 |
#include <usb.h> |
| 54 |
#include <linux/usb.h> |
| 55 |
#else |
| 56 |
#include <linux/usb_ch9.h> |
| 57 |
#endif |
| 58 |
#endif |
| 59 |
|
| 60 |
#include <linux/usbdevice_fs.h> |
| 61 |
#include <sys/ioctl.h> |
| 62 |
#include <stdio.h> |
| 63 |
|
| 64 |
#include <errno.h> |
| 65 |
|
| 66 |
#include "libfli-libfli.h" |
| 67 |
#include "libfli-sys.h" |
| 68 |
#include "libfli-mem.h" |
| 69 |
#include "libfli-usb.h" |
| 70 |
#include "fliusb_ioctl.h" |
| 71 |
|
| 72 |
long linux_usb_connect(flidev_t dev, fli_unixio_t *io, char *name) |
| 73 |
{ |
| 74 |
struct usb_device_descriptor usbdesc; |
| 75 |
fliusb_string_descriptor_t strdesc; |
| 76 |
int confg, r; |
| 77 |
|
| 78 |
if ((io->fd = open(name, O_RDWR)) == -1) |
| 79 |
{ |
| 80 |
xfree(io); |
| 81 |
return -errno; |
| 82 |
} |
| 83 |
|
| 84 |
if (ioctl(io->fd, FLIUSB_GET_DEVICE_DESCRIPTOR, &usbdesc) == -1) |
| 85 |
{ |
| 86 |
debug(FLIDEBUG_FAIL, "%s: Could not read descriptor: %s", |
| 87 |
__PRETTY_FUNCTION__, strerror(errno)); |
| 88 |
return -EIO; |
| 89 |
} |
| 90 |
|
| 91 |
if (usbdesc.idVendor != FLIUSB_VENDORID) |
| 92 |
{ |
| 93 |
debug(FLIDEBUG_INFO, "%s: Not a FLI device!", __PRETTY_FUNCTION__); |
| 94 |
return -ENODEV; |
| 95 |
} |
| 96 |
|
| 97 |
switch (usbdesc.idProduct) |
| 98 |
{ |
| 99 |
/* These are valid product IDs */ |
| 100 |
case FLIUSB_CAM_ID: |
| 101 |
case FLIUSB_FOCUSER_ID: |
| 102 |
case FLIUSB_FILTER_ID: |
| 103 |
case FLIUSB_PROLINE_ID: |
| 104 |
break; |
| 105 |
|
| 106 |
default: |
| 107 |
/* Anything else is unknown */ |
| 108 |
return -ENODEV; |
| 109 |
} |
| 110 |
|
| 111 |
DEVICE->devinfo.devid = usbdesc.idProduct; |
| 112 |
DEVICE->devinfo.fwrev = usbdesc.bcdDevice; |
| 113 |
|
| 114 |
strdesc.index = 3; |
| 115 |
if (ioctl(io->fd, FLIUSB_GET_STRING_DESCRIPTOR, &strdesc) != 0) |
| 116 |
{ |
| 117 |
debug(FLIDEBUG_FAIL, "%s: Could not read descriptor: %s", |
| 118 |
__PRETTY_FUNCTION__, strerror(errno)); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
DEVICE->devinfo.serial = xstrndup((char *) strdesc.buf, sizeof(strdesc.buf)); |
| 123 |
} |
| 124 |
|
| 125 |
confg = 0; |
| 126 |
r = ioctl (io->fd, USBDEVFS_SETCONFIGURATION, &confg); |
| 127 |
debug(FLIDEBUG_INFO, "USBDEVFS_SETCONFIGURATION return %i", r); |
| 128 |
confg = 1; |
| 129 |
r = ioctl (io->fd, USBDEVFS_SETCONFIGURATION, &confg); |
| 130 |
debug(FLIDEBUG_INFO, "USBDEVFS_SETCONFIGURATION return %i", r); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
long linux_bulktransfer(flidev_t dev, int ep, void *buf, long *len) |
| 135 |
{ |
| 136 |
fli_unixio_t *io; |
| 137 |
fliusb_bulktransfer_t bulkxfer; |
| 138 |
size_t remaining; |
| 139 |
int err = 0; |
| 140 |
|
| 141 |
#define _DEBUG |
| 142 |
|
| 143 |
#ifdef _DEBUG |
| 144 |
debug(FLIDEBUG_INFO, "%s: attempting %ld bytes %s", |
| 145 |
__PRETTY_FUNCTION__, *len, (ep & USB_DIR_IN) ? "in" : "out"); |
| 146 |
#endif |
| 147 |
|
| 148 |
io = DEVICE->io_data; |
| 149 |
|
| 150 |
|
| 151 |
#ifdef _DEBUG |
| 152 |
if ((ep & 0xf0) == 0) { |
| 153 |
char buffer[1024]; |
| 154 |
int i; |
| 155 |
|
| 156 |
sprintf(buffer, "OUT %6ld: ", *len); |
| 157 |
for (i = 0; i < ((*len > 16)?16:*len); i++) { |
| 158 |
sprintf(buffer + strlen(buffer), "%02x ", ((unsigned char *) buf)[i]); |
| 159 |
} |
| 160 |
|
| 161 |
debug(FLIDEBUG_INFO, buffer); |
| 162 |
} |
| 163 |
|
| 164 |
#endif /* _DEBUG */ |
| 165 |
|
| 166 |
remaining = *len; |
| 167 |
while (remaining) /* read up to USB_READ_SIZ_MAX bytes at a time */ |
| 168 |
{ |
| 169 |
int bytes; |
| 170 |
|
| 171 |
bulkxfer.ep = ep; |
| 172 |
bulkxfer.count = MIN(remaining, USB_READ_SIZ_MAX); |
| 173 |
bulkxfer.timeout = DEVICE->io_timeout; |
| 174 |
bulkxfer.buf = buf + *len - remaining; |
| 175 |
|
| 176 |
/* This ioctl returns the number of bytes transferred */ |
| 177 |
bytes = ioctl(io->fd, |
| 178 |
(ep & USB_DIR_IN) ? FLIUSB_BULKREAD : FLIUSB_BULKWRITE, |
| 179 |
&bulkxfer); |
| 180 |
|
| 181 |
if (bytes < 0) |
| 182 |
break; |
| 183 |
remaining -= bytes; |
| 184 |
if (bytes < bulkxfer.count) |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
/* Set *len to the number of bytes actually transfered */ |
| 189 |
if (remaining) |
| 190 |
err = -errno; |
| 191 |
*len -= remaining; |
| 192 |
|
| 193 |
#ifdef _DEBUG |
| 194 |
|
| 195 |
if ((ep & 0xf0) != 0) { |
| 196 |
char buffer[1024]; |
| 197 |
int i; |
| 198 |
|
| 199 |
sprintf(buffer, " IN %6ld: ", *len); |
| 200 |
for (i = 0; i < ((*len > 16)?16:*len); i++) { |
| 201 |
sprintf(buffer + strlen(buffer), "%02x ", ((unsigned char *) buf)[i]); |
| 202 |
} |
| 203 |
|
| 204 |
debug(FLIDEBUG_INFO, buffer); |
| 205 |
} |
| 206 |
|
| 207 |
#endif /* _DEBUG */ |
| 208 |
|
| 209 |
return err; |
| 210 |
} |
| 211 |
|
| 212 |
long linux_bulkwrite(flidev_t dev, void *buf, long *wlen) |
| 213 |
{ |
| 214 |
int ep; |
| 215 |
|
| 216 |
switch (DEVICE->devinfo.devid) |
| 217 |
{ |
| 218 |
case FLIUSB_CAM_ID: |
| 219 |
case FLIUSB_FOCUSER_ID: |
| 220 |
case FLIUSB_FILTER_ID: |
| 221 |
ep = 0x02; |
| 222 |
break; |
| 223 |
|
| 224 |
case FLIUSB_PROLINE_ID: |
| 225 |
ep = 0x01; |
| 226 |
break; |
| 227 |
|
| 228 |
default: |
| 229 |
debug(FLIDEBUG_FAIL, "Unknown device type."); |
| 230 |
return -EINVAL; |
| 231 |
} |
| 232 |
|
| 233 |
return linux_bulktransfer(dev, ep | USB_DIR_OUT, buf, wlen); |
| 234 |
} |
| 235 |
|
| 236 |
long linux_bulkread(flidev_t dev, void *buf, long *rlen) |
| 237 |
{ |
| 238 |
int ep; |
| 239 |
|
| 240 |
switch (DEVICE->devinfo.devid) |
| 241 |
{ |
| 242 |
case FLIUSB_CAM_ID: |
| 243 |
case FLIUSB_FOCUSER_ID: |
| 244 |
case FLIUSB_FILTER_ID: |
| 245 |
ep = 0x02; |
| 246 |
break; |
| 247 |
|
| 248 |
case FLIUSB_PROLINE_ID: |
| 249 |
ep = 0x01; |
| 250 |
break; |
| 251 |
|
| 252 |
default: |
| 253 |
debug(FLIDEBUG_FAIL, "Unknown device type."); |
| 254 |
return -EINVAL; |
| 255 |
} |
| 256 |
|
| 257 |
return linux_bulktransfer(dev, ep | USB_DIR_IN, buf, rlen); |
| 258 |
} |
| 259 |
|
| 260 |
long linux_usb_disconnect(flidev_t dev) |
| 261 |
{ |
| 262 |
fli_unixio_t *io; |
| 263 |
|
| 264 |
io = DEVICE->io_data; |
| 265 |
|
| 266 |
long err = 0; |
| 267 |
|
| 268 |
if (io->fd != (-1)) |
| 269 |
err = close(io->fd); |
| 270 |
|
| 271 |
return err; |
| 272 |
} |