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