commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / sound / pci / fm801.c
1 /*
2  *  The driver for the ForteMedia FM801 based soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/ac97_codec.h>
31 #include <sound/mpu401.h>
32 #include <sound/opl3.h>
33 #define SNDRV_GET_ID
34 #include <sound/initval.h>
35
36 #include <asm/io.h>
37
38 #define chip_t fm801_t
39
40 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("ForteMedia FM801");
42 MODULE_LICENSE("GPL");
43 MODULE_CLASSES("{sound}");
44 MODULE_DEVICES("{{ForteMedia,FM801},"
45                 "{Genius,SoundMaker Live 5.1}}");
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
50
51 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
52 MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
53 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
54 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
55 MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
56 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
57 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
58 MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
59 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
60
61 /*
62  *  Direct registers
63  */
64
65 #define FM801_REG(chip, reg)    (chip->port + FM801_##reg)
66
67 #define FM801_PCM_VOL           0x00    /* PCM Output Volume */
68 #define FM801_FM_VOL            0x02    /* FM Output Volume */
69 #define FM801_I2S_VOL           0x04    /* I2S Volume */
70 #define FM801_REC_SRC           0x06    /* Record Source */
71 #define FM801_PLY_CTRL          0x08    /* Playback Control */
72 #define FM801_PLY_COUNT         0x0a    /* Playback Count */
73 #define FM801_PLY_BUF1          0x0c    /* Playback Bufer I */
74 #define FM801_PLY_BUF2          0x10    /* Playback Buffer II */
75 #define FM801_CAP_CTRL          0x14    /* Capture Control */
76 #define FM801_CAP_COUNT         0x16    /* Capture Count */
77 #define FM801_CAP_BUF1          0x18    /* Capture Buffer I */
78 #define FM801_CAP_BUF2          0x1c    /* Capture Buffer II */
79 #define FM801_CODEC_CTRL        0x22    /* Codec Control */
80 #define FM801_I2S_MODE          0x24    /* I2S Mode Control */
81 #define FM801_VOLUME            0x26    /* Volume Up/Down/Mute Status */
82 #define FM801_I2C_CTRL          0x29    /* I2C Control */
83 #define FM801_AC97_CMD          0x2a    /* AC'97 Command */
84 #define FM801_AC97_DATA         0x2c    /* AC'97 Data */
85 #define FM801_MPU401_DATA       0x30    /* MPU401 Data */
86 #define FM801_MPU401_CMD        0x31    /* MPU401 Command */
87 #define FM801_GPIO_CTRL         0x52    /* General Purpose I/O Control */
88 #define FM801_GEN_CTRL          0x54    /* General Control */
89 #define FM801_IRQ_MASK          0x56    /* Interrupt Mask */
90 #define FM801_IRQ_STATUS        0x5a    /* Interrupt Status */
91 #define FM801_OPL3_BANK0        0x68    /* OPL3 Status Read / Bank 0 Write */
92 #define FM801_OPL3_DATA0        0x69    /* OPL3 Data 0 Write */
93 #define FM801_OPL3_BANK1        0x6a    /* OPL3 Bank 1 Write */
94 #define FM801_OPL3_DATA1        0x6b    /* OPL3 Bank 1 Write */
95 #define FM801_POWERDOWN         0x70    /* Blocks Power Down Control */
96
97 #define FM801_AC97_ADDR_SHIFT   10
98
99 /* playback and record control register bits */
100 #define FM801_BUF1_LAST         (1<<1)
101 #define FM801_BUF2_LAST         (1<<2)
102 #define FM801_START             (1<<5)
103 #define FM801_PAUSE             (1<<6)
104 #define FM801_IMMED_STOP        (1<<7)
105 #define FM801_RATE_SHIFT        8
106 #define FM801_RATE_MASK         (15 << FM801_RATE_SHIFT)
107 #define FM801_CHANNELS_4        (1<<12) /* playback only */
108 #define FM801_CHANNELS_6        (2<<12) /* playback only */
109 #define FM801_CHANNELS_6MS      (3<<12) /* playback only */
110 #define FM801_CHANNELS_MASK     (3<<12)
111 #define FM801_16BIT             (1<<14)
112 #define FM801_STEREO            (1<<15)
113
114 /* IRQ status bits */
115 #define FM801_IRQ_PLAYBACK      (1<<8)
116 #define FM801_IRQ_CAPTURE       (1<<9)
117 #define FM801_IRQ_VOLUME        (1<<14)
118 #define FM801_IRQ_MPU           (1<<15)
119         
120 /*
121
122  */
123
124 typedef struct _snd_fm801 fm801_t;
125
126 struct _snd_fm801 {
127         int irq;
128
129         unsigned long port;     /* I/O port number */
130         struct resource *res_port;
131         unsigned int multichannel: 1,   /* multichannel support */
132                      secondary: 1;      /* secondary codec */
133         unsigned char secondary_addr;   /* address of the secondary codec */
134
135         unsigned short ply_ctrl; /* playback control */
136         unsigned short cap_ctrl; /* capture control */
137
138         unsigned long ply_buffer;
139         unsigned int ply_buf;
140         unsigned int ply_count;
141         unsigned int ply_size;
142         unsigned int ply_pos;
143
144         unsigned long cap_buffer;
145         unsigned int cap_buf;
146         unsigned int cap_count;
147         unsigned int cap_size;
148         unsigned int cap_pos;
149
150         ac97_t *ac97;
151         ac97_t *ac97_sec;
152
153         struct pci_dev *pci;
154         snd_card_t *card;
155         snd_pcm_t *pcm;
156         snd_rawmidi_t *rmidi;
157         snd_pcm_substream_t *playback_substream;
158         snd_pcm_substream_t *capture_substream;
159         unsigned int p_dma_size;
160         unsigned int c_dma_size;
161
162         spinlock_t reg_lock;
163         snd_info_entry_t *proc_entry;
164 };
165
166 static struct pci_device_id snd_fm801_ids[] = {
167         { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
168         { 0, }
169 };
170
171 MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
172
173 /*
174  *  common I/O routines
175  */
176
177 static int snd_fm801_update_bits(fm801_t *chip, unsigned short reg,
178                                  unsigned short mask, unsigned short value)
179 {
180         int change;
181         unsigned short old, new;
182
183         spin_lock(&chip->reg_lock);
184         old = inw(chip->port + reg);
185         new = (old & ~mask) | value;
186         change = old != new;
187         if (change)
188                 outw(new, chip->port + reg);
189         spin_unlock(&chip->reg_lock);
190         return change;
191 }
192
193 static void snd_fm801_codec_write(ac97_t *ac97,
194                                   unsigned short reg,
195                                   unsigned short val)
196 {
197         fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
198         int idx;
199
200         /*
201          *  Wait until the codec interface is not ready..
202          */
203         for (idx = 0; idx < 100; idx++) {
204                 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
205                         goto ok1;
206                 udelay(10);
207         }
208         snd_printk("AC'97 interface is busy (1)\n");
209         return;
210
211  ok1:
212         /* write data and address */
213         outw(val, FM801_REG(chip, AC97_DATA));
214         outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
215         /*
216          *  Wait until the write command is not completed..
217          */
218         for (idx = 0; idx < 1000; idx++) {
219                 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
220                         return;
221                 udelay(10);
222         }
223         snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
224 }
225
226 static unsigned short snd_fm801_codec_read(ac97_t *ac97, unsigned short reg)
227 {
228         fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return -ENXIO);
229         int idx;
230
231         /*
232          *  Wait until the codec interface is not ready..
233          */
234         for (idx = 0; idx < 100; idx++) {
235                 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
236                         goto ok1;
237                 udelay(10);
238         }
239         snd_printk("AC'97 interface is busy (1)\n");
240         return 0;
241
242  ok1:
243         /* read command */
244         outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | (1<<7), FM801_REG(chip, AC97_CMD));
245         for (idx = 0; idx < 100; idx++) {
246                 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
247                         goto ok2;
248                 udelay(10);
249         }
250         snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
251         return 0;
252
253  ok2:
254         for (idx = 0; idx < 1000; idx++) {
255                 if (inw(FM801_REG(chip, AC97_CMD)) & (1<<8))
256                         goto ok3;
257                 udelay(10);
258         }
259         snd_printk("AC'97 interface #%d is not valid (2)\n", ac97->num);
260         return 0;
261
262  ok3:
263         return inw(FM801_REG(chip, AC97_DATA));
264 }
265
266 static unsigned int rates[] = {
267   5500,  8000,  9600, 11025,
268   16000, 19200, 22050, 32000,
269   38400, 44100, 48000
270 };
271
272 #define RATES sizeof(rates) / sizeof(rates[0])
273
274 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
275         .count = RATES,
276         .list = rates,
277         .mask = 0,
278 };
279
280 static unsigned int channels[] = {
281   2, 4, 6
282 };
283
284 #define CHANNELS sizeof(channels) / sizeof(channels[0])
285
286 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
287         .count = CHANNELS,
288         .list = channels,
289         .mask = 0,
290 };
291
292 /*
293  *  Sample rate routines
294  */
295
296 static unsigned short snd_fm801_rate_bits(unsigned int rate)
297 {
298         unsigned int idx;
299
300         for (idx = 0; idx < 11; idx++)
301                 if (rates[idx] == rate)
302                         return idx;
303         snd_BUG();
304         return RATES - 1;
305 }
306
307 /*
308  *  PCM part
309  */
310
311 static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
312                                       int cmd)
313 {
314         fm801_t *chip = snd_pcm_substream_chip(substream);
315
316         spin_lock(&chip->reg_lock);
317         switch (cmd) {
318         case SNDRV_PCM_TRIGGER_START:
319                 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
320                                      FM801_BUF2_LAST |
321                                      FM801_PAUSE);
322                 chip->ply_ctrl |= FM801_START |
323                                    FM801_IMMED_STOP;
324                 break;
325         case SNDRV_PCM_TRIGGER_STOP:
326                 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
327                 break;
328         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
329                 chip->ply_ctrl |= FM801_PAUSE;
330                 break;
331         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
332                 chip->ply_ctrl &= ~FM801_PAUSE;
333                 break;
334         default:
335                 spin_unlock(&chip->reg_lock);
336                 snd_BUG();
337                 return -EINVAL;
338         }
339         outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
340         spin_unlock(&chip->reg_lock);
341         return 0;
342 }
343
344 static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
345                                      int cmd)
346 {
347         fm801_t *chip = snd_pcm_substream_chip(substream);
348
349         spin_lock(&chip->reg_lock);
350         switch (cmd) {
351         case SNDRV_PCM_TRIGGER_START:
352                 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
353                                      FM801_BUF2_LAST |
354                                      FM801_PAUSE);
355                 chip->cap_ctrl |= FM801_START |
356                                    FM801_IMMED_STOP;
357                 break;
358         case SNDRV_PCM_TRIGGER_STOP:
359                 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
360                 break;
361         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
362                 chip->cap_ctrl |= FM801_PAUSE;
363                 break;
364         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
365                 chip->cap_ctrl &= ~FM801_PAUSE;
366                 break;
367         default:
368                 spin_unlock(&chip->reg_lock);
369                 snd_BUG();
370                 return -EINVAL;
371         }
372         outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
373         spin_unlock(&chip->reg_lock);
374         return 0;
375 }
376
377 static int snd_fm801_hw_params(snd_pcm_substream_t * substream,
378                                snd_pcm_hw_params_t * hw_params)
379 {
380         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
381 }
382
383 static int snd_fm801_hw_free(snd_pcm_substream_t * substream)
384 {
385         return snd_pcm_lib_free_pages(substream);
386 }
387
388 static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
389 {
390         unsigned long flags;
391         fm801_t *chip = snd_pcm_substream_chip(substream);
392         snd_pcm_runtime_t *runtime = substream->runtime;
393
394         chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
395         chip->ply_count = snd_pcm_lib_period_bytes(substream);
396         spin_lock_irqsave(&chip->reg_lock, flags);
397         chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
398                              FM801_STEREO | FM801_RATE_MASK |
399                              FM801_CHANNELS_MASK);
400         if (snd_pcm_format_width(runtime->format) == 16)
401                 chip->ply_ctrl |= FM801_16BIT;
402         if (runtime->channels > 1) {
403                 chip->ply_ctrl |= FM801_STEREO;
404                 if (runtime->channels == 4)
405                         chip->ply_ctrl |= FM801_CHANNELS_4;
406                 else if (runtime->channels == 6)
407                         chip->ply_ctrl |= FM801_CHANNELS_6;
408         }
409         chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
410         chip->ply_buf = 0;
411         outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
412         outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
413         chip->ply_buffer = runtime->dma_addr;
414         chip->ply_pos = 0;
415         outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
416         outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
417         spin_unlock_irqrestore(&chip->reg_lock, flags);
418         return 0;
419 }
420
421 static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
422 {
423         unsigned long flags;
424         fm801_t *chip = snd_pcm_substream_chip(substream);
425         snd_pcm_runtime_t *runtime = substream->runtime;
426
427         chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
428         chip->cap_count = snd_pcm_lib_period_bytes(substream);
429         spin_lock_irqsave(&chip->reg_lock, flags);
430         chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
431                              FM801_STEREO | FM801_RATE_MASK);
432         if (snd_pcm_format_width(runtime->format) == 16)
433                 chip->cap_ctrl |= FM801_16BIT;
434         if (runtime->channels > 1)
435                 chip->cap_ctrl |= FM801_STEREO;
436         chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
437         chip->cap_buf = 0;
438         outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
439         outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
440         chip->cap_buffer = runtime->dma_addr;
441         chip->cap_pos = 0;
442         outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
443         outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
444         spin_unlock_irqrestore(&chip->reg_lock, flags);
445         return 0;
446 }
447
448 static snd_pcm_uframes_t snd_fm801_playback_pointer(snd_pcm_substream_t * substream)
449 {
450         fm801_t *chip = snd_pcm_substream_chip(substream);
451         unsigned long flags;
452         size_t ptr;
453
454         if (!(chip->ply_ctrl & FM801_START))
455                 return 0;
456         spin_lock_irqsave(&chip->reg_lock, flags);
457         ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
458         if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
459                 ptr += chip->ply_count;
460                 ptr %= chip->ply_size;
461         }
462         spin_unlock_irqrestore(&chip->reg_lock, flags);
463         return bytes_to_frames(substream->runtime, ptr);
464 }
465
466 static snd_pcm_uframes_t snd_fm801_capture_pointer(snd_pcm_substream_t * substream)
467 {
468         fm801_t *chip = snd_pcm_substream_chip(substream);
469         unsigned long flags;
470         size_t ptr;
471
472         if (!(chip->cap_ctrl & FM801_START))
473                 return 0;
474         spin_lock_irqsave(&chip->reg_lock, flags);
475         ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
476         if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
477                 ptr += chip->cap_count;
478                 ptr %= chip->cap_size;
479         }
480         spin_unlock_irqrestore(&chip->reg_lock, flags);
481         return bytes_to_frames(substream->runtime, ptr);
482 }
483
484 static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
485 {
486         fm801_t *chip = snd_magic_cast(fm801_t, dev_id, return IRQ_NONE);
487         unsigned short status;
488         unsigned int tmp;
489
490         status = inw(FM801_REG(chip, IRQ_STATUS));
491         status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
492         if (! status)
493                 return IRQ_NONE;
494         /* ack first */
495         outw(status, FM801_REG(chip, IRQ_STATUS));
496         if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
497                 spin_lock(&chip->reg_lock);
498                 chip->ply_buf++;
499                 chip->ply_pos += chip->ply_count;
500                 chip->ply_pos %= chip->ply_size;
501                 tmp = chip->ply_pos + chip->ply_count;
502                 tmp %= chip->ply_size;
503                 outl(chip->ply_buffer + tmp,
504                                 (chip->ply_buf & 1) ?
505                                         FM801_REG(chip, PLY_BUF1) :
506                                         FM801_REG(chip, PLY_BUF2));
507                 spin_unlock(&chip->reg_lock);
508                 snd_pcm_period_elapsed(chip->playback_substream);
509         }
510         if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
511                 spin_lock(&chip->reg_lock);
512                 chip->cap_buf++;
513                 chip->cap_pos += chip->cap_count;
514                 chip->cap_pos %= chip->cap_size;
515                 tmp = chip->cap_pos + chip->cap_count;
516                 tmp %= chip->cap_size;
517                 outl(chip->cap_buffer + tmp,
518                                 (chip->cap_buf & 1) ?
519                                         FM801_REG(chip, CAP_BUF1) :
520                                         FM801_REG(chip, CAP_BUF2));
521                 spin_unlock(&chip->reg_lock);
522                 snd_pcm_period_elapsed(chip->capture_substream);
523         }
524         if (chip->rmidi && (status & FM801_IRQ_MPU))
525                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
526         if (status & FM801_IRQ_VOLUME)
527                 ;/* TODO */
528
529         return IRQ_HANDLED;
530 }
531
532 static snd_pcm_hardware_t snd_fm801_playback =
533 {
534         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
535                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
536                                  SNDRV_PCM_INFO_PAUSE |
537                                  SNDRV_PCM_INFO_MMAP_VALID),
538         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
539         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
540         .rate_min =             5500,
541         .rate_max =             48000,
542         .channels_min =         1,
543         .channels_max =         2,
544         .buffer_bytes_max =     (128*1024),
545         .period_bytes_min =     64,
546         .period_bytes_max =     (128*1024),
547         .periods_min =          1,
548         .periods_max =          1024,
549         .fifo_size =            0,
550 };
551
552 static snd_pcm_hardware_t snd_fm801_capture =
553 {
554         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
555                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
556                                  SNDRV_PCM_INFO_PAUSE |
557                                  SNDRV_PCM_INFO_MMAP_VALID),
558         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
559         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
560         .rate_min =             5500,
561         .rate_max =             48000,
562         .channels_min =         1,
563         .channels_max =         2,
564         .buffer_bytes_max =     (128*1024),
565         .period_bytes_min =     64,
566         .period_bytes_max =     (128*1024),
567         .periods_min =          1,
568         .periods_max =          1024,
569         .fifo_size =            0,
570 };
571
572 static int snd_fm801_playback_open(snd_pcm_substream_t * substream)
573 {
574         fm801_t *chip = snd_pcm_substream_chip(substream);
575         snd_pcm_runtime_t *runtime = substream->runtime;
576         int err;
577
578         chip->playback_substream = substream;
579         runtime->hw = snd_fm801_playback;
580         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
581         if (chip->multichannel) {
582                 runtime->hw.channels_max = 6;
583                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
584         }
585         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
586                 return err;
587         return 0;
588 }
589
590 static int snd_fm801_capture_open(snd_pcm_substream_t * substream)
591 {
592         fm801_t *chip = snd_pcm_substream_chip(substream);
593         snd_pcm_runtime_t *runtime = substream->runtime;
594         int err;
595
596         chip->capture_substream = substream;
597         runtime->hw = snd_fm801_capture;
598         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
599         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
600                 return err;
601         return 0;
602 }
603
604 static int snd_fm801_playback_close(snd_pcm_substream_t * substream)
605 {
606         fm801_t *chip = snd_pcm_substream_chip(substream);
607
608         chip->playback_substream = NULL;
609         return 0;
610 }
611
612 static int snd_fm801_capture_close(snd_pcm_substream_t * substream)
613 {
614         fm801_t *chip = snd_pcm_substream_chip(substream);
615
616         chip->capture_substream = NULL;
617         return 0;
618 }
619
620 static snd_pcm_ops_t snd_fm801_playback_ops = {
621         .open =         snd_fm801_playback_open,
622         .close =        snd_fm801_playback_close,
623         .ioctl =        snd_pcm_lib_ioctl,
624         .hw_params =    snd_fm801_hw_params,
625         .hw_free =      snd_fm801_hw_free,
626         .prepare =      snd_fm801_playback_prepare,
627         .trigger =      snd_fm801_playback_trigger,
628         .pointer =      snd_fm801_playback_pointer,
629 };
630
631 static snd_pcm_ops_t snd_fm801_capture_ops = {
632         .open =         snd_fm801_capture_open,
633         .close =        snd_fm801_capture_close,
634         .ioctl =        snd_pcm_lib_ioctl,
635         .hw_params =    snd_fm801_hw_params,
636         .hw_free =      snd_fm801_hw_free,
637         .prepare =      snd_fm801_capture_prepare,
638         .trigger =      snd_fm801_capture_trigger,
639         .pointer =      snd_fm801_capture_pointer,
640 };
641
642 static void snd_fm801_pcm_free(snd_pcm_t *pcm)
643 {
644         fm801_t *chip = snd_magic_cast(fm801_t, pcm->private_data, return);
645         chip->pcm = NULL;
646         snd_pcm_lib_preallocate_free_for_all(pcm);
647 }
648
649 static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
650 {
651         snd_pcm_t *pcm;
652         int err;
653
654         if (rpcm)
655                 *rpcm = NULL;
656         if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
657                 return err;
658
659         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
660         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
661
662         pcm->private_data = chip;
663         pcm->private_free = snd_fm801_pcm_free;
664         pcm->info_flags = 0;
665         strcpy(pcm->name, "FM801");
666         chip->pcm = pcm;
667
668         snd_pcm_lib_preallocate_pci_pages_for_all(chip->pci, pcm, chip->multichannel ? 128*1024 : 64*1024, 128*1024);
669
670         if (rpcm)
671                 *rpcm = pcm;
672         return 0;
673 }
674
675 /*
676  *  Mixer routines
677  */
678
679 #define FM801_SINGLE(xname, reg, shift, mask, invert) \
680 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
681   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
682   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
683
684 static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
685 {
686         int mask = (kcontrol->private_value >> 16) & 0xff;
687
688         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
689         uinfo->count = 1;
690         uinfo->value.integer.min = 0;
691         uinfo->value.integer.max = mask;
692         return 0;
693 }
694
695 static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
696 {
697         fm801_t *chip = snd_kcontrol_chip(kcontrol);
698         int reg = kcontrol->private_value & 0xff;
699         int shift = (kcontrol->private_value >> 8) & 0xff;
700         int mask = (kcontrol->private_value >> 16) & 0xff;
701         int invert = (kcontrol->private_value >> 24) & 0xff;
702
703         ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
704         if (invert)
705                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
706         return 0;
707 }
708
709 static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
710 {
711         fm801_t *chip = snd_kcontrol_chip(kcontrol);
712         int reg = kcontrol->private_value & 0xff;
713         int shift = (kcontrol->private_value >> 8) & 0xff;
714         int mask = (kcontrol->private_value >> 16) & 0xff;
715         int invert = (kcontrol->private_value >> 24) & 0xff;
716         unsigned short val;
717
718         val = (ucontrol->value.integer.value[0] & mask);
719         if (invert)
720                 val = mask - val;
721         return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
722 }
723
724 #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
725 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
726   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
727   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
728
729 static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
730 {
731         int mask = (kcontrol->private_value >> 16) & 0xff;
732
733         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
734         uinfo->count = 2;
735         uinfo->value.integer.min = 0;
736         uinfo->value.integer.max = mask;
737         return 0;
738 }
739
740 static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
741 {
742         fm801_t *chip = snd_kcontrol_chip(kcontrol);
743         int reg = kcontrol->private_value & 0xff;
744         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
745         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
746         int mask = (kcontrol->private_value >> 16) & 0xff;
747         int invert = (kcontrol->private_value >> 24) & 0xff;
748
749         spin_lock(&chip->reg_lock);
750         ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
751         ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
752         spin_unlock(&chip->reg_lock);
753         if (invert) {
754                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
755                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
756         }
757         return 0;
758 }
759
760 static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
761 {
762         fm801_t *chip = snd_kcontrol_chip(kcontrol);
763         int reg = kcontrol->private_value & 0xff;
764         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
765         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
766         int mask = (kcontrol->private_value >> 16) & 0xff;
767         int invert = (kcontrol->private_value >> 24) & 0xff;
768         unsigned short val1, val2;
769  
770         val1 = ucontrol->value.integer.value[0] & mask;
771         val2 = ucontrol->value.integer.value[1] & mask;
772         if (invert) {
773                 val1 = mask - val1;
774                 val2 = mask - val2;
775         }
776         return snd_fm801_update_bits(chip, reg,
777                                      (mask << shift_left) | (mask << shift_right),
778                                      (val1 << shift_left ) | (val2 << shift_right));
779 }
780
781 static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
782 {
783         static char *texts[5] = {
784                 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
785         };
786  
787         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
788         uinfo->count = 1;
789         uinfo->value.enumerated.items = 5;
790         if (uinfo->value.enumerated.item > 4)
791                 uinfo->value.enumerated.item = 4;
792         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
793         return 0;
794 }
795
796 static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
797 {
798         fm801_t *chip = snd_kcontrol_chip(kcontrol);
799         unsigned short val;
800  
801         val = inw(FM801_REG(chip, REC_SRC)) & 7;
802         if (val > 4)
803                 val = 4;
804         ucontrol->value.enumerated.item[0] = val;
805         return 0;
806 }
807
808 static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
809 {
810         fm801_t *chip = snd_kcontrol_chip(kcontrol);
811         unsigned short val;
812  
813         if ((val = ucontrol->value.enumerated.item[0]) > 4)
814                 return -EINVAL;
815         return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
816 }
817
818 #define FM801_CONTROLS (sizeof(snd_fm801_controls)/sizeof(snd_kcontrol_new_t))
819
820 static snd_kcontrol_new_t snd_fm801_controls[] __devinitdata = {
821 FM801_DOUBLE("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1),
822 FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
823 FM801_DOUBLE("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1),
824 FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
825 FM801_DOUBLE("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1),
826 FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
827 {
828         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829         .name = "Digital Capture Source",
830         .info = snd_fm801_info_mux,
831         .get = snd_fm801_get_mux,
832         .put = snd_fm801_put_mux,
833 }
834 };
835
836 #define FM801_CONTROLS_MULTI (sizeof(snd_fm801_controls_multi)/sizeof(snd_kcontrol_new_t))
837
838 static snd_kcontrol_new_t snd_fm801_controls_multi[] __devinitdata = {
839 FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
840 FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
841 FM801_SINGLE("IEC958 Capture Switch", FM801_I2S_MODE, 8, 1, 0),
842 FM801_SINGLE("IEC958 Raw Data Playback Switch", FM801_I2S_MODE, 9, 1, 0),
843 FM801_SINGLE("IEC958 Raw Data Capture Switch", FM801_I2S_MODE, 10, 1, 0),
844 FM801_SINGLE("IEC958 Playback Switch", FM801_GEN_CTRL, 2, 1, 0),
845 };
846
847 static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
848 {
849         fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
850         if (ac97->num == 0) {
851                 chip->ac97 = NULL;
852         } else {
853                 chip->ac97_sec = NULL;
854         }
855 }
856
857 static int __devinit snd_fm801_mixer(fm801_t *chip)
858 {
859         ac97_t ac97;
860         unsigned int i;
861         int err;
862
863         memset(&ac97, 0, sizeof(ac97));
864         ac97.write = snd_fm801_codec_write;
865         ac97.read = snd_fm801_codec_read;
866         ac97.private_data = chip;
867         ac97.private_free = snd_fm801_mixer_free_ac97;
868         if ((err = snd_ac97_mixer(chip->card, &ac97, &chip->ac97)) < 0)
869                 return err;
870         if (chip->secondary) {
871                 ac97.num = 1;
872                 ac97.addr = chip->secondary_addr;
873                 if ((err = snd_ac97_mixer(chip->card, &ac97, &chip->ac97_sec)) < 0)
874                         return err;
875         }
876         for (i = 0; i < FM801_CONTROLS; i++)
877                 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
878         if (chip->multichannel) {
879                 for (i = 0; i < FM801_CONTROLS_MULTI; i++)
880                         snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
881         }
882         return 0;
883 }
884
885 /*
886  *  initialization routines
887  */
888
889 static int snd_fm801_free(fm801_t *chip)
890 {
891         unsigned short cmdw;
892
893         if (chip->irq < 0)
894                 goto __end_hw;
895
896         /* interrupt setup - mask everything */
897         cmdw = inw(FM801_REG(chip, IRQ_MASK));
898         cmdw |= 0x00c3;
899         outw(cmdw, FM801_REG(chip, IRQ_MASK));
900
901       __end_hw:
902         if (chip->res_port) {
903                 release_resource(chip->res_port);
904                 kfree_nocheck(chip->res_port);
905         }
906         if (chip->irq >= 0)
907                 free_irq(chip->irq, (void *)chip);
908
909         snd_magic_kfree(chip);
910         return 0;
911 }
912
913 static int snd_fm801_dev_free(snd_device_t *device)
914 {
915         fm801_t *chip = snd_magic_cast(fm801_t, device->device_data, return -ENXIO);
916         return snd_fm801_free(chip);
917 }
918
919 static int __devinit snd_fm801_create(snd_card_t * card,
920                                    struct pci_dev * pci,
921                                    fm801_t ** rchip)
922 {
923         fm801_t *chip;
924         unsigned char rev, id;
925         unsigned short cmdw;
926         unsigned long timeout;
927         int err;
928         static snd_device_ops_t ops = {
929                 .dev_free =     snd_fm801_dev_free,
930         };
931
932         *rchip = NULL;
933         if ((err = pci_enable_device(pci)) < 0)
934                 return err;
935         chip = snd_magic_kcalloc(fm801_t, 0, GFP_KERNEL);
936         if (chip == NULL)
937                 return -ENOMEM;
938         spin_lock_init(&chip->reg_lock);
939         chip->card = card;
940         chip->pci = pci;
941         chip->irq = -1;
942         chip->port = pci_resource_start(pci, 0);
943         if ((chip->res_port = request_region(chip->port, 0x80, "FM801")) == NULL) {
944                 snd_printk("unable to grab region 0x%lx-0x%lx\n", chip->port, chip->port + 0x80 - 1);
945                 snd_fm801_free(chip);
946                 return -EBUSY;
947         }
948         if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ, "FM801", (void *)chip)) {
949                 snd_printk("unable to grab IRQ %d\n", chip->irq);
950                 snd_fm801_free(chip);
951                 return -EBUSY;
952         }
953         chip->irq = pci->irq;
954         pci_set_master(pci);
955
956         pci_read_config_byte(pci, PCI_REVISION_ID, &rev);
957         if (rev >= 0xb1)        /* FM801-AU */
958                 chip->multichannel = 1;
959
960         /* codec cold reset + AC'97 warm reset */
961         outw((1<<5)|(1<<6), FM801_REG(chip, CODEC_CTRL));
962         inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
963         udelay(100);
964         outw(0, FM801_REG(chip, CODEC_CTRL));
965
966         timeout = (jiffies + (3 * HZ) / 4) + 1;         /* min 750ms */
967
968         outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
969         udelay(5);
970         do {
971                 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
972                         goto __ac97_secondary;
973                 set_current_state(TASK_UNINTERRUPTIBLE);
974                 schedule_timeout(1);
975         } while (time_after(timeout, jiffies));
976         snd_printk("Primary AC'97 codec not found\n");
977         snd_fm801_free(chip);
978         return -EIO;
979
980       __ac97_secondary:
981         if (!chip->multichannel)        /* lookup is not required */
982                 goto __ac97_ok;
983         for (id = 3; id > 0; id--) {    /* my card has the secondary codec */
984                                         /* at address #3, so the loop is inverted */
985
986                 timeout = jiffies + HZ / 20;
987
988                 outw((1<<7) | (id << FM801_AC97_ADDR_SHIFT) | AC97_VENDOR_ID1, FM801_REG(chip, AC97_CMD));
989                 udelay(5);
990                 do {
991                         if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8)) {
992                                 cmdw = inw(FM801_REG(chip, AC97_DATA));
993                                 if (cmdw != 0xffff && cmdw != 0) {
994                                         chip->secondary = 1;
995                                         chip->secondary_addr = id;
996                                         goto __ac97_ok;
997                                 }
998                         }
999                         set_current_state(TASK_UNINTERRUPTIBLE);
1000                         schedule_timeout(1);
1001                 } while (time_after(timeout, jiffies));
1002         }
1003
1004         /* the recovery phase, it seems that probing for non-existing codec might */
1005         /* cause timeout problems */
1006         timeout = (jiffies + (3 * HZ) / 4) + 1;         /* min 750ms */
1007
1008         outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
1009         udelay(5);
1010         do {
1011                 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
1012                         goto __ac97_ok;
1013                 set_current_state(TASK_UNINTERRUPTIBLE);
1014                 schedule_timeout(1);
1015         } while (time_after(timeout, jiffies));
1016         snd_printk("Primary AC'97 codec not responding\n");
1017         snd_fm801_free(chip);
1018         return -EIO;
1019
1020       __ac97_ok:
1021
1022         /* init volume */
1023         outw(0x0808, FM801_REG(chip, PCM_VOL));
1024         outw(0x9f1f, FM801_REG(chip, FM_VOL));
1025         outw(0x8808, FM801_REG(chip, I2S_VOL));
1026
1027         /* I2S control - I2S mode */
1028         outw(0x0003, FM801_REG(chip, I2S_MODE));
1029
1030         /* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
1031         cmdw = inw(FM801_REG(chip, IRQ_MASK));
1032         cmdw &= ~0x0083;
1033         outw(cmdw, FM801_REG(chip, IRQ_MASK));
1034
1035         /* interrupt clear */
1036         outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1037
1038         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1039                 snd_fm801_free(chip);
1040                 return err;
1041         }
1042
1043         *rchip = chip;
1044         return 0;
1045 }
1046
1047 static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
1048                                           const struct pci_device_id *pci_id)
1049 {
1050         static int dev;
1051         snd_card_t *card;
1052         fm801_t *chip;
1053         opl3_t *opl3;
1054         int err;
1055
1056         if (dev >= SNDRV_CARDS)
1057                 return -ENODEV;
1058         if (!enable[dev]) {
1059                 dev++;
1060                 return -ENOENT;
1061         }
1062
1063         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1064         if (card == NULL)
1065                 return -ENOMEM;
1066         if ((err = snd_fm801_create(card, pci, &chip)) < 0) {
1067                 snd_card_free(card);
1068                 return err;
1069         }
1070         if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
1071                 snd_card_free(card);
1072                 return err;
1073         }
1074         if ((err = snd_fm801_mixer(chip)) < 0) {
1075                 snd_card_free(card);
1076                 return err;
1077         }
1078         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1079                                        FM801_REG(chip, MPU401_DATA), 1,
1080                                        chip->irq, 0, &chip->rmidi)) < 0) {
1081                 snd_card_free(card);
1082                 return err;
1083         }
1084         if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
1085                                    FM801_REG(chip, OPL3_BANK1),
1086                                    OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1087                 snd_card_free(card);
1088                 return err;
1089         }
1090         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1091                 snd_card_free(card);
1092                 return err;
1093         }
1094
1095         strcpy(card->driver, "FM801");
1096         strcpy(card->shortname, "ForteMedia FM801-");
1097         strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1098         sprintf(card->longname, "%s at 0x%lx, irq %i",
1099                 card->shortname, chip->port, chip->irq);
1100
1101         if ((err = snd_card_register(card)) < 0) {
1102                 snd_card_free(card);
1103                 return err;
1104         }
1105         pci_set_drvdata(pci, card);
1106         dev++;
1107         return 0;
1108 }
1109
1110 static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
1111 {
1112         snd_card_free(pci_get_drvdata(pci));
1113         pci_set_drvdata(pci, NULL);
1114 }
1115
1116 static struct pci_driver driver = {
1117         .name = "FM801",
1118         .id_table = snd_fm801_ids,
1119         .probe = snd_card_fm801_probe,
1120         .remove = __devexit_p(snd_card_fm801_remove),
1121 };
1122
1123 static int __init alsa_card_fm801_init(void)
1124 {
1125         int err;
1126
1127         if ((err = pci_module_init(&driver)) < 0) {
1128 #ifdef MODULE
1129                 printk(KERN_ERR "ForteMedia FM801 soundcard not found or device busy\n");
1130 #endif
1131                 return err;
1132         }
1133         return 0;
1134 }
1135
1136 static void __exit alsa_card_fm801_exit(void)
1137 {
1138         pci_unregister_driver(&driver);
1139 }
1140
1141 module_init(alsa_card_fm801_init)
1142 module_exit(alsa_card_fm801_exit)
1143
1144 #ifndef MODULE
1145
1146 /* format is: snd-fm801=enable,index,id */
1147
1148 static int __init alsa_card_fm801_setup(char *str)
1149 {
1150         static unsigned __initdata nr_dev = 0;
1151
1152         if (nr_dev >= SNDRV_CARDS)
1153                 return 0;
1154         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
1155                get_option(&str,&index[nr_dev]) == 2 &&
1156                get_id(&str,&id[nr_dev]) == 2);
1157         nr_dev++;
1158         return 1;
1159 }
1160
1161 __setup("snd-fm801=", alsa_card_fm801_setup);
1162
1163 #endif /* ifndef MODULE */