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