当前位置: 技术问答>linux和unix
怎样利用linux的系统接口来操作USB设备
来源: 互联网 发布时间:2016-05-11
本文导语: 我想用linux的标准接口来操作USB设备,就像USB驱动的测试程序一样,能够读取USB的属性, 问一下怎么操作,要用到linux哪些系统接口,哪个做过的大虾帮忙一下,DX。。 | ...
我想用linux的标准接口来操作USB设备,就像USB驱动的测试程序一样,能够读取USB的属性,
问一下怎么操作,要用到linux哪些系统接口,哪个做过的大虾帮忙一下,DX。。
|
链接: http://libusb.wiki.sourceforge.net/
|
/* armtools/src/usbdl.h
*
* USB Download Tool,base on dltool.c by Ben Dooks
* copyright (c) 2007 thexin chan
*
* 2007-10-24 create version.
*/
#ifndef __USBDL_H_
#define __USBDL_H_
#include
#include
#include
#include
#include
#include
#include
#include
/* usb downloader struct */
/* ep:which ep used to download*/
struct usb_down_t {
char *name;
int id_vendor;
int id_product;
struct usb_bus *bus;
struct usb_bus *busses;
struct usb_device *device;
usb_dev_handle *devh;
/*which ep used for download*/
int ep;
/*
* private data for this struct.
* you can give some data into here like address,size...
* or some struct variable.
*/
void *priv_data;
/*
* transmit file,chars sended will return.
*/
unsigned long (*transmit_file)(struct usb_down_t *usbdl,const char *fname);
};
int usbdl_init(struct usb_down_t *usbdl);
int usbdl_transmit(struct usb_down_t *usbdl,unsigned char *data,unsigned long size);
#endif /*__USBDL_H_*/
/* armtools/src/usbdl.c
*
* copyright (c) thexin chan
*
* 2007-10-24 create version.
*/
#include
#include
#include
#include "usbdl.h"
#include "debug.h"
static void print_usb_device(struct usb_device *dev);
/*
* check the usb device is match the vendor and product id,
* return 0:invalid 1:valid
*/
static int usbdl_verify_device(int vendor, int product,struct usb_device *dev)
{
if (dev->descriptor.bNumConfigurations != 1)
return 0;
/*debug information*/
print_usb_device(dev);
if (dev->descriptor.idVendor == vendor &&
dev->descriptor.idProduct == product)
return 1;
return 0;
}
/*
* match the device on bus with id_vendor,and id_product.
* return NULL: no match ,usb_device*: match.
*/
static struct usb_device* usbdl_match_bus(int vendor,int product,struct usb_bus *bus)
{
struct usb_device *dev;
dprintk("bus %p: dirname %sn", bus, bus->dirname);
for(dev=bus->devices;dev!=NULL;dev=dev->next){
dprintk("dev %p filename %sn",dev,dev->filename);
if(!usbdl_verify_device(vendor,product,dev))
continue;
return dev;
}
return NULL;
}
/*
* match the usb_down_t
* set usbdl->device,usbdl->bus to first valid device and bus.
* return 0:matched,busses;pbus!=NULL;pbus=pbus->next){
pdev = usbdl_match_bus(usbdl->id_vendor,usbdl->id_product,pbus);
if(pdev!=NULL){
usbdl->device = pdev;
usbdl->bus = pbus;
dprintk("matched: bus %p, device %pn", pbus, pdev);
return 0;
}
}
usbdl->device = NULL;
usbdl->bus = NULL;
return -1;
}
int usbdl_init(struct usb_down_t *usbdl)
{
usb_init();
usb_find_busses();
usb_find_devices();
usbdl->busses = usb_get_busses();
return usbdl_match(usbdl);
}
/*
* transmit data to usb_device 's ep.
* return bytes writed,or busses)
usbdl_init(usbdl);
if(!usbdl->device || !usbdl->bus){
dprintk("no matched usb device,id_vendor=%d,id_product=%dn",
usbdl->id_vendor,usbdl->id_product);
return -1;
}
usbdl->devh = usb_open(usbdl->device);
if(!usbdl->devh){
dprintk("usb_open error: %sn",strerror(errno));
return -2;
}
if(usb_claim_interface(usbdl->devh,0)devh);
return -3;
}
ret = usb_bulk_write(usbdl->devh,usbdl->ep,(void*)data,size,5*1000*1000);
if(ret != size){
dprintk("usb_bulk_write request write %ld bytes,but %ld writed : %sn",
size,ret,strerror(errno));
}else
dprintk("usb_bulk_write success write %ld bytes datan",ret);
usb_release_interface(usbdl->devh,0);
usb_close(usbdl->devh);
usbdl->devh = 0;
return 0;
}
/*print functions for print usb configurations*/
void print_endpoint(struct usb_endpoint_descriptor *endpoint)
{
dprintk(" bEndpointAddress: %02xhn", endpoint->bEndpointAddress);
dprintk(" bmAttributes: %02xhn", endpoint->bmAttributes);
dprintk(" wMaxPacketSize: %dn", endpoint->wMaxPacketSize);
dprintk(" bInterval: %dn", endpoint->bInterval);
dprintk(" bRefresh: %dn", endpoint->bRefresh);
dprintk(" bSynchAddress: %dn", endpoint->bSynchAddress);
}
void print_altsetting(struct usb_interface_descriptor *interface)
{
int i;
dprintk(" bInterfaceNumber: %dn", interface->bInterfaceNumber);
dprintk(" bAlternateSetting: %dn", interface->bAlternateSetting);
dprintk(" bNumEndpoints: %dn", interface->bNumEndpoints);
dprintk(" bInterfaceClass: %dn", interface->bInterfaceClass);
dprintk(" bInterfaceSubClass: %dn", interface->bInterfaceSubClass);
dprintk(" bInterfaceProtocol: %dn", interface->bInterfaceProtocol);
dprintk(" iInterface: %dn", interface->iInterface);
for (i = 0; i bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
void print_interface(struct usb_interface *interface)
{
int i;
for (i = 0; i num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
void print_configuration(struct usb_config_descriptor *config)
{
int i;
dprintk(" wTotalLength: %dn", config->wTotalLength);
dprintk(" bNumInterfaces: %dn", config->bNumInterfaces);
dprintk(" bConfigurationValue: %dn", config->bConfigurationValue);
dprintk(" iConfiguration: %dn", config->iConfiguration);
dprintk(" bmAttributes: %02xhn", config->bmAttributes);
dprintk(" MaxPower: %dn", config->MaxPower);
for (i = 0; i bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
void print_device_desc(struct usb_device_descriptor *desc)
{
dprintk("t=> bNumConfigurations %dn",desc->bNumConfigurations);
dprintk("t=> bLength %dn", desc->bLength);
dprintk("t=> bType %dn", desc->bDescriptorType);
dprintk("t=> bcdUSB %xn", desc->bcdUSB);
dprintk("t=> idVendor %xn", desc->idVendor);
dprintk("t=> idProduct %xn", desc->idProduct);
}
void print_usb_device(struct usb_device *dev)
{
int index;
print_device_desc(&dev->descriptor);
for(index=0;indexdescriptor.bNumConfigurations;index++)
print_configuration(&dev->config[index]);
}
*
* USB Download Tool,base on dltool.c by Ben Dooks
* copyright (c) 2007 thexin chan
*
* 2007-10-24 create version.
*/
#ifndef __USBDL_H_
#define __USBDL_H_
#include
#include
#include
#include
#include
#include
#include
#include
/* usb downloader struct */
/* ep:which ep used to download*/
struct usb_down_t {
char *name;
int id_vendor;
int id_product;
struct usb_bus *bus;
struct usb_bus *busses;
struct usb_device *device;
usb_dev_handle *devh;
/*which ep used for download*/
int ep;
/*
* private data for this struct.
* you can give some data into here like address,size...
* or some struct variable.
*/
void *priv_data;
/*
* transmit file,chars sended will return.
*/
unsigned long (*transmit_file)(struct usb_down_t *usbdl,const char *fname);
};
int usbdl_init(struct usb_down_t *usbdl);
int usbdl_transmit(struct usb_down_t *usbdl,unsigned char *data,unsigned long size);
#endif /*__USBDL_H_*/
/* armtools/src/usbdl.c
*
* copyright (c) thexin chan
*
* 2007-10-24 create version.
*/
#include
#include
#include
#include "usbdl.h"
#include "debug.h"
static void print_usb_device(struct usb_device *dev);
/*
* check the usb device is match the vendor and product id,
* return 0:invalid 1:valid
*/
static int usbdl_verify_device(int vendor, int product,struct usb_device *dev)
{
if (dev->descriptor.bNumConfigurations != 1)
return 0;
/*debug information*/
print_usb_device(dev);
if (dev->descriptor.idVendor == vendor &&
dev->descriptor.idProduct == product)
return 1;
return 0;
}
/*
* match the device on bus with id_vendor,and id_product.
* return NULL: no match ,usb_device*: match.
*/
static struct usb_device* usbdl_match_bus(int vendor,int product,struct usb_bus *bus)
{
struct usb_device *dev;
dprintk("bus %p: dirname %sn", bus, bus->dirname);
for(dev=bus->devices;dev!=NULL;dev=dev->next){
dprintk("dev %p filename %sn",dev,dev->filename);
if(!usbdl_verify_device(vendor,product,dev))
continue;
return dev;
}
return NULL;
}
/*
* match the usb_down_t
* set usbdl->device,usbdl->bus to first valid device and bus.
* return 0:matched,busses;pbus!=NULL;pbus=pbus->next){
pdev = usbdl_match_bus(usbdl->id_vendor,usbdl->id_product,pbus);
if(pdev!=NULL){
usbdl->device = pdev;
usbdl->bus = pbus;
dprintk("matched: bus %p, device %pn", pbus, pdev);
return 0;
}
}
usbdl->device = NULL;
usbdl->bus = NULL;
return -1;
}
int usbdl_init(struct usb_down_t *usbdl)
{
usb_init();
usb_find_busses();
usb_find_devices();
usbdl->busses = usb_get_busses();
return usbdl_match(usbdl);
}
/*
* transmit data to usb_device 's ep.
* return bytes writed,or busses)
usbdl_init(usbdl);
if(!usbdl->device || !usbdl->bus){
dprintk("no matched usb device,id_vendor=%d,id_product=%dn",
usbdl->id_vendor,usbdl->id_product);
return -1;
}
usbdl->devh = usb_open(usbdl->device);
if(!usbdl->devh){
dprintk("usb_open error: %sn",strerror(errno));
return -2;
}
if(usb_claim_interface(usbdl->devh,0)devh);
return -3;
}
ret = usb_bulk_write(usbdl->devh,usbdl->ep,(void*)data,size,5*1000*1000);
if(ret != size){
dprintk("usb_bulk_write request write %ld bytes,but %ld writed : %sn",
size,ret,strerror(errno));
}else
dprintk("usb_bulk_write success write %ld bytes datan",ret);
usb_release_interface(usbdl->devh,0);
usb_close(usbdl->devh);
usbdl->devh = 0;
return 0;
}
/*print functions for print usb configurations*/
void print_endpoint(struct usb_endpoint_descriptor *endpoint)
{
dprintk(" bEndpointAddress: %02xhn", endpoint->bEndpointAddress);
dprintk(" bmAttributes: %02xhn", endpoint->bmAttributes);
dprintk(" wMaxPacketSize: %dn", endpoint->wMaxPacketSize);
dprintk(" bInterval: %dn", endpoint->bInterval);
dprintk(" bRefresh: %dn", endpoint->bRefresh);
dprintk(" bSynchAddress: %dn", endpoint->bSynchAddress);
}
void print_altsetting(struct usb_interface_descriptor *interface)
{
int i;
dprintk(" bInterfaceNumber: %dn", interface->bInterfaceNumber);
dprintk(" bAlternateSetting: %dn", interface->bAlternateSetting);
dprintk(" bNumEndpoints: %dn", interface->bNumEndpoints);
dprintk(" bInterfaceClass: %dn", interface->bInterfaceClass);
dprintk(" bInterfaceSubClass: %dn", interface->bInterfaceSubClass);
dprintk(" bInterfaceProtocol: %dn", interface->bInterfaceProtocol);
dprintk(" iInterface: %dn", interface->iInterface);
for (i = 0; i bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
void print_interface(struct usb_interface *interface)
{
int i;
for (i = 0; i num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
void print_configuration(struct usb_config_descriptor *config)
{
int i;
dprintk(" wTotalLength: %dn", config->wTotalLength);
dprintk(" bNumInterfaces: %dn", config->bNumInterfaces);
dprintk(" bConfigurationValue: %dn", config->bConfigurationValue);
dprintk(" iConfiguration: %dn", config->iConfiguration);
dprintk(" bmAttributes: %02xhn", config->bmAttributes);
dprintk(" MaxPower: %dn", config->MaxPower);
for (i = 0; i bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
void print_device_desc(struct usb_device_descriptor *desc)
{
dprintk("t=> bNumConfigurations %dn",desc->bNumConfigurations);
dprintk("t=> bLength %dn", desc->bLength);
dprintk("t=> bType %dn", desc->bDescriptorType);
dprintk("t=> bcdUSB %xn", desc->bcdUSB);
dprintk("t=> idVendor %xn", desc->idVendor);
dprintk("t=> idProduct %xn", desc->idProduct);
}
void print_usb_device(struct usb_device *dev)
{
int index;
print_device_desc(&dev->descriptor);
for(index=0;indexdescriptor.bNumConfigurations;index++)
print_configuration(&dev->config[index]);
}