1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91#include <linux/delay.h>
92#include <linux/init.h>
93#include <linux/interrupt.h>
94#include <linux/pci.h>
95#include <linux/slab.h>
96#include <linux/wait.h>
97#include <linux/gameport.h>
98#include <linux/device.h>
99#include <linux/firmware.h>
100#include <linux/kernel.h>
101#include <linux/module.h>
102#include <linux/io.h>
103#include <sound/core.h>
104#include <sound/info.h>
105#include <sound/control.h>
106#include <sound/pcm.h>
107#include <sound/pcm_params.h>
108#include <sound/ac97_codec.h>
109#include <sound/mpu401.h>
110#include <sound/opl3.h>
111#include <sound/initval.h>
112
113#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
114#define SUPPORT_JOYSTICK 1
115#endif
116
117MODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
118MODULE_DESCRIPTION("riptide");
119MODULE_LICENSE("GPL");
120MODULE_SUPPORTED_DEVICE("{{Conexant,Riptide}}");
121MODULE_FIRMWARE("riptide.hex");
122
123static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
124static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
125static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
126
127#ifdef SUPPORT_JOYSTICK
128static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
129#endif
130static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
131static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
132
133module_param_array(index, int, NULL, 0444);
134MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
135module_param_array(id, charp, NULL, 0444);
136MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
137module_param_array(enable, bool, NULL, 0444);
138MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
139#ifdef SUPPORT_JOYSTICK
140module_param_array(joystick_port, int, NULL, 0444);
141MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
142#endif
143module_param_array(mpu_port, int, NULL, 0444);
144MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
145module_param_array(opl3_port, int, NULL, 0444);
146MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
147
148
149
150
151#define MPU401_HW_RIPTIDE MPU401_HW_MPU401
152#define OPL3_HW_RIPTIDE OPL3_HW_OPL3
153
154#define PCI_EXT_CapId 0x40
155#define PCI_EXT_NextCapPrt 0x41
156#define PCI_EXT_PWMC 0x42
157#define PCI_EXT_PWSCR 0x44
158#define PCI_EXT_Data00 0x46
159#define PCI_EXT_PMSCR_BSE 0x47
160#define PCI_EXT_SB_Base 0x48
161#define PCI_EXT_FM_Base 0x4a
162#define PCI_EXT_MPU_Base 0x4C
163#define PCI_EXT_Game_Base 0x4E
164#define PCI_EXT_Legacy_Mask 0x50
165#define PCI_EXT_AsicRev 0x52
166#define PCI_EXT_Reserved3 0x53
167
168#define LEGACY_ENABLE_ALL 0x8000
169#define LEGACY_ENABLE_SB 0x4000
170#define LEGACY_ENABLE_FM 0x2000
171#define LEGACY_ENABLE_MPU_INT 0x1000
172#define LEGACY_ENABLE_MPU 0x0800
173#define LEGACY_ENABLE_GAMEPORT 0x0400
174
175#define MAX_WRITE_RETRY 10
176#define MAX_ERROR_COUNT 10
177#define CMDIF_TIMEOUT 50000
178#define RESET_TRIES 5
179
180#define READ_PORT_ULONG(p) inl((unsigned long)&(p))
181#define WRITE_PORT_ULONG(p,x) outl(x,(unsigned long)&(p))
182
183#define READ_AUDIO_CONTROL(p) READ_PORT_ULONG(p->audio_control)
184#define WRITE_AUDIO_CONTROL(p,x) WRITE_PORT_ULONG(p->audio_control,x)
185#define UMASK_AUDIO_CONTROL(p,x) WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
186#define MASK_AUDIO_CONTROL(p,x) WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
187#define READ_AUDIO_STATUS(p) READ_PORT_ULONG(p->audio_status)
188
189#define SET_GRESET(p) UMASK_AUDIO_CONTROL(p,0x0001)
190#define UNSET_GRESET(p) MASK_AUDIO_CONTROL(p,~0x0001)
191#define SET_AIE(p) UMASK_AUDIO_CONTROL(p,0x0004)
192#define UNSET_AIE(p) MASK_AUDIO_CONTROL(p,~0x0004)
193#define SET_AIACK(p) UMASK_AUDIO_CONTROL(p,0x0008)
194#define UNSET_AIACKT(p) MASKAUDIO_CONTROL(p,~0x0008)
195#define SET_ECMDAE(p) UMASK_AUDIO_CONTROL(p,0x0010)
196#define UNSET_ECMDAE(p) MASK_AUDIO_CONTROL(p,~0x0010)
197#define SET_ECMDBE(p) UMASK_AUDIO_CONTROL(p,0x0020)
198#define UNSET_ECMDBE(p) MASK_AUDIO_CONTROL(p,~0x0020)
199#define SET_EDATAF(p) UMASK_AUDIO_CONTROL(p,0x0040)
200#define UNSET_EDATAF(p) MASK_AUDIO_CONTROL(p,~0x0040)
201#define SET_EDATBF(p) UMASK_AUDIO_CONTROL(p,0x0080)
202#define UNSET_EDATBF(p) MASK_AUDIO_CONTROL(p,~0x0080)
203#define SET_ESBIRQON(p) UMASK_AUDIO_CONTROL(p,0x0100)
204#define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
205#define SET_EMPUIRQ(p) UMASK_AUDIO_CONTROL(p,0x0200)
206#define UNSET_EMPUIRQ(p) MASK_AUDIO_CONTROL(p,~0x0200)
207#define IS_CMDE(a) (READ_PORT_ULONG(a->stat)&0x1)
208#define IS_DATF(a) (READ_PORT_ULONG(a->stat)&0x2)
209#define IS_READY(p) (READ_AUDIO_STATUS(p)&0x0001)
210#define IS_DLREADY(p) (READ_AUDIO_STATUS(p)&0x0002)
211#define IS_DLERR(p) (READ_AUDIO_STATUS(p)&0x0004)
212#define IS_GERR(p) (READ_AUDIO_STATUS(p)&0x0008)
213#define IS_CMDAEIRQ(p) (READ_AUDIO_STATUS(p)&0x0010)
214#define IS_CMDBEIRQ(p) (READ_AUDIO_STATUS(p)&0x0020)
215#define IS_DATAFIRQ(p) (READ_AUDIO_STATUS(p)&0x0040)
216#define IS_DATBFIRQ(p) (READ_AUDIO_STATUS(p)&0x0080)
217#define IS_EOBIRQ(p) (READ_AUDIO_STATUS(p)&0x0100)
218#define IS_EOSIRQ(p) (READ_AUDIO_STATUS(p)&0x0200)
219#define IS_EOCIRQ(p) (READ_AUDIO_STATUS(p)&0x0400)
220#define IS_UNSLIRQ(p) (READ_AUDIO_STATUS(p)&0x0800)
221#define IS_SBIRQ(p) (READ_AUDIO_STATUS(p)&0x1000)
222#define IS_MPUIRQ(p) (READ_AUDIO_STATUS(p)&0x2000)
223
224#define RESP 0x00000001
225#define PARM 0x00000002
226#define CMDA 0x00000004
227#define CMDB 0x00000008
228#define NILL 0x00000000
229
230#define LONG0(a) ((u32)a)
231#define BYTE0(a) (LONG0(a)&0xff)
232#define BYTE1(a) (BYTE0(a)<<8)
233#define BYTE2(a) (BYTE0(a)<<16)
234#define BYTE3(a) (BYTE0(a)<<24)
235#define WORD0(a) (LONG0(a)&0xffff)
236#define WORD1(a) (WORD0(a)<<8)
237#define WORD2(a) (WORD0(a)<<16)
238#define TRINIB0(a) (LONG0(a)&0xffffff)
239#define TRINIB1(a) (TRINIB0(a)<<8)
240
241#define RET(a) ((union cmdret *)(a))
242
243#define SEND_GETV(p,b) sendcmd(p,RESP,GETV,0,RET(b))
244#define SEND_GETC(p,b,c) sendcmd(p,PARM|RESP,GETC,c,RET(b))
245#define SEND_GUNS(p,b) sendcmd(p,RESP,GUNS,0,RET(b))
246#define SEND_SCID(p,b) sendcmd(p,RESP,SCID,0,RET(b))
247#define SEND_RMEM(p,b,c,d) sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))
248#define SEND_SMEM(p,b,c) sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))
249#define SEND_WMEM(p,b,c) sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))
250#define SEND_SDTM(p,b,c) sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))
251#define SEND_GOTO(p,b) sendcmd(p,PARM,GOTO,LONG0(b),RET(0))
252#define SEND_SETDPLL(p) sendcmd(p,0,ARM_SETDPLL,0,RET(0))
253#define SEND_SSTR(p,b,c) sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))
254#define SEND_PSTR(p,b) sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))
255#define SEND_KSTR(p,b) sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))
256#define SEND_KDMA(p) sendcmd(p,0,KDMA,0,RET(0))
257#define SEND_GPOS(p,b,c,d) sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))
258#define SEND_SETF(p,b,c,d,e,f,g) sendcmd(p,PARM,SETF|WORD1(b)|BYTE3(c),d|BYTE1(e)|BYTE2(f)|BYTE3(g),RET(0))
259#define SEND_GSTS(p,b,c,d) sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
260#define SEND_NGPOS(p,b,c,d) sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
261#define SEND_PSEL(p,b,c) sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))
262#define SEND_PCLR(p,b,c) sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))
263#define SEND_PLST(p,b) sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
264#define SEND_RSSV(p,b,c,d) sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
265#define SEND_LSEL(p,b,c,d,e,f,g,h) sendcmd(p,PARM,LSEL|BYTE1(b)|BYTE2(c)|BYTE3(d),BYTE0(e)|BYTE1(f)|BYTE2(g)|BYTE3(h),RET(0))
266#define SEND_SSRC(p,b,c,d,e) sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))
267#define SEND_SLST(p,b) sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
268#define SEND_RSRC(p,b,c) sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))
269#define SEND_SSRB(p,b,c) sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
270#define SEND_SDGV(p,b,c,d,e) sendcmd(p,PARM,SDGV|BYTE2(b)|BYTE3(c),WORD0(d)|WORD2(e),RET(0))
271#define SEND_RDGV(p,b,c,d) sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))
272#define SEND_DLST(p,b) sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
273#define SEND_SACR(p,b,c) sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))
274#define SEND_RACR(p,b,c) sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))
275#define SEND_ALST(p,b) sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
276#define SEND_TXAC(p,b,c,d,e,f) sendcmd(p,PARM,TXAC|BYTE1(b)|WORD2(c),WORD0(d)|BYTE2(e)|BYTE3(f),RET(0))
277#define SEND_RXAC(p,b,c,d) sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
278#define SEND_SI2S(p,b) sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
279
280#define EOB_STATUS 0x80000000
281#define EOS_STATUS 0x40000000
282#define EOC_STATUS 0x20000000
283#define ERR_STATUS 0x10000000
284#define EMPTY_STATUS 0x08000000
285
286#define IEOB_ENABLE 0x1
287#define IEOS_ENABLE 0x2
288#define IEOC_ENABLE 0x4
289#define RDONCE 0x8
290#define DESC_MAX_MASK 0xff
291
292#define ST_PLAY 0x1
293#define ST_STOP 0x2
294#define ST_PAUSE 0x4
295
296#define I2S_INTDEC 3
297#define I2S_MERGER 0
298#define I2S_SPLITTER 0
299#define I2S_MIXER 7
300#define I2S_RATE 44100
301
302#define MODEM_INTDEC 4
303#define MODEM_MERGER 3
304#define MODEM_SPLITTER 0
305#define MODEM_MIXER 11
306
307#define FM_INTDEC 3
308#define FM_MERGER 0
309#define FM_SPLITTER 0
310#define FM_MIXER 9
311
312#define SPLIT_PATH 0x80
313
314enum FIRMWARE {
315 DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
316 EXT_LIN_ADDR_REC,
317};
318
319enum CMDS {
320 GETV = 0x00, GETC, GUNS, SCID, RMEM =
321 0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
322 0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
323 0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
324 0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
325 0x70, ARM_SETDPLL = 0x72,
326};
327
328enum E1SOURCE {
329 ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
330 ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
331 ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
332 ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
333 INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
334 INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
335 INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
336 DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
337 ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
338 DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
339 GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
340 SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
341 SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
342 MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
343};
344
345enum E2SINK {
346 LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
347 LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
348 INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
349 INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
350 DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
351 PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
352 DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
353 DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
354 DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
355 DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
356 GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
357 SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
358 MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
359 MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
360};
361
362enum LBUS_SINK {
363 LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
364 LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
365 LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
366};
367
368enum RT_CHANNEL_IDS {
369 M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
370 DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
371 M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
372 CHANNEL_ID_COUNTER
373};
374
375enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
376
377struct lbuspath {
378 unsigned char *noconv;
379 unsigned char *stereo;
380 unsigned char *mono;
381};
382
383struct cmdport {
384 u32 data1;
385 u32 data2;
386 u32 stat;
387 u32 pad[5];
388};
389
390struct riptideport {
391 u32 audio_control;
392 u32 audio_status;
393 u32 pad[2];
394 struct cmdport port[2];
395};
396
397struct cmdif {
398 struct riptideport *hwport;
399 spinlock_t lock;
400 unsigned int cmdcnt;
401 unsigned int cmdtime;
402 unsigned int cmdtimemax;
403 unsigned int cmdtimemin;
404 unsigned int errcnt;
405 int is_reset;
406};
407
408struct riptide_firmware {
409 u16 ASIC;
410 u16 CODEC;
411 u16 AUXDSP;
412 u16 PROG;
413};
414
415union cmdret {
416 u8 retbytes[8];
417 u16 retwords[4];
418 u32 retlongs[2];
419};
420
421union firmware_version {
422 union cmdret ret;
423 struct riptide_firmware firmware;
424};
425
426#define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
427
428#define PLAYBACK_SUBSTREAMS 3
429struct snd_riptide {
430 struct snd_card *card;
431 struct pci_dev *pci;
432 const struct firmware *fw_entry;
433
434 struct cmdif *cif;
435
436 struct snd_pcm *pcm;
437 struct snd_pcm *pcm_i2s;
438 struct snd_rawmidi *rmidi;
439 struct snd_opl3 *opl3;
440 struct snd_ac97 *ac97;
441 struct snd_ac97_bus *ac97_bus;
442
443 struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
444 struct snd_pcm_substream *capture_substream;
445
446 int openstreams;
447
448 int irq;
449 unsigned long port;
450 unsigned short mpuaddr;
451 unsigned short opladdr;
452#ifdef SUPPORT_JOYSTICK
453 unsigned short gameaddr;
454#endif
455 struct resource *res_port;
456
457 unsigned short device_id;
458
459 union firmware_version firmware;
460
461 spinlock_t lock;
462 struct tasklet_struct riptide_tq;
463 struct snd_info_entry *proc_entry;
464
465 unsigned long received_irqs;
466 unsigned long handled_irqs;
467#ifdef CONFIG_PM_SLEEP
468 int in_suspend;
469#endif
470};
471
472struct sgd {
473 u32 dwNextLink;
474 u32 dwSegPtrPhys;
475 u32 dwSegLen;
476 u32 dwStat_Ctl;
477};
478
479struct pcmhw {
480 struct lbuspath paths;
481 unsigned char *lbuspath;
482 unsigned char source;
483 unsigned char intdec[2];
484 unsigned char mixer;
485 unsigned char id;
486 unsigned char state;
487 unsigned int rate;
488 unsigned int channels;
489 snd_pcm_format_t format;
490 struct snd_dma_buffer sgdlist;
491 struct sgd *sgdbuf;
492 unsigned int size;
493 unsigned int pages;
494 unsigned int oldpos;
495 unsigned int pointer;
496};
497
498#define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
499
500static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
501 union cmdret *ret);
502static int getsourcesink(struct cmdif *cif, unsigned char source,
503 unsigned char sink, unsigned char *a,
504 unsigned char *b);
505static int snd_riptide_initialize(struct snd_riptide *chip);
506static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
507
508
509
510
511static const struct pci_device_id snd_riptide_ids[] = {
512 { PCI_DEVICE(0x127a, 0x4310) },
513 { PCI_DEVICE(0x127a, 0x4320) },
514 { PCI_DEVICE(0x127a, 0x4330) },
515 { PCI_DEVICE(0x127a, 0x4340) },
516 {0,},
517};
518
519#ifdef SUPPORT_JOYSTICK
520static const struct pci_device_id snd_riptide_joystick_ids[] = {
521 { PCI_DEVICE(0x127a, 0x4312) },
522 { PCI_DEVICE(0x127a, 0x4322) },
523 { PCI_DEVICE(0x127a, 0x4332) },
524 { PCI_DEVICE(0x127a, 0x4342) },
525 {0,},
526};
527#endif
528
529MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
530
531
532
533
534static unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
535 {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
536 LS_NONE2},
537 {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
538 LS_NONE2},
539 {INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
540 {INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
541 {INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
542 {INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
543 LS_SRC_INTERPOLATORM},
544 {INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
545 LS_SRC_INTERPOLATORM},
546 {INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
547 LS_SRC_INTERPOLATORM},
548 {DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
549 {DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
550 {DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
551 LS_NONE2},
552 {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
553 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
554 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
555 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
556 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
557 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
558 {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
559 {GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
560 LS_MIXER_GAIN_FUNCTION},
561 {GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
562 LS_MIXER_GAIN_FUNCTION},
563 {GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
564 {SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
565 {SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
566 {MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
567 {MERGER1_OUT, LS_SRC_MERGER},
568 {MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
569 {MERGER2_OUT, LS_SRC_MERGER},
570 {MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
571 LS_NONE2},
572};
573
574static unsigned char lbus_play_opl3[] = {
575 DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
576};
577static unsigned char lbus_play_modem[] = {
578 DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
579};
580static unsigned char lbus_play_i2s[] = {
581 INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
582};
583static unsigned char lbus_play_out[] = {
584 PDAC2ACLNK, 0xff
585};
586static unsigned char lbus_play_outhp[] = {
587 HNDSPK2ACLNK, 0xff
588};
589static unsigned char lbus_play_noconv1[] = {
590 DIGITAL_MIXER_IN0, 0xff
591};
592static unsigned char lbus_play_stereo1[] = {
593 INTER0_IN, DIGITAL_MIXER_IN0, 0xff
594};
595static unsigned char lbus_play_mono1[] = {
596 INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
597};
598static unsigned char lbus_play_noconv2[] = {
599 DIGITAL_MIXER_IN1, 0xff
600};
601static unsigned char lbus_play_stereo2[] = {
602 INTER1_IN, DIGITAL_MIXER_IN1, 0xff
603};
604static unsigned char lbus_play_mono2[] = {
605 INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
606};
607static unsigned char lbus_play_noconv3[] = {
608 DIGITAL_MIXER_IN2, 0xff
609};
610static unsigned char lbus_play_stereo3[] = {
611 INTER2_IN, DIGITAL_MIXER_IN2, 0xff
612};
613static unsigned char lbus_play_mono3[] = {
614 INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
615};
616static unsigned char lbus_rec_noconv1[] = {
617 LBUS2ARM_FIFO5, 0xff
618};
619static unsigned char lbus_rec_stereo1[] = {
620 DECIM0_IN, LBUS2ARM_FIFO5, 0xff
621};
622static unsigned char lbus_rec_mono1[] = {
623 DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
624};
625
626static unsigned char play_ids[] = { 4, 1, 2, };
627static unsigned char play_sources[] = {
628 ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
629};
630static struct lbuspath lbus_play_paths[] = {
631 {
632 .noconv = lbus_play_noconv1,
633 .stereo = lbus_play_stereo1,
634 .mono = lbus_play_mono1,
635 },
636 {
637 .noconv = lbus_play_noconv2,
638 .stereo = lbus_play_stereo2,
639 .mono = lbus_play_mono2,
640 },
641 {
642 .noconv = lbus_play_noconv3,
643 .stereo = lbus_play_stereo3,
644 .mono = lbus_play_mono3,
645 },
646};
647static struct lbuspath lbus_rec_path = {
648 .noconv = lbus_rec_noconv1,
649 .stereo = lbus_rec_stereo1,
650 .mono = lbus_rec_mono1,
651};
652
653#define FIRMWARE_VERSIONS 1
654static union firmware_version firmware_versions[] = {
655 {
656 .firmware = {
657 .ASIC = 3,
658 .CODEC = 2,
659 .AUXDSP = 3,
660 .PROG = 773,
661 },
662 },
663};
664
665static u32 atoh(const unsigned char *in, unsigned int len)
666{
667 u32 sum = 0;
668 unsigned int mult = 1;
669 unsigned char c;
670
671 while (len) {
672 int value;
673
674 c = in[len - 1];
675 value = hex_to_bin(c);
676 if (value >= 0)
677 sum += mult * value;
678 mult *= 16;
679 --len;
680 }
681 return sum;
682}
683
684static int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
685{
686 u32 addr;
687 u32 data;
688 u32 i;
689 const unsigned char *p;
690
691 i = atoh(&in[1], 2);
692 addr = offset + atoh(&in[3], 4);
693 if (SEND_SMEM(cif, 0, addr) != 0)
694 return -EACCES;
695 p = in + 9;
696 while (i) {
697 data = atoh(p, 8);
698 if (SEND_WMEM(cif, 2,
699 ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
700 >> 4)))
701 return -EACCES;
702 i -= 4;
703 p += 8;
704 }
705 return 0;
706}
707
708static int loadfirmware(struct cmdif *cif, const unsigned char *img,
709 unsigned int size)
710{
711 const unsigned char *in;
712 u32 laddr, saddr, t, val;
713 int err = 0;
714
715 laddr = saddr = 0;
716 while (size > 0 && err == 0) {
717 in = img;
718 if (in[0] == ':') {
719 t = atoh(&in[7], 2);
720 switch (t) {
721 case DATA_REC:
722 err = senddata(cif, in, laddr + saddr);
723 break;
724 case EXT_SEG_ADDR_REC:
725 saddr = atoh(&in[9], 4) << 4;
726 break;
727 case EXT_LIN_ADDR_REC:
728 laddr = atoh(&in[9], 4) << 16;
729 break;
730 case EXT_GOTO_CMD_REC:
731 val = atoh(&in[9], 8);
732 if (SEND_GOTO(cif, val) != 0)
733 err = -EACCES;
734 break;
735 case EXT_END_OF_FILE:
736 size = 0;
737 break;
738 default:
739 break;
740 }
741 while (size > 0) {
742 size--;
743 if (*img++ == '\n')
744 break;
745 }
746 }
747 }
748 snd_printdd("load firmware return %d\n", err);
749 return err;
750}
751
752static void
753alloclbuspath(struct cmdif *cif, unsigned char source,
754 unsigned char *path, unsigned char *mixer, unsigned char *s)
755{
756 while (*path != 0xff) {
757 unsigned char sink, type;
758
759 sink = *path & (~SPLIT_PATH);
760 if (sink != E2SINK_MAX) {
761 snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
762 SEND_PSEL(cif, source, sink);
763 source = lbusin2out[sink][0];
764 type = lbusin2out[sink][1];
765 if (type == LS_MIXER_IN) {
766 if (mixer)
767 *mixer = sink - DIGITAL_MIXER_IN0;
768 }
769 if (type == LS_SRC_DECIMATORM ||
770 type == LS_SRC_DECIMATOR ||
771 type == LS_SRC_INTERPOLATORM ||
772 type == LS_SRC_INTERPOLATOR) {
773 if (s) {
774 if (s[0] != 0xff)
775 s[1] = sink;
776 else
777 s[0] = sink;
778 }
779 }
780 }
781 if (*path++ & SPLIT_PATH) {
782 unsigned char *npath = path;
783
784 while (*npath != 0xff)
785 npath++;
786 alloclbuspath(cif, source + 1, ++npath, mixer, s);
787 }
788 }
789}
790
791static void
792freelbuspath(struct cmdif *cif, unsigned char source, unsigned char *path)
793{
794 while (*path != 0xff) {
795 unsigned char sink;
796
797 sink = *path & (~SPLIT_PATH);
798 if (sink != E2SINK_MAX) {
799 snd_printdd("free path 0x%x->0x%x\n", source, sink);
800 SEND_PCLR(cif, source, sink);
801 source = lbusin2out[sink][0];
802 }
803 if (*path++ & SPLIT_PATH) {
804 unsigned char *npath = path;
805
806 while (*npath != 0xff)
807 npath++;
808 freelbuspath(cif, source + 1, ++npath);
809 }
810 }
811}
812
813static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
814{
815 union cmdret rptr = CMDRET_ZERO;
816 unsigned int i = MAX_WRITE_RETRY;
817 int flag = 1;
818
819 SEND_RMEM(cif, 0x02, addr, &rptr);
820 rptr.retlongs[0] &= (~mask);
821
822 while (--i) {
823 SEND_SMEM(cif, 0x01, addr);
824 SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
825 SEND_RMEM(cif, 0x02, addr, &rptr);
826 if ((rptr.retlongs[0] & data) == data) {
827 flag = 0;
828 break;
829 } else
830 rptr.retlongs[0] &= ~mask;
831 }
832 snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
833 flag);
834 return flag;
835}
836
837static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
838 union cmdret *ret)
839{
840 int i, j;
841 int err;
842 unsigned int time = 0;
843 unsigned long irqflags;
844 struct riptideport *hwport;
845 struct cmdport *cmdport = NULL;
846
847 if (snd_BUG_ON(!cif))
848 return -EINVAL;
849
850 hwport = cif->hwport;
851 if (cif->errcnt > MAX_ERROR_COUNT) {
852 if (cif->is_reset) {
853 snd_printk(KERN_ERR
854 "Riptide: Too many failed cmds, reinitializing\n");
855 if (riptide_reset(cif, NULL) == 0) {
856 cif->errcnt = 0;
857 return -EIO;
858 }
859 }
860 snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
861 return -EINVAL;
862 }
863 if (ret) {
864 ret->retlongs[0] = 0;
865 ret->retlongs[1] = 0;
866 }
867 i = 0;
868 spin_lock_irqsave(&cif->lock, irqflags);
869 while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
870 udelay(10);
871 if (i > CMDIF_TIMEOUT) {
872 err = -EBUSY;
873 goto errout;
874 }
875
876 err = 0;
877 for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
878 cmdport = &(hwport->port[j % 2]);
879 if (IS_DATF(cmdport)) {
880 READ_PORT_ULONG(cmdport->data1);
881 READ_PORT_ULONG(cmdport->data2);
882 }
883 if (IS_CMDE(cmdport)) {
884 if (flags & PARM)
885 WRITE_PORT_ULONG(cmdport->data2, parm);
886 WRITE_PORT_ULONG(cmdport->data1, cmd);
887 if ((flags & RESP) && ret) {
888 while (!IS_DATF(cmdport) &&
889 time < CMDIF_TIMEOUT) {
890 udelay(10);
891 time++;
892 }
893 if (time < CMDIF_TIMEOUT) {
894 ret->retlongs[0] =
895 READ_PORT_ULONG(cmdport->data1);
896 ret->retlongs[1] =
897 READ_PORT_ULONG(cmdport->data2);
898 } else {
899 err = -ENOSYS;
900 goto errout;
901 }
902 }
903 break;
904 }
905 udelay(20);
906 }
907 if (time == CMDIF_TIMEOUT) {
908 err = -ENODATA;
909 goto errout;
910 }
911 spin_unlock_irqrestore(&cif->lock, irqflags);
912
913 cif->cmdcnt++;
914 cif->cmdtime += time;
915 if (time > cif->cmdtimemax)
916 cif->cmdtimemax = time;
917 if (time < cif->cmdtimemin)
918 cif->cmdtimemin = time;
919 if ((cif->cmdcnt) % 1000 == 0)
920 snd_printdd
921 ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
922 cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
923 cif->cmdtimemax, cif->errcnt);
924 return 0;
925
926 errout:
927 cif->errcnt++;
928 spin_unlock_irqrestore(&cif->lock, irqflags);
929 snd_printdd
930 ("send cmd %d hw: 0x%x flag: 0x%x cmd: 0x%x parm: 0x%x ret: 0x%x 0x%x CMDE: %d DATF: %d failed %d\n",
931 cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
932 flags, cmd, parm, ret ? ret->retlongs[0] : 0,
933 ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
934 err);
935 return err;
936}
937
938static int
939setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
940{
941 union cmdret rptr = CMDRET_ZERO;
942 int i = 0;
943
944 snd_printdd("sent mixer %d: 0x%x 0x%x\n", num, rval, lval);
945 do {
946 SEND_SDGV(cif, num, num, rval, lval);
947 SEND_RDGV(cif, num, num, &rptr);
948 if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
949 return 0;
950 } while (i++ < MAX_WRITE_RETRY);
951 snd_printdd("sent mixer failed\n");
952 return -EIO;
953}
954
955static int getpaths(struct cmdif *cif, unsigned char *o)
956{
957 unsigned char src[E2SINK_MAX];
958 unsigned char sink[E2SINK_MAX];
959 int i, j = 0;
960
961 for (i = 0; i < E2SINK_MAX; i++) {
962 getsourcesink(cif, i, i, &src[i], &sink[i]);
963 if (sink[i] < E2SINK_MAX) {
964 o[j++] = sink[i];
965 o[j++] = i;
966 }
967 }
968 return j;
969}
970
971static int
972getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
973 unsigned char *a, unsigned char *b)
974{
975 union cmdret rptr = CMDRET_ZERO;
976
977 if (SEND_RSSV(cif, source, sink, &rptr) &&
978 SEND_RSSV(cif, source, sink, &rptr))
979 return -EIO;
980 *a = rptr.retbytes[0];
981 *b = rptr.retbytes[1];
982 snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
983 return 0;
984}
985
986static int
987getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
988{
989 unsigned char *s;
990 unsigned int p[2] = { 0, 0 };
991 int i;
992 union cmdret rptr = CMDRET_ZERO;
993
994 s = intdec;
995 for (i = 0; i < 2; i++) {
996 if (*s != 0xff) {
997 if (SEND_RSRC(cif, *s, &rptr) &&
998 SEND_RSRC(cif, *s, &rptr))
999 return -EIO;
1000 p[i] += rptr.retwords[1];
1001 p[i] *= rptr.retwords[2];
1002 p[i] += rptr.retwords[3];
1003 p[i] /= 65536;
1004 }
1005 s++;
1006 }
1007 if (p[0]) {
1008 if (p[1] != p[0])
1009 snd_printdd("rates differ %d %d\n", p[0], p[1]);
1010 *rate = (unsigned int)p[0];
1011 } else
1012 *rate = (unsigned int)p[1];
1013 snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
1014 return 0;
1015}
1016
1017static int
1018setsampleformat(struct cmdif *cif,
1019 unsigned char mixer, unsigned char id,
1020 unsigned char channels, unsigned char format)
1021{
1022 unsigned char w, ch, sig, order;
1023
1024 snd_printdd
1025 ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1026 mixer, id, channels, format);
1027 ch = channels == 1;
1028 w = snd_pcm_format_width(format) == 8;
1029 sig = snd_pcm_format_unsigned(format) != 0;
1030 order = snd_pcm_format_big_endian(format) != 0;
1031
1032 if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1033 SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1034 snd_printdd("setsampleformat failed\n");
1035 return -EIO;
1036 }
1037 return 0;
1038}
1039
1040static int
1041setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1042{
1043 u32 D, M, N;
1044 union cmdret rptr = CMDRET_ZERO;
1045 int i;
1046
1047 snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1048 intdec[1], rate);
1049 D = 48000;
1050 M = ((rate == 48000) ? 47999 : rate) * 65536;
1051 N = M % D;
1052 M /= D;
1053 for (i = 0; i < 2; i++) {
1054 if (*intdec != 0xff) {
1055 do {
1056 SEND_SSRC(cif, *intdec, D, M, N);
1057 SEND_RSRC(cif, *intdec, &rptr);
1058 } while (rptr.retwords[1] != D &&
1059 rptr.retwords[2] != M &&
1060 rptr.retwords[3] != N &&
1061 i++ < MAX_WRITE_RETRY);
1062 if (i > MAX_WRITE_RETRY) {
1063 snd_printdd("sent samplerate %d: %d failed\n",
1064 *intdec, rate);
1065 return -EIO;
1066 }
1067 }
1068 intdec++;
1069 }
1070 return 0;
1071}
1072
1073static int
1074getmixer(struct cmdif *cif, short num, unsigned short *rval,
1075 unsigned short *lval)
1076{
1077 union cmdret rptr = CMDRET_ZERO;
1078
1079 if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1080 return -EIO;
1081 *rval = rptr.retwords[0];
1082 *lval = rptr.retwords[1];
1083 snd_printdd("got mixer %d: 0x%x 0x%x\n", num, *rval, *lval);
1084 return 0;
1085}
1086
1087static void riptide_handleirq(unsigned long dev_id)
1088{
1089 struct snd_riptide *chip = (void *)dev_id;
1090 struct cmdif *cif = chip->cif;
1091 struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1092 struct snd_pcm_runtime *runtime;
1093 struct pcmhw *data = NULL;
1094 unsigned int pos, period_bytes;
1095 struct sgd *c;
1096 int i, j;
1097 unsigned int flag;
1098
1099 if (!cif)
1100 return;
1101
1102 for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1103 substream[i] = chip->playback_substream[i];
1104 substream[i] = chip->capture_substream;
1105 for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
1106 if (substream[i] &&
1107 (runtime = substream[i]->runtime) &&
1108 (data = runtime->private_data) && data->state != ST_STOP) {
1109 pos = 0;
1110 for (j = 0; j < data->pages; j++) {
1111 c = &data->sgdbuf[j];
1112 flag = le32_to_cpu(c->dwStat_Ctl);
1113 if (flag & EOB_STATUS)
1114 pos += le32_to_cpu(c->dwSegLen);
1115 if (flag & EOC_STATUS)
1116 pos += le32_to_cpu(c->dwSegLen);
1117 if ((flag & EOS_STATUS)
1118 && (data->state == ST_PLAY)) {
1119 data->state = ST_STOP;
1120 snd_printk(KERN_ERR
1121 "Riptide: DMA stopped unexpectedly\n");
1122 }
1123 c->dwStat_Ctl =
1124 cpu_to_le32(flag &
1125 ~(EOS_STATUS | EOB_STATUS |
1126 EOC_STATUS));
1127 }
1128 data->pointer += pos;
1129 pos += data->oldpos;
1130 if (data->state != ST_STOP) {
1131 period_bytes =
1132 frames_to_bytes(runtime,
1133 runtime->period_size);
1134 snd_printdd
1135 ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1136 READ_AUDIO_STATUS(cif->hwport),
1137 bytes_to_frames(runtime, pos),
1138 runtime->period_size);
1139 j = 0;
1140 if (pos >= period_bytes) {
1141 j++;
1142 while (pos >= period_bytes)
1143 pos -= period_bytes;
1144 }
1145 data->oldpos = pos;
1146 if (j > 0)
1147 snd_pcm_period_elapsed(substream[i]);
1148 }
1149 }
1150 }
1151}
1152
1153#ifdef CONFIG_PM_SLEEP
1154static int riptide_suspend(struct device *dev)
1155{
1156 struct snd_card *card = dev_get_drvdata(dev);
1157 struct snd_riptide *chip = card->private_data;
1158
1159 chip->in_suspend = 1;
1160 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1161 snd_pcm_suspend_all(chip->pcm);
1162 snd_ac97_suspend(chip->ac97);
1163 return 0;
1164}
1165
1166static int riptide_resume(struct device *dev)
1167{
1168 struct snd_card *card = dev_get_drvdata(dev);
1169 struct snd_riptide *chip = card->private_data;
1170
1171 snd_riptide_initialize(chip);
1172 snd_ac97_resume(chip->ac97);
1173 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1174 chip->in_suspend = 0;
1175 return 0;
1176}
1177
1178static SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
1179#define RIPTIDE_PM_OPS &riptide_pm
1180#else
1181#define RIPTIDE_PM_OPS NULL
1182#endif
1183
1184static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
1185{
1186 union firmware_version firmware = { .ret = CMDRET_ZERO };
1187 int i, timeout, err;
1188
1189 for (i = 0; i < 2; i++) {
1190 WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1191 WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1192 }
1193 SET_GRESET(cif->hwport);
1194 udelay(100);
1195 UNSET_GRESET(cif->hwport);
1196 udelay(100);
1197
1198 for (timeout = 100000; --timeout; udelay(10)) {
1199 if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1200 break;
1201 }
1202 if (!timeout) {
1203 snd_printk(KERN_ERR
1204 "Riptide: device not ready, audio status: 0x%x "
1205 "ready: %d gerr: %d\n",
1206 READ_AUDIO_STATUS(cif->hwport),
1207 IS_READY(cif->hwport), IS_GERR(cif->hwport));
1208 return -EIO;
1209 } else {
1210 snd_printdd
1211 ("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1212 READ_AUDIO_STATUS(cif->hwport),
1213 IS_READY(cif->hwport), IS_GERR(cif->hwport));
1214 }
1215
1216 SEND_GETV(cif, &firmware.ret);
1217 snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1218 firmware.firmware.ASIC, firmware.firmware.CODEC,
1219 firmware.firmware.AUXDSP, firmware.firmware.PROG);
1220
1221 if (!chip)
1222 return 1;
1223
1224 for (i = 0; i < FIRMWARE_VERSIONS; i++) {
1225 if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
1226 return 1;
1227
1228 }
1229
1230 snd_printdd("Writing Firmware\n");
1231 if (!chip->fw_entry) {
1232 err = request_firmware(&chip->fw_entry, "riptide.hex",
1233 &chip->pci->dev);
1234 if (err) {
1235 snd_printk(KERN_ERR
1236 "Riptide: Firmware not available %d\n", err);
1237 return -EIO;
1238 }
1239 }
1240 err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
1241 if (err) {
1242 snd_printk(KERN_ERR
1243 "Riptide: Could not load firmware %d\n", err);
1244 return err;
1245 }
1246
1247 chip->firmware = firmware;
1248
1249 return 1;
1250}
1251
1252static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1253{
1254 union cmdret rptr = CMDRET_ZERO;
1255 int err, tries;
1256
1257 if (!cif)
1258 return -EINVAL;
1259
1260 cif->cmdcnt = 0;
1261 cif->cmdtime = 0;
1262 cif->cmdtimemax = 0;
1263 cif->cmdtimemin = 0xffffffff;
1264 cif->errcnt = 0;
1265 cif->is_reset = 0;
1266
1267 tries = RESET_TRIES;
1268 do {
1269 err = try_to_load_firmware(cif, chip);
1270 if (err < 0)
1271 return err;
1272 } while (!err && --tries);
1273
1274 SEND_SACR(cif, 0, AC97_RESET);
1275 SEND_RACR(cif, AC97_RESET, &rptr);
1276 snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1277
1278 SEND_PLST(cif, 0);
1279 SEND_SLST(cif, 0);
1280 SEND_DLST(cif, 0);
1281 SEND_ALST(cif, 0);
1282 SEND_KDMA(cif);
1283
1284 writearm(cif, 0x301F8, 1, 1);
1285 writearm(cif, 0x301F4, 1, 1);
1286
1287 SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1288 MODEM_SPLITTER, MODEM_MIXER);
1289 setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1290 alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1291
1292 SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1293 FM_MIXER);
1294 setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1295 writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1296 writearm(cif, 0x301A8, 0x02, 0x00000002);
1297 writearm(cif, 0x30264, 0x08, 0xffffffff);
1298 alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1299
1300 SEND_SSRC(cif, I2S_INTDEC, 48000,
1301 ((u32) I2S_RATE * 65536) / 48000,
1302 ((u32) I2S_RATE * 65536) % 48000);
1303 SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1304 I2S_MIXER);
1305 SEND_SI2S(cif, 1);
1306 alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1307 alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1308 alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1309
1310 SET_AIACK(cif->hwport);
1311 SET_AIE(cif->hwport);
1312 SET_AIACK(cif->hwport);
1313 cif->is_reset = 1;
1314
1315 return 0;
1316}
1317
1318static struct snd_pcm_hardware snd_riptide_playback = {
1319 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1320 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1321 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1322 .formats =
1323 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1324 | SNDRV_PCM_FMTBIT_U16_LE,
1325 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1326 .rate_min = 5500,
1327 .rate_max = 48000,
1328 .channels_min = 1,
1329 .channels_max = 2,
1330 .buffer_bytes_max = (64 * 1024),
1331 .period_bytes_min = PAGE_SIZE >> 1,
1332 .period_bytes_max = PAGE_SIZE << 8,
1333 .periods_min = 2,
1334 .periods_max = 64,
1335 .fifo_size = 0,
1336};
1337static struct snd_pcm_hardware snd_riptide_capture = {
1338 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1339 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1340 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1341 .formats =
1342 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1343 | SNDRV_PCM_FMTBIT_U16_LE,
1344 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1345 .rate_min = 5500,
1346 .rate_max = 48000,
1347 .channels_min = 1,
1348 .channels_max = 2,
1349 .buffer_bytes_max = (64 * 1024),
1350 .period_bytes_min = PAGE_SIZE >> 1,
1351 .period_bytes_max = PAGE_SIZE << 3,
1352 .periods_min = 2,
1353 .periods_max = 64,
1354 .fifo_size = 0,
1355};
1356
1357static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1358 *substream)
1359{
1360 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 struct pcmhw *data = get_pcmhwdev(substream);
1363 struct cmdif *cif = chip->cif;
1364 union cmdret rptr = CMDRET_ZERO;
1365 snd_pcm_uframes_t ret;
1366
1367 SEND_GPOS(cif, 0, data->id, &rptr);
1368 if (data->size && runtime->period_size) {
1369 snd_printdd
1370 ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1371 data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1372 bytes_to_frames(runtime, rptr.retlongs[1]),
1373 bytes_to_frames(runtime,
1374 rptr.retlongs[1]) % runtime->period_size);
1375 if (rptr.retlongs[1] > data->pointer)
1376 ret =
1377 bytes_to_frames(runtime,
1378 rptr.retlongs[1] % data->size);
1379 else
1380 ret =
1381 bytes_to_frames(runtime,
1382 data->pointer % data->size);
1383 } else {
1384 snd_printdd("stream not started or strange parms (%d %ld)\n",
1385 data->size, runtime->period_size);
1386 ret = bytes_to_frames(runtime, 0);
1387 }
1388 return ret;
1389}
1390
1391static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1392{
1393 int i, j;
1394 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1395 struct pcmhw *data = get_pcmhwdev(substream);
1396 struct cmdif *cif = chip->cif;
1397 union cmdret rptr = CMDRET_ZERO;
1398
1399 spin_lock(&chip->lock);
1400 switch (cmd) {
1401 case SNDRV_PCM_TRIGGER_START:
1402 case SNDRV_PCM_TRIGGER_RESUME:
1403 if (!(data->state & ST_PLAY)) {
1404 SEND_SSTR(cif, data->id, data->sgdlist.addr);
1405 SET_AIE(cif->hwport);
1406 data->state = ST_PLAY;
1407 if (data->mixer != 0xff)
1408 setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1409 chip->openstreams++;
1410 data->oldpos = 0;
1411 data->pointer = 0;
1412 }
1413 break;
1414 case SNDRV_PCM_TRIGGER_STOP:
1415 case SNDRV_PCM_TRIGGER_SUSPEND:
1416 if (data->mixer != 0xff)
1417 setmixer(cif, data->mixer, 0, 0);
1418 setmixer(cif, data->mixer, 0, 0);
1419 SEND_KSTR(cif, data->id);
1420 data->state = ST_STOP;
1421 chip->openstreams--;
1422 j = 0;
1423 do {
1424 i = rptr.retlongs[1];
1425 SEND_GPOS(cif, 0, data->id, &rptr);
1426 udelay(1);
1427 } while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
1428 if (j > MAX_WRITE_RETRY)
1429 snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1430 break;
1431 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1432 if (!(data->state & ST_PAUSE)) {
1433 SEND_PSTR(cif, data->id);
1434 data->state |= ST_PAUSE;
1435 chip->openstreams--;
1436 }
1437 break;
1438 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1439 if (data->state & ST_PAUSE) {
1440 SEND_SSTR(cif, data->id, data->sgdlist.addr);
1441 data->state &= ~ST_PAUSE;
1442 chip->openstreams++;
1443 }
1444 break;
1445 default:
1446 spin_unlock(&chip->lock);
1447 return -EINVAL;
1448 }
1449 spin_unlock(&chip->lock);
1450 return 0;
1451}
1452
1453static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1454{
1455 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1456 struct snd_pcm_runtime *runtime = substream->runtime;
1457 struct pcmhw *data = get_pcmhwdev(substream);
1458 struct cmdif *cif = chip->cif;
1459 unsigned char *lbuspath = NULL;
1460 unsigned int rate, channels;
1461 int err = 0;
1462 snd_pcm_format_t format;
1463
1464 if (snd_BUG_ON(!cif || !data))
1465 return -EINVAL;
1466
1467 snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1468 runtime->channels, runtime->format, runtime->rate);
1469
1470 spin_lock_irq(&chip->lock);
1471 channels = runtime->channels;
1472 format = runtime->format;
1473 rate = runtime->rate;
1474 switch (channels) {
1475 case 1:
1476 if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1477 lbuspath = data->paths.noconv;
1478 else
1479 lbuspath = data->paths.mono;
1480 break;
1481 case 2:
1482 if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1483 lbuspath = data->paths.noconv;
1484 else
1485 lbuspath = data->paths.stereo;
1486 break;
1487 }
1488 snd_printdd("use sgdlist at 0x%p\n",
1489 data->sgdlist.area);
1490 if (data->sgdlist.area) {
1491 unsigned int i, j, size, pages, f, pt, period;
1492 struct sgd *c, *p = NULL;
1493
1494 size = frames_to_bytes(runtime, runtime->buffer_size);
1495 period = frames_to_bytes(runtime, runtime->period_size);
1496 f = PAGE_SIZE;
1497 while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1498 f = f >> 1;
1499 pages = (size + f - 1) / f;
1500 data->size = size;
1501 data->pages = pages;
1502 snd_printdd
1503 ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1504 size, pages, f, period);
1505 pt = 0;
1506 j = 0;
1507 for (i = 0; i < pages; i++) {
1508 unsigned int ofs, addr;
1509 c = &data->sgdbuf[i];
1510 if (p)
1511 p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1512 (i *
1513 sizeof(struct
1514 sgd)));
1515 c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
1516 ofs = j << PAGE_SHIFT;
1517 addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
1518 c->dwSegPtrPhys = cpu_to_le32(addr);
1519 pt = (pt + f) % PAGE_SIZE;
1520 if (pt == 0)
1521 j++;
1522 c->dwSegLen = cpu_to_le32(f);
1523 c->dwStat_Ctl =
1524 cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1525 IEOC_ENABLE);
1526 p = c;
1527 size -= f;
1528 }
1529 data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1530 }
1531 if (lbuspath && lbuspath != data->lbuspath) {
1532 if (data->lbuspath)
1533 freelbuspath(cif, data->source, data->lbuspath);
1534 alloclbuspath(cif, data->source, lbuspath,
1535 &data->mixer, data->intdec);
1536 data->lbuspath = lbuspath;
1537 data->rate = 0;
1538 }
1539 if (data->rate != rate || data->format != format ||
1540 data->channels != channels) {
1541 data->rate = rate;
1542 data->format = format;
1543 data->channels = channels;
1544 if (setsampleformat
1545 (cif, data->mixer, data->id, channels, format)
1546 || setsamplerate(cif, data->intdec, rate))
1547 err = -EIO;
1548 }
1549 spin_unlock_irq(&chip->lock);
1550 return err;
1551}
1552
1553static int
1554snd_riptide_hw_params(struct snd_pcm_substream *substream,
1555 struct snd_pcm_hw_params *hw_params)
1556{
1557 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1558 struct pcmhw *data = get_pcmhwdev(substream);
1559 struct snd_dma_buffer *sgdlist = &data->sgdlist;
1560 int err;
1561
1562 snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1563 sgdlist->area, (unsigned long)sgdlist->addr,
1564 (int)sgdlist->bytes);
1565 if (sgdlist->area)
1566 snd_dma_free_pages(sgdlist);
1567 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
1568 snd_dma_pci_data(chip->pci),
1569 sizeof(struct sgd) * (DESC_MAX_MASK + 1),
1570 sgdlist)) < 0) {
1571 snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1572 (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1573 return err;
1574 }
1575 data->sgdbuf = (struct sgd *)sgdlist->area;
1576 return snd_pcm_lib_malloc_pages(substream,
1577 params_buffer_bytes(hw_params));
1578}
1579
1580static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1581{
1582 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1583 struct pcmhw *data = get_pcmhwdev(substream);
1584 struct cmdif *cif = chip->cif;
1585
1586 if (cif && data) {
1587 if (data->lbuspath)
1588 freelbuspath(cif, data->source, data->lbuspath);
1589 data->lbuspath = NULL;
1590 data->source = 0xff;
1591 data->intdec[0] = 0xff;
1592 data->intdec[1] = 0xff;
1593
1594 if (data->sgdlist.area) {
1595 snd_dma_free_pages(&data->sgdlist);
1596 data->sgdlist.area = NULL;
1597 }
1598 }
1599 return snd_pcm_lib_free_pages(substream);
1600}
1601
1602static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1603{
1604 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1605 struct snd_pcm_runtime *runtime = substream->runtime;
1606 struct pcmhw *data;
1607 int sub_num = substream->number;
1608
1609 chip->playback_substream[sub_num] = substream;
1610 runtime->hw = snd_riptide_playback;
1611
1612 data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1613 if (data == NULL)
1614 return -ENOMEM;
1615 data->paths = lbus_play_paths[sub_num];
1616 data->id = play_ids[sub_num];
1617 data->source = play_sources[sub_num];
1618 data->intdec[0] = 0xff;
1619 data->intdec[1] = 0xff;
1620 data->state = ST_STOP;
1621 runtime->private_data = data;
1622 return snd_pcm_hw_constraint_integer(runtime,
1623 SNDRV_PCM_HW_PARAM_PERIODS);
1624}
1625
1626static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1627{
1628 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1629 struct snd_pcm_runtime *runtime = substream->runtime;
1630 struct pcmhw *data;
1631
1632 chip->capture_substream = substream;
1633 runtime->hw = snd_riptide_capture;
1634
1635 data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1636 if (data == NULL)
1637 return -ENOMEM;
1638 data->paths = lbus_rec_path;
1639 data->id = PADC;
1640 data->source = ACLNK2PADC;
1641 data->intdec[0] = 0xff;
1642 data->intdec[1] = 0xff;
1643 data->state = ST_STOP;
1644 runtime->private_data = data;
1645 return snd_pcm_hw_constraint_integer(runtime,
1646 SNDRV_PCM_HW_PARAM_PERIODS);
1647}
1648
1649static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1650{
1651 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1652 struct pcmhw *data = get_pcmhwdev(substream);
1653 int sub_num = substream->number;
1654
1655 substream->runtime->private_data = NULL;
1656 chip->playback_substream[sub_num] = NULL;
1657 kfree(data);
1658 return 0;
1659}
1660
1661static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1662{
1663 struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1664 struct pcmhw *data = get_pcmhwdev(substream);
1665
1666 substream->runtime->private_data = NULL;
1667 chip->capture_substream = NULL;
1668 kfree(data);
1669 return 0;
1670}
1671
1672static struct snd_pcm_ops snd_riptide_playback_ops = {
1673 .open = snd_riptide_playback_open,
1674 .close = snd_riptide_playback_close,
1675 .ioctl = snd_pcm_lib_ioctl,
1676 .hw_params = snd_riptide_hw_params,
1677 .hw_free = snd_riptide_hw_free,
1678 .prepare = snd_riptide_prepare,
1679 .page = snd_pcm_sgbuf_ops_page,
1680 .trigger = snd_riptide_trigger,
1681 .pointer = snd_riptide_pointer,
1682};
1683static struct snd_pcm_ops snd_riptide_capture_ops = {
1684 .open = snd_riptide_capture_open,
1685 .close = snd_riptide_capture_close,
1686 .ioctl = snd_pcm_lib_ioctl,
1687 .hw_params = snd_riptide_hw_params,
1688 .hw_free = snd_riptide_hw_free,
1689 .prepare = snd_riptide_prepare,
1690 .page = snd_pcm_sgbuf_ops_page,
1691 .trigger = snd_riptide_trigger,
1692 .pointer = snd_riptide_pointer,
1693};
1694
1695static int snd_riptide_pcm(struct snd_riptide *chip, int device)
1696{
1697 struct snd_pcm *pcm;
1698 int err;
1699
1700 if ((err =
1701 snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
1702 &pcm)) < 0)
1703 return err;
1704 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1705 &snd_riptide_playback_ops);
1706 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1707 &snd_riptide_capture_ops);
1708 pcm->private_data = chip;
1709 pcm->info_flags = 0;
1710 strcpy(pcm->name, "RIPTIDE");
1711 chip->pcm = pcm;
1712 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1713 snd_dma_pci_data(chip->pci),
1714 64 * 1024, 128 * 1024);
1715 return 0;
1716}
1717
1718static irqreturn_t
1719snd_riptide_interrupt(int irq, void *dev_id)
1720{
1721 struct snd_riptide *chip = dev_id;
1722 struct cmdif *cif = chip->cif;
1723
1724 if (cif) {
1725 chip->received_irqs++;
1726 if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1727 IS_EOCIRQ(cif->hwport)) {
1728 chip->handled_irqs++;
1729 tasklet_schedule(&chip->riptide_tq);
1730 }
1731 if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1732 chip->handled_irqs++;
1733 snd_mpu401_uart_interrupt(irq,
1734 chip->rmidi->private_data);
1735 }
1736 SET_AIACK(cif->hwport);
1737 }
1738 return IRQ_HANDLED;
1739}
1740
1741static void
1742snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1743 unsigned short val)
1744{
1745 struct snd_riptide *chip = ac97->private_data;
1746 struct cmdif *cif = chip->cif;
1747 union cmdret rptr = CMDRET_ZERO;
1748 int i = 0;
1749
1750 if (snd_BUG_ON(!cif))
1751 return;
1752
1753 snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1754 do {
1755 SEND_SACR(cif, val, reg);
1756 SEND_RACR(cif, reg, &rptr);
1757 } while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
1758 if (i > MAX_WRITE_RETRY)
1759 snd_printdd("Write AC97 reg failed\n");
1760}
1761
1762static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1763 unsigned short reg)
1764{
1765 struct snd_riptide *chip = ac97->private_data;
1766 struct cmdif *cif = chip->cif;
1767 union cmdret rptr = CMDRET_ZERO;
1768
1769 if (snd_BUG_ON(!cif))
1770 return 0;
1771
1772 if (SEND_RACR(cif, reg, &rptr) != 0)
1773 SEND_RACR(cif, reg, &rptr);
1774 snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1775 return rptr.retwords[1];
1776}
1777
1778static int snd_riptide_initialize(struct snd_riptide *chip)
1779{
1780 struct cmdif *cif;
1781 unsigned int device_id;
1782 int err;
1783
1784 if (snd_BUG_ON(!chip))
1785 return -EINVAL;
1786
1787 cif = chip->cif;
1788 if (!cif) {
1789 if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
1790 return -ENOMEM;
1791 cif->hwport = (struct riptideport *)chip->port;
1792 spin_lock_init(&cif->lock);
1793 chip->cif = cif;
1794 }
1795 cif->is_reset = 0;
1796 if ((err = riptide_reset(cif, chip)) != 0)
1797 return err;
1798 device_id = chip->device_id;
1799 switch (device_id) {
1800 case 0x4310:
1801 case 0x4320:
1802 case 0x4330:
1803 snd_printdd("Modem enable?\n");
1804 SEND_SETDPLL(cif);
1805 break;
1806 }
1807 snd_printdd("Enabling MPU IRQs\n");
1808 if (chip->rmidi)
1809 SET_EMPUIRQ(cif->hwport);
1810 return err;
1811}
1812
1813static int snd_riptide_free(struct snd_riptide *chip)
1814{
1815 struct cmdif *cif;
1816
1817 if (!chip)
1818 return 0;
1819
1820 if ((cif = chip->cif)) {
1821 SET_GRESET(cif->hwport);
1822 udelay(100);
1823 UNSET_GRESET(cif->hwport);
1824 kfree(chip->cif);
1825 }
1826 if (chip->irq >= 0)
1827 free_irq(chip->irq, chip);
1828 release_firmware(chip->fw_entry);
1829 release_and_free_resource(chip->res_port);
1830 kfree(chip);
1831 return 0;
1832}
1833
1834static int snd_riptide_dev_free(struct snd_device *device)
1835{
1836 struct snd_riptide *chip = device->device_data;
1837
1838 return snd_riptide_free(chip);
1839}
1840
1841static int
1842snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
1843 struct snd_riptide **rchip)
1844{
1845 struct snd_riptide *chip;
1846 struct riptideport *hwport;
1847 int err;
1848 static struct snd_device_ops ops = {
1849 .dev_free = snd_riptide_dev_free,
1850 };
1851
1852 *rchip = NULL;
1853 if ((err = pci_enable_device(pci)) < 0)
1854 return err;
1855 if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
1856 return -ENOMEM;
1857
1858 spin_lock_init(&chip->lock);
1859 chip->card = card;
1860 chip->pci = pci;
1861 chip->irq = -1;
1862 chip->openstreams = 0;
1863 chip->port = pci_resource_start(pci, 0);
1864 chip->received_irqs = 0;
1865 chip->handled_irqs = 0;
1866 chip->cif = NULL;
1867 tasklet_init(&chip->riptide_tq, riptide_handleirq, (unsigned long)chip);
1868
1869 if ((chip->res_port =
1870 request_region(chip->port, 64, "RIPTIDE")) == NULL) {
1871 snd_printk(KERN_ERR
1872 "Riptide: unable to grab region 0x%lx-0x%lx\n",
1873 chip->port, chip->port + 64 - 1);
1874 snd_riptide_free(chip);
1875 return -EBUSY;
1876 }
1877 hwport = (struct riptideport *)chip->port;
1878 UNSET_AIE(hwport);
1879
1880 if (request_irq(pci->irq, snd_riptide_interrupt, IRQF_SHARED,
1881 KBUILD_MODNAME, chip)) {
1882 snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1883 pci->irq);
1884 snd_riptide_free(chip);
1885 return -EBUSY;
1886 }
1887 chip->irq = pci->irq;
1888 chip->device_id = pci->device;
1889 pci_set_master(pci);
1890 if ((err = snd_riptide_initialize(chip)) < 0) {
1891 snd_riptide_free(chip);
1892 return err;
1893 }
1894
1895 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1896 snd_riptide_free(chip);
1897 return err;
1898 }
1899
1900 *rchip = chip;
1901 return 0;
1902}
1903
1904static void
1905snd_riptide_proc_read(struct snd_info_entry *entry,
1906 struct snd_info_buffer *buffer)
1907{
1908 struct snd_riptide *chip = entry->private_data;
1909 struct pcmhw *data;
1910 int i;
1911 struct cmdif *cif = NULL;
1912 unsigned char p[256];
1913 unsigned short rval = 0, lval = 0;
1914 unsigned int rate;
1915
1916 if (!chip)
1917 return;
1918
1919 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1920 snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1921 chip->device_id, chip->handled_irqs, chip->received_irqs);
1922 for (i = 0; i < 64; i += 4)
1923 snd_iprintf(buffer, "%c%02x: %08x",
1924 (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
1925 if ((cif = chip->cif)) {
1926 snd_iprintf(buffer,
1927 "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1928 chip->firmware.firmware.ASIC,
1929 chip->firmware.firmware.CODEC,
1930 chip->firmware.firmware.AUXDSP,
1931 chip->firmware.firmware.PROG);
1932 snd_iprintf(buffer, "\nDigital mixer:");
1933 for (i = 0; i < 12; i++) {
1934 getmixer(cif, i, &rval, &lval);
1935 snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1936 }
1937 snd_iprintf(buffer,
1938 "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1939 cif->cmdcnt, cif->errcnt,
1940 cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1941 }
1942 snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1943 for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
1944 if (chip->playback_substream[i]
1945 && chip->playback_substream[i]->runtime
1946 && (data =
1947 chip->playback_substream[i]->runtime->private_data)) {
1948 snd_iprintf(buffer,
1949 "stream: %d mixer: %d source: %d (%d,%d)\n",
1950 data->id, data->mixer, data->source,
1951 data->intdec[0], data->intdec[1]);
1952 if (!(getsamplerate(cif, data->intdec, &rate)))
1953 snd_iprintf(buffer, "rate: %d\n", rate);
1954 }
1955 }
1956 if (chip->capture_substream
1957 && chip->capture_substream->runtime
1958 && (data = chip->capture_substream->runtime->private_data)) {
1959 snd_iprintf(buffer,
1960 "stream: %d mixer: %d source: %d (%d,%d)\n",
1961 data->id, data->mixer,
1962 data->source, data->intdec[0], data->intdec[1]);
1963 if (!(getsamplerate(cif, data->intdec, &rate)))
1964 snd_iprintf(buffer, "rate: %d\n", rate);
1965 }
1966 snd_iprintf(buffer, "Paths:\n");
1967 i = getpaths(cif, p);
1968 while (i >= 2) {
1969 i -= 2;
1970 snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
1971 }
1972 snd_iprintf(buffer, "\n");
1973}
1974
1975static void snd_riptide_proc_init(struct snd_riptide *chip)
1976{
1977 struct snd_info_entry *entry;
1978
1979 if (!snd_card_proc_new(chip->card, "riptide", &entry))
1980 snd_info_set_text_ops(entry, chip, snd_riptide_proc_read);
1981}
1982
1983static int snd_riptide_mixer(struct snd_riptide *chip)
1984{
1985 struct snd_ac97_bus *pbus;
1986 struct snd_ac97_template ac97;
1987 int err = 0;
1988 static struct snd_ac97_bus_ops ops = {
1989 .write = snd_riptide_codec_write,
1990 .read = snd_riptide_codec_read,
1991 };
1992
1993 memset(&ac97, 0, sizeof(ac97));
1994 ac97.private_data = chip;
1995 ac97.scaps = AC97_SCAP_SKIP_MODEM;
1996
1997 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
1998 return err;
1999
2000 chip->ac97_bus = pbus;
2001 ac97.pci = chip->pci;
2002 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2003 return err;
2004 return err;
2005}
2006
2007#ifdef SUPPORT_JOYSTICK
2008
2009static int
2010snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
2011{
2012 static int dev;
2013 struct gameport *gameport;
2014 int ret;
2015
2016 if (dev >= SNDRV_CARDS)
2017 return -ENODEV;
2018
2019 if (!enable[dev]) {
2020 ret = -ENOENT;
2021 goto inc_dev;
2022 }
2023
2024 if (!joystick_port[dev]) {
2025 ret = 0;
2026 goto inc_dev;
2027 }
2028
2029 gameport = gameport_allocate_port();
2030 if (!gameport) {
2031 ret = -ENOMEM;
2032 goto inc_dev;
2033 }
2034 if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
2035 snd_printk(KERN_WARNING
2036 "Riptide: cannot grab gameport 0x%x\n",
2037 joystick_port[dev]);
2038 gameport_free_port(gameport);
2039 ret = -EBUSY;
2040 goto inc_dev;
2041 }
2042
2043 gameport->io = joystick_port[dev];
2044 gameport_register_port(gameport);
2045 pci_set_drvdata(pci, gameport);
2046
2047 ret = 0;
2048inc_dev:
2049 dev++;
2050 return ret;
2051}
2052
2053static void snd_riptide_joystick_remove(struct pci_dev *pci)
2054{
2055 struct gameport *gameport = pci_get_drvdata(pci);
2056 if (gameport) {
2057 release_region(gameport->io, 8);
2058 gameport_unregister_port(gameport);
2059 }
2060}
2061#endif
2062
2063static int
2064snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2065{
2066 static int dev;
2067 struct snd_card *card;
2068 struct snd_riptide *chip;
2069 unsigned short val;
2070 int err;
2071
2072 if (dev >= SNDRV_CARDS)
2073 return -ENODEV;
2074 if (!enable[dev]) {
2075 dev++;
2076 return -ENOENT;
2077 }
2078
2079 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2080 0, &card);
2081 if (err < 0)
2082 return err;
2083 err = snd_riptide_create(card, pci, &chip);
2084 if (err < 0)
2085 goto error;
2086 card->private_data = chip;
2087 err = snd_riptide_pcm(chip, 0);
2088 if (err < 0)
2089 goto error;
2090 err = snd_riptide_mixer(chip);
2091 if (err < 0)
2092 goto error;
2093
2094 val = LEGACY_ENABLE_ALL;
2095 if (opl3_port[dev])
2096 val |= LEGACY_ENABLE_FM;
2097#ifdef SUPPORT_JOYSTICK
2098 if (joystick_port[dev])
2099 val |= LEGACY_ENABLE_GAMEPORT;
2100#endif
2101 if (mpu_port[dev])
2102 val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
2103 val |= (chip->irq << 4) & 0xf0;
2104 pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
2105 if (mpu_port[dev]) {
2106 val = mpu_port[dev];
2107 pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
2108 err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2109 val, MPU401_INFO_IRQ_HOOK, -1,
2110 &chip->rmidi);
2111 if (err < 0)
2112 snd_printk(KERN_WARNING
2113 "Riptide: Can't Allocate MPU at 0x%x\n",
2114 val);
2115 else
2116 chip->mpuaddr = val;
2117 }
2118 if (opl3_port[dev]) {
2119 val = opl3_port[dev];
2120 pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
2121 err = snd_opl3_create(card, val, val + 2,
2122 OPL3_HW_RIPTIDE, 0, &chip->opl3);
2123 if (err < 0)
2124 snd_printk(KERN_WARNING
2125 "Riptide: Can't Allocate OPL3 at 0x%x\n",
2126 val);
2127 else {
2128 chip->opladdr = val;
2129 err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
2130 if (err < 0)
2131 snd_printk(KERN_WARNING
2132 "Riptide: Can't Allocate OPL3-HWDEP\n");
2133 }
2134 }
2135#ifdef SUPPORT_JOYSTICK
2136 if (joystick_port[dev]) {
2137 val = joystick_port[dev];
2138 pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
2139 chip->gameaddr = val;
2140 }
2141#endif
2142
2143 strcpy(card->driver, "RIPTIDE");
2144 strcpy(card->shortname, "Riptide");
2145#ifdef SUPPORT_JOYSTICK
2146 snprintf(card->longname, sizeof(card->longname),
2147 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2148 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2149 chip->opladdr, chip->gameaddr);
2150#else
2151 snprintf(card->longname, sizeof(card->longname),
2152 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2153 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2154 chip->opladdr);
2155#endif
2156 snd_riptide_proc_init(chip);
2157 err = snd_card_register(card);
2158 if (err < 0)
2159 goto error;
2160 pci_set_drvdata(pci, card);
2161 dev++;
2162 return 0;
2163
2164 error:
2165 snd_card_free(card);
2166 return err;
2167}
2168
2169static void snd_card_riptide_remove(struct pci_dev *pci)
2170{
2171 snd_card_free(pci_get_drvdata(pci));
2172}
2173
2174static struct pci_driver driver = {
2175 .name = KBUILD_MODNAME,
2176 .id_table = snd_riptide_ids,
2177 .probe = snd_card_riptide_probe,
2178 .remove = snd_card_riptide_remove,
2179 .driver = {
2180 .pm = RIPTIDE_PM_OPS,
2181 },
2182};
2183
2184#ifdef SUPPORT_JOYSTICK
2185static struct pci_driver joystick_driver = {
2186 .name = KBUILD_MODNAME "-joystick",
2187 .id_table = snd_riptide_joystick_ids,
2188 .probe = snd_riptide_joystick_probe,
2189 .remove = snd_riptide_joystick_remove,
2190};
2191#endif
2192
2193static int __init alsa_card_riptide_init(void)
2194{
2195 int err;
2196 err = pci_register_driver(&driver);
2197 if (err < 0)
2198 return err;
2199#if defined(SUPPORT_JOYSTICK)
2200 err = pci_register_driver(&joystick_driver);
2201
2202 if (err < 0)
2203 pci_unregister_driver(&driver);
2204#endif
2205 return err;
2206}
2207
2208static void __exit alsa_card_riptide_exit(void)
2209{
2210 pci_unregister_driver(&driver);
2211#if defined(SUPPORT_JOYSTICK)
2212 pci_unregister_driver(&joystick_driver);
2213#endif
2214}
2215
2216module_init(alsa_card_riptide_init);
2217module_exit(alsa_card_riptide_exit);
2218