[ALSA] oxygen: more initialization
[safe/jmp/linux-2.6] / sound / pci / oxygen / oxygen_pcm.c
1 /*
2  * C-Media CMI8788 driver - PCM code
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 2.
9  *
10  *  This driver 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 driver; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 #include <linux/pci.h>
21 #include <sound/control.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include "oxygen.h"
26
27 static const struct snd_pcm_hardware oxygen_stereo_hardware = {
28         .info = SNDRV_PCM_INFO_MMAP |
29                 SNDRV_PCM_INFO_MMAP_VALID |
30                 SNDRV_PCM_INFO_INTERLEAVED |
31                 SNDRV_PCM_INFO_PAUSE |
32                 SNDRV_PCM_INFO_SYNC_START,
33         .formats = SNDRV_PCM_FMTBIT_S16_LE |
34                    SNDRV_PCM_FMTBIT_S32_LE,
35         .rates = SNDRV_PCM_RATE_32000 |
36                  SNDRV_PCM_RATE_44100 |
37                  SNDRV_PCM_RATE_48000 |
38                  SNDRV_PCM_RATE_64000 |
39                  SNDRV_PCM_RATE_88200 |
40                  SNDRV_PCM_RATE_96000 |
41                  SNDRV_PCM_RATE_176400 |
42                  SNDRV_PCM_RATE_192000,
43         .rate_min = 32000,
44         .rate_max = 192000,
45         .channels_min = 2,
46         .channels_max = 2,
47         .buffer_bytes_max = 256 * 1024,
48         .period_bytes_min = 128,
49         .period_bytes_max = 128 * 1024,
50         .periods_min = 2,
51         .periods_max = 2048,
52 };
53 static const struct snd_pcm_hardware oxygen_multichannel_hardware = {
54         .info = SNDRV_PCM_INFO_MMAP |
55                 SNDRV_PCM_INFO_MMAP_VALID |
56                 SNDRV_PCM_INFO_INTERLEAVED |
57                 SNDRV_PCM_INFO_PAUSE |
58                 SNDRV_PCM_INFO_SYNC_START,
59         .formats = SNDRV_PCM_FMTBIT_S16_LE |
60                    SNDRV_PCM_FMTBIT_S32_LE,
61         .rates = SNDRV_PCM_RATE_32000 |
62                  SNDRV_PCM_RATE_44100 |
63                  SNDRV_PCM_RATE_48000 |
64                  SNDRV_PCM_RATE_64000 |
65                  SNDRV_PCM_RATE_88200 |
66                  SNDRV_PCM_RATE_96000 |
67                  SNDRV_PCM_RATE_176400 |
68                  SNDRV_PCM_RATE_192000,
69         .rate_min = 32000,
70         .rate_max = 192000,
71         .channels_min = 2,
72         .channels_max = 8,
73         .buffer_bytes_max = 2048 * 1024,
74         .period_bytes_min = 128,
75         .period_bytes_max = 256 * 1024,
76         .periods_min = 2,
77         .periods_max = 16384,
78 };
79 static const struct snd_pcm_hardware oxygen_ac97_hardware = {
80         .info = SNDRV_PCM_INFO_MMAP |
81                 SNDRV_PCM_INFO_MMAP_VALID |
82                 SNDRV_PCM_INFO_INTERLEAVED |
83                 SNDRV_PCM_INFO_PAUSE |
84                 SNDRV_PCM_INFO_SYNC_START,
85         .formats = SNDRV_PCM_FMTBIT_S16_LE,
86         .rates = SNDRV_PCM_RATE_48000,
87         .rate_min = 48000,
88         .rate_max = 48000,
89         .channels_min = 2,
90         .channels_max = 2,
91         .buffer_bytes_max = 256 * 1024,
92         .period_bytes_min = 128,
93         .period_bytes_max = 128 * 1024,
94         .periods_min = 2,
95         .periods_max = 2048,
96 };
97
98 static const struct snd_pcm_hardware *const oxygen_hardware[PCM_COUNT] = {
99         [PCM_A] = &oxygen_stereo_hardware,
100         [PCM_B] = &oxygen_stereo_hardware,
101         [PCM_C] = &oxygen_stereo_hardware,
102         [PCM_SPDIF] = &oxygen_stereo_hardware,
103         [PCM_MULTICH] = &oxygen_multichannel_hardware,
104         [PCM_AC97] = &oxygen_ac97_hardware,
105 };
106
107 static inline unsigned int
108 oxygen_substream_channel(struct snd_pcm_substream *substream)
109 {
110         return (unsigned int)(uintptr_t)substream->runtime->private_data;
111 }
112
113 static int oxygen_open(struct snd_pcm_substream *substream,
114                        unsigned int channel)
115 {
116         struct oxygen *chip = snd_pcm_substream_chip(substream);
117         struct snd_pcm_runtime *runtime = substream->runtime;
118         int err;
119
120         runtime->private_data = (void *)(uintptr_t)channel;
121         runtime->hw = *oxygen_hardware[channel];
122         switch (channel) {
123         case PCM_C:
124                 runtime->hw.rates &= ~(SNDRV_PCM_RATE_32000 |
125                                        SNDRV_PCM_RATE_64000);
126                 runtime->hw.rate_min = 44100;
127                 break;
128         case PCM_MULTICH:
129                 runtime->hw.channels_max = chip->model->dac_channels;
130                 break;
131         }
132         if (chip->model->pcm_hardware_filter)
133                 chip->model->pcm_hardware_filter(channel, &runtime->hw);
134         err = snd_pcm_hw_constraint_step(runtime, 0,
135                                          SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
136         if (err < 0)
137                 return err;
138         err = snd_pcm_hw_constraint_step(runtime, 0,
139                                          SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
140         if (err < 0)
141                 return err;
142         if (runtime->hw.formats & SNDRV_PCM_FMTBIT_S32_LE) {
143                 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
144                 if (err < 0)
145                         return err;
146         }
147         if (runtime->hw.channels_max > 2) {
148                 err = snd_pcm_hw_constraint_step(runtime, 0,
149                                                  SNDRV_PCM_HW_PARAM_CHANNELS,
150                                                  2);
151                 if (err < 0)
152                         return err;
153         }
154         snd_pcm_set_sync(substream);
155         chip->streams[channel] = substream;
156
157         mutex_lock(&chip->mutex);
158         chip->pcm_active |= 1 << channel;
159         if (channel == PCM_SPDIF) {
160                 chip->spdif_pcm_bits = chip->spdif_bits;
161                 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
162                         ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
163                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
164                                SNDRV_CTL_EVENT_MASK_INFO,
165                                &chip->controls[CONTROL_SPDIF_PCM]->id);
166         }
167         mutex_unlock(&chip->mutex);
168
169         return 0;
170 }
171
172 static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
173 {
174         return oxygen_open(substream, PCM_A);
175 }
176
177 static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
178 {
179         return oxygen_open(substream, PCM_B);
180 }
181
182 static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
183 {
184         return oxygen_open(substream, PCM_C);
185 }
186
187 static int oxygen_spdif_open(struct snd_pcm_substream *substream)
188 {
189         return oxygen_open(substream, PCM_SPDIF);
190 }
191
192 static int oxygen_multich_open(struct snd_pcm_substream *substream)
193 {
194         return oxygen_open(substream, PCM_MULTICH);
195 }
196
197 static int oxygen_ac97_open(struct snd_pcm_substream *substream)
198 {
199         return oxygen_open(substream, PCM_AC97);
200 }
201
202 static int oxygen_close(struct snd_pcm_substream *substream)
203 {
204         struct oxygen *chip = snd_pcm_substream_chip(substream);
205         unsigned int channel = oxygen_substream_channel(substream);
206
207         mutex_lock(&chip->mutex);
208         chip->pcm_active &= ~(1 << channel);
209         if (channel == PCM_SPDIF) {
210                 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
211                         SNDRV_CTL_ELEM_ACCESS_INACTIVE;
212                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
213                                SNDRV_CTL_EVENT_MASK_INFO,
214                                &chip->controls[CONTROL_SPDIF_PCM]->id);
215         }
216         if (channel == PCM_SPDIF || channel == PCM_MULTICH)
217                 oxygen_update_spdif_source(chip);
218         mutex_unlock(&chip->mutex);
219
220         chip->streams[channel] = NULL;
221         return 0;
222 }
223
224 static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
225 {
226         if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
227                 return OXYGEN_FORMAT_24;
228         else
229                 return OXYGEN_FORMAT_16;
230 }
231
232 static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
233 {
234         switch (params_rate(hw_params)) {
235         case 32000:
236                 return OXYGEN_RATE_32000;
237         case 44100:
238                 return OXYGEN_RATE_44100;
239         default: /* 48000 */
240                 return OXYGEN_RATE_48000;
241         case 64000:
242                 return OXYGEN_RATE_64000;
243         case 88200:
244                 return OXYGEN_RATE_88200;
245         case 96000:
246                 return OXYGEN_RATE_96000;
247         case 176400:
248                 return OXYGEN_RATE_176400;
249         case 192000:
250                 return OXYGEN_RATE_192000;
251         }
252 }
253
254 static unsigned int oxygen_i2s_mclk(struct snd_pcm_hw_params *hw_params)
255 {
256         if (params_rate(hw_params) <= 96000)
257                 return OXYGEN_I2S_MCLK_256;
258         else
259                 return OXYGEN_I2S_MCLK_128;
260 }
261
262 static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
263 {
264         if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
265                 return OXYGEN_I2S_BITS_24;
266         else
267                 return OXYGEN_I2S_BITS_16;
268 }
269
270 static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
271 {
272         switch (params_channels(hw_params)) {
273         default: /* 2 */
274                 return OXYGEN_PLAY_CHANNELS_2;
275         case 4:
276                 return OXYGEN_PLAY_CHANNELS_4;
277         case 6:
278                 return OXYGEN_PLAY_CHANNELS_6;
279         case 8:
280                 return OXYGEN_PLAY_CHANNELS_8;
281         }
282 }
283
284 static const unsigned int channel_base_registers[PCM_COUNT] = {
285         [PCM_A] = OXYGEN_DMA_A_ADDRESS,
286         [PCM_B] = OXYGEN_DMA_B_ADDRESS,
287         [PCM_C] = OXYGEN_DMA_C_ADDRESS,
288         [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
289         [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
290         [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
291 };
292
293 static int oxygen_hw_params(struct snd_pcm_substream *substream,
294                             struct snd_pcm_hw_params *hw_params)
295 {
296         struct oxygen *chip = snd_pcm_substream_chip(substream);
297         unsigned int channel = oxygen_substream_channel(substream);
298         int err;
299
300         err = snd_pcm_lib_malloc_pages(substream,
301                                        params_buffer_bytes(hw_params));
302         if (err < 0)
303                 return err;
304
305         oxygen_write32(chip, channel_base_registers[channel],
306                        (u32)substream->runtime->dma_addr);
307         if (channel == PCM_MULTICH) {
308                 oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
309                                params_buffer_bytes(hw_params) / 4 - 1);
310                 oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
311                                params_period_bytes(hw_params) / 4 - 1);
312         } else {
313                 oxygen_write16(chip, channel_base_registers[channel] + 4,
314                                params_buffer_bytes(hw_params) / 4 - 1);
315                 oxygen_write16(chip, channel_base_registers[channel] + 6,
316                                params_period_bytes(hw_params) / 4 - 1);
317         }
318         return 0;
319 }
320
321 static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
322                                   struct snd_pcm_hw_params *hw_params)
323 {
324         struct oxygen *chip = snd_pcm_substream_chip(substream);
325         int err;
326
327         err = oxygen_hw_params(substream, hw_params);
328         if (err < 0)
329                 return err;
330
331         spin_lock_irq(&chip->reg_lock);
332         oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
333                              oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
334                              OXYGEN_REC_FORMAT_A_MASK);
335         oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
336                               oxygen_rate(hw_params) |
337                               oxygen_i2s_mclk(hw_params) |
338                               chip->model->adc_i2s_format |
339                               oxygen_i2s_bits(hw_params),
340                               OXYGEN_I2S_RATE_MASK |
341                               OXYGEN_I2S_FORMAT_MASK |
342                               OXYGEN_I2S_MCLK_MASK |
343                               OXYGEN_I2S_BITS_MASK);
344         spin_unlock_irq(&chip->reg_lock);
345
346         mutex_lock(&chip->mutex);
347         chip->model->set_adc_params(chip, hw_params);
348         mutex_unlock(&chip->mutex);
349         return 0;
350 }
351
352 static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
353                                   struct snd_pcm_hw_params *hw_params)
354 {
355         struct oxygen *chip = snd_pcm_substream_chip(substream);
356         int err;
357
358         err = oxygen_hw_params(substream, hw_params);
359         if (err < 0)
360                 return err;
361
362         spin_lock_irq(&chip->reg_lock);
363         oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
364                              oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
365                              OXYGEN_REC_FORMAT_B_MASK);
366         oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
367                               oxygen_rate(hw_params) |
368                               oxygen_i2s_mclk(hw_params) |
369                               chip->model->adc_i2s_format |
370                               oxygen_i2s_bits(hw_params),
371                               OXYGEN_I2S_RATE_MASK |
372                               OXYGEN_I2S_FORMAT_MASK |
373                               OXYGEN_I2S_MCLK_MASK |
374                               OXYGEN_I2S_BITS_MASK);
375         spin_unlock_irq(&chip->reg_lock);
376
377         mutex_lock(&chip->mutex);
378         chip->model->set_adc_params(chip, hw_params);
379         mutex_unlock(&chip->mutex);
380         return 0;
381 }
382
383 static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
384                                   struct snd_pcm_hw_params *hw_params)
385 {
386         struct oxygen *chip = snd_pcm_substream_chip(substream);
387         int err;
388
389         err = oxygen_hw_params(substream, hw_params);
390         if (err < 0)
391                 return err;
392
393         spin_lock_irq(&chip->reg_lock);
394         oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
395                              oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
396                              OXYGEN_REC_FORMAT_C_MASK);
397         spin_unlock_irq(&chip->reg_lock);
398         return 0;
399 }
400
401 static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
402                                   struct snd_pcm_hw_params *hw_params)
403 {
404         struct oxygen *chip = snd_pcm_substream_chip(substream);
405         int err;
406
407         err = oxygen_hw_params(substream, hw_params);
408         if (err < 0)
409                 return err;
410
411         spin_lock_irq(&chip->reg_lock);
412         oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
413                             OXYGEN_SPDIF_OUT_ENABLE);
414         oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
415                              oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
416                              OXYGEN_SPDIF_FORMAT_MASK);
417         oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
418                               oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
419                               OXYGEN_SPDIF_OUT_RATE_MASK);
420         oxygen_update_spdif_source(chip);
421         spin_unlock_irq(&chip->reg_lock);
422         return 0;
423 }
424
425 static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
426                                     struct snd_pcm_hw_params *hw_params)
427 {
428         struct oxygen *chip = snd_pcm_substream_chip(substream);
429         int err;
430
431         err = oxygen_hw_params(substream, hw_params);
432         if (err < 0)
433                 return err;
434
435         spin_lock_irq(&chip->reg_lock);
436         oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
437                              oxygen_play_channels(hw_params),
438                              OXYGEN_PLAY_CHANNELS_MASK);
439         oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
440                              oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
441                              OXYGEN_MULTICH_FORMAT_MASK);
442         oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
443                               oxygen_rate(hw_params) |
444                               chip->model->dac_i2s_format |
445                               oxygen_i2s_bits(hw_params),
446                               OXYGEN_I2S_RATE_MASK |
447                               OXYGEN_I2S_FORMAT_MASK |
448                               OXYGEN_I2S_BITS_MASK);
449         oxygen_update_dac_routing(chip);
450         oxygen_update_spdif_source(chip);
451         spin_unlock_irq(&chip->reg_lock);
452
453         mutex_lock(&chip->mutex);
454         chip->model->set_dac_params(chip, hw_params);
455         mutex_unlock(&chip->mutex);
456         return 0;
457 }
458
459 static int oxygen_hw_free(struct snd_pcm_substream *substream)
460 {
461         struct oxygen *chip = snd_pcm_substream_chip(substream);
462         unsigned int channel = oxygen_substream_channel(substream);
463
464         spin_lock_irq(&chip->reg_lock);
465         chip->interrupt_mask &= ~(1 << channel);
466         oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
467         spin_unlock_irq(&chip->reg_lock);
468
469         return snd_pcm_lib_free_pages(substream);
470 }
471
472 static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
473 {
474         struct oxygen *chip = snd_pcm_substream_chip(substream);
475
476         spin_lock_irq(&chip->reg_lock);
477         oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
478                             OXYGEN_SPDIF_OUT_ENABLE);
479         spin_unlock_irq(&chip->reg_lock);
480         return oxygen_hw_free(substream);
481 }
482
483 static int oxygen_prepare(struct snd_pcm_substream *substream)
484 {
485         struct oxygen *chip = snd_pcm_substream_chip(substream);
486         unsigned int channel = oxygen_substream_channel(substream);
487         unsigned int channel_mask = 1 << channel;
488
489         spin_lock_irq(&chip->reg_lock);
490         oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
491         oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
492
493         chip->interrupt_mask |= channel_mask;
494         oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
495         spin_unlock_irq(&chip->reg_lock);
496         return 0;
497 }
498
499 static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
500 {
501         struct oxygen *chip = snd_pcm_substream_chip(substream);
502         struct snd_pcm_substream *s;
503         unsigned int mask = 0;
504         int pausing;
505
506         switch (cmd) {
507         case SNDRV_PCM_TRIGGER_STOP:
508         case SNDRV_PCM_TRIGGER_START:
509                 pausing = 0;
510                 break;
511         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
512         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
513                 pausing = 1;
514                 break;
515         default:
516                 return -EINVAL;
517         }
518
519         snd_pcm_group_for_each_entry(s, substream) {
520                 if (snd_pcm_substream_chip(s) == chip) {
521                         mask |= 1 << oxygen_substream_channel(s);
522                         snd_pcm_trigger_done(s, substream);
523                 }
524         }
525
526         spin_lock(&chip->reg_lock);
527         if (!pausing) {
528                 if (cmd == SNDRV_PCM_TRIGGER_START)
529                         chip->pcm_running |= mask;
530                 else
531                         chip->pcm_running &= ~mask;
532                 oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
533         } else {
534                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
535                         oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
536                 else
537                         oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
538         }
539         spin_unlock(&chip->reg_lock);
540         return 0;
541 }
542
543 static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
544 {
545         struct oxygen *chip = snd_pcm_substream_chip(substream);
546         struct snd_pcm_runtime *runtime = substream->runtime;
547         unsigned int channel = oxygen_substream_channel(substream);
548         u32 curr_addr;
549
550         /* no spinlock, this read should be atomic */
551         curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
552         return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
553 }
554
555 static struct snd_pcm_ops oxygen_rec_a_ops = {
556         .open      = oxygen_rec_a_open,
557         .close     = oxygen_close,
558         .ioctl     = snd_pcm_lib_ioctl,
559         .hw_params = oxygen_rec_a_hw_params,
560         .hw_free   = oxygen_hw_free,
561         .prepare   = oxygen_prepare,
562         .trigger   = oxygen_trigger,
563         .pointer   = oxygen_pointer,
564 };
565
566 static struct snd_pcm_ops oxygen_rec_b_ops = {
567         .open      = oxygen_rec_b_open,
568         .close     = oxygen_close,
569         .ioctl     = snd_pcm_lib_ioctl,
570         .hw_params = oxygen_rec_b_hw_params,
571         .hw_free   = oxygen_hw_free,
572         .prepare   = oxygen_prepare,
573         .trigger   = oxygen_trigger,
574         .pointer   = oxygen_pointer,
575 };
576
577 static struct snd_pcm_ops oxygen_rec_c_ops = {
578         .open      = oxygen_rec_c_open,
579         .close     = oxygen_close,
580         .ioctl     = snd_pcm_lib_ioctl,
581         .hw_params = oxygen_rec_c_hw_params,
582         .hw_free   = oxygen_hw_free,
583         .prepare   = oxygen_prepare,
584         .trigger   = oxygen_trigger,
585         .pointer   = oxygen_pointer,
586 };
587
588 static struct snd_pcm_ops oxygen_spdif_ops = {
589         .open      = oxygen_spdif_open,
590         .close     = oxygen_close,
591         .ioctl     = snd_pcm_lib_ioctl,
592         .hw_params = oxygen_spdif_hw_params,
593         .hw_free   = oxygen_spdif_hw_free,
594         .prepare   = oxygen_prepare,
595         .trigger   = oxygen_trigger,
596         .pointer   = oxygen_pointer,
597 };
598
599 static struct snd_pcm_ops oxygen_multich_ops = {
600         .open      = oxygen_multich_open,
601         .close     = oxygen_close,
602         .ioctl     = snd_pcm_lib_ioctl,
603         .hw_params = oxygen_multich_hw_params,
604         .hw_free   = oxygen_hw_free,
605         .prepare   = oxygen_prepare,
606         .trigger   = oxygen_trigger,
607         .pointer   = oxygen_pointer,
608 };
609
610 static struct snd_pcm_ops oxygen_ac97_ops = {
611         .open      = oxygen_ac97_open,
612         .close     = oxygen_close,
613         .ioctl     = snd_pcm_lib_ioctl,
614         .hw_params = oxygen_hw_params,
615         .hw_free   = oxygen_hw_free,
616         .prepare   = oxygen_prepare,
617         .trigger   = oxygen_trigger,
618         .pointer   = oxygen_pointer,
619 };
620
621 static void oxygen_pcm_free(struct snd_pcm *pcm)
622 {
623         snd_pcm_lib_preallocate_free_for_all(pcm);
624 }
625
626 int __devinit oxygen_pcm_init(struct oxygen *chip)
627 {
628         struct snd_pcm *pcm;
629         int outs, ins;
630         int err;
631
632         outs = 1; /* OXYGEN_CHANNEL_MULTICH is always used */
633         ins = !!(chip->model->used_channels & (OXYGEN_CHANNEL_A |
634                                                OXYGEN_CHANNEL_B));
635         err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
636         if (err < 0)
637                 return err;
638         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &oxygen_multich_ops);
639         if (chip->model->used_channels & OXYGEN_CHANNEL_A)
640                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
641                                 &oxygen_rec_a_ops);
642         else if (chip->model->used_channels & OXYGEN_CHANNEL_B)
643                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
644                                 &oxygen_rec_b_ops);
645         pcm->private_data = chip;
646         pcm->private_free = oxygen_pcm_free;
647         strcpy(pcm->name, "Analog");
648         snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
649                                       SNDRV_DMA_TYPE_DEV,
650                                       snd_dma_pci_data(chip->pci),
651                                       512 * 1024, 2048 * 1024);
652         if (ins)
653                 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
654                                               SNDRV_DMA_TYPE_DEV,
655                                               snd_dma_pci_data(chip->pci),
656                                               128 * 1024, 256 * 1024);
657
658         outs = !!(chip->model->used_channels & OXYGEN_CHANNEL_SPDIF);
659         ins = !!(chip->model->used_channels & OXYGEN_CHANNEL_C);
660         if (outs | ins) {
661                 err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
662                 if (err < 0)
663                         return err;
664                 if (outs)
665                         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
666                                         &oxygen_spdif_ops);
667                 if (ins)
668                         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
669                                         &oxygen_rec_c_ops);
670                 pcm->private_data = chip;
671                 pcm->private_free = oxygen_pcm_free;
672                 strcpy(pcm->name, "Digital");
673                 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
674                                                       snd_dma_pci_data(chip->pci),
675                                                       128 * 1024, 256 * 1024);
676         }
677
678         outs = chip->has_ac97_1 &&
679                 (chip->model->used_channels & OXYGEN_CHANNEL_AC97);
680         ins = (chip->model->used_channels & (OXYGEN_CHANNEL_A |
681                                              OXYGEN_CHANNEL_B))
682                 == (OXYGEN_CHANNEL_A | OXYGEN_CHANNEL_B);
683         if (outs | ins) {
684                 err = snd_pcm_new(chip->card, ins ? "Analog2" : "AC97",
685                                   2, outs, ins, &pcm);
686                 if (err < 0)
687                         return err;
688                 if (outs)
689                         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
690                                         &oxygen_ac97_ops);
691                 if (ins)
692                         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
693                                         &oxygen_rec_b_ops);
694                 pcm->private_data = chip;
695                 pcm->private_free = oxygen_pcm_free;
696                 strcpy(pcm->name, ins ? "Analog 2" : "Front Panel");
697                 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
698                                                       snd_dma_pci_data(chip->pci),
699                                                       128 * 1024, 256 * 1024);
700         }
701         return 0;
702 }