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