first sample

This commit is contained in:
ioacademy-jikim
2015-08-19 19:26:23 +09:00
commit e92d98c6d0
62 changed files with 2573 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
+10
View File
@@ -0,0 +1,10 @@
obj-m = blocking_dev.o
KDIR = /usr/src/tegra
PWD = $(shell pwd)
default:
make -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
make -C $(KDIR) SUBDIRS=$(PWD) clean
+75
View File
@@ -0,0 +1,75 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> //misc_register
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
static int temp;
static DECLARE_WAIT_QUEUE_HEAD(my_wait);
int my_open (struct inode *inode, struct file *filp)
{
temp = -999;
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
irqreturn_t my_handler(int irq, void *id)
{
printk("my_handler()\n");
temp=23;
wake_up_interruptible(&my_wait);
return IRQ_HANDLED;
}
ssize_t my_read (struct file *filp, char __user *buff, size_t size,
loff_t *off)
{
printk("my_read()\n");
if( temp == -999 )
interruptible_sleep_on(&my_wait);
put_user( temp, (int*)buff );
return 4;
}
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
.read = my_read,
};
static struct miscdevice misc =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &fops,
};
int my_init(void)
{
int ret;
unsigned long flags=IRQF_SHARED;
misc_register( &misc );
flags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
ret = request_irq(342, my_handler, flags, "MY_INT", my_handler);
return 0;
}
void my_exit(void)
{
free_irq( 342, my_handler);
misc_deregister( &misc );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+99
View File
@@ -0,0 +1,99 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> //misc_register
#include <asm/ioctl.h>
#include <asm/uaccess.h>
int my_open (struct inode *inode, struct file *filp)
{
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
//#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
#define LED_RATIO _IOW( 'm', 1, int )
static int ratio;
long my_ioctl(struct file *filp, unsigned int cmd, unsigned long opt)
{
unsigned int size;
int ret;
printk("my_ioctl()\n");
size = _IOC_SIZE(cmd);
switch( cmd )
{
case LED_RATIO:
if( size != sizeof(int))
return -EINVAL;
ret = copy_from_user( &ratio, (void*)opt, size);
printk("LED_RATIO, ratio=%d\n", ratio );
break;
}
return 0;
}
/*
//#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
#define LED_RATIO _IO( 'm', 1 )
static int ratio;
long my_ioctl(struct file *filp, unsigned int cmd, unsigned long opt)
{
printk("my_ioctl()\n");
if( _IOC_TYPE(cmd) != 'm' )
return -EINVAL;
if( _IOC_NR(cmd) != 1 )
return -EINVAL;
switch( cmd )
{
case LED_RATIO: ratio = 128;
printk("LED_RATIO, ratio=%d\n", ratio );
break;
}
return 0;
}
*/
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
.unlocked_ioctl = my_ioctl,
.compat_ioctl = my_ioctl,
};
static struct miscdevice misc =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &fops,
};
int my_init(void)
{
misc_register( &misc );
return 0;
}
void my_exit(void)
{
misc_deregister( &misc );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+8
View File
@@ -0,0 +1,8 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -fPIE
LOCAL_LDFLAGS := -fPIE -pie
LOCAL_MODULE := myapp
LOCAL_SRC_FILES := blocking_app.c
include $(BUILD_EXECUTABLE)
+52
View File
@@ -0,0 +1,52 @@
#if 1
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#define LED_RATIO _IOW( 'm', 1 , int)
int main()
{
int fd, ret;
int ratio;
char buff[10];
printf("밝기 입력( 0-255 ) : ");
scanf("%d", &ratio );
fd = open( "/dev/mydev", O_RDONLY );
ret = ioctl(fd, LED_RATIO, &ratio);
if( ret < 0 )
{
printf("ret=%d, errno=%d\n", ret, errno );
return 0;
}
close(fd);
return 0;
}
#endif
#if 0
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#define LED_RATIO _IO( 'm', 1 )
int main()
{
int fd, ret;
char buff[10];
fd = open( "/dev/mydev", O_RDONLY );
ret = ioctl(fd, LED_RATIO);
if( ret < 0 )
{
printf("ret=%d, errno=%d\n", ret, errno );
return 0;
}
close(fd);
return 0;
}
#endif
+21
View File
@@ -0,0 +1,21 @@
#if 1
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
int main()
{
int fd;
char *p;
fd = open("/dev/mydev", O_RDWR );
p = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
strcpy( p , "hello world\n");
printf("%s\n", p );
munmap(p, 4096);
close(fd);
return 0;
}
#endif
+10
View File
@@ -0,0 +1,10 @@
#include <fcntl.h>
int main()
{
int fd;
fd = open( "/sys/devices/virtual/misc/mydev/uevent", O_WRONLY );
write( fd, "add\n", 4 );
close(fd);
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
#if 1
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
int main()
{
int fd, ret;
int temp;
char buff[1024];
struct pollfd fds[2];
fd = open( "/dev/mydev", O_RDONLY );
fds[0].fd = 0 ;
fds[0].events = POLLIN;
fds[1].fd = fd ;
fds[1].events = POLLIN;
poll( fds, 2, -1 );
if( fds[0].revents & POLLIN )
{
read( 0, buff, sizeof buff );
buff[ret-1] = 0;
printf("buff=%s\n", buff );
}
if( fds[1].revents & POLLIN )
{
read( fd, &temp, sizeof temp );
printf("temp=%d\n", temp );
}
close(fd);
return 0;
}
#endif
#if 0
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#define LED_RATIO _IO( 'm', 1 )
int main()
{
int fd, ret;
char buff[10];
fd = open( "/dev/mydev", O_RDONLY );
ret = ioctl(fd, LED_RATIO);
if( ret < 0 )
{
printf("ret=%d, errno=%d\n", ret, errno );
return 0;
}
close(fd);
return 0;
}
#endif
+57
View File
@@ -0,0 +1,57 @@
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main()
{
int fd, ret;
char buff[10];
fd = open( "/dev/mydev", O_RDONLY );
ret = read( fd, buff, sizeof buff );
buff[ret] = 0;
printf( "buff[%s], ret=%d\n", buff, ret );
close(fd);
return 0;
}
/*
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main()
{
int fd, ret;
int temp;
fd = open( "/dev/mydev", O_RDONLY );
ret = read( fd, &temp, sizeof temp );
printf( "temp=%d, ret=%d\n", temp, ret );
close(fd);
return 0;
}
*/
/*
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main()
{
int fd, ret;
char buff[] = "LED_ON";
fd = open( "/dev/mydev", O_WRONLY );
ret = write( fd, buff, strlen(buff) );
printf( "ret=%d\n", ret );
close(fd);
return 0;
}
*/
/*
#include <fcntl.h>
int main()
{
int fd;
int flag=128;
fd = open( "/dev/mydev", O_WRONLY );
write( fd, &flag, sizeof flag );
close(fd);
return 0;
}
*/
+29
View File
@@ -0,0 +1,29 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/gfp.h>
// /data/aaa
int my_init(void) {
char *buff;
struct file *filp;
int ret;
filp = filp_open( "/data/bbb", O_WRONLY|O_TRUNC|O_CREAT, 0666 );
if( filp == 0 )
return 0;
buff = (char*)__get_free_pages( GFP_KERNEL, 0 );
strcpy(buff, "hello world");
ret = kernel_write( filp, buff, strlen(buff), 0 );
printk("ret=%d\n", ret );
free_pages( (unsigned long)buff, 0 );
filp_close( filp, 0 );
return 0;
}
void my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+34
View File
@@ -0,0 +1,34 @@
#if 1
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
int main()
{
int fd;
char *p;
printf("main=%p\n", main);
fd = open("aaa", O_RDWR | O_TRUNC | O_CREAT , 0666 );
ftruncate(fd, 1024);
p = mmap(0, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
strcpy( p , "hello world\n");
//printf("%s\n", p );
munmap(p, 1024);
close(fd);
return 0;
}
#endif
#if 0
#include <fcntl.h>
int main()
{
//mmap();
int fd;
fd = open("aaa", O_WRONLY | O_TRUNC | O_CREAT , 0666 );
write( fd, "hello world", 11 );
close(fd);
return 0;
}
#endif
+60
View File
@@ -0,0 +1,60 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> //misc_register
#include <linux/mm_types.h>
#include <linux/gfp.h>
#include <linux/mm.h>
int my_open (struct inode *inode, struct file *filp)
{
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
int my_mmap(struct file *filp, struct vm_area_struct *vma)
{
unsigned long pfn;
unsigned long page;
printk("my_mmap()\n");
page = __get_free_pages( GFP_KERNEL, 0 );
pfn = __pa( page );
remap_pfn_range(vma, vma->vm_start,
pfn>>12, 1<<12, vma->vm_page_prot);
return 0;
}
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
.mmap = my_mmap,
};
static struct miscdevice misc =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &fops,
};
int my_init(void)
{
misc_register( &misc );
return 0;
}
void my_exit(void)
{
misc_deregister( &misc );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+28
View File
@@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
irqreturn_t my_handler(int irq, void *id)
{
printk("my_handler()\n");
return IRQ_HANDLED;
}
int my_init(void)
{
int ret;
unsigned long flags=IRQF_SHARED;
flags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
ret = request_irq(342, my_handler, flags, "MY_INT", my_handler);
return 0;
}
void my_exit(void)
{
free_irq( 342, my_handler);
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+56
View File
@@ -0,0 +1,56 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gfp.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
struct my_struct
{
char name[20];
int id;
};
int my_init(void)
{
char *p;
struct my_struct *task;
struct kmem_cache *my_struct_cachep;
p = (char*)__get_free_pages( GFP_KERNEL, 0 );
p[4095] = 'x';
printk("%c\n", p[4095]);
free_pages((unsigned long)p, 0);
p = (char*)kmalloc( 64, GFP_KERNEL);
p[63] = 'x';
printk("%c\n", p[63]);
kfree(p);
#define L1_CACHE_SHIFT 6
#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT)
#define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
my_struct_cachep =
kmem_cache_create("my_struct", sizeof(struct my_struct),
ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
task = kmem_cache_alloc( my_struct_cachep, GFP_KERNEL);
strcpy( task->name, "kji");
task->id = 1234;
printk("%s, %d\n", task->name, task->id);
kmem_cache_free( my_struct_cachep, task);
kmem_cache_destroy( my_struct_cachep );
p = vmalloc( 8192 * 1024 );
p[8192 * 1024-1] = 'x';
printk("%c\n", p[8192 * 1024-1] );
vfree( p );
return 0;
}
void my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+74
View File
@@ -0,0 +1,74 @@
#if 1
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
int main()
{
char buff[1024];
int ret;
int fd;
struct pollfd fds[2];
// mkfifo myfifo
fd = open("myfifo", O_RDWR );
while(1)
{
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = fd;
fds[1].events = POLLIN;
poll( fds, 2, -1);
if( fds[0].revents & POLLIN )
{
ret = read(0, buff, sizeof buff );
buff[ret-1] = 0;
printf("키보드 [%s]\n", buff );
}
if( fds[1].revents & POLLIN )
{
ret = read(fd, buff, sizeof buff );
buff[ret-1] = 0;
printf("myfifo [%s]\n", buff );
}
}
close(fd);
return 0;
}
#endif
#if 0
#include <fcntl.h>
#include <stdio.h>
int main()
{
char buff[1024];
int ret;
int fd;
// mkfifo myfifo
fd = open("myfifo", O_RDWR );
while(1)
{
ret = read(0, buff, sizeof buff );
buff[ret-1] = 0;
printf("키보드 [%s]\n", buff );
ret = read(fd, buff, sizeof buff );
buff[ret-1] = 0;
printf("myfifo [%s]\n", buff );
}
close(fd);
return 0;
}
#endif
#if 0
#include <fcntl.h>
int main()
{
char buff[1024];
int ret;
ret = read(0, buff, sizeof buff );
write( 1, buff, ret );
return 0;
}
#endif
+87
View File
@@ -0,0 +1,87 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> //misc_register
#include <linux/wait.h>
#include <linux/timer.h>
#include <asm/uaccess.h>
#include <linux/poll.h>
static int temp;
static DECLARE_WAIT_QUEUE_HEAD(my_wait);
static struct timer_list timer;
void my_function(unsigned long data)
{
printk("my_function(%lu)\n", data );
temp = 28;
wake_up_interruptible( &my_wait );
}
int my_open (struct inode *inode, struct file *filp)
{
init_timer( &timer );
timer.expires = jiffies + 3*HZ;
timer.function = my_function;
timer.data = 0;
add_timer( &timer );
temp = -999;
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
unsigned int my_poll(struct file *filp, struct poll_table_struct *table)
{
unsigned int mask=0;
printk("my_poll()\n");
poll_wait(filp, &my_wait, table);
if( temp != -999 )
mask = POLLIN;
return mask;
}
ssize_t my_read (struct file *filp, char __user *buff, size_t size,
loff_t *off)
{
printk("my_read()\n");
put_user( temp, (int*)buff );
return 4;
}
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
.poll = my_poll,
.read = my_read,
};
static struct miscdevice misc =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &fops,
};
int my_init(void)
{
misc_register( &misc );
return 0;
}
void my_exit(void)
{
del_timer( &timer );
misc_deregister( &misc );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+10
View File
@@ -0,0 +1,10 @@
#include <stdio.h>
int main()
{
int a[4];
int *p = a;
printf("sizeof(a) =%lu\n", sizeof(a));
printf("sizeof(int[4])=%lu\n", sizeof(int[4]));
printf("sizeof(p) =%lu\n", sizeof(p));
printf("sizeof(int*) =%lu\n", sizeof(int*));
}