blob: b9f78ef3edc3409e7d9f4bd794a2dd520d4e4009 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kevin Lloyd32fe5e32008-07-10 14:14:57 -07002#include <scsi/scsi.h>
3#include <scsi/scsi_host.h>
4#include <scsi/scsi_cmnd.h>
5#include <scsi/scsi_device.h>
6#include <linux/usb.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -04007#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Kevin Lloyd32fe5e32008-07-10 14:14:57 -07009
10#include "usb.h"
11#include "transport.h"
12#include "protocol.h"
13#include "scsiglue.h"
14#include "sierra_ms.h"
15#include "debug.h"
16
17#define SWIMS_USB_REQUEST_SetSwocMode 0x0B
18#define SWIMS_USB_REQUEST_GetSwocInfo 0x0A
19#define SWIMS_USB_INDEX_SetMode 0x0000
20#define SWIMS_SET_MODE_Modem 0x0001
21
22#define TRU_NORMAL 0x01
23#define TRU_FORCE_MS 0x02
24#define TRU_FORCE_MODEM 0x03
25
26static unsigned int swi_tru_install = 1;
27module_param(swi_tru_install, uint, S_IRUGO | S_IWUSR);
28MODULE_PARM_DESC(swi_tru_install, "TRU-Install mode (1=Full Logic (def),"
29 " 2=Force CD-Rom, 3=Force Modem)");
30
31struct swoc_info {
32 __u8 rev;
33 __u8 reserved[8];
34 __u16 LinuxSKU;
35 __u16 LinuxVer;
36 __u8 reserved2[47];
37} __attribute__((__packed__));
38
39static bool containsFullLinuxPackage(struct swoc_info *swocInfo)
40{
41 if ((swocInfo->LinuxSKU >= 0x2100 && swocInfo->LinuxSKU <= 0x2FFF) ||
42 (swocInfo->LinuxSKU >= 0x7100 && swocInfo->LinuxSKU <= 0x7FFF))
43 return true;
44 else
45 return false;
46}
47
48static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
49{
50 int result;
Joe Perches191648d2013-04-19 11:44:00 -070051 dev_dbg(&udev->dev, "SWIMS: %s", "DEVICE MODE SWITCH\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070052 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
53 SWIMS_USB_REQUEST_SetSwocMode, /* __u8 request */
54 USB_TYPE_VENDOR | USB_DIR_OUT, /* __u8 request type */
55 eSWocMode, /* __u16 value */
56 0x0000, /* __u16 index */
57 NULL, /* void *data */
58 0, /* __u16 size */
59 USB_CTRL_SET_TIMEOUT); /* int timeout */
60 return result;
61}
62
63
64static int sierra_get_swoc_info(struct usb_device *udev,
65 struct swoc_info *swocInfo)
66{
67 int result;
68
Joe Perches191648d2013-04-19 11:44:00 -070069 dev_dbg(&udev->dev, "SWIMS: Attempting to get TRU-Install info\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070070
71 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
72 SWIMS_USB_REQUEST_GetSwocInfo, /* __u8 request */
73 USB_TYPE_VENDOR | USB_DIR_IN, /* __u8 request type */
74 0, /* __u16 value */
75 0, /* __u16 index */
76 (void *) swocInfo, /* void *data */
77 sizeof(struct swoc_info), /* __u16 size */
78 USB_CTRL_SET_TIMEOUT); /* int timeout */
79
80 swocInfo->LinuxSKU = le16_to_cpu(swocInfo->LinuxSKU);
81 swocInfo->LinuxVer = le16_to_cpu(swocInfo->LinuxVer);
82 return result;
83}
84
Joe Perches191648d2013-04-19 11:44:00 -070085static void debug_swoc(const struct device *dev, struct swoc_info *swocInfo)
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070086{
Joe Perches191648d2013-04-19 11:44:00 -070087 dev_dbg(dev, "SWIMS: SWoC Rev: %02d\n", swocInfo->rev);
88 dev_dbg(dev, "SWIMS: Linux SKU: %04X\n", swocInfo->LinuxSKU);
89 dev_dbg(dev, "SWIMS: Linux Version: %04X\n", swocInfo->LinuxVer);
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070090}
91
92
Greg Kroah-Hartman7f26ee42018-01-23 11:24:06 +010093static ssize_t truinst_show(struct device *dev, struct device_attribute *attr,
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070094 char *buf)
95{
96 struct swoc_info *swocInfo;
97 struct usb_interface *intf = to_usb_interface(dev);
98 struct usb_device *udev = interface_to_usbdev(intf);
99 int result;
100 if (swi_tru_install == TRU_FORCE_MS) {
101 result = snprintf(buf, PAGE_SIZE, "Forced Mass Storage\n");
102 } else {
103 swocInfo = kmalloc(sizeof(struct swoc_info), GFP_KERNEL);
104 if (!swocInfo) {
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700105 snprintf(buf, PAGE_SIZE, "Error\n");
106 return -ENOMEM;
107 }
108 result = sierra_get_swoc_info(udev, swocInfo);
109 if (result < 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700110 dev_dbg(dev, "SWIMS: failed SWoC query\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700111 kfree(swocInfo);
112 snprintf(buf, PAGE_SIZE, "Error\n");
113 return -EIO;
114 }
Joe Perches191648d2013-04-19 11:44:00 -0700115 debug_swoc(dev, swocInfo);
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700116 result = snprintf(buf, PAGE_SIZE,
117 "REV=%02d SKU=%04X VER=%04X\n",
118 swocInfo->rev,
119 swocInfo->LinuxSKU,
120 swocInfo->LinuxVer);
121 kfree(swocInfo);
122 }
123 return result;
124}
Greg Kroah-Hartman7f26ee42018-01-23 11:24:06 +0100125static DEVICE_ATTR_RO(truinst);
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700126
127int sierra_ms_init(struct us_data *us)
128{
129 int result, retries;
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700130 struct swoc_info *swocInfo;
131 struct usb_device *udev;
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700132
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700133 retries = 3;
134 result = 0;
135 udev = us->pusb_dev;
136
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700137 /* Force Modem mode */
138 if (swi_tru_install == TRU_FORCE_MODEM) {
Joe Perches191648d2013-04-19 11:44:00 -0700139 usb_stor_dbg(us, "SWIMS: Forcing Modem Mode\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700140 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
141 if (result < 0)
Joe Perches191648d2013-04-19 11:44:00 -0700142 usb_stor_dbg(us, "SWIMS: Failed to switch to modem mode\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700143 return -EIO;
144 }
145 /* Force Mass Storage mode (keep CD-Rom) */
146 else if (swi_tru_install == TRU_FORCE_MS) {
Joe Perches191648d2013-04-19 11:44:00 -0700147 usb_stor_dbg(us, "SWIMS: Forcing Mass Storage Mode\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700148 goto complete;
149 }
150 /* Normal TRU-Install Logic */
151 else {
Joe Perches191648d2013-04-19 11:44:00 -0700152 usb_stor_dbg(us, "SWIMS: Normal SWoC Logic\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700153
154 swocInfo = kmalloc(sizeof(struct swoc_info),
155 GFP_KERNEL);
Joe Perches191648d2013-04-19 11:44:00 -0700156 if (!swocInfo)
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700157 return -ENOMEM;
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700158
159 retries = 3;
160 do {
161 retries--;
162 result = sierra_get_swoc_info(udev, swocInfo);
163 if (result < 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700164 usb_stor_dbg(us, "SWIMS: Failed SWoC query\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700165 schedule_timeout_uninterruptible(2*HZ);
166 }
167 } while (retries && result < 0);
168
169 if (result < 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700170 usb_stor_dbg(us, "SWIMS: Completely failed SWoC query\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700171 kfree(swocInfo);
172 return -EIO;
173 }
174
Joe Perches191648d2013-04-19 11:44:00 -0700175 debug_swoc(&us->pusb_dev->dev, swocInfo);
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700176
Felipe Balbif0183a32016-04-18 13:09:11 +0300177 /*
178 * If there is not Linux software on the TRU-Install device
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700179 * then switch to modem mode
180 */
181 if (!containsFullLinuxPackage(swocInfo)) {
Joe Perches191648d2013-04-19 11:44:00 -0700182 usb_stor_dbg(us, "SWIMS: Switching to Modem Mode\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700183 result = sierra_set_ms_mode(udev,
184 SWIMS_SET_MODE_Modem);
185 if (result < 0)
Joe Perches191648d2013-04-19 11:44:00 -0700186 usb_stor_dbg(us, "SWIMS: Failed to switch modem\n");
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700187 kfree(swocInfo);
188 return -EIO;
189 }
190 kfree(swocInfo);
191 }
192complete:
Kangjie Lu1a137b42019-03-24 22:08:28 -0500193 return device_create_file(&us->pusb_intf->dev, &dev_attr_truinst);
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700194}
195