cs4231.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:83k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: cs4231.c,v 1.47 2001/10/08 22:19:50 davem Exp $
  2.  * drivers/sbus/audio/cs4231.c
  3.  *
  4.  * Copyright 1996, 1997, 1998, 1999 Derrick J Brashear (shadow@andrew.cmu.edu)
  5.  * The 4231/ebus support was written by David Miller, who didn't bother
  6.  * crediting himself here, so I will.
  7.  *
  8.  * Based on the AMD7930 driver:
  9.  * Copyright 1996 Thomas K. Dyas (tdyas@noc.rutgers.edu)
  10.  *
  11.  * This is the lowlevel driver for the CS4231 audio chip found on some
  12.  * sun4m and sun4u machines.
  13.  * 
  14.  * This was culled from the Crystal docs on the 4231a, and the addendum they
  15.  * faxed me on the 4231.
  16.  * The APC DMA controller support unfortunately is not documented. Thanks, Sun.
  17.  */
  18. #include <linux/config.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/soundcard.h>
  28. #include <linux/version.h>
  29. #include <linux/ioport.h>
  30. #include <asm/openprom.h>
  31. #include <asm/oplib.h>
  32. #include <asm/system.h>
  33. #include <asm/irq.h>
  34. #include <asm/io.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/sbus.h>
  37. #if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
  38. #define EB4231_SUPPORT
  39. #include <asm/ebus.h>
  40. #include <asm/pbm.h>
  41. #endif
  42. #include <asm/audioio.h>
  43. #include "cs4231.h"
  44. #undef __CS4231_DEBUG
  45. #undef __CS4231_TRACE
  46. #define __CS4231_ERROR
  47. #ifdef __CS4231_ERROR
  48. #define eprintk(x) printk x
  49. #else
  50. #define eprintk(x)
  51. #endif
  52. #ifdef __CS4231_TRACE
  53. #define tprintk(x) printk x
  54. #else
  55. #define tprintk(x)
  56. #endif
  57. #ifdef __CS4231_DEBUG
  58. #define dprintk(x) printk x
  59. #else
  60. #define dprintk(x)
  61. #endif
  62. #define MAX_DRIVERS 1
  63. static struct sparcaudio_driver drivers[MAX_DRIVERS];
  64. static int num_drivers;
  65. static int cs4231_record_gain(struct sparcaudio_driver *drv, int value, 
  66.                               unsigned char balance);
  67. static int cs4231_play_gain(struct sparcaudio_driver *drv, int value, 
  68.                             unsigned char balance);
  69. static void cs4231_ready(struct sparcaudio_driver *drv);
  70. static void cs4231_playintr(struct sparcaudio_driver *drv, int);
  71. static int cs4231_recintr(struct sparcaudio_driver *drv);
  72. static int cs4231_output_muted(struct sparcaudio_driver *drv, int value);
  73. static void cs4231_pollinput(struct sparcaudio_driver *drv);
  74. static int cs4231_length_to_samplecount(struct audio_prinfo *thisdir, 
  75.                                         unsigned int length);
  76. static void cs4231_getsamplecount(struct sparcaudio_driver *drv, 
  77.                                   unsigned int length, unsigned int value);
  78. #ifdef EB4231_SUPPORT
  79. static void eb4231_pollinput(struct sparcaudio_driver *drv);
  80. #endif
  81. /* Serveral shorthands save typing... */
  82. #define CHIP_READY() 
  83. do { udelay(100); cs4231_ready(drv); udelay(1000); } while(0)
  84. #define WRITE_IAR(__VAL) 
  85. CS4231_WRITE8(cs4231_chip, cs4231_chip->regs + IAR, __VAL)
  86. #define WRITE_IDR(__VAL) 
  87. CS4231_WRITE8(cs4231_chip, cs4231_chip->regs + IDR, __VAL)
  88. #define READ_IAR() 
  89. CS4231_READ8(cs4231_chip, cs4231_chip->regs + IAR)
  90. #define READ_IDR() 
  91. CS4231_READ8(cs4231_chip, cs4231_chip->regs + IDR)
  92. /* Enable cs4231 interrupts atomically. */
  93. static void cs4231_enable_interrupts(struct sparcaudio_driver *drv)
  94. {
  95.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  96.         unsigned long flags;
  97.         tprintk(("enabling interruptsn"));
  98.         save_flags(flags);
  99.         cli();
  100.         if ((cs4231_chip->status & CS_STATUS_INTS_ON) == 0) {
  101.                 WRITE_IAR(0xa);
  102.                 WRITE_IDR(INTR_ON);
  103.                 cs4231_chip->status |= CS_STATUS_INTS_ON;
  104.         }
  105.         restore_flags(flags);
  106. }
  107. /* Disable cs4231 interrupts atomically. */
  108. static void cs4231_disable_interrupts(struct sparcaudio_driver *drv)
  109. {
  110.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  111.         unsigned long flags;
  112.         tprintk(("disabling interruptsn"));
  113.         save_flags(flags);
  114.         cli();
  115.         if ((cs4231_chip->status & CS_STATUS_INTS_ON) != 0) {
  116.                 WRITE_IAR(0xa);
  117.                 WRITE_IDR(INTR_OFF);
  118.                 cs4231_chip->status &= ~CS_STATUS_INTS_ON;
  119.         }
  120.         restore_flags(flags);
  121. }
  122. static void cs4231_enable_play(struct sparcaudio_driver *drv)
  123. {
  124.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  125.         unsigned long flags;
  126.         tprintk(("enabling playn"));
  127.         save_flags(flags);
  128.         cli();
  129.         WRITE_IAR(0x9);
  130.         WRITE_IDR(READ_IDR() | PEN_ENABLE);
  131.         restore_flags(flags);
  132. }
  133. static void cs4231_disable_play(struct sparcaudio_driver *drv)
  134. {
  135.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  136.         unsigned long flags;
  137.         tprintk(("disabling playn"));
  138.         save_flags(flags);
  139.         cli();
  140.         WRITE_IAR(0x9);
  141.         WRITE_IDR(READ_IDR() & PEN_DISABLE);
  142.         restore_flags(flags);
  143. }
  144. static void cs4231_enable_rec(struct sparcaudio_driver *drv)
  145. {
  146.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  147.         unsigned long flags;
  148.         tprintk(("enabling recn"));
  149.         save_flags(flags);
  150.         cli();
  151.         WRITE_IAR(0x9);
  152.         WRITE_IDR(READ_IDR() | CEN_ENABLE);
  153.         restore_flags(flags);
  154. }
  155. static void cs4231_disable_rec(struct sparcaudio_driver *drv)
  156. {
  157.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  158.         unsigned long flags;
  159.         tprintk(("disabling recn"));
  160.         save_flags(flags);
  161.         cli();
  162.         WRITE_IAR(0x9);
  163.         WRITE_IDR(READ_IDR() & CEN_DISABLE);
  164.         restore_flags(flags);
  165. }
  166. static struct cs4231_rates {
  167.         int speed, bits;
  168. } cs4231_rate_table[] = {
  169.         { 5512, CS4231_DFR_5512 },
  170.         { 6615, CS4231_DFR_6615 },
  171.         { 8000, CS4231_DFR_8000 },
  172.         { 9600, CS4231_DFR_9600 },
  173.         { 11025, CS4231_DFR_11025 },
  174.         { 16000, CS4231_DFR_16000 },
  175.         { 18900, CS4231_DFR_18900 },
  176.         { 22050, CS4231_DFR_22050 },
  177.         { 27429, CS4231_DFR_27429 },
  178.         { 32000, CS4231_DFR_32000 },
  179.         { 33075, CS4231_DFR_33075 },
  180.         { 37800, CS4231_DFR_37800 },
  181.         { 44100, CS4231_DFR_44100 },
  182.         { 48000, CS4231_DFR_48000 }
  183. };
  184. #define NUM_RATES (sizeof(cs4231_rate_table) / sizeof(struct cs4231_rates))
  185. static int cs4231_rate_to_bits(struct sparcaudio_driver *drv, int *value)
  186. {
  187.         struct cs4231_rates *p = &cs4231_rate_table[0];
  188.         int i, wanted = *value;
  189.         /* We try to be nice and approximate what the user asks for. */
  190.         if (wanted < 5512)
  191.                 wanted = 5512;
  192.         if (wanted > 48000)
  193.                 wanted = 48000;
  194.         for (i = 0; i < NUM_RATES; i++, p++) {
  195.                 /* Exact match? */
  196.                 if (wanted == p->speed)
  197.                         break;
  198.                 /* If we're inbetween two entries, and neither is exact,
  199.                  * pick the closest one.
  200.                  */
  201.                 if (wanted == p[1].speed)
  202.                         continue;
  203.                 if (wanted > p->speed && wanted < p[1].speed) {
  204.                         int diff1, diff2;
  205.                         diff1 = wanted - p->speed;
  206.                         diff2 = p[1].speed - wanted;
  207.                         if (diff2 < diff1)
  208.                                 p++;
  209.                         break;
  210.                 }
  211.         }
  212.         *value = p->speed;
  213.         return p->bits;
  214. }
  215. static int cs4231_encoding_to_bits(struct sparcaudio_driver *drv, int value)
  216. {
  217.         int set_bits;
  218.   
  219.         switch (value) {
  220.         case AUDIO_ENCODING_ULAW:
  221.                 set_bits = CS4231_DFR_ULAW;
  222.                 break;
  223.         case AUDIO_ENCODING_ALAW:
  224.                 set_bits = CS4231_DFR_ALAW;
  225.                 break;
  226.         case AUDIO_ENCODING_DVI:
  227.                 set_bits = CS4231_DFR_ADPCM;
  228.                 break;
  229.         case AUDIO_ENCODING_LINEARLE:
  230.                 set_bits = CS4231_DFR_LINEARLE;
  231.                 break;
  232.         case AUDIO_ENCODING_LINEAR:
  233.                 set_bits = CS4231_DFR_LINEARBE;
  234.                 break;
  235.         case AUDIO_ENCODING_LINEAR8:
  236.                 set_bits = CS4231_DFR_LINEAR8;
  237.                 break;
  238.         default:
  239.                 set_bits = -EINVAL;
  240.                 break;
  241.         };
  242.   
  243.         return set_bits;
  244. }
  245. static int cs4231_set_output_encoding(struct sparcaudio_driver *drv, int value)
  246. {
  247.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  248.         int tmp_bits, set_bits;
  249.         tprintk(("output encoding %dn", value));
  250.         if (value != 0) {
  251.                 set_bits = cs4231_encoding_to_bits(drv, value);
  252.                 if (set_bits >= 0) {
  253.                         READ_IDR();
  254.                         READ_IDR();
  255.                         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
  256.                         tmp_bits = READ_IDR();
  257.                         WRITE_IDR(CHANGE_ENCODING(tmp_bits, set_bits));
  258.                         READ_IDR();
  259.                         READ_IDR();
  260.                         CHIP_READY();
  261.                         cs4231_chip->perchip_info.play.encoding = value;
  262.                         return 0;
  263.                 }
  264.         }
  265.         dprintk(("output enc failedn"));
  266.         return -EINVAL;
  267. }
  268. static int cs4231_get_output_encoding(struct sparcaudio_driver *drv)
  269. {
  270.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  271.       return cs4231_chip->perchip_info.play.encoding;
  272. }
  273. static int cs4231_set_input_encoding(struct sparcaudio_driver *drv, int value)
  274. {
  275.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  276.         int tmp_bits, set_bits;
  277.         tprintk(("input encoding %dn", value));
  278.         if (value != 0) {
  279.                 set_bits = cs4231_encoding_to_bits(drv, value);
  280.                 if (set_bits >= 0) {
  281.                         READ_IDR();
  282.                         READ_IDR();
  283.                         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
  284.                         tmp_bits = READ_IDR();
  285.                         WRITE_IDR(CHANGE_ENCODING(tmp_bits, set_bits));
  286.                         READ_IDR();
  287.                         READ_IDR();
  288.                         CHIP_READY();
  289.                         cs4231_chip->perchip_info.record.encoding = value;
  290.                         return 0;
  291.                 }
  292.         }
  293.         dprintk(("input enc failedn"));
  294.         return -EINVAL;
  295. }
  296. static int cs4231_get_input_encoding(struct sparcaudio_driver *drv)
  297. {
  298.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  299.       return cs4231_chip->perchip_info.record.encoding;
  300. }
  301. static int cs4231_set_output_rate(struct sparcaudio_driver *drv, int value)
  302. {
  303.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  304.         int tmp_bits, set_bits;
  305.         tprintk(("output rate %dn", value));
  306.         if (value != 0) {
  307.                 set_bits = cs4231_rate_to_bits(drv, &value);
  308.                 if (set_bits >= 0) {
  309.                         READ_IDR();
  310.                         READ_IDR();
  311.                         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
  312.                         tmp_bits = READ_IDR();
  313.                         WRITE_IDR(CHANGE_DFR(tmp_bits, set_bits));
  314.                         READ_IDR();
  315.                         READ_IDR();
  316.                         CHIP_READY();
  317.                         cs4231_chip->perchip_info.play.sample_rate = value;
  318.                         tprintk(("tmp_bits[%02x] set_bits[%02x] CHANGE_DFR[%02x]n",
  319.                                  tmp_bits, set_bits, CHANGE_DFR(tmp_bits, set_bits)));
  320.                         return 0;
  321.                 }
  322.         }
  323.         dprintk(("output rate failedn"));
  324.         return -EINVAL;
  325. }
  326. static int cs4231_get_output_rate(struct sparcaudio_driver *drv)
  327. {
  328.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  329.       return cs4231_chip->perchip_info.play.sample_rate;
  330. }
  331. static int cs4231_set_input_rate(struct sparcaudio_driver *drv, int value)
  332. {
  333.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  334.         int tmp_bits, set_bits;
  335.         tprintk(("input rate %dn", value));
  336.         if (value != 0) {
  337.                 set_bits = cs4231_rate_to_bits(drv, &value);
  338.                 if (set_bits >= 0) {
  339.                         READ_IDR();
  340.                         READ_IDR();
  341.                         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
  342.                         tmp_bits = READ_IDR();
  343.                         WRITE_IDR(CHANGE_DFR(tmp_bits, set_bits));
  344.                         READ_IDR();
  345.                         READ_IDR();
  346.                         CHIP_READY();
  347.                         cs4231_chip->perchip_info.record.sample_rate = value;
  348.                         return 0;
  349.                 }
  350.         }
  351.         dprintk(("input rate failedn"));
  352.         return -EINVAL;
  353. }
  354. static int cs4231_get_input_rate(struct sparcaudio_driver *drv)
  355. {
  356.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  357.       return cs4231_chip->perchip_info.record.sample_rate;
  358. }
  359. /* Generically we support 4 channels. This hardware does 2 */
  360. static int cs4231_set_input_channels(struct sparcaudio_driver *drv, int value)
  361. {
  362.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  363.         int tmp_bits;
  364.         tprintk(("input channels %dn", value));
  365.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
  366.         tmp_bits = READ_IDR();
  367.         switch (value) {
  368.         case 1:
  369.                 WRITE_IDR(CS4231_MONO_ON(tmp_bits));
  370.                 break;
  371.         case 2:
  372.                 WRITE_IDR(CS4231_STEREO_ON(tmp_bits));
  373.                 break;
  374.         default:
  375.                 dprintk(("input chan failedn"));
  376.                 return -EINVAL;
  377.         };
  378.         CHIP_READY();
  379.         cs4231_chip->perchip_info.record.channels = value;
  380.         return 0;
  381. }
  382. static int cs4231_get_input_channels(struct sparcaudio_driver *drv)
  383. {
  384.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  385.       return cs4231_chip->perchip_info.record.channels;
  386. }
  387. /* Generically we support 4 channels. This hardware does 2 */
  388. static int cs4231_set_output_channels(struct sparcaudio_driver *drv, int value)
  389. {
  390.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  391.         int tmp_bits;
  392.         tprintk(("output channels %dn", value));
  393.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
  394.         tmp_bits = READ_IDR();
  395.         switch (value) {
  396.         case 1:
  397.                 WRITE_IDR(CS4231_MONO_ON(tmp_bits));
  398.                 break;
  399.         case 2:
  400.                 WRITE_IDR(CS4231_STEREO_ON(tmp_bits));
  401.                 break;
  402.         default:
  403.                 dprintk(("output chan failedn"));
  404.                 return -EINVAL;
  405.         };
  406.         CHIP_READY();
  407.     
  408. cs4231_chip->perchip_info.play.channels = value;
  409.         return 0;
  410. }
  411. static int cs4231_get_output_channels(struct sparcaudio_driver *drv)
  412. {
  413.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  414.       return cs4231_chip->perchip_info.play.channels;
  415. }
  416. static int cs4231_get_input_precision(struct sparcaudio_driver *drv)
  417. {
  418.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  419.       return cs4231_chip->perchip_info.record.precision;
  420. }
  421. static int cs4231_get_output_precision(struct sparcaudio_driver *drv)
  422. {
  423.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  424.       return cs4231_chip->perchip_info.play.precision;
  425. }
  426. static int cs4231_set_input_precision(struct sparcaudio_driver *drv, int val)
  427. {
  428.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  429.       cs4231_chip->perchip_info.record.precision = val;
  430.       return cs4231_chip->perchip_info.record.precision;
  431. }
  432. static int cs4231_set_output_precision(struct sparcaudio_driver *drv, int val)
  433. {
  434.       struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private; 
  435.       cs4231_chip->perchip_info.play.precision = val;
  436.       return cs4231_chip->perchip_info.play.precision;
  437. }
  438. /* Wait until the auto calibration process has finished */
  439. static void cs4231_ready(struct sparcaudio_driver *drv) 
  440. {
  441.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  442.         unsigned int x;
  443.         WRITE_IAR(IAR_AUTOCAL_END);
  444.         x = 0;
  445.         do {
  446.                 if (READ_IDR() != IAR_NOT_READY)
  447.                         break;
  448.                 x++;
  449.         } while (x <= CS_TIMEOUT);
  450.         WRITE_IAR(0x0b);
  451.         x = 0;
  452.         do {
  453.                 if (READ_IDR() != AUTOCAL_IN_PROGRESS)
  454.                         break;
  455.                 x++;
  456.         } while (x <= CS_TIMEOUT);
  457. }
  458. /* Set output mute */
  459. static int cs4231_output_muted(struct sparcaudio_driver *drv, int value)
  460. {
  461.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  462.         tprintk(("in cs4231_output_muted: %dn", value));
  463.         if (!value) {
  464.                 WRITE_IAR(0x7);
  465.                 WRITE_IDR(READ_IDR() & OUTCR_UNMUTE);
  466.                 WRITE_IAR(0x6);
  467.                 WRITE_IDR(READ_IDR() & OUTCR_UNMUTE);
  468.                 cs4231_chip->perchip_info.output_muted = 0;
  469.         } else {
  470.                 WRITE_IAR(0x7);
  471.                 WRITE_IDR(READ_IDR() | OUTCR_MUTE);
  472.                 WRITE_IAR(0x6);
  473.                 WRITE_IDR(READ_IDR() | OUTCR_MUTE);
  474.                 cs4231_chip->perchip_info.output_muted = 1;
  475.         }
  476.         return 0;
  477. }
  478. static int cs4231_get_output_muted(struct sparcaudio_driver *drv)
  479. {
  480.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  481.         return cs4231_chip->perchip_info.output_muted;
  482. }
  483. static int cs4231_get_formats(struct sparcaudio_driver *drv)
  484. {
  485.         return (AFMT_MU_LAW | AFMT_A_LAW |
  486.                 AFMT_U8 | AFMT_IMA_ADPCM | 
  487.                 AFMT_S16_LE | AFMT_S16_BE);
  488. }
  489. static int cs4231_get_output_ports(struct sparcaudio_driver *drv)
  490. {
  491.         return (AUDIO_LINE_OUT | AUDIO_SPEAKER | AUDIO_HEADPHONE);
  492. }
  493. static int cs4231_get_input_ports(struct sparcaudio_driver *drv)
  494. {
  495.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  496.         /* This apparently applies only to APC ultras, not ebus ultras */
  497.         if (cs4231_chip->status & CS_STATUS_IS_ULTRA)
  498.                 return (AUDIO_LINE_IN | AUDIO_MICROPHONE | AUDIO_ANALOG_LOOPBACK);
  499.         else
  500.                 return (AUDIO_INTERNAL_CD_IN | AUDIO_LINE_IN |
  501.                         AUDIO_MICROPHONE | AUDIO_ANALOG_LOOPBACK);
  502. }
  503. /* Set chip "output" port */
  504. static int cs4231_set_output_port(struct sparcaudio_driver *drv, int value)
  505. {
  506.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  507.         int retval = 0;
  508.         tprintk(("output port: %dn", value));
  509.         /* Aaaaaah! It's all coming so fast! Turn it all off, then selectively
  510.          * enable things.
  511.          */
  512.         WRITE_IAR(0x1a);
  513.         WRITE_IDR(READ_IDR() | MONO_IOCR_MUTE);
  514.         WRITE_IAR(0x0a);
  515.         WRITE_IDR(READ_IDR() | PINCR_LINE_MUTE);
  516.         WRITE_IDR(READ_IDR() | PINCR_HDPH_MUTE);
  517.         if (value & AUDIO_SPEAKER) {
  518.                 WRITE_IAR(0x1a);
  519.                 WRITE_IDR(READ_IDR() & ~MONO_IOCR_MUTE);
  520.                 retval |= AUDIO_SPEAKER;
  521.         }
  522.         if (value & AUDIO_HEADPHONE) {
  523.                 WRITE_IAR(0x0a);
  524.                 WRITE_IDR(READ_IDR() & ~PINCR_HDPH_MUTE);
  525.                 retval |= AUDIO_HEADPHONE;
  526.         }
  527.         if (value & AUDIO_LINE_OUT) {
  528.                 WRITE_IAR(0x0a);
  529.                 WRITE_IDR(READ_IDR() & ~PINCR_LINE_MUTE);
  530.                 retval |= AUDIO_LINE_OUT;
  531.         }
  532.   
  533.         cs4231_chip->perchip_info.play.port = retval;
  534.         return (retval);
  535. }
  536. static int cs4231_get_output_port(struct sparcaudio_driver *drv)
  537. {
  538.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  539.         return cs4231_chip->perchip_info.play.port;
  540. }
  541. /* Set chip "input" port */
  542. static int cs4231_set_input_port(struct sparcaudio_driver *drv, int value)
  543. {
  544.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  545.         int retval = 0;
  546.         tprintk(("input port: %dn", value));
  547.         /* You can have one and only one. This is probably wrong, but
  548.          * appears to be how SunOS is doing it. Should be able to mix.
  549.          * More work to be done. CD input mixable, analog loopback may be.
  550.          */
  551.         /* Ultra systems do not support AUDIO_INTERNAL_CD_IN */
  552.         /* This apparently applies only to APC ultras, not ebus ultras */
  553.         if (!(cs4231_chip->status & CS_STATUS_IS_ULTRA)) {
  554.                 if (value & AUDIO_INTERNAL_CD_IN) {
  555.                         WRITE_IAR(0x1);
  556.                         WRITE_IDR(CDROM_ENABLE(READ_IDR()));
  557.                         WRITE_IAR(0x0);
  558.                         WRITE_IDR(CDROM_ENABLE(READ_IDR()));
  559.                         retval = AUDIO_INTERNAL_CD_IN;
  560.                 }
  561.         }
  562.         if ((value & AUDIO_LINE_IN)) {
  563.                 WRITE_IAR(0x1);
  564.                 WRITE_IDR(LINE_ENABLE(READ_IDR()));
  565.                 WRITE_IAR(0x0);
  566.                 WRITE_IDR(LINE_ENABLE(READ_IDR()));
  567.                 retval = AUDIO_LINE_IN;
  568.         } else if (value & AUDIO_MICROPHONE) {
  569.                 WRITE_IAR(0x1);
  570.                 WRITE_IDR(MIC_ENABLE(READ_IDR()));
  571.                 WRITE_IAR(0x0);
  572.                 WRITE_IDR(MIC_ENABLE(READ_IDR()));
  573.                 retval = AUDIO_MICROPHONE;
  574.         } else if (value & AUDIO_ANALOG_LOOPBACK) {
  575.                 WRITE_IAR(0x1);
  576.                 WRITE_IDR(OUTPUTLOOP_ENABLE(READ_IDR()));
  577.                 WRITE_IAR(0x0);
  578.                 WRITE_IDR(OUTPUTLOOP_ENABLE(READ_IDR()));
  579.                 retval = AUDIO_ANALOG_LOOPBACK;
  580.         }
  581.         cs4231_chip->perchip_info.record.port = retval;
  582.         return retval;
  583. }
  584. static int cs4231_get_input_port(struct sparcaudio_driver *drv)
  585. {
  586.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  587.         return cs4231_chip->perchip_info.record.port;
  588. }
  589. /* Set chip "monitor" gain */
  590. static int cs4231_set_monitor_volume(struct sparcaudio_driver *drv, int value)
  591. {
  592.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  593.         int a = 0;
  594.         tprintk(("monitor gain: %dn", value));
  595.         /* This interpolation really sucks. The question is, be compatible 
  596.          * with ScumOS/Sloaris or not?
  597.          */
  598.         a = CS4231_MON_MAX_ATEN - (value * (CS4231_MON_MAX_ATEN + 1) / 
  599.                                    (AUDIO_MAX_GAIN + 1));
  600.         WRITE_IAR(0x0d);
  601.         if (a >= CS4231_MON_MAX_ATEN) 
  602.                 WRITE_IDR(LOOPB_OFF);
  603.         else 
  604.                 WRITE_IDR((a << 2) | LOOPB_ON);
  605.         if (value == AUDIO_MAX_GAIN) 
  606.                 cs4231_chip->perchip_info.monitor_gain = AUDIO_MAX_GAIN;
  607.         else 
  608.                 cs4231_chip->perchip_info.monitor_gain =
  609.                         ((CS4231_MAX_DEV_ATEN - a) * 
  610.                          (AUDIO_MAX_GAIN + 1) / 
  611.                          (CS4231_MAX_DEV_ATEN + 1));
  612.         return 0;
  613. }
  614. static int cs4231_get_monitor_volume(struct sparcaudio_driver *drv)
  615. {
  616.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  617.         return (int) cs4231_chip->perchip_info.monitor_gain;
  618. }
  619. static int cs4231_get_output_error(struct sparcaudio_driver *drv)
  620. {
  621.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  622.         return (int) cs4231_chip->perchip_info.play.error;
  623. }
  624. static int cs4231_get_input_error(struct sparcaudio_driver *drv)
  625. {
  626.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  627.         return (int) cs4231_chip->perchip_info.record.error;
  628. }
  629. #ifdef EB4231_SUPPORT
  630. static int eb4231_get_output_samples(struct sparcaudio_driver *drv)
  631. {
  632.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  633.         u32 dbcr = readl(cs4231_chip->eb2p + EBDMA_COUNT);
  634.         int count =
  635.                 cs4231_length_to_samplecount(&cs4231_chip->perchip_info.play, dbcr);
  636.         return (cs4231_chip->perchip_info.play.samples - 
  637.                 ((count > cs4231_chip->perchip_info.play.samples) 
  638.                  ? 0 : count));
  639. }
  640. static int eb4231_get_input_samples(struct sparcaudio_driver *drv)
  641. {
  642.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  643.         u32 dbcr = readl(cs4231_chip->eb2c + EBDMA_COUNT);
  644.         int count =
  645.                 cs4231_length_to_samplecount(&cs4231_chip->perchip_info.record, dbcr);
  646.         return (cs4231_chip->perchip_info.record.samples - 
  647.                 ((count > cs4231_chip->perchip_info.record.samples) ?
  648.                 0 : count));
  649. }
  650. #endif
  651. static int cs4231_get_output_samples(struct sparcaudio_driver *drv)
  652. {
  653.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  654.         u32 dmapc = sbus_readl(cs4231_chip->regs + APCPC);
  655.         int count = 
  656.           cs4231_length_to_samplecount(&cs4231_chip->perchip_info.play, dmapc);
  657.         return (cs4231_chip->perchip_info.play.samples - 
  658.                 ((count > cs4231_chip->perchip_info.play.samples) 
  659.                  ? 0 : count));
  660. }
  661. static int cs4231_get_input_samples(struct sparcaudio_driver *drv)
  662. {
  663.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  664.         u32 dmacc = sbus_readl(cs4231_chip->regs + APCCC);
  665.         int count = 
  666.           cs4231_length_to_samplecount(&cs4231_chip->perchip_info.record, dmacc);
  667.         return (cs4231_chip->perchip_info.record.samples - 
  668.                 ((count > cs4231_chip->perchip_info.record.samples) ?
  669.                 0 : count));
  670. }
  671. static int cs4231_get_output_pause(struct sparcaudio_driver *drv)
  672. {
  673.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  674.         return (int) cs4231_chip->perchip_info.play.pause;
  675. }
  676. static int cs4231_get_input_pause(struct sparcaudio_driver *drv)
  677. {
  678.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  679.         return (int) cs4231_chip->perchip_info.record.pause;
  680. }
  681. /* But for play/record we have these cheesy jacket routines because of 
  682.  * how this crap gets set.
  683.  */
  684. static int cs4231_set_input_volume(struct sparcaudio_driver *drv, int value)
  685. {
  686.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  687. cs4231_record_gain(drv, value, 
  688.                            cs4231_chip->perchip_info.record.balance);
  689.         return 0;
  690. }
  691. static int cs4231_get_input_volume(struct sparcaudio_driver *drv)
  692. {
  693.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  694.         return (int) cs4231_chip->perchip_info.record.gain;
  695. }
  696. static int cs4231_set_output_volume(struct sparcaudio_driver *drv, int value)
  697. {
  698.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  699. cs4231_play_gain(drv, value, cs4231_chip->perchip_info.play.balance);
  700.         return 0;
  701. }
  702. static int cs4231_get_output_volume(struct sparcaudio_driver *drv)
  703. {
  704.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  705.         return cs4231_chip->perchip_info.play.gain;
  706. }
  707. /* Likewise for balance */
  708. static int cs4231_set_input_balance(struct sparcaudio_driver *drv, int value)
  709. {
  710.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  711. cs4231_chip->perchip_info.record.balance = value;
  712. cs4231_record_gain(drv, cs4231_chip->perchip_info.record.gain, 
  713.                            cs4231_chip->perchip_info.record.balance);
  714.         return 0;
  715. }
  716. static int cs4231_get_input_balance(struct sparcaudio_driver *drv)
  717. {
  718.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  719.         return (int) cs4231_chip->perchip_info.record.balance;
  720. }
  721. static int cs4231_set_output_balance(struct sparcaudio_driver *drv, int value)
  722. {
  723.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  724. cs4231_chip->perchip_info.play.balance = value;
  725. cs4231_play_gain(drv, cs4231_chip->perchip_info.play.gain, 
  726.                          cs4231_chip->perchip_info.play.balance);
  727.         return 0;
  728. }
  729. static int cs4231_get_output_balance(struct sparcaudio_driver *drv)
  730. {
  731.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  732.         return (int) cs4231_chip->perchip_info.play.balance;
  733. }
  734. /* Set chip record gain */
  735. static int cs4231_record_gain(struct sparcaudio_driver *drv, int value,
  736.                               unsigned char balance)
  737. {
  738.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  739.         int tmp = 0, r, l, r_adj, l_adj;
  740.         unsigned char old_gain;
  741.         r = l = value;
  742.         if (balance < AUDIO_MID_BALANCE) {
  743.                 r = (int) (value -
  744.                            ((AUDIO_MID_BALANCE - balance) << AUDIO_BALANCE_SHIFT));
  745.                 if (r < 0)
  746.                         r = 0;
  747.         } else if (balance > AUDIO_MID_BALANCE) {
  748.                 l = (int) (value -
  749.                            ((balance - AUDIO_MID_BALANCE) << AUDIO_BALANCE_SHIFT));
  750.                 if (l < 0)
  751.                         l = 0;
  752.         }
  753.         l_adj = l * (CS4231_MAX_GAIN + 1) / (AUDIO_MAX_GAIN + 1);
  754.         r_adj = r * (CS4231_MAX_GAIN + 1) / (AUDIO_MAX_GAIN + 1);
  755.   
  756.         WRITE_IAR(0x0);
  757.         old_gain = READ_IDR();
  758.         WRITE_IDR(RECGAIN_SET(old_gain, l_adj));
  759.         WRITE_IAR(0x1);
  760.         old_gain = READ_IDR();
  761.         WRITE_IDR(RECGAIN_SET(old_gain, r_adj));
  762.   
  763.         if (l == value) {
  764.                 (l == 0) ? (tmp = 0) : (tmp = ((l_adj + 1) * AUDIO_MAX_GAIN) / 
  765.                                         (CS4231_MAX_GAIN + 1));
  766.         } else if (r == value) {
  767.                 (r == 0) ? (tmp = 0) : (tmp = ((r_adj + 1) * AUDIO_MAX_GAIN) / 
  768.                                         (CS4231_MAX_GAIN + 1));
  769.         }
  770.         cs4231_chip->perchip_info.record.gain = tmp;
  771.         return 0;
  772. }
  773. /* Set chip play gain */
  774. static int cs4231_play_gain(struct sparcaudio_driver *drv, int value,
  775.                             unsigned char balance)
  776. {
  777.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  778.         int tmp = 0, r, l, r_adj, l_adj;
  779.         unsigned char old_gain;
  780.         tprintk(("in play_gain: %d %cn", value, balance));
  781.         r = l = value;
  782.         if (balance < AUDIO_MID_BALANCE) {
  783.                 r = (int) (value -
  784.                            ((AUDIO_MID_BALANCE - balance) << AUDIO_BALANCE_SHIFT));
  785.                 if (r < 0)
  786.                         r = 0;
  787.         } else if (balance > AUDIO_MID_BALANCE) {
  788.                 l = (int) (value -
  789.                            ((balance - AUDIO_MID_BALANCE) << AUDIO_BALANCE_SHIFT));
  790.                 if (l < 0)
  791.                         l = 0;
  792.         }
  793.         (l == 0) ? (l_adj = CS4231_MAX_DEV_ATEN) : (l_adj = CS4231_MAX_ATEN - 
  794.                                                     (l * (CS4231_MAX_ATEN + 1) / 
  795.                                                      (AUDIO_MAX_GAIN + 1)));
  796.         (r == 0) ? (r_adj = CS4231_MAX_DEV_ATEN) : (r_adj = CS4231_MAX_ATEN -
  797.                                                     (r * (CS4231_MAX_ATEN + 1) /
  798.                                                      (AUDIO_MAX_GAIN + 1)));
  799.   
  800.         WRITE_IAR(0x6);
  801.         old_gain = READ_IDR();
  802.         WRITE_IDR(GAIN_SET(old_gain, l_adj));
  803.         WRITE_IAR(0x7);
  804.         old_gain = READ_IDR();
  805.         WRITE_IDR(GAIN_SET(old_gain, r_adj));
  806.   
  807.         if ((value == 0) || (value == AUDIO_MAX_GAIN)) {
  808.                 tmp = value;
  809.         } else {
  810.                 if (value == l) {
  811.                         tmp = ((CS4231_MAX_ATEN - l_adj) * (AUDIO_MAX_GAIN + 1) / 
  812.                                (CS4231_MAX_ATEN + 1));
  813.                 } else if (value == r) {
  814.                         tmp = ((CS4231_MAX_ATEN - r_adj) * (AUDIO_MAX_GAIN + 1) / 
  815.                                (CS4231_MAX_ATEN + 1));
  816.                 }
  817.         }
  818.         cs4231_chip->perchip_info.play.gain = tmp;
  819.         return 0;
  820. }
  821. /* Reset the audio chip to a sane state. */
  822. static void cs4231_chip_reset(struct sparcaudio_driver *drv)
  823. {
  824.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  825.         unsigned char vers;
  826.         tprintk(("in cs4231_chip_resetn"));
  827.         if (cs4231_chip->status & CS_STATUS_IS_EBUS) {
  828. #ifdef EB4231_SUPPORT
  829.                 writel(EBUS_DCSR_RESET, cs4231_chip->eb2p + EBDMA_CSR);
  830.                 writel(EBUS_DCSR_RESET, cs4231_chip->eb2c + EBDMA_CSR);
  831.                 writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2p + EBDMA_CSR);
  832.                 writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2c + EBDMA_CSR);
  833. #endif
  834.         } else {
  835.                 u32 tmp;
  836.                 sbus_writel(APC_CHIP_RESET, cs4231_chip->regs + APCCSR);
  837.                 sbus_writel(0x00, cs4231_chip->regs + APCCSR);
  838.                 tmp = sbus_readl(cs4231_chip->regs + APCCSR);
  839.                 tmp |= APC_CDC_RESET;
  840.                 sbus_writel(tmp, cs4231_chip->regs + APCCSR);
  841.   
  842.                 udelay(20);
  843.   
  844.                 tmp = sbus_readl(cs4231_chip->regs + APCCSR);
  845.                 tmp &= ~(APC_CDC_RESET);
  846.                 sbus_writel(tmp, cs4231_chip->regs + APCCSR);
  847.         }
  848.         WRITE_IAR(READ_IAR() | IAR_AUTOCAL_BEGIN);
  849.         CHIP_READY();
  850.     
  851.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x0c);
  852.         WRITE_IDR(MISC_IR_MODE2);
  853.         /* This is the equivalent of DEFAULT_DATA_FMAT */
  854.         cs4231_set_input_encoding(drv, AUDIO_ENCODING_ULAW);
  855.         cs4231_set_input_rate(drv, CS4231_RATE);
  856.         cs4231_set_input_channels(drv, CS4231_CHANNELS);
  857.         cs4231_set_input_precision(drv, CS4231_PRECISION);
  858.         cs4231_set_output_encoding(drv, AUDIO_ENCODING_ULAW);
  859.         cs4231_set_output_rate(drv, CS4231_RATE);
  860.         cs4231_set_output_channels(drv, CS4231_CHANNELS);
  861.         cs4231_set_output_precision(drv, CS4231_PRECISION);
  862.         WRITE_IAR(0x19);
  863.         /* see what we can turn on */
  864.         vers = READ_IDR();
  865.         if (vers & CS4231A) {
  866.                 tprintk(("This is a CS4231An"));
  867.                 cs4231_chip->status |= CS_STATUS_REV_A;
  868.         } else {
  869.                 cs4231_chip->status &= ~CS_STATUS_REV_A;
  870.         }
  871.   
  872.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x10);
  873.         WRITE_IDR(OLB_ENABLE);
  874.   
  875.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x11);
  876.         if (cs4231_chip->status & CS_STATUS_REV_A)
  877.                 WRITE_IDR(HPF_ON | XTALE_ON);
  878.         else
  879.                 WRITE_IDR(HPF_ON);
  880.   
  881.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1a);
  882.         WRITE_IDR(0x00);
  883.   
  884.         /* Now set things up for defaults */
  885.         cs4231_set_input_balance(drv, AUDIO_MID_BALANCE);
  886.         cs4231_set_output_balance(drv, AUDIO_MID_BALANCE);
  887.         cs4231_set_input_volume(drv, CS4231_DEFAULT_RECGAIN);
  888.         cs4231_set_output_volume(drv, CS4231_DEFAULT_PLAYGAIN);
  889.         cs4231_set_input_port(drv, AUDIO_MICROPHONE);
  890.         cs4231_set_output_port(drv, AUDIO_SPEAKER);
  891.         cs4231_set_monitor_volume(drv, LOOPB_OFF);
  892.   
  893.         WRITE_IAR(IAR_AUTOCAL_END);
  894.   
  895.         cs4231_ready(drv);
  896.   
  897.         WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x09);
  898.         WRITE_IDR(READ_IDR() & ACAL_DISABLE);
  899.         WRITE_IAR(IAR_AUTOCAL_END);
  900.   
  901.         cs4231_ready(drv);
  902.         cs4231_output_muted(drv, 0);
  903.         cs4231_chip->recording_count = 0;
  904.         cs4231_chip->input_next_dma_handle = 0;
  905.         cs4231_chip->input_dma_handle = 0;
  906.         cs4231_chip->input_next_dma_size = 0;
  907.         cs4231_chip->input_dma_size = 0;
  908.         cs4231_chip->playing_count = 0;
  909.         cs4231_chip->output_next_dma_handle = 0;
  910.         cs4231_chip->output_dma_handle = 0;
  911.         cs4231_chip->output_next_dma_size = 0;
  912.         cs4231_chip->output_dma_size = 0;
  913. }
  914. static int
  915. cs4231_length_to_samplecount(struct audio_prinfo *thisdir, unsigned int length)
  916. {
  917.         unsigned int count;
  918.         if (thisdir->channels == 2)
  919.                 count = (length / 2);
  920.         else 
  921.                 count = length;
  922.   
  923.         if (thisdir->encoding == AUDIO_ENCODING_LINEAR)
  924.                 count = (count / 2);
  925.         else if (thisdir->encoding == AUDIO_ENCODING_DVI)
  926.                 count = (count / 4);
  927.   
  928.         return count;
  929. }
  930. #ifdef EB4231_SUPPORT
  931. static void eb4231_getsamplecount(struct sparcaudio_driver *drv,
  932.                                   unsigned int length,
  933.                                   unsigned int direction)
  934. {
  935.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  936.         struct audio_prinfo *thisdir;
  937.         unsigned int count, curcount, nextcount, dbcr;
  938.         if(direction == 1) {
  939.                 thisdir = &cs4231_chip->perchip_info.record;
  940.                 dbcr = readl(cs4231_chip->eb2c + EBDMA_COUNT);
  941.                 nextcount = cs4231_chip->input_next_dma_size;
  942.         } else {
  943.                 thisdir = &cs4231_chip->perchip_info.play;
  944.                 dbcr = readl(cs4231_chip->eb2p + EBDMA_COUNT);
  945.                 nextcount = cs4231_chip->output_next_dma_size;
  946.         }
  947.         curcount = cs4231_length_to_samplecount(thisdir, dbcr);
  948.         count = thisdir->samples;
  949.         length = cs4231_length_to_samplecount(thisdir, length);
  950.         /* normalize for where we are. */
  951.         thisdir->samples = ((count - nextcount) + (length - curcount));
  952. }
  953. #endif
  954. static void cs4231_getsamplecount(struct sparcaudio_driver *drv,
  955.                                   unsigned int length,
  956.                                   unsigned int direction)
  957. {
  958.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  959.         struct audio_prinfo *thisdir;
  960.         unsigned int count, nextcount, curcount;
  961.         u32 tmp;
  962.         if (direction == 1) {
  963.                 /* record */ 
  964.                 thisdir = &cs4231_chip->perchip_info.record;
  965.                 tmp = sbus_readl(cs4231_chip->regs + APCCC);
  966.                 curcount = cs4231_length_to_samplecount(thisdir, tmp);
  967.                 tmp = sbus_readl(cs4231_chip->regs + APCCNC);
  968.                 nextcount = cs4231_length_to_samplecount(thisdir, tmp);
  969.         } else {
  970.                 /* play */
  971.                 thisdir = &cs4231_chip->perchip_info.play;
  972.                 tmp = sbus_readl(cs4231_chip->regs + APCPC);
  973.                 curcount = cs4231_length_to_samplecount(thisdir, tmp);
  974.                 tmp = sbus_readl(cs4231_chip->regs + APCPNC);
  975.                 nextcount = cs4231_length_to_samplecount(thisdir, tmp);
  976.         }
  977.         count = thisdir->samples;
  978.         length = cs4231_length_to_samplecount(thisdir, length);
  979.         /* normalize for where we are. */
  980.         thisdir->samples = ((count - nextcount) + (length - curcount));
  981. }
  982. static int cs4231_open(struct inode * inode, struct file * file, struct sparcaudio_driver *drv)
  983. {
  984.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  985.         /* Set the default audio parameters if not already in use. */
  986.         if (file->f_mode & FMODE_WRITE) {
  987.                 if (!(drv->flags & SDF_OPEN_WRITE) && 
  988.                     (cs4231_chip->perchip_info.play.active == 0)) {
  989.                         cs4231_chip->perchip_info.play.open = 1;
  990.                         cs4231_chip->perchip_info.play.samples =
  991.                                 cs4231_chip->perchip_info.play.error = 0;
  992.                 }
  993.         }
  994.         if (file->f_mode & FMODE_READ) {
  995.                 if (!(drv->flags & SDF_OPEN_READ) && 
  996.                     (cs4231_chip->perchip_info.record.active == 0)) {
  997.                         cs4231_chip->perchip_info.record.open = 1;
  998.                         cs4231_chip->perchip_info.record.samples = 
  999.                                 cs4231_chip->perchip_info.record.error = 0;
  1000.                 }
  1001.         }  
  1002.         cs4231_ready(drv);
  1003.         CHIP_READY();
  1004.     
  1005.         MOD_INC_USE_COUNT;
  1006.   
  1007.         return 0;
  1008. }
  1009. static void cs4231_release(struct inode * inode, struct file * file, struct sparcaudio_driver *drv)
  1010. {
  1011. struct cs4231_chip *cs4231_chip = (struct cs4231_chip *)drv->private;
  1012. void (*dma_unmap_single)(struct sbus_dev *, dma_addr_t, size_t, int) = sbus_unmap_single;
  1013. #ifdef EB4231_SUPPORT
  1014. if (cs4231_chip->status & CS_STATUS_IS_EBUS)
  1015. dma_unmap_single = (void (*)(struct sbus_dev *, dma_addr_t, size_t, int)) pci_unmap_single;
  1016. #endif
  1017.         /* zero out any info about what data we have as well */
  1018.         if (file->f_mode & FMODE_READ) {
  1019.                 /* stop capture here or midlevel? */
  1020.                 cs4231_chip->perchip_info.record.open = 0;
  1021.                 if (cs4231_chip->input_dma_handle) {
  1022. dma_unmap_single(drv->dev,
  1023.  cs4231_chip->input_dma_handle,
  1024.  cs4231_chip->input_dma_size,
  1025.  SBUS_DMA_FROMDEVICE);
  1026.                         cs4231_chip->input_dma_handle = 0;
  1027.                         cs4231_chip->input_dma_size = 0;
  1028.                 }
  1029.                 if (cs4231_chip->input_next_dma_handle) {
  1030. dma_unmap_single(drv->dev,
  1031.  cs4231_chip->input_next_dma_handle,
  1032.  cs4231_chip->input_next_dma_size,
  1033.  SBUS_DMA_FROMDEVICE);
  1034.                         cs4231_chip->input_next_dma_handle = 0;
  1035.                         cs4231_chip->input_next_dma_size = 0;
  1036.                 }
  1037.         }
  1038.         if (file->f_mode & FMODE_WRITE) {
  1039.                 cs4231_chip->perchip_info.play.active =
  1040.                         cs4231_chip->perchip_info.play.open = 0;
  1041.                 if (cs4231_chip->output_dma_handle) {
  1042. dma_unmap_single(drv->dev,
  1043.  cs4231_chip->output_dma_handle,
  1044.  cs4231_chip->output_dma_size,
  1045.  SBUS_DMA_TODEVICE);
  1046.                         cs4231_chip->output_dma_handle = 0;
  1047.                         cs4231_chip->output_dma_size = 0;
  1048.                 }
  1049.                 if (cs4231_chip->output_next_dma_handle) {
  1050. dma_unmap_single(drv->dev,
  1051.  cs4231_chip->output_next_dma_handle,
  1052.  cs4231_chip->output_next_dma_size,
  1053.  SBUS_DMA_TODEVICE);
  1054.                         cs4231_chip->output_next_dma_handle = 0;
  1055.                         cs4231_chip->output_next_dma_size = 0;
  1056.                 }
  1057.         }
  1058.         if (!cs4231_chip->perchip_info.play.open && 
  1059.             !cs4231_chip->perchip_info.record.open && 
  1060.             (cs4231_chip->status & CS_STATUS_INIT_ON_CLOSE)) {
  1061.                 cs4231_chip_reset(drv);
  1062.                 cs4231_chip->status &= ~CS_STATUS_INIT_ON_CLOSE;
  1063.         }
  1064.         MOD_DEC_USE_COUNT;
  1065. }
  1066. static void cs4231_playintr(struct sparcaudio_driver *drv, int push)
  1067. {
  1068.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1069.         int status = 0;
  1070.         if (!push) {
  1071.                 if (!cs4231_chip->perchip_info.play.active) {
  1072.                         sbus_writel(cs4231_chip->output_next_dma_handle,
  1073.                                     cs4231_chip->regs + APCPNVA);
  1074.                         sbus_writel(cs4231_chip->output_next_dma_size,
  1075.                                     cs4231_chip->regs + APCPNC);
  1076.                 }
  1077.                 sparcaudio_output_done(drv, 0);
  1078.                 return;
  1079.         }
  1080.         if (cs4231_chip->playlen == 0 && cs4231_chip->output_size > 0) 
  1081.                 cs4231_chip->playlen = cs4231_chip->output_size;
  1082.         if (cs4231_chip->output_dma_handle) {
  1083.                 sbus_unmap_single(drv->dev,
  1084.                                   cs4231_chip->output_dma_handle,
  1085.                                   cs4231_chip->output_dma_size,
  1086.                                   SBUS_DMA_TODEVICE);
  1087.                 cs4231_chip->output_dma_handle = 0;
  1088.                 cs4231_chip->output_dma_size = 0;
  1089.                 cs4231_chip->playing_count--;
  1090.                 status++;
  1091.         }
  1092.         if (cs4231_chip->output_next_dma_handle) {
  1093.                 cs4231_chip->output_dma_handle = cs4231_chip->output_next_dma_handle;
  1094.                 cs4231_chip->output_dma_size = cs4231_chip->output_next_dma_size;
  1095.                 cs4231_chip->output_next_dma_size = 0;
  1096.                 cs4231_chip->output_next_dma_handle = 0;
  1097.         }
  1098.         if ((cs4231_chip->output_ptr && cs4231_chip->output_size > 0) && 
  1099.             !(cs4231_chip->perchip_info.play.pause)) {
  1100.                 cs4231_chip->output_next_dma_handle =
  1101.                         sbus_map_single(drv->dev,
  1102.                                         (char *)cs4231_chip->output_ptr,
  1103.                                         cs4231_chip->output_size,
  1104.                                         SBUS_DMA_TODEVICE);
  1105.                 cs4231_chip->output_next_dma_size = cs4231_chip->output_size;
  1106.                 sbus_writel(cs4231_chip->output_next_dma_handle,
  1107.                             cs4231_chip->regs + APCPNVA);
  1108.                 sbus_writel(cs4231_chip->output_next_dma_size,
  1109.                             cs4231_chip->regs + APCPNC);
  1110.                 cs4231_chip->output_size = 0;
  1111.                 cs4231_chip->output_ptr = NULL;
  1112.                 cs4231_chip->playing_count++;
  1113.                 status += 2;
  1114.         } else {
  1115.                 sbus_writel(0, cs4231_chip->regs + APCPNVA);
  1116.                 sbus_writel(0, cs4231_chip->regs + APCPNC);
  1117.         }
  1118.         sparcaudio_output_done(drv, status);
  1119. }
  1120. #ifdef EB4231_SUPPORT
  1121. static void eb4231_playintr(struct sparcaudio_driver *drv)
  1122. {
  1123.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1124.         int status = 0;
  1125.         if (cs4231_chip->playlen == 0 && cs4231_chip->output_size > 0) 
  1126.                 cs4231_chip->playlen = cs4231_chip->output_size;
  1127.         if (cs4231_chip->output_dma_handle) {
  1128. pci_unmap_single((struct pci_dev *)drv->dev,
  1129.  cs4231_chip->output_dma_handle,
  1130.  cs4231_chip->output_dma_size,
  1131.                                  PCI_DMA_TODEVICE);
  1132.                 cs4231_chip->output_dma_handle = 0;
  1133.                 cs4231_chip->output_dma_size = 0;
  1134.                 cs4231_chip->playing_count--;
  1135.                 status++;
  1136.         }
  1137.         if(cs4231_chip->output_next_dma_handle) {
  1138.                 cs4231_chip->output_dma_handle = cs4231_chip->output_next_dma_handle;
  1139.                 cs4231_chip->output_dma_size = cs4231_chip->output_next_dma_size;
  1140.                 cs4231_chip->output_next_dma_handle = 0;
  1141.                 cs4231_chip->output_next_dma_size = 0;
  1142.         }
  1143.         if ((cs4231_chip->output_ptr && cs4231_chip->output_size > 0) && 
  1144.             !(cs4231_chip->perchip_info.play.pause)) {
  1145.                 cs4231_chip->output_next_dma_handle =
  1146. pci_map_single((struct pci_dev *)drv->dev,
  1147.        (char *)cs4231_chip->output_ptr,
  1148.        cs4231_chip->output_size,
  1149.                                        PCI_DMA_TODEVICE);
  1150.                 cs4231_chip->output_next_dma_size = cs4231_chip->output_size;
  1151.                 writel(cs4231_chip->output_next_dma_size,
  1152.                        cs4231_chip->eb2p + EBDMA_COUNT);
  1153.                 writel(cs4231_chip->output_next_dma_handle,
  1154.                        cs4231_chip->eb2p + EBDMA_ADDR);
  1155.                 cs4231_chip->output_size = 0;
  1156.                 cs4231_chip->output_ptr = NULL;
  1157.                 cs4231_chip->playing_count++;
  1158.                 status += 2;
  1159.         }
  1160.         sparcaudio_output_done(drv, status);
  1161. }
  1162. #endif
  1163. static void cs4231_recclear(int fmt, char *dmabuf, int length)
  1164. {
  1165.         switch (fmt) {
  1166.         case AUDIO_ENCODING_LINEAR:
  1167.                 memset(dmabuf, 0x00, length);
  1168.                 break;
  1169.         case AUDIO_ENCODING_ALAW:
  1170.                 memset(dmabuf, 0xd5, length);
  1171.                 break;
  1172.         case AUDIO_ENCODING_ULAW:
  1173.                 memset(dmabuf, 0xff, length);
  1174.                 break;
  1175.         }
  1176. }
  1177. static int cs4231_recintr(struct sparcaudio_driver *drv)
  1178. {
  1179.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1180.         int status = 0;
  1181.         if (cs4231_chip->perchip_info.record.active == 0) {
  1182.                 dprintk(("going inactiven"));
  1183.                 cs4231_pollinput(drv);
  1184.                 cs4231_disable_rec(drv);    
  1185.         } 
  1186.         if (cs4231_chip->input_dma_handle) {
  1187.                 sbus_unmap_single(drv->dev,
  1188.                                   cs4231_chip->input_dma_handle,
  1189.                                   cs4231_chip->input_dma_size,
  1190.                                   SBUS_DMA_FROMDEVICE);
  1191.                 cs4231_chip->input_dma_handle = 0;
  1192.                 cs4231_chip->input_dma_size = 0;
  1193.                 cs4231_chip->recording_count--;
  1194.                 status++;
  1195.         }
  1196.         if (cs4231_chip->input_next_dma_handle) {
  1197.                 cs4231_chip->input_dma_handle = cs4231_chip->input_next_dma_handle;
  1198.                 cs4231_chip->input_dma_size = cs4231_chip->input_next_dma_size;
  1199.                 cs4231_chip->input_next_dma_size = 0;
  1200.                 cs4231_chip->input_next_dma_handle = 0;
  1201.         }
  1202.         if ((cs4231_chip->input_ptr && cs4231_chip->input_size > 0) && 
  1203.             !(cs4231_chip->perchip_info.record.pause)) {
  1204.                 cs4231_recclear(cs4231_chip->perchip_info.record.encoding, 
  1205.                                 (char *)cs4231_chip->input_ptr,
  1206.                                 cs4231_chip->input_size);
  1207.                 cs4231_chip->input_next_dma_handle =
  1208.                         sbus_map_single(drv->dev,
  1209.                                         (char *)cs4231_chip->input_ptr,
  1210.                                         cs4231_chip->input_size,
  1211.                                         SBUS_DMA_FROMDEVICE);
  1212.                 cs4231_chip->input_next_dma_size = cs4231_chip->input_size;
  1213.                 sbus_writel(cs4231_chip->input_next_dma_handle,
  1214.                             cs4231_chip->regs + APCCNVA);
  1215.                 sbus_writel(cs4231_chip->input_next_dma_size,
  1216.                             cs4231_chip->regs + APCCNC);
  1217.                 cs4231_chip->input_size = 0;
  1218.                 cs4231_chip->input_ptr = NULL;
  1219.                 cs4231_chip->recording_count++;
  1220.                 status += 2;
  1221.         } else {
  1222.                 sbus_writel(0, cs4231_chip->regs + APCCNVA);
  1223.                 sbus_writel(0, cs4231_chip->regs + APCCNC);
  1224.         }
  1225.         sparcaudio_input_done(drv, status);
  1226.         return 1;
  1227. }
  1228. #ifdef EB4231_SUPPORT
  1229. static int eb4231_recintr(struct sparcaudio_driver *drv)
  1230. {
  1231.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1232.         int status = 0;
  1233.         if (cs4231_chip->perchip_info.record.active == 0) {
  1234.                 dprintk(("going inactiven"));
  1235.                 eb4231_pollinput(drv);
  1236.                 cs4231_disable_rec(drv);    
  1237.         } 
  1238.         if (cs4231_chip->input_dma_handle) {
  1239. pci_unmap_single((struct pci_dev *)drv->dev,
  1240.  cs4231_chip->input_dma_handle,
  1241.  cs4231_chip->input_dma_size,
  1242.                                  PCI_DMA_FROMDEVICE);
  1243.                 cs4231_chip->input_dma_handle = 0;
  1244.                 cs4231_chip->input_dma_size = 0;
  1245.                 cs4231_chip->recording_count--;
  1246.                 status++;
  1247.         }
  1248.         if (cs4231_chip->input_next_dma_handle) {
  1249.                 cs4231_chip->input_dma_handle = cs4231_chip->input_next_dma_handle;
  1250.                 cs4231_chip->input_dma_size = cs4231_chip->input_next_dma_size;
  1251.                 cs4231_chip->input_next_dma_size = 0;
  1252.                 cs4231_chip->input_next_dma_handle = 0;
  1253.         }
  1254.         if ((cs4231_chip->input_ptr && cs4231_chip->input_size > 0) && 
  1255.             !(cs4231_chip->perchip_info.record.pause)) {
  1256.                 cs4231_recclear(cs4231_chip->perchip_info.record.encoding, 
  1257.                                 (char *)cs4231_chip->input_ptr,
  1258.                                 cs4231_chip->input_size);
  1259.                 cs4231_chip->input_next_dma_handle =
  1260. pci_map_single((struct pci_dev *)drv->dev,
  1261.        (char *)cs4231_chip->input_ptr,
  1262.        cs4231_chip->input_size,
  1263.                                        PCI_DMA_FROMDEVICE);
  1264.                 cs4231_chip->input_next_dma_size = cs4231_chip->input_size;
  1265.                 writel(cs4231_chip->input_next_dma_size,
  1266.                        cs4231_chip->eb2c + EBDMA_COUNT);
  1267.                 writel(cs4231_chip->input_next_dma_handle,
  1268.                        cs4231_chip->eb2c + EBDMA_ADDR);
  1269.                 cs4231_chip->input_size = 0;
  1270.                 cs4231_chip->input_ptr = NULL;
  1271.                 cs4231_chip->recording_count++;
  1272.                 status += 2;
  1273.         }
  1274.         sparcaudio_input_done(drv, status);
  1275.         return 1;
  1276. }
  1277. #endif
  1278. #ifdef EB4231_SUPPORT
  1279. static void eb4231_start_output(struct sparcaudio_driver *drv, __u8 * buffer,
  1280.                                 unsigned long count)
  1281. {
  1282.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1283.         u32 dcsr;
  1284.         cs4231_chip->output_ptr = buffer;
  1285.         cs4231_chip->output_size = count;
  1286.         if (cs4231_chip->perchip_info.play.active || 
  1287.             (cs4231_chip->perchip_info.play.pause))
  1288.                 return;
  1289.         cs4231_ready(drv);
  1290.         cs4231_chip->perchip_info.play.active = 1;
  1291.         cs4231_chip->playing_count = 0;
  1292.         dcsr = readl(cs4231_chip->eb2p + EBDMA_CSR);
  1293.         if (!(dcsr & EBUS_DCSR_EN_DMA)) {
  1294.                 writel(EBUS_DCSR_RESET, cs4231_chip->eb2p + EBDMA_CSR);
  1295.                 writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2p + EBDMA_CSR);
  1296.                 eb4231_playintr(drv);
  1297.                 writel(EBUS_DCSR_BURST_SZ_16 |
  1298.                        (EBUS_DCSR_EN_DMA | EBUS_DCSR_INT_EN |
  1299.                         EBUS_DCSR_EN_CNT | EBUS_DCSR_EN_NEXT),
  1300.                        cs4231_chip->eb2p + EBDMA_CSR);
  1301.                 cs4231_enable_play(drv);
  1302.                 cs4231_ready(drv);
  1303.         } else {
  1304.                 eb4231_playintr(drv);
  1305.         }
  1306. }
  1307. #endif
  1308. static void cs4231_start_output(struct sparcaudio_driver *drv, __u8 * buffer,
  1309.                                 unsigned long count)
  1310. {
  1311.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1312.         u32 csr;
  1313.         tprintk(("in 4231 start outputn"));
  1314.         cs4231_chip->output_ptr = buffer;
  1315.         cs4231_chip->output_size = count;
  1316.         if (cs4231_chip->perchip_info.play.active || 
  1317.             (cs4231_chip->perchip_info.play.pause))
  1318.                 return;
  1319.         cs4231_ready(drv);
  1320.         cs4231_chip->perchip_info.play.active = 1;
  1321.         cs4231_chip->playing_count = 0;
  1322.         csr = sbus_readl(cs4231_chip->regs + APCCSR);
  1323.         if ((csr & APC_PPAUSE) || !(csr & APC_PDMA_READY)) {
  1324.                 u32 pnva;
  1325.                 csr &= ~APC_XINT_PLAY;
  1326.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1327.                 csr &= ~APC_PPAUSE;
  1328.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1329.     
  1330.                 pnva = sbus_readl(cs4231_chip->regs + APCPNVA);
  1331.                 cs4231_playintr(drv, (pnva == 0) ? 1 : 0);
  1332.                 csr |= APC_PLAY_SETUP;
  1333.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1334.                 cs4231_enable_play(drv);
  1335.                 cs4231_ready(drv);
  1336.         }
  1337. }
  1338. #ifdef EB4231_SUPPORT
  1339. static void eb4231_stop_output(struct sparcaudio_driver *drv)
  1340. {
  1341.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1342.         u32 dcsr;
  1343.         dprintk(("eb4231_stop_output: dcsr 0x%x dacr 0x%x dbcr %dn",
  1344.                  readl(cs4231_chip->eb2p + EBDMA_CSR),
  1345.                  readl(cs4231_chip->eb2p + EBDMA_ADDR),
  1346.                  readl(cs4231_chip->eb2p + EBDMA_COUNT)));
  1347.         cs4231_chip->output_ptr = NULL;
  1348.         cs4231_chip->output_size = 0;
  1349.         if (cs4231_chip->output_dma_handle) {
  1350. pci_unmap_single((struct pci_dev *)drv->dev,
  1351.  cs4231_chip->output_dma_handle,
  1352.  cs4231_chip->output_dma_size,
  1353.                                  PCI_DMA_TODEVICE);
  1354.                 cs4231_chip->output_dma_handle = 0;
  1355.                 cs4231_chip->output_dma_size = 0;
  1356.         }
  1357.         if (cs4231_chip->output_next_dma_handle) {
  1358. pci_unmap_single((struct pci_dev *)drv->dev,
  1359.  cs4231_chip->output_next_dma_handle,
  1360.  cs4231_chip->output_next_dma_size,
  1361.                                  PCI_DMA_TODEVICE);
  1362.                 cs4231_chip->output_next_dma_handle = 0;
  1363.                 cs4231_chip->output_next_dma_size = 0;
  1364.         }
  1365.         dcsr = readl(cs4231_chip->eb2p + EBDMA_CSR);
  1366.         if(dcsr & EBUS_DCSR_EN_DMA)
  1367.                 writel(dcsr & ~EBUS_DCSR_EN_DMA,
  1368.                        cs4231_chip->eb2p + EBDMA_CSR);
  1369.         /* Else subsequent speed setting changes are ignored by the chip. */
  1370.         cs4231_disable_play(drv);
  1371. }
  1372. #endif
  1373. static void cs4231_stop_output(struct sparcaudio_driver *drv)
  1374. {
  1375.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1376.         tprintk(("in cs4231_stop_outputn"));
  1377.         cs4231_chip->output_ptr = NULL;
  1378.         cs4231_chip->output_size = 0;
  1379.         if (cs4231_chip->output_dma_handle) {
  1380.                 sbus_unmap_single(drv->dev,
  1381.                                   cs4231_chip->output_dma_handle,
  1382.                                   cs4231_chip->output_dma_size,
  1383.                                   SBUS_DMA_TODEVICE);
  1384.                 cs4231_chip->output_dma_handle = 0;
  1385.                 cs4231_chip->output_dma_size = 0;
  1386.         }
  1387.         if (cs4231_chip->output_next_dma_handle) {
  1388.                 sbus_unmap_single(drv->dev,
  1389.                                   cs4231_chip->output_next_dma_handle,
  1390.                                   cs4231_chip->output_next_dma_size,
  1391.                                   SBUS_DMA_TODEVICE);
  1392.                 cs4231_chip->output_next_dma_handle = 0;
  1393.                 cs4231_chip->output_next_dma_size = 0;
  1394.         }
  1395. #if 0 /* Not safe without shutting off the DMA controller as well. -DaveM */
  1396.         /* Else subsequent speed setting changes are ignored by the chip. */
  1397.         cs4231_disable_play(drv);
  1398. #endif
  1399. }
  1400. #ifdef EB4231_SUPPORT
  1401. static void eb4231_pollinput(struct sparcaudio_driver *drv)
  1402. {
  1403.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1404.         int x;
  1405.         u32 dcsr;
  1406.         x = 0;
  1407.         do {
  1408.                 dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
  1409.                 if (dcsr & EBUS_DCSR_TC)
  1410.                         break;
  1411.                 x++;
  1412.         } while (x <= CS_TIMEOUT);
  1413.         writel(dcsr | EBUS_DCSR_TC,
  1414.                cs4231_chip->eb2c + EBDMA_CSR);
  1415. }
  1416. #endif
  1417. static void cs4231_pollinput(struct sparcaudio_driver *drv)
  1418. {
  1419.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1420.         int x;
  1421.         u32 csr;
  1422.         x = 0;
  1423.         do {
  1424.                 csr = sbus_readl(cs4231_chip->regs + APCCSR);
  1425.                 if (csr & APC_XINT_COVF)
  1426.                         break;
  1427.                 x++;
  1428.         } while (x <= CS_TIMEOUT);
  1429.         sbus_writel(csr | APC_XINT_CEMP,
  1430.                     cs4231_chip->regs + APCCSR);
  1431. }
  1432. static void cs4231_start_input(struct sparcaudio_driver *drv, __u8 * buffer, 
  1433.                                unsigned long count)
  1434. {
  1435.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1436.         u32 csr;
  1437.         cs4231_chip->input_ptr = buffer;
  1438.         cs4231_chip->input_size = count;
  1439.         if (cs4231_chip->perchip_info.record.active || 
  1440.             (cs4231_chip->perchip_info.record.pause))
  1441.                 return;
  1442.         cs4231_ready(drv);
  1443.         cs4231_chip->perchip_info.record.active = 1;
  1444.         cs4231_chip->recording_count = 0;
  1445.         csr = sbus_readl(cs4231_chip->regs + APCCSR);
  1446.         if ((csr & APC_CPAUSE) || !(csr & APC_CDMA_READY)) {
  1447.                 csr &= ~APC_XINT_CAPT;
  1448.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1449.                 csr &= ~APC_CPAUSE;
  1450.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1451.                 cs4231_recintr(drv);
  1452.                 csr |= APC_CAPT_SETUP;
  1453.                 sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1454.                 cs4231_enable_rec(drv);
  1455.                 cs4231_ready(drv);
  1456.         } else {
  1457.                 cs4231_recintr(drv);
  1458.         }
  1459. }
  1460. static void cs4231_stop_input(struct sparcaudio_driver *drv)
  1461. {
  1462.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1463.         u32 csr;
  1464.         cs4231_chip->perchip_info.record.active = 0;
  1465.         csr = sbus_readl(cs4231_chip->regs + APCCSR);
  1466.         csr |= APC_CPAUSE;
  1467.         sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1468.         cs4231_chip->input_ptr = NULL;
  1469.         cs4231_chip->input_size = 0;
  1470.         if (cs4231_chip->input_dma_handle) {
  1471.                 sbus_unmap_single(drv->dev,
  1472.                                   cs4231_chip->input_dma_handle,
  1473.                                   cs4231_chip->input_dma_size,
  1474.                                   SBUS_DMA_FROMDEVICE);
  1475.                 cs4231_chip->input_dma_handle = 0;
  1476.                 cs4231_chip->input_dma_size = 0;
  1477.         }
  1478.         if (cs4231_chip->input_next_dma_handle) {
  1479.                 sbus_unmap_single(drv->dev,
  1480.                                   cs4231_chip->input_next_dma_handle,
  1481.                                   cs4231_chip->input_next_dma_size,
  1482.                                   SBUS_DMA_FROMDEVICE);
  1483.                 cs4231_chip->input_next_dma_handle = 0;
  1484.                 cs4231_chip->input_next_dma_size = 0;
  1485.         }
  1486.         cs4231_pollinput(drv);
  1487. }
  1488. #ifdef EB4231_SUPPORT
  1489. static void eb4231_start_input(struct sparcaudio_driver *drv, __u8 * buffer, 
  1490.                                unsigned long count)
  1491. {
  1492.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1493.         u32 dcsr;
  1494.         cs4231_chip->input_ptr = buffer;
  1495.         cs4231_chip->input_size = count;
  1496.         if (cs4231_chip->perchip_info.record.active || 
  1497.             (cs4231_chip->perchip_info.record.pause))
  1498.                 return;
  1499.         cs4231_ready(drv);
  1500.         cs4231_chip->perchip_info.record.active = 1;
  1501.         cs4231_chip->recording_count = 0;
  1502.         dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
  1503.         if (!(dcsr & EBUS_DCSR_EN_DMA)) {
  1504.                 writel(EBUS_DCSR_RESET, cs4231_chip->eb2c + EBDMA_CSR);
  1505.                 writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2c + EBDMA_CSR);
  1506.                 eb4231_recintr(drv);
  1507.                 writel(EBUS_DCSR_BURST_SZ_16 |
  1508.                        (EBUS_DCSR_EN_DMA | EBUS_DCSR_INT_EN |
  1509.                         EBUS_DCSR_EN_CNT | EBUS_DCSR_EN_NEXT),
  1510.                        cs4231_chip->eb2c + EBDMA_CSR);
  1511.                 cs4231_enable_rec(drv);
  1512.                 cs4231_ready(drv);
  1513.         } else {
  1514.                 eb4231_recintr(drv);
  1515.         }
  1516. }
  1517. static void eb4231_stop_input(struct sparcaudio_driver *drv)
  1518. {
  1519.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1520.         u32 dcsr;
  1521.         cs4231_chip->perchip_info.record.active = 0;
  1522.         cs4231_chip->input_ptr = NULL;
  1523.         cs4231_chip->input_size = 0;
  1524.         if (cs4231_chip->input_dma_handle) {
  1525. pci_unmap_single((struct pci_dev *)drv->dev,
  1526.  cs4231_chip->input_dma_handle,
  1527.  cs4231_chip->input_dma_size,
  1528.                                  PCI_DMA_FROMDEVICE);
  1529.                 cs4231_chip->input_dma_handle = 0;
  1530.                 cs4231_chip->input_dma_size = 0;
  1531.         }
  1532.         if (cs4231_chip->input_next_dma_handle) {
  1533. pci_unmap_single((struct pci_dev *)drv->dev,
  1534.  cs4231_chip->input_next_dma_handle,
  1535.  cs4231_chip->input_next_dma_size,
  1536.                                  PCI_DMA_FROMDEVICE);
  1537.                 cs4231_chip->input_next_dma_handle = 0;
  1538.                 cs4231_chip->input_next_dma_size = 0;
  1539.         }
  1540.         dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
  1541.         if (dcsr & EBUS_DCSR_EN_DMA)
  1542.                 writel(dcsr & ~EBUS_DCSR_EN_DMA, cs4231_chip->eb2c + EBDMA_CSR);
  1543.         cs4231_disable_rec(drv);
  1544. }
  1545. #endif
  1546. static int cs4231_set_output_pause(struct sparcaudio_driver *drv, int value)
  1547. {
  1548.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1549.         cs4231_chip->perchip_info.play.pause = value;
  1550.         if (!value)
  1551.                 sparcaudio_output_done(drv, 0);
  1552.         return value;
  1553. }
  1554. static int cs4231_set_output_error(struct sparcaudio_driver *drv, int value)
  1555. {
  1556.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1557.         int i;
  1558.   
  1559.         i = cs4231_chip->perchip_info.play.error;
  1560.         cs4231_chip->perchip_info.play.error = value;
  1561.   
  1562.         return i;
  1563. }
  1564. static int cs4231_set_input_error(struct sparcaudio_driver *drv, int value)
  1565. {
  1566.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1567.         int i;
  1568.   
  1569.         i = cs4231_chip->perchip_info.record.error;
  1570.         cs4231_chip->perchip_info.record.error = value;
  1571.   
  1572.         return i;
  1573. }
  1574. static int cs4231_set_output_samples(struct sparcaudio_driver *drv, int value)
  1575. {
  1576.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *)drv->private;
  1577.         int i;
  1578.   
  1579.         i = cs4231_chip->perchip_info.play.samples;
  1580.         cs4231_chip->perchip_info.play.samples = value;
  1581.   
  1582.         return i;
  1583. }
  1584. static int cs4231_set_input_samples(struct sparcaudio_driver *drv, int value)
  1585. {
  1586.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1587.         int i;
  1588.   
  1589.         i = cs4231_chip->perchip_info.record.samples;
  1590.         cs4231_chip->perchip_info.record.samples = value;
  1591.         return i;
  1592. }
  1593. static int cs4231_set_input_pause(struct sparcaudio_driver *drv, int value)
  1594. {
  1595.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1596.         cs4231_chip->perchip_info.record.pause = value;
  1597.         if (value)
  1598.                 cs4231_stop_input(drv);
  1599.         return value;
  1600. }
  1601. static void cs4231_audio_getdev(struct sparcaudio_driver *drv,
  1602.                                  audio_device_t * audinfo)
  1603. {
  1604.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1605.         strncpy(audinfo->name, "SUNW,CS4231", sizeof(audinfo->name) - 1);
  1606.         /* versions */
  1607.         /* a: SPARCstation 4/5          b: Ultra 1/2 (electron)       */
  1608.         /* c: Ultra 1/2 PCI? (positron) d: ppc                        */
  1609.         /* e: x86                       f: Ultra Enterprise? (tazmo)  */
  1610.         /* g: Ultra 30? (quark)         h: Ultra 5/10? (darwin)       */
  1611.         /* apparently Ultra 1, Ultra 2 don't have internal CD input */
  1612.         if (cs4231_chip->status & CS_STATUS_IS_ULTRA)
  1613.                 strncpy(audinfo->version, "b", sizeof(audinfo->version) - 1);
  1614.         else
  1615.                 strncpy(audinfo->version, "a", sizeof(audinfo->version) - 1);
  1616.         strncpy(audinfo->config, "onboard1", sizeof(audinfo->config) - 1);
  1617. }
  1618. static int cs4231_audio_getdev_sunos(struct sparcaudio_driver *drv)
  1619. {
  1620.         return AUDIO_DEV_CS4231;
  1621. }
  1622. static void cs4231_loopback(struct sparcaudio_driver *drv, unsigned int value)
  1623. {
  1624.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1625.         WRITE_IAR(0x0d);
  1626.         WRITE_IDR(value ? LOOPB_ON : 0);
  1627. }
  1628. static int cs4231_ioctl(struct inode * inode, struct file * file,
  1629. unsigned int cmd, unsigned long arg, 
  1630. struct sparcaudio_driver *drv)
  1631. {
  1632.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1633.         int retval = 0;
  1634.   
  1635.         switch (cmd) {
  1636.         case AUDIO_DIAG_LOOPBACK:
  1637.                 cs4231_chip->status |= CS_STATUS_INIT_ON_CLOSE;
  1638.                 cs4231_loopback(drv, (unsigned int)arg);
  1639.                 break;
  1640.         default:
  1641.                 retval = -EINVAL;
  1642.         };
  1643.         return retval;
  1644. }
  1645. #ifdef EB4231_SUPPORT
  1646. /* ebus audio capture interrupt handler. */
  1647. void eb4231_cinterrupt(int irq, void *dev_id, struct pt_regs *regs)
  1648. {
  1649.         struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
  1650.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1651.         u32 dummy;
  1652.   
  1653.         /* Clear the interrupt. */
  1654.         dummy = readl(cs4231_chip->eb2c + EBDMA_CSR);
  1655.         writel(dummy, cs4231_chip->eb2c + EBDMA_CSR);
  1656.         if ((dummy & EBUS_DCSR_TC) != 0
  1657.             /*&& (dummy & EBUS_DCSR_A_LOADED) != 0*/) {
  1658.                 cs4231_chip->perchip_info.record.samples += 
  1659.                     cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.record), 
  1660.                                                  cs4231_chip->reclen);
  1661.                 eb4231_recintr(drv);
  1662.         }
  1663.         if ((dummy & EBUS_DCSR_A_LOADED) == 0) {
  1664.                 cs4231_chip->perchip_info.record.active = 0;
  1665.                 eb4231_recintr(drv);
  1666.                 eb4231_getsamplecount(drv, cs4231_chip->reclen, 1);
  1667.         }
  1668. }
  1669. /* ebus audio play interrupt handler. */
  1670. void eb4231_pinterrupt(int irq, void *dev_id, struct pt_regs *regs)
  1671. {
  1672.         struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
  1673.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1674.         u32 dummy;
  1675.   
  1676.         /* Clear the interrupt.  Bleh, when not using the next-address
  1677.          * feature, TC can only be cleared by a reset.
  1678.          */
  1679.         dummy = readl(cs4231_chip->eb2p + EBDMA_CSR);
  1680.         writel(dummy, cs4231_chip->eb2p + EBDMA_CSR);
  1681.         /* If we get a terminal count and address loaded condition,
  1682.          * this means the DNAR was copied into DACR.
  1683.          */
  1684.         if((dummy & EBUS_DCSR_TC) != 0
  1685.            /*&& (dummy & EBUS_DCSR_A_LOADED) != 0*/) {
  1686.                 cs4231_chip->perchip_info.play.samples += 
  1687.                         cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.play), 
  1688.                                                      cs4231_chip->playlen); 
  1689.                 eb4231_playintr(drv);
  1690.         }
  1691.         if((dummy & EBUS_DCSR_A_LOADED) == 0) {
  1692.                 cs4231_chip->perchip_info.play.active = 0;
  1693.                 eb4231_playintr(drv);
  1694.                 eb4231_getsamplecount(drv, cs4231_chip->playlen, 0);
  1695.         }
  1696. }
  1697. #endif
  1698. /* Audio interrupt handler. */
  1699. void cs4231_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1700. {
  1701.         struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
  1702.         struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  1703.         __u32 dummy;
  1704.   
  1705.         dprintk(("in cs4231_interruptn"));
  1706.         /* Clear the interrupt. */
  1707.         dummy = sbus_readl(cs4231_chip->regs + APCCSR);
  1708.         sbus_writel(dummy, cs4231_chip->regs + APCCSR);
  1709.         /* now go through and figure out what gets to claim the interrupt 
  1710.          * if anything since we may be doing shared interrupts 
  1711.          */
  1712.         if (dummy & APC_PLAY_INT) {
  1713.                 if (dummy & APC_XINT_PNVA) {
  1714.                         cs4231_chip->perchip_info.play.samples += 
  1715.                          cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.play), 
  1716.                                                       cs4231_chip->playlen); 
  1717.                         if (!(dummy & APC_XINT_EMPT))
  1718.                                 cs4231_playintr(drv, 1);
  1719.                 } 
  1720.                 /* Any other conditions we need worry about? */
  1721.         }
  1722.         if (dummy & APC_CAPT_INT) {
  1723.                 if (dummy & APC_XINT_CNVA) {
  1724.                   cs4231_chip->perchip_info.record.samples += 
  1725.                     cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.record), 
  1726.                                                  cs4231_chip->reclen);
  1727.                   cs4231_recintr(drv);
  1728.                 }
  1729.                 /* Any other conditions we need worry about? */
  1730.         }
  1731.   
  1732.         if (dummy & APC_XINT_CEMP) {
  1733.                 if (cs4231_chip->perchip_info.record.active == 0) {
  1734.                         /* Fix me */
  1735.                         cs4231_chip->perchip_info.record.active = 0;
  1736.                         cs4231_chip->perchip_info.record.error = 1;
  1737.                         cs4231_recintr(drv);
  1738.                 }
  1739.         }
  1740.         if (dummy & APC_XINT_EMPT) {
  1741.                 if (!cs4231_chip->output_next_dma_handle) {
  1742.                         u32 csr = sbus_readl(cs4231_chip->regs + APCCSR);
  1743.                         csr |= APC_PPAUSE;
  1744.                         sbus_writel(csr, cs4231_chip->regs + APCCSR);
  1745.                         cs4231_disable_play(drv);
  1746.                         cs4231_chip->perchip_info.play.error = 1;
  1747.                 }
  1748.                 cs4231_chip->perchip_info.play.active = 0;
  1749.                 cs4231_playintr(drv, 0);
  1750.                 cs4231_getsamplecount(drv, cs4231_chip->playlen, 0);
  1751.         }
  1752.         if (dummy & APC_GENL_INT) {
  1753.                 /* If we get here we must be sharing an interrupt, but I haven't code 
  1754.                  * to handle this right now.
  1755.                  */
  1756.         }
  1757. }
  1758. static struct sparcaudio_operations cs4231_ops = {
  1759. cs4231_open,
  1760. cs4231_release,
  1761. cs4231_ioctl,
  1762. cs4231_start_output,
  1763. cs4231_stop_output,
  1764. cs4231_start_input,
  1765.         cs4231_stop_input,
  1766. cs4231_audio_getdev,
  1767.         cs4231_set_output_volume,
  1768.         cs4231_get_output_volume,
  1769.         cs4231_set_input_volume,
  1770.         cs4231_get_input_volume,
  1771.         cs4231_set_monitor_volume,
  1772.         cs4231_get_monitor_volume,
  1773. cs4231_set_output_balance,
  1774. cs4231_get_output_balance,
  1775.         cs4231_set_input_balance,
  1776.         cs4231_get_input_balance,
  1777.         cs4231_set_output_channels,
  1778.         cs4231_get_output_channels,
  1779.         cs4231_set_input_channels,
  1780.         cs4231_get_input_channels,
  1781.         cs4231_set_output_precision,
  1782.         cs4231_get_output_precision,
  1783.         cs4231_set_input_precision,
  1784.         cs4231_get_input_precision,
  1785.         cs4231_set_output_port,
  1786.         cs4231_get_output_port,
  1787.         cs4231_set_input_port,
  1788.         cs4231_get_input_port,
  1789.         cs4231_set_output_encoding,
  1790.         cs4231_get_output_encoding,
  1791.         cs4231_set_input_encoding,
  1792.         cs4231_get_input_encoding,
  1793.         cs4231_set_output_rate,
  1794.         cs4231_get_output_rate,
  1795.         cs4231_set_input_rate,
  1796. cs4231_get_input_rate,
  1797. cs4231_audio_getdev_sunos,
  1798. cs4231_get_output_ports,
  1799. cs4231_get_input_ports,
  1800. cs4231_output_muted,
  1801. cs4231_get_output_muted,
  1802. cs4231_set_output_pause,
  1803. cs4231_get_output_pause,
  1804. cs4231_set_input_pause,
  1805. cs4231_get_input_pause,
  1806. cs4231_set_output_samples,
  1807. cs4231_get_output_samples,
  1808. cs4231_set_input_samples,
  1809. cs4231_get_input_samples,
  1810. cs4231_set_output_error,
  1811. cs4231_get_output_error,
  1812. cs4231_set_input_error,
  1813. cs4231_get_input_error,
  1814.         cs4231_get_formats,
  1815. };
  1816. #ifdef EB4231_SUPPORT
  1817. static struct sparcaudio_operations eb4231_ops = {
  1818. cs4231_open,
  1819. cs4231_release,
  1820. cs4231_ioctl,
  1821. eb4231_start_output,
  1822. eb4231_stop_output,
  1823. eb4231_start_input,
  1824.         eb4231_stop_input,
  1825. cs4231_audio_getdev,
  1826.         cs4231_set_output_volume,
  1827.         cs4231_get_output_volume,
  1828.         cs4231_set_input_volume,
  1829.         cs4231_get_input_volume,
  1830.         cs4231_set_monitor_volume,
  1831.         cs4231_get_monitor_volume,
  1832. cs4231_set_output_balance,
  1833. cs4231_get_output_balance,
  1834.         cs4231_set_input_balance,
  1835.         cs4231_get_input_balance,
  1836.         cs4231_set_output_channels,
  1837.         cs4231_get_output_channels,
  1838.         cs4231_set_input_channels,
  1839.         cs4231_get_input_channels,
  1840.         cs4231_set_output_precision,
  1841.         cs4231_get_output_precision,
  1842.         cs4231_set_input_precision,
  1843.         cs4231_get_input_precision,
  1844.         cs4231_set_output_port,
  1845.         cs4231_get_output_port,
  1846.         cs4231_set_input_port,
  1847.         cs4231_get_input_port,
  1848.         cs4231_set_output_encoding,
  1849.         cs4231_get_output_encoding,
  1850.         cs4231_set_input_encoding,
  1851.         cs4231_get_input_encoding,
  1852.         cs4231_set_output_rate,
  1853.         cs4231_get_output_rate,
  1854.         cs4231_set_input_rate,
  1855. cs4231_get_input_rate,
  1856. cs4231_audio_getdev_sunos,
  1857. cs4231_get_output_ports,
  1858. cs4231_get_input_ports,
  1859. cs4231_output_muted,
  1860. cs4231_get_output_muted,
  1861. cs4231_set_output_pause,
  1862. cs4231_get_output_pause,
  1863. cs4231_set_input_pause,
  1864. cs4231_get_input_pause,
  1865. cs4231_set_output_samples,
  1866. eb4231_get_output_samples,
  1867. cs4231_set_input_samples,
  1868. eb4231_get_input_samples,
  1869. cs4231_set_output_error,
  1870. cs4231_get_output_error,
  1871. cs4231_set_input_error,
  1872. cs4231_get_input_error,
  1873.         cs4231_get_formats,
  1874. };
  1875. #endif
  1876. /* Attach to an cs4231 chip given its PROM node. */
  1877. static int cs4231_attach(struct sparcaudio_driver *drv, 
  1878.  struct sbus_dev *sdev)
  1879. {
  1880.         struct cs4231_chip *cs4231_chip;
  1881.         int err;
  1882.         /* Allocate our private information structure. */
  1883.         drv->private = kmalloc(sizeof(struct cs4231_chip), GFP_KERNEL);
  1884.         if (drv->private == NULL)
  1885.                 return -ENOMEM;
  1886.         /* Point at the information structure and initialize it. */
  1887.         drv->ops = &cs4231_ops;
  1888.         cs4231_chip = (struct cs4231_chip *) drv->private;
  1889.         cs4231_chip->input_ptr = cs4231_chip->output_ptr = NULL;
  1890.         cs4231_chip->input_size = cs4231_chip->output_size = 0;
  1891.         cs4231_chip->status = 0;
  1892.         drv->dev = sdev;
  1893.         /* Map the registers into memory. */
  1894.         cs4231_chip->regs_size = sdev->reg_addrs[0].reg_size;
  1895.         cs4231_chip->regs = sbus_ioremap(&sdev->resource[0], 0,
  1896.                                          sdev->reg_addrs[0].reg_size,
  1897.                                          "cs4231");
  1898.         if (!cs4231_chip->regs) {
  1899.                 printk(KERN_ERR "cs4231: could not remap registersn");
  1900.                 kfree(drv->private);
  1901.                 return -EIO;
  1902.         }
  1903.         /* Attach the interrupt handler to the audio interrupt. */
  1904.         cs4231_chip->irq = sdev->irqs[0];
  1905.         request_irq(cs4231_chip->irq, cs4231_interrupt, SA_SHIRQ, "cs4231", drv);
  1906.         cs4231_chip->nirqs = 1;
  1907.         cs4231_enable_interrupts(drv);
  1908.         /* Reset the audio chip. */
  1909.         cs4231_chip_reset(drv);
  1910.         /* Register ourselves with the midlevel audio driver. */
  1911.         err = register_sparcaudio_driver(drv, 1);
  1912.         if (err < 0) {
  1913.                 printk(KERN_ERR "cs4231: unable to registern");
  1914.                 cs4231_disable_interrupts(drv);
  1915.                 free_irq(cs4231_chip->irq, drv);
  1916.                 sbus_iounmap(cs4231_chip->regs, cs4231_chip->regs_size);
  1917.                 kfree(drv->private);
  1918.                 return -EIO;
  1919.         }
  1920.         cs4231_chip->perchip_info.play.active = 
  1921.                 cs4231_chip->perchip_info.play.pause = 0;
  1922.         cs4231_chip->perchip_info.record.active = 
  1923.                 cs4231_chip->perchip_info.record.pause = 0;
  1924.         cs4231_chip->perchip_info.play.avail_ports = (AUDIO_HEADPHONE |
  1925.                                                       AUDIO_SPEAKER |
  1926.                                                       AUDIO_LINE_OUT);
  1927.         cs4231_chip->perchip_info.record.avail_ports = (AUDIO_INTERNAL_CD_IN |
  1928.                                                         AUDIO_LINE_IN | 
  1929.                                                         AUDIO_MICROPHONE |
  1930.                                                         AUDIO_ANALOG_LOOPBACK);
  1931.         /* Announce the hardware to the user. */
  1932.         printk(KERN_INFO "audio%d: cs4231%c at %lx irq %sn",
  1933.                drv->index, (cs4231_chip->status & CS_STATUS_REV_A) ? 'a' : ' ', 
  1934.                cs4231_chip->regs, __irq_itoa(cs4231_chip->irq));
  1935.   
  1936.         /* Success! */
  1937.         return 0;
  1938. }
  1939. #ifdef EB4231_SUPPORT
  1940. /* Attach to an cs4231 chip given its PROM node. */
  1941. static int eb4231_attach(struct sparcaudio_driver *drv, 
  1942.  struct linux_ebus_device *edev)
  1943. {
  1944.         struct cs4231_chip *cs4231_chip;
  1945.         int len, err, nregs;
  1946.         struct linux_prom_registers regs[4];
  1947.         /* Allocate our private information structure. */
  1948.         drv->private = kmalloc(sizeof(struct cs4231_chip), GFP_KERNEL);
  1949.         if (drv->private == NULL)
  1950.                 return -ENOMEM;
  1951.         /* Point at the information structure and initialize it. */
  1952.         drv->ops = &eb4231_ops;
  1953.         cs4231_chip = (struct cs4231_chip *) drv->private;
  1954.         cs4231_chip->input_ptr = cs4231_chip->output_ptr = NULL;
  1955.         cs4231_chip->input_size = cs4231_chip->output_size = 0;
  1956.         cs4231_chip->status = 0;
  1957.         drv->dev = (struct sbus_dev *)edev->bus->self;
  1958.         len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
  1959.         if ((len % sizeof(regs[0])) != 0) {
  1960.                 printk("eb4231: Strange reg property size %dn", len);
  1961.                 return -ENODEV;
  1962.         }
  1963.         nregs = len / sizeof(regs[0]);
  1964.         cs4231_chip->regs = (unsigned long)ioremap(edev->resource[0].start, 0x10);
  1965.         cs4231_chip->eb2p = (unsigned long)ioremap(edev->resource[1].start, 0x10);
  1966.         cs4231_chip->eb2c = (unsigned long)ioremap(edev->resource[2].start, 0x10);
  1967.         cs4231_chip->status |= CS_STATUS_IS_EBUS;
  1968.         /* Attach the interrupt handler to the audio interrupt. */
  1969.         cs4231_chip->irq = edev->irqs[0];
  1970.         cs4231_chip->irq2 = edev->irqs[1];
  1971.         if(request_irq(cs4231_chip->irq, eb4231_cinterrupt, SA_SHIRQ, "cs4231", drv) ||
  1972.            request_irq(cs4231_chip->irq2, eb4231_pinterrupt, SA_SHIRQ, "cs4231", drv))
  1973.                 goto bail;
  1974.         cs4231_chip->nirqs = 2;
  1975.         cs4231_enable_interrupts(drv);
  1976.         /* Reset the audio chip. */
  1977.         cs4231_chip_reset(drv);
  1978.         /* Register ourselves with the midlevel audio driver. */
  1979.         err = register_sparcaudio_driver(drv, 1);
  1980.         if (err < 0) {
  1981.         bail:
  1982.                 printk(KERN_ERR "cs4231: unable to registern");
  1983.                 cs4231_disable_interrupts(drv);
  1984.                 free_irq(cs4231_chip->irq, drv);
  1985.                 free_irq(cs4231_chip->irq2, drv);
  1986.                 kfree(drv->private);
  1987.                 return -EIO;
  1988.         }
  1989.         cs4231_chip->perchip_info.play.active = 
  1990.                 cs4231_chip->perchip_info.play.pause = 0;
  1991.         cs4231_chip->perchip_info.record.active = 
  1992.                 cs4231_chip->perchip_info.record.pause = 0;
  1993.         cs4231_chip->perchip_info.play.avail_ports = (AUDIO_HEADPHONE |
  1994.                                                       AUDIO_SPEAKER |
  1995.                                                       AUDIO_LINE_OUT);
  1996.         cs4231_chip->perchip_info.record.avail_ports = (AUDIO_INTERNAL_CD_IN |
  1997.                                                         AUDIO_LINE_IN | 
  1998.                                                         AUDIO_MICROPHONE |
  1999.                                                         AUDIO_ANALOG_LOOPBACK);
  2000.         /* Announce the hardware to the user. */
  2001.         printk(KERN_INFO "audio%d: cs4231%c(eb2) at %lx irq %sn",
  2002.                drv->index, (cs4231_chip->status & CS_STATUS_REV_A) ? 'a' : ' ', 
  2003.                cs4231_chip->regs, __irq_itoa(cs4231_chip->irq));
  2004.   
  2005.         /* Success! */
  2006.         return 0;
  2007. }
  2008. #endif
  2009. #ifdef EB4231_SUPPORT
  2010. static int __init ebus_cs4231_p(struct linux_ebus_device *edev)
  2011. {
  2012.         if (!strcmp(edev->prom_name, "SUNW,CS4231"))
  2013.                 return 1;
  2014.         if (!strcmp(edev->prom_name, "audio")) {
  2015.                 char compat[16];
  2016.                 prom_getstring(edev->prom_node, "compatible",
  2017.                                compat, sizeof(compat));
  2018.                 compat[15] = '';
  2019.                 if (!strcmp(compat, "SUNW,CS4231"))
  2020.                         return 1;
  2021.         }
  2022.         return 0;
  2023. }
  2024. #endif
  2025. /* Detach from an cs4231 chip given the device structure. */
  2026. static void __exit cs4231_detach(struct sparcaudio_driver *drv)
  2027. {
  2028. struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
  2029. cs4231_disable_interrupts(drv);
  2030. unregister_sparcaudio_driver(drv, 1);
  2031. free_irq(cs4231_chip->irq, drv);
  2032. if (!(cs4231_chip->status & CS_STATUS_IS_EBUS)) {
  2033. sbus_iounmap(cs4231_chip->regs, cs4231_chip->regs_size);
  2034. } else {
  2035. #ifdef EB4231_SUPPORT
  2036. iounmap(cs4231_chip->regs);
  2037. iounmap(cs4231_chip->eb2p);
  2038. iounmap(cs4231_chip->eb2c);
  2039. free_irq(cs4231_chip->irq2, drv);
  2040. #endif
  2041. }
  2042. kfree(drv->private);
  2043. }
  2044. /* Probe for the cs4231 chip and then attach the driver. */
  2045. static int __init cs4231_init(void)
  2046. {
  2047.         struct sbus_bus *sbus;
  2048.         struct sbus_dev *sdev;
  2049. #ifdef EB4231_SUPPORT
  2050.         struct linux_ebus *ebus;
  2051.         struct linux_ebus_device *edev;
  2052. #endif
  2053.         num_drivers = 0;
  2054.   
  2055.         /* Probe each SBUS for cs4231 chips. */
  2056.         for_all_sbusdev(sdev, sbus) {
  2057.                 if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
  2058.                         /* Don't go over the max number of drivers. */
  2059.                         if (num_drivers >= MAX_DRIVERS)
  2060.                                 continue;
  2061.       
  2062.                         if (cs4231_attach(&drivers[num_drivers], sdev) == 0)
  2063.                                 num_drivers++;
  2064.                 }
  2065.         }
  2066.   
  2067. #ifdef EB4231_SUPPORT
  2068.         for_each_ebus(ebus) {
  2069.                 for_each_ebusdev(edev, ebus) {
  2070.                         if (ebus_cs4231_p(edev)) {
  2071.                                 /* Don't go over the max number of drivers. */
  2072.                                 if (num_drivers >= MAX_DRIVERS)
  2073.                                         continue;
  2074.       
  2075.                                 if (eb4231_attach(&drivers[num_drivers], edev) == 0)
  2076.                                         num_drivers++;
  2077.                         }
  2078.                 }
  2079.         }
  2080. #endif
  2081.         /* Only return success if we found some cs4231 chips. */
  2082.         return (num_drivers > 0) ? 0 : -EIO;
  2083. }
  2084. static void __exit cs4231_exit(void)
  2085. {
  2086.         register int i;
  2087.         for (i = 0; i < num_drivers; i++) {
  2088.                 cs4231_detach(&drivers[i]);
  2089.                 num_drivers--;
  2090.         }
  2091. }
  2092. module_init(cs4231_init);
  2093. module_exit(cs4231_exit);
  2094. MODULE_LICENSE("GPL");
  2095. /*
  2096.  * Overrides for Emacs so that we follow Linus's tabbing style.
  2097.  * Emacs will notice this stuff at the end of the file and automatically
  2098.  * adjust the settings for this buffer only.  This must remain at the end
  2099.  * of the file.
  2100.  * ---------------------------------------------------------------------------
  2101.  * Local variables:
  2102.  * c-indent-level: 4
  2103.  * c-brace-imaginary-offset: 0
  2104.  * c-brace-offset: -4
  2105.  * c-argdecl-indent: 4
  2106.  * c-label-offset: -4
  2107.  * c-continued-statement-offset: 4
  2108.  * c-continued-brace-offset: 0
  2109.  * indent-tabs-mode: nil
  2110.  * tab-width: 8
  2111.  * End:
  2112.  */