linux/sound/soc/codecs/sigmadsp.c
<<
>>
Prefs
   1/*
   2 * Load Analog Devices SigmaStudio firmware files
   3 *
   4 * Copyright 2009-2014 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later.
   7 */
   8
   9#include <linux/crc32.h>
  10#include <linux/firmware.h>
  11#include <linux/kernel.h>
  12#include <linux/i2c.h>
  13#include <linux/regmap.h>
  14#include <linux/module.h>
  15#include <linux/slab.h>
  16
  17#include <sound/control.h>
  18#include <sound/soc.h>
  19
  20#include "sigmadsp.h"
  21
  22#define SIGMA_MAGIC "ADISIGM"
  23
  24#define SIGMA_FW_CHUNK_TYPE_DATA 0
  25#define SIGMA_FW_CHUNK_TYPE_CONTROL 1
  26#define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
  27
  28struct sigmadsp_control {
  29        struct list_head head;
  30        uint32_t samplerates;
  31        unsigned int addr;
  32        unsigned int num_bytes;
  33        const char *name;
  34        struct snd_kcontrol *kcontrol;
  35        bool cached;
  36        uint8_t cache[];
  37};
  38
  39struct sigmadsp_data {
  40        struct list_head head;
  41        uint32_t samplerates;
  42        unsigned int addr;
  43        unsigned int length;
  44        uint8_t data[];
  45};
  46
  47struct sigma_fw_chunk {
  48        __le32 length;
  49        __le32 tag;
  50        __le32 samplerates;
  51} __packed;
  52
  53struct sigma_fw_chunk_data {
  54        struct sigma_fw_chunk chunk;
  55        __le16 addr;
  56        uint8_t data[];
  57} __packed;
  58
  59struct sigma_fw_chunk_control {
  60        struct sigma_fw_chunk chunk;
  61        __le16 type;
  62        __le16 addr;
  63        __le16 num_bytes;
  64        const char name[];
  65} __packed;
  66
  67struct sigma_fw_chunk_samplerate {
  68        struct sigma_fw_chunk chunk;
  69        __le32 samplerates[];
  70} __packed;
  71
  72struct sigma_firmware_header {
  73        unsigned char magic[7];
  74        u8 version;
  75        __le32 crc;
  76} __packed;
  77
  78enum {
  79        SIGMA_ACTION_WRITEXBYTES = 0,
  80        SIGMA_ACTION_WRITESINGLE,
  81        SIGMA_ACTION_WRITESAFELOAD,
  82        SIGMA_ACTION_END,
  83};
  84
  85struct sigma_action {
  86        u8 instr;
  87        u8 len_hi;
  88        __le16 len;
  89        __be16 addr;
  90        unsigned char payload[];
  91} __packed;
  92
  93static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
  94        const uint8_t data[], size_t len)
  95{
  96        return sigmadsp->write(sigmadsp->control_data, addr, data, len);
  97}
  98
  99static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
 100        uint8_t data[], size_t len)
 101{
 102        return sigmadsp->read(sigmadsp->control_data, addr, data, len);
 103}
 104
 105static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
 106        struct snd_ctl_elem_info *info)
 107{
 108        struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
 109
 110        info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
 111        info->count = ctrl->num_bytes;
 112
 113        return 0;
 114}
 115
 116static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
 117        struct sigmadsp_control *ctrl, void *data)
 118{
 119        /* safeload loads up to 20 bytes in a atomic operation */
 120        if (ctrl->num_bytes <= 20 && sigmadsp->ops && sigmadsp->ops->safeload)
 121                return sigmadsp->ops->safeload(sigmadsp, ctrl->addr, data,
 122                        ctrl->num_bytes);
 123        else
 124                return sigmadsp_write(sigmadsp, ctrl->addr, data,
 125                        ctrl->num_bytes);
 126}
 127
 128static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
 129        struct snd_ctl_elem_value *ucontrol)
 130{
 131        struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
 132        struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
 133        uint8_t *data;
 134        int ret = 0;
 135
 136        mutex_lock(&sigmadsp->lock);
 137
 138        data = ucontrol->value.bytes.data;
 139
 140        if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
 141                ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
 142
 143        if (ret == 0) {
 144                memcpy(ctrl->cache, data, ctrl->num_bytes);
 145                ctrl->cached = true;
 146        }
 147
 148        mutex_unlock(&sigmadsp->lock);
 149
 150        return ret;
 151}
 152
 153static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
 154        struct snd_ctl_elem_value *ucontrol)
 155{
 156        struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
 157        struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
 158        int ret = 0;
 159
 160        mutex_lock(&sigmadsp->lock);
 161
 162        if (!ctrl->cached) {
 163                ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
 164                        ctrl->num_bytes);
 165        }
 166
 167        if (ret == 0) {
 168                ctrl->cached = true;
 169                memcpy(ucontrol->value.bytes.data, ctrl->cache,
 170                        ctrl->num_bytes);
 171        }
 172
 173        mutex_unlock(&sigmadsp->lock);
 174
 175        return ret;
 176}
 177
 178static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
 179{
 180        struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
 181
 182        ctrl->kcontrol = NULL;
 183}
 184
 185static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
 186{
 187        unsigned int i;
 188
 189        for (i = 0; i < len; i++) {
 190                /* Normal ASCII characters are valid */
 191                if (name[i] < ' ' || name[i] > '~')
 192                        return false;
 193        }
 194
 195        return true;
 196}
 197
 198static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
 199        const struct sigma_fw_chunk *chunk, unsigned int length)
 200{
 201        const struct sigma_fw_chunk_control *ctrl_chunk;
 202        struct sigmadsp_control *ctrl;
 203        unsigned int num_bytes;
 204        size_t name_len;
 205        char *name;
 206        int ret;
 207
 208        if (length <= sizeof(*ctrl_chunk))
 209                return -EINVAL;
 210
 211        ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
 212
 213        name_len = length - sizeof(*ctrl_chunk);
 214        if (name_len >= SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
 215                name_len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1;
 216
 217        /* Make sure there are no non-displayable characaters in the string */
 218        if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
 219                return -EINVAL;
 220
 221        num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
 222        ctrl = kzalloc(sizeof(*ctrl) + num_bytes, GFP_KERNEL);
 223        if (!ctrl)
 224                return -ENOMEM;
 225
 226        name = kzalloc(name_len + 1, GFP_KERNEL);
 227        if (!name) {
 228                ret = -ENOMEM;
 229                goto err_free_ctrl;
 230        }
 231        memcpy(name, ctrl_chunk->name, name_len);
 232        name[name_len] = '\0';
 233        ctrl->name = name;
 234
 235        ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
 236        ctrl->num_bytes = num_bytes;
 237        ctrl->samplerates = le32_to_cpu(chunk->samplerates);
 238
 239        list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
 240
 241        return 0;
 242
 243err_free_ctrl:
 244        kfree(ctrl);
 245
 246        return ret;
 247}
 248
 249static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
 250        const struct sigma_fw_chunk *chunk, unsigned int length)
 251{
 252        const struct sigma_fw_chunk_data *data_chunk;
 253        struct sigmadsp_data *data;
 254
 255        if (length <= sizeof(*data_chunk))
 256                return -EINVAL;
 257
 258        data_chunk = (struct sigma_fw_chunk_data *)chunk;
 259
 260        length -= sizeof(*data_chunk);
 261
 262        data = kzalloc(sizeof(*data) + length, GFP_KERNEL);
 263        if (!data)
 264                return -ENOMEM;
 265
 266        data->addr = le16_to_cpu(data_chunk->addr);
 267        data->length = length;
 268        data->samplerates = le32_to_cpu(chunk->samplerates);
 269        memcpy(data->data, data_chunk->data, length);
 270        list_add_tail(&data->head, &sigmadsp->data_list);
 271
 272        return 0;
 273}
 274
 275static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
 276        const struct sigma_fw_chunk *chunk, unsigned int length)
 277{
 278        const struct sigma_fw_chunk_samplerate *rate_chunk;
 279        unsigned int num_rates;
 280        unsigned int *rates;
 281        unsigned int i;
 282
 283        rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
 284
 285        num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
 286
 287        if (num_rates > 32 || num_rates == 0)
 288                return -EINVAL;
 289
 290        /* We only allow one samplerates block per file */
 291        if (sigmadsp->rate_constraints.count)
 292                return -EINVAL;
 293
 294        rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
 295        if (!rates)
 296                return -ENOMEM;
 297
 298        for (i = 0; i < num_rates; i++)
 299                rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
 300
 301        sigmadsp->rate_constraints.count = num_rates;
 302        sigmadsp->rate_constraints.list = rates;
 303
 304        return 0;
 305}
 306
 307static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
 308        const struct firmware *fw)
 309{
 310        struct sigma_fw_chunk *chunk;
 311        unsigned int length, pos;
 312        int ret;
 313
 314        /*
 315         * Make sure that there is at least one chunk to avoid integer
 316         * underflows later on. Empty firmware is still valid though.
 317         */
 318        if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
 319                return 0;
 320
 321        pos = sizeof(struct sigma_firmware_header);
 322
 323        while (pos < fw->size - sizeof(*chunk)) {
 324                chunk = (struct sigma_fw_chunk *)(fw->data + pos);
 325
 326                length = le32_to_cpu(chunk->length);
 327
 328                if (length > fw->size - pos || length < sizeof(*chunk))
 329                        return -EINVAL;
 330
 331                switch (le32_to_cpu(chunk->tag)) {
 332                case SIGMA_FW_CHUNK_TYPE_DATA:
 333                        ret = sigma_fw_load_data(sigmadsp, chunk, length);
 334                        break;
 335                case SIGMA_FW_CHUNK_TYPE_CONTROL:
 336                        ret = sigma_fw_load_control(sigmadsp, chunk, length);
 337                        break;
 338                case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
 339                        ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
 340                        break;
 341                default:
 342                        dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
 343                                chunk->tag);
 344                        ret = 0;
 345                        break;
 346                }
 347
 348                if (ret)
 349                        return ret;
 350
 351                /*
 352                 * This can not overflow since if length is larger than the
 353                 * maximum firmware size (0x4000000) we'll error out earilier.
 354                 */
 355                pos += ALIGN(length, sizeof(__le32));
 356        }
 357
 358        return 0;
 359}
 360
 361static inline u32 sigma_action_len(struct sigma_action *sa)
 362{
 363        return (sa->len_hi << 16) | le16_to_cpu(sa->len);
 364}
 365
 366static size_t sigma_action_size(struct sigma_action *sa)
 367{
 368        size_t payload = 0;
 369
 370        switch (sa->instr) {
 371        case SIGMA_ACTION_WRITEXBYTES:
 372        case SIGMA_ACTION_WRITESINGLE:
 373        case SIGMA_ACTION_WRITESAFELOAD:
 374                payload = sigma_action_len(sa);
 375                break;
 376        default:
 377                break;
 378        }
 379
 380        payload = ALIGN(payload, 2);
 381
 382        return payload + sizeof(struct sigma_action);
 383}
 384
 385/*
 386 * Returns a negative error value in case of an error, 0 if processing of
 387 * the firmware should be stopped after this action, 1 otherwise.
 388 */
 389static int process_sigma_action(struct sigmadsp *sigmadsp,
 390        struct sigma_action *sa)
 391{
 392        size_t len = sigma_action_len(sa);
 393        struct sigmadsp_data *data;
 394
 395        pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
 396                sa->instr, sa->addr, len);
 397
 398        switch (sa->instr) {
 399        case SIGMA_ACTION_WRITEXBYTES:
 400        case SIGMA_ACTION_WRITESINGLE:
 401        case SIGMA_ACTION_WRITESAFELOAD:
 402                if (len < 3)
 403                        return -EINVAL;
 404
 405                data = kzalloc(sizeof(*data) + len - 2, GFP_KERNEL);
 406                if (!data)
 407                        return -ENOMEM;
 408
 409                data->addr = be16_to_cpu(sa->addr);
 410                data->length = len - 2;
 411                memcpy(data->data, sa->payload, data->length);
 412                list_add_tail(&data->head, &sigmadsp->data_list);
 413                break;
 414        case SIGMA_ACTION_END:
 415                return 0;
 416        default:
 417                return -EINVAL;
 418        }
 419
 420        return 1;
 421}
 422
 423static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
 424        const struct firmware *fw)
 425{
 426        struct sigma_action *sa;
 427        size_t size, pos;
 428        int ret;
 429
 430        pos = sizeof(struct sigma_firmware_header);
 431
 432        while (pos + sizeof(*sa) <= fw->size) {
 433                sa = (struct sigma_action *)(fw->data + pos);
 434
 435                size = sigma_action_size(sa);
 436                pos += size;
 437                if (pos > fw->size || size == 0)
 438                        break;
 439
 440                ret = process_sigma_action(sigmadsp, sa);
 441
 442                pr_debug("%s: action returned %i\n", __func__, ret);
 443
 444                if (ret <= 0)
 445                        return ret;
 446        }
 447
 448        if (pos != fw->size)
 449                return -EINVAL;
 450
 451        return 0;
 452}
 453
 454static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
 455{
 456        struct sigmadsp_control *ctrl, *_ctrl;
 457        struct sigmadsp_data *data, *_data;
 458
 459        list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
 460                kfree(ctrl->name);
 461                kfree(ctrl);
 462        }
 463
 464        list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
 465                kfree(data);
 466
 467        INIT_LIST_HEAD(&sigmadsp->ctrl_list);
 468        INIT_LIST_HEAD(&sigmadsp->data_list);
 469}
 470
 471static void devm_sigmadsp_release(struct device *dev, void *res)
 472{
 473        sigmadsp_firmware_release((struct sigmadsp *)res);
 474}
 475
 476static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
 477{
 478        const struct sigma_firmware_header *ssfw_head;
 479        const struct firmware *fw;
 480        int ret;
 481        u32 crc;
 482
 483        /* first load the blob */
 484        ret = request_firmware(&fw, name, sigmadsp->dev);
 485        if (ret) {
 486                pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
 487                goto done;
 488        }
 489
 490        /* then verify the header */
 491        ret = -EINVAL;
 492
 493        /*
 494         * Reject too small or unreasonable large files. The upper limit has been
 495         * chosen a bit arbitrarily, but it should be enough for all practical
 496         * purposes and having the limit makes it easier to avoid integer
 497         * overflows later in the loading process.
 498         */
 499        if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
 500                dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
 501                goto done;
 502        }
 503
 504        ssfw_head = (void *)fw->data;
 505        if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
 506                dev_err(sigmadsp->dev, "Failed to load firmware: Invalid magic\n");
 507                goto done;
 508        }
 509
 510        crc = crc32(0, fw->data + sizeof(*ssfw_head),
 511                        fw->size - sizeof(*ssfw_head));
 512        pr_debug("%s: crc=%x\n", __func__, crc);
 513        if (crc != le32_to_cpu(ssfw_head->crc)) {
 514                dev_err(sigmadsp->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
 515                        le32_to_cpu(ssfw_head->crc), crc);
 516                goto done;
 517        }
 518
 519        switch (ssfw_head->version) {
 520        case 1:
 521                ret = sigmadsp_fw_load_v1(sigmadsp, fw);
 522                break;
 523        case 2:
 524                ret = sigmadsp_fw_load_v2(sigmadsp, fw);
 525                break;
 526        default:
 527                dev_err(sigmadsp->dev,
 528                        "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
 529                        ssfw_head->version);
 530                ret = -EINVAL;
 531                break;
 532        }
 533
 534        if (ret)
 535                sigmadsp_firmware_release(sigmadsp);
 536
 537done:
 538        release_firmware(fw);
 539
 540        return ret;
 541}
 542
 543static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
 544        const struct sigmadsp_ops *ops, const char *firmware_name)
 545{
 546        sigmadsp->ops = ops;
 547        sigmadsp->dev = dev;
 548
 549        INIT_LIST_HEAD(&sigmadsp->ctrl_list);
 550        INIT_LIST_HEAD(&sigmadsp->data_list);
 551        mutex_init(&sigmadsp->lock);
 552
 553        return sigmadsp_firmware_load(sigmadsp, firmware_name);
 554}
 555
 556/**
 557 * devm_sigmadsp_init() - Initialize SigmaDSP instance
 558 * @dev: The parent device
 559 * @ops: The sigmadsp_ops to use for this instance
 560 * @firmware_name: Name of the firmware file to load
 561 *
 562 * Allocates a SigmaDSP instance and loads the specified firmware file.
 563 *
 564 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
 565 */
 566struct sigmadsp *devm_sigmadsp_init(struct device *dev,
 567        const struct sigmadsp_ops *ops, const char *firmware_name)
 568{
 569        struct sigmadsp *sigmadsp;
 570        int ret;
 571
 572        sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
 573                GFP_KERNEL);
 574        if (!sigmadsp)
 575                return ERR_PTR(-ENOMEM);
 576
 577        ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
 578        if (ret) {
 579                devres_free(sigmadsp);
 580                return ERR_PTR(ret);
 581        }
 582
 583        devres_add(dev, sigmadsp);
 584
 585        return sigmadsp;
 586}
 587EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
 588
 589static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
 590{
 591        unsigned int i;
 592
 593        for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
 594                if (sigmadsp->rate_constraints.list[i] == rate)
 595                        return i;
 596        }
 597
 598        return -EINVAL;
 599}
 600
 601static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
 602        unsigned int samplerate)
 603{
 604        int samplerate_index;
 605
 606        if (samplerate == 0)
 607                return 0;
 608
 609        if (sigmadsp->rate_constraints.count) {
 610                samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
 611                if (samplerate_index < 0)
 612                        return 0;
 613
 614                return BIT(samplerate_index);
 615        } else {
 616                return ~0;
 617        }
 618}
 619
 620static bool sigmadsp_samplerate_valid(unsigned int supported,
 621        unsigned int requested)
 622{
 623        /* All samplerates are supported */
 624        if (!supported)
 625                return true;
 626
 627        return supported & requested;
 628}
 629
 630static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
 631        struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
 632{
 633        struct snd_kcontrol_new template;
 634        struct snd_kcontrol *kcontrol;
 635
 636        memset(&template, 0, sizeof(template));
 637        template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 638        template.name = ctrl->name;
 639        template.info = sigmadsp_ctrl_info;
 640        template.get = sigmadsp_ctrl_get;
 641        template.put = sigmadsp_ctrl_put;
 642        template.private_value = (unsigned long)ctrl;
 643        template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
 644        if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
 645                template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
 646
 647        kcontrol = snd_ctl_new1(&template, sigmadsp);
 648        if (!kcontrol)
 649                return -ENOMEM;
 650
 651        kcontrol->private_free = sigmadsp_control_free;
 652        ctrl->kcontrol = kcontrol;
 653
 654        return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
 655}
 656
 657static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
 658        struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
 659{
 660        struct snd_card *card = sigmadsp->component->card->snd_card;
 661        struct snd_kcontrol_volatile *vd;
 662        struct snd_ctl_elem_id id;
 663        bool active;
 664        bool changed = false;
 665
 666        active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
 667
 668        down_write(&card->controls_rwsem);
 669        if (!ctrl->kcontrol) {
 670                up_write(&card->controls_rwsem);
 671                return;
 672        }
 673
 674        id = ctrl->kcontrol->id;
 675        vd = &ctrl->kcontrol->vd[0];
 676        if (active == (bool)(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)) {
 677                vd->access ^= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
 678                changed = true;
 679        }
 680        up_write(&card->controls_rwsem);
 681
 682        if (active && changed) {
 683                mutex_lock(&sigmadsp->lock);
 684                if (ctrl->cached)
 685                        sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
 686                mutex_unlock(&sigmadsp->lock);
 687        }
 688
 689        if (changed)
 690                snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &id);
 691}
 692
 693/**
 694 * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
 695 * @sigmadsp: The sigmadsp instance to attach
 696 * @component: The component to attach to
 697 *
 698 * Typically called in the components probe callback.
 699 *
 700 * Note, once this function has been called the firmware must not be released
 701 * until after the ALSA snd_card that the component belongs to has been
 702 * disconnected, even if sigmadsp_attach() returns an error.
 703 */
 704int sigmadsp_attach(struct sigmadsp *sigmadsp,
 705        struct snd_soc_component *component)
 706{
 707        struct sigmadsp_control *ctrl;
 708        unsigned int samplerate_mask;
 709        int ret;
 710
 711        sigmadsp->component = component;
 712
 713        samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
 714                sigmadsp->current_samplerate);
 715
 716        list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
 717                ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
 718                if (ret)
 719                        return ret;
 720        }
 721
 722        return 0;
 723}
 724EXPORT_SYMBOL_GPL(sigmadsp_attach);
 725
 726/**
 727 * sigmadsp_setup() - Setup the DSP for the specified samplerate
 728 * @sigmadsp: The sigmadsp instance to configure
 729 * @samplerate: The samplerate the DSP should be configured for
 730 *
 731 * Loads the appropriate firmware program and parameter memory (if not already
 732 * loaded) and enables the controls for the specified samplerate. Any control
 733 * parameter changes that have been made previously will be restored.
 734 *
 735 * Returns 0 on success, a negative error code otherwise.
 736 */
 737int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
 738{
 739        struct sigmadsp_control *ctrl;
 740        unsigned int samplerate_mask;
 741        struct sigmadsp_data *data;
 742        int ret;
 743
 744        if (sigmadsp->current_samplerate == samplerate)
 745                return 0;
 746
 747        samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
 748        if (samplerate_mask == 0)
 749                return -EINVAL;
 750
 751        list_for_each_entry(data, &sigmadsp->data_list, head) {
 752                if (!sigmadsp_samplerate_valid(data->samplerates,
 753                    samplerate_mask))
 754                        continue;
 755                ret = sigmadsp_write(sigmadsp, data->addr, data->data,
 756                        data->length);
 757                if (ret)
 758                        goto err;
 759        }
 760
 761        list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
 762                sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
 763
 764        sigmadsp->current_samplerate = samplerate;
 765
 766        return 0;
 767err:
 768        sigmadsp_reset(sigmadsp);
 769
 770        return ret;
 771}
 772EXPORT_SYMBOL_GPL(sigmadsp_setup);
 773
 774/**
 775 * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
 776 * @sigmadsp: The sigmadsp instance to reset
 777 *
 778 * Should be called whenever the DSP has been reset and parameter and program
 779 * memory need to be re-loaded.
 780 */
 781void sigmadsp_reset(struct sigmadsp *sigmadsp)
 782{
 783        struct sigmadsp_control *ctrl;
 784
 785        list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
 786                sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
 787
 788        sigmadsp->current_samplerate = 0;
 789}
 790EXPORT_SYMBOL_GPL(sigmadsp_reset);
 791
 792/**
 793 * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
 794 * @sigmadsp: The sigmadsp instance
 795 * @substream: The substream to restrict
 796 *
 797 * Applies samplerate constraints that may be required by the firmware Should
 798 * typically be called from the CODEC/component drivers startup callback.
 799 *
 800 * Returns 0 on success, a negative error code otherwise.
 801 */
 802int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
 803        struct snd_pcm_substream *substream)
 804{
 805        if (sigmadsp->rate_constraints.count == 0)
 806                return 0;
 807
 808        return snd_pcm_hw_constraint_list(substream->runtime, 0,
 809                SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
 810}
 811EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
 812
 813MODULE_LICENSE("GPL");
 814