4 * Copyright (C) 2008 Dan Williams <dcbw@redhat.com>
5 * Copyright (C) 2008 Peter Henn <support@option.com>
7 * Heavily based on the 'ozerocdoff' tool by Peter Henn.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details:
25 /* Borrowed from /usr/include/linux/usb/ch9.h */
26 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
27 #define USB_ENDPOINT_XFER_BULK 2
28 #define USB_ENDPOINT_DIR_MASK 0x80
29 #define USB_DIR_OUT 0 /* to device */
30 #define USB_DIR_IN 0x80 /* to host */
33 option_zerocd_find (int vid, int pid)
36 struct usb_device *dev;
38 for (bus = usb_get_busses(); bus; bus = bus->next) {
39 for (dev = bus->devices; dev; dev = dev->next) {
40 if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid) {
41 debug ("Found mass storage device:");
42 debug (" Endpoints: %d", dev->config[0].interface[0].altsetting[0].bNumEndpoints);
43 debug (" Class: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceClass);
44 debug (" SubClass: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceSubClass);
45 debug (" Protocol: 0x%X", dev->config[0].interface[0].altsetting[0].bInterfaceProtocol);
47 if ( (dev->config[0].interface[0].altsetting[0].bNumEndpoints == 2)
48 && (dev->config[0].interface[0].altsetting[0].bInterfaceClass == 0x08)
49 && (dev->config[0].interface[0].altsetting[0].bInterfaceSubClass == 0x06)
50 && (dev->config[0].interface[0].altsetting[0].bInterfaceProtocol == 0x50) ) {
51 debug ("Found modem mass storage device '%s'", dev->filename);
61 find_endpoints (struct usb_device *dev, int *in_ep, int *out_ep)
65 for (i = 0; i < dev->config[0].interface[0].altsetting[0].bNumEndpoints; i++) {
66 struct usb_endpoint_descriptor *ep = &(dev->config[0].interface[0].altsetting[0].endpoint[i]);
68 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
69 unsigned int direction = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
71 if (!*out_ep && (direction == USB_DIR_OUT))
72 *out_ep = ep->bEndpointAddress;
73 else if (!*in_ep && (direction == USB_DIR_IN))
74 *in_ep = ep->bEndpointAddress;
77 if (*in_ep && *out_ep)
85 option_zerocd_switch (struct usb_dev_handle *dh, struct usb_device *dev)
87 const char const rezero_cbw[] = {
88 0x55, 0x53, 0x42, 0x43, /* bulk command signature (LE) */
89 0x78, 0x56, 0x34, 0x12, /* bulk command host tag */
90 0x01, 0x00, 0x00, 0x00, /* bulk command data transfer length (LE) */
91 0x80, /* flags: direction data-in */
93 0x06, /* SCSI command length */
94 0x01, /* SCSI command: REZERO */
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* filler */
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
99 int ret = -1, ep_in = 0, ep_out = 0;
102 /* Find the device's bulk in and out endpoints */
103 if (find_endpoints (dev, &ep_in, &ep_out) < 0) {
104 debug ("%s: couldn't find correct USB endpoints.", dev->filename);
108 usb_clear_halt (dh, ep_out);
109 ret = usb_set_altinterface (dh, 0);
111 debug ("%s: couldn't set device alternate interface.", dev->filename);
115 /* Let the mass storage device settle */
118 /* Send the modeswitch command */
119 ret = usb_bulk_write (dh, ep_out, (char *) rezero_cbw, sizeof (rezero_cbw), 1000);
123 debug ("%s: REZERO command sent.", dev->filename);
125 /* Some devices need to be read from */
126 ret = usb_bulk_read (dh, ep_in, buffer, sizeof (buffer), 1000);