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