0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include <linux/blkdev.h>
0032 #include <linux/dma-mapping.h>
0033 #include <linux/module.h>
0034 #include <linux/mutex.h>
0035
0036 #include <scsi/scsi.h>
0037 #include <scsi/scsi_cmnd.h>
0038 #include <scsi/scsi_devinfo.h>
0039 #include <scsi/scsi_device.h>
0040 #include <scsi/scsi_eh.h>
0041
0042 #include "usb.h"
0043 #include <linux/usb/hcd.h>
0044 #include "scsiglue.h"
0045 #include "debug.h"
0046 #include "transport.h"
0047 #include "protocol.h"
0048
0049
0050
0051
0052
0053 #define VENDOR_ID_NOKIA 0x0421
0054 #define VENDOR_ID_NIKON 0x04b0
0055 #define VENDOR_ID_PENTAX 0x0a17
0056 #define VENDOR_ID_MOTOROLA 0x22b8
0057
0058
0059
0060
0061
0062 static const char* host_info(struct Scsi_Host *host)
0063 {
0064 struct us_data *us = host_to_us(host);
0065 return us->scsi_name;
0066 }
0067
0068 static int slave_alloc (struct scsi_device *sdev)
0069 {
0070 struct us_data *us = host_to_us(sdev->host);
0071
0072
0073
0074
0075
0076
0077 sdev->inquiry_len = 36;
0078
0079
0080
0081
0082
0083 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
0084
0085
0086 if (us->protocol == USB_PR_BULK && us->max_lun > 0)
0087 sdev->sdev_bflags |= BLIST_FORCELUN;
0088
0089 return 0;
0090 }
0091
0092 static int slave_configure(struct scsi_device *sdev)
0093 {
0094 struct us_data *us = host_to_us(sdev->host);
0095 struct device *dev = us->pusb_dev->bus->sysdev;
0096
0097
0098
0099
0100
0101
0102 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
0103 unsigned int max_sectors = 64;
0104
0105 if (us->fflags & US_FL_MAX_SECTORS_MIN)
0106 max_sectors = PAGE_SIZE >> 9;
0107 if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
0108 blk_queue_max_hw_sectors(sdev->request_queue,
0109 max_sectors);
0110 } else if (sdev->type == TYPE_TAPE) {
0111
0112
0113
0114
0115
0116 blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
0117 } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
0118
0119
0120
0121
0122 blk_queue_max_hw_sectors(sdev->request_queue, 2048);
0123 }
0124
0125
0126
0127
0128
0129 blk_queue_max_hw_sectors(sdev->request_queue,
0130 min_t(size_t, queue_max_hw_sectors(sdev->request_queue),
0131 dma_max_mapping_size(dev) >> SECTOR_SHIFT));
0132
0133
0134
0135
0136
0137
0138 if (!hcd_uses_dma(bus_to_hcd(us->pusb_dev->bus)) ||
0139 (bus_to_hcd(us->pusb_dev->bus)->localmem_pool != NULL))
0140 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
0141
0142
0143
0144
0145
0146
0147 if (sdev->type == TYPE_DISK) {
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
0158 case VENDOR_ID_NOKIA:
0159 case VENDOR_ID_NIKON:
0160 case VENDOR_ID_PENTAX:
0161 case VENDOR_ID_MOTOROLA:
0162 if (!(us->fflags & (US_FL_FIX_CAPACITY |
0163 US_FL_CAPACITY_OK)))
0164 us->fflags |= US_FL_CAPACITY_HEURISTICS;
0165 break;
0166 }
0167
0168
0169
0170
0171
0172
0173 if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
0174 sdev->use_10_for_ms = 1;
0175
0176
0177
0178
0179
0180 sdev->use_192_bytes_for_3f = 1;
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 if (us->fflags & US_FL_NO_WP_DETECT)
0192 sdev->skip_ms_page_3f = 1;
0193
0194
0195
0196
0197
0198 sdev->skip_ms_page_8 = 1;
0199
0200
0201
0202
0203
0204 sdev->skip_vpd_pages = !sdev->try_vpd_pages;
0205
0206
0207 sdev->no_report_opcodes = 1;
0208
0209
0210 sdev->no_write_same = 1;
0211
0212
0213
0214
0215
0216
0217 if (us->fflags & US_FL_FIX_CAPACITY)
0218 sdev->fix_capacity = 1;
0219
0220
0221
0222
0223
0224
0225 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
0226 sdev->guess_capacity = 1;
0227
0228
0229 if (us->fflags & US_FL_NO_READ_CAPACITY_16)
0230 sdev->no_read_capacity_16 = 1;
0231
0232
0233
0234
0235
0236
0237
0238 if (!(us->fflags & US_FL_NEEDS_CAP16))
0239 sdev->try_rc_10_first = 1;
0240
0241
0242
0243
0244
0245 if (sdev->scsi_level > SCSI_SPC_2 &&
0246 !(us->fflags & US_FL_BAD_SENSE))
0247 us->fflags |= US_FL_SANE_SENSE;
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257 sdev->retry_hwerror = 1;
0258
0259
0260
0261
0262
0263 sdev->allow_restart = 1;
0264
0265
0266
0267
0268
0269
0270 sdev->last_sector_bug = 1;
0271
0272
0273
0274
0275
0276
0277 if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
0278 US_FL_SCM_MULT_TARG)) &&
0279 us->protocol == USB_PR_BULK)
0280 us->use_last_sector_hacks = 1;
0281
0282
0283 if (us->fflags & US_FL_WRITE_CACHE)
0284 sdev->wce_default_on = 1;
0285
0286
0287 if (us->fflags & US_FL_BROKEN_FUA)
0288 sdev->broken_fua = 1;
0289
0290
0291 if (us->fflags & US_FL_ALWAYS_SYNC) {
0292
0293 sdev->skip_ms_page_8 = 1;
0294 sdev->skip_ms_page_3f = 1;
0295
0296 sdev->wce_default_on = 1;
0297 }
0298 } else {
0299
0300
0301
0302
0303
0304
0305 sdev->use_10_for_ms = 1;
0306
0307
0308 if (us->fflags & US_FL_NO_READ_DISC_INFO)
0309 sdev->no_read_disc_info = 1;
0310 }
0311
0312
0313
0314
0315
0316
0317
0318
0319 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
0320 sdev->scsi_level == SCSI_UNKNOWN)
0321 us->max_lun = 0;
0322
0323
0324
0325
0326
0327 if (us->fflags & US_FL_NOT_LOCKABLE)
0328 sdev->lockable = 0;
0329
0330
0331
0332
0333
0334 return 0;
0335 }
0336
0337 static int target_alloc(struct scsi_target *starget)
0338 {
0339 struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
0340
0341
0342
0343
0344
0345
0346
0347 starget->no_report_luns = 1;
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358 if (us->subclass == USB_SC_UFI)
0359 starget->pdt_1f_for_no_lun = 1;
0360
0361 return 0;
0362 }
0363
0364
0365
0366 static int queuecommand_lck(struct scsi_cmnd *srb)
0367 {
0368 void (*done)(struct scsi_cmnd *) = scsi_done;
0369 struct us_data *us = host_to_us(srb->device->host);
0370
0371
0372 if (us->srb != NULL) {
0373 dev_err(&us->pusb_intf->dev,
0374 "Error in %s: us->srb = %p\n", __func__, us->srb);
0375 return SCSI_MLQUEUE_HOST_BUSY;
0376 }
0377
0378
0379 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
0380 usb_stor_dbg(us, "Fail command during disconnect\n");
0381 srb->result = DID_NO_CONNECT << 16;
0382 done(srb);
0383 return 0;
0384 }
0385
0386 if ((us->fflags & US_FL_NO_ATA_1X) &&
0387 (srb->cmnd[0] == ATA_12 || srb->cmnd[0] == ATA_16)) {
0388 memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
0389 sizeof(usb_stor_sense_invalidCDB));
0390 srb->result = SAM_STAT_CHECK_CONDITION;
0391 done(srb);
0392 return 0;
0393 }
0394
0395
0396 us->srb = srb;
0397 complete(&us->cmnd_ready);
0398
0399 return 0;
0400 }
0401
0402 static DEF_SCSI_QCMD(queuecommand)
0403
0404
0405
0406
0407
0408
0409 static int command_abort(struct scsi_cmnd *srb)
0410 {
0411 struct us_data *us = host_to_us(srb->device->host);
0412
0413 usb_stor_dbg(us, "%s called\n", __func__);
0414
0415
0416
0417
0418
0419 scsi_lock(us_to_host(us));
0420
0421
0422 if (us->srb != srb) {
0423 scsi_unlock(us_to_host(us));
0424 usb_stor_dbg(us, "-- nothing to abort\n");
0425 return FAILED;
0426 }
0427
0428
0429
0430
0431
0432
0433
0434
0435 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
0436 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
0437 set_bit(US_FLIDX_ABORTING, &us->dflags);
0438 usb_stor_stop_transport(us);
0439 }
0440 scsi_unlock(us_to_host(us));
0441
0442
0443 wait_for_completion(&us->notify);
0444 return SUCCESS;
0445 }
0446
0447
0448
0449
0450
0451 static int device_reset(struct scsi_cmnd *srb)
0452 {
0453 struct us_data *us = host_to_us(srb->device->host);
0454 int result;
0455
0456 usb_stor_dbg(us, "%s called\n", __func__);
0457
0458
0459 mutex_lock(&(us->dev_mutex));
0460 result = us->transport_reset(us);
0461 mutex_unlock(&us->dev_mutex);
0462
0463 return result < 0 ? FAILED : SUCCESS;
0464 }
0465
0466
0467 static int bus_reset(struct scsi_cmnd *srb)
0468 {
0469 struct us_data *us = host_to_us(srb->device->host);
0470 int result;
0471
0472 usb_stor_dbg(us, "%s called\n", __func__);
0473
0474 result = usb_stor_port_reset(us);
0475 return result < 0 ? FAILED : SUCCESS;
0476 }
0477
0478
0479
0480
0481
0482
0483 void usb_stor_report_device_reset(struct us_data *us)
0484 {
0485 int i;
0486 struct Scsi_Host *host = us_to_host(us);
0487
0488 scsi_report_device_reset(host, 0, 0);
0489 if (us->fflags & US_FL_SCM_MULT_TARG) {
0490 for (i = 1; i < host->max_id; ++i)
0491 scsi_report_device_reset(host, 0, i);
0492 }
0493 }
0494
0495
0496
0497
0498
0499
0500 void usb_stor_report_bus_reset(struct us_data *us)
0501 {
0502 struct Scsi_Host *host = us_to_host(us);
0503
0504 scsi_lock(host);
0505 scsi_report_bus_reset(host, 0);
0506 scsi_unlock(host);
0507 }
0508
0509
0510
0511
0512
0513 static int write_info(struct Scsi_Host *host, char *buffer, int length)
0514 {
0515
0516 return length;
0517 }
0518
0519 static int show_info (struct seq_file *m, struct Scsi_Host *host)
0520 {
0521 struct us_data *us = host_to_us(host);
0522 const char *string;
0523
0524
0525 seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
0526
0527
0528 if (us->pusb_dev->manufacturer)
0529 string = us->pusb_dev->manufacturer;
0530 else if (us->unusual_dev->vendorName)
0531 string = us->unusual_dev->vendorName;
0532 else
0533 string = "Unknown";
0534 seq_printf(m, " Vendor: %s\n", string);
0535 if (us->pusb_dev->product)
0536 string = us->pusb_dev->product;
0537 else if (us->unusual_dev->productName)
0538 string = us->unusual_dev->productName;
0539 else
0540 string = "Unknown";
0541 seq_printf(m, " Product: %s\n", string);
0542 if (us->pusb_dev->serial)
0543 string = us->pusb_dev->serial;
0544 else
0545 string = "None";
0546 seq_printf(m, "Serial Number: %s\n", string);
0547
0548
0549 seq_printf(m, " Protocol: %s\n", us->protocol_name);
0550 seq_printf(m, " Transport: %s\n", us->transport_name);
0551
0552
0553 seq_printf(m, " Quirks:");
0554
0555 #define US_FLAG(name, value) \
0556 if (us->fflags & value) seq_printf(m, " " #name);
0557 US_DO_ALL_FLAGS
0558 #undef US_FLAG
0559 seq_putc(m, '\n');
0560 return 0;
0561 }
0562
0563
0564
0565
0566
0567
0568 static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
0569 {
0570 struct scsi_device *sdev = to_scsi_device(dev);
0571
0572 return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
0573 }
0574
0575
0576 static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
0577 size_t count)
0578 {
0579 struct scsi_device *sdev = to_scsi_device(dev);
0580 unsigned short ms;
0581
0582 if (sscanf(buf, "%hu", &ms) > 0) {
0583 blk_queue_max_hw_sectors(sdev->request_queue, ms);
0584 return count;
0585 }
0586 return -EINVAL;
0587 }
0588 static DEVICE_ATTR_RW(max_sectors);
0589
0590 static struct attribute *usb_sdev_attrs[] = {
0591 &dev_attr_max_sectors.attr,
0592 NULL,
0593 };
0594
0595 ATTRIBUTE_GROUPS(usb_sdev);
0596
0597
0598
0599
0600
0601 static const struct scsi_host_template usb_stor_host_template = {
0602
0603 .name = "usb-storage",
0604 .proc_name = "usb-storage",
0605 .show_info = show_info,
0606 .write_info = write_info,
0607 .info = host_info,
0608
0609
0610 .queuecommand = queuecommand,
0611
0612
0613 .eh_abort_handler = command_abort,
0614 .eh_device_reset_handler = device_reset,
0615 .eh_bus_reset_handler = bus_reset,
0616
0617
0618 .can_queue = 1,
0619
0620
0621 .this_id = -1,
0622
0623 .slave_alloc = slave_alloc,
0624 .slave_configure = slave_configure,
0625 .target_alloc = target_alloc,
0626
0627
0628 .sg_tablesize = SG_MAX_SEGMENTS,
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648 .max_sectors = 240,
0649
0650
0651 .emulated = 1,
0652
0653
0654 .skip_settle_delay = 1,
0655
0656
0657 .sdev_groups = usb_sdev_groups,
0658
0659
0660 .module = THIS_MODULE
0661 };
0662
0663 void usb_stor_host_template_init(struct scsi_host_template *sht,
0664 const char *name, struct module *owner)
0665 {
0666 *sht = usb_stor_host_template;
0667 sht->name = name;
0668 sht->proc_name = name;
0669 sht->module = owner;
0670 }
0671 EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
0672
0673
0674 unsigned char usb_stor_sense_invalidCDB[18] = {
0675 [0] = 0x70,
0676 [2] = ILLEGAL_REQUEST,
0677 [7] = 0x0a,
0678 [12] = 0x24
0679 };
0680 EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);