OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [ppdev.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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/devfs_fs_kernel.h>
63
#include <linux/ioctl.h>
64
#include <linux/parport.h>
65
#include <linux/ctype.h>
66
#include <linux/poll.h>
67
#include <asm/uaccess.h>
68
#include <linux/ppdev.h>
69
#include <linux/smp_lock.h>
70
 
71
#define PP_VERSION "ppdev: user-space parallel port driver"
72
#define CHRDEV "ppdev"
73
 
74
struct pp_struct {
75
        struct pardevice * pdev;
76
        wait_queue_head_t irq_wait;
77
        atomic_t irqc;
78
        unsigned int flags;
79
        int irqresponse;
80
        unsigned char irqctl;
81
        struct ieee1284_info state;
82
        struct ieee1284_info saved_state;
83
        long default_inactivity;
84
};
85
 
86
/* pp_struct.flags bitfields */
87
#define PP_CLAIMED    (1<<0)
88
#define PP_EXCL       (1<<1)
89
 
90
/* Other constants */
91
#define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
92
#define PP_BUFFER_SIZE 1024
93
#define PARDEVICE_MAX 8
94
 
95
/* ROUND_UP macro from fs/select.c */
96
#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
97
 
98
static inline void pp_enable_irq (struct pp_struct *pp)
99
{
100
        struct parport *port = pp->pdev->port;
101
        port->ops->enable_irq (port);
102
}
103
 
104
static ssize_t pp_read (struct file * file, char * buf, size_t count,
105
                        loff_t * ppos)
106
{
107
        unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
108
        struct pp_struct *pp = file->private_data;
109
        char * kbuffer;
110
        ssize_t bytes_read = 0;
111
        struct parport *pport;
112
        int mode;
113
 
114
        if (!(pp->flags & PP_CLAIMED)) {
115
                /* Don't have the port claimed */
116
                printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
117
                        minor);
118
                return -EINVAL;
119
        }
120
 
121
        /* Trivial case. */
122
        if (count == 0)
123
                return 0;
124
 
125
        kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
126
        if (!kbuffer) {
127
                return -ENOMEM;
128
        }
129
        pport = pp->pdev->port;
130
        mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
131
 
132
        parport_set_timeout (pp->pdev,
133
                             (file->f_flags & O_NONBLOCK) ?
134
                             PARPORT_INACTIVITY_O_NONBLOCK :
135
                             pp->default_inactivity);
136
 
137
        while (bytes_read == 0) {
138
                ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
139
 
140
                if (mode == IEEE1284_MODE_EPP) {
141
                        /* various specials for EPP mode */
142
                        int flags = 0;
143
                        size_t (*fn)(struct parport *, void *, size_t, int);
144
 
145
                        if (pp->flags & PP_W91284PIC) {
146
                                flags |= PARPORT_W91284PIC;
147
                        }
148
                        if (pp->flags & PP_FASTREAD) {
149
                                flags |= PARPORT_EPP_FAST;
150
                        }
151
                        if (pport->ieee1284.mode & IEEE1284_ADDR) {
152
                                fn = pport->ops->epp_read_addr;
153
                        } else {
154
                                fn = pport->ops->epp_read_data;
155
                        }
156
                        bytes_read = (*fn)(pport, kbuffer, need, flags);
157
                } else {
158
                        bytes_read = parport_read (pport, kbuffer, need);
159
                }
160
 
161
                if (bytes_read != 0)
162
                        break;
163
 
164
                if (file->f_flags & O_NONBLOCK) {
165
                        bytes_read = -EAGAIN;
166
                        break;
167
                }
168
 
169
                if (signal_pending (current)) {
170
                        bytes_read = -ERESTARTSYS;
171
                        break;
172
                }
173
 
174
                if (current->need_resched)
175
                        schedule ();
176
        }
177
 
178
        parport_set_timeout (pp->pdev, pp->default_inactivity);
179
 
180
        if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
181
                bytes_read = -EFAULT;
182
 
183
        kfree (kbuffer);
184
        pp_enable_irq (pp);
185
        return bytes_read;
186
}
187
 
188
static ssize_t pp_write (struct file * file, const char * buf, size_t count,
189
                         loff_t * ppos)
190
{
191
        unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
192
        struct pp_struct *pp = file->private_data;
193
        char * kbuffer;
194
        ssize_t bytes_written = 0;
195
        ssize_t wrote;
196
        int mode;
197
        struct parport *pport;
198
 
199
        if (!(pp->flags & PP_CLAIMED)) {
200
                /* Don't have the port claimed */
201
                printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
202
                        minor);
203
                return -EINVAL;
204
        }
205
 
206
        kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
207
        if (!kbuffer) {
208
                return -ENOMEM;
209
        }
210
        pport = pp->pdev->port;
211
        mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
212
 
213
        parport_set_timeout (pp->pdev,
214
                             (file->f_flags & O_NONBLOCK) ?
215
                             PARPORT_INACTIVITY_O_NONBLOCK :
216
                             pp->default_inactivity);
217
 
218
        while (bytes_written < count) {
219
                ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
220
 
221
                if (copy_from_user (kbuffer, buf + bytes_written, n)) {
222
                        bytes_written = -EFAULT;
223
                        break;
224
                }
225
 
226
                if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
227
                        /* do a fast EPP write */
228
                        if (pport->ieee1284.mode & IEEE1284_ADDR) {
229
                                wrote = pport->ops->epp_write_addr (pport,
230
                                        kbuffer, n, PARPORT_EPP_FAST);
231
                        } else {
232
                                wrote = pport->ops->epp_write_data (pport,
233
                                        kbuffer, n, PARPORT_EPP_FAST);
234
                        }
235
                } else {
236
                        wrote = parport_write (pp->pdev->port, kbuffer, n);
237
                }
238
 
239
                if (wrote <= 0) {
240
                        if (!bytes_written) {
241
                                bytes_written = wrote;
242
                        }
243
                        break;
244
                }
245
 
246
                bytes_written += wrote;
247
 
248
                if (file->f_flags & O_NONBLOCK) {
249
                        if (!bytes_written)
250
                                bytes_written = -EAGAIN;
251
                        break;
252
                }
253
 
254
                if (signal_pending (current)) {
255
                        if (!bytes_written) {
256
                                bytes_written = -EINTR;
257
                        }
258
                        break;
259
                }
260
 
261
                if (current->need_resched) {
262
                        schedule ();
263
                }
264
        }
265
 
266
        parport_set_timeout (pp->pdev, pp->default_inactivity);
267
 
268
        kfree (kbuffer);
269
        pp_enable_irq (pp);
270
        return bytes_written;
271
}
272
 
273
static void pp_irq (int irq, void * private, struct pt_regs * unused)
274
{
275
        struct pp_struct * pp = (struct pp_struct *) private;
276
 
277
        if (pp->irqresponse) {
278
                parport_write_control (pp->pdev->port, pp->irqctl);
279
                pp->irqresponse = 0;
280
        }
281
 
282
        atomic_inc (&pp->irqc);
283
        wake_up_interruptible (&pp->irq_wait);
284
}
285
 
286
static int register_device (int minor, struct pp_struct *pp)
287
{
288
        struct parport *port;
289
        struct pardevice * pdev = NULL;
290
        char *name;
291
        int fl;
292
 
293
        name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
294
        if (name == NULL)
295
                return -ENOMEM;
296
 
297
        sprintf (name, CHRDEV "%x", minor);
298
 
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;
318
        printk (KERN_DEBUG "%s: registered pardevice\n", name);
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
 
333
static int pp_ioctl(struct inode *inode, struct file *file,
334
                    unsigned int cmd, unsigned long arg)
335
{
336
        unsigned int minor = MINOR(inode->i_rdev);
337
        struct pp_struct *pp = file->private_data;
338
        struct parport * port;
339
 
340
        /* First handle the cases that don't take arguments. */
341
        switch (cmd) {
342
        case PPCLAIM:
343
            {
344
                struct ieee1284_info *info;
345
                int ret;
346
 
347
                if (pp->flags & PP_CLAIMED) {
348
                        printk (KERN_DEBUG CHRDEV
349
                                "%x: you've already got it!\n", minor);
350
                        return -EINVAL;
351
                }
352
 
353
                /* Deferred device registration. */
354
                if (!pp->pdev) {
355
                        int err = register_device (minor, pp);
356
                        if (err) {
357
                                return err;
358
                        }
359
                }
360
 
361
                ret = parport_claim_or_block (pp->pdev);
362
                if (ret < 0)
363
                        return ret;
364
 
365
                pp->flags |= PP_CLAIMED;
366
 
367
                /* For interrupt-reporting to work, we need to be
368
                 * informed of each interrupt. */
369
                pp_enable_irq (pp);
370
 
371
                /* We may need to fix up the state machine. */
372
                info = &pp->pdev->port->ieee1284;
373
                pp->saved_state.mode = info->mode;
374
                pp->saved_state.phase = info->phase;
375
                info->mode = pp->state.mode;
376
                info->phase = pp->state.phase;
377
                pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
378
                parport_set_timeout (pp->pdev, pp->default_inactivity);
379
 
380
                return 0;
381
            }
382
        case PPEXCL:
383
                if (pp->pdev) {
384
                        printk (KERN_DEBUG CHRDEV "%x: too late for PPEXCL; "
385
                                "already registered\n", minor);
386
                        if (pp->flags & PP_EXCL)
387
                                /* But it's not really an error. */
388
                                return 0;
389
                        /* There's no chance of making the driver happy. */
390
                        return -EINVAL;
391
                }
392
 
393
                /* Just remember to register the device exclusively
394
                 * when we finally do the registration. */
395
                pp->flags |= PP_EXCL;
396
                return 0;
397
        case PPSETMODE:
398
            {
399
                int mode;
400
                if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
401
                        return -EFAULT;
402
                /* FIXME: validate mode */
403
                pp->state.mode = mode;
404
                pp->state.phase = init_phase (mode);
405
 
406
                if (pp->flags & PP_CLAIMED) {
407
                        pp->pdev->port->ieee1284.mode = mode;
408
                        pp->pdev->port->ieee1284.phase = pp->state.phase;
409
                }
410
 
411
                return 0;
412
            }
413
        case PPGETMODE:
414
            {
415
                int mode;
416
 
417
                if (pp->flags & PP_CLAIMED) {
418
                        mode = pp->pdev->port->ieee1284.mode;
419
                } else {
420
                        mode = pp->state.mode;
421
                }
422
                if (copy_to_user ((int *)arg, &mode, sizeof (mode))) {
423
                        return -EFAULT;
424
                }
425
                return 0;
426
            }
427
        case PPSETPHASE:
428
            {
429
                int phase;
430
                if (copy_from_user (&phase, (int *) arg, sizeof (phase))) {
431
                        return -EFAULT;
432
                }
433
                /* FIXME: validate phase */
434
                pp->state.phase = phase;
435
 
436
                if (pp->flags & PP_CLAIMED) {
437
                        pp->pdev->port->ieee1284.phase = phase;
438
                }
439
 
440
                return 0;
441
            }
442
        case PPGETPHASE:
443
            {
444
                int phase;
445
 
446
                if (pp->flags & PP_CLAIMED) {
447
                        phase = pp->pdev->port->ieee1284.phase;
448
                } else {
449
                        phase = pp->state.phase;
450
                }
451
                if (copy_to_user ((int *)arg, &phase, sizeof (phase))) {
452
                        return -EFAULT;
453
                }
454
                return 0;
455
            }
456
        case PPGETMODES:
457
            {
458
                unsigned int modes;
459
 
460
                port = parport_find_number (minor);
461
                if (!port)
462
                        return -ENODEV;
463
 
464
                modes = port->modes;
465
                if (copy_to_user ((unsigned int *)arg, &modes, sizeof (modes))) {
466
                        return -EFAULT;
467
                }
468
                return 0;
469
            }
470
        case PPSETFLAGS:
471
            {
472
                int uflags;
473
 
474
                if (copy_from_user (&uflags, (int *)arg, sizeof (uflags))) {
475
                        return -EFAULT;
476
                }
477
                pp->flags &= ~PP_FLAGMASK;
478
                pp->flags |= (uflags & PP_FLAGMASK);
479
                return 0;
480
            }
481
        case PPGETFLAGS:
482
            {
483
                int uflags;
484
 
485
                uflags = pp->flags & PP_FLAGMASK;
486
                if (copy_to_user ((int *)arg, &uflags, sizeof (uflags))) {
487
                        return -EFAULT;
488
                }
489
                return 0;
490
            }
491
        }       /* end switch() */
492
 
493
        /* Everything else requires the port to be claimed, so check
494
         * that now. */
495
        if ((pp->flags & PP_CLAIMED) == 0) {
496
                printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
497
                        minor);
498
                return -EINVAL;
499
        }
500
 
501
        port = pp->pdev->port;
502
        switch (cmd) {
503
                struct ieee1284_info *info;
504
                unsigned char reg;
505
                unsigned char mask;
506
                int mode;
507
                int ret;
508
                struct timeval par_timeout;
509
                long to_jiffies;
510
 
511
        case PPRSTATUS:
512
                reg = parport_read_status (port);
513
                if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
514
                        return -EFAULT;
515
                return 0;
516
        case PPRDATA:
517
                reg = parport_read_data (port);
518
                if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
519
                        return -EFAULT;
520
                return 0;
521
        case PPRCONTROL:
522
                reg = parport_read_control (port);
523
                if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
524
                        return -EFAULT;
525
                return 0;
526
        case PPYIELD:
527
                parport_yield_blocking (pp->pdev);
528
                return 0;
529
 
530
        case PPRELEASE:
531
                /* Save the state machine's state. */
532
                info = &pp->pdev->port->ieee1284;
533
                pp->state.mode = info->mode;
534
                pp->state.phase = info->phase;
535
                info->mode = pp->saved_state.mode;
536
                info->phase = pp->saved_state.phase;
537
                parport_release (pp->pdev);
538
                pp->flags &= ~PP_CLAIMED;
539
                return 0;
540
 
541
        case PPWCONTROL:
542
                if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
543
                        return -EFAULT;
544
                parport_write_control (port, reg);
545
                return 0;
546
 
547
        case PPWDATA:
548
                if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
549
                        return -EFAULT;
550
                parport_write_data (port, reg);
551
                return 0;
552
 
553
        case PPFCONTROL:
554
                if (copy_from_user (&mask, (unsigned char *) arg,
555
                                    sizeof (mask)))
556
                        return -EFAULT;
557
                if (copy_from_user (&reg, 1 + (unsigned char *) arg,
558
                                    sizeof (reg)))
559
                        return -EFAULT;
560
                parport_frob_control (port, mask, reg);
561
                return 0;
562
 
563
        case PPDATADIR:
564
                if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
565
                        return -EFAULT;
566
                if (mode)
567
                        port->ops->data_reverse (port);
568
                else
569
                        port->ops->data_forward (port);
570
                return 0;
571
 
572
        case PPNEGOT:
573
                if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
574
                        return -EFAULT;
575
                switch ((ret = parport_negotiate (port, mode))) {
576
                case 0: break;
577
                case -1: /* handshake failed, peripheral not IEEE 1284 */
578
                        ret = -EIO;
579
                        break;
580
                case 1:  /* handshake succeeded, peripheral rejected mode */
581
                        ret = -ENXIO;
582
                        break;
583
                }
584
                pp_enable_irq (pp);
585
                return ret;
586
 
587
        case PPWCTLONIRQ:
588
                if (copy_from_user (&reg, (unsigned char *) arg,
589
                                    sizeof (reg)))
590
                        return -EFAULT;
591
 
592
                /* Remember what to set the control lines to, for next
593
                 * time we get an interrupt. */
594
                pp->irqctl = reg;
595
                pp->irqresponse = 1;
596
                return 0;
597
 
598
        case PPCLRIRQ:
599
                ret = atomic_read (&pp->irqc);
600
                if (copy_to_user ((int *) arg, &ret, sizeof (ret)))
601
                        return -EFAULT;
602
                atomic_sub (ret, &pp->irqc);
603
                return 0;
604
 
605
        case PPSETTIME:
606
                if (copy_from_user (&par_timeout, (struct timeval *)arg,
607
                                    sizeof(struct timeval))) {
608
                        return -EFAULT;
609
                }
610
                /* Convert to jiffies, place in pp->pdev->timeout */
611
                if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
612
                        return -EINVAL;
613
                }
614
                to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
615
                to_jiffies += par_timeout.tv_sec * (long)HZ;
616
                if (to_jiffies <= 0) {
617
                        return -EINVAL;
618
                }
619
                pp->pdev->timeout = to_jiffies;
620
                return 0;
621
 
622
        case PPGETTIME:
623
                to_jiffies = pp->pdev->timeout;
624
                par_timeout.tv_sec = to_jiffies / HZ;
625
                par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
626
                if (copy_to_user ((struct timeval *)arg, &par_timeout,
627
                                  sizeof(struct timeval))) {
628
                        return -EFAULT;
629
                }
630
                return 0;
631
 
632
        default:
633
                printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
634
                        cmd);
635
                return -EINVAL;
636
        }
637
 
638
        /* Keep the compiler happy */
639
        return 0;
640
}
641
 
642
static int pp_open (struct inode * inode, struct file * file)
643
{
644
        unsigned int minor = MINOR (inode->i_rdev);
645
        struct pp_struct *pp;
646
 
647
        if (minor >= PARPORT_MAX)
648
                return -ENXIO;
649
 
650
        pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
651
        if (!pp)
652
                return -ENOMEM;
653
 
654
        pp->state.mode = IEEE1284_MODE_COMPAT;
655
        pp->state.phase = init_phase (pp->state.mode);
656
        pp->flags = 0;
657
        pp->irqresponse = 0;
658
        atomic_set (&pp->irqc, 0);
659
        init_waitqueue_head (&pp->irq_wait);
660
 
661
        /* Defer the actual device registration until the first claim.
662
         * That way, we know whether or not the driver wants to have
663
         * exclusive access to the port (PPEXCL).
664
         */
665
        pp->pdev = NULL;
666
        file->private_data = pp;
667
 
668
        return 0;
669
}
670
 
671
static int pp_release (struct inode * inode, struct file * file)
672
{
673
        unsigned int minor = MINOR (inode->i_rdev);
674
        struct pp_struct *pp = file->private_data;
675
        int compat_negot;
676
 
677
        lock_kernel();
678
        compat_negot = 0;
679
        if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
680
            (pp->state.mode != IEEE1284_MODE_COMPAT)) {
681
                struct ieee1284_info *info;
682
 
683
                /* parport released, but not in compatability mode */
684
                parport_claim_or_block (pp->pdev);
685
                pp->flags |= PP_CLAIMED;
686
                info = &pp->pdev->port->ieee1284;
687
                pp->saved_state.mode = info->mode;
688
                pp->saved_state.phase = info->phase;
689
                info->mode = pp->state.mode;
690
                info->phase = pp->state.phase;
691
                compat_negot = 1;
692
        } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
693
            (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
694
                compat_negot = 2;
695
        }
696
        if (compat_negot) {
697
                parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
698
                printk (KERN_DEBUG CHRDEV
699
                        "%x: negotiated back to compatibility mode because "
700
                        "user-space forgot\n", minor);
701
        }
702
 
703
        if (pp->flags & PP_CLAIMED) {
704
                struct ieee1284_info *info;
705
 
706
                info = &pp->pdev->port->ieee1284;
707
                pp->state.mode = info->mode;
708
                pp->state.phase = info->phase;
709
                info->mode = pp->saved_state.mode;
710
                info->phase = pp->saved_state.phase;
711
                parport_release (pp->pdev);
712
                if (compat_negot != 1) {
713
                        printk (KERN_DEBUG CHRDEV "%x: released pardevice "
714
                                "because user-space forgot\n", minor);
715
                }
716
        }
717
 
718
        if (pp->pdev) {
719
                const char *name = pp->pdev->name;
720
                parport_unregister_device (pp->pdev);
721
                kfree (name);
722
                pp->pdev = NULL;
723
                printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
724
                        minor);
725
        }
726
        unlock_kernel();
727
 
728
        kfree (pp);
729
 
730
        return 0;
731
}
732
 
733
/* No kernel lock held - fine */
734
static unsigned int pp_poll (struct file * file, poll_table * wait)
735
{
736
        struct pp_struct *pp = file->private_data;
737
        unsigned int mask = 0;
738
 
739
        poll_wait (file, &pp->irq_wait, wait);
740
        if (atomic_read (&pp->irqc))
741
                mask |= POLLIN | POLLRDNORM;
742
 
743
        return mask;
744
}
745
 
746
static struct file_operations pp_fops = {
747
        owner:          THIS_MODULE,
748
        llseek:         no_llseek,
749
        read:           pp_read,
750
        write:          pp_write,
751
        poll:           pp_poll,
752
        ioctl:          pp_ioctl,
753
        open:           pp_open,
754
        release:        pp_release,
755
};
756
 
757
static devfs_handle_t devfs_handle;
758
 
759
static int __init ppdev_init (void)
760
{
761
        if (devfs_register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
762
                printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
763
                        PP_MAJOR);
764
                return -EIO;
765
        }
766
        devfs_handle = devfs_mk_dir (NULL, "parports", NULL);
767
        devfs_register_series (devfs_handle, "%u", PARPORT_MAX,
768
                               DEVFS_FL_DEFAULT, PP_MAJOR, 0,
769
                               S_IFCHR | S_IRUGO | S_IWUGO,
770
                               &pp_fops, NULL);
771
 
772
        printk (KERN_INFO PP_VERSION "\n");
773
        return 0;
774
}
775
 
776
static void __exit ppdev_cleanup (void)
777
{
778
        /* Clean up all parport stuff */
779
        devfs_unregister (devfs_handle);
780
        devfs_unregister_chrdev (PP_MAJOR, CHRDEV);
781
}
782
 
783
module_init(ppdev_init);
784
module_exit(ppdev_cleanup);
785
 
786
MODULE_LICENSE("GPL");
787
 
788
EXPORT_NO_SYMBOLS;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.