Skip to content

Commit b08c296

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
system/usbmsc: Add support for setting paths that bind to LUN at runtime
Help nsh> msconn -h Usage: msconn [-o OPTION]... [-l LUNs]... Configures the USB mass storage device and exports the LUN(s). Supported arguments -o nc: No const LUN ro: Readonly rw: Read/Write(default) -l Device path to export Examples 1. Export const LUN(s) only msconn 2. Export /dev/ramx and const LUN(s) msconn -l /dev/ramx Signed-off-by: wangjianyu3 <[email protected]>
1 parent 6600a5f commit b08c296

File tree

3 files changed

+131
-115
lines changed

3 files changed

+131
-115
lines changed

system/usbmsc/Kconfig

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ if SYSTEM_USBMSC
1818

1919
config SYSTEM_USBMSC_NLUNS
2020
int "Number of LUNs"
21-
default 1
21+
default 8
2222
---help---
23-
Defines the number of logical units (LUNs) exported by the USB
23+
Defines the max number of logical units (LUNs) exported by the USB
2424
storage driver. Each LUN corresponds to one exported block driver
25-
(or partition of a block driver). May be 1, 2, or 3. Default is 1.
25+
(or partition of a block driver). May be 1, 2, or 3.
2626

2727
config SYSTEM_USBMSC_DEVMINOR1
2828
int "LUN1 Minor Device Number"
29-
default 0
29+
default -1
3030
---help---
3131
The minor device number of the block driver for the first LUN. For
3232
example, N in /dev/mmcsdN. Used for registering the block driver.
33-
Default is zero.
33+
Default -1 means the LUN is disabled.
34+
35+
if SYSTEM_USBMSC_DEVMINOR1 > -1
3436

3537
config SYSTEM_USBMSC_DEVPATH1
3638
string "LUN1 Device Path"
@@ -46,13 +48,17 @@ config SYSTEM_USBMSC_WRITEPROTECT1
4648
Enable this if you want to write-protect the first LUN. Default is
4749
off.
4850

51+
endif
52+
4953
config SYSTEM_USBMSC_DEVMINOR2
5054
int "LUN2 Minor Device Number"
51-
default 1
55+
default -1
5256
---help---
5357
The minor device number of the block driver for the second LUN. For
5458
example, N in /dev/mmcsdN. Used for registering the block driver.
55-
Ignored if SYSTEM_USBMSC_NLUNS < 2. Default is one.
59+
Default -1 means the LUN is disabled.
60+
61+
if SYSTEM_USBMSC_DEVMINOR2 > -1
5662

5763
config SYSTEM_USBMSC_DEVPATH2
5864
string "LUN2 Device Path"
@@ -68,13 +74,17 @@ config SYSTEM_USBMSC_WRITEPROTECT2
6874
Enable this if you want to write-protect the second LUN. Ignored if
6975
SYSTEM_USBMSC_NLUNS < 2. Default is off.
7076

77+
endif
78+
7179
config SYSTEM_USBMSC_DEVMINOR3
7280
int "LUN3 Minor Device Number"
73-
default 2
81+
default -1
7482
---help---
7583
The minor device number of the block driver for the third LUN. For
7684
example, N in /dev/mmcsdN. Used for registering the block driver.
77-
Ignored if SYSTEM_USBMSC_NLUNS < 3. Default is two.
85+
Default -1 means the LUN is disabled.
86+
87+
if SYSTEM_USBMSC_DEVMINOR3 > -1
7888

7989
config SYSTEM_USBMSC_DEVPATH3
8090
string "LUN3 Device Path"
@@ -90,6 +100,8 @@ config SYSTEM_USBMSC_WRITEPROTECT3
90100
Enable this if you want to write-protect the third LUN. Ignored if
91101
SYSTEM_USBMSC_NLUNS < 3. Default is off.
92102

103+
endif
104+
93105
config SYSTEM_USBMSC_DEBUGMM
94106
bool "USB MSC MM Debug"
95107
default n

system/usbmsc/usbmsc.h

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,6 @@
3232
* Pre-Processor Definitions
3333
****************************************************************************/
3434

35-
/* Configuration ************************************************************/
36-
37-
#ifndef CONFIG_SYSTEM_USBMSC_NLUNS
38-
# define CONFIG_SYSTEM_USBMSC_NLUNS 1
39-
#endif
40-
41-
#ifndef CONFIG_SYSTEM_USBMSC_DEVMINOR1
42-
# define CONFIG_SYSTEM_USBMSC_DEVMINOR1 0
43-
#endif
44-
45-
#ifndef CONFIG_SYSTEM_USBMSC_DEVPATH1
46-
# define CONFIG_SYSTEM_USBMSC_DEVPATH1 "/dev/mmcsd0"
47-
#endif
48-
49-
#if CONFIG_SYSTEM_USBMSC_NLUNS > 1
50-
# ifndef CONFIG_SYSTEM_USBMSC_DEVMINOR2
51-
# error "CONFIG_SYSTEM_USBMSC_DEVMINOR2 for LUN=2"
52-
# endif
53-
# ifndef CONFIG_SYSTEM_USBMSC_DEVPATH2
54-
# error "CONFIG_SYSTEM_USBMSC_DEVPATH2 for LUN=2"
55-
# endif
56-
# if CONFIG_SYSTEM_USBMSC_NLUNS > 2
57-
# ifndef CONFIG_SYSTEM_USBMSC_DEVMINOR3
58-
# error "CONFIG_SYSTEM_USBMSC_DEVMINOR2 for LUN=3"
59-
# endif
60-
# ifndef CONFIG_SYSTEM_USBMSC_DEVPATH3
61-
# error "CONFIG_SYSTEM_USBMSC_DEVPATH3 for LUN=3"
62-
# endif
63-
# if CONFIG_SYSTEM_USBMSC_NLUNS > 3
64-
# error "CONFIG_SYSTEM_USBMSC_NLUNS must be {1,2,3}"
65-
# endif
66-
# else
67-
# undef CONFIG_SYSTEM_USBMSC_DEVMINOR3
68-
# undef CONFIG_SYSTEM_USBMSC_DEVPATH3
69-
# endif
70-
#else
71-
# undef CONFIG_SYSTEM_USBMSC_DEVMINOR2
72-
# undef CONFIG_SYSTEM_USBMSC_DEVPATH2
73-
# undef CONFIG_SYSTEM_USBMSC_DEVMINOR3
74-
# undef CONFIG_SYSTEM_USBMSC_DEVPATH3
75-
#endif
76-
7735
/****************************************************************************
7836
* Public Types
7937
****************************************************************************/

system/usbmsc/usbmsc_main.c

Lines changed: 110 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sys/types.h>
2828
#include <sys/boardctl.h>
2929

30+
#include <fcntl.h>
3031
#include <stdio.h>
3132
#include <stdbool.h>
3233
#include <stdlib.h>
@@ -80,6 +81,26 @@
8081
#define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|TRACE_CLASS_BITS|\
8182
TRACE_TRANSFER_BITS|TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
8283

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+
83104
/****************************************************************************
84105
* Private Data
85106
****************************************************************************/
@@ -392,6 +413,23 @@ static void usbmsc_disconnect(FAR void *handle)
392413
boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
393414
}
394415

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+
395433
/****************************************************************************
396434
* Public Functions
397435
****************************************************************************/
@@ -410,9 +448,34 @@ static void usbmsc_disconnect(FAR void *handle)
410448
int main(int argc, FAR char *argv[])
411449
{
412450
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;
413454
FAR void *handle = NULL;
414455
int ret;
415456

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+
416479
/* If this program is implemented as the NSH 'msconn' command, then we
417480
* need to do a little error checking to assure that we are not being
418481
* called re-entrantly.
@@ -428,6 +491,38 @@ int main(int argc, FAR char *argv[])
428491
return EXIT_FAILURE;
429492
}
430493

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+
431526
#ifdef CONFIG_SYSTEM_USBMSC_DEBUGMM
432527
g_usbmsc.mmstart = mallinfo();
433528
g_usbmsc.mmprevious = g_usbmsc.mmstart;
@@ -462,8 +557,8 @@ int main(int argc, FAR char *argv[])
462557
/* Then exports the LUN(s) */
463558

464559
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);
467562
if (ret < 0)
468563
{
469564
printf("mcsonn_main: usbmsc_configure failed: %d\n", -ret);
@@ -478,73 +573,24 @@ int main(int argc, FAR char *argv[])
478573
printf("mcsonn_main: handle=%p\n", handle);
479574
check_test_memory_usage("After usbmsc_configure()");
480575

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)
492577
{
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);
524580

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+
}
527590

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()");
541592
}
542593

543-
check_test_memory_usage("After usbmsc_bindlun() #3");
544-
545-
#endif
546-
#endif
547-
548594
ret = usbmsc_exportluns(handle);
549595
if (ret < 0)
550596
{

0 commit comments

Comments
 (0)