blob: e0f76bb05915cb63647a5470c71714c61e0a52ce [file] [log] [blame]
Dan Williams281b0642008-12-14 12:39:22 -05001/*
2 * Driver for Option High Speed Mobile Devices.
3 *
4 * (c) 2008 Dan Williams <dcbw@redhat.com>
5 *
6 * Inspiration taken from sierra_ms.c by Kevin Lloyd <klloyd@sierrawireless.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/usb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040025#include <linux/module.h>
Dan Williams281b0642008-12-14 12:39:22 -050026
27#include "usb.h"
28#include "transport.h"
29#include "option_ms.h"
30#include "debug.h"
31
32#define ZCD_FORCE_MODEM 0x01
33#define ZCD_ALLOW_MS 0x02
34
35static unsigned int option_zero_cd = ZCD_FORCE_MODEM;
36module_param(option_zero_cd, uint, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default),"
38 " 2=Allow CD-Rom");
39
40#define RESPONSE_LEN 1024
41
Josua Dietze32ebbe72009-05-24 23:21:42 +020042static int option_rezero(struct us_data *us)
Dan Williams281b0642008-12-14 12:39:22 -050043{
44 const unsigned char rezero_msg[] = {
45 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
46 0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x01,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
49 };
50 char *buffer;
51 int result;
52
53 US_DEBUGP("Option MS: %s", "DEVICE MODE SWITCH\n");
54
55 buffer = kzalloc(RESPONSE_LEN, GFP_KERNEL);
56 if (buffer == NULL)
57 return USB_STOR_TRANSPORT_ERROR;
58
Josua Dietze32ebbe72009-05-24 23:21:42 +020059 memcpy(buffer, rezero_msg, sizeof(rezero_msg));
Dan Williams281b0642008-12-14 12:39:22 -050060 result = usb_stor_bulk_transfer_buf(us,
Josua Dietze32ebbe72009-05-24 23:21:42 +020061 us->send_bulk_pipe,
62 buffer, sizeof(rezero_msg), NULL);
Dan Williams281b0642008-12-14 12:39:22 -050063 if (result != USB_STOR_XFER_GOOD) {
64 result = USB_STOR_XFER_ERROR;
65 goto out;
66 }
67
68 /* Some of the devices need to be asked for a response, but we don't
69 * care what that response is.
70 */
Josua Dietze32ebbe72009-05-24 23:21:42 +020071 usb_stor_bulk_transfer_buf(us,
72 us->recv_bulk_pipe,
Dan Williams281b0642008-12-14 12:39:22 -050073 buffer, RESPONSE_LEN, NULL);
Josua Dietze32ebbe72009-05-24 23:21:42 +020074
75 /* Read the CSW */
76 usb_stor_bulk_transfer_buf(us,
77 us->recv_bulk_pipe,
78 buffer, 13, NULL);
79
Dan Williams281b0642008-12-14 12:39:22 -050080 result = USB_STOR_XFER_GOOD;
81
82out:
83 kfree(buffer);
84 return result;
85}
86
Josua Dietze32ebbe72009-05-24 23:21:42 +020087static int option_inquiry(struct us_data *us)
88{
89 const unsigned char inquiry_msg[] = {
90 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78,
91 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12,
92 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
94 };
95 char *buffer;
96 int result;
97
98 US_DEBUGP("Option MS: %s", "device inquiry for vendor name\n");
99
100 buffer = kzalloc(0x24, GFP_KERNEL);
101 if (buffer == NULL)
102 return USB_STOR_TRANSPORT_ERROR;
103
104 memcpy(buffer, inquiry_msg, sizeof(inquiry_msg));
105 result = usb_stor_bulk_transfer_buf(us,
106 us->send_bulk_pipe,
107 buffer, sizeof(inquiry_msg), NULL);
108 if (result != USB_STOR_XFER_GOOD) {
109 result = USB_STOR_XFER_ERROR;
110 goto out;
111 }
112
113 result = usb_stor_bulk_transfer_buf(us,
114 us->recv_bulk_pipe,
115 buffer, 0x24, NULL);
116 if (result != USB_STOR_XFER_GOOD) {
117 result = USB_STOR_XFER_ERROR;
118 goto out;
119 }
120
121 result = memcmp(buffer+8, "Option", 6);
122
Jonathan McDowell2ab21782009-07-05 12:29:51 +0100123 if (result != 0)
124 result = memcmp(buffer+8, "ZCOPTION", 8);
125
Josua Dietze32ebbe72009-05-24 23:21:42 +0200126 /* Read the CSW */
127 usb_stor_bulk_transfer_buf(us,
128 us->recv_bulk_pipe,
129 buffer, 13, NULL);
130
131out:
132 kfree(buffer);
133 return result;
134}
135
136
Dan Williams281b0642008-12-14 12:39:22 -0500137int option_ms_init(struct us_data *us)
138{
Josua Dietze32ebbe72009-05-24 23:21:42 +0200139 int result;
Dan Williams281b0642008-12-14 12:39:22 -0500140
141 US_DEBUGP("Option MS: option_ms_init called\n");
142
Josua Dietze32ebbe72009-05-24 23:21:42 +0200143 /* Additional test for vendor information via INQUIRY,
144 * because some vendor/product IDs are ambiguous
145 */
146 result = option_inquiry(us);
147 if (result != 0) {
148 US_DEBUGP("Option MS: vendor is not Option or not determinable,"
149 " no action taken\n");
Alan Sternbe475d92009-05-21 17:37:58 -0400150 return 0;
Josua Dietze32ebbe72009-05-24 23:21:42 +0200151 } else
152 US_DEBUGP("Option MS: this is a genuine Option device,"
153 " proceeding\n");
Dan Williams281b0642008-12-14 12:39:22 -0500154
155 /* Force Modem mode */
156 if (option_zero_cd == ZCD_FORCE_MODEM) {
157 US_DEBUGP("Option MS: %s", "Forcing Modem Mode\n");
Josua Dietze32ebbe72009-05-24 23:21:42 +0200158 result = option_rezero(us);
Dan Williams281b0642008-12-14 12:39:22 -0500159 if (result != USB_STOR_XFER_GOOD)
160 US_DEBUGP("Option MS: Failed to switch to modem mode.\n");
161 return -EIO;
162 } else if (option_zero_cd == ZCD_ALLOW_MS) {
163 /* Allow Mass Storage mode (keep CD-Rom) */
164 US_DEBUGP("Option MS: %s", "Allowing Mass Storage Mode if device"
165 " requests it\n");
166 }
167
Alan Sternbe475d92009-05-21 17:37:58 -0400168 return 0;
Dan Williams281b0642008-12-14 12:39:22 -0500169}
170