Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * audio.c -- Audio gadget driver
  3 *
  4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  5 * Copyright (C) 2008 Analog Devices, Inc
  6 *
  7 * Enter bugs at http://blackfin.uclinux.org/
  8 *
  9 * Licensed under the GPL-2 or later.
 10 */
 11
 12/* #define VERBOSE_DEBUG */
 13
 14#include <linux/kernel.h>
 15#include <linux/utsname.h>
 16
 17#include "u_audio.h"
 18
 
 19#define DRIVER_DESC		"Linux USB Audio Gadget"
 20#define DRIVER_VERSION		"Dec 18, 2008"
 21
 22/*-------------------------------------------------------------------------*/
 23
 24/*
 25 * Kbuild is not very cooperative with respect to linking separately
 26 * compiled library objects into one module.  So for now we won't use
 27 * separate compilation ... ensuring init/exit sections work to shrink
 28 * the runtime footprint, and giving us at least some parts of what
 29 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
 30 */
 31#include "composite.c"
 32#include "usbstring.c"
 33#include "config.c"
 34#include "epautoconf.c"
 35
 36#include "u_audio.c"
 37#include "f_audio.c"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 38
 39/*-------------------------------------------------------------------------*/
 40
 41/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 42 * Instead:  allocate your own, using normal USB-IF procedures.
 43 */
 44
 45/* Thanks to Linux Foundation for donating this product ID. */
 46#define AUDIO_VENDOR_NUM		0x1d6b	/* Linux Foundation */
 47#define AUDIO_PRODUCT_NUM		0x0101	/* Linux-USB Audio Gadget */
 48
 49/*-------------------------------------------------------------------------*/
 50
 51static struct usb_device_descriptor device_desc = {
 52	.bLength =		sizeof device_desc,
 53	.bDescriptorType =	USB_DT_DEVICE,
 54
 55	.bcdUSB =		__constant_cpu_to_le16(0x200),
 56
 
 57	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
 58	.bDeviceSubClass =	0,
 59	.bDeviceProtocol =	0,
 
 
 
 
 
 60	/* .bMaxPacketSize0 = f(hardware) */
 61
 62	/* Vendor and product id defaults change according to what configs
 63	 * we support.  (As does bNumConfigurations.)  These values can
 64	 * also be overridden by module parameters.
 65	 */
 66	.idVendor =		__constant_cpu_to_le16(AUDIO_VENDOR_NUM),
 67	.idProduct =		__constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
 68	/* .bcdDevice = f(hardware) */
 69	/* .iManufacturer = DYNAMIC */
 70	/* .iProduct = DYNAMIC */
 71	/* NO SERIAL NUMBER */
 72	.bNumConfigurations =	1,
 73};
 74
 75static struct usb_otg_descriptor otg_descriptor = {
 76	.bLength =		sizeof otg_descriptor,
 77	.bDescriptorType =	USB_DT_OTG,
 78
 79	/* REVISIT SRP-only hardware is possible, although
 80	 * it would not be called "OTG" ...
 81	 */
 82	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP,
 83};
 84
 85static const struct usb_descriptor_header *otg_desc[] = {
 86	(struct usb_descriptor_header *) &otg_descriptor,
 87	NULL,
 88};
 89
 90/*-------------------------------------------------------------------------*/
 91
 92static int __init audio_do_config(struct usb_configuration *c)
 93{
 94	/* FIXME alloc iConfiguration string, set it in c->strings */
 95
 96	if (gadget_is_otg(c->cdev->gadget)) {
 97		c->descriptors = otg_desc;
 98		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 99	}
100
101	audio_bind_config(c);
102
103	return 0;
104}
105
106static struct usb_configuration audio_config_driver = {
107	.label			= DRIVER_DESC,
108	.bConfigurationValue	= 1,
109	/* .iConfiguration = DYNAMIC */
110	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
 
 
 
111};
112
113/*-------------------------------------------------------------------------*/
114
115static int __init audio_bind(struct usb_composite_dev *cdev)
116{
117	int			gcnum;
118	int			status;
119
120	gcnum = usb_gadget_controller_number(cdev->gadget);
121	if (gcnum >= 0)
122		device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
123	else {
124		ERROR(cdev, "controller '%s' not recognized; trying %s\n",
125			cdev->gadget->name,
126			audio_config_driver.label);
127		device_desc.bcdDevice =
128			__constant_cpu_to_le16(0x0300 | 0x0099);
129	}
130
131	/* device descriptor strings: manufacturer, product */
132	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
133		init_utsname()->sysname, init_utsname()->release,
134		cdev->gadget->name);
135	status = usb_string_id(cdev);
136	if (status < 0)
137		goto fail;
138	strings_dev[STRING_MANUFACTURER_IDX].id = status;
139	device_desc.iManufacturer = status;
140
141	status = usb_string_id(cdev);
142	if (status < 0)
143		goto fail;
144	strings_dev[STRING_PRODUCT_IDX].id = status;
145	device_desc.iProduct = status;
146
147	status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
148	if (status < 0)
149		goto fail;
 
150
151	INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
152	return 0;
153
154fail:
155	return status;
156}
157
158static int __exit audio_unbind(struct usb_composite_dev *cdev)
159{
 
160	gaudio_cleanup();
 
161	return 0;
162}
163
164static struct usb_composite_driver audio_driver = {
165	.name		= "g_audio",
166	.dev		= &device_desc,
167	.strings	= audio_strings,
168	.max_speed	= USB_SPEED_HIGH,
 
169	.unbind		= __exit_p(audio_unbind),
170};
171
172static int __init init(void)
173{
174	return usb_composite_probe(&audio_driver, audio_bind);
175}
176module_init(init);
177
178static void __exit cleanup(void)
179{
180	usb_composite_unregister(&audio_driver);
181}
182module_exit(cleanup);
183
184MODULE_DESCRIPTION(DRIVER_DESC);
185MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
186MODULE_LICENSE("GPL");
187
v3.15
  1/*
  2 * audio.c -- Audio gadget driver
  3 *
  4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  5 * Copyright (C) 2008 Analog Devices, Inc
  6 *
  7 * Enter bugs at http://blackfin.uclinux.org/
  8 *
  9 * Licensed under the GPL-2 or later.
 10 */
 11
 12/* #define VERBOSE_DEBUG */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/usb/composite.h>
 
 17
 18#include "gadget_chips.h"
 19#define DRIVER_DESC		"Linux USB Audio Gadget"
 20#define DRIVER_VERSION		"Feb 2, 2012"
 21
 22USB_GADGET_COMPOSITE_OPTIONS();
 23
 24/* string IDs are assigned dynamically */
 25
 26static struct usb_string strings_dev[] = {
 27	[USB_GADGET_MANUFACTURER_IDX].s = "",
 28	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
 29	[USB_GADGET_SERIAL_IDX].s = "",
 30	{  } /* end of list */
 31};
 
 
 
 32
 33static struct usb_gadget_strings stringtab_dev = {
 34	.language = 0x0409,	/* en-us */
 35	.strings = strings_dev,
 36};
 37
 38static struct usb_gadget_strings *audio_strings[] = {
 39	&stringtab_dev,
 40	NULL,
 41};
 42
 43#ifdef CONFIG_GADGET_UAC1
 44#include "u_uac1.h"
 45#include "u_uac1.c"
 46#include "f_uac1.c"
 47#else
 48#include "f_uac2.c"
 49#endif
 50
 51/*-------------------------------------------------------------------------*/
 52
 53/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 54 * Instead:  allocate your own, using normal USB-IF procedures.
 55 */
 56
 57/* Thanks to Linux Foundation for donating this product ID. */
 58#define AUDIO_VENDOR_NUM		0x1d6b	/* Linux Foundation */
 59#define AUDIO_PRODUCT_NUM		0x0101	/* Linux-USB Audio Gadget */
 60
 61/*-------------------------------------------------------------------------*/
 62
 63static struct usb_device_descriptor device_desc = {
 64	.bLength =		sizeof device_desc,
 65	.bDescriptorType =	USB_DT_DEVICE,
 66
 67	.bcdUSB =		__constant_cpu_to_le16(0x200),
 68
 69#ifdef CONFIG_GADGET_UAC1
 70	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
 71	.bDeviceSubClass =	0,
 72	.bDeviceProtocol =	0,
 73#else
 74	.bDeviceClass =		USB_CLASS_MISC,
 75	.bDeviceSubClass =	0x02,
 76	.bDeviceProtocol =	0x01,
 77#endif
 78	/* .bMaxPacketSize0 = f(hardware) */
 79
 80	/* Vendor and product id defaults change according to what configs
 81	 * we support.  (As does bNumConfigurations.)  These values can
 82	 * also be overridden by module parameters.
 83	 */
 84	.idVendor =		__constant_cpu_to_le16(AUDIO_VENDOR_NUM),
 85	.idProduct =		__constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
 86	/* .bcdDevice = f(hardware) */
 87	/* .iManufacturer = DYNAMIC */
 88	/* .iProduct = DYNAMIC */
 89	/* NO SERIAL NUMBER */
 90	.bNumConfigurations =	1,
 91};
 92
 93static struct usb_otg_descriptor otg_descriptor = {
 94	.bLength =		sizeof otg_descriptor,
 95	.bDescriptorType =	USB_DT_OTG,
 96
 97	/* REVISIT SRP-only hardware is possible, although
 98	 * it would not be called "OTG" ...
 99	 */
100	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP,
101};
102
103static const struct usb_descriptor_header *otg_desc[] = {
104	(struct usb_descriptor_header *) &otg_descriptor,
105	NULL,
106};
107
108/*-------------------------------------------------------------------------*/
109
110static int __init audio_do_config(struct usb_configuration *c)
111{
112	/* FIXME alloc iConfiguration string, set it in c->strings */
113
114	if (gadget_is_otg(c->cdev->gadget)) {
115		c->descriptors = otg_desc;
116		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
117	}
118
119	audio_bind_config(c);
120
121	return 0;
122}
123
124static struct usb_configuration audio_config_driver = {
125	.label			= DRIVER_DESC,
126	.bConfigurationValue	= 1,
127	/* .iConfiguration = DYNAMIC */
128	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
129#ifndef CONFIG_GADGET_UAC1
130	.unbind			= uac2_unbind_config,
131#endif
132};
133
134/*-------------------------------------------------------------------------*/
135
136static int __init audio_bind(struct usb_composite_dev *cdev)
137{
 
138	int			status;
139
140	status = usb_string_ids_tab(cdev, strings_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141	if (status < 0)
142		goto fail;
143	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
144	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
145
146	status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
147	if (status < 0)
148		goto fail;
149	usb_composite_overwrite_options(cdev, &coverwrite);
150
151	INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
152	return 0;
153
154fail:
155	return status;
156}
157
158static int __exit audio_unbind(struct usb_composite_dev *cdev)
159{
160#ifdef CONFIG_GADGET_UAC1
161	gaudio_cleanup();
162#endif
163	return 0;
164}
165
166static __refdata struct usb_composite_driver audio_driver = {
167	.name		= "g_audio",
168	.dev		= &device_desc,
169	.strings	= audio_strings,
170	.max_speed	= USB_SPEED_HIGH,
171	.bind		= audio_bind,
172	.unbind		= __exit_p(audio_unbind),
173};
174
175static int __init init(void)
176{
177	return usb_composite_probe(&audio_driver);
178}
179module_init(init);
180
181static void __exit cleanup(void)
182{
183	usb_composite_unregister(&audio_driver);
184}
185module_exit(cleanup);
186
187MODULE_DESCRIPTION(DRIVER_DESC);
188MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
189MODULE_LICENSE("GPL");
190