// SPDX-License-Identifier: GPL-2.0+ /* * Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable * * Current development and maintenance by: * (c) 2000, 2001 Robert Baruch (autophile@starband.net) * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org> * * Developed with the assistance of: * (c) 2002 Alan Stern <stern@rowland.org> * * Flash support based on earlier work by: * (c) 2002 Thomas Kreiling <usbdev@sm04.de> * * Many originally ATAPI devices were slightly modified to meet the USB * market by using some kind of translation from ATAPI to USB on the host, * and the peripheral would translate from USB back to ATAPI. * * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, * which does the USB-to-ATAPI conversion. By obtaining the data sheet on * their device under nondisclosure agreement, I have been able to write * this driver for Linux. * * The chip used in the device can also be used for EPP and ISA translation * as well. This driver is only guaranteed to work with the ATAPI * translation. * * See the Kconfig help text for a list of devices known to be supported by * this driver.
*/
/* * Convenience function to produce an ATA read/write sectors command * Use cmd=0x20 for read, cmd=0x30 for write
*/ staticvoid usbat_pack_ata_sector_cmd(unsignedchar *buf, unsignedchar thistime,
u32 sector, unsignedchar cmd)
{
buf[0] = 0;
buf[1] = thistime;
buf[2] = sector & 0xFF;
buf[3] = (sector >> 8) & 0xFF;
buf[4] = (sector >> 16) & 0xFF;
buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
buf[6] = cmd;
}
/* * Convenience function to get the device type (flash or hp8200)
*/ staticint usbat_get_device_type(struct us_data *us)
{ return ((struct usbat_info*)us->extra)->devicetype;
}
/* * Some USBAT-specific commands can only be executed over a command transport * This transport allows one (len=8) or two (len=16) vendor-specific commands * to be executed.
*/ staticint usbat_execute_command(struct us_data *us, unsignedchar *commands, unsignedint len)
{ return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
commands, len);
}
/* * Read the status register
*/ staticint usbat_get_status(struct us_data *us, unsignedchar *status)
{ int rc;
rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
/* * The only bit relevant to ATA access is bit 6 * which defines 8 bit data access (set) or 16 bit (unset)
*/
command[2] = epp_control;
/* * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1, * ET1 and ET2 define an external event to be checked for on event of a * _read_blocks or _write_blocks operation. The read/write will not take * place unless the defined trigger signal is active.
*/
command[3] = external_trigger;
/* * The resultant byte of the mask operation (see mask_byte) is compared for * equivalence with this test pattern. If equal, the read/write will take * place.
*/
command[4] = test_pattern;
/* * This value is logically ANDed with the status register field specified * in the read/write command.
*/
command[5] = mask_byte;
/* * If ALQ is set in the qualifier, this field contains the address of the * registers where the byte count should be read for transferring the data. * If ALQ is not set, then this field contains the number of bytes to be * transferred.
*/
command[6] = subcountL;
command[7] = subcountH;
return usbat_execute_command(us, command, 8);
}
/* * Block, waiting for an ATA device to become not busy or to report * an error condition.
*/ staticint usbat_wait_not_busy(struct us_data *us, int minutes)
{ int i; int result; unsignedchar *status = us->iobuf;
/* * Synchronizing cache on a CDR could take a heck of a long time, * but probably not more than 10 minutes or so. On the other hand, * doing a full blank on a CDRW at speed 1 will take about 75 * minutes!
*/
for (i=0; i<1200+minutes*60; i++) {
result = usbat_get_status(us, status);
if (result!=USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR; if (*status & 0x01) { /* check condition */
result = usbat_read(us, USBAT_ATA, 0x10, status); return USB_STOR_TRANSPORT_FAILED;
} if (*status & 0x20) /* device fault */ return USB_STOR_TRANSPORT_FAILED;
if ((*status & 0x80)==0x00) { /* not busy */
usb_stor_dbg(us, "Waited not busy for %d steps\n", i); return USB_STOR_TRANSPORT_GOOD;
}
usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
minutes); return USB_STOR_TRANSPORT_FAILED;
}
/* * Read block data from the data register
*/ staticint usbat_read_block(struct us_data *us, void* buf, unsignedshort len, int use_sg)
{ int result; unsignedchar *command = us->iobuf;
/* * Write block data via the data register
*/ staticint usbat_write_block(struct us_data *us, unsignedchar access, void* buf, unsignedshort len, int minutes, int use_sg)
{ int result; unsignedchar *command = us->iobuf;
if (result != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
result = usbat_bulk_write(us, buf, len, use_sg); if (result != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
return usbat_wait_not_busy(us, minutes);
}
/* * Process read and write requests
*/ staticint usbat_hp8200e_rw_block_test(struct us_data *us, unsignedchar access, unsignedchar *registers, unsignedchar *data_out, unsignedshort num_registers, unsignedchar data_reg, unsignedchar status_reg, unsignedchar timeout, unsignedchar qualifier, int direction, void *buf, unsignedshort len, int use_sg, int minutes)
{ int result; unsignedint pipe = (direction == DMA_FROM_DEVICE) ?
us->recv_bulk_pipe : us->send_bulk_pipe;
unsignedchar *command = us->iobuf; int i, j; int cmdlen; unsignedchar *data = us->iobuf; unsignedchar *status = us->iobuf;
BUG_ON(num_registers > US_IOBUF_SIZE/2);
for (i=0; i<20; i++) {
/* * The first time we send the full command, which consists * of downloading the SCSI command followed by downloading * the data via a write-and-test. Any other time we only * send the command to download the data -- the SCSI command * is still 'active' in some sense in the device. * * We're only going to try sending the data 10 times. After * that, we just return a failure.
*/
if (i==0) {
cmdlen = 16; /* * Write to multiple registers * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is * necessary here, but that's what came out of the * trace every single time.
*/
command[0] = 0x40;
command[1] = access | USBAT_CMD_WRITE_REGS;
command[2] = 0x07;
command[3] = 0x17;
command[4] = 0xFC;
command[5] = 0xE7;
command[6] = LSB_of(num_registers*2);
command[7] = MSB_of(num_registers*2);
} else
cmdlen = 8;
result = usbat_bulk_write(us, data, num_registers*2, 0); if (result != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
}
result = usb_stor_bulk_transfer_sg(us,
pipe, buf, len, use_sg, NULL);
/* * If we get a stall on the bulk download, we'll retry * the bulk download -- but not the SCSI command because * in some sense the SCSI command is still 'active' and * waiting for the data. Don't ask me why this should be; * I'm only following what the Windoze driver did. * * Note that a stall for the test-and-read/write command means * that the test failed. In this case we're testing to make * sure that the device is error-free * (i.e. bit 0 -- CHK -- of status is 0). The most likely * hypothesis is that the USBAT chip somehow knows what * the device will accept, but doesn't give the device any * data until all data is received. Thus, the device would * still be waiting for the first byte of data if a stall * occurs, even if the stall implies that some data was * transferred.
*/
if (result == USB_STOR_XFER_SHORT ||
result == USB_STOR_XFER_STALLED) {
/* * If we're reading and we stalled, then clear * the bulk output pipe only the first time.
*/
if (direction==DMA_FROM_DEVICE && i==0) { if (usb_stor_clear_halt(us,
us->send_bulk_pipe) < 0) return USB_STOR_TRANSPORT_ERROR;
}
/* * Read status: is the device angry, or just busy?
*/
result = usbat_read(us, USBAT_ATA,
direction==DMA_TO_DEVICE ?
USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
status);
if (result!=USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR; if (*status & 0x01) /* check condition */ return USB_STOR_TRANSPORT_FAILED; if (*status & 0x20) /* device fault */ return USB_STOR_TRANSPORT_FAILED;
usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
direction == DMA_TO_DEVICE ? "Writing" : "Reading");
return USB_STOR_TRANSPORT_FAILED;
}
/* * Write to multiple registers: * Allows us to write specific data to any registers. The data to be written * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN * which gets sent through bulk out. * Not designed for large transfers of data!
*/ staticint usbat_multiple_write(struct us_data *us, unsignedchar *registers, unsignedchar *data_out, unsignedshort num_registers)
{ int i, result; unsignedchar *data = us->iobuf; unsignedchar *command = us->iobuf;
BUG_ON(num_registers > US_IOBUF_SIZE/2);
/* Write to multiple registers, ATA access */
command[0] = 0x40;
command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
/* * Conditionally read blocks from device: * Allows us to read blocks from a specific data register, based upon the * condition that a status register can be successfully masked with a status * qualifier. If this condition is not initially met, the read will wait * up until a maximum amount of time has elapsed, as specified by timeout. * The read will start when the condition is met, otherwise the command aborts. * * The qualifier defined here is not the value that is masked, it defines * conditions for the write to take place. The actual masked qualifier (and * other related details) are defined beforehand with _set_shuttle_features().
*/ staticint usbat_read_blocks(struct us_data *us, void* buffer, int len, int use_sg)
{ int result; unsignedchar *command = us->iobuf;
/* Multiple block read setup command */
result = usbat_execute_command(us, command, 8); if (result != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_FAILED;
/* Read the blocks we just asked for */
result = usbat_bulk_read(us, buffer, len, use_sg); if (result != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_FAILED;
return USB_STOR_TRANSPORT_GOOD;
}
/* * Conditionally write blocks to device: * Allows us to write blocks to a specific data register, based upon the * condition that a status register can be successfully masked with a status * qualifier. If this condition is not initially met, the write will wait * up until a maximum amount of time has elapsed, as specified by timeout. * The read will start when the condition is met, otherwise the command aborts. * * The qualifier defined here is not the value that is masked, it defines * conditions for the write to take place. The actual masked qualifier (and * other related details) are defined beforehand with _set_shuttle_features().
*/ staticint usbat_write_blocks(struct us_data *us, void* buffer, int len, int use_sg)
{ int result; unsignedchar *command = us->iobuf;
/* Enable peripheral control signals and card detect */
rc = usbat_write_user_io(us,
USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
USBAT_UIO_EPAD | USBAT_UIO_1); if (rc != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
return USB_STOR_TRANSPORT_GOOD;
}
/* * Determine if media is present.
*/ staticint usbat_flash_check_media_present(struct us_data *us, unsignedchar *uio)
{ if (*uio & USBAT_UIO_UI0) {
usb_stor_dbg(us, "no media detected\n"); return USBAT_FLASH_MEDIA_NONE;
}
return USBAT_FLASH_MEDIA_CF;
}
/* * Determine if media has changed since last operation
*/ staticint usbat_flash_check_media_changed(struct us_data *us, unsignedchar *uio)
{ if (*uio & USBAT_UIO_0) {
usb_stor_dbg(us, "media change detected\n"); return USBAT_FLASH_MEDIA_CHANGED;
}
return USBAT_FLASH_MEDIA_SAME;
}
/* * Check for media change / no media and handle the situation appropriately
*/ staticint usbat_flash_check_media(struct us_data *us, struct usbat_info *info)
{ int rc; unsignedchar *uio = us->iobuf;
rc = usbat_read_user_io(us, uio); if (rc != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
/* Check for media existence */
rc = usbat_flash_check_media_present(us, uio); if (rc == USBAT_FLASH_MEDIA_NONE) {
info->sense_key = 0x02;
info->sense_asc = 0x3A;
info->sense_ascq = 0x00; return USB_STOR_TRANSPORT_FAILED;
}
/* Check for media change */
rc = usbat_flash_check_media_changed(us, uio); if (rc == USBAT_FLASH_MEDIA_CHANGED) {
/* Reset and re-enable card detect */
rc = usbat_device_reset(us); if (rc != USB_STOR_TRANSPORT_GOOD) return rc;
rc = usbat_device_enable_cdt(us); if (rc != USB_STOR_TRANSPORT_GOOD) return rc;
msleep(50);
rc = usbat_read_user_io(us, uio); if (rc != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
/* * Determine whether we are controlling a flash-based reader/writer, * or a HP8200-based CD drive. * Sets transport functions as appropriate.
*/ staticint usbat_identify_device(struct us_data *us, struct usbat_info *info)
{ int rc; unsignedchar status;
if (!us || !info) return USB_STOR_TRANSPORT_ERROR;
rc = usbat_device_reset(us); if (rc != USB_STOR_TRANSPORT_GOOD) return rc;
msleep(500);
/* * In attempt to distinguish between HP CDRW's and Flash readers, we now * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash * readers), this command should fail with error. On ATAPI devices (i.e. * CDROM drives), it should succeed.
*/
rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1); if (rc != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
rc = usbat_get_status(us, &status); if (rc != USB_STOR_XFER_GOOD) return USB_STOR_TRANSPORT_ERROR;
/* Check for error bit, or if the command 'fell through' */ if (status == 0xA1 || !(status & 0x01)) { /* Device is HP 8200 */
usb_stor_dbg(us, "Detected HP8200 CDRW\n");
info->devicetype = USBAT_DEV_HP8200;
} else { /* Device is a CompactFlash reader/writer */
usb_stor_dbg(us, "Detected Flash reader/writer\n");
info->devicetype = USBAT_DEV_FLASH;
}
return USB_STOR_TRANSPORT_GOOD;
}
/* * Set the transport function based on the device type
*/ staticint usbat_set_transport(struct us_data *us, struct usbat_info *info, int devicetype)
{
if (!info->devicetype)
info->devicetype = devicetype;
if (!info->devicetype)
usbat_identify_device(us, info);
result = usbat_flash_check_media(us, info); if (result != USB_STOR_TRANSPORT_GOOD) return result;
/* * we're working in LBA mode. according to the ATA spec, * we can support up to 28-bit addressing. I don't know if Jumpshot * supports beyond 24-bit addressing. It's kind of hard to test * since it requires > 8GB CF card.
*/
if (sector > 0x0FFFFFFF) return USB_STOR_TRANSPORT_ERROR;
totallen = sectors * info->ssize;
/* * Since we don't read more than 64 KB at a time, we have to create * a bounce buffer and move the data a piece at a time between the * bounce buffer and the actual transfer buffer.
*/
do { /* * loop, never allocate or transfer more than 64k at once * (min(128k, 255*info->ssize) is the real limit)
*/
len = min(totallen, alloclen);
thistime = (len / info->ssize) & 0xff;
result = usbat_flash_check_media(us, info); if (result != USB_STOR_TRANSPORT_GOOD) return result;
/* * we're working in LBA mode. according to the ATA spec, * we can support up to 28-bit addressing. I don't know if the device * supports beyond 24-bit addressing. It's kind of hard to test * since it requires > 8GB media.
*/
if (sector > 0x0FFFFFFF) return USB_STOR_TRANSPORT_ERROR;
totallen = sectors * info->ssize;
/* * Since we don't write more than 64 KB at a time, we have to create * a bounce buffer and move the data a piece at a time between the * bounce buffer and the actual transfer buffer.
*/
do { /* * loop, never allocate or transfer more than 64k at once * (min(128k, 255*info->ssize) is the real limit)
*/
len = min(totallen, alloclen);
thistime = (len / info->ssize) & 0xff;
/* Get the data from the transfer buffer */
usb_stor_access_xfer_buf(buffer, len, us->srb,
&sg, &sg_offset, FROM_XFER_BUF);
/* * Since we're requesting more data than we can handle in * a single read command (max is 64k-1), we will perform * multiple reads, but each read must be in multiples of * a sector. Luckily the sector size is in srb->transfersize * (see linux/drivers/scsi/sr.c).
*/
if (data[7+0] == GPCMD_READ_CD) {
len = short_pack(data[7+9], data[7+8]);
len <<= 16;
len |= data[7+7];
usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
srb->transfersize = scsi_bufflen(srb)/len;
}
if (!srb->transfersize) {
srb->transfersize = 2048; /* A guess */
usb_stor_dbg(us, "transfersize 0, forcing %d\n",
srb->transfersize);
}
/* * Since we only read in one block at a time, we have to create * a bounce buffer and move the data a piece at a time between the * bounce buffer and the actual transfer buffer.
*/
/* * Transport for the HP 8200e
*/ staticint usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
{ int result; unsignedchar *status = us->iobuf; unsignedchar registers[32]; unsignedchar data[32]; unsignedint len; int i;
len = scsi_bufflen(srb);
/* * Send A0 (ATA PACKET COMMAND). * Note: I guess we're never going to get any of the ATA * commands... just ATA Packet Commands.
*/
if (len > 0xFFFF) {
usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
len); return USB_STOR_TRANSPORT_ERROR;
}
result = usbat_multiple_write(us, registers, data, 7);
if (result != USB_STOR_TRANSPORT_GOOD) return result;
/* * Write the 12-byte command header. * * If the command is BLANK then set the timer for 75 minutes. * Otherwise set it for 10 minutes. * * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW * AT SPEED 4 IS UNRELIABLE!!!
*/
if (len > 0xFF) { /* need to read cylH also */
len = *status; if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
USB_STOR_XFER_GOOD) { return USB_STOR_TRANSPORT_ERROR;
}
len += ((unsignedint) *status)<<8;
} else
len = *status;
result = usbat_read_block(us, scsi_sglist(srb), len,
scsi_sg_count(srb));
}
return result;
}
/* * Transport for USBAT02-based CompactFlash and similar storage devices
*/ staticint usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
{ int rc; struct usbat_info *info = (struct usbat_info *) (us->extra); unsignedlong block, blocks; unsignedchar *ptr = us->iobuf; staticconstunsignedchar inquiry_response[36] = {
0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
};
if (srb->cmnd[0] == READ_CAPACITY) {
rc = usbat_flash_check_media(us, info); if (rc != USB_STOR_TRANSPORT_GOOD) return rc;
rc = usbat_flash_get_sector_count(us, info); if (rc != USB_STOR_TRANSPORT_GOOD) return rc;
/* hard coded 512 byte sectors as per ATA spec */
info->ssize = 0x200;
usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
info->sectors, info->ssize);
/* * build the reply * note: must return the sector number of the last sector, * *not* the total number of sectors
*/
((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
usb_stor_set_xfer_buf(ptr, 8, srb);
if (srb->cmnd[0] == READ_12) { /* * I don't think we'll ever see a READ_12 but support it anyway
*/
block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
if (srb->cmnd[0] == WRITE_12) { /* * I don't think we'll ever see a WRITE_12 but support it anyway
*/
block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { /* * sure. whatever. not like we can stop the user from popping * the media out of the device (no locking doors, etc)
*/ return USB_STOR_TRANSPORT_GOOD;
}
result = usb_stor_probe1(&us, intf, id,
(id - usbat_usb_ids) + usbat_unusual_dev_list,
&usbat_host_template); if (result) return result;
/* * The actual transport will be determined later by the * initialization routine; this is just a placeholder.
*/
us->transport_name = "Shuttle USBAT";
us->transport = usbat_flash_transport;
us->transport_reset = usb_stor_CB_reset;
us->max_lun = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.