playing morse signals in Linux box

Recently, someone asked me about what TSC (Time Stamp Counter) and it reminds me one of my old day code. morse playing module!. Here’s the code. It unfortunately has old style code and I don’t have enough time to change it.

/* morse_io.c */
#include 
#include 
#include 
#include 
#include 
#include 
#include 

MODULE_LICENSE("GPL");

#define CLK_FREQ	(1193180L)
#define PIO		(0x61)
#define PIT_CMD		(0x43)
#define PIT_DATA	(0x42)
#define SETUP		(0xB6)
#define TONE_ON		(0x03)
#define TONE_OFF	(0xFC)

void sound(int freq) {
	unsigned int value = inb(PIO);
	freq = CLK_FREQ / freq;
	if ((value & TONE_ON) == 0) {
		outb(value | TONE_ON, PIO);
		outb(SETUP, PIT_CMD);
	}
	outb(freq & 0xff, PIT_DATA);
	outb((freq >> 8) & 0xff, PIT_DATA);
}
void nosound(void) {
	unsigned int value = inb(PIO);
	value &= TONE_OFF;
	outb(value, PIO);
}
#define SPACE_MASK	(1 <= 'a' && c  58) continue;
		c = codes[c];
		if (c & SPACE_MASK) {
			word_space(); continue;
		}
		while (c & BIT_MASK) {
			if (c & 1) send_dash();
			else send_dot();
			c >>= 1;
		}
		letter_space();
	}
}
ssize_t m_write(struct file *filp, const char *buffer,
		size_t length, loff_t *offset) {
	char *data = (char *)kmalloc(length, GFP_KERNEL);
	if (data == NULL) return 0;
	copy_from_user(data, buffer, length);
	morse(data);
	kfree(data);
	return length;
}
struct file_operations m_fops = {
	.write = m_write
};
int major_no = 0;
int init_module() {
	major_no = register_chrdev(0, "morse", &m_fops);
	return 0;
}
void cleanup_module() {
	unregister_chrdev(major_no, "morse");
}

http://pagead2.googlesyndication.com/pagead/show_ads.js

Related Makefile is here.


KERNEL_DIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

obj-m += morse_io.o

default:
	$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules

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

After compile it you need to load the module and make a node.

[root@localhost ~]# insmod ./morse_io.ko
[root@localhost ~]# grep morse /proc/devices 
246 morse
[root@localhost ~]# mknod mymorse c 246 0
[root@localhost ~]# echo SOS > mymorse

You now can hear ‘SOS’ in morse signals!!! 🙂

Leave a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.