source: mainline/uspace/drv/time/cmos-rtc/cmos-rtc.c@ 5ef13847

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5ef13847 was 5ef13847, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

RTC: move the rtc driver under drv/time/cmos-rtc

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2012 Maurizio Lombardi
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/**
30 * @defgroup CMOS RTC driver.
31 * @brief HelenOS RTC driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <errno.h>
39#include <ddi.h>
40#include <stdio.h>
41#include <ddf/driver.h>
42#include <ddf/log.h>
43#include <ops/clock.h>
44#include <fibril_synch.h>
45#include <device/hw_res.h>
46#include <devman.h>
47
48#define NAME "cmos-rtc"
49
50typedef struct rtc {
51 /** DDF device node */
52 ddf_dev_t *dev;
53 /** DDF function node */
54 ddf_fun_t *fun;
55 /** The fibril mutex for synchronizing the access to the device */
56 fibril_mutex_t mutex;
57} rtc_t;
58
59
60static int
61rtc_time_get(ddf_fun_t *fun, time_t *t);
62
63static int
64rtc_time_set(ddf_fun_t *fun, time_t t);
65
66static int
67rtc_dev_add(ddf_dev_t *dev);
68
69static int
70rtc_dev_initialize(rtc_t *rtc);
71
72
73static ddf_dev_ops_t rtc_dev_ops;
74
75/** The RTC device driver's standard operations */
76static driver_ops_t rtc_ops = {
77 .dev_add = rtc_dev_add,
78 .dev_remove = NULL,
79};
80
81/** The RTC device driver structure */
82static driver_t rtc_driver = {
83 .name = NAME,
84 .driver_ops = &rtc_ops,
85};
86
87/** Clock interface */
88static clock_dev_ops_t rtc_clock_dev_ops = {
89 .time_get = rtc_time_get,
90 .time_set = rtc_time_set,
91};
92
93/** Initialize the RTC driver */
94static void
95rtc_init(void)
96{
97 ddf_log_init(NAME, LVL_ERROR);
98
99 rtc_dev_ops.open = NULL;
100 rtc_dev_ops.close = NULL;
101
102 rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
103 rtc_dev_ops.default_handler = NULL;
104}
105
106/** Initialize the RTC device
107 *
108 * @param rtc Pointer to the RTC device
109 *
110 * @return EOK on success or a negative error code
111 */
112static int
113rtc_dev_initialize(rtc_t *rtc)
114{
115 int rc;
116
117 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
118
119 hw_resource_list_t hw_resources;
120 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
121
122 /* Connect to the parent's driver */
123
124 rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
125 rtc->dev->handle, IPC_FLAG_BLOCKING);
126 if (!rtc->dev->parent_sess) {
127 ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
128 of device %s.", rtc->dev->name);
129 return ENOENT;
130 }
131
132 /* Get the HW resources */
133 rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
134 if (rc != EOK) {
135 ddf_msg(LVL_ERROR, "Failed to get HW resources\
136 for device %s", rtc->dev->name);
137 return rc;
138 }
139
140 return EOK;
141}
142
143/** Read the current time from the CMOS
144 *
145 * @param fun The RTC function
146 * @param t Pointer to the time variable
147 *
148 * @return EOK on success or a negative error code
149 */
150static int
151rtc_time_get(ddf_fun_t *fun, time_t *t)
152{
153 return EOK;
154}
155
156/** Set the time in the RTC
157 *
158 * @param fun The RTC function
159 * @param t The time value to set
160 *
161 * @return EOK or a negative error code
162 */
163static int
164rtc_time_set(ddf_fun_t *fun, time_t t)
165{
166 return EOK;
167}
168
169/** The dev_add callback of the rtc driver
170 *
171 * @param dev The RTC device
172 *
173 * @return EOK on success or a negative error code
174 */
175static int
176rtc_dev_add(ddf_dev_t *dev)
177{
178 rtc_t *rtc;
179 int rc;
180
181 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
182 dev->name, (int) dev->handle);
183
184 rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
185 if (!rtc)
186 return ENOMEM;
187
188 rtc->dev = dev;
189 fibril_mutex_initialize(&rtc->mutex);
190
191 rc = rtc_dev_initialize(rtc);
192 if (rc != EOK)
193 return rc;
194
195 return rc;
196}
197
198int
199main(int argc, char **argv)
200{
201 printf(NAME ": HelenOS RTC driver\n");
202 rtc_init();
203 return ddf_driver_main(&rtc_driver);
204}
205
206/**
207 * @}
208 */
Note: See TracBrowser for help on using the repository browser.