Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sierra_ms.c
Go to the documentation of this file.
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/module.h>
7 #include <linux/slab.h>
8 
9 #include "usb.h"
10 #include "transport.h"
11 #include "protocol.h"
12 #include "scsiglue.h"
13 #include "sierra_ms.h"
14 #include "debug.h"
15 
16 #define SWIMS_USB_REQUEST_SetSwocMode 0x0B
17 #define SWIMS_USB_REQUEST_GetSwocInfo 0x0A
18 #define SWIMS_USB_INDEX_SetMode 0x0000
19 #define SWIMS_SET_MODE_Modem 0x0001
20 
21 #define TRU_NORMAL 0x01
22 #define TRU_FORCE_MS 0x02
23 #define TRU_FORCE_MODEM 0x03
24 
25 static unsigned int swi_tru_install = 1;
26 module_param(swi_tru_install, uint, S_IRUGO | S_IWUSR);
27 MODULE_PARM_DESC(swi_tru_install, "TRU-Install mode (1=Full Logic (def),"
28  " 2=Force CD-Rom, 3=Force Modem)");
29 
30 struct swoc_info {
36 } __attribute__((__packed__));
37 
38 static bool containsFullLinuxPackage(struct swoc_info *swocInfo)
39 {
40  if ((swocInfo->LinuxSKU >= 0x2100 && swocInfo->LinuxSKU <= 0x2FFF) ||
41  (swocInfo->LinuxSKU >= 0x7100 && swocInfo->LinuxSKU <= 0x7FFF))
42  return true;
43  else
44  return false;
45 }
46 
47 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
48 {
49  int result;
50  US_DEBUGP("SWIMS: %s", "DEVICE MODE SWITCH\n");
51  result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
52  SWIMS_USB_REQUEST_SetSwocMode, /* __u8 request */
53  USB_TYPE_VENDOR | USB_DIR_OUT, /* __u8 request type */
54  eSWocMode, /* __u16 value */
55  0x0000, /* __u16 index */
56  NULL, /* void *data */
57  0, /* __u16 size */
58  USB_CTRL_SET_TIMEOUT); /* int timeout */
59  return result;
60 }
61 
62 
63 static int sierra_get_swoc_info(struct usb_device *udev,
64  struct swoc_info *swocInfo)
65 {
66  int result;
67 
68  US_DEBUGP("SWIMS: Attempting to get TRU-Install info.\n");
69 
70  result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
71  SWIMS_USB_REQUEST_GetSwocInfo, /* __u8 request */
72  USB_TYPE_VENDOR | USB_DIR_IN, /* __u8 request type */
73  0, /* __u16 value */
74  0, /* __u16 index */
75  (void *) swocInfo, /* void *data */
76  sizeof(struct swoc_info), /* __u16 size */
77  USB_CTRL_SET_TIMEOUT); /* int timeout */
78 
79  swocInfo->LinuxSKU = le16_to_cpu(swocInfo->LinuxSKU);
80  swocInfo->LinuxVer = le16_to_cpu(swocInfo->LinuxVer);
81  return result;
82 }
83 
84 static void debug_swoc(struct swoc_info *swocInfo)
85 {
86  US_DEBUGP("SWIMS: SWoC Rev: %02d \n", swocInfo->rev);
87  US_DEBUGP("SWIMS: Linux SKU: %04X \n", swocInfo->LinuxSKU);
88  US_DEBUGP("SWIMS: Linux Version: %04X \n", swocInfo->LinuxVer);
89 }
90 
91 
92 static ssize_t show_truinst(struct device *dev, struct device_attribute *attr,
93  char *buf)
94 {
95  struct swoc_info *swocInfo;
96  struct usb_interface *intf = to_usb_interface(dev);
97  struct usb_device *udev = interface_to_usbdev(intf);
98  int result;
99  if (swi_tru_install == TRU_FORCE_MS) {
100  result = snprintf(buf, PAGE_SIZE, "Forced Mass Storage\n");
101  } else {
102  swocInfo = kmalloc(sizeof(struct swoc_info), GFP_KERNEL);
103  if (!swocInfo) {
104  US_DEBUGP("SWIMS: Allocation failure\n");
105  snprintf(buf, PAGE_SIZE, "Error\n");
106  return -ENOMEM;
107  }
108  result = sierra_get_swoc_info(udev, swocInfo);
109  if (result < 0) {
110  US_DEBUGP("SWIMS: failed SWoC query\n");
111  kfree(swocInfo);
112  snprintf(buf, PAGE_SIZE, "Error\n");
113  return -EIO;
114  }
115  debug_swoc(swocInfo);
116  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 }
125 static DEVICE_ATTR(truinst, S_IRUGO, show_truinst, NULL);
126 
127 int sierra_ms_init(struct us_data *us)
128 {
129  int result, retries;
130  struct swoc_info *swocInfo;
131  struct usb_device *udev;
132  struct Scsi_Host *sh;
133 
134  retries = 3;
135  result = 0;
136  udev = us->pusb_dev;
137 
138  sh = us_to_host(us);
139  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");
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,
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