1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Digigram miXart soundcards 4 * 5 * main file with alsa callbacks 6 * 7 * Copyright (c) 2003 by Digigram <alsa@digigram.com> 8 */ 9 10 11 #include <linux/init.h> 12 #include <linux/interrupt.h> 13 #include <linux/pci.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/module.h> 16 #include <linux/mutex.h> 17 #include <linux/slab.h> 18 19 #include <sound/core.h> 20 #include <sound/initval.h> 21 #include <sound/info.h> 22 #include <sound/control.h> 23 #include <sound/pcm.h> 24 #include <sound/pcm_params.h> 25 #include "mixart.h" 26 #include "mixart_hwdep.h" 27 #include "mixart_core.h" 28 #include "mixart_mixer.h" 29 30 #define CARD_NAME "miXart" 31 32 MODULE_AUTHOR("Digigram <alsa@digigram.com>"); 33 MODULE_DESCRIPTION("Digigram " CARD_NAME); 34 MODULE_LICENSE("GPL"); 35 MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}"); 36 37 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 38 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 39 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ 40 41 module_param_array(index, int, NULL, 0444); 42 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard."); 43 module_param_array(id, charp, NULL, 0444); 44 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard."); 45 module_param_array(enable, bool, NULL, 0444); 46 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard."); 47 48 /* 49 */ 50 51 static const struct pci_device_id snd_mixart_ids[] = { 52 { PCI_VDEVICE(MOTOROLA, 0x0003), 0, }, /* MC8240 */ 53 { 0, } 54 }; 55 56 MODULE_DEVICE_TABLE(pci, snd_mixart_ids); 57 58 59 static int mixart_set_pipe_state(struct mixart_mgr *mgr, 60 struct mixart_pipe *pipe, int start) 61 { 62 struct mixart_group_state_req group_state; 63 struct mixart_group_state_resp group_state_resp; 64 struct mixart_msg request; 65 int err; 66 u32 system_msg_uid; 67 68 switch(pipe->status) { 69 case PIPE_RUNNING: 70 case PIPE_CLOCK_SET: 71 if(start) return 0; /* already started */ 72 break; 73 case PIPE_STOPPED: 74 if(!start) return 0; /* already stopped */ 75 break; 76 default: 77 dev_err(&mgr->pci->dev, 78 "error mixart_set_pipe_state called with wrong pipe->status!\n"); 79 return -EINVAL; /* function called with wrong pipe status */ 80 } 81 82 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */ 83 84 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */ 85 86 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD; 87 request.uid = (struct mixart_uid){0,0}; 88 request.data = &system_msg_uid; 89 request.size = sizeof(system_msg_uid); 90 91 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid); 92 if(err) { 93 dev_err(&mgr->pci->dev, 94 "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n"); 95 return err; 96 } 97 98 /* start or stop the pipe (1 pipe) */ 99 100 memset(&group_state, 0, sizeof(group_state)); 101 group_state.pipe_count = 1; 102 group_state.pipe_uid[0] = pipe->group_uid; 103 104 if(start) 105 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET; 106 else 107 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET; 108 109 request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/ 110 request.data = &group_state; 111 request.size = sizeof(group_state); 112 113 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp); 114 if (err < 0 || group_state_resp.txx_status != 0) { 115 dev_err(&mgr->pci->dev, 116 "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", 117 err, group_state_resp.txx_status); 118 return -EINVAL; 119 } 120 121 if(start) { 122 u32 stat = 0; 123 124 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */ 125 126 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp); 127 if (err < 0 || group_state_resp.txx_status != 0) { 128 dev_err(&mgr->pci->dev, 129 "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", 130 err, group_state_resp.txx_status); 131 return -EINVAL; 132 } 133 134 /* in case of start send a synchro top */ 135 136 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD; 137 request.uid = (struct mixart_uid){0,0}; 138 request.data = NULL; 139 request.size = 0; 140 141 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat); 142 if (err < 0 || stat != 0) { 143 dev_err(&mgr->pci->dev, 144 "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", 145 err, stat); 146 return -EINVAL; 147 } 148 149 pipe->status = PIPE_RUNNING; 150 } 151 else /* !start */ 152 pipe->status = PIPE_STOPPED; 153 154 return 0; 155 } 156 157 158 static int mixart_set_clock(struct mixart_mgr *mgr, 159 struct mixart_pipe *pipe, unsigned int rate) 160 { 161 struct mixart_msg request; 162 struct mixart_clock_properties clock_properties; 163 struct mixart_clock_properties_resp 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 /* fall through */ 173 default: 174 if(rate == 0) 175 return 0; /* nothing to do */ 176 else { 177 dev_err(&mgr->pci->dev, 178 "error mixart_set_clock(%d) called with wrong pipe->status !\n", 179 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 dev_dbg(&mgr->pci->dev, "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 dev_err(&mgr->pci->dev, 201 "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", 202 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 */ 216 struct mixart_pipe * 217 snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture, 218 int monitoring) 219 { 220 int stream_count; 221 struct mixart_pipe *pipe; 222 struct mixart_msg request; 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 { 251 struct mixart_streaming_group_req sgroup_req; 252 struct mixart_streaming_group sgroup_resp; 253 } *buf; 254 255 dev_dbg(chip->card->dev, 256 "add_ref_pipe audio chip(%d) pcm(%d)\n", 257 chip->chip_idx, pcm_number); 258 259 buf = kmalloc(sizeof(*buf), GFP_KERNEL); 260 if (!buf) 261 return NULL; 262 263 request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */ 264 request.data = &buf->sgroup_req; 265 request.size = sizeof(buf->sgroup_req); 266 267 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req)); 268 269 buf->sgroup_req.stream_count = stream_count; 270 buf->sgroup_req.channel_count = 2; 271 buf->sgroup_req.latency = 256; 272 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */ 273 274 for (i=0; i<stream_count; i++) { 275 int j; 276 struct mixart_flowinfo *flowinfo; 277 struct mixart_bufferinfo *bufferinfo; 278 279 /* we don't yet know the format, so config 16 bit pcm audio for instance */ 280 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024; 281 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256; 282 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */ 283 284 /* find the right bufferinfo_array */ 285 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i; 286 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */ 287 288 buf->sgroup_req.flow_entry[i] = j; 289 290 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area; 291 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo)); 292 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */ 293 294 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area; 295 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */ 296 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */ 297 298 /* construct the identifier of the stream buffer received in the interrupts ! */ 299 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i; 300 if(capture) { 301 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK; 302 } 303 } 304 305 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp); 306 if((err < 0) || (buf->sgroup_resp.status != 0)) { 307 dev_err(chip->card->dev, 308 "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", 309 err, buf->sgroup_resp.status); 310 kfree(buf); 311 return NULL; 312 } 313 314 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */ 315 pipe->stream_count = buf->sgroup_resp.stream_count; 316 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */ 317 318 pipe->status = PIPE_STOPPED; 319 kfree(buf); 320 } 321 322 if(monitoring) pipe->monitoring = 1; 323 else pipe->references++; 324 325 return pipe; 326 } 327 328 329 int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr, 330 struct mixart_pipe *pipe, int monitoring) 331 { 332 int err = 0; 333 334 if(pipe->status == PIPE_UNDEFINED) 335 return 0; 336 337 if(monitoring) 338 pipe->monitoring = 0; 339 else 340 pipe->references--; 341 342 if((pipe->references <= 0) && (pipe->monitoring == 0)) { 343 344 struct mixart_msg request; 345 struct mixart_delete_group_resp delete_resp; 346 347 /* release the clock */ 348 err = mixart_set_clock( mgr, pipe, 0); 349 if( err < 0 ) { 350 dev_err(&mgr->pci->dev, 351 "mixart_set_clock(0) return error!\n"); 352 } 353 354 /* stop the pipe */ 355 err = mixart_set_pipe_state(mgr, pipe, 0); 356 if( err < 0 ) { 357 dev_err(&mgr->pci->dev, "error stopping pipe!\n"); 358 } 359 360 request.message_id = MSG_STREAM_DELETE_GROUP; 361 request.uid = (struct mixart_uid){0,0}; 362 request.data = &pipe->group_uid; /* the streaming group ! */ 363 request.size = sizeof(pipe->group_uid); 364 365 /* delete the pipe */ 366 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp); 367 if ((err < 0) || (delete_resp.status != 0)) { 368 dev_err(&mgr->pci->dev, 369 "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", 370 err, delete_resp.status); 371 } 372 373 pipe->group_uid = (struct mixart_uid){0,0}; 374 pipe->stream_count = 0; 375 pipe->status = PIPE_UNDEFINED; 376 } 377 378 return err; 379 } 380 381 static int mixart_set_stream_state(struct mixart_stream *stream, int start) 382 { 383 struct snd_mixart *chip; 384 struct mixart_stream_state_req stream_state_req; 385 struct mixart_msg request; 386 387 if(!stream->substream) 388 return -EINVAL; 389 390 memset(&stream_state_req, 0, sizeof(stream_state_req)); 391 stream_state_req.stream_count = 1; 392 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid; 393 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number; 394 395 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 396 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET; 397 else 398 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET; 399 400 request.uid = (struct mixart_uid){0,0}; 401 request.data = &stream_state_req; 402 request.size = sizeof(stream_state_req); 403 404 stream->abs_period_elapsed = 0; /* reset stream pos */ 405 stream->buf_periods = 0; 406 stream->buf_period_frag = 0; 407 408 chip = snd_pcm_substream_chip(stream->substream); 409 410 return snd_mixart_send_msg_nonblock(chip->mgr, &request); 411 } 412 413 /* 414 * Trigger callback 415 */ 416 417 static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd) 418 { 419 struct mixart_stream *stream = subs->runtime->private_data; 420 421 switch (cmd) { 422 case SNDRV_PCM_TRIGGER_START: 423 424 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_TRIGGER_START\n"); 425 426 /* START_STREAM */ 427 if( mixart_set_stream_state(stream, 1) ) 428 return -EINVAL; 429 430 stream->status = MIXART_STREAM_STATUS_RUNNING; 431 432 break; 433 case SNDRV_PCM_TRIGGER_STOP: 434 435 /* STOP_STREAM */ 436 if( mixart_set_stream_state(stream, 0) ) 437 return -EINVAL; 438 439 stream->status = MIXART_STREAM_STATUS_OPEN; 440 441 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_TRIGGER_STOP\n"); 442 443 break; 444 445 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 446 /* TODO */ 447 stream->status = MIXART_STREAM_STATUS_PAUSE; 448 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_PAUSE_PUSH\n"); 449 break; 450 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 451 /* TODO */ 452 stream->status = MIXART_STREAM_STATUS_RUNNING; 453 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_PAUSE_RELEASE\n"); 454 break; 455 default: 456 return -EINVAL; 457 } 458 return 0; 459 } 460 461 static int mixart_sync_nonblock_events(struct mixart_mgr *mgr) 462 { 463 unsigned long timeout = jiffies + HZ; 464 while (atomic_read(&mgr->msg_processed) > 0) { 465 if (time_after(jiffies, timeout)) { 466 dev_err(&mgr->pci->dev, 467 "mixart: cannot process nonblock events!\n"); 468 return -EBUSY; 469 } 470 schedule_timeout_uninterruptible(1); 471 } 472 return 0; 473 } 474 475 /* 476 * prepare callback for all pcms 477 */ 478 static int snd_mixart_prepare(struct snd_pcm_substream *subs) 479 { 480 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 481 struct mixart_stream *stream = subs->runtime->private_data; 482 483 /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */ 484 485 dev_dbg(chip->card->dev, "snd_mixart_prepare\n"); 486 487 mixart_sync_nonblock_events(chip->mgr); 488 489 /* only the first stream can choose the sample rate */ 490 /* the further opened streams will be limited to its frequency (see open) */ 491 if(chip->mgr->ref_count_rate == 1) 492 chip->mgr->sample_rate = subs->runtime->rate; 493 494 /* set the clock only once (first stream) on the same pipe */ 495 if(stream->pipe->references == 1) { 496 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) ) 497 return -EINVAL; 498 } 499 500 return 0; 501 } 502 503 504 static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format) 505 { 506 int err; 507 struct snd_mixart *chip; 508 struct mixart_msg request; 509 struct mixart_stream_param_desc stream_param; 510 struct mixart_return_uid resp; 511 512 chip = snd_pcm_substream_chip(stream->substream); 513 514 memset(&stream_param, 0, sizeof(stream_param)); 515 516 stream_param.coding_type = CT_LINEAR; 517 stream_param.number_of_channel = stream->channels; 518 519 stream_param.sampling_freq = chip->mgr->sample_rate; 520 if(stream_param.sampling_freq == 0) 521 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */ 522 523 switch(format){ 524 case SNDRV_PCM_FORMAT_U8: 525 stream_param.sample_type = ST_INTEGER_8; 526 stream_param.sample_size = 8; 527 break; 528 case SNDRV_PCM_FORMAT_S16_LE: 529 stream_param.sample_type = ST_INTEGER_16LE; 530 stream_param.sample_size = 16; 531 break; 532 case SNDRV_PCM_FORMAT_S16_BE: 533 stream_param.sample_type = ST_INTEGER_16BE; 534 stream_param.sample_size = 16; 535 break; 536 case SNDRV_PCM_FORMAT_S24_3LE: 537 stream_param.sample_type = ST_INTEGER_24LE; 538 stream_param.sample_size = 24; 539 break; 540 case SNDRV_PCM_FORMAT_S24_3BE: 541 stream_param.sample_type = ST_INTEGER_24BE; 542 stream_param.sample_size = 24; 543 break; 544 case SNDRV_PCM_FORMAT_FLOAT_LE: 545 stream_param.sample_type = ST_FLOATING_POINT_32LE; 546 stream_param.sample_size = 32; 547 break; 548 case SNDRV_PCM_FORMAT_FLOAT_BE: 549 stream_param.sample_type = ST_FLOATING_POINT_32BE; 550 stream_param.sample_size = 32; 551 break; 552 default: 553 dev_err(chip->card->dev, 554 "error mixart_set_format() : unknown format\n"); 555 return -EINVAL; 556 } 557 558 dev_dbg(chip->card->dev, 559 "set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n", 560 stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels); 561 562 /* TODO: what else to configure ? */ 563 /* stream_param.samples_per_frame = 2; */ 564 /* stream_param.bytes_per_frame = 4; */ 565 /* stream_param.bytes_per_sample = 2; */ 566 567 stream_param.pipe_count = 1; /* set to 1 */ 568 stream_param.stream_count = 1; /* set to 1 */ 569 stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid; 570 stream_param.stream_desc[0].stream_idx = stream->substream->number; 571 572 request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM; 573 request.uid = (struct mixart_uid){0,0}; 574 request.data = &stream_param; 575 request.size = sizeof(stream_param); 576 577 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp); 578 if((err < 0) || resp.error_code) { 579 dev_err(chip->card->dev, 580 "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", 581 err, resp.error_code); 582 return -EINVAL; 583 } 584 return 0; 585 } 586 587 588 /* 589 * HW_PARAMS callback for all pcms 590 */ 591 static int snd_mixart_hw_params(struct snd_pcm_substream *subs, 592 struct snd_pcm_hw_params *hw) 593 { 594 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 595 struct mixart_mgr *mgr = chip->mgr; 596 struct mixart_stream *stream = subs->runtime->private_data; 597 snd_pcm_format_t format; 598 int err; 599 int channels; 600 601 /* set up channels */ 602 channels = params_channels(hw); 603 604 /* set up format for the stream */ 605 format = params_format(hw); 606 607 mutex_lock(&mgr->setup_mutex); 608 609 /* update the stream levels */ 610 if( stream->pcm_number <= MIXART_PCM_DIGITAL ) { 611 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG; 612 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK ) 613 mixart_update_playback_stream_level(chip, is_aes, subs->number); 614 else 615 mixart_update_capture_stream_level( chip, is_aes); 616 } 617 618 stream->channels = channels; 619 620 /* set the format to the board */ 621 err = mixart_set_format(stream, format); 622 if(err < 0) { 623 mutex_unlock(&mgr->setup_mutex); 624 return err; 625 } 626 627 if (subs->runtime->buffer_changed) { 628 struct mixart_bufferinfo *bufferinfo; 629 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number; 630 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) { 631 i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */ 632 } 633 634 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area; 635 bufferinfo[i].buffer_address = subs->runtime->dma_addr; 636 bufferinfo[i].available_length = subs->runtime->dma_bytes; 637 /* bufferinfo[i].buffer_id is already defined */ 638 639 dev_dbg(chip->card->dev, 640 "snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", 641 i, bufferinfo[i].buffer_address, 642 bufferinfo[i].available_length, 643 subs->number); 644 } 645 mutex_unlock(&mgr->setup_mutex); 646 647 return 0; 648 } 649 650 static int snd_mixart_hw_free(struct snd_pcm_substream *subs) 651 { 652 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 653 mixart_sync_nonblock_events(chip->mgr); 654 return 0; 655 } 656 657 658 659 /* 660 * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max 661 */ 662 static const struct snd_pcm_hardware snd_mixart_analog_caps = 663 { 664 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 665 SNDRV_PCM_INFO_MMAP_VALID | 666 SNDRV_PCM_INFO_PAUSE), 667 .formats = ( SNDRV_PCM_FMTBIT_U8 | 668 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | 669 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | 670 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ), 671 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, 672 .rate_min = 8000, 673 .rate_max = 48000, 674 .channels_min = 1, 675 .channels_max = 2, 676 .buffer_bytes_max = (32*1024), 677 .period_bytes_min = 256, /* 256 frames U8 mono*/ 678 .period_bytes_max = (16*1024), 679 .periods_min = 2, 680 .periods_max = (32*1024/256), 681 }; 682 683 static const struct snd_pcm_hardware snd_mixart_digital_caps = 684 { 685 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 686 SNDRV_PCM_INFO_MMAP_VALID | 687 SNDRV_PCM_INFO_PAUSE), 688 .formats = ( SNDRV_PCM_FMTBIT_U8 | 689 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | 690 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | 691 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ), 692 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 693 .rate_min = 32000, 694 .rate_max = 48000, 695 .channels_min = 1, 696 .channels_max = 2, 697 .buffer_bytes_max = (32*1024), 698 .period_bytes_min = 256, /* 256 frames U8 mono*/ 699 .period_bytes_max = (16*1024), 700 .periods_min = 2, 701 .periods_max = (32*1024/256), 702 }; 703 704 705 static int snd_mixart_playback_open(struct snd_pcm_substream *subs) 706 { 707 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 708 struct mixart_mgr *mgr = chip->mgr; 709 struct snd_pcm_runtime *runtime = subs->runtime; 710 struct snd_pcm *pcm = subs->pcm; 711 struct mixart_stream *stream; 712 struct mixart_pipe *pipe; 713 int err = 0; 714 int pcm_number; 715 716 mutex_lock(&mgr->setup_mutex); 717 718 if ( pcm == chip->pcm ) { 719 pcm_number = MIXART_PCM_ANALOG; 720 runtime->hw = snd_mixart_analog_caps; 721 } else { 722 snd_BUG_ON(pcm != chip->pcm_dig); 723 pcm_number = MIXART_PCM_DIGITAL; 724 runtime->hw = snd_mixart_digital_caps; 725 } 726 dev_dbg(chip->card->dev, 727 "snd_mixart_playback_open C%d/P%d/Sub%d\n", 728 chip->chip_idx, pcm_number, subs->number); 729 730 /* get stream info */ 731 stream = &(chip->playback_stream[pcm_number][subs->number]); 732 733 if (stream->status != MIXART_STREAM_STATUS_FREE){ 734 /* streams in use */ 735 dev_err(chip->card->dev, 736 "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", 737 chip->chip_idx, pcm_number, subs->number); 738 err = -EBUSY; 739 goto _exit_open; 740 } 741 742 /* get pipe pointer (out pipe) */ 743 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0); 744 745 if (pipe == NULL) { 746 err = -EINVAL; 747 goto _exit_open; 748 } 749 750 /* start the pipe if necessary */ 751 err = mixart_set_pipe_state(chip->mgr, pipe, 1); 752 if( err < 0 ) { 753 dev_err(chip->card->dev, "error starting pipe!\n"); 754 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0); 755 err = -EINVAL; 756 goto _exit_open; 757 } 758 759 stream->pipe = pipe; 760 stream->pcm_number = pcm_number; 761 stream->status = MIXART_STREAM_STATUS_OPEN; 762 stream->substream = subs; 763 stream->channels = 0; /* not configured yet */ 764 765 runtime->private_data = stream; 766 767 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); 768 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64); 769 770 /* if a sample rate is already used, another stream cannot change */ 771 if(mgr->ref_count_rate++) { 772 if(mgr->sample_rate) { 773 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate; 774 } 775 } 776 777 _exit_open: 778 mutex_unlock(&mgr->setup_mutex); 779 780 return err; 781 } 782 783 784 static int snd_mixart_capture_open(struct snd_pcm_substream *subs) 785 { 786 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 787 struct mixart_mgr *mgr = chip->mgr; 788 struct snd_pcm_runtime *runtime = subs->runtime; 789 struct snd_pcm *pcm = subs->pcm; 790 struct mixart_stream *stream; 791 struct mixart_pipe *pipe; 792 int err = 0; 793 int pcm_number; 794 795 mutex_lock(&mgr->setup_mutex); 796 797 if ( pcm == chip->pcm ) { 798 pcm_number = MIXART_PCM_ANALOG; 799 runtime->hw = snd_mixart_analog_caps; 800 } else { 801 snd_BUG_ON(pcm != chip->pcm_dig); 802 pcm_number = MIXART_PCM_DIGITAL; 803 runtime->hw = snd_mixart_digital_caps; 804 } 805 806 runtime->hw.channels_min = 2; /* for instance, no mono */ 807 808 dev_dbg(chip->card->dev, "snd_mixart_capture_open C%d/P%d/Sub%d\n", 809 chip->chip_idx, pcm_number, subs->number); 810 811 /* get stream info */ 812 stream = &(chip->capture_stream[pcm_number]); 813 814 if (stream->status != MIXART_STREAM_STATUS_FREE){ 815 /* streams in use */ 816 dev_err(chip->card->dev, 817 "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", 818 chip->chip_idx, pcm_number, subs->number); 819 err = -EBUSY; 820 goto _exit_open; 821 } 822 823 /* get pipe pointer (in pipe) */ 824 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0); 825 826 if (pipe == NULL) { 827 err = -EINVAL; 828 goto _exit_open; 829 } 830 831 /* start the pipe if necessary */ 832 err = mixart_set_pipe_state(chip->mgr, pipe, 1); 833 if( err < 0 ) { 834 dev_err(chip->card->dev, "error starting pipe!\n"); 835 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0); 836 err = -EINVAL; 837 goto _exit_open; 838 } 839 840 stream->pipe = pipe; 841 stream->pcm_number = pcm_number; 842 stream->status = MIXART_STREAM_STATUS_OPEN; 843 stream->substream = subs; 844 stream->channels = 0; /* not configured yet */ 845 846 runtime->private_data = stream; 847 848 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); 849 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64); 850 851 /* if a sample rate is already used, another stream cannot change */ 852 if(mgr->ref_count_rate++) { 853 if(mgr->sample_rate) { 854 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate; 855 } 856 } 857 858 _exit_open: 859 mutex_unlock(&mgr->setup_mutex); 860 861 return err; 862 } 863 864 865 866 static int snd_mixart_close(struct snd_pcm_substream *subs) 867 { 868 struct snd_mixart *chip = snd_pcm_substream_chip(subs); 869 struct mixart_mgr *mgr = chip->mgr; 870 struct mixart_stream *stream = subs->runtime->private_data; 871 872 mutex_lock(&mgr->setup_mutex); 873 874 dev_dbg(chip->card->dev, "snd_mixart_close C%d/P%d/Sub%d\n", 875 chip->chip_idx, stream->pcm_number, subs->number); 876 877 /* sample rate released */ 878 if(--mgr->ref_count_rate == 0) { 879 mgr->sample_rate = 0; 880 } 881 882 /* delete pipe */ 883 if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) { 884 885 dev_err(chip->card->dev, 886 "error snd_mixart_kill_ref_pipe C%dP%d\n", 887 chip->chip_idx, stream->pcm_number); 888 } 889 890 stream->pipe = NULL; 891 stream->status = MIXART_STREAM_STATUS_FREE; 892 stream->substream = NULL; 893 894 mutex_unlock(&mgr->setup_mutex); 895 return 0; 896 } 897 898 899 static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs) 900 { 901 struct snd_pcm_runtime *runtime = subs->runtime; 902 struct mixart_stream *stream = runtime->private_data; 903 904 return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag); 905 } 906 907 908 909 static const struct snd_pcm_ops snd_mixart_playback_ops = { 910 .open = snd_mixart_playback_open, 911 .close = snd_mixart_close, 912 .ioctl = snd_pcm_lib_ioctl, 913 .prepare = snd_mixart_prepare, 914 .hw_params = snd_mixart_hw_params, 915 .hw_free = snd_mixart_hw_free, 916 .trigger = snd_mixart_trigger, 917 .pointer = snd_mixart_stream_pointer, 918 }; 919 920 static const struct snd_pcm_ops snd_mixart_capture_ops = { 921 .open = snd_mixart_capture_open, 922 .close = snd_mixart_close, 923 .ioctl = snd_pcm_lib_ioctl, 924 .prepare = snd_mixart_prepare, 925 .hw_params = snd_mixart_hw_params, 926 .hw_free = snd_mixart_hw_free, 927 .trigger = snd_mixart_trigger, 928 .pointer = snd_mixart_stream_pointer, 929 }; 930 931 static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm) 932 { 933 #if 0 934 struct snd_pcm_substream *subs; 935 int stream; 936 937 for (stream = 0; stream < 2; stream++) { 938 int idx = 0; 939 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++) 940 /* set up the unique device id with the chip index */ 941 subs->dma_device.id = subs->pcm->device << 16 | 942 subs->stream << 8 | (subs->number + 1) | 943 (chip->chip_idx + 1) << 24; 944 } 945 #endif 946 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, 947 &chip->mgr->pci->dev, 948 32*1024, 32*1024); 949 } 950 951 /* 952 */ 953 static int snd_mixart_pcm_analog(struct snd_mixart *chip) 954 { 955 int err; 956 struct snd_pcm *pcm; 957 char name[32]; 958 959 sprintf(name, "miXart analog %d", chip->chip_idx); 960 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG, 961 MIXART_PLAYBACK_STREAMS, 962 MIXART_CAPTURE_STREAMS, &pcm)) < 0) { 963 dev_err(chip->card->dev, 964 "cannot create the analog pcm %d\n", chip->chip_idx); 965 return err; 966 } 967 968 pcm->private_data = chip; 969 970 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops); 971 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops); 972 973 pcm->info_flags = 0; 974 pcm->nonatomic = true; 975 strcpy(pcm->name, name); 976 977 preallocate_buffers(chip, pcm); 978 979 chip->pcm = pcm; 980 return 0; 981 } 982 983 984 /* 985 */ 986 static int snd_mixart_pcm_digital(struct snd_mixart *chip) 987 { 988 int err; 989 struct snd_pcm *pcm; 990 char name[32]; 991 992 sprintf(name, "miXart AES/EBU %d", chip->chip_idx); 993 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL, 994 MIXART_PLAYBACK_STREAMS, 995 MIXART_CAPTURE_STREAMS, &pcm)) < 0) { 996 dev_err(chip->card->dev, 997 "cannot create the digital pcm %d\n", chip->chip_idx); 998 return err; 999 } 1000 1001 pcm->private_data = chip; 1002 1003 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops); 1004 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops); 1005 1006 pcm->info_flags = 0; 1007 pcm->nonatomic = true; 1008 strcpy(pcm->name, name); 1009 1010 preallocate_buffers(chip, pcm); 1011 1012 chip->pcm_dig = pcm; 1013 return 0; 1014 } 1015 1016 static int snd_mixart_chip_free(struct snd_mixart *chip) 1017 { 1018 kfree(chip); 1019 return 0; 1020 } 1021 1022 static int snd_mixart_chip_dev_free(struct snd_device *device) 1023 { 1024 struct snd_mixart *chip = device->device_data; 1025 return snd_mixart_chip_free(chip); 1026 } 1027 1028 1029 /* 1030 */ 1031 static int snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx) 1032 { 1033 int err; 1034 struct snd_mixart *chip; 1035 static struct snd_device_ops ops = { 1036 .dev_free = snd_mixart_chip_dev_free, 1037 }; 1038 1039 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1040 if (!chip) 1041 return -ENOMEM; 1042 1043 chip->card = card; 1044 chip->chip_idx = idx; 1045 chip->mgr = mgr; 1046 1047 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 1048 snd_mixart_chip_free(chip); 1049 return err; 1050 } 1051 1052 mgr->chip[idx] = chip; 1053 return 0; 1054 } 1055 1056 int snd_mixart_create_pcm(struct snd_mixart* chip) 1057 { 1058 int err; 1059 1060 err = snd_mixart_pcm_analog(chip); 1061 if (err < 0) 1062 return err; 1063 1064 if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) { 1065 1066 err = snd_mixart_pcm_digital(chip); 1067 if (err < 0) 1068 return err; 1069 } 1070 return err; 1071 } 1072 1073 1074 /* 1075 * release all the cards assigned to a manager instance 1076 */ 1077 static int snd_mixart_free(struct mixart_mgr *mgr) 1078 { 1079 unsigned int i; 1080 1081 for (i = 0; i < mgr->num_cards; i++) { 1082 if (mgr->chip[i]) 1083 snd_card_free(mgr->chip[i]->card); 1084 } 1085 1086 /* stop mailbox */ 1087 snd_mixart_exit_mailbox(mgr); 1088 1089 /* release irq */ 1090 if (mgr->irq >= 0) 1091 free_irq(mgr->irq, mgr); 1092 1093 /* reset board if some firmware was loaded */ 1094 if(mgr->dsp_loaded) { 1095 snd_mixart_reset_board(mgr); 1096 dev_dbg(&mgr->pci->dev, "reset miXart !\n"); 1097 } 1098 1099 /* release the i/o ports */ 1100 for (i = 0; i < 2; ++i) 1101 iounmap(mgr->mem[i].virt); 1102 1103 pci_release_regions(mgr->pci); 1104 1105 /* free flowarray */ 1106 if(mgr->flowinfo.area) { 1107 snd_dma_free_pages(&mgr->flowinfo); 1108 mgr->flowinfo.area = NULL; 1109 } 1110 /* free bufferarray */ 1111 if(mgr->bufferinfo.area) { 1112 snd_dma_free_pages(&mgr->bufferinfo); 1113 mgr->bufferinfo.area = NULL; 1114 } 1115 1116 pci_disable_device(mgr->pci); 1117 kfree(mgr); 1118 return 0; 1119 } 1120 1121 /* 1122 * proc interface 1123 */ 1124 1125 /* 1126 mixart_BA0 proc interface for BAR 0 - read callback 1127 */ 1128 static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry, 1129 void *file_private_data, 1130 struct file *file, char __user *buf, 1131 size_t count, loff_t pos) 1132 { 1133 struct mixart_mgr *mgr = entry->private_data; 1134 1135 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */ 1136 if (copy_to_user_fromio(buf, MIXART_MEM(mgr, pos), count)) 1137 return -EFAULT; 1138 return count; 1139 } 1140 1141 /* 1142 mixart_BA1 proc interface for BAR 1 - read callback 1143 */ 1144 static ssize_t snd_mixart_BA1_read(struct snd_info_entry *entry, 1145 void *file_private_data, 1146 struct file *file, char __user *buf, 1147 size_t count, loff_t pos) 1148 { 1149 struct mixart_mgr *mgr = entry->private_data; 1150 1151 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */ 1152 if (copy_to_user_fromio(buf, MIXART_REG(mgr, pos), count)) 1153 return -EFAULT; 1154 return count; 1155 } 1156 1157 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = { 1158 .read = snd_mixart_BA0_read, 1159 }; 1160 1161 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = { 1162 .read = snd_mixart_BA1_read, 1163 }; 1164 1165 1166 static void snd_mixart_proc_read(struct snd_info_entry *entry, 1167 struct snd_info_buffer *buffer) 1168 { 1169 struct snd_mixart *chip = entry->private_data; 1170 u32 ref; 1171 1172 snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx); 1173 1174 /* stats available when embedded OS is running */ 1175 if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) { 1176 snd_iprintf(buffer, "- hardware -\n"); 1177 switch (chip->mgr->board_type ) { 1178 case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break; 1179 case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break; 1180 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break; 1181 default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break; 1182 } 1183 1184 snd_iprintf(buffer, "- system load -\n"); 1185 1186 /* get perf reference */ 1187 1188 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET)); 1189 1190 if (ref) { 1191 u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref; 1192 u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref; 1193 u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref; 1194 1195 snd_iprintf(buffer, "\tstreaming : %d\n", streaming); 1196 snd_iprintf(buffer, "\tmailbox : %d\n", mailbox); 1197 snd_iprintf(buffer, "\tinterrupts handling : %d\n\n", interr); 1198 } 1199 } /* endif elf loaded */ 1200 } 1201 1202 static void snd_mixart_proc_init(struct snd_mixart *chip) 1203 { 1204 struct snd_info_entry *entry; 1205 1206 /* text interface to read perf and temp meters */ 1207 snd_card_ro_proc_new(chip->card, "board_info", chip, 1208 snd_mixart_proc_read); 1209 1210 if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) { 1211 entry->content = SNDRV_INFO_CONTENT_DATA; 1212 entry->private_data = chip->mgr; 1213 entry->c.ops = &snd_mixart_proc_ops_BA0; 1214 entry->size = MIXART_BA0_SIZE; 1215 } 1216 if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) { 1217 entry->content = SNDRV_INFO_CONTENT_DATA; 1218 entry->private_data = chip->mgr; 1219 entry->c.ops = &snd_mixart_proc_ops_BA1; 1220 entry->size = MIXART_BA1_SIZE; 1221 } 1222 } 1223 /* end of proc interface */ 1224 1225 1226 /* 1227 * probe function - creates the card manager 1228 */ 1229 static int snd_mixart_probe(struct pci_dev *pci, 1230 const struct pci_device_id *pci_id) 1231 { 1232 static int dev; 1233 struct mixart_mgr *mgr; 1234 unsigned int i; 1235 int err; 1236 size_t size; 1237 1238 /* 1239 */ 1240 if (dev >= SNDRV_CARDS) 1241 return -ENODEV; 1242 if (! enable[dev]) { 1243 dev++; 1244 return -ENOENT; 1245 } 1246 1247 /* enable PCI device */ 1248 if ((err = pci_enable_device(pci)) < 0) 1249 return err; 1250 pci_set_master(pci); 1251 1252 /* check if we can restrict PCI DMA transfers to 32 bits */ 1253 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) { 1254 dev_err(&pci->dev, 1255 "architecture does not support 32bit PCI busmaster DMA\n"); 1256 pci_disable_device(pci); 1257 return -ENXIO; 1258 } 1259 1260 /* 1261 */ 1262 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); 1263 if (! mgr) { 1264 pci_disable_device(pci); 1265 return -ENOMEM; 1266 } 1267 1268 mgr->pci = pci; 1269 mgr->irq = -1; 1270 1271 /* resource assignment */ 1272 if ((err = pci_request_regions(pci, CARD_NAME)) < 0) { 1273 kfree(mgr); 1274 pci_disable_device(pci); 1275 return err; 1276 } 1277 for (i = 0; i < 2; i++) { 1278 mgr->mem[i].phys = pci_resource_start(pci, i); 1279 mgr->mem[i].virt = pci_ioremap_bar(pci, i); 1280 if (!mgr->mem[i].virt) { 1281 dev_err(&pci->dev, "unable to remap resource 0x%lx\n", 1282 mgr->mem[i].phys); 1283 snd_mixart_free(mgr); 1284 return -EBUSY; 1285 } 1286 } 1287 1288 if (request_threaded_irq(pci->irq, snd_mixart_interrupt, 1289 snd_mixart_threaded_irq, IRQF_SHARED, 1290 KBUILD_MODNAME, mgr)) { 1291 dev_err(&pci->dev, "unable to grab IRQ %d\n", pci->irq); 1292 snd_mixart_free(mgr); 1293 return -EBUSY; 1294 } 1295 mgr->irq = pci->irq; 1296 1297 /* init mailbox */ 1298 mgr->msg_fifo_readptr = 0; 1299 mgr->msg_fifo_writeptr = 0; 1300 1301 mutex_init(&mgr->lock); 1302 mutex_init(&mgr->msg_lock); 1303 init_waitqueue_head(&mgr->msg_sleep); 1304 atomic_set(&mgr->msg_processed, 0); 1305 1306 /* init setup mutex*/ 1307 mutex_init(&mgr->setup_mutex); 1308 1309 /* card assignment */ 1310 mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */ 1311 for (i = 0; i < mgr->num_cards; i++) { 1312 struct snd_card *card; 1313 char tmpid[16]; 1314 int idx; 1315 1316 if (index[dev] < 0) 1317 idx = index[dev]; 1318 else 1319 idx = index[dev] + i; 1320 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i); 1321 err = snd_card_new(&pci->dev, idx, tmpid, THIS_MODULE, 1322 0, &card); 1323 1324 if (err < 0) { 1325 dev_err(&pci->dev, "cannot allocate the card %d\n", i); 1326 snd_mixart_free(mgr); 1327 return err; 1328 } 1329 1330 strcpy(card->driver, CARD_NAME); 1331 snprintf(card->shortname, sizeof(card->shortname), 1332 "Digigram miXart [PCM #%d]", i); 1333 snprintf(card->longname, sizeof(card->longname), 1334 "Digigram miXart at 0x%lx & 0x%lx, irq %i [PCM #%d]", 1335 mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq, i); 1336 1337 if ((err = snd_mixart_create(mgr, card, i)) < 0) { 1338 snd_card_free(card); 1339 snd_mixart_free(mgr); 1340 return err; 1341 } 1342 1343 if(i==0) { 1344 /* init proc interface only for chip0 */ 1345 snd_mixart_proc_init(mgr->chip[i]); 1346 } 1347 1348 if ((err = snd_card_register(card)) < 0) { 1349 snd_mixart_free(mgr); 1350 return err; 1351 } 1352 } 1353 1354 /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */ 1355 mgr->board_type = MIXART_DAUGHTER_TYPE_NONE; 1356 1357 /* create array of streaminfo */ 1358 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * 1359 sizeof(struct mixart_flowinfo)) ); 1360 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev, 1361 size, &mgr->flowinfo) < 0) { 1362 snd_mixart_free(mgr); 1363 return -ENOMEM; 1364 } 1365 /* init streaminfo_array */ 1366 memset(mgr->flowinfo.area, 0, size); 1367 1368 /* create array of bufferinfo */ 1369 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * 1370 sizeof(struct mixart_bufferinfo)) ); 1371 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev, 1372 size, &mgr->bufferinfo) < 0) { 1373 snd_mixart_free(mgr); 1374 return -ENOMEM; 1375 } 1376 /* init bufferinfo_array */ 1377 memset(mgr->bufferinfo.area, 0, size); 1378 1379 /* set up firmware */ 1380 err = snd_mixart_setup_firmware(mgr); 1381 if (err < 0) { 1382 snd_mixart_free(mgr); 1383 return err; 1384 } 1385 1386 pci_set_drvdata(pci, mgr); 1387 dev++; 1388 return 0; 1389 } 1390 1391 static void snd_mixart_remove(struct pci_dev *pci) 1392 { 1393 snd_mixart_free(pci_get_drvdata(pci)); 1394 } 1395 1396 static struct pci_driver mixart_driver = { 1397 .name = KBUILD_MODNAME, 1398 .id_table = snd_mixart_ids, 1399 .probe = snd_mixart_probe, 1400 .remove = snd_mixart_remove, 1401 }; 1402 1403 module_pci_driver(mixart_driver); 1404