xref: /freebsd-13.1/sys/dev/extres/phy/phy_usb.c (revision f9f020a4)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/queue.h>
34 #include <sys/sx.h>
35 
36 
37 #include <dev/extres/phy/phy_usb.h>
38 #include <dev/extres/phy/phy_internal.h>
39 
40 #include "phydev_if.h"
41 
42 /*
43  * USB phy controller methods.
44  */
45 static phynode_usb_method_t phynode_usb_methods[] = {
46 
47 	PHYNODEUSBMETHOD_END
48 };
49 DEFINE_CLASS_1(phynode_usb, phynode_usb_class, phynode_usb_methods,
50     0, phynode_class);
51 
52 /*
53  * Create and initialize phy object, but do not register it.
54  */
55 struct phynode *
phynode_usb_create(device_t pdev,phynode_class_t phynode_class,struct phynode_usb_init_def * def)56 phynode_usb_create(device_t pdev, phynode_class_t phynode_class,
57     struct phynode_usb_init_def *def)
58 
59 {
60 	struct phynode *phynode;
61 	struct phynode_usb_sc *sc;
62 
63 	phynode = phynode_create(pdev, phynode_class, &def->phynode_init_def);
64 	if (phynode == NULL)
65 		return (NULL);
66 	sc = phynode_get_softc(phynode);
67 	sc->std_param = def->std_param;
68 	return (phynode);
69 }
70 
71 struct phynode
phynode_usb_register(struct phynode * phynode)72 *phynode_usb_register(struct phynode *phynode)
73 {
74 
75 	return (phynode_register(phynode));
76 }
77 
78 /* --------------------------------------------------------------------------
79  *
80  * Real consumers executive
81  *
82  */
83 
84 /*
85  * Set USB phy mode. (PHY_USB_MODE_*)
86  */
87 int
phynode_usb_set_mode(struct phynode * phynode,int usb_mode)88 phynode_usb_set_mode(struct phynode *phynode, int usb_mode)
89 {
90 	int rv;
91 
92 	PHY_TOPO_ASSERT();
93 
94 	PHYNODE_XLOCK(phynode);
95 	rv = PHYNODE_USB_SET_MODE(phynode, usb_mode);
96 	PHYNODE_UNLOCK(phynode);
97 	return (rv);
98 }
99 
100 /*
101  * Get USB phy mode. (PHY_USB_MODE_*)
102  */
103 int
phynode_usb_get_mode(struct phynode * phynode,int * usb_mode)104 phynode_usb_get_mode(struct phynode *phynode, int *usb_mode)
105 {
106 	int rv;
107 
108 	PHY_TOPO_ASSERT();
109 
110 	PHYNODE_XLOCK(phynode);
111 	rv = PHYNODE_USB_GET_MODE(phynode, usb_mode);
112 	PHYNODE_UNLOCK(phynode);
113 	return (rv);
114 }
115 
116  /* --------------------------------------------------------------------------
117  *
118  * USB phy consumers interface.
119  *
120  */
phy_usb_set_mode(phy_t phy,int usb_mode)121 int phy_usb_set_mode(phy_t phy, int usb_mode)
122 {
123 	int rv;
124 	struct phynode *phynode;
125 
126 	phynode = phy->phynode;
127 	KASSERT(phynode->ref_cnt > 0,
128 	   ("Attempt to access unreferenced phy.\n"));
129 
130 	PHY_TOPO_SLOCK();
131 	rv = phynode_usb_set_mode(phynode, usb_mode);
132 	PHY_TOPO_UNLOCK();
133 	return (rv);
134 }
135 
phy_usb_get_mode(phy_t phy,int * usb_mode)136 int phy_usb_get_mode(phy_t phy, int *usb_mode)
137 {
138 	int rv;
139 	struct phynode *phynode;
140 
141 	phynode = phy->phynode;
142 	KASSERT(phynode->ref_cnt > 0,
143 	   ("Attempt to access unreferenced phy.\n"));
144 
145 	PHY_TOPO_SLOCK();
146 	rv = phynode_usb_get_mode(phynode, usb_mode);
147 	PHY_TOPO_UNLOCK();
148 	return (rv);
149 }
150