1313#include <linux/device.h>
1414#include <linux/err.h>
1515#include <linux/module.h>
16- #include <linux/rculist .h>
16+ #include <linux/mutex .h>
1717#include <linux/slab.h>
1818#include <linux/of.h>
1919#include <linux/of_dma.h>
2020
2121static LIST_HEAD (of_dma_list );
22- static DEFINE_SPINLOCK (of_dma_lock );
22+ static DEFINE_MUTEX (of_dma_lock );
2323
2424/**
25- * of_dma_get_controller - Get a DMA controller in DT DMA helpers list
25+ * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
2626 * @dma_spec: pointer to DMA specifier as found in the device tree
2727 *
2828 * Finds a DMA controller with matching device node and number for dma cells
29- * in a list of registered DMA controllers. If a match is found the use_count
30- * variable is increased and a valid pointer to the DMA data stored is retuned.
31- * A NULL pointer is returned if no match is found.
29+ * in a list of registered DMA controllers. If a match is found a valid pointer
30+ * to the DMA data stored is retuned. A NULL pointer is returned if no match is
31+ * found.
3232 */
33- static struct of_dma * of_dma_get_controller (struct of_phandle_args * dma_spec )
33+ static struct of_dma * of_dma_find_controller (struct of_phandle_args * dma_spec )
3434{
3535 struct of_dma * ofdma ;
3636
37- spin_lock (& of_dma_lock );
38-
39- if (list_empty (& of_dma_list )) {
40- spin_unlock (& of_dma_lock );
41- return NULL ;
42- }
43-
4437 list_for_each_entry (ofdma , & of_dma_list , of_dma_controllers )
4538 if ((ofdma -> of_node == dma_spec -> np ) &&
46- (ofdma -> of_dma_nbcells == dma_spec -> args_count )) {
47- ofdma -> use_count ++ ;
48- spin_unlock (& of_dma_lock );
39+ (ofdma -> of_dma_nbcells == dma_spec -> args_count ))
4940 return ofdma ;
50- }
51-
52- spin_unlock (& of_dma_lock );
5341
5442 pr_debug ("%s: can't find DMA controller %s\n" , __func__ ,
5543 dma_spec -> np -> full_name );
5644
5745 return NULL ;
5846}
5947
60- /**
61- * of_dma_put_controller - Decrement use count for a registered DMA controller
62- * @of_dma: pointer to DMA controller data
63- *
64- * Decrements the use_count variable in the DMA data structure. This function
65- * should be called only when a valid pointer is returned from
66- * of_dma_get_controller() and no further accesses to data referenced by that
67- * pointer are needed.
68- */
69- static void of_dma_put_controller (struct of_dma * ofdma )
70- {
71- spin_lock (& of_dma_lock );
72- ofdma -> use_count -- ;
73- spin_unlock (& of_dma_lock );
74- }
75-
7648/**
7749 * of_dma_controller_register - Register a DMA controller to DT DMA helpers
7850 * @np: device node of DMA controller
@@ -93,6 +65,7 @@ int of_dma_controller_register(struct device_node *np,
9365{
9466 struct of_dma * ofdma ;
9567 int nbcells ;
68+ const __be32 * prop ;
9669
9770 if (!np || !of_dma_xlate ) {
9871 pr_err ("%s: not enough information provided\n" , __func__ );
@@ -103,8 +76,11 @@ int of_dma_controller_register(struct device_node *np,
10376 if (!ofdma )
10477 return - ENOMEM ;
10578
106- nbcells = be32_to_cpup (of_get_property (np , "#dma-cells" , NULL ));
107- if (!nbcells ) {
79+ prop = of_get_property (np , "#dma-cells" , NULL );
80+ if (prop )
81+ nbcells = be32_to_cpup (prop );
82+
83+ if (!prop || !nbcells ) {
10884 pr_err ("%s: #dma-cells property is missing or invalid\n" ,
10985 __func__ );
11086 kfree (ofdma );
@@ -115,12 +91,11 @@ int of_dma_controller_register(struct device_node *np,
11591 ofdma -> of_dma_nbcells = nbcells ;
11692 ofdma -> of_dma_xlate = of_dma_xlate ;
11793 ofdma -> of_dma_data = data ;
118- ofdma -> use_count = 0 ;
11994
12095 /* Now queue of_dma controller structure in list */
121- spin_lock (& of_dma_lock );
96+ mutex_lock (& of_dma_lock );
12297 list_add_tail (& ofdma -> of_dma_controllers , & of_dma_list );
123- spin_unlock (& of_dma_lock );
98+ mutex_unlock (& of_dma_lock );
12499
125100 return 0 ;
126101}
@@ -132,32 +107,20 @@ EXPORT_SYMBOL_GPL(of_dma_controller_register);
132107 *
133108 * Memory allocated by of_dma_controller_register() is freed here.
134109 */
135- int of_dma_controller_free (struct device_node * np )
110+ void of_dma_controller_free (struct device_node * np )
136111{
137112 struct of_dma * ofdma ;
138113
139- spin_lock (& of_dma_lock );
140-
141- if (list_empty (& of_dma_list )) {
142- spin_unlock (& of_dma_lock );
143- return - ENODEV ;
144- }
114+ mutex_lock (& of_dma_lock );
145115
146116 list_for_each_entry (ofdma , & of_dma_list , of_dma_controllers )
147117 if (ofdma -> of_node == np ) {
148- if (ofdma -> use_count ) {
149- spin_unlock (& of_dma_lock );
150- return - EBUSY ;
151- }
152-
153118 list_del (& ofdma -> of_dma_controllers );
154- spin_unlock (& of_dma_lock );
155119 kfree (ofdma );
156- return 0 ;
120+ break ;
157121 }
158122
159- spin_unlock (& of_dma_lock );
160- return - ENODEV ;
123+ mutex_unlock (& of_dma_lock );
161124}
162125EXPORT_SYMBOL_GPL (of_dma_controller_free );
163126
@@ -172,8 +135,8 @@ EXPORT_SYMBOL_GPL(of_dma_controller_free);
172135 * specifiers, matches the name provided. Returns 0 if the name matches and
173136 * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
174137 */
175- static int of_dma_match_channel (struct device_node * np , char * name , int index ,
176- struct of_phandle_args * dma_spec )
138+ static int of_dma_match_channel (struct device_node * np , const char * name ,
139+ int index , struct of_phandle_args * dma_spec )
177140{
178141 const char * s ;
179142
@@ -198,7 +161,7 @@ static int of_dma_match_channel(struct device_node *np, char *name, int index,
198161 * Returns pointer to appropriate dma channel on success or NULL on error.
199162 */
200163struct dma_chan * of_dma_request_slave_channel (struct device_node * np ,
201- char * name )
164+ const char * name )
202165{
203166 struct of_phandle_args dma_spec ;
204167 struct of_dma * ofdma ;
@@ -220,14 +183,15 @@ struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
220183 if (of_dma_match_channel (np , name , i , & dma_spec ))
221184 continue ;
222185
223- ofdma = of_dma_get_controller (& dma_spec );
224-
225- if (!ofdma )
226- continue ;
186+ mutex_lock (& of_dma_lock );
187+ ofdma = of_dma_find_controller (& dma_spec );
227188
228- chan = ofdma -> of_dma_xlate (& dma_spec , ofdma );
189+ if (ofdma )
190+ chan = ofdma -> of_dma_xlate (& dma_spec , ofdma );
191+ else
192+ chan = NULL ;
229193
230- of_dma_put_controller ( ofdma );
194+ mutex_unlock ( & of_dma_lock );
231195
232196 of_node_put (dma_spec .np );
233197
0 commit comments