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