3f9684f1d1d2a24e1fe7cbcf24f98d3a4ca3a5a7
[linux-2.6.git] / sound / mips / au1x00.c
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *  Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port
4  *
5  * Copyright 2004 Cooper Street Innovations Inc.
6  * Author: Charles Eidsness     <charles@cooper-street.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
14  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
15  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
16  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
17  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
19  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
21  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  *  You should have received a copy of the  GNU General Public License along
25  *  with this program; if not, write  to the Free Software Foundation, Inc.,
26  *  675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  * History:
29  *
30  * 2004-09-09 Charles Eidsness  -- Original verion -- based on
31  *                                sa11xx-uda1341.c ALSA driver and the
32  *                                au1000.c OSS driver.
33  * 2004-09-09 Matt Porter       -- Added support for ALSA 1.0.6
34  *
35  */
36
37 #include <linux/ioport.h>
38 #include <linux/interrupt.h>
39 #include <sound/driver.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/version.h>
43 #include <sound/core.h>
44 #include <sound/initval.h>
45 #include <sound/pcm.h>
46 #include <sound/ac97_codec.h>
47 #include <asm/mach-au1x00/au1000.h>
48 #include <asm/mach-au1x00/au1000_dma.h>
49
50 MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
51 MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
52 MODULE_LICENSE("GPL");
53 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
54 MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
55 #else
56 MODULE_CLASSES("{sound}");
57 MODULE_DEVICES("{{AMD,Au1000 AC'97}}");
58 #endif
59
60 #define chip_t au1000_t
61
62 #define PLAYBACK 0
63 #define CAPTURE 1
64 #define AC97_SLOT_3 0x01
65 #define AC97_SLOT_4 0x02
66 #define AC97_SLOT_6 0x08
67 #define AC97_CMD_IRQ 31
68 #define READ 0
69 #define WRITE 1
70 #define READ_WAIT 2
71 #define RW_DONE 3
72
73 DECLARE_WAIT_QUEUE_HEAD(ac97_command_wq);
74
75 typedef struct au1000_period au1000_period_t;
76 struct au1000_period
77 {
78         u32 start;
79         u32 relative_end;       /*realtive to start of buffer*/
80         au1000_period_t * next;
81 };
82
83 /*Au1000 AC97 Port Control Reisters*/
84 typedef struct au1000_ac97_reg au1000_ac97_reg_t;
85 struct au1000_ac97_reg {
86         u32 volatile config;
87         u32 volatile status;
88         u32 volatile data;
89         u32 volatile cmd;
90         u32 volatile cntrl;
91 };
92
93 typedef struct audio_stream audio_stream_t;
94 struct audio_stream {
95         snd_pcm_substream_t * substream;
96         int dma;
97         spinlock_t dma_lock;
98         au1000_period_t * buffer;
99         unsigned long period_size;
100 };
101
102 typedef struct snd_card_au1000 {
103         snd_card_t *card;
104         au1000_ac97_reg_t volatile *ac97_ioport;
105
106         struct resource *ac97_res_port;
107         spinlock_t ac97_lock;
108         ac97_t *ac97;
109
110         snd_pcm_t *pcm;
111         audio_stream_t *stream[2];      /* playback & capture */
112 } au1000_t;
113
114 static au1000_t *au1000 = NULL;
115
116 /*--------------------------- Local Functions --------------------------------*/
117 static void
118 au1000_set_ac97_xmit_slots(long xmit_slots)
119 {
120         u32 volatile ac97_config;
121
122         spin_lock(&au1000->ac97_lock);
123         ac97_config = au1000->ac97_ioport->config;
124         ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
125         ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
126         au1000->ac97_ioport->config = ac97_config;
127         spin_unlock(&au1000->ac97_lock);
128 }
129
130 static void
131 au1000_set_ac97_recv_slots(long recv_slots)
132 {
133         u32 volatile ac97_config;
134
135         spin_lock(&au1000->ac97_lock);
136         ac97_config = au1000->ac97_ioport->config;
137         ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK;
138         ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
139         au1000->ac97_ioport->config = ac97_config;
140         spin_unlock(&au1000->ac97_lock);
141 }
142
143
144 static void
145 au1000_dma_stop(audio_stream_t *stream)
146 {
147         unsigned long   flags;
148         au1000_period_t * pointer;
149         au1000_period_t * pointer_next;
150
151         if (stream->buffer != NULL) {
152                 spin_lock_irqsave(&stream->dma_lock, flags);
153                 disable_dma(stream->dma);
154                 spin_unlock_irqrestore(&stream->dma_lock, flags);
155
156                 pointer = stream->buffer;
157                 pointer_next = stream->buffer->next;
158
159                 do {
160                         kfree(pointer);
161                         pointer = pointer_next;
162                         pointer_next = pointer->next;
163                 } while (pointer != stream->buffer);
164
165                 stream->buffer = NULL;
166         }
167 }
168
169 static void
170 au1000_dma_start(audio_stream_t *stream)
171 {
172         snd_pcm_substream_t *substream = stream->substream;
173         snd_pcm_runtime_t *runtime = substream->runtime;
174
175         unsigned long flags, dma_start;
176         int i;
177         au1000_period_t * pointer;
178
179         if (stream->buffer == NULL) {
180                 dma_start = virt_to_phys(runtime->dma_area);
181
182                 stream->period_size = frames_to_bytes(runtime,
183                         runtime->period_size);
184                 stream->buffer = kmalloc(sizeof(au1000_period_t), GFP_KERNEL);
185                 pointer = stream->buffer;
186                 for (i = 0 ; i < runtime->periods ; i++) {
187                         pointer->start = (u32)(dma_start +
188                                 (i * stream->period_size));
189                         pointer->relative_end = (u32)
190                                 (((i+1) * stream->period_size) - 0x1);
191                         if ( i < runtime->periods - 1) {
192                                 pointer->next = kmalloc(sizeof(au1000_period_t)
193                                         , GFP_KERNEL);
194                                 pointer = pointer->next;
195                         }
196                 }
197                 pointer->next = stream->buffer;
198
199                 spin_lock_irqsave(&stream->dma_lock, flags);
200                 init_dma(stream->dma);
201                 if (get_dma_active_buffer(stream->dma) == 0) {
202                         clear_dma_done0(stream->dma);
203                         set_dma_addr0(stream->dma, stream->buffer->start);
204                         set_dma_count0(stream->dma, stream->period_size >> 1);
205                         set_dma_addr1(stream->dma, stream->buffer->next->start);
206                         set_dma_count1(stream->dma, stream->period_size >> 1);
207                 } else {
208                         clear_dma_done1(stream->dma);
209                         set_dma_addr1(stream->dma, stream->buffer->start);
210                         set_dma_count1(stream->dma, stream->period_size >> 1);
211                         set_dma_addr0(stream->dma, stream->buffer->next->start);
212                         set_dma_count0(stream->dma, stream->period_size >> 1);
213                 }
214                 enable_dma_buffers(stream->dma);
215                 start_dma(stream->dma);
216                 spin_unlock_irqrestore(&stream->dma_lock, flags);
217         }
218 }
219
220 static irqreturn_t
221 au1000_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
222 {
223         audio_stream_t *stream = (audio_stream_t *) dev_id;
224         snd_pcm_substream_t *substream = stream->substream;
225
226         spin_lock(&stream->dma_lock);
227         switch (get_dma_buffer_done(stream->dma)) {
228         case DMA_D0:
229                 stream->buffer = stream->buffer->next;
230                 clear_dma_done0(stream->dma);
231                 set_dma_addr0(stream->dma, stream->buffer->next->start);
232                 set_dma_count0(stream->dma, stream->period_size >> 1);
233                 enable_dma_buffer0(stream->dma);
234                 break;
235         case DMA_D1:
236                 stream->buffer = stream->buffer->next;
237                 clear_dma_done1(stream->dma);
238                 set_dma_addr1(stream->dma, stream->buffer->next->start);
239                 set_dma_count1(stream->dma, stream->period_size >> 1);
240                 enable_dma_buffer1(stream->dma);
241                 break;
242         case (DMA_D0 | DMA_D1):
243                 spin_unlock(&stream->dma_lock);
244                 printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
245                 au1000_dma_stop(stream);
246                 au1000_dma_start(stream);
247                 spin_lock(&stream->dma_lock);
248                 break;
249         case (~DMA_D0 & ~DMA_D1):
250                 printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
251         }
252         spin_unlock(&stream->dma_lock);
253         snd_pcm_period_elapsed(substream);
254         return IRQ_HANDLED;
255 }
256
257 /*-------------------------- PCM Audio Streams -------------------------------*/
258
259 static unsigned int rates[] = {8000, 11025, 16000, 22050};
260 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
261         .count  =  sizeof(rates) / sizeof(rates[0]),
262         .list   = rates,
263         .mask   = 0,
264 };
265
266 static snd_pcm_hardware_t snd_au1000 =
267 {
268         .info                   = (SNDRV_PCM_INFO_INTERLEAVED | \
269                                 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
270         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
271         .rates                  = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
272                                 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
273         .rate_min               = 8000,
274         .rate_max               = 22050,
275         .channels_min           = 1,
276         .channels_max           = 2,
277         .buffer_bytes_max       = 128*1024,
278         .period_bytes_min       = 32,
279         .period_bytes_max       = 16*1024,
280         .periods_min            = 8,
281         .periods_max            = 255,
282         .fifo_size              = 16,
283 };
284
285 static int
286 snd_au1000_playback_open(snd_pcm_substream_t * substream)
287 {
288         au1000->stream[PLAYBACK]->substream = substream;
289         au1000->stream[PLAYBACK]->buffer = NULL;
290         substream->private_data = au1000->stream[PLAYBACK];
291         substream->runtime->hw = snd_au1000;
292         return (snd_pcm_hw_constraint_list(substream->runtime, 0,
293                 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
294 }
295
296 static int
297 snd_au1000_capture_open(snd_pcm_substream_t * substream)
298 {
299         au1000->stream[CAPTURE]->substream = substream;
300         au1000->stream[CAPTURE]->buffer = NULL;
301         substream->private_data = au1000->stream[CAPTURE];
302         substream->runtime->hw = snd_au1000;
303         return (snd_pcm_hw_constraint_list(substream->runtime, 0,
304                 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
305
306 }
307
308 static int
309 snd_au1000_playback_close(snd_pcm_substream_t * substream)
310 {
311         au1000->stream[PLAYBACK]->substream = NULL;
312         return 0;
313 }
314
315 static int
316 snd_au1000_capture_close(snd_pcm_substream_t * substream)
317 {
318         au1000->stream[CAPTURE]->substream = NULL;
319         return 0;
320 }
321
322 static int
323 snd_au1000_hw_params(snd_pcm_substream_t * substream,
324                                         snd_pcm_hw_params_t * hw_params)
325 {
326         return snd_pcm_lib_malloc_pages(substream,
327                                         params_buffer_bytes(hw_params));
328 }
329
330 static int
331 snd_au1000_hw_free(snd_pcm_substream_t * substream)
332 {
333         return snd_pcm_lib_free_pages(substream);
334 }
335
336 static int
337 snd_au1000_playback_prepare(snd_pcm_substream_t * substream)
338 {
339         snd_pcm_runtime_t *runtime = substream->runtime;
340
341         if (runtime->channels == 1 )
342                 au1000_set_ac97_xmit_slots(AC97_SLOT_4);
343         else
344                 au1000_set_ac97_xmit_slots(AC97_SLOT_3 | AC97_SLOT_4);
345         snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
346         return 0;
347 }
348
349 static int
350 snd_au1000_capture_prepare(snd_pcm_substream_t * substream)
351 {
352         snd_pcm_runtime_t *runtime = substream->runtime;
353
354         if (runtime->channels == 1 )
355                 au1000_set_ac97_recv_slots(AC97_SLOT_4);
356         else
357                 au1000_set_ac97_recv_slots(AC97_SLOT_3 | AC97_SLOT_4);
358         snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
359         return 0;
360 }
361
362 static int
363 snd_au1000_trigger(snd_pcm_substream_t * substream, int cmd)
364 {
365         audio_stream_t *stream = substream->private_data;
366         int err = 0;
367
368         switch (cmd) {
369         case SNDRV_PCM_TRIGGER_START:
370                 au1000_dma_start(stream);
371                 break;
372         case SNDRV_PCM_TRIGGER_STOP:
373                 au1000_dma_stop(stream);
374                 break;
375         default:
376                 err = -EINVAL;
377                 break;
378         }
379         return err;
380 }
381
382 static snd_pcm_uframes_t
383 snd_au1000_pointer(snd_pcm_substream_t * substream)
384 {
385         audio_stream_t *stream = substream->private_data;
386         snd_pcm_runtime_t *runtime = substream->runtime;
387         unsigned long flags;
388         long location;
389
390         spin_lock_irqsave(&stream->dma_lock, flags);
391         location = get_dma_residue(stream->dma);
392         spin_unlock_irqrestore(&stream->dma_lock, flags);
393         location = stream->buffer->relative_end - location;
394         if (location == -1)
395                 location = 0;
396         return bytes_to_frames(runtime,location);
397 }
398
399 static snd_pcm_ops_t snd_card_au1000_playback_ops = {
400         .open                   = snd_au1000_playback_open,
401         .close                  = snd_au1000_playback_close,
402         .ioctl                  = snd_pcm_lib_ioctl,
403         .hw_params              = snd_au1000_hw_params,
404         .hw_free                = snd_au1000_hw_free,
405         .prepare                = snd_au1000_playback_prepare,
406         .trigger                = snd_au1000_trigger,
407         .pointer                = snd_au1000_pointer,
408 };
409
410 static snd_pcm_ops_t snd_card_au1000_capture_ops = {
411         .open                   = snd_au1000_capture_open,
412         .close                  = snd_au1000_capture_close,
413         .ioctl                  = snd_pcm_lib_ioctl,
414         .hw_params              = snd_au1000_hw_params,
415         .hw_free                = snd_au1000_hw_free,
416         .prepare                = snd_au1000_capture_prepare,
417         .trigger                = snd_au1000_trigger,
418         .pointer                = snd_au1000_pointer,
419 };
420
421 static int __devinit
422 snd_au1000_pcm_new(void)
423 {
424         snd_pcm_t *pcm;
425         int err;
426         unsigned long flags;
427
428         if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
429                 return err;
430
431         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
432                 snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
433
434         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
435                 &snd_card_au1000_playback_ops);
436         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
437                 &snd_card_au1000_capture_ops);
438
439         pcm->private_data = au1000;
440         pcm->info_flags = 0;
441         strcpy(pcm->name, "Au1000 AC97 PCM");
442
443         flags = claim_dma_lock();
444         if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX,
445                         "AC97 TX", au1000_dma_interrupt, SA_INTERRUPT,
446                         au1000->stream[PLAYBACK])) < 0) {
447                 release_dma_lock(flags);
448                 return -EBUSY;
449         }
450         if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX,
451                         "AC97 RX", au1000_dma_interrupt, SA_INTERRUPT,
452                         au1000->stream[CAPTURE])) < 0){
453                 release_dma_lock(flags);
454                 return -EBUSY;
455         }
456         /* enable DMA coherency in read/write DMA channels */
457         set_dma_mode(au1000->stream[PLAYBACK]->dma,
458                      get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
459         set_dma_mode(au1000->stream[CAPTURE]->dma,
460                      get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
461         release_dma_lock(flags);
462         spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
463         spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
464         au1000->pcm = pcm;
465         return 0;
466 }
467
468
469 /*-------------------------- AC97 CODEC Control ------------------------------*/
470
471 static unsigned short
472 snd_au1000_ac97_read(ac97_t *ac97, unsigned short reg)
473 {
474         u32 volatile cmd;
475         u16 volatile data;
476         int             i;
477         spin_lock(au1000->ac97_lock);
478 /* would rather use the interupt than this polling but it works and I can't
479 get the interupt driven case to work efficiently */
480         for (i = 0; i < 0x5000; i++)
481                 if (!(au1000->ac97_ioport->status & AC97C_CP))
482                         break;
483         if (i == 0x5000)
484                 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
485
486         cmd = (u32) reg & AC97C_INDEX_MASK;
487         cmd |= AC97C_READ;
488         au1000->ac97_ioport->cmd = cmd;
489
490         /* now wait for the data */
491         for (i = 0; i < 0x5000; i++)
492                 if (!(au1000->ac97_ioport->status & AC97C_CP))
493                         break;
494         if (i == 0x5000) {
495                 printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
496                 return 0;
497         }
498
499         data = au1000->ac97_ioport->cmd & 0xffff;
500         spin_unlock(au1000->ac97_lock);
501
502         return data;
503
504 }
505
506
507 static void
508 snd_au1000_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
509 {
510         u32 cmd;
511         int i;
512         spin_lock(au1000->ac97_lock);
513 /* would rather use the interupt than this polling but it works and I can't
514 get the interupt driven case to work efficiently */
515         for (i = 0; i < 0x5000; i++)
516                 if (!(au1000->ac97_ioport->status & AC97C_CP))
517                         break;
518         if (i == 0x5000)
519                 printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
520
521         cmd = (u32) reg & AC97C_INDEX_MASK;
522         cmd &= ~AC97C_READ;
523         cmd |= ((u32) val << AC97C_WD_BIT);
524         au1000->ac97_ioport->cmd = cmd;
525         spin_unlock(au1000->ac97_lock);
526 }
527 static void
528 snd_au1000_ac97_free(ac97_t *ac97)
529 {
530         au1000->ac97 = NULL;
531 }
532
533 static int __devinit
534 snd_au1000_ac97_new(void)
535 {
536         int err;
537
538 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
539         ac97_bus_t *pbus;
540         ac97_template_t ac97;
541         static ac97_bus_ops_t ops = {
542                 .write = snd_au1000_ac97_write,
543                 .read = snd_au1000_ac97_read,
544         };
545 #else
546         ac97_bus_t bus, *pbus;
547         ac97_t ac97;
548 #endif
549
550         if ((au1000->ac97_res_port = request_region(AC97C_CONFIG,
551                         sizeof(au1000_ac97_reg_t), "Au1x00 AC97")) == NULL) {
552                 snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n");
553                 return -EBUSY;
554         }
555         au1000->ac97_ioport = (au1000_ac97_reg_t *) au1000->ac97_res_port->start;
556
557         spin_lock_init(&au1000->ac97_lock);
558
559         spin_lock(&au1000->ac97_lock);
560
561         /* configure pins for AC'97
562         TODO: move to board_setup.c */
563         au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
564
565         /* Initialise Au1000's AC'97 Control Block */
566         au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
567         udelay(10);
568         au1000->ac97_ioport->cntrl = AC97C_CE;
569         udelay(10);
570
571         /* Initialise External CODEC -- cold reset */
572         au1000->ac97_ioport->config = AC97C_RESET;
573         udelay(10);
574         au1000->ac97_ioport->config = 0x0;
575         mdelay(5);
576
577         spin_unlock(&au1000->ac97_lock);
578
579         /* Initialise AC97 middle-layer */
580 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
581         if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0)
582                 return err;
583 #else
584         memset(&bus, 0, sizeof(bus));
585         bus.write = snd_au1000_ac97_write;
586         bus.read = snd_au1000_ac97_read;
587         if ((err = snd_ac97_bus(au1000->card, &bus, &pbus)) < 0)
588                 return err;
589 #endif
590         memset(&ac97, 0, sizeof(ac97));
591         ac97.private_data = au1000;
592         ac97.private_free = snd_au1000_ac97_free;
593         if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0)
594                 return err;
595         return 0;
596
597 }
598
599 /*------------------------------ Setup / Destroy ----------------------------*/
600
601 void
602 snd_au1000_free(snd_card_t *card)
603 {
604
605         if (au1000->ac97_res_port) {
606                 /* put internal AC97 block into reset */
607                 au1000->ac97_ioport->cntrl = AC97C_RS;
608                 au1000->ac97_ioport = NULL;
609                 release_resource(au1000->ac97_res_port);
610                 kfree_nocheck(au1000->ac97_res_port);
611         }
612
613         if (au1000->stream[PLAYBACK]->dma >= 0)
614                 free_au1000_dma(au1000->stream[PLAYBACK]->dma);
615
616         if (au1000->stream[CAPTURE]->dma >= 0)
617                 free_au1000_dma(au1000->stream[CAPTURE]->dma);
618
619         kfree(au1000->stream[PLAYBACK]);
620         au1000->stream[PLAYBACK] = NULL;
621         kfree(au1000->stream[CAPTURE]);
622         au1000->stream[CAPTURE] = NULL;
623         kfree(au1000);
624         au1000 = NULL;
625
626 }
627
628 static int __init
629 au1000_init(void)
630 {
631         int err;
632
633         au1000 = kmalloc(sizeof(au1000_t), GFP_KERNEL);
634         if (au1000 == NULL)
635                 return -ENOMEM;
636         au1000->stream[PLAYBACK] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
637         if (au1000->stream[PLAYBACK] == NULL)
638                 return -ENOMEM;
639         au1000->stream[CAPTURE] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
640         if (au1000->stream[CAPTURE] == NULL)
641                 return -ENOMEM;
642         /* so that snd_au1000_free will work as intended */
643         au1000->stream[PLAYBACK]->dma = -1;
644         au1000->stream[CAPTURE]->dma = -1;
645         au1000->ac97_res_port = NULL;
646
647         au1000->card = snd_card_new(-1, "AC97", THIS_MODULE, sizeof(au1000_t));
648         if (au1000->card == NULL) {
649                 snd_au1000_free(au1000->card);
650                 return -ENOMEM;
651         }
652
653         au1000->card->private_data = (au1000_t *)au1000;
654         au1000->card->private_free = snd_au1000_free;
655
656         if ((err = snd_au1000_ac97_new()) < 0 ) {
657                 snd_card_free(au1000->card);
658                 return err;
659         }
660
661         if ((err = snd_au1000_pcm_new()) < 0) {
662                 snd_card_free(au1000->card);
663                 return err;
664         }
665
666         strcpy(au1000->card->driver, "AMD-Au1000-AC97");
667         strcpy(au1000->card->shortname, "Au1000-AC97");
668         sprintf(au1000->card->longname, "AMD Au1000--AC97 ALSA Driver");
669
670         if ((err = snd_card_set_generic_dev(au1000->card)) < 0) {
671                 snd_card_free(au1000->card);
672                 return err;
673         }
674
675         if ((err = snd_card_register(au1000->card)) < 0) {
676                 snd_card_free(au1000->card);
677                 return err;
678         }
679
680         printk( KERN_INFO "ALSA AC97: Driver Initialized\n" );
681         return 0;
682 }
683
684 static void __exit au1000_exit(void)
685 {
686         snd_card_free(au1000->card);
687 }
688
689 module_init(au1000_init);
690 module_exit(au1000_exit);
691