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