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