blob: 6af1ce04b3dac9ab1fd2e20d52d1930e6798502b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/ioctl.h>
64#include <linux/parport.h>
65#include <linux/ctype.h>
66#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Rene Hermane6a67842006-03-25 03:07:13 -080068#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/ppdev.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020070#include <linux/mutex.h>
Alan Cox6d535d32008-07-25 01:48:16 -070071#include <linux/uaccess.h>
Bamvor Jian Zhang17a35962016-01-08 15:50:49 +080072#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define PP_VERSION "ppdev: user-space parallel port driver"
75#define CHRDEV "ppdev"
76
77struct pp_struct {
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +053078 struct pardevice *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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 Bergmann613655f2010-06-02 14:28:52 +0200101static DEFINE_MUTEX(pp_do_mutex);
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800102
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 Mukherjee783ea442016-02-12 18:33:39 +0530109static inline void pp_enable_irq(struct pp_struct *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct parport *port = pp->pdev->port;
Sudip Mukherjee27f3b8a2016-02-12 18:33:41 +0530112
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530113 port->ops->enable_irq(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530116static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530117 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Al Viro496ad9a2013-01-23 17:07:38 -0500119 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct pp_struct *pp = file->private_data;
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530121 char *kbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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 Buesch81fc4012009-06-17 16:27:49 -0700128 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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 Mukherjee83e80602016-02-12 18:33:42 +0530137 if (!kbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 pport = pp->pdev->port;
140 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
141
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530142 parport_set_timeout(pp->pdev,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530143 (file->f_flags & O_NONBLOCK) ?
144 PARPORT_INACTIVITY_O_NONBLOCK :
145 pp->default_inactivity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
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 Mukherjee83e80602016-02-12 18:33:42 +0530155 if (pp->flags & PP_W91284PIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 flags |= PARPORT_W91284PIC;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530157 if (pp->flags & PP_FASTREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 flags |= PARPORT_EPP_FAST;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530159 if (pport->ieee1284.mode & IEEE1284_ADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 fn = pport->ops->epp_read_addr;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530161 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 fn = pport->ops->epp_read_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 bytes_read = (*fn)(pport, kbuffer, need, flags);
164 } else {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530165 bytes_read = parport_read(pport, kbuffer, need);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
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 Mukherjee783ea442016-02-12 18:33:39 +0530176 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bytes_read = -ERESTARTSYS;
178 break;
179 }
180
181 cond_resched();
182 }
183
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530184 parport_set_timeout(pp->pdev, pp->default_inactivity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530186 if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 bytes_read = -EFAULT;
188
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530189 kfree(kbuffer);
190 pp_enable_irq(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return bytes_read;
192}
193
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530194static ssize_t pp_write(struct file *file, const char __user *buf,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530195 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Al Viro496ad9a2013-01-23 17:07:38 -0500197 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct pp_struct *pp = file->private_data;
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530199 char *kbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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 Buesch81fc4012009-06-17 16:27:49 -0700207 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EINVAL;
209 }
210
211 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530212 if (!kbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -ENOMEM;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 pport = pp->pdev->port;
216 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
217
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530218 parport_set_timeout(pp->pdev,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530219 (file->f_flags & O_NONBLOCK) ?
220 PARPORT_INACTIVITY_O_NONBLOCK :
221 pp->default_inactivity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 while (bytes_written < count) {
224 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
225
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530226 if (copy_from_user(kbuffer, buf + bytes_written, n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 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 Mukherjee783ea442016-02-12 18:33:39 +0530234 wrote = pport->ops->epp_write_addr(pport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 kbuffer, n, PARPORT_EPP_FAST);
236 } else {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530237 wrote = pport->ops->epp_write_data(pport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kbuffer, n, PARPORT_EPP_FAST);
239 }
240 } else {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530241 wrote = parport_write(pp->pdev->port, kbuffer, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
244 if (wrote <= 0) {
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530245 if (!bytes_written)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 bytes_written = wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 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 Mukherjee783ea442016-02-12 18:33:39 +0530258 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 cond_resched();
262 }
263
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530264 parport_set_timeout(pp->pdev, pp->default_inactivity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530266 kfree(kbuffer);
267 pp_enable_irq(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return bytes_written;
269}
270
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530271static void pp_irq(void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Jeff Garzik5712cb32007-10-19 02:54:26 -0400273 struct pp_struct *pp = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (pp->irqresponse) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530276 parport_write_control(pp->pdev->port, pp->irqctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 pp->irqresponse = 0;
278 }
279
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530280 atomic_inc(&pp->irqc);
281 wake_up_interruptible(&pp->irq_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530284static int register_device(int minor, struct pp_struct *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct parport *port;
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530287 struct pardevice *pdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 char *name;
Sudip Mukherjee8b7d3a92016-06-01 17:50:08 +0530289 struct pardev_cb ppdev_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Julia Lawall87575432010-05-26 14:43:55 -0700291 name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (name == NULL)
293 return -ENOMEM;
294
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530295 port = parport_find_number(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!port) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530297 printk(KERN_WARNING "%s: no associated port!\n", name);
298 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -ENXIO;
300 }
301
Sudip Mukherjee8b7d3a92016-06-01 17:50:08 +0530302 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 Mukherjee783ea442016-02-12 18:33:39 +0530307 parport_put_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (!pdev) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530310 printk(KERN_WARNING "%s: failed to register device!\n", name);
311 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENXIO;
313 }
314
315 pp->pdev = pdev;
Sudip Mukherjee396ec3de2016-02-12 18:33:46 +0530316 dev_dbg(&pdev->dev, "registered pardevice\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530320static enum ieee1284_phase init_phase(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
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 Zhang3b9ab372016-01-08 15:50:48 +0800331static 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 Cox6d535d32008-07-25 01:48:16 -0700347static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Al Viro496ad9a2013-01-23 17:07:38 -0500349 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct pp_struct *pp = file->private_data;
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530351 struct parport *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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 Mukherjee396ec3de2016-02-12 18:33:46 +0530362 dev_dbg(&pp->pdev->dev, "you've already got it!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -EINVAL;
364 }
365
366 /* Deferred device registration. */
367 if (!pp->pdev) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530368 int err = register_device(minor, pp);
Sudip Mukherjee27f3b8a2016-02-12 18:33:41 +0530369
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530370 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530374 ret = parport_claim_or_block(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 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 Mukherjee783ea442016-02-12 18:33:39 +0530382 pp_enable_irq(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
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 Mukherjee783ea442016-02-12 18:33:39 +0530390 pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
391 parport_set_timeout(pp->pdev, pp->default_inactivity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return 0;
394 }
395 case PPEXCL:
396 if (pp->pdev) {
Sudip Mukherjee396ec3de2016-02-12 18:33:46 +0530397 dev_dbg(&pp->pdev->dev,
398 "too late for PPEXCL; already registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 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 Mukherjee27f3b8a2016-02-12 18:33:41 +0530413
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530414 if (copy_from_user(&mode, argp, sizeof(mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -EFAULT;
416 /* FIXME: validate mode */
417 pp->state.mode = mode;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530418 pp->state.phase = init_phase(mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
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 Mukherjee83e80602016-02-12 18:33:42 +0530431 if (pp->flags & PP_CLAIMED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 mode = pp->pdev->port->ieee1284.mode;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530433 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 mode = pp->state.mode;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530435
436 if (copy_to_user(argp, &mode, sizeof(mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 }
440 case PPSETPHASE:
441 {
442 int phase;
Sudip Mukherjee27f3b8a2016-02-12 18:33:41 +0530443
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530444 if (copy_from_user(&phase, argp, sizeof(phase)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EFAULT;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* FIXME: validate phase */
448 pp->state.phase = phase;
449
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530450 if (pp->flags & PP_CLAIMED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 pp->pdev->port->ieee1284.phase = phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return 0;
454 }
455 case PPGETPHASE:
456 {
457 int phase;
458
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530459 if (pp->flags & PP_CLAIMED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 phase = pp->pdev->port->ieee1284.phase;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530461 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 phase = pp->state.phase;
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530463 if (copy_to_user(argp, &phase, sizeof(phase)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466 }
467 case PPGETMODES:
468 {
469 unsigned int modes;
470
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530471 port = parport_find_number(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!port)
473 return -ENODEV;
474
475 modes = port->modes;
Julia Lawalld98808a2011-05-26 16:25:59 -0700476 parport_put_port(port);
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530477 if (copy_to_user(argp, &modes, sizeof(modes)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480 }
481 case PPSETFLAGS:
482 {
483 int uflags;
484
Sudip Mukherjee83e80602016-02-12 18:33:42 +0530485 if (copy_from_user(&uflags, argp, sizeof(uflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 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 Mukherjee83e80602016-02-12 18:33:42 +0530496 if (copy_to_user(argp, &uflags, sizeof(uflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 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 Buesch81fc4012009-06-17 16:27:49 -0700505 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 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 Zhang3b9ab372016-01-08 15:50:48 +0800515 s32 time32[2];
516 s64 time64[2];
517 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 case PPRSTATUS:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530521 reg = parport_read_status(port);
522 if (copy_to_user(argp, &reg, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EFAULT;
524 return 0;
525 case PPRDATA:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530526 reg = parport_read_data(port);
527 if (copy_to_user(argp, &reg, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EFAULT;
529 return 0;
530 case PPRCONTROL:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530531 reg = parport_read_control(port);
532 if (copy_to_user(argp, &reg, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EFAULT;
534 return 0;
535 case PPYIELD:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530536 parport_yield_blocking(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 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 Mukherjee783ea442016-02-12 18:33:39 +0530546 parport_release(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 pp->flags &= ~PP_CLAIMED;
548 return 0;
549
550 case PPWCONTROL:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530551 if (copy_from_user(&reg, argp, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530553 parport_write_control(port, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555
556 case PPWDATA:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530557 if (copy_from_user(&reg, argp, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530559 parport_write_data(port, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561
562 case PPFCONTROL:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530563 if (copy_from_user(&mask, argp,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530564 sizeof(mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530566 if (copy_from_user(&reg, 1 + (unsigned char __user *) arg,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530567 sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530569 parport_frob_control(port, mask, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571
572 case PPDATADIR:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530573 if (copy_from_user(&mode, argp, sizeof(mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return -EFAULT;
575 if (mode)
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530576 port->ops->data_reverse(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530578 port->ops->data_forward(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580
581 case PPNEGOT:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530582 if (copy_from_user(&mode, argp, sizeof(mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530584 switch ((ret = parport_negotiate(port, mode))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 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 Mukherjee783ea442016-02-12 18:33:39 +0530593 pp_enable_irq(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return ret;
595
596 case PPWCTLONIRQ:
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530597 if (copy_from_user(&reg, argp, sizeof(reg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 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 Mukherjee783ea442016-02-12 18:33:39 +0530607 ret = atomic_read(&pp->irqc);
608 if (copy_to_user(argp, &ret, sizeof(ret)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EFAULT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530610 atomic_sub(ret, &pp->irqc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800613 case PPSETTIME32:
614 if (copy_from_user(time32, argp, sizeof(time32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EFAULT;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800616
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 Torvalds1da177e2005-04-16 15:20:36 -0700630 return -EINVAL;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800631
632 if (copy_to_user(argp, time32, sizeof(time32)))
633 return -EFAULT;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800637 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 Torvalds1da177e2005-04-16 15:20:36 -0700645 return -EFAULT;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648
649 default:
Sudip Mukherjee396ec3de2016-02-12 18:33:46 +0530650 dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EINVAL;
652 }
653
654 /* Keep the compiler happy */
655 return 0;
656}
657
Alan Cox6d535d32008-07-25 01:48:16 -0700658static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
659{
660 long ret;
Sudip Mukherjee27f3b8a2016-02-12 18:33:41 +0530661
Arnd Bergmann613655f2010-06-02 14:28:52 +0200662 mutex_lock(&pp_do_mutex);
Alan Cox6d535d32008-07-25 01:48:16 -0700663 ret = pp_do_ioctl(file, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200664 mutex_unlock(&pp_do_mutex);
Alan Cox6d535d32008-07-25 01:48:16 -0700665 return ret;
666}
667
Bamvor Jian Zhang17a35962016-01-08 15:50:49 +0800668#ifdef CONFIG_COMPAT
669static long pp_compat_ioctl(struct file *file, unsigned int cmd,
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530670 unsigned long arg)
Bamvor Jian Zhang17a35962016-01-08 15:50:49 +0800671{
672 return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
673}
674#endif
675
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530676static int pp_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 unsigned int minor = iminor(inode);
679 struct pp_struct *pp;
680
681 if (minor >= PARPORT_MAX)
682 return -ENXIO;
683
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530684 pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!pp)
686 return -ENOMEM;
687
688 pp->state.mode = IEEE1284_MODE_COMPAT;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530689 pp->state.phase = init_phase(pp->state.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 pp->flags = 0;
691 pp->irqresponse = 0;
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530692 atomic_set(&pp->irqc, 0);
693 init_waitqueue_head(&pp->irq_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
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 Mukherjeed85c1a22016-02-12 18:33:40 +0530705static int pp_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
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 Mukherjee3c8db582016-02-12 18:33:44 +0530714 struct ieee1284_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* parport released, but not in compatibility mode */
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530717 parport_claim_or_block(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 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 Mukherjee783ea442016-02-12 18:33:39 +0530730 parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
Sudip Mukherjee396ec3de2016-02-12 18:33:46 +0530731 dev_dbg(&pp->pdev->dev,
732 "negotiated back to compatibility mode because user-space forgot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
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 Mukherjee783ea442016-02-12 18:33:39 +0530743 parport_release(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (compat_negot != 1) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700745 pr_debug(CHRDEV "%x: released pardevice "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 "because user-space forgot\n", minor);
747 }
748 }
749
750 if (pp->pdev) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530751 parport_unregister_device(pp->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 pp->pdev = NULL;
Michael Buesch81fc4012009-06-17 16:27:49 -0700753 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530756 kfree(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return 0;
759}
760
761/* No kernel lock held - fine */
Sudip Mukherjeed85c1a22016-02-12 18:33:40 +0530762static unsigned int pp_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct pp_struct *pp = file->private_data;
765 unsigned int mask = 0;
766
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530767 poll_wait(file, &pp->irq_wait, wait);
768 if (atomic_read(&pp->irqc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 mask |= POLLIN | POLLRDNORM;
770
771 return mask;
772}
773
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800774static struct class *ppdev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Arjan van de Ven62322d22006-07-03 00:24:21 -0700776static const struct file_operations pp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .owner = THIS_MODULE,
778 .llseek = no_llseek,
779 .read = pp_read,
780 .write = pp_write,
781 .poll = pp_poll,
Alan Cox6d535d32008-07-25 01:48:16 -0700782 .unlocked_ioctl = pp_ioctl,
Bamvor Jian Zhang17a35962016-01-08 15:50:49 +0800783#ifdef CONFIG_COMPAT
784 .compat_ioctl = pp_compat_ioctl,
785#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .open = pp_open,
787 .release = pp_release,
788};
789
790static void pp_attach(struct parport *port)
791{
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700792 device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
793 NULL, "parport%d", port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796static void pp_detach(struct parport *port)
797{
Greg Kroah-Hartman04880ed2006-09-12 17:00:10 +0200798 device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Sudip Mukherjee8b7d3a92016-06-01 17:50:08 +0530801static 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 Torvalds1da177e2005-04-16 15:20:36 -0700812static struct parport_driver pp_driver = {
813 .name = CHRDEV,
Sudip Mukherjee8b7d3a92016-06-01 17:50:08 +0530814 .probe = pp_probe,
815 .match_port = pp_attach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .detach = pp_detach,
Sudip Mukherjee8b7d3a92016-06-01 17:50:08 +0530817 .devmodel = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818};
819
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530820static int __init ppdev_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700822 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530824 if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
825 printk(KERN_WARNING CHRDEV ": unable to get major %d\n",
Sudip Mukherjeea7c71c02016-02-12 18:33:43 +0530826 PP_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -EIO;
828 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800829 ppdev_class = class_create(THIS_MODULE, CHRDEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (IS_ERR(ppdev_class)) {
831 err = PTR_ERR(ppdev_class);
832 goto out_chrdev;
833 }
Alexey Khoroshilov9a32bb32012-09-02 00:31:58 +0400834 err = parport_register_driver(&pp_driver);
835 if (err < 0) {
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530836 printk(KERN_WARNING CHRDEV ": unable to register with parport\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto out_class;
838 }
839
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530840 printk(KERN_INFO PP_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto out;
842
843out_class:
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800844 class_destroy(ppdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845out_chrdev:
846 unregister_chrdev(PP_MAJOR, CHRDEV);
847out:
848 return err;
849}
850
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530851static void __exit ppdev_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Clean up all parport stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 parport_unregister_driver(&pp_driver);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800855 class_destroy(ppdev_class);
Sudip Mukherjee783ea442016-02-12 18:33:39 +0530856 unregister_chrdev(PP_MAJOR, CHRDEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859module_init(ppdev_init);
860module_exit(ppdev_cleanup);
861
862MODULE_LICENSE("GPL");
863MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);