1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/****************************************************************************
* drivers/timers/rtc.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/rtc.h>
/****************************************************************************
* Private Types
****************************************************************************/
struct rtc_upperhalf_s
{
FAR struct rtc_lowerhalf_s *lower; /* Contained lower half driver */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
uint8_t crefs; /* Number of open references */
bool unlinked; /* True if the driver has been unlinked */
#endif
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Internal logic */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static void rtc_destroy(FAR struct rtc_upperhalf_s *upper);
#endif
/* Character driver methods */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int rtc_open(FAR struct file *filep);
static int rtc_close(FAR struct file *filep);
#endif
static ssize_t rtc_read(FAR struct file *filep, FAR char *, size_t);
static ssize_t rtc_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int rtc_unlink(FAR struct inode *inode);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations rtc_fops =
{
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
rtc_open, /* open */
rtc_close, /* close */
#else
0, /* open */
0, /* close */
#endif
rtc_read, /* read */
rtc_write, /* write */
0, /* seek */
rtc_ioctl, /* ioctl */
#ifndef CONFIG_DISABLE_POLL
0, /* poll */
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
rtc_unlink /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: rtc_destory
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static void rtc_destroy(FAR struct rtc_upperhalf_s *upper)
{
/* If the lower half driver provided a destroy method, then call that
* method now in order order to clean up resources used by the lower-half
* driver.
*/
DEBUGASSERT(upper->lower && upper->lower->ops);
if (upper->lower->ops->destroy)
{
upper->lower->ops->destroy(upper->lower);
}
/* And free our container */
kmm_free(upper);
}
#endif
/****************************************************************************
* Name: rtc_open
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int rtc_open(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct rtc_upperhalf_s *upper;
/* Get the reference to our internal state structure from the inode
* structure.
*/
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
upper = inode->i_private;
/* Increment the count of open references on the RTC driver */
upper->crefs++;
DEBUGASSERT(upper->crefs > 0);
return OK;
}
#endif
/****************************************************************************
* Name: rtc_close
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int rtc_close(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct rtc_upperhalf_s *upper;
/* Get the reference to our internal state structure from the inode
* structure.
*/
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
upper = inode->i_private;
/* Decrement the count of open references on the RTC driver */
DEBUGASSERT(upper->crefs > 0);
upper->crefs--;
/* If the count has decremented to zero and the driver has been unlinked,
* then commit Hara-Kiri now.
*/
if (upper->crefs == 0 && upper->unlinked)
{
rtc_destroy(upper);
}
return OK;
}
#endif
/****************************************************************************
* Name: rtc_read
****************************************************************************/
static ssize_t rtc_read(FAR struct file *filep, FAR char *buffer, size_t len)
{
return 0; /* Return EOF */
}
/****************************************************************************
* Name: rtc_write
****************************************************************************/
static ssize_t rtc_write(FAR struct file *filep, FAR const char *buffer, size_t len)
{
return len; /* Say that everything was written */
}
/****************************************************************************
* Name: rtc_ioctl
****************************************************************************/
static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode;
FAR struct rtc_upperhalf_s *upper;
FAR const struct rtc_ops_s *ops;
int ret = -ENOSYS;
/* Get the reference to our internal state structure from the inode
* structure.
*/
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
upper = inode->i_private;
DEBUGASSERT(upper->lower && upper->lower->ops);
/* We simply forward all ioctl() commands to the lower half. The upper
* half is nothing more than a thin driver shell over the lower level
* RTC implementation.
*/
ops = upper->lower->ops;
switch (cmd)
{
/* RTC_RD_TIME returns the current RTC time.
*
* Argument: A writeable reference to a struct rtc_time to receive the
* RTC's time.
*/
case RTC_RD_TIME:
{
FAR struct rtc_time *rtctime = (FAR struct rtc_time *)((uintptr_t)arg);
if (ops->rdtime)
{
ret = ops->rdtime(upper->lower, rtctime);
}
}
break;
/* RTC_SET_TIME sets the RTC's time
*
* Argument: A read-only reference to a struct rtc_time containing the
* the new time to be set.
*/
case RTC_SET_TIME:
{
FAR const struct rtc_time *rtctime =
(FAR const struct rtc_time *)((uintptr_t)arg);
if (ops->settime)
{
ret = ops->settime(upper->lower, rtctime);
}
}
break;
#ifdef CONFIG_RTC_ALARM
/* RTC_ALM_READ reads the alarm time (for RTCs that support alarms)
*
* Argument: A writeable reference to a struct rtc_time to receive the
* RTC's alarm time.
*/
case RTC_ALM_READ:
{
FAR struct rtc_time *almtime = (FAR struct rtc_time *)((uintptr_t)arg);
if (ops->almread)
{
ret = ops->almread(upper->lower, almtime);
}
}
break;
/* RTC_ALM_SET sets the alarm time (for RTCs that support alarms).
*
* Argument: A read-only reference to a struct rtc_time containing the
* new alarm time to be set.
*/
case RTC_ALM_SET:
{
FAR const struct rtc_time *almtime =
(FAR const struct rtc_time *)((uintptr_t)arg);
if (ops->almset)
{
ret = ops->almset(upper->lower, almtime);
}
}
break;
#endif /* CONFIG_RTC_ALARM */
#ifdef CONFIG_RTC_PERIODIC
/* RTC_IRQP_READ read the frequency for periodic interrupts (for RTCs
* that support periodic interrupts)
*
* Argument: A pointer to a writeable unsigned long value in which to
* receive the frequency value.
*/
case RTC_IRQP_READ:
{
FAR unsigned long *irqpfreq = (FAR unsigned long *)((uintptr_t)arg);
if (ops->irqpread)
{
ret = ops->irqpread(upper->lower, irqpfreq);
}
}
break;
/* RTC_IRQP_SET set the frequency for periodic interrupts (for RTCs that
* support periodic interrupts)
*
* Argument: An unsigned long value providing the new periodic frequency
*/
case RTC_IRQP_SET:
{
if (ops->irqpset)
{
ret = ops->irqpset(upper->lower, arg);
}
}
break;
#endif /* CONFIG_RTC_PERIODIC */
#ifdef CONFIG_RTC_ALARM
/* RTC_AIE_ON enable alarm interrupts (for RTCs that support alarms)
*
* Argument: None
*/
case RTC_AIE_ON:
{
if (ops->aie)
{
ret = ops->aie(upper->lower, true);
}
}
break;
/* RTC_AIE_OFF disable the alarm interrupt (for RTCs that support
* alarms)
*
* Argument: None
*/
case RTC_AIE_OFF:
{
if (ops->aie)
{
ret = ops->aie(upper->lower, false);
}
}
break;
#endif /* CONFIG_RTC_ALARM */
#ifdef CONFIG_RTC_ONESEC
/* RTC_UIE_ON enable the interrupt on every clock update (for RTCs that
* support this once-per-second interrupt).
*
* Argument: None
*/
case RTC_UIE_ON:
{
if (ops->uie)
{
ret = ops->uie(upper->lower, true);
}
}
break;
/* RTC_UIE_OFF disable the interrupt on every clock update (for RTCs
* that support this once-per-second interrupt).
*
* Argument: None
*/
case RTC_UIE_OFF:
{
if (ops->uie)
{
ret = ops->uie(upper->lower, false);
}
}
break;
#endif /* CONFIG_RTC_ONESEC */
#ifdef CONFIG_RTC_PERIODIC
/* RTC_PIE_ON enable the periodic interrupt (for RTCs that support these
* periodic interrupts).
*
* Argument: None
*/
case RTC_PIE_ON:
{
if (ops->pie)
{
ret = ops->pie(upper->lower, true);
}
}
break;
/* RTC_PIE_OFF disable the periodic interrupt (for RTCs that support
* these periodic interrupts).
*
* Argument: None
*/
case RTC_PIE_OFF:
{
if (ops->pie)
{
ret = ops->pie(upper->lower, false);
}
}
break;
#endif /* CONFIG_RTC_PERIODIC */
#ifdef CONFIG_RTC_EPOCHYEAR
/* RTC_EPOCH_READ read the Epoch.
*
* Argument: A reference to a writeable unsigned low variable that will
* receive the Epoch value.
*/
case RTC_EPOCH_READ:
{
FAR unsigned long *epoch = (FAR unsigned long *)((uintptr_t)arg);
if (ops->rdepoch)
{
ret = ops->rdepoch(upper->lower, epoch);
}
}
break;
/* RTC_EPOCH_SET set the Epoch
*
* Argument: An unsigned long value containing the new Epoch value to be
* set.
*/
case RTC_EPOCH_SET:
{
if (ops->setepoch)
{
ret = ops->setepoch(upper->lower, arg);
}
}
break;
#endif /* CONFIG_RTC_EPOCHYEAR */
#ifdef CONFIG_RTC_ALARM
/* RTC_WKALM_RD read the current alarm
*
* Argument: A writeable reference to struct rtc_wkalrm to receive the
* current alarm settings.
*/
case RTC_WKALM_RD:
{
FAR struct rtc_wkalrm *wkalrm = (FAR struct rtc_wkalrm *)((uintptr_t)arg);
if (ops->rdwkalm)
{
ret = ops->rdwkalm(upper->lower, wkalrm);
}
}
break;
/* RTC_WKALM_SET set the alarm.
*
* Argument: A read-only reference to struct rtc_wkalrm containing the
* new alarm settings.
*/
case RTC_WKALM_SET:
{
FAR const struct rtc_wkalrm *wkalrm =
(FAR const struct rtc_wkalrm *)((uintptr_t)arg);
if (ops->setwkalm)
{
ret = ops->setwkalm(upper->lower, wkalrm);
}
}
break;
#endif /* CONFIG_RTC_ALARM */
/* Forward any unrecognized IOCTLs to the lower half driver... they
* may represent some architecture-specific command.
*/
default:
{
ret = -ENOTTY;
#ifdef CONFIG_RTC_IOCTL
if (ops->ioctl)
{
ret = ops->ioctl(upper->lower, cmd, arg);
}
#endif
}
break;
}
return ret;
}
/****************************************************************************
* Name: rtc_unlink
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int rtc_unlink(FAR struct inode *inode)
{
FAR struct rtc_upperhalf_s *upper;
/* Get the reference to our internal state structure from the inode
* structure.
*/
DEBUGASSERT(inode && inode->i_private);
upper = inode->i_private;
/* Indicate that the driver has been unlinked */
upper->unlinked = true;
/* If there are no further open references to the driver, then commit
* Hara-Kiri now.
*/
if (upper->crefs == 0)
{
rtc_destroy(upper);
}
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: rtc_initialize
*
* Description:
* Create an RTC driver by binding to the lower half RTC driver instance
* provided to this function. The resulting RTC driver will be registered
* at /dev/rtcN where N is the minor number provided to this function.
*
* Input parameters:
* minor - The minor number of the RTC device. The N in /dev/rtcN
* lower - The lower half driver instance.
*
* Returned Value:
* Zero (OK) on success; A negated errno value on failure.
*
****************************************************************************/
int rtc_initialize(int minor, FAR struct rtc_lowerhalf_s *lower)
{
FAR struct rtc_upperhalf_s *upper;
char devpath[16];
int ret;
DEBUGASSERT(lower && lower->ops && minor >= 0 && minor < 1000);
/* Allocate an upper half container structure */
upper = (FAR struct rtc_upperhalf_s *)kmm_malloc(sizeof(struct rtc_upperhalf_s));
if (!upper)
{
return -ENOMEM;
}
/* Initialize the upper half container */
upper->lower = lower; /* Contain lower half driver */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
upper->crefs = 0; /* No open references */
upper->unlinked = false; /* Driver is not unlinked */
#endif
/* Create the driver name. There is space for the a minor number up to 6
* characters
*/
snprintf(devpath, 16, "/dev/rtc%d", minor);
/* And, finally, register the new RTC driver */
ret = register_driver(devpath, &rtc_fops, 0666, upper);
if (ret < 0)
{
kmm_free(upper);
return ret;
}
return OK;
}
|