Mark Greer | 184992e | 2016-01-13 14:07:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Greybus Audio Device Class Protocol helpers |
| 3 | * |
| 4 | * Copyright 2015-2016 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #include "greybus.h" |
| 10 | #include "greybus_protocols.h" |
| 11 | #include "operation.h" |
Viresh Kumar | 1045451 | 2016-01-28 15:50:47 +0530 | [diff] [blame] | 12 | #include "audio_codec.h" |
Mark Greer | 184992e | 2016-01-13 14:07:46 -0700 | [diff] [blame] | 13 | |
| 14 | /* TODO: Split into separate calls */ |
| 15 | int gb_audio_gb_get_topology(struct gb_connection *connection, |
| 16 | struct gb_audio_topology **topology) |
| 17 | { |
| 18 | struct gb_audio_get_topology_size_response size_resp; |
| 19 | struct gb_audio_topology *topo; |
| 20 | uint16_t size; |
| 21 | int ret; |
| 22 | |
| 23 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_TOPOLOGY_SIZE, |
| 24 | NULL, 0, &size_resp, sizeof(size_resp)); |
| 25 | if (ret) |
| 26 | return ret; |
| 27 | |
| 28 | size = le16_to_cpu(size_resp.size); |
| 29 | if (size < sizeof(*topo)) |
| 30 | return -ENODATA; |
| 31 | |
| 32 | topo = kzalloc(size, GFP_KERNEL); |
| 33 | if (!topo) |
| 34 | return -ENOMEM; |
| 35 | |
| 36 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_TOPOLOGY, NULL, 0, |
| 37 | topo, size); |
| 38 | if (ret) { |
| 39 | kfree(topo); |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | *topology = topo; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(gb_audio_gb_get_topology); |
| 48 | |
| 49 | int gb_audio_gb_get_control(struct gb_connection *connection, |
| 50 | uint8_t control_id, uint8_t index, |
| 51 | struct gb_audio_ctl_elem_value *value) |
| 52 | { |
| 53 | struct gb_audio_get_control_request req; |
| 54 | struct gb_audio_get_control_response resp; |
| 55 | int ret; |
| 56 | |
| 57 | req.control_id = control_id; |
| 58 | req.index = index; |
| 59 | |
| 60 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_CONTROL, |
| 61 | &req, sizeof(req), &resp, sizeof(resp)); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | |
| 65 | memcpy(value, &resp.value, sizeof(*value)); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(gb_audio_gb_get_control); |
| 70 | |
| 71 | int gb_audio_gb_set_control(struct gb_connection *connection, |
| 72 | uint8_t control_id, uint8_t index, |
| 73 | struct gb_audio_ctl_elem_value *value) |
| 74 | { |
| 75 | struct gb_audio_set_control_request req; |
| 76 | |
| 77 | req.control_id = control_id; |
| 78 | req.index = index; |
| 79 | memcpy(&req.value, value, sizeof(req.value)); |
| 80 | |
| 81 | return gb_operation_sync(connection, GB_AUDIO_TYPE_SET_CONTROL, |
| 82 | &req, sizeof(req), NULL, 0); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(gb_audio_gb_set_control); |
| 85 | |
| 86 | int gb_audio_gb_enable_widget(struct gb_connection *connection, |
| 87 | uint8_t widget_id) |
| 88 | { |
| 89 | struct gb_audio_enable_widget_request req; |
| 90 | |
| 91 | req.widget_id = widget_id; |
| 92 | |
| 93 | return gb_operation_sync(connection, GB_AUDIO_TYPE_ENABLE_WIDGET, |
| 94 | &req, sizeof(req), NULL, 0); |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(gb_audio_gb_enable_widget); |
| 97 | |
| 98 | int gb_audio_gb_disable_widget(struct gb_connection *connection, |
| 99 | uint8_t widget_id) |
| 100 | { |
| 101 | struct gb_audio_disable_widget_request req; |
| 102 | |
| 103 | req.widget_id = widget_id; |
| 104 | |
| 105 | return gb_operation_sync(connection, GB_AUDIO_TYPE_DISABLE_WIDGET, |
| 106 | &req, sizeof(req), NULL, 0); |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(gb_audio_gb_disable_widget); |
| 109 | |
| 110 | int gb_audio_gb_get_pcm(struct gb_connection *connection, uint16_t data_cport, |
| 111 | uint32_t *format, uint32_t *rate, uint8_t *channels, |
| 112 | uint8_t *sig_bits) |
| 113 | { |
| 114 | struct gb_audio_get_pcm_request req; |
| 115 | struct gb_audio_get_pcm_response resp; |
| 116 | int ret; |
| 117 | |
| 118 | req.data_cport = cpu_to_le16(data_cport); |
| 119 | |
| 120 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_PCM, |
| 121 | &req, sizeof(req), &resp, sizeof(resp)); |
| 122 | if (ret) |
| 123 | return ret; |
| 124 | |
| 125 | *format = le32_to_cpu(resp.format); |
| 126 | *rate = le32_to_cpu(resp.rate); |
| 127 | *channels = resp.channels; |
| 128 | *sig_bits = resp.sig_bits; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(gb_audio_gb_get_pcm); |
| 133 | |
| 134 | int gb_audio_gb_set_pcm(struct gb_connection *connection, uint16_t data_cport, |
| 135 | uint32_t format, uint32_t rate, uint8_t channels, |
| 136 | uint8_t sig_bits) |
| 137 | { |
| 138 | struct gb_audio_set_pcm_request req; |
| 139 | |
| 140 | req.data_cport = cpu_to_le16(data_cport); |
| 141 | req.format = cpu_to_le32(format); |
| 142 | req.rate = cpu_to_le32(rate); |
| 143 | req.channels = channels; |
| 144 | req.sig_bits = sig_bits; |
| 145 | |
| 146 | return gb_operation_sync(connection, GB_AUDIO_TYPE_SET_PCM, |
| 147 | &req, sizeof(req), NULL, 0); |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(gb_audio_gb_set_pcm); |
| 150 | |
| 151 | int gb_audio_gb_set_tx_data_size(struct gb_connection *connection, |
| 152 | uint16_t data_cport, uint16_t size) |
| 153 | { |
| 154 | struct gb_audio_set_tx_data_size_request req; |
| 155 | |
| 156 | req.data_cport = cpu_to_le16(data_cport); |
| 157 | req.size = cpu_to_le16(size); |
| 158 | |
| 159 | return gb_operation_sync(connection, GB_AUDIO_TYPE_SET_TX_DATA_SIZE, |
| 160 | &req, sizeof(req), NULL, 0); |
| 161 | } |
| 162 | EXPORT_SYMBOL_GPL(gb_audio_gb_set_tx_data_size); |
| 163 | |
| 164 | int gb_audio_gb_get_tx_delay(struct gb_connection *connection, |
| 165 | uint16_t data_cport, uint32_t *delay) |
| 166 | { |
| 167 | struct gb_audio_get_tx_delay_request req; |
| 168 | struct gb_audio_get_tx_delay_response resp; |
| 169 | int ret; |
| 170 | |
| 171 | req.data_cport = cpu_to_le16(data_cport); |
| 172 | |
| 173 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_TX_DELAY, |
| 174 | &req, sizeof(req), &resp, sizeof(resp)); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
| 178 | *delay = le32_to_cpu(resp.delay); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(gb_audio_gb_get_tx_delay); |
| 183 | |
| 184 | int gb_audio_gb_activate_tx(struct gb_connection *connection, |
| 185 | uint16_t data_cport) |
| 186 | { |
| 187 | struct gb_audio_activate_tx_request req; |
| 188 | |
| 189 | req.data_cport = cpu_to_le16(data_cport); |
| 190 | |
| 191 | return gb_operation_sync(connection, GB_AUDIO_TYPE_ACTIVATE_TX, |
| 192 | &req, sizeof(req), NULL, 0); |
| 193 | } |
| 194 | EXPORT_SYMBOL_GPL(gb_audio_gb_activate_tx); |
| 195 | |
| 196 | int gb_audio_gb_deactivate_tx(struct gb_connection *connection, |
| 197 | uint16_t data_cport) |
| 198 | { |
| 199 | struct gb_audio_deactivate_tx_request req; |
| 200 | |
| 201 | req.data_cport = cpu_to_le16(data_cport); |
| 202 | |
| 203 | return gb_operation_sync(connection, GB_AUDIO_TYPE_DEACTIVATE_TX, |
| 204 | &req, sizeof(req), NULL, 0); |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(gb_audio_gb_deactivate_tx); |
| 207 | |
| 208 | int gb_audio_gb_set_rx_data_size(struct gb_connection *connection, |
| 209 | uint16_t data_cport, uint16_t size) |
| 210 | { |
| 211 | struct gb_audio_set_rx_data_size_request req; |
| 212 | |
| 213 | req.data_cport = cpu_to_le16(data_cport); |
| 214 | req.size = cpu_to_le16(size); |
| 215 | |
| 216 | return gb_operation_sync(connection, GB_AUDIO_TYPE_SET_RX_DATA_SIZE, |
| 217 | &req, sizeof(req), NULL, 0); |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(gb_audio_gb_set_rx_data_size); |
| 220 | |
| 221 | int gb_audio_gb_get_rx_delay(struct gb_connection *connection, |
| 222 | uint16_t data_cport, uint32_t *delay) |
| 223 | { |
| 224 | struct gb_audio_get_rx_delay_request req; |
| 225 | struct gb_audio_get_rx_delay_response resp; |
| 226 | int ret; |
| 227 | |
| 228 | req.data_cport = cpu_to_le16(data_cport); |
| 229 | |
| 230 | ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_RX_DELAY, |
| 231 | &req, sizeof(req), &resp, sizeof(resp)); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | *delay = le32_to_cpu(resp.delay); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(gb_audio_gb_get_rx_delay); |
| 240 | |
| 241 | int gb_audio_gb_activate_rx(struct gb_connection *connection, |
| 242 | uint16_t data_cport) |
| 243 | { |
| 244 | struct gb_audio_activate_rx_request req; |
| 245 | |
| 246 | req.data_cport = cpu_to_le16(data_cport); |
| 247 | |
| 248 | return gb_operation_sync(connection, GB_AUDIO_TYPE_ACTIVATE_RX, |
| 249 | &req, sizeof(req), NULL, 0); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(gb_audio_gb_activate_rx); |
| 252 | |
| 253 | int gb_audio_gb_deactivate_rx(struct gb_connection *connection, |
| 254 | uint16_t data_cport) |
| 255 | { |
| 256 | struct gb_audio_deactivate_rx_request req; |
| 257 | |
| 258 | req.data_cport = cpu_to_le16(data_cport); |
| 259 | |
| 260 | return gb_operation_sync(connection, GB_AUDIO_TYPE_DEACTIVATE_RX, |
| 261 | &req, sizeof(req), NULL, 0); |
| 262 | } |
| 263 | EXPORT_SYMBOL_GPL(gb_audio_gb_deactivate_rx); |
| 264 | |
| 265 | MODULE_LICENSE("GPL v2"); |
| 266 | MODULE_ALIAS("greybus:audio-gb"); |
| 267 | MODULE_DESCRIPTION("Greybus Audio Device Class Protocol library"); |
| 268 | MODULE_AUTHOR("Mark Greer <mgreer@animalcreek.com>"); |