blob: 801e597a15413ece58964805e494c959315b26a1 [file] [log] [blame]
Tony Lindgren3cb22d62008-11-24 12:02:21 -08001/*
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
12#include <linux/kernel.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040013#include <linux/export.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080014#include <linux/device.h>
15
16#include <linux/usb/otg.h>
17
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020018static struct usb_phy *phy;
Tony Lindgren3cb22d62008-11-24 12:02:21 -080019
20/**
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020021 * usb_get_transceiver - find the (single) USB transceiver
Tony Lindgren3cb22d62008-11-24 12:02:21 -080022 *
23 * Returns the transceiver driver, after getting a refcount to it; or
24 * null if there is no such transceiver. The caller is responsible for
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020025 * calling usb_put_transceiver() to release that count.
Tony Lindgren3cb22d62008-11-24 12:02:21 -080026 *
27 * For use by USB host and peripheral drivers.
28 */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020029struct usb_phy *usb_get_transceiver(void)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080030{
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020031 if (phy)
32 get_device(phy->dev);
33 return phy;
Tony Lindgren3cb22d62008-11-24 12:02:21 -080034}
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020035EXPORT_SYMBOL(usb_get_transceiver);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080036
37/**
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020038 * usb_put_transceiver - release the (single) USB transceiver
39 * @x: the transceiver returned by usb_get_transceiver()
Tony Lindgren3cb22d62008-11-24 12:02:21 -080040 *
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020041 * Releases a refcount the caller received from usb_get_transceiver().
Tony Lindgren3cb22d62008-11-24 12:02:21 -080042 *
43 * For use by USB host and peripheral drivers.
44 */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020045void usb_put_transceiver(struct usb_phy *x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080046{
Robert Jarzmikecf85e42009-04-21 20:33:10 -070047 if (x)
48 put_device(x->dev);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080049}
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020050EXPORT_SYMBOL(usb_put_transceiver);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080051
52/**
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020053 * usb_set_transceiver - declare the (single) USB transceiver
54 * @x: the USB transceiver to be used; or NULL
Tony Lindgren3cb22d62008-11-24 12:02:21 -080055 *
56 * This call is exclusively for use by transceiver drivers, which
57 * coordinate the activities of drivers for host and peripheral
58 * controllers, and in some cases for VBUS current regulation.
59 */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020060int usb_set_transceiver(struct usb_phy *x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080061{
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020062 if (phy && x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080063 return -EBUSY;
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020064 phy = x;
Tony Lindgren3cb22d62008-11-24 12:02:21 -080065 return 0;
66}
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020067EXPORT_SYMBOL(usb_set_transceiver);
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +020068
69const char *otg_state_string(enum usb_otg_state state)
70{
71 switch (state) {
72 case OTG_STATE_A_IDLE:
73 return "a_idle";
74 case OTG_STATE_A_WAIT_VRISE:
75 return "a_wait_vrise";
76 case OTG_STATE_A_WAIT_BCON:
77 return "a_wait_bcon";
78 case OTG_STATE_A_HOST:
79 return "a_host";
80 case OTG_STATE_A_SUSPEND:
81 return "a_suspend";
82 case OTG_STATE_A_PERIPHERAL:
83 return "a_peripheral";
84 case OTG_STATE_A_WAIT_VFALL:
85 return "a_wait_vfall";
86 case OTG_STATE_A_VBUS_ERR:
87 return "a_vbus_err";
88 case OTG_STATE_B_IDLE:
89 return "b_idle";
90 case OTG_STATE_B_SRP_INIT:
91 return "b_srp_init";
92 case OTG_STATE_B_PERIPHERAL:
93 return "b_peripheral";
94 case OTG_STATE_B_WAIT_ACON:
95 return "b_wait_acon";
96 case OTG_STATE_B_HOST:
97 return "b_host";
98 default:
99 return "UNDEFINED";
100 }
101}
102EXPORT_SYMBOL(otg_state_string);