Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/ppdev.c |
| 3 | * |
| 4 | * This is the code behind /dev/parport* -- it allows a user-space |
| 5 | * application to use the parport subsystem. |
| 6 | * |
| 7 | * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * A /dev/parportx device node represents an arbitrary device |
| 15 | * on port 'x'. The following operations are possible: |
| 16 | * |
| 17 | * open do nothing, set up default IEEE 1284 protocol to be COMPAT |
| 18 | * close release port and unregister device (if necessary) |
| 19 | * ioctl |
| 20 | * EXCL register device exclusively (may fail) |
| 21 | * CLAIM (register device first time) parport_claim_or_block |
| 22 | * RELEASE parport_release |
| 23 | * SETMODE set the IEEE 1284 protocol to use for read/write |
| 24 | * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be |
| 25 | * confused with ioctl(fd, SETPHASER, &stun). ;-) |
| 26 | * DATADIR data_forward / data_reverse |
| 27 | * WDATA write_data |
| 28 | * RDATA read_data |
| 29 | * WCONTROL write_control |
| 30 | * RCONTROL read_control |
| 31 | * FCONTROL frob_control |
| 32 | * RSTATUS read_status |
| 33 | * NEGOT parport_negotiate |
| 34 | * YIELD parport_yield_blocking |
| 35 | * WCTLONIRQ on interrupt, set control lines |
| 36 | * CLRIRQ clear (and return) interrupt count |
| 37 | * SETTIME sets device timeout (struct timeval) |
| 38 | * GETTIME gets device timeout (struct timeval) |
| 39 | * GETMODES gets hardware supported modes (unsigned int) |
| 40 | * GETMODE gets the current IEEE1284 mode |
| 41 | * GETPHASE gets the current IEEE1284 phase |
| 42 | * GETFLAGS gets current (user-visible) flags |
| 43 | * SETFLAGS sets current (user-visible) flags |
| 44 | * read/write read or write in current IEEE 1284 protocol |
| 45 | * select wait for interrupt (in readfds) |
| 46 | * |
| 47 | * Changes: |
| 48 | * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999. |
| 49 | * |
| 50 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25 |
| 51 | * - On error, copy_from_user and copy_to_user do not return -EFAULT, |
| 52 | * They return the positive number of bytes *not* copied due to address |
| 53 | * space errors. |
| 54 | * |
| 55 | * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001. |
| 56 | * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001 |
| 57 | */ |
| 58 | |
| 59 | #include <linux/module.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/sched.h> |
| 62 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/ioctl.h> |
| 64 | #include <linux/parport.h> |
| 65 | #include <linux/ctype.h> |
| 66 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 67 | #include <linux/slab.h> |
Rene Herman | e6a6784 | 2006-03-25 03:07:13 -0800 | [diff] [blame] | 68 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <linux/ppdev.h> |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 70 | #include <linux/mutex.h> |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 71 | #include <linux/uaccess.h> |
Bamvor Jian Zhang | 17a3596 | 2016-01-08 15:50:49 +0800 | [diff] [blame] | 72 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | #define PP_VERSION "ppdev: user-space parallel port driver" |
| 75 | #define CHRDEV "ppdev" |
| 76 | |
| 77 | struct pp_struct { |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 78 | struct pardevice *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | wait_queue_head_t irq_wait; |
| 80 | atomic_t irqc; |
| 81 | unsigned int flags; |
| 82 | int irqresponse; |
| 83 | unsigned char irqctl; |
| 84 | struct ieee1284_info state; |
| 85 | struct ieee1284_info saved_state; |
| 86 | long default_inactivity; |
| 87 | }; |
| 88 | |
| 89 | /* pp_struct.flags bitfields */ |
| 90 | #define PP_CLAIMED (1<<0) |
| 91 | #define PP_EXCL (1<<1) |
| 92 | |
| 93 | /* Other constants */ |
| 94 | #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */ |
| 95 | #define PP_BUFFER_SIZE 1024 |
| 96 | #define PARDEVICE_MAX 8 |
| 97 | |
| 98 | /* ROUND_UP macro from fs/select.c */ |
| 99 | #define ROUND_UP(x,y) (((x)+(y)-1)/(y)) |
| 100 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 101 | static DEFINE_MUTEX(pp_do_mutex); |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 102 | |
| 103 | /* define fixed sized ioctl cmd for y2038 migration */ |
| 104 | #define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2]) |
| 105 | #define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2]) |
| 106 | #define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2]) |
| 107 | #define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2]) |
| 108 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 109 | static inline void pp_enable_irq(struct pp_struct *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct parport *port = pp->pdev->port; |
Sudip Mukherjee | 27f3b8a | 2016-02-12 18:33:41 +0530 | [diff] [blame] | 112 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 113 | port->ops->enable_irq(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 116 | static ssize_t pp_read(struct file *file, char __user *buf, size_t count, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 117 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 119 | unsigned int minor = iminor(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct pp_struct *pp = file->private_data; |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 121 | char *kbuffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | ssize_t bytes_read = 0; |
| 123 | struct parport *pport; |
| 124 | int mode; |
| 125 | |
| 126 | if (!(pp->flags & PP_CLAIMED)) { |
| 127 | /* Don't have the port claimed */ |
Michael Buesch | 81fc401 | 2009-06-17 16:27:49 -0700 | [diff] [blame] | 128 | pr_debug(CHRDEV "%x: claim the port first\n", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | /* Trivial case. */ |
| 133 | if (count == 0) |
| 134 | return 0; |
| 135 | |
| 136 | kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL); |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 137 | if (!kbuffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | pport = pp->pdev->port; |
| 140 | mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR); |
| 141 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 142 | parport_set_timeout(pp->pdev, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 143 | (file->f_flags & O_NONBLOCK) ? |
| 144 | PARPORT_INACTIVITY_O_NONBLOCK : |
| 145 | pp->default_inactivity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | while (bytes_read == 0) { |
| 148 | ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE); |
| 149 | |
| 150 | if (mode == IEEE1284_MODE_EPP) { |
| 151 | /* various specials for EPP mode */ |
| 152 | int flags = 0; |
| 153 | size_t (*fn)(struct parport *, void *, size_t, int); |
| 154 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 155 | if (pp->flags & PP_W91284PIC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | flags |= PARPORT_W91284PIC; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 157 | if (pp->flags & PP_FASTREAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | flags |= PARPORT_EPP_FAST; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 159 | if (pport->ieee1284.mode & IEEE1284_ADDR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | fn = pport->ops->epp_read_addr; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 161 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | fn = pport->ops->epp_read_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | bytes_read = (*fn)(pport, kbuffer, need, flags); |
| 164 | } else { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 165 | bytes_read = parport_read(pport, kbuffer, need); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (bytes_read != 0) |
| 169 | break; |
| 170 | |
| 171 | if (file->f_flags & O_NONBLOCK) { |
| 172 | bytes_read = -EAGAIN; |
| 173 | break; |
| 174 | } |
| 175 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 176 | if (signal_pending(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | bytes_read = -ERESTARTSYS; |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | cond_resched(); |
| 182 | } |
| 183 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 184 | parport_set_timeout(pp->pdev, pp->default_inactivity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 186 | if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | bytes_read = -EFAULT; |
| 188 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 189 | kfree(kbuffer); |
| 190 | pp_enable_irq(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return bytes_read; |
| 192 | } |
| 193 | |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 194 | static ssize_t pp_write(struct file *file, const char __user *buf, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 195 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 197 | unsigned int minor = iminor(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct pp_struct *pp = file->private_data; |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 199 | char *kbuffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | ssize_t bytes_written = 0; |
| 201 | ssize_t wrote; |
| 202 | int mode; |
| 203 | struct parport *pport; |
| 204 | |
| 205 | if (!(pp->flags & PP_CLAIMED)) { |
| 206 | /* Don't have the port claimed */ |
Michael Buesch | 81fc401 | 2009-06-17 16:27:49 -0700 | [diff] [blame] | 207 | pr_debug(CHRDEV "%x: claim the port first\n", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
| 210 | |
| 211 | kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL); |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 212 | if (!kbuffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return -ENOMEM; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | pport = pp->pdev->port; |
| 216 | mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR); |
| 217 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 218 | parport_set_timeout(pp->pdev, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 219 | (file->f_flags & O_NONBLOCK) ? |
| 220 | PARPORT_INACTIVITY_O_NONBLOCK : |
| 221 | pp->default_inactivity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | while (bytes_written < count) { |
| 224 | ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE); |
| 225 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 226 | if (copy_from_user(kbuffer, buf + bytes_written, n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | bytes_written = -EFAULT; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) { |
| 232 | /* do a fast EPP write */ |
| 233 | if (pport->ieee1284.mode & IEEE1284_ADDR) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 234 | wrote = pport->ops->epp_write_addr(pport, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | kbuffer, n, PARPORT_EPP_FAST); |
| 236 | } else { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 237 | wrote = pport->ops->epp_write_data(pport, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | kbuffer, n, PARPORT_EPP_FAST); |
| 239 | } |
| 240 | } else { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 241 | wrote = parport_write(pp->pdev->port, kbuffer, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (wrote <= 0) { |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 245 | if (!bytes_written) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | bytes_written = wrote; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | |
| 250 | bytes_written += wrote; |
| 251 | |
| 252 | if (file->f_flags & O_NONBLOCK) { |
| 253 | if (!bytes_written) |
| 254 | bytes_written = -EAGAIN; |
| 255 | break; |
| 256 | } |
| 257 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 258 | if (signal_pending(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | cond_resched(); |
| 262 | } |
| 263 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 264 | parport_set_timeout(pp->pdev, pp->default_inactivity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 266 | kfree(kbuffer); |
| 267 | pp_enable_irq(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return bytes_written; |
| 269 | } |
| 270 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 271 | static void pp_irq(void *private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Jeff Garzik | 5712cb3 | 2007-10-19 02:54:26 -0400 | [diff] [blame] | 273 | struct pp_struct *pp = private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if (pp->irqresponse) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 276 | parport_write_control(pp->pdev->port, pp->irqctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | pp->irqresponse = 0; |
| 278 | } |
| 279 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 280 | atomic_inc(&pp->irqc); |
| 281 | wake_up_interruptible(&pp->irq_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 284 | static int register_device(int minor, struct pp_struct *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | struct parport *port; |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 287 | struct pardevice *pdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | char *name; |
Sudip Mukherjee | 8b7d3a9 | 2016-06-01 17:50:08 +0530 | [diff] [blame] | 289 | struct pardev_cb ppdev_cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Julia Lawall | 8757543 | 2010-05-26 14:43:55 -0700 | [diff] [blame] | 291 | name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (name == NULL) |
| 293 | return -ENOMEM; |
| 294 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 295 | port = parport_find_number(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (!port) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 297 | printk(KERN_WARNING "%s: no associated port!\n", name); |
| 298 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return -ENXIO; |
| 300 | } |
| 301 | |
Sudip Mukherjee | 8b7d3a9 | 2016-06-01 17:50:08 +0530 | [diff] [blame] | 302 | memset(&ppdev_cb, 0, sizeof(ppdev_cb)); |
| 303 | ppdev_cb.irq_func = pp_irq; |
| 304 | ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0; |
| 305 | ppdev_cb.private = pp; |
| 306 | pdev = parport_register_dev_model(port, name, &ppdev_cb, minor); |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 307 | parport_put_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | if (!pdev) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 310 | printk(KERN_WARNING "%s: failed to register device!\n", name); |
| 311 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return -ENXIO; |
| 313 | } |
| 314 | |
| 315 | pp->pdev = pdev; |
Sudip Mukherjee | 396ec3de | 2016-02-12 18:33:46 +0530 | [diff] [blame] | 316 | dev_dbg(&pdev->dev, "registered pardevice\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 320 | static enum ieee1284_phase init_phase(int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | switch (mode & ~(IEEE1284_DEVICEID |
| 323 | | IEEE1284_ADDR)) { |
| 324 | case IEEE1284_MODE_NIBBLE: |
| 325 | case IEEE1284_MODE_BYTE: |
| 326 | return IEEE1284_PH_REV_IDLE; |
| 327 | } |
| 328 | return IEEE1284_PH_FWD_IDLE; |
| 329 | } |
| 330 | |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 331 | static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec) |
| 332 | { |
| 333 | long to_jiffies; |
| 334 | |
| 335 | if ((tv_sec < 0) || (tv_usec < 0)) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | to_jiffies = usecs_to_jiffies(tv_usec); |
| 339 | to_jiffies += tv_sec * HZ; |
| 340 | if (to_jiffies <= 0) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | pdev->timeout = to_jiffies; |
| 344 | return 0; |
| 345 | } |
| 346 | |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 347 | static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 349 | unsigned int minor = iminor(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | struct pp_struct *pp = file->private_data; |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 351 | struct parport *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | void __user *argp = (void __user *)arg; |
| 353 | |
| 354 | /* First handle the cases that don't take arguments. */ |
| 355 | switch (cmd) { |
| 356 | case PPCLAIM: |
| 357 | { |
| 358 | struct ieee1284_info *info; |
| 359 | int ret; |
| 360 | |
| 361 | if (pp->flags & PP_CLAIMED) { |
Sudip Mukherjee | 396ec3de | 2016-02-12 18:33:46 +0530 | [diff] [blame] | 362 | dev_dbg(&pp->pdev->dev, "you've already got it!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
| 366 | /* Deferred device registration. */ |
| 367 | if (!pp->pdev) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 368 | int err = register_device(minor, pp); |
Sudip Mukherjee | 27f3b8a | 2016-02-12 18:33:41 +0530 | [diff] [blame] | 369 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 370 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 374 | ret = parport_claim_or_block(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
| 378 | pp->flags |= PP_CLAIMED; |
| 379 | |
| 380 | /* For interrupt-reporting to work, we need to be |
| 381 | * informed of each interrupt. */ |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 382 | pp_enable_irq(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* We may need to fix up the state machine. */ |
| 385 | info = &pp->pdev->port->ieee1284; |
| 386 | pp->saved_state.mode = info->mode; |
| 387 | pp->saved_state.phase = info->phase; |
| 388 | info->mode = pp->state.mode; |
| 389 | info->phase = pp->state.phase; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 390 | pp->default_inactivity = parport_set_timeout(pp->pdev, 0); |
| 391 | parport_set_timeout(pp->pdev, pp->default_inactivity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | case PPEXCL: |
| 396 | if (pp->pdev) { |
Sudip Mukherjee | 396ec3de | 2016-02-12 18:33:46 +0530 | [diff] [blame] | 397 | dev_dbg(&pp->pdev->dev, |
| 398 | "too late for PPEXCL; already registered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (pp->flags & PP_EXCL) |
| 400 | /* But it's not really an error. */ |
| 401 | return 0; |
| 402 | /* There's no chance of making the driver happy. */ |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | /* Just remember to register the device exclusively |
| 407 | * when we finally do the registration. */ |
| 408 | pp->flags |= PP_EXCL; |
| 409 | return 0; |
| 410 | case PPSETMODE: |
| 411 | { |
| 412 | int mode; |
Sudip Mukherjee | 27f3b8a | 2016-02-12 18:33:41 +0530 | [diff] [blame] | 413 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 414 | if (copy_from_user(&mode, argp, sizeof(mode))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return -EFAULT; |
| 416 | /* FIXME: validate mode */ |
| 417 | pp->state.mode = mode; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 418 | pp->state.phase = init_phase(mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | if (pp->flags & PP_CLAIMED) { |
| 421 | pp->pdev->port->ieee1284.mode = mode; |
| 422 | pp->pdev->port->ieee1284.phase = pp->state.phase; |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | case PPGETMODE: |
| 428 | { |
| 429 | int mode; |
| 430 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 431 | if (pp->flags & PP_CLAIMED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | mode = pp->pdev->port->ieee1284.mode; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 433 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | mode = pp->state.mode; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 435 | |
| 436 | if (copy_to_user(argp, &mode, sizeof(mode))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | case PPSETPHASE: |
| 441 | { |
| 442 | int phase; |
Sudip Mukherjee | 27f3b8a | 2016-02-12 18:33:41 +0530 | [diff] [blame] | 443 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 444 | if (copy_from_user(&phase, argp, sizeof(phase))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return -EFAULT; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | /* FIXME: validate phase */ |
| 448 | pp->state.phase = phase; |
| 449 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 450 | if (pp->flags & PP_CLAIMED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | pp->pdev->port->ieee1284.phase = phase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | case PPGETPHASE: |
| 456 | { |
| 457 | int phase; |
| 458 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 459 | if (pp->flags & PP_CLAIMED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | phase = pp->pdev->port->ieee1284.phase; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 461 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | phase = pp->state.phase; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 463 | if (copy_to_user(argp, &phase, sizeof(phase))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | case PPGETMODES: |
| 468 | { |
| 469 | unsigned int modes; |
| 470 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 471 | port = parport_find_number(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (!port) |
| 473 | return -ENODEV; |
| 474 | |
| 475 | modes = port->modes; |
Julia Lawall | d98808a | 2011-05-26 16:25:59 -0700 | [diff] [blame] | 476 | parport_put_port(port); |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 477 | if (copy_to_user(argp, &modes, sizeof(modes))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | case PPSETFLAGS: |
| 482 | { |
| 483 | int uflags; |
| 484 | |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 485 | if (copy_from_user(&uflags, argp, sizeof(uflags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | pp->flags &= ~PP_FLAGMASK; |
| 488 | pp->flags |= (uflags & PP_FLAGMASK); |
| 489 | return 0; |
| 490 | } |
| 491 | case PPGETFLAGS: |
| 492 | { |
| 493 | int uflags; |
| 494 | |
| 495 | uflags = pp->flags & PP_FLAGMASK; |
Sudip Mukherjee | 83e8060 | 2016-02-12 18:33:42 +0530 | [diff] [blame] | 496 | if (copy_to_user(argp, &uflags, sizeof(uflags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | } /* end switch() */ |
| 501 | |
| 502 | /* Everything else requires the port to be claimed, so check |
| 503 | * that now. */ |
| 504 | if ((pp->flags & PP_CLAIMED) == 0) { |
Michael Buesch | 81fc401 | 2009-06-17 16:27:49 -0700 | [diff] [blame] | 505 | pr_debug(CHRDEV "%x: claim the port first\n", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | port = pp->pdev->port; |
| 510 | switch (cmd) { |
| 511 | struct ieee1284_info *info; |
| 512 | unsigned char reg; |
| 513 | unsigned char mask; |
| 514 | int mode; |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 515 | s32 time32[2]; |
| 516 | s64 time64[2]; |
| 517 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | case PPRSTATUS: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 521 | reg = parport_read_status(port); |
| 522 | if (copy_to_user(argp, ®, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | return -EFAULT; |
| 524 | return 0; |
| 525 | case PPRDATA: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 526 | reg = parport_read_data(port); |
| 527 | if (copy_to_user(argp, ®, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return -EFAULT; |
| 529 | return 0; |
| 530 | case PPRCONTROL: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 531 | reg = parport_read_control(port); |
| 532 | if (copy_to_user(argp, ®, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return -EFAULT; |
| 534 | return 0; |
| 535 | case PPYIELD: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 536 | parport_yield_blocking(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | |
| 539 | case PPRELEASE: |
| 540 | /* Save the state machine's state. */ |
| 541 | info = &pp->pdev->port->ieee1284; |
| 542 | pp->state.mode = info->mode; |
| 543 | pp->state.phase = info->phase; |
| 544 | info->mode = pp->saved_state.mode; |
| 545 | info->phase = pp->saved_state.phase; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 546 | parport_release(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | pp->flags &= ~PP_CLAIMED; |
| 548 | return 0; |
| 549 | |
| 550 | case PPWCONTROL: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 551 | if (copy_from_user(®, argp, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 553 | parport_write_control(port, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | return 0; |
| 555 | |
| 556 | case PPWDATA: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 557 | if (copy_from_user(®, argp, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 559 | parport_write_data(port, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | |
| 562 | case PPFCONTROL: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 563 | if (copy_from_user(&mask, argp, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 564 | sizeof(mask))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 566 | if (copy_from_user(®, 1 + (unsigned char __user *) arg, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 567 | sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 569 | parport_frob_control(port, mask, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | return 0; |
| 571 | |
| 572 | case PPDATADIR: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 573 | if (copy_from_user(&mode, argp, sizeof(mode))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return -EFAULT; |
| 575 | if (mode) |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 576 | port->ops->data_reverse(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | else |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 578 | port->ops->data_forward(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return 0; |
| 580 | |
| 581 | case PPNEGOT: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 582 | if (copy_from_user(&mode, argp, sizeof(mode))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 584 | switch ((ret = parport_negotiate(port, mode))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | case 0: break; |
| 586 | case -1: /* handshake failed, peripheral not IEEE 1284 */ |
| 587 | ret = -EIO; |
| 588 | break; |
| 589 | case 1: /* handshake succeeded, peripheral rejected mode */ |
| 590 | ret = -ENXIO; |
| 591 | break; |
| 592 | } |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 593 | pp_enable_irq(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return ret; |
| 595 | |
| 596 | case PPWCTLONIRQ: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 597 | if (copy_from_user(®, argp, sizeof(reg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return -EFAULT; |
| 599 | |
| 600 | /* Remember what to set the control lines to, for next |
| 601 | * time we get an interrupt. */ |
| 602 | pp->irqctl = reg; |
| 603 | pp->irqresponse = 1; |
| 604 | return 0; |
| 605 | |
| 606 | case PPCLRIRQ: |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 607 | ret = atomic_read(&pp->irqc); |
| 608 | if (copy_to_user(argp, &ret, sizeof(ret))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return -EFAULT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 610 | atomic_sub(ret, &pp->irqc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 613 | case PPSETTIME32: |
| 614 | if (copy_from_user(time32, argp, sizeof(time32))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return -EFAULT; |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 616 | |
| 617 | return pp_set_timeout(pp->pdev, time32[0], time32[1]); |
| 618 | |
| 619 | case PPSETTIME64: |
| 620 | if (copy_from_user(time64, argp, sizeof(time64))) |
| 621 | return -EFAULT; |
| 622 | |
| 623 | return pp_set_timeout(pp->pdev, time64[0], time64[1]); |
| 624 | |
| 625 | case PPGETTIME32: |
| 626 | jiffies_to_timespec64(pp->pdev->timeout, &ts); |
| 627 | time32[0] = ts.tv_sec; |
| 628 | time32[1] = ts.tv_nsec / NSEC_PER_USEC; |
| 629 | if ((time32[0] < 0) || (time32[1] < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | return -EINVAL; |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 631 | |
| 632 | if (copy_to_user(argp, time32, sizeof(time32))) |
| 633 | return -EFAULT; |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 637 | case PPGETTIME64: |
| 638 | jiffies_to_timespec64(pp->pdev->timeout, &ts); |
| 639 | time64[0] = ts.tv_sec; |
| 640 | time64[1] = ts.tv_nsec / NSEC_PER_USEC; |
| 641 | if ((time64[0] < 0) || (time64[1] < 0)) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | if (copy_to_user(argp, time64, sizeof(time64))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | return -EFAULT; |
Bamvor Jian Zhang | 3b9ab37 | 2016-01-08 15:50:48 +0800 | [diff] [blame] | 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return 0; |
| 648 | |
| 649 | default: |
Sudip Mukherjee | 396ec3de | 2016-02-12 18:33:46 +0530 | [diff] [blame] | 650 | dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | } |
| 653 | |
| 654 | /* Keep the compiler happy */ |
| 655 | return 0; |
| 656 | } |
| 657 | |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 658 | static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 659 | { |
| 660 | long ret; |
Sudip Mukherjee | 27f3b8a | 2016-02-12 18:33:41 +0530 | [diff] [blame] | 661 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 662 | mutex_lock(&pp_do_mutex); |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 663 | ret = pp_do_ioctl(file, cmd, arg); |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 664 | mutex_unlock(&pp_do_mutex); |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 665 | return ret; |
| 666 | } |
| 667 | |
Bamvor Jian Zhang | 17a3596 | 2016-01-08 15:50:49 +0800 | [diff] [blame] | 668 | #ifdef CONFIG_COMPAT |
| 669 | static long pp_compat_ioctl(struct file *file, unsigned int cmd, |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 670 | unsigned long arg) |
Bamvor Jian Zhang | 17a3596 | 2016-01-08 15:50:49 +0800 | [diff] [blame] | 671 | { |
| 672 | return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 673 | } |
| 674 | #endif |
| 675 | |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 676 | static int pp_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
| 678 | unsigned int minor = iminor(inode); |
| 679 | struct pp_struct *pp; |
| 680 | |
| 681 | if (minor >= PARPORT_MAX) |
| 682 | return -ENXIO; |
| 683 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 684 | pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (!pp) |
| 686 | return -ENOMEM; |
| 687 | |
| 688 | pp->state.mode = IEEE1284_MODE_COMPAT; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 689 | pp->state.phase = init_phase(pp->state.mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | pp->flags = 0; |
| 691 | pp->irqresponse = 0; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 692 | atomic_set(&pp->irqc, 0); |
| 693 | init_waitqueue_head(&pp->irq_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | /* Defer the actual device registration until the first claim. |
| 696 | * That way, we know whether or not the driver wants to have |
| 697 | * exclusive access to the port (PPEXCL). |
| 698 | */ |
| 699 | pp->pdev = NULL; |
| 700 | file->private_data = pp; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 705 | static int pp_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
| 707 | unsigned int minor = iminor(inode); |
| 708 | struct pp_struct *pp = file->private_data; |
| 709 | int compat_negot; |
| 710 | |
| 711 | compat_negot = 0; |
| 712 | if (!(pp->flags & PP_CLAIMED) && pp->pdev && |
| 713 | (pp->state.mode != IEEE1284_MODE_COMPAT)) { |
Sudip Mukherjee | 3c8db58 | 2016-02-12 18:33:44 +0530 | [diff] [blame] | 714 | struct ieee1284_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | /* parport released, but not in compatibility mode */ |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 717 | parport_claim_or_block(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | pp->flags |= PP_CLAIMED; |
| 719 | info = &pp->pdev->port->ieee1284; |
| 720 | pp->saved_state.mode = info->mode; |
| 721 | pp->saved_state.phase = info->phase; |
| 722 | info->mode = pp->state.mode; |
| 723 | info->phase = pp->state.phase; |
| 724 | compat_negot = 1; |
| 725 | } else if ((pp->flags & PP_CLAIMED) && pp->pdev && |
| 726 | (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) { |
| 727 | compat_negot = 2; |
| 728 | } |
| 729 | if (compat_negot) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 730 | parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT); |
Sudip Mukherjee | 396ec3de | 2016-02-12 18:33:46 +0530 | [diff] [blame] | 731 | dev_dbg(&pp->pdev->dev, |
| 732 | "negotiated back to compatibility mode because user-space forgot\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (pp->flags & PP_CLAIMED) { |
| 736 | struct ieee1284_info *info; |
| 737 | |
| 738 | info = &pp->pdev->port->ieee1284; |
| 739 | pp->state.mode = info->mode; |
| 740 | pp->state.phase = info->phase; |
| 741 | info->mode = pp->saved_state.mode; |
| 742 | info->phase = pp->saved_state.phase; |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 743 | parport_release(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (compat_negot != 1) { |
Michael Buesch | 81fc401 | 2009-06-17 16:27:49 -0700 | [diff] [blame] | 745 | pr_debug(CHRDEV "%x: released pardevice " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | "because user-space forgot\n", minor); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (pp->pdev) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 751 | parport_unregister_device(pp->pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | pp->pdev = NULL; |
Michael Buesch | 81fc401 | 2009-06-17 16:27:49 -0700 | [diff] [blame] | 753 | pr_debug(CHRDEV "%x: unregistered pardevice\n", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 756 | kfree(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /* No kernel lock held - fine */ |
Sudip Mukherjee | d85c1a2 | 2016-02-12 18:33:40 +0530 | [diff] [blame] | 762 | static unsigned int pp_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
| 764 | struct pp_struct *pp = file->private_data; |
| 765 | unsigned int mask = 0; |
| 766 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 767 | poll_wait(file, &pp->irq_wait, wait); |
| 768 | if (atomic_read(&pp->irqc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | mask |= POLLIN | POLLRDNORM; |
| 770 | |
| 771 | return mask; |
| 772 | } |
| 773 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 774 | static struct class *ppdev_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 776 | static const struct file_operations pp_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | .owner = THIS_MODULE, |
| 778 | .llseek = no_llseek, |
| 779 | .read = pp_read, |
| 780 | .write = pp_write, |
| 781 | .poll = pp_poll, |
Alan Cox | 6d535d3 | 2008-07-25 01:48:16 -0700 | [diff] [blame] | 782 | .unlocked_ioctl = pp_ioctl, |
Bamvor Jian Zhang | 17a3596 | 2016-01-08 15:50:49 +0800 | [diff] [blame] | 783 | #ifdef CONFIG_COMPAT |
| 784 | .compat_ioctl = pp_compat_ioctl, |
| 785 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | .open = pp_open, |
| 787 | .release = pp_release, |
| 788 | }; |
| 789 | |
| 790 | static void pp_attach(struct parport *port) |
| 791 | { |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 792 | device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number), |
| 793 | NULL, "parport%d", port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static void pp_detach(struct parport *port) |
| 797 | { |
Greg Kroah-Hartman | 04880ed | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 798 | device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Sudip Mukherjee | 8b7d3a9 | 2016-06-01 17:50:08 +0530 | [diff] [blame] | 801 | static int pp_probe(struct pardevice *par_dev) |
| 802 | { |
| 803 | struct device_driver *drv = par_dev->dev.driver; |
| 804 | int len = strlen(drv->name); |
| 805 | |
| 806 | if (strncmp(par_dev->name, drv->name, len)) |
| 807 | return -ENODEV; |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | static struct parport_driver pp_driver = { |
| 813 | .name = CHRDEV, |
Sudip Mukherjee | 8b7d3a9 | 2016-06-01 17:50:08 +0530 | [diff] [blame] | 814 | .probe = pp_probe, |
| 815 | .match_port = pp_attach, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | .detach = pp_detach, |
Sudip Mukherjee | 8b7d3a9 | 2016-06-01 17:50:08 +0530 | [diff] [blame] | 817 | .devmodel = true, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | }; |
| 819 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 820 | static int __init ppdev_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 822 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 824 | if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) { |
| 825 | printk(KERN_WARNING CHRDEV ": unable to get major %d\n", |
Sudip Mukherjee | a7c71c0 | 2016-02-12 18:33:43 +0530 | [diff] [blame] | 826 | PP_MAJOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | return -EIO; |
| 828 | } |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 829 | ppdev_class = class_create(THIS_MODULE, CHRDEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (IS_ERR(ppdev_class)) { |
| 831 | err = PTR_ERR(ppdev_class); |
| 832 | goto out_chrdev; |
| 833 | } |
Alexey Khoroshilov | 9a32bb3 | 2012-09-02 00:31:58 +0400 | [diff] [blame] | 834 | err = parport_register_driver(&pp_driver); |
| 835 | if (err < 0) { |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 836 | printk(KERN_WARNING CHRDEV ": unable to register with parport\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | goto out_class; |
| 838 | } |
| 839 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 840 | printk(KERN_INFO PP_VERSION "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | goto out; |
| 842 | |
| 843 | out_class: |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 844 | class_destroy(ppdev_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | out_chrdev: |
| 846 | unregister_chrdev(PP_MAJOR, CHRDEV); |
| 847 | out: |
| 848 | return err; |
| 849 | } |
| 850 | |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 851 | static void __exit ppdev_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | /* Clean up all parport stuff */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | parport_unregister_driver(&pp_driver); |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 855 | class_destroy(ppdev_class); |
Sudip Mukherjee | 783ea44 | 2016-02-12 18:33:39 +0530 | [diff] [blame] | 856 | unregister_chrdev(PP_MAJOR, CHRDEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | module_init(ppdev_init); |
| 860 | module_exit(ppdev_cleanup); |
| 861 | |
| 862 | MODULE_LICENSE("GPL"); |
| 863 | MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR); |