blob: 231b0efe8fdc12704f596e84c7fbab8f86e9aee7 [file] [log] [blame]
Bryan Wuc6994e62009-06-03 09:17:58 -04001/*
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>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020015#include <linux/module.h>
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020016#include <linux/usb/composite.h>
Bryan Wuc6994e62009-06-03 09:17:58 -040017
Sebastian Andrzej Siewiordc995fc2012-09-06 20:11:12 +020018#include "gadget_chips.h"
Bryan Wuc6994e62009-06-03 09:17:58 -040019#define DRIVER_DESC "Linux USB Audio Gadget"
Jassi Brar132fcb42012-02-02 22:01:34 +053020#define DRIVER_VERSION "Feb 2, 2012"
Bryan Wuc6994e62009-06-03 09:17:58 -040021
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020022USB_GADGET_COMPOSITE_OPTIONS();
Bryan Wuc6994e62009-06-03 09:17:58 -040023
Jassi Brard747a912012-02-02 22:01:12 +053024/* string IDs are assigned dynamically */
25
Jassi Brard747a912012-02-02 22:01:12 +053026static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020027 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020028 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
29 [USB_GADGET_SERIAL_IDX].s = "",
Jassi Brard747a912012-02-02 22:01:12 +053030 { } /* 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
Jassi Brar132fcb42012-02-02 22:01:34 +053043#ifdef CONFIG_GADGET_UAC1
44#include "u_uac1.h"
Jassi Brar18b5b3b2012-02-02 21:59:53 +053045#include "u_uac1.c"
46#include "f_uac1.c"
Jassi Brar132fcb42012-02-02 22:01:34 +053047#else
48#include "f_uac2.c"
49#endif
Bryan Wuc6994e62009-06-03 09:17:58 -040050
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
Greg Kroah-Hartman05cbc2d2009-06-23 16:01:06 -070057/* 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 */
Bryan Wuc6994e62009-06-03 09:17:58 -040060
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
Jassi Brar132fcb42012-02-02 22:01:34 +053069#ifdef CONFIG_GADGET_UAC1
Bryan Wuc6994e62009-06-03 09:17:58 -040070 .bDeviceClass = USB_CLASS_PER_INTERFACE,
71 .bDeviceSubClass = 0,
72 .bDeviceProtocol = 0,
Jassi Brar132fcb42012-02-02 22:01:34 +053073#else
74 .bDeviceClass = USB_CLASS_MISC,
75 .bDeviceSubClass = 0x02,
76 .bDeviceProtocol = 0x01,
77#endif
Bryan Wuc6994e62009-06-03 09:17:58 -040078 /* .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
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200110static int __init audio_do_config(struct usb_configuration *c)
Bryan Wuc6994e62009-06-03 09:17:58 -0400111{
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,
Bryan Wuc6994e62009-06-03 09:17:58 -0400126 .bConfigurationValue = 1,
127 /* .iConfiguration = DYNAMIC */
128 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
Jassi Brar132fcb42012-02-02 22:01:34 +0530129#ifndef CONFIG_GADGET_UAC1
130 .unbind = uac2_unbind_config,
131#endif
Bryan Wuc6994e62009-06-03 09:17:58 -0400132};
133
134/*-------------------------------------------------------------------------*/
135
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200136static int __init audio_bind(struct usb_composite_dev *cdev)
Bryan Wuc6994e62009-06-03 09:17:58 -0400137{
Bryan Wuc6994e62009-06-03 09:17:58 -0400138 int status;
139
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200140 status = usb_string_ids_tab(cdev, strings_dev);
Bryan Wuc6994e62009-06-03 09:17:58 -0400141 if (status < 0)
142 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200143 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
144 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Bryan Wuc6994e62009-06-03 09:17:58 -0400145
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200146 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
Bryan Wuc6994e62009-06-03 09:17:58 -0400147 if (status < 0)
148 goto fail;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200149 usb_composite_overwrite_options(cdev, &coverwrite);
Bryan Wuc6994e62009-06-03 09:17:58 -0400150
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{
Jassi Brar132fcb42012-02-02 22:01:34 +0530160#ifdef CONFIG_GADGET_UAC1
Cliff Caifeef1d92009-12-09 22:28:39 -0500161 gaudio_cleanup();
Jassi Brar132fcb42012-02-02 22:01:34 +0530162#endif
Bryan Wuc6994e62009-06-03 09:17:58 -0400163 return 0;
164}
165
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200166static __refdata struct usb_composite_driver audio_driver = {
Bryan Wuc6994e62009-06-03 09:17:58 -0400167 .name = "g_audio",
168 .dev = &device_desc,
169 .strings = audio_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300170 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200171 .bind = audio_bind,
Bryan Wuc6994e62009-06-03 09:17:58 -0400172 .unbind = __exit_p(audio_unbind),
173};
174
175static int __init init(void)
176{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200177 return usb_composite_probe(&audio_driver);
Bryan Wuc6994e62009-06-03 09:17:58 -0400178}
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