cx88-alsa.c 20 KB

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