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