GNU Linux-libre 5.10.217-gnu1
[releases.git] / sound / pci / maestro3.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for ESS Maestro3/Allegro (ES1988) soundcards.
4  * Copyright (c) 2000 by Zach Brown <zab@zabbo.net>
5  *                       Takashi Iwai <tiwai@suse.de>
6  *
7  * Most of the hardware init stuffs are based on maestro3 driver for
8  * OSS/Free by Zach Brown.  Many thanks to Zach!
9  *
10  * ChangeLog:
11  * Aug. 27, 2001
12  *     - Fixed deadlock on capture
13  *     - Added Canyon3D-2 support by Rob Riggs <rob@pangalactic.org>
14  */
15  
16 #define CARD_NAME "ESS Maestro3/Allegro/Canyon3D-2"
17 #define DRIVER_NAME "Maestro3"
18
19 #include <linux/io.h>
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/firmware.h>
29 #include <linux/input.h>
30 #include <sound/core.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/mpu401.h>
35 #include <sound/ac97_codec.h>
36 #include <sound/initval.h>
37 #include <asm/byteorder.h>
38
39 MODULE_AUTHOR("Zach Brown <zab@zabbo.net>, Takashi Iwai <tiwai@suse.de>");
40 MODULE_DESCRIPTION("ESS Maestro3 PCI");
41 MODULE_LICENSE("GPL");
42 MODULE_SUPPORTED_DEVICE("{{ESS,Maestro3 PCI},"
43                 "{ESS,ES1988},"
44                 "{ESS,Allegro PCI},"
45                 "{ESS,Allegro-1 PCI},"
46                 "{ESS,Canyon3D-2/LE PCI}}");
47 /*(DEBLOBBED)*/
48
49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
51 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* all enabled */
52 static bool external_amp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
53 static int amp_gpio[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
54
55 module_param_array(index, int, NULL, 0444);
56 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
57 module_param_array(id, charp, NULL, 0444);
58 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
59 module_param_array(enable, bool, NULL, 0444);
60 MODULE_PARM_DESC(enable, "Enable this soundcard.");
61 module_param_array(external_amp, bool, NULL, 0444);
62 MODULE_PARM_DESC(external_amp, "Enable external amp for " CARD_NAME " soundcard.");
63 module_param_array(amp_gpio, int, NULL, 0444);
64 MODULE_PARM_DESC(amp_gpio, "GPIO pin number for external amp. (default = -1)");
65
66 #define MAX_PLAYBACKS   2
67 #define MAX_CAPTURES    1
68 #define NR_DSPS         (MAX_PLAYBACKS + MAX_CAPTURES)
69
70
71 /*
72  * maestro3 registers
73  */
74
75 /* Allegro PCI configuration registers */
76 #define PCI_LEGACY_AUDIO_CTRL   0x40
77 #define SOUND_BLASTER_ENABLE    0x00000001
78 #define FM_SYNTHESIS_ENABLE     0x00000002
79 #define GAME_PORT_ENABLE        0x00000004
80 #define MPU401_IO_ENABLE        0x00000008
81 #define MPU401_IRQ_ENABLE       0x00000010
82 #define ALIAS_10BIT_IO          0x00000020
83 #define SB_DMA_MASK             0x000000C0
84 #define SB_DMA_0                0x00000040
85 #define SB_DMA_1                0x00000040
86 #define SB_DMA_R                0x00000080
87 #define SB_DMA_3                0x000000C0
88 #define SB_IRQ_MASK             0x00000700
89 #define SB_IRQ_5                0x00000000
90 #define SB_IRQ_7                0x00000100
91 #define SB_IRQ_9                0x00000200
92 #define SB_IRQ_10               0x00000300
93 #define MIDI_IRQ_MASK           0x00003800
94 #define SERIAL_IRQ_ENABLE       0x00004000
95 #define DISABLE_LEGACY          0x00008000
96
97 #define PCI_ALLEGRO_CONFIG      0x50
98 #define SB_ADDR_240             0x00000004
99 #define MPU_ADDR_MASK           0x00000018
100 #define MPU_ADDR_330            0x00000000
101 #define MPU_ADDR_300            0x00000008
102 #define MPU_ADDR_320            0x00000010
103 #define MPU_ADDR_340            0x00000018
104 #define USE_PCI_TIMING          0x00000040
105 #define POSTED_WRITE_ENABLE     0x00000080
106 #define DMA_POLICY_MASK         0x00000700
107 #define DMA_DDMA                0x00000000
108 #define DMA_TDMA                0x00000100
109 #define DMA_PCPCI               0x00000200
110 #define DMA_WBDMA16             0x00000400
111 #define DMA_WBDMA4              0x00000500
112 #define DMA_WBDMA2              0x00000600
113 #define DMA_WBDMA1              0x00000700
114 #define DMA_SAFE_GUARD          0x00000800
115 #define HI_PERF_GP_ENABLE       0x00001000
116 #define PIC_SNOOP_MODE_0        0x00002000
117 #define PIC_SNOOP_MODE_1        0x00004000
118 #define SOUNDBLASTER_IRQ_MASK   0x00008000
119 #define RING_IN_ENABLE          0x00010000
120 #define SPDIF_TEST_MODE         0x00020000
121 #define CLK_MULT_MODE_SELECT_2  0x00040000
122 #define EEPROM_WRITE_ENABLE     0x00080000
123 #define CODEC_DIR_IN            0x00100000
124 #define HV_BUTTON_FROM_GD       0x00200000
125 #define REDUCED_DEBOUNCE        0x00400000
126 #define HV_CTRL_ENABLE          0x00800000
127 #define SPDIF_ENABLE            0x01000000
128 #define CLK_DIV_SELECT          0x06000000
129 #define CLK_DIV_BY_48           0x00000000
130 #define CLK_DIV_BY_49           0x02000000
131 #define CLK_DIV_BY_50           0x04000000
132 #define CLK_DIV_RESERVED        0x06000000
133 #define PM_CTRL_ENABLE          0x08000000
134 #define CLK_MULT_MODE_SELECT    0x30000000
135 #define CLK_MULT_MODE_SHIFT     28
136 #define CLK_MULT_MODE_0         0x00000000
137 #define CLK_MULT_MODE_1         0x10000000
138 #define CLK_MULT_MODE_2         0x20000000
139 #define CLK_MULT_MODE_3         0x30000000
140 #define INT_CLK_SELECT          0x40000000
141 #define INT_CLK_MULT_RESET      0x80000000
142
143 /* M3 */
144 #define INT_CLK_SRC_NOT_PCI     0x00100000
145 #define INT_CLK_MULT_ENABLE     0x80000000
146
147 #define PCI_ACPI_CONTROL        0x54
148 #define PCI_ACPI_D0             0x00000000
149 #define PCI_ACPI_D1             0xB4F70000
150 #define PCI_ACPI_D2             0xB4F7B4F7
151
152 #define PCI_USER_CONFIG         0x58
153 #define EXT_PCI_MASTER_ENABLE   0x00000001
154 #define SPDIF_OUT_SELECT        0x00000002
155 #define TEST_PIN_DIR_CTRL       0x00000004
156 #define AC97_CODEC_TEST         0x00000020
157 #define TRI_STATE_BUFFER        0x00000080
158 #define IN_CLK_12MHZ_SELECT     0x00000100
159 #define MULTI_FUNC_DISABLE      0x00000200
160 #define EXT_MASTER_PAIR_SEL     0x00000400
161 #define PCI_MASTER_SUPPORT      0x00000800
162 #define STOP_CLOCK_ENABLE       0x00001000
163 #define EAPD_DRIVE_ENABLE       0x00002000
164 #define REQ_TRI_STATE_ENABLE    0x00004000
165 #define REQ_LOW_ENABLE          0x00008000
166 #define MIDI_1_ENABLE           0x00010000
167 #define MIDI_2_ENABLE           0x00020000
168 #define SB_AUDIO_SYNC           0x00040000
169 #define HV_CTRL_TEST            0x00100000
170 #define SOUNDBLASTER_TEST       0x00400000
171
172 #define PCI_USER_CONFIG_C       0x5C
173
174 #define PCI_DDMA_CTRL           0x60
175 #define DDMA_ENABLE             0x00000001
176
177
178 /* Allegro registers */
179 #define HOST_INT_CTRL           0x18
180 #define SB_INT_ENABLE           0x0001
181 #define MPU401_INT_ENABLE       0x0002
182 #define ASSP_INT_ENABLE         0x0010
183 #define RING_INT_ENABLE         0x0020
184 #define HV_INT_ENABLE           0x0040
185 #define CLKRUN_GEN_ENABLE       0x0100
186 #define HV_CTRL_TO_PME          0x0400
187 #define SOFTWARE_RESET_ENABLE   0x8000
188
189 /*
190  * should be using the above defines, probably.
191  */
192 #define REGB_ENABLE_RESET               0x01
193 #define REGB_STOP_CLOCK                 0x10
194
195 #define HOST_INT_STATUS         0x1A
196 #define SB_INT_PENDING          0x01
197 #define MPU401_INT_PENDING      0x02
198 #define ASSP_INT_PENDING        0x10
199 #define RING_INT_PENDING        0x20
200 #define HV_INT_PENDING          0x40
201
202 #define HARDWARE_VOL_CTRL       0x1B
203 #define SHADOW_MIX_REG_VOICE    0x1C
204 #define HW_VOL_COUNTER_VOICE    0x1D
205 #define SHADOW_MIX_REG_MASTER   0x1E
206 #define HW_VOL_COUNTER_MASTER   0x1F
207
208 #define CODEC_COMMAND           0x30
209 #define CODEC_READ_B            0x80
210
211 #define CODEC_STATUS            0x30
212 #define CODEC_BUSY_B            0x01
213
214 #define CODEC_DATA              0x32
215
216 #define RING_BUS_CTRL_A         0x36
217 #define RAC_PME_ENABLE          0x0100
218 #define RAC_SDFS_ENABLE         0x0200
219 #define LAC_PME_ENABLE          0x0400
220 #define LAC_SDFS_ENABLE         0x0800
221 #define SERIAL_AC_LINK_ENABLE   0x1000
222 #define IO_SRAM_ENABLE          0x2000
223 #define IIS_INPUT_ENABLE        0x8000
224
225 #define RING_BUS_CTRL_B         0x38
226 #define SECOND_CODEC_ID_MASK    0x0003
227 #define SPDIF_FUNC_ENABLE       0x0010
228 #define SECOND_AC_ENABLE        0x0020
229 #define SB_MODULE_INTF_ENABLE   0x0040
230 #define SSPE_ENABLE             0x0040
231 #define M3I_DOCK_ENABLE         0x0080
232
233 #define SDO_OUT_DEST_CTRL       0x3A
234 #define COMMAND_ADDR_OUT        0x0003
235 #define PCM_LR_OUT_LOCAL        0x0000
236 #define PCM_LR_OUT_REMOTE       0x0004
237 #define PCM_LR_OUT_MUTE         0x0008
238 #define PCM_LR_OUT_BOTH         0x000C
239 #define LINE1_DAC_OUT_LOCAL     0x0000
240 #define LINE1_DAC_OUT_REMOTE    0x0010
241 #define LINE1_DAC_OUT_MUTE      0x0020
242 #define LINE1_DAC_OUT_BOTH      0x0030
243 #define PCM_CLS_OUT_LOCAL       0x0000
244 #define PCM_CLS_OUT_REMOTE      0x0040
245 #define PCM_CLS_OUT_MUTE        0x0080
246 #define PCM_CLS_OUT_BOTH        0x00C0
247 #define PCM_RLF_OUT_LOCAL       0x0000
248 #define PCM_RLF_OUT_REMOTE      0x0100
249 #define PCM_RLF_OUT_MUTE        0x0200
250 #define PCM_RLF_OUT_BOTH        0x0300
251 #define LINE2_DAC_OUT_LOCAL     0x0000
252 #define LINE2_DAC_OUT_REMOTE    0x0400
253 #define LINE2_DAC_OUT_MUTE      0x0800
254 #define LINE2_DAC_OUT_BOTH      0x0C00
255 #define HANDSET_OUT_LOCAL       0x0000
256 #define HANDSET_OUT_REMOTE      0x1000
257 #define HANDSET_OUT_MUTE        0x2000
258 #define HANDSET_OUT_BOTH        0x3000
259 #define IO_CTRL_OUT_LOCAL       0x0000
260 #define IO_CTRL_OUT_REMOTE      0x4000
261 #define IO_CTRL_OUT_MUTE        0x8000
262 #define IO_CTRL_OUT_BOTH        0xC000
263
264 #define SDO_IN_DEST_CTRL        0x3C
265 #define STATUS_ADDR_IN          0x0003
266 #define PCM_LR_IN_LOCAL         0x0000
267 #define PCM_LR_IN_REMOTE        0x0004
268 #define PCM_LR_RESERVED         0x0008
269 #define PCM_LR_IN_BOTH          0x000C
270 #define LINE1_ADC_IN_LOCAL      0x0000
271 #define LINE1_ADC_IN_REMOTE     0x0010
272 #define LINE1_ADC_IN_MUTE       0x0020
273 #define MIC_ADC_IN_LOCAL        0x0000
274 #define MIC_ADC_IN_REMOTE       0x0040
275 #define MIC_ADC_IN_MUTE         0x0080
276 #define LINE2_DAC_IN_LOCAL      0x0000
277 #define LINE2_DAC_IN_REMOTE     0x0400
278 #define LINE2_DAC_IN_MUTE       0x0800
279 #define HANDSET_IN_LOCAL        0x0000
280 #define HANDSET_IN_REMOTE       0x1000
281 #define HANDSET_IN_MUTE         0x2000
282 #define IO_STATUS_IN_LOCAL      0x0000
283 #define IO_STATUS_IN_REMOTE     0x4000
284
285 #define SPDIF_IN_CTRL           0x3E
286 #define SPDIF_IN_ENABLE         0x0001
287
288 #define GPIO_DATA               0x60
289 #define GPIO_DATA_MASK          0x0FFF
290 #define GPIO_HV_STATUS          0x3000
291 #define GPIO_PME_STATUS         0x4000
292
293 #define GPIO_MASK               0x64
294 #define GPIO_DIRECTION          0x68
295 #define GPO_PRIMARY_AC97        0x0001
296 #define GPI_LINEOUT_SENSE       0x0004
297 #define GPO_SECONDARY_AC97      0x0008
298 #define GPI_VOL_DOWN            0x0010
299 #define GPI_VOL_UP              0x0020
300 #define GPI_IIS_CLK             0x0040
301 #define GPI_IIS_LRCLK           0x0080
302 #define GPI_IIS_DATA            0x0100
303 #define GPI_DOCKING_STATUS      0x0100
304 #define GPI_HEADPHONE_SENSE     0x0200
305 #define GPO_EXT_AMP_SHUTDOWN    0x1000
306
307 #define GPO_EXT_AMP_M3          1       /* default m3 amp */
308 #define GPO_EXT_AMP_ALLEGRO     8       /* default allegro amp */
309
310 /* M3 */
311 #define GPO_M3_EXT_AMP_SHUTDN   0x0002
312
313 #define ASSP_INDEX_PORT         0x80
314 #define ASSP_MEMORY_PORT        0x82
315 #define ASSP_DATA_PORT          0x84
316
317 #define MPU401_DATA_PORT        0x98
318 #define MPU401_STATUS_PORT      0x99
319
320 #define CLK_MULT_DATA_PORT      0x9C
321
322 #define ASSP_CONTROL_A          0xA2
323 #define ASSP_0_WS_ENABLE        0x01
324 #define ASSP_CTRL_A_RESERVED1   0x02
325 #define ASSP_CTRL_A_RESERVED2   0x04
326 #define ASSP_CLK_49MHZ_SELECT   0x08
327 #define FAST_PLU_ENABLE         0x10
328 #define ASSP_CTRL_A_RESERVED3   0x20
329 #define DSP_CLK_36MHZ_SELECT    0x40
330
331 #define ASSP_CONTROL_B          0xA4
332 #define RESET_ASSP              0x00
333 #define RUN_ASSP                0x01
334 #define ENABLE_ASSP_CLOCK       0x00
335 #define STOP_ASSP_CLOCK         0x10
336 #define RESET_TOGGLE            0x40
337
338 #define ASSP_CONTROL_C          0xA6
339 #define ASSP_HOST_INT_ENABLE    0x01
340 #define FM_ADDR_REMAP_DISABLE   0x02
341 #define HOST_WRITE_PORT_ENABLE  0x08
342
343 #define ASSP_HOST_INT_STATUS    0xAC
344 #define DSP2HOST_REQ_PIORECORD  0x01
345 #define DSP2HOST_REQ_I2SRATE    0x02
346 #define DSP2HOST_REQ_TIMER      0x04
347
348 /*
349  * ASSP control regs
350  */
351 #define DSP_PORT_TIMER_COUNT    0x06
352
353 #define DSP_PORT_MEMORY_INDEX   0x80
354
355 #define DSP_PORT_MEMORY_TYPE    0x82
356 #define MEMTYPE_INTERNAL_CODE   0x0002
357 #define MEMTYPE_INTERNAL_DATA   0x0003
358 #define MEMTYPE_MASK            0x0003
359
360 #define DSP_PORT_MEMORY_DATA    0x84
361
362 #define DSP_PORT_CONTROL_REG_A  0xA2
363 #define DSP_PORT_CONTROL_REG_B  0xA4
364 #define DSP_PORT_CONTROL_REG_C  0xA6
365
366 #define REV_A_CODE_MEMORY_BEGIN         0x0000
367 #define REV_A_CODE_MEMORY_END           0x0FFF
368 #define REV_A_CODE_MEMORY_UNIT_LENGTH   0x0040
369 #define REV_A_CODE_MEMORY_LENGTH        (REV_A_CODE_MEMORY_END - REV_A_CODE_MEMORY_BEGIN + 1)
370
371 #define REV_B_CODE_MEMORY_BEGIN         0x0000
372 #define REV_B_CODE_MEMORY_END           0x0BFF
373 #define REV_B_CODE_MEMORY_UNIT_LENGTH   0x0040
374 #define REV_B_CODE_MEMORY_LENGTH        (REV_B_CODE_MEMORY_END - REV_B_CODE_MEMORY_BEGIN + 1)
375
376 #define REV_A_DATA_MEMORY_BEGIN         0x1000
377 #define REV_A_DATA_MEMORY_END           0x2FFF
378 #define REV_A_DATA_MEMORY_UNIT_LENGTH   0x0080
379 #define REV_A_DATA_MEMORY_LENGTH        (REV_A_DATA_MEMORY_END - REV_A_DATA_MEMORY_BEGIN + 1)
380
381 #define REV_B_DATA_MEMORY_BEGIN         0x1000
382 #define REV_B_DATA_MEMORY_END           0x2BFF
383 #define REV_B_DATA_MEMORY_UNIT_LENGTH   0x0080
384 #define REV_B_DATA_MEMORY_LENGTH        (REV_B_DATA_MEMORY_END - REV_B_DATA_MEMORY_BEGIN + 1)
385
386
387 #define NUM_UNITS_KERNEL_CODE          16
388 #define NUM_UNITS_KERNEL_DATA           2
389
390 #define NUM_UNITS_KERNEL_CODE_WITH_HSP 16
391 #define NUM_UNITS_KERNEL_DATA_WITH_HSP  5
392
393 /*
394  * Kernel data layout
395  */
396
397 #define DP_SHIFT_COUNT                  7
398
399 #define KDATA_BASE_ADDR                 0x1000
400 #define KDATA_BASE_ADDR2                0x1080
401
402 #define KDATA_TASK0                     (KDATA_BASE_ADDR + 0x0000)
403 #define KDATA_TASK1                     (KDATA_BASE_ADDR + 0x0001)
404 #define KDATA_TASK2                     (KDATA_BASE_ADDR + 0x0002)
405 #define KDATA_TASK3                     (KDATA_BASE_ADDR + 0x0003)
406 #define KDATA_TASK4                     (KDATA_BASE_ADDR + 0x0004)
407 #define KDATA_TASK5                     (KDATA_BASE_ADDR + 0x0005)
408 #define KDATA_TASK6                     (KDATA_BASE_ADDR + 0x0006)
409 #define KDATA_TASK7                     (KDATA_BASE_ADDR + 0x0007)
410 #define KDATA_TASK_ENDMARK              (KDATA_BASE_ADDR + 0x0008)
411
412 #define KDATA_CURRENT_TASK              (KDATA_BASE_ADDR + 0x0009)
413 #define KDATA_TASK_SWITCH               (KDATA_BASE_ADDR + 0x000A)
414
415 #define KDATA_INSTANCE0_POS3D           (KDATA_BASE_ADDR + 0x000B)
416 #define KDATA_INSTANCE1_POS3D           (KDATA_BASE_ADDR + 0x000C)
417 #define KDATA_INSTANCE2_POS3D           (KDATA_BASE_ADDR + 0x000D)
418 #define KDATA_INSTANCE3_POS3D           (KDATA_BASE_ADDR + 0x000E)
419 #define KDATA_INSTANCE4_POS3D           (KDATA_BASE_ADDR + 0x000F)
420 #define KDATA_INSTANCE5_POS3D           (KDATA_BASE_ADDR + 0x0010)
421 #define KDATA_INSTANCE6_POS3D           (KDATA_BASE_ADDR + 0x0011)
422 #define KDATA_INSTANCE7_POS3D           (KDATA_BASE_ADDR + 0x0012)
423 #define KDATA_INSTANCE8_POS3D           (KDATA_BASE_ADDR + 0x0013)
424 #define KDATA_INSTANCE_POS3D_ENDMARK    (KDATA_BASE_ADDR + 0x0014)
425
426 #define KDATA_INSTANCE0_SPKVIRT         (KDATA_BASE_ADDR + 0x0015)
427 #define KDATA_INSTANCE_SPKVIRT_ENDMARK  (KDATA_BASE_ADDR + 0x0016)
428
429 #define KDATA_INSTANCE0_SPDIF           (KDATA_BASE_ADDR + 0x0017)
430 #define KDATA_INSTANCE_SPDIF_ENDMARK    (KDATA_BASE_ADDR + 0x0018)
431
432 #define KDATA_INSTANCE0_MODEM           (KDATA_BASE_ADDR + 0x0019)
433 #define KDATA_INSTANCE_MODEM_ENDMARK    (KDATA_BASE_ADDR + 0x001A)
434
435 #define KDATA_INSTANCE0_SRC             (KDATA_BASE_ADDR + 0x001B)
436 #define KDATA_INSTANCE1_SRC             (KDATA_BASE_ADDR + 0x001C)
437 #define KDATA_INSTANCE_SRC_ENDMARK      (KDATA_BASE_ADDR + 0x001D)
438
439 #define KDATA_INSTANCE0_MINISRC         (KDATA_BASE_ADDR + 0x001E)
440 #define KDATA_INSTANCE1_MINISRC         (KDATA_BASE_ADDR + 0x001F)
441 #define KDATA_INSTANCE2_MINISRC         (KDATA_BASE_ADDR + 0x0020)
442 #define KDATA_INSTANCE3_MINISRC         (KDATA_BASE_ADDR + 0x0021)
443 #define KDATA_INSTANCE_MINISRC_ENDMARK  (KDATA_BASE_ADDR + 0x0022)
444
445 #define KDATA_INSTANCE0_CPYTHRU         (KDATA_BASE_ADDR + 0x0023)
446 #define KDATA_INSTANCE1_CPYTHRU         (KDATA_BASE_ADDR + 0x0024)
447 #define KDATA_INSTANCE_CPYTHRU_ENDMARK  (KDATA_BASE_ADDR + 0x0025)
448
449 #define KDATA_CURRENT_DMA               (KDATA_BASE_ADDR + 0x0026)
450 #define KDATA_DMA_SWITCH                (KDATA_BASE_ADDR + 0x0027)
451 #define KDATA_DMA_ACTIVE                (KDATA_BASE_ADDR + 0x0028)
452
453 #define KDATA_DMA_XFER0                 (KDATA_BASE_ADDR + 0x0029)
454 #define KDATA_DMA_XFER1                 (KDATA_BASE_ADDR + 0x002A)
455 #define KDATA_DMA_XFER2                 (KDATA_BASE_ADDR + 0x002B)
456 #define KDATA_DMA_XFER3                 (KDATA_BASE_ADDR + 0x002C)
457 #define KDATA_DMA_XFER4                 (KDATA_BASE_ADDR + 0x002D)
458 #define KDATA_DMA_XFER5                 (KDATA_BASE_ADDR + 0x002E)
459 #define KDATA_DMA_XFER6                 (KDATA_BASE_ADDR + 0x002F)
460 #define KDATA_DMA_XFER7                 (KDATA_BASE_ADDR + 0x0030)
461 #define KDATA_DMA_XFER8                 (KDATA_BASE_ADDR + 0x0031)
462 #define KDATA_DMA_XFER_ENDMARK          (KDATA_BASE_ADDR + 0x0032)
463
464 #define KDATA_I2S_SAMPLE_COUNT          (KDATA_BASE_ADDR + 0x0033)
465 #define KDATA_I2S_INT_METER             (KDATA_BASE_ADDR + 0x0034)
466 #define KDATA_I2S_ACTIVE                (KDATA_BASE_ADDR + 0x0035)
467
468 #define KDATA_TIMER_COUNT_RELOAD        (KDATA_BASE_ADDR + 0x0036)
469 #define KDATA_TIMER_COUNT_CURRENT       (KDATA_BASE_ADDR + 0x0037)
470
471 #define KDATA_HALT_SYNCH_CLIENT         (KDATA_BASE_ADDR + 0x0038)
472 #define KDATA_HALT_SYNCH_DMA            (KDATA_BASE_ADDR + 0x0039)
473 #define KDATA_HALT_ACKNOWLEDGE          (KDATA_BASE_ADDR + 0x003A)
474
475 #define KDATA_ADC1_XFER0                (KDATA_BASE_ADDR + 0x003B)
476 #define KDATA_ADC1_XFER_ENDMARK         (KDATA_BASE_ADDR + 0x003C)
477 #define KDATA_ADC1_LEFT_VOLUME                  (KDATA_BASE_ADDR + 0x003D)
478 #define KDATA_ADC1_RIGHT_VOLUME                 (KDATA_BASE_ADDR + 0x003E)
479 #define KDATA_ADC1_LEFT_SUR_VOL                 (KDATA_BASE_ADDR + 0x003F)
480 #define KDATA_ADC1_RIGHT_SUR_VOL                (KDATA_BASE_ADDR + 0x0040)
481
482 #define KDATA_ADC2_XFER0                (KDATA_BASE_ADDR + 0x0041)
483 #define KDATA_ADC2_XFER_ENDMARK         (KDATA_BASE_ADDR + 0x0042)
484 #define KDATA_ADC2_LEFT_VOLUME                  (KDATA_BASE_ADDR + 0x0043)
485 #define KDATA_ADC2_RIGHT_VOLUME                 (KDATA_BASE_ADDR + 0x0044)
486 #define KDATA_ADC2_LEFT_SUR_VOL                 (KDATA_BASE_ADDR + 0x0045)
487 #define KDATA_ADC2_RIGHT_SUR_VOL                (KDATA_BASE_ADDR + 0x0046)
488
489 #define KDATA_CD_XFER0                                  (KDATA_BASE_ADDR + 0x0047)                                      
490 #define KDATA_CD_XFER_ENDMARK                   (KDATA_BASE_ADDR + 0x0048)
491 #define KDATA_CD_LEFT_VOLUME                    (KDATA_BASE_ADDR + 0x0049)
492 #define KDATA_CD_RIGHT_VOLUME                   (KDATA_BASE_ADDR + 0x004A)
493 #define KDATA_CD_LEFT_SUR_VOL                   (KDATA_BASE_ADDR + 0x004B)
494 #define KDATA_CD_RIGHT_SUR_VOL                  (KDATA_BASE_ADDR + 0x004C)
495
496 #define KDATA_MIC_XFER0                                 (KDATA_BASE_ADDR + 0x004D)
497 #define KDATA_MIC_XFER_ENDMARK                  (KDATA_BASE_ADDR + 0x004E)
498 #define KDATA_MIC_VOLUME                                (KDATA_BASE_ADDR + 0x004F)
499 #define KDATA_MIC_SUR_VOL                               (KDATA_BASE_ADDR + 0x0050)
500
501 #define KDATA_I2S_XFER0                 (KDATA_BASE_ADDR + 0x0051)
502 #define KDATA_I2S_XFER_ENDMARK          (KDATA_BASE_ADDR + 0x0052)
503
504 #define KDATA_CHI_XFER0                 (KDATA_BASE_ADDR + 0x0053)
505 #define KDATA_CHI_XFER_ENDMARK          (KDATA_BASE_ADDR + 0x0054)
506
507 #define KDATA_SPDIF_XFER                (KDATA_BASE_ADDR + 0x0055)
508 #define KDATA_SPDIF_CURRENT_FRAME       (KDATA_BASE_ADDR + 0x0056)
509 #define KDATA_SPDIF_FRAME0              (KDATA_BASE_ADDR + 0x0057)
510 #define KDATA_SPDIF_FRAME1              (KDATA_BASE_ADDR + 0x0058)
511 #define KDATA_SPDIF_FRAME2              (KDATA_BASE_ADDR + 0x0059)
512
513 #define KDATA_SPDIF_REQUEST             (KDATA_BASE_ADDR + 0x005A)
514 #define KDATA_SPDIF_TEMP                (KDATA_BASE_ADDR + 0x005B)
515
516 #define KDATA_SPDIFIN_XFER0             (KDATA_BASE_ADDR + 0x005C)
517 #define KDATA_SPDIFIN_XFER_ENDMARK      (KDATA_BASE_ADDR + 0x005D)
518 #define KDATA_SPDIFIN_INT_METER         (KDATA_BASE_ADDR + 0x005E)
519
520 #define KDATA_DSP_RESET_COUNT           (KDATA_BASE_ADDR + 0x005F)
521 #define KDATA_DEBUG_OUTPUT              (KDATA_BASE_ADDR + 0x0060)
522
523 #define KDATA_KERNEL_ISR_LIST           (KDATA_BASE_ADDR + 0x0061)
524
525 #define KDATA_KERNEL_ISR_CBSR1          (KDATA_BASE_ADDR + 0x0062)
526 #define KDATA_KERNEL_ISR_CBER1          (KDATA_BASE_ADDR + 0x0063)
527 #define KDATA_KERNEL_ISR_CBCR           (KDATA_BASE_ADDR + 0x0064)
528 #define KDATA_KERNEL_ISR_AR0            (KDATA_BASE_ADDR + 0x0065)
529 #define KDATA_KERNEL_ISR_AR1            (KDATA_BASE_ADDR + 0x0066)
530 #define KDATA_KERNEL_ISR_AR2            (KDATA_BASE_ADDR + 0x0067)
531 #define KDATA_KERNEL_ISR_AR3            (KDATA_BASE_ADDR + 0x0068)
532 #define KDATA_KERNEL_ISR_AR4            (KDATA_BASE_ADDR + 0x0069)
533 #define KDATA_KERNEL_ISR_AR5            (KDATA_BASE_ADDR + 0x006A)
534 #define KDATA_KERNEL_ISR_BRCR           (KDATA_BASE_ADDR + 0x006B)
535 #define KDATA_KERNEL_ISR_PASR           (KDATA_BASE_ADDR + 0x006C)
536 #define KDATA_KERNEL_ISR_PAER           (KDATA_BASE_ADDR + 0x006D)
537
538 #define KDATA_CLIENT_SCRATCH0           (KDATA_BASE_ADDR + 0x006E)
539 #define KDATA_CLIENT_SCRATCH1           (KDATA_BASE_ADDR + 0x006F)
540 #define KDATA_KERNEL_SCRATCH            (KDATA_BASE_ADDR + 0x0070)
541 #define KDATA_KERNEL_ISR_SCRATCH        (KDATA_BASE_ADDR + 0x0071)
542
543 #define KDATA_OUEUE_LEFT                (KDATA_BASE_ADDR + 0x0072)
544 #define KDATA_QUEUE_RIGHT               (KDATA_BASE_ADDR + 0x0073)
545
546 #define KDATA_ADC1_REQUEST              (KDATA_BASE_ADDR + 0x0074)
547 #define KDATA_ADC2_REQUEST              (KDATA_BASE_ADDR + 0x0075)
548 #define KDATA_CD_REQUEST                                (KDATA_BASE_ADDR + 0x0076)
549 #define KDATA_MIC_REQUEST                               (KDATA_BASE_ADDR + 0x0077)
550
551 #define KDATA_ADC1_MIXER_REQUEST        (KDATA_BASE_ADDR + 0x0078)
552 #define KDATA_ADC2_MIXER_REQUEST        (KDATA_BASE_ADDR + 0x0079)
553 #define KDATA_CD_MIXER_REQUEST                  (KDATA_BASE_ADDR + 0x007A)
554 #define KDATA_MIC_MIXER_REQUEST                 (KDATA_BASE_ADDR + 0x007B)
555 #define KDATA_MIC_SYNC_COUNTER                  (KDATA_BASE_ADDR + 0x007C)
556
557 /*
558  * second 'segment' (?) reserved for mixer
559  * buffers..
560  */
561
562 #define KDATA_MIXER_WORD0               (KDATA_BASE_ADDR2 + 0x0000)
563 #define KDATA_MIXER_WORD1               (KDATA_BASE_ADDR2 + 0x0001)
564 #define KDATA_MIXER_WORD2               (KDATA_BASE_ADDR2 + 0x0002)
565 #define KDATA_MIXER_WORD3               (KDATA_BASE_ADDR2 + 0x0003)
566 #define KDATA_MIXER_WORD4               (KDATA_BASE_ADDR2 + 0x0004)
567 #define KDATA_MIXER_WORD5               (KDATA_BASE_ADDR2 + 0x0005)
568 #define KDATA_MIXER_WORD6               (KDATA_BASE_ADDR2 + 0x0006)
569 #define KDATA_MIXER_WORD7               (KDATA_BASE_ADDR2 + 0x0007)
570 #define KDATA_MIXER_WORD8               (KDATA_BASE_ADDR2 + 0x0008)
571 #define KDATA_MIXER_WORD9               (KDATA_BASE_ADDR2 + 0x0009)
572 #define KDATA_MIXER_WORDA               (KDATA_BASE_ADDR2 + 0x000A)
573 #define KDATA_MIXER_WORDB               (KDATA_BASE_ADDR2 + 0x000B)
574 #define KDATA_MIXER_WORDC               (KDATA_BASE_ADDR2 + 0x000C)
575 #define KDATA_MIXER_WORDD               (KDATA_BASE_ADDR2 + 0x000D)
576 #define KDATA_MIXER_WORDE               (KDATA_BASE_ADDR2 + 0x000E)
577 #define KDATA_MIXER_WORDF               (KDATA_BASE_ADDR2 + 0x000F)
578
579 #define KDATA_MIXER_XFER0               (KDATA_BASE_ADDR2 + 0x0010)
580 #define KDATA_MIXER_XFER1               (KDATA_BASE_ADDR2 + 0x0011)
581 #define KDATA_MIXER_XFER2               (KDATA_BASE_ADDR2 + 0x0012)
582 #define KDATA_MIXER_XFER3               (KDATA_BASE_ADDR2 + 0x0013)
583 #define KDATA_MIXER_XFER4               (KDATA_BASE_ADDR2 + 0x0014)
584 #define KDATA_MIXER_XFER5               (KDATA_BASE_ADDR2 + 0x0015)
585 #define KDATA_MIXER_XFER6               (KDATA_BASE_ADDR2 + 0x0016)
586 #define KDATA_MIXER_XFER7               (KDATA_BASE_ADDR2 + 0x0017)
587 #define KDATA_MIXER_XFER8               (KDATA_BASE_ADDR2 + 0x0018)
588 #define KDATA_MIXER_XFER9               (KDATA_BASE_ADDR2 + 0x0019)
589 #define KDATA_MIXER_XFER_ENDMARK        (KDATA_BASE_ADDR2 + 0x001A)
590
591 #define KDATA_MIXER_TASK_NUMBER         (KDATA_BASE_ADDR2 + 0x001B)
592 #define KDATA_CURRENT_MIXER             (KDATA_BASE_ADDR2 + 0x001C)
593 #define KDATA_MIXER_ACTIVE              (KDATA_BASE_ADDR2 + 0x001D)
594 #define KDATA_MIXER_BANK_STATUS         (KDATA_BASE_ADDR2 + 0x001E)
595 #define KDATA_DAC_LEFT_VOLUME           (KDATA_BASE_ADDR2 + 0x001F)
596 #define KDATA_DAC_RIGHT_VOLUME          (KDATA_BASE_ADDR2 + 0x0020)
597
598 #define MAX_INSTANCE_MINISRC            (KDATA_INSTANCE_MINISRC_ENDMARK - KDATA_INSTANCE0_MINISRC)
599 #define MAX_VIRTUAL_DMA_CHANNELS        (KDATA_DMA_XFER_ENDMARK - KDATA_DMA_XFER0)
600 #define MAX_VIRTUAL_MIXER_CHANNELS      (KDATA_MIXER_XFER_ENDMARK - KDATA_MIXER_XFER0)
601 #define MAX_VIRTUAL_ADC1_CHANNELS       (KDATA_ADC1_XFER_ENDMARK - KDATA_ADC1_XFER0)
602
603 /*
604  * client data area offsets
605  */
606 #define CDATA_INSTANCE_READY            0x00
607
608 #define CDATA_HOST_SRC_ADDRL            0x01
609 #define CDATA_HOST_SRC_ADDRH            0x02
610 #define CDATA_HOST_SRC_END_PLUS_1L      0x03
611 #define CDATA_HOST_SRC_END_PLUS_1H      0x04
612 #define CDATA_HOST_SRC_CURRENTL         0x05
613 #define CDATA_HOST_SRC_CURRENTH         0x06
614
615 #define CDATA_IN_BUF_CONNECT            0x07
616 #define CDATA_OUT_BUF_CONNECT           0x08
617
618 #define CDATA_IN_BUF_BEGIN              0x09
619 #define CDATA_IN_BUF_END_PLUS_1         0x0A
620 #define CDATA_IN_BUF_HEAD               0x0B
621 #define CDATA_IN_BUF_TAIL               0x0C
622 #define CDATA_OUT_BUF_BEGIN             0x0D
623 #define CDATA_OUT_BUF_END_PLUS_1        0x0E
624 #define CDATA_OUT_BUF_HEAD              0x0F
625 #define CDATA_OUT_BUF_TAIL              0x10
626
627 #define CDATA_DMA_CONTROL               0x11
628 #define CDATA_RESERVED                  0x12
629
630 #define CDATA_FREQUENCY                 0x13
631 #define CDATA_LEFT_VOLUME               0x14
632 #define CDATA_RIGHT_VOLUME              0x15
633 #define CDATA_LEFT_SUR_VOL              0x16
634 #define CDATA_RIGHT_SUR_VOL             0x17
635
636 #define CDATA_HEADER_LEN                0x18
637
638 #define SRC3_DIRECTION_OFFSET           CDATA_HEADER_LEN
639 #define SRC3_MODE_OFFSET                (CDATA_HEADER_LEN + 1)
640 #define SRC3_WORD_LENGTH_OFFSET         (CDATA_HEADER_LEN + 2)
641 #define SRC3_PARAMETER_OFFSET           (CDATA_HEADER_LEN + 3)
642 #define SRC3_COEFF_ADDR_OFFSET          (CDATA_HEADER_LEN + 8)
643 #define SRC3_FILTAP_ADDR_OFFSET         (CDATA_HEADER_LEN + 10)
644 #define SRC3_TEMP_INBUF_ADDR_OFFSET     (CDATA_HEADER_LEN + 16)
645 #define SRC3_TEMP_OUTBUF_ADDR_OFFSET    (CDATA_HEADER_LEN + 17)
646
647 #define MINISRC_IN_BUFFER_SIZE   ( 0x50 * 2 )
648 #define MINISRC_OUT_BUFFER_SIZE  ( 0x50 * 2 * 2)
649 #define MINISRC_TMP_BUFFER_SIZE  ( 112 + ( MINISRC_BIQUAD_STAGE * 3 + 4 ) * 2 * 2 )
650 #define MINISRC_BIQUAD_STAGE    2
651 #define MINISRC_COEF_LOC          0x175
652
653 #define DMACONTROL_BLOCK_MASK           0x000F
654 #define  DMAC_BLOCK0_SELECTOR           0x0000
655 #define  DMAC_BLOCK1_SELECTOR           0x0001
656 #define  DMAC_BLOCK2_SELECTOR           0x0002
657 #define  DMAC_BLOCK3_SELECTOR           0x0003
658 #define  DMAC_BLOCK4_SELECTOR           0x0004
659 #define  DMAC_BLOCK5_SELECTOR           0x0005
660 #define  DMAC_BLOCK6_SELECTOR           0x0006
661 #define  DMAC_BLOCK7_SELECTOR           0x0007
662 #define  DMAC_BLOCK8_SELECTOR           0x0008
663 #define  DMAC_BLOCK9_SELECTOR           0x0009
664 #define  DMAC_BLOCKA_SELECTOR           0x000A
665 #define  DMAC_BLOCKB_SELECTOR           0x000B
666 #define  DMAC_BLOCKC_SELECTOR           0x000C
667 #define  DMAC_BLOCKD_SELECTOR           0x000D
668 #define  DMAC_BLOCKE_SELECTOR           0x000E
669 #define  DMAC_BLOCKF_SELECTOR           0x000F
670 #define DMACONTROL_PAGE_MASK            0x00F0
671 #define  DMAC_PAGE0_SELECTOR            0x0030
672 #define  DMAC_PAGE1_SELECTOR            0x0020
673 #define  DMAC_PAGE2_SELECTOR            0x0010
674 #define  DMAC_PAGE3_SELECTOR            0x0000
675 #define DMACONTROL_AUTOREPEAT           0x1000
676 #define DMACONTROL_STOPPED              0x2000
677 #define DMACONTROL_DIRECTION            0x0100
678
679 /*
680  * an arbitrary volume we set the internal
681  * volume settings to so that the ac97 volume
682  * range is a little less insane.  0x7fff is 
683  * max.
684  */
685 #define ARB_VOLUME ( 0x6800 )
686
687 /*
688  */
689
690 struct m3_list {
691         int curlen;
692         int mem_addr;
693         int max;
694 };
695
696 struct m3_dma {
697
698         int number;
699         struct snd_pcm_substream *substream;
700
701         struct assp_instance {
702                 unsigned short code, data;
703         } inst;
704
705         int running;
706         int opened;
707
708         unsigned long buffer_addr;
709         int dma_size;
710         int period_size;
711         unsigned int hwptr;
712         int count;
713
714         int index[3];
715         struct m3_list *index_list[3];
716
717         int in_lists;
718         
719         struct list_head list;
720
721 };
722     
723 struct snd_m3 {
724         
725         struct snd_card *card;
726
727         unsigned long iobase;
728
729         int irq;
730         unsigned int allegro_flag : 1;
731
732         struct snd_ac97 *ac97;
733
734         struct snd_pcm *pcm;
735
736         struct pci_dev *pci;
737
738         int dacs_active;
739         int timer_users;
740
741         struct m3_list  msrc_list;
742         struct m3_list  mixer_list;
743         struct m3_list  adc1_list;
744         struct m3_list  dma_list;
745
746         /* for storing reset state..*/
747         u8 reset_state;
748
749         int external_amp;
750         int amp_gpio;   /* gpio pin #  for external amp, -1 = default */
751         unsigned int hv_config;         /* hardware-volume config bits */
752         unsigned irda_workaround :1;    /* avoid to touch 0x10 on GPIO_DIRECTION
753                                            (e.g. for IrDA on Dell Inspirons) */
754         unsigned is_omnibook :1;        /* Do HP OmniBook GPIO magic? */
755
756         /* midi */
757         struct snd_rawmidi *rmidi;
758
759         /* pcm streams */
760         int num_substreams;
761         struct m3_dma *substreams;
762
763         spinlock_t reg_lock;
764
765 #ifdef CONFIG_SND_MAESTRO3_INPUT
766         struct input_dev *input_dev;
767         char phys[64];                  /* physical device path */
768 #else
769         struct snd_kcontrol *master_switch;
770         struct snd_kcontrol *master_volume;
771 #endif
772         struct work_struct hwvol_work;
773
774         unsigned int in_suspend;
775
776 #ifdef CONFIG_PM_SLEEP
777         u16 *suspend_mem;
778 #endif
779
780         const struct firmware *assp_kernel_image;
781         const struct firmware *assp_minisrc_image;
782 };
783
784 /*
785  * pci ids
786  */
787 static const struct pci_device_id snd_m3_ids[] = {
788         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ALLEGRO_1, PCI_ANY_ID, PCI_ANY_ID,
789          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
790         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ALLEGRO, PCI_ANY_ID, PCI_ANY_ID,
791          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
792         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_CANYON3D_2LE, PCI_ANY_ID, PCI_ANY_ID,
793          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
794         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_CANYON3D_2, PCI_ANY_ID, PCI_ANY_ID,
795          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
796         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_MAESTRO3, PCI_ANY_ID, PCI_ANY_ID,
797          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
798         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_MAESTRO3_1, PCI_ANY_ID, PCI_ANY_ID,
799          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
800         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_MAESTRO3_HW, PCI_ANY_ID, PCI_ANY_ID,
801          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
802         {PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_MAESTRO3_2, PCI_ANY_ID, PCI_ANY_ID,
803          PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0},
804         {0,},
805 };
806
807 MODULE_DEVICE_TABLE(pci, snd_m3_ids);
808
809 static const struct snd_pci_quirk m3_amp_quirk_list[] = {
810         SND_PCI_QUIRK(0x0E11, 0x0094, "Compaq Evo N600c", 0x0c),
811         SND_PCI_QUIRK(0x10f7, 0x833e, "Panasonic CF-28", 0x0d),
812         SND_PCI_QUIRK(0x10f7, 0x833d, "Panasonic CF-72", 0x0d),
813         SND_PCI_QUIRK(0x1033, 0x80f1, "NEC LM800J/7", 0x03),
814         SND_PCI_QUIRK(0x1509, 0x1740, "LEGEND ZhaoYang 3100CF", 0x03),
815         { } /* END */
816 };
817
818 static const struct snd_pci_quirk m3_irda_quirk_list[] = {
819         SND_PCI_QUIRK(0x1028, 0x00b0, "Dell Inspiron 4000", 1),
820         SND_PCI_QUIRK(0x1028, 0x00a4, "Dell Inspiron 8000", 1),
821         SND_PCI_QUIRK(0x1028, 0x00e6, "Dell Inspiron 8100", 1),
822         { } /* END */
823 };
824
825 /* hardware volume quirks */
826 static const struct snd_pci_quirk m3_hv_quirk_list[] = {
827         /* Allegro chips */
828         SND_PCI_QUIRK(0x0E11, 0x002E, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
829         SND_PCI_QUIRK(0x0E11, 0x0094, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
830         SND_PCI_QUIRK(0x0E11, 0xB112, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
831         SND_PCI_QUIRK(0x0E11, 0xB114, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
832         SND_PCI_QUIRK(0x103C, 0x0012, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
833         SND_PCI_QUIRK(0x103C, 0x0018, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
834         SND_PCI_QUIRK(0x103C, 0x001C, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
835         SND_PCI_QUIRK(0x103C, 0x001D, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
836         SND_PCI_QUIRK(0x103C, 0x001E, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
837         SND_PCI_QUIRK(0x107B, 0x3350, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
838         SND_PCI_QUIRK(0x10F7, 0x8338, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
839         SND_PCI_QUIRK(0x10F7, 0x833C, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
840         SND_PCI_QUIRK(0x10F7, 0x833D, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
841         SND_PCI_QUIRK(0x10F7, 0x833E, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
842         SND_PCI_QUIRK(0x10F7, 0x833F, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
843         SND_PCI_QUIRK(0x13BD, 0x1018, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
844         SND_PCI_QUIRK(0x13BD, 0x1019, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
845         SND_PCI_QUIRK(0x13BD, 0x101A, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
846         SND_PCI_QUIRK(0x14FF, 0x0F03, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
847         SND_PCI_QUIRK(0x14FF, 0x0F04, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
848         SND_PCI_QUIRK(0x14FF, 0x0F05, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
849         SND_PCI_QUIRK(0x156D, 0xB400, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
850         SND_PCI_QUIRK(0x156D, 0xB795, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
851         SND_PCI_QUIRK(0x156D, 0xB797, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
852         SND_PCI_QUIRK(0x156D, 0xC700, NULL, HV_CTRL_ENABLE | HV_BUTTON_FROM_GD),
853         SND_PCI_QUIRK(0x1033, 0x80F1, NULL,
854                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
855         SND_PCI_QUIRK(0x103C, 0x001A, NULL, /* HP OmniBook 6100 */
856                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
857         SND_PCI_QUIRK(0x107B, 0x340A, NULL,
858                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
859         SND_PCI_QUIRK(0x107B, 0x3450, NULL,
860                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
861         SND_PCI_QUIRK(0x109F, 0x3134, NULL,
862                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
863         SND_PCI_QUIRK(0x109F, 0x3161, NULL,
864                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
865         SND_PCI_QUIRK(0x144D, 0x3280, NULL,
866                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
867         SND_PCI_QUIRK(0x144D, 0x3281, NULL,
868                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
869         SND_PCI_QUIRK(0x144D, 0xC002, NULL,
870                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
871         SND_PCI_QUIRK(0x144D, 0xC003, NULL,
872                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
873         SND_PCI_QUIRK(0x1509, 0x1740, NULL,
874                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
875         SND_PCI_QUIRK(0x1610, 0x0010, NULL,
876                       HV_CTRL_ENABLE | HV_BUTTON_FROM_GD | REDUCED_DEBOUNCE),
877         SND_PCI_QUIRK(0x1042, 0x1042, NULL, HV_CTRL_ENABLE),
878         SND_PCI_QUIRK(0x107B, 0x9500, NULL, HV_CTRL_ENABLE),
879         SND_PCI_QUIRK(0x14FF, 0x0F06, NULL, HV_CTRL_ENABLE),
880         SND_PCI_QUIRK(0x1558, 0x8586, NULL, HV_CTRL_ENABLE),
881         SND_PCI_QUIRK(0x161F, 0x2011, NULL, HV_CTRL_ENABLE),
882         /* Maestro3 chips */
883         SND_PCI_QUIRK(0x103C, 0x000E, NULL, HV_CTRL_ENABLE),
884         SND_PCI_QUIRK(0x103C, 0x0010, NULL, HV_CTRL_ENABLE),
885         SND_PCI_QUIRK(0x103C, 0x0011, NULL, HV_CTRL_ENABLE),
886         SND_PCI_QUIRK(0x103C, 0x001B, NULL, HV_CTRL_ENABLE),
887         SND_PCI_QUIRK(0x104D, 0x80A6, NULL, HV_CTRL_ENABLE),
888         SND_PCI_QUIRK(0x104D, 0x80AA, NULL, HV_CTRL_ENABLE),
889         SND_PCI_QUIRK(0x107B, 0x5300, NULL, HV_CTRL_ENABLE),
890         SND_PCI_QUIRK(0x110A, 0x1998, NULL, HV_CTRL_ENABLE),
891         SND_PCI_QUIRK(0x13BD, 0x1015, NULL, HV_CTRL_ENABLE),
892         SND_PCI_QUIRK(0x13BD, 0x101C, NULL, HV_CTRL_ENABLE),
893         SND_PCI_QUIRK(0x13BD, 0x1802, NULL, HV_CTRL_ENABLE),
894         SND_PCI_QUIRK(0x1599, 0x0715, NULL, HV_CTRL_ENABLE),
895         SND_PCI_QUIRK(0x5643, 0x5643, NULL, HV_CTRL_ENABLE),
896         SND_PCI_QUIRK(0x144D, 0x3260, NULL, HV_CTRL_ENABLE | REDUCED_DEBOUNCE),
897         SND_PCI_QUIRK(0x144D, 0x3261, NULL, HV_CTRL_ENABLE | REDUCED_DEBOUNCE),
898         SND_PCI_QUIRK(0x144D, 0xC000, NULL, HV_CTRL_ENABLE | REDUCED_DEBOUNCE),
899         SND_PCI_QUIRK(0x144D, 0xC001, NULL, HV_CTRL_ENABLE | REDUCED_DEBOUNCE),
900         { } /* END */
901 };
902
903 /* HP Omnibook quirks */
904 static const struct snd_pci_quirk m3_omnibook_quirk_list[] = {
905         SND_PCI_QUIRK_ID(0x103c, 0x0010), /* HP OmniBook 6000 */
906         SND_PCI_QUIRK_ID(0x103c, 0x0011), /* HP OmniBook 500 */
907         { } /* END */
908 };
909
910 /*
911  * lowlevel functions
912  */
913
914 static inline void snd_m3_outw(struct snd_m3 *chip, u16 value, unsigned long reg)
915 {
916         outw(value, chip->iobase + reg);
917 }
918
919 static inline u16 snd_m3_inw(struct snd_m3 *chip, unsigned long reg)
920 {
921         return inw(chip->iobase + reg);
922 }
923
924 static inline void snd_m3_outb(struct snd_m3 *chip, u8 value, unsigned long reg)
925 {
926         outb(value, chip->iobase + reg);
927 }
928
929 static inline u8 snd_m3_inb(struct snd_m3 *chip, unsigned long reg)
930 {
931         return inb(chip->iobase + reg);
932 }
933
934 /*
935  * access 16bit words to the code or data regions of the dsp's memory.
936  * index addresses 16bit words.
937  */
938 static u16 snd_m3_assp_read(struct snd_m3 *chip, u16 region, u16 index)
939 {
940         snd_m3_outw(chip, region & MEMTYPE_MASK, DSP_PORT_MEMORY_TYPE);
941         snd_m3_outw(chip, index, DSP_PORT_MEMORY_INDEX);
942         return snd_m3_inw(chip, DSP_PORT_MEMORY_DATA);
943 }
944
945 static void snd_m3_assp_write(struct snd_m3 *chip, u16 region, u16 index, u16 data)
946 {
947         snd_m3_outw(chip, region & MEMTYPE_MASK, DSP_PORT_MEMORY_TYPE);
948         snd_m3_outw(chip, index, DSP_PORT_MEMORY_INDEX);
949         snd_m3_outw(chip, data, DSP_PORT_MEMORY_DATA);
950 }
951
952 static void snd_m3_assp_halt(struct snd_m3 *chip)
953 {
954         chip->reset_state = snd_m3_inb(chip, DSP_PORT_CONTROL_REG_B) & ~REGB_STOP_CLOCK;
955         msleep(10);
956         snd_m3_outb(chip, chip->reset_state & ~REGB_ENABLE_RESET, DSP_PORT_CONTROL_REG_B);
957 }
958
959 static void snd_m3_assp_continue(struct snd_m3 *chip)
960 {
961         snd_m3_outb(chip, chip->reset_state | REGB_ENABLE_RESET, DSP_PORT_CONTROL_REG_B);
962 }
963
964
965 /*
966  * This makes me sad. the maestro3 has lists
967  * internally that must be packed.. 0 terminates,
968  * apparently, or maybe all unused entries have
969  * to be 0, the lists have static lengths set
970  * by the binary code images.
971  */
972
973 static int snd_m3_add_list(struct snd_m3 *chip, struct m3_list *list, u16 val)
974 {
975         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
976                           list->mem_addr + list->curlen,
977                           val);
978         return list->curlen++;
979 }
980
981 static void snd_m3_remove_list(struct snd_m3 *chip, struct m3_list *list, int index)
982 {
983         u16  val;
984         int lastindex = list->curlen - 1;
985
986         if (index != lastindex) {
987                 val = snd_m3_assp_read(chip, MEMTYPE_INTERNAL_DATA,
988                                        list->mem_addr + lastindex);
989                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
990                                   list->mem_addr + index,
991                                   val);
992         }
993
994         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
995                           list->mem_addr + lastindex,
996                           0);
997
998         list->curlen--;
999 }
1000
1001 static void snd_m3_inc_timer_users(struct snd_m3 *chip)
1002 {
1003         chip->timer_users++;
1004         if (chip->timer_users != 1) 
1005                 return;
1006
1007         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1008                           KDATA_TIMER_COUNT_RELOAD,
1009                           240);
1010
1011         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1012                           KDATA_TIMER_COUNT_CURRENT,
1013                           240);
1014
1015         snd_m3_outw(chip,
1016                     snd_m3_inw(chip, HOST_INT_CTRL) | CLKRUN_GEN_ENABLE,
1017                     HOST_INT_CTRL);
1018 }
1019
1020 static void snd_m3_dec_timer_users(struct snd_m3 *chip)
1021 {
1022         chip->timer_users--;
1023         if (chip->timer_users > 0)  
1024                 return;
1025
1026         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1027                           KDATA_TIMER_COUNT_RELOAD,
1028                           0);
1029
1030         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1031                           KDATA_TIMER_COUNT_CURRENT,
1032                           0);
1033
1034         snd_m3_outw(chip,
1035                     snd_m3_inw(chip, HOST_INT_CTRL) & ~CLKRUN_GEN_ENABLE,
1036                     HOST_INT_CTRL);
1037 }
1038
1039 /*
1040  * start/stop
1041  */
1042
1043 /* spinlock held! */
1044 static int snd_m3_pcm_start(struct snd_m3 *chip, struct m3_dma *s,
1045                             struct snd_pcm_substream *subs)
1046 {
1047         if (! s || ! subs)
1048                 return -EINVAL;
1049
1050         snd_m3_inc_timer_users(chip);
1051         switch (subs->stream) {
1052         case SNDRV_PCM_STREAM_PLAYBACK:
1053                 chip->dacs_active++;
1054                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1055                                   s->inst.data + CDATA_INSTANCE_READY, 1);
1056                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1057                                   KDATA_MIXER_TASK_NUMBER,
1058                                   chip->dacs_active);
1059                 break;
1060         case SNDRV_PCM_STREAM_CAPTURE:
1061                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1062                                   KDATA_ADC1_REQUEST, 1);
1063                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1064                                   s->inst.data + CDATA_INSTANCE_READY, 1);
1065                 break;
1066         }
1067         return 0;
1068 }
1069
1070 /* spinlock held! */
1071 static int snd_m3_pcm_stop(struct snd_m3 *chip, struct m3_dma *s,
1072                            struct snd_pcm_substream *subs)
1073 {
1074         if (! s || ! subs)
1075                 return -EINVAL;
1076
1077         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1078                           s->inst.data + CDATA_INSTANCE_READY, 0);
1079         snd_m3_dec_timer_users(chip);
1080         switch (subs->stream) {
1081         case SNDRV_PCM_STREAM_PLAYBACK:
1082                 chip->dacs_active--;
1083                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1084                                   KDATA_MIXER_TASK_NUMBER, 
1085                                   chip->dacs_active);
1086                 break;
1087         case SNDRV_PCM_STREAM_CAPTURE:
1088                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1089                                   KDATA_ADC1_REQUEST, 0);
1090                 break;
1091         }
1092         return 0;
1093 }
1094
1095 static int
1096 snd_m3_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
1097 {
1098         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1099         struct m3_dma *s = subs->runtime->private_data;
1100         int err = -EINVAL;
1101
1102         if (snd_BUG_ON(!s))
1103                 return -ENXIO;
1104
1105         spin_lock(&chip->reg_lock);
1106         switch (cmd) {
1107         case SNDRV_PCM_TRIGGER_START:
1108         case SNDRV_PCM_TRIGGER_RESUME:
1109                 if (s->running)
1110                         err = -EBUSY;
1111                 else {
1112                         s->running = 1;
1113                         err = snd_m3_pcm_start(chip, s, subs);
1114                 }
1115                 break;
1116         case SNDRV_PCM_TRIGGER_STOP:
1117         case SNDRV_PCM_TRIGGER_SUSPEND:
1118                 if (! s->running)
1119                         err = 0; /* should return error? */
1120                 else {
1121                         s->running = 0;
1122                         err = snd_m3_pcm_stop(chip, s, subs);
1123                 }
1124                 break;
1125         }
1126         spin_unlock(&chip->reg_lock);
1127         return err;
1128 }
1129
1130 /*
1131  * setup
1132  */
1133 static void 
1134 snd_m3_pcm_setup1(struct snd_m3 *chip, struct m3_dma *s, struct snd_pcm_substream *subs)
1135 {
1136         int dsp_in_size, dsp_out_size, dsp_in_buffer, dsp_out_buffer;
1137         struct snd_pcm_runtime *runtime = subs->runtime;
1138
1139         if (subs->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1140                 dsp_in_size = MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
1141                 dsp_out_size = MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
1142         } else {
1143                 dsp_in_size = MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
1144                 dsp_out_size = MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
1145         }
1146         dsp_in_buffer = s->inst.data + (MINISRC_TMP_BUFFER_SIZE / 2);
1147         dsp_out_buffer = dsp_in_buffer + (dsp_in_size / 2) + 1;
1148
1149         s->dma_size = frames_to_bytes(runtime, runtime->buffer_size);
1150         s->period_size = frames_to_bytes(runtime, runtime->period_size);
1151         s->hwptr = 0;
1152         s->count = 0;
1153
1154 #define LO(x) ((x) & 0xffff)
1155 #define HI(x) LO((x) >> 16)
1156
1157         /* host dma buffer pointers */
1158         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1159                           s->inst.data + CDATA_HOST_SRC_ADDRL,
1160                           LO(s->buffer_addr));
1161
1162         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1163                           s->inst.data + CDATA_HOST_SRC_ADDRH,
1164                           HI(s->buffer_addr));
1165
1166         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1167                           s->inst.data + CDATA_HOST_SRC_END_PLUS_1L,
1168                           LO(s->buffer_addr + s->dma_size));
1169
1170         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1171                           s->inst.data + CDATA_HOST_SRC_END_PLUS_1H,
1172                           HI(s->buffer_addr + s->dma_size));
1173
1174         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1175                           s->inst.data + CDATA_HOST_SRC_CURRENTL,
1176                           LO(s->buffer_addr));
1177
1178         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1179                           s->inst.data + CDATA_HOST_SRC_CURRENTH,
1180                           HI(s->buffer_addr));
1181 #undef LO
1182 #undef HI
1183
1184         /* dsp buffers */
1185
1186         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1187                           s->inst.data + CDATA_IN_BUF_BEGIN,
1188                           dsp_in_buffer);
1189
1190         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1191                           s->inst.data + CDATA_IN_BUF_END_PLUS_1,
1192                           dsp_in_buffer + (dsp_in_size / 2));
1193
1194         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1195                           s->inst.data + CDATA_IN_BUF_HEAD,
1196                           dsp_in_buffer);
1197     
1198         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1199                           s->inst.data + CDATA_IN_BUF_TAIL,
1200                           dsp_in_buffer);
1201
1202         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1203                           s->inst.data + CDATA_OUT_BUF_BEGIN,
1204                           dsp_out_buffer);
1205
1206         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1207                           s->inst.data + CDATA_OUT_BUF_END_PLUS_1,
1208                           dsp_out_buffer + (dsp_out_size / 2));
1209
1210         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1211                           s->inst.data + CDATA_OUT_BUF_HEAD,
1212                           dsp_out_buffer);
1213
1214         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1215                           s->inst.data + CDATA_OUT_BUF_TAIL,
1216                           dsp_out_buffer);
1217 }
1218
1219 static void snd_m3_pcm_setup2(struct snd_m3 *chip, struct m3_dma *s,
1220                               struct snd_pcm_runtime *runtime)
1221 {
1222         u32 freq;
1223
1224         /* 
1225          * put us in the lists if we're not already there
1226          */
1227         if (! s->in_lists) {
1228                 s->index[0] = snd_m3_add_list(chip, s->index_list[0],
1229                                               s->inst.data >> DP_SHIFT_COUNT);
1230                 s->index[1] = snd_m3_add_list(chip, s->index_list[1],
1231                                               s->inst.data >> DP_SHIFT_COUNT);
1232                 s->index[2] = snd_m3_add_list(chip, s->index_list[2],
1233                                               s->inst.data >> DP_SHIFT_COUNT);
1234                 s->in_lists = 1;
1235         }
1236
1237         /* write to 'mono' word */
1238         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1239                           s->inst.data + SRC3_DIRECTION_OFFSET + 1, 
1240                           runtime->channels == 2 ? 0 : 1);
1241         /* write to '8bit' word */
1242         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1243                           s->inst.data + SRC3_DIRECTION_OFFSET + 2, 
1244                           snd_pcm_format_width(runtime->format) == 16 ? 0 : 1);
1245
1246         /* set up dac/adc rate */
1247         freq = ((runtime->rate << 15) + 24000 ) / 48000;
1248         if (freq) 
1249                 freq--;
1250
1251         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1252                           s->inst.data + CDATA_FREQUENCY,
1253                           freq);
1254 }
1255
1256
1257 static const struct play_vals {
1258         u16 addr, val;
1259 } pv[] = {
1260         {CDATA_LEFT_VOLUME, ARB_VOLUME},
1261         {CDATA_RIGHT_VOLUME, ARB_VOLUME},
1262         {SRC3_DIRECTION_OFFSET, 0} ,
1263         /* +1, +2 are stereo/16 bit */
1264         {SRC3_DIRECTION_OFFSET + 3, 0x0000}, /* fraction? */
1265         {SRC3_DIRECTION_OFFSET + 4, 0}, /* first l */
1266         {SRC3_DIRECTION_OFFSET + 5, 0}, /* first r */
1267         {SRC3_DIRECTION_OFFSET + 6, 0}, /* second l */
1268         {SRC3_DIRECTION_OFFSET + 7, 0}, /* second r */
1269         {SRC3_DIRECTION_OFFSET + 8, 0}, /* delta l */
1270         {SRC3_DIRECTION_OFFSET + 9, 0}, /* delta r */
1271         {SRC3_DIRECTION_OFFSET + 10, 0x8000}, /* round */
1272         {SRC3_DIRECTION_OFFSET + 11, 0xFF00}, /* higher bute mark */
1273         {SRC3_DIRECTION_OFFSET + 13, 0}, /* temp0 */
1274         {SRC3_DIRECTION_OFFSET + 14, 0}, /* c fraction */
1275         {SRC3_DIRECTION_OFFSET + 15, 0}, /* counter */
1276         {SRC3_DIRECTION_OFFSET + 16, 8}, /* numin */
1277         {SRC3_DIRECTION_OFFSET + 17, 50*2}, /* numout */
1278         {SRC3_DIRECTION_OFFSET + 18, MINISRC_BIQUAD_STAGE - 1}, /* numstage */
1279         {SRC3_DIRECTION_OFFSET + 20, 0}, /* filtertap */
1280         {SRC3_DIRECTION_OFFSET + 21, 0} /* booster */
1281 };
1282
1283
1284 /* the mode passed should be already shifted and masked */
1285 static void
1286 snd_m3_playback_setup(struct snd_m3 *chip, struct m3_dma *s,
1287                       struct snd_pcm_substream *subs)
1288 {
1289         unsigned int i;
1290
1291         /*
1292          * some per client initializers
1293          */
1294
1295         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1296                           s->inst.data + SRC3_DIRECTION_OFFSET + 12,
1297                           s->inst.data + 40 + 8);
1298
1299         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1300                           s->inst.data + SRC3_DIRECTION_OFFSET + 19,
1301                           s->inst.code + MINISRC_COEF_LOC);
1302
1303         /* enable or disable low pass filter? */
1304         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1305                           s->inst.data + SRC3_DIRECTION_OFFSET + 22,
1306                           subs->runtime->rate > 45000 ? 0xff : 0);
1307     
1308         /* tell it which way dma is going? */
1309         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1310                           s->inst.data + CDATA_DMA_CONTROL,
1311                           DMACONTROL_AUTOREPEAT + DMAC_PAGE3_SELECTOR + DMAC_BLOCKF_SELECTOR);
1312
1313         /*
1314          * set an armload of static initializers
1315          */
1316         for (i = 0; i < ARRAY_SIZE(pv); i++) 
1317                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1318                                   s->inst.data + pv[i].addr, pv[i].val);
1319 }
1320
1321 /*
1322  *    Native record driver 
1323  */
1324 static const struct rec_vals {
1325         u16 addr, val;
1326 } rv[] = {
1327         {CDATA_LEFT_VOLUME, ARB_VOLUME},
1328         {CDATA_RIGHT_VOLUME, ARB_VOLUME},
1329         {SRC3_DIRECTION_OFFSET, 1} ,
1330         /* +1, +2 are stereo/16 bit */
1331         {SRC3_DIRECTION_OFFSET + 3, 0x0000}, /* fraction? */
1332         {SRC3_DIRECTION_OFFSET + 4, 0}, /* first l */
1333         {SRC3_DIRECTION_OFFSET + 5, 0}, /* first r */
1334         {SRC3_DIRECTION_OFFSET + 6, 0}, /* second l */
1335         {SRC3_DIRECTION_OFFSET + 7, 0}, /* second r */
1336         {SRC3_DIRECTION_OFFSET + 8, 0}, /* delta l */
1337         {SRC3_DIRECTION_OFFSET + 9, 0}, /* delta r */
1338         {SRC3_DIRECTION_OFFSET + 10, 0x8000}, /* round */
1339         {SRC3_DIRECTION_OFFSET + 11, 0xFF00}, /* higher bute mark */
1340         {SRC3_DIRECTION_OFFSET + 13, 0}, /* temp0 */
1341         {SRC3_DIRECTION_OFFSET + 14, 0}, /* c fraction */
1342         {SRC3_DIRECTION_OFFSET + 15, 0}, /* counter */
1343         {SRC3_DIRECTION_OFFSET + 16, 50},/* numin */
1344         {SRC3_DIRECTION_OFFSET + 17, 8}, /* numout */
1345         {SRC3_DIRECTION_OFFSET + 18, 0}, /* numstage */
1346         {SRC3_DIRECTION_OFFSET + 19, 0}, /* coef */
1347         {SRC3_DIRECTION_OFFSET + 20, 0}, /* filtertap */
1348         {SRC3_DIRECTION_OFFSET + 21, 0}, /* booster */
1349         {SRC3_DIRECTION_OFFSET + 22, 0xff} /* skip lpf */
1350 };
1351
1352 static void
1353 snd_m3_capture_setup(struct snd_m3 *chip, struct m3_dma *s, struct snd_pcm_substream *subs)
1354 {
1355         unsigned int i;
1356
1357         /*
1358          * some per client initializers
1359          */
1360
1361         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1362                           s->inst.data + SRC3_DIRECTION_OFFSET + 12,
1363                           s->inst.data + 40 + 8);
1364
1365         /* tell it which way dma is going? */
1366         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1367                           s->inst.data + CDATA_DMA_CONTROL,
1368                           DMACONTROL_DIRECTION + DMACONTROL_AUTOREPEAT + 
1369                           DMAC_PAGE3_SELECTOR + DMAC_BLOCKF_SELECTOR);
1370
1371         /*
1372          * set an armload of static initializers
1373          */
1374         for (i = 0; i < ARRAY_SIZE(rv); i++) 
1375                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
1376                                   s->inst.data + rv[i].addr, rv[i].val);
1377 }
1378
1379 static int snd_m3_pcm_hw_params(struct snd_pcm_substream *substream,
1380                                 struct snd_pcm_hw_params *hw_params)
1381 {
1382         struct m3_dma *s = substream->runtime->private_data;
1383
1384         /* set buffer address */
1385         s->buffer_addr = substream->runtime->dma_addr;
1386         if (s->buffer_addr & 0x3) {
1387                 dev_err(substream->pcm->card->dev, "oh my, not aligned\n");
1388                 s->buffer_addr = s->buffer_addr & ~0x3;
1389         }
1390         return 0;
1391 }
1392
1393 static int snd_m3_pcm_hw_free(struct snd_pcm_substream *substream)
1394 {
1395         struct m3_dma *s;
1396         
1397         if (substream->runtime->private_data == NULL)
1398                 return 0;
1399         s = substream->runtime->private_data;
1400         s->buffer_addr = 0;
1401         return 0;
1402 }
1403
1404 static int
1405 snd_m3_pcm_prepare(struct snd_pcm_substream *subs)
1406 {
1407         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1408         struct snd_pcm_runtime *runtime = subs->runtime;
1409         struct m3_dma *s = runtime->private_data;
1410
1411         if (snd_BUG_ON(!s))
1412                 return -ENXIO;
1413
1414         if (runtime->format != SNDRV_PCM_FORMAT_U8 &&
1415             runtime->format != SNDRV_PCM_FORMAT_S16_LE)
1416                 return -EINVAL;
1417         if (runtime->rate > 48000 ||
1418             runtime->rate < 8000)
1419                 return -EINVAL;
1420
1421         spin_lock_irq(&chip->reg_lock);
1422
1423         snd_m3_pcm_setup1(chip, s, subs);
1424
1425         if (subs->stream == SNDRV_PCM_STREAM_PLAYBACK)
1426                 snd_m3_playback_setup(chip, s, subs);
1427         else
1428                 snd_m3_capture_setup(chip, s, subs);
1429
1430         snd_m3_pcm_setup2(chip, s, runtime);
1431
1432         spin_unlock_irq(&chip->reg_lock);
1433
1434         return 0;
1435 }
1436
1437 /*
1438  * get current pointer
1439  */
1440 static unsigned int
1441 snd_m3_get_pointer(struct snd_m3 *chip, struct m3_dma *s, struct snd_pcm_substream *subs)
1442 {
1443         u16 hi = 0, lo = 0;
1444         int retry = 10;
1445         u32 addr;
1446
1447         /*
1448          * try and get a valid answer
1449          */
1450         while (retry--) {
1451                 hi =  snd_m3_assp_read(chip, MEMTYPE_INTERNAL_DATA,
1452                                        s->inst.data + CDATA_HOST_SRC_CURRENTH);
1453
1454                 lo = snd_m3_assp_read(chip, MEMTYPE_INTERNAL_DATA,
1455                                       s->inst.data + CDATA_HOST_SRC_CURRENTL);
1456
1457                 if (hi == snd_m3_assp_read(chip, MEMTYPE_INTERNAL_DATA,
1458                                            s->inst.data + CDATA_HOST_SRC_CURRENTH))
1459                         break;
1460         }
1461         addr = lo | ((u32)hi<<16);
1462         return (unsigned int)(addr - s->buffer_addr);
1463 }
1464
1465 static snd_pcm_uframes_t
1466 snd_m3_pcm_pointer(struct snd_pcm_substream *subs)
1467 {
1468         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1469         unsigned int ptr;
1470         struct m3_dma *s = subs->runtime->private_data;
1471
1472         if (snd_BUG_ON(!s))
1473                 return 0;
1474
1475         spin_lock(&chip->reg_lock);
1476         ptr = snd_m3_get_pointer(chip, s, subs);
1477         spin_unlock(&chip->reg_lock);
1478         return bytes_to_frames(subs->runtime, ptr);
1479 }
1480
1481
1482 /* update pointer */
1483 /* spinlock held! */
1484 static void snd_m3_update_ptr(struct snd_m3 *chip, struct m3_dma *s)
1485 {
1486         struct snd_pcm_substream *subs = s->substream;
1487         unsigned int hwptr;
1488         int diff;
1489
1490         if (! s->running)
1491                 return;
1492
1493         hwptr = snd_m3_get_pointer(chip, s, subs);
1494
1495         /* try to avoid expensive modulo divisions */
1496         if (hwptr >= s->dma_size)
1497                 hwptr %= s->dma_size;
1498
1499         diff = s->dma_size + hwptr - s->hwptr;
1500         if (diff >= s->dma_size)
1501                 diff %= s->dma_size;
1502
1503         s->hwptr = hwptr;
1504         s->count += diff;
1505
1506         if (s->count >= (signed)s->period_size) {
1507
1508                 if (s->count < 2 * (signed)s->period_size)
1509                         s->count -= (signed)s->period_size;
1510                 else
1511                         s->count %= s->period_size;
1512
1513                 spin_unlock(&chip->reg_lock);
1514                 snd_pcm_period_elapsed(subs);
1515                 spin_lock(&chip->reg_lock);
1516         }
1517 }
1518
1519 /* The m3's hardware volume works by incrementing / decrementing 2 counters
1520    (without wrap around) in response to volume button presses and then
1521    generating an interrupt. The pair of counters is stored in bits 1-3 and 5-7
1522    of a byte wide register. The meaning of bits 0 and 4 is unknown. */
1523 static void snd_m3_update_hw_volume(struct work_struct *work)
1524 {
1525         struct snd_m3 *chip = container_of(work, struct snd_m3, hwvol_work);
1526         int x, val;
1527
1528         /* Figure out which volume control button was pushed,
1529            based on differences from the default register
1530            values. */
1531         x = inb(chip->iobase + SHADOW_MIX_REG_VOICE) & 0xee;
1532
1533         /* Reset the volume counters to 4. Tests on the allegro integrated
1534            into a Compaq N600C laptop, have revealed that:
1535            1) Writing any value will result in the 2 counters being reset to
1536               4 so writing 0x88 is not strictly necessary
1537            2) Writing to any of the 4 involved registers will reset all 4
1538               of them (and reading them always returns the same value for all
1539               of them)
1540            It could be that a maestro deviates from this, so leave the code
1541            as is. */
1542         outb(0x88, chip->iobase + SHADOW_MIX_REG_VOICE);
1543         outb(0x88, chip->iobase + HW_VOL_COUNTER_VOICE);
1544         outb(0x88, chip->iobase + SHADOW_MIX_REG_MASTER);
1545         outb(0x88, chip->iobase + HW_VOL_COUNTER_MASTER);
1546
1547         /* Ignore spurious HV interrupts during suspend / resume, this avoids
1548            mistaking them for a mute button press. */
1549         if (chip->in_suspend)
1550                 return;
1551
1552 #ifndef CONFIG_SND_MAESTRO3_INPUT
1553         if (!chip->master_switch || !chip->master_volume)
1554                 return;
1555
1556         val = snd_ac97_read(chip->ac97, AC97_MASTER);
1557         switch (x) {
1558         case 0x88:
1559                 /* The counters have not changed, yet we've received a HV
1560                    interrupt. According to tests run by various people this
1561                    happens when pressing the mute button. */
1562                 val ^= 0x8000;
1563                 break;
1564         case 0xaa:
1565                 /* counters increased by 1 -> volume up */
1566                 if ((val & 0x7f) > 0)
1567                         val--;
1568                 if ((val & 0x7f00) > 0)
1569                         val -= 0x0100;
1570                 break;
1571         case 0x66:
1572                 /* counters decreased by 1 -> volume down */
1573                 if ((val & 0x7f) < 0x1f)
1574                         val++;
1575                 if ((val & 0x7f00) < 0x1f00)
1576                         val += 0x0100;
1577                 break;
1578         }
1579         if (snd_ac97_update(chip->ac97, AC97_MASTER, val))
1580                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1581                                &chip->master_switch->id);
1582 #else
1583         if (!chip->input_dev)
1584                 return;
1585
1586         val = 0;
1587         switch (x) {
1588         case 0x88:
1589                 /* The counters have not changed, yet we've received a HV
1590                    interrupt. According to tests run by various people this
1591                    happens when pressing the mute button. */
1592                 val = KEY_MUTE;
1593                 break;
1594         case 0xaa:
1595                 /* counters increased by 1 -> volume up */
1596                 val = KEY_VOLUMEUP;
1597                 break;
1598         case 0x66:
1599                 /* counters decreased by 1 -> volume down */
1600                 val = KEY_VOLUMEDOWN;
1601                 break;
1602         }
1603
1604         if (val) {
1605                 input_report_key(chip->input_dev, val, 1);
1606                 input_sync(chip->input_dev);
1607                 input_report_key(chip->input_dev, val, 0);
1608                 input_sync(chip->input_dev);
1609         }
1610 #endif
1611 }
1612
1613 static irqreturn_t snd_m3_interrupt(int irq, void *dev_id)
1614 {
1615         struct snd_m3 *chip = dev_id;
1616         u8 status;
1617         int i;
1618
1619         status = inb(chip->iobase + HOST_INT_STATUS);
1620
1621         if (status == 0xff)
1622                 return IRQ_NONE;
1623
1624         if (status & HV_INT_PENDING)
1625                 schedule_work(&chip->hwvol_work);
1626
1627         /*
1628          * ack an assp int if its running
1629          * and has an int pending
1630          */
1631         if (status & ASSP_INT_PENDING) {
1632                 u8 ctl = inb(chip->iobase + ASSP_CONTROL_B);
1633                 if (!(ctl & STOP_ASSP_CLOCK)) {
1634                         ctl = inb(chip->iobase + ASSP_HOST_INT_STATUS);
1635                         if (ctl & DSP2HOST_REQ_TIMER) {
1636                                 outb(DSP2HOST_REQ_TIMER, chip->iobase + ASSP_HOST_INT_STATUS);
1637                                 /* update adc/dac info if it was a timer int */
1638                                 spin_lock(&chip->reg_lock);
1639                                 for (i = 0; i < chip->num_substreams; i++) {
1640                                         struct m3_dma *s = &chip->substreams[i];
1641                                         if (s->running)
1642                                                 snd_m3_update_ptr(chip, s);
1643                                 }
1644                                 spin_unlock(&chip->reg_lock);
1645                         }
1646                 }
1647         }
1648
1649 #if 0 /* TODO: not supported yet */
1650         if ((status & MPU401_INT_PENDING) && chip->rmidi)
1651                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
1652 #endif
1653
1654         /* ack ints */
1655         outb(status, chip->iobase + HOST_INT_STATUS);
1656
1657         return IRQ_HANDLED;
1658 }
1659
1660
1661 /*
1662  */
1663
1664 static const struct snd_pcm_hardware snd_m3_playback =
1665 {
1666         .info =                 (SNDRV_PCM_INFO_MMAP |
1667                                  SNDRV_PCM_INFO_INTERLEAVED |
1668                                  SNDRV_PCM_INFO_MMAP_VALID |
1669                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1670                                  /*SNDRV_PCM_INFO_PAUSE |*/
1671                                  SNDRV_PCM_INFO_RESUME),
1672         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1673         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1674         .rate_min =             8000,
1675         .rate_max =             48000,
1676         .channels_min =         1,
1677         .channels_max =         2,
1678         .buffer_bytes_max =     (512*1024),
1679         .period_bytes_min =     64,
1680         .period_bytes_max =     (512*1024),
1681         .periods_min =          1,
1682         .periods_max =          1024,
1683 };
1684
1685 static const struct snd_pcm_hardware snd_m3_capture =
1686 {
1687         .info =                 (SNDRV_PCM_INFO_MMAP |
1688                                  SNDRV_PCM_INFO_INTERLEAVED |
1689                                  SNDRV_PCM_INFO_MMAP_VALID |
1690                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1691                                  /*SNDRV_PCM_INFO_PAUSE |*/
1692                                  SNDRV_PCM_INFO_RESUME),
1693         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1694         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1695         .rate_min =             8000,
1696         .rate_max =             48000,
1697         .channels_min =         1,
1698         .channels_max =         2,
1699         .buffer_bytes_max =     (512*1024),
1700         .period_bytes_min =     64,
1701         .period_bytes_max =     (512*1024),
1702         .periods_min =          1,
1703         .periods_max =          1024,
1704 };
1705
1706
1707 /*
1708  */
1709
1710 static int
1711 snd_m3_substream_open(struct snd_m3 *chip, struct snd_pcm_substream *subs)
1712 {
1713         int i;
1714         struct m3_dma *s;
1715
1716         spin_lock_irq(&chip->reg_lock);
1717         for (i = 0; i < chip->num_substreams; i++) {
1718                 s = &chip->substreams[i];
1719                 if (! s->opened)
1720                         goto __found;
1721         }
1722         spin_unlock_irq(&chip->reg_lock);
1723         return -ENOMEM;
1724 __found:
1725         s->opened = 1;
1726         s->running = 0;
1727         spin_unlock_irq(&chip->reg_lock);
1728
1729         subs->runtime->private_data = s;
1730         s->substream = subs;
1731
1732         /* set list owners */
1733         if (subs->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1734                 s->index_list[0] = &chip->mixer_list;
1735         } else
1736                 s->index_list[0] = &chip->adc1_list;
1737         s->index_list[1] = &chip->msrc_list;
1738         s->index_list[2] = &chip->dma_list;
1739
1740         return 0;
1741 }
1742
1743 static void
1744 snd_m3_substream_close(struct snd_m3 *chip, struct snd_pcm_substream *subs)
1745 {
1746         struct m3_dma *s = subs->runtime->private_data;
1747
1748         if (s == NULL)
1749                 return; /* not opened properly */
1750
1751         spin_lock_irq(&chip->reg_lock);
1752         if (s->substream && s->running)
1753                 snd_m3_pcm_stop(chip, s, s->substream); /* does this happen? */
1754         if (s->in_lists) {
1755                 snd_m3_remove_list(chip, s->index_list[0], s->index[0]);
1756                 snd_m3_remove_list(chip, s->index_list[1], s->index[1]);
1757                 snd_m3_remove_list(chip, s->index_list[2], s->index[2]);
1758                 s->in_lists = 0;
1759         }
1760         s->running = 0;
1761         s->opened = 0;
1762         spin_unlock_irq(&chip->reg_lock);
1763 }
1764
1765 static int
1766 snd_m3_playback_open(struct snd_pcm_substream *subs)
1767 {
1768         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1769         struct snd_pcm_runtime *runtime = subs->runtime;
1770         int err;
1771
1772         if ((err = snd_m3_substream_open(chip, subs)) < 0)
1773                 return err;
1774
1775         runtime->hw = snd_m3_playback;
1776
1777         return 0;
1778 }
1779
1780 static int
1781 snd_m3_playback_close(struct snd_pcm_substream *subs)
1782 {
1783         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1784
1785         snd_m3_substream_close(chip, subs);
1786         return 0;
1787 }
1788
1789 static int
1790 snd_m3_capture_open(struct snd_pcm_substream *subs)
1791 {
1792         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1793         struct snd_pcm_runtime *runtime = subs->runtime;
1794         int err;
1795
1796         if ((err = snd_m3_substream_open(chip, subs)) < 0)
1797                 return err;
1798
1799         runtime->hw = snd_m3_capture;
1800
1801         return 0;
1802 }
1803
1804 static int
1805 snd_m3_capture_close(struct snd_pcm_substream *subs)
1806 {
1807         struct snd_m3 *chip = snd_pcm_substream_chip(subs);
1808
1809         snd_m3_substream_close(chip, subs);
1810         return 0;
1811 }
1812
1813 /*
1814  * create pcm instance
1815  */
1816
1817 static const struct snd_pcm_ops snd_m3_playback_ops = {
1818         .open =         snd_m3_playback_open,
1819         .close =        snd_m3_playback_close,
1820         .hw_params =    snd_m3_pcm_hw_params,
1821         .hw_free =      snd_m3_pcm_hw_free,
1822         .prepare =      snd_m3_pcm_prepare,
1823         .trigger =      snd_m3_pcm_trigger,
1824         .pointer =      snd_m3_pcm_pointer,
1825 };
1826
1827 static const struct snd_pcm_ops snd_m3_capture_ops = {
1828         .open =         snd_m3_capture_open,
1829         .close =        snd_m3_capture_close,
1830         .hw_params =    snd_m3_pcm_hw_params,
1831         .hw_free =      snd_m3_pcm_hw_free,
1832         .prepare =      snd_m3_pcm_prepare,
1833         .trigger =      snd_m3_pcm_trigger,
1834         .pointer =      snd_m3_pcm_pointer,
1835 };
1836
1837 static int
1838 snd_m3_pcm(struct snd_m3 * chip, int device)
1839 {
1840         struct snd_pcm *pcm;
1841         int err;
1842
1843         err = snd_pcm_new(chip->card, chip->card->driver, device,
1844                           MAX_PLAYBACKS, MAX_CAPTURES, &pcm);
1845         if (err < 0)
1846                 return err;
1847
1848         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_m3_playback_ops);
1849         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_m3_capture_ops);
1850
1851         pcm->private_data = chip;
1852         pcm->info_flags = 0;
1853         strcpy(pcm->name, chip->card->driver);
1854         chip->pcm = pcm;
1855         
1856         snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1857                                        &chip->pci->dev, 64*1024, 64*1024);
1858
1859         return 0;
1860 }
1861
1862
1863 /*
1864  * ac97 interface
1865  */
1866
1867 /*
1868  * Wait for the ac97 serial bus to be free.
1869  * return nonzero if the bus is still busy.
1870  */
1871 static int snd_m3_ac97_wait(struct snd_m3 *chip)
1872 {
1873         int i = 10000;
1874
1875         do {
1876                 if (! (snd_m3_inb(chip, 0x30) & 1))
1877                         return 0;
1878                 cpu_relax();
1879         } while (i-- > 0);
1880
1881         dev_err(chip->card->dev, "ac97 serial bus busy\n");
1882         return 1;
1883 }
1884
1885 static unsigned short
1886 snd_m3_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
1887 {
1888         struct snd_m3 *chip = ac97->private_data;
1889         unsigned short data = 0xffff;
1890
1891         if (snd_m3_ac97_wait(chip))
1892                 goto fail;
1893         snd_m3_outb(chip, 0x80 | (reg & 0x7f), CODEC_COMMAND);
1894         if (snd_m3_ac97_wait(chip))
1895                 goto fail;
1896         data = snd_m3_inw(chip, CODEC_DATA);
1897 fail:
1898         return data;
1899 }
1900
1901 static void
1902 snd_m3_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
1903 {
1904         struct snd_m3 *chip = ac97->private_data;
1905
1906         if (snd_m3_ac97_wait(chip))
1907                 return;
1908         snd_m3_outw(chip, val, CODEC_DATA);
1909         snd_m3_outb(chip, reg & 0x7f, CODEC_COMMAND);
1910         /*
1911          * Workaround for buggy ES1988 integrated AC'97 codec. It remains silent
1912          * until the MASTER volume or mute is touched (alsactl restore does not
1913          * work).
1914          */
1915         if (ac97->id == 0x45838308 && reg == AC97_MASTER) {
1916                 snd_m3_ac97_wait(chip);
1917                 snd_m3_outw(chip, val, CODEC_DATA);
1918                 snd_m3_outb(chip, reg & 0x7f, CODEC_COMMAND);
1919         }
1920 }
1921
1922
1923 static void snd_m3_remote_codec_config(struct snd_m3 *chip, int isremote)
1924 {
1925         int io = chip->iobase;
1926         u16 tmp;
1927
1928         isremote = isremote ? 1 : 0;
1929
1930         tmp = inw(io + RING_BUS_CTRL_B) & ~SECOND_CODEC_ID_MASK;
1931         /* enable dock on Dell Latitude C810 */
1932         if (chip->pci->subsystem_vendor == 0x1028 &&
1933             chip->pci->subsystem_device == 0x00e5)
1934                 tmp |= M3I_DOCK_ENABLE;
1935         outw(tmp | isremote, io + RING_BUS_CTRL_B);
1936         outw((inw(io + SDO_OUT_DEST_CTRL) & ~COMMAND_ADDR_OUT) | isremote,
1937              io + SDO_OUT_DEST_CTRL);
1938         outw((inw(io + SDO_IN_DEST_CTRL) & ~STATUS_ADDR_IN) | isremote,
1939              io + SDO_IN_DEST_CTRL);
1940 }
1941
1942 /* 
1943  * hack, returns non zero on err 
1944  */
1945 static int snd_m3_try_read_vendor(struct snd_m3 *chip)
1946 {
1947         u16 ret;
1948
1949         if (snd_m3_ac97_wait(chip))
1950                 return 1;
1951
1952         snd_m3_outb(chip, 0x80 | (AC97_VENDOR_ID1 & 0x7f), 0x30);
1953
1954         if (snd_m3_ac97_wait(chip))
1955                 return 1;
1956
1957         ret = snd_m3_inw(chip, 0x32);
1958
1959         return (ret == 0) || (ret == 0xffff);
1960 }
1961
1962 static void snd_m3_ac97_reset(struct snd_m3 *chip)
1963 {
1964         u16 dir;
1965         int delay1 = 0, delay2 = 0, i;
1966         int io = chip->iobase;
1967
1968         if (chip->allegro_flag) {
1969                 /*
1970                  * the onboard codec on the allegro seems 
1971                  * to want to wait a very long time before
1972                  * coming back to life 
1973                  */
1974                 delay1 = 50;
1975                 delay2 = 800;
1976         } else {
1977                 /* maestro3 */
1978                 delay1 = 20;
1979                 delay2 = 500;
1980         }
1981
1982         for (i = 0; i < 5; i++) {
1983                 dir = inw(io + GPIO_DIRECTION);
1984                 if (!chip->irda_workaround)
1985                         dir |= 0x10; /* assuming pci bus master? */
1986
1987                 snd_m3_remote_codec_config(chip, 0);
1988
1989                 outw(IO_SRAM_ENABLE, io + RING_BUS_CTRL_A);
1990                 udelay(20);
1991
1992                 outw(dir & ~GPO_PRIMARY_AC97 , io + GPIO_DIRECTION);
1993                 outw(~GPO_PRIMARY_AC97 , io + GPIO_MASK);
1994                 outw(0, io + GPIO_DATA);
1995                 outw(dir | GPO_PRIMARY_AC97, io + GPIO_DIRECTION);
1996
1997                 schedule_timeout_uninterruptible(msecs_to_jiffies(delay1));
1998
1999                 outw(GPO_PRIMARY_AC97, io + GPIO_DATA);
2000                 udelay(5);
2001                 /* ok, bring back the ac-link */
2002                 outw(IO_SRAM_ENABLE | SERIAL_AC_LINK_ENABLE, io + RING_BUS_CTRL_A);
2003                 outw(~0, io + GPIO_MASK);
2004
2005                 schedule_timeout_uninterruptible(msecs_to_jiffies(delay2));
2006
2007                 if (! snd_m3_try_read_vendor(chip))
2008                         break;
2009
2010                 delay1 += 10;
2011                 delay2 += 100;
2012
2013                 dev_dbg(chip->card->dev,
2014                         "retrying codec reset with delays of %d and %d ms\n",
2015                            delay1, delay2);
2016         }
2017
2018 #if 0
2019         /* more gung-ho reset that doesn't
2020          * seem to work anywhere :)
2021          */
2022         tmp = inw(io + RING_BUS_CTRL_A);
2023         outw(RAC_SDFS_ENABLE|LAC_SDFS_ENABLE, io + RING_BUS_CTRL_A);
2024         msleep(20);
2025         outw(tmp, io + RING_BUS_CTRL_A);
2026         msleep(50);
2027 #endif
2028 }
2029
2030 static int snd_m3_mixer(struct snd_m3 *chip)
2031 {
2032         struct snd_ac97_bus *pbus;
2033         struct snd_ac97_template ac97;
2034 #ifndef CONFIG_SND_MAESTRO3_INPUT
2035         struct snd_ctl_elem_id elem_id;
2036 #endif
2037         int err;
2038         static const struct snd_ac97_bus_ops ops = {
2039                 .write = snd_m3_ac97_write,
2040                 .read = snd_m3_ac97_read,
2041         };
2042
2043         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
2044                 return err;
2045         
2046         memset(&ac97, 0, sizeof(ac97));
2047         ac97.private_data = chip;
2048         if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2049                 return err;
2050
2051         /* seems ac97 PCM needs initialization.. hack hack.. */
2052         snd_ac97_write(chip->ac97, AC97_PCM, 0x8000 | (15 << 8) | 15);
2053         schedule_timeout_uninterruptible(msecs_to_jiffies(100));
2054         snd_ac97_write(chip->ac97, AC97_PCM, 0);
2055
2056 #ifndef CONFIG_SND_MAESTRO3_INPUT
2057         memset(&elem_id, 0, sizeof(elem_id));
2058         elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2059         strcpy(elem_id.name, "Master Playback Switch");
2060         chip->master_switch = snd_ctl_find_id(chip->card, &elem_id);
2061         memset(&elem_id, 0, sizeof(elem_id));
2062         elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2063         strcpy(elem_id.name, "Master Playback Volume");
2064         chip->master_volume = snd_ctl_find_id(chip->card, &elem_id);
2065 #endif
2066
2067         return 0;
2068 }
2069
2070
2071 /*
2072  * initialize ASSP
2073  */
2074
2075 #define MINISRC_LPF_LEN 10
2076 static const u16 minisrc_lpf[MINISRC_LPF_LEN] = {
2077         0X0743, 0X1104, 0X0A4C, 0XF88D, 0X242C,
2078         0X1023, 0X1AA9, 0X0B60, 0XEFDD, 0X186F
2079 };
2080
2081 static void snd_m3_assp_init(struct snd_m3 *chip)
2082 {
2083         unsigned int i;
2084         const __le16 *data;
2085
2086         /* zero kernel data */
2087         for (i = 0; i < (REV_B_DATA_MEMORY_UNIT_LENGTH * NUM_UNITS_KERNEL_DATA) / 2; i++)
2088                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA, 
2089                                   KDATA_BASE_ADDR + i, 0);
2090
2091         /* zero mixer data? */
2092         for (i = 0; i < (REV_B_DATA_MEMORY_UNIT_LENGTH * NUM_UNITS_KERNEL_DATA) / 2; i++)
2093                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2094                                   KDATA_BASE_ADDR2 + i, 0);
2095
2096         /* init dma pointer */
2097         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2098                           KDATA_CURRENT_DMA,
2099                           KDATA_DMA_XFER0);
2100
2101         /* write kernel into code memory.. */
2102         data = (const __le16 *)chip->assp_kernel_image->data;
2103         for (i = 0 ; i * 2 < chip->assp_kernel_image->size; i++) {
2104                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_CODE, 
2105                                   REV_B_CODE_MEMORY_BEGIN + i,
2106                                   le16_to_cpu(data[i]));
2107         }
2108
2109         /*
2110          * We only have this one client and we know that 0x400
2111          * is free in our kernel's mem map, so lets just
2112          * drop it there.  It seems that the minisrc doesn't
2113          * need vectors, so we won't bother with them..
2114          */
2115         data = (const __le16 *)chip->assp_minisrc_image->data;
2116         for (i = 0; i * 2 < chip->assp_minisrc_image->size; i++) {
2117                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_CODE, 
2118                                   0x400 + i, le16_to_cpu(data[i]));
2119         }
2120
2121         /*
2122          * write the coefficients for the low pass filter?
2123          */
2124         for (i = 0; i < MINISRC_LPF_LEN ; i++) {
2125                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_CODE,
2126                                   0x400 + MINISRC_COEF_LOC + i,
2127                                   minisrc_lpf[i]);
2128         }
2129
2130         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_CODE,
2131                           0x400 + MINISRC_COEF_LOC + MINISRC_LPF_LEN,
2132                           0x8000);
2133
2134         /*
2135          * the minisrc is the only thing on
2136          * our task list..
2137          */
2138         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA, 
2139                           KDATA_TASK0,
2140                           0x400);
2141
2142         /*
2143          * init the mixer number..
2144          */
2145
2146         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2147                           KDATA_MIXER_TASK_NUMBER,0);
2148
2149         /*
2150          * EXTREME KERNEL MASTER VOLUME
2151          */
2152         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2153                           KDATA_DAC_LEFT_VOLUME, ARB_VOLUME);
2154         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2155                           KDATA_DAC_RIGHT_VOLUME, ARB_VOLUME);
2156
2157         chip->mixer_list.curlen = 0;
2158         chip->mixer_list.mem_addr = KDATA_MIXER_XFER0;
2159         chip->mixer_list.max = MAX_VIRTUAL_MIXER_CHANNELS;
2160         chip->adc1_list.curlen = 0;
2161         chip->adc1_list.mem_addr = KDATA_ADC1_XFER0;
2162         chip->adc1_list.max = MAX_VIRTUAL_ADC1_CHANNELS;
2163         chip->dma_list.curlen = 0;
2164         chip->dma_list.mem_addr = KDATA_DMA_XFER0;
2165         chip->dma_list.max = MAX_VIRTUAL_DMA_CHANNELS;
2166         chip->msrc_list.curlen = 0;
2167         chip->msrc_list.mem_addr = KDATA_INSTANCE0_MINISRC;
2168         chip->msrc_list.max = MAX_INSTANCE_MINISRC;
2169 }
2170
2171
2172 static int snd_m3_assp_client_init(struct snd_m3 *chip, struct m3_dma *s, int index)
2173 {
2174         int data_bytes = 2 * ( MINISRC_TMP_BUFFER_SIZE / 2 + 
2175                                MINISRC_IN_BUFFER_SIZE / 2 +
2176                                1 + MINISRC_OUT_BUFFER_SIZE / 2 + 1 );
2177         int address, i;
2178
2179         /*
2180          * the revb memory map has 0x1100 through 0x1c00
2181          * free.  
2182          */
2183
2184         /*
2185          * align instance address to 256 bytes so that its
2186          * shifted list address is aligned.
2187          * list address = (mem address >> 1) >> 7;
2188          */
2189         data_bytes = ALIGN(data_bytes, 256);
2190         address = 0x1100 + ((data_bytes/2) * index);
2191
2192         if ((address + (data_bytes/2)) >= 0x1c00) {
2193                 dev_err(chip->card->dev,
2194                         "no memory for %d bytes at ind %d (addr 0x%x)\n",
2195                            data_bytes, index, address);
2196                 return -ENOMEM;
2197         }
2198
2199         s->number = index;
2200         s->inst.code = 0x400;
2201         s->inst.data = address;
2202
2203         for (i = data_bytes / 2; i > 0; address++, i--) {
2204                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA,
2205                                   address, 0);
2206         }
2207
2208         return 0;
2209 }
2210
2211
2212 /* 
2213  * this works for the reference board, have to find
2214  * out about others
2215  *
2216  * this needs more magic for 4 speaker, but..
2217  */
2218 static void
2219 snd_m3_amp_enable(struct snd_m3 *chip, int enable)
2220 {
2221         int io = chip->iobase;
2222         u16 gpo, polarity;
2223
2224         if (! chip->external_amp)
2225                 return;
2226
2227         polarity = enable ? 0 : 1;
2228         polarity = polarity << chip->amp_gpio;
2229         gpo = 1 << chip->amp_gpio;
2230
2231         outw(~gpo, io + GPIO_MASK);
2232
2233         outw(inw(io + GPIO_DIRECTION) | gpo,
2234              io + GPIO_DIRECTION);
2235
2236         outw((GPO_SECONDARY_AC97 | GPO_PRIMARY_AC97 | polarity),
2237              io + GPIO_DATA);
2238
2239         outw(0xffff, io + GPIO_MASK);
2240 }
2241
2242 static void
2243 snd_m3_hv_init(struct snd_m3 *chip)
2244 {
2245         unsigned long io = chip->iobase;
2246         u16 val = GPI_VOL_DOWN | GPI_VOL_UP;
2247
2248         if (!chip->is_omnibook)
2249                 return;
2250
2251         /*
2252          * Volume buttons on some HP OmniBook laptops
2253          * require some GPIO magic to work correctly.
2254          */
2255         outw(0xffff, io + GPIO_MASK);
2256         outw(0x0000, io + GPIO_DATA);
2257
2258         outw(~val, io + GPIO_MASK);
2259         outw(inw(io + GPIO_DIRECTION) & ~val, io + GPIO_DIRECTION);
2260         outw(val, io + GPIO_MASK);
2261
2262         outw(0xffff, io + GPIO_MASK);
2263 }
2264
2265 static int
2266 snd_m3_chip_init(struct snd_m3 *chip)
2267 {
2268         struct pci_dev *pcidev = chip->pci;
2269         unsigned long io = chip->iobase;
2270         u32 n;
2271         u16 w;
2272         u8 t; /* makes as much sense as 'n', no? */
2273
2274         pci_read_config_word(pcidev, PCI_LEGACY_AUDIO_CTRL, &w);
2275         w &= ~(SOUND_BLASTER_ENABLE|FM_SYNTHESIS_ENABLE|
2276                MPU401_IO_ENABLE|MPU401_IRQ_ENABLE|ALIAS_10BIT_IO|
2277                DISABLE_LEGACY);
2278         pci_write_config_word(pcidev, PCI_LEGACY_AUDIO_CTRL, w);
2279
2280         pci_read_config_dword(pcidev, PCI_ALLEGRO_CONFIG, &n);
2281         n &= ~(HV_CTRL_ENABLE | REDUCED_DEBOUNCE | HV_BUTTON_FROM_GD);
2282         n |= chip->hv_config;
2283         /* For some reason we must always use reduced debounce. */
2284         n |= REDUCED_DEBOUNCE;
2285         n |= PM_CTRL_ENABLE | CLK_DIV_BY_49 | USE_PCI_TIMING;
2286         pci_write_config_dword(pcidev, PCI_ALLEGRO_CONFIG, n);
2287
2288         outb(RESET_ASSP, chip->iobase + ASSP_CONTROL_B);
2289         pci_read_config_dword(pcidev, PCI_ALLEGRO_CONFIG, &n);
2290         n &= ~INT_CLK_SELECT;
2291         if (!chip->allegro_flag) {
2292                 n &= ~INT_CLK_MULT_ENABLE; 
2293                 n |= INT_CLK_SRC_NOT_PCI;
2294         }
2295         n &=  ~( CLK_MULT_MODE_SELECT | CLK_MULT_MODE_SELECT_2 );
2296         pci_write_config_dword(pcidev, PCI_ALLEGRO_CONFIG, n);
2297
2298         if (chip->allegro_flag) {
2299                 pci_read_config_dword(pcidev, PCI_USER_CONFIG, &n);
2300                 n |= IN_CLK_12MHZ_SELECT;
2301                 pci_write_config_dword(pcidev, PCI_USER_CONFIG, n);
2302         }
2303
2304         t = inb(chip->iobase + ASSP_CONTROL_A);
2305         t &= ~( DSP_CLK_36MHZ_SELECT  | ASSP_CLK_49MHZ_SELECT);
2306         t |= ASSP_CLK_49MHZ_SELECT;
2307         t |= ASSP_0_WS_ENABLE; 
2308         outb(t, chip->iobase + ASSP_CONTROL_A);
2309
2310         snd_m3_assp_init(chip); /* download DSP code before starting ASSP below */
2311         outb(RUN_ASSP, chip->iobase + ASSP_CONTROL_B); 
2312
2313         outb(0x00, io + HARDWARE_VOL_CTRL);
2314         outb(0x88, io + SHADOW_MIX_REG_VOICE);
2315         outb(0x88, io + HW_VOL_COUNTER_VOICE);
2316         outb(0x88, io + SHADOW_MIX_REG_MASTER);
2317         outb(0x88, io + HW_VOL_COUNTER_MASTER);
2318
2319         return 0;
2320
2321
2322 static void
2323 snd_m3_enable_ints(struct snd_m3 *chip)
2324 {
2325         unsigned long io = chip->iobase;
2326         unsigned short val;
2327
2328         /* TODO: MPU401 not supported yet */
2329         val = ASSP_INT_ENABLE /*| MPU401_INT_ENABLE*/;
2330         if (chip->hv_config & HV_CTRL_ENABLE)
2331                 val |= HV_INT_ENABLE;
2332         outb(val, chip->iobase + HOST_INT_STATUS);
2333         outw(val, io + HOST_INT_CTRL);
2334         outb(inb(io + ASSP_CONTROL_C) | ASSP_HOST_INT_ENABLE,
2335              io + ASSP_CONTROL_C);
2336 }
2337
2338
2339 /*
2340  */
2341
2342 static int snd_m3_free(struct snd_m3 *chip)
2343 {
2344         struct m3_dma *s;
2345         int i;
2346
2347         cancel_work_sync(&chip->hwvol_work);
2348 #ifdef CONFIG_SND_MAESTRO3_INPUT
2349         if (chip->input_dev)
2350                 input_unregister_device(chip->input_dev);
2351 #endif
2352
2353         if (chip->substreams) {
2354                 spin_lock_irq(&chip->reg_lock);
2355                 for (i = 0; i < chip->num_substreams; i++) {
2356                         s = &chip->substreams[i];
2357                         /* check surviving pcms; this should not happen though.. */
2358                         if (s->substream && s->running)
2359                                 snd_m3_pcm_stop(chip, s, s->substream);
2360                 }
2361                 spin_unlock_irq(&chip->reg_lock);
2362                 kfree(chip->substreams);
2363         }
2364         if (chip->iobase) {
2365                 outw(0, chip->iobase + HOST_INT_CTRL); /* disable ints */
2366         }
2367
2368 #ifdef CONFIG_PM_SLEEP
2369         vfree(chip->suspend_mem);
2370 #endif
2371
2372         if (chip->irq >= 0)
2373                 free_irq(chip->irq, chip);
2374
2375         if (chip->iobase)
2376                 pci_release_regions(chip->pci);
2377
2378         release_firmware(chip->assp_kernel_image);
2379         release_firmware(chip->assp_minisrc_image);
2380
2381         pci_disable_device(chip->pci);
2382         kfree(chip);
2383         return 0;
2384 }
2385
2386
2387 /*
2388  * APM support
2389  */
2390 #ifdef CONFIG_PM_SLEEP
2391 static int m3_suspend(struct device *dev)
2392 {
2393         struct snd_card *card = dev_get_drvdata(dev);
2394         struct snd_m3 *chip = card->private_data;
2395         int i, dsp_index;
2396
2397         if (chip->suspend_mem == NULL)
2398                 return 0;
2399
2400         chip->in_suspend = 1;
2401         cancel_work_sync(&chip->hwvol_work);
2402         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2403         snd_ac97_suspend(chip->ac97);
2404
2405         msleep(10); /* give the assp a chance to idle.. */
2406
2407         snd_m3_assp_halt(chip);
2408
2409         /* save dsp image */
2410         dsp_index = 0;
2411         for (i = REV_B_CODE_MEMORY_BEGIN; i <= REV_B_CODE_MEMORY_END; i++)
2412                 chip->suspend_mem[dsp_index++] =
2413                         snd_m3_assp_read(chip, MEMTYPE_INTERNAL_CODE, i);
2414         for (i = REV_B_DATA_MEMORY_BEGIN ; i <= REV_B_DATA_MEMORY_END; i++)
2415                 chip->suspend_mem[dsp_index++] =
2416                         snd_m3_assp_read(chip, MEMTYPE_INTERNAL_DATA, i);
2417         return 0;
2418 }
2419
2420 static int m3_resume(struct device *dev)
2421 {
2422         struct snd_card *card = dev_get_drvdata(dev);
2423         struct snd_m3 *chip = card->private_data;
2424         int i, dsp_index;
2425
2426         if (chip->suspend_mem == NULL)
2427                 return 0;
2428
2429         /* first lets just bring everything back. .*/
2430         snd_m3_outw(chip, 0, 0x54);
2431         snd_m3_outw(chip, 0, 0x56);
2432
2433         snd_m3_chip_init(chip);
2434         snd_m3_assp_halt(chip);
2435         snd_m3_ac97_reset(chip);
2436
2437         /* restore dsp image */
2438         dsp_index = 0;
2439         for (i = REV_B_CODE_MEMORY_BEGIN; i <= REV_B_CODE_MEMORY_END; i++)
2440                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_CODE, i, 
2441                                   chip->suspend_mem[dsp_index++]);
2442         for (i = REV_B_DATA_MEMORY_BEGIN ; i <= REV_B_DATA_MEMORY_END; i++)
2443                 snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA, i, 
2444                                   chip->suspend_mem[dsp_index++]);
2445
2446         /* tell the dma engine to restart itself */
2447         snd_m3_assp_write(chip, MEMTYPE_INTERNAL_DATA, 
2448                           KDATA_DMA_ACTIVE, 0);
2449
2450         /* restore ac97 registers */
2451         snd_ac97_resume(chip->ac97);
2452
2453         snd_m3_assp_continue(chip);
2454         snd_m3_enable_ints(chip);
2455         snd_m3_amp_enable(chip, 1);
2456
2457         snd_m3_hv_init(chip);
2458
2459         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2460         chip->in_suspend = 0;
2461         return 0;
2462 }
2463
2464 static SIMPLE_DEV_PM_OPS(m3_pm, m3_suspend, m3_resume);
2465 #define M3_PM_OPS       &m3_pm
2466 #else
2467 #define M3_PM_OPS       NULL
2468 #endif /* CONFIG_PM_SLEEP */
2469
2470 #ifdef CONFIG_SND_MAESTRO3_INPUT
2471 static int snd_m3_input_register(struct snd_m3 *chip)
2472 {
2473         struct input_dev *input_dev;
2474         int err;
2475
2476         input_dev = input_allocate_device();
2477         if (!input_dev)
2478                 return -ENOMEM;
2479
2480         snprintf(chip->phys, sizeof(chip->phys), "pci-%s/input0",
2481                  pci_name(chip->pci));
2482
2483         input_dev->name = chip->card->driver;
2484         input_dev->phys = chip->phys;
2485         input_dev->id.bustype = BUS_PCI;
2486         input_dev->id.vendor  = chip->pci->vendor;
2487         input_dev->id.product = chip->pci->device;
2488         input_dev->dev.parent = &chip->pci->dev;
2489
2490         __set_bit(EV_KEY, input_dev->evbit);
2491         __set_bit(KEY_MUTE, input_dev->keybit);
2492         __set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
2493         __set_bit(KEY_VOLUMEUP, input_dev->keybit);
2494
2495         err = input_register_device(input_dev);
2496         if (err) {
2497                 input_free_device(input_dev);
2498                 return err;
2499         }
2500
2501         chip->input_dev = input_dev;
2502         return 0;
2503 }
2504 #endif /* CONFIG_INPUT */
2505
2506 /*
2507  */
2508
2509 static int snd_m3_dev_free(struct snd_device *device)
2510 {
2511         struct snd_m3 *chip = device->device_data;
2512         return snd_m3_free(chip);
2513 }
2514
2515 static int
2516 snd_m3_create(struct snd_card *card, struct pci_dev *pci,
2517               int enable_amp,
2518               int amp_gpio,
2519               struct snd_m3 **chip_ret)
2520 {
2521         struct snd_m3 *chip;
2522         int i, err;
2523         const struct snd_pci_quirk *quirk;
2524         static const struct snd_device_ops ops = {
2525                 .dev_free =     snd_m3_dev_free,
2526         };
2527
2528         *chip_ret = NULL;
2529
2530         if (pci_enable_device(pci))
2531                 return -EIO;
2532
2533         /* check, if we can restrict PCI DMA transfers to 28 bits */
2534         if (dma_set_mask(&pci->dev, DMA_BIT_MASK(28)) < 0 ||
2535             dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(28)) < 0) {
2536                 dev_err(card->dev,
2537                         "architecture does not support 28bit PCI busmaster DMA\n");
2538                 pci_disable_device(pci);
2539                 return -ENXIO;
2540         }
2541
2542         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2543         if (chip == NULL) {
2544                 pci_disable_device(pci);
2545                 return -ENOMEM;
2546         }
2547
2548         spin_lock_init(&chip->reg_lock);
2549
2550         switch (pci->device) {
2551         case PCI_DEVICE_ID_ESS_ALLEGRO:
2552         case PCI_DEVICE_ID_ESS_ALLEGRO_1:
2553         case PCI_DEVICE_ID_ESS_CANYON3D_2LE:
2554         case PCI_DEVICE_ID_ESS_CANYON3D_2:
2555                 chip->allegro_flag = 1;
2556                 break;
2557         }
2558
2559         chip->card = card;
2560         chip->pci = pci;
2561         chip->irq = -1;
2562         INIT_WORK(&chip->hwvol_work, snd_m3_update_hw_volume);
2563
2564         chip->external_amp = enable_amp;
2565         if (amp_gpio >= 0 && amp_gpio <= 0x0f)
2566                 chip->amp_gpio = amp_gpio;
2567         else {
2568                 quirk = snd_pci_quirk_lookup(pci, m3_amp_quirk_list);
2569                 if (quirk) {
2570                         dev_info(card->dev, "set amp-gpio for '%s'\n",
2571                                  snd_pci_quirk_name(quirk));
2572                         chip->amp_gpio = quirk->value;
2573                 } else if (chip->allegro_flag)
2574                         chip->amp_gpio = GPO_EXT_AMP_ALLEGRO;
2575                 else /* presumably this is for all 'maestro3's.. */
2576                         chip->amp_gpio = GPO_EXT_AMP_M3;
2577         }
2578
2579         quirk = snd_pci_quirk_lookup(pci, m3_irda_quirk_list);
2580         if (quirk) {
2581                 dev_info(card->dev, "enabled irda workaround for '%s'\n",
2582                          snd_pci_quirk_name(quirk));
2583                 chip->irda_workaround = 1;
2584         }
2585         quirk = snd_pci_quirk_lookup(pci, m3_hv_quirk_list);
2586         if (quirk)
2587                 chip->hv_config = quirk->value;
2588         if (snd_pci_quirk_lookup(pci, m3_omnibook_quirk_list))
2589                 chip->is_omnibook = 1;
2590
2591         chip->num_substreams = NR_DSPS;
2592         chip->substreams = kcalloc(chip->num_substreams, sizeof(struct m3_dma),
2593                                    GFP_KERNEL);
2594         if (chip->substreams == NULL) {
2595                 kfree(chip);
2596                 pci_disable_device(pci);
2597                 return -ENOMEM;
2598         }
2599
2600         err = reject_firmware(&chip->assp_kernel_image,
2601                                "/*(DEBLOBBED)*/", &pci->dev);
2602         if (err < 0)
2603                 goto free_chip;
2604
2605         err = reject_firmware(&chip->assp_minisrc_image,
2606                                "/*(DEBLOBBED)*/", &pci->dev);
2607         if (err < 0)
2608                 goto free_chip;
2609
2610         err = pci_request_regions(pci, card->driver);
2611         if (err < 0)
2612                 goto free_chip;
2613
2614         chip->iobase = pci_resource_start(pci, 0);
2615         
2616         /* just to be sure */
2617         pci_set_master(pci);
2618
2619         snd_m3_chip_init(chip);
2620         snd_m3_assp_halt(chip);
2621
2622         snd_m3_ac97_reset(chip);
2623
2624         snd_m3_amp_enable(chip, 1);
2625
2626         snd_m3_hv_init(chip);
2627
2628         if (request_irq(pci->irq, snd_m3_interrupt, IRQF_SHARED,
2629                         KBUILD_MODNAME, chip)) {
2630                 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2631                 err = -ENOMEM;
2632                 goto free_chip;
2633         }
2634         chip->irq = pci->irq;
2635         card->sync_irq = chip->irq;
2636
2637 #ifdef CONFIG_PM_SLEEP
2638         chip->suspend_mem =
2639                 vmalloc(array_size(sizeof(u16),
2640                                    REV_B_CODE_MEMORY_LENGTH +
2641                                         REV_B_DATA_MEMORY_LENGTH));
2642         if (chip->suspend_mem == NULL)
2643                 dev_warn(card->dev, "can't allocate apm buffer\n");
2644 #endif
2645
2646         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2647         if (err < 0)
2648                 goto free_chip;
2649
2650         if ((err = snd_m3_mixer(chip)) < 0)
2651                 return err;
2652
2653         for (i = 0; i < chip->num_substreams; i++) {
2654                 struct m3_dma *s = &chip->substreams[i];
2655                 if ((err = snd_m3_assp_client_init(chip, s, i)) < 0)
2656                         return err;
2657         }
2658
2659         if ((err = snd_m3_pcm(chip, 0)) < 0)
2660                 return err;
2661
2662 #ifdef CONFIG_SND_MAESTRO3_INPUT
2663         if (chip->hv_config & HV_CTRL_ENABLE) {
2664                 err = snd_m3_input_register(chip);
2665                 if (err)
2666                         dev_warn(card->dev,
2667                                  "Input device registration failed with error %i",
2668                                  err);
2669         }
2670 #endif
2671
2672         snd_m3_enable_ints(chip);
2673         snd_m3_assp_continue(chip);
2674
2675         *chip_ret = chip;
2676
2677         return 0; 
2678
2679 free_chip:
2680         snd_m3_free(chip);
2681         return err;
2682 }
2683
2684 /*
2685  */
2686 static int
2687 snd_m3_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2688 {
2689         static int dev;
2690         struct snd_card *card;
2691         struct snd_m3 *chip;
2692         int err;
2693
2694         /* don't pick up modems */
2695         if (((pci->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
2696                 return -ENODEV;
2697
2698         if (dev >= SNDRV_CARDS)
2699                 return -ENODEV;
2700         if (!enable[dev]) {
2701                 dev++;
2702                 return -ENOENT;
2703         }
2704
2705         err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2706                            0, &card);
2707         if (err < 0)
2708                 return err;
2709
2710         switch (pci->device) {
2711         case PCI_DEVICE_ID_ESS_ALLEGRO:
2712         case PCI_DEVICE_ID_ESS_ALLEGRO_1:
2713                 strcpy(card->driver, "Allegro");
2714                 break;
2715         case PCI_DEVICE_ID_ESS_CANYON3D_2LE:
2716         case PCI_DEVICE_ID_ESS_CANYON3D_2:
2717                 strcpy(card->driver, "Canyon3D-2");
2718                 break;
2719         default:
2720                 strcpy(card->driver, "Maestro3");
2721                 break;
2722         }
2723
2724         err = snd_m3_create(card, pci, external_amp[dev], amp_gpio[dev], &chip);
2725         if (err < 0)
2726                 goto free_card;
2727
2728         card->private_data = chip;
2729
2730         sprintf(card->shortname, "ESS %s PCI", card->driver);
2731         sprintf(card->longname, "%s at 0x%lx, irq %d",
2732                 card->shortname, chip->iobase, chip->irq);
2733
2734         err = snd_card_register(card);
2735         if (err < 0)
2736                 goto free_card;
2737
2738 #if 0 /* TODO: not supported yet */
2739         /* TODO enable MIDI IRQ and I/O */
2740         err = snd_mpu401_uart_new(chip->card, 0, MPU401_HW_MPU401,
2741                                   chip->iobase + MPU401_DATA_PORT,
2742                                   MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
2743                                   -1, &chip->rmidi);
2744         if (err < 0)
2745                 dev_warn(card->dev, "no MIDI support.\n");
2746 #endif
2747
2748         pci_set_drvdata(pci, card);
2749         dev++;
2750         return 0;
2751
2752 free_card:
2753         snd_card_free(card);
2754         return err;
2755 }
2756
2757 static void snd_m3_remove(struct pci_dev *pci)
2758 {
2759         snd_card_free(pci_get_drvdata(pci));
2760 }
2761
2762 static struct pci_driver m3_driver = {
2763         .name = KBUILD_MODNAME,
2764         .id_table = snd_m3_ids,
2765         .probe = snd_m3_probe,
2766         .remove = snd_m3_remove,
2767         .driver = {
2768                 .pm = M3_PM_OPS,
2769         },
2770 };
2771         
2772 module_pci_driver(m3_driver);