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