aw2-alsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 snd_aw2_create(struct snd_card *card,
  105. struct pci_dev *pci, struct aw2 **rchip);
  106. static int snd_aw2_probe(struct pci_dev *pci,
  107. const struct pci_device_id *pci_id);
  108. static void 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 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 = 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 = {
  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 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 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 snd_aw2_remove(struct pci_dev *pci)
  341. {
  342. snd_card_free(pci_get_drvdata(pci));
  343. }
  344. /* open callback */
  345. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
  346. {
  347. struct snd_pcm_runtime *runtime = substream->runtime;
  348. snd_printdd(KERN_DEBUG "aw2: Playback_open\n");
  349. runtime->hw = snd_aw2_playback_hw;
  350. return 0;
  351. }
  352. /* close callback */
  353. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
  354. {
  355. return 0;
  356. }
  357. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
  358. {
  359. struct snd_pcm_runtime *runtime = substream->runtime;
  360. snd_printdd(KERN_DEBUG "aw2: Capture_open\n");
  361. runtime->hw = snd_aw2_capture_hw;
  362. return 0;
  363. }
  364. /* close callback */
  365. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
  366. {
  367. /* TODO: something to do ? */
  368. return 0;
  369. }
  370. /* hw_params callback */
  371. static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
  372. struct snd_pcm_hw_params *hw_params)
  373. {
  374. return snd_pcm_lib_malloc_pages(substream,
  375. params_buffer_bytes(hw_params));
  376. }
  377. /* hw_free callback */
  378. static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
  379. {
  380. return snd_pcm_lib_free_pages(substream);
  381. }
  382. /* prepare callback for playback */
  383. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
  384. {
  385. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  386. struct aw2 *chip = pcm_device->chip;
  387. struct snd_pcm_runtime *runtime = substream->runtime;
  388. unsigned long period_size, buffer_size;
  389. mutex_lock(&chip->mtx);
  390. period_size = snd_pcm_lib_period_bytes(substream);
  391. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  392. snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
  393. pcm_device->stream_number,
  394. runtime->dma_addr, period_size,
  395. buffer_size);
  396. /* Define Interrupt callback */
  397. snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
  398. (snd_aw2_saa7146_it_cb)
  399. snd_pcm_period_elapsed,
  400. (void *)substream);
  401. mutex_unlock(&chip->mtx);
  402. return 0;
  403. }
  404. /* prepare callback for capture */
  405. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
  406. {
  407. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  408. struct aw2 *chip = pcm_device->chip;
  409. struct snd_pcm_runtime *runtime = substream->runtime;
  410. unsigned long period_size, buffer_size;
  411. mutex_lock(&chip->mtx);
  412. period_size = snd_pcm_lib_period_bytes(substream);
  413. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  414. snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
  415. pcm_device->stream_number,
  416. runtime->dma_addr, period_size,
  417. buffer_size);
  418. /* Define Interrupt callback */
  419. snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
  420. (snd_aw2_saa7146_it_cb)
  421. snd_pcm_period_elapsed,
  422. (void *)substream);
  423. mutex_unlock(&chip->mtx);
  424. return 0;
  425. }
  426. /* playback trigger callback */
  427. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  428. int cmd)
  429. {
  430. int status = 0;
  431. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  432. struct aw2 *chip = pcm_device->chip;
  433. spin_lock(&chip->reg_lock);
  434. switch (cmd) {
  435. case SNDRV_PCM_TRIGGER_START:
  436. snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
  437. pcm_device->
  438. stream_number);
  439. break;
  440. case SNDRV_PCM_TRIGGER_STOP:
  441. snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
  442. pcm_device->
  443. stream_number);
  444. break;
  445. default:
  446. status = -EINVAL;
  447. }
  448. spin_unlock(&chip->reg_lock);
  449. return status;
  450. }
  451. /* capture trigger callback */
  452. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  453. int cmd)
  454. {
  455. int status = 0;
  456. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  457. struct aw2 *chip = pcm_device->chip;
  458. spin_lock(&chip->reg_lock);
  459. switch (cmd) {
  460. case SNDRV_PCM_TRIGGER_START:
  461. snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
  462. pcm_device->
  463. stream_number);
  464. break;
  465. case SNDRV_PCM_TRIGGER_STOP:
  466. snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
  467. pcm_device->
  468. stream_number);
  469. break;
  470. default:
  471. status = -EINVAL;
  472. }
  473. spin_unlock(&chip->reg_lock);
  474. return status;
  475. }
  476. /* playback pointer callback */
  477. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  478. *substream)
  479. {
  480. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  481. struct aw2 *chip = pcm_device->chip;
  482. unsigned int current_ptr;
  483. /* get the current hardware pointer */
  484. struct snd_pcm_runtime *runtime = substream->runtime;
  485. current_ptr =
  486. snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
  487. pcm_device->stream_number,
  488. runtime->dma_area,
  489. runtime->buffer_size);
  490. return bytes_to_frames(substream->runtime, current_ptr);
  491. }
  492. /* capture pointer callback */
  493. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(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_capture(&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. /* create a pcm device */
  509. static int snd_aw2_new_pcm(struct aw2 *chip)
  510. {
  511. struct snd_pcm *pcm_playback_ana;
  512. struct snd_pcm *pcm_playback_num;
  513. struct snd_pcm *pcm_capture;
  514. struct aw2_pcm_device *pcm_device;
  515. int err = 0;
  516. /* Create new Alsa PCM device */
  517. err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
  518. &pcm_playback_ana);
  519. if (err < 0) {
  520. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  521. return err;
  522. }
  523. /* Creation ok */
  524. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
  525. /* Set PCM device name */
  526. strcpy(pcm_playback_ana->name, "Analog playback");
  527. /* Associate private data to PCM device */
  528. pcm_playback_ana->private_data = pcm_device;
  529. /* set operators of PCM device */
  530. snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
  531. &snd_aw2_playback_ops);
  532. /* store PCM device */
  533. pcm_device->pcm = pcm_playback_ana;
  534. /* give base chip pointer to our internal pcm device
  535. structure */
  536. pcm_device->chip = chip;
  537. /* Give stream number to PCM device */
  538. pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
  539. /* pre-allocation of buffers */
  540. /* Preallocate continuous pages. */
  541. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
  542. SNDRV_DMA_TYPE_DEV,
  543. snd_dma_pci_data
  544. (chip->pci),
  545. 64 * 1024, 64 * 1024);
  546. if (err)
  547. printk(KERN_ERR "aw2: snd_pcm_lib_preallocate_pages_for_all "
  548. "error (0x%X)\n", err);
  549. err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
  550. &pcm_playback_num);
  551. if (err < 0) {
  552. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  553. return err;
  554. }
  555. /* Creation ok */
  556. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
  557. /* Set PCM device name */
  558. strcpy(pcm_playback_num->name, "Digital playback");
  559. /* Associate private data to PCM device */
  560. pcm_playback_num->private_data = pcm_device;
  561. /* set operators of PCM device */
  562. snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
  563. &snd_aw2_playback_ops);
  564. /* store PCM device */
  565. pcm_device->pcm = pcm_playback_num;
  566. /* give base chip pointer to our internal pcm device
  567. structure */
  568. pcm_device->chip = chip;
  569. /* Give stream number to PCM device */
  570. pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
  571. /* pre-allocation of buffers */
  572. /* Preallocate continuous pages. */
  573. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
  574. SNDRV_DMA_TYPE_DEV,
  575. snd_dma_pci_data
  576. (chip->pci),
  577. 64 * 1024, 64 * 1024);
  578. if (err)
  579. printk(KERN_ERR
  580. "aw2: snd_pcm_lib_preallocate_pages_for_all error "
  581. "(0x%X)\n", err);
  582. err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
  583. &pcm_capture);
  584. if (err < 0) {
  585. printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
  586. return err;
  587. }
  588. /* Creation ok */
  589. pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
  590. /* Set PCM device name */
  591. strcpy(pcm_capture->name, "Capture");
  592. /* Associate private data to PCM device */
  593. pcm_capture->private_data = pcm_device;
  594. /* set operators of PCM device */
  595. snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
  596. &snd_aw2_capture_ops);
  597. /* store PCM device */
  598. pcm_device->pcm = pcm_capture;
  599. /* give base chip pointer to our internal pcm device
  600. structure */
  601. pcm_device->chip = chip;
  602. /* Give stream number to PCM device */
  603. pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
  604. /* pre-allocation of buffers */
  605. /* Preallocate continuous pages. */
  606. err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
  607. SNDRV_DMA_TYPE_DEV,
  608. snd_dma_pci_data
  609. (chip->pci),
  610. 64 * 1024, 64 * 1024);
  611. if (err)
  612. printk(KERN_ERR
  613. "aw2: snd_pcm_lib_preallocate_pages_for_all error "
  614. "(0x%X)\n", err);
  615. /* Create control */
  616. err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
  617. if (err < 0) {
  618. printk(KERN_ERR "aw2: snd_ctl_add error (0x%X)\n", err);
  619. return err;
  620. }
  621. return 0;
  622. }
  623. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  624. struct snd_ctl_elem_info *uinfo)
  625. {
  626. static char *texts[2] = {
  627. "Analog", "Digital"
  628. };
  629. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  630. uinfo->count = 1;
  631. uinfo->value.enumerated.items = 2;
  632. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
  633. uinfo->value.enumerated.item =
  634. uinfo->value.enumerated.items - 1;
  635. }
  636. strcpy(uinfo->value.enumerated.name,
  637. texts[uinfo->value.enumerated.item]);
  638. return 0;
  639. }
  640. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  641. struct snd_ctl_elem_value
  642. *ucontrol)
  643. {
  644. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  645. if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
  646. ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
  647. else
  648. ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
  649. return 0;
  650. }
  651. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  652. struct snd_ctl_elem_value
  653. *ucontrol)
  654. {
  655. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  656. int changed = 0;
  657. int is_disgital =
  658. snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
  659. if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
  660. && !is_disgital)
  661. || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
  662. && is_disgital)) {
  663. snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
  664. changed = 1;
  665. }
  666. return changed;
  667. }