xref: /linux/drivers/usb/gadget/function/f_serial.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * f_serial.c - generic USB serial function 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  * This software is distributed under the terms of the GNU General
10  * Public License ("GPL") as published by the Free Software Foundation,
11  * either version 2 of that License or (at your option) any later version.
12  */
13 
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 
19 #include "u_serial.h"
20 
21 
22 /*
23  * This function packages a simple "generic serial" port with no real
24  * control mechanisms, just raw data transfer over two bulk endpoints.
25  *
26  * Because it's not standardized, this isn't as interoperable as the
27  * CDC ACM driver.  However, for many purposes it's just as functional
28  * if you can arrange appropriate host side drivers.
29  */
30 
31 struct f_gser {
32 	struct gserial			port;
33 	u8				data_id;
34 	u8				port_num;
35 };
36 
37 static inline struct f_gser *func_to_gser(struct usb_function *f)
38 {
39 	return container_of(f, struct f_gser, port.func);
40 }
41 
42 /*-------------------------------------------------------------------------*/
43 
44 /* interface descriptor: */
45 
46 static struct usb_interface_descriptor gser_interface_desc = {
47 	.bLength =		USB_DT_INTERFACE_SIZE,
48 	.bDescriptorType =	USB_DT_INTERFACE,
49 	/* .bInterfaceNumber = DYNAMIC */
50 	.bNumEndpoints =	2,
51 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
52 	.bInterfaceSubClass =	0,
53 	.bInterfaceProtocol =	0,
54 	/* .iInterface = DYNAMIC */
55 };
56 
57 /* full speed support: */
58 
59 static struct usb_endpoint_descriptor gser_fs_in_desc = {
60 	.bLength =		USB_DT_ENDPOINT_SIZE,
61 	.bDescriptorType =	USB_DT_ENDPOINT,
62 	.bEndpointAddress =	USB_DIR_IN,
63 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
64 };
65 
66 static struct usb_endpoint_descriptor gser_fs_out_desc = {
67 	.bLength =		USB_DT_ENDPOINT_SIZE,
68 	.bDescriptorType =	USB_DT_ENDPOINT,
69 	.bEndpointAddress =	USB_DIR_OUT,
70 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
71 };
72 
73 static struct usb_descriptor_header *gser_fs_function[] = {
74 	(struct usb_descriptor_header *) &gser_interface_desc,
75 	(struct usb_descriptor_header *) &gser_fs_in_desc,
76 	(struct usb_descriptor_header *) &gser_fs_out_desc,
77 	NULL,
78 };
79 
80 /* high speed support: */
81 
82 static struct usb_endpoint_descriptor gser_hs_in_desc = {
83 	.bLength =		USB_DT_ENDPOINT_SIZE,
84 	.bDescriptorType =	USB_DT_ENDPOINT,
85 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
86 	.wMaxPacketSize =	cpu_to_le16(512),
87 };
88 
89 static struct usb_endpoint_descriptor gser_hs_out_desc = {
90 	.bLength =		USB_DT_ENDPOINT_SIZE,
91 	.bDescriptorType =	USB_DT_ENDPOINT,
92 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
93 	.wMaxPacketSize =	cpu_to_le16(512),
94 };
95 
96 static struct usb_descriptor_header *gser_hs_function[] = {
97 	(struct usb_descriptor_header *) &gser_interface_desc,
98 	(struct usb_descriptor_header *) &gser_hs_in_desc,
99 	(struct usb_descriptor_header *) &gser_hs_out_desc,
100 	NULL,
101 };
102 
103 static struct usb_endpoint_descriptor gser_ss_in_desc = {
104 	.bLength =		USB_DT_ENDPOINT_SIZE,
105 	.bDescriptorType =	USB_DT_ENDPOINT,
106 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
107 	.wMaxPacketSize =	cpu_to_le16(1024),
108 };
109 
110 static struct usb_endpoint_descriptor gser_ss_out_desc = {
111 	.bLength =		USB_DT_ENDPOINT_SIZE,
112 	.bDescriptorType =	USB_DT_ENDPOINT,
113 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
114 	.wMaxPacketSize =	cpu_to_le16(1024),
115 };
116 
117 static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
118 	.bLength =              sizeof gser_ss_bulk_comp_desc,
119 	.bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
120 };
121 
122 static struct usb_descriptor_header *gser_ss_function[] = {
123 	(struct usb_descriptor_header *) &gser_interface_desc,
124 	(struct usb_descriptor_header *) &gser_ss_in_desc,
125 	(struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
126 	(struct usb_descriptor_header *) &gser_ss_out_desc,
127 	(struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
128 	NULL,
129 };
130 
131 /* string descriptors: */
132 
133 static struct usb_string gser_string_defs[] = {
134 	[0].s = "Generic Serial",
135 	{  } /* end of list */
136 };
137 
138 static struct usb_gadget_strings gser_string_table = {
139 	.language =		0x0409,	/* en-us */
140 	.strings =		gser_string_defs,
141 };
142 
143 static struct usb_gadget_strings *gser_strings[] = {
144 	&gser_string_table,
145 	NULL,
146 };
147 
148 /*-------------------------------------------------------------------------*/
149 
150 static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
151 {
152 	struct f_gser		*gser = func_to_gser(f);
153 	struct usb_composite_dev *cdev = f->config->cdev;
154 
155 	/* we know alt == 0, so this is an activation or a reset */
156 
157 	if (gser->port.in->enabled) {
158 		dev_dbg(&cdev->gadget->dev,
159 			"reset generic ttyGS%d\n", gser->port_num);
160 		gserial_disconnect(&gser->port);
161 	}
162 	if (!gser->port.in->desc || !gser->port.out->desc) {
163 		dev_dbg(&cdev->gadget->dev,
164 			"activate generic ttyGS%d\n", gser->port_num);
165 		if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
166 		    config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
167 			gser->port.in->desc = NULL;
168 			gser->port.out->desc = NULL;
169 			return -EINVAL;
170 		}
171 	}
172 	gserial_connect(&gser->port, gser->port_num);
173 	return 0;
174 }
175 
176 static void gser_disable(struct usb_function *f)
177 {
178 	struct f_gser	*gser = func_to_gser(f);
179 	struct usb_composite_dev *cdev = f->config->cdev;
180 
181 	dev_dbg(&cdev->gadget->dev,
182 		"generic ttyGS%d deactivated\n", gser->port_num);
183 	gserial_disconnect(&gser->port);
184 }
185 
186 /*-------------------------------------------------------------------------*/
187 
188 /* serial function driver setup/binding */
189 
190 static int gser_bind(struct usb_configuration *c, struct usb_function *f)
191 {
192 	struct usb_composite_dev *cdev = c->cdev;
193 	struct f_gser		*gser = func_to_gser(f);
194 	int			status;
195 	struct usb_ep		*ep;
196 
197 	/* REVISIT might want instance-specific strings to help
198 	 * distinguish instances ...
199 	 */
200 
201 	/* maybe allocate device-global string ID */
202 	if (gser_string_defs[0].id == 0) {
203 		status = usb_string_id(c->cdev);
204 		if (status < 0)
205 			return status;
206 		gser_string_defs[0].id = status;
207 	}
208 
209 	/* allocate instance-specific interface IDs */
210 	status = usb_interface_id(c, f);
211 	if (status < 0)
212 		goto fail;
213 	gser->data_id = status;
214 	gser_interface_desc.bInterfaceNumber = status;
215 
216 	status = -ENODEV;
217 
218 	/* allocate instance-specific endpoints */
219 	ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
220 	if (!ep)
221 		goto fail;
222 	gser->port.in = ep;
223 
224 	ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
225 	if (!ep)
226 		goto fail;
227 	gser->port.out = ep;
228 
229 	/* support all relevant hardware speeds... we expect that when
230 	 * hardware is dual speed, all bulk-capable endpoints work at
231 	 * both speeds
232 	 */
233 	gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
234 	gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
235 
236 	gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
237 	gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
238 
239 	status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
240 			gser_ss_function, NULL);
241 	if (status)
242 		goto fail;
243 	dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
244 		gser->port_num,
245 		gadget_is_superspeed(c->cdev->gadget) ? "super" :
246 		gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
247 		gser->port.in->name, gser->port.out->name);
248 	return 0;
249 
250 fail:
251 	ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
252 
253 	return status;
254 }
255 
256 static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
257 {
258 	return container_of(to_config_group(item), struct f_serial_opts,
259 			    func_inst.group);
260 }
261 
262 static void serial_attr_release(struct config_item *item)
263 {
264 	struct f_serial_opts *opts = to_f_serial_opts(item);
265 
266 	usb_put_function_instance(&opts->func_inst);
267 }
268 
269 static struct configfs_item_operations serial_item_ops = {
270 	.release	= serial_attr_release,
271 };
272 
273 static ssize_t f_serial_port_num_show(struct config_item *item, char *page)
274 {
275 	return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
276 }
277 
278 CONFIGFS_ATTR_RO(f_serial_, port_num);
279 
280 static struct configfs_attribute *acm_attrs[] = {
281 	&f_serial_attr_port_num,
282 	NULL,
283 };
284 
285 static struct config_item_type serial_func_type = {
286 	.ct_item_ops	= &serial_item_ops,
287 	.ct_attrs	= acm_attrs,
288 	.ct_owner	= THIS_MODULE,
289 };
290 
291 static void gser_free_inst(struct usb_function_instance *f)
292 {
293 	struct f_serial_opts *opts;
294 
295 	opts = container_of(f, struct f_serial_opts, func_inst);
296 	gserial_free_line(opts->port_num);
297 	kfree(opts);
298 }
299 
300 static struct usb_function_instance *gser_alloc_inst(void)
301 {
302 	struct f_serial_opts *opts;
303 	int ret;
304 
305 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
306 	if (!opts)
307 		return ERR_PTR(-ENOMEM);
308 
309 	opts->func_inst.free_func_inst = gser_free_inst;
310 	ret = gserial_alloc_line(&opts->port_num);
311 	if (ret) {
312 		kfree(opts);
313 		return ERR_PTR(ret);
314 	}
315 	config_group_init_type_name(&opts->func_inst.group, "",
316 				    &serial_func_type);
317 
318 	return &opts->func_inst;
319 }
320 
321 static void gser_free(struct usb_function *f)
322 {
323 	struct f_gser *serial;
324 
325 	serial = func_to_gser(f);
326 	kfree(serial);
327 }
328 
329 static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
330 {
331 	usb_free_all_descriptors(f);
332 }
333 
334 static struct usb_function *gser_alloc(struct usb_function_instance *fi)
335 {
336 	struct f_gser	*gser;
337 	struct f_serial_opts *opts;
338 
339 	/* allocate and initialize one new instance */
340 	gser = kzalloc(sizeof(*gser), GFP_KERNEL);
341 	if (!gser)
342 		return ERR_PTR(-ENOMEM);
343 
344 	opts = container_of(fi, struct f_serial_opts, func_inst);
345 
346 	gser->port_num = opts->port_num;
347 
348 	gser->port.func.name = "gser";
349 	gser->port.func.strings = gser_strings;
350 	gser->port.func.bind = gser_bind;
351 	gser->port.func.unbind = gser_unbind;
352 	gser->port.func.set_alt = gser_set_alt;
353 	gser->port.func.disable = gser_disable;
354 	gser->port.func.free_func = gser_free;
355 
356 	return &gser->port.func;
357 }
358 
359 DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
360 MODULE_LICENSE("GPL");
361 MODULE_AUTHOR("Al Borchers");
362 MODULE_AUTHOR("David Brownell");
363