2 * bebob_maudio.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
10 #include <sound/control.h>
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.
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.
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).
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
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.
38 /* Offset from information register */
39 #define INFO_OFFSET_SW_DATE 0x20
41 /* Bootloader Protocol Version 1 */
42 #define MAUDIO_BOOTLOADER_CUE1 0x00000001
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)
48 #define MAUDIO_BOOTLOADER_CUE2 0x01110000
50 #define MAUDIO_BOOTLOADER_CUE3 0x00000000
52 #define MAUDIO_SPECIFIC_ADDRESS 0xffc700000000ULL
54 #define METER_OFFSET 0x00600000
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
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"
77 #define UNKNOWN_METER "Unknown"
79 struct special_params {
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;
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.
92 * If we make any transactions to load firmware, the operation may failed.
94 int snd_bebob_maudio_load_firmware(struct fw_unit *unit)
96 struct fw_device *device = fw_parent_device(unit);
101 /* check date of software used to build */
102 err = snd_bebob_read_block(unit, INFO_OFFSET_SW_DATE,
107 * firmware version 5058 or later has date later than "20070401", but
108 * 'date' is not null-terminated.
110 if (date < 0x3230303730343031LL) {
111 dev_err(&unit->device,
112 "Use firmware version 5058 or later\n");
116 cues = kmalloc_array(3, sizeof(*cues), GFP_KERNEL);
120 cues[0] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE1);
121 cues[1] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE2);
122 cues[2] = cpu_to_le32(MAUDIO_BOOTLOADER_CUE3);
124 rcode = fw_run_transaction(device->card, TCODE_WRITE_BLOCK_REQUEST,
125 device->node_id, device->generation,
126 device->max_speed, BEBOB_ADDR_REG_REQ,
127 cues, 3 * sizeof(*cues));
129 if (rcode != RCODE_COMPLETE) {
130 dev_err(&unit->device,
131 "Failed to send a cue to load firmware\n");
139 get_meter(struct snd_bebob *bebob, void *buf, unsigned int size)
141 return snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
142 MAUDIO_SPECIFIC_ADDRESS + METER_OFFSET,
147 check_clk_sync(struct snd_bebob *bebob, unsigned int size, bool *sync)
152 buf = kmalloc(size, GFP_KERNEL);
156 err = get_meter(bebob, buf, size);
160 /* if synced, this value is the same as SFC of FDF in CIP header */
161 *sync = (buf[size - 2] != 0xff);
168 * dig_fmt: 0x00:S/PDIF, 0x01:ADAT
169 * clk_lock: 0x00:unlock, 0x01:lock
172 avc_maudio_set_special_clk(struct snd_bebob *bebob, unsigned int clk_src,
173 unsigned int dig_in_fmt, unsigned int dig_out_fmt,
174 unsigned int clk_lock)
176 struct special_params *params = bebob->maudio_special_quirk;
180 if (amdtp_stream_running(&bebob->rx_stream) ||
181 amdtp_stream_running(&bebob->tx_stream))
184 buf = kmalloc(12, GFP_KERNEL);
188 buf[0] = 0x00; /* CONTROL */
189 buf[1] = 0xff; /* UNIT */
190 buf[2] = 0x00; /* vendor dependent */
191 buf[3] = 0x04; /* company ID high */
192 buf[4] = 0x00; /* company ID middle */
193 buf[5] = 0x04; /* company ID low */
194 buf[6] = 0xff & clk_src; /* clock source */
195 buf[7] = 0xff & dig_in_fmt; /* input digital format */
196 buf[8] = 0xff & dig_out_fmt; /* output digital format */
197 buf[9] = 0xff & clk_lock; /* lock these settings */
198 buf[10] = 0x00; /* padding */
199 buf[11] = 0x00; /* padding */
201 err = fcp_avc_transaction(bebob->unit, buf, 12, buf, 12,
202 BIT(1) | BIT(2) | BIT(3) | BIT(4) |
203 BIT(5) | BIT(6) | BIT(7) | BIT(8) |
205 if ((err > 0) && (err < 10))
207 else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
209 else if (buf[0] == 0x0a) /* REJECTED */
214 params->clk_src = buf[6];
215 params->dig_in_fmt = buf[7];
216 params->dig_out_fmt = buf[8];
217 params->clk_lock = buf[9];
219 if (params->ctl_id_sync)
220 snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
221 params->ctl_id_sync);
229 special_stream_formation_set(struct snd_bebob *bebob)
231 static const unsigned int ch_table[2][2][3] = {
232 /* AMDTP_OUT_STREAM */
233 { { 6, 6, 4 }, /* SPDIF */
234 { 12, 8, 4 } }, /* ADAT */
235 /* AMDTP_IN_STREAM */
236 { { 10, 10, 2 }, /* SPDIF */
237 { 16, 12, 2 } } /* ADAT */
239 struct special_params *params = bebob->maudio_special_quirk;
242 max = SND_BEBOB_STRM_FMT_ENTRIES - 1;
246 for (i = 0; i < max; i++) {
247 bebob->tx_stream_formations[i + 1].pcm =
248 ch_table[AMDTP_IN_STREAM][params->dig_in_fmt][i / 2];
249 bebob->tx_stream_formations[i + 1].midi = 1;
251 bebob->rx_stream_formations[i + 1].pcm =
252 ch_table[AMDTP_OUT_STREAM][params->dig_out_fmt][i / 2];
253 bebob->rx_stream_formations[i + 1].midi = 1;
257 static int add_special_controls(struct snd_bebob *bebob);
259 snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814)
261 struct special_params *params;
264 params = kzalloc(sizeof(struct special_params), GFP_KERNEL);
268 mutex_lock(&bebob->mutex);
270 bebob->maudio_special_quirk = (void *)params;
271 params->is1814 = is1814;
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);
278 dev_err(&bebob->unit->device,
279 "fail to initialize clock params: %d\n", err);
283 err = add_special_controls(bebob);
287 special_stream_formation_set(bebob);
289 if (params->is1814) {
290 bebob->midi_input_ports = 1;
291 bebob->midi_output_ports = 1;
293 bebob->midi_input_ports = 2;
294 bebob->midi_output_ports = 2;
299 bebob->maudio_special_quirk = NULL;
301 mutex_unlock(&bebob->mutex);
305 /* Input plug shows actual rate. Output plug is needless for this purpose. */
306 static int special_get_rate(struct snd_bebob *bebob, unsigned int *rate)
312 err = avc_general_get_sig_fmt(bebob->unit, rate,
313 AVC_GENERAL_PLUG_DIR_IN, 0);
314 } while (err == -EAGAIN && ++trials < 3);
318 static int special_set_rate(struct snd_bebob *bebob, unsigned int rate)
320 struct special_params *params = bebob->maudio_special_quirk;
323 err = avc_general_set_sig_fmt(bebob->unit, rate,
324 AVC_GENERAL_PLUG_DIR_OUT, 0);
329 * Just after changing sampling rate for output, a followed command
330 * for input is easy to fail. This is a workaround fot this issue.
334 err = avc_general_set_sig_fmt(bebob->unit, rate,
335 AVC_GENERAL_PLUG_DIR_IN, 0);
339 if (params->ctl_id_sync)
340 snd_ctl_notify(bebob->card, SNDRV_CTL_EVENT_MASK_VALUE,
341 params->ctl_id_sync);
346 /* Clock source control for special firmware */
347 static enum snd_bebob_clock_type special_clk_types[] = {
348 SND_BEBOB_CLOCK_TYPE_INTERNAL, /* With digital mute */
349 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* SPDIF/ADAT */
350 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* Word Clock */
351 SND_BEBOB_CLOCK_TYPE_INTERNAL,
353 static int special_clk_get(struct snd_bebob *bebob, unsigned int *id)
355 struct special_params *params = bebob->maudio_special_quirk;
356 *id = params->clk_src;
359 static int special_clk_ctl_info(struct snd_kcontrol *kctl,
360 struct snd_ctl_elem_info *einf)
362 static const char *const special_clk_labels[] = {
363 "Internal with Digital Mute",
368 return snd_ctl_enum_info(einf, 1, ARRAY_SIZE(special_clk_types),
371 static int special_clk_ctl_get(struct snd_kcontrol *kctl,
372 struct snd_ctl_elem_value *uval)
374 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
375 struct special_params *params = bebob->maudio_special_quirk;
376 uval->value.enumerated.item[0] = params->clk_src;
379 static int special_clk_ctl_put(struct snd_kcontrol *kctl,
380 struct snd_ctl_elem_value *uval)
382 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
383 struct special_params *params = bebob->maudio_special_quirk;
386 id = uval->value.enumerated.item[0];
387 if (id >= ARRAY_SIZE(special_clk_types))
390 mutex_lock(&bebob->mutex);
392 err = avc_maudio_set_special_clk(bebob, id,
396 mutex_unlock(&bebob->mutex);
403 static struct snd_kcontrol_new special_clk_ctl = {
404 .name = "Clock Source",
405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
406 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
407 .info = special_clk_ctl_info,
408 .get = special_clk_ctl_get,
409 .put = special_clk_ctl_put
412 /* Clock synchronization control for special firmware */
413 static int special_sync_ctl_info(struct snd_kcontrol *kctl,
414 struct snd_ctl_elem_info *einf)
416 einf->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
418 einf->value.integer.min = 0;
419 einf->value.integer.max = 1;
423 static int special_sync_ctl_get(struct snd_kcontrol *kctl,
424 struct snd_ctl_elem_value *uval)
426 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
430 err = check_clk_sync(bebob, METER_SIZE_SPECIAL, &synced);
432 uval->value.integer.value[0] = synced;
436 static struct snd_kcontrol_new special_sync_ctl = {
437 .name = "Sync Status",
438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
439 .access = SNDRV_CTL_ELEM_ACCESS_READ,
440 .info = special_sync_ctl_info,
441 .get = special_sync_ctl_get,
444 /* Digital input interface control for special firmware */
445 static const char *const special_dig_in_iface_labels[] = {
446 "S/PDIF Optical", "S/PDIF Coaxial", "ADAT Optical"
448 static int special_dig_in_iface_ctl_info(struct snd_kcontrol *kctl,
449 struct snd_ctl_elem_info *einf)
451 return snd_ctl_enum_info(einf, 1,
452 ARRAY_SIZE(special_dig_in_iface_labels),
453 special_dig_in_iface_labels);
455 static int special_dig_in_iface_ctl_get(struct snd_kcontrol *kctl,
456 struct snd_ctl_elem_value *uval)
458 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
459 struct special_params *params = bebob->maudio_special_quirk;
460 unsigned int dig_in_iface;
463 mutex_lock(&bebob->mutex);
465 err = avc_audio_get_selector(bebob->unit, 0x00, 0x04,
468 dev_err(&bebob->unit->device,
469 "fail to get digital input interface: %d\n", err);
473 /* encoded id for user value */
474 val = (params->dig_in_fmt << 1) | (dig_in_iface & 0x01);
476 /* for ADAT Optical */
480 uval->value.enumerated.item[0] = val;
482 mutex_unlock(&bebob->mutex);
485 static int special_dig_in_iface_ctl_set(struct snd_kcontrol *kctl,
486 struct snd_ctl_elem_value *uval)
488 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
489 struct special_params *params = bebob->maudio_special_quirk;
490 unsigned int id, dig_in_fmt, dig_in_iface;
493 id = uval->value.enumerated.item[0];
494 if (id >= ARRAY_SIZE(special_dig_in_iface_labels))
497 /* decode user value */
498 dig_in_fmt = (id >> 1) & 0x01;
499 dig_in_iface = id & 0x01;
501 mutex_lock(&bebob->mutex);
503 err = avc_maudio_set_special_clk(bebob,
511 /* For ADAT, optical interface is only available. */
512 if (params->dig_in_fmt > 0) {
517 /* For S/PDIF, optical/coaxial interfaces are selectable. */
518 err = avc_audio_set_selector(bebob->unit, 0x00, 0x04, dig_in_iface);
520 dev_err(&bebob->unit->device,
521 "fail to set digital input interface: %d\n", err);
524 special_stream_formation_set(bebob);
525 mutex_unlock(&bebob->mutex);
528 static struct snd_kcontrol_new special_dig_in_iface_ctl = {
529 .name = "Digital Input Interface",
530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
531 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
532 .info = special_dig_in_iface_ctl_info,
533 .get = special_dig_in_iface_ctl_get,
534 .put = special_dig_in_iface_ctl_set
537 /* Digital output interface control for special firmware */
538 static const char *const special_dig_out_iface_labels[] = {
539 "S/PDIF Optical and Coaxial", "ADAT Optical"
541 static int special_dig_out_iface_ctl_info(struct snd_kcontrol *kctl,
542 struct snd_ctl_elem_info *einf)
544 return snd_ctl_enum_info(einf, 1,
545 ARRAY_SIZE(special_dig_out_iface_labels),
546 special_dig_out_iface_labels);
548 static int special_dig_out_iface_ctl_get(struct snd_kcontrol *kctl,
549 struct snd_ctl_elem_value *uval)
551 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
552 struct special_params *params = bebob->maudio_special_quirk;
553 mutex_lock(&bebob->mutex);
554 uval->value.enumerated.item[0] = params->dig_out_fmt;
555 mutex_unlock(&bebob->mutex);
558 static int special_dig_out_iface_ctl_set(struct snd_kcontrol *kctl,
559 struct snd_ctl_elem_value *uval)
561 struct snd_bebob *bebob = snd_kcontrol_chip(kctl);
562 struct special_params *params = bebob->maudio_special_quirk;
566 id = uval->value.enumerated.item[0];
567 if (id >= ARRAY_SIZE(special_dig_out_iface_labels))
570 mutex_lock(&bebob->mutex);
572 err = avc_maudio_set_special_clk(bebob,
575 id, params->clk_lock);
577 special_stream_formation_set(bebob);
581 mutex_unlock(&bebob->mutex);
584 static struct snd_kcontrol_new special_dig_out_iface_ctl = {
585 .name = "Digital Output Interface",
586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
587 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
588 .info = special_dig_out_iface_ctl_info,
589 .get = special_dig_out_iface_ctl_get,
590 .put = special_dig_out_iface_ctl_set
593 static int add_special_controls(struct snd_bebob *bebob)
595 struct snd_kcontrol *kctl;
596 struct special_params *params = bebob->maudio_special_quirk;
599 kctl = snd_ctl_new1(&special_clk_ctl, bebob);
600 err = snd_ctl_add(bebob->card, kctl);
604 kctl = snd_ctl_new1(&special_sync_ctl, bebob);
605 err = snd_ctl_add(bebob->card, kctl);
608 params->ctl_id_sync = &kctl->id;
610 kctl = snd_ctl_new1(&special_dig_in_iface_ctl, bebob);
611 err = snd_ctl_add(bebob->card, kctl);
615 kctl = snd_ctl_new1(&special_dig_out_iface_ctl, bebob);
616 err = snd_ctl_add(bebob->card, kctl);
621 /* Hardware metering for special firmware */
622 static const char *const special_meter_labels[] = {
623 ANA_IN, ANA_IN, ANA_IN, ANA_IN,
625 ADAT_IN, ADAT_IN, ADAT_IN, ADAT_IN,
628 ADAT_OUT, ADAT_OUT, ADAT_OUT, ADAT_OUT,
633 special_meter_get(struct snd_bebob *bebob, u32 *target, unsigned int size)
636 unsigned int i, c, channels;
639 channels = ARRAY_SIZE(special_meter_labels) * 2;
640 if (size < channels * sizeof(u32))
643 /* omit last 4 bytes because it's clock info. */
644 buf = kmalloc(METER_SIZE_SPECIAL - 4, GFP_KERNEL);
648 err = get_meter(bebob, (void *)buf, METER_SIZE_SPECIAL - 4);
652 /* Its format is u16 and some channels are unknown. */
654 for (c = 2; c < channels + 2; c++)
655 target[i++] = be16_to_cpu(buf[c]) << 16;
661 /* last 4 bytes are omitted because it's clock info. */
662 static const char *const fw410_meter_labels[] = {
664 ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT, DIG_OUT,
667 static const char *const audiophile_meter_labels[] = {
669 ANA_OUT, ANA_OUT, DIG_OUT,
672 static const char *const solo_meter_labels[] = {
679 static const char *const ozonic_meter_labels[] = {
684 /* TODO: need testers. these positions are based on authour's assumption */
685 static const char *const nrv10_meter_labels[] = {
686 ANA_IN, ANA_IN, ANA_IN, ANA_IN,
688 ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
692 normal_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
694 const struct snd_bebob_meter_spec *spec = bebob->spec->meter;
695 unsigned int c, channels;
698 channels = spec->num * 2;
699 if (size < channels * sizeof(u32))
702 err = get_meter(bebob, (void *)buf, size);
706 for (c = 0; c < channels; c++)
707 be32_to_cpus(&buf[c]);
709 /* swap stream channels because inverted */
710 if (spec->labels == solo_meter_labels) {
711 swap(buf[4], buf[6]);
712 swap(buf[5], buf[7]);
718 /* for special customized devices */
719 static const struct snd_bebob_rate_spec special_rate_spec = {
720 .get = &special_get_rate,
721 .set = &special_set_rate,
723 static const struct snd_bebob_clock_spec special_clk_spec = {
724 .num = ARRAY_SIZE(special_clk_types),
725 .types = special_clk_types,
726 .get = &special_clk_get,
728 static const struct snd_bebob_meter_spec special_meter_spec = {
729 .num = ARRAY_SIZE(special_meter_labels),
730 .labels = special_meter_labels,
731 .get = &special_meter_get
733 const struct snd_bebob_spec maudio_special_spec = {
734 .clock = &special_clk_spec,
735 .rate = &special_rate_spec,
736 .meter = &special_meter_spec
739 /* Firewire 410 specification */
740 static const struct snd_bebob_rate_spec usual_rate_spec = {
741 .get = &snd_bebob_stream_get_rate,
742 .set = &snd_bebob_stream_set_rate,
744 static const struct snd_bebob_meter_spec fw410_meter_spec = {
745 .num = ARRAY_SIZE(fw410_meter_labels),
746 .labels = fw410_meter_labels,
747 .get = &normal_meter_get
749 const struct snd_bebob_spec maudio_fw410_spec = {
751 .rate = &usual_rate_spec,
752 .meter = &fw410_meter_spec
755 /* Firewire Audiophile specification */
756 static const struct snd_bebob_meter_spec audiophile_meter_spec = {
757 .num = ARRAY_SIZE(audiophile_meter_labels),
758 .labels = audiophile_meter_labels,
759 .get = &normal_meter_get
761 const struct snd_bebob_spec maudio_audiophile_spec = {
763 .rate = &usual_rate_spec,
764 .meter = &audiophile_meter_spec
767 /* Firewire Solo specification */
768 static const struct snd_bebob_meter_spec solo_meter_spec = {
769 .num = ARRAY_SIZE(solo_meter_labels),
770 .labels = solo_meter_labels,
771 .get = &normal_meter_get
773 const struct snd_bebob_spec maudio_solo_spec = {
775 .rate = &usual_rate_spec,
776 .meter = &solo_meter_spec
779 /* Ozonic specification */
780 static const struct snd_bebob_meter_spec ozonic_meter_spec = {
781 .num = ARRAY_SIZE(ozonic_meter_labels),
782 .labels = ozonic_meter_labels,
783 .get = &normal_meter_get
785 const struct snd_bebob_spec maudio_ozonic_spec = {
787 .rate = &usual_rate_spec,
788 .meter = &ozonic_meter_spec
791 /* NRV10 specification */
792 static const struct snd_bebob_meter_spec nrv10_meter_spec = {
793 .num = ARRAY_SIZE(nrv10_meter_labels),
794 .labels = nrv10_meter_labels,
795 .get = &normal_meter_get
797 const struct snd_bebob_spec maudio_nrv10_spec = {
799 .rate = &usual_rate_spec,
800 .meter = &nrv10_meter_spec