Home | History | Annotate | Download | only in server
      1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #include <alsa/asoundlib.h>
      7 #include <linux/input.h>
      8 #include <regex.h>
      9 #include <syslog.h>
     10 
     11 #include "cras_alsa_jack.h"
     12 #include "cras_alsa_mixer.h"
     13 #include "cras_alsa_ucm.h"
     14 #include "cras_system_state.h"
     15 #include "cras_gpio_jack.h"
     16 #include "cras_tm.h"
     17 #include "cras_util.h"
     18 #include "edid_utils.h"
     19 #include "utlist.h"
     20 
     21 static const unsigned int DISPLAY_INFO_RETRY_DELAY_MS = 200;
     22 static const unsigned int DISPLAY_INFO_MAX_RETRIES = 10;
     23 static const unsigned int DISPLAY_INFO_GPIO_MAX_RETRIES = 25;
     24 
     25 /* Constants used to retrieve monitor name from ELD buffer. */
     26 static const unsigned int ELD_MNL_MASK = 31;
     27 static const unsigned int ELD_MNL_OFFSET = 4;
     28 static const unsigned int ELD_MONITOR_NAME_OFFSET = 20;
     29 
     30 /* Keeps an fd that is registered with system settings.  A list of fds must be
     31  * kept so that they can be removed when the jack list is destroyed. */
     32 struct jack_poll_fd {
     33 	int fd;
     34 	struct jack_poll_fd *prev, *next;
     35 };
     36 
     37 /* cras_gpio_jack:  Describes headphone & microphone jack connected to GPIO
     38  *
     39  *   On Arm-based systems, the headphone & microphone jacks are
     40  *   connected to GPIOs which are plumbed through the /dev/input/event
     41  *   system.  For these jacks, the software is written to open the
     42  *   corresponding /dev/input/event file and monitor it for 'insert' &
     43  *   'remove' activity.
     44  *
     45  *   fd           : File descriptor corresponding to the /dev/input/event file.
     46  *
     47  *   switch_event : Indicates the type of the /dev/input/event file.
     48  *                  Either SW_HEADPHONE_INSERT, or SW_MICROPHONE_INSERT.
     49  *
     50  *   current_state: 0 -> device not plugged in
     51  *                  1 -> device plugged in
     52  *   device_name  : Device name extracted from /dev/input/event[0..9]+.
     53  *                  Allocated on heap; must free.
     54  */
     55 struct cras_gpio_jack {
     56 	int fd;
     57 	unsigned switch_event;
     58 	unsigned current_state;
     59 	char *device_name;
     60 };
     61 
     62 /* Represents a single alsa Jack, e.g. "Headphone Jack" or "Mic Jack".
     63  *    is_gpio: 1 -> gpio switch (union field: gpio)
     64  *             0 -> Alsa 'jack' (union field: elem)
     65  *    elem - alsa hcontrol element for this jack, when is_gpio == 0.
     66  *    gpio - description of gpio-based jack, when is_gpio != 0.
     67  *    eld_control - mixer control for ELD info buffer.
     68  *    jack_list - list of jacks this belongs to.
     69  *    mixer_output - mixer output control used to control audio to this jack.
     70  *        This will be null for input jacks.
     71  *    mixer_input - mixer input control used to control audio to this jack.
     72  *        This will be null for output jacks.
     73  *    ucm_device - Name of the ucm device if found, otherwise, NULL.
     74  *    edid_file - File to read the EDID from (if available, HDMI only).
     75  *    display_info_timer - Timer used to poll display info for HDMI jacks.
     76  *    display_info_retries - Number of times to retry reading display info.
     77  */
     78 struct cras_alsa_jack {
     79 	unsigned is_gpio;	/* !0 -> 'gpio' valid
     80 				 *  0 -> 'elem' valid
     81 				 */
     82 	union {
     83 		snd_hctl_elem_t *elem;
     84 		struct cras_gpio_jack gpio;
     85 	};
     86 
     87 	snd_hctl_elem_t *eld_control;
     88 	struct cras_alsa_jack_list *jack_list;
     89 	struct mixer_control *mixer_output;
     90 	struct mixer_control *mixer_input;
     91 	char *ucm_device;
     92 	const char *dsp_name;
     93 	const char* override_type_name;
     94 	const char *edid_file;
     95 	struct cras_timer *display_info_timer;
     96 	unsigned int display_info_retries;
     97 	struct cras_alsa_jack *prev, *next;
     98 };
     99 
    100 /* Contains all Jacks for a given device.
    101  *    hctl - alsa hcontrol for this device's card
    102  *         - not opened by the jack list.
    103  *    mixer - cras mixer for the card providing this device.
    104  *    ucm - CRAS use case manager if available.
    105  *    card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
    106  *    card_name - The name of the card.
    107  *    device_index - Index ALSA uses to refer to the device.  The Y in "hw:X,Y".
    108  *    is_first_device - whether this device is the first device on the card.
    109  *    direction - Input or output.
    110  *    change_callback - function to call when the state of a jack changes.
    111  *    callback_data - data to pass back to the callback.
    112  *    jacks - list of jacks for this device.
    113  */
    114 struct cras_alsa_jack_list {
    115 	snd_hctl_t *hctl;
    116 	struct cras_alsa_mixer *mixer;
    117 	struct cras_use_case_mgr *ucm;
    118 	unsigned int card_index;
    119 	const char *card_name;
    120 	size_t device_index;
    121 	int is_first_device;
    122 	enum CRAS_STREAM_DIRECTION direction;
    123 	jack_state_change_callback *change_callback;
    124 	void *callback_data;
    125 	struct cras_alsa_jack *jacks;
    126 };
    127 
    128 /* Used to contain information needed while looking through GPIO jacks.
    129  *    jack_list - The current jack_list.
    130  *    section - An associated UCM section.
    131  *    result_jack - The resulting jack.
    132  *    rc - The return code for the operation.
    133  */
    134 struct gpio_switch_list_data {
    135 	struct cras_alsa_jack_list *jack_list;
    136 	struct ucm_section *section;
    137 	struct cras_alsa_jack *result_jack;
    138 	int rc;
    139 };
    140 
    141 /*
    142  * Local Helpers.
    143  */
    144 
    145 #define BITS_PER_BYTE		(8)
    146 #define BITS_PER_LONG		(sizeof(long) * BITS_PER_BYTE)
    147 #define NBITS(x)		((((x) - 1) / BITS_PER_LONG) + 1)
    148 #define OFF(x)			((x) % BITS_PER_LONG)
    149 #define BIT(x)			(1UL << OFF(x))
    150 #define LONG(x)			((x) / BITS_PER_LONG)
    151 #define IS_BIT_SET(bit, array)	!!((array[LONG(bit)]) & (1UL << OFF(bit)))
    152 
    153 static int sys_input_get_switch_state(int fd, unsigned sw, unsigned *state)
    154 {
    155 	unsigned long bits[NBITS(SW_CNT)];
    156 	const unsigned long switch_no = sw;
    157 
    158 	memset(bits, '\0', sizeof(bits));
    159 	/* If switch event present & supported, get current state. */
    160 	if (gpio_switch_eviocgbit(fd, bits, sizeof(bits)) < 0)
    161 		return -EIO;
    162 
    163 	if (IS_BIT_SET(switch_no, bits))
    164 		if (gpio_switch_eviocgsw(fd, bits, sizeof(bits)) >= 0) {
    165 			*state = IS_BIT_SET(switch_no, bits);
    166 			return 0;
    167 		}
    168 
    169 	return -1;
    170 }
    171 
    172 static inline struct cras_alsa_jack *cras_alloc_jack(int is_gpio)
    173 {
    174 	struct cras_alsa_jack *jack = calloc(1, sizeof(*jack));
    175 	if (jack == NULL)
    176 		return NULL;
    177 	jack->is_gpio = is_gpio;
    178 	return jack;
    179 }
    180 
    181 static void cras_free_jack(struct cras_alsa_jack *jack,
    182 			   int rm_select_fd)
    183 {
    184 	if (!jack)
    185 		return;
    186 
    187 	free(jack->ucm_device);
    188 	free((void *)jack->edid_file);
    189 	if (jack->display_info_timer)
    190 		cras_tm_cancel_timer(cras_system_state_get_tm(),
    191 				     jack->display_info_timer);
    192 
    193 	if (jack->is_gpio) {
    194 		free(jack->gpio.device_name);
    195 		if (jack->gpio.fd >= 0) {
    196 			if (rm_select_fd)
    197 				cras_system_rm_select_fd(jack->gpio.fd);
    198 			close(jack->gpio.fd);
    199 		}
    200 	}
    201 
    202 	/*
    203 	 * Remove the jack callback set on hctl. Otherwise, snd_hctl_close will
    204 	 * trigger a callback while iodev might already be destroyed.
    205 	 */
    206 	if (!jack->is_gpio && jack->elem)
    207 		snd_hctl_elem_set_callback(jack->elem, NULL);
    208 
    209 	free((void *)jack->override_type_name);
    210 	free((void *)jack->dsp_name);
    211 	free(jack);
    212 }
    213 
    214 /* Gets the current plug state of the jack */
    215 static int get_jack_current_state(struct cras_alsa_jack *jack)
    216 {
    217 	snd_ctl_elem_value_t *elem_value;
    218 
    219 	if (jack->is_gpio)
    220 		return jack->gpio.current_state;
    221 
    222 	snd_ctl_elem_value_alloca(&elem_value);
    223 	snd_hctl_elem_read(jack->elem, elem_value);
    224 
    225 	return snd_ctl_elem_value_get_boolean(elem_value, 0);
    226 }
    227 
    228 static int read_jack_edid(const struct cras_alsa_jack *jack, uint8_t *edid)
    229 {
    230 	int fd, nread;
    231 
    232 	fd = open(jack->edid_file, O_RDONLY);
    233 	if (fd < 0)
    234 		return -1;
    235 
    236 	nread = read(fd, edid, EEDID_SIZE);
    237 	close(fd);
    238 
    239 	if (nread < EDID_SIZE || !edid_valid(edid))
    240 		return -1;
    241 	return 0;
    242 }
    243 
    244 static int check_jack_edid(struct cras_alsa_jack *jack)
    245 {
    246 	uint8_t edid[EEDID_SIZE];
    247 
    248 	if (read_jack_edid(jack, edid))
    249 		return -1;
    250 
    251 	/* If the jack supports EDID, check that it supports audio, clearing
    252 	 * the plugged state if it doesn't.
    253 	 */
    254 	if (!edid_lpcm_support(edid, edid[EDID_EXT_FLAG]))
    255 		jack->gpio.current_state = 0;
    256 	return 0;
    257 }
    258 
    259 static int get_jack_edid_monitor_name(const struct cras_alsa_jack *jack,
    260 				      char *buf,
    261 				      unsigned int buf_size)
    262 {
    263 	uint8_t edid[EEDID_SIZE];
    264 
    265 	if (read_jack_edid(jack, edid))
    266 		return -1;
    267 
    268 	return edid_get_monitor_name(edid, buf, buf_size);
    269 }
    270 
    271 /* Checks the ELD control of the jack to see if the ELD buffer
    272  * is ready to read and report the plug status.
    273  */
    274 static int check_jack_eld(struct cras_alsa_jack *jack)
    275 {
    276 	snd_ctl_elem_info_t *elem_info;
    277 	snd_ctl_elem_info_alloca(&elem_info);
    278 
    279 	/* Poll ELD control by getting the count of ELD buffer.
    280 	 * When seeing zero buffer count, retry after a delay until
    281 	 * it's ready or reached the max number of retries. */
    282 	if (snd_hctl_elem_info(jack->eld_control, elem_info) != 0)
    283 		return -1;
    284 	if (snd_ctl_elem_info_get_count(elem_info) == 0)
    285 		return -1;
    286 	return 0;
    287 }
    288 
    289 static void display_info_delay_cb(struct cras_timer *timer, void *arg);
    290 
    291 /* Callback function doing following things:
    292  * 1. Reset timer and update max number of retries.
    293  * 2. Check all conditions to see if it's okay or needed to
    294  *    report jack status directly. E.g. jack is unplugged or
    295  *    EDID is not ready for some reason.
    296  * 3. Check if max number of retries is reached and decide
    297  *    to set timer for next callback or report jack state.
    298  */
    299 static inline void jack_state_change_cb(struct cras_alsa_jack *jack, int retry)
    300 {
    301 	struct cras_tm *tm = cras_system_state_get_tm();
    302 
    303 	if (jack->display_info_timer) {
    304 		cras_tm_cancel_timer(tm, jack->display_info_timer);
    305 		jack->display_info_timer = NULL;
    306 	}
    307 	if (retry) {
    308 		jack->display_info_retries =
    309 				jack->is_gpio ? DISPLAY_INFO_GPIO_MAX_RETRIES
    310 					      : DISPLAY_INFO_MAX_RETRIES;
    311 	}
    312 
    313 	if (!get_jack_current_state(jack))
    314 		goto report_jack_state;
    315 
    316 	/* If there is an edid file, check it.  If it is ready continue, if we
    317 	 * need to try again later, return here as the timer has been armed and
    318 	 * will check again later.
    319 	 */
    320 	if (jack->edid_file == NULL && jack->eld_control == NULL)
    321 		goto report_jack_state;
    322 	if (jack->edid_file && (check_jack_edid(jack) == 0))
    323 		goto report_jack_state;
    324 	if (jack->eld_control && (check_jack_eld(jack) == 0))
    325 		goto report_jack_state;
    326 
    327 	if (--jack->display_info_retries == 0) {
    328 		if (jack->is_gpio)
    329 			jack->gpio.current_state = 0;
    330 		if (jack->edid_file)
    331 			syslog(LOG_ERR, "Timeout to read EDID from %s",
    332 			       jack->edid_file);
    333 		goto report_jack_state;
    334 	}
    335 
    336 	jack->display_info_timer = cras_tm_create_timer(tm,
    337 						DISPLAY_INFO_RETRY_DELAY_MS,
    338 						display_info_delay_cb, jack);
    339 	return;
    340 
    341 report_jack_state:
    342 	jack->jack_list->change_callback(jack,
    343 					 get_jack_current_state(jack),
    344 					 jack->jack_list->callback_data);
    345 }
    346 
    347 /* gpio_switch_initial_state
    348  *
    349  *   Determines the initial state of a gpio-based switch.
    350  */
    351 static void gpio_switch_initial_state(struct cras_alsa_jack *jack)
    352 {
    353 	unsigned v;
    354 	int r = sys_input_get_switch_state(jack->gpio.fd,
    355 					   jack->gpio.switch_event, &v);
    356 	jack->gpio.current_state = r == 0 ? v : 0;
    357 	jack_state_change_cb(jack, 1);
    358 }
    359 
    360 /* Check if the input event is an audio switch event. */
    361 static inline int is_audio_switch_event(const struct input_event *ev,
    362 					int sw_code)
    363 {
    364 	return (ev->type == EV_SW && ev->code == sw_code);
    365 }
    366 
    367 /* Timer callback to read display info after a hotplug event for an HDMI jack.
    368  */
    369 static void display_info_delay_cb(struct cras_timer *timer, void *arg)
    370 {
    371 	struct cras_alsa_jack *jack = (struct cras_alsa_jack *)arg;
    372 
    373 	jack->display_info_timer = NULL;
    374 	jack_state_change_cb(jack, 0);
    375 }
    376 
    377 /* gpio_switch_callback
    378  *
    379  *   This callback is invoked whenever the associated /dev/input/event
    380  *   file has data to read.  Perform autoswitching to / from the
    381  *   associated device when data is available.
    382  */
    383 static void gpio_switch_callback(void *arg)
    384 {
    385 	struct cras_alsa_jack *jack = arg;
    386 	int i;
    387 	int r;
    388 	struct input_event ev[64];
    389 
    390 	r = gpio_switch_read(jack->gpio.fd, ev,
    391 			     ARRAY_SIZE(ev) * sizeof(struct input_event));
    392 	if (r < 0)
    393 		return;
    394 
    395 	for (i = 0; i < r / sizeof(struct input_event); ++i)
    396 		if (is_audio_switch_event(&ev[i], jack->gpio.switch_event)) {
    397 			jack->gpio.current_state = ev[i].value;
    398 
    399 			jack_state_change_cb(jack, 1);
    400 		}
    401 }
    402 
    403 /* Determines if the GPIO jack should be associated with the device of the
    404  * jack list. If the device name is not specified in UCM (common case),
    405  * assume it should be associated with the first input device or the first
    406  * output device on the card.
    407  */
    408 static unsigned int gpio_jack_match_device(const struct cras_alsa_jack *jack,
    409 			struct cras_alsa_jack_list* jack_list,
    410 			const char *card_name,
    411 			enum CRAS_STREAM_DIRECTION direction)
    412 {
    413 	const char* target_device_name = NULL;
    414 	char current_device_name[CRAS_IODEV_NAME_BUFFER_SIZE];
    415 	unsigned int rc;
    416 
    417 	/* If the device name is not specified in UCM, assume it should be
    418 	 * associated with device 0. */
    419 	if (!jack_list->ucm || !jack->ucm_device)
    420 		return jack_list->is_first_device;
    421 
    422 	/* Look for device name specified in a device section of UCM. */
    423 	target_device_name = ucm_get_device_name_for_dev(
    424 				jack_list->ucm, jack->ucm_device, direction);
    425 
    426 	if (!target_device_name)
    427 		return jack_list->is_first_device;
    428 
    429 	syslog(LOG_DEBUG, "Matching GPIO jack, target device name: %s, "
    430 	       "current card name: %s, device index: %zu\n",
    431 		target_device_name, card_name, jack_list->device_index);
    432 
    433 	/* Device name of format "hw:<card_name>,<device_index>", should fit
    434 	 * in the string of size CRAS_IODEV_NAME_BUFFER_SIZE.*/
    435 	snprintf(current_device_name,
    436 		 sizeof(current_device_name),
    437 		 "hw:%s,%zu",
    438 		 card_name,
    439 		 jack_list->device_index);
    440 
    441 	rc = !strcmp(current_device_name, target_device_name);
    442 	free((void*)target_device_name);
    443 	return rc;
    444 }
    445 
    446 static int create_jack_for_gpio(struct cras_alsa_jack_list *jack_list,
    447 				const char *pathname,
    448 				const char *dev_name,
    449 				unsigned switch_event,
    450 				struct cras_alsa_jack **out_jack)
    451 {
    452 	struct cras_alsa_jack *jack;
    453 	unsigned long bits[NBITS(SW_CNT)];
    454 	const char *card_name = jack_list->card_name;
    455 	int r;
    456 
    457 	if (!out_jack)
    458 		return -EINVAL;
    459 	*out_jack = NULL;
    460 
    461 	jack = cras_alloc_jack(1);
    462 	if (jack == NULL)
    463 		return -ENOMEM;
    464 
    465 	jack->gpio.fd = gpio_switch_open(pathname);
    466 	if (jack->gpio.fd == -1) {
    467 		r = -EIO;
    468 		goto error;
    469 	}
    470 
    471 	jack->gpio.switch_event = switch_event;
    472 	jack->jack_list = jack_list;
    473 	jack->gpio.device_name = strdup(dev_name);
    474 	if (!jack->gpio.device_name) {
    475 		r = -ENOMEM;
    476 		goto error;
    477 	}
    478 
    479 	if (!strstr(jack->gpio.device_name, card_name) ||
    480 	    (gpio_switch_eviocgbit(jack->gpio.fd, bits, sizeof(bits)) < 0) ||
    481 	    !IS_BIT_SET(switch_event, bits)) {
    482 		r = -EIO;
    483 		goto error;
    484 	}
    485 
    486 	*out_jack = jack;
    487 	return 0;
    488 
    489 error:
    490 	/* Not yet registered with system select. */
    491 	cras_free_jack(jack, 0);
    492 	return r;
    493 }
    494 
    495 /* Take ownership and finish setup of the jack.
    496  * Add the jack to the jack_list if everything goes well, or destroy it.
    497  */
    498 static int cras_complete_gpio_jack(struct gpio_switch_list_data *data,
    499 				   struct cras_alsa_jack *jack,
    500 				   unsigned switch_event)
    501 {
    502 	struct cras_alsa_jack_list *jack_list = data->jack_list;
    503 	enum CRAS_STREAM_DIRECTION direction = jack_list->direction;
    504 	int r;
    505 
    506 	if (jack->ucm_device) {
    507 		jack->edid_file = ucm_get_edid_file_for_dev(jack_list->ucm,
    508 							    jack->ucm_device);
    509 		jack->dsp_name = ucm_get_dsp_name(
    510 			jack->jack_list->ucm, jack->ucm_device, direction);
    511 	}
    512 
    513 	r = sys_input_get_switch_state(jack->gpio.fd, switch_event,
    514 				       &jack->gpio.current_state);
    515 	if (r < 0) {
    516 		cras_free_jack(jack, 0);
    517 		return -EIO;
    518 	}
    519 	r = cras_system_add_select_fd(jack->gpio.fd,
    520 				      gpio_switch_callback, jack);
    521 	if (r < 0) {
    522 		/* Not yet registered with system select. */
    523 		cras_free_jack(jack, 0);
    524 		return r;
    525 	}
    526 
    527 	DL_APPEND(jack_list->jacks, jack);
    528 	if (!data->result_jack)
    529 		data->result_jack = jack;
    530 	else if (data->section)
    531 		syslog(LOG_ERR,
    532 		       "More than one jack for SectionDevice '%s'.",
    533 		       data->section->name);
    534 	return 0;
    535 }
    536 
    537 /* open_and_monitor_gpio:
    538  *
    539  *   Opens a /dev/input/event file associated with a headphone /
    540  *   microphone jack and watches it for activity.
    541  *   Returns 0 when a jack has been successfully added.
    542  */
    543 static int open_and_monitor_gpio(struct gpio_switch_list_data *data,
    544 				 const char *pathname,
    545 				 const char *dev_name,
    546 				 unsigned switch_event)
    547 {
    548 	struct cras_alsa_jack *jack;
    549 	struct cras_alsa_jack_list *jack_list = data->jack_list;
    550 	const char *card_name = jack_list->card_name;
    551 	enum CRAS_STREAM_DIRECTION direction = jack_list->direction;
    552 	int r;
    553 
    554 	r = create_jack_for_gpio(jack_list, pathname, dev_name,
    555 				 switch_event, &jack);
    556 	if (r != 0)
    557 		return r;
    558 
    559 	if (jack_list->ucm)
    560 		jack->ucm_device =
    561 			ucm_get_dev_for_jack(jack_list->ucm,
    562 					     jack->gpio.device_name,
    563 					     direction);
    564 
    565 	if (!gpio_jack_match_device(jack, jack_list, card_name, direction)) {
    566 		cras_free_jack(jack, 0);
    567 		return -EIO;
    568 	}
    569 
    570 	if (direction == CRAS_STREAM_OUTPUT &&
    571 	    (strstr(jack->gpio.device_name, "Headphone") ||
    572 	     strstr(jack->gpio.device_name, "Headset")))
    573 		jack->mixer_output = cras_alsa_mixer_get_output_matching_name(
    574 			jack_list->mixer,
    575 			"Headphone");
    576 	else if (direction == CRAS_STREAM_OUTPUT &&
    577 		 strstr(jack->gpio.device_name, "HDMI"))
    578 		jack->mixer_output = cras_alsa_mixer_get_output_matching_name(
    579 			jack_list->mixer,
    580 			"HDMI");
    581 
    582 	if (jack->ucm_device && direction == CRAS_STREAM_INPUT) {
    583 		char *control_name;
    584 		control_name = ucm_get_cap_control(jack->jack_list->ucm,
    585 						   jack->ucm_device);
    586 		if (control_name)
    587 			jack->mixer_input =
    588 				cras_alsa_mixer_get_input_matching_name(
    589 					jack_list->mixer,
    590 					control_name);
    591 	}
    592 
    593 	return cras_complete_gpio_jack(data, jack, switch_event);
    594 }
    595 
    596 static int open_and_monitor_gpio_with_section(
    597 			struct gpio_switch_list_data *data,
    598 			const char *pathname,
    599 			unsigned switch_event)
    600 {
    601 	struct cras_alsa_jack *jack;
    602 	struct cras_alsa_jack_list *jack_list = data->jack_list;
    603 	struct ucm_section *section = data->section;
    604 	enum CRAS_STREAM_DIRECTION direction = jack_list->direction;
    605 	int r;
    606 
    607 	r = create_jack_for_gpio(jack_list, pathname, section->jack_name,
    608 				 switch_event, &jack);
    609 	if (r != 0)
    610 		return r;
    611 
    612 	jack->ucm_device = strdup(section->name);
    613 	if (!jack->ucm_device) {
    614 		cras_free_jack(jack, 0);
    615 		return -ENOMEM;
    616 	}
    617 
    618 	if (direction == CRAS_STREAM_OUTPUT)
    619 		jack->mixer_output = cras_alsa_mixer_get_control_for_section(
    620 						jack_list->mixer, section);
    621 	else if (direction == CRAS_STREAM_INPUT)
    622 		jack->mixer_input = cras_alsa_mixer_get_control_for_section(
    623 						jack_list->mixer, section);
    624 
    625 	return cras_complete_gpio_jack(data, jack, switch_event);
    626 }
    627 
    628 /* Monitor GPIO switches for this jack_list.
    629  * Args:
    630  *    data - Data for GPIO switch search.
    631  *    dev_path - Device full path.
    632  *    dev_name - Device name.
    633  * Returns:
    634  *    0 for success, or negative on error. Assumes success if no jack is
    635  *    found, or if the jack could not be accessed.
    636  */
    637 static int gpio_switches_monitor_device(struct gpio_switch_list_data *data,
    638 					const char *dev_path,
    639 					const char *dev_name)
    640 {
    641 	static const int out_switches[] = {SW_HEADPHONE_INSERT,
    642 					   SW_LINEOUT_INSERT};
    643 	static const int in_switches[] = {SW_MICROPHONE_INSERT};
    644 	int sw;
    645 	const int *switches = out_switches;
    646 	int num_switches = ARRAY_SIZE(out_switches);
    647 	int success = 1;
    648 	int rc = 0;
    649 
    650 	if (data->section && data->section->jack_switch >= 0) {
    651 		switches = &data->section->jack_switch;
    652 		num_switches = 1;
    653 	}
    654 	else if (data->jack_list->direction == CRAS_STREAM_INPUT) {
    655 		switches = in_switches;
    656 		num_switches = ARRAY_SIZE(in_switches);
    657 	}
    658 
    659 	/* Assume that -EIO is returned for jacks that we shouldn't
    660 	 * be looking at, but stop trying if we run into another
    661 	 * type of error.
    662 	 */
    663 	for (sw = 0; (rc == 0 || rc == -EIO)
    664 		     && sw < num_switches; sw++) {
    665 		if (data->section)
    666 			rc = open_and_monitor_gpio_with_section(
    667 				data, dev_path, switches[sw]);
    668 		else
    669 			rc = open_and_monitor_gpio(
    670 				data, dev_path, dev_name, switches[sw]);
    671 		if (rc != 0 && rc != -EIO)
    672 			success = 0;
    673 	}
    674 
    675 	if (success)
    676 		return 0;
    677 	return rc;
    678 }
    679 
    680 static int gpio_switch_list_with_section(const char *dev_path,
    681 					 const char *dev_name,
    682 					 void *arg)
    683 {
    684 	struct gpio_switch_list_data *data =
    685 		(struct gpio_switch_list_data *)arg;
    686 
    687 	if (strcmp(dev_name, data->section->jack_name)) {
    688 		/* No match: continue searching. */
    689 		return 0;
    690 	}
    691 
    692 	data->rc = gpio_switches_monitor_device(data, dev_path, dev_name);
    693 	/* Found the only possible match: stop searching. */
    694 	return 1;
    695 }
    696 
    697 /* Match the given jack name to the given regular expression.
    698  * Args:
    699  *    jack_name - The jack's name.
    700  *    re - Regular expression string.
    701  * Returns:
    702  *    Non-zero for success, or 0 for failure.
    703  */
    704 static int jack_matches_regex(const char *jack_name, const char *re)
    705 {
    706 	regmatch_t m[1];
    707 	regex_t regex;
    708 	int rc;
    709 
    710 	rc = regcomp(&regex, re, REG_EXTENDED);
    711 	if (rc != 0) {
    712 		syslog(LOG_ERR, "Failed to compile regular expression: %s", re);
    713 		return 0;
    714 	}
    715 
    716 	rc = regexec(&regex, jack_name, ARRAY_SIZE(m), m, 0) == 0;
    717 	regfree(&regex);
    718 	return rc;
    719 }
    720 
    721 static int gpio_switch_list_by_matching(const char *dev_path,
    722 					const char *dev_name,
    723 					void *arg)
    724 {
    725 	struct gpio_switch_list_data *data =
    726 		(struct gpio_switch_list_data *)arg;
    727 
    728 	if (data->jack_list->direction == CRAS_STREAM_INPUT) {
    729 		if (!jack_matches_regex(dev_name, "^.*Mic Jack$") &&
    730 		    !jack_matches_regex(dev_name, "^.*Headset Jack$")) {
    731 			/* Continue searching. */
    732 			return 0;
    733 		}
    734 	}
    735 	else if (data->jack_list->direction == CRAS_STREAM_OUTPUT) {
    736 		if (!jack_matches_regex(dev_name, "^.*Headphone Jack$") &&
    737 		    !jack_matches_regex(dev_name, "^.*Headset Jack$") &&
    738 		    !jack_matches_regex(dev_name, "^.*HDMI Jack$")) {
    739 			/* Continue searching. */
    740 			return 0;
    741 		}
    742 	}
    743 
    744 	data->rc = gpio_switches_monitor_device(data, dev_path, dev_name);
    745 	/* Stop searching for failure. */
    746 	return data->rc;
    747 }
    748 
    749 /* Find GPIO jacks for this jack_list.
    750  * Args:
    751  *    jack_list - Jack list to add to.
    752  *    section - UCM section.
    753  *    result_jack - Filled with a pointer to the resulting cras_alsa_jack.
    754  * Returns:
    755  *    0 for success, or negative on error. Assumes success if no jack is
    756  *    found, or if the jack could not be accessed.
    757  */
    758 static int find_gpio_jacks(struct cras_alsa_jack_list *jack_list,
    759 			   struct ucm_section *section,
    760 			   struct cras_alsa_jack **result_jack)
    761 {
    762 	/* GPIO switches are on Arm-based machines, and are
    763 	 * only associated with on-board devices.
    764 	 */
    765 	struct gpio_switch_list_data data;
    766 	int rc;
    767 
    768 	rc = wait_for_dev_input_access();
    769 	if (rc != 0) {
    770 		syslog(LOG_WARNING, "Could not access /dev/input/event0: %s",
    771 		       strerror(rc));
    772 		return 0;
    773 	}
    774 
    775 	data.jack_list = jack_list;
    776 	data.section = section;
    777 	data.result_jack = NULL;
    778 	data.rc = 0;
    779 
    780 	if (section)
    781 		gpio_switch_list_for_each(
    782 			gpio_switch_list_with_section, &data);
    783 	else
    784 		gpio_switch_list_for_each(
    785 			gpio_switch_list_by_matching, &data);
    786 	if (result_jack)
    787 		*result_jack = data.result_jack;
    788 	return data.rc;
    789 }
    790 
    791 /* Callback from alsa when a jack control changes.  This is registered with
    792  * snd_hctl_elem_set_callback in find_jack_controls and run by calling
    793  * snd_hctl_handle_events in alsa_control_event_pending below.
    794  * Args:
    795  *    elem - The ALSA control element that has changed.
    796  *    mask - unused.
    797  */
    798 static int hctl_jack_cb(snd_hctl_elem_t *elem, unsigned int mask)
    799 {
    800 	const char *name;
    801 	snd_ctl_elem_value_t *elem_value;
    802 	struct cras_alsa_jack *jack;
    803 
    804 	jack = snd_hctl_elem_get_callback_private(elem);
    805 	if (jack == NULL) {
    806 		syslog(LOG_ERR, "Invalid jack from control event.");
    807 		return -EINVAL;
    808 	}
    809 
    810 	snd_ctl_elem_value_alloca(&elem_value);
    811 	snd_hctl_elem_read(elem, elem_value);
    812 	name = snd_hctl_elem_get_name(elem);
    813 
    814 	syslog(LOG_DEBUG,
    815 	       "Jack %s %s",
    816 	       name,
    817 	       snd_ctl_elem_value_get_boolean(elem_value, 0) ? "plugged"
    818 							     : "unplugged");
    819 	jack_state_change_cb(jack, 1);
    820 	return 0;
    821 }
    822 
    823 /* Determines the device associated with this jack if any.  If the device cannot
    824  * be determined (common case), assume device 0. */
    825 static unsigned int hctl_jack_device_index(const char *name)
    826 {
    827 	/* Look for the substring 'pcm=<device number>' in the element name. */
    828 	static const char pcm_search[] = "pcm=";
    829 	const char *substr;
    830 	int device_index;
    831 
    832 	substr = strstr(name, pcm_search);
    833 	if (substr == NULL)
    834 		return 0;
    835 	substr += ARRAY_SIZE(pcm_search) - 1;
    836 	if (*substr == '\0')
    837 		return 0;
    838 	device_index = atoi(substr);
    839 	if (device_index < 0)
    840 		return 0;
    841 	return (unsigned int)device_index;
    842 }
    843 
    844 /* For non-gpio jack, check if it's of type hdmi/dp by
    845  * matching jack name. */
    846 static int is_jack_hdmi_dp(const char *jack_name)
    847 {
    848 	static const char *hdmi_dp = "HDMI/DP";
    849 	return strncmp(jack_name, hdmi_dp, strlen(hdmi_dp)) == 0;
    850 }
    851 
    852 /* Checks if the given control name is in the supplied list of possible jack
    853  * control base names. */
    854 static int is_jack_control_in_list(const char * const *list,
    855 				   unsigned int list_length,
    856 				   const char *control_name)
    857 {
    858 	unsigned int i;
    859 
    860 	for (i = 0; i < list_length; i++)
    861 		if (strncmp(control_name, list[i], strlen(list[i])) == 0)
    862 			return 1;
    863 	return 0;
    864 }
    865 
    866 /* Looks for any JACK controls.  Monitors any found controls for changes and
    867  * decides to route based on plug/unlpug events. */
    868 static int find_jack_controls(struct cras_alsa_jack_list *jack_list)
    869 {
    870 	snd_hctl_elem_t *elem;
    871 	struct cras_alsa_jack *jack;
    872 	const char *name;
    873 	static const char * const output_jack_base_names[] = {
    874 		"Headphone Jack",
    875 		"Front Headphone Jack",
    876 		"HDMI/DP",
    877 		"Speaker Phantom Jack",
    878 	};
    879 	static const char * const input_jack_base_names[] = {
    880 		"Mic Jack",
    881 	};
    882 	static const char eld_control_name[] = "ELD";
    883 	const char * const *jack_names;
    884 	unsigned int num_jack_names;
    885 	char device_name[6];
    886 
    887 	if (!jack_list->hctl) {
    888 		syslog(LOG_WARNING, "Can't search hctl for jacks.");
    889 		return 0;
    890 	}
    891 
    892 	if (jack_list->direction == CRAS_STREAM_OUTPUT) {
    893 		jack_names = output_jack_base_names;
    894 		num_jack_names = ARRAY_SIZE(output_jack_base_names);
    895 	} else {
    896 		jack_names = input_jack_base_names;
    897 		num_jack_names = ARRAY_SIZE(input_jack_base_names);
    898 	}
    899 
    900 	for (elem = snd_hctl_first_elem(jack_list->hctl); elem != NULL;
    901 			elem = snd_hctl_elem_next(elem)) {
    902 		snd_ctl_elem_iface_t iface;
    903 
    904 		iface = snd_hctl_elem_get_interface(elem);
    905 		if (iface != SND_CTL_ELEM_IFACE_CARD)
    906 			continue;
    907 		name = snd_hctl_elem_get_name(elem);
    908 		if (!is_jack_control_in_list(jack_names, num_jack_names, name))
    909 			continue;
    910 		if (hctl_jack_device_index(name) != jack_list->device_index)
    911 			continue;
    912 
    913 		jack = cras_alloc_jack(0);
    914 		if (jack == NULL)
    915 			return -ENOMEM;
    916 		jack->elem = elem;
    917 		jack->jack_list = jack_list;
    918 		DL_APPEND(jack_list->jacks, jack);
    919 
    920 		syslog(LOG_DEBUG, "Found Jack: %s for %s", name, device_name);
    921 		snd_hctl_elem_set_callback(elem, hctl_jack_cb);
    922 		snd_hctl_elem_set_callback_private(elem, jack);
    923 
    924 		if (jack_list->direction == CRAS_STREAM_OUTPUT)
    925 			jack->mixer_output =
    926 				cras_alsa_mixer_get_output_matching_name(
    927 					jack_list->mixer,
    928 					name);
    929 		if (jack_list->ucm)
    930 			jack->ucm_device =
    931 				ucm_get_dev_for_jack(jack_list->ucm, name,
    932 						     jack_list->direction);
    933 
    934 		if (jack->ucm_device && jack_list->direction == CRAS_STREAM_INPUT) {
    935 			char *control_name;
    936 			control_name = ucm_get_cap_control(jack->jack_list->ucm,
    937 						       jack->ucm_device);
    938 			if (control_name)
    939 				jack->mixer_input =
    940 					cras_alsa_mixer_get_input_matching_name(
    941 						jack_list->mixer,
    942 						control_name);
    943 		}
    944 
    945 		if (jack->ucm_device) {
    946 			jack->dsp_name = ucm_get_dsp_name(
    947 				jack->jack_list->ucm, jack->ucm_device,
    948 				jack_list->direction);
    949 			jack->override_type_name = ucm_get_override_type_name(
    950 				jack->jack_list->ucm, jack->ucm_device);
    951 		}
    952 	}
    953 
    954 	/* Look up ELD controls */
    955 	DL_FOREACH(jack_list->jacks, jack) {
    956 		if (jack->is_gpio || jack->eld_control)
    957 			continue;
    958 		name = snd_hctl_elem_get_name(jack->elem);
    959 		if (!is_jack_hdmi_dp(name))
    960 			continue;
    961 		for (elem = snd_hctl_first_elem(jack_list->hctl); elem != NULL;
    962 		     elem = snd_hctl_elem_next(elem)) {
    963 			if (strcmp(snd_hctl_elem_get_name(elem),
    964 				   eld_control_name))
    965 				continue;
    966 			if (snd_hctl_elem_get_device(elem)
    967 			    != jack_list->device_index)
    968 				continue;
    969 			jack->eld_control = elem;
    970 			break;
    971 		}
    972 	}
    973 
    974 	return 0;
    975 }
    976 
    977 /*
    978  * Exported Interface.
    979  */
    980 
    981 int cras_alsa_jack_list_find_jacks_by_name_matching(
    982 	struct cras_alsa_jack_list *jack_list)
    983 {
    984 	int rc;
    985 
    986 	rc = find_jack_controls(jack_list);
    987 	if (rc != 0)
    988 		return rc;
    989 
    990 	return find_gpio_jacks(jack_list, NULL, NULL);
    991 }
    992 
    993 static int find_hctl_jack_for_section(
    994 		struct cras_alsa_jack_list *jack_list,
    995 		struct ucm_section *section,
    996 		struct cras_alsa_jack **result_jack)
    997 {
    998 	static const char eld_control_name[] = "ELD";
    999 	snd_hctl_elem_t *elem;
   1000 	snd_ctl_elem_id_t *elem_id;
   1001 	struct cras_alsa_jack *jack;
   1002 
   1003 	if (!jack_list->hctl) {
   1004 		syslog(LOG_WARNING, "Can't search hctl for jacks.");
   1005 		return -ENODEV;
   1006 	}
   1007 
   1008 	snd_ctl_elem_id_alloca(&elem_id);
   1009 	snd_ctl_elem_id_clear(elem_id);
   1010 	snd_ctl_elem_id_set_interface(elem_id, SND_CTL_ELEM_IFACE_CARD);
   1011 	snd_ctl_elem_id_set_device(elem_id, jack_list->device_index);
   1012 	snd_ctl_elem_id_set_name(elem_id, section->jack_name);
   1013 	elem = snd_hctl_find_elem(jack_list->hctl, elem_id);
   1014 	if (!elem)
   1015 		return -ENOENT;
   1016 
   1017 	syslog(LOG_DEBUG, "Found Jack: %s for %s",
   1018 	       section->jack_name, section->name);
   1019 
   1020 	jack = cras_alloc_jack(0);
   1021 	if (jack == NULL)
   1022 		return -ENOMEM;
   1023 	jack->elem = elem;
   1024 	jack->jack_list = jack_list;
   1025 
   1026 	jack->ucm_device = strdup(section->name);
   1027 	if (!jack->ucm_device) {
   1028 		free(jack);
   1029 		return -ENOMEM;
   1030 	}
   1031 	if (jack_list->direction == CRAS_STREAM_OUTPUT)
   1032 		jack->mixer_output = cras_alsa_mixer_get_control_for_section(
   1033 					jack_list->mixer, section);
   1034 	else if (jack_list->direction == CRAS_STREAM_INPUT)
   1035 		jack->mixer_input = cras_alsa_mixer_get_control_for_section(
   1036 					jack_list->mixer, section);
   1037 
   1038 	jack->dsp_name = ucm_get_dsp_name(
   1039 		jack->jack_list->ucm, jack->ucm_device,
   1040 		jack_list->direction);
   1041 
   1042 	snd_hctl_elem_set_callback(elem, hctl_jack_cb);
   1043 	snd_hctl_elem_set_callback_private(elem, jack);
   1044 	DL_APPEND(jack_list->jacks, jack);
   1045 	if (result_jack)
   1046 		*result_jack = jack;
   1047 
   1048 	if (!strcmp(jack->ucm_device, "HDMI") ||
   1049 	    !strcmp(jack->ucm_device, "DP"))
   1050 		return 0;
   1051 
   1052 	/* Look up ELD control. */
   1053 	snd_ctl_elem_id_set_name(elem_id, eld_control_name);
   1054 	elem = snd_hctl_find_elem(jack_list->hctl, elem_id);
   1055 	if (elem)
   1056 		jack->eld_control = elem;
   1057 	return 0;
   1058 }
   1059 
   1060 /*
   1061  * Exported Interface.
   1062  */
   1063 
   1064 int cras_alsa_jack_list_add_jack_for_section(
   1065 	struct cras_alsa_jack_list *jack_list,
   1066 	struct ucm_section *ucm_section,
   1067 	struct cras_alsa_jack **result_jack)
   1068 {
   1069 	if (result_jack)
   1070 		*result_jack = NULL;
   1071 	if (!ucm_section)
   1072 		return -EINVAL;
   1073 
   1074 	if (!ucm_section->jack_name) {
   1075 		/* No jacks defined for this device. */
   1076 		return 0;
   1077 	}
   1078 
   1079 	if (!ucm_section->jack_type) {
   1080 		syslog(LOG_ERR,
   1081 		       "Must specify the JackType for jack '%s' in '%s'.",
   1082 		       ucm_section->jack_name, ucm_section->name);
   1083 		return -EINVAL;
   1084 	}
   1085 
   1086 	if (!strcmp(ucm_section->jack_type, "hctl")) {
   1087 		return find_hctl_jack_for_section(
   1088 				jack_list, ucm_section, result_jack);
   1089 	} else if (!strcmp(ucm_section->jack_type, "gpio")) {
   1090 		return find_gpio_jacks(jack_list, ucm_section, result_jack);
   1091 	} else {
   1092 		syslog(LOG_ERR,
   1093 		       "Invalid JackType '%s' in '%s'.",
   1094 		       ucm_section->jack_type, ucm_section->name);
   1095 		return -EINVAL;
   1096 	}
   1097 }
   1098 
   1099 struct cras_alsa_jack_list *cras_alsa_jack_list_create(
   1100 		unsigned int card_index,
   1101 		const char *card_name,
   1102 		unsigned int device_index,
   1103 		int is_first_device,
   1104 		struct cras_alsa_mixer *mixer,
   1105 		struct cras_use_case_mgr *ucm,
   1106 		snd_hctl_t *hctl,
   1107 		enum CRAS_STREAM_DIRECTION direction,
   1108 		jack_state_change_callback *cb,
   1109 		void *cb_data)
   1110 {
   1111 	struct cras_alsa_jack_list *jack_list;
   1112 
   1113 	if (direction != CRAS_STREAM_INPUT && direction != CRAS_STREAM_OUTPUT)
   1114 		return NULL;
   1115 
   1116 	jack_list = (struct cras_alsa_jack_list *)calloc(1, sizeof(*jack_list));
   1117 	if (jack_list == NULL)
   1118 		return NULL;
   1119 
   1120 	jack_list->change_callback = cb;
   1121 	jack_list->callback_data = cb_data;
   1122 	jack_list->mixer = mixer;
   1123 	jack_list->ucm = ucm;
   1124 	jack_list->hctl = hctl;
   1125 	jack_list->card_index = card_index;
   1126 	jack_list->card_name = card_name;
   1127 	jack_list->device_index = device_index;
   1128 	jack_list->is_first_device = is_first_device;
   1129 	jack_list->direction = direction;
   1130 
   1131 	return jack_list;
   1132 }
   1133 
   1134 void cras_alsa_jack_list_destroy(struct cras_alsa_jack_list *jack_list)
   1135 {
   1136 	struct cras_alsa_jack *jack;
   1137 
   1138 	if (jack_list == NULL)
   1139 		return;
   1140 	DL_FOREACH(jack_list->jacks, jack) {
   1141 		DL_DELETE(jack_list->jacks, jack);
   1142 		cras_free_jack(jack, 1);
   1143 	}
   1144 	free(jack_list);
   1145 }
   1146 
   1147 int cras_alsa_jack_list_has_hctl_jacks(struct cras_alsa_jack_list *jack_list)
   1148 {
   1149 	struct cras_alsa_jack *jack;
   1150 
   1151 	if (!jack_list)
   1152 		return 0;
   1153 	DL_FOREACH(jack_list->jacks, jack) {
   1154 		if (!jack->is_gpio)
   1155 			return 1;
   1156 	}
   1157 	return 0;
   1158 }
   1159 
   1160 struct mixer_control *cras_alsa_jack_get_mixer_output(
   1161 		const struct cras_alsa_jack *jack)
   1162 {
   1163 	if (jack == NULL)
   1164 		return NULL;
   1165 	return jack->mixer_output;
   1166 }
   1167 
   1168 struct mixer_control *cras_alsa_jack_get_mixer_input(
   1169 		const struct cras_alsa_jack *jack)
   1170 {
   1171 	return jack ? jack->mixer_input : NULL;
   1172 }
   1173 
   1174 void cras_alsa_jack_list_report(const struct cras_alsa_jack_list *jack_list)
   1175 {
   1176 	struct cras_alsa_jack *jack;
   1177 
   1178 	if (jack_list == NULL)
   1179 		return;
   1180 
   1181 	DL_FOREACH(jack_list->jacks, jack)
   1182 		if (jack->is_gpio)
   1183 			gpio_switch_initial_state(jack);
   1184 		else
   1185 			hctl_jack_cb(jack->elem, 0);
   1186 }
   1187 
   1188 const char *cras_alsa_jack_get_name(const struct cras_alsa_jack *jack)
   1189 {
   1190 	if (jack == NULL)
   1191 		return NULL;
   1192 	if (jack->is_gpio)
   1193 		return jack->gpio.device_name;
   1194 	return snd_hctl_elem_get_name(jack->elem);
   1195 }
   1196 
   1197 const char *cras_alsa_jack_get_ucm_device(const struct cras_alsa_jack *jack)
   1198 {
   1199 	return jack->ucm_device;
   1200 }
   1201 
   1202 void cras_alsa_jack_update_monitor_name(const struct cras_alsa_jack *jack,
   1203 					char *name_buf,
   1204 					unsigned int buf_size)
   1205 {
   1206 	snd_ctl_elem_value_t *elem_value;
   1207 	snd_ctl_elem_info_t *elem_info;
   1208 	const char *buf = NULL;
   1209 	int count;
   1210 	int mnl = 0;
   1211 
   1212 	if (!jack->eld_control) {
   1213 		if (jack->edid_file)
   1214 			get_jack_edid_monitor_name(jack, name_buf, buf_size);
   1215 		return;
   1216 	}
   1217 
   1218 	snd_ctl_elem_info_alloca(&elem_info);
   1219 	if (snd_hctl_elem_info(jack->eld_control, elem_info) < 0)
   1220 		goto fallback_jack_name;
   1221 
   1222 	count = snd_ctl_elem_info_get_count(elem_info);
   1223 	if (count <= ELD_MNL_OFFSET)
   1224 		goto fallback_jack_name;
   1225 
   1226 	snd_ctl_elem_value_alloca(&elem_value);
   1227 	if (snd_hctl_elem_read(jack->eld_control, elem_value) < 0)
   1228 		goto fallback_jack_name;
   1229 
   1230 	buf = (const char *)snd_ctl_elem_value_get_bytes(elem_value);
   1231 	mnl = buf[ELD_MNL_OFFSET] & ELD_MNL_MASK;
   1232 
   1233 	if (count < ELD_MONITOR_NAME_OFFSET + mnl)
   1234 		goto fallback_jack_name;
   1235 
   1236 	/* Note that monitor name string does not contain terminate character.
   1237 	 * Check monitor name length with name buffer size.
   1238 	 */
   1239 	if (mnl >= buf_size)
   1240 		mnl = buf_size - 1;
   1241 	strncpy(name_buf, buf + ELD_MONITOR_NAME_OFFSET, mnl);
   1242 	name_buf[mnl] = '\0';
   1243 
   1244 	return;
   1245 
   1246 fallback_jack_name:
   1247 	buf = cras_alsa_jack_get_name(jack);
   1248 	strncpy(name_buf, buf, buf_size - 1);
   1249 
   1250 	return;
   1251 }
   1252 
   1253 void cras_alsa_jack_update_node_type(const struct cras_alsa_jack *jack,
   1254 				     enum CRAS_NODE_TYPE *type)
   1255 {
   1256 	if (!jack->override_type_name)
   1257 		return;
   1258 	if (!strcmp(jack->override_type_name, "Internal Speaker"))
   1259 		*type = CRAS_NODE_TYPE_INTERNAL_SPEAKER;
   1260 	return;
   1261 }
   1262 
   1263 const char *cras_alsa_jack_get_dsp_name(const struct cras_alsa_jack *jack)
   1264 {
   1265 	if (jack == NULL)
   1266 		return NULL;
   1267 	return jack->dsp_name;
   1268 }
   1269 
   1270 void cras_alsa_jack_enable_ucm(const struct cras_alsa_jack *jack, int enable)
   1271 {
   1272 	if (jack && jack->ucm_device)
   1273 		ucm_set_enabled(jack->jack_list->ucm, jack->ucm_device, enable);
   1274 }
   1275