aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/device/xmm6260/xmm6260_modemctl.c
blob: d969feda821b6dbe33eee5525f7b2661789d9b76 (plain)
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
/*
 * XMM6260 Modem Control functions
 * Copyright (C) 2012 Alexander Tarasikov <alexander.tarasikov@gmail.com>
 * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
 *
 * based on the incomplete C++ implementation which is
 * Copyright (C) 2012 Sergey Gridasov <grindars@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

//for timeval
#include <sys/time.h>

//for mmap
#include <sys/mman.h>
#include <sys/stat.h>

#include "ipc_private.h"

#include "xmm6260_loader.h"
#include "xmm6260_modemctl.h"
#include "modem_prj.h"

/*
 * modemctl generic functions
 */

int modemctl_link_set_active(struct ipc_client *client,
    struct modemctl_io_data *io_data, bool enabled) {
    unsigned status = enabled;
    int ret;
    unsigned long ioctl_code;

    ioctl_code = IOCTL_LINK_CONTROL_ACTIVE;
    ret = ioctl(io_data->link_fd, ioctl_code, &status);

    if (ret < 0) {
        ipc_client_log(client, "failed to set link active to %d", enabled);
        goto fail;
    }

    return 0;
fail:
    return ret;
}

int modemctl_link_set_enabled(struct ipc_client *client,
    struct modemctl_io_data *io_data, bool enabled) {
    unsigned status = enabled;
    int ret;
    unsigned long ioctl_code;

    ioctl_code = IOCTL_LINK_CONTROL_ENABLE;
    ret = ioctl(io_data->link_fd, ioctl_code, &status);

    if (ret < 0) {
        ipc_client_log(client, "failed to set link state to %d", enabled);
        goto fail;
    }

    return 0;
fail:
    return ret;
}

int modemctl_wait_link_ready(struct ipc_client *client,
    struct modemctl_io_data *io_data) {
    int ret;

    struct timeval tv_start = {};
    struct timeval tv_end = {};

    gettimeofday(&tv_start, 0);;

    //link wakeup timeout in milliseconds
    long diff = 0;

    do {
        ret = ioctl(io_data->link_fd, IOCTL_LINK_CONNECTED, 0);
        if (ret < 0) {
            goto fail;
        }

        if (ret == 1) {
            return 0;
        }

        usleep(LINK_POLL_DELAY_US);
        gettimeofday(&tv_end, 0);;

        diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000;
        diff += (tv_end.tv_usec - tv_start.tv_usec) / 1000;
    } while (diff < LINK_TIMEOUT_MS);

    ret = -ETIMEDOUT;

fail:
    return ret;
}

int modemctl_wait_modem_online(struct ipc_client *client,
    struct modemctl_io_data *io_data) {
    int ret;

    struct timeval tv_start = {};
    struct timeval tv_end = {};

    gettimeofday(&tv_start, 0);;

    //link wakeup timeout in milliseconds
    long diff = 0;

    do {
        ret = ioctl(io_data->boot_fd, IOCTL_MODEM_STATUS, 0);
        if (ret < 0) {
            goto fail;
        }

        if (ret == STATE_ONLINE) {
            return 0;
        }

        usleep(LINK_POLL_DELAY_US);
        gettimeofday(&tv_end, 0);;

        diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000;
        diff += (tv_end.tv_usec - tv_start.tv_usec) / 1000;
    } while (diff < LINK_TIMEOUT_MS);

    ret = -ETIMEDOUT;

fail:
    return ret;
}

int modemctl_modem_power(struct ipc_client *client,
    struct modemctl_io_data *io_data, bool enabled) {
    if (enabled) {
        return ioctl(io_data->boot_fd, IOCTL_MODEM_ON, 0);
    }
    else {
        return ioctl(io_data->boot_fd, IOCTL_MODEM_OFF, 0);
    }
    return -1;
}

int modemctl_modem_boot_power(struct ipc_client *client,
    struct modemctl_io_data *io_data, bool enabled) {
    if (enabled) {
        return ioctl(io_data->boot_fd, IOCTL_MODEM_BOOT_ON, 0);
    }
    else {
        return ioctl(io_data->boot_fd, IOCTL_MODEM_BOOT_OFF, 0);
    }
    return -1;
}

// vim:ts=4:sw=4:expandtab