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 "cras_device_blocklist.h"
7 #include "iniparser_wrapper.h"
8 #include "utlist.h"
9 
10 struct cras_device_blocklist {
11 	dictionary *ini;
12 };
13 
14 /*
15  * Exported Interface
16  */
17 
18 struct cras_device_blocklist *
cras_device_blocklist_create(const char * config_path)19 cras_device_blocklist_create(const char *config_path)
20 {
21 	struct cras_device_blocklist *blocklist;
22 	char ini_name[MAX_INI_NAME_LENGTH + 1];
23 
24 	blocklist = calloc(1, sizeof(*blocklist));
25 	if (!blocklist)
26 		return NULL;
27 
28 	snprintf(ini_name, MAX_INI_NAME_LENGTH, "%s/%s", config_path,
29 		 "device_blocklist");
30 	ini_name[MAX_INI_NAME_LENGTH] = '\0';
31 	blocklist->ini = iniparser_load_wrapper(ini_name);
32 
33 	return blocklist;
34 }
35 
cras_device_blocklist_destroy(struct cras_device_blocklist * blocklist)36 void cras_device_blocklist_destroy(struct cras_device_blocklist *blocklist)
37 {
38 	if (blocklist && blocklist->ini)
39 		iniparser_freedict(blocklist->ini);
40 	free(blocklist);
41 }
42 
cras_device_blocklist_check(struct cras_device_blocklist * blocklist,unsigned vendor_id,unsigned product_id,unsigned desc_checksum,unsigned device_index)43 int cras_device_blocklist_check(struct cras_device_blocklist *blocklist,
44 				unsigned vendor_id, unsigned product_id,
45 				unsigned desc_checksum, unsigned device_index)
46 {
47 	char ini_key[MAX_INI_KEY_LENGTH + 1];
48 
49 	if (!blocklist)
50 		return 0;
51 
52 	snprintf(ini_key, MAX_INI_KEY_LENGTH, "USB_Outputs:%04x_%04x_%08x_%u",
53 		 vendor_id, product_id, desc_checksum, device_index);
54 	ini_key[MAX_INI_KEY_LENGTH] = 0;
55 	return iniparser_getboolean(blocklist->ini, ini_key, 0);
56 }
57