27
27
#include <sys/types.h>
28
28
#include <sys/boardctl.h>
29
29
30
+ #include <fcntl.h>
30
31
#include <stdio.h>
31
32
#include <stdbool.h>
32
33
#include <stdlib.h>
80
81
#define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|TRACE_CLASS_BITS|\
81
82
TRACE_TRANSFER_BITS|TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
82
83
84
+ /* Save device path that will bind to a USB storage LUN later */
85
+
86
+ #define LUN_ADD_BIND (l , i , p , f ) \
87
+ do \
88
+ { \
89
+ l[i].path = p; \
90
+ l[i].flags = f; \
91
+ i++; \
92
+ } while (0)
93
+
94
+ /****************************************************************************
95
+ * Private Types
96
+ ****************************************************************************/
97
+
98
+ struct usbmsc_lun_t
99
+ {
100
+ FAR const char * path ; /* The full path to the block driver */
101
+ int flags ; /* Access modes */
102
+ };
103
+
83
104
/****************************************************************************
84
105
* Private Data
85
106
****************************************************************************/
@@ -392,6 +413,23 @@ static void usbmsc_disconnect(FAR void *handle)
392
413
boardctl (BOARDIOC_USBDEV_CONTROL , (uintptr_t )& ctrl );
393
414
}
394
415
416
+ static void help_conn (void )
417
+ {
418
+ printf ("Usage: msconn [-o OPTION]... [-l LUNs]...\n" );
419
+ printf ("Configures the USB mass storage device and exports the LUN(s).\n" );
420
+ printf ("\nSupported arguments\n" );
421
+ printf (" -o\n" );
422
+ printf (" ro: Readonly\n" );
423
+ printf (" rw: Read/Write(default)\n" );
424
+ printf (" -l\n" );
425
+ printf (" Device path to export\n" );
426
+ printf ("\nExamples\n" );
427
+ printf (" 1. Export const LUN(s) only\n" );
428
+ printf (" msconn\n" );
429
+ printf (" 2. Export /dev/ramx and const LUN(s)\n" );
430
+ printf (" msconn -l /dev/ramx\n" );
431
+ }
432
+
395
433
/****************************************************************************
396
434
* Public Functions
397
435
****************************************************************************/
@@ -410,9 +448,34 @@ static void usbmsc_disconnect(FAR void *handle)
410
448
int main (int argc , FAR char * argv [])
411
449
{
412
450
struct boardioc_usbdev_ctrl_s ctrl ;
451
+ struct usbmsc_lun_t luns [CONFIG_SYSTEM_USBMSC_NLUNS ];
452
+ int num_luns = 0 ;
453
+ int flags = O_RDWR ;
413
454
FAR void * handle = NULL ;
414
455
int ret ;
415
456
457
+ memset (luns , 0 , sizeof (luns ));
458
+
459
+ while ((ret = getopt (argc , argv , "l:o:" )) != -1 )
460
+ {
461
+ switch (ret )
462
+ {
463
+ case 'o' :
464
+ if (!strcmp ("ro" , optarg ))
465
+ {
466
+ flags = O_RDONLY ;
467
+ }
468
+ else if (!strcmp ("rw" , optarg ))
469
+ {
470
+ flags = O_RDWR ;
471
+ }
472
+ break ;
473
+ case 'l' :
474
+ LUN_ADD_BIND (luns , num_luns , optarg , flags );
475
+ break ;
476
+ }
477
+ }
478
+
416
479
/* If this program is implemented as the NSH 'msconn' command, then we
417
480
* need to do a little error checking to assure that we are not being
418
481
* called re-entrantly.
@@ -428,6 +491,38 @@ int main(int argc, FAR char *argv[])
428
491
return EXIT_FAILURE ;
429
492
}
430
493
494
+ /* Add const LUNs */
495
+
496
+ #if CONFIG_SYSTEM_USBMSC_DEVMINOR1 > -1
497
+ # ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT1
498
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH1 , O_RDONLY );
499
+ # else
500
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH1 , O_RDWR );
501
+ # endif
502
+ #endif
503
+
504
+ #if CONFIG_SYSTEM_USBMSC_DEVMINOR2 > -1
505
+ # ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT2
506
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH2 , O_RDONLY );
507
+ # else
508
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH2 , O_RDWR );
509
+ # endif
510
+ #endif
511
+
512
+ #if CONFIG_SYSTEM_USBMSC_DEVMINOR3 > -1
513
+ # ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT3
514
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH3 , O_RDONLY );
515
+ # else
516
+ LUN_ADD_BIND (luns , num_luns , CONFIG_SYSTEM_USBMSC_DEVPATH3 , O_RDWR );
517
+ # endif
518
+ #endif
519
+
520
+ if (num_luns <= 0 )
521
+ {
522
+ help_conn ();
523
+ return EINVAL ;
524
+ }
525
+
431
526
#ifdef CONFIG_SYSTEM_USBMSC_DEBUGMM
432
527
g_usbmsc .mmstart = mallinfo ();
433
528
g_usbmsc .mmprevious = g_usbmsc .mmstart ;
@@ -462,8 +557,8 @@ int main(int argc, FAR char *argv[])
462
557
/* Then exports the LUN(s) */
463
558
464
559
printf ("mcsonn_main: Configuring with NLUNS=%d\n" ,
465
- CONFIG_SYSTEM_USBMSC_NLUNS );
466
- ret = usbmsc_configure (CONFIG_SYSTEM_USBMSC_NLUNS , & handle );
560
+ num_luns );
561
+ ret = usbmsc_configure (num_luns , & handle );
467
562
if (ret < 0 )
468
563
{
469
564
printf ("mcsonn_main: usbmsc_configure failed: %d\n" , - ret );
@@ -478,73 +573,24 @@ int main(int argc, FAR char *argv[])
478
573
printf ("mcsonn_main: handle=%p\n" , handle );
479
574
check_test_memory_usage ("After usbmsc_configure()" );
480
575
481
- printf ("mcsonn_main: Bind LUN=0 to %s\n" ,
482
- CONFIG_SYSTEM_USBMSC_DEVPATH1 );
483
-
484
- #ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT1
485
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH1 , 0 , 0 , 0 ,
486
- true);
487
- #else
488
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH1 , 0 , 0 , 0 ,
489
- false);
490
- #endif
491
- if (ret < 0 )
576
+ while (-- num_luns >= 0 && luns [num_luns ].path != NULL )
492
577
{
493
- printf ("mcsonn_main: usbmsc_bindlun failed for LUN 1 using %s: %d\n" ,
494
- CONFIG_SYSTEM_USBMSC_DEVPATH1 , - ret );
495
- usbmsc_disconnect (handle );
496
- return EXIT_FAILURE ;
497
- }
498
-
499
- check_test_memory_usage ("After usbmsc_bindlun()" );
500
-
501
- #if CONFIG_SYSTEM_USBMSC_NLUNS > 1
502
-
503
- printf ("mcsonn_main: Bind LUN=1 to %s\n" ,
504
- CONFIG_SYSTEM_USBMSC_DEVPATH2 );
505
-
506
- #ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT2
507
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH2 , 1 , 0 , 0 ,
508
- true);
509
- #else
510
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH2 , 1 , 0 , 0 ,
511
- false);
512
- #endif
513
- if (ret < 0 )
514
- {
515
- printf ("mcsonn_main: usbmsc_bindlun failed for LUN 2 using %s: %d\n" ,
516
- CONFIG_SYSTEM_USBMSC_DEVPATH2 , - ret );
517
- usbmsc_disconnect (handle );
518
- return EXIT_FAILURE ;
519
- }
520
-
521
- check_test_memory_usage ("After usbmsc_bindlun() #2" );
522
-
523
- #if CONFIG_SYSTEM_USBMSC_NLUNS > 2
578
+ printf ("mcsonn_main: Bind LUN=%d to %s\n" ,
579
+ num_luns , luns [num_luns ].path );
524
580
525
- printf ("mcsonn_main: Bind LUN=2 to %s\n" ,
526
- CONFIG_SYSTEM_USBMSC_DEVPATH3 );
581
+ ret = usbmsc_bindlun (handle , luns [num_luns ].path , 0 , 0 , 0 ,
582
+ luns [num_luns ].flags & O_WROK ? false : true);
583
+ if (ret < 0 )
584
+ {
585
+ printf ("mcsonn_main: usbmsc_bindlun failed for LUN %d using %s: "
586
+ "%d\n" , num_luns , luns [num_luns ].path , - ret );
587
+ usbmsc_disconnect (handle );
588
+ return EXIT_FAILURE ;
589
+ }
527
590
528
- #ifdef CONFIG_SYSTEM_USBMSC_WRITEPROTECT3
529
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH3 , 2 , 0 , 0 ,
530
- true);
531
- #else
532
- ret = usbmsc_bindlun (handle , CONFIG_SYSTEM_USBMSC_DEVPATH3 , 2 , 0 , 0 ,
533
- false);
534
- #endif
535
- if (ret < 0 )
536
- {
537
- printf ("mcsonn_main: usbmsc_bindlun failed for LUN 3 using %s: %d\n" ,
538
- CONFIG_SYSTEM_USBMSC_DEVPATH3 , - ret );
539
- usbmsc_disconnect (handle );
540
- return EXIT_FAILURE ;
591
+ check_test_memory_usage ("After usbmsc_bindlun()" );
541
592
}
542
593
543
- check_test_memory_usage ("After usbmsc_bindlun() #3" );
544
-
545
- #endif
546
- #endif
547
-
548
594
ret = usbmsc_exportluns (handle );
549
595
if (ret < 0 )
550
596
{
0 commit comments