fx2/linux/libstrm/usb2strm.c

96 lines
2.8 KiB
C
Raw Normal View History

2013-10-07 09:13:29 +02:00
/* *****************************************************************************
* Copyright (c) 2013 Benjamin Krill <benjamin@krll.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ****************************************************************************/
2013-10-02 10:43:54 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <usb.h>
#include <string.h>
#include <endian.h>
#include <strm.h>
#define DEBUG 0
#if defined(DEBUG) && DEBUG == 1
#define printf_dbg(fmt, args...) printf(fmt, ## args)
#else
#define printf_dbg(args...)
#endif
static usb_dev_handle *hd = NULL;
/* TODO: extend to support multiple USB device on one host! */
static struct usb_device *
find_device(struct usb_bus* busses, int vendor, int product)
{
struct usb_bus *bus;
struct usb_device *dev;
for (bus = busses; bus; bus = bus->next) {
printf_dbg("scanning bus %s\n", bus->dirname);
for (dev = bus->devices; dev; dev=dev->next) {
/* compare device vendor and product */
if ((dev->descriptor.idVendor == vendor) &&
(dev->descriptor.idProduct == product)) {
/* print device information */
printf_dbg("\tfound device %s bus %s (idVendor 0x%04x idProduct 0x%04x)\n",
dev->filename, dev->bus->dirname, dev->descriptor.idVendor,
dev->descriptor.idProduct);
return dev;
} else {
printf_dbg("\tdevice %s does not match\n", dev->filename);
}
}
}
return NULL;
}
usb_dev_handle *
strm_init(int vendor, int product)
{
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
if ((dev = find_device(usb_get_busses(), vendor, product)) == NULL)
return NULL;
if (!(hd = usb_open(dev))) {
printf("FAILED: cannot open device.\n");
return NULL;
}
/* reset usb device */
usb_reset(hd);
printf_dbg("Lets do the magic (%p) ...\n", hd);
return hd;
}
int
strm_fini(void)
{
if (hd)
usb_close(hd);
hd = NULL;
return 0;
}