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