Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for Datafab USB Compact Flash reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * datafab driver v0.1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * First release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Current development and maintenance by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *   (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *   which I used as a template for this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *   Some bugfixes and scatter-gather code by Gregory P. Smith 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *   (greg-usb@electricrain.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *   Fix for media change by Joerg Schneider (js@joergschneider.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Other contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *   (c) 2002 Alan Stern <stern@rowland.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * This driver attempts to support USB CompactFlash reader/writer devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * based on Datafab USB-to-ATA chips.  It was specifically developed for the 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Datafab MDCFE-B USB CompactFlash reader but has since been found to work 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * with a variety of Datafab-based devices from a number of manufacturers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * I've received a report of this driver working with a Datafab-based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * SmartMedia device though please be aware that I'm personally unable to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * test SmartMedia support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * This driver supports reading and writing.  If you're truly paranoid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * however, you can force the driver into a write-protected state by setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * the WP enable bits in datafab_handle_mode_sense().  See the comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * in that routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include "usb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include "transport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include "scsiglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define DRV_NAME "ums-datafab"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) MODULE_IMPORT_NS(USB_STORAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct datafab_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	unsigned long   sectors;	/* total sector count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned long   ssize;		/* sector size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	signed char	lun;		/* used for dual-slot readers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* the following aren't used yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	unsigned char   sense_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	unsigned long   sense_asc;	/* additional sense code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned long   sense_ascq;	/* additional sense code qualifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int datafab_determine_lun(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				 struct datafab_info *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * The table of devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		    vendorName, productName, useProtocol, useTransport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		    initFunction, flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)   .driver_info = (flags) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static struct usb_device_id datafab_usb_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #	include "unusual_datafab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{ }		/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) MODULE_DEVICE_TABLE(usb, datafab_usb_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * The flags table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		    vendor_name, product_name, use_protocol, use_transport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		    init_function, Flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	.vendorName = vendor_name,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	.productName = product_name,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	.useProtocol = use_protocol,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	.useTransport = use_transport,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	.initFunction = init_function,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static struct us_unusual_dev datafab_unusual_dev_list[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #	include "unusual_datafab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	{ }		/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return USB_STOR_XFER_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	usb_stor_dbg(us, "len = %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			data, len, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return USB_STOR_XFER_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	usb_stor_dbg(us, "len = %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			data, len, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int datafab_read_data(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			     struct datafab_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			     u32 sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			     u32 sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	unsigned char  thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	unsigned int totallen, alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	int len, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unsigned int sg_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct scatterlist *sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	// we're working in LBA mode.  according to the ATA spec, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	// we can support up to 28-bit addressing.  I don't know if Datafab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	// supports beyond 24-bit addressing.  It's kind of hard to test 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	// since it requires > 8GB CF card.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (sectors > 0x0FFFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (info->lun == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		result = datafab_determine_lun(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		if (result != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	totallen = sectors * info->ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	// Since we don't read more than 64 KB at a time, we have to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	// a bounce buffer and move the data a piece at a time between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	// bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	alloclen = min(totallen, 65536u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	buffer = kmalloc(alloclen, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		// loop, never allocate or transfer more than 64k at once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		// (min(128k, 255*info->ssize) is the real limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		len = min(totallen, alloclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		thistime = (len / info->ssize) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		command[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		command[1] = thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		command[2] = sector & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		command[3] = (sector >> 8) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		command[4] = (sector >> 16) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		command[5] = 0xE0 + (info->lun << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		command[5] |= (sector >> 24) & 0x0F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		command[6] = 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		command[7] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		// send the read command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		result = datafab_bulk_write(us, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		// read the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		result = datafab_bulk_read(us, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		// Store the data in the transfer buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				 &sg, &sg_offset, TO_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		sector += thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		totallen -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	} while (totallen > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int datafab_write_data(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			      struct datafab_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			      u32 sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			      u32 sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	unsigned char *reply = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	unsigned char thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	unsigned int totallen, alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int len, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	unsigned int sg_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct scatterlist *sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	// we're working in LBA mode.  according to the ATA spec, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	// we can support up to 28-bit addressing.  I don't know if Datafab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	// supports beyond 24-bit addressing.  It's kind of hard to test 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	// since it requires > 8GB CF card.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (sectors > 0x0FFFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (info->lun == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		result = datafab_determine_lun(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (result != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	totallen = sectors * info->ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	// Since we don't write more than 64 KB at a time, we have to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	// a bounce buffer and move the data a piece at a time between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	// bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	alloclen = min(totallen, 65536u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	buffer = kmalloc(alloclen, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		// loop, never allocate or transfer more than 64k at once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		// (min(128k, 255*info->ssize) is the real limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		len = min(totallen, alloclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		thistime = (len / info->ssize) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		// Get the data from the transfer buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				&sg, &sg_offset, FROM_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		command[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		command[1] = thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		command[2] = sector & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		command[3] = (sector >> 8) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		command[4] = (sector >> 16) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		command[5] = 0xE0 + (info->lun << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		command[5] |= (sector >> 24) & 0x0F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		command[6] = 0x30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		command[7] = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		// send the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		result = datafab_bulk_write(us, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		// send the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		result = datafab_bulk_write(us, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		// read the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		result = datafab_bulk_read(us, reply, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		if (reply[0] != 0x50 && reply[1] != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			usb_stor_dbg(us, "Gah! write return code: %02x %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				     reply[0], reply[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		sector += thistime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		totallen -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	} while (totallen > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int datafab_determine_lun(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				 struct datafab_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	// Dual-slot readers can be thought of as dual-LUN devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	// We need to determine which card slot is being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	// We'll send an IDENTIFY DEVICE command and see which LUN responds...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	// There might be a better way of doing this?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int count = 0, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	memcpy(command, scommand, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	buf = kmalloc(512, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	usb_stor_dbg(us, "locating...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	// we'll try 3 times before giving up...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	while (count++ < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		command[5] = 0xa0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		rc = datafab_bulk_write(us, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (rc != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			rc = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		rc = datafab_bulk_read(us, buf, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (rc == USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			info->lun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			rc = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		command[5] = 0xb0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		rc = datafab_bulk_write(us, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		if (rc != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			rc = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		rc = datafab_bulk_read(us, buf, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		if (rc == USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			info->lun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			rc = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	rc = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int datafab_id_device(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			     struct datafab_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	// this is a variation of the ATA "IDENTIFY DEVICE" command...according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	// to the ATA spec, 'Sector Count' isn't used but the Windows driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	// sets this bit so we do too...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	unsigned char *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (info->lun == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		rc = datafab_determine_lun(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		if (rc != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	memcpy(command, scommand, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	reply = kmalloc(512, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!reply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	command[5] += (info->lun << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	rc = datafab_bulk_write(us, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (rc != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		rc = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	// we'll go ahead and extract the media capacity while we're here...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	rc = datafab_bulk_read(us, reply, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (rc == USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		// capacity is at word offset 57-58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		info->sectors = ((u32)(reply[117]) << 24) | 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 				((u32)(reply[116]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 				((u32)(reply[115]) <<  8) | 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 				((u32)(reply[114])      );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		rc = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	rc = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	kfree(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static int datafab_handle_mode_sense(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 				     struct scsi_cmnd * srb, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 				     int sense_6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	static unsigned char rw_err_page[12] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	static unsigned char cache_page[12] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	static unsigned char rbac_page[12] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	static unsigned char timer_page[8] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		0x1C, 0x6, 0, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	unsigned char pc, page_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct datafab_info *info = (struct datafab_info *) (us->extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	unsigned char *ptr = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	// most of this stuff is just a hack to get things working.  the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	// datafab reader doesn't present a SCSI interface so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	// fudge the SCSI commands...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	pc = srb->cmnd[2] >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	page_code = srb->cmnd[2] & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	switch (pc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	   case 0x0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		   usb_stor_dbg(us, "Current values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	   case 0x1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		   usb_stor_dbg(us, "Changeable values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	   case 0x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		   usb_stor_dbg(us, "Default values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	   case 0x3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		   usb_stor_dbg(us, "Saves values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	memset(ptr, 0, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (sense_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		ptr[2] = 0x00;		// WP enable: 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		i = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		ptr[3] = 0x00;		// WP enable: 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		i = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	switch (page_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	   default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		// vendor-specific mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		info->sense_key = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		info->sense_asc = 0x24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		info->sense_ascq = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	   case 0x1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		i += sizeof(rw_err_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	   case 0x8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		memcpy(ptr + i, cache_page, sizeof(cache_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		i += sizeof(cache_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	   case 0x1B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		i += sizeof(rbac_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	   case 0x1C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		memcpy(ptr + i, timer_page, sizeof(timer_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		i += sizeof(timer_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	   case 0x3F:		// retrieve all pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		memcpy(ptr + i, timer_page, sizeof(timer_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		i += sizeof(timer_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		i += sizeof(rbac_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		memcpy(ptr + i, cache_page, sizeof(cache_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		i += sizeof(cache_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		i += sizeof(rw_err_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (sense_6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		ptr[0] = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	usb_stor_set_xfer_buf(ptr, i, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static void datafab_info_destructor(void *extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	// this routine is a placeholder...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	// currently, we don't allocate any extra memory so we're okay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) // Transport for the Datafab MDCFE-B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	struct datafab_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	unsigned long block, blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	unsigned char *ptr = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	static unsigned char inquiry_reply[8] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (!us->extra) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		if (!us->extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		us->extra_destructor = datafab_info_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)   		((struct datafab_info *)us->extra)->lun = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	info = (struct datafab_info *) (us->extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	if (srb->cmnd[0] == INQUIRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		fill_inquiry_response(us, ptr, 36);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (srb->cmnd[0] == READ_CAPACITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		info->ssize = 0x200;  // hard coded 512 byte sectors as per ATA spec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		rc = datafab_id_device(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		if (rc != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		usb_stor_dbg(us, "READ_CAPACITY:  %ld sectors, %ld bytes per sector\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			     info->sectors, info->ssize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		// build the reply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		// we need the last sector, not the number of sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		usb_stor_set_xfer_buf(ptr, 8, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	if (srb->cmnd[0] == MODE_SELECT_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	// don't bother implementing READ_6 or WRITE_6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (srb->cmnd[0] == READ_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			     block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		return datafab_read_data(us, info, block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (srb->cmnd[0] == READ_12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		// we'll probably never see a READ_12 but we'll do it anyway...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 			     block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		return datafab_read_data(us, info, block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (srb->cmnd[0] == WRITE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 			     block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		return datafab_write_data(us, info, block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	if (srb->cmnd[0] == WRITE_12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		// we'll probably never see a WRITE_12 but we'll do it anyway...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			     block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		return datafab_write_data(us, info, block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (srb->cmnd[0] == TEST_UNIT_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		usb_stor_dbg(us, "TEST_UNIT_READY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		return datafab_id_device(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	if (srb->cmnd[0] == REQUEST_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		usb_stor_dbg(us, "REQUEST_SENSE - Returning faked response\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		// this response is pretty bogus right now.  eventually if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		// we can set the correct sense data.  so far though it hasn't been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		// necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		//
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		memset(ptr, 0, 18);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		ptr[0] = 0xF0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		ptr[2] = info->sense_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		ptr[7] = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		ptr[12] = info->sense_asc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		ptr[13] = info->sense_ascq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		usb_stor_set_xfer_buf(ptr, 18, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (srb->cmnd[0] == MODE_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		return datafab_handle_mode_sense(us, srb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (srb->cmnd[0] == MODE_SENSE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		return datafab_handle_mode_sense(us, srb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		 * sure.  whatever.  not like we can stop the user from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		 * popping the media out of the device (no locking doors, etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	if (srb->cmnd[0] == START_STOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		 * this is used by sd.c'check_scsidisk_media_change to detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		 * media change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		usb_stor_dbg(us, "START_STOP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		 * the first datafab_id_device after a media change returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		 * an error (determined experimentally)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		rc = datafab_id_device(us, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		if (rc == USB_STOR_TRANSPORT_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 			info->sense_key = NO_SENSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			srb->result = SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			info->sense_key = UNIT_ATTENTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			srb->result = SAM_STAT_CHECK_CONDITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		     srb->cmnd[0], srb->cmnd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	info->sense_key = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	info->sense_asc = 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	info->sense_ascq = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static struct scsi_host_template datafab_host_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) static int datafab_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			 const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	struct us_data *us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	result = usb_stor_probe1(&us, intf, id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 			(id - datafab_usb_ids) + datafab_unusual_dev_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 			&datafab_host_template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	us->transport_name  = "Datafab Bulk-Only";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	us->transport = datafab_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	us->transport_reset = usb_stor_Bulk_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	us->max_lun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	result = usb_stor_probe2(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) static struct usb_driver datafab_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	.name =		DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	.probe =	datafab_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	.disconnect =	usb_stor_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	.suspend =	usb_stor_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	.resume =	usb_stor_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	.reset_resume =	usb_stor_reset_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	.pre_reset =	usb_stor_pre_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	.post_reset =	usb_stor_post_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	.id_table =	datafab_usb_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	.soft_unbind =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	.no_dynamic_id = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) module_usb_stor_driver(datafab_driver, datafab_host_template, DRV_NAME);