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