Linux Audio

Check our new training course

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