Skip to content

Instantly share code, notes, and snippets.

@Jack2
Created December 5, 2015 17:58
Show Gist options
  • Save Jack2/92dda00449274807c070 to your computer and use it in GitHub Desktop.
Save Jack2/92dda00449274807c070 to your computer and use it in GitHub Desktop.
/* hddinfo.c */
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/ide.h>
#include <asm/uaccess.h>
#define HDA 0x0300
#define HARDDISK HDA
char kernel_version[] = UTS_RELEASE;
static int hddinfo_open(struct inode *node, struct file *f)
{
return 0;
}
static int hddinfo_release(struct inode *node, struct file *f)
{
return 0;
}
static ssize_t hddinfo_read(struct file *f, char *buf, size_t nbytes, loff_t *ppos)
{
return nbytes;
}
static ssize_t read_serial(char *dst)
{
ide_drive_t *drv;
drv = get_info_ptr(HARDDISK);
if (drv)
copy_to_user(dst, drv->id->serial_no, 20);
else
{
;//PDEBUG("HDDINFO : Cannot get the drive information\n");
return 0;
}
return 20;
}
int hddinfo_ioctl(struct inode *node, struct file *f, unsigned int ioctl_num, unsigned long ioctl_param)
{
switch (ioctl_num)
{
case 0 :
read_serial((char *)(ioctl_param));
break;
}
return 0;
}
struct file_operations Fops = {
NULL,
NULL,
hddinfo_read,
NULL,
NULL,
NULL,
hddinfo_ioctl,
NULL,
hddinfo_open,
NULL,
hddinfo_release
};
int init_module()
{
if (register_chrdev(212, "hddinfo", &Fops) < 0)
{
//PDEBUG("HDDINFO : Unable to register driver.\n");
return -EIO;
}
return 0;
}
void cleanup_module()
{
if (unregister_chrdev(212, "hddinfo") < 0)
;//PDEBUG("HDDINFO : Unable to unregister\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment