1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 #include "dm_helpers.h"
30 #include "core_types.h"
31 
32 /*******************************************************************************
33  * Private functions
34  ******************************************************************************/
35 
dc_sink_destruct(struct dc_sink * sink)36 static void dc_sink_destruct(struct dc_sink *sink)
37 {
38 	if (sink->dc_container_id) {
39 		kfree(sink->dc_container_id);
40 		sink->dc_container_id = NULL;
41 	}
42 }
43 
dc_sink_construct(struct dc_sink * sink,const struct dc_sink_init_data * init_params)44 static bool dc_sink_construct(struct dc_sink *sink, const struct dc_sink_init_data *init_params)
45 {
46 
47 	struct dc_link *link = init_params->link;
48 
49 	if (!link)
50 		return false;
51 
52 	sink->sink_signal = init_params->sink_signal;
53 	sink->link = link;
54 	sink->ctx = link->ctx;
55 	sink->dongle_max_pix_clk = init_params->dongle_max_pix_clk;
56 	sink->converter_disable_audio = init_params->converter_disable_audio;
57 	sink->dc_container_id = NULL;
58 	sink->sink_id = init_params->link->ctx->dc_sink_id_count;
59 	// increment dc_sink_id_count because we don't want two sinks with same ID
60 	// unless they are actually the same
61 	init_params->link->ctx->dc_sink_id_count++;
62 
63 	return true;
64 }
65 
66 /*******************************************************************************
67  * Public functions
68  ******************************************************************************/
69 
dc_sink_retain(struct dc_sink * sink)70 void dc_sink_retain(struct dc_sink *sink)
71 {
72 	kref_get(&sink->refcount);
73 }
74 
dc_sink_free(struct kref * kref)75 static void dc_sink_free(struct kref *kref)
76 {
77 	struct dc_sink *sink = container_of(kref, struct dc_sink, refcount);
78 	dc_sink_destruct(sink);
79 	kfree(sink);
80 }
81 
dc_sink_release(struct dc_sink * sink)82 void dc_sink_release(struct dc_sink *sink)
83 {
84 	kref_put(&sink->refcount, dc_sink_free);
85 }
86 
dc_sink_create(const struct dc_sink_init_data * init_params)87 struct dc_sink *dc_sink_create(const struct dc_sink_init_data *init_params)
88 {
89 	struct dc_sink *sink = kzalloc(sizeof(*sink), GFP_KERNEL);
90 
91 	if (NULL == sink)
92 		goto alloc_fail;
93 
94 	if (false == dc_sink_construct(sink, init_params))
95 		goto construct_fail;
96 
97 	kref_init(&sink->refcount);
98 
99 	return sink;
100 
101 construct_fail:
102 	kfree(sink);
103 
104 alloc_fail:
105 	return NULL;
106 }
107 
108 /*******************************************************************************
109  * Protected functions - visible only inside of DC (not visible in DM)
110  ******************************************************************************/
111