zte: partially support Icera-based devices like T-Mobile Rocket 2

This commit is contained in:
Dan Williams
2010-12-31 00:30:47 -06:00
parent adcc29fa6b
commit 657e5ac7f6
3 changed files with 340 additions and 8 deletions

View File

@@ -328,5 +328,6 @@ CLEANFILES = $(BUILT_SOURCES)
EXTRA_DIST = \
$(udevrules_DATA) \
mm-modem-option-utils.c
mm-modem-option-utils.c \
mm-modem-icera-utils.c

View File

@@ -0,0 +1,256 @@
/* -*- 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) 2010 Red Hat, Inc.
*/
/******************************************
* Generic utilities for Icera-based modems
******************************************/
#include "mm-callback-info.h"
#include "mm-at-serial-port.h"
#include "mm-generic-gsm.h"
#include "mm-modem-helpers.h"
static void
icera_get_allowed_mode_done (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
MMCallbackInfo *info = (MMCallbackInfo *) user_data;
gboolean parsed = FALSE;
if (error)
info->error = g_error_copy (error);
else if (!g_str_has_prefix (response->str, "%IPSYS: ")) {
int a, b;
if (sscanf (response->str + 8, "%d,%d", &a, &b)) {
MMModemGsmAllowedMode mode = MM_MODEM_GSM_ALLOWED_MODE_ANY;
switch (a) {
case 0:
mode = MM_MODEM_GSM_ALLOWED_MODE_2G_ONLY;
break;
case 1:
mode = MM_MODEM_GSM_ALLOWED_MODE_3G_ONLY;
break;
case 2:
mode = MM_MODEM_GSM_ALLOWED_MODE_2G_PREFERRED;
break;
case 3:
mode = MM_MODEM_GSM_ALLOWED_MODE_3G_PREFERRED;
break;
default:
break;
}
mm_callback_info_set_result (info, GUINT_TO_POINTER (mode), NULL);
parsed = TRUE;
}
}
if (!error && !parsed)
info->error = g_error_new_literal (MM_MODEM_ERROR, MM_MODEM_ERROR_GENERAL,
"Could not parse allowed mode results");
mm_callback_info_schedule (info);
}
static void
icera_get_allowed_mode (MMGenericGsm *gsm,
MMModemUIntFn callback,
gpointer user_data)
{
MMCallbackInfo *info;
MMAtSerialPort *port;
info = mm_callback_info_uint_new (MM_MODEM (gsm), callback, user_data);
port = mm_generic_gsm_get_best_at_port (gsm, &info->error);
if (!port) {
mm_callback_info_schedule (info);
return;
}
mm_at_serial_port_queue_command (port, "%IPSYS?", 3, icera_get_allowed_mode_done, info);
}
static void
icera_set_allowed_mode_done (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
MMCallbackInfo *info = (MMCallbackInfo *) user_data;
if (error)
info->error = g_error_copy (error);
mm_callback_info_schedule (info);
}
static void
icera_set_allowed_mode (MMGenericGsm *gsm,
MMModemGsmAllowedMode mode,
MMModemFn callback,
gpointer user_data)
{
MMCallbackInfo *info;
MMAtSerialPort *port;
char *command;
int i;
info = mm_callback_info_new (MM_MODEM (gsm), callback, user_data);
port = mm_generic_gsm_get_best_at_port (gsm, &info->error);
if (!port) {
mm_callback_info_schedule (info);
return;
}
switch (mode) {
case MM_MODEM_GSM_ALLOWED_MODE_2G_ONLY:
i = 0;
break;
case MM_MODEM_GSM_ALLOWED_MODE_3G_ONLY:
i = 1;
break;
case MM_MODEM_GSM_ALLOWED_MODE_2G_PREFERRED:
i = 2;
break;
case MM_MODEM_GSM_ALLOWED_MODE_3G_PREFERRED:
i = 3;
break;
case MM_MODEM_GSM_ALLOWED_MODE_ANY:
default:
i = 5;
break;
}
command = g_strdup_printf ("%%IPSYS=%d", i);
mm_at_serial_port_queue_command (port, command, 3, icera_set_allowed_mode_done, info);
g_free (command);
}
static MMModemGsmAccessTech
nwstate_to_act (const char *str)
{
/* small 'g' means CS, big 'G' means PS */
if (!strcmp (str, "2G-GPRS"))
return MM_MODEM_GSM_ACCESS_TECH_GPRS;
else if (!strcmp (str, "2G-EDGE"))
return MM_MODEM_GSM_ACCESS_TECH_EDGE;
else if (!strcmp (str, "3G"))
return MM_MODEM_GSM_ACCESS_TECH_UMTS;
else if (!strcmp (str, "3G-HSDPA"))
return MM_MODEM_GSM_ACCESS_TECH_HSDPA;
else if (!strcmp (str, "3G-HSUPA"))
return MM_MODEM_GSM_ACCESS_TECH_HSUPA;
else if (!strcmp (str, "3G-HSDPA-HSUPA"))
return MM_MODEM_GSM_ACCESS_TECH_HSPA;
return MM_MODEM_GSM_ACCESS_TECH_UNKNOWN;
}
#define LAST_ACT_TAG "icera-last-act"
static void
icera_nwstate_changed (MMAtSerialPort *port,
GMatchInfo *info,
gpointer user_data)
{
MMModemGsmAccessTech act = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN;
char *str;
int rssi = -1;
str = g_match_info_fetch (info, 1);
if (str) {
rssi = atoi (str);
rssi = CLAMP (rssi, -1, 5);
g_free (str);
}
str = g_match_info_fetch (info, 3);
if (str) {
act = nwstate_to_act (str);
g_free (str);
}
g_object_set_data (G_OBJECT (user_data), LAST_ACT_TAG, GUINT_TO_POINTER (act));
mm_generic_gsm_update_access_technology (MM_GENERIC_GSM (user_data), act);
}
static void
icera_register_unsolicted_handlers (MMGenericGsm *modem, MMAtSerialPort *port)
{
GRegex *regex;
/* %NWSTATE: <rssi>,<mccmnc>,<tech>,<connected>,<regulation> */
regex = g_regex_new ("\\r\\n%NWSTATE:\\s*(\\d+),(\\d+),([^,]*),([^,]*),(\\d+)\\r\\n", G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
mm_at_serial_port_add_unsolicited_msg_handler (port, regex, icera_nwstate_changed, modem, NULL);
g_regex_unref (regex);
}
static void
icera_change_unsolicited_messages (MMGenericGsm *modem, gboolean enabled)
{
MMAtSerialPort *primary;
primary = mm_generic_gsm_get_at_port (modem, MM_PORT_TYPE_PRIMARY);
g_assert (primary);
mm_at_serial_port_queue_command (primary, enabled ? "%NWSTATE=1" : "%NWSTATE=0", 3, NULL, NULL);
}
static void
get_nwstate_done (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
MMCallbackInfo *info = user_data;
MMModemGsmAccessTech act = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN;
info->error = mm_modem_check_removed (info->modem, error);
if (!info->error) {
/* The unsolicited message handler will already have run and
* removed the NWSTATE response, so we have to work around that.
*/
act = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (info->modem), LAST_ACT_TAG));
mm_callback_info_set_result (info, GUINT_TO_POINTER (act), NULL);
g_object_set_data (G_OBJECT (info->modem), LAST_ACT_TAG, NULL);
}
mm_callback_info_schedule (info);
}
static void
icera_get_access_technology (MMGenericGsm *modem,
MMModemUIntFn callback,
gpointer user_data)
{
MMAtSerialPort *port;
MMCallbackInfo *info;
info = mm_callback_info_uint_new (MM_MODEM (modem), callback, user_data);
port = mm_generic_gsm_get_best_at_port (modem, &info->error);
if (!port) {
mm_callback_info_schedule (info);
return;
}
mm_at_serial_port_queue_command (port, "%NWSTATE=1", 3, get_nwstate_done, info);
}

View File

@@ -36,6 +36,7 @@ typedef struct {
gboolean init_retried;
guint32 cpms_tries;
guint cpms_timeout;
gboolean is_icera;
} MMModemZtePrivate;
MMModem *
@@ -58,6 +59,8 @@ mm_modem_zte_new (const char *device,
NULL));
}
#include "mm-modem-icera-utils.c"
/*****************************************************************************/
static void
@@ -149,9 +152,15 @@ get_allowed_mode (MMGenericGsm *gsm,
MMModemUIntFn callback,
gpointer user_data)
{
MMModemZte *self = MM_MODEM_ZTE (gsm);
MMCallbackInfo *info;
MMAtSerialPort *port;
if (MM_MODEM_ZTE_GET_PRIVATE (self)->is_icera) {
icera_get_allowed_mode (gsm, callback, user_data);
return;
}
info = mm_callback_info_uint_new (MM_MODEM (gsm), callback, user_data);
port = mm_generic_gsm_get_best_at_port (gsm, &info->error);
@@ -183,11 +192,17 @@ set_allowed_mode (MMGenericGsm *gsm,
MMModemFn callback,
gpointer user_data)
{
MMModemZte *self = MM_MODEM_ZTE (gsm);
MMCallbackInfo *info;
MMAtSerialPort *port;
char *command;
int cm_mode = 0, pref_acq = 0;
if (MM_MODEM_ZTE_GET_PRIVATE (self)->is_icera) {
icera_set_allowed_mode (gsm, mode, callback, user_data);
return;
}
info = mm_callback_info_new (MM_MODEM (gsm), callback, user_data);
port = mm_generic_gsm_get_best_at_port (gsm, &info->error);
@@ -248,16 +263,22 @@ get_act_request_done (MMAtSerialPort *port,
}
static void
get_access_technology (MMGenericGsm *modem,
get_access_technology (MMGenericGsm *gsm,
MMModemUIntFn callback,
gpointer user_data)
{
MMModemZte *self = MM_MODEM_ZTE (gsm);
MMAtSerialPort *port;
MMCallbackInfo *info;
info = mm_callback_info_uint_new (MM_MODEM (modem), callback, user_data);
if (MM_MODEM_ZTE_GET_PRIVATE (self)->is_icera) {
icera_get_access_technology (gsm, callback, user_data);
return;
}
port = mm_generic_gsm_get_best_at_port (modem, &info->error);
info = mm_callback_info_uint_new (MM_MODEM (gsm), callback, user_data);
port = mm_generic_gsm_get_best_at_port (gsm, &info->error);
if (!port) {
mm_callback_info_schedule (info);
return;
@@ -314,6 +335,10 @@ cpms_try_done (MMAtSerialPort *port,
}
}
/* Turn on unsolicited network state messages */
if (priv->is_icera)
icera_change_unsolicited_messages (MM_GENERIC_GSM (info->modem), TRUE);
mm_generic_gsm_enable_complete (MM_GENERIC_GSM (info->modem), error, info);
}
@@ -339,6 +364,19 @@ static void enable_flash_done (MMSerialPort *port,
GError *error,
gpointer user_data);
static void
icera_check_cb (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
if (!error) {
MMModemZte *self = MM_MODEM_ZTE (user_data);
MM_MODEM_ZTE_GET_PRIVATE (self)->is_icera = TRUE;
}
}
static void
pre_init_done (MMAtSerialPort *port,
GString *response,
@@ -358,6 +396,7 @@ pre_init_done (MMAtSerialPort *port,
mm_generic_gsm_enable_complete (MM_GENERIC_GSM (info->modem), error, info);
} else {
/* Finish the initialization */
mm_at_serial_port_queue_command (port, "%IPSYS?", 10, icera_check_cb, info->modem);
mm_at_serial_port_queue_command (port, "Z E0 V1 X4 &C1 +CMEE=1;+CFUN=1;", 10, init_modem_done, info);
}
}
@@ -389,19 +428,52 @@ do_enable (MMGenericGsm *modem, MMModemFn callback, gpointer user_data)
mm_serial_port_flash (MM_SERIAL_PORT (primary), 100, FALSE, enable_flash_done, info);
}
typedef struct {
MMModem *modem;
MMModemFn callback;
gpointer user_data;
} DisableInfo;
static void
disable_unsolicited_done (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
MMModem *parent_modem_iface;
DisableInfo *info = user_data;
parent_modem_iface = g_type_interface_peek_parent (MM_MODEM_GET_INTERFACE (info->modem));
parent_modem_iface->disable (info->modem, info->callback, info->user_data);
g_free (info);
}
static void
disable (MMModem *modem,
MMModemFn callback,
gpointer user_data)
{
MMModemZtePrivate *priv = MM_MODEM_ZTE_GET_PRIVATE (modem);
MMModem *parent_modem_iface;
MMAtSerialPort *primary;
DisableInfo *info;
priv->init_retried = FALSE;
/* Do the normal disable stuff */
parent_modem_iface = g_type_interface_peek_parent (MM_MODEM_GET_INTERFACE (modem));
parent_modem_iface->disable (modem, callback, user_data);
info = g_malloc0 (sizeof (DisableInfo));
info->callback = callback;
info->user_data = user_data;
info->modem = modem;
primary = mm_generic_gsm_get_at_port (MM_GENERIC_GSM (modem), MM_PORT_TYPE_PRIMARY);
g_assert (primary);
/* Turn off unsolicited responses */
if (priv->is_icera)
icera_change_unsolicited_messages (MM_GENERIC_GSM (modem), FALSE);
/* Random command to ensure unsolicited message disable completes */
mm_at_serial_port_queue_command (primary, "E0", 5, disable_unsolicited_done, info);
}
static gboolean
@@ -453,6 +525,9 @@ grab_port (MMModem *modem,
regex = g_regex_new ("\\r\\n\\+ZEND\\r\\n", G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
mm_at_serial_port_add_unsolicited_msg_handler (MM_AT_SERIAL_PORT (port), regex, NULL, NULL, NULL);
g_regex_unref (regex);
/* Add Icera-specific handlers */
icera_register_unsolicted_handlers (gsm, MM_AT_SERIAL_PORT (port));
}
return !!port;