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