aboutsummaryrefslogtreecommitdiff
path: root/plugins/qcom-soc/mm-broadband-modem-qmi-qcom-soc.c
blob: 0dc54923c09423aa9c6353bfa6e9e4e9fd3b1163 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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:
 *
 * Copyright (C) 2020 Aleksander Morgado <aleksander@aleksander.es>
 */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#include "ModemManager.h"
#include "mm-log.h"
#include "mm-broadband-modem-qmi-qcom-soc.h"

G_DEFINE_TYPE (MMBroadbandModemQmiQcomSoc, mm_broadband_modem_qmi_qcom_soc, MM_TYPE_BROADBAND_MODEM_QMI)

/*****************************************************************************/

static const QmiSioPort sio_port_per_port_number[] = {
    QMI_SIO_PORT_A2_MUX_RMNET0,
    QMI_SIO_PORT_A2_MUX_RMNET1,
    QMI_SIO_PORT_A2_MUX_RMNET2,
    QMI_SIO_PORT_A2_MUX_RMNET3,
    QMI_SIO_PORT_A2_MUX_RMNET4,
    QMI_SIO_PORT_A2_MUX_RMNET5,
    QMI_SIO_PORT_A2_MUX_RMNET6,
    QMI_SIO_PORT_A2_MUX_RMNET7
};

static MMPortQmi *
peek_port_qmi_for_data_bam_dmux (MMBroadbandModemQmi  *self,
                                 MMPort               *data,
                                 QmiSioPort           *out_sio_port,
                                 GError              **error)
{
    GList          *rpmsg_qmi_ports;
    MMPortQmi      *found = NULL;
    MMKernelDevice *net_port;
    gint            net_port_number;

    net_port = mm_port_peek_kernel_device (data);

    /* The dev_port notified by the bam-dmux driver indicates which SIO port we should be using */
    net_port_number = mm_kernel_device_get_attribute_as_int (net_port, "dev_port");
    if (net_port_number < 0 || net_port_number >= (gint) G_N_ELEMENTS (sio_port_per_port_number)) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_NOT_FOUND,
                     "Couldn't find SIO port number for 'net/%s'",
                     mm_port_get_device (data));
        return NULL;
    }

    /* Find one QMI port, we don't care which one */
    rpmsg_qmi_ports = mm_base_modem_find_ports (MM_BASE_MODEM (self),
                                                MM_PORT_SUBSYS_RPMSG,
                                                MM_PORT_TYPE_QMI);
    if (!rpmsg_qmi_ports) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_NOT_FOUND,
                     "Couldn't find any QMI port for 'net/%s'",
                     mm_port_get_device (data));
        return NULL;
    }

    /* Set outputs */
    if (out_sio_port)
        *out_sio_port = sio_port_per_port_number[net_port_number];
    found = MM_PORT_QMI (rpmsg_qmi_ports->data);

    g_list_free_full (rpmsg_qmi_ports, g_object_unref);

    return found;
}

static MMPortQmi *
peek_port_qmi_for_data_ipa (MMBroadbandModemQmi  *self,
                            MMPort               *data,
                            QmiSioPort           *out_sio_port,
                            GError              **error)
{
    MMPortQmi *found = NULL;

    /* when using IPA, we have a master network interface that will be multiplexed
     * to create link interfaces. We can assume any of the available QMI ports is
     * able to manage that. */

    found = mm_broadband_modem_qmi_peek_port_qmi (self);

    if (!found)
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_NOT_FOUND,
                     "Couldn't find any QMI port for 'net/%s'",
                     mm_port_get_device (data));
    else if (out_sio_port)
        *out_sio_port = QMI_SIO_PORT_NONE;

    return found;
}

static MMPortQmi *
peek_port_qmi_for_data (MMBroadbandModemQmi  *self,
                        MMPort               *data,
                        QmiSioPort           *out_sio_port,
                        GError              **error)
{
    MMKernelDevice *net_port;
    const gchar    *net_port_driver;

    g_assert (MM_IS_BROADBAND_MODEM_QMI (self));
    g_assert (mm_port_get_subsys (data) == MM_PORT_SUBSYS_NET);

    net_port = mm_port_peek_kernel_device (data);
    net_port_driver = mm_kernel_device_get_driver (net_port);

    if (g_strcmp0 (net_port_driver, "ipa") == 0)
        return peek_port_qmi_for_data_ipa (self, data, out_sio_port, error);

    if (g_strcmp0 (net_port_driver, "bam-dmux") == 0)
        return peek_port_qmi_for_data_bam_dmux (self, data, out_sio_port, error);

    g_set_error (error,
                 MM_CORE_ERROR,
                 MM_CORE_ERROR_FAILED,
                 "Unsupported QMI kernel driver for 'net/%s': %s",
                 mm_port_get_device (data),
                 net_port_driver);
    return NULL;
}

/*****************************************************************************/

MMBroadbandModemQmiQcomSoc *
mm_broadband_modem_qmi_qcom_soc_new (const gchar  *device,
                                     const gchar **drivers,
                                     const gchar  *plugin,
                                     guint16       vendor_id,
                                     guint16       product_id)
{
    return g_object_new (MM_TYPE_BROADBAND_MODEM_QMI_QCOM_SOC,
                         MM_BASE_MODEM_DEVICE,     device,
                         MM_BASE_MODEM_DRIVERS,    drivers,
                         MM_BASE_MODEM_PLUGIN,     plugin,
                         MM_BASE_MODEM_VENDOR_ID,  vendor_id,
                         MM_BASE_MODEM_PRODUCT_ID, product_id,
                         /* QMI bearer supports NET only */
                         MM_BASE_MODEM_DATA_NET_SUPPORTED, TRUE,
                         MM_BASE_MODEM_DATA_TTY_SUPPORTED, FALSE,
                         NULL);
}

static void
mm_broadband_modem_qmi_qcom_soc_init (MMBroadbandModemQmiQcomSoc *self)
{
}

static void
mm_broadband_modem_qmi_qcom_soc_class_init (MMBroadbandModemQmiQcomSocClass *klass)
{
    MMBroadbandModemQmiClass *broadband_modem_qmi_class = MM_BROADBAND_MODEM_QMI_CLASS (klass);

    broadband_modem_qmi_class->peek_port_qmi_for_data = peek_port_qmi_for_data;
}