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