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 
containsFullLinuxPackage(struct swoc_info * swocInfo)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 
sierra_set_ms_mode(struct usb_device * udev,__u16 eSWocMode)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 
sierra_get_swoc_info(struct usb_device * udev,struct swoc_info * swocInfo)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 
debug_swoc(struct swoc_info * swocInfo)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 
show_truinst(struct device * dev,struct device_attribute * attr,char * buf)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_IRUGO, show_truinst, NULL);
125 
sierra_ms_init(struct us_data * us)126 int sierra_ms_init(struct us_data *us)
127 {
128 	int result, retries;
129 	struct swoc_info *swocInfo;
130 	struct usb_device *udev;
131 	struct Scsi_Host *sh;
132 	struct scsi_device *sd;
133 
134 	retries = 3;
135 	result = 0;
136 	udev = us->pusb_dev;
137 
138 	sh = us_to_host(us);
139 	sd = scsi_get_host_dev(sh);
140 
141 	US_DEBUGP("SWIMS: sierra_ms_init called\n");
142 
143 	/* Force Modem mode */
144 	if (swi_tru_install == TRU_FORCE_MODEM) {
145 		US_DEBUGP("SWIMS: %s", "Forcing Modem Mode\n");
146 		result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
147 		if (result < 0)
148 			US_DEBUGP("SWIMS: Failed to switch to modem mode.\n");
149 		return -EIO;
150 	}
151 	/* Force Mass Storage mode (keep CD-Rom) */
152 	else if (swi_tru_install == TRU_FORCE_MS) {
153 		US_DEBUGP("SWIMS: %s", "Forcing Mass Storage Mode\n");
154 		goto complete;
155 	}
156 	/* Normal TRU-Install Logic */
157 	else {
158 		US_DEBUGP("SWIMS: %s", "Normal SWoC Logic\n");
159 
160 		swocInfo = kmalloc(sizeof(struct swoc_info),
161 				GFP_KERNEL);
162 		if (!swocInfo) {
163 			US_DEBUGP("SWIMS: %s", "Allocation failure\n");
164 			return -ENOMEM;
165 		}
166 
167 		retries = 3;
168 		do {
169 			retries--;
170 			result = sierra_get_swoc_info(udev, swocInfo);
171 			if (result < 0) {
172 				US_DEBUGP("SWIMS: %s", "Failed SWoC query\n");
173 				schedule_timeout_uninterruptible(2*HZ);
174 			}
175 		} while (retries && result < 0);
176 
177 		if (result < 0) {
178 			US_DEBUGP("SWIMS: %s",
179 				  "Completely failed SWoC query\n");
180 			kfree(swocInfo);
181 			return -EIO;
182 		}
183 
184 		debug_swoc(swocInfo);
185 
186 		/* If there is not Linux software on the TRU-Install device
187 		 * then switch to modem mode
188 		 */
189 		if (!containsFullLinuxPackage(swocInfo)) {
190 			US_DEBUGP("SWIMS: %s",
191 				"Switching to Modem Mode\n");
192 			result = sierra_set_ms_mode(udev,
193 				SWIMS_SET_MODE_Modem);
194 			if (result < 0)
195 				US_DEBUGP("SWIMS: Failed to switch modem\n");
196 			kfree(swocInfo);
197 			return -EIO;
198 		}
199 		kfree(swocInfo);
200 	}
201 complete:
202 	result = device_create_file(&us->pusb_intf->dev, &dev_attr_truinst);
203 
204 	return 0;
205 }
206 
207