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