]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/mixart/mixart.c
[PATCH] drivers/scsi/*: use time_after() and friends
[net-next-2.6.git] / sound / pci / mixart / mixart.c
CommitLineData
1da177e4
LT
1/*
2 * Driver for Digigram miXart soundcards
3 *
4 * main file with alsa callbacks
5 *
6 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23
24#include <sound/driver.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/pci.h>
9d2f928d 28#include <linux/dma-mapping.h>
1da177e4 29#include <linux/moduleparam.h>
62932df8 30#include <linux/mutex.h>
1da177e4
LT
31#include <sound/core.h>
32#include <sound/initval.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include "mixart.h"
38#include "mixart_hwdep.h"
39#include "mixart_core.h"
40#include "mixart_mixer.h"
41
42#define CARD_NAME "miXart"
43
44MODULE_AUTHOR("Digigram <alsa@digigram.com>");
45MODULE_DESCRIPTION("Digigram " CARD_NAME);
46MODULE_LICENSE("GPL");
47MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
48
49static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
50static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
51static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
59
60/*
61 */
62
63static struct pci_device_id snd_mixart_ids[] = {
64 { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
65 { 0, }
66};
67
68MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
69
70
67b48b88
TI
71static int mixart_set_pipe_state(struct mixart_mgr *mgr,
72 struct mixart_pipe *pipe, int start)
1da177e4 73{
67b48b88
TI
74 struct mixart_group_state_req group_state;
75 struct mixart_group_state_resp group_state_resp;
76 struct mixart_msg request;
1da177e4
LT
77 int err;
78 u32 system_msg_uid;
79
80 switch(pipe->status) {
81 case PIPE_RUNNING:
82 case PIPE_CLOCK_SET:
83 if(start) return 0; /* already started */
84 break;
85 case PIPE_STOPPED:
86 if(!start) return 0; /* already stopped */
87 break;
88 default:
89 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
90 return -EINVAL; /* function called with wrong pipe status */
91 }
92
93 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
94
95 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
96
97 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
67b48b88 98 request.uid = (struct mixart_uid){0,0};
1da177e4
LT
99 request.data = &system_msg_uid;
100 request.size = sizeof(system_msg_uid);
101
102 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
103 if(err) {
104 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
105 return err;
106 }
107
108 /* start or stop the pipe (1 pipe) */
109
110 memset(&group_state, 0, sizeof(group_state));
111 group_state.pipe_count = 1;
112 group_state.pipe_uid[0] = pipe->group_uid;
113
114 if(start)
115 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
116 else
117 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
118
67b48b88 119 request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
1da177e4
LT
120 request.data = &group_state;
121 request.size = sizeof(group_state);
122
123 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
124 if (err < 0 || group_state_resp.txx_status != 0) {
125 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
126 return -EINVAL;
127 }
128
129 if(start) {
130 u32 stat;
131
132 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
133
134 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
135 if (err < 0 || group_state_resp.txx_status != 0) {
136 snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
137 return -EINVAL;
138 }
139
140 /* in case of start send a synchro top */
141
142 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
67b48b88 143 request.uid = (struct mixart_uid){0,0};
1da177e4
LT
144 request.data = NULL;
145 request.size = 0;
146
147 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
148 if (err < 0 || stat != 0) {
149 snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
150 return -EINVAL;
151 }
152
153 pipe->status = PIPE_RUNNING;
154 }
155 else /* !start */
156 pipe->status = PIPE_STOPPED;
157
158 return 0;
159}
160
161
67b48b88
TI
162static int mixart_set_clock(struct mixart_mgr *mgr,
163 struct mixart_pipe *pipe, unsigned int rate)
1da177e4 164{
67b48b88
TI
165 struct mixart_msg request;
166 struct mixart_clock_properties clock_properties;
167 struct mixart_clock_properties_resp clock_prop_resp;
1da177e4
LT
168 int err;
169
170 switch(pipe->status) {
171 case PIPE_CLOCK_SET:
172 break;
173 case PIPE_RUNNING:
174 if(rate != 0)
175 break;
176 default:
177 if(rate == 0)
178 return 0; /* nothing to do */
179 else {
180 snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
181 return -EINVAL;
182 }
183 }
184
185 memset(&clock_properties, 0, sizeof(clock_properties));
186 clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
187 clock_properties.clock_mode = CM_STANDALONE;
188 clock_properties.frequency = rate;
189 clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
190 clock_properties.uid_caller[0] = pipe->group_uid;
191
192 snd_printdd("mixart_set_clock to %d kHz\n", rate);
193
194 request.message_id = MSG_CLOCK_SET_PROPERTIES;
195 request.uid = mgr->uid_console_manager;
196 request.data = &clock_properties;
197 request.size = sizeof(clock_properties);
198
199 err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
200 if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
201 snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);
202 return -EINVAL;
203 }
204
205 if(rate) pipe->status = PIPE_CLOCK_SET;
206 else pipe->status = PIPE_RUNNING;
207
208 return 0;
209}
210
211
212/*
213 * Allocate or reference output pipe for analog IOs (pcmp0/1)
214 */
67b48b88
TI
215struct mixart_pipe *
216snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
217 int monitoring)
1da177e4
LT
218{
219 int stream_count;
67b48b88
TI
220 struct mixart_pipe *pipe;
221 struct mixart_msg request;
1da177e4
LT
222
223 if(capture) {
224 if (pcm_number == MIXART_PCM_ANALOG) {
225 pipe = &(chip->pipe_in_ana); /* analog inputs */
226 } else {
227 pipe = &(chip->pipe_in_dig); /* digital inputs */
228 }
229 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
230 stream_count = MIXART_CAPTURE_STREAMS;
231 } else {
232 if (pcm_number == MIXART_PCM_ANALOG) {
233 pipe = &(chip->pipe_out_ana); /* analog outputs */
234 } else {
235 pipe = &(chip->pipe_out_dig); /* digital outputs */
236 }
237 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
238 stream_count = MIXART_PLAYBACK_STREAMS;
239 }
240
241 /* a new stream is opened and there are already all streams in use */
242 if( (monitoring == 0) && (pipe->references >= stream_count) ) {
243 return NULL;
244 }
245
246 /* pipe is not yet defined */
247 if( pipe->status == PIPE_UNDEFINED ) {
248 int err, i;
249 struct {
67b48b88
TI
250 struct mixart_streaming_group_req sgroup_req;
251 struct mixart_streaming_group sgroup_resp;
1da177e4
LT
252 } *buf;
253
254 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
255
256 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
257 if (!buf)
258 return NULL;
259
67b48b88 260 request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
1da177e4
LT
261 request.data = &buf->sgroup_req;
262 request.size = sizeof(buf->sgroup_req);
263
264 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
265
266 buf->sgroup_req.stream_count = stream_count;
267 buf->sgroup_req.channel_count = 2;
268 buf->sgroup_req.latency = 256;
269 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
270
271 for (i=0; i<stream_count; i++) {
272 int j;
273 struct mixart_flowinfo *flowinfo;
274 struct mixart_bufferinfo *bufferinfo;
275
276 /* we don't yet know the format, so config 16 bit pcm audio for instance */
277 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
278 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
279 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
280
281 /* find the right bufferinfo_array */
282 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
283 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
284
285 buf->sgroup_req.flow_entry[i] = j;
286
287 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
67b48b88 288 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
1da177e4
LT
289 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
290
291 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
292 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
293 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
294
295 /* construct the identifier of the stream buffer received in the interrupts ! */
296 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
297 if(capture) {
298 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
299 }
300 }
301
302 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
303 if((err < 0) || (buf->sgroup_resp.status != 0)) {
304 snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
305 kfree(buf);
306 return NULL;
307 }
308
309 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
310 pipe->stream_count = buf->sgroup_resp.stream_count;
311 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
312
313 pipe->status = PIPE_STOPPED;
314 kfree(buf);
315 }
316
317 if(monitoring) pipe->monitoring = 1;
318 else pipe->references++;
319
320 return pipe;
321}
322
323
67b48b88
TI
324int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
325 struct mixart_pipe *pipe, int monitoring)
1da177e4
LT
326{
327 int err = 0;
328
329 if(pipe->status == PIPE_UNDEFINED)
330 return 0;
331
332 if(monitoring)
333 pipe->monitoring = 0;
334 else
335 pipe->references--;
336
337 if((pipe->references <= 0) && (pipe->monitoring == 0)) {
338
67b48b88
TI
339 struct mixart_msg request;
340 struct mixart_delete_group_resp delete_resp;
1da177e4
LT
341
342 /* release the clock */
343 err = mixart_set_clock( mgr, pipe, 0);
344 if( err < 0 ) {
345 snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
346 }
347
348 /* stop the pipe */
349 err = mixart_set_pipe_state(mgr, pipe, 0);
350 if( err < 0 ) {
351 snd_printk(KERN_ERR "error stopping pipe!\n");
352 }
353
354 request.message_id = MSG_STREAM_DELETE_GROUP;
67b48b88 355 request.uid = (struct mixart_uid){0,0};
1da177e4
LT
356 request.data = &pipe->group_uid; /* the streaming group ! */
357 request.size = sizeof(pipe->group_uid);
358
359 /* delete the pipe */
360 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
361 if ((err < 0) || (delete_resp.status != 0)) {
362 snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
363 }
364
67b48b88 365 pipe->group_uid = (struct mixart_uid){0,0};
1da177e4
LT
366 pipe->stream_count = 0;
367 pipe->status = PIPE_UNDEFINED;
368 }
369
370 return err;
371}
372
67b48b88 373static int mixart_set_stream_state(struct mixart_stream *stream, int start)
1da177e4 374{
67b48b88
TI
375 struct snd_mixart *chip;
376 struct mixart_stream_state_req stream_state_req;
377 struct mixart_msg request;
1da177e4
LT
378
379 if(!stream->substream)
380 return -EINVAL;
381
382 memset(&stream_state_req, 0, sizeof(stream_state_req));
383 stream_state_req.stream_count = 1;
384 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
385 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
386
387 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
389 else
390 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
391
67b48b88 392 request.uid = (struct mixart_uid){0,0};
1da177e4
LT
393 request.data = &stream_state_req;
394 request.size = sizeof(stream_state_req);
395
396 stream->abs_period_elapsed = 0; /* reset stream pos */
397 stream->buf_periods = 0;
398 stream->buf_period_frag = 0;
399
400 chip = snd_pcm_substream_chip(stream->substream);
401
402 return snd_mixart_send_msg_nonblock(chip->mgr, &request);
403}
404
405/*
406 * Trigger callback
407 */
408
67b48b88 409static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
1da177e4 410{
67b48b88 411 struct mixart_stream *stream = subs->runtime->private_data;
1da177e4
LT
412
413 switch (cmd) {
414 case SNDRV_PCM_TRIGGER_START:
415
416 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
417
418 /* START_STREAM */
419 if( mixart_set_stream_state(stream, 1) )
420 return -EINVAL;
421
422 stream->status = MIXART_STREAM_STATUS_RUNNING;
423
424 break;
425 case SNDRV_PCM_TRIGGER_STOP:
426
427 /* STOP_STREAM */
428 if( mixart_set_stream_state(stream, 0) )
429 return -EINVAL;
430
431 stream->status = MIXART_STREAM_STATUS_OPEN;
432
433 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
434
435 break;
436
437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
438 /* TODO */
439 stream->status = MIXART_STREAM_STATUS_PAUSE;
440 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
441 break;
442 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
443 /* TODO */
444 stream->status = MIXART_STREAM_STATUS_RUNNING;
445 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
446 break;
447 default:
448 return -EINVAL;
449 }
450 return 0;
451}
452
67b48b88 453static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
1da177e4 454{
ef21ca24 455 unsigned long timeout = jiffies + HZ;
1da177e4 456 while (atomic_read(&mgr->msg_processed) > 0) {
ef21ca24 457 if (time_after(jiffies, timeout)) {
1da177e4
LT
458 snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
459 return -EBUSY;
460 }
8433a509 461 schedule_timeout_uninterruptible(1);
1da177e4
LT
462 }
463 return 0;
464}
465
466/*
467 * prepare callback for all pcms
468 */
67b48b88 469static int snd_mixart_prepare(struct snd_pcm_substream *subs)
1da177e4 470{
67b48b88
TI
471 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
472 struct mixart_stream *stream = subs->runtime->private_data;
1da177e4
LT
473
474