Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updates

  • Loading branch information...
commit c74ec321dceb285d5142158f03447de749735947 1 parent 2f51adf
@makelinux authored
View
8 Makefile
@@ -30,8 +30,10 @@ dio: CPPFLAGS+= -DCTRACER_ON -include ctracer.h -g
#dio: CPPFLAGS+= -D VERBOSE
#_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h ldt_configfs_basic.c ctracer.h tracing.h
-_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h ldt_configfs_basic.c
+_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h ldt_configfs_basic.c misc_drv.c
checkpatch:
- #/usr/src/linux-headers-$(shell uname -r)/scripts/checkpatch.pl --no-tree --show-types --ignore LONG_LINE,LINE_CONTINUATIONS --terse -f $(_src) Makefile
- /home/const/main/mix-prj/linux-all/linux.git/scripts/checkpatch.pl --no-tree --show-types --ignore LINE_CONTINUATIONS --terse -f $(_src) Makefile
+ checkpatch.pl --no-tree --show-types --ignore LINE_CONTINUATIONS --terse -f $(_src) Makefile
+
+checkpatch2:
+ checkpatch.pl --no-tree --show-types --ignore LONG_LINE,LINE_CONTINUATIONS --terse -f $(_src) Makefile
View
4 README.md
@@ -8,7 +8,7 @@ module, platform driver, file operations (read/write, mmap, ioctl, blocking and
Just run
-git clone git://github.com/makelinux/ldt.git && cd ldt && make && ./ldt-test && ./misc_drv_test
+git clone git://github.com/makelinux/ldt.git && cd ldt && make && ./ldt-test && sudo ./misc_drv_test
and explore sources.
@@ -21,7 +21,7 @@ Test script, run it: **[ldt-test](https://github.com/makelinux/ldt/blob/master/l
Generic testing utility for Device I/O: **[dio.c](https://github.com/makelinux/ldt/blob/master/dio.c)**
-Simple misc driver with read, write, fifo tasklet and IRQ:
+Simple misc driver with read, write, fifo, tasklet and IRQ:
**[misc_drv.c](https://github.com/makelinux/ldt/blob/master/misc_drv.c)**
Browse the rest of source: https://github.com/makelinux/ldt/
View
2  ctracer.h
@@ -39,7 +39,7 @@ extern __thread int ret;
#define do_statement(a) do { a } while (0)
/*
- trace variables: integer, hex, string, pointer, float, time value, with and w/o new line
+ trace variables: integer, hex, string, pointer, float, time value
macro with '_' doesn't prints new line
notation:
tr = trace
View
15 ldt_plat_drv.c
@@ -27,12 +27,14 @@ static __devinit int ldt_plat_probe(struct platform_device *pdev)
{
char *data = NULL;
struct resource *r;
+ struct device *dev = &pdev->dev;
+
_entry:
- dev_dbg(&pdev->dev, "%s\n", __func__);
- data = pdev->dev.platform_data;
+ dev_dbg(dev, "probe\n");
+ data = pdev->dev.platform_data;
irq = platform_get_irq(pdev, 0);
r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- pr_debug("%s: pdev->dev.of_node = %p\n", __func__, pdev->dev.of_node);
+ pr_debug("pdev->dev.of_node = %p\n", pdev->dev.of_node);
#ifdef CONFIG_OF_DEVICE
if (pdev->dev.of_node) {
const __be32 *p;
@@ -55,6 +57,13 @@ static __devinit int ldt_plat_probe(struct platform_device *pdev)
r = platform_get_resource(pdev, IORESOURCE_IO, 0);
port = r->start;
port_size = resource_size(r);
+ /*
+ devm_kzalloc
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ r = devm_request_and_ioremap(&pdev->dev, res);
+
+ */
return 0;
}
View
48 misc_drv.c
@@ -18,9 +18,8 @@
* kfifo
* interrupt
* tasklet
- * driven by IRQ 6
*
- * Run test script misc-drv-test to test the driver
+ * Run test script misc_drv_test to test the driver
*
*/
@@ -43,6 +42,11 @@
#undef pr_fmt
#define pr_fmt(fmt) "%s.c:%d %s " fmt, KBUILD_MODNAME, __LINE__, __func__
+/*
+ * It's supposed you computer doesn't use floppy device.
+ * We use IRQ and port of floppy device for demonstration.
+*/
+
static int port = 0x3f4;
module_param(port, int, 0);
MODULE_PARM_DESC(port, "io port number, default 0x3f4 - floppy");
@@ -55,16 +59,25 @@ static int irq = 6;
module_param(irq, int, 0);
MODULE_PARM_DESC(irq, "interrupt request number, default 6 - floppy");
-
-#define FIFO_SIZE 128 /* must be power of two */
+/*
+ * Offsets of registers in port_emulation
+ *
+ * Pay attention that MISC_DRV_TX = MISC_DRV_RX and MISC_DRV_TX_FULL = MISC_DRV_RX_READY.
+ * It emulates loopback device. Transmitted data becomes received.
+ */
#define MISC_DRV_TX 0
#define MISC_DRV_RX 0
#define MISC_DRV_TX_FULL 1
#define MISC_DRV_RX_READY 1
+/**
+ * port_emulation[] - array to emulate port I/0
+ */
+
static char port_emulation[2];
+
/**
* struct misc_drv_data - the driver data
* @in_fifo: input queue for write
@@ -78,6 +91,8 @@ static char port_emulation[2];
* struct misc_drv_data *drvdata = platform_get_drvdata(pdev);
*/
+#define FIFO_SIZE 128 /* must be power of two */
+
struct misc_drv_data {
struct mutex read_lock;
struct mutex write_lock;
@@ -95,15 +110,15 @@ static struct misc_drv_data *drvdata;
static void misc_drv_tasklet_func(unsigned long d)
{
char data_out, data_in;
- struct misc_drv_data *drvdata = (void*) d;
+ struct misc_drv_data *drvdata = (void *)d;
- while ( !ioread8(drvdata->port_ptr + MISC_DRV_TX_FULL)
+ while (!ioread8(drvdata->port_ptr + MISC_DRV_TX_FULL)
&& kfifo_out_spinlocked(&drvdata->out_fifo,
&data_out, sizeof(data_out), &drvdata->fifo_lock)) {
wake_up_interruptible(&drvdata->writeable);
pr_debug("data_out=%d %c\n", data_out, data_out >= 32 ? data_out : ' ');
iowrite8(data_out, drvdata->port_ptr + MISC_DRV_TX);
- /* set full flag and implicitly ready flag */
+ /* set full tx flag and implicitly rx ready flag */
iowrite8(1, drvdata->port_ptr + MISC_DRV_TX_FULL);
/*
In regular drivers hardware invokes interrupts.
@@ -119,14 +134,14 @@ static void misc_drv_tasklet_func(unsigned long d)
kfifo_in_spinlocked(&drvdata->in_fifo, &data_in,
sizeof(data_in), &drvdata->fifo_lock);
wake_up_interruptible(&drvdata->readable);
- /* clear ready flag and implicitly full flag */
+ /* clear rx ready flag and implicitly tx full flag */
iowrite8(0, drvdata->port_ptr + MISC_DRV_RX_READY);
}
}
static irqreturn_t misc_drv_isr(int irq, void *d)
{
- struct misc_drv_data *drvdata = (void*) d;
+ struct misc_drv_data *drvdata = (void *)d;
tasklet_schedule(&drvdata->misc_drv_tasklet);
return IRQ_HANDLED;
@@ -238,25 +253,25 @@ static struct miscdevice misc_drv_dev = {
};
/*
- * main initialization and cleanup section
+ * Initialization and cleanup section
*/
static void misc_drv_cleanup(void)
{
if (misc_drv_dev.this_device)
misc_deregister(&misc_drv_dev);
- if (irq) {
+ if (irq)
free_irq(irq, drvdata);
- }
tasklet_kill(&drvdata->misc_drv_tasklet);
- if (drvdata->port_ptr) ioport_unmap(drvdata->port_ptr);
+ if (drvdata->port_ptr)
+ ioport_unmap(drvdata->port_ptr);
if (drvdata->port_res)
release_region(port, port_size);
kfree(drvdata);
}
-static struct misc_drv_data * misc_drv_data_init(void)
+static struct misc_drv_data *misc_drv_data_init(void)
{
struct misc_drv_data *drvdata;
@@ -273,7 +288,6 @@ static struct misc_drv_data * misc_drv_data_init(void)
return drvdata;
}
-
static __devinit int misc_drv_init(void)
{
int ret = 0;
@@ -293,9 +307,9 @@ static __devinit int misc_drv_init(void)
return -EBUSY;
}
/*
- Real port can mappled with function with ioport_map:
+ Real I/O port should be mapped with function with ioport_map:
drvdata->port_ptr = ioport_map(port, port_size);
- But, because we use emulation mode, we use array instead mapped ports
+ But, because we work in emulation mode, we use array instead mapped ports
*/
drvdata->port_ptr = (void __iomem *) port_emulation;
if (!drvdata->port_ptr) {
View
23 misc_drv_test
@@ -1,25 +1,23 @@
#!/bin/bash
+# The script is intended to be run from user root
+
RED="\\033[0;31m"
NOCOLOR="\\033[0;39m"
GREEN="\\033[0;32m"
GRAY="\\033[0;37m"
+dmesg -c > /dev/null # clear log buffer
# It's supposed you computer doesn't use floppy device.
# misc_drv uses IRQ 6 of floppy device demonstrate IRQ usage.
-sudo dmesg -c > /dev/null
-
-# The script is intend to be run from unprivileged user.
-# It usese sudo to run privileged operations
-sudo rmmod floppy 2> /dev/null
-sudo rmmod misc_drv 2> /dev/null
-sudo insmod ./misc_drv.ko
-sudo chmod go+rw /dev/misc_drv
+rmmod floppy 2> /dev/null
+rmmod misc_drv 2> /dev/null
+insmod ./misc_drv.ko
data=123rw
cat /dev/misc_drv > R.tmp &
sleep 0.1; echo $data > /dev/misc_drv;
-sleep 0.1
+sleep 0.5
kill %1; wait %1 2> /dev/null || true
received=`cat R.tmp`
rm -f R.tmp
@@ -32,7 +30,6 @@ echo expected $data
echo received $received
fi
-
data=123nb
echo -n $data > /dev/misc_drv
sleep 0.5
@@ -45,6 +42,6 @@ echo expected $data
echo received $received
fi
-sudo rmmod misc_drv.ko
-sudo dmesg --notime --show-delta --read-clear 2>/dev/null > kernel.log || \
-sudo dmesg -c > kernel.log && echo kernel.log saved
+rmmod misc_drv.ko
+dmesg --notime --show-delta --read-clear 2>/dev/null > kernel.log || \
+dmesg -c > kernel.log && echo kernel.log saved
Please sign in to comment.
Something went wrong with that request. Please try again.