13af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
23af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Use of this source code is governed by a BSD-style license that can be
33af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * found in the LICENSE file.
43af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
53af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
63af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/*
73af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * IO list manages the list of inputs and outputs available.
83af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
93af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid#ifndef CRAS_IODEV_LIST_H_
103af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid#define CRAS_IODEV_LIST_H_
113af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
123af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid#include <stdint.h>
133af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
14ce895e7bb75a63c593bec7a5a36bfed3241ab829Dylan Reid#include "cras_types.h"
15ce895e7bb75a63c593bec7a5a36bfed3241ab829Dylan Reid
163af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidstruct cras_iodev;
173af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidstruct cras_iodev_info;
18f1a7e0c094f29c16f6aa9f97d328a93769e69e5cChih-Chung Changstruct cras_ionode;
193af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidstruct cras_rclient;
203af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidstruct cras_rstream;
213af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidstruct cras_audio_format;
22a7978895f3f660c5691887f83fda26169013247dDylan Reidstruct stream_list;
233af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
242ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen/* Device enabled/disabled callback.
252ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen * enabled=1 when a device is enabled, enabled=0 when a device is disabled.
262ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen */
272ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chentypedef void (*device_enabled_callback_t)(struct cras_iodev *dev, int enabled,
282ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen					  void *cb_data);
292ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen
30e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reid/* Initialize the list of iodevs. */
31e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reidvoid cras_iodev_list_init();
32e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reid
33e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reid/* Clean up any resources used by iodev. */
34e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reidvoid cras_iodev_list_deinit();
35e28134b543e2ed27c39a505d0f9e8fd76f916875Dylan Reid
363af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/* Adds an output to the output list.
373af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
38ece5813aeae7b1779d00292ec7ecc404e9396811Dylan Reid *    output - the output to add.
393af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
403af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    0 on success, negative error on failure.
413af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
42f0b5ce85d54d7d655ac7b8694a5635ecab365d55Dylan Reidint cras_iodev_list_add_output(struct cras_iodev *output);
433af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
443af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/* Adds an input to the input list.
453af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
463af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    input - the input to add.
473af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
483af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    0 on success, negative error on failure.
493af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
50f0b5ce85d54d7d655ac7b8694a5635ecab365d55Dylan Reidint cras_iodev_list_add_input(struct cras_iodev *input);
513af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
523af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/* Removes an output from the output list.
533af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
543af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    output - the output to remove.
553af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
563af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    0 on success, negative error on failure.
573af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
583af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidint cras_iodev_list_rm_output(struct cras_iodev *output);
593af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
603af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid/* Removes an input from the input list.
613af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
623af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    output - the input to remove.
633af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
643af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    0 on success, negative error on failure.
653af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
663af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidint cras_iodev_list_rm_input(struct cras_iodev *input);
673af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
68a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid/* Gets a list of outputs. Callee must free the list when finished.  If list_out
69a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid * is NULL, this function can be used to return the number of outputs.
703af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
713af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    list_out - This will be set to the malloc'd area containing the list of
72a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid *        devices.  Ignored if NULL.
733af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
743af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    The number of devices on the list.
753af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
763af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidint cras_iodev_list_get_outputs(struct cras_iodev_info **list_out);
773af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
78a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid/* Gets a list of inputs. Callee must free the list when finished.  If list_out
79a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid * is NULL, this function can be used to return the number of inputs.
803af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Args:
813af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    list_out - This will be set to the malloc'd area containing the list of
82a3bafb94aa13ba1445140b22611104fadb655629Dylan Reid *        devices.  Ignored if NULL.
833af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid * Returns:
843af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid *    The number of devices on the list.
853af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid */
863af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reidint cras_iodev_list_get_inputs(struct cras_iodev_info **list_out);
873af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid
88364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen/* Returns the first enabled device.
89364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen * Args:
90364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen *    direction - Playback or capture.
91364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen * Returns:
92364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen *    Pointer to the first enabled device of direction.
93364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen */
94364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chenstruct cras_iodev *cras_iodev_list_get_first_enabled_iodev(
95364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen	enum CRAS_STREAM_DIRECTION direction);
96364fa0c086e209b9f1e6a7dce050c132a5c1b75aChinyue Chen
979827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang/* Returns the active node id.
989827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang * Args:
999827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang *    direction - Playback or capture.
1009827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang * Returns:
1019827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang *    The id of the active node.
1029827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang */
1039827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Changcras_node_id_t cras_iodev_list_get_active_node_id(
1049827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang	enum CRAS_STREAM_DIRECTION direction);
1059827d4b8ffd6483687f7760db4a235477fd90cedChih-Chung Chang
106b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang/* Stores the following data to the shared memory server state region:
107b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang * (1) device list
108b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang * (2) node list
109b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang * (3) selected nodes
110b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang */
11104cf46dbfbbf557847b462e4bb0e2b42402bd3cbChih-Chung Changvoid cras_iodev_list_update_device_list();
1122c78ba503e3c97f90d4e8b0171c8e3ac50eada42Dylan Reid
113b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang/* Stores the node list in the shared memory server state region. */
114b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Changvoid cras_iodev_list_update_node_list();
115b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang
1168bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao/* Gets the supported hotword models of an ionode. Caller should free
1178bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao * the returned string after use. */
1188bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chaochar *cras_iodev_list_get_hotword_models(cras_node_id_t node_id);
1198bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao
1208bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao/* Sets the desired hotword model to an ionode. */
1218bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chaoint cras_iodev_list_set_hotword_model(cras_node_id_t id,
1228bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao				      const char *model_name);
1238bc976545c90c84ae43429a6041caf950dcd2feeHsin-Yu Chao
124b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang/* Notify that nodes are added/removed. */
125b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Changvoid cras_iodev_list_notify_nodes_changed();
126b2ad33424e21106930eb23db093746dc30926f27Chih-Chung Chang
127dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reid/* Notify that active node is changed for the given direction.
128dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reid * Args:
129dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reid *    direction - Direction of the node.
130dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reid */
131dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reidvoid cras_iodev_list_notify_active_node_changed(
132dc115f02e335a6d197b6ef4bf0516e4866a7fc6eDylan Reid		enum CRAS_STREAM_DIRECTION direction);
133c732e27a905ddce9ccb4507bec7431b6406fdda4Chih-Chung Chang
134d59b94ce647077ea9a4bb1c3593d32ac5ed02e22Chih-Chung Chang/* Sets an attribute of an ionode on a device.
135741ad2848629417c45ce2064b77cde0e26538f04Chih-Chung Chang * Args:
136770ea30175b730e5b771eeae808bbf959e132c3eChih-Chung Chang *    id - the id of the ionode.
137741ad2848629417c45ce2064b77cde0e26538f04Chih-Chung Chang *    node_index - Index of the ionode on the device.
138d59b94ce647077ea9a4bb1c3593d32ac5ed02e22Chih-Chung Chang *    attr - the attribute we want to change.
139d59b94ce647077ea9a4bb1c3593d32ac5ed02e22Chih-Chung Chang *    value - the value we want to set.
140741ad2848629417c45ce2064b77cde0e26538f04Chih-Chung Chang */
141770ea30175b730e5b771eeae808bbf959e132c3eChih-Chung Changint cras_iodev_list_set_node_attr(cras_node_id_t id,
142d59b94ce647077ea9a4bb1c3593d32ac5ed02e22Chih-Chung Chang				  enum ionode_attr attr, int value);
143741ad2848629417c45ce2064b77cde0e26538f04Chih-Chung Chang
144ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang/* Select a node as the preferred node.
1456f0a5e6a967558105f37513801af573167890f67Chih-Chung Chang * Args:
1466f0a5e6a967558105f37513801af573167890f67Chih-Chung Chang *    direction - Playback or capture.
147ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang *    node_id - the id of the ionode to be selected. As a special case, if
148ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang *        node_id is 0, don't select any node in this direction.
1496f0a5e6a967558105f37513801af573167890f67Chih-Chung Chang */
150ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Changvoid cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,
151ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang				 cras_node_id_t node_id);
152ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang
153347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao/* Checks if an iodev is enabled. */
154ab692f0b051c25a94e30d25c906df321b3fc443dCheng-Yi Chiangint cras_iodev_list_dev_is_enabled(const struct cras_iodev *dev);
155347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao
156347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao/* Enables an iodev. If the fallback device was already enabled, this
157347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao * call will disable it. */
158347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chaovoid cras_iodev_list_enable_dev(struct cras_iodev *dev);
159347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao
160347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao/* Disables an iodev. If this is the last device to disable, the
161347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao * fallback devices will be enabled accordingly. */
162347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chaovoid cras_iodev_list_disable_dev(struct cras_iodev *dev);
163347db4998669c852ef99e7ac7cfbe3ff85dcd7f5Hsin-Yu Chao
164aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao/* Adds a node to the active devices list.
165aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao * Args:
166ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chao *    direction - Playback or capture.
167aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao *    node_id - The id of the ionode to be added.
168aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao */
169ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chaovoid cras_iodev_list_add_active_node(enum CRAS_STREAM_DIRECTION direction,
170ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chao				     cras_node_id_t node_id);
171aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao
172aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao/* Removes a node from the active devices list.
173aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao * Args:
174ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chao *    direction - Playback or capture.
175aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao *    node_id - The id of the ionode to be removed.
176aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao */
177ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chaovoid cras_iodev_list_rm_active_node(enum CRAS_STREAM_DIRECTION direction,
178ddab985b9d96e8a3b386079c620422c1aab557a9Hsin-Yu Chao				    cras_node_id_t node_id);
179aa7392943403d106e899be550e52cc2a0053642fHsin-Yu Chao
180ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Chang/* Returns 1 if the node is selected, 0 otherwise. */
181ea1b78a018d72fd22f5ce5650121f5e4b8325a1dChih-Chung Changint cras_iodev_list_node_selected(struct cras_ionode *node);
1826f0a5e6a967558105f37513801af573167890f67Chih-Chung Chang
183b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reid/* Notify the current volume of the given node. */
184b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reidvoid cras_iodev_list_notify_node_volume(struct cras_ionode *node);
185b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reid
186b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reid/* Notify the current capture gain of the given node. */
187b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reidvoid cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node);
188b6670c0295704e171c518e7ae9fd9a2c6f88f535Dylan Reid
1893ac00e542de73e404423161a412b04b64fe7d847Cheng-Yi Chiang/* Notify the current left right channel swapping state of the given node. */
1903ac00e542de73e404423161a412b04b64fe7d847Cheng-Yi Chiangvoid cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node);
1913ac00e542de73e404423161a412b04b64fe7d847Cheng-Yi Chiang
192cc13562bff05f6b254983c450a32a34090ee3a7fDylan Reid/* Handles the adding and removing of test iodevs. */
193cc13562bff05f6b254983c450a32a34090ee3a7fDylan Reidvoid cras_iodev_list_add_test_dev(enum TEST_IODEV_TYPE type);
1943ac00e542de73e404423161a412b04b64fe7d847Cheng-Yi Chiang
195a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reid/* Handles sending a command to a test iodev. */
196a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reidvoid cras_iodev_list_test_dev_command(unsigned int iodev_idx,
197a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reid				      enum CRAS_TEST_IODEV_CMD command,
198a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reid				      unsigned int data_len,
199a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reid				      const uint8_t *data);
200a37abd9b84aefd5758d18579a930c9b38b5ad65eDylan Reid
2010e5fc2e0264e7adf3ab4f39d6be156fc53114544Dylan Reid/* Gets the audio thread used by the devices. */
2020e5fc2e0264e7adf3ab4f39d6be156fc53114544Dylan Reidstruct audio_thread *cras_iodev_list_get_audio_thread();
2030e5fc2e0264e7adf3ab4f39d6be156fc53114544Dylan Reid
204a7978895f3f660c5691887f83fda26169013247dDylan Reid/* Gets the list of all active audio streams attached to devices. */
205a7978895f3f660c5691887f83fda26169013247dDylan Reidstruct stream_list *cras_iodev_list_get_stream_list();
206a7978895f3f660c5691887f83fda26169013247dDylan Reid
2072ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen/* Sets the function to call when a device is enabled or disabled. */
2082ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chenint cras_iodev_list_set_device_enabled_callback(
2092ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen		device_enabled_callback_t device_enabled_cb, void *cb_data);
2102ad34b0e8483eadf4c6140329cf3e80c4c50961eChinyue Chen
2118392426821216de7fd739913c229382c92b6fde6Dylan Reid/* For unit test only. */
2128392426821216de7fd739913c229382c92b6fde6Dylan Reidvoid cras_iodev_list_reset();
2138392426821216de7fd739913c229382c92b6fde6Dylan Reid
2143af1108724ed69d138d2c3399cd95e0e0e7e382aDylan Reid#endif /* CRAS_IODEV_LIST_H_ */
215