blob: 1e7449d6d1209dc400e9dd8233421e3cd630f14b [file] [log] [blame]
Al Cho126bb032010-09-08 00:42:32 -07001#include <linux/sched.h>
2#include <linux/errno.h>
3#include <linux/slab.h>
4
5#include <scsi/scsi.h>
6#include <scsi/scsi_eh.h>
7#include <scsi/scsi_device.h>
8
9#include "usb.h"
10#include "scsiglue.h"
11#include "transport.h"
Rui Miguel Silvab7b14622014-04-12 01:33:11 +010012#include "smil.h"
Al Cho126bb032010-09-08 00:42:32 -070013#include "init.h"
14
Al Choe93192a2010-11-26 19:06:39 +080015/*
16 * ENE_InitMedia():
17 */
Al Cho126bb032010-09-08 00:42:32 -070018int ENE_InitMedia(struct us_data *us)
19{
20 int result;
Himangi Saraogi432f8572014-03-16 03:14:57 +053021 u8 MiscReg03 = 0;
Al Cho126bb032010-09-08 00:42:32 -070022
Laura Lawniczak29b31422013-06-06 18:10:47 +020023 dev_info(&us->pusb_dev->dev, "--- Init Media ---\n");
Katrina Prosise9799d122013-05-12 17:17:41 -070024 result = ene_read_byte(us, REG_CARD_STATUS, &MiscReg03);
Al Choe93192a2010-11-26 19:06:39 +080025 if (result != USB_STOR_XFER_GOOD) {
Laura Lawniczak6fbb90b2013-06-06 18:10:48 +020026 dev_err(&us->pusb_dev->dev, "Failed to read register\n");
Al Cho126bb032010-09-08 00:42:32 -070027 return USB_STOR_TRANSPORT_ERROR;
28 }
Laura Lawniczak29b31422013-06-06 18:10:47 +020029 dev_info(&us->pusb_dev->dev, "MiscReg03 = %x\n", MiscReg03);
Al Cho126bb032010-09-08 00:42:32 -070030
Al Choe93192a2010-11-26 19:06:39 +080031 if (MiscReg03 & 0x02) {
32 if (!us->SM_Status.Ready && !us->MS_Status.Ready) {
Al Cho126bb032010-09-08 00:42:32 -070033 result = ENE_SMInit(us);
William Blair383b7fc2012-06-10 15:10:33 -040034 if (result != USB_STOR_XFER_GOOD)
Cho, Yu-Chen20c3d7f2011-07-07 11:27:14 +080035 return USB_STOR_TRANSPORT_ERROR;
Al Cho126bb032010-09-08 00:42:32 -070036 }
37
38 }
39 return result;
40}
41
Al Choe93192a2010-11-26 19:06:39 +080042/*
Katrina Prosise9799d122013-05-12 17:17:41 -070043 * ene_read_byte() :
Al Choe93192a2010-11-26 19:06:39 +080044 */
Himangi Saraogi432f8572014-03-16 03:14:57 +053045int ene_read_byte(struct us_data *us, u16 index, void *buf)
Al Cho126bb032010-09-08 00:42:32 -070046{
47 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
48 int result;
49
Konstantin Katuev307ae1d2010-10-29 12:18:18 +110050 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -070051 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
52 bcb->DataTransferLength = 0x01;
53 bcb->Flags = 0x80;
54 bcb->CDB[0] = 0xED;
Himangi Saraogi432f8572014-03-16 03:14:57 +053055 bcb->CDB[2] = (u8)(index>>8);
56 bcb->CDB[3] = (u8)index;
Al Cho126bb032010-09-08 00:42:32 -070057
58 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
59 return result;
60}
61
Al Choe93192a2010-11-26 19:06:39 +080062/*
Al Choe93192a2010-11-26 19:06:39 +080063 *ENE_SMInit()
64 */
Al Cho126bb032010-09-08 00:42:32 -070065int ENE_SMInit(struct us_data *us)
66{
67 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
68 int result;
Himangi Saraogi432f8572014-03-16 03:14:57 +053069 u8 buf[0x200];
Al Cho126bb032010-09-08 00:42:32 -070070
Laura Lawniczak29b31422013-06-06 18:10:47 +020071 dev_dbg(&us->pusb_dev->dev, "transport --- ENE_SMInit\n");
Al Cho126bb032010-09-08 00:42:32 -070072
73 result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
Al Choe93192a2010-11-26 19:06:39 +080074 if (result != USB_STOR_XFER_GOOD) {
Laura Lawniczak6fbb90b2013-06-06 18:10:48 +020075 dev_info(&us->pusb_dev->dev,
76 "Failed to load SmartMedia init code\n: result= %x\n",
77 result);
Al Cho126bb032010-09-08 00:42:32 -070078 return USB_STOR_TRANSPORT_ERROR;
79 }
80
Konstantin Katuev307ae1d2010-10-29 12:18:18 +110081 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -070082 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
83 bcb->DataTransferLength = 0x200;
84 bcb->Flags = 0x80;
85 bcb->CDB[0] = 0xF1;
86 bcb->CDB[1] = 0x01;
87
88 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
Al Choe93192a2010-11-26 19:06:39 +080089 if (result != USB_STOR_XFER_GOOD) {
Laura Lawniczak6fbb90b2013-06-06 18:10:48 +020090 dev_err(&us->pusb_dev->dev,
91 "Failed to load SmartMedia init code: result = %x\n",
92 result);
Al Cho126bb032010-09-08 00:42:32 -070093 return USB_STOR_TRANSPORT_ERROR;
94 }
95
Johannes Schilling8be88042013-06-06 18:10:45 +020096 us->SM_Status = *(struct keucr_sm_status *)&buf[0];
Al Cho126bb032010-09-08 00:42:32 -070097
98 us->SM_DeviceID = buf[1];
99 us->SM_CardID = buf[2];
100
Al Choe93192a2010-11-26 19:06:39 +0800101 if (us->SM_Status.Insert && us->SM_Status.Ready) {
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200102 dev_info(&us->pusb_dev->dev, "Insert = %x\n",
103 us->SM_Status.Insert);
104 dev_info(&us->pusb_dev->dev, "Ready = %x\n",
105 us->SM_Status.Ready);
106 dev_info(&us->pusb_dev->dev, "WtP = %x\n",
107 us->SM_Status.WtP);
108 dev_info(&us->pusb_dev->dev, "DeviceID = %x\n",
109 us->SM_DeviceID);
110 dev_info(&us->pusb_dev->dev, "CardID = %x\n",
111 us->SM_CardID);
Al Cho126bb032010-09-08 00:42:32 -0700112 MediaChange = 1;
113 Check_D_MediaFmt(us);
Al Choe93192a2010-11-26 19:06:39 +0800114 } else {
Laura Lawniczak6fbb90b2013-06-06 18:10:48 +0200115 dev_err(&us->pusb_dev->dev,
116 "SmartMedia Card Not Ready --- %x\n", buf[0]);
Al Cho126bb032010-09-08 00:42:32 -0700117 return USB_STOR_TRANSPORT_ERROR;
118 }
119
120 return USB_STOR_TRANSPORT_GOOD;
121}
122
Al Choe93192a2010-11-26 19:06:39 +0800123/*
Al Choe93192a2010-11-26 19:06:39 +0800124 * ENE_LoadBinCode()
125 */
Himangi Saraogi432f8572014-03-16 03:14:57 +0530126int ENE_LoadBinCode(struct us_data *us, u8 flag)
Al Cho126bb032010-09-08 00:42:32 -0700127{
128 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
129 int result;
Al Choe93192a2010-11-26 19:06:39 +0800130 /* void *buf; */
Himangi Saraogi432f8572014-03-16 03:14:57 +0530131 u8 *buf;
Al Cho126bb032010-09-08 00:42:32 -0700132
Laura Lawniczak29b31422013-06-06 18:10:47 +0200133 /* dev_info(&us->pusb_dev->dev, "transport --- ENE_LoadBinCode\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700134 if (us->BIN_FLAG == flag)
135 return USB_STOR_TRANSPORT_GOOD;
136
137 buf = kmalloc(0x800, GFP_KERNEL);
Vasiliy Kulikovb1f5f542010-09-19 11:36:23 +0400138 if (buf == NULL)
139 return USB_STOR_TRANSPORT_ERROR;
Al Choe93192a2010-11-26 19:06:39 +0800140 switch (flag) {
Al Choe93192a2010-11-26 19:06:39 +0800141 /* For SS */
142 case SM_INIT_PATTERN:
Laura Lawniczak29b31422013-06-06 18:10:47 +0200143 dev_dbg(&us->pusb_dev->dev, "SM_INIT_PATTERN\n");
Al Choe93192a2010-11-26 19:06:39 +0800144 memcpy(buf, SM_Init, 0x800);
Al Cho126bb032010-09-08 00:42:32 -0700145 break;
Al Choe93192a2010-11-26 19:06:39 +0800146 case SM_RW_PATTERN:
Laura Lawniczak29b31422013-06-06 18:10:47 +0200147 dev_dbg(&us->pusb_dev->dev, "SM_RW_PATTERN\n");
Al Choe93192a2010-11-26 19:06:39 +0800148 memcpy(buf, SM_Rdwr, 0x800);
Al Cho126bb032010-09-08 00:42:32 -0700149 break;
150 }
151
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100152 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700153 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
154 bcb->DataTransferLength = 0x800;
Al Choe93192a2010-11-26 19:06:39 +0800155 bcb->Flags = 0x00;
Al Cho126bb032010-09-08 00:42:32 -0700156 bcb->CDB[0] = 0xEF;
157
158 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
159
160 kfree(buf);
161 us->BIN_FLAG = flag;
162 return result;
163}
164
Al Choe93192a2010-11-26 19:06:39 +0800165/*
166 * ENE_SendScsiCmd():
167 */
Himangi Saraogi432f8572014-03-16 03:14:57 +0530168int ENE_SendScsiCmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
Al Cho126bb032010-09-08 00:42:32 -0700169{
170 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
171 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
172
173 int result;
Al Choe93192a2010-11-26 19:06:39 +0800174 unsigned int transfer_length = bcb->DataTransferLength,
175 cswlen = 0, partial = 0;
Al Cho126bb032010-09-08 00:42:32 -0700176 unsigned int residue;
177
Laura Lawniczak29b31422013-06-06 18:10:47 +0200178 /* dev_dbg(&us->pusb_dev->dev, "transport --- ENE_SendScsiCmd\n"); */
Al Choe93192a2010-11-26 19:06:39 +0800179 /* send cmd to out endpoint */
180 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
181 bcb, US_BULK_CB_WRAP_LEN, NULL);
182 if (result != USB_STOR_XFER_GOOD) {
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200183 dev_err(&us->pusb_dev->dev,
184 "send cmd to out endpoint fail ---\n");
Al Cho126bb032010-09-08 00:42:32 -0700185 return USB_STOR_TRANSPORT_ERROR;
186 }
187
Al Choe93192a2010-11-26 19:06:39 +0800188 if (buf) {
Al Cho6efe04e2010-11-26 19:07:27 +0800189 unsigned int pipe = fDir;
190
191 if (fDir == FDIR_READ)
192 pipe = us->recv_bulk_pipe;
193 else
194 pipe = us->send_bulk_pipe;
195
Al Choe93192a2010-11-26 19:06:39 +0800196 /* Bulk */
Al Cho126bb032010-09-08 00:42:32 -0700197 if (use_sg)
198 result = usb_stor_bulk_srb(us, pipe, us->srb);
199 else
Al Choe93192a2010-11-26 19:06:39 +0800200 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
201 transfer_length, 0, &partial);
202 if (result != USB_STOR_XFER_GOOD) {
Laura Lawniczak29b31422013-06-06 18:10:47 +0200203 dev_err(&us->pusb_dev->dev, "data transfer fail ---\n");
Al Cho126bb032010-09-08 00:42:32 -0700204 return USB_STOR_TRANSPORT_ERROR;
205 }
206 }
207
Al Choe93192a2010-11-26 19:06:39 +0800208 /* Get CSW for device status */
209 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
210 US_BULK_CS_WRAP_LEN, &cswlen);
Al Cho126bb032010-09-08 00:42:32 -0700211
Al Choe93192a2010-11-26 19:06:39 +0800212 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200213 dev_warn(&us->pusb_dev->dev,
214 "Received 0-length CSW; retrying...\n");
Al Choe93192a2010-11-26 19:06:39 +0800215 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
216 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
Al Cho126bb032010-09-08 00:42:32 -0700217 }
218
Al Choe93192a2010-11-26 19:06:39 +0800219 if (result == USB_STOR_XFER_STALLED) {
Al Cho126bb032010-09-08 00:42:32 -0700220 /* get the status again */
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200221 dev_warn(&us->pusb_dev->dev,
222 "Attempting to get CSW (2nd try)...\n");
Al Choe93192a2010-11-26 19:06:39 +0800223 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
224 bcs, US_BULK_CS_WRAP_LEN, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700225 }
226
227 if (result != USB_STOR_XFER_GOOD)
228 return USB_STOR_TRANSPORT_ERROR;
229
230 /* check bulk status */
231 residue = le32_to_cpu(bcs->Residue);
232
Al Choe93192a2010-11-26 19:06:39 +0800233 /*
234 * try to compute the actual residue, based on how much data
235 * was really transferred and what the device tells us
236 */
237 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
Al Cho126bb032010-09-08 00:42:32 -0700238 residue = min(residue, transfer_length);
huajun li41e568d2011-03-04 10:56:18 +0800239 if (us->srb)
240 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
241 (int) residue));
Al Cho126bb032010-09-08 00:42:32 -0700242 }
243
244 if (bcs->Status != US_BULK_STAT_OK)
245 return USB_STOR_TRANSPORT_ERROR;
246
247 return USB_STOR_TRANSPORT_GOOD;
248}
249
Al Choe93192a2010-11-26 19:06:39 +0800250/*
251 * ENE_Read_Data()
252 */
Al Cho126bb032010-09-08 00:42:32 -0700253int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
254{
255 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
256 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
257 int result;
258
Laura Lawniczak29b31422013-06-06 18:10:47 +0200259 /* dev_dbg(&us->pusb_dev->dev, "transport --- ENE_Read_Data\n"); */
Al Choe93192a2010-11-26 19:06:39 +0800260 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100261 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700262 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
263 bcb->DataTransferLength = length;
Al Choe93192a2010-11-26 19:06:39 +0800264 bcb->Flags = 0x80;
Al Cho126bb032010-09-08 00:42:32 -0700265 bcb->CDB[0] = 0xED;
266 bcb->CDB[2] = 0xFF;
267 bcb->CDB[3] = 0x81;
268
Al Choe93192a2010-11-26 19:06:39 +0800269 /* send cmd to out endpoint */
270 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
271 US_BULK_CB_WRAP_LEN, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700272 if (result != USB_STOR_XFER_GOOD)
273 return USB_STOR_TRANSPORT_ERROR;
274
Al Choe93192a2010-11-26 19:06:39 +0800275 /* R/W data */
276 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
277 buf, length, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700278 if (result != USB_STOR_XFER_GOOD)
279 return USB_STOR_TRANSPORT_ERROR;
280
Al Choe93192a2010-11-26 19:06:39 +0800281 /* Get CSW for device status */
282 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
283 US_BULK_CS_WRAP_LEN, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700284 if (result != USB_STOR_XFER_GOOD)
285 return USB_STOR_TRANSPORT_ERROR;
286 if (bcs->Status != US_BULK_STAT_OK)
287 return USB_STOR_TRANSPORT_ERROR;
288
289 return USB_STOR_TRANSPORT_GOOD;
290}
291
Al Choe93192a2010-11-26 19:06:39 +0800292/*
293 * ENE_Write_Data():
294 */
Al Cho126bb032010-09-08 00:42:32 -0700295int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
296{
297 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
298 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
299 int result;
300
Al Choe93192a2010-11-26 19:06:39 +0800301 /* printk("transport --- ENE_Write_Data\n"); */
302 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100303 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700304 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
305 bcb->DataTransferLength = length;
Al Choe93192a2010-11-26 19:06:39 +0800306 bcb->Flags = 0x00;
Al Cho126bb032010-09-08 00:42:32 -0700307 bcb->CDB[0] = 0xEE;
308 bcb->CDB[2] = 0xFF;
309 bcb->CDB[3] = 0x81;
310
Al Choe93192a2010-11-26 19:06:39 +0800311 /* send cmd to out endpoint */
312 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
313 US_BULK_CB_WRAP_LEN, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700314 if (result != USB_STOR_XFER_GOOD)
315 return USB_STOR_TRANSPORT_ERROR;
316
Al Choe93192a2010-11-26 19:06:39 +0800317 /* R/W data */
318 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
319 buf, length, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700320 if (result != USB_STOR_XFER_GOOD)
321 return USB_STOR_TRANSPORT_ERROR;
322
Al Choe93192a2010-11-26 19:06:39 +0800323 /* Get CSW for device status */
324 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
325 US_BULK_CS_WRAP_LEN, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700326 if (result != USB_STOR_XFER_GOOD)
327 return USB_STOR_TRANSPORT_ERROR;
328 if (bcs->Status != US_BULK_STAT_OK)
329 return USB_STOR_TRANSPORT_ERROR;
330
331 return USB_STOR_TRANSPORT_GOOD;
332}
333