USB driver. Bulk transfer! Endpoint trouble!

Hi!
I’m a newbie in USB and drivers this is my first this kind project so
please don’t judge my hard, if I’ve made any silly mistake. :slight_smile:
I’ve USB device (mp3 player) and i would like to write or read data
from this device in QNX (6.2.1)! As i know it is a mass device so i
use Bulk transfer. I install devu-ohci and I get this
conf:

usb -tvvv

USB (OHCI) v1.10, v1.01 DDK
Control, Interrupt, Bulk, Isoch, Low speed, High speed

Device Address : 1
Vendor : 0x10d6
Product : 0x1100 (USB 2.0(FS) FLASH DISK )
Device Release : r1.00
USB Spec Release : v1.10
Serial Number : USB 2.0(FS) FLASH DISK
Class : 0x00 (Independant per interface)
Max PacketSize0 : 8
Languages : 0x0409 (English)
Current Frame : 32160 (1024 bytes)
Configurations : 1

  • Configuration : 1
    | Attributes : 0x80 (Bus-powered)
    | Max Power : 100 mA
    | Interfaces : 1
    | + Interface : 0 / 0
    | | Class : 0x08 (Mass Storage)
    | | Subclass : 0x05 (SFF8070i)
    | | Protocol : 0x50
    | | Endpoints : Control + 2
    | | + Endpoint : 0
    | | | Attributes : Control
    | | | Max Packet Size: 8
    | | + Endpoint : 1
    | | | Attributes : Bulk/OUT
    | | | Max Packet Size: 64
    | | + Endpoint : 2
    | | | Attributes : Bulk/IN
    | | | Max Packet Size: 64

[b:ed6ab268fb]I searched and I’ve found few pieces of code mainly on
this page so I get it together in a simple
applicaton:[/b:ed6ab268fb]

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/usbdi.h>
#include <errno.h>
#include <string.h>
#include <iostream.h>
#include <unistd.h>
#include <pthread.h>

#define VENDOR 0x10d6
#define PRODUCT 0x1100
#define CLASS 0x08
#define SUBCLASS 0x05
#define PROTOCOL 0x50

struct usbd_urb *link_ctr=NULL;
struct usbd_urb *urb_ctr=NULL;
struct usbd_pipe *pipe_ctr=NULL;

usbd_endpoint_descriptor_t *ep0;

usbd_configuration_descriptor_t *conf=NULL;
usbd_interface_descriptor_t *idesc=NULL;
usbd_device_descriptor_t *ddesc=NULL;

struct usbd_desc_node *node=NULL;
struct usbd_connection *connection;
usbd_device_instance_t instance;
int attach_handle;
struct usbd_device *device;
int error;

unsigned char *usbd_ctr;

////////////////////////////////////////////conf params
//////////////////////////////////////////
int bConfigurationValue = 1;
int bInterfaceNumber = 0;
int bAlternateSetting = 0;



_uint32 status;
_uint32 len;



void dc_bulk_cbf( struct usbd_urb *urb, struct usbd_pipe *pipe,
void *hdl )
{
pipe = pipe, hdl = hdl;
int i;
i = usbd_urb_status( urb, &status, &len );
printf(“i=%d, status=%x, len=%d\n”,i,status,len);

pthread_sleepon_lock( );
pthread_sleepon_signal( urb );
pthread_sleepon_unlock( );
return;
}

int dc_bulk_wait( struct usbd_urb *urb, struct usbd_pipe
*pipe) {
_uint32 ustatus;
_uint32 alen;
int status;

pthread_sleepon_lock( );
while( ( status = usbd_urb_status( urb, &ustatus,
&alen )) == EBUSY ) {
pthread_sleepon_wait( urb );
}
pthread_sleepon_unlock( );
return(alen);
}



/////////////////////////////////////////////////////////Insertion///////////////////////////////////////////////////////////////////
void insertion(struct usbd_connection *usb_connection,
usbd_device_instance_t *usb_instance)
{
printf (“DEVICE INSERTED\n”);

if (attach_handle == EOK)
{
printf(“DEVICE ATTACHED successfully\n”);

}
else if (attach_handle == ENODEV )
{
printf(“Error - ENODEV\n”);
}
else if (attach_handle == EBUSY )
{
printf(“Error - EBUSY\n”);
}
else if (attach_handle == ENOMEM )
{
printf(“Error - ENOMEM\n”);
}
else
{
printf(“FAILURE %d\n”, attach_handle);
}
return;
}


///////////////////////////////////////////main///////////////////////////////////////////////////////////////////////
int main()
{

int argc;
char **argv;

void (*removal)(struct usbd_connection *,
usbd_device_instance_t *instance);
// i didnt write this yet but i think this is not the couse of the
problem i manualy detach device at the end


usbd_device_ident_t interest = {
VENDOR,
PRODUCT,
CLASS,
SUBCLASS,
PROTOCOL,
//USBD_CONNECT_WILDCARD,
//USBD_CONNECT_WILDCARD,
//USBD_CONNECT_WILDCARD,
};

usbd_funcs_t funcs = {
_USBDI_NFUNCS,
insertion,
removal,
NULL
};



usbd_connect_parm_t cparms = {
NULL,
USB_VERSION,
USBD_VERSION,
0,
argc,
argv,
0,
&interest,
&funcs
};

//////////////////////////////////////Connection/////////////////////////////////////////////////

error = usbd_connect(&cparms, &connection);

/////////////////////////////////////Instance
params///////////////////////////////////////////
/*
instance.path = 0;
instance.devno = 1;
instance.generation;
instance.ident = interest;
instance.config = 1;
instance.iface = 0;
instance.alternate = 0;
*/
/////////////////////////////Instance////////////////////////////////////////////////////////////////////////////////
int devno;
for (devno = 0; devno < 64; ++devno)
{
memset(&instance, USBD_CONNECT_WILDCARD,
sizeof(usbd_device_instance_t));
instance.path = 0, instance.devno = devno;

if(usbd_attach(connection, &instance,0,
&device)==EOK)
{
printf(“Numbet of dev: %d
\n”,instance.devno);
}
}

//////////////////////////Device
descriptor////////////////////////////////////////////////////

ddesc = usbd_device_descriptor(device,&node );
if (ddesc == NULL )
{
printf("parsing error ");
} else {
printf(“DEV DESCRIPTORS ok \n”);
}


///////////////////////////////Buffor//////////////////////////////////////////////////////////////

usbd_ctr= (unsigned char* )usbd_alloc( 64 );
if (usbd_ctr==NULL )
{
printf("unable to allocate buffer usbd ");
} else {
printf(“USBD_CTR BUFFER success \n”);
}
memset(usbd_ctr,0,sizeof(usbd_ctr));


///////////////////////////////////////////////Conf
descriptor////////////////////////////////////

conf = usbd_configuration_descriptor(device, bConfigurationValue,
&node);
if (conf == NULL)
{
printf("parsing error ");
} else {
printf(“CONF DESCRIPTOR ok \n”);
}

/////////////////////////////////////////////////Interface
descriptor//////////////////////////////

idesc =
usbd_interface_descriptor(device,bConfigurationValue,bInterfaceNumber,bAlternateSetting,&node);
if (idesc == NULL )
{
printf("parsing error ");
} else {
printf(“INTF DESCRIPTOR ok \n”);
}


////////////////////////////////////////endpoint
descriptors//////////////////////////////////////////

ep0 = usbd_endpoint_descriptor( device, 1,0,0,0x01, &node
);
if(ep0==0) { cout <<
“usbd_endpoint_descriptor(ep0) error”; throw
errno; }


////////////////////////////////////select int
&conf//////////////////////////////////////////////////////
if (usbd_select_interface(device, bInterfaceNumber,
bAlternateSetting ) == EOK)
{
printf(“INTERFACE SELECTED! ok \n”);
} else {
printf("parsing error ");
}

if ( usbd_select_config(device, bConfigurationValue ) ==
EOK)
{
printf(“CONFIGURATION SELECTED ok \n”);
} else {
printf("parsing error ");
}

/////////////////////////////////////////////////URB/////////////////////////////////////////

urb_ctr = usbd_alloc_urb(link_ctr);
if (urb_ctr == NULL)
{
printf("unable to allocate URB ");
} else {
printf(“URB SUCCESS \n”);
}


///////////////////////////////////////////////////open
PIPE////////////////////////////////////

usbd_descriptors_t _ep0 =
reinterpret_cast<usbd_descriptors_t
>(ep0);

int r;
r = usbd_open_pipe(device,_ep0, &pipe_ctr);
if(r!=EOK) { cout <<
“usbd_open_pipe(ep0) error”; throw r; }


//////////////////////////DATA
//////////////////////////////////////////////////////////////////

char * my_data_1 = “DANE”;

////////////////////////////////////////////////////////////////////////////////////////

int i;
i = usbd_urb_status( urb_ctr, &status, &len );
printf(“i=%d, status=%x, len=%d\n”,i,status,len);

memcpy(usbd_ctr,my_data_1, strlen(my_data_1));
cout <<usbd_ctr<<endl;


for (int c=0;c<1;c++)
{

if ( usbd_setup_bulk(urb_ctr, URB_DIR_OUT,usbd_ctr, 64
)==EOK)
{
printf(“BULK SETUP! ok \n”);
} else {
printf("parsing error ");
}

if ( usbd_io( urb_ctr, pipe_ctr, dc_bulk_cbf,
NULL,USBD_TIME_INFINITY ) == EOK) {
printf(“BULKO IO ok \n”);
} else {
printf("BulkShit! ");
}

dc_bulk_wait(urb_ctr,pipe_ctr);

}

//////////////////////////////////////free/////////////////////////////////////////////////////////
usbd_free_urb(urb_ctr);
usbd_free((unsigned char *)usbd_ctr);
usbd_close_pipe( pipe_ctr );

//////////////////detach&disconnect//////////////////////////////////////////////////////////////
if(usbd_detach(device) == EOK)
{
printf("detach ok!\n ");
}

if(usbd_disconnect(connection)==EOK)
{
printf("disconnect ok!\n ");
}

/////////////////end of
main///////////////////////////////////////////////////////////////
}

[b:ed6ab268fb]When i execute the program i’ve got:[/b:ed6ab268fb]

./nadaj

DEVICE INSERTED
DEVICE ATTACHED successfully
Numbet of dev: 1
DEV DESCRIPTORS ok
USBD_CTR BUFFER success
CONF DESCRIPTOR ok
INTF DESCRIPTOR ok
INTERFACE SELECTED! ok
CONFIGURATION SELECTED ok
URB SUCCESS
i=16, status=0, len=0
DANE
BULK SETUP! ok
BULKO IO ok
i=5, status=2000004, len=0
detach ok!
disconnect ok!

[b:ed6ab268fb]So everyting seems to be ok except endpoint i don’t know
what can be wrong I get the endpoint description from 1 endpoint
wchich is BULk_OUT end in usbd_setup_bulk I set URB_DIR_OUT so it
should be OK! It looks like the device is all the time BUSY “i =
usbd_urb_status” and i=16 (EBUSY) and in the end after usbd_io it
return i =5, EIO (error I/O) why? I’ve tied it with endpoint 0
(control) and 2 and the results are the same! the usbd_urb_status
allways return EBUSY! I’ve try to get a decriptors by
“usbd_parse_descriptors(device, node, USB_DESC_ENDPOINT,1,
&node);” and the effect is the same! :cry:

If anyone can help or give any clue here or on
my email I will be very grateful!

my email:
[color=blue:ed6ab268fb]gacek_mail@poczta.onet.pl[/color:ed6ab268fb][/b:ed6ab268fb]