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