[RFC Patch v1 53/55] ARC: Hostlink Pseudo-Driver for Metaware Debugger

2012-11-12 Thread Vineet.Gupta1
From: Vineet Gupta 

This allows ARC Target to do I/O to host in absence of any peripherals
whatsoever, assisted by Metaware Hostlink facility.

Further we have a FUSE based filesystem which makes us mount/access host
filesystem on target and do fops.

Signed-off-by: Vineet Gupta 
---
 arch/arc/Kconfig   |9 +++
 arch/arc/kernel/Makefile   |1 +
 arch/arc/kernel/arc_hostlink.c |  114 
 3 files changed, 124 insertions(+), 0 deletions(-)
 create mode 100644 arch/arc/kernel/arc_hostlink.c

diff --git a/arch/arc/Kconfig b/arch/arc/Kconfig
index 79e8f2f..4653070 100644
--- a/arch/arc/Kconfig
+++ b/arch/arc/Kconfig
@@ -394,6 +394,15 @@ config HZ
int "Timer Frequency"
default 100
 
+config ARC_METAWARE_HLINK
+   bool "Support for Metaware debugger assisted Host access"
+   default n
+   help
+ This options allows a Linux userland apps to directly access
+ host file system (open/creat/read/write etc) with help from
+ Metaware Debugger. This can come in handy for Linux-host communication
+ when there is no real usable peripheral such as EMAC.
+
 menuconfig ARC_DBG
bool "ARC debugging"
default y
diff --git a/arch/arc/kernel/Makefile b/arch/arc/kernel/Makefile
index a2b7493..ce14b45 100644
--- a/arch/arc/kernel/Makefile
+++ b/arch/arc/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_ARC_DW2_UNWIND)  += unwind.o
 obj-$(CONFIG_KPROBES)  += kprobes.o
 obj-$(CONFIG_ARC_MISALIGN_ACCESS)  += unaligned.o
 obj-$(CONFIG_KGDB) += kgdb.o
+obj-$(CONFIG_ARC_METAWARE_HLINK)   += arc_hostlink.o
 
 obj-$(CONFIG_ARC_FPU_SAVE_RESTORE) += fpu.o
 CFLAGS_fpu.o   += -mdpfp
diff --git a/arch/arc/kernel/arc_hostlink.c b/arch/arc/kernel/arc_hostlink.c
new file mode 100644
index 000..de3bc42
--- /dev/null
+++ b/arch/arc/kernel/arc_hostlink.c
@@ -0,0 +1,114 @@
+/*
+ * arc_hostlink.c: Pseudo-driver for Metaware provided "hostlink" facility
+ *
+ * Allows Linux userland access to host in absence of any peripherals.
+ *
+ * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
+ *
+ * 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.
+ */
+
+#include   /* file_operations */
+#include   /* class_create */
+#include /* cdev */
+#include   /* VM_IO */
+#include 
+#include 
+
+static unsigned char __HOSTLINK__[4 * PAGE_SIZE] __aligned(PAGE_SIZE);
+
+static int arc_hl_mmap(struct file *fp, struct vm_area_struct *vma);
+static long arc_hl_ioctl(struct file *file, unsigned int cmd,
+   unsigned long arg);
+
+static const struct file_operations arc_hl_fops = {
+   .owner  = THIS_MODULE,
+   .unlocked_ioctl = arc_hl_ioctl,
+   .mmap   = arc_hl_mmap,
+};
+
+static int arc_hl_major;
+static int arc_hl_minor;
+static int arc_hl_nr_devs = 1;
+static const char arc_hl_devnm[] = "hostlink";
+static struct cdev arc_hl_cdev;
+static struct class *arc_hl_class;
+
+static int __init arc_hl_linux_glue(void)
+{
+   dev_t arc_hl_dev;
+   int i;
+
+   if (arc_hl_major) { /* Preallocated MAJOR */
+
+   arc_hl_dev = MKDEV(arc_hl_major, arc_hl_minor);
+   register_chrdev_region(arc_hl_dev, arc_hl_nr_devs,
+  arc_hl_devnm);
+   } else {/* allocates Major to devices */
+   alloc_chrdev_region(_hl_dev, 0, arc_hl_nr_devs,
+   arc_hl_devnm);
+   arc_hl_major = MAJOR(arc_hl_dev);
+   }
+
+   /* Populate sysfs entries: creates /sys/class/ sub-node for device */
+
+   arc_hl_class = class_create(THIS_MODULE, arc_hl_devnm);
+
+   /* connect file ops with cdev */
+
+   cdev_init(_hl_cdev, _hl_fops);
+   arc_hl_cdev.owner = THIS_MODULE;
+
+   /* Connect major/minor number to cdev
+* makes device available.
+*  device nodes created with 'mknod` are probably already active.
+*/
+
+   cdev_add(_hl_cdev, arc_hl_dev, arc_hl_nr_devs);
+
+   /* creates /sys/devices/virtual// node with
+*  link from/sys/class/, needed by mdev.
+*/
+
+   for (i = 0; i < arc_hl_nr_devs; i++)
+   device_create(arc_hl_class, NULL, MKDEV(MAJOR(arc_hl_dev), i),
+ NULL, arc_hl_devnm);
+
+   pr_info("Hostlink dev to mknod is %d:%d\n",
+   arc_hl_major, arc_hl_minor);
+   return 0;
+}
+
+static int __init arc_hl_init(void)
+{
+   arc_hl_linux_glue();
+
+   pr_info("Hlink buffer mmap @ 0x%p\n", __HOSTLINK__);
+
+   return 0;
+}
+
+static int arc_hl_mmap(struct file *fp, struct vm_area_struct *vma)
+{
+   vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+   if 

[RFC Patch v1 53/55] ARC: Hostlink Pseudo-Driver for Metaware Debugger

2012-11-12 Thread Vineet.Gupta1
From: Vineet Gupta vgu...@synopsys.com

This allows ARC Target to do I/O to host in absence of any peripherals
whatsoever, assisted by Metaware Hostlink facility.

Further we have a FUSE based filesystem which makes us mount/access host
filesystem on target and do fops.

Signed-off-by: Vineet Gupta vgu...@synopsys.com
---
 arch/arc/Kconfig   |9 +++
 arch/arc/kernel/Makefile   |1 +
 arch/arc/kernel/arc_hostlink.c |  114 
 3 files changed, 124 insertions(+), 0 deletions(-)
 create mode 100644 arch/arc/kernel/arc_hostlink.c

diff --git a/arch/arc/Kconfig b/arch/arc/Kconfig
index 79e8f2f..4653070 100644
--- a/arch/arc/Kconfig
+++ b/arch/arc/Kconfig
@@ -394,6 +394,15 @@ config HZ
int Timer Frequency
default 100
 
+config ARC_METAWARE_HLINK
+   bool Support for Metaware debugger assisted Host access
+   default n
+   help
+ This options allows a Linux userland apps to directly access
+ host file system (open/creat/read/write etc) with help from
+ Metaware Debugger. This can come in handy for Linux-host communication
+ when there is no real usable peripheral such as EMAC.
+
 menuconfig ARC_DBG
bool ARC debugging
default y
diff --git a/arch/arc/kernel/Makefile b/arch/arc/kernel/Makefile
index a2b7493..ce14b45 100644
--- a/arch/arc/kernel/Makefile
+++ b/arch/arc/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_ARC_DW2_UNWIND)  += unwind.o
 obj-$(CONFIG_KPROBES)  += kprobes.o
 obj-$(CONFIG_ARC_MISALIGN_ACCESS)  += unaligned.o
 obj-$(CONFIG_KGDB) += kgdb.o
+obj-$(CONFIG_ARC_METAWARE_HLINK)   += arc_hostlink.o
 
 obj-$(CONFIG_ARC_FPU_SAVE_RESTORE) += fpu.o
 CFLAGS_fpu.o   += -mdpfp
diff --git a/arch/arc/kernel/arc_hostlink.c b/arch/arc/kernel/arc_hostlink.c
new file mode 100644
index 000..de3bc42
--- /dev/null
+++ b/arch/arc/kernel/arc_hostlink.c
@@ -0,0 +1,114 @@
+/*
+ * arc_hostlink.c: Pseudo-driver for Metaware provided hostlink facility
+ *
+ * Allows Linux userland access to host in absence of any peripherals.
+ *
+ * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
+ *
+ * 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.
+ */
+
+#include linux/fs.h  /* file_operations */
+#include linux/device.h  /* class_create */
+#include linux/cdev.h/* cdev */
+#include linux/mm.h  /* VM_IO */
+#include linux/module.h
+#include linux/uaccess.h
+
+static unsigned char __HOSTLINK__[4 * PAGE_SIZE] __aligned(PAGE_SIZE);
+
+static int arc_hl_mmap(struct file *fp, struct vm_area_struct *vma);
+static long arc_hl_ioctl(struct file *file, unsigned int cmd,
+   unsigned long arg);
+
+static const struct file_operations arc_hl_fops = {
+   .owner  = THIS_MODULE,
+   .unlocked_ioctl = arc_hl_ioctl,
+   .mmap   = arc_hl_mmap,
+};
+
+static int arc_hl_major;
+static int arc_hl_minor;
+static int arc_hl_nr_devs = 1;
+static const char arc_hl_devnm[] = hostlink;
+static struct cdev arc_hl_cdev;
+static struct class *arc_hl_class;
+
+static int __init arc_hl_linux_glue(void)
+{
+   dev_t arc_hl_dev;
+   int i;
+
+   if (arc_hl_major) { /* Preallocated MAJOR */
+
+   arc_hl_dev = MKDEV(arc_hl_major, arc_hl_minor);
+   register_chrdev_region(arc_hl_dev, arc_hl_nr_devs,
+  arc_hl_devnm);
+   } else {/* allocates Major to devices */
+   alloc_chrdev_region(arc_hl_dev, 0, arc_hl_nr_devs,
+   arc_hl_devnm);
+   arc_hl_major = MAJOR(arc_hl_dev);
+   }
+
+   /* Populate sysfs entries: creates /sys/class/ sub-node for device */
+
+   arc_hl_class = class_create(THIS_MODULE, arc_hl_devnm);
+
+   /* connect file ops with cdev */
+
+   cdev_init(arc_hl_cdev, arc_hl_fops);
+   arc_hl_cdev.owner = THIS_MODULE;
+
+   /* Connect major/minor number to cdev
+* makes device available.
+*  device nodes created with 'mknod` are probably already active.
+*/
+
+   cdev_add(arc_hl_cdev, arc_hl_dev, arc_hl_nr_devs);
+
+   /* creates /sys/devices/virtual/dev_name/names[i] node with
+*  link from/sys/class/dev_name, needed by mdev.
+*/
+
+   for (i = 0; i  arc_hl_nr_devs; i++)
+   device_create(arc_hl_class, NULL, MKDEV(MAJOR(arc_hl_dev), i),
+ NULL, arc_hl_devnm);
+
+   pr_info(Hostlink dev to mknod is %d:%d\n,
+   arc_hl_major, arc_hl_minor);
+   return 0;
+}
+
+static int __init arc_hl_init(void)
+{
+   arc_hl_linux_glue();
+
+   pr_info(Hlink buffer mmap @ 0x%p\n, __HOSTLINK__);
+
+   return 0;
+}
+
+static int