Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 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 | |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 17 | #include "gadget_chips.h" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 18 | #define DRIVER_DESC "Linux USB Audio Gadget" |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 19 | #define DRIVER_VERSION "Feb 2, 2012" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 20 | |
| 21 | /*-------------------------------------------------------------------------*/ |
| 22 | |
| 23 | /* |
| 24 | * Kbuild is not very cooperative with respect to linking separately |
| 25 | * compiled library objects into one module. So for now we won't use |
| 26 | * separate compilation ... ensuring init/exit sections work to shrink |
| 27 | * the runtime footprint, and giving us at least some parts of what |
| 28 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 29 | */ |
| 30 | #include "composite.c" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 31 | |
Jassi Brar | d747a91 | 2012-02-02 22:01:12 +0530 | [diff] [blame] | 32 | /* string IDs are assigned dynamically */ |
| 33 | |
| 34 | #define STRING_MANUFACTURER_IDX 0 |
| 35 | #define STRING_PRODUCT_IDX 1 |
| 36 | |
| 37 | static char manufacturer[50]; |
| 38 | |
| 39 | static struct usb_string strings_dev[] = { |
| 40 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
| 41 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, |
| 42 | { } /* end of list */ |
| 43 | }; |
| 44 | |
| 45 | static struct usb_gadget_strings stringtab_dev = { |
| 46 | .language = 0x0409, /* en-us */ |
| 47 | .strings = strings_dev, |
| 48 | }; |
| 49 | |
| 50 | static struct usb_gadget_strings *audio_strings[] = { |
| 51 | &stringtab_dev, |
| 52 | NULL, |
| 53 | }; |
| 54 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 55 | #ifdef CONFIG_GADGET_UAC1 |
| 56 | #include "u_uac1.h" |
Jassi Brar | 18b5b3b | 2012-02-02 21:59:53 +0530 | [diff] [blame] | 57 | #include "u_uac1.c" |
| 58 | #include "f_uac1.c" |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 59 | #else |
| 60 | #include "f_uac2.c" |
| 61 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 62 | |
| 63 | /*-------------------------------------------------------------------------*/ |
| 64 | |
| 65 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 66 | * Instead: allocate your own, using normal USB-IF procedures. |
| 67 | */ |
| 68 | |
Greg Kroah-Hartman | 05cbc2d | 2009-06-23 16:01:06 -0700 | [diff] [blame] | 69 | /* Thanks to Linux Foundation for donating this product ID. */ |
| 70 | #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
| 71 | #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 72 | |
| 73 | /*-------------------------------------------------------------------------*/ |
| 74 | |
| 75 | static struct usb_device_descriptor device_desc = { |
| 76 | .bLength = sizeof device_desc, |
| 77 | .bDescriptorType = USB_DT_DEVICE, |
| 78 | |
| 79 | .bcdUSB = __constant_cpu_to_le16(0x200), |
| 80 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 81 | #ifdef CONFIG_GADGET_UAC1 |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 82 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 83 | .bDeviceSubClass = 0, |
| 84 | .bDeviceProtocol = 0, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 85 | #else |
| 86 | .bDeviceClass = USB_CLASS_MISC, |
| 87 | .bDeviceSubClass = 0x02, |
| 88 | .bDeviceProtocol = 0x01, |
| 89 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 90 | /* .bMaxPacketSize0 = f(hardware) */ |
| 91 | |
| 92 | /* Vendor and product id defaults change according to what configs |
| 93 | * we support. (As does bNumConfigurations.) These values can |
| 94 | * also be overridden by module parameters. |
| 95 | */ |
| 96 | .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM), |
| 97 | .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM), |
| 98 | /* .bcdDevice = f(hardware) */ |
| 99 | /* .iManufacturer = DYNAMIC */ |
| 100 | /* .iProduct = DYNAMIC */ |
| 101 | /* NO SERIAL NUMBER */ |
| 102 | .bNumConfigurations = 1, |
| 103 | }; |
| 104 | |
| 105 | static struct usb_otg_descriptor otg_descriptor = { |
| 106 | .bLength = sizeof otg_descriptor, |
| 107 | .bDescriptorType = USB_DT_OTG, |
| 108 | |
| 109 | /* REVISIT SRP-only hardware is possible, although |
| 110 | * it would not be called "OTG" ... |
| 111 | */ |
| 112 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 113 | }; |
| 114 | |
| 115 | static const struct usb_descriptor_header *otg_desc[] = { |
| 116 | (struct usb_descriptor_header *) &otg_descriptor, |
| 117 | NULL, |
| 118 | }; |
| 119 | |
| 120 | /*-------------------------------------------------------------------------*/ |
| 121 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 122 | static int __init audio_do_config(struct usb_configuration *c) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 123 | { |
| 124 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
| 125 | |
| 126 | if (gadget_is_otg(c->cdev->gadget)) { |
| 127 | c->descriptors = otg_desc; |
| 128 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 129 | } |
| 130 | |
| 131 | audio_bind_config(c); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static struct usb_configuration audio_config_driver = { |
| 137 | .label = DRIVER_DESC, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 138 | .bConfigurationValue = 1, |
| 139 | /* .iConfiguration = DYNAMIC */ |
| 140 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 141 | #ifndef CONFIG_GADGET_UAC1 |
| 142 | .unbind = uac2_unbind_config, |
| 143 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /*-------------------------------------------------------------------------*/ |
| 147 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 148 | static int __init audio_bind(struct usb_composite_dev *cdev) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 149 | { |
| 150 | int gcnum; |
| 151 | int status; |
| 152 | |
| 153 | gcnum = usb_gadget_controller_number(cdev->gadget); |
| 154 | if (gcnum >= 0) |
| 155 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
| 156 | else { |
| 157 | ERROR(cdev, "controller '%s' not recognized; trying %s\n", |
| 158 | cdev->gadget->name, |
| 159 | audio_config_driver.label); |
| 160 | device_desc.bcdDevice = |
| 161 | __constant_cpu_to_le16(0x0300 | 0x0099); |
| 162 | } |
| 163 | |
| 164 | /* device descriptor strings: manufacturer, product */ |
| 165 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
| 166 | init_utsname()->sysname, init_utsname()->release, |
| 167 | cdev->gadget->name); |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame^] | 168 | status = usb_string_ids_tab(cdev, strings_dev); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 169 | if (status < 0) |
| 170 | goto fail; |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame^] | 171 | device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id; |
| 172 | device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 173 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 174 | status = usb_add_config(cdev, &audio_config_driver, audio_do_config); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 175 | if (status < 0) |
| 176 | goto fail; |
| 177 | |
| 178 | INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION); |
| 179 | return 0; |
| 180 | |
| 181 | fail: |
| 182 | return status; |
| 183 | } |
| 184 | |
| 185 | static int __exit audio_unbind(struct usb_composite_dev *cdev) |
| 186 | { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 187 | #ifdef CONFIG_GADGET_UAC1 |
Cliff Cai | feef1d9 | 2009-12-09 22:28:39 -0500 | [diff] [blame] | 188 | gaudio_cleanup(); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 189 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 193 | static __refdata struct usb_composite_driver audio_driver = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 194 | .name = "g_audio", |
| 195 | .dev = &device_desc, |
| 196 | .strings = audio_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 197 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 198 | .bind = audio_bind, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 199 | .unbind = __exit_p(audio_unbind), |
| 200 | }; |
| 201 | |
| 202 | static int __init init(void) |
| 203 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 204 | return usb_composite_probe(&audio_driver); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 205 | } |
| 206 | module_init(init); |
| 207 | |
| 208 | static void __exit cleanup(void) |
| 209 | { |
| 210 | usb_composite_unregister(&audio_driver); |
| 211 | } |
| 212 | module_exit(cleanup); |
| 213 | |
| 214 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 215 | MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>"); |
| 216 | MODULE_LICENSE("GPL"); |
| 217 | |