summaryrefslogtreecommitdiff
path: root/drivers/char/ir.c
blob: 67fe4628606d75007dd81a31e216e89db8fb2b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2005-2014 Freescale Semiconductor, Inc. All Rights Reserved.
 */
/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/ir.h>

#define IR_MINOR 255

static DEFINE_MUTEX(ir_mutex);

static int ir_open_cnt;	/* #times opened */

static int ir_open(struct inode *inode, struct file *file)
{
	int ret;

	mutex_lock(&ir_mutex);
	if (0 == ir_open_cnt) {
		ret = 0;
		ir_open_cnt++;
	} else {
		ret = -1;
	}
	mutex_unlock(&ir_mutex);
	return ret;
}

static int ir_release(struct inode *inode, struct file *file)
{
	mutex_lock(&ir_mutex);
	ir_open_cnt--;
	mutex_unlock(&ir_mutex);
	return 0;
}


static long ir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct miscdevice *c;
	void *devdata;
	int ret = 0;

	mutex_lock(&ir_mutex);
	c = file->private_data;
	if (!c) {
		mutex_unlock(&ir_mutex);
		return -EINVAL;
	}
	devdata = dev_get_drvdata(c->this_device);
	if (!devdata) {
		mutex_unlock(&ir_mutex);
		return -EINVAL;
	}

	switch (cmd) {
	case IR_GET_CARRIER_FREQS:
	{
		struct ir_carrier_freq ir_carrier;
		int id;

		if (copy_from_user(&ir_carrier,
				arg,
				sizeof(struct ir_carrier_freq))) {
			ret = -EINVAL;
		} else {
			id = ir_carrier.id;
			ret = ir_get_carrier_range(id,
										&ir_carrier.min,
										&ir_carrier.max);
			if (0 == ret) {
				if (copy_to_user(arg, &ir_carrier, sizeof(ir_carrier)))
					ret = -EINVAL;
			}
		}
		break;
	}
	case IR_GET_CARRIER_FREQS_NUM:
	{
		int num = ir_get_num_carrier_freqs();
		if (copy_to_user(arg, &num, 1))
			ret = -EINVAL;
		break;
	}
	case IR_CFG_CARRIER:
	{
		int carrier = (int)(arg);
		ir_config(devdata, carrier);
		break;
	}
	case IR_DATA_TRANSMIT:
	{
		struct ir_data_pattern p;
		int *pattern;

		if (!arg) {
			mutex_unlock(&ir_mutex);
			return -EINVAL;
		}

		if (copy_from_user(&p, arg, sizeof(struct ir_data_pattern))) {
			mutex_unlock(&ir_mutex);
			return -EINVAL;
		}

		pattern = kzalloc(p.len*sizeof(int), GFP_KERNEL);
		if (!pattern) {
			mutex_unlock(&ir_mutex);
			printk(KERN_ERR "memory allocate for IR pattern error.\n");
			return -EINVAL;
		}

		if (copy_from_user(pattern, p.pattern, p.len*sizeof(int))) {
			printk(KERN_ERR "memory allocate for IR pattern error.\n");
			ret = -EINVAL;
		} else {
			ir_transmit(devdata, p.len, pattern, 1);
		}
		kfree(pattern);
		break;
	}
	default:
		break;
	}
	mutex_unlock(&ir_mutex);
	return ret;
}

static const struct file_operations ir_fops = {
	.owner		= THIS_MODULE,
	.open       = ir_open,
	.release    = ir_release,
	.unlocked_ioctl	= ir_ioctl,
};

static struct miscdevice ir_miscdev = {
	.minor = IR_MINOR,
	.name = "ir",
	.fops = &ir_fops,
};

void ir_device_register(const char *name, struct device *parent, void *devdata)
{
	int ret;
	ret = misc_register(&ir_miscdev);
	dev_set_drvdata(ir_miscdev.this_device, devdata);
	return ret;
}
EXPORT_SYMBOL(ir_device_register);

void ir_device_unregister(void)
{
	misc_deregister(&ir_miscdev);
}
EXPORT_SYMBOL(ir_device_unregister);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("IR Low Abstract Layer");
MODULE_LICENSE("GPL");