diff options
author | Dan Williams <dcbw@redhat.com> | 2008-12-17 10:45:20 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2008-12-17 10:45:20 -0500 |
commit | 778c2817d538499295686146c4d10c5c6d1b5f1c (patch) | |
tree | 726eb8284ebfaaef183d103fa14ce55b840987e7 | |
parent | d5fbda7092fc83129a1bbf5d306943c508109e04 (diff) |
Make modem switcher generic; fix rules install location
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | modem-modeswitch/61-option-modem-modeswitch.rules (renamed from option-zerocd/61-option-zerocd.rules) | 56 | ||||
-rw-r--r-- | modem-modeswitch/Makefile.am | 12 | ||||
-rw-r--r-- | modem-modeswitch/modem-modeswitch.8 (renamed from option-zerocd/option-zerocd.8) | 29 | ||||
-rw-r--r-- | modem-modeswitch/modem-modeswitch.c (renamed from option-zerocd/option-zerocd.c) | 237 | ||||
-rw-r--r-- | option-zerocd/Makefile.am | 12 |
7 files changed, 246 insertions, 104 deletions
diff --git a/Makefile.am b/Makefile.am index 10a088b..7a82f97 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,2 +1,2 @@ -SUBDIRS = probe-modem option-zerocd +SUBDIRS = probe-modem modem-modeswitch diff --git a/configure.ac b/configure.ac index 9818bcd..bbf2eb7 100644 --- a/configure.ac +++ b/configure.ac @@ -95,7 +95,7 @@ fi AC_CONFIG_FILES([ Makefile probe-modem/Makefile -option-zerocd/Makefile +modem-modeswitch/Makefile ]) AC_OUTPUT diff --git a/option-zerocd/61-option-zerocd.rules b/modem-modeswitch/61-option-modem-modeswitch.rules index 5ec44a6..0404177 100644 --- a/option-zerocd/61-option-zerocd.rules +++ b/modem-modeswitch/61-option-modem-modeswitch.rules @@ -6,33 +6,33 @@ SUBSYSTEM=="scsi", ENV{DEVTYPE}=="scsi_device", GOTO="option_zerocd_disable" GOTO="option_zerocd_end" LABEL="option_zerocd_disable" -ATTRS{idVendor}=="05c6", ATTRS{idProduct}=="1000", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6711", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6711", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6731", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6751", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6771", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6791", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6811", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6911", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6951", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6971", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7011", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7031", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7051", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7071", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7111", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7211", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7251", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7271", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7311", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="c031", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="d031", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="d033", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7301", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7361", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7401", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7501", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" -ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7601", RUN+="/lib/udev/option-zerocd -v 0x%s{idVendor} -p 0x%s{idProduct}" +ATTRS{idVendor}=="05c6", ATTRS{idProduct}=="1000", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6711", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6711", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6731", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6751", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6771", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6791", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6811", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6911", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6951", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6971", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7011", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7031", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7051", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7071", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7111", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7211", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7251", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7271", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7311", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="c031", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="d031", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="d033", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7301", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7361", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7401", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7501", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" +ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="7601", RUN+="/lib/udev/modem-modeswitch -v 0x%s{idVendor} -p 0x%s{idProduct} -t option-zerocd" LABEL="option_zerocd_end" diff --git a/modem-modeswitch/Makefile.am b/modem-modeswitch/Makefile.am new file mode 100644 index 0000000..be2e57f --- /dev/null +++ b/modem-modeswitch/Makefile.am @@ -0,0 +1,12 @@ +libudevdir = $(libdir)/udev + +libudev_PROGRAMS = modem-modeswitch +modem_modeswitch_SOURCES = modem-modeswitch.c +modem_modeswitch_CPPFLAGS = $(LIBUSB_CFLAGS) +modem_modeswitch_LDADD = $(LIBUSB_LIBS) + +rulesdir = $(sysconfdir)/udev/rules.d +rules_DATA = 61-option-modem-modeswitch.rules + +man_MANS = modem-modeswitch.8 + diff --git a/option-zerocd/option-zerocd.8 b/modem-modeswitch/modem-modeswitch.8 index 7fd761b..2c01202 100644 --- a/option-zerocd/option-zerocd.8 +++ b/modem-modeswitch/modem-modeswitch.8 @@ -1,26 +1,33 @@ -.TH OPTION_ZEROCD 8 "November 2005" "" "Linux Administrator's Manual" +.TH MODEM_MODESWITCH 8 "November 2005" "" "Linux Administrator's Manual" .SH NAME -option-zerocd \- udev callout to switch Option N.V. mobile broadband devices -to their intended modem mode +modem-modeswitch \- udev callout to switch mobile broadband devices to their +intended modem mode .SH SYNOPSIS -.BI option-zerocd -\fI--vendor <vendor id>\fP \fI--product <product id>\fP [\fI--debug\fP] \fI<devpath>\fP +.BI modem-modeswitch +\fI--vendor <vendor id>\fP \fI--product <product id>\fP [\fI--debug\fP] +\fI--type <type>\fP \fI<devpath>\fP .SH "DESCRIPTION" -.B option-zerocd -is normally called from a udev rule, to eject the driver CD that many Option -mobile broadband devices masquerade as by default, and switch into modem mode. +.B modem-modeswitch +is normally called from a udev rule, to eject the driver CD that many mobile +broadband devices mount by default, and switch them into modem mode. .SH USAGE -.B option-zerocd +.B modem-modeswitch switches the device into modem mode .SH OPTIONS The following commandline switches are supported: .TP -.BI \-\-vendor +.BI \-\-vendor\ <vid> the USB vendor ID of the mobile broadband device to switch .TP -.BI \-\-product +.BI \-\-product\ <pid> the USB product ID of the mobile broadband device to switch .TP +.BI \-\-type\ <type> +the type of switch to perform (one of: option-zerocd) +.TP +.BI \-\-log\ <file> +log verbose debugging information about the switching process +.TP .BI \-\-debug print verbose debugging information about the switching process .RE diff --git a/option-zerocd/option-zerocd.c b/modem-modeswitch/modem-modeswitch.c index cc124dd..2c1c764 100644 --- a/option-zerocd/option-zerocd.c +++ b/modem-modeswitch/modem-modeswitch.c @@ -1,5 +1,5 @@ /* - * Option Zero-CD Disabler + * Modem mode switcher * * Copyright (C) 2008 Dan Williams <dcbw@redhat.com> * Copyright (C) 2008 Peter Henn <support@option.com> @@ -22,8 +22,10 @@ #include <string.h> #include <unistd.h> #include <signal.h> - +#include <stdarg.h> +#include <time.h> #include <getopt.h> + #include <usb.h> /* Borrowed from /usr/include/linux/usb/ch9.h */ @@ -33,16 +35,100 @@ #define USB_DIR_OUT 0 /* to device */ #define USB_DIR_IN 0x80 /* to host */ -int debug = 0; +static int debug = 0; +static int quiet = 0; +static FILE *logfile = NULL; struct usb_dev_handle *handle = NULL; +typedef int (*SwitchFunc) (struct usb_dev_handle *dh, + int ep_in, + int ep_out, + const char *devname); + +typedef enum { + ST_UNKNOWN = 0, + ST_OPTION_ZEROCD +} SwitchType; + +typedef struct SwitchEntry { + SwitchType st; + const char *clopt; + SwitchFunc func; +} SwitchEntry; + +/* Device-specific switcher functions */ +static int switch_option_zerocd (struct usb_dev_handle *dh, + int ep_in, + int ep_out, + const char *devname); + +static SwitchEntry switch_types[] = { + { ST_OPTION_ZEROCD, "option-zerocd", switch_option_zerocd }, + { ST_UNKNOWN, NULL, NULL } +}; + + +static void +do_log (int err, const char *fmt, ...) +{ + va_list args; + char buffer[1024]; + + va_start (args, fmt); + vsnprintf (buffer, sizeof (buffer), fmt, args); + va_end (args); + + if (logfile) + fprintf (logfile, "%c: %s\n", err ? 'E' : 'L', buffer); + if (!quiet) + fprintf (err ? stderr : stdout, "%c: %s\n", err ? 'E' : 'L', buffer); +} + +#define logmsg(fmt, args...) do_log (0, fmt, ##args); +#define logerr(fmt, args...) do_log (1, fmt, ##args); + #define debug(fmt, args...) \ if (debug) { \ - fprintf (stdout, "%s(): " fmt "\n", __func__, ##args); \ + logmsg ("%s(): " fmt, __func__, ##args); \ } +static int +switch_option_zerocd (struct usb_dev_handle *dh, + int ep_in, + int ep_out, + const char *devname) +{ + const char const rezero_cbw[] = { + 0x55, 0x53, 0x42, 0x43, /* bulk command signature (LE) */ + 0x78, 0x56, 0x34, 0x12, /* bulk command host tag */ + 0x01, 0x00, 0x00, 0x00, /* bulk command data transfer length (LE) */ + 0x80, /* flags: direction data-in */ + 0x00, /* LUN */ + 0x06, /* SCSI command length */ + 0x01, /* SCSI command: REZERO */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* filler */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + int ret; + char buffer[256]; + + /* Send the modeswitch command */ + ret = usb_bulk_write (dh, ep_out, (char *) rezero_cbw, sizeof (rezero_cbw), 1000); + if (ret < 0) + return ret; + + debug ("%s: REZERO command sent.", devname); + + /* Some devices need to be read from */ + ret = usb_bulk_read (dh, ep_in, buffer, sizeof (buffer), 1000); + + return ret; +} + + static struct usb_device * -find_zerocd_device (int vid, int pid) +find_device (int vid, int pid) { struct usb_bus *bus; struct usb_device *dev; @@ -50,7 +136,7 @@ find_zerocd_device (int vid, int pid) for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid) { - debug ("Found possible ZeroCD device:"); + debug ("Found mass storage device:"); debug (" Endpoints: %d", dev->config[0].interface[0].altsetting[0].bNumEndpoints); debug (" Class: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceClass); debug (" SubClass: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceSubClass); @@ -60,7 +146,7 @@ find_zerocd_device (int vid, int pid) && (dev->config[0].interface[0].altsetting[0].bInterfaceClass == 0x08) && (dev->config[0].interface[0].altsetting[0].bInterfaceSubClass == 0x06) && (dev->config[0].interface[0].altsetting[0].bInterfaceProtocol == 0x50) ) { - debug ("Found ZeroCD device '%s'", dev->filename); + debug ("Found modem mass storage device '%s'", dev->filename); return dev; } } @@ -103,16 +189,38 @@ release_usb_device (int param) static void print_usage (void) { - printf ("Usage: option-zerocd [-hd] -p <product-id>\n" + printf ("Usage: modem-modeswitch [-hdq] [-l <file>] -v <vendor-id> -p <product-id> -t <type>\n" " -h, --help show this help message\n" " -v, --vendor <n> target USB vendor ID\n" " -p, --product <n> target USB product ID\n" - " -d, --debug print debugging messages\n\n" + " -t, --type <type> type of switch to attempt, varies by device:\n" + " option-zerocd - For many Option N.V. devices\n" + " -l, --log <file> log output to a file\n" + " -q, --quiet don't print anything to stdout\n" + " -d, --debug display debugging messages\n\n" "Examples:\n" - " option-zerocd -v 0x0af0 -p 0xc031\n"); + " modem-modeswitch -v 0x0af0 -p 0xc031 -t option-zerocd\n"); } -#define BUF_SIZE 256 +static SwitchEntry * +parse_type (const char *s) +{ + SwitchEntry *entry = &switch_types[0]; + + while (entry->clopt) { + if (!strcmp (entry->clopt, "option-zerocd")) + return entry; + } + + return NULL; +} + +static void +do_exit (int val) +{ + if (logfile) fclose (logfile); + exit (val); +} int main(int argc, char **argv) { @@ -120,40 +228,47 @@ int main(int argc, char **argv) { "help", no_argument, NULL, 'h' }, { "vendor", required_argument, NULL, 'v' }, { "product", required_argument, NULL, 'p' }, + { "type", required_argument, NULL, 't' }, + { "log", required_argument, NULL, 'l' }, { "debug", no_argument, NULL, 'd' }, + { "quiet", no_argument, NULL, 'q' }, { NULL, 0, NULL, 0} }; - const char const rezero_cbw[] = { - 0x55, 0x53, 0x42, 0x43, /* bulk command signature (LE) */ - 0x78, 0x56, 0x34, 0x12, /* bulk command host tag */ - 0x01, 0x00, 0x00, 0x00, /* bulk command data transfer length (LE) */ - 0x80, /* flags: direction data-in */ - 0x00, /* LUN */ - 0x06, /* SCSI command length */ - 0x01, /* SCSI command: REZERO */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* filler */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - struct usb_device *dev; int vid = 0, pid = 0, bulk_in_ep = 0, bulk_out_ep = 0; - char buffer[BUF_SIZE]; + const char *logpath = NULL; + char buffer[256]; int ret; + SwitchEntry *sentry = NULL; while (1) { int option; - option = getopt_long(argc, argv, "hv:p:d", options, NULL); + option = getopt_long(argc, argv, "hv:p:l:t:dq", options, NULL); if (option == -1) break; switch (option) { case 'v': - vid = strtol(optarg, NULL, 0); + vid = strtol (optarg, NULL, 0); break; case 'p': - pid = strtol(optarg, NULL, 0); + pid = strtol (optarg, NULL, 0); + break; + case 't': + sentry = parse_type (optarg); + if (!sentry) { + logerr ("unknown switch type '%s'", optarg); + print_usage (); + exit (1); + } + break; + case 'l': + logpath = optarg; + break; + case 'q': + quiet = 1; break; case 'd': debug = 1; @@ -165,34 +280,57 @@ int main(int argc, char **argv) } } + if (logpath) { + time_t t = time (NULL); + + logfile = fopen (logpath, "a+"); + if (!logfile) { + fprintf (stderr, "Couldn't open/create logfile %s", logpath); + exit (2); + } + + logmsg ("\n**** Started: %s\n", ctime (&t)); + } + + if (!sentry) { + if (!quiet) + print_usage (); + else + logerr ("missing device switch type."); + do_exit (3); + } + if (!vid || !pid) { - fprintf (stderr, "Error: missing vendor and device IDs."); - exit (2); + if (!quiet) + print_usage (); + else + logerr ("missing vendor and device IDs."); + do_exit (3); } usb_init(); if (usb_find_busses() < 0) { - fprintf (stderr, "Error: no USB busses found."); - exit (3); + logerr ("no USB busses found."); + do_exit (4); } if (usb_find_devices() < 0) { - fprintf (stderr, "Error: no USB devices found."); - exit (4); + logerr ("no USB devices found."); + do_exit (4); } - dev = find_zerocd_device (vid, pid); + dev = find_device (vid, pid); if (dev == NULL) { - fprintf (stderr, "Error: no ZeroCD device found."); - exit (5); + logerr ("no mass storage device found."); + do_exit (5); } handle = usb_open (dev); if (handle == NULL) { - fprintf (stderr, "%s: error: could not access the device.", + logerr ("%s: could not access the device.", dev->filename); - exit(6); + do_exit (6); } /* detach running default driver */ @@ -205,7 +343,7 @@ int main(int argc, char **argv) if (ret != 0) { debug ("%s: error: unable to detach current driver.", dev->filename); usb_close (handle); - exit(7); + do_exit (7); } } @@ -214,7 +352,7 @@ int main(int argc, char **argv) debug ("%s: couldn't claim device's USB interface: %d.", dev->filename, ret); usb_close (handle); - exit(8); + do_exit (8); } /* Find the device's bulk in and out endpoints */ @@ -222,7 +360,7 @@ int main(int argc, char **argv) debug ("%s: couldn't find correct USB endpoints.", dev->filename); usb_release_interface (handle, 0); usb_close (handle); - exit (9); + do_exit (9); } usb_clear_halt (handle, bulk_out_ep); @@ -231,30 +369,27 @@ int main(int argc, char **argv) debug ("%s: couldn't set device alternate interface.", dev->filename); usb_release_interface (handle, 0); usb_close (handle); - exit(10); + do_exit (10); } /* Let the mass storage device settle */ - sleep(1); + sleep (1); - /* Send the rezero command */ - ret = usb_bulk_write (handle, bulk_out_ep, (char *) rezero_cbw, sizeof (rezero_cbw), 1000); + ret = (*sentry->func) (handle, bulk_in_ep, bulk_out_ep, dev->filename); if (ret < 0) { - debug ("%s: couldn't send rezero command.", dev->filename); + debug ("%s: failed to switch device to modem mode.", dev->filename); usb_release_interface (handle, 0); usb_close (handle); - exit(11); + do_exit(11); } - debug ("%s: REZERO command sent.", dev->filename); - /* Some devices need to be read from */ - ret = usb_bulk_read (handle, bulk_in_ep, buffer, sizeof (buffer), 1000); + usb_release_interface (handle, 0); ret = usb_close (handle); if (ret < 0) debug ("%s: failed to close the device.", dev->filename); - usb_release_interface (handle, 0); + do_exit (0); return 0; } diff --git a/option-zerocd/Makefile.am b/option-zerocd/Makefile.am deleted file mode 100644 index f45b7d7..0000000 --- a/option-zerocd/Makefile.am +++ /dev/null @@ -1,12 +0,0 @@ -libudevdir = $(libdir)/udev - -libudev_PROGRAMS = option-zerocd -option_zerocd_SOURCES = option-zerocd.c -option_zerocd_CPPFLAGS = $(LIBUSB_CFLAGS) -option_zerocd_LDADD = $(LIBUSB_LIBS) - -rulesdir = $(sysconfdir)/udev -rules_DATA = 61-option-zerocd.rules - -man_MANS = option-zerocd.8 - |