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