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