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