1/* 2 btaudio - bt878 audio dma driver for linux 2.4.x 3 4 (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org> 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 20*/ 21 22#include <linux/module.h> 23#include <linux/errno.h> 24#include <linux/pci.h> 25#include <linux/sched.h> 26#include <linux/signal.h> 27#include <linux/types.h> 28#include <linux/interrupt.h> 29#include <linux/init.h> 30#include <linux/poll.h> 31#include <linux/sound.h> 32#include <linux/soundcard.h> 33#include <linux/slab.h> 34#include <linux/kdev_t.h> 35#include <linux/mutex.h> 36 37#include <asm/uaccess.h> 38#include <asm/io.h> 39 40 41/* mmio access */ 42#define btwrite(dat,adr) writel((dat), (bta->mmio+(adr))) 43#define btread(adr) readl(bta->mmio+(adr)) 44 45#define btand(dat,adr) btwrite((dat) & btread(adr), adr) 46#define btor(dat,adr) btwrite((dat) | btread(adr), adr) 47#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr) 48 49/* registers (shifted because bta->mmio is long) */ 50#define REG_INT_STAT (0x100 >> 2) 51#define REG_INT_MASK (0x104 >> 2) 52#define REG_GPIO_DMA_CTL (0x10c >> 2) 53#define REG_PACKET_LEN (0x110 >> 2) 54#define REG_RISC_STRT_ADD (0x114 >> 2) 55#define REG_RISC_COUNT (0x120 >> 2) 56 57/* IRQ bits - REG_INT_(STAT|MASK) */ 58#define IRQ_SCERR (1 << 19) 59#define IRQ_OCERR (1 << 18) 60#define IRQ_PABORT (1 << 17) 61#define IRQ_RIPERR (1 << 16) 62#define IRQ_PPERR (1 << 15) 63#define IRQ_FDSR (1 << 14) 64#define IRQ_FTRGT (1 << 13) 65#define IRQ_FBUS (1 << 12) 66#define IRQ_RISCI (1 << 11) 67#define IRQ_OFLOW (1 << 3) 68 69#define IRQ_BTAUDIO (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\ 70 IRQ_PPERR | IRQ_FDSR | IRQ_FTRGT | IRQ_FBUS |\ 71 IRQ_RISCI) 72 73/* REG_GPIO_DMA_CTL bits */ 74#define DMA_CTL_A_PWRDN (1 << 26) 75#define DMA_CTL_DA_SBR (1 << 14) 76#define DMA_CTL_DA_ES2 (1 << 13) 77#define DMA_CTL_ACAP_EN (1 << 4) 78#define DMA_CTL_RISC_EN (1 << 1) 79#define DMA_CTL_FIFO_EN (1 << 0) 80 81/* RISC instructions */ 82#define RISC_WRITE (0x01 << 28) 83#define RISC_JUMP (0x07 << 28) 84#define RISC_SYNC (0x08 << 28) 85 86/* RISC bits */ 87#define RISC_WR_SOL (1 << 27) 88#define RISC_WR_EOL (1 << 26) 89#define RISC_IRQ (1 << 24) 90#define RISC_SYNC_RESYNC (1 << 15) 91#define RISC_SYNC_FM1 0x06 92#define RISC_SYNC_VRO 0x0c 93 94#define HWBASE_AD (448000) 95 96/* -------------------------------------------------------------- */ 97 98struct btaudio { 99 /* linked list */ 100 struct btaudio *next; 101 102 /* device info */ 103 int dsp_digital; 104 int dsp_analog; 105 int mixer_dev; 106 struct pci_dev *pci; 107 unsigned int irq; 108 unsigned long mem; 109 unsigned long __iomem *mmio; 110 111 /* locking */ 112 int users; 113 struct mutex lock; 114 115 /* risc instructions */ 116 unsigned int risc_size; 117 unsigned long *risc_cpu; 118 dma_addr_t risc_dma; 119 120 /* audio data */ 121 unsigned int buf_size; 122 unsigned char *buf_cpu; 123 dma_addr_t buf_dma; 124 125 /* buffer setup */ 126 int line_bytes; 127 int line_count; 128 int block_bytes; 129 int block_count; 130 131 /* read fifo management */ 132 int recording; 133 int dma_block; 134 int read_offset; 135 int read_count; 136 wait_queue_head_t readq; 137 138 /* settings */ 139 int gain[3]; 140 int source; 141 int bits; 142 int decimation; 143 int mixcount; 144 int sampleshift; 145 int channels; 146 int analog; 147 int rate; 148}; 149 150struct cardinfo { 151 char *name; 152 int rate; 153}; 154 155static struct btaudio *btaudios; 156static unsigned int debug; 157static unsigned int irq_debug; 158 159/* -------------------------------------------------------------- */ 160 161#define BUF_DEFAULT 128*1024 162#define BUF_MIN 8192 163 164static int alloc_buffer(struct btaudio *bta) 165{ 166 if (NULL == bta->buf_cpu) { 167 for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN; 168 bta->buf_size = bta->buf_size >> 1) { 169 bta->buf_cpu = pci_alloc_consistent 170 (bta->pci, bta->buf_size, &bta->buf_dma); 171 if (NULL != bta->buf_cpu) 172 break; 173 } 174 if (NULL == bta->buf_cpu) 175 return -ENOMEM; 176 memset(bta->buf_cpu,0,bta->buf_size); 177 } 178 if (NULL == bta->risc_cpu) { 179 bta->risc_size = PAGE_SIZE; 180 bta->risc_cpu = pci_alloc_consistent 181 (bta->pci, bta->risc_size, &bta->risc_dma); 182 if (NULL == bta->risc_cpu) { 183 pci_free_consistent(bta->pci, bta->buf_size, bta->buf_cpu, bta->buf_dma); 184 bta->buf_cpu = NULL; 185 return -ENOMEM; 186 } 187 } 188 return 0; 189} 190 191static void free_buffer(struct btaudio *bta) 192{ 193 if (NULL != bta->buf_cpu) { 194 pci_free_consistent(bta->pci, bta->buf_size, 195 bta->buf_cpu, bta->buf_dma); 196 bta->buf_cpu = NULL; 197 } 198 if (NULL != bta->risc_cpu) { 199 pci_free_consistent(bta->pci, bta->risc_size, 200 bta->risc_cpu, bta->risc_dma); 201 bta->risc_cpu = NULL; 202 } 203} 204 205static int make_risc(struct btaudio *bta) 206{ 207 int rp, bp, line, block; 208 unsigned long risc; 209 210 bta->block_bytes = bta->buf_size >> 4; 211 bta->block_count = 1 << 4; 212 bta->line_bytes = bta->block_bytes; 213 bta->line_count = bta->block_count; 214 while (bta->line_bytes > 4095) { 215 bta->line_bytes >>= 1; 216 bta->line_count <<= 1; 217 } 218 if (bta->line_count > 255) 219 return -EINVAL; 220 if (debug) 221 printk(KERN_DEBUG 222 "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n", 223 bta->buf_size,bta->block_bytes,bta->block_count, 224 bta->line_bytes,bta->line_count); 225 rp = 0; bp = 0; 226 block = 0; 227 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1); 228 bta->risc_cpu[rp++] = cpu_to_le32(0); 229 for (line = 0; line < bta->line_count; line++) { 230 risc = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL; 231 risc |= bta->line_bytes; 232 if (0 == (bp & (bta->block_bytes-1))) { 233 risc |= RISC_IRQ; 234 risc |= (block & 0x0f) << 16; 235 risc |= (~block & 0x0f) << 20; 236 block++; 237 } 238 bta->risc_cpu[rp++] = cpu_to_le32(risc); 239 bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp); 240 bp += bta->line_bytes; 241 } 242 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO); 243 bta->risc_cpu[rp++] = cpu_to_le32(0); 244 bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP); 245 bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma); 246 return 0; 247} 248 249static int start_recording(struct btaudio *bta) 250{ 251 int ret; 252 253 if (0 != (ret = alloc_buffer(bta))) 254 return ret; 255 if (0 != (ret = make_risc(bta))) 256 return ret; 257 258 btwrite(bta->risc_dma, REG_RISC_STRT_ADD); 259 btwrite((bta->line_count << 16) | bta->line_bytes, 260 REG_PACKET_LEN); 261 btwrite(IRQ_BTAUDIO, REG_INT_MASK); 262 if (bta->analog) { 263 btwrite(DMA_CTL_ACAP_EN | 264 DMA_CTL_RISC_EN | 265 DMA_CTL_FIFO_EN | 266 DMA_CTL_DA_ES2 | 267 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) | 268 (bta->gain[bta->source] << 28) | 269 (bta->source << 24) | 270 (bta->decimation << 8), 271 REG_GPIO_DMA_CTL); 272 } else { 273 btwrite(DMA_CTL_ACAP_EN | 274 DMA_CTL_RISC_EN | 275 DMA_CTL_FIFO_EN | 276 DMA_CTL_DA_ES2 | 277 DMA_CTL_A_PWRDN | 278 (1 << 6) | 279 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) | 280 (bta->gain[bta->source] << 28) | 281 (bta->source << 24) | 282 (bta->decimation << 8), 283 REG_GPIO_DMA_CTL); 284 } 285 bta->dma_block = 0; 286 bta->read_offset = 0; 287 bta->read_count = 0; 288 bta->recording = 1; 289 if (debug) 290 printk(KERN_DEBUG "btaudio: recording started\n"); 291 return 0; 292} 293 294static void stop_recording(struct btaudio *bta) 295{ 296 btand(~15, REG_GPIO_DMA_CTL); 297 bta->recording = 0; 298 if (debug) 299 printk(KERN_DEBUG "btaudio: recording stopped\n"); 300} 301 302 303/* -------------------------------------------------------------- */ 304 305static int btaudio_mixer_open(struct inode *inode, struct file *file) 306{ 307 int minor = iminor(inode); 308 struct btaudio *bta; 309 310 for (bta = btaudios; bta != NULL; bta = bta->next) 311 if (bta->mixer_dev == minor) 312 break; 313 if (NULL == bta) 314 return -ENODEV; 315 316 if (debug) 317 printk("btaudio: open mixer [%d]\n",minor); 318 file->private_data = bta; 319 return 0; 320} 321 322static int btaudio_mixer_release(struct inode *inode, struct file *file) 323{ 324 return 0; 325} 326 327static int btaudio_mixer_ioctl(struct inode *inode, struct file *file, 328 unsigned int cmd, unsigned long arg) 329{ 330 struct btaudio *bta = file->private_data; 331 int ret,val=0,i=0; 332 void __user *argp = (void __user *)arg; 333 334 if (cmd == SOUND_MIXER_INFO) { 335 mixer_info info; 336 memset(&info,0,sizeof(info)); 337 strlcpy(info.id,"bt878",sizeof(info.id)); 338 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name)); 339 info.modify_counter = bta->mixcount; 340 if (copy_to_user(argp, &info, sizeof(info))) 341 return -EFAULT; 342 return 0; 343 } 344 if (cmd == SOUND_OLD_MIXER_INFO) { 345 _old_mixer_info info; 346 memset(&info,0,sizeof(info)); 347 strlcpy(info.id, "bt878", sizeof(info.id)); 348 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name)); 349 if (copy_to_user(argp, &info, sizeof(info))) 350 return -EFAULT; 351 return 0; 352 } 353 if (cmd == OSS_GETVERSION) 354 return put_user(SOUND_VERSION, (int __user *)argp); 355 356 /* read */ 357 if (_SIOC_DIR(cmd) & _SIOC_WRITE) 358 if (get_user(val, (int __user *)argp)) 359 return -EFAULT; 360 361 switch (cmd) { 362 case MIXER_READ(SOUND_MIXER_CAPS): 363 ret = SOUND_CAP_EXCL_INPUT; 364 break; 365 case MIXER_READ(SOUND_MIXER_STEREODEVS): 366 ret = 0; 367 break; 368 case MIXER_READ(SOUND_MIXER_RECMASK): 369 case MIXER_READ(SOUND_MIXER_DEVMASK): 370 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3; 371 break; 372 373 case MIXER_WRITE(SOUND_MIXER_RECSRC): 374 if (val & SOUND_MASK_LINE1 && bta->source != 0) 375 bta->source = 0; 376 else if (val & SOUND_MASK_LINE2 && bta->source != 1) 377 bta->source = 1; 378 else if (val & SOUND_MASK_LINE3 && bta->source != 2) 379 bta->source = 2; 380 btaor((bta->gain[bta->source] << 28) | 381 (bta->source << 24), 382 0x0cffffff, REG_GPIO_DMA_CTL); 383 case MIXER_READ(SOUND_MIXER_RECSRC): 384 switch (bta->source) { 385 case 0: ret = SOUND_MASK_LINE1; break; 386 case 1: ret = SOUND_MASK_LINE2; break; 387 case 2: ret = SOUND_MASK_LINE3; break; 388 default: ret = 0; 389 } 390 break; 391 392 case MIXER_WRITE(SOUND_MIXER_LINE1): 393 case MIXER_WRITE(SOUND_MIXER_LINE2): 394 case MIXER_WRITE(SOUND_MIXER_LINE3): 395 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd) 396 i = 0; 397 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd) 398 i = 1; 399 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd) 400 i = 2; 401 bta->gain[i] = (val & 0xff) * 15 / 100; 402 if (bta->gain[i] > 15) bta->gain[i] = 15; 403 if (bta->gain[i] < 0) bta->gain[i] = 0; 404 if (i == bta->source) 405 btaor((bta->gain[bta->source]<<28), 406 0x0fffffff, REG_GPIO_DMA_CTL); 407 ret = bta->gain[i] * 100 / 15; 408 ret |= ret << 8; 409 break; 410 411 case MIXER_READ(SOUND_MIXER_LINE1): 412 case MIXER_READ(SOUND_MIXER_LINE2): 413 case MIXER_READ(SOUND_MIXER_LINE3): 414 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd) 415 i = 0; 416 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd) 417 i = 1; 418 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd) 419 i = 2; 420 ret = bta->gain[i] * 100 / 15; 421 ret |= ret << 8; 422 break; 423 424 default: 425 return -EINVAL; 426 } 427 if (put_user(ret, (int __user *)argp)) 428 return -EFAULT; 429 return 0; 430} 431 432static const struct file_operations btaudio_mixer_fops = { 433 .owner = THIS_MODULE, 434 .llseek = no_llseek, 435 .open = btaudio_mixer_open, 436 .release = btaudio_mixer_release, 437 .ioctl = btaudio_mixer_ioctl, 438}; 439 440/* -------------------------------------------------------------- */ 441 442static int btaudio_dsp_open(struct inode *inode, struct file *file, 443 struct btaudio *bta, int analog) 444{ 445 mutex_lock(&bta->lock); 446 if (bta->users) 447 goto busy; 448 bta->users++; 449 file->private_data = bta; 450 451 bta->analog = analog; 452 bta->dma_block = 0; 453 bta->read_offset = 0; 454 bta->read_count = 0; 455 bta->sampleshift = 0; 456 457 mutex_unlock(&bta->lock); 458 return 0; 459 460 busy: 461 mutex_unlock(&bta->lock); 462 return -EBUSY; 463} 464 465static int btaudio_dsp_open_digital(struct inode *inode, struct file *file) 466{ 467 int minor = iminor(inode); 468 struct btaudio *bta; 469 470 for (bta = btaudios; bta != NULL; bta = bta->next) 471 if (bta->dsp_digital == minor) 472 break; 473 if (NULL == bta) 474 return -ENODEV; 475 476 if (debug) 477 printk("btaudio: open digital dsp [%d]\n",minor); 478 return btaudio_dsp_open(inode,file,bta,0); 479} 480 481static int btaudio_dsp_open_analog(struct inode *inode, struct file *file) 482{ 483 int minor = iminor(inode); 484 struct btaudio *bta; 485 486 for (bta = btaudios; bta != NULL; bta = bta->next) 487 if (bta->dsp_analog == minor) 488 break; 489 if (NULL == bta) 490 return -ENODEV; 491 492 if (debug) 493 printk("btaudio: open analog dsp [%d]\n",minor); 494 return btaudio_dsp_open(inode,file,bta,1); 495} 496 497static int btaudio_dsp_release(struct inode *inode, struct file *file) 498{ 499 struct btaudio *bta = file->private_data; 500 501 mutex_lock(&bta->lock); 502 if (bta->recording) 503 stop_recording(bta); 504 bta->users--; 505 mutex_unlock(&bta->lock); 506 return 0; 507} 508 509static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer, 510 size_t swcount, loff_t *ppos) 511{ 512 struct btaudio *bta = file->private_data; 513 int hwcount = swcount << bta->sampleshift; 514 int nsrc, ndst, err, ret = 0; 515 DECLARE_WAITQUEUE(wait, current); 516 517 add_wait_queue(&bta->readq, &wait); 518 mutex_lock(&bta->lock); 519 while (swcount > 0) { 520 if (0 == bta->read_count) { 521 if (!bta->recording) { 522 if (0 != (err = start_recording(bta))) { 523 if (0 == ret) 524 ret = err; 525 break; 526 } 527 } 528 if (file->f_flags & O_NONBLOCK) { 529 if (0 == ret) 530 ret = -EAGAIN; 531 break; 532 } 533 mutex_unlock(&bta->lock); 534 current->state = TASK_INTERRUPTIBLE; 535 schedule(); 536 mutex_lock(&bta->lock); 537 if(signal_pending(current)) { 538 if (0 == ret) 539 ret = -EINTR; 540 break; 541 } 542 } 543 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount; 544 if (nsrc > bta->buf_size - bta->read_offset) 545 nsrc = bta->buf_size - bta->read_offset; 546 ndst = nsrc >> bta->sampleshift; 547 548 if ((bta->analog && 0 == bta->sampleshift) || 549 (!bta->analog && 2 == bta->channels)) { 550 /* just copy */ 551 if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) { 552 if (0 == ret) 553 ret = -EFAULT; 554 break; 555 } 556 557 } else if (!bta->analog) { 558 /* stereo => mono (digital audio) */ 559 __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset); 560 __s16 __user *dst = (__s16 __user *)(buffer + ret); 561 __s16 avg; 562 int n = ndst>>1; 563 if (!access_ok(VERIFY_WRITE, dst, ndst)) { 564 if (0 == ret) 565 ret = -EFAULT; 566 break; 567 } 568 for (; n; n--, dst++) { 569 avg = (__s16)le16_to_cpu(*src) / 2; src++; 570 avg += (__s16)le16_to_cpu(*src) / 2; src++; 571 __put_user(cpu_to_le16(avg),dst); 572 } 573 574 } else if (8 == bta->bits) { 575 /* copy + byte downsampling (audio A/D) */ 576 __u8 *src = bta->buf_cpu + bta->read_offset; 577 __u8 __user *dst = buffer + ret; 578 int n = ndst; 579 if (!access_ok(VERIFY_WRITE, dst, ndst)) { 580 if (0 == ret) 581 ret = -EFAULT; 582 break; 583 } 584 for (; n; n--, src += (1 << bta->sampleshift), dst++) 585 __put_user(*src, dst); 586 587 } else { 588 /* copy + word downsampling (audio A/D) */ 589 __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset); 590 __u16 __user *dst = (__u16 __user *)(buffer + ret); 591 int n = ndst>>1; 592 if (!access_ok(VERIFY_WRITE,dst,ndst)) { 593 if (0 == ret) 594 ret = -EFAULT; 595 break; 596 } 597 for (; n; n--, src += (1 << bta->sampleshift), dst++) 598 __put_user(*src, dst); 599 } 600 601 ret += ndst; 602 swcount -= ndst; 603 hwcount -= nsrc; 604 bta->read_count -= nsrc; 605 bta->read_offset += nsrc; 606 if (bta->read_offset == bta->buf_size) 607 bta->read_offset = 0; 608 } 609 mutex_unlock(&bta->lock); 610 remove_wait_queue(&bta->readq, &wait); 611 current->state = TASK_RUNNING; 612 return ret; 613} 614 615static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer, 616 size_t count, loff_t *ppos) 617{ 618 return -EINVAL; 619} 620 621static int btaudio_dsp_ioctl(struct inode *inode, struct file *file, 622 unsigned int cmd, unsigned long arg) 623{ 624 struct btaudio *bta = file->private_data; 625 int s, i, ret, val = 0; 626 void __user *argp = (void __user *)arg; 627 int __user *p = argp; 628 629 switch (cmd) { 630 case OSS_GETVERSION: 631 return put_user(SOUND_VERSION, p); 632 case SNDCTL_DSP_GETCAPS: 633 return 0; 634 635 case SNDCTL_DSP_SPEED: 636 if (get_user(val, p)) 637 return -EFAULT; 638 if (bta->analog) { 639 for (s = 0; s < 16; s++) 640 if (val << s >= HWBASE_AD*4/15) 641 break; 642 for (i = 15; i >= 5; i--) 643 if (val << s <= HWBASE_AD*4/i) 644 break; 645 bta->sampleshift = s; 646 bta->decimation = i; 647 if (debug) 648 printk(KERN_DEBUG "btaudio: rate: req=%d " 649 "dec=%d shift=%d hwrate=%d swrate=%d\n", 650 val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s); 651 } else { 652 bta->sampleshift = (bta->channels == 2) ? 0 : 1; 653 bta->decimation = 0; 654 } 655 if (bta->recording) { 656 mutex_lock(&bta->lock); 657 stop_recording(bta); 658 start_recording(bta); 659 mutex_unlock(&bta->lock); 660 } 661 /* fall through */ 662 case SOUND_PCM_READ_RATE: 663 if (bta->analog) { 664 return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, p); 665 } else { 666 return put_user(bta->rate, p); 667 } 668 669 case SNDCTL_DSP_STEREO: 670 if (!bta->analog) { 671 if (get_user(val, p)) 672 return -EFAULT; 673 bta->channels = (val > 0) ? 2 : 1; 674 bta->sampleshift = (bta->channels == 2) ? 0 : 1; 675 if (debug) 676 printk(KERN_INFO 677 "btaudio: stereo=%d channels=%d\n", 678 val,bta->channels); 679 } else { 680 if (val == 1) 681 return -EFAULT; 682 else { 683 bta->channels = 1; 684 if (debug) 685 printk(KERN_INFO 686 "btaudio: stereo=0 channels=1\n"); 687 } 688 } 689 return put_user((bta->channels)-1, p); 690 691 case SNDCTL_DSP_CHANNELS: 692 if (!bta->analog) { 693 if (get_user(val, p)) 694 return -EFAULT; 695 bta->channels = (val > 1) ? 2 : 1; 696 bta->sampleshift = (bta->channels == 2) ? 0 : 1; 697 if (debug) 698 printk(KERN_DEBUG 699 "btaudio: val=%d channels=%d\n", 700 val,bta->channels); 701 } 702 /* fall through */ 703 case SOUND_PCM_READ_CHANNELS: 704 return put_user(bta->channels, p); 705 706 case SNDCTL_DSP_GETFMTS: /* Returns a mask */ 707 if (bta->analog) 708 return put_user(AFMT_S16_LE|AFMT_S8, p); 709 else 710 return put_user(AFMT_S16_LE, p); 711 712 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/ 713 if (get_user(val, p)) 714 return -EFAULT; 715 if (val != AFMT_QUERY) { 716 if (bta->analog) 717 bta->bits = (val == AFMT_S8) ? 8 : 16; 718 else 719 bta->bits = 16; 720 if (bta->recording) { 721 mutex_lock(&bta->lock); 722 stop_recording(bta); 723 start_recording(bta); 724 mutex_unlock(&bta->lock); 725 } 726 } 727 if (debug) 728 printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits); 729 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8, 730 p); 731 break; 732 case SOUND_PCM_READ_BITS: 733 return put_user(bta->bits, p); 734 735 case SNDCTL_DSP_NONBLOCK: 736 file->f_flags |= O_NONBLOCK; 737 return 0; 738 739 case SNDCTL_DSP_RESET: 740 if (bta->recording) { 741 mutex_lock(&bta->lock); 742 stop_recording(bta); 743 mutex_unlock(&bta->lock); 744 } 745 return 0; 746 case SNDCTL_DSP_GETBLKSIZE: 747 if (!bta->recording) { 748 if (0 != (ret = alloc_buffer(bta))) 749 return ret; 750 if (0 != (ret = make_risc(bta))) 751 return ret; 752 } 753 return put_user(bta->block_bytes>>bta->sampleshift,p); 754 755 case SNDCTL_DSP_SYNC: 756 /* NOP */ 757 return 0; 758 case SNDCTL_DSP_GETISPACE: 759 { 760 audio_buf_info info; 761 if (!bta->recording) 762 return -EINVAL; 763 info.fragsize = bta->block_bytes>>bta->sampleshift; 764 info.fragstotal = bta->block_count; 765 info.bytes = bta->read_count; 766 info.fragments = info.bytes / info.fragsize; 767 if (debug) 768 printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE " 769 "returns %d/%d/%d/%d\n", 770 info.fragsize, info.fragstotal, 771 info.bytes, info.fragments); 772 if (copy_to_user(argp, &info, sizeof(info))) 773 return -EFAULT; 774 return 0; 775 } 776 default: 777 return -EINVAL; 778 } 779} 780 781static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait) 782{ 783 struct btaudio *bta = file->private_data; 784 unsigned int mask = 0; 785 786 poll_wait(file, &bta->readq, wait); 787 788 if (0 != bta->read_count) 789 mask |= (POLLIN | POLLRDNORM); 790 791 return mask; 792} 793 794static const struct file_operations btaudio_digital_dsp_fops = { 795 .owner = THIS_MODULE, 796 .llseek = no_llseek, 797 .open = btaudio_dsp_open_digital, 798 .release = btaudio_dsp_release, 799 .read = btaudio_dsp_read, 800 .write = btaudio_dsp_write, 801 .ioctl = btaudio_dsp_ioctl, 802 .poll = btaudio_dsp_poll, 803}; 804 805static const struct file_operations btaudio_analog_dsp_fops = { 806 .owner = THIS_MODULE, 807 .llseek = no_llseek, 808 .open = btaudio_dsp_open_analog, 809 .release = btaudio_dsp_release, 810 .read = btaudio_dsp_read, 811 .write = btaudio_dsp_write, 812 .ioctl = btaudio_dsp_ioctl, 813 .poll = btaudio_dsp_poll, 814}; 815 816/* -------------------------------------------------------------- */ 817 818static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "", 819 "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR", 820 "RIPERR", "PABORT", "OCERR", "SCERR" }; 821 822static irqreturn_t btaudio_irq(int irq, void *dev_id) 823{ 824 int count = 0; 825 u32 stat,astat; 826 struct btaudio *bta = dev_id; 827 int handled = 0; 828 829 for (;;) { 830 count++; 831 stat = btread(REG_INT_STAT); 832 astat = stat & btread(REG_INT_MASK); 833 if (!astat) 834 return IRQ_RETVAL(handled); 835 handled = 1; 836 btwrite(astat,REG_INT_STAT); 837 838 if (irq_debug) { 839 int i; 840 printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:", 841 count, stat>>28); 842 for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) { 843 if (stat & (1 << i)) 844 printk(" %s",irq_name[i]); 845 if (astat & (1 << i)) 846 printk("*"); 847 } 848 printk("\n"); 849 } 850 if (stat & IRQ_RISCI) { 851 int blocks; 852 blocks = (stat >> 28) - bta->dma_block; 853 if (blocks < 0) 854 blocks += bta->block_count; 855 bta->dma_block = stat >> 28; 856 if (bta->read_count + 2*bta->block_bytes > bta->buf_size) { 857 stop_recording(bta); 858 printk(KERN_INFO "btaudio: buffer overrun\n"); 859 } 860 if (blocks > 0) { 861 bta->read_count += blocks * bta->block_bytes; 862 wake_up_interruptible(&bta->readq); 863 } 864 } 865 if (count > 10) { 866 printk(KERN_WARNING 867 "btaudio: Oops - irq mask cleared\n"); 868 btwrite(0, REG_INT_MASK); 869 } 870 } 871 return IRQ_NONE; 872} 873 874/* -------------------------------------------------------------- */ 875 876static unsigned int dsp1 = -1; 877static unsigned int dsp2 = -1; 878static unsigned int mixer = -1; 879static int latency = -1; 880static int digital = 1; 881static int analog = 1; 882static int rate; 883 884#define BTA_OSPREY200 1 885 886static struct cardinfo cards[] = { 887 [0] = { 888 .name = "default", 889 .rate = 32000, 890 }, 891 [BTA_OSPREY200] = { 892 .name = "Osprey 200", 893 .rate = 44100, 894 }, 895}; 896 897static int __devinit btaudio_probe(struct pci_dev *pci_dev, 898 const struct pci_device_id *pci_id) 899{ 900 struct btaudio *bta; 901 struct cardinfo *card = &cards[pci_id->driver_data]; 902 unsigned char revision,lat; 903 int rc = -EBUSY; 904 905 if (pci_enable_device(pci_dev)) 906 return -EIO; 907 if (!request_mem_region(pci_resource_start(pci_dev,0), 908 pci_resource_len(pci_dev,0), 909 "btaudio")) { 910 return -EBUSY; 911 } 912 913 bta = kzalloc(sizeof(*bta),GFP_ATOMIC); 914 if (!bta) { 915 rc = -ENOMEM; 916 goto fail0; 917 } 918 919 bta->pci = pci_dev; 920 bta->irq = pci_dev->irq; 921 bta->mem = pci_resource_start(pci_dev,0); 922 bta->mmio = ioremap(pci_resource_start(pci_dev,0), 923 pci_resource_len(pci_dev,0)); 924 925 bta->source = 1; 926 bta->bits = 8; 927 bta->channels = 1; 928 if (bta->analog) { 929 bta->decimation = 15; 930 } else { 931 bta->decimation = 0; 932 bta->sampleshift = 1; 933 } 934 935 /* sample rate */ 936 bta->rate = card->rate; 937 if (rate) 938 bta->rate = rate; 939 940 mutex_init(&bta->lock); 941 init_waitqueue_head(&bta->readq); 942 943 if (-1 != latency) { 944 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n", 945 latency); 946 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency); 947 } 948 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision); 949 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat); 950 printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ", 951 pci_dev->device,revision,pci_dev->bus->number, 952 PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn)); 953 printk("irq: %d, latency: %d, mmio: 0x%lx\n", 954 bta->irq, lat, bta->mem); 955 printk("btaudio: using card config \"%s\"\n", card->name); 956 957 /* init hw */ 958 btwrite(0, REG_GPIO_DMA_CTL); 959 btwrite(0, REG_INT_MASK); 960 btwrite(~0U, REG_INT_STAT); 961 pci_set_master(pci_dev); 962 963 if ((rc = request_irq(bta->irq, btaudio_irq, IRQF_SHARED|IRQF_DISABLED, 964 "btaudio",(void *)bta)) < 0) { 965 printk(KERN_WARNING 966 "btaudio: can't request irq (rc=%d)\n",rc); 967 goto fail1; 968 } 969 970 /* register devices */ 971 if (digital) { 972 rc = bta->dsp_digital = 973 register_sound_dsp(&btaudio_digital_dsp_fops,dsp1); 974 if (rc < 0) { 975 printk(KERN_WARNING 976 "btaudio: can't register digital dsp (rc=%d)\n",rc); 977 goto fail2; 978 } 979 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n", 980 bta->dsp_digital >> 4); 981 } 982 if (analog) { 983 rc = bta->dsp_analog = 984 register_sound_dsp(&btaudio_analog_dsp_fops,dsp2); 985 if (rc < 0) { 986 printk(KERN_WARNING 987 "btaudio: can't register analog dsp (rc=%d)\n",rc); 988 goto fail3; 989 } 990 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n", 991 bta->dsp_analog >> 4); 992 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer); 993 if (rc < 0) { 994 printk(KERN_WARNING 995 "btaudio: can't register mixer (rc=%d)\n",rc); 996 goto fail4; 997 } 998 printk(KERN_INFO "btaudio: registered device mixer%d\n", 999 bta->mixer_dev >> 4); 1000 } 1001 1002 /* hook into linked list */ 1003 bta->next = btaudios; 1004 btaudios = bta; 1005 1006 pci_set_drvdata(pci_dev,bta); 1007 return 0; 1008 1009 fail4: 1010 unregister_sound_dsp(bta->dsp_analog); 1011 fail3: 1012 if (digital) 1013 unregister_sound_dsp(bta->dsp_digital); 1014 fail2: 1015 free_irq(bta->irq,bta); 1016 fail1: 1017 iounmap(bta->mmio); 1018 kfree(bta); 1019 fail0: 1020 release_mem_region(pci_resource_start(pci_dev,0), 1021 pci_resource_len(pci_dev,0)); 1022 return rc; 1023} 1024 1025static void __devexit btaudio_remove(struct pci_dev *pci_dev) 1026{ 1027 struct btaudio *bta = pci_get_drvdata(pci_dev); 1028 struct btaudio *walk; 1029 1030 /* turn off all DMA / IRQs */ 1031 btand(~15, REG_GPIO_DMA_CTL); 1032 btwrite(0, REG_INT_MASK); 1033 btwrite(~0U, REG_INT_STAT); 1034 1035 /* unregister devices */ 1036 if (digital) { 1037 unregister_sound_dsp(bta->dsp_digital); 1038 } 1039 if (analog) { 1040 unregister_sound_dsp(bta->dsp_analog); 1041 unregister_sound_mixer(bta->mixer_dev); 1042 } 1043 1044 /* free resources */ 1045 free_buffer(bta); 1046 free_irq(bta->irq,bta); 1047 release_mem_region(pci_resource_start(pci_dev,0), 1048 pci_resource_len(pci_dev,0)); 1049 iounmap(bta->mmio); 1050 1051 /* remove from linked list */ 1052 if (bta == btaudios) { 1053 btaudios = NULL; 1054 } else { 1055 for (walk = btaudios; walk->next != bta; walk = walk->next) 1056 ; /* if (NULL == walk->next) BUG(); */ 1057 walk->next = bta->next; 1058 } 1059 1060 pci_set_drvdata(pci_dev, NULL); 1061 kfree(bta); 1062 return; 1063} 1064 1065/* -------------------------------------------------------------- */ 1066 1067static struct pci_device_id btaudio_pci_tbl[] = { 1068 { 1069 .vendor = PCI_VENDOR_ID_BROOKTREE, 1070 .device = 0x0878, 1071 .subvendor = 0x0070, 1072 .subdevice = 0xff01, 1073 .driver_data = BTA_OSPREY200, 1074 },{ 1075 .vendor = PCI_VENDOR_ID_BROOKTREE, 1076 .device = 0x0878, 1077 .subvendor = PCI_ANY_ID, 1078 .subdevice = PCI_ANY_ID, 1079 },{ 1080 .vendor = PCI_VENDOR_ID_BROOKTREE, 1081 .device = 0x0878, 1082 .subvendor = PCI_ANY_ID, 1083 .subdevice = PCI_ANY_ID, 1084 },{ 1085 /* --- end of list --- */ 1086 } 1087}; 1088 1089static struct pci_driver btaudio_pci_driver = { 1090 .name = "btaudio", 1091 .id_table = btaudio_pci_tbl, 1092 .probe = btaudio_probe, 1093 .remove = __devexit_p(btaudio_remove), 1094}; 1095 1096static int btaudio_init_module(void) 1097{ 1098 printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n", 1099 digital ? "digital" : "", 1100 analog && digital ? "+" : "", 1101 analog ? "analog" : ""); 1102 return pci_register_driver(&btaudio_pci_driver); 1103} 1104 1105static void btaudio_cleanup_module(void) 1106{ 1107 pci_unregister_driver(&btaudio_pci_driver); 1108 return; 1109} 1110 1111module_init(btaudio_init_module); 1112module_exit(btaudio_cleanup_module); 1113 1114module_param(dsp1, int, S_IRUGO); 1115module_param(dsp2, int, S_IRUGO); 1116module_param(mixer, int, S_IRUGO); 1117module_param(debug, int, S_IRUGO | S_IWUSR); 1118module_param(irq_debug, int, S_IRUGO | S_IWUSR); 1119module_param(digital, int, S_IRUGO); 1120module_param(analog, int, S_IRUGO); 1121module_param(rate, int, S_IRUGO); 1122module_param(latency, int, S_IRUGO); 1123MODULE_PARM_DESC(latency,"pci latency timer"); 1124 1125MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl); 1126MODULE_DESCRIPTION("bt878 audio dma driver"); 1127MODULE_AUTHOR("Gerd Knorr"); 1128MODULE_LICENSE("GPL"); 1129 1130/* 1131 * Local variables: 1132 * c-basic-offset: 8 1133 * End: 1134 */ 1135