~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/usb/usx2y/usbusx2yaudio.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *   US-X2Y AUDIO
  3  *   Copyright (c) 2002-2004 by Karsten Wiese
  4  *
  5  *   based on
  6  *
  7  *   (Tentative) USB Audio Driver for ALSA
  8  *
  9  *   Main and PCM part
 10  *
 11  *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
 12  *
 13  *   Many codes borrowed from audio.c by 
 14  *          Alan Cox (alan@lxorguk.ukuu.org.uk)
 15  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
 16  *
 17  *
 18  *   This program is free software; you can redistribute it and/or modify
 19  *   it under the terms of the GNU General Public License as published by
 20  *   the Free Software Foundation; either version 2 of the License, or
 21  *   (at your option) any later version.
 22  *
 23  *   This program is distributed in the hope that it will be useful,
 24  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  *   GNU General Public License for more details.
 27  *
 28  *   You should have received a copy of the GNU General Public License
 29  *   along with this program; if not, write to the Free Software
 30  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 31  */
 32 
 33 
 34 #include <linux/interrupt.h>
 35 #include <linux/slab.h>
 36 #include <linux/usb.h>
 37 #include <linux/moduleparam.h>
 38 #include <sound/core.h>
 39 #include <sound/info.h>
 40 #include <sound/pcm.h>
 41 #include <sound/pcm_params.h>
 42 #include "usx2y.h"
 43 #include "usbusx2y.h"
 44 
 45 #define USX2Y_NRPACKS 4                 /* Default value used for nr of packs per urb.
 46                                           1 to 4 have been tested ok on uhci.
 47                                           To use 3 on ohci, you'd need a patch:
 48                                           look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
 49                                           "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
 50                                           .
 51                                           1, 2 and 4 work out of the box on ohci, if I recall correctly.
 52                                           Bigger is safer operation,
 53                                           smaller gives lower latencies.
 54                                         */
 55 #define USX2Y_NRPACKS_VARIABLE y        /* If your system works ok with this module's parameter
 56                                            nrpacks set to 1, you might as well comment 
 57                                            this #define out, and thereby produce smaller, faster code.
 58                                            You'd also set USX2Y_NRPACKS to 1 then.
 59                                         */
 60 
 61 #ifdef USX2Y_NRPACKS_VARIABLE
 62  static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
 63  #define  nr_of_packs() nrpacks
 64  module_param(nrpacks, int, 0444);
 65  MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
 66 #else
 67  #define nr_of_packs() USX2Y_NRPACKS
 68 #endif
 69 
 70 
 71 static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
 72 {
 73         struct urb      *urb = subs->completed_urb;
 74         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
 75         unsigned char   *cp;
 76         int             i, len, lens = 0, hwptr_done = subs->hwptr_done;
 77         struct usX2Ydev *usX2Y = subs->usX2Y;
 78 
 79         for (i = 0; i < nr_of_packs(); i++) {
 80                 cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 81                 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
 82                         snd_printk(KERN_ERR "active frame status %i. "
 83                                    "Most propably some hardware problem.\n",
 84                                    urb->iso_frame_desc[i].status);
 85                         return urb->iso_frame_desc[i].status;
 86                 }
 87                 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
 88                 if (! len) {
 89                         snd_printd("0 == len ERROR!\n");
 90                         continue;
 91                 }
 92 
 93                 /* copy a data chunk */
 94                 if ((hwptr_done + len) > runtime->buffer_size) {
 95                         int cnt = runtime->buffer_size - hwptr_done;
 96                         int blen = cnt * usX2Y->stride;
 97                         memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
 98                         memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
 99                 } else {
100                         memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
101                                len * usX2Y->stride);
102                 }
103                 lens += len;
104                 if ((hwptr_done += len) >= runtime->buffer_size)
105                         hwptr_done -= runtime->buffer_size;
106         }
107 
108         subs->hwptr_done = hwptr_done;
109         subs->transfer_done += lens;
110         /* update the pointer, call callback if necessary */
111         if (subs->transfer_done >= runtime->period_size) {
112                 subs->transfer_done -= runtime->period_size;
113                 snd_pcm_period_elapsed(subs->pcm_substream);
114         }
115         return 0;
116 }
117 /*
118  * prepare urb for playback data pipe
119  *
120  * we copy the data directly from the pcm buffer.
121  * the current position to be copied is held in hwptr field.
122  * since a urb can handle only a single linear buffer, if the total
123  * transferred area overflows the buffer boundary, we cannot send
124  * it directly from the buffer.  thus the data is once copied to
125  * a temporary buffer and urb points to that.
126  */
127 static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
128                                   struct urb *cap_urb,
129                                   struct urb *urb)
130 {
131         int count, counts, pack;
132         struct usX2Ydev *usX2Y = subs->usX2Y;
133         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
134 
135         count = 0;
136         for (pack = 0; pack <  nr_of_packs(); pack++) {
137                 /* calculate the size of a packet */
138                 counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
139                 count += counts;
140                 if (counts < 43 || counts > 50) {
141                         snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
142                         return -EPIPE;
143                 }
144                 /* set up descriptor */
145                 urb->iso_frame_desc[pack].offset = pack ?
146                         urb->iso_frame_desc[pack - 1].offset +
147                         urb->iso_frame_desc[pack - 1].length :
148                         0;
149                 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
150         }
151         if (atomic_read(&subs->state) >= state_PRERUNNING)
152                 if (subs->hwptr + count > runtime->buffer_size) {
153                         /* err, the transferred area goes over buffer boundary.
154                          * copy the data to the temp buffer.
155                          */
156                         int len;
157                         len = runtime->buffer_size - subs->hwptr;
158                         urb->transfer_buffer = subs->tmpbuf;
159                         memcpy(subs->tmpbuf, runtime->dma_area +
160                                subs->hwptr * usX2Y->stride, len * usX2Y->stride);
161                         memcpy(subs->tmpbuf + len * usX2Y->stride,
162                                runtime->dma_area, (count - len) * usX2Y->stride);
163                         subs->hwptr += count;
164                         subs->hwptr -= runtime->buffer_size;
165                 } else {
166                         /* set the buffer pointer */
167                         urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
168                         if ((subs->hwptr += count) >= runtime->buffer_size)
169                         subs->hwptr -= runtime->buffer_size;                    
170                 }
171         else
172                 urb->transfer_buffer = subs->tmpbuf;
173         urb->transfer_buffer_length = count * usX2Y->stride;
174         return 0;
175 }
176 
177 /*
178  * process after playback data complete
179  *
180  * update the current position and call callback if a period is processed.
181  */
182 static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
183 {
184         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
185         int             len = urb->actual_length / subs->usX2Y->stride;
186 
187         subs->transfer_done += len;
188         subs->hwptr_done +=  len;
189         if (subs->hwptr_done >= runtime->buffer_size)
190                 subs->hwptr_done -= runtime->buffer_size;
191         if (subs->transfer_done >= runtime->period_size) {
192                 subs->transfer_done -= runtime->period_size;
193                 snd_pcm_period_elapsed(subs->pcm_substream);
194         }
195 }
196 
197 static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
198 {
199         int err;
200         if (!urb)
201                 return -ENODEV;
202         urb->start_frame = (frame + NRURBS * nr_of_packs());  // let hcd do rollover sanity checks
203         urb->hcpriv = NULL;
204         urb->dev = subs->usX2Y->dev; /* we need to set this at each time */
205         if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
206                 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
207                 return err;
208         }
209         return 0;
210 }
211 
212 static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
213                                           struct snd_usX2Y_substream *playbacksubs,
214                                           int frame)
215 {
216         int err, state;
217         struct urb *urb = playbacksubs->completed_urb;
218 
219         state = atomic_read(&playbacksubs->state);
220         if (NULL != urb) {
221                 if (state == state_RUNNING)
222                         usX2Y_urb_play_retire(playbacksubs, urb);
223                 else if (state >= state_PRERUNNING)
224                         atomic_inc(&playbacksubs->state);
225         } else {
226                 switch (state) {
227                 case state_STARTING1:
228                         urb = playbacksubs->urb[0];
229                         atomic_inc(&playbacksubs->state);
230                         break;
231                 case state_STARTING2:
232                         urb = playbacksubs->urb[1];
233                         atomic_inc(&playbacksubs->state);
234                         break;
235                 }
236         }
237         if (urb) {
238                 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
239                     (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
240                         return err;
241                 }
242         }
243 
244         playbacksubs->completed_urb = NULL;
245 
246         state = atomic_read(&capsubs->state);
247         if (state >= state_PREPARED) {
248                 if (state == state_RUNNING) {
249                         if ((err = usX2Y_urb_capt_retire(capsubs)))
250                                 return err;
251                 } else if (state >= state_PRERUNNING)
252                         atomic_inc(&capsubs->state);
253                 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
254                         return err;
255         }
256         capsubs->completed_urb = NULL;
257         return 0;
258 }
259 
260 
261 static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
262 {
263         int s, u;
264 
265         for (s = 0; s < 4; s++) {
266                 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
267                 if (subs) {
268                         snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
269                         atomic_set(&subs->state, state_STOPPED);
270                 }
271         }
272         for (s = 0; s < 4; s++) {
273                 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
274                 if (subs) {
275                         if (atomic_read(&subs->state) >= state_PRERUNNING) {
276                                 unsigned long flags;
277 
278                                 snd_pcm_stream_lock_irqsave(subs->pcm_substream, flags);
279                                 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
280                                 snd_pcm_stream_unlock_irqrestore(subs->pcm_substream, flags);
281                         }
282                         for (u = 0; u < NRURBS; u++) {
283                                 struct urb *urb = subs->urb[u];
284                                 if (NULL != urb)
285                                         snd_printdd("%i status=%i start_frame=%i\n",
286                                                     u, urb->status, urb->start_frame);
287                         }
288                 }
289         }
290         usX2Y->prepare_subs = NULL;
291         wake_up(&usX2Y->prepare_wait_queue);
292 }
293 
294 static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
295                                    struct snd_usX2Y_substream *subs, struct urb *urb)
296 {
297         snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
298         urb->status = 0;
299         usX2Y_clients_stop(usX2Y);
300 }
301 
302 static void i_usX2Y_urb_complete(struct urb *urb)
303 {
304         struct snd_usX2Y_substream *subs = urb->context;
305         struct usX2Ydev *usX2Y = subs->usX2Y;
306 
307         if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
308                 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
309                             usb_get_current_frame_number(usX2Y->dev),
310                             subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
311                             urb->status, urb->start_frame);
312                 return;
313         }
314         if (unlikely(urb->status)) {
315                 usX2Y_error_urb_status(usX2Y, subs, urb);
316                 return;
317         }
318 
319         subs->completed_urb = urb;
320 
321         {
322                 struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
323                         *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
324                 if (capsubs->completed_urb &&
325                     atomic_read(&capsubs->state) >= state_PREPARED &&
326                     (playbacksubs->completed_urb ||
327                      atomic_read(&playbacksubs->state) < state_PREPARED)) {
328                         if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
329                                 usX2Y->wait_iso_frame += nr_of_packs();
330                         else {
331                                 snd_printdd("\n");
332                                 usX2Y_clients_stop(usX2Y);
333                         }
334                 }
335         }
336 }
337 
338 static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
339                                     void (*complete)(struct urb *))
340 {
341         int s, u;
342         for (s = 0; s < 4; s++) {
343                 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
344                 if (NULL != subs)
345                         for (u = 0; u < NRURBS; u++) {
346                                 struct urb * urb = subs->urb[u];
347                                 if (NULL != urb)
348                                         urb->complete = complete;
349                         }
350         }
351 }
352 
353 static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
354 {
355         usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
356         usX2Y->prepare_subs = NULL;
357 }
358 
359 static void i_usX2Y_subs_startup(struct urb *urb)
360 {
361         struct snd_usX2Y_substream *subs = urb->context;
362         struct usX2Ydev *usX2Y = subs->usX2Y;
363         struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
364         if (NULL != prepare_subs)
365                 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
366                         usX2Y_subs_startup_finish(usX2Y);
367                         atomic_inc(&prepare_subs->state);
368                         wake_up(&usX2Y->prepare_wait_queue);
369                 }
370 
371         i_usX2Y_urb_complete(urb);
372 }
373 
374 static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
375 {
376         snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
377                     subs, subs->endpoint, subs->urb[0], subs->urb[1]);
378         /* reset the pointer */
379         subs->hwptr = 0;
380         subs->hwptr_done = 0;
381         subs->transfer_done = 0;
382 }
383 
384 
385 static void usX2Y_urb_release(struct urb **urb, int free_tb)
386 {
387         if (*urb) {
388                 usb_kill_urb(*urb);
389                 if (free_tb)
390                         kfree((*urb)->transfer_buffer);
391                 usb_free_urb(*urb);
392                 *urb = NULL;
393         }
394 }
395 /*
396  * release a substreams urbs
397  */
398 static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
399 {
400         int i;
401         snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
402         for (i = 0; i < NRURBS; i++)
403                 usX2Y_urb_release(subs->urb + i,
404                                   subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
405 
406         kfree(subs->tmpbuf);
407         subs->tmpbuf = NULL;
408 }
409 /*
410  * initialize a substream's urbs
411  */
412 static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
413 {
414         int i;
415         unsigned int pipe;
416         int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
417         struct usb_device *dev = subs->usX2Y->dev;
418 
419         pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
420                         usb_rcvisocpipe(dev, subs->endpoint);
421         subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
422         if (!subs->maxpacksize)
423                 return -EINVAL;
424 
425         if (is_playback && NULL == subs->tmpbuf) {      /* allocate a temporary buffer for playback */
426                 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
427                 if (NULL == subs->tmpbuf) {
428                         snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
429                         return -ENOMEM;
430                 }
431         }
432         /* allocate and initialize data urbs */
433         for (i = 0; i < NRURBS; i++) {
434                 struct urb **purb = subs->urb + i;
435                 if (*purb) {
436                         usb_kill_urb(*purb);
437                         continue;
438                 }
439                 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
440                 if (NULL == *purb) {
441                         usX2Y_urbs_release(subs);
442                         return -ENOMEM;
443                 }
444                 if (!is_playback && !(*purb)->transfer_buffer) {
445                         /* allocate a capture buffer per urb */
446                         (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
447                         if (NULL == (*purb)->transfer_buffer) {
448                                 usX2Y_urbs_release(subs);
449                                 return -ENOMEM;
450                         }
451                 }
452                 (*purb)->dev = dev;
453                 (*purb)->pipe = pipe;
454                 (*purb)->number_of_packets = nr_of_packs();
455                 (*purb)->context = subs;
456                 (*purb)->interval = 1;
457                 (*purb)->complete = i_usX2Y_subs_startup;
458         }
459         return 0;
460 }
461 
462 static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
463 {
464         struct usX2Ydev *usX2Y = subs->usX2Y;
465         usX2Y->prepare_subs = subs;
466         subs->urb[0]->start_frame = -1;
467         wmb();
468         usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
469 }
470 
471 static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
472 {
473         int i, err;
474         struct usX2Ydev *usX2Y = subs->usX2Y;
475 
476         if ((err = usX2Y_urbs_allocate(subs)) < 0)
477                 return err;
478         subs->completed_urb = NULL;
479         for (i = 0; i < 4; i++) {
480                 struct snd_usX2Y_substream *subs = usX2Y->subs[i];
481                 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
482                         goto start;
483         }
484 
485  start:
486         usX2Y_subs_startup(subs);
487         for (i = 0; i < NRURBS; i++) {
488                 struct urb *urb = subs->urb[i];
489                 if (usb_pipein(urb->pipe)) {
490                         unsigned long pack;
491                         if (0 == i)
492                                 atomic_set(&subs->state, state_STARTING3);
493                         urb->dev = usX2Y->dev;
494                         urb->transfer_flags = URB_ISO_ASAP;
495                         for (pack = 0; pack < nr_of_packs(); pack++) {
496                                 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
497                                 urb->iso_frame_desc[pack].length = subs->maxpacksize;
498                         }
499                         urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 
500                         if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
501                                 snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
502                                 err = -EPIPE;
503                                 goto cleanup;
504                         } else
505                                 if (i == 0)
506                                         usX2Y->wait_iso_frame = urb->start_frame;
507                         urb->transfer_flags = 0;
508                 } else {
509                         atomic_set(&subs->state, state_STARTING1);
510                         break;
511                 }
512         }
513         err = 0;
514         wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
515         if (atomic_read(&subs->state) != state_PREPARED)
516                 err = -EPIPE;
517 
518  cleanup:
519         if (err) {
520                 usX2Y_subs_startup_finish(usX2Y);
521                 usX2Y_clients_stop(usX2Y);              // something is completely wroong > stop evrything
522         }
523         return err;
524 }
525 
526 /*
527  * return the current pcm pointer.  just return the hwptr_done value.
528  */
529 static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
530 {
531         struct snd_usX2Y_substream *subs = substream->runtime->private_data;
532         return subs->hwptr_done;
533 }
534 /*
535  * start/stop substream
536  */
537 static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
538 {
539         struct snd_usX2Y_substream *subs = substream->runtime->private_data;
540 
541         switch (cmd) {
542         case SNDRV_PCM_TRIGGER_START:
543                 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
544                 if (atomic_read(&subs->state) == state_PREPARED &&
545                     atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
546                         atomic_set(&subs->state, state_PRERUNNING);
547                 } else {
548                         snd_printdd("\n");
549                         return -EPIPE;
550                 }
551                 break;
552         case SNDRV_PCM_TRIGGER_STOP:
553                 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
554                 if (atomic_read(&subs->state) >= state_PRERUNNING)
555                         atomic_set(&subs->state, state_PREPARED);
556                 break;
557         default:
558                 return -EINVAL;
559         }
560         return 0;
561 }
562 
563 
564 /*
565  * allocate a buffer, setup samplerate
566  *
567  * so far we use a physically linear buffer although packetize transfer
568  * doesn't need a continuous area.
569  * if sg buffer is supported on the later version of alsa, we'll follow
570  * that.
571  */
572 static struct s_c2
573 {
574         char c1, c2;
575 }
576         SetRate44100[] =
577 {
578         { 0x14, 0x08},  // this line sets 44100, well actually a little less
579         { 0x18, 0x40},  // only tascam / frontier design knows the further lines .......
580         { 0x18, 0x42},
581         { 0x18, 0x45},
582         { 0x18, 0x46},
583         { 0x18, 0x48},
584         { 0x18, 0x4A},
585         { 0x18, 0x4C},
586         { 0x18, 0x4E},
587         { 0x18, 0x50},
588         { 0x18, 0x52},
589         { 0x18, 0x54},
590         { 0x18, 0x56},
591         { 0x18, 0x58},
592         { 0x18, 0x5A},
593         { 0x18, 0x5C},
594         { 0x18, 0x5E},
595         { 0x18, 0x60},
596         { 0x18, 0x62},
597         { 0x18, 0x64},
598         { 0x18, 0x66},
599         { 0x18, 0x68},
600         { 0x18, 0x6A},
601         { 0x18, 0x6C},
602         { 0x18, 0x6E},
603         { 0x18, 0x70},
604         { 0x18, 0x72},
605         { 0x18, 0x74},
606         { 0x18, 0x76},
607         { 0x18, 0x78},
608         { 0x18, 0x7A},
609         { 0x18, 0x7C},
610         { 0x18, 0x7E}
611 };
612 static struct s_c2 SetRate48000[] =
613 {
614         { 0x14, 0x09},  // this line sets 48000, well actually a little less
615         { 0x18, 0x40},  // only tascam / frontier design knows the further lines .......
616         { 0x18, 0x42},
617         { 0x18, 0x45},
618         { 0x18, 0x46},
619         { 0x18, 0x48},
620         { 0x18, 0x4A},
621         { 0x18, 0x4C},
622         { 0x18, 0x4E},
623         { 0x18, 0x50},
624         { 0x18, 0x52},
625         { 0x18, 0x54},
626         { 0x18, 0x56},
627         { 0x18, 0x58},
628         { 0x18, 0x5A},
629         { 0x18, 0x5C},
630         { 0x18, 0x5E},
631         { 0x18, 0x60},
632         { 0x18, 0x62},
633         { 0x18, 0x64},
634         { 0x18, 0x66},
635         { 0x18, 0x68},
636         { 0x18, 0x6A},
637         { 0x18, 0x6C},
638         { 0x18, 0x6E},
639         { 0x18, 0x70},
640         { 0x18, 0x73},
641         { 0x18, 0x74},
642         { 0x18, 0x76},
643         { 0x18, 0x78},
644         { 0x18, 0x7A},
645         { 0x18, 0x7C},
646         { 0x18, 0x7E}
647 };
648 #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
649 
650 static void i_usX2Y_04Int(struct urb *urb)
651 {
652         struct usX2Ydev *usX2Y = urb->context;
653         
654         if (urb->status)
655                 snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
656         if (0 == --usX2Y->US04->len)
657                 wake_up(&usX2Y->In04WaitQueue);
658 }
659 
660 static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
661 {
662         int                     err = 0, i;
663         struct snd_usX2Y_urbSeq *us = NULL;
664         int                     *usbdata = NULL;
665         struct s_c2             *ra = rate == 48000 ? SetRate48000 : SetRate44100;
666 
667         if (usX2Y->rate != rate) {
668                 us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
669                 if (NULL == us) {
670                         err = -ENOMEM;
671                         goto cleanup;
672                 }
673                 usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
674                 if (NULL == usbdata) {
675                         err = -ENOMEM;
676                         goto cleanup;
677                 }
678                 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
679                         if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
680                                 err = -ENOMEM;
681                                 goto cleanup;
682                         }
683                         ((char*)(usbdata + i))[0] = ra[i].c1;
684                         ((char*)(usbdata + i))[1] = ra[i].c2;
685                         usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4),
686                                           usbdata + i, 2, i_usX2Y_04Int, usX2Y);
687 #ifdef OLD_USB
688                         us->urb[i]->transfer_flags = USB_QUEUE_BULK;
689 #endif
690                 }
691                 us->submitted = 0;
692                 us->len =       NOOF_SETRATE_URBS;
693                 usX2Y->US04 =   us;
694                 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
695                 usX2Y->US04 =   NULL;
696                 if (us->len)
697                         err = -ENODEV;
698         cleanup:
699                 if (us) {
700                         us->submitted = 2*NOOF_SETRATE_URBS;
701                         for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
702                                 struct urb *urb = us->urb[i];
703                                 if (urb->status) {
704                                         if (!err)
705                                                 err = -ENODEV;
706                                         usb_kill_urb(urb);
707                                 }
708                                 usb_free_urb(urb);
709                         }
710                         usX2Y->US04 = NULL;
711                         kfree(usbdata);
712                         kfree(us);
713                         if (!err)
714                                 usX2Y->rate = rate;
715                 }
716         }
717 
718         return err;
719 }
720 
721 
722 static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
723 {
724         int alternate, err;
725         struct list_head* p;
726         if (format == SNDRV_PCM_FORMAT_S24_3LE) {
727                 alternate = 2;
728                 usX2Y->stride = 6;
729         } else {
730                 alternate = 1;
731                 usX2Y->stride = 4;
732         }
733         list_for_each(p, &usX2Y->midi_list) {
734                 snd_usbmidi_input_stop(p);
735         }
736         usb_kill_urb(usX2Y->In04urb);
737         if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) {
738                 snd_printk(KERN_ERR "usb_set_interface error \n");
739                 return err;
740         }
741         usX2Y->In04urb->dev = usX2Y->dev;
742         err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
743         list_for_each(p, &usX2Y->midi_list) {
744                 snd_usbmidi_input_start(p);
745         }
746         usX2Y->format = format;
747         usX2Y->rate = 0;
748         return err;
749 }
750 
751 
752 static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
753                                    struct snd_pcm_hw_params *hw_params)
754 {
755         int                     err = 0;
756         unsigned int            rate = params_rate(hw_params);
757         snd_pcm_format_t        format = params_format(hw_params);
758         struct snd_card *card = substream->pstr->pcm->card;
759         struct list_head *list;
760 
761         snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
762         // all pcm substreams off one usX2Y have to operate at the same rate & format
763         list_for_each(list, &card->devices) {
764                 struct snd_device *dev;
765                 struct snd_pcm *pcm;
766                 int s;
767                 dev = snd_device(list);
768                 if (dev->type != SNDRV_DEV_PCM)
769                         continue;
770                 pcm = dev->device_data;
771                 for (s = 0; s < 2; ++s) {
772                         struct snd_pcm_substream *test_substream;
773                         test_substream = pcm->streams[s].substream;
774                         if (test_substream && test_substream != substream  &&
775                             test_substream->runtime &&
776                             ((test_substream->runtime->format &&
777                               test_substream->runtime->format != format) ||
778                              (test_substream->runtime->rate &&
779                               test_substream->runtime->rate != rate)))
780                                 return -EINVAL;
781                 }
782         }
783         if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
784                 snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
785                            substream, params_buffer_bytes(hw_params), err);
786                 return err;
787         }
788         return 0;
789 }
790 
791 /*
792  * free the buffer
793  */
794 static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
795 {
796         struct snd_pcm_runtime *runtime = substream->runtime;
797         struct snd_usX2Y_substream *subs = runtime->private_data;
798         mutex_lock(&subs->usX2Y->prepare_mutex);
799         snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
800 
801         if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
802                 struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
803                 atomic_set(&subs->state, state_STOPPED);
804                 usX2Y_urbs_release(subs);
805                 if (!cap_subs->pcm_substream ||
806                     !cap_subs->pcm_substream->runtime ||
807                     !cap_subs->pcm_substream->runtime->status ||
808                     cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
809                         atomic_set(&cap_subs->state, state_STOPPED);
810                         usX2Y_urbs_release(cap_subs);
811                 }
812         } else {
813                 struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
814                 if (atomic_read(&playback_subs->state) < state_PREPARED) {
815                         atomic_set(&subs->state, state_STOPPED);
816                         usX2Y_urbs_release(subs);
817                 }
818         }
819         mutex_unlock(&subs->usX2Y->prepare_mutex);
820         return snd_pcm_lib_free_pages(substream);
821 }
822 /*
823  * prepare callback
824  *
825  * set format and initialize urbs
826  */
827 static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
828 {
829         struct snd_pcm_runtime *runtime = substream->runtime;
830         struct snd_usX2Y_substream *subs = runtime->private_data;
831         struct usX2Ydev *usX2Y = subs->usX2Y;
832         struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
833         int err = 0;
834         snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
835 
836         mutex_lock(&usX2Y->prepare_mutex);
837         usX2Y_subs_prepare(subs);
838 // Start hardware streams
839 // SyncStream first....
840         if (atomic_read(&capsubs->state) < state_PREPARED) {
841                 if (usX2Y->format != runtime->format)
842                         if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
843                                 goto up_prepare_mutex;
844                 if (usX2Y->rate != runtime->rate)
845                         if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
846                                 goto up_prepare_mutex;
847                 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
848                 if (0 > (err = usX2Y_urbs_start(capsubs)))
849                         goto up_prepare_mutex;
850         }
851 
852         if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
853                 err = usX2Y_urbs_start(subs);
854 
855  up_prepare_mutex:
856         mutex_unlock(&usX2Y->prepare_mutex);
857         return err;
858 }
859 
860 static struct snd_pcm_hardware snd_usX2Y_2c =
861 {
862         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
863                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
864                                  SNDRV_PCM_INFO_MMAP_VALID |
865                                  SNDRV_PCM_INFO_BATCH),
866         .formats =                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
867         .rates =                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
868         .rate_min =                44100,
869         .rate_max =                48000,
870         .channels_min =            2,
871         .channels_max =            2,
872         .buffer_bytes_max =     (2*128*1024),
873         .period_bytes_min =     64,
874         .period_bytes_max =     (128*1024),
875         .periods_min =          2,
876         .periods_max =          1024,
877         .fifo_size =              0
878 };
879 
880 
881 
882 static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
883 {
884         struct snd_usX2Y_substream      *subs = ((struct snd_usX2Y_substream **)
885                                          snd_pcm_substream_chip(substream))[substream->stream];
886         struct snd_pcm_runtime  *runtime = substream->runtime;
887 
888         if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
889                 return -EBUSY;
890 
891         runtime->hw = snd_usX2Y_2c;
892         runtime->private_data = subs;
893         subs->pcm_substream = substream;
894         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
895         return 0;
896 }
897 
898 
899 
900 static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
901 {
902         struct snd_pcm_runtime *runtime = substream->runtime;
903         struct snd_usX2Y_substream *subs = runtime->private_data;
904 
905         subs->pcm_substream = NULL;
906 
907         return 0;
908 }
909 
910 
911 static struct snd_pcm_ops snd_usX2Y_pcm_ops = 
912 {
913         .open =         snd_usX2Y_pcm_open,
914         .close =        snd_usX2Y_pcm_close,
915         .ioctl =        snd_pcm_lib_ioctl,
916         .hw_params =    snd_usX2Y_pcm_hw_params,
917         .hw_free =      snd_usX2Y_pcm_hw_free,
918         .prepare =      snd_usX2Y_pcm_prepare,
919         .trigger =      snd_usX2Y_pcm_trigger,
920         .pointer =      snd_usX2Y_pcm_pointer,
921 };
922 
923 
924 /*
925  * free a usb stream instance
926  */
927 static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
928 {
929         kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
930         usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
931 
932         kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
933         usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
934 }
935 
936 static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
937 {
938         struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
939         if (usX2Y_stream)
940                 usX2Y_audio_stream_free(usX2Y_stream);
941 }
942 
943 static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
944 {
945         struct snd_pcm *pcm;
946         int err, i;
947         struct snd_usX2Y_substream **usX2Y_substream =
948                 usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs;
949 
950         for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
951              i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
952                 usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
953                 if (NULL == usX2Y_substream[i]) {
954                         snd_printk(KERN_ERR "cannot malloc\n");
955                         return -ENOMEM;
956                 }
957                 usX2Y_substream[i]->usX2Y = usX2Y(card);
958         }
959 
960         if (playback_endpoint)
961                 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
962         usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
963 
964         err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs,
965                           playback_endpoint ? 1 : 0, 1,
966                           &pcm);
967         if (err < 0) {
968                 usX2Y_audio_stream_free(usX2Y_substream);
969                 return err;
970         }
971 
972         if (playback_endpoint)
973                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
974         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
975 
976         pcm->private_data = usX2Y_substream;
977         pcm->private_free = snd_usX2Y_pcm_private_free;
978         pcm->info_flags = 0;
979 
980         sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs);
981 
982         if ((playback_endpoint &&
983              0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
984                                                      SNDRV_DMA_TYPE_CONTINUOUS,
985                                                      snd_dma_continuous_data(GFP_KERNEL),
986                                                      64*1024, 128*1024))) ||
987             0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
988                                                      SNDRV_DMA_TYPE_CONTINUOUS,
989                                                      snd_dma_continuous_data(GFP_KERNEL),
990                                                      64*1024, 128*1024))) {
991                 snd_usX2Y_pcm_private_free(pcm);
992                 return err;
993         }
994         usX2Y(card)->pcm_devs++;
995 
996         return 0;
997 }
998 
999 /*
1000  * create a chip instance and set its names.
1001  */
1002 int usX2Y_audio_create(struct snd_card *card)
1003 {
1004         int err = 0;
1005         
1006         INIT_LIST_HEAD(&usX2Y(card)->pcm_list);
1007 
1008         if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1009                 return err;
1010         if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428)
1011              if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1012                      return err;
1013         if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122)
1014                 err = usX2Y_rate_set(usX2Y(card), 44100);       // Lets us428 recognize output-volume settings, disturbs us122.
1015         return err;
1016 }
1017 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp