aw2-alsa.c 22 KB

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