USB thread problem

bridged with qdn.public.ddk.usb
Post Reply
Markus Lavin

USB thread problem

Post by Markus Lavin » Mon Sep 10, 2001 8:51 am

Hi all,

We're having some minor problems with the USB stack. The code included below
should illustrate the problem.

There are actually two issues with this code, they may be related however.
As we can see, main registers two callback functions, one for insertion and
one for removal, main shall then wait in a while(1) loop doing nothing. Here
the first problem arises, when we get the insertion callback and start doing
usb calls in it (to get the device descriptor) the program crashes on a
SIGSEGV signal. This can however be temporarily fixed by replacing the while
(1); loop with while(1) sleep(50); and the program will not crash until the
50 seconds have elapsed.

The other issue is the thread that is created from the insertion callback
with tfork(), when this thread has exited, the program will crash in the
removal callback when the device is later removed. This does not happen if
the thread does not exit, but instead sleeps forever.

It is not obvious to us why this happens, so any help would be highly
apreciated.
We're running QNX 4.25 if that may be of any interest.

best regards
/Markus

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include <errno.h>
#include <process.h>
#include <sys/usbdi.h>
#include <sys/kernel.h>

void thread(void *arg)
{
printf("This is the thread\n");
exit(0);
}

void insertion_callback(struct usbd_connection *connection,
usbd_device_instance_t *instance)

{
int status;
struct usbd_device *dev;
usbd_device_descriptor_t *dev_desc;
struct usb_desc_node *desc_node;
char *stack;


printf("insertion, about to do attach()\n");
status = usbd_attach(connection, instance, 0, &dev);
assert(status == EOK);

dev_desc = usbd_device_descriptor(dev, &desc_node);
assert(status == EOK);

printf("Vendor ID : 0x%04X\n", dev_desc->idVendor);
printf("Device ID : 0x%04X\n", dev_desc->bcdDevice);
printf("Product ID: 0x%04X\n", dev_desc->idProduct);

#if 1
stack = malloc(4096);
assert(stack != NULL);
status = tfork(stack, 4096, thread, NULL, 0);
assert(status != -1);
#endif
}

void removal_callback(struct usbd_connection *connection,
usbd_device_instance_t *instance)
{
struct usbd_device *dev;
int status;
printf("removal_callback\n");

dev = usbd_device_lookup(connection, instance);
assert(dev != NULL);
status = usbd_detach(dev);
assert(status == EOK);
}

int main(int argc, char **argv)
{
int status;
static usbd_connect_parm_t parm;
static usbd_funcs_t funcs;
static usbd_device_ident_t interest;
struct usbd_connection *connection;

funcs.nentries = _USBDI_NFUNCS;
funcs.insertion = insertion_callback;
funcs.removal = removal_callback;
funcs.event = NULL;

interest.vendor = USBD_CONNECT_WILDCARD;
interest.device = USBD_CONNECT_WILDCARD;
interest.class = USBD_CONNECT_WILDCARD;
interest.subclass = USBD_CONNECT_WILDCARD;
interest.protocol = USBD_CONNECT_WILDCARD;

parm.path = NULL;
parm.vusb = USB_VERSION;
parm.vusbd = USBD_VERSION;
parm.flags = 0;
parm.argc = 0;
parm.argv = NULL;
parm.evtbufsz = 0;
parm.ident = &interest;
parm.funcs = &funcs;
parm.connect_wait = USBD_CONNECT_WAIT;

status = usbd_connect(&parm, &connection);
assert(status == EOK);

#if FAST_CRASH
while (1);
#else
while (1)sleep(50);
#endif
return 0;
}

Lung Han

Re: USB thread problem

Post by Lung Han » Thu Sep 13, 2001 10:27 pm

"Markus Lavin" <markus.lavin@gambro.com> wrote in message
news:9nhuv3$oja$1@nntp.qnx.com...
Hi all,

We're having some minor problems with the USB stack. The code included
below
should illustrate the problem.

There are actually two issues with this code, they may be related however.
As we can see, main registers two callback functions, one for insertion
and
one for removal, main shall then wait in a while(1) loop doing nothing.
Here
the first problem arises, when we get the insertion callback and start
doing
usb calls in it (to get the device descriptor) the program crashes on a
SIGSEGV signal. This can however be temporarily fixed by replacing the
while
(1); loop with while(1) sleep(50); and the program will not crash until
the
50 seconds have elapsed.

The other issue is the thread that is created from the insertion callback
with tfork(), when this thread has exited, the program will crash in the
removal callback when the device is later removed. This does not happen if
the thread does not exit, but instead sleeps forever.

It is not obvious to us why this happens, so any help would be highly
apreciated.
We're running QNX 4.25 if that may be of any interest.

best regards
/Markus

#include <stdlib.h
#include <stdio.h
#include <assert.h

#include <errno.h
#include <process.h
#include <sys/usbdi.h
#include <sys/kernel.h

void thread(void *arg)
{
printf("This is the thread\n");
exit(0);
}

void insertion_callback(struct usbd_connection *connection,
usbd_device_instance_t *instance)

{
int status;
struct usbd_device *dev;
usbd_device_descriptor_t *dev_desc;
struct usb_desc_node *desc_node;
char *stack;


printf("insertion, about to do attach()\n");
status = usbd_attach(connection, instance, 0, &dev);
assert(status == EOK);

dev_desc = usbd_device_descriptor(dev, &desc_node);
assert(status == EOK);

printf("Vendor ID : 0x%04X\n", dev_desc->idVendor);
printf("Device ID : 0x%04X\n", dev_desc->bcdDevice);
printf("Product ID: 0x%04X\n", dev_desc->idProduct);

#if 1
stack = malloc(4096);
assert(stack != NULL);
status = tfork(stack, 4096, thread, NULL, 0);
assert(status != -1);
#endif
}

void removal_callback(struct usbd_connection *connection,
usbd_device_instance_t *instance)
{
struct usbd_device *dev;
int status;
printf("removal_callback\n");

dev = usbd_device_lookup(connection, instance);
assert(dev != NULL);
status = usbd_detach(dev);
assert(status == EOK);
}

int main(int argc, char **argv)
{
int status;
static usbd_connect_parm_t parm;
static usbd_funcs_t funcs;
static usbd_device_ident_t interest;
struct usbd_connection *connection;

funcs.nentries = _USBDI_NFUNCS;
funcs.insertion = insertion_callback;
funcs.removal = removal_callback;
funcs.event = NULL;

interest.vendor = USBD_CONNECT_WILDCARD;
interest.device = USBD_CONNECT_WILDCARD;
interest.class = USBD_CONNECT_WILDCARD;
interest.subclass = USBD_CONNECT_WILDCARD;
interest.protocol = USBD_CONNECT_WILDCARD;

parm.path = NULL;
parm.vusb = USB_VERSION;
parm.vusbd = USBD_VERSION;
parm.flags = 0;
parm.argc = 0;
parm.argv = NULL;
parm.evtbufsz = 0;
parm.ident = &interest;
parm.funcs = &funcs;
parm.connect_wait = USBD_CONNECT_WAIT;

status = usbd_connect(&parm, &connection);
assert(status == EOK);

#if FAST_CRASH
while (1);
#else
while (1)sleep(50);
#endif
return 0;
}


Lung Han

to set up a usb printer

Post by Lung Han » Thu Sep 13, 2001 11:11 pm

Hi all,

I have tied to setup a Epson sc670 printer to QNX RTP 6.1 and failed. Please
kindly let me know what are the steps to
have the printer printing.

I have done "devu-uhci &" and have the directory /dev/usb/ created.
Then I tried "devu-prn &", but /dev/usbprn0/ is not created.
I also do spooler -d/dev/usbprn0/ -nUSBPRT -cepson.cfg. The printer is
displayed in the spooler mgr, but nothing gets printing.

one more thing I have done is I created a /etc/rc.d/rc.local file and added
two commands to this file
devu-uhci
/sbin/devu-prn

Please let me know what i have done wrong and what i have missing.

Thanks a lot.

lung

Post Reply

Return to “qdn.public.ddk.usb”