1/* MiroSOUND PCM20 radio rds interface driver
2 * (c) 2001 Robert Siemer <Robert.Siemer@gmx.de>
3 * Thanks to Fred Seidel. See miropcm20-rds-core.c for further information.
4 */
5
6/* Revision history:
7 *
8 *   2001-04-18  Robert Siemer <Robert.Siemer@gmx.de>
9 *        separate file for user interface driver
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <asm/uaccess.h>
16#include <linux/devfs_fs_kernel.h>
17#include "miropcm20-rds-core.h"
18
19devfs_handle_t dfsh;
20char * text_buffer;
21static int rds_users = 0;
22
23
24static int rds_f_open(struct inode *in, struct file *fi)
25{
26	if(rds_users)
27		return -EBUSY;
28
29	if ((text_buffer=kmalloc(66, GFP_KERNEL)) == 0) {
30		printk(KERN_NOTICE "aci-rds: Out of memory by open()...\n");
31		return -ENOMEM;
32	}
33
34	rds_users++;
35	MOD_INC_USE_COUNT;
36	return 0;
37}
38
39static int rds_f_release(struct inode *in, struct file *fi)
40{
41	kfree(text_buffer);
42
43	rds_users--;
44	MOD_DEC_USE_COUNT;
45	return 0;
46}
47
48static void print_matrix(char *ch, char out[])
49{
50        int j;
51
52	for (j=7; j>=0; j--) {
53		 out[7-j] = ((*ch >> j) & 0x1) + '0';
54	}
55}
56
57static ssize_t rds_f_read(struct file *file, char *buffer, size_t length, loff_t *offset)
58{
59//	i = sprintf(text_buffer, "length: %d, offset: %d\n", length, *offset);
60
61	char c;
62	char bits[8];
63
64	current->state=TASK_UNINTERRUPTIBLE;
65	schedule_timeout(2*HZ);
66	aci_rds_cmd(RDS_STATUS, &c, 1);
67	print_matrix(&c, bits);
68	if (copy_to_user(buffer, bits, 8))
69		return -EFAULT;
70
71/*	if ((c >> 3) & 1) {
72		aci_rds_cmd(RDS_STATIONNAME, text_buffer+1, 8);
73		text_buffer[0]  = ' ' ;
74		text_buffer[9]  = '\n';
75		return copy_to_user(buffer+8, text_buffer, 10) ? -EFAULT: 18;
76	}
77*/
78/*	if ((c >> 6) & 1) {
79		aci_rds_cmd(RDS_PTYTATP, &c, 1);
80		if ( c & 1)
81			sprintf(text_buffer, " M");
82		else
83			sprintf(text_buffer, " S");
84		if ((c >> 1) & 1)
85			sprintf(text_buffer+2, " TA");
86		else
87			sprintf(text_buffer+2, " --");
88		if ((c >> 7) & 1)
89			sprintf(text_buffer+5, " TP");
90		else
91			sprintf(text_buffer+5, " --");
92		sprintf(text_buffer+8, " %2d\n", (c >> 2) & 0x1f);
93		return copy_to_user(buffer+8, text_buffer, 12) ? -EFAULT: 20;
94	}
95*/
96
97	if ((c >> 4) & 1) {
98		aci_rds_cmd(RDS_TEXT, text_buffer, 65);
99		text_buffer[0]  = ' ' ;
100		text_buffer[65] = '\n';
101		return copy_to_user(buffer+8, text_buffer,66) ? -EFAULT : 66+8;
102	} else {
103		put_user('\n', buffer+8);
104		return 9;
105	}
106}
107
108static struct file_operations rds_f_ops = {
109	read:    rds_f_read,
110	open:    rds_f_open,
111	release: rds_f_release
112};
113
114
115static int __init miropcm20_rds_init(void)
116{
117	if ((dfsh = devfs_register(NULL, "v4l/rds/radiotext",
118				   DEVFS_FL_DEFAULT | DEVFS_FL_AUTO_DEVNUM,
119				   0, 0, S_IRUGO | S_IFCHR, &rds_f_ops, NULL))
120	    == NULL)
121		goto devfs_register;
122
123	printk("miropcm20-rds: userinterface driver loaded.\n");
124#if DEBUG
125	printk("v4l-name: %s\n", devfs_get_name(pcm20_radio.devfs_handle, 0));
126#endif
127
128	return 0;
129
130 devfs_register:
131	return -EINVAL;
132}
133
134static void __exit miropcm20_rds_cleanup(void)
135{
136	devfs_unregister(dfsh);
137}
138
139module_init(miropcm20_rds_init);
140module_exit(miropcm20_rds_cleanup);
141MODULE_LICENSE("GPL");
142