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