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