2 * This file is part of libsamsung-ipc.
4 * Copyright (C) 2011-2013 Paul Kocialkowski <contact@paulk.fr>
5 * Copyright (C) 2011 Simon Busch <morphis@gravedo.de>
6 * Copyright (C) 2011 Joerie de Gram <j.de.gram@gmail.com>
8 * libsamsung-ipc is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * libsamsung-ipc is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/ioctl.h>
31 #include <samsung-ipc.h>
35 #include "crespo_modem_ctl.h"
38 #include "crespo_ipc.h"
40 int crespo_ipc_bootstrap(struct ipc_client *client)
42 void *modem_image_data = NULL;
44 int modem_ctl_fd = -1;
53 ipc_client_log(client, "Starting crespo modem bootstrap");
55 modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000);
56 if (modem_image_data == NULL) {
57 ipc_client_log(client, "Reading modem image data failed");
60 ipc_client_log(client, "Read modem image data");
62 modem_ctl_fd = open(CRESPO_MODEM_CTL_DEVICE, O_RDWR | O_NDELAY);
63 if (modem_ctl_fd < 0) {
64 ipc_client_log(client, "Opening modem ctl failed");
67 ipc_client_log(client, "Opened modem ctl");
69 rc = ioctl(modem_ctl_fd, IOCTL_MODEM_RESET);
71 ipc_client_log(client, "Resetting modem failed");
74 ipc_client_log(client, "Reset modem");
76 serial_fd = open(CRESPO_MODEM_SERIAL_DEVICE, O_RDWR | O_NDELAY);
78 ipc_client_log(client, "Opening serial failed");
81 ipc_client_log(client, "Opened serial");
85 p = (unsigned char *) modem_image_data;
87 rc = xmm6160_psi_send(client, serial_fd, (void *) p, CRESPO_PSI_SIZE);
89 ipc_client_log(client, "Sending XMM6160 PSI failed");
92 ipc_client_log(client, "Sent XMM6160 PSI");
96 lseek(modem_ctl_fd, 0, SEEK_SET);
98 rc = xmm6160_modem_image_send(client, modem_ctl_fd, NULL, (void *) p, CRESPO_MODEM_IMAGE_SIZE - CRESPO_PSI_SIZE);
100 ipc_client_log(client, "Sending XMM6160 modem image failed");
103 ipc_client_log(client, "Sent XMM6160 modem image");
105 lseek(modem_ctl_fd, CRESPO_MODEM_CTL_NV_DATA_OFFSET, SEEK_SET);
107 rc = xmm6160_nv_data_send(client, modem_ctl_fd, NULL);
109 ipc_client_log(client, "Sending XMM6160 nv_data failed");
112 ipc_client_log(client, "Sent XMM6160 nv_data");
121 if (modem_image_data != NULL)
122 free(modem_image_data);
127 if (modem_ctl_fd >= 0)
133 int crespo_ipc_fmt_send(struct ipc_client *client, struct ipc_message_info *request)
135 struct ipc_header header;
139 if (client == NULL || client->handlers == NULL || client->handlers->write == NULL || request == NULL)
142 ipc_header_fill(&header, request);
144 memset(&mio, 0, sizeof(struct modem_io));
145 mio.size = request->length + sizeof(struct ipc_header);
146 mio.data = malloc(mio.size);
147 memcpy(mio.data, &header, sizeof(struct ipc_header));
148 if (request->data != NULL && request->length > 0)
149 memcpy((void *) ((unsigned char *) mio.data + sizeof(struct ipc_header)), request->data, request->length);
151 ipc_client_log_send(client, request, __func__);
153 rc = client->handlers->write(client->handlers->transport_data, (void *) &mio, sizeof(struct modem_io));
160 int crespo_ipc_fmt_recv(struct ipc_client *client, struct ipc_message_info *response)
162 struct ipc_header *header;
166 if (client == NULL || client->handlers == NULL || client->handlers->read == NULL || response == NULL)
169 memset(&mio, 0, sizeof(struct modem_io));
170 mio.size = CRESPO_DATA_SIZE;
171 mio.data = malloc(mio.size);
173 rc = client->handlers->read(client->handlers->transport_data, &mio, sizeof(struct modem_io) + mio.size);
174 if (rc < 0 || mio.data == NULL || mio.size < sizeof(struct ipc_header)) {
175 ipc_client_log(client, "Reading FMT data from the modem failed");
179 header = (struct ipc_header *) mio.data;
180 ipc_message_info_fill(header, response);
182 if (mio.size > sizeof(struct ipc_header)) {
183 response->length = mio.size - sizeof(struct ipc_header);
184 response->data = malloc(response->length);
185 memcpy(response->data, (void *) ((unsigned char *) mio.data + sizeof(struct ipc_header)), response->length);
188 ipc_client_log_recv(client, response, __func__);
196 if (mio.data != NULL)
202 int crespo_ipc_rfs_send(struct ipc_client *client, struct ipc_message_info *request)
207 if (client == NULL || client->handlers == NULL || client->handlers->write == NULL || request == NULL)
210 memset(&mio, 0, sizeof(struct modem_io));
211 mio.id = request->mseq;
212 mio.cmd = request->index;
213 mio.size = request->length;
214 if (request->data != NULL && request->length > 0) {
215 mio.data = malloc(mio.size);
216 memcpy(mio.data, request->data, request->length);
219 ipc_client_log_send(client, request, __func__);
221 rc = client->handlers->write(client->handlers->transport_data, (void *) &mio, sizeof(struct modem_io));
223 if (mio.data != NULL)
229 int crespo_ipc_rfs_recv(struct ipc_client *client, struct ipc_message_info *response)
234 if (client == NULL || client->handlers == NULL || client->handlers->read == NULL || response == NULL)
237 memset(&mio, 0, sizeof(struct modem_io));
238 mio.size = CRESPO_DATA_SIZE;
239 mio.data = malloc(mio.size);
241 rc = client->handlers->read(client->handlers->transport_data, &mio, sizeof(struct modem_io) + mio.size);
242 if (rc < 0 || mio.data == NULL || mio.size <= 0) {
243 ipc_client_log(client, "Reading RFS data from the modem failed");
247 memset(response, 0, sizeof(struct ipc_message_info));
248 response->aseq = mio.id;
249 response->group = IPC_GROUP_RFS;
250 response->index = mio.cmd;
253 response->length = mio.size;
254 response->data = malloc(response->length);
255 memcpy(response->data, mio.data, response->length);
258 ipc_client_log_recv(client, response, __func__);
266 if (mio.data != NULL)
272 int crespo_ipc_open(void *data, int type)
274 struct crespo_ipc_transport_data *transport_data;
280 transport_data = (struct crespo_ipc_transport_data *) data;
284 case IPC_CLIENT_TYPE_FMT:
285 fd = open(CRESPO_MODEM_FMT_DEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
287 case IPC_CLIENT_TYPE_RFS:
288 fd = open(CRESPO_MODEM_RFS_DEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
297 transport_data->fd = fd;
302 int crespo_ipc_close(void *data)
304 struct crespo_ipc_transport_data *transport_data;
310 transport_data = (struct crespo_ipc_transport_data *) data;
312 fd = transport_data->fd;
316 transport_data->fd = -1;
322 int crespo_ipc_read(void *data, void *buffer, unsigned int length)
324 struct crespo_ipc_transport_data *transport_data;
328 if (data == NULL || buffer == NULL || length == 0)
331 transport_data = (struct crespo_ipc_transport_data *) data;
333 fd = transport_data->fd;
337 rc = ioctl(fd, IOCTL_MODEM_RECV, buffer);
344 int crespo_ipc_write(void *data, void *buffer, unsigned int length)
346 struct crespo_ipc_transport_data *transport_data;
350 if (data == NULL || buffer == NULL || length == 0)
353 transport_data = (struct crespo_ipc_transport_data *) data;
355 fd = transport_data->fd;
359 rc = ioctl(fd, IOCTL_MODEM_SEND, buffer);
366 int crespo_ipc_poll(void *data, struct timeval *timeout)
368 struct crespo_ipc_transport_data *transport_data;
376 transport_data = (struct crespo_ipc_transport_data *) data;
378 fd = transport_data->fd;
385 rc = select(FD_SETSIZE, &fds, NULL, NULL, timeout);
389 int crespo_ipc_power_on(void *data)
394 fd = open(CRESPO_MODEM_CTL_DEVICE, O_RDWR);
398 rc = ioctl(fd, IOCTL_MODEM_START);
408 int crespo_ipc_power_off(void *data)
413 fd = open(CRESPO_MODEM_CTL_DEVICE, O_RDWR);
417 rc = ioctl(fd, IOCTL_MODEM_OFF);
427 int crespo_ipc_data_create(void **transport_data, void **power_data, void **gprs_data)
429 if (transport_data == NULL)
432 *transport_data = (void *) malloc(sizeof(struct crespo_ipc_transport_data));
433 memset(*transport_data, 0, sizeof(struct crespo_ipc_transport_data));
438 int crespo_ipc_data_destroy(void *transport_data, void *power_data, void *gprs_data)
440 if (transport_data == NULL)
443 free(transport_data);
448 char *crespo_ipc_gprs_get_iface_single(int cid)
452 asprintf(&iface, "%s%d", CRESPO_GPRS_IFACE_PREFIX, 0);
457 int crespo_ipc_gprs_get_capabilities_single(struct ipc_client_gprs_capabilities *capabilities)
459 if (capabilities == NULL)
462 capabilities->port_list = 0;
463 capabilities->cid_max = 1;
468 char *crespo_ipc_gprs_get_iface(int cid)
472 if (cid > CRESPO_GPRS_IFACE_COUNT)
475 asprintf(&iface, "%s%d", CRESPO_GPRS_IFACE_PREFIX, cid - 1);
480 int crespo_ipc_gprs_get_capabilities(struct ipc_client_gprs_capabilities *capabilities)
482 if (capabilities == NULL)
485 capabilities->port_list = 0;
486 capabilities->cid_max = CRESPO_GPRS_IFACE_COUNT;
491 struct ipc_ops crespo_ipc_fmt_ops = {
492 .bootstrap = crespo_ipc_bootstrap,
493 .send = crespo_ipc_fmt_send,
494 .recv = crespo_ipc_fmt_recv,
497 struct ipc_ops crespo_ipc_rfs_ops = {
499 .send = crespo_ipc_rfs_send,
500 .recv = crespo_ipc_rfs_recv,
503 struct ipc_handlers crespo_ipc_handlers = {
504 .open = crespo_ipc_open,
505 .close = crespo_ipc_close,
506 .read = crespo_ipc_read,
507 .write = crespo_ipc_write,
508 .poll = crespo_ipc_poll,
509 .transport_data = NULL,
510 .power_on = crespo_ipc_power_on,
511 .power_off = crespo_ipc_power_off,
513 .gprs_activate = NULL,
514 .gprs_deactivate = NULL,
516 .data_create = crespo_ipc_data_create,
517 .data_destroy = crespo_ipc_data_destroy,
520 struct ipc_gprs_specs crespo_ipc_gprs_specs_single = {
521 .gprs_get_iface = crespo_ipc_gprs_get_iface_single,
522 .gprs_get_capabilities = crespo_ipc_gprs_get_capabilities_single,
525 struct ipc_gprs_specs crespo_ipc_gprs_specs = {
526 .gprs_get_iface = crespo_ipc_gprs_get_iface,
527 .gprs_get_capabilities = crespo_ipc_gprs_get_capabilities,
530 // vim:ts=4:sw=4:expandtab