xref: /linux/sound/firewire/bebob/bebob_maudio.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * bebob_maudio.c - a part of driver for BeBoB based devices
3  *
4  * Copyright (c) 2013-2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "./bebob.h"
10 #include <sound/control.h>
11 
12 /*
13  * Just powering on, Firewire 410/Audiophile/1814 and ProjectMix I/O wait to
14  * download firmware blob. To enable these devices, drivers should upload
15  * firmware blob and send a command to initialize configuration to factory
16  * settings when completing uploading. Then these devices generate bus reset
17  * and are recognized as new devices with the firmware.
18  *
19  * But with firmware version 5058 or later, the firmware is stored to flash
20  * memory in the device and drivers can tell bootloader to load the firmware
21  * by sending a cue. This cue must be sent one time.
22  *
23  * For streaming, both of output and input streams are needed for Firewire 410
24  * and Ozonic. The single stream is OK for the other devices even if the clock
25  * source is not SYT-Match (I note no devices use SYT-Match).
26  *
27  * Without streaming, the devices except for Firewire Audiophile can mix any
28  * input and output. For this reason, Audiophile cannot be used as standalone
29  * mixer.
30  *
31  * Firewire 1814 and ProjectMix I/O uses special firmware. It will be freezed
32  * when receiving any commands which the firmware can't understand. These
33  * devices utilize completely different system to control. It is some
34  * write-transaction directly into a certain address. All of addresses for mixer
35  * functionality is between 0xffc700700000 to 0xffc70070009c.
36  */
37 
38 /* Offset from information register */
39 #define INFO_OFFSET_SW_DATE	0x20
40 
41 /* Bootloader Protocol Version 1 */
42 #define MAUDIO_BOOTLOADER_CUE1	0x00000001
43 /*
44  * Initializing configuration to factory settings (= 0x1101), (swapped in line),
45  * Command code is zero (= 0x00),
46  * the number of operands is zero (= 0x00)(at least significant byte)
47  */
48 #define MAUDIO_BOOTLOADER_CUE2	0x01110000
49 /* padding */
50 #define MAUDIO_BOOTLOADER_CUE3	0x00000000
51 
52 #define MAUDIO_SPECIFIC_ADDRESS	0xffc700000000ULL
53 
54 #define METER_OFFSET		0x00600000
55 
56 /* some device has sync info after metering data */
57 #define METER_SIZE_SPECIAL	84	/* with sync info */
58 #define METER_SIZE_FW410	76	/* with sync info */
59 #define METER_SIZE_AUDIOPHILE	60	/* with sync info */
60 #define METER_SIZE_SOLO		52	/* with sync info */
61 #define METER_SIZE_OZONIC	48
62 #define METER_SIZE_NRV10	80
63 
64 /* labels for metering */
65 #define ANA_IN		"Analog In"
66 #define ANA_OUT		"Analog Out"
67 #define DIG_IN		"Digital In"
68 #define SPDIF_IN	"S/PDIF In"
69 #define ADAT_IN		"ADAT In"
70 #define DIG_OUT		"Digital Out"
71 #define SPDIF_OUT	"S/PDIF Out"
72 #define ADAT_OUT	"ADAT Out"
73 #define STRM_IN		"Stream In"
74 #define AUX_OUT		"Aux Out"
75 #define HP_OUT		"HP Out"
76 /* for NRV */
77 #define UNKNOWN_METER	"Unknown"
78 
79 struct special_params {
80 	bool is1814;
81 	unsigned int clk_src;
82 	unsigned int dig_in_fmt;
83 	unsigned int dig_out_fmt;
84 	unsigned int clk_lock;
85 	struct snd_ctl_elem_id *ctl_id_sync;
86 };
87 
88 /*
89  * For some M-Audio devices, this module just send cue to load firmware. After
90  * loading, the device generates bus reset and newly detected.
91  *
92  * If we make any transactions to load firmware, the operation may failed.
93  */
94 int snd_bebob_maudio_load_firmware(struct fw_unit *unit)
95 {
96 	struct fw_device *device = fw_parent_device(unit);
97 	int err, rcode;
98 	u64 date;
99 	__be32 cues[3] = {
100 		MAUDIO_BOOTLOADER_CUE1,
101 		MAUDIO_BOOTLOADER_CUE2,
102 		MAUDIO_BOOTLOADER_CUE3
103 	};
104 
105 	/* check date of software used to build */
106 	err = snd_bebob_read_block(unit, INFO_OFFSET_SW_DATE,
107 				   &date, sizeof(u64));
108 	if (err < 0)
109 		goto end;
110 	/*
111 	 * firmware version 5058 or later has date later than "20070401", but
112 	 * 'date' is not null-terminated.
113 	 */
114 	if (date < 0x3230303730343031LL) {
115 		dev_err(&unit->device,
116 			"Use firmware version 5058 or later\n");
117 		err = -ENOSYS;
118 		goto end;
119 	}
120 
121 	rcode = fw_run_transaction(device->card, TCODE_WRITE_BLOCK_REQUEST,
122 				   device->node_id, device->generation,
123 				   device->max_speed, BEBOB_ADDR_REG_REQ,
124 				   cues, sizeof(cues));
125 	if (rcode != RCODE_COMPLETE) {
126 		dev_err(&unit->device,
127 			"Failed to send a cue to load firmware\n");
128 		err = -EIO;
129 	}
130 end:
131 	return err;
132 }
133 
134 static inline int
135 get_meter(struct snd_bebob *bebob, void *buf, unsigned int size)
136 {
137 	return snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
138 				  MAUDIO_SPECIFIC_ADDRESS + METER_OFFSET,
139 				  buf, size, 0);
140 }
141 
142 static int
143 check_clk_sync(struct snd_bebob *bebob, unsigned int size, bool *sync)
144 {
145 	int err;
146 	u8 *buf;
147 
148 	buf = kmalloc(size, GFP_KERNEL);
149 	if (buf == NULL)
150 		return -ENOMEM;
151 
152 	err = get_meter(bebob, buf, size);
153 	if (err < 0)
154 		goto end;
155 
156 	/* if synced, this value is the same as SFC of FDF in CIP header */
157 	*sync = (buf[size - 2] != 0xff);
158 end:
159 	kfree(buf);
160 	return err;
161 }
162 
163 /*
164  * dig_fmt: 0x00:S/PDIF, 0x01:ADAT
165  * clk_lock: 0x00:unlock, 0x01:lock
166  */
167 static int
168 avc_maudio_set_special_clk(struct snd_bebob *bebob, unsigned int clk_src,
169 			   unsigned int dig_in_fmt, unsigned int dig_out_fmt,
170 			   unsigned int clk_lock)
171 {
172 	struct special_params *params = bebob->maudio_special_quirk;
173 	int err;
174 	u8 *buf;
175 
176 	if (amdtp_stream_running(&bebob->rx_stream) ||
177 	    amdtp_stream_running(&bebob->tx_stream))
178 		return -EBUSY;
179 
180 	buf = kmalloc(12, GFP_KERNEL);
181 	if (buf == NULL)
182 		return -ENOMEM;
183 
184 	buf[0]  = 0x00;		/* CONTROL */
185 	buf[1]  = 0xff;		/* UNIT */
186 	buf[2]  = 0x00;		/* vendor dependent */
187 	buf[3]  = 0x04;		/* company ID high */
188 	buf[4]  = 0x00;		/* company ID middle */
189 	buf[5]  = 0x04;		/* company ID low */
190 	buf[6]  = 0xff & clk_src;	/* clock source */
191 	buf[7]  = 0xff & dig_in_fmt;	/* input digital format */
192 	buf[8]  = 0xff & dig_out_fmt;	/* output digital format */
193 	buf[9]  = 0xff & clk_lock;	/* lock these settings */
194 	buf[10] = 0x00;		/* padding  */
195 	buf[11] = 0x00;		/* padding */
196 
197 	err = fcp_avc_transaction(bebob->unit, buf, 12, buf, 12,
198 				  BIT(1) | BIT(2) | BIT(3) | BIT(4) |
199 				  BIT(5) | BIT(6) | BIT(7) | BIT(8) |
200 				  BIT(9));
201 	if ((err > 0) && (err < 10))
202 		err = -EIO;
203 	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
204 		err = -ENOSYS;
205 	else if (buf[0] == 0x0a) /* REJECTED */
206 		err = -EINVAL;
207 	if (err < 0)
208 		goto end;
209 
210 	params->clk_src		= buf[6];
211 	params->dig_in_fmt	= buf[7];
212 	params->dig_out_fmt	= buf[8];
213 	params->clk_lock	= buf[9];
214 
215 	if (params->ctl_id_sync)
216 		snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
217 			       params->ctl_id_sync);
218 
219 	err = 0;
220 end:
221 	kfree(buf);
222 	return err;
223 }
224 static void
225 special_stream_formation_set(struct snd_bebob *bebob)
226 {
227 	static const unsigned int ch_table[2][2][3] = {
228 		/* AMDTP_OUT_STREAM */
229 		{ {  6,  6,  4 },	/* SPDIF */
230 		  { 12,  8,  4 } },	/* ADAT */
231 		/* AMDTP_IN_STREAM */
232 		{ { 10, 10,  2 },	/* SPDIF */
233 		  { 16, 12,  2 } }	/* ADAT */
234 	};
235 	struct special_params *params = bebob->maudio_special_quirk;
236 	unsigned int i, max;
237 
238 	max = SND_BEBOB_STRM_FMT_ENTRIES - 1;
239 	if (!params->is1814)
240 		max -= 2;
241 
242 	for (i = 0; i < max; i++) {
243 		bebob->tx_stream_formations[i + 1].pcm =
244 			ch_table[AMDTP_IN_STREAM][params->dig_in_fmt][i / 2];
245 		bebob->tx_stream_formations[i + 1].midi = 1;
246 
247 		bebob->rx_stream_formations[i + 1].pcm =
248 			ch_table[AMDTP_OUT_STREAM][params->dig_out_fmt][i / 2];
249 		bebob->rx_stream_formations[i + 1].midi = 1;
250 	}
251 }
252 
253 static int add_special_controls(struct snd_bebob *bebob);
254 int
255 snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814)
256 {
257 	struct special_params *params;
258 	int err;
259 
260 	params = kzalloc(sizeof(struct special_params), GFP_KERNEL);
261 	if (params == NULL)
262 		return -ENOMEM;
263 
264 	mutex_lock(&bebob->mutex);
265 
266 	bebob->maudio_special_quirk = (void *)params;
267 	params->is1814 = is1814;
268 
269 	/* initialize these parameters because driver is not allowed to ask */
270 	bebob->rx_stream.context = ERR_PTR(-1);
271 	bebob->tx_stream.context = ERR_PTR(-1);
272 	err = avc_maudio_set_special_clk(bebob, 0x03, 0x00, 0x00, 0x00);
273 	if (err < 0) {
274 		dev_err(&bebob->unit->device,
275 			"fail to initialize clock params: %d\n", err);
276 		goto end;
277 	}
278 
279 	err = add_special_controls(bebob);
280 	if (err < 0)
281 		goto end;
282 
283 	special_stream_formation_set(bebob);
284 
285 	if (params->is1814) {
286 		bebob->midi_input_ports = 1;
287 		bebob->midi_output_ports = 1;
288 	} else {
289 		bebob->midi_input_ports = 2;
290 		bebob->midi_output_ports = 2;
291 	}
292 end:
293 	if (err < 0) {
294 		kfree(params);
295 		bebob->maudio_special_quirk = NULL;
296 	}
297 	mutex_unlock(&bebob->mutex);
298 	return err;
299 }
300 
301 /* Input plug shows actual rate. Output plug is needless for this purpose. */
302 static int special_get_rate(struct snd_bebob *bebob, unsigned int *rate)
303 {
304 	int err, trials;
305 
306 	trials = 0;
307 	do {
308 		err = avc_general_get_sig_fmt(bebob->unit, rate,
309 					      AVC_GENERAL_PLUG_DIR_IN, 0);
310 	} while (err == -EAGAIN && ++trials < 3);
311 
312 	return err;
313 }
314 static int special_set_rate(struct snd_bebob *bebob, unsigned int rate)
315 {
316 	struct special_params *params = bebob->maudio_special_quirk;
317 	int err;
318 
319 	err = avc_general_set_sig_fmt(bebob->unit, rate,
320 				      AVC_GENERAL_PLUG_DIR_OUT, 0);
321 	if (err < 0)
322 		goto end;
323 
324 	/*
325 	 * Just after changing sampling rate for output, a followed command
326 	 * for input is easy to fail. This is a workaround fot this issue.
327 	 */
328 	msleep(100);
329 
330 	err = avc_general_set_sig_fmt(bebob->unit, rate,
331 				      AVC_GENERAL_PLUG_DIR_IN, 0);
332 	if (err < 0)
333 		goto end;
334 
335 	if (params->ctl_id_sync)
336 		snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
337 			       params->ctl_id_sync);
338 end:
339 	return err;
340 }
341 
342 /* Clock source control for special firmware */
343 static char *const special_clk_labels[] = {
344 	SND_BEBOB_CLOCK_INTERNAL " with Digital Mute", "Digital",
345 	"Word Clock", SND_BEBOB_CLOCK_INTERNAL};
346 static int special_clk_get(struct snd_bebob *bebob, unsigned int *id)
347 {
348 	struct special_params *params = bebob->maudio_special_quirk;
349 	*id = params->clk_src;
350 	return 0;
351 }
352 static int special_clk_ctl_info(struct snd_kcontrol *kctl,
353 				struct snd_ctl_elem_info *einf)
354 {
355 	einf->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
356 	einf->count = 1;
357 	einf->value.enumerated.items = ARRAY_SIZE(special_clk_labels);
358 
359 	if (einf->value.enumerated.item >= einf->value.enumerated.items)
360 		einf->value.enumerated.item = einf->value.enumerated.items - 1;
361 
362 	strcpy(einf->value.enumerated.name,
363 	       special_clk_labels[einf->value.enumerated.item]);
364 
365 	return 0;
366 }
367 static int special_clk_ctl_get(struct snd_kcontrol *kctl,
368 			       struct snd_ctl_elem_value *uval)
369 {
370 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
371 	struct special_params *params = bebob->maudio_special_quirk;
372 	uval->value.enumerated.item[0] = params->clk_src;
373 	return 0;
374 }
375 static int special_clk_ctl_put(struct snd_kcontrol *kctl,
376 			       struct snd_ctl_elem_value *uval)
377 {
378 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
379 	struct special_params *params = bebob->maudio_special_quirk;
380 	int err, id;
381 
382 	id = uval->value.enumerated.item[0];
383 	if (id >= ARRAY_SIZE(special_clk_labels))
384 		return -EINVAL;
385 
386 	mutex_lock(&bebob->mutex);
387 
388 	err = avc_maudio_set_special_clk(bebob, id,
389 					 params->dig_in_fmt,
390 					 params->dig_out_fmt,
391 					 params->clk_lock);
392 	mutex_unlock(&bebob->mutex);
393 
394 	if (err >= 0)
395 		err = 1;
396 
397 	return err;
398 }
399 static struct snd_kcontrol_new special_clk_ctl = {
400 	.name	= "Clock Source",
401 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
402 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
403 	.info	= special_clk_ctl_info,
404 	.get	= special_clk_ctl_get,
405 	.put	= special_clk_ctl_put
406 };
407 
408 /* Clock synchronization control for special firmware */
409 static int special_sync_ctl_info(struct snd_kcontrol *kctl,
410 				 struct snd_ctl_elem_info *einf)
411 {
412 	einf->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
413 	einf->count = 1;
414 	einf->value.integer.min = 0;
415 	einf->value.integer.max = 1;
416 
417 	return 0;
418 }
419 static int special_sync_ctl_get(struct snd_kcontrol *kctl,
420 				struct snd_ctl_elem_value *uval)
421 {
422 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
423 	int err;
424 	bool synced = 0;
425 
426 	err = check_clk_sync(bebob, METER_SIZE_SPECIAL, &synced);
427 	if (err >= 0)
428 		uval->value.integer.value[0] = synced;
429 
430 	return 0;
431 }
432 static struct snd_kcontrol_new special_sync_ctl = {
433 	.name	= "Sync Status",
434 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
435 	.access	= SNDRV_CTL_ELEM_ACCESS_READ,
436 	.info	= special_sync_ctl_info,
437 	.get	= special_sync_ctl_get,
438 };
439 
440 /* Digital input interface control for special firmware */
441 static char *const special_dig_in_iface_labels[] = {
442 	"S/PDIF Optical", "S/PDIF Coaxial", "ADAT Optical"
443 };
444 static int special_dig_in_iface_ctl_info(struct snd_kcontrol *kctl,
445 					 struct snd_ctl_elem_info *einf)
446 {
447 	einf->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
448 	einf->count = 1;
449 	einf->value.enumerated.items = ARRAY_SIZE(special_dig_in_iface_labels);
450 
451 	if (einf->value.enumerated.item >= einf->value.enumerated.items)
452 		einf->value.enumerated.item = einf->value.enumerated.items - 1;
453 
454 	strcpy(einf->value.enumerated.name,
455 	       special_dig_in_iface_labels[einf->value.enumerated.item]);
456 
457 	return 0;
458 }
459 static int special_dig_in_iface_ctl_get(struct snd_kcontrol *kctl,
460 					struct snd_ctl_elem_value *uval)
461 {
462 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
463 	struct special_params *params = bebob->maudio_special_quirk;
464 	unsigned int dig_in_iface;
465 	int err, val;
466 
467 	mutex_lock(&bebob->mutex);
468 
469 	err = avc_audio_get_selector(bebob->unit, 0x00, 0x04,
470 				     &dig_in_iface);
471 	if (err < 0) {
472 		dev_err(&bebob->unit->device,
473 			"fail to get digital input interface: %d\n", err);
474 		goto end;
475 	}
476 
477 	/* encoded id for user value */
478 	val = (params->dig_in_fmt << 1) | (dig_in_iface & 0x01);
479 
480 	/* for ADAT Optical */
481 	if (val > 2)
482 		val = 2;
483 
484 	uval->value.enumerated.item[0] = val;
485 end:
486 	mutex_unlock(&bebob->mutex);
487 	return err;
488 }
489 static int special_dig_in_iface_ctl_set(struct snd_kcontrol *kctl,
490 					struct snd_ctl_elem_value *uval)
491 {
492 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
493 	struct special_params *params = bebob->maudio_special_quirk;
494 	unsigned int id, dig_in_fmt, dig_in_iface;
495 	int err;
496 
497 	id = uval->value.enumerated.item[0];
498 	if (id >= ARRAY_SIZE(special_dig_in_iface_labels))
499 		return -EINVAL;
500 
501 	/* decode user value */
502 	dig_in_fmt = (id >> 1) & 0x01;
503 	dig_in_iface = id & 0x01;
504 
505 	mutex_lock(&bebob->mutex);
506 
507 	err = avc_maudio_set_special_clk(bebob,
508 					 params->clk_src,
509 					 dig_in_fmt,
510 					 params->dig_out_fmt,
511 					 params->clk_lock);
512 	if (err < 0)
513 		goto end;
514 
515 	/* For ADAT, optical interface is only available. */
516 	if (params->dig_in_fmt > 0) {
517 		err = 1;
518 		goto end;
519 	}
520 
521 	/* For S/PDIF, optical/coaxial interfaces are selectable. */
522 	err = avc_audio_set_selector(bebob->unit, 0x00, 0x04, dig_in_iface);
523 	if (err < 0)
524 		dev_err(&bebob->unit->device,
525 			"fail to set digital input interface: %d\n", err);
526 	err = 1;
527 end:
528 	special_stream_formation_set(bebob);
529 	mutex_unlock(&bebob->mutex);
530 	return err;
531 }
532 static struct snd_kcontrol_new special_dig_in_iface_ctl = {
533 	.name	= "Digital Input Interface",
534 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
535 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
536 	.info	= special_dig_in_iface_ctl_info,
537 	.get	= special_dig_in_iface_ctl_get,
538 	.put	= special_dig_in_iface_ctl_set
539 };
540 
541 /* Digital output interface control for special firmware */
542 static char *const special_dig_out_iface_labels[] = {
543 	"S/PDIF Optical and Coaxial", "ADAT Optical"
544 };
545 static int special_dig_out_iface_ctl_info(struct snd_kcontrol *kctl,
546 					  struct snd_ctl_elem_info *einf)
547 {
548 	einf->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
549 	einf->count = 1;
550 	einf->value.enumerated.items = ARRAY_SIZE(special_dig_out_iface_labels);
551 
552 	if (einf->value.enumerated.item >= einf->value.enumerated.items)
553 		einf->value.enumerated.item = einf->value.enumerated.items - 1;
554 
555 	strcpy(einf->value.enumerated.name,
556 	       special_dig_out_iface_labels[einf->value.enumerated.item]);
557 
558 	return 0;
559 }
560 static int special_dig_out_iface_ctl_get(struct snd_kcontrol *kctl,
561 					 struct snd_ctl_elem_value *uval)
562 {
563 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
564 	struct special_params *params = bebob->maudio_special_quirk;
565 	mutex_lock(&bebob->mutex);
566 	uval->value.enumerated.item[0] = params->dig_out_fmt;
567 	mutex_unlock(&bebob->mutex);
568 	return 0;
569 }
570 static int special_dig_out_iface_ctl_set(struct snd_kcontrol *kctl,
571 					 struct snd_ctl_elem_value *uval)
572 {
573 	struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
574 	struct special_params *params = bebob->maudio_special_quirk;
575 	unsigned int id;
576 	int err;
577 
578 	id = uval->value.enumerated.item[0];
579 	if (id >= ARRAY_SIZE(special_dig_out_iface_labels))
580 		return -EINVAL;
581 
582 	mutex_lock(&bebob->mutex);
583 
584 	err = avc_maudio_set_special_clk(bebob,
585 					 params->clk_src,
586 					 params->dig_in_fmt,
587 					 id, params->clk_lock);
588 	if (err >= 0) {
589 		special_stream_formation_set(bebob);
590 		err = 1;
591 	}
592 
593 	mutex_unlock(&bebob->mutex);
594 	return err;
595 }
596 static struct snd_kcontrol_new special_dig_out_iface_ctl = {
597 	.name	= "Digital Output Interface",
598 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
599 	.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE,
600 	.info	= special_dig_out_iface_ctl_info,
601 	.get	= special_dig_out_iface_ctl_get,
602 	.put	= special_dig_out_iface_ctl_set
603 };
604 
605 static int add_special_controls(struct snd_bebob *bebob)
606 {
607 	struct snd_kcontrol *kctl;
608 	struct special_params *params = bebob->maudio_special_quirk;
609 	int err;
610 
611 	kctl = snd_ctl_new1(&special_clk_ctl, bebob);
612 	err = snd_ctl_add(bebob->card, kctl);
613 	if (err < 0)
614 		goto end;
615 
616 	kctl = snd_ctl_new1(&special_sync_ctl, bebob);
617 	err = snd_ctl_add(bebob->card, kctl);
618 	if (err < 0)
619 		goto end;
620 	params->ctl_id_sync = &kctl->id;
621 
622 	kctl = snd_ctl_new1(&special_dig_in_iface_ctl, bebob);
623 	err = snd_ctl_add(bebob->card, kctl);
624 	if (err < 0)
625 		goto end;
626 
627 	kctl = snd_ctl_new1(&special_dig_out_iface_ctl, bebob);
628 	err = snd_ctl_add(bebob->card, kctl);
629 end:
630 	return err;
631 }
632 
633 /* Hardware metering for special firmware */
634 static char *const special_meter_labels[] = {
635 	ANA_IN, ANA_IN, ANA_IN, ANA_IN,
636 	SPDIF_IN,
637 	ADAT_IN, ADAT_IN, ADAT_IN, ADAT_IN,
638 	ANA_OUT, ANA_OUT,
639 	SPDIF_OUT,
640 	ADAT_OUT, ADAT_OUT, ADAT_OUT, ADAT_OUT,
641 	HP_OUT, HP_OUT,
642 	AUX_OUT
643 };
644 static int
645 special_meter_get(struct snd_bebob *bebob, u32 *target, unsigned int size)
646 {
647 	u16 *buf;
648 	unsigned int i, c, channels;
649 	int err;
650 
651 	channels = ARRAY_SIZE(special_meter_labels) * 2;
652 	if (size < channels * sizeof(u32))
653 		return -EINVAL;
654 
655 	/* omit last 4 bytes because it's clock info. */
656 	buf = kmalloc(METER_SIZE_SPECIAL - 4, GFP_KERNEL);
657 	if (buf == NULL)
658 		return -ENOMEM;
659 
660 	err = get_meter(bebob, (void *)buf, METER_SIZE_SPECIAL - 4);
661 	if (err < 0)
662 		goto end;
663 
664 	/* Its format is u16 and some channels are unknown. */
665 	i = 0;
666 	for (c = 2; c < channels + 2; c++)
667 		target[i++] = be16_to_cpu(buf[c]) << 16;
668 end:
669 	kfree(buf);
670 	return err;
671 }
672 
673 /* last 4 bytes are omitted because it's clock info. */
674 static char *const fw410_meter_labels[] = {
675 	ANA_IN, DIG_IN,
676 	ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT, DIG_OUT,
677 	HP_OUT
678 };
679 static char *const audiophile_meter_labels[] = {
680 	ANA_IN, DIG_IN,
681 	ANA_OUT, ANA_OUT, DIG_OUT,
682 	HP_OUT, AUX_OUT,
683 };
684 static char *const solo_meter_labels[] = {
685 	ANA_IN, DIG_IN,
686 	STRM_IN, STRM_IN,
687 	ANA_OUT, DIG_OUT
688 };
689 
690 /* no clock info */
691 static char *const ozonic_meter_labels[] = {
692 	ANA_IN, ANA_IN,
693 	STRM_IN, STRM_IN,
694 	ANA_OUT, ANA_OUT
695 };
696 /* TODO: need testers. these positions are based on authour's assumption */
697 static char *const nrv10_meter_labels[] = {
698 	ANA_IN, ANA_IN, ANA_IN, ANA_IN,
699 	DIG_IN,
700 	ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
701 	DIG_IN
702 };
703 static int
704 normal_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
705 {
706 	struct snd_bebob_meter_spec *spec = bebob->spec->meter;
707 	unsigned int c, channels;
708 	int err;
709 
710 	channels = spec->num * 2;
711 	if (size < channels * sizeof(u32))
712 		return -EINVAL;
713 
714 	err = get_meter(bebob, (void *)buf, size);
715 	if (err < 0)
716 		goto end;
717 
718 	for (c = 0; c < channels; c++)
719 		be32_to_cpus(&buf[c]);
720 
721 	/* swap stream channels because inverted */
722 	if (spec->labels == solo_meter_labels) {
723 		swap(buf[4], buf[6]);
724 		swap(buf[5], buf[7]);
725 	}
726 end:
727 	return err;
728 }
729 
730 /* for special customized devices */
731 static struct snd_bebob_rate_spec special_rate_spec = {
732 	.get	= &special_get_rate,
733 	.set	= &special_set_rate,
734 };
735 static struct snd_bebob_clock_spec special_clk_spec = {
736 	.num	= ARRAY_SIZE(special_clk_labels),
737 	.labels	= special_clk_labels,
738 	.get	= &special_clk_get,
739 };
740 static struct snd_bebob_meter_spec special_meter_spec = {
741 	.num	= ARRAY_SIZE(special_meter_labels),
742 	.labels	= special_meter_labels,
743 	.get	= &special_meter_get
744 };
745 struct snd_bebob_spec maudio_special_spec = {
746 	.clock	= &special_clk_spec,
747 	.rate	= &special_rate_spec,
748 	.meter	= &special_meter_spec
749 };
750 
751 /* Firewire 410 specification */
752 static struct snd_bebob_rate_spec usual_rate_spec = {
753 	.get	= &snd_bebob_stream_get_rate,
754 	.set	= &snd_bebob_stream_set_rate,
755 };
756 static struct snd_bebob_meter_spec fw410_meter_spec = {
757 	.num	= ARRAY_SIZE(fw410_meter_labels),
758 	.labels	= fw410_meter_labels,
759 	.get	= &normal_meter_get
760 };
761 struct snd_bebob_spec maudio_fw410_spec = {
762 	.clock	= NULL,
763 	.rate	= &usual_rate_spec,
764 	.meter	= &fw410_meter_spec
765 };
766 
767 /* Firewire Audiophile specification */
768 static struct snd_bebob_meter_spec audiophile_meter_spec = {
769 	.num	= ARRAY_SIZE(audiophile_meter_labels),
770 	.labels	= audiophile_meter_labels,
771 	.get	= &normal_meter_get
772 };
773 struct snd_bebob_spec maudio_audiophile_spec = {
774 	.clock	= NULL,
775 	.rate	= &usual_rate_spec,
776 	.meter	= &audiophile_meter_spec
777 };
778 
779 /* Firewire Solo specification */
780 static struct snd_bebob_meter_spec solo_meter_spec = {
781 	.num	= ARRAY_SIZE(solo_meter_labels),
782 	.labels	= solo_meter_labels,
783 	.get	= &normal_meter_get
784 };
785 struct snd_bebob_spec maudio_solo_spec = {
786 	.clock	= NULL,
787 	.rate	= &usual_rate_spec,
788 	.meter	= &solo_meter_spec
789 };
790 
791 /* Ozonic specification */
792 static struct snd_bebob_meter_spec ozonic_meter_spec = {
793 	.num	= ARRAY_SIZE(ozonic_meter_labels),
794 	.labels	= ozonic_meter_labels,
795 	.get	= &normal_meter_get
796 };
797 struct snd_bebob_spec maudio_ozonic_spec = {
798 	.clock	= NULL,
799 	.rate	= &usual_rate_spec,
800 	.meter	= &ozonic_meter_spec
801 };
802 
803 /* NRV10 specification */
804 static struct snd_bebob_meter_spec nrv10_meter_spec = {
805 	.num	= ARRAY_SIZE(nrv10_meter_labels),
806 	.labels	= nrv10_meter_labels,
807 	.get	= &normal_meter_get
808 };
809 struct snd_bebob_spec maudio_nrv10_spec = {
810 	.clock	= NULL,
811 	.rate	= &usual_rate_spec,
812 	.meter	= &nrv10_meter_spec
813 };
814