cx88-alsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. *
  3. * Support for audio capture
  4. * PCI function #1 of the cx2388x.
  5. *
  6. * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
  7. * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
  8. * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  9. * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
  10. * Based on dummy.c by Jaroslav Kysela <perex@suse.cz>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/pci.h>
  33. #include <asm/delay.h>
  34. #include <sound/driver.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include <sound/control.h>
  39. #include <sound/initval.h>
  40. #include "cx88.h"
  41. #include "cx88-reg.h"
  42. #define dprintk(level,fmt, arg...) if (debug >= level) \
  43. printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg)
  44. #define dprintk_core(level,fmt, arg...) if (debug >= level) \
  45. printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
  46. /****************************************************************************
  47. Data type declarations - Can be moded to a header file later
  48. ****************************************************************************/
  49. struct cx88_audio_dev {
  50. struct cx88_core *core;
  51. struct cx88_dmaqueue q;
  52. /* pci i/o */
  53. struct pci_dev *pci;
  54. /* audio controls */
  55. int irq;
  56. struct snd_card *card;
  57. spinlock_t reg_lock;
  58. atomic_t count;
  59. unsigned int dma_size;
  60. unsigned int period_size;
  61. unsigned int num_periods;
  62. struct videobuf_dmabuf dma_risc;
  63. struct cx88_buffer *buf;
  64. struct snd_pcm_substream *substream;
  65. };
  66. typedef struct cx88_audio_dev snd_cx88_card_t;
  67. /****************************************************************************
  68. Module global static vars
  69. ****************************************************************************/
  70. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  71. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  72. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  73. module_param_array(enable, bool, NULL, 0444);
  74. MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
  75. module_param_array(index, int, NULL, 0444);
  76. MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
  77. /****************************************************************************
  78. Module macros
  79. ****************************************************************************/
  80. MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
  81. MODULE_AUTHOR("Ricardo Cerqueira");
  82. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  83. MODULE_LICENSE("GPL");
  84. MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
  85. "{{Conexant,23882},"
  86. "{{Conexant,23883}");
  87. static unsigned int debug;
  88. module_param(debug,int,0644);
  89. MODULE_PARM_DESC(debug,"enable debug messages");
  90. /****************************************************************************
  91. Module specific funtions
  92. ****************************************************************************/
  93. /*
  94. * BOARD Specific: Sets audio DMA
  95. */
  96. static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
  97. {
  98. struct cx88_buffer *buf = chip->buf;
  99. struct cx88_core *core=chip->core;
  100. struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
  101. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  102. cx_clear(MO_AUD_DMACNTRL, 0x11);
  103. /* setup fifo + format - out channel */
  104. cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
  105. /* sets bpl size */
  106. cx_write(MO_AUDD_LNGTH, buf->bpl);
  107. /* reset counter */
  108. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  109. atomic_set(&chip->count, 0);
  110. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
  111. "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
  112. chip->num_periods, buf->bpl * chip->num_periods);
  113. /* Enables corresponding bits at AUD_INT_STAT */
  114. cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  115. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  116. /* Clean any pending interrupt bits already set */
  117. cx_write(MO_AUD_INTSTAT, ~0);
  118. /* enable audio irqs */
  119. cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
  120. /* start dma */
  121. cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  122. cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
  123. if (debug)
  124. cx88_sram_channel_dump(chip->core, audio_ch);
  125. return 0;
  126. }
  127. /*
  128. * BOARD Specific: Resets audio DMA
  129. */
  130. static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
  131. {
  132. struct cx88_core *core=chip->core;
  133. dprintk(1, "Stopping audio DMA\n");
  134. /* stop dma */
  135. cx_clear(MO_AUD_DMACNTRL, 0x11);
  136. /* disable irqs */
  137. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  138. cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  139. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  140. if (debug)
  141. cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
  142. return 0;
  143. }
  144. #define MAX_IRQ_LOOP 50
  145. /*
  146. * BOARD Specific: IRQ dma bits
  147. */
  148. static char *cx88_aud_irqs[32] = {
  149. "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
  150. NULL, /* reserved */
  151. "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
  152. NULL, /* reserved */
  153. "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
  154. NULL, /* reserved */
  155. "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
  156. NULL, /* reserved */
  157. "opc_err", "par_err", "rip_err", /* 16-18 */
  158. "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
  159. };
  160. /*
  161. * BOARD Specific: Threats IRQ audio specific calls
  162. */
  163. static void cx8801_aud_irq(snd_cx88_card_t *chip)
  164. {
  165. struct cx88_core *core = chip->core;
  166. u32 status, mask;
  167. status = cx_read(MO_AUD_INTSTAT);
  168. mask = cx_read(MO_AUD_INTMSK);
  169. if (0 == (status & mask))
  170. return;
  171. cx_write(MO_AUD_INTSTAT, status);
  172. if (debug > 1 || (status & mask & ~0xff))
  173. cx88_print_irqbits(core->name, "irq aud",
  174. cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
  175. status, mask);
  176. /* risc op code error */
  177. if (status & AUD_INT_OPC_ERR) {
  178. printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
  179. cx_clear(MO_AUD_DMACNTRL, 0x11);
  180. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
  181. }
  182. if (status & AUD_INT_DN_SYNC) {
  183. dprintk(1, "Downstream sync error\n");
  184. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  185. return;
  186. }
  187. /* risc1 downstream */
  188. if (status & AUD_INT_DN_RISCI1) {
  189. atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
  190. snd_pcm_period_elapsed(chip->substream);
  191. }
  192. /* FIXME: Any other status should deserve a special handling? */
  193. }
  194. /*
  195. * BOARD Specific: Handles IRQ calls
  196. */
  197. static irqreturn_t cx8801_irq(int irq, void *dev_id)
  198. {
  199. snd_cx88_card_t *chip = dev_id;
  200. struct cx88_core *core = chip->core;
  201. u32 status;
  202. int loop, handled = 0;
  203. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  204. status = cx_read(MO_PCI_INTSTAT) &
  205. (core->pci_irqmask | PCI_INT_AUDINT);
  206. if (0 == status)
  207. goto out;
  208. dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
  209. loop, MAX_IRQ_LOOP, status);
  210. handled = 1;
  211. cx_write(MO_PCI_INTSTAT, status);
  212. if (status & core->pci_irqmask)
  213. cx88_core_irq(core, status);
  214. if (status & PCI_INT_AUDINT)
  215. cx8801_aud_irq(chip);
  216. }
  217. if (MAX_IRQ_LOOP == loop) {
  218. printk(KERN_ERR
  219. "%s/1: IRQ loop detected, disabling interrupts\n",
  220. core->name);
  221. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  222. }
  223. out:
  224. return IRQ_RETVAL(handled);
  225. }
  226. static int dsp_buffer_free(snd_cx88_card_t *chip)
  227. {
  228. BUG_ON(!chip->dma_size);
  229. dprintk(2,"Freeing buffer\n");
  230. videobuf_pci_dma_unmap(chip->pci, &chip->dma_risc);
  231. videobuf_dma_free(&chip->dma_risc);
  232. btcx_riscmem_free(chip->pci,&chip->buf->risc);
  233. kfree(chip->buf);
  234. chip->dma_size = 0;
  235. return 0;
  236. }
  237. /****************************************************************************
  238. ALSA PCM Interface
  239. ****************************************************************************/
  240. /*
  241. * Digital hardware definition
  242. */
  243. #define DEFAULT_FIFO_SIZE 4096
  244. static struct snd_pcm_hardware snd_cx88_digital_hw = {
  245. .info = SNDRV_PCM_INFO_MMAP |
  246. SNDRV_PCM_INFO_INTERLEAVED |
  247. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  248. SNDRV_PCM_INFO_MMAP_VALID,
  249. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  250. .rates = SNDRV_PCM_RATE_48000,
  251. .rate_min = 48000,
  252. .rate_max = 48000,
  253. .channels_min = 2,
  254. .channels_max = 2,
  255. /* Analog audio output will be full of clicks and pops if there
  256. are not exactly four lines in the SRAM FIFO buffer. */
  257. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  258. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  259. .periods_min = 1,
  260. .periods_max = 1024,
  261. .buffer_bytes_max = (1024*1024),
  262. };
  263. /*
  264. * audio pcm capture open callback
  265. */
  266. static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
  267. {
  268. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  269. struct snd_pcm_runtime *runtime = substream->runtime;
  270. int err;
  271. err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
  272. if (err < 0)
  273. goto _error;
  274. chip->substream = substream;
  275. runtime->hw = snd_cx88_digital_hw;
  276. if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
  277. unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
  278. bpl &= ~7; /* must be multiple of 8 */
  279. runtime->hw.period_bytes_min = bpl;
  280. runtime->hw.period_bytes_max = bpl;
  281. }
  282. return 0;
  283. _error:
  284. dprintk(1,"Error opening PCM!\n");
  285. return err;
  286. }
  287. /*
  288. * audio close callback
  289. */
  290. static int snd_cx88_close(struct snd_pcm_substream *substream)
  291. {
  292. return 0;
  293. }
  294. /*
  295. * hw_params callback
  296. */
  297. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  298. struct snd_pcm_hw_params * hw_params)
  299. {
  300. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  301. struct cx88_buffer *buf;
  302. int ret;
  303. if (substream->runtime->dma_area) {
  304. dsp_buffer_free(chip);
  305. substream->runtime->dma_area = NULL;
  306. }
  307. chip->period_size = params_period_bytes(hw_params);
  308. chip->num_periods = params_periods(hw_params);
  309. chip->dma_size = chip->period_size * params_periods(hw_params);
  310. BUG_ON(!chip->dma_size);
  311. BUG_ON(chip->num_periods & (chip->num_periods-1));
  312. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  313. if (NULL == buf)
  314. return -ENOMEM;
  315. buf->vb.memory = V4L2_MEMORY_MMAP;
  316. buf->vb.field = V4L2_FIELD_NONE;
  317. buf->vb.width = chip->period_size;
  318. buf->bpl = chip->period_size;
  319. buf->vb.height = chip->num_periods;
  320. buf->vb.size = chip->dma_size;
  321. videobuf_dma_init(&buf->vb.dma);
  322. ret = videobuf_dma_init_kernel(&buf->vb.dma, PCI_DMA_FROMDEVICE,
  323. (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
  324. if (ret < 0)
  325. goto error;
  326. ret = videobuf_pci_dma_map(chip->pci,&buf->vb.dma);
  327. if (ret < 0)
  328. goto error;
  329. ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->vb.dma.sglist,
  330. buf->vb.width, buf->vb.height, 1);
  331. if (ret < 0)
  332. goto error;
  333. /* Loop back to start of program */
  334. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  335. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  336. buf->vb.state = STATE_PREPARED;
  337. chip->buf = buf;
  338. chip->dma_risc = buf->vb.dma;
  339. substream->runtime->dma_area = chip->dma_risc.vmalloc;
  340. substream->runtime->dma_bytes = chip->dma_size;
  341. substream->runtime->dma_addr = 0;
  342. return 0;
  343. error:
  344. kfree(buf);
  345. return ret;
  346. }
  347. /*
  348. * hw free callback
  349. */
  350. static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
  351. {
  352. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  353. if (substream->runtime->dma_area) {
  354. dsp_buffer_free(chip);
  355. substream->runtime->dma_area = NULL;
  356. }
  357. return 0;
  358. }
  359. /*
  360. * prepare callback
  361. */
  362. static int snd_cx88_prepare(struct snd_pcm_substream *substream)
  363. {
  364. return 0;
  365. }
  366. /*
  367. * trigger callback
  368. */
  369. static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
  370. {
  371. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  372. int err;
  373. /* Local interrupts are already disabled by ALSA */
  374. spin_lock(&chip->reg_lock);
  375. switch (cmd) {
  376. case SNDRV_PCM_TRIGGER_START:
  377. err=_cx88_start_audio_dma(chip);
  378. break;
  379. case SNDRV_PCM_TRIGGER_STOP:
  380. err=_cx88_stop_audio_dma(chip);
  381. break;
  382. default:
  383. err=-EINVAL;
  384. break;
  385. }
  386. spin_unlock(&chip->reg_lock);
  387. return err;
  388. }
  389. /*
  390. * pointer callback
  391. */
  392. static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
  393. {
  394. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  395. struct snd_pcm_runtime *runtime = substream->runtime;
  396. u16 count;
  397. count = atomic_read(&chip->count);
  398. // dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __FUNCTION__,
  399. // count, new, count & (runtime->periods-1),
  400. // runtime->period_size * (count & (runtime->periods-1)));
  401. return runtime->period_size * (count & (runtime->periods-1));
  402. }
  403. /*
  404. * page callback (needed for mmap)
  405. */
  406. static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
  407. unsigned long offset)
  408. {
  409. void *pageptr = substream->runtime->dma_area + offset;
  410. return vmalloc_to_page(pageptr);
  411. }
  412. /*
  413. * operators
  414. */
  415. static struct snd_pcm_ops snd_cx88_pcm_ops = {
  416. .open = snd_cx88_pcm_open,
  417. .close = snd_cx88_close,
  418. .ioctl = snd_pcm_lib_ioctl,
  419. .hw_params = snd_cx88_hw_params,
  420. .hw_free = snd_cx88_hw_free,
  421. .prepare = snd_cx88_prepare,
  422. .trigger = snd_cx88_card_trigger,
  423. .pointer = snd_cx88_pointer,
  424. .page = snd_cx88_page,
  425. };
  426. /*
  427. * create a PCM device
  428. */
  429. static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
  430. {
  431. int err;
  432. struct snd_pcm *pcm;
  433. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  434. if (err < 0)
  435. return err;
  436. pcm->private_data = chip;
  437. strcpy(pcm->name, name);
  438. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
  439. return 0;
  440. }
  441. /****************************************************************************
  442. CONTROL INTERFACE
  443. ****************************************************************************/
  444. static int snd_cx88_capture_volume_info(struct snd_kcontrol *kcontrol,
  445. struct snd_ctl_elem_info *info)
  446. {
  447. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  448. info->count = 2;
  449. info->value.integer.min = 0;
  450. info->value.integer.max = 0x3f;
  451. return 0;
  452. }
  453. /* OK - TODO: test it */
  454. static int snd_cx88_capture_volume_get(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_value *value)
  456. {
  457. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  458. struct cx88_core *core=chip->core;
  459. int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
  460. bal = cx_read(AUD_BAL_CTL);
  461. value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
  462. vol -= (bal & 0x3f);
  463. value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
  464. return 0;
  465. }
  466. /* OK - TODO: test it */
  467. static int snd_cx88_capture_volume_put(struct snd_kcontrol *kcontrol,
  468. struct snd_ctl_elem_value *value)
  469. {
  470. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  471. struct cx88_core *core=chip->core;
  472. int v, b;
  473. int changed = 0;
  474. u32 old;
  475. b = value->value.integer.value[1] - value->value.integer.value[0];
  476. if (b < 0) {
  477. v = 0x3f - value->value.integer.value[0];
  478. b = (-b) | 0x40;
  479. } else {
  480. v = 0x3f - value->value.integer.value[1];
  481. }
  482. /* Do we really know this will always be called with IRQs on? */
  483. spin_lock_irq(&chip->reg_lock);
  484. old = cx_read(AUD_VOL_CTL);
  485. if (v != (old & 0x3f)) {
  486. cx_write(AUD_VOL_CTL, (old & ~0x3f) | v);
  487. changed = 1;
  488. }
  489. if (cx_read(AUD_BAL_CTL) != b) {
  490. cx_write(AUD_BAL_CTL, b);
  491. changed = 1;
  492. }
  493. spin_unlock_irq(&chip->reg_lock);
  494. return changed;
  495. }
  496. static struct snd_kcontrol_new snd_cx88_capture_volume = {
  497. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  498. .name = "Capture Volume",
  499. .info = snd_cx88_capture_volume_info,
  500. .get = snd_cx88_capture_volume_get,
  501. .put = snd_cx88_capture_volume_put,
  502. };
  503. /****************************************************************************
  504. Basic Flow for Sound Devices
  505. ****************************************************************************/
  506. /*
  507. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  508. * Only boards with eeprom and byte 1 at eeprom=1 have it
  509. */
  510. static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
  511. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  512. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  513. {0, }
  514. };
  515. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  516. /*
  517. * Chip-specific destructor
  518. */
  519. static int snd_cx88_free(snd_cx88_card_t *chip)
  520. {
  521. if (chip->irq >= 0){
  522. synchronize_irq(chip->irq);
  523. free_irq(chip->irq, chip);
  524. }
  525. cx88_core_put(chip->core,chip->pci);
  526. pci_disable_device(chip->pci);
  527. return 0;
  528. }
  529. /*
  530. * Component Destructor
  531. */
  532. static void snd_cx88_dev_free(struct snd_card * card)
  533. {
  534. snd_cx88_card_t *chip = card->private_data;
  535. snd_cx88_free(chip);
  536. }
  537. /*
  538. * Alsa Constructor - Component probe
  539. */
  540. static int devno;
  541. static int __devinit snd_cx88_create(struct snd_card *card,
  542. struct pci_dev *pci,
  543. snd_cx88_card_t **rchip)
  544. {
  545. snd_cx88_card_t *chip;
  546. struct cx88_core *core;
  547. int err;
  548. unsigned char pci_lat;
  549. *rchip = NULL;
  550. err = pci_enable_device(pci);
  551. if (err < 0)
  552. return err;
  553. pci_set_master(pci);
  554. chip = (snd_cx88_card_t *) card->private_data;
  555. core = cx88_core_get(pci);
  556. if (NULL == core) {
  557. err = -EINVAL;
  558. kfree (chip);
  559. return err;
  560. }
  561. if (!pci_dma_supported(pci,DMA_32BIT_MASK)) {
  562. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  563. err = -EIO;
  564. cx88_core_put(core,pci);
  565. return err;
  566. }
  567. /* pci init */
  568. chip->card = card;
  569. chip->pci = pci;
  570. chip->irq = -1;
  571. spin_lock_init(&chip->reg_lock);
  572. chip->core = core;
  573. /* get irq */
  574. err = request_irq(chip->pci->irq, cx8801_irq,
  575. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  576. if (err < 0) {
  577. dprintk(0, "%s: can't get IRQ %d\n",
  578. chip->core->name, chip->pci->irq);
  579. return err;
  580. }
  581. /* print pci info */
  582. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  583. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  584. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  585. pci_name(pci), pci->revision, pci->irq,
  586. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  587. chip->irq = pci->irq;
  588. synchronize_irq(chip->irq);
  589. snd_card_set_dev(card, &pci->dev);
  590. *rchip = chip;
  591. return 0;
  592. }
  593. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  594. const struct pci_device_id *pci_id)
  595. {
  596. struct snd_card *card;
  597. snd_cx88_card_t *chip;
  598. int err;
  599. if (devno >= SNDRV_CARDS)
  600. return (-ENODEV);
  601. if (!enable[devno]) {
  602. ++devno;
  603. return (-ENOENT);
  604. }
  605. card = snd_card_new(index[devno], id[devno], THIS_MODULE, sizeof(snd_cx88_card_t));
  606. if (!card)
  607. return (-ENOMEM);
  608. card->private_free = snd_cx88_dev_free;
  609. err = snd_cx88_create(card, pci, &chip);
  610. if (err < 0)
  611. return (err);
  612. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  613. if (err < 0)
  614. goto error;
  615. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_capture_volume, chip));
  616. if (err < 0)
  617. goto error;
  618. strcpy (card->driver, "CX88x");
  619. sprintf(card->shortname, "Conexant CX%x", pci->device);
  620. sprintf(card->longname, "%s at %#llx",
  621. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  622. strcpy (card->mixername, "CX88");
  623. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  624. card->driver,devno);
  625. err = snd_card_register(card);
  626. if (err < 0)
  627. goto error;
  628. pci_set_drvdata(pci,card);
  629. devno++;
  630. return 0;
  631. error:
  632. snd_card_free(card);
  633. return err;
  634. }
  635. /*
  636. * ALSA destructor
  637. */
  638. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  639. {
  640. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  641. snd_card_free((void *)card);
  642. pci_set_drvdata(pci, NULL);
  643. devno--;
  644. }
  645. /*
  646. * PCI driver definition
  647. */
  648. static struct pci_driver cx88_audio_pci_driver = {
  649. .name = "cx88_audio",
  650. .id_table = cx88_audio_pci_tbl,
  651. .probe = cx88_audio_initdev,
  652. .remove = cx88_audio_finidev,
  653. };
  654. /****************************************************************************
  655. LINUX MODULE INIT
  656. ****************************************************************************/
  657. /*
  658. * module init
  659. */
  660. static int cx88_audio_init(void)
  661. {
  662. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  663. (CX88_VERSION_CODE >> 16) & 0xff,
  664. (CX88_VERSION_CODE >> 8) & 0xff,
  665. CX88_VERSION_CODE & 0xff);
  666. #ifdef SNAPSHOT
  667. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  668. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  669. #endif
  670. return pci_register_driver(&cx88_audio_pci_driver);
  671. }
  672. /*
  673. * module remove
  674. */
  675. static void cx88_audio_fini(void)
  676. {
  677. pci_unregister_driver(&cx88_audio_pci_driver);
  678. }
  679. module_init(cx88_audio_init);
  680. module_exit(cx88_audio_fini);
  681. /* ----------------------------------------------------------- */
  682. /*
  683. * Local variables:
  684. * c-basic-offset: 8
  685. * End:
  686. */