1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/char/ppdev.c
5 * This is the code behind /dev/parport* -- it allows a user-space
6 * application to use the parport subsystem.
8 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
10 * A /dev/parportx device node represents an arbitrary device
11 * on port 'x'. The following operations are possible:
13 * open do nothing, set up default IEEE 1284 protocol to be COMPAT
14 * close release port and unregister device (if necessary)
16 * EXCL register device exclusively (may fail)
17 * CLAIM (register device first time) parport_claim_or_block
18 * RELEASE parport_release
19 * SETMODE set the IEEE 1284 protocol to use for read/write
20 * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
21 * confused with ioctl(fd, SETPHASER, &stun). ;-)
22 * DATADIR data_forward / data_reverse
25 * WCONTROL write_control
26 * RCONTROL read_control
27 * FCONTROL frob_control
29 * NEGOT parport_negotiate
30 * YIELD parport_yield_blocking
31 * WCTLONIRQ on interrupt, set control lines
32 * CLRIRQ clear (and return) interrupt count
33 * SETTIME sets device timeout (struct timeval)
34 * GETTIME gets device timeout (struct timeval)
35 * GETMODES gets hardware supported modes (unsigned int)
36 * GETMODE gets the current IEEE1284 mode
37 * GETPHASE gets the current IEEE1284 phase
38 * GETFLAGS gets current (user-visible) flags
39 * SETFLAGS sets current (user-visible) flags
40 * read/write read or write in current IEEE 1284 protocol
41 * select wait for interrupt (in readfds)
44 * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
46 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
47 * - On error, copy_from_user and copy_to_user do not return -EFAULT,
48 * They return the positive number of bytes *not* copied due to address
51 * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
52 * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/sched/signal.h>
58 #include <linux/device.h>
59 #include <linux/ioctl.h>
60 #include <linux/parport.h>
61 #include <linux/ctype.h>
62 #include <linux/poll.h>
63 #include <linux/slab.h>
64 #include <linux/major.h>
65 #include <linux/ppdev.h>
66 #include <linux/mutex.h>
67 #include <linux/uaccess.h>
68 #include <linux/compat.h>
70 #define PP_VERSION "ppdev: user-space parallel port driver"
71 #define CHRDEV "ppdev"
74 struct pardevice *pdev;
75 wait_queue_head_t irq_wait;
80 struct ieee1284_info state;
81 struct ieee1284_info saved_state;
82 long default_inactivity;
86 /* should we use PARDEVICE_MAX here? */
87 static struct device *devices[PARPORT_MAX];
89 static DEFINE_IDA(ida_index);
91 /* pp_struct.flags bitfields */
92 #define PP_CLAIMED (1<<0)
93 #define PP_EXCL (1<<1)
96 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
97 #define PP_BUFFER_SIZE 1024
98 #define PARDEVICE_MAX 8
100 static DEFINE_MUTEX(pp_do_mutex);
102 /* define fixed sized ioctl cmd for y2038 migration */
103 #define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
104 #define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
105 #define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
106 #define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
108 static inline void pp_enable_irq(struct pp_struct *pp)
110 struct parport *port = pp->pdev->port;
112 port->ops->enable_irq(port);
115 static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
118 unsigned int minor = iminor(file_inode(file));
119 struct pp_struct *pp = file->private_data;
121 ssize_t bytes_read = 0;
122 struct parport *pport;
125 if (!(pp->flags & PP_CLAIMED)) {
126 /* Don't have the port claimed */
127 pr_debug(CHRDEV "%x: claim the port first\n", minor);
135 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
138 pport = pp->pdev->port;
139 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
141 parport_set_timeout(pp->pdev,
142 (file->f_flags & O_NONBLOCK) ?
143 PARPORT_INACTIVITY_O_NONBLOCK :
144 pp->default_inactivity);
146 while (bytes_read == 0) {
147 ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
149 if (mode == IEEE1284_MODE_EPP) {
150 /* various specials for EPP mode */
152 size_t (*fn)(struct parport *, void *, size_t, int);
154 if (pp->flags & PP_W91284PIC)
155 flags |= PARPORT_W91284PIC;
156 if (pp->flags & PP_FASTREAD)
157 flags |= PARPORT_EPP_FAST;
158 if (pport->ieee1284.mode & IEEE1284_ADDR)
159 fn = pport->ops->epp_read_addr;
161 fn = pport->ops->epp_read_data;
162 bytes_read = (*fn)(pport, kbuffer, need, flags);
164 bytes_read = parport_read(pport, kbuffer, need);
170 if (file->f_flags & O_NONBLOCK) {
171 bytes_read = -EAGAIN;
175 if (signal_pending(current)) {
176 bytes_read = -ERESTARTSYS;
183 parport_set_timeout(pp->pdev, pp->default_inactivity);
185 if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
186 bytes_read = -EFAULT;
193 static ssize_t pp_write(struct file *file, const char __user *buf,
194 size_t count, loff_t *ppos)
196 unsigned int minor = iminor(file_inode(file));
197 struct pp_struct *pp = file->private_data;
199 ssize_t bytes_written = 0;
202 struct parport *pport;
204 if (!(pp->flags & PP_CLAIMED)) {
205 /* Don't have the port claimed */
206 pr_debug(CHRDEV "%x: claim the port first\n", minor);
210 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
214 pport = pp->pdev->port;
215 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
217 parport_set_timeout(pp->pdev,
218 (file->f_flags & O_NONBLOCK) ?
219 PARPORT_INACTIVITY_O_NONBLOCK :
220 pp->default_inactivity);
222 while (bytes_written < count) {
223 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
225 if (copy_from_user(kbuffer, buf + bytes_written, n)) {
226 bytes_written = -EFAULT;
230 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
231 /* do a fast EPP write */
232 if (pport->ieee1284.mode & IEEE1284_ADDR) {
233 wrote = pport->ops->epp_write_addr(pport,
234 kbuffer, n, PARPORT_EPP_FAST);
236 wrote = pport->ops->epp_write_data(pport,
237 kbuffer, n, PARPORT_EPP_FAST);
240 wrote = parport_write(pp->pdev->port, kbuffer, n);
245 bytes_written = wrote;
249 bytes_written += wrote;
251 if (file->f_flags & O_NONBLOCK) {
253 bytes_written = -EAGAIN;
257 if (signal_pending(current))
263 parport_set_timeout(pp->pdev, pp->default_inactivity);
267 return bytes_written;
270 static void pp_irq(void *private)
272 struct pp_struct *pp = private;
274 if (pp->irqresponse) {
275 parport_write_control(pp->pdev->port, pp->irqctl);
279 atomic_inc(&pp->irqc);
280 wake_up_interruptible(&pp->irq_wait);
283 static int register_device(int minor, struct pp_struct *pp)
285 struct parport *port;
286 struct pardevice *pdev = NULL;
288 struct pardev_cb ppdev_cb;
291 name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
295 port = parport_find_number(minor);
297 pr_warn("%s: no associated port!\n", name);
302 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
303 memset(&ppdev_cb, 0, sizeof(ppdev_cb));
304 ppdev_cb.irq_func = pp_irq;
305 ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
306 ppdev_cb.private = pp;
307 pdev = parport_register_dev_model(port, name, &ppdev_cb, index);
308 parport_put_port(port);
311 pr_warn("%s: failed to register device!\n", name);
313 ida_simple_remove(&ida_index, index);
319 dev_dbg(&pdev->dev, "registered pardevice\n");
325 static enum ieee1284_phase init_phase(int mode)
327 switch (mode & ~(IEEE1284_DEVICEID
329 case IEEE1284_MODE_NIBBLE:
330 case IEEE1284_MODE_BYTE:
331 return IEEE1284_PH_REV_IDLE;
333 return IEEE1284_PH_FWD_IDLE;
336 static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
340 if ((tv_sec < 0) || (tv_usec < 0))
343 to_jiffies = usecs_to_jiffies(tv_usec);
344 to_jiffies += tv_sec * HZ;
348 pdev->timeout = to_jiffies;
352 static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
354 unsigned int minor = iminor(file_inode(file));
355 struct pp_struct *pp = file->private_data;
356 struct parport *port;
357 void __user *argp = (void __user *)arg;
358 struct ieee1284_info *info;
364 struct timespec64 ts;
367 /* First handle the cases that don't take arguments. */
371 if (pp->flags & PP_CLAIMED) {
372 dev_dbg(&pp->pdev->dev, "you've already got it!\n");
376 /* Deferred device registration. */
378 int err = register_device(minor, pp);
384 ret = parport_claim_or_block(pp->pdev);
388 pp->flags |= PP_CLAIMED;
390 /* For interrupt-reporting to work, we need to be
391 * informed of each interrupt. */
394 /* We may need to fix up the state machine. */
395 info = &pp->pdev->port->ieee1284;
396 pp->saved_state.mode = info->mode;
397 pp->saved_state.phase = info->phase;
398 info->mode = pp->state.mode;
399 info->phase = pp->state.phase;
400 pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
401 parport_set_timeout(pp->pdev, pp->default_inactivity);
407 dev_dbg(&pp->pdev->dev,
408 "too late for PPEXCL; already registered\n");
409 if (pp->flags & PP_EXCL)
410 /* But it's not really an error. */
412 /* There's no chance of making the driver happy. */
416 /* Just remember to register the device exclusively
417 * when we finally do the registration. */
418 pp->flags |= PP_EXCL;
424 if (copy_from_user(&mode, argp, sizeof(mode)))
426 /* FIXME: validate mode */
427 pp->state.mode = mode;
428 pp->state.phase = init_phase(mode);
430 if (pp->flags & PP_CLAIMED) {
431 pp->pdev->port->ieee1284.mode = mode;
432 pp->pdev->port->ieee1284.phase = pp->state.phase;
441 if (pp->flags & PP_CLAIMED)
442 mode = pp->pdev->port->ieee1284.mode;
444 mode = pp->state.mode;
446 if (copy_to_user(argp, &mode, sizeof(mode)))
454 if (copy_from_user(&phase, argp, sizeof(phase)))
457 /* FIXME: validate phase */
458 pp->state.phase = phase;
460 if (pp->flags & PP_CLAIMED)
461 pp->pdev->port->ieee1284.phase = phase;
469 if (pp->flags & PP_CLAIMED)
470 phase = pp->pdev->port->ieee1284.phase;
472 phase = pp->state.phase;
473 if (copy_to_user(argp, &phase, sizeof(phase)))
481 port = parport_find_number(minor);
486 parport_put_port(port);
487 if (copy_to_user(argp, &modes, sizeof(modes)))
495 if (copy_from_user(&uflags, argp, sizeof(uflags)))
497 pp->flags &= ~PP_FLAGMASK;
498 pp->flags |= (uflags & PP_FLAGMASK);
505 uflags = pp->flags & PP_FLAGMASK;
506 if (copy_to_user(argp, &uflags, sizeof(uflags)))
512 /* Everything else requires the port to be claimed, so check
514 if ((pp->flags & PP_CLAIMED) == 0) {
515 pr_debug(CHRDEV "%x: claim the port first\n", minor);
519 port = pp->pdev->port;
522 reg = parport_read_status(port);
523 if (copy_to_user(argp, ®, sizeof(reg)))
527 reg = parport_read_data(port);
528 if (copy_to_user(argp, ®, sizeof(reg)))
532 reg = parport_read_control(port);
533 if (copy_to_user(argp, ®, sizeof(reg)))
537 parport_yield_blocking(pp->pdev);
541 /* Save the state machine's state. */
542 info = &pp->pdev->port->ieee1284;
543 pp->state.mode = info->mode;
544 pp->state.phase = info->phase;
545 info->mode = pp->saved_state.mode;
546 info->phase = pp->saved_state.phase;
547 parport_release(pp->pdev);
548 pp->flags &= ~PP_CLAIMED;
552 if (copy_from_user(®, argp, sizeof(reg)))
554 parport_write_control(port, reg);
558 if (copy_from_user(®, argp, sizeof(reg)))
560 parport_write_data(port, reg);
564 if (copy_from_user(&mask, argp,
567 if (copy_from_user(®, 1 + (unsigned char __user *) arg,
570 parport_frob_control(port, mask, reg);
574 if (copy_from_user(&mode, argp, sizeof(mode)))
577 port->ops->data_reverse(port);
579 port->ops->data_forward(port);
583 if (copy_from_user(&mode, argp, sizeof(mode)))
585 switch ((ret = parport_negotiate(port, mode))) {
587 case -1: /* handshake failed, peripheral not IEEE 1284 */
590 case 1: /* handshake succeeded, peripheral rejected mode */
598 if (copy_from_user(®, argp, sizeof(reg)))
601 /* Remember what to set the control lines to, for next
602 * time we get an interrupt. */
608 ret = atomic_read(&pp->irqc);
609 if (copy_to_user(argp, &ret, sizeof(ret)))
611 atomic_sub(ret, &pp->irqc);
615 if (copy_from_user(time32, argp, sizeof(time32)))
618 if ((time32[0] < 0) || (time32[1] < 0))
621 return pp_set_timeout(pp->pdev, time32[0], time32[1]);
624 if (copy_from_user(time64, argp, sizeof(time64)))
627 if ((time64[0] < 0) || (time64[1] < 0))
630 if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
633 return pp_set_timeout(pp->pdev, time64[0], time64[1]);
636 jiffies_to_timespec64(pp->pdev->timeout, &ts);
637 time32[0] = ts.tv_sec;
638 time32[1] = ts.tv_nsec / NSEC_PER_USEC;
640 if (copy_to_user(argp, time32, sizeof(time32)))
646 jiffies_to_timespec64(pp->pdev->timeout, &ts);
647 time64[0] = ts.tv_sec;
648 time64[1] = ts.tv_nsec / NSEC_PER_USEC;
650 if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
653 if (copy_to_user(argp, time64, sizeof(time64)))
659 dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
663 /* Keep the compiler happy */
667 static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
671 mutex_lock(&pp_do_mutex);
672 ret = pp_do_ioctl(file, cmd, arg);
673 mutex_unlock(&pp_do_mutex);
677 static int pp_open(struct inode *inode, struct file *file)
679 unsigned int minor = iminor(inode);
680 struct pp_struct *pp;
682 if (minor >= PARPORT_MAX)
685 pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
689 pp->state.mode = IEEE1284_MODE_COMPAT;
690 pp->state.phase = init_phase(pp->state.mode);
693 atomic_set(&pp->irqc, 0);
694 init_waitqueue_head(&pp->irq_wait);
696 /* Defer the actual device registration until the first claim.
697 * That way, we know whether or not the driver wants to have
698 * exclusive access to the port (PPEXCL).
701 file->private_data = pp;
706 static int pp_release(struct inode *inode, struct file *file)
708 unsigned int minor = iminor(inode);
709 struct pp_struct *pp = file->private_data;
713 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
714 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
715 struct ieee1284_info *info;
717 /* parport released, but not in compatibility mode */
718 parport_claim_or_block(pp->pdev);
719 pp->flags |= PP_CLAIMED;
720 info = &pp->pdev->port->ieee1284;
721 pp->saved_state.mode = info->mode;
722 pp->saved_state.phase = info->phase;
723 info->mode = pp->state.mode;
724 info->phase = pp->state.phase;
726 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
727 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
731 parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
732 dev_dbg(&pp->pdev->dev,
733 "negotiated back to compatibility mode because user-space forgot\n");
736 if ((pp->flags & PP_CLAIMED) && pp->pdev) {
737 struct ieee1284_info *info;
739 info = &pp->pdev->port->ieee1284;
740 pp->state.mode = info->mode;
741 pp->state.phase = info->phase;
742 info->mode = pp->saved_state.mode;
743 info->phase = pp->saved_state.phase;
744 parport_release(pp->pdev);
745 if (compat_negot != 1) {
746 pr_debug(CHRDEV "%x: released pardevice "
747 "because user-space forgot\n", minor);
752 parport_unregister_device(pp->pdev);
753 ida_simple_remove(&ida_index, pp->index);
755 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
763 /* No kernel lock held - fine */
764 static __poll_t pp_poll(struct file *file, poll_table *wait)
766 struct pp_struct *pp = file->private_data;
769 poll_wait(file, &pp->irq_wait, wait);
770 if (atomic_read(&pp->irqc))
771 mask |= EPOLLIN | EPOLLRDNORM;
776 static const struct class ppdev_class = {
780 static const struct file_operations pp_fops = {
781 .owner = THIS_MODULE,
786 .unlocked_ioctl = pp_ioctl,
787 .compat_ioctl = compat_ptr_ioctl,
789 .release = pp_release,
792 static void pp_attach(struct parport *port)
796 if (devices[port->number])
799 ret = device_create(&ppdev_class, port->dev,
800 MKDEV(PP_MAJOR, port->number), NULL,
801 "parport%d", port->number);
803 pr_err("Failed to create device parport%d\n",
807 devices[port->number] = ret;
810 static void pp_detach(struct parport *port)
812 if (!devices[port->number])
815 device_destroy(&ppdev_class, MKDEV(PP_MAJOR, port->number));
816 devices[port->number] = NULL;
819 static int pp_probe(struct pardevice *par_dev)
821 struct device_driver *drv = par_dev->dev.driver;
822 int len = strlen(drv->name);
824 if (strncmp(par_dev->name, drv->name, len))
830 static struct parport_driver pp_driver = {
833 .match_port = pp_attach,
838 static int __init ppdev_init(void)
842 if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
843 pr_warn(CHRDEV ": unable to get major %d\n", PP_MAJOR);
846 err = class_register(&ppdev_class);
850 err = parport_register_driver(&pp_driver);
852 pr_warn(CHRDEV ": unable to register with parport\n");
856 pr_info(PP_VERSION "\n");
860 class_unregister(&ppdev_class);
862 unregister_chrdev(PP_MAJOR, CHRDEV);
867 static void __exit ppdev_cleanup(void)
869 /* Clean up all parport stuff */
870 parport_unregister_driver(&pp_driver);
871 class_unregister(&ppdev_class);
872 unregister_chrdev(PP_MAJOR, CHRDEV);
875 module_init(ppdev_init);
876 module_exit(ppdev_cleanup);
878 MODULE_LICENSE("GPL");
879 MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);