1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/module.h>
32#include <linux/mutex.h>
33
34#include <scsi/scsi.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_devinfo.h>
37#include <scsi/scsi_device.h>
38#include <scsi/scsi_eh.h>
39
40#include "usb.h"
41#include "scsiglue.h"
42#include "debug.h"
43#include "transport.h"
44#include "protocol.h"
45
46
47
48
49
50#define VENDOR_ID_NOKIA 0x0421
51#define VENDOR_ID_NIKON 0x04b0
52#define VENDOR_ID_PENTAX 0x0a17
53#define VENDOR_ID_MOTOROLA 0x22b8
54
55
56
57
58
59static const char* host_info(struct Scsi_Host *host)
60{
61 struct us_data *us = host_to_us(host);
62 return us->scsi_name;
63}
64
65static int slave_alloc (struct scsi_device *sdev)
66{
67 struct us_data *us = host_to_us(sdev->host);
68 int maxp;
69
70
71
72
73
74
75 sdev->inquiry_len = 36;
76
77
78
79
80
81
82
83 maxp = usb_maxpacket(us->pusb_dev, us->recv_bulk_pipe, 0);
84 blk_queue_virt_boundary(sdev->request_queue, maxp - 1);
85
86
87
88
89
90 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
91
92
93 if (us->protocol == USB_PR_BULK && us->max_lun > 0)
94 sdev->sdev_bflags |= BLIST_FORCELUN;
95
96 return 0;
97}
98
99static int slave_configure(struct scsi_device *sdev)
100{
101 struct us_data *us = host_to_us(sdev->host);
102
103
104
105
106
107
108 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
109 unsigned int max_sectors = 64;
110
111 if (us->fflags & US_FL_MAX_SECTORS_MIN)
112 max_sectors = PAGE_SIZE >> 9;
113 if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
114 blk_queue_max_hw_sectors(sdev->request_queue,
115 max_sectors);
116 } else if (sdev->type == TYPE_TAPE) {
117
118
119
120
121
122 blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
123 } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
124
125
126
127
128 blk_queue_max_hw_sectors(sdev->request_queue, 2048);
129 }
130
131
132
133
134
135
136
137 if (!us->pusb_dev->bus->controller->dma_mask)
138 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
139
140
141
142
143
144
145 if (sdev->type == TYPE_DISK) {
146
147
148
149
150
151
152
153
154
155 switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
156 case VENDOR_ID_NOKIA:
157 case VENDOR_ID_NIKON:
158 case VENDOR_ID_PENTAX:
159 case VENDOR_ID_MOTOROLA:
160 if (!(us->fflags & (US_FL_FIX_CAPACITY |
161 US_FL_CAPACITY_OK)))
162 us->fflags |= US_FL_CAPACITY_HEURISTICS;
163 break;
164 }
165
166
167
168
169
170
171 if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
172 sdev->use_10_for_ms = 1;
173
174
175
176
177
178 sdev->use_192_bytes_for_3f = 1;
179
180
181
182
183
184
185
186
187
188
189 if (us->fflags & US_FL_NO_WP_DETECT)
190 sdev->skip_ms_page_3f = 1;
191
192
193
194
195
196 sdev->skip_ms_page_8 = 1;
197
198
199 sdev->skip_vpd_pages = 1;
200
201
202 sdev->no_report_opcodes = 1;
203
204
205 sdev->no_write_same = 1;
206
207
208
209
210
211
212 if (us->fflags & US_FL_FIX_CAPACITY)
213 sdev->fix_capacity = 1;
214
215
216
217
218
219
220 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
221 sdev->guess_capacity = 1;
222
223
224 if (us->fflags & US_FL_NO_READ_CAPACITY_16)
225 sdev->no_read_capacity_16 = 1;
226
227
228
229
230
231
232
233 if (!(us->fflags & US_FL_NEEDS_CAP16))
234 sdev->try_rc_10_first = 1;
235
236
237
238
239
240 if (sdev->scsi_level > SCSI_SPC_2 &&
241 !(us->fflags & US_FL_BAD_SENSE))
242 us->fflags |= US_FL_SANE_SENSE;
243
244
245
246
247
248
249
250
251
252 sdev->retry_hwerror = 1;
253
254
255
256
257
258 sdev->allow_restart = 1;
259
260
261
262
263
264
265 sdev->last_sector_bug = 1;
266
267
268
269
270
271
272 if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
273 US_FL_SCM_MULT_TARG)) &&
274 us->protocol == USB_PR_BULK)
275 us->use_last_sector_hacks = 1;
276
277
278 if (us->fflags & US_FL_WRITE_CACHE)
279 sdev->wce_default_on = 1;
280
281
282 if (us->fflags & US_FL_BROKEN_FUA)
283 sdev->broken_fua = 1;
284
285
286 if (us->fflags & US_FL_ALWAYS_SYNC) {
287
288 sdev->skip_ms_page_8 = 1;
289 sdev->skip_ms_page_3f = 1;
290
291 sdev->wce_default_on = 1;
292 }
293 } else {
294
295
296
297
298
299
300 sdev->use_10_for_ms = 1;
301
302
303 if (us->fflags & US_FL_NO_READ_DISC_INFO)
304 sdev->no_read_disc_info = 1;
305 }
306
307
308
309
310
311
312
313
314 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
315 sdev->scsi_level == SCSI_UNKNOWN)
316 us->max_lun = 0;
317
318
319
320
321
322 if (us->fflags & US_FL_NOT_LOCKABLE)
323 sdev->lockable = 0;
324
325
326
327
328
329 return 0;
330}
331
332static int target_alloc(struct scsi_target *starget)
333{
334 struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
335
336
337
338
339
340
341
342 starget->no_report_luns = 1;
343
344
345
346
347
348
349
350
351
352
353 if (us->subclass == USB_SC_UFI)
354 starget->pdt_1f_for_no_lun = 1;
355
356 return 0;
357}
358
359
360
361static int queuecommand_lck(struct scsi_cmnd *srb,
362 void (*done)(struct scsi_cmnd *))
363{
364 struct us_data *us = host_to_us(srb->device->host);
365
366
367 if (us->srb != NULL) {
368 printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
369 __func__, us->srb);
370 return SCSI_MLQUEUE_HOST_BUSY;
371 }
372
373
374 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
375 usb_stor_dbg(us, "Fail command during disconnect\n");
376 srb->result = DID_NO_CONNECT << 16;
377 done(srb);
378 return 0;
379 }
380
381 if ((us->fflags & US_FL_NO_ATA_1X) &&
382 (srb->cmnd[0] == ATA_12 || srb->cmnd[0] == ATA_16)) {
383 memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
384 sizeof(usb_stor_sense_invalidCDB));
385 srb->result = SAM_STAT_CHECK_CONDITION;
386 done(srb);
387 return 0;
388 }
389
390
391 srb->scsi_done = done;
392 us->srb = srb;
393 complete(&us->cmnd_ready);
394
395 return 0;
396}
397
398static DEF_SCSI_QCMD(queuecommand)
399
400
401
402
403
404
405static int command_abort(struct scsi_cmnd *srb)
406{
407 struct us_data *us = host_to_us(srb->device->host);
408
409 usb_stor_dbg(us, "%s called\n", __func__);
410
411
412
413
414
415 scsi_lock(us_to_host(us));
416
417
418 if (us->srb != srb) {
419 scsi_unlock(us_to_host(us));
420 usb_stor_dbg(us, "-- nothing to abort\n");
421 return FAILED;
422 }
423
424
425
426
427
428
429
430
431 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
432 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
433 set_bit(US_FLIDX_ABORTING, &us->dflags);
434 usb_stor_stop_transport(us);
435 }
436 scsi_unlock(us_to_host(us));
437
438
439 wait_for_completion(&us->notify);
440 return SUCCESS;
441}
442
443
444
445
446
447static int device_reset(struct scsi_cmnd *srb)
448{
449 struct us_data *us = host_to_us(srb->device->host);
450 int result;
451
452 usb_stor_dbg(us, "%s called\n", __func__);
453
454
455 mutex_lock(&(us->dev_mutex));
456 result = us->transport_reset(us);
457 mutex_unlock(&us->dev_mutex);
458
459 return result < 0 ? FAILED : SUCCESS;
460}
461
462
463static int bus_reset(struct scsi_cmnd *srb)
464{
465 struct us_data *us = host_to_us(srb->device->host);
466 int result;
467
468 usb_stor_dbg(us, "%s called\n", __func__);
469
470 result = usb_stor_port_reset(us);
471 return result < 0 ? FAILED : SUCCESS;
472}
473
474
475
476
477
478
479void usb_stor_report_device_reset(struct us_data *us)
480{
481 int i;
482 struct Scsi_Host *host = us_to_host(us);
483
484 scsi_report_device_reset(host, 0, 0);
485 if (us->fflags & US_FL_SCM_MULT_TARG) {
486 for (i = 1; i < host->max_id; ++i)
487 scsi_report_device_reset(host, 0, i);
488 }
489}
490
491
492
493
494
495
496void usb_stor_report_bus_reset(struct us_data *us)
497{
498 struct Scsi_Host *host = us_to_host(us);
499
500 scsi_lock(host);
501 scsi_report_bus_reset(host, 0);
502 scsi_unlock(host);
503}
504
505
506
507
508
509static int write_info(struct Scsi_Host *host, char *buffer, int length)
510{
511
512 return length;
513}
514
515static int show_info (struct seq_file *m, struct Scsi_Host *host)
516{
517 struct us_data *us = host_to_us(host);
518 const char *string;
519
520
521 seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
522
523
524 if (us->pusb_dev->manufacturer)
525 string = us->pusb_dev->manufacturer;
526 else if (us->unusual_dev->vendorName)
527 string = us->unusual_dev->vendorName;
528 else
529 string = "Unknown";
530 seq_printf(m, " Vendor: %s\n", string);
531 if (us->pusb_dev->product)
532 string = us->pusb_dev->product;
533 else if (us->unusual_dev->productName)
534 string = us->unusual_dev->productName;
535 else
536 string = "Unknown";
537 seq_printf(m, " Product: %s\n", string);
538 if (us->pusb_dev->serial)
539 string = us->pusb_dev->serial;
540 else
541 string = "None";
542 seq_printf(m, "Serial Number: %s\n", string);
543
544
545 seq_printf(m, " Protocol: %s\n", us->protocol_name);
546 seq_printf(m, " Transport: %s\n", us->transport_name);
547
548
549 seq_printf(m, " Quirks:");
550
551#define US_FLAG(name, value) \
552 if (us->fflags & value) seq_printf(m, " " #name);
553US_DO_ALL_FLAGS
554#undef US_FLAG
555 seq_putc(m, '\n');
556 return 0;
557}
558
559
560
561
562
563
564static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
565{
566 struct scsi_device *sdev = to_scsi_device(dev);
567
568 return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
569}
570
571
572static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
573 size_t count)
574{
575 struct scsi_device *sdev = to_scsi_device(dev);
576 unsigned short ms;
577
578 if (sscanf(buf, "%hu", &ms) > 0) {
579 blk_queue_max_hw_sectors(sdev->request_queue, ms);
580 return count;
581 }
582 return -EINVAL;
583}
584static DEVICE_ATTR_RW(max_sectors);
585
586static struct device_attribute *sysfs_device_attr_list[] = {
587 &dev_attr_max_sectors,
588 NULL,
589};
590
591
592
593
594
595static const struct scsi_host_template usb_stor_host_template = {
596
597 .name = "usb-storage",
598 .proc_name = "usb-storage",
599 .show_info = show_info,
600 .write_info = write_info,
601 .info = host_info,
602
603
604 .queuecommand = queuecommand,
605
606
607 .eh_abort_handler = command_abort,
608 .eh_device_reset_handler = device_reset,
609 .eh_bus_reset_handler = bus_reset,
610
611
612 .can_queue = 1,
613
614
615 .this_id = -1,
616
617 .slave_alloc = slave_alloc,
618 .slave_configure = slave_configure,
619 .target_alloc = target_alloc,
620
621
622 .sg_tablesize = SG_MAX_SEGMENTS,
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642 .max_sectors = 240,
643
644
645 .emulated = 1,
646
647
648 .skip_settle_delay = 1,
649
650
651 .sdev_attrs = sysfs_device_attr_list,
652
653
654 .module = THIS_MODULE
655};
656
657void usb_stor_host_template_init(struct scsi_host_template *sht,
658 const char *name, struct module *owner)
659{
660 *sht = usb_stor_host_template;
661 sht->name = name;
662 sht->proc_name = name;
663 sht->module = owner;
664}
665EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
666
667
668unsigned char usb_stor_sense_invalidCDB[18] = {
669 [0] = 0x70,
670 [2] = ILLEGAL_REQUEST,
671 [7] = 0x0a,
672 [12] = 0x24
673};
674EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
675