Merge branch 'upstream'
[pandora-kernel.git] / sound / pci / als300.c
1 /*
2  *  als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
3  *  Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  *  TODO
20  *  4 channel playback for ALS300+
21  *  gameport
22  *  mpu401
23  *  opl3
24  *
25  *  NOTES
26  *  The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
27  *  the position in the current period, NOT the whole buffer. It is important
28  *  to know which period we are in so we can calculate the correct pointer.
29  *  This is why we always use 2 periods. We can then use a flip-flop variable
30  *  to keep track of what period we are in.
31  */
32
33 #include <sound/driver.h>
34 #include <linux/delay.h>
35 #include <linux/init.h>
36 #include <linux/moduleparam.h>
37 #include <linux/pci.h>
38 #include <linux/interrupt.h>
39 #include <linux/slab.h>
40
41 #include <asm/io.h>
42
43 #include <sound/core.h>
44 #include <sound/control.h>
45 #include <sound/initval.h>
46 #include <sound/pcm.h>
47 #include <sound/pcm_params.h>
48 #include <sound/ac97_codec.h>
49 #include <sound/opl3.h>
50
51 /* snd_als300_set_irq_flag */
52 #define IRQ_DISABLE             0
53 #define IRQ_ENABLE              1
54
55 /* I/O port layout */
56 #define AC97_ACCESS             0x00
57 #define AC97_READ               0x04
58 #define AC97_STATUS             0x06
59 #define   AC97_DATA_AVAIL               (1<<6)
60 #define   AC97_BUSY                     (1<<7)
61 #define ALS300_IRQ_STATUS       0x07            /* ALS300 Only */
62 #define   IRQ_PLAYBACK                  (1<<3)
63 #define   IRQ_CAPTURE                   (1<<2)
64 #define GCR_DATA                0x08
65 #define GCR_INDEX               0x0C
66 #define ALS300P_DRAM_IRQ_STATUS 0x0D            /* ALS300+ Only */
67 #define MPU_IRQ_STATUS          0x0E            /* ALS300 Rev. E+, ALS300+ */
68 #define ALS300P_IRQ_STATUS      0x0F            /* ALS300+ Only */
69
70 /* General Control Registers */
71 #define PLAYBACK_START          0x80
72 #define PLAYBACK_END            0x81
73 #define PLAYBACK_CONTROL        0x82
74 #define   TRANSFER_START                (1<<16)
75 #define   FIFO_PAUSE                    (1<<17)
76 #define RECORD_START            0x83
77 #define RECORD_END              0x84
78 #define RECORD_CONTROL          0x85
79 #define DRAM_WRITE_CONTROL      0x8B
80 #define   WRITE_TRANS_START             (1<<16)
81 #define   DRAM_MODE_2                   (1<<17)
82 #define MISC_CONTROL            0x8C
83 #define   IRQ_SET_BIT                   (1<<15)
84 #define   VMUTE_NORMAL                  (1<<20)
85 #define   MMUTE_NORMAL                  (1<<21)
86 #define MUS_VOC_VOL             0x8E
87 #define PLAYBACK_BLOCK_COUNTER  0x9A
88 #define RECORD_BLOCK_COUNTER    0x9B
89
90 #define DEBUG_CALLS     1
91 #define DEBUG_PLAY_REC  1
92
93 #if DEBUG_CALLS
94 #define snd_als300_dbgcalls(format, args...) printk(format, ##args)
95 #define snd_als300_dbgcallenter() printk(KERN_ERR "--> %s\n", __FUNCTION__)
96 #define snd_als300_dbgcallleave() printk(KERN_ERR "<-- %s\n", __FUNCTION__)
97 #else
98 #define snd_als300_dbgcalls(format, args...)
99 #define snd_als300_dbgcallenter()
100 #define snd_als300_dbgcallleave()
101 #endif
102
103 #if DEBUG_PLAY_REC
104 #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
105 #else
106 #define snd_als300_dbgplay(format, args...)
107 #endif          
108
109 enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
110
111 MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
112 MODULE_DESCRIPTION("Avance Logic ALS300");
113 MODULE_LICENSE("GPL");
114 MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
115
116 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
117 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
118 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
119
120 struct snd_als300 {
121         unsigned long port;
122         spinlock_t reg_lock;
123         struct snd_card *card;
124         struct pci_dev *pci;
125
126         struct snd_pcm *pcm;
127         struct snd_pcm_substream *playback_substream;
128         struct snd_pcm_substream *capture_substream;
129
130         struct snd_ac97 *ac97;
131         struct snd_opl3 *opl3;
132
133         struct resource *res_port;
134
135         int irq;
136
137         int chip_type; /* ALS300 or ALS300+ */
138
139         char revision;  
140 };
141
142 struct snd_als300_substream_data {
143         int period_flipflop;
144         int control_register;
145         int block_counter_register;
146 };
147
148 static struct pci_device_id snd_als300_ids[] = {
149         { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
150         { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
151         { 0, }
152 };
153
154 MODULE_DEVICE_TABLE(pci, snd_als300_ids);
155
156 static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
157 {
158         outb(reg, port+GCR_INDEX);
159         return inl(port+GCR_DATA);
160 }
161
162 static inline void snd_als300_gcr_write(unsigned long port,
163                                                 unsigned short reg, u32 val)
164 {
165         outb(reg, port+GCR_INDEX);
166         outl(val, port+GCR_DATA);
167 }
168
169 /* Enable/Disable Interrupts */
170 static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
171 {
172         u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
173         snd_als300_dbgcallenter();
174
175         /* boolean XOR check, since old vs. new hardware have
176            directly reversed bit setting for ENABLE and DISABLE.
177            ALS300+ acts like newer versions of ALS300 */
178         if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
179                                                 (cmd == IRQ_ENABLE)) == 0)
180                 tmp |= IRQ_SET_BIT;
181         else
182                 tmp &= ~IRQ_SET_BIT;
183         snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
184         snd_als300_dbgcallleave();
185 }
186
187 static int snd_als300_free(struct snd_als300 *chip)
188 {
189         snd_als300_dbgcallenter();
190         snd_als300_set_irq_flag(chip, IRQ_DISABLE);
191         if (chip->irq >= 0)
192                 free_irq(chip->irq, (void *)chip);
193         pci_release_regions(chip->pci);
194         pci_disable_device(chip->pci);
195         kfree(chip);
196         snd_als300_dbgcallleave();
197         return 0;
198 }
199
200 static int snd_als300_dev_free(struct snd_device *device)
201 {
202         struct snd_als300 *chip = device->device_data;
203         return snd_als300_free(chip);
204 }
205
206 static irqreturn_t snd_als300_interrupt(int irq, void *dev_id,
207                                                 struct pt_regs *regs)
208 {
209         u8 status;
210         struct snd_als300 *chip = dev_id;
211         struct snd_als300_substream_data *data;
212
213         status = inb(chip->port+ALS300_IRQ_STATUS);
214         if (!status) /* shared IRQ, for different device?? Exit ASAP! */
215                 return IRQ_NONE;
216
217         /* ACK everything ASAP */
218         outb(status, chip->port+ALS300_IRQ_STATUS);
219         if (status & IRQ_PLAYBACK) {
220                 if (chip->pcm && chip->playback_substream) {
221                         data = chip->playback_substream->runtime->private_data;
222                         data->period_flipflop ^= 1;
223                         snd_pcm_period_elapsed(chip->playback_substream);
224                         snd_als300_dbgplay("IRQ_PLAYBACK\n");
225                 }
226         }
227         if (status & IRQ_CAPTURE) {
228                 if (chip->pcm && chip->capture_substream) {
229                         data = chip->capture_substream->runtime->private_data;
230                         data->period_flipflop ^= 1;
231                         snd_pcm_period_elapsed(chip->capture_substream);
232                         snd_als300_dbgplay("IRQ_CAPTURE\n");
233                 }
234         }
235         return IRQ_HANDLED;
236 }
237
238 static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id,
239                                                 struct pt_regs *regs)
240 {
241         u8 general, mpu, dram;
242         struct snd_als300 *chip = dev_id;
243         struct snd_als300_substream_data *data;
244         
245         general = inb(chip->port+ALS300P_IRQ_STATUS);
246         mpu = inb(chip->port+MPU_IRQ_STATUS);
247         dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
248
249         /* shared IRQ, for different device?? Exit ASAP! */
250         if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
251                 return IRQ_NONE;
252
253         if (general & IRQ_PLAYBACK) {
254                 if (chip->pcm && chip->playback_substream) {
255                         outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
256                         data = chip->playback_substream->runtime->private_data;
257                         data->period_flipflop ^= 1;
258                         snd_pcm_period_elapsed(chip->playback_substream);
259                         snd_als300_dbgplay("IRQ_PLAYBACK\n");
260                 }
261         }
262         if (general & IRQ_CAPTURE) {
263                 if (chip->pcm && chip->capture_substream) {
264                         outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
265                         data = chip->capture_substream->runtime->private_data;
266                         data->period_flipflop ^= 1;
267                         snd_pcm_period_elapsed(chip->capture_substream);
268                         snd_als300_dbgplay("IRQ_CAPTURE\n");
269                 }
270         }
271         /* FIXME: Ack other interrupt types. Not important right now as
272          * those other devices aren't enabled. */
273         return IRQ_HANDLED;
274 }
275
276 static void __devexit snd_als300_remove(struct pci_dev *pci)
277 {
278         snd_als300_dbgcallenter();
279         snd_card_free(pci_get_drvdata(pci));
280         pci_set_drvdata(pci, NULL);
281         snd_als300_dbgcallleave();
282 }
283
284 static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
285                                                         unsigned short reg)
286 {
287         int i;
288         struct snd_als300 *chip = ac97->private_data;
289
290         for (i = 0; i < 1000; i++) {
291                 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
292                         break;
293                 udelay(10);
294         }
295         outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
296
297         for (i = 0; i < 1000; i++) {
298                 if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
299                         break;
300                 udelay(10);
301         }
302         return inw(chip->port+AC97_READ);
303 }
304
305 static void snd_als300_ac97_write(struct snd_ac97 *ac97,
306                                 unsigned short reg, unsigned short val)
307 {
308         int i;
309         struct snd_als300 *chip = ac97->private_data;
310
311         for (i = 0; i < 1000; i++) {
312                 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
313                         break;
314                 udelay(10);
315         }
316         outl((reg << 24) | val, chip->port+AC97_ACCESS);
317 }
318
319 static int snd_als300_ac97(struct snd_als300 *chip)
320 {
321         struct snd_ac97_bus *bus;
322         struct snd_ac97_template ac97;
323         int err;
324         static struct snd_ac97_bus_ops ops = {
325                 .write = snd_als300_ac97_write,
326                 .read = snd_als300_ac97_read,
327         };
328
329         snd_als300_dbgcallenter();
330         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
331                 return err;
332
333         memset(&ac97, 0, sizeof(ac97));
334         ac97.private_data = chip;
335
336         snd_als300_dbgcallleave();
337         return snd_ac97_mixer(bus, &ac97, &chip->ac97);
338 }
339
340 /* hardware definition
341  *
342  * In AC97 mode, we always use 48k/16bit/stereo.
343  * Any request to change data type is ignored by
344  * the card when it is running outside of legacy
345  * mode.
346  */
347 static struct snd_pcm_hardware snd_als300_playback_hw =
348 {
349         .info =                 (SNDRV_PCM_INFO_MMAP |
350                                 SNDRV_PCM_INFO_INTERLEAVED |
351                                 SNDRV_PCM_INFO_PAUSE |
352                                 SNDRV_PCM_INFO_MMAP_VALID),
353         .formats =              SNDRV_PCM_FMTBIT_S16,
354         .rates =                SNDRV_PCM_RATE_48000,
355         .rate_min =             48000,
356         .rate_max =             48000,
357         .channels_min =         2,
358         .channels_max =         2,
359         .buffer_bytes_max =     64 * 1024,
360         .period_bytes_min =     64,
361         .period_bytes_max =     32 * 1024,
362         .periods_min =          2,
363         .periods_max =          2,
364 };
365
366 static struct snd_pcm_hardware snd_als300_capture_hw =
367 {
368         .info =                 (SNDRV_PCM_INFO_MMAP |
369                                 SNDRV_PCM_INFO_INTERLEAVED |
370                                 SNDRV_PCM_INFO_PAUSE |
371                                 SNDRV_PCM_INFO_MMAP_VALID),
372         .formats =              SNDRV_PCM_FMTBIT_S16,
373         .rates =                SNDRV_PCM_RATE_48000,
374         .rate_min =             48000,
375         .rate_max =             48000,
376         .channels_min =         2,
377         .channels_max =         2,
378         .buffer_bytes_max =     64 * 1024,
379         .period_bytes_min =     64,
380         .period_bytes_max =     32 * 1024,
381         .periods_min =          2,
382         .periods_max =          2,
383 };
384
385 static int snd_als300_playback_open(struct snd_pcm_substream *substream)
386 {
387         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
388         struct snd_pcm_runtime *runtime = substream->runtime;
389         struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
390                                                                 GFP_KERNEL);
391
392         snd_als300_dbgcallenter();
393         chip->playback_substream = substream;
394         runtime->hw = snd_als300_playback_hw;
395         runtime->private_data = data;
396         data->control_register = PLAYBACK_CONTROL;
397         data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
398         snd_als300_dbgcallleave();
399         return 0;
400 }
401
402 static int snd_als300_playback_close(struct snd_pcm_substream *substream)
403 {
404         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
405         struct snd_als300_substream_data *data;
406
407         data = substream->runtime->private_data;
408         snd_als300_dbgcallenter();
409         kfree(data);
410         chip->playback_substream = NULL;
411         snd_pcm_lib_free_pages(substream);
412         snd_als300_dbgcallleave();
413         return 0;
414 }
415
416 static int snd_als300_capture_open(struct snd_pcm_substream *substream)
417 {
418         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
419         struct snd_pcm_runtime *runtime = substream->runtime;
420         struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
421                                                                 GFP_KERNEL);
422
423         snd_als300_dbgcallenter();
424         chip->capture_substream = substream;
425         runtime->hw = snd_als300_capture_hw;
426         runtime->private_data = data;
427         data->control_register = RECORD_CONTROL;
428         data->block_counter_register = RECORD_BLOCK_COUNTER;
429         snd_als300_dbgcallleave();
430         return 0;
431 }
432
433 static int snd_als300_capture_close(struct snd_pcm_substream *substream)
434 {
435         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
436         struct snd_als300_substream_data *data;
437
438         data = substream->runtime->private_data;
439         snd_als300_dbgcallenter();
440         kfree(data);
441         chip->capture_substream = NULL;
442         snd_pcm_lib_free_pages(substream);
443         snd_als300_dbgcallleave();
444         return 0;
445 }
446
447 static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream,
448                                         snd_pcm_hw_params_t * hw_params)
449 {
450         return snd_pcm_lib_malloc_pages(substream,
451                                         params_buffer_bytes(hw_params));
452 }
453
454 static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream)
455 {
456         return snd_pcm_lib_free_pages(substream);
457 }
458
459 static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
460 {
461         u32 tmp;
462         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
463         struct snd_pcm_runtime *runtime = substream->runtime;
464         unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
465         unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
466         
467         snd_als300_dbgcallenter();
468         spin_lock_irq(&chip->reg_lock);
469         tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
470         tmp &= ~TRANSFER_START;
471
472         snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
473                                                 period_bytes, buffer_bytes);
474         
475         /* set block size */
476         tmp &= 0xffff0000;
477         tmp |= period_bytes - 1;
478         snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
479
480         /* set dma area */
481         snd_als300_gcr_write(chip->port, PLAYBACK_START,
482                                         runtime->dma_addr);
483         snd_als300_gcr_write(chip->port, PLAYBACK_END,
484                                         runtime->dma_addr + buffer_bytes - 1);
485         spin_unlock_irq(&chip->reg_lock);
486         snd_als300_dbgcallleave();
487         return 0;
488 }
489
490 static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
491 {
492         u32 tmp;
493         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
494         struct snd_pcm_runtime *runtime = substream->runtime;
495         unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
496         unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
497
498         snd_als300_dbgcallenter();
499         spin_lock_irq(&chip->reg_lock);
500         tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
501         tmp &= ~TRANSFER_START;
502
503         snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
504                                                         buffer_bytes);
505
506         /* set block size */
507         tmp &= 0xffff0000;
508         tmp |= period_bytes - 1;
509
510         /* set dma area */
511         snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
512         snd_als300_gcr_write(chip->port, RECORD_START,
513                                         runtime->dma_addr);
514         snd_als300_gcr_write(chip->port, RECORD_END,
515                                         runtime->dma_addr + buffer_bytes - 1);
516         spin_unlock_irq(&chip->reg_lock);
517         snd_als300_dbgcallleave();
518         return 0;
519 }
520
521 static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
522 {
523         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
524         u32 tmp;
525         struct snd_als300_substream_data *data;
526         unsigned short reg;
527         int ret = 0;
528
529         data = substream->runtime->private_data;
530         reg = data->control_register;
531
532         snd_als300_dbgcallenter();
533         spin_lock(&chip->reg_lock);
534         switch (cmd) {
535         case SNDRV_PCM_TRIGGER_START:
536         case SNDRV_PCM_TRIGGER_RESUME:
537                 tmp = snd_als300_gcr_read(chip->port, reg);
538                 data->period_flipflop = 1;
539                 snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
540                 snd_als300_dbgplay("TRIGGER START\n");
541                 break;
542         case SNDRV_PCM_TRIGGER_STOP:
543         case SNDRV_PCM_TRIGGER_SUSPEND:
544                 tmp = snd_als300_gcr_read(chip->port, reg);
545                 snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
546                 snd_als300_dbgplay("TRIGGER STOP\n");
547                 break;
548         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
549                 tmp = snd_als300_gcr_read(chip->port, reg);
550                 snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
551                 snd_als300_dbgplay("TRIGGER PAUSE\n");
552                 break;
553         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
554                 tmp = snd_als300_gcr_read(chip->port, reg);
555                 snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
556                 snd_als300_dbgplay("TRIGGER RELEASE\n");
557                 break;
558         default:
559                 snd_als300_dbgplay("TRIGGER INVALID\n");
560                 ret = -EINVAL;
561         }
562         spin_unlock(&chip->reg_lock);
563         snd_als300_dbgcallleave();
564         return ret;
565 }
566
567 static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
568 {
569         u16 current_ptr;
570         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
571         struct snd_als300_substream_data *data;
572         unsigned short period_bytes;
573
574         data = substream->runtime->private_data;
575         period_bytes = snd_pcm_lib_period_bytes(substream);
576         
577         snd_als300_dbgcallenter();
578         spin_lock(&chip->reg_lock);
579         current_ptr = (u16) snd_als300_gcr_read(chip->port,
580                                         data->block_counter_register) + 4;
581         spin_unlock(&chip->reg_lock);
582         if (current_ptr > period_bytes)
583                 current_ptr = 0;
584         else
585                 current_ptr = period_bytes - current_ptr;
586
587         if (data->period_flipflop == 0)
588                 current_ptr += period_bytes;
589         snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
590         snd_als300_dbgcallleave();
591         return bytes_to_frames(substream->runtime, current_ptr);
592 }
593
594 static struct snd_pcm_ops snd_als300_playback_ops = {
595         .open =         snd_als300_playback_open,
596         .close =        snd_als300_playback_close,
597         .ioctl =        snd_pcm_lib_ioctl,
598         .hw_params =    snd_als300_pcm_hw_params,
599         .hw_free =      snd_als300_pcm_hw_free,
600         .prepare =      snd_als300_playback_prepare,
601         .trigger =      snd_als300_trigger,
602         .pointer =      snd_als300_pointer,
603 };
604
605 static struct snd_pcm_ops snd_als300_capture_ops = {
606         .open =         snd_als300_capture_open,
607         .close =        snd_als300_capture_close,
608         .ioctl =        snd_pcm_lib_ioctl,
609         .hw_params =    snd_als300_pcm_hw_params,
610         .hw_free =      snd_als300_pcm_hw_free,
611         .prepare =      snd_als300_capture_prepare,
612         .trigger =      snd_als300_trigger,
613         .pointer =      snd_als300_pointer,
614 };
615
616 static int __devinit snd_als300_new_pcm(struct snd_als300 *chip)
617 {
618         struct snd_pcm *pcm;
619         int err;
620
621         snd_als300_dbgcallenter();
622         err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
623         if (err < 0)
624                 return err;
625         pcm->private_data = chip;
626         strcpy(pcm->name, "ALS300");
627         chip->pcm = pcm;
628
629         /* set operators */
630         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
631                                 &snd_als300_playback_ops);
632         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
633                                 &snd_als300_capture_ops);
634
635         /* pre-allocation of buffers */
636         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
637         snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
638         snd_als300_dbgcallleave();
639         return 0;
640 }
641
642 static void snd_als300_init(struct snd_als300 *chip)
643 {
644         unsigned long flags;
645         u32 tmp;
646         
647         snd_als300_dbgcallenter();
648         spin_lock_irqsave(&chip->reg_lock, flags);
649         chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
650                                                                 & 0x0000000F;
651         /* Setup DRAM */
652         tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
653         snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
654                                                 (tmp | DRAM_MODE_2)
655                                                 & ~WRITE_TRANS_START);
656
657         /* Enable IRQ output */
658         snd_als300_set_irq_flag(chip, IRQ_ENABLE);
659
660         /* Unmute hardware devices so their outputs get routed to
661          * the onboard mixer */
662         tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
663         snd_als300_gcr_write(chip->port, MISC_CONTROL,
664                         tmp | VMUTE_NORMAL | MMUTE_NORMAL);
665
666         /* Reset volumes */
667         snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
668
669         /* Make sure playback transfer is stopped */
670         tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
671         snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
672                         tmp & ~TRANSFER_START);
673         spin_unlock_irqrestore(&chip->reg_lock, flags);
674         snd_als300_dbgcallleave();
675 }
676
677 static int __devinit snd_als300_create(snd_card_t *card,
678                                        struct pci_dev *pci, int chip_type,
679                                        struct snd_als300 **rchip)
680 {
681         struct snd_als300 *chip;
682         void *irq_handler;
683         int err;
684
685         static snd_device_ops_t ops = {
686                 .dev_free = snd_als300_dev_free,
687         };
688         *rchip = NULL;
689
690         snd_als300_dbgcallenter();
691         if ((err = pci_enable_device(pci)) < 0)
692                 return err;
693
694         if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
695                 pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
696                 printk(KERN_ERR "error setting 28bit DMA mask\n");
697                 pci_disable_device(pci);
698                 return -ENXIO;
699         }
700         pci_set_master(pci);
701
702         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
703         if (chip == NULL) {
704                 pci_disable_device(pci);
705                 return -ENOMEM;
706         }
707
708         chip->card = card;
709         chip->pci = pci;
710         chip->irq = -1;
711         chip->chip_type = chip_type;
712         spin_lock_init(&chip->reg_lock);
713
714         if ((err = pci_request_regions(pci, "ALS300")) < 0) {
715                 kfree(chip);
716                 pci_disable_device(pci);
717                 return err;
718         }
719         chip->port = pci_resource_start(pci, 0);
720
721         if (chip->chip_type == DEVICE_ALS300_PLUS)
722                 irq_handler = snd_als300plus_interrupt;
723         else
724                 irq_handler = snd_als300_interrupt;
725
726         if (request_irq(pci->irq, irq_handler, SA_INTERRUPT|SA_SHIRQ,
727                                         card->shortname, (void *)chip)) {
728                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
729                 snd_als300_free(chip);
730                 return -EBUSY;
731         }
732         chip->irq = pci->irq;
733
734
735         snd_als300_init(chip);
736
737         if (snd_als300_ac97(chip) < 0) {
738                 snd_printk(KERN_WARNING "Could not create ac97\n");
739                 snd_als300_free(chip);
740                 return err;
741         }
742
743         if ((err = snd_als300_new_pcm(chip)) < 0) {
744                 snd_printk(KERN_WARNING "Could not create PCM\n");
745                 snd_als300_free(chip);
746                 return err;
747         }
748
749         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
750                                                 chip, &ops)) < 0) {
751                 snd_als300_free(chip);
752                 return err;
753         }
754
755         snd_card_set_dev(card, &pci->dev);
756
757         *rchip = chip;
758         snd_als300_dbgcallleave();
759         return 0;
760 }
761
762 #ifdef CONFIG_PM
763 static int snd_als300_suspend(struct pci_dev *pci, pm_message_t state)
764 {
765         struct snd_card *card = pci_get_drvdata(pci);
766         struct snd_als300 *chip = card->private_data;
767
768         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
769         snd_pcm_suspend_all(chip->pcm);
770         snd_ac97_suspend(chip->ac97);
771
772         pci_set_power_state(pci, PCI_D3hot);
773         pci_disable_device(pci);
774         pci_save_state(pci);
775         return 0;
776 }
777
778 static int snd_als300_resume(struct pci_dev *pci)
779 {
780         struct snd_card *card = pci_get_drvdata(pci);
781         struct snd_als300 *chip = card->private_data;
782
783         pci_restore_state(pci);
784         pci_enable_device(pci);
785         pci_set_power_state(pci, PCI_D0);
786         pci_set_master(pci);
787
788         snd_als300_init(chip);
789         snd_ac97_resume(chip->ac97);
790
791         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
792         return 0;
793 }
794 #endif
795
796 static int __devinit snd_als300_probe(struct pci_dev *pci,
797                              const struct pci_device_id *pci_id)
798 {
799         static int dev;
800         struct snd_card *card;
801         struct snd_als300 *chip;
802         int err, chip_type;
803
804         if (dev >= SNDRV_CARDS)
805                 return -ENODEV;
806         if (!enable[dev]) {
807                 dev++;
808                 return -ENOENT;
809         }
810
811         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
812
813         if (card == NULL)
814                 return -ENOMEM;
815
816         chip_type = pci_id->driver_data;
817
818         if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
819                 snd_card_free(card);
820                 return err;
821         }
822         card->private_data = chip;
823
824         strcpy(card->driver, "ALS300");
825         if (chip->chip_type == DEVICE_ALS300_PLUS)
826                 /* don't know much about ALS300+ yet
827                  * print revision number for now */
828                 sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
829         else
830                 sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
831                                                         chip->revision - 1);
832         sprintf(card->longname, "%s at 0x%lx irq %i",
833                                 card->shortname, chip->port, chip->irq);
834
835         if ((err = snd_card_register(card)) < 0) {
836                 snd_card_free(card);
837                 return err;
838         }
839         pci_set_drvdata(pci, card);
840         dev++;
841         return 0;
842 }
843
844 static struct pci_driver driver = {
845         .name = "ALS300",
846         .id_table = snd_als300_ids,
847         .probe = snd_als300_probe,
848         .remove = __devexit_p(snd_als300_remove),
849 #ifdef CONFIG_PM
850         .suspend = snd_als300_suspend,
851         .resume = snd_als300_resume,
852 #endif
853 };
854
855 static int __init alsa_card_als300_init(void)
856 {
857         return pci_register_driver(&driver);
858 }
859
860 static void __exit alsa_card_als300_exit(void)
861 {
862         pci_unregister_driver(&driver);
863 }
864
865 module_init(alsa_card_als300_init)
866 module_exit(alsa_card_als300_exit)