Sat Aug 8 10:38:17 2009 UTC ()
Register with the PMF, and when detaching, just the right order.


(mbalmer)
diff -r1.10 -r1.11 src/sys/dev/gpio/gpioow.c

cvs diff -r1.10 -r1.11 src/sys/dev/gpio/gpioow.c (switch to unified diff)

--- src/sys/dev/gpio/gpioow.c 2009/08/07 08:15:52 1.10
+++ src/sys/dev/gpio/gpioow.c 2009/08/08 10:38:17 1.11
@@ -1,247 +1,252 @@ @@ -1,247 +1,252 @@
1/* $NetBSD: gpioow.c,v 1.10 2009/08/07 08:15:52 mbalmer Exp $ */ 1/* $NetBSD: gpioow.c,v 1.11 2009/08/08 10:38:17 mbalmer Exp $ */
2/* $OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */ 2/* $OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org> 5 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
6 * 6 *
7 * Permission to use, copy, modify, and distribute this software for any 7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above 8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies. 9 * copyright notice and this permission notice appear in all copies.
10 * 10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */ 18 */
19 19
20#include <sys/cdefs.h> 20#include <sys/cdefs.h>
21__KERNEL_RCSID(0, "$NetBSD: gpioow.c,v 1.10 2009/08/07 08:15:52 mbalmer Exp $"); 21__KERNEL_RCSID(0, "$NetBSD: gpioow.c,v 1.11 2009/08/08 10:38:17 mbalmer Exp $");
22 22
23/* 23/*
24 * 1-Wire bus bit-banging through GPIO pin. 24 * 1-Wire bus bit-banging through GPIO pin.
25 */ 25 */
26 26
27#include <sys/param.h> 27#include <sys/param.h>
28#include <sys/systm.h> 28#include <sys/systm.h>
29#include <sys/device.h> 29#include <sys/device.h>
30#include <sys/gpio.h> 30#include <sys/gpio.h>
31 31
32#include <dev/gpio/gpiovar.h> 32#include <dev/gpio/gpiovar.h>
33 33
34#include <dev/onewire/onewirevar.h> 34#include <dev/onewire/onewirevar.h>
35 35
36#define GPIOOW_NPINS 1 36#define GPIOOW_NPINS 1
37#define GPIOOW_PIN_DATA 0 37#define GPIOOW_PIN_DATA 0
38 38
39struct gpioow_softc { 39struct gpioow_softc {
40 void * sc_gpio; 40 void * sc_gpio;
41 struct gpio_pinmap sc_map; 41 struct gpio_pinmap sc_map;
42 int _map[GPIOOW_NPINS]; 42 int _map[GPIOOW_NPINS];
43 43
44 struct onewire_bus sc_ow_bus; 44 struct onewire_bus sc_ow_bus;
45 device_t sc_ow_dev; 45 device_t sc_ow_dev;
46 46
47 int sc_data; 47 int sc_data;
48 int sc_dying; 48 int sc_dying;
49}; 49};
50 50
51int gpioow_match(device_t, cfdata_t, void *); 51int gpioow_match(device_t, cfdata_t, void *);
52void gpioow_attach(device_t, device_t, void *); 52void gpioow_attach(device_t, device_t, void *);
53int gpioow_detach(device_t, int); 53int gpioow_detach(device_t, int);
54int gpioow_activate(device_t, enum devact); 54int gpioow_activate(device_t, enum devact);
55 55
56int gpioow_ow_reset(void *); 56int gpioow_ow_reset(void *);
57int gpioow_ow_bit(void *, int); 57int gpioow_ow_bit(void *, int);
58 58
59void gpioow_bb_rx(void *); 59void gpioow_bb_rx(void *);
60void gpioow_bb_tx(void *); 60void gpioow_bb_tx(void *);
61int gpioow_bb_get(void *); 61int gpioow_bb_get(void *);
62void gpioow_bb_set(void *, int); 62void gpioow_bb_set(void *, int);
63 63
64CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc), 64CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc),
65 gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate); 65 gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate);
66 66
67extern struct cfdriver gpioow_cd; 67extern struct cfdriver gpioow_cd;
68 68
69static const struct onewire_bbops gpioow_bbops = { 69static const struct onewire_bbops gpioow_bbops = {
70 gpioow_bb_rx, 70 gpioow_bb_rx,
71 gpioow_bb_tx, 71 gpioow_bb_tx,
72 gpioow_bb_get, 72 gpioow_bb_get,
73 gpioow_bb_set 73 gpioow_bb_set
74}; 74};
75 75
76int 76int
77gpioow_match(device_t parent, cfdata_t cf, void *aux) 77gpioow_match(device_t parent, cfdata_t cf, void *aux)
78{ 78{
79 struct gpio_attach_args *ga = aux; 79 struct gpio_attach_args *ga = aux;
80 80
81 if (strcmp(ga->ga_dvname, cf->cf_name)) 81 if (strcmp(ga->ga_dvname, cf->cf_name))
82 return 0; 82 return 0;
83 83
84 if (ga->ga_offset == -1) 84 if (ga->ga_offset == -1)
85 return 0; 85 return 0;
86 86
87 /* Check that we have enough pins */ 87 /* Check that we have enough pins */
88 if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) { 88 if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
89 aprint_debug("%s: invalid pin mask 0x%02x/n", cf->cf_name, 89 aprint_debug("%s: invalid pin mask 0x%02x/n", cf->cf_name,
90 ga->ga_mask); 90 ga->ga_mask);
91 return 0; 91 return 0;
92 } 92 }
93 return 1; 93 return 1;
94} 94}
95 95
96void 96void
97gpioow_attach(device_t parent, device_t self, void *aux) 97gpioow_attach(device_t parent, device_t self, void *aux)
98{ 98{
99 struct gpioow_softc *sc = device_private(self); 99 struct gpioow_softc *sc = device_private(self);
100 struct gpio_attach_args *ga = aux; 100 struct gpio_attach_args *ga = aux;
101 struct onewirebus_attach_args oba; 101 struct onewirebus_attach_args oba;
102 int caps; 102 int caps;
103 103
104 /* Map pins */ 104 /* Map pins */
105 sc->sc_gpio = ga->ga_gpio; 105 sc->sc_gpio = ga->ga_gpio;
106 sc->sc_map.pm_map = sc->_map; 106 sc->sc_map.pm_map = sc->_map;
107 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 107 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
108 &sc->sc_map)) { 108 &sc->sc_map)) {
109 aprint_error(": can't map pins\n"); 109 aprint_error(": can't map pins\n");
110 return; 110 return;
111 } 111 }
112 112
113 /* Configure data pin */ 113 /* Configure data pin */
114 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA); 114 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
115 if (!(caps & GPIO_PIN_OUTPUT)) { 115 if (!(caps & GPIO_PIN_OUTPUT)) {
116 aprint_error(": data pin is unable to drive output\n"); 116 aprint_error(": data pin is unable to drive output\n");
117 goto fail; 117 goto fail;
118 } 118 }
119 if (!(caps & GPIO_PIN_INPUT)) { 119 if (!(caps & GPIO_PIN_INPUT)) {
120 aprint_error(": data pin is unable to read input\n"); 120 aprint_error(": data pin is unable to read input\n");
121 goto fail; 121 goto fail;
122 } 122 }
123 aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]); 123 aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
124 sc->sc_data = GPIO_PIN_OUTPUT; 124 sc->sc_data = GPIO_PIN_OUTPUT;
125 if (caps & GPIO_PIN_OPENDRAIN) { 125 if (caps & GPIO_PIN_OPENDRAIN) {
126 aprint_normal(" open-drain"); 126 aprint_normal(" open-drain");
127 sc->sc_data |= GPIO_PIN_OPENDRAIN; 127 sc->sc_data |= GPIO_PIN_OPENDRAIN;
128 } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) { 128 } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
129 aprint_normal(" push-pull tri-state"); 129 aprint_normal(" push-pull tri-state");
130 sc->sc_data |= GPIO_PIN_PUSHPULL; 130 sc->sc_data |= GPIO_PIN_PUSHPULL;
131 } 131 }
132 if (caps & GPIO_PIN_PULLUP) { 132 if (caps & GPIO_PIN_PULLUP) {
133 aprint_normal(" pull-up"); 133 aprint_normal(" pull-up");
134 sc->sc_data |= GPIO_PIN_PULLUP; 134 sc->sc_data |= GPIO_PIN_PULLUP;
135 } 135 }
136 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data); 136 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
137 137
138 aprint_normal("\n"); 138 aprint_normal("\n");
139 139
140 /* Attach 1-Wire bus */ 140 /* Attach 1-Wire bus */
141 sc->sc_ow_bus.bus_cookie = sc; 141 sc->sc_ow_bus.bus_cookie = sc;
142 sc->sc_ow_bus.bus_reset = gpioow_ow_reset; 142 sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
143 sc->sc_ow_bus.bus_bit = gpioow_ow_bit; 143 sc->sc_ow_bus.bus_bit = gpioow_ow_bit;
144 144
145 memset(&oba, 0, sizeof(oba)); 145 memset(&oba, 0, sizeof(oba));
146 oba.oba_bus = &sc->sc_ow_bus; 146 oba.oba_bus = &sc->sc_ow_bus;
147 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print); 147 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
148 148
 149 if (!pmf_device_register(self, NULL, NULL))
 150 aprint_error("%s: could not establish power handler\n",
 151 device_xname(self));
149 return; 152 return;
150 153
151fail: 154fail:
152 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 155 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
153} 156}
154 157
155int 158int
156gpioow_detach(device_t self, int flags) 159gpioow_detach(device_t self, int flags)
157{ 160{
158 struct gpioow_softc *sc = device_private(self); 161 struct gpioow_softc *sc = device_private(self);
159 int rv = 0; 162 int rv = 0;
160 163
161 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 
162 
163 if (sc->sc_ow_dev != NULL) 164 if (sc->sc_ow_dev != NULL)
164 rv = config_detach(sc->sc_ow_dev, flags); 165 rv = config_detach(sc->sc_ow_dev, flags);
165 166
 167 if (!rv) {
 168 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
 169 pmf_device_deregister(self);
 170 }
166 return rv; 171 return rv;
167} 172}
168 173
169int 174int
170gpioow_activate(device_t self, enum devact act) 175gpioow_activate(device_t self, enum devact act)
171{ 176{
172 struct gpioow_softc *sc = device_private(self); 177 struct gpioow_softc *sc = device_private(self);
173 int rv = 0; 178 int rv = 0;
174 179
175 switch (act) { 180 switch (act) {
176 case DVACT_ACTIVATE: 181 case DVACT_ACTIVATE:
177 return EOPNOTSUPP; 182 return EOPNOTSUPP;
178 case DVACT_DEACTIVATE: 183 case DVACT_DEACTIVATE:
179 sc->sc_dying = 1; 184 sc->sc_dying = 1;
180 if (sc->sc_ow_dev != NULL) 185 if (sc->sc_ow_dev != NULL)
181 rv = config_deactivate(sc->sc_ow_dev); 186 rv = config_deactivate(sc->sc_ow_dev);
182 break; 187 break;
183 } 188 }
184 return rv; 189 return rv;
185} 190}
186 191
187int 192int
188gpioow_ow_reset(void *arg) 193gpioow_ow_reset(void *arg)
189{ 194{
190 return (onewire_bb_reset(&gpioow_bbops, arg)); 195 return (onewire_bb_reset(&gpioow_bbops, arg));
191} 196}
192 197
193int 198int
194gpioow_ow_bit(void *arg, int value) 199gpioow_ow_bit(void *arg, int value)
195{ 200{
196 return (onewire_bb_bit(&gpioow_bbops, arg, value)); 201 return (onewire_bb_bit(&gpioow_bbops, arg, value));
197} 202}
198 203
199void 204void
200gpioow_bb_rx(void *arg) 205gpioow_bb_rx(void *arg)
201{ 206{
202 struct gpioow_softc *sc = arg; 207 struct gpioow_softc *sc = arg;
203 int data = sc->sc_data; 208 int data = sc->sc_data;
204 209
205 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 210 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
206 data |= GPIO_PIN_INPUT; 211 data |= GPIO_PIN_INPUT;
207 if (data & GPIO_PIN_PUSHPULL) 212 if (data & GPIO_PIN_PUSHPULL)
208 data |= GPIO_PIN_TRISTATE; 213 data |= GPIO_PIN_TRISTATE;
209 if (sc->sc_data != data) { 214 if (sc->sc_data != data) {
210 sc->sc_data = data; 215 sc->sc_data = data;
211 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 216 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
212 sc->sc_data); 217 sc->sc_data);
213 } 218 }
214} 219}
215 220
216void 221void
217gpioow_bb_tx(void *arg) 222gpioow_bb_tx(void *arg)
218{ 223{
219 struct gpioow_softc *sc = arg; 224 struct gpioow_softc *sc = arg;
220 int data = sc->sc_data; 225 int data = sc->sc_data;
221 226
222 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 227 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
223 data |= GPIO_PIN_OUTPUT; 228 data |= GPIO_PIN_OUTPUT;
224 if (sc->sc_data != data) { 229 if (sc->sc_data != data) {
225 sc->sc_data = data; 230 sc->sc_data = data;
226 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 231 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
227 sc->sc_data); 232 sc->sc_data);
228 } 233 }
229} 234}
230 235
231int 236int
232gpioow_bb_get(void *arg) 237gpioow_bb_get(void *arg)
233{ 238{
234 struct gpioow_softc *sc = arg; 239 struct gpioow_softc *sc = arg;
235 240
236 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) == 241 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
237 GPIO_PIN_HIGH ? 1 : 0); 242 GPIO_PIN_HIGH ? 1 : 0);
238} 243}
239 244
240void 245void
241gpioow_bb_set(void *arg, int value) 246gpioow_bb_set(void *arg, int value)
242{ 247{
243 struct gpioow_softc *sc = arg; 248 struct gpioow_softc *sc = arg;
244 249
245 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 250 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
246 value ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 251 value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
247} 252}