xref: /linux/drivers/usb/storage/sierra_ms.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
1 #include <scsi/scsi.h>
2 #include <scsi/scsi_host.h>
3 #include <scsi/scsi_cmnd.h>
4 #include <scsi/scsi_device.h>
5 #include <linux/usb.h>
6 #include <linux/slab.h>
7 
8 #include "usb.h"
9 #include "transport.h"
10 #include "protocol.h"
11 #include "scsiglue.h"
12 #include "sierra_ms.h"
13 #include "debug.h"
14 
15 #define SWIMS_USB_REQUEST_SetSwocMode	0x0B
16 #define SWIMS_USB_REQUEST_GetSwocInfo	0x0A
17 #define SWIMS_USB_INDEX_SetMode		0x0000
18 #define SWIMS_SET_MODE_Modem		0x0001
19 
20 #define TRU_NORMAL 			0x01
21 #define TRU_FORCE_MS 			0x02
22 #define TRU_FORCE_MODEM 		0x03
23 
24 static unsigned int swi_tru_install = 1;
25 module_param(swi_tru_install, uint, S_IRUGO | S_IWUSR);
26 MODULE_PARM_DESC(swi_tru_install, "TRU-Install mode (1=Full Logic (def),"
27 		 " 2=Force CD-Rom, 3=Force Modem)");
28 
29 struct swoc_info {
30 	__u8 rev;
31 	__u8 reserved[8];
32 	__u16 LinuxSKU;
33 	__u16 LinuxVer;
34 	__u8 reserved2[47];
35 } __attribute__((__packed__));
36 
37 static bool containsFullLinuxPackage(struct swoc_info *swocInfo)
38 {
39 	if ((swocInfo->LinuxSKU >= 0x2100 && swocInfo->LinuxSKU <= 0x2FFF) ||
40 	   (swocInfo->LinuxSKU >= 0x7100 && swocInfo->LinuxSKU <= 0x7FFF))
41 		return true;
42 	else
43 		return false;
44 }
45 
46 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
47 {
48 	int result;
49 	US_DEBUGP("SWIMS: %s", "DEVICE MODE SWITCH\n");
50 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
51 			SWIMS_USB_REQUEST_SetSwocMode,	/* __u8 request      */
52 			USB_TYPE_VENDOR | USB_DIR_OUT,	/* __u8 request type */
53 			eSWocMode,			/* __u16 value       */
54 			0x0000,				/* __u16 index       */
55 			NULL,				/* void *data        */
56 			0,				/* __u16 size 	     */
57 			USB_CTRL_SET_TIMEOUT);		/* int timeout       */
58 	return result;
59 }
60 
61 
62 static int sierra_get_swoc_info(struct usb_device *udev,
63 				struct swoc_info *swocInfo)
64 {
65 	int result;
66 
67 	US_DEBUGP("SWIMS: Attempting to get TRU-Install info.\n");
68 
69 	result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
70 			SWIMS_USB_REQUEST_GetSwocInfo,	/* __u8 request      */
71 			USB_TYPE_VENDOR | USB_DIR_IN,	/* __u8 request type */
72 			0,				/* __u16 value       */
73 			0,				/* __u16 index       */
74 			(void *) swocInfo,		/* void *data        */
75 			sizeof(struct swoc_info),	/* __u16 size 	     */
76 			USB_CTRL_SET_TIMEOUT);		/* int timeout 	     */
77 
78 	swocInfo->LinuxSKU = le16_to_cpu(swocInfo->LinuxSKU);
79 	swocInfo->LinuxVer = le16_to_cpu(swocInfo->LinuxVer);
80 	return result;
81 }
82 
83 static void debug_swoc(struct swoc_info *swocInfo)
84 {
85 	US_DEBUGP("SWIMS: SWoC Rev: %02d \n", swocInfo->rev);
86 	US_DEBUGP("SWIMS: Linux SKU: %04X \n", swocInfo->LinuxSKU);
87 	US_DEBUGP("SWIMS: Linux Version: %04X \n", swocInfo->LinuxVer);
88 }
89 
90 
91 static ssize_t show_truinst(struct device *dev, struct device_attribute *attr,
92 			char *buf)
93 {
94 	struct swoc_info *swocInfo;
95 	struct usb_interface *intf = to_usb_interface(dev);
96 	struct usb_device *udev = interface_to_usbdev(intf);
97 	int result;
98 	if (swi_tru_install == TRU_FORCE_MS) {
99 		result = snprintf(buf, PAGE_SIZE, "Forced Mass Storage\n");
100 	} else {
101 		swocInfo = kmalloc(sizeof(struct swoc_info), GFP_KERNEL);
102 		if (!swocInfo) {
103 			US_DEBUGP("SWIMS: Allocation failure\n");
104 			snprintf(buf, PAGE_SIZE, "Error\n");
105 			return -ENOMEM;
106 		}
107 		result = sierra_get_swoc_info(udev, swocInfo);
108 		if (result < 0) {
109 			US_DEBUGP("SWIMS: failed SWoC query\n");
110 			kfree(swocInfo);
111 			snprintf(buf, PAGE_SIZE, "Error\n");
112 			return -EIO;
113 		}
114 		debug_swoc(swocInfo);
115 		result = snprintf(buf, PAGE_SIZE,
116 			"REV=%02d SKU=%04X VER=%04X\n",
117 			swocInfo->rev,
118 			swocInfo->LinuxSKU,
119 			swocInfo->LinuxVer);
120 		kfree(swocInfo);
121 	}
122 	return result;
123 }
124 static DEVICE_ATTR(truinst, S_IWUGO | S_IRUGO, show_truinst, NULL);
125 
126 int sierra_ms_init(struct us_data *us)
127 {
128 	int result, retries;
129 	signed long delay_t;
130 	struct swoc_info *swocInfo;
131 	struct usb_device *udev;
132 	struct Scsi_Host *sh;
133 	struct scsi_device *sd;
134 
135 	delay_t = 2;
136 	retries = 3;
137 	result = 0;
138 	udev = us->pusb_dev;
139 
140 	sh = us_to_host(us);
141 	sd = scsi_get_host_dev(sh);
142 
143 	US_DEBUGP("SWIMS: sierra_ms_init called\n");
144 
145 	/* Force Modem mode */
146 	if (swi_tru_install == TRU_FORCE_MODEM) {
147 		US_DEBUGP("SWIMS: %s", "Forcing Modem Mode\n");
148 		result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
149 		if (result < 0)
150 			US_DEBUGP("SWIMS: Failed to switch to modem mode.\n");
151 		return -EIO;
152 	}
153 	/* Force Mass Storage mode (keep CD-Rom) */
154 	else if (swi_tru_install == TRU_FORCE_MS) {
155 		US_DEBUGP("SWIMS: %s", "Forcing Mass Storage Mode\n");
156 		goto complete;
157 	}
158 	/* Normal TRU-Install Logic */
159 	else {
160 		US_DEBUGP("SWIMS: %s", "Normal SWoC Logic\n");
161 
162 		swocInfo = kmalloc(sizeof(struct swoc_info),
163 				GFP_KERNEL);
164 		if (!swocInfo) {
165 			US_DEBUGP("SWIMS: %s", "Allocation failure\n");
166 			return -ENOMEM;
167 		}
168 
169 		retries = 3;
170 		do {
171 			retries--;
172 			result = sierra_get_swoc_info(udev, swocInfo);
173 			if (result < 0) {
174 				US_DEBUGP("SWIMS: %s", "Failed SWoC query\n");
175 				schedule_timeout_uninterruptible(2*HZ);
176 			}
177 		} while (retries && result < 0);
178 
179 		if (result < 0) {
180 			US_DEBUGP("SWIMS: %s",
181 				  "Completely failed SWoC query\n");
182 			kfree(swocInfo);
183 			return -EIO;
184 		}
185 
186 		debug_swoc(swocInfo);
187 
188 		/* If there is not Linux software on the TRU-Install device
189 		 * then switch to modem mode
190 		 */
191 		if (!containsFullLinuxPackage(swocInfo)) {
192 			US_DEBUGP("SWIMS: %s",
193 				"Switching to Modem Mode\n");
194 			result = sierra_set_ms_mode(udev,
195 				SWIMS_SET_MODE_Modem);
196 			if (result < 0)
197 				US_DEBUGP("SWIMS: Failed to switch modem\n");
198 			kfree(swocInfo);
199 			return -EIO;
200 		}
201 		kfree(swocInfo);
202 	}
203 complete:
204 	result = device_create_file(&us->pusb_intf->dev, &dev_attr_truinst);
205 
206 	return 0;
207 }
208 
209