aw2-alsa.c 22 KB

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