6040feb7f4789df901a86c5095fa2d6095a14199
[pub/USBasp.git] / Demos / Host / LowLevel / MassStorageHost / Lib / MassStoreCommands.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Mass Storage Device commands, to issue MSD commands to the device for
34 * reading device status, capacity, and other characteristics. This file
35 * also contains block read and write functions, so that device blocks
36 * can be read and written. In general, these functions would be chained
37 * to a FAT library to give file-level access to an attached device's contents.
38 *
39 * \note Many Mass Storage devices on the market are non-compliant to the
40 * specifications and thus can prove difficult to interface with. It
41 * may be necessary to retry the functions in the module several times
42 * after they have returned and error to successfully send the command
43 * to the device. Some devices may also need to have the stream function
44 * timeout period extended beyond 100ms (some badly designed devices exceeding
45 * 1.5 seconds occasionally) by defining USB_STREAM_TIMEOUT_MS to a
46 * larger value in the project makefile and passing it to the compiler
47 * via the -D switch.
48 */
49
50 #define INCLUDE_FROM_MASSSTORE_COMMANDS_C
51 #include "MassStoreCommands.h"
52
53 /** Current Tag value used in issued CBWs to the device. This is automatically incremented
54 * each time a command is sent, and is not externally accessible.
55 */
56 static uint32_t MassStore_Tag = 1;
57
58
59 /** Routine to send the current CBW to the device, and increment the Tag value as needed.
60 *
61 * \param[in] SCSICommandBlock Pointer to a SCSI command block structure to send to the attached device
62 * \param[in,out] BufferPtr Pointer to a buffer for the data to send or receive to/from the device, or NULL if no data
63 *
64 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
65 */
66 static uint8_t MassStore_SendCommand(MS_CommandBlockWrapper_t* const SCSICommandBlock,
67 void* BufferPtr)
68 {
69 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
70
71 /* Each transmission should have a unique tag value, increment before use */
72 SCSICommandBlock->Tag = ++MassStore_Tag;
73
74 /* Wrap Tag value when invalid - MS class defines tag values of 0 and 0xFFFFFFFF to be invalid */
75 if (MassStore_Tag == 0xFFFFFFFF)
76 MassStore_Tag = 1;
77
78 /* Select the OUT data pipe for CBW transmission */
79 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
80 Pipe_Unfreeze();
81
82 /* Write the CBW command to the OUT pipe */
83 if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t))) != PIPE_RWSTREAM_NoError)
84 return ErrorCode;
85
86 /* Send the data in the OUT pipe to the attached device */
87 Pipe_ClearOUT();
88
89 /* Wait until command has been sent */
90 Pipe_WaitUntilReady();
91
92 /* Freeze pipe after use */
93 Pipe_Freeze();
94
95 /* Send data if any */
96 if ((BufferPtr != NULL) &&
97 ((ErrorCode = MassStore_SendReceiveData(SCSICommandBlock, BufferPtr)) != PIPE_READYWAIT_NoError))
98 {
99 Pipe_Freeze();
100 return ErrorCode;
101 }
102
103 return ErrorCode;
104 }
105
106 /** Waits until the attached device is ready to accept data following a CBW, checking
107 * to ensure that the device has not stalled the transaction.
108 *
109 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
110 */
111 static uint8_t MassStore_WaitForDataReceived(void)
112 {
113 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
114 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
115
116 /* Select the IN data pipe for data reception */
117 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
118 Pipe_Unfreeze();
119
120 /* Wait until data received in the IN pipe */
121 while (!(Pipe_IsINReceived()))
122 {
123 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
124
125 /* Check to see if a new frame has been issued (1ms elapsed) */
126 if (CurrentFrameNumber != PreviousFrameNumber)
127 {
128 /* Save the new frame number and decrement the timeout period */
129 PreviousFrameNumber = CurrentFrameNumber;
130 TimeoutMSRem--;
131
132 /* Check to see if the timeout period for the command has elapsed */
133 if (!(TimeoutMSRem))
134 return PIPE_RWSTREAM_Timeout;
135 }
136
137 Pipe_Freeze();
138 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
139 Pipe_Unfreeze();
140
141 /* Check if pipe stalled (command failed by device) */
142 if (Pipe_IsStalled())
143 {
144 /* Clear the stall condition on the OUT pipe */
145 USB_Host_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE);
146
147 return PIPE_RWSTREAM_PipeStalled;
148 }
149
150 Pipe_Freeze();
151 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
152 Pipe_Unfreeze();
153
154 /* Check if pipe stalled (command failed by device) */
155 if (Pipe_IsStalled())
156 {
157 /* Clear the stall condition on the IN pipe */
158 USB_Host_ClearPipeStall(MASS_STORE_DATA_IN_PIPE);
159
160 return PIPE_RWSTREAM_PipeStalled;
161 }
162
163 /* Check to see if the device was disconnected, if so exit function */
164 if (USB_HostState == HOST_STATE_Unattached)
165 return PIPE_RWSTREAM_DeviceDisconnected;
166 };
167
168 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
169 Pipe_Freeze();
170
171 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
172 Pipe_Freeze();
173
174 return PIPE_RWSTREAM_NoError;
175 }
176
177 /** Sends or receives the transaction's data stage to or from the attached device, reading or
178 * writing to the nominated buffer.
179 *
180 * \param[in] SCSICommandBlock Pointer to a SCSI command block structure being sent to the attached device
181 * \param[in,out] BufferPtr Pointer to the data buffer to read from or write to
182 *
183 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
184 */
185 static uint8_t MassStore_SendReceiveData(MS_CommandBlockWrapper_t* const SCSICommandBlock,
186 void* BufferPtr)
187 {
188 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
189 uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
190
191 /* Check the direction of the SCSI command data stage */
192 if (SCSICommandBlock->Flags & MS_COMMAND_DIR_DATA_IN)
193 {
194 /* Wait until the device has replied with some data */
195 if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
196 return ErrorCode;
197
198 /* Select the IN data pipe for data reception */
199 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
200 Pipe_Unfreeze();
201
202 /* Read in the block data from the pipe */
203 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError)
204 return ErrorCode;
205
206 /* Acknowledge the packet */
207 Pipe_ClearIN();
208 }
209 else
210 {
211 /* Select the OUT data pipe for data transmission */
212 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
213 Pipe_Unfreeze();
214
215 /* Write the block data to the pipe */
216 if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError)
217 return ErrorCode;
218
219 /* Acknowledge the packet */
220 Pipe_ClearOUT();
221
222 while (!(Pipe_IsOUTReady()))
223 {
224 if (USB_HostState == HOST_STATE_Unattached)
225 return PIPE_RWSTREAM_DeviceDisconnected;
226 }
227 }
228
229 /* Freeze used pipe after use */
230 Pipe_Freeze();
231
232 return PIPE_RWSTREAM_NoError;
233 }
234
235 /** Routine to receive the current CSW from the device.
236 *
237 * \param[out] SCSICommandStatus Pointer to a destination where the returned status data should be stored
238 *
239 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
240 */
241 static uint8_t MassStore_GetReturnedStatus(MS_CommandStatusWrapper_t* const SCSICommandStatus)
242 {
243 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
244
245 /* If an error in the command occurred, abort */
246 if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
247 return ErrorCode;
248
249 /* Select the IN data pipe for data reception */
250 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
251 Pipe_Unfreeze();
252
253 /* Load in the CSW from the attached device */
254 if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t))) != PIPE_RWSTREAM_NoError)
255 return ErrorCode;
256
257 /* Clear the data ready for next reception */
258 Pipe_ClearIN();
259
260 /* Freeze the IN pipe after use */
261 Pipe_Freeze();
262
263 /* Check to see if command failed */
264 if (SCSICommandStatus->Status != MS_SCSI_COMMAND_Pass)
265 ErrorCode = MASS_STORE_SCSI_COMMAND_FAILED;
266
267 return ErrorCode;
268 }
269
270 /** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface,
271 * readying the device for the next CBW.
272 *
273 * \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
274 */
275 uint8_t MassStore_MassStorageReset(void)
276 {
277 USB_ControlRequest = (USB_Request_Header_t)
278 {
279 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
280 .bRequest = MS_REQ_MassStorageReset,
281 .wValue = 0,
282 .wIndex = 0,
283 .wLength = 0,
284 };
285
286 /* Select the control pipe for the request transfer */
287 Pipe_SelectPipe(PIPE_CONTROLPIPE);
288
289 return USB_Host_SendControlRequest(NULL);
290 }
291
292 /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical
293 * Unit in the attached device.
294 *
295 * \note Some devices do not support this request, and will STALL it when issued. To get around this,
296 * on unsupported devices the max LUN index will be reported as zero and no error will be returned
297 * if the device STALLs the request.
298 *
299 * \param[out] MaxLUNIndex Pointer to the location that the maximum LUN index value should be stored
300 *
301 * \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
302 */
303 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
304 {
305 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
306
307 USB_ControlRequest = (USB_Request_Header_t)
308 {
309 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
310 .bRequest = MS_REQ_GetMaxLUN,
311 .wValue = 0,
312 .wIndex = 0,
313 .wLength = 1,
314 };
315
316 /* Select the control pipe for the request transfer */
317 Pipe_SelectPipe(PIPE_CONTROLPIPE);
318
319 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
320 {
321 /* Clear the pipe stall */
322 Pipe_ClearStall();
323
324 /* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */
325 *MaxLUNIndex = 0;
326
327 /* Clear the error, and pretend the request executed correctly if the device STALLed it */
328 ErrorCode = HOST_SENDCONTROL_Successful;
329 }
330
331 return ErrorCode;
332 }
333
334 /** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This
335 * gives information on the device's capabilities.
336 *
337 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
338 * \param[out] InquiryPtr Pointer to the inquiry data structure where the inquiry data from the device is to be stored
339 *
340 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
341 */
342 uint8_t MassStore_Inquiry(const uint8_t LUNIndex,
343 SCSI_Inquiry_Response_t* const InquiryPtr)
344 {
345 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
346
347 /* Create a CBW with a SCSI command to issue INQUIRY command */
348 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
349 {
350 .Signature = MS_CBW_SIGNATURE,
351 .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
352 .Flags = MS_COMMAND_DIR_DATA_IN,
353 .LUN = LUNIndex,
354 .SCSICommandLength = 6,
355 .SCSICommandData =
356 {
357 SCSI_CMD_INQUIRY,
358 0x00, // Reserved
359 0x00, // Reserved
360 0x00, // Reserved
361 sizeof(SCSI_Inquiry_Response_t), // Allocation Length
362 0x00 // Unused (control)
363 }
364 };
365
366 MS_CommandStatusWrapper_t SCSICommandStatus;
367
368 /* Send the command and any data to the attached device */
369 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, InquiryPtr)) != PIPE_RWSTREAM_NoError)
370 {
371 Pipe_Freeze();
372 return ErrorCode;
373 }
374
375 /* Retrieve status information from the attached device */
376 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
377 {
378 Pipe_Freeze();
379 return ErrorCode;
380 }
381
382 return ErrorCode;
383 }
384
385 /** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This
386 * gives error codes for the last issued SCSI command to the device.
387 *
388 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
389 * \param[out] SensePtr Pointer to the sense data structure where the sense data from the device is to be stored
390 *
391 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
392 */
393 uint8_t MassStore_RequestSense(const uint8_t LUNIndex,
394 SCSI_Request_Sense_Response_t* const SensePtr)
395 {
396 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
397
398 /* Create a CBW with a SCSI command to issue REQUEST SENSE command */
399 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
400 {
401 .Signature = MS_CBW_SIGNATURE,
402 .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
403 .Flags = MS_COMMAND_DIR_DATA_IN,
404 .LUN = LUNIndex,
405 .SCSICommandLength = 6,
406 .SCSICommandData =
407 {
408 SCSI_CMD_REQUEST_SENSE,
409 0x00, // Reserved
410 0x00, // Reserved
411 0x00, // Reserved
412 sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
413 0x00 // Unused (control)
414 }
415 };
416
417 MS_CommandStatusWrapper_t SCSICommandStatus;
418
419 /* Send the command and any data to the attached device */
420 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, SensePtr)) != PIPE_RWSTREAM_NoError)
421 {
422 Pipe_Freeze();
423 return ErrorCode;
424 }
425
426 /* Retrieve status information from the attached device */
427 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
428 {
429 Pipe_Freeze();
430 return ErrorCode;
431 }
432
433 return ErrorCode;
434 }
435
436 /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the
437 * storage medium into a buffer.
438 *
439 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
440 * \param[in] BlockAddress Start block address to read from
441 * \param[in] Blocks Number of blocks to read from the device
442 * \param[in] BlockSize Size in bytes of each block to read
443 * \param[out] BufferPtr Pointer to the buffer where the read data is to be written to
444 *
445 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
446 */
447 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex,
448 const uint32_t BlockAddress,
449 const uint8_t Blocks,
450 const uint16_t BlockSize,
451 void* BufferPtr)
452 {
453 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
454
455 /* Create a CBW with a SCSI command to read in the given blocks from the device */
456 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
457 {
458 .Signature = MS_CBW_SIGNATURE,
459 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
460 .Flags = MS_COMMAND_DIR_DATA_IN,
461 .LUN = LUNIndex,
462 .SCSICommandLength = 10,
463 .SCSICommandData =
464 {
465 SCSI_CMD_READ_10,
466 0x00, // Unused (control bits, all off)
467 (BlockAddress >> 24), // MSB of Block Address
468 (BlockAddress >> 16),
469 (BlockAddress >> 8),
470 (BlockAddress & 0xFF), // LSB of Block Address
471 0x00, // Reserved
472 0x00, // MSB of Total Blocks to Read
473 Blocks, // LSB of Total Blocks to Read
474 0x00 // Unused (control)
475 }
476 };
477
478 MS_CommandStatusWrapper_t SCSICommandStatus;
479
480 /* Send the command and any data to the attached device */
481 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, BufferPtr)) != PIPE_RWSTREAM_NoError)
482 {
483 Pipe_Freeze();
484 return ErrorCode;
485 }
486
487 /* Retrieve status information from the attached device */
488 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
489 {
490 Pipe_Freeze();
491 return ErrorCode;
492 }
493
494 return ErrorCode;
495 }
496
497 /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the
498 * storage medium from a buffer.
499 *
500 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
501 * \param[in] BlockAddress Start block address to write to
502 * \param[in] Blocks Number of blocks to write to in the device
503 * \param[in] BlockSize Size in bytes of each block to write
504 * \param[in] BufferPtr Pointer to the buffer where the write data is to be sourced from
505 *
506 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
507 */
508 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex,
509 const uint32_t BlockAddress,
510 const uint8_t Blocks,
511 const uint16_t BlockSize,
512 void* BufferPtr)
513 {
514 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
515
516 /* Create a CBW with a SCSI command to write the given blocks to the device */
517 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
518 {
519 .Signature = MS_CBW_SIGNATURE,
520 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
521 .Flags = MS_COMMAND_DIR_DATA_OUT,
522 .LUN = LUNIndex,
523 .SCSICommandLength = 10,
524 .SCSICommandData =
525 {
526 SCSI_CMD_WRITE_10,
527 0x00, // Unused (control bits, all off)
528 (BlockAddress >> 24), // MSB of Block Address
529 (BlockAddress >> 16),
530 (BlockAddress >> 8),
531 (BlockAddress & 0xFF), // LSB of Block Address
532 0x00, // Unused (reserved)
533 0x00, // MSB of Total Blocks to Write
534 Blocks, // LSB of Total Blocks to Write
535 0x00 // Unused (control)
536 }
537 };
538
539 MS_CommandStatusWrapper_t SCSICommandStatus;
540
541 /* Send the command and any data to the attached device */
542 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, BufferPtr)) != PIPE_RWSTREAM_NoError)
543 {
544 Pipe_Freeze();
545 return ErrorCode;
546 }
547
548 /* Retrieve status information from the attached device */
549 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
550 {
551 Pipe_Freeze();
552 return ErrorCode;
553 }
554
555 return ErrorCode;
556 }
557
558 /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept
559 * other commands.
560 *
561 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
562 *
563 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
564 */
565 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex)
566 {
567 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
568
569 /* Create a CBW with a SCSI command to issue TEST UNIT READY command */
570 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
571 {
572 .Signature = MS_CBW_SIGNATURE,
573 .DataTransferLength = 0,
574 .Flags = MS_COMMAND_DIR_DATA_IN,
575 .LUN = LUNIndex,
576 .SCSICommandLength = 6,
577 .SCSICommandData =
578 {
579 SCSI_CMD_TEST_UNIT_READY,
580 0x00, // Reserved
581 0x00, // Reserved
582 0x00, // Reserved
583 0x00, // Reserved
584 0x00 // Unused (control)
585 }
586 };
587
588 MS_CommandStatusWrapper_t SCSICommandStatus;
589
590 /* Send the command and any data to the attached device */
591 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
592 {
593 Pipe_Freeze();
594 return ErrorCode;
595 }
596
597 /* Retrieve status information from the attached device */
598 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
599 {
600 Pipe_Freeze();
601 return ErrorCode;
602 }
603
604 return ErrorCode;
605 }
606
607 /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the
608 * given Logical Unit within the device.
609 *
610 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
611 * \param[out] CapacityPtr Device capacity structure where the capacity data is to be stored
612 *
613 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
614 */
615 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex,
616 SCSI_Capacity_t* const CapacityPtr)
617 {
618 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
619
620 /* Create a CBW with a SCSI command to issue READ CAPACITY command */
621 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
622 {
623 .Signature = MS_CBW_SIGNATURE,
624 .DataTransferLength = sizeof(SCSI_Capacity_t),
625 .Flags = MS_COMMAND_DIR_DATA_IN,
626 .LUN = LUNIndex,
627 .SCSICommandLength = 10,
628 .SCSICommandData =
629 {
630 SCSI_CMD_READ_CAPACITY_10,
631 0x00, // Reserved
632 0x00, // MSB of Logical block address
633 0x00,
634 0x00,
635 0x00, // LSB of Logical block address
636 0x00, // Reserved
637 0x00, // Reserved
638 0x00, // Partial Medium Indicator
639 0x00 // Unused (control)
640 }
641 };
642
643 MS_CommandStatusWrapper_t SCSICommandStatus;
644
645 /* Send the command and any data to the attached device */
646 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, CapacityPtr)) != PIPE_RWSTREAM_NoError)
647 {
648 Pipe_Freeze();
649 return ErrorCode;
650 }
651
652 /* Endian-correct the read data */
653 CapacityPtr->Blocks = SwapEndian_32(CapacityPtr->Blocks);
654 CapacityPtr->BlockSize = SwapEndian_32(CapacityPtr->BlockSize);
655
656 /* Retrieve status information from the attached device */
657 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
658 {
659 Pipe_Freeze();
660 return ErrorCode;
661 }
662
663 return ErrorCode;
664 }
665
666 /** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from
667 * being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still
668 * be issued before the first read or write command is sent.
669 *
670 * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
671 * \param[in] PreventRemoval Whether or not the LUN media should be locked to prevent removal or not
672 *
673 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
674 */
675 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex,
676 const bool PreventRemoval)
677 {
678 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
679
680 /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */
681 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
682 {
683 .Signature = MS_CBW_SIGNATURE,
684 .DataTransferLength = 0,
685 .Flags = MS_COMMAND_DIR_DATA_OUT,
686 .LUN = LUNIndex,
687 .SCSICommandLength = 6,
688 .SCSICommandData =
689 {
690 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
691 0x00, // Reserved
692 0x00, // Reserved
693 PreventRemoval, // Prevent flag
694 0x00, // Reserved
695 0x00 // Unused (control)
696 }
697 };
698
699 MS_CommandStatusWrapper_t SCSICommandStatus;
700
701 /* Send the command and any data to the attached device */
702 if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
703 {
704 Pipe_Freeze();
705 return ErrorCode;
706 }
707
708 /* Retrieve status information from the attached device */
709 if ((ErrorCode = MassStore_GetReturnedStatus(&SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
710 {
711 Pipe_Freeze();
712 return ErrorCode;
713 }
714
715 return ErrorCode;
716 }
717