1 /* $Id: parport_share.c,v 1.15 1998/01/11 12:06:17 philip Exp $
2 * Parallel-port resource manager code.
3 *
4 * Authors: David Campbell <campbell@tirian.che.curtin.edu.au>
5 * Tim Waugh <tim@cyberelk.demon.co.uk>
6 * Jose Renau <renau@acm.org>
7 * Philip Blundell <philb@gnu.org>
8 * Andrea Arcangeli
9 *
10 * based on work by Grant Guenther <grant@torque.net>
11 * and Philip Blundell
12 *
13 * Any part of this program may be used in documents licensed under
14 * the GNU Free Documentation License, Version 1.1 or any later version
15 * published by the Free Software Foundation.
16 */
17
18 #undef PARPORT_DEBUG_SHARING /* undef for production */
19
20 #include <linux/config.h>
21 #include <linux/string.h>
22 #include <linux/threads.h>
23 #include <linux/parport.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioport.h>
28 #include <linux/kernel.h>
29 #include <linux/malloc.h>
30 #include <linux/sched.h>
31 #include <linux/kmod.h>
32
33 #include <linux/spinlock.h>
34 #include <asm/irq.h>
35
36 #undef PARPORT_PARANOID
37
38 #define PARPORT_DEFAULT_TIMESLICE (HZ/5)
39
40 unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
41 int parport_default_spintime = DEFAULT_SPIN_TIME;
42
43 static struct parport *portlist = NULL, *portlist_tail = NULL;
44 spinlock_t parportlist_lock = SPIN_LOCK_UNLOCKED;
45
46 static struct parport_driver *driver_chain = NULL;
47 spinlock_t driverlist_lock = SPIN_LOCK_UNLOCKED;
48
49 /* What you can do to a port that's gone away.. */
50 static void dead_write_lines (struct parport *p, unsigned char b){}
51 static unsigned char dead_read_lines (struct parport *p) { return 0; }
52 static unsigned char dead_frob_lines (struct parport *p, unsigned char b,
53 unsigned char c) { return 0; }
54 static void dead_onearg (struct parport *p){}
55 static void dead_initstate (struct pardevice *d, struct parport_state *s) { }
56 static void dead_state (struct parport *p, struct parport_state *s) { }
57 static void dead_noargs (void) { }
58 static size_t dead_write (struct parport *p, const void *b, size_t l, int f)
59 { return 0; }
60 static size_t dead_read (struct parport *p, void *b, size_t l, int f)
61 { return 0; }
62 static struct parport_operations dead_ops = {
63 dead_write_lines, /* data */
64 dead_read_lines,
65 dead_write_lines, /* control */
66 dead_read_lines,
67 dead_frob_lines,
68 dead_read_lines, /* status */
69 dead_onearg, /* enable_irq */
70 dead_onearg, /* disable_irq */
71 dead_onearg, /* data_forward */
72 dead_onearg, /* data_reverse */
73 dead_initstate, /* init_state */
74 dead_state,
75 dead_state,
76 dead_noargs, /* xxx_use_count */
77 dead_noargs,
78 dead_write, /* epp */
79 dead_read,
80 dead_write,
81 dead_read,
82 dead_write, /* ecp */
83 dead_read,
84 dead_write,
85 dead_write, /* compat */
86 dead_read, /* nibble */
87 dead_read /* byte */
88 };
89
90 /* Call attach(port) for each registered driver. */
91 static void attach_driver_chain(struct parport *port)
92 {
93 struct parport_driver *drv;
94 void (**attach) (struct parport *);
95 int count = 0, i;
96
97 /* This is complicated because attach() must be able to block,
98 * but we can't let it do that while we're holding a
99 * spinlock. */
100
101 spin_lock (&driverlist_lock);
102 for (drv = driver_chain; drv; drv = drv->next)
103 count++;
104 spin_unlock (&driverlist_lock);
105
106 /* Drivers can unregister here; that's okay. If they register
107 * they'll be given an attach during parport_register_driver,
108 * so that's okay too. The only worry is that someone might
109 * get given an attach twice if they registered just before
110 * this function gets called. */
111
112 /* Hmm, this could be fixed with a generation number..
113 * FIXME */
114
115 attach = kmalloc (sizeof (void(*)(struct parport *)) * count,
116 GFP_KERNEL);
117 if (!attach) {
118 printk (KERN_WARNING "parport: not enough memory to attach\n");
119 return;
120 }
121
122 spin_lock (&driverlist_lock);
123 for (i = 0, drv = driver_chain; drv && i < count; drv = drv->next)
124 attach[i++] = drv->attach;
125 spin_unlock (&driverlist_lock);
126
127 for (count = 0; count < i; count++)
128 (*attach[count]) (port);
129
130 kfree (attach);
131 }
132
133 /* Call detach(port) for each registered driver. */
134 static void detach_driver_chain(struct parport *port)
135 {
136 struct parport_driver *drv;
137
138 spin_lock (&driverlist_lock);
139 for (drv = driver_chain; drv; drv = drv->next)
140 drv->detach (port);
141 spin_unlock (&driverlist_lock);
142 }
143
144 /* Ask kmod for some lowlevel drivers. */
145 static void get_lowlevel_driver (void)
146 {
147 /* There is no actual module called this: you should set
148 * up an alias for modutils. */
149 request_module ("parport_lowlevel");
150 }
151
152 /**
153 * parport_register_driver - register a parallel port device driver
154 * @drv: structure describing the driver
155 *
156 * This can be called by a parallel port device driver in order
157 * to receive notifications about ports being found in the
158 * system, as well as ports no longer available.
159 *
160 * The @drv structure is allocated by the caller and must not be
161 * deallocated until after calling parport_unregister_driver().
162 *
163 * The driver's attach() function may block. The port that
164 * attach() is given will be valid for the duration of the
165 * callback, but if the driver wants to take a copy of the
166 * pointer it must call parport_get_port() to do so. Calling
167 * parport_register_device() on that port will do this for you.
168 *
169 * The driver's detach() function may not block. The port that
170 * detach() is given will be valid for the duration of the
171 * callback, but if the driver wants to take a copy of the
172 * pointer it must call parport_get_port() to do so.
173 *
174 * Returns 0 on success. Currently it always succeeds.
175 **/
176
177 int parport_register_driver (struct parport_driver *drv)
178 {
179 struct parport *port;
180 struct parport **ports;
181 int count = 0, i;
182
183 if (!portlist)
184 get_lowlevel_driver ();
185
186 /* We have to take the portlist lock for this to be sure
187 * that port is valid for the duration of the callback. */
188
189 /* This is complicated by the fact that attach must be allowed
190 * to block, so we can't be holding any spinlocks when we call
191 * it. But we need to hold a spinlock to iterate over the
192 * list of ports.. */
193
194 spin_lock (&parportlist_lock);
195 for (port = portlist; port; port = port->next)
196 count++;
197 spin_unlock (&parportlist_lock);
198
199 ports = kmalloc (sizeof (struct parport *) * count, GFP_KERNEL);
200 if (!ports)
201 printk (KERN_WARNING "parport: not enough memory to attach\n");
202 else {
203 spin_lock (&parportlist_lock);
204 for (i = 0, port = portlist; port && i < count;
205 port = port->next)
206 ports[i++] = port;
207 spin_unlock (&parportlist_lock);
208
209 for (count = 0; count < i; count++)
210 drv->attach (ports[count]);
211
212 kfree (ports);
213 }
214
215 spin_lock (&driverlist_lock);
216 drv->next = driver_chain;
217 driver_chain = drv;
218 spin_unlock (&driverlist_lock);
219
220 return 0;
221 }
222
223 /**
224 * parport_unregister_driver - deregister a parallel port device driver
225 * @arg: structure describing the driver that was given to
226 * parport_register_driver()
227 *
228 * This should be called by a parallel port device driver that
229 * has registered itself using parport_register_driver() when it
230 * is about to be unloaded.
231 *
232 * When it returns, the driver's attach() routine will no longer
233 * be called, and for each port that attach() was called for, the
234 * detach() routine will have been called.
235 *
236 * If the caller's attach() function can block, it is their
237 * responsibility to make sure to wait for it to exit before
238 * unloading.
239 *
240 * All the driver's detach() calls are guaranteed to have
241 * finished by the time this function returns.
242 *
243 * The driver's detach() call is not allowed to block.
244 **/
245
246 void parport_unregister_driver (struct parport_driver *arg)
247 {
248 struct parport_driver *drv = driver_chain, *olddrv = NULL;
249
250 while (drv) {
251 if (drv == arg) {
252 struct parport *port;
253
254 spin_lock (&driverlist_lock);
255 if (olddrv)
256 olddrv->next = drv->next;
257 else
258 driver_chain = drv->next;
259 spin_unlock (&driverlist_lock);
260
261 /* Call the driver's detach routine for each
262 * port to clean up any resources that the
263 * attach routine acquired. */
264 spin_lock (&parportlist_lock);
265 for (port = portlist; port; port = port->next)
266 drv->detach (port);
267 spin_unlock (&parportlist_lock);
268
269 return;
270 }
271 olddrv = drv;
272 drv = drv->next;
273 }
274 }
275
276 static void free_port (struct parport *port)
277 {
278 int d;
279 for (d = 0; d < 5; d++) {
280 if (port->probe_info[d].class_name)
281 kfree (port->probe_info[d].class_name);
282 if (port->probe_info[d].mfr)
283 kfree (port->probe_info[d].mfr);
284 if (port->probe_info[d].model)
285 kfree (port->probe_info[d].model);
286 if (port->probe_info[d].cmdset)
287 kfree (port->probe_info[d].cmdset);
288 if (port->probe_info[d].description)
289 kfree (port->probe_info[d].description);
290 }
291
292 kfree(port->name);
293 kfree(port);
294 }
295
296 /**
297 * parport_get_port - increment a port's reference count
298 * @port: the port
299 *
300 * This ensure's that a struct parport pointer remains valid
301 * until the matching parport_put_port() call.
302 **/
303
304 struct parport *parport_get_port (struct parport *port)
305 {
306 atomic_inc (&port->ref_count);
307 return port;
308 }
309
310 /**
311 * parport_put_port - decrement a port's reference count
312 * @port: the port
313 *
314 * This should be called once for each call to parport_get_port(),
315 * once the port is no longer needed.
316 **/
317
318 void parport_put_port (struct parport *port)
319 {
320 if (atomic_dec_and_test (&port->ref_count))
321 /* Can destroy it now. */
322 free_port (port);
323
324 return;
325 }
326
327 /**
328 * parport_enumerate - return a list of the system's parallel ports
329 *
330 * This returns the head of the list of parallel ports in the
331 * system, as a &struct parport. The structure that is returned
332 * describes the first port in the list, and its 'next' member
333 * points to the next port, or %NULL if it's the last port.
334 *
335 * If there are no parallel ports in the system,
336 * parport_enumerate() will return %NULL.
337 **/
338
339 struct parport *parport_enumerate(void)
340 {
341 /* Don't use this: use parport_register_driver instead. */
342
343 if (!portlist)
344 get_lowlevel_driver ();
345
346 return portlist;
347 }
348
349 /**
350 * parport_register_port - register a parallel port
351 * @base: base I/O address
352 * @irq: IRQ line
353 * @dma: DMA channel
354 * @ops: pointer to the port driver's port operations structure
355 *
356 * When a parallel port (lowlevel) driver finds a port that
357 * should be made available to parallel port device drivers, it
358 * should call parport_register_port(). The @base, @irq, and
359 * @dma parameters are for the convenience of port drivers, and
360 * for ports where they aren't meaningful needn't be set to
361 * anything special. They can be altered afterwards by adjusting
362 * the relevant members of the parport structure that is returned
363 * and represents the port. They should not be tampered with
364 * after calling parport_announce_port, however.
365 *
366 * If there are parallel port device drivers in the system that
367 * have registered themselves using parport_register_driver(),
368 * they are not told about the port at this time; that is done by
369 * parport_announce_port().
370 *
371 * The @ops structure is allocated by the caller, and must not be
372 * deallocated before calling parport_unregister_port().
373 *
374 * If there is no memory to allocate a new parport structure,
375 * this function will return %NULL.
376 **/
377
378 struct parport *parport_register_port(unsigned long base, int irq, int dma,
379 struct parport_operations *ops)
380 {
381 struct parport *tmp;
382 int portnum;
383 int device;
384 char *name;
385
386 tmp = kmalloc(sizeof(struct parport), GFP_KERNEL);
387 if (!tmp) {
388 printk(KERN_WARNING "parport: memory squeeze\n");
389 return NULL;
390 }
391
392 /* Search for the lowest free parport number. */
393
394 spin_lock_irq (&parportlist_lock);
395 for (portnum = 0; ; portnum++) {
396 struct parport *itr = portlist;
397 while (itr) {
398 if (itr->number == portnum)
399 /* No good, already used. */
400 break;
401 else
402 itr = itr->next;
403 }
404
405 if (itr == NULL)
406 /* Got to the end of the list. */
407 break;
408 }
409 spin_unlock_irq (&parportlist_lock);
410
411 /* Init our structure */
412 memset(tmp, 0, sizeof(struct parport));
413 tmp->base = base;
414 tmp->irq = irq;
415 tmp->dma = dma;
416 tmp->muxport = tmp->daisy = tmp->muxsel = -1;
417 tmp->modes = 0;
418 tmp->next = NULL;
419 tmp->devices = tmp->cad = NULL;
420 tmp->flags = 0;
421 tmp->ops = ops;
422 tmp->portnum = tmp->number = portnum;
423 tmp->physport = tmp;
424 memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info));
425 tmp->cad_lock = RW_LOCK_UNLOCKED;
426 spin_lock_init(&tmp->waitlist_lock);
427 spin_lock_init(&tmp->pardevice_lock);
428 tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
429 tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
430 init_MUTEX_LOCKED (&tmp->ieee1284.irq); /* actually a semaphore at 0 */
431 tmp->spintime = parport_default_spintime;
432 atomic_set (&tmp->ref_count, 1);
433
434 name = kmalloc(15, GFP_KERNEL);
435 if (!name) {
436 printk(KERN_ERR "parport: memory squeeze\n");
437 kfree(tmp);
438 return NULL;
439 }
440 sprintf(name, "parport%d", portnum);
441 tmp->name = name;
442
443 /*
444 * Chain the entry to our list.
445 *
446 * This function must not run from an irq handler so we don' t need
447 * to clear irq on the local CPU. -arca
448 */
449
450 spin_lock(&parportlist_lock);
451
452 /* We are locked against anyone else performing alterations, but
453 * because of parport_enumerate people can still _read_ the list
454 * while we are changing it; so be careful..
455 *
456 * It's okay to have portlist_tail a little bit out of sync
457 * since it's only used for changing the list, not for reading
458 * from it.
459 */
460
461 if (portlist_tail)
462 portlist_tail->next = tmp;
463 portlist_tail = tmp;
464 if (!portlist)
465 portlist = tmp;
466 spin_unlock(&parportlist_lock);
467
468 for (device = 0; device < 5; device++)
469 /* assume the worst */
470 tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
471
472 tmp->waithead = tmp->waittail = NULL;
473
474 return tmp;
475 }
476
477 /**
478 * parport_announce_port - tell device drivers about a parallel port
479 * @port: parallel port to announce
480 *
481 * After a port driver has registered a parallel port with
482 * parport_register_port, and performed any necessary
483 * initialisation or adjustments, it should call
484 * parport_announce_port() in order to notify all device drivers
485 * that have called parport_register_driver(). Their attach()
486 * functions will be called, with @port as the parameter.
487 **/
488
489 void parport_announce_port (struct parport *port)
490 {
491 #ifdef CONFIG_PARPORT_1284
492 /* Analyse the IEEE1284.3 topology of the port. */
493 if (parport_daisy_init (port) == 0) {
494 /* No devices were detected. Perhaps they are in some
495 funny state; let's try to reset them and see if
496 they wake up. */
497 parport_daisy_fini (port);
498 parport_write_control (port, PARPORT_CONTROL_SELECT);
499 udelay (50);
500 parport_write_control (port,
501 PARPORT_CONTROL_SELECT |
502 PARPORT_CONTROL_INIT);
503 udelay (50);
504 parport_daisy_init (port);
505 }
506 #endif
507
508 /* Let drivers know that a new port has arrived. */
509 attach_driver_chain (port);
510 }
511
512 /**
513 * parport_unregister_port - deregister a parallel port
514 * @port: parallel port to deregister
515 *
516 * When a parallel port driver is forcibly unloaded, or a
517 * parallel port becomes inaccessible, the port driver must call
518 * this function in order to deal with device drivers that still
519 * want to use it.
520 *
521 * The parport structure associated with the port has its
522 * operations structure replaced with one containing 'null'
523 * operations that return errors or just don't do anything.
524 *
525 * Any drivers that have registered themselves using
526 * parport_register_driver() are notified that the port is no
527 * longer accessible by having their detach() routines called
528 * with @port as the parameter.
529 **/
530
531 void parport_unregister_port(struct parport *port)
532 {
533 struct parport *p;
534
535 port->ops = &dead_ops;
536
537 /* Spread the word. */
538 detach_driver_chain (port);
539
540 #ifdef CONFIG_PARPORT_1284
541 /* Forget the IEEE1284.3 topology of the port. */
542 parport_daisy_fini (port);
543 #endif
544
545 spin_lock(&parportlist_lock);
546
547 /* We are protected from other people changing the list, but
548 * they can still see it (using parport_enumerate). So be
549 * careful about the order of writes.. */
550 if (portlist == port) {
551 if ((portlist = port->next) == NULL)
552 portlist_tail = NULL;
553 } else {
554 for (p = portlist; (p != NULL) && (p->next != port);
555 p=p->next);
556 if (p) {
557 if ((p->next = port->next) == NULL)
558 portlist_tail = p;
559 }
560 else printk (KERN_WARNING
561 "%s not found in port list!\n", port->name);
562 }
563 spin_unlock(&parportlist_lock);
564
565 /* Yes, parport_enumerate _is_ unsafe. Don't use it. */
566 parport_put_port (port);
567 }
568
569 /**
570 * parport_register_device - register a device on a parallel port
571 * @port: port to which the device is attached
572 * @name: a name to refer to the device
573 * @pf: preemption callback
574 * @kf: kick callback (wake-up)
575 * @irq_func: interrupt handler
576 * @flags: registration flags
577 * @handle: data for callback functions
578 *
579 * This function, called by parallel port device drivers,
580 * declares that a device is connected to a port, and tells the
581 * system all it needs to know.
582 *
583 * The @name is allocated by the caller and must not be
584 * deallocated until the caller calls @parport_unregister_device
585 * for that device.
586 *
587 * The preemption callback function, @pf, is called when this
588 * device driver has claimed access to the port but another
589 * device driver wants to use it. It is given @handle as its
590 * parameter, and should return zero if it is willing for the
591 * system to release the port to another driver on its behalf.
592 * If it wants to keep control of the port it should return
593 * non-zero, and no action will be taken. It is good manners for
594 * the driver to try to release the port at the earliest
595 * opportunity after its preemption callback rejects a preemption
596 * attempt. Note that if a preemption callback is happy for
597 * preemption to go ahead, there is no need to release the port;
598 * it is done automatically. This function may not block, as it
599 * may be called from interrupt context. If the device driver
600 * does not support preemption, @pf can be %NULL.
601 *
602 * The wake-up ("kick") callback function, @kf, is called when
603 * the port is available to be claimed for exclusive access; that
604 * is, parport_claim() is guaranteed to succeed when called from
605 * inside the wake-up callback function. If the driver wants to
606 * claim the port it should do so; otherwise, it need not take
607 * any action. This function may not block, as it may be called
608 * from interrupt context. If the device driver does not want to
609 * be explicitly invited to claim the port in this way, @kf can
610 * be %NULL.
611 *
612 * The interrupt handler, @irq_func, is called when an interrupt
613 * arrives from the parallel port. Note that if a device driver
614 * wants to use interrupts it should use parport_enable_irq(),
615 * and can also check the irq member of the parport structure
616 * representing the port.
617 *
618 * The parallel port (lowlevel) driver is the one that has called
619 * request_irq() and whose interrupt handler is called first.
620 * This handler does whatever needs to be done to the hardware to
621 * acknowledge the interrupt (for PC-style ports there is nothing
622 * special to be done). It then tells the IEEE 1284 code about
623 * the interrupt, which may involve reacting to an IEEE 1284
624 * event depending on the current IEEE 1284 phase. After this,
625 * it calls @irq_func. Needless to say, @irq_func will be called
626 * from interrupt context, and may not block.
627 *
628 * The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
629 * so should only be used when sharing the port with other device
630 * drivers is impossible and would lead to incorrect behaviour.
631 * Use it sparingly! Normally, @flags will be zero.
632 *
633 * This function returns a pointer to a structure that represents
634 * the device on the port, or %NULL if there is not enough memory
635 * to allocate space for that structure.
636 **/
637
638 struct pardevice *
639 parport_register_device(struct parport *port, const char *name,
640 int (*pf)(void *), void (*kf)(void *),
641 void (*irq_func)(int, void *, struct pt_regs *),
642 int flags, void *handle)
643 {
644 struct pardevice *tmp;
645
646 if (port->physport->flags & PARPORT_FLAG_EXCL) {
647 /* An exclusive device is registered. */
648 printk (KERN_DEBUG "%s: no more devices allowed\n",
649 port->name);
650 return NULL;
651 }
652
653 if (flags & PARPORT_DEV_LURK) {
654 if (!pf || !kf) {
655 printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
656 return NULL;
657 }
658 }
659
660 /* We up our own module reference count, and that of the port
661 on which a device is to be registered, to ensure that
662 neither of us gets unloaded while we sleep in (e.g.)
663 kmalloc. To be absolutely safe, we have to require that
664 our caller doesn't sleep in between parport_enumerate and
665 parport_register_device.. */
666 inc_parport_count();
667 port->ops->inc_use_count();
668 parport_get_port (port);
669
670 tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
671 if (tmp == NULL) {
672 printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
673 goto out;
674 }
675
676 tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
677 if (tmp->state == NULL) {
678 printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
679 goto out_free_pardevice;
680 }
681
682 tmp->name = name;
683 tmp->port = port;
684 tmp->daisy = -1;
685 tmp->preempt = pf;
686 tmp->wakeup = kf;
687 tmp->private = handle;
688 tmp->flags = flags;
689 tmp->irq_func = irq_func;
690 tmp->waiting = 0;
691 tmp->timeout = 5 * HZ;
692
693 /* Chain this onto the list */
694 tmp->prev = NULL;
695 /*
696 * This function must not run from an irq handler so we don' t need
697 * to clear irq on the local CPU. -arca
698 */
699 spin_lock(&port->physport->pardevice_lock);
700
701 if (flags & PARPORT_DEV_EXCL) {
702 if (port->physport->devices) {
703 spin_unlock (&port->physport->pardevice_lock);
704 printk (KERN_DEBUG
705 "%s: cannot grant exclusive access for "
706 "device %s\n", port->name, name);
707 goto out_free_all;
708 }
709 port->flags |= PARPORT_FLAG_EXCL;
710 }
711
712 tmp->next = port->physport->devices;
713 wmb(); /* Make sure that tmp->next is written before it's
714 added to the list; see comments marked 'no locking
715 required' */
716 if (port->physport->devices)
717 port->physport->devices->prev = tmp;
718 port->physport->devices = tmp;
719 spin_unlock(&port->physport->pardevice_lock);
720
721 init_waitqueue_head(&tmp->wait_q);
722 tmp->timeslice = parport_default_timeslice;
723 tmp->waitnext = tmp->waitprev = NULL;
724
725 /*
726 * This has to be run as last thing since init_state may need other
727 * pardevice fields. -arca
728 */
729 port->ops->init_state(tmp, tmp->state);
730 parport_device_proc_register(tmp);
731 return tmp;
732
733 out_free_all:
734 kfree (tmp->state);
735 out_free_pardevice:
736 kfree (tmp);
737 out:
738 dec_parport_count();
739 port->ops->dec_use_count();
740 parport_put_port (port);
741 return NULL;
742 }
743
744 /**
745 * parport_unregister_device - deregister a device on a parallel port
746 * @dev: pointer to structure representing device
747 *
748 * This undoes the effect of parport_register_device().
749 **/
750
751 void parport_unregister_device(struct pardevice *dev)
752 {
753 struct parport *port;
754
755 #ifdef PARPORT_PARANOID
756 if (dev == NULL) {
757 printk(KERN_ERR "parport_unregister_device: passed NULL\n");
758 return;
759 }
760 #endif
761
762 parport_device_proc_unregister(dev);
763
764 port = dev->port->physport;
765
766 if (port->cad == dev) {
767 printk(KERN_DEBUG "%s: %s forgot to release port\n",
768 port->name, dev->name);
769 parport_release (dev);
770 }
771
772 spin_lock(&port->pardevice_lock);
773 if (dev->next)
774 dev->next->prev = dev->prev;
775 if (dev->prev)
776 dev->prev->next = dev->next;
777 else
778 port->devices = dev->next;
779
780 if (dev->flags & PARPORT_DEV_EXCL)
781 port->flags &= ~PARPORT_FLAG_EXCL;
782
783 spin_unlock(&port->pardevice_lock);
784
785 kfree(dev->state);
786 kfree(dev);
787
788 dec_parport_count();
789 port->ops->dec_use_count();
790 parport_put_port (port);
791
792 /* Yes, that's right, someone _could_ still have a pointer to
793 * port, if they used parport_enumerate. That's why they
794 * shouldn't use it (and use parport_register_driver instead)..
795 */
796 }
797
798 /**
799 * parport_find_number - find a parallel port by number
800 * @number: parallel port number
801 *
802 * This returns the parallel port with the specified number, or
803 * %NULL if there is none.
804 *
805 * There is an implicit parport_get_port() done already; to throw
806 * away the reference to the port that parport_find_number()
807 * gives you, use parport_put_port().
808 */
809
810 struct parport *parport_find_number (int number)
811 {
812 struct parport *port, *result = NULL;
813 spin_lock (&parportlist_lock);
814 for (port = portlist; port; port = port->next)
815 if (port->number == number) {
816 result = parport_get_port (port);
817 break;
818 }
819 spin_unlock (&parportlist_lock);
820 return result;
821 }
822
823 /**
824 * parport_find_base - find a parallel port by base address
825 * @base: base I/O address
826 *
827 * This returns the parallel port with the specified base
828 * address, or %NULL if there is none.
829 *
830 * There is an implicit parport_get_port() done already; to throw
831 * away the reference to the port that parport_find_base()
832 * gives you, use parport_put_port().
833 */
834
835 struct parport *parport_find_base (unsigned long base)
836 {
837 struct parport *port, *result = NULL;
838 spin_lock (&parportlist_lock);
839 for (port = portlist; port; port = port->next)
840 if (port->base == base) {
841 result = parport_get_port (port);
842 break;
843 }
844 spin_unlock (&parportlist_lock);
845 return result;
846 }
847
848 /**
849 * parport_claim - claim access to a parallel port device
850 * @dev: pointer to structure representing a device on the port
851 *
852 * This function will not block and so can be used from interrupt
853 * context. If parport_claim() succeeds in claiming access to
854 * the port it returns zero and the port is available to use. It
855 * may fail (returning non-zero) if the port is in use by another
856 * driver and that driver is not willing to relinquish control of
857 * the port.
858 **/
859
860 int parport_claim(struct pardevice *dev)
861 {
862 struct pardevice *oldcad;
863 struct parport *port = dev->port->physport;
864 unsigned long flags;
865
866 if (port->cad == dev) {
867 printk(KERN_INFO "%s: %s already owner\n",
868 dev->port->name,dev->name);
869 return 0;
870 }
871
872 /* Preempt any current device */
873 write_lock_irqsave (&port->cad_lock, flags);
874 if ((oldcad = port->cad) != NULL) {
875 if (oldcad->preempt) {
876 if (oldcad->preempt(oldcad->private))
877 goto blocked;
878 port->ops->save_state(port, dev->state);
879 } else
880 goto blocked;
881
882 if (port->cad != oldcad) {
883 /* I think we'll actually deadlock rather than
884 get here, but just in case.. */
885 printk(KERN_WARNING
886 "%s: %s released port when preempted!\n",
887 port->name, oldcad->name);
888 if (port->cad)
889 goto blocked;
890 }
891 }
892
893 /* Can't fail from now on, so mark ourselves as no longer waiting. */
894 if (dev->waiting & 1) {
895 dev->waiting = 0;
896
897 /* Take ourselves out of the wait list again. */
898 spin_lock_irq (&port->waitlist_lock);
899 if (dev->waitprev)
900 dev->waitprev->waitnext = dev->waitnext;
901 else
902 port->waithead = dev->waitnext;
903 if (dev->waitnext)
904 dev->waitnext->waitprev = dev->waitprev;
905 else
906 port->waittail = dev->waitprev;
907 spin_unlock_irq (&port->waitlist_lock);
908 dev->waitprev = dev->waitnext = NULL;
909 }
910
911 /* Now we do the change of devices */
912 port->cad = dev;
913
914 #ifdef CONFIG_PARPORT_1284
915 /* If it's a mux port, select it. */
916 if (dev->port->muxport >= 0) {
917 /* FIXME */
918 port->muxsel = dev->port->muxport;
919 }
920
921 /* If it's a daisy chain device, select it. */
922 if (dev->daisy >= 0) {
923 /* This could be lazier. */
924 if (!parport_daisy_select (port, dev->daisy,
925 IEEE1284_MODE_COMPAT))
926 port->daisy = dev->daisy;
927 }
928 #endif /* IEEE1284.3 support */
929
930 /* Restore control registers */
931 port->ops->restore_state(port, dev->state);
932 write_unlock_irqrestore(&port->cad_lock, flags);
933 dev->time = jiffies;
934 return 0;
935
936 blocked:
937 /* If this is the first time we tried to claim the port, register an
938 interest. This is only allowed for devices sleeping in
939 parport_claim_or_block(), or those with a wakeup function. */
940
941 /* The cad_lock is still held for writing here */
942 if (dev->waiting & 2 || dev->wakeup) {
943 spin_lock (&port->waitlist_lock);
944 if (test_and_set_bit(0, &dev->waiting) == 0) {
945 /* First add ourselves to the end of the wait list. */
946 dev->waitnext = NULL;
947 dev->waitprev = port->waittail;
948 if (port->waittail) {
949 port->waittail->waitnext = dev;
950 port->waittail = dev;
951 } else
952 port->waithead = port->waittail = dev;
953 }
954 spin_unlock (&port->waitlist_lock);
955 }
956 write_unlock_irqrestore (&port->cad_lock, flags);
957 return -EAGAIN;
958 }
959
960 /**
961 * parport_claim_or_block - claim access to a parallel port device
962 * @dev: pointer to structure representing a device on the port
963 *
964 * This behaves like parport_claim(), but will block if necessary
965 * to wait for the port to be free. A return value of 1
966 * indicates that it slept; 0 means that it succeeded without
967 * needing to sleep. A negative error code indicates failure.
968 **/
969
970 int parport_claim_or_block(struct pardevice *dev)
971 {
972 int r;
973
974 /* Signal to parport_claim() that we can wait even without a
975 wakeup function. */
976 dev->waiting = 2;
977
978 /* Try to claim the port. If this fails, we need to sleep. */
979 r = parport_claim(dev);
980 if (r == -EAGAIN) {
981 unsigned long flags;
982 #ifdef PARPORT_DEBUG_SHARING
983 printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
984 #endif
985 save_flags (flags);
986 cli();
987 /* If dev->waiting is clear now, an interrupt
988 gave us the port and we would deadlock if we slept. */
989 if (dev->waiting) {
990 sleep_on(&dev->wait_q);
991 r = 1;
992 } else {
993 r = 0;
994 #ifdef PARPORT_DEBUG_SHARING
995 printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
996 dev->name);
997 #endif
998 }
999 restore_flags(flags);
1000 #ifdef PARPORT_DEBUG_SHARING
1001 if (dev->port->physport->cad != dev)
1002 printk(KERN_DEBUG "%s: exiting parport_claim_or_block "
1003 "but %s owns port!\n", dev->name,
1004 dev->port->physport->cad ?
1005 dev->port->physport->cad->name:"nobody");
1006 #endif
1007 }
1008 dev->waiting = 0;
1009 return r;
1010 }
1011
1012 /**
1013 * parport_release - give up access to a parallel port device
1014 * @dev: pointer to structure representing parallel port device
1015 *
1016 * This function cannot fail, but it should not be called without
1017 * the port claimed. Similarly, if the port is already claimed
1018 * you should not try claiming it again.
1019 **/
1020
1021 void parport_release(struct pardevice *dev)
1022 {
1023 struct parport *port = dev->port->physport;
1024 struct pardevice *pd;
1025 unsigned long flags;
1026
1027 /* Make sure that dev is the current device */
1028 write_lock_irqsave(&port->cad_lock, flags);
1029 if (port->cad != dev) {
1030 write_unlock_irqrestore (&port->cad_lock, flags);
1031 printk(KERN_WARNING "%s: %s tried to release parport "
1032 "when not owner\n", port->name, dev->name);
1033 return;
1034 }
1035
1036 #ifdef CONFIG_PARPORT_1284
1037 /* If this is on a mux port, deselect it. */
1038 if (dev->port->muxport >= 0) {
1039 /* FIXME */
1040 port->muxsel = -1;
1041 }
1042
1043 /* If this is a daisy device, deselect it. */
1044 if (dev->daisy >= 0) {
1045 parport_daisy_deselect_all (port);
1046 port->daisy = -1;
1047 }
1048 #endif
1049
1050 port->cad = NULL;
1051 write_unlock_irqrestore(&port->cad_lock, flags);
1052
1053 /* Save control registers */
1054 port->ops->save_state(port, dev->state);
1055
1056 /* If anybody is waiting, find out who's been there longest and
1057 then wake them up. (Note: no locking required) */
1058 for (pd = port->waithead; pd; pd = pd->waitnext) {
1059 if (pd->waiting & 2) { /* sleeping in claim_or_block */
1060 parport_claim(pd);
1061 if (waitqueue_active(&pd->wait_q))
1062 wake_up(&pd->wait_q);
1063 return;
1064 } else if (pd->wakeup) {
1065 pd->wakeup(pd->private);
1066 if (dev->port->cad) /* racy but no matter */
1067 return;
1068 } else {
1069 printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
1070 }
1071 }
1072
1073 /* Nobody was waiting, so walk the list to see if anyone is
1074 interested in being woken up. (Note: no locking required) */
1075 for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) {
1076 if (pd->wakeup && pd != dev)
1077 pd->wakeup(pd->private);
1078 }
1079 }
1080
1081 static int parport_parse_params (int nports, const char *str[], int val[],
1082 int automatic, int none, int nofifo)
1083 {
1084 unsigned int i;
1085 for (i = 0; i < nports && str[i]; i++) {
1086 if (!strncmp(str[i], "auto", 4))
1087 val[i] = automatic;
1088 else if (!strncmp(str[i], "none", 4))
1089 val[i] = none;
1090 else if (nofifo && !strncmp(str[i], "nofifo", 4))
1091 val[i] = nofifo;
1092 else {
1093 char *ep;
1094 unsigned long r = simple_strtoul(str[i], &ep, 0);
1095 if (ep != str[i])
1096 val[i] = r;
1097 else {
1098 printk("parport: bad specifier `%s'\n", str[i]);
1099 return -1;
1100 }
1101 }
1102 }
1103
1104 return 0;
1105 }
1106
1107 int parport_parse_irqs(int nports, const char *irqstr[], int irqval[])
1108 {
1109 return parport_parse_params (nports, irqstr, irqval, PARPORT_IRQ_AUTO,
1110 PARPORT_IRQ_NONE, 0);
1111 }
1112
1113 int parport_parse_dmas(int nports, const char *dmastr[], int dmaval[])
1114 {
1115 return parport_parse_params (nports, dmastr, dmaval, PARPORT_DMA_AUTO,
1116 PARPORT_DMA_NONE, PARPORT_DMA_NOFIFO);
1117 }
1118
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.