Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Support for VIA 82Cxxx Audio Codecs | |
3 | * Copyright 1999,2000 Jeff Garzik | |
4 | * | |
5 | * Updated to support the VIA 8233/8235 audio subsystem | |
6 | * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc | |
7 | * | |
8 | * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2. | |
9 | * See the "COPYING" file distributed with this software for more info. | |
10 | * NO WARRANTY | |
11 | * | |
12 | * For a list of known bugs (errata) and documentation, | |
13 | * see via-audio.pdf in Documentation/DocBook. | |
14 | * If this documentation does not exist, run "make pdfdocs". | |
15 | */ | |
16 | ||
17 | ||
18 | #define VIA_VERSION "1.9.1-ac4-2.5" | |
19 | ||
20 | ||
21 | #include <linux/config.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/fs.h> | |
25 | #include <linux/mm.h> | |
26 | #include <linux/pci.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/interrupt.h> | |
29 | #include <linux/proc_fs.h> | |
30 | #include <linux/spinlock.h> | |
31 | #include <linux/sound.h> | |
32 | #include <linux/poll.h> | |
33 | #include <linux/soundcard.h> | |
34 | #include <linux/ac97_codec.h> | |
35 | #include <linux/smp_lock.h> | |
36 | #include <linux/ioport.h> | |
37 | #include <linux/delay.h> | |
3e90928d | 38 | #include <linux/dma-mapping.h> |
1da177e4 LT |
39 | #include <asm/io.h> |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/semaphore.h> | |
42 | #include "sound_config.h" | |
43 | #include "dev_table.h" | |
44 | #include "mpu401.h" | |
45 | ||
46 | ||
47 | #undef VIA_DEBUG /* define to enable debugging output and checks */ | |
48 | #ifdef VIA_DEBUG | |
49 | /* note: prints function name for you */ | |
50 | #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args) | |
51 | #else | |
52 | #define DPRINTK(fmt, args...) | |
53 | #endif | |
54 | ||
55 | #undef VIA_NDEBUG /* define to disable lightweight runtime checks */ | |
56 | #ifdef VIA_NDEBUG | |
57 | #define assert(expr) | |
58 | #else | |
59 | #define assert(expr) \ | |
60 | if(!(expr)) { \ | |
61 | printk( "Assertion failed! %s,%s,%s,line=%d\n", \ | |
62 | #expr,__FILE__,__FUNCTION__,__LINE__); \ | |
63 | } | |
64 | #endif | |
65 | ||
66 | #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */ | |
67 | ||
68 | #define MAX_CARDS 1 | |
69 | ||
70 | #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION | |
71 | #define VIA_MODULE_NAME "via82cxxx" | |
72 | #define PFX VIA_MODULE_NAME ": " | |
73 | ||
74 | #define VIA_COUNTER_LIMIT 100000 | |
75 | ||
76 | /* size of DMA buffers */ | |
77 | #define VIA_MAX_BUFFER_DMA_PAGES 32 | |
78 | ||
79 | /* buffering default values in ms */ | |
80 | #define VIA_DEFAULT_FRAG_TIME 20 | |
81 | #define VIA_DEFAULT_BUFFER_TIME 500 | |
82 | ||
83 | /* the hardware has a 256 fragment limit */ | |
84 | #define VIA_MIN_FRAG_NUMBER 2 | |
85 | #define VIA_MAX_FRAG_NUMBER 128 | |
86 | ||
87 | #define VIA_MAX_FRAG_SIZE PAGE_SIZE | |
88 | #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER) | |
89 | ||
90 | ||
91 | /* 82C686 function 5 (audio codec) PCI configuration registers */ | |
92 | #define VIA_ACLINK_STATUS 0x40 | |
93 | #define VIA_ACLINK_CTRL 0x41 | |
94 | #define VIA_FUNC_ENABLE 0x42 | |
95 | #define VIA_PNP_CONTROL 0x43 | |
96 | #define VIA_FM_NMI_CTRL 0x48 | |
97 | ||
98 | /* | |
99 | * controller base 0 (scatter-gather) registers | |
100 | * | |
101 | * NOTE: Via datasheet lists first channel as "read" | |
102 | * channel and second channel as "write" channel. | |
103 | * I changed the naming of the constants to be more | |
104 | * clear than I felt the datasheet to be. | |
105 | */ | |
106 | ||
107 | #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */ | |
108 | #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00 | |
109 | #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01 | |
110 | #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02 | |
111 | ||
112 | #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */ | |
113 | #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10 | |
114 | #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11 | |
115 | #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12 | |
116 | ||
117 | /* offsets from base */ | |
118 | #define VIA_PCM_STATUS 0x00 | |
119 | #define VIA_PCM_CONTROL 0x01 | |
120 | #define VIA_PCM_TYPE 0x02 | |
121 | #define VIA_PCM_LEFTVOL 0x02 | |
122 | #define VIA_PCM_RIGHTVOL 0x03 | |
123 | #define VIA_PCM_TABLE_ADDR 0x04 | |
124 | #define VIA_PCM_STOPRATE 0x08 /* 8233+ */ | |
125 | #define VIA_PCM_BLOCK_COUNT 0x0C | |
126 | ||
127 | /* XXX unused DMA channel for FM PCM data */ | |
128 | #define VIA_BASE0_FM_OUT_CHAN 0x20 | |
129 | #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20 | |
130 | #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21 | |
131 | #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22 | |
132 | ||
133 | /* Six channel audio output on 8233 */ | |
134 | #define VIA_BASE0_MULTI_OUT_CHAN 0x40 | |
135 | #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40 | |
136 | #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41 | |
137 | #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42 | |
138 | ||
139 | #define VIA_BASE0_AC97_CTRL 0x80 | |
140 | #define VIA_BASE0_SGD_STATUS_SHADOW 0x84 | |
141 | #define VIA_BASE0_GPI_INT_ENABLE 0x8C | |
142 | #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8)) | |
143 | #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9)) | |
144 | #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10)) | |
145 | #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM) | |
146 | ||
147 | /* Newer VIA we need to monitor the low 3 bits of each channel. This | |
148 | mask covers the channels we don't yet use as well | |
149 | */ | |
150 | ||
151 | #define VIA_NEW_INTR_MASK 0x77077777UL | |
152 | ||
153 | /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */ | |
154 | #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */ | |
155 | #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */ | |
156 | #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */ | |
157 | #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */ | |
158 | #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */ | |
159 | #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */ | |
160 | #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */ | |
161 | #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */ | |
162 | #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */ | |
163 | #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT) | |
164 | #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \ | |
165 | VIA_IRQ_ON_FLAG | \ | |
166 | VIA_IRQ_ON_EOL) | |
167 | #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT) | |
168 | ||
169 | /* PCI configuration register bits and masks */ | |
170 | #define VIA_CR40_AC97_READY 0x01 | |
171 | #define VIA_CR40_AC97_LOW_POWER 0x02 | |
172 | #define VIA_CR40_SECONDARY_READY 0x04 | |
173 | ||
174 | #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */ | |
175 | #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */ | |
176 | #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */ | |
177 | #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */ | |
178 | #define VIA_CR41_VRA 0x08 /* enable variable sample rate */ | |
179 | #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */ | |
180 | #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */ | |
181 | #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */ | |
182 | #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \ | |
183 | VIA_CR41_AC97_WAKEUP | \ | |
184 | VIA_CR41_AC97_SDO) | |
185 | #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \ | |
186 | VIA_CR41_AC97_RESET | \ | |
187 | VIA_CR41_VRA | \ | |
188 | VIA_CR41_PCM_ENABLE) | |
189 | ||
190 | #define VIA_CR42_SB_ENABLE 0x01 | |
191 | #define VIA_CR42_MIDI_ENABLE 0x02 | |
192 | #define VIA_CR42_FM_ENABLE 0x04 | |
193 | #define VIA_CR42_GAME_ENABLE 0x08 | |
194 | #define VIA_CR42_MIDI_IRQMASK 0x40 | |
195 | #define VIA_CR42_MIDI_PNP 0x80 | |
196 | ||
197 | #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6) | |
198 | #define VIA_CR44_AC_LINK_ACCESS (1 << 7) | |
199 | ||
200 | #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2) | |
201 | ||
202 | /* controller base 0 register bitmasks */ | |
203 | #define VIA_INT_DISABLE_MASK (~(0x01|0x02)) | |
204 | #define VIA_SGD_STOPPED (1 << 2) | |
205 | #define VIA_SGD_PAUSED (1 << 6) | |
206 | #define VIA_SGD_ACTIVE (1 << 7) | |
207 | #define VIA_SGD_TERMINATE (1 << 6) | |
208 | #define VIA_SGD_FLAG (1 << 0) | |
209 | #define VIA_SGD_EOL (1 << 1) | |
210 | #define VIA_SGD_START (1 << 7) | |
211 | ||
212 | #define VIA_CR80_FIRST_CODEC 0 | |
213 | #define VIA_CR80_SECOND_CODEC (1 << 30) | |
214 | #define VIA_CR80_FIRST_CODEC_VALID (1 << 25) | |
215 | #define VIA_CR80_VALID (1 << 25) | |
216 | #define VIA_CR80_SECOND_CODEC_VALID (1 << 27) | |
217 | #define VIA_CR80_BUSY (1 << 24) | |
218 | #define VIA_CR83_BUSY (1) | |
219 | #define VIA_CR83_FIRST_CODEC_VALID (1 << 1) | |
220 | #define VIA_CR80_READ (1 << 23) | |
221 | #define VIA_CR80_WRITE_MODE 0 | |
222 | #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16) | |
223 | ||
224 | /* capabilities we announce */ | |
225 | #ifdef VIA_SUPPORT_MMAP | |
226 | #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \ | |
227 | DSP_CAP_TRIGGER | DSP_CAP_REALTIME) | |
228 | #else | |
229 | #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \ | |
230 | DSP_CAP_TRIGGER | DSP_CAP_REALTIME) | |
231 | #endif | |
232 | ||
233 | /* scatter-gather DMA table entry, exactly as passed to hardware */ | |
234 | struct via_sgd_table { | |
235 | u32 addr; | |
236 | u32 count; /* includes additional VIA_xxx bits also */ | |
237 | }; | |
238 | ||
239 | #define VIA_EOL (1 << 31) | |
240 | #define VIA_FLAG (1 << 30) | |
241 | #define VIA_STOP (1 << 29) | |
242 | ||
243 | ||
244 | enum via_channel_states { | |
245 | sgd_stopped = 0, | |
246 | sgd_in_progress = 1, | |
247 | }; | |
248 | ||
249 | ||
250 | struct via_buffer_pgtbl { | |
251 | dma_addr_t handle; | |
252 | void *cpuaddr; | |
253 | }; | |
254 | ||
255 | ||
256 | struct via_channel { | |
257 | atomic_t n_frags; | |
258 | atomic_t hw_ptr; | |
259 | wait_queue_head_t wait; | |
260 | ||
261 | unsigned int sw_ptr; | |
262 | unsigned int slop_len; | |
263 | unsigned int n_irqs; | |
264 | int bytes; | |
265 | ||
266 | unsigned is_active : 1; | |
267 | unsigned is_record : 1; | |
268 | unsigned is_mapped : 1; | |
269 | unsigned is_enabled : 1; | |
270 | unsigned is_multi: 1; /* 8233 6 channel */ | |
271 | u8 pcm_fmt; /* VIA_PCM_FMT_xxx */ | |
272 | u8 channels; /* Channel count */ | |
273 | ||
274 | unsigned rate; /* sample rate */ | |
275 | unsigned int frag_size; | |
276 | unsigned int frag_number; | |
277 | ||
278 | unsigned char intmask; | |
279 | ||
280 | volatile struct via_sgd_table *sgtable; | |
281 | dma_addr_t sgt_handle; | |
282 | ||
283 | unsigned int page_number; | |
284 | struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES]; | |
285 | ||
286 | long iobase; | |
287 | ||
288 | const char *name; | |
289 | }; | |
290 | ||
291 | ||
292 | /* data stored for each chip */ | |
293 | struct via_info { | |
294 | struct pci_dev *pdev; | |
295 | long baseaddr; | |
296 | ||
297 | struct ac97_codec *ac97; | |
298 | spinlock_t ac97_lock; | |
299 | spinlock_t lock; | |
300 | int card_num; /* unique card number, from 0 */ | |
301 | ||
302 | int dev_dsp; /* /dev/dsp index from register_sound_dsp() */ | |
303 | ||
304 | unsigned rev_h : 1; | |
305 | unsigned legacy: 1; /* Has legacy ports */ | |
306 | unsigned intmask: 1; /* Needs int bits */ | |
307 | unsigned sixchannel: 1; /* 8233/35 with 6 channel support */ | |
308 | unsigned volume: 1; | |
309 | ||
310 | int locked_rate : 1; | |
311 | ||
312 | int mixer_vol; /* 8233/35 volume - not yet implemented */ | |
313 | ||
314 | struct semaphore syscall_sem; | |
315 | struct semaphore open_sem; | |
316 | ||
317 | /* The 8233/8235 have 4 DX audio channels, two record and | |
318 | one six channel out. We bind ch_in to DX 1, ch_out to multichannel | |
319 | and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the | |
320 | moment */ | |
321 | ||
322 | struct via_channel ch_in; | |
323 | struct via_channel ch_out; | |
324 | struct via_channel ch_fm; | |
325 | ||
326 | #ifdef CONFIG_MIDI_VIA82CXXX | |
327 | void *midi_devc; | |
328 | struct address_info midi_info; | |
329 | #endif | |
330 | }; | |
331 | ||
332 | ||
333 | /* number of cards, used for assigning unique numbers to cards */ | |
334 | static unsigned via_num_cards; | |
335 | ||
336 | ||
337 | ||
338 | /**************************************************************** | |
339 | * | |
340 | * prototypes | |
341 | * | |
342 | * | |
343 | */ | |
344 | ||
345 | static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id); | |
346 | static void __devexit via_remove_one (struct pci_dev *pdev); | |
347 | ||
348 | static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos); | |
349 | static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos); | |
350 | static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait); | |
351 | static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg); | |
352 | static int via_dsp_open (struct inode *inode, struct file *file); | |
353 | static int via_dsp_release(struct inode *inode, struct file *file); | |
354 | static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma); | |
355 | ||
356 | static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg); | |
357 | static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value); | |
358 | static u8 via_ac97_wait_idle (struct via_info *card); | |
359 | ||
360 | static void via_chan_free (struct via_info *card, struct via_channel *chan); | |
361 | static void via_chan_clear (struct via_info *card, struct via_channel *chan); | |
362 | static void via_chan_pcm_fmt (struct via_channel *chan, int reset); | |
363 | static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan); | |
364 | ||
365 | ||
366 | /**************************************************************** | |
367 | * | |
368 | * Various data the driver needs | |
369 | * | |
370 | * | |
371 | */ | |
372 | ||
373 | ||
374 | static struct pci_device_id via_pci_tbl[] = { | |
375 | { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, | |
376 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, | |
377 | { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, | |
378 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1}, | |
379 | { 0, } | |
380 | }; | |
381 | MODULE_DEVICE_TABLE(pci,via_pci_tbl); | |
382 | ||
383 | ||
384 | static struct pci_driver via_driver = { | |
385 | .name = VIA_MODULE_NAME, | |
386 | .id_table = via_pci_tbl, | |
387 | .probe = via_init_one, | |
388 | .remove = __devexit_p(via_remove_one), | |
389 | }; | |
390 | ||
391 | ||
392 | /**************************************************************** | |
393 | * | |
394 | * Low-level base 0 register read/write helpers | |
395 | * | |
396 | * | |
397 | */ | |
398 | ||
399 | /** | |
400 | * via_chan_stop - Terminate DMA on specified PCM channel | |
401 | * @iobase: PCI base address for SGD channel registers | |
402 | * | |
403 | * Terminate scatter-gather DMA operation for given | |
404 | * channel (derived from @iobase), if DMA is active. | |
405 | * | |
406 | * Note that @iobase is not the PCI base address, | |
407 | * but the PCI base address plus an offset to | |
408 | * one of three PCM channels supported by the chip. | |
409 | * | |
410 | */ | |
411 | ||
412 | static inline void via_chan_stop (long iobase) | |
413 | { | |
414 | if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE) | |
415 | outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL); | |
416 | } | |
417 | ||
418 | ||
419 | /** | |
420 | * via_chan_status_clear - Clear status flags on specified DMA channel | |
421 | * @iobase: PCI base address for SGD channel registers | |
422 | * | |
423 | * Clear any pending status flags for the given | |
424 | * DMA channel (derived from @iobase), if any | |
425 | * flags are asserted. | |
426 | * | |
427 | * Note that @iobase is not the PCI base address, | |
428 | * but the PCI base address plus an offset to | |
429 | * one of three PCM channels supported by the chip. | |
430 | * | |
431 | */ | |
432 | ||
433 | static inline void via_chan_status_clear (long iobase) | |
434 | { | |
435 | u8 tmp = inb (iobase + VIA_PCM_STATUS); | |
436 | ||
437 | if (tmp != 0) | |
438 | outb (tmp, iobase + VIA_PCM_STATUS); | |
439 | } | |
440 | ||
441 | ||
442 | /** | |
443 | * sg_begin - Begin recording or playback on a PCM channel | |
444 | * @chan: Channel for which DMA operation shall begin | |
445 | * | |
446 | * Start scatter-gather DMA for the given channel. | |
447 | * | |
448 | */ | |
449 | ||
450 | static inline void sg_begin (struct via_channel *chan) | |
451 | { | |
452 | DPRINTK("Start with intmask %d\n", chan->intmask); | |
453 | DPRINTK("About to start from %d to %d\n", | |
454 | inl(chan->iobase + VIA_PCM_BLOCK_COUNT), | |
455 | inb(chan->iobase + VIA_PCM_STOPRATE + 3)); | |
456 | outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL); | |
457 | DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS)); | |
458 | DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL)); | |
459 | } | |
460 | ||
461 | ||
462 | static int sg_active (long iobase) | |
463 | { | |
464 | u8 tmp = inb (iobase + VIA_PCM_STATUS); | |
465 | if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) { | |
466 | printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n"); | |
467 | return 0; | |
468 | } | |
469 | if (tmp & VIA_SGD_ACTIVE) | |
470 | return 1; | |
471 | return 0; | |
472 | } | |
473 | ||
474 | static int via_sg_offset(struct via_channel *chan) | |
475 | { | |
476 | return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF; | |
477 | } | |
478 | ||
479 | /**************************************************************** | |
480 | * | |
481 | * Miscellaneous debris | |
482 | * | |
483 | * | |
484 | */ | |
485 | ||
486 | ||
487 | /** | |
488 | * via_syscall_down - down the card-specific syscell semaphore | |
489 | * @card: Private info for specified board | |
490 | * @nonblock: boolean, non-zero if O_NONBLOCK is set | |
491 | * | |
492 | * Encapsulates standard method of acquiring the syscall sem. | |
493 | * | |
494 | * Returns negative errno on error, or zero for success. | |
495 | */ | |
496 | ||
497 | static inline int via_syscall_down (struct via_info *card, int nonblock) | |
498 | { | |
499 | /* Thomas Sailer: | |
500 | * EAGAIN is supposed to be used if IO is pending, | |
501 | * not if there is contention on some internal | |
502 | * synchronization primitive which should be | |
503 | * held only for a short time anyway | |
504 | */ | |
505 | nonblock = 0; | |
506 | ||
507 | if (nonblock) { | |
508 | if (down_trylock (&card->syscall_sem)) | |
509 | return -EAGAIN; | |
510 | } else { | |
511 | if (down_interruptible (&card->syscall_sem)) | |
512 | return -ERESTARTSYS; | |
513 | } | |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
518 | ||
519 | /** | |
520 | * via_stop_everything - Stop all audio operations | |
521 | * @card: Private info for specified board | |
522 | * | |
523 | * Stops all DMA operations and interrupts, and clear | |
524 | * any pending status bits resulting from those operations. | |
525 | */ | |
526 | ||
527 | static void via_stop_everything (struct via_info *card) | |
528 | { | |
529 | u8 tmp, new_tmp; | |
530 | ||
531 | DPRINTK ("ENTER\n"); | |
532 | ||
533 | assert (card != NULL); | |
534 | ||
535 | /* | |
536 | * terminate any existing operations on audio read/write channels | |
537 | */ | |
538 | via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN); | |
539 | via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN); | |
540 | via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN); | |
541 | if(card->sixchannel) | |
542 | via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN); | |
543 | ||
544 | /* | |
545 | * clear any existing stops / flags (sanity check mainly) | |
546 | */ | |
547 | via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN); | |
548 | via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN); | |
549 | via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN); | |
550 | if(card->sixchannel) | |
551 | via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN); | |
552 | ||
553 | /* | |
554 | * clear any enabled interrupt bits | |
555 | */ | |
556 | tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE); | |
557 | new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL); | |
558 | if (tmp != new_tmp) | |
559 | outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE); | |
560 | ||
561 | tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE); | |
562 | new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL); | |
563 | if (tmp != new_tmp) | |
564 | outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE); | |
565 | ||
566 | tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE); | |
567 | new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL); | |
568 | if (tmp != new_tmp) | |
569 | outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE); | |
570 | ||
571 | if(card->sixchannel) | |
572 | { | |
573 | tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE); | |
574 | new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL); | |
575 | if (tmp != new_tmp) | |
576 | outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE); | |
577 | } | |
578 | ||
579 | udelay(10); | |
580 | ||
581 | /* | |
582 | * clear any existing flags | |
583 | */ | |
584 | via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN); | |
585 | via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN); | |
586 | via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN); | |
587 | ||
588 | DPRINTK ("EXIT\n"); | |
589 | } | |
590 | ||
591 | ||
592 | /** | |
593 | * via_set_rate - Set PCM rate for given channel | |
594 | * @ac97: Pointer to generic codec info struct | |
595 | * @chan: Private info for specified channel | |
596 | * @rate: Desired PCM sample rate, in Khz | |
597 | * | |
598 | * Sets the PCM sample rate for a channel. | |
599 | * | |
600 | * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz, | |
601 | * due to hardware constraints. | |
602 | */ | |
603 | ||
604 | static int via_set_rate (struct ac97_codec *ac97, | |
605 | struct via_channel *chan, unsigned rate) | |
606 | { | |
607 | struct via_info *card = ac97->private_data; | |
608 | int rate_reg; | |
609 | u32 dacp; | |
610 | u32 mast_vol, phone_vol, mono_vol, pcm_vol; | |
611 | u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */ | |
612 | ||
613 | DPRINTK ("ENTER, rate = %d\n", rate); | |
614 | ||
615 | if (chan->rate == rate) | |
616 | goto out; | |
617 | if (card->locked_rate) { | |
618 | chan->rate = 48000; | |
619 | goto out; | |
620 | } | |
621 | ||
622 | if (rate > 48000) rate = 48000; | |
623 | if (rate < 4000) rate = 4000; | |
624 | ||
625 | rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE : | |
626 | AC97_PCM_FRONT_DAC_RATE; | |
627 | ||
628 | /* Save current state */ | |
629 | dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL); | |
630 | mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO); | |
631 | mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO); | |
632 | phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL); | |
633 | pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL); | |
634 | /* Mute - largely reduces popping */ | |
635 | via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol); | |
636 | via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol); | |
637 | via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol); | |
638 | via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol); | |
639 | /* Power down the DAC */ | |
640 | via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200); | |
641 | ||
642 | /* Set new rate */ | |
643 | via_ac97_write_reg (ac97, rate_reg, rate); | |
644 | ||
645 | /* Power DAC back up */ | |
646 | via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp); | |
647 | udelay (200); /* reduces popping */ | |
648 | ||
649 | /* Restore volumes */ | |
650 | via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol); | |
651 | via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol); | |
652 | via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol); | |
653 | via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol); | |
654 | ||
655 | /* the hardware might return a value different than what we | |
656 | * passed to it, so read the rate value back from hardware | |
657 | * to see what we came up with | |
658 | */ | |
659 | chan->rate = via_ac97_read_reg (ac97, rate_reg); | |
660 | ||
661 | if (chan->rate == 0) { | |
662 | card->locked_rate = 1; | |
663 | chan->rate = 48000; | |
664 | printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n"); | |
665 | } | |
666 | ||
667 | out: | |
668 | DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate); | |
669 | return chan->rate; | |
670 | } | |
671 | ||
672 | ||
673 | /**************************************************************** | |
674 | * | |
675 | * Channel-specific operations | |
676 | * | |
677 | * | |
678 | */ | |
679 | ||
680 | ||
681 | /** | |
682 | * via_chan_init_defaults - Initialize a struct via_channel | |
683 | * @card: Private audio chip info | |
684 | * @chan: Channel to be initialized | |
685 | * | |
686 | * Zero @chan, and then set all static defaults for the structure. | |
687 | */ | |
688 | ||
689 | static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan) | |
690 | { | |
691 | memset (chan, 0, sizeof (*chan)); | |
692 | ||
693 | if(card->intmask) | |
694 | chan->intmask = 0x23; /* Turn on the IRQ bits */ | |
695 | ||
696 | if (chan == &card->ch_out) { | |
697 | chan->name = "PCM-OUT"; | |
698 | if(card->sixchannel) | |
699 | { | |
700 | chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN; | |
701 | chan->is_multi = 1; | |
702 | DPRINTK("Using multichannel for pcm out\n"); | |
703 | } | |
704 | else | |
705 | chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN; | |
706 | } else if (chan == &card->ch_in) { | |
707 | chan->name = "PCM-IN"; | |
708 | chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN; | |
709 | chan->is_record = 1; | |
710 | } else if (chan == &card->ch_fm) { | |
711 | chan->name = "PCM-OUT-FM"; | |
712 | chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN; | |
713 | } else { | |
714 | BUG(); | |
715 | } | |
716 | ||
717 | init_waitqueue_head (&chan->wait); | |
718 | ||
719 | chan->pcm_fmt = VIA_PCM_FMT_MASK; | |
720 | chan->is_enabled = 1; | |
721 | ||
722 | chan->frag_number = 0; | |
723 | chan->frag_size = 0; | |
724 | atomic_set(&chan->n_frags, 0); | |
725 | atomic_set (&chan->hw_ptr, 0); | |
726 | } | |
727 | ||
728 | /** | |
729 | * via_chan_init - Initialize PCM channel | |
730 | * @card: Private audio chip info | |
731 | * @chan: Channel to be initialized | |
732 | * | |
733 | * Performs some of the preparations necessary to begin | |
734 | * using a PCM channel. | |
735 | * | |
736 | * Currently the preparations consist of | |
737 | * setting the PCM channel to a known state. | |
738 | */ | |
739 | ||
740 | ||
741 | static void via_chan_init (struct via_info *card, struct via_channel *chan) | |
742 | { | |
743 | ||
744 | DPRINTK ("ENTER\n"); | |
745 | ||
746 | /* bzero channel structure, and init members to defaults */ | |
747 | via_chan_init_defaults (card, chan); | |
748 | ||
749 | /* stop any existing channel output */ | |
750 | via_chan_clear (card, chan); | |
751 | via_chan_status_clear (chan->iobase); | |
752 | via_chan_pcm_fmt (chan, 1); | |
753 | ||
754 | DPRINTK ("EXIT\n"); | |
755 | } | |
756 | ||
757 | /** | |
758 | * via_chan_buffer_init - Initialize PCM channel buffer | |
759 | * @card: Private audio chip info | |
760 | * @chan: Channel to be initialized | |
761 | * | |
762 | * Performs some of the preparations necessary to begin | |
763 | * using a PCM channel. | |
764 | * | |
765 | * Currently the preparations include allocating the | |
766 | * scatter-gather DMA table and buffers, | |
767 | * and passing the | |
768 | * address of the DMA table to the hardware. | |
769 | * | |
770 | * Note that special care is taken when passing the | |
771 | * DMA table address to hardware, because it was found | |
772 | * during driver development that the hardware did not | |
773 | * always "take" the address. | |
774 | */ | |
775 | ||
776 | static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan) | |
777 | { | |
778 | int page, offset; | |
779 | int i; | |
780 | ||
781 | DPRINTK ("ENTER\n"); | |
782 | ||
783 | ||
784 | chan->intmask = 0; | |
785 | if(card->intmask) | |
786 | chan->intmask = 0x23; /* Turn on the IRQ bits */ | |
787 | ||
788 | if (chan->sgtable != NULL) { | |
789 | DPRINTK ("EXIT\n"); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /* alloc DMA-able memory for scatter-gather table */ | |
794 | chan->sgtable = pci_alloc_consistent (card->pdev, | |
795 | (sizeof (struct via_sgd_table) * chan->frag_number), | |
796 | &chan->sgt_handle); | |
797 | if (!chan->sgtable) { | |
798 | printk (KERN_ERR PFX "DMA table alloc fail, aborting\n"); | |
799 | DPRINTK ("EXIT\n"); | |
800 | return -ENOMEM; | |
801 | } | |
802 | ||
803 | memset ((void*)chan->sgtable, 0, | |
804 | (sizeof (struct via_sgd_table) * chan->frag_number)); | |
805 | ||
806 | /* alloc DMA-able memory for scatter-gather buffers */ | |
807 | ||
808 | chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE + | |
809 | (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0); | |
810 | ||
811 | for (i = 0; i < chan->page_number; i++) { | |
812 | chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE, | |
813 | &chan->pgtbl[i].handle); | |
814 | ||
815 | if (!chan->pgtbl[i].cpuaddr) { | |
816 | chan->page_number = i; | |
817 | goto err_out_nomem; | |
818 | } | |
819 | ||
820 | #ifndef VIA_NDEBUG | |
821 | memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size); | |
822 | #endif | |
823 | ||
824 | #if 1 | |
825 | DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n", | |
826 | i, (long)chan->pgtbl[i].handle, | |
827 | virt_to_phys(chan->pgtbl[i].cpuaddr), | |
828 | chan->pgtbl[i].cpuaddr); | |
829 | #endif | |
830 | } | |
831 | ||
832 | for (i = 0; i < chan->frag_number; i++) { | |
833 | ||
834 | page = i / (PAGE_SIZE / chan->frag_size); | |
835 | offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size; | |
836 | ||
837 | chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG); | |
838 | chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset); | |
839 | ||
840 | #if 1 | |
841 | DPRINTK ("dmabuf #%d (32(h)=%lx)\n", | |
842 | i, | |
843 | (long)chan->sgtable[i].addr); | |
844 | #endif | |
845 | } | |
846 | ||
847 | /* overwrite the last buffer information */ | |
848 | chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL); | |
849 | ||
850 | /* set location of DMA-able scatter-gather info table */ | |
851 | DPRINTK ("outl (0x%X, 0x%04lX)\n", | |
852 | chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR); | |
853 | ||
854 | via_ac97_wait_idle (card); | |
855 | outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR); | |
856 | udelay (20); | |
857 | via_ac97_wait_idle (card); | |
858 | /* load no rate adaption, stereo 16bit, set up ring slots */ | |
859 | if(card->sixchannel) | |
860 | { | |
861 | if(!chan->is_multi) | |
862 | { | |
863 | outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE); | |
864 | udelay (20); | |
865 | via_ac97_wait_idle (card); | |
866 | } | |
867 | } | |
868 | ||
869 | DPRINTK ("inl (0x%lX) = %x\n", | |
870 | chan->iobase + VIA_PCM_TABLE_ADDR, | |
871 | inl(chan->iobase + VIA_PCM_TABLE_ADDR)); | |
872 | ||
873 | DPRINTK ("EXIT\n"); | |
874 | return 0; | |
875 | ||
876 | err_out_nomem: | |
877 | printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n"); | |
878 | via_chan_buffer_free (card, chan); | |
879 | DPRINTK ("EXIT\n"); | |
880 | return -ENOMEM; | |
881 | } | |
882 | ||
883 | ||
884 | /** | |
885 | * via_chan_free - Release a PCM channel | |
886 | * @card: Private audio chip info | |
887 | * @chan: Channel to be released | |
888 | * | |
889 | * Performs all the functions necessary to clean up | |
890 | * an initialized channel. | |
891 | * | |
892 | * Currently these functions include disabled any | |
893 | * active DMA operations, setting the PCM channel | |
894 | * back to a known state, and releasing any allocated | |
895 | * sound buffers. | |
896 | */ | |
897 | ||
898 | static void via_chan_free (struct via_info *card, struct via_channel *chan) | |
899 | { | |
900 | DPRINTK ("ENTER\n"); | |
901 | ||
902 | spin_lock_irq (&card->lock); | |
903 | ||
904 | /* stop any existing channel output */ | |
905 | via_chan_status_clear (chan->iobase); | |
906 | via_chan_stop (chan->iobase); | |
907 | via_chan_status_clear (chan->iobase); | |
908 | ||
909 | spin_unlock_irq (&card->lock); | |
910 | ||
911 | synchronize_irq(card->pdev->irq); | |
912 | ||
913 | DPRINTK ("EXIT\n"); | |
914 | } | |
915 | ||
916 | static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan) | |
917 | { | |
918 | int i; | |
919 | ||
920 | DPRINTK ("ENTER\n"); | |
921 | ||
922 | /* zero location of DMA-able scatter-gather info table */ | |
923 | via_ac97_wait_idle(card); | |
924 | outl (0, chan->iobase + VIA_PCM_TABLE_ADDR); | |
925 | ||
926 | for (i = 0; i < chan->page_number; i++) | |
927 | if (chan->pgtbl[i].cpuaddr) { | |
928 | pci_free_consistent (card->pdev, PAGE_SIZE, | |
929 | chan->pgtbl[i].cpuaddr, | |
930 | chan->pgtbl[i].handle); | |
931 | chan->pgtbl[i].cpuaddr = NULL; | |
932 | chan->pgtbl[i].handle = 0; | |
933 | } | |
934 | ||
935 | chan->page_number = 0; | |
936 | ||
937 | if (chan->sgtable) { | |
938 | pci_free_consistent (card->pdev, | |
939 | (sizeof (struct via_sgd_table) * chan->frag_number), | |
940 | (void*)chan->sgtable, chan->sgt_handle); | |
941 | chan->sgtable = NULL; | |
942 | } | |
943 | ||
944 | DPRINTK ("EXIT\n"); | |
945 | } | |
946 | ||
947 | ||
948 | /** | |
949 | * via_chan_pcm_fmt - Update PCM channel settings | |
950 | * @chan: Channel to be updated | |
951 | * @reset: Boolean. If non-zero, channel will be reset | |
952 | * to 8-bit mono mode. | |
953 | * | |
954 | * Stores the settings of the current PCM format, | |
955 | * 8-bit or 16-bit, and mono/stereo, into the | |
956 | * hardware settings for the specified channel. | |
957 | * If @reset is non-zero, the channel is reset | |
958 | * to 8-bit mono mode. Otherwise, the channel | |
959 | * is set to the values stored in the channel | |
960 | * information struct @chan. | |
961 | */ | |
962 | ||
963 | static void via_chan_pcm_fmt (struct via_channel *chan, int reset) | |
964 | { | |
965 | DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n", | |
966 | chan->pcm_fmt, reset ? "yes" : "no"); | |
967 | ||
968 | assert (chan != NULL); | |
969 | ||
970 | if (reset) | |
971 | { | |
972 | /* reset to 8-bit mono mode */ | |
973 | chan->pcm_fmt = 0; | |
974 | chan->channels = 1; | |
975 | } | |
976 | ||
977 | /* enable interrupts on FLAG and EOL */ | |
978 | chan->pcm_fmt |= VIA_CHAN_TYPE_MASK; | |
979 | ||
980 | /* if we are recording, enable recording fifo bit */ | |
981 | if (chan->is_record) | |
982 | chan->pcm_fmt |= VIA_PCM_REC_FIFO; | |
983 | /* set interrupt select bits where applicable (PCM in & out channels) */ | |
984 | if (!chan->is_record) | |
985 | chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT; | |
986 | ||
987 | DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi); | |
988 | ||
989 | if(chan->intmask) | |
990 | { | |
991 | u32 m; | |
992 | ||
993 | /* | |
994 | * Channel 0x4 is up to 6 x 16bit and has to be | |
995 | * programmed differently | |
996 | */ | |
997 | ||
998 | if(chan->is_multi) | |
999 | { | |
1000 | u8 c = 0; | |
1001 | ||
1002 | /* | |
1003 | * Load the type bit for num channels | |
1004 | * and 8/16bit | |
1005 | */ | |
1006 | ||
1007 | if(chan->pcm_fmt & VIA_PCM_FMT_16BIT) | |
1008 | c = 1 << 7; | |
1009 | if(chan->pcm_fmt & VIA_PCM_FMT_STEREO) | |
1010 | c |= (2<<4); | |
1011 | else | |
1012 | c |= (1<<4); | |
1013 | ||
1014 | outb(c, chan->iobase + VIA_PCM_TYPE); | |
1015 | ||
1016 | /* | |
1017 | * Set the channel steering | |
1018 | * Mono | |
1019 | * Channel 0 to slot 3 | |
1020 | * Channel 0 to slot 4 | |
1021 | * Stereo | |
1022 | * Channel 0 to slot 3 | |
1023 | * Channel 1 to slot 4 | |
1024 | */ | |
1025 | ||
1026 | switch(chan->channels) | |
1027 | { | |
1028 | case 1: | |
1029 | outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE); | |
1030 | break; | |
1031 | case 2: | |
1032 | outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE); | |
1033 | break; | |
1034 | case 4: | |
1035 | outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE); | |
1036 | break; | |
1037 | case 6: | |
1038 | outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE); | |
1039 | break; | |
1040 | } | |
1041 | } | |
1042 | else | |
1043 | { | |
1044 | /* | |
1045 | * New style, turn off channel volume | |
1046 | * control, set bits in the right register | |
1047 | */ | |
1048 | outb(0x0, chan->iobase + VIA_PCM_LEFTVOL); | |
1049 | outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL); | |
1050 | ||
1051 | m = inl(chan->iobase + VIA_PCM_STOPRATE); | |
1052 | m &= ~(3<<20); | |
1053 | if(chan->pcm_fmt & VIA_PCM_FMT_STEREO) | |
1054 | m |= (1 << 20); | |
1055 | if(chan->pcm_fmt & VIA_PCM_FMT_16BIT) | |
1056 | m |= (1 << 21); | |
1057 | outl(m, chan->iobase + VIA_PCM_STOPRATE); | |
1058 | } | |
1059 | } | |
1060 | else | |
1061 | outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE); | |
1062 | ||
1063 | ||
1064 | DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n", | |
1065 | chan->pcm_fmt, | |
1066 | inb (chan->iobase + VIA_PCM_TYPE)); | |
1067 | } | |
1068 | ||
1069 | ||
1070 | /** | |
1071 | * via_chan_clear - Stop DMA channel operation, and reset pointers | |
1072 | * @card: the chip to accessed | |
1073 | * @chan: Channel to be cleared | |
1074 | * | |
1075 | * Call via_chan_stop to halt DMA operations, and then resets | |
1076 | * all software pointers which track DMA operation. | |
1077 | */ | |
1078 | ||
1079 | static void via_chan_clear (struct via_info *card, struct via_channel *chan) | |
1080 | { | |
1081 | DPRINTK ("ENTER\n"); | |
1082 | via_chan_stop (chan->iobase); | |
1083 | via_chan_buffer_free(card, chan); | |
1084 | chan->is_active = 0; | |
1085 | chan->is_mapped = 0; | |
1086 | chan->is_enabled = 1; | |
1087 | chan->slop_len = 0; | |
1088 | chan->sw_ptr = 0; | |
1089 | chan->n_irqs = 0; | |
1090 | atomic_set (&chan->hw_ptr, 0); | |
1091 | DPRINTK ("EXIT\n"); | |
1092 | } | |
1093 | ||
1094 | ||
1095 | /** | |
1096 | * via_chan_set_speed - Set PCM sample rate for given channel | |
1097 | * @card: Private info for specified board | |
1098 | * @chan: Channel whose sample rate will be adjusted | |
1099 | * @val: New sample rate, in Khz | |
1100 | * | |
1101 | * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics | |
1102 | * demand that all audio operations halt (if they are not already | |
1103 | * halted) when the %SNDCTL_DSP_SPEED is given. | |
1104 | * | |
1105 | * This function halts all audio operations for the given channel | |
1106 | * @chan, and then calls via_set_rate to set the audio hardware | |
1107 | * to the new rate. | |
1108 | */ | |
1109 | ||
1110 | static int via_chan_set_speed (struct via_info *card, | |
1111 | struct via_channel *chan, int val) | |
1112 | { | |
1113 | DPRINTK ("ENTER, requested rate = %d\n", val); | |
1114 | ||
1115 | via_chan_clear (card, chan); | |
1116 | ||
1117 | val = via_set_rate (card->ac97, chan, val); | |
1118 | ||
1119 | DPRINTK ("EXIT, returning %d\n", val); | |
1120 | return val; | |
1121 | } | |
1122 | ||
1123 | ||
1124 | /** | |
1125 | * via_chan_set_fmt - Set PCM sample size for given channel | |
1126 | * @card: Private info for specified board | |
1127 | * @chan: Channel whose sample size will be adjusted | |
1128 | * @val: New sample size, use the %AFMT_xxx constants | |
1129 | * | |
1130 | * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics | |
1131 | * demand that all audio operations halt (if they are not already | |
1132 | * halted) when the %SNDCTL_DSP_SETFMT is given. | |
1133 | * | |
1134 | * This function halts all audio operations for the given channel | |
1135 | * @chan, and then calls via_chan_pcm_fmt to set the audio hardware | |
1136 | * to the new sample size, either 8-bit or 16-bit. | |
1137 | */ | |
1138 | ||
1139 | static int via_chan_set_fmt (struct via_info *card, | |
1140 | struct via_channel *chan, int val) | |
1141 | { | |
1142 | DPRINTK ("ENTER, val=%s\n", | |
1143 | val == AFMT_U8 ? "AFMT_U8" : | |
1144 | val == AFMT_S16_LE ? "AFMT_S16_LE" : | |
1145 | "unknown"); | |
1146 | ||
1147 | via_chan_clear (card, chan); | |
1148 | ||
1149 | assert (val != AFMT_QUERY); /* this case is handled elsewhere */ | |
1150 | ||
1151 | switch (val) { | |
1152 | case AFMT_S16_LE: | |
1153 | if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) { | |
1154 | chan->pcm_fmt |= VIA_PCM_FMT_16BIT; | |
1155 | via_chan_pcm_fmt (chan, 0); | |
1156 | } | |
1157 | break; | |
1158 | ||
1159 | case AFMT_U8: | |
1160 | if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) { | |
1161 | chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT; | |
1162 | via_chan_pcm_fmt (chan, 0); | |
1163 | } | |
1164 | break; | |
1165 | ||
1166 | default: | |
1167 | DPRINTK ("unknown AFMT: 0x%X\n", val); | |
1168 | val = AFMT_S16_LE; | |
1169 | } | |
1170 | ||
1171 | DPRINTK ("EXIT\n"); | |
1172 | return val; | |
1173 | } | |
1174 | ||
1175 | ||
1176 | /** | |
1177 | * via_chan_set_stereo - Enable or disable stereo for a DMA channel | |
1178 | * @card: Private info for specified board | |
1179 | * @chan: Channel whose stereo setting will be adjusted | |
1180 | * @val: New sample size, use the %AFMT_xxx constants | |
1181 | * | |
1182 | * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics | |
1183 | * demand that all audio operations halt (if they are not already | |
1184 | * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given. | |
1185 | * | |
1186 | * This function halts all audio operations for the given channel | |
1187 | * @chan, and then calls via_chan_pcm_fmt to set the audio hardware | |
1188 | * to enable or disable stereo. | |
1189 | */ | |
1190 | ||
1191 | static int via_chan_set_stereo (struct via_info *card, | |
1192 | struct via_channel *chan, int val) | |
1193 | { | |
1194 | DPRINTK ("ENTER, channels = %d\n", val); | |
1195 | ||
1196 | via_chan_clear (card, chan); | |
1197 | ||
1198 | switch (val) { | |
1199 | ||
1200 | /* mono */ | |
1201 | case 1: | |
1202 | chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO; | |
1203 | chan->channels = 1; | |
1204 | via_chan_pcm_fmt (chan, 0); | |
1205 | break; | |
1206 | ||
1207 | /* stereo */ | |
1208 | case 2: | |
1209 | chan->pcm_fmt |= VIA_PCM_FMT_STEREO; | |
1210 | chan->channels = 2; | |
1211 | via_chan_pcm_fmt (chan, 0); | |
1212 | break; | |
1213 | ||
1214 | case 4: | |
1215 | case 6: | |
1216 | if(chan->is_multi) | |
1217 | { | |
1218 | chan->pcm_fmt |= VIA_PCM_FMT_STEREO; | |
1219 | chan->channels = val; | |
1220 | break; | |
1221 | } | |
1222 | /* unknown */ | |
1223 | default: | |
1224 | val = -EINVAL; | |
1225 | break; | |
1226 | } | |
1227 | ||
1228 | DPRINTK ("EXIT, returning %d\n", val); | |
1229 | return val; | |
1230 | } | |
1231 | ||
1232 | static int via_chan_set_buffering (struct via_info *card, | |
1233 | struct via_channel *chan, int val) | |
1234 | { | |
1235 | int shift; | |
1236 | ||
1237 | DPRINTK ("ENTER\n"); | |
1238 | ||
1239 | /* in both cases the buffer cannot be changed */ | |
1240 | if (chan->is_active || chan->is_mapped) { | |
1241 | DPRINTK ("EXIT\n"); | |
1242 | return -EINVAL; | |
1243 | } | |
1244 | ||
1245 | /* called outside SETFRAGMENT */ | |
1246 | /* set defaults or do nothing */ | |
1247 | if (val < 0) { | |
1248 | ||
1249 | if (chan->frag_size && chan->frag_number) | |
1250 | goto out; | |
1251 | ||
1252 | DPRINTK ("\n"); | |
1253 | ||
1254 | chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels | |
1255 | * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1; | |
1256 | ||
1257 | shift = 0; | |
1258 | while (chan->frag_size) { | |
1259 | chan->frag_size >>= 1; | |
1260 | shift++; | |
1261 | } | |
1262 | chan->frag_size = 1 << shift; | |
1263 | ||
1264 | chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME); | |
1265 | ||
1266 | DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number); | |
1267 | } else { | |
1268 | chan->frag_size = 1 << (val & 0xFFFF); | |
1269 | chan->frag_number = (val >> 16) & 0xFFFF; | |
1270 | ||
1271 | DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number); | |
1272 | } | |
1273 | ||
1274 | /* quake3 wants frag_number to be a power of two */ | |
1275 | shift = 0; | |
1276 | while (chan->frag_number) { | |
1277 | chan->frag_number >>= 1; | |
1278 | shift++; | |
1279 | } | |
1280 | chan->frag_number = 1 << shift; | |
1281 | ||
1282 | if (chan->frag_size > VIA_MAX_FRAG_SIZE) | |
1283 | chan->frag_size = VIA_MAX_FRAG_SIZE; | |
1284 | else if (chan->frag_size < VIA_MIN_FRAG_SIZE) | |
1285 | chan->frag_size = VIA_MIN_FRAG_SIZE; | |
1286 | ||
1287 | if (chan->frag_number < VIA_MIN_FRAG_NUMBER) | |
1288 | chan->frag_number = VIA_MIN_FRAG_NUMBER; | |
1289 | if (chan->frag_number > VIA_MAX_FRAG_NUMBER) | |
1290 | chan->frag_number = VIA_MAX_FRAG_NUMBER; | |
1291 | ||
1292 | if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES) | |
1293 | chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size; | |
1294 | ||
1295 | out: | |
1296 | if (chan->is_record) | |
1297 | atomic_set (&chan->n_frags, 0); | |
1298 | else | |
1299 | atomic_set (&chan->n_frags, chan->frag_number); | |
1300 | ||
1301 | DPRINTK ("EXIT\n"); | |
1302 | ||
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | #ifdef VIA_CHAN_DUMP_BUFS | |
1307 | /** | |
1308 | * via_chan_dump_bufs - Display DMA table contents | |
1309 | * @chan: Channel whose DMA table will be displayed | |
1310 | * | |
1311 | * Debugging function which displays the contents of the | |
1312 | * scatter-gather DMA table for the given channel @chan. | |
1313 | */ | |
1314 | ||
1315 | static void via_chan_dump_bufs (struct via_channel *chan) | |
1316 | { | |
1317 | int i; | |
1318 | ||
1319 | for (i = 0; i < chan->frag_number; i++) { | |
1320 | DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n", | |
1321 | i, chan->sgtable[i].addr, | |
1322 | chan->sgtable[i].count & 0x00FFFFFF, | |
1323 | chan->sgtable[i].count & VIA_FLAG ? 1 : 0, | |
1324 | chan->sgtable[i].count & VIA_EOL ? 1 : 0); | |
1325 | } | |
1326 | DPRINTK ("buf_in_use = %d, nextbuf = %d\n", | |
1327 | atomic_read (&chan->buf_in_use), | |
1328 | atomic_read (&chan->sw_ptr)); | |
1329 | } | |
1330 | #endif /* VIA_CHAN_DUMP_BUFS */ | |
1331 | ||
1332 | ||
1333 | /** | |
1334 | * via_chan_flush_frag - Flush partially-full playback buffer to hardware | |
1335 | * @chan: Channel whose DMA table will be flushed | |
1336 | * | |
1337 | * Flushes partially-full playback buffer to hardware. | |
1338 | */ | |
1339 | ||
1340 | static void via_chan_flush_frag (struct via_channel *chan) | |
1341 | { | |
1342 | DPRINTK ("ENTER\n"); | |
1343 | ||
1344 | assert (chan->slop_len > 0); | |
1345 | ||
1346 | if (chan->sw_ptr == (chan->frag_number - 1)) | |
1347 | chan->sw_ptr = 0; | |
1348 | else | |
1349 | chan->sw_ptr++; | |
1350 | ||
1351 | chan->slop_len = 0; | |
1352 | ||
1353 | assert (atomic_read (&chan->n_frags) > 0); | |
1354 | atomic_dec (&chan->n_frags); | |
1355 | ||
1356 | DPRINTK ("EXIT\n"); | |
1357 | } | |
1358 | ||
1359 | ||
1360 | ||
1361 | /** | |
1362 | * via_chan_maybe_start - Initiate audio hardware DMA operation | |
1363 | * @chan: Channel whose DMA is to be started | |
1364 | * | |
1365 | * Initiate DMA operation, if the DMA engine for the given | |
1366 | * channel @chan is not already active. | |
1367 | */ | |
1368 | ||
1369 | static inline void via_chan_maybe_start (struct via_channel *chan) | |
1370 | { | |
1371 | assert (chan->is_active == sg_active(chan->iobase)); | |
1372 | ||
1373 | DPRINTK ("MAYBE START %s\n", chan->name); | |
1374 | if (!chan->is_active && chan->is_enabled) { | |
1375 | chan->is_active = 1; | |
1376 | sg_begin (chan); | |
1377 | DPRINTK ("starting channel %s\n", chan->name); | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | ||
1382 | /**************************************************************** | |
1383 | * | |
1384 | * Interface to ac97-codec module | |
1385 | * | |
1386 | * | |
1387 | */ | |
1388 | ||
1389 | /** | |
1390 | * via_ac97_wait_idle - Wait until AC97 codec is not busy | |
1391 | * @card: Private info for specified board | |
1392 | * | |
1393 | * Sleep until the AC97 codec is no longer busy. | |
1394 | * Returns the final value read from the SGD | |
1395 | * register being polled. | |
1396 | */ | |
1397 | ||
1398 | static u8 via_ac97_wait_idle (struct via_info *card) | |
1399 | { | |
1400 | u8 tmp8; | |
1401 | int counter = VIA_COUNTER_LIMIT; | |
1402 | ||
1403 | DPRINTK ("ENTER/EXIT\n"); | |
1404 | ||
1405 | assert (card != NULL); | |
1406 | assert (card->pdev != NULL); | |
1407 | ||
1408 | do { | |
1409 | udelay (15); | |
1410 | ||
1411 | tmp8 = inb (card->baseaddr + 0x83); | |
1412 | } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0)); | |
1413 | ||
1414 | if (tmp8 & VIA_CR83_BUSY) | |
1415 | printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n"); | |
1416 | return tmp8; | |
1417 | } | |
1418 | ||
1419 | ||
1420 | /** | |
1421 | * via_ac97_read_reg - Read AC97 standard register | |
1422 | * @codec: Pointer to generic AC97 codec info | |
1423 | * @reg: Index of AC97 register to be read | |
1424 | * | |
1425 | * Read the value of a single AC97 codec register, | |
1426 | * as defined by the Intel AC97 specification. | |
1427 | * | |
1428 | * Defines the standard AC97 read-register operation | |
1429 | * required by the kernel's ac97_codec interface. | |
1430 | * | |
1431 | * Returns the 16-bit value stored in the specified | |
1432 | * register. | |
1433 | */ | |
1434 | ||
1435 | static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg) | |
1436 | { | |
1437 | unsigned long data; | |
1438 | struct via_info *card; | |
1439 | int counter; | |
1440 | ||
1441 | DPRINTK ("ENTER\n"); | |
1442 | ||
1443 | assert (codec != NULL); | |
1444 | assert (codec->private_data != NULL); | |
1445 | ||
1446 | card = codec->private_data; | |
1447 | ||
1448 | spin_lock(&card->ac97_lock); | |
1449 | ||
1450 | /* Every time we write to register 80 we cause a transaction. | |
1451 | The only safe way to clear the valid bit is to write it at | |
1452 | the same time as the command */ | |
1453 | data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID; | |
1454 | ||
1455 | outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL); | |
1456 | udelay (20); | |
1457 | ||
1458 | for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) { | |
1459 | udelay (1); | |
1460 | if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) & | |
1461 | (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID)) | |
1462 | goto out; | |
1463 | } | |
1464 | ||
1465 | printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data); | |
1466 | goto err_out; | |
1467 | ||
1468 | out: | |
1469 | /* Once the valid bit has become set, we must wait a complete AC97 | |
1470 | frame before the data has settled. */ | |
1471 | udelay(25); | |
1472 | data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL); | |
1473 | ||
1474 | outb (0x02, card->baseaddr + 0x83); | |
1475 | ||
1476 | if (((data & 0x007F0000) >> 16) == reg) { | |
1477 | DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n", | |
1478 | data, data & 0x0000FFFF); | |
1479 | spin_unlock(&card->ac97_lock); | |
1480 | return data & 0x0000FFFF; | |
1481 | } | |
1482 | ||
1483 | printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n", | |
1484 | reg, ((data & 0x007F0000) >> 16)); | |
1485 | ||
1486 | err_out: | |
1487 | spin_unlock(&card->ac97_lock); | |
1488 | DPRINTK ("EXIT, returning 0\n"); | |
1489 | return 0; | |
1490 | } | |
1491 | ||
1492 | ||
1493 | /** | |
1494 | * via_ac97_write_reg - Write AC97 standard register | |
1495 | * @codec: Pointer to generic AC97 codec info | |
1496 | * @reg: Index of AC97 register to be written | |
1497 | * @value: Value to be written to AC97 register | |
1498 | * | |
1499 | * Write the value of a single AC97 codec register, | |
1500 | * as defined by the Intel AC97 specification. | |
1501 | * | |
1502 | * Defines the standard AC97 write-register operation | |
1503 | * required by the kernel's ac97_codec interface. | |
1504 | */ | |
1505 | ||
1506 | static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value) | |
1507 | { | |
1508 | u32 data; | |
1509 | struct via_info *card; | |
1510 | int counter; | |
1511 | ||
1512 | DPRINTK ("ENTER\n"); | |
1513 | ||
1514 | assert (codec != NULL); | |
1515 | assert (codec->private_data != NULL); | |
1516 | ||
1517 | card = codec->private_data; | |
1518 | ||
1519 | spin_lock(&card->ac97_lock); | |
1520 | ||
1521 | data = (reg << 16) + value; | |
1522 | outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL); | |
1523 | udelay (10); | |
1524 | ||
1525 | for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) { | |
1526 | if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0) | |
1527 | goto out; | |
1528 | ||
1529 | udelay (15); | |
1530 | } | |
1531 | ||
1532 | printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value); | |
1533 | ||
1534 | out: | |
1535 | spin_unlock(&card->ac97_lock); | |
1536 | DPRINTK ("EXIT\n"); | |
1537 | } | |
1538 | ||
1539 | ||
1540 | static int via_mixer_open (struct inode *inode, struct file *file) | |
1541 | { | |
1542 | int minor = iminor(inode); | |
1543 | struct via_info *card; | |
1544 | struct pci_dev *pdev = NULL; | |
1545 | struct pci_driver *drvr; | |
1546 | ||
1547 | DPRINTK ("ENTER\n"); | |
1548 | ||
1549 | while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) { | |
1550 | drvr = pci_dev_driver (pdev); | |
1551 | if (drvr == &via_driver) { | |
1552 | assert (pci_get_drvdata (pdev) != NULL); | |
1553 | ||
1554 | card = pci_get_drvdata (pdev); | |
1555 | if (card->ac97->dev_mixer == minor) | |
1556 | goto match; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | DPRINTK ("EXIT, returning -ENODEV\n"); | |
1561 | return -ENODEV; | |
1562 | ||
1563 | match: | |
1564 | file->private_data = card->ac97; | |
1565 | ||
1566 | DPRINTK ("EXIT, returning 0\n"); | |
1567 | return nonseekable_open(inode, file); | |
1568 | } | |
1569 | ||
1570 | static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd, | |
1571 | unsigned long arg) | |
1572 | { | |
1573 | struct ac97_codec *codec = file->private_data; | |
1574 | struct via_info *card; | |
1575 | int nonblock = (file->f_flags & O_NONBLOCK); | |
1576 | int rc; | |
1577 | ||
1578 | DPRINTK ("ENTER\n"); | |
1579 | ||
1580 | assert (codec != NULL); | |
1581 | card = codec->private_data; | |
1582 | assert (card != NULL); | |
1583 | ||
1584 | rc = via_syscall_down (card, nonblock); | |
1585 | if (rc) goto out; | |
1586 | ||
1587 | #if 0 | |
1588 | /* | |
1589 | * Intercept volume control on 8233 and 8235 | |
1590 | */ | |
1591 | if(card->volume) | |
1592 | { | |
1593 | switch(cmd) | |
1594 | { | |
1595 | case SOUND_MIXER_READ_VOLUME: | |
1596 | return card->mixer_vol; | |
1597 | case SOUND_MIXER_WRITE_VOLUME: | |
1598 | { | |
1599 | int v; | |
1600 | if(get_user(v, (int *)arg)) | |
1601 | { | |
1602 | rc = -EFAULT; | |
1603 | goto out; | |
1604 | } | |
1605 | card->mixer_vol = v; | |
1606 | } | |
1607 | } | |
1608 | } | |
1609 | #endif | |
1610 | rc = codec->mixer_ioctl(codec, cmd, arg); | |
1611 | ||
1612 | up (&card->syscall_sem); | |
1613 | ||
1614 | out: | |
1615 | DPRINTK ("EXIT, returning %d\n", rc); | |
1616 | return rc; | |
1617 | } | |
1618 | ||
1619 | ||
1620 | static struct file_operations via_mixer_fops = { | |
1621 | .owner = THIS_MODULE, | |
1622 | .open = via_mixer_open, | |
1623 | .llseek = no_llseek, | |
1624 | .ioctl = via_mixer_ioctl, | |
1625 | }; | |
1626 | ||
1627 | ||
1628 | static int __devinit via_ac97_reset (struct via_info *card) | |
1629 | { | |
1630 | struct pci_dev *pdev = card->pdev; | |
1631 | u8 tmp8; | |
1632 | u16 tmp16; | |
1633 | ||
1634 | DPRINTK ("ENTER\n"); | |
1635 | ||
1636 | assert (pdev != NULL); | |
1637 | ||
1638 | #ifndef NDEBUG | |
1639 | { | |
1640 | u8 r40,r41,r42,r43,r44,r48; | |
1641 | pci_read_config_byte (card->pdev, 0x40, &r40); | |
1642 | pci_read_config_byte (card->pdev, 0x41, &r41); | |
1643 | pci_read_config_byte (card->pdev, 0x42, &r42); | |
1644 | pci_read_config_byte (card->pdev, 0x43, &r43); | |
1645 | pci_read_config_byte (card->pdev, 0x44, &r44); | |
1646 | pci_read_config_byte (card->pdev, 0x48, &r48); | |
1647 | DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n", | |
1648 | r40,r41,r42,r43,r44,r48); | |
1649 | ||
1650 | spin_lock_irq (&card->lock); | |
1651 | DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n", | |
1652 | inb (card->baseaddr + 0x00), | |
1653 | inb (card->baseaddr + 0x01), | |
1654 | inb (card->baseaddr + 0x02), | |
1655 | inl (card->baseaddr + 0x04), | |
1656 | inl (card->baseaddr + 0x0C), | |
1657 | inl (card->baseaddr + 0x80), | |
1658 | inl (card->baseaddr + 0x84)); | |
1659 | spin_unlock_irq (&card->lock); | |
1660 | ||
1661 | } | |
1662 | #endif | |
1663 | ||
1664 | /* | |
1665 | * Reset AC97 controller: enable, disable, enable, | |
1666 | * pausing after each command for good luck. Only | |
1667 | * do this if the codec is not ready, because it causes | |
1668 | * loud pops and such due to such a hard codec reset. | |
1669 | */ | |
1670 | pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8); | |
1671 | if ((tmp8 & VIA_CR40_AC97_READY) == 0) { | |
1672 | pci_write_config_byte (pdev, VIA_ACLINK_CTRL, | |
1673 | VIA_CR41_AC97_ENABLE | | |
1674 | VIA_CR41_AC97_RESET | | |
1675 | VIA_CR41_AC97_WAKEUP); | |
1676 | udelay (100); | |
1677 | ||
1678 | pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0); | |
1679 | udelay (100); | |
1680 | ||
1681 | pci_write_config_byte (pdev, VIA_ACLINK_CTRL, | |
1682 | VIA_CR41_AC97_ENABLE | | |
1683 | VIA_CR41_PCM_ENABLE | | |
1684 | VIA_CR41_VRA | VIA_CR41_AC97_RESET); | |
1685 | udelay (100); | |
1686 | } | |
1687 | ||
1688 | /* Make sure VRA is enabled, in case we didn't do a | |
1689 | * complete codec reset, above | |
1690 | */ | |
1691 | pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8); | |
1692 | if (((tmp8 & VIA_CR41_VRA) == 0) || | |
1693 | ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) || | |
1694 | ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) || | |
1695 | ((tmp8 & VIA_CR41_AC97_RESET) == 0)) { | |
1696 | pci_write_config_byte (pdev, VIA_ACLINK_CTRL, | |
1697 | VIA_CR41_AC97_ENABLE | | |
1698 | VIA_CR41_PCM_ENABLE | | |
1699 | VIA_CR41_VRA | VIA_CR41_AC97_RESET); | |
1700 | udelay (100); | |
1701 | } | |
1702 | ||
1703 | if(card->legacy) | |
1704 | { | |
1705 | #if 0 /* this breaks on K7M */ | |
1706 | /* disable legacy stuff */ | |
1707 | pci_write_config_byte (pdev, 0x42, 0x00); | |
1708 | udelay(10); | |
1709 | #endif | |
1710 | ||
1711 | /* route FM trap to IRQ, disable FM trap */ | |
1712 | pci_write_config_byte (pdev, 0x48, 0x05); | |
1713 | udelay(10); | |
1714 | } | |
1715 | ||
1716 | /* disable all codec GPI interrupts */ | |
1717 | outl (0, pci_resource_start (pdev, 0) + 0x8C); | |
1718 | ||
1719 | /* WARNING: this line is magic. Remove this | |
1720 | * and things break. */ | |
1721 | /* enable variable rate */ | |
1722 | tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS); | |
1723 | if ((tmp16 & 1) == 0) | |
1724 | via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1); | |
1725 | ||
1726 | DPRINTK ("EXIT, returning 0\n"); | |
1727 | return 0; | |
1728 | } | |
1729 | ||
1730 | ||
1731 | static void via_ac97_codec_wait (struct ac97_codec *codec) | |
1732 | { | |
1733 | assert (codec->private_data != NULL); | |
1734 | via_ac97_wait_idle (codec->private_data); | |
1735 | } | |
1736 | ||
1737 | ||
1738 | static int __devinit via_ac97_init (struct via_info *card) | |
1739 | { | |
1740 | int rc; | |
1741 | u16 tmp16; | |
1742 | ||
1743 | DPRINTK ("ENTER\n"); | |
1744 | ||
1745 | assert (card != NULL); | |
1746 | ||
1747 | card->ac97 = ac97_alloc_codec(); | |
1748 | if(card->ac97 == NULL) | |
1749 | return -ENOMEM; | |
1750 | ||
1751 | card->ac97->private_data = card; | |
1752 | card->ac97->codec_read = via_ac97_read_reg; | |
1753 | card->ac97->codec_write = via_ac97_write_reg; | |
1754 | card->ac97->codec_wait = via_ac97_codec_wait; | |
1755 | ||
1756 | card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1); | |
1757 | if (card->ac97->dev_mixer < 0) { | |
1758 | printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n"); | |
1759 | DPRINTK ("EXIT, returning -EIO\n"); | |
1760 | ac97_release_codec(card->ac97); | |
1761 | return -EIO; | |
1762 | } | |
1763 | ||
1764 | rc = via_ac97_reset (card); | |
1765 | if (rc) { | |
1766 | printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n"); | |
1767 | goto err_out; | |
1768 | } | |
1769 | ||
1770 | mdelay(10); | |
1771 | ||
1772 | if (ac97_probe_codec (card->ac97) == 0) { | |
1773 | printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n"); | |
1774 | rc = -EIO; | |
1775 | goto err_out; | |
1776 | } | |
1777 | ||
1778 | /* enable variable rate */ | |
1779 | tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS); | |
1780 | via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1); | |
1781 | ||
1782 | /* | |
1783 | * If we cannot enable VRA, we have a locked-rate codec. | |
1784 | * We try again to enable VRA before assuming so, however. | |
1785 | */ | |
1786 | tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS); | |
1787 | if ((tmp16 & 1) == 0) { | |
1788 | via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1); | |
1789 | tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS); | |
1790 | if ((tmp16 & 1) == 0) { | |
1791 | card->locked_rate = 1; | |
1792 | printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n"); | |
1793 | } | |
1794 | } | |
1795 | ||
1796 | DPRINTK ("EXIT, returning 0\n"); | |
1797 | return 0; | |
1798 | ||
1799 | err_out: | |
1800 | unregister_sound_mixer (card->ac97->dev_mixer); | |
1801 | DPRINTK ("EXIT, returning %d\n", rc); | |
1802 | ac97_release_codec(card->ac97); | |
1803 | return rc; | |
1804 | } | |
1805 | ||
1806 | ||
1807 | static void via_ac97_cleanup (struct via_info *card) | |
1808 | { | |
1809 | DPRINTK ("ENTER\n"); | |
1810 | ||
1811 | assert (card != NULL); | |
1812 | assert (card->ac97->dev_mixer >= 0); | |
1813 | ||
1814 | unregister_sound_mixer (card->ac97->dev_mixer); | |
1815 | ac97_release_codec(card->ac97); | |
1816 | ||
1817 | DPRINTK ("EXIT\n"); | |
1818 | } | |
1819 | ||
1820 | ||
1821 | ||
1822 | /**************************************************************** | |
1823 | * | |
1824 | * Interrupt-related code | |
1825 | * | |
1826 | */ | |
1827 | ||
1828 | /** | |
1829 | * via_intr_channel - handle an interrupt for a single channel | |
1830 | * @card: unused | |
1831 | * @chan: handle interrupt for this channel | |
1832 | * | |
1833 | * This is the "meat" of the interrupt handler, | |
1834 | * containing the actions taken each time an interrupt | |
1835 | * occurs. All communication and coordination with | |
1836 | * userspace takes place here. | |
1837 | * | |
1838 | * Locking: inside card->lock | |
1839 | */ | |
1840 | ||
1841 | static void via_intr_channel (struct via_info *card, struct via_channel *chan) | |
1842 | { | |
1843 | u8 status; | |
1844 | int n; | |
1845 | ||
1846 | /* check pertinent bits of status register for action bits */ | |
1847 | status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED); | |
1848 | if (!status) | |
1849 | return; | |
1850 | ||
1851 | /* acknowledge any flagged bits ASAP */ | |
1852 | outb (status, chan->iobase); | |
1853 | ||
1854 | if (!chan->sgtable) /* XXX: temporary solution */ | |
1855 | return; | |
1856 | ||
1857 | /* grab current h/w ptr value */ | |
1858 | n = atomic_read (&chan->hw_ptr); | |
1859 | ||
1860 | /* sanity check: make sure our h/w ptr doesn't have a weird value */ | |
1861 | assert (n >= 0); | |
1862 | assert (n < chan->frag_number); | |
1863 | ||
1864 | ||
1865 | /* reset SGD data structure in memory to reflect a full buffer, | |
1866 | * and advance the h/w ptr, wrapping around to zero if needed | |
1867 | */ | |
1868 | if (n == (chan->frag_number - 1)) { | |
1869 | chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL); | |
1870 | atomic_set (&chan->hw_ptr, 0); | |
1871 | } else { | |
1872 | chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG); | |
1873 | atomic_inc (&chan->hw_ptr); | |
1874 | } | |
1875 | ||
1876 | /* accounting crap for SNDCTL_DSP_GETxPTR */ | |
1877 | chan->n_irqs++; | |
1878 | chan->bytes += chan->frag_size; | |
1879 | /* FIXME - signed overflow is undefined */ | |
1880 | if (chan->bytes < 0) /* handle overflow of 31-bit value */ | |
1881 | chan->bytes = chan->frag_size; | |
1882 | /* all following checks only occur when not in mmap(2) mode */ | |
1883 | if (!chan->is_mapped) | |
1884 | { | |
1885 | /* If we are recording, then n_frags represents the number | |
1886 | * of fragments waiting to be handled by userspace. | |
1887 | * If we are playback, then n_frags represents the number | |
1888 | * of fragments remaining to be filled by userspace. | |
1889 | * We increment here. If we reach max number of fragments, | |
1890 | * this indicates an underrun/overrun. For this case under OSS, | |
1891 | * we stop the record/playback process. | |
1892 | */ | |
1893 | if (atomic_read (&chan->n_frags) < chan->frag_number) | |
1894 | atomic_inc (&chan->n_frags); | |
1895 | assert (atomic_read (&chan->n_frags) <= chan->frag_number); | |
1896 | if (atomic_read (&chan->n_frags) == chan->frag_number) { | |
1897 | chan->is_active = 0; | |
1898 | via_chan_stop (chan->iobase); | |
1899 | } | |
1900 | } | |
1901 | /* wake up anyone listening to see when interrupts occur */ | |
1902 | wake_up_all (&chan->wait); | |
1903 | ||
1904 | DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n", | |
1905 | chan->name, status, (long) inl (chan->iobase + 0x04), | |
1906 | atomic_read (&chan->hw_ptr)); | |
1907 | ||
1908 | DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name, | |
1909 | atomic_read (&chan->n_frags), missed); | |
1910 | } | |
1911 | ||
1912 | ||
1913 | static irqreturn_t via_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
1914 | { | |
1915 | struct via_info *card = dev_id; | |
1916 | u32 status32; | |
1917 | ||
1918 | /* to minimize interrupt sharing costs, we use the SGD status | |
1919 | * shadow register to check the status of all inputs and | |
1920 | * outputs with a single 32-bit bus read. If no interrupt | |
1921 | * conditions are flagged, we exit immediately | |
1922 | */ | |
1923 | status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW); | |
1924 | if (!(status32 & VIA_INTR_MASK)) | |
1925 | { | |
1926 | #ifdef CONFIG_MIDI_VIA82CXXX | |
1927 | if (card->midi_devc) | |
1928 | uart401intr(irq, card->midi_devc, regs); | |
1929 | #endif | |
1930 | return IRQ_HANDLED; | |
1931 | } | |
1932 | DPRINTK ("intr, status32 == 0x%08X\n", status32); | |
1933 | ||
1934 | /* synchronize interrupt handling under SMP. this spinlock | |
1935 | * goes away completely on UP | |
1936 | */ | |
1937 | spin_lock (&card->lock); | |
1938 | ||
1939 | if (status32 & VIA_INTR_OUT) | |
1940 | via_intr_channel (card, &card->ch_out); | |
1941 | if (status32 & VIA_INTR_IN) | |
1942 | via_intr_channel (card, &card->ch_in); | |
1943 | if (status32 & VIA_INTR_FM) | |
1944 | via_intr_channel (card, &card->ch_fm); | |
1945 | ||
1946 | spin_unlock (&card->lock); | |
1947 | ||
1948 | return IRQ_HANDLED; | |
1949 | } | |
1950 | ||
1951 | static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
1952 | { | |
1953 | struct via_info *card = dev_id; | |
1954 | u32 status32; | |
1955 | ||
1956 | /* to minimize interrupt sharing costs, we use the SGD status | |
1957 | * shadow register to check the status of all inputs and | |
1958 | * outputs with a single 32-bit bus read. If no interrupt | |
1959 | * conditions are flagged, we exit immediately | |
1960 | */ | |
1961 | status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW); | |
1962 | if (!(status32 & VIA_NEW_INTR_MASK)) | |
1963 | return IRQ_NONE; | |
1964 | /* | |
1965 | * goes away completely on UP | |
1966 | */ | |
1967 | spin_lock (&card->lock); | |
1968 | ||
1969 | via_intr_channel (card, &card->ch_out); | |
1970 | via_intr_channel (card, &card->ch_in); | |
1971 | via_intr_channel (card, &card->ch_fm); | |
1972 | ||
1973 | spin_unlock (&card->lock); | |
1974 | return IRQ_HANDLED; | |
1975 | } | |
1976 | ||
1977 | ||
1978 | /** | |
1979 | * via_interrupt_init - Initialize interrupt handling | |
1980 | * @card: Private info for specified board | |
1981 | * | |
1982 | * Obtain and reserve IRQ for using in handling audio events. | |
1983 | * Also, disable any IRQ-generating resources, to make sure | |
1984 | * we don't get interrupts before we want them. | |
1985 | */ | |
1986 | ||
1987 | static int via_interrupt_init (struct via_info *card) | |
1988 | { | |
1989 | u8 tmp8; | |
1990 | ||
1991 | DPRINTK ("ENTER\n"); | |
1992 | ||
1993 | assert (card != NULL); | |
1994 | assert (card->pdev != NULL); | |
1995 | ||
1996 | /* check for sane IRQ number. can this ever happen? */ | |
1997 | if (card->pdev->irq < 2) { | |
1998 | printk (KERN_ERR PFX "insane IRQ %d, aborting\n", | |
1999 | card->pdev->irq); | |
2000 | DPRINTK ("EXIT, returning -EIO\n"); | |
2001 | return -EIO; | |
2002 | } | |
2003 | ||
2004 | /* VIA requires this is done */ | |
2005 | pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq); | |
2006 | ||
2007 | if(card->legacy) | |
2008 | { | |
2009 | /* make sure FM irq is not routed to us */ | |
2010 | pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8); | |
2011 | if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) { | |
2012 | tmp8 |= VIA_CR48_FM_TRAP_TO_NMI; | |
2013 | pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8); | |
2014 | } | |
2015 | if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) { | |
2016 | printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n", | |
2017 | card->pdev->irq); | |
2018 | DPRINTK ("EXIT, returning -EBUSY\n"); | |
2019 | return -EBUSY; | |
2020 | } | |
2021 | } | |
2022 | else | |
2023 | { | |
2024 | if (request_irq (card->pdev->irq, via_new_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) { | |
2025 | printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n", | |
2026 | card->pdev->irq); | |
2027 | DPRINTK ("EXIT, returning -EBUSY\n"); | |
2028 | return -EBUSY; | |
2029 | } | |
2030 | } | |
2031 | ||
2032 | DPRINTK ("EXIT, returning 0\n"); | |
2033 | return 0; | |
2034 | } | |
2035 | ||
2036 | ||
2037 | /**************************************************************** | |
2038 | * | |
2039 | * OSS DSP device | |
2040 | * | |
2041 | */ | |
2042 | ||
2043 | static struct file_operations via_dsp_fops = { | |
2044 | .owner = THIS_MODULE, | |
2045 | .open = via_dsp_open, | |
2046 | .release = via_dsp_release, | |
2047 | .read = via_dsp_read, | |
2048 | .write = via_dsp_write, | |
2049 | .poll = via_dsp_poll, | |
2050 | .llseek = no_llseek, | |
2051 | .ioctl = via_dsp_ioctl, | |
2052 | .mmap = via_dsp_mmap, | |
2053 | }; | |
2054 | ||
2055 | ||
2056 | static int __devinit via_dsp_init (struct via_info *card) | |
2057 | { | |
2058 | u8 tmp8; | |
2059 | ||
2060 | DPRINTK ("ENTER\n"); | |
2061 | ||
2062 | assert (card != NULL); | |
2063 | ||
2064 | if(card->legacy) | |
2065 | { | |
2066 | /* turn off legacy features, if not already */ | |
2067 | pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8); | |
2068 | if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) { | |
2069 | tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE); | |
2070 | pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8); | |
2071 | } | |
2072 | } | |
2073 | ||
2074 | via_stop_everything (card); | |
2075 | ||
2076 | card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1); | |
2077 | if (card->dev_dsp < 0) { | |
2078 | DPRINTK ("EXIT, returning -ENODEV\n"); | |
2079 | return -ENODEV; | |
2080 | } | |
2081 | DPRINTK ("EXIT, returning 0\n"); | |
2082 | return 0; | |
2083 | } | |
2084 | ||
2085 | ||
2086 | static void via_dsp_cleanup (struct via_info *card) | |
2087 | { | |
2088 | DPRINTK ("ENTER\n"); | |
2089 | ||
2090 | assert (card != NULL); | |
2091 | assert (card->dev_dsp >= 0); | |
2092 | ||
2093 | via_stop_everything (card); | |
2094 | ||
2095 | unregister_sound_dsp (card->dev_dsp); | |
2096 | ||
2097 | DPRINTK ("EXIT\n"); | |
2098 | } | |
2099 | ||
2100 | ||
2101 | static struct page * via_mm_nopage (struct vm_area_struct * vma, | |
2102 | unsigned long address, int *type) | |
2103 | { | |
2104 | struct via_info *card = vma->vm_private_data; | |
2105 | struct via_channel *chan = &card->ch_out; | |
2106 | struct page *dmapage; | |
2107 | unsigned long pgoff; | |
2108 | int rd, wr; | |
2109 | ||
2110 | DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n", | |
2111 | vma->vm_start, | |
2112 | address - vma->vm_start, | |
2113 | (address - vma->vm_start) >> PAGE_SHIFT, | |
2114 | address); | |
2115 | ||
2116 | if (address > vma->vm_end) { | |
2117 | DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n"); | |
2118 | return NOPAGE_SIGBUS; /* Disallow mremap */ | |
2119 | } | |
2120 | if (!card) { | |
2121 | DPRINTK ("EXIT, returning NOPAGE_OOM\n"); | |
2122 | return NOPAGE_OOM; /* Nothing allocated */ | |
2123 | } | |
2124 | ||
2125 | pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT); | |
2126 | rd = card->ch_in.is_mapped; | |
2127 | wr = card->ch_out.is_mapped; | |
2128 | ||
2129 | #ifndef VIA_NDEBUG | |
2130 | { | |
2131 | unsigned long max_bufs = chan->frag_number; | |
2132 | if (rd && wr) max_bufs *= 2; | |
2133 | /* via_dsp_mmap() should ensure this */ | |
2134 | assert (pgoff < max_bufs); | |
2135 | } | |
2136 | #endif | |
2137 | ||
2138 | /* if full-duplex (read+write) and we have two sets of bufs, | |
2139 | * then the playback buffers come first, sez soundcard.c */ | |
2140 | if (pgoff >= chan->page_number) { | |
2141 | pgoff -= chan->page_number; | |
2142 | chan = &card->ch_in; | |
2143 | } else if (!wr) | |
2144 | chan = &card->ch_in; | |
2145 | ||
2146 | assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0); | |
2147 | ||
2148 | dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr); | |
2149 | DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n", | |
2150 | dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr); | |
2151 | get_page (dmapage); | |
2152 | if (type) | |
2153 | *type = VM_FAULT_MINOR; | |
2154 | return dmapage; | |
2155 | } | |
2156 | ||
2157 | ||
2158 | #ifndef VM_RESERVED | |
2159 | static int via_mm_swapout (struct page *page, struct file *filp) | |
2160 | { | |
2161 | return 0; | |
2162 | } | |
2163 | #endif /* VM_RESERVED */ | |
2164 | ||
2165 | ||
2166 | static struct vm_operations_struct via_mm_ops = { | |
2167 | .nopage = via_mm_nopage, | |
2168 | ||
2169 | #ifndef VM_RESERVED | |
2170 | .swapout = via_mm_swapout, | |
2171 | #endif | |
2172 | }; | |
2173 | ||
2174 | ||
2175 | static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma) | |
2176 | { | |
2177 | struct via_info *card; | |
2178 | int nonblock = (file->f_flags & O_NONBLOCK); | |
2179 | int rc = -EINVAL, rd=0, wr=0; | |
2180 | unsigned long max_size, size, start, offset; | |
2181 | ||
2182 | assert (file != NULL); | |
2183 | assert (vma != NULL); | |
2184 | card = file->private_data; | |
2185 | assert (card != NULL); | |
2186 | ||
2187 | DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n", | |
2188 | vma->vm_start, | |
2189 | vma->vm_end - vma->vm_start, | |
2190 | vma->vm_pgoff); | |
2191 | ||
2192 | max_size = 0; | |
2193 | if (vma->vm_flags & VM_READ) { | |
2194 | rd = 1; | |
2195 | via_chan_set_buffering(card, &card->ch_in, -1); | |
2196 | via_chan_buffer_init (card, &card->ch_in); | |
2197 | max_size += card->ch_in.page_number << PAGE_SHIFT; | |
2198 | } | |
2199 | if (vma->vm_flags & VM_WRITE) { | |
2200 | wr = 1; | |
2201 | via_chan_set_buffering(card, &card->ch_out, -1); | |
2202 | via_chan_buffer_init (card, &card->ch_out); | |
2203 | max_size += card->ch_out.page_number << PAGE_SHIFT; | |
2204 | } | |
2205 | ||
2206 | start = vma->vm_start; | |
2207 | offset = (vma->vm_pgoff << PAGE_SHIFT); | |
2208 | size = vma->vm_end - vma->vm_start; | |
2209 | ||
2210 | /* some basic size/offset sanity checks */ | |
2211 | if (size > max_size) | |
2212 | goto out; | |
2213 | if (offset > max_size - size) | |
2214 | goto out; | |
2215 | ||
2216 | rc = via_syscall_down (card, nonblock); | |
2217 | if (rc) goto out; | |
2218 | ||
2219 | vma->vm_ops = &via_mm_ops; | |
2220 | vma->vm_private_data = card; | |
2221 | ||
2222 | #ifdef VM_RESERVED | |
2223 | vma->vm_flags |= VM_RESERVED; | |
2224 | #endif | |
2225 | ||
2226 | if (rd) | |
2227 | card->ch_in.is_mapped = 1; | |
2228 | if (wr) | |
2229 | card->ch_out.is_mapped = 1; | |
2230 | ||
2231 | up (&card->syscall_sem); | |
2232 | rc = 0; | |
2233 | ||
2234 | out: | |
2235 | DPRINTK ("EXIT, returning %d\n", rc); | |
2236 | return rc; | |
2237 | } | |
2238 | ||
2239 | ||
2240 | static ssize_t via_dsp_do_read (struct via_info *card, | |
2241 | char __user *userbuf, size_t count, | |
2242 | int nonblock) | |
2243 | { | |
2244 | DECLARE_WAITQUEUE(wait, current); | |
2245 | const char __user *orig_userbuf = userbuf; | |
2246 | struct via_channel *chan = &card->ch_in; | |
2247 | size_t size; | |
2248 | int n, tmp; | |
2249 | ssize_t ret = 0; | |
2250 | ||
2251 | /* if SGD has not yet been started, start it */ | |
2252 | via_chan_maybe_start (chan); | |
2253 | ||
2254 | handle_one_block: | |
2255 | /* just to be a nice neighbor */ | |
2256 | /* Thomas Sailer: | |
2257 | * But also to ourselves, release semaphore if we do so */ | |
2258 | if (need_resched()) { | |
2259 | up(&card->syscall_sem); | |
2260 | schedule (); | |
2261 | ret = via_syscall_down (card, nonblock); | |
2262 | if (ret) | |
2263 | goto out; | |
2264 | } | |
2265 | ||
2266 | /* grab current channel software pointer. In the case of | |
2267 | * recording, this is pointing to the next buffer that | |
2268 | * will receive data from the audio hardware. | |
2269 | */ | |
2270 | n = chan->sw_ptr; | |
2271 | ||
2272 | /* n_frags represents the number of fragments waiting | |
2273 | * to be copied to userland. sleep until at least | |
2274 | * one buffer has been read from the audio hardware. | |
2275 | */ | |
2276 | add_wait_queue(&chan->wait, &wait); | |
2277 | for (;;) { | |
2278 | __set_current_state(TASK_INTERRUPTIBLE); | |
2279 | tmp = atomic_read (&chan->n_frags); | |
2280 | assert (tmp >= 0); | |
2281 | assert (tmp <= chan->frag_number); | |
2282 | if (tmp) | |
2283 | break; | |
2284 | if (nonblock || !chan->is_active) { | |
2285 | ret = -EAGAIN; | |
2286 | break; | |
2287 | } | |
2288 | ||
2289 | up(&card->syscall_sem); | |
2290 | ||
2291 | DPRINTK ("Sleeping on block %d\n", n); | |
2292 | schedule(); | |
2293 | ||
2294 | ret = via_syscall_down (card, nonblock); | |
2295 | if (ret) | |
2296 | break; | |
2297 | ||
2298 | if (signal_pending (current)) { | |
2299 | ret = -ERESTARTSYS; | |
2300 | break; | |
2301 | } | |
2302 | } | |
2303 | set_current_state(TASK_RUNNING); | |
2304 | remove_wait_queue(&chan->wait, &wait); | |
2305 | if (ret) | |
2306 | goto out; | |
2307 | ||
2308 | /* Now that we have a buffer we can read from, send | |
2309 | * as much as sample data possible to userspace. | |
2310 | */ | |
2311 | while ((count > 0) && (chan->slop_len < chan->frag_size)) { | |
2312 | size_t slop_left = chan->frag_size - chan->slop_len; | |
2313 | void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr; | |
2314 | unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size; | |
2315 | ||
2316 | size = (count < slop_left) ? count : slop_left; | |
2317 | if (copy_to_user (userbuf, | |
2318 | base + ofs + chan->slop_len, | |
2319 | size)) { | |
2320 | ret = -EFAULT; | |
2321 | goto out; | |
2322 | } | |
2323 | ||
2324 | count -= size; | |
2325 | chan->slop_len += size; | |
2326 | userbuf += size; | |
2327 | } | |
2328 | ||
2329 | /* If we didn't copy the buffer completely to userspace, | |
2330 | * stop now. | |
2331 | */ | |
2332 | if (chan->slop_len < chan->frag_size) | |
2333 | goto out; | |
2334 | ||
2335 | /* | |
2336 | * If we get to this point, we copied one buffer completely | |
2337 | * to userspace, give the buffer back to the hardware. | |
2338 | */ | |
2339 | ||
2340 | /* advance channel software pointer to point to | |
2341 | * the next buffer from which we will copy | |
2342 | */ | |
2343 | if (chan->sw_ptr == (chan->frag_number - 1)) | |
2344 | chan->sw_ptr = 0; | |
2345 | else | |
2346 | chan->sw_ptr++; | |
2347 | ||
2348 | /* mark one less buffer waiting to be processed */ | |
2349 | assert (atomic_read (&chan->n_frags) > 0); | |
2350 | atomic_dec (&chan->n_frags); | |
2351 | ||
2352 | /* we are at a block boundary, there is no fragment data */ | |
2353 | chan->slop_len = 0; | |
2354 | ||
2355 | DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n", | |
2356 | n, chan->sw_ptr, atomic_read (&chan->n_frags)); | |
2357 | ||
2358 | DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n", | |
2359 | inb (card->baseaddr + 0x00), | |
2360 | inb (card->baseaddr + 0x01), | |
2361 | inb (card->baseaddr + 0x02), | |
2362 | inl (card->baseaddr + 0x04), | |
2363 | inl (card->baseaddr + 0x0C), | |
2364 | inl (card->baseaddr + 0x80), | |
2365 | inl (card->baseaddr + 0x84)); | |
2366 | ||
2367 | if (count > 0) | |
2368 | goto handle_one_block; | |
2369 | ||
2370 | out: | |
2371 | return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret; | |
2372 | } | |
2373 | ||
2374 | ||
2375 | static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) | |
2376 | { | |
2377 | struct via_info *card; | |
2378 | int nonblock = (file->f_flags & O_NONBLOCK); | |
2379 | int rc; | |
2380 | ||
2381 | DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n", | |
2382 | file, buffer, count, ppos ? ((unsigned long)*ppos) : 0); | |
2383 | ||
2384 | assert (file != NULL); | |
2385 | card = file->private_data; | |
2386 | assert (card != NULL); | |
2387 | ||
2388 | rc = via_syscall_down (card, nonblock); | |
2389 | if (rc) goto out; | |
2390 | ||
2391 | if (card->ch_in.is_mapped) { | |
2392 | rc = -ENXIO; | |
2393 | goto out_up; | |
2394 | } | |
2395 | ||
2396 | via_chan_set_buffering(card, &card->ch_in, -1); | |
2397 | rc = via_chan_buffer_init (card, &card->ch_in); | |
2398 | ||
2399 | if (rc) | |
2400 | goto out_up; | |
2401 | ||
2402 | rc = via_dsp_do_read (card, buffer, count, nonblock); | |
2403 | ||
2404 | out_up: | |
2405 | up (&card->syscall_sem); | |
2406 | out: | |
2407 | DPRINTK ("EXIT, returning %ld\n",(long) rc); | |
2408 | return rc; | |
2409 | } | |
2410 | ||
2411 | ||
2412 | static ssize_t via_dsp_do_write (struct via_info *card, | |
2413 | const char __user *userbuf, size_t count, | |
2414 | int nonblock) | |
2415 | { | |
2416 | DECLARE_WAITQUEUE(wait, current); | |
2417 | const char __user *orig_userbuf = userbuf; | |
2418 | struct via_channel *chan = &card->ch_out; | |
2419 | volatile struct via_sgd_table *sgtable = chan->sgtable; | |
2420 | size_t size; | |
2421 | int n, tmp; | |
2422 | ssize_t ret = 0; | |
2423 | ||
2424 | handle_one_block: | |
2425 | /* just to be a nice neighbor */ | |
2426 | /* Thomas Sailer: | |
2427 | * But also to ourselves, release semaphore if we do so */ | |
2428 | if (need_resched()) { | |
2429 | up(&card->syscall_sem); | |
2430 | schedule (); | |
2431 | ret = via_syscall_down (card, nonblock); | |
2432 | if (ret) | |
2433 | goto out; | |
2434 | } | |
2435 | ||
2436 | /* grab current channel fragment pointer. In the case of | |
2437 | * playback, this is pointing to the next fragment that | |
2438 | * should receive data from userland. | |
2439 | */ | |
2440 | n = chan->sw_ptr; | |
2441 | ||
2442 | /* n_frags represents the number of fragments remaining | |
2443 | * to be filled by userspace. Sleep until | |
2444 | * at least one fragment is available for our use. | |
2445 | */ | |
2446 | add_wait_queue(&chan->wait, &wait); | |
2447 | for (;;) { | |
2448 | __set_current_state(TASK_INTERRUPTIBLE); | |
2449 | tmp = atomic_read (&chan->n_frags); | |
2450 | assert (tmp >= 0); | |
2451 | assert (tmp <= chan->frag_number); | |
2452 | if (tmp) | |
2453 | break; | |
2454 | if (nonblock || !chan->is_active) { | |
2455 | ret = -EAGAIN; | |
2456 | break; | |
2457 | } | |
2458 | ||
2459 | up(&card->syscall_sem); | |
2460 | ||
2461 | DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record); | |
2462 | schedule(); | |
2463 | ||
2464 | ret = via_syscall_down (card, nonblock); | |
2465 | if (ret) | |
2466 | break; | |
2467 | ||
2468 | if (signal_pending (current)) { | |
2469 | ret = -ERESTARTSYS; | |
2470 | break; | |
2471 | } | |
2472 | } | |
2473 | set_current_state(TASK_RUNNING); | |
2474 | remove_wait_queue(&chan->wait, &wait); | |
2475 | if (ret) | |
2476 | goto out; | |
2477 | ||
2478 | /* Now that we have at least one fragment we can write to, fill the buffer | |
2479 | * as much as possible with data from userspace. | |
2480 | */ | |
2481 | while ((count > 0) && (chan->slop_len < chan->frag_size)) { | |
2482 | size_t slop_left = chan->frag_size - chan->slop_len; | |
2483 | ||
2484 | size = (count < slop_left) ? count : slop_left; | |
2485 | if (copy_from_user (chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr + (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size + chan->slop_len, | |
2486 | userbuf, size)) { | |
2487 | ret = -EFAULT; | |
2488 | goto out; | |
2489 | } | |
2490 | ||
2491 | count -= size; | |
2492 | chan->slop_len += size; | |
2493 | userbuf += size; | |
2494 | } | |
2495 | ||
2496 | /* If we didn't fill up the buffer with data, stop now. | |
2497 | * Put a 'stop' marker in the DMA table too, to tell the | |
2498 | * audio hardware to stop if it gets here. | |
2499 | */ | |
2500 | if (chan->slop_len < chan->frag_size) { | |
2501 | sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP); | |
2502 | goto out; | |
2503 | } | |
2504 | ||
2505 | /* | |
2506 | * If we get to this point, we have filled a buffer with | |
2507 | * audio data, flush the buffer to audio hardware. | |
2508 | */ | |
2509 | ||
2510 | /* Record the true size for the audio hardware to notice */ | |
2511 | if (n == (chan->frag_number - 1)) | |
2512 | sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL); | |
2513 | else | |
2514 | sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG); | |
2515 | ||
2516 | /* advance channel software pointer to point to | |
2517 | * the next buffer we will fill with data | |
2518 | */ | |
2519 | if (chan->sw_ptr == (chan->frag_number - 1)) | |
2520 | chan->sw_ptr = 0; | |
2521 | else | |
2522 | chan->sw_ptr++; | |
2523 | ||
2524 | /* mark one less buffer as being available for userspace consumption */ | |
2525 | assert (atomic_read (&chan->n_frags) > 0); | |
2526 | atomic_dec (&chan->n_frags); | |
2527 | ||
2528 | /* we are at a block boundary, there is no fragment data */ | |
2529 | chan->slop_len = 0; | |
2530 | ||
2531 | /* if SGD has not yet been started, start it */ | |
2532 | via_chan_maybe_start (chan); | |
2533 | ||
2534 | DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n", | |
2535 | n, chan->sw_ptr, atomic_read (&chan->n_frags)); | |
2536 | ||
2537 | DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n", | |
2538 | inb (card->baseaddr + 0x00), | |
2539 | inb (card->baseaddr + 0x01), | |
2540 | inb (card->baseaddr + 0x02), | |
2541 | inl (card->baseaddr + 0x04), | |
2542 | inl (card->baseaddr + 0x08), | |
2543 | inl (card->baseaddr + 0x0C), | |
2544 | inl (card->baseaddr + 0x80), | |
2545 | inl (card->baseaddr + 0x84)); | |
2546 | ||
2547 | if (count > 0) | |
2548 | goto handle_one_block; | |
2549 | ||
2550 | out: | |
2551 | if (userbuf - orig_userbuf) | |
2552 | return userbuf - orig_userbuf; | |
2553 | else | |
2554 | return ret; | |
2555 | } | |
2556 | ||
2557 | ||
2558 | static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | |
2559 | { | |
2560 | struct via_info *card; | |
2561 | ssize_t rc; | |
2562 | int nonblock = (file->f_flags & O_NONBLOCK); | |
2563 | ||
2564 | DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n", | |
2565 | file, buffer, count, ppos ? ((unsigned long)*ppos) : 0); | |
2566 | ||
2567 | assert (file != NULL); | |
2568 | card = file->private_data; | |
2569 | assert (card != NULL); | |
2570 | ||
2571 | rc = via_syscall_down (card, nonblock); | |
2572 | if (rc) goto out; | |
2573 | ||
2574 | if (card->ch_out.is_mapped) { | |
2575 | rc = -ENXIO; | |
2576 | goto out_up; | |
2577 | } | |
2578 | ||
2579 | via_chan_set_buffering(card, &card->ch_out, -1); | |
2580 | rc = via_chan_buffer_init (card, &card->ch_out); | |
2581 | ||
2582 | if (rc) | |
2583 | goto out_up; | |
2584 | ||
2585 | rc = via_dsp_do_write (card, buffer, count, nonblock); | |
2586 | ||
2587 | out_up: | |
2588 | up (&card->syscall_sem); | |
2589 | out: | |
2590 | DPRINTK ("EXIT, returning %ld\n",(long) rc); | |
2591 | return rc; | |
2592 | } | |
2593 | ||
2594 | ||
2595 | static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait) | |
2596 | { | |
2597 | struct via_info *card; | |
2598 | struct via_channel *chan; | |
2599 | unsigned int mask = 0; | |
2600 | ||
2601 | DPRINTK ("ENTER\n"); | |
2602 | ||
2603 | assert (file != NULL); | |
2604 | card = file->private_data; | |
2605 | assert (card != NULL); | |
2606 | ||
2607 | if (file->f_mode & FMODE_READ) { | |
2608 | chan = &card->ch_in; | |
2609 | if (sg_active (chan->iobase)) | |
2610 | poll_wait(file, &chan->wait, wait); | |
2611 | if (atomic_read (&chan->n_frags) > 0) | |
2612 | mask |= POLLIN | POLLRDNORM; | |
2613 | } | |
2614 | ||
2615 | if (file->f_mode & FMODE_WRITE) { | |
2616 | chan = &card->ch_out; | |
2617 | if (sg_active (chan->iobase)) | |
2618 | poll_wait(file, &chan->wait, wait); | |
2619 | if (atomic_read (&chan->n_frags) > 0) | |
2620 | mask |= POLLOUT | POLLWRNORM; | |
2621 | } | |
2622 | ||
2623 | DPRINTK ("EXIT, returning %u\n", mask); | |
2624 | return mask; | |
2625 | } | |
2626 | ||
2627 | ||
2628 | /** | |
2629 | * via_dsp_drain_playback - sleep until all playback samples are flushed | |
2630 | * @card: Private info for specified board | |
2631 | * @chan: Channel to drain | |
2632 | * @nonblock: boolean, non-zero if O_NONBLOCK is set | |
2633 | * | |
2634 | * Sleeps until all playback has been flushed to the audio | |
2635 | * hardware. | |
2636 | * | |
2637 | * Locking: inside card->syscall_sem | |
2638 | */ | |
2639 | ||
2640 | static int via_dsp_drain_playback (struct via_info *card, | |
2641 | struct via_channel *chan, int nonblock) | |
2642 | { | |
2643 | DECLARE_WAITQUEUE(wait, current); | |
2644 | int ret = 0; | |
2645 | ||
2646 | DPRINTK ("ENTER, nonblock = %d\n", nonblock); | |
2647 | ||
2648 | if (chan->slop_len > 0) | |
2649 | via_chan_flush_frag (chan); | |
2650 | ||
2651 | if (atomic_read (&chan->n_frags) == chan->frag_number) | |
2652 | goto out; | |
2653 | ||
2654 | via_chan_maybe_start (chan); | |
2655 | ||
2656 | add_wait_queue(&chan->wait, &wait); | |
2657 | for (;;) { | |
2658 | DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number); | |
2659 | __set_current_state(TASK_INTERRUPTIBLE); | |
2660 | if (atomic_read (&chan->n_frags) >= chan->frag_number) | |
2661 | break; | |
2662 | ||
2663 | if (nonblock) { | |
2664 | DPRINTK ("EXIT, returning -EAGAIN\n"); | |
2665 | ret = -EAGAIN; | |
2666 | break; | |
2667 | } | |
2668 | ||
2669 | #ifdef VIA_DEBUG | |
2670 | { | |
2671 | u8 r40,r41,r42,r43,r44,r48; | |
2672 | pci_read_config_byte (card->pdev, 0x40, &r40); | |
2673 | pci_read_config_byte (card->pdev, 0x41, &r41); | |
2674 | pci_read_config_byte (card->pdev, 0x42, &r42); | |
2675 | pci_read_config_byte (card->pdev, 0x43, &r43); | |
2676 | pci_read_config_byte (card->pdev, 0x44, &r44); | |
2677 | pci_read_config_byte (card->pdev, 0x48, &r48); | |
2678 | DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n", | |
2679 | r40,r41,r42,r43,r44,r48); | |
2680 | ||
2681 | DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n", | |
2682 | inb (card->baseaddr + 0x00), | |
2683 | inb (card->baseaddr + 0x01), | |
2684 | inb (card->baseaddr + 0x02), | |
2685 | inl (card->baseaddr + 0x04), | |
2686 | inl (card->baseaddr + 0x0C), | |
2687 | inl (card->baseaddr + 0x80), | |
2688 | inl (card->baseaddr + 0x84)); | |
2689 | } | |
2690 | ||
2691 | if (!chan->is_active) | |
2692 | printk (KERN_ERR "sleeping but not active\n"); | |
2693 | #endif | |
2694 | ||
2695 | up(&card->syscall_sem); | |
2696 | ||
2697 | DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags)); | |
2698 | schedule(); | |
2699 | ||
2700 | if ((ret = via_syscall_down (card, nonblock))) | |
2701 | break; | |
2702 | ||
2703 | if (signal_pending (current)) { | |
2704 | DPRINTK ("EXIT, returning -ERESTARTSYS\n"); | |
2705 | ret = -ERESTARTSYS; | |
2706 | break; | |
2707 | } | |
2708 | } | |
2709 | set_current_state(TASK_RUNNING); | |
2710 | remove_wait_queue(&chan->wait, &wait); | |
2711 | ||
2712 | #ifdef VIA_DEBUG | |
2713 | { | |
2714 | u8 r40,r41,r42,r43,r44,r48; | |
2715 | pci_read_config_byte (card->pdev, 0x40, &r40); | |
2716 | pci_read_config_byte (card->pdev, 0x41, &r41); | |
2717 | pci_read_config_byte (card->pdev, 0x42, &r42); | |
2718 | pci_read_config_byte (card->pdev, 0x43, &r43); | |
2719 | pci_read_config_byte (card->pdev, 0x44, &r44); | |
2720 | pci_read_config_byte (card->pdev, 0x48, &r48); | |
2721 | DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n", | |
2722 | r40,r41,r42,r43,r44,r48); | |
2723 | ||
2724 | DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n", | |
2725 | inb (card->baseaddr + 0x00), | |
2726 | inb (card->baseaddr + 0x01), | |
2727 | inb (card->baseaddr + 0x02), | |
2728 | inl (card->baseaddr + 0x04), | |
2729 | inl (card->baseaddr + 0x0C), | |
2730 | inl (card->baseaddr + 0x80), | |
2731 | inl (card->baseaddr + 0x84)); | |
2732 | ||
2733 | DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags)); | |
2734 | } | |
2735 | #endif | |
2736 | ||
2737 | out: | |
2738 | DPRINTK ("EXIT, returning %d\n", ret); | |
2739 | return ret; | |
2740 | } | |
2741 | ||
2742 | ||
2743 | /** | |
2744 | * via_dsp_ioctl_space - get information about channel buffering | |
2745 | * @card: Private info for specified board | |
2746 | * @chan: pointer to channel-specific info | |
2747 | * @arg: user buffer for returned information | |
2748 | * | |
2749 | * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE. | |
2750 | * | |
2751 | * Locking: inside card->syscall_sem | |
2752 | */ | |
2753 | ||
2754 | static int via_dsp_ioctl_space (struct via_info *card, | |
2755 | struct via_channel *chan, | |
2756 | void __user *arg) | |
2757 | { | |
2758 | audio_buf_info info; | |
2759 | ||
2760 | via_chan_set_buffering(card, chan, -1); | |
2761 | ||
2762 | info.fragstotal = chan->frag_number; | |
2763 | info.fragsize = chan->frag_size; | |
2764 | ||
2765 | /* number of full fragments we can read/write without blocking */ | |
2766 | info.fragments = atomic_read (&chan->n_frags); | |
2767 | ||
2768 | if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0)) | |
2769 | info.fragments--; | |
2770 | ||
2771 | /* number of bytes that can be read or written immediately | |
2772 | * without blocking. | |
2773 | */ | |
2774 | info.bytes = (info.fragments * chan->frag_size); | |
2775 | if (chan->slop_len % chan->frag_size > 0) | |
2776 | info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size); | |
2777 | ||
2778 | DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n", | |
2779 | info.fragstotal, | |
2780 | info.fragsize, | |
2781 | info.fragments, | |
2782 | info.bytes); | |
2783 | ||
2784 | return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0; | |
2785 | } | |
2786 | ||
2787 | ||
2788 | /** | |
2789 | * via_dsp_ioctl_ptr - get information about hardware buffer ptr | |
2790 | * @card: Private info for specified board | |
2791 | * @chan: pointer to channel-specific info | |
2792 | * @arg: user buffer for returned information | |
2793 | * | |
2794 | * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR. | |
2795 | * | |
2796 | * Locking: inside card->syscall_sem | |
2797 | */ | |
2798 | ||
2799 | static int via_dsp_ioctl_ptr (struct via_info *card, | |
2800 | struct via_channel *chan, | |
2801 | void __user *arg) | |
2802 | { | |
2803 | count_info info; | |
2804 | ||
2805 | spin_lock_irq (&card->lock); | |
2806 | ||
2807 | info.bytes = chan->bytes; | |
2808 | info.blocks = chan->n_irqs; | |
2809 | chan->n_irqs = 0; | |
2810 | ||
2811 | spin_unlock_irq (&card->lock); | |
2812 | ||
2813 | if (chan->is_active) { | |
2814 | unsigned long extra; | |
2815 | info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size; | |
2816 | extra = chan->frag_size - via_sg_offset(chan); | |
2817 | info.ptr += extra; | |
2818 | info.bytes += extra; | |
2819 | } else { | |
2820 | info.ptr = 0; | |
2821 | } | |
2822 | ||
2823 | DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n", | |
2824 | info.bytes, | |
2825 | info.blocks, | |
2826 | info.ptr); | |
2827 | ||
2828 | return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0; | |
2829 | } | |
2830 | ||
2831 | ||
2832 | static int via_dsp_ioctl_trigger (struct via_channel *chan, int val) | |
2833 | { | |
2834 | int enable, do_something; | |
2835 | ||
2836 | if (chan->is_record) | |
2837 | enable = (val & PCM_ENABLE_INPUT); | |
2838 | else | |
2839 | enable = (val & PCM_ENABLE_OUTPUT); | |
2840 | ||
2841 | if (!chan->is_enabled && enable) { | |
2842 | do_something = 1; | |
2843 | } else if (chan->is_enabled && !enable) { | |
2844 | do_something = -1; | |
2845 | } else { | |
2846 | do_something = 0; | |
2847 | } | |
2848 | ||
2849 | DPRINTK ("enable=%d, do_something=%d\n", | |
2850 | enable, do_something); | |
2851 | ||
2852 | if (chan->is_active && do_something) | |
2853 | return -EINVAL; | |
2854 | ||
2855 | if (do_something == 1) { | |
2856 | chan->is_enabled = 1; | |
2857 | via_chan_maybe_start (chan); | |
2858 | DPRINTK ("Triggering input\n"); | |
2859 | } | |
2860 | ||
2861 | else if (do_something == -1) { | |
2862 | chan->is_enabled = 0; | |
2863 | DPRINTK ("Setup input trigger\n"); | |
2864 | } | |
2865 | ||
2866 | return 0; | |
2867 | } | |
2868 | ||
2869 | ||
2870 | static int via_dsp_ioctl (struct inode *inode, struct file *file, | |
2871 | unsigned int cmd, unsigned long arg) | |
2872 | { | |
2873 | int rc, rd=0, wr=0, val=0; | |
2874 | struct via_info *card; | |
2875 | struct via_channel *chan; | |
2876 | int nonblock = (file->f_flags & O_NONBLOCK); | |
2877 | int __user *ip = (int __user *)arg; | |
2878 | void __user *p = (void __user *)arg; | |
2879 | ||
2880 | assert (file != NULL); | |
2881 | card = file->private_data; | |
2882 | assert (card != NULL); | |
2883 | ||
2884 | if (file->f_mode & FMODE_WRITE) | |
2885 | wr = 1; | |
2886 | if (file->f_mode & FMODE_READ) | |
2887 | rd = 1; | |
2888 | ||
2889 | rc = via_syscall_down (card, nonblock); | |
2890 | if (rc) | |
2891 | return rc; | |
2892 | rc = -EINVAL; | |
2893 | ||
2894 | switch (cmd) { | |
2895 | ||
2896 | /* OSS API version. XXX unverified */ | |
2897 | case OSS_GETVERSION: | |
2898 | DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n"); | |
2899 | rc = put_user (SOUND_VERSION, ip); | |
2900 | break; | |
2901 | ||
2902 | /* list of supported PCM data formats */ | |
2903 | case SNDCTL_DSP_GETFMTS: | |
2904 | DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n"); | |
2905 | rc = put_user (AFMT_U8 | AFMT_S16_LE, ip); | |
2906 | break; | |
2907 | ||
2908 | /* query or set current channel's PCM data format */ | |
2909 | case SNDCTL_DSP_SETFMT: | |
2910 | if (get_user(val, ip)) { | |
2911 | rc = -EFAULT; | |
2912 | break; | |
2913 | } | |
2914 | DPRINTK ("DSP_SETFMT, val==%d\n", val); | |
2915 | if (val != AFMT_QUERY) { | |
2916 | rc = 0; | |
2917 | ||
2918 | if (rd) | |
2919 | rc = via_chan_set_fmt (card, &card->ch_in, val); | |
2920 | ||
2921 | if (rc >= 0 && wr) | |
2922 | rc = via_chan_set_fmt (card, &card->ch_out, val); | |
2923 | ||
2924 | if (rc < 0) | |
2925 | break; | |
2926 | ||
2927 | val = rc; | |
2928 | } else { | |
2929 | if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) || | |
2930 | (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT))) | |
2931 | val = AFMT_S16_LE; | |
2932 | else | |
2933 | val = AFMT_U8; | |
2934 | } | |
2935 | DPRINTK ("SETFMT EXIT, returning %d\n", val); | |
2936 | rc = put_user (val, ip); | |
2937 | break; | |
2938 | ||
2939 | /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */ | |
2940 | case SNDCTL_DSP_CHANNELS: | |
2941 | if (get_user(val, ip)) { | |
2942 | rc = -EFAULT; | |
2943 | break; | |
2944 | } | |
2945 | DPRINTK ("DSP_CHANNELS, val==%d\n", val); | |
2946 | if (val != 0) { | |
2947 | rc = 0; | |
2948 | ||
2949 | if (rd) | |
2950 | rc = via_chan_set_stereo (card, &card->ch_in, val); | |
2951 | ||
2952 | if (rc >= 0 && wr) | |
2953 | rc = via_chan_set_stereo (card, &card->ch_out, val); | |
2954 | ||
2955 | if (rc < 0) | |
2956 | break; | |
2957 | ||
2958 | val = rc; | |
2959 | } else { | |
2960 | if (rd) | |
2961 | val = card->ch_in.channels; | |
2962 | else | |
2963 | val = card->ch_out.channels; | |
2964 | } | |
2965 | DPRINTK ("CHANNELS EXIT, returning %d\n", val); | |
2966 | rc = put_user (val, ip); | |
2967 | break; | |
2968 | ||
2969 | /* enable (val is not zero) or disable (val == 0) stereo */ | |
2970 | case SNDCTL_DSP_STEREO: | |
2971 | if (get_user(val, ip)) { | |
2972 | rc = -EFAULT; | |
2973 | break; | |
2974 | } | |
2975 | DPRINTK ("DSP_STEREO, val==%d\n", val); | |
2976 | rc = 0; | |
2977 | ||
2978 | if (rd) | |
2979 | rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1); | |
2980 | if (rc >= 0 && wr) | |
2981 | rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1); | |
2982 | ||
2983 | if (rc < 0) | |
2984 | break; | |
2985 | ||
2986 | val = rc - 1; | |
2987 | ||
2988 | DPRINTK ("STEREO EXIT, returning %d\n", val); | |
2989 | rc = put_user(val, ip); | |
2990 | break; | |
2991 | ||
2992 | /* query or set sampling rate */ | |
2993 | case SNDCTL_DSP_SPEED: | |
2994 | if (get_user(val, ip)) { | |
2995 | rc = -EFAULT; | |
2996 | break; | |
2997 | } | |
2998 | DPRINTK ("DSP_SPEED, val==%d\n", val); | |
2999 | if (val < 0) { | |
3000 | rc = -EINVAL; | |
3001 | break; | |
3002 | } | |
3003 | if (val > 0) { | |
3004 | rc = 0; | |
3005 | ||
3006 | if (rd) | |
3007 | rc = via_chan_set_speed (card, &card->ch_in, val); | |
3008 | if (rc >= 0 && wr) | |
3009 | rc = via_chan_set_speed (card, &card->ch_out, val); | |
3010 | ||
3011 | if (rc < 0) | |
3012 | break; | |
3013 | ||
3014 | val = rc; | |
3015 | } else { | |
3016 | if (rd) | |
3017 | val = card->ch_in.rate; | |
3018 | else if (wr) | |
3019 | val = card->ch_out.rate; | |
3020 | else | |
3021 | val = 0; | |
3022 | } | |
3023 | DPRINTK ("SPEED EXIT, returning %d\n", val); | |
3024 | rc = put_user (val, ip); | |
3025 | break; | |
3026 | ||
3027 | /* wait until all buffers have been played, and then stop device */ | |
3028 | case SNDCTL_DSP_SYNC: | |
3029 | DPRINTK ("DSP_SYNC\n"); | |
3030 | rc = 0; | |
3031 | if (wr) { | |
3032 | DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n"); | |
3033 | rc = via_dsp_drain_playback (card, &card->ch_out, nonblock); | |
3034 | } | |
3035 | break; | |
3036 | ||
3037 | /* stop recording/playback immediately */ | |
3038 | case SNDCTL_DSP_RESET: | |
3039 | DPRINTK ("DSP_RESET\n"); | |
3040 | if (rd) { | |
3041 | via_chan_clear (card, &card->ch_in); | |
3042 | card->ch_in.frag_number = 0; | |
3043 | card->ch_in.frag_size = 0; | |
3044 | atomic_set(&card->ch_in.n_frags, 0); | |
3045 | } | |
3046 | ||
3047 | if (wr) { | |
3048 | via_chan_clear (card, &card->ch_out); | |
3049 | card->ch_out.frag_number = 0; | |
3050 | card->ch_out.frag_size = 0; | |
3051 | atomic_set(&card->ch_out.n_frags, 0); | |
3052 | } | |
3053 | ||
3054 | rc = 0; | |
3055 | break; | |
3056 | ||
3057 | case SNDCTL_DSP_NONBLOCK: | |
3058 | file->f_flags |= O_NONBLOCK; | |
3059 | rc = 0; | |
3060 | break; | |
3061 | ||
3062 | /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */ | |
3063 | case SNDCTL_DSP_GETCAPS: | |
3064 | DPRINTK ("DSP_GETCAPS\n"); | |
3065 | rc = put_user(VIA_DSP_CAP, ip); | |
3066 | break; | |
3067 | ||
3068 | /* obtain buffer fragment size */ | |
3069 | case SNDCTL_DSP_GETBLKSIZE: | |
3070 | DPRINTK ("DSP_GETBLKSIZE\n"); | |
3071 | ||
3072 | if (rd) { | |
3073 | via_chan_set_buffering(card, &card->ch_in, -1); | |
3074 | rc = put_user(card->ch_in.frag_size, ip); | |
3075 | } else if (wr) { | |
3076 | via_chan_set_buffering(card, &card->ch_out, -1); | |
3077 | rc = put_user(card->ch_out.frag_size, ip); | |
3078 | } | |
3079 | break; | |
3080 | ||
3081 | /* obtain information about input buffering */ | |
3082 | case SNDCTL_DSP_GETISPACE: | |
3083 | DPRINTK ("DSP_GETISPACE\n"); | |
3084 | if (rd) | |
3085 | rc = via_dsp_ioctl_space (card, &card->ch_in, p); | |
3086 | break; | |
3087 | ||
3088 | /* obtain information about output buffering */ | |
3089 | case SNDCTL_DSP_GETOSPACE: | |
3090 | DPRINTK ("DSP_GETOSPACE\n"); | |
3091 | if (wr) | |
3092 | rc = via_dsp_ioctl_space (card, &card->ch_out, p); | |
3093 | break; | |
3094 | ||
3095 | /* obtain information about input hardware pointer */ | |
3096 | case SNDCTL_DSP_GETIPTR: | |
3097 | DPRINTK ("DSP_GETIPTR\n"); | |
3098 | if (rd) | |
3099 | rc = via_dsp_ioctl_ptr (card, &card->ch_in, p); | |
3100 | break; | |
3101 | ||
3102 | /* obtain information about output hardware pointer */ | |
3103 | case SNDCTL_DSP_GETOPTR: | |
3104 | DPRINTK ("DSP_GETOPTR\n"); | |
3105 | if (wr) | |
3106 | rc = via_dsp_ioctl_ptr (card, &card->ch_out, p); | |
3107 | break; | |
3108 | ||
3109 | /* return number of bytes remaining to be played by DMA engine */ | |
3110 | case SNDCTL_DSP_GETODELAY: | |
3111 | { | |
3112 | DPRINTK ("DSP_GETODELAY\n"); | |
3113 | ||
3114 | chan = &card->ch_out; | |
3115 | ||
3116 | if (!wr) | |
3117 | break; | |
3118 | ||
3119 | if (chan->is_active) { | |
3120 | ||
3121 | val = chan->frag_number - atomic_read (&chan->n_frags); | |
3122 | ||
3123 | assert(val >= 0); | |
3124 | ||
3125 | if (val > 0) { | |
3126 | val *= chan->frag_size; | |
3127 | val -= chan->frag_size - via_sg_offset(chan); | |
3128 | } | |
3129 | val += chan->slop_len % chan->frag_size; | |
3130 | } else | |
3131 | val = 0; | |
3132 | ||
3133 | assert (val <= (chan->frag_size * chan->frag_number)); | |
3134 | ||
3135 | DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val); | |
3136 | rc = put_user (val, ip); | |
3137 | break; | |
3138 | } | |
3139 | ||
3140 | /* handle the quick-start of a channel, | |
3141 | * or the notification that a quick-start will | |
3142 | * occur in the future | |
3143 | */ | |
3144 | case SNDCTL_DSP_SETTRIGGER: | |
3145 | if (get_user(val, ip)) { | |
3146 | rc = -EFAULT; | |
3147 | break; | |
3148 | } | |
3149 | DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n", | |
3150 | rd, wr, card->ch_in.is_active, card->ch_out.is_active, | |
3151 | card->ch_in.is_enabled, card->ch_out.is_enabled); | |
3152 | ||
3153 | rc = 0; | |
3154 | ||
3155 | if (rd) | |
3156 | rc = via_dsp_ioctl_trigger (&card->ch_in, val); | |
3157 | ||
3158 | if (!rc && wr) | |
3159 | rc = via_dsp_ioctl_trigger (&card->ch_out, val); | |
3160 | ||
3161 | break; | |
3162 | ||
3163 | case SNDCTL_DSP_GETTRIGGER: | |
3164 | val = 0; | |
3165 | if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled) | |
3166 | val |= PCM_ENABLE_INPUT; | |
3167 | if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled) | |
3168 | val |= PCM_ENABLE_OUTPUT; | |
3169 | rc = put_user(val, ip); | |
3170 | break; | |
3171 | ||
3172 | /* Enable full duplex. Since we do this as soon as we are opened | |
3173 | * with O_RDWR, this is mainly a no-op that always returns success. | |
3174 | */ | |
3175 | case SNDCTL_DSP_SETDUPLEX: | |
3176 | DPRINTK ("DSP_SETDUPLEX\n"); | |
3177 | if (!rd || !wr) | |
3178 | break; | |
3179 | rc = 0; | |
3180 | break; | |
3181 | ||
3182 | /* set fragment size. implemented as a successful no-op for now */ | |
3183 | case SNDCTL_DSP_SETFRAGMENT: | |
3184 | if (get_user(val, ip)) { | |
3185 | rc = -EFAULT; | |
3186 | break; | |
3187 | } | |
3188 | DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val); | |
3189 | ||
3190 | if (rd) | |
3191 | rc = via_chan_set_buffering(card, &card->ch_in, val); | |
3192 | ||
3193 | if (wr) | |
3194 | rc = via_chan_set_buffering(card, &card->ch_out, val); | |
3195 | ||
3196 | DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n", | |
3197 | val & 0xFFFF, | |
3198 | val & 0xFFFF, | |
3199 | (val >> 16) & 0xFFFF, | |
3200 | (val >> 16) & 0xFFFF); | |
3201 | ||
3202 | rc = 0; | |
3203 | break; | |
3204 | ||
3205 | /* inform device of an upcoming pause in input (or output). */ | |
3206 | case SNDCTL_DSP_POST: | |
3207 | DPRINTK ("DSP_POST\n"); | |
3208 | if (wr) { | |
3209 | if (card->ch_out.slop_len > 0) | |
3210 | via_chan_flush_frag (&card->ch_out); | |
3211 | via_chan_maybe_start (&card->ch_out); | |
3212 | } | |
3213 | ||
3214 | rc = 0; | |
3215 | break; | |
3216 | ||
3217 | /* not implemented */ | |
3218 | default: | |
3219 | DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n", | |
3220 | cmd, p); | |
3221 | break; | |
3222 | } | |
3223 | ||
3224 | up (&card->syscall_sem); | |
3225 | DPRINTK ("EXIT, returning %d\n", rc); | |
3226 | return rc; | |
3227 | } | |
3228 | ||
3229 | ||
3230 | static int via_dsp_open (struct inode *inode, struct file *file) | |
3231 | { | |
3232 | int minor = iminor(inode); | |
3233 | struct via_info *card; | |
3234 | struct pci_dev *pdev = NULL; | |
3235 | struct via_channel *chan; | |
3236 | struct pci_driver *drvr; | |
3237 | int nonblock = (file->f_flags & O_NONBLOCK); | |
3238 | ||
3239 | DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode); | |
3240 | ||
3241 | if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) { | |
3242 | DPRINTK ("EXIT, returning -EINVAL\n"); | |
3243 | return -EINVAL; | |
3244 | } | |
3245 | ||
3246 | card = NULL; | |
3247 | while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) { | |
3248 | drvr = pci_dev_driver (pdev); | |
3249 | if (drvr == &via_driver) { | |
3250 | assert (pci_get_drvdata (pdev) != NULL); | |
3251 | ||
3252 | card = pci_get_drvdata (pdev); | |
3253 | DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n", | |
3254 | card->dev_dsp, minor, | |
3255 | (card->dev_dsp ^ minor) & ~0xf); | |
3256 | ||
3257 | if (((card->dev_dsp ^ minor) & ~0xf) == 0) | |
3258 | goto match; | |
3259 | } | |
3260 | } | |
3261 | ||
3262 | DPRINTK ("no matching %s found\n", card ? "minor" : "driver"); | |
3263 | return -ENODEV; | |
3264 | ||
3265 | match: | |
3266 | if (nonblock) { | |
3267 | if (down_trylock (&card->open_sem)) { | |
3268 | DPRINTK ("EXIT, returning -EAGAIN\n"); | |
3269 | return -EAGAIN; | |
3270 | } | |
3271 | } else { | |
3272 | if (down_interruptible (&card->open_sem)) { | |
3273 | DPRINTK ("EXIT, returning -ERESTARTSYS\n"); | |
3274 | return -ERESTARTSYS; | |
3275 | } | |
3276 | } | |
3277 | ||
3278 | file->private_data = card; | |
3279 | DPRINTK ("file->f_mode == 0x%x\n", file->f_mode); | |
3280 | ||
3281 | /* handle input from analog source */ | |
3282 | if (file->f_mode & FMODE_READ) { | |
3283 | chan = &card->ch_in; | |
3284 | ||
3285 | via_chan_init (card, chan); | |
3286 | ||
3287 | /* why is this forced to 16-bit stereo in all drivers? */ | |
3288 | chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO; | |
3289 | chan->channels = 2; | |
3290 | ||
3291 | // TO DO - use FIFO: via_capture_fifo(card, 1); | |
3292 | via_chan_pcm_fmt (chan, 0); | |
3293 | via_set_rate (card->ac97, chan, 44100); | |
3294 | } | |
3295 | ||
3296 | /* handle output to analog source */ | |
3297 | if (file->f_mode & FMODE_WRITE) { | |
3298 | chan = &card->ch_out; | |
3299 | ||
3300 | via_chan_init (card, chan); | |
3301 | ||
3302 | if (file->f_mode & FMODE_READ) { | |
3303 | /* if in duplex mode make the recording and playback channels | |
3304 | have the same settings */ | |
3305 | chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO; | |
3306 | chan->channels = 2; | |
3307 | via_chan_pcm_fmt (chan, 0); | |
3308 | via_set_rate (card->ac97, chan, 44100); | |
3309 | } else { | |
3310 | if ((minor & 0xf) == SND_DEV_DSP16) { | |
3311 | chan->pcm_fmt = VIA_PCM_FMT_16BIT; | |
3312 | via_chan_pcm_fmt (chan, 0); | |
3313 | via_set_rate (card->ac97, chan, 44100); | |
3314 | } else { | |
3315 | via_chan_pcm_fmt (chan, 1); | |
3316 | via_set_rate (card->ac97, chan, 8000); | |
3317 | } | |
3318 | } | |
3319 | } | |
3320 | ||
3321 | DPRINTK ("EXIT, returning 0\n"); | |
3322 | return nonseekable_open(inode, file); | |
3323 | } | |
3324 | ||
3325 | ||
3326 | static int via_dsp_release(struct inode *inode, struct file *file) | |
3327 | { | |
3328 | struct via_info *card; | |
3329 | int nonblock = (file->f_flags & O_NONBLOCK); | |
3330 | int rc; | |
3331 | ||
3332 | DPRINTK ("ENTER\n"); | |
3333 | ||
3334 | assert (file != NULL); | |
3335 | card = file->private_data; | |
3336 | assert (card != NULL); | |
3337 | ||
3338 | rc = via_syscall_down (card, nonblock); | |
3339 | if (rc) { | |
3340 | DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc); | |
3341 | return rc; | |
3342 | } | |
3343 | ||
3344 | if (file->f_mode & FMODE_WRITE) { | |
3345 | rc = via_dsp_drain_playback (card, &card->ch_out, nonblock); | |
3346 | if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */ | |
3347 | printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc); | |
3348 | ||
3349 | via_chan_free (card, &card->ch_out); | |
3350 | via_chan_buffer_free(card, &card->ch_out); | |
3351 | } | |
3352 | ||
3353 | if (file->f_mode & FMODE_READ) { | |
3354 | via_chan_free (card, &card->ch_in); | |
3355 | via_chan_buffer_free (card, &card->ch_in); | |
3356 | } | |
3357 | ||
3358 | up (&card->syscall_sem); | |
3359 | up (&card->open_sem); | |
3360 | ||
3361 | DPRINTK ("EXIT, returning 0\n"); | |
3362 | return 0; | |
3363 | } | |
3364 | ||
3365 | ||
3366 | /**************************************************************** | |
3367 | * | |
3368 | * Chip setup and kernel registration | |
3369 | * | |
3370 | * | |
3371 | */ | |
3372 | ||
3373 | static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id) | |
3374 | { | |
3375 | #ifdef CONFIG_MIDI_VIA82CXXX | |
3376 | u8 r42; | |
3377 | #endif | |
3378 | int rc; | |
3379 | struct via_info *card; | |
3380 | static int printed_version; | |
3381 | ||
3382 | DPRINTK ("ENTER\n"); | |
3383 | ||
3384 | if (printed_version++ == 0) | |
3385 | printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n"); | |
3386 | ||
3387 | rc = pci_enable_device (pdev); | |
3388 | if (rc) | |
3389 | goto err_out; | |
3390 | ||
3391 | rc = pci_request_regions (pdev, "via82cxxx_audio"); | |
3392 | if (rc) | |
3393 | goto err_out_disable; | |
3394 | ||
3e90928d | 3395 | rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK); |
1da177e4 LT |
3396 | if (rc) |
3397 | goto err_out_res; | |
3e90928d | 3398 | rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); |
1da177e4 LT |
3399 | if (rc) |
3400 | goto err_out_res; | |
3401 | ||
3402 | card = kmalloc (sizeof (*card), GFP_KERNEL); | |
3403 | if (!card) { | |
3404 | printk (KERN_ERR PFX "out of memory, aborting\n"); | |
3405 | rc = -ENOMEM; | |
3406 | goto err_out_res; | |
3407 | } | |
3408 | ||
3409 | pci_set_drvdata (pdev, card); | |
3410 | ||
3411 | memset (card, 0, sizeof (*card)); | |
3412 | card->pdev = pdev; | |
3413 | card->baseaddr = pci_resource_start (pdev, 0); | |
3414 | card->card_num = via_num_cards++; | |
3415 | spin_lock_init (&card->lock); | |
3416 | spin_lock_init (&card->ac97_lock); | |
3417 | init_MUTEX (&card->syscall_sem); | |
3418 | init_MUTEX (&card->open_sem); | |
3419 | ||
3420 | /* we must init these now, in case the intr handler needs them */ | |
3421 | via_chan_init_defaults (card, &card->ch_out); | |
3422 | via_chan_init_defaults (card, &card->ch_in); | |
3423 | via_chan_init_defaults (card, &card->ch_fm); | |
3424 | ||
3425 | /* if BAR 2 is present, chip is Rev H or later, | |
3426 | * which means it has a few extra features */ | |
3427 | if (pci_resource_start (pdev, 2) > 0) | |
3428 | card->rev_h = 1; | |
3429 | ||
3430 | /* Overkill for now, but more flexible done right */ | |
3431 | ||
3432 | card->intmask = id->driver_data; | |
3433 | card->legacy = !card->intmask; | |
3434 | card->sixchannel = id->driver_data; | |
3435 | ||
3436 | if(card->sixchannel) | |
3437 | printk(KERN_INFO PFX "Six channel audio available\n"); | |
3438 | if (pdev->irq < 1) { | |
3439 | printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq); | |
3440 | rc = -ENODEV; | |
3441 | goto err_out_kfree; | |
3442 | } | |
3443 | ||
3444 | if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) { | |
3445 | printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n"); | |
3446 | rc = -ENODEV; | |
3447 | goto err_out_kfree; | |
3448 | } | |
3449 | ||
3450 | pci_set_master(pdev); | |
3451 | ||
3452 | /* | |
3453 | * init AC97 mixer and codec | |
3454 | */ | |
3455 | rc = via_ac97_init (card); | |
3456 | if (rc) { | |
3457 | printk (KERN_ERR PFX "AC97 init failed, aborting\n"); | |
3458 | goto err_out_kfree; | |
3459 | } | |
3460 | ||
3461 | /* | |
3462 | * init DSP device | |
3463 | */ | |
3464 | rc = via_dsp_init (card); | |
3465 | if (rc) { | |
3466 | printk (KERN_ERR PFX "DSP device init failed, aborting\n"); | |
3467 | goto err_out_have_mixer; | |
3468 | } | |
3469 | ||
3470 | /* | |
3471 | * init and turn on interrupts, as the last thing we do | |
3472 | */ | |
3473 | rc = via_interrupt_init (card); | |
3474 | if (rc) { | |
3475 | printk (KERN_ERR PFX "interrupt init failed, aborting\n"); | |
3476 | goto err_out_have_dsp; | |
3477 | } | |
3478 | ||
3479 | printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n", | |
3480 | card->card_num + 1, card->baseaddr, pdev->irq); | |
3481 | ||
3482 | #ifdef CONFIG_MIDI_VIA82CXXX | |
3483 | /* Disable by default */ | |
3484 | card->midi_info.io_base = 0; | |
3485 | ||
3486 | if(card->legacy) | |
3487 | { | |
3488 | pci_read_config_byte (pdev, 0x42, &r42); | |
3489 | /* Disable MIDI interrupt */ | |
3490 | pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK); | |
3491 | if (r42 & VIA_CR42_MIDI_ENABLE) | |
3492 | { | |
3493 | if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */ | |
3494 | card->midi_info.io_base = pci_resource_start (pdev, 2); | |
3495 | else /* Address selected by byte 0x43 */ | |
3496 | { | |
3497 | u8 r43; | |
3498 | pci_read_config_byte (pdev, 0x43, &r43); | |
3499 | card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2); | |
3500 | } | |
3501 | ||
3502 | card->midi_info.irq = -pdev->irq; | |
3503 | if (probe_uart401(& card->midi_info, THIS_MODULE)) | |
3504 | { | |
3505 | card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc; | |
3506 | pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK); | |
3507 | printk("Enabled Via MIDI\n"); | |
3508 | } | |
3509 | } | |
3510 | } | |
3511 | #endif | |
3512 | ||
3513 | DPRINTK ("EXIT, returning 0\n"); | |
3514 | return 0; | |
3515 | ||
3516 | err_out_have_dsp: | |
3517 | via_dsp_cleanup (card); | |
3518 | ||
3519 | err_out_have_mixer: | |
3520 | via_ac97_cleanup (card); | |
3521 | ||
3522 | err_out_kfree: | |
3523 | #ifndef VIA_NDEBUG | |
3524 | memset (card, 0xAB, sizeof (*card)); /* poison memory */ | |
3525 | #endif | |
3526 | kfree (card); | |
3527 | ||
3528 | err_out_res: | |
3529 | pci_release_regions (pdev); | |
3530 | ||
3531 | err_out_disable: | |
3532 | pci_disable_device (pdev); | |
3533 | ||
3534 | err_out: | |
3535 | pci_set_drvdata (pdev, NULL); | |
3536 | DPRINTK ("EXIT - returning %d\n", rc); | |
3537 | return rc; | |
3538 | } | |
3539 | ||
3540 | ||
3541 | static void __devexit via_remove_one (struct pci_dev *pdev) | |
3542 | { | |
3543 | struct via_info *card; | |
3544 | ||
3545 | DPRINTK ("ENTER\n"); | |
3546 | ||
3547 | assert (pdev != NULL); | |
3548 | card = pci_get_drvdata (pdev); | |
3549 | assert (card != NULL); | |
3550 | ||
3551 | #ifdef CONFIG_MIDI_VIA82CXXX | |
3552 | if (card->midi_info.io_base) | |
3553 | unload_uart401(&card->midi_info); | |
3554 | #endif | |
3555 | ||
3556 | free_irq (card->pdev->irq, card); | |
3557 | via_dsp_cleanup (card); | |
3558 | via_ac97_cleanup (card); | |
3559 | ||
3560 | #ifndef VIA_NDEBUG | |
3561 | memset (card, 0xAB, sizeof (*card)); /* poison memory */ | |
3562 | #endif | |
3563 | kfree (card); | |
3564 | ||
3565 | pci_set_drvdata (pdev, NULL); | |
3566 | ||
3567 | pci_release_regions (pdev); | |
3568 | pci_disable_device (pdev); | |
3569 | pci_set_power_state (pdev, 3); /* ...zzzzzz */ | |
3570 | ||
3571 | DPRINTK ("EXIT\n"); | |
3572 | return; | |
3573 | } | |
3574 | ||
3575 | ||
3576 | /**************************************************************** | |
3577 | * | |
3578 | * Driver initialization and cleanup | |
3579 | * | |
3580 | * | |
3581 | */ | |
3582 | ||
3583 | static int __init init_via82cxxx_audio(void) | |
3584 | { | |
3585 | int rc; | |
3586 | ||
3587 | DPRINTK ("ENTER\n"); | |
3588 | ||
3589 | rc = pci_register_driver (&via_driver); | |
3590 | if (rc) { | |
3591 | DPRINTK ("EXIT, returning %d\n", rc); | |
3592 | return rc; | |
3593 | } | |
3594 | ||
3595 | DPRINTK ("EXIT, returning 0\n"); | |
3596 | return 0; | |
3597 | } | |
3598 | ||
3599 | ||
3600 | static void __exit cleanup_via82cxxx_audio(void) | |
3601 | { | |
3602 | DPRINTK ("ENTER\n"); | |
3603 | ||
3604 | pci_unregister_driver (&via_driver); | |
3605 | ||
3606 | DPRINTK ("EXIT\n"); | |
3607 | } | |
3608 | ||
3609 | ||
3610 | module_init(init_via82cxxx_audio); | |
3611 | module_exit(cleanup_via82cxxx_audio); | |
3612 | ||
3613 | MODULE_AUTHOR("Jeff Garzik"); | |
3614 | MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices"); | |
3615 | MODULE_LICENSE("GPL"); | |
3616 |