USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / storage / isd200.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
f0183a33
FB
2/*
3 * Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
1da177e4
LT
4 *
5 * Current development and maintenance:
96de0e25 6 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
1da177e4
LT
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>
32d5493e 49#include <linux/module.h>
1da177e4 50#include <linux/slab.h>
d8881cda 51#include <linux/ata.h>
1da177e4 52#include <linux/hdreg.h>
48c23d3e 53#include <linux/scatterlist.h>
1da177e4
LT
54
55#include <scsi/scsi.h>
56#include <scsi/scsi_cmnd.h>
57#include <scsi/scsi_device.h>
58
59#include "usb.h"
60#include "transport.h"
61#include "protocol.h"
62#include "debug.h"
63#include "scsiglue.h"
32d5493e 64
aa519be3
AM
65#define DRV_NAME "ums-isd200"
66
4246b06a 67MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
d36b6910 68MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
4246b06a 69MODULE_LICENSE("GPL");
32d5493e
AS
70
71static int isd200_Initialization(struct us_data *us);
72
73
74/*
75 * The table of devices
76 */
77#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
78 vendorName, productName, useProtocol, useTransport, \
79 initFunction, flags) \
80{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
f61870ee 81 .driver_info = (flags) }
32d5493e 82
e7724baf 83static struct usb_device_id isd200_usb_ids[] = {
32d5493e
AS
84# include "unusual_isd200.h"
85 { } /* Terminating entry */
86};
87MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
88
89#undef UNUSUAL_DEV
32d5493e
AS
90
91/*
92 * The flags table
93 */
94#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
95 vendor_name, product_name, use_protocol, use_transport, \
96 init_function, Flags) \
97{ \
98 .vendorName = vendor_name, \
99 .productName = product_name, \
100 .useProtocol = use_protocol, \
101 .useTransport = use_transport, \
102 .initFunction = init_function, \
103}
104
105static struct us_unusual_dev isd200_unusual_dev_list[] = {
106# include "unusual_isd200.h"
107 { } /* Terminating entry */
108};
109
110#undef UNUSUAL_DEV
1da177e4
LT
111
112/* Timeout defines (in Seconds) */
113
114#define ISD200_ENUM_BSY_TIMEOUT 35
115#define ISD200_ENUM_DETECT_TIMEOUT 30
116#define ISD200_DEFAULT_TIMEOUT 30
117
118/* device flags */
119#define DF_ATA_DEVICE 0x0001
120#define DF_MEDIA_STATUS_ENABLED 0x0002
121#define DF_REMOVABLE_MEDIA 0x0004
122
123/* capability bit definitions */
124#define CAPABILITY_DMA 0x01
125#define CAPABILITY_LBA 0x02
126
127/* command_setX bit definitions */
128#define COMMANDSET_REMOVABLE 0x02
129#define COMMANDSET_MEDIA_STATUS 0x10
130
131/* ATA Vendor Specific defines */
132#define ATA_ADDRESS_DEVHEAD_STD 0xa0
133#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
134#define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
135
136/* Action Select bits */
137#define ACTION_SELECT_0 0x01
138#define ACTION_SELECT_1 0x02
139#define ACTION_SELECT_2 0x04
140#define ACTION_SELECT_3 0x08
141#define ACTION_SELECT_4 0x10
142#define ACTION_SELECT_5 0x20
143#define ACTION_SELECT_6 0x40
144#define ACTION_SELECT_7 0x80
145
146/* Register Select bits */
147#define REG_ALTERNATE_STATUS 0x01
148#define REG_DEVICE_CONTROL 0x01
149#define REG_ERROR 0x02
150#define REG_FEATURES 0x02
151#define REG_SECTOR_COUNT 0x04
152#define REG_SECTOR_NUMBER 0x08
153#define REG_CYLINDER_LOW 0x10
154#define REG_CYLINDER_HIGH 0x20
155#define REG_DEVICE_HEAD 0x40
156#define REG_STATUS 0x80
157#define REG_COMMAND 0x80
158
c74e8095
BZ
159/* ATA registers offset definitions */
160#define ATA_REG_ERROR_OFFSET 1
161#define ATA_REG_LCYL_OFFSET 4
162#define ATA_REG_HCYL_OFFSET 5
163#define ATA_REG_STATUS_OFFSET 7
164
1da177e4
LT
165/* ATA error definitions not in <linux/hdreg.h> */
166#define ATA_ERROR_MEDIA_CHANGE 0x20
167
168/* ATA command definitions not in <linux/hdreg.h> */
169#define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
170#define ATA_COMMAND_MEDIA_EJECT 0xED
171
172/* ATA drive control definitions */
173#define ATA_DC_DISABLE_INTERRUPTS 0x02
174#define ATA_DC_RESET_CONTROLLER 0x04
175#define ATA_DC_REENABLE_CONTROLLER 0x00
176
177/*
178 * General purpose return codes
179 */
180
181#define ISD200_ERROR -1
182#define ISD200_GOOD 0
183
184/*
185 * Transport return codes
186 */
187
188#define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
189#define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
190#define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
191
192/* driver action codes */
193#define ACTION_READ_STATUS 0
194#define ACTION_RESET 1
195#define ACTION_REENABLE 2
196#define ACTION_SOFT_RESET 3
197#define ACTION_ENUM 4
198#define ACTION_IDENTIFY 5
199
200
201/*
202 * ata_cdb struct
203 */
204
205
206union ata_cdb {
207 struct {
208 unsigned char SignatureByte0;
209 unsigned char SignatureByte1;
210 unsigned char ActionSelect;
211 unsigned char RegisterSelect;
212 unsigned char TransferBlockSize;
213 unsigned char WriteData3F6;
214 unsigned char WriteData1F1;
215 unsigned char WriteData1F2;
216 unsigned char WriteData1F3;
217 unsigned char WriteData1F4;
218 unsigned char WriteData1F5;
219 unsigned char WriteData1F6;
220 unsigned char WriteData1F7;
221 unsigned char Reserved[3];
222 } generic;
223
224 struct {
225 unsigned char SignatureByte0;
226 unsigned char SignatureByte1;
227 unsigned char ActionSelect;
228 unsigned char RegisterSelect;
229 unsigned char TransferBlockSize;
230 unsigned char AlternateStatusByte;
231 unsigned char ErrorByte;
232 unsigned char SectorCountByte;
233 unsigned char SectorNumberByte;
234 unsigned char CylinderLowByte;
235 unsigned char CylinderHighByte;
236 unsigned char DeviceHeadByte;
237 unsigned char StatusByte;
238 unsigned char Reserved[3];
239 } read;
240
241 struct {
242 unsigned char SignatureByte0;
243 unsigned char SignatureByte1;
244 unsigned char ActionSelect;
245 unsigned char RegisterSelect;
246 unsigned char TransferBlockSize;
247 unsigned char DeviceControlByte;
248 unsigned char FeaturesByte;
249 unsigned char SectorCountByte;
250 unsigned char SectorNumberByte;
251 unsigned char CylinderLowByte;
252 unsigned char CylinderHighByte;
253 unsigned char DeviceHeadByte;
254 unsigned char CommandByte;
255 unsigned char Reserved[3];
256 } write;
257};
258
259
260/*
261 * Inquiry data structure. This is the data returned from the target
262 * after it receives an inquiry.
263 *
264 * This structure may be extended by the number of bytes specified
265 * in the field AdditionalLength. The defined size constant only
266 * includes fields through ProductRevisionLevel.
267 */
268
269/*
270 * DeviceType field
271 */
272#define DIRECT_ACCESS_DEVICE 0x00 /* disks */
273#define DEVICE_REMOVABLE 0x80
274
275struct inquiry_data {
276 unsigned char DeviceType;
277 unsigned char DeviceTypeModifier;
278 unsigned char Versions;
279 unsigned char Format;
280 unsigned char AdditionalLength;
281 unsigned char Reserved[2];
282 unsigned char Capability;
283 unsigned char VendorId[8];
284 unsigned char ProductId[16];
285 unsigned char ProductRevisionLevel[4];
286 unsigned char VendorSpecific[20];
287 unsigned char Reserved3[40];
288} __attribute__ ((packed));
289
290/*
291 * INQUIRY data buffer size
292 */
293
294#define INQUIRYDATABUFFERSIZE 36
295
296
297/*
298 * ISD200 CONFIG data struct
299 */
300
301#define ATACFG_TIMING 0x0f
302#define ATACFG_ATAPI_RESET 0x10
303#define ATACFG_MASTER 0x20
304#define ATACFG_BLOCKSIZE 0xa0
305
306#define ATACFGE_LAST_LUN 0x07
307#define ATACFGE_DESC_OVERRIDE 0x08
308#define ATACFGE_STATE_SUSPEND 0x10
309#define ATACFGE_SKIP_BOOT 0x20
310#define ATACFGE_CONF_DESC2 0x40
311#define ATACFGE_INIT_STATUS 0x80
312
313#define CFG_CAPABILITY_SRST 0x01
314
315struct isd200_config {
316 unsigned char EventNotification;
317 unsigned char ExternalClock;
318 unsigned char ATAInitTimeout;
319 unsigned char ATAConfig;
320 unsigned char ATAMajorCommand;
321 unsigned char ATAMinorCommand;
322 unsigned char ATAExtraConfig;
323 unsigned char Capability;
324}__attribute__ ((packed));
325
326
327/*
328 * ISD200 driver information struct
329 */
330
331struct isd200_info {
332 struct inquiry_data InquiryData;
d8881cda 333 u16 *id;
1da177e4
LT
334 struct isd200_config ConfigData;
335 unsigned char *RegsBuf;
336 unsigned char ATARegs[8];
337 unsigned char DeviceHead;
338 unsigned char DeviceFlags;
339
340 /* maximum number of LUNs supported */
341 unsigned char MaxLUNs;
64a87b24 342 unsigned char cmnd[BLK_MAX_CDB];
1da177e4 343 struct scsi_cmnd srb;
48c23d3e 344 struct scatterlist sg;
1da177e4
LT
345};
346
347
348/*
349 * Read Capacity Data - returned in Big Endian format
350 */
351
352struct read_capacity_data {
353 __be32 LogicalBlockAddress;
354 __be32 BytesPerBlock;
355};
356
357/*
358 * Read Block Limits Data - returned in Big Endian format
359 * This structure returns the maximum and minimum block
360 * size for a TAPE device.
361 */
362
363struct read_block_limits {
364 unsigned char Reserved;
365 unsigned char BlockMaximumSize[3];
366 unsigned char BlockMinimumSize[2];
367};
368
369
370/*
371 * Sense Data Format
372 */
373
374#define SENSE_ERRCODE 0x7f
375#define SENSE_ERRCODE_VALID 0x80
376#define SENSE_FLAG_SENSE_KEY 0x0f
377#define SENSE_FLAG_BAD_LENGTH 0x20
378#define SENSE_FLAG_END_OF_MEDIA 0x40
379#define SENSE_FLAG_FILE_MARK 0x80
380struct sense_data {
381 unsigned char ErrorCode;
382 unsigned char SegmentNumber;
383 unsigned char Flags;
384 unsigned char Information[4];
385 unsigned char AdditionalSenseLength;
386 unsigned char CommandSpecificInformation[4];
387 unsigned char AdditionalSenseCode;
388 unsigned char AdditionalSenseCodeQualifier;
389 unsigned char FieldReplaceableUnitCode;
390 unsigned char SenseKeySpecific[3];
391} __attribute__ ((packed));
392
393/*
394 * Default request sense buffer size
395 */
396
397#define SENSE_BUFFER_SIZE 18
398
399/***********************************************************************
400 * Helper routines
401 ***********************************************************************/
402
403/**************************************************************************
404 * isd200_build_sense
405 *
406 * Builds an artificial sense buffer to report the results of a
407 * failed command.
408 *
409 * RETURNS:
410 * void
411 */
412static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
413{
414 struct isd200_info *info = (struct isd200_info *)us->extra;
415 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
c74e8095 416 unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
1da177e4
LT
417
418 if(error & ATA_ERROR_MEDIA_CHANGE) {
419 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
420 buf->AdditionalSenseLength = 0xb;
421 buf->Flags = UNIT_ATTENTION;
422 buf->AdditionalSenseCode = 0;
423 buf->AdditionalSenseCodeQualifier = 0;
fcd3ec69 424 } else if (error & ATA_MCR) {
1da177e4
LT
425 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
426 buf->AdditionalSenseLength = 0xb;
427 buf->Flags = UNIT_ATTENTION;
428 buf->AdditionalSenseCode = 0;
429 buf->AdditionalSenseCodeQualifier = 0;
fcd3ec69 430 } else if (error & ATA_TRK0NF) {
1da177e4
LT
431 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
432 buf->AdditionalSenseLength = 0xb;
433 buf->Flags = NOT_READY;
434 buf->AdditionalSenseCode = 0;
435 buf->AdditionalSenseCodeQualifier = 0;
fcd3ec69 436 } else if (error & ATA_UNC) {
1da177e4
LT
437 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
438 buf->AdditionalSenseLength = 0xb;
439 buf->Flags = DATA_PROTECT;
440 buf->AdditionalSenseCode = 0;
441 buf->AdditionalSenseCodeQualifier = 0;
442 } else {
443 buf->ErrorCode = 0;
444 buf->AdditionalSenseLength = 0;
445 buf->Flags = 0;
446 buf->AdditionalSenseCode = 0;
447 buf->AdditionalSenseCodeQualifier = 0;
448 }
449}
450
451
452/***********************************************************************
453 * Transport routines
454 ***********************************************************************/
455
48c23d3e
BH
456/**************************************************************************
457 * isd200_set_srb(), isd200_srb_set_bufflen()
458 *
459 * Two helpers to facilitate in initialization of scsi_cmnd structure
460 * Will need to change when struct scsi_cmnd changes
461 */
462static void isd200_set_srb(struct isd200_info *info,
463 enum dma_data_direction dir, void* buff, unsigned bufflen)
464{
465 struct scsi_cmnd *srb = &info->srb;
466
467 if (buff)
468 sg_init_one(&info->sg, buff, bufflen);
469
470 srb->sc_data_direction = dir;
30b0c37b
BH
471 srb->sdb.table.sgl = buff ? &info->sg : NULL;
472 srb->sdb.length = bufflen;
473 srb->sdb.table.nents = buff ? 1 : 0;
48c23d3e
BH
474}
475
476static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
477{
30b0c37b 478 srb->sdb.length = bufflen;
48c23d3e
BH
479}
480
1da177e4
LT
481
482/**************************************************************************
483 * isd200_action
484 *
485 * Routine for sending commands to the isd200
486 *
487 * RETURNS:
488 * ISD status code
489 */
490static int isd200_action( struct us_data *us, int action,
491 void* pointer, int value )
492{
493 union ata_cdb ata;
0df4d8ef
IM
494 /* static to prevent this large struct being placed on the valuable stack */
495 static struct scsi_device srb_dev;
1da177e4
LT
496 struct isd200_info *info = (struct isd200_info *)us->extra;
497 struct scsi_cmnd *srb = &info->srb;
498 int status;
499
500 memset(&ata, 0, sizeof(ata));
64a87b24 501 srb->cmnd = info->cmnd;
1da177e4 502 srb->device = &srb_dev;
1da177e4
LT
503
504 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
505 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
506 ata.generic.TransferBlockSize = 1;
507
508 switch ( action ) {
509 case ACTION_READ_STATUS:
191648d0 510 usb_stor_dbg(us, " isd200_action(READ_STATUS)\n");
1da177e4
LT
511 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
512 ata.generic.RegisterSelect =
513 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
514 REG_STATUS | REG_ERROR;
48c23d3e 515 isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
1da177e4
LT
516 break;
517
518 case ACTION_ENUM:
191648d0 519 usb_stor_dbg(us, " isd200_action(ENUM,0x%02x)\n", value);
1da177e4
LT
520 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
521 ACTION_SELECT_3|ACTION_SELECT_4|
522 ACTION_SELECT_5;
523 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
524 ata.write.DeviceHeadByte = value;
48c23d3e 525 isd200_set_srb(info, DMA_NONE, NULL, 0);
1da177e4
LT
526 break;
527
528 case ACTION_RESET:
191648d0 529 usb_stor_dbg(us, " isd200_action(RESET)\n");
1da177e4
LT
530 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
531 ACTION_SELECT_3|ACTION_SELECT_4;
532 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
533 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
48c23d3e 534 isd200_set_srb(info, DMA_NONE, NULL, 0);
1da177e4
LT
535 break;
536
537 case ACTION_REENABLE:
191648d0 538 usb_stor_dbg(us, " isd200_action(REENABLE)\n");
1da177e4
LT
539 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
540 ACTION_SELECT_3|ACTION_SELECT_4;
541 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
542 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
48c23d3e 543 isd200_set_srb(info, DMA_NONE, NULL, 0);
1da177e4
LT
544 break;
545
546 case ACTION_SOFT_RESET:
191648d0 547 usb_stor_dbg(us, " isd200_action(SOFT_RESET)\n");
1da177e4
LT
548 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
549 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
550 ata.write.DeviceHeadByte = info->DeviceHead;
f08f31f7 551 ata.write.CommandByte = ATA_CMD_DEV_RESET;
48c23d3e 552 isd200_set_srb(info, DMA_NONE, NULL, 0);
1da177e4
LT
553 break;
554
555 case ACTION_IDENTIFY:
191648d0 556 usb_stor_dbg(us, " isd200_action(IDENTIFY)\n");
1da177e4 557 ata.generic.RegisterSelect = REG_COMMAND;
f08f31f7 558 ata.write.CommandByte = ATA_CMD_ID_ATA;
48c23d3e 559 isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
d8881cda 560 ATA_ID_WORDS * 2);
1da177e4
LT
561 break;
562
563 default:
191648d0 564 usb_stor_dbg(us, "Error: Undefined action %d\n", action);
48c23d3e 565 return ISD200_ERROR;
1da177e4
LT
566 }
567
568 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
569 srb->cmd_len = sizeof(ata.generic);
570 status = usb_stor_Bulk_transport(srb, us);
571 if (status == USB_STOR_TRANSPORT_GOOD)
572 status = ISD200_GOOD;
573 else {
191648d0
JP
574 usb_stor_dbg(us, " isd200_action(0x%02x) error: %d\n",
575 action, status);
1da177e4
LT
576 status = ISD200_ERROR;
577 /* need to reset device here */
578 }
579
580 return status;
581}
582
583/**************************************************************************
584 * isd200_read_regs
585 *
586 * Read ATA Registers
587 *
588 * RETURNS:
589 * ISD status code
590 */
591static int isd200_read_regs( struct us_data *us )
592{
593 struct isd200_info *info = (struct isd200_info *)us->extra;
594 int retStatus = ISD200_GOOD;
595 int transferStatus;
596
191648d0 597 usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
1da177e4
LT
598
599 transferStatus = isd200_action( us, ACTION_READ_STATUS,
600 info->RegsBuf, sizeof(info->ATARegs) );
601 if (transferStatus != ISD200_TRANSPORT_GOOD) {
191648d0 602 usb_stor_dbg(us, " Error reading ATA registers\n");
1da177e4
LT
603 retStatus = ISD200_ERROR;
604 } else {
605 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
191648d0
JP
606 usb_stor_dbg(us, " Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
607 info->ATARegs[ATA_REG_ERROR_OFFSET]);
1da177e4
LT
608 }
609
610 return retStatus;
611}
612
613
614/**************************************************************************
615 * Invoke the transport and basic error-handling/recovery methods
616 *
617 * This is used by the protocol layers to actually send the message to
618 * the device and receive the response.
619 */
620static void isd200_invoke_transport( struct us_data *us,
621 struct scsi_cmnd *srb,
622 union ata_cdb *ataCdb )
623{
624 int need_auto_sense = 0;
625 int transferStatus;
626 int result;
627
628 /* send the command to the transport layer */
629 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
630 srb->cmd_len = sizeof(ataCdb->generic);
631 transferStatus = usb_stor_Bulk_transport(srb, us);
632
f0183a33
FB
633 /*
634 * if the command gets aborted by the higher layers, we need to
1da177e4
LT
635 * short-circuit all other processing
636 */
7e4d6c38 637 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
191648d0 638 usb_stor_dbg(us, "-- command was aborted\n");
1da177e4
LT
639 goto Handle_Abort;
640 }
641
642 switch (transferStatus) {
643
644 case USB_STOR_TRANSPORT_GOOD:
645 /* Indicate a good result */
646 srb->result = SAM_STAT_GOOD;
647 break;
648
649 case USB_STOR_TRANSPORT_NO_SENSE:
191648d0 650 usb_stor_dbg(us, "-- transport indicates protocol failure\n");
1da177e4
LT
651 srb->result = SAM_STAT_CHECK_CONDITION;
652 return;
653
654 case USB_STOR_TRANSPORT_FAILED:
191648d0 655 usb_stor_dbg(us, "-- transport indicates command failure\n");
1da177e4
LT
656 need_auto_sense = 1;
657 break;
658
659 case USB_STOR_TRANSPORT_ERROR:
191648d0 660 usb_stor_dbg(us, "-- transport indicates transport error\n");
1da177e4
LT
661 srb->result = DID_ERROR << 16;
662 /* Need reset here */
663 return;
664
665 default:
191648d0 666 usb_stor_dbg(us, "-- transport indicates unknown error\n");
1da177e4
LT
667 srb->result = DID_ERROR << 16;
668 /* Need reset here */
669 return;
670 }
671
48c23d3e 672 if ((scsi_get_resid(srb) > 0) &&
1da177e4
LT
673 !((srb->cmnd[0] == REQUEST_SENSE) ||
674 (srb->cmnd[0] == INQUIRY) ||
675 (srb->cmnd[0] == MODE_SENSE) ||
676 (srb->cmnd[0] == LOG_SENSE) ||
677 (srb->cmnd[0] == MODE_SENSE_10))) {
191648d0 678 usb_stor_dbg(us, "-- unexpectedly short transfer\n");
1da177e4
LT
679 need_auto_sense = 1;
680 }
681
682 if (need_auto_sense) {
683 result = isd200_read_regs(us);
7e4d6c38 684 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
191648d0 685 usb_stor_dbg(us, "-- auto-sense aborted\n");
1da177e4
LT
686 goto Handle_Abort;
687 }
688 if (result == ISD200_GOOD) {
689 isd200_build_sense(us, srb);
690 srb->result = SAM_STAT_CHECK_CONDITION;
691
692 /* If things are really okay, then let's show that */
693 if ((srb->sense_buffer[2] & 0xf) == 0x0)
694 srb->result = SAM_STAT_GOOD;
695 } else {
696 srb->result = DID_ERROR << 16;
697 /* Need reset here */
698 }
699 }
700
f0183a33
FB
701 /*
702 * Regardless of auto-sense, if we _know_ we have an error
1da177e4
LT
703 * condition, show that in the result code
704 */
705 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
706 srb->result = SAM_STAT_CHECK_CONDITION;
707 return;
708
f0183a33
FB
709 /*
710 * abort processing: the bulk-only transport requires a reset
711 * following an abort
712 */
1da177e4
LT
713 Handle_Abort:
714 srb->result = DID_ABORT << 16;
715
716 /* permit the reset transfer to take place */
7e4d6c38 717 clear_bit(US_FLIDX_ABORTING, &us->dflags);
1da177e4
LT
718 /* Need reset here */
719}
720
721#ifdef CONFIG_USB_STORAGE_DEBUG
191648d0 722static void isd200_log_config(struct us_data *us, struct isd200_info *info)
1da177e4 723{
191648d0
JP
724 usb_stor_dbg(us, " Event Notification: 0x%x\n",
725 info->ConfigData.EventNotification);
726 usb_stor_dbg(us, " External Clock: 0x%x\n",
727 info->ConfigData.ExternalClock);
728 usb_stor_dbg(us, " ATA Init Timeout: 0x%x\n",
729 info->ConfigData.ATAInitTimeout);
730 usb_stor_dbg(us, " ATAPI Command Block Size: 0x%x\n",
731 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
732 usb_stor_dbg(us, " Master/Slave Selection: 0x%x\n",
733 info->ConfigData.ATAConfig & ATACFG_MASTER);
734 usb_stor_dbg(us, " ATAPI Reset: 0x%x\n",
735 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
736 usb_stor_dbg(us, " ATA Timing: 0x%x\n",
737 info->ConfigData.ATAConfig & ATACFG_TIMING);
738 usb_stor_dbg(us, " ATA Major Command: 0x%x\n",
739 info->ConfigData.ATAMajorCommand);
740 usb_stor_dbg(us, " ATA Minor Command: 0x%x\n",
741 info->ConfigData.ATAMinorCommand);
742 usb_stor_dbg(us, " Init Status: 0x%x\n",
743 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
744 usb_stor_dbg(us, " Config Descriptor 2: 0x%x\n",
745 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
746 usb_stor_dbg(us, " Skip Device Boot: 0x%x\n",
747 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
333c65bc 748 usb_stor_dbg(us, " ATA 3 State Suspend: 0x%x\n",
191648d0
JP
749 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
750 usb_stor_dbg(us, " Descriptor Override: 0x%x\n",
751 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
752 usb_stor_dbg(us, " Last LUN Identifier: 0x%x\n",
753 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
754 usb_stor_dbg(us, " SRST Enable: 0x%x\n",
755 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
1da177e4
LT
756}
757#endif
758
759/**************************************************************************
760 * isd200_write_config
761 *
762 * Write the ISD200 Configuration data
763 *
764 * RETURNS:
765 * ISD status code
766 */
767static int isd200_write_config( struct us_data *us )
768{
769 struct isd200_info *info = (struct isd200_info *)us->extra;
770 int retStatus = ISD200_GOOD;
771 int result;
772
773#ifdef CONFIG_USB_STORAGE_DEBUG
191648d0
JP
774 usb_stor_dbg(us, "Entering isd200_write_config\n");
775 usb_stor_dbg(us, " Writing the following ISD200 Config Data:\n");
776 isd200_log_config(us, info);
1da177e4
LT
777#endif
778
779 /* let's send the command via the control pipe */
780 result = usb_stor_ctrl_transfer(
781 us,
782 us->send_ctrl_pipe,
783 0x01,
784 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
785 0x0000,
786 0x0002,
787 (void *) &info->ConfigData,
788 sizeof(info->ConfigData));
789
790 if (result >= 0) {
191648d0 791 usb_stor_dbg(us, " ISD200 Config Data was written successfully\n");
1da177e4 792 } else {
191648d0 793 usb_stor_dbg(us, " Request to write ISD200 Config Data failed!\n");
1da177e4
LT
794 retStatus = ISD200_ERROR;
795 }
796
191648d0 797 usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
1da177e4
LT
798 return retStatus;
799}
800
801
802/**************************************************************************
803 * isd200_read_config
804 *
805 * Reads the ISD200 Configuration data
806 *
807 * RETURNS:
808 * ISD status code
809 */
810static int isd200_read_config( struct us_data *us )
811{
812 struct isd200_info *info = (struct isd200_info *)us->extra;
813 int retStatus = ISD200_GOOD;
814 int result;
815
191648d0 816 usb_stor_dbg(us, "Entering isd200_read_config\n");
1da177e4
LT
817
818 /* read the configuration information from ISD200. Use this to */
819 /* determine what the special ATA CDB bytes are. */
820
821 result = usb_stor_ctrl_transfer(
822 us,
823 us->recv_ctrl_pipe,
824 0x02,
825 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
826 0x0000,
827 0x0002,
828 (void *) &info->ConfigData,
829 sizeof(info->ConfigData));
830
831
832 if (result >= 0) {
191648d0 833 usb_stor_dbg(us, " Retrieved the following ISD200 Config Data:\n");
1da177e4 834#ifdef CONFIG_USB_STORAGE_DEBUG
191648d0 835 isd200_log_config(us, info);
1da177e4
LT
836#endif
837 } else {
191648d0 838 usb_stor_dbg(us, " Request to get ISD200 Config Data failed!\n");
1da177e4
LT
839 retStatus = ISD200_ERROR;
840 }
841
191648d0 842 usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
1da177e4
LT
843 return retStatus;
844}
845
846
847/**************************************************************************
848 * isd200_atapi_soft_reset
849 *
850 * Perform an Atapi Soft Reset on the device
851 *
852 * RETURNS:
853 * NT status code
854 */
855static int isd200_atapi_soft_reset( struct us_data *us )
856{
857 int retStatus = ISD200_GOOD;
858 int transferStatus;
859
191648d0 860 usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
1da177e4
LT
861
862 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
863 if (transferStatus != ISD200_TRANSPORT_GOOD) {
191648d0 864 usb_stor_dbg(us, " Error issuing Atapi Soft Reset\n");
1da177e4
LT
865 retStatus = ISD200_ERROR;
866 }
867
191648d0 868 usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
1da177e4
LT
869 return retStatus;
870}
871
872
873/**************************************************************************
874 * isd200_srst
875 *
876 * Perform an SRST on the device
877 *
878 * RETURNS:
879 * ISD status code
880 */
881static int isd200_srst( struct us_data *us )
882{
883 int retStatus = ISD200_GOOD;
884 int transferStatus;
885
191648d0 886 usb_stor_dbg(us, "Entering isd200_SRST\n");
1da177e4
LT
887
888 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
889
890 /* check to see if this request failed */
891 if (transferStatus != ISD200_TRANSPORT_GOOD) {
191648d0 892 usb_stor_dbg(us, " Error issuing SRST\n");
1da177e4
LT
893 retStatus = ISD200_ERROR;
894 } else {
895 /* delay 10ms to give the drive a chance to see it */
896 msleep(10);
897
898 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
899 if (transferStatus != ISD200_TRANSPORT_GOOD) {
191648d0 900 usb_stor_dbg(us, " Error taking drive out of reset\n");
1da177e4
LT
901 retStatus = ISD200_ERROR;
902 } else {
903 /* delay 50ms to give the drive a chance to recover after SRST */
904 msleep(50);
905 }
906 }
907
191648d0 908 usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
1da177e4
LT
909 return retStatus;
910}
911
912
913/**************************************************************************
914 * isd200_try_enum
915 *
916 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
917 * and tries to analyze the status registers
918 *
919 * RETURNS:
920 * ISD status code
921 */
922static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
923 int detect )
924{
925 int status = ISD200_GOOD;
926 unsigned long endTime;
927 struct isd200_info *info = (struct isd200_info *)us->extra;
928 unsigned char *regs = info->RegsBuf;
929 int recheckAsMaster = 0;
930
931 if ( detect )
932 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
933 else
934 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
935
936 /* loop until we detect !BSY or timeout */
937 while(1) {
1da177e4
LT
938
939 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
940 if ( status != ISD200_GOOD )
941 break;
942
943 status = isd200_action( us, ACTION_READ_STATUS,
944 regs, 8 );
945 if ( status != ISD200_GOOD )
946 break;
947
948 if (!detect) {
fcd3ec69 949 if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
191648d0
JP
950 usb_stor_dbg(us, " %s status is still BSY, try again...\n",
951 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
952 "Master" : "Slave");
1da177e4 953 } else {
191648d0
JP
954 usb_stor_dbg(us, " %s status !BSY, continue with next operation\n",
955 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
956 "Master" : "Slave");
1da177e4
LT
957 break;
958 }
959 }
fcd3ec69
BZ
960 /* check for ATA_BUSY and */
961 /* ATA_DF (workaround ATA Zip drive) and */
962 /* ATA_ERR (workaround for Archos CD-ROM) */
c74e8095 963 else if (regs[ATA_REG_STATUS_OFFSET] &
fcd3ec69 964 (ATA_BUSY | ATA_DF | ATA_ERR)) {
191648d0 965 usb_stor_dbg(us, " Status indicates it is not ready, try again...\n");
1da177e4
LT
966 }
967 /* check for DRDY, ATA devices set DRDY after SRST */
fcd3ec69 968 else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
191648d0 969 usb_stor_dbg(us, " Identified ATA device\n");
1da177e4
LT
970 info->DeviceFlags |= DF_ATA_DEVICE;
971 info->DeviceHead = master_slave;
972 break;
973 }
f0183a33
FB
974 /*
975 * check Cylinder High/Low to
976 * determine if it is an ATAPI device
977 */
c74e8095
BZ
978 else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
979 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
f0183a33
FB
980 /*
981 * It seems that the RICOH
982 * MP6200A CD/RW drive will
983 * report itself okay as a
984 * slave when it is really a
985 * master. So this check again
986 * as a master device just to
987 * make sure it doesn't report
988 * itself okay as a master also
989 */
1da177e4
LT
990 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
991 !recheckAsMaster) {
191648d0 992 usb_stor_dbg(us, " Identified ATAPI device as slave. Rechecking again as master\n");
1da177e4
LT
993 recheckAsMaster = 1;
994 master_slave = ATA_ADDRESS_DEVHEAD_STD;
995 } else {
191648d0 996 usb_stor_dbg(us, " Identified ATAPI device\n");
1da177e4
LT
997 info->DeviceHead = master_slave;
998
999 status = isd200_atapi_soft_reset(us);
1000 break;
1001 }
1002 } else {
191648d0 1003 usb_stor_dbg(us, " Not ATA, not ATAPI - Weird\n");
1da177e4
LT
1004 break;
1005 }
1006
1007 /* check for timeout on this request */
1008 if (time_after_eq(jiffies, endTime)) {
1009 if (!detect)
191648d0 1010 usb_stor_dbg(us, " BSY check timeout, just continue with next operation...\n");
1da177e4 1011 else
191648d0 1012 usb_stor_dbg(us, " Device detect timeout!\n");
1da177e4
LT
1013 break;
1014 }
1015 }
1016
1017 return status;
1018}
1019
1020/**************************************************************************
1021 * isd200_manual_enum
1022 *
1023 * Determines if the drive attached is an ATA or ATAPI and if it is a
1024 * master or slave.
1025 *
1026 * RETURNS:
1027 * ISD status code
1028 */
1029static int isd200_manual_enum(struct us_data *us)
1030{
1031 struct isd200_info *info = (struct isd200_info *)us->extra;
1032 int retStatus = ISD200_GOOD;
1033
191648d0 1034 usb_stor_dbg(us, "Entering isd200_manual_enum\n");
1da177e4
LT
1035
1036 retStatus = isd200_read_config(us);
1037 if (retStatus == ISD200_GOOD) {
1038 int isslave;
1039 /* master or slave? */
1040 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1041 if (retStatus == ISD200_GOOD)
1042 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1043
1044 if (retStatus == ISD200_GOOD) {
1045 retStatus = isd200_srst(us);
1046 if (retStatus == ISD200_GOOD)
1047 /* ata or atapi? */
1048 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1049 }
1050
1051 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1052 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
191648d0
JP
1053 usb_stor_dbg(us, " Setting Master/Slave selection to %d\n",
1054 isslave);
1da177e4
LT
1055 info->ConfigData.ATAConfig &= 0x3f;
1056 info->ConfigData.ATAConfig |= (isslave<<6);
1057 retStatus = isd200_write_config(us);
1058 }
1059 }
1060
191648d0 1061 usb_stor_dbg(us, "Leaving isd200_manual_enum %08X\n", retStatus);
1da177e4
LT
1062 return(retStatus);
1063}
1064
d8881cda 1065static void isd200_fix_driveid(u16 *id)
3794ade5
AC
1066{
1067#ifndef __LITTLE_ENDIAN
1068# ifdef __BIG_ENDIAN
1069 int i;
d8881cda
BZ
1070
1071 for (i = 0; i < ATA_ID_WORDS; i++)
1072 id[i] = __le16_to_cpu(id[i]);
3794ade5
AC
1073# else
1074# error "Please fix <asm/byteorder.h>"
1075# endif
1076#endif
1077}
1078
191648d0 1079static void isd200_dump_driveid(struct us_data *us, u16 *id)
d8881cda 1080{
191648d0
JP
1081 usb_stor_dbg(us, " Identify Data Structure:\n");
1082 usb_stor_dbg(us, " config = 0x%x\n", id[ATA_ID_CONFIG]);
1083 usb_stor_dbg(us, " cyls = 0x%x\n", id[ATA_ID_CYLS]);
1084 usb_stor_dbg(us, " heads = 0x%x\n", id[ATA_ID_HEADS]);
1085 usb_stor_dbg(us, " track_bytes = 0x%x\n", id[4]);
1086 usb_stor_dbg(us, " sector_bytes = 0x%x\n", id[5]);
1087 usb_stor_dbg(us, " sectors = 0x%x\n", id[ATA_ID_SECTORS]);
1088 usb_stor_dbg(us, " serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1089 usb_stor_dbg(us, " buf_type = 0x%x\n", id[20]);
1090 usb_stor_dbg(us, " buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
1091 usb_stor_dbg(us, " ecc_bytes = 0x%x\n", id[22]);
1092 usb_stor_dbg(us, " fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
1093 usb_stor_dbg(us, " model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
1094 usb_stor_dbg(us, " max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1095 usb_stor_dbg(us, " dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
1096 usb_stor_dbg(us, " capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
1097 usb_stor_dbg(us, " tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
1098 usb_stor_dbg(us, " tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
1099 usb_stor_dbg(us, " field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
1100 usb_stor_dbg(us, " cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
1101 usb_stor_dbg(us, " cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
1102 usb_stor_dbg(us, " cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
1103 usb_stor_dbg(us, " cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1104 usb_stor_dbg(us, " multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
1105 usb_stor_dbg(us, " lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1106 usb_stor_dbg(us, " command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1107 usb_stor_dbg(us, " command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
d8881cda 1108}
1da177e4
LT
1109
1110/**************************************************************************
1111 * isd200_get_inquiry_data
1112 *
1113 * Get inquiry data
1114 *
1115 * RETURNS:
1116 * ISD status code
1117 */
1118static int isd200_get_inquiry_data( struct us_data *us )
1119{
1120 struct isd200_info *info = (struct isd200_info *)us->extra;
1121 int retStatus = ISD200_GOOD;
d8881cda 1122 u16 *id = info->id;
1da177e4 1123
191648d0 1124 usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n");
1da177e4
LT
1125
1126 /* set default to Master */
1127 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1128
1129 /* attempt to manually enumerate this device */
1130 retStatus = isd200_manual_enum(us);
1131 if (retStatus == ISD200_GOOD) {
1132 int transferStatus;
1133
1134 /* check for an ATA device */
1135 if (info->DeviceFlags & DF_ATA_DEVICE) {
1136 /* this must be an ATA device */
1137 /* perform an ATA Command Identify */
1138 transferStatus = isd200_action( us, ACTION_IDENTIFY,
d8881cda 1139 id, ATA_ID_WORDS * 2);
1da177e4
LT
1140 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1141 /* Error issuing ATA Command Identify */
191648d0 1142 usb_stor_dbg(us, " Error issuing ATA Command Identify\n");
1da177e4
LT
1143 retStatus = ISD200_ERROR;
1144 } else {
1145 /* ATA Command Identify successful */
1146 int i;
1147 __be16 *src;
1148 __u16 *dest;
1da177e4 1149
d8881cda 1150 isd200_fix_driveid(id);
191648d0 1151 isd200_dump_driveid(us, id);
1da177e4
LT
1152
1153 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1154
1155 /* Standard IDE interface only supports disks */
1156 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1157
1158 /* The length must be at least 36 (5 + 31) */
1159 info->InquiryData.AdditionalLength = 0x1F;
1160
d8881cda 1161 if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
1da177e4
LT
1162 /* set the removable bit */
1163 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1164 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1165 }
1166
1167 /* Fill in vendor identification fields */
d8881cda 1168 src = (__be16 *)&id[ATA_ID_PROD];
1da177e4
LT
1169 dest = (__u16*)info->InquiryData.VendorId;
1170 for (i=0;i<4;i++)
1171 dest[i] = be16_to_cpu(src[i]);
1172
d8881cda 1173 src = (__be16 *)&id[ATA_ID_PROD + 8/2];
1da177e4
LT
1174 dest = (__u16*)info->InquiryData.ProductId;
1175 for (i=0;i<8;i++)
1176 dest[i] = be16_to_cpu(src[i]);
1177
d8881cda 1178 src = (__be16 *)&id[ATA_ID_FW_REV];
1da177e4
LT
1179 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1180 for (i=0;i<2;i++)
1181 dest[i] = be16_to_cpu(src[i]);
1182
1183 /* determine if it supports Media Status Notification */
d8881cda 1184 if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
191648d0 1185 usb_stor_dbg(us, " Device supports Media Status Notification\n");
1da177e4 1186
f0183a33
FB
1187 /*
1188 * Indicate that it is enabled, even
1189 * though it is not.
1190 * This allows the lock/unlock of the
1191 * media to work correctly.
1da177e4
LT
1192 */
1193 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1194 }
1195 else
1196 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1197
1198 }
1199 } else {
1200 /*
1201 * this must be an ATAPI device
1202 * use an ATAPI protocol (Transparent SCSI)
1203 */
1204 us->protocol_name = "Transparent SCSI";
1205 us->proto_handler = usb_stor_transparent_scsi_command;
1206
191648d0
JP
1207 usb_stor_dbg(us, "Protocol changed to: %s\n",
1208 us->protocol_name);
1da177e4 1209
f0183a33 1210 /* Free driver structure */
1da177e4 1211 us->extra_destructor(info);
ba2ef3b3 1212 kfree(info);
1da177e4
LT
1213 us->extra = NULL;
1214 us->extra_destructor = NULL;
1215 }
1216 }
1217
191648d0 1218 usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
1da177e4
LT
1219
1220 return(retStatus);
1221}
1222
1da177e4
LT
1223/**************************************************************************
1224 * isd200_scsi_to_ata
1225 *
1226 * Translate SCSI commands to ATA commands.
1227 *
1228 * RETURNS:
1229 * 1 if the command needs to be sent to the transport layer
1230 * 0 otherwise
1231 */
1232static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1233 union ata_cdb * ataCdb)
1234{
1235 struct isd200_info *info = (struct isd200_info *)us->extra;
d8881cda 1236 u16 *id = info->id;
1da177e4
LT
1237 int sendToTransport = 1;
1238 unsigned char sectnum, head;
1239 unsigned short cylinder;
1240 unsigned long lba;
1241 unsigned long blockCount;
1242 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1243
1244 memset(ataCdb, 0, sizeof(union ata_cdb));
1245
1246 /* SCSI Command */
1247 switch (srb->cmnd[0]) {
1248 case INQUIRY:
191648d0 1249 usb_stor_dbg(us, " ATA OUT - INQUIRY\n");
1da177e4
LT
1250
1251 /* copy InquiryData */
1252 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1253 sizeof(info->InquiryData), srb);
1254 srb->result = SAM_STAT_GOOD;
1255 sendToTransport = 0;
1256 break;
1257
1258 case MODE_SENSE:
191648d0 1259 usb_stor_dbg(us, " ATA OUT - SCSIOP_MODE_SENSE\n");
1da177e4
LT
1260
1261 /* Initialize the return buffer */
1262 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1263
1264 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1265 {
1266 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1267 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1268 ataCdb->generic.TransferBlockSize = 1;
1269 ataCdb->generic.RegisterSelect = REG_COMMAND;
1270 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
48c23d3e 1271 isd200_srb_set_bufflen(srb, 0);
1da177e4 1272 } else {
191648d0 1273 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
1da177e4
LT
1274 srb->result = SAM_STAT_GOOD;
1275 sendToTransport = 0;
1276 }
1277 break;
1278
1279 case TEST_UNIT_READY:
191648d0 1280 usb_stor_dbg(us, " ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1da177e4
LT
1281
1282 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
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 = ATA_COMMAND_GET_MEDIA_STATUS;
48c23d3e 1289 isd200_srb_set_bufflen(srb, 0);
1da177e4 1290 } else {
191648d0 1291 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
1da177e4
LT
1292 srb->result = SAM_STAT_GOOD;
1293 sendToTransport = 0;
1294 }
1295 break;
1296
1297 case READ_CAPACITY:
1298 {
1299 unsigned long capacity;
1300 struct read_capacity_data readCapacityData;
1301
191648d0 1302 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ_CAPACITY\n");
1da177e4 1303
d8881cda
BZ
1304 if (ata_id_has_lba(id))
1305 capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1306 else
1307 capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1308 id[ATA_ID_SECTORS]) - 1;
1309
1da177e4
LT
1310 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1311 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1312
1313 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1314 sizeof(readCapacityData), srb);
1315 srb->result = SAM_STAT_GOOD;
1316 sendToTransport = 0;
1317 }
1318 break;
1319
1320 case READ_10:
191648d0 1321 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ\n");
1da177e4
LT
1322
1323 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1324 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1325
d8881cda 1326 if (ata_id_has_lba(id)) {
1da177e4
LT
1327 sectnum = (unsigned char)(lba);
1328 cylinder = (unsigned short)(lba>>8);
1329 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1330 } else {
d8881cda
BZ
1331 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1332 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1333 id[ATA_ID_HEADS]));
1334 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1335 id[ATA_ID_HEADS]);
1da177e4
LT
1336 }
1337 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1338 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1339 ataCdb->generic.TransferBlockSize = 1;
1340 ataCdb->generic.RegisterSelect =
1341 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1342 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1343 REG_DEVICE_HEAD | REG_COMMAND;
1344 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1345 ataCdb->write.SectorNumberByte = sectnum;
1346 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1347 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1348 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
f08f31f7 1349 ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
1da177e4
LT
1350 break;
1351
1352 case WRITE_10:
191648d0 1353 usb_stor_dbg(us, " ATA OUT - SCSIOP_WRITE\n");
1da177e4
LT
1354
1355 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1356 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1357
d8881cda 1358 if (ata_id_has_lba(id)) {
1da177e4
LT
1359 sectnum = (unsigned char)(lba);
1360 cylinder = (unsigned short)(lba>>8);
1361 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1362 } else {
d8881cda
BZ
1363 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1364 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1365 id[ATA_ID_HEADS]));
1366 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1367 id[ATA_ID_HEADS]);
1da177e4
LT
1368 }
1369 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1370 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1371 ataCdb->generic.TransferBlockSize = 1;
1372 ataCdb->generic.RegisterSelect =
1373 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1374 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1375 REG_DEVICE_HEAD | REG_COMMAND;
1376 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1377 ataCdb->write.SectorNumberByte = sectnum;
1378 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1379 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1380 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
f08f31f7 1381 ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
1da177e4
LT
1382 break;
1383
1384 case ALLOW_MEDIUM_REMOVAL:
191648d0 1385 usb_stor_dbg(us, " ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1da177e4
LT
1386
1387 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
191648d0
JP
1388 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n",
1389 srb->cmnd[4]);
1da177e4
LT
1390
1391 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1392 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1393 ataCdb->generic.TransferBlockSize = 1;
1394 ataCdb->generic.RegisterSelect = REG_COMMAND;
1395 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
f08f31f7 1396 ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
48c23d3e 1397 isd200_srb_set_bufflen(srb, 0);
1da177e4 1398 } else {
191648d0 1399 usb_stor_dbg(us, " Not removeable media, just report okay\n");
1da177e4
LT
1400 srb->result = SAM_STAT_GOOD;
1401 sendToTransport = 0;
1402 }
1403 break;
1404
1405 case START_STOP:
191648d0
JP
1406 usb_stor_dbg(us, " ATA OUT - SCSIOP_START_STOP_UNIT\n");
1407 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1da177e4
LT
1408
1409 if ((srb->cmnd[4] & 0x3) == 0x2) {
191648d0 1410 usb_stor_dbg(us, " Media Eject\n");
1da177e4
LT
1411 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1412 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1413 ataCdb->generic.TransferBlockSize = 0;
1414 ataCdb->generic.RegisterSelect = REG_COMMAND;
1415 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1416 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
191648d0 1417 usb_stor_dbg(us, " Get Media Status\n");
1da177e4
LT
1418 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1419 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1420 ataCdb->generic.TransferBlockSize = 1;
1421 ataCdb->generic.RegisterSelect = REG_COMMAND;
1422 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
48c23d3e 1423 isd200_srb_set_bufflen(srb, 0);
1da177e4 1424 } else {
191648d0 1425 usb_stor_dbg(us, " Nothing to do, just report okay\n");
1da177e4
LT
1426 srb->result = SAM_STAT_GOOD;
1427 sendToTransport = 0;
1428 }
1429 break;
1430
1431 default:
191648d0
JP
1432 usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
1433 srb->cmnd[0]);
1da177e4
LT
1434 srb->result = DID_ERROR << 16;
1435 sendToTransport = 0;
1436 break;
1437 }
1438
1439 return(sendToTransport);
1440}
1441
1442
1443/**************************************************************************
1444 * isd200_free_info
1445 *
1446 * Frees the driver structure.
1447 */
1448static void isd200_free_info_ptrs(void *info_)
1449{
1450 struct isd200_info *info = (struct isd200_info *) info_;
1451
1452 if (info) {
1453 kfree(info->id);
1454 kfree(info->RegsBuf);
e92e63aa 1455 kfree(info->srb.sense_buffer);
1da177e4
LT
1456 }
1457}
1458
1459/**************************************************************************
1460 * isd200_init_info
1461 *
1462 * Allocates (if necessary) and initializes the driver structure.
1463 *
1464 * RETURNS:
1465 * ISD status code
1466 */
1467static int isd200_init_info(struct us_data *us)
1468{
1da177e4
LT
1469 struct isd200_info *info;
1470
a5cc8049 1471 info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1da177e4 1472 if (!info)
8091e0ca 1473 return ISD200_ERROR;
1da177e4 1474
8091e0ca
KB
1475 info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
1476 info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1477 info->srb.sense_buffer = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1478
1479 if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1480 isd200_free_info_ptrs(info);
1481 kfree(info);
1482 return ISD200_ERROR;
191648d0 1483 }
1da177e4 1484
8091e0ca
KB
1485 us->extra = info;
1486 us->extra_destructor = isd200_free_info_ptrs;
1487
1488 return ISD200_GOOD;
1da177e4
LT
1489}
1490
1491/**************************************************************************
1492 * Initialization for the ISD200
1493 */
1494
32d5493e 1495static int isd200_Initialization(struct us_data *us)
1da177e4 1496{
191648d0 1497 usb_stor_dbg(us, "ISD200 Initialization...\n");
1da177e4
LT
1498
1499 /* Initialize ISD200 info struct */
1500
1501 if (isd200_init_info(us) == ISD200_ERROR) {
191648d0 1502 usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
1da177e4
LT
1503 } else {
1504 /* Get device specific data */
1505
1506 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
191648d0 1507 usb_stor_dbg(us, "ISD200 Initialization Failure\n");
1da177e4 1508 else
191648d0 1509 usb_stor_dbg(us, "ISD200 Initialization complete\n");
1da177e4
LT
1510 }
1511
1512 return 0;
1513}
1514
1515
1516/**************************************************************************
1517 * Protocol and Transport for the ISD200 ASIC
1518 *
1519 * This protocol and transport are for ATA devices connected to an ISD200
25985edc 1520 * ASIC. An ATAPI device that is connected as a slave device will be
1da177e4
LT
1521 * detected in the driver initialization function and the protocol will
1522 * be changed to an ATAPI protocol (Transparent SCSI).
1523 *
1524 */
1525
32d5493e 1526static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1da177e4 1527{
48c23d3e 1528 int sendToTransport = 1, orig_bufflen;
1da177e4
LT
1529 union ata_cdb ataCdb;
1530
1531 /* Make sure driver was initialized */
1532
446230f5 1533 if (us->extra == NULL) {
191648d0 1534 usb_stor_dbg(us, "ERROR Driver not initialized\n");
446230f5
CIK
1535 srb->result = DID_ERROR << 16;
1536 return;
1537 }
1da177e4 1538
48c23d3e
BH
1539 scsi_set_resid(srb, 0);
1540 /* scsi_bufflen might change in protocol translation to ata */
1541 orig_bufflen = scsi_bufflen(srb);
1da177e4
LT
1542 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1543
1544 /* send the command to the transport layer */
1545 if (sendToTransport)
1546 isd200_invoke_transport(us, srb, &ataCdb);
48c23d3e
BH
1547
1548 isd200_srb_set_bufflen(srb, orig_bufflen);
1da177e4 1549}
32d5493e 1550
aa519be3
AM
1551static struct scsi_host_template isd200_host_template;
1552
32d5493e
AS
1553static int isd200_probe(struct usb_interface *intf,
1554 const struct usb_device_id *id)
1555{
1556 struct us_data *us;
1557 int result;
1558
1559 result = usb_stor_probe1(&us, intf, id,
aa519be3
AM
1560 (id - isd200_usb_ids) + isd200_unusual_dev_list,
1561 &isd200_host_template);
32d5493e
AS
1562 if (result)
1563 return result;
1564
1565 us->protocol_name = "ISD200 ATA/ATAPI";
1566 us->proto_handler = isd200_ata_command;
1567
1568 result = usb_stor_probe2(us);
1569 return result;
1570}
1571
1572static struct usb_driver isd200_driver = {
aa519be3 1573 .name = DRV_NAME,
32d5493e
AS
1574 .probe = isd200_probe,
1575 .disconnect = usb_stor_disconnect,
1576 .suspend = usb_stor_suspend,
1577 .resume = usb_stor_resume,
1578 .reset_resume = usb_stor_reset_resume,
1579 .pre_reset = usb_stor_pre_reset,
1580 .post_reset = usb_stor_post_reset,
1581 .id_table = isd200_usb_ids,
1582 .soft_unbind = 1,
e73b2db6 1583 .no_dynamic_id = 1,
32d5493e
AS
1584};
1585
aa519be3 1586module_usb_stor_driver(isd200_driver, isd200_host_template, DRV_NAME);