Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[ALSA] Several fixes for the Sun DBRI driver

SPARC DBRI driver
This patch contains the following fixes to the Alsa DBRI driver:

- Remove 2.6.13 build warning on the prom_getproperty() call.

- Rework command synchronization: send a sequence number with D_WAIT,
and check it in the completion interrupt.
Move synchronization delays from _cmdsend() to _cmdlock() allowing the
CPU to do other usefull things while the DBRI is processing the
commands.

- Fix first argument of printk() calls.

- Enable burst transfers for DBRI. Original 2.4 patch from Krzysztof
Helt

- Make dbri_debug module parameter writable from sysfs. Remove obsolete
write access to the /proc debug file.

- Replace udelay() with msleep_interruptible() where possible.

- Update documentation comments.

Signed-off-by: Martin Habets <errandir_news@mph.eclipse.co.uk>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by

Martin Habets and committed by
Jaroslav Kysela
4338829e afe0f1f6

+103 -102
+103 -102
sound/sparc/dbri.c
··· 1 1 /* 2 2 * Driver for DBRI sound chip found on Sparcs. 3 - * Copyright (C) 2004 Martin Habets (mhabets@users.sourceforge.net) 3 + * Copyright (C) 2004, 2005 Martin Habets (mhabets@users.sourceforge.net) 4 4 * 5 5 * Based entirely upon drivers/sbus/audio/dbri.c which is: 6 6 * Copyright (C) 1997 Rudolf Koenig (rfkoenig@immd4.informatik.uni-erlangen.de) ··· 43 43 * audio devices. But the SUN HW group decided against it, at least on my 44 44 * LX the speakerbox connector has at least 1 pin missing and 1 wrongly 45 45 * connected. 46 + * 47 + * I've tried to stick to the following function naming conventions: 48 + * snd_* ALSA stuff 49 + * cs4215_* CS4215 codec specfic stuff 50 + * dbri_* DBRI high-level stuff 51 + * other DBRI low-level stuff 46 52 */ 47 53 48 54 #include <sound/driver.h> ··· 93 87 #define D_DESC (1<<5) 94 88 95 89 static int dbri_debug = 0; 96 - module_param(dbri_debug, int, 0444); 90 + module_param(dbri_debug, int, 0644); 97 91 MODULE_PARM_DESC(dbri_debug, "Debug value for Sun DBRI soundcard."); 98 92 99 93 #ifdef DBRI_DEBUG ··· 326 320 void __iomem *regs; /* dbri HW regs */ 327 321 int dbri_version; /* 'e' and up is OK */ 328 322 int dbri_irqp; /* intr queue pointer */ 329 - int wait_seen; 323 + int wait_send; /* sequence of command buffers send */ 324 + int wait_ackd; /* sequence of command buffers acknowledged */ 330 325 331 326 struct dbri_pipe pipes[DBRI_NO_PIPES]; /* DBRI's 32 data pipes */ 332 327 struct dbri_desc descs[DBRI_NO_DESCS]; ··· 632 625 633 626 Commands are sent to the DBRI by building a list of them in memory, 634 627 then writing the address of the first list item to DBRI register 8. 635 - The list is terminated with a WAIT command, which can generate a 636 - CPU interrupt if required. 628 + The list is terminated with a WAIT command, which generates a 629 + CPU interrupt to signal completion. 637 630 638 631 Since the DBRI can run in parallel with the CPU, several means of 639 - synchronization present themselves. The original scheme (Rudolf's) 640 - was to set a flag when we "cmdlock"ed the DBRI, clear the flag when 641 - an interrupt signaled completion, and wait on a wait_queue if a routine 642 - attempted to cmdlock while the flag was set. The problems arose when 643 - we tried to cmdlock from inside an interrupt handler, which might 644 - cause scheduling in an interrupt (if we waited), etc, etc 632 + synchronization present themselves. The method implemented here is close 633 + to the original scheme (Rudolf's), and uses 2 counters (wait_send and 634 + wait_ackd) to synchronize the command buffer between the CPU and the DBRI. 645 635 646 636 A more sophisticated scheme might involve a circular command buffer 647 637 or an array of command buffers. A routine could fill one with ··· 646 642 completion of the current command buffer, look on the list for 647 643 the next one. 648 644 649 - I've decided to implement something much simpler - after each command, 650 - the CPU waits for the DBRI to finish the command by polling the P bit 651 - in DBRI register 0. I've tried to implement this in such a way 652 - that might make implementing a more sophisticated scheme easier. 653 - 654 645 Every time a routine wants to write commands to the DBRI, it must 655 646 first call dbri_cmdlock() and get an initial pointer into dbri->dma->cmd 656 - in return. After the commands have been writen, dbri_cmdsend() is 657 - called with the final pointer value. 647 + in return. dbri_cmdlock() will block if the previous commands have not 648 + been completed yet. After this the commands can be written to the buffer, 649 + and dbri_cmdsend() is called with the final pointer value to send them 650 + to the DBRI. 658 651 659 652 */ 660 653 654 + static void dbri_process_interrupt_buffer(snd_dbri_t * dbri); 655 + 661 656 enum dbri_lock_t { NoGetLock, GetLock }; 657 + #define MAXLOOPS 10 662 658 663 659 static volatile s32 *dbri_cmdlock(snd_dbri_t * dbri, enum dbri_lock_t get) 664 660 { 661 + int maxloops = MAXLOOPS; 662 + 665 663 #ifndef SMP 666 664 if ((get == GetLock) && spin_is_locked(&dbri->lock)) { 667 665 printk(KERN_ERR "DBRI: cmdlock called while in spinlock."); 668 666 } 669 667 #endif 670 668 669 + /* Delay if previous commands are still being processed */ 670 + while ((--maxloops) > 0 && (dbri->wait_send != dbri->wait_ackd)) { 671 + msleep_interruptible(1); 672 + /* If dbri_cmdlock() got called from inside the 673 + * interrupt handler, this will do the processing. 674 + */ 675 + dbri_process_interrupt_buffer(dbri); 676 + } 677 + if (maxloops == 0) { 678 + printk(KERN_ERR "DBRI: Chip never completed command buffer %d\n", 679 + dbri->wait_send); 680 + } else { 681 + dprintk(D_CMD, "Chip completed command buffer (%d)\n", 682 + MAXLOOPS - maxloops - 1); 683 + } 684 + 671 685 /*if (get == GetLock) spin_lock(&dbri->lock); */ 672 686 return &dbri->dma->cmd[0]; 673 687 } 674 688 675 - static void dbri_process_interrupt_buffer(snd_dbri_t *); 676 - 677 689 static void dbri_cmdsend(snd_dbri_t * dbri, volatile s32 * cmd) 678 690 { 679 - int MAXLOOPS = 1000000; 680 - int maxloops = MAXLOOPS; 681 691 volatile s32 *ptr; 692 + u32 reg; 682 693 683 694 for (ptr = &dbri->dma->cmd[0]; ptr < cmd; ptr++) { 684 695 dprintk(D_CMD, "cmd: %lx:%08x\n", (unsigned long)ptr, *ptr); 685 696 } 686 697 687 698 if ((cmd - &dbri->dma->cmd[0]) >= DBRI_NO_CMDS - 1) { 688 - printk("DBRI: Command buffer overflow! (bug in driver)\n"); 699 + printk(KERN_ERR "DBRI: Command buffer overflow! (bug in driver)\n"); 689 700 /* Ignore the last part. */ 690 701 cmd = &dbri->dma->cmd[DBRI_NO_CMDS - 3]; 691 702 } 692 703 704 + dbri->wait_send++; 705 + dbri->wait_send &= 0xffff; /* restrict it to a 16 bit counter. */ 693 706 *(cmd++) = DBRI_CMD(D_PAUSE, 0, 0); 694 - *(cmd++) = DBRI_CMD(D_WAIT, 1, 0); 695 - dbri->wait_seen = 0; 707 + *(cmd++) = DBRI_CMD(D_WAIT, 1, dbri->wait_send); 708 + 709 + /* Set command pointer and signal it is valid. */ 696 710 sbus_writel(dbri->dma_dvma, dbri->regs + REG8); 697 - while ((--maxloops) > 0 && (sbus_readl(dbri->regs + REG0) & D_P)) 698 - barrier(); 699 - if (maxloops == 0) { 700 - printk(KERN_ERR "DBRI: Chip never completed command buffer\n"); 701 - dprintk(D_CMD, "DBRI: Chip never completed command buffer\n"); 702 - } else { 703 - while ((--maxloops) > 0 && (!dbri->wait_seen)) 704 - dbri_process_interrupt_buffer(dbri); 705 - if (maxloops == 0) { 706 - printk(KERN_ERR "DBRI: Chip never acked WAIT\n"); 707 - dprintk(D_CMD, "DBRI: Chip never acked WAIT\n"); 708 - } else { 709 - dprintk(D_CMD, "Chip completed command " 710 - "buffer (%d)\n", MAXLOOPS - maxloops); 711 - } 712 - } 711 + reg = sbus_readl(dbri->regs + REG0); 712 + reg |= D_P; 713 + sbus_writel(reg, dbri->regs + REG0); 713 714 714 715 /*spin_unlock(&dbri->lock); */ 715 716 } ··· 766 757 for (n = 0; n < DBRI_NO_PIPES; n++) 767 758 dbri->pipes[n].desc = dbri->pipes[n].first_desc = -1; 768 759 769 - /* We should query the openprom to see what burst sizes this 770 - * SBus supports. For now, just disable all SBus bursts */ 760 + /* A brute approach - DBRI falls back to working burst size by itself 761 + * On SS20 D_S does not work, so do not try so high. */ 771 762 tmp = sbus_readl(dbri->regs + REG0); 772 - tmp &= ~(D_G | D_S | D_E); 763 + tmp |= D_G | D_E; 764 + tmp &= ~D_S; 773 765 sbus_writel(tmp, dbri->regs + REG0); 774 766 775 767 /* ··· 815 805 volatile int *cmd; 816 806 817 807 if (pipe < 0 || pipe > 31) { 818 - printk("DBRI: reset_pipe called with illegal pipe number\n"); 808 + printk(KERN_ERR "DBRI: reset_pipe called with illegal pipe number\n"); 819 809 return; 820 810 } 821 811 822 812 sdp = dbri->pipes[pipe].sdp; 823 813 if (sdp == 0) { 824 - printk("DBRI: reset_pipe called on uninitialized pipe\n"); 814 + printk(KERN_ERR "DBRI: reset_pipe called on uninitialized pipe\n"); 825 815 return; 826 816 } 827 817 ··· 844 834 static void setup_pipe(snd_dbri_t * dbri, int pipe, int sdp) 845 835 { 846 836 if (pipe < 0 || pipe > 31) { 847 - printk("DBRI: setup_pipe called with illegal pipe number\n"); 837 + printk(KERN_ERR "DBRI: setup_pipe called with illegal pipe number\n"); 848 838 return; 849 839 } 850 840 851 841 if ((sdp & 0xf800) != sdp) { 852 - printk("DBRI: setup_pipe called with strange SDP value\n"); 842 + printk(KERN_ERR "DBRI: setup_pipe called with strange SDP value\n"); 853 843 /* sdp &= 0xf800; */ 854 844 } 855 845 ··· 882 872 int nextpipe; 883 873 884 874 if (pipe < 0 || pipe > 31 || basepipe < 0 || basepipe > 31) { 885 - printk 886 - ("DBRI: link_time_slot called with illegal pipe number\n"); 875 + printk(KERN_ERR 876 + "DBRI: link_time_slot called with illegal pipe number\n"); 887 877 return; 888 878 } 889 879 890 880 if (dbri->pipes[pipe].sdp == 0 || dbri->pipes[basepipe].sdp == 0) { 891 - printk("DBRI: link_time_slot called on uninitialized pipe\n"); 881 + printk(KERN_ERR "DBRI: link_time_slot called on uninitialized pipe\n"); 892 882 return; 893 883 } 894 884 ··· 970 960 int val; 971 961 972 962 if (pipe < 0 || pipe > 31 || prevpipe < 0 || prevpipe > 31) { 973 - printk 974 - ("DBRI: unlink_time_slot called with illegal pipe number\n"); 963 + printk(KERN_ERR 964 + "DBRI: unlink_time_slot called with illegal pipe number\n"); 975 965 return; 976 966 } 977 967 ··· 1011 1001 volatile s32 *cmd; 1012 1002 1013 1003 if (pipe < 16 || pipe > 31) { 1014 - printk("DBRI: xmit_fixed: Illegal pipe number\n"); 1004 + printk(KERN_ERR "DBRI: xmit_fixed: Illegal pipe number\n"); 1015 1005 return; 1016 1006 } 1017 1007 1018 1008 if (D_SDP_MODE(dbri->pipes[pipe].sdp) == 0) { 1019 - printk("DBRI: xmit_fixed: Uninitialized pipe %d\n", pipe); 1009 + printk(KERN_ERR "DBRI: xmit_fixed: Uninitialized pipe %d\n", pipe); 1020 1010 return; 1021 1011 } 1022 1012 1023 1013 if (D_SDP_MODE(dbri->pipes[pipe].sdp) != D_SDP_FIXED) { 1024 - printk("DBRI: xmit_fixed: Non-fixed pipe %d\n", pipe); 1014 + printk(KERN_ERR "DBRI: xmit_fixed: Non-fixed pipe %d\n", pipe); 1025 1015 return; 1026 1016 } 1027 1017 1028 1018 if (!(dbri->pipes[pipe].sdp & D_SDP_TO_SER)) { 1029 - printk("DBRI: xmit_fixed: Called on receive pipe %d\n", pipe); 1019 + printk(KERN_ERR "DBRI: xmit_fixed: Called on receive pipe %d\n", pipe); 1030 1020 return; 1031 1021 } 1032 1022 ··· 1046 1036 static void recv_fixed(snd_dbri_t * dbri, int pipe, volatile __u32 * ptr) 1047 1037 { 1048 1038 if (pipe < 16 || pipe > 31) { 1049 - printk("DBRI: recv_fixed called with illegal pipe number\n"); 1039 + printk(KERN_ERR "DBRI: recv_fixed called with illegal pipe number\n"); 1050 1040 return; 1051 1041 } 1052 1042 1053 1043 if (D_SDP_MODE(dbri->pipes[pipe].sdp) != D_SDP_FIXED) { 1054 - printk("DBRI: recv_fixed called on non-fixed pipe %d\n", pipe); 1044 + printk(KERN_ERR "DBRI: recv_fixed called on non-fixed pipe %d\n", pipe); 1055 1045 return; 1056 1046 } 1057 1047 1058 1048 if (dbri->pipes[pipe].sdp & D_SDP_TO_SER) { 1059 - printk("DBRI: recv_fixed called on transmit pipe %d\n", pipe); 1049 + printk(KERN_ERR "DBRI: recv_fixed called on transmit pipe %d\n", pipe); 1060 1050 return; 1061 1051 } 1062 1052 ··· 1085 1075 int last_desc = -1; 1086 1076 1087 1077 if (info->pipe < 0 || info->pipe > 15) { 1088 - printk("DBRI: setup_descs: Illegal pipe number\n"); 1078 + printk(KERN_ERR "DBRI: setup_descs: Illegal pipe number\n"); 1089 1079 return -2; 1090 1080 } 1091 1081 1092 1082 if (dbri->pipes[info->pipe].sdp == 0) { 1093 - printk("DBRI: setup_descs: Uninitialized pipe %d\n", 1083 + printk(KERN_ERR "DBRI: setup_descs: Uninitialized pipe %d\n", 1094 1084 info->pipe); 1095 1085 return -2; 1096 1086 } ··· 1100 1090 1101 1091 if (streamno == DBRI_PLAY) { 1102 1092 if (!(dbri->pipes[info->pipe].sdp & D_SDP_TO_SER)) { 1103 - printk("DBRI: setup_descs: Called on receive pipe %d\n", 1093 + printk(KERN_ERR "DBRI: setup_descs: Called on receive pipe %d\n", 1104 1094 info->pipe); 1105 1095 return -2; 1106 1096 } 1107 1097 } else { 1108 1098 if (dbri->pipes[info->pipe].sdp & D_SDP_TO_SER) { 1109 - printk 1110 - ("DBRI: setup_descs: Called on transmit pipe %d\n", 1099 + printk(KERN_ERR 1100 + "DBRI: setup_descs: Called on transmit pipe %d\n", 1111 1101 info->pipe); 1112 1102 return -2; 1113 1103 } 1114 1104 /* Should be able to queue multiple buffers to receive on a pipe */ 1115 1105 if (pipe_active(dbri, info->pipe)) { 1116 - printk("DBRI: recv_on_pipe: Called on active pipe %d\n", 1106 + printk(KERN_ERR "DBRI: recv_on_pipe: Called on active pipe %d\n", 1117 1107 info->pipe); 1118 1108 return -2; 1119 1109 } ··· 1130 1120 break; 1131 1121 } 1132 1122 if (desc == DBRI_NO_DESCS) { 1133 - printk("DBRI: setup_descs: No descriptors\n"); 1123 + printk(KERN_ERR "DBRI: setup_descs: No descriptors\n"); 1134 1124 return -1; 1135 1125 } 1136 1126 ··· 1175 1165 } 1176 1166 1177 1167 if (first_desc == -1 || last_desc == -1) { 1178 - printk("DBRI: setup_descs: Not enough descriptors available\n"); 1168 + printk(KERN_ERR "DBRI: setup_descs: Not enough descriptors available\n"); 1179 1169 return -1; 1180 1170 } 1181 1171 ··· 1280 1270 int divisor = 12288 / clockrate; 1281 1271 1282 1272 if (divisor > 255 || divisor * clockrate != 12288) 1283 - printk("DBRI: illegal bits_per_frame in setup_chi\n"); 1273 + printk(KERN_ERR "DBRI: illegal bits_per_frame in setup_chi\n"); 1284 1274 1285 1275 *(cmd++) = DBRI_CMD(D_CHI, 0, D_CHI_CHICM(divisor) | D_CHI_FD 1286 1276 | D_CHI_BPF(bits_per_frame)); ··· 1484 1474 /* Temporarily mute outputs, and wait 1/8000 sec (125 us) 1485 1475 * to make sure this takes. This avoids clicking noises. 1486 1476 */ 1487 - 1488 1477 cs4215_setdata(dbri, 1); 1489 1478 udelay(125); 1490 1479 ··· 1539 1530 tmp |= D_C; /* Enable CHI */ 1540 1531 sbus_writel(tmp, dbri->regs + REG0); 1541 1532 1542 - for (i = 64; ((dbri->mm.status & 0xe4) != 0x20); --i) { 1543 - udelay(125); 1533 + for (i = 10; ((dbri->mm.status & 0xe4) != 0x20); --i) { 1534 + msleep_interruptible(1); 1544 1535 } 1545 1536 if (i == 0) { 1546 1537 dprintk(D_MM, "CS4215 didn't respond to CLB (0x%02x)\n", ··· 1687 1678 Complicated interrupts are handled by dedicated functions (which 1688 1679 appear first in this file). Any pending interrupts can be serviced by 1689 1680 calling dbri_process_interrupt_buffer(), which works even if the CPU's 1690 - interrupts are disabled. This function is used by dbri_cmdsend() 1691 - to make sure we're synced up with the chip after each command sequence, 1681 + interrupts are disabled. This function is used by dbri_cmdlock() 1682 + to make sure we're synced up with the chip before each command sequence, 1692 1683 even if we're running cli'ed. 1693 1684 1694 1685 */ ··· 1774 1765 * Called by main interrupt handler when DBRI signals transmission complete 1775 1766 * on a pipe (interrupt triggered by the B bit in a transmit descriptor). 1776 1767 * 1777 - * Walks through the pipe's list of transmit buffer descriptors, releasing 1778 - * each one's DMA buffer (if present), flagging the descriptor available, 1779 - * and signaling its callback routine (if present), before proceeding 1780 - * to the next one. Stops when the first descriptor is found without 1768 + * Walks through the pipe's list of transmit buffer descriptors and marks 1769 + * them as available. Stops when the first descriptor is found without 1781 1770 * TBC (Transmit Buffer Complete) set, or we've run through them all. 1771 + * 1772 + * The DMA buffers are not released, but re-used. Since the transmit buffer 1773 + * descriptors are not clobbered, they can be re-submitted as is. This is 1774 + * done by the xmit_descs() tasklet above since that could take longer. 1782 1775 */ 1783 1776 1784 1777 static void transmission_complete_intr(snd_dbri_t * dbri, int pipe) ··· 1896 1885 } 1897 1886 1898 1887 if (channel == D_INTR_CMD && command == D_WAIT) { 1899 - dbri->wait_seen++; 1888 + dbri->wait_ackd = val; 1889 + if (dbri->wait_send != val) { 1890 + printk(KERN_ERR "Processing wait command %d when %d was send.\n", 1891 + val, dbri->wait_send); 1892 + } 1900 1893 return; 1901 1894 } 1902 1895 ··· 2009 1994 * The only one I've seen is MRR, which will be triggered 2010 1995 * if you let a transmit pipe underrun, then try to CDP it. 2011 1996 * 2012 - * If these things persist, we should probably reset 2013 - * and re-init the chip. 1997 + * If these things persist, we reset the chip. 2014 1998 */ 2015 1999 if ((++errcnt) % 10 == 0) { 2016 2000 dprintk(D_INT, "Interrupt errors exceeded.\n"); ··· 2108 2094 2109 2095 if ((ret = snd_pcm_lib_malloc_pages(substream, 2110 2096 params_buffer_bytes(hw_params))) < 0) { 2111 - snd_printk(KERN_ERR "malloc_pages failed with %d\n", ret); 2097 + printk(KERN_ERR "malloc_pages failed with %d\n", ret); 2112 2098 return ret; 2113 2099 } 2114 2100 ··· 2469 2455 2470 2456 for (idx = 0; idx < NUM_CS4215_CONTROLS; idx++) { 2471 2457 if ((err = snd_ctl_add(card, 2472 - snd_ctl_new1(&dbri_controls[idx], 2473 - dbri))) < 0) 2458 + snd_ctl_new1(&dbri_controls[idx], dbri))) < 0) 2474 2459 return err; 2475 2460 } 2476 2461 ··· 2503 2490 int pipe; 2504 2491 snd_iprintf(buffer, "debug=%d\n", dbri_debug); 2505 2492 2506 - snd_iprintf(buffer, "CHI pipe in=%d, out=%d\n", 2507 - dbri->chi_in_pipe, dbri->chi_out_pipe); 2508 2493 for (pipe = 0; pipe < 32; pipe++) { 2509 2494 if (pipe_active(dbri, pipe)) { 2510 2495 struct dbri_pipe *pptr = &dbri->pipes[pipe]; ··· 2515 2504 pptr->desc, pptr->length, pptr->cycle, 2516 2505 pptr->prevpipe, pptr->nextpipe); 2517 2506 } 2518 - } 2519 - } 2520 - 2521 - static void dbri_debug_write(snd_info_entry_t * entry, 2522 - snd_info_buffer_t * buffer) 2523 - { 2524 - char line[80]; 2525 - int i; 2526 - 2527 - if (snd_info_get_line(buffer, line, 80) == 0) { 2528 - sscanf(line, "%d\n", &i); 2529 - dbri_debug = i & 0x3f; 2530 2507 } 2531 2508 } 2532 2509 #endif ··· 2530 2531 #ifdef DBRI_DEBUG 2531 2532 err = snd_card_proc_new(dbri->card, "debug", &entry); 2532 2533 snd_info_set_text_ops(entry, dbri, 4096, dbri_debug_read); 2533 - entry->mode = S_IFREG | S_IRUGO | S_IWUSR; /* Writable for root */ 2534 - entry->c.text.write_size = 256; 2535 - entry->c.text.write = dbri_debug_write; 2534 + entry->mode = S_IFREG | S_IRUGO; /* Readable only. */ 2536 2535 #endif 2537 2536 } 2538 2537 ··· 2634 2637 return -ENOENT; 2635 2638 } 2636 2639 2637 - prom_getproperty(prom_node, "intr", (char *)&irq, sizeof(irq)); 2640 + err = prom_getproperty(prom_node, "intr", (char *)&irq, sizeof(irq)); 2641 + if (err < 0) { 2642 + printk(KERN_ERR "DBRI-%d: Firmware node lacks IRQ property.\n", dev); 2643 + return -ENODEV; 2644 + } 2638 2645 2639 2646 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 2640 2647 sizeof(snd_dbri_t));