LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2011, 09:23 AM   #1
zwiebelchen
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Rep: Reputation: 0
Unhappy Can't access usb device with libusb, crashes at usb_set_configuration()


Hi,

I'm new to linux development and I try to send a few bytes over USB to the JB media light manager (I think it doesn't matter which device it is).

My programm crashes when it comes to usb_set_configuration().
If I commit this method out (//) and let the programm invoke directly the usb_claim_interface() method it crashes, too.

I'm developing with Eclipse and I'm using the GCC C++ Compiler under Ubuntu 10.10 32bit.
lsusb tells me following:
...
Bus 005 Device 004: ID 16c0:0a32 VOTI
...

That's the device because disconnecting it makes the line disappear.

Can anyone help ?

Code:
#include <usb.h>
#include <stdio.h>
#include <iostream>

/* the device's vendor and product id */
#define MY_VID 0x16c0
#define MY_PID 0x0a32

/* the device's endpoints */
#define EP_IN 0x81
#define EP_OUT 0x01

#define BUF_SIZE 8

using namespace std;

struct usb_device *device;

usb_dev_handle *open_dev(void);

usb_dev_handle *open_dev(void)
{
  struct usb_bus *bus;

  for(bus = usb_get_busses(); bus; bus = bus->next)
    {
      for(device = bus->devices; device; device = device->next)
        {
          if(device->descriptor.idVendor == MY_VID
             && device->descriptor.idProduct == MY_PID)
            {
              return usb_open(device);
            }
        }
    }
  return NULL;
}

int main(void)
{
  usb_dev_handle *dev_handl = NULL; /* the device handle */
  char tmp[BUF_SIZE] = {0x05, 0x22, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00};

  usb_init(); /* initialize the library */
  usb_find_busses(); /* find all busses */
  usb_find_devices(); /* find all connected devices */


  if(!(dev_handl = open_dev()))
    {
      printf("error: device not found!\n");
      return 0;
    }

  usb_config_descriptor *config = device->config;
  usb_interface *interface = config->interface;
  usb_interface_descriptor *altsetting = interface->altsetting;

  if(usb_set_configuration(dev_handl, config->iConfiguration) < 0)
      {
       printf("error: setting config failed\n");
      usb_close(dev_handl);
      return 0;
   }

  if(usb_claim_interface(dev_handl, altsetting->bInterfaceNumber) < 0)
    {
      printf("error: claiming interface failed\n");
      usb_close(dev_handl);
      return 0;
    }

  if(usb_bulk_write(dev_handl, EP_OUT, tmp, sizeof(tmp), 5000)
     != sizeof(tmp))
    {
      printf("error: bulk write failed\n");
    }

  if(usb_bulk_read(dev_handl, EP_IN, tmp, sizeof(tmp), 5000)
     != sizeof(tmp))
    {
      printf("error: bulk read failed\n");
    }

  usb_release_interface(dev_handl, 0);
  usb_close(dev_handl);

  return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libusb for homemade device Rusttree Linux - Kernel 2 09-17-2010 01:51 AM
USB device driver notification using libusb v0.1 API marioatallah Linux - Hardware 1 01-19-2010 03:26 AM
Unable to read from my usb device using libusb on rhel4 bergy8 Linux - Hardware 2 10-13-2009 11:03 AM
device file for scanner with libusb fredgt Linux - Hardware 0 04-08-2004 10:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration