LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-17-2009, 03:06 AM   #1
keros
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
ioctl -> Bad address


Hello

I have a problem with ioctl call.

My code:
Code:
if (ioctl (fd, SG_IO, &sg_io))
{
	return -1;
}

fd:
Code:
fd = open (file, O_RDWR|O_NONBLOCK)
SG_IO from sg.h
Code:
/* synchronous SCSI command ioctl, (only in version 3 interface) */
#define SG_IO 0x2285   /* similar effect as write() followed by read() */
sg_io is a struct
Code:
typedef struct sg_io_hdr
{
  int interface_id;           /* [i] 'S' for SCSI generic (required) */
  int dxfer_direction;        /* [i] data transfer direction  */
  unsigned char cmd_len;      /* [i] SCSI command length ( <= 16 bytes) */
  unsigned char mx_sb_len;    /* [i] max length to write to sbp */
  unsigned short int iovec_count; /* [i] 0 implies no scatter gather */
  unsigned int dxfer_len;     /* [i] byte count of data transfer */
  void * dxferp;              /* [i], [*io] points to data transfer memory
				 or scatter gather list */
  unsigned char * cmdp;       /* [i], [*i] points to command to perform */
  unsigned char * sbp;        /* [i], [*o] points to sense_buffer memory */
  unsigned int timeout;       /* [i] MAX_UINT->no timeout (unit: millisec) */
  unsigned int flags;         /* [i] 0 -> default, see SG_FLAG... */
  int pack_id;                /* [i->o] unused internally (normally) */
  void * usr_ptr;             /* [i->o] unused internally */
  unsigned char status;       /* [o] scsi status */
  unsigned char masked_status;/* [o] shifted, masked scsi status */
  unsigned char msg_status;   /* [o] messaging level data (optional) */
  unsigned char sb_len_wr;    /* [o] byte count actually written to sbp */
  unsigned short int host_status; /* [o] errors from host adapter */
  unsigned short int driver_status;/* [o] errors from software driver */
  int resid;                  /* [o] dxfer_len - actual_transferred */
  unsigned int duration;      /* [o] time taken by cmd (unit: millisec) */
  unsigned int info;          /* [o] auxiliary information */
} sg_io_hdr_t;
The struct is filled with data on different places in code.

But now to the important things. I'm building my own MMC commands and send them over ioctl to my DVD drive.
Code:
int err = 0;
Scsi_Command cmd;
cmd.associate (device);

cmd[0] = 0x35; // Synchronize Cache
cmd[1] = 0x00; // Immed
cmd[2] = 0x00; // Logical Block Address (drive may ignore this)
cmd[3] = 0x00; // Logical Block Address (drive may ignore this)
cmd[4] = 0x00; // Logical Block Address (drive may ignore this)
cmd[5] = 0x00; // Logical Block Address (drive may ignore this)

cmd[6] = 0x00; // Reserved
cmd[7] = 0x00; // Number of blocks (drive may ignore this)
cmd[8] = 0x00; // Number of blocks (drive may ignore this)
cmd[9] = 0x00; // Control

err = cmd.transport (); // -> ioctl
// err = cmd.transport (WRITE, buffer, 4); // testing

return err;
Thats working fine.

But my drive have also a few non standard MMC commands. I have tested three of them, but only one has worked. With the other two commands i get -1 back from the ioctl call and the errno variable is set. In errno i find a "14 - Bad address" error.

I have searched and found that Bad address means with the third parameter (sg_io) in the ioctl commando is something wrong (But i don't understand what it is).

I don't understand why i get this error. All standard MMC commands are working fine and the one none standard command also works.

On windows all the commands are working (also send over ioctl)
Code:
SendScsiCommand(hbd, output, prds, SCSI_PASS_THROUGH_DIRECT_ENGINE.SCSI_IOCTL_DATA_OUT, counter, mem);
What have i overseen?
 
Old 02-17-2009, 04:53 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
I have absolutely no experience with SCSI programming, but:
Quote:
sg_io is a struct
[snip]
The struct is filled with data on different places in code.

But now to the important things.
Not so fast. You've probably just skipped over the important things.

Look at the content of your structure sg_io. I see three memory pointers there; four if you include usr_ptr.

Have those pointers all been properly initialized?

If so, I don't know what's wrong, but check them first.
 
Old 02-18-2009, 09:37 AM   #3
keros
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thx for help.

I have checked the whole struct.

Code:
 int interface_id;           // set to 'S'
  int dxfer_direction;        // -1, -2, -3 (none, write, read) all tested
  unsigned char cmd_len;      // is 12
  unsigned char mx_sb_len;    // is 64 (sg_io.mx_sb_len = sizeof(_sense))
  unsigned short int iovec_count; // is 0
  unsigned int dxfer_len;     // the length of my data buffer
  void * dxferp;              // my data buffer (unsigned char *) i have tested many possibilities (NULL, 1, 2, 3, 4 .... ;) )
  unsigned char * cmdp;       // my command buffer (MMC command, struct cdrom_generic_command)
  unsigned char * sbp;        // Sense buffer (sg_io.sbp = _sense.u)

union
{
	struct request_sense s; // request_sense is defined in cdrom.h
	unsigned char u[18];
} _sense;


  unsigned int timeout;       // MAX_UINT-> no timeout
  unsigned int flags;         // sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
  int pack_id;                /* [i->o] unused internally (normally) */
  void * usr_ptr;             /* [i->o] unused internally */
  unsigned char status;       // is 0
  unsigned char masked_status;// is 0
  unsigned char msg_status;   // is 0
  unsigned char sb_len_wr;    // is 0
  unsigned short int host_status; // is 0
  unsigned short int driver_status;// is 0
  int resid;                  // is 0
  unsigned int duration;      // is 0
  unsigned int info;          // is 0
Any ideas? I think everything is initialized in the right way (ok can't be i get an error ).

But it's strange that this initialized struct works with nearly all MMC commands.
 
Old 02-18-2009, 09:55 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
Again, I'm way out of my depth here, but I see only two possibilities:
  1. there's a bug in the driver; or
  2. you've overlooked something, and I have no idea what.
Since a driver must be rewritten for each operating system, and your code works on Windows, item 1 in this list is more than a remote possibility.

But this is of no help, I know.

Anyone else out there have a clue?
 
Old 02-19-2009, 03:51 AM   #5
keros
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Does anyone know a ioctl forum/mailing list/irc chan/newsgroup or something else where i can ask for help?
 
  


Reply


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
ioctl: LOOP_CLR_FD: No such device or address newbie2007 Linux - Newbie 1 01-27-2008 12:19 PM
How to pass IOCTL arguments from usespace ioctl call devkpict Linux - Kernel 1 12-07-2007 07:45 PM
How to add a gateway address using ioctl in C in linux ? sweta Linux - Networking 1 12-28-2006 12:26 AM
HOw to get Broad cast address using ioctl( ) and print it... touqeer.ansar Programming 1 07-03-2006 12:35 PM
ioctl[SIOCGIFADDR]: Cannot assign requested address mmcgann Linux - Wireless Networking 1 06-20-2006 01:09 PM


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

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