source: mainline/uspace/drv/nic/rtl8139/general.c@ 5fd9c30

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5fd9c30 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Michalec
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/** @file
30 *
31 * General functions and structures used in rtl8139 driver
32 */
33
34#include "general.h"
35
36#include <mem.h>
37#include <errno.h>
38#include <stdint.h>
39
40/** Copy block of the memory from wrapped source buffer.
41 *
42 * Start on the specific offset in the source buffer and * copy data_size bytes
43 * - continue from the buffer start after getting the end
44 *
45 * @param dest The destination memory
46 * @param src_begin The begin of the source buffer
47 * @param src_offset The offset in the source buffer to start copy
48 * @param src_size The source buffer size
49 * @param data_size The amount of data to copy
50 *
51 * @return NULL if the error occures, dest if succeed
52 */
53void* rtl8139_memcpy_wrapped(void *dest, const void *src, size_t src_offset,
54 size_t src_size, size_t data_size)
55{
56 src_offset %= src_size;
57 if (data_size > src_size)
58 return NULL;
59
60 size_t to_src_end = src_size - src_offset;
61 if (data_size <= to_src_end) {
62 return memcpy(dest, src + src_offset, data_size);
63 }
64
65 size_t rem_size = data_size - to_src_end;
66
67 /* First copy the end part of the data (from the source begining),
68 * then copy the begining
69 */
70 memcpy(dest + to_src_end, src, rem_size);
71 return memcpy(dest, src + src_offset, to_src_end);
72}
73
74/** Initialize the timer register structures
75 *
76 * The structure will be initialized to the state that the first call of
77 * rtl8139_timer_act_step function will be the period expiration
78 *
79 * @param ta The timer structure
80 * @param timer_freq The timer frequency in kHz
81 * @param time The requested time
82 *
83 * @return EOK if succeed, negative error code otherwise
84 */
85int rtl8139_timer_act_init(rtl8139_timer_act_t * ta, uint32_t timer_freq,
86 const struct timeval *time)
87{
88 if (!ta || timer_freq == 0 || !time)
89 return EINVAL;
90 memset(ta, 0, sizeof(rtl8139_timer_act_t));
91
92 uint32_t tics_per_ms = timer_freq;
93 uint32_t seconds_in_reg = UINT32_MAX / (tics_per_ms * 1000);
94 ta->full_val = seconds_in_reg * tics_per_ms * 1000;
95
96 struct timeval remains = *time;
97 ta->full_skips = remains.tv_sec / seconds_in_reg;
98 remains.tv_sec = remains.tv_sec % seconds_in_reg;
99
100 if (remains.tv_usec > RTL8139_USEC_IN_SEC) {
101 remains.tv_sec += remains.tv_usec / RTL8139_USEC_IN_SEC;
102 remains.tv_usec = remains.tv_usec % RTL8139_USEC_IN_SEC;
103
104 /* it can be increased above seconds_in_reg again */
105 ta->full_skips += remains.tv_sec / seconds_in_reg;
106 remains.tv_sec = remains.tv_sec % seconds_in_reg;
107 }
108
109 ta->last_val = remains.tv_sec * 1000 + remains.tv_usec / 1000;
110 ta->last_val *= tics_per_ms;
111
112 /* Force inital setting in the next step */
113 ta->full_skips_remains = 0;
114 ta->last_run = 1;
115 return EOK;
116};
117
118
119/** Make one step timer step
120 *
121 * @param ta Timer structure
122 * @param new_reg[out] Register value to set
123 *
124 * @return Nonzero if whole period expired, zero if part of period expired
125 */
126int rtl8139_timer_act_step(rtl8139_timer_act_t * ta, uint32_t *new_reg)
127{
128 uint32_t next_val = 0;
129 int expired = 0;
130
131 if (ta->last_run || (ta->last_val == 0 && ta->full_skips_remains == 0)) {
132 ta->full_skips_remains = ta->full_skips;
133 ta->last_run = 0;
134 expired = 1;
135 }
136
137 if (ta->full_skips_remains > 0) {
138 next_val = ta->full_val;
139 ta->full_skips_remains--;
140 } else {
141 next_val = ta->last_val;
142 ta->last_run = 1;
143 }
144
145 if (new_reg)
146 *new_reg = next_val;
147
148 return expired;
149};
150
Note: See TracBrowser for help on using the repository browser.