1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29076c22dSTakashi Sakamoto /*
39076c22dSTakashi Sakamoto  * bebob_maudio.c - a part of driver for BeBoB based devices
49076c22dSTakashi Sakamoto  *
59076c22dSTakashi Sakamoto  * Copyright (c) 2013-2014 Takashi Sakamoto
69076c22dSTakashi Sakamoto  */
79076c22dSTakashi Sakamoto 
89076c22dSTakashi Sakamoto #include "./bebob.h"
93149ac48STakashi Sakamoto #include <sound/control.h>
109076c22dSTakashi Sakamoto 
119076c22dSTakashi Sakamoto /*
123149ac48STakashi Sakamoto  * Just powering on, Firewire 410/Audiophile/1814 and ProjectMix I/O wait to
139076c22dSTakashi Sakamoto  * download firmware blob. To enable these devices, drivers should upload
149076c22dSTakashi Sakamoto  * firmware blob and send a command to initialize configuration to factory
159076c22dSTakashi Sakamoto  * settings when completing uploading. Then these devices generate bus reset
169076c22dSTakashi Sakamoto  * and are recognized as new devices with the firmware.
179076c22dSTakashi Sakamoto  *
18a2b2a779STakashi Sakamoto  * But with firmware version 5058 or later, the firmware is stored to flash
19a2b2a779STakashi Sakamoto  * memory in the device and drivers can tell bootloader to load the firmware
20a2b2a779STakashi Sakamoto  * by sending a cue. This cue must be sent one time.
21a2b2a779STakashi Sakamoto  *
229076c22dSTakashi Sakamoto  * For streaming, both of output and input streams are needed for Firewire 410
239076c22dSTakashi Sakamoto  * and Ozonic. The single stream is OK for the other devices even if the clock
249076c22dSTakashi Sakamoto  * source is not SYT-Match (I note no devices use SYT-Match).
259076c22dSTakashi Sakamoto  *
269076c22dSTakashi Sakamoto  * Without streaming, the devices except for Firewire Audiophile can mix any
279076c22dSTakashi Sakamoto  * input and output. For this reason, Audiophile cannot be used as standalone
289076c22dSTakashi Sakamoto  * mixer.
293149ac48STakashi Sakamoto  *
303149ac48STakashi Sakamoto  * Firewire 1814 and ProjectMix I/O uses special firmware. It will be freezed
313149ac48STakashi Sakamoto  * when receiving any commands which the firmware can't understand. These
323149ac48STakashi Sakamoto  * devices utilize completely different system to control. It is some
333149ac48STakashi Sakamoto  * write-transaction directly into a certain address. All of addresses for mixer
343149ac48STakashi Sakamoto  * functionality is between 0xffc700700000 to 0xffc70070009c.
359076c22dSTakashi Sakamoto  */
369076c22dSTakashi Sakamoto 
37a2b2a779STakashi Sakamoto /* Offset from information register */
38a2b2a779STakashi Sakamoto #define INFO_OFFSET_SW_DATE	0x20
39a2b2a779STakashi Sakamoto 
40a2b2a779STakashi Sakamoto /* Bootloader Protocol Version 1 */
41a2b2a779STakashi Sakamoto #define MAUDIO_BOOTLOADER_CUE1	0x00000001
42a2b2a779STakashi Sakamoto /*
43a2b2a779STakashi Sakamoto  * Initializing configuration to factory settings (= 0x1101), (swapped in line),
44a2b2a779STakashi Sakamoto  * Command code is zero (= 0x00),
45a2b2a779STakashi Sakamoto  * the number of operands is zero (= 0x00)(at least significant byte)
46a2b2a779STakashi Sakamoto  */
47a2b2a779STakashi Sakamoto #define MAUDIO_BOOTLOADER_CUE2	0x01110000
48a2b2a779STakashi Sakamoto /* padding */
49a2b2a779STakashi Sakamoto #define MAUDIO_BOOTLOADER_CUE3	0x00000000
50a2b2a779STakashi Sakamoto 
519b5f0edfSTakashi Sakamoto #define MAUDIO_SPECIFIC_ADDRESS	0xffc700000000ULL
529076c22dSTakashi Sakamoto 
539076c22dSTakashi Sakamoto #define METER_OFFSET		0x00600000
549076c22dSTakashi Sakamoto 
559076c22dSTakashi Sakamoto /* some device has sync info after metering data */
563149ac48STakashi Sakamoto #define METER_SIZE_SPECIAL	84	/* with sync info */
579076c22dSTakashi Sakamoto #define METER_SIZE_FW410	76	/* with sync info */
589076c22dSTakashi Sakamoto #define METER_SIZE_AUDIOPHILE	60	/* with sync info */
599076c22dSTakashi Sakamoto #define METER_SIZE_SOLO		52	/* with sync info */
609076c22dSTakashi Sakamoto #define METER_SIZE_OZONIC	48
619076c22dSTakashi Sakamoto #define METER_SIZE_NRV10	80
629076c22dSTakashi Sakamoto 
639076c22dSTakashi Sakamoto /* labels for metering */
649076c22dSTakashi Sakamoto #define ANA_IN		"Analog In"
659076c22dSTakashi Sakamoto #define ANA_OUT		"Analog Out"
669076c22dSTakashi Sakamoto #define DIG_IN		"Digital In"
679076c22dSTakashi Sakamoto #define SPDIF_IN	"S/PDIF In"
689076c22dSTakashi Sakamoto #define ADAT_IN		"ADAT In"
699076c22dSTakashi Sakamoto #define DIG_OUT		"Digital Out"
709076c22dSTakashi Sakamoto #define SPDIF_OUT	"S/PDIF Out"
719076c22dSTakashi Sakamoto #define ADAT_OUT	"ADAT Out"
729076c22dSTakashi Sakamoto #define STRM_IN		"Stream In"
739076c22dSTakashi Sakamoto #define AUX_OUT		"Aux Out"
749076c22dSTakashi Sakamoto #define HP_OUT		"HP Out"
759076c22dSTakashi Sakamoto /* for NRV */
769076c22dSTakashi Sakamoto #define UNKNOWN_METER	"Unknown"
779076c22dSTakashi Sakamoto 
783149ac48STakashi Sakamoto struct special_params {
793149ac48STakashi Sakamoto 	bool is1814;
803149ac48STakashi Sakamoto 	unsigned int clk_src;
813149ac48STakashi Sakamoto 	unsigned int dig_in_fmt;
823149ac48STakashi Sakamoto 	unsigned int dig_out_fmt;
833149ac48STakashi Sakamoto 	unsigned int clk_lock;
843149ac48STakashi Sakamoto 	struct snd_ctl_elem_id *ctl_id_sync;
853149ac48STakashi Sakamoto };
863149ac48STakashi Sakamoto 
87a2b2a779STakashi Sakamoto /*
88a2b2a779STakashi Sakamoto  * For some M-Audio devices, this module just send cue to load firmware. After
89a2b2a779STakashi Sakamoto  * loading, the device generates bus reset and newly detected.
90a2b2a779STakashi Sakamoto  *
91a2b2a779STakashi Sakamoto  * If we make any transactions to load firmware, the operation may failed.
92a2b2a779STakashi Sakamoto  */
snd_bebob_maudio_load_firmware(struct fw_unit * unit)93a2b2a779STakashi Sakamoto int snd_bebob_maudio_load_firmware(struct fw_unit *unit)
94a2b2a779STakashi Sakamoto {
95a2b2a779STakashi Sakamoto 	struct fw_device *device = fw_parent_device(unit);
96a2b2a779STakashi Sakamoto 	int err, rcode;
97a2b2a779STakashi Sakamoto 	u64 date;
98493626f2STakashi Sakamoto 	__le32 *cues;
99a2b2a779STakashi Sakamoto 
100a2b2a779STakashi Sakamoto 	/* check date of software used to build */
101a2b2a779STakashi Sakamoto 	err = snd_bebob_read_block(unit, INFO_OFFSET_SW_DATE,
102a2b2a779STakashi Sakamoto 				   &date, sizeof(u64));
103a2b2a779STakashi Sakamoto 	if (err < 0)
104493626f2STakashi Sakamoto 		return err;
105a2b2a779STakashi Sakamoto 	/*
106a2b2a779STakashi Sakamoto 	 * firmware version 5058 or later has date later than "20070401", but
107a2b2a779STakashi Sakamoto 	 * 'date' is not null-terminated.
108a2b2a779STakashi Sakamoto 	 */
1099b5f0edfSTakashi Sakamoto 	if (date < 0x3230303730343031LL) {
110a2b2a779STakashi Sakamoto 		dev_err(&unit->device,
111a2b2a779STakashi Sakamoto 			"Use firmware version 5058 or later\n");
112493626f2STakashi Sakamoto 		return -ENXIO;
113a2b2a779STakashi Sakamoto 	}
114a2b2a779STakashi Sakamoto 
115493626f2STakashi Sakamoto 	cues = kmalloc_array(3, sizeof(*cues), GFP_KERNEL);
116493626f2STakashi Sakamoto 	if (!cues)
117493626f2STakashi Sakamoto 		return -ENOMEM;
118493626f2STakashi Sakamoto 
119493626f2STakashi Sakamoto 	cues[0] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE1);
120493626f2STakashi Sakamoto 	cues[1] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE2);
121493626f2STakashi Sakamoto 	cues[2] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE3);
122493626f2STakashi Sakamoto 
123a2b2a779STakashi Sakamoto 	rcode = fw_run_transaction(device->card, TCODE_WRITE_BLOCK_REQUEST,
124a2b2a779STakashi Sakamoto 				   device->node_id, device->generation,
125a2b2a779STakashi Sakamoto 				   device->max_speed, BEBOB_ADDR_REG_REQ,
126493626f2STakashi Sakamoto 				   cues, 3 * sizeof(*cues));
127493626f2STakashi Sakamoto 	kfree(cues);
128a2b2a779STakashi Sakamoto 	if (rcode != RCODE_COMPLETE) {
129a2b2a779STakashi Sakamoto 		dev_err(&unit->device,
130a2b2a779STakashi Sakamoto 			"Failed to send a cue to load firmware\n");
131a2b2a779STakashi Sakamoto 		err = -EIO;
132a2b2a779STakashi Sakamoto 	}
133493626f2STakashi Sakamoto 
134a2b2a779STakashi Sakamoto 	return err;
135a2b2a779STakashi Sakamoto }
136a2b2a779STakashi Sakamoto 
1379076c22dSTakashi Sakamoto static inline int
get_meter(struct snd_bebob * bebob,void * buf,unsigned int size)1389076c22dSTakashi Sakamoto get_meter(struct snd_bebob *bebob, void *buf, unsigned int size)
1399076c22dSTakashi Sakamoto {
1409076c22dSTakashi Sakamoto 	return snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
1419076c22dSTakashi Sakamoto 				  MAUDIO_SPECIFIC_ADDRESS + METER_OFFSET,
1429076c22dSTakashi Sakamoto 				  buf, size, 0);
1439076c22dSTakashi Sakamoto }
1449076c22dSTakashi Sakamoto 
1453149ac48STakashi Sakamoto static int
check_clk_sync(struct snd_bebob * bebob,unsigned int size,bool * sync)1463149ac48STakashi Sakamoto check_clk_sync(struct snd_bebob *bebob, unsigned int size, bool *sync)
1473149ac48STakashi Sakamoto {
1483149ac48STakashi Sakamoto 	int err;
1493149ac48STakashi Sakamoto 	u8 *buf;
1503149ac48STakashi Sakamoto 
1513149ac48STakashi Sakamoto 	buf = kmalloc(size, GFP_KERNEL);
1523149ac48STakashi Sakamoto 	if (buf == NULL)
1533149ac48STakashi Sakamoto 		return -ENOMEM;
1543149ac48STakashi Sakamoto 
1553149ac48STakashi Sakamoto 	err = get_meter(bebob, buf, size);
1563149ac48STakashi Sakamoto 	if (err < 0)
1573149ac48STakashi Sakamoto 		goto end;
1583149ac48STakashi Sakamoto 
1593149ac48STakashi Sakamoto 	/* if synced, this value is the same as SFC of FDF in CIP header */
1603149ac48STakashi Sakamoto 	*sync = (buf[size - 2] != 0xff);
1613149ac48STakashi Sakamoto end:
1623149ac48STakashi Sakamoto 	kfree(buf);
1633149ac48STakashi Sakamoto 	return err;
1643149ac48STakashi Sakamoto }
1653149ac48STakashi Sakamoto 
1663149ac48STakashi Sakamoto /*
1673149ac48STakashi Sakamoto  * dig_fmt: 0x00:S/PDIF, 0x01:ADAT
1683149ac48STakashi Sakamoto  * clk_lock: 0x00:unlock, 0x01:lock
1693149ac48STakashi Sakamoto  */
1703149ac48STakashi Sakamoto static int
avc_maudio_set_special_clk(struct snd_bebob * bebob,unsigned int clk_src,unsigned int dig_in_fmt,unsigned int dig_out_fmt,unsigned int clk_lock)1713149ac48STakashi Sakamoto avc_maudio_set_special_clk(struct snd_bebob *bebob, unsigned int clk_src,
1723149ac48STakashi Sakamoto 			   unsigned int dig_in_fmt, unsigned int dig_out_fmt,
1733149ac48STakashi Sakamoto 			   unsigned int clk_lock)
1743149ac48STakashi Sakamoto {
1753149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
1763149ac48STakashi Sakamoto 	int err;
1773149ac48STakashi Sakamoto 	u8 *buf;
1783149ac48STakashi Sakamoto 
1793149ac48STakashi Sakamoto 	if (amdtp_stream_running(&bebob->rx_stream) ||
1803149ac48STakashi Sakamoto 	    amdtp_stream_running(&bebob->tx_stream))
1813149ac48STakashi Sakamoto 		return -EBUSY;
1823149ac48STakashi Sakamoto 
1833149ac48STakashi Sakamoto 	buf = kmalloc(12, GFP_KERNEL);
1843149ac48STakashi Sakamoto 	if (buf == NULL)
1853149ac48STakashi Sakamoto 		return -ENOMEM;
1863149ac48STakashi Sakamoto 
1873149ac48STakashi Sakamoto 	buf[0]  = 0x00;		/* CONTROL */
1883149ac48STakashi Sakamoto 	buf[1]  = 0xff;		/* UNIT */
1893149ac48STakashi Sakamoto 	buf[2]  = 0x00;		/* vendor dependent */
1903149ac48STakashi Sakamoto 	buf[3]  = 0x04;		/* company ID high */
1913149ac48STakashi Sakamoto 	buf[4]  = 0x00;		/* company ID middle */
1923149ac48STakashi Sakamoto 	buf[5]  = 0x04;		/* company ID low */
1933149ac48STakashi Sakamoto 	buf[6]  = 0xff & clk_src;	/* clock source */
1943149ac48STakashi Sakamoto 	buf[7]  = 0xff & dig_in_fmt;	/* input digital format */
1953149ac48STakashi Sakamoto 	buf[8]  = 0xff & dig_out_fmt;	/* output digital format */
1963149ac48STakashi Sakamoto 	buf[9]  = 0xff & clk_lock;	/* lock these settings */
1973149ac48STakashi Sakamoto 	buf[10] = 0x00;		/* padding  */
1983149ac48STakashi Sakamoto 	buf[11] = 0x00;		/* padding */
1993149ac48STakashi Sakamoto 
2003149ac48STakashi Sakamoto 	err = fcp_avc_transaction(bebob->unit, buf, 12, buf, 12,
2013149ac48STakashi Sakamoto 				  BIT(1) | BIT(2) | BIT(3) | BIT(4) |
2023149ac48STakashi Sakamoto 				  BIT(5) | BIT(6) | BIT(7) | BIT(8) |
2033149ac48STakashi Sakamoto 				  BIT(9));
2043149ac48STakashi Sakamoto 	if ((err > 0) && (err < 10))
2053149ac48STakashi Sakamoto 		err = -EIO;
2063149ac48STakashi Sakamoto 	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
2073149ac48STakashi Sakamoto 		err = -ENOSYS;
2083149ac48STakashi Sakamoto 	else if (buf[0] == 0x0a) /* REJECTED */
2093149ac48STakashi Sakamoto 		err = -EINVAL;
2103149ac48STakashi Sakamoto 	if (err < 0)
2113149ac48STakashi Sakamoto 		goto end;
2123149ac48STakashi Sakamoto 
2133149ac48STakashi Sakamoto 	params->clk_src		= buf[6];
2143149ac48STakashi Sakamoto 	params->dig_in_fmt	= buf[7];
2153149ac48STakashi Sakamoto 	params->dig_out_fmt	= buf[8];
2163149ac48STakashi Sakamoto 	params->clk_lock	= buf[9];
2173149ac48STakashi Sakamoto 
2183149ac48STakashi Sakamoto 	if (params->ctl_id_sync)
2193149ac48STakashi Sakamoto 		snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
2203149ac48STakashi Sakamoto 			       params->ctl_id_sync);
2213149ac48STakashi Sakamoto 
2223149ac48STakashi Sakamoto 	err = 0;
2233149ac48STakashi Sakamoto end:
2243149ac48STakashi Sakamoto 	kfree(buf);
2253149ac48STakashi Sakamoto 	return err;
2263149ac48STakashi Sakamoto }
2273149ac48STakashi Sakamoto static void
special_stream_formation_set(struct snd_bebob * bebob)2283149ac48STakashi Sakamoto special_stream_formation_set(struct snd_bebob *bebob)
2293149ac48STakashi Sakamoto {
2303149ac48STakashi Sakamoto 	static const unsigned int ch_table[2][2][3] = {
2313149ac48STakashi Sakamoto 		/* AMDTP_OUT_STREAM */
2323149ac48STakashi Sakamoto 		{ {  6,  6,  4 },	/* SPDIF */
2333149ac48STakashi Sakamoto 		  { 12,  8,  4 } },	/* ADAT */
2343149ac48STakashi Sakamoto 		/* AMDTP_IN_STREAM */
2353149ac48STakashi Sakamoto 		{ { 10, 10,  2 },	/* SPDIF */
2363149ac48STakashi Sakamoto 		  { 16, 12,  2 } }	/* ADAT */
2373149ac48STakashi Sakamoto 	};
2383149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
2393149ac48STakashi Sakamoto 	unsigned int i, max;
2403149ac48STakashi Sakamoto 
2413149ac48STakashi Sakamoto 	max = SND_BEBOB_STRM_FMT_ENTRIES - 1;
2423149ac48STakashi Sakamoto 	if (!params->is1814)
2433149ac48STakashi Sakamoto 		max -= 2;
2443149ac48STakashi Sakamoto 
2453149ac48STakashi Sakamoto 	for (i = 0; i < max; i++) {
2463149ac48STakashi Sakamoto 		bebob->tx_stream_formations[i + 1].pcm =
2473149ac48STakashi Sakamoto 			ch_table[AMDTP_IN_STREAM][params->dig_in_fmt][i / 2];
2483149ac48STakashi Sakamoto 		bebob->tx_stream_formations[i + 1].midi = 1;
2493149ac48STakashi Sakamoto 
2503149ac48STakashi Sakamoto 		bebob->rx_stream_formations[i + 1].pcm =
2513149ac48STakashi Sakamoto 			ch_table[AMDTP_OUT_STREAM][params->dig_out_fmt][i / 2];
2523149ac48STakashi Sakamoto 		bebob->rx_stream_formations[i + 1].midi = 1;
2533149ac48STakashi Sakamoto 	}
2543149ac48STakashi Sakamoto }
2553149ac48STakashi Sakamoto 
2563149ac48STakashi Sakamoto static int add_special_controls(struct snd_bebob *bebob);
2573149ac48STakashi Sakamoto int
snd_bebob_maudio_special_discover(struct snd_bebob * bebob,bool is1814)2583149ac48STakashi Sakamoto snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814)
2593149ac48STakashi Sakamoto {
2603149ac48STakashi Sakamoto 	struct special_params *params;
2613149ac48STakashi Sakamoto 	int err;
2623149ac48STakashi Sakamoto 
263a3aaf7d2STakashi Sakamoto 	params = devm_kzalloc(&bebob->card->card_dev,
264a3aaf7d2STakashi Sakamoto 			      sizeof(struct special_params), GFP_KERNEL);
265a3aaf7d2STakashi Sakamoto 	if (!params)
2663149ac48STakashi Sakamoto 		return -ENOMEM;
2673149ac48STakashi Sakamoto 
2683149ac48STakashi Sakamoto 	mutex_lock(&bebob->mutex);
2693149ac48STakashi Sakamoto 
2703149ac48STakashi Sakamoto 	bebob->maudio_special_quirk = (void *)params;
2713149ac48STakashi Sakamoto 	params->is1814 = is1814;
2723149ac48STakashi Sakamoto 
2733149ac48STakashi Sakamoto 	/* initialize these parameters because driver is not allowed to ask */
2743149ac48STakashi Sakamoto 	bebob->rx_stream.context = ERR_PTR(-1);
2753149ac48STakashi Sakamoto 	bebob->tx_stream.context = ERR_PTR(-1);
2763149ac48STakashi Sakamoto 	err = avc_maudio_set_special_clk(bebob, 0x03, 0x00, 0x00, 0x00);
2773149ac48STakashi Sakamoto 	if (err < 0) {
2783149ac48STakashi Sakamoto 		dev_err(&bebob->unit->device,
2793149ac48STakashi Sakamoto 			"fail to initialize clock params: %d\n", err);
2803149ac48STakashi Sakamoto 		goto end;
2813149ac48STakashi Sakamoto 	}
2823149ac48STakashi Sakamoto 
2833149ac48STakashi Sakamoto 	err = add_special_controls(bebob);
2843149ac48STakashi Sakamoto 	if (err < 0)
2853149ac48STakashi Sakamoto 		goto end;
2863149ac48STakashi Sakamoto 
2873149ac48STakashi Sakamoto 	special_stream_formation_set(bebob);
2883149ac48STakashi Sakamoto 
2893149ac48STakashi Sakamoto 	if (params->is1814) {
2903149ac48STakashi Sakamoto 		bebob->midi_input_ports = 1;
2913149ac48STakashi Sakamoto 		bebob->midi_output_ports = 1;
2923149ac48STakashi Sakamoto 	} else {
2933149ac48STakashi Sakamoto 		bebob->midi_input_ports = 2;
2943149ac48STakashi Sakamoto 		bebob->midi_output_ports = 2;
2953149ac48STakashi Sakamoto 	}
2963149ac48STakashi Sakamoto end:
2973149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
2983149ac48STakashi Sakamoto 	return err;
2993149ac48STakashi Sakamoto }
3003149ac48STakashi Sakamoto 
3013149ac48STakashi Sakamoto /* Input plug shows actual rate. Output plug is needless for this purpose. */
special_get_rate(struct snd_bebob * bebob,unsigned int * rate)3023149ac48STakashi Sakamoto static int special_get_rate(struct snd_bebob *bebob, unsigned int *rate)
3033149ac48STakashi Sakamoto {
3043149ac48STakashi Sakamoto 	int err, trials;
3053149ac48STakashi Sakamoto 
3063149ac48STakashi Sakamoto 	trials = 0;
3073149ac48STakashi Sakamoto 	do {
3083149ac48STakashi Sakamoto 		err = avc_general_get_sig_fmt(bebob->unit, rate,
3093149ac48STakashi Sakamoto 					      AVC_GENERAL_PLUG_DIR_IN, 0);
3103149ac48STakashi Sakamoto 	} while (err == -EAGAIN && ++trials < 3);
3113149ac48STakashi Sakamoto 
3123149ac48STakashi Sakamoto 	return err;
3133149ac48STakashi Sakamoto }
special_set_rate(struct snd_bebob * bebob,unsigned int rate)3143149ac48STakashi Sakamoto static int special_set_rate(struct snd_bebob *bebob, unsigned int rate)
3153149ac48STakashi Sakamoto {
3163149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
3173149ac48STakashi Sakamoto 	int err;
3183149ac48STakashi Sakamoto 
3193149ac48STakashi Sakamoto 	err = avc_general_set_sig_fmt(bebob->unit, rate,
3203149ac48STakashi Sakamoto 				      AVC_GENERAL_PLUG_DIR_OUT, 0);
3213149ac48STakashi Sakamoto 	if (err < 0)
3223149ac48STakashi Sakamoto 		goto end;
3233149ac48STakashi Sakamoto 
3243149ac48STakashi Sakamoto 	/*
3253149ac48STakashi Sakamoto 	 * Just after changing sampling rate for output, a followed command
3263149ac48STakashi Sakamoto 	 * for input is easy to fail. This is a workaround fot this issue.
3273149ac48STakashi Sakamoto 	 */
3283149ac48STakashi Sakamoto 	msleep(100);
3293149ac48STakashi Sakamoto 
3303149ac48STakashi Sakamoto 	err = avc_general_set_sig_fmt(bebob->unit, rate,
3313149ac48STakashi Sakamoto 				      AVC_GENERAL_PLUG_DIR_IN, 0);
3323149ac48STakashi Sakamoto 	if (err < 0)
3333149ac48STakashi Sakamoto 		goto end;
3343149ac48STakashi Sakamoto 
3353149ac48STakashi Sakamoto 	if (params->ctl_id_sync)
3363149ac48STakashi Sakamoto 		snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
3373149ac48STakashi Sakamoto 			       params->ctl_id_sync);
3383149ac48STakashi Sakamoto end:
3393149ac48STakashi Sakamoto 	return err;
3403149ac48STakashi Sakamoto }
3413149ac48STakashi Sakamoto 
3423149ac48STakashi Sakamoto /* Clock source control for special firmware */
343782fbec7STakashi Sakamoto static const enum snd_bebob_clock_type special_clk_types[] = {
344ba517713STakashi Sakamoto 	SND_BEBOB_CLOCK_TYPE_INTERNAL,	/* With digital mute */
345ba517713STakashi Sakamoto 	SND_BEBOB_CLOCK_TYPE_EXTERNAL,	/* SPDIF/ADAT */
346ba517713STakashi Sakamoto 	SND_BEBOB_CLOCK_TYPE_EXTERNAL,	/* Word Clock */
347ba517713STakashi Sakamoto 	SND_BEBOB_CLOCK_TYPE_INTERNAL,
348ba517713STakashi Sakamoto };
special_clk_get(struct snd_bebob * bebob,unsigned int * id)3493149ac48STakashi Sakamoto static int special_clk_get(struct snd_bebob *bebob, unsigned int *id)
3503149ac48STakashi Sakamoto {
3513149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
3523149ac48STakashi Sakamoto 	*id = params->clk_src;
3533149ac48STakashi Sakamoto 	return 0;
3543149ac48STakashi Sakamoto }
special_clk_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * einf)3553149ac48STakashi Sakamoto static int special_clk_ctl_info(struct snd_kcontrol *kctl,
3563149ac48STakashi Sakamoto 				struct snd_ctl_elem_info *einf)
3573149ac48STakashi Sakamoto {
358554d8983STakashi Sakamoto 	static const char *const special_clk_labels[] = {
359554d8983STakashi Sakamoto 		"Internal with Digital Mute",
360554d8983STakashi Sakamoto 		"Digital",
361554d8983STakashi Sakamoto 		"Word Clock",
362554d8983STakashi Sakamoto 		"Internal"
363554d8983STakashi Sakamoto 	};
364ba517713STakashi Sakamoto 	return snd_ctl_enum_info(einf, 1, ARRAY_SIZE(special_clk_types),
36541be5164STakashi Iwai 				 special_clk_labels);
3663149ac48STakashi Sakamoto }
special_clk_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)3673149ac48STakashi Sakamoto static int special_clk_ctl_get(struct snd_kcontrol *kctl,
3683149ac48STakashi Sakamoto 			       struct snd_ctl_elem_value *uval)
3693149ac48STakashi Sakamoto {
3703149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
3713149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
3723149ac48STakashi Sakamoto 	uval->value.enumerated.item[0] = params->clk_src;
3733149ac48STakashi Sakamoto 	return 0;
3743149ac48STakashi Sakamoto }
special_clk_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)3753149ac48STakashi Sakamoto static int special_clk_ctl_put(struct snd_kcontrol *kctl,
3763149ac48STakashi Sakamoto 			       struct snd_ctl_elem_value *uval)
3773149ac48STakashi Sakamoto {
3783149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
3793149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
3803149ac48STakashi Sakamoto 	int err, id;
3813149ac48STakashi Sakamoto 
3823149ac48STakashi Sakamoto 	id = uval->value.enumerated.item[0];
383ba517713STakashi Sakamoto 	if (id >= ARRAY_SIZE(special_clk_types))
384eb12f72eSTakashi Sakamoto 		return -EINVAL;
3853149ac48STakashi Sakamoto 
38690140116STakashi Sakamoto 	mutex_lock(&bebob->mutex);
38790140116STakashi Sakamoto 
3883149ac48STakashi Sakamoto 	err = avc_maudio_set_special_clk(bebob, id,
3893149ac48STakashi Sakamoto 					 params->dig_in_fmt,
3903149ac48STakashi Sakamoto 					 params->dig_out_fmt,
3913149ac48STakashi Sakamoto 					 params->clk_lock);
3923149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
3933149ac48STakashi Sakamoto 
394f77ac91eSTakashi Sakamoto 	if (err >= 0)
395f77ac91eSTakashi Sakamoto 		err = 1;
396f77ac91eSTakashi Sakamoto 
397f77ac91eSTakashi Sakamoto 	return err;
3983149ac48STakashi Sakamoto }
399905e46acSBhumika Goyal static const struct snd_kcontrol_new special_clk_ctl = {
4003149ac48STakashi Sakamoto 	.name	= "Clock Source",
4013149ac48STakashi Sakamoto 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
4023149ac48STakashi Sakamoto 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
4033149ac48STakashi Sakamoto 	.info	= special_clk_ctl_info,
4043149ac48STakashi Sakamoto 	.get	= special_clk_ctl_get,
4053149ac48STakashi Sakamoto 	.put	= special_clk_ctl_put
4063149ac48STakashi Sakamoto };
4073149ac48STakashi Sakamoto 
4083149ac48STakashi Sakamoto /* Clock synchronization control for special firmware */
special_sync_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * einf)4093149ac48STakashi Sakamoto static int special_sync_ctl_info(struct snd_kcontrol *kctl,
4103149ac48STakashi Sakamoto 				 struct snd_ctl_elem_info *einf)
4113149ac48STakashi Sakamoto {
4123149ac48STakashi Sakamoto 	einf->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4133149ac48STakashi Sakamoto 	einf->count = 1;
4143149ac48STakashi Sakamoto 	einf->value.integer.min = 0;
4153149ac48STakashi Sakamoto 	einf->value.integer.max = 1;
4163149ac48STakashi Sakamoto 
4173149ac48STakashi Sakamoto 	return 0;
4183149ac48STakashi Sakamoto }
special_sync_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)4193149ac48STakashi Sakamoto static int special_sync_ctl_get(struct snd_kcontrol *kctl,
4203149ac48STakashi Sakamoto 				struct snd_ctl_elem_value *uval)
4213149ac48STakashi Sakamoto {
4223149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
4233149ac48STakashi Sakamoto 	int err;
4243149ac48STakashi Sakamoto 	bool synced = 0;
4253149ac48STakashi Sakamoto 
4263149ac48STakashi Sakamoto 	err = check_clk_sync(bebob, METER_SIZE_SPECIAL, &synced);
4273149ac48STakashi Sakamoto 	if (err >= 0)
4283149ac48STakashi Sakamoto 		uval->value.integer.value[0] = synced;
4293149ac48STakashi Sakamoto 
4303149ac48STakashi Sakamoto 	return 0;
4313149ac48STakashi Sakamoto }
432905e46acSBhumika Goyal static const struct snd_kcontrol_new special_sync_ctl = {
4333149ac48STakashi Sakamoto 	.name	= "Sync Status",
4343149ac48STakashi Sakamoto 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
4353149ac48STakashi Sakamoto 	.access	= SNDRV_CTL_ELEM_ACCESS_READ,
4363149ac48STakashi Sakamoto 	.info	= special_sync_ctl_info,
4373149ac48STakashi Sakamoto 	.get	= special_sync_ctl_get,
4383149ac48STakashi Sakamoto };
4393149ac48STakashi Sakamoto 
4405a0438f4STakashi Sakamoto /* Digital input interface control for special firmware */
441e7ced413STakashi Iwai static const char *const special_dig_in_iface_labels[] = {
4423149ac48STakashi Sakamoto 	"S/PDIF Optical", "S/PDIF Coaxial", "ADAT Optical"
4433149ac48STakashi Sakamoto };
special_dig_in_iface_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * einf)4443149ac48STakashi Sakamoto static int special_dig_in_iface_ctl_info(struct snd_kcontrol *kctl,
4453149ac48STakashi Sakamoto 					 struct snd_ctl_elem_info *einf)
4463149ac48STakashi Sakamoto {
44741be5164STakashi Iwai 	return snd_ctl_enum_info(einf, 1,
44841be5164STakashi Iwai 				 ARRAY_SIZE(special_dig_in_iface_labels),
44941be5164STakashi Iwai 				 special_dig_in_iface_labels);
4503149ac48STakashi Sakamoto }
special_dig_in_iface_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)4513149ac48STakashi Sakamoto static int special_dig_in_iface_ctl_get(struct snd_kcontrol *kctl,
4523149ac48STakashi Sakamoto 					struct snd_ctl_elem_value *uval)
4533149ac48STakashi Sakamoto {
4543149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
4553149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
4563149ac48STakashi Sakamoto 	unsigned int dig_in_iface;
4573149ac48STakashi Sakamoto 	int err, val;
4583149ac48STakashi Sakamoto 
4593149ac48STakashi Sakamoto 	mutex_lock(&bebob->mutex);
4603149ac48STakashi Sakamoto 
4613149ac48STakashi Sakamoto 	err = avc_audio_get_selector(bebob->unit, 0x00, 0x04,
4623149ac48STakashi Sakamoto 				     &dig_in_iface);
4633149ac48STakashi Sakamoto 	if (err < 0) {
4643149ac48STakashi Sakamoto 		dev_err(&bebob->unit->device,
4653149ac48STakashi Sakamoto 			"fail to get digital input interface: %d\n", err);
4663149ac48STakashi Sakamoto 		goto end;
4673149ac48STakashi Sakamoto 	}
4683149ac48STakashi Sakamoto 
4693149ac48STakashi Sakamoto 	/* encoded id for user value */
4703149ac48STakashi Sakamoto 	val = (params->dig_in_fmt << 1) | (dig_in_iface & 0x01);
4713149ac48STakashi Sakamoto 
4723149ac48STakashi Sakamoto 	/* for ADAT Optical */
4733149ac48STakashi Sakamoto 	if (val > 2)
4743149ac48STakashi Sakamoto 		val = 2;
4753149ac48STakashi Sakamoto 
4763149ac48STakashi Sakamoto 	uval->value.enumerated.item[0] = val;
4773149ac48STakashi Sakamoto end:
4783149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
4793149ac48STakashi Sakamoto 	return err;
4803149ac48STakashi Sakamoto }
special_dig_in_iface_ctl_set(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)4813149ac48STakashi Sakamoto static int special_dig_in_iface_ctl_set(struct snd_kcontrol *kctl,
4823149ac48STakashi Sakamoto 					struct snd_ctl_elem_value *uval)
4833149ac48STakashi Sakamoto {
4843149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
4853149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
4863149ac48STakashi Sakamoto 	unsigned int id, dig_in_fmt, dig_in_iface;
4873149ac48STakashi Sakamoto 	int err;
4883149ac48STakashi Sakamoto 
4893149ac48STakashi Sakamoto 	id = uval->value.enumerated.item[0];
490f77ac91eSTakashi Sakamoto 	if (id >= ARRAY_SIZE(special_dig_in_iface_labels))
491f77ac91eSTakashi Sakamoto 		return -EINVAL;
4923149ac48STakashi Sakamoto 
4933149ac48STakashi Sakamoto 	/* decode user value */
4943149ac48STakashi Sakamoto 	dig_in_fmt = (id >> 1) & 0x01;
4953149ac48STakashi Sakamoto 	dig_in_iface = id & 0x01;
4963149ac48STakashi Sakamoto 
497f77ac91eSTakashi Sakamoto 	mutex_lock(&bebob->mutex);
498f77ac91eSTakashi Sakamoto 
4993149ac48STakashi Sakamoto 	err = avc_maudio_set_special_clk(bebob,
5003149ac48STakashi Sakamoto 					 params->clk_src,
5013149ac48STakashi Sakamoto 					 dig_in_fmt,
5023149ac48STakashi Sakamoto 					 params->dig_out_fmt,
5033149ac48STakashi Sakamoto 					 params->clk_lock);
5045a0438f4STakashi Sakamoto 	if (err < 0)
5053149ac48STakashi Sakamoto 		goto end;
5063149ac48STakashi Sakamoto 
5075a0438f4STakashi Sakamoto 	/* For ADAT, optical interface is only available. */
508f77ac91eSTakashi Sakamoto 	if (params->dig_in_fmt > 0) {
509f77ac91eSTakashi Sakamoto 		err = 1;
5105a0438f4STakashi Sakamoto 		goto end;
511f77ac91eSTakashi Sakamoto 	}
5125a0438f4STakashi Sakamoto 
5135a0438f4STakashi Sakamoto 	/* For S/PDIF, optical/coaxial interfaces are selectable. */
5143149ac48STakashi Sakamoto 	err = avc_audio_set_selector(bebob->unit, 0x00, 0x04, dig_in_iface);
5153149ac48STakashi Sakamoto 	if (err < 0)
5163149ac48STakashi Sakamoto 		dev_err(&bebob->unit->device,
5173149ac48STakashi Sakamoto 			"fail to set digital input interface: %d\n", err);
518f77ac91eSTakashi Sakamoto 	err = 1;
5193149ac48STakashi Sakamoto end:
5203149ac48STakashi Sakamoto 	special_stream_formation_set(bebob);
5213149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
5223149ac48STakashi Sakamoto 	return err;
5233149ac48STakashi Sakamoto }
524905e46acSBhumika Goyal static const struct snd_kcontrol_new special_dig_in_iface_ctl = {
5253149ac48STakashi Sakamoto 	.name	= "Digital Input Interface",
5263149ac48STakashi Sakamoto 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
5273149ac48STakashi Sakamoto 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
5283149ac48STakashi Sakamoto 	.info	= special_dig_in_iface_ctl_info,
5293149ac48STakashi Sakamoto 	.get	= special_dig_in_iface_ctl_get,
5303149ac48STakashi Sakamoto 	.put	= special_dig_in_iface_ctl_set
5313149ac48STakashi Sakamoto };
5323149ac48STakashi Sakamoto 
5335a0438f4STakashi Sakamoto /* Digital output interface control for special firmware */
534e7ced413STakashi Iwai static const char *const special_dig_out_iface_labels[] = {
5355a0438f4STakashi Sakamoto 	"S/PDIF Optical and Coaxial", "ADAT Optical"
5365a0438f4STakashi Sakamoto };
special_dig_out_iface_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * einf)5373149ac48STakashi Sakamoto static int special_dig_out_iface_ctl_info(struct snd_kcontrol *kctl,
5383149ac48STakashi Sakamoto 					  struct snd_ctl_elem_info *einf)
5393149ac48STakashi Sakamoto {
54041be5164STakashi Iwai 	return snd_ctl_enum_info(einf, 1,
54141be5164STakashi Iwai 				 ARRAY_SIZE(special_dig_out_iface_labels),
54241be5164STakashi Iwai 				 special_dig_out_iface_labels);
5433149ac48STakashi Sakamoto }
special_dig_out_iface_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)5443149ac48STakashi Sakamoto static int special_dig_out_iface_ctl_get(struct snd_kcontrol *kctl,
5453149ac48STakashi Sakamoto 					 struct snd_ctl_elem_value *uval)
5463149ac48STakashi Sakamoto {
5473149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
5483149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
5493149ac48STakashi Sakamoto 	mutex_lock(&bebob->mutex);
5503149ac48STakashi Sakamoto 	uval->value.enumerated.item[0] = params->dig_out_fmt;
5513149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
5523149ac48STakashi Sakamoto 	return 0;
5533149ac48STakashi Sakamoto }
special_dig_out_iface_ctl_set(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uval)5543149ac48STakashi Sakamoto static int special_dig_out_iface_ctl_set(struct snd_kcontrol *kctl,
5553149ac48STakashi Sakamoto 					 struct snd_ctl_elem_value *uval)
5563149ac48STakashi Sakamoto {
5573149ac48STakashi Sakamoto 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
5583149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
5593149ac48STakashi Sakamoto 	unsigned int id;
5603149ac48STakashi Sakamoto 	int err;
5613149ac48STakashi Sakamoto 
5623149ac48STakashi Sakamoto 	id = uval->value.enumerated.item[0];
563f77ac91eSTakashi Sakamoto 	if (id >= ARRAY_SIZE(special_dig_out_iface_labels))
564f77ac91eSTakashi Sakamoto 		return -EINVAL;
565f77ac91eSTakashi Sakamoto 
566f77ac91eSTakashi Sakamoto 	mutex_lock(&bebob->mutex);
5673149ac48STakashi Sakamoto 
5683149ac48STakashi Sakamoto 	err = avc_maudio_set_special_clk(bebob,
5693149ac48STakashi Sakamoto 					 params->clk_src,
5703149ac48STakashi Sakamoto 					 params->dig_in_fmt,
5713149ac48STakashi Sakamoto 					 id, params->clk_lock);
572f77ac91eSTakashi Sakamoto 	if (err >= 0) {
5733149ac48STakashi Sakamoto 		special_stream_formation_set(bebob);
574f77ac91eSTakashi Sakamoto 		err = 1;
575f77ac91eSTakashi Sakamoto 	}
5763149ac48STakashi Sakamoto 
5773149ac48STakashi Sakamoto 	mutex_unlock(&bebob->mutex);
5783149ac48STakashi Sakamoto 	return err;
5793149ac48STakashi Sakamoto }
580905e46acSBhumika Goyal static const struct snd_kcontrol_new special_dig_out_iface_ctl = {
5813149ac48STakashi Sakamoto 	.name	= "Digital Output Interface",
5823149ac48STakashi Sakamoto 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
5833149ac48STakashi Sakamoto 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
5843149ac48STakashi Sakamoto 	.info	= special_dig_out_iface_ctl_info,
5853149ac48STakashi Sakamoto 	.get	= special_dig_out_iface_ctl_get,
5863149ac48STakashi Sakamoto 	.put	= special_dig_out_iface_ctl_set
5873149ac48STakashi Sakamoto };
5883149ac48STakashi Sakamoto 
add_special_controls(struct snd_bebob * bebob)5893149ac48STakashi Sakamoto static int add_special_controls(struct snd_bebob *bebob)
5903149ac48STakashi Sakamoto {
5913149ac48STakashi Sakamoto 	struct snd_kcontrol *kctl;
5923149ac48STakashi Sakamoto 	struct special_params *params = bebob->maudio_special_quirk;
5933149ac48STakashi Sakamoto 	int err;
5943149ac48STakashi Sakamoto 
5953149ac48STakashi Sakamoto 	kctl = snd_ctl_new1(&special_clk_ctl, bebob);
5963149ac48STakashi Sakamoto 	err = snd_ctl_add(bebob->card, kctl);
5973149ac48STakashi Sakamoto 	if (err < 0)
5983149ac48STakashi Sakamoto 		goto end;
5993149ac48STakashi Sakamoto 
6003149ac48STakashi Sakamoto 	kctl = snd_ctl_new1(&special_sync_ctl, bebob);
6013149ac48STakashi Sakamoto 	err = snd_ctl_add(bebob->card, kctl);
6023149ac48STakashi Sakamoto 	if (err < 0)
6033149ac48STakashi Sakamoto 		goto end;
6043149ac48STakashi Sakamoto 	params->ctl_id_sync = &kctl->id;
6053149ac48STakashi Sakamoto 
6063149ac48STakashi Sakamoto 	kctl = snd_ctl_new1(&special_dig_in_iface_ctl, bebob);
6073149ac48STakashi Sakamoto 	err = snd_ctl_add(bebob->card, kctl);
6083149ac48STakashi Sakamoto 	if (err < 0)
6093149ac48STakashi Sakamoto 		goto end;
6103149ac48STakashi Sakamoto 
6113149ac48STakashi Sakamoto 	kctl = snd_ctl_new1(&special_dig_out_iface_ctl, bebob);
6123149ac48STakashi Sakamoto 	err = snd_ctl_add(bebob->card, kctl);
6133149ac48STakashi Sakamoto end:
6143149ac48STakashi Sakamoto 	return err;
6153149ac48STakashi Sakamoto }
6163149ac48STakashi Sakamoto 
6173149ac48STakashi Sakamoto /* Hardware metering for special firmware */
618e7ced413STakashi Iwai static const char *const special_meter_labels[] = {
6193149ac48STakashi Sakamoto 	ANA_IN, ANA_IN, ANA_IN, ANA_IN,
6203149ac48STakashi Sakamoto 	SPDIF_IN,
6213149ac48STakashi Sakamoto 	ADAT_IN, ADAT_IN, ADAT_IN, ADAT_IN,
6223149ac48STakashi Sakamoto 	ANA_OUT, ANA_OUT,
6233149ac48STakashi Sakamoto 	SPDIF_OUT,
6243149ac48STakashi Sakamoto 	ADAT_OUT, ADAT_OUT, ADAT_OUT, ADAT_OUT,
6253149ac48STakashi Sakamoto 	HP_OUT, HP_OUT,
6263149ac48STakashi Sakamoto 	AUX_OUT
6273149ac48STakashi Sakamoto };
6283149ac48STakashi Sakamoto static int
special_meter_get(struct snd_bebob * bebob,u32 * target,unsigned int size)6293149ac48STakashi Sakamoto special_meter_get(struct snd_bebob *bebob, u32 *target, unsigned int size)
6303149ac48STakashi Sakamoto {
631fef586d5STakashi Sakamoto 	__be16 *buf;
6323149ac48STakashi Sakamoto 	unsigned int i, c, channels;
6333149ac48STakashi Sakamoto 	int err;
6343149ac48STakashi Sakamoto 
6353149ac48STakashi Sakamoto 	channels = ARRAY_SIZE(special_meter_labels) * 2;
6363149ac48STakashi Sakamoto 	if (size < channels * sizeof(u32))
6373149ac48STakashi Sakamoto 		return -EINVAL;
6383149ac48STakashi Sakamoto 
6393149ac48STakashi Sakamoto 	/* omit last 4 bytes because it's clock info. */
6403149ac48STakashi Sakamoto 	buf = kmalloc(METER_SIZE_SPECIAL - 4, GFP_KERNEL);
6413149ac48STakashi Sakamoto 	if (buf == NULL)
6423149ac48STakashi Sakamoto 		return -ENOMEM;
6433149ac48STakashi Sakamoto 
6443149ac48STakashi Sakamoto 	err = get_meter(bebob, (void *)buf, METER_SIZE_SPECIAL - 4);
6453149ac48STakashi Sakamoto 	if (err < 0)
6463149ac48STakashi Sakamoto 		goto end;
6473149ac48STakashi Sakamoto 
6483149ac48STakashi Sakamoto 	/* Its format is u16 and some channels are unknown. */
6493149ac48STakashi Sakamoto 	i = 0;
6503149ac48STakashi Sakamoto 	for (c = 2; c < channels + 2; c++)
6513149ac48STakashi Sakamoto 		target[i++] = be16_to_cpu(buf[c]) << 16;
6523149ac48STakashi Sakamoto end:
6533149ac48STakashi Sakamoto 	kfree(buf);
6543149ac48STakashi Sakamoto 	return err;
6553149ac48STakashi Sakamoto }
6563149ac48STakashi Sakamoto 
6579076c22dSTakashi Sakamoto /* last 4 bytes are omitted because it's clock info. */
658e7ced413STakashi Iwai static const char *const fw410_meter_labels[] = {
6599076c22dSTakashi Sakamoto 	ANA_IN, DIG_IN,
6609076c22dSTakashi Sakamoto 	ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT, DIG_OUT,
6619076c22dSTakashi Sakamoto 	HP_OUT
6629076c22dSTakashi Sakamoto };
663e7ced413STakashi Iwai static const char *const audiophile_meter_labels[] = {
6649076c22dSTakashi Sakamoto 	ANA_IN, DIG_IN,
6659076c22dSTakashi Sakamoto 	ANA_OUT, ANA_OUT, DIG_OUT,
6669076c22dSTakashi Sakamoto 	HP_OUT, AUX_OUT,
6679076c22dSTakashi Sakamoto };
668e7ced413STakashi Iwai static const char *const solo_meter_labels[] = {
6699076c22dSTakashi Sakamoto 	ANA_IN, DIG_IN,
6709076c22dSTakashi Sakamoto 	STRM_IN, STRM_IN,
6719076c22dSTakashi Sakamoto 	ANA_OUT, DIG_OUT
6729076c22dSTakashi Sakamoto };
6739076c22dSTakashi Sakamoto 
6749076c22dSTakashi Sakamoto /* no clock info */
675e7ced413STakashi Iwai static const char *const ozonic_meter_labels[] = {
6769076c22dSTakashi Sakamoto 	ANA_IN, ANA_IN,
6779076c22dSTakashi Sakamoto 	STRM_IN, STRM_IN,
6789076c22dSTakashi Sakamoto 	ANA_OUT, ANA_OUT
6799076c22dSTakashi Sakamoto };
6809076c22dSTakashi Sakamoto /* TODO: need testers. these positions are based on authour's assumption */
681e7ced413STakashi Iwai static const char *const nrv10_meter_labels[] = {
6829076c22dSTakashi Sakamoto 	ANA_IN, ANA_IN, ANA_IN, ANA_IN,
6839076c22dSTakashi Sakamoto 	DIG_IN,
6849076c22dSTakashi Sakamoto 	ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
6859076c22dSTakashi Sakamoto 	DIG_IN
6869076c22dSTakashi Sakamoto };
6879076c22dSTakashi Sakamoto static int
normal_meter_get(struct snd_bebob * bebob,u32 * buf,unsigned int size)6889076c22dSTakashi Sakamoto normal_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
6899076c22dSTakashi Sakamoto {
6906b9866c8SJulia Lawall 	const struct snd_bebob_meter_spec *spec = bebob->spec->meter;
6919076c22dSTakashi Sakamoto 	unsigned int c, channels;
6929076c22dSTakashi Sakamoto 	int err;
6939076c22dSTakashi Sakamoto 
6949076c22dSTakashi Sakamoto 	channels = spec->num * 2;
6959076c22dSTakashi Sakamoto 	if (size < channels * sizeof(u32))
6969076c22dSTakashi Sakamoto 		return -EINVAL;
6979076c22dSTakashi Sakamoto 
6989076c22dSTakashi Sakamoto 	err = get_meter(bebob, (void *)buf, size);
6999076c22dSTakashi Sakamoto 	if (err < 0)
7009076c22dSTakashi Sakamoto 		goto end;
7019076c22dSTakashi Sakamoto 
7029076c22dSTakashi Sakamoto 	for (c = 0; c < channels; c++)
7039076c22dSTakashi Sakamoto 		be32_to_cpus(&buf[c]);
7049076c22dSTakashi Sakamoto 
7059076c22dSTakashi Sakamoto 	/* swap stream channels because inverted */
7069076c22dSTakashi Sakamoto 	if (spec->labels == solo_meter_labels) {
7079076c22dSTakashi Sakamoto 		swap(buf[4], buf[6]);
7089076c22dSTakashi Sakamoto 		swap(buf[5], buf[7]);
7099076c22dSTakashi Sakamoto 	}
7109076c22dSTakashi Sakamoto end:
7119076c22dSTakashi Sakamoto 	return err;
7129076c22dSTakashi Sakamoto }
7139076c22dSTakashi Sakamoto 
7143149ac48STakashi Sakamoto /* for special customized devices */
7156b9866c8SJulia Lawall static const struct snd_bebob_rate_spec special_rate_spec = {
7163149ac48STakashi Sakamoto 	.get	= &special_get_rate,
7173149ac48STakashi Sakamoto 	.set	= &special_set_rate,
7183149ac48STakashi Sakamoto };
7196b9866c8SJulia Lawall static const struct snd_bebob_clock_spec special_clk_spec = {
720ba517713STakashi Sakamoto 	.num	= ARRAY_SIZE(special_clk_types),
721ba517713STakashi Sakamoto 	.types	= special_clk_types,
7223149ac48STakashi Sakamoto 	.get	= &special_clk_get,
7233149ac48STakashi Sakamoto };
7246b9866c8SJulia Lawall static const struct snd_bebob_meter_spec special_meter_spec = {
7253149ac48STakashi Sakamoto 	.num	= ARRAY_SIZE(special_meter_labels),
7263149ac48STakashi Sakamoto 	.labels	= special_meter_labels,
7273149ac48STakashi Sakamoto 	.get	= &special_meter_get
7283149ac48STakashi Sakamoto };
7296b9866c8SJulia Lawall const struct snd_bebob_spec maudio_special_spec = {
7303149ac48STakashi Sakamoto 	.clock	= &special_clk_spec,
7313149ac48STakashi Sakamoto 	.rate	= &special_rate_spec,
7323149ac48STakashi Sakamoto 	.meter	= &special_meter_spec
7333149ac48STakashi Sakamoto };
7343149ac48STakashi Sakamoto 
7359076c22dSTakashi Sakamoto /* Firewire 410 specification */
7366b9866c8SJulia Lawall static const struct snd_bebob_rate_spec usual_rate_spec = {
7379076c22dSTakashi Sakamoto 	.get	= &snd_bebob_stream_get_rate,
7389076c22dSTakashi Sakamoto 	.set	= &snd_bebob_stream_set_rate,
7399076c22dSTakashi Sakamoto };
7406b9866c8SJulia Lawall static const struct snd_bebob_meter_spec fw410_meter_spec = {
7419076c22dSTakashi Sakamoto 	.num	= ARRAY_SIZE(fw410_meter_labels),
7429076c22dSTakashi Sakamoto 	.labels	= fw410_meter_labels,
7439076c22dSTakashi Sakamoto 	.get	= &normal_meter_get
7449076c22dSTakashi Sakamoto };
7456b9866c8SJulia Lawall const struct snd_bebob_spec maudio_fw410_spec = {
7469076c22dSTakashi Sakamoto 	.clock	= NULL,
7479076c22dSTakashi Sakamoto 	.rate	= &usual_rate_spec,
7489076c22dSTakashi Sakamoto 	.meter	= &fw410_meter_spec
7499076c22dSTakashi Sakamoto };
7509076c22dSTakashi Sakamoto 
7519076c22dSTakashi Sakamoto /* Firewire Audiophile specification */
7526b9866c8SJulia Lawall static const struct snd_bebob_meter_spec audiophile_meter_spec = {
7539076c22dSTakashi Sakamoto 	.num	= ARRAY_SIZE(audiophile_meter_labels),
7549076c22dSTakashi Sakamoto 	.labels	= audiophile_meter_labels,
7559076c22dSTakashi Sakamoto 	.get	= &normal_meter_get
7569076c22dSTakashi Sakamoto };
7576b9866c8SJulia Lawall const struct snd_bebob_spec maudio_audiophile_spec = {
7589076c22dSTakashi Sakamoto 	.clock	= NULL,
7599076c22dSTakashi Sakamoto 	.rate	= &usual_rate_spec,
7609076c22dSTakashi Sakamoto 	.meter	= &audiophile_meter_spec
7619076c22dSTakashi Sakamoto };
7629076c22dSTakashi Sakamoto 
7639076c22dSTakashi Sakamoto /* Firewire Solo specification */
7646b9866c8SJulia Lawall static const struct snd_bebob_meter_spec solo_meter_spec = {
7659076c22dSTakashi Sakamoto 	.num	= ARRAY_SIZE(solo_meter_labels),
7669076c22dSTakashi Sakamoto 	.labels	= solo_meter_labels,
7679076c22dSTakashi Sakamoto 	.get	= &normal_meter_get
7689076c22dSTakashi Sakamoto };
7696b9866c8SJulia Lawall const struct snd_bebob_spec maudio_solo_spec = {
7709076c22dSTakashi Sakamoto 	.clock	= NULL,
7719076c22dSTakashi Sakamoto 	.rate	= &usual_rate_spec,
7729076c22dSTakashi Sakamoto 	.meter	= &solo_meter_spec
7739076c22dSTakashi Sakamoto };
7749076c22dSTakashi Sakamoto 
7759076c22dSTakashi Sakamoto /* Ozonic specification */
7766b9866c8SJulia Lawall static const struct snd_bebob_meter_spec ozonic_meter_spec = {
7779076c22dSTakashi Sakamoto 	.num	= ARRAY_SIZE(ozonic_meter_labels),
7789076c22dSTakashi Sakamoto 	.labels	= ozonic_meter_labels,
7799076c22dSTakashi Sakamoto 	.get	= &normal_meter_get
7809076c22dSTakashi Sakamoto };
7816b9866c8SJulia Lawall const struct snd_bebob_spec maudio_ozonic_spec = {
7829076c22dSTakashi Sakamoto 	.clock	= NULL,
7839076c22dSTakashi Sakamoto 	.rate	= &usual_rate_spec,
7849076c22dSTakashi Sakamoto 	.meter	= &ozonic_meter_spec
7859076c22dSTakashi Sakamoto };
7869076c22dSTakashi Sakamoto 
7879076c22dSTakashi Sakamoto /* NRV10 specification */
7886b9866c8SJulia Lawall static const struct snd_bebob_meter_spec nrv10_meter_spec = {
7899076c22dSTakashi Sakamoto 	.num	= ARRAY_SIZE(nrv10_meter_labels),
7909076c22dSTakashi Sakamoto 	.labels	= nrv10_meter_labels,
7919076c22dSTakashi Sakamoto 	.get	= &normal_meter_get
7929076c22dSTakashi Sakamoto };
7936b9866c8SJulia Lawall const struct snd_bebob_spec maudio_nrv10_spec = {
7949076c22dSTakashi Sakamoto 	.clock	= NULL,
7959076c22dSTakashi Sakamoto 	.rate	= &usual_rate_spec,
7969076c22dSTakashi Sakamoto 	.meter	= &nrv10_meter_spec
7979076c22dSTakashi Sakamoto };
798