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