aw2-alsa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*****************************************************************************
  2. *
  3. * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
  4. * Jean-Christian Hassler <jhassler@free.fr>
  5. *
  6. * This file is part of the Audiowerk2 ALSA driver
  7. *
  8. * The Audiowerk2 ALSA driver is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2.
  11. *
  12. * The Audiowerk2 ALSA driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with the Audiowerk2 ALSA driver; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  20. * USA.
  21. *
  22. *****************************************************************************/
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <asm/io.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/control.h>
  35. #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 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. module_param_array(index, int, NULL, 0444);
  145. MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
  146. module_param_array(id, charp, NULL, 0444);
  147. MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
  148. module_param_array(enable, bool, NULL, 0444);
  149. MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
  150. static struct pci_device_id snd_aw2_ids[] = {
  151. {PCI_VENDOR_ID_SAA7146, PCI_DEVICE_ID_SAA7146, 0, 0,
  152. 0, 0, 0},
  153. {0}
  154. };
  155. MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
  156. /* pci_driver definition */
  157. static struct pci_driver driver = {
  158. .name = "Emagic Audiowerk 2",
  159. .id_table = snd_aw2_ids,
  160. .probe = snd_aw2_probe,
  161. .remove = __devexit_p(snd_aw2_remove),
  162. };
  163. /* operators for playback PCM alsa interface */
  164. static struct snd_pcm_ops snd_aw2_playback_ops = {
  165. .open = snd_aw2_pcm_playback_open,
  166. .close = snd_aw2_pcm_playback_close,
  167. .ioctl = snd_pcm_lib_ioctl,
  168. .hw_params = snd_aw2_pcm_hw_params,
  169. .hw_free = snd_aw2_pcm_hw_free,
  170. .prepare = snd_aw2_pcm_prepare_playback,
  171. .trigger = snd_aw2_pcm_trigger_playback,
  172. .pointer = snd_aw2_pcm_pointer_playback,
  173. };
  174. /* operators for capture PCM alsa interface */
  175. static struct snd_pcm_ops snd_aw2_capture_ops = {
  176. .open = snd_aw2_pcm_capture_open,
  177. .close = snd_aw2_pcm_capture_close,
  178. .ioctl = snd_pcm_lib_ioctl,
  179. .hw_params = snd_aw2_pcm_hw_params,
  180. .hw_free = snd_aw2_pcm_hw_free,
  181. .prepare = snd_aw2_pcm_prepare_capture,
  182. .trigger = snd_aw2_pcm_trigger_capture,
  183. .pointer = snd_aw2_pcm_pointer_capture,
  184. };
  185. static struct snd_kcontrol_new aw2_control __devinitdata = {
  186. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  187. .name = "PCM Capture Route",
  188. .index = 0,
  189. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  190. .private_value = 0xffff,
  191. .info = snd_aw2_control_switch_capture_info,
  192. .get = snd_aw2_control_switch_capture_get,
  193. .put = snd_aw2_control_switch_capture_put
  194. };
  195. /*********************************
  196. * FUNCTION IMPLEMENTATIONS
  197. ********************************/
  198. /* initialization of the module */
  199. static int __init alsa_card_aw2_init(void)
  200. {
  201. snd_printdd(KERN_DEBUG "aw2: Load aw2 module\n");
  202. return pci_register_driver(&driver);
  203. }
  204. /* clean up the module */
  205. static void __exit alsa_card_aw2_exit(void)
  206. {
  207. snd_printdd(KERN_DEBUG "aw2: Unload aw2 module\n");
  208. pci_unregister_driver(&driver);
  209. }
  210. module_init(alsa_card_aw2_init);
  211. module_exit(alsa_card_aw2_exit);
  212. /* component-destructor */
  213. static int snd_aw2_dev_free(struct snd_device *device)
  214. {
  215. struct aw2 *chip = device->device_data;
  216. /* Free hardware */
  217. snd_aw2_saa7146_free(&chip->saa7146);
  218. /* release the irq */
  219. if (chip->irq >= 0)
  220. free_irq(chip->irq, (void *)chip);
  221. /* release the i/o ports & memory */
  222. if (chip->iobase_virt)
  223. iounmap(chip->iobase_virt);
  224. pci_release_regions(chip->pci);
  225. /* disable the PCI entry */
  226. pci_disable_device(chip->pci);
  227. /* release the data */
  228. kfree(chip);
  229. return 0;
  230. }
  231. /* chip-specific constructor */
  232. static int __devinit snd_aw2_create(struct snd_card *card,
  233. struct pci_dev *pci, struct aw2 **rchip)
  234. {
  235. struct aw2 *chip;
  236. int err;
  237. static struct snd_device_ops ops = {
  238. .dev_free = snd_aw2_dev_free,
  239. };
  240. *rchip = NULL;
  241. /* initialize the PCI entry */
  242. err = pci_enable_device(pci);
  243. if (err < 0)
  244. return err;
  245. pci_set_master(pci);
  246. /* check PCI availability (32bit DMA) */
  247. if ((pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0) ||
  248. (pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32)) < 0)) {
  249. printk(KERN_ERR "aw2: Impossible to set 32bit mask DMA\n");
  250. pci_disable_device(pci);
  251. return -ENXIO;
  252. }
  253. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  254. if (chip == NULL) {
  255. pci_disable_device(pci);
  256. return -ENOMEM;
  257. }
  258. /* initialize the stuff */
  259. chip->card = card;
  260. chip->pci = pci;
  261. chip->irq = -1;
  262. /* (1) PCI resource allocation */
  263. err = pci_request_regions(pci, "Audiowerk2");
  264. if (err < 0) {
  265. pci_disable_device(pci);
  266. kfree(chip);
  267. return err;
  268. }
  269. chip->iobase_phys = pci_resource_start(pci, 0);
  270. chip->iobase_virt =
  271. ioremap_nocache(chip->iobase_phys,
  272. pci_resource_len(pci, 0));
  273. if (chip->iobase_virt == NULL) {
  274. printk(KERN_ERR "aw2: unable to remap memory region");
  275. pci_release_regions(pci);
  276. pci_disable_device(pci);
  277. kfree(chip);
  278. return -ENOMEM;
  279. }
  280. /* (2) initialization of the chip hardware */
  281. snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
  282. if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
  283. IRQF_SHARED, "Audiowerk2", chip)) {
  284. printk(KERN_ERR "aw2: Cannot grab irq %d\n", pci->irq);
  285. iounmap(chip->iobase_virt);
  286. pci_release_regions(chip->pci);
  287. pci_disable_device(chip->pci);
  288. kfree(chip);
  289. return -EBUSY;
  290. }
  291. chip->irq = pci->irq;
  292. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  293. if (err < 0) {
  294. free_irq(chip->irq, (void *)chip);
  295. iounmap(chip->iobase_virt);
  296. pci_release_regions(chip->pci);
  297. pci_disable_device(chip->pci);
  298. kfree(chip);
  299. return err;
  300. }
  301. snd_card_set_dev(card, &pci->dev);
  302. *rchip = chip;
  303. printk(KERN_INFO
  304. "Audiowerk 2 sound card (saa7146 chipset) detected and "
  305. "managed\n");
  306. return 0;
  307. }
  308. /* constructor */
  309. static int __devinit snd_aw2_probe(struct pci_dev *pci,
  310. const struct pci_device_id *pci_id)
  311. {
  312. static int dev;
  313. struct snd_card *card;
  314. struct aw2 *chip;
  315. int err;
  316. /* (1) Continue if device is not enabled, else inc dev */
  317. if (dev >= SNDRV_CARDS)
  318. return -ENODEV;
  319. if (!enable[dev]) {
  320. dev++;
  321. return -ENOENT;
  322. }
  323. /* (2) Create card instance */
  324. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  325. if (err < 0)
  326. return err;
  327. /* (3) Create main component */
  328. err = snd_aw2_create(card, pci, &chip);
  329. if (err < 0) {
  330. snd_card_free(card);
  331. return err;
  332. }
  333. /* initialize mutex */
  334. mutex_init(&chip->mtx);
  335. /* init spinlock */
  336. spin_lock_init(&chip->reg_lock);
  337. /* (4) Define driver ID and name string */
  338. strcpy(card->driver, "aw2");
  339. strcpy(card->shortname, "Audiowerk2");
  340. sprintf(card->longname, "%s with SAA7146 irq %i",
  341. card->shortname, chip->irq);
  342. /* (5) Create other components */
  343. snd_aw2_new_pcm(chip);
  344. /* (6) Register card instance */
  345. err = snd_card_register(card);
  346. if (err < 0) {
  347. snd_card_free(card);
  348. return err;
  349. }
  350. /* (7) Set PCI driver data */
  351. pci_set_drvdata(pci, card);
  352. dev++;
  353. return 0;
  354. }
  355. /* destructor */
  356. static void __devexit snd_aw2_remove(struct pci_dev *pci)
  357. {
  358. snd_card_free(pci_get_drvdata(pci));
  359. pci_set_drvdata(pci, NULL);
  360. }
  361. /* open callback */
  362. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
  363. {
  364. struct snd_pcm_runtime *runtime = substream->runtime;
  365. snd_printdd(KERN_DEBUG "aw2: Playback_open \n");
  366. runtime->hw = snd_aw2_playback_hw;
  367. return 0;
  368. }
  369. /* close callback */
  370. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
  371. {
  372. return 0;
  373. }
  374. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
  375. {
  376. struct snd_pcm_runtime *runtime = substream->runtime;
  377. snd_printdd(KERN_DEBUG "aw2: Capture_open \n");
  378. runtime->hw = snd_aw2_capture_hw;
  379. return 0;
  380. }
  381. /* close callback */
  382. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
  383. {
  384. /* TODO: something to do ? */
  385. return 0;
  386. }
  387. /* hw_params callback */
  388. static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
  389. struct snd_pcm_hw_params *hw_params)
  390. {
  391. return snd_pcm_lib_malloc_pages(substream,
  392. params_buffer_bytes(hw_params));
  393. }
  394. /* hw_free callback */
  395. static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
  396. {
  397. return snd_pcm_lib_free_pages(substream);
  398. }
  399. /* prepare callback for playback */
  400. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
  401. {
  402. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  403. struct aw2 *chip = pcm_device->chip;
  404. struct snd_pcm_runtime *runtime = substream->runtime;
  405. unsigned long period_size, buffer_size;
  406. mutex_lock(&chip->mtx);
  407. period_size = snd_pcm_lib_period_bytes(substream);
  408. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  409. snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
  410. pcm_device->stream_number,
  411. runtime->dma_addr, period_size,
  412. buffer_size);
  413. /* Define Interrupt callback */
  414. snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
  415. (snd_aw2_saa7146_it_cb)
  416. snd_pcm_period_elapsed,
  417. (void *)substream);
  418. mutex_unlock(&chip->mtx);
  419. return 0;
  420. }
  421. /* prepare callback for capture */
  422. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
  423. {
  424. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  425. struct aw2 *chip = pcm_device->chip;
  426. struct snd_pcm_runtime *runtime = substream->runtime;
  427. unsigned long period_size, buffer_size;
  428. mutex_lock(&chip->mtx);
  429. period_size = snd_pcm_lib_period_bytes(substream);
  430. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  431. snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
  432. pcm_device->stream_number,
  433. runtime->dma_addr, period_size,
  434. buffer_size);
  435. /* Define Interrupt callback */
  436. snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
  437. (snd_aw2_saa7146_it_cb)
  438. snd_pcm_period_elapsed,
  439. (void *)substream);
  440. mutex_unlock(&chip->mtx);
  441. return 0;
  442. }
  443. /* playback trigger callback */
  444. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  445. int cmd)
  446. {
  447. int status = 0;
  448. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  449. struct aw2 *chip = pcm_device->chip;
  450. spin_lock(&chip->reg_lock);
  451. switch (cmd) {
  452. case SNDRV_PCM_TRIGGER_START:
  453. snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
  454. pcm_device->
  455. stream_number);
  456. break;
  457. case SNDRV_PCM_TRIGGER_STOP:
  458. snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
  459. pcm_device->
  460. stream_number);
  461. break;
  462. default:
  463. status = -EINVAL;
  464. }
  465. spin_unlock(&chip->reg_lock);
  466. return status;
  467. }
  468. /* capture trigger callback */
  469. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  470. int cmd)
  471. {
  472. int status = 0;
  473. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  474. struct aw2 *chip = pcm_device->chip;
  475. spin_lock(&chip->reg_lock);
  476. switch (cmd) {
  477. case SNDRV_PCM_TRIGGER_START:
  478. snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
  479. pcm_device->
  480. stream_number);
  481. break;
  482. case SNDRV_PCM_TRIGGER_STOP:
  483. snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
  484. pcm_device->
  485. stream_number);
  486. break;
  487. default:
  488. status = -EINVAL;
  489. }
  490. spin_unlock(&chip->reg_lock);
  491. return status;
  492. }
  493. /* playback pointer callback */
  494. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  495. *substream)
  496. {
  497. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  498. struct aw2 *chip = pcm_device->chip;
  499. unsigned int current_ptr;
  500. /* get the current hardware pointer */
  501. struct snd_pcm_runtime *runtime = substream->runtime;
  502. current_ptr =
  503. snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
  504. pcm_device->stream_number,
  505. runtime->dma_area,
  506. runtime->buffer_size);
  507. return bytes_to_frames(substream->runtime, current_ptr);
  508. }
  509. /* capture pointer callback */
  510. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
  511. *substream)
  512. {
  513. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  514. struct aw2 *chip = pcm_device->chip;
  515. unsigned int current_ptr;
  516. /* get the current hardware pointer */
  517. struct snd_pcm_runtime *runtime = substream->runtime;
  518. current_ptr =
  519. snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
  520. pcm_device->stream_number,
  521. runtime->dma_area,
  522. runtime->buffer_size);
  523. return bytes_to_frames(substream->runtime, current_ptr);
  524. }
  525. /* create a pcm device */
  526. static int __devinit snd_aw2_new_pcm(struct aw2 *chip)
  527. {
  528. struct snd_pcm *pcm_playback_ana;
  529. struct snd_pcm *pcm_playback_num;
  530. struct snd_pcm *pcm_capture;
  531. struct aw2_pcm_device *pcm_device;
  532. int err = 0;
  533. /* Create new Alsa PCM device */
  534. err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
  535. &pcm_playback_ana);
  536. if (err < 0) {
  537. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  538. return err;
  539. }
  540. /* Creation ok */
  541. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
  542. /* Set PCM device name */
  543. strcpy(pcm_playback_ana->name, "Analog playback");
  544. /* Associate private data to PCM device */
  545. pcm_playback_ana->private_data = pcm_device;
  546. /* set operators of PCM device */
  547. snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
  548. &snd_aw2_playback_ops);
  549. /* store PCM device */
  550. pcm_device->pcm = pcm_playback_ana;
  551. /* give base chip pointer to our internal pcm device
  552. structure */
  553. pcm_device->chip = chip;
  554. /* Give stream number to PCM device */
  555. pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
  556. /* pre-allocation of buffers */
  557. /* Preallocate continuous pages. */
  558. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
  559. SNDRV_DMA_TYPE_DEV,
  560. snd_dma_pci_data
  561. (chip->pci),
  562. 64 * 1024, 64 * 1024);
  563. if (err)
  564. printk(KERN_ERR "aw2: snd_pcm_lib_preallocate_pages_for_all "
  565. "error (0x%X)\n", err);
  566. err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
  567. &pcm_playback_num);
  568. if (err < 0) {
  569. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  570. return err;
  571. }
  572. /* Creation ok */
  573. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
  574. /* Set PCM device name */
  575. strcpy(pcm_playback_num->name, "Digital playback");
  576. /* Associate private data to PCM device */
  577. pcm_playback_num->private_data = pcm_device;
  578. /* set operators of PCM device */
  579. snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
  580. &snd_aw2_playback_ops);
  581. /* store PCM device */
  582. pcm_device->pcm = pcm_playback_num;
  583. /* give base chip pointer to our internal pcm device
  584. structure */
  585. pcm_device->chip = chip;
  586. /* Give stream number to PCM device */
  587. pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
  588. /* pre-allocation of buffers */
  589. /* Preallocate continuous pages. */
  590. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
  591. SNDRV_DMA_TYPE_DEV,
  592. snd_dma_pci_data
  593. (chip->pci),
  594. 64 * 1024, 64 * 1024);
  595. if (err)
  596. printk(KERN_ERR
  597. "aw2: snd_pcm_lib_preallocate_pages_for_all error "
  598. "(0x%X)\n", err);
  599. err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
  600. &pcm_capture);
  601. if (err < 0) {
  602. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  603. return err;
  604. }
  605. /* Creation ok */
  606. pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
  607. /* Set PCM device name */
  608. strcpy(pcm_capture->name, "Capture");
  609. /* Associate private data to PCM device */
  610. pcm_capture->private_data = pcm_device;
  611. /* set operators of PCM device */
  612. snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
  613. &snd_aw2_capture_ops);
  614. /* store PCM device */
  615. pcm_device->pcm = pcm_capture;
  616. /* give base chip pointer to our internal pcm device
  617. structure */
  618. pcm_device->chip = chip;
  619. /* Give stream number to PCM device */
  620. pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
  621. /* pre-allocation of buffers */
  622. /* Preallocate continuous pages. */
  623. err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
  624. SNDRV_DMA_TYPE_DEV,
  625. snd_dma_pci_data
  626. (chip->pci),
  627. 64 * 1024, 64 * 1024);
  628. if (err)
  629. printk(KERN_ERR
  630. "aw2: snd_pcm_lib_preallocate_pages_for_all error "
  631. "(0x%X)\n", err);
  632. /* Create control */
  633. err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
  634. if (err < 0) {
  635. printk(KERN_ERR "aw2: snd_ctl_add error (0x%X)\n", err);
  636. return err;
  637. }
  638. return 0;
  639. }
  640. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  641. struct snd_ctl_elem_info *uinfo)
  642. {
  643. static char *texts[2] = {
  644. "Analog", "Digital"
  645. };
  646. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  647. uinfo->count = 1;
  648. uinfo->value.enumerated.items = 2;
  649. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
  650. uinfo->value.enumerated.item =
  651. uinfo->value.enumerated.items - 1;
  652. }
  653. strcpy(uinfo->value.enumerated.name,
  654. texts[uinfo->value.enumerated.item]);
  655. return 0;
  656. }
  657. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  658. struct snd_ctl_elem_value
  659. *ucontrol)
  660. {
  661. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  662. if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
  663. ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
  664. else
  665. ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
  666. return 0;
  667. }
  668. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  669. struct snd_ctl_elem_value
  670. *ucontrol)
  671. {
  672. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  673. int changed = 0;
  674. int is_disgital =
  675. snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
  676. if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
  677. && !is_disgital)
  678. || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
  679. && is_disgital)) {
  680. snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
  681. changed = 1;
  682. }
  683. return changed;
  684. }