From 47143c3868ceeb04c15444633089282d87647742 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Wed, 2 May 2007 03:13:55 +0900 Subject: [PATCH] add bogus bidi test utility Signed-off-by: FUJITA Tomonori --- Makefile | 5 ++- bidi_test.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletions(-) diff --git a/Makefile b/Makefile index 16ed0c5..738598b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ KSRC ?= /lib/modules/$(KERNELRELEASE)/bu CFLAGS += -O2 -fno-inline -Wall -Wstrict-prototypes -g -I$(KSRC)/include -PROGRAMS = sgv4_dd sgv4_bench smp_rep_manufacturer +PROGRAMS = sgv4_dd sgv4_bench smp_rep_manufacturer bidi_test all: $(PROGRAMS) @@ -16,5 +16,8 @@ sgv4_bench: sgv4_bench.o libbsg.o smp_rep_manufacturer: smp_rep_manufacturer.o libbsg.o $(CC) $^ -o $@ +bidi_test: bidi_test.o libbsg.o + $(CC) $^ -o $@ + clean: rm -f *.o $(PROGRAMS) diff --git a/bidi_test.c b/bidi_test.c new file mode 100644 index 0000000..c420b77 --- /dev/null +++ b/bidi_test.c @@ -0,0 +1,125 @@ +/* + * makeshift tool to send bogus bidi requests via bsg + * + * Copyright (C) 2007 FUJITA Tomonori + * + * Released under the terms of the GNU GPL v2.0. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libbsg.h" + +#define SECTOR_SIZE 512 + +#ifndef VARLEN_CDB +#define VARLEN_CDB 0x7f +#endif + +static char pname[] = "osd_list"; + +static struct option const long_options[] = +{ + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0}, +}; + +static void usage(int status) +{ + if (status) + fprintf(stderr, "Try `%s --help' for more information.\n", pname); + else { + printf("Usage: %s [DEVICE]\n", pname); + printf("\ + -h, --help display this help and exit\n\ +"); + } + exit(status); +} + +static int do_bsg(int bsg_fd) +{ + struct sg_io_v4 hdr; +/* char scb[250], sense[32], *buf; */ + char scb[16], sense[32], *buf; + int bufsize = 1024, ret; + + buf = valloc(bufsize * 2); + + memset(&hdr, 0, sizeof(hdr)); + memset(&scb, 0, sizeof(scb)); + + /* XDWRITEREAD(10) */ + scb[0] = 0x50; +/* scb[0] = VARLEN_CDB; */ +/* scb[7] = 200 - 8; */ + scb[7] = (unsigned char)(((bufsize / SECTOR_SIZE) >> 8) & 0xff); + scb[8] = (unsigned char)((bufsize / SECTOR_SIZE) & 0xff); + + hdr.guard = 'Q'; + hdr.request_len = sizeof(scb); + hdr.request = (unsigned long) scb; + hdr.max_response_len = sizeof(sense); + hdr.response = (unsigned long) sense; + + hdr.din_xfer_len = bufsize; + hdr.din_xferp = (unsigned long) buf; + + hdr.dout_xfer_len = bufsize; + hdr.dout_xferp = (unsigned long) (buf + bufsize); + + ret = write(bsg_fd, &hdr, sizeof(hdr)); + if (ret != sizeof(hdr)) { + fprintf(stderr, "Can't send a bsg request, %m\n"); + goto out; + } + + ret = read(bsg_fd, &hdr, sizeof(hdr)); + if (ret != sizeof(hdr)) + fprintf(stderr, "Can't get a bsg response, %m\n"); + +out: + free(buf); + return 0; +} + +int main(int argc, char **argv) +{ + int longindex, ch; + int bsg_fd; + + while ((ch = getopt_long(argc, argv, "h", long_options, + &longindex)) >= 0) { + switch (ch) { + case 'h': + usage(0); + break; + default: + usage(1); + break; + } + } + + bsg_fd = open_bsg_dev(argv[optind]); + if (bsg_fd < 0) { + fprintf(stderr, "Can't open %s's bsg device.\n", argv[optind]); + } + + do_bsg(bsg_fd); + + close(bsg_fd); + + return 0; +} -- 1.4.3.2