otg.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * otg.c -- USB OTG utility code
  3. *
  4. * Copyright (C) 2004 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/export.h>
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/usb/otg.h>
  17. static LIST_HEAD(phy_list);
  18. static LIST_HEAD(phy_bind_list);
  19. static DEFINE_SPINLOCK(phy_lock);
  20. static struct usb_phy *__usb_find_phy(struct list_head *list,
  21. enum usb_phy_type type)
  22. {
  23. struct usb_phy *phy = NULL;
  24. list_for_each_entry(phy, list, head) {
  25. if (phy->type != type)
  26. continue;
  27. return phy;
  28. }
  29. return ERR_PTR(-ENODEV);
  30. }
  31. static struct usb_phy *__usb_find_phy_dev(struct device *dev,
  32. struct list_head *list, u8 index)
  33. {
  34. struct usb_phy_bind *phy_bind = NULL;
  35. list_for_each_entry(phy_bind, list, list) {
  36. if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
  37. phy_bind->index == index) {
  38. if (phy_bind->phy)
  39. return phy_bind->phy;
  40. else
  41. return ERR_PTR(-EPROBE_DEFER);
  42. }
  43. }
  44. return ERR_PTR(-ENODEV);
  45. }
  46. static void devm_usb_phy_release(struct device *dev, void *res)
  47. {
  48. struct usb_phy *phy = *(struct usb_phy **)res;
  49. usb_put_phy(phy);
  50. }
  51. static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
  52. {
  53. return res == match_data;
  54. }
  55. /**
  56. * devm_usb_get_phy - find the USB PHY
  57. * @dev - device that requests this phy
  58. * @type - the type of the phy the controller requires
  59. *
  60. * Gets the phy using usb_get_phy(), and associates a device with it using
  61. * devres. On driver detach, release function is invoked on the devres data,
  62. * then, devres data is freed.
  63. *
  64. * For use by USB host and peripheral drivers.
  65. */
  66. struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
  67. {
  68. struct usb_phy **ptr, *phy;
  69. ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
  70. if (!ptr)
  71. return NULL;
  72. phy = usb_get_phy(type);
  73. if (!IS_ERR(phy)) {
  74. *ptr = phy;
  75. devres_add(dev, ptr);
  76. } else
  77. devres_free(ptr);
  78. return phy;
  79. }
  80. EXPORT_SYMBOL(devm_usb_get_phy);
  81. /**
  82. * usb_get_phy - find the USB PHY
  83. * @type - the type of the phy the controller requires
  84. *
  85. * Returns the phy driver, after getting a refcount to it; or
  86. * -ENODEV if there is no such phy. The caller is responsible for
  87. * calling usb_put_phy() to release that count.
  88. *
  89. * For use by USB host and peripheral drivers.
  90. */
  91. struct usb_phy *usb_get_phy(enum usb_phy_type type)
  92. {
  93. struct usb_phy *phy = NULL;
  94. unsigned long flags;
  95. spin_lock_irqsave(&phy_lock, flags);
  96. phy = __usb_find_phy(&phy_list, type);
  97. if (IS_ERR(phy)) {
  98. pr_err("unable to find transceiver of type %s\n",
  99. usb_phy_type_string(type));
  100. goto err0;
  101. }
  102. get_device(phy->dev);
  103. err0:
  104. spin_unlock_irqrestore(&phy_lock, flags);
  105. return phy;
  106. }
  107. EXPORT_SYMBOL(usb_get_phy);
  108. /**
  109. * usb_get_phy_dev - find the USB PHY
  110. * @dev - device that requests this phy
  111. * @index - the index of the phy
  112. *
  113. * Returns the phy driver, after getting a refcount to it; or
  114. * -ENODEV if there is no such phy. The caller is responsible for
  115. * calling usb_put_phy() to release that count.
  116. *
  117. * For use by USB host and peripheral drivers.
  118. */
  119. struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
  120. {
  121. struct usb_phy *phy = NULL;
  122. unsigned long flags;
  123. spin_lock_irqsave(&phy_lock, flags);
  124. phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
  125. if (IS_ERR(phy)) {
  126. pr_err("unable to find transceiver\n");
  127. goto err0;
  128. }
  129. get_device(phy->dev);
  130. err0:
  131. spin_unlock_irqrestore(&phy_lock, flags);
  132. return phy;
  133. }
  134. EXPORT_SYMBOL(usb_get_phy_dev);
  135. /**
  136. * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
  137. * @dev - device that requests this phy
  138. * @index - the index of the phy
  139. *
  140. * Gets the phy using usb_get_phy_dev(), and associates a device with it using
  141. * devres. On driver detach, release function is invoked on the devres data,
  142. * then, devres data is freed.
  143. *
  144. * For use by USB host and peripheral drivers.
  145. */
  146. struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
  147. {
  148. struct usb_phy **ptr, *phy;
  149. ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
  150. if (!ptr)
  151. return NULL;
  152. phy = usb_get_phy_dev(dev, index);
  153. if (!IS_ERR(phy)) {
  154. *ptr = phy;
  155. devres_add(dev, ptr);
  156. } else
  157. devres_free(ptr);
  158. return phy;
  159. }
  160. EXPORT_SYMBOL(devm_usb_get_phy_dev);
  161. /**
  162. * devm_usb_put_phy - release the USB PHY
  163. * @dev - device that wants to release this phy
  164. * @phy - the phy returned by devm_usb_get_phy()
  165. *
  166. * destroys the devres associated with this phy and invokes usb_put_phy
  167. * to release the phy.
  168. *
  169. * For use by USB host and peripheral drivers.
  170. */
  171. void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
  172. {
  173. int r;
  174. r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
  175. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  176. }
  177. EXPORT_SYMBOL(devm_usb_put_phy);
  178. /**
  179. * usb_put_phy - release the USB PHY
  180. * @x: the phy returned by usb_get_phy()
  181. *
  182. * Releases a refcount the caller received from usb_get_phy().
  183. *
  184. * For use by USB host and peripheral drivers.
  185. */
  186. void usb_put_phy(struct usb_phy *x)
  187. {
  188. if (x)
  189. put_device(x->dev);
  190. }
  191. EXPORT_SYMBOL(usb_put_phy);
  192. /**
  193. * usb_add_phy - declare the USB PHY
  194. * @x: the USB phy to be used; or NULL
  195. * @type - the type of this PHY
  196. *
  197. * This call is exclusively for use by phy drivers, which
  198. * coordinate the activities of drivers for host and peripheral
  199. * controllers, and in some cases for VBUS current regulation.
  200. */
  201. int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
  202. {
  203. int ret = 0;
  204. unsigned long flags;
  205. struct usb_phy *phy;
  206. if (x->type != USB_PHY_TYPE_UNDEFINED) {
  207. dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
  208. return -EINVAL;
  209. }
  210. spin_lock_irqsave(&phy_lock, flags);
  211. list_for_each_entry(phy, &phy_list, head) {
  212. if (phy->type == type) {
  213. ret = -EBUSY;
  214. dev_err(x->dev, "transceiver type %s already exists\n",
  215. usb_phy_type_string(type));
  216. goto out;
  217. }
  218. }
  219. x->type = type;
  220. list_add_tail(&x->head, &phy_list);
  221. out:
  222. spin_unlock_irqrestore(&phy_lock, flags);
  223. return ret;
  224. }
  225. EXPORT_SYMBOL(usb_add_phy);
  226. /**
  227. * usb_add_phy_dev - declare the USB PHY
  228. * @x: the USB phy to be used; or NULL
  229. *
  230. * This call is exclusively for use by phy drivers, which
  231. * coordinate the activities of drivers for host and peripheral
  232. * controllers, and in some cases for VBUS current regulation.
  233. */
  234. int usb_add_phy_dev(struct usb_phy *x)
  235. {
  236. struct usb_phy_bind *phy_bind;
  237. unsigned long flags;
  238. if (!x->dev) {
  239. dev_err(x->dev, "no device provided for PHY\n");
  240. return -EINVAL;
  241. }
  242. spin_lock_irqsave(&phy_lock, flags);
  243. list_for_each_entry(phy_bind, &phy_bind_list, list)
  244. if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
  245. phy_bind->phy = x;
  246. list_add_tail(&x->head, &phy_list);
  247. spin_unlock_irqrestore(&phy_lock, flags);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(usb_add_phy_dev);
  251. /**
  252. * usb_remove_phy - remove the OTG PHY
  253. * @x: the USB OTG PHY to be removed;
  254. *
  255. * This reverts the effects of usb_add_phy
  256. */
  257. void usb_remove_phy(struct usb_phy *x)
  258. {
  259. unsigned long flags;
  260. struct usb_phy_bind *phy_bind;
  261. spin_lock_irqsave(&phy_lock, flags);
  262. if (x) {
  263. list_for_each_entry(phy_bind, &phy_bind_list, list)
  264. if (phy_bind->phy == x)
  265. phy_bind->phy = NULL;
  266. list_del(&x->head);
  267. }
  268. spin_unlock_irqrestore(&phy_lock, flags);
  269. }
  270. EXPORT_SYMBOL(usb_remove_phy);
  271. /**
  272. * usb_bind_phy - bind the phy and the controller that uses the phy
  273. * @dev_name: the device name of the device that will bind to the phy
  274. * @index: index to specify the port number
  275. * @phy_dev_name: the device name of the phy
  276. *
  277. * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
  278. * be used when the phy driver registers the phy and when the controller
  279. * requests this phy.
  280. *
  281. * To be used by platform specific initialization code.
  282. */
  283. int __init usb_bind_phy(const char *dev_name, u8 index,
  284. const char *phy_dev_name)
  285. {
  286. struct usb_phy_bind *phy_bind;
  287. unsigned long flags;
  288. phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
  289. if (!phy_bind) {
  290. pr_err("phy_bind(): No memory for phy_bind");
  291. return -ENOMEM;
  292. }
  293. phy_bind->dev_name = dev_name;
  294. phy_bind->phy_dev_name = phy_dev_name;
  295. phy_bind->index = index;
  296. spin_lock_irqsave(&phy_lock, flags);
  297. list_add_tail(&phy_bind->list, &phy_bind_list);
  298. spin_unlock_irqrestore(&phy_lock, flags);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL_GPL(usb_bind_phy);
  302. const char *otg_state_string(enum usb_otg_state state)
  303. {
  304. switch (state) {
  305. case OTG_STATE_A_IDLE:
  306. return "a_idle";
  307. case OTG_STATE_A_WAIT_VRISE:
  308. return "a_wait_vrise";
  309. case OTG_STATE_A_WAIT_BCON:
  310. return "a_wait_bcon";
  311. case OTG_STATE_A_HOST:
  312. return "a_host";
  313. case OTG_STATE_A_SUSPEND:
  314. return "a_suspend";
  315. case OTG_STATE_A_PERIPHERAL:
  316. return "a_peripheral";
  317. case OTG_STATE_A_WAIT_VFALL:
  318. return "a_wait_vfall";
  319. case OTG_STATE_A_VBUS_ERR:
  320. return "a_vbus_err";
  321. case OTG_STATE_B_IDLE:
  322. return "b_idle";
  323. case OTG_STATE_B_SRP_INIT:
  324. return "b_srp_init";
  325. case OTG_STATE_B_PERIPHERAL:
  326. return "b_peripheral";
  327. case OTG_STATE_B_WAIT_ACON:
  328. return "b_wait_acon";
  329. case OTG_STATE_B_HOST:
  330. return "b_host";
  331. default:
  332. return "UNDEFINED";
  333. }
  334. }
  335. EXPORT_SYMBOL(otg_state_string);