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 | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame^] | 15 | #include <linux/module.h> |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 16 | #include <linux/usb/composite.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 17 | |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 18 | #include "gadget_chips.h" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 19 | #define DRIVER_DESC "Linux USB Audio Gadget" |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 20 | #define DRIVER_VERSION "Feb 2, 2012" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 21 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 22 | USB_GADGET_COMPOSITE_OPTIONS(); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 23 | |
Jassi Brar | d747a91 | 2012-02-02 22:01:12 +0530 | [diff] [blame] | 24 | /* string IDs are assigned dynamically */ |
| 25 | |
Jassi Brar | d747a91 | 2012-02-02 22:01:12 +0530 | [diff] [blame] | 26 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 27 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 28 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 29 | [USB_GADGET_SERIAL_IDX].s = "", |
Jassi Brar | d747a91 | 2012-02-02 22:01:12 +0530 | [diff] [blame] | 30 | { } /* end of list */ |
| 31 | }; |
| 32 | |
| 33 | static struct usb_gadget_strings stringtab_dev = { |
| 34 | .language = 0x0409, /* en-us */ |
| 35 | .strings = strings_dev, |
| 36 | }; |
| 37 | |
| 38 | static struct usb_gadget_strings *audio_strings[] = { |
| 39 | &stringtab_dev, |
| 40 | NULL, |
| 41 | }; |
| 42 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 43 | #ifdef CONFIG_GADGET_UAC1 |
| 44 | #include "u_uac1.h" |
Jassi Brar | 18b5b3b | 2012-02-02 21:59:53 +0530 | [diff] [blame] | 45 | #include "u_uac1.c" |
| 46 | #include "f_uac1.c" |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 47 | #else |
| 48 | #include "f_uac2.c" |
| 49 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 05cbc2d | 2009-06-23 16:01:06 -0700 | [diff] [blame] | 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 */ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 60 | |
| 61 | /*-------------------------------------------------------------------------*/ |
| 62 | |
| 63 | static 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 Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 69 | #ifdef CONFIG_GADGET_UAC1 |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 70 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 71 | .bDeviceSubClass = 0, |
| 72 | .bDeviceProtocol = 0, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 73 | #else |
| 74 | .bDeviceClass = USB_CLASS_MISC, |
| 75 | .bDeviceSubClass = 0x02, |
| 76 | .bDeviceProtocol = 0x01, |
| 77 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 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 | |
| 93 | static 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 | |
| 103 | static const struct usb_descriptor_header *otg_desc[] = { |
| 104 | (struct usb_descriptor_header *) &otg_descriptor, |
| 105 | NULL, |
| 106 | }; |
| 107 | |
| 108 | /*-------------------------------------------------------------------------*/ |
| 109 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 110 | static int __init audio_do_config(struct usb_configuration *c) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 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 | |
| 124 | static struct usb_configuration audio_config_driver = { |
| 125 | .label = DRIVER_DESC, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 126 | .bConfigurationValue = 1, |
| 127 | /* .iConfiguration = DYNAMIC */ |
| 128 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 129 | #ifndef CONFIG_GADGET_UAC1 |
| 130 | .unbind = uac2_unbind_config, |
| 131 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /*-------------------------------------------------------------------------*/ |
| 135 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 136 | static int __init audio_bind(struct usb_composite_dev *cdev) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 137 | { |
| 138 | int gcnum; |
| 139 | int status; |
| 140 | |
| 141 | gcnum = usb_gadget_controller_number(cdev->gadget); |
| 142 | if (gcnum >= 0) |
| 143 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
| 144 | else { |
| 145 | ERROR(cdev, "controller '%s' not recognized; trying %s\n", |
| 146 | cdev->gadget->name, |
| 147 | audio_config_driver.label); |
| 148 | device_desc.bcdDevice = |
| 149 | __constant_cpu_to_le16(0x0300 | 0x0099); |
| 150 | } |
| 151 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 152 | status = usb_string_ids_tab(cdev, strings_dev); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 153 | if (status < 0) |
| 154 | goto fail; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 155 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 156 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 157 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 158 | status = usb_add_config(cdev, &audio_config_driver, audio_do_config); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 159 | if (status < 0) |
| 160 | goto fail; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 161 | usb_composite_overwrite_options(cdev, &coverwrite); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 162 | |
| 163 | INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION); |
| 164 | return 0; |
| 165 | |
| 166 | fail: |
| 167 | return status; |
| 168 | } |
| 169 | |
| 170 | static int __exit audio_unbind(struct usb_composite_dev *cdev) |
| 171 | { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 172 | #ifdef CONFIG_GADGET_UAC1 |
Cliff Cai | feef1d9 | 2009-12-09 22:28:39 -0500 | [diff] [blame] | 173 | gaudio_cleanup(); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 174 | #endif |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 178 | static __refdata struct usb_composite_driver audio_driver = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 179 | .name = "g_audio", |
| 180 | .dev = &device_desc, |
| 181 | .strings = audio_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 182 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 183 | .bind = audio_bind, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 184 | .unbind = __exit_p(audio_unbind), |
| 185 | }; |
| 186 | |
| 187 | static int __init init(void) |
| 188 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 189 | return usb_composite_probe(&audio_driver); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 190 | } |
| 191 | module_init(init); |
| 192 | |
| 193 | static void __exit cleanup(void) |
| 194 | { |
| 195 | usb_composite_unregister(&audio_driver); |
| 196 | } |
| 197 | module_exit(cleanup); |
| 198 | |
| 199 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 200 | MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>"); |
| 201 | MODULE_LICENSE("GPL"); |
| 202 | |