Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 26 | |
| 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 | |
| 35 | static unsigned int option_zero_cd = ZCD_FORCE_MODEM; |
| 36 | module_param(option_zero_cd, uint, S_IRUGO | S_IWUSR); |
| 37 | MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default)," |
| 38 | " 2=Allow CD-Rom"); |
| 39 | |
| 40 | #define RESPONSE_LEN 1024 |
| 41 | |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 42 | static int option_rezero(struct us_data *us) |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 43 | { |
| 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 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 53 | usb_stor_dbg(us, "Option MS: %s\n", "DEVICE MODE SWITCH"); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 54 | |
| 55 | buffer = kzalloc(RESPONSE_LEN, GFP_KERNEL); |
| 56 | if (buffer == NULL) |
| 57 | return USB_STOR_TRANSPORT_ERROR; |
| 58 | |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 59 | memcpy(buffer, rezero_msg, sizeof(rezero_msg)); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 60 | result = usb_stor_bulk_transfer_buf(us, |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 61 | us->send_bulk_pipe, |
| 62 | buffer, sizeof(rezero_msg), NULL); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 63 | if (result != USB_STOR_XFER_GOOD) { |
| 64 | result = USB_STOR_XFER_ERROR; |
| 65 | goto out; |
| 66 | } |
| 67 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 68 | /* |
| 69 | * Some of the devices need to be asked for a response, but we don't |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 70 | * care what that response is. |
| 71 | */ |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 72 | usb_stor_bulk_transfer_buf(us, |
| 73 | us->recv_bulk_pipe, |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 74 | buffer, RESPONSE_LEN, NULL); |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 75 | |
| 76 | /* Read the CSW */ |
| 77 | usb_stor_bulk_transfer_buf(us, |
| 78 | us->recv_bulk_pipe, |
| 79 | buffer, 13, NULL); |
| 80 | |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 81 | result = USB_STOR_XFER_GOOD; |
| 82 | |
| 83 | out: |
| 84 | kfree(buffer); |
| 85 | return result; |
| 86 | } |
| 87 | |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 88 | static int option_inquiry(struct us_data *us) |
| 89 | { |
| 90 | const unsigned char inquiry_msg[] = { |
| 91 | 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78, |
| 92 | 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12, |
| 93 | 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, |
| 94 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 95 | }; |
| 96 | char *buffer; |
| 97 | int result; |
| 98 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 99 | usb_stor_dbg(us, "Option MS: %s\n", "device inquiry for vendor name"); |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 100 | |
| 101 | buffer = kzalloc(0x24, GFP_KERNEL); |
| 102 | if (buffer == NULL) |
| 103 | return USB_STOR_TRANSPORT_ERROR; |
| 104 | |
| 105 | memcpy(buffer, inquiry_msg, sizeof(inquiry_msg)); |
| 106 | result = usb_stor_bulk_transfer_buf(us, |
| 107 | us->send_bulk_pipe, |
| 108 | buffer, sizeof(inquiry_msg), NULL); |
| 109 | if (result != USB_STOR_XFER_GOOD) { |
| 110 | result = USB_STOR_XFER_ERROR; |
| 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | result = usb_stor_bulk_transfer_buf(us, |
| 115 | us->recv_bulk_pipe, |
| 116 | buffer, 0x24, NULL); |
| 117 | if (result != USB_STOR_XFER_GOOD) { |
| 118 | result = USB_STOR_XFER_ERROR; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | result = memcmp(buffer+8, "Option", 6); |
| 123 | |
Jonathan McDowell | 2ab2178 | 2009-07-05 12:29:51 +0100 | [diff] [blame] | 124 | if (result != 0) |
| 125 | result = memcmp(buffer+8, "ZCOPTION", 8); |
| 126 | |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 127 | /* Read the CSW */ |
| 128 | usb_stor_bulk_transfer_buf(us, |
| 129 | us->recv_bulk_pipe, |
| 130 | buffer, 13, NULL); |
| 131 | |
| 132 | out: |
| 133 | kfree(buffer); |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 138 | int option_ms_init(struct us_data *us) |
| 139 | { |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 140 | int result; |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 141 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 142 | usb_stor_dbg(us, "Option MS: %s\n", "option_ms_init called"); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 143 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 144 | /* |
| 145 | * Additional test for vendor information via INQUIRY, |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 146 | * because some vendor/product IDs are ambiguous |
| 147 | */ |
| 148 | result = option_inquiry(us); |
| 149 | if (result != 0) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 150 | usb_stor_dbg(us, "Option MS: %s\n", |
| 151 | "vendor is not Option or not determinable, no action taken"); |
Alan Stern | be475d9 | 2009-05-21 17:37:58 -0400 | [diff] [blame] | 152 | return 0; |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 153 | } else |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 154 | usb_stor_dbg(us, "Option MS: %s\n", |
| 155 | "this is a genuine Option device, proceeding"); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 156 | |
| 157 | /* Force Modem mode */ |
| 158 | if (option_zero_cd == ZCD_FORCE_MODEM) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 159 | usb_stor_dbg(us, "Option MS: %s\n", "Forcing Modem Mode"); |
Josua Dietze | 32ebbe7 | 2009-05-24 23:21:42 +0200 | [diff] [blame] | 160 | result = option_rezero(us); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 161 | if (result != USB_STOR_XFER_GOOD) |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 162 | usb_stor_dbg(us, "Option MS: %s\n", |
| 163 | "Failed to switch to modem mode"); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 164 | return -EIO; |
| 165 | } else if (option_zero_cd == ZCD_ALLOW_MS) { |
| 166 | /* Allow Mass Storage mode (keep CD-Rom) */ |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 167 | usb_stor_dbg(us, "Option MS: %s\n", |
| 168 | "Allowing Mass Storage Mode if device requests it"); |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Alan Stern | be475d9 | 2009-05-21 17:37:58 -0400 | [diff] [blame] | 171 | return 0; |
Dan Williams | 281b064 | 2008-12-14 12:39:22 -0500 | [diff] [blame] | 172 | } |
| 173 | |