summaryrefslogtreecommitdiffstats
path: root/commands/usbserial.c
blob: 7e82112df04cc41ffd4c1866248f920c6a2f9935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * usbserial.c - usb serial gadget command
 *
 * Copyright (c) 2011 Eric Bénard <eric@eukrea.com>, Eukréa Electromatique
 * based on dfu.c which is :
 * Copyright (c) 2009 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <common.h>
#include <command.h>
#include <errno.h>
#include <malloc.h>
#include <getopt.h>
#include <fs.h>
#include <xfuncs.h>
#include <usb/usbserial.h>

static int do_usbserial(int argc, char *argv[])
{
	int opt;
	struct usb_serial_pdata pdata;
	char *manufacturer = "barebox";
	const char *productname = barebox_get_model();
	u16 idVendor = 0, idProduct = 0;
	int acm = 1;

	while ((opt = getopt(argc, argv, "m:p:V:P:asd")) > 0) {
		switch (opt) {
		case 'm':
			manufacturer = optarg;
			break;
		case 'p':
			productname = optarg;
			break;
		case 'V':
			idVendor = simple_strtoul(optarg, NULL, 0);
			break;
		case 'P':
			idProduct = simple_strtoul(optarg, NULL, 0);
			break;
		case 'a':
			acm = 1;
			break;
		case 's':
			acm = 0;
			break;
		case 'd':
			usb_serial_unregister();
			return 0;
		}
	}

	pdata.manufacturer = manufacturer;
	pdata.productname = productname;
	pdata.idVendor = idVendor;
	pdata.idProduct = idProduct;
	pdata.acm = acm;

	return usb_serial_register(&pdata);
}

BAREBOX_CMD_HELP_START(usbserial)
BAREBOX_CMD_HELP_TEXT("Enable / disable a serial gadget on the USB device interface.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-m STR",  "Manufacturer string (barebox)")
BAREBOX_CMD_HELP_OPT ("-p STR",  "product string")
BAREBOX_CMD_HELP_OPT ("-V ID",   "vendor id")
BAREBOX_CMD_HELP_OPT ("-P ID",   "product id")
BAREBOX_CMD_HELP_OPT ("-a",   "CDC ACM (default)")
BAREBOX_CMD_HELP_OPT ("-s",   "Generic Serial")
BAREBOX_CMD_HELP_OPT ("-d",   "Disable the serial gadget")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(usbserial)
	.cmd		= do_usbserial,
	BAREBOX_CMD_DESC("serial gadget enable/disable")
	BAREBOX_CMD_OPTS("[-mpVPasd] <description>")
	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
	BAREBOX_CMD_HELP(cmd_usbserial_help)
BAREBOX_CMD_END