blob: f570e667a640d0619cd6abaffaf9705738fcab24 [file] [log] [blame]
Bryan Wuc6994e62009-06-03 09:17:58 -04001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Bryan Wuc6994e62009-06-03 09:17:58 -040013#include <linux/kernel.h>
14#include <linux/device.h>
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Bryan Wuc6994e62009-06-03 09:17:58 -040016
Jassi Brar18b5b3b2012-02-02 21:59:53 +053017#include "u_uac1.h"
Bryan Wuc6994e62009-06-03 09:17:58 -040018
19#define OUT_EP_MAX_PACKET_SIZE 200
20static int req_buf_size = OUT_EP_MAX_PACKET_SIZE;
21module_param(req_buf_size, int, S_IRUGO);
22MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
23
24static int req_count = 256;
25module_param(req_count, int, S_IRUGO);
26MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
27
28static int audio_buf_size = 48000;
29module_param(audio_buf_size, int, S_IRUGO);
30MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
31
Laurent Pinchartb95cd7e2009-06-21 23:21:55 +020032static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
33static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
34
Bryan Wuc6994e62009-06-03 09:17:58 -040035/*
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 */
49static 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
Laurent Pinchart512ad272009-06-21 23:23:05 +020057DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
Bryan Wuc6994e62009-06-03 09:17:58 -040058
Laurent Pinchart512ad272009-06-21 23:23:05 +020059#define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
Cliff Caid16f1722009-12-09 22:21:12 -050060/* 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))
Bryan Wuc6994e62009-06-03 09:17:58 -040063/* B.3.2 Class-Specific AC Interface Descriptor */
Daniel Mack69da9bc2010-06-16 17:57:28 +020064static struct uac1_ac_header_descriptor_2 ac_header_desc = {
Laurent Pinchart512ad272009-06-21 23:23:05 +020065 .bLength = UAC_DT_AC_HEADER_LENGTH,
Bryan Wuc6994e62009-06-03 09:17:58 -040066 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +020067 .bDescriptorSubtype = UAC_HEADER,
Bryan Wuc6994e62009-06-03 09:17:58 -040068 .bcdADC = __constant_cpu_to_le16(0x0100),
Cliff Caid16f1722009-12-09 22:21:12 -050069 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
Bryan Wuc6994e62009-06-03 09:17:58 -040070 .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
Laurent Pinchart512ad272009-06-21 23:23:05 +020078static struct uac_input_terminal_descriptor input_terminal_desc = {
79 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
Bryan Wuc6994e62009-06-03 09:17:58 -040080 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +020081 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
Bryan Wuc6994e62009-06-03 09:17:58 -040082 .bTerminalID = INPUT_TERMINAL_ID,
Laurent Pinchart512ad272009-06-21 23:23:05 +020083 .wTerminalType = UAC_TERMINAL_STREAMING,
Bryan Wuc6994e62009-06-03 09:17:58 -040084 .bAssocTerminal = 0,
85 .wChannelConfig = 0x3,
86};
87
Laurent Pinchart512ad272009-06-21 23:23:05 +020088DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
Bryan Wuc6994e62009-06-03 09:17:58 -040089
90#define FEATURE_UNIT_ID 2
Laurent Pinchart512ad272009-06-21 23:23:05 +020091static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
92 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
Bryan Wuc6994e62009-06-03 09:17:58 -040093 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +020094 .bDescriptorSubtype = UAC_FEATURE_UNIT,
Bryan Wuc6994e62009-06-03 09:17:58 -040095 .bUnitID = FEATURE_UNIT_ID,
96 .bSourceID = INPUT_TERMINAL_ID,
97 .bControlSize = 2,
Laurent Pinchart512ad272009-06-21 23:23:05 +020098 .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
Bryan Wuc6994e62009-06-03 09:17:58 -040099};
100
101static struct usb_audio_control mute_control = {
102 .list = LIST_HEAD_INIT(mute_control.list),
103 .name = "Mute Control",
Takashi Iwaif07ff972010-06-01 07:42:03 +0200104 .type = UAC_FU_MUTE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400105 /* Todo: add real Mute control code */
106 .set = generic_set_cmd,
107 .get = generic_get_cmd,
108};
109
110static struct usb_audio_control volume_control = {
111 .list = LIST_HEAD_INIT(volume_control.list),
112 .name = "Volume Control",
Takashi Iwaif07ff972010-06-01 07:42:03 +0200113 .type = UAC_FU_VOLUME,
Bryan Wuc6994e62009-06-03 09:17:58 -0400114 /* Todo: add real Volume control code */
115 .set = generic_set_cmd,
116 .get = generic_get_cmd,
117};
118
119static 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",
Laurent Pinchart512ad272009-06-21 23:23:05 +0200123 .type = UAC_FEATURE_UNIT,
Bryan Wuc6994e62009-06-03 09:17:58 -0400124 .desc = (struct usb_descriptor_header *)&feature_unit_desc,
125};
126
127#define OUTPUT_TERMINAL_ID 3
Daniel Mack69da9bc2010-06-16 17:57:28 +0200128static struct uac1_output_terminal_descriptor output_terminal_desc = {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200129 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400130 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200131 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
Bryan Wuc6994e62009-06-03 09:17:58 -0400132 .bTerminalID = OUTPUT_TERMINAL_ID,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200133 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
Bryan Wuc6994e62009-06-03 09:17:58 -0400134 .bAssocTerminal = FEATURE_UNIT_ID,
135 .bSourceID = FEATURE_UNIT_ID,
136};
137
138/* B.4.1 Standard AS Interface Descriptor */
139static 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
148static 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 */
Daniel Mack69da9bc2010-06-16 17:57:28 +0200158static struct uac1_as_header_descriptor as_header_desc = {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200159 .bLength = UAC_DT_AS_HEADER_SIZE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400160 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200161 .bDescriptorSubtype = UAC_AS_GENERAL,
Bryan Wuc6994e62009-06-03 09:17:58 -0400162 .bTerminalLink = INPUT_TERMINAL_ID,
163 .bDelay = 1,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200164 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
Bryan Wuc6994e62009-06-03 09:17:58 -0400165};
166
Laurent Pinchart512ad272009-06-21 23:23:05 +0200167DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
Bryan Wuc6994e62009-06-03 09:17:58 -0400168
Laurent Pinchart512ad272009-06-21 23:23:05 +0200169static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
170 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
Bryan Wuc6994e62009-06-03 09:17:58 -0400171 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200172 .bDescriptorSubtype = UAC_FORMAT_TYPE,
173 .bFormatType = UAC_FORMAT_TYPE_I,
Bryan Wuc6994e62009-06-03 09:17:58 -0400174 .bSubframeSize = 2,
175 .bBitResolution = 16,
176 .bSamFreqType = 1,
177};
178
179/* Standard ISO OUT Endpoint Descriptor */
Martin Jacksone6251a92011-05-17 11:02:06 +0200180static struct usb_endpoint_descriptor as_out_ep_desc = {
Bryan Wuc6994e62009-06-03 09:17:58 -0400181 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT,
Laurent Pinchart85e08ca2009-06-21 23:19:23 +0200184 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
Bryan Wuc6994e62009-06-03 09:17:58 -0400185 | 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 */
Laurent Pinchart512ad272009-06-21 23:23:05 +0200191static struct uac_iso_endpoint_descriptor as_iso_out_desc __initdata = {
192 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400193 .bDescriptorType = USB_DT_CS_ENDPOINT,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200194 .bDescriptorSubtype = UAC_EP_GENERAL,
Bryan Wuc6994e62009-06-03 09:17:58 -0400195 .bmAttributes = 1,
196 .bLockDelayUnits = 1,
197 .wLockDelay = __constant_cpu_to_le16(1),
198};
199
200static 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
Bryan Wuc6994e62009-06-03 09:17:58 -0400219/*
220 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
221 */
222
223/*-------------------------------------------------------------------------*/
224struct f_audio_buf {
225 u8 *buf;
226 int actual;
227 struct list_head list;
228};
229
230static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
231{
232 struct f_audio_buf *copy_buf;
233
234 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
235 if (!copy_buf)
Julia Lawallff3b9682009-12-09 14:23:32 +0100236 return ERR_PTR(-ENOMEM);
Bryan Wuc6994e62009-06-03 09:17:58 -0400237
238 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
239 if (!copy_buf->buf) {
240 kfree(copy_buf);
Julia Lawallff3b9682009-12-09 14:23:32 +0100241 return ERR_PTR(-ENOMEM);
Bryan Wuc6994e62009-06-03 09:17:58 -0400242 }
243
244 return copy_buf;
245}
246
247static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
248{
249 kfree(audio_buf->buf);
250 kfree(audio_buf);
251}
252/*-------------------------------------------------------------------------*/
253
254struct f_audio {
255 struct gaudio card;
256
257 /* endpoints handle full and/or high speeds */
258 struct usb_ep *out_ep;
Bryan Wuc6994e62009-06-03 09:17:58 -0400259
260 spinlock_t lock;
261 struct f_audio_buf *copy_buf;
262 struct work_struct playback_work;
263 struct list_head play_queue;
264
265 /* Control Set command */
266 struct list_head cs;
267 u8 set_cmd;
268 struct usb_audio_control *set_con;
269};
270
271static inline struct f_audio *func_to_audio(struct usb_function *f)
272{
273 return container_of(f, struct f_audio, card.func);
274}
275
276/*-------------------------------------------------------------------------*/
277
278static void f_audio_playback_work(struct work_struct *data)
279{
280 struct f_audio *audio = container_of(data, struct f_audio,
281 playback_work);
282 struct f_audio_buf *play_buf;
283
284 spin_lock_irq(&audio->lock);
285 if (list_empty(&audio->play_queue)) {
286 spin_unlock_irq(&audio->lock);
287 return;
288 }
289 play_buf = list_first_entry(&audio->play_queue,
290 struct f_audio_buf, list);
291 list_del(&play_buf->list);
292 spin_unlock_irq(&audio->lock);
293
294 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
295 f_audio_buffer_free(play_buf);
Bryan Wuc6994e62009-06-03 09:17:58 -0400296}
297
298static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
299{
300 struct f_audio *audio = req->context;
301 struct usb_composite_dev *cdev = audio->card.func.config->cdev;
302 struct f_audio_buf *copy_buf = audio->copy_buf;
303 int err;
304
305 if (!copy_buf)
306 return -EINVAL;
307
308 /* Copy buffer is full, add it to the play_queue */
309 if (audio_buf_size - copy_buf->actual < req->actual) {
310 list_add_tail(&copy_buf->list, &audio->play_queue);
311 schedule_work(&audio->playback_work);
312 copy_buf = f_audio_buffer_alloc(audio_buf_size);
Julia Lawallff3b9682009-12-09 14:23:32 +0100313 if (IS_ERR(copy_buf))
Bryan Wuc6994e62009-06-03 09:17:58 -0400314 return -ENOMEM;
315 }
316
317 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
318 copy_buf->actual += req->actual;
319 audio->copy_buf = copy_buf;
320
321 err = usb_ep_queue(ep, req, GFP_ATOMIC);
322 if (err)
323 ERROR(cdev, "%s queue req: %d\n", ep->name, err);
324
325 return 0;
326
327}
328
329static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
330{
331 struct f_audio *audio = req->context;
332 int status = req->status;
333 u32 data = 0;
334 struct usb_ep *out_ep = audio->out_ep;
335
336 switch (status) {
337
338 case 0: /* normal completion? */
339 if (ep == out_ep)
340 f_audio_out_ep_complete(ep, req);
341 else if (audio->set_con) {
342 memcpy(&data, req->buf, req->length);
343 audio->set_con->set(audio->set_con, audio->set_cmd,
344 le16_to_cpu(data));
345 audio->set_con = NULL;
346 }
347 break;
348 default:
349 break;
350 }
351}
352
353static int audio_set_intf_req(struct usb_function *f,
354 const struct usb_ctrlrequest *ctrl)
355{
356 struct f_audio *audio = func_to_audio(f);
357 struct usb_composite_dev *cdev = f->config->cdev;
358 struct usb_request *req = cdev->req;
359 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
360 u16 len = le16_to_cpu(ctrl->wLength);
361 u16 w_value = le16_to_cpu(ctrl->wValue);
362 u8 con_sel = (w_value >> 8) & 0xFF;
363 u8 cmd = (ctrl->bRequest & 0x0F);
364 struct usb_audio_control_selector *cs;
365 struct usb_audio_control *con;
366
367 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
368 ctrl->bRequest, w_value, len, id);
369
370 list_for_each_entry(cs, &audio->cs, list) {
371 if (cs->id == id) {
372 list_for_each_entry(con, &cs->control, list) {
373 if (con->type == con_sel) {
374 audio->set_con = con;
375 break;
376 }
377 }
378 break;
379 }
380 }
381
382 audio->set_cmd = cmd;
383 req->context = audio;
384 req->complete = f_audio_complete;
385
386 return len;
387}
388
389static int audio_get_intf_req(struct usb_function *f,
390 const struct usb_ctrlrequest *ctrl)
391{
392 struct f_audio *audio = func_to_audio(f);
393 struct usb_composite_dev *cdev = f->config->cdev;
394 struct usb_request *req = cdev->req;
395 int value = -EOPNOTSUPP;
396 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
397 u16 len = le16_to_cpu(ctrl->wLength);
398 u16 w_value = le16_to_cpu(ctrl->wValue);
399 u8 con_sel = (w_value >> 8) & 0xFF;
400 u8 cmd = (ctrl->bRequest & 0x0F);
401 struct usb_audio_control_selector *cs;
402 struct usb_audio_control *con;
403
404 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
405 ctrl->bRequest, w_value, len, id);
406
407 list_for_each_entry(cs, &audio->cs, list) {
408 if (cs->id == id) {
409 list_for_each_entry(con, &cs->control, list) {
410 if (con->type == con_sel && con->get) {
411 value = con->get(con, cmd);
412 break;
413 }
414 }
415 break;
416 }
417 }
418
419 req->context = audio;
420 req->complete = f_audio_complete;
421 memcpy(req->buf, &value, len);
422
423 return len;
424}
425
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200426static int audio_set_endpoint_req(struct usb_function *f,
427 const struct usb_ctrlrequest *ctrl)
428{
429 struct usb_composite_dev *cdev = f->config->cdev;
430 int value = -EOPNOTSUPP;
431 u16 ep = le16_to_cpu(ctrl->wIndex);
432 u16 len = le16_to_cpu(ctrl->wLength);
433 u16 w_value = le16_to_cpu(ctrl->wValue);
434
435 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
436 ctrl->bRequest, w_value, len, ep);
437
438 switch (ctrl->bRequest) {
439 case UAC_SET_CUR:
Felipe Balbi7c5881d2011-08-29 11:54:08 +0300440 value = len;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200441 break;
442
443 case UAC_SET_MIN:
444 break;
445
446 case UAC_SET_MAX:
447 break;
448
449 case UAC_SET_RES:
450 break;
451
452 case UAC_SET_MEM:
453 break;
454
455 default:
456 break;
457 }
458
459 return value;
460}
461
462static int audio_get_endpoint_req(struct usb_function *f,
463 const struct usb_ctrlrequest *ctrl)
464{
465 struct usb_composite_dev *cdev = f->config->cdev;
466 int value = -EOPNOTSUPP;
467 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
468 u16 len = le16_to_cpu(ctrl->wLength);
469 u16 w_value = le16_to_cpu(ctrl->wValue);
470
471 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
472 ctrl->bRequest, w_value, len, ep);
473
474 switch (ctrl->bRequest) {
475 case UAC_GET_CUR:
476 case UAC_GET_MIN:
477 case UAC_GET_MAX:
478 case UAC_GET_RES:
Felipe Balbi7c5881d2011-08-29 11:54:08 +0300479 value = len;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200480 break;
481 case UAC_GET_MEM:
482 break;
483 default:
484 break;
485 }
486
487 return value;
488}
489
Bryan Wuc6994e62009-06-03 09:17:58 -0400490static int
491f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
492{
493 struct usb_composite_dev *cdev = f->config->cdev;
494 struct usb_request *req = cdev->req;
495 int value = -EOPNOTSUPP;
496 u16 w_index = le16_to_cpu(ctrl->wIndex);
497 u16 w_value = le16_to_cpu(ctrl->wValue);
498 u16 w_length = le16_to_cpu(ctrl->wLength);
499
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200500 /* composite driver infrastructure handles everything; interface
501 * activation uses set_alt().
Bryan Wuc6994e62009-06-03 09:17:58 -0400502 */
503 switch (ctrl->bRequestType) {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200504 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Bryan Wuc6994e62009-06-03 09:17:58 -0400505 value = audio_set_intf_req(f, ctrl);
506 break;
507
Laurent Pinchart512ad272009-06-21 23:23:05 +0200508 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Bryan Wuc6994e62009-06-03 09:17:58 -0400509 value = audio_get_intf_req(f, ctrl);
510 break;
511
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200512 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
513 value = audio_set_endpoint_req(f, ctrl);
514 break;
515
516 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
517 value = audio_get_endpoint_req(f, ctrl);
518 break;
519
Bryan Wuc6994e62009-06-03 09:17:58 -0400520 default:
521 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
522 ctrl->bRequestType, ctrl->bRequest,
523 w_value, w_index, w_length);
524 }
525
526 /* respond with data transfer or status phase? */
527 if (value >= 0) {
528 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
529 ctrl->bRequestType, ctrl->bRequest,
530 w_value, w_index, w_length);
531 req->zero = 0;
532 req->length = value;
533 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
534 if (value < 0)
535 ERROR(cdev, "audio response on err %d\n", value);
536 }
537
538 /* device either stalls (value < 0) or reports success */
539 return value;
540}
541
542static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
543{
544 struct f_audio *audio = func_to_audio(f);
545 struct usb_composite_dev *cdev = f->config->cdev;
546 struct usb_ep *out_ep = audio->out_ep;
547 struct usb_request *req;
548 int i = 0, err = 0;
549
550 DBG(cdev, "intf %d, alt %d\n", intf, alt);
551
552 if (intf == 1) {
553 if (alt == 1) {
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300554 usb_ep_enable(out_ep);
Bryan Wuc6994e62009-06-03 09:17:58 -0400555 out_ep->driver_data = audio;
556 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
Julia Lawallff3b9682009-12-09 14:23:32 +0100557 if (IS_ERR(audio->copy_buf))
558 return -ENOMEM;
Bryan Wuc6994e62009-06-03 09:17:58 -0400559
560 /*
561 * allocate a bunch of read buffers
562 * and queue them all at once.
563 */
564 for (i = 0; i < req_count && err == 0; i++) {
565 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
566 if (req) {
567 req->buf = kzalloc(req_buf_size,
568 GFP_ATOMIC);
569 if (req->buf) {
570 req->length = req_buf_size;
571 req->context = audio;
572 req->complete =
573 f_audio_complete;
574 err = usb_ep_queue(out_ep,
575 req, GFP_ATOMIC);
576 if (err)
577 ERROR(cdev,
578 "%s queue req: %d\n",
579 out_ep->name, err);
580 } else
581 err = -ENOMEM;
582 } else
583 err = -ENOMEM;
584 }
585
586 } else {
587 struct f_audio_buf *copy_buf = audio->copy_buf;
588 if (copy_buf) {
589 list_add_tail(&copy_buf->list,
590 &audio->play_queue);
591 schedule_work(&audio->playback_work);
592 }
593 }
594 }
595
596 return err;
597}
598
599static void f_audio_disable(struct usb_function *f)
600{
601 return;
602}
603
604/*-------------------------------------------------------------------------*/
605
606static void f_audio_build_desc(struct f_audio *audio)
607{
608 struct gaudio *card = &audio->card;
609 u8 *sam_freq;
610 int rate;
611
612 /* Set channel numbers */
613 input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
614 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
615
616 /* Set sample rates */
617 rate = u_audio_get_playback_rate(card);
618 sam_freq = as_type_i_desc.tSamFreq[0];
619 memcpy(sam_freq, &rate, 3);
620
621 /* Todo: Set Sample bits and other parameters */
622
623 return;
624}
625
626/* audio function driver setup/binding */
627static int __init
628f_audio_bind(struct usb_configuration *c, struct usb_function *f)
629{
630 struct usb_composite_dev *cdev = c->cdev;
631 struct f_audio *audio = func_to_audio(f);
632 int status;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200633 struct usb_ep *ep = NULL;
Bryan Wuc6994e62009-06-03 09:17:58 -0400634
635 f_audio_build_desc(audio);
636
637 /* allocate instance-specific interface IDs, and patch descriptors */
638 status = usb_interface_id(c, f);
639 if (status < 0)
640 goto fail;
641 ac_interface_desc.bInterfaceNumber = status;
642
643 status = usb_interface_id(c, f);
644 if (status < 0)
645 goto fail;
646 as_interface_alt_0_desc.bInterfaceNumber = status;
647 as_interface_alt_1_desc.bInterfaceNumber = status;
648
649 status = -ENODEV;
650
651 /* allocate instance-specific endpoints */
652 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
653 if (!ep)
654 goto fail;
655 audio->out_ep = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300656 audio->out_ep->desc = &as_out_ep_desc;
Bryan Wuc6994e62009-06-03 09:17:58 -0400657 ep->driver_data = cdev; /* claim */
658
659 status = -ENOMEM;
660
Felipe Balbief7f5842011-08-26 12:48:15 +0300661 /* copy descriptors, and track endpoint copies */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200662 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL);
663 if (status)
664 goto fail;
Bryan Wuc6994e62009-06-03 09:17:58 -0400665 return 0;
666
667fail:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200668 if (ep)
669 ep->driver_data = NULL;
Bryan Wuc6994e62009-06-03 09:17:58 -0400670 return status;
671}
672
673static void
674f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
675{
676 struct f_audio *audio = func_to_audio(f);
677
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200678 usb_free_all_descriptors(f);
Bryan Wuc6994e62009-06-03 09:17:58 -0400679 kfree(audio);
680}
681
682/*-------------------------------------------------------------------------*/
683
Laurent Pinchartb95cd7e2009-06-21 23:21:55 +0200684static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
685{
686 con->data[cmd] = value;
687
688 return 0;
689}
690
691static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
692{
693 return con->data[cmd];
694}
695
Bryan Wuc6994e62009-06-03 09:17:58 -0400696/* Todo: add more control selecotor dynamically */
697int __init control_selector_init(struct f_audio *audio)
698{
699 INIT_LIST_HEAD(&audio->cs);
700 list_add(&feature_unit.list, &audio->cs);
701
702 INIT_LIST_HEAD(&feature_unit.control);
703 list_add(&mute_control.list, &feature_unit.control);
704 list_add(&volume_control.list, &feature_unit.control);
705
Laurent Pinchart512ad272009-06-21 23:23:05 +0200706 volume_control.data[UAC__CUR] = 0xffc0;
707 volume_control.data[UAC__MIN] = 0xe3a0;
708 volume_control.data[UAC__MAX] = 0xfff0;
709 volume_control.data[UAC__RES] = 0x0030;
Bryan Wuc6994e62009-06-03 09:17:58 -0400710
711 return 0;
712}
713
714/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300715 * audio_bind_config - add USB audio function to a configuration
Bryan Wuc6994e62009-06-03 09:17:58 -0400716 * @c: the configuration to supcard the USB audio function
717 * Context: single threaded during gadget setup
718 *
719 * Returns zero on success, else negative errno.
720 */
721int __init audio_bind_config(struct usb_configuration *c)
722{
723 struct f_audio *audio;
724 int status;
725
726 /* allocate and initialize one new instance */
727 audio = kzalloc(sizeof *audio, GFP_KERNEL);
728 if (!audio)
729 return -ENOMEM;
730
731 audio->card.func.name = "g_audio";
732 audio->card.gadget = c->cdev->gadget;
733
734 INIT_LIST_HEAD(&audio->play_queue);
735 spin_lock_init(&audio->lock);
736
737 /* set up ASLA audio devices */
738 status = gaudio_setup(&audio->card);
739 if (status < 0)
740 goto setup_fail;
741
742 audio->card.func.strings = audio_strings;
743 audio->card.func.bind = f_audio_bind;
744 audio->card.func.unbind = f_audio_unbind;
745 audio->card.func.set_alt = f_audio_set_alt;
746 audio->card.func.setup = f_audio_setup;
747 audio->card.func.disable = f_audio_disable;
Bryan Wuc6994e62009-06-03 09:17:58 -0400748
749 control_selector_init(audio);
750
751 INIT_WORK(&audio->playback_work, f_audio_playback_work);
752
753 status = usb_add_function(c, &audio->card.func);
754 if (status)
755 goto add_fail;
756
757 INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
758 audio_buf_size, req_buf_size, req_count);
759
760 return status;
761
762add_fail:
Cliff Caifeef1d92009-12-09 22:28:39 -0500763 gaudio_cleanup();
Bryan Wuc6994e62009-06-03 09:17:58 -0400764setup_fail:
765 kfree(audio);
766 return status;
767}