1/*
2 * Driver for Digigram pcxhr compatible soundcards
3 *
4 * hwdep device manager
5 *
6 * Copyright (c) 2004 by Digigram <alsa@digigram.com>
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 */
22
23#include <sound/driver.h>
24#include <linux/interrupt.h>
25#include <linux/vmalloc.h>
26#include <linux/firmware.h>
27#include <linux/pci.h>
28#include <asm/io.h>
29#include <sound/core.h>
30#include <sound/hwdep.h>
31#include "pcxhr.h"
32#include "pcxhr_mixer.h"
33#include "pcxhr_hwdep.h"
34#include "pcxhr_core.h"
35
36
37#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
38#if !defined(CONFIG_USE_PCXHRLOADER) && !defined(CONFIG_SND_PCXHR)     /* built-in \
39	kernel */
40#define SND_PCXHR_FW_LOADER	/* use the standard firmware loader */
41#endif
42#endif
43
44
45/*
46 * get basic information and init pcxhr card
47 */
48
49static int pcxhr_init_board(struct pcxhr_mgr *mgr)
50{
51	int err;
52	struct pcxhr_rmh rmh;
53	int card_streams;
54
55	/* calc the number of all streams used */
56	if (mgr->mono_capture)
57		card_streams = mgr->capture_chips * 2;
58	else
59		card_streams = mgr->capture_chips;
60	card_streams += mgr->playback_chips * PCXHR_PLAYBACK_STREAMS;
61
62	/* enable interrupts */
63	pcxhr_enable_dsp(mgr);
64
65	pcxhr_init_rmh(&rmh, CMD_SUPPORTED);
66	err = pcxhr_send_msg(mgr, &rmh);
67	if (err)
68		return err;
69	/* test 8 or 12 phys out */
70	snd_assert((rmh.stat[0] & MASK_FIRST_FIELD) == mgr->playback_chips*2,
71		   return -EINVAL);
72	/* test 8 or 2 phys in */
73	snd_assert(((rmh.stat[0] >> (2*FIELD_SIZE)) & MASK_FIRST_FIELD) ==
74		   mgr->capture_chips * 2, return -EINVAL);
75	/* test max nb substream per board */
76	snd_assert((rmh.stat[1] & 0x5F) >= card_streams, return -EINVAL);
77	/* test max nb substream per pipe */
78	snd_assert(((rmh.stat[1]>>7)&0x5F) >= PCXHR_PLAYBACK_STREAMS, return -EINVAL);
79
80	pcxhr_init_rmh(&rmh, CMD_VERSION);
81	/* firmware num for DSP */
82	rmh.cmd[0] |= mgr->firmware_num;
83	/* transfer granularity in samples (should be multiple of 48) */
84	rmh.cmd[1] = (1<<23) + PCXHR_GRANULARITY;
85	rmh.cmd_len = 2;
86	err = pcxhr_send_msg(mgr, &rmh);
87	if (err)
88		return err;
89	snd_printdd("PCXHR DSP version is %d.%d.%d\n",
90		    (rmh.stat[0]>>16)&0xff, (rmh.stat[0]>>8)&0xff, rmh.stat[0]&0xff);
91	mgr->dsp_version = rmh.stat[0];
92
93	/* get options */
94	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
95	rmh.cmd[0] |= IO_NUM_REG_STATUS;
96	rmh.cmd[1]  = REG_STATUS_OPTIONS;
97	rmh.cmd_len = 2;
98	err = pcxhr_send_msg(mgr, &rmh);
99	if (err)
100		return err;
101
102	if ((rmh.stat[1] & REG_STATUS_OPT_DAUGHTER_MASK) == REG_STATUS_OPT_ANALOG_BOARD)
103		mgr->board_has_analog = 1;	/* analog addon board available */
104	else
105		/* analog addon board not available -> no support for instance */
106		return -EINVAL;
107
108	/* unmute inputs */
109	err = pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS,
110					  REG_CONT_UNMUTE_INPUTS, NULL);
111	if (err)
112		return err;
113	/* unmute outputs */
114	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ); /* a write to IO_NUM_REG_MUTE_OUT mutes! */
115	rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
116	err = pcxhr_send_msg(mgr, &rmh);
117	return err;
118}
119
120void pcxhr_reset_board(struct pcxhr_mgr *mgr)
121{
122	struct pcxhr_rmh rmh;
123
124	if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) {
125		/* mute outputs */
126		/* a read to IO_NUM_REG_MUTE_OUT register unmutes! */
127		pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
128		rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
129		pcxhr_send_msg(mgr, &rmh);
130		/* mute inputs */
131		pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS, 0, NULL);
132	}
133	/* reset pcxhr dsp */
134	if (mgr->dsp_loaded & ( 1 << PCXHR_FIRMWARE_DSP_EPRM_INDEX))
135		pcxhr_reset_dsp(mgr);
136	/* reset second xilinx */
137	if (mgr->dsp_loaded & ( 1 << PCXHR_FIRMWARE_XLX_COM_INDEX))
138		pcxhr_reset_xilinx_com(mgr);
139	return;
140}
141
142
143/*
144 *  allocate a playback/capture pipe (pcmp0/pcmc0)
145 */
146static int pcxhr_dsp_allocate_pipe( struct pcxhr_mgr *mgr, struct pcxhr_pipe *pipe,
147				    int is_capture, int pin)
148{
149	int stream_count, audio_count;
150	int err;
151	struct pcxhr_rmh rmh;
152
153	if (is_capture) {
154		stream_count = 1;
155		if (mgr->mono_capture)
156			audio_count = 1;
157		else
158			audio_count = 2;
159	} else {
160		stream_count = PCXHR_PLAYBACK_STREAMS;
161		audio_count = 2;	/* always stereo */
162	}
163	snd_printdd("snd_add_ref_pipe pin(%d) pcm%c0\n", pin, is_capture ? 'c' : 'p');
164	pipe->is_capture = is_capture;
165	pipe->first_audio = pin;
166	/* define pipe (P_PCM_ONLY_MASK (0x020000) is not necessary) */
167	pcxhr_init_rmh(&rmh, CMD_RES_PIPE);
168	pcxhr_set_pipe_cmd_params(&rmh, is_capture, pin, audio_count, stream_count);
169	err = pcxhr_send_msg(mgr, &rmh);
170	if (err < 0) {
171		snd_printk(KERN_ERR "error pipe allocation (CMD_RES_PIPE) err=%x!\n", err );
172		return err;
173	}
174	pipe->status = PCXHR_PIPE_DEFINED;
175
176	return 0;
177}
178
179/*
180 *  free playback/capture pipe (pcmp0/pcmc0)
181 */
182
183
184static int pcxhr_config_pipes(struct pcxhr_mgr *mgr)
185{
186	int err, i, j;
187	struct snd_pcxhr *chip;
188	struct pcxhr_pipe *pipe;
189
190	/* allocate the pipes on the dsp */
191	for (i = 0; i < mgr->num_cards; i++) {
192		chip = mgr->chip[i];
193		if (chip->nb_streams_play) {
194			pipe = &chip->playback_pipe;
195			err = pcxhr_dsp_allocate_pipe( mgr, pipe, 0, i*2);
196			if (err)
197				return err;
198			for(j = 0; j < chip->nb_streams_play; j++)
199				chip->playback_stream[j].pipe = pipe;
200		}
201		for (j = 0; j < chip->nb_streams_capt; j++) {
202			pipe = &chip->capture_pipe[j];
203			err = pcxhr_dsp_allocate_pipe(mgr, pipe, 1, i*2 + j);
204			if (err)
205				return err;
206			chip->capture_stream[j].pipe = pipe;
207		}
208	}
209	return 0;
210}
211
212static int pcxhr_start_pipes(struct pcxhr_mgr *mgr)
213{
214	int i, j;
215	struct snd_pcxhr *chip;
216	int playback_mask = 0;
217	int capture_mask = 0;
218
219	/* start all the pipes on the dsp */
220	for (i = 0; i < mgr->num_cards; i++) {
221		chip = mgr->chip[i];
222		if (chip->nb_streams_play)
223			playback_mask |= (1 << chip->playback_pipe.first_audio);
224		for (j = 0; j < chip->nb_streams_capt; j++)
225			capture_mask |= (1 << chip->capture_pipe[j].first_audio);
226	}
227	return pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 1);
228}
229
230
231static int pcxhr_dsp_load(struct pcxhr_mgr *mgr, int index, const struct firmware *dsp)
232{
233	int err, card_index;
234
235	snd_printdd("loading dsp [%d] size = %Zd\n", index, dsp->size);
236
237	switch (index) {
238	case PCXHR_FIRMWARE_XLX_INT_INDEX:
239		pcxhr_reset_xilinx_com(mgr);
240		return pcxhr_load_xilinx_binary(mgr, dsp, 0);
241
242	case PCXHR_FIRMWARE_XLX_COM_INDEX:
243		pcxhr_reset_xilinx_com(mgr);
244		return pcxhr_load_xilinx_binary(mgr, dsp, 1);
245
246	case PCXHR_FIRMWARE_DSP_EPRM_INDEX:
247		pcxhr_reset_dsp(mgr);
248		return pcxhr_load_eeprom_binary(mgr, dsp);
249
250	case PCXHR_FIRMWARE_DSP_BOOT_INDEX:
251		return pcxhr_load_boot_binary(mgr, dsp);
252
253	case PCXHR_FIRMWARE_DSP_MAIN_INDEX:
254		err = pcxhr_load_dsp_binary(mgr, dsp);
255		if (err)
256			return err;
257		break;	/* continue with first init */
258	default:
259		snd_printk(KERN_ERR "wrong file index\n");
260		return -EFAULT;
261	} /* end of switch file index*/
262
263	/* first communication with embedded */
264	err = pcxhr_init_board(mgr);
265        if (err < 0) {
266		snd_printk(KERN_ERR "pcxhr could not be set up\n");
267		return err;
268	}
269	err = pcxhr_config_pipes(mgr);
270        if (err < 0) {
271		snd_printk(KERN_ERR "pcxhr pipes could not be set up\n");
272		return err;
273	}
274       	/* create devices and mixer in accordance with HW options*/
275        for (card_index = 0; card_index < mgr->num_cards; card_index++) {
276		struct snd_pcxhr *chip = mgr->chip[card_index];
277
278		if ((err = pcxhr_create_pcm(chip)) < 0)
279			return err;
280
281		if (card_index == 0) {
282			if ((err = pcxhr_create_mixer(chip->mgr)) < 0)
283				return err;
284		}
285		if ((err = snd_card_register(chip->card)) < 0)
286			return err;
287	}
288	err = pcxhr_start_pipes(mgr);
289        if (err < 0) {
290		snd_printk(KERN_ERR "pcxhr pipes could not be started\n");
291		return err;
292	}
293	snd_printdd("pcxhr firmware downloaded and successfully set up\n");
294
295	return 0;
296}
297
298/*
299 * fw loader entry
300 */
301#ifdef SND_PCXHR_FW_LOADER
302
303int pcxhr_setup_firmware(struct pcxhr_mgr *mgr)
304{
305	static char *fw_files[5] = {
306		"xi_1_882.dat",
307		"xc_1_882.dat",
308		"e321_512.e56",
309		"b321_512.b56",
310		"d321_512.d56"
311	};
312	char path[32];
313
314	const struct firmware *fw_entry;
315	int i, err;
316
317	for (i = 0; i < ARRAY_SIZE(fw_files); i++) {
318		sprintf(path, "pcxhr/%s", fw_files[i]);
319		if (request_firmware(&fw_entry, path, &mgr->pci->dev)) {
320			snd_printk(KERN_ERR "pcxhr: can't load firmware %s\n", path);
321			return -ENOENT;
322		}
323		/* fake hwdep dsp record */
324		err = pcxhr_dsp_load(mgr, i, fw_entry);
325		release_firmware(fw_entry);
326		if (err < 0)
327			return err;
328		mgr->dsp_loaded |= 1 << i;
329	}
330	return 0;
331}
332
333MODULE_FIRMWARE("pcxhr/xi_1_882.dat");
334MODULE_FIRMWARE("pcxhr/xc_1_882.dat");
335MODULE_FIRMWARE("pcxhr/e321_512.e56");
336MODULE_FIRMWARE("pcxhr/b321_512.b56");
337MODULE_FIRMWARE("pcxhr/d321_512.d56");
338
339#else /* old style firmware loading */
340
341/* pcxhr hwdep interface id string */
342#define PCXHR_HWDEP_ID       "pcxhr loader"
343
344
345static int pcxhr_hwdep_dsp_status(struct snd_hwdep *hw,
346				  struct snd_hwdep_dsp_status *info)
347{
348	strcpy(info->id, "pcxhr");
349        info->num_dsps = PCXHR_FIRMWARE_FILES_MAX_INDEX;
350
351	if (hw->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX))
352		info->chip_ready = 1;
353
354	info->version = PCXHR_DRIVER_VERSION;
355	return 0;
356}
357
358static int pcxhr_hwdep_dsp_load(struct snd_hwdep *hw,
359				struct snd_hwdep_dsp_image *dsp)
360{
361	struct pcxhr_mgr *mgr = hw->private_data;
362	int err;
363	struct firmware fw;
364
365	fw.size = dsp->length;
366	fw.data = vmalloc(fw.size);
367	if (! fw.data) {
368		snd_printk(KERN_ERR "pcxhr: cannot allocate dsp image (%lu bytes)\n",
369			   (unsigned long)fw.size);
370		return -ENOMEM;
371	}
372	if (copy_from_user(fw.data, dsp->image, dsp->length)) {
373		vfree(fw.data);
374		return -EFAULT;
375	}
376	err = pcxhr_dsp_load(mgr, dsp->index, &fw);
377	vfree(fw.data);
378	if (err < 0)
379		return err;
380	mgr->dsp_loaded |= 1 << dsp->index;
381	return 0;
382}
383
384static int pcxhr_hwdep_open(struct snd_hwdep *hw, struct file *file)
385{
386	return 0;
387}
388
389static int pcxhr_hwdep_release(struct snd_hwdep *hw, struct file *file)
390{
391	return 0;
392}
393
394int pcxhr_setup_firmware(struct pcxhr_mgr *mgr)
395{
396	int err;
397	struct snd_hwdep *hw;
398
399	/* only create hwdep interface for first cardX (see "index" module parameter)*/
400	if ((err = snd_hwdep_new(mgr->chip[0]->card, PCXHR_HWDEP_ID, 0, &hw)) < 0)
401		return err;
402
403	hw->iface = SNDRV_HWDEP_IFACE_PCXHR;
404	hw->private_data = mgr;
405	hw->ops.open = pcxhr_hwdep_open;
406	hw->ops.release = pcxhr_hwdep_release;
407	hw->ops.dsp_status = pcxhr_hwdep_dsp_status;
408	hw->ops.dsp_load = pcxhr_hwdep_dsp_load;
409	hw->exclusive = 1;
410	mgr->dsp_loaded = 0;
411	sprintf(hw->name, PCXHR_HWDEP_ID);
412
413	if ((err = snd_card_register(mgr->chip[0]->card)) < 0)
414		return err;
415	return 0;
416}
417
418#endif /* SND_PCXHR_FW_LOADER */
419