diff options
author | Dan Williams <dcbw@redhat.com> | 2008-12-16 08:48:24 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2008-12-16 08:48:24 -0500 |
commit | 65c6e90e49de338d0d18e38eda0e3f48a12999bb (patch) | |
tree | 6eac0392ceeca04a8e40ca2c4ae44b4c36dc95fc | |
parent | 24fcdd00a5bba0c8e5af75ddd9c01b09203c7c8e (diff) |
More updates; cleaned up option rezero
-rw-r--r-- | option-zerocd/50-option-zerocd.rules | 38 | ||||
-rw-r--r-- | option-zerocd/Makefile.am | 12 | ||||
-rw-r--r-- | option-zerocd/option-zerocd.8 | 29 | ||||
-rw-r--r-- | option-zerocd/option-zerocd.c | 260 |
4 files changed, 339 insertions, 0 deletions
diff --git a/option-zerocd/50-option-zerocd.rules b/option-zerocd/50-option-zerocd.rules new file mode 100644 index 0000000..5ec44a6 --- /dev/null +++ b/option-zerocd/50-option-zerocd.rules @@ -0,0 +1,38 @@ +ACTION!="add", GOTO="option_zerocd_end" + +SUBSYSTEM=="usb_device", ATTR{bDeviceClass}!="ff", GOTO="option_zerocd_disable" +SUBSYSTEM=="usb", ATTR{bDeviceClass}!="ff" ,ENV{DEVTYPE}=="usb_device", GOTO="option_zerocd_disable" +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}" + +LABEL="option_zerocd_end" diff --git a/option-zerocd/Makefile.am b/option-zerocd/Makefile.am new file mode 100644 index 0000000..033f9c3 --- /dev/null +++ b/option-zerocd/Makefile.am @@ -0,0 +1,12 @@ +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 = 50-option-zerocd.rules + +man_MANS = option-zerocd.8 + diff --git a/option-zerocd/option-zerocd.8 b/option-zerocd/option-zerocd.8 new file mode 100644 index 0000000..3370b1b --- /dev/null +++ b/option-zerocd/option-zerocd.8 @@ -0,0 +1,29 @@ +.TH EDD_ID 8 "November 2005" "" "Linux Administrator's Manual" +.SH NAME +probe-modem \- udev callout to identify Hayes-compatible modem capabilities +.SH SYNOPSIS +.BI probe-modem +[\fI--export\fP] \fI<devpath>\fP +.SH "DESCRIPTION" +.B probe-modem +is normally called from a udev rule, to provide udev with the modem +capabilities for Hayes-compatible modems. +.SH USAGE +.B probe-modem +opens the tty node specified at the commandline and prints the +discovered modem capabilities. +.SH OPTIONS +The following commandline switches are supported to specify what modem_caps +should print: +.TP +.BI \-\-export +print values as environment keys +.TP +.BI \-\-verbose +print debugging information +.RE +.SH SEE ALSO +.BR udev (7) +.SH AUTHORS +Developed by Dan Williams <dcbw@redhat.com>. + diff --git a/option-zerocd/option-zerocd.c b/option-zerocd/option-zerocd.c new file mode 100644 index 0000000..47b099d --- /dev/null +++ b/option-zerocd/option-zerocd.c @@ -0,0 +1,260 @@ +/* + * Option Zero-CD Disabler + * + * Copyright (C) 2008 Dan Williams <dcbw@redhat.com> + * Copyright (C) 2008 Peter Henn <support@option.com> + * + * Heavily based on the 'ozerocdoff' tool by Peter Henn. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details: + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <signal.h> + +#include <getopt.h> +#include <usb.h> + +/* Borrowed from /usr/include/linux/usb/ch9.h */ +#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ +#define USB_ENDPOINT_XFER_BULK 2 +#define USB_ENDPOINT_DIR_MASK 0x80 +#define USB_DIR_OUT 0 /* to device */ +#define USB_DIR_IN 0x80 /* to host */ + +int debug = 0; +struct usb_dev_handle *handle = NULL; + +#define debug(fmt, args...) \ +if (debug) { \ + fprintf (stdout, "%s(): " fmt "\n", __func__, ##args); \ +} + +static struct usb_device * +find_zerocd_device (int vid, int pid) +{ + struct usb_bus *bus; + struct usb_device *dev; + + 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 (" 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); + debug (" Protocol: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceProtocol); + + if ( (dev->config[0].interface[0].altsetting[0].bNumEndpoints == 2) + && (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); + return dev; + } + } + } + } + return NULL; +} + +static int +find_endpoints (struct usb_device *dev, int *in_ep, int *out_ep) +{ + int i; + + for (i = 0; i < dev->config[0].interface[0].altsetting[0].bNumEndpoints; i++) { + struct usb_endpoint_descriptor *ep = &(dev->config[0].interface[0].altsetting[0].endpoint[i]); + + if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) { + unsigned int direction = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK; + + if (!*out_ep && (direction == USB_DIR_OUT)) + *out_ep = ep->bEndpointAddress; + else if (!*in_ep && (direction == USB_DIR_IN)) + *in_ep = ep->bEndpointAddress; + } + + if (*in_ep && *out_ep) + return 0; + } + + return -1; +} + +static void +release_usb_device (int param) +{ + usb_release_interface (handle, 0); + usb_close (handle); +} + +static void +print_usage (void) +{ + printf ("Usage: option-zerocd [-hd] -p <product-id>\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" + "Examples:\n" + " option-zerocd -v 0x0af0 -p 0xc031\n"); +} + +#define BUF_SIZE 256 + +int main(int argc, char **argv) +{ + static struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "vendor", required_argument, NULL, 'v' }, + { "product", required_argument, NULL, 'p' }, + { "debug", no_argument, NULL, 'd' }, + { 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]; + int ret; + + while (1) { + int option; + + option = getopt_long(argc, argv, "hv:p:d", options, NULL); + if (option == -1) + break; + + switch (option) { + case 'v': + vid = strtol(optarg, NULL, 0); + break; + case 'p': + pid = strtol(optarg, NULL, 0); + break; + case 'd': + debug = 1; + break; + case 'h': + default: + print_usage (); + exit (1); + } + } + + if (!vid || !pid) { + fprintf (stderr, "Error: missing vendor and device IDs."); + exit (2); + } + + usb_init(); + + if (usb_find_busses() < 0) { + fprintf (stderr, "Error: no USB busses found."); + exit (3); + } + + if (usb_find_devices() < 0) { + fprintf (stderr, "Error: no USB devices found."); + exit (4); + } + + dev = find_zerocd_device (vid, pid); + if (dev == NULL) { + fprintf (stderr, "Error: no ZeroCD device found."); + exit (5); + } + + handle = usb_open (dev); + if (handle == NULL) { + fprintf (stderr, "%s: error: could not access the device.", + dev->filename); + exit(6); + } + + /* detach running default driver */ + signal (SIGTERM, release_usb_device); + ret = usb_get_driver_np (handle, 0, buffer, sizeof (buffer)); + if (ret == 0) { + debug ("%s: found already attached driver '%s'", dev->filename, buffer); + + ret = usb_detach_kernel_driver_np (handle, 0); + if (ret != 0) { + debug ("%s: error: unable to detach current driver.", dev->filename); + usb_close (handle); + exit(7); + } + } + + ret = usb_claim_interface (handle, 0); + if (ret != 0) { + debug ("%s: couldn't claim device's USB interface: %d.", + dev->filename, ret); + usb_close (handle); + exit(8); + } + + /* Find the device's bulk in and out endpoints */ + if (find_endpoints (dev, &bulk_in_ep, &bulk_out_ep) < 0) { + debug ("%s: couldn't find correct USB endpoints.", dev->filename); + usb_release_interface (handle, 0); + usb_close (handle); + exit (9); + } + + usb_clear_halt (handle, bulk_out_ep); + ret = usb_set_altinterface (handle, 0); + if (ret != 0) { + debug ("%s: couldn't set device alternate interface.", dev->filename); + usb_release_interface (handle, 0); + usb_close (handle); + exit(10); + } + + /* Let the mass storage device settle */ + sleep(1); + + /* Send the rezero command */ + ret = usb_bulk_write (handle, bulk_out_ep, (char *) rezero_cbw, sizeof (rezero_cbw), 1000); + if (ret < 0) { + debug ("%s: couldn't send rezero command.", dev->filename); + usb_release_interface (handle, 0); + usb_close (handle); + 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); + + ret = usb_close (handle); + if (ret < 0) + debug ("%s: failed to close the device.", dev->filename); + + usb_release_interface (handle, 0); + return 0; +} + |