ppdev.c 20.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 * linux/drivers/char/ppdev.c
 *
 * This is the code behind /dev/parport* -- it allows a user-space
 * application to use the parport subsystem.
 *
 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * A /dev/parportx device node represents an arbitrary device
 * on port 'x'.  The following operations are possible:
 *
 * open		do nothing, set up default IEEE 1284 protocol to be COMPAT
 * close	release port and unregister device (if necessary)
 * ioctl
 *   EXCL	register device exclusively (may fail)
 *   CLAIM	(register device first time) parport_claim_or_block
 *   RELEASE	parport_release
 *   SETMODE	set the IEEE 1284 protocol to use for read/write
 *   SETPHASE	set the IEEE 1284 phase of a particular mode.  Not to be
 *              confused with ioctl(fd, SETPHASER, &stun). ;-)
 *   DATADIR	data_forward / data_reverse
 *   WDATA	write_data
 *   RDATA	read_data
 *   WCONTROL	write_control
 *   RCONTROL	read_control
 *   FCONTROL	frob_control
 *   RSTATUS	read_status
 *   NEGOT	parport_negotiate
 *   YIELD	parport_yield_blocking
 *   WCTLONIRQ	on interrupt, set control lines
 *   CLRIRQ	clear (and return) interrupt count
 *   SETTIME	sets device timeout (struct timeval)
 *   GETTIME	gets device timeout (struct timeval)
 *   GETMODES	gets hardware supported modes (unsigned int)
 *   GETMODE	gets the current IEEE1284 mode
 *   GETPHASE   gets the current IEEE1284 phase
 *   GETFLAGS   gets current (user-visible) flags
 *   SETFLAGS   sets current (user-visible) flags
 * read/write	read or write in current IEEE 1284 protocol
 * select	wait for interrupt (in readfds)
 *
 * Changes:
 * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
 *
 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
 * - On error, copy_from_user and copy_to_user do not return -EFAULT,
 *   They return the positive number of bytes *not* copied due to address
 *   space errors.
 *
 * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
 * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/parport.h>
#include <linux/ctype.h>
#include <linux/poll.h>
67
#include <linux/slab.h>
68
#include <linux/major.h>
L
Linus Torvalds 已提交
69
#include <linux/ppdev.h>
70
#include <linux/mutex.h>
71
#include <linux/uaccess.h>
72
#include <linux/compat.h>
L
Linus Torvalds 已提交
73 74 75 76 77

#define PP_VERSION "ppdev: user-space parallel port driver"
#define CHRDEV "ppdev"

struct pp_struct {
78
	struct pardevice *pdev;
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	wait_queue_head_t irq_wait;
	atomic_t irqc;
	unsigned int flags;
	int irqresponse;
	unsigned char irqctl;
	struct ieee1284_info state;
	struct ieee1284_info saved_state;
	long default_inactivity;
};

/* pp_struct.flags bitfields */
#define PP_CLAIMED    (1<<0)
#define PP_EXCL       (1<<1)

/* Other constants */
#define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
#define PP_BUFFER_SIZE 1024
#define PARDEVICE_MAX 8

/* ROUND_UP macro from fs/select.c */
#define ROUND_UP(x,y) (((x)+(y)-1)/(y))

101
static DEFINE_MUTEX(pp_do_mutex);
B
Bamvor Jian Zhang 已提交
102 103 104 105 106 107 108

/* define fixed sized ioctl cmd for y2038 migration */
#define PPGETTIME32	_IOR(PP_IOCTL, 0x95, s32[2])
#define PPSETTIME32	_IOW(PP_IOCTL, 0x96, s32[2])
#define PPGETTIME64	_IOR(PP_IOCTL, 0x95, s64[2])
#define PPSETTIME64	_IOW(PP_IOCTL, 0x96, s64[2])

109
static inline void pp_enable_irq(struct pp_struct *pp)
L
Linus Torvalds 已提交
110 111
{
	struct parport *port = pp->pdev->port;
S
Sudip Mukherjee 已提交
112

113
	port->ops->enable_irq(port);
L
Linus Torvalds 已提交
114 115
}

116
static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
117
		       loff_t *ppos)
L
Linus Torvalds 已提交
118
{
A
Al Viro 已提交
119
	unsigned int minor = iminor(file_inode(file));
L
Linus Torvalds 已提交
120
	struct pp_struct *pp = file->private_data;
121
	char *kbuffer;
L
Linus Torvalds 已提交
122 123 124 125 126 127
	ssize_t bytes_read = 0;
	struct parport *pport;
	int mode;

	if (!(pp->flags & PP_CLAIMED)) {
		/* Don't have the port claimed */
M
Michael Buesch 已提交
128
		pr_debug(CHRDEV "%x: claim the port first\n", minor);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136
		return -EINVAL;
	}

	/* Trivial case. */
	if (count == 0)
		return 0;

	kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
S
Sudip Mukherjee 已提交
137
	if (!kbuffer)
L
Linus Torvalds 已提交
138 139 140 141
		return -ENOMEM;
	pport = pp->pdev->port;
	mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);

142
	parport_set_timeout(pp->pdev,
143 144 145
			    (file->f_flags & O_NONBLOCK) ?
			    PARPORT_INACTIVITY_O_NONBLOCK :
			    pp->default_inactivity);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154

	while (bytes_read == 0) {
		ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);

		if (mode == IEEE1284_MODE_EPP) {
			/* various specials for EPP mode */
			int flags = 0;
			size_t (*fn)(struct parport *, void *, size_t, int);

S
Sudip Mukherjee 已提交
155
			if (pp->flags & PP_W91284PIC)
L
Linus Torvalds 已提交
156
				flags |= PARPORT_W91284PIC;
S
Sudip Mukherjee 已提交
157
			if (pp->flags & PP_FASTREAD)
L
Linus Torvalds 已提交
158
				flags |= PARPORT_EPP_FAST;
S
Sudip Mukherjee 已提交
159
			if (pport->ieee1284.mode & IEEE1284_ADDR)
L
Linus Torvalds 已提交
160
				fn = pport->ops->epp_read_addr;
S
Sudip Mukherjee 已提交
161
			else
L
Linus Torvalds 已提交
162 163 164
				fn = pport->ops->epp_read_data;
			bytes_read = (*fn)(pport, kbuffer, need, flags);
		} else {
165
			bytes_read = parport_read(pport, kbuffer, need);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175
		}

		if (bytes_read != 0)
			break;

		if (file->f_flags & O_NONBLOCK) {
			bytes_read = -EAGAIN;
			break;
		}

176
		if (signal_pending(current)) {
L
Linus Torvalds 已提交
177 178 179 180 181 182 183
			bytes_read = -ERESTARTSYS;
			break;
		}

		cond_resched();
	}

184
	parport_set_timeout(pp->pdev, pp->default_inactivity);
L
Linus Torvalds 已提交
185

186
	if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
L
Linus Torvalds 已提交
187 188
		bytes_read = -EFAULT;

189 190
	kfree(kbuffer);
	pp_enable_irq(pp);
L
Linus Torvalds 已提交
191 192 193
	return bytes_read;
}

194
static ssize_t pp_write(struct file *file, const char __user *buf,
195
			size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
196
{
A
Al Viro 已提交
197
	unsigned int minor = iminor(file_inode(file));
L
Linus Torvalds 已提交
198
	struct pp_struct *pp = file->private_data;
199
	char *kbuffer;
L
Linus Torvalds 已提交
200 201 202 203 204 205 206
	ssize_t bytes_written = 0;
	ssize_t wrote;
	int mode;
	struct parport *pport;

	if (!(pp->flags & PP_CLAIMED)) {
		/* Don't have the port claimed */
M
Michael Buesch 已提交
207
		pr_debug(CHRDEV "%x: claim the port first\n", minor);
L
Linus Torvalds 已提交
208 209 210 211
		return -EINVAL;
	}

	kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
S
Sudip Mukherjee 已提交
212
	if (!kbuffer)
L
Linus Torvalds 已提交
213
		return -ENOMEM;
S
Sudip Mukherjee 已提交
214

L
Linus Torvalds 已提交
215 216 217
	pport = pp->pdev->port;
	mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);

218
	parport_set_timeout(pp->pdev,
219 220 221
			    (file->f_flags & O_NONBLOCK) ?
			    PARPORT_INACTIVITY_O_NONBLOCK :
			    pp->default_inactivity);
L
Linus Torvalds 已提交
222 223 224 225

	while (bytes_written < count) {
		ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);

226
		if (copy_from_user(kbuffer, buf + bytes_written, n)) {
L
Linus Torvalds 已提交
227 228 229 230 231 232 233
			bytes_written = -EFAULT;
			break;
		}

		if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
			/* do a fast EPP write */
			if (pport->ieee1284.mode & IEEE1284_ADDR) {
234
				wrote = pport->ops->epp_write_addr(pport,
L
Linus Torvalds 已提交
235 236
					kbuffer, n, PARPORT_EPP_FAST);
			} else {
237
				wrote = pport->ops->epp_write_data(pport,
L
Linus Torvalds 已提交
238 239 240
					kbuffer, n, PARPORT_EPP_FAST);
			}
		} else {
241
			wrote = parport_write(pp->pdev->port, kbuffer, n);
L
Linus Torvalds 已提交
242 243 244
		}

		if (wrote <= 0) {
S
Sudip Mukherjee 已提交
245
			if (!bytes_written)
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257
				bytes_written = wrote;
			break;
		}

		bytes_written += wrote;

		if (file->f_flags & O_NONBLOCK) {
			if (!bytes_written)
				bytes_written = -EAGAIN;
			break;
		}

258
		if (signal_pending(current))
L
Linus Torvalds 已提交
259 260 261 262 263
			break;

		cond_resched();
	}

264
	parport_set_timeout(pp->pdev, pp->default_inactivity);
L
Linus Torvalds 已提交
265

266 267
	kfree(kbuffer);
	pp_enable_irq(pp);
L
Linus Torvalds 已提交
268 269 270
	return bytes_written;
}

271
static void pp_irq(void *private)
L
Linus Torvalds 已提交
272
{
273
	struct pp_struct *pp = private;
L
Linus Torvalds 已提交
274 275

	if (pp->irqresponse) {
276
		parport_write_control(pp->pdev->port, pp->irqctl);
L
Linus Torvalds 已提交
277 278 279
		pp->irqresponse = 0;
	}

280 281
	atomic_inc(&pp->irqc);
	wake_up_interruptible(&pp->irq_wait);
L
Linus Torvalds 已提交
282 283
}

284
static int register_device(int minor, struct pp_struct *pp)
L
Linus Torvalds 已提交
285 286
{
	struct parport *port;
287
	struct pardevice *pdev = NULL;
L
Linus Torvalds 已提交
288
	char *name;
289
	struct pardev_cb ppdev_cb;
L
Linus Torvalds 已提交
290

291
	name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
L
Linus Torvalds 已提交
292 293 294
	if (name == NULL)
		return -ENOMEM;

295
	port = parport_find_number(minor);
L
Linus Torvalds 已提交
296
	if (!port) {
297 298
		printk(KERN_WARNING "%s: no associated port!\n", name);
		kfree(name);
L
Linus Torvalds 已提交
299 300 301
		return -ENXIO;
	}

302 303 304 305 306
	memset(&ppdev_cb, 0, sizeof(ppdev_cb));
	ppdev_cb.irq_func = pp_irq;
	ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
	ppdev_cb.private = pp;
	pdev = parport_register_dev_model(port, name, &ppdev_cb, minor);
307
	parport_put_port(port);
L
Linus Torvalds 已提交
308 309

	if (!pdev) {
310 311
		printk(KERN_WARNING "%s: failed to register device!\n", name);
		kfree(name);
L
Linus Torvalds 已提交
312 313 314 315
		return -ENXIO;
	}

	pp->pdev = pdev;
S
Sudip Mukherjee 已提交
316
	dev_dbg(&pdev->dev, "registered pardevice\n");
L
Linus Torvalds 已提交
317 318 319
	return 0;
}

320
static enum ieee1284_phase init_phase(int mode)
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330
{
	switch (mode & ~(IEEE1284_DEVICEID
			 | IEEE1284_ADDR)) {
	case IEEE1284_MODE_NIBBLE:
	case IEEE1284_MODE_BYTE:
		return IEEE1284_PH_REV_IDLE;
	}
	return IEEE1284_PH_FWD_IDLE;
}

B
Bamvor Jian Zhang 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
{
	long to_jiffies;

	if ((tv_sec < 0) || (tv_usec < 0))
		return -EINVAL;

	to_jiffies = usecs_to_jiffies(tv_usec);
	to_jiffies += tv_sec * HZ;
	if (to_jiffies <= 0)
		return -EINVAL;

	pdev->timeout = to_jiffies;
	return 0;
}

347
static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
348
{
A
Al Viro 已提交
349
	unsigned int minor = iminor(file_inode(file));
L
Linus Torvalds 已提交
350
	struct pp_struct *pp = file->private_data;
351
	struct parport *port;
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361
	void __user *argp = (void __user *)arg;

	/* First handle the cases that don't take arguments. */
	switch (cmd) {
	case PPCLAIM:
	    {
		struct ieee1284_info *info;
		int ret;

		if (pp->flags & PP_CLAIMED) {
S
Sudip Mukherjee 已提交
362
			dev_dbg(&pp->pdev->dev, "you've already got it!\n");
L
Linus Torvalds 已提交
363 364 365 366 367
			return -EINVAL;
		}

		/* Deferred device registration. */
		if (!pp->pdev) {
368
			int err = register_device(minor, pp);
S
Sudip Mukherjee 已提交
369

S
Sudip Mukherjee 已提交
370
			if (err)
L
Linus Torvalds 已提交
371 372 373
				return err;
		}

374
		ret = parport_claim_or_block(pp->pdev);
L
Linus Torvalds 已提交
375 376 377 378 379 380 381
		if (ret < 0)
			return ret;

		pp->flags |= PP_CLAIMED;

		/* For interrupt-reporting to work, we need to be
		 * informed of each interrupt. */
382
		pp_enable_irq(pp);
L
Linus Torvalds 已提交
383 384 385 386 387 388 389

		/* We may need to fix up the state machine. */
		info = &pp->pdev->port->ieee1284;
		pp->saved_state.mode = info->mode;
		pp->saved_state.phase = info->phase;
		info->mode = pp->state.mode;
		info->phase = pp->state.phase;
390 391
		pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
		parport_set_timeout(pp->pdev, pp->default_inactivity);
L
Linus Torvalds 已提交
392 393 394 395 396

		return 0;
	    }
	case PPEXCL:
		if (pp->pdev) {
S
Sudip Mukherjee 已提交
397 398
			dev_dbg(&pp->pdev->dev,
				"too late for PPEXCL; already registered\n");
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412
			if (pp->flags & PP_EXCL)
				/* But it's not really an error. */
				return 0;
			/* There's no chance of making the driver happy. */
			return -EINVAL;
		}

		/* Just remember to register the device exclusively
		 * when we finally do the registration. */
		pp->flags |= PP_EXCL;
		return 0;
	case PPSETMODE:
	    {
		int mode;
S
Sudip Mukherjee 已提交
413

414
		if (copy_from_user(&mode, argp, sizeof(mode)))
L
Linus Torvalds 已提交
415 416 417
			return -EFAULT;
		/* FIXME: validate mode */
		pp->state.mode = mode;
418
		pp->state.phase = init_phase(mode);
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428 429 430

		if (pp->flags & PP_CLAIMED) {
			pp->pdev->port->ieee1284.mode = mode;
			pp->pdev->port->ieee1284.phase = pp->state.phase;
		}

		return 0;
	    }
	case PPGETMODE:
	    {
		int mode;

S
Sudip Mukherjee 已提交
431
		if (pp->flags & PP_CLAIMED)
L
Linus Torvalds 已提交
432
			mode = pp->pdev->port->ieee1284.mode;
S
Sudip Mukherjee 已提交
433
		else
L
Linus Torvalds 已提交
434
			mode = pp->state.mode;
S
Sudip Mukherjee 已提交
435 436

		if (copy_to_user(argp, &mode, sizeof(mode)))
L
Linus Torvalds 已提交
437 438 439 440 441 442
			return -EFAULT;
		return 0;
	    }
	case PPSETPHASE:
	    {
		int phase;
S
Sudip Mukherjee 已提交
443

S
Sudip Mukherjee 已提交
444
		if (copy_from_user(&phase, argp, sizeof(phase)))
L
Linus Torvalds 已提交
445
			return -EFAULT;
S
Sudip Mukherjee 已提交
446

L
Linus Torvalds 已提交
447 448 449
		/* FIXME: validate phase */
		pp->state.phase = phase;

S
Sudip Mukherjee 已提交
450
		if (pp->flags & PP_CLAIMED)
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458
			pp->pdev->port->ieee1284.phase = phase;

		return 0;
	    }
	case PPGETPHASE:
	    {
		int phase;

S
Sudip Mukherjee 已提交
459
		if (pp->flags & PP_CLAIMED)
L
Linus Torvalds 已提交
460
			phase = pp->pdev->port->ieee1284.phase;
S
Sudip Mukherjee 已提交
461
		else
L
Linus Torvalds 已提交
462
			phase = pp->state.phase;
S
Sudip Mukherjee 已提交
463
		if (copy_to_user(argp, &phase, sizeof(phase)))
L
Linus Torvalds 已提交
464 465 466 467 468 469 470
			return -EFAULT;
		return 0;
	    }
	case PPGETMODES:
	    {
		unsigned int modes;

471
		port = parport_find_number(minor);
L
Linus Torvalds 已提交
472 473 474 475
		if (!port)
			return -ENODEV;

		modes = port->modes;
476
		parport_put_port(port);
S
Sudip Mukherjee 已提交
477
		if (copy_to_user(argp, &modes, sizeof(modes)))
L
Linus Torvalds 已提交
478 479 480 481 482 483 484
			return -EFAULT;
		return 0;
	    }
	case PPSETFLAGS:
	    {
		int uflags;

S
Sudip Mukherjee 已提交
485
		if (copy_from_user(&uflags, argp, sizeof(uflags)))
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495
			return -EFAULT;
		pp->flags &= ~PP_FLAGMASK;
		pp->flags |= (uflags & PP_FLAGMASK);
		return 0;
	    }
	case PPGETFLAGS:
	    {
		int uflags;

		uflags = pp->flags & PP_FLAGMASK;
S
Sudip Mukherjee 已提交
496
		if (copy_to_user(argp, &uflags, sizeof(uflags)))
L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504
			return -EFAULT;
		return 0;
	    }
	}	/* end switch() */

	/* Everything else requires the port to be claimed, so check
	 * that now. */
	if ((pp->flags & PP_CLAIMED) == 0) {
M
Michael Buesch 已提交
505
		pr_debug(CHRDEV "%x: claim the port first\n", minor);
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514
		return -EINVAL;
	}

	port = pp->pdev->port;
	switch (cmd) {
		struct ieee1284_info *info;
		unsigned char reg;
		unsigned char mask;
		int mode;
B
Bamvor Jian Zhang 已提交
515 516 517
		s32 time32[2];
		s64 time64[2];
		struct timespec64 ts;
L
Linus Torvalds 已提交
518 519 520
		int ret;

	case PPRSTATUS:
521 522
		reg = parport_read_status(port);
		if (copy_to_user(argp, &reg, sizeof(reg)))
L
Linus Torvalds 已提交
523 524 525
			return -EFAULT;
		return 0;
	case PPRDATA:
526 527
		reg = parport_read_data(port);
		if (copy_to_user(argp, &reg, sizeof(reg)))
L
Linus Torvalds 已提交
528 529 530
			return -EFAULT;
		return 0;
	case PPRCONTROL:
531 532
		reg = parport_read_control(port);
		if (copy_to_user(argp, &reg, sizeof(reg)))
L
Linus Torvalds 已提交
533 534 535
			return -EFAULT;
		return 0;
	case PPYIELD:
536
		parport_yield_blocking(pp->pdev);
L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544 545
		return 0;

	case PPRELEASE:
		/* Save the state machine's state. */
		info = &pp->pdev->port->ieee1284;
		pp->state.mode = info->mode;
		pp->state.phase = info->phase;
		info->mode = pp->saved_state.mode;
		info->phase = pp->saved_state.phase;
546
		parport_release(pp->pdev);
L
Linus Torvalds 已提交
547 548 549 550
		pp->flags &= ~PP_CLAIMED;
		return 0;

	case PPWCONTROL:
551
		if (copy_from_user(&reg, argp, sizeof(reg)))
L
Linus Torvalds 已提交
552
			return -EFAULT;
553
		parport_write_control(port, reg);
L
Linus Torvalds 已提交
554 555 556
		return 0;

	case PPWDATA:
557
		if (copy_from_user(&reg, argp, sizeof(reg)))
L
Linus Torvalds 已提交
558
			return -EFAULT;
559
		parport_write_data(port, reg);
L
Linus Torvalds 已提交
560 561 562
		return 0;

	case PPFCONTROL:
563
		if (copy_from_user(&mask, argp,
564
				   sizeof(mask)))
L
Linus Torvalds 已提交
565
			return -EFAULT;
566
		if (copy_from_user(&reg, 1 + (unsigned char __user *) arg,
567
				   sizeof(reg)))
L
Linus Torvalds 已提交
568
			return -EFAULT;
569
		parport_frob_control(port, mask, reg);
L
Linus Torvalds 已提交
570 571 572
		return 0;

	case PPDATADIR:
573
		if (copy_from_user(&mode, argp, sizeof(mode)))
L
Linus Torvalds 已提交
574 575
			return -EFAULT;
		if (mode)
576
			port->ops->data_reverse(port);
L
Linus Torvalds 已提交
577
		else
578
			port->ops->data_forward(port);
L
Linus Torvalds 已提交
579 580 581
		return 0;

	case PPNEGOT:
582
		if (copy_from_user(&mode, argp, sizeof(mode)))
L
Linus Torvalds 已提交
583
			return -EFAULT;
584
		switch ((ret = parport_negotiate(port, mode))) {
L
Linus Torvalds 已提交
585 586 587 588 589 590 591 592
		case 0: break;
		case -1: /* handshake failed, peripheral not IEEE 1284 */
			ret = -EIO;
			break;
		case 1:  /* handshake succeeded, peripheral rejected mode */
			ret = -ENXIO;
			break;
		}
593
		pp_enable_irq(pp);
L
Linus Torvalds 已提交
594 595 596
		return ret;

	case PPWCTLONIRQ:
597
		if (copy_from_user(&reg, argp, sizeof(reg)))
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606
			return -EFAULT;

		/* Remember what to set the control lines to, for next
		 * time we get an interrupt. */
		pp->irqctl = reg;
		pp->irqresponse = 1;
		return 0;

	case PPCLRIRQ:
607 608
		ret = atomic_read(&pp->irqc);
		if (copy_to_user(argp, &ret, sizeof(ret)))
L
Linus Torvalds 已提交
609
			return -EFAULT;
610
		atomic_sub(ret, &pp->irqc);
L
Linus Torvalds 已提交
611 612
		return 0;

B
Bamvor Jian Zhang 已提交
613 614
	case PPSETTIME32:
		if (copy_from_user(time32, argp, sizeof(time32)))
L
Linus Torvalds 已提交
615
			return -EFAULT;
B
Bamvor Jian Zhang 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629

		return pp_set_timeout(pp->pdev, time32[0], time32[1]);

	case PPSETTIME64:
		if (copy_from_user(time64, argp, sizeof(time64)))
			return -EFAULT;

		return pp_set_timeout(pp->pdev, time64[0], time64[1]);

	case PPGETTIME32:
		jiffies_to_timespec64(pp->pdev->timeout, &ts);
		time32[0] = ts.tv_sec;
		time32[1] = ts.tv_nsec / NSEC_PER_USEC;
		if ((time32[0] < 0) || (time32[1] < 0))
L
Linus Torvalds 已提交
630
			return -EINVAL;
B
Bamvor Jian Zhang 已提交
631 632 633 634

		if (copy_to_user(argp, time32, sizeof(time32)))
			return -EFAULT;

L
Linus Torvalds 已提交
635 636
		return 0;

B
Bamvor Jian Zhang 已提交
637 638 639 640 641 642 643 644
	case PPGETTIME64:
		jiffies_to_timespec64(pp->pdev->timeout, &ts);
		time64[0] = ts.tv_sec;
		time64[1] = ts.tv_nsec / NSEC_PER_USEC;
		if ((time64[0] < 0) || (time64[1] < 0))
			return -EINVAL;

		if (copy_to_user(argp, time64, sizeof(time64)))
L
Linus Torvalds 已提交
645
			return -EFAULT;
B
Bamvor Jian Zhang 已提交
646

L
Linus Torvalds 已提交
647 648 649
		return 0;

	default:
S
Sudip Mukherjee 已提交
650
		dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
L
Linus Torvalds 已提交
651 652 653 654 655 656 657
		return -EINVAL;
	}

	/* Keep the compiler happy */
	return 0;
}

658 659 660
static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	long ret;
S
Sudip Mukherjee 已提交
661

662
	mutex_lock(&pp_do_mutex);
663
	ret = pp_do_ioctl(file, cmd, arg);
664
	mutex_unlock(&pp_do_mutex);
665 666 667
	return ret;
}

668 669
#ifdef CONFIG_COMPAT
static long pp_compat_ioctl(struct file *file, unsigned int cmd,
670
			    unsigned long arg)
671 672 673 674 675
{
	return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
#endif

676
static int pp_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
677 678 679 680 681 682 683
{
	unsigned int minor = iminor(inode);
	struct pp_struct *pp;

	if (minor >= PARPORT_MAX)
		return -ENXIO;

684
	pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
L
Linus Torvalds 已提交
685 686 687 688
	if (!pp)
		return -ENOMEM;

	pp->state.mode = IEEE1284_MODE_COMPAT;
689
	pp->state.phase = init_phase(pp->state.mode);
L
Linus Torvalds 已提交
690 691
	pp->flags = 0;
	pp->irqresponse = 0;
692 693
	atomic_set(&pp->irqc, 0);
	init_waitqueue_head(&pp->irq_wait);
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702 703 704

	/* Defer the actual device registration until the first claim.
	 * That way, we know whether or not the driver wants to have
	 * exclusive access to the port (PPEXCL).
	 */
	pp->pdev = NULL;
	file->private_data = pp;

	return 0;
}

705
static int pp_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713
{
	unsigned int minor = iminor(inode);
	struct pp_struct *pp = file->private_data;
	int compat_negot;

	compat_negot = 0;
	if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
	    (pp->state.mode != IEEE1284_MODE_COMPAT)) {
S
Sudip Mukherjee 已提交
714
		struct ieee1284_info *info;
L
Linus Torvalds 已提交
715 716

		/* parport released, but not in compatibility mode */
717
		parport_claim_or_block(pp->pdev);
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725 726 727 728 729
		pp->flags |= PP_CLAIMED;
		info = &pp->pdev->port->ieee1284;
		pp->saved_state.mode = info->mode;
		pp->saved_state.phase = info->phase;
		info->mode = pp->state.mode;
		info->phase = pp->state.phase;
		compat_negot = 1;
	} else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
	    (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
		compat_negot = 2;
	}
	if (compat_negot) {
730
		parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
S
Sudip Mukherjee 已提交
731 732
		dev_dbg(&pp->pdev->dev,
			"negotiated back to compatibility mode because user-space forgot\n");
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742
	}

	if (pp->flags & PP_CLAIMED) {
		struct ieee1284_info *info;

		info = &pp->pdev->port->ieee1284;
		pp->state.mode = info->mode;
		pp->state.phase = info->phase;
		info->mode = pp->saved_state.mode;
		info->phase = pp->saved_state.phase;
743
		parport_release(pp->pdev);
L
Linus Torvalds 已提交
744
		if (compat_negot != 1) {
M
Michael Buesch 已提交
745
			pr_debug(CHRDEV "%x: released pardevice "
L
Linus Torvalds 已提交
746 747 748 749 750
				"because user-space forgot\n", minor);
		}
	}

	if (pp->pdev) {
751
		parport_unregister_device(pp->pdev);
L
Linus Torvalds 已提交
752
		pp->pdev = NULL;
M
Michael Buesch 已提交
753
		pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
L
Linus Torvalds 已提交
754 755
	}

756
	kfree(pp);
L
Linus Torvalds 已提交
757 758 759 760 761

	return 0;
}

/* No kernel lock held - fine */
762
static unsigned int pp_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
763 764 765 766
{
	struct pp_struct *pp = file->private_data;
	unsigned int mask = 0;

767 768
	poll_wait(file, &pp->irq_wait, wait);
	if (atomic_read(&pp->irqc))
L
Linus Torvalds 已提交
769 770 771 772 773
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

774
static struct class *ppdev_class;
L
Linus Torvalds 已提交
775

776
static const struct file_operations pp_fops = {
L
Linus Torvalds 已提交
777 778 779 780 781
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= pp_read,
	.write		= pp_write,
	.poll		= pp_poll,
782
	.unlocked_ioctl	= pp_ioctl,
783 784 785
#ifdef CONFIG_COMPAT
	.compat_ioctl   = pp_compat_ioctl,
#endif
L
Linus Torvalds 已提交
786 787 788 789 790 791
	.open		= pp_open,
	.release	= pp_release,
};

static void pp_attach(struct parport *port)
{
792 793
	device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
		      NULL, "parport%d", port->number);
L
Linus Torvalds 已提交
794 795 796 797
}

static void pp_detach(struct parport *port)
{
798
	device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
L
Linus Torvalds 已提交
799 800
}

801 802 803 804 805 806 807 808 809 810 811
static int pp_probe(struct pardevice *par_dev)
{
	struct device_driver *drv = par_dev->dev.driver;
	int len = strlen(drv->name);

	if (strncmp(par_dev->name, drv->name, len))
		return -ENODEV;

	return 0;
}

L
Linus Torvalds 已提交
812 813
static struct parport_driver pp_driver = {
	.name		= CHRDEV,
814 815
	.probe		= pp_probe,
	.match_port	= pp_attach,
L
Linus Torvalds 已提交
816
	.detach		= pp_detach,
817
	.devmodel	= true,
L
Linus Torvalds 已提交
818 819
};

820
static int __init ppdev_init(void)
L
Linus Torvalds 已提交
821
{
822
	int err = 0;
L
Linus Torvalds 已提交
823

824 825
	if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
		printk(KERN_WARNING CHRDEV ": unable to get major %d\n",
826
		       PP_MAJOR);
L
Linus Torvalds 已提交
827 828
		return -EIO;
	}
829
	ppdev_class = class_create(THIS_MODULE, CHRDEV);
L
Linus Torvalds 已提交
830 831 832 833
	if (IS_ERR(ppdev_class)) {
		err = PTR_ERR(ppdev_class);
		goto out_chrdev;
	}
834 835
	err = parport_register_driver(&pp_driver);
	if (err < 0) {
836
		printk(KERN_WARNING CHRDEV ": unable to register with parport\n");
L
Linus Torvalds 已提交
837 838 839
		goto out_class;
	}

840
	printk(KERN_INFO PP_VERSION "\n");
L
Linus Torvalds 已提交
841 842 843
	goto out;

out_class:
844
	class_destroy(ppdev_class);
L
Linus Torvalds 已提交
845 846 847 848 849 850
out_chrdev:
	unregister_chrdev(PP_MAJOR, CHRDEV);
out:
	return err;
}

851
static void __exit ppdev_cleanup(void)
L
Linus Torvalds 已提交
852 853 854
{
	/* Clean up all parport stuff */
	parport_unregister_driver(&pp_driver);
855
	class_destroy(ppdev_class);
856
	unregister_chrdev(PP_MAJOR, CHRDEV);
L
Linus Torvalds 已提交
857 858 859 860 861 862 863
}

module_init(ppdev_init);
module_exit(ppdev_cleanup);

MODULE_LICENSE("GPL");
MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);