Skip to content

Instantly share code, notes, and snippets.

@kimhoki
Last active August 29, 2015 14:23
Show Gist options
  • Save kimhoki/931f891433e7da2b84d2 to your computer and use it in GitHub Desktop.
Save kimhoki/931f891433e7da2b84d2 to your computer and use it in GitHub Desktop.
ioctl

ioctl module

ioctl

###mknod /dev/mydev c 90 0


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

jni폴더 생성 안에 /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    := myapp 
LOCAL_SRC_FILES := myapp.c

include $(BUILD_EXECUTABLE)

makefile.c:

CC	:= /usr/local/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-gcc
obj-m := ioctl.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
	

ioctl.c:

#if 1
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.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
#define LED_ON _IO('c',1)

long my_ioctl(struct file *filp,unsigned int cmd, unsigned long opt)
{
	printk("my_ioctl()\n");
	switch(cmd)
	{
		case LED_ON : printk("LED_ON\n");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

myapp.c:

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

//#define LED_ON 1
#define LED_ON _IO('c',1)

int main(int argc, char **argv)
{
	int fd;
	fd = open("/dev/mydev",O_RDWR);
	ioctl(fd,LED_ON);
	close(fd);
	return 0;
}
#endif

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