LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-24-2007, 07:57 PM   #1
aegolden
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
Need help porting an open source driver to Linux


I'm trying to develop a (user space) Linux driver for one of my company's products, the bTop-2:

http://perfsci.com/hardware.htm

We currently have software to control the device in Mac OS X and in Windows. All of the software is open source and available from our website:

http://perfsci.com/btop_software.htm

Sorry for the long introduction. Now here's my problem:

I've been partially successful in porting the code in our Windows development kit to Linux. I can, from user space, discover the board when it's plugged in, set a configuration, claim an interface, and set an alternative setting. I can even write to the bulk out endpoints using:

Code:
  struct usb_bulktransfer bulk;
  int ret, sent = 0;
  do{
    bulk.ep = EP & BTOP_EP_MASK;
    bulk.len = *bufferSize - sent;
    bulk.timeout = 5000;
    bulk.data = (unsigned char*)buffer + sent;
    ret = ioctl(m_handle, IOCTL_USB_BULK, &bulk);
    if(ret < 0){
      printf("error (%d) (transferData): %s\n", ret, strerror(errno));
      return ret;
    }
    sent += ret;
  }while(ret > 0 && sent < *bufferSize);
But if I try to use this same function to read from a bulk in endpoint I get an "Operation timed out" error every single time. (I've tried extending the timeout to a full minute, and it really shouldn't take more than a fraction of a second anyway). I'm pretty sure the endpoints are right since I get a different error (no such file) if I use a nonsense endpoint. My one other endpoint related clue comes from libusb. I tried using a function from that library:

Code:
ret = usb_bulk_read(dev_handle, EP & BTOP_EP_MASK,
                        (char*)buffer, *bufferSize, 1000);
where dev_handle was set up according to the usual libusb method.

Code:
ret = usb_bulk_write(dev_handle, EP & BTOP_EP_MASK,
                        (char*)buffer, *bufferSize, 1000);
does work with the appropriate out endpoints, but when I try to use a bulk in endpoint with the usb_bulk_read command, I get a "No such file" error. It turns out that in the usb_bulk_read function, the input endpoint number is OR'd with USB_ENDPOINT_IN (0x80), so

(EP & BTOP_EP_MASK) becomes ((EP & BTOP_EP_MASK) | USB_ENDPOINT_IN), which is not a valid endpoint number for our device. So does this mean that the endpoint numbers from the device are somehow fundamentally incompatible with Linux? That seems very unlikely to me, but I don't have much experience with the Linux USB system.

I'm opening the device with:

m_handle = open(path_to_device, O_RDWR | O_APPEND)

and I'm running the test code as root so I know it's not a permissions issue. Any ideas?

If anyone else would like to take a crack at this port let me know and I'll send you all my test code. (I might even be able to get you a free bTop). As I mentioned above, all of our bTop related code for Windows and Mac OS X is already open source. I've looked at a bunch of example code for controlling USB devices from user space either with ioctl calls or with libusb, but those examples were all mysteriously lacking bulk reads.
 
Old 04-24-2007, 09:42 PM   #2
J.W.
LQ Veteran
 
Registered: Mar 2003
Location: Milwaukee, WI
Distribution: Mint
Posts: 6,642

Rep: Reputation: 69
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-24-2007, 10:49 PM   #3
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Precise voltage control, and voltage measuring. Input/Output. This device looks delicious. You could interface it with anything that can be controlled by electricity. It looks like it could have interesting analogue purposes too as well. Very exciting. All from the convenience of your keyboard. I want. I want.

Actually I do have one simple idea. Seeing as how libusb is cross platform, you could attempt to try the exact same thing you are doing on the Mac. And if it also fails to work on the Mac, you could atleast verify that the problem is with the way you are using libusb or possibly even a bug in libusb itself. Could be a rare case scenario where it theres something about your device it doesn't like.
Well either way, time to load up good ol' gdb.

Last edited by slantoflight; 04-24-2007 at 10:59 PM.
 
Old 04-25-2007, 06:09 AM   #4
aegolden
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Solved (I'm pretty sure anyway)

So I finally stepped through the bulk read process (I can't believe I didn't do this right away). I was reading from the bulk in endpoint in order to get a structure for which I wouldn't know the type until I saw its first byte. So since the various potential structures had different sizes I tried to read the maximum possible (64 bytes) from the endpoint. When all the data were read the last call to ioctl always timed out!

So all I had to do was *ignore* the timeout error as long as I had received some data, and it looked sensible. Basically, if I get that first byte and it's one of a few magic ID numbers then I'm happy. In reality I don't want to deal with any timeouts so I'll have to make the read function a little smarter -- maybe after getting the first byte I'll reset the expected data size to match the size of the structure that I now expect to receive. I'm pretty sure that I never want to see more than one of the structures come down the pipe at any given time, but I'll have to check .

So after I clean up the code and do some more testing I'll put together a neat little package for controlling the bTop-2 under Linux and make it available with the rest of my company's free software.
 
Old 04-25-2007, 08:43 AM   #5
reverse
Member
 
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
Quote:
maybe after getting the first byte I'll reset the expected data size to match the size of the structure that I now expect to receive.
That's what I'd do. Good luck with further development.
 
Old 04-26-2007, 05:27 PM   #6
aegolden
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Talking Software posted

I've posted a bTop-2 development disk for Linux alongside our development disks for Mac OS X, and Windows:

http://perfsci.com/btop_software.htm#dev_tools_linux

The bTop-2 should now be just as usable under Linux as it is under Windows. Huzzah!
 
Old 04-29-2007, 03:13 AM   #7
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Hate to rain on your parade, but your code contains memory leaks and potential buffer overflows (sprintf). Consider using the std C++ template container library and RAII techniques to clean things up.
 
Old 04-29-2007, 12:35 PM   #8
aegolden
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ugenn
Hate to rain on your parade, but your code contains memory leaks and potential buffer overflows (sprintf). Consider using the std C++ template container library and RAII techniques to clean things up.
I replaced the sprintf calls with snprintf's, and added a few more calls to free (I'm pretty sure that every malloc has a matching free now). Let me know if you find any more errors or security issues.
 
Old 05-04-2007, 09:30 AM   #9
ray.shashi
LQ Newbie
 
Registered: May 2007
Posts: 5

Rep: Reputation: 0
Unhappy i too need help for my final year project on usb read/write on pen drive

Hi Sir,
Congrats that your problem of read operation is solved.we people are doing our final year project on read/write operation(user space) on a pendrive.we have reached a stage where we can configure the device.now we are trying to attempt usb_bulk_read/write but we are not getting the result.we are using same library like libusb as yours.can u please guide us how shall we proceed.i wd like to inform you that we are running out of time as submission date is close by.
we wd be greatly thankful for your kind help.
 
Old 05-30-2007, 08:30 AM   #10
asprakash
Member
 
Registered: May 2006
Location: Chennai, India
Posts: 74
Blog Entries: 3

Rep: Reputation: 18
I tried to read/write the pen drive by using Libusb. Then I posted my query in Libusb mailing list. I got -ve reply.
ppl suggested that, Libusb cant able to read/write data to/from pen drive.For handling pen drive data, we need file system. Libusb cant do that.
any corrections?
 
  


Reply

Tags
driver, porting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
The Open Source Hook: Porting KDE to Mac and Windows Sharkscott LQ Articles Discussion 0 02-21-2007 01:39 PM
LXer: The Open Source Hook: Porting KDE to Mac and Windows LXer Syndicated Linux News 0 01-31-2007 07:21 AM
Any open source USB mouse driver for Linux malikamirshehzad Linux - Software 2 02-07-2006 12:17 PM
porting driver from linux to vxworks r9379 Linux - Software 0 08-23-2005 02:46 AM
driver porting to 64-bit-linux yogeshwar_s Programming 0 07-19-2005 09:18 AM


All times are GMT -5. The time now is 09:14 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration