2

Using Windows I can easily communicate with my USB device using the following simplified code:

DWORD dwJunk;    // discard results from DeviceIOControl()
int   iReply;
char  cBuffer[100];
// cBuffer is initialized here.
HANDLE hDevice; // handle to the drive to be examined 
CString sDrive = _T(\\\\.\\H:); // drive H: for this test
hDevice = CreateFile(sDrive,            // drive to open
   GENERIC_READ | GENERIC_WRITE,        // read and write access to the drive
   FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode           
   NULL,                                // default security attributes
   OPEN_EXISTING,                       // disposition
   0,                                   // file attributes
   NULL);                               // do not copy file attributes

iReply = DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, &cBuffer, sizeof(cBuffer), &cBuffer, sizeof(cBuffer), &dwJunk, (LPOVERLAPPED)NULL);

I'm trying to do the same in linux but have not been able to figure out the ioctrl() parameters, or better put the structure. A code snippet would be vey much appreciated. Thanks.

3 Answers 3

1

Unfortunately the code I modified using your link didn't return any results. Here's the stripped code I used. ioctl() returned without errors:

#define DEF_TIMEOUT 5000 // 5 seconds
char cDiskName[] = "/dev/sg3";
int fd = open(cDiskName, O_RDWR);
if (fd < 0)
{
  printf("Open error: %s, errno=%d (%s)\n", cDiskName, errno, strerror(errno));
  return 1;
}

unsigned char turCmbBlk[] = {0x00, 0, 0, 0, 0, 0};
struct sg_io_hdr io_hdr;

unsigned char cIOBuffer[100];

// buffer initialization code omitted

memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(turCmbBlk);
io_hdr.mx_sb_len = sizeof(cIOBuffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.cmdp = turCmbBlk;
io_hdr.sbp = cIOBuffer;
io_hdr.timeout = DEF_TIMEOUT;
if (ioctl(fd, SG_IO, &io_hdr) < 0)
{
  printf("ioctl error: errno=%d (%s)\n", errno, strerror(errno));
}

// Code returned here without any errors but cIOBuffer remains unchanged.

Maybe the call needs a different request code?

8
  • 1
    Please replace io_hdr.dxfer_direction = SG_DXFER_NONE (= no data transfer) with io_hdr.dxfer_direction = SG_DXFER_TO_FROM_DEV (= transfer in both directions).
    – ollo
    Feb 11, 2013 at 17:16
  • Thanks for bearing with me. I tried using SG_DXFER_TO_FROM_DEV in place of SG_DXFER_NONE, but my buffer is still returned unchanged.
    – MaxiFlash
    Feb 11, 2013 at 17:43
  • Please take a look at my changes (edit of my answer). my changes are marked with // - comments. Changes: a.) dynamic memory, b.) flags set, c.) usage of dxferp.
    – ollo
    Feb 11, 2013 at 18:47
  • I'm afraid I don't understand the dxferp parameter, but this is what I did: After the definition of replyBuffer I added memcpy(replyBuffer, cIOBuffer, BUFF_SIZE); and then modified your dxferp to dxferp = replyBuffer. Is this what you meant? The call returns without error but both cIOBuffer and the new replyBuffer are still unchanged (both identical).
    – MaxiFlash
    Feb 11, 2013 at 19:49
  • Yes, you are right, dxferp = replyBuffer; write error. (btw. ive added two more links, i hope they can help you a bit).
    – ollo
    Feb 11, 2013 at 19:54
1

Here's some more documentation:

0

See here:

#include <sys/ioctl.h>

int ioctl(int d, int request, ...); 

Parameters:

  1. Filedescriptor (must be open!)
  2. Request code number (depends on device)
  3. Untyped pointer to memory (going to / coming from driver)

Example

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>

void main(int argc, char **argv)
{
    const char *filename;
    int fd;

    filename = argv[1];

    fd = open(filename, O_WRONLY);
    ioctl(fd, USBDEVFS_RESET, 0);

    close(fd);

    return;
}

Documentation:


Edit

#define BUFF_SIZE 100 // - Buffersize

#define DEF_TIMEOUT 5000 // 5 seconds
char cDiskName[] = "/dev/sg3";
int fd = open(cDiskName, O_RDWR);
if (fd < 0)
{
  printf("Open error: %s, errno=%d (%s)\n", cDiskName, errno, strerror(errno));
  return 1;
}

unsigned char turCmbBlk[] = {0x00, 0, 0, 0, 0, 0};
struct sg_io_hdr *p = (struct sg_io_hdr *) malloc(sizeof(struct sg_io_hdr)); // - dynamic memory allocation - free() required somewhere

unsigned char cIOBuffer[BUFF_SIZE];
unsigned char replyBuffer[BUFF_SIZE]; // - dxfer buffer

// buffer initialization code omitted

memset(p, 0, sizeof(struct sg_io_hdr));
p->interface_id = 'S';
p->cmd_len = sizeof(turCmbBlk);
p->mx_sb_len = BUFF_SIZE;
p->dxfer_direction = SG_DXFER_NONE;
p->cmdp = turCmbBlk;
p->sbp = cIOBuffer;
p->timeout = DEF_TIMEOUT;
p->flags = SG_FLAG_DIRECT_IO; // - Does this help?
p->dxferp = replyBuffer; // - Set dxferp buffer - (A)
p->dxfer_len = BUFF_SIZE; // - buffersize

if (ioctl(fd, SG_IO, p) < 0)
{
  printf("ioctl error: errno=%d (%s)\n", errno, strerror(errno));
}

// Code returned here without any errors but cIOBuffer remains unchanged.

Note (A): Please try setting your input / output buffer you work on here.

Documentation:

5
  • 2
    Thank you for taking the time to post the links. Your parameters would be exactly what I'm looking for, but I believe the magic word is the request code for the Windows "IOCTL_SCSI_PASS_THROUGH_DIRECT" equivalent. I've already been searching on and off for well over 2 weeks but haven't found any code that works in Linux. In Windows the code works with standard drivers so I don't believe the request code depends on the device. All I want to do is send the buffer to the USB device. The USB device will then update the buffer accordingly.
    – MaxiFlash
    Feb 10, 2013 at 15:21
  • 3
    Is SG_IO what you need? "The SG_IO ioctl permits user applications to send SCSI commands to a device." see here
    – ollo
    Feb 10, 2013 at 15:40
  • 2
    I've just looked up SG_IO and it sounds prommising except it needs a correctly filled in sg_io_hdr_t - and this looks most complicated, especially in that it needs a SCSI command, which I don't have. All I want to do is transparently send a 100 byte buffer to the USB device. In Windows it's so easy, I just supply the buffer and the size of the buffer. Surely something like this must exist in linux? Edit I've just looked again at your last link and it may be what I want. I'll have to wait until tomorrow to try it out. I'll reply tomorrow.
    – MaxiFlash
    Feb 10, 2013 at 15:58
  • 2
    Did you SCSI Generic HowTo, especially write()-function?
    – ollo
    Feb 10, 2013 at 18:17
  • I have just also tried with write and read. Both functions appear to work but my buffer remains unchanged
    – MaxiFlash
    Feb 11, 2013 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.