Debian Codegraph @

ports/stm32/usbhost/Class/MSC/Src/usbh_msc.c

raw
1 /**
2 ******************************************************************************
3 * @file usbh_msc.c
4 * @author MCD Application Team
5 * @version V3.0.0
6 * @date 18-February-2014
7 * @brief This file implements the MSC class driver functions
8 * ===================================================================
9 * MSC Class Description
10 * ===================================================================
11 * This module manages the MSC class V1.0 following the "Universal
12 * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0
13 * Sep. 31, 1999".
14 * This driver implements the following aspects of the specification:
15 * - Bulk-Only Transport protocol
16 * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3))
17 *
18 * @endverbatim
19 *
20 ******************************************************************************
21 * @attention
22 *
23 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
24 *
25 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
26 * You may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at:
28 *
29 * http://www.st.com/software_license_agreement_liberty_v2
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 *
37 ******************************************************************************
38 */
39
40 /* Includes ------------------------------------------------------------------*/
41
42 #include "usbh_msc.h"
43 #include "usbh_msc_bot.h"
44 #include "usbh_msc_scsi.h"
45
46
47 /** @addtogroup USBH_LIB
48 * @{
49 */
50
51 /** @addtogroup USBH_CLASS
52 * @{
53 */
54
55 /** @addtogroup USBH_MSC_CLASS
56 * @{
57 */
58
59 /** @defgroup USBH_MSC_CORE
60 * @brief This file includes the mass storage related functions
61 * @{
62 */
63
64
65 /** @defgroup USBH_MSC_CORE_Private_TypesDefinitions
66 * @{
67 */
68 /**
69 * @}
70 */
71
72 /** @defgroup USBH_MSC_CORE_Private_Defines
73 * @{
74 */
75 /**
76 * @}
77 */
78
79 /** @defgroup USBH_MSC_CORE_Private_Macros
80 * @{
81 */
82 /**
83 * @}
84 */
85
86
87 /** @defgroup USBH_MSC_CORE_Private_Variables
88 * @{
89 */
90 /**
91 * @}
92 */
93
94
95 /** @defgroup USBH_MSC_CORE_Private_FunctionPrototypes
96 * @{
97 */
98
99 static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost);
100
101 static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost);
102
103 static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost);
104
105 static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost);
106
107 static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost);
108
109 static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun);
110
111 USBH_ClassTypeDef USBH_msc =
112 {
113 "MSC",
114 USB_MSC_CLASS,
115 USBH_MSC_InterfaceInit,
116 USBH_MSC_InterfaceDeInit,
117 USBH_MSC_ClassRequest,
118 USBH_MSC_Process,
119 USBH_MSC_SOFProcess,
120 NULL,
121 };
122
123
124 /**
125 * @}
126 */
127
128
129 /** @defgroup USBH_MSC_CORE_Exported_Variables
130 * @{
131 */
132
133 /**
134 * @}
135 */
136
137
138 /** @defgroup USBH_MSC_CORE_Private_Functions
139 * @{
140 */
141
142
143 /**
144 * @brief USBH_MSC_InterfaceInit
145 * The function init the MSC class.
146 * @param phost: Host handle
147 * @retval USBH Status
148 */
149 static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost)
150 {
151 uint8_t interface = 0;
152 USBH_StatusTypeDef status = USBH_FAIL ;
153 MSC_HandleTypeDef *MSC_Handle;
154
155 interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, MSC_TRANSPARENT, MSC_BOT);
156
157 if(interface == 0xFF) /* Not Valid Interface */
158 {
159 USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name);
160 status = USBH_FAIL;
161 }
162 else
163 {
164 USBH_SelectInterface (phost, interface);
165
166 phost->pActiveClass->pData = (MSC_HandleTypeDef *)USBH_malloc (sizeof(MSC_HandleTypeDef));
167 MSC_Handle = phost->pActiveClass->pData;
168
169 if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress & 0x80)
170 {
171 MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress);
172 MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize;
173 }
174 else
175 {
176 MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress);
177 MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize;
178 }
179
180 if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress & 0x80)
181 {
182 MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress);
183 MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize;
184 }
185 else
186 {
187 MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress);
188 MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize;
189 }
190
191 MSC_Handle->current_lun = 0;
192 MSC_Handle->rw_lun = 0;
193 MSC_Handle->state = MSC_INIT;
194 MSC_Handle->error = MSC_OK;
195 MSC_Handle->req_state = MSC_REQ_IDLE;
196 MSC_Handle->OutPipe = USBH_AllocPipe(phost, MSC_Handle->OutEp);
197 MSC_Handle->InPipe = USBH_AllocPipe(phost, MSC_Handle->InEp);
198
199 USBH_MSC_BOT_Init(phost);
200
201 /* De-Initialize LUNs information */
202 USBH_memset(MSC_Handle->unit, 0, sizeof(MSC_Handle->unit));
203
204 /* Open the new channels */
205 USBH_OpenPipe (phost,
206 MSC_Handle->OutPipe,
207 MSC_Handle->OutEp,
208 phost->device.address,
209 phost->device.speed,
210 USB_EP_TYPE_BULK,
211 MSC_Handle->OutEpSize);
212
213 USBH_OpenPipe (phost,
214 MSC_Handle->InPipe,
215 MSC_Handle->InEp,
216 phost->device.address,
217 phost->device.speed,
218 USB_EP_TYPE_BULK,
219 MSC_Handle->InEpSize);
220
221
222 USBH_LL_SetToggle (phost, MSC_Handle->InPipe,0);
223 USBH_LL_SetToggle (phost, MSC_Handle->OutPipe,0);
224 status = USBH_OK;
225 }
226 return status;
227 }
228
229 /**
230 * @brief USBH_MSC_InterfaceDeInit
231 * The function DeInit the Pipes used for the MSC class.
232 * @param phost: Host handle
233 * @retval USBH Status
234 */
235 USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost)
236 {
237 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
238
239 if ( MSC_Handle->OutPipe)
240 {
241 USBH_ClosePipe(phost, MSC_Handle->OutPipe);
242 USBH_FreePipe (phost, MSC_Handle->OutPipe);
243 MSC_Handle->OutPipe = 0; /* Reset the Channel as Free */
244 }
245
246 if ( MSC_Handle->InPipe)
247 {
248 USBH_ClosePipe(phost, MSC_Handle->InPipe);
249 USBH_FreePipe (phost, MSC_Handle->InPipe);
250 MSC_Handle->InPipe = 0; /* Reset the Channel as Free */
251 }
252
253 if(phost->pActiveClass->pData)
254 {
255 USBH_free (phost->pActiveClass->pData);
256 phost->pActiveClass->pData = 0;
257 }
258
259 return USBH_OK;
260 }
261
262 /**
263 * @brief USBH_MSC_ClassRequest
264 * The function is responsible for handling Standard requests
265 * for MSC class.
266 * @param phost: Host handle
267 * @retval USBH Status
268 */
269 static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost)
270 {
271 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
272 USBH_StatusTypeDef status = USBH_BUSY;
273 uint8_t i;
274
275 /* Switch MSC REQ state machine */
276 switch (MSC_Handle->req_state)
277 {
278 case MSC_REQ_IDLE:
279 case MSC_REQ_GET_MAX_LUN:
280 /* Issue GetMaxLUN request */
281 if(USBH_MSC_BOT_REQ_GetMaxLUN(phost, (uint8_t *)&MSC_Handle->max_lun) == USBH_OK )
282 {
283 MSC_Handle->max_lun = (uint8_t )(MSC_Handle->max_lun) + 1;
284 USBH_UsrLog ("Number of supported LUN: %lu", (int32_t)(MSC_Handle->max_lun));
285
286 for(i = 0; i < MSC_Handle->max_lun; i++)
287 {
288 MSC_Handle->unit[i].prev_ready_state = USBH_FAIL;
289 MSC_Handle->unit[i].state_changed = 0;
290 }
291 status = USBH_OK;
292 }
293 break;
294
295 case MSC_REQ_ERROR :
296 /* a Clear Feature should be issued here */
297 if(USBH_ClrFeature(phost, 0x00) == USBH_OK)
298 {
299 MSC_Handle->req_state = MSC_Handle->prev_req_state;
300 }
301 break;
302
303 default:
304 break;
305 }
306
307 return status;
308 }
309
310 /**
311 * @brief USBH_MSC_Process
312 * The function is for managing state machine for MSC data transfers
313 * @param phost: Host handle
314 * @retval USBH Status
315 */
316 static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost)
317 {
318 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
319 USBH_StatusTypeDef error = USBH_BUSY ;
320 USBH_StatusTypeDef scsi_status = USBH_BUSY ;
321 USBH_StatusTypeDef ready_status = USBH_BUSY ;
322
323 switch (MSC_Handle->state)
324 {
325 case MSC_INIT:
326
327 if(MSC_Handle->current_lun < MSC_Handle->max_lun)
328 {
329
330 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY;
331 /* Switch MSC REQ state machine */
332 switch (MSC_Handle->unit[MSC_Handle->current_lun].state)
333 {
334 case MSC_INIT:
335 USBH_UsrLog ("LUN #%d: ", MSC_Handle->current_lun);
336 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_INQUIRY;
337 MSC_Handle->timer = phost->Timer + 10000;
338
339 case MSC_READ_INQUIRY:
340 scsi_status = USBH_MSC_SCSI_Inquiry(phost, MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].inquiry);
341
342 if( scsi_status == USBH_OK)
343 {
344 USBH_UsrLog ("Inquiry Vendor : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.vendor_id);
345 USBH_UsrLog ("Inquiry Product : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.product_id);
346 USBH_UsrLog ("Inquiry Version : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.revision_id);
347 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY;
348 }
349 if( scsi_status == USBH_FAIL)
350 {
351 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
352 }
353 else if(scsi_status == USBH_UNRECOVERED_ERROR)
354 {
355 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
356 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
357 }
358 break;
359
360 case MSC_TEST_UNIT_READY:
361 ready_status = USBH_MSC_SCSI_TestUnitReady(phost, MSC_Handle->current_lun);
362
363 if( ready_status == USBH_OK)
364 {
365 if( MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_OK)
366 {
367 MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1;
368 USBH_UsrLog ("Mass Storage Device ready");
369 }
370 else
371 {
372 MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0;
373 }
374 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_CAPACITY10;
375 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK;
376 MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_OK;
377 }
378 if( ready_status == USBH_FAIL)
379 {
380 /* Media not ready, so try to check again during 10s */
381 if( MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_FAIL)
382 {
383 MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1;
384 USBH_UsrLog ("Mass Storage Device NOT ready");
385 }
386 else
387 {
388 MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0;
389 }
390 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
391 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY;
392 MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_FAIL;
393 }
394 else if(ready_status == USBH_UNRECOVERED_ERROR)
395 {
396 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
397 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
398 }
399 break;
400
401 case MSC_READ_CAPACITY10:
402 scsi_status = USBH_MSC_SCSI_ReadCapacity(phost,MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].capacity) ;
403
404 if(scsi_status == USBH_OK)
405 {
406 if(MSC_Handle->unit[MSC_Handle->current_lun].state_changed == 1)
407 {
408 USBH_UsrLog ("Mass Storage Device capacity : %lu MB", \
409 (int32_t)((MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr * MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size)/1024/1024));
410 USBH_UsrLog ("Block number : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr));
411 USBH_UsrLog ("Block Size : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size));
412 }
413 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
414 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK;
415 MSC_Handle->current_lun++;
416 }
417 else if( scsi_status == USBH_FAIL)
418 {
419 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
420 }
421 else if(scsi_status == USBH_UNRECOVERED_ERROR)
422 {
423 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
424 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
425 }
426 break;
427
428 case MSC_REQUEST_SENSE:
429 scsi_status = USBH_MSC_SCSI_RequestSense(phost, MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].sense);
430
431 if( scsi_status == USBH_OK)
432 {
433 if((MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION) ||
434 (MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_NOT_READY) )
435 {
436
437 if(phost->Timer <= MSC_Handle->timer)
438 {
439 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY;
440 break;
441 }
442 }
443
444 USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.key);
445 USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.asc);
446 USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.ascq);
447 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
448 MSC_Handle->current_lun++;
449 }
450 if( scsi_status == USBH_FAIL)
451 {
452 USBH_UsrLog ("Mass Storage Device NOT ready");
453 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_UNRECOVERED_ERROR;
454 }
455 else if(scsi_status == USBH_UNRECOVERED_ERROR)
456 {
457 MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
458 MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
459 }
460 break;
461
462 case MSC_UNRECOVERED_ERROR:
463 MSC_Handle->current_lun++;
464 break;
465
466 default:
467 break;
468 }
469
470 #if (USBH_USE_OS == 1)
471 osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0);
472 #endif
473 }
474 else
475 {
476 MSC_Handle->current_lun = 0;
477 MSC_Handle->state = MSC_IDLE;
478 #if (USBH_USE_OS == 1)
479 osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0);
480 #endif
481 phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
482 }
483 break;
484
485 case MSC_IDLE:
486 error = USBH_OK;
487 break;
488
489 default:
490 break;
491 }
492 return error;
493 }
494
495
496 /**
497 * @brief USBH_MSC_SOFProcess
498 * The function is for SOF state
499 * @param phost: Host handle
500 * @retval USBH Status
501 */
502 static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost)
503 {
504
505 return USBH_OK;
506 }
507 /**
508 * @brief USBH_MSC_RdWrProcess
509 * The function is for managing state machine for MSC I/O Process
510 * @param phost: Host handle
511 * @param lun: logical Unit Number
512 * @retval USBH Status
513 */
514 static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun)
515 {
516 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
517 USBH_StatusTypeDef error = USBH_BUSY ;
518 USBH_StatusTypeDef scsi_status = USBH_BUSY ;
519
520 /* Switch MSC REQ state machine */
521 switch (MSC_Handle->unit[lun].state)
522 {
523
524 case MSC_READ:
525 scsi_status = USBH_MSC_SCSI_Read(phost,lun, 0, NULL, 0) ;
526
527 if(scsi_status == USBH_OK)
528 {
529 MSC_Handle->unit[lun].state = MSC_IDLE;
530 error = USBH_OK;
531 }
532 else if( scsi_status == USBH_FAIL)
533 {
534 MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE;
535 }
536 else if(scsi_status == USBH_UNRECOVERED_ERROR)
537 {
538 MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
539 error = USBH_FAIL;
540 }
541 #if (USBH_USE_OS == 1)
542 osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0);
543 #endif
544 break;
545
546 case MSC_WRITE:
547 scsi_status = USBH_MSC_SCSI_Write(phost,lun, 0, NULL, 0) ;
548
549 if(scsi_status == USBH_OK)
550 {
551 MSC_Handle->unit[lun].state = MSC_IDLE;
552 error = USBH_OK;
553 }
554 else if( scsi_status == USBH_FAIL)
555 {
556 MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE;
557 }
558 else if(scsi_status == USBH_UNRECOVERED_ERROR)
559 {
560 MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
561 error = USBH_FAIL;
562 }
563 #if (USBH_USE_OS == 1)
564 osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0);
565 #endif
566 break;
567
568 case MSC_REQUEST_SENSE:
569 scsi_status = USBH_MSC_SCSI_RequestSense(phost, lun, &MSC_Handle->unit[lun].sense);
570
571 if( scsi_status == USBH_OK)
572 {
573 USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[lun].sense.key);
574 USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[lun].sense.asc);
575 USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[lun].sense.ascq);
576 MSC_Handle->unit[lun].state = MSC_IDLE;
577 MSC_Handle->unit[lun].error = MSC_ERROR;
578
579 error = USBH_FAIL;
580 }
581 if( scsi_status == USBH_FAIL)
582 {
583 USBH_UsrLog ("Mass Storage Device NOT ready");
584 }
585 else if(scsi_status == USBH_UNRECOVERED_ERROR)
586 {
587 MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
588 error = USBH_FAIL;
589 }
590 #if (USBH_USE_OS == 1)
591 osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0);
592 #endif
593 break;
594
595 default:
596 break;
597
598 }
599 return error;
600 }
601
602 /**
603 * @brief USBH_MSC_IsReady
604 * The function check if the MSC function is ready
605 * @param phost: Host handle
606 * @retval USBH Status
607 */
608 uint8_t USBH_MSC_IsReady (USBH_HandleTypeDef *phost)
609 {
610 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
611
612 if(phost->gState == HOST_CLASS)
613 {
614 return (MSC_Handle->state == MSC_IDLE);
615 }
616 else
617 {
618 return 0;
619 }
620 }
621
622 /**
623 * @brief USBH_MSC_GetMaxLUN
624 * The function return the Max LUN supported
625 * @param phost: Host handle
626 * @retval logical Unit Number supported
627 */
628 int8_t USBH_MSC_GetMaxLUN (USBH_HandleTypeDef *phost)
629 {
630 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
631
632 if ((phost->gState != HOST_CLASS) && (MSC_Handle->state == MSC_IDLE))
633 {
634 return MSC_Handle->max_lun;
635 }
636 return 0xFF;
637 }
638
639 /**
640 * @brief USBH_MSC_UnitIsReady
641 * The function check whether a LUN is ready
642 * @param phost: Host handle
643 * @param lun: logical Unit Number
644 * @retval Lun status (0: not ready / 1: ready)
645 */
646 uint8_t USBH_MSC_UnitIsReady (USBH_HandleTypeDef *phost, uint8_t lun)
647 {
648 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
649
650 if(phost->gState == HOST_CLASS)
651 {
652 return (MSC_Handle->unit[lun].error == MSC_OK);
653 }
654 else
655 {
656 return 0;
657 }
658 }
659
660 /**
661 * @brief USBH_MSC_GetLUNInfo
662 * The function return a LUN information
663 * @param phost: Host handle
664 * @param lun: logical Unit Number
665 * @retval USBH Status
666 */
667 USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info)
668 {
669 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
670 if(phost->gState == HOST_CLASS)
671 {
672 USBH_memcpy(info,&MSC_Handle->unit[lun], sizeof(MSC_LUNTypeDef));
673 return USBH_OK;
674 }
675 else
676 {
677 return USBH_FAIL;
678 }
679 }
680
681 /**
682 * @brief USBH_MSC_Read
683 * The function performs a Read operation
684 * @param phost: Host handle
685 * @param lun: logical Unit Number
686 * @param address: sector address
687 * @param pbuf: pointer to data
688 * @param length: number of sector to read
689 * @retval USBH Status
690 */
691 USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost,
692 uint8_t lun,
693 uint32_t address,
694 uint8_t *pbuf,
695 uint32_t length)
696 {
697 uint32_t timeout;
698 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
699
700 if ((phost->device.is_connected == 0) ||
701 (phost->gState != HOST_CLASS) ||
702 (MSC_Handle->unit[lun].state != MSC_IDLE))
703 {
704 return USBH_FAIL;
705 }
706 MSC_Handle->state = MSC_READ;
707 MSC_Handle->unit[lun].state = MSC_READ;
708 MSC_Handle->rw_lun = lun;
709 USBH_MSC_SCSI_Read(phost,
710 lun,
711 address,
712 pbuf,
713 length);
714
715 timeout = phost->Timer + (10000 * length);
716 while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY)
717 {
718 if((phost->Timer > timeout) || (phost->device.is_connected == 0))
719 {
720 MSC_Handle->state = MSC_IDLE;
721 return USBH_FAIL;
722 }
723 }
724 MSC_Handle->state = MSC_IDLE;
725 return USBH_OK;
726 }
727
728 /**
729 * @brief USBH_MSC_Write
730 * The function performs a Write operation
731 * @param phost: Host handle
732 * @param lun: logical Unit Number
733 * @param address: sector address
734 * @param pbuf: pointer to data
735 * @param length: number of sector to write
736 * @retval USBH Status
737 */
738 USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost,
739 uint8_t lun,
740 uint32_t address,
741 uint8_t *pbuf,
742 uint32_t length)
743 {
744 uint32_t timeout;
745 MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData;
746
747 if ((phost->device.is_connected == 0) ||
748 (phost->gState != HOST_CLASS) ||
749 (MSC_Handle->unit[lun].state != MSC_IDLE))
750 {
751 return USBH_FAIL;
752 }
753 MSC_Handle->state = MSC_WRITE;
754 MSC_Handle->unit[lun].state = MSC_WRITE;
755 MSC_Handle->rw_lun = lun;
756 USBH_MSC_SCSI_Write(phost,
757 lun,
758 address,
759 pbuf,
760 length);
761
762 timeout = phost->Timer + (10000 * length);
763 while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY)
764 {
765 if((phost->Timer > timeout) || (phost->device.is_connected == 0))
766 {
767 MSC_Handle->state = MSC_IDLE;
768 return USBH_FAIL;
769 }
770 }
771 MSC_Handle->state = MSC_IDLE;
772 return USBH_OK;
773 }
774
775 /**
776 * @}
777 */
778
779 /**
780 * @}
781 */
782
783 /**
784 * @}
785 */
786
787 /**
788 * @}
789 */
790
791 /**
792 * @}
793 */
794
795 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/