^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 SanDisk SDDR-55 SmartMedia reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * SDDR55 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) 2002 Simon Munton
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "usb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "transport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "scsiglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DRV_NAME "ums-sddr55"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_AUTHOR("Simon Munton");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_IMPORT_NS(USB_STORAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * The table of devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) vendorName, productName, useProtocol, useTransport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) initFunction, flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .driver_info = (flags) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct usb_device_id sddr55_usb_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # include "unusual_sddr55.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { } /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * The flags table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) vendor_name, product_name, use_protocol, use_transport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) init_function, Flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .vendorName = vendor_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .productName = product_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .useProtocol = use_protocol, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .useTransport = use_transport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .initFunction = init_function, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct us_unusual_dev sddr55_unusual_dev_list[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # include "unusual_sddr55.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) { } /* Terminating entry */
^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) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define LSB_of(s) ((s)&0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define MSB_of(s) ((s)>>8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define PAGESIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define set_sense_info(sk, asc, ascq) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) info->sense_data[2] = sk; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) info->sense_data[12] = asc; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) info->sense_data[13] = ascq; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct sddr55_card_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long capacity; /* Size of card in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int max_log_blks; /* maximum number of logical blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int pageshift; /* log2 of pagesize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int smallpageshift; /* 1 if pagesize == 256 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int blocksize; /* Size of block in pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int blockshift; /* log2 of blocksize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int blockmask; /* 2^blockshift - 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int read_only; /* non zero if card is write protected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int force_read_only; /* non zero if we find a map error*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int *lba_to_pba; /* logical to physical map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int *pba_to_lba; /* physical to logical map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int fatal_error; /* set if we detect something nasty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned long last_access; /* number of jiffies since we last talked to device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned char sense_data[18];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define NOT_ALLOCATED 0xffffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define BAD_BLOCK 0xffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define CIS_BLOCK 0x400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define UNUSED_BLOCK 0x3ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) sddr55_bulk_transport(struct us_data *us, int direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned char *data, unsigned int len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) us->recv_bulk_pipe : us->send_bulk_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return USB_STOR_XFER_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) info->last_access = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^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) * check if card inserted, if there is, update read_only status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * return non zero if no card
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int sddr55_status(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned char *status = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* send command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) memset(command, 0, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) command[5] = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) command[7] = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) DMA_TO_DEVICE, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) usb_stor_dbg(us, "Result for send_command in status %d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) set_sense_info (4, 0, 0); /* hardware error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) DMA_FROM_DEVICE, status, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* expect to get short transfer if no card fitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* had a short transfer, no card inserted, free map memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) kfree(info->lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) kfree(info->pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) info->lba_to_pba = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) info->pba_to_lba = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) info->fatal_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) info->force_read_only = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) set_sense_info (4, 0, 0); /* hardware error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* check write protect status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) info->read_only = (status[0] & 0x20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* now read status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) DMA_FROM_DEVICE, status, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) set_sense_info (4, 0, 0); /* hardware error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return (result == USB_STOR_XFER_GOOD ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int sddr55_read_data(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned int lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned int page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned short sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned char *status = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned int pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned long address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned short pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int len, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) // Since we only read in one block at a time, we have to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) // a bounce buffer and move the data a piece at a time between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) // bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) info->smallpageshift) * PAGESIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) buffer = kmalloc(len, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return USB_STOR_TRANSPORT_ERROR; /* out of memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) while (sectors>0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* have we got to end? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (lba >= info->max_log_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) pba = info->lba_to_pba[lba];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) // Read as many sectors as possible in this block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) pages = min((unsigned int) sectors << info->smallpageshift,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) info->blocksize - page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) len = pages << info->pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pages, pba, lba, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (pba == NOT_ALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* no pba for this lba, fill with zeroes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) memset (buffer, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) address = (pba << info->blockshift) + page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) command[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) command[1] = LSB_of(address>>16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) command[2] = LSB_of(address>>8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) command[3] = LSB_of(address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) command[4] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) command[5] = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) command[6] = LSB_of(pages << (1 - info->smallpageshift));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) command[7] = 0x85;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* send command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) DMA_TO_DEVICE, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) usb_stor_dbg(us, "Result for send_command in read_data %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* read data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) DMA_FROM_DEVICE, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* now read status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) DMA_FROM_DEVICE, status, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* check status for error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (status[0] == 0xff && status[1] == 0x4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) set_sense_info (3, 0x11, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) // Store the data in the transfer buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) &sg, &offset, TO_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) lba++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) sectors -= pages >> info->smallpageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int sddr55_write_data(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned int lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) unsigned int page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) unsigned short sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) unsigned char *status = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) unsigned int pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) unsigned int new_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) unsigned long address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) unsigned short pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned int len, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* check if we are allowed to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (info->read_only || info->force_read_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) set_sense_info (7, 0x27, 0); /* read only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) // Since we only write one block at a time, we have to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) // a bounce buffer and move the data a piece at a time between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) // bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) info->smallpageshift) * PAGESIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) buffer = kmalloc(len, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) while (sectors > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* have we got to end? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (lba >= info->max_log_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) pba = info->lba_to_pba[lba];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) // Write as many sectors as possible in this block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) pages = min((unsigned int) sectors << info->smallpageshift,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) info->blocksize - page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) len = pages << info->pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) // Get the data from the transfer buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) &sg, &offset, FROM_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) pages, pba, lba, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) command[4] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (pba == NOT_ALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* no pba allocated for this lba, find a free pba to use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int max_pba = (info->max_log_blks / 250 ) * 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int found_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int found_pba = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /* set pba to first block in zone lba is in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) pba = (lba / 1000) * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (max_pba > 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) max_pba = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * Scan through the map looking for an unused block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * leave 16 unused blocks at start (or as many as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * possible) since the sddr55 seems to reuse a used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * block when it shouldn't if we don't leave space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) for (i = 0; i < max_pba; i++, pba++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) found_pba = pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (found_count++ > 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) pba = found_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (pba == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* oh dear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) usb_stor_dbg(us, "Couldn't find unallocated block\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) set_sense_info (3, 0x31, 0); /* medium error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) pba, lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* set writing to unallocated block flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) command[4] = 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) address = (pba << info->blockshift) + page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) command[1] = LSB_of(address>>16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) command[2] = LSB_of(address>>8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) command[3] = LSB_of(address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* set the lba into the command, modulo 1000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) command[0] = LSB_of(lba % 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) command[6] = MSB_of(lba % 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) command[4] |= LSB_of(pages >> info->smallpageshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) command[5] = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) command[7] = 0x86;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /* send command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) DMA_TO_DEVICE, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) usb_stor_dbg(us, "Result for send_command in write_data %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /* set_sense_info is superfluous here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) set_sense_info (3, 0x3, 0);/* peripheral write error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* send the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) DMA_TO_DEVICE, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) usb_stor_dbg(us, "Result for send_data in write_data %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* set_sense_info is superfluous here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) set_sense_info (3, 0x3, 0);/* peripheral write error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* now read status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) usb_stor_dbg(us, "Result for get_status in write_data %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* set_sense_info is superfluous here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) set_sense_info (3, 0x3, 0);/* peripheral write error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) >> info->blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /* check status for error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (status[0] == 0xff && status[1] == 0x4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) info->pba_to_lba[new_pba] = BAD_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) set_sense_info (3, 0x0c, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) goto leave;
^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) usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) lba, pba, new_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /* update the lba<->pba maps, note new_pba might be the same as pba */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) info->lba_to_pba[lba] = new_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) info->pba_to_lba[pba] = UNUSED_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /* check that new_pba wasn't already being used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) new_pba, info->pba_to_lba[new_pba]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) info->fatal_error = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) set_sense_info (3, 0x31, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) result = USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* update the pba<->lba maps for new_pba */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) info->pba_to_lba[new_pba] = lba % 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) lba++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) sectors -= pages >> info->smallpageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static int sddr55_read_deviceID(struct us_data *us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) unsigned char *manufacturerID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) unsigned char *deviceID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) unsigned char *content = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) memset(command, 0, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) command[5] = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) command[7] = 0x84;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) DMA_FROM_DEVICE, content, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) *manufacturerID = content[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) *deviceID = content[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (content[0] != 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) result = sddr55_bulk_transport(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) DMA_FROM_DEVICE, content, 2);
^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) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static int sddr55_reset(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static unsigned long sddr55_get_capacity(struct us_data *us) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) unsigned char manufacturerID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) unsigned char deviceID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) usb_stor_dbg(us, "Reading capacity...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) result = sddr55_read_deviceID(us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) &manufacturerID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) &deviceID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) usb_stor_dbg(us, "Manuf ID = %02X\n", manufacturerID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) info->pageshift = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) info->smallpageshift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) info->blocksize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) info->blockshift = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) info->blockmask = 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) switch (deviceID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) case 0x6e: // 1MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) case 0xe8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) case 0xec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) info->pageshift = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) info->smallpageshift = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return 0x00100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) case 0xea: // 2MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) case 0x64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) info->pageshift = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) info->smallpageshift = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) case 0x5d: // 5d is a ROM card with pagesize 512.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return 0x00200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) case 0xe3: // 4MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) case 0xe5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) case 0x6b:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) case 0xd5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return 0x00400000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) case 0xe6: // 8MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) case 0xd6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return 0x00800000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) case 0x73: // 16MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) info->blocksize = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) info->blockshift = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) info->blockmask = 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return 0x01000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) case 0x75: // 32MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) info->blocksize = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) info->blockshift = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) info->blockmask = 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return 0x02000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) case 0x76: // 64MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) info->blocksize = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) info->blockshift = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) info->blockmask = 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return 0x04000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) case 0x79: // 128MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) info->blocksize = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) info->blockshift = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) info->blockmask = 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return 0x08000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) default: // unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return 0;
^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) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static int sddr55_read_map(struct us_data *us) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) int numblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) unsigned short lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) unsigned short max_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!info->capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) numblocks = info->capacity >> (info->blockshift + info->pageshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) buffer = kmalloc_array(numblocks, 2, GFP_NOIO );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) memset(command, 0, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) command[5] = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) command[6] = numblocks * 2 / 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) command[7] = 0x8A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if ( result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) kfree (buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if ( result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) kfree (buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return -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) result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if ( result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) kfree (buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) kfree(info->lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) kfree(info->pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) info->lba_to_pba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) info->pba_to_lba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) kfree(info->lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) kfree(info->pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) info->lba_to_pba = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) info->pba_to_lba = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /* set maximum lba */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) max_lba = info->max_log_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (max_lba > 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) max_lba = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * Each block is 64 bytes of control data, so block i is located in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
^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) for (i=0; i<numblocks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) int zone = i / 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * Every 1024 physical blocks ("zone"), the LBA numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * go back to zero, but are within a higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * block of LBA's. Also, there is a maximum of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * 1000 LBA's per zone. In other words, in PBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * 1024-2047 you will find LBA 0-999 which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * really LBA 1000-1999. Yes, this wastes 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * physical blocks per zone. Go figure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * These devices can have blocks go bad, so there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * are 24 spare blocks to use when blocks do go bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * SDDR55 returns 0xffff for a bad block, and 0x400 for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * CIS block. (Is this true for cards 8MB or less??)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * Record these in the physical to logical map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) info->pba_to_lba[i] = lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (lba >= max_lba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) !info->force_read_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) "sddr55: map inconsistency at LBA %04X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) lba + zone * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) info->force_read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) info->lba_to_pba[lba + zone * 1000] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static void sddr55_card_info_destructor(void *extra) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (!extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) kfree(info->lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) kfree(info->pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * Transport for the Sandisk SDDR-55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static unsigned char inquiry_response[8] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) // write-protected for now, no block descriptor support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) static unsigned char mode_page_01[20] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 0x01, 0x0A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) unsigned char *ptr = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) unsigned long capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) unsigned int lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) unsigned int pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) unsigned int page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) unsigned short pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) struct sddr55_card_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (!us->extra) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) us->extra = kzalloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) sizeof(struct sddr55_card_info), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (!us->extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) us->extra_destructor = sddr55_card_info_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) info = (struct sddr55_card_info *)(us->extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (srb->cmnd[0] == REQUEST_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) info->sense_data[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) info->sense_data[12],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) info->sense_data[13]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) memcpy (ptr, info->sense_data, sizeof info->sense_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ptr[0] = 0x70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) ptr[7] = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) memset (info->sense_data, 0, sizeof info->sense_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) memset (info->sense_data, 0, sizeof info->sense_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * Dummy up a response for INQUIRY since SDDR55 doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * respond to INQUIRY commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (srb->cmnd[0] == INQUIRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) memcpy(ptr, inquiry_response, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) fill_inquiry_response(us, ptr, 36);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * only check card status if the map isn't allocated, ie no card seen yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * or if it's been over half a second since we last accessed it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /* check to see if a card is fitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) result = sddr55_status (us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) result = sddr55_status (us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (!result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * if we detected a problem with the map when writing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * don't allow any more access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (info->fatal_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) set_sense_info (3, 0x31, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (srb->cmnd[0] == READ_CAPACITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) capacity = sddr55_get_capacity(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (!capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) set_sense_info (3, 0x30, 0); /* incompatible medium */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) info->capacity = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * figure out the maximum logical block number, allowing for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * the fact that only 250 out of every 256 are used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) * Last page in the card, adjust as we only use 250 out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * every 256 pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) capacity = (capacity / 256) * 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) capacity /= PAGESIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) capacity--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) usb_stor_set_xfer_buf(ptr, 8, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) sddr55_read_map(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (srb->cmnd[0] == MODE_SENSE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) memcpy(ptr, mode_page_01, sizeof mode_page_01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) usb_stor_dbg(us, "Dummy up request for mode page 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) usb_stor_dbg(us, "Dummy up request for all mode pages\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) set_sense_info (5, 0x24, 0); /* invalid field in command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) page = short_pack(srb->cmnd[3], srb->cmnd[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) page <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) page <<= info->smallpageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) // convert page to block and page-within-block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) lba = page >> info->blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) page = page & info->blockmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) // locate physical block corresponding to logical block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (lba >= info->max_log_blks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) lba, info->max_log_blks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) set_sense_info (5, 0x24, 0); /* invalid field in command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) pba = info->lba_to_pba[lba];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (srb->cmnd[0] == WRITE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) pba, lba, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return sddr55_write_data(us, lba, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) pba, lba, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) return sddr55_read_data(us, lba, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) if (srb->cmnd[0] == TEST_UNIT_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (srb->cmnd[0] == START_STOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) set_sense_info (5, 0x20, 0); /* illegal command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) static struct scsi_host_template sddr55_host_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) static int sddr55_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) struct us_data *us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) result = usb_stor_probe1(&us, intf, id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) &sddr55_host_template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) us->transport_name = "SDDR55";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) us->transport = sddr55_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) us->transport_reset = sddr55_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) us->max_lun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) result = usb_stor_probe2(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static struct usb_driver sddr55_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) .name = DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) .probe = sddr55_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) .disconnect = usb_stor_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) .suspend = usb_stor_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) .resume = usb_stor_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) .reset_resume = usb_stor_reset_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) .pre_reset = usb_stor_pre_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) .post_reset = usb_stor_post_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) .id_table = sddr55_usb_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) .soft_unbind = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) .no_dynamic_id = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);