aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/otg/otg.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/otg/otg.c')
-rw-r--r--drivers/usb/otg/otg.c181
1 files changed, 158 insertions, 23 deletions
diff --git a/drivers/usb/otg/otg.c b/drivers/usb/otg/otg.c
index 801e597a1541..1bf60a22595c 100644
--- a/drivers/usb/otg/otg.c
+++ b/drivers/usb/otg/otg.c
@@ -11,60 +11,195 @@
11 11
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/export.h> 13#include <linux/export.h>
14#include <linux/err.h>
14#include <linux/device.h> 15#include <linux/device.h>
16#include <linux/slab.h>
15 17
16#include <linux/usb/otg.h> 18#include <linux/usb/otg.h>
17 19
18static struct usb_phy *phy; 20static LIST_HEAD(phy_list);
21static DEFINE_SPINLOCK(phy_lock);
22
23static struct usb_phy *__usb_find_phy(struct list_head *list,
24 enum usb_phy_type type)
25{
26 struct usb_phy *phy = NULL;
27
28 list_for_each_entry(phy, list, head) {
29 if (phy->type != type)
30 continue;
31
32 return phy;
33 }
34
35 return ERR_PTR(-ENODEV);
36}
37
38static void devm_usb_phy_release(struct device *dev, void *res)
39{
40 struct usb_phy *phy = *(struct usb_phy **)res;
41
42 usb_put_phy(phy);
43}
44
45static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
46{
47 return res == match_data;
48}
19 49
20/** 50/**
21 * usb_get_transceiver - find the (single) USB transceiver 51 * devm_usb_get_phy - find the USB PHY
52 * @dev - device that requests this phy
53 * @type - the type of the phy the controller requires
22 * 54 *
23 * Returns the transceiver driver, after getting a refcount to it; or 55 * Gets the phy using usb_get_phy(), and associates a device with it using
24 * null if there is no such transceiver. The caller is responsible for 56 * devres. On driver detach, release function is invoked on the devres data,
25 * calling usb_put_transceiver() to release that count. 57 * then, devres data is freed.
26 * 58 *
27 * For use by USB host and peripheral drivers. 59 * For use by USB host and peripheral drivers.
28 */ 60 */
29struct usb_phy *usb_get_transceiver(void) 61struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
30{ 62{
31 if (phy) 63 struct usb_phy **ptr, *phy;
32 get_device(phy->dev); 64
65 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
66 if (!ptr)
67 return NULL;
68
69 phy = usb_get_phy(type);
70 if (!IS_ERR(phy)) {
71 *ptr = phy;
72 devres_add(dev, ptr);
73 } else
74 devres_free(ptr);
75
76 return phy;
77}
78EXPORT_SYMBOL(devm_usb_get_phy);
79
80/**
81 * usb_get_phy - find the USB PHY
82 * @type - the type of the phy the controller requires
83 *
84 * Returns the phy driver, after getting a refcount to it; or
85 * -ENODEV if there is no such phy. The caller is responsible for
86 * calling usb_put_phy() to release that count.
87 *
88 * For use by USB host and peripheral drivers.
89 */
90struct usb_phy *usb_get_phy(enum usb_phy_type type)
91{
92 struct usb_phy *phy = NULL;
93 unsigned long flags;
94
95 spin_lock_irqsave(&phy_lock, flags);
96
97 phy = __usb_find_phy(&phy_list, type);
98 if (IS_ERR(phy)) {
99 pr_err("unable to find transceiver of type %s\n",
100 usb_phy_type_string(type));
101 goto err0;
102 }
103
104 get_device(phy->dev);
105
106err0:
107 spin_unlock_irqrestore(&phy_lock, flags);
108
33 return phy; 109 return phy;
34} 110}
35EXPORT_SYMBOL(usb_get_transceiver); 111EXPORT_SYMBOL(usb_get_phy);
112
113/**
114 * devm_usb_put_phy - release the USB PHY
115 * @dev - device that wants to release this phy
116 * @phy - the phy returned by devm_usb_get_phy()
117 *
118 * destroys the devres associated with this phy and invokes usb_put_phy
119 * to release the phy.
120 *
121 * For use by USB host and peripheral drivers.
122 */
123void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
124{
125 int r;
126
127 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
128 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
129}
130EXPORT_SYMBOL(devm_usb_put_phy);
36 131
37/** 132/**
38 * usb_put_transceiver - release the (single) USB transceiver 133 * usb_put_phy - release the USB PHY
39 * @x: the transceiver returned by usb_get_transceiver() 134 * @x: the phy returned by usb_get_phy()
40 * 135 *
41 * Releases a refcount the caller received from usb_get_transceiver(). 136 * Releases a refcount the caller received from usb_get_phy().
42 * 137 *
43 * For use by USB host and peripheral drivers. 138 * For use by USB host and peripheral drivers.
44 */ 139 */
45void usb_put_transceiver(struct usb_phy *x) 140void usb_put_phy(struct usb_phy *x)
46{ 141{
47 if (x) 142 if (x)
48 put_device(x->dev); 143 put_device(x->dev);
49} 144}
50EXPORT_SYMBOL(usb_put_transceiver); 145EXPORT_SYMBOL(usb_put_phy);
51 146
52/** 147/**
53 * usb_set_transceiver - declare the (single) USB transceiver 148 * usb_add_phy - declare the USB PHY
54 * @x: the USB transceiver to be used; or NULL 149 * @x: the USB phy to be used; or NULL
150 * @type - the type of this PHY
55 * 151 *
56 * This call is exclusively for use by transceiver drivers, which 152 * This call is exclusively for use by phy drivers, which
57 * coordinate the activities of drivers for host and peripheral 153 * coordinate the activities of drivers for host and peripheral
58 * controllers, and in some cases for VBUS current regulation. 154 * controllers, and in some cases for VBUS current regulation.
59 */ 155 */
60int usb_set_transceiver(struct usb_phy *x) 156int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
157{
158 int ret = 0;
159 unsigned long flags;
160 struct usb_phy *phy;
161
162 if (x && x->type != USB_PHY_TYPE_UNDEFINED) {
163 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
164 return -EINVAL;
165 }
166
167 spin_lock_irqsave(&phy_lock, flags);
168
169 list_for_each_entry(phy, &phy_list, head) {
170 if (phy->type == type) {
171 ret = -EBUSY;
172 dev_err(x->dev, "transceiver type %s already exists\n",
173 usb_phy_type_string(type));
174 goto out;
175 }
176 }
177
178 x->type = type;
179 list_add_tail(&x->head, &phy_list);
180
181out:
182 spin_unlock_irqrestore(&phy_lock, flags);
183 return ret;
184}
185EXPORT_SYMBOL(usb_add_phy);
186
187/**
188 * usb_remove_phy - remove the OTG PHY
189 * @x: the USB OTG PHY to be removed;
190 *
191 * This reverts the effects of usb_add_phy
192 */
193void usb_remove_phy(struct usb_phy *x)
61{ 194{
62 if (phy && x) 195 unsigned long flags;
63 return -EBUSY; 196
64 phy = x; 197 spin_lock_irqsave(&phy_lock, flags);
65 return 0; 198 if (x)
199 list_del(&x->head);
200 spin_unlock_irqrestore(&phy_lock, flags);
66} 201}
67EXPORT_SYMBOL(usb_set_transceiver); 202EXPORT_SYMBOL(usb_remove_phy);
68 203
69const char *otg_state_string(enum usb_otg_state state) 204const char *otg_state_string(enum usb_otg_state state)
70{ 205{