[zaptel-commits] tzafrir: branch tzafrir/sysfs r3151 - /team/tzafrir/sysfs/zaptel-sysfs.c

SVN commits to the Zaptel project zaptel-commits at lists.digium.com
Wed Oct 17 04:57:44 CDT 2007


Author: tzafrir
Date: Wed Oct 17 04:57:44 2007
New Revision: 3151

URL: http://svn.digium.com/view/zaptel?view=rev&rev=3151
Log:
Better include the extra source file zatel-sysfs.c as well.

Added:
    team/tzafrir/sysfs/zaptel-sysfs.c   (with props)

Added: team/tzafrir/sysfs/zaptel-sysfs.c
URL: http://svn.digium.com/view/zaptel/team/tzafrir/sysfs/zaptel-sysfs.c?view=auto&rev=3151
==============================================================================
--- team/tzafrir/sysfs/zaptel-sysfs.c (added)
+++ team/tzafrir/sysfs/zaptel-sysfs.c Wed Oct 17 04:57:44 2007
@@ -1,0 +1,293 @@
+/*
+ * Written by Tzafrir Cohen <tzafrir.cohen at xorcom.com>
+ * Copyright (C) 2007, Xorcom
+ *
+ * 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 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.
+ *
+ * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+
+/****************************************************
+ *
+ * Device Model Stuff
+ *
+ */
+#include <linux/version.h>
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
+#  warning "This module is tested only with 2.6 kernels"
+#endif
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/device.h>
+#include "zap_dbg.h"
+#include "zaptel.h"
+#include "zaptel-sysfs.h"
+#include "zaptel-base.h"
+
+static const char rcsid[] = "$Id";
+
+/* Kernel versions... */
+/*
+ * Hotplug replaced with uevent in 2.6.16
+ */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
+#define	OLD_HOPLUG_SUPPORT	// for older kernels
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
+#define	DEVICE_ATTR_FUNC(name,dev,buf)	\
+		ssize_t name(struct device *dev, struct device_attribute *attr, char *buf)
+#else
+#define	DEVICE_ATTR_FUNC(name,dev,buf)	\
+		ssize_t name(struct device *dev, char *buf)
+#endif
+
+/* Always match, because the zaptel core handles all devices. */
+static int zap_bus_match(struct device *dev, struct device_driver *driver)
+{
+	zap_dbg("dev->bus_id = %s, driver->name = %s\n", dev->bus_id, driver->name);
+	return 1;
+}
+
+#ifdef OLD_HOPLUG_SUPPORT
+static int zap_bus_hotplug(struct device *dev, char **envp, int envnum, char *buff, int bufsize)
+{
+	struct zt_span	*span;
+
+	if(!dev)
+		return -ENODEV;
+	span = dev_to_span(dev);
+	envp[0] = buff;
+	if(snprintf(buff, bufsize, "XBUS_NAME=%s", span->name) >= bufsize)
+		return -ENOMEM;
+	envp[1] = NULL;
+	return 0;
+}
+#else
+
+#define SPAN_ADD_UEVENT_VAR(fmt, val...)			\
+	do {							\
+		int err = add_uevent_var(envp, num_envp, &i,	\
+				buffer, buffer_size, &len,	\
+				fmt, val);			\
+		if (err)					\
+		return err;					\
+	} while (0)
+
+static int zap_bus_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
+{
+	struct zt_span	*span;
+	int	i = 0;
+	int	len = 0;
+
+	if(!dev)
+		return -ENODEV;
+	span = dev_to_span(dev);
+	zap_dbg("bus_id=%s span=%s\n", dev->bus_id, span->name);
+	SPAN_ADD_UEVENT_VAR("SPAN_NUM=%02d", span->spanno);
+	SPAN_ADD_UEVENT_VAR("SPAN_NAME=%s", span->name);
+	envp[i] = NULL;
+	return 0;
+}
+#endif
+
+static void zap_bus_release(struct device *dev)
+{
+	zap_dbg("\n");
+}
+
+static void zap_dev_release(struct device *dev)
+{
+	struct zt_span	*span;
+
+	BUG_ON(!dev);
+	span = dev_to_span(dev);
+	zap_dbg_span(span, "\n");
+}
+
+static struct bus_type zap_bus_type = {
+	.name           = "zaptel",
+	.match          = zap_bus_match,
+#ifdef OLD_HOPLUG_SUPPORT
+	.hotplug 	= zap_bus_hotplug,
+#else
+	.uevent         = zap_bus_uevent,
+#endif
+};
+
+static struct device zap_bus = {
+	.bus_id		= "zapbus",
+	.release	= zap_bus_release
+};
+
+static struct device_driver zap_driver = {
+	.name		= "zapdrv",
+	.bus		= &zap_bus_type,
+#ifndef OLD_HOPLUG_SUPPORT
+	.owner		= THIS_MODULE
+#endif
+};
+
+int register_zap_bus(void)
+{
+	int	ret;
+
+	if((ret = bus_register(&zap_bus_type)) < 0) {
+		zap_err("%s: bus_register failed. Error number %d", __FUNCTION__, ret);
+		goto failed_bus;
+	}
+	if((ret = device_register(&zap_bus)) < 0) {
+		zap_err("%s: registration of zap_bus failed. Error number %d",
+			__FUNCTION__, ret);
+		goto failed_busdevice;
+	}
+	if((ret = driver_register(&zap_driver)) < 0) {
+		zap_err("%s: driver_register failed. Error number %d", __FUNCTION__, ret);
+		goto failed_driver;
+	}
+	return 0;
+failed_driver:
+	device_unregister(&zap_bus);
+failed_busdevice:
+	bus_unregister(&zap_bus_type);
+failed_bus:
+	return ret;
+}
+
+void unregister_zap_bus(void)
+{
+	driver_unregister(&zap_driver);
+	device_unregister(&zap_bus);
+	bus_unregister(&zap_bus_type);
+}
+
+/*--------- Sysfs Device handling ----*/
+static DEVICE_ATTR_FUNC(digital_coding_show, dev, buf)
+{
+	struct	zt_span *span = dev_to_span(dev);
+	int	lineconfig = span->lineconfig;
+	char*	name;
+
+	lineconfig &= ~(ZT_CONFIG_AMI | ZT_CONFIG_B8ZS | ZT_CONFIG_HDB3 );
+	switch (lineconfig) {
+		case ZT_CONFIG_AMI: name="ami"; break;
+		case ZT_CONFIG_B8ZS: name="b8zf"; break;
+		case ZT_CONFIG_HDB3: name="hdb3"; break;
+		default: name="unknown"; break;
+	}
+	return snprintf(buf, PAGE_SIZE, "%s\n", name);
+}
+static	DEVICE_ATTR(digital_coding, S_IRUGO, digital_coding_show, NULL);
+
+static DEVICE_ATTR_FUNC(digital_framing_show, dev, buf)
+{
+	struct	zt_span *span = dev_to_span(dev);
+	int	lineconfig = span->lineconfig;
+	char*	name;
+
+	lineconfig &= ~(ZT_CONFIG_D4 | ZT_CONFIG_ESF | ZT_CONFIG_CCS);
+	switch (lineconfig) {
+		case 0: name="cas"; break;
+		case ZT_CONFIG_D4: name="d4"; break;
+		case ZT_CONFIG_ESF: name="esf"; break;
+		case ZT_CONFIG_CCS: name="ccs"; break;
+		default: name="unknown"; break;
+	}
+	return snprintf(buf, PAGE_SIZE, "%s\n", name);
+}
+static	DEVICE_ATTR(digital_framing, S_IRUGO, digital_framing_show, NULL);
+
+static DEVICE_ATTR_FUNC(flags_show, dev, buf)
+{
+	struct zt_span *span = dev_to_span(dev);
+	return snprintf(buf, PAGE_SIZE, "%x\n", span->flags);
+}
+static	DEVICE_ATTR(flags, S_IRUGO, flags_show, NULL);
+
+static DEVICE_ATTR_FUNC(name_show, dev, buf)
+{
+	struct zt_span *span = dev_to_span(dev);
+	return snprintf(buf, PAGE_SIZE, "%s\n", span->name);
+}
+static	DEVICE_ATTR(name, S_IRUGO, name_show, NULL);
+
+static DEVICE_ATTR_FUNC(sync_master_show, dev, buf)
+{
+	struct zt_span *span = dev_to_span(dev);
+	return snprintf(buf, PAGE_SIZE, "%s\n", (span == master) ? "Y":"N");
+}
+static	DEVICE_ATTR(sync_master, S_IRUGO, sync_master_show, NULL);
+
+void span_sysfs_remove(struct zt_span *span)
+{
+	struct device	*device;
+
+	BUG_ON(!span);
+	zap_dbg_span(span, "\n");
+	device = &span->device;
+	BUG_ON(!device);
+	device_remove_file(&span->device, &dev_attr_digital_coding);
+	device_remove_file(&span->device, &dev_attr_digital_framing);
+	device_remove_file(&span->device, &dev_attr_flags);
+	device_remove_file(&span->device, &dev_attr_name);
+	device_remove_file(&span->device, &dev_attr_sync_master);
+	device_unregister(&span->device);
+}
+
+#define span_add_attribute(name) \
+	ret = device_create_file(device, &dev_attr_##name); \
+	if(ret) { \
+		zap_err_span(span, "%s: device_create_file failed (flags): %d\n", __FUNCTION__, ret); \
+		goto out; \
+	}
+int span_sysfs_create(struct zt_span *span)
+{
+	struct device	*device;
+	int		ret = 0;
+
+	BUG_ON(!span);
+	device = &span->device;
+	BUG_ON(!device);
+	zap_dbg_span(span, "\n");
+	device_initialize(device);
+	device->bus = &zap_bus_type;
+	device->parent = &zap_bus;
+	snprintf(device->bus_id, BUS_ID_SIZE, "span-%02d", span->spanno);
+	device->driver_data = NULL;	/* FIXME: add some usefull data */
+	device->release = zap_dev_release;
+	ret = device_register(device);
+	if(ret) {
+		zap_err_span(span, "%s: device_add failed: %d\n", __FUNCTION__, ret);
+		goto out;
+	}
+	span_add_attribute(digital_coding);
+	span_add_attribute(digital_framing);
+	span_add_attribute(flags);
+	span_add_attribute(name);
+	span_add_attribute(sync_master);
+out:
+	return ret;
+}
+/*
+ * End device Model Stuff
+ *
+ ****************************************************/
+
+

Propchange: team/tzafrir/sysfs/zaptel-sysfs.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/tzafrir/sysfs/zaptel-sysfs.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/tzafrir/sysfs/zaptel-sysfs.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the zaptel-commits mailing list