Skip to content

Instantly share code, notes, and snippets.

@kimhoki
Last active August 29, 2015 14:23
Show Gist options
  • Save kimhoki/9b5135c79b86113fce0f to your computer and use it in GitHub Desktop.
Save kimhoki/9b5135c79b86113fce0f to your computer and use it in GitHub Desktop.
3ioctl_module (read , write)

ioctl module , read , write

ioctl3

###mknod /dev/mydev c 90 0


  • Android.mk
  • makefile.c
  • 3ioctl.c
  • 3myapp.c

Android.mk:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := 3myapp 
LOCAL_SRC_FILES := 3myapp.c

include $(BUILD_EXECUTABLE)

makefile.c:

CC	:= /usr/local/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-gcc
obj-m := 3ioctl.o
KDIR  := /Smart4412Linux/Source/kernel/kernel_4412

all:
	make -C $(KDIR) SUBDIRS=$(PWD)

clean:
	rm -rf *.o*.ko
	rm -rf *.mod
	rm -rf *.ko
	rm -rf *.o
	

3ioctl.c:

#if 1
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "ioctl_test"
#define DEVICE_MAJOR 90

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_colse()\n");
	return 0;
}
//#define LED_ON 1
//BC_TRANSACTION
#define LED_ON _IO('c',1)
#define LED_RATIO _IOW('c',2,int)
#define LED_RATIO_R _IOR('c',3,int)

long my_ioctl(struct file *filp,unsigned int cmd, unsigned long opt)
{
	int ratio, size, ret;

	printk("my_ioctl()\n");

	if(_IOC_TYPE(cmd) != 'c')
	{
		printk("type error\n");
	}

	if(_IOC_NR(cmd) < 1 || _IOC_NR(cmd) > 3)
	{
		printk("nr erro\n");
	}

	if(_IOC_DIR(cmd) == _IOC_WRITE)
	{
		size = _IOC_SIZE(cmd);
		ret = copy_from_user((void*)&ratio, (const void*)opt, size); // 반환인자
	}

	if(_IOC_DIR(cmd) == _IOC_READ)
	{
		ratio = 100;
		size = _IOC_SIZE(cmd);
		// opt는 유저 영역 주소
		ret = copy_to_user((void *)opt,(const void *)&ratio,size);
	}



	switch(cmd)
	{
		case LED_ON : printk("LED_ON\n");	break;
		case LED_RATIO : printk("LED_RATIO, ratio = %d\n",ratio);	break;
	//case LED_RATIO_R : printk("LED_RATIO_R, ratio = %d\n",ratio);	break;
	}
	return 0;
}
static struct file_operations fops =
{
	.open		=my_open,
	.release	=my_close,
	.unlocked_ioctl=my_ioctl,
};
static int __init ioctl_init(void)
{
	int result;
	result = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &fops);
	if(result <0 )
	{
		printk("unable to register devices %s\n",DEVICE_NAME);
		return result;
	}
	return 0;
}

static void __exit ioctl_exit(void)
{
	unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
	printk("rmmod %s\n",DEVICE_NAME);
}
MODULE_LICENSE("GPL");
module_init(ioctl_init);
module_exit(ioctl_exit);

#endif

3myapp.c:

#if 1
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

//#define LED_ON 1
#define LED_ON _IO('c',1)
#define LED_RATIO _IOW('c',2,int)
#define LED_RATIO_R _IOR('c',3,int)

int main(int argc, char **argv)
{
	int fd;
	int ratio = 255;
	fd = open("/dev/mydev",O_RDWR);

	ioctl(fd,LED_RATIO,&ratio);
	ioctl(fd,LED_RATIO_R,&ratio);
	printf("ratio: %d\n",ratio);
	close(fd);
	return 0;
}

#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment