Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dsp_pipeline.c: pipelined audio processing
0004  *
0005  * Copyright (C) 2007, Nadi Sarrar
0006  *
0007  * Nadi Sarrar <nadi@beronet.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/slab.h>
0012 #include <linux/list.h>
0013 #include <linux/string.h>
0014 #include <linux/mISDNif.h>
0015 #include <linux/mISDNdsp.h>
0016 #include <linux/export.h>
0017 #include "dsp.h"
0018 #include "dsp_hwec.h"
0019 
0020 struct dsp_pipeline_entry {
0021     struct mISDN_dsp_element *elem;
0022     void                *p;
0023     struct list_head     list;
0024 };
0025 struct dsp_element_entry {
0026     struct mISDN_dsp_element *elem;
0027     struct device        dev;
0028     struct list_head     list;
0029 };
0030 
0031 static LIST_HEAD(dsp_elements);
0032 
0033 /* sysfs */
0034 static struct class *elements_class;
0035 
0036 static ssize_t
0037 attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
0038 {
0039     struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
0040     int i;
0041     char *p = buf;
0042 
0043     *buf = 0;
0044     for (i = 0; i < elem->num_args; i++)
0045         p += sprintf(p, "Name:        %s\n%s%s%sDescription: %s\n\n",
0046                  elem->args[i].name,
0047                  elem->args[i].def ? "Default:     " : "",
0048                  elem->args[i].def ? elem->args[i].def : "",
0049                  elem->args[i].def ? "\n" : "",
0050                  elem->args[i].desc);
0051 
0052     return p - buf;
0053 }
0054 
0055 static struct device_attribute element_attributes[] = {
0056     __ATTR(args, 0444, attr_show_args, NULL),
0057 };
0058 
0059 static void
0060 mISDN_dsp_dev_release(struct device *dev)
0061 {
0062     struct dsp_element_entry *entry =
0063         container_of(dev, struct dsp_element_entry, dev);
0064     list_del(&entry->list);
0065     kfree(entry);
0066 }
0067 
0068 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
0069 {
0070     struct dsp_element_entry *entry;
0071     int ret, i;
0072 
0073     if (!elem)
0074         return -EINVAL;
0075 
0076     entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
0077     if (!entry)
0078         return -ENOMEM;
0079 
0080     entry->elem = elem;
0081 
0082     entry->dev.class = elements_class;
0083     entry->dev.release = mISDN_dsp_dev_release;
0084     dev_set_drvdata(&entry->dev, elem);
0085     dev_set_name(&entry->dev, "%s", elem->name);
0086     ret = device_register(&entry->dev);
0087     if (ret) {
0088         printk(KERN_ERR "%s: failed to register %s\n",
0089                __func__, elem->name);
0090         goto err1;
0091     }
0092     list_add_tail(&entry->list, &dsp_elements);
0093 
0094     for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
0095         ret = device_create_file(&entry->dev,
0096                      &element_attributes[i]);
0097         if (ret) {
0098             printk(KERN_ERR "%s: failed to create device file\n",
0099                    __func__);
0100             goto err2;
0101         }
0102     }
0103 
0104     return 0;
0105 
0106 err2:
0107     device_unregister(&entry->dev);
0108     return ret;
0109 err1:
0110     kfree(entry);
0111     return ret;
0112 }
0113 EXPORT_SYMBOL(mISDN_dsp_element_register);
0114 
0115 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
0116 {
0117     struct dsp_element_entry *entry, *n;
0118 
0119     if (!elem)
0120         return;
0121 
0122     list_for_each_entry_safe(entry, n, &dsp_elements, list)
0123         if (entry->elem == elem) {
0124             device_unregister(&entry->dev);
0125             return;
0126         }
0127     printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
0128 }
0129 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
0130 
0131 int dsp_pipeline_module_init(void)
0132 {
0133     elements_class = class_create(THIS_MODULE, "dsp_pipeline");
0134     if (IS_ERR(elements_class))
0135         return PTR_ERR(elements_class);
0136 
0137     dsp_hwec_init();
0138 
0139     return 0;
0140 }
0141 
0142 void dsp_pipeline_module_exit(void)
0143 {
0144     struct dsp_element_entry *entry, *n;
0145 
0146     dsp_hwec_exit();
0147 
0148     class_destroy(elements_class);
0149 
0150     list_for_each_entry_safe(entry, n, &dsp_elements, list) {
0151         list_del(&entry->list);
0152         printk(KERN_WARNING "%s: element was still registered: %s\n",
0153                __func__, entry->elem->name);
0154         kfree(entry);
0155     }
0156 }
0157 
0158 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
0159 {
0160     if (!pipeline)
0161         return -EINVAL;
0162 
0163     INIT_LIST_HEAD(&pipeline->list);
0164 
0165     return 0;
0166 }
0167 
0168 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
0169 {
0170     struct dsp_pipeline_entry *entry, *n;
0171 
0172     list_for_each_entry_safe(entry, n, &pipeline->list, list) {
0173         list_del(&entry->list);
0174         if (entry->elem == dsp_hwec)
0175             dsp_hwec_disable(container_of(pipeline, struct dsp,
0176                               pipeline));
0177         else
0178             entry->elem->free(entry->p);
0179         kfree(entry);
0180     }
0181 }
0182 
0183 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
0184 {
0185 
0186     if (!pipeline)
0187         return;
0188 
0189     _dsp_pipeline_destroy(pipeline);
0190 }
0191 
0192 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
0193 {
0194     int found = 0;
0195     char *dup, *next, *tok, *name, *args;
0196     struct dsp_element_entry *entry, *n;
0197     struct dsp_pipeline_entry *pipeline_entry;
0198     struct mISDN_dsp_element *elem;
0199 
0200     if (!pipeline)
0201         return -EINVAL;
0202 
0203     if (!list_empty(&pipeline->list))
0204         _dsp_pipeline_destroy(pipeline);
0205 
0206     dup = next = kstrdup(cfg, GFP_ATOMIC);
0207     if (!dup)
0208         return 0;
0209     while ((tok = strsep(&next, "|"))) {
0210         if (!strlen(tok))
0211             continue;
0212         name = strsep(&tok, "(");
0213         args = strsep(&tok, ")");
0214         if (args && !*args)
0215             args = NULL;
0216 
0217         list_for_each_entry_safe(entry, n, &dsp_elements, list)
0218             if (!strcmp(entry->elem->name, name)) {
0219                 elem = entry->elem;
0220 
0221                 pipeline_entry = kmalloc(sizeof(struct
0222                                 dsp_pipeline_entry), GFP_ATOMIC);
0223                 if (!pipeline_entry) {
0224                     printk(KERN_ERR "%s: failed to add "
0225                            "entry to pipeline: %s (out of "
0226                            "memory)\n", __func__, elem->name);
0227                     goto _out;
0228                 }
0229                 pipeline_entry->elem = elem;
0230 
0231                 if (elem == dsp_hwec) {
0232                     /* This is a hack to make the hwec
0233                        available as a pipeline module */
0234                     dsp_hwec_enable(container_of(pipeline,
0235                                      struct dsp, pipeline), args);
0236                     list_add_tail(&pipeline_entry->list,
0237                               &pipeline->list);
0238                 } else {
0239                     pipeline_entry->p = elem->new(args);
0240                     if (pipeline_entry->p) {
0241                         list_add_tail(&pipeline_entry->
0242                                   list, &pipeline->list);
0243                     } else {
0244                         printk(KERN_ERR "%s: failed "
0245                                "to add entry to pipeline: "
0246                                "%s (new() returned NULL)\n",
0247                                __func__, elem->name);
0248                         kfree(pipeline_entry);
0249                     }
0250                 }
0251                 found = 1;
0252                 break;
0253             }
0254 
0255         if (found)
0256             found = 0;
0257         else
0258             printk(KERN_ERR "%s: element not found, skipping: "
0259                    "%s\n", __func__, name);
0260     }
0261 
0262 _out:
0263     if (!list_empty(&pipeline->list))
0264         pipeline->inuse = 1;
0265     else
0266         pipeline->inuse = 0;
0267 
0268     kfree(dup);
0269     return 0;
0270 }
0271 
0272 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
0273 {
0274     struct dsp_pipeline_entry *entry;
0275 
0276     if (!pipeline)
0277         return;
0278 
0279     list_for_each_entry(entry, &pipeline->list, list)
0280         if (entry->elem->process_tx)
0281             entry->elem->process_tx(entry->p, data, len);
0282 }
0283 
0284 void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len,
0285                  unsigned int txlen)
0286 {
0287     struct dsp_pipeline_entry *entry;
0288 
0289     if (!pipeline)
0290         return;
0291 
0292     list_for_each_entry_reverse(entry, &pipeline->list, list)
0293         if (entry->elem->process_rx)
0294             entry->elem->process_rx(entry->p, data, len, txlen);
0295 }