1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-12-23 12:45:31 +02:00

tools/lib/: copied USB device opening code over from f32xbase

We shared it across projects so far, but since the code will be further
customized, it's better to keep it local (at least for a while).

- usbopen.h, usbopen.c: moved over f32xbase/include/usb.h and
  f32xbase/lib/usb.c (for further customization)
- Makefile (CFLAGS, OBJS_host): use usbopen.o instead of usb.o from
  f32xbase
- atusb.c: use usbopen.h instead of usb.h from f32xbase (also removes
  include path ambiguity)
This commit is contained in:
Werner Almesberger 2011-04-10 07:43:42 -03:00
parent 1a27e590d9
commit a75edf0cc0
4 changed files with 127 additions and 4 deletions

View File

@ -15,9 +15,7 @@ LIB = libatrf.a
include ../Makefile.common
CFLAGS += -Wall -I$(F32XBASE)/include
OBJS_host = atusb.o $(F32XBASE)/lib/usb.o
OBJS_host = atusb.o usbopen.o
OBJS_ben_jlime = atben.o
OBJS_ben_openwrt = atben.o

View File

@ -14,10 +14,10 @@
#include <stdio.h>
#include <usb.h>
#include "f32xbase/usb.h"
#include "atusb/ep0.h"
#include "atusb/usb-ids.h"
#include "usbopen.h"
#include "driver.h"

101
tools/lib/usbopen.c Normal file
View File

@ -0,0 +1,101 @@
/*
* lib/usbopen.c - Common USB device lookup and open code
*
* Written 2008-2010 by Werner Almesberger
* Copyright 2008-2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <usb.h>
#include "usbopen.h"
static uint16_t vendor = 0;
static uint16_t product = 0;
usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
{
const struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *handle;
#ifdef DO_FULL_USB_BUREAUCRACY
int res;
#endif
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor !=
(vendor ? vendor : default_vendor))
continue;
if (dev->descriptor.idProduct !=
(product ? product : default_product))
continue;
handle = usb_open(dev);
#ifdef DO_FULL_USB_BUREAUCRACY
if (!handle)
return NULL;
res = usb_set_configuration(handle, 1);
if (res < 0) {
fprintf(stderr, "usb_set_configuration: %d\n",
res);
return NULL;
}
res = usb_claim_interface(handle, 0);
if (res < 0) {
fprintf(stderr, "usb_claim_interface: %d\n",
res);
return NULL;
}
res = usb_set_altinterface(handle, 0);
if (res < 0) {
fprintf(stderr, "usb_set_altinterface: %d\n",
res);
return NULL;
}
#endif
return handle;
}
return NULL;
}
static void bad_id(const char *id)
{
fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id);
exit(1);
}
void parse_usb_id(const char *id)
{
unsigned long tmp;
char *end;
tmp = strtoul(id, &end, 16);
if (*end != ':')
bad_id(id);
if (tmp > 0xffff)
bad_id(id);
vendor = tmp;
tmp = strtoul(end+1, &end, 16);
if (*end)
bad_id(id);
if (tmp > 0xffff)
bad_id(id);
product = tmp;
}

24
tools/lib/usbopen.h Normal file
View File

@ -0,0 +1,24 @@
/*
* lib/usbopen.h - Common USB device lookup and open code
*
* Written 2008-2010 by Werner Almesberger
* Copyright 2008-2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef USB_OPEN_H
#define USB_OPEN_H
#include <stdint.h>
#include <usb.h>
usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product);
void parse_usb_id(const char *id);
#endif /* !USB_OPEN_H */