cx88-alsa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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, status, mask);
  185. /* risc op code error */
  186. if (status & (1 << 16)) {
  187. printk(KERN_WARNING "%s/0: audio risc op code error\n",core->name);
  188. cx_clear(MO_AUD_DMACNTRL, 0x11);
  189. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
  190. }
  191. /* risc1 downstream */
  192. if (status & 0x01) {
  193. spin_lock(&chip->reg_lock);
  194. count = cx_read(MO_AUDD_GPCNT);
  195. spin_unlock(&chip->reg_lock);
  196. if (chip->read_count == 0)
  197. chip->read_count += chip->dma_size;
  198. }
  199. if (chip->read_count >= chip->period_size) {
  200. dprintk(2, "Elapsing period\n");
  201. snd_pcm_period_elapsed(chip->substream);
  202. }
  203. dprintk(3,"Leaving audio IRQ handler...\n");
  204. /* FIXME: Any other status should deserve a special handling? */
  205. }
  206. /*
  207. * BOARD Specific: Handles IRQ calls
  208. */
  209. static irqreturn_t cx8801_irq(int irq, void *dev_id)
  210. {
  211. snd_cx88_card_t *chip = dev_id;
  212. struct cx88_core *core = chip->core;
  213. u32 status;
  214. int loop, handled = 0;
  215. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  216. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x02);
  217. if (0 == status)
  218. goto out;
  219. dprintk( 3, "cx8801_irq\n" );
  220. dprintk( 3, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
  221. dprintk( 3, " status: %d\n", status );
  222. handled = 1;
  223. cx_write(MO_PCI_INTSTAT, status);
  224. if (status & 0x02)
  225. {
  226. dprintk( 2, " ALSA IRQ handling\n" );
  227. cx8801_aud_irq(chip);
  228. }
  229. };
  230. if (MAX_IRQ_LOOP == loop) {
  231. dprintk( 0, "clearing mask\n" );
  232. dprintk(1,"%s/0: irq loop -- clearing mask\n",
  233. core->name);
  234. cx_clear(MO_PCI_INTMSK,0x02);
  235. }
  236. out:
  237. return IRQ_RETVAL(handled);
  238. }
  239. static int dsp_buffer_free(snd_cx88_card_t *chip)
  240. {
  241. BUG_ON(!chip->dma_size);
  242. dprintk(2,"Freeing buffer\n");
  243. videobuf_pci_dma_unmap(chip->pci, &chip->dma_risc);
  244. videobuf_dma_free(&chip->dma_risc);
  245. btcx_riscmem_free(chip->pci,&chip->buf->risc);
  246. kfree(chip->buf);
  247. chip->dma_size = 0;
  248. return 0;
  249. }
  250. /****************************************************************************
  251. ALSA PCM Interface
  252. ****************************************************************************/
  253. /*
  254. * Digital hardware definition
  255. */
  256. static struct snd_pcm_hardware snd_cx88_digital_hw = {
  257. .info = SNDRV_PCM_INFO_MMAP |
  258. SNDRV_PCM_INFO_INTERLEAVED |
  259. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  260. SNDRV_PCM_INFO_MMAP_VALID,
  261. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  262. .rates = SNDRV_PCM_RATE_48000,
  263. .rate_min = 48000,
  264. .rate_max = 48000,
  265. .channels_min = 1,
  266. .channels_max = 2,
  267. .buffer_bytes_max = (2*2048),
  268. .period_bytes_min = 2048,
  269. .period_bytes_max = 2048,
  270. .periods_min = 2,
  271. .periods_max = 2,
  272. };
  273. /*
  274. * audio pcm capture runtime free
  275. */
  276. static void snd_card_cx88_runtime_free(struct snd_pcm_runtime *runtime)
  277. {
  278. }
  279. /*
  280. * audio pcm capture open callback
  281. */
  282. static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
  283. {
  284. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  285. struct snd_pcm_runtime *runtime = substream->runtime;
  286. int err;
  287. if (test_and_set_bit(0, &chip->opened))
  288. return -EBUSY;
  289. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  290. if (err < 0)
  291. goto _error;
  292. chip->substream = substream;
  293. chip->read_count = 0;
  294. chip->read_offset = 0;
  295. runtime->private_free = snd_card_cx88_runtime_free;
  296. runtime->hw = snd_cx88_digital_hw;
  297. return 0;
  298. _error:
  299. dprintk(1,"Error opening PCM!\n");
  300. clear_bit(0, &chip->opened);
  301. smp_mb__after_clear_bit();
  302. return err;
  303. }
  304. /*
  305. * audio close callback
  306. */
  307. static int snd_cx88_close(struct snd_pcm_substream *substream)
  308. {
  309. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  310. clear_bit(0, &chip->opened);
  311. smp_mb__after_clear_bit();
  312. return 0;
  313. }
  314. /*
  315. * hw_params callback
  316. */
  317. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  318. struct snd_pcm_hw_params * hw_params)
  319. {
  320. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  321. struct cx88_buffer *buf;
  322. if (substream->runtime->dma_area) {
  323. dsp_buffer_free(chip);
  324. substream->runtime->dma_area = NULL;
  325. }
  326. chip->period_size = params_period_bytes(hw_params);
  327. chip->num_periods = params_periods(hw_params);
  328. chip->dma_size = chip->period_size * params_periods(hw_params);
  329. BUG_ON(!chip->dma_size);
  330. dprintk(1,"Setting buffer\n");
  331. buf = kmalloc(sizeof(*buf),GFP_KERNEL);
  332. if (NULL == buf)
  333. return -ENOMEM;
  334. memset(buf,0,sizeof(*buf));
  335. buf->vb.memory = V4L2_MEMORY_MMAP;
  336. buf->vb.width = chip->period_size;
  337. buf->vb.height = chip->num_periods;
  338. buf->vb.size = chip->dma_size;
  339. buf->vb.field = V4L2_FIELD_NONE;
  340. videobuf_dma_init(&buf->vb.dma);
  341. videobuf_dma_init_kernel(&buf->vb.dma,PCI_DMA_FROMDEVICE,
  342. (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
  343. videobuf_pci_dma_map(chip->pci,&buf->vb.dma);
  344. cx88_risc_databuffer(chip->pci, &buf->risc,
  345. buf->vb.dma.sglist,
  346. buf->vb.width, buf->vb.height);
  347. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  348. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  349. buf->vb.state = STATE_PREPARED;
  350. buf->bpl = chip->period_size;
  351. chip->buf = buf;
  352. chip->dma_risc = buf->vb.dma;
  353. dprintk(1,"Buffer ready at %u\n",chip->dma_risc.nr_pages);
  354. substream->runtime->dma_area = chip->dma_risc.vmalloc;
  355. return 0;
  356. }
  357. /*
  358. * hw free callback
  359. */
  360. static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
  361. {
  362. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  363. if (substream->runtime->dma_area) {
  364. dsp_buffer_free(chip);
  365. substream->runtime->dma_area = NULL;
  366. }
  367. return 0;
  368. }
  369. /*
  370. * prepare callback
  371. */
  372. static int snd_cx88_prepare(struct snd_pcm_substream *substream)
  373. {
  374. return 0;
  375. }
  376. /*
  377. * trigger callback
  378. */
  379. static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
  380. {
  381. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  382. int err;
  383. spin_lock(&chip->reg_lock);
  384. switch (cmd) {
  385. case SNDRV_PCM_TRIGGER_START:
  386. err=_cx88_start_audio_dma(chip);
  387. break;
  388. case SNDRV_PCM_TRIGGER_STOP:
  389. err=_cx88_stop_audio_dma(chip);
  390. break;
  391. default:
  392. err=-EINVAL;
  393. break;
  394. }
  395. spin_unlock(&chip->reg_lock);
  396. return err;
  397. }
  398. /*
  399. * pointer callback
  400. */
  401. static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
  402. {
  403. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  404. struct snd_pcm_runtime *runtime = substream->runtime;
  405. if (chip->read_count) {
  406. chip->read_count -= snd_pcm_lib_period_bytes(substream);
  407. chip->read_offset += snd_pcm_lib_period_bytes(substream);
  408. if (chip->read_offset == chip->dma_size)
  409. chip->read_offset = 0;
  410. }
  411. dprintk(2, "Pointer time, will return %li, read %li\n",chip->read_offset,chip->read_count);
  412. return bytes_to_frames(runtime, chip->read_offset);
  413. }
  414. /*
  415. * operators
  416. */
  417. static struct snd_pcm_ops snd_cx88_pcm_ops = {
  418. .open = snd_cx88_pcm_open,
  419. .close = snd_cx88_close,
  420. .ioctl = snd_pcm_lib_ioctl,
  421. .hw_params = snd_cx88_hw_params,
  422. .hw_free = snd_cx88_hw_free,
  423. .prepare = snd_cx88_prepare,
  424. .trigger = snd_cx88_card_trigger,
  425. .pointer = snd_cx88_pointer,
  426. };
  427. /*
  428. * create a PCM device
  429. */
  430. static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
  431. {
  432. int err;
  433. struct snd_pcm *pcm;
  434. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  435. if (err < 0)
  436. return err;
  437. pcm->private_data = chip;
  438. strcpy(pcm->name, name);
  439. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
  440. return 0;
  441. }
  442. /****************************************************************************
  443. CONTROL INTERFACE
  444. ****************************************************************************/
  445. static int snd_cx88_capture_volume_info(struct snd_kcontrol *kcontrol,
  446. struct snd_ctl_elem_info *info)
  447. {
  448. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  449. info->count = 1;
  450. info->value.integer.min = 0;
  451. info->value.integer.max = 0x3f;
  452. return 0;
  453. }
  454. /* OK - TODO: test it */
  455. static int snd_cx88_capture_volume_get(struct snd_kcontrol *kcontrol,
  456. struct snd_ctl_elem_value *value)
  457. {
  458. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  459. struct cx88_core *core=chip->core;
  460. value->value.integer.value[0] = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
  461. return 0;
  462. }
  463. /* OK - TODO: test it */
  464. static int snd_cx88_capture_volume_put(struct snd_kcontrol *kcontrol,
  465. struct snd_ctl_elem_value *value)
  466. {
  467. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  468. struct cx88_core *core=chip->core;
  469. int v;
  470. u32 old_control;
  471. spin_lock_irq(&chip->reg_lock);
  472. old_control = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
  473. v = 0x3f - (value->value.integer.value[0] & 0x3f);
  474. cx_andor(AUD_VOL_CTL, 0x3f, v);
  475. spin_unlock_irq(&chip->reg_lock);
  476. return v != old_control;
  477. }
  478. static struct snd_kcontrol_new snd_cx88_capture_volume = {
  479. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  480. .name = "Capture Volume",
  481. .info = snd_cx88_capture_volume_info,
  482. .get = snd_cx88_capture_volume_get,
  483. .put = snd_cx88_capture_volume_put,
  484. };
  485. /****************************************************************************
  486. Basic Flow for Sound Devices
  487. ****************************************************************************/
  488. /*
  489. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  490. * Only boards with eeprom and byte 1 at eeprom=1 have it
  491. */
  492. static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
  493. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  494. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  495. {0, }
  496. };
  497. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  498. /*
  499. * Chip-specific destructor
  500. */
  501. static int snd_cx88_free(snd_cx88_card_t *chip)
  502. {
  503. if (chip->irq >= 0){
  504. synchronize_irq(chip->irq);
  505. free_irq(chip->irq, chip);
  506. }
  507. cx88_core_put(chip->core,chip->pci);
  508. pci_disable_device(chip->pci);
  509. return 0;
  510. }
  511. /*
  512. * Component Destructor
  513. */
  514. static void snd_cx88_dev_free(struct snd_card * card)
  515. {
  516. snd_cx88_card_t *chip = card->private_data;
  517. snd_cx88_free(chip);
  518. }
  519. /*
  520. * Alsa Constructor - Component probe
  521. */
  522. static int devno;
  523. static int __devinit snd_cx88_create(struct snd_card *card,
  524. struct pci_dev *pci,
  525. snd_cx88_card_t **rchip)
  526. {
  527. snd_cx88_card_t *chip;
  528. struct cx88_core *core;
  529. int err;
  530. *rchip = NULL;
  531. err = pci_enable_device(pci);
  532. if (err < 0)
  533. return err;
  534. pci_set_master(pci);
  535. chip = (snd_cx88_card_t *) card->private_data;
  536. core = cx88_core_get(pci);
  537. if (NULL == core) {
  538. err = -EINVAL;
  539. kfree (chip);
  540. return err;
  541. }
  542. if (!pci_dma_supported(pci,0xffffffff)) {
  543. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  544. err = -EIO;
  545. cx88_core_put(core,pci);
  546. return err;
  547. }
  548. /* pci init */
  549. chip->card = card;
  550. chip->pci = pci;
  551. chip->irq = -1;
  552. spin_lock_init(&chip->reg_lock);
  553. chip->core = core;
  554. /* get irq */
  555. err = request_irq(chip->pci->irq, cx8801_irq,
  556. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  557. if (err < 0) {
  558. dprintk(0, "%s: can't get IRQ %d\n",
  559. chip->core->name, chip->pci->irq);
  560. return err;
  561. }
  562. /* print pci info */
  563. pci_read_config_byte(pci, PCI_CLASS_REVISION, &chip->pci_rev);
  564. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &chip->pci_lat);
  565. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  566. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  567. pci_name(pci), chip->pci_rev, pci->irq,
  568. chip->pci_lat,(unsigned long long)pci_resource_start(pci,0));
  569. chip->irq = pci->irq;
  570. synchronize_irq(chip->irq);
  571. snd_card_set_dev(card, &pci->dev);
  572. *rchip = chip;
  573. return 0;
  574. }
  575. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  576. const struct pci_device_id *pci_id)
  577. {
  578. struct snd_card *card;
  579. snd_cx88_card_t *chip;
  580. int err;
  581. if (devno >= SNDRV_CARDS)
  582. return (-ENODEV);
  583. if (!enable[devno]) {
  584. ++devno;
  585. return (-ENOENT);
  586. }
  587. card = snd_card_new(index[devno], id[devno], THIS_MODULE, sizeof(snd_cx88_card_t));
  588. if (!card)
  589. return (-ENOMEM);
  590. card->private_free = snd_cx88_dev_free;
  591. err = snd_cx88_create(card, pci, &chip);
  592. if (err < 0)
  593. return (err);
  594. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  595. if (err < 0) {
  596. snd_card_free(card);
  597. return (err);
  598. }
  599. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_capture_volume, chip));
  600. if (err < 0) {
  601. snd_card_free(card);
  602. return (err);
  603. }
  604. strcpy (card->driver, "CX88x");
  605. sprintf(card->shortname, "Conexant CX%x", pci->device);
  606. sprintf(card->longname, "%s at %#llx",
  607. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  608. strcpy (card->mixername, "CX88");
  609. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  610. card->driver,devno);
  611. err = snd_card_register(card);
  612. if (err < 0) {
  613. snd_card_free(card);
  614. return (err);
  615. }
  616. snd_cx88_cards[devno] = card;
  617. pci_set_drvdata(pci,card);
  618. devno++;
  619. return 0;
  620. }
  621. /*
  622. * ALSA destructor
  623. */
  624. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  625. {
  626. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  627. snd_card_free((void *)card);
  628. pci_set_drvdata(pci, NULL);
  629. devno--;
  630. }
  631. /*
  632. * PCI driver definition
  633. */
  634. static struct pci_driver cx88_audio_pci_driver = {
  635. .name = "cx88_audio",
  636. .id_table = cx88_audio_pci_tbl,
  637. .probe = cx88_audio_initdev,
  638. .remove = cx88_audio_finidev,
  639. };
  640. /****************************************************************************
  641. LINUX MODULE INIT
  642. ****************************************************************************/
  643. /*
  644. * module init
  645. */
  646. static int cx88_audio_init(void)
  647. {
  648. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  649. (CX88_VERSION_CODE >> 16) & 0xff,
  650. (CX88_VERSION_CODE >> 8) & 0xff,
  651. CX88_VERSION_CODE & 0xff);
  652. #ifdef SNAPSHOT
  653. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  654. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  655. #endif
  656. return pci_register_driver(&cx88_audio_pci_driver);
  657. }
  658. /*
  659. * module remove
  660. */
  661. static void cx88_audio_fini(void)
  662. {
  663. pci_unregister_driver(&cx88_audio_pci_driver);
  664. }
  665. module_init(cx88_audio_init);
  666. module_exit(cx88_audio_fini);
  667. /* ----------------------------------------------------------- */
  668. /*
  669. * Local variables:
  670. * c-basic-offset: 8
  671. * End:
  672. */