Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Load Analog Devices SigmaStudio firmware files
0004  *
0005  * Copyright 2009-2014 Analog Devices Inc.
0006  */
0007 
0008 #include <linux/crc32.h>
0009 #include <linux/firmware.h>
0010 #include <linux/kernel.h>
0011 #include <linux/i2c.h>
0012 #include <linux/regmap.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 
0016 #include <sound/control.h>
0017 #include <sound/soc.h>
0018 
0019 #include "sigmadsp.h"
0020 
0021 #define SIGMA_MAGIC "ADISIGM"
0022 
0023 #define SIGMA_FW_CHUNK_TYPE_DATA 0
0024 #define SIGMA_FW_CHUNK_TYPE_CONTROL 1
0025 #define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
0026 
0027 #define READBACK_CTRL_NAME "ReadBack"
0028 
0029 struct sigmadsp_control {
0030     struct list_head head;
0031     uint32_t samplerates;
0032     unsigned int addr;
0033     unsigned int num_bytes;
0034     const char *name;
0035     struct snd_kcontrol *kcontrol;
0036     bool is_readback;
0037     bool cached;
0038     uint8_t cache[];
0039 };
0040 
0041 struct sigmadsp_data {
0042     struct list_head head;
0043     uint32_t samplerates;
0044     unsigned int addr;
0045     unsigned int length;
0046     uint8_t data[];
0047 };
0048 
0049 struct sigma_fw_chunk {
0050     __le32 length;
0051     __le32 tag;
0052     __le32 samplerates;
0053 } __packed;
0054 
0055 struct sigma_fw_chunk_data {
0056     struct sigma_fw_chunk chunk;
0057     __le16 addr;
0058     uint8_t data[];
0059 } __packed;
0060 
0061 struct sigma_fw_chunk_control {
0062     struct sigma_fw_chunk chunk;
0063     __le16 type;
0064     __le16 addr;
0065     __le16 num_bytes;
0066     const char name[];
0067 } __packed;
0068 
0069 struct sigma_fw_chunk_samplerate {
0070     struct sigma_fw_chunk chunk;
0071     __le32 samplerates[];
0072 } __packed;
0073 
0074 struct sigma_firmware_header {
0075     unsigned char magic[7];
0076     u8 version;
0077     __le32 crc;
0078 } __packed;
0079 
0080 enum {
0081     SIGMA_ACTION_WRITEXBYTES = 0,
0082     SIGMA_ACTION_WRITESINGLE,
0083     SIGMA_ACTION_WRITESAFELOAD,
0084     SIGMA_ACTION_END,
0085 };
0086 
0087 struct sigma_action {
0088     u8 instr;
0089     u8 len_hi;
0090     __le16 len;
0091     __be16 addr;
0092     unsigned char payload[];
0093 } __packed;
0094 
0095 static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
0096     const uint8_t data[], size_t len)
0097 {
0098     return sigmadsp->write(sigmadsp->control_data, addr, data, len);
0099 }
0100 
0101 static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
0102     uint8_t data[], size_t len)
0103 {
0104     return sigmadsp->read(sigmadsp->control_data, addr, data, len);
0105 }
0106 
0107 static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
0108     struct snd_ctl_elem_info *info)
0109 {
0110     struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
0111 
0112     info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
0113     info->count = ctrl->num_bytes;
0114 
0115     return 0;
0116 }
0117 
0118 static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
0119     struct sigmadsp_control *ctrl, void *data)
0120 {
0121     /* safeload loads up to 20 bytes in a atomic operation */
0122     if (ctrl->num_bytes <= 20 && sigmadsp->ops && sigmadsp->ops->safeload)
0123         return sigmadsp->ops->safeload(sigmadsp, ctrl->addr, data,
0124             ctrl->num_bytes);
0125     else
0126         return sigmadsp_write(sigmadsp, ctrl->addr, data,
0127             ctrl->num_bytes);
0128 }
0129 
0130 static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
0131     struct snd_ctl_elem_value *ucontrol)
0132 {
0133     struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
0134     struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
0135     uint8_t *data;
0136     int ret = 0;
0137 
0138     mutex_lock(&sigmadsp->lock);
0139 
0140     data = ucontrol->value.bytes.data;
0141 
0142     if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
0143         ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
0144 
0145     if (ret == 0) {
0146         memcpy(ctrl->cache, data, ctrl->num_bytes);
0147         if (!ctrl->is_readback)
0148             ctrl->cached = true;
0149     }
0150 
0151     mutex_unlock(&sigmadsp->lock);
0152 
0153     return ret;
0154 }
0155 
0156 static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
0157     struct snd_ctl_elem_value *ucontrol)
0158 {
0159     struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
0160     struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
0161     int ret = 0;
0162 
0163     mutex_lock(&sigmadsp->lock);
0164 
0165     if (!ctrl->cached) {
0166         ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
0167             ctrl->num_bytes);
0168     }
0169 
0170     if (ret == 0) {
0171         if (!ctrl->is_readback)
0172             ctrl->cached = true;
0173         memcpy(ucontrol->value.bytes.data, ctrl->cache,
0174             ctrl->num_bytes);
0175     }
0176 
0177     mutex_unlock(&sigmadsp->lock);
0178 
0179     return ret;
0180 }
0181 
0182 static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
0183 {
0184     struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
0185 
0186     ctrl->kcontrol = NULL;
0187 }
0188 
0189 static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
0190 {
0191     unsigned int i;
0192 
0193     for (i = 0; i < len; i++) {
0194         /* Normal ASCII characters are valid */
0195         if (name[i] < ' ' || name[i] > '~')
0196             return false;
0197     }
0198 
0199     return true;
0200 }
0201 
0202 static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
0203     const struct sigma_fw_chunk *chunk, unsigned int length)
0204 {
0205     const struct sigma_fw_chunk_control *ctrl_chunk;
0206     struct sigmadsp_control *ctrl;
0207     unsigned int num_bytes;
0208     size_t name_len;
0209     char *name;
0210     int ret;
0211 
0212     if (length <= sizeof(*ctrl_chunk))
0213         return -EINVAL;
0214 
0215     ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
0216 
0217     name_len = length - sizeof(*ctrl_chunk);
0218     if (name_len >= SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
0219         name_len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1;
0220 
0221     /* Make sure there are no non-displayable characaters in the string */
0222     if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
0223         return -EINVAL;
0224 
0225     num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
0226     ctrl = kzalloc(sizeof(*ctrl) + num_bytes, GFP_KERNEL);
0227     if (!ctrl)
0228         return -ENOMEM;
0229 
0230     name = kzalloc(name_len + 1, GFP_KERNEL);
0231     if (!name) {
0232         ret = -ENOMEM;
0233         goto err_free_ctrl;
0234     }
0235     memcpy(name, ctrl_chunk->name, name_len);
0236     name[name_len] = '\0';
0237     ctrl->name = name;
0238 
0239     /*
0240      * Readbacks doesn't work with non-volatile controls, since the
0241      * firmware updates the control value without driver interaction. Mark
0242      * the readbacks to ensure that the values are not cached.
0243      */
0244     if (ctrl->name && strncmp(ctrl->name, READBACK_CTRL_NAME,
0245                   (sizeof(READBACK_CTRL_NAME) - 1)) == 0)
0246         ctrl->is_readback = true;
0247 
0248     ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
0249     ctrl->num_bytes = num_bytes;
0250     ctrl->samplerates = le32_to_cpu(chunk->samplerates);
0251 
0252     list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
0253 
0254     return 0;
0255 
0256 err_free_ctrl:
0257     kfree(ctrl);
0258 
0259     return ret;
0260 }
0261 
0262 static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
0263     const struct sigma_fw_chunk *chunk, unsigned int length)
0264 {
0265     const struct sigma_fw_chunk_data *data_chunk;
0266     struct sigmadsp_data *data;
0267 
0268     if (length <= sizeof(*data_chunk))
0269         return -EINVAL;
0270 
0271     data_chunk = (struct sigma_fw_chunk_data *)chunk;
0272 
0273     length -= sizeof(*data_chunk);
0274 
0275     data = kzalloc(sizeof(*data) + length, GFP_KERNEL);
0276     if (!data)
0277         return -ENOMEM;
0278 
0279     data->addr = le16_to_cpu(data_chunk->addr);
0280     data->length = length;
0281     data->samplerates = le32_to_cpu(chunk->samplerates);
0282     memcpy(data->data, data_chunk->data, length);
0283     list_add_tail(&data->head, &sigmadsp->data_list);
0284 
0285     return 0;
0286 }
0287 
0288 static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
0289     const struct sigma_fw_chunk *chunk, unsigned int length)
0290 {
0291     const struct sigma_fw_chunk_samplerate *rate_chunk;
0292     unsigned int num_rates;
0293     unsigned int *rates;
0294     unsigned int i;
0295 
0296     rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
0297 
0298     num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
0299 
0300     if (num_rates > 32 || num_rates == 0)
0301         return -EINVAL;
0302 
0303     /* We only allow one samplerates block per file */
0304     if (sigmadsp->rate_constraints.count)
0305         return -EINVAL;
0306 
0307     rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
0308     if (!rates)
0309         return -ENOMEM;
0310 
0311     for (i = 0; i < num_rates; i++)
0312         rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
0313 
0314     sigmadsp->rate_constraints.count = num_rates;
0315     sigmadsp->rate_constraints.list = rates;
0316 
0317     return 0;
0318 }
0319 
0320 static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
0321     const struct firmware *fw)
0322 {
0323     struct sigma_fw_chunk *chunk;
0324     unsigned int length, pos;
0325     int ret;
0326 
0327     /*
0328      * Make sure that there is at least one chunk to avoid integer
0329      * underflows later on. Empty firmware is still valid though.
0330      */
0331     if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
0332         return 0;
0333 
0334     pos = sizeof(struct sigma_firmware_header);
0335 
0336     while (pos < fw->size - sizeof(*chunk)) {
0337         chunk = (struct sigma_fw_chunk *)(fw->data + pos);
0338 
0339         length = le32_to_cpu(chunk->length);
0340 
0341         if (length > fw->size - pos || length < sizeof(*chunk))
0342             return -EINVAL;
0343 
0344         switch (le32_to_cpu(chunk->tag)) {
0345         case SIGMA_FW_CHUNK_TYPE_DATA:
0346             ret = sigma_fw_load_data(sigmadsp, chunk, length);
0347             break;
0348         case SIGMA_FW_CHUNK_TYPE_CONTROL:
0349             ret = sigma_fw_load_control(sigmadsp, chunk, length);
0350             break;
0351         case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
0352             ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
0353             break;
0354         default:
0355             dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
0356                 chunk->tag);
0357             ret = 0;
0358             break;
0359         }
0360 
0361         if (ret)
0362             return ret;
0363 
0364         /*
0365          * This can not overflow since if length is larger than the
0366          * maximum firmware size (0x4000000) we'll error out earilier.
0367          */
0368         pos += ALIGN(length, sizeof(__le32));
0369     }
0370 
0371     return 0;
0372 }
0373 
0374 static inline u32 sigma_action_len(struct sigma_action *sa)
0375 {
0376     return (sa->len_hi << 16) | le16_to_cpu(sa->len);
0377 }
0378 
0379 static size_t sigma_action_size(struct sigma_action *sa)
0380 {
0381     size_t payload = 0;
0382 
0383     switch (sa->instr) {
0384     case SIGMA_ACTION_WRITEXBYTES:
0385     case SIGMA_ACTION_WRITESINGLE:
0386     case SIGMA_ACTION_WRITESAFELOAD:
0387         payload = sigma_action_len(sa);
0388         break;
0389     default:
0390         break;
0391     }
0392 
0393     payload = ALIGN(payload, 2);
0394 
0395     return payload + sizeof(struct sigma_action);
0396 }
0397 
0398 /*
0399  * Returns a negative error value in case of an error, 0 if processing of
0400  * the firmware should be stopped after this action, 1 otherwise.
0401  */
0402 static int process_sigma_action(struct sigmadsp *sigmadsp,
0403     struct sigma_action *sa)
0404 {
0405     size_t len = sigma_action_len(sa);
0406     struct sigmadsp_data *data;
0407 
0408     pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
0409         sa->instr, sa->addr, len);
0410 
0411     switch (sa->instr) {
0412     case SIGMA_ACTION_WRITEXBYTES:
0413     case SIGMA_ACTION_WRITESINGLE:
0414     case SIGMA_ACTION_WRITESAFELOAD:
0415         if (len < 3)
0416             return -EINVAL;
0417 
0418         data = kzalloc(sizeof(*data) + len - 2, GFP_KERNEL);
0419         if (!data)
0420             return -ENOMEM;
0421 
0422         data->addr = be16_to_cpu(sa->addr);
0423         data->length = len - 2;
0424         memcpy(data->data, sa->payload, data->length);
0425         list_add_tail(&data->head, &sigmadsp->data_list);
0426         break;
0427     case SIGMA_ACTION_END:
0428         return 0;
0429     default:
0430         return -EINVAL;
0431     }
0432 
0433     return 1;
0434 }
0435 
0436 static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
0437     const struct firmware *fw)
0438 {
0439     struct sigma_action *sa;
0440     size_t size, pos;
0441     int ret;
0442 
0443     pos = sizeof(struct sigma_firmware_header);
0444 
0445     while (pos + sizeof(*sa) <= fw->size) {
0446         sa = (struct sigma_action *)(fw->data + pos);
0447 
0448         size = sigma_action_size(sa);
0449         pos += size;
0450         if (pos > fw->size || size == 0)
0451             break;
0452 
0453         ret = process_sigma_action(sigmadsp, sa);
0454 
0455         pr_debug("%s: action returned %i\n", __func__, ret);
0456 
0457         if (ret <= 0)
0458             return ret;
0459     }
0460 
0461     if (pos != fw->size)
0462         return -EINVAL;
0463 
0464     return 0;
0465 }
0466 
0467 static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
0468 {
0469     struct sigmadsp_control *ctrl, *_ctrl;
0470     struct sigmadsp_data *data, *_data;
0471 
0472     list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
0473         kfree(ctrl->name);
0474         kfree(ctrl);
0475     }
0476 
0477     list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
0478         kfree(data);
0479 
0480     INIT_LIST_HEAD(&sigmadsp->ctrl_list);
0481     INIT_LIST_HEAD(&sigmadsp->data_list);
0482 }
0483 
0484 static void devm_sigmadsp_release(struct device *dev, void *res)
0485 {
0486     sigmadsp_firmware_release((struct sigmadsp *)res);
0487 }
0488 
0489 static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
0490 {
0491     const struct sigma_firmware_header *ssfw_head;
0492     const struct firmware *fw;
0493     int ret;
0494     u32 crc;
0495 
0496     /* first load the blob */
0497     ret = request_firmware(&fw, name, sigmadsp->dev);
0498     if (ret) {
0499         pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
0500         goto done;
0501     }
0502 
0503     /* then verify the header */
0504     ret = -EINVAL;
0505 
0506     /*
0507      * Reject too small or unreasonable large files. The upper limit has been
0508      * chosen a bit arbitrarily, but it should be enough for all practical
0509      * purposes and having the limit makes it easier to avoid integer
0510      * overflows later in the loading process.
0511      */
0512     if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
0513         dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
0514         goto done;
0515     }
0516 
0517     ssfw_head = (void *)fw->data;
0518     if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
0519         dev_err(sigmadsp->dev, "Failed to load firmware: Invalid magic\n");
0520         goto done;
0521     }
0522 
0523     crc = crc32(0, fw->data + sizeof(*ssfw_head),
0524             fw->size - sizeof(*ssfw_head));
0525     pr_debug("%s: crc=%x\n", __func__, crc);
0526     if (crc != le32_to_cpu(ssfw_head->crc)) {
0527         dev_err(sigmadsp->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
0528             le32_to_cpu(ssfw_head->crc), crc);
0529         goto done;
0530     }
0531 
0532     switch (ssfw_head->version) {
0533     case 1:
0534         ret = sigmadsp_fw_load_v1(sigmadsp, fw);
0535         break;
0536     case 2:
0537         ret = sigmadsp_fw_load_v2(sigmadsp, fw);
0538         break;
0539     default:
0540         dev_err(sigmadsp->dev,
0541             "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
0542             ssfw_head->version);
0543         ret = -EINVAL;
0544         break;
0545     }
0546 
0547     if (ret)
0548         sigmadsp_firmware_release(sigmadsp);
0549 
0550 done:
0551     release_firmware(fw);
0552 
0553     return ret;
0554 }
0555 
0556 static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
0557     const struct sigmadsp_ops *ops, const char *firmware_name)
0558 {
0559     sigmadsp->ops = ops;
0560     sigmadsp->dev = dev;
0561 
0562     INIT_LIST_HEAD(&sigmadsp->ctrl_list);
0563     INIT_LIST_HEAD(&sigmadsp->data_list);
0564     mutex_init(&sigmadsp->lock);
0565 
0566     return sigmadsp_firmware_load(sigmadsp, firmware_name);
0567 }
0568 
0569 /**
0570  * devm_sigmadsp_init() - Initialize SigmaDSP instance
0571  * @dev: The parent device
0572  * @ops: The sigmadsp_ops to use for this instance
0573  * @firmware_name: Name of the firmware file to load
0574  *
0575  * Allocates a SigmaDSP instance and loads the specified firmware file.
0576  *
0577  * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
0578  */
0579 struct sigmadsp *devm_sigmadsp_init(struct device *dev,
0580     const struct sigmadsp_ops *ops, const char *firmware_name)
0581 {
0582     struct sigmadsp *sigmadsp;
0583     int ret;
0584 
0585     sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
0586         GFP_KERNEL);
0587     if (!sigmadsp)
0588         return ERR_PTR(-ENOMEM);
0589 
0590     ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
0591     if (ret) {
0592         devres_free(sigmadsp);
0593         return ERR_PTR(ret);
0594     }
0595 
0596     devres_add(dev, sigmadsp);
0597 
0598     return sigmadsp;
0599 }
0600 EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
0601 
0602 static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
0603 {
0604     unsigned int i;
0605 
0606     for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
0607         if (sigmadsp->rate_constraints.list[i] == rate)
0608             return i;
0609     }
0610 
0611     return -EINVAL;
0612 }
0613 
0614 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
0615     unsigned int samplerate)
0616 {
0617     int samplerate_index;
0618 
0619     if (samplerate == 0)
0620         return 0;
0621 
0622     if (sigmadsp->rate_constraints.count) {
0623         samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
0624         if (samplerate_index < 0)
0625             return 0;
0626 
0627         return BIT(samplerate_index);
0628     } else {
0629         return ~0;
0630     }
0631 }
0632 
0633 static bool sigmadsp_samplerate_valid(unsigned int supported,
0634     unsigned int requested)
0635 {
0636     /* All samplerates are supported */
0637     if (!supported)
0638         return true;
0639 
0640     return supported & requested;
0641 }
0642 
0643 static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
0644     struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
0645 {
0646     struct snd_kcontrol_new template;
0647     struct snd_kcontrol *kcontrol;
0648 
0649     memset(&template, 0, sizeof(template));
0650     template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0651     template.name = ctrl->name;
0652     template.info = sigmadsp_ctrl_info;
0653     template.get = sigmadsp_ctrl_get;
0654     template.put = sigmadsp_ctrl_put;
0655     template.private_value = (unsigned long)ctrl;
0656     template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
0657     if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
0658         template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
0659 
0660     kcontrol = snd_ctl_new1(&template, sigmadsp);
0661     if (!kcontrol)
0662         return -ENOMEM;
0663 
0664     kcontrol->private_free = sigmadsp_control_free;
0665     ctrl->kcontrol = kcontrol;
0666 
0667     return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
0668 }
0669 
0670 static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
0671     struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
0672 {
0673     struct snd_card *card = sigmadsp->component->card->snd_card;
0674     struct snd_kcontrol_volatile *vd;
0675     struct snd_ctl_elem_id id;
0676     bool active;
0677     bool changed = false;
0678 
0679     active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
0680 
0681     down_write(&card->controls_rwsem);
0682     if (!ctrl->kcontrol) {
0683         up_write(&card->controls_rwsem);
0684         return;
0685     }
0686 
0687     id = ctrl->kcontrol->id;
0688     vd = &ctrl->kcontrol->vd[0];
0689     if (active == (bool)(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)) {
0690         vd->access ^= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
0691         changed = true;
0692     }
0693     up_write(&card->controls_rwsem);
0694 
0695     if (active && changed) {
0696         mutex_lock(&sigmadsp->lock);
0697         if (ctrl->cached)
0698             sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
0699         mutex_unlock(&sigmadsp->lock);
0700     }
0701 
0702     if (changed)
0703         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &id);
0704 }
0705 
0706 /**
0707  * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
0708  * @sigmadsp: The sigmadsp instance to attach
0709  * @component: The component to attach to
0710  *
0711  * Typically called in the components probe callback.
0712  *
0713  * Note, once this function has been called the firmware must not be released
0714  * until after the ALSA snd_card that the component belongs to has been
0715  * disconnected, even if sigmadsp_attach() returns an error.
0716  */
0717 int sigmadsp_attach(struct sigmadsp *sigmadsp,
0718     struct snd_soc_component *component)
0719 {
0720     struct sigmadsp_control *ctrl;
0721     unsigned int samplerate_mask;
0722     int ret;
0723 
0724     sigmadsp->component = component;
0725 
0726     samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
0727         sigmadsp->current_samplerate);
0728 
0729     list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
0730         ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
0731         if (ret)
0732             return ret;
0733     }
0734 
0735     return 0;
0736 }
0737 EXPORT_SYMBOL_GPL(sigmadsp_attach);
0738 
0739 /**
0740  * sigmadsp_setup() - Setup the DSP for the specified samplerate
0741  * @sigmadsp: The sigmadsp instance to configure
0742  * @samplerate: The samplerate the DSP should be configured for
0743  *
0744  * Loads the appropriate firmware program and parameter memory (if not already
0745  * loaded) and enables the controls for the specified samplerate. Any control
0746  * parameter changes that have been made previously will be restored.
0747  *
0748  * Returns 0 on success, a negative error code otherwise.
0749  */
0750 int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
0751 {
0752     struct sigmadsp_control *ctrl;
0753     unsigned int samplerate_mask;
0754     struct sigmadsp_data *data;
0755     int ret;
0756 
0757     if (sigmadsp->current_samplerate == samplerate)
0758         return 0;
0759 
0760     samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
0761     if (samplerate_mask == 0)
0762         return -EINVAL;
0763 
0764     list_for_each_entry(data, &sigmadsp->data_list, head) {
0765         if (!sigmadsp_samplerate_valid(data->samplerates,
0766             samplerate_mask))
0767             continue;
0768         ret = sigmadsp_write(sigmadsp, data->addr, data->data,
0769             data->length);
0770         if (ret)
0771             goto err;
0772     }
0773 
0774     list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
0775         sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
0776 
0777     sigmadsp->current_samplerate = samplerate;
0778 
0779     return 0;
0780 err:
0781     sigmadsp_reset(sigmadsp);
0782 
0783     return ret;
0784 }
0785 EXPORT_SYMBOL_GPL(sigmadsp_setup);
0786 
0787 /**
0788  * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
0789  * @sigmadsp: The sigmadsp instance to reset
0790  *
0791  * Should be called whenever the DSP has been reset and parameter and program
0792  * memory need to be re-loaded.
0793  */
0794 void sigmadsp_reset(struct sigmadsp *sigmadsp)
0795 {
0796     struct sigmadsp_control *ctrl;
0797 
0798     list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
0799         sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
0800 
0801     sigmadsp->current_samplerate = 0;
0802 }
0803 EXPORT_SYMBOL_GPL(sigmadsp_reset);
0804 
0805 /**
0806  * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
0807  * @sigmadsp: The sigmadsp instance
0808  * @substream: The substream to restrict
0809  *
0810  * Applies samplerate constraints that may be required by the firmware Should
0811  * typically be called from the CODEC/component drivers startup callback.
0812  *
0813  * Returns 0 on success, a negative error code otherwise.
0814  */
0815 int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
0816     struct snd_pcm_substream *substream)
0817 {
0818     if (sigmadsp->rate_constraints.count == 0)
0819         return 0;
0820 
0821     return snd_pcm_hw_constraint_list(substream->runtime, 0,
0822         SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
0823 }
0824 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
0825 
0826 MODULE_LICENSE("GPL");