aw2-alsa.c 22 KB

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