Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * oxfw-spkr.c - a part of driver for OXFW970/971 based devices
0004  *
0005  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
0006  */
0007 
0008 #include "oxfw.h"
0009 
0010 struct fw_spkr {
0011     bool mute;
0012     s16 volume[6];
0013     s16 volume_min;
0014     s16 volume_max;
0015 
0016     unsigned int mixer_channels;
0017     u8 mute_fb_id;
0018     u8 volume_fb_id;
0019 };
0020 
0021 enum control_action { CTL_READ, CTL_WRITE };
0022 enum control_attribute {
0023     CTL_MIN     = 0x02,
0024     CTL_MAX     = 0x03,
0025     CTL_CURRENT = 0x10,
0026 };
0027 
0028 static int avc_audio_feature_mute(struct fw_unit *unit, u8 fb_id, bool *value,
0029                   enum control_action action)
0030 {
0031     u8 *buf;
0032     u8 response_ok;
0033     int err;
0034 
0035     buf = kmalloc(11, GFP_KERNEL);
0036     if (!buf)
0037         return -ENOMEM;
0038 
0039     if (action == CTL_READ) {
0040         buf[0] = 0x01;      /* AV/C, STATUS */
0041         response_ok = 0x0c; /*       STABLE */
0042     } else {
0043         buf[0] = 0x00;      /* AV/C, CONTROL */
0044         response_ok = 0x09; /*       ACCEPTED */
0045     }
0046     buf[1] = 0x08;          /* audio unit 0 */
0047     buf[2] = 0xb8;          /* FUNCTION BLOCK */
0048     buf[3] = 0x81;          /* function block type: feature */
0049     buf[4] = fb_id;         /* function block ID */
0050     buf[5] = 0x10;          /* control attribute: current */
0051     buf[6] = 0x02;          /* selector length */
0052     buf[7] = 0x00;          /* audio channel number */
0053     buf[8] = 0x01;          /* control selector: mute */
0054     buf[9] = 0x01;          /* control data length */
0055     if (action == CTL_READ)
0056         buf[10] = 0xff;
0057     else
0058         buf[10] = *value ? 0x70 : 0x60;
0059 
0060     err = fcp_avc_transaction(unit, buf, 11, buf, 11, 0x3fe);
0061     if (err < 0)
0062         goto error;
0063     if (err < 11) {
0064         dev_err(&unit->device, "short FCP response\n");
0065         err = -EIO;
0066         goto error;
0067     }
0068     if (buf[0] != response_ok) {
0069         dev_err(&unit->device, "mute command failed\n");
0070         err = -EIO;
0071         goto error;
0072     }
0073     if (action == CTL_READ)
0074         *value = buf[10] == 0x70;
0075 
0076     err = 0;
0077 
0078 error:
0079     kfree(buf);
0080 
0081     return err;
0082 }
0083 
0084 static int avc_audio_feature_volume(struct fw_unit *unit, u8 fb_id, s16 *value,
0085                     unsigned int channel,
0086                     enum control_attribute attribute,
0087                     enum control_action action)
0088 {
0089     u8 *buf;
0090     u8 response_ok;
0091     int err;
0092 
0093     buf = kmalloc(12, GFP_KERNEL);
0094     if (!buf)
0095         return -ENOMEM;
0096 
0097     if (action == CTL_READ) {
0098         buf[0] = 0x01;      /* AV/C, STATUS */
0099         response_ok = 0x0c; /*       STABLE */
0100     } else {
0101         buf[0] = 0x00;      /* AV/C, CONTROL */
0102         response_ok = 0x09; /*       ACCEPTED */
0103     }
0104     buf[1] = 0x08;          /* audio unit 0 */
0105     buf[2] = 0xb8;          /* FUNCTION BLOCK */
0106     buf[3] = 0x81;          /* function block type: feature */
0107     buf[4] = fb_id;         /* function block ID */
0108     buf[5] = attribute;     /* control attribute */
0109     buf[6] = 0x02;          /* selector length */
0110     buf[7] = channel;       /* audio channel number */
0111     buf[8] = 0x02;          /* control selector: volume */
0112     buf[9] = 0x02;          /* control data length */
0113     if (action == CTL_READ) {
0114         buf[10] = 0xff;
0115         buf[11] = 0xff;
0116     } else {
0117         buf[10] = *value >> 8;
0118         buf[11] = *value;
0119     }
0120 
0121     err = fcp_avc_transaction(unit, buf, 12, buf, 12, 0x3fe);
0122     if (err < 0)
0123         goto error;
0124     if (err < 12) {
0125         dev_err(&unit->device, "short FCP response\n");
0126         err = -EIO;
0127         goto error;
0128     }
0129     if (buf[0] != response_ok) {
0130         dev_err(&unit->device, "volume command failed\n");
0131         err = -EIO;
0132         goto error;
0133     }
0134     if (action == CTL_READ)
0135         *value = (buf[10] << 8) | buf[11];
0136 
0137     err = 0;
0138 
0139 error:
0140     kfree(buf);
0141 
0142     return err;
0143 }
0144 
0145 static int spkr_mute_get(struct snd_kcontrol *control,
0146              struct snd_ctl_elem_value *value)
0147 {
0148     struct snd_oxfw *oxfw = control->private_data;
0149     struct fw_spkr *spkr = oxfw->spec;
0150 
0151     value->value.integer.value[0] = !spkr->mute;
0152 
0153     return 0;
0154 }
0155 
0156 static int spkr_mute_put(struct snd_kcontrol *control,
0157              struct snd_ctl_elem_value *value)
0158 {
0159     struct snd_oxfw *oxfw = control->private_data;
0160     struct fw_spkr *spkr = oxfw->spec;
0161     bool mute;
0162     int err;
0163 
0164     mute = !value->value.integer.value[0];
0165 
0166     if (mute == spkr->mute)
0167         return 0;
0168 
0169     err = avc_audio_feature_mute(oxfw->unit, spkr->mute_fb_id, &mute,
0170                      CTL_WRITE);
0171     if (err < 0)
0172         return err;
0173     spkr->mute = mute;
0174 
0175     return 1;
0176 }
0177 
0178 static int spkr_volume_info(struct snd_kcontrol *control,
0179                 struct snd_ctl_elem_info *info)
0180 {
0181     struct snd_oxfw *oxfw = control->private_data;
0182     struct fw_spkr *spkr = oxfw->spec;
0183 
0184     info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0185     info->count = spkr->mixer_channels;
0186     info->value.integer.min = spkr->volume_min;
0187     info->value.integer.max = spkr->volume_max;
0188 
0189     return 0;
0190 }
0191 
0192 static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
0193 
0194 static int spkr_volume_get(struct snd_kcontrol *control,
0195                struct snd_ctl_elem_value *value)
0196 {
0197     struct snd_oxfw *oxfw = control->private_data;
0198     struct fw_spkr *spkr = oxfw->spec;
0199     unsigned int i;
0200 
0201     for (i = 0; i < spkr->mixer_channels; ++i)
0202         value->value.integer.value[channel_map[i]] = spkr->volume[i];
0203 
0204     return 0;
0205 }
0206 
0207 static int spkr_volume_put(struct snd_kcontrol *control,
0208                struct snd_ctl_elem_value *value)
0209 {
0210     struct snd_oxfw *oxfw = control->private_data;
0211     struct fw_spkr *spkr = oxfw->spec;
0212     unsigned int i, changed_channels;
0213     bool equal_values = true;
0214     s16 volume;
0215     int err;
0216 
0217     for (i = 0; i < spkr->mixer_channels; ++i) {
0218         if (value->value.integer.value[i] < spkr->volume_min ||
0219             value->value.integer.value[i] > spkr->volume_max)
0220             return -EINVAL;
0221         if (value->value.integer.value[i] !=
0222             value->value.integer.value[0])
0223             equal_values = false;
0224     }
0225 
0226     changed_channels = 0;
0227     for (i = 0; i < spkr->mixer_channels; ++i)
0228         if (value->value.integer.value[channel_map[i]] !=
0229                             spkr->volume[i])
0230             changed_channels |= 1 << (i + 1);
0231 
0232     if (equal_values && changed_channels != 0)
0233         changed_channels = 1 << 0;
0234 
0235     for (i = 0; i <= spkr->mixer_channels; ++i) {
0236         volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
0237         if (changed_channels & (1 << i)) {
0238             err = avc_audio_feature_volume(oxfw->unit,
0239                           spkr->volume_fb_id, &volume,
0240                           i, CTL_CURRENT, CTL_WRITE);
0241             if (err < 0)
0242                 return err;
0243         }
0244         if (i > 0)
0245             spkr->volume[i - 1] = volume;
0246     }
0247 
0248     return changed_channels != 0;
0249 }
0250 
0251 int snd_oxfw_add_spkr(struct snd_oxfw *oxfw, bool is_lacie)
0252 {
0253     static const struct snd_kcontrol_new controls[] = {
0254         {
0255             .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0256             .name = "PCM Playback Switch",
0257             .info = snd_ctl_boolean_mono_info,
0258             .get = spkr_mute_get,
0259             .put = spkr_mute_put,
0260         },
0261         {
0262             .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0263             .name = "PCM Playback Volume",
0264             .info = spkr_volume_info,
0265             .get = spkr_volume_get,
0266             .put = spkr_volume_put,
0267         },
0268     };
0269     struct fw_spkr *spkr;
0270     unsigned int i, first_ch;
0271     int err;
0272 
0273     spkr = devm_kzalloc(&oxfw->card->card_dev, sizeof(struct fw_spkr),
0274                 GFP_KERNEL);
0275     if (!spkr)
0276         return -ENOMEM;
0277     oxfw->spec = spkr;
0278 
0279     if (is_lacie) {
0280         spkr->mixer_channels = 1;
0281         spkr->mute_fb_id = 0x01;
0282         spkr->volume_fb_id = 0x01;
0283     } else {
0284         spkr->mixer_channels = 6;
0285         spkr->mute_fb_id = 0x01;
0286         spkr->volume_fb_id = 0x02;
0287     }
0288 
0289     err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
0290                        &spkr->volume_min, 0, CTL_MIN, CTL_READ);
0291     if (err < 0)
0292         return err;
0293     err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
0294                        &spkr->volume_max, 0, CTL_MAX, CTL_READ);
0295     if (err < 0)
0296         return err;
0297 
0298     err = avc_audio_feature_mute(oxfw->unit, spkr->mute_fb_id, &spkr->mute,
0299                      CTL_READ);
0300     if (err < 0)
0301         return err;
0302 
0303     first_ch = spkr->mixer_channels == 1 ? 0 : 1;
0304     for (i = 0; i < spkr->mixer_channels; ++i) {
0305         err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
0306                            &spkr->volume[i], first_ch + i,
0307                            CTL_CURRENT, CTL_READ);
0308         if (err < 0)
0309             return err;
0310     }
0311 
0312     for (i = 0; i < ARRAY_SIZE(controls); ++i) {
0313         err = snd_ctl_add(oxfw->card,
0314                   snd_ctl_new1(&controls[i], oxfw));
0315         if (err < 0)
0316             return err;
0317     }
0318 
0319     return 0;
0320 }