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