Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the Intel Corporation nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 29 | */ |
| 30 | |
| 31 | #include <stdint.h> |
| 32 | #include <stddef.h> |
| 33 | #include <errno.h> |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 34 | #include <sof/sof.h> |
| 35 | #include <sof/lock.h> |
| 36 | #include <sof/list.h> |
| 37 | #include <sof/stream.h> |
| 38 | #include <sof/alloc.h> |
| 39 | #include <sof/audio/component.h> |
| 40 | #include <sof/audio/pipeline.h> |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 41 | #include <uapi/ipc.h> |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 42 | |
| 43 | struct comp_data { |
| 44 | struct list_item list; /* list of components */ |
| 45 | spinlock_t lock; |
| 46 | }; |
| 47 | |
| 48 | static struct comp_data *cd; |
| 49 | |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 50 | static struct comp_driver *get_drv(uint32_t type) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 51 | { |
| 52 | struct list_item *clist; |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 53 | struct comp_driver *drv = NULL; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 54 | |
| 55 | spin_lock(&cd->lock); |
| 56 | |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 57 | /* search driver list for driver type */ |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 58 | list_for_item(clist, &cd->list) { |
| 59 | |
| 60 | drv = container_of(clist, struct comp_driver, list); |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 61 | if (drv->type == type) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 62 | goto out; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 65 | /* not found */ |
| 66 | drv = NULL; |
| 67 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 68 | out: |
| 69 | spin_unlock(&cd->lock); |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 70 | return drv; |
| 71 | } |
| 72 | |
| 73 | struct comp_dev *comp_new(struct sof_ipc_comp *comp) |
| 74 | { |
| 75 | struct comp_dev *cdev; |
| 76 | struct comp_driver *drv; |
| 77 | |
| 78 | /* find the driver for our new component */ |
| 79 | drv = get_drv(comp->type); |
| 80 | if (drv == NULL) { |
| 81 | trace_comp_error("eCD"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 82 | trace_error_value(comp->type); |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | /* create the new component */ |
| 87 | cdev = drv->ops.new(comp); |
| 88 | if (cdev == NULL) { |
| 89 | trace_comp_error("eCN"); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | /* init component */ |
| 94 | memcpy(&cdev->comp, comp, sizeof(*comp)); |
| 95 | cdev->drv = drv; |
Liam Girdwood | 425aa5e | 2017-06-06 20:34:10 +0100 | [diff] [blame] | 96 | spinlock_init(&cdev->lock); |
| 97 | list_init(&cdev->bsource_list); |
| 98 | list_init(&cdev->bsink_list); |
| 99 | |
| 100 | return cdev; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int comp_register(struct comp_driver *drv) |
| 104 | { |
| 105 | spin_lock(&cd->lock); |
| 106 | list_item_prepend(&drv->list, &cd->list); |
| 107 | spin_unlock(&cd->lock); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void comp_unregister(struct comp_driver *drv) |
| 113 | { |
| 114 | spin_lock(&cd->lock); |
| 115 | list_item_del(&drv->list); |
| 116 | spin_unlock(&cd->lock); |
| 117 | } |
| 118 | |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 119 | int comp_set_state(struct comp_dev *dev, int cmd) |
| 120 | { |
| 121 | int ret = 0; |
| 122 | |
| 123 | switch (cmd) { |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 124 | case COMP_TRIGGER_START: |
Wu Zhigang | 5198517 | 2018-07-19 09:22:12 +0800 | [diff] [blame] | 125 | if (dev->state == COMP_STATE_PREPARE) { |
Liam Girdwood | e7b2192 | 2017-09-20 23:29:35 +0100 | [diff] [blame] | 126 | dev->state = COMP_STATE_ACTIVE; |
| 127 | } else { |
| 128 | trace_comp_error("CES"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 129 | trace_error_value(dev->state); |
Liam Girdwood | e7b2192 | 2017-09-20 23:29:35 +0100 | [diff] [blame] | 130 | ret = -EINVAL; |
| 131 | } |
| 132 | break; |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 133 | case COMP_TRIGGER_RELEASE: |
Liam Girdwood | e7b2192 | 2017-09-20 23:29:35 +0100 | [diff] [blame] | 134 | if (dev->state == COMP_STATE_PAUSED) { |
| 135 | dev->state = COMP_STATE_ACTIVE; |
| 136 | } else { |
| 137 | trace_comp_error("CEr"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 138 | trace_error_value(dev->state); |
Liam Girdwood | e7b2192 | 2017-09-20 23:29:35 +0100 | [diff] [blame] | 139 | ret = -EINVAL; |
| 140 | } |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 141 | break; |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 142 | case COMP_TRIGGER_STOP: |
| 143 | case COMP_TRIGGER_XRUN: |
Wu Zhigang | 5198517 | 2018-07-19 09:22:12 +0800 | [diff] [blame] | 144 | if (dev->state == COMP_STATE_ACTIVE || |
| 145 | dev->state == COMP_STATE_PAUSED) { |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 146 | dev->state = COMP_STATE_PREPARE; |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 147 | } else { |
| 148 | trace_comp_error("CEs"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 149 | trace_error_value(dev->state); |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 150 | ret = -EINVAL; |
| 151 | } |
| 152 | break; |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 153 | case COMP_TRIGGER_PAUSE: |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 154 | /* only support pausing for running */ |
Liam Girdwood | 4ad0a66 | 2017-09-20 12:21:53 +0100 | [diff] [blame] | 155 | if (dev->state == COMP_STATE_ACTIVE) |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 156 | dev->state = COMP_STATE_PAUSED; |
| 157 | else { |
| 158 | trace_comp_error("CEp"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 159 | trace_error_value(dev->state); |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 160 | ret = -EINVAL; |
| 161 | } |
| 162 | break; |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 163 | case COMP_TRIGGER_RESET: |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 164 | /* reset always succeeds */ |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 165 | if (dev->state == COMP_STATE_ACTIVE || |
| 166 | dev->state == COMP_STATE_PAUSED) { |
| 167 | trace_comp_error("CER"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 168 | trace_error_value(dev->state); |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 169 | ret = 0; |
| 170 | } |
Wu Zhigang | 563ab55 | 2018-07-18 16:42:00 +0800 | [diff] [blame] | 171 | dev->state = COMP_STATE_READY; |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 172 | break; |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 173 | case COMP_TRIGGER_PREPARE: |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 174 | if (dev->state == COMP_STATE_PREPARE || |
| 175 | dev->state == COMP_STATE_READY) { |
| 176 | dev->state = COMP_STATE_PREPARE; |
| 177 | } else { |
| 178 | trace_comp_error("CEP"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 179 | trace_error_value(dev->state); |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 180 | ret = -EINVAL; |
| 181 | } |
| 182 | break; |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 183 | default: |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 190 | void sys_comp_init(void) |
| 191 | { |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 192 | cd = rzalloc(RZONE_SYS, SOF_MEM_CAPS_RAM, sizeof(*cd)); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 193 | list_init(&cd->list); |
| 194 | spinlock_init(&cd->lock); |
| 195 | } |