]> err.no Git - linux-2.6/blob - sound/pci/aw2/aw2-alsa.c
[ALSA] sound/pci/aw2/aw2-alsa.c needs dma-mapping.h
[linux-2.6] / sound / pci / aw2 / aw2-alsa.c
1 /*****************************************************************************
2  *
3  * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
4  * Jean-Christian Hassler <jhassler@free.fr>
5  *
6  * This file is part of the Audiowerk2 ALSA driver
7  *
8  * The Audiowerk2 ALSA driver is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2.
11  *
12  * The Audiowerk2 ALSA driver is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with the Audiowerk2 ALSA driver; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20  * USA.
21  *
22  *****************************************************************************/
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <asm/io.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/control.h>
35
36 #include "saa7146.h"
37 #include "aw2-saa7146.h"
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
41               "Jean-Christian Hassler <jhassler@free.fr>");
42 MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
43 MODULE_LICENSE("GPL");
44
45 /*********************************
46  * DEFINES
47  ********************************/
48 #define PCI_VENDOR_ID_SAA7146             0x1131
49 #define PCI_DEVICE_ID_SAA7146             0x7146
50
51 #define CTL_ROUTE_ANALOG 0
52 #define CTL_ROUTE_DIGITAL 1
53
54 /*********************************
55  * TYPEDEFS
56  ********************************/
57   /* hardware definition */
58 static struct snd_pcm_hardware snd_aw2_playback_hw = {
59         .info = (SNDRV_PCM_INFO_MMAP |
60                  SNDRV_PCM_INFO_INTERLEAVED |
61                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
62         .formats = SNDRV_PCM_FMTBIT_S16_LE,
63         .rates = SNDRV_PCM_RATE_44100,
64         .rate_min = 44100,
65         .rate_max = 44100,
66         .channels_min = 2,
67         .channels_max = 4,
68         .buffer_bytes_max = 32768,
69         .period_bytes_min = 4096,
70         .period_bytes_max = 32768,
71         .periods_min = 1,
72         .periods_max = 1024,
73 };
74
75 static struct snd_pcm_hardware snd_aw2_capture_hw = {
76         .info = (SNDRV_PCM_INFO_MMAP |
77                  SNDRV_PCM_INFO_INTERLEAVED |
78                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
79         .formats = SNDRV_PCM_FMTBIT_S16_LE,
80         .rates = SNDRV_PCM_RATE_44100,
81         .rate_min = 44100,
82         .rate_max = 44100,
83         .channels_min = 2,
84         .channels_max = 2,
85         .buffer_bytes_max = 32768,
86         .period_bytes_min = 4096,
87         .period_bytes_max = 32768,
88         .periods_min = 1,
89         .periods_max = 1024,
90 };
91
92 struct aw2_pcm_device {
93         struct snd_pcm *pcm;
94         unsigned int stream_number;
95         struct aw2 *chip;
96 };
97
98 struct aw2 {
99         struct snd_aw2_saa7146 saa7146;
100
101         struct pci_dev *pci;
102         int irq;
103         spinlock_t reg_lock;
104         struct mutex mtx;
105
106         unsigned long iobase_phys;
107         void __iomem *iobase_virt;
108
109         struct snd_card *card;
110
111         struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
112         struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
113 };
114
115 /*********************************
116  * FUNCTION DECLARATIONS
117  ********************************/
118 static int __init alsa_card_aw2_init(void);
119 static void __exit alsa_card_aw2_exit(void);
120 static int snd_aw2_dev_free(struct snd_device *device);
121 static int __devinit snd_aw2_create(struct snd_card *card,
122                                     struct pci_dev *pci, struct aw2 **rchip);
123 static int __devinit snd_aw2_probe(struct pci_dev *pci,
124                                    const struct pci_device_id *pci_id);
125 static void __devexit snd_aw2_remove(struct pci_dev *pci);
126 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
127 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
128 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
129 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
130 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
131                                  struct snd_pcm_hw_params *hw_params);
132 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
133 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
134 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
135 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
136                                         int cmd);
137 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
138                                        int cmd);
139 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
140                                                       *substream);
141 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
142                                                      *substream);
143 static int __devinit snd_aw2_new_pcm(struct aw2 *chip);
144
145 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
146                                                struct snd_ctl_elem_info *uinfo);
147 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
148                                               struct snd_ctl_elem_value
149                                               *ucontrol);
150 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
151                                               struct snd_ctl_elem_value
152                                               *ucontrol);
153
154 /*********************************
155  * VARIABLES
156  ********************************/
157 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
158 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
159 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
160
161 module_param_array(index, int, NULL, 0444);
162 MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
163 module_param_array(id, charp, NULL, 0444);
164 MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
165 module_param_array(enable, bool, NULL, 0444);
166 MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
167
168 static struct pci_device_id snd_aw2_ids[] = {
169         {PCI_VENDOR_ID_SAA7146, PCI_DEVICE_ID_SAA7146, PCI_ANY_ID, PCI_ANY_ID,
170          0, 0, 0},
171         {0}
172 };
173
174 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
175
176 /* pci_driver definition */
177 static struct pci_driver driver = {
178         .name = "Emagic Audiowerk 2",
179         .id_table = snd_aw2_ids,
180         .probe = snd_aw2_probe,
181         .remove = __devexit_p(snd_aw2_remove),
182 };
183
184 /* operators for playback PCM alsa interface */
185 static struct snd_pcm_ops snd_aw2_playback_ops = {
186         .open = snd_aw2_pcm_playback_open,
187         .close = snd_aw2_pcm_playback_close,
188         .ioctl = snd_pcm_lib_ioctl,
189         .hw_params = snd_aw2_pcm_hw_params,
190         .hw_free = snd_aw2_pcm_hw_free,
191         .prepare = snd_aw2_pcm_prepare_playback,
192         .trigger = snd_aw2_pcm_trigger_playback,
193         .pointer = snd_aw2_pcm_pointer_playback,
194 };
195
196 /* operators for capture PCM alsa interface */
197 static struct snd_pcm_ops snd_aw2_capture_ops = {
198         .open = snd_aw2_pcm_capture_open,
199         .close = snd_aw2_pcm_capture_close,
200         .ioctl = snd_pcm_lib_ioctl,
201         .hw_params = snd_aw2_pcm_hw_params,
202         .hw_free = snd_aw2_pcm_hw_free,
203         .prepare = snd_aw2_pcm_prepare_capture,
204         .trigger = snd_aw2_pcm_trigger_capture,
205         .pointer = snd_aw2_pcm_pointer_capture,
206 };
207
208 static struct snd_kcontrol_new aw2_control __devinitdata = {
209         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
210         .name = "PCM Capture Route",
211         .index = 0,
212         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
213         .private_value = 0xffff,
214         .info = snd_aw2_control_switch_capture_info,
215         .get = snd_aw2_control_switch_capture_get,
216         .put = snd_aw2_control_switch_capture_put
217 };
218
219 /*********************************
220  * FUNCTION IMPLEMENTATIONS
221  ********************************/
222
223 /* initialization of the module */
224 static int __init alsa_card_aw2_init(void)
225 {
226         snd_printdd(KERN_DEBUG "aw2: Load aw2 module\n");
227         return pci_register_driver(&driver);
228 }
229
230 /* clean up the module */
231 static void __exit alsa_card_aw2_exit(void)
232 {
233         snd_printdd(KERN_DEBUG "aw2: Unload aw2 module\n");
234         pci_unregister_driver(&driver);
235 }
236
237 module_init(alsa_card_aw2_init);
238 module_exit(alsa_card_aw2_exit);
239
240 /* component-destructor */
241 static int snd_aw2_dev_free(struct snd_device *device)
242 {
243         struct aw2 *chip = device->device_data;
244
245         /* Free hardware */
246         snd_aw2_saa7146_free(&chip->saa7146);
247
248         /* release the irq */
249         if (chip->irq >= 0)
250                 free_irq(chip->irq, (void *)chip);
251         /* release the i/o ports & memory */
252         if (chip->iobase_virt)
253                 iounmap(chip->iobase_virt);
254
255         pci_release_regions(chip->pci);
256         /* disable the PCI entry */
257         pci_disable_device(chip->pci);
258         /* release the data */
259         kfree(chip);
260
261         return 0;
262 }
263
264 /* chip-specific constructor */
265 static int __devinit snd_aw2_create(struct snd_card *card,
266                                     struct pci_dev *pci, struct aw2 **rchip)
267 {
268         struct aw2 *chip;
269         int err;
270         static struct snd_device_ops ops = {
271                 .dev_free = snd_aw2_dev_free,
272         };
273
274         *rchip = NULL;
275
276         /* initialize the PCI entry */
277         err = pci_enable_device(pci);
278         if (err < 0)
279                 return err;
280         pci_set_master(pci);
281
282         /* check PCI availability (32bit DMA) */
283         if ((pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) ||
284             (pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0)) {
285                 printk(KERN_ERR "aw2: Impossible to set 32bit mask DMA\n");
286                 pci_disable_device(pci);
287                 return -ENXIO;
288         }
289         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
290         if (chip == NULL) {
291                 pci_disable_device(pci);
292                 return -ENOMEM;
293         }
294
295         /* initialize the stuff */
296         chip->card = card;
297         chip->pci = pci;
298         chip->irq = -1;
299
300         /* (1) PCI resource allocation */
301         err = pci_request_regions(pci, "Audiowerk2");
302         if (err < 0) {
303                 pci_disable_device(pci);
304                 kfree(chip);
305                 return err;
306         }
307         chip->iobase_phys = pci_resource_start(pci, 0);
308         chip->iobase_virt =
309                 ioremap_nocache(chip->iobase_phys,
310                                 pci_resource_len(pci, 0));
311
312         if (chip->iobase_virt == NULL) {
313                 printk(KERN_ERR "aw2: unable to remap memory region");
314                 pci_release_regions(pci);
315                 pci_disable_device(pci);
316                 kfree(chip);
317                 return -ENOMEM;
318         }
319
320
321         if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
322                         IRQF_SHARED, "Audiowerk2", chip)) {
323                 printk(KERN_ERR "aw2: Cannot grab irq %d\n", pci->irq);
324
325                 iounmap(chip->iobase_virt);
326                 pci_release_regions(chip->pci);
327                 pci_disable_device(chip->pci);
328                 kfree(chip);
329                 return -EBUSY;
330         }
331         chip->irq = pci->irq;
332
333         /* (2) initialization of the chip hardware */
334         snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
335         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
336         if (err < 0) {
337                 free_irq(chip->irq, (void *)chip);
338                 iounmap(chip->iobase_virt);
339                 pci_release_regions(chip->pci);
340                 pci_disable_device(chip->pci);
341                 kfree(chip);
342                 return err;
343         }
344
345         snd_card_set_dev(card, &pci->dev);
346         *rchip = chip;
347
348         printk(KERN_INFO
349                "Audiowerk 2 sound card (saa7146 chipset) detected and "
350                "managed\n");
351         return 0;
352 }
353
354 /* constructor */
355 static int __devinit snd_aw2_probe(struct pci_dev *pci,
356                                    const struct pci_device_id *pci_id)
357 {
358         static int dev;
359         struct snd_card *card;
360         struct aw2 *chip;
361         int err;
362
363         /* (1) Continue if device is not enabled, else inc dev */
364         if (dev >= SNDRV_CARDS)
365                 return -ENODEV;
366         if (!enable[dev]) {
367                 dev++;
368                 return -ENOENT;
369         }
370
371         /* (2) Create card instance */
372         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
373         if (card == NULL)
374                 return -ENOMEM;
375
376         /* (3) Create main component */
377         err = snd_aw2_create(card, pci, &chip);
378         if (err < 0) {
379                 snd_card_free(card);
380                 return err;
381         }
382
383         /* initialize mutex */
384         mutex_init(&chip->mtx);
385         /* init spinlock */
386         spin_lock_init(&chip->reg_lock);
387         /* (4) Define driver ID and name string */
388         strcpy(card->driver, "aw2");
389         strcpy(card->shortname, "Audiowerk2");
390
391         sprintf(card->longname, "%s with SAA7146 irq %i",
392                 card->shortname, chip->irq);
393
394         /* (5) Create other components */
395         snd_aw2_new_pcm(chip);
396
397         /* (6) Register card instance */
398         err = snd_card_register(card);
399         if (err < 0) {
400                 snd_card_free(card);
401                 return err;
402         }
403
404         /* (7) Set PCI driver data */
405         pci_set_drvdata(pci, card);
406
407         dev++;
408         return 0;
409 }
410
411 /* destructor */
412 static void __devexit snd_aw2_remove(struct pci_dev *pci)
413 {
414         snd_card_free(pci_get_drvdata(pci));
415         pci_set_drvdata(pci, NULL);
416 }
417
418 /* open callback */
419 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
420 {
421         struct snd_pcm_runtime *runtime = substream->runtime;
422
423         snd_printdd(KERN_DEBUG "aw2: Playback_open \n");
424         runtime->hw = snd_aw2_playback_hw;
425         return 0;
426 }
427
428 /* close callback */
429 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
430 {
431         return 0;
432
433 }
434
435 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
436 {
437         struct snd_pcm_runtime *runtime = substream->runtime;
438
439         snd_printdd(KERN_DEBUG "aw2: Capture_open \n");
440         runtime->hw = snd_aw2_capture_hw;
441         return 0;
442 }
443
444 /* close callback */
445 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
446 {
447         /* TODO: something to do ? */
448         return 0;
449 }
450
451  /* hw_params callback */
452 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
453                                  struct snd_pcm_hw_params *hw_params)
454 {
455         return snd_pcm_lib_malloc_pages(substream,
456                                         params_buffer_bytes(hw_params));
457 }
458
459 /* hw_free callback */
460 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
461 {
462         return snd_pcm_lib_free_pages(substream);
463 }
464
465 /* prepare callback for playback */
466 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
467 {
468         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
469         struct aw2 *chip = pcm_device->chip;
470         struct snd_pcm_runtime *runtime = substream->runtime;
471         unsigned long period_size, buffer_size;
472
473         mutex_lock(&chip->mtx);
474
475         period_size = snd_pcm_lib_period_bytes(substream);
476         buffer_size = snd_pcm_lib_buffer_bytes(substream);
477
478         snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
479                                           pcm_device->stream_number,
480                                           runtime->dma_addr, period_size,
481                                           buffer_size);
482
483         /* Define Interrupt callback */
484         snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
485                                                     (snd_aw2_saa7146_it_cb)
486                                                     snd_pcm_period_elapsed,
487                                                     (void *)substream);
488
489         mutex_unlock(&chip->mtx);
490
491         return 0;
492 }
493
494 /* prepare callback for capture */
495 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
496 {
497         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
498         struct aw2 *chip = pcm_device->chip;
499         struct snd_pcm_runtime *runtime = substream->runtime;
500         unsigned long period_size, buffer_size;
501
502         mutex_lock(&chip->mtx);
503
504         period_size = snd_pcm_lib_period_bytes(substream);
505         buffer_size = snd_pcm_lib_buffer_bytes(substream);
506
507         snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
508                                          pcm_device->stream_number,
509                                          runtime->dma_addr, period_size,
510                                          buffer_size);
511
512         /* Define Interrupt callback */
513         snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
514                                                    (snd_aw2_saa7146_it_cb)
515                                                    snd_pcm_period_elapsed,
516                                                    (void *)substream);
517
518         mutex_unlock(&chip->mtx);
519
520         return 0;
521 }
522
523 /* playback trigger callback */
524 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
525                                         int cmd)
526 {
527         int status = 0;
528         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
529         struct aw2 *chip = pcm_device->chip;
530         spin_lock(&chip->reg_lock);
531         switch (cmd) {
532         case SNDRV_PCM_TRIGGER_START:
533                 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
534                                                            pcm_device->
535                                                            stream_number);
536                 break;
537         case SNDRV_PCM_TRIGGER_STOP:
538                 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
539                                                           pcm_device->
540                                                           stream_number);
541                 break;
542         default:
543                 status = -EINVAL;
544         }
545         spin_unlock(&chip->reg_lock);
546         return status;
547 }
548
549 /* capture trigger callback */
550 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
551                                        int cmd)
552 {
553         int status = 0;
554         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
555         struct aw2 *chip = pcm_device->chip;
556         spin_lock(&chip->reg_lock);
557         switch (cmd) {
558         case SNDRV_PCM_TRIGGER_START:
559                 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
560                                                           pcm_device->
561                                                           stream_number);
562                 break;
563         case SNDRV_PCM_TRIGGER_STOP:
564                 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
565                                                          pcm_device->
566                                                          stream_number);
567                 break;
568         default:
569                 status = -EINVAL;
570         }
571         spin_unlock(&chip->reg_lock);
572         return status;
573 }
574
575 /* playback pointer callback */
576 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
577                                                       *substream)
578 {
579         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
580         struct aw2 *chip = pcm_device->chip;
581         unsigned int current_ptr;
582
583         /* get the current hardware pointer */
584         struct snd_pcm_runtime *runtime = substream->runtime;
585         current_ptr =
586                 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
587                                                     pcm_device->stream_number,
588                                                     runtime->dma_area,
589                                                     runtime->buffer_size);
590
591         return bytes_to_frames(substream->runtime, current_ptr);
592 }
593
594 /* capture pointer callback */
595 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
596                                                      *substream)
597 {
598         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
599         struct aw2 *chip = pcm_device->chip;
600         unsigned int current_ptr;
601
602         /* get the current hardware pointer */
603         struct snd_pcm_runtime *runtime = substream->runtime;
604         current_ptr =
605                 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
606                                                    pcm_device->stream_number,
607                                                    runtime->dma_area,
608                                                    runtime->buffer_size);
609
610         return bytes_to_frames(substream->runtime, current_ptr);
611 }
612
613 /* create a pcm device */
614 static int __devinit snd_aw2_new_pcm(struct aw2 *chip)
615 {
616         struct snd_pcm *pcm_playback_ana;
617         struct snd_pcm *pcm_playback_num;
618         struct snd_pcm *pcm_capture;
619         struct aw2_pcm_device *pcm_device;
620         int err = 0;
621
622         /* Create new Alsa PCM device */
623
624         err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
625                           &pcm_playback_ana);
626         if (err < 0) {
627                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
628                 return err;
629         }
630
631         /* Creation ok */
632         pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
633
634         /* Set PCM device name */
635         strcpy(pcm_playback_ana->name, "Analog playback");
636         /* Associate private data to PCM device */
637         pcm_playback_ana->private_data = pcm_device;
638         /* set operators of PCM device */
639         snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
640                         &snd_aw2_playback_ops);
641         /* store PCM device */
642         pcm_device->pcm = pcm_playback_ana;
643         /* give base chip pointer to our internal pcm device
644            structure */
645         pcm_device->chip = chip;
646         /* Give stream number to PCM device */
647         pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
648
649         /* pre-allocation of buffers */
650         /* Preallocate continuous pages. */
651         err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
652                                                     SNDRV_DMA_TYPE_DEV,
653                                                     snd_dma_pci_data
654                                                     (chip->pci),
655                                                     64 * 1024, 64 * 1024);
656         if (err)
657                 printk(KERN_ERR "aw2: snd_pcm_lib_preallocate_pages_for_all "
658                        "error (0x%X)\n", err);
659
660         err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
661                           &pcm_playback_num);
662
663         if (err < 0) {
664                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
665                 return err;
666         }
667         /* Creation ok */
668         pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
669
670         /* Set PCM device name */
671         strcpy(pcm_playback_num->name, "Digital playback");
672         /* Associate private data to PCM device */
673         pcm_playback_num->private_data = pcm_device;
674         /* set operators of PCM device */
675         snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
676                         &snd_aw2_playback_ops);
677         /* store PCM device */
678         pcm_device->pcm = pcm_playback_num;
679         /* give base chip pointer to our internal pcm device
680            structure */
681         pcm_device->chip = chip;
682         /* Give stream number to PCM device */
683         pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
684
685         /* pre-allocation of buffers */
686         /* Preallocate continuous pages. */
687         err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
688                                                     SNDRV_DMA_TYPE_DEV,
689                                                     snd_dma_pci_data
690                                                     (chip->pci),
691                                                     64 * 1024, 64 * 1024);
692         if (err)
693                 printk(KERN_ERR
694                        "aw2: snd_pcm_lib_preallocate_pages_for_all error "
695                        "(0x%X)\n", err);
696
697
698
699         err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
700                           &pcm_capture);
701
702         if (err < 0) {
703                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
704                 return err;
705         }
706
707         /* Creation ok */
708         pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
709
710         /* Set PCM device name */
711         strcpy(pcm_capture->name, "Capture");
712         /* Associate private data to PCM device */
713         pcm_capture->private_data = pcm_device;
714         /* set operators of PCM device */
715         snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
716                         &snd_aw2_capture_ops);
717         /* store PCM device */
718         pcm_device->pcm = pcm_capture;
719         /* give base chip pointer to our internal pcm device
720            structure */
721         pcm_device->chip = chip;
722         /* Give stream number to PCM device */
723         pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
724
725         /* pre-allocation of buffers */
726         /* Preallocate continuous pages. */
727         err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
728                                                     SNDRV_DMA_TYPE_DEV,
729                                                     snd_dma_pci_data
730                                                     (chip->pci),
731                                                     64 * 1024, 64 * 1024);
732         if (err)
733                 printk(KERN_ERR
734                        "aw2: snd_pcm_lib_preallocate_pages_for_all error "
735                        "(0x%X)\n", err);
736
737
738         /* Create control */
739         err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
740         if (err < 0) {
741                 printk(KERN_ERR "aw2: snd_ctl_add error (0x%X)\n", err);
742                 return err;
743         }
744
745         return 0;
746 }
747
748 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
749                                                struct snd_ctl_elem_info *uinfo)
750 {
751         static char *texts[2] = {
752                 "Analog", "Digital"
753         };
754         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
755         uinfo->count = 1;
756         uinfo->value.enumerated.items = 2;
757         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
758                 uinfo->value.enumerated.item =
759                     uinfo->value.enumerated.items - 1;
760         }
761         strcpy(uinfo->value.enumerated.name,
762                texts[uinfo->value.enumerated.item]);
763         return 0;
764 }
765
766 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
767                                               struct snd_ctl_elem_value
768                                               *ucontrol)
769 {
770         struct aw2 *chip = snd_kcontrol_chip(kcontrol);
771         if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
772                 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
773         else
774                 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
775         return 0;
776 }
777
778 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
779                                               struct snd_ctl_elem_value
780                                               *ucontrol)
781 {
782         struct aw2 *chip = snd_kcontrol_chip(kcontrol);
783         int changed = 0;
784         int is_disgital =
785             snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
786
787         if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
788              && !is_disgital)
789             || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
790                 && is_disgital)) {
791                 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
792                 changed = 1;
793         }
794         return changed;
795 }