1 /*
2 ********************************************************************************************************************
3 *                                              usb host driver
4 *
5 *                              (c) Copyright 2007-2010, javen.China
6 *                                       All Rights Reserved
7 *
8 * File Name     : UsbBlkDev.c
9 *
10 * Author        : javen
11 *
12 * Version       : 2.0
13 *
14 * Date          : 2010.03.02
15 *
16 * Description   :
17 *
18 * History       :
19 *
20 ********************************************************************************************************************
21 */
22 #include  "usbh_buff_manager.h"
23 #include  "usb_msc_i.h"
24 #include  "Scsi2.h"
25 #include  "BlkDev.h"
26 #include  "CD.h"
27 #include  "error.h"
28 
29 /*
30 *******************************************************************************
31 *                     DiskOpen
32 *
33 * Description:
34 *
35 *
36 * Parameters:
37 *
38 *
39 * Return value:
40 *    0  :成功
41 *   !0  :失败
42 *
43 * note:
44 *    无
45 *
46 *******************************************************************************
47 */
DiskOpen(void * open_arg,uint32_t mode)48 static void *DiskOpen(void *open_arg, uint32_t mode)
49 {
50     __UsbBlkDev_t *BlkDev = NULL;
51 
52     if (open_arg == NULL)
53     {
54         hal_log_err("ERR: DiskOpen: input error, open_arg = %x", open_arg);
55         return NULL;
56     }
57 
58     BlkDev = (__UsbBlkDev_t *)open_arg;
59 
60     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
61     {
62         hal_log_err("ERR: DiskOpen: BlkDev Magic(%x) is invalid", BlkDev->Magic);
63         return NULL;
64     }
65 
66     BlkDev->used++;
67     return (void *)open_arg;
68 }
69 
70 /*
71 *******************************************************************************
72 *                     DiskClose
73 *
74 * Description:
75 *
76 *
77 * Parameters:
78 *
79 *
80 * Return value:
81 *    0  :成功
82 *   !0  :失败
83 *
84 * note:
85 *    无
86 *
87 *******************************************************************************
88 */
DiskClose(void * hDev)89 static int32_t DiskClose(void * hDev)
90 {
91     __UsbBlkDev_t *BlkDev = NULL;
92 
93     if (hDev == NULL)
94     {
95         hal_log_err("ERR: DiskClose: input error, hDev = %x", hDev);
96         return EPDK_FAIL;
97     }
98 
99     BlkDev = (__UsbBlkDev_t *)hDev;
100 
101     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
102     {
103         hal_log_err("ERR: DiskClose: BlkDev Magic(%x) is invalid", BlkDev->Magic);
104         return EPDK_FAIL;
105     }
106 
107     BlkDev->used--;
108     return EPDK_OK;
109 }
110 
111 /*
112 *******************************************************************************
113 *                     __DiskRead
114 *
115 * Description:
116 *
117 *
118 * Parameters:
119 *
120 *
121 * Return value:
122 *    0  :成功
123 *   !0  :失败
124 *
125 * note:
126 *    无
127 *
128 *******************************************************************************
129 */
__DiskRead(void * pBuffer,unsigned int blk,unsigned int n,void * hDev)130 static unsigned int __DiskRead(void *pBuffer, unsigned int blk, unsigned int n, void * hDev)
131 {
132     __UsbBlkDev_t *BlkDev = NULL;
133     __mscLun_t *mscLun = NULL;
134     unsigned int cmd_version = 10;   /* 默认为10 */
135     int ret = 0;
136 
137     if (hDev == NULL)
138     {
139         hal_log_err("ERR: __DiskRead: input error, hDev = %x", hDev);
140         return EPDK_FAIL;
141     }
142 
143     if (n == 0)
144     {
145         hal_log_err("ERR: __DiskRead: the read length can not be zero");
146         return EPDK_FAIL;
147     }
148 
149     //DMSG_MSC_TEST("__DiskRead: Blk = %d, N = %d, LastBlk = %d", blk, n, (blk + n));
150     BlkDev = (__UsbBlkDev_t *)hDev;
151 
152     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
153     {
154         hal_log_err("ERR: __DiskRead: BlkDev Magic(%x) is invalid", BlkDev->Magic);
155         return EPDK_FAIL;
156     }
157 
158     /* 如果没有注册disk设备, 就不能够读数据 */
159     if (!BlkDev->is_RegDisk)
160     {
161         hal_log_err("ERR: __DiskRead: Not reged Disk, can not read");
162         return EPDK_FAIL;
163     }
164 
165     mscLun = BlkDev->Lun;
166 
167     if (mscLun == NULL)
168     {
169         hal_log_err("ERR: __DiskRead: mscLun == NULL");
170         return EPDK_FAIL;
171     }
172 
173     /* 如果介质不存在, 就不能够从设备读数据 */
174     if (!mscLun->MediaPresent)
175     {
176         hal_log_err("ERR: __DiskRead: media is not present, __DiskRead failed");
177         return EPDK_FAIL;
178     }
179 
180     if ((blk + n) > mscLun->disk_info.capacity)
181     {
182         hal_log_err("ERR: __DiskRead: block(%x, %x) is adjacence max capacity(%x)", blk, n, mscLun->disk_info.capacity);
183         return EPDK_FAIL;
184     }
185 
186     //set_usbh_disk_status(USB_STORAGE_DEVICE_STATUS_READ);
187     /*
188         if(mscLun->mscDev->SubClass == USBMSC_SUBCLASS_SCSI){
189             if(mscLun->disk_info.capacity > 0xffffffff){
190                 cmd_version = 16;
191             }else if(mscLun->disk_info.capacity > 0x1fffff){
192                 cmd_version = 10;
193             }else{
194                 cmd_version = 6;
195             }
196         }else{
197             cmd_version = 10;
198         }
199     */
200     /* 目前所见的USB设备里面都使用的是10字节命名,而且协议规定的是10/12字节命令 */
201     cmd_version = 10;
202     hal_sem_wait(mscLun->Lock);
203 
204     if (cmd_version == 16)
205     {
206         ret = ScsiRead(mscLun, 16, blk, n, pBuffer,
207                        (n * (mscLun->disk_info.sector_size)));
208     }
209     else if (cmd_version == 10)
210     {
211         ret = ScsiRead(mscLun, 10, blk, n, pBuffer,
212                        (n * (mscLun->disk_info.sector_size)));
213     }
214     else
215     {
216         ret = ScsiRead(mscLun, 6, blk, n, pBuffer,
217                        (n * (mscLun->disk_info.sector_size)));
218     }
219 
220     hal_sem_post(mscLun->Lock);
221     //set_usbh_disk_status(USB_STORAGE_DEVICE_STATUS_IDLE);
222 
223     if (ret != 0)
224     {
225         hal_log_err("ERR: ScsiRead%d failed. DevNo = %d, blk = %d, n = %d", cmd_version, BlkDev->DevNo, blk, n);
226         return EPDK_FAIL;
227     }
228 
229     return n;
230 }
231 
232 /*
233 *******************************************************************************
234 *                     __DiskWrite
235 *
236 * Description:
237 *
238 *
239 * Parameters:
240 *
241 *
242 * Return value:
243 *    0  :成功
244 *   !0  :失败
245 *
246 * note:
247 *    无
248 *
249 *******************************************************************************
250 */
__DiskWrite(const void * pBuffer,unsigned int blk,unsigned int n,void * hDev)251 static unsigned int __DiskWrite(const void *pBuffer, unsigned int blk, unsigned int n, void * hDev)
252 {
253     __UsbBlkDev_t *BlkDev = NULL;
254     __mscLun_t *mscLun = NULL;
255     unsigned int cmd_version = 10;   /* 默认为10 */
256     int ret = 0;
257 
258     if (hDev == NULL)
259     {
260         hal_log_err("ERR: __DiskWrite: input error, hDev = %x", hDev);
261         return 0;
262     }
263 
264     if (n == 0)
265     {
266         hal_log_err("ERR: __DiskWrite: the write length can not be zero");
267         return 0;
268     }
269 
270     //DMSG_MSC_TEST("__DiskWrite: Blk = %d, N = %d, LastBlk = %d", blk, n, (blk + n));
271     BlkDev = (__UsbBlkDev_t *)hDev;
272 
273     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
274     {
275         hal_log_err("ERR: __DiskWrite: BlkDev Magic(%x) is invalid", BlkDev->Magic);
276         return 0;
277     }
278 
279     /* 如果没有注册disk设备, 就不能够读数据 */
280     if (!BlkDev->is_RegDisk)
281     {
282         hal_log_err("ERR: __DiskWrite: Not reged Disk, can not read");
283         return 0;
284     }
285 
286     mscLun = BlkDev->Lun;
287 
288     if (mscLun == NULL)
289     {
290         hal_log_err("ERR: __DiskWrite: mscLun == NULL");
291         return 0;
292     }
293 
294     /* 如果介质不存在, 就不能够往设备写数据 */
295     if (!mscLun->MediaPresent)
296     {
297         hal_log_err("ERR: __DiskWrite: media is not present, __DiskWrite failed");
298         return 0;
299     }
300 
301     if (mscLun->WriteProtect)
302     {
303         hal_log_err("ERR: __DiskWrite: device is protect, can't write");
304         return 0;
305     }
306 
307     if ((blk + n) > mscLun->disk_info.capacity)
308     {
309         hal_log_err("ERR: __DiskWrite: block(%x, %x) is adjacence max capacity(%x)", blk, n, mscLun->disk_info.capacity);
310         return 0;
311     }
312 
313     //set_usbh_disk_status(USB_STORAGE_DEVICE_STATUS_WRITE);
314     /*
315         if(mscLun->mscDev->SubClass == USBMSC_SUBCLASS_SCSI){
316             if(mscLun->disk_info.capacity > 0xffffffff){
317                 cmd_version = 16;
318             }else if(mscLun->disk_info.capacity > 0x1fffff){
319                 cmd_version = 10;
320             }else{
321                 cmd_version = 6;
322             }
323         }else{
324             cmd_version = 10;
325         }
326     */
327     /* 目前所见的USB设备里面都使用的是10字节命名,而且协议规定的是10/12字节命令 */
328     cmd_version = 10;
329     hal_sem_wait(mscLun->Lock);
330 
331     if (cmd_version == 16)
332     {
333         ret = ScsiWrite(mscLun, 16, blk, n, (void *)pBuffer,
334                         (n * (mscLun->disk_info.sector_size)));
335     }
336     else if (cmd_version == 10)
337     {
338         ret = ScsiWrite(mscLun, 10, blk, n, (void *)pBuffer,
339                         (n * (mscLun->disk_info.sector_size)));
340     }
341     else
342     {
343         ret = ScsiWrite(mscLun, 6, blk, n, (void *)pBuffer,
344                         (n * (mscLun->disk_info.sector_size)));
345     }
346 
347     hal_sem_post(mscLun->Lock);
348     //set_usbh_disk_status(USB_STORAGE_DEVICE_STATUS_IDLE);
349 
350     if (ret != 0)
351     {
352         hal_log_err("ERR: ScsiWrite%d failed. DevNo = %d, blk = %d, n = %d", cmd_version, BlkDev->DevNo, blk, n);
353         return 0;
354     }
355 
356     return n;
357 }
358 
359 #if 1
360 /*
361 *******************************************************************************
362 *                     DiskRead
363 *
364 * Description:
365 *    设备读
366 *
367 * Parameters:
368 *    pBuffer    :  output. 装载读回来的数据
369 *    blk        :  input.  起始扇区
370 *    n          :  input.  扇区个数
371 *    hDev       :  input.  设备
372 *
373 * Return value:
374 *    0  :成功
375 *   !0  :失败
376 *
377 * note:
378 *    无
379 *
380 *******************************************************************************
381 */
DiskRead(void * pBuffer,uint32_t blk,uint32_t n,void * hDev)382 static uint32_t DiskRead(void *pBuffer, uint32_t blk, uint32_t n, void * hDev)
383 {
384     __UsbBlkDev_t *BlkDev = NULL;
385     __mscLun_t *mscLun = NULL;
386     unsigned int cnt = 0;
387 
388     if (hDev == NULL)
389     {
390         hal_log_err("ERR: DiskRead: input error, hDev = %x", hDev);
391         return 0;
392     }
393 
394     //DMSG_MSC_TEST("DiskRead: Blk = %d, N = %d, LastBlk = %d", blk, n, (blk + n));
395     BlkDev = (__UsbBlkDev_t *)hDev;
396 
397     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
398     {
399         hal_log_err("ERR: DiskRead: BlkDev Magic(%x) is invalid", BlkDev->Magic);
400         return 0;
401     }
402 
403     mscLun = BlkDev->Lun;
404 
405     if (mscLun == NULL)
406     {
407         hal_log_err("ERR: DiskRead: mscLun == NULL");
408         return 0;
409     }
410 
411     if ((blk + n) > mscLun->disk_info.capacity)
412     {
413         hal_log_err("ERR: DiskRead: block(%x, %x) is adjacence max capacity(%x)", blk, n, mscLun->disk_info.capacity);
414         return 0;
415     }
416 
417     /* 1、lba0 不预读
418        2、本次读写长度超过了最大容量,不预读
419     */
420     if ((blk != 0)
421         && ((blk + (USBH_TEMP_BUFFER_MAX_LEN / mscLun->disk_info.sector_size)) < mscLun->disk_info.capacity))
422     {
423         cnt = usbh_msc_special_read((void *)pBuffer,
424                                     blk,
425                                     n,
426                                     hDev,
427                                     BlkDev->DevNo,
428                                     mscLun->disk_info.sector_size,
429                                     (void *)__DiskRead);
430     }
431     else
432     {
433         hal_log_info("DiskRead: block(%x, %x) is adjacence max capacity(%x), can't use special write",
434                   blk, n, mscLun->disk_info.capacity);
435         cnt = __DiskRead(pBuffer, blk, n, hDev);
436     }
437 
438     if (cnt != n)
439     {
440         hal_log_err("ERR: DiskRead failed, want(%d) != real(%d)", n, cnt);
441         return 0;
442     }
443 
444     //DMSG_MSC_TEST("read end");
445     return n;
446 }
447 
448 /*
449 *******************************************************************************
450 *                     DiskWrite
451 *
452 * Description:
453 *    设备写
454 *
455 * Parameters:
456 *    pBuffer    :  input. 要写的数据
457 *    blk        :  input. 起始扇区
458 *    n          :  input. 扇区个数
459 *    hDev       :  input. 设备
460 *
461 * Return value:
462 *    0  :成功
463 *   !0  :失败
464 *
465 * note:
466 *    无
467 *
468 *******************************************************************************
469 */
DiskWrite(const void * pBuffer,uint32_t blk,uint32_t n,void * hDev)470 static uint32_t DiskWrite(const void *pBuffer, uint32_t blk, uint32_t n, void * hDev)
471 {
472     __UsbBlkDev_t *BlkDev = NULL;
473     __mscLun_t *mscLun = NULL;
474     unsigned int cnt = 0;
475 
476     if (hDev == NULL)
477     {
478         hal_log_err("ERR: DiskWrite: input error, hDev = %x", hDev);
479         return 0;
480     }
481 
482     //DMSG_MSC_TEST("DiskWrite: Blk = %d, N = %d, LastBlk = %d", blk, n, (blk + n));
483     BlkDev = (__UsbBlkDev_t *)hDev;
484 
485     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
486     {
487         hal_log_err("ERR: DiskWrite: BlkDev Magic(%x) is invalid", BlkDev->Magic);
488         return 0;
489     }
490 
491     mscLun = BlkDev->Lun;
492 
493     if (mscLun == NULL)
494     {
495         hal_log_err("ERR: __DiskWrite: mscLun == NULL");
496         return 0;
497     }
498 
499     if ((blk + n) > mscLun->disk_info.capacity)
500     {
501         hal_log_err("ERR: DiskWrite: block(%x, %x) is adjacence max capacity(%x)", blk, n, mscLun->disk_info.capacity);
502         return 0;
503     }
504 
505     /* 1、lba0 不预读
506        2、本次读写长度超过了最大容量,不预读
507     */
508     if ((blk != 0)
509         && ((blk + (USBH_TEMP_BUFFER_MAX_LEN / mscLun->disk_info.sector_size)) < mscLun->disk_info.capacity))
510     {
511         cnt = usbh_msc_special_write((void *)pBuffer,
512                                      blk,
513                                      n,
514                                      hDev,
515                                      BlkDev->DevNo,
516                                      mscLun->disk_info.sector_size,
517                                      (void *)__DiskWrite);
518     }
519     else
520     {
521         hal_log_info("DiskWrite: block(%x, %x) is adjacence max capacity(%x), can't use special write",
522                   blk, n, mscLun->disk_info.capacity);
523         set_usbh_temp_buff_invalid_by_dev(BlkDev->DevNo);
524         cnt = __DiskWrite(pBuffer, blk, n, hDev);
525     }
526 
527     if (cnt != n)
528     {
529         hal_log_err("ERR: DiskWrite failed, want(%d) != real(%d)", n, cnt);
530         return 0;
531     }
532 
533     //DMSG_MSC_TEST("write end");
534     return n;
535 }
536 
537 #else
538 
DiskRead(void * pBuffer,unsigned int blk,unsigned int n,void * hDev)539 static unsigned int DiskRead(void *pBuffer, unsigned int blk, unsigned int n, void * hDev)
540 {
541     return __DiskRead(pBuffer, blk, n, hDev);
542 }
543 
DiskWrite(const void * pBuffer,unsigned int blk,unsigned int n,void * hDev)544 static unsigned int DiskWrite(const void *pBuffer, unsigned int blk, unsigned int n, void * hDev)
545 {
546     return __DiskWrite(pBuffer, blk, n, hDev);
547 }
548 
549 #endif
550 
551 /*
552 *******************************************************************************
553 *                     DiskIoctl
554 *
555 * Description:
556 *
557 *
558 * Parameters:
559 *
560 *
561 * Return value:
562 *    0  :成功
563 *   !0  :失败
564 *
565 * note:
566 *    无
567 *
568 *******************************************************************************
569 */
DiskIoctl(void * hDev,uint32_t Cmd,long Aux,void * pBuffer)570 static int32_t DiskIoctl(void * hDev, uint32_t Cmd, long Aux, void *pBuffer)
571 {
572     __UsbBlkDev_t *BlkDev = NULL;
573     __mscLun_t *mscLun = NULL;
574     unsigned int ret = 0;
575 
576     if (hDev == NULL)
577     {
578         hal_log_err("ERR: DiskIoctl: input error, hDev = %x", hDev);
579         return EPDK_FAIL;
580     }
581 
582     BlkDev = (__UsbBlkDev_t *)hDev;
583 
584     if (BlkDev->Magic != USB_BLK_DEV_MAGIC)
585     {
586         hal_log_err("ERR: DiskIoctl: BlkDev Magic(%x) is invalid", BlkDev->Magic);
587         return EPDK_FAIL;
588     }
589 
590     mscLun = BlkDev->Lun;
591 
592     if (mscLun == NULL)
593     {
594         hal_log_err("ERR: DiskIoctl: mscLun == NULL");
595         return EPDK_FAIL;
596     }
597 
598     /* 由于块设备不支持光驱设备, 因此这里就只能做个临时版本 */
599     if (mscLun->DeviceType == SCSI_DEVICE_CDROM)
600     {
601         ret = CDIOCtrl(BlkDev, Cmd, Aux, pBuffer);
602 
603         if (ret == EPDK_OK)
604         {
605             hal_log_err("[CD_ROM]: Cmd(%x) is USB CD_ROM command");
606             return EPDK_OK;
607         }
608     }
609 
610     switch (Cmd)
611     {
612         case DEV_CMD_GET_INFO:
613         {
614             if (!pBuffer)
615             {
616                 hal_log_err("ERR : usb storage disk ,pBuffer == NULL");
617                 return EPDK_FAIL;
618             }
619 
620             ((__dev_blkinfo_t *)pBuffer)->hiddennum = 0;
621             ((__dev_blkinfo_t *)pBuffer)->headnum   = 0;
622             ((__dev_blkinfo_t *)pBuffer)->secnum    = 0;
623             ((__dev_blkinfo_t *)pBuffer)->partsize  = mscLun->disk_info.capacity;
624             ((__dev_blkinfo_t *)pBuffer)->secsize   = mscLun->disk_info.sector_size;
625             hal_log_info("[usb storage]: DEV_CMD_GET_INFO, capacity = %dM, sector = %d",
626                       ((mscLun->disk_info.capacity) >> 11), mscLun->disk_info.sector_size);
627         }
628         break;
629 
630         case DEV_CMD_GET_STATUS:
631             return EPDK_OK;
632 
633         case DEV_IOC_USR_FLUSH_CACHE:
634             return EPDK_OK;
635 
636         case DEV_CDROM_LAST_WRITTEN:
637             return EPDK_FAIL;
638 
639         case DEV_CDROM_MULTISESSION:
640             return EPDK_FAIL;
641 
642         default:
643             hal_log_err("WARN : DiskIoctl ,cmd = %x ,not support now", Cmd);
644             return EPDK_FAIL;
645     }
646 
647     return EPDK_OK;
648 }
649 
650 /*
651 *******************************************************************************
652 *                     UsbBlkDevAllocInit
653 *
654 * Description:
655 *
656 *
657 * Parameters:
658 *
659 *
660 * Return value:
661 *    0  :成功
662 *   !0  :失败
663 *
664 * note:
665 *    无
666 *
667 *******************************************************************************
668 */
669 #define  USB_STORAGE_DEV_NAME       "SCSI_DISK"
670 #define  USB_CDROM_DEV_NAME         "USB_CDROM"
671 
UsbBlkDevAllocInit(__mscLun_t * mscLun)672 __UsbBlkDev_t *UsbBlkDevAllocInit(__mscLun_t *mscLun)
673 {
674     __UsbBlkDev_t *BlkDev = NULL;
675     unsigned char temp_buff[32];
676     unsigned int temp = 0;
677 
678     if (mscLun == NULL)
679     {
680         hal_log_err("ERR: UsbBlkDevAllocInit: input error");
681         return NULL;
682     }
683 
684     //--<1>--create a block device
685     BlkDev = (__UsbBlkDev_t *)hal_malloc(sizeof(__UsbBlkDev_t));
686 
687     if (BlkDev == NULL)
688     {
689         hal_log_err("ERR: UsbBlkDevAllocInit: USB_OS_MALLOC failed");
690         return NULL;
691     }
692 
693     memset(BlkDev, 0, sizeof(__UsbBlkDev_t));
694     BlkDev->Lun = mscLun;
695     //--<2>--create lock
696     //--<3>--create DevNo
697     BlkDev->DevNo = (mscLun->LunNo * 1) + (mscLun->mscDev->mscDevNo * 10);
698 
699     //--<4>--create sub device name
700     if (mscLun->DeviceType == SCSI_DEVICE_CDROM)
701     {
702         memcpy((void *)BlkDev->DevName, USB_CDROM_DEV_NAME, strlen(USB_CDROM_DEV_NAME));
703         strcat((char *)BlkDev->DevName, "_");
704     }
705     else
706     {
707         memcpy((void *)BlkDev->DevName, USB_STORAGE_DEV_NAME, strlen(USB_STORAGE_DEV_NAME));
708         strcat((char *)BlkDev->DevName, "_");
709     }
710 
711     /* usb controler number */
712     temp = 0;
713     memset(temp_buff, 0, 32);
714     Usb_uint2str_dec(temp, (char *)temp_buff);
715     strcat((char *)BlkDev->DevName, (const char *)temp_buff);
716     /* mscDevNo */
717     memset(temp_buff, 0, 32);
718     Usb_uint2str_dec(mscLun->mscDev->mscDevNo, (char *)temp_buff);
719     strcat((char *)BlkDev->DevName, (const char *)temp_buff);
720     /* LunNo */
721     memset(temp_buff, 0, 32);
722     Usb_uint2str_dec(mscLun->LunNo, (char *)temp_buff);
723     strcat((char *)BlkDev->DevName, (const char *)temp_buff);
724     /* init device operation function */
725     BlkDev->DiskOp.Open  = DiskOpen;
726     BlkDev->DiskOp.Close = DiskClose;
727     BlkDev->DiskOp.Read  = DiskRead;
728     BlkDev->DiskOp.Write = DiskWrite;
729     BlkDev->DiskOp.Ioctl = DiskIoctl;
730     return BlkDev;
731 }
732 
733 /*
734 *******************************************************************************
735 *                     UsbBlkDevFree
736 *
737 * Description:
738 *
739 *
740 * Parameters:
741 *
742 *
743 * Return value:
744 *    0  :成功
745 *   !0  :失败
746 *
747 * note:
748 *    无
749 *
750 *******************************************************************************
751 */
UsbBlkDevFree(__UsbBlkDev_t * BlkDev)752 int UsbBlkDevFree(__UsbBlkDev_t *BlkDev)
753 {
754     if (BlkDev == NULL)
755     {
756         hal_log_err("ERR: UsbBlkDevFree: input error");
757         return -1;
758     }
759 
760     BlkDev->DiskOp.Open  = NULL;
761     BlkDev->DiskOp.Close = NULL;
762     BlkDev->DiskOp.Read  = NULL;
763     BlkDev->DiskOp.Write = NULL;
764     BlkDev->DiskOp.Ioctl = NULL;
765     hal_free(BlkDev);
766     return 0;
767 }
768 
769 /*
770 *******************************************************************************
771 *                     PrintDevStatus
772 *
773 * Description:
774 *
775 *
776 * Parameters:
777 *
778 *
779 * Return value:
780 *
781 *
782 * note:
783 *
784 *
785 *******************************************************************************
786 */
PrintDevStatus(unsigned char * FunName,int status)787 static void PrintDevStatus(unsigned char *FunName, int status)
788 {
789     hal_log_info("FunName: %s,", FunName);
790 
791     switch (status)
792     {
793         case  USB_STATUS_SUCCESS:
794             hal_log_info("USB_STATUS_SUCCESS");
795             break;
796 
797         case  USB_STATUS_DEVICE_DISCONNECTED:
798             hal_log_info("USB_STATUS_DEVICE_DISCONNECTED");
799             break;
800 
801         case USB_STATUS_IO_TIMEOUT:
802             hal_log_info("USB_STATUS_IO_TIMEOUT");
803             break;
804 
805         case USB_STATUS_IO_DEVICE_ERROR:
806             hal_log_info("USB_STATUS_IO_DEVICE_ERROR");
807             break;
808 
809         case USB_STATUS_DEVICE_BUSY:
810             hal_log_info("USB_STATUS_DEVICE_BUSY");
811             break;
812 
813         case USB_STATUS_BUFFER_TOO_SMALL:
814             hal_log_info("USB_STATUS_BUFFER_TOO_SMALL");
815             break;
816 
817         case USB_STATUS_INVALID_COMMAND:
818             hal_log_info("USB_STATUS_INVALID_COMMAND");
819             break;
820 
821         case USB_STATUS_INVALID_FIELD_IN_COMMAND:
822             hal_log_info("USB_STATUS_INVALID_FIELD_IN_COMMAND");
823             break;
824 
825         case USB_STATUS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE:
826             hal_log_info("USB_STATUS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE");
827             break;
828 
829         case USB_STATUS_MEDIA_NOT_PRESENT:
830             hal_log_info("USB_STATUS_MEDIA_NOT_PRESENT");
831             break;
832 
833         case USB_STATUS_NOT_READY_TO_READY_TRANSITION:
834             hal_log_info("USB_STATUS_NOT_READY_TO_READY_TRANSITION");
835             break;
836 
837         case USB_STATUS_UNRECOGNIZED_MEDIA:
838             hal_log_info("USB_STATUS_UNRECOGNIZED_MEDIA");
839             break;
840 
841         case USB_STATUS_UNKOWN_ERROR:
842             hal_log_info("USB_STATUS_UNKOWN_ERROR");
843             break;
844 
845         default:
846             hal_log_info("unkonw status %d", status);
847             break;
848     }
849 
850     return ;
851 }
852 
Pr__s32DiskInfo(__UsbBlkDev_t * BlkDev)853 static void Pr__s32DiskInfo(__UsbBlkDev_t *BlkDev)
854 {
855     __mscLun_t *mscLun = NULL;
856 
857     if (BlkDev == NULL)
858     {
859         hal_log_err("ERR: Pr__s32DiskInfo: input error, BlkDev = %x", BlkDev);
860         return ;
861     }
862 
863     mscLun = BlkDev->Lun;
864 
865     if (mscLun == NULL)
866     {
867         hal_log_err("ERR: UnitReady: mscLun == NULL");
868         return ;
869     }
870 
871     hal_log_info("-----------------Disk Information-----------------");
872     hal_log_info("WriteProtect = %d", mscLun->WriteProtect);
873     hal_log_info("MediaPresent = %d", mscLun->MediaPresent);
874     hal_log_info("WCE          = %d", mscLun->WCE);
875     hal_log_info("RCD          = %d", mscLun->RCD);
876     hal_log_info("capacity     = %dM, sector number = %d", (mscLun->disk_info.capacity * 512) / (1024 * 1024), mscLun->disk_info.capacity);
877     hal_log_info("sector_size  = %d", mscLun->disk_info.sector_size);
878     hal_log_info("DevNo        = %d", BlkDev->DevNo);
879     hal_log_info("ClassName    = %s", BlkDev->ClassName);
880     hal_log_info("DevName      = %s", BlkDev->DevName);
881     hal_log_info("--------------------------------------------------");
882     return ;
883 }
884 
885 
886 /*
887 *******************************************************************************
888 *                     WaitForDeviceReady
889 *
890 * Description:
891 *
892 *
893 * Parameters:
894 *
895 *
896 * Return value:
897 *
898 *
899 * note:
900 *
901 *
902 *******************************************************************************
903 */
WaitForDeviceReady(__UsbBlkDev_t * BlkDev)904 static void WaitForDeviceReady(__UsbBlkDev_t *BlkDev)
905 {
906     __mscLun_t *mscLun = NULL;
907     unsigned int Repeat = 10;
908     int ret = USB_STATUS_SUCCESS;
909 
910     if (BlkDev == NULL)
911     {
912         hal_log_err("ERR: UnitReady: input error");
913         return ;
914     }
915 
916     mscLun = BlkDev->Lun;
917 
918     if (mscLun == NULL)
919     {
920         hal_log_err("ERR: UnitReady: mscLun == NULL");
921         return ;
922     }
923 
924     hal_sem_wait(mscLun->Lock);
925     /* Unit Ready? */
926     ret = ScsiTestUnitReady(mscLun);
927 
928     if (ret == USB_STATUS_SUCCESS)
929     {
930         goto end;
931     }
932     else if (mscLun->DeviceType == SCSI_DEVICE_CDROM)
933     {
934         PrintDevStatus("ScsiTestUnitReady", ret);
935         ScsiStartStopUnit(mscLun, 0, 1);
936     }
937 
938     /* wait for unit ready */
939     do
940     {
941         ret = ScsiTestUnitReady(mscLun);
942 
943         if (ret == USB_STATUS_INVALID_COMMAND)
944         {
945             hal_log_err("ERR: Device Not Support ScsiTestUnitReady command");
946             PrintDevStatus("ScsiTestUnitReady", ret);
947             break;
948         }
949 
950         /* 判断media的状态 */
951         if (ret != USB_STATUS_SUCCESS)
952         {
953             mscLun->MediaPresent = 0;
954         }
955         else if ((ret == USB_STATUS_SUCCESS && !(mscLun->MediaPresent)))
956         {
957             hal_log_info("Media not Present, but ScsiTestUnitReady command is successful,"
958                       " Repeat = %d", Repeat);
959             mscLun->MediaPresent = 1;
960         }
961 
962         --Repeat;
963 
964         if (USB_STATUS_SUCCESS != ret && Repeat != 0)
965         {
966             hal_msleep(1000);
967         }
968     } while (Repeat && !(mscLun->MediaPresent));
969 
970 end:
971     hal_sem_post(mscLun->Lock);
972     return ;
973 }
974 
975 /*
976 *******************************************************************************
977 *                     ReadCapacity
978 *
979 * Description:
980 *
981 *
982 * Parameters:
983 *
984 *
985 * Return value:
986 *
987 *
988 * note:
989 *
990 *
991 *******************************************************************************
992 */
ReadCapacity(__UsbBlkDev_t * BlkDev)993 static void ReadCapacity(__UsbBlkDev_t *BlkDev)
994 {
995     __mscLun_t *mscLun = NULL;
996     int ret = 0;
997 
998     if (BlkDev == NULL)
999     {
1000         hal_log_err("ERR: UnitReady: input error");
1001         return ;
1002     }
1003 
1004     mscLun = BlkDev->Lun;
1005 
1006     if (mscLun == NULL)
1007     {
1008         hal_log_err("ERR: UnitReady: mscLun == NULL");
1009         return ;
1010     }
1011 
1012     if (mscLun->MediaPresent)
1013     {
1014         hal_sem_wait(mscLun->Lock);
1015         ret = ScsiReadCapacity(mscLun);
1016 
1017         if (ret != USB_STATUS_SUCCESS)
1018         {
1019             hal_log_err("ERR: ScsiReadCapacity failed");
1020             PrintDevStatus("ScsiReadCapacity", ret);
1021         }
1022 
1023         hal_sem_post(mscLun->Lock);
1024     }
1025     else
1026     {
1027         hal_log_err("ERR: media is not present, ReadCapacity failed");
1028     }
1029 
1030     return ;
1031 }
1032 
1033 /*
1034 *******************************************************************************
1035 *                     ReadProtectFlag
1036 *
1037 * Description:
1038 *    获得设备写保护标志位
1039 *
1040 * Parameters:
1041 *
1042 *
1043 * Return value:
1044 *
1045 *
1046 * note:
1047 *
1048 *
1049 *******************************************************************************
1050 */
ReadProtectFlag(__UsbBlkDev_t * BlkDev)1051 static void ReadProtectFlag(__UsbBlkDev_t *BlkDev)
1052 {
1053     __mscLun_t *mscLun = NULL;
1054     __BlockDevSpecPara_t *DevSpecPara = NULL;
1055     unsigned int cmd_version = 6;   /* 默认使用mode sense 6 */
1056     int ret = 0;
1057     unsigned int ActLen = 0;
1058     unsigned char  buffer[SCSI_MODE_SENSE_MAX_DATA_LEN];
1059 
1060     if (BlkDev == NULL)
1061     {
1062         hal_log_err("ERR: UnitReady: input error");
1063         return ;
1064     }
1065 
1066     mscLun = BlkDev->Lun;
1067 
1068     if (mscLun == NULL)
1069     {
1070         hal_log_err("ERR: UnitReady: mscLun == NULL");
1071         return ;
1072     }
1073 
1074     if ((mscLun->mscDev->SubClass != USBMSC_SUBCLASS_SCSI) || (mscLun->DeviceType == SCSI_DEVICE_CDROM))
1075     {
1076         cmd_version = 10;
1077     }
1078 
1079     memset(buffer, 0, SCSI_MODE_SENSE_MAX_DATA_LEN);
1080     hal_sem_wait(mscLun->Lock);
1081 
1082     /* send mode sense command */
1083     if (cmd_version == 6)
1084     {
1085         ret = ScsiModeSense6(mscLun,
1086                              MODE_PAGE_ALL_PAGES,
1087                              ((mscLun->DeviceType == SCSI_DEVICE_CDROM) ? 1 : 0),
1088                              (void *)buffer,
1089                              SCSI_MODE_SENSE_MAX_DATA_LEN,
1090                              &ActLen);
1091 
1092         if (ret == USB_STATUS_INVALID_COMMAND)
1093         {
1094             cmd_version = 10;
1095         }
1096         else if (ret != USB_STATUS_SUCCESS)
1097         {
1098             hal_log_err("ERR: ScsiModeSense6 failed");
1099             PrintDevStatus("ScsiModeSense6", ret);
1100             cmd_version = 0;
1101         }
1102     }
1103 
1104     if (cmd_version == 10)
1105     {
1106         ret = ScsiModeSense10(mscLun,
1107                               MODE_PAGE_ALL_PAGES,
1108                               ((mscLun->DeviceType == SCSI_DEVICE_CDROM) ? 1 : 0),
1109                               (void *)buffer,
1110                               SCSI_MODE_SENSE_MAX_DATA_LEN,
1111                               &ActLen);
1112 
1113         if (ret != USB_STATUS_SUCCESS)
1114         {
1115             hal_log_err("ERR: ScsiModeSense10 failed");
1116             PrintDevStatus("ScsiModeSense10", ret);
1117             cmd_version = 0;
1118         }
1119     }
1120 
1121     /* parse mode sense data */
1122     if (ActLen)
1123     {
1124         if (cmd_version == 6)
1125         {
1126             DevSpecPara = (__BlockDevSpecPara_t *) & (buffer[2]);
1127             mscLun->WriteProtect = DevSpecPara->WP;
1128         }
1129         else if (cmd_version == 10)
1130         {
1131             DevSpecPara = (__BlockDevSpecPara_t *) & (buffer[3]);
1132             mscLun->WriteProtect = DevSpecPara->WP;
1133         }
1134         else
1135         {
1136             mscLun->WriteProtect = 0;
1137         }
1138     }
1139     else   /* if deivce not have 0x3f page, then no mode sense data */
1140     {
1141         hal_log_err("ReadProtectFlag: no mode sense data");
1142         mscLun->WriteProtect = 0;
1143     }
1144 
1145     hal_sem_post(mscLun->Lock);
1146     return ;
1147 }
1148 
1149 /*
1150 *******************************************************************************
1151 *                     ReadCacheType
1152 *
1153 * Description:
1154 *
1155 *
1156 * Parameters:
1157 *
1158 *
1159 * Return value:
1160 *
1161 *
1162 * note:
1163 *
1164 *
1165 *******************************************************************************
1166 */
ReadCacheType(__UsbBlkDev_t * BlkDev)1167 static void ReadCacheType(__UsbBlkDev_t *BlkDev)
1168 {
1169     __mscLun_t *mscLun = NULL;
1170     unsigned int cmd_version = 6;   /* 默认使用mode sense 6 */
1171     int ret = 0;
1172     unsigned char  buffer[SCSI_MODE_SENSE_MAX_DATA_LEN];
1173     unsigned int ActLen = 0;
1174     unsigned int CachepageValid = 0;
1175     unsigned int CachepageAddr = 0;
1176 
1177     if (BlkDev == NULL)
1178     {
1179         hal_log_err("ERR: UnitReady: input error");
1180         return ;
1181     }
1182 
1183     mscLun = BlkDev->Lun;
1184 
1185     if (mscLun == NULL)
1186     {
1187         hal_log_err("ERR: UnitReady: mscLun == NULL");
1188         return ;
1189     }
1190 
1191     if ((mscLun->mscDev->SubClass != USBMSC_SUBCLASS_SCSI) || (mscLun->DeviceType == SCSI_DEVICE_CDROM))
1192     {
1193         cmd_version = 10;
1194     }
1195 
1196     memset(buffer, 0, SCSI_MODE_SENSE_MAX_DATA_LEN);
1197     hal_sem_wait(mscLun->Lock);
1198 
1199     /* send mode sense command */
1200     if (cmd_version == 6)
1201     {
1202         ret = ScsiModeSense6(mscLun,
1203                              MODE_PAGE_WCACHING_PAGE,
1204                              ((mscLun->DeviceType == SCSI_DEVICE_CDROM) ? 1 : 0),
1205                              (void *)buffer,
1206                              SCSI_MODE_SENSE_MAX_DATA_LEN,
1207                              &ActLen);
1208 
1209         if (ret == USB_STATUS_INVALID_COMMAND)
1210         {
1211             cmd_version = 10;
1212         }
1213         else if (ret != USB_STATUS_SUCCESS)
1214         {
1215             hal_log_err("ERR: ScsiModeSense6 failed");
1216             PrintDevStatus("ScsiModeSense6", ret);
1217             cmd_version = 0;
1218         }
1219     }
1220 
1221     if (cmd_version == 10)
1222     {
1223         ret = ScsiModeSense10(mscLun,
1224                               MODE_PAGE_WCACHING_PAGE,
1225                               ((mscLun->DeviceType == SCSI_DEVICE_CDROM) ? 1 : 0),
1226                               (void *)buffer,
1227                               SCSI_MODE_SENSE_MAX_DATA_LEN,
1228                               &ActLen);
1229 
1230         if (ret != USB_STATUS_SUCCESS)
1231         {
1232             hal_log_err("ERR: ScsiModeSense10 failed");
1233             PrintDevStatus("ScsiModeSense10", ret);
1234             cmd_version = 0;
1235         }
1236     }
1237 
1238     /* parse mode sense data */
1239     if (ActLen)
1240     {
1241         if (cmd_version == 6)
1242         {
1243             CachepageAddr  = 4;
1244             CachepageValid = 1;
1245         }
1246         else if (cmd_version == 10)
1247         {
1248             CachepageAddr  = 8;
1249             CachepageValid = 1;
1250         }
1251         else
1252         {
1253             CachepageAddr  = 0;
1254             CachepageValid = 0;
1255         }
1256 
1257         if (CachepageValid)
1258         {
1259             mscLun->WCE = (buffer[CachepageAddr + 2] & 0x04) ? 1 : 0;   /* bit2 */
1260             mscLun->RCD = (buffer[CachepageAddr + 2] & 0x01) ? 1 : 0;   /* bit0 */
1261         }
1262         else
1263         {
1264             mscLun->WCE = 0;
1265             mscLun->RCD = 0;
1266         }
1267     }
1268     else   /* if deivce not have cahe page, then no mode sense data */
1269     {
1270         hal_log_err("ReadCacheType: no mode sense data");
1271         mscLun->WCE = 0;
1272         mscLun->RCD = 0;
1273     }
1274 
1275     hal_sem_post(mscLun->Lock);
1276     return ;
1277 }
1278 
1279 
1280 /*
1281 *******************************************************************************
1282 *                     GetDiskInfo
1283 *
1284 * Description:
1285 *
1286 *
1287 * Parameters:
1288 *
1289 *
1290 * Return value:
1291 *    0  :成功
1292 *   !0  :失败
1293 *
1294 * note:
1295 *    无
1296 *
1297 *******************************************************************************
1298 */
GetDiskInfo(__UsbBlkDev_t * BlkDev)1299 void GetDiskInfo(__UsbBlkDev_t *BlkDev)
1300 {
1301     __mscLun_t *mscLun = NULL;
1302 
1303     if (BlkDev == NULL)
1304     {
1305         hal_log_err("ERR: GetDiskInfo: input error");
1306         return ;
1307     }
1308 
1309     mscLun = BlkDev->Lun;
1310 
1311     if (mscLun == NULL)
1312     {
1313         hal_log_err("ERR: GetDiskInfo: mscLun == NULL");
1314         return ;
1315     }
1316 
1317     mscLun->MediaPresent          = 1;
1318     mscLun->WriteProtect          = 0;
1319     mscLun->WCE                   = 0;
1320     mscLun->RCD                   = 0;
1321     mscLun->disk_info.capacity    = 0;
1322     mscLun->disk_info.sector_size = 0;
1323     /* check unit is ready? */
1324     WaitForDeviceReady(BlkDev);
1325 
1326     if (!mscLun->MediaPresent)
1327     {
1328         hal_log_err("ERR: can not get disk info, for unit is not ready");
1329         mscLun->MediaPresent          = 0;
1330         mscLun->WriteProtect          = 0;
1331         mscLun->WCE                   = 0;
1332         mscLun->RCD                   = 0;
1333         mscLun->disk_info.capacity    = 0;
1334         mscLun->disk_info.sector_size = 0;
1335         return;
1336     }
1337 
1338     /* read capacity */
1339     ReadCapacity(BlkDev);
1340     /* Lun是否写保护? */
1341     ReadProtectFlag(BlkDev);
1342     /* Lun是否带cache? */
1343     ReadCacheType(BlkDev);
1344     Pr__s32DiskInfo(BlkDev);
1345 //  printf("%s %d %s\n", __FILE__, __LINE__, __func__);
1346     return ;
1347 }
1348 
1349 /*
1350 *******************************************************************************
1351 *                     WaitForDeviceReady
1352 *
1353 * Description:
1354 *
1355 *
1356 * Parameters:
1357 *
1358 *
1359 * Return value:
1360 *
1361 *
1362 * note:
1363 *
1364 *
1365 *******************************************************************************
1366 */
ShutDown(__UsbBlkDev_t * BlkDev)1367 void ShutDown(__UsbBlkDev_t *BlkDev)
1368 {
1369     __mscLun_t *mscLun = NULL;
1370 
1371     if (BlkDev == NULL)
1372     {
1373         hal_log_err("ERR: GetDiskInfo: input error");
1374         return ;
1375     }
1376 
1377     mscLun = BlkDev->Lun;
1378 
1379     if (mscLun == NULL)
1380     {
1381         hal_log_err("ERR: GetDiskInfo: mscLun == NULL");
1382         return ;
1383     }
1384 
1385     /* sync cache */
1386     if (mscLun->WCE)
1387     {
1388         ScsiSynchronizeCache(mscLun);
1389     }
1390 
1391     return ;
1392 }
1393 
1394 /*
1395 *******************************************************************************
1396 *                     UsbBlkDevReg
1397 *
1398 * Description:
1399 *
1400 *
1401 * Parameters:
1402 *
1403 *
1404 * Return value:
1405 *    0  :成功
1406 *   !0  :失败
1407 *
1408 * note:
1409 *    无
1410 *
1411 *******************************************************************************
1412 */
UsbBlkDevReg(__UsbBlkDev_t * BlkDev,unsigned char * ClassName,unsigned int RegDisk)1413 int UsbBlkDevReg(__UsbBlkDev_t *BlkDev, unsigned char *ClassName, unsigned int RegDisk)
1414 {
1415 extern unsigned int hub_GetHubSeries(struct usb_host_virt_dev *udev);
1416 extern unsigned int hub_GetHubNo(struct usb_host_virt_dev *udev);
1417 
1418     if (BlkDev == NULL || ClassName == NULL)
1419     {
1420         hal_log_err("ERR: UsbBlkDevInit: input error");
1421         return -1;
1422     }
1423 
1424     //更新标志位
1425     BlkDev->Magic = USB_BLK_DEV_MAGIC;
1426 
1427     if (RegDisk)
1428     {
1429         BlkDev->is_RegDisk = 1;
1430     }
1431 
1432     strncpy((char *)BlkDev->ClassName, (const char *)ClassName, strlen((const char *)ClassName));
1433     /* save deivce info */
1434     memset(&BlkDev->device_info, 0, sizeof(usbh_disk_device_info_t));
1435     strcpy((char *)BlkDev->device_info.Classname, (char *)BlkDev->ClassName);
1436     strcpy((char *)BlkDev->device_info.DeviceName, (char *)BlkDev->DevName);
1437     strcpy((char *)BlkDev->device_info.DeivceInfo.Vender, (char *)BlkDev->Lun->Vendor);
1438     strcpy((char *)BlkDev->device_info.DeivceInfo.Product, (char *)BlkDev->Lun->Product);
1439     strcpy((char *)BlkDev->device_info.DeivceInfo.Serial, (char *)BlkDev->Lun->Revision);
1440 
1441     BlkDev->device_info.DeivceInfo.HubPortNo = (hub_GetHubNo(BlkDev->Lun->mscDev->pusb_dev) & 0x0f)
1442             | ((hub_GetHubSeries(BlkDev->Lun->mscDev->pusb_dev) & 0x0f) << 8);
1443 
1444     if ((BlkDev->Lun->DeviceType == SCSI_DEVICE_DIRECT_ACCESS) && (BlkDev->Lun->RemoveAble == 0))
1445     {
1446         BlkDev->device_info.DeivceInfo.DeviceType = USB_STORAGE_DEVICE_TYPE_HDD;
1447     }
1448     else if ((BlkDev->Lun->DeviceType == SCSI_DEVICE_DIRECT_ACCESS) && BlkDev->Lun->RemoveAble)
1449     {
1450         BlkDev->device_info.DeivceInfo.DeviceType = USB_STORAGE_DEVICE_TYPE_UDISK;
1451     }
1452     else if (BlkDev->Lun->DeviceType == SCSI_DEVICE_CDROM)
1453     {
1454         BlkDev->device_info.DeivceInfo.DeviceType = USB_STORAGE_DEVICE_TYPE_CDROM;
1455     }
1456     else
1457     {
1458         BlkDev->device_info.DeivceInfo.DeviceType = USB_STORAGE_DEVICE_TYPE_UNKOWN;
1459     }
1460 
1461     printf("Classname  = %s\n", BlkDev->device_info.Classname);
1462     printf("DevName  = %s\n", BlkDev->DevName);
1463     printf("DeviceName = %s\n", BlkDev->device_info.DeviceName);
1464     printf("Vender     = %s\n", BlkDev->device_info.DeivceInfo.Vender);
1465     printf("Product    = %s\n", BlkDev->device_info.DeivceInfo.Product);
1466     printf("Serial     = %s\n", BlkDev->device_info.DeivceInfo.Serial);
1467     printf("HubPortNo  = %d\n", BlkDev->device_info.DeivceInfo.HubPortNo);
1468     printf("DeviceType = %d\n", BlkDev->device_info.DeivceInfo.DeviceType);
1469     usbh_disk_SaveDeviceInfo(&BlkDev->device_info);
1470     /* reg disk */
1471     BlkDev->DevRegHdle = esDEV_DevReg((const char *)ClassName, (const char *)BlkDev->DevName, &(BlkDev->DiskOp), (void *)BlkDev);
1472 
1473     if (BlkDev->DevRegHdle == NULL)
1474     {
1475         hal_log_err("ERR: Block device register failed.");
1476         BlkDev->Magic = 0;
1477         BlkDev->is_RegDisk = 0;
1478         return USB_ERR_UNKOWN_ERROR;
1479     }
1480 
1481     printf("..............................................................................\n");
1482     printf("[USB Disk]: Register new device, class = [%s], dev = [%s]\n", BlkDev->ClassName, BlkDev->DevName);
1483     printf("..............................................................................\n");
1484     return USB_ERR_SUCCESS;
1485 }
1486 
1487 /*
1488 *******************************************************************************
1489 *                     UsbBlkDevUnReg
1490 *
1491 * Description:
1492 *
1493 *
1494 * Parameters:
1495 *
1496 *
1497 * Return value:
1498 *    0  :成功
1499 *   !0  :失败
1500 *
1501 * note:
1502 *    无
1503 *
1504 *******************************************************************************
1505 */
UsbBlkDevUnReg(__UsbBlkDev_t * BlkDev)1506 int UsbBlkDevUnReg(__UsbBlkDev_t *BlkDev)
1507 {
1508     if (BlkDev == NULL)
1509     {
1510         hal_log_err("ERR: UsbBlkDevUnReg: input error");
1511         return -1;
1512         //return USB_ERR_BAD_ARGUMENTS;
1513     }
1514 
1515     if (BlkDev->DevRegHdle)
1516     {
1517         hal_log_info("..............................................................................");
1518         hal_log_info("[USB Disk]: UnRegister old device, class = [%s], dev = [%s]", BlkDev->ClassName, BlkDev->DevName);
1519         hal_log_info("..............................................................................");
1520         usbh_disk_FreeDeviceInfo(&BlkDev->device_info);
1521         esDEV_DevUnreg(BlkDev->DevRegHdle);
1522         BlkDev->DevRegHdle = NULL;
1523         BlkDev->is_RegDisk = 0;
1524         memset(BlkDev->ClassName, 0, USB_BULK_DISK_MAX_NAME_LEN);
1525     }
1526     else
1527     {
1528         hal_log_err("ERR: UsbBlkDevUnReg: DevRegHdle = NULL");
1529         //return USB_ERR_BAD_ARGUMENTS;
1530         return -1;
1531     }
1532 
1533     set_usbh_temp_buff_invalid_by_dev(BlkDev->DevNo);
1534     BlkDev->Magic = 0;
1535     return 0;
1536     //return USB_ERR_SUCCESS;
1537 
1538 }
1539 
1540 
1541