xref: /openbmc/linux/drivers/usb/gadget/legacy/serial.c (revision 52945c1c)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * serial.c -- USB gadget serial driver
4  *
5  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
6  * Copyright (C) 2008 by David Brownell
7  * Copyright (C) 2008 by Nokia Corporation
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/kstrtox.h>
13 #include <linux/module.h>
14 #include <linux/tty.h>
15 #include <linux/tty_flip.h>
16 
17 #include "u_serial.h"
18 
19 
20 /* Defines */
21 
22 #define GS_VERSION_STR			"v2.4"
23 #define GS_VERSION_NUM			0x2400
24 
25 #define GS_LONG_NAME			"Gadget Serial"
26 #define GS_VERSION_NAME			GS_LONG_NAME " " GS_VERSION_STR
27 
28 /*-------------------------------------------------------------------------*/
29 USB_GADGET_COMPOSITE_OPTIONS();
30 
31 /* Thanks to NetChip Technologies for donating this product ID.
32 *
33 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
34 * Instead:  allocate your own, using normal USB-IF procedures.
35 */
36 #define GS_VENDOR_ID			0x0525	/* NetChip */
37 #define GS_PRODUCT_ID			0xa4a6	/* Linux-USB Serial Gadget */
38 #define GS_CDC_PRODUCT_ID		0xa4a7	/* ... as CDC-ACM */
39 #define GS_CDC_OBEX_PRODUCT_ID		0xa4a9	/* ... as CDC-OBEX */
40 
41 /* string IDs are assigned dynamically */
42 
43 #define STRING_DESCRIPTION_IDX		USB_GADGET_FIRST_AVAIL_IDX
44 
45 static struct usb_string strings_dev[] = {
46 	[USB_GADGET_MANUFACTURER_IDX].s = "",
47 	[USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
48 	[USB_GADGET_SERIAL_IDX].s = "",
49 	[STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
50 	{  } /* end of list */
51 };
52 
53 static struct usb_gadget_strings stringtab_dev = {
54 	.language	= 0x0409,	/* en-us */
55 	.strings	= strings_dev,
56 };
57 
58 static struct usb_gadget_strings *dev_strings[] = {
59 	&stringtab_dev,
60 	NULL,
61 };
62 
63 static struct usb_device_descriptor device_desc = {
64 	.bLength =		USB_DT_DEVICE_SIZE,
65 	.bDescriptorType =	USB_DT_DEVICE,
66 	/* .bcdUSB = DYNAMIC */
67 	/* .bDeviceClass = f(use_acm) */
68 	.bDeviceSubClass =	0,
69 	.bDeviceProtocol =	0,
70 	/* .bMaxPacketSize0 = f(hardware) */
71 	.idVendor =		cpu_to_le16(GS_VENDOR_ID),
72 	/* .idProduct =	f(use_acm) */
73 	.bcdDevice = cpu_to_le16(GS_VERSION_NUM),
74 	/* .iManufacturer = DYNAMIC */
75 	/* .iProduct = DYNAMIC */
76 	.bNumConfigurations =	1,
77 };
78 
79 static const struct usb_descriptor_header *otg_desc[2];
80 
81 /*-------------------------------------------------------------------------*/
82 
83 /* Module */
84 MODULE_DESCRIPTION(GS_VERSION_NAME);
85 MODULE_AUTHOR("Al Borchers");
86 MODULE_AUTHOR("David Brownell");
87 MODULE_LICENSE("GPL");
88 
89 static bool use_acm = true;
90 module_param(use_acm, bool, 0);
91 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
92 
93 static bool use_obex = false;
94 module_param(use_obex, bool, 0);
95 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
96 
97 static unsigned n_ports = 1;
98 module_param(n_ports, uint, 0);
99 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
100 
101 static bool enable = true;
102 
103 static int switch_gserial_enable(bool do_enable);
104 
105 static int enable_set(const char *s, const struct kernel_param *kp)
106 {
107 	bool do_enable;
108 	int ret;
109 
110 	if (!s)	/* called for no-arg enable == default */
111 		return 0;
112 
113 	ret = kstrtobool(s, &do_enable);
114 	if (ret || enable == do_enable)
115 		return ret;
116 
117 	ret = switch_gserial_enable(do_enable);
118 	if (!ret)
119 		enable = do_enable;
120 
121 	return ret;
122 }
123 
124 static const struct kernel_param_ops enable_ops = {
125 	.set = enable_set,
126 	.get = param_get_bool,
127 };
128 
129 module_param_cb(enable, &enable_ops, &enable, 0644);
130 
131 /*-------------------------------------------------------------------------*/
132 
133 static struct usb_configuration serial_config_driver = {
134 	/* .label = f(use_acm) */
135 	/* .bConfigurationValue = f(use_acm) */
136 	/* .iConfiguration = DYNAMIC */
137 	.bmAttributes	= USB_CONFIG_ATT_SELFPOWER,
138 };
139 
140 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
141 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
142 
143 static int serial_register_ports(struct usb_composite_dev *cdev,
144 		struct usb_configuration *c, const char *f_name)
145 {
146 	int i;
147 	int ret;
148 
149 	ret = usb_add_config_only(cdev, c);
150 	if (ret)
151 		goto out;
152 
153 	for (i = 0; i < n_ports; i++) {
154 
155 		fi_serial[i] = usb_get_function_instance(f_name);
156 		if (IS_ERR(fi_serial[i])) {
157 			ret = PTR_ERR(fi_serial[i]);
158 			goto fail;
159 		}
160 
161 		f_serial[i] = usb_get_function(fi_serial[i]);
162 		if (IS_ERR(f_serial[i])) {
163 			ret = PTR_ERR(f_serial[i]);
164 			goto err_get_func;
165 		}
166 
167 		ret = usb_add_function(c, f_serial[i]);
168 		if (ret)
169 			goto err_add_func;
170 	}
171 
172 	return 0;
173 
174 err_add_func:
175 	usb_put_function(f_serial[i]);
176 err_get_func:
177 	usb_put_function_instance(fi_serial[i]);
178 
179 fail:
180 	i--;
181 	while (i >= 0) {
182 		usb_remove_function(c, f_serial[i]);
183 		usb_put_function(f_serial[i]);
184 		usb_put_function_instance(fi_serial[i]);
185 		i--;
186 	}
187 out:
188 	return ret;
189 }
190 
191 static int gs_bind(struct usb_composite_dev *cdev)
192 {
193 	int			status;
194 
195 	/* Allocate string descriptor numbers ... note that string
196 	 * contents can be overridden by the composite_dev glue.
197 	 */
198 
199 	status = usb_string_ids_tab(cdev, strings_dev);
200 	if (status < 0)
201 		goto fail;
202 	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
203 	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
204 	status = strings_dev[STRING_DESCRIPTION_IDX].id;
205 	serial_config_driver.iConfiguration = status;
206 
207 	if (gadget_is_otg(cdev->gadget)) {
208 		if (!otg_desc[0]) {
209 			struct usb_descriptor_header *usb_desc;
210 
211 			usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
212 			if (!usb_desc) {
213 				status = -ENOMEM;
214 				goto fail;
215 			}
216 			usb_otg_descriptor_init(cdev->gadget, usb_desc);
217 			otg_desc[0] = usb_desc;
218 			otg_desc[1] = NULL;
219 		}
220 		serial_config_driver.descriptors = otg_desc;
221 		serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
222 	}
223 
224 	/* register our configuration */
225 	if (use_acm) {
226 		status  = serial_register_ports(cdev, &serial_config_driver,
227 				"acm");
228 		usb_ep_autoconfig_reset(cdev->gadget);
229 	} else if (use_obex)
230 		status = serial_register_ports(cdev, &serial_config_driver,
231 				"obex");
232 	else {
233 		status = serial_register_ports(cdev, &serial_config_driver,
234 				"gser");
235 	}
236 	if (status < 0)
237 		goto fail1;
238 
239 	usb_composite_overwrite_options(cdev, &coverwrite);
240 	INFO(cdev, "%s\n", GS_VERSION_NAME);
241 
242 	return 0;
243 fail1:
244 	kfree(otg_desc[0]);
245 	otg_desc[0] = NULL;
246 fail:
247 	return status;
248 }
249 
250 static int gs_unbind(struct usb_composite_dev *cdev)
251 {
252 	int i;
253 
254 	for (i = 0; i < n_ports; i++) {
255 		usb_put_function(f_serial[i]);
256 		usb_put_function_instance(fi_serial[i]);
257 	}
258 
259 	kfree(otg_desc[0]);
260 	otg_desc[0] = NULL;
261 
262 	return 0;
263 }
264 
265 static struct usb_composite_driver gserial_driver = {
266 	.name		= "g_serial",
267 	.dev		= &device_desc,
268 	.strings	= dev_strings,
269 	.max_speed	= USB_SPEED_SUPER,
270 	.bind		= gs_bind,
271 	.unbind		= gs_unbind,
272 };
273 
274 static int switch_gserial_enable(bool do_enable)
275 {
276 	if (!serial_config_driver.label)
277 		/* gserial_init() was not called, yet */
278 		return 0;
279 
280 	if (do_enable)
281 		return usb_composite_probe(&gserial_driver);
282 
283 	usb_composite_unregister(&gserial_driver);
284 	return 0;
285 }
286 
287 static int __init gserial_init(void)
288 {
289 	/* We *could* export two configs; that'd be much cleaner...
290 	 * but neither of these product IDs was defined that way.
291 	 */
292 	if (use_acm) {
293 		serial_config_driver.label = "CDC ACM config";
294 		serial_config_driver.bConfigurationValue = 2;
295 		device_desc.bDeviceClass = USB_CLASS_COMM;
296 		device_desc.idProduct =
297 				cpu_to_le16(GS_CDC_PRODUCT_ID);
298 	} else if (use_obex) {
299 		serial_config_driver.label = "CDC OBEX config";
300 		serial_config_driver.bConfigurationValue = 3;
301 		device_desc.bDeviceClass = USB_CLASS_COMM;
302 		device_desc.idProduct =
303 			cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
304 	} else {
305 		serial_config_driver.label = "Generic Serial config";
306 		serial_config_driver.bConfigurationValue = 1;
307 		device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
308 		device_desc.idProduct =
309 				cpu_to_le16(GS_PRODUCT_ID);
310 	}
311 	strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
312 
313 	if (!enable)
314 		return 0;
315 
316 	return usb_composite_probe(&gserial_driver);
317 }
318 module_init(gserial_init);
319 
320 static void __exit gserial_cleanup(void)
321 {
322 	if (enable)
323 		usb_composite_unregister(&gserial_driver);
324 }
325 module_exit(gserial_cleanup);
326