123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- #include <linux/module.h>
- #include <linux/mutex.h>
- #include <scsi/scsi.h>
- #include <scsi/scsi_cmnd.h>
- #include <scsi/scsi_devinfo.h>
- #include <scsi/scsi_device.h>
- #include <scsi/scsi_eh.h>
- #include "usb.h"
- #include "scsiglue.h"
- #include "debug.h"
- #include "transport.h"
- #include "protocol.h"
- #define VENDOR_ID_NOKIA 0x0421
- #define VENDOR_ID_NIKON 0x04b0
- #define VENDOR_ID_PENTAX 0x0a17
- #define VENDOR_ID_MOTOROLA 0x22b8
- static const char* host_info(struct Scsi_Host *host)
- {
- struct us_data *us = host_to_us(host);
- return us->scsi_name;
- }
- static int slave_alloc (struct scsi_device *sdev)
- {
- struct us_data *us = host_to_us(sdev->host);
-
- sdev->inquiry_len = 36;
-
- blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
-
- if (us->protocol == USB_PR_BULK && us->max_lun > 0)
- sdev->sdev_bflags |= BLIST_FORCELUN;
- return 0;
- }
- static int slave_configure(struct scsi_device *sdev)
- {
- struct us_data *us = host_to_us(sdev->host);
-
- if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
- unsigned int max_sectors = 64;
- if (us->fflags & US_FL_MAX_SECTORS_MIN)
- max_sectors = PAGE_SIZE >> 9;
- if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
- blk_queue_max_hw_sectors(sdev->request_queue,
- max_sectors);
- } else if (sdev->type == TYPE_TAPE) {
-
- blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
- } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
-
- blk_queue_max_hw_sectors(sdev->request_queue, 2048);
- }
-
- if (!us->pusb_dev->bus->controller->dma_mask)
- blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
-
- if (sdev->type == TYPE_DISK) {
-
- switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
- case VENDOR_ID_NOKIA:
- case VENDOR_ID_NIKON:
- case VENDOR_ID_PENTAX:
- case VENDOR_ID_MOTOROLA:
- if (!(us->fflags & (US_FL_FIX_CAPACITY |
- US_FL_CAPACITY_OK)))
- us->fflags |= US_FL_CAPACITY_HEURISTICS;
- break;
- }
-
- if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
- sdev->use_10_for_ms = 1;
-
- sdev->use_192_bytes_for_3f = 1;
-
- if (us->fflags & US_FL_NO_WP_DETECT)
- sdev->skip_ms_page_3f = 1;
-
- sdev->skip_ms_page_8 = 1;
-
- sdev->skip_vpd_pages = 1;
-
- sdev->no_report_opcodes = 1;
-
- sdev->no_write_same = 1;
-
- if (us->fflags & US_FL_FIX_CAPACITY)
- sdev->fix_capacity = 1;
-
- if (us->fflags & US_FL_CAPACITY_HEURISTICS)
- sdev->guess_capacity = 1;
-
- if (us->fflags & US_FL_NO_READ_CAPACITY_16)
- sdev->no_read_capacity_16 = 1;
-
- if (!(us->fflags & US_FL_NEEDS_CAP16))
- sdev->try_rc_10_first = 1;
-
- if (sdev->scsi_level > SCSI_SPC_2)
- us->fflags |= US_FL_SANE_SENSE;
-
- sdev->retry_hwerror = 1;
-
- sdev->allow_restart = 1;
-
- sdev->last_sector_bug = 1;
-
- if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
- US_FL_SCM_MULT_TARG)) &&
- us->protocol == USB_PR_BULK)
- us->use_last_sector_hacks = 1;
-
- if (us->fflags & US_FL_WRITE_CACHE)
- sdev->wce_default_on = 1;
-
- if (us->fflags & US_FL_BROKEN_FUA)
- sdev->broken_fua = 1;
-
- if (us->fflags & US_FL_ALWAYS_SYNC) {
-
- sdev->skip_ms_page_8 = 1;
- sdev->skip_ms_page_3f = 1;
-
- sdev->wce_default_on = 1;
- }
- } else {
-
- sdev->use_10_for_ms = 1;
-
- if (us->fflags & US_FL_NO_READ_DISC_INFO)
- sdev->no_read_disc_info = 1;
- }
-
- if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
- sdev->scsi_level == SCSI_UNKNOWN)
- us->max_lun = 0;
-
- if (us->fflags & US_FL_NOT_LOCKABLE)
- sdev->lockable = 0;
-
- return 0;
- }
- static int target_alloc(struct scsi_target *starget)
- {
- struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
-
- starget->no_report_luns = 1;
-
- if (us->subclass == USB_SC_UFI)
- starget->pdt_1f_for_no_lun = 1;
- return 0;
- }
- static int queuecommand_lck(struct scsi_cmnd *srb,
- void (*done)(struct scsi_cmnd *))
- {
- struct us_data *us = host_to_us(srb->device->host);
-
- if (us->srb != NULL) {
- printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
- __func__, us->srb);
- return SCSI_MLQUEUE_HOST_BUSY;
- }
-
- if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
- usb_stor_dbg(us, "Fail command during disconnect\n");
- srb->result = DID_NO_CONNECT << 16;
- done(srb);
- return 0;
- }
-
- srb->scsi_done = done;
- us->srb = srb;
- complete(&us->cmnd_ready);
- return 0;
- }
- static DEF_SCSI_QCMD(queuecommand)
- static int command_abort(struct scsi_cmnd *srb)
- {
- struct us_data *us = host_to_us(srb->device->host);
- usb_stor_dbg(us, "%s called\n", __func__);
-
- scsi_lock(us_to_host(us));
-
- if (us->srb != srb) {
- scsi_unlock(us_to_host(us));
- usb_stor_dbg(us, "-- nothing to abort\n");
- return FAILED;
- }
-
- set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
- if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
- set_bit(US_FLIDX_ABORTING, &us->dflags);
- usb_stor_stop_transport(us);
- }
- scsi_unlock(us_to_host(us));
-
- wait_for_completion(&us->notify);
- return SUCCESS;
- }
- static int device_reset(struct scsi_cmnd *srb)
- {
- struct us_data *us = host_to_us(srb->device->host);
- int result;
- usb_stor_dbg(us, "%s called\n", __func__);
-
- mutex_lock(&(us->dev_mutex));
- result = us->transport_reset(us);
- mutex_unlock(&us->dev_mutex);
- return result < 0 ? FAILED : SUCCESS;
- }
- static int bus_reset(struct scsi_cmnd *srb)
- {
- struct us_data *us = host_to_us(srb->device->host);
- int result;
- usb_stor_dbg(us, "%s called\n", __func__);
- result = usb_stor_port_reset(us);
- return result < 0 ? FAILED : SUCCESS;
- }
- void usb_stor_report_device_reset(struct us_data *us)
- {
- int i;
- struct Scsi_Host *host = us_to_host(us);
- scsi_report_device_reset(host, 0, 0);
- if (us->fflags & US_FL_SCM_MULT_TARG) {
- for (i = 1; i < host->max_id; ++i)
- scsi_report_device_reset(host, 0, i);
- }
- }
- void usb_stor_report_bus_reset(struct us_data *us)
- {
- struct Scsi_Host *host = us_to_host(us);
- scsi_lock(host);
- scsi_report_bus_reset(host, 0);
- scsi_unlock(host);
- }
- static int write_info(struct Scsi_Host *host, char *buffer, int length)
- {
-
- return length;
- }
- static int show_info (struct seq_file *m, struct Scsi_Host *host)
- {
- struct us_data *us = host_to_us(host);
- const char *string;
-
- seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
-
- if (us->pusb_dev->manufacturer)
- string = us->pusb_dev->manufacturer;
- else if (us->unusual_dev->vendorName)
- string = us->unusual_dev->vendorName;
- else
- string = "Unknown";
- seq_printf(m, " Vendor: %s\n", string);
- if (us->pusb_dev->product)
- string = us->pusb_dev->product;
- else if (us->unusual_dev->productName)
- string = us->unusual_dev->productName;
- else
- string = "Unknown";
- seq_printf(m, " Product: %s\n", string);
- if (us->pusb_dev->serial)
- string = us->pusb_dev->serial;
- else
- string = "None";
- seq_printf(m, "Serial Number: %s\n", string);
-
- seq_printf(m, " Protocol: %s\n", us->protocol_name);
- seq_printf(m, " Transport: %s\n", us->transport_name);
-
- seq_printf(m, " Quirks:");
- #define US_FLAG(name, value) \
- if (us->fflags & value) seq_printf(m, " " #name);
- US_DO_ALL_FLAGS
- #undef US_FLAG
- seq_putc(m, '\n');
- return 0;
- }
- static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct scsi_device *sdev = to_scsi_device(dev);
- return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
- }
- static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
- size_t count)
- {
- struct scsi_device *sdev = to_scsi_device(dev);
- unsigned short ms;
- if (sscanf(buf, "%hu", &ms) > 0) {
- blk_queue_max_hw_sectors(sdev->request_queue, ms);
- return count;
- }
- return -EINVAL;
- }
- static DEVICE_ATTR_RW(max_sectors);
- static struct device_attribute *sysfs_device_attr_list[] = {
- &dev_attr_max_sectors,
- NULL,
- };
- static const struct scsi_host_template usb_stor_host_template = {
-
- .name = "usb-storage",
- .proc_name = "usb-storage",
- .show_info = show_info,
- .write_info = write_info,
- .info = host_info,
-
- .queuecommand = queuecommand,
-
- .eh_abort_handler = command_abort,
- .eh_device_reset_handler = device_reset,
- .eh_bus_reset_handler = bus_reset,
-
- .can_queue = 1,
-
- .this_id = -1,
- .slave_alloc = slave_alloc,
- .slave_configure = slave_configure,
- .target_alloc = target_alloc,
-
- .sg_tablesize = SG_MAX_SEGMENTS,
-
- .max_sectors = 240,
-
- .use_clustering = 1,
-
- .emulated = 1,
-
- .skip_settle_delay = 1,
-
- .sdev_attrs = sysfs_device_attr_list,
-
- .module = THIS_MODULE
- };
- void usb_stor_host_template_init(struct scsi_host_template *sht,
- const char *name, struct module *owner)
- {
- *sht = usb_stor_host_template;
- sht->name = name;
- sht->proc_name = name;
- sht->module = owner;
- }
- EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
- unsigned char usb_stor_sense_invalidCDB[18] = {
- [0] = 0x70,
- [2] = ILLEGAL_REQUEST,
- [7] = 0x0a,
- [12] = 0x24
- };
- EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
|