aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/storage
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/storage
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/usb/storage')
-rw-r--r--drivers/usb/storage/Kconfig113
-rw-r--r--drivers/usb/storage/Makefile23
-rw-r--r--drivers/usb/storage/datafab.c669
-rw-r--r--drivers/usb/storage/datafab.h40
-rw-r--r--drivers/usb/storage/debug.c177
-rw-r--r--drivers/usb/storage/debug.h65
-rw-r--r--drivers/usb/storage/dpcm.c89
-rw-r--r--drivers/usb/storage/dpcm.h34
-rw-r--r--drivers/usb/storage/freecom.c488
-rw-r--r--drivers/usb/storage/freecom.h36
-rw-r--r--drivers/usb/storage/initializers.c93
-rw-r--r--drivers/usb/storage/initializers.h54
-rw-r--r--drivers/usb/storage/isd200.c1442
-rw-r--r--drivers/usb/storage/isd200.h31
-rw-r--r--drivers/usb/storage/jumpshot.c596
-rw-r--r--drivers/usb/storage/jumpshot.h39
-rw-r--r--drivers/usb/storage/protocol.c254
-rw-r--r--drivers/usb/storage/protocol.h74
-rw-r--r--drivers/usb/storage/scsiglue.c500
-rw-r--r--drivers/usb/storage/scsiglue.h49
-rw-r--r--drivers/usb/storage/sddr09.c1608
-rw-r--r--drivers/usb/storage/sddr09.h48
-rw-r--r--drivers/usb/storage/sddr55.c938
-rw-r--r--drivers/usb/storage/sddr55.h34
-rw-r--r--drivers/usb/storage/shuttle_usbat.c1712
-rw-r--r--drivers/usb/storage/shuttle_usbat.h123
-rw-r--r--drivers/usb/storage/transport.c1215
-rw-r--r--drivers/usb/storage/transport.h174
-rw-r--r--drivers/usb/storage/unusual_devs.h986
-rw-r--r--drivers/usb/storage/usb.c1051
-rw-r--r--drivers/usb/storage/usb.h204
31 files changed, 12959 insertions, 0 deletions
diff --git a/drivers/usb/storage/Kconfig b/drivers/usb/storage/Kconfig
new file mode 100644
index 000000000000..f1f1c0608c22
--- /dev/null
+++ b/drivers/usb/storage/Kconfig
@@ -0,0 +1,113 @@
1#
2# USB Storage driver configuration
3#
4
5comment "NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support' may also be needed; see USB_STORAGE Help for more information"
6 depends on USB
7
8config USB_STORAGE
9 tristate "USB Mass Storage support"
10 depends on USB
11 select SCSI
12 ---help---
13 Say Y here if you want to connect USB mass storage devices to your
14 computer's USB port. This is the driver you need for USB
15 floppy drives, USB hard disks, USB tape drives, USB CD-ROMs,
16 USB flash devices, and memory sticks, along with
17 similar devices. This driver may also be used for some cameras
18 and card readers.
19
20 This option 'selects' (turns on, enables) 'SCSI', but you
21 probably also need 'SCSI device support: SCSI disk support'
22 (BLK_DEV_SD) for most USB storage devices.
23
24 To compile this driver as a module, choose M here: the
25 module will be called usb-storage.
26
27config USB_STORAGE_DEBUG
28 bool "USB Mass Storage verbose debug"
29 depends on USB_STORAGE
30 help
31 Say Y here in order to have the USB Mass Storage code generate
32 verbose debugging messages.
33
34config USB_STORAGE_DATAFAB
35 bool "Datafab Compact Flash Reader support (EXPERIMENTAL)"
36 depends on USB_STORAGE && EXPERIMENTAL
37 help
38 Support for certain Datafab CompactFlash readers.
39 Datafab has a web page at <http://www.datafabusa.com/>.
40
41config USB_STORAGE_FREECOM
42 bool "Freecom USB/ATAPI Bridge support"
43 depends on USB_STORAGE
44 help
45 Support for the Freecom USB to IDE/ATAPI adaptor.
46 Freecom has a web page at <http://www.freecom.de/>.
47
48config USB_STORAGE_ISD200
49 bool "ISD-200 USB/ATA Bridge support"
50 depends on USB_STORAGE && BLK_DEV_IDE
51 ---help---
52 Say Y here if you want to use USB Mass Store devices based
53 on the In-Systems Design ISD-200 USB/ATA bridge.
54
55 Some of the products that use this chip are:
56
57 - Archos Jukebox 6000
58 - ISD SmartCable for Storage
59 - Taiwan Skymaster CD530U/DEL-0241 IDE bridge
60 - Sony CRX10U CD-R/RW drive
61 - CyQ've CQ8060A CDRW drive
62 - Planex eXtreme Drive RX-25HU USB-IDE cable (not model RX-25U)
63
64config USB_STORAGE_DPCM
65 bool "Microtech/ZiO! CompactFlash/SmartMedia support"
66 depends on USB_STORAGE
67 help
68 Say Y here to support the Microtech/ZiO! CompactFlash reader.
69 There is a web page at <http://www.ziocorp.com/products/>.
70
71config USB_STORAGE_USBAT
72 bool "USBAT/USBAT02-based storage support (EXPERIMENTAL)"
73 depends on USB_STORAGE && EXPERIMENTAL
74 help
75 Say Y here to include additional code to support storage devices
76 based on the SCM/Shuttle USBAT/USBAT02 processors.
77
78 Devices reported to work with this driver include:
79 - CompactFlash reader included with Kodak DC3800 camera
80 - Dane-Elec Zmate CompactFlash reader
81 - Delkin Efilm reader2
82 - HP 8200e/8210e/8230e CD-Writer Plus drives
83 - I-JAM JS-50U
84 - Jessops CompactFlash JESDCFRU BLACK
85 - Kingston Technology PCREAD-USB/CF
86 - Maxell UA4 CompactFlash reader
87 - Memorex UCF-100
88 - Microtech ZiO! ICS-45 CF2
89 - RCA LYRA MP3 portable
90 - Sandisk ImageMate SDDR-05b
91
92config USB_STORAGE_SDDR09
93 bool "SanDisk SDDR-09 (and other SmartMedia) support (EXPERIMENTAL)"
94 depends on USB_STORAGE && EXPERIMENTAL
95 help
96 Say Y here to include additional code to support the Sandisk SDDR-09
97 SmartMedia reader in the USB Mass Storage driver.
98 Also works for the Microtech Zio! SmartMedia reader.
99
100config USB_STORAGE_SDDR55
101 bool "SanDisk SDDR-55 SmartMedia support (EXPERIMENTAL)"
102 depends on USB_STORAGE && EXPERIMENTAL
103 help
104 Say Y here to include additional code to support the Sandisk SDDR-55
105 SmartMedia reader in the USB Mass Storage driver.
106
107config USB_STORAGE_JUMPSHOT
108 bool "Lexar Jumpshot Compact Flash Reader (EXPERIMENTAL)"
109 depends on USB_STORAGE && EXPERIMENTAL
110 help
111 Say Y here to include additional code to support the Lexar Jumpshot
112 USB CompactFlash reader.
113
diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
new file mode 100644
index 000000000000..56652ccc2881
--- /dev/null
+++ b/drivers/usb/storage/Makefile
@@ -0,0 +1,23 @@
1#
2# Makefile for the USB Mass Storage device drivers.
3#
4# 15 Aug 2000, Christoph Hellwig <hch@infradead.org>
5# Rewritten to use lists instead of if-statements.
6#
7
8EXTRA_CFLAGS := -Idrivers/scsi
9
10obj-$(CONFIG_USB_STORAGE) += usb-storage.o
11
12usb-storage-obj-$(CONFIG_USB_STORAGE_DEBUG) += debug.o
13usb-storage-obj-$(CONFIG_USB_STORAGE_USBAT) += shuttle_usbat.o
14usb-storage-obj-$(CONFIG_USB_STORAGE_SDDR09) += sddr09.o
15usb-storage-obj-$(CONFIG_USB_STORAGE_SDDR55) += sddr55.o
16usb-storage-obj-$(CONFIG_USB_STORAGE_FREECOM) += freecom.o
17usb-storage-obj-$(CONFIG_USB_STORAGE_DPCM) += dpcm.o
18usb-storage-obj-$(CONFIG_USB_STORAGE_ISD200) += isd200.o
19usb-storage-obj-$(CONFIG_USB_STORAGE_DATAFAB) += datafab.o
20usb-storage-obj-$(CONFIG_USB_STORAGE_JUMPSHOT) += jumpshot.o
21
22usb-storage-objs := scsiglue.o protocol.o transport.o usb.o \
23 initializers.o $(usb-storage-obj-y)
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
new file mode 100644
index 000000000000..54e3e6c7ecd8
--- /dev/null
+++ b/drivers/usb/storage/datafab.c
@@ -0,0 +1,669 @@
1/* Driver for Datafab USB Compact Flash reader
2 *
3 * $Id: datafab.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
4 *
5 * datafab driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
11 *
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
14 *
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
17 *
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
19 *
20 * Other contributors:
21 * (c) 2002 Alan Stern <stern@rowland.org>
22 *
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2, or (at your option) any
26 * later version.
27 *
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, write to the Free Software Foundation, Inc.,
35 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
38/*
39 * This driver attempts to support USB CompactFlash reader/writer devices
40 * based on Datafab USB-to-ATA chips. It was specifically developed for the
41 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
42 * with a variety of Datafab-based devices from a number of manufacturers.
43 * I've received a report of this driver working with a Datafab-based
44 * SmartMedia device though please be aware that I'm personally unable to
45 * test SmartMedia support.
46 *
47 * This driver supports reading and writing. If you're truly paranoid,
48 * however, you can force the driver into a write-protected state by setting
49 * the WP enable bits in datafab_handle_mode_sense(). See the comments
50 * in that routine.
51 */
52
53#include <linux/sched.h>
54#include <linux/errno.h>
55#include <linux/slab.h>
56
57#include <scsi/scsi.h>
58#include <scsi/scsi_cmnd.h>
59
60#include "usb.h"
61#include "transport.h"
62#include "protocol.h"
63#include "debug.h"
64#include "datafab.h"
65
66static int datafab_determine_lun(struct us_data *us,
67 struct datafab_info *info);
68
69
70static inline int
71datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
72 if (len == 0)
73 return USB_STOR_XFER_GOOD;
74
75 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
76 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
77 data, len, NULL);
78}
79
80
81static inline int
82datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
83 if (len == 0)
84 return USB_STOR_XFER_GOOD;
85
86 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
87 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
88 data, len, NULL);
89}
90
91
92static int datafab_read_data(struct us_data *us,
93 struct datafab_info *info,
94 u32 sector,
95 u32 sectors)
96{
97 unsigned char *command = us->iobuf;
98 unsigned char *buffer;
99 unsigned char thistime;
100 unsigned int totallen, alloclen;
101 int len, result;
102 unsigned int sg_idx = 0, sg_offset = 0;
103
104 // we're working in LBA mode. according to the ATA spec,
105 // we can support up to 28-bit addressing. I don't know if Datafab
106 // supports beyond 24-bit addressing. It's kind of hard to test
107 // since it requires > 8GB CF card.
108 //
109 if (sectors > 0x0FFFFFFF)
110 return USB_STOR_TRANSPORT_ERROR;
111
112 if (info->lun == -1) {
113 result = datafab_determine_lun(us, info);
114 if (result != USB_STOR_TRANSPORT_GOOD)
115 return result;
116 }
117
118 totallen = sectors * info->ssize;
119
120 // Since we don't read more than 64 KB at a time, we have to create
121 // a bounce buffer and move the data a piece at a time between the
122 // bounce buffer and the actual transfer buffer.
123
124 alloclen = min(totallen, 65536u);
125 buffer = kmalloc(alloclen, GFP_NOIO);
126 if (buffer == NULL)
127 return USB_STOR_TRANSPORT_ERROR;
128
129 do {
130 // loop, never allocate or transfer more than 64k at once
131 // (min(128k, 255*info->ssize) is the real limit)
132
133 len = min(totallen, alloclen);
134 thistime = (len / info->ssize) & 0xff;
135
136 command[0] = 0;
137 command[1] = thistime;
138 command[2] = sector & 0xFF;
139 command[3] = (sector >> 8) & 0xFF;
140 command[4] = (sector >> 16) & 0xFF;
141
142 command[5] = 0xE0 + (info->lun << 4);
143 command[5] |= (sector >> 24) & 0x0F;
144 command[6] = 0x20;
145 command[7] = 0x01;
146
147 // send the read command
148 result = datafab_bulk_write(us, command, 8);
149 if (result != USB_STOR_XFER_GOOD)
150 goto leave;
151
152 // read the result
153 result = datafab_bulk_read(us, buffer, len);
154 if (result != USB_STOR_XFER_GOOD)
155 goto leave;
156
157 // Store the data in the transfer buffer
158 usb_stor_access_xfer_buf(buffer, len, us->srb,
159 &sg_idx, &sg_offset, TO_XFER_BUF);
160
161 sector += thistime;
162 totallen -= len;
163 } while (totallen > 0);
164
165 kfree(buffer);
166 return USB_STOR_TRANSPORT_GOOD;
167
168 leave:
169 kfree(buffer);
170 return USB_STOR_TRANSPORT_ERROR;
171}
172
173
174static int datafab_write_data(struct us_data *us,
175 struct datafab_info *info,
176 u32 sector,
177 u32 sectors)
178{
179 unsigned char *command = us->iobuf;
180 unsigned char *reply = us->iobuf;
181 unsigned char *buffer;
182 unsigned char thistime;
183 unsigned int totallen, alloclen;
184 int len, result;
185 unsigned int sg_idx = 0, sg_offset = 0;
186
187 // we're working in LBA mode. according to the ATA spec,
188 // we can support up to 28-bit addressing. I don't know if Datafab
189 // supports beyond 24-bit addressing. It's kind of hard to test
190 // since it requires > 8GB CF card.
191 //
192 if (sectors > 0x0FFFFFFF)
193 return USB_STOR_TRANSPORT_ERROR;
194
195 if (info->lun == -1) {
196 result = datafab_determine_lun(us, info);
197 if (result != USB_STOR_TRANSPORT_GOOD)
198 return result;
199 }
200
201 totallen = sectors * info->ssize;
202
203 // Since we don't write more than 64 KB at a time, we have to create
204 // a bounce buffer and move the data a piece at a time between the
205 // bounce buffer and the actual transfer buffer.
206
207 alloclen = min(totallen, 65536u);
208 buffer = kmalloc(alloclen, GFP_NOIO);
209 if (buffer == NULL)
210 return USB_STOR_TRANSPORT_ERROR;
211
212 do {
213 // loop, never allocate or transfer more than 64k at once
214 // (min(128k, 255*info->ssize) is the real limit)
215
216 len = min(totallen, alloclen);
217 thistime = (len / info->ssize) & 0xff;
218
219 // Get the data from the transfer buffer
220 usb_stor_access_xfer_buf(buffer, len, us->srb,
221 &sg_idx, &sg_offset, FROM_XFER_BUF);
222
223 command[0] = 0;
224 command[1] = thistime;
225 command[2] = sector & 0xFF;
226 command[3] = (sector >> 8) & 0xFF;
227 command[4] = (sector >> 16) & 0xFF;
228
229 command[5] = 0xE0 + (info->lun << 4);
230 command[5] |= (sector >> 24) & 0x0F;
231 command[6] = 0x30;
232 command[7] = 0x02;
233
234 // send the command
235 result = datafab_bulk_write(us, command, 8);
236 if (result != USB_STOR_XFER_GOOD)
237 goto leave;
238
239 // send the data
240 result = datafab_bulk_write(us, buffer, len);
241 if (result != USB_STOR_XFER_GOOD)
242 goto leave;
243
244 // read the result
245 result = datafab_bulk_read(us, reply, 2);
246 if (result != USB_STOR_XFER_GOOD)
247 goto leave;
248
249 if (reply[0] != 0x50 && reply[1] != 0) {
250 US_DEBUGP("datafab_write_data: Gah! "
251 "write return code: %02x %02x\n",
252 reply[0], reply[1]);
253 result = USB_STOR_TRANSPORT_ERROR;
254 goto leave;
255 }
256
257 sector += thistime;
258 totallen -= len;
259 } while (totallen > 0);
260
261 kfree(buffer);
262 return USB_STOR_TRANSPORT_GOOD;
263
264 leave:
265 kfree(buffer);
266 return USB_STOR_TRANSPORT_ERROR;
267}
268
269
270static int datafab_determine_lun(struct us_data *us,
271 struct datafab_info *info)
272{
273 // Dual-slot readers can be thought of as dual-LUN devices.
274 // We need to determine which card slot is being used.
275 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
276 //
277 // There might be a better way of doing this?
278
279 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
280 unsigned char *command = us->iobuf;
281 unsigned char *buf;
282 int count = 0, rc;
283
284 if (!us || !info)
285 return USB_STOR_TRANSPORT_ERROR;
286
287 memcpy(command, scommand, 8);
288 buf = kmalloc(512, GFP_NOIO);
289 if (!buf)
290 return USB_STOR_TRANSPORT_ERROR;
291
292 US_DEBUGP("datafab_determine_lun: locating...\n");
293
294 // we'll try 3 times before giving up...
295 //
296 while (count++ < 3) {
297 command[5] = 0xa0;
298
299 rc = datafab_bulk_write(us, command, 8);
300 if (rc != USB_STOR_XFER_GOOD) {
301 rc = USB_STOR_TRANSPORT_ERROR;
302 goto leave;
303 }
304
305 rc = datafab_bulk_read(us, buf, 512);
306 if (rc == USB_STOR_XFER_GOOD) {
307 info->lun = 0;
308 rc = USB_STOR_TRANSPORT_GOOD;
309 goto leave;
310 }
311
312 command[5] = 0xb0;
313
314 rc = datafab_bulk_write(us, command, 8);
315 if (rc != USB_STOR_XFER_GOOD) {
316 rc = USB_STOR_TRANSPORT_ERROR;
317 goto leave;
318 }
319
320 rc = datafab_bulk_read(us, buf, 512);
321 if (rc == USB_STOR_XFER_GOOD) {
322 info->lun = 1;
323 rc = USB_STOR_TRANSPORT_GOOD;
324 goto leave;
325 }
326
327 msleep(20);
328 }
329
330 rc = USB_STOR_TRANSPORT_ERROR;
331
332 leave:
333 kfree(buf);
334 return rc;
335}
336
337static int datafab_id_device(struct us_data *us,
338 struct datafab_info *info)
339{
340 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
341 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
342 // sets this bit so we do too...
343 //
344 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
345 unsigned char *command = us->iobuf;
346 unsigned char *reply;
347 int rc;
348
349 if (!us || !info)
350 return USB_STOR_TRANSPORT_ERROR;
351
352 if (info->lun == -1) {
353 rc = datafab_determine_lun(us, info);
354 if (rc != USB_STOR_TRANSPORT_GOOD)
355 return rc;
356 }
357
358 memcpy(command, scommand, 8);
359 reply = kmalloc(512, GFP_NOIO);
360 if (!reply)
361 return USB_STOR_TRANSPORT_ERROR;
362
363 command[5] += (info->lun << 4);
364
365 rc = datafab_bulk_write(us, command, 8);
366 if (rc != USB_STOR_XFER_GOOD) {
367 rc = USB_STOR_TRANSPORT_ERROR;
368 goto leave;
369 }
370
371 // we'll go ahead and extract the media capacity while we're here...
372 //
373 rc = datafab_bulk_read(us, reply, 512);
374 if (rc == USB_STOR_XFER_GOOD) {
375 // capacity is at word offset 57-58
376 //
377 info->sectors = ((u32)(reply[117]) << 24) |
378 ((u32)(reply[116]) << 16) |
379 ((u32)(reply[115]) << 8) |
380 ((u32)(reply[114]) );
381 rc = USB_STOR_TRANSPORT_GOOD;
382 goto leave;
383 }
384
385 rc = USB_STOR_TRANSPORT_ERROR;
386
387 leave:
388 kfree(reply);
389 return rc;
390}
391
392
393static int datafab_handle_mode_sense(struct us_data *us,
394 struct scsi_cmnd * srb,
395 int sense_6)
396{
397 static unsigned char rw_err_page[12] = {
398 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
399 };
400 static unsigned char cache_page[12] = {
401 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
402 };
403 static unsigned char rbac_page[12] = {
404 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
405 };
406 static unsigned char timer_page[8] = {
407 0x1C, 0x6, 0, 0, 0, 0
408 };
409 unsigned char pc, page_code;
410 unsigned int i = 0;
411 struct datafab_info *info = (struct datafab_info *) (us->extra);
412 unsigned char *ptr = us->iobuf;
413
414 // most of this stuff is just a hack to get things working. the
415 // datafab reader doesn't present a SCSI interface so we
416 // fudge the SCSI commands...
417 //
418
419 pc = srb->cmnd[2] >> 6;
420 page_code = srb->cmnd[2] & 0x3F;
421
422 switch (pc) {
423 case 0x0:
424 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
425 break;
426 case 0x1:
427 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
428 break;
429 case 0x2:
430 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
431 break;
432 case 0x3:
433 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
434 break;
435 }
436
437 memset(ptr, 0, 8);
438 if (sense_6) {
439 ptr[2] = 0x00; // WP enable: 0x80
440 i = 4;
441 } else {
442 ptr[3] = 0x00; // WP enable: 0x80
443 i = 8;
444 }
445
446 switch (page_code) {
447 default:
448 // vendor-specific mode
449 info->sense_key = 0x05;
450 info->sense_asc = 0x24;
451 info->sense_ascq = 0x00;
452 return USB_STOR_TRANSPORT_FAILED;
453
454 case 0x1:
455 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
456 i += sizeof(rw_err_page);
457 break;
458
459 case 0x8:
460 memcpy(ptr + i, cache_page, sizeof(cache_page));
461 i += sizeof(cache_page);
462 break;
463
464 case 0x1B:
465 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
466 i += sizeof(rbac_page);
467 break;
468
469 case 0x1C:
470 memcpy(ptr + i, timer_page, sizeof(timer_page));
471 i += sizeof(timer_page);
472 break;
473
474 case 0x3F: // retrieve all pages
475 memcpy(ptr + i, timer_page, sizeof(timer_page));
476 i += sizeof(timer_page);
477 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
478 i += sizeof(rbac_page);
479 memcpy(ptr + i, cache_page, sizeof(cache_page));
480 i += sizeof(cache_page);
481 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
482 i += sizeof(rw_err_page);
483 break;
484 }
485
486 if (sense_6)
487 ptr[0] = i - 1;
488 else
489 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
490 usb_stor_set_xfer_buf(ptr, i, srb);
491
492 return USB_STOR_TRANSPORT_GOOD;
493}
494
495static void datafab_info_destructor(void *extra)
496{
497 // this routine is a placeholder...
498 // currently, we don't allocate any extra memory so we're okay
499}
500
501
502// Transport for the Datafab MDCFE-B
503//
504int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
505{
506 struct datafab_info *info;
507 int rc;
508 unsigned long block, blocks;
509 unsigned char *ptr = us->iobuf;
510 static unsigned char inquiry_reply[8] = {
511 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
512 };
513
514 if (!us->extra) {
515 us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO);
516 if (!us->extra) {
517 US_DEBUGP("datafab_transport: Gah! "
518 "Can't allocate storage for Datafab info struct!\n");
519 return USB_STOR_TRANSPORT_ERROR;
520 }
521 memset(us->extra, 0, sizeof(struct datafab_info));
522 us->extra_destructor = datafab_info_destructor;
523 ((struct datafab_info *)us->extra)->lun = -1;
524 }
525
526 info = (struct datafab_info *) (us->extra);
527
528 if (srb->cmnd[0] == INQUIRY) {
529 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
530 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
531 fill_inquiry_response(us, ptr, 36);
532 return USB_STOR_TRANSPORT_GOOD;
533 }
534
535 if (srb->cmnd[0] == READ_CAPACITY) {
536 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
537 rc = datafab_id_device(us, info);
538 if (rc != USB_STOR_TRANSPORT_GOOD)
539 return rc;
540
541 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
542 info->sectors, info->ssize);
543
544 // build the reply
545 // we need the last sector, not the number of sectors
546 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
547 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
548 usb_stor_set_xfer_buf(ptr, 8, srb);
549
550 return USB_STOR_TRANSPORT_GOOD;
551 }
552
553 if (srb->cmnd[0] == MODE_SELECT_10) {
554 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
555 return USB_STOR_TRANSPORT_ERROR;
556 }
557
558 // don't bother implementing READ_6 or WRITE_6.
559 //
560 if (srb->cmnd[0] == READ_10) {
561 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
562 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
563
564 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
565
566 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
567 return datafab_read_data(us, info, block, blocks);
568 }
569
570 if (srb->cmnd[0] == READ_12) {
571 // we'll probably never see a READ_12 but we'll do it anyway...
572 //
573 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
574 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
575
576 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
577 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
578
579 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
580 return datafab_read_data(us, info, block, blocks);
581 }
582
583 if (srb->cmnd[0] == WRITE_10) {
584 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
585 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
586
587 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
588
589 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
590 return datafab_write_data(us, info, block, blocks);
591 }
592
593 if (srb->cmnd[0] == WRITE_12) {
594 // we'll probably never see a WRITE_12 but we'll do it anyway...
595 //
596 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
597 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
598
599 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
600 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
601
602 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
603 return datafab_write_data(us, info, block, blocks);
604 }
605
606 if (srb->cmnd[0] == TEST_UNIT_READY) {
607 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
608 return datafab_id_device(us, info);
609 }
610
611 if (srb->cmnd[0] == REQUEST_SENSE) {
612 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
613
614 // this response is pretty bogus right now. eventually if necessary
615 // we can set the correct sense data. so far though it hasn't been
616 // necessary
617 //
618 memset(ptr, 0, 18);
619 ptr[0] = 0xF0;
620 ptr[2] = info->sense_key;
621 ptr[7] = 11;
622 ptr[12] = info->sense_asc;
623 ptr[13] = info->sense_ascq;
624 usb_stor_set_xfer_buf(ptr, 18, srb);
625
626 return USB_STOR_TRANSPORT_GOOD;
627 }
628
629 if (srb->cmnd[0] == MODE_SENSE) {
630 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
631 return datafab_handle_mode_sense(us, srb, 1);
632 }
633
634 if (srb->cmnd[0] == MODE_SENSE_10) {
635 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
636 return datafab_handle_mode_sense(us, srb, 0);
637 }
638
639 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
640 // sure. whatever. not like we can stop the user from
641 // popping the media out of the device (no locking doors, etc)
642 //
643 return USB_STOR_TRANSPORT_GOOD;
644 }
645
646 if (srb->cmnd[0] == START_STOP) {
647 /* this is used by sd.c'check_scsidisk_media_change to detect
648 media change */
649 US_DEBUGP("datafab_transport: START_STOP.\n");
650 /* the first datafab_id_device after a media change returns
651 an error (determined experimentally) */
652 rc = datafab_id_device(us, info);
653 if (rc == USB_STOR_TRANSPORT_GOOD) {
654 info->sense_key = NO_SENSE;
655 srb->result = SUCCESS;
656 } else {
657 info->sense_key = UNIT_ATTENTION;
658 srb->result = SAM_STAT_CHECK_CONDITION;
659 }
660 return rc;
661 }
662
663 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
664 srb->cmnd[0], srb->cmnd[0]);
665 info->sense_key = 0x05;
666 info->sense_asc = 0x20;
667 info->sense_ascq = 0x00;
668 return USB_STOR_TRANSPORT_FAILED;
669}
diff --git a/drivers/usb/storage/datafab.h b/drivers/usb/storage/datafab.h
new file mode 100644
index 000000000000..32e3f271e582
--- /dev/null
+++ b/drivers/usb/storage/datafab.h
@@ -0,0 +1,40 @@
1/* Driver for Datafab MDCFE-B USB Compact Flash reader
2 * Header File
3 *
4 * Current development and maintenance by:
5 * (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
6 *
7 * See datafab.c for more explanation
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef _USB_DATAFAB_MDCFE_B_H
25#define _USB_DATAFAB_MDCFE_B_H
26
27extern int datafab_transport(struct scsi_cmnd *srb, struct us_data *us);
28
29struct datafab_info {
30 unsigned long sectors; // total sector count
31 unsigned long ssize; // sector size in bytes
32 signed char lun; // used for dual-slot readers
33
34 // the following aren't used yet
35 unsigned char sense_key;
36 unsigned long sense_asc; // additional sense code
37 unsigned long sense_ascq; // additional sense code qualifier
38};
39
40#endif
diff --git a/drivers/usb/storage/debug.c b/drivers/usb/storage/debug.c
new file mode 100644
index 000000000000..d76483706bc9
--- /dev/null
+++ b/drivers/usb/storage/debug.c
@@ -0,0 +1,177 @@
1/* Driver for USB Mass Storage compliant devices
2 * Debugging Functions Source Code File
3 *
4 * $Id: debug.c,v 1.9 2002/04/22 03:39:43 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Developed with the assistance of:
10 * (c) 2002 Alan Stern <stern@rowland.org>
11 *
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 *
15 * This driver is based on the 'USB Mass Storage Class' document. This
16 * describes in detail the protocol used to communicate with such
17 * devices. Clearly, the designers had SCSI and ATAPI commands in
18 * mind when they created this document. The commands are all very
19 * similar to commands in the SCSI-II and ATAPI specifications.
20 *
21 * It is important to note that in a number of cases this class
22 * exhibits class-specific exemptions from the USB specification.
23 * Notably the usage of NAK, STALL and ACK differs from the norm, in
24 * that they are used to communicate wait, failed and OK on commands.
25 *
26 * Also, for certain devices, the interrupt endpoint is used to convey
27 * status of a command.
28 *
29 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
30 * information about this driver.
31 *
32 * This program is free software; you can redistribute it and/or modify it
33 * under the terms of the GNU General Public License as published by the
34 * Free Software Foundation; either version 2, or (at your option) any
35 * later version.
36 *
37 * This program is distributed in the hope that it will be useful, but
38 * WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 * General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License along
43 * with this program; if not, write to the Free Software Foundation, Inc.,
44 * 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
46
47#include <linux/cdrom.h>
48#include <scsi/scsi.h>
49#include <scsi/scsi_cmnd.h>
50
51#include "debug.h"
52#include "scsi.h"
53
54
55void usb_stor_show_command(struct scsi_cmnd *srb)
56{
57 char *what = NULL;
58 int i;
59
60 switch (srb->cmnd[0]) {
61 case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;
62 case REZERO_UNIT: what = "REZERO_UNIT"; break;
63 case REQUEST_SENSE: what = "REQUEST_SENSE"; break;
64 case FORMAT_UNIT: what = "FORMAT_UNIT"; break;
65 case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;
66 case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;
67 case READ_6: what = "READ_6"; break;
68 case WRITE_6: what = "WRITE_6"; break;
69 case SEEK_6: what = "SEEK_6"; break;
70 case READ_REVERSE: what = "READ_REVERSE"; break;
71 case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;
72 case SPACE: what = "SPACE"; break;
73 case INQUIRY: what = "INQUIRY"; break;
74 case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;
75 case MODE_SELECT: what = "MODE_SELECT"; break;
76 case RESERVE: what = "RESERVE"; break;
77 case RELEASE: what = "RELEASE"; break;
78 case COPY: what = "COPY"; break;
79 case ERASE: what = "ERASE"; break;
80 case MODE_SENSE: what = "MODE_SENSE"; break;
81 case START_STOP: what = "START_STOP"; break;
82 case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;
83 case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;
84 case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;
85 case SET_WINDOW: what = "SET_WINDOW"; break;
86 case READ_CAPACITY: what = "READ_CAPACITY"; break;
87 case READ_10: what = "READ_10"; break;
88 case WRITE_10: what = "WRITE_10"; break;
89 case SEEK_10: what = "SEEK_10"; break;
90 case WRITE_VERIFY: what = "WRITE_VERIFY"; break;
91 case VERIFY: what = "VERIFY"; break;
92 case SEARCH_HIGH: what = "SEARCH_HIGH"; break;
93 case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;
94 case SEARCH_LOW: what = "SEARCH_LOW"; break;
95 case SET_LIMITS: what = "SET_LIMITS"; break;
96 case READ_POSITION: what = "READ_POSITION"; break;
97 case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;
98 case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;
99 case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;
100 case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;
101 case COMPARE: what = "COMPARE"; break;
102 case COPY_VERIFY: what = "COPY_VERIFY"; break;
103 case WRITE_BUFFER: what = "WRITE_BUFFER"; break;
104 case READ_BUFFER: what = "READ_BUFFER"; break;
105 case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;
106 case READ_LONG: what = "READ_LONG"; break;
107 case WRITE_LONG: what = "WRITE_LONG"; break;
108 case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;
109 case WRITE_SAME: what = "WRITE_SAME"; break;
110 case GPCMD_READ_SUBCHANNEL: what = "READ SUBCHANNEL"; break;
111 case READ_TOC: what = "READ_TOC"; break;
112 case GPCMD_READ_HEADER: what = "READ HEADER"; break;
113 case GPCMD_PLAY_AUDIO_10: what = "PLAY AUDIO (10)"; break;
114 case GPCMD_PLAY_AUDIO_MSF: what = "PLAY AUDIO MSF"; break;
115 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
116 what = "GET EVENT/STATUS NOTIFICATION"; break;
117 case GPCMD_PAUSE_RESUME: what = "PAUSE/RESUME"; break;
118 case LOG_SELECT: what = "LOG_SELECT"; break;
119 case LOG_SENSE: what = "LOG_SENSE"; break;
120 case GPCMD_STOP_PLAY_SCAN: what = "STOP PLAY/SCAN"; break;
121 case GPCMD_READ_DISC_INFO: what = "READ DISC INFORMATION"; break;
122 case GPCMD_READ_TRACK_RZONE_INFO:
123 what = "READ TRACK INFORMATION"; break;
124 case GPCMD_RESERVE_RZONE_TRACK: what = "RESERVE TRACK"; break;
125 case GPCMD_SEND_OPC: what = "SEND OPC"; break;
126 case MODE_SELECT_10: what = "MODE_SELECT_10"; break;
127 case GPCMD_REPAIR_RZONE_TRACK: what = "REPAIR TRACK"; break;
128 case 0x59: what = "READ MASTER CUE"; break;
129 case MODE_SENSE_10: what = "MODE_SENSE_10"; break;
130 case GPCMD_CLOSE_TRACK: what = "CLOSE TRACK/SESSION"; break;
131 case 0x5C: what = "READ BUFFER CAPACITY"; break;
132 case 0x5D: what = "SEND CUE SHEET"; break;
133 case GPCMD_BLANK: what = "BLANK"; break;
134 case MOVE_MEDIUM: what = "MOVE_MEDIUM or PLAY AUDIO (12)"; break;
135 case READ_12: what = "READ_12"; break;
136 case WRITE_12: what = "WRITE_12"; break;
137 case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;
138 case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;
139 case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;
140 case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;
141 case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;
142 case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;
143 case GPCMD_READ_CD_MSF: what = "READ CD MSF"; break;
144 case GPCMD_SCAN: what = "SCAN"; break;
145 case GPCMD_SET_SPEED: what = "SET CD SPEED"; break;
146 case GPCMD_MECHANISM_STATUS: what = "MECHANISM STATUS"; break;
147 case GPCMD_READ_CD: what = "READ CD"; break;
148 case 0xE1: what = "WRITE CONTINUE"; break;
149 case WRITE_LONG_2: what = "WRITE_LONG_2"; break;
150 default: what = "(unknown command)"; break;
151 }
152 US_DEBUGP("Command %s (%d bytes)\n", what, srb->cmd_len);
153 US_DEBUGP("");
154 for (i = 0; i < srb->cmd_len && i < 16; i++)
155 US_DEBUGPX(" %02x", srb->cmnd[i]);
156 US_DEBUGPX("\n");
157}
158
159void usb_stor_show_sense(
160 unsigned char key,
161 unsigned char asc,
162 unsigned char ascq) {
163
164 const char *what, *keystr;
165
166 keystr = scsi_sense_key_string(key);
167 what = scsi_extd_sense_format(asc, ascq);
168
169 if (keystr == NULL)
170 keystr = "(Unknown Key)";
171 if (what == NULL)
172 what = "(unknown ASC/ASCQ)";
173
174 US_DEBUGP("%s: ", keystr);
175 US_DEBUGPX(what, ascq);
176 US_DEBUGPX("\n");
177}
diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
new file mode 100644
index 000000000000..cd2096acc723
--- /dev/null
+++ b/drivers/usb/storage/debug.h
@@ -0,0 +1,65 @@
1/* Driver for USB Mass Storage compliant devices
2 * Debugging Functions Header File
3 *
4 * $Id: debug.h,v 1.6 2001/01/12 23:51:04 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Initial work by:
10 * (c) 1999 Michael Gee (michael@linuxspecific.com)
11 *
12 * This driver is based on the 'USB Mass Storage Class' document. This
13 * describes in detail the protocol used to communicate with such
14 * devices. Clearly, the designers had SCSI and ATAPI commands in
15 * mind when they created this document. The commands are all very
16 * similar to commands in the SCSI-II and ATAPI specifications.
17 *
18 * It is important to note that in a number of cases this class
19 * exhibits class-specific exemptions from the USB specification.
20 * Notably the usage of NAK, STALL and ACK differs from the norm, in
21 * that they are used to communicate wait, failed and OK on commands.
22 *
23 * Also, for certain devices, the interrupt endpoint is used to convey
24 * status of a command.
25 *
26 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
27 * information about this driver.
28 *
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License as published by the
31 * Free Software Foundation; either version 2, or (at your option) any
32 * later version.
33 *
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License along
40 * with this program; if not, write to the Free Software Foundation, Inc.,
41 * 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#ifndef _DEBUG_H_
45#define _DEBUG_H_
46
47#include <linux/config.h>
48#include <linux/kernel.h>
49
50#define USB_STORAGE "usb-storage: "
51
52#ifdef CONFIG_USB_STORAGE_DEBUG
53void usb_stor_show_command(struct scsi_cmnd *srb);
54void usb_stor_show_sense( unsigned char key,
55 unsigned char asc, unsigned char ascq );
56#define US_DEBUGP(x...) printk( KERN_DEBUG USB_STORAGE x )
57#define US_DEBUGPX(x...) printk( x )
58#define US_DEBUG(x) x
59#else
60#define US_DEBUGP(x...)
61#define US_DEBUGPX(x...)
62#define US_DEBUG(x)
63#endif
64
65#endif
diff --git a/drivers/usb/storage/dpcm.c b/drivers/usb/storage/dpcm.c
new file mode 100644
index 000000000000..92b69e4c8047
--- /dev/null
+++ b/drivers/usb/storage/dpcm.c
@@ -0,0 +1,89 @@
1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader
2 *
3 * $Id: dpcm.c,v 1.4 2001/06/11 02:54:25 mdharm Exp $
4 *
5 * DPCM driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Brian Webb (webbb@earthlink.net)
11 *
12 * This device contains both a CompactFlash card reader, which
13 * uses the Control/Bulk w/o Interrupt protocol and
14 * a SmartMedia card reader that uses the same protocol
15 * as the SDDR09.
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2, or (at your option) any
20 * later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32#include <linux/config.h>
33#include <scsi/scsi.h>
34#include <scsi/scsi_cmnd.h>
35#include <scsi/scsi_device.h>
36
37#include "usb.h"
38#include "transport.h"
39#include "protocol.h"
40#include "debug.h"
41#include "dpcm.h"
42#include "sddr09.h"
43
44/*
45 * Transport for the Microtech DPCM-USB
46 *
47 */
48int dpcm_transport(struct scsi_cmnd *srb, struct us_data *us)
49{
50 int ret;
51
52 if(srb == NULL)
53 return USB_STOR_TRANSPORT_ERROR;
54
55 US_DEBUGP("dpcm_transport: LUN=%d\n", srb->device->lun);
56
57 switch(srb->device->lun) {
58 case 0:
59
60 /*
61 * LUN 0 corresponds to the CompactFlash card reader.
62 */
63 ret = usb_stor_CB_transport(srb, us);
64 break;
65
66#ifdef CONFIG_USB_STORAGE_SDDR09
67 case 1:
68
69 /*
70 * LUN 1 corresponds to the SmartMedia card reader.
71 */
72
73 /*
74 * Set the LUN to 0 (just in case).
75 */
76 srb->device->lun = 0; us->srb->device->lun = 0;
77 ret = sddr09_transport(srb, us);
78 srb->device->lun = 1; us->srb->device->lun = 1;
79 break;
80
81#endif
82
83 default:
84 US_DEBUGP("dpcm_transport: Invalid LUN %d\n", srb->device->lun);
85 ret = USB_STOR_TRANSPORT_ERROR;
86 break;
87 }
88 return ret;
89}
diff --git a/drivers/usb/storage/dpcm.h b/drivers/usb/storage/dpcm.h
new file mode 100644
index 000000000000..81b464cfcc1e
--- /dev/null
+++ b/drivers/usb/storage/dpcm.h
@@ -0,0 +1,34 @@
1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader
2 *
3 * $Id: dpcm.h,v 1.2 2000/08/25 00:13:51 mdharm Exp $
4 *
5 * DPCM driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Brian Webb (webbb@earthlink.net)
11 *
12 * See dpcm.c for more explanation
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#ifndef _MICROTECH_DPCM_USB_H
30#define _MICROTECH_DPCM_USB_H
31
32extern int dpcm_transport(struct scsi_cmnd *srb, struct us_data *us);
33
34#endif
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
new file mode 100644
index 000000000000..30e96050fe0c
--- /dev/null
+++ b/drivers/usb/storage/freecom.c
@@ -0,0 +1,488 @@
1/* Driver for Freecom USB/IDE adaptor
2 *
3 * $Id: freecom.c,v 1.22 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Freecom v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (C) 2000 David Brown <usb-storage@davidb.org>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * This driver was developed with information provided in FREECOM's USB
27 * Programmers Reference Guide. For further information contact Freecom
28 * (http://www.freecom.de/)
29 */
30
31#include <linux/config.h>
32#include <linux/hdreg.h>
33
34#include <scsi/scsi.h>
35#include <scsi/scsi_cmnd.h>
36
37#include "usb.h"
38#include "transport.h"
39#include "protocol.h"
40#include "debug.h"
41#include "freecom.h"
42
43#ifdef CONFIG_USB_STORAGE_DEBUG
44static void pdump (void *, int);
45#endif
46
47/* Bits of HD_STATUS */
48#define ERR_STAT 0x01
49#define DRQ_STAT 0x08
50
51/* All of the outgoing packets are 64 bytes long. */
52struct freecom_cb_wrap {
53 u8 Type; /* Command type. */
54 u8 Timeout; /* Timeout in seconds. */
55 u8 Atapi[12]; /* An ATAPI packet. */
56 u8 Filler[50]; /* Padding Data. */
57};
58
59struct freecom_xfer_wrap {
60 u8 Type; /* Command type. */
61 u8 Timeout; /* Timeout in seconds. */
62 __le32 Count; /* Number of bytes to transfer. */
63 u8 Pad[58];
64} __attribute__ ((packed));
65
66struct freecom_ide_out {
67 u8 Type; /* Type + IDE register. */
68 u8 Pad;
69 __le16 Value; /* Value to write. */
70 u8 Pad2[60];
71};
72
73struct freecom_ide_in {
74 u8 Type; /* Type | IDE register. */
75 u8 Pad[63];
76};
77
78struct freecom_status {
79 u8 Status;
80 u8 Reason;
81 __le16 Count;
82 u8 Pad[60];
83};
84
85/* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
86 * register. */
87#define FCM_INT_STATUS 0x02 /* INDEX_STAT */
88#define FCM_STATUS_BUSY 0x80
89
90/* These are the packet types. The low bit indicates that this command
91 * should wait for an interrupt. */
92#define FCM_PACKET_ATAPI 0x21
93#define FCM_PACKET_STATUS 0x20
94
95/* Receive data from the IDE interface. The ATAPI packet has already
96 * waited, so the data should be immediately available. */
97#define FCM_PACKET_INPUT 0x81
98
99/* Send data to the IDE interface. */
100#define FCM_PACKET_OUTPUT 0x01
101
102/* Write a value to an ide register. Or the ide register to write after
103 * munging the address a bit. */
104#define FCM_PACKET_IDE_WRITE 0x40
105#define FCM_PACKET_IDE_READ 0xC0
106
107/* All packets (except for status) are 64 bytes long. */
108#define FCM_PACKET_LENGTH 64
109#define FCM_STATUS_PACKET_LENGTH 4
110
111static int
112freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
113 unsigned int ipipe, unsigned int opipe, int count)
114{
115 struct freecom_xfer_wrap *fxfr =
116 (struct freecom_xfer_wrap *) us->iobuf;
117 int result;
118
119 fxfr->Type = FCM_PACKET_INPUT | 0x00;
120 fxfr->Timeout = 0; /* Short timeout for debugging. */
121 fxfr->Count = cpu_to_le32 (count);
122 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
123
124 US_DEBUGP("Read data Freecom! (c=%d)\n", count);
125
126 /* Issue the transfer command. */
127 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
128 FCM_PACKET_LENGTH, NULL);
129 if (result != USB_STOR_XFER_GOOD) {
130 US_DEBUGP ("Freecom readdata transport error\n");
131 return USB_STOR_TRANSPORT_ERROR;
132 }
133
134 /* Now transfer all of our blocks. */
135 US_DEBUGP("Start of read\n");
136 result = usb_stor_bulk_transfer_sg(us, ipipe, srb->request_buffer,
137 count, srb->use_sg, &srb->resid);
138 US_DEBUGP("freecom_readdata done!\n");
139
140 if (result > USB_STOR_XFER_SHORT)
141 return USB_STOR_TRANSPORT_ERROR;
142 return USB_STOR_TRANSPORT_GOOD;
143}
144
145static int
146freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
147 int unsigned ipipe, unsigned int opipe, int count)
148{
149 struct freecom_xfer_wrap *fxfr =
150 (struct freecom_xfer_wrap *) us->iobuf;
151 int result;
152
153 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
154 fxfr->Timeout = 0; /* Short timeout for debugging. */
155 fxfr->Count = cpu_to_le32 (count);
156 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
157
158 US_DEBUGP("Write data Freecom! (c=%d)\n", count);
159
160 /* Issue the transfer command. */
161 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
162 FCM_PACKET_LENGTH, NULL);
163 if (result != USB_STOR_XFER_GOOD) {
164 US_DEBUGP ("Freecom writedata transport error\n");
165 return USB_STOR_TRANSPORT_ERROR;
166 }
167
168 /* Now transfer all of our blocks. */
169 US_DEBUGP("Start of write\n");
170 result = usb_stor_bulk_transfer_sg(us, opipe, srb->request_buffer,
171 count, srb->use_sg, &srb->resid);
172
173 US_DEBUGP("freecom_writedata done!\n");
174 if (result > USB_STOR_XFER_SHORT)
175 return USB_STOR_TRANSPORT_ERROR;
176 return USB_STOR_TRANSPORT_GOOD;
177}
178
179/*
180 * Transport for the Freecom USB/IDE adaptor.
181 *
182 */
183int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
184{
185 struct freecom_cb_wrap *fcb;
186 struct freecom_status *fst;
187 unsigned int ipipe, opipe; /* We need both pipes. */
188 int result;
189 unsigned int partial;
190 int length;
191
192 fcb = (struct freecom_cb_wrap *) us->iobuf;
193 fst = (struct freecom_status *) us->iobuf;
194
195 US_DEBUGP("Freecom TRANSPORT STARTED\n");
196
197 /* Get handles for both transports. */
198 opipe = us->send_bulk_pipe;
199 ipipe = us->recv_bulk_pipe;
200
201 /* The ATAPI Command always goes out first. */
202 fcb->Type = FCM_PACKET_ATAPI | 0x00;
203 fcb->Timeout = 0;
204 memcpy (fcb->Atapi, srb->cmnd, 12);
205 memset (fcb->Filler, 0, sizeof (fcb->Filler));
206
207 US_DEBUG(pdump (srb->cmnd, 12));
208
209 /* Send it out. */
210 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
211 FCM_PACKET_LENGTH, NULL);
212
213 /* The Freecom device will only fail if there is something wrong in
214 * USB land. It returns the status in its own registers, which
215 * come back in the bulk pipe. */
216 if (result != USB_STOR_XFER_GOOD) {
217 US_DEBUGP ("freecom transport error\n");
218 return USB_STOR_TRANSPORT_ERROR;
219 }
220
221 /* There are times we can optimize out this status read, but it
222 * doesn't hurt us to always do it now. */
223 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
224 FCM_STATUS_PACKET_LENGTH, &partial);
225 US_DEBUGP("foo Status result %d %u\n", result, partial);
226 if (result != USB_STOR_XFER_GOOD)
227 return USB_STOR_TRANSPORT_ERROR;
228
229 US_DEBUG(pdump ((void *) fst, partial));
230
231 /* The firmware will time-out commands after 20 seconds. Some commands
232 * can legitimately take longer than this, so we use a different
233 * command that only waits for the interrupt and then sends status,
234 * without having to send a new ATAPI command to the device.
235 *
236 * NOTE: There is some indication that a data transfer after a timeout
237 * may not work, but that is a condition that should never happen.
238 */
239 while (fst->Status & FCM_STATUS_BUSY) {
240 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
241 US_DEBUGP("fst->Status is %x\n", fst->Status);
242
243 /* Get the status again */
244 fcb->Type = FCM_PACKET_STATUS;
245 fcb->Timeout = 0;
246 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
247 memset (fcb->Filler, 0, sizeof (fcb->Filler));
248
249 /* Send it out. */
250 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
251 FCM_PACKET_LENGTH, NULL);
252
253 /* The Freecom device will only fail if there is something
254 * wrong in USB land. It returns the status in its own
255 * registers, which come back in the bulk pipe.
256 */
257 if (result != USB_STOR_XFER_GOOD) {
258 US_DEBUGP ("freecom transport error\n");
259 return USB_STOR_TRANSPORT_ERROR;
260 }
261
262 /* get the data */
263 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
264 FCM_STATUS_PACKET_LENGTH, &partial);
265
266 US_DEBUGP("bar Status result %d %u\n", result, partial);
267 if (result != USB_STOR_XFER_GOOD)
268 return USB_STOR_TRANSPORT_ERROR;
269
270 US_DEBUG(pdump ((void *) fst, partial));
271 }
272
273 if (partial != 4)
274 return USB_STOR_TRANSPORT_ERROR;
275 if ((fst->Status & 1) != 0) {
276 US_DEBUGP("operation failed\n");
277 return USB_STOR_TRANSPORT_FAILED;
278 }
279
280 /* The device might not have as much data available as we
281 * requested. If you ask for more than the device has, this reads
282 * and such will hang. */
283 US_DEBUGP("Device indicates that it has %d bytes available\n",
284 le16_to_cpu (fst->Count));
285 US_DEBUGP("SCSI requested %d\n", srb->request_bufflen);
286
287 /* Find the length we desire to read. */
288 switch (srb->cmnd[0]) {
289 case INQUIRY:
290 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
291 case MODE_SENSE:
292 case MODE_SENSE_10:
293 length = le16_to_cpu(fst->Count);
294 break;
295 default:
296 length = srb->request_bufflen;
297 }
298
299 /* verify that this amount is legal */
300 if (length > srb->request_bufflen) {
301 length = srb->request_bufflen;
302 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
303 }
304
305 /* What we do now depends on what direction the data is supposed to
306 * move in. */
307
308 switch (us->srb->sc_data_direction) {
309 case DMA_FROM_DEVICE:
310 /* catch bogus "read 0 length" case */
311 if (!length)
312 break;
313 /* Make sure that the status indicates that the device
314 * wants data as well. */
315 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
316 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
317 return USB_STOR_TRANSPORT_FAILED;
318 }
319 result = freecom_readdata (srb, us, ipipe, opipe, length);
320 if (result != USB_STOR_TRANSPORT_GOOD)
321 return result;
322
323 US_DEBUGP("FCM: Waiting for status\n");
324 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
325 FCM_PACKET_LENGTH, &partial);
326 US_DEBUG(pdump ((void *) fst, partial));
327
328 if (partial != 4 || result > USB_STOR_XFER_SHORT)
329 return USB_STOR_TRANSPORT_ERROR;
330 if ((fst->Status & ERR_STAT) != 0) {
331 US_DEBUGP("operation failed\n");
332 return USB_STOR_TRANSPORT_FAILED;
333 }
334 if ((fst->Reason & 3) != 3) {
335 US_DEBUGP("Drive seems still hungry\n");
336 return USB_STOR_TRANSPORT_FAILED;
337 }
338 US_DEBUGP("Transfer happy\n");
339 break;
340
341 case DMA_TO_DEVICE:
342 /* catch bogus "write 0 length" case */
343 if (!length)
344 break;
345 /* Make sure the status indicates that the device wants to
346 * send us data. */
347 /* !!IMPLEMENT!! */
348 result = freecom_writedata (srb, us, ipipe, opipe, length);
349 if (result != USB_STOR_TRANSPORT_GOOD)
350 return result;
351
352 US_DEBUGP("FCM: Waiting for status\n");
353 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
354 FCM_PACKET_LENGTH, &partial);
355
356 if (partial != 4 || result > USB_STOR_XFER_SHORT)
357 return USB_STOR_TRANSPORT_ERROR;
358 if ((fst->Status & ERR_STAT) != 0) {
359 US_DEBUGP("operation failed\n");
360 return USB_STOR_TRANSPORT_FAILED;
361 }
362 if ((fst->Reason & 3) != 3) {
363 US_DEBUGP("Drive seems still hungry\n");
364 return USB_STOR_TRANSPORT_FAILED;
365 }
366
367 US_DEBUGP("Transfer happy\n");
368 break;
369
370
371 case DMA_NONE:
372 /* Easy, do nothing. */
373 break;
374
375 default:
376 /* should never hit here -- filtered in usb.c */
377 US_DEBUGP ("freecom unimplemented direction: %d\n",
378 us->srb->sc_data_direction);
379 // Return fail, SCSI seems to handle this better.
380 return USB_STOR_TRANSPORT_FAILED;
381 break;
382 }
383
384 return USB_STOR_TRANSPORT_GOOD;
385}
386
387int
388freecom_init (struct us_data *us)
389{
390 int result;
391 char *buffer = us->iobuf;
392
393 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
394 * all our packets. No need to allocate any extra buffer space.
395 */
396
397 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
398 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
399 buffer[32] = '\0';
400 US_DEBUGP("String returned from FC init is: %s\n", buffer);
401
402 /* Special thanks to the people at Freecom for providing me with
403 * this "magic sequence", which they use in their Windows and MacOS
404 * drivers to make sure that all the attached perhiperals are
405 * properly reset.
406 */
407
408 /* send reset */
409 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
410 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
411 US_DEBUGP("result from activate reset is %d\n", result);
412
413 /* wait 250ms */
414 mdelay(250);
415
416 /* clear reset */
417 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
418 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
419 US_DEBUGP("result from clear reset is %d\n", result);
420
421 /* wait 3 seconds */
422 mdelay(3 * 1000);
423
424 return USB_STOR_TRANSPORT_GOOD;
425}
426
427int usb_stor_freecom_reset(struct us_data *us)
428{
429 printk (KERN_CRIT "freecom reset called\n");
430
431 /* We don't really have this feature. */
432 return FAILED;
433}
434
435#ifdef CONFIG_USB_STORAGE_DEBUG
436static void pdump (void *ibuffer, int length)
437{
438 static char line[80];
439 int offset = 0;
440 unsigned char *buffer = (unsigned char *) ibuffer;
441 int i, j;
442 int from, base;
443
444 offset = 0;
445 for (i = 0; i < length; i++) {
446 if ((i & 15) == 0) {
447 if (i > 0) {
448 offset += sprintf (line+offset, " - ");
449 for (j = i - 16; j < i; j++) {
450 if (buffer[j] >= 32 && buffer[j] <= 126)
451 line[offset++] = buffer[j];
452 else
453 line[offset++] = '.';
454 }
455 line[offset] = 0;
456 US_DEBUGP("%s\n", line);
457 offset = 0;
458 }
459 offset += sprintf (line+offset, "%08x:", i);
460 }
461 else if ((i & 7) == 0) {
462 offset += sprintf (line+offset, " -");
463 }
464 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
465 }
466
467 /* Add the last "chunk" of data. */
468 from = (length - 1) % 16;
469 base = ((length - 1) / 16) * 16;
470
471 for (i = from + 1; i < 16; i++)
472 offset += sprintf (line+offset, " ");
473 if (from < 8)
474 offset += sprintf (line+offset, " ");
475 offset += sprintf (line+offset, " - ");
476
477 for (i = 0; i <= from; i++) {
478 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
479 line[offset++] = buffer[base+i];
480 else
481 line[offset++] = '.';
482 }
483 line[offset] = 0;
484 US_DEBUGP("%s\n", line);
485 offset = 0;
486}
487#endif
488
diff --git a/drivers/usb/storage/freecom.h b/drivers/usb/storage/freecom.h
new file mode 100644
index 000000000000..1b012d62d0a8
--- /dev/null
+++ b/drivers/usb/storage/freecom.h
@@ -0,0 +1,36 @@
1/* Driver for Freecom USB/IDE adaptor
2 *
3 * $Id: freecom.h,v 1.4 2000/08/29 14:49:15 dlbrown Exp $
4 *
5 * Freecom v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 David Brown <usb-storage@davidb.org>
11 *
12 * See freecom.c for more explanation
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#ifndef _FREECOM_USB_H
30#define _FREECOM_USB_H
31
32extern int freecom_transport(struct scsi_cmnd *srb, struct us_data *us);
33extern int usb_stor_freecom_reset(struct us_data *us);
34extern int freecom_init (struct us_data *us);
35
36#endif
diff --git a/drivers/usb/storage/initializers.c b/drivers/usb/storage/initializers.c
new file mode 100644
index 000000000000..5b06f9240d05
--- /dev/null
+++ b/drivers/usb/storage/initializers.c
@@ -0,0 +1,93 @@
1/* Special Initializers for certain USB Mass Storage devices
2 *
3 * $Id: initializers.c,v 1.2 2000/09/06 22:35:57 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * This driver is based on the 'USB Mass Storage Class' document. This
9 * describes in detail the protocol used to communicate with such
10 * devices. Clearly, the designers had SCSI and ATAPI commands in
11 * mind when they created this document. The commands are all very
12 * similar to commands in the SCSI-II and ATAPI specifications.
13 *
14 * It is important to note that in a number of cases this class
15 * exhibits class-specific exemptions from the USB specification.
16 * Notably the usage of NAK, STALL and ACK differs from the norm, in
17 * that they are used to communicate wait, failed and OK on commands.
18 *
19 * Also, for certain devices, the interrupt endpoint is used to convey
20 * status of a command.
21 *
22 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
23 * information about this driver.
24 *
25 * This program is free software; you can redistribute it and/or modify it
26 * under the terms of the GNU General Public License as published by the
27 * Free Software Foundation; either version 2, or (at your option) any
28 * later version.
29 *
30 * This program is distributed in the hope that it will be useful, but
31 * WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40#include <linux/sched.h>
41#include <linux/errno.h>
42
43#include "usb.h"
44#include "initializers.h"
45#include "debug.h"
46#include "transport.h"
47
48/* This places the Shuttle/SCM USB<->SCSI bridge devices in multi-target
49 * mode */
50int usb_stor_euscsi_init(struct us_data *us)
51{
52 int result;
53
54 US_DEBUGP("Attempting to init eUSCSI bridge...\n");
55 us->iobuf[0] = 0x1;
56 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
57 0x0C, USB_RECIP_INTERFACE | USB_TYPE_VENDOR,
58 0x01, 0x0, us->iobuf, 0x1, 5*HZ);
59 US_DEBUGP("-- result is %d\n", result);
60
61 return 0;
62}
63
64/* This function is required to activate all four slots on the UCR-61S2B
65 * flash reader */
66int usb_stor_ucr61s2b_init(struct us_data *us)
67{
68 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap*) us->iobuf;
69 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap*) us->iobuf;
70 int res, partial;
71 static char init_string[] = "\xec\x0a\x06\x00$PCCHIPS";
72
73 US_DEBUGP("Sending UCR-61S2B initialization packet...\n");
74
75 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
76 bcb->Tag = 0;
77 bcb->DataTransferLength = cpu_to_le32(0);
78 bcb->Flags = bcb->Lun = 0;
79 bcb->Length = sizeof(init_string) - 1;
80 memset(bcb->CDB, 0, sizeof(bcb->CDB));
81 memcpy(bcb->CDB, init_string, sizeof(init_string) - 1);
82
83 res = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
84 US_BULK_CB_WRAP_LEN, &partial);
85 if(res)
86 return res;
87
88 US_DEBUGP("Getting status packet...\n");
89 res = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
90 US_BULK_CS_WRAP_LEN, &partial);
91
92 return (res ? -1 : 0);
93}
diff --git a/drivers/usb/storage/initializers.h b/drivers/usb/storage/initializers.h
new file mode 100644
index 000000000000..7372386f33d5
--- /dev/null
+++ b/drivers/usb/storage/initializers.h
@@ -0,0 +1,54 @@
1/* Header file for Special Initializers for certain USB Mass Storage devices
2 *
3 * $Id: initializers.h,v 1.1 2000/08/29 23:07:02 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * This driver is based on the 'USB Mass Storage Class' document. This
9 * describes in detail the protocol used to communicate with such
10 * devices. Clearly, the designers had SCSI and ATAPI commands in
11 * mind when they created this document. The commands are all very
12 * similar to commands in the SCSI-II and ATAPI specifications.
13 *
14 * It is important to note that in a number of cases this class
15 * exhibits class-specific exemptions from the USB specification.
16 * Notably the usage of NAK, STALL and ACK differs from the norm, in
17 * that they are used to communicate wait, failed and OK on commands.
18 *
19 * Also, for certain devices, the interrupt endpoint is used to convey
20 * status of a command.
21 *
22 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
23 * information about this driver.
24 *
25 * This program is free software; you can redistribute it and/or modify it
26 * under the terms of the GNU General Public License as published by the
27 * Free Software Foundation; either version 2, or (at your option) any
28 * later version.
29 *
30 * This program is distributed in the hope that it will be useful, but
31 * WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40#include <linux/config.h>
41#include "usb.h"
42#include "transport.h"
43
44/* This places the Shuttle/SCM USB<->SCSI bridge devices in multi-target
45 * mode */
46int usb_stor_euscsi_init(struct us_data *us);
47
48#ifdef CONFIG_USB_STORAGE_SDDR09
49int sddr09_init(struct us_data *us);
50#endif
51
52/* This function is required to activate all four slots on the UCR-61S2B
53 * flash reader */
54int usb_stor_ucr61s2b_init(struct us_data *us);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
new file mode 100644
index 000000000000..ecb328aa9ea1
--- /dev/null
+++ b/drivers/usb/storage/isd200.c
@@ -0,0 +1,1442 @@
1/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
2 *
3 * $Id: isd200.c,v 1.16 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance:
6 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
7 *
8 * Developed with the assistance of:
9 * (C) 2002 Alan Stern <stern@rowland.org>
10 *
11 * Initial work:
12 * (C) 2000 In-System Design, Inc. (support@in-system.com)
13 *
14 * The ISD200 ASIC does not natively support ATA devices. The chip
15 * does implement an interface, the ATA Command Block (ATACB) which provides
16 * a means of passing ATA commands and ATA register accesses to a device.
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2, or (at your option) any
21 * later version.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 *
32 * History:
33 *
34 * 2002-10-19: Removed the specialized transfer routines.
35 * (Alan Stern <stern@rowland.harvard.edu>)
36 * 2001-02-24: Removed lots of duplicate code and simplified the structure.
37 * (bjorn@haxx.se)
38 * 2002-01-16: Fixed endianness bug so it works on the ppc arch.
39 * (Luc Saillard <luc@saillard.org>)
40 * 2002-01-17: All bitfields removed.
41 * (bjorn@haxx.se)
42 */
43
44
45/* Include files */
46
47#include <linux/jiffies.h>
48#include <linux/errno.h>
49#include <linux/slab.h>
50#include <linux/hdreg.h>
51#include <linux/ide.h>
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56
57#include "usb.h"
58#include "transport.h"
59#include "protocol.h"
60#include "debug.h"
61#include "scsiglue.h"
62#include "isd200.h"
63
64
65/* Timeout defines (in Seconds) */
66
67#define ISD200_ENUM_BSY_TIMEOUT 35
68#define ISD200_ENUM_DETECT_TIMEOUT 30
69#define ISD200_DEFAULT_TIMEOUT 30
70
71/* device flags */
72#define DF_ATA_DEVICE 0x0001
73#define DF_MEDIA_STATUS_ENABLED 0x0002
74#define DF_REMOVABLE_MEDIA 0x0004
75
76/* capability bit definitions */
77#define CAPABILITY_DMA 0x01
78#define CAPABILITY_LBA 0x02
79
80/* command_setX bit definitions */
81#define COMMANDSET_REMOVABLE 0x02
82#define COMMANDSET_MEDIA_STATUS 0x10
83
84/* ATA Vendor Specific defines */
85#define ATA_ADDRESS_DEVHEAD_STD 0xa0
86#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
87#define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
88
89/* Action Select bits */
90#define ACTION_SELECT_0 0x01
91#define ACTION_SELECT_1 0x02
92#define ACTION_SELECT_2 0x04
93#define ACTION_SELECT_3 0x08
94#define ACTION_SELECT_4 0x10
95#define ACTION_SELECT_5 0x20
96#define ACTION_SELECT_6 0x40
97#define ACTION_SELECT_7 0x80
98
99/* Register Select bits */
100#define REG_ALTERNATE_STATUS 0x01
101#define REG_DEVICE_CONTROL 0x01
102#define REG_ERROR 0x02
103#define REG_FEATURES 0x02
104#define REG_SECTOR_COUNT 0x04
105#define REG_SECTOR_NUMBER 0x08
106#define REG_CYLINDER_LOW 0x10
107#define REG_CYLINDER_HIGH 0x20
108#define REG_DEVICE_HEAD 0x40
109#define REG_STATUS 0x80
110#define REG_COMMAND 0x80
111
112/* ATA error definitions not in <linux/hdreg.h> */
113#define ATA_ERROR_MEDIA_CHANGE 0x20
114
115/* ATA command definitions not in <linux/hdreg.h> */
116#define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
117#define ATA_COMMAND_MEDIA_EJECT 0xED
118
119/* ATA drive control definitions */
120#define ATA_DC_DISABLE_INTERRUPTS 0x02
121#define ATA_DC_RESET_CONTROLLER 0x04
122#define ATA_DC_REENABLE_CONTROLLER 0x00
123
124/*
125 * General purpose return codes
126 */
127
128#define ISD200_ERROR -1
129#define ISD200_GOOD 0
130
131/*
132 * Transport return codes
133 */
134
135#define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
136#define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
137#define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
138
139/* driver action codes */
140#define ACTION_READ_STATUS 0
141#define ACTION_RESET 1
142#define ACTION_REENABLE 2
143#define ACTION_SOFT_RESET 3
144#define ACTION_ENUM 4
145#define ACTION_IDENTIFY 5
146
147
148/*
149 * ata_cdb struct
150 */
151
152
153union ata_cdb {
154 struct {
155 unsigned char SignatureByte0;
156 unsigned char SignatureByte1;
157 unsigned char ActionSelect;
158 unsigned char RegisterSelect;
159 unsigned char TransferBlockSize;
160 unsigned char WriteData3F6;
161 unsigned char WriteData1F1;
162 unsigned char WriteData1F2;
163 unsigned char WriteData1F3;
164 unsigned char WriteData1F4;
165 unsigned char WriteData1F5;
166 unsigned char WriteData1F6;
167 unsigned char WriteData1F7;
168 unsigned char Reserved[3];
169 } generic;
170
171 struct {
172 unsigned char SignatureByte0;
173 unsigned char SignatureByte1;
174 unsigned char ActionSelect;
175 unsigned char RegisterSelect;
176 unsigned char TransferBlockSize;
177 unsigned char AlternateStatusByte;
178 unsigned char ErrorByte;
179 unsigned char SectorCountByte;
180 unsigned char SectorNumberByte;
181 unsigned char CylinderLowByte;
182 unsigned char CylinderHighByte;
183 unsigned char DeviceHeadByte;
184 unsigned char StatusByte;
185 unsigned char Reserved[3];
186 } read;
187
188 struct {
189 unsigned char SignatureByte0;
190 unsigned char SignatureByte1;
191 unsigned char ActionSelect;
192 unsigned char RegisterSelect;
193 unsigned char TransferBlockSize;
194 unsigned char DeviceControlByte;
195 unsigned char FeaturesByte;
196 unsigned char SectorCountByte;
197 unsigned char SectorNumberByte;
198 unsigned char CylinderLowByte;
199 unsigned char CylinderHighByte;
200 unsigned char DeviceHeadByte;
201 unsigned char CommandByte;
202 unsigned char Reserved[3];
203 } write;
204};
205
206
207/*
208 * Inquiry data structure. This is the data returned from the target
209 * after it receives an inquiry.
210 *
211 * This structure may be extended by the number of bytes specified
212 * in the field AdditionalLength. The defined size constant only
213 * includes fields through ProductRevisionLevel.
214 */
215
216/*
217 * DeviceType field
218 */
219#define DIRECT_ACCESS_DEVICE 0x00 /* disks */
220#define DEVICE_REMOVABLE 0x80
221
222struct inquiry_data {
223 unsigned char DeviceType;
224 unsigned char DeviceTypeModifier;
225 unsigned char Versions;
226 unsigned char Format;
227 unsigned char AdditionalLength;
228 unsigned char Reserved[2];
229 unsigned char Capability;
230 unsigned char VendorId[8];
231 unsigned char ProductId[16];
232 unsigned char ProductRevisionLevel[4];
233 unsigned char VendorSpecific[20];
234 unsigned char Reserved3[40];
235} __attribute__ ((packed));
236
237/*
238 * INQUIRY data buffer size
239 */
240
241#define INQUIRYDATABUFFERSIZE 36
242
243
244/*
245 * ISD200 CONFIG data struct
246 */
247
248#define ATACFG_TIMING 0x0f
249#define ATACFG_ATAPI_RESET 0x10
250#define ATACFG_MASTER 0x20
251#define ATACFG_BLOCKSIZE 0xa0
252
253#define ATACFGE_LAST_LUN 0x07
254#define ATACFGE_DESC_OVERRIDE 0x08
255#define ATACFGE_STATE_SUSPEND 0x10
256#define ATACFGE_SKIP_BOOT 0x20
257#define ATACFGE_CONF_DESC2 0x40
258#define ATACFGE_INIT_STATUS 0x80
259
260#define CFG_CAPABILITY_SRST 0x01
261
262struct isd200_config {
263 unsigned char EventNotification;
264 unsigned char ExternalClock;
265 unsigned char ATAInitTimeout;
266 unsigned char ATAConfig;
267 unsigned char ATAMajorCommand;
268 unsigned char ATAMinorCommand;
269 unsigned char ATAExtraConfig;
270 unsigned char Capability;
271}__attribute__ ((packed));
272
273
274/*
275 * ISD200 driver information struct
276 */
277
278struct isd200_info {
279 struct inquiry_data InquiryData;
280 struct hd_driveid *id;
281 struct isd200_config ConfigData;
282 unsigned char *RegsBuf;
283 unsigned char ATARegs[8];
284 unsigned char DeviceHead;
285 unsigned char DeviceFlags;
286
287 /* maximum number of LUNs supported */
288 unsigned char MaxLUNs;
289 struct scsi_cmnd srb;
290};
291
292
293/*
294 * Read Capacity Data - returned in Big Endian format
295 */
296
297struct read_capacity_data {
298 __be32 LogicalBlockAddress;
299 __be32 BytesPerBlock;
300};
301
302/*
303 * Read Block Limits Data - returned in Big Endian format
304 * This structure returns the maximum and minimum block
305 * size for a TAPE device.
306 */
307
308struct read_block_limits {
309 unsigned char Reserved;
310 unsigned char BlockMaximumSize[3];
311 unsigned char BlockMinimumSize[2];
312};
313
314
315/*
316 * Sense Data Format
317 */
318
319#define SENSE_ERRCODE 0x7f
320#define SENSE_ERRCODE_VALID 0x80
321#define SENSE_FLAG_SENSE_KEY 0x0f
322#define SENSE_FLAG_BAD_LENGTH 0x20
323#define SENSE_FLAG_END_OF_MEDIA 0x40
324#define SENSE_FLAG_FILE_MARK 0x80
325struct sense_data {
326 unsigned char ErrorCode;
327 unsigned char SegmentNumber;
328 unsigned char Flags;
329 unsigned char Information[4];
330 unsigned char AdditionalSenseLength;
331 unsigned char CommandSpecificInformation[4];
332 unsigned char AdditionalSenseCode;
333 unsigned char AdditionalSenseCodeQualifier;
334 unsigned char FieldReplaceableUnitCode;
335 unsigned char SenseKeySpecific[3];
336} __attribute__ ((packed));
337
338/*
339 * Default request sense buffer size
340 */
341
342#define SENSE_BUFFER_SIZE 18
343
344/***********************************************************************
345 * Helper routines
346 ***********************************************************************/
347
348/**************************************************************************
349 * isd200_build_sense
350 *
351 * Builds an artificial sense buffer to report the results of a
352 * failed command.
353 *
354 * RETURNS:
355 * void
356 */
357static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
358{
359 struct isd200_info *info = (struct isd200_info *)us->extra;
360 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
361 unsigned char error = info->ATARegs[IDE_ERROR_OFFSET];
362
363 if(error & ATA_ERROR_MEDIA_CHANGE) {
364 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
365 buf->AdditionalSenseLength = 0xb;
366 buf->Flags = UNIT_ATTENTION;
367 buf->AdditionalSenseCode = 0;
368 buf->AdditionalSenseCodeQualifier = 0;
369 } else if(error & MCR_ERR) {
370 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
371 buf->AdditionalSenseLength = 0xb;
372 buf->Flags = UNIT_ATTENTION;
373 buf->AdditionalSenseCode = 0;
374 buf->AdditionalSenseCodeQualifier = 0;
375 } else if(error & TRK0_ERR) {
376 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
377 buf->AdditionalSenseLength = 0xb;
378 buf->Flags = NOT_READY;
379 buf->AdditionalSenseCode = 0;
380 buf->AdditionalSenseCodeQualifier = 0;
381 } else if(error & ECC_ERR) {
382 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
383 buf->AdditionalSenseLength = 0xb;
384 buf->Flags = DATA_PROTECT;
385 buf->AdditionalSenseCode = 0;
386 buf->AdditionalSenseCodeQualifier = 0;
387 } else {
388 buf->ErrorCode = 0;
389 buf->AdditionalSenseLength = 0;
390 buf->Flags = 0;
391 buf->AdditionalSenseCode = 0;
392 buf->AdditionalSenseCodeQualifier = 0;
393 }
394}
395
396
397/***********************************************************************
398 * Transport routines
399 ***********************************************************************/
400
401
402/**************************************************************************
403 * isd200_action
404 *
405 * Routine for sending commands to the isd200
406 *
407 * RETURNS:
408 * ISD status code
409 */
410static int isd200_action( struct us_data *us, int action,
411 void* pointer, int value )
412{
413 union ata_cdb ata;
414 struct scsi_device srb_dev;
415 struct isd200_info *info = (struct isd200_info *)us->extra;
416 struct scsi_cmnd *srb = &info->srb;
417 int status;
418
419 memset(&ata, 0, sizeof(ata));
420 memset(&srb_dev, 0, sizeof(srb_dev));
421 srb->device = &srb_dev;
422 ++srb->serial_number;
423
424 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
425 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
426 ata.generic.TransferBlockSize = 1;
427
428 switch ( action ) {
429 case ACTION_READ_STATUS:
430 US_DEBUGP(" isd200_action(READ_STATUS)\n");
431 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
432 ata.generic.RegisterSelect =
433 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
434 REG_STATUS | REG_ERROR;
435 srb->sc_data_direction = DMA_FROM_DEVICE;
436 srb->request_buffer = pointer;
437 srb->request_bufflen = value;
438 break;
439
440 case ACTION_ENUM:
441 US_DEBUGP(" isd200_action(ENUM,0x%02x)\n",value);
442 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
443 ACTION_SELECT_3|ACTION_SELECT_4|
444 ACTION_SELECT_5;
445 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
446 ata.write.DeviceHeadByte = value;
447 srb->sc_data_direction = DMA_NONE;
448 break;
449
450 case ACTION_RESET:
451 US_DEBUGP(" isd200_action(RESET)\n");
452 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
453 ACTION_SELECT_3|ACTION_SELECT_4;
454 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
455 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
456 srb->sc_data_direction = DMA_NONE;
457 break;
458
459 case ACTION_REENABLE:
460 US_DEBUGP(" isd200_action(REENABLE)\n");
461 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
462 ACTION_SELECT_3|ACTION_SELECT_4;
463 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
464 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
465 srb->sc_data_direction = DMA_NONE;
466 break;
467
468 case ACTION_SOFT_RESET:
469 US_DEBUGP(" isd200_action(SOFT_RESET)\n");
470 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
471 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
472 ata.write.DeviceHeadByte = info->DeviceHead;
473 ata.write.CommandByte = WIN_SRST;
474 srb->sc_data_direction = DMA_NONE;
475 break;
476
477 case ACTION_IDENTIFY:
478 US_DEBUGP(" isd200_action(IDENTIFY)\n");
479 ata.generic.RegisterSelect = REG_COMMAND;
480 ata.write.CommandByte = WIN_IDENTIFY;
481 srb->sc_data_direction = DMA_FROM_DEVICE;
482 srb->request_buffer = (void *) info->id;
483 srb->request_bufflen = sizeof(struct hd_driveid);
484 break;
485
486 default:
487 US_DEBUGP("Error: Undefined action %d\n",action);
488 break;
489 }
490
491 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
492 srb->cmd_len = sizeof(ata.generic);
493 status = usb_stor_Bulk_transport(srb, us);
494 if (status == USB_STOR_TRANSPORT_GOOD)
495 status = ISD200_GOOD;
496 else {
497 US_DEBUGP(" isd200_action(0x%02x) error: %d\n",action,status);
498 status = ISD200_ERROR;
499 /* need to reset device here */
500 }
501
502 return status;
503}
504
505/**************************************************************************
506 * isd200_read_regs
507 *
508 * Read ATA Registers
509 *
510 * RETURNS:
511 * ISD status code
512 */
513static int isd200_read_regs( struct us_data *us )
514{
515 struct isd200_info *info = (struct isd200_info *)us->extra;
516 int retStatus = ISD200_GOOD;
517 int transferStatus;
518
519 US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
520
521 transferStatus = isd200_action( us, ACTION_READ_STATUS,
522 info->RegsBuf, sizeof(info->ATARegs) );
523 if (transferStatus != ISD200_TRANSPORT_GOOD) {
524 US_DEBUGP(" Error reading ATA registers\n");
525 retStatus = ISD200_ERROR;
526 } else {
527 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
528 US_DEBUGP(" Got ATA Register[IDE_ERROR_OFFSET] = 0x%x\n",
529 info->ATARegs[IDE_ERROR_OFFSET]);
530 }
531
532 return retStatus;
533}
534
535
536/**************************************************************************
537 * Invoke the transport and basic error-handling/recovery methods
538 *
539 * This is used by the protocol layers to actually send the message to
540 * the device and receive the response.
541 */
542static void isd200_invoke_transport( struct us_data *us,
543 struct scsi_cmnd *srb,
544 union ata_cdb *ataCdb )
545{
546 int need_auto_sense = 0;
547 int transferStatus;
548 int result;
549
550 /* send the command to the transport layer */
551 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
552 srb->cmd_len = sizeof(ataCdb->generic);
553 transferStatus = usb_stor_Bulk_transport(srb, us);
554
555 /* if the command gets aborted by the higher layers, we need to
556 * short-circuit all other processing
557 */
558 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
559 US_DEBUGP("-- command was aborted\n");
560 goto Handle_Abort;
561 }
562
563 switch (transferStatus) {
564
565 case USB_STOR_TRANSPORT_GOOD:
566 /* Indicate a good result */
567 srb->result = SAM_STAT_GOOD;
568 break;
569
570 case USB_STOR_TRANSPORT_NO_SENSE:
571 US_DEBUGP("-- transport indicates protocol failure\n");
572 srb->result = SAM_STAT_CHECK_CONDITION;
573 return;
574
575 case USB_STOR_TRANSPORT_FAILED:
576 US_DEBUGP("-- transport indicates command failure\n");
577 need_auto_sense = 1;
578 break;
579
580 case USB_STOR_TRANSPORT_ERROR:
581 US_DEBUGP("-- transport indicates transport error\n");
582 srb->result = DID_ERROR << 16;
583 /* Need reset here */
584 return;
585
586 default:
587 US_DEBUGP("-- transport indicates unknown error\n");
588 srb->result = DID_ERROR << 16;
589 /* Need reset here */
590 return;
591 }
592
593 if ((srb->resid > 0) &&
594 !((srb->cmnd[0] == REQUEST_SENSE) ||
595 (srb->cmnd[0] == INQUIRY) ||
596 (srb->cmnd[0] == MODE_SENSE) ||
597 (srb->cmnd[0] == LOG_SENSE) ||
598 (srb->cmnd[0] == MODE_SENSE_10))) {
599 US_DEBUGP("-- unexpectedly short transfer\n");
600 need_auto_sense = 1;
601 }
602
603 if (need_auto_sense) {
604 result = isd200_read_regs(us);
605 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
606 US_DEBUGP("-- auto-sense aborted\n");
607 goto Handle_Abort;
608 }
609 if (result == ISD200_GOOD) {
610 isd200_build_sense(us, srb);
611 srb->result = SAM_STAT_CHECK_CONDITION;
612
613 /* If things are really okay, then let's show that */
614 if ((srb->sense_buffer[2] & 0xf) == 0x0)
615 srb->result = SAM_STAT_GOOD;
616 } else {
617 srb->result = DID_ERROR << 16;
618 /* Need reset here */
619 }
620 }
621
622 /* Regardless of auto-sense, if we _know_ we have an error
623 * condition, show that in the result code
624 */
625 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
626 srb->result = SAM_STAT_CHECK_CONDITION;
627 return;
628
629 /* abort processing: the bulk-only transport requires a reset
630 * following an abort */
631 Handle_Abort:
632 srb->result = DID_ABORT << 16;
633
634 /* permit the reset transfer to take place */
635 clear_bit(US_FLIDX_ABORTING, &us->flags);
636 /* Need reset here */
637}
638
639#ifdef CONFIG_USB_STORAGE_DEBUG
640static void isd200_log_config( struct isd200_info* info )
641{
642 US_DEBUGP(" Event Notification: 0x%x\n",
643 info->ConfigData.EventNotification);
644 US_DEBUGP(" External Clock: 0x%x\n",
645 info->ConfigData.ExternalClock);
646 US_DEBUGP(" ATA Init Timeout: 0x%x\n",
647 info->ConfigData.ATAInitTimeout);
648 US_DEBUGP(" ATAPI Command Block Size: 0x%x\n",
649 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
650 US_DEBUGP(" Master/Slave Selection: 0x%x\n",
651 info->ConfigData.ATAConfig & ATACFG_MASTER);
652 US_DEBUGP(" ATAPI Reset: 0x%x\n",
653 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
654 US_DEBUGP(" ATA Timing: 0x%x\n",
655 info->ConfigData.ATAConfig & ATACFG_TIMING);
656 US_DEBUGP(" ATA Major Command: 0x%x\n",
657 info->ConfigData.ATAMajorCommand);
658 US_DEBUGP(" ATA Minor Command: 0x%x\n",
659 info->ConfigData.ATAMinorCommand);
660 US_DEBUGP(" Init Status: 0x%x\n",
661 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
662 US_DEBUGP(" Config Descriptor 2: 0x%x\n",
663 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
664 US_DEBUGP(" Skip Device Boot: 0x%x\n",
665 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
666 US_DEBUGP(" ATA 3 State Supsend: 0x%x\n",
667 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
668 US_DEBUGP(" Descriptor Override: 0x%x\n",
669 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
670 US_DEBUGP(" Last LUN Identifier: 0x%x\n",
671 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
672 US_DEBUGP(" SRST Enable: 0x%x\n",
673 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
674}
675#endif
676
677/**************************************************************************
678 * isd200_write_config
679 *
680 * Write the ISD200 Configuration data
681 *
682 * RETURNS:
683 * ISD status code
684 */
685static int isd200_write_config( struct us_data *us )
686{
687 struct isd200_info *info = (struct isd200_info *)us->extra;
688 int retStatus = ISD200_GOOD;
689 int result;
690
691#ifdef CONFIG_USB_STORAGE_DEBUG
692 US_DEBUGP("Entering isd200_write_config\n");
693 US_DEBUGP(" Writing the following ISD200 Config Data:\n");
694 isd200_log_config(info);
695#endif
696
697 /* let's send the command via the control pipe */
698 result = usb_stor_ctrl_transfer(
699 us,
700 us->send_ctrl_pipe,
701 0x01,
702 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
703 0x0000,
704 0x0002,
705 (void *) &info->ConfigData,
706 sizeof(info->ConfigData));
707
708 if (result >= 0) {
709 US_DEBUGP(" ISD200 Config Data was written successfully\n");
710 } else {
711 US_DEBUGP(" Request to write ISD200 Config Data failed!\n");
712 retStatus = ISD200_ERROR;
713 }
714
715 US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
716 return retStatus;
717}
718
719
720/**************************************************************************
721 * isd200_read_config
722 *
723 * Reads the ISD200 Configuration data
724 *
725 * RETURNS:
726 * ISD status code
727 */
728static int isd200_read_config( struct us_data *us )
729{
730 struct isd200_info *info = (struct isd200_info *)us->extra;
731 int retStatus = ISD200_GOOD;
732 int result;
733
734 US_DEBUGP("Entering isd200_read_config\n");
735
736 /* read the configuration information from ISD200. Use this to */
737 /* determine what the special ATA CDB bytes are. */
738
739 result = usb_stor_ctrl_transfer(
740 us,
741 us->recv_ctrl_pipe,
742 0x02,
743 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
744 0x0000,
745 0x0002,
746 (void *) &info->ConfigData,
747 sizeof(info->ConfigData));
748
749
750 if (result >= 0) {
751 US_DEBUGP(" Retrieved the following ISD200 Config Data:\n");
752#ifdef CONFIG_USB_STORAGE_DEBUG
753 isd200_log_config(info);
754#endif
755 } else {
756 US_DEBUGP(" Request to get ISD200 Config Data failed!\n");
757 retStatus = ISD200_ERROR;
758 }
759
760 US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
761 return retStatus;
762}
763
764
765/**************************************************************************
766 * isd200_atapi_soft_reset
767 *
768 * Perform an Atapi Soft Reset on the device
769 *
770 * RETURNS:
771 * NT status code
772 */
773static int isd200_atapi_soft_reset( struct us_data *us )
774{
775 int retStatus = ISD200_GOOD;
776 int transferStatus;
777
778 US_DEBUGP("Entering isd200_atapi_soft_reset\n");
779
780 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
781 if (transferStatus != ISD200_TRANSPORT_GOOD) {
782 US_DEBUGP(" Error issuing Atapi Soft Reset\n");
783 retStatus = ISD200_ERROR;
784 }
785
786 US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
787 return retStatus;
788}
789
790
791/**************************************************************************
792 * isd200_srst
793 *
794 * Perform an SRST on the device
795 *
796 * RETURNS:
797 * ISD status code
798 */
799static int isd200_srst( struct us_data *us )
800{
801 int retStatus = ISD200_GOOD;
802 int transferStatus;
803
804 US_DEBUGP("Entering isd200_SRST\n");
805
806 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
807
808 /* check to see if this request failed */
809 if (transferStatus != ISD200_TRANSPORT_GOOD) {
810 US_DEBUGP(" Error issuing SRST\n");
811 retStatus = ISD200_ERROR;
812 } else {
813 /* delay 10ms to give the drive a chance to see it */
814 msleep(10);
815
816 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
817 if (transferStatus != ISD200_TRANSPORT_GOOD) {
818 US_DEBUGP(" Error taking drive out of reset\n");
819 retStatus = ISD200_ERROR;
820 } else {
821 /* delay 50ms to give the drive a chance to recover after SRST */
822 msleep(50);
823 }
824 }
825
826 US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
827 return retStatus;
828}
829
830
831/**************************************************************************
832 * isd200_try_enum
833 *
834 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
835 * and tries to analyze the status registers
836 *
837 * RETURNS:
838 * ISD status code
839 */
840static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
841 int detect )
842{
843 int status = ISD200_GOOD;
844 unsigned long endTime;
845 struct isd200_info *info = (struct isd200_info *)us->extra;
846 unsigned char *regs = info->RegsBuf;
847 int recheckAsMaster = 0;
848
849 if ( detect )
850 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
851 else
852 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
853
854 /* loop until we detect !BSY or timeout */
855 while(1) {
856#ifdef CONFIG_USB_STORAGE_DEBUG
857 char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
858 "Master" : "Slave";
859#endif
860
861 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
862 if ( status != ISD200_GOOD )
863 break;
864
865 status = isd200_action( us, ACTION_READ_STATUS,
866 regs, 8 );
867 if ( status != ISD200_GOOD )
868 break;
869
870 if (!detect) {
871 if (regs[IDE_STATUS_OFFSET] & BUSY_STAT ) {
872 US_DEBUGP(" %s status is still BSY, try again...\n",mstr);
873 } else {
874 US_DEBUGP(" %s status !BSY, continue with next operation\n",mstr);
875 break;
876 }
877 }
878 /* check for BUSY_STAT and */
879 /* WRERR_STAT (workaround ATA Zip drive) and */
880 /* ERR_STAT (workaround for Archos CD-ROM) */
881 else if (regs[IDE_STATUS_OFFSET] &
882 (BUSY_STAT | WRERR_STAT | ERR_STAT )) {
883 US_DEBUGP(" Status indicates it is not ready, try again...\n");
884 }
885 /* check for DRDY, ATA devices set DRDY after SRST */
886 else if (regs[IDE_STATUS_OFFSET] & READY_STAT) {
887 US_DEBUGP(" Identified ATA device\n");
888 info->DeviceFlags |= DF_ATA_DEVICE;
889 info->DeviceHead = master_slave;
890 break;
891 }
892 /* check Cylinder High/Low to
893 determine if it is an ATAPI device
894 */
895 else if ((regs[IDE_HCYL_OFFSET] == 0xEB) &&
896 (regs[IDE_LCYL_OFFSET] == 0x14)) {
897 /* It seems that the RICOH
898 MP6200A CD/RW drive will
899 report itself okay as a
900 slave when it is really a
901 master. So this check again
902 as a master device just to
903 make sure it doesn't report
904 itself okay as a master also
905 */
906 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
907 !recheckAsMaster) {
908 US_DEBUGP(" Identified ATAPI device as slave. Rechecking again as master\n");
909 recheckAsMaster = 1;
910 master_slave = ATA_ADDRESS_DEVHEAD_STD;
911 } else {
912 US_DEBUGP(" Identified ATAPI device\n");
913 info->DeviceHead = master_slave;
914
915 status = isd200_atapi_soft_reset(us);
916 break;
917 }
918 } else {
919 US_DEBUGP(" Not ATA, not ATAPI. Weird.\n");
920 break;
921 }
922
923 /* check for timeout on this request */
924 if (time_after_eq(jiffies, endTime)) {
925 if (!detect)
926 US_DEBUGP(" BSY check timeout, just continue with next operation...\n");
927 else
928 US_DEBUGP(" Device detect timeout!\n");
929 break;
930 }
931 }
932
933 return status;
934}
935
936/**************************************************************************
937 * isd200_manual_enum
938 *
939 * Determines if the drive attached is an ATA or ATAPI and if it is a
940 * master or slave.
941 *
942 * RETURNS:
943 * ISD status code
944 */
945static int isd200_manual_enum(struct us_data *us)
946{
947 struct isd200_info *info = (struct isd200_info *)us->extra;
948 int retStatus = ISD200_GOOD;
949
950 US_DEBUGP("Entering isd200_manual_enum\n");
951
952 retStatus = isd200_read_config(us);
953 if (retStatus == ISD200_GOOD) {
954 int isslave;
955 /* master or slave? */
956 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
957 if (retStatus == ISD200_GOOD)
958 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
959
960 if (retStatus == ISD200_GOOD) {
961 retStatus = isd200_srst(us);
962 if (retStatus == ISD200_GOOD)
963 /* ata or atapi? */
964 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
965 }
966
967 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
968 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
969 US_DEBUGP(" Setting Master/Slave selection to %d\n", isslave);
970 info->ConfigData.ATAConfig &= 0x3f;
971 info->ConfigData.ATAConfig |= (isslave<<6);
972 retStatus = isd200_write_config(us);
973 }
974 }
975
976 US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
977 return(retStatus);
978}
979
980
981/**************************************************************************
982 * isd200_get_inquiry_data
983 *
984 * Get inquiry data
985 *
986 * RETURNS:
987 * ISD status code
988 */
989static int isd200_get_inquiry_data( struct us_data *us )
990{
991 struct isd200_info *info = (struct isd200_info *)us->extra;
992 int retStatus = ISD200_GOOD;
993 struct hd_driveid *id = info->id;
994
995 US_DEBUGP("Entering isd200_get_inquiry_data\n");
996
997 /* set default to Master */
998 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
999
1000 /* attempt to manually enumerate this device */
1001 retStatus = isd200_manual_enum(us);
1002 if (retStatus == ISD200_GOOD) {
1003 int transferStatus;
1004
1005 /* check for an ATA device */
1006 if (info->DeviceFlags & DF_ATA_DEVICE) {
1007 /* this must be an ATA device */
1008 /* perform an ATA Command Identify */
1009 transferStatus = isd200_action( us, ACTION_IDENTIFY,
1010 id,
1011 sizeof(struct hd_driveid) );
1012 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1013 /* Error issuing ATA Command Identify */
1014 US_DEBUGP(" Error issuing ATA Command Identify\n");
1015 retStatus = ISD200_ERROR;
1016 } else {
1017 /* ATA Command Identify successful */
1018 int i;
1019 __be16 *src;
1020 __u16 *dest;
1021 ide_fix_driveid(id);
1022
1023 US_DEBUGP(" Identify Data Structure:\n");
1024 US_DEBUGP(" config = 0x%x\n", id->config);
1025 US_DEBUGP(" cyls = 0x%x\n", id->cyls);
1026 US_DEBUGP(" heads = 0x%x\n", id->heads);
1027 US_DEBUGP(" track_bytes = 0x%x\n", id->track_bytes);
1028 US_DEBUGP(" sector_bytes = 0x%x\n", id->sector_bytes);
1029 US_DEBUGP(" sectors = 0x%x\n", id->sectors);
1030 US_DEBUGP(" serial_no[0] = 0x%x\n", id->serial_no[0]);
1031 US_DEBUGP(" buf_type = 0x%x\n", id->buf_type);
1032 US_DEBUGP(" buf_size = 0x%x\n", id->buf_size);
1033 US_DEBUGP(" ecc_bytes = 0x%x\n", id->ecc_bytes);
1034 US_DEBUGP(" fw_rev[0] = 0x%x\n", id->fw_rev[0]);
1035 US_DEBUGP(" model[0] = 0x%x\n", id->model[0]);
1036 US_DEBUGP(" max_multsect = 0x%x\n", id->max_multsect);
1037 US_DEBUGP(" dword_io = 0x%x\n", id->dword_io);
1038 US_DEBUGP(" capability = 0x%x\n", id->capability);
1039 US_DEBUGP(" tPIO = 0x%x\n", id->tPIO);
1040 US_DEBUGP(" tDMA = 0x%x\n", id->tDMA);
1041 US_DEBUGP(" field_valid = 0x%x\n", id->field_valid);
1042 US_DEBUGP(" cur_cyls = 0x%x\n", id->cur_cyls);
1043 US_DEBUGP(" cur_heads = 0x%x\n", id->cur_heads);
1044 US_DEBUGP(" cur_sectors = 0x%x\n", id->cur_sectors);
1045 US_DEBUGP(" cur_capacity = 0x%x\n", (id->cur_capacity1 << 16) + id->cur_capacity0 );
1046 US_DEBUGP(" multsect = 0x%x\n", id->multsect);
1047 US_DEBUGP(" lba_capacity = 0x%x\n", id->lba_capacity);
1048 US_DEBUGP(" command_set_1 = 0x%x\n", id->command_set_1);
1049 US_DEBUGP(" command_set_2 = 0x%x\n", id->command_set_2);
1050
1051 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1052
1053 /* Standard IDE interface only supports disks */
1054 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1055
1056 /* The length must be at least 36 (5 + 31) */
1057 info->InquiryData.AdditionalLength = 0x1F;
1058
1059 if (id->command_set_1 & COMMANDSET_MEDIA_STATUS) {
1060 /* set the removable bit */
1061 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1062 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1063 }
1064
1065 /* Fill in vendor identification fields */
1066 src = (__be16*)id->model;
1067 dest = (__u16*)info->InquiryData.VendorId;
1068 for (i=0;i<4;i++)
1069 dest[i] = be16_to_cpu(src[i]);
1070
1071 src = (__be16*)(id->model+8);
1072 dest = (__u16*)info->InquiryData.ProductId;
1073 for (i=0;i<8;i++)
1074 dest[i] = be16_to_cpu(src[i]);
1075
1076 src = (__be16*)id->fw_rev;
1077 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1078 for (i=0;i<2;i++)
1079 dest[i] = be16_to_cpu(src[i]);
1080
1081 /* determine if it supports Media Status Notification */
1082 if (id->command_set_2 & COMMANDSET_MEDIA_STATUS) {
1083 US_DEBUGP(" Device supports Media Status Notification\n");
1084
1085 /* Indicate that it is enabled, even though it is not
1086 * This allows the lock/unlock of the media to work
1087 * correctly.
1088 */
1089 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1090 }
1091 else
1092 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1093
1094 }
1095 } else {
1096 /*
1097 * this must be an ATAPI device
1098 * use an ATAPI protocol (Transparent SCSI)
1099 */
1100 us->protocol_name = "Transparent SCSI";
1101 us->proto_handler = usb_stor_transparent_scsi_command;
1102
1103 US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
1104
1105 /* Free driver structure */
1106 us->extra_destructor(info);
1107 us->extra = NULL;
1108 us->extra_destructor = NULL;
1109 }
1110 }
1111
1112 US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
1113
1114 return(retStatus);
1115}
1116
1117
1118/**************************************************************************
1119 * isd200_scsi_to_ata
1120 *
1121 * Translate SCSI commands to ATA commands.
1122 *
1123 * RETURNS:
1124 * 1 if the command needs to be sent to the transport layer
1125 * 0 otherwise
1126 */
1127static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1128 union ata_cdb * ataCdb)
1129{
1130 struct isd200_info *info = (struct isd200_info *)us->extra;
1131 struct hd_driveid *id = info->id;
1132 int sendToTransport = 1;
1133 unsigned char sectnum, head;
1134 unsigned short cylinder;
1135 unsigned long lba;
1136 unsigned long blockCount;
1137 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1138
1139 memset(ataCdb, 0, sizeof(union ata_cdb));
1140
1141 /* SCSI Command */
1142 switch (srb->cmnd[0]) {
1143 case INQUIRY:
1144 US_DEBUGP(" ATA OUT - INQUIRY\n");
1145
1146 /* copy InquiryData */
1147 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1148 sizeof(info->InquiryData), srb);
1149 srb->result = SAM_STAT_GOOD;
1150 sendToTransport = 0;
1151 break;
1152
1153 case MODE_SENSE:
1154 US_DEBUGP(" ATA OUT - SCSIOP_MODE_SENSE\n");
1155
1156 /* Initialize the return buffer */
1157 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1158
1159 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1160 {
1161 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1162 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1163 ataCdb->generic.TransferBlockSize = 1;
1164 ataCdb->generic.RegisterSelect = REG_COMMAND;
1165 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1166 srb->request_bufflen = 0;
1167 } else {
1168 US_DEBUGP(" Media Status not supported, just report okay\n");
1169 srb->result = SAM_STAT_GOOD;
1170 sendToTransport = 0;
1171 }
1172 break;
1173
1174 case TEST_UNIT_READY:
1175 US_DEBUGP(" ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1176
1177 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1178 {
1179 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1180 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1181 ataCdb->generic.TransferBlockSize = 1;
1182 ataCdb->generic.RegisterSelect = REG_COMMAND;
1183 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1184 srb->request_bufflen = 0;
1185 } else {
1186 US_DEBUGP(" Media Status not supported, just report okay\n");
1187 srb->result = SAM_STAT_GOOD;
1188 sendToTransport = 0;
1189 }
1190 break;
1191
1192 case READ_CAPACITY:
1193 {
1194 unsigned long capacity;
1195 struct read_capacity_data readCapacityData;
1196
1197 US_DEBUGP(" ATA OUT - SCSIOP_READ_CAPACITY\n");
1198
1199 if (id->capability & CAPABILITY_LBA ) {
1200 capacity = id->lba_capacity - 1;
1201 } else {
1202 capacity = (id->heads *
1203 id->cyls *
1204 id->sectors) - 1;
1205 }
1206 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1207 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1208
1209 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1210 sizeof(readCapacityData), srb);
1211 srb->result = SAM_STAT_GOOD;
1212 sendToTransport = 0;
1213 }
1214 break;
1215
1216 case READ_10:
1217 US_DEBUGP(" ATA OUT - SCSIOP_READ\n");
1218
1219 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1220 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1221
1222 if (id->capability & CAPABILITY_LBA) {
1223 sectnum = (unsigned char)(lba);
1224 cylinder = (unsigned short)(lba>>8);
1225 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1226 } else {
1227 sectnum = (unsigned char)((lba % id->sectors) + 1);
1228 cylinder = (unsigned short)(lba / (id->sectors *
1229 id->heads));
1230 head = (unsigned char)((lba / id->sectors) %
1231 id->heads);
1232 }
1233 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1234 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1235 ataCdb->generic.TransferBlockSize = 1;
1236 ataCdb->generic.RegisterSelect =
1237 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1238 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1239 REG_DEVICE_HEAD | REG_COMMAND;
1240 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1241 ataCdb->write.SectorNumberByte = sectnum;
1242 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1243 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1244 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1245 ataCdb->write.CommandByte = WIN_READ;
1246 break;
1247
1248 case WRITE_10:
1249 US_DEBUGP(" ATA OUT - SCSIOP_WRITE\n");
1250
1251 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1252 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1253
1254 if (id->capability & CAPABILITY_LBA) {
1255 sectnum = (unsigned char)(lba);
1256 cylinder = (unsigned short)(lba>>8);
1257 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1258 } else {
1259 sectnum = (unsigned char)((lba % id->sectors) + 1);
1260 cylinder = (unsigned short)(lba / (id->sectors * id->heads));
1261 head = (unsigned char)((lba / id->sectors) % id->heads);
1262 }
1263 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1264 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1265 ataCdb->generic.TransferBlockSize = 1;
1266 ataCdb->generic.RegisterSelect =
1267 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1268 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1269 REG_DEVICE_HEAD | REG_COMMAND;
1270 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1271 ataCdb->write.SectorNumberByte = sectnum;
1272 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1273 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1274 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1275 ataCdb->write.CommandByte = WIN_WRITE;
1276 break;
1277
1278 case ALLOW_MEDIUM_REMOVAL:
1279 US_DEBUGP(" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1280
1281 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1282 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1283
1284 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1285 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1286 ataCdb->generic.TransferBlockSize = 1;
1287 ataCdb->generic.RegisterSelect = REG_COMMAND;
1288 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1289 WIN_DOORLOCK : WIN_DOORUNLOCK;
1290 srb->request_bufflen = 0;
1291 } else {
1292 US_DEBUGP(" Not removeable media, just report okay\n");
1293 srb->result = SAM_STAT_GOOD;
1294 sendToTransport = 0;
1295 }
1296 break;
1297
1298 case START_STOP:
1299 US_DEBUGP(" ATA OUT - SCSIOP_START_STOP_UNIT\n");
1300 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1301
1302 if ((srb->cmnd[4] & 0x3) == 0x2) {
1303 US_DEBUGP(" Media Eject\n");
1304 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1305 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1306 ataCdb->generic.TransferBlockSize = 0;
1307 ataCdb->generic.RegisterSelect = REG_COMMAND;
1308 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1309 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
1310 US_DEBUGP(" Get Media Status\n");
1311 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1312 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1313 ataCdb->generic.TransferBlockSize = 1;
1314 ataCdb->generic.RegisterSelect = REG_COMMAND;
1315 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1316 srb->request_bufflen = 0;
1317 } else {
1318 US_DEBUGP(" Nothing to do, just report okay\n");
1319 srb->result = SAM_STAT_GOOD;
1320 sendToTransport = 0;
1321 }
1322 break;
1323
1324 default:
1325 US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
1326 srb->result = DID_ERROR << 16;
1327 sendToTransport = 0;
1328 break;
1329 }
1330
1331 return(sendToTransport);
1332}
1333
1334
1335/**************************************************************************
1336 * isd200_free_info
1337 *
1338 * Frees the driver structure.
1339 */
1340static void isd200_free_info_ptrs(void *info_)
1341{
1342 struct isd200_info *info = (struct isd200_info *) info_;
1343
1344 if (info) {
1345 kfree(info->id);
1346 kfree(info->RegsBuf);
1347 }
1348}
1349
1350/**************************************************************************
1351 * isd200_init_info
1352 *
1353 * Allocates (if necessary) and initializes the driver structure.
1354 *
1355 * RETURNS:
1356 * ISD status code
1357 */
1358static int isd200_init_info(struct us_data *us)
1359{
1360 int retStatus = ISD200_GOOD;
1361 struct isd200_info *info;
1362
1363 info = (struct isd200_info *)
1364 kmalloc(sizeof(struct isd200_info), GFP_KERNEL);
1365 if (!info)
1366 retStatus = ISD200_ERROR;
1367 else {
1368 memset(info, 0, sizeof(struct isd200_info));
1369 info->id = (struct hd_driveid *)
1370 kmalloc(sizeof(struct hd_driveid), GFP_KERNEL);
1371 info->RegsBuf = (unsigned char *)
1372 kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1373 if (!info->id || !info->RegsBuf) {
1374 isd200_free_info_ptrs(info);
1375 kfree(info);
1376 retStatus = ISD200_ERROR;
1377 } else
1378 memset(info->id, 0, sizeof(struct hd_driveid));
1379 }
1380
1381 if (retStatus == ISD200_GOOD) {
1382 us->extra = info;
1383 us->extra_destructor = isd200_free_info_ptrs;
1384 } else
1385 US_DEBUGP("ERROR - kmalloc failure\n");
1386
1387 return(retStatus);
1388}
1389
1390/**************************************************************************
1391 * Initialization for the ISD200
1392 */
1393
1394int isd200_Initialization(struct us_data *us)
1395{
1396 US_DEBUGP("ISD200 Initialization...\n");
1397
1398 /* Initialize ISD200 info struct */
1399
1400 if (isd200_init_info(us) == ISD200_ERROR) {
1401 US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
1402 } else {
1403 /* Get device specific data */
1404
1405 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1406 US_DEBUGP("ISD200 Initialization Failure\n");
1407 else
1408 US_DEBUGP("ISD200 Initialization complete\n");
1409 }
1410
1411 return 0;
1412}
1413
1414
1415/**************************************************************************
1416 * Protocol and Transport for the ISD200 ASIC
1417 *
1418 * This protocol and transport are for ATA devices connected to an ISD200
1419 * ASIC. An ATAPI device that is conected as a slave device will be
1420 * detected in the driver initialization function and the protocol will
1421 * be changed to an ATAPI protocol (Transparent SCSI).
1422 *
1423 */
1424
1425void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1426{
1427 int sendToTransport = 1;
1428 union ata_cdb ataCdb;
1429
1430 /* Make sure driver was initialized */
1431
1432 if (us->extra == NULL)
1433 US_DEBUGP("ERROR Driver not initialized\n");
1434
1435 /* Convert command */
1436 srb->resid = 0;
1437 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1438
1439 /* send the command to the transport layer */
1440 if (sendToTransport)
1441 isd200_invoke_transport(us, srb, &ataCdb);
1442}
diff --git a/drivers/usb/storage/isd200.h b/drivers/usb/storage/isd200.h
new file mode 100644
index 000000000000..0a35f4fa78f8
--- /dev/null
+++ b/drivers/usb/storage/isd200.h
@@ -0,0 +1,31 @@
1/* Header File for In-System Design, Inc. ISD200 ASIC
2 *
3 * First release
4 *
5 * Current development and maintenance by:
6 * (c) 2000 In-System Design, Inc. (support@in-system.com)
7 *
8 * See isd200.c for more information.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#ifndef _USB_ISD200_H
26#define _USB_ISD200_H
27
28extern void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us);
29extern int isd200_Initialization(struct us_data *us);
30
31#endif
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
new file mode 100644
index 000000000000..aff9d51c327c
--- /dev/null
+++ b/drivers/usb/storage/jumpshot.c
@@ -0,0 +1,596 @@
1/* Driver for Lexar "Jumpshot" Compact Flash reader
2 *
3 * $Id: jumpshot.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
4 *
5 * jumpshot driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
11 *
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
14 *
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
17 *
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
19 *
20 * Developed with the assistance of:
21 *
22 * (C) 2002 Alan Stern <stern@rowland.org>
23 *
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2, or (at your option) any
27 * later version.
28 *
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
37 */
38
39 /*
40 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
41 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
42 * a USB-to-ATA chip.
43 *
44 * This driver supports reading and writing. If you're truly paranoid,
45 * however, you can force the driver into a write-protected state by setting
46 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
47 * in that routine.
48 */
49
50#include <linux/sched.h>
51#include <linux/errno.h>
52#include <linux/slab.h>
53
54#include <scsi/scsi.h>
55#include <scsi/scsi_cmnd.h>
56
57#include "usb.h"
58#include "transport.h"
59#include "protocol.h"
60#include "debug.h"
61#include "jumpshot.h"
62
63
64static inline int jumpshot_bulk_read(struct us_data *us,
65 unsigned char *data,
66 unsigned int len)
67{
68 if (len == 0)
69 return USB_STOR_XFER_GOOD;
70
71 US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
72 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
73 data, len, NULL);
74}
75
76
77static inline int jumpshot_bulk_write(struct us_data *us,
78 unsigned char *data,
79 unsigned int len)
80{
81 if (len == 0)
82 return USB_STOR_XFER_GOOD;
83
84 US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
85 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
86 data, len, NULL);
87}
88
89
90static int jumpshot_get_status(struct us_data *us)
91{
92 int rc;
93
94 if (!us)
95 return USB_STOR_TRANSPORT_ERROR;
96
97 // send the setup
98 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
99 0, 0xA0, 0, 7, us->iobuf, 1);
100
101 if (rc != USB_STOR_XFER_GOOD)
102 return USB_STOR_TRANSPORT_ERROR;
103
104 if (us->iobuf[0] != 0x50) {
105 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
106 us->iobuf[0]);
107 return USB_STOR_TRANSPORT_ERROR;
108 }
109
110 return USB_STOR_TRANSPORT_GOOD;
111}
112
113static int jumpshot_read_data(struct us_data *us,
114 struct jumpshot_info *info,
115 u32 sector,
116 u32 sectors)
117{
118 unsigned char *command = us->iobuf;
119 unsigned char *buffer;
120 unsigned char thistime;
121 unsigned int totallen, alloclen;
122 int len, result;
123 unsigned int sg_idx = 0, sg_offset = 0;
124
125 // we're working in LBA mode. according to the ATA spec,
126 // we can support up to 28-bit addressing. I don't know if Jumpshot
127 // supports beyond 24-bit addressing. It's kind of hard to test
128 // since it requires > 8GB CF card.
129
130 if (sector > 0x0FFFFFFF)
131 return USB_STOR_TRANSPORT_ERROR;
132
133 totallen = sectors * info->ssize;
134
135 // Since we don't read more than 64 KB at a time, we have to create
136 // a bounce buffer and move the data a piece at a time between the
137 // bounce buffer and the actual transfer buffer.
138
139 alloclen = min(totallen, 65536u);
140 buffer = kmalloc(alloclen, GFP_NOIO);
141 if (buffer == NULL)
142 return USB_STOR_TRANSPORT_ERROR;
143
144 do {
145 // loop, never allocate or transfer more than 64k at once
146 // (min(128k, 255*info->ssize) is the real limit)
147 len = min(totallen, alloclen);
148 thistime = (len / info->ssize) & 0xff;
149
150 command[0] = 0;
151 command[1] = thistime;
152 command[2] = sector & 0xFF;
153 command[3] = (sector >> 8) & 0xFF;
154 command[4] = (sector >> 16) & 0xFF;
155
156 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
157 command[6] = 0x20;
158
159 // send the setup + command
160 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
161 0, 0x20, 0, 1, command, 7);
162 if (result != USB_STOR_XFER_GOOD)
163 goto leave;
164
165 // read the result
166 result = jumpshot_bulk_read(us, buffer, len);
167 if (result != USB_STOR_XFER_GOOD)
168 goto leave;
169
170 US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
171
172 // Store the data in the transfer buffer
173 usb_stor_access_xfer_buf(buffer, len, us->srb,
174 &sg_idx, &sg_offset, TO_XFER_BUF);
175
176 sector += thistime;
177 totallen -= len;
178 } while (totallen > 0);
179
180 kfree(buffer);
181 return USB_STOR_TRANSPORT_GOOD;
182
183 leave:
184 kfree(buffer);
185 return USB_STOR_TRANSPORT_ERROR;
186}
187
188
189static int jumpshot_write_data(struct us_data *us,
190 struct jumpshot_info *info,
191 u32 sector,
192 u32 sectors)
193{
194 unsigned char *command = us->iobuf;
195 unsigned char *buffer;
196 unsigned char thistime;
197 unsigned int totallen, alloclen;
198 int len, result, waitcount;
199 unsigned int sg_idx = 0, sg_offset = 0;
200
201 // we're working in LBA mode. according to the ATA spec,
202 // we can support up to 28-bit addressing. I don't know if Jumpshot
203 // supports beyond 24-bit addressing. It's kind of hard to test
204 // since it requires > 8GB CF card.
205 //
206 if (sector > 0x0FFFFFFF)
207 return USB_STOR_TRANSPORT_ERROR;
208
209 totallen = sectors * info->ssize;
210
211 // Since we don't write more than 64 KB at a time, we have to create
212 // a bounce buffer and move the data a piece at a time between the
213 // bounce buffer and the actual transfer buffer.
214
215 alloclen = min(totallen, 65536u);
216 buffer = kmalloc(alloclen, GFP_NOIO);
217 if (buffer == NULL)
218 return USB_STOR_TRANSPORT_ERROR;
219
220 do {
221 // loop, never allocate or transfer more than 64k at once
222 // (min(128k, 255*info->ssize) is the real limit)
223
224 len = min(totallen, alloclen);
225 thistime = (len / info->ssize) & 0xff;
226
227 // Get the data from the transfer buffer
228 usb_stor_access_xfer_buf(buffer, len, us->srb,
229 &sg_idx, &sg_offset, FROM_XFER_BUF);
230
231 command[0] = 0;
232 command[1] = thistime;
233 command[2] = sector & 0xFF;
234 command[3] = (sector >> 8) & 0xFF;
235 command[4] = (sector >> 16) & 0xFF;
236
237 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
238 command[6] = 0x30;
239
240 // send the setup + command
241 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
242 0, 0x20, 0, 1, command, 7);
243 if (result != USB_STOR_XFER_GOOD)
244 goto leave;
245
246 // send the data
247 result = jumpshot_bulk_write(us, buffer, len);
248 if (result != USB_STOR_XFER_GOOD)
249 goto leave;
250
251 // read the result. apparently the bulk write can complete
252 // before the jumpshot drive is finished writing. so we loop
253 // here until we get a good return code
254 waitcount = 0;
255 do {
256 result = jumpshot_get_status(us);
257 if (result != USB_STOR_TRANSPORT_GOOD) {
258 // I have not experimented to find the smallest value.
259 //
260 msleep(50);
261 }
262 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
263
264 if (result != USB_STOR_TRANSPORT_GOOD)
265 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
266
267 sector += thistime;
268 totallen -= len;
269 } while (totallen > 0);
270
271 kfree(buffer);
272 return result;
273
274 leave:
275 kfree(buffer);
276 return USB_STOR_TRANSPORT_ERROR;
277}
278
279static int jumpshot_id_device(struct us_data *us,
280 struct jumpshot_info *info)
281{
282 unsigned char *command = us->iobuf;
283 unsigned char *reply;
284 int rc;
285
286 if (!us || !info)
287 return USB_STOR_TRANSPORT_ERROR;
288
289 command[0] = 0xE0;
290 command[1] = 0xEC;
291 reply = kmalloc(512, GFP_NOIO);
292 if (!reply)
293 return USB_STOR_TRANSPORT_ERROR;
294
295 // send the setup
296 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
297 0, 0x20, 0, 6, command, 2);
298
299 if (rc != USB_STOR_XFER_GOOD) {
300 US_DEBUGP("jumpshot_id_device: Gah! "
301 "send_control for read_capacity failed\n");
302 rc = USB_STOR_TRANSPORT_ERROR;
303 goto leave;
304 }
305
306 // read the reply
307 rc = jumpshot_bulk_read(us, reply, 512);
308 if (rc != USB_STOR_XFER_GOOD) {
309 rc = USB_STOR_TRANSPORT_ERROR;
310 goto leave;
311 }
312
313 info->sectors = ((u32)(reply[117]) << 24) |
314 ((u32)(reply[116]) << 16) |
315 ((u32)(reply[115]) << 8) |
316 ((u32)(reply[114]) );
317
318 rc = USB_STOR_TRANSPORT_GOOD;
319
320 leave:
321 kfree(reply);
322 return rc;
323}
324
325static int jumpshot_handle_mode_sense(struct us_data *us,
326 struct scsi_cmnd * srb,
327 int sense_6)
328{
329 static unsigned char rw_err_page[12] = {
330 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
331 };
332 static unsigned char cache_page[12] = {
333 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
334 };
335 static unsigned char rbac_page[12] = {
336 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
337 };
338 static unsigned char timer_page[8] = {
339 0x1C, 0x6, 0, 0, 0, 0
340 };
341 unsigned char pc, page_code;
342 unsigned int i = 0;
343 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
344 unsigned char *ptr = us->iobuf;
345
346 pc = srb->cmnd[2] >> 6;
347 page_code = srb->cmnd[2] & 0x3F;
348
349 switch (pc) {
350 case 0x0:
351 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
352 break;
353 case 0x1:
354 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
355 break;
356 case 0x2:
357 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
358 break;
359 case 0x3:
360 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
361 break;
362 }
363
364 memset(ptr, 0, 8);
365 if (sense_6) {
366 ptr[2] = 0x00; // WP enable: 0x80
367 i = 4;
368 } else {
369 ptr[3] = 0x00; // WP enable: 0x80
370 i = 8;
371 }
372
373 switch (page_code) {
374 case 0x0:
375 // vendor-specific mode
376 info->sense_key = 0x05;
377 info->sense_asc = 0x24;
378 info->sense_ascq = 0x00;
379 return USB_STOR_TRANSPORT_FAILED;
380
381 case 0x1:
382 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
383 i += sizeof(rw_err_page);
384 break;
385
386 case 0x8:
387 memcpy(ptr + i, cache_page, sizeof(cache_page));
388 i += sizeof(cache_page);
389 break;
390
391 case 0x1B:
392 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
393 i += sizeof(rbac_page);
394 break;
395
396 case 0x1C:
397 memcpy(ptr + i, timer_page, sizeof(timer_page));
398 i += sizeof(timer_page);
399 break;
400
401 case 0x3F:
402 memcpy(ptr + i, timer_page, sizeof(timer_page));
403 i += sizeof(timer_page);
404 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
405 i += sizeof(rbac_page);
406 memcpy(ptr + i, cache_page, sizeof(cache_page));
407 i += sizeof(cache_page);
408 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
409 i += sizeof(rw_err_page);
410 break;
411 }
412
413 if (sense_6)
414 ptr[0] = i - 1;
415 else
416 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
417 usb_stor_set_xfer_buf(ptr, i, srb);
418
419 return USB_STOR_TRANSPORT_GOOD;
420}
421
422
423static void jumpshot_info_destructor(void *extra)
424{
425 // this routine is a placeholder...
426 // currently, we don't allocate any extra blocks so we're okay
427}
428
429
430
431// Transport for the Lexar 'Jumpshot'
432//
433int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
434{
435 struct jumpshot_info *info;
436 int rc;
437 unsigned long block, blocks;
438 unsigned char *ptr = us->iobuf;
439 static unsigned char inquiry_response[8] = {
440 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
441 };
442
443 if (!us->extra) {
444 us->extra = kmalloc(sizeof(struct jumpshot_info), GFP_NOIO);
445 if (!us->extra) {
446 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
447 return USB_STOR_TRANSPORT_ERROR;
448 }
449 memset(us->extra, 0, sizeof(struct jumpshot_info));
450 us->extra_destructor = jumpshot_info_destructor;
451 }
452
453 info = (struct jumpshot_info *) (us->extra);
454
455 if (srb->cmnd[0] == INQUIRY) {
456 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
457 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
458 fill_inquiry_response(us, ptr, 36);
459 return USB_STOR_TRANSPORT_GOOD;
460 }
461
462 if (srb->cmnd[0] == READ_CAPACITY) {
463 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
464
465 rc = jumpshot_get_status(us);
466 if (rc != USB_STOR_TRANSPORT_GOOD)
467 return rc;
468
469 rc = jumpshot_id_device(us, info);
470 if (rc != USB_STOR_TRANSPORT_GOOD)
471 return rc;
472
473 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
474 info->sectors, info->ssize);
475
476 // build the reply
477 //
478 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
479 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
480 usb_stor_set_xfer_buf(ptr, 8, srb);
481
482 return USB_STOR_TRANSPORT_GOOD;
483 }
484
485 if (srb->cmnd[0] == MODE_SELECT_10) {
486 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
487 return USB_STOR_TRANSPORT_ERROR;
488 }
489
490 if (srb->cmnd[0] == READ_10) {
491 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
492 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
493
494 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
495
496 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
497 return jumpshot_read_data(us, info, block, blocks);
498 }
499
500 if (srb->cmnd[0] == READ_12) {
501 // I don't think we'll ever see a READ_12 but support it anyway...
502 //
503 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
504 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
505
506 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
507 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
508
509 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
510 return jumpshot_read_data(us, info, block, blocks);
511 }
512
513 if (srb->cmnd[0] == WRITE_10) {
514 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
515 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
516
517 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
518
519 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
520 return jumpshot_write_data(us, info, block, blocks);
521 }
522
523 if (srb->cmnd[0] == WRITE_12) {
524 // I don't think we'll ever see a WRITE_12 but support it anyway...
525 //
526 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
527 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
528
529 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
530 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
531
532 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
533 return jumpshot_write_data(us, info, block, blocks);
534 }
535
536
537 if (srb->cmnd[0] == TEST_UNIT_READY) {
538 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
539 return jumpshot_get_status(us);
540 }
541
542 if (srb->cmnd[0] == REQUEST_SENSE) {
543 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
544
545 memset(ptr, 0, 18);
546 ptr[0] = 0xF0;
547 ptr[2] = info->sense_key;
548 ptr[7] = 11;
549 ptr[12] = info->sense_asc;
550 ptr[13] = info->sense_ascq;
551 usb_stor_set_xfer_buf(ptr, 18, srb);
552
553 return USB_STOR_TRANSPORT_GOOD;
554 }
555
556 if (srb->cmnd[0] == MODE_SENSE) {
557 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
558 return jumpshot_handle_mode_sense(us, srb, 1);
559 }
560
561 if (srb->cmnd[0] == MODE_SENSE_10) {
562 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
563 return jumpshot_handle_mode_sense(us, srb, 0);
564 }
565
566 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
567 // sure. whatever. not like we can stop the user from popping
568 // the media out of the device (no locking doors, etc)
569 //
570 return USB_STOR_TRANSPORT_GOOD;
571 }
572
573 if (srb->cmnd[0] == START_STOP) {
574 /* this is used by sd.c'check_scsidisk_media_change to detect
575 media change */
576 US_DEBUGP("jumpshot_transport: START_STOP.\n");
577 /* the first jumpshot_id_device after a media change returns
578 an error (determined experimentally) */
579 rc = jumpshot_id_device(us, info);
580 if (rc == USB_STOR_TRANSPORT_GOOD) {
581 info->sense_key = NO_SENSE;
582 srb->result = SUCCESS;
583 } else {
584 info->sense_key = UNIT_ATTENTION;
585 srb->result = SAM_STAT_CHECK_CONDITION;
586 }
587 return rc;
588 }
589
590 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
591 srb->cmnd[0], srb->cmnd[0]);
592 info->sense_key = 0x05;
593 info->sense_asc = 0x20;
594 info->sense_ascq = 0x00;
595 return USB_STOR_TRANSPORT_FAILED;
596}
diff --git a/drivers/usb/storage/jumpshot.h b/drivers/usb/storage/jumpshot.h
new file mode 100644
index 000000000000..19bac9d1558f
--- /dev/null
+++ b/drivers/usb/storage/jumpshot.h
@@ -0,0 +1,39 @@
1/* Driver for Lexar "Jumpshot" USB Compact Flash reader
2 * Header File
3 *
4 * Current development and maintenance by:
5 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
6 *
7 * See jumpshot.c for more explanation
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef _USB_JUMPSHOT_H
25#define _USB_JUMPSHOT_H
26
27extern int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us);
28
29struct jumpshot_info {
30 unsigned long sectors; // total sector count
31 unsigned long ssize; // sector size in bytes
32
33 // the following aren't used yet
34 unsigned char sense_key;
35 unsigned long sense_asc; // additional sense code
36 unsigned long sense_ascq; // additional sense code qualifier
37};
38
39#endif
diff --git a/drivers/usb/storage/protocol.c b/drivers/usb/storage/protocol.c
new file mode 100644
index 000000000000..9ad30428d2dd
--- /dev/null
+++ b/drivers/usb/storage/protocol.c
@@ -0,0 +1,254 @@
1/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: protocol.c,v 1.14 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2002 Alan Stern (stern@rowland.org)
11 *
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 *
15 * This driver is based on the 'USB Mass Storage Class' document. This
16 * describes in detail the protocol used to communicate with such
17 * devices. Clearly, the designers had SCSI and ATAPI commands in
18 * mind when they created this document. The commands are all very
19 * similar to commands in the SCSI-II and ATAPI specifications.
20 *
21 * It is important to note that in a number of cases this class
22 * exhibits class-specific exemptions from the USB specification.
23 * Notably the usage of NAK, STALL and ACK differs from the norm, in
24 * that they are used to communicate wait, failed and OK on commands.
25 *
26 * Also, for certain devices, the interrupt endpoint is used to convey
27 * status of a command.
28 *
29 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
30 * information about this driver.
31 *
32 * This program is free software; you can redistribute it and/or modify it
33 * under the terms of the GNU General Public License as published by the
34 * Free Software Foundation; either version 2, or (at your option) any
35 * later version.
36 *
37 * This program is distributed in the hope that it will be useful, but
38 * WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 * General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License along
43 * with this program; if not, write to the Free Software Foundation, Inc.,
44 * 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
46
47#include <linux/highmem.h>
48#include <scsi/scsi.h>
49#include <scsi/scsi_cmnd.h>
50
51#include "usb.h"
52#include "protocol.h"
53#include "debug.h"
54#include "scsiglue.h"
55#include "transport.h"
56
57/***********************************************************************
58 * Protocol routines
59 ***********************************************************************/
60
61void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
62{
63 /* Pad the ATAPI command with zeros
64 *
65 * NOTE: This only works because a scsi_cmnd struct field contains
66 * a unsigned char cmnd[16], so we know we have storage available
67 */
68 for (; srb->cmd_len<12; srb->cmd_len++)
69 srb->cmnd[srb->cmd_len] = 0;
70
71 /* set command length to 12 bytes */
72 srb->cmd_len = 12;
73
74 /* send the command to the transport layer */
75 usb_stor_invoke_transport(srb, us);
76}
77
78void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
79{
80 /* Pad the ATAPI command with zeros
81 *
82 * NOTE: This only works because a scsi_cmnd struct field contains
83 * a unsigned char cmnd[16], so we know we have storage available
84 */
85
86 /* Pad the ATAPI command with zeros */
87 for (; srb->cmd_len<12; srb->cmd_len++)
88 srb->cmnd[srb->cmd_len] = 0;
89
90 /* set command length to 12 bytes */
91 srb->cmd_len = 12;
92
93 /* send the command to the transport layer */
94 usb_stor_invoke_transport(srb, us);
95}
96
97
98void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
99{
100 /* fix some commands -- this is a form of mode translation
101 * UFI devices only accept 12 byte long commands
102 *
103 * NOTE: This only works because a scsi_cmnd struct field contains
104 * a unsigned char cmnd[16], so we know we have storage available
105 */
106
107 /* Pad the ATAPI command with zeros */
108 for (; srb->cmd_len<12; srb->cmd_len++)
109 srb->cmnd[srb->cmd_len] = 0;
110
111 /* set command length to 12 bytes (this affects the transport layer) */
112 srb->cmd_len = 12;
113
114 /* XXX We should be constantly re-evaluating the need for these */
115
116 /* determine the correct data length for these commands */
117 switch (srb->cmnd[0]) {
118
119 /* for INQUIRY, UFI devices only ever return 36 bytes */
120 case INQUIRY:
121 srb->cmnd[4] = 36;
122 break;
123
124 /* again, for MODE_SENSE_10, we get the minimum (8) */
125 case MODE_SENSE_10:
126 srb->cmnd[7] = 0;
127 srb->cmnd[8] = 8;
128 break;
129
130 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
131 case REQUEST_SENSE:
132 srb->cmnd[4] = 18;
133 break;
134 } /* end switch on cmnd[0] */
135
136 /* send the command to the transport layer */
137 usb_stor_invoke_transport(srb, us);
138}
139
140void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
141 struct us_data *us)
142{
143 /* send the command to the transport layer */
144 usb_stor_invoke_transport(srb, us);
145}
146
147/***********************************************************************
148 * Scatter-gather transfer buffer access routines
149 ***********************************************************************/
150
151/* Copy a buffer of length buflen to/from the srb's transfer buffer.
152 * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
153 * points to a list of s-g entries and we ignore srb->request_bufflen.
154 * For non-scatter-gather transfers, srb->request_buffer points to the
155 * transfer buffer itself and srb->request_bufflen is the buffer's length.)
156 * Update the *index and *offset variables so that the next copy will
157 * pick up from where this one left off. */
158
159unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
160 unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index,
161 unsigned int *offset, enum xfer_buf_dir dir)
162{
163 unsigned int cnt;
164
165 /* If not using scatter-gather, just transfer the data directly.
166 * Make certain it will fit in the available buffer space. */
167 if (srb->use_sg == 0) {
168 if (*offset >= srb->request_bufflen)
169 return 0;
170 cnt = min(buflen, srb->request_bufflen - *offset);
171 if (dir == TO_XFER_BUF)
172 memcpy((unsigned char *) srb->request_buffer + *offset,
173 buffer, cnt);
174 else
175 memcpy(buffer, (unsigned char *) srb->request_buffer +
176 *offset, cnt);
177 *offset += cnt;
178
179 /* Using scatter-gather. We have to go through the list one entry
180 * at a time. Each s-g entry contains some number of pages, and
181 * each page has to be kmap()'ed separately. If the page is already
182 * in kernel-addressable memory then kmap() will return its address.
183 * If the page is not directly accessible -- such as a user buffer
184 * located in high memory -- then kmap() will map it to a temporary
185 * position in the kernel's virtual address space. */
186 } else {
187 struct scatterlist *sg =
188 (struct scatterlist *) srb->request_buffer
189 + *index;
190
191 /* This loop handles a single s-g list entry, which may
192 * include multiple pages. Find the initial page structure
193 * and the starting offset within the page, and update
194 * the *offset and *index values for the next loop. */
195 cnt = 0;
196 while (cnt < buflen && *index < srb->use_sg) {
197 struct page *page = sg->page +
198 ((sg->offset + *offset) >> PAGE_SHIFT);
199 unsigned int poff =
200 (sg->offset + *offset) & (PAGE_SIZE-1);
201 unsigned int sglen = sg->length - *offset;
202
203 if (sglen > buflen - cnt) {
204
205 /* Transfer ends within this s-g entry */
206 sglen = buflen - cnt;
207 *offset += sglen;
208 } else {
209
210 /* Transfer continues to next s-g entry */
211 *offset = 0;
212 ++*index;
213 ++sg;
214 }
215
216 /* Transfer the data for all the pages in this
217 * s-g entry. For each page: call kmap(), do the
218 * transfer, and call kunmap() immediately after. */
219 while (sglen > 0) {
220 unsigned int plen = min(sglen, (unsigned int)
221 PAGE_SIZE - poff);
222 unsigned char *ptr = kmap(page);
223
224 if (dir == TO_XFER_BUF)
225 memcpy(ptr + poff, buffer + cnt, plen);
226 else
227 memcpy(buffer + cnt, ptr + poff, plen);
228 kunmap(page);
229
230 /* Start at the beginning of the next page */
231 poff = 0;
232 ++page;
233 cnt += plen;
234 sglen -= plen;
235 }
236 }
237 }
238
239 /* Return the amount actually transferred */
240 return cnt;
241}
242
243/* Store the contents of buffer into srb's transfer buffer and set the
244 * SCSI residue. */
245void usb_stor_set_xfer_buf(unsigned char *buffer,
246 unsigned int buflen, struct scsi_cmnd *srb)
247{
248 unsigned int index = 0, offset = 0;
249
250 usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
251 TO_XFER_BUF);
252 if (buflen < srb->request_bufflen)
253 srb->resid = srb->request_bufflen - buflen;
254}
diff --git a/drivers/usb/storage/protocol.h b/drivers/usb/storage/protocol.h
new file mode 100644
index 000000000000..02bff01ab09c
--- /dev/null
+++ b/drivers/usb/storage/protocol.h
@@ -0,0 +1,74 @@
1/* Driver for USB Mass Storage compliant devices
2 * Protocol Functions Header File
3 *
4 * $Id: protocol.h,v 1.4 2001/02/13 07:10:03 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * This driver is based on the 'USB Mass Storage Class' document. This
10 * describes in detail the protocol used to communicate with such
11 * devices. Clearly, the designers had SCSI and ATAPI commands in
12 * mind when they created this document. The commands are all very
13 * similar to commands in the SCSI-II and ATAPI specifications.
14 *
15 * It is important to note that in a number of cases this class
16 * exhibits class-specific exemptions from the USB specification.
17 * Notably the usage of NAK, STALL and ACK differs from the norm, in
18 * that they are used to communicate wait, failed and OK on commands.
19 *
20 * Also, for certain devices, the interrupt endpoint is used to convey
21 * status of a command.
22 *
23 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
24 * information about this driver.
25 *
26 * This program is free software; you can redistribute it and/or modify it
27 * under the terms of the GNU General Public License as published by the
28 * Free Software Foundation; either version 2, or (at your option) any
29 * later version.
30 *
31 * This program is distributed in the hope that it will be useful, but
32 * WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License along
37 * with this program; if not, write to the Free Software Foundation, Inc.,
38 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 */
40
41#ifndef _PROTOCOL_H_
42#define _PROTOCOL_H_
43
44/* Sub Classes */
45
46#define US_SC_RBC 0x01 /* Typically, flash devices */
47#define US_SC_8020 0x02 /* CD-ROM */
48#define US_SC_QIC 0x03 /* QIC-157 Tapes */
49#define US_SC_UFI 0x04 /* Floppy */
50#define US_SC_8070 0x05 /* Removable media */
51#define US_SC_SCSI 0x06 /* Transparent */
52#define US_SC_ISD200 0x07 /* ISD200 ATA */
53#define US_SC_MIN US_SC_RBC
54#define US_SC_MAX US_SC_ISD200
55
56#define US_SC_DEVICE 0xff /* Use device's value */
57
58/* Protocol handling routines */
59extern void usb_stor_ATAPI_command(struct scsi_cmnd*, struct us_data*);
60extern void usb_stor_qic157_command(struct scsi_cmnd*, struct us_data*);
61extern void usb_stor_ufi_command(struct scsi_cmnd*, struct us_data*);
62extern void usb_stor_transparent_scsi_command(struct scsi_cmnd*,
63 struct us_data*);
64
65/* struct scsi_cmnd transfer buffer access utilities */
66enum xfer_buf_dir {TO_XFER_BUF, FROM_XFER_BUF};
67
68extern unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
69 unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index,
70 unsigned int *offset, enum xfer_buf_dir dir);
71
72extern void usb_stor_set_xfer_buf(unsigned char *buffer,
73 unsigned int buflen, struct scsi_cmnd *srb);
74#endif
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
new file mode 100644
index 000000000000..22e48a2b0bd1
--- /dev/null
+++ b/drivers/usb/storage/scsiglue.c
@@ -0,0 +1,500 @@
1/* Driver for USB Mass Storage compliant devices
2 * SCSI layer glue code
3 *
4 * $Id: scsiglue.c,v 1.26 2002/04/22 03:39:43 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Developed with the assistance of:
10 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
11 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
12 *
13 * Initial work by:
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 *
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
21 *
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
36 * later version.
37 *
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
48#include <linux/slab.h>
49#include <linux/module.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_devinfo.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_eh.h>
56
57#include "usb.h"
58#include "scsiglue.h"
59#include "debug.h"
60#include "transport.h"
61#include "protocol.h"
62
63/***********************************************************************
64 * Host functions
65 ***********************************************************************/
66
67static const char* host_info(struct Scsi_Host *host)
68{
69 return "SCSI emulation for USB Mass Storage devices";
70}
71
72static int slave_alloc (struct scsi_device *sdev)
73{
74 /*
75 * Set the INQUIRY transfer length to 36. We don't use any of
76 * the extra data and many devices choke if asked for more or
77 * less than 36 bytes.
78 */
79 sdev->inquiry_len = 36;
80 return 0;
81}
82
83static int slave_configure(struct scsi_device *sdev)
84{
85 struct us_data *us = host_to_us(sdev->host);
86
87 /* Scatter-gather buffers (all but the last) must have a length
88 * divisible by the bulk maxpacket size. Otherwise a data packet
89 * would end up being short, causing a premature end to the data
90 * transfer. Since high-speed bulk pipes have a maxpacket size
91 * of 512, we'll use that as the scsi device queue's DMA alignment
92 * mask. Guaranteeing proper alignment of the first buffer will
93 * have the desired effect because, except at the beginning and
94 * the end, scatter-gather buffers follow page boundaries. */
95 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
96
97 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
98 * what is originally reported. We need this to avoid confusing
99 * the SCSI layer with devices that report 0 or 1, but need 10-byte
100 * commands (ala ATAPI devices behind certain bridges, or devices
101 * which simply have broken INQUIRY data).
102 *
103 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
104 * actual information. This seems to be the preference for
105 * programs like that.
106 *
107 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
108 * the actual value or the modified one, depending on where the
109 * data comes from.
110 */
111 if (sdev->scsi_level < SCSI_2)
112 sdev->scsi_level = SCSI_2;
113
114 /* According to the technical support people at Genesys Logic,
115 * devices using their chips have problems transferring more than
116 * 32 KB at a time. In practice people have found that 64 KB
117 * works okay and that's what Windows does. But we'll be
118 * conservative; people can always use the sysfs interface to
119 * increase max_sectors. */
120 if (le16_to_cpu(us->pusb_dev->descriptor.idVendor) == USB_VENDOR_ID_GENESYS &&
121 sdev->request_queue->max_sectors > 64)
122 blk_queue_max_sectors(sdev->request_queue, 64);
123
124 /* We can't put these settings in slave_alloc() because that gets
125 * called before the device type is known. Consequently these
126 * settings can't be overridden via the scsi devinfo mechanism. */
127 if (sdev->type == TYPE_DISK) {
128
129 /* Disk-type devices use MODE SENSE(6) if the protocol
130 * (SubClass) is Transparent SCSI, otherwise they use
131 * MODE SENSE(10). */
132 if (us->subclass != US_SC_SCSI)
133 sdev->use_10_for_ms = 1;
134
135 /* Many disks only accept MODE SENSE transfer lengths of
136 * 192 bytes (that's what Windows uses). */
137 sdev->use_192_bytes_for_3f = 1;
138
139 /* Some devices don't like MODE SENSE with page=0x3f,
140 * which is the command used for checking if a device
141 * is write-protected. Now that we tell the sd driver
142 * to do a 192-byte transfer with this command the
143 * majority of devices work fine, but a few still can't
144 * handle it. The sd driver will simply assume those
145 * devices are write-enabled. */
146 if (us->flags & US_FL_NO_WP_DETECT)
147 sdev->skip_ms_page_3f = 1;
148
149 /* A number of devices have problems with MODE SENSE for
150 * page x08, so we will skip it. */
151 sdev->skip_ms_page_8 = 1;
152
153 /* Some disks return the total number of blocks in response
154 * to READ CAPACITY rather than the highest block number.
155 * If this device makes that mistake, tell the sd driver. */
156 if (us->flags & US_FL_FIX_CAPACITY)
157 sdev->fix_capacity = 1;
158 } else {
159
160 /* Non-disk-type devices don't need to blacklist any pages
161 * or to force 192-byte transfer lengths for MODE SENSE.
162 * But they do need to use MODE SENSE(10). */
163 sdev->use_10_for_ms = 1;
164 }
165
166 /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
167 * REMOVAL command, so suppress those commands. */
168 if (us->flags & US_FL_NOT_LOCKABLE)
169 sdev->lockable = 0;
170
171 /* this is to satisfy the compiler, tho I don't think the
172 * return code is ever checked anywhere. */
173 return 0;
174}
175
176/* queue a command */
177/* This is always called with scsi_lock(host) held */
178static int queuecommand(struct scsi_cmnd *srb,
179 void (*done)(struct scsi_cmnd *))
180{
181 struct us_data *us = host_to_us(srb->device->host);
182
183 US_DEBUGP("%s called\n", __FUNCTION__);
184
185 /* check for state-transition errors */
186 if (us->srb != NULL) {
187 printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
188 __FUNCTION__, us->srb);
189 return SCSI_MLQUEUE_HOST_BUSY;
190 }
191
192 /* fail the command if we are disconnecting */
193 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
194 US_DEBUGP("Fail command during disconnect\n");
195 srb->result = DID_NO_CONNECT << 16;
196 done(srb);
197 return 0;
198 }
199
200 /* enqueue the command and wake up the control thread */
201 srb->scsi_done = done;
202 us->srb = srb;
203 up(&(us->sema));
204
205 return 0;
206}
207
208/***********************************************************************
209 * Error handling functions
210 ***********************************************************************/
211
212/* Command timeout and abort */
213/* This is always called with scsi_lock(host) held */
214static int command_abort(struct scsi_cmnd *srb)
215{
216 struct us_data *us = host_to_us(srb->device->host);
217
218 US_DEBUGP("%s called\n", __FUNCTION__);
219
220 /* Is this command still active? */
221 if (us->srb != srb) {
222 US_DEBUGP ("-- nothing to abort\n");
223 return FAILED;
224 }
225
226 /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
227 * a device reset isn't already in progress (to avoid interfering
228 * with the reset). To prevent races with auto-reset, we must
229 * stop any ongoing USB transfers while still holding the host
230 * lock. */
231 set_bit(US_FLIDX_TIMED_OUT, &us->flags);
232 if (!test_bit(US_FLIDX_RESETTING, &us->flags)) {
233 set_bit(US_FLIDX_ABORTING, &us->flags);
234 usb_stor_stop_transport(us);
235 }
236 scsi_unlock(us_to_host(us));
237
238 /* Wait for the aborted command to finish */
239 wait_for_completion(&us->notify);
240
241 /* Reacquire the lock and allow USB transfers to resume */
242 scsi_lock(us_to_host(us));
243 clear_bit(US_FLIDX_ABORTING, &us->flags);
244 clear_bit(US_FLIDX_TIMED_OUT, &us->flags);
245 return SUCCESS;
246}
247
248/* This invokes the transport reset mechanism to reset the state of the
249 * device */
250/* This is always called with scsi_lock(host) held */
251static int device_reset(struct scsi_cmnd *srb)
252{
253 struct us_data *us = host_to_us(srb->device->host);
254 int result;
255
256 US_DEBUGP("%s called\n", __FUNCTION__);
257
258 scsi_unlock(us_to_host(us));
259
260 /* lock the device pointers and do the reset */
261 down(&(us->dev_semaphore));
262 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
263 result = FAILED;
264 US_DEBUGP("No reset during disconnect\n");
265 } else
266 result = us->transport_reset(us);
267 up(&(us->dev_semaphore));
268
269 /* lock the host for the return */
270 scsi_lock(us_to_host(us));
271 return result;
272}
273
274/* This resets the device's USB port. */
275/* It refuses to work if there's more than one interface in
276 * the device, so that other users are not affected. */
277/* This is always called with scsi_lock(host) held */
278static int bus_reset(struct scsi_cmnd *srb)
279{
280 struct us_data *us = host_to_us(srb->device->host);
281 int result, rc;
282
283 US_DEBUGP("%s called\n", __FUNCTION__);
284
285 scsi_unlock(us_to_host(us));
286
287 /* The USB subsystem doesn't handle synchronisation between
288 * a device's several drivers. Therefore we reset only devices
289 * with just one interface, which we of course own. */
290
291 down(&(us->dev_semaphore));
292 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
293 result = -EIO;
294 US_DEBUGP("No reset during disconnect\n");
295 } else if (us->pusb_dev->actconfig->desc.bNumInterfaces != 1) {
296 result = -EBUSY;
297 US_DEBUGP("Refusing to reset a multi-interface device\n");
298 } else {
299 rc = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
300 if (rc < 0) {
301 US_DEBUGP("unable to lock device for reset: %d\n", rc);
302 result = rc;
303 } else {
304 result = usb_reset_device(us->pusb_dev);
305 if (rc)
306 usb_unlock_device(us->pusb_dev);
307 US_DEBUGP("usb_reset_device returns %d\n", result);
308 }
309 }
310 up(&(us->dev_semaphore));
311
312 /* lock the host for the return */
313 scsi_lock(us_to_host(us));
314 return result < 0 ? FAILED : SUCCESS;
315}
316
317/* Report a driver-initiated device reset to the SCSI layer.
318 * Calling this for a SCSI-initiated reset is unnecessary but harmless.
319 * The caller must own the SCSI host lock. */
320void usb_stor_report_device_reset(struct us_data *us)
321{
322 int i;
323 struct Scsi_Host *host = us_to_host(us);
324
325 scsi_report_device_reset(host, 0, 0);
326 if (us->flags & US_FL_SCM_MULT_TARG) {
327 for (i = 1; i < host->max_id; ++i)
328 scsi_report_device_reset(host, 0, i);
329 }
330}
331
332/***********************************************************************
333 * /proc/scsi/ functions
334 ***********************************************************************/
335
336/* we use this macro to help us write into the buffer */
337#undef SPRINTF
338#define SPRINTF(args...) \
339 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
340
341static int proc_info (struct Scsi_Host *host, char *buffer,
342 char **start, off_t offset, int length, int inout)
343{
344 struct us_data *us = host_to_us(host);
345 char *pos = buffer;
346 const char *string;
347
348 /* if someone is sending us data, just throw it away */
349 if (inout)
350 return length;
351
352 /* print the controller name */
353 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
354
355 /* print product, vendor, and serial number strings */
356 if (us->pusb_dev->manufacturer)
357 string = us->pusb_dev->manufacturer;
358 else if (us->unusual_dev->vendorName)
359 string = us->unusual_dev->vendorName;
360 else
361 string = "Unknown";
362 SPRINTF(" Vendor: %s\n", string);
363 if (us->pusb_dev->product)
364 string = us->pusb_dev->product;
365 else if (us->unusual_dev->productName)
366 string = us->unusual_dev->productName;
367 else
368 string = "Unknown";
369 SPRINTF(" Product: %s\n", string);
370 if (us->pusb_dev->serial)
371 string = us->pusb_dev->serial;
372 else
373 string = "None";
374 SPRINTF("Serial Number: %s\n", string);
375
376 /* show the protocol and transport */
377 SPRINTF(" Protocol: %s\n", us->protocol_name);
378 SPRINTF(" Transport: %s\n", us->transport_name);
379
380 /* show the device flags */
381 if (pos < buffer + length) {
382 pos += sprintf(pos, " Quirks:");
383
384#define US_FLAG(name, value) \
385 if (us->flags & value) pos += sprintf(pos, " " #name);
386US_DO_ALL_FLAGS
387#undef US_FLAG
388
389 *(pos++) = '\n';
390 }
391
392 /*
393 * Calculate start of next buffer, and return value.
394 */
395 *start = buffer + offset;
396
397 if ((pos - buffer) < offset)
398 return (0);
399 else if ((pos - buffer - offset) < length)
400 return (pos - buffer - offset);
401 else
402 return (length);
403}
404
405/***********************************************************************
406 * Sysfs interface
407 ***********************************************************************/
408
409/* Output routine for the sysfs max_sectors file */
410static ssize_t show_max_sectors(struct device *dev, char *buf)
411{
412 struct scsi_device *sdev = to_scsi_device(dev);
413
414 return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
415}
416
417/* Input routine for the sysfs max_sectors file */
418static ssize_t store_max_sectors(struct device *dev, const char *buf,
419 size_t count)
420{
421 struct scsi_device *sdev = to_scsi_device(dev);
422 unsigned short ms;
423
424 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
425 blk_queue_max_sectors(sdev->request_queue, ms);
426 return strlen(buf);
427 }
428 return -EINVAL;
429}
430
431static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
432 store_max_sectors);
433
434static struct device_attribute *sysfs_device_attr_list[] = {
435 &dev_attr_max_sectors,
436 NULL,
437 };
438
439/*
440 * this defines our host template, with which we'll allocate hosts
441 */
442
443struct scsi_host_template usb_stor_host_template = {
444 /* basic userland interface stuff */
445 .name = "usb-storage",
446 .proc_name = "usb-storage",
447 .proc_info = proc_info,
448 .info = host_info,
449
450 /* command interface -- queued only */
451 .queuecommand = queuecommand,
452
453 /* error and abort handlers */
454 .eh_abort_handler = command_abort,
455 .eh_device_reset_handler = device_reset,
456 .eh_bus_reset_handler = bus_reset,
457
458 /* queue commands only, only one command per LUN */
459 .can_queue = 1,
460 .cmd_per_lun = 1,
461
462 /* unknown initiator id */
463 .this_id = -1,
464
465 .slave_alloc = slave_alloc,
466 .slave_configure = slave_configure,
467
468 /* lots of sg segments can be handled */
469 .sg_tablesize = SG_ALL,
470
471 /* limit the total size of a transfer to 120 KB */
472 .max_sectors = 240,
473
474 /* merge commands... this seems to help performance, but
475 * periodically someone should test to see which setting is more
476 * optimal.
477 */
478 .use_clustering = 1,
479
480 /* emulated HBA */
481 .emulated = 1,
482
483 /* we do our own delay after a device or bus reset */
484 .skip_settle_delay = 1,
485
486 /* sysfs device attributes */
487 .sdev_attrs = sysfs_device_attr_list,
488
489 /* module management */
490 .module = THIS_MODULE
491};
492
493/* To Report "Illegal Request: Invalid Field in CDB */
494unsigned char usb_stor_sense_invalidCDB[18] = {
495 [0] = 0x70, /* current error */
496 [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
497 [7] = 0x0a, /* additional length */
498 [12] = 0x24 /* Invalid Field in CDB */
499};
500
diff --git a/drivers/usb/storage/scsiglue.h b/drivers/usb/storage/scsiglue.h
new file mode 100644
index 000000000000..d0a49af026c4
--- /dev/null
+++ b/drivers/usb/storage/scsiglue.h
@@ -0,0 +1,49 @@
1/* Driver for USB Mass Storage compliant devices
2 * SCSI Connecting Glue Header File
3 *
4 * $Id: scsiglue.h,v 1.4 2000/08/25 00:13:51 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * This driver is based on the 'USB Mass Storage Class' document. This
10 * describes in detail the protocol used to communicate with such
11 * devices. Clearly, the designers had SCSI and ATAPI commands in
12 * mind when they created this document. The commands are all very
13 * similar to commands in the SCSI-II and ATAPI specifications.
14 *
15 * It is important to note that in a number of cases this class
16 * exhibits class-specific exemptions from the USB specification.
17 * Notably the usage of NAK, STALL and ACK differs from the norm, in
18 * that they are used to communicate wait, failed and OK on commands.
19 *
20 * Also, for certain devices, the interrupt endpoint is used to convey
21 * status of a command.
22 *
23 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
24 * information about this driver.
25 *
26 * This program is free software; you can redistribute it and/or modify it
27 * under the terms of the GNU General Public License as published by the
28 * Free Software Foundation; either version 2, or (at your option) any
29 * later version.
30 *
31 * This program is distributed in the hope that it will be useful, but
32 * WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License along
37 * with this program; if not, write to the Free Software Foundation, Inc.,
38 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 */
40
41#ifndef _SCSIGLUE_H_
42#define _SCSIGLUE_H_
43
44extern void usb_stor_report_device_reset(struct us_data *us);
45
46extern unsigned char usb_stor_sense_invalidCDB[18];
47extern struct scsi_host_template usb_stor_host_template;
48
49#endif
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
new file mode 100644
index 000000000000..0ea2f5ab66ba
--- /dev/null
+++ b/drivers/usb/storage/sddr09.c
@@ -0,0 +1,1608 @@
1/* Driver for SanDisk SDDR-09 SmartMedia reader
2 *
3 * $Id: sddr09.c,v 1.24 2002/04/22 03:39:43 mdharm Exp $
4 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
5 * (c) 2002 Andries Brouwer (aeb@cwi.nl)
6 * Developed with the assistance of:
7 * (c) 2002 Alan Stern <stern@rowland.org>
8 *
9 * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip.
10 * This chip is a programmable USB controller. In the SDDR-09, it has
11 * been programmed to obey a certain limited set of SCSI commands.
12 * This driver translates the "real" SCSI commands to the SDDR-09 SCSI
13 * commands.
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2, or (at your option) any
18 * later version.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30/*
31 * Known vendor commands: 12 bytes, first byte is opcode
32 *
33 * E7: read scatter gather
34 * E8: read
35 * E9: write
36 * EA: erase
37 * EB: reset
38 * EC: read status
39 * ED: read ID
40 * EE: write CIS (?)
41 * EF: compute checksum (?)
42 */
43
44#include <linux/sched.h>
45#include <linux/errno.h>
46#include <linux/slab.h>
47
48#include <scsi/scsi.h>
49#include <scsi/scsi_cmnd.h>
50
51#include "usb.h"
52#include "transport.h"
53#include "protocol.h"
54#include "debug.h"
55#include "sddr09.h"
56
57
58#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
59#define LSB_of(s) ((s)&0xFF)
60#define MSB_of(s) ((s)>>8)
61
62/* #define US_DEBUGP printk */
63
64/*
65 * First some stuff that does not belong here:
66 * data on SmartMedia and other cards, completely
67 * unrelated to this driver.
68 * Similar stuff occurs in <linux/mtd/nand_ids.h>.
69 */
70
71struct nand_flash_dev {
72 int model_id;
73 int chipshift; /* 1<<cs bytes total capacity */
74 char pageshift; /* 1<<ps bytes in a page */
75 char blockshift; /* 1<<bs pages in an erase block */
76 char zoneshift; /* 1<<zs blocks in a zone */
77 /* # of logical blocks is 125/128 of this */
78 char pageadrlen; /* length of an address in bytes - 1 */
79};
80
81/*
82 * NAND Flash Manufacturer ID Codes
83 */
84#define NAND_MFR_AMD 0x01
85#define NAND_MFR_NATSEMI 0x8f
86#define NAND_MFR_TOSHIBA 0x98
87#define NAND_MFR_SAMSUNG 0xec
88
89static inline char *nand_flash_manufacturer(int manuf_id) {
90 switch(manuf_id) {
91 case NAND_MFR_AMD:
92 return "AMD";
93 case NAND_MFR_NATSEMI:
94 return "NATSEMI";
95 case NAND_MFR_TOSHIBA:
96 return "Toshiba";
97 case NAND_MFR_SAMSUNG:
98 return "Samsung";
99 default:
100 return "unknown";
101 }
102}
103
104/*
105 * It looks like it is unnecessary to attach manufacturer to the
106 * remaining data: SSFDC prescribes manufacturer-independent id codes.
107 *
108 * 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda.
109 */
110
111static struct nand_flash_dev nand_flash_ids[] = {
112 /* NAND flash */
113 { 0x6e, 20, 8, 4, 8, 2}, /* 1 MB */
114 { 0xe8, 20, 8, 4, 8, 2}, /* 1 MB */
115 { 0xec, 20, 8, 4, 8, 2}, /* 1 MB */
116 { 0x64, 21, 8, 4, 9, 2}, /* 2 MB */
117 { 0xea, 21, 8, 4, 9, 2}, /* 2 MB */
118 { 0x6b, 22, 9, 4, 9, 2}, /* 4 MB */
119 { 0xe3, 22, 9, 4, 9, 2}, /* 4 MB */
120 { 0xe5, 22, 9, 4, 9, 2}, /* 4 MB */
121 { 0xe6, 23, 9, 4, 10, 2}, /* 8 MB */
122 { 0x73, 24, 9, 5, 10, 2}, /* 16 MB */
123 { 0x75, 25, 9, 5, 10, 2}, /* 32 MB */
124 { 0x76, 26, 9, 5, 10, 3}, /* 64 MB */
125 { 0x79, 27, 9, 5, 10, 3}, /* 128 MB */
126
127 /* MASK ROM */
128 { 0x5d, 21, 9, 4, 8, 2}, /* 2 MB */
129 { 0xd5, 22, 9, 4, 9, 2}, /* 4 MB */
130 { 0xd6, 23, 9, 4, 10, 2}, /* 8 MB */
131 { 0x57, 24, 9, 4, 11, 2}, /* 16 MB */
132 { 0x58, 25, 9, 4, 12, 2}, /* 32 MB */
133 { 0,}
134};
135
136#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
137
138static struct nand_flash_dev *
139nand_find_id(unsigned char id) {
140 int i;
141
142 for (i = 0; i < SIZE(nand_flash_ids); i++)
143 if (nand_flash_ids[i].model_id == id)
144 return &(nand_flash_ids[i]);
145 return NULL;
146}
147
148/*
149 * ECC computation.
150 */
151static unsigned char parity[256];
152static unsigned char ecc2[256];
153
154static void nand_init_ecc(void) {
155 int i, j, a;
156
157 parity[0] = 0;
158 for (i = 1; i < 256; i++)
159 parity[i] = (parity[i&(i-1)] ^ 1);
160
161 for (i = 0; i < 256; i++) {
162 a = 0;
163 for (j = 0; j < 8; j++) {
164 if (i & (1<<j)) {
165 if ((j & 1) == 0)
166 a ^= 0x04;
167 if ((j & 2) == 0)
168 a ^= 0x10;
169 if ((j & 4) == 0)
170 a ^= 0x40;
171 }
172 }
173 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
174 }
175}
176
177/* compute 3-byte ecc on 256 bytes */
178static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
179 int i, j, a;
180 unsigned char par, bit, bits[8];
181
182 par = 0;
183 for (j = 0; j < 8; j++)
184 bits[j] = 0;
185
186 /* collect 16 checksum bits */
187 for (i = 0; i < 256; i++) {
188 par ^= data[i];
189 bit = parity[data[i]];
190 for (j = 0; j < 8; j++)
191 if ((i & (1<<j)) == 0)
192 bits[j] ^= bit;
193 }
194
195 /* put 4+4+4 = 12 bits in the ecc */
196 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
197 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
198
199 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
200 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
201
202 ecc[2] = ecc2[par];
203}
204
205static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
206 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
207}
208
209static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
210 memcpy(data, ecc, 3);
211}
212
213/*
214 * The actual driver starts here.
215 */
216
217/*
218 * On my 16MB card, control blocks have size 64 (16 real control bytes,
219 * and 48 junk bytes). In reality of course the card uses 16 control bytes,
220 * so the reader makes up the remaining 48. Don't know whether these numbers
221 * depend on the card. For now a constant.
222 */
223#define CONTROL_SHIFT 6
224
225/*
226 * On my Combo CF/SM reader, the SM reader has LUN 1.
227 * (and things fail with LUN 0).
228 * It seems LUN is irrelevant for others.
229 */
230#define LUN 1
231#define LUNBITS (LUN << 5)
232
233/*
234 * LBA and PBA are unsigned ints. Special values.
235 */
236#define UNDEF 0xffffffff
237#define SPARE 0xfffffffe
238#define UNUSABLE 0xfffffffd
239
240static int erase_bad_lba_entries = 0;
241
242/* send vendor interface command (0x41) */
243/* called for requests 0, 1, 8 */
244static int
245sddr09_send_command(struct us_data *us,
246 unsigned char request,
247 unsigned char direction,
248 unsigned char *xfer_data,
249 unsigned int xfer_len) {
250 unsigned int pipe;
251 unsigned char requesttype = (0x41 | direction);
252 int rc;
253
254 // Get the receive or send control pipe number
255
256 if (direction == USB_DIR_IN)
257 pipe = us->recv_ctrl_pipe;
258 else
259 pipe = us->send_ctrl_pipe;
260
261 rc = usb_stor_ctrl_transfer(us, pipe, request, requesttype,
262 0, 0, xfer_data, xfer_len);
263 return (rc == USB_STOR_XFER_GOOD ? USB_STOR_TRANSPORT_GOOD :
264 USB_STOR_TRANSPORT_ERROR);
265}
266
267static int
268sddr09_send_scsi_command(struct us_data *us,
269 unsigned char *command,
270 unsigned int command_len) {
271 return sddr09_send_command(us, 0, USB_DIR_OUT, command, command_len);
272}
273
274#if 0
275/*
276 * Test Unit Ready Command: 12 bytes.
277 * byte 0: opcode: 00
278 */
279static int
280sddr09_test_unit_ready(struct us_data *us) {
281 unsigned char *command = us->iobuf;
282 int result;
283
284 memset(command, 0, 6);
285 command[1] = LUNBITS;
286
287 result = sddr09_send_scsi_command(us, command, 6);
288
289 US_DEBUGP("sddr09_test_unit_ready returns %d\n", result);
290
291 return result;
292}
293#endif
294
295/*
296 * Request Sense Command: 12 bytes.
297 * byte 0: opcode: 03
298 * byte 4: data length
299 */
300static int
301sddr09_request_sense(struct us_data *us, unsigned char *sensebuf, int buflen) {
302 unsigned char *command = us->iobuf;
303 int result;
304
305 memset(command, 0, 12);
306 command[0] = 0x03;
307 command[1] = LUNBITS;
308 command[4] = buflen;
309
310 result = sddr09_send_scsi_command(us, command, 12);
311 if (result != USB_STOR_TRANSPORT_GOOD) {
312 US_DEBUGP("request sense failed\n");
313 return result;
314 }
315
316 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
317 sensebuf, buflen, NULL);
318 if (result != USB_STOR_XFER_GOOD) {
319 US_DEBUGP("request sense bulk in failed\n");
320 return USB_STOR_TRANSPORT_ERROR;
321 } else {
322 US_DEBUGP("request sense worked\n");
323 return USB_STOR_TRANSPORT_GOOD;
324 }
325}
326
327/*
328 * Read Command: 12 bytes.
329 * byte 0: opcode: E8
330 * byte 1: last two bits: 00: read data, 01: read blockwise control,
331 * 10: read both, 11: read pagewise control.
332 * It turns out we need values 20, 21, 22, 23 here (LUN 1).
333 * bytes 2-5: address (interpretation depends on byte 1, see below)
334 * bytes 10-11: count (idem)
335 *
336 * A page has 512 data bytes and 64 control bytes (16 control and 48 junk).
337 * A read data command gets data in 512-byte pages.
338 * A read control command gets control in 64-byte chunks.
339 * A read both command gets data+control in 576-byte chunks.
340 *
341 * Blocks are groups of 32 pages, and read blockwise control jumps to the
342 * next block, while read pagewise control jumps to the next page after
343 * reading a group of 64 control bytes.
344 * [Here 512 = 1<<pageshift, 32 = 1<<blockshift, 64 is constant?]
345 *
346 * (1 MB and 2 MB cards are a bit different, but I have only a 16 MB card.)
347 */
348
349static int
350sddr09_readX(struct us_data *us, int x, unsigned long fromaddress,
351 int nr_of_pages, int bulklen, unsigned char *buf,
352 int use_sg) {
353
354 unsigned char *command = us->iobuf;
355 int result;
356
357 command[0] = 0xE8;
358 command[1] = LUNBITS | x;
359 command[2] = MSB_of(fromaddress>>16);
360 command[3] = LSB_of(fromaddress>>16);
361 command[4] = MSB_of(fromaddress & 0xFFFF);
362 command[5] = LSB_of(fromaddress & 0xFFFF);
363 command[6] = 0;
364 command[7] = 0;
365 command[8] = 0;
366 command[9] = 0;
367 command[10] = MSB_of(nr_of_pages);
368 command[11] = LSB_of(nr_of_pages);
369
370 result = sddr09_send_scsi_command(us, command, 12);
371
372 if (result != USB_STOR_TRANSPORT_GOOD) {
373 US_DEBUGP("Result for send_control in sddr09_read2%d %d\n",
374 x, result);
375 return result;
376 }
377
378 result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
379 buf, bulklen, use_sg, NULL);
380
381 if (result != USB_STOR_XFER_GOOD) {
382 US_DEBUGP("Result for bulk_transfer in sddr09_read2%d %d\n",
383 x, result);
384 return USB_STOR_TRANSPORT_ERROR;
385 }
386 return USB_STOR_TRANSPORT_GOOD;
387}
388
389/*
390 * Read Data
391 *
392 * fromaddress counts data shorts:
393 * increasing it by 256 shifts the bytestream by 512 bytes;
394 * the last 8 bits are ignored.
395 *
396 * nr_of_pages counts pages of size (1 << pageshift).
397 */
398static int
399sddr09_read20(struct us_data *us, unsigned long fromaddress,
400 int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
401 int bulklen = nr_of_pages << pageshift;
402
403 /* The last 8 bits of fromaddress are ignored. */
404 return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen,
405 buf, use_sg);
406}
407
408/*
409 * Read Blockwise Control
410 *
411 * fromaddress gives the starting position (as in read data;
412 * the last 8 bits are ignored); increasing it by 32*256 shifts
413 * the output stream by 64 bytes.
414 *
415 * count counts control groups of size (1 << controlshift).
416 * For me, controlshift = 6. Is this constant?
417 *
418 * After getting one control group, jump to the next block
419 * (fromaddress += 8192).
420 */
421static int
422sddr09_read21(struct us_data *us, unsigned long fromaddress,
423 int count, int controlshift, unsigned char *buf, int use_sg) {
424
425 int bulklen = (count << controlshift);
426 return sddr09_readX(us, 1, fromaddress, count, bulklen,
427 buf, use_sg);
428}
429
430/*
431 * Read both Data and Control
432 *
433 * fromaddress counts data shorts, ignoring control:
434 * increasing it by 256 shifts the bytestream by 576 = 512+64 bytes;
435 * the last 8 bits are ignored.
436 *
437 * nr_of_pages counts pages of size (1 << pageshift) + (1 << controlshift).
438 */
439static int
440sddr09_read22(struct us_data *us, unsigned long fromaddress,
441 int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
442
443 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
444 US_DEBUGP("sddr09_read22: reading %d pages, %d bytes\n",
445 nr_of_pages, bulklen);
446 return sddr09_readX(us, 2, fromaddress, nr_of_pages, bulklen,
447 buf, use_sg);
448}
449
450#if 0
451/*
452 * Read Pagewise Control
453 *
454 * fromaddress gives the starting position (as in read data;
455 * the last 8 bits are ignored); increasing it by 256 shifts
456 * the output stream by 64 bytes.
457 *
458 * count counts control groups of size (1 << controlshift).
459 * For me, controlshift = 6. Is this constant?
460 *
461 * After getting one control group, jump to the next page
462 * (fromaddress += 256).
463 */
464static int
465sddr09_read23(struct us_data *us, unsigned long fromaddress,
466 int count, int controlshift, unsigned char *buf, int use_sg) {
467
468 int bulklen = (count << controlshift);
469 return sddr09_readX(us, 3, fromaddress, count, bulklen,
470 buf, use_sg);
471}
472#endif
473
474/*
475 * Erase Command: 12 bytes.
476 * byte 0: opcode: EA
477 * bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
478 *
479 * Always precisely one block is erased; bytes 2-5 and 10-11 are ignored.
480 * The byte address being erased is 2*Eaddress.
481 * The CIS cannot be erased.
482 */
483static int
484sddr09_erase(struct us_data *us, unsigned long Eaddress) {
485 unsigned char *command = us->iobuf;
486 int result;
487
488 US_DEBUGP("sddr09_erase: erase address %lu\n", Eaddress);
489
490 memset(command, 0, 12);
491 command[0] = 0xEA;
492 command[1] = LUNBITS;
493 command[6] = MSB_of(Eaddress>>16);
494 command[7] = LSB_of(Eaddress>>16);
495 command[8] = MSB_of(Eaddress & 0xFFFF);
496 command[9] = LSB_of(Eaddress & 0xFFFF);
497
498 result = sddr09_send_scsi_command(us, command, 12);
499
500 if (result != USB_STOR_TRANSPORT_GOOD)
501 US_DEBUGP("Result for send_control in sddr09_erase %d\n",
502 result);
503
504 return result;
505}
506
507/*
508 * Write CIS Command: 12 bytes.
509 * byte 0: opcode: EE
510 * bytes 2-5: write address in shorts
511 * bytes 10-11: sector count
512 *
513 * This writes at the indicated address. Don't know how it differs
514 * from E9. Maybe it does not erase? However, it will also write to
515 * the CIS.
516 *
517 * When two such commands on the same page follow each other directly,
518 * the second one is not done.
519 */
520
521/*
522 * Write Command: 12 bytes.
523 * byte 0: opcode: E9
524 * bytes 2-5: write address (big-endian, counting shorts, sector aligned).
525 * bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
526 * bytes 10-11: sector count (big-endian, in 512-byte sectors).
527 *
528 * If write address equals erase address, the erase is done first,
529 * otherwise the write is done first. When erase address equals zero
530 * no erase is done?
531 */
532static int
533sddr09_writeX(struct us_data *us,
534 unsigned long Waddress, unsigned long Eaddress,
535 int nr_of_pages, int bulklen, unsigned char *buf, int use_sg) {
536
537 unsigned char *command = us->iobuf;
538 int result;
539
540 command[0] = 0xE9;
541 command[1] = LUNBITS;
542
543 command[2] = MSB_of(Waddress>>16);
544 command[3] = LSB_of(Waddress>>16);
545 command[4] = MSB_of(Waddress & 0xFFFF);
546 command[5] = LSB_of(Waddress & 0xFFFF);
547
548 command[6] = MSB_of(Eaddress>>16);
549 command[7] = LSB_of(Eaddress>>16);
550 command[8] = MSB_of(Eaddress & 0xFFFF);
551 command[9] = LSB_of(Eaddress & 0xFFFF);
552
553 command[10] = MSB_of(nr_of_pages);
554 command[11] = LSB_of(nr_of_pages);
555
556 result = sddr09_send_scsi_command(us, command, 12);
557
558 if (result != USB_STOR_TRANSPORT_GOOD) {
559 US_DEBUGP("Result for send_control in sddr09_writeX %d\n",
560 result);
561 return result;
562 }
563
564 result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
565 buf, bulklen, use_sg, NULL);
566
567 if (result != USB_STOR_XFER_GOOD) {
568 US_DEBUGP("Result for bulk_transfer in sddr09_writeX %d\n",
569 result);
570 return USB_STOR_TRANSPORT_ERROR;
571 }
572 return USB_STOR_TRANSPORT_GOOD;
573}
574
575/* erase address, write same address */
576static int
577sddr09_write_inplace(struct us_data *us, unsigned long address,
578 int nr_of_pages, int pageshift, unsigned char *buf,
579 int use_sg) {
580 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
581 return sddr09_writeX(us, address, address, nr_of_pages, bulklen,
582 buf, use_sg);
583}
584
585#if 0
586/*
587 * Read Scatter Gather Command: 3+4n bytes.
588 * byte 0: opcode E7
589 * byte 2: n
590 * bytes 4i-1,4i,4i+1: page address
591 * byte 4i+2: page count
592 * (i=1..n)
593 *
594 * This reads several pages from the card to a single memory buffer.
595 * The last two bits of byte 1 have the same meaning as for E8.
596 */
597static int
598sddr09_read_sg_test_only(struct us_data *us) {
599 unsigned char *command = us->iobuf;
600 int result, bulklen, nsg, ct;
601 unsigned char *buf;
602 unsigned long address;
603
604 nsg = bulklen = 0;
605 command[0] = 0xE7;
606 command[1] = LUNBITS;
607 command[2] = 0;
608 address = 040000; ct = 1;
609 nsg++;
610 bulklen += (ct << 9);
611 command[4*nsg+2] = ct;
612 command[4*nsg+1] = ((address >> 9) & 0xFF);
613 command[4*nsg+0] = ((address >> 17) & 0xFF);
614 command[4*nsg-1] = ((address >> 25) & 0xFF);
615
616 address = 0340000; ct = 1;
617 nsg++;
618 bulklen += (ct << 9);
619 command[4*nsg+2] = ct;
620 command[4*nsg+1] = ((address >> 9) & 0xFF);
621 command[4*nsg+0] = ((address >> 17) & 0xFF);
622 command[4*nsg-1] = ((address >> 25) & 0xFF);
623
624 address = 01000000; ct = 2;
625 nsg++;
626 bulklen += (ct << 9);
627 command[4*nsg+2] = ct;
628 command[4*nsg+1] = ((address >> 9) & 0xFF);
629 command[4*nsg+0] = ((address >> 17) & 0xFF);
630 command[4*nsg-1] = ((address >> 25) & 0xFF);
631
632 command[2] = nsg;
633
634 result = sddr09_send_scsi_command(us, command, 4*nsg+3);
635
636 if (result != USB_STOR_TRANSPORT_GOOD) {
637 US_DEBUGP("Result for send_control in sddr09_read_sg %d\n",
638 result);
639 return result;
640 }
641
642 buf = (unsigned char *) kmalloc(bulklen, GFP_NOIO);
643 if (!buf)
644 return USB_STOR_TRANSPORT_ERROR;
645
646 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
647 buf, bulklen, NULL);
648 kfree(buf);
649 if (result != USB_STOR_XFER_GOOD) {
650 US_DEBUGP("Result for bulk_transfer in sddr09_read_sg %d\n",
651 result);
652 return USB_STOR_TRANSPORT_ERROR;
653 }
654
655 return USB_STOR_TRANSPORT_GOOD;
656}
657#endif
658
659/*
660 * Read Status Command: 12 bytes.
661 * byte 0: opcode: EC
662 *
663 * Returns 64 bytes, all zero except for the first.
664 * bit 0: 1: Error
665 * bit 5: 1: Suspended
666 * bit 6: 1: Ready
667 * bit 7: 1: Not write-protected
668 */
669
670static int
671sddr09_read_status(struct us_data *us, unsigned char *status) {
672
673 unsigned char *command = us->iobuf;
674 unsigned char *data = us->iobuf;
675 int result;
676
677 US_DEBUGP("Reading status...\n");
678
679 memset(command, 0, 12);
680 command[0] = 0xEC;
681 command[1] = LUNBITS;
682
683 result = sddr09_send_scsi_command(us, command, 12);
684 if (result != USB_STOR_TRANSPORT_GOOD)
685 return result;
686
687 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
688 data, 64, NULL);
689 *status = data[0];
690 return (result == USB_STOR_XFER_GOOD ?
691 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
692}
693
694static int
695sddr09_read_data(struct us_data *us,
696 unsigned long address,
697 unsigned int sectors) {
698
699 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
700 unsigned char *buffer;
701 unsigned int lba, maxlba, pba;
702 unsigned int page, pages;
703 unsigned int len, index, offset;
704 int result;
705
706 // Since we only read in one block at a time, we have to create
707 // a bounce buffer and move the data a piece at a time between the
708 // bounce buffer and the actual transfer buffer.
709
710 len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
711 buffer = kmalloc(len, GFP_NOIO);
712 if (buffer == NULL) {
713 printk("sddr09_read_data: Out of memory\n");
714 return USB_STOR_TRANSPORT_ERROR;
715 }
716
717 // Figure out the initial LBA and page
718 lba = address >> info->blockshift;
719 page = (address & info->blockmask);
720 maxlba = info->capacity >> (info->pageshift + info->blockshift);
721
722 // This could be made much more efficient by checking for
723 // contiguous LBA's. Another exercise left to the student.
724
725 result = USB_STOR_TRANSPORT_GOOD;
726 index = offset = 0;
727
728 while (sectors > 0) {
729
730 /* Find number of pages we can read in this block */
731 pages = min(sectors, info->blocksize - page);
732 len = pages << info->pageshift;
733
734 /* Not overflowing capacity? */
735 if (lba >= maxlba) {
736 US_DEBUGP("Error: Requested lba %u exceeds "
737 "maximum %u\n", lba, maxlba);
738 result = USB_STOR_TRANSPORT_ERROR;
739 break;
740 }
741
742 /* Find where this lba lives on disk */
743 pba = info->lba_to_pba[lba];
744
745 if (pba == UNDEF) { /* this lba was never written */
746
747 US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
748 pages, lba, page);
749
750 /* This is not really an error. It just means
751 that the block has never been written.
752 Instead of returning USB_STOR_TRANSPORT_ERROR
753 it is better to return all zero data. */
754
755 memset(buffer, 0, len);
756
757 } else {
758 US_DEBUGP("Read %d pages, from PBA %d"
759 " (LBA %d) page %d\n",
760 pages, pba, lba, page);
761
762 address = ((pba << info->blockshift) + page) <<
763 info->pageshift;
764
765 result = sddr09_read20(us, address>>1,
766 pages, info->pageshift, buffer, 0);
767 if (result != USB_STOR_TRANSPORT_GOOD)
768 break;
769 }
770
771 // Store the data in the transfer buffer
772 usb_stor_access_xfer_buf(buffer, len, us->srb,
773 &index, &offset, TO_XFER_BUF);
774
775 page = 0;
776 lba++;
777 sectors -= pages;
778 }
779
780 kfree(buffer);
781 return result;
782}
783
784static unsigned int
785sddr09_find_unused_pba(struct sddr09_card_info *info, unsigned int lba) {
786 static unsigned int lastpba = 1;
787 int zonestart, end, i;
788
789 zonestart = (lba/1000) << 10;
790 end = info->capacity >> (info->blockshift + info->pageshift);
791 end -= zonestart;
792 if (end > 1024)
793 end = 1024;
794
795 for (i = lastpba+1; i < end; i++) {
796 if (info->pba_to_lba[zonestart+i] == UNDEF) {
797 lastpba = i;
798 return zonestart+i;
799 }
800 }
801 for (i = 0; i <= lastpba; i++) {
802 if (info->pba_to_lba[zonestart+i] == UNDEF) {
803 lastpba = i;
804 return zonestart+i;
805 }
806 }
807 return 0;
808}
809
810static int
811sddr09_write_lba(struct us_data *us, unsigned int lba,
812 unsigned int page, unsigned int pages,
813 unsigned char *ptr, unsigned char *blockbuffer) {
814
815 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
816 unsigned long address;
817 unsigned int pba, lbap;
818 unsigned int pagelen;
819 unsigned char *bptr, *cptr, *xptr;
820 unsigned char ecc[3];
821 int i, result, isnew;
822
823 lbap = ((lba % 1000) << 1) | 0x1000;
824 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
825 lbap ^= 1;
826 pba = info->lba_to_pba[lba];
827 isnew = 0;
828
829 if (pba == UNDEF) {
830 pba = sddr09_find_unused_pba(info, lba);
831 if (!pba) {
832 printk("sddr09_write_lba: Out of unused blocks\n");
833 return USB_STOR_TRANSPORT_ERROR;
834 }
835 info->pba_to_lba[pba] = lba;
836 info->lba_to_pba[lba] = pba;
837 isnew = 1;
838 }
839
840 if (pba == 1) {
841 /* Maybe it is impossible to write to PBA 1.
842 Fake success, but don't do anything. */
843 printk("sddr09: avoid writing to pba 1\n");
844 return USB_STOR_TRANSPORT_GOOD;
845 }
846
847 pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
848
849 /* read old contents */
850 address = (pba << (info->pageshift + info->blockshift));
851 result = sddr09_read22(us, address>>1, info->blocksize,
852 info->pageshift, blockbuffer, 0);
853 if (result != USB_STOR_TRANSPORT_GOOD)
854 return result;
855
856 /* check old contents and fill lba */
857 for (i = 0; i < info->blocksize; i++) {
858 bptr = blockbuffer + i*pagelen;
859 cptr = bptr + info->pagesize;
860 nand_compute_ecc(bptr, ecc);
861 if (!nand_compare_ecc(cptr+13, ecc)) {
862 US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
863 i, pba);
864 nand_store_ecc(cptr+13, ecc);
865 }
866 nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
867 if (!nand_compare_ecc(cptr+8, ecc)) {
868 US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
869 i, pba);
870 nand_store_ecc(cptr+8, ecc);
871 }
872 cptr[6] = cptr[11] = MSB_of(lbap);
873 cptr[7] = cptr[12] = LSB_of(lbap);
874 }
875
876 /* copy in new stuff and compute ECC */
877 xptr = ptr;
878 for (i = page; i < page+pages; i++) {
879 bptr = blockbuffer + i*pagelen;
880 cptr = bptr + info->pagesize;
881 memcpy(bptr, xptr, info->pagesize);
882 xptr += info->pagesize;
883 nand_compute_ecc(bptr, ecc);
884 nand_store_ecc(cptr+13, ecc);
885 nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
886 nand_store_ecc(cptr+8, ecc);
887 }
888
889 US_DEBUGP("Rewrite PBA %d (LBA %d)\n", pba, lba);
890
891 result = sddr09_write_inplace(us, address>>1, info->blocksize,
892 info->pageshift, blockbuffer, 0);
893
894 US_DEBUGP("sddr09_write_inplace returns %d\n", result);
895
896#if 0
897 {
898 unsigned char status = 0;
899 int result2 = sddr09_read_status(us, &status);
900 if (result2 != USB_STOR_TRANSPORT_GOOD)
901 US_DEBUGP("sddr09_write_inplace: cannot read status\n");
902 else if (status != 0xc0)
903 US_DEBUGP("sddr09_write_inplace: status after write: 0x%x\n",
904 status);
905 }
906#endif
907
908#if 0
909 {
910 int result2 = sddr09_test_unit_ready(us);
911 }
912#endif
913
914 return result;
915}
916
917static int
918sddr09_write_data(struct us_data *us,
919 unsigned long address,
920 unsigned int sectors) {
921
922 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
923 unsigned int lba, page, pages;
924 unsigned int pagelen, blocklen;
925 unsigned char *blockbuffer;
926 unsigned char *buffer;
927 unsigned int len, index, offset;
928 int result;
929
930 // blockbuffer is used for reading in the old data, overwriting
931 // with the new data, and performing ECC calculations
932
933 /* TODO: instead of doing kmalloc/kfree for each write,
934 add a bufferpointer to the info structure */
935
936 pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
937 blocklen = (pagelen << info->blockshift);
938 blockbuffer = kmalloc(blocklen, GFP_NOIO);
939 if (!blockbuffer) {
940 printk("sddr09_write_data: Out of memory\n");
941 return USB_STOR_TRANSPORT_ERROR;
942 }
943
944 // Since we don't write the user data directly to the device,
945 // we have to create a bounce buffer and move the data a piece
946 // at a time between the bounce buffer and the actual transfer buffer.
947
948 len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
949 buffer = kmalloc(len, GFP_NOIO);
950 if (buffer == NULL) {
951 printk("sddr09_write_data: Out of memory\n");
952 kfree(blockbuffer);
953 return USB_STOR_TRANSPORT_ERROR;
954 }
955
956 // Figure out the initial LBA and page
957 lba = address >> info->blockshift;
958 page = (address & info->blockmask);
959
960 result = USB_STOR_TRANSPORT_GOOD;
961 index = offset = 0;
962
963 while (sectors > 0) {
964
965 // Write as many sectors as possible in this block
966
967 pages = min(sectors, info->blocksize - page);
968 len = (pages << info->pageshift);
969
970 // Get the data from the transfer buffer
971 usb_stor_access_xfer_buf(buffer, len, us->srb,
972 &index, &offset, FROM_XFER_BUF);
973
974 result = sddr09_write_lba(us, lba, page, pages,
975 buffer, blockbuffer);
976 if (result != USB_STOR_TRANSPORT_GOOD)
977 break;
978
979 page = 0;
980 lba++;
981 sectors -= pages;
982 }
983
984 kfree(buffer);
985 kfree(blockbuffer);
986
987 return result;
988}
989
990static int
991sddr09_read_control(struct us_data *us,
992 unsigned long address,
993 unsigned int blocks,
994 unsigned char *content,
995 int use_sg) {
996
997 US_DEBUGP("Read control address %lu, blocks %d\n",
998 address, blocks);
999
1000 return sddr09_read21(us, address, blocks,
1001 CONTROL_SHIFT, content, use_sg);
1002}
1003
1004/*
1005 * Read Device ID Command: 12 bytes.
1006 * byte 0: opcode: ED
1007 *
1008 * Returns 2 bytes: Manufacturer ID and Device ID.
1009 * On more recent cards 3 bytes: the third byte is an option code A5
1010 * signifying that the secret command to read an 128-bit ID is available.
1011 * On still more recent cards 4 bytes: the fourth byte C0 means that
1012 * a second read ID cmd is available.
1013 */
1014static int
1015sddr09_read_deviceID(struct us_data *us, unsigned char *deviceID) {
1016 unsigned char *command = us->iobuf;
1017 unsigned char *content = us->iobuf;
1018 int result, i;
1019
1020 memset(command, 0, 12);
1021 command[0] = 0xED;
1022 command[1] = LUNBITS;
1023
1024 result = sddr09_send_scsi_command(us, command, 12);
1025 if (result != USB_STOR_TRANSPORT_GOOD)
1026 return result;
1027
1028 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1029 content, 64, NULL);
1030
1031 for (i = 0; i < 4; i++)
1032 deviceID[i] = content[i];
1033
1034 return (result == USB_STOR_XFER_GOOD ?
1035 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
1036}
1037
1038static int
1039sddr09_get_wp(struct us_data *us, struct sddr09_card_info *info) {
1040 int result;
1041 unsigned char status;
1042
1043 result = sddr09_read_status(us, &status);
1044 if (result != USB_STOR_TRANSPORT_GOOD) {
1045 US_DEBUGP("sddr09_get_wp: read_status fails\n");
1046 return result;
1047 }
1048 US_DEBUGP("sddr09_get_wp: status 0x%02X", status);
1049 if ((status & 0x80) == 0) {
1050 info->flags |= SDDR09_WP; /* write protected */
1051 US_DEBUGP(" WP");
1052 }
1053 if (status & 0x40)
1054 US_DEBUGP(" Ready");
1055 if (status & LUNBITS)
1056 US_DEBUGP(" Suspended");
1057 if (status & 0x1)
1058 US_DEBUGP(" Error");
1059 US_DEBUGP("\n");
1060 return USB_STOR_TRANSPORT_GOOD;
1061}
1062
1063#if 0
1064/*
1065 * Reset Command: 12 bytes.
1066 * byte 0: opcode: EB
1067 */
1068static int
1069sddr09_reset(struct us_data *us) {
1070
1071 unsigned char *command = us->iobuf;
1072
1073 memset(command, 0, 12);
1074 command[0] = 0xEB;
1075 command[1] = LUNBITS;
1076
1077 return sddr09_send_scsi_command(us, command, 12);
1078}
1079#endif
1080
1081static struct nand_flash_dev *
1082sddr09_get_cardinfo(struct us_data *us, unsigned char flags) {
1083 struct nand_flash_dev *cardinfo;
1084 unsigned char deviceID[4];
1085 char blurbtxt[256];
1086 int result;
1087
1088 US_DEBUGP("Reading capacity...\n");
1089
1090 result = sddr09_read_deviceID(us, deviceID);
1091
1092 if (result != USB_STOR_TRANSPORT_GOOD) {
1093 US_DEBUGP("Result of read_deviceID is %d\n", result);
1094 printk("sddr09: could not read card info\n");
1095 return NULL;
1096 }
1097
1098 sprintf(blurbtxt, "sddr09: Found Flash card, ID = %02X %02X %02X %02X",
1099 deviceID[0], deviceID[1], deviceID[2], deviceID[3]);
1100
1101 /* Byte 0 is the manufacturer */
1102 sprintf(blurbtxt + strlen(blurbtxt),
1103 ": Manuf. %s",
1104 nand_flash_manufacturer(deviceID[0]));
1105
1106 /* Byte 1 is the device type */
1107 cardinfo = nand_find_id(deviceID[1]);
1108 if (cardinfo) {
1109 /* MB or MiB? It is neither. A 16 MB card has
1110 17301504 raw bytes, of which 16384000 are
1111 usable for user data. */
1112 sprintf(blurbtxt + strlen(blurbtxt),
1113 ", %d MB", 1<<(cardinfo->chipshift - 20));
1114 } else {
1115 sprintf(blurbtxt + strlen(blurbtxt),
1116 ", type unrecognized");
1117 }
1118
1119 /* Byte 2 is code to signal availability of 128-bit ID */
1120 if (deviceID[2] == 0xa5) {
1121 sprintf(blurbtxt + strlen(blurbtxt),
1122 ", 128-bit ID");
1123 }
1124
1125 /* Byte 3 announces the availability of another read ID command */
1126 if (deviceID[3] == 0xc0) {
1127 sprintf(blurbtxt + strlen(blurbtxt),
1128 ", extra cmd");
1129 }
1130
1131 if (flags & SDDR09_WP)
1132 sprintf(blurbtxt + strlen(blurbtxt),
1133 ", WP");
1134
1135 printk("%s\n", blurbtxt);
1136
1137 return cardinfo;
1138}
1139
1140static int
1141sddr09_read_map(struct us_data *us) {
1142
1143 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
1144 int numblocks, alloc_len, alloc_blocks;
1145 int i, j, result;
1146 unsigned char *buffer, *buffer_end, *ptr;
1147 unsigned int lba, lbact;
1148
1149 if (!info->capacity)
1150 return -1;
1151
1152 // size of a block is 1 << (blockshift + pageshift) bytes
1153 // divide into the total capacity to get the number of blocks
1154
1155 numblocks = info->capacity >> (info->blockshift + info->pageshift);
1156
1157 // read 64 bytes for every block (actually 1 << CONTROL_SHIFT)
1158 // but only use a 64 KB buffer
1159 // buffer size used must be a multiple of (1 << CONTROL_SHIFT)
1160#define SDDR09_READ_MAP_BUFSZ 65536
1161
1162 alloc_blocks = min(numblocks, SDDR09_READ_MAP_BUFSZ >> CONTROL_SHIFT);
1163 alloc_len = (alloc_blocks << CONTROL_SHIFT);
1164 buffer = kmalloc(alloc_len, GFP_NOIO);
1165 if (buffer == NULL) {
1166 printk("sddr09_read_map: out of memory\n");
1167 result = -1;
1168 goto done;
1169 }
1170 buffer_end = buffer + alloc_len;
1171
1172#undef SDDR09_READ_MAP_BUFSZ
1173
1174 kfree(info->lba_to_pba);
1175 kfree(info->pba_to_lba);
1176 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
1177 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
1178
1179 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
1180 printk("sddr09_read_map: out of memory\n");
1181 result = -1;
1182 goto done;
1183 }
1184
1185 for (i = 0; i < numblocks; i++)
1186 info->lba_to_pba[i] = info->pba_to_lba[i] = UNDEF;
1187
1188 /*
1189 * Define lba-pba translation table
1190 */
1191
1192 ptr = buffer_end;
1193 for (i = 0; i < numblocks; i++) {
1194 ptr += (1 << CONTROL_SHIFT);
1195 if (ptr >= buffer_end) {
1196 unsigned long address;
1197
1198 address = i << (info->pageshift + info->blockshift);
1199 result = sddr09_read_control(
1200 us, address>>1,
1201 min(alloc_blocks, numblocks - i),
1202 buffer, 0);
1203 if (result != USB_STOR_TRANSPORT_GOOD) {
1204 result = -1;
1205 goto done;
1206 }
1207 ptr = buffer;
1208 }
1209
1210 if (i == 0 || i == 1) {
1211 info->pba_to_lba[i] = UNUSABLE;
1212 continue;
1213 }
1214
1215 /* special PBAs have control field 0^16 */
1216 for (j = 0; j < 16; j++)
1217 if (ptr[j] != 0)
1218 goto nonz;
1219 info->pba_to_lba[i] = UNUSABLE;
1220 printk("sddr09: PBA %d has no logical mapping\n", i);
1221 continue;
1222
1223 nonz:
1224 /* unwritten PBAs have control field FF^16 */
1225 for (j = 0; j < 16; j++)
1226 if (ptr[j] != 0xff)
1227 goto nonff;
1228 continue;
1229
1230 nonff:
1231 /* normal PBAs start with six FFs */
1232 if (j < 6) {
1233 printk("sddr09: PBA %d has no logical mapping: "
1234 "reserved area = %02X%02X%02X%02X "
1235 "data status %02X block status %02X\n",
1236 i, ptr[0], ptr[1], ptr[2], ptr[3],
1237 ptr[4], ptr[5]);
1238 info->pba_to_lba[i] = UNUSABLE;
1239 continue;
1240 }
1241
1242 if ((ptr[6] >> 4) != 0x01) {
1243 printk("sddr09: PBA %d has invalid address field "
1244 "%02X%02X/%02X%02X\n",
1245 i, ptr[6], ptr[7], ptr[11], ptr[12]);
1246 info->pba_to_lba[i] = UNUSABLE;
1247 continue;
1248 }
1249
1250 /* check even parity */
1251 if (parity[ptr[6] ^ ptr[7]]) {
1252 printk("sddr09: Bad parity in LBA for block %d"
1253 " (%02X %02X)\n", i, ptr[6], ptr[7]);
1254 info->pba_to_lba[i] = UNUSABLE;
1255 continue;
1256 }
1257
1258 lba = short_pack(ptr[7], ptr[6]);
1259 lba = (lba & 0x07FF) >> 1;
1260
1261 /*
1262 * Every 1024 physical blocks ("zone"), the LBA numbers
1263 * go back to zero, but are within a higher block of LBA's.
1264 * Also, there is a maximum of 1000 LBA's per zone.
1265 * In other words, in PBA 1024-2047 you will find LBA 0-999
1266 * which are really LBA 1000-1999. This allows for 24 bad
1267 * or special physical blocks per zone.
1268 */
1269
1270 if (lba >= 1000) {
1271 printk("sddr09: Bad low LBA %d for block %d\n",
1272 lba, i);
1273 goto possibly_erase;
1274 }
1275
1276 lba += 1000*(i/0x400);
1277
1278 if (info->lba_to_pba[lba] != UNDEF) {
1279 printk("sddr09: LBA %d seen for PBA %d and %d\n",
1280 lba, info->lba_to_pba[lba], i);
1281 goto possibly_erase;
1282 }
1283
1284 info->pba_to_lba[i] = lba;
1285 info->lba_to_pba[lba] = i;
1286 continue;
1287
1288 possibly_erase:
1289 if (erase_bad_lba_entries) {
1290 unsigned long address;
1291
1292 address = (i << (info->pageshift + info->blockshift));
1293 sddr09_erase(us, address>>1);
1294 info->pba_to_lba[i] = UNDEF;
1295 } else
1296 info->pba_to_lba[i] = UNUSABLE;
1297 }
1298
1299 /*
1300 * Approximate capacity. This is not entirely correct yet,
1301 * since a zone with less than 1000 usable pages leads to
1302 * missing LBAs. Especially if it is the last zone, some
1303 * LBAs can be past capacity.
1304 */
1305 lbact = 0;
1306 for (i = 0; i < numblocks; i += 1024) {
1307 int ct = 0;
1308
1309 for (j = 0; j < 1024 && i+j < numblocks; j++) {
1310 if (info->pba_to_lba[i+j] != UNUSABLE) {
1311 if (ct >= 1000)
1312 info->pba_to_lba[i+j] = SPARE;
1313 else
1314 ct++;
1315 }
1316 }
1317 lbact += ct;
1318 }
1319 info->lbact = lbact;
1320 US_DEBUGP("Found %d LBA's\n", lbact);
1321 result = 0;
1322
1323 done:
1324 if (result != 0) {
1325 kfree(info->lba_to_pba);
1326 kfree(info->pba_to_lba);
1327 info->lba_to_pba = NULL;
1328 info->pba_to_lba = NULL;
1329 }
1330 kfree(buffer);
1331 return result;
1332}
1333
1334static void
1335sddr09_card_info_destructor(void *extra) {
1336 struct sddr09_card_info *info = (struct sddr09_card_info *)extra;
1337
1338 if (!info)
1339 return;
1340
1341 kfree(info->lba_to_pba);
1342 kfree(info->pba_to_lba);
1343}
1344
1345static void
1346sddr09_init_card_info(struct us_data *us) {
1347 if (!us->extra) {
1348 us->extra = kmalloc(sizeof(struct sddr09_card_info), GFP_NOIO);
1349 if (us->extra) {
1350 memset(us->extra, 0, sizeof(struct sddr09_card_info));
1351 us->extra_destructor = sddr09_card_info_destructor;
1352 }
1353 }
1354}
1355
1356/*
1357 * This is needed at a very early stage. If this is not listed in the
1358 * unusual devices list but called from here then LUN 0 of the combo reader
1359 * is not recognized. But I do not know what precisely these calls do.
1360 */
1361int
1362sddr09_init(struct us_data *us) {
1363 int result;
1364 unsigned char *data = us->iobuf;
1365
1366 result = sddr09_send_command(us, 0x01, USB_DIR_IN, data, 2);
1367 if (result != USB_STOR_TRANSPORT_GOOD) {
1368 US_DEBUGP("sddr09_init: send_command fails\n");
1369 return result;
1370 }
1371
1372 US_DEBUGP("SDDR09init: %02X %02X\n", data[0], data[1]);
1373 // get 07 02
1374
1375 result = sddr09_send_command(us, 0x08, USB_DIR_IN, data, 2);
1376 if (result != USB_STOR_TRANSPORT_GOOD) {
1377 US_DEBUGP("sddr09_init: 2nd send_command fails\n");
1378 return result;
1379 }
1380
1381 US_DEBUGP("SDDR09init: %02X %02X\n", data[0], data[1]);
1382 // get 07 00
1383
1384 result = sddr09_request_sense(us, data, 18);
1385 if (result == USB_STOR_TRANSPORT_GOOD && data[2] != 0) {
1386 int j;
1387 for (j=0; j<18; j++)
1388 printk(" %02X", data[j]);
1389 printk("\n");
1390 // get 70 00 00 00 00 00 00 * 00 00 00 00 00 00
1391 // 70: current command
1392 // sense key 0, sense code 0, extd sense code 0
1393 // additional transfer length * = sizeof(data) - 7
1394 // Or: 70 00 06 00 00 00 00 0b 00 00 00 00 28 00 00 00 00 00
1395 // sense key 06, sense code 28: unit attention,
1396 // not ready to ready transition
1397 }
1398
1399 // test unit ready
1400
1401 return USB_STOR_TRANSPORT_GOOD; /* not result */
1402}
1403
1404/*
1405 * Transport for the Sandisk SDDR-09
1406 */
1407int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us)
1408{
1409 static unsigned char sensekey = 0, sensecode = 0;
1410 static unsigned char havefakesense = 0;
1411 int result, i;
1412 unsigned char *ptr = us->iobuf;
1413 unsigned long capacity;
1414 unsigned int page, pages;
1415
1416 struct sddr09_card_info *info;
1417
1418 static unsigned char inquiry_response[8] = {
1419 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
1420 };
1421
1422 /* note: no block descriptor support */
1423 static unsigned char mode_page_01[19] = {
1424 0x00, 0x0F, 0x00, 0x0, 0x0, 0x0, 0x00,
1425 0x01, 0x0A,
1426 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1427 };
1428
1429 info = (struct sddr09_card_info *)us->extra;
1430 if (!info) {
1431 nand_init_ecc();
1432 sddr09_init_card_info(us);
1433 info = (struct sddr09_card_info *)us->extra;
1434 if (!info)
1435 return USB_STOR_TRANSPORT_ERROR;
1436 }
1437
1438 if (srb->cmnd[0] == REQUEST_SENSE && havefakesense) {
1439 /* for a faked command, we have to follow with a faked sense */
1440 memset(ptr, 0, 18);
1441 ptr[0] = 0x70;
1442 ptr[2] = sensekey;
1443 ptr[7] = 11;
1444 ptr[12] = sensecode;
1445 usb_stor_set_xfer_buf(ptr, 18, srb);
1446 sensekey = sensecode = havefakesense = 0;
1447 return USB_STOR_TRANSPORT_GOOD;
1448 }
1449
1450 havefakesense = 1;
1451
1452 /* Dummy up a response for INQUIRY since SDDR09 doesn't
1453 respond to INQUIRY commands */
1454
1455 if (srb->cmnd[0] == INQUIRY) {
1456 memcpy(ptr, inquiry_response, 8);
1457 fill_inquiry_response(us, ptr, 36);
1458 return USB_STOR_TRANSPORT_GOOD;
1459 }
1460
1461 if (srb->cmnd[0] == READ_CAPACITY) {
1462 struct nand_flash_dev *cardinfo;
1463
1464 sddr09_get_wp(us, info); /* read WP bit */
1465
1466 cardinfo = sddr09_get_cardinfo(us, info->flags);
1467 if (!cardinfo) {
1468 /* probably no media */
1469 init_error:
1470 sensekey = 0x02; /* not ready */
1471 sensecode = 0x3a; /* medium not present */
1472 return USB_STOR_TRANSPORT_FAILED;
1473 }
1474
1475 info->capacity = (1 << cardinfo->chipshift);
1476 info->pageshift = cardinfo->pageshift;
1477 info->pagesize = (1 << info->pageshift);
1478 info->blockshift = cardinfo->blockshift;
1479 info->blocksize = (1 << info->blockshift);
1480 info->blockmask = info->blocksize - 1;
1481
1482 // map initialization, must follow get_cardinfo()
1483 if (sddr09_read_map(us)) {
1484 /* probably out of memory */
1485 goto init_error;
1486 }
1487
1488 // Report capacity
1489
1490 capacity = (info->lbact << info->blockshift) - 1;
1491
1492 ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
1493
1494 // Report page size
1495
1496 ((__be32 *) ptr)[1] = cpu_to_be32(info->pagesize);
1497 usb_stor_set_xfer_buf(ptr, 8, srb);
1498
1499 return USB_STOR_TRANSPORT_GOOD;
1500 }
1501
1502 if (srb->cmnd[0] == MODE_SENSE_10) {
1503 int modepage = (srb->cmnd[2] & 0x3F);
1504
1505 /* They ask for the Read/Write error recovery page,
1506 or for all pages. */
1507 /* %% We should check DBD %% */
1508 if (modepage == 0x01 || modepage == 0x3F) {
1509 US_DEBUGP("SDDR09: Dummy up request for "
1510 "mode page 0x%x\n", modepage);
1511
1512 memcpy(ptr, mode_page_01, sizeof(mode_page_01));
1513 ((__be16*)ptr)[0] = cpu_to_be16(sizeof(mode_page_01) - 2);
1514 ptr[3] = (info->flags & SDDR09_WP) ? 0x80 : 0;
1515 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
1516 return USB_STOR_TRANSPORT_GOOD;
1517 }
1518
1519 sensekey = 0x05; /* illegal request */
1520 sensecode = 0x24; /* invalid field in CDB */
1521 return USB_STOR_TRANSPORT_FAILED;
1522 }
1523
1524 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)
1525 return USB_STOR_TRANSPORT_GOOD;
1526
1527 havefakesense = 0;
1528
1529 if (srb->cmnd[0] == READ_10) {
1530
1531 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1532 page <<= 16;
1533 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1534 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1535
1536 US_DEBUGP("READ_10: read page %d pagect %d\n",
1537 page, pages);
1538
1539 return sddr09_read_data(us, page, pages);
1540 }
1541
1542 if (srb->cmnd[0] == WRITE_10) {
1543
1544 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1545 page <<= 16;
1546 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1547 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1548
1549 US_DEBUGP("WRITE_10: write page %d pagect %d\n",
1550 page, pages);
1551
1552 return sddr09_write_data(us, page, pages);
1553 }
1554
1555 /* catch-all for all other commands, except
1556 * pass TEST_UNIT_READY and REQUEST_SENSE through
1557 */
1558 if (srb->cmnd[0] != TEST_UNIT_READY &&
1559 srb->cmnd[0] != REQUEST_SENSE) {
1560 sensekey = 0x05; /* illegal request */
1561 sensecode = 0x20; /* invalid command */
1562 havefakesense = 1;
1563 return USB_STOR_TRANSPORT_FAILED;
1564 }
1565
1566 for (; srb->cmd_len<12; srb->cmd_len++)
1567 srb->cmnd[srb->cmd_len] = 0;
1568
1569 srb->cmnd[1] = LUNBITS;
1570
1571 ptr[0] = 0;
1572 for (i=0; i<12; i++)
1573 sprintf(ptr+strlen(ptr), "%02X ", srb->cmnd[i]);
1574
1575 US_DEBUGP("SDDR09: Send control for command %s\n", ptr);
1576
1577 result = sddr09_send_scsi_command(us, srb->cmnd, 12);
1578 if (result != USB_STOR_TRANSPORT_GOOD) {
1579 US_DEBUGP("sddr09_transport: sddr09_send_scsi_command "
1580 "returns %d\n", result);
1581 return result;
1582 }
1583
1584 if (srb->request_bufflen == 0)
1585 return USB_STOR_TRANSPORT_GOOD;
1586
1587 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1588 srb->sc_data_direction == DMA_FROM_DEVICE) {
1589 unsigned int pipe = (srb->sc_data_direction == DMA_TO_DEVICE)
1590 ? us->send_bulk_pipe : us->recv_bulk_pipe;
1591
1592 US_DEBUGP("SDDR09: %s %d bytes\n",
1593 (srb->sc_data_direction == DMA_TO_DEVICE) ?
1594 "sending" : "receiving",
1595 srb->request_bufflen);
1596
1597 result = usb_stor_bulk_transfer_sg(us, pipe,
1598 srb->request_buffer,
1599 srb->request_bufflen,
1600 srb->use_sg, &srb->resid);
1601
1602 return (result == USB_STOR_XFER_GOOD ?
1603 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
1604 }
1605
1606 return USB_STOR_TRANSPORT_GOOD;
1607}
1608
diff --git a/drivers/usb/storage/sddr09.h b/drivers/usb/storage/sddr09.h
new file mode 100644
index 000000000000..c9d78d6188b1
--- /dev/null
+++ b/drivers/usb/storage/sddr09.h
@@ -0,0 +1,48 @@
1/* Driver for SanDisk SDDR-09 SmartMedia reader
2 * Header File
3 *
4 * $Id: sddr09.h,v 1.5 2000/08/25 00:13:51 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 2000 Robert Baruch (autophile@dol.net)
8 * (c) 2002 Andries Brouwer (aeb@cwi.nl)
9 *
10 * See sddr09.c for more explanation
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#ifndef _USB_SHUTTLE_EUSB_SDDR09_H
28#define _USB_SHUTTLE_EUSB_SDDR09_H
29
30/* Sandisk SDDR-09 stuff */
31
32extern int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
33
34struct sddr09_card_info {
35 unsigned long capacity; /* Size of card in bytes */
36 int pagesize; /* Size of page in bytes */
37 int pageshift; /* log2 of pagesize */
38 int blocksize; /* Size of block in pages */
39 int blockshift; /* log2 of blocksize */
40 int blockmask; /* 2^blockshift - 1 */
41 int *lba_to_pba; /* logical to physical map */
42 int *pba_to_lba; /* physical to logical map */
43 int lbact; /* number of available pages */
44 int flags;
45#define SDDR09_WP 1 /* write protected */
46};
47
48#endif
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
new file mode 100644
index 000000000000..229ca181716b
--- /dev/null
+++ b/drivers/usb/storage/sddr55.c
@@ -0,0 +1,938 @@
1/* Driver for SanDisk SDDR-55 SmartMedia reader
2 *
3 * $Id:$
4 *
5 * SDDR55 driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2002 Simon Munton
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/jiffies.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
33
34#include "usb.h"
35#include "transport.h"
36#include "protocol.h"
37#include "debug.h"
38#include "sddr55.h"
39
40
41#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
42#define LSB_of(s) ((s)&0xFF)
43#define MSB_of(s) ((s)>>8)
44#define PAGESIZE 512
45
46#define set_sense_info(sk, asc, ascq) \
47 do { \
48 info->sense_data[2] = sk; \
49 info->sense_data[12] = asc; \
50 info->sense_data[13] = ascq; \
51 } while (0)
52
53
54struct sddr55_card_info {
55 unsigned long capacity; /* Size of card in bytes */
56 int max_log_blks; /* maximum number of logical blocks */
57 int pageshift; /* log2 of pagesize */
58 int smallpageshift; /* 1 if pagesize == 256 */
59 int blocksize; /* Size of block in pages */
60 int blockshift; /* log2 of blocksize */
61 int blockmask; /* 2^blockshift - 1 */
62 int read_only; /* non zero if card is write protected */
63 int force_read_only; /* non zero if we find a map error*/
64 int *lba_to_pba; /* logical to physical map */
65 int *pba_to_lba; /* physical to logical map */
66 int fatal_error; /* set if we detect something nasty */
67 unsigned long last_access; /* number of jiffies since we last talked to device */
68 unsigned char sense_data[18];
69};
70
71
72#define NOT_ALLOCATED 0xffffffff
73#define BAD_BLOCK 0xffff
74#define CIS_BLOCK 0x400
75#define UNUSED_BLOCK 0x3ff
76
77static int
78sddr55_bulk_transport(struct us_data *us, int direction,
79 unsigned char *data, unsigned int len) {
80 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
81 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
82 us->recv_bulk_pipe : us->send_bulk_pipe;
83
84 if (!len)
85 return USB_STOR_XFER_GOOD;
86 info->last_access = jiffies;
87 return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
88}
89
90/* check if card inserted, if there is, update read_only status
91 * return non zero if no card
92 */
93
94static int sddr55_status(struct us_data *us)
95{
96 int result;
97 unsigned char *command = us->iobuf;
98 unsigned char *status = us->iobuf;
99 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
100
101 /* send command */
102 memset(command, 0, 8);
103 command[5] = 0xB0;
104 command[7] = 0x80;
105 result = sddr55_bulk_transport(us,
106 DMA_TO_DEVICE, command, 8);
107
108 US_DEBUGP("Result for send_command in status %d\n",
109 result);
110
111 if (result != USB_STOR_XFER_GOOD) {
112 set_sense_info (4, 0, 0); /* hardware error */
113 return USB_STOR_TRANSPORT_ERROR;
114 }
115
116 result = sddr55_bulk_transport(us,
117 DMA_FROM_DEVICE, status, 4);
118
119 /* expect to get short transfer if no card fitted */
120 if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
121 /* had a short transfer, no card inserted, free map memory */
122 if (info->lba_to_pba)
123 kfree(info->lba_to_pba);
124 if (info->pba_to_lba)
125 kfree(info->pba_to_lba);
126 info->lba_to_pba = NULL;
127 info->pba_to_lba = NULL;
128
129 info->fatal_error = 0;
130 info->force_read_only = 0;
131
132 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
133 return USB_STOR_TRANSPORT_FAILED;
134 }
135
136 if (result != USB_STOR_XFER_GOOD) {
137 set_sense_info (4, 0, 0); /* hardware error */
138 return USB_STOR_TRANSPORT_FAILED;
139 }
140
141 /* check write protect status */
142 info->read_only = (status[0] & 0x20);
143
144 /* now read status */
145 result = sddr55_bulk_transport(us,
146 DMA_FROM_DEVICE, status, 2);
147
148 if (result != USB_STOR_XFER_GOOD) {
149 set_sense_info (4, 0, 0); /* hardware error */
150 }
151
152 return (result == USB_STOR_XFER_GOOD ?
153 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
154}
155
156
157static int sddr55_read_data(struct us_data *us,
158 unsigned int lba,
159 unsigned int page,
160 unsigned short sectors) {
161
162 int result = USB_STOR_TRANSPORT_GOOD;
163 unsigned char *command = us->iobuf;
164 unsigned char *status = us->iobuf;
165 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
166 unsigned char *buffer;
167
168 unsigned int pba;
169 unsigned long address;
170
171 unsigned short pages;
172 unsigned int len, index, offset;
173
174 // Since we only read in one block at a time, we have to create
175 // a bounce buffer and move the data a piece at a time between the
176 // bounce buffer and the actual transfer buffer.
177
178 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
179 info->smallpageshift) * PAGESIZE;
180 buffer = kmalloc(len, GFP_NOIO);
181 if (buffer == NULL)
182 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
183 index = offset = 0;
184
185 while (sectors>0) {
186
187 /* have we got to end? */
188 if (lba >= info->max_log_blks)
189 break;
190
191 pba = info->lba_to_pba[lba];
192
193 // Read as many sectors as possible in this block
194
195 pages = min((unsigned int) sectors << info->smallpageshift,
196 info->blocksize - page);
197 len = pages << info->pageshift;
198
199 US_DEBUGP("Read %02X pages, from PBA %04X"
200 " (LBA %04X) page %02X\n",
201 pages, pba, lba, page);
202
203 if (pba == NOT_ALLOCATED) {
204 /* no pba for this lba, fill with zeroes */
205 memset (buffer, 0, len);
206 } else {
207
208 address = (pba << info->blockshift) + page;
209
210 command[0] = 0;
211 command[1] = LSB_of(address>>16);
212 command[2] = LSB_of(address>>8);
213 command[3] = LSB_of(address);
214
215 command[4] = 0;
216 command[5] = 0xB0;
217 command[6] = LSB_of(pages << (1 - info->smallpageshift));
218 command[7] = 0x85;
219
220 /* send command */
221 result = sddr55_bulk_transport(us,
222 DMA_TO_DEVICE, command, 8);
223
224 US_DEBUGP("Result for send_command in read_data %d\n",
225 result);
226
227 if (result != USB_STOR_XFER_GOOD) {
228 result = USB_STOR_TRANSPORT_ERROR;
229 goto leave;
230 }
231
232 /* read data */
233 result = sddr55_bulk_transport(us,
234 DMA_FROM_DEVICE, buffer, len);
235
236 if (result != USB_STOR_XFER_GOOD) {
237 result = USB_STOR_TRANSPORT_ERROR;
238 goto leave;
239 }
240
241 /* now read status */
242 result = sddr55_bulk_transport(us,
243 DMA_FROM_DEVICE, status, 2);
244
245 if (result != USB_STOR_XFER_GOOD) {
246 result = USB_STOR_TRANSPORT_ERROR;
247 goto leave;
248 }
249
250 /* check status for error */
251 if (status[0] == 0xff && status[1] == 0x4) {
252 set_sense_info (3, 0x11, 0);
253 result = USB_STOR_TRANSPORT_FAILED;
254 goto leave;
255 }
256 }
257
258 // Store the data in the transfer buffer
259 usb_stor_access_xfer_buf(buffer, len, us->srb,
260 &index, &offset, TO_XFER_BUF);
261
262 page = 0;
263 lba++;
264 sectors -= pages >> info->smallpageshift;
265 }
266
267 result = USB_STOR_TRANSPORT_GOOD;
268
269leave:
270 kfree(buffer);
271
272 return result;
273}
274
275static int sddr55_write_data(struct us_data *us,
276 unsigned int lba,
277 unsigned int page,
278 unsigned short sectors) {
279
280 int result = USB_STOR_TRANSPORT_GOOD;
281 unsigned char *command = us->iobuf;
282 unsigned char *status = us->iobuf;
283 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
284 unsigned char *buffer;
285
286 unsigned int pba;
287 unsigned int new_pba;
288 unsigned long address;
289
290 unsigned short pages;
291 int i;
292 unsigned int len, index, offset;
293
294 /* check if we are allowed to write */
295 if (info->read_only || info->force_read_only) {
296 set_sense_info (7, 0x27, 0); /* read only */
297 return USB_STOR_TRANSPORT_FAILED;
298 }
299
300 // Since we only write one block at a time, we have to create
301 // a bounce buffer and move the data a piece at a time between the
302 // bounce buffer and the actual transfer buffer.
303
304 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
305 info->smallpageshift) * PAGESIZE;
306 buffer = kmalloc(len, GFP_NOIO);
307 if (buffer == NULL)
308 return USB_STOR_TRANSPORT_ERROR;
309 index = offset = 0;
310
311 while (sectors > 0) {
312
313 /* have we got to end? */
314 if (lba >= info->max_log_blks)
315 break;
316
317 pba = info->lba_to_pba[lba];
318
319 // Write as many sectors as possible in this block
320
321 pages = min((unsigned int) sectors << info->smallpageshift,
322 info->blocksize - page);
323 len = pages << info->pageshift;
324
325 // Get the data from the transfer buffer
326 usb_stor_access_xfer_buf(buffer, len, us->srb,
327 &index, &offset, FROM_XFER_BUF);
328
329 US_DEBUGP("Write %02X pages, to PBA %04X"
330 " (LBA %04X) page %02X\n",
331 pages, pba, lba, page);
332
333 command[4] = 0;
334
335 if (pba == NOT_ALLOCATED) {
336 /* no pba allocated for this lba, find a free pba to use */
337
338 int max_pba = (info->max_log_blks / 250 ) * 256;
339 int found_count = 0;
340 int found_pba = -1;
341
342 /* set pba to first block in zone lba is in */
343 pba = (lba / 1000) * 1024;
344
345 US_DEBUGP("No PBA for LBA %04X\n",lba);
346
347 if (max_pba > 1024)
348 max_pba = 1024;
349
350 /*
351 * Scan through the map looking for an unused block
352 * leave 16 unused blocks at start (or as many as
353 * possible) since the sddr55 seems to reuse a used
354 * block when it shouldn't if we don't leave space.
355 */
356 for (i = 0; i < max_pba; i++, pba++) {
357 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
358 found_pba = pba;
359 if (found_count++ > 16)
360 break;
361 }
362 }
363
364 pba = found_pba;
365
366 if (pba == -1) {
367 /* oh dear */
368 US_DEBUGP("Couldn't find unallocated block\n");
369
370 set_sense_info (3, 0x31, 0); /* medium error */
371 result = USB_STOR_TRANSPORT_FAILED;
372 goto leave;
373 }
374
375 US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);
376
377 /* set writing to unallocated block flag */
378 command[4] = 0x40;
379 }
380
381 address = (pba << info->blockshift) + page;
382
383 command[1] = LSB_of(address>>16);
384 command[2] = LSB_of(address>>8);
385 command[3] = LSB_of(address);
386
387 /* set the lba into the command, modulo 1000 */
388 command[0] = LSB_of(lba % 1000);
389 command[6] = MSB_of(lba % 1000);
390
391 command[4] |= LSB_of(pages >> info->smallpageshift);
392 command[5] = 0xB0;
393 command[7] = 0x86;
394
395 /* send command */
396 result = sddr55_bulk_transport(us,
397 DMA_TO_DEVICE, command, 8);
398
399 if (result != USB_STOR_XFER_GOOD) {
400 US_DEBUGP("Result for send_command in write_data %d\n",
401 result);
402
403 /* set_sense_info is superfluous here? */
404 set_sense_info (3, 0x3, 0);/* peripheral write error */
405 result = USB_STOR_TRANSPORT_FAILED;
406 goto leave;
407 }
408
409 /* send the data */
410 result = sddr55_bulk_transport(us,
411 DMA_TO_DEVICE, buffer, len);
412
413 if (result != USB_STOR_XFER_GOOD) {
414 US_DEBUGP("Result for send_data in write_data %d\n",
415 result);
416
417 /* set_sense_info is superfluous here? */
418 set_sense_info (3, 0x3, 0);/* peripheral write error */
419 result = USB_STOR_TRANSPORT_FAILED;
420 goto leave;
421 }
422
423 /* now read status */
424 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
425
426 if (result != USB_STOR_XFER_GOOD) {
427 US_DEBUGP("Result for get_status in write_data %d\n",
428 result);
429
430 /* set_sense_info is superfluous here? */
431 set_sense_info (3, 0x3, 0);/* peripheral write error */
432 result = USB_STOR_TRANSPORT_FAILED;
433 goto leave;
434 }
435
436 new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
437 >> info->blockshift;
438
439 /* check status for error */
440 if (status[0] == 0xff && status[1] == 0x4) {
441 info->pba_to_lba[new_pba] = BAD_BLOCK;
442
443 set_sense_info (3, 0x0c, 0);
444 result = USB_STOR_TRANSPORT_FAILED;
445 goto leave;
446 }
447
448 US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
449 lba, pba, new_pba);
450
451 /* update the lba<->pba maps, note new_pba might be the same as pba */
452 info->lba_to_pba[lba] = new_pba;
453 info->pba_to_lba[pba] = UNUSED_BLOCK;
454
455 /* check that new_pba wasn't already being used */
456 if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
457 printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
458 new_pba, info->pba_to_lba[new_pba]);
459 info->fatal_error = 1;
460 set_sense_info (3, 0x31, 0);
461 result = USB_STOR_TRANSPORT_FAILED;
462 goto leave;
463 }
464
465 /* update the pba<->lba maps for new_pba */
466 info->pba_to_lba[new_pba] = lba % 1000;
467
468 page = 0;
469 lba++;
470 sectors -= pages >> info->smallpageshift;
471 }
472 result = USB_STOR_TRANSPORT_GOOD;
473
474 leave:
475 kfree(buffer);
476 return result;
477}
478
479static int sddr55_read_deviceID(struct us_data *us,
480 unsigned char *manufacturerID,
481 unsigned char *deviceID) {
482
483 int result;
484 unsigned char *command = us->iobuf;
485 unsigned char *content = us->iobuf;
486
487 memset(command, 0, 8);
488 command[5] = 0xB0;
489 command[7] = 0x84;
490 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
491
492 US_DEBUGP("Result of send_control for device ID is %d\n",
493 result);
494
495 if (result != USB_STOR_XFER_GOOD)
496 return USB_STOR_TRANSPORT_ERROR;
497
498 result = sddr55_bulk_transport(us,
499 DMA_FROM_DEVICE, content, 4);
500
501 if (result != USB_STOR_XFER_GOOD)
502 return USB_STOR_TRANSPORT_ERROR;
503
504 *manufacturerID = content[0];
505 *deviceID = content[1];
506
507 if (content[0] != 0xff) {
508 result = sddr55_bulk_transport(us,
509 DMA_FROM_DEVICE, content, 2);
510 }
511
512 return USB_STOR_TRANSPORT_GOOD;
513}
514
515
516int sddr55_reset(struct us_data *us) {
517 return 0;
518}
519
520
521static unsigned long sddr55_get_capacity(struct us_data *us) {
522
523 unsigned char manufacturerID;
524 unsigned char deviceID;
525 int result;
526 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
527
528 US_DEBUGP("Reading capacity...\n");
529
530 result = sddr55_read_deviceID(us,
531 &manufacturerID,
532 &deviceID);
533
534 US_DEBUGP("Result of read_deviceID is %d\n",
535 result);
536
537 if (result != USB_STOR_XFER_GOOD)
538 return 0;
539
540 US_DEBUGP("Device ID = %02X\n", deviceID);
541 US_DEBUGP("Manuf ID = %02X\n", manufacturerID);
542
543 info->pageshift = 9;
544 info->smallpageshift = 0;
545 info->blocksize = 16;
546 info->blockshift = 4;
547 info->blockmask = 15;
548
549 switch (deviceID) {
550
551 case 0x6e: // 1MB
552 case 0xe8:
553 case 0xec:
554 info->pageshift = 8;
555 info->smallpageshift = 1;
556 return 0x00100000;
557
558 case 0xea: // 2MB
559 case 0x64:
560 info->pageshift = 8;
561 info->smallpageshift = 1;
562 case 0x5d: // 5d is a ROM card with pagesize 512.
563 return 0x00200000;
564
565 case 0xe3: // 4MB
566 case 0xe5:
567 case 0x6b:
568 case 0xd5:
569 return 0x00400000;
570
571 case 0xe6: // 8MB
572 case 0xd6:
573 return 0x00800000;
574
575 case 0x73: // 16MB
576 info->blocksize = 32;
577 info->blockshift = 5;
578 info->blockmask = 31;
579 return 0x01000000;
580
581 case 0x75: // 32MB
582 info->blocksize = 32;
583 info->blockshift = 5;
584 info->blockmask = 31;
585 return 0x02000000;
586
587 case 0x76: // 64MB
588 info->blocksize = 32;
589 info->blockshift = 5;
590 info->blockmask = 31;
591 return 0x04000000;
592
593 case 0x79: // 128MB
594 info->blocksize = 32;
595 info->blockshift = 5;
596 info->blockmask = 31;
597 return 0x08000000;
598
599 default: // unknown
600 return 0;
601
602 }
603}
604
605static int sddr55_read_map(struct us_data *us) {
606
607 struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
608 int numblocks;
609 unsigned char *buffer;
610 unsigned char *command = us->iobuf;
611 int i;
612 unsigned short lba;
613 unsigned short max_lba;
614 int result;
615
616 if (!info->capacity)
617 return -1;
618
619 numblocks = info->capacity >> (info->blockshift + info->pageshift);
620
621 buffer = kmalloc( numblocks * 2, GFP_NOIO );
622
623 if (!buffer)
624 return -1;
625
626 memset(command, 0, 8);
627 command[5] = 0xB0;
628 command[6] = numblocks * 2 / 256;
629 command[7] = 0x8A;
630
631 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
632
633 if ( result != USB_STOR_XFER_GOOD) {
634 kfree (buffer);
635 return -1;
636 }
637
638 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
639
640 if ( result != USB_STOR_XFER_GOOD) {
641 kfree (buffer);
642 return -1;
643 }
644
645 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
646
647 if ( result != USB_STOR_XFER_GOOD) {
648 kfree (buffer);
649 return -1;
650 }
651
652 if (info->lba_to_pba)
653 kfree(info->lba_to_pba);
654 if (info->pba_to_lba)
655 kfree(info->pba_to_lba);
656 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
657 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
658
659 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
660 if (info->lba_to_pba != NULL)
661 kfree(info->lba_to_pba);
662 if (info->pba_to_lba != NULL)
663 kfree(info->pba_to_lba);
664 info->lba_to_pba = NULL;
665 info->pba_to_lba = NULL;
666 kfree(buffer);
667 return -1;
668 }
669
670 memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
671 memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
672
673 /* set maximum lba */
674 max_lba = info->max_log_blks;
675 if (max_lba > 1000)
676 max_lba = 1000;
677
678 // Each block is 64 bytes of control data, so block i is located in
679 // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
680
681 for (i=0; i<numblocks; i++) {
682 int zone = i / 1024;
683
684 lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
685
686 /* Every 1024 physical blocks ("zone"), the LBA numbers
687 * go back to zero, but are within a higher
688 * block of LBA's. Also, there is a maximum of
689 * 1000 LBA's per zone. In other words, in PBA
690 * 1024-2047 you will find LBA 0-999 which are
691 * really LBA 1000-1999. Yes, this wastes 24
692 * physical blocks per zone. Go figure.
693 * These devices can have blocks go bad, so there
694 * are 24 spare blocks to use when blocks do go bad.
695 */
696
697 /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
698 * CIS block. (Is this true for cards 8MB or less??)
699 * Record these in the physical to logical map
700 */
701
702 info->pba_to_lba[i] = lba;
703
704 if (lba >= max_lba) {
705 continue;
706 }
707
708 if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
709 !info->force_read_only) {
710 printk("sddr55: map inconsistency at LBA %04X\n", lba + zone * 1000);
711 info->force_read_only = 1;
712 }
713
714 if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
715 US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i);
716
717 info->lba_to_pba[lba + zone * 1000] = i;
718 }
719
720 kfree(buffer);
721 return 0;
722}
723
724
725static void sddr55_card_info_destructor(void *extra) {
726 struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
727
728 if (!extra)
729 return;
730
731 if (info->lba_to_pba)
732 kfree(info->lba_to_pba);
733 if (info->pba_to_lba)
734 kfree(info->pba_to_lba);
735}
736
737
738/*
739 * Transport for the Sandisk SDDR-55
740 */
741int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
742{
743 int result;
744 static unsigned char inquiry_response[8] = {
745 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
746 };
747 // write-protected for now, no block descriptor support
748 static unsigned char mode_page_01[20] = {
749 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
750 0x01, 0x0A,
751 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
752 };
753 unsigned char *ptr = us->iobuf;
754 unsigned long capacity;
755 unsigned int lba;
756 unsigned int pba;
757 unsigned int page;
758 unsigned short pages;
759 struct sddr55_card_info *info;
760
761 if (!us->extra) {
762 us->extra = kmalloc(
763 sizeof(struct sddr55_card_info), GFP_NOIO);
764 if (!us->extra)
765 return USB_STOR_TRANSPORT_ERROR;
766 memset(us->extra, 0, sizeof(struct sddr55_card_info));
767 us->extra_destructor = sddr55_card_info_destructor;
768 }
769
770 info = (struct sddr55_card_info *)(us->extra);
771
772 if (srb->cmnd[0] == REQUEST_SENSE) {
773 US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], info->sense_data[13]);
774
775 memcpy (ptr, info->sense_data, sizeof info->sense_data);
776 ptr[0] = 0x70;
777 ptr[7] = 11;
778 usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
779 memset (info->sense_data, 0, sizeof info->sense_data);
780
781 return USB_STOR_TRANSPORT_GOOD;
782 }
783
784 memset (info->sense_data, 0, sizeof info->sense_data);
785
786 /* Dummy up a response for INQUIRY since SDDR55 doesn't
787 respond to INQUIRY commands */
788
789 if (srb->cmnd[0] == INQUIRY) {
790 memcpy(ptr, inquiry_response, 8);
791 fill_inquiry_response(us, ptr, 36);
792 return USB_STOR_TRANSPORT_GOOD;
793 }
794
795 /* only check card status if the map isn't allocated, ie no card seen yet
796 * or if it's been over half a second since we last accessed it
797 */
798 if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
799
800 /* check to see if a card is fitted */
801 result = sddr55_status (us);
802 if (result) {
803 result = sddr55_status (us);
804 if (!result) {
805 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
806 }
807 return USB_STOR_TRANSPORT_FAILED;
808 }
809 }
810
811 /* if we detected a problem with the map when writing,
812 don't allow any more access */
813 if (info->fatal_error) {
814
815 set_sense_info (3, 0x31, 0);
816 return USB_STOR_TRANSPORT_FAILED;
817 }
818
819 if (srb->cmnd[0] == READ_CAPACITY) {
820
821 capacity = sddr55_get_capacity(us);
822
823 if (!capacity) {
824 set_sense_info (3, 0x30, 0); /* incompatible medium */
825 return USB_STOR_TRANSPORT_FAILED;
826 }
827
828 info->capacity = capacity;
829
830 /* figure out the maximum logical block number, allowing for
831 * the fact that only 250 out of every 256 are used */
832 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
833
834 /* Last page in the card, adjust as we only use 250 out of
835 * every 256 pages */
836 capacity = (capacity / 256) * 250;
837
838 capacity /= PAGESIZE;
839 capacity--;
840
841 ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
842 ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
843 usb_stor_set_xfer_buf(ptr, 8, srb);
844
845 sddr55_read_map(us);
846
847 return USB_STOR_TRANSPORT_GOOD;
848 }
849
850 if (srb->cmnd[0] == MODE_SENSE_10) {
851
852 memcpy(ptr, mode_page_01, sizeof mode_page_01);
853 ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
854 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
855
856 if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
857 US_DEBUGP(
858 "SDDR55: Dummy up request for mode page 1\n");
859 return USB_STOR_TRANSPORT_GOOD;
860
861 } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
862 US_DEBUGP(
863 "SDDR55: Dummy up request for all mode pages\n");
864 return USB_STOR_TRANSPORT_GOOD;
865 }
866
867 set_sense_info (5, 0x24, 0); /* invalid field in command */
868 return USB_STOR_TRANSPORT_FAILED;
869 }
870
871 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
872
873 US_DEBUGP(
874 "SDDR55: %s medium removal. Not that I can do"
875 " anything about it...\n",
876 (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
877
878 return USB_STOR_TRANSPORT_GOOD;
879
880 }
881
882 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
883
884 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
885 page <<= 16;
886 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
887 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
888
889 page <<= info->smallpageshift;
890
891 // convert page to block and page-within-block
892
893 lba = page >> info->blockshift;
894 page = page & info->blockmask;
895
896 // locate physical block corresponding to logical block
897
898 if (lba >= info->max_log_blks) {
899
900 US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
901 "block %04X\n", lba, info->max_log_blks-1);
902
903 set_sense_info (5, 0x24, 0); /* invalid field in command */
904
905 return USB_STOR_TRANSPORT_FAILED;
906 }
907
908 pba = info->lba_to_pba[lba];
909
910 if (srb->cmnd[0] == WRITE_10) {
911 US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
912 " pages %d\n",
913 pba, lba, page, pages);
914
915 return sddr55_write_data(us, lba, page, pages);
916 } else {
917 US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
918 " pages %d\n",
919 pba, lba, page, pages);
920
921 return sddr55_read_data(us, lba, page, pages);
922 }
923 }
924
925
926 if (srb->cmnd[0] == TEST_UNIT_READY) {
927 return USB_STOR_TRANSPORT_GOOD;
928 }
929
930 if (srb->cmnd[0] == START_STOP) {
931 return USB_STOR_TRANSPORT_GOOD;
932 }
933
934 set_sense_info (5, 0x20, 0); /* illegal command */
935
936 return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
937}
938
diff --git a/drivers/usb/storage/sddr55.h b/drivers/usb/storage/sddr55.h
new file mode 100644
index 000000000000..d6bd32f6c9f3
--- /dev/null
+++ b/drivers/usb/storage/sddr55.h
@@ -0,0 +1,34 @@
1/* Driver for SanDisk SDDR-55 SmartMedia reader
2 * Header File
3 *
4 * $Id:$
5 *
6 * Current development and maintenance by:
7 * (c) 2002 Simon Munton
8 *
9 * See sddr55.c for more explanation
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#ifndef _USB_SHUTTLE_EUSB_SDDR55_H
27#define _USB_SHUTTLE_EUSB_SDDR55_H
28
29/* Sandisk SDDR-55 stuff */
30
31extern int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us);
32extern int sddr55_reset(struct us_data *us);
33
34#endif
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
new file mode 100644
index 000000000000..7eff03d9b041
--- /dev/null
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -0,0 +1,1712 @@
1/* Driver for SCM Microsystems USB-ATAPI cable
2 *
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
8 *
9 * Developed with the assistance of:
10 * (c) 2002 Alan Stern <stern@rowland.org>
11 *
12 * Flash support based on earlier work by:
13 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
14 *
15 * Many originally ATAPI devices were slightly modified to meet the USB
16 * market by using some kind of translation from ATAPI to USB on the host,
17 * and the peripheral would translate from USB back to ATAPI.
18 *
19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
20 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
21 * their device under nondisclosure agreement, I have been able to write
22 * this driver for Linux.
23 *
24 * The chip used in the device can also be used for EPP and ISA translation
25 * as well. This driver is only guaranteed to work with the ATAPI
26 * translation.
27 *
28 * See the Kconfig help text for a list of devices known to be supported by
29 * this driver.
30 *
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the
33 * Free Software Foundation; either version 2, or (at your option) any
34 * later version.
35 *
36 * This program is distributed in the hope that it will be useful, but
37 * WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 675 Mass Ave, Cambridge, MA 02139, USA.
44 */
45
46#include <linux/sched.h>
47#include <linux/errno.h>
48#include <linux/slab.h>
49#include <linux/cdrom.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53
54#include "usb.h"
55#include "transport.h"
56#include "protocol.h"
57#include "debug.h"
58#include "shuttle_usbat.h"
59
60#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
61#define LSB_of(s) ((s)&0xFF)
62#define MSB_of(s) ((s)>>8)
63
64static int transferred = 0;
65
66static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
67static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
68
69/*
70 * Convenience function to produce an ATAPI read/write sectors command
71 * Use cmd=0x20 for read, cmd=0x30 for write
72 */
73static void usbat_pack_atapi_sector_cmd(unsigned char *buf,
74 unsigned char thistime,
75 u32 sector, unsigned char cmd)
76{
77 buf[0] = 0;
78 buf[1] = thistime;
79 buf[2] = sector & 0xFF;
80 buf[3] = (sector >> 8) & 0xFF;
81 buf[4] = (sector >> 16) & 0xFF;
82 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
83 buf[6] = cmd;
84}
85
86/*
87 * Convenience function to get the device type (flash or hp8200)
88 */
89static int usbat_get_device_type(struct us_data *us)
90{
91 return ((struct usbat_info*)us->extra)->devicetype;
92}
93
94/*
95 * Read a register from the device
96 */
97static int usbat_read(struct us_data *us,
98 unsigned char access,
99 unsigned char reg,
100 unsigned char *content)
101{
102 return usb_stor_ctrl_transfer(us,
103 us->recv_ctrl_pipe,
104 access | USBAT_CMD_READ_REG,
105 0xC0,
106 (u16)reg,
107 0,
108 content,
109 1);
110}
111
112/*
113 * Write to a register on the device
114 */
115static int usbat_write(struct us_data *us,
116 unsigned char access,
117 unsigned char reg,
118 unsigned char content)
119{
120 return usb_stor_ctrl_transfer(us,
121 us->send_ctrl_pipe,
122 access | USBAT_CMD_WRITE_REG,
123 0x40,
124 short_pack(reg, content),
125 0,
126 NULL,
127 0);
128}
129
130/*
131 * Convenience function to perform a bulk read
132 */
133static int usbat_bulk_read(struct us_data *us,
134 unsigned char *data,
135 unsigned int len)
136{
137 if (len == 0)
138 return USB_STOR_XFER_GOOD;
139
140 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
141 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, data, len, NULL);
142}
143
144/*
145 * Convenience function to perform a bulk write
146 */
147static int usbat_bulk_write(struct us_data *us,
148 unsigned char *data,
149 unsigned int len)
150{
151 if (len == 0)
152 return USB_STOR_XFER_GOOD;
153
154 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
155 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, data, len, NULL);
156}
157
158/*
159 * Some USBAT-specific commands can only be executed over a command transport
160 * This transport allows one (len=8) or two (len=16) vendor-specific commands
161 * to be executed.
162 */
163static int usbat_execute_command(struct us_data *us,
164 unsigned char *commands,
165 unsigned int len)
166{
167 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
168 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
169 commands, len);
170}
171
172/*
173 * Read the status register
174 */
175static int usbat_get_status(struct us_data *us, unsigned char *status)
176{
177 int rc;
178 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
179
180 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
181 return rc;
182}
183
184/*
185 * Check the device status
186 */
187static int usbat_check_status(struct us_data *us)
188{
189 unsigned char *reply = us->iobuf;
190 int rc;
191
192 if (!us)
193 return USB_STOR_TRANSPORT_ERROR;
194
195 rc = usbat_get_status(us, reply);
196 if (rc != USB_STOR_XFER_GOOD)
197 return USB_STOR_TRANSPORT_FAILED;
198
199 if (*reply & 0x01 && *reply != 0x51) // error/check condition (0x51 is ok)
200 return USB_STOR_TRANSPORT_FAILED;
201
202 if (*reply & 0x20) // device fault
203 return USB_STOR_TRANSPORT_FAILED;
204
205 return USB_STOR_TRANSPORT_GOOD;
206}
207
208/*
209 * Stores critical information in internal registers in prepartion for the execution
210 * of a conditional usbat_read_blocks or usbat_write_blocks call.
211 */
212static int usbat_set_shuttle_features(struct us_data *us,
213 unsigned char external_trigger,
214 unsigned char epp_control,
215 unsigned char mask_byte,
216 unsigned char test_pattern,
217 unsigned char subcountH,
218 unsigned char subcountL)
219{
220 unsigned char *command = us->iobuf;
221
222 command[0] = 0x40;
223 command[1] = USBAT_CMD_SET_FEAT;
224
225 // The only bit relevant to ATA access is bit 6
226 // which defines 8 bit data access (set) or 16 bit (unset)
227 command[2] = epp_control;
228
229 // If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
230 // ET1 and ET2 define an external event to be checked for on event of a
231 // _read_blocks or _write_blocks operation. The read/write will not take
232 // place unless the defined trigger signal is active.
233 command[3] = external_trigger;
234
235 // The resultant byte of the mask operation (see mask_byte) is compared for
236 // equivalence with this test pattern. If equal, the read/write will take
237 // place.
238 command[4] = test_pattern;
239
240 // This value is logically ANDed with the status register field specified
241 // in the read/write command.
242 command[5] = mask_byte;
243
244 // If ALQ is set in the qualifier, this field contains the address of the
245 // registers where the byte count should be read for transferring the data.
246 // If ALQ is not set, then this field contains the number of bytes to be
247 // transferred.
248 command[6] = subcountL;
249 command[7] = subcountH;
250
251 return usbat_execute_command(us, command, 8);
252}
253
254/*
255 * Block, waiting for an ATA device to become not busy or to report
256 * an error condition.
257 */
258static int usbat_wait_not_busy(struct us_data *us, int minutes)
259{
260 int i;
261 int result;
262 unsigned char *status = us->iobuf;
263
264 /* Synchronizing cache on a CDR could take a heck of a long time,
265 * but probably not more than 10 minutes or so. On the other hand,
266 * doing a full blank on a CDRW at speed 1 will take about 75
267 * minutes!
268 */
269
270 for (i=0; i<1200+minutes*60; i++) {
271
272 result = usbat_get_status(us, status);
273
274 if (result!=USB_STOR_XFER_GOOD)
275 return USB_STOR_TRANSPORT_ERROR;
276 if (*status & 0x01) { // check condition
277 result = usbat_read(us, USBAT_ATA, 0x10, status);
278 return USB_STOR_TRANSPORT_FAILED;
279 }
280 if (*status & 0x20) // device fault
281 return USB_STOR_TRANSPORT_FAILED;
282
283 if ((*status & 0x80)==0x00) { // not busy
284 US_DEBUGP("Waited not busy for %d steps\n", i);
285 return USB_STOR_TRANSPORT_GOOD;
286 }
287
288 if (i<500)
289 msleep(10); // 5 seconds
290 else if (i<700)
291 msleep(50); // 10 seconds
292 else if (i<1200)
293 msleep(100); // 50 seconds
294 else
295 msleep(1000); // X minutes
296 }
297
298 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
299 minutes);
300 return USB_STOR_TRANSPORT_FAILED;
301}
302
303/*
304 * Read block data from the data register
305 */
306static int usbat_read_block(struct us_data *us,
307 unsigned char *content,
308 unsigned short len)
309{
310 int result;
311 unsigned char *command = us->iobuf;
312
313 if (!len)
314 return USB_STOR_TRANSPORT_GOOD;
315
316 command[0] = 0xC0;
317 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
318 command[2] = USBAT_ATA_DATA;
319 command[3] = 0;
320 command[4] = 0;
321 command[5] = 0;
322 command[6] = LSB_of(len);
323 command[7] = MSB_of(len);
324
325 result = usbat_execute_command(us, command, 8);
326 if (result != USB_STOR_XFER_GOOD)
327 return USB_STOR_TRANSPORT_ERROR;
328
329 result = usbat_bulk_read(us, content, len);
330 return (result == USB_STOR_XFER_GOOD ?
331 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
332}
333
334/*
335 * Write block data via the data register
336 */
337static int usbat_write_block(struct us_data *us,
338 unsigned char access,
339 unsigned char *content,
340 unsigned short len,
341 int minutes)
342{
343 int result;
344 unsigned char *command = us->iobuf;
345
346 if (!len)
347 return USB_STOR_TRANSPORT_GOOD;
348
349 command[0] = 0x40;
350 command[1] = access | USBAT_CMD_WRITE_BLOCK;
351 command[2] = USBAT_ATA_DATA;
352 command[3] = 0;
353 command[4] = 0;
354 command[5] = 0;
355 command[6] = LSB_of(len);
356 command[7] = MSB_of(len);
357
358 result = usbat_execute_command(us, command, 8);
359
360 if (result != USB_STOR_XFER_GOOD)
361 return USB_STOR_TRANSPORT_ERROR;
362
363 result = usbat_bulk_write(us, content, len);
364 if (result != USB_STOR_XFER_GOOD)
365 return USB_STOR_TRANSPORT_ERROR;
366
367 return usbat_wait_not_busy(us, minutes);
368}
369
370/*
371 * Process read and write requests
372 */
373static int usbat_hp8200e_rw_block_test(struct us_data *us,
374 unsigned char access,
375 unsigned char *registers,
376 unsigned char *data_out,
377 unsigned short num_registers,
378 unsigned char data_reg,
379 unsigned char status_reg,
380 unsigned char timeout,
381 unsigned char qualifier,
382 int direction,
383 unsigned char *content,
384 unsigned short len,
385 int use_sg,
386 int minutes)
387{
388 int result;
389 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
390 us->recv_bulk_pipe : us->send_bulk_pipe;
391
392 unsigned char *command = us->iobuf;
393 int i, j;
394 int cmdlen;
395 unsigned char *data = us->iobuf;
396 unsigned char *status = us->iobuf;
397
398 BUG_ON(num_registers > US_IOBUF_SIZE/2);
399
400 for (i=0; i<20; i++) {
401
402 /*
403 * The first time we send the full command, which consists
404 * of downloading the SCSI command followed by downloading
405 * the data via a write-and-test. Any other time we only
406 * send the command to download the data -- the SCSI command
407 * is still 'active' in some sense in the device.
408 *
409 * We're only going to try sending the data 10 times. After
410 * that, we just return a failure.
411 */
412
413 if (i==0) {
414 cmdlen = 16;
415 // Write to multiple registers
416 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
417 // but that's what came out of the trace every single time.
418 command[0] = 0x40;
419 command[1] = access | USBAT_CMD_WRITE_REGS;
420 command[2] = 0x07;
421 command[3] = 0x17;
422 command[4] = 0xFC;
423 command[5] = 0xE7;
424 command[6] = LSB_of(num_registers*2);
425 command[7] = MSB_of(num_registers*2);
426 } else
427 cmdlen = 8;
428
429 // Conditionally read or write blocks
430 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
431 command[cmdlen-7] = access |
432 (direction==DMA_TO_DEVICE ?
433 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
434 command[cmdlen-6] = data_reg;
435 command[cmdlen-5] = status_reg;
436 command[cmdlen-4] = timeout;
437 command[cmdlen-3] = qualifier;
438 command[cmdlen-2] = LSB_of(len);
439 command[cmdlen-1] = MSB_of(len);
440
441 result = usbat_execute_command(us, command, cmdlen);
442
443 if (result != USB_STOR_XFER_GOOD)
444 return USB_STOR_TRANSPORT_ERROR;
445
446 if (i==0) {
447
448 for (j=0; j<num_registers; j++) {
449 data[j<<1] = registers[j];
450 data[1+(j<<1)] = data_out[j];
451 }
452
453 result = usbat_bulk_write(us, data, num_registers*2);
454 if (result != USB_STOR_XFER_GOOD)
455 return USB_STOR_TRANSPORT_ERROR;
456
457 }
458
459
460 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
461 // direction == DMA_TO_DEVICE ? "out" : "in",
462 // len, use_sg);
463
464 result = usb_stor_bulk_transfer_sg(us,
465 pipe, content, len, use_sg, NULL);
466
467 /*
468 * If we get a stall on the bulk download, we'll retry
469 * the bulk download -- but not the SCSI command because
470 * in some sense the SCSI command is still 'active' and
471 * waiting for the data. Don't ask me why this should be;
472 * I'm only following what the Windoze driver did.
473 *
474 * Note that a stall for the test-and-read/write command means
475 * that the test failed. In this case we're testing to make
476 * sure that the device is error-free
477 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
478 * hypothesis is that the USBAT chip somehow knows what
479 * the device will accept, but doesn't give the device any
480 * data until all data is received. Thus, the device would
481 * still be waiting for the first byte of data if a stall
482 * occurs, even if the stall implies that some data was
483 * transferred.
484 */
485
486 if (result == USB_STOR_XFER_SHORT ||
487 result == USB_STOR_XFER_STALLED) {
488
489 /*
490 * If we're reading and we stalled, then clear
491 * the bulk output pipe only the first time.
492 */
493
494 if (direction==DMA_FROM_DEVICE && i==0) {
495 if (usb_stor_clear_halt(us,
496 us->send_bulk_pipe) < 0)
497 return USB_STOR_TRANSPORT_ERROR;
498 }
499
500 /*
501 * Read status: is the device angry, or just busy?
502 */
503
504 result = usbat_read(us, USBAT_ATA,
505 direction==DMA_TO_DEVICE ?
506 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
507 status);
508
509 if (result!=USB_STOR_XFER_GOOD)
510 return USB_STOR_TRANSPORT_ERROR;
511 if (*status & 0x01) // check condition
512 return USB_STOR_TRANSPORT_FAILED;
513 if (*status & 0x20) // device fault
514 return USB_STOR_TRANSPORT_FAILED;
515
516 US_DEBUGP("Redoing %s\n",
517 direction==DMA_TO_DEVICE ? "write" : "read");
518
519 } else if (result != USB_STOR_XFER_GOOD)
520 return USB_STOR_TRANSPORT_ERROR;
521 else
522 return usbat_wait_not_busy(us, minutes);
523
524 }
525
526 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
527 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
528
529 return USB_STOR_TRANSPORT_FAILED;
530}
531
532/*
533 * Write to multiple registers:
534 * Allows us to write specific data to any registers. The data to be written
535 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
536 * which gets sent through bulk out.
537 * Not designed for large transfers of data!
538 */
539static int usbat_multiple_write(struct us_data *us,
540 unsigned char *registers,
541 unsigned char *data_out,
542 unsigned short num_registers)
543{
544 int i, result;
545 unsigned char *data = us->iobuf;
546 unsigned char *command = us->iobuf;
547
548 BUG_ON(num_registers > US_IOBUF_SIZE/2);
549
550 // Write to multiple registers, ATA access
551 command[0] = 0x40;
552 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
553
554 // No relevance
555 command[2] = 0;
556 command[3] = 0;
557 command[4] = 0;
558 command[5] = 0;
559
560 // Number of bytes to be transferred (incl. addresses and data)
561 command[6] = LSB_of(num_registers*2);
562 command[7] = MSB_of(num_registers*2);
563
564 // The setup command
565 result = usbat_execute_command(us, command, 8);
566 if (result != USB_STOR_XFER_GOOD)
567 return USB_STOR_TRANSPORT_ERROR;
568
569 // Create the reg/data, reg/data sequence
570 for (i=0; i<num_registers; i++) {
571 data[i<<1] = registers[i];
572 data[1+(i<<1)] = data_out[i];
573 }
574
575 // Send the data
576 result = usbat_bulk_write(us, data, num_registers*2);
577 if (result != USB_STOR_XFER_GOOD)
578 return USB_STOR_TRANSPORT_ERROR;
579
580 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
581 return usbat_wait_not_busy(us, 0);
582 else
583 return USB_STOR_TRANSPORT_GOOD;
584}
585
586/*
587 * Conditionally read blocks from device:
588 * Allows us to read blocks from a specific data register, based upon the
589 * condition that a status register can be successfully masked with a status
590 * qualifier. If this condition is not initially met, the read will wait
591 * up until a maximum amount of time has elapsed, as specified by timeout.
592 * The read will start when the condition is met, otherwise the command aborts.
593 *
594 * The qualifier defined here is not the value that is masked, it defines
595 * conditions for the write to take place. The actual masked qualifier (and
596 * other related details) are defined beforehand with _set_shuttle_features().
597 */
598static int usbat_read_blocks(struct us_data *us,
599 unsigned char *buffer,
600 int len)
601{
602 int result;
603 unsigned char *command = us->iobuf;
604
605 command[0] = 0xC0;
606 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
607 command[2] = USBAT_ATA_DATA;
608 command[3] = USBAT_ATA_STATUS;
609 command[4] = 0xFD; // Timeout (ms);
610 command[5] = USBAT_QUAL_FCQ;
611 command[6] = LSB_of(len);
612 command[7] = MSB_of(len);
613
614 // Multiple block read setup command
615 result = usbat_execute_command(us, command, 8);
616 if (result != USB_STOR_XFER_GOOD)
617 return USB_STOR_TRANSPORT_FAILED;
618
619 // Read the blocks we just asked for
620 result = usbat_bulk_read(us, buffer, len);
621 if (result != USB_STOR_XFER_GOOD)
622 return USB_STOR_TRANSPORT_FAILED;
623
624 return USB_STOR_TRANSPORT_GOOD;
625}
626
627/*
628 * Conditionally write blocks to device:
629 * Allows us to write blocks to a specific data register, based upon the
630 * condition that a status register can be successfully masked with a status
631 * qualifier. If this condition is not initially met, the write will wait
632 * up until a maximum amount of time has elapsed, as specified by timeout.
633 * The read will start when the condition is met, otherwise the command aborts.
634 *
635 * The qualifier defined here is not the value that is masked, it defines
636 * conditions for the write to take place. The actual masked qualifier (and
637 * other related details) are defined beforehand with _set_shuttle_features().
638 */
639static int usbat_write_blocks(struct us_data *us,
640 unsigned char *buffer,
641 int len)
642{
643 int result;
644 unsigned char *command = us->iobuf;
645
646 command[0] = 0x40;
647 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
648 command[2] = USBAT_ATA_DATA;
649 command[3] = USBAT_ATA_STATUS;
650 command[4] = 0xFD; // Timeout (ms)
651 command[5] = USBAT_QUAL_FCQ;
652 command[6] = LSB_of(len);
653 command[7] = MSB_of(len);
654
655 // Multiple block write setup command
656 result = usbat_execute_command(us, command, 8);
657 if (result != USB_STOR_XFER_GOOD)
658 return USB_STOR_TRANSPORT_FAILED;
659
660 // Write the data
661 result = usbat_bulk_write(us, buffer, len);
662 if (result != USB_STOR_XFER_GOOD)
663 return USB_STOR_TRANSPORT_FAILED;
664
665 return USB_STOR_TRANSPORT_GOOD;
666}
667
668/*
669 * Read the User IO register
670 */
671static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
672{
673 int result;
674
675 result = usb_stor_ctrl_transfer(us,
676 us->recv_ctrl_pipe,
677 USBAT_CMD_UIO,
678 0xC0,
679 0,
680 0,
681 data_flags,
682 USBAT_UIO_READ);
683
684 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
685
686 return result;
687}
688
689/*
690 * Write to the User IO register
691 */
692static int usbat_write_user_io(struct us_data *us,
693 unsigned char enable_flags,
694 unsigned char data_flags)
695{
696 return usb_stor_ctrl_transfer(us,
697 us->send_ctrl_pipe,
698 USBAT_CMD_UIO,
699 0x40,
700 short_pack(enable_flags, data_flags),
701 0,
702 NULL,
703 USBAT_UIO_WRITE);
704}
705
706/*
707 * Reset the device
708 * Often needed on media change.
709 */
710static int usbat_device_reset(struct us_data *us)
711{
712 int rc;
713
714 // Reset peripheral, enable peripheral control signals
715 // (bring reset signal up)
716 rc = usbat_write_user_io(us,
717 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
718 USBAT_UIO_EPAD | USBAT_UIO_1);
719 if (rc != USB_STOR_XFER_GOOD)
720 return USB_STOR_TRANSPORT_ERROR;
721
722 // Enable peripheral control signals
723 // (bring reset signal down)
724 rc = usbat_write_user_io(us,
725 USBAT_UIO_OE1 | USBAT_UIO_OE0,
726 USBAT_UIO_EPAD | USBAT_UIO_1);
727 if (rc != USB_STOR_XFER_GOOD)
728 return USB_STOR_TRANSPORT_ERROR;
729
730 return USB_STOR_TRANSPORT_GOOD;
731}
732
733/*
734 * Enable card detect
735 */
736static int usbat_device_enable_cdt(struct us_data *us)
737{
738 int rc;
739
740 // Enable peripheral control signals and card detect
741 rc = usbat_write_user_io(us,
742 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
743 USBAT_UIO_EPAD | USBAT_UIO_1);
744 if (rc != USB_STOR_XFER_GOOD)
745 return USB_STOR_TRANSPORT_ERROR;
746
747 return USB_STOR_TRANSPORT_GOOD;
748}
749
750/*
751 * Determine if media is present.
752 */
753static int usbat_flash_check_media_present(unsigned char *uio)
754{
755 if (*uio & USBAT_UIO_UI0) {
756 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
757 return USBAT_FLASH_MEDIA_NONE;
758 }
759
760 return USBAT_FLASH_MEDIA_CF;
761}
762
763/*
764 * Determine if media has changed since last operation
765 */
766static int usbat_flash_check_media_changed(unsigned char *uio)
767{
768 if (*uio & USBAT_UIO_0) {
769 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
770 return USBAT_FLASH_MEDIA_CHANGED;
771 }
772
773 return USBAT_FLASH_MEDIA_SAME;
774}
775
776/*
777 * Check for media change / no media and handle the situation appropriately
778 */
779static int usbat_flash_check_media(struct us_data *us,
780 struct usbat_info *info)
781{
782 int rc;
783 unsigned char *uio = us->iobuf;
784
785 rc = usbat_read_user_io(us, uio);
786 if (rc != USB_STOR_XFER_GOOD)
787 return USB_STOR_TRANSPORT_ERROR;
788
789 // Check for media existance
790 rc = usbat_flash_check_media_present(uio);
791 if (rc == USBAT_FLASH_MEDIA_NONE) {
792 info->sense_key = 0x02;
793 info->sense_asc = 0x3A;
794 info->sense_ascq = 0x00;
795 return USB_STOR_TRANSPORT_FAILED;
796 }
797
798 // Check for media change
799 rc = usbat_flash_check_media_changed(uio);
800 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
801
802 // Reset and re-enable card detect
803 rc = usbat_device_reset(us);
804 if (rc != USB_STOR_TRANSPORT_GOOD)
805 return rc;
806 rc = usbat_device_enable_cdt(us);
807 if (rc != USB_STOR_TRANSPORT_GOOD)
808 return rc;
809
810 msleep(50);
811
812 rc = usbat_read_user_io(us, uio);
813 if (rc != USB_STOR_XFER_GOOD)
814 return USB_STOR_TRANSPORT_ERROR;
815
816 info->sense_key = UNIT_ATTENTION;
817 info->sense_asc = 0x28;
818 info->sense_ascq = 0x00;
819 return USB_STOR_TRANSPORT_FAILED;
820 }
821
822 return USB_STOR_TRANSPORT_GOOD;
823}
824
825/*
826 * Determine whether we are controlling a flash-based reader/writer,
827 * or a HP8200-based CD drive.
828 * Sets transport functions as appropriate.
829 */
830static int usbat_identify_device(struct us_data *us,
831 struct usbat_info *info)
832{
833 int rc;
834 unsigned char status;
835
836 if (!us || !info)
837 return USB_STOR_TRANSPORT_ERROR;
838
839 rc = usbat_device_reset(us);
840 if (rc != USB_STOR_TRANSPORT_GOOD)
841 return rc;
842
843 /*
844 * By examining the device signature after a reset, we can identify
845 * whether the device supports the ATAPI packet interface.
846 * The flash-devices do not support this, whereas the HP CDRW's obviously
847 * do.
848 *
849 * This method is not ideal, but works because no other devices have been
850 * produced based on the USBAT/USBAT02.
851 *
852 * Section 9.1 of the ATAPI-4 spec states (amongst other things) that
853 * after a device reset, a Cylinder low of 0x14 indicates that the device
854 * does support packet commands.
855 */
856 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, &status);
857 if (rc != USB_STOR_XFER_GOOD)
858 return USB_STOR_TRANSPORT_ERROR;
859
860 US_DEBUGP("usbat_identify_device: Cylinder low is %02X\n", status);
861
862 if (status == 0x14) {
863 // Device is HP 8200
864 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
865 info->devicetype = USBAT_DEV_HP8200;
866 } else {
867 // Device is a CompactFlash reader/writer
868 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
869 info->devicetype = USBAT_DEV_FLASH;
870 }
871
872 return USB_STOR_TRANSPORT_GOOD;
873}
874
875/*
876 * Set the transport function based on the device type
877 */
878static int usbat_set_transport(struct us_data *us,
879 struct usbat_info *info)
880{
881 int rc;
882
883 if (!info->devicetype) {
884 rc = usbat_identify_device(us, info);
885 if (rc != USB_STOR_TRANSPORT_GOOD) {
886 US_DEBUGP("usbat_set_transport: Could not identify device\n");
887 return 1;
888 }
889 }
890
891 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
892 us->transport = usbat_hp8200e_transport;
893 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
894 us->transport = usbat_flash_transport;
895
896 return 0;
897}
898
899/*
900 * Read the media capacity
901 */
902static int usbat_flash_get_sector_count(struct us_data *us,
903 struct usbat_info *info)
904{
905 unsigned char registers[3] = {
906 USBAT_ATA_SECCNT,
907 USBAT_ATA_DEVICE,
908 USBAT_ATA_CMD,
909 };
910 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
911 unsigned char *reply;
912 unsigned char status;
913 int rc;
914
915 if (!us || !info)
916 return USB_STOR_TRANSPORT_ERROR;
917
918 reply = kmalloc(512, GFP_NOIO);
919 if (!reply)
920 return USB_STOR_TRANSPORT_ERROR;
921
922 // ATAPI command : IDENTIFY DEVICE
923 rc = usbat_multiple_write(us, registers, command, 3);
924 if (rc != USB_STOR_XFER_GOOD) {
925 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
926 rc = USB_STOR_TRANSPORT_ERROR;
927 goto leave;
928 }
929
930 // Read device status
931 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
932 rc = USB_STOR_TRANSPORT_ERROR;
933 goto leave;
934 }
935
936 msleep(100);
937
938 // Read the device identification data
939 rc = usbat_read_block(us, reply, 512);
940 if (rc != USB_STOR_TRANSPORT_GOOD)
941 goto leave;
942
943 info->sectors = ((u32)(reply[117]) << 24) |
944 ((u32)(reply[116]) << 16) |
945 ((u32)(reply[115]) << 8) |
946 ((u32)(reply[114]) );
947
948 rc = USB_STOR_TRANSPORT_GOOD;
949
950 leave:
951 kfree(reply);
952 return rc;
953}
954
955/*
956 * Read data from device
957 */
958static int usbat_flash_read_data(struct us_data *us,
959 struct usbat_info *info,
960 u32 sector,
961 u32 sectors)
962{
963 unsigned char registers[7] = {
964 USBAT_ATA_FEATURES,
965 USBAT_ATA_SECCNT,
966 USBAT_ATA_SECNUM,
967 USBAT_ATA_LBA_ME,
968 USBAT_ATA_LBA_HI,
969 USBAT_ATA_DEVICE,
970 USBAT_ATA_STATUS,
971 };
972 unsigned char command[7];
973 unsigned char *buffer;
974 unsigned char thistime;
975 unsigned int totallen, alloclen;
976 int len, result;
977 unsigned int sg_idx = 0, sg_offset = 0;
978
979 result = usbat_flash_check_media(us, info);
980 if (result != USB_STOR_TRANSPORT_GOOD)
981 return result;
982
983 // we're working in LBA mode. according to the ATA spec,
984 // we can support up to 28-bit addressing. I don't know if Jumpshot
985 // supports beyond 24-bit addressing. It's kind of hard to test
986 // since it requires > 8GB CF card.
987
988 if (sector > 0x0FFFFFFF)
989 return USB_STOR_TRANSPORT_ERROR;
990
991 totallen = sectors * info->ssize;
992
993 // Since we don't read more than 64 KB at a time, we have to create
994 // a bounce buffer and move the data a piece at a time between the
995 // bounce buffer and the actual transfer buffer.
996
997 alloclen = min(totallen, 65536u);
998 buffer = kmalloc(alloclen, GFP_NOIO);
999 if (buffer == NULL)
1000 return USB_STOR_TRANSPORT_ERROR;
1001
1002 do {
1003 // loop, never allocate or transfer more than 64k at once
1004 // (min(128k, 255*info->ssize) is the real limit)
1005 len = min(totallen, alloclen);
1006 thistime = (len / info->ssize) & 0xff;
1007
1008 // ATAPI command 0x20 (READ SECTORS)
1009 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x20);
1010
1011 // Write/execute ATAPI read command
1012 result = usbat_multiple_write(us, registers, command, 7);
1013 if (result != USB_STOR_TRANSPORT_GOOD)
1014 goto leave;
1015
1016 // Read the data we just requested
1017 result = usbat_read_blocks(us, buffer, len);
1018 if (result != USB_STOR_TRANSPORT_GOOD)
1019 goto leave;
1020
1021 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1022
1023 // Store the data in the transfer buffer
1024 usb_stor_access_xfer_buf(buffer, len, us->srb,
1025 &sg_idx, &sg_offset, TO_XFER_BUF);
1026
1027 sector += thistime;
1028 totallen -= len;
1029 } while (totallen > 0);
1030
1031 kfree(buffer);
1032 return USB_STOR_TRANSPORT_GOOD;
1033
1034leave:
1035 kfree(buffer);
1036 return USB_STOR_TRANSPORT_ERROR;
1037}
1038
1039/*
1040 * Write data to device
1041 */
1042static int usbat_flash_write_data(struct us_data *us,
1043 struct usbat_info *info,
1044 u32 sector,
1045 u32 sectors)
1046{
1047 unsigned char registers[7] = {
1048 USBAT_ATA_FEATURES,
1049 USBAT_ATA_SECCNT,
1050 USBAT_ATA_SECNUM,
1051 USBAT_ATA_LBA_ME,
1052 USBAT_ATA_LBA_HI,
1053 USBAT_ATA_DEVICE,
1054 USBAT_ATA_STATUS,
1055 };
1056 unsigned char command[7];
1057 unsigned char *buffer;
1058 unsigned char thistime;
1059 unsigned int totallen, alloclen;
1060 int len, result;
1061 unsigned int sg_idx = 0, sg_offset = 0;
1062
1063 result = usbat_flash_check_media(us, info);
1064 if (result != USB_STOR_TRANSPORT_GOOD)
1065 return result;
1066
1067 // we're working in LBA mode. according to the ATA spec,
1068 // we can support up to 28-bit addressing. I don't know if Jumpshot
1069 // supports beyond 24-bit addressing. It's kind of hard to test
1070 // since it requires > 8GB CF card.
1071
1072 if (sector > 0x0FFFFFFF)
1073 return USB_STOR_TRANSPORT_ERROR;
1074
1075 totallen = sectors * info->ssize;
1076
1077 // Since we don't write more than 64 KB at a time, we have to create
1078 // a bounce buffer and move the data a piece at a time between the
1079 // bounce buffer and the actual transfer buffer.
1080
1081 alloclen = min(totallen, 65536u);
1082 buffer = kmalloc(alloclen, GFP_NOIO);
1083 if (buffer == NULL)
1084 return USB_STOR_TRANSPORT_ERROR;
1085
1086 do {
1087 // loop, never allocate or transfer more than 64k at once
1088 // (min(128k, 255*info->ssize) is the real limit)
1089 len = min(totallen, alloclen);
1090 thistime = (len / info->ssize) & 0xff;
1091
1092 // Get the data from the transfer buffer
1093 usb_stor_access_xfer_buf(buffer, len, us->srb,
1094 &sg_idx, &sg_offset, FROM_XFER_BUF);
1095
1096 // ATAPI command 0x30 (WRITE SECTORS)
1097 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x30);
1098
1099 // Write/execute ATAPI write command
1100 result = usbat_multiple_write(us, registers, command, 7);
1101 if (result != USB_STOR_TRANSPORT_GOOD)
1102 goto leave;
1103
1104 // Write the data
1105 result = usbat_write_blocks(us, buffer, len);
1106 if (result != USB_STOR_TRANSPORT_GOOD)
1107 goto leave;
1108
1109 sector += thistime;
1110 totallen -= len;
1111 } while (totallen > 0);
1112
1113 kfree(buffer);
1114 return result;
1115
1116leave:
1117 kfree(buffer);
1118 return USB_STOR_TRANSPORT_ERROR;
1119}
1120
1121/*
1122 * Squeeze a potentially huge (> 65535 byte) read10 command into
1123 * a little ( <= 65535 byte) ATAPI pipe
1124 */
1125static int usbat_hp8200e_handle_read10(struct us_data *us,
1126 unsigned char *registers,
1127 unsigned char *data,
1128 struct scsi_cmnd *srb)
1129{
1130 int result = USB_STOR_TRANSPORT_GOOD;
1131 unsigned char *buffer;
1132 unsigned int len;
1133 unsigned int sector;
1134 unsigned int sg_segment = 0;
1135 unsigned int sg_offset = 0;
1136
1137 US_DEBUGP("handle_read10: transfersize %d\n",
1138 srb->transfersize);
1139
1140 if (srb->request_bufflen < 0x10000) {
1141
1142 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1143 registers, data, 19,
1144 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1145 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1146 DMA_FROM_DEVICE,
1147 srb->request_buffer,
1148 srb->request_bufflen, srb->use_sg, 1);
1149
1150 return result;
1151 }
1152
1153 /*
1154 * Since we're requesting more data than we can handle in
1155 * a single read command (max is 64k-1), we will perform
1156 * multiple reads, but each read must be in multiples of
1157 * a sector. Luckily the sector size is in srb->transfersize
1158 * (see linux/drivers/scsi/sr.c).
1159 */
1160
1161 if (data[7+0] == GPCMD_READ_CD) {
1162 len = short_pack(data[7+9], data[7+8]);
1163 len <<= 16;
1164 len |= data[7+7];
1165 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1166 srb->transfersize = srb->request_bufflen/len;
1167 }
1168
1169 if (!srb->transfersize) {
1170 srb->transfersize = 2048; /* A guess */
1171 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1172 srb->transfersize);
1173 }
1174
1175 // Since we only read in one block at a time, we have to create
1176 // a bounce buffer and move the data a piece at a time between the
1177 // bounce buffer and the actual transfer buffer.
1178
1179 len = (65535/srb->transfersize) * srb->transfersize;
1180 US_DEBUGP("Max read is %d bytes\n", len);
1181 len = min(len, srb->request_bufflen);
1182 buffer = kmalloc(len, GFP_NOIO);
1183 if (buffer == NULL) // bloody hell!
1184 return USB_STOR_TRANSPORT_FAILED;
1185 sector = short_pack(data[7+3], data[7+2]);
1186 sector <<= 16;
1187 sector |= short_pack(data[7+5], data[7+4]);
1188 transferred = 0;
1189
1190 sg_segment = 0; // for keeping track of where we are in
1191 sg_offset = 0; // the scatter/gather list
1192
1193 while (transferred != srb->request_bufflen) {
1194
1195 if (len > srb->request_bufflen - transferred)
1196 len = srb->request_bufflen - transferred;
1197
1198 data[3] = len&0xFF; // (cylL) = expected length (L)
1199 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1200
1201 // Fix up the SCSI command sector and num sectors
1202
1203 data[7+2] = MSB_of(sector>>16); // SCSI command sector
1204 data[7+3] = LSB_of(sector>>16);
1205 data[7+4] = MSB_of(sector&0xFFFF);
1206 data[7+5] = LSB_of(sector&0xFFFF);
1207 if (data[7+0] == GPCMD_READ_CD)
1208 data[7+6] = 0;
1209 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
1210 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
1211
1212 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1213 registers, data, 19,
1214 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1215 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1216 DMA_FROM_DEVICE,
1217 buffer,
1218 len, 0, 1);
1219
1220 if (result != USB_STOR_TRANSPORT_GOOD)
1221 break;
1222
1223 // Store the data in the transfer buffer
1224 usb_stor_access_xfer_buf(buffer, len, srb,
1225 &sg_segment, &sg_offset, TO_XFER_BUF);
1226
1227 // Update the amount transferred and the sector number
1228
1229 transferred += len;
1230 sector += len / srb->transfersize;
1231
1232 } // while transferred != srb->request_bufflen
1233
1234 kfree(buffer);
1235 return result;
1236}
1237
1238static int usbat_select_and_test_registers(struct us_data *us)
1239{
1240 int selector;
1241 unsigned char *status = us->iobuf;
1242 unsigned char max_selector = 0xB0;
1243 if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
1244 max_selector = 0xA0;
1245
1246 // try device = master, then device = slave.
1247
1248 for (selector = 0xA0; selector <= max_selector; selector += 0x10) {
1249
1250 if (usbat_get_device_type(us) == USBAT_DEV_HP8200 &&
1251 usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1252 USB_STOR_XFER_GOOD)
1253 return USB_STOR_TRANSPORT_ERROR;
1254
1255 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1256 USB_STOR_XFER_GOOD)
1257 return USB_STOR_TRANSPORT_ERROR;
1258
1259 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1260 USB_STOR_XFER_GOOD)
1261 return USB_STOR_TRANSPORT_ERROR;
1262
1263 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1264 USB_STOR_XFER_GOOD)
1265 return USB_STOR_TRANSPORT_ERROR;
1266
1267 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1268 USB_STOR_XFER_GOOD)
1269 return USB_STOR_TRANSPORT_ERROR;
1270
1271 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1272 USB_STOR_XFER_GOOD)
1273 return USB_STOR_TRANSPORT_ERROR;
1274
1275 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1276 USB_STOR_XFER_GOOD)
1277 return USB_STOR_TRANSPORT_ERROR;
1278
1279 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1280 USB_STOR_XFER_GOOD)
1281 return USB_STOR_TRANSPORT_ERROR;
1282
1283 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1284 USB_STOR_XFER_GOOD)
1285 return USB_STOR_TRANSPORT_ERROR;
1286 }
1287
1288 return USB_STOR_TRANSPORT_GOOD;
1289}
1290
1291/*
1292 * Initialize the USBAT processor and the storage device
1293 */
1294int init_usbat(struct us_data *us)
1295{
1296 int rc;
1297 struct usbat_info *info;
1298 unsigned char subcountH = USBAT_ATA_LBA_HI;
1299 unsigned char subcountL = USBAT_ATA_LBA_ME;
1300 unsigned char *status = us->iobuf;
1301
1302 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
1303 if (!us->extra) {
1304 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1305 return 1;
1306 }
1307 memset(us->extra, 0, sizeof(struct usbat_info));
1308 info = (struct usbat_info *) (us->extra);
1309
1310 // Enable peripheral control signals
1311 rc = usbat_write_user_io(us,
1312 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1313 USBAT_UIO_EPAD | USBAT_UIO_1);
1314 if (rc != USB_STOR_XFER_GOOD)
1315 return USB_STOR_TRANSPORT_ERROR;
1316
1317 US_DEBUGP("INIT 1\n");
1318
1319 msleep(2000);
1320
1321 rc = usbat_read_user_io(us, status);
1322 if (rc != USB_STOR_TRANSPORT_GOOD)
1323 return rc;
1324
1325 US_DEBUGP("INIT 2\n");
1326
1327 rc = usbat_read_user_io(us, status);
1328 if (rc != USB_STOR_XFER_GOOD)
1329 return USB_STOR_TRANSPORT_ERROR;
1330
1331 rc = usbat_read_user_io(us, status);
1332 if (rc != USB_STOR_XFER_GOOD)
1333 return USB_STOR_TRANSPORT_ERROR;
1334
1335 US_DEBUGP("INIT 3\n");
1336
1337 // At this point, we need to detect which device we are using
1338 if (usbat_set_transport(us, info))
1339 return USB_STOR_TRANSPORT_ERROR;
1340
1341 US_DEBUGP("INIT 4\n");
1342
1343 if (usbat_get_device_type(us) == USBAT_DEV_HP8200) {
1344 msleep(250);
1345
1346 // Write 0x80 to ISA port 0x3F
1347 rc = usbat_write(us, USBAT_ISA, 0x3F, 0x80);
1348 if (rc != USB_STOR_XFER_GOOD)
1349 return USB_STOR_TRANSPORT_ERROR;
1350
1351 US_DEBUGP("INIT 5\n");
1352
1353 // Read ISA port 0x27
1354 rc = usbat_read(us, USBAT_ISA, 0x27, status);
1355 if (rc != USB_STOR_XFER_GOOD)
1356 return USB_STOR_TRANSPORT_ERROR;
1357
1358 US_DEBUGP("INIT 6\n");
1359
1360 rc = usbat_read_user_io(us, status);
1361 if (rc != USB_STOR_XFER_GOOD)
1362 return USB_STOR_TRANSPORT_ERROR;
1363
1364 US_DEBUGP("INIT 7\n");
1365 }
1366
1367 rc = usbat_select_and_test_registers(us);
1368 if (rc != USB_STOR_TRANSPORT_GOOD)
1369 return rc;
1370
1371 US_DEBUGP("INIT 8\n");
1372
1373 rc = usbat_read_user_io(us, status);
1374 if (rc != USB_STOR_XFER_GOOD)
1375 return USB_STOR_TRANSPORT_ERROR;
1376
1377 US_DEBUGP("INIT 9\n");
1378
1379 // Enable peripheral control signals and card detect
1380 rc = usbat_device_enable_cdt(us);
1381 if (rc != USB_STOR_TRANSPORT_GOOD)
1382 return rc;
1383
1384 US_DEBUGP("INIT 10\n");
1385
1386 rc = usbat_read_user_io(us, status);
1387 if (rc != USB_STOR_XFER_GOOD)
1388 return USB_STOR_TRANSPORT_ERROR;
1389
1390 US_DEBUGP("INIT 11\n");
1391
1392 msleep(1400);
1393
1394 rc = usbat_read_user_io(us, status);
1395 if (rc != USB_STOR_XFER_GOOD)
1396 return USB_STOR_TRANSPORT_ERROR;
1397
1398 US_DEBUGP("INIT 12\n");
1399
1400 rc = usbat_select_and_test_registers(us);
1401 if (rc != USB_STOR_TRANSPORT_GOOD)
1402 return rc;
1403
1404 US_DEBUGP("INIT 13\n");
1405
1406 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1407 subcountH = 0x02;
1408 subcountL = 0x00;
1409 }
1410 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1411 0x00, 0x88, 0x08, subcountH, subcountL);
1412 if (rc != USB_STOR_XFER_GOOD)
1413 return USB_STOR_TRANSPORT_ERROR;
1414
1415 US_DEBUGP("INIT 14\n");
1416
1417 return USB_STOR_TRANSPORT_GOOD;
1418}
1419
1420/*
1421 * Transport for the HP 8200e
1422 */
1423static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1424{
1425 int result;
1426 unsigned char *status = us->iobuf;
1427 unsigned char registers[32];
1428 unsigned char data[32];
1429 unsigned int len;
1430 int i;
1431 char string[64];
1432
1433 len = srb->request_bufflen;
1434
1435 /* Send A0 (ATA PACKET COMMAND).
1436 Note: I guess we're never going to get any of the ATA
1437 commands... just ATA Packet Commands.
1438 */
1439
1440 registers[0] = USBAT_ATA_FEATURES;
1441 registers[1] = USBAT_ATA_SECCNT;
1442 registers[2] = USBAT_ATA_SECNUM;
1443 registers[3] = USBAT_ATA_LBA_ME;
1444 registers[4] = USBAT_ATA_LBA_HI;
1445 registers[5] = USBAT_ATA_DEVICE;
1446 registers[6] = USBAT_ATA_CMD;
1447 data[0] = 0x00;
1448 data[1] = 0x00;
1449 data[2] = 0x00;
1450 data[3] = len&0xFF; // (cylL) = expected length (L)
1451 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1452 data[5] = 0xB0; // (device sel) = slave
1453 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
1454
1455 for (i=7; i<19; i++) {
1456 registers[i] = 0x10;
1457 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1458 }
1459
1460 result = usbat_get_status(us, status);
1461 US_DEBUGP("Status = %02X\n", *status);
1462 if (result != USB_STOR_XFER_GOOD)
1463 return USB_STOR_TRANSPORT_ERROR;
1464 if (srb->cmnd[0] == TEST_UNIT_READY)
1465 transferred = 0;
1466
1467 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1468
1469 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1470 registers, data, 19,
1471 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1472 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1473 DMA_TO_DEVICE,
1474 srb->request_buffer,
1475 len, srb->use_sg, 10);
1476
1477 if (result == USB_STOR_TRANSPORT_GOOD) {
1478 transferred += len;
1479 US_DEBUGP("Wrote %08X bytes\n", transferred);
1480 }
1481
1482 return result;
1483
1484 } else if (srb->cmnd[0] == READ_10 ||
1485 srb->cmnd[0] == GPCMD_READ_CD) {
1486
1487 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1488
1489 }
1490
1491 if (len > 0xFFFF) {
1492 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1493 len);
1494 return USB_STOR_TRANSPORT_ERROR;
1495 }
1496
1497 if ( (result = usbat_multiple_write(us,
1498 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1499 return result;
1500 }
1501
1502 // Write the 12-byte command header.
1503
1504 // If the command is BLANK then set the timer for 75 minutes.
1505 // Otherwise set it for 10 minutes.
1506
1507 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1508 // AT SPEED 4 IS UNRELIABLE!!!
1509
1510 if ( (result = usbat_write_block(us,
1511 USBAT_ATA, srb->cmnd, 12,
1512 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1513 USB_STOR_TRANSPORT_GOOD) {
1514 return result;
1515 }
1516
1517 // If there is response data to be read in
1518 // then do it here.
1519
1520 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1521
1522 // How many bytes to read in? Check cylL register
1523
1524 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1525 USB_STOR_XFER_GOOD) {
1526 return USB_STOR_TRANSPORT_ERROR;
1527 }
1528
1529 if (len > 0xFF) { // need to read cylH also
1530 len = *status;
1531 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1532 USB_STOR_XFER_GOOD) {
1533 return USB_STOR_TRANSPORT_ERROR;
1534 }
1535 len += ((unsigned int) *status)<<8;
1536 }
1537 else
1538 len = *status;
1539
1540
1541 result = usbat_read_block(us, srb->request_buffer, len);
1542
1543 /* Debug-print the first 32 bytes of the transfer */
1544
1545 if (!srb->use_sg) {
1546 string[0] = 0;
1547 for (i=0; i<len && i<32; i++) {
1548 sprintf(string+strlen(string), "%02X ",
1549 ((unsigned char *)srb->request_buffer)[i]);
1550 if ((i%16)==15) {
1551 US_DEBUGP("%s\n", string);
1552 string[0] = 0;
1553 }
1554 }
1555 if (string[0]!=0)
1556 US_DEBUGP("%s\n", string);
1557 }
1558 }
1559
1560 return result;
1561}
1562
1563/*
1564 * Transport for USBAT02-based CompactFlash and similar storage devices
1565 */
1566static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1567{
1568 int rc;
1569 struct usbat_info *info = (struct usbat_info *) (us->extra);
1570 unsigned long block, blocks;
1571 unsigned char *ptr = us->iobuf;
1572 static unsigned char inquiry_response[36] = {
1573 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1574 };
1575
1576 if (srb->cmnd[0] == INQUIRY) {
1577 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1578 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1579 fill_inquiry_response(us, ptr, 36);
1580 return USB_STOR_TRANSPORT_GOOD;
1581 }
1582
1583 if (srb->cmnd[0] == READ_CAPACITY) {
1584 rc = usbat_flash_check_media(us, info);
1585 if (rc != USB_STOR_TRANSPORT_GOOD)
1586 return rc;
1587
1588 rc = usbat_flash_get_sector_count(us, info);
1589 if (rc != USB_STOR_TRANSPORT_GOOD)
1590 return rc;
1591
1592 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
1593 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1594 info->sectors, info->ssize);
1595
1596 // build the reply
1597 // note: must return the sector number of the last sector,
1598 // *not* the total number of sectors
1599 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1600 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1601 usb_stor_set_xfer_buf(ptr, 8, srb);
1602
1603 return USB_STOR_TRANSPORT_GOOD;
1604 }
1605
1606 if (srb->cmnd[0] == MODE_SELECT_10) {
1607 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1608 return USB_STOR_TRANSPORT_ERROR;
1609 }
1610
1611 if (srb->cmnd[0] == READ_10) {
1612 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1613 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1614
1615 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1616
1617 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1618 return usbat_flash_read_data(us, info, block, blocks);
1619 }
1620
1621 if (srb->cmnd[0] == READ_12) {
1622 // I don't think we'll ever see a READ_12 but support it anyway...
1623 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1624 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1625
1626 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1627 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1628
1629 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1630 return usbat_flash_read_data(us, info, block, blocks);
1631 }
1632
1633 if (srb->cmnd[0] == WRITE_10) {
1634 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1635 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1636
1637 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1638
1639 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1640 return usbat_flash_write_data(us, info, block, blocks);
1641 }
1642
1643 if (srb->cmnd[0] == WRITE_12) {
1644 // I don't think we'll ever see a WRITE_12 but support it anyway...
1645 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1646 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1647
1648 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1649 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1650
1651 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1652 return usbat_flash_write_data(us, info, block, blocks);
1653 }
1654
1655
1656 if (srb->cmnd[0] == TEST_UNIT_READY) {
1657 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1658
1659 rc = usbat_flash_check_media(us, info);
1660 if (rc != USB_STOR_TRANSPORT_GOOD)
1661 return rc;
1662
1663 return usbat_check_status(us);
1664 }
1665
1666 if (srb->cmnd[0] == REQUEST_SENSE) {
1667 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1668
1669 memset(ptr, 0, 18);
1670 ptr[0] = 0xF0;
1671 ptr[2] = info->sense_key;
1672 ptr[7] = 11;
1673 ptr[12] = info->sense_asc;
1674 ptr[13] = info->sense_ascq;
1675 usb_stor_set_xfer_buf(ptr, 18, srb);
1676
1677 return USB_STOR_TRANSPORT_GOOD;
1678 }
1679
1680 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1681 // sure. whatever. not like we can stop the user from popping
1682 // the media out of the device (no locking doors, etc)
1683 return USB_STOR_TRANSPORT_GOOD;
1684 }
1685
1686 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1687 srb->cmnd[0], srb->cmnd[0]);
1688 info->sense_key = 0x05;
1689 info->sense_asc = 0x20;
1690 info->sense_ascq = 0x00;
1691 return USB_STOR_TRANSPORT_FAILED;
1692}
1693
1694/*
1695 * Default transport function. Attempts to detect which transport function
1696 * should be called, makes it the new default, and calls it.
1697 *
1698 * This function should never be called. Our usbat_init() function detects the
1699 * device type and changes the us->transport ptr to the transport function
1700 * relevant to the device.
1701 * However, we'll support this impossible(?) case anyway.
1702 */
1703int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1704{
1705 struct usbat_info *info = (struct usbat_info*) (us->extra);
1706
1707 if (usbat_set_transport(us, info))
1708 return USB_STOR_TRANSPORT_ERROR;
1709
1710 return us->transport(srb, us);
1711}
1712
diff --git a/drivers/usb/storage/shuttle_usbat.h b/drivers/usb/storage/shuttle_usbat.h
new file mode 100644
index 000000000000..5b8e867e2ae5
--- /dev/null
+++ b/drivers/usb/storage/shuttle_usbat.h
@@ -0,0 +1,123 @@
1/* Driver for SCM Microsystems USB-ATAPI cable
2 * Header File
3 *
4 * $Id: shuttle_usbat.h,v 1.5 2000/09/17 14:44:52 groovyjava Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 2000 Robert Baruch (autophile@dol.net)
8 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
9 *
10 * See shuttle_usbat.c for more explanation
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#ifndef _USB_SHUTTLE_USBAT_H
28#define _USB_SHUTTLE_USBAT_H
29
30/* Supported device types */
31#define USBAT_DEV_HP8200 0x01
32#define USBAT_DEV_FLASH 0x02
33
34#define USBAT_EPP_PORT 0x10
35#define USBAT_EPP_REGISTER 0x30
36#define USBAT_ATA 0x40
37#define USBAT_ISA 0x50
38
39/* Commands (need to be logically OR'd with an access type */
40#define USBAT_CMD_READ_REG 0x00
41#define USBAT_CMD_WRITE_REG 0x01
42#define USBAT_CMD_READ_BLOCK 0x02
43#define USBAT_CMD_WRITE_BLOCK 0x03
44#define USBAT_CMD_COND_READ_BLOCK 0x04
45#define USBAT_CMD_COND_WRITE_BLOCK 0x05
46#define USBAT_CMD_WRITE_REGS 0x07
47
48/* Commands (these don't need an access type) */
49#define USBAT_CMD_EXEC_CMD 0x80
50#define USBAT_CMD_SET_FEAT 0x81
51#define USBAT_CMD_UIO 0x82
52
53/* Methods of accessing UIO register */
54#define USBAT_UIO_READ 1
55#define USBAT_UIO_WRITE 0
56
57/* Qualifier bits */
58#define USBAT_QUAL_FCQ 0x20 // full compare
59#define USBAT_QUAL_ALQ 0x10 // auto load subcount
60
61/* USBAT Flash Media status types */
62#define USBAT_FLASH_MEDIA_NONE 0
63#define USBAT_FLASH_MEDIA_CF 1
64
65/* USBAT Flash Media change types */
66#define USBAT_FLASH_MEDIA_SAME 0
67#define USBAT_FLASH_MEDIA_CHANGED 1
68
69/* USBAT ATA registers */
70#define USBAT_ATA_DATA 0x10 // read/write data (R/W)
71#define USBAT_ATA_FEATURES 0x11 // set features (W)
72#define USBAT_ATA_ERROR 0x11 // error (R)
73#define USBAT_ATA_SECCNT 0x12 // sector count (R/W)
74#define USBAT_ATA_SECNUM 0x13 // sector number (R/W)
75#define USBAT_ATA_LBA_ME 0x14 // cylinder low (R/W)
76#define USBAT_ATA_LBA_HI 0x15 // cylinder high (R/W)
77#define USBAT_ATA_DEVICE 0x16 // head/device selection (R/W)
78#define USBAT_ATA_STATUS 0x17 // device status (R)
79#define USBAT_ATA_CMD 0x17 // device command (W)
80#define USBAT_ATA_ALTSTATUS 0x0E // status (no clear IRQ) (R)
81
82/* USBAT User I/O Data registers */
83#define USBAT_UIO_EPAD 0x80 // Enable Peripheral Control Signals
84#define USBAT_UIO_CDT 0x40 // Card Detect (Read Only)
85 // CDT = ACKD & !UI1 & !UI0
86#define USBAT_UIO_1 0x20 // I/O 1
87#define USBAT_UIO_0 0x10 // I/O 0
88#define USBAT_UIO_EPP_ATA 0x08 // 1=EPP mode, 0=ATA mode
89#define USBAT_UIO_UI1 0x04 // Input 1
90#define USBAT_UIO_UI0 0x02 // Input 0
91#define USBAT_UIO_INTR_ACK 0x01 // Interrupt (ATA & ISA)/Acknowledge (EPP)
92
93/* USBAT User I/O Enable registers */
94#define USBAT_UIO_DRVRST 0x80 // Reset Peripheral
95#define USBAT_UIO_ACKD 0x40 // Enable Card Detect
96#define USBAT_UIO_OE1 0x20 // I/O 1 set=output/clr=input
97 // If ACKD=1, set OE1 to 1 also.
98#define USBAT_UIO_OE0 0x10 // I/O 0 set=output/clr=input
99#define USBAT_UIO_ADPRST 0x01 // Reset SCM chip
100
101/* USBAT Features */
102#define USBAT_FEAT_ETEN 0x80 // External trigger enable
103#define USBAT_FEAT_U1 0x08
104#define USBAT_FEAT_U0 0x04
105#define USBAT_FEAT_ET1 0x02
106#define USBAT_FEAT_ET2 0x01
107
108extern int usbat_transport(struct scsi_cmnd *srb, struct us_data *us);
109extern int init_usbat(struct us_data *us);
110
111struct usbat_info {
112 int devicetype;
113
114 /* Used for Flash readers only */
115 unsigned long sectors; // total sector count
116 unsigned long ssize; // sector size in bytes
117
118 unsigned char sense_key;
119 unsigned long sense_asc; // additional sense code
120 unsigned long sense_ascq; // additional sense code qualifier
121};
122
123#endif
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
new file mode 100644
index 000000000000..d2c3d2fa082e
--- /dev/null
+++ b/drivers/usb/storage/transport.c
@@ -0,0 +1,1215 @@
1/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: transport.c,v 1.47 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
11 * (c) 2002 Alan Stern <stern@rowland.org>
12 *
13 * Initial work by:
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 *
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
21 *
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
36 * later version.
37 *
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
48#include <linux/config.h>
49#include <linux/sched.h>
50#include <linux/errno.h>
51#include <linux/slab.h>
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56
57#include "usb.h"
58#include "transport.h"
59#include "protocol.h"
60#include "scsiglue.h"
61#include "debug.h"
62
63
64/***********************************************************************
65 * Data transfer routines
66 ***********************************************************************/
67
68/*
69 * This is subtle, so pay attention:
70 * ---------------------------------
71 * We're very concerned about races with a command abort. Hanging this code
72 * is a sure fire way to hang the kernel. (Note that this discussion applies
73 * only to transactions resulting from a scsi queued-command, since only
74 * these transactions are subject to a scsi abort. Other transactions, such
75 * as those occurring during device-specific initialization, must be handled
76 * by a separate code path.)
77 *
78 * The abort function (usb_storage_command_abort() in scsiglue.c) first
79 * sets the machine state and the ABORTING bit in us->flags to prevent
80 * new URBs from being submitted. It then calls usb_stor_stop_transport()
81 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->flags
82 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
83 * bit is tested to see if the current_sg scatter-gather request needs to be
84 * stopped. The timeout callback routine does much the same thing.
85 *
86 * When a disconnect occurs, the DISCONNECTING bit in us->flags is set to
87 * prevent new URBs from being submitted, and usb_stor_stop_transport() is
88 * called to stop any ongoing requests.
89 *
90 * The submit function first verifies that the submitting is allowed
91 * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
92 * completes without errors, and only then sets the URB_ACTIVE bit. This
93 * prevents the stop_transport() function from trying to cancel the URB
94 * while the submit call is underway. Next, the submit function must test
95 * the flags to see if an abort or disconnect occurred during the submission
96 * or before the URB_ACTIVE bit was set. If so, it's essential to cancel
97 * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
98 * is still set). Either way, the function must then wait for the URB to
99 * finish. Note that because the URB_ASYNC_UNLINK flag is set, the URB can
100 * still be in progress even after a call to usb_unlink_urb() returns.
101 *
102 * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
103 * either the stop_transport() function or the submitting function
104 * is guaranteed to call usb_unlink_urb() for an active URB,
105 * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
106 * called more than once or from being called during usb_submit_urb().
107 */
108
109/* This is the completion handler which will wake us up when an URB
110 * completes.
111 */
112static void usb_stor_blocking_completion(struct urb *urb, struct pt_regs *regs)
113{
114 struct completion *urb_done_ptr = (struct completion *)urb->context;
115
116 complete(urb_done_ptr);
117}
118
119/* This is the timeout handler which will cancel an URB when its timeout
120 * expires.
121 */
122static void timeout_handler(unsigned long us_)
123{
124 struct us_data *us = (struct us_data *) us_;
125
126 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
127 US_DEBUGP("Timeout -- cancelling URB\n");
128 usb_unlink_urb(us->current_urb);
129 }
130}
131
132/* This is the common part of the URB message submission code
133 *
134 * All URBs from the usb-storage driver involved in handling a queued scsi
135 * command _must_ pass through this function (or something like it) for the
136 * abort mechanisms to work properly.
137 */
138static int usb_stor_msg_common(struct us_data *us, int timeout)
139{
140 struct completion urb_done;
141 struct timer_list to_timer;
142 int status;
143
144 /* don't submit URBs during abort/disconnect processing */
145 if (us->flags & ABORTING_OR_DISCONNECTING)
146 return -EIO;
147
148 /* set up data structures for the wakeup system */
149 init_completion(&urb_done);
150
151 /* fill the common fields in the URB */
152 us->current_urb->context = &urb_done;
153 us->current_urb->actual_length = 0;
154 us->current_urb->error_count = 0;
155 us->current_urb->status = 0;
156
157 /* we assume that if transfer_buffer isn't us->iobuf then it
158 * hasn't been mapped for DMA. Yes, this is clunky, but it's
159 * easier than always having the caller tell us whether the
160 * transfer buffer has already been mapped. */
161 us->current_urb->transfer_flags =
162 URB_ASYNC_UNLINK | URB_NO_SETUP_DMA_MAP;
163 if (us->current_urb->transfer_buffer == us->iobuf)
164 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
165 us->current_urb->transfer_dma = us->iobuf_dma;
166 us->current_urb->setup_dma = us->cr_dma;
167
168 /* submit the URB */
169 status = usb_submit_urb(us->current_urb, GFP_NOIO);
170 if (status) {
171 /* something went wrong */
172 return status;
173 }
174
175 /* since the URB has been submitted successfully, it's now okay
176 * to cancel it */
177 set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
178
179 /* did an abort/disconnect occur during the submission? */
180 if (us->flags & ABORTING_OR_DISCONNECTING) {
181
182 /* cancel the URB, if it hasn't been cancelled already */
183 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
184 US_DEBUGP("-- cancelling URB\n");
185 usb_unlink_urb(us->current_urb);
186 }
187 }
188
189 /* submit the timeout timer, if a timeout was requested */
190 if (timeout > 0) {
191 init_timer(&to_timer);
192 to_timer.expires = jiffies + timeout;
193 to_timer.function = timeout_handler;
194 to_timer.data = (unsigned long) us;
195 add_timer(&to_timer);
196 }
197
198 /* wait for the completion of the URB */
199 wait_for_completion(&urb_done);
200 clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
201
202 /* clean up the timeout timer */
203 if (timeout > 0)
204 del_timer_sync(&to_timer);
205
206 /* return the URB status */
207 return us->current_urb->status;
208}
209
210/*
211 * Transfer one control message, with timeouts, and allowing early
212 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
213 */
214int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
215 u8 request, u8 requesttype, u16 value, u16 index,
216 void *data, u16 size, int timeout)
217{
218 int status;
219
220 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
221 __FUNCTION__, request, requesttype,
222 value, index, size);
223
224 /* fill in the devrequest structure */
225 us->cr->bRequestType = requesttype;
226 us->cr->bRequest = request;
227 us->cr->wValue = cpu_to_le16(value);
228 us->cr->wIndex = cpu_to_le16(index);
229 us->cr->wLength = cpu_to_le16(size);
230
231 /* fill and submit the URB */
232 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
233 (unsigned char*) us->cr, data, size,
234 usb_stor_blocking_completion, NULL);
235 status = usb_stor_msg_common(us, timeout);
236
237 /* return the actual length of the data transferred if no error */
238 if (status == 0)
239 status = us->current_urb->actual_length;
240 return status;
241}
242
243/* This is a version of usb_clear_halt() that allows early termination and
244 * doesn't read the status from the device -- this is because some devices
245 * crash their internal firmware when the status is requested after a halt.
246 *
247 * A definitive list of these 'bad' devices is too difficult to maintain or
248 * make complete enough to be useful. This problem was first observed on the
249 * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
250 * MacOS nor Windows checks the status after clearing a halt.
251 *
252 * Since many vendors in this space limit their testing to interoperability
253 * with these two OSes, specification violations like this one are common.
254 */
255int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
256{
257 int result;
258 int endp = usb_pipeendpoint(pipe);
259
260 if (usb_pipein (pipe))
261 endp |= USB_DIR_IN;
262
263 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
264 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
265 USB_ENDPOINT_HALT, endp,
266 NULL, 0, 3*HZ);
267
268 /* reset the endpoint toggle */
269 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
270 usb_pipeout(pipe), 0);
271
272 US_DEBUGP("%s: result = %d\n", __FUNCTION__, result);
273 return result;
274}
275
276
277/*
278 * Interpret the results of a URB transfer
279 *
280 * This function prints appropriate debugging messages, clears halts on
281 * non-control endpoints, and translates the status to the corresponding
282 * USB_STOR_XFER_xxx return code.
283 */
284static int interpret_urb_result(struct us_data *us, unsigned int pipe,
285 unsigned int length, int result, unsigned int partial)
286{
287 US_DEBUGP("Status code %d; transferred %u/%u\n",
288 result, partial, length);
289 switch (result) {
290
291 /* no error code; did we send all the data? */
292 case 0:
293 if (partial != length) {
294 US_DEBUGP("-- short transfer\n");
295 return USB_STOR_XFER_SHORT;
296 }
297
298 US_DEBUGP("-- transfer complete\n");
299 return USB_STOR_XFER_GOOD;
300
301 /* stalled */
302 case -EPIPE:
303 /* for control endpoints, (used by CB[I]) a stall indicates
304 * a failed command */
305 if (usb_pipecontrol(pipe)) {
306 US_DEBUGP("-- stall on control pipe\n");
307 return USB_STOR_XFER_STALLED;
308 }
309
310 /* for other sorts of endpoint, clear the stall */
311 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
312 if (usb_stor_clear_halt(us, pipe) < 0)
313 return USB_STOR_XFER_ERROR;
314 return USB_STOR_XFER_STALLED;
315
316 /* timeout or excessively long NAK */
317 case -ETIMEDOUT:
318 US_DEBUGP("-- timeout or NAK\n");
319 return USB_STOR_XFER_ERROR;
320
321 /* babble - the device tried to send more than we wanted to read */
322 case -EOVERFLOW:
323 US_DEBUGP("-- babble\n");
324 return USB_STOR_XFER_LONG;
325
326 /* the transfer was cancelled by abort, disconnect, or timeout */
327 case -ECONNRESET:
328 US_DEBUGP("-- transfer cancelled\n");
329 return USB_STOR_XFER_ERROR;
330
331 /* short scatter-gather read transfer */
332 case -EREMOTEIO:
333 US_DEBUGP("-- short read transfer\n");
334 return USB_STOR_XFER_SHORT;
335
336 /* abort or disconnect in progress */
337 case -EIO:
338 US_DEBUGP("-- abort or disconnect in progress\n");
339 return USB_STOR_XFER_ERROR;
340
341 /* the catch-all error case */
342 default:
343 US_DEBUGP("-- unknown error\n");
344 return USB_STOR_XFER_ERROR;
345 }
346}
347
348/*
349 * Transfer one control message, without timeouts, but allowing early
350 * termination. Return codes are USB_STOR_XFER_xxx.
351 */
352int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
353 u8 request, u8 requesttype, u16 value, u16 index,
354 void *data, u16 size)
355{
356 int result;
357
358 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
359 __FUNCTION__, request, requesttype,
360 value, index, size);
361
362 /* fill in the devrequest structure */
363 us->cr->bRequestType = requesttype;
364 us->cr->bRequest = request;
365 us->cr->wValue = cpu_to_le16(value);
366 us->cr->wIndex = cpu_to_le16(index);
367 us->cr->wLength = cpu_to_le16(size);
368
369 /* fill and submit the URB */
370 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
371 (unsigned char*) us->cr, data, size,
372 usb_stor_blocking_completion, NULL);
373 result = usb_stor_msg_common(us, 0);
374
375 return interpret_urb_result(us, pipe, size, result,
376 us->current_urb->actual_length);
377}
378
379/*
380 * Receive one interrupt buffer, without timeouts, but allowing early
381 * termination. Return codes are USB_STOR_XFER_xxx.
382 *
383 * This routine always uses us->recv_intr_pipe as the pipe and
384 * us->ep_bInterval as the interrupt interval.
385 */
386static int usb_stor_intr_transfer(struct us_data *us, void *buf,
387 unsigned int length)
388{
389 int result;
390 unsigned int pipe = us->recv_intr_pipe;
391 unsigned int maxp;
392
393 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
394
395 /* calculate the max packet size */
396 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
397 if (maxp > length)
398 maxp = length;
399
400 /* fill and submit the URB */
401 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
402 maxp, usb_stor_blocking_completion, NULL,
403 us->ep_bInterval);
404 result = usb_stor_msg_common(us, 0);
405
406 return interpret_urb_result(us, pipe, length, result,
407 us->current_urb->actual_length);
408}
409
410/*
411 * Transfer one buffer via bulk pipe, without timeouts, but allowing early
412 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
413 * stalls during the transfer, the halt is automatically cleared.
414 */
415int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
416 void *buf, unsigned int length, unsigned int *act_len)
417{
418 int result;
419
420 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
421
422 /* fill and submit the URB */
423 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
424 usb_stor_blocking_completion, NULL);
425 result = usb_stor_msg_common(us, 0);
426
427 /* store the actual length of the data transferred */
428 if (act_len)
429 *act_len = us->current_urb->actual_length;
430 return interpret_urb_result(us, pipe, length, result,
431 us->current_urb->actual_length);
432}
433
434/*
435 * Transfer a scatter-gather list via bulk transfer
436 *
437 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
438 * above, but it uses the usbcore scatter-gather library.
439 */
440static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
441 struct scatterlist *sg, int num_sg, unsigned int length,
442 unsigned int *act_len)
443{
444 int result;
445
446 /* don't submit s-g requests during abort/disconnect processing */
447 if (us->flags & ABORTING_OR_DISCONNECTING)
448 return USB_STOR_XFER_ERROR;
449
450 /* initialize the scatter-gather request block */
451 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
452 length, num_sg);
453 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
454 sg, num_sg, length, SLAB_NOIO);
455 if (result) {
456 US_DEBUGP("usb_sg_init returned %d\n", result);
457 return USB_STOR_XFER_ERROR;
458 }
459
460 /* since the block has been initialized successfully, it's now
461 * okay to cancel it */
462 set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
463
464 /* did an abort/disconnect occur during the submission? */
465 if (us->flags & ABORTING_OR_DISCONNECTING) {
466
467 /* cancel the request, if it hasn't been cancelled already */
468 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
469 US_DEBUGP("-- cancelling sg request\n");
470 usb_sg_cancel(&us->current_sg);
471 }
472 }
473
474 /* wait for the completion of the transfer */
475 usb_sg_wait(&us->current_sg);
476 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
477
478 result = us->current_sg.status;
479 if (act_len)
480 *act_len = us->current_sg.bytes;
481 return interpret_urb_result(us, pipe, length, result,
482 us->current_sg.bytes);
483}
484
485/*
486 * Transfer an entire SCSI command's worth of data payload over the bulk
487 * pipe.
488 *
489 * Note that this uses usb_stor_bulk_transfer_buf() and
490 * usb_stor_bulk_transfer_sglist() to achieve its goals --
491 * this function simply determines whether we're going to use
492 * scatter-gather or not, and acts appropriately.
493 */
494int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
495 void *buf, unsigned int length_left, int use_sg, int *residual)
496{
497 int result;
498 unsigned int partial;
499
500 /* are we scatter-gathering? */
501 if (use_sg) {
502 /* use the usb core scatter-gather primitives */
503 result = usb_stor_bulk_transfer_sglist(us, pipe,
504 (struct scatterlist *) buf, use_sg,
505 length_left, &partial);
506 length_left -= partial;
507 } else {
508 /* no scatter-gather, just make the request */
509 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
510 length_left, &partial);
511 length_left -= partial;
512 }
513
514 /* store the residual and return the error code */
515 if (residual)
516 *residual = length_left;
517 return result;
518}
519
520/***********************************************************************
521 * Transport routines
522 ***********************************************************************/
523
524/* Invoke the transport and basic error-handling/recovery methods
525 *
526 * This is used by the protocol layers to actually send the message to
527 * the device and receive the response.
528 */
529void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
530{
531 int need_auto_sense;
532 int result;
533
534 /* send the command to the transport layer */
535 srb->resid = 0;
536 result = us->transport(srb, us);
537
538 /* if the command gets aborted by the higher layers, we need to
539 * short-circuit all other processing
540 */
541 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
542 US_DEBUGP("-- command was aborted\n");
543 goto Handle_Abort;
544 }
545
546 /* if there is a transport error, reset and don't auto-sense */
547 if (result == USB_STOR_TRANSPORT_ERROR) {
548 US_DEBUGP("-- transport indicates error, resetting\n");
549 us->transport_reset(us);
550 srb->result = DID_ERROR << 16;
551 return;
552 }
553
554 /* if the transport provided its own sense data, don't auto-sense */
555 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
556 srb->result = SAM_STAT_CHECK_CONDITION;
557 return;
558 }
559
560 srb->result = SAM_STAT_GOOD;
561
562 /* Determine if we need to auto-sense
563 *
564 * I normally don't use a flag like this, but it's almost impossible
565 * to understand what's going on here if I don't.
566 */
567 need_auto_sense = 0;
568
569 /*
570 * If we're running the CB transport, which is incapable
571 * of determining status on its own, we will auto-sense
572 * unless the operation involved a data-in transfer. Devices
573 * can signal most data-in errors by stalling the bulk-in pipe.
574 */
575 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
576 srb->sc_data_direction != DMA_FROM_DEVICE) {
577 US_DEBUGP("-- CB transport device requiring auto-sense\n");
578 need_auto_sense = 1;
579 }
580
581 /*
582 * If we have a failure, we're going to do a REQUEST_SENSE
583 * automatically. Note that we differentiate between a command
584 * "failure" and an "error" in the transport mechanism.
585 */
586 if (result == USB_STOR_TRANSPORT_FAILED) {
587 US_DEBUGP("-- transport indicates command failure\n");
588 need_auto_sense = 1;
589 }
590
591 /*
592 * A short transfer on a command where we don't expect it
593 * is unusual, but it doesn't mean we need to auto-sense.
594 */
595 if ((srb->resid > 0) &&
596 !((srb->cmnd[0] == REQUEST_SENSE) ||
597 (srb->cmnd[0] == INQUIRY) ||
598 (srb->cmnd[0] == MODE_SENSE) ||
599 (srb->cmnd[0] == LOG_SENSE) ||
600 (srb->cmnd[0] == MODE_SENSE_10))) {
601 US_DEBUGP("-- unexpectedly short transfer\n");
602 }
603
604 /* Now, if we need to do the auto-sense, let's do it */
605 if (need_auto_sense) {
606 int temp_result;
607 void* old_request_buffer;
608 unsigned short old_sg;
609 unsigned old_request_bufflen;
610 unsigned char old_sc_data_direction;
611 unsigned char old_cmd_len;
612 unsigned char old_cmnd[MAX_COMMAND_SIZE];
613 unsigned long old_serial_number;
614 int old_resid;
615
616 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
617
618 /* save the old command */
619 memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
620 old_cmd_len = srb->cmd_len;
621
622 /* set the command and the LUN */
623 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
624 srb->cmnd[0] = REQUEST_SENSE;
625 srb->cmnd[1] = old_cmnd[1] & 0xE0;
626 srb->cmnd[4] = 18;
627
628 /* FIXME: we must do the protocol translation here */
629 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
630 srb->cmd_len = 6;
631 else
632 srb->cmd_len = 12;
633
634 /* set the transfer direction */
635 old_sc_data_direction = srb->sc_data_direction;
636 srb->sc_data_direction = DMA_FROM_DEVICE;
637
638 /* use the new buffer we have */
639 old_request_buffer = srb->request_buffer;
640 srb->request_buffer = srb->sense_buffer;
641
642 /* set the buffer length for transfer */
643 old_request_bufflen = srb->request_bufflen;
644 srb->request_bufflen = 18;
645
646 /* set up for no scatter-gather use */
647 old_sg = srb->use_sg;
648 srb->use_sg = 0;
649
650 /* change the serial number -- toggle the high bit*/
651 old_serial_number = srb->serial_number;
652 srb->serial_number ^= 0x80000000;
653
654 /* issue the auto-sense command */
655 old_resid = srb->resid;
656 srb->resid = 0;
657 temp_result = us->transport(us->srb, us);
658
659 /* let's clean up right away */
660 srb->resid = old_resid;
661 srb->request_buffer = old_request_buffer;
662 srb->request_bufflen = old_request_bufflen;
663 srb->use_sg = old_sg;
664 srb->serial_number = old_serial_number;
665 srb->sc_data_direction = old_sc_data_direction;
666 srb->cmd_len = old_cmd_len;
667 memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
668
669 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
670 US_DEBUGP("-- auto-sense aborted\n");
671 goto Handle_Abort;
672 }
673 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
674 US_DEBUGP("-- auto-sense failure\n");
675
676 /* we skip the reset if this happens to be a
677 * multi-target device, since failure of an
678 * auto-sense is perfectly valid
679 */
680 if (!(us->flags & US_FL_SCM_MULT_TARG))
681 us->transport_reset(us);
682 srb->result = DID_ERROR << 16;
683 return;
684 }
685
686 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
687 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
688 srb->sense_buffer[0],
689 srb->sense_buffer[2] & 0xf,
690 srb->sense_buffer[12],
691 srb->sense_buffer[13]);
692#ifdef CONFIG_USB_STORAGE_DEBUG
693 usb_stor_show_sense(
694 srb->sense_buffer[2] & 0xf,
695 srb->sense_buffer[12],
696 srb->sense_buffer[13]);
697#endif
698
699 /* set the result so the higher layers expect this data */
700 srb->result = SAM_STAT_CHECK_CONDITION;
701
702 /* If things are really okay, then let's show that. Zero
703 * out the sense buffer so the higher layers won't realize
704 * we did an unsolicited auto-sense. */
705 if (result == USB_STOR_TRANSPORT_GOOD &&
706 /* Filemark 0, ignore EOM, ILI 0, no sense */
707 (srb->sense_buffer[2] & 0xaf) == 0 &&
708 /* No ASC or ASCQ */
709 srb->sense_buffer[12] == 0 &&
710 srb->sense_buffer[13] == 0) {
711 srb->result = SAM_STAT_GOOD;
712 srb->sense_buffer[0] = 0x0;
713 }
714 }
715
716 /* Did we transfer less than the minimum amount required? */
717 if (srb->result == SAM_STAT_GOOD &&
718 srb->request_bufflen - srb->resid < srb->underflow)
719 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
720
721 return;
722
723 /* abort processing: the bulk-only transport requires a reset
724 * following an abort */
725 Handle_Abort:
726 srb->result = DID_ABORT << 16;
727 if (us->protocol == US_PR_BULK)
728 us->transport_reset(us);
729}
730
731/* Stop the current URB transfer */
732void usb_stor_stop_transport(struct us_data *us)
733{
734 US_DEBUGP("%s called\n", __FUNCTION__);
735
736 /* If the state machine is blocked waiting for an URB,
737 * let's wake it up. The test_and_clear_bit() call
738 * guarantees that if a URB has just been submitted,
739 * it won't be cancelled more than once. */
740 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
741 US_DEBUGP("-- cancelling URB\n");
742 usb_unlink_urb(us->current_urb);
743 }
744
745 /* If we are waiting for a scatter-gather operation, cancel it. */
746 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
747 US_DEBUGP("-- cancelling sg request\n");
748 usb_sg_cancel(&us->current_sg);
749 }
750}
751
752/*
753 * Control/Bulk/Interrupt transport
754 */
755
756int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
757{
758 unsigned int transfer_length = srb->request_bufflen;
759 unsigned int pipe = 0;
760 int result;
761
762 /* COMMAND STAGE */
763 /* let's send the command via the control pipe */
764 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
765 US_CBI_ADSC,
766 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
767 us->ifnum, srb->cmnd, srb->cmd_len);
768
769 /* check the return code for the command */
770 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
771
772 /* if we stalled the command, it means command failed */
773 if (result == USB_STOR_XFER_STALLED) {
774 return USB_STOR_TRANSPORT_FAILED;
775 }
776
777 /* Uh oh... serious problem here */
778 if (result != USB_STOR_XFER_GOOD) {
779 return USB_STOR_TRANSPORT_ERROR;
780 }
781
782 /* DATA STAGE */
783 /* transfer the data payload for this command, if one exists*/
784 if (transfer_length) {
785 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
786 us->recv_bulk_pipe : us->send_bulk_pipe;
787 result = usb_stor_bulk_transfer_sg(us, pipe,
788 srb->request_buffer, transfer_length,
789 srb->use_sg, &srb->resid);
790 US_DEBUGP("CBI data stage result is 0x%x\n", result);
791
792 /* if we stalled the data transfer it means command failed */
793 if (result == USB_STOR_XFER_STALLED)
794 return USB_STOR_TRANSPORT_FAILED;
795 if (result > USB_STOR_XFER_STALLED)
796 return USB_STOR_TRANSPORT_ERROR;
797 }
798
799 /* STATUS STAGE */
800 result = usb_stor_intr_transfer(us, us->iobuf, 2);
801 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
802 us->iobuf[0], us->iobuf[1]);
803 if (result != USB_STOR_XFER_GOOD)
804 return USB_STOR_TRANSPORT_ERROR;
805
806 /* UFI gives us ASC and ASCQ, like a request sense
807 *
808 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
809 * devices, so we ignore the information for those commands. Note
810 * that this means we could be ignoring a real error on these
811 * commands, but that can't be helped.
812 */
813 if (us->subclass == US_SC_UFI) {
814 if (srb->cmnd[0] == REQUEST_SENSE ||
815 srb->cmnd[0] == INQUIRY)
816 return USB_STOR_TRANSPORT_GOOD;
817 if (us->iobuf[0])
818 goto Failed;
819 return USB_STOR_TRANSPORT_GOOD;
820 }
821
822 /* If not UFI, we interpret the data as a result code
823 * The first byte should always be a 0x0.
824 *
825 * Some bogus devices don't follow that rule. They stuff the ASC
826 * into the first byte -- so if it's non-zero, call it a failure.
827 */
828 if (us->iobuf[0]) {
829 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
830 us->iobuf[0]);
831 goto Failed;
832
833 }
834
835 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
836 switch (us->iobuf[1] & 0x0F) {
837 case 0x00:
838 return USB_STOR_TRANSPORT_GOOD;
839 case 0x01:
840 goto Failed;
841 }
842 return USB_STOR_TRANSPORT_ERROR;
843
844 /* the CBI spec requires that the bulk pipe must be cleared
845 * following any data-in/out command failure (section 2.4.3.1.3)
846 */
847 Failed:
848 if (pipe)
849 usb_stor_clear_halt(us, pipe);
850 return USB_STOR_TRANSPORT_FAILED;
851}
852
853/*
854 * Control/Bulk transport
855 */
856int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
857{
858 unsigned int transfer_length = srb->request_bufflen;
859 int result;
860
861 /* COMMAND STAGE */
862 /* let's send the command via the control pipe */
863 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
864 US_CBI_ADSC,
865 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
866 us->ifnum, srb->cmnd, srb->cmd_len);
867
868 /* check the return code for the command */
869 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
870
871 /* if we stalled the command, it means command failed */
872 if (result == USB_STOR_XFER_STALLED) {
873 return USB_STOR_TRANSPORT_FAILED;
874 }
875
876 /* Uh oh... serious problem here */
877 if (result != USB_STOR_XFER_GOOD) {
878 return USB_STOR_TRANSPORT_ERROR;
879 }
880
881 /* DATA STAGE */
882 /* transfer the data payload for this command, if one exists*/
883 if (transfer_length) {
884 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
885 us->recv_bulk_pipe : us->send_bulk_pipe;
886 result = usb_stor_bulk_transfer_sg(us, pipe,
887 srb->request_buffer, transfer_length,
888 srb->use_sg, &srb->resid);
889 US_DEBUGP("CB data stage result is 0x%x\n", result);
890
891 /* if we stalled the data transfer it means command failed */
892 if (result == USB_STOR_XFER_STALLED)
893 return USB_STOR_TRANSPORT_FAILED;
894 if (result > USB_STOR_XFER_STALLED)
895 return USB_STOR_TRANSPORT_ERROR;
896 }
897
898 /* STATUS STAGE */
899 /* NOTE: CB does not have a status stage. Silly, I know. So
900 * we have to catch this at a higher level.
901 */
902 return USB_STOR_TRANSPORT_GOOD;
903}
904
905/*
906 * Bulk only transport
907 */
908
909/* Determine what the maximum LUN supported is */
910int usb_stor_Bulk_max_lun(struct us_data *us)
911{
912 int result;
913
914 /* issue the command */
915 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
916 US_BULK_GET_MAX_LUN,
917 USB_DIR_IN | USB_TYPE_CLASS |
918 USB_RECIP_INTERFACE,
919 0, us->ifnum, us->iobuf, 1, HZ);
920
921 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
922 result, us->iobuf[0]);
923
924 /* if we have a successful request, return the result */
925 if (result > 0)
926 return us->iobuf[0];
927
928 /*
929 * Some devices (i.e. Iomega Zip100) need this -- apparently
930 * the bulk pipes get STALLed when the GetMaxLUN request is
931 * processed. This is, in theory, harmless to all other devices
932 * (regardless of if they stall or not).
933 */
934 if (result == -EPIPE) {
935 usb_stor_clear_halt(us, us->recv_bulk_pipe);
936 usb_stor_clear_halt(us, us->send_bulk_pipe);
937 }
938
939 /*
940 * Some devices don't like GetMaxLUN. They may STALL the control
941 * pipe, they may return a zero-length result, they may do nothing at
942 * all and timeout, or they may fail in even more bizarrely creative
943 * ways. In these cases the best approach is to use the default
944 * value: only one LUN.
945 */
946 return 0;
947}
948
949int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
950{
951 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
952 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
953 unsigned int transfer_length = srb->request_bufflen;
954 unsigned int residue;
955 int result;
956 int fake_sense = 0;
957 unsigned int cswlen;
958 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
959
960 /* Take care of BULK32 devices; set extra byte to 0 */
961 if ( unlikely(us->flags & US_FL_BULK32)) {
962 cbwlen = 32;
963 us->iobuf[31] = 0;
964 }
965
966 /* set up the command wrapper */
967 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
968 bcb->DataTransferLength = cpu_to_le32(transfer_length);
969 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
970 bcb->Tag = srb->serial_number;
971 bcb->Lun = srb->device->lun;
972 if (us->flags & US_FL_SCM_MULT_TARG)
973 bcb->Lun |= srb->device->id << 4;
974 bcb->Length = srb->cmd_len;
975
976 /* copy the command payload */
977 memset(bcb->CDB, 0, sizeof(bcb->CDB));
978 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
979
980 /* send it to out endpoint */
981 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
982 le32_to_cpu(bcb->Signature), bcb->Tag,
983 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
984 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
985 bcb->Length);
986 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
987 bcb, cbwlen, NULL);
988 US_DEBUGP("Bulk command transfer result=%d\n", result);
989 if (result != USB_STOR_XFER_GOOD)
990 return USB_STOR_TRANSPORT_ERROR;
991
992 /* DATA STAGE */
993 /* send/receive data payload, if there is any */
994
995 /* Some USB-IDE converter chips need a 100us delay between the
996 * command phase and the data phase. Some devices need a little
997 * more than that, probably because of clock rate inaccuracies. */
998 if (unlikely(us->flags & US_FL_GO_SLOW))
999 udelay(110);
1000
1001 if (transfer_length) {
1002 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1003 us->recv_bulk_pipe : us->send_bulk_pipe;
1004 result = usb_stor_bulk_transfer_sg(us, pipe,
1005 srb->request_buffer, transfer_length,
1006 srb->use_sg, &srb->resid);
1007 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
1008 if (result == USB_STOR_XFER_ERROR)
1009 return USB_STOR_TRANSPORT_ERROR;
1010
1011 /* If the device tried to send back more data than the
1012 * amount requested, the spec requires us to transfer
1013 * the CSW anyway. Since there's no point retrying the
1014 * the command, we'll return fake sense data indicating
1015 * Illegal Request, Invalid Field in CDB.
1016 */
1017 if (result == USB_STOR_XFER_LONG)
1018 fake_sense = 1;
1019 }
1020
1021 /* See flow chart on pg 15 of the Bulk Only Transport spec for
1022 * an explanation of how this code works.
1023 */
1024
1025 /* get CSW for device status */
1026 US_DEBUGP("Attempting to get CSW...\n");
1027 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1028 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1029
1030 /* Some broken devices add unnecessary zero-length packets to the
1031 * end of their data transfers. Such packets show up as 0-length
1032 * CSWs. If we encounter such a thing, try to read the CSW again.
1033 */
1034 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1035 US_DEBUGP("Received 0-length CSW; retrying...\n");
1036 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1037 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1038 }
1039
1040 /* did the attempt to read the CSW fail? */
1041 if (result == USB_STOR_XFER_STALLED) {
1042
1043 /* get the status again */
1044 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
1045 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1046 bcs, US_BULK_CS_WRAP_LEN, NULL);
1047 }
1048
1049 /* if we still have a failure at this point, we're in trouble */
1050 US_DEBUGP("Bulk status result = %d\n", result);
1051 if (result != USB_STOR_XFER_GOOD)
1052 return USB_STOR_TRANSPORT_ERROR;
1053
1054 /* check bulk status */
1055 residue = le32_to_cpu(bcs->Residue);
1056 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1057 le32_to_cpu(bcs->Signature), bcs->Tag,
1058 residue, bcs->Status);
1059 if (bcs->Tag != srb->serial_number || bcs->Status > US_BULK_STAT_PHASE) {
1060 US_DEBUGP("Bulk logical error\n");
1061 return USB_STOR_TRANSPORT_ERROR;
1062 }
1063
1064 /* Some broken devices report odd signatures, so we do not check them
1065 * for validity against the spec. We store the first one we see,
1066 * and check subsequent transfers for validity against this signature.
1067 */
1068 if (!us->bcs_signature) {
1069 us->bcs_signature = bcs->Signature;
1070 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1071 US_DEBUGP("Learnt BCS signature 0x%08X\n",
1072 le32_to_cpu(us->bcs_signature));
1073 } else if (bcs->Signature != us->bcs_signature) {
1074 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
1075 le32_to_cpu(bcs->Signature),
1076 le32_to_cpu(us->bcs_signature));
1077 return USB_STOR_TRANSPORT_ERROR;
1078 }
1079
1080 /* try to compute the actual residue, based on how much data
1081 * was really transferred and what the device tells us */
1082 if (residue) {
1083 if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
1084 residue = min(residue, transfer_length);
1085 srb->resid = max(srb->resid, (int) residue);
1086 }
1087 }
1088
1089 /* based on the status code, we report good or bad */
1090 switch (bcs->Status) {
1091 case US_BULK_STAT_OK:
1092 /* device babbled -- return fake sense data */
1093 if (fake_sense) {
1094 memcpy(srb->sense_buffer,
1095 usb_stor_sense_invalidCDB,
1096 sizeof(usb_stor_sense_invalidCDB));
1097 return USB_STOR_TRANSPORT_NO_SENSE;
1098 }
1099
1100 /* command good -- note that data could be short */
1101 return USB_STOR_TRANSPORT_GOOD;
1102
1103 case US_BULK_STAT_FAIL:
1104 /* command failed */
1105 return USB_STOR_TRANSPORT_FAILED;
1106
1107 case US_BULK_STAT_PHASE:
1108 /* phase error -- note that a transport reset will be
1109 * invoked by the invoke_transport() function
1110 */
1111 return USB_STOR_TRANSPORT_ERROR;
1112 }
1113
1114 /* we should never get here, but if we do, we're in trouble */
1115 return USB_STOR_TRANSPORT_ERROR;
1116}
1117
1118/***********************************************************************
1119 * Reset routines
1120 ***********************************************************************/
1121
1122/* This is the common part of the device reset code.
1123 *
1124 * It's handy that every transport mechanism uses the control endpoint for
1125 * resets.
1126 *
1127 * Basically, we send a reset with a 20-second timeout, so we don't get
1128 * jammed attempting to do the reset.
1129 */
1130static int usb_stor_reset_common(struct us_data *us,
1131 u8 request, u8 requesttype,
1132 u16 value, u16 index, void *data, u16 size)
1133{
1134 int result;
1135 int result2;
1136 int rc = FAILED;
1137
1138 /* Let the SCSI layer know we are doing a reset, set the
1139 * RESETTING bit, and clear the ABORTING bit so that the reset
1140 * may proceed.
1141 */
1142 scsi_lock(us_to_host(us));
1143 usb_stor_report_device_reset(us);
1144 set_bit(US_FLIDX_RESETTING, &us->flags);
1145 clear_bit(US_FLIDX_ABORTING, &us->flags);
1146 scsi_unlock(us_to_host(us));
1147
1148 /* A 20-second timeout may seem rather long, but a LaCie
1149 * StudioDrive USB2 device takes 16+ seconds to get going
1150 * following a powerup or USB attach event.
1151 */
1152 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1153 request, requesttype, value, index, data, size,
1154 20*HZ);
1155 if (result < 0) {
1156 US_DEBUGP("Soft reset failed: %d\n", result);
1157 goto Done;
1158 }
1159
1160 /* Give the device some time to recover from the reset,
1161 * but don't delay disconnect processing. */
1162 wait_event_interruptible_timeout(us->delay_wait,
1163 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
1164 HZ*6);
1165 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1166 US_DEBUGP("Reset interrupted by disconnect\n");
1167 goto Done;
1168 }
1169
1170 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1171 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1172
1173 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1174 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1175
1176 /* return a result code based on the result of the control message */
1177 if (result < 0 || result2 < 0) {
1178 US_DEBUGP("Soft reset failed\n");
1179 goto Done;
1180 }
1181 US_DEBUGP("Soft reset done\n");
1182 rc = SUCCESS;
1183
1184 Done:
1185 clear_bit(US_FLIDX_RESETTING, &us->flags);
1186 return rc;
1187}
1188
1189/* This issues a CB[I] Reset to the device in question
1190 */
1191#define CB_RESET_CMD_SIZE 12
1192
1193int usb_stor_CB_reset(struct us_data *us)
1194{
1195 US_DEBUGP("%s called\n", __FUNCTION__);
1196
1197 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1198 us->iobuf[0] = SEND_DIAGNOSTIC;
1199 us->iobuf[1] = 4;
1200 return usb_stor_reset_common(us, US_CBI_ADSC,
1201 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1202 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1203}
1204
1205/* This issues a Bulk-only Reset to the device in question, including
1206 * clearing the subsequent endpoint halts that may occur.
1207 */
1208int usb_stor_Bulk_reset(struct us_data *us)
1209{
1210 US_DEBUGP("%s called\n", __FUNCTION__);
1211
1212 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1213 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1214 0, us->ifnum, NULL, 0);
1215}
diff --git a/drivers/usb/storage/transport.h b/drivers/usb/storage/transport.h
new file mode 100644
index 000000000000..e25f8d8fc741
--- /dev/null
+++ b/drivers/usb/storage/transport.h
@@ -0,0 +1,174 @@
1/* Driver for USB Mass Storage compliant devices
2 * Transport Functions Header File
3 *
4 * $Id: transport.h,v 1.18 2002/04/21 02:57:59 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * This driver is based on the 'USB Mass Storage Class' document. This
10 * describes in detail the protocol used to communicate with such
11 * devices. Clearly, the designers had SCSI and ATAPI commands in
12 * mind when they created this document. The commands are all very
13 * similar to commands in the SCSI-II and ATAPI specifications.
14 *
15 * It is important to note that in a number of cases this class
16 * exhibits class-specific exemptions from the USB specification.
17 * Notably the usage of NAK, STALL and ACK differs from the norm, in
18 * that they are used to communicate wait, failed and OK on commands.
19 *
20 * Also, for certain devices, the interrupt endpoint is used to convey
21 * status of a command.
22 *
23 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
24 * information about this driver.
25 *
26 * This program is free software; you can redistribute it and/or modify it
27 * under the terms of the GNU General Public License as published by the
28 * Free Software Foundation; either version 2, or (at your option) any
29 * later version.
30 *
31 * This program is distributed in the hope that it will be useful, but
32 * WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License along
37 * with this program; if not, write to the Free Software Foundation, Inc.,
38 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 */
40
41#ifndef _TRANSPORT_H_
42#define _TRANSPORT_H_
43
44#include <linux/config.h>
45#include <linux/blkdev.h>
46
47/* Protocols */
48
49#define US_PR_CBI 0x00 /* Control/Bulk/Interrupt */
50#define US_PR_CB 0x01 /* Control/Bulk w/o interrupt */
51#define US_PR_BULK 0x50 /* bulk only */
52#ifdef CONFIG_USB_STORAGE_USBAT
53#define US_PR_SCM_ATAPI 0x80 /* SCM-ATAPI bridge */
54#endif
55#ifdef CONFIG_USB_STORAGE_SDDR09
56#define US_PR_EUSB_SDDR09 0x81 /* SCM-SCSI bridge for SDDR-09 */
57#endif
58#ifdef CONFIG_USB_STORAGE_SDDR55
59#define US_PR_SDDR55 0x82 /* SDDR-55 (made up) */
60#endif
61#define US_PR_DPCM_USB 0xf0 /* Combination CB/SDDR09 */
62
63#ifdef CONFIG_USB_STORAGE_FREECOM
64#define US_PR_FREECOM 0xf1 /* Freecom */
65#endif
66
67#ifdef CONFIG_USB_STORAGE_DATAFAB
68#define US_PR_DATAFAB 0xf2 /* Datafab chipsets */
69#endif
70
71#ifdef CONFIG_USB_STORAGE_JUMPSHOT
72#define US_PR_JUMPSHOT 0xf3 /* Lexar Jumpshot */
73#endif
74
75#define US_PR_DEVICE 0xff /* Use device's value */
76
77/*
78 * Bulk only data structures
79 */
80
81/* command block wrapper */
82struct bulk_cb_wrap {
83 __le32 Signature; /* contains 'USBC' */
84 __u32 Tag; /* unique per command id */
85 __le32 DataTransferLength; /* size of data */
86 __u8 Flags; /* direction in bit 0 */
87 __u8 Lun; /* LUN normally 0 */
88 __u8 Length; /* of of the CDB */
89 __u8 CDB[16]; /* max command */
90};
91
92#define US_BULK_CB_WRAP_LEN 31
93#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
94#define US_BULK_FLAG_IN 1
95#define US_BULK_FLAG_OUT 0
96
97/* command status wrapper */
98struct bulk_cs_wrap {
99 __le32 Signature; /* should = 'USBS' */
100 __u32 Tag; /* same as original command */
101 __le32 Residue; /* amount not transferred */
102 __u8 Status; /* see below */
103 __u8 Filler[18];
104};
105
106#define US_BULK_CS_WRAP_LEN 13
107#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
108#define US_BULK_STAT_OK 0
109#define US_BULK_STAT_FAIL 1
110#define US_BULK_STAT_PHASE 2
111
112/* bulk-only class specific requests */
113#define US_BULK_RESET_REQUEST 0xff
114#define US_BULK_GET_MAX_LUN 0xfe
115
116/*
117 * usb_stor_bulk_transfer_xxx() return codes, in order of severity
118 */
119
120#define USB_STOR_XFER_GOOD 0 /* good transfer */
121#define USB_STOR_XFER_SHORT 1 /* transferred less than expected */
122#define USB_STOR_XFER_STALLED 2 /* endpoint stalled */
123#define USB_STOR_XFER_LONG 3 /* device tried to send too much */
124#define USB_STOR_XFER_ERROR 4 /* transfer died in the middle */
125
126/*
127 * Transport return codes
128 */
129
130#define USB_STOR_TRANSPORT_GOOD 0 /* Transport good, command good */
131#define USB_STOR_TRANSPORT_FAILED 1 /* Transport good, command failed */
132#define USB_STOR_TRANSPORT_NO_SENSE 2 /* Command failed, no auto-sense */
133#define USB_STOR_TRANSPORT_ERROR 3 /* Transport bad (i.e. device dead) */
134
135/*
136 * We used to have USB_STOR_XFER_ABORTED and USB_STOR_TRANSPORT_ABORTED
137 * return codes. But now the transport and low-level transfer routines
138 * treat an abort as just another error (-ENOENT for a cancelled URB).
139 * It is up to the invoke_transport() function to test for aborts and
140 * distinguish them from genuine communication errors.
141 */
142
143/*
144 * CBI accept device specific command
145 */
146
147#define US_CBI_ADSC 0
148
149extern int usb_stor_CBI_transport(struct scsi_cmnd *, struct us_data*);
150
151extern int usb_stor_CB_transport(struct scsi_cmnd *, struct us_data*);
152extern int usb_stor_CB_reset(struct us_data*);
153
154extern int usb_stor_Bulk_transport(struct scsi_cmnd *, struct us_data*);
155extern int usb_stor_Bulk_max_lun(struct us_data*);
156extern int usb_stor_Bulk_reset(struct us_data*);
157
158extern void usb_stor_invoke_transport(struct scsi_cmnd *, struct us_data*);
159extern void usb_stor_stop_transport(struct us_data*);
160
161extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
162 u8 request, u8 requesttype, u16 value, u16 index,
163 void *data, u16 size, int timeout);
164extern int usb_stor_clear_halt(struct us_data *us, unsigned int pipe);
165
166extern int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
167 u8 request, u8 requesttype, u16 value, u16 index,
168 void *data, u16 size);
169extern int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
170 void *buf, unsigned int length, unsigned int *act_len);
171extern int usb_stor_bulk_transfer_sg(struct us_data *us, unsigned int pipe,
172 void *buf, unsigned int length, int use_sg, int *residual);
173
174#endif
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
new file mode 100644
index 000000000000..d53f777c4f50
--- /dev/null
+++ b/drivers/usb/storage/unusual_devs.h
@@ -0,0 +1,986 @@
1/* Driver for USB Mass Storage compliant devices
2 * Ununsual Devices File
3 *
4 * $Id: unusual_devs.h,v 1.32 2002/02/25 02:41:24 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 2000-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Initial work by:
10 * (c) 2000 Adam J. Richter (adam@yggdrasil.com), Yggdrasil Computing, Inc.
11 *
12 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
13 * information about this driver.
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2, or (at your option) any
18 * later version.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30/* IMPORTANT NOTE: This file must be included in another file which does
31 * the following thing for it to work:
32 * The macro UNUSUAL_DEV() must be defined before this file is included
33 */
34#include <linux/config.h>
35
36/* If you edit this file, please try to keep it sorted first by VendorID,
37 * then by ProductID.
38 *
39 * If you want to add an entry for this file, be sure to include the
40 * following information:
41 * - a patch that adds the entry for your device, including your
42 * email address right above the entry (plus maybe a brief
43 * explanation of the reason for the entry),
44 * - a copy of /proc/bus/usb/devices with your device plugged in
45 * running with this patch.
46 * Send your submission to either Phil Dibowitz <phil@ipom.com> or
47 * Alan Stern <stern@rowland.harvard.edu>, and don't forget to CC: the
48 * USB development list <linux-usb-devel@lists.sourceforge.net>.
49 */
50
51UNUSUAL_DEV( 0x03ee, 0x6901, 0x0000, 0x0100,
52 "Mitsumi",
53 "USB FDD",
54 US_SC_DEVICE, US_PR_DEVICE, NULL,
55 US_FL_SINGLE_LUN ),
56
57UNUSUAL_DEV( 0x03f0, 0x0107, 0x0200, 0x0200,
58 "HP",
59 "CD-Writer+",
60 US_SC_8070, US_PR_CB, NULL, 0),
61
62#ifdef CONFIG_USB_STORAGE_USBAT
63UNUSUAL_DEV( 0x03f0, 0x0207, 0x0001, 0x0001,
64 "HP",
65 "CD-Writer+ 8200e",
66 US_SC_8070, US_PR_SCM_ATAPI, init_usbat, 0),
67
68UNUSUAL_DEV( 0x03f0, 0x0307, 0x0001, 0x0001,
69 "HP",
70 "CD-Writer+ CD-4e",
71 US_SC_8070, US_PR_SCM_ATAPI, init_usbat, 0),
72#endif
73
74/* Deduced by Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
75 * Entry needed for flags: US_FL_FIX_INQUIRY because initial inquiry message
76 * always fails and confuses drive.
77 */
78UNUSUAL_DEV( 0x0411, 0x001c, 0x0113, 0x0113,
79 "Buffalo",
80 "DUB-P40G HDD",
81 US_SC_DEVICE, US_PR_DEVICE, NULL,
82 US_FL_FIX_INQUIRY ),
83
84#ifdef CONFIG_USB_STORAGE_DPCM
85UNUSUAL_DEV( 0x0436, 0x0005, 0x0100, 0x0100,
86 "Microtech",
87 "CameraMate (DPCM_USB)",
88 US_SC_SCSI, US_PR_DPCM_USB, NULL, 0 ),
89#endif
90
91/* Patch submitted by Philipp Friedrich <philipp@void.at> */
92UNUSUAL_DEV( 0x0482, 0x0100, 0x0100, 0x0100,
93 "Kyocera",
94 "Finecam S3x",
95 US_SC_8070, US_PR_CB, NULL, US_FL_FIX_INQUIRY),
96
97/* Patch submitted by Philipp Friedrich <philipp@void.at> */
98UNUSUAL_DEV( 0x0482, 0x0101, 0x0100, 0x0100,
99 "Kyocera",
100 "Finecam S4",
101 US_SC_8070, US_PR_CB, NULL, US_FL_FIX_INQUIRY),
102
103/* Patch submitted by Stephane Galles <stephane.galles@free.fr> */
104UNUSUAL_DEV( 0x0482, 0x0103, 0x0100, 0x0100,
105 "Kyocera",
106 "Finecam S5",
107 US_SC_DEVICE, US_PR_DEVICE, NULL, US_FL_FIX_INQUIRY),
108
109/* Patch for Kyocera Finecam L3
110 * Submitted by Michael Krauth <michael.krauth@web.de>
111 * and Alessandro Fracchetti <al.fracchetti@tin.it>
112 */
113UNUSUAL_DEV( 0x0482, 0x0105, 0x0100, 0x0100,
114 "Kyocera",
115 "Finecam L3",
116 US_SC_SCSI, US_PR_BULK, NULL,
117 US_FL_FIX_INQUIRY),
118
119/* Reported by Paul Stewart <stewart@wetlogic.net>
120 * This entry is needed because the device reports Sub=ff */
121UNUSUAL_DEV( 0x04a4, 0x0004, 0x0001, 0x0001,
122 "Hitachi",
123 "DVD-CAM DZ-MV100A Camcorder",
124 US_SC_SCSI, US_PR_CB, NULL, US_FL_SINGLE_LUN),
125
126/* Reported by Andreas Bockhold <andreas@bockionline.de> */
127UNUSUAL_DEV( 0x04b0, 0x0405, 0x0100, 0x0100,
128 "NIKON",
129 "NIKON DSC D70",
130 US_SC_DEVICE, US_PR_DEVICE, NULL,
131 US_FL_FIX_CAPACITY),
132
133/* BENQ DC5330
134 * Reported by Manuel Fombuena <mfombuena@ya.com> and
135 * Frank Copeland <fjc@thingy.apana.org.au> */
136UNUSUAL_DEV( 0x04a5, 0x3010, 0x0100, 0x0100,
137 "Tekom Technologies, Inc",
138 "300_CAMERA",
139 US_SC_DEVICE, US_PR_DEVICE, NULL,
140 US_FL_IGNORE_RESIDUE ),
141
142/* Reported by Simon Levitt <simon@whattf.com>
143 * This entry needs Sub and Proto fields */
144UNUSUAL_DEV( 0x04b8, 0x0601, 0x0100, 0x0100,
145 "Epson",
146 "875DC Storage",
147 US_SC_SCSI, US_PR_CB, NULL, US_FL_FIX_INQUIRY),
148
149/* Reported by Khalid Aziz <khalid@gonehiking.org>
150 * This entry is needed because the device reports Sub=ff */
151UNUSUAL_DEV( 0x04b8, 0x0602, 0x0110, 0x0110,
152 "Epson",
153 "785EPX Storage",
154 US_SC_SCSI, US_PR_BULK, NULL, US_FL_SINGLE_LUN),
155
156/* Not sure who reported this originally but
157 * Pavel Machek <pavel@ucw.cz> reported that the extra US_FL_SINGLE_LUN
158 * flag be added */
159UNUSUAL_DEV( 0x04cb, 0x0100, 0x0000, 0x2210,
160 "Fujifilm",
161 "FinePix 1400Zoom",
162 US_SC_UFI, US_PR_DEVICE, NULL, US_FL_FIX_INQUIRY | US_FL_SINGLE_LUN),
163
164/* Reported by Peter Wächtler <pwaechtler@loewe-komp.de>
165 * The device needs the flags only.
166 */
167UNUSUAL_DEV( 0x04ce, 0x0002, 0x0074, 0x0074,
168 "ScanLogic",
169 "SL11R-IDE",
170 US_SC_DEVICE, US_PR_DEVICE, NULL,
171 US_FL_FIX_INQUIRY),
172
173/* Reported by Kriston Fincher <kriston@airmail.net>
174 * Patch submitted by Sean Millichamp <sean@bruenor.org>
175 * This is to support the Panasonic PalmCam PV-SD4090
176 * This entry is needed because the device reports Sub=ff
177 */
178UNUSUAL_DEV( 0x04da, 0x0901, 0x0100, 0x0200,
179 "Panasonic",
180 "LS-120 Camera",
181 US_SC_UFI, US_PR_DEVICE, NULL, 0),
182
183/* From Yukihiro Nakai, via zaitcev@yahoo.com.
184 * This is needed for CB instead of CBI */
185UNUSUAL_DEV( 0x04da, 0x0d05, 0x0000, 0x0000,
186 "Sharp CE-CW05",
187 "CD-R/RW Drive",
188 US_SC_8070, US_PR_CB, NULL, 0),
189
190/* Reported by Adriaan Penning <a.penning@luon.net> */
191UNUSUAL_DEV( 0x04da, 0x2372, 0x0000, 0x9999,
192 "Panasonic",
193 "DMC-LCx Camera",
194 US_SC_DEVICE, US_PR_DEVICE, NULL,
195 US_FL_FIX_CAPACITY | US_FL_NOT_LOCKABLE ),
196
197/* Most of the following entries were developed with the help of
198 * Shuttle/SCM directly.
199 */
200UNUSUAL_DEV( 0x04e6, 0x0001, 0x0200, 0x0200,
201 "Matshita",
202 "LS-120",
203 US_SC_8020, US_PR_CB, NULL, 0),
204
205UNUSUAL_DEV( 0x04e6, 0x0002, 0x0100, 0x0100,
206 "Shuttle",
207 "eUSCSI Bridge",
208 US_SC_DEVICE, US_PR_DEVICE, usb_stor_euscsi_init,
209 US_FL_SCM_MULT_TARG ),
210
211#ifdef CONFIG_USB_STORAGE_SDDR09
212UNUSUAL_DEV( 0x04e6, 0x0003, 0x0000, 0x9999,
213 "Sandisk",
214 "ImageMate SDDR09",
215 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
216 US_FL_SINGLE_LUN ),
217
218/* This entry is from Andries.Brouwer@cwi.nl */
219UNUSUAL_DEV( 0x04e6, 0x0005, 0x0100, 0x0208,
220 "SCM Microsystems",
221 "eUSB SmartMedia / CompactFlash Adapter",
222 US_SC_SCSI, US_PR_DPCM_USB, sddr09_init,
223 0),
224#endif
225
226/* Reported by Markus Demleitner <msdemlei@cl.uni-heidelberg.de> */
227UNUSUAL_DEV( 0x04e6, 0x0006, 0x0100, 0x0100,
228 "SCM Microsystems Inc.",
229 "eUSB MMC Adapter",
230 US_SC_SCSI, US_PR_CB, NULL,
231 US_FL_SINGLE_LUN),
232
233/* Reported by Daniel Nouri <dpunktnpunkt@web.de> */
234UNUSUAL_DEV( 0x04e6, 0x0006, 0x0205, 0x0205,
235 "Shuttle",
236 "eUSB MMC Adapter",
237 US_SC_SCSI, US_PR_DEVICE, NULL,
238 US_FL_SINGLE_LUN),
239
240UNUSUAL_DEV( 0x04e6, 0x0007, 0x0100, 0x0200,
241 "Sony",
242 "Hifd",
243 US_SC_SCSI, US_PR_CB, NULL,
244 US_FL_SINGLE_LUN),
245
246UNUSUAL_DEV( 0x04e6, 0x0009, 0x0200, 0x0200,
247 "Shuttle",
248 "eUSB ATA/ATAPI Adapter",
249 US_SC_8020, US_PR_CB, NULL, 0),
250
251UNUSUAL_DEV( 0x04e6, 0x000a, 0x0200, 0x0200,
252 "Shuttle",
253 "eUSB CompactFlash Adapter",
254 US_SC_8020, US_PR_CB, NULL, 0),
255
256UNUSUAL_DEV( 0x04e6, 0x000B, 0x0100, 0x0100,
257 "Shuttle",
258 "eUSCSI Bridge",
259 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
260 US_FL_SCM_MULT_TARG ),
261
262UNUSUAL_DEV( 0x04e6, 0x000C, 0x0100, 0x0100,
263 "Shuttle",
264 "eUSCSI Bridge",
265 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
266 US_FL_SCM_MULT_TARG ),
267
268UNUSUAL_DEV( 0x04e6, 0x0101, 0x0200, 0x0200,
269 "Shuttle",
270 "CD-RW Device",
271 US_SC_8020, US_PR_CB, NULL, 0),
272
273/* Entry and supporting patch by Theodore Kilgore <kilgota@auburn.edu>.
274 * Device uses standards-violating 32-byte Bulk Command Block Wrappers and
275 * reports itself as "Proprietary SCSI Bulk." Cf. device entry 0x084d:0x0011.
276 */
277
278UNUSUAL_DEV( 0x04fc, 0x80c2, 0x0100, 0x0100,
279 "Kobian Mercury",
280 "Binocam DCB-132",
281 US_SC_DEVICE, US_PR_DEVICE, NULL,
282 US_FL_BULK32),
283
284#ifdef CONFIG_USB_STORAGE_USBAT
285UNUSUAL_DEV( 0x04e6, 0x1010, 0x0000, 0x9999,
286 "SCM",
287 "SCM USBAT-02",
288 US_SC_SCSI, US_PR_SCM_ATAPI, init_usbat,
289 US_FL_SINGLE_LUN),
290#endif
291
292/* Reported by Bob Sass <rls@vectordb.com> -- only rev 1.33 tested */
293UNUSUAL_DEV( 0x050d, 0x0115, 0x0133, 0x0133,
294 "Belkin",
295 "USB SCSI Adaptor",
296 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
297 US_FL_SCM_MULT_TARG ),
298
299/* Iomega Clik! Drive
300 * Reported by David Chatenay <dchatenay@hotmail.com>
301 * The reason this is needed is not fully known.
302 */
303UNUSUAL_DEV( 0x0525, 0xa140, 0x0100, 0x0100,
304 "Iomega",
305 "USB Clik! 40",
306 US_SC_8070, US_PR_BULK, NULL,
307 US_FL_FIX_INQUIRY ),
308
309/* Yakumo Mega Image 37
310 * Submitted by Stephan Fuhrmann <atomenergie@t-online.de> */
311UNUSUAL_DEV( 0x052b, 0x1801, 0x0100, 0x0100,
312 "Tekom Technologies, Inc",
313 "300_CAMERA",
314 US_SC_DEVICE, US_PR_DEVICE, NULL,
315 US_FL_IGNORE_RESIDUE ),
316
317/* Another Yakumo camera.
318 * Reported by Michele Alzetta <michele.alzetta@aliceposta.it> */
319UNUSUAL_DEV( 0x052b, 0x1804, 0x0100, 0x0100,
320 "Tekom Technologies, Inc",
321 "300_CAMERA",
322 US_SC_DEVICE, US_PR_DEVICE, NULL,
323 US_FL_IGNORE_RESIDUE ),
324
325/* Reported by Iacopo Spalletti <avvisi@spalletti.it> */
326UNUSUAL_DEV( 0x052b, 0x1807, 0x0100, 0x0100,
327 "Tekom Technologies, Inc",
328 "300_CAMERA",
329 US_SC_DEVICE, US_PR_DEVICE, NULL,
330 US_FL_IGNORE_RESIDUE ),
331
332/* Yakumo Mega Image 47
333 * Reported by Bjoern Paetzel <kolrabi@kolrabi.de> */
334UNUSUAL_DEV( 0x052b, 0x1905, 0x0100, 0x0100,
335 "Tekom Technologies, Inc",
336 "400_CAMERA",
337 US_SC_DEVICE, US_PR_DEVICE, NULL,
338 US_FL_IGNORE_RESIDUE ),
339
340/* Reported by Paul Ortyl <ortylp@3miasto.net>
341 * Note that it's similar to the device above, only different prodID */
342UNUSUAL_DEV( 0x052b, 0x1911, 0x0100, 0x0100,
343 "Tekom Technologies, Inc",
344 "400_CAMERA",
345 US_SC_DEVICE, US_PR_DEVICE, NULL,
346 US_FL_IGNORE_RESIDUE ),
347
348UNUSUAL_DEV( 0x054c, 0x0010, 0x0106, 0x0450,
349 "Sony",
350 "DSC-S30/S70/S75/505V/F505/F707/F717/P8",
351 US_SC_SCSI, US_PR_DEVICE, NULL,
352 US_FL_SINGLE_LUN | US_FL_NOT_LOCKABLE | US_FL_NO_WP_DETECT ),
353
354/* This entry is needed because the device reports Sub=ff */
355UNUSUAL_DEV( 0x054c, 0x0010, 0x0500, 0x0500,
356 "Sony",
357 "DSC-T1",
358 US_SC_8070, US_PR_DEVICE, NULL,
359 US_FL_SINGLE_LUN ),
360
361
362/* Reported by wim@geeks.nl */
363UNUSUAL_DEV( 0x054c, 0x0025, 0x0100, 0x0100,
364 "Sony",
365 "Memorystick NW-MS7",
366 US_SC_DEVICE, US_PR_DEVICE, NULL,
367 US_FL_SINGLE_LUN ),
368
369#ifdef CONFIG_USB_STORAGE_ISD200
370UNUSUAL_DEV( 0x054c, 0x002b, 0x0100, 0x0110,
371 "Sony",
372 "Portable USB Harddrive V2",
373 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
374 0 ),
375#endif
376
377/* Submitted by Olaf Hering, <olh@suse.de> SuSE Bugzilla #49049 */
378UNUSUAL_DEV( 0x054c, 0x002c, 0x0501, 0x0501,
379 "Sony",
380 "USB Floppy Drive",
381 US_SC_DEVICE, US_PR_DEVICE, NULL,
382 US_FL_SINGLE_LUN ),
383
384UNUSUAL_DEV( 0x054c, 0x002d, 0x0100, 0x0100,
385 "Sony",
386 "Memorystick MSAC-US1",
387 US_SC_DEVICE, US_PR_DEVICE, NULL,
388 US_FL_SINGLE_LUN ),
389
390/* Submitted by Klaus Mueller <k.mueller@intershop.de> */
391UNUSUAL_DEV( 0x054c, 0x002e, 0x0106, 0x0310,
392 "Sony",
393 "Handycam",
394 US_SC_SCSI, US_PR_DEVICE, NULL,
395 US_FL_SINGLE_LUN ),
396
397/* Submitted by Rajesh Kumble Nayak <nayak@obs-nice.fr> */
398UNUSUAL_DEV( 0x054c, 0x002e, 0x0500, 0x0500,
399 "Sony",
400 "Handycam HC-85",
401 US_SC_UFI, US_PR_DEVICE, NULL,
402 US_FL_SINGLE_LUN ),
403
404UNUSUAL_DEV( 0x054c, 0x0032, 0x0000, 0x9999,
405 "Sony",
406 "Memorystick MSC-U01N",
407 US_SC_DEVICE, US_PR_DEVICE, NULL,
408 US_FL_SINGLE_LUN ),
409
410/* Submitted by Michal Mlotek <mlotek@foobar.pl> */
411UNUSUAL_DEV( 0x054c, 0x0058, 0x0000, 0x9999,
412 "Sony",
413 "PEG N760c Memorystick",
414 US_SC_DEVICE, US_PR_DEVICE, NULL,
415 US_FL_FIX_INQUIRY ),
416
417UNUSUAL_DEV( 0x054c, 0x0069, 0x0000, 0x9999,
418 "Sony",
419 "Memorystick MSC-U03",
420 US_SC_UFI, US_PR_CB, NULL,
421 US_FL_SINGLE_LUN ),
422
423/* Submitted by Nathan Babb <nathan@lexi.com> */
424UNUSUAL_DEV( 0x054c, 0x006d, 0x0000, 0x9999,
425 "Sony",
426 "PEG Mass Storage",
427 US_SC_DEVICE, US_PR_DEVICE, NULL,
428 US_FL_FIX_INQUIRY ),
429
430/* Submitted by Mike Alborn <malborn@deandra.homeip.net> */
431UNUSUAL_DEV( 0x054c, 0x016a, 0x0000, 0x9999,
432 "Sony",
433 "PEG Mass Storage",
434 US_SC_DEVICE, US_PR_DEVICE, NULL,
435 US_FL_FIX_INQUIRY ),
436
437/* Submitted by Frank Engel <frankie@cse.unsw.edu.au> */
438UNUSUAL_DEV( 0x054c, 0x0099, 0x0000, 0x9999,
439 "Sony",
440 "PEG Mass Storage",
441 US_SC_DEVICE, US_PR_DEVICE, NULL,
442 US_FL_FIX_INQUIRY ),
443
444
445UNUSUAL_DEV( 0x057b, 0x0000, 0x0000, 0x0299,
446 "Y-E Data",
447 "Flashbuster-U",
448 US_SC_DEVICE, US_PR_CB, NULL,
449 US_FL_SINGLE_LUN),
450
451UNUSUAL_DEV( 0x057b, 0x0000, 0x0300, 0x9999,
452 "Y-E Data",
453 "Flashbuster-U",
454 US_SC_DEVICE, US_PR_DEVICE, NULL,
455 US_FL_SINGLE_LUN),
456
457/* Reported by Johann Cardon <johann.cardon@free.fr>
458 * This entry is needed only because the device reports
459 * bInterfaceClass = 0xff (vendor-specific)
460 */
461UNUSUAL_DEV( 0x057b, 0x0022, 0x0000, 0x9999,
462 "Y-E Data",
463 "Silicon Media R/W",
464 US_SC_DEVICE, US_PR_DEVICE, NULL, 0),
465
466/* Fabrizio Fellini <fello@libero.it> */
467UNUSUAL_DEV( 0x0595, 0x4343, 0x0000, 0x2210,
468 "Fujifilm",
469 "Digital Camera EX-20 DSC",
470 US_SC_8070, US_PR_DEVICE, NULL, 0 ),
471
472/* The entry was here before I took over, and had US_SC_RBC. It turns
473 * out that isn't needed. Additionally, Torsten Eriksson
474 * <Torsten.Eriksson@bergianska.se> is able to use his device fine
475 * without this entry at all - but I don't suspect that will be true
476 * for all users (the protocol is likely needed), so is staying at
477 * this time. - Phil Dibowitz <phil@ipom.com>
478 */
479UNUSUAL_DEV( 0x059f, 0xa601, 0x0200, 0x0200,
480 "LaCie",
481 "USB Hard Disk",
482 US_SC_DEVICE, US_PR_CB, NULL, 0 ),
483
484/* Submitted by Joel Bourquard <numlock@freesurf.ch>
485 * Some versions of this device need the SubClass and Protocol overrides
486 * while others don't.
487 */
488UNUSUAL_DEV( 0x05ab, 0x0060, 0x1104, 0x1110,
489 "In-System",
490 "PyroGate External CD-ROM Enclosure (FCD-523)",
491 US_SC_SCSI, US_PR_BULK, NULL,
492 US_FL_NEED_OVERRIDE ),
493
494#ifdef CONFIG_USB_STORAGE_ISD200
495UNUSUAL_DEV( 0x05ab, 0x0031, 0x0100, 0x0110,
496 "In-System",
497 "USB/IDE Bridge (ATA/ATAPI)",
498 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
499 0 ),
500
501UNUSUAL_DEV( 0x05ab, 0x0301, 0x0100, 0x0110,
502 "In-System",
503 "Portable USB Harddrive V2",
504 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
505 0 ),
506
507UNUSUAL_DEV( 0x05ab, 0x0351, 0x0100, 0x0110,
508 "In-System",
509 "Portable USB Harddrive V2",
510 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
511 0 ),
512
513UNUSUAL_DEV( 0x05ab, 0x5701, 0x0100, 0x0110,
514 "In-System",
515 "USB Storage Adapter V2",
516 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
517 0 ),
518#endif
519
520/* Reported by Avi Kivity <avi@argo.co.il> */
521UNUSUAL_DEV( 0x05ac, 0x1203, 0x0001, 0x0001,
522 "Apple",
523 "iPod",
524 US_SC_DEVICE, US_PR_DEVICE, NULL,
525 US_FL_FIX_CAPACITY ),
526
527UNUSUAL_DEV( 0x05ac, 0x1205, 0x0001, 0x0001,
528 "Apple",
529 "iPod",
530 US_SC_DEVICE, US_PR_DEVICE, NULL,
531 US_FL_FIX_CAPACITY ),
532
533#ifdef CONFIG_USB_STORAGE_JUMPSHOT
534UNUSUAL_DEV( 0x05dc, 0x0001, 0x0000, 0x0001,
535 "Lexar",
536 "Jumpshot USB CF Reader",
537 US_SC_SCSI, US_PR_JUMPSHOT, NULL,
538 US_FL_NEED_OVERRIDE ),
539#endif
540
541/* Reported by Blake Matheny <bmatheny@purdue.edu> */
542UNUSUAL_DEV( 0x05dc, 0xb002, 0x0000, 0x0113,
543 "Lexar",
544 "USB CF Reader",
545 US_SC_DEVICE, US_PR_DEVICE, NULL,
546 US_FL_FIX_INQUIRY ),
547
548/* The following two entries are for a Genesys USB to IDE
549 * converter chip, but it changes its ProductId depending
550 * on whether or not a disk or an optical device is enclosed
551 * They were originally reported by Alexander Oltu
552 * <alexander@all-2.com> and Peter Marks <peter.marks@turner.com>
553 * respectively.
554 */
555UNUSUAL_DEV( 0x05e3, 0x0701, 0x0000, 0xffff,
556 "Genesys Logic",
557 "USB to IDE Optical",
558 US_SC_DEVICE, US_PR_DEVICE, NULL,
559 US_FL_GO_SLOW ),
560
561UNUSUAL_DEV( 0x05e3, 0x0702, 0x0000, 0xffff,
562 "Genesys Logic",
563 "USB to IDE Disk",
564 US_SC_DEVICE, US_PR_DEVICE, NULL,
565 US_FL_GO_SLOW ),
566
567/* Reported by Hanno Boeck <hanno@gmx.de>
568 * Taken from the Lycoris Kernel */
569UNUSUAL_DEV( 0x0636, 0x0003, 0x0000, 0x9999,
570 "Vivitar",
571 "Vivicam 35Xx",
572 US_SC_SCSI, US_PR_BULK, NULL,
573 US_FL_FIX_INQUIRY ),
574
575UNUSUAL_DEV( 0x0644, 0x0000, 0x0100, 0x0100,
576 "TEAC",
577 "Floppy Drive",
578 US_SC_UFI, US_PR_CB, NULL, 0 ),
579
580#ifdef CONFIG_USB_STORAGE_SDDR09
581UNUSUAL_DEV( 0x066b, 0x0105, 0x0100, 0x0100,
582 "Olympus",
583 "Camedia MAUSB-2",
584 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
585 US_FL_SINGLE_LUN ),
586#endif
587
588/* Reported by Darsen Lu <darsen@micro.ee.nthu.edu.tw> */
589UNUSUAL_DEV( 0x066f, 0x8000, 0x0001, 0x0001,
590 "SigmaTel",
591 "USBMSC Audio Player",
592 US_SC_DEVICE, US_PR_DEVICE, NULL,
593 US_FL_FIX_CAPACITY ),
594
595/* Reported by Richard -=[]=- <micro_flyer@hotmail.com> */
596UNUSUAL_DEV( 0x067b, 0x2507, 0x0100, 0x0100,
597 "Prolific Technology Inc.",
598 "Mass Storage Device",
599 US_SC_DEVICE, US_PR_DEVICE, NULL,
600 US_FL_FIX_CAPACITY | US_FL_GO_SLOW ),
601
602/* Reported by Alex Butcher <alex.butcher@assursys.co.uk> */
603UNUSUAL_DEV( 0x067b, 0x3507, 0x0001, 0x0001,
604 "Prolific Technology Inc.",
605 "ATAPI-6 Bridge Controller",
606 US_SC_DEVICE, US_PR_DEVICE, NULL,
607 US_FL_FIX_CAPACITY | US_FL_GO_SLOW ),
608
609/* Submitted by Benny Sjostrand <benny@hostmobility.com> */
610UNUSUAL_DEV( 0x0686, 0x4011, 0x0001, 0x0001,
611 "Minolta",
612 "Dimage F300",
613 US_SC_SCSI, US_PR_BULK, NULL, 0 ),
614
615/* Reported by Miguel A. Fosas <amn3s1a@ono.com> */
616UNUSUAL_DEV( 0x0686, 0x4017, 0x0001, 0x0001,
617 "Minolta",
618 "DIMAGE E223",
619 US_SC_SCSI, US_PR_DEVICE, NULL, 0 ),
620
621UNUSUAL_DEV( 0x0693, 0x0002, 0x0100, 0x0100,
622 "Hagiwara",
623 "FlashGate SmartMedia",
624 US_SC_SCSI, US_PR_BULK, NULL, 0 ),
625
626UNUSUAL_DEV( 0x0693, 0x0005, 0x0100, 0x0100,
627 "Hagiwara",
628 "Flashgate",
629 US_SC_SCSI, US_PR_BULK, NULL, 0 ),
630
631UNUSUAL_DEV( 0x0781, 0x0001, 0x0200, 0x0200,
632 "Sandisk",
633 "ImageMate SDDR-05a",
634 US_SC_SCSI, US_PR_CB, NULL,
635 US_FL_SINGLE_LUN ),
636
637UNUSUAL_DEV( 0x0781, 0x0100, 0x0100, 0x0100,
638 "Sandisk",
639 "ImageMate SDDR-12",
640 US_SC_SCSI, US_PR_CB, NULL,
641 US_FL_SINGLE_LUN ),
642
643#ifdef CONFIG_USB_STORAGE_SDDR09
644UNUSUAL_DEV( 0x0781, 0x0200, 0x0000, 0x9999,
645 "Sandisk",
646 "ImageMate SDDR-09",
647 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
648 US_FL_SINGLE_LUN ),
649#endif
650
651#ifdef CONFIG_USB_STORAGE_FREECOM
652UNUSUAL_DEV( 0x07ab, 0xfc01, 0x0000, 0x9999,
653 "Freecom",
654 "USB-IDE",
655 US_SC_QIC, US_PR_FREECOM, freecom_init, 0),
656#endif
657
658/* Reported by Eero Volotinen <eero@ping-viini.org> */
659UNUSUAL_DEV( 0x07ab, 0xfccd, 0x0406, 0x0406,
660 "Freecom Technologies",
661 "FHD-Classic",
662 US_SC_DEVICE, US_PR_DEVICE, NULL,
663 US_FL_FIX_CAPACITY),
664
665UNUSUAL_DEV( 0x07af, 0x0004, 0x0100, 0x0133,
666 "Microtech",
667 "USB-SCSI-DB25",
668 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
669 US_FL_SCM_MULT_TARG ),
670
671UNUSUAL_DEV( 0x07af, 0x0005, 0x0100, 0x0100,
672 "Microtech",
673 "USB-SCSI-HD50",
674 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
675 US_FL_SCM_MULT_TARG ),
676
677#ifdef CONFIG_USB_STORAGE_DPCM
678UNUSUAL_DEV( 0x07af, 0x0006, 0x0100, 0x0100,
679 "Microtech",
680 "CameraMate (DPCM_USB)",
681 US_SC_SCSI, US_PR_DPCM_USB, NULL, 0 ),
682#endif
683
684#ifdef CONFIG_USB_STORAGE_DATAFAB
685UNUSUAL_DEV( 0x07c4, 0xa000, 0x0000, 0x0015,
686 "Datafab",
687 "MDCFE-B USB CF Reader",
688 US_SC_SCSI, US_PR_DATAFAB, NULL,
689 0 ),
690
691/*
692 * The following Datafab-based devices may or may not work
693 * using the current driver...the 0xffff is arbitrary since I
694 * don't know what device versions exist for these guys.
695 *
696 * The 0xa003 and 0xa004 devices in particular I'm curious about.
697 * I'm told they exist but so far nobody has come forward to say that
698 * they work with this driver. Given the success we've had getting
699 * other Datafab-based cards operational with this driver, I've decided
700 * to leave these two devices in the list.
701 */
702UNUSUAL_DEV( 0x07c4, 0xa001, 0x0000, 0xffff,
703 "SIIG/Datafab",
704 "SIIG/Datafab Memory Stick+CF Reader/Writer",
705 US_SC_SCSI, US_PR_DATAFAB, NULL,
706 0 ),
707
708/* Reported by Josef Reisinger <josef.reisinger@netcologne.de> */
709UNUSUAL_DEV( 0x07c4, 0xa002, 0x0000, 0xffff,
710 "Datafab/Unknown",
711 "MD2/MD3 Disk enclosure",
712 US_SC_SCSI, US_PR_DATAFAB, NULL,
713 US_FL_SINGLE_LUN ),
714
715UNUSUAL_DEV( 0x07c4, 0xa003, 0x0000, 0xffff,
716 "Datafab/Unknown",
717 "Datafab-based Reader",
718 US_SC_SCSI, US_PR_DATAFAB, NULL,
719 0 ),
720
721UNUSUAL_DEV( 0x07c4, 0xa004, 0x0000, 0xffff,
722 "Datafab/Unknown",
723 "Datafab-based Reader",
724 US_SC_SCSI, US_PR_DATAFAB, NULL,
725 0 ),
726
727UNUSUAL_DEV( 0x07c4, 0xa005, 0x0000, 0xffff,
728 "PNY/Datafab",
729 "PNY/Datafab CF+SM Reader",
730 US_SC_SCSI, US_PR_DATAFAB, NULL,
731 0 ),
732
733UNUSUAL_DEV( 0x07c4, 0xa006, 0x0000, 0xffff,
734 "Simple Tech/Datafab",
735 "Simple Tech/Datafab CF+SM Reader",
736 US_SC_SCSI, US_PR_DATAFAB, NULL,
737 0 ),
738#endif
739
740#ifdef CONFIG_USB_STORAGE_SDDR55
741/* Contributed by Peter Waechtler */
742UNUSUAL_DEV( 0x07c4, 0xa103, 0x0000, 0x9999,
743 "Datafab",
744 "MDSM-B reader",
745 US_SC_SCSI, US_PR_SDDR55, NULL,
746 US_FL_FIX_INQUIRY ),
747#endif
748
749#ifdef CONFIG_USB_STORAGE_DATAFAB
750/* Submitted by Olaf Hering <olh@suse.de> */
751UNUSUAL_DEV( 0x07c4, 0xa109, 0x0000, 0xffff,
752 "Datafab Systems, Inc.",
753 "USB to CF + SM Combo (LC1)",
754 US_SC_SCSI, US_PR_DATAFAB, NULL,
755 0 ),
756#endif
757#ifdef CONFIG_USB_STORAGE_SDDR55
758/* SM part - aeb <Andries.Brouwer@cwi.nl> */
759UNUSUAL_DEV( 0x07c4, 0xa109, 0x0000, 0xffff,
760 "Datafab Systems, Inc.",
761 "USB to CF + SM Combo (LC1)",
762 US_SC_SCSI, US_PR_SDDR55, NULL,
763 US_FL_SINGLE_LUN ),
764#endif
765
766/* Datafab KECF-USB / Sagatek DCS-CF / Simpletech Flashlink UCF-100
767 * Only revision 1.13 tested (same for all of the above devices,
768 * based on the Datafab DF-UG-07 chip). Needed for US_FL_FIX_INQUIRY.
769 * Submitted by Marek Michalkiewicz <marekm@amelek.gda.pl>.
770 * See also http://martin.wilck.bei.t-online.de/#kecf .
771 */
772UNUSUAL_DEV( 0x07c4, 0xa400, 0x0000, 0xffff,
773 "Datafab",
774 "KECF-USB",
775 US_SC_DEVICE, US_PR_DEVICE, NULL,
776 US_FL_FIX_INQUIRY ),
777
778/* Casio QV 2x00/3x00/4000/8000 digital still cameras are not conformant
779 * to the USB storage specification in two ways:
780 * - They tell us they are using transport protocol CBI. In reality they
781 * are using transport protocol CB.
782 * - They don't like the INQUIRY command. So we must handle this command
783 * of the SCSI layer ourselves.
784 * - Some cameras with idProduct=0x1001 and bcdDevice=0x1000 have
785 * bInterfaceProtocol=0x00 (US_PR_CBI) while others have 0x01 (US_PR_CB).
786 * So don't remove the US_PR_CB override!
787 * - Cameras with bcdDevice=0x9009 require the US_SC_8070 override.
788 */
789UNUSUAL_DEV( 0x07cf, 0x1001, 0x1000, 0x9999,
790 "Casio",
791 "QV DigitalCamera",
792 US_SC_8070, US_PR_CB, NULL,
793 US_FL_NEED_OVERRIDE | US_FL_FIX_INQUIRY ),
794
795/* Submitted by Hartmut Wahl <hwahl@hwahl.de>*/
796UNUSUAL_DEV( 0x0839, 0x000a, 0x0001, 0x0001,
797 "Samsung",
798 "Digimax 410",
799 US_SC_DEVICE, US_PR_DEVICE, NULL,
800 US_FL_FIX_INQUIRY),
801
802/* Entry and supporting patch by Theodore Kilgore <kilgota@auburn.edu>.
803 * Flag will support Bulk devices which use a standards-violating 32-byte
804 * Command Block Wrapper. Here, the "DC2MEGA" cameras (several brands) with
805 * Grandtech GT892x chip, which request "Proprietary SCSI Bulk" support.
806 */
807
808UNUSUAL_DEV( 0x084d, 0x0011, 0x0110, 0x0110,
809 "Grandtech",
810 "DC2MEGA",
811 US_SC_DEVICE, US_PR_DEVICE, NULL,
812 US_FL_BULK32),
813
814
815/* Entry needed for flags. Moreover, all devices with this ID use
816 * bulk-only transport, but _some_ falsely report Control/Bulk instead.
817 * One example is "Trumpion Digital Research MYMP3".
818 * Submitted by Bjoern Brill <brill(at)fs.math.uni-frankfurt.de>
819 */
820UNUSUAL_DEV( 0x090a, 0x1001, 0x0100, 0x0100,
821 "Trumpion",
822 "t33520 USB Flash Card Controller",
823 US_SC_DEVICE, US_PR_BULK, NULL,
824 US_FL_NEED_OVERRIDE ),
825
826/* Trumpion Microelectronics MP3 player (felipe_alfaro@linuxmail.org) */
827UNUSUAL_DEV( 0x090a, 0x1200, 0x0000, 0x9999,
828 "Trumpion",
829 "MP3 player",
830 US_SC_RBC, US_PR_BULK, NULL,
831 0 ),
832
833/* aeb */
834UNUSUAL_DEV( 0x090c, 0x1132, 0x0000, 0xffff,
835 "Feiya",
836 "5-in-1 Card Reader",
837 US_SC_DEVICE, US_PR_DEVICE, NULL,
838 US_FL_FIX_CAPACITY ),
839
840/* This Pentax still camera is not conformant
841 * to the USB storage specification: -
842 * - It does not like the INQUIRY command. So we must handle this command
843 * of the SCSI layer ourselves.
844 * Tested on Rev. 10.00 (0x1000)
845 * Submitted by James Courtier-Dutton <James@superbug.demon.co.uk>
846 */
847UNUSUAL_DEV( 0x0a17, 0x0004, 0x1000, 0x1000,
848 "Pentax",
849 "Optio 2/3/400",
850 US_SC_DEVICE, US_PR_DEVICE, NULL,
851 US_FL_FIX_INQUIRY ),
852
853
854/* Submitted by Per Winkvist <per.winkvist@uk.com> */
855UNUSUAL_DEV( 0x0a17, 0x006, 0x0000, 0xffff,
856 "Pentax",
857 "Optio S/S4",
858 US_SC_DEVICE, US_PR_DEVICE, NULL,
859 US_FL_FIX_INQUIRY ),
860
861#ifdef CONFIG_USB_STORAGE_ISD200
862UNUSUAL_DEV( 0x0bf6, 0xa001, 0x0100, 0x0110,
863 "ATI",
864 "USB Cable 205",
865 US_SC_ISD200, US_PR_BULK, isd200_Initialization,
866 0 ),
867#endif
868
869#ifdef CONFIG_USB_STORAGE_DATAFAB
870UNUSUAL_DEV( 0x0c0b, 0xa109, 0x0000, 0xffff,
871 "Acomdata",
872 "CF",
873 US_SC_SCSI, US_PR_DATAFAB, NULL,
874 US_FL_SINGLE_LUN ),
875#endif
876#ifdef CONFIG_USB_STORAGE_SDDR55
877UNUSUAL_DEV( 0x0c0b, 0xa109, 0x0000, 0xffff,
878 "Acomdata",
879 "SM",
880 US_SC_SCSI, US_PR_SDDR55, NULL,
881 US_FL_SINGLE_LUN ),
882#endif
883
884/* Submitted by Joris Struyve <joris@struyve.be> */
885UNUSUAL_DEV( 0x0d96, 0x410a, 0x0001, 0xffff,
886 "Medion",
887 "MD 7425",
888 US_SC_DEVICE, US_PR_DEVICE, NULL,
889 US_FL_FIX_INQUIRY),
890
891/*
892 * Entry for Jenoptik JD 5200z3
893 *
894 * email: car.busse@gmx.de
895 */
896UNUSUAL_DEV( 0x0d96, 0x5200, 0x0001, 0x0200,
897 "Jenoptik",
898 "JD 5200 z3",
899 US_SC_DEVICE, US_PR_DEVICE, NULL, US_FL_FIX_INQUIRY),
900
901/* Reported by Lubomir Blaha <tritol@trilogic.cz>
902 * I _REALLY_ don't know what 3rd, 4th number and all defines mean, but this
903 * works for me. Can anybody correct these values? (I able to test corrected
904 * version.)
905 */
906UNUSUAL_DEV( 0x0dd8, 0x1060, 0x0000, 0xffff,
907 "Netac",
908 "USB-CF-Card",
909 US_SC_DEVICE, US_PR_DEVICE, NULL,
910 US_FL_FIX_INQUIRY ),
911
912/* Patch by Stephan Walter <stephan.walter@epfl.ch>
913 * I don't know why, but it works... */
914UNUSUAL_DEV( 0x0dda, 0x0001, 0x0012, 0x0012,
915 "WINWARD",
916 "Music Disk",
917 US_SC_DEVICE, US_PR_DEVICE, NULL,
918 US_FL_IGNORE_RESIDUE ),
919
920/* Reported by Ian McConnell <ian at emit.demon.co.uk> */
921UNUSUAL_DEV( 0x0dda, 0x0301, 0x0012, 0x0012,
922 "PNP_MP3",
923 "PNP_MP3 PLAYER",
924 US_SC_DEVICE, US_PR_DEVICE, NULL,
925 US_FL_IGNORE_RESIDUE ),
926
927/* Submitted by Antoine Mairesse <antoine.mairesse@free.fr> */
928UNUSUAL_DEV( 0x0ed1, 0x6660, 0x0100, 0x0300,
929 "USB",
930 "Solid state disk",
931 US_SC_DEVICE, US_PR_DEVICE, NULL,
932 US_FL_FIX_INQUIRY ),
933
934/* Submitted by Daniel Drake <dsd@gentoo.org>
935 * Reported by dayul on the Gentoo Forums */
936UNUSUAL_DEV( 0x0ea0, 0x2168, 0x0110, 0x0110,
937 "Ours Technology",
938 "Flash Disk",
939 US_SC_DEVICE, US_PR_DEVICE, NULL,
940 US_FL_IGNORE_RESIDUE ),
941
942/* Reported by Rastislav Stanik <rs_kernel@yahoo.com> */
943UNUSUAL_DEV( 0x0ea0, 0x6828, 0x0110, 0x0110,
944 "USB",
945 "Flash Disk",
946 US_SC_DEVICE, US_PR_DEVICE, NULL,
947 US_FL_IGNORE_RESIDUE ),
948
949/* Reported by Michael Stattmann <michael@stattmann.com> */
950UNUSUAL_DEV( 0x0fce, 0xd008, 0x0000, 0x0000,
951 "Sony Ericsson",
952 "V800-Vodafone 802",
953 US_SC_DEVICE, US_PR_DEVICE, NULL,
954 US_FL_NO_WP_DETECT ),
955
956/* Reported by Kevin Cernekee <kpc-usbdev@gelato.uiuc.edu>
957 * Tested on hardware version 1.10.
958 * Entry is needed only for the initializer function override.
959 */
960UNUSUAL_DEV( 0x1019, 0x0c55, 0x0000, 0x9999,
961 "Desknote",
962 "UCR-61S2B",
963 US_SC_DEVICE, US_PR_DEVICE, usb_stor_ucr61s2b_init,
964 0 ),
965
966/* Reported by Kotrla Vitezslav <kotrla@ceb.cz> */
967UNUSUAL_DEV( 0x1370, 0x6828, 0x0110, 0x0110,
968 "SWISSBIT",
969 "Black Silver",
970 US_SC_DEVICE, US_PR_DEVICE, NULL,
971 US_FL_IGNORE_RESIDUE ),
972
973/* Reported by Radovan Garabik <garabik@kassiopeia.juls.savba.sk> */
974UNUSUAL_DEV( 0x2735, 0x100b, 0x0000, 0x9999,
975 "MPIO",
976 "HS200",
977 US_SC_DEVICE, US_PR_DEVICE, NULL,
978 US_FL_GO_SLOW ),
979
980#ifdef CONFIG_USB_STORAGE_SDDR55
981UNUSUAL_DEV( 0x55aa, 0xa103, 0x0000, 0x9999,
982 "Sandisk",
983 "ImageMate SDDR55",
984 US_SC_SCSI, US_PR_SDDR55, NULL,
985 US_FL_SINGLE_LUN),
986#endif
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
new file mode 100644
index 000000000000..35c1ca6b5a8e
--- /dev/null
+++ b/drivers/usb/storage/usb.c
@@ -0,0 +1,1051 @@
1/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
11 *
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 *
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
17 *
18 * This driver is based on the 'USB Mass Storage Class' document. This
19 * describes in detail the protocol used to communicate with such
20 * devices. Clearly, the designers had SCSI and ATAPI commands in
21 * mind when they created this document. The commands are all very
22 * similar to commands in the SCSI-II and ATAPI specifications.
23 *
24 * It is important to note that in a number of cases this class
25 * exhibits class-specific exemptions from the USB specification.
26 * Notably the usage of NAK, STALL and ACK differs from the norm, in
27 * that they are used to communicate wait, failed and OK on commands.
28 *
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
31 *
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
34 *
35 * This program is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU General Public License as published by the
37 * Free Software Foundation; either version 2, or (at your option) any
38 * later version.
39 *
40 * This program is distributed in the hope that it will be useful, but
41 * WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 675 Mass Ave, Cambridge, MA 02139, USA.
48 */
49
50#include <linux/config.h>
51#include <linux/sched.h>
52#include <linux/errno.h>
53#include <linux/suspend.h>
54#include <linux/module.h>
55#include <linux/init.h>
56#include <linux/slab.h>
57
58#include <scsi/scsi.h>
59#include <scsi/scsi_cmnd.h>
60#include <scsi/scsi_device.h>
61
62#include "usb.h"
63#include "scsiglue.h"
64#include "transport.h"
65#include "protocol.h"
66#include "debug.h"
67#include "initializers.h"
68
69#ifdef CONFIG_USB_STORAGE_USBAT
70#include "shuttle_usbat.h"
71#endif
72#ifdef CONFIG_USB_STORAGE_SDDR09
73#include "sddr09.h"
74#endif
75#ifdef CONFIG_USB_STORAGE_SDDR55
76#include "sddr55.h"
77#endif
78#ifdef CONFIG_USB_STORAGE_DPCM
79#include "dpcm.h"
80#endif
81#ifdef CONFIG_USB_STORAGE_FREECOM
82#include "freecom.h"
83#endif
84#ifdef CONFIG_USB_STORAGE_ISD200
85#include "isd200.h"
86#endif
87#ifdef CONFIG_USB_STORAGE_DATAFAB
88#include "datafab.h"
89#endif
90#ifdef CONFIG_USB_STORAGE_JUMPSHOT
91#include "jumpshot.h"
92#endif
93
94
95/* Some informational data */
96MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
97MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
98MODULE_LICENSE("GPL");
99
100static unsigned int delay_use = 5;
101module_param(delay_use, uint, S_IRUGO | S_IWUSR);
102MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
103
104
105/* These are used to make sure the module doesn't unload before all the
106 * threads have exited.
107 */
108static atomic_t total_threads = ATOMIC_INIT(0);
109static DECLARE_COMPLETION(threads_gone);
110
111
112static int storage_probe(struct usb_interface *iface,
113 const struct usb_device_id *id);
114
115static void storage_disconnect(struct usb_interface *iface);
116
117/* The entries in this table, except for final ones here
118 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
119 * line for line with the entries of us_unsuaul_dev_list[].
120 */
121
122#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
123 vendorName, productName,useProtocol, useTransport, \
124 initFunction, flags) \
125{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
126
127static struct usb_device_id storage_usb_ids [] = {
128
129# include "unusual_devs.h"
130#undef UNUSUAL_DEV
131 /* Control/Bulk transport for all SubClass values */
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
135 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
138
139 /* Control/Bulk/Interrupt transport for all SubClass values */
140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
141 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
143 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
144 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
145 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
146
147 /* Bulk-only transport for all SubClass values */
148 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
149 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
150 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
151 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
152 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
153 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
154
155 /* Terminating entry */
156 { }
157};
158
159MODULE_DEVICE_TABLE (usb, storage_usb_ids);
160
161/* This is the list of devices we recognize, along with their flag data */
162
163/* The vendor name should be kept at eight characters or less, and
164 * the product name should be kept at 16 characters or less. If a device
165 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
166 * normally generated by a device thorugh the INQUIRY response will be
167 * taken from this list, and this is the reason for the above size
168 * restriction. However, if the flag is not present, then you
169 * are free to use as many characters as you like.
170 */
171
172#undef UNUSUAL_DEV
173#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
174 vendor_name, product_name, use_protocol, use_transport, \
175 init_function, Flags) \
176{ \
177 .vendorName = vendor_name, \
178 .productName = product_name, \
179 .useProtocol = use_protocol, \
180 .useTransport = use_transport, \
181 .initFunction = init_function, \
182 .flags = Flags, \
183}
184
185static struct us_unusual_dev us_unusual_dev_list[] = {
186# include "unusual_devs.h"
187# undef UNUSUAL_DEV
188 /* Control/Bulk transport for all SubClass values */
189 { .useProtocol = US_SC_RBC,
190 .useTransport = US_PR_CB},
191 { .useProtocol = US_SC_8020,
192 .useTransport = US_PR_CB},
193 { .useProtocol = US_SC_QIC,
194 .useTransport = US_PR_CB},
195 { .useProtocol = US_SC_UFI,
196 .useTransport = US_PR_CB},
197 { .useProtocol = US_SC_8070,
198 .useTransport = US_PR_CB},
199 { .useProtocol = US_SC_SCSI,
200 .useTransport = US_PR_CB},
201
202 /* Control/Bulk/Interrupt transport for all SubClass values */
203 { .useProtocol = US_SC_RBC,
204 .useTransport = US_PR_CBI},
205 { .useProtocol = US_SC_8020,
206 .useTransport = US_PR_CBI},
207 { .useProtocol = US_SC_QIC,
208 .useTransport = US_PR_CBI},
209 { .useProtocol = US_SC_UFI,
210 .useTransport = US_PR_CBI},
211 { .useProtocol = US_SC_8070,
212 .useTransport = US_PR_CBI},
213 { .useProtocol = US_SC_SCSI,
214 .useTransport = US_PR_CBI},
215
216 /* Bulk-only transport for all SubClass values */
217 { .useProtocol = US_SC_RBC,
218 .useTransport = US_PR_BULK},
219 { .useProtocol = US_SC_8020,
220 .useTransport = US_PR_BULK},
221 { .useProtocol = US_SC_QIC,
222 .useTransport = US_PR_BULK},
223 { .useProtocol = US_SC_UFI,
224 .useTransport = US_PR_BULK},
225 { .useProtocol = US_SC_8070,
226 .useTransport = US_PR_BULK},
227 { .useProtocol = US_SC_SCSI,
228 .useTransport = US_PR_BULK},
229
230 /* Terminating entry */
231 { NULL }
232};
233
234static struct usb_driver usb_storage_driver = {
235 .owner = THIS_MODULE,
236 .name = "usb-storage",
237 .probe = storage_probe,
238 .disconnect = storage_disconnect,
239 .id_table = storage_usb_ids,
240};
241
242/*
243 * fill_inquiry_response takes an unsigned char array (which must
244 * be at least 36 characters) and populates the vendor name,
245 * product name, and revision fields. Then the array is copied
246 * into the SCSI command's response buffer (oddly enough
247 * called request_buffer). data_len contains the length of the
248 * data array, which again must be at least 36.
249 */
250
251void fill_inquiry_response(struct us_data *us, unsigned char *data,
252 unsigned int data_len)
253{
254 if (data_len<36) // You lose.
255 return;
256
257 if(data[0]&0x20) { /* USB device currently not connected. Return
258 peripheral qualifier 001b ("...however, the
259 physical device is not currently connected
260 to this logical unit") and leave vendor and
261 product identification empty. ("If the target
262 does store some of the INQUIRY data on the
263 device, it may return zeros or ASCII spaces
264 (20h) in those fields until the data is
265 available from the device."). */
266 memset(data+8,0,28);
267 } else {
268 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
269 memcpy(data+8, us->unusual_dev->vendorName,
270 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
271 strlen(us->unusual_dev->vendorName));
272 memcpy(data+16, us->unusual_dev->productName,
273 strlen(us->unusual_dev->productName) > 16 ? 16 :
274 strlen(us->unusual_dev->productName));
275 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
276 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
277 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
278 data[35] = 0x30 + ((bcdDevice) & 0x0F);
279 }
280
281 usb_stor_set_xfer_buf(data, data_len, us->srb);
282}
283
284static int usb_stor_control_thread(void * __us)
285{
286 struct us_data *us = (struct us_data *)__us;
287 struct Scsi_Host *host = us_to_host(us);
288
289 lock_kernel();
290
291 /*
292 * This thread doesn't need any user-level access,
293 * so get rid of all our resources.
294 */
295 daemonize("usb-storage");
296 current->flags |= PF_NOFREEZE;
297 unlock_kernel();
298
299 /* acquire a reference to the host, so it won't be deallocated
300 * until we're ready to exit */
301 scsi_host_get(host);
302
303 /* signal that we've started the thread */
304 complete(&(us->notify));
305
306 for(;;) {
307 US_DEBUGP("*** thread sleeping.\n");
308 if(down_interruptible(&us->sema))
309 break;
310
311 US_DEBUGP("*** thread awakened.\n");
312
313 /* lock the device pointers */
314 down(&(us->dev_semaphore));
315
316 /* if the device has disconnected, we are free to exit */
317 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
318 US_DEBUGP("-- exiting\n");
319 up(&(us->dev_semaphore));
320 break;
321 }
322
323 /* lock access to the state */
324 scsi_lock(host);
325
326 /* has the command timed out *already* ? */
327 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
328 us->srb->result = DID_ABORT << 16;
329 goto SkipForAbort;
330 }
331
332 scsi_unlock(host);
333
334 /* reject the command if the direction indicator
335 * is UNKNOWN
336 */
337 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
338 US_DEBUGP("UNKNOWN data direction\n");
339 us->srb->result = DID_ERROR << 16;
340 }
341
342 /* reject if target != 0 or if LUN is higher than
343 * the maximum known LUN
344 */
345 else if (us->srb->device->id &&
346 !(us->flags & US_FL_SCM_MULT_TARG)) {
347 US_DEBUGP("Bad target number (%d:%d)\n",
348 us->srb->device->id, us->srb->device->lun);
349 us->srb->result = DID_BAD_TARGET << 16;
350 }
351
352 else if (us->srb->device->lun > us->max_lun) {
353 US_DEBUGP("Bad LUN (%d:%d)\n",
354 us->srb->device->id, us->srb->device->lun);
355 us->srb->result = DID_BAD_TARGET << 16;
356 }
357
358 /* Handle those devices which need us to fake
359 * their inquiry data */
360 else if ((us->srb->cmnd[0] == INQUIRY) &&
361 (us->flags & US_FL_FIX_INQUIRY)) {
362 unsigned char data_ptr[36] = {
363 0x00, 0x80, 0x02, 0x02,
364 0x1F, 0x00, 0x00, 0x00};
365
366 US_DEBUGP("Faking INQUIRY command\n");
367 fill_inquiry_response(us, data_ptr, 36);
368 us->srb->result = SAM_STAT_GOOD;
369 }
370
371 /* we've got a command, let's do it! */
372 else {
373 US_DEBUG(usb_stor_show_command(us->srb));
374 us->proto_handler(us->srb, us);
375 }
376
377 /* lock access to the state */
378 scsi_lock(host);
379
380 /* indicate that the command is done */
381 if (us->srb->result != DID_ABORT << 16) {
382 US_DEBUGP("scsi cmd done, result=0x%x\n",
383 us->srb->result);
384 us->srb->scsi_done(us->srb);
385 } else {
386SkipForAbort:
387 US_DEBUGP("scsi command aborted\n");
388 }
389
390 /* If an abort request was received we need to signal that
391 * the abort has finished. The proper test for this is
392 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
393 * a timeout/abort request might be received after all the
394 * USB processing was complete. */
395 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags))
396 complete(&(us->notify));
397
398 /* finished working on this command */
399 us->srb = NULL;
400 scsi_unlock(host);
401
402 /* unlock the device pointers */
403 up(&(us->dev_semaphore));
404 } /* for (;;) */
405
406 scsi_host_put(host);
407
408 /* notify the exit routine that we're actually exiting now
409 *
410 * complete()/wait_for_completion() is similar to up()/down(),
411 * except that complete() is safe in the case where the structure
412 * is getting deleted in a parallel mode of execution (i.e. just
413 * after the down() -- that's necessary for the thread-shutdown
414 * case.
415 *
416 * complete_and_exit() goes even further than this -- it is safe in
417 * the case that the thread of the caller is going away (not just
418 * the structure) -- this is necessary for the module-remove case.
419 * This is important in preemption kernels, which transfer the flow
420 * of execution immediately upon a complete().
421 */
422 complete_and_exit(&threads_gone, 0);
423}
424
425/***********************************************************************
426 * Device probing and disconnecting
427 ***********************************************************************/
428
429/* Associate our private data with the USB device */
430static int associate_dev(struct us_data *us, struct usb_interface *intf)
431{
432 US_DEBUGP("-- %s\n", __FUNCTION__);
433
434 /* Fill in the device-related fields */
435 us->pusb_dev = interface_to_usbdev(intf);
436 us->pusb_intf = intf;
437 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
438 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
439 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
440 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
441 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
442 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
443 intf->cur_altsetting->desc.bInterfaceSubClass,
444 intf->cur_altsetting->desc.bInterfaceProtocol);
445
446 /* Store our private data in the interface */
447 usb_set_intfdata(intf, us);
448
449 /* Allocate the device-related DMA-mapped buffers */
450 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
451 GFP_KERNEL, &us->cr_dma);
452 if (!us->cr) {
453 US_DEBUGP("usb_ctrlrequest allocation failed\n");
454 return -ENOMEM;
455 }
456
457 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
458 GFP_KERNEL, &us->iobuf_dma);
459 if (!us->iobuf) {
460 US_DEBUGP("I/O buffer allocation failed\n");
461 return -ENOMEM;
462 }
463 return 0;
464}
465
466/* Get the unusual_devs entries and the string descriptors */
467static void get_device_info(struct us_data *us, int id_index)
468{
469 struct usb_device *dev = us->pusb_dev;
470 struct usb_interface_descriptor *idesc =
471 &us->pusb_intf->cur_altsetting->desc;
472 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
473 struct usb_device_id *id = &storage_usb_ids[id_index];
474
475 /* Store the entries */
476 us->unusual_dev = unusual_dev;
477 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
478 idesc->bInterfaceSubClass :
479 unusual_dev->useProtocol;
480 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
481 idesc->bInterfaceProtocol :
482 unusual_dev->useTransport;
483 us->flags = unusual_dev->flags;
484
485 /*
486 * This flag is only needed when we're in high-speed, so let's
487 * disable it if we're in full-speed
488 */
489 if (dev->speed != USB_SPEED_HIGH)
490 us->flags &= ~US_FL_GO_SLOW;
491
492 /* Log a message if a non-generic unusual_dev entry contains an
493 * unnecessary subclass or protocol override. This may stimulate
494 * reports from users that will help us remove unneeded entries
495 * from the unusual_devs.h table.
496 */
497 if (id->idVendor || id->idProduct) {
498 static char *msgs[3] = {
499 "an unneeded SubClass entry",
500 "an unneeded Protocol entry",
501 "unneeded SubClass and Protocol entries"};
502 struct usb_device_descriptor *ddesc = &dev->descriptor;
503 int msg = -1;
504
505 if (unusual_dev->useProtocol != US_SC_DEVICE &&
506 us->subclass == idesc->bInterfaceSubClass)
507 msg += 1;
508 if (unusual_dev->useTransport != US_PR_DEVICE &&
509 us->protocol == idesc->bInterfaceProtocol)
510 msg += 2;
511 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
512 printk(KERN_NOTICE USB_STORAGE "This device "
513 "(%04x,%04x,%04x S %02x P %02x)"
514 " has %s in unusual_devs.h\n"
515 " Please send a copy of this message to "
516 "<linux-usb-devel@lists.sourceforge.net>\n",
517 le16_to_cpu(ddesc->idVendor),
518 le16_to_cpu(ddesc->idProduct),
519 le16_to_cpu(ddesc->bcdDevice),
520 idesc->bInterfaceSubClass,
521 idesc->bInterfaceProtocol,
522 msgs[msg]);
523 }
524}
525
526/* Get the transport settings */
527static int get_transport(struct us_data *us)
528{
529 switch (us->protocol) {
530 case US_PR_CB:
531 us->transport_name = "Control/Bulk";
532 us->transport = usb_stor_CB_transport;
533 us->transport_reset = usb_stor_CB_reset;
534 us->max_lun = 7;
535 break;
536
537 case US_PR_CBI:
538 us->transport_name = "Control/Bulk/Interrupt";
539 us->transport = usb_stor_CBI_transport;
540 us->transport_reset = usb_stor_CB_reset;
541 us->max_lun = 7;
542 break;
543
544 case US_PR_BULK:
545 us->transport_name = "Bulk";
546 us->transport = usb_stor_Bulk_transport;
547 us->transport_reset = usb_stor_Bulk_reset;
548 break;
549
550#ifdef CONFIG_USB_STORAGE_USBAT
551 case US_PR_SCM_ATAPI:
552 us->transport_name = "SCM/ATAPI";
553 us->transport = usbat_transport;
554 us->transport_reset = usb_stor_CB_reset;
555 us->max_lun = 1;
556 break;
557#endif
558
559#ifdef CONFIG_USB_STORAGE_SDDR09
560 case US_PR_EUSB_SDDR09:
561 us->transport_name = "EUSB/SDDR09";
562 us->transport = sddr09_transport;
563 us->transport_reset = usb_stor_CB_reset;
564 us->max_lun = 0;
565 break;
566#endif
567
568#ifdef CONFIG_USB_STORAGE_SDDR55
569 case US_PR_SDDR55:
570 us->transport_name = "SDDR55";
571 us->transport = sddr55_transport;
572 us->transport_reset = sddr55_reset;
573 us->max_lun = 0;
574 break;
575#endif
576
577#ifdef CONFIG_USB_STORAGE_DPCM
578 case US_PR_DPCM_USB:
579 us->transport_name = "Control/Bulk-EUSB/SDDR09";
580 us->transport = dpcm_transport;
581 us->transport_reset = usb_stor_CB_reset;
582 us->max_lun = 1;
583 break;
584#endif
585
586#ifdef CONFIG_USB_STORAGE_FREECOM
587 case US_PR_FREECOM:
588 us->transport_name = "Freecom";
589 us->transport = freecom_transport;
590 us->transport_reset = usb_stor_freecom_reset;
591 us->max_lun = 0;
592 break;
593#endif
594
595#ifdef CONFIG_USB_STORAGE_DATAFAB
596 case US_PR_DATAFAB:
597 us->transport_name = "Datafab Bulk-Only";
598 us->transport = datafab_transport;
599 us->transport_reset = usb_stor_Bulk_reset;
600 us->max_lun = 1;
601 break;
602#endif
603
604#ifdef CONFIG_USB_STORAGE_JUMPSHOT
605 case US_PR_JUMPSHOT:
606 us->transport_name = "Lexar Jumpshot Control/Bulk";
607 us->transport = jumpshot_transport;
608 us->transport_reset = usb_stor_Bulk_reset;
609 us->max_lun = 1;
610 break;
611#endif
612
613 default:
614 return -EIO;
615 }
616 US_DEBUGP("Transport: %s\n", us->transport_name);
617
618 /* fix for single-lun devices */
619 if (us->flags & US_FL_SINGLE_LUN)
620 us->max_lun = 0;
621 return 0;
622}
623
624/* Get the protocol settings */
625static int get_protocol(struct us_data *us)
626{
627 switch (us->subclass) {
628 case US_SC_RBC:
629 us->protocol_name = "Reduced Block Commands (RBC)";
630 us->proto_handler = usb_stor_transparent_scsi_command;
631 break;
632
633 case US_SC_8020:
634 us->protocol_name = "8020i";
635 us->proto_handler = usb_stor_ATAPI_command;
636 us->max_lun = 0;
637 break;
638
639 case US_SC_QIC:
640 us->protocol_name = "QIC-157";
641 us->proto_handler = usb_stor_qic157_command;
642 us->max_lun = 0;
643 break;
644
645 case US_SC_8070:
646 us->protocol_name = "8070i";
647 us->proto_handler = usb_stor_ATAPI_command;
648 us->max_lun = 0;
649 break;
650
651 case US_SC_SCSI:
652 us->protocol_name = "Transparent SCSI";
653 us->proto_handler = usb_stor_transparent_scsi_command;
654 break;
655
656 case US_SC_UFI:
657 us->protocol_name = "Uniform Floppy Interface (UFI)";
658 us->proto_handler = usb_stor_ufi_command;
659 break;
660
661#ifdef CONFIG_USB_STORAGE_ISD200
662 case US_SC_ISD200:
663 us->protocol_name = "ISD200 ATA/ATAPI";
664 us->proto_handler = isd200_ata_command;
665 break;
666#endif
667
668 default:
669 return -EIO;
670 }
671 US_DEBUGP("Protocol: %s\n", us->protocol_name);
672 return 0;
673}
674
675/* Get the pipe settings */
676static int get_pipes(struct us_data *us)
677{
678 struct usb_host_interface *altsetting =
679 us->pusb_intf->cur_altsetting;
680 int i;
681 struct usb_endpoint_descriptor *ep;
682 struct usb_endpoint_descriptor *ep_in = NULL;
683 struct usb_endpoint_descriptor *ep_out = NULL;
684 struct usb_endpoint_descriptor *ep_int = NULL;
685
686 /*
687 * Find the endpoints we need.
688 * We are expecting a minimum of 2 endpoints - in and out (bulk).
689 * An optional interrupt is OK (necessary for CBI protocol).
690 * We will ignore any others.
691 */
692 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
693 ep = &altsetting->endpoint[i].desc;
694
695 /* Is it a BULK endpoint? */
696 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
697 == USB_ENDPOINT_XFER_BULK) {
698 /* BULK in or out? */
699 if (ep->bEndpointAddress & USB_DIR_IN)
700 ep_in = ep;
701 else
702 ep_out = ep;
703 }
704
705 /* Is it an interrupt endpoint? */
706 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
707 == USB_ENDPOINT_XFER_INT) {
708 ep_int = ep;
709 }
710 }
711
712 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
713 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
714 return -EIO;
715 }
716
717 /* Calculate and store the pipe values */
718 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
719 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
720 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
721 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
722 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
723 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
724 if (ep_int) {
725 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
726 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
727 us->ep_bInterval = ep_int->bInterval;
728 }
729 return 0;
730}
731
732/* Initialize all the dynamic resources we need */
733static int usb_stor_acquire_resources(struct us_data *us)
734{
735 int p;
736
737 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
738 if (!us->current_urb) {
739 US_DEBUGP("URB allocation failed\n");
740 return -ENOMEM;
741 }
742
743 /* Lock the device while we carry out the next two operations */
744 down(&us->dev_semaphore);
745
746 /* For bulk-only devices, determine the max LUN value */
747 if (us->protocol == US_PR_BULK) {
748 p = usb_stor_Bulk_max_lun(us);
749 if (p < 0) {
750 up(&us->dev_semaphore);
751 return p;
752 }
753 us->max_lun = p;
754 }
755
756 /* Just before we start our control thread, initialize
757 * the device if it needs initialization */
758 if (us->unusual_dev->initFunction)
759 us->unusual_dev->initFunction(us);
760
761 up(&us->dev_semaphore);
762
763 /* Start up our control thread */
764 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
765 if (p < 0) {
766 printk(KERN_WARNING USB_STORAGE
767 "Unable to start control thread\n");
768 return p;
769 }
770 us->pid = p;
771 atomic_inc(&total_threads);
772
773 /* Wait for the thread to start */
774 wait_for_completion(&(us->notify));
775
776 return 0;
777}
778
779/* Release all our dynamic resources */
780static void usb_stor_release_resources(struct us_data *us)
781{
782 US_DEBUGP("-- %s\n", __FUNCTION__);
783
784 /* Tell the control thread to exit. The SCSI host must
785 * already have been removed so it won't try to queue
786 * any more commands.
787 */
788 US_DEBUGP("-- sending exit command to thread\n");
789 up(&us->sema);
790
791 /* Call the destructor routine, if it exists */
792 if (us->extra_destructor) {
793 US_DEBUGP("-- calling extra_destructor()\n");
794 us->extra_destructor(us->extra);
795 }
796
797 /* Free the extra data and the URB */
798 kfree(us->extra);
799 usb_free_urb(us->current_urb);
800}
801
802/* Dissociate from the USB device */
803static void dissociate_dev(struct us_data *us)
804{
805 US_DEBUGP("-- %s\n", __FUNCTION__);
806
807 /* Free the device-related DMA-mapped buffers */
808 if (us->cr)
809 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
810 us->cr_dma);
811 if (us->iobuf)
812 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
813 us->iobuf_dma);
814
815 /* Remove our private data from the interface */
816 usb_set_intfdata(us->pusb_intf, NULL);
817}
818
819/* Thread to carry out delayed SCSI-device scanning */
820static int usb_stor_scan_thread(void * __us)
821{
822 struct us_data *us = (struct us_data *)__us;
823
824 /*
825 * This thread doesn't need any user-level access,
826 * so get rid of all our resources.
827 */
828 lock_kernel();
829 daemonize("usb-stor-scan");
830 unlock_kernel();
831
832 /* Acquire a reference to the host, so it won't be deallocated
833 * until we're ready to exit */
834 scsi_host_get(us_to_host(us));
835
836 /* Signal that we've started the thread */
837 complete(&(us->notify));
838
839 printk(KERN_DEBUG
840 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
841
842 /* Wait for the timeout to expire or for a disconnect */
843 if (delay_use > 0) {
844 printk(KERN_DEBUG "usb-storage: waiting for device "
845 "to settle before scanning\n");
846retry:
847 wait_event_interruptible_timeout(us->delay_wait,
848 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
849 delay_use * HZ);
850 if (current->flags & PF_FREEZE) {
851 refrigerator(PF_FREEZE);
852 goto retry;
853 }
854 }
855
856 /* If the device is still connected, perform the scanning */
857 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
858 scsi_scan_host(us_to_host(us));
859 printk(KERN_DEBUG "usb-storage: device scan complete\n");
860
861 /* Should we unbind if no devices were detected? */
862 }
863
864 scsi_host_put(us_to_host(us));
865 complete_and_exit(&threads_gone, 0);
866}
867
868
869/* Probe to see if we can drive a newly-connected USB device */
870static int storage_probe(struct usb_interface *intf,
871 const struct usb_device_id *id)
872{
873 struct Scsi_Host *host;
874 struct us_data *us;
875 const int id_index = id - storage_usb_ids;
876 int result;
877
878 US_DEBUGP("USB Mass Storage device detected\n");
879
880 /*
881 * Ask the SCSI layer to allocate a host structure, with extra
882 * space at the end for our private us_data structure.
883 */
884 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
885 if (!host) {
886 printk(KERN_WARNING USB_STORAGE
887 "Unable to allocate the scsi host\n");
888 return -ENOMEM;
889 }
890
891 us = host_to_us(host);
892 memset(us, 0, sizeof(struct us_data));
893 init_MUTEX(&(us->dev_semaphore));
894 init_MUTEX_LOCKED(&(us->sema));
895 init_completion(&(us->notify));
896 init_waitqueue_head(&us->delay_wait);
897
898 /* Associate the us_data structure with the USB device */
899 result = associate_dev(us, intf);
900 if (result)
901 goto BadDevice;
902
903 /*
904 * Get the unusual_devs entries and the descriptors
905 *
906 * id_index is calculated in the declaration to be the index number
907 * of the match from the usb_device_id table, so we can find the
908 * corresponding entry in the private table.
909 */
910 get_device_info(us, id_index);
911
912#ifdef CONFIG_USB_STORAGE_SDDR09
913 if (us->protocol == US_PR_EUSB_SDDR09 ||
914 us->protocol == US_PR_DPCM_USB) {
915 /* set the configuration -- STALL is an acceptable response here */
916 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
917 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
918 ->actconfig->desc.bConfigurationValue);
919 goto BadDevice;
920 }
921 result = usb_reset_configuration(us->pusb_dev);
922
923 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
924 if (result == -EPIPE) {
925 US_DEBUGP("-- stall on control interface\n");
926 } else if (result != 0) {
927 /* it's not a stall, but another error -- time to bail */
928 US_DEBUGP("-- Unknown error. Rejecting device\n");
929 goto BadDevice;
930 }
931 }
932#endif
933
934 /* Get the transport, protocol, and pipe settings */
935 result = get_transport(us);
936 if (result)
937 goto BadDevice;
938 result = get_protocol(us);
939 if (result)
940 goto BadDevice;
941 result = get_pipes(us);
942 if (result)
943 goto BadDevice;
944
945 /* Acquire all the other resources and add the host */
946 result = usb_stor_acquire_resources(us);
947 if (result)
948 goto BadDevice;
949 result = scsi_add_host(host, &intf->dev);
950 if (result) {
951 printk(KERN_WARNING USB_STORAGE
952 "Unable to add the scsi host\n");
953 goto BadDevice;
954 }
955
956 /* Start up the thread for delayed SCSI-device scanning */
957 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
958 if (result < 0) {
959 printk(KERN_WARNING USB_STORAGE
960 "Unable to start the device-scanning thread\n");
961 scsi_remove_host(host);
962 goto BadDevice;
963 }
964 atomic_inc(&total_threads);
965
966 /* Wait for the thread to start */
967 wait_for_completion(&(us->notify));
968
969 return 0;
970
971 /* We come here if there are any problems */
972BadDevice:
973 US_DEBUGP("storage_probe() failed\n");
974 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
975 usb_stor_release_resources(us);
976 dissociate_dev(us);
977 scsi_host_put(host);
978 return result;
979}
980
981/* Handle a disconnect event from the USB core */
982static void storage_disconnect(struct usb_interface *intf)
983{
984 struct us_data *us = usb_get_intfdata(intf);
985
986 US_DEBUGP("storage_disconnect() called\n");
987
988 /* Prevent new USB transfers, stop the current command, and
989 * interrupt a SCSI-scan or device-reset delay */
990 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
991 usb_stor_stop_transport(us);
992 wake_up(&us->delay_wait);
993
994 /* It doesn't matter if the SCSI-scanning thread is still running.
995 * The thread will exit when it sees the DISCONNECTING flag. */
996
997 /* Wait for the current command to finish, then remove the host */
998 down(&us->dev_semaphore);
999 up(&us->dev_semaphore);
1000 scsi_remove_host(us_to_host(us));
1001
1002 /* Wait for everything to become idle and release all our resources */
1003 usb_stor_release_resources(us);
1004 dissociate_dev(us);
1005
1006 /* Drop our reference to the host; the SCSI core will free it
1007 * (and "us" along with it) when the refcount becomes 0. */
1008 scsi_host_put(us_to_host(us));
1009}
1010
1011/***********************************************************************
1012 * Initialization and registration
1013 ***********************************************************************/
1014
1015static int __init usb_stor_init(void)
1016{
1017 int retval;
1018 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1019
1020 /* register the driver, return usb_register return code if error */
1021 retval = usb_register(&usb_storage_driver);
1022 if (retval == 0)
1023 printk(KERN_INFO "USB Mass Storage support registered.\n");
1024
1025 return retval;
1026}
1027
1028static void __exit usb_stor_exit(void)
1029{
1030 US_DEBUGP("usb_stor_exit() called\n");
1031
1032 /* Deregister the driver
1033 * This will cause disconnect() to be called for each
1034 * attached unit
1035 */
1036 US_DEBUGP("-- calling usb_deregister()\n");
1037 usb_deregister(&usb_storage_driver) ;
1038
1039 /* Don't return until all of our control and scanning threads
1040 * have exited. Since each thread signals threads_gone as its
1041 * last act, we have to call wait_for_completion the right number
1042 * of times.
1043 */
1044 while (atomic_read(&total_threads) > 0) {
1045 wait_for_completion(&threads_gone);
1046 atomic_dec(&total_threads);
1047 }
1048}
1049
1050module_init(usb_stor_init);
1051module_exit(usb_stor_exit);
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
new file mode 100644
index 000000000000..625b7aa98074
--- /dev/null
+++ b/drivers/usb/storage/usb.h
@@ -0,0 +1,204 @@
1/* Driver for USB Mass Storage compliant devices
2 * Main Header File
3 *
4 * $Id: usb.h,v 1.21 2002/04/21 02:57:59 mdharm Exp $
5 *
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Initial work by:
10 * (c) 1999 Michael Gee (michael@linuxspecific.com)
11 *
12 * This driver is based on the 'USB Mass Storage Class' document. This
13 * describes in detail the protocol used to communicate with such
14 * devices. Clearly, the designers had SCSI and ATAPI commands in
15 * mind when they created this document. The commands are all very
16 * similar to commands in the SCSI-II and ATAPI specifications.
17 *
18 * It is important to note that in a number of cases this class
19 * exhibits class-specific exemptions from the USB specification.
20 * Notably the usage of NAK, STALL and ACK differs from the norm, in
21 * that they are used to communicate wait, failed and OK on commands.
22 *
23 * Also, for certain devices, the interrupt endpoint is used to convey
24 * status of a command.
25 *
26 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
27 * information about this driver.
28 *
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License as published by the
31 * Free Software Foundation; either version 2, or (at your option) any
32 * later version.
33 *
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License along
40 * with this program; if not, write to the Free Software Foundation, Inc.,
41 * 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#ifndef _USB_H_
45#define _USB_H_
46
47#include <linux/usb.h>
48#include <linux/blkdev.h>
49#include <linux/smp_lock.h>
50#include <linux/completion.h>
51#include <scsi/scsi_host.h>
52
53struct us_data;
54struct scsi_cmnd;
55
56/*
57 * Unusual device list definitions
58 */
59
60struct us_unusual_dev {
61 const char* vendorName;
62 const char* productName;
63 __u8 useProtocol;
64 __u8 useTransport;
65 int (*initFunction)(struct us_data *);
66 unsigned int flags;
67};
68
69/*
70 * Static flag definitions. We use this roundabout technique so that the
71 * proc_info() routine can automatically display a message for each flag.
72 */
73#define US_DO_ALL_FLAGS \
74 US_FLAG(SINGLE_LUN, 0x00000001) \
75 /* allow access to only LUN 0 */ \
76 US_FLAG(NEED_OVERRIDE, 0x00000002) \
77 /* unusual_devs entry is necessary */ \
78 US_FLAG(SCM_MULT_TARG, 0x00000004) \
79 /* supports multiple targets */ \
80 US_FLAG(FIX_INQUIRY, 0x00000008) \
81 /* INQUIRY response needs faking */ \
82 US_FLAG(FIX_CAPACITY, 0x00000010) \
83 /* READ CAPACITY response too big */ \
84 US_FLAG(IGNORE_RESIDUE, 0x00000020) \
85 /* reported residue is wrong */ \
86 US_FLAG(BULK32, 0x00000040) \
87 /* Uses 32-byte CBW length */ \
88 US_FLAG(NOT_LOCKABLE, 0x00000080) \
89 /* PREVENT/ALLOW not supported */ \
90 US_FLAG(GO_SLOW, 0x00000100) \
91 /* Need delay after Command phase */ \
92 US_FLAG(NO_WP_DETECT, 0x00000200) \
93 /* Don't check for write-protect */ \
94
95#define US_FLAG(name, value) US_FL_##name = value ,
96enum { US_DO_ALL_FLAGS };
97#undef US_FLAG
98
99/* Dynamic flag definitions: used in set_bit() etc. */
100#define US_FLIDX_URB_ACTIVE 18 /* 0x00040000 current_urb is in use */
101#define US_FLIDX_SG_ACTIVE 19 /* 0x00080000 current_sg is in use */
102#define US_FLIDX_ABORTING 20 /* 0x00100000 abort is in progress */
103#define US_FLIDX_DISCONNECTING 21 /* 0x00200000 disconnect in progress */
104#define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \
105 (1UL << US_FLIDX_DISCONNECTING))
106#define US_FLIDX_RESETTING 22 /* 0x00400000 device reset in progress */
107#define US_FLIDX_TIMED_OUT 23 /* 0x00800000 SCSI midlayer timed out */
108
109
110#define USB_STOR_STRING_LEN 32
111
112/*
113 * We provide a DMA-mapped I/O buffer for use with small USB transfers.
114 * It turns out that CB[I] needs a 12-byte buffer and Bulk-only needs a
115 * 31-byte buffer. But Freecom needs a 64-byte buffer, so that's the
116 * size we'll allocate.
117 */
118
119#define US_IOBUF_SIZE 64 /* Size of the DMA-mapped I/O buffer */
120
121typedef int (*trans_cmnd)(struct scsi_cmnd *, struct us_data*);
122typedef int (*trans_reset)(struct us_data*);
123typedef void (*proto_cmnd)(struct scsi_cmnd*, struct us_data*);
124typedef void (*extra_data_destructor)(void *); /* extra data destructor */
125
126/* we allocate one of these for every device that we remember */
127struct us_data {
128 /* The device we're working with
129 * It's important to note:
130 * (o) you must hold dev_semaphore to change pusb_dev
131 */
132 struct semaphore dev_semaphore; /* protect pusb_dev */
133 struct usb_device *pusb_dev; /* this usb_device */
134 struct usb_interface *pusb_intf; /* this interface */
135 struct us_unusual_dev *unusual_dev; /* device-filter entry */
136 unsigned long flags; /* from filter initially */
137 unsigned int send_bulk_pipe; /* cached pipe values */
138 unsigned int recv_bulk_pipe;
139 unsigned int send_ctrl_pipe;
140 unsigned int recv_ctrl_pipe;
141 unsigned int recv_intr_pipe;
142
143 /* information about the device */
144 char *transport_name;
145 char *protocol_name;
146 __le32 bcs_signature;
147 u8 subclass;
148 u8 protocol;
149 u8 max_lun;
150
151 u8 ifnum; /* interface number */
152 u8 ep_bInterval; /* interrupt interval */
153
154 /* function pointers for this device */
155 trans_cmnd transport; /* transport function */
156 trans_reset transport_reset; /* transport device reset */
157 proto_cmnd proto_handler; /* protocol handler */
158
159 /* SCSI interfaces */
160 struct scsi_cmnd *srb; /* current srb */
161
162 /* thread information */
163 int pid; /* control thread */
164
165 /* control and bulk communications data */
166 struct urb *current_urb; /* USB requests */
167 struct usb_ctrlrequest *cr; /* control requests */
168 struct usb_sg_request current_sg; /* scatter-gather req. */
169 unsigned char *iobuf; /* I/O buffer */
170 dma_addr_t cr_dma; /* buffer DMA addresses */
171 dma_addr_t iobuf_dma;
172
173 /* mutual exclusion and synchronization structures */
174 struct semaphore sema; /* to sleep thread on */
175 struct completion notify; /* thread begin/end */
176 wait_queue_head_t delay_wait; /* wait during scan, reset */
177
178 /* subdriver information */
179 void *extra; /* Any extra data */
180 extra_data_destructor extra_destructor;/* extra data destructor */
181};
182
183/* Convert between us_data and the corresponding Scsi_Host */
184static struct Scsi_Host inline *us_to_host(struct us_data *us) {
185 return container_of((void *) us, struct Scsi_Host, hostdata);
186}
187static struct us_data inline *host_to_us(struct Scsi_Host *host) {
188 return (struct us_data *) host->hostdata;
189}
190
191/* Function to fill an inquiry response. See usb.c for details */
192extern void fill_inquiry_response(struct us_data *us,
193 unsigned char *data, unsigned int data_len);
194
195/* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
196 * single queue element srb for write access */
197#define scsi_unlock(host) spin_unlock_irq(host->host_lock)
198#define scsi_lock(host) spin_lock_irq(host->host_lock)
199
200
201/* Vendor ID list for devices that require special handling */
202#define USB_VENDOR_ID_GENESYS 0x05e3 /* Genesys Logic */
203
204#endif