treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / serio / serport.c
blob8ac970a423de6e6b0434ef6c645ee88e24eff4f2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Input device TTY line discipline
5 * Copyright (c) 1999-2002 Vojtech Pavlik
7 * This is a module that converts a tty line into a much simpler
8 * 'serial io port' abstraction that the input device drivers use.
9 */
12 #include <linux/uaccess.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/serio.h>
19 #include <linux/tty.h>
20 #include <linux/compat.h>
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION("Input device TTY line discipline");
24 MODULE_LICENSE("GPL");
25 MODULE_ALIAS_LDISC(N_MOUSE);
27 #define SERPORT_BUSY 1
28 #define SERPORT_ACTIVE 2
29 #define SERPORT_DEAD 3
31 struct serport {
32 struct tty_struct *tty;
33 wait_queue_head_t wait;
34 struct serio *serio;
35 struct serio_device_id id;
36 spinlock_t lock;
37 unsigned long flags;
41 * Callback functions from the serio code.
44 static int serport_serio_write(struct serio *serio, unsigned char data)
46 struct serport *serport = serio->port_data;
47 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
50 static int serport_serio_open(struct serio *serio)
52 struct serport *serport = serio->port_data;
53 unsigned long flags;
55 spin_lock_irqsave(&serport->lock, flags);
56 set_bit(SERPORT_ACTIVE, &serport->flags);
57 spin_unlock_irqrestore(&serport->lock, flags);
59 return 0;
63 static void serport_serio_close(struct serio *serio)
65 struct serport *serport = serio->port_data;
66 unsigned long flags;
68 spin_lock_irqsave(&serport->lock, flags);
69 clear_bit(SERPORT_ACTIVE, &serport->flags);
70 spin_unlock_irqrestore(&serport->lock, flags);
74 * serport_ldisc_open() is the routine that is called upon setting our line
75 * discipline on a tty. It prepares the serio struct.
78 static int serport_ldisc_open(struct tty_struct *tty)
80 struct serport *serport;
82 if (!capable(CAP_SYS_ADMIN))
83 return -EPERM;
85 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
86 if (!serport)
87 return -ENOMEM;
89 serport->tty = tty;
90 spin_lock_init(&serport->lock);
91 init_waitqueue_head(&serport->wait);
93 tty->disc_data = serport;
94 tty->receive_room = 256;
95 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
97 return 0;
101 * serport_ldisc_close() is the opposite of serport_ldisc_open()
104 static void serport_ldisc_close(struct tty_struct *tty)
106 struct serport *serport = (struct serport *) tty->disc_data;
108 kfree(serport);
112 * serport_ldisc_receive() is called by the low level tty driver when characters
113 * are ready for us. We forward the characters and flags, one by one to the
114 * 'interrupt' routine.
117 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
119 struct serport *serport = (struct serport*) tty->disc_data;
120 unsigned long flags;
121 unsigned int ch_flags = 0;
122 int i;
124 spin_lock_irqsave(&serport->lock, flags);
126 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
127 goto out;
129 for (i = 0; i < count; i++) {
130 if (fp) {
131 switch (fp[i]) {
132 case TTY_FRAME:
133 ch_flags = SERIO_FRAME;
134 break;
136 case TTY_PARITY:
137 ch_flags = SERIO_PARITY;
138 break;
140 default:
141 ch_flags = 0;
142 break;
146 serio_interrupt(serport->serio, cp[i], ch_flags);
149 out:
150 spin_unlock_irqrestore(&serport->lock, flags);
154 * serport_ldisc_read() just waits indefinitely if everything goes well.
155 * However, when the serio driver closes the serio port, it finishes,
156 * returning 0 characters.
159 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
161 struct serport *serport = (struct serport*) tty->disc_data;
162 struct serio *serio;
164 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
165 return -EBUSY;
167 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
168 if (!serio)
169 return -ENOMEM;
171 strlcpy(serio->name, "Serial port", sizeof(serio->name));
172 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
173 serio->id = serport->id;
174 serio->id.type = SERIO_RS232;
175 serio->write = serport_serio_write;
176 serio->open = serport_serio_open;
177 serio->close = serport_serio_close;
178 serio->port_data = serport;
179 serio->dev.parent = tty->dev;
181 serio_register_port(serport->serio);
182 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
184 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
185 serio_unregister_port(serport->serio);
186 serport->serio = NULL;
188 clear_bit(SERPORT_DEAD, &serport->flags);
189 clear_bit(SERPORT_BUSY, &serport->flags);
191 return 0;
194 static void serport_set_type(struct tty_struct *tty, unsigned long type)
196 struct serport *serport = tty->disc_data;
198 serport->id.proto = type & 0x000000ff;
199 serport->id.id = (type & 0x0000ff00) >> 8;
200 serport->id.extra = (type & 0x00ff0000) >> 16;
204 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
207 static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
208 unsigned int cmd, unsigned long arg)
210 if (cmd == SPIOCSTYPE) {
211 unsigned long type;
213 if (get_user(type, (unsigned long __user *) arg))
214 return -EFAULT;
216 serport_set_type(tty, type);
217 return 0;
220 return -EINVAL;
223 #ifdef CONFIG_COMPAT
224 #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
225 static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
226 struct file *file,
227 unsigned int cmd, unsigned long arg)
229 if (cmd == COMPAT_SPIOCSTYPE) {
230 void __user *uarg = compat_ptr(arg);
231 compat_ulong_t compat_type;
233 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
234 return -EFAULT;
236 serport_set_type(tty, compat_type);
237 return 0;
240 return -EINVAL;
242 #endif
244 static int serport_ldisc_hangup(struct tty_struct *tty)
246 struct serport *serport = (struct serport *) tty->disc_data;
247 unsigned long flags;
249 spin_lock_irqsave(&serport->lock, flags);
250 set_bit(SERPORT_DEAD, &serport->flags);
251 spin_unlock_irqrestore(&serport->lock, flags);
253 wake_up_interruptible(&serport->wait);
254 return 0;
257 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
259 struct serport *serport = (struct serport *) tty->disc_data;
260 unsigned long flags;
262 spin_lock_irqsave(&serport->lock, flags);
263 if (test_bit(SERPORT_ACTIVE, &serport->flags))
264 serio_drv_write_wakeup(serport->serio);
265 spin_unlock_irqrestore(&serport->lock, flags);
269 * The line discipline structure.
272 static struct tty_ldisc_ops serport_ldisc = {
273 .owner = THIS_MODULE,
274 .name = "input",
275 .open = serport_ldisc_open,
276 .close = serport_ldisc_close,
277 .read = serport_ldisc_read,
278 .ioctl = serport_ldisc_ioctl,
279 #ifdef CONFIG_COMPAT
280 .compat_ioctl = serport_ldisc_compat_ioctl,
281 #endif
282 .receive_buf = serport_ldisc_receive,
283 .hangup = serport_ldisc_hangup,
284 .write_wakeup = serport_ldisc_write_wakeup
288 * The functions for insering/removing us as a module.
291 static int __init serport_init(void)
293 int retval;
294 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
295 if (retval)
296 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
298 return retval;
301 static void __exit serport_exit(void)
303 tty_unregister_ldisc(N_MOUSE);
306 module_init(serport_init);
307 module_exit(serport_exit);