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