Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * f_obex.c -- USB CDC OBEX function driver
  4 *
  5 * Copyright (C) 2008 Nokia Corporation
  6 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  7 *
  8 * Based on f_acm.c by Al Borchers and David Brownell.
 
 
 
 
 
  9 */
 10
 11/* #define VERBOSE_DEBUG */
 12
 13#include <linux/slab.h>
 14#include <linux/kernel.h>
 15#include <linux/device.h>
 16#include <linux/module.h>
 17
 18#include "u_serial.h"
 19
 20
 21/*
 22 * This CDC OBEX function support just packages a TTY-ish byte stream.
 23 * A user mode server will put it into "raw" mode and handle all the
 24 * relevant protocol details ... this is just a kernel passthrough.
 25 * When possible, we prevent gadget enumeration until that server is
 26 * ready to handle the commands.
 27 */
 28
 29struct f_obex {
 30	struct gserial			port;
 31	u8				ctrl_id;
 32	u8				data_id;
 33	u8				cur_alt;
 34	u8				port_num;
 35};
 36
 37static inline struct f_obex *func_to_obex(struct usb_function *f)
 38{
 39	return container_of(f, struct f_obex, port.func);
 40}
 41
 42static inline struct f_obex *port_to_obex(struct gserial *p)
 43{
 44	return container_of(p, struct f_obex, port);
 45}
 46
 47/*-------------------------------------------------------------------------*/
 48
 49#define OBEX_CTRL_IDX	0
 50#define OBEX_DATA_IDX	1
 51
 52static struct usb_string obex_string_defs[] = {
 53	[OBEX_CTRL_IDX].s	= "CDC Object Exchange (OBEX)",
 54	[OBEX_DATA_IDX].s	= "CDC OBEX Data",
 55	{  },	/* end of list */
 56};
 57
 58static struct usb_gadget_strings obex_string_table = {
 59	.language		= 0x0409,	/* en-US */
 60	.strings		= obex_string_defs,
 61};
 62
 63static struct usb_gadget_strings *obex_strings[] = {
 64	&obex_string_table,
 65	NULL,
 66};
 67
 68/*-------------------------------------------------------------------------*/
 69
 70static struct usb_interface_descriptor obex_control_intf = {
 71	.bLength		= sizeof(obex_control_intf),
 72	.bDescriptorType	= USB_DT_INTERFACE,
 73	.bInterfaceNumber	= 0,
 74
 75	.bAlternateSetting	= 0,
 76	.bNumEndpoints		= 0,
 77	.bInterfaceClass	= USB_CLASS_COMM,
 78	.bInterfaceSubClass	= USB_CDC_SUBCLASS_OBEX,
 79};
 80
 81static struct usb_interface_descriptor obex_data_nop_intf = {
 82	.bLength		= sizeof(obex_data_nop_intf),
 83	.bDescriptorType	= USB_DT_INTERFACE,
 84	.bInterfaceNumber	= 1,
 85
 86	.bAlternateSetting	= 0,
 87	.bNumEndpoints		= 0,
 88	.bInterfaceClass	= USB_CLASS_CDC_DATA,
 89};
 90
 91static struct usb_interface_descriptor obex_data_intf = {
 92	.bLength		= sizeof(obex_data_intf),
 93	.bDescriptorType	= USB_DT_INTERFACE,
 94	.bInterfaceNumber	= 2,
 95
 96	.bAlternateSetting	= 1,
 97	.bNumEndpoints		= 2,
 98	.bInterfaceClass	= USB_CLASS_CDC_DATA,
 99};
100
101static struct usb_cdc_header_desc obex_cdc_header_desc = {
102	.bLength		= sizeof(obex_cdc_header_desc),
103	.bDescriptorType	= USB_DT_CS_INTERFACE,
104	.bDescriptorSubType	= USB_CDC_HEADER_TYPE,
105	.bcdCDC			= cpu_to_le16(0x0120),
106};
107
108static struct usb_cdc_union_desc obex_cdc_union_desc = {
109	.bLength		= sizeof(obex_cdc_union_desc),
110	.bDescriptorType	= USB_DT_CS_INTERFACE,
111	.bDescriptorSubType	= USB_CDC_UNION_TYPE,
112	.bMasterInterface0	= 1,
113	.bSlaveInterface0	= 2,
114};
115
116static struct usb_cdc_obex_desc obex_desc = {
117	.bLength		= sizeof(obex_desc),
118	.bDescriptorType	= USB_DT_CS_INTERFACE,
119	.bDescriptorSubType	= USB_CDC_OBEX_TYPE,
120	.bcdVersion		= cpu_to_le16(0x0100),
121};
122
123/* High-Speed Support */
124
125static struct usb_endpoint_descriptor obex_hs_ep_out_desc = {
126	.bLength		= USB_DT_ENDPOINT_SIZE,
127	.bDescriptorType	= USB_DT_ENDPOINT,
128
129	.bEndpointAddress	= USB_DIR_OUT,
130	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
131	.wMaxPacketSize		= cpu_to_le16(512),
132};
133
134static struct usb_endpoint_descriptor obex_hs_ep_in_desc = {
135	.bLength		= USB_DT_ENDPOINT_SIZE,
136	.bDescriptorType	= USB_DT_ENDPOINT,
137
138	.bEndpointAddress	= USB_DIR_IN,
139	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
140	.wMaxPacketSize		= cpu_to_le16(512),
141};
142
143static struct usb_descriptor_header *hs_function[] = {
144	(struct usb_descriptor_header *) &obex_control_intf,
145	(struct usb_descriptor_header *) &obex_cdc_header_desc,
146	(struct usb_descriptor_header *) &obex_desc,
147	(struct usb_descriptor_header *) &obex_cdc_union_desc,
148
149	(struct usb_descriptor_header *) &obex_data_nop_intf,
150	(struct usb_descriptor_header *) &obex_data_intf,
151	(struct usb_descriptor_header *) &obex_hs_ep_in_desc,
152	(struct usb_descriptor_header *) &obex_hs_ep_out_desc,
153	NULL,
154};
155
156/* Full-Speed Support */
157
158static struct usb_endpoint_descriptor obex_fs_ep_in_desc = {
159	.bLength		= USB_DT_ENDPOINT_SIZE,
160	.bDescriptorType	= USB_DT_ENDPOINT,
161
162	.bEndpointAddress	= USB_DIR_IN,
163	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
164};
165
166static struct usb_endpoint_descriptor obex_fs_ep_out_desc = {
167	.bLength		= USB_DT_ENDPOINT_SIZE,
168	.bDescriptorType	= USB_DT_ENDPOINT,
169
170	.bEndpointAddress	= USB_DIR_OUT,
171	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
172};
173
174static struct usb_descriptor_header *fs_function[] = {
175	(struct usb_descriptor_header *) &obex_control_intf,
176	(struct usb_descriptor_header *) &obex_cdc_header_desc,
177	(struct usb_descriptor_header *) &obex_desc,
178	(struct usb_descriptor_header *) &obex_cdc_union_desc,
179
180	(struct usb_descriptor_header *) &obex_data_nop_intf,
181	(struct usb_descriptor_header *) &obex_data_intf,
182	(struct usb_descriptor_header *) &obex_fs_ep_in_desc,
183	(struct usb_descriptor_header *) &obex_fs_ep_out_desc,
184	NULL,
185};
186
187/*-------------------------------------------------------------------------*/
188
189static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
190{
191	struct f_obex		*obex = func_to_obex(f);
192	struct usb_composite_dev *cdev = f->config->cdev;
193
194	if (intf == obex->ctrl_id) {
195		if (alt != 0)
196			goto fail;
197		/* NOP */
198		dev_dbg(&cdev->gadget->dev,
199			"reset obex ttyGS%d control\n", obex->port_num);
200
201	} else if (intf == obex->data_id) {
202		if (alt > 1)
203			goto fail;
204
205		if (obex->port.in->enabled) {
206			dev_dbg(&cdev->gadget->dev,
207				"reset obex ttyGS%d\n", obex->port_num);
208			gserial_disconnect(&obex->port);
209		}
210
211		if (!obex->port.in->desc || !obex->port.out->desc) {
212			dev_dbg(&cdev->gadget->dev,
213				"init obex ttyGS%d\n", obex->port_num);
214			if (config_ep_by_speed(cdev->gadget, f,
215					       obex->port.in) ||
216			    config_ep_by_speed(cdev->gadget, f,
217					       obex->port.out)) {
218				obex->port.out->desc = NULL;
219				obex->port.in->desc = NULL;
220				goto fail;
221			}
222		}
223
224		if (alt == 1) {
225			dev_dbg(&cdev->gadget->dev,
226				"activate obex ttyGS%d\n", obex->port_num);
227			gserial_connect(&obex->port, obex->port_num);
228		}
229
230	} else
231		goto fail;
232
233	obex->cur_alt = alt;
234
235	return 0;
236
237fail:
238	return -EINVAL;
239}
240
241static int obex_get_alt(struct usb_function *f, unsigned intf)
242{
243	struct f_obex		*obex = func_to_obex(f);
244
245	return obex->cur_alt;
246}
247
248static void obex_disable(struct usb_function *f)
249{
250	struct f_obex	*obex = func_to_obex(f);
251	struct usb_composite_dev *cdev = f->config->cdev;
252
253	dev_dbg(&cdev->gadget->dev, "obex ttyGS%d disable\n", obex->port_num);
254	gserial_disconnect(&obex->port);
255}
256
257/*-------------------------------------------------------------------------*/
258
259static void obex_connect(struct gserial *g)
260{
261	struct f_obex		*obex = port_to_obex(g);
262	struct usb_composite_dev *cdev = g->func.config->cdev;
263	int			status;
264
265	status = usb_function_activate(&g->func);
266	if (status)
267		dev_dbg(&cdev->gadget->dev,
268			"obex ttyGS%d function activate --> %d\n",
269			obex->port_num, status);
270}
271
272static void obex_disconnect(struct gserial *g)
273{
274	struct f_obex		*obex = port_to_obex(g);
275	struct usb_composite_dev *cdev = g->func.config->cdev;
276	int			status;
277
278	status = usb_function_deactivate(&g->func);
279	if (status)
280		dev_dbg(&cdev->gadget->dev,
281			"obex ttyGS%d function deactivate --> %d\n",
282			obex->port_num, status);
283}
284
285/*-------------------------------------------------------------------------*/
286
287/* Some controllers can't support CDC OBEX ... */
288static inline bool can_support_obex(struct usb_configuration *c)
289{
290	/* Since the first interface is a NOP, we can ignore the
291	 * issue of multi-interface support on most controllers.
292	 *
293	 * Altsettings are mandatory, however...
294	 */
295	if (!gadget_is_altset_supported(c->cdev->gadget))
296		return false;
297
298	/* everything else is *probably* fine ... */
299	return true;
300}
301
302static int obex_bind(struct usb_configuration *c, struct usb_function *f)
303{
304	struct usb_composite_dev *cdev = c->cdev;
305	struct f_obex		*obex = func_to_obex(f);
306	struct usb_string	*us;
307	int			status;
308	struct usb_ep		*ep;
309
310	if (!can_support_obex(c))
311		return -EINVAL;
312
313	us = usb_gstrings_attach(cdev, obex_strings,
314				 ARRAY_SIZE(obex_string_defs));
315	if (IS_ERR(us))
316		return PTR_ERR(us);
317	obex_control_intf.iInterface = us[OBEX_CTRL_IDX].id;
318	obex_data_nop_intf.iInterface = us[OBEX_DATA_IDX].id;
319	obex_data_intf.iInterface = us[OBEX_DATA_IDX].id;
320
321	/* allocate instance-specific interface IDs, and patch descriptors */
322
323	status = usb_interface_id(c, f);
324	if (status < 0)
325		goto fail;
326	obex->ctrl_id = status;
327
328	obex_control_intf.bInterfaceNumber = status;
329	obex_cdc_union_desc.bMasterInterface0 = status;
330
331	status = usb_interface_id(c, f);
332	if (status < 0)
333		goto fail;
334	obex->data_id = status;
335
336	obex_data_nop_intf.bInterfaceNumber = status;
337	obex_data_intf.bInterfaceNumber = status;
338	obex_cdc_union_desc.bSlaveInterface0 = status;
339
340	/* allocate instance-specific endpoints */
341
342	status = -ENODEV;
343	ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
344	if (!ep)
345		goto fail;
346	obex->port.in = ep;
347
348	ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
349	if (!ep)
350		goto fail;
351	obex->port.out = ep;
352
353	/* support all relevant hardware speeds... we expect that when
354	 * hardware is dual speed, all bulk-capable endpoints work at
355	 * both speeds
356	 */
357
358	obex_hs_ep_in_desc.bEndpointAddress =
359		obex_fs_ep_in_desc.bEndpointAddress;
360	obex_hs_ep_out_desc.bEndpointAddress =
361		obex_fs_ep_out_desc.bEndpointAddress;
362
363	status = usb_assign_descriptors(f, fs_function, hs_function, NULL,
364					NULL);
365	if (status)
366		goto fail;
367
368	dev_dbg(&cdev->gadget->dev, "obex ttyGS%d: IN/%s OUT/%s\n",
369		obex->port_num,
 
370		obex->port.in->name, obex->port.out->name);
371
372	return 0;
373
374fail:
375	ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
376
377	return status;
378}
379
380static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
381{
382	return container_of(to_config_group(item), struct f_serial_opts,
383			    func_inst.group);
384}
385
386static void obex_attr_release(struct config_item *item)
387{
388	struct f_serial_opts *opts = to_f_serial_opts(item);
389
390	usb_put_function_instance(&opts->func_inst);
391}
392
393static struct configfs_item_operations obex_item_ops = {
394	.release	= obex_attr_release,
395};
396
397static ssize_t f_obex_port_num_show(struct config_item *item, char *page)
398{
399	return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
400}
401
402CONFIGFS_ATTR_RO(f_obex_, port_num);
403
404static struct configfs_attribute *acm_attrs[] = {
405	&f_obex_attr_port_num,
406	NULL,
407};
408
409static const struct config_item_type obex_func_type = {
410	.ct_item_ops	= &obex_item_ops,
411	.ct_attrs	= acm_attrs,
412	.ct_owner	= THIS_MODULE,
413};
414
415static void obex_free_inst(struct usb_function_instance *f)
416{
417	struct f_serial_opts *opts;
418
419	opts = container_of(f, struct f_serial_opts, func_inst);
420	gserial_free_line(opts->port_num);
421	kfree(opts);
422}
423
424static struct usb_function_instance *obex_alloc_inst(void)
425{
426	struct f_serial_opts *opts;
427	int ret;
428
429	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
430	if (!opts)
431		return ERR_PTR(-ENOMEM);
432
433	opts->func_inst.free_func_inst = obex_free_inst;
434	ret = gserial_alloc_line_no_console(&opts->port_num);
435	if (ret) {
436		kfree(opts);
437		return ERR_PTR(ret);
438	}
439	config_group_init_type_name(&opts->func_inst.group, "",
440				    &obex_func_type);
441
442	return &opts->func_inst;
443}
444
445static void obex_free(struct usb_function *f)
446{
447	struct f_obex *obex;
448
449	obex = func_to_obex(f);
450	kfree(obex);
451}
452
453static void obex_unbind(struct usb_configuration *c, struct usb_function *f)
454{
455	usb_free_all_descriptors(f);
456}
457
458static struct usb_function *obex_alloc(struct usb_function_instance *fi)
459{
460	struct f_obex	*obex;
461	struct f_serial_opts *opts;
462
463	/* allocate and initialize one new instance */
464	obex = kzalloc(sizeof(*obex), GFP_KERNEL);
465	if (!obex)
466		return ERR_PTR(-ENOMEM);
467
468	opts = container_of(fi, struct f_serial_opts, func_inst);
469
470	obex->port_num = opts->port_num;
471
472	obex->port.connect = obex_connect;
473	obex->port.disconnect = obex_disconnect;
474
475	obex->port.func.name = "obex";
476	/* descriptors are per-instance copies */
477	obex->port.func.bind = obex_bind;
478	obex->port.func.unbind = obex_unbind;
479	obex->port.func.set_alt = obex_set_alt;
480	obex->port.func.get_alt = obex_get_alt;
481	obex->port.func.disable = obex_disable;
482	obex->port.func.free_func = obex_free;
483	obex->port.func.bind_deactivated = true;
484
485	return &obex->port.func;
486}
487
488DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc);
489MODULE_AUTHOR("Felipe Balbi");
490MODULE_LICENSE("GPL");
v4.10.11
 
  1/*
  2 * f_obex.c -- USB CDC OBEX function driver
  3 *
  4 * Copyright (C) 2008 Nokia Corporation
  5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  6 *
  7 * Based on f_acm.c by Al Borchers and David Brownell.
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 2 of the License, or
 12 * (at your option) any later version.
 13 */
 14
 15/* #define VERBOSE_DEBUG */
 16
 17#include <linux/slab.h>
 18#include <linux/kernel.h>
 19#include <linux/device.h>
 20#include <linux/module.h>
 21
 22#include "u_serial.h"
 23
 24
 25/*
 26 * This CDC OBEX function support just packages a TTY-ish byte stream.
 27 * A user mode server will put it into "raw" mode and handle all the
 28 * relevant protocol details ... this is just a kernel passthrough.
 29 * When possible, we prevent gadget enumeration until that server is
 30 * ready to handle the commands.
 31 */
 32
 33struct f_obex {
 34	struct gserial			port;
 35	u8				ctrl_id;
 36	u8				data_id;
 37	u8				cur_alt;
 38	u8				port_num;
 39};
 40
 41static inline struct f_obex *func_to_obex(struct usb_function *f)
 42{
 43	return container_of(f, struct f_obex, port.func);
 44}
 45
 46static inline struct f_obex *port_to_obex(struct gserial *p)
 47{
 48	return container_of(p, struct f_obex, port);
 49}
 50
 51/*-------------------------------------------------------------------------*/
 52
 53#define OBEX_CTRL_IDX	0
 54#define OBEX_DATA_IDX	1
 55
 56static struct usb_string obex_string_defs[] = {
 57	[OBEX_CTRL_IDX].s	= "CDC Object Exchange (OBEX)",
 58	[OBEX_DATA_IDX].s	= "CDC OBEX Data",
 59	{  },	/* end of list */
 60};
 61
 62static struct usb_gadget_strings obex_string_table = {
 63	.language		= 0x0409,	/* en-US */
 64	.strings		= obex_string_defs,
 65};
 66
 67static struct usb_gadget_strings *obex_strings[] = {
 68	&obex_string_table,
 69	NULL,
 70};
 71
 72/*-------------------------------------------------------------------------*/
 73
 74static struct usb_interface_descriptor obex_control_intf = {
 75	.bLength		= sizeof(obex_control_intf),
 76	.bDescriptorType	= USB_DT_INTERFACE,
 77	.bInterfaceNumber	= 0,
 78
 79	.bAlternateSetting	= 0,
 80	.bNumEndpoints		= 0,
 81	.bInterfaceClass	= USB_CLASS_COMM,
 82	.bInterfaceSubClass	= USB_CDC_SUBCLASS_OBEX,
 83};
 84
 85static struct usb_interface_descriptor obex_data_nop_intf = {
 86	.bLength		= sizeof(obex_data_nop_intf),
 87	.bDescriptorType	= USB_DT_INTERFACE,
 88	.bInterfaceNumber	= 1,
 89
 90	.bAlternateSetting	= 0,
 91	.bNumEndpoints		= 0,
 92	.bInterfaceClass	= USB_CLASS_CDC_DATA,
 93};
 94
 95static struct usb_interface_descriptor obex_data_intf = {
 96	.bLength		= sizeof(obex_data_intf),
 97	.bDescriptorType	= USB_DT_INTERFACE,
 98	.bInterfaceNumber	= 2,
 99
100	.bAlternateSetting	= 1,
101	.bNumEndpoints		= 2,
102	.bInterfaceClass	= USB_CLASS_CDC_DATA,
103};
104
105static struct usb_cdc_header_desc obex_cdc_header_desc = {
106	.bLength		= sizeof(obex_cdc_header_desc),
107	.bDescriptorType	= USB_DT_CS_INTERFACE,
108	.bDescriptorSubType	= USB_CDC_HEADER_TYPE,
109	.bcdCDC			= cpu_to_le16(0x0120),
110};
111
112static struct usb_cdc_union_desc obex_cdc_union_desc = {
113	.bLength		= sizeof(obex_cdc_union_desc),
114	.bDescriptorType	= USB_DT_CS_INTERFACE,
115	.bDescriptorSubType	= USB_CDC_UNION_TYPE,
116	.bMasterInterface0	= 1,
117	.bSlaveInterface0	= 2,
118};
119
120static struct usb_cdc_obex_desc obex_desc = {
121	.bLength		= sizeof(obex_desc),
122	.bDescriptorType	= USB_DT_CS_INTERFACE,
123	.bDescriptorSubType	= USB_CDC_OBEX_TYPE,
124	.bcdVersion		= cpu_to_le16(0x0100),
125};
126
127/* High-Speed Support */
128
129static struct usb_endpoint_descriptor obex_hs_ep_out_desc = {
130	.bLength		= USB_DT_ENDPOINT_SIZE,
131	.bDescriptorType	= USB_DT_ENDPOINT,
132
133	.bEndpointAddress	= USB_DIR_OUT,
134	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
135	.wMaxPacketSize		= cpu_to_le16(512),
136};
137
138static struct usb_endpoint_descriptor obex_hs_ep_in_desc = {
139	.bLength		= USB_DT_ENDPOINT_SIZE,
140	.bDescriptorType	= USB_DT_ENDPOINT,
141
142	.bEndpointAddress	= USB_DIR_IN,
143	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
144	.wMaxPacketSize		= cpu_to_le16(512),
145};
146
147static struct usb_descriptor_header *hs_function[] = {
148	(struct usb_descriptor_header *) &obex_control_intf,
149	(struct usb_descriptor_header *) &obex_cdc_header_desc,
150	(struct usb_descriptor_header *) &obex_desc,
151	(struct usb_descriptor_header *) &obex_cdc_union_desc,
152
153	(struct usb_descriptor_header *) &obex_data_nop_intf,
154	(struct usb_descriptor_header *) &obex_data_intf,
155	(struct usb_descriptor_header *) &obex_hs_ep_in_desc,
156	(struct usb_descriptor_header *) &obex_hs_ep_out_desc,
157	NULL,
158};
159
160/* Full-Speed Support */
161
162static struct usb_endpoint_descriptor obex_fs_ep_in_desc = {
163	.bLength		= USB_DT_ENDPOINT_SIZE,
164	.bDescriptorType	= USB_DT_ENDPOINT,
165
166	.bEndpointAddress	= USB_DIR_IN,
167	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
168};
169
170static struct usb_endpoint_descriptor obex_fs_ep_out_desc = {
171	.bLength		= USB_DT_ENDPOINT_SIZE,
172	.bDescriptorType	= USB_DT_ENDPOINT,
173
174	.bEndpointAddress	= USB_DIR_OUT,
175	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
176};
177
178static struct usb_descriptor_header *fs_function[] = {
179	(struct usb_descriptor_header *) &obex_control_intf,
180	(struct usb_descriptor_header *) &obex_cdc_header_desc,
181	(struct usb_descriptor_header *) &obex_desc,
182	(struct usb_descriptor_header *) &obex_cdc_union_desc,
183
184	(struct usb_descriptor_header *) &obex_data_nop_intf,
185	(struct usb_descriptor_header *) &obex_data_intf,
186	(struct usb_descriptor_header *) &obex_fs_ep_in_desc,
187	(struct usb_descriptor_header *) &obex_fs_ep_out_desc,
188	NULL,
189};
190
191/*-------------------------------------------------------------------------*/
192
193static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
194{
195	struct f_obex		*obex = func_to_obex(f);
196	struct usb_composite_dev *cdev = f->config->cdev;
197
198	if (intf == obex->ctrl_id) {
199		if (alt != 0)
200			goto fail;
201		/* NOP */
202		dev_dbg(&cdev->gadget->dev,
203			"reset obex ttyGS%d control\n", obex->port_num);
204
205	} else if (intf == obex->data_id) {
206		if (alt > 1)
207			goto fail;
208
209		if (obex->port.in->enabled) {
210			dev_dbg(&cdev->gadget->dev,
211				"reset obex ttyGS%d\n", obex->port_num);
212			gserial_disconnect(&obex->port);
213		}
214
215		if (!obex->port.in->desc || !obex->port.out->desc) {
216			dev_dbg(&cdev->gadget->dev,
217				"init obex ttyGS%d\n", obex->port_num);
218			if (config_ep_by_speed(cdev->gadget, f,
219					       obex->port.in) ||
220			    config_ep_by_speed(cdev->gadget, f,
221					       obex->port.out)) {
222				obex->port.out->desc = NULL;
223				obex->port.in->desc = NULL;
224				goto fail;
225			}
226		}
227
228		if (alt == 1) {
229			dev_dbg(&cdev->gadget->dev,
230				"activate obex ttyGS%d\n", obex->port_num);
231			gserial_connect(&obex->port, obex->port_num);
232		}
233
234	} else
235		goto fail;
236
237	obex->cur_alt = alt;
238
239	return 0;
240
241fail:
242	return -EINVAL;
243}
244
245static int obex_get_alt(struct usb_function *f, unsigned intf)
246{
247	struct f_obex		*obex = func_to_obex(f);
248
249	return obex->cur_alt;
250}
251
252static void obex_disable(struct usb_function *f)
253{
254	struct f_obex	*obex = func_to_obex(f);
255	struct usb_composite_dev *cdev = f->config->cdev;
256
257	dev_dbg(&cdev->gadget->dev, "obex ttyGS%d disable\n", obex->port_num);
258	gserial_disconnect(&obex->port);
259}
260
261/*-------------------------------------------------------------------------*/
262
263static void obex_connect(struct gserial *g)
264{
265	struct f_obex		*obex = port_to_obex(g);
266	struct usb_composite_dev *cdev = g->func.config->cdev;
267	int			status;
268
269	status = usb_function_activate(&g->func);
270	if (status)
271		dev_dbg(&cdev->gadget->dev,
272			"obex ttyGS%d function activate --> %d\n",
273			obex->port_num, status);
274}
275
276static void obex_disconnect(struct gserial *g)
277{
278	struct f_obex		*obex = port_to_obex(g);
279	struct usb_composite_dev *cdev = g->func.config->cdev;
280	int			status;
281
282	status = usb_function_deactivate(&g->func);
283	if (status)
284		dev_dbg(&cdev->gadget->dev,
285			"obex ttyGS%d function deactivate --> %d\n",
286			obex->port_num, status);
287}
288
289/*-------------------------------------------------------------------------*/
290
291/* Some controllers can't support CDC OBEX ... */
292static inline bool can_support_obex(struct usb_configuration *c)
293{
294	/* Since the first interface is a NOP, we can ignore the
295	 * issue of multi-interface support on most controllers.
296	 *
297	 * Altsettings are mandatory, however...
298	 */
299	if (!gadget_is_altset_supported(c->cdev->gadget))
300		return false;
301
302	/* everything else is *probably* fine ... */
303	return true;
304}
305
306static int obex_bind(struct usb_configuration *c, struct usb_function *f)
307{
308	struct usb_composite_dev *cdev = c->cdev;
309	struct f_obex		*obex = func_to_obex(f);
310	struct usb_string	*us;
311	int			status;
312	struct usb_ep		*ep;
313
314	if (!can_support_obex(c))
315		return -EINVAL;
316
317	us = usb_gstrings_attach(cdev, obex_strings,
318				 ARRAY_SIZE(obex_string_defs));
319	if (IS_ERR(us))
320		return PTR_ERR(us);
321	obex_control_intf.iInterface = us[OBEX_CTRL_IDX].id;
322	obex_data_nop_intf.iInterface = us[OBEX_DATA_IDX].id;
323	obex_data_intf.iInterface = us[OBEX_DATA_IDX].id;
324
325	/* allocate instance-specific interface IDs, and patch descriptors */
326
327	status = usb_interface_id(c, f);
328	if (status < 0)
329		goto fail;
330	obex->ctrl_id = status;
331
332	obex_control_intf.bInterfaceNumber = status;
333	obex_cdc_union_desc.bMasterInterface0 = status;
334
335	status = usb_interface_id(c, f);
336	if (status < 0)
337		goto fail;
338	obex->data_id = status;
339
340	obex_data_nop_intf.bInterfaceNumber = status;
341	obex_data_intf.bInterfaceNumber = status;
342	obex_cdc_union_desc.bSlaveInterface0 = status;
343
344	/* allocate instance-specific endpoints */
345
346	status = -ENODEV;
347	ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
348	if (!ep)
349		goto fail;
350	obex->port.in = ep;
351
352	ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
353	if (!ep)
354		goto fail;
355	obex->port.out = ep;
356
357	/* support all relevant hardware speeds... we expect that when
358	 * hardware is dual speed, all bulk-capable endpoints work at
359	 * both speeds
360	 */
361
362	obex_hs_ep_in_desc.bEndpointAddress =
363		obex_fs_ep_in_desc.bEndpointAddress;
364	obex_hs_ep_out_desc.bEndpointAddress =
365		obex_fs_ep_out_desc.bEndpointAddress;
366
367	status = usb_assign_descriptors(f, fs_function, hs_function, NULL,
368					NULL);
369	if (status)
370		goto fail;
371
372	dev_dbg(&cdev->gadget->dev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
373		obex->port_num,
374		gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
375		obex->port.in->name, obex->port.out->name);
376
377	return 0;
378
379fail:
380	ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
381
382	return status;
383}
384
385static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
386{
387	return container_of(to_config_group(item), struct f_serial_opts,
388			    func_inst.group);
389}
390
391static void obex_attr_release(struct config_item *item)
392{
393	struct f_serial_opts *opts = to_f_serial_opts(item);
394
395	usb_put_function_instance(&opts->func_inst);
396}
397
398static struct configfs_item_operations obex_item_ops = {
399	.release	= obex_attr_release,
400};
401
402static ssize_t f_obex_port_num_show(struct config_item *item, char *page)
403{
404	return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
405}
406
407CONFIGFS_ATTR_RO(f_obex_, port_num);
408
409static struct configfs_attribute *acm_attrs[] = {
410	&f_obex_attr_port_num,
411	NULL,
412};
413
414static struct config_item_type obex_func_type = {
415	.ct_item_ops	= &obex_item_ops,
416	.ct_attrs	= acm_attrs,
417	.ct_owner	= THIS_MODULE,
418};
419
420static void obex_free_inst(struct usb_function_instance *f)
421{
422	struct f_serial_opts *opts;
423
424	opts = container_of(f, struct f_serial_opts, func_inst);
425	gserial_free_line(opts->port_num);
426	kfree(opts);
427}
428
429static struct usb_function_instance *obex_alloc_inst(void)
430{
431	struct f_serial_opts *opts;
432	int ret;
433
434	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
435	if (!opts)
436		return ERR_PTR(-ENOMEM);
437
438	opts->func_inst.free_func_inst = obex_free_inst;
439	ret = gserial_alloc_line(&opts->port_num);
440	if (ret) {
441		kfree(opts);
442		return ERR_PTR(ret);
443	}
444	config_group_init_type_name(&opts->func_inst.group, "",
445				    &obex_func_type);
446
447	return &opts->func_inst;
448}
449
450static void obex_free(struct usb_function *f)
451{
452	struct f_obex *obex;
453
454	obex = func_to_obex(f);
455	kfree(obex);
456}
457
458static void obex_unbind(struct usb_configuration *c, struct usb_function *f)
459{
460	usb_free_all_descriptors(f);
461}
462
463static struct usb_function *obex_alloc(struct usb_function_instance *fi)
464{
465	struct f_obex	*obex;
466	struct f_serial_opts *opts;
467
468	/* allocate and initialize one new instance */
469	obex = kzalloc(sizeof(*obex), GFP_KERNEL);
470	if (!obex)
471		return ERR_PTR(-ENOMEM);
472
473	opts = container_of(fi, struct f_serial_opts, func_inst);
474
475	obex->port_num = opts->port_num;
476
477	obex->port.connect = obex_connect;
478	obex->port.disconnect = obex_disconnect;
479
480	obex->port.func.name = "obex";
481	/* descriptors are per-instance copies */
482	obex->port.func.bind = obex_bind;
483	obex->port.func.unbind = obex_unbind;
484	obex->port.func.set_alt = obex_set_alt;
485	obex->port.func.get_alt = obex_get_alt;
486	obex->port.func.disable = obex_disable;
487	obex->port.func.free_func = obex_free;
488	obex->port.func.bind_deactivated = true;
489
490	return &obex->port.func;
491}
492
493DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc);
494MODULE_AUTHOR("Felipe Balbi");
495MODULE_LICENSE("GPL");