]>
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 | 13 | #include <linux/kernel.h> |
f3a3406b | 14 | #include <linux/module.h> |
c6994e6f | 15 | #include <linux/device.h> |
60063497 | 16 | #include <linux/atomic.h> |
c6994e6f | 17 | |
18b5b3b5 | 18 | #include "u_uac1.h" |
c6994e6f | 19 | |
b95cd7ec LP |
20 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); |
21 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); | |
22 | ||
c6994e6f BW |
23 | /* |
24 | * DESCRIPTORS ... most are static, but strings and full | |
25 | * configuration descriptors are built on demand. | |
26 | */ | |
27 | ||
28 | /* | |
29 | * We have two interfaces- AudioControl and AudioStreaming | |
30 | * TODO: only supcard playback currently | |
31 | */ | |
32 | #define F_AUDIO_AC_INTERFACE 0 | |
33 | #define F_AUDIO_AS_INTERFACE 1 | |
34 | #define F_AUDIO_NUM_INTERFACES 2 | |
35 | ||
36 | /* B.3.1 Standard AC Interface Descriptor */ | |
f3a3406b | 37 | static struct usb_interface_descriptor ac_interface_desc = { |
c6994e6f BW |
38 | .bLength = USB_DT_INTERFACE_SIZE, |
39 | .bDescriptorType = USB_DT_INTERFACE, | |
40 | .bNumEndpoints = 0, | |
41 | .bInterfaceClass = USB_CLASS_AUDIO, | |
42 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, | |
43 | }; | |
44 | ||
512ad27d | 45 | DECLARE_UAC_AC_HEADER_DESCRIPTOR(2); |
c6994e6f | 46 | |
512ad27d | 47 | #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) |
d16f1726 CC |
48 | /* 1 input terminal, 1 output terminal and 1 feature unit */ |
49 | #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \ | |
50 | + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0)) | |
c6994e6f | 51 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
69da9bcb | 52 | static struct uac1_ac_header_descriptor_2 ac_header_desc = { |
512ad27d | 53 | .bLength = UAC_DT_AC_HEADER_LENGTH, |
c6994e6f | 54 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d | 55 | .bDescriptorSubtype = UAC_HEADER, |
c6994e6f | 56 | .bcdADC = __constant_cpu_to_le16(0x0100), |
d16f1726 | 57 | .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), |
c6994e6f BW |
58 | .bInCollection = F_AUDIO_NUM_INTERFACES, |
59 | .baInterfaceNr = { | |
60 | [0] = F_AUDIO_AC_INTERFACE, | |
61 | [1] = F_AUDIO_AS_INTERFACE, | |
62 | } | |
63 | }; | |
64 | ||
65 | #define INPUT_TERMINAL_ID 1 | |
512ad27d LP |
66 | static struct uac_input_terminal_descriptor input_terminal_desc = { |
67 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, | |
c6994e6f | 68 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d | 69 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
c6994e6f | 70 | .bTerminalID = INPUT_TERMINAL_ID, |
512ad27d | 71 | .wTerminalType = UAC_TERMINAL_STREAMING, |
c6994e6f BW |
72 | .bAssocTerminal = 0, |
73 | .wChannelConfig = 0x3, | |
74 | }; | |
75 | ||
512ad27d | 76 | DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); |
c6994e6f BW |
77 | |
78 | #define FEATURE_UNIT_ID 2 | |
512ad27d LP |
79 | static struct uac_feature_unit_descriptor_0 feature_unit_desc = { |
80 | .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), | |
c6994e6f | 81 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d | 82 | .bDescriptorSubtype = UAC_FEATURE_UNIT, |
c6994e6f BW |
83 | .bUnitID = FEATURE_UNIT_ID, |
84 | .bSourceID = INPUT_TERMINAL_ID, | |
85 | .bControlSize = 2, | |
512ad27d | 86 | .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME), |
c6994e6f BW |
87 | }; |
88 | ||
89 | static struct usb_audio_control mute_control = { | |
90 | .list = LIST_HEAD_INIT(mute_control.list), | |
91 | .name = "Mute Control", | |
f07ff97b | 92 | .type = UAC_FU_MUTE, |
c6994e6f BW |
93 | /* Todo: add real Mute control code */ |
94 | .set = generic_set_cmd, | |
95 | .get = generic_get_cmd, | |
96 | }; | |
97 | ||
98 | static struct usb_audio_control volume_control = { | |
99 | .list = LIST_HEAD_INIT(volume_control.list), | |
100 | .name = "Volume Control", | |
f07ff97b | 101 | .type = UAC_FU_VOLUME, |
c6994e6f BW |
102 | /* Todo: add real Volume control code */ |
103 | .set = generic_set_cmd, | |
104 | .get = generic_get_cmd, | |
105 | }; | |
106 | ||
107 | static struct usb_audio_control_selector feature_unit = { | |
108 | .list = LIST_HEAD_INIT(feature_unit.list), | |
109 | .id = FEATURE_UNIT_ID, | |
110 | .name = "Mute & Volume Control", | |
512ad27d | 111 | .type = UAC_FEATURE_UNIT, |
c6994e6f BW |
112 | .desc = (struct usb_descriptor_header *)&feature_unit_desc, |
113 | }; | |
114 | ||
115 | #define OUTPUT_TERMINAL_ID 3 | |
69da9bcb | 116 | static struct uac1_output_terminal_descriptor output_terminal_desc = { |
512ad27d | 117 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
c6994e6f | 118 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d | 119 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
c6994e6f | 120 | .bTerminalID = OUTPUT_TERMINAL_ID, |
512ad27d | 121 | .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, |
c6994e6f BW |
122 | .bAssocTerminal = FEATURE_UNIT_ID, |
123 | .bSourceID = FEATURE_UNIT_ID, | |
124 | }; | |
125 | ||
126 | /* B.4.1 Standard AS Interface Descriptor */ | |
127 | static struct usb_interface_descriptor as_interface_alt_0_desc = { | |
128 | .bLength = USB_DT_INTERFACE_SIZE, | |
129 | .bDescriptorType = USB_DT_INTERFACE, | |
130 | .bAlternateSetting = 0, | |
131 | .bNumEndpoints = 0, | |
132 | .bInterfaceClass = USB_CLASS_AUDIO, | |
133 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
134 | }; | |
135 | ||
136 | static struct usb_interface_descriptor as_interface_alt_1_desc = { | |
137 | .bLength = USB_DT_INTERFACE_SIZE, | |
138 | .bDescriptorType = USB_DT_INTERFACE, | |
139 | .bAlternateSetting = 1, | |
140 | .bNumEndpoints = 1, | |
141 | .bInterfaceClass = USB_CLASS_AUDIO, | |
142 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
143 | }; | |
144 | ||
145 | /* B.4.2 Class-Specific AS Interface Descriptor */ | |
69da9bcb | 146 | static struct uac1_as_header_descriptor as_header_desc = { |
512ad27d | 147 | .bLength = UAC_DT_AS_HEADER_SIZE, |
c6994e6f | 148 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d | 149 | .bDescriptorSubtype = UAC_AS_GENERAL, |
c6994e6f BW |
150 | .bTerminalLink = INPUT_TERMINAL_ID, |
151 | .bDelay = 1, | |
512ad27d | 152 | .wFormatTag = UAC_FORMAT_TYPE_I_PCM, |
c6994e6f BW |
153 | }; |
154 | ||
512ad27d | 155 | DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); |
c6994e6f | 156 | |
512ad27d LP |
157 | static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { |
158 | .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), | |
c6994e6f | 159 | .bDescriptorType = USB_DT_CS_INTERFACE, |
512ad27d LP |
160 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
161 | .bFormatType = UAC_FORMAT_TYPE_I, | |
c6994e6f BW |
162 | .bSubframeSize = 2, |
163 | .bBitResolution = 16, | |
164 | .bSamFreqType = 1, | |
165 | }; | |
166 | ||
167 | /* Standard ISO OUT Endpoint Descriptor */ | |
e6251a92 | 168 | static struct usb_endpoint_descriptor as_out_ep_desc = { |
c6994e6f BW |
169 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
170 | .bDescriptorType = USB_DT_ENDPOINT, | |
171 | .bEndpointAddress = USB_DIR_OUT, | |
85e08ca5 | 172 | .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
c6994e6f | 173 | | USB_ENDPOINT_XFER_ISOC, |
bcec9784 | 174 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
c6994e6f BW |
175 | .bInterval = 4, |
176 | }; | |
177 | ||
178 | /* Class-specific AS ISO OUT Endpoint Descriptor */ | |
f3a3406b | 179 | static struct uac_iso_endpoint_descriptor as_iso_out_desc = { |
512ad27d | 180 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
c6994e6f | 181 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
512ad27d | 182 | .bDescriptorSubtype = UAC_EP_GENERAL, |
c6994e6f BW |
183 | .bmAttributes = 1, |
184 | .bLockDelayUnits = 1, | |
185 | .wLockDelay = __constant_cpu_to_le16(1), | |
186 | }; | |
187 | ||
f3a3406b | 188 | static struct usb_descriptor_header *f_audio_desc[] = { |
c6994e6f BW |
189 | (struct usb_descriptor_header *)&ac_interface_desc, |
190 | (struct usb_descriptor_header *)&ac_header_desc, | |
191 | ||
192 | (struct usb_descriptor_header *)&input_terminal_desc, | |
193 | (struct usb_descriptor_header *)&output_terminal_desc, | |
194 | (struct usb_descriptor_header *)&feature_unit_desc, | |
195 | ||
196 | (struct usb_descriptor_header *)&as_interface_alt_0_desc, | |
197 | (struct usb_descriptor_header *)&as_interface_alt_1_desc, | |
198 | (struct usb_descriptor_header *)&as_header_desc, | |
199 | ||
200 | (struct usb_descriptor_header *)&as_type_i_desc, | |
201 | ||
202 | (struct usb_descriptor_header *)&as_out_ep_desc, | |
203 | (struct usb_descriptor_header *)&as_iso_out_desc, | |
204 | NULL, | |
205 | }; | |
206 | ||
f73db69f AP |
207 | enum { |
208 | STR_AC_IF, | |
209 | STR_INPUT_TERMINAL, | |
210 | STR_INPUT_TERMINAL_CH_NAMES, | |
211 | STR_FEAT_DESC_0, | |
212 | STR_OUTPUT_TERMINAL, | |
213 | STR_AS_IF_ALT0, | |
214 | STR_AS_IF_ALT1, | |
215 | }; | |
216 | ||
217 | static struct usb_string strings_uac1[] = { | |
218 | [STR_AC_IF].s = "AC Interface", | |
219 | [STR_INPUT_TERMINAL].s = "Input terminal", | |
220 | [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels", | |
221 | [STR_FEAT_DESC_0].s = "Volume control & mute", | |
222 | [STR_OUTPUT_TERMINAL].s = "Output terminal", | |
223 | [STR_AS_IF_ALT0].s = "AS Interface", | |
224 | [STR_AS_IF_ALT1].s = "AS Interface", | |
225 | { }, | |
226 | }; | |
227 | ||
228 | static struct usb_gadget_strings str_uac1 = { | |
229 | .language = 0x0409, /* en-us */ | |
230 | .strings = strings_uac1, | |
231 | }; | |
232 | ||
233 | static struct usb_gadget_strings *uac1_strings[] = { | |
234 | &str_uac1, | |
235 | NULL, | |
236 | }; | |
237 | ||
c6994e6f BW |
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) | |
ff3b968c | 255 | return ERR_PTR(-ENOMEM); |
c6994e6f BW |
256 | |
257 | copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); | |
258 | if (!copy_buf->buf) { | |
259 | kfree(copy_buf); | |
ff3b968c | 260 | return ERR_PTR(-ENOMEM); |
c6994e6f BW |
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; | |
c6994e6f BW |
278 | |
279 | spinlock_t lock; | |
280 | struct f_audio_buf *copy_buf; | |
281 | struct work_struct playback_work; | |
282 | struct list_head play_queue; | |
283 | ||
284 | /* Control Set command */ | |
285 | struct list_head cs; | |
286 | u8 set_cmd; | |
287 | struct usb_audio_control *set_con; | |
288 | }; | |
289 | ||
290 | static inline struct f_audio *func_to_audio(struct usb_function *f) | |
291 | { | |
292 | return container_of(f, struct f_audio, card.func); | |
293 | } | |
294 | ||
295 | /*-------------------------------------------------------------------------*/ | |
296 | ||
297 | static void f_audio_playback_work(struct work_struct *data) | |
298 | { | |
299 | struct f_audio *audio = container_of(data, struct f_audio, | |
300 | playback_work); | |
301 | struct f_audio_buf *play_buf; | |
302 | ||
303 | spin_lock_irq(&audio->lock); | |
304 | if (list_empty(&audio->play_queue)) { | |
305 | spin_unlock_irq(&audio->lock); | |
306 | return; | |
307 | } | |
308 | play_buf = list_first_entry(&audio->play_queue, | |
309 | struct f_audio_buf, list); | |
310 | list_del(&play_buf->list); | |
311 | spin_unlock_irq(&audio->lock); | |
312 | ||
313 | u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); | |
314 | f_audio_buffer_free(play_buf); | |
c6994e6f BW |
315 | } |
316 | ||
317 | static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) | |
318 | { | |
319 | struct f_audio *audio = req->context; | |
320 | struct usb_composite_dev *cdev = audio->card.func.config->cdev; | |
321 | struct f_audio_buf *copy_buf = audio->copy_buf; | |
f3a3406b AP |
322 | struct f_uac1_opts *opts; |
323 | int audio_buf_size; | |
c6994e6f BW |
324 | int err; |
325 | ||
f3a3406b AP |
326 | opts = container_of(audio->card.func.fi, struct f_uac1_opts, |
327 | func_inst); | |
328 | audio_buf_size = opts->audio_buf_size; | |
605ef833 | 329 | |
c6994e6f BW |
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; | |
fddedd83 | 446 | len = min_t(size_t, sizeof(value), len); |
c6994e6f BW |
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: | |
7c5881d1 | 466 | value = len; |
0ad72524 LP |
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: | |
7c5881d1 | 505 | value = len; |
0ad72524 LP |
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; | |
f3a3406b AP |
574 | struct f_uac1_opts *opts; |
575 | int req_buf_size, req_count, audio_buf_size; | |
c6994e6f BW |
576 | int i = 0, err = 0; |
577 | ||
578 | DBG(cdev, "intf %d, alt %d\n", intf, alt); | |
579 | ||
f3a3406b AP |
580 | opts = container_of(f->fi, struct f_uac1_opts, func_inst); |
581 | req_buf_size = opts->req_buf_size; | |
582 | req_count = opts->req_count; | |
583 | audio_buf_size = opts->audio_buf_size; | |
584 | ||
c6994e6f BW |
585 | if (intf == 1) { |
586 | if (alt == 1) { | |
625763d1 | 587 | config_ep_by_speed(cdev->gadget, f, out_ep); |
72c973dd | 588 | usb_ep_enable(out_ep); |
c6994e6f BW |
589 | out_ep->driver_data = audio; |
590 | audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); | |
ff3b968c JL |
591 | if (IS_ERR(audio->copy_buf)) |
592 | return -ENOMEM; | |
c6994e6f BW |
593 | |
594 | /* | |
595 | * allocate a bunch of read buffers | |
596 | * and queue them all at once. | |
597 | */ | |
598 | for (i = 0; i < req_count && err == 0; i++) { | |
599 | req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); | |
600 | if (req) { | |
601 | req->buf = kzalloc(req_buf_size, | |
602 | GFP_ATOMIC); | |
603 | if (req->buf) { | |
604 | req->length = req_buf_size; | |
605 | req->context = audio; | |
606 | req->complete = | |
607 | f_audio_complete; | |
608 | err = usb_ep_queue(out_ep, | |
609 | req, GFP_ATOMIC); | |
610 | if (err) | |
611 | ERROR(cdev, | |
612 | "%s queue req: %d\n", | |
613 | out_ep->name, err); | |
614 | } else | |
615 | err = -ENOMEM; | |
616 | } else | |
617 | err = -ENOMEM; | |
618 | } | |
619 | ||
620 | } else { | |
621 | struct f_audio_buf *copy_buf = audio->copy_buf; | |
622 | if (copy_buf) { | |
623 | list_add_tail(©_buf->list, | |
624 | &audio->play_queue); | |
625 | schedule_work(&audio->playback_work); | |
626 | } | |
627 | } | |
628 | } | |
629 | ||
630 | return err; | |
631 | } | |
632 | ||
633 | static void f_audio_disable(struct usb_function *f) | |
634 | { | |
635 | return; | |
636 | } | |
637 | ||
638 | /*-------------------------------------------------------------------------*/ | |
639 | ||
640 | static void f_audio_build_desc(struct f_audio *audio) | |
641 | { | |
642 | struct gaudio *card = &audio->card; | |
643 | u8 *sam_freq; | |
644 | int rate; | |
645 | ||
646 | /* Set channel numbers */ | |
647 | input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); | |
648 | as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); | |
649 | ||
650 | /* Set sample rates */ | |
651 | rate = u_audio_get_playback_rate(card); | |
652 | sam_freq = as_type_i_desc.tSamFreq[0]; | |
653 | memcpy(sam_freq, &rate, 3); | |
654 | ||
655 | /* Todo: Set Sample bits and other parameters */ | |
656 | ||
657 | return; | |
658 | } | |
659 | ||
660 | /* audio function driver setup/binding */ | |
f3a3406b | 661 | static int |
c6994e6f BW |
662 | f_audio_bind(struct usb_configuration *c, struct usb_function *f) |
663 | { | |
664 | struct usb_composite_dev *cdev = c->cdev; | |
665 | struct f_audio *audio = func_to_audio(f); | |
807dccdb | 666 | struct usb_string *us; |
c6994e6f | 667 | int status; |
10287bae | 668 | struct usb_ep *ep = NULL; |
f3a3406b AP |
669 | struct f_uac1_opts *audio_opts; |
670 | ||
671 | audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); | |
672 | audio->card.gadget = c->cdev->gadget; | |
673 | audio_opts->card = &audio->card; | |
674 | /* set up ASLA audio devices */ | |
675 | if (!audio_opts->bound) { | |
676 | status = gaudio_setup(&audio->card); | |
677 | if (status < 0) | |
678 | return status; | |
679 | audio_opts->bound = true; | |
680 | } | |
807dccdb AP |
681 | us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); |
682 | if (IS_ERR(us)) | |
683 | return PTR_ERR(us); | |
684 | ac_interface_desc.iInterface = us[STR_AC_IF].id; | |
685 | input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id; | |
686 | input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id; | |
687 | feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id; | |
688 | output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id; | |
689 | as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id; | |
690 | as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id; | |
691 | ||
c6994e6f BW |
692 | |
693 | f_audio_build_desc(audio); | |
694 | ||
695 | /* allocate instance-specific interface IDs, and patch descriptors */ | |
696 | status = usb_interface_id(c, f); | |
697 | if (status < 0) | |
698 | goto fail; | |
699 | ac_interface_desc.bInterfaceNumber = status; | |
700 | ||
701 | status = usb_interface_id(c, f); | |
702 | if (status < 0) | |
703 | goto fail; | |
704 | as_interface_alt_0_desc.bInterfaceNumber = status; | |
705 | as_interface_alt_1_desc.bInterfaceNumber = status; | |
706 | ||
707 | status = -ENODEV; | |
708 | ||
709 | /* allocate instance-specific endpoints */ | |
710 | ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); | |
711 | if (!ep) | |
712 | goto fail; | |
713 | audio->out_ep = ep; | |
72c973dd | 714 | audio->out_ep->desc = &as_out_ep_desc; |
c6994e6f BW |
715 | ep->driver_data = cdev; /* claim */ |
716 | ||
717 | status = -ENOMEM; | |
718 | ||
ef7f584c | 719 | /* copy descriptors, and track endpoint copies */ |
10287bae SAS |
720 | status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL); |
721 | if (status) | |
722 | goto fail; | |
c6994e6f BW |
723 | return 0; |
724 | ||
725 | fail: | |
f3a3406b | 726 | gaudio_cleanup(&audio->card); |
10287bae SAS |
727 | if (ep) |
728 | ep->driver_data = NULL; | |
c6994e6f BW |
729 | return status; |
730 | } | |
731 | ||
c6994e6f BW |
732 | /*-------------------------------------------------------------------------*/ |
733 | ||
b95cd7ec LP |
734 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) |
735 | { | |
736 | con->data[cmd] = value; | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
741 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) | |
742 | { | |
743 | return con->data[cmd]; | |
744 | } | |
745 | ||
c6994e6f | 746 | /* Todo: add more control selecotor dynamically */ |
f3a3406b | 747 | static int control_selector_init(struct f_audio *audio) |
c6994e6f BW |
748 | { |
749 | INIT_LIST_HEAD(&audio->cs); | |
750 | list_add(&feature_unit.list, &audio->cs); | |
751 | ||
752 | INIT_LIST_HEAD(&feature_unit.control); | |
753 | list_add(&mute_control.list, &feature_unit.control); | |
754 | list_add(&volume_control.list, &feature_unit.control); | |
755 | ||
512ad27d LP |
756 | volume_control.data[UAC__CUR] = 0xffc0; |
757 | volume_control.data[UAC__MIN] = 0xe3a0; | |
758 | volume_control.data[UAC__MAX] = 0xfff0; | |
759 | volume_control.data[UAC__RES] = 0x0030; | |
c6994e6f BW |
760 | |
761 | return 0; | |
762 | } | |
763 | ||
0854611a AP |
764 | static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item) |
765 | { | |
766 | return container_of(to_config_group(item), struct f_uac1_opts, | |
767 | func_inst.group); | |
768 | } | |
769 | ||
770 | CONFIGFS_ATTR_STRUCT(f_uac1_opts); | |
771 | CONFIGFS_ATTR_OPS(f_uac1_opts); | |
772 | ||
773 | static void f_uac1_attr_release(struct config_item *item) | |
774 | { | |
775 | struct f_uac1_opts *opts = to_f_uac1_opts(item); | |
776 | ||
777 | usb_put_function_instance(&opts->func_inst); | |
778 | } | |
779 | ||
780 | static struct configfs_item_operations f_uac1_item_ops = { | |
781 | .release = f_uac1_attr_release, | |
782 | .show_attribute = f_uac1_opts_attr_show, | |
783 | .store_attribute = f_uac1_opts_attr_store, | |
784 | }; | |
785 | ||
786 | #define UAC1_INT_ATTRIBUTE(name) \ | |
787 | static ssize_t f_uac1_opts_##name##_show(struct f_uac1_opts *opts, \ | |
788 | char *page) \ | |
789 | { \ | |
790 | int result; \ | |
791 | \ | |
792 | mutex_lock(&opts->lock); \ | |
793 | result = sprintf(page, "%u\n", opts->name); \ | |
794 | mutex_unlock(&opts->lock); \ | |
795 | \ | |
796 | return result; \ | |
797 | } \ | |
798 | \ | |
799 | static ssize_t f_uac1_opts_##name##_store(struct f_uac1_opts *opts, \ | |
800 | const char *page, size_t len) \ | |
801 | { \ | |
802 | int ret; \ | |
803 | u32 num; \ | |
804 | \ | |
805 | mutex_lock(&opts->lock); \ | |
806 | if (opts->refcnt) { \ | |
807 | ret = -EBUSY; \ | |
808 | goto end; \ | |
809 | } \ | |
810 | \ | |
811 | ret = kstrtou32(page, 0, &num); \ | |
812 | if (ret) \ | |
813 | goto end; \ | |
814 | \ | |
815 | opts->name = num; \ | |
816 | ret = len; \ | |
817 | \ | |
818 | end: \ | |
819 | mutex_unlock(&opts->lock); \ | |
820 | return ret; \ | |
821 | } \ | |
822 | \ | |
823 | static struct f_uac1_opts_attribute f_uac1_opts_##name = \ | |
824 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
825 | f_uac1_opts_##name##_show, \ | |
826 | f_uac1_opts_##name##_store) | |
827 | ||
828 | UAC1_INT_ATTRIBUTE(req_buf_size); | |
829 | UAC1_INT_ATTRIBUTE(req_count); | |
830 | UAC1_INT_ATTRIBUTE(audio_buf_size); | |
831 | ||
832 | #define UAC1_STR_ATTRIBUTE(name) \ | |
833 | static ssize_t f_uac1_opts_##name##_show(struct f_uac1_opts *opts, \ | |
834 | char *page) \ | |
835 | { \ | |
836 | int result; \ | |
837 | \ | |
838 | mutex_lock(&opts->lock); \ | |
839 | result = sprintf(page, "%s\n", opts->name); \ | |
840 | mutex_unlock(&opts->lock); \ | |
841 | \ | |
842 | return result; \ | |
843 | } \ | |
844 | \ | |
845 | static ssize_t f_uac1_opts_##name##_store(struct f_uac1_opts *opts, \ | |
846 | const char *page, size_t len) \ | |
847 | { \ | |
848 | int ret = -EBUSY; \ | |
849 | char *tmp; \ | |
850 | \ | |
851 | mutex_lock(&opts->lock); \ | |
852 | if (opts->refcnt) \ | |
853 | goto end; \ | |
854 | \ | |
855 | tmp = kstrndup(page, len, GFP_KERNEL); \ | |
856 | if (tmp) { \ | |
857 | ret = -ENOMEM; \ | |
858 | goto end; \ | |
859 | } \ | |
860 | if (opts->name##_alloc) \ | |
861 | kfree(opts->name); \ | |
862 | opts->name##_alloc = true; \ | |
863 | opts->name = tmp; \ | |
864 | ret = len; \ | |
865 | \ | |
866 | end: \ | |
867 | mutex_unlock(&opts->lock); \ | |
868 | return ret; \ | |
869 | } \ | |
870 | \ | |
871 | static struct f_uac1_opts_attribute f_uac1_opts_##name = \ | |
872 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
873 | f_uac1_opts_##name##_show, \ | |
874 | f_uac1_opts_##name##_store) | |
875 | ||
876 | UAC1_STR_ATTRIBUTE(fn_play); | |
877 | UAC1_STR_ATTRIBUTE(fn_cap); | |
878 | UAC1_STR_ATTRIBUTE(fn_cntl); | |
879 | ||
880 | static struct configfs_attribute *f_uac1_attrs[] = { | |
881 | &f_uac1_opts_req_buf_size.attr, | |
882 | &f_uac1_opts_req_count.attr, | |
883 | &f_uac1_opts_audio_buf_size.attr, | |
884 | &f_uac1_opts_fn_play.attr, | |
885 | &f_uac1_opts_fn_cap.attr, | |
886 | &f_uac1_opts_fn_cntl.attr, | |
887 | NULL, | |
888 | }; | |
889 | ||
890 | static struct config_item_type f_uac1_func_type = { | |
891 | .ct_item_ops = &f_uac1_item_ops, | |
892 | .ct_attrs = f_uac1_attrs, | |
893 | .ct_owner = THIS_MODULE, | |
894 | }; | |
895 | ||
f3a3406b AP |
896 | static void f_audio_free_inst(struct usb_function_instance *f) |
897 | { | |
898 | struct f_uac1_opts *opts; | |
899 | ||
900 | opts = container_of(f, struct f_uac1_opts, func_inst); | |
901 | gaudio_cleanup(opts->card); | |
0854611a AP |
902 | if (opts->fn_play_alloc) |
903 | kfree(opts->fn_play); | |
904 | if (opts->fn_cap_alloc) | |
905 | kfree(opts->fn_cap); | |
906 | if (opts->fn_cntl_alloc) | |
907 | kfree(opts->fn_cntl); | |
f3a3406b AP |
908 | kfree(opts); |
909 | } | |
910 | ||
911 | static struct usb_function_instance *f_audio_alloc_inst(void) | |
912 | { | |
913 | struct f_uac1_opts *opts; | |
914 | ||
915 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
916 | if (!opts) | |
917 | return ERR_PTR(-ENOMEM); | |
918 | ||
0854611a | 919 | mutex_init(&opts->lock); |
f3a3406b AP |
920 | opts->func_inst.free_func_inst = f_audio_free_inst; |
921 | ||
0854611a AP |
922 | config_group_init_type_name(&opts->func_inst.group, "", |
923 | &f_uac1_func_type); | |
924 | ||
925 | opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; | |
926 | opts->req_count = UAC1_REQ_COUNT; | |
927 | opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; | |
928 | opts->fn_play = FILE_PCM_PLAYBACK; | |
929 | opts->fn_cap = FILE_PCM_CAPTURE; | |
930 | opts->fn_cntl = FILE_CONTROL; | |
f3a3406b AP |
931 | return &opts->func_inst; |
932 | } | |
933 | ||
934 | static void f_audio_free(struct usb_function *f) | |
935 | { | |
936 | struct f_audio *audio = func_to_audio(f); | |
0854611a | 937 | struct f_uac1_opts *opts; |
f3a3406b | 938 | |
0854611a | 939 | opts = container_of(f->fi, struct f_uac1_opts, func_inst); |
f3a3406b | 940 | kfree(audio); |
0854611a AP |
941 | mutex_lock(&opts->lock); |
942 | --opts->refcnt; | |
943 | mutex_unlock(&opts->lock); | |
f3a3406b AP |
944 | } |
945 | ||
946 | static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) | |
947 | { | |
948 | usb_free_all_descriptors(f); | |
949 | } | |
950 | ||
951 | static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) | |
952 | { | |
953 | struct f_audio *audio; | |
0854611a | 954 | struct f_uac1_opts *opts; |
f3a3406b AP |
955 | |
956 | /* allocate and initialize one new instance */ | |
957 | audio = kzalloc(sizeof(*audio), GFP_KERNEL); | |
958 | if (!audio) | |
959 | return ERR_PTR(-ENOMEM); | |
960 | ||
961 | audio->card.func.name = "g_audio"; | |
962 | ||
0854611a AP |
963 | opts = container_of(fi, struct f_uac1_opts, func_inst); |
964 | mutex_lock(&opts->lock); | |
965 | ++opts->refcnt; | |
966 | mutex_unlock(&opts->lock); | |
f3a3406b AP |
967 | INIT_LIST_HEAD(&audio->play_queue); |
968 | spin_lock_init(&audio->lock); | |
969 | ||
f3a3406b AP |
970 | audio->card.func.bind = f_audio_bind; |
971 | audio->card.func.unbind = f_audio_unbind; | |
972 | audio->card.func.set_alt = f_audio_set_alt; | |
973 | audio->card.func.setup = f_audio_setup; | |
974 | audio->card.func.disable = f_audio_disable; | |
975 | audio->card.func.free_func = f_audio_free; | |
976 | ||
977 | control_selector_init(audio); | |
978 | ||
979 | INIT_WORK(&audio->playback_work, f_audio_playback_work); | |
980 | ||
981 | return &audio->card.func; | |
982 | } | |
983 | ||
984 | DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc); | |
985 | MODULE_LICENSE("GPL"); | |
986 | MODULE_AUTHOR("Bryan Wu"); |