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