xref: /freebsd/sys/dev/phy/phy_usb.c (revision 950a6087)
1*950a6087SEmmanuel Vadot /*-
2*950a6087SEmmanuel Vadot  * Copyright 2018 Michal Meloun <mmel@FreeBSD.org>
3*950a6087SEmmanuel Vadot  * All rights reserved.
4*950a6087SEmmanuel Vadot  *
5*950a6087SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6*950a6087SEmmanuel Vadot  * modification, are permitted provided that the following conditions
7*950a6087SEmmanuel Vadot  * are met:
8*950a6087SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9*950a6087SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10*950a6087SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11*950a6087SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12*950a6087SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13*950a6087SEmmanuel Vadot  *
14*950a6087SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*950a6087SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*950a6087SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*950a6087SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*950a6087SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*950a6087SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*950a6087SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*950a6087SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*950a6087SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*950a6087SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*950a6087SEmmanuel Vadot  * SUCH DAMAGE.
25*950a6087SEmmanuel Vadot  */
26*950a6087SEmmanuel Vadot 
27*950a6087SEmmanuel Vadot #include <sys/param.h>
28*950a6087SEmmanuel Vadot #include <sys/systm.h>
29*950a6087SEmmanuel Vadot #include <sys/kernel.h>
30*950a6087SEmmanuel Vadot #include <sys/lock.h>
31*950a6087SEmmanuel Vadot #include <sys/queue.h>
32*950a6087SEmmanuel Vadot #include <sys/sx.h>
33*950a6087SEmmanuel Vadot 
34*950a6087SEmmanuel Vadot 
35*950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
36*950a6087SEmmanuel Vadot #include <dev/phy/phy_internal.h>
37*950a6087SEmmanuel Vadot 
38*950a6087SEmmanuel Vadot /*
39*950a6087SEmmanuel Vadot  * USB phy controller methods.
40*950a6087SEmmanuel Vadot  */
41*950a6087SEmmanuel Vadot static phynode_usb_method_t phynode_usb_methods[] = {
42*950a6087SEmmanuel Vadot 
43*950a6087SEmmanuel Vadot 	PHYNODEUSBMETHOD_END
44*950a6087SEmmanuel Vadot };
45*950a6087SEmmanuel Vadot DEFINE_CLASS_1(phynode_usb, phynode_usb_class, phynode_usb_methods,
46*950a6087SEmmanuel Vadot     0, phynode_class);
47*950a6087SEmmanuel Vadot 
48*950a6087SEmmanuel Vadot /*
49*950a6087SEmmanuel Vadot  * Create and initialize phy object, but do not register it.
50*950a6087SEmmanuel Vadot  */
51*950a6087SEmmanuel Vadot struct phynode *
phynode_usb_create(device_t pdev,phynode_class_t phynode_class,struct phynode_usb_init_def * def)52*950a6087SEmmanuel Vadot phynode_usb_create(device_t pdev, phynode_class_t phynode_class,
53*950a6087SEmmanuel Vadot     struct phynode_usb_init_def *def)
54*950a6087SEmmanuel Vadot 
55*950a6087SEmmanuel Vadot {
56*950a6087SEmmanuel Vadot 	struct phynode *phynode;
57*950a6087SEmmanuel Vadot 	struct phynode_usb_sc *sc;
58*950a6087SEmmanuel Vadot 
59*950a6087SEmmanuel Vadot 	phynode = phynode_create(pdev, phynode_class, &def->phynode_init_def);
60*950a6087SEmmanuel Vadot 	if (phynode == NULL)
61*950a6087SEmmanuel Vadot 		return (NULL);
62*950a6087SEmmanuel Vadot 	sc = phynode_get_softc(phynode);
63*950a6087SEmmanuel Vadot 	sc->std_param = def->std_param;
64*950a6087SEmmanuel Vadot 	return (phynode);
65*950a6087SEmmanuel Vadot }
66*950a6087SEmmanuel Vadot 
67*950a6087SEmmanuel Vadot struct phynode
phynode_usb_register(struct phynode * phynode)68*950a6087SEmmanuel Vadot *phynode_usb_register(struct phynode *phynode)
69*950a6087SEmmanuel Vadot {
70*950a6087SEmmanuel Vadot 
71*950a6087SEmmanuel Vadot 	return (phynode_register(phynode));
72*950a6087SEmmanuel Vadot }
73*950a6087SEmmanuel Vadot 
74*950a6087SEmmanuel Vadot /* --------------------------------------------------------------------------
75*950a6087SEmmanuel Vadot  *
76*950a6087SEmmanuel Vadot  * Real consumers executive
77*950a6087SEmmanuel Vadot  *
78*950a6087SEmmanuel Vadot  */
79*950a6087SEmmanuel Vadot 
80*950a6087SEmmanuel Vadot /*
81*950a6087SEmmanuel Vadot  * Set USB phy mode. (PHY_USB_MODE_*)
82*950a6087SEmmanuel Vadot  */
83*950a6087SEmmanuel Vadot int
phynode_usb_set_mode(struct phynode * phynode,int usb_mode)84*950a6087SEmmanuel Vadot phynode_usb_set_mode(struct phynode *phynode, int usb_mode)
85*950a6087SEmmanuel Vadot {
86*950a6087SEmmanuel Vadot 	int rv;
87*950a6087SEmmanuel Vadot 
88*950a6087SEmmanuel Vadot 	PHY_TOPO_ASSERT();
89*950a6087SEmmanuel Vadot 
90*950a6087SEmmanuel Vadot 	PHYNODE_XLOCK(phynode);
91*950a6087SEmmanuel Vadot 	rv = PHYNODE_USB_SET_MODE(phynode, usb_mode);
92*950a6087SEmmanuel Vadot 	PHYNODE_UNLOCK(phynode);
93*950a6087SEmmanuel Vadot 	return (rv);
94*950a6087SEmmanuel Vadot }
95*950a6087SEmmanuel Vadot 
96*950a6087SEmmanuel Vadot /*
97*950a6087SEmmanuel Vadot  * Get USB phy mode. (PHY_USB_MODE_*)
98*950a6087SEmmanuel Vadot  */
99*950a6087SEmmanuel Vadot int
phynode_usb_get_mode(struct phynode * phynode,int * usb_mode)100*950a6087SEmmanuel Vadot phynode_usb_get_mode(struct phynode *phynode, int *usb_mode)
101*950a6087SEmmanuel Vadot {
102*950a6087SEmmanuel Vadot 	int rv;
103*950a6087SEmmanuel Vadot 
104*950a6087SEmmanuel Vadot 	PHY_TOPO_ASSERT();
105*950a6087SEmmanuel Vadot 
106*950a6087SEmmanuel Vadot 	PHYNODE_XLOCK(phynode);
107*950a6087SEmmanuel Vadot 	rv = PHYNODE_USB_GET_MODE(phynode, usb_mode);
108*950a6087SEmmanuel Vadot 	PHYNODE_UNLOCK(phynode);
109*950a6087SEmmanuel Vadot 	return (rv);
110*950a6087SEmmanuel Vadot }
111*950a6087SEmmanuel Vadot 
112*950a6087SEmmanuel Vadot  /* --------------------------------------------------------------------------
113*950a6087SEmmanuel Vadot  *
114*950a6087SEmmanuel Vadot  * USB phy consumers interface.
115*950a6087SEmmanuel Vadot  *
116*950a6087SEmmanuel Vadot  */
phy_usb_set_mode(phy_t phy,int usb_mode)117*950a6087SEmmanuel Vadot int phy_usb_set_mode(phy_t phy, int usb_mode)
118*950a6087SEmmanuel Vadot {
119*950a6087SEmmanuel Vadot 	int rv;
120*950a6087SEmmanuel Vadot 	struct phynode *phynode;
121*950a6087SEmmanuel Vadot 
122*950a6087SEmmanuel Vadot 	phynode = phy->phynode;
123*950a6087SEmmanuel Vadot 	KASSERT(phynode->ref_cnt > 0,
124*950a6087SEmmanuel Vadot 	   ("Attempt to access unreferenced phy.\n"));
125*950a6087SEmmanuel Vadot 
126*950a6087SEmmanuel Vadot 	PHY_TOPO_SLOCK();
127*950a6087SEmmanuel Vadot 	rv = phynode_usb_set_mode(phynode, usb_mode);
128*950a6087SEmmanuel Vadot 	PHY_TOPO_UNLOCK();
129*950a6087SEmmanuel Vadot 	return (rv);
130*950a6087SEmmanuel Vadot }
131*950a6087SEmmanuel Vadot 
phy_usb_get_mode(phy_t phy,int * usb_mode)132*950a6087SEmmanuel Vadot int phy_usb_get_mode(phy_t phy, int *usb_mode)
133*950a6087SEmmanuel Vadot {
134*950a6087SEmmanuel Vadot 	int rv;
135*950a6087SEmmanuel Vadot 	struct phynode *phynode;
136*950a6087SEmmanuel Vadot 
137*950a6087SEmmanuel Vadot 	phynode = phy->phynode;
138*950a6087SEmmanuel Vadot 	KASSERT(phynode->ref_cnt > 0,
139*950a6087SEmmanuel Vadot 	   ("Attempt to access unreferenced phy.\n"));
140*950a6087SEmmanuel Vadot 
141*950a6087SEmmanuel Vadot 	PHY_TOPO_SLOCK();
142*950a6087SEmmanuel Vadot 	rv = phynode_usb_get_mode(phynode, usb_mode);
143*950a6087SEmmanuel Vadot 	PHY_TOPO_UNLOCK();
144*950a6087SEmmanuel Vadot 	return (rv);
145*950a6087SEmmanuel Vadot }
146