chiark / gitweb /
fix randonm findings from llvm-clang-analyzer
[elogind.git] / extras / modem-modeswitch / option.c
1 /*
2  * Modem mode switcher
3  *
4  * Copyright (C) 2008  Dan Williams <dcbw@redhat.com>
5  * Copyright (C) 2008  Peter Henn <support@option.com>
6  *
7  * Heavily based on the 'ozerocdoff' tool by Peter Henn.
8  *
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.
13  *
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:
18  */
19
20 #include <usb.h>
21
22 #include "utils.h"
23 #include "option.h"
24
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 */
31
32 struct usb_device *
33 option_zerocd_find (int vid, int pid)
34 {
35         struct usb_bus *bus;
36         struct usb_device *dev;
37
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);
46
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);
52                                         return dev;
53                                 }
54                         }
55                 }
56         }
57         return NULL;
58 }
59
60 static int
61 find_endpoints (struct usb_device *dev, int *in_ep, int *out_ep)
62 {
63         int i;
64
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]);
67
68                 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
69                         unsigned int direction = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
70
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;
75                 }
76
77                 if (*in_ep && *out_ep)
78                         return 0;
79         }
80
81         return -1;
82 }
83
84 int
85 option_zerocd_switch (struct usb_dev_handle *dh, struct usb_device *dev)
86 {
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 */
92                 0x00,                   /* LUN */
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
97         };
98
99         int ret = -1, ep_in = 0, ep_out = 0;
100         char buffer[256];
101
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);
105                 goto out;
106         }
107
108         usb_clear_halt (dh, ep_out);
109         ret = usb_set_altinterface (dh, 0);
110         if (ret != 0) {
111                 debug ("%s: couldn't set device alternate interface.", dev->filename);
112                 goto out;
113         }
114
115         /* Let the mass storage device settle */
116         sleep (1);
117
118         /* Send the modeswitch command */
119         ret = usb_bulk_write (dh, ep_out, (char *) rezero_cbw, sizeof (rezero_cbw), 1000);
120         if (ret < 0)
121                 return ret;
122
123         debug ("%s: REZERO command sent.", dev->filename);
124
125         /* Some devices need to be read from */
126         ret = usb_bulk_read (dh, ep_in, buffer, sizeof (buffer), 1000);
127
128 out:
129         return ret;
130 }
131