Blame view

sound/usb/usx2y/usbusx2yaudio.c 29.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
   *   US-X2Y AUDIO
   *   Copyright (c) 2002-2004 by Karsten Wiese
   *
   *   based on
   *
   *   (Tentative) USB Audio Driver for ALSA
   *
   *   Main and PCM part
   *
   *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
   *
   *   Many codes borrowed from audio.c by 
   *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
   *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  #include <linux/interrupt.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
33
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include <linux/usb.h>
31623caaf   Paul Gortmaker   sound: add module...
35
  #include <linux/moduleparam.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <sound/core.h>
  #include <sound/info.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  #include "usx2y.h"
  #include "usbusx2y.h"
  
  #define USX2Y_NRPACKS 4			/* Default value used for nr of packs per urb.
  					  1 to 4 have been tested ok on uhci.
  					  To use 3 on ohci, you'd need a patch:
  					  look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
  					  "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
  					  .
  					  1, 2 and 4 work out of the box on ohci, if I recall correctly.
  					  Bigger is safer operation,
  					  smaller gives lower latencies.
  					*/
  #define USX2Y_NRPACKS_VARIABLE y	/* If your system works ok with this module's parameter
  					   nrpacks set to 1, you might as well comment 
  					   this #define out, and thereby produce smaller, faster code.
  					   You'd also set USX2Y_NRPACKS to 1 then.
  					*/
  
  #ifdef USX2Y_NRPACKS_VARIABLE
   static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
   #define  nr_of_packs() nrpacks
   module_param(nrpacks, int, 0444);
   MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
  #else
   #define nr_of_packs() USX2Y_NRPACKS
  #endif
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
67
  static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
  {
  	struct urb	*urb = subs->completed_urb;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
70
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
  	unsigned char	*cp;
  	int 		i, len, lens = 0, hwptr_done = subs->hwptr_done;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
73
  	struct usX2Ydev	*usX2Y = subs->usX2Y;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
76
77
  
  	for (i = 0; i < nr_of_packs(); i++) {
  		cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  		if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
78
79
80
81
  			snd_printk(KERN_ERR "active frame status %i. "
  				   "Most propably some hardware problem.
  ",
  				   urb->iso_frame_desc[i].status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  			return urb->iso_frame_desc[i].status;
  		}
  		len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
  		if (! len) {
  			snd_printd("0 == len ERROR!
  ");
  			continue;
  		}
  
  		/* copy a data chunk */
  		if ((hwptr_done + len) > runtime->buffer_size) {
  			int cnt = runtime->buffer_size - hwptr_done;
  			int blen = cnt * usX2Y->stride;
  			memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
  			memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
  		} else {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
98
99
  			memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
  			       len * usX2Y->stride);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  		}
  		lens += len;
  		if ((hwptr_done += len) >= runtime->buffer_size)
  			hwptr_done -= runtime->buffer_size;
  	}
  
  	subs->hwptr_done = hwptr_done;
  	subs->transfer_done += lens;
  	/* update the pointer, call callback if necessary */
  	if (subs->transfer_done >= runtime->period_size) {
  		subs->transfer_done -= runtime->period_size;
  		snd_pcm_period_elapsed(subs->pcm_substream);
  	}
  	return 0;
  }
  /*
   * prepare urb for playback data pipe
   *
   * we copy the data directly from the pcm buffer.
   * the current position to be copied is held in hwptr field.
   * since a urb can handle only a single linear buffer, if the total
   * transferred area overflows the buffer boundary, we cannot send
   * it directly from the buffer.  thus the data is once copied to
   * a temporary buffer and urb points to that.
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
125
  static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
  				  struct urb *cap_urb,
  				  struct urb *urb)
  {
  	int count, counts, pack;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
130
131
  	struct usX2Ydev *usX2Y = subs->usX2Y;
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
135
136
137
138
  
  	count = 0;
  	for (pack = 0; pack <  nr_of_packs(); pack++) {
  		/* calculate the size of a packet */
  		counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
  		count += counts;
  		if (counts < 43 || counts > 50) {
d3d579f84   Takashi Iwai   [ALSA] Add missin...
139
140
  			snd_printk(KERN_ERR "should not be here with counts=%i
  ", counts);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
  			return -EPIPE;
  		}
  		/* set up descriptor */
  		urb->iso_frame_desc[pack].offset = pack ?
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
145
146
  			urb->iso_frame_desc[pack - 1].offset +
  			urb->iso_frame_desc[pack - 1].length :
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
150
151
152
153
154
155
156
157
  			0;
  		urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
  	}
  	if (atomic_read(&subs->state) >= state_PRERUNNING)
  		if (subs->hwptr + count > runtime->buffer_size) {
  			/* err, the transferred area goes over buffer boundary.
  			 * copy the data to the temp buffer.
  			 */
  			int len;
  			len = runtime->buffer_size - subs->hwptr;
  			urb->transfer_buffer = subs->tmpbuf;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
158
159
160
161
  			memcpy(subs->tmpbuf, runtime->dma_area +
  			       subs->hwptr * usX2Y->stride, len * usX2Y->stride);
  			memcpy(subs->tmpbuf + len * usX2Y->stride,
  			       runtime->dma_area, (count - len) * usX2Y->stride);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  			subs->hwptr += count;
  			subs->hwptr -= runtime->buffer_size;
  		} else {
  			/* set the buffer pointer */
  			urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
  			if ((subs->hwptr += count) >= runtime->buffer_size)
  			subs->hwptr -= runtime->buffer_size;			
  		}
  	else
  		urb->transfer_buffer = subs->tmpbuf;
  	urb->transfer_buffer_length = count * usX2Y->stride;
  	return 0;
  }
  
  /*
   * process after playback data complete
   *
   * update the current position and call callback if a period is processed.
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
181
  static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
183
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
188
189
190
191
192
193
194
  	int		len = urb->actual_length / subs->usX2Y->stride;
  
  	subs->transfer_done += len;
  	subs->hwptr_done +=  len;
  	if (subs->hwptr_done >= runtime->buffer_size)
  		subs->hwptr_done -= runtime->buffer_size;
  	if (subs->transfer_done >= runtime->period_size) {
  		subs->transfer_done -= runtime->period_size;
  		snd_pcm_period_elapsed(subs->pcm_substream);
  	}
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
195
  static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
  {
  	int err;
  	if (!urb)
  		return -ENODEV;
  	urb->start_frame = (frame + NRURBS * nr_of_packs());  // let hcd do rollover sanity checks
  	urb->hcpriv = NULL;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
202
  	urb->dev = subs->usX2Y->dev; /* we need to set this at each time */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
d3d579f84   Takashi Iwai   [ALSA] Add missin...
204
205
  		snd_printk(KERN_ERR "usb_submit_urb() returned %i
  ", err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
  		return err;
  	}
  	return 0;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
210
211
212
  static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
  					  struct snd_usX2Y_substream *playbacksubs,
  					  int frame)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
  {
  	int err, state;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
215
216
217
218
219
220
  	struct urb *urb = playbacksubs->completed_urb;
  
  	state = atomic_read(&playbacksubs->state);
  	if (NULL != urb) {
  		if (state == state_RUNNING)
  			usX2Y_urb_play_retire(playbacksubs, urb);
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
221
222
  		else if (state >= state_PRERUNNING)
  			atomic_inc(&playbacksubs->state);
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
223
224
225
226
227
228
229
230
231
232
  	} else {
  		switch (state) {
  		case state_STARTING1:
  			urb = playbacksubs->urb[0];
  			atomic_inc(&playbacksubs->state);
  			break;
  		case state_STARTING2:
  			urb = playbacksubs->urb[1];
  			atomic_inc(&playbacksubs->state);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  	}
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
235
236
  	if (urb) {
  		if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
237
  		    (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
238
  			return err;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
239
  		}
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
240
241
242
  	}
  
  	playbacksubs->completed_urb = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
244
245
246
247
  	state = atomic_read(&capsubs->state);
  	if (state >= state_PREPARED) {
  		if (state == state_RUNNING) {
  			if ((err = usX2Y_urb_capt_retire(capsubs)))
  				return err;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
248
249
  		} else if (state >= state_PRERUNNING)
  			atomic_inc(&capsubs->state);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
253
254
255
  		if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
  			return err;
  	}
  	capsubs->completed_urb = NULL;
  	return 0;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
256
  static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
  {
  	int s, u;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
259

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  	for (s = 0; s < 4; s++) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
261
  		struct snd_usX2Y_substream *subs = usX2Y->subs[s];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
267
268
  		if (subs) {
  			snd_printdd("%i %p state=%i
  ", s, subs, atomic_read(&subs->state));
  			atomic_set(&subs->state, state_STOPPED);
  		}
  	}
  	for (s = 0; s < 4; s++) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
269
  		struct snd_usX2Y_substream *subs = usX2Y->subs[s];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
272
273
274
275
276
  		if (subs) {
  			if (atomic_read(&subs->state) >= state_PRERUNNING) {
  				snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  			}
  			for (u = 0; u < NRURBS; u++) {
  				struct urb *urb = subs->urb[u];
  				if (NULL != urb)
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
277
278
279
  					snd_printdd("%i status=%i start_frame=%i
  ",
  						    u, urb->status, urb->start_frame);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
281
282
283
284
285
  			}
  		}
  	}
  	usX2Y->prepare_subs = NULL;
  	wake_up(&usX2Y->prepare_wait_queue);
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
286
287
  static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
  				   struct snd_usX2Y_substream *subs, struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  {
d3d579f84   Takashi Iwai   [ALSA] Add missin...
289
290
  	snd_printk(KERN_ERR "ep=%i stalled with status=%i
  ", subs->endpoint, urb->status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
293
  	urb->status = 0;
  	usX2Y_clients_stop(usX2Y);
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
294
295
  static void usX2Y_error_sequence(struct usX2Ydev *usX2Y,
  				 struct snd_usX2Y_substream *subs, struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  {
ad361c988   Joe Perches   Remove multiple K...
297
298
299
300
301
302
303
  	snd_printk(KERN_ERR
  "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).
  "
  "Most propably some urb of usb-frame %i is still missing.
  "
  "Cause could be too long delays in usb-hcd interrupt handling.
  ",
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
304
  		   usb_get_current_frame_number(usX2Y->dev),
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
305
306
  		   subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
  		   usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
  	usX2Y_clients_stop(usX2Y);
  }
7d12e780e   David Howells   IRQ: Maintain reg...
309
  static void i_usX2Y_urb_complete(struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
311
312
  	struct snd_usX2Y_substream *subs = urb->context;
  	struct usX2Ydev *usX2Y = subs->usX2Y;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
314
  
  	if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
315
316
  		snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i
  ",
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
317
  			    usb_get_current_frame_number(usX2Y->dev),
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
318
319
  			    subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
  			    urb->status, urb->start_frame);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
  		return;
  	}
  	if (unlikely(urb->status)) {
  		usX2Y_error_urb_status(usX2Y, subs, urb);
  		return;
  	}
bc6191b10   Karsten Wiese   [ALSA] Repair snd...
326
  	if (likely((urb->start_frame & 0xFFFF) == (usX2Y->wait_iso_frame & 0xFFFF)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
332
  		subs->completed_urb = urb;
  	else {
  		usX2Y_error_sequence(usX2Y, subs, urb);
  		return;
  	}
  	{
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
333
  		struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  			*playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
335
336
337
338
  		if (capsubs->completed_urb &&
  		    atomic_read(&capsubs->state) >= state_PREPARED &&
  		    (playbacksubs->completed_urb ||
  		     atomic_read(&playbacksubs->state) < state_PREPARED)) {
635bbb355   Karsten Wiese   [ALSA] Repair snd...
339
340
341
  			if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
  				usX2Y->wait_iso_frame += nr_of_packs();
  			else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
343
344
345
346
347
348
  				snd_printdd("
  ");
  				usX2Y_clients_stop(usX2Y);
  			}
  		}
  	}
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
349
  static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
7d12e780e   David Howells   IRQ: Maintain reg...
350
  				    void (*complete)(struct urb *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
353
  {
  	int s, u;
  	for (s = 0; s < 4; s++) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
354
  		struct snd_usX2Y_substream *subs = usX2Y->subs[s];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
360
361
362
  		if (NULL != subs)
  			for (u = 0; u < NRURBS; u++) {
  				struct urb * urb = subs->urb[u];
  				if (NULL != urb)
  					urb->complete = complete;
  			}
  	}
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
363
  static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
  {
  	usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
  	usX2Y->prepare_subs = NULL;
  }
7d12e780e   David Howells   IRQ: Maintain reg...
368
  static void i_usX2Y_subs_startup(struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
370
371
372
  	struct snd_usX2Y_substream *subs = urb->context;
  	struct usX2Ydev *usX2Y = subs->usX2Y;
  	struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
376
377
378
  	if (NULL != prepare_subs)
  		if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
  			usX2Y_subs_startup_finish(usX2Y);
  			atomic_inc(&prepare_subs->state);
  			wake_up(&usX2Y->prepare_wait_queue);
  		}
7d12e780e   David Howells   IRQ: Maintain reg...
379
  	i_usX2Y_urb_complete(urb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
381
  static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
383
384
385
  	snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p
  ",
  		    subs, subs->endpoint, subs->urb[0], subs->urb[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
  	/* reset the pointer */
  	subs->hwptr = 0;
  	subs->hwptr_done = 0;
  	subs->transfer_done = 0;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
391
  static void usX2Y_urb_release(struct urb **urb, int free_tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
393
394
395
396
397
398
399
400
401
402
403
  {
  	if (*urb) {
  		usb_kill_urb(*urb);
  		if (free_tb)
  			kfree((*urb)->transfer_buffer);
  		usb_free_urb(*urb);
  		*urb = NULL;
  	}
  }
  /*
   * release a substreams urbs
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
404
  static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
406
407
408
409
  {
  	int i;
  	snd_printdd("usX2Y_urbs_release() %i
  ", subs->endpoint);
  	for (i = 0; i < NRURBS; i++)
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
410
411
  		usX2Y_urb_release(subs->urb + i,
  				  subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412

4d572776d   Jesper Juhl   [ALSA] Remove red...
413
414
  	kfree(subs->tmpbuf);
  	subs->tmpbuf = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
418
  }
  /*
   * initialize a substream's urbs
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
419
  static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
  {
  	int i;
  	unsigned int pipe;
  	int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
424
  	struct usb_device *dev = subs->usX2Y->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  
  	pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
  			usb_rcvisocpipe(dev, subs->endpoint);
  	subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
  	if (!subs->maxpacksize)
  		return -EINVAL;
  
  	if (is_playback && NULL == subs->tmpbuf) {	/* allocate a temporary buffer for playback */
  		subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
  		if (NULL == subs->tmpbuf) {
  			snd_printk(KERN_ERR "cannot malloc tmpbuf
  ");
  			return -ENOMEM;
  		}
  	}
  	/* allocate and initialize data urbs */
  	for (i = 0; i < NRURBS; i++) {
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
442
  		struct urb **purb = subs->urb + i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  		if (*purb) {
  			usb_kill_urb(*purb);
  			continue;
  		}
  		*purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
  		if (NULL == *purb) {
  			usX2Y_urbs_release(subs);
  			return -ENOMEM;
  		}
  		if (!is_playback && !(*purb)->transfer_buffer) {
  			/* allocate a capture buffer per urb */
  			(*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
  			if (NULL == (*purb)->transfer_buffer) {
  				usX2Y_urbs_release(subs);
  				return -ENOMEM;
  			}
  		}
  		(*purb)->dev = dev;
  		(*purb)->pipe = pipe;
  		(*purb)->number_of_packets = nr_of_packs();
  		(*purb)->context = subs;
  		(*purb)->interval = 1;
  		(*purb)->complete = i_usX2Y_subs_startup;
  	}
  	return 0;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
469
  static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
471
  	struct usX2Ydev *usX2Y = subs->usX2Y;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
474
475
476
  	usX2Y->prepare_subs = subs;
  	subs->urb[0]->start_frame = -1;
  	wmb();
  	usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
477
  static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
  {
  	int i, err;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
480
  	struct usX2Ydev *usX2Y = subs->usX2Y;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
482
483
484
485
  
  	if ((err = usX2Y_urbs_allocate(subs)) < 0)
  		return err;
  	subs->completed_urb = NULL;
  	for (i = 0; i < 4; i++) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
486
  		struct snd_usX2Y_substream *subs = usX2Y->subs[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
  		if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
  			goto start;
  	}
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
490

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
   start:
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
492
493
494
495
496
497
498
  	usX2Y_subs_startup(subs);
  	for (i = 0; i < NRURBS; i++) {
  		struct urb *urb = subs->urb[i];
  		if (usb_pipein(urb->pipe)) {
  			unsigned long pack;
  			if (0 == i)
  				atomic_set(&subs->state, state_STARTING3);
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
499
  			urb->dev = usX2Y->dev;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
500
501
502
503
504
505
506
507
508
509
510
  			urb->transfer_flags = URB_ISO_ASAP;
  			for (pack = 0; pack < nr_of_packs(); pack++) {
  				urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
  				urb->iso_frame_desc[pack].length = subs->maxpacksize;
  			}
  			urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 
  			if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  				snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d
  ", i, err);
  				err = -EPIPE;
  				goto cleanup;
635bbb355   Karsten Wiese   [ALSA] Repair snd...
511
512
  			} else
  				if (i == 0)
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
513
  					usX2Y->wait_iso_frame = urb->start_frame;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
514
515
516
517
  			urb->transfer_flags = 0;
  		} else {
  			atomic_set(&subs->state, state_STARTING1);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
  		}
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
519
520
521
522
523
524
525
526
527
528
  	}
  	err = 0;
  	wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
  	if (atomic_read(&subs->state) != state_PREPARED)
  		err = -EPIPE;
  
   cleanup:
  	if (err) {
  		usX2Y_subs_startup_finish(usX2Y);
  		usX2Y_clients_stop(usX2Y);		// something is completely wroong > stop evrything
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
  	}
  	return err;
  }
  
  /*
   * return the current pcm pointer.  just return the hwptr_done value.
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
536
  static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
538
  	struct snd_usX2Y_substream *subs = substream->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
540
541
542
543
  	return subs->hwptr_done;
  }
  /*
   * start/stop substream
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
544
  static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
546
  	struct snd_usX2Y_substream *subs = substream->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  		snd_printdd("snd_usX2Y_pcm_trigger(START)
  ");
  		if (atomic_read(&subs->state) == state_PREPARED &&
  		    atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
  			atomic_set(&subs->state, state_PRERUNNING);
  		} else {
  			snd_printdd("
  ");
  			return -EPIPE;
  		}
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  		snd_printdd("snd_usX2Y_pcm_trigger(STOP)
  ");
  		if (atomic_read(&subs->state) >= state_PRERUNNING)
  			atomic_set(&subs->state, state_PREPARED);
  		break;
  	default:
  		return -EINVAL;
  	}
  	return 0;
  }
  
  
  /*
   * allocate a buffer, setup samplerate
   *
   * so far we use a physically linear buffer although packetize transfer
   * doesn't need a continuous area.
   * if sg buffer is supported on the later version of alsa, we'll follow
   * that.
   */
  static struct s_c2
  {
  	char c1, c2;
  }
  	SetRate44100[] =
  {
  	{ 0x14, 0x08},	// this line sets 44100, well actually a little less
  	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
  	{ 0x18, 0x42},
  	{ 0x18, 0x45},
  	{ 0x18, 0x46},
  	{ 0x18, 0x48},
  	{ 0x18, 0x4A},
  	{ 0x18, 0x4C},
  	{ 0x18, 0x4E},
  	{ 0x18, 0x50},
  	{ 0x18, 0x52},
  	{ 0x18, 0x54},
  	{ 0x18, 0x56},
  	{ 0x18, 0x58},
  	{ 0x18, 0x5A},
  	{ 0x18, 0x5C},
  	{ 0x18, 0x5E},
  	{ 0x18, 0x60},
  	{ 0x18, 0x62},
  	{ 0x18, 0x64},
  	{ 0x18, 0x66},
  	{ 0x18, 0x68},
  	{ 0x18, 0x6A},
  	{ 0x18, 0x6C},
  	{ 0x18, 0x6E},
  	{ 0x18, 0x70},
  	{ 0x18, 0x72},
  	{ 0x18, 0x74},
  	{ 0x18, 0x76},
  	{ 0x18, 0x78},
  	{ 0x18, 0x7A},
  	{ 0x18, 0x7C},
  	{ 0x18, 0x7E}
  };
  static struct s_c2 SetRate48000[] =
  {
  	{ 0x14, 0x09},	// this line sets 48000, well actually a little less
  	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
  	{ 0x18, 0x42},
  	{ 0x18, 0x45},
  	{ 0x18, 0x46},
  	{ 0x18, 0x48},
  	{ 0x18, 0x4A},
  	{ 0x18, 0x4C},
  	{ 0x18, 0x4E},
  	{ 0x18, 0x50},
  	{ 0x18, 0x52},
  	{ 0x18, 0x54},
  	{ 0x18, 0x56},
  	{ 0x18, 0x58},
  	{ 0x18, 0x5A},
  	{ 0x18, 0x5C},
  	{ 0x18, 0x5E},
  	{ 0x18, 0x60},
  	{ 0x18, 0x62},
  	{ 0x18, 0x64},
  	{ 0x18, 0x66},
  	{ 0x18, 0x68},
  	{ 0x18, 0x6A},
  	{ 0x18, 0x6C},
  	{ 0x18, 0x6E},
  	{ 0x18, 0x70},
  	{ 0x18, 0x73},
  	{ 0x18, 0x74},
  	{ 0x18, 0x76},
  	{ 0x18, 0x78},
  	{ 0x18, 0x7A},
  	{ 0x18, 0x7C},
  	{ 0x18, 0x7E}
  };
  #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
7d12e780e   David Howells   IRQ: Maintain reg...
659
  static void i_usX2Y_04Int(struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
661
  	struct usX2Ydev *usX2Y = urb->context;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
  	
d3d579f84   Takashi Iwai   [ALSA] Add missin...
663
664
665
  	if (urb->status)
  		snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i
  ", urb->status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
  	if (0 == --usX2Y->US04->len)
  		wake_up(&usX2Y->In04WaitQueue);
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
669
  static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
671
  {
  	int			err = 0, i;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
672
  	struct snd_usX2Y_urbSeq	*us = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
  	int			*usbdata = NULL;
  	struct s_c2		*ra = rate == 48000 ? SetRate48000 : SetRate44100;
  
  	if (usX2Y->rate != rate) {
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
677
  		us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
  		if (NULL == us) {
  			err = -ENOMEM;
  			goto cleanup;
  		}
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
682
  		usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
685
686
687
688
689
690
691
692
693
  		if (NULL == usbdata) {
  			err = -ENOMEM;
  			goto cleanup;
  		}
  		for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  			if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  				err = -ENOMEM;
  				goto cleanup;
  			}
  			((char*)(usbdata + i))[0] = ra[i].c1;
  			((char*)(usbdata + i))[1] = ra[i].c2;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
694
  			usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
  					  usbdata + i, 2, i_usX2Y_04Int, usX2Y);
  #ifdef OLD_USB
  			us->urb[i]->transfer_flags = USB_QUEUE_BULK;
  #endif
  		}
  		us->submitted =	0;
  		us->len =	NOOF_SETRATE_URBS;
  		usX2Y->US04 =	us;
  		wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
  		usX2Y->US04 =	NULL;
  		if (us->len)
  			err = -ENODEV;
  	cleanup:
  		if (us) {
  			us->submitted =	2*NOOF_SETRATE_URBS;
  			for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  				struct urb *urb = us->urb[i];
  				if (urb->status) {
  					if (!err)
  						err = -ENODEV;
  					usb_kill_urb(urb);
  				}
  				usb_free_urb(urb);
  			}
  			usX2Y->US04 = NULL;
  			kfree(usbdata);
  			kfree(us);
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
722
  			if (!err)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
  				usX2Y->rate = rate;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
725
726
727
728
  		}
  	}
  
  	return err;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
729
  static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
731
732
733
734
735
736
737
738
739
  {
  	int alternate, err;
  	struct list_head* p;
  	if (format == SNDRV_PCM_FORMAT_S24_3LE) {
  		alternate = 2;
  		usX2Y->stride = 6;
  	} else {
  		alternate = 1;
  		usX2Y->stride = 4;
  	}
d82af9f9a   Clemens Ladisch   sound: usb: make ...
740
  	list_for_each(p, &usX2Y->midi_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
743
  		snd_usbmidi_input_stop(p);
  	}
  	usb_kill_urb(usX2Y->In04urb);
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
744
  	if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) {
d3d579f84   Takashi Iwai   [ALSA] Add missin...
745
746
  		snd_printk(KERN_ERR "usb_set_interface error 
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
748
  		return err;
  	}
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
749
  	usX2Y->In04urb->dev = usX2Y->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
  	err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
d82af9f9a   Clemens Ladisch   sound: usb: make ...
751
  	list_for_each(p, &usX2Y->midi_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
756
757
  		snd_usbmidi_input_start(p);
  	}
  	usX2Y->format = format;
  	usX2Y->rate = 0;
  	return err;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
758
759
  static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
  				   struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
760
761
762
763
  {
  	int			err = 0;
  	unsigned int		rate = params_rate(hw_params);
  	snd_pcm_format_t	format = params_format(hw_params);
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
764
765
  	struct snd_card *card = substream->pstr->pcm->card;
  	struct list_head *list;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
766
767
768
769
770
  
  	snd_printdd("snd_usX2Y_hw_params(%p, %p)
  ", substream, hw_params);
  	// all pcm substreams off one usX2Y have to operate at the same rate & format
  	list_for_each(list, &card->devices) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
771
772
  		struct snd_device *dev;
  		struct snd_pcm *pcm;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
773
774
775
776
777
778
  		int s;
  		dev = snd_device(list);
  		if (dev->type != SNDRV_DEV_PCM)
  			continue;
  		pcm = dev->device_data;
  		for (s = 0; s < 2; ++s) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
779
  			struct snd_pcm_substream *test_substream;
cb432379e   Takashi Iwai   [ALSA] usx2y - Co...
780
781
782
783
784
785
786
787
  			test_substream = pcm->streams[s].substream;
  			if (test_substream && test_substream != substream  &&
  			    test_substream->runtime &&
  			    ((test_substream->runtime->format &&
  			      test_substream->runtime->format != format) ||
  			     (test_substream->runtime->rate &&
  			      test_substream->runtime->rate != rate)))
  				return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
  		}
  	}
  	if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
791
792
793
  		snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i
  ",
  			   substream, params_buffer_bytes(hw_params), err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
797
798
799
800
801
  		return err;
  	}
  	return 0;
  }
  
  /*
   * free the buffer
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
802
  static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
803
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
804
805
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_usX2Y_substream *subs = runtime->private_data;
12aa75790   Ingo Molnar   [ALSA] semaphore ...
806
  	mutex_lock(&subs->usX2Y->prepare_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
808
809
810
  	snd_printdd("snd_usX2Y_hw_free(%p)
  ", substream);
  
  	if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
811
  		struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
815
816
817
818
819
820
821
  		atomic_set(&subs->state, state_STOPPED);
  		usX2Y_urbs_release(subs);
  		if (!cap_subs->pcm_substream ||
  		    !cap_subs->pcm_substream->runtime ||
  		    !cap_subs->pcm_substream->runtime->status ||
  		    cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
  			atomic_set(&cap_subs->state, state_STOPPED);
  			usX2Y_urbs_release(cap_subs);
  		}
  	} else {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
822
  		struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
826
827
  		if (atomic_read(&playback_subs->state) < state_PREPARED) {
  			atomic_set(&subs->state, state_STOPPED);
  			usX2Y_urbs_release(subs);
  		}
  	}
12aa75790   Ingo Molnar   [ALSA] semaphore ...
828
  	mutex_unlock(&subs->usX2Y->prepare_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829
830
831
832
833
834
835
  	return snd_pcm_lib_free_pages(substream);
  }
  /*
   * prepare callback
   *
   * set format and initialize urbs
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
836
  static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
838
839
840
841
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_usX2Y_substream *subs = runtime->private_data;
  	struct usX2Ydev *usX2Y = subs->usX2Y;
  	struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
  	int err = 0;
  	snd_printdd("snd_usX2Y_pcm_prepare(%p)
  ", substream);
12aa75790   Ingo Molnar   [ALSA] semaphore ...
845
  	mutex_lock(&usX2Y->prepare_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  	usX2Y_subs_prepare(subs);
  // Start hardware streams
  // SyncStream first....
  	if (atomic_read(&capsubs->state) < state_PREPARED) {
  		if (usX2Y->format != runtime->format)
  			if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
  				goto up_prepare_mutex;
  		if (usX2Y->rate != runtime->rate)
  			if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
  				goto up_prepare_mutex;
  		snd_printdd("starting capture pipe for %s
  ", subs == capsubs ? "self" : "playpipe");
  		if (0 > (err = usX2Y_urbs_start(capsubs)))
  			goto up_prepare_mutex;
  	}
  
  	if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
  		err = usX2Y_urbs_start(subs);
  
   up_prepare_mutex:
12aa75790   Ingo Molnar   [ALSA] semaphore ...
866
  	mutex_unlock(&usX2Y->prepare_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
868
  	return err;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
869
  static struct snd_pcm_hardware snd_usX2Y_2c =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870
871
872
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
2008f137e   Takashi Iwai   ALSA: Add missing...
873
874
  				 SNDRV_PCM_INFO_MMAP_VALID |
  				 SNDRV_PCM_INFO_BATCH),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
877
878
879
880
881
882
883
884
885
886
887
  	.formats =                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
  	.rates =                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  	.rate_min =                44100,
  	.rate_max =                48000,
  	.channels_min =            2,
  	.channels_max =            2,
  	.buffer_bytes_max =	(2*128*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(128*1024),
  	.periods_min =		2,
  	.periods_max =		1024,
  	.fifo_size =              0
  };
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
888
  static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
890
  	struct snd_usX2Y_substream	*subs = ((struct snd_usX2Y_substream **)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
891
  					 snd_pcm_substream_chip(substream))[substream->stream];
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
892
  	struct snd_pcm_runtime	*runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
894
895
896
897
898
899
900
901
902
  
  	if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
  		return -EBUSY;
  
  	runtime->hw = snd_usX2Y_2c;
  	runtime->private_data = subs;
  	subs->pcm_substream = substream;
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
  	return 0;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
903
  static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
905
906
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_usX2Y_substream *subs = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
  
  	subs->pcm_substream = NULL;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
909
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
911
  static struct snd_pcm_ops snd_usX2Y_pcm_ops = 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
  {
  	.open =		snd_usX2Y_pcm_open,
  	.close =	snd_usX2Y_pcm_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_usX2Y_pcm_hw_params,
  	.hw_free =	snd_usX2Y_pcm_hw_free,
  	.prepare =	snd_usX2Y_pcm_prepare,
  	.trigger =	snd_usX2Y_pcm_trigger,
  	.pointer =	snd_usX2Y_pcm_pointer,
  };
  
  
  /*
   * free a usb stream instance
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
927
  static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
  {
c111b8de9   Richard Knutsson   [ALSA] usbusx2yau...
929
930
  	kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
  	usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
  	kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
  	usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
934
  static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
936
  	struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
c3e6f7d87   Takashi Iwai   [ALSA] Remove sup...
937
  	if (usX2Y_stream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
  		usX2Y_audio_stream_free(usX2Y_stream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
  }
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
940
  static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
941
  {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
942
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
  	int err, i;
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
944
  	struct snd_usX2Y_substream **usX2Y_substream =
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
945
  		usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
947
948
  
  	for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
  	     i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
949
  		usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
950
951
952
953
954
955
956
957
958
959
960
  		if (NULL == usX2Y_substream[i]) {
  			snd_printk(KERN_ERR "cannot malloc
  ");
  			return -ENOMEM;
  		}
  		usX2Y_substream[i]->usX2Y = usX2Y(card);
  	}
  
  	if (playback_endpoint)
  		usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
  	usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
961
  	err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
963
964
965
966
967
968
969
970
971
972
973
974
975
  			  playback_endpoint ? 1 : 0, 1,
  			  &pcm);
  	if (err < 0) {
  		usX2Y_audio_stream_free(usX2Y_substream);
  		return err;
  	}
  
  	if (playback_endpoint)
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
  
  	pcm->private_data = usX2Y_substream;
  	pcm->private_free = snd_usX2Y_pcm_private_free;
  	pcm->info_flags = 0;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
976
  	sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
977
978
979
980
981
982
983
984
985
986
987
988
989
  
  	if ((playback_endpoint &&
  	     0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  						     SNDRV_DMA_TYPE_CONTINUOUS,
  						     snd_dma_continuous_data(GFP_KERNEL),
  						     64*1024, 128*1024))) ||
  	    0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  	    					     SNDRV_DMA_TYPE_CONTINUOUS,
  	    					     snd_dma_continuous_data(GFP_KERNEL),
  						     64*1024, 128*1024))) {
  		snd_usX2Y_pcm_private_free(pcm);
  		return err;
  	}
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
990
  	usX2Y(card)->pcm_devs++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
991
992
993
994
995
996
997
  
  	return 0;
  }
  
  /*
   * create a chip instance and set its names.
   */
bbe85bbd0   Takashi Iwai   [ALSA] Remove xxx...
998
  int usX2Y_audio_create(struct snd_card *card)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
  {
  	int err = 0;
  	
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
1002
  	INIT_LIST_HEAD(&usX2Y(card)->pcm_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003
1004
1005
  
  	if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
  		return err;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
1006
  	if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
1008
  	     if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
  		     return err;
a014bbadb   Clemens Ladisch   sound: usxxx: cle...
1009
  	if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
  		err = usX2Y_rate_set(usX2Y(card), 44100);	// Lets us428 recognize output-volume settings, disturbs us122.
  	return err;
  }