Lines Matching +full:positive +full:- +full:phase
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * This is the code behind /dev/parport* -- it allows a user-space
8 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
20 * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
21 * confused with ioctl(fd, SETPHASER, &stun). ;-)
37 * GETPHASE gets the current IEEE1284 phase
38 * GETFLAGS gets current (user-visible) flags
39 * SETFLAGS sets current (user-visible) flags
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
70 #define PP_VERSION "ppdev: user-space parallel port driver"
110 struct parport *port = pp->pdev->port; in pp_enable_irq()
112 port->ops->enable_irq(port); in pp_enable_irq()
119 struct pp_struct *pp = file->private_data; in pp_read()
125 if (!(pp->flags & PP_CLAIMED)) { in pp_read()
128 return -EINVAL; in pp_read()
137 return -ENOMEM; in pp_read()
138 pport = pp->pdev->port; in pp_read()
139 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR); in pp_read()
141 parport_set_timeout(pp->pdev, in pp_read()
142 (file->f_flags & O_NONBLOCK) ? in pp_read()
144 pp->default_inactivity); in pp_read()
154 if (pp->flags & PP_W91284PIC) in pp_read()
156 if (pp->flags & PP_FASTREAD) in pp_read()
158 if (pport->ieee1284.mode & IEEE1284_ADDR) in pp_read()
159 fn = pport->ops->epp_read_addr; in pp_read()
161 fn = pport->ops->epp_read_data; in pp_read()
170 if (file->f_flags & O_NONBLOCK) { in pp_read()
171 bytes_read = -EAGAIN; in pp_read()
176 bytes_read = -ERESTARTSYS; in pp_read()
183 parport_set_timeout(pp->pdev, pp->default_inactivity); in pp_read()
186 bytes_read = -EFAULT; in pp_read()
197 struct pp_struct *pp = file->private_data; in pp_write()
204 if (!(pp->flags & PP_CLAIMED)) { in pp_write()
207 return -EINVAL; in pp_write()
212 return -ENOMEM; in pp_write()
214 pport = pp->pdev->port; in pp_write()
215 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR); in pp_write()
217 parport_set_timeout(pp->pdev, in pp_write()
218 (file->f_flags & O_NONBLOCK) ? in pp_write()
220 pp->default_inactivity); in pp_write()
223 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE); in pp_write()
226 bytes_written = -EFAULT; in pp_write()
230 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) { in pp_write()
232 if (pport->ieee1284.mode & IEEE1284_ADDR) { in pp_write()
233 wrote = pport->ops->epp_write_addr(pport, in pp_write()
236 wrote = pport->ops->epp_write_data(pport, in pp_write()
240 wrote = parport_write(pp->pdev->port, kbuffer, n); in pp_write()
251 if (file->f_flags & O_NONBLOCK) { in pp_write()
253 bytes_written = -EAGAIN; in pp_write()
263 parport_set_timeout(pp->pdev, pp->default_inactivity); in pp_write()
274 if (pp->irqresponse) { in pp_irq()
275 parport_write_control(pp->pdev->port, pp->irqctl); in pp_irq()
276 pp->irqresponse = 0; in pp_irq()
279 atomic_inc(&pp->irqc); in pp_irq()
280 wake_up_interruptible(&pp->irq_wait); in pp_irq()
293 return -ENOMEM; in register_device()
298 rc = -ENXIO; in register_device()
311 ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0; in register_device()
317 rc = -ENXIO; in register_device()
322 pp->pdev = pdev; in register_device()
323 pp->index = index; in register_device()
324 dev_dbg(&pdev->dev, "registered pardevice\n"); in register_device()
348 return -EINVAL; in pp_set_timeout()
353 return -EINVAL; in pp_set_timeout()
355 pdev->timeout = to_jiffies; in pp_set_timeout()
362 struct pp_struct *pp = file->private_data; in pp_do_ioctl()
378 if (pp->flags & PP_CLAIMED) { in pp_do_ioctl()
379 dev_dbg(&pp->pdev->dev, "you've already got it!\n"); in pp_do_ioctl()
380 return -EINVAL; in pp_do_ioctl()
384 if (!pp->pdev) { in pp_do_ioctl()
391 ret = parport_claim_or_block(pp->pdev); in pp_do_ioctl()
395 pp->flags |= PP_CLAIMED; in pp_do_ioctl()
397 /* For interrupt-reporting to work, we need to be in pp_do_ioctl()
402 info = &pp->pdev->port->ieee1284; in pp_do_ioctl()
403 pp->saved_state.mode = info->mode; in pp_do_ioctl()
404 pp->saved_state.phase = info->phase; in pp_do_ioctl()
405 info->mode = pp->state.mode; in pp_do_ioctl()
406 info->phase = pp->state.phase; in pp_do_ioctl()
407 pp->default_inactivity = parport_set_timeout(pp->pdev, 0); in pp_do_ioctl()
408 parport_set_timeout(pp->pdev, pp->default_inactivity); in pp_do_ioctl()
413 if (pp->pdev) { in pp_do_ioctl()
414 dev_dbg(&pp->pdev->dev, in pp_do_ioctl()
416 if (pp->flags & PP_EXCL) in pp_do_ioctl()
420 return -EINVAL; in pp_do_ioctl()
425 pp->flags |= PP_EXCL; in pp_do_ioctl()
432 return -EFAULT; in pp_do_ioctl()
434 pp->state.mode = mode; in pp_do_ioctl()
435 pp->state.phase = init_phase(mode); in pp_do_ioctl()
437 if (pp->flags & PP_CLAIMED) { in pp_do_ioctl()
438 pp->pdev->port->ieee1284.mode = mode; in pp_do_ioctl()
439 pp->pdev->port->ieee1284.phase = pp->state.phase; in pp_do_ioctl()
448 if (pp->flags & PP_CLAIMED) in pp_do_ioctl()
449 mode = pp->pdev->port->ieee1284.mode; in pp_do_ioctl()
451 mode = pp->state.mode; in pp_do_ioctl()
454 return -EFAULT; in pp_do_ioctl()
459 int phase; in pp_do_ioctl() local
461 if (copy_from_user(&phase, argp, sizeof(phase))) in pp_do_ioctl()
462 return -EFAULT; in pp_do_ioctl()
464 /* FIXME: validate phase */ in pp_do_ioctl()
465 pp->state.phase = phase; in pp_do_ioctl()
467 if (pp->flags & PP_CLAIMED) in pp_do_ioctl()
468 pp->pdev->port->ieee1284.phase = phase; in pp_do_ioctl()
474 int phase; in pp_do_ioctl() local
476 if (pp->flags & PP_CLAIMED) in pp_do_ioctl()
477 phase = pp->pdev->port->ieee1284.phase; in pp_do_ioctl()
479 phase = pp->state.phase; in pp_do_ioctl()
480 if (copy_to_user(argp, &phase, sizeof(phase))) in pp_do_ioctl()
481 return -EFAULT; in pp_do_ioctl()
490 return -ENODEV; in pp_do_ioctl()
492 modes = port->modes; in pp_do_ioctl()
495 return -EFAULT; in pp_do_ioctl()
503 return -EFAULT; in pp_do_ioctl()
504 pp->flags &= ~PP_FLAGMASK; in pp_do_ioctl()
505 pp->flags |= (uflags & PP_FLAGMASK); in pp_do_ioctl()
512 uflags = pp->flags & PP_FLAGMASK; in pp_do_ioctl()
514 return -EFAULT; in pp_do_ioctl()
521 if ((pp->flags & PP_CLAIMED) == 0) { in pp_do_ioctl()
523 return -EINVAL; in pp_do_ioctl()
526 port = pp->pdev->port; in pp_do_ioctl()
531 return -EFAULT; in pp_do_ioctl()
536 return -EFAULT; in pp_do_ioctl()
541 return -EFAULT; in pp_do_ioctl()
544 parport_yield_blocking(pp->pdev); in pp_do_ioctl()
549 info = &pp->pdev->port->ieee1284; in pp_do_ioctl()
550 pp->state.mode = info->mode; in pp_do_ioctl()
551 pp->state.phase = info->phase; in pp_do_ioctl()
552 info->mode = pp->saved_state.mode; in pp_do_ioctl()
553 info->phase = pp->saved_state.phase; in pp_do_ioctl()
554 parport_release(pp->pdev); in pp_do_ioctl()
555 pp->flags &= ~PP_CLAIMED; in pp_do_ioctl()
560 return -EFAULT; in pp_do_ioctl()
566 return -EFAULT; in pp_do_ioctl()
573 return -EFAULT; in pp_do_ioctl()
576 return -EFAULT; in pp_do_ioctl()
582 return -EFAULT; in pp_do_ioctl()
584 port->ops->data_reverse(port); in pp_do_ioctl()
586 port->ops->data_forward(port); in pp_do_ioctl()
591 return -EFAULT; in pp_do_ioctl()
594 case -1: /* handshake failed, peripheral not IEEE 1284 */ in pp_do_ioctl()
595 ret = -EIO; in pp_do_ioctl()
598 ret = -ENXIO; in pp_do_ioctl()
606 return -EFAULT; in pp_do_ioctl()
610 pp->irqctl = reg; in pp_do_ioctl()
611 pp->irqresponse = 1; in pp_do_ioctl()
615 ret = atomic_read(&pp->irqc); in pp_do_ioctl()
617 return -EFAULT; in pp_do_ioctl()
618 atomic_sub(ret, &pp->irqc); in pp_do_ioctl()
623 return -EFAULT; in pp_do_ioctl()
626 return -EINVAL; in pp_do_ioctl()
628 return pp_set_timeout(pp->pdev, time32[0], time32[1]); in pp_do_ioctl()
632 return -EFAULT; in pp_do_ioctl()
635 return -EINVAL; in pp_do_ioctl()
640 return pp_set_timeout(pp->pdev, time64[0], time64[1]); in pp_do_ioctl()
643 jiffies_to_timespec64(pp->pdev->timeout, &ts); in pp_do_ioctl()
648 return -EFAULT; in pp_do_ioctl()
653 jiffies_to_timespec64(pp->pdev->timeout, &ts); in pp_do_ioctl()
661 return -EFAULT; in pp_do_ioctl()
666 dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd); in pp_do_ioctl()
667 return -EINVAL; in pp_do_ioctl()
690 return -ENXIO; in pp_open()
694 return -ENOMEM; in pp_open()
696 pp->state.mode = IEEE1284_MODE_COMPAT; in pp_open()
697 pp->state.phase = init_phase(pp->state.mode); in pp_open()
698 pp->flags = 0; in pp_open()
699 pp->irqresponse = 0; in pp_open()
700 atomic_set(&pp->irqc, 0); in pp_open()
701 init_waitqueue_head(&pp->irq_wait); in pp_open()
707 pp->pdev = NULL; in pp_open()
708 file->private_data = pp; in pp_open()
716 struct pp_struct *pp = file->private_data; in pp_release()
720 if (!(pp->flags & PP_CLAIMED) && pp->pdev && in pp_release()
721 (pp->state.mode != IEEE1284_MODE_COMPAT)) { in pp_release()
725 parport_claim_or_block(pp->pdev); in pp_release()
726 pp->flags |= PP_CLAIMED; in pp_release()
727 info = &pp->pdev->port->ieee1284; in pp_release()
728 pp->saved_state.mode = info->mode; in pp_release()
729 pp->saved_state.phase = info->phase; in pp_release()
730 info->mode = pp->state.mode; in pp_release()
731 info->phase = pp->state.phase; in pp_release()
733 } else if ((pp->flags & PP_CLAIMED) && pp->pdev && in pp_release()
734 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) { in pp_release()
738 parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT); in pp_release()
739 dev_dbg(&pp->pdev->dev, in pp_release()
740 "negotiated back to compatibility mode because user-space forgot\n"); in pp_release()
743 if ((pp->flags & PP_CLAIMED) && pp->pdev) { in pp_release()
746 info = &pp->pdev->port->ieee1284; in pp_release()
747 pp->state.mode = info->mode; in pp_release()
748 pp->state.phase = info->phase; in pp_release()
749 info->mode = pp->saved_state.mode; in pp_release()
750 info->phase = pp->saved_state.phase; in pp_release()
751 parport_release(pp->pdev); in pp_release()
754 "because user-space forgot\n", minor); in pp_release()
758 if (pp->pdev) { in pp_release()
759 parport_unregister_device(pp->pdev); in pp_release()
760 ida_free(&ida_index, pp->index); in pp_release()
761 pp->pdev = NULL; in pp_release()
770 /* No kernel lock held - fine */
773 struct pp_struct *pp = file->private_data; in pp_poll()
776 poll_wait(file, &pp->irq_wait, wait); in pp_poll()
777 if (atomic_read(&pp->irqc)) in pp_poll()
802 if (devices[port->number]) in pp_attach()
805 ret = device_create(&ppdev_class, port->dev, in pp_attach()
806 MKDEV(PP_MAJOR, port->number), NULL, in pp_attach()
807 "parport%d", port->number); in pp_attach()
810 port->number); in pp_attach()
813 devices[port->number] = ret; in pp_attach()
818 if (!devices[port->number]) in pp_detach()
821 device_destroy(&ppdev_class, MKDEV(PP_MAJOR, port->number)); in pp_detach()
822 devices[port->number] = NULL; in pp_detach()
827 struct device_driver *drv = par_dev->dev.driver; in pp_probe()
828 int len = strlen(drv->name); in pp_probe()
830 if (strncmp(par_dev->name, drv->name, len)) in pp_probe()
831 return -ENODEV; in pp_probe()
849 return -EIO; in ppdev_init()
883 MODULE_DESCRIPTION("Support for user-space parallel port device drivers");