Skip to content

Instantly share code, notes, and snippets.

@d1b
Created July 31, 2011 16:29
Show Gist options
  • Save d1b/1116932 to your computer and use it in GitHub Desktop.
Save d1b/1116932 to your computer and use it in GitHub Desktop.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#define DRIVER_AUTHOR "Dave B. <db@d1b.org>"
#define DRIVER_DESC "Rickroll ;)"
#define DEVICE_NAME "chardev"
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
static int device_release(struct inode * inode, struct file * );
static char * get_rickroll_line(void);
static int Major;
static int Device_Open;
static char *msg_Ptr;
static struct file_operations fops =
{
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, & fops);
if (Major < 0)
{
printk(KERN_ALERT "Failed to register the rickroll device with %d\n", Major);
return Major;
}
printk(KERN_INFO "Registered the rickroll device On major %d", Major);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(Major, DEVICE_NAME);
}
static int device_open(struct inode *inode, struct file * file)
{
if (Device_Open)
{
return -EBUSY;
}
Device_Open++;
msg_Ptr = get_rickroll_line();
try_module_get(THIS_MODULE);
return 0;
}
static int device_release(struct inode * inode, struct file * file)
{
Device_Open--;
module_put(THIS_MODULE);
return 0;
}
static ssize_t device_read(struct file * filp, char * buffer, size_t length, loff_t * offset)
{
int bytes_read = 0;
char * newline = "\n\0";
if (* msg_Ptr == 0)
{
return 0;
}
while (length && * msg_Ptr)
{
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
put_user(*newline, buffer++);
bytes_read++;
return bytes_read;
}
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
return -EINVAL;
}
char * get_rickroll_line(void)
{
static int pos = 0;
int value_of_pos;
if (pos > 34)
{
pos = 0;
}
value_of_pos = pos;
pos++;
switch(value_of_pos)
{
case 0 :
return "We're no strangers to love";
case 1 :
return "You know the rules and so do I";
case 2 :
return "A full commitment's what I'm thinking of";
case 3 :
return "You wouldn't get this from any other guy";
case 4 :
return "I just wanna tell you how I'm feeling";
case 5 :
return "Gotta make you understand";
case 6 :
return "Never gonna give you up";
case 7 :
return "Never gonna let you down";
case 8 :
return "Never gonna run around and desert you";
case 9 :
return "Never gonna make you cry";
case 10 :
return "Never gonna say goodbye";
case 11 :
return "Never gonna tell a lie and hurt you";
case 12 :
return "We've know each other for so long";
case 13 :
return "Your heart's been aching";
case 14 :
return "But you're too shy to say it";
case 15 :
return "Inside we both know what's been going on";
case 16 :
return "We know the game and we're gonna play it";
case 17 :
return "And if you ask me how I'm feeling";
case 18 :
return "Don't tell me you're too blind to see";
case 19 :
return "Never gonna give you up";
case 20 :
return "Never gonna let you down";
case 21 :
return "Never gonna run around and desert you";
case 22 :
return "Never gonna make you cry";
case 23 :
return "Never gonna say goodbye";
case 24 :
return "Never gonna tell a lie and hurt you";
case 25 :
return "(Repeat Chorus)";
case 26 :
return "Give you up, give you up";
case 27 :
return "Give you up, give you up";
case 28 :
return "Never gonna give,";
case 29 :
return "Never gonna give, give you up";
case 30 :
return "Never gonna give,";
case 31 :
return "Never gonna give, give you up";
case 32 :
return "(Last four lines repeat)";
case 33 :
return "I just wanna tell you how I'm feeling";
case 34 :
return "Gotta make you understand";
}
return "ERROR";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment