-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
usb_disk.cpp
1437 lines (1205 loc) · 40.2 KB
/
usb_disk.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008-2012, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Michael Lotz <mmlr@mlotz.ch>
*/
#include "usb_disk.h"
#include <ByteOrder.h>
#include <Drivers.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fs/devfs.h>
#include "scsi_sense.h"
#include "usb_disk_scsi.h"
#define DRIVER_NAME "usb_disk"
#define DEVICE_NAME_BASE "disk/usb/"
#define DEVICE_NAME DEVICE_NAME_BASE"%" B_PRIu32 "/%d/raw"
//#define TRACE_USB_DISK
#ifdef TRACE_USB_DISK
#define TRACE(x...) dprintf(DRIVER_NAME ": " x)
#define TRACE_ALWAYS(x...) dprintf(DRIVER_NAME ": " x)
#else
#define TRACE(x...) /* nothing */
#define TRACE_ALWAYS(x...) dprintf(DRIVER_NAME ": " x)
#endif
int32 api_version = B_CUR_DRIVER_API_VERSION;
static usb_module_info *gUSBModule = NULL;
static disk_device *gDeviceList = NULL;
static uint32 gDeviceCount = 0;
static uint32 gLunCount = 0;
static mutex gDeviceListLock;
static char **gDeviceNames = NULL;
static const uint8 kDeviceIcon[] = {
0x6e, 0x63, 0x69, 0x66, 0x0a, 0x04, 0x01, 0x73, 0x05, 0x01, 0x02, 0x01,
0x06, 0x02, 0xb1, 0xf8, 0x5d, 0x3a, 0x2f, 0xbf, 0xbe, 0xdb, 0x67, 0xb6,
0x98, 0x06, 0x4b, 0x22, 0x15, 0x47, 0x13, 0x02, 0x00, 0xed, 0xed, 0xed,
0xff, 0xab, 0xbc, 0xc6, 0x02, 0x01, 0x06, 0x02, 0xb9, 0x82, 0x56, 0x32,
0x7d, 0xfb, 0xb8, 0x06, 0x39, 0xbe, 0xd9, 0xb5, 0x4b, 0x7d, 0x31, 0x4a,
0xa4, 0xe7, 0x00, 0xd1, 0xde, 0xe4, 0xff, 0x7a, 0x9c, 0xae, 0x02, 0x00,
0x16, 0x02, 0x38, 0xe9, 0xaa, 0x3b, 0x7b, 0x1d, 0xbf, 0xb0, 0xa6, 0x3d,
0x16, 0x76, 0x4b, 0x84, 0x81, 0x48, 0x37, 0x36, 0x00, 0x99, 0xff, 0x53,
0x02, 0x00, 0x16, 0x02, 0xba, 0x38, 0x9a, 0xb8, 0xef, 0x79, 0x3e, 0x34,
0x8b, 0xbf, 0x56, 0x52, 0x48, 0x2c, 0x61, 0x4c, 0x4e, 0xec, 0x00, 0x40,
0xff, 0x01, 0x05, 0xff, 0x05, 0x46, 0x02, 0x01, 0x16, 0x02, 0x35, 0xc2,
0x71, 0x3a, 0xf6, 0x84, 0xb9, 0xf3, 0x5b, 0x34, 0x81, 0xa0, 0x49, 0xc0,
0x57, 0x49, 0x6e, 0x51, 0xff, 0xf3, 0x00, 0x52, 0x02, 0x01, 0x06, 0x02,
0x38, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x80, 0x00,
0x49, 0xa0, 0x00, 0x49, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x06,
0xe3, 0x06, 0x0c, 0x06, 0x09, 0xab, 0xaa, 0x03, 0x3e, 0x5e, 0x3c, 0x5f,
0x3e, 0x5e, 0x60, 0x4d, 0x5a, 0x4a, 0x60, 0x47, 0x56, 0x42, 0x50, 0x45,
0x4c, 0x43, 0x2a, 0x54, 0x36, 0x5d, 0x36, 0x5d, 0x3a, 0x60, 0x06, 0x08,
0xfb, 0xea, 0x27, 0x49, 0x26, 0x48, 0x27, 0x49, 0x32, 0x56, 0x37, 0x59,
0x37, 0x59, 0x38, 0x5a, 0x3b, 0x59, 0x3a, 0x5a, 0x3b, 0x59, 0x58, 0x3c,
0x52, 0x36, 0x43, 0x29, 0x27, 0x45, 0x27, 0x45, 0x26, 0x46, 0x06, 0x05,
0xab, 0x03, 0x27, 0x49, 0x26, 0x48, 0x27, 0x49, 0x32, 0x56, 0x52, 0x36,
0x43, 0x29, 0x27, 0x45, 0x27, 0x45, 0x26, 0x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 0xe2, 0x3b, 0x54,
0xe2, 0x3f, 0xe9, 0x8e, 0xc8, 0xa5, 0xc8, 0x48, 0xeb, 0x05
};
//
//#pragma mark - Forward Declarations
//
static void usb_disk_callback(void *cookie, status_t status, void *data,
size_t actualLength);
status_t usb_disk_mass_storage_reset(disk_device *device);
uint8 usb_disk_get_max_lun(disk_device *device);
void usb_disk_reset_recovery(disk_device *device);
status_t usb_disk_transfer_data(disk_device *device, bool directionIn,
void *data, size_t dataLength);
status_t usb_disk_receive_csw(disk_device *device,
usb_massbulk_command_status_wrapper *status);
status_t usb_disk_operation(device_lun *lun, uint8 operation,
uint8 opLength, uint32 logicalBlockAddress,
uint16 transferLength, void *data, size_t *dataLength,
bool directionIn, err_act *action = NULL);
status_t usb_disk_request_sense(device_lun *lun, err_act *action);
status_t usb_disk_mode_sense(device_lun *lun);
status_t usb_disk_test_unit_ready(device_lun *lun, err_act *action = NULL);
status_t usb_disk_inquiry(device_lun *lun);
status_t usb_disk_reset_capacity(device_lun *lun);
status_t usb_disk_update_capacity(device_lun *lun);
status_t usb_disk_synchronize(device_lun *lun, bool force);
//
//#pragma mark - Device Allocation Helper Functions
//
void
usb_disk_free_device_and_luns(disk_device *device)
{
mutex_lock(&device->lock);
mutex_destroy(&device->lock);
delete_sem(device->notify);
for (uint8 i = 0; i < device->lun_count; i++)
free(device->luns[i]);
free(device->luns);
free(device);
}
//
//#pragma mark - Bulk-only Mass Storage Functions
//
status_t
usb_disk_mass_storage_reset(disk_device *device)
{
return gUSBModule->send_request(device->device, USB_REQTYPE_INTERFACE_OUT
| USB_REQTYPE_CLASS, USB_MASSBULK_REQUEST_MASS_STORAGE_RESET, 0x0000,
device->interface, 0, NULL, NULL);
}
uint8
usb_disk_get_max_lun(disk_device *device)
{
uint8 result = 0;
size_t actualLength = 0;
// devices that do not support multiple LUNs may stall this request
if (gUSBModule->send_request(device->device, USB_REQTYPE_INTERFACE_IN
| USB_REQTYPE_CLASS, USB_MASSBULK_REQUEST_GET_MAX_LUN, 0x0000,
device->interface, 1, &result, &actualLength) != B_OK
|| actualLength != 1) {
return 0;
}
if (result > MAX_LOGICAL_UNIT_NUMBER) {
// invalid max lun
return 0;
}
return result;
}
void
usb_disk_reset_recovery(disk_device *device)
{
usb_disk_mass_storage_reset(device);
gUSBModule->clear_feature(device->bulk_in, USB_FEATURE_ENDPOINT_HALT);
gUSBModule->clear_feature(device->bulk_out, USB_FEATURE_ENDPOINT_HALT);
}
status_t
usb_disk_transfer_data(disk_device *device, bool directionIn, void *data,
size_t dataLength)
{
status_t result = gUSBModule->queue_bulk(directionIn ? device->bulk_in
: device->bulk_out, data, dataLength, usb_disk_callback, device);
if (result != B_OK) {
TRACE_ALWAYS("failed to queue data transfer: %s\n", strerror(result));
return result;
}
do {
result = acquire_sem_etc(device->notify, 1, B_RELATIVE_TIMEOUT,
10 * 1000 * 1000);
if (result == B_TIMED_OUT) {
// Cancel the transfer and collect the sem that should now be
// released through the callback on cancel. Handling of device
// reset is done in usb_disk_operation() when it detects that
// the transfer failed.
gUSBModule->cancel_queued_transfers(directionIn ? device->bulk_in
: device->bulk_out);
acquire_sem_etc(device->notify, 1, B_RELATIVE_TIMEOUT, 0);
}
} while (result == B_INTERRUPTED);
if (result != B_OK) {
TRACE_ALWAYS("acquire_sem failed while waiting for data transfer: %s\n",
strerror(result));
return result;
}
return B_OK;
}
status_t
usb_disk_receive_csw(disk_device *device,
usb_massbulk_command_status_wrapper *status)
{
status_t result = usb_disk_transfer_data(device, true, status,
sizeof(usb_massbulk_command_status_wrapper));
if (result != B_OK)
return result;
if (device->status != B_OK
|| device->actual_length
!= sizeof(usb_massbulk_command_status_wrapper)) {
// receiving the command status wrapper failed
return B_ERROR;
}
return B_OK;
}
status_t
usb_disk_operation(device_lun *lun, uint8 operation, uint8 opLength,
uint32 logicalBlockAddress, uint16 transferLength, void *data,
size_t *dataLength, bool directionIn, err_act *_action)
{
TRACE("operation: lun: %u; op: %u; oplen: %u; lba: %" B_PRIu32
"; tlen: %u; data: %p; dlen: %p (%lu); in: %c\n",
lun->logical_unit_number, operation, opLength, logicalBlockAddress,
transferLength, data, dataLength, dataLength ? *dataLength : 0,
directionIn ? 'y' : 'n');
disk_device *device = lun->device;
usb_massbulk_command_block_wrapper command;
command.signature = USB_MASSBULK_CBW_SIGNATURE;
command.tag = device->current_tag++;
command.data_transfer_length = (dataLength != NULL ? *dataLength : 0);
command.flags = (directionIn ? USB_MASSBULK_CBW_DATA_INPUT
: USB_MASSBULK_CBW_DATA_OUTPUT);
command.lun = lun->logical_unit_number;
command.command_block_length
= device->is_atapi ? ATAPI_COMMAND_LENGTH : opLength;
memset(command.command_block, 0, sizeof(command.command_block));
switch (opLength) {
case 6:
{
scsi_command_6 *commandBlock
= (scsi_command_6 *)command.command_block;
commandBlock->operation = operation;
commandBlock->lun = lun->logical_unit_number << 5;
commandBlock->allocation_length = (uint8)transferLength;
if (operation == SCSI_MODE_SENSE_6) {
// we hijack the lba argument to transport the desired page
commandBlock->reserved[1] = (uint8)logicalBlockAddress;
}
break;
}
case 10:
{
scsi_command_10 *commandBlock
= (scsi_command_10 *)command.command_block;
commandBlock->operation = operation;
commandBlock->lun_flags = lun->logical_unit_number << 5;
commandBlock->logical_block_address = htonl(logicalBlockAddress);
commandBlock->transfer_length = htons(transferLength);
break;
}
default:
TRACE_ALWAYS("unsupported operation length %d\n", opLength);
return B_BAD_VALUE;
}
status_t result = usb_disk_transfer_data(device, false, &command,
sizeof(usb_massbulk_command_block_wrapper));
if (result != B_OK)
return result;
if (device->status != B_OK ||
device->actual_length != sizeof(usb_massbulk_command_block_wrapper)) {
// sending the command block wrapper failed
TRACE_ALWAYS("sending the command block wrapper failed: %s\n",
strerror(device->status));
usb_disk_reset_recovery(device);
return B_ERROR;
}
size_t transferedData = 0;
if (data != NULL && dataLength != NULL && *dataLength > 0) {
// we have data to transfer in a data stage
result = usb_disk_transfer_data(device, directionIn, data,
*dataLength);
if (result != B_OK)
return result;
transferedData = device->actual_length;
if (device->status != B_OK || transferedData != *dataLength) {
// sending or receiving of the data failed
if (device->status == B_DEV_STALLED) {
TRACE("stall while transfering data\n");
gUSBModule->clear_feature(directionIn ? device->bulk_in
: device->bulk_out, USB_FEATURE_ENDPOINT_HALT);
} else {
TRACE_ALWAYS("sending or receiving of the data failed: %s\n",
strerror(device->status));
usb_disk_reset_recovery(device);
return B_ERROR;
}
}
}
usb_massbulk_command_status_wrapper status;
result = usb_disk_receive_csw(device, &status);
if (result != B_OK) {
// in case of a stall or error clear the stall and try again
gUSBModule->clear_feature(device->bulk_in, USB_FEATURE_ENDPOINT_HALT);
result = usb_disk_receive_csw(device, &status);
}
if (result != B_OK) {
TRACE_ALWAYS("receiving the command status wrapper failed: %s\n",
strerror(result));
usb_disk_reset_recovery(device);
return result;
}
if (status.signature != USB_MASSBULK_CSW_SIGNATURE
|| status.tag != command.tag) {
// the command status wrapper is not valid
TRACE_ALWAYS("command status wrapper is not valid: %#" B_PRIx32 "\n",
status.signature);
usb_disk_reset_recovery(device);
return B_ERROR;
}
switch (status.status) {
case USB_MASSBULK_CSW_STATUS_COMMAND_PASSED:
case USB_MASSBULK_CSW_STATUS_COMMAND_FAILED:
{
// The residue from "status.data_residue" is not maintained
// correctly by some devices, so calculate it instead.
uint32 residue = command.data_transfer_length - transferedData;
if (dataLength != NULL) {
*dataLength -= residue;
if (transferedData < *dataLength) {
TRACE_ALWAYS("less data transfered than indicated: %"
B_PRIuSIZE " vs. %" B_PRIuSIZE "\n", transferedData,
*dataLength);
*dataLength = transferedData;
}
}
if (status.status == USB_MASSBULK_CSW_STATUS_COMMAND_PASSED) {
// the operation is complete and has succeeded
return B_OK;
} else {
if (operation == SCSI_REQUEST_SENSE_6)
return B_ERROR;
// the operation is complete but has failed at the SCSI level
if (operation != SCSI_TEST_UNIT_READY_6) {
TRACE_ALWAYS("operation %#" B_PRIx8
" failed at the SCSI level\n", operation);
}
result = usb_disk_request_sense(lun, _action);
return result == B_OK ? B_ERROR : result;
}
}
case USB_MASSBULK_CSW_STATUS_PHASE_ERROR:
{
// a protocol or device error occured
TRACE_ALWAYS("phase error in operation %#" B_PRIx8 "\n", operation);
usb_disk_reset_recovery(device);
return B_ERROR;
}
default:
{
// command status wrapper is not meaningful
TRACE_ALWAYS("command status wrapper has invalid status\n");
usb_disk_reset_recovery(device);
return B_ERROR;
}
}
}
//
//#pragma mark - Helper/Convenience Functions
//
status_t
usb_disk_request_sense(device_lun *lun, err_act *_action)
{
size_t dataLength = sizeof(scsi_request_sense_6_parameter);
scsi_request_sense_6_parameter parameter;
status_t result = usb_disk_operation(lun, SCSI_REQUEST_SENSE_6, 6, 0,
dataLength, ¶meter, &dataLength, true);
if (result != B_OK) {
TRACE_ALWAYS("getting request sense data failed: %s\n",
strerror(result));
return result;
}
const char *label = NULL;
err_act action = err_act_fail;
status_t status = B_ERROR;
scsi_get_sense_asc_info((parameter.additional_sense_code << 8)
| parameter.additional_sense_code_qualifier, &label, &action,
&status);
if (parameter.sense_key > SCSI_SENSE_KEY_NOT_READY
&& parameter.sense_key != SCSI_SENSE_KEY_UNIT_ATTENTION) {
TRACE_ALWAYS("request_sense: key: 0x%02x; asc: 0x%02x; ascq: "
"0x%02x; %s\n", parameter.sense_key, parameter.additional_sense_code,
parameter.additional_sense_code_qualifier,
label ? label : "(unknown)");
}
if ((parameter.additional_sense_code == 0
&& parameter.additional_sense_code_qualifier == 0)
|| label == NULL) {
scsi_get_sense_key_info(parameter.sense_key, &label, &action, &status);
}
if (status == B_DEV_MEDIA_CHANGED) {
lun->media_changed = true;
lun->media_present = true;
} else if (parameter.sense_key == SCSI_SENSE_KEY_UNIT_ATTENTION
&& status != B_DEV_NO_MEDIA) {
lun->media_present = true;
} else if (status == B_DEV_NOT_READY) {
lun->media_present = false;
usb_disk_reset_capacity(lun);
}
if (_action != NULL)
*_action = action;
return status;
}
status_t
usb_disk_mode_sense(device_lun *lun)
{
size_t dataLength = sizeof(scsi_mode_sense_6_parameter);
scsi_mode_sense_6_parameter parameter;
status_t result = usb_disk_operation(lun, SCSI_MODE_SENSE_6, 6,
SCSI_MODE_PAGE_DEVICE_CONFIGURATION, dataLength, ¶meter,
&dataLength, true);
if (result != B_OK) {
TRACE_ALWAYS("getting mode sense data failed: %s\n", strerror(result));
return result;
}
lun->write_protected
= (parameter.device_specific & SCSI_DEVICE_SPECIFIC_WRITE_PROTECT)
!= 0;
TRACE_ALWAYS("write protected: %s\n", lun->write_protected ? "yes" : "no");
return B_OK;
}
status_t
usb_disk_test_unit_ready(device_lun *lun, err_act *_action)
{
// if unsupported we assume the unit is fixed and therefore always ok
if (!lun->device->tur_supported)
return B_OK;
status_t result;
if (lun->device->is_atapi) {
result = usb_disk_operation(lun, SCSI_START_STOP_UNIT_6, 6, 0, 1,
NULL, NULL, false, _action);
} else {
result = usb_disk_operation(lun, SCSI_TEST_UNIT_READY_6, 6, 0, 0,
NULL, NULL, true, _action);
}
if (result == B_DEV_INVALID_IOCTL) {
lun->device->tur_supported = false;
return B_OK;
}
return result;
}
status_t
usb_disk_inquiry(device_lun *lun)
{
size_t dataLength = sizeof(scsi_inquiry_6_parameter);
scsi_inquiry_6_parameter parameter;
status_t result = B_ERROR;
err_act action = err_act_ok;
for (uint32 tries = 0; tries < 3; tries++) {
result = usb_disk_operation(lun, SCSI_INQUIRY_6, 6, 0, dataLength,
¶meter, &dataLength, true, &action);
if (result == B_OK || (action != err_act_retry
&& action != err_act_many_retries)) {
break;
}
}
if (result != B_OK) {
TRACE_ALWAYS("getting inquiry data failed: %s\n", strerror(result));
lun->device_type = B_DISK;
lun->removable = true;
return result;
}
TRACE("peripherial_device_type 0x%02x\n",
parameter.peripherial_device_type);
TRACE("peripherial_qualifier 0x%02x\n",
parameter.peripherial_qualifier);
TRACE("removable_medium %s\n",
parameter.removable_medium ? "yes" : "no");
TRACE("version 0x%02x\n", parameter.version);
TRACE("response_data_format 0x%02x\n", parameter.response_data_format);
TRACE_ALWAYS("vendor_identification \"%.8s\"\n",
parameter.vendor_identification);
TRACE_ALWAYS("product_identification \"%.16s\"\n",
parameter.product_identification);
TRACE_ALWAYS("product_revision_level \"%.4s\"\n",
parameter.product_revision_level);
memcpy(lun->vendor_name, parameter.vendor_identification,
MIN(sizeof(lun->vendor_name), sizeof(parameter.vendor_identification)));
memcpy(lun->product_name, parameter.product_identification,
MIN(sizeof(lun->product_name),
sizeof(parameter.product_identification)));
memcpy(lun->product_revision, parameter.product_revision_level,
MIN(sizeof(lun->product_revision),
sizeof(parameter.product_revision_level)));
lun->device_type = parameter.peripherial_device_type; /* 1:1 mapping */
lun->removable = (parameter.removable_medium == 1);
return B_OK;
}
status_t
usb_disk_reset_capacity(device_lun *lun)
{
lun->block_size = 512;
lun->block_count = 0;
return B_OK;
}
status_t
usb_disk_update_capacity(device_lun *lun)
{
size_t dataLength = sizeof(scsi_read_capacity_10_parameter);
scsi_read_capacity_10_parameter parameter;
status_t result = B_ERROR;
err_act action = err_act_ok;
// Retry reading the capacity up to three times. The first try might only
// yield a unit attention telling us that the device or media status
// changed, which is more or less expected if it is the first operation
// on the device or the device only clears the unit atention for capacity
// reads.
for (int32 i = 0; i < 3; i++) {
result = usb_disk_operation(lun, SCSI_READ_CAPACITY_10, 10, 0, 0,
¶meter, &dataLength, true, &action);
if (result == B_OK || (action != err_act_retry
&& action != err_act_many_retries)) {
break;
}
}
if (result != B_OK) {
TRACE_ALWAYS("failed to update capacity: %s\n", strerror(result));
lun->media_present = false;
lun->media_changed = false;
usb_disk_reset_capacity(lun);
return result;
}
lun->media_present = true;
lun->media_changed = false;
lun->block_size = ntohl(parameter.logical_block_length);
lun->block_count = ntohl(parameter.last_logical_block_address) + 1;
return B_OK;
}
status_t
usb_disk_synchronize(device_lun *lun, bool force)
{
if (lun->device->sync_support == 0) {
// this device reported an illegal request when syncing or repeatedly
// returned an other error, it apparently does not support syncing...
return B_UNSUPPORTED;
}
if (!lun->should_sync && !force)
return B_OK;
status_t result = usb_disk_operation(lun, SCSI_SYNCHRONIZE_CACHE_10, 10,
0, 0, NULL, NULL, false);
if (result == B_OK) {
lun->device->sync_support = SYNC_SUPPORT_RELOAD;
lun->should_sync = false;
return B_OK;
}
if (result == B_DEV_INVALID_IOCTL)
lun->device->sync_support = 0;
else
lun->device->sync_support--;
return result;
}
//
//#pragma mark - Device Attach/Detach Notifications and Callback
//
static void
usb_disk_callback(void *cookie, status_t status, void *data,
size_t actualLength)
{
//TRACE("callback()\n");
disk_device *device = (disk_device *)cookie;
device->status = status;
device->actual_length = actualLength;
release_sem(device->notify);
}
static status_t
usb_disk_device_added(usb_device newDevice, void **cookie)
{
TRACE("device_added(0x%08" B_PRIx32 ")\n", newDevice);
disk_device *device = (disk_device *)malloc(sizeof(disk_device));
device->device = newDevice;
device->removed = false;
device->open_count = 0;
device->interface = 0xff;
device->current_tag = 0;
device->sync_support = SYNC_SUPPORT_RELOAD;
device->tur_supported = true;
device->is_atapi = false;
device->luns = NULL;
// scan through the interfaces to find our bulk-only data interface
const usb_configuration_info *configuration
= gUSBModule->get_configuration(newDevice);
if (configuration == NULL) {
free(device);
return B_ERROR;
}
for (size_t i = 0; i < configuration->interface_count; i++) {
usb_interface_info *interface = configuration->interface[i].active;
if (interface == NULL)
continue;
if (interface->descr->interface_class == USB_MASS_STORAGE_DEVICE_CLASS
&& (interface->descr->interface_subclass == 0x06 /* SCSI */
|| interface->descr->interface_subclass == 0x02 /* ATAPI */
|| interface->descr->interface_subclass == 0x05 /* ATAPI */)
&& interface->descr->interface_protocol == 0x50 /* bulk-only */) {
bool hasIn = false;
bool hasOut = false;
for (size_t j = 0; j < interface->endpoint_count; j++) {
usb_endpoint_info *endpoint = &interface->endpoint[j];
if (endpoint == NULL
|| endpoint->descr->attributes != USB_ENDPOINT_ATTR_BULK)
continue;
if (!hasIn && (endpoint->descr->endpoint_address
& USB_ENDPOINT_ADDR_DIR_IN) != 0) {
device->bulk_in = endpoint->handle;
hasIn = true;
} else if (!hasOut && (endpoint->descr->endpoint_address
& USB_ENDPOINT_ADDR_DIR_IN) == 0) {
device->bulk_out = endpoint->handle;
hasOut = true;
}
if (hasIn && hasOut)
break;
}
if (!(hasIn && hasOut))
continue;
device->interface = interface->descr->interface_number;
device->is_atapi = interface->descr->interface_subclass != 0x06;
break;
}
}
if (device->interface == 0xff) {
TRACE_ALWAYS("no valid bulk-only interface found\n");
free(device);
return B_ERROR;
}
mutex_init(&device->lock, "usb_disk device lock");
device->notify = create_sem(0, "usb_disk callback notify");
if (device->notify < B_OK) {
mutex_destroy(&device->lock);
status_t result = device->notify;
free(device);
return result;
}
device->lun_count = usb_disk_get_max_lun(device) + 1;
device->luns = (device_lun **)malloc(device->lun_count
* sizeof(device_lun *));
for (uint8 i = 0; i < device->lun_count; i++)
device->luns[i] = NULL;
status_t result = B_OK;
TRACE_ALWAYS("device reports a lun count of %d\n", device->lun_count);
for (uint8 i = 0; i < device->lun_count; i++) {
// create the individual luns present on this device
device_lun *lun = (device_lun *)malloc(sizeof(device_lun));
if (lun == NULL) {
result = B_NO_MEMORY;
break;
}
device->luns[i] = lun;
lun->device = device;
lun->logical_unit_number = i;
lun->should_sync = false;
lun->media_present = true;
lun->media_changed = true;
memset(lun->vendor_name, 0, sizeof(lun->vendor_name));
memset(lun->product_name, 0, sizeof(lun->product_name));
memset(lun->product_revision, 0, sizeof(lun->product_revision));
usb_disk_reset_capacity(lun);
// initialize this lun
result = usb_disk_inquiry(lun);
err_act action = err_act_ok;
for (uint32 tries = 0; tries < 8; tries++) {
TRACE("usb lun %" B_PRIu8 " inquiry attempt %" B_PRIu32 " begin\n",
i, tries);
status_t ready = usb_disk_test_unit_ready(lun, &action);
if (ready == B_OK || ready == B_DEV_NO_MEDIA
|| ready == B_DEV_MEDIA_CHANGED) {
if (lun->device_type == B_CD)
lun->write_protected = true;
// TODO: check for write protection; disabled since some
// devices lock up when getting the mode sense
else if (/*usb_disk_mode_sense(lun) != B_OK*/true)
lun->write_protected = false;
TRACE("usb lun %" B_PRIu8 " ready. write protected = %c%s\n", i,
lun->write_protected ? 'y' : 'n',
ready == B_DEV_NO_MEDIA ? " (no media inserted)" : "");
break;
}
TRACE("usb lun %" B_PRIu8 " inquiry attempt %" B_PRIu32 " failed\n",
i, tries);
if (action != err_act_retry && action != err_act_many_retries)
break;
bigtime_t snoozeTime = 1000000 * tries;
TRACE("snoozing %" B_PRIu64 " microseconds for usb lun\n",
snoozeTime);
snooze(snoozeTime);
}
if (result != B_OK)
break;
}
if (result != B_OK) {
TRACE_ALWAYS("failed to initialize logical units: %s\n",
strerror(result));
usb_disk_free_device_and_luns(device);
return result;
}
mutex_lock(&gDeviceListLock);
device->device_number = 0;
disk_device *other = gDeviceList;
while (other != NULL) {
if (other->device_number >= device->device_number)
device->device_number = other->device_number + 1;
other = (disk_device *)other->link;
}
device->link = (void *)gDeviceList;
gDeviceList = device;
gLunCount += device->lun_count;
for (uint8 i = 0; i < device->lun_count; i++)
sprintf(device->luns[i]->name, DEVICE_NAME, device->device_number, i);
mutex_unlock(&gDeviceListLock);
TRACE("new device: 0x%p\n", device);
*cookie = (void *)device;
return B_OK;
}
static status_t
usb_disk_device_removed(void *cookie)
{
TRACE("device_removed(0x%p)\n", cookie);
disk_device *device = (disk_device *)cookie;
mutex_lock(&gDeviceListLock);
if (gDeviceList == device) {
gDeviceList = (disk_device *)device->link;
} else {
disk_device *element = gDeviceList;
while (element) {
if (element->link == device) {
element->link = device->link;
break;
}
element = (disk_device *)element->link;
}
}
gLunCount -= device->lun_count;
gDeviceCount--;
device->removed = true;
gUSBModule->cancel_queued_transfers(device->bulk_in);
gUSBModule->cancel_queued_transfers(device->bulk_out);
if (device->open_count == 0)
usb_disk_free_device_and_luns(device);
mutex_unlock(&gDeviceListLock);
return B_OK;
}
//
//#pragma mark - Partial Buffer Functions
//
static bool
usb_disk_needs_partial_buffer(device_lun *lun, off_t position, size_t length,
uint32 &blockPosition, uint16 &blockCount)
{
blockPosition = (uint32)(position / lun->block_size);
if ((off_t)blockPosition * lun->block_size != position)
return true;
blockCount = (uint16)(length / lun->block_size);
if ((size_t)blockCount * lun->block_size != length)
return true;
return false;
}
static status_t
usb_disk_block_read(device_lun *lun, uint32 blockPosition, uint16 blockCount,
void *buffer, size_t *length)
{
status_t result = usb_disk_operation(lun, SCSI_READ_10, 10, blockPosition,
blockCount, buffer, length, true);
return result;
}
static status_t
usb_disk_block_write(device_lun *lun, uint32 blockPosition, uint16 blockCount,
void *buffer, size_t *length)
{
status_t result = usb_disk_operation(lun, SCSI_WRITE_10, 10, blockPosition,
blockCount, buffer, length, false);
if (result == B_OK)
lun->should_sync = true;
return result;
}
static status_t
usb_disk_prepare_partial_buffer(device_lun *lun, off_t position, size_t length,
void *&partialBuffer, void *&blockBuffer, uint32 &blockPosition,
uint16 &blockCount)
{
blockPosition = (uint32)(position / lun->block_size);
blockCount = (uint16)((uint32)((position + length + lun->block_size - 1)
/ lun->block_size) - blockPosition);
size_t blockLength = blockCount * lun->block_size;
blockBuffer = malloc(blockLength);
if (blockBuffer == NULL) {
TRACE_ALWAYS("no memory to allocate partial buffer\n");
return B_NO_MEMORY;
}
status_t result = usb_disk_block_read(lun, blockPosition, blockCount,
blockBuffer, &blockLength);
if (result != B_OK) {
TRACE_ALWAYS("block read failed when filling partial buffer: %s\n",
strerror(result));
free(blockBuffer);
return result;
}
off_t offset = position - (off_t)blockPosition * lun->block_size;
partialBuffer = (uint8 *)blockBuffer + offset;
return B_OK;
}
//
//#pragma mark - Driver Hooks
//
static status_t
usb_disk_open(const char *name, uint32 flags, void **cookie)
{
TRACE("open(%s)\n", name);
if (strncmp(name, DEVICE_NAME_BASE, strlen(DEVICE_NAME_BASE)) != 0)
return B_NAME_NOT_FOUND;
int32 lastPart = 0;
size_t nameLength = strlen(name);
for (int32 i = nameLength - 1; i >= 0; i--) {
if (name[i] == '/') {
lastPart = i;
break;
}
}
char rawName[nameLength + 4];
strncpy(rawName, name, lastPart + 1);
rawName[lastPart + 1] = 0;
strcat(rawName, "raw");
TRACE("opening raw device %s for %s\n", rawName, name);