| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * f_audio.c -- USB Audio class function 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 | #include <linux/kernel.h> | 
 | 13 | #include <linux/device.h> | 
 | 14 | #include <asm/atomic.h> | 
 | 15 |  | 
 | 16 | #include "u_audio.h" | 
 | 17 |  | 
 | 18 | #define OUT_EP_MAX_PACKET_SIZE	200 | 
 | 19 | static int req_buf_size = OUT_EP_MAX_PACKET_SIZE; | 
 | 20 | module_param(req_buf_size, int, S_IRUGO); | 
 | 21 | MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size"); | 
 | 22 |  | 
 | 23 | static int req_count = 256; | 
 | 24 | module_param(req_count, int, S_IRUGO); | 
 | 25 | MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count"); | 
 | 26 |  | 
 | 27 | static int audio_buf_size = 48000; | 
 | 28 | module_param(audio_buf_size, int, S_IRUGO); | 
 | 29 | MODULE_PARM_DESC(audio_buf_size, "Audio buffer size"); | 
 | 30 |  | 
| Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 31 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); | 
 | 32 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); | 
 | 33 |  | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 34 | /* | 
 | 35 |  * DESCRIPTORS ... most are static, but strings and full | 
 | 36 |  * configuration descriptors are built on demand. | 
 | 37 |  */ | 
 | 38 |  | 
 | 39 | /* | 
 | 40 |  * We have two interfaces- AudioControl and AudioStreaming | 
 | 41 |  * TODO: only supcard playback currently | 
 | 42 |  */ | 
 | 43 | #define F_AUDIO_AC_INTERFACE	0 | 
 | 44 | #define F_AUDIO_AS_INTERFACE	1 | 
 | 45 | #define F_AUDIO_NUM_INTERFACES	2 | 
 | 46 |  | 
 | 47 | /* B.3.1  Standard AC Interface Descriptor */ | 
 | 48 | static struct usb_interface_descriptor ac_interface_desc __initdata = { | 
 | 49 | 	.bLength =		USB_DT_INTERFACE_SIZE, | 
 | 50 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 51 | 	.bNumEndpoints =	0, | 
 | 52 | 	.bInterfaceClass =	USB_CLASS_AUDIO, | 
 | 53 | 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL, | 
 | 54 | }; | 
 | 55 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 56 | DECLARE_UAC_AC_HEADER_DESCRIPTOR(2); | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 57 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 58 | #define UAC_DT_AC_HEADER_LENGTH	UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 59 | /* B.3.2  Class-Specific AC Interface Descriptor */ | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 60 | static struct uac_ac_header_descriptor_2 ac_header_desc = { | 
 | 61 | 	.bLength =		UAC_DT_AC_HEADER_LENGTH, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 62 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 63 | 	.bDescriptorSubtype =	UAC_HEADER, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 64 | 	.bcdADC =		__constant_cpu_to_le16(0x0100), | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 65 | 	.wTotalLength =		__constant_cpu_to_le16(UAC_DT_AC_HEADER_LENGTH), | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 66 | 	.bInCollection =	F_AUDIO_NUM_INTERFACES, | 
 | 67 | 	.baInterfaceNr = { | 
 | 68 | 		[0] =		F_AUDIO_AC_INTERFACE, | 
 | 69 | 		[1] =		F_AUDIO_AS_INTERFACE, | 
 | 70 | 	} | 
 | 71 | }; | 
 | 72 |  | 
 | 73 | #define INPUT_TERMINAL_ID	1 | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 74 | static struct uac_input_terminal_descriptor input_terminal_desc = { | 
 | 75 | 	.bLength =		UAC_DT_INPUT_TERMINAL_SIZE, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 76 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 77 | 	.bDescriptorSubtype =	UAC_INPUT_TERMINAL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 78 | 	.bTerminalID =		INPUT_TERMINAL_ID, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 79 | 	.wTerminalType =	UAC_TERMINAL_STREAMING, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 80 | 	.bAssocTerminal =	0, | 
 | 81 | 	.wChannelConfig =	0x3, | 
 | 82 | }; | 
 | 83 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 84 | DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 85 |  | 
 | 86 | #define FEATURE_UNIT_ID		2 | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 87 | static struct uac_feature_unit_descriptor_0 feature_unit_desc = { | 
 | 88 | 	.bLength		= UAC_DT_FEATURE_UNIT_SIZE(0), | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 89 | 	.bDescriptorType	= USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 90 | 	.bDescriptorSubtype	= UAC_FEATURE_UNIT, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 91 | 	.bUnitID		= FEATURE_UNIT_ID, | 
 | 92 | 	.bSourceID		= INPUT_TERMINAL_ID, | 
 | 93 | 	.bControlSize		= 2, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 94 | 	.bmaControls[0]		= (UAC_FU_MUTE | UAC_FU_VOLUME), | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 95 | }; | 
 | 96 |  | 
 | 97 | static struct usb_audio_control mute_control = { | 
 | 98 | 	.list = LIST_HEAD_INIT(mute_control.list), | 
 | 99 | 	.name = "Mute Control", | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 100 | 	.type = UAC_MUTE_CONTROL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 101 | 	/* Todo: add real Mute control code */ | 
 | 102 | 	.set = generic_set_cmd, | 
 | 103 | 	.get = generic_get_cmd, | 
 | 104 | }; | 
 | 105 |  | 
 | 106 | static struct usb_audio_control volume_control = { | 
 | 107 | 	.list = LIST_HEAD_INIT(volume_control.list), | 
 | 108 | 	.name = "Volume Control", | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 109 | 	.type = UAC_VOLUME_CONTROL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 110 | 	/* Todo: add real Volume control code */ | 
 | 111 | 	.set = generic_set_cmd, | 
 | 112 | 	.get = generic_get_cmd, | 
 | 113 | }; | 
 | 114 |  | 
 | 115 | static struct usb_audio_control_selector feature_unit = { | 
 | 116 | 	.list = LIST_HEAD_INIT(feature_unit.list), | 
 | 117 | 	.id = FEATURE_UNIT_ID, | 
 | 118 | 	.name = "Mute & Volume Control", | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 119 | 	.type = UAC_FEATURE_UNIT, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 120 | 	.desc = (struct usb_descriptor_header *)&feature_unit_desc, | 
 | 121 | }; | 
 | 122 |  | 
 | 123 | #define OUTPUT_TERMINAL_ID	3 | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 124 | static struct uac_output_terminal_descriptor output_terminal_desc = { | 
 | 125 | 	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 126 | 	.bDescriptorType	= USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 127 | 	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 128 | 	.bTerminalID		= OUTPUT_TERMINAL_ID, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 129 | 	.wTerminalType		= UAC_OUTPUT_TERMINAL_SPEAKER, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 130 | 	.bAssocTerminal		= FEATURE_UNIT_ID, | 
 | 131 | 	.bSourceID		= FEATURE_UNIT_ID, | 
 | 132 | }; | 
 | 133 |  | 
 | 134 | /* B.4.1  Standard AS Interface Descriptor */ | 
 | 135 | static struct usb_interface_descriptor as_interface_alt_0_desc = { | 
 | 136 | 	.bLength =		USB_DT_INTERFACE_SIZE, | 
 | 137 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 138 | 	.bAlternateSetting =	0, | 
 | 139 | 	.bNumEndpoints =	0, | 
 | 140 | 	.bInterfaceClass =	USB_CLASS_AUDIO, | 
 | 141 | 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING, | 
 | 142 | }; | 
 | 143 |  | 
 | 144 | static struct usb_interface_descriptor as_interface_alt_1_desc = { | 
 | 145 | 	.bLength =		USB_DT_INTERFACE_SIZE, | 
 | 146 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 147 | 	.bAlternateSetting =	1, | 
 | 148 | 	.bNumEndpoints =	1, | 
 | 149 | 	.bInterfaceClass =	USB_CLASS_AUDIO, | 
 | 150 | 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING, | 
 | 151 | }; | 
 | 152 |  | 
 | 153 | /* B.4.2  Class-Specific AS Interface Descriptor */ | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 154 | static struct uac_as_header_descriptor as_header_desc = { | 
 | 155 | 	.bLength =		UAC_DT_AS_HEADER_SIZE, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 156 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 157 | 	.bDescriptorSubtype =	UAC_AS_GENERAL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 158 | 	.bTerminalLink =	INPUT_TERMINAL_ID, | 
 | 159 | 	.bDelay =		1, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 160 | 	.wFormatTag =		UAC_FORMAT_TYPE_I_PCM, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 161 | }; | 
 | 162 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 163 | DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 164 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 165 | static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { | 
 | 166 | 	.bLength =		UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 167 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 168 | 	.bDescriptorSubtype =	UAC_FORMAT_TYPE, | 
 | 169 | 	.bFormatType =		UAC_FORMAT_TYPE_I, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 170 | 	.bSubframeSize =	2, | 
 | 171 | 	.bBitResolution =	16, | 
 | 172 | 	.bSamFreqType =		1, | 
 | 173 | }; | 
 | 174 |  | 
 | 175 | /* Standard ISO OUT Endpoint Descriptor */ | 
 | 176 | static struct usb_endpoint_descriptor as_out_ep_desc __initdata = { | 
 | 177 | 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE, | 
 | 178 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 179 | 	.bEndpointAddress =	USB_DIR_OUT, | 
| Laurent Pinchart | 85e08ca | 2009-06-21 23:19:23 +0200 | [diff] [blame] | 180 | 	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 181 | 				| USB_ENDPOINT_XFER_ISOC, | 
 | 182 | 	.wMaxPacketSize =	__constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE), | 
 | 183 | 	.bInterval =		4, | 
 | 184 | }; | 
 | 185 |  | 
 | 186 | /* Class-specific AS ISO OUT Endpoint Descriptor */ | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 187 | static struct uac_iso_endpoint_descriptor as_iso_out_desc __initdata = { | 
 | 188 | 	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 189 | 	.bDescriptorType =	USB_DT_CS_ENDPOINT, | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 190 | 	.bDescriptorSubtype =	UAC_EP_GENERAL, | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 191 | 	.bmAttributes = 	1, | 
 | 192 | 	.bLockDelayUnits =	1, | 
 | 193 | 	.wLockDelay =		__constant_cpu_to_le16(1), | 
 | 194 | }; | 
 | 195 |  | 
 | 196 | static struct usb_descriptor_header *f_audio_desc[] __initdata = { | 
 | 197 | 	(struct usb_descriptor_header *)&ac_interface_desc, | 
 | 198 | 	(struct usb_descriptor_header *)&ac_header_desc, | 
 | 199 |  | 
 | 200 | 	(struct usb_descriptor_header *)&input_terminal_desc, | 
 | 201 | 	(struct usb_descriptor_header *)&output_terminal_desc, | 
 | 202 | 	(struct usb_descriptor_header *)&feature_unit_desc, | 
 | 203 |  | 
 | 204 | 	(struct usb_descriptor_header *)&as_interface_alt_0_desc, | 
 | 205 | 	(struct usb_descriptor_header *)&as_interface_alt_1_desc, | 
 | 206 | 	(struct usb_descriptor_header *)&as_header_desc, | 
 | 207 |  | 
 | 208 | 	(struct usb_descriptor_header *)&as_type_i_desc, | 
 | 209 |  | 
 | 210 | 	(struct usb_descriptor_header *)&as_out_ep_desc, | 
 | 211 | 	(struct usb_descriptor_header *)&as_iso_out_desc, | 
 | 212 | 	NULL, | 
 | 213 | }; | 
 | 214 |  | 
 | 215 | /* string IDs are assigned dynamically */ | 
 | 216 |  | 
 | 217 | #define STRING_MANUFACTURER_IDX		0 | 
 | 218 | #define STRING_PRODUCT_IDX		1 | 
 | 219 |  | 
 | 220 | static char manufacturer[50]; | 
 | 221 |  | 
 | 222 | static struct usb_string strings_dev[] = { | 
 | 223 | 	[STRING_MANUFACTURER_IDX].s = manufacturer, | 
 | 224 | 	[STRING_PRODUCT_IDX].s = DRIVER_DESC, | 
 | 225 | 	{  } /* end of list */ | 
 | 226 | }; | 
 | 227 |  | 
 | 228 | static struct usb_gadget_strings stringtab_dev = { | 
 | 229 | 	.language	= 0x0409,	/* en-us */ | 
 | 230 | 	.strings	= strings_dev, | 
 | 231 | }; | 
 | 232 |  | 
 | 233 | static struct usb_gadget_strings *audio_strings[] = { | 
 | 234 | 	&stringtab_dev, | 
 | 235 | 	NULL, | 
 | 236 | }; | 
 | 237 |  | 
 | 238 | /* | 
 | 239 |  * This function is an ALSA sound card following USB Audio Class Spec 1.0. | 
 | 240 |  */ | 
 | 241 |  | 
 | 242 | /*-------------------------------------------------------------------------*/ | 
 | 243 | struct f_audio_buf { | 
 | 244 | 	u8 *buf; | 
 | 245 | 	int actual; | 
 | 246 | 	struct list_head list; | 
 | 247 | }; | 
 | 248 |  | 
 | 249 | static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) | 
 | 250 | { | 
 | 251 | 	struct f_audio_buf *copy_buf; | 
 | 252 |  | 
 | 253 | 	copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); | 
 | 254 | 	if (!copy_buf) | 
 | 255 | 		return (struct f_audio_buf *)-ENOMEM; | 
 | 256 |  | 
 | 257 | 	copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); | 
 | 258 | 	if (!copy_buf->buf) { | 
 | 259 | 		kfree(copy_buf); | 
 | 260 | 		return (struct f_audio_buf *)-ENOMEM; | 
 | 261 | 	} | 
 | 262 |  | 
 | 263 | 	return copy_buf; | 
 | 264 | } | 
 | 265 |  | 
 | 266 | static void f_audio_buffer_free(struct f_audio_buf *audio_buf) | 
 | 267 | { | 
 | 268 | 	kfree(audio_buf->buf); | 
 | 269 | 	kfree(audio_buf); | 
 | 270 | } | 
 | 271 | /*-------------------------------------------------------------------------*/ | 
 | 272 |  | 
 | 273 | struct f_audio { | 
 | 274 | 	struct gaudio			card; | 
 | 275 |  | 
 | 276 | 	/* endpoints handle full and/or high speeds */ | 
 | 277 | 	struct usb_ep			*out_ep; | 
 | 278 | 	struct usb_endpoint_descriptor	*out_desc; | 
 | 279 |  | 
 | 280 | 	spinlock_t			lock; | 
 | 281 | 	struct f_audio_buf *copy_buf; | 
 | 282 | 	struct work_struct playback_work; | 
 | 283 | 	struct list_head play_queue; | 
 | 284 |  | 
 | 285 | 	/* Control Set command */ | 
 | 286 | 	struct list_head cs; | 
 | 287 | 	u8 set_cmd; | 
 | 288 | 	struct usb_audio_control *set_con; | 
 | 289 | }; | 
 | 290 |  | 
 | 291 | static inline struct f_audio *func_to_audio(struct usb_function *f) | 
 | 292 | { | 
 | 293 | 	return container_of(f, struct f_audio, card.func); | 
 | 294 | } | 
 | 295 |  | 
 | 296 | /*-------------------------------------------------------------------------*/ | 
 | 297 |  | 
 | 298 | static void f_audio_playback_work(struct work_struct *data) | 
 | 299 | { | 
 | 300 | 	struct f_audio *audio = container_of(data, struct f_audio, | 
 | 301 | 					playback_work); | 
 | 302 | 	struct f_audio_buf *play_buf; | 
 | 303 |  | 
 | 304 | 	spin_lock_irq(&audio->lock); | 
 | 305 | 	if (list_empty(&audio->play_queue)) { | 
 | 306 | 		spin_unlock_irq(&audio->lock); | 
 | 307 | 		return; | 
 | 308 | 	} | 
 | 309 | 	play_buf = list_first_entry(&audio->play_queue, | 
 | 310 | 			struct f_audio_buf, list); | 
 | 311 | 	list_del(&play_buf->list); | 
 | 312 | 	spin_unlock_irq(&audio->lock); | 
 | 313 |  | 
 | 314 | 	u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); | 
 | 315 | 	f_audio_buffer_free(play_buf); | 
 | 316 |  | 
 | 317 | 	return; | 
 | 318 | } | 
 | 319 |  | 
 | 320 | static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) | 
 | 321 | { | 
 | 322 | 	struct f_audio *audio = req->context; | 
 | 323 | 	struct usb_composite_dev *cdev = audio->card.func.config->cdev; | 
 | 324 | 	struct f_audio_buf *copy_buf = audio->copy_buf; | 
 | 325 | 	int err; | 
 | 326 |  | 
 | 327 | 	if (!copy_buf) | 
 | 328 | 		return -EINVAL; | 
 | 329 |  | 
 | 330 | 	/* Copy buffer is full, add it to the play_queue */ | 
 | 331 | 	if (audio_buf_size - copy_buf->actual < req->actual) { | 
 | 332 | 		list_add_tail(©_buf->list, &audio->play_queue); | 
 | 333 | 		schedule_work(&audio->playback_work); | 
 | 334 | 		copy_buf = f_audio_buffer_alloc(audio_buf_size); | 
 | 335 | 		if (copy_buf < 0) | 
 | 336 | 			return -ENOMEM; | 
 | 337 | 	} | 
 | 338 |  | 
 | 339 | 	memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); | 
 | 340 | 	copy_buf->actual += req->actual; | 
 | 341 | 	audio->copy_buf = copy_buf; | 
 | 342 |  | 
 | 343 | 	err = usb_ep_queue(ep, req, GFP_ATOMIC); | 
 | 344 | 	if (err) | 
 | 345 | 		ERROR(cdev, "%s queue req: %d\n", ep->name, err); | 
 | 346 |  | 
 | 347 | 	return 0; | 
 | 348 |  | 
 | 349 | } | 
 | 350 |  | 
 | 351 | static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) | 
 | 352 | { | 
 | 353 | 	struct f_audio *audio = req->context; | 
 | 354 | 	int status = req->status; | 
 | 355 | 	u32 data = 0; | 
 | 356 | 	struct usb_ep *out_ep = audio->out_ep; | 
 | 357 |  | 
 | 358 | 	switch (status) { | 
 | 359 |  | 
 | 360 | 	case 0:				/* normal completion? */ | 
 | 361 | 		if (ep == out_ep) | 
 | 362 | 			f_audio_out_ep_complete(ep, req); | 
 | 363 | 		else if (audio->set_con) { | 
 | 364 | 			memcpy(&data, req->buf, req->length); | 
 | 365 | 			audio->set_con->set(audio->set_con, audio->set_cmd, | 
 | 366 | 					le16_to_cpu(data)); | 
 | 367 | 			audio->set_con = NULL; | 
 | 368 | 		} | 
 | 369 | 		break; | 
 | 370 | 	default: | 
 | 371 | 		break; | 
 | 372 | 	} | 
 | 373 | } | 
 | 374 |  | 
 | 375 | static int audio_set_intf_req(struct usb_function *f, | 
 | 376 | 		const struct usb_ctrlrequest *ctrl) | 
 | 377 | { | 
 | 378 | 	struct f_audio		*audio = func_to_audio(f); | 
 | 379 | 	struct usb_composite_dev *cdev = f->config->cdev; | 
 | 380 | 	struct usb_request	*req = cdev->req; | 
 | 381 | 	u8			id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); | 
 | 382 | 	u16			len = le16_to_cpu(ctrl->wLength); | 
 | 383 | 	u16			w_value = le16_to_cpu(ctrl->wValue); | 
 | 384 | 	u8			con_sel = (w_value >> 8) & 0xFF; | 
 | 385 | 	u8			cmd = (ctrl->bRequest & 0x0F); | 
 | 386 | 	struct usb_audio_control_selector *cs; | 
 | 387 | 	struct usb_audio_control *con; | 
 | 388 |  | 
 | 389 | 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", | 
 | 390 | 			ctrl->bRequest, w_value, len, id); | 
 | 391 |  | 
 | 392 | 	list_for_each_entry(cs, &audio->cs, list) { | 
 | 393 | 		if (cs->id == id) { | 
 | 394 | 			list_for_each_entry(con, &cs->control, list) { | 
 | 395 | 				if (con->type == con_sel) { | 
 | 396 | 					audio->set_con = con; | 
 | 397 | 					break; | 
 | 398 | 				} | 
 | 399 | 			} | 
 | 400 | 			break; | 
 | 401 | 		} | 
 | 402 | 	} | 
 | 403 |  | 
 | 404 | 	audio->set_cmd = cmd; | 
 | 405 | 	req->context = audio; | 
 | 406 | 	req->complete = f_audio_complete; | 
 | 407 |  | 
 | 408 | 	return len; | 
 | 409 | } | 
 | 410 |  | 
 | 411 | static int audio_get_intf_req(struct usb_function *f, | 
 | 412 | 		const struct usb_ctrlrequest *ctrl) | 
 | 413 | { | 
 | 414 | 	struct f_audio		*audio = func_to_audio(f); | 
 | 415 | 	struct usb_composite_dev *cdev = f->config->cdev; | 
 | 416 | 	struct usb_request	*req = cdev->req; | 
 | 417 | 	int			value = -EOPNOTSUPP; | 
 | 418 | 	u8			id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); | 
 | 419 | 	u16			len = le16_to_cpu(ctrl->wLength); | 
 | 420 | 	u16			w_value = le16_to_cpu(ctrl->wValue); | 
 | 421 | 	u8			con_sel = (w_value >> 8) & 0xFF; | 
 | 422 | 	u8			cmd = (ctrl->bRequest & 0x0F); | 
 | 423 | 	struct usb_audio_control_selector *cs; | 
 | 424 | 	struct usb_audio_control *con; | 
 | 425 |  | 
 | 426 | 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", | 
 | 427 | 			ctrl->bRequest, w_value, len, id); | 
 | 428 |  | 
 | 429 | 	list_for_each_entry(cs, &audio->cs, list) { | 
 | 430 | 		if (cs->id == id) { | 
 | 431 | 			list_for_each_entry(con, &cs->control, list) { | 
 | 432 | 				if (con->type == con_sel && con->get) { | 
 | 433 | 					value = con->get(con, cmd); | 
 | 434 | 					break; | 
 | 435 | 				} | 
 | 436 | 			} | 
 | 437 | 			break; | 
 | 438 | 		} | 
 | 439 | 	} | 
 | 440 |  | 
 | 441 | 	req->context = audio; | 
 | 442 | 	req->complete = f_audio_complete; | 
 | 443 | 	memcpy(req->buf, &value, len); | 
 | 444 |  | 
 | 445 | 	return len; | 
 | 446 | } | 
 | 447 |  | 
 | 448 | static int | 
 | 449 | f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | 
 | 450 | { | 
 | 451 | 	struct usb_composite_dev *cdev = f->config->cdev; | 
 | 452 | 	struct usb_request	*req = cdev->req; | 
 | 453 | 	int			value = -EOPNOTSUPP; | 
 | 454 | 	u16			w_index = le16_to_cpu(ctrl->wIndex); | 
 | 455 | 	u16			w_value = le16_to_cpu(ctrl->wValue); | 
 | 456 | 	u16			w_length = le16_to_cpu(ctrl->wLength); | 
 | 457 |  | 
 | 458 | 	/* composite driver infrastructure handles everything except | 
 | 459 | 	 * Audio class messages; interface activation uses set_alt(). | 
 | 460 | 	 */ | 
 | 461 | 	switch (ctrl->bRequestType) { | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 462 | 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 463 | 		value = audio_set_intf_req(f, ctrl); | 
 | 464 | 		break; | 
 | 465 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 466 | 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 467 | 		value = audio_get_intf_req(f, ctrl); | 
 | 468 | 		break; | 
 | 469 |  | 
 | 470 | 	default: | 
 | 471 | 		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | 
 | 472 | 			ctrl->bRequestType, ctrl->bRequest, | 
 | 473 | 			w_value, w_index, w_length); | 
 | 474 | 	} | 
 | 475 |  | 
 | 476 | 	/* respond with data transfer or status phase? */ | 
 | 477 | 	if (value >= 0) { | 
 | 478 | 		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n", | 
 | 479 | 			ctrl->bRequestType, ctrl->bRequest, | 
 | 480 | 			w_value, w_index, w_length); | 
 | 481 | 		req->zero = 0; | 
 | 482 | 		req->length = value; | 
 | 483 | 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | 
 | 484 | 		if (value < 0) | 
 | 485 | 			ERROR(cdev, "audio response on err %d\n", value); | 
 | 486 | 	} | 
 | 487 |  | 
 | 488 | 	/* device either stalls (value < 0) or reports success */ | 
 | 489 | 	return value; | 
 | 490 | } | 
 | 491 |  | 
 | 492 | static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | 
 | 493 | { | 
 | 494 | 	struct f_audio		*audio = func_to_audio(f); | 
 | 495 | 	struct usb_composite_dev *cdev = f->config->cdev; | 
 | 496 | 	struct usb_ep *out_ep = audio->out_ep; | 
 | 497 | 	struct usb_request *req; | 
 | 498 | 	int i = 0, err = 0; | 
 | 499 |  | 
 | 500 | 	DBG(cdev, "intf %d, alt %d\n", intf, alt); | 
 | 501 |  | 
 | 502 | 	if (intf == 1) { | 
 | 503 | 		if (alt == 1) { | 
 | 504 | 			usb_ep_enable(out_ep, audio->out_desc); | 
 | 505 | 			out_ep->driver_data = audio; | 
 | 506 | 			audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); | 
 | 507 |  | 
 | 508 | 			/* | 
 | 509 | 			 * allocate a bunch of read buffers | 
 | 510 | 			 * and queue them all at once. | 
 | 511 | 			 */ | 
 | 512 | 			for (i = 0; i < req_count && err == 0; i++) { | 
 | 513 | 				req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); | 
 | 514 | 				if (req) { | 
 | 515 | 					req->buf = kzalloc(req_buf_size, | 
 | 516 | 							GFP_ATOMIC); | 
 | 517 | 					if (req->buf) { | 
 | 518 | 						req->length = req_buf_size; | 
 | 519 | 						req->context = audio; | 
 | 520 | 						req->complete = | 
 | 521 | 							f_audio_complete; | 
 | 522 | 						err = usb_ep_queue(out_ep, | 
 | 523 | 							req, GFP_ATOMIC); | 
 | 524 | 						if (err) | 
 | 525 | 							ERROR(cdev, | 
 | 526 | 							"%s queue req: %d\n", | 
 | 527 | 							out_ep->name, err); | 
 | 528 | 					} else | 
 | 529 | 						err = -ENOMEM; | 
 | 530 | 				} else | 
 | 531 | 					err = -ENOMEM; | 
 | 532 | 			} | 
 | 533 |  | 
 | 534 | 		} else { | 
 | 535 | 			struct f_audio_buf *copy_buf = audio->copy_buf; | 
 | 536 | 			if (copy_buf) { | 
 | 537 | 				list_add_tail(©_buf->list, | 
 | 538 | 						&audio->play_queue); | 
 | 539 | 				schedule_work(&audio->playback_work); | 
 | 540 | 			} | 
 | 541 | 		} | 
 | 542 | 	} | 
 | 543 |  | 
 | 544 | 	return err; | 
 | 545 | } | 
 | 546 |  | 
 | 547 | static void f_audio_disable(struct usb_function *f) | 
 | 548 | { | 
 | 549 | 	return; | 
 | 550 | } | 
 | 551 |  | 
 | 552 | /*-------------------------------------------------------------------------*/ | 
 | 553 |  | 
 | 554 | static void f_audio_build_desc(struct f_audio *audio) | 
 | 555 | { | 
 | 556 | 	struct gaudio *card = &audio->card; | 
 | 557 | 	u8 *sam_freq; | 
 | 558 | 	int rate; | 
 | 559 |  | 
 | 560 | 	/* Set channel numbers */ | 
 | 561 | 	input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); | 
 | 562 | 	as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); | 
 | 563 |  | 
 | 564 | 	/* Set sample rates */ | 
 | 565 | 	rate = u_audio_get_playback_rate(card); | 
 | 566 | 	sam_freq = as_type_i_desc.tSamFreq[0]; | 
 | 567 | 	memcpy(sam_freq, &rate, 3); | 
 | 568 |  | 
 | 569 | 	/* Todo: Set Sample bits and other parameters */ | 
 | 570 |  | 
 | 571 | 	return; | 
 | 572 | } | 
 | 573 |  | 
 | 574 | /* audio function driver setup/binding */ | 
 | 575 | static int __init | 
 | 576 | f_audio_bind(struct usb_configuration *c, struct usb_function *f) | 
 | 577 | { | 
 | 578 | 	struct usb_composite_dev *cdev = c->cdev; | 
 | 579 | 	struct f_audio		*audio = func_to_audio(f); | 
 | 580 | 	int			status; | 
 | 581 | 	struct usb_ep		*ep; | 
 | 582 |  | 
 | 583 | 	f_audio_build_desc(audio); | 
 | 584 |  | 
 | 585 | 	/* allocate instance-specific interface IDs, and patch descriptors */ | 
 | 586 | 	status = usb_interface_id(c, f); | 
 | 587 | 	if (status < 0) | 
 | 588 | 		goto fail; | 
 | 589 | 	ac_interface_desc.bInterfaceNumber = status; | 
 | 590 |  | 
 | 591 | 	status = usb_interface_id(c, f); | 
 | 592 | 	if (status < 0) | 
 | 593 | 		goto fail; | 
 | 594 | 	as_interface_alt_0_desc.bInterfaceNumber = status; | 
 | 595 | 	as_interface_alt_1_desc.bInterfaceNumber = status; | 
 | 596 |  | 
 | 597 | 	status = -ENODEV; | 
 | 598 |  | 
 | 599 | 	/* allocate instance-specific endpoints */ | 
 | 600 | 	ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); | 
 | 601 | 	if (!ep) | 
 | 602 | 		goto fail; | 
 | 603 | 	audio->out_ep = ep; | 
 | 604 | 	ep->driver_data = cdev;	/* claim */ | 
 | 605 |  | 
 | 606 | 	status = -ENOMEM; | 
 | 607 |  | 
 | 608 | 	/* supcard all relevant hardware speeds... we expect that when | 
 | 609 | 	 * hardware is dual speed, all bulk-capable endpoints work at | 
 | 610 | 	 * both speeds | 
 | 611 | 	 */ | 
 | 612 |  | 
 | 613 | 	/* copy descriptors, and track endpoint copies */ | 
 | 614 | 	if (gadget_is_dualspeed(c->cdev->gadget)) { | 
 | 615 | 		c->highspeed = true; | 
 | 616 | 		f->hs_descriptors = usb_copy_descriptors(f_audio_desc); | 
 | 617 | 	} else | 
 | 618 | 		f->descriptors = usb_copy_descriptors(f_audio_desc); | 
 | 619 |  | 
 | 620 | 	return 0; | 
 | 621 |  | 
 | 622 | fail: | 
 | 623 |  | 
 | 624 | 	return status; | 
 | 625 | } | 
 | 626 |  | 
 | 627 | static void | 
 | 628 | f_audio_unbind(struct usb_configuration *c, struct usb_function *f) | 
 | 629 | { | 
 | 630 | 	struct f_audio		*audio = func_to_audio(f); | 
 | 631 |  | 
 | 632 | 	usb_free_descriptors(f->descriptors); | 
 | 633 | 	kfree(audio); | 
 | 634 | } | 
 | 635 |  | 
 | 636 | /*-------------------------------------------------------------------------*/ | 
 | 637 |  | 
| Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 638 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) | 
 | 639 | { | 
 | 640 | 	con->data[cmd] = value; | 
 | 641 |  | 
 | 642 | 	return 0; | 
 | 643 | } | 
 | 644 |  | 
 | 645 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) | 
 | 646 | { | 
 | 647 | 	return con->data[cmd]; | 
 | 648 | } | 
 | 649 |  | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 650 | /* Todo: add more control selecotor dynamically */ | 
 | 651 | int __init control_selector_init(struct f_audio *audio) | 
 | 652 | { | 
 | 653 | 	INIT_LIST_HEAD(&audio->cs); | 
 | 654 | 	list_add(&feature_unit.list, &audio->cs); | 
 | 655 |  | 
 | 656 | 	INIT_LIST_HEAD(&feature_unit.control); | 
 | 657 | 	list_add(&mute_control.list, &feature_unit.control); | 
 | 658 | 	list_add(&volume_control.list, &feature_unit.control); | 
 | 659 |  | 
| Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 660 | 	volume_control.data[UAC__CUR] = 0xffc0; | 
 | 661 | 	volume_control.data[UAC__MIN] = 0xe3a0; | 
 | 662 | 	volume_control.data[UAC__MAX] = 0xfff0; | 
 | 663 | 	volume_control.data[UAC__RES] = 0x0030; | 
| Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 664 |  | 
 | 665 | 	return 0; | 
 | 666 | } | 
 | 667 |  | 
 | 668 | /** | 
 | 669 |  * audio_bind_config - add USB audio fucntion to a configuration | 
 | 670 |  * @c: the configuration to supcard the USB audio function | 
 | 671 |  * Context: single threaded during gadget setup | 
 | 672 |  * | 
 | 673 |  * Returns zero on success, else negative errno. | 
 | 674 |  */ | 
 | 675 | int __init audio_bind_config(struct usb_configuration *c) | 
 | 676 | { | 
 | 677 | 	struct f_audio *audio; | 
 | 678 | 	int status; | 
 | 679 |  | 
 | 680 | 	/* allocate and initialize one new instance */ | 
 | 681 | 	audio = kzalloc(sizeof *audio, GFP_KERNEL); | 
 | 682 | 	if (!audio) | 
 | 683 | 		return -ENOMEM; | 
 | 684 |  | 
 | 685 | 	audio->card.func.name = "g_audio"; | 
 | 686 | 	audio->card.gadget = c->cdev->gadget; | 
 | 687 |  | 
 | 688 | 	INIT_LIST_HEAD(&audio->play_queue); | 
 | 689 | 	spin_lock_init(&audio->lock); | 
 | 690 |  | 
 | 691 | 	/* set up ASLA audio devices */ | 
 | 692 | 	status = gaudio_setup(&audio->card); | 
 | 693 | 	if (status < 0) | 
 | 694 | 		goto setup_fail; | 
 | 695 |  | 
 | 696 | 	audio->card.func.strings = audio_strings; | 
 | 697 | 	audio->card.func.bind = f_audio_bind; | 
 | 698 | 	audio->card.func.unbind = f_audio_unbind; | 
 | 699 | 	audio->card.func.set_alt = f_audio_set_alt; | 
 | 700 | 	audio->card.func.setup = f_audio_setup; | 
 | 701 | 	audio->card.func.disable = f_audio_disable; | 
 | 702 | 	audio->out_desc = &as_out_ep_desc; | 
 | 703 |  | 
 | 704 | 	control_selector_init(audio); | 
 | 705 |  | 
 | 706 | 	INIT_WORK(&audio->playback_work, f_audio_playback_work); | 
 | 707 |  | 
 | 708 | 	status = usb_add_function(c, &audio->card.func); | 
 | 709 | 	if (status) | 
 | 710 | 		goto add_fail; | 
 | 711 |  | 
 | 712 | 	INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n", | 
 | 713 | 		audio_buf_size, req_buf_size, req_count); | 
 | 714 |  | 
 | 715 | 	return status; | 
 | 716 |  | 
 | 717 | add_fail: | 
 | 718 | 	gaudio_cleanup(&audio->card); | 
 | 719 | setup_fail: | 
 | 720 | 	kfree(audio); | 
 | 721 | 	return status; | 
 | 722 | } |