1 // SPDX-License-Identifier: GPL-2.0
3 #include <scsi/scsi_host.h>
4 #include <scsi/scsi_cmnd.h>
5 #include <scsi/scsi_device.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
11 #include "transport.h"
14 #include "sierra_ms.h"
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
22 #define TRU_NORMAL 0x01
23 #define TRU_FORCE_MS 0x02
24 #define TRU_FORCE_MODEM 0x03
26 static unsigned int swi_tru_install
= 1;
27 module_param(swi_tru_install
, uint
, S_IRUGO
| S_IWUSR
);
28 MODULE_PARM_DESC(swi_tru_install
, "TRU-Install mode (1=Full Logic (def),"
29 " 2=Force CD-Rom, 3=Force Modem)");
37 } __attribute__((__packed__
));
39 static bool containsFullLinuxPackage(struct swoc_info
*swocInfo
)
41 if ((swocInfo
->LinuxSKU
>= 0x2100 && swocInfo
->LinuxSKU
<= 0x2FFF) ||
42 (swocInfo
->LinuxSKU
>= 0x7100 && swocInfo
->LinuxSKU
<= 0x7FFF))
48 static int sierra_set_ms_mode(struct usb_device
*udev
, __u16 eSWocMode
)
51 dev_dbg(&udev
->dev
, "SWIMS: %s", "DEVICE MODE SWITCH\n");
52 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 */
59 USB_CTRL_SET_TIMEOUT
); /* int timeout */
64 static int sierra_get_swoc_info(struct usb_device
*udev
,
65 struct swoc_info
*swocInfo
)
69 dev_dbg(&udev
->dev
, "SWIMS: Attempting to get TRU-Install info\n");
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 */
76 (void *) swocInfo
, /* void *data */
77 sizeof(struct swoc_info
), /* __u16 size */
78 USB_CTRL_SET_TIMEOUT
); /* int timeout */
80 swocInfo
->LinuxSKU
= le16_to_cpu(swocInfo
->LinuxSKU
);
81 swocInfo
->LinuxVer
= le16_to_cpu(swocInfo
->LinuxVer
);
85 static void debug_swoc(const struct device
*dev
, struct swoc_info
*swocInfo
)
87 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
);
93 static ssize_t
truinst_show(struct device
*dev
, struct device_attribute
*attr
,
96 struct swoc_info
*swocInfo
;
97 struct usb_interface
*intf
= to_usb_interface(dev
);
98 struct usb_device
*udev
= interface_to_usbdev(intf
);
100 if (swi_tru_install
== TRU_FORCE_MS
) {
101 result
= snprintf(buf
, PAGE_SIZE
, "Forced Mass Storage\n");
103 swocInfo
= kmalloc(sizeof(struct swoc_info
), GFP_KERNEL
);
105 snprintf(buf
, PAGE_SIZE
, "Error\n");
108 result
= sierra_get_swoc_info(udev
, swocInfo
);
110 dev_dbg(dev
, "SWIMS: failed SWoC query\n");
112 snprintf(buf
, PAGE_SIZE
, "Error\n");
115 debug_swoc(dev
, swocInfo
);
116 result
= snprintf(buf
, PAGE_SIZE
,
117 "REV=%02d SKU=%04X VER=%04X\n",
125 static DEVICE_ATTR_RO(truinst
);
127 int sierra_ms_init(struct us_data
*us
)
130 struct swoc_info
*swocInfo
;
131 struct usb_device
*udev
;
137 /* Force Modem mode */
138 if (swi_tru_install
== TRU_FORCE_MODEM
) {
139 usb_stor_dbg(us
, "SWIMS: Forcing Modem Mode\n");
140 result
= sierra_set_ms_mode(udev
, SWIMS_SET_MODE_Modem
);
142 usb_stor_dbg(us
, "SWIMS: Failed to switch to modem mode\n");
145 /* Force Mass Storage mode (keep CD-Rom) */
146 else if (swi_tru_install
== TRU_FORCE_MS
) {
147 usb_stor_dbg(us
, "SWIMS: Forcing Mass Storage Mode\n");
150 /* Normal TRU-Install Logic */
152 usb_stor_dbg(us
, "SWIMS: Normal SWoC Logic\n");
154 swocInfo
= kmalloc(sizeof(struct swoc_info
),
162 result
= sierra_get_swoc_info(udev
, swocInfo
);
164 usb_stor_dbg(us
, "SWIMS: Failed SWoC query\n");
165 schedule_timeout_uninterruptible(2*HZ
);
167 } while (retries
&& result
< 0);
170 usb_stor_dbg(us
, "SWIMS: Completely failed SWoC query\n");
175 debug_swoc(&us
->pusb_dev
->dev
, swocInfo
);
178 * If there is not Linux software on the TRU-Install device
179 * then switch to modem mode
181 if (!containsFullLinuxPackage(swocInfo
)) {
182 usb_stor_dbg(us
, "SWIMS: Switching to Modem Mode\n");
183 result
= sierra_set_ms_mode(udev
,
184 SWIMS_SET_MODE_Modem
);
186 usb_stor_dbg(us
, "SWIMS: Failed to switch modem\n");
193 return device_create_file(&us
->pusb_intf
->dev
, &dev_attr_truinst
);