From 1bfa2749cd1f0c361a33c48770dea581b254d22b Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Mon, 18 Sep 2017 21:27:55 -0500 Subject: [PATCH] mbim: Add skeleton mbim_device object --- Makefile.am | 6 +- drivers/mbimmodem/mbim.c | 162 +++++++++++++++++++++++++++++++++++++++ drivers/mbimmodem/mbim.h | 39 ++++++++++ 3 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 drivers/mbimmodem/mbim.c create mode 100644 drivers/mbimmodem/mbim.h diff --git a/Makefile.am b/Makefile.am index c95a9ee9..2b48724c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -607,8 +607,12 @@ builtin_cflags += @ELL_CFLAGS@ builtin_libadd += @ELL_LIBS@ if MBIMMODEM +mbim_sources = drivers/mbimmodem/mbim.h \ + drivers/mbimmodem/mbim.c + builtin_modules += mbimmodem -builtin_sources += drivers/mbimmodem/util.h \ +builtin_sources += $(mbim_sources) \ + drivers/mbimmodem/util.h \ drivers/mbimmodem/mbimmodem.h \ drivers/mbimmodem/mbimmodem.c diff --git a/drivers/mbimmodem/mbim.c b/drivers/mbimmodem/mbim.c new file mode 100644 index 00000000..7cc1738c --- /dev/null +++ b/drivers/mbimmodem/mbim.c @@ -0,0 +1,162 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2017 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include + +#include "mbim.h" + +struct mbim_device { + int ref_count; + int fd; + struct l_io *io; + mbim_device_debug_func_t debug_handler; + void *debug_data; + mbim_device_destroy_func_t debug_destroy; + mbim_device_disconnect_func_t disconnect_handler; + void *disconnect_data; + mbim_device_destroy_func_t disconnect_destroy; + + bool close_on_unref : 1; +}; + +static void disconnect_handler(struct l_io *io, void *user_data) +{ + struct mbim_device *device = user_data; + + l_util_debug(device->debug_handler, device->debug_data, "disconnect"); + + if (device->disconnect_handler) + device->disconnect_handler(device->disconnect_data); +} + +static bool open_write_handler(struct l_io *io, void *user_data) +{ + return false; +} + +static bool open_read_handler(struct l_io *io, void *user_data) +{ + return true; +} + +struct mbim_device *mbim_device_new(int fd) +{ + struct mbim_device *device; + + if (unlikely(fd < 0)) + return NULL; + + device = l_new(struct mbim_device, 1); + + device->fd = fd; + device->close_on_unref = false; + + device->io = l_io_new(fd); + l_io_set_disconnect_handler(device->io, disconnect_handler, + device, NULL); + + l_io_set_read_handler(device->io, open_read_handler, device, NULL); + l_io_set_write_handler(device->io, open_write_handler, device, NULL); + + return mbim_device_ref(device); +} + +struct mbim_device *mbim_device_ref(struct mbim_device *device) +{ + if (unlikely(!device)) + return NULL; + + __sync_fetch_and_add(&device->ref_count, 1); + + return device; +} + +void mbim_device_unref(struct mbim_device *device) +{ + if (unlikely(!device)) + return; + + if (__sync_sub_and_fetch(&device->ref_count, 1)) + return; + + l_io_destroy(device->io); + + if (device->close_on_unref) + close(device->fd); + + if (device->debug_destroy) + device->debug_destroy(device->debug_data); + + if (device->disconnect_destroy) + device->disconnect_destroy(device->disconnect_data); + + l_free(device); +} + +bool mbim_device_set_disconnect_handler(struct mbim_device *device, + mbim_device_disconnect_func_t function, + void *user_data, + mbim_device_destroy_func_t destroy) +{ + if (unlikely(!device)) + return false; + + if (device->disconnect_destroy) + device->disconnect_destroy(device->disconnect_data); + + device->disconnect_handler = function; + device->disconnect_destroy = destroy; + device->disconnect_data = user_data; + + return true; +} + +bool mbim_device_set_debug(struct mbim_device *device, + mbim_device_debug_func_t func, void *user_data, + mbim_device_destroy_func_t destroy) +{ + if (unlikely(!device)) + return false; + + if (device->debug_destroy) + device->debug_destroy(device->debug_data); + + device->debug_handler = func; + device->debug_data = user_data; + device->debug_destroy = destroy; + + return true; +} + +bool mbim_device_set_close_on_unref(struct mbim_device *device, bool do_close) +{ + if (unlikely(!device)) + return false; + + device->close_on_unref = do_close; + return true; +} diff --git a/drivers/mbimmodem/mbim.h b/drivers/mbimmodem/mbim.h new file mode 100644 index 00000000..7fe754b4 --- /dev/null +++ b/drivers/mbimmodem/mbim.h @@ -0,0 +1,39 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2017 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +struct mbim_device; + +typedef void (*mbim_device_debug_func_t) (const char *str, void *user_data); +typedef void (*mbim_device_disconnect_func_t) (void *user_data); +typedef void (*mbim_device_destroy_func_t) (void *user_data); + +struct mbim_device *mbim_device_new(int fd); +bool mbim_device_set_close_on_unref(struct mbim_device *device, bool do_close); +struct mbim_device *mbim_device_ref(struct mbim_device *device); +void mbim_device_unref(struct mbim_device *device); + +bool mbim_device_set_debug(struct mbim_device *device, + mbim_device_debug_func_t func, void *user_data, + mbim_device_destroy_func_t destroy); +bool mbim_device_set_disconnect_handler(struct mbim_device *device, + mbim_device_disconnect_func_t function, + void *user_data, + mbim_device_destroy_func_t destroy);