File Index
Files and directories
Sign in
FILE

serport.c

File preview with syntax highlighting.

linux-6.19.14/drivers/input/serio/serport.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Input device TTY line discipline
  4 *
  5 * Copyright (c) 1999-2002 Vojtech Pavlik
  6 *
  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 */
 10
 11
 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>
 21
 22MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 23MODULE_DESCRIPTION("Input device TTY line discipline");
 24MODULE_LICENSE("GPL");
 25MODULE_ALIAS_LDISC(N_MOUSE);
 26
 27#define SERPORT_BUSY	1
 28#define SERPORT_ACTIVE	2
 29#define SERPORT_DEAD	3
 30
 31struct 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;
 38};
 39
 40/*
 41 * Callback functions from the serio code.
 42 */
 43
 44static int serport_serio_write(struct serio *serio, unsigned char data)
 45{
 46	struct serport *serport = serio->port_data;
 47	return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
 48}
 49
 50static int serport_serio_open(struct serio *serio)
 51{
 52	struct serport *serport = serio->port_data;
 53
 54	guard(spinlock_irqsave)(&serport->lock);
 55	set_bit(SERPORT_ACTIVE, &serport->flags);
 56
 57	return 0;
 58}
 59
 60
 61static void serport_serio_close(struct serio *serio)
 62{
 63	struct serport *serport = serio->port_data;
 64
 65	guard(spinlock_irqsave)(&serport->lock);
 66	clear_bit(SERPORT_ACTIVE, &serport->flags);
 67}
 68
 69/*
 70 * serport_ldisc_open() is the routine that is called upon setting our line
 71 * discipline on a tty. It prepares the serio struct.
 72 */
 73
 74static int serport_ldisc_open(struct tty_struct *tty)
 75{
 76	struct serport *serport;
 77
 78	if (!capable(CAP_SYS_ADMIN))
 79		return -EPERM;
 80
 81	serport = kzalloc(sizeof(*serport), GFP_KERNEL);
 82	if (!serport)
 83		return -ENOMEM;
 84
 85	serport->tty = tty;
 86	spin_lock_init(&serport->lock);
 87	init_waitqueue_head(&serport->wait);
 88
 89	tty->disc_data = serport;
 90	tty->receive_room = 256;
 91	set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
 92
 93	return 0;
 94}
 95
 96/*
 97 * serport_ldisc_close() is the opposite of serport_ldisc_open()
 98 */
 99
100static void serport_ldisc_close(struct tty_struct *tty)
101{
102	struct serport *serport = tty->disc_data;
103
104	kfree(serport);
105}
106
107/*
108 * serport_ldisc_receive() is called by the low level tty driver when characters
109 * are ready for us. We forward the characters and flags, one by one to the
110 * 'interrupt' routine.
111 */
112
113static void serport_ldisc_receive(struct tty_struct *tty, const u8 *cp,
114				  const u8 *fp, size_t count)
115{
116	struct serport *serport = tty->disc_data;
117	unsigned int ch_flags = 0;
118	int i;
119
120	guard(spinlock_irqsave)(&serport->lock);
121
122	if (!test_bit(SERPORT_ACTIVE, &serport->flags))
123		return;
124
125	for (i = 0; i < count; i++) {
126		if (fp) {
127			switch (fp[i]) {
128			case TTY_FRAME:
129				ch_flags = SERIO_FRAME;
130				break;
131
132			case TTY_PARITY:
133				ch_flags = SERIO_PARITY;
134				break;
135
136			default:
137				ch_flags = 0;
138				break;
139			}
140		}
141
142		serio_interrupt(serport->serio, cp[i], ch_flags);
143	}
144}
145
146/*
147 * serport_ldisc_read() just waits indefinitely if everything goes well.
148 * However, when the serio driver closes the serio port, it finishes,
149 * returning 0 characters.
150 */
151
152static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file,
153				  u8 *kbuf, size_t nr, void **cookie,
154				  unsigned long offset)
155{
156	struct serport *serport = tty->disc_data;
157	struct serio *serio;
158
159	if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
160		return -EBUSY;
161
162	serport->serio = serio = kzalloc(sizeof(*serio), GFP_KERNEL);
163	if (!serio)
164		return -ENOMEM;
165
166	strscpy(serio->name, "Serial port", sizeof(serio->name));
167	snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
168	serio->id = serport->id;
169	serio->id.type = SERIO_RS232;
170	serio->write = serport_serio_write;
171	serio->open = serport_serio_open;
172	serio->close = serport_serio_close;
173	serio->port_data = serport;
174	serio->dev.parent = tty->dev;
175
176	serio_register_port(serport->serio);
177	printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
178
179	wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
180	serio_unregister_port(serport->serio);
181	serport->serio = NULL;
182
183	clear_bit(SERPORT_DEAD, &serport->flags);
184	clear_bit(SERPORT_BUSY, &serport->flags);
185
186	return 0;
187}
188
189static void serport_set_type(struct tty_struct *tty, unsigned long type)
190{
191	struct serport *serport = tty->disc_data;
192
193	serport->id.proto = type & 0x000000ff;
194	serport->id.id    = (type & 0x0000ff00) >> 8;
195	serport->id.extra = (type & 0x00ff0000) >> 16;
196}
197
198/*
199 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
200 */
201
202static int serport_ldisc_ioctl(struct tty_struct *tty, unsigned int cmd,
203			       unsigned long arg)
204{
205	if (cmd == SPIOCSTYPE) {
206		unsigned long type;
207
208		if (get_user(type, (unsigned long __user *) arg))
209			return -EFAULT;
210
211		serport_set_type(tty, type);
212		return 0;
213	}
214
215	return -EINVAL;
216}
217
218#ifdef CONFIG_COMPAT
219#define COMPAT_SPIOCSTYPE	_IOW('q', 0x01, compat_ulong_t)
220static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
221				       unsigned int cmd, unsigned long arg)
222{
223	if (cmd == COMPAT_SPIOCSTYPE) {
224		void __user *uarg = compat_ptr(arg);
225		compat_ulong_t compat_type;
226
227		if (get_user(compat_type, (compat_ulong_t __user *)uarg))
228			return -EFAULT;
229
230		serport_set_type(tty, compat_type);
231		return 0;
232	}
233
234	return -EINVAL;
235}
236#endif
237
238static void serport_ldisc_hangup(struct tty_struct *tty)
239{
240	struct serport *serport = tty->disc_data;
241
242	scoped_guard(spinlock_irqsave, &serport->lock)
243		set_bit(SERPORT_DEAD, &serport->flags);
244
245	wake_up_interruptible(&serport->wait);
246}
247
248static void serport_ldisc_write_wakeup(struct tty_struct * tty)
249{
250	struct serport *serport = tty->disc_data;
251
252	guard(spinlock_irqsave)(&serport->lock);
253
254	if (test_bit(SERPORT_ACTIVE, &serport->flags))
255		serio_drv_write_wakeup(serport->serio);
256}
257
258/*
259 * The line discipline structure.
260 */
261
262static struct tty_ldisc_ops serport_ldisc = {
263	.owner =	THIS_MODULE,
264	.num =		N_MOUSE,
265	.name =		"input",
266	.open =		serport_ldisc_open,
267	.close =	serport_ldisc_close,
268	.read =		serport_ldisc_read,
269	.ioctl =	serport_ldisc_ioctl,
270#ifdef CONFIG_COMPAT
271	.compat_ioctl =	serport_ldisc_compat_ioctl,
272#endif
273	.receive_buf =	serport_ldisc_receive,
274	.hangup =	serport_ldisc_hangup,
275	.write_wakeup =	serport_ldisc_write_wakeup
276};
277
278/*
279 * The functions for insering/removing us as a module.
280 */
281
282static int __init serport_init(void)
283{
284	int retval;
285	retval = tty_register_ldisc(&serport_ldisc);
286	if (retval)
287		printk(KERN_ERR "serport.c: Error registering line discipline.\n");
288
289	return  retval;
290}
291
292static void __exit serport_exit(void)
293{
294	tty_unregister_ldisc(&serport_ldisc);
295}
296
297module_init(serport_init);
298module_exit(serport_exit);
~/ Read-only file preview.