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