]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ow/ow.c
Regularize my copyright notice
[FreeBSD/FreeBSD.git] / sys / dev / ow / ow.c
1 /*-
2  * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice unmodified, this list of conditions, and the following
9  *    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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32
33 #include <sys/bus.h>
34 #include <sys/errno.h>
35 #include <sys/libkern.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41
42 #include <dev/ow/ow.h>
43 #include <dev/ow/owll.h>
44 #include <dev/ow/own.h>
45
46 /*
47  * lldev - link level device
48  * ndev - network / transport device (this module)
49  * pdev - presentation device (children of this module)
50  */
51
52 typedef int ow_enum_fn(device_t, device_t);
53 typedef int ow_found_fn(device_t, romid_t);
54
55 struct ow_softc
56 {
57         device_t        dev;            /* Newbus driver back pointer */
58         struct mtx      mtx;            /* bus mutex */
59         device_t        owner;          /* bus owner, if != NULL */
60 };
61
62 struct ow_devinfo
63 {
64         romid_t romid;
65 };
66
67 static int ow_acquire_bus(device_t ndev, device_t pdev, int how);
68 static void ow_release_bus(device_t ndev, device_t pdev);
69
70 #define OW_LOCK(_sc) mtx_lock(&(_sc)->mtx)
71 #define OW_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
72 #define OW_LOCK_DESTROY(_sc) mtx_destroy(&_sc->mtx)
73 #define OW_ASSERT_LOCKED(_sc) mtx_assert(&_sc->mtx, MA_OWNED)
74 #define OW_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->mtx, MA_NOTOWNED)
75
76 static MALLOC_DEFINE(M_OW, "ow", "House keeping data for 1wire bus");
77
78 static const struct ow_timing timing_regular_min = {
79         .t_slot = 60,
80         .t_low0 = 60,
81         .t_low1 = 1,
82         .t_release = 0,
83         .t_rec = 1,
84         .t_rdv = 15,            /* fixed */
85         .t_rstl = 480,
86         .t_rsth = 480,
87         .t_pdl = 60,
88         .t_pdh = 15,
89         .t_lowr = 1,
90 };
91
92 static const struct ow_timing timing_regular_max = {
93         .t_slot = 120,
94         .t_low0 = 120,
95         .t_low1 = 15,
96         .t_release = 45,
97         .t_rec = 960,           /* infinity */
98         .t_rdv = 15,            /* fixed */
99         .t_rstl = 960,          /* infinity */
100         .t_rsth = 960,          /* infinity */
101         .t_pdl = 240,           /* 60us to 240us */
102         .t_pdh = 60,            /* 15us to 60us */
103         .t_lowr = 15,           /* 1us */
104 };
105
106 static struct ow_timing timing_regular = {
107         .t_slot = 60,           /*  60 <= t < 120 */
108         .t_low0 = 60,           /*  60 <= t < t_slot < 120 */
109         .t_low1 = 1,            /*   1 <= t < 15 */
110         .t_release = 45,        /*   0 <= t < 45 */
111         .t_rec = 15,            /*   1 <= t < inf */
112         .t_rdv = 15,            /* t == 15 */
113         .t_rstl = 480,          /* 480 <= t < inf */
114         .t_rsth = 480,          /* 480 <= t < inf */
115         .t_pdl = 60,            /*  60 <= t < 240 */
116         .t_pdh = 60,            /*  15 <= t < 60 */
117         .t_lowr = 1,            /*   1 <= t < 15 */
118 };
119
120 /* NB: Untested */
121 static const struct ow_timing timing_overdrive_min = {
122         .t_slot = 6,
123         .t_low0 = 6,
124         .t_low1 = 1,
125         .t_release = 0,
126         .t_rec = 1,
127         .t_rdv = 2,             /* fixed */
128         .t_rstl = 48,
129         .t_rsth = 48,
130         .t_pdl = 8,
131         .t_pdh = 2,
132         .t_lowr = 1,
133 };
134
135 static const struct ow_timing timing_overdrive_max = {
136         .t_slot = 16,
137         .t_low0 = 16,
138         .t_low1 = 2,
139         .t_release = 4,
140         .t_rec = 960,           /* infinity */
141         .t_rdv = 2,             /* fixed */
142         .t_rstl = 80,
143         .t_rsth = 960,          /* infinity */
144         .t_pdl = 24,
145         .t_pdh = 6,
146         .t_lowr = 2,
147 };
148
149 static struct ow_timing timing_overdrive = {
150         .t_slot = 11,           /* 6 <= t < 16 */
151         .t_low0 = 6,            /* 6 <= t < t_slot < 16 */
152         .t_low1 = 1,            /* 1 <= t < 2 */
153         .t_release = 4,         /* 0 <= t < 4 */
154         .t_rec = 1,             /* 1 <= t < inf */
155         .t_rdv = 2,             /* t == 2 */
156         .t_rstl = 48,           /* 48 <= t < 80 */
157         .t_rsth = 48,           /* 48 <= t < inf */
158         .t_pdl = 8,             /* 8 <= t < 24 */
159         .t_pdh = 2,             /* 2 <= t < 6 */
160         .t_lowr = 1,            /* 1 <= t < 2 */
161 };
162
163 SYSCTL_NODE(_hw, OID_AUTO, ow, CTLFLAG_RD, 0, "1-Wire protocol");
164 SYSCTL_NODE(_hw_ow, OID_AUTO, regular, CTLFLAG_RD, 0,
165     "Regular mode timings");
166 SYSCTL_NODE(_hw_ow, OID_AUTO, overdrive, CTLFLAG_RD, 0,
167     "Overdrive mode timings");
168
169 #define _OW_TIMING_SYSCTL(mode, param)          \
170     static int \
171     sysctl_ow_timing_ ## mode ## _ ## param(SYSCTL_HANDLER_ARGS) \
172     { \
173             int val = timing_ ## mode.param; \
174             int err; \
175             err = sysctl_handle_int(oidp, &val, 0, req); \
176             if (err != 0 || req->newptr == NULL) \
177                 return (err); \
178             if (val < timing_ ## mode ## _min.param) \
179                 return (EINVAL); \
180             else if (val >= timing_ ## mode ## _max.param) \
181                 return (EINVAL); \
182             timing_ ## mode.param = val; \
183             return (0); \
184     } \
185 SYSCTL_PROC(_hw_ow_ ## mode, OID_AUTO, param, \
186     CTLTYPE_INT | CTLFLAG_RWTUN, 0, sizeof(int), \
187     sysctl_ow_timing_ ## mode ## _ ## param, "I", \
188     "1-Wire timing parameter in microseconds (-1 resets to default)")
189
190 #define OW_TIMING_SYSCTL(param) \
191     _OW_TIMING_SYSCTL(regular, param); \
192     _OW_TIMING_SYSCTL(overdrive, param)
193
194 OW_TIMING_SYSCTL(t_slot);
195 OW_TIMING_SYSCTL(t_low0);
196 OW_TIMING_SYSCTL(t_low1);
197 OW_TIMING_SYSCTL(t_release);
198 OW_TIMING_SYSCTL(t_rec);
199 OW_TIMING_SYSCTL(t_rdv);
200 OW_TIMING_SYSCTL(t_rstl);
201 OW_TIMING_SYSCTL(t_rsth);
202 OW_TIMING_SYSCTL(t_pdl);
203 OW_TIMING_SYSCTL(t_pdh);
204 OW_TIMING_SYSCTL(t_lowr);
205
206 #undef _OW_TIMING_SYSCTL
207 #undef OW_TIMING_SYSCTL
208
209 static void
210 ow_send_byte(device_t lldev, struct ow_timing *t, uint8_t byte)
211 {
212         int i;
213
214         for (i = 0; i < 8; i++)
215                 if (byte & (1 << i))
216                         OWLL_WRITE_ONE(lldev, t);
217                 else
218                         OWLL_WRITE_ZERO(lldev, t);
219 }
220
221 static void
222 ow_read_byte(device_t lldev, struct ow_timing *t, uint8_t *bytep)
223 {
224         int i;
225         uint8_t byte = 0;
226         int bit;
227
228         for (i = 0; i < 8; i++) {
229                 OWLL_READ_DATA(lldev, t, &bit);
230                 byte |= bit << i;
231         }
232         *bytep = byte;
233 }
234
235 static int
236 ow_send_command(device_t ndev, device_t pdev, struct ow_cmd *cmd)
237 {
238         int present, i, bit, tries;
239         device_t lldev;
240         struct ow_timing *t;
241
242         lldev = device_get_parent(ndev);
243
244         /*
245          * Retry the reset a couple of times before giving up.
246          */
247         tries = 4;
248         do {
249                 OWLL_RESET_AND_PRESENCE(lldev, &timing_regular, &present);
250                 if (present == 1)
251                         device_printf(ndev, "Reset said no device on bus?.\n");
252         } while (present == 1 && tries-- > 0);
253         if (present == 1) {
254                 device_printf(ndev, "Reset said the device wasn't there.\n");
255                 return ENOENT;          /* No devices acked the RESET */
256         }
257         if (present == -1) {
258                 device_printf(ndev, "Reset discovered bus wired wrong.\n");
259                 return ENOENT;
260         }
261
262         for (i = 0; i < cmd->rom_len; i++)
263                 ow_send_byte(lldev, &timing_regular, cmd->rom_cmd[i]);
264         for (i = 0; i < cmd->rom_read_len; i++)
265                 ow_read_byte(lldev, &timing_regular, cmd->rom_read + i);
266         if (cmd->xpt_len) {
267                 /*
268                  * Per AN937, the reset pulse and ROM level are always
269                  * done with the regular timings. Certain ROM commands
270                  * put the device into overdrive mode for the remainder
271                  * of the data transfer, which is why we have to pass the
272                  * timings here. Commands that need to be handled like this
273                  * are expected to be flagged by the client.
274                  */
275                 t = (cmd->flags & OW_FLAG_OVERDRIVE) ?
276                     &timing_overdrive : &timing_regular;
277                 for (i = 0; i < cmd->xpt_len; i++)
278                         ow_send_byte(lldev, t, cmd->xpt_cmd[i]);
279                 if (cmd->flags & OW_FLAG_READ_BIT) {
280                         memset(cmd->xpt_read, 0, (cmd->xpt_read_len + 7) / 8);
281                         for (i = 0; i < cmd->xpt_read_len; i++) {
282                                 OWLL_READ_DATA(lldev, t, &bit);
283                                 cmd->xpt_read[i / 8] |= bit << (i % 8);
284                         }
285                 } else {
286                         for (i = 0; i < cmd->xpt_read_len; i++)
287                                 ow_read_byte(lldev, t, cmd->xpt_read + i);
288                 }
289         }
290         return 0;
291 }
292
293 static int
294 ow_search_rom(device_t lldev, device_t dev)
295 {
296         struct ow_cmd cmd;
297
298         memset(&cmd, 0, sizeof(cmd));
299         cmd.rom_cmd[0] = SEARCH_ROM;
300         cmd.rom_len = 1;
301         return ow_send_command(lldev, dev, &cmd);
302 }
303
304 #if 0
305 static int
306 ow_alarm_search(device_t lldev, device_t dev)
307 {
308         struct ow_cmd cmd;
309
310         memset(&cmd, 0, sizeof(cmd));
311         cmd.rom_cmd[0] = ALARM_SEARCH;
312         cmd.rom_len = 1;
313         return ow_send_command(lldev, dev, &cmd);
314 }
315 #endif
316
317 static int
318 ow_add_child(device_t dev, romid_t romid)
319 {
320         struct ow_devinfo *di;
321         device_t child;
322
323         di = malloc(sizeof(*di), M_OW, M_WAITOK);
324         di->romid = romid;
325         child = device_add_child(dev, NULL, -1);
326         if (child == NULL) {
327                 free(di, M_OW);
328                 return ENOMEM;
329         }
330         device_set_ivars(child, di);
331         return (0);
332 }
333
334 static device_t
335 ow_child_by_romid(device_t dev, romid_t romid)
336 {
337         device_t *children, retval, child;
338         int nkid, i;
339         struct ow_devinfo *di;
340
341         if (device_get_children(dev, &children, &nkid) != 0)
342                 return (NULL);
343         retval = NULL;
344         for (i = 0; i < nkid; i++) {
345                 child = children[i];
346                 di = device_get_ivars(child);
347                 if (di->romid == romid) {
348                         retval = child;
349                         break;
350                 }
351         }
352         free(children, M_TEMP);
353
354         return (retval);
355 }
356
357 /*
358  * CRC generator table -- taken from AN937 DOW CRC LOOKUP FUNCTION Table 2
359  */
360 const uint8_t ow_crc_table[] = {
361         0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
362         157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
363         35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
364         190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
365         70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
366         219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
367         101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
368         248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
369         140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242,  172, 47, 113,147, 205,
370         17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80,
371         175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238,
372         50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
373         202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
374         87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
375         233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
376         116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
377 };
378
379 /*
380  * Converted from DO_CRC page 131 ANN937
381  */
382 static uint8_t
383 ow_crc(device_t ndev, device_t pdev, uint8_t *buffer, size_t len)
384 {
385         uint8_t crc = 0;
386         int i;
387
388         for (i = 0; i < len; i++)
389                 crc = ow_crc_table[crc ^ buffer[i]];
390         return crc;
391 }
392
393 static int
394 ow_check_crc(romid_t romid)
395 {
396         return ow_crc(NULL, NULL, (uint8_t *)&romid, sizeof(romid)) == 0;
397 }
398
399 static int
400 ow_device_found(device_t dev, romid_t romid)
401 {
402
403         /* XXX Move this up into enumerate? */
404         /*
405          * All valid ROM IDs have a valid CRC. Check that first.
406          */
407         if (!ow_check_crc(romid)) {
408                 device_printf(dev, "Device romid %8D failed CRC.\n",
409                     &romid, ":");
410                 return EINVAL;
411         }
412
413         /*
414          * If we've seen this child before, don't add a new one for it.
415          */
416         if (ow_child_by_romid(dev, romid) != NULL)
417                 return 0;
418
419         return ow_add_child(dev, romid);
420 }
421
422 static int
423 ow_enumerate(device_t dev, ow_enum_fn *enumfp, ow_found_fn *foundfp)
424 {
425         device_t lldev = device_get_parent(dev);
426         int first, second, i, dir, prior, last, err, retries;
427         uint64_t probed, last_mask;
428         int sanity = 10;
429
430         prior = -1;
431         last_mask = 0;
432         retries = 0;
433         last = -2;
434         err = ow_acquire_bus(dev, dev, OWN_DONTWAIT);
435         if (err != 0)
436                 return err;
437         while (last != -1) {
438                 if (sanity-- < 0) {
439                         printf("Reached the sanity limit\n");
440                         return EIO;
441                 }
442 again:
443                 probed = 0;
444                 last = -1;
445
446                 /*
447                  * See AN397 section 5.II.C.3 for the algorithm (though a bit
448                  * poorly stated). The search command forces each device to
449                  * send ROM ID bits one at a time (first the bit, then the
450                  * complement) the master (us) sends back a bit. If the
451                  * device's bit doesn't match what we send back, that device
452                  * stops sending bits back. So each time through we remember
453                  * where we made the last decision (always 0). If there's a
454                  * conflict there this time (and there will be in the absence
455                  * of a hardware failure) we go with 1. This way, we prune the
456                  * devices on the bus and wind up with a unique ROM. We know
457                  * we're done when we detect no new conflicts. The same
458                  * algorithm is used for devices in alarm state as well.
459                  *
460                  * In addition, experience has shown that sometimes devices
461                  * stop responding in the middle of enumeration, so try this
462                  * step again a few times when that happens. It is unclear if
463                  * this is due to a nosiy electrical environment or some odd
464                  * timing issue.
465                  */
466
467                 /*
468                  * The enumeration command should be successfully sent, if not,
469                  * we have big issues on the bus so punt. Lower layers report
470                  * any unusual errors, so we don't need to here.
471                  */
472                 err = enumfp(dev, dev);
473                 if (err != 0)
474                         return (err);
475
476                 for (i = 0; i < 64; i++) {
477                         OWLL_READ_DATA(lldev, &timing_regular, &first);
478                         OWLL_READ_DATA(lldev, &timing_regular, &second);
479                         switch (first | second << 1) {
480                         case 0: /* Conflict */
481                                 if (i < prior)
482                                         dir = (last_mask >> i) & 1;
483                                 else
484                                         dir = i == prior;
485
486                                 if (dir == 0)
487                                         last = i;
488                                 break;
489                         case 1: /* 1 then 0 -> 1 for all */
490                                 dir = 1;
491                                 break;
492                         case 2: /* 0 then 1 -> 0 for all */
493                                 dir = 0;
494                                 break;
495                         case 3:
496                                 /*
497                                  * No device responded. This is unexpected, but
498                                  * experience has shown that on some platforms
499                                  * we miss a timing window, or otherwise have
500                                  * an issue. Start this step over. Since we've
501                                  * not updated prior yet, we can just jump to
502                                  * the top of the loop for a re-do of this step.
503                                  */
504                                 printf("oops, starting over\n");
505                                 if (++retries > 5)
506                                         return (EIO);
507                                 goto again;
508                         default: /* NOTREACHED */
509                                 __unreachable();
510                         }
511                         if (dir) {
512                                 OWLL_WRITE_ONE(lldev, &timing_regular);
513                                 probed |= 1ull << i;
514                         } else {
515                                 OWLL_WRITE_ZERO(lldev, &timing_regular);
516                         }
517                 }
518                 retries = 0;
519                 foundfp(dev, probed);
520                 last_mask = probed;
521                 prior = last;
522         }
523         ow_release_bus(dev, dev);
524
525         return (0);
526 }
527
528 static int
529 ow_probe(device_t dev)
530 {
531
532         device_set_desc(dev, "1 Wire Bus");
533         return (BUS_PROBE_GENERIC);
534 }
535
536 static int
537 ow_attach(device_t ndev)
538 {
539         struct ow_softc *sc;
540
541         /*
542          * Find all the devices on the bus. We don't probe / attach them in the
543          * enumeration phase. We do this because we want to allow the probe /
544          * attach routines of the child drivers to have as full an access to the
545          * bus as possible. While we reset things before the next step of the
546          * search (so it would likely be OK to allow access by the clients to
547          * the bus), it is more conservative to find them all, then to do the
548          * attach of the devices. This also allows the child devices to have
549          * more knowledge of the bus. We also ignore errors from the enumeration
550          * because they might happen after we've found a few devices.
551          */
552         sc = device_get_softc(ndev);
553         sc->dev = ndev;
554         mtx_init(&sc->mtx, device_get_nameunit(sc->dev), "ow", MTX_DEF);
555         ow_enumerate(ndev, ow_search_rom, ow_device_found);
556         return bus_generic_attach(ndev);
557 }
558
559 static int
560 ow_detach(device_t ndev)
561 {
562         device_t *children, child;
563         int nkid, i;
564         struct ow_devinfo *di;
565         struct ow_softc *sc;
566
567         sc = device_get_softc(ndev);
568         /*
569          * detach all the children first. This is blocking until any threads
570          * have stopped, etc.
571          */
572         bus_generic_detach(ndev);
573
574         /*
575          * We delete all the children, and free up the ivars 
576          */
577         if (device_get_children(ndev, &children, &nkid) != 0)
578                 return ENOMEM;
579         for (i = 0; i < nkid; i++) {
580                 child = children[i];
581                 di = device_get_ivars(child);
582                 free(di, M_OW);
583                 device_delete_child(ndev, child);
584         }
585         free(children, M_TEMP);
586
587         OW_LOCK_DESTROY(sc);
588         return 0;
589 }
590
591 /*
592  * Not sure this is really needed. I'm having trouble figuring out what
593  * location means in the context of the one wire bus.
594  */
595 static int
596 ow_child_location_str(device_t dev, device_t child, char *buf,
597     size_t buflen)
598 {
599
600         *buf = '\0';
601         return (0);
602 }
603
604 static int
605 ow_child_pnpinfo_str(device_t dev, device_t child, char *buf,
606     size_t buflen)
607 {
608         struct ow_devinfo *di;
609
610         di = device_get_ivars(child);
611         snprintf(buf, buflen, "romid=%8D", &di->romid, ":");
612         return (0);
613 }
614
615 static int
616 ow_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
617 {
618         struct ow_devinfo *di;
619         romid_t **ptr;
620
621         di = device_get_ivars(child);
622         switch (which) {
623         case OW_IVAR_FAMILY:
624                 *result = di->romid & 0xff;
625                 break;
626         case OW_IVAR_ROMID:
627                 ptr = (romid_t **)result;
628                 *ptr = &di->romid;
629                 break;
630         default:
631                 return EINVAL;
632         }
633
634         return 0;
635 }
636
637 static int
638 ow_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
639 {
640
641         return EINVAL;
642 }
643
644 static int
645 ow_print_child(device_t ndev, device_t pdev)
646 {
647         int retval = 0;
648         struct ow_devinfo *di;
649
650         di = device_get_ivars(pdev);
651
652         retval += bus_print_child_header(ndev, pdev);
653         retval += printf(" romid %8D", &di->romid, ":");
654         retval += bus_print_child_footer(ndev, pdev);
655
656         return retval;
657 }
658
659 static void
660 ow_probe_nomatch(device_t ndev, device_t pdev)
661 {
662         struct ow_devinfo *di;
663
664         di = device_get_ivars(pdev);
665         device_printf(ndev, "romid %8D: no driver\n", &di->romid, ":");
666 }
667
668 static int
669 ow_acquire_bus(device_t ndev, device_t pdev, int how)
670 {
671         struct ow_softc *sc;
672
673         sc = device_get_softc(ndev);
674         OW_ASSERT_UNLOCKED(sc);
675         OW_LOCK(sc);
676         if (sc->owner != NULL) {
677                 if (sc->owner == pdev)
678                         panic("%s: %s recursively acquiring the bus.\n",
679                             device_get_nameunit(ndev),
680                             device_get_nameunit(pdev));
681                 if (how == OWN_DONTWAIT) {
682                         OW_UNLOCK(sc);
683                         return EWOULDBLOCK;
684                 }
685                 while (sc->owner != NULL)
686                         mtx_sleep(sc, &sc->mtx, 0, "owbuswait", 0);
687         }
688         sc->owner = pdev;
689         OW_UNLOCK(sc);
690
691         return 0;
692 }
693
694 static void
695 ow_release_bus(device_t ndev, device_t pdev)
696 {
697         struct ow_softc *sc;
698
699         sc = device_get_softc(ndev);
700         OW_ASSERT_UNLOCKED(sc);
701         OW_LOCK(sc);
702         if (sc->owner == NULL)
703                 panic("%s: %s releasing unowned bus.", device_get_nameunit(ndev),
704                     device_get_nameunit(pdev));
705         if (sc->owner != pdev)
706                 panic("%s: %s don't own the bus. %s does. game over.",
707                     device_get_nameunit(ndev), device_get_nameunit(pdev),
708                     device_get_nameunit(sc->owner));
709         sc->owner = NULL;
710         wakeup(sc);
711         OW_UNLOCK(sc);
712 }
713
714 devclass_t ow_devclass;
715
716 static device_method_t ow_methods[] = {
717         /* Device interface */
718         DEVMETHOD(device_probe,         ow_probe),
719         DEVMETHOD(device_attach,        ow_attach),
720         DEVMETHOD(device_detach,        ow_detach),
721
722         /* Bus interface */
723         DEVMETHOD(bus_child_pnpinfo_str, ow_child_pnpinfo_str),
724         DEVMETHOD(bus_child_location_str, ow_child_location_str),
725         DEVMETHOD(bus_read_ivar,        ow_read_ivar),
726         DEVMETHOD(bus_write_ivar,       ow_write_ivar),
727         DEVMETHOD(bus_print_child,      ow_print_child),
728         DEVMETHOD(bus_probe_nomatch,    ow_probe_nomatch),
729
730         /* One Wire Network/Transport layer interface */
731         DEVMETHOD(own_send_command,     ow_send_command),
732         DEVMETHOD(own_acquire_bus,      ow_acquire_bus),
733         DEVMETHOD(own_release_bus,      ow_release_bus),
734         DEVMETHOD(own_crc,              ow_crc),
735         { 0, 0 }
736 };
737
738 static driver_t ow_driver = {
739         "ow",
740         ow_methods,
741         sizeof(struct ow_softc),
742 };
743
744 DRIVER_MODULE(ow, owc, ow_driver, ow_devclass, 0, 0);
745 MODULE_VERSION(ow, 1);