source: mainline/uspace/lib/c/generic/pio_trace.c@ df956b9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since df956b9b was b5c2f56, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

libc: Make pio_read_* accept const ioports.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <assert.h>
36#include <errno.h>
37#include <malloc.h>
38#include <adt/list.h>
39#include <fibril_synch.h>
40#include <ddi.h>
41#include <str.h>
42
43
44typedef struct {
45 link_t link;
46 volatile void *base;
47 size_t size;
48 void *data;
49 trace_fnc log;
50} region_t;
51
52static inline region_t * region_instance(link_t *l)
53{
54 return list_get_instance(l, region_t, link);
55}
56
57static inline region_t * region_create(volatile void* base, size_t size,
58 trace_fnc log, void* data)
59{
60 region_t *new_reg = malloc(sizeof(region_t));
61 if (new_reg) {
62 link_initialize(&new_reg->link);
63 new_reg->base = base;
64 new_reg->data = data;
65 new_reg->size = size;
66 new_reg->log = log;
67 }
68 return new_reg;
69}
70
71static inline void region_destroy(region_t *r)
72{
73 free(r);
74}
75
76typedef struct {
77 list_t list;
78 fibril_rwlock_t guard;
79} pio_regions_t;
80
81static pio_regions_t * get_regions(void)
82{
83 static pio_regions_t regions = {
84 .list = {
85 .head = { &regions.list.head, &regions.list.head },
86 },
87 .guard = FIBRIL_RWLOCK_INITIALIZER(regions.guard),
88 };
89 return &regions;
90}
91
92
93void pio_trace_log(const volatile void *r, uint32_t val, bool write)
94{
95 pio_regions_t *regions = get_regions();
96 fibril_rwlock_read_lock(&regions->guard);
97 list_foreach(regions->list, it) {
98 assert(it);
99 region_t *reg = region_instance(it);
100 assert(reg);
101 if ((r >= reg->base) && (r < reg->base + reg->size)) {
102 if (reg->log)
103 reg->log(r, val, reg->base,
104 reg->size, reg->data, write);
105 break;
106 }
107 }
108 fibril_rwlock_read_unlock(&regions->guard);
109}
110
111int pio_trace_enable(void *base, size_t size, trace_fnc log, void *data)
112{
113 pio_regions_t *regions = get_regions();
114 assert(regions);
115
116 region_t *region = region_create(base, size, log, data);
117 if (!region)
118 return ENOMEM;
119
120 fibril_rwlock_write_lock(&regions->guard);
121 list_append(&region->link, &regions->list);
122 fibril_rwlock_write_unlock(&regions->guard);
123 return EOK;
124}
125
126void pio_trace_disable(void *r)
127{
128 pio_regions_t *regions = get_regions();
129 assert(regions);
130
131 fibril_rwlock_write_lock(&regions->guard);
132 list_foreach_safe(regions->list, it, next) {
133 assert(it);
134 region_t *reg = region_instance(it);
135 assert(reg);
136 if (r >= reg->base && (r < reg->base + reg->size)) {
137 list_remove(&reg->link);
138 region_destroy(reg);
139 }
140 }
141 fibril_rwlock_write_unlock(&regions->guard);
142}
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.