ioctl

Viktor Vasilev viktor.vasilev at stud.tu-darmstadt.de
Tue May 15 16:10:39 UTC 2007


On Monday 14 May 2007 15:28 Dag-Erling Smørgrav wrote:
> Mohsen Pahlevanzadeh <mohsen at pahlevanzadeh.org> writes:
> > Our FreeBSD is 4.11 because we can't use another version.
>
> In that case, we can't help you.

Maybe he still has a chance. The following works on FreeBSD 4.9 for ATA 
devices. I could only test it with an ATA CDROM, but here's the output:

 Model:    CD-224E
 Revision: 1.9A
 Serial:


#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <sys/types.h>
#include <sys/ata.h>

int main(int argc, char **argv) {
	struct ata_cmd iocmd;
	struct ata_params ap;
	int channel, device, fd, i;

	if(argc != 3) {
		errx(1, "usage: %s <channel> <device>", argv[0]);
	}
	
	channel = atoi(argv[1]);
	device = atoi(argv[2]);
	
	if ((fd = open("/dev/ata", O_RDONLY)) == -1) {
		err(1, "error opening /dev/ata");
	}

	bzero(&iocmd, sizeof(struct ata_cmd));
	iocmd.channel = channel;
	iocmd.device = channel;
	iocmd.cmd = ATAGPARM;
	
	if (ioctl(fd, IOCATA, &iocmd) == -1) {
		err(1, "error executing ioctl");
	}

	if (iocmd.u.param.type[device]) {
		ap = iocmd.u.param.params[device];
	} else {
		errx(1, "no information for device %d channel %d",
			device, channel);
	}	
	
	printf("Model:    ");
	for(i = 0; i < 40 && ap.model[i] != '\0'; i++)
		printf("%c", ap.model[i]);
	putchar('\n');

	printf("Revision: ");
	for(i = 0; i < 8 && ap.revision[i] != '\0'; i++)
		printf("%c", ap.revision[i]);
	putchar('\n');

	printf("Serial:   ");
	for(i = 0; i < 20 && ap.serial[i] != '\0'; i++)
		printf("%c", ap.serial[i]);
	putchar('\n');
	
	return 0;
}


Cheers,
Vik


More information about the freebsd-hackers mailing list