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