aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/modems/generic-xmm626/generic-xmm626_hsic.c
blob: b33a9e7f83057b6d2bff183b891ee646e8b399b6 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2012 Alexander Tarasikov <alexander.tarasikov@gmail.com>
 * Copyright (C) 2013-2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * Based on the incomplete C++ implementation which is:
 * Copyright (C) 2012 Sergey Gridasov <grindars@gmail.com>
 *
 * libsamsung-ipc 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.
 *
 * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>

#include <samsung-ipc.h>

#include "../xmm626/xmm626.h"
#include "../xmm626/xmm626_hsic.h"

int generic_xmm626_hsic_command_send(__attribute__((unused)) void *transport_data,
				     int device_fd, unsigned short code,
				     const void *data, size_t size,
				     size_t command_data_size, int ack)
{
    struct xmm626_hsic_command_header header;
    void *buffer = NULL;
    size_t length;
    struct timeval timeout;
    fd_set fds;
    unsigned char *p;
    int rc;
    int i;

    if (device_fd < 0 || data == NULL || size == 0 || command_data_size == 0 || command_data_size < size)
        return -1;

    header.checksum = (size & 0xffff) + code;
    header.code = code;
    header.data_size = size;

    p = (unsigned char *) data;

    for (i = 0; i < (int) size; i++)
        header.checksum += *p++;

    length = command_data_size + sizeof(header);
    buffer = calloc(1, length);

    memset(buffer, 0, length);
    p = (unsigned char *) buffer;
    memcpy(p, &header, sizeof(header));
    p += sizeof(header);
    memcpy(p, data, size);

    rc = write(device_fd, buffer, length);
    if (rc < (int) length)
        goto error;

    if (!ack) {
        rc = 0;
        goto complete;
    }

    memset(buffer, 0, length);

    FD_ZERO(&fds);
    FD_SET(device_fd, &fds);

    timeout.tv_sec = 1;
    timeout.tv_usec = 0;

    rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
    if (rc <= 0)
        goto error;

    rc = read(device_fd, &header, sizeof(header));
    if (rc < (int) sizeof(header)) {
	    printf("got no header\n");
        goto error;
    }

    rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
    if (rc <= 0) {
	    printf("select failed\n");
        goto error;
    }

    rc = read(device_fd, buffer, command_data_size);
    if (rc < (int) command_data_size) {
	    printf("not enough read\n");
        goto error;
    }

    if (header.code != code) {
	    printf("header code mismatch");
        goto error;
    }

    rc = 0;
    goto complete;

error:
    rc = -1;

complete:
    if (buffer != NULL)
        free(buffer);

    return rc;
}

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