aw2-alsa.c 22 KB

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