File: /usr/src/linux/drivers/parport/share.c
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/slab.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 /* Make sure we haven't left any pointers around in the wait
786 * list. */
787 spin_lock (&port->waitlist_lock);
788 if (dev->waitprev || dev->waitnext || port->waithead == dev) {
789 if (dev->waitprev)
790 dev->waitprev->waitnext = dev->waitnext;
791 else
792 port->waithead = dev->waitnext;
793 if (dev->waitnext)
794 dev->waitnext->waitprev = dev->waitprev;
795 else
796 port->waittail = dev->waitprev;
797 }
798 spin_unlock (&port->waitlist_lock);
799
800 kfree(dev->state);
801 kfree(dev);
802
803 dec_parport_count();
804 port->ops->dec_use_count();
805 parport_put_port (port);
806
807 /* Yes, that's right, someone _could_ still have a pointer to
808 * port, if they used parport_enumerate. That's why they
809 * shouldn't use it (and use parport_register_driver instead)..
810 */
811 }
812
813 /**
814 * parport_find_number - find a parallel port by number
815 * @number: parallel port number
816 *
817 * This returns the parallel port with the specified number, or
818 * %NULL if there is none.
819 *
820 * There is an implicit parport_get_port() done already; to throw
821 * away the reference to the port that parport_find_number()
822 * gives you, use parport_put_port().
823 */
824
825 struct parport *parport_find_number (int number)
826 {
827 struct parport *port, *result = NULL;
828
829 if (!portlist)
830 get_lowlevel_driver ();
831
832 spin_lock (&parportlist_lock);
833 for (port = portlist; port; port = port->next)
834 if (port->number == number) {
835 result = parport_get_port (port);
836 break;
837 }
838 spin_unlock (&parportlist_lock);
839 return result;
840 }
841
842 /**
843 * parport_find_base - find a parallel port by base address
844 * @base: base I/O address
845 *
846 * This returns the parallel port with the specified base
847 * address, or %NULL if there is none.
848 *
849 * There is an implicit parport_get_port() done already; to throw
850 * away the reference to the port that parport_find_base()
851 * gives you, use parport_put_port().
852 */
853
854 struct parport *parport_find_base (unsigned long base)
855 {
856 struct parport *port, *result = NULL;
857
858 if (!portlist)
859 get_lowlevel_driver ();
860
861 spin_lock (&parportlist_lock);
862 for (port = portlist; port; port = port->next)
863 if (port->base == base) {
864 result = parport_get_port (port);
865 break;
866 }
867 spin_unlock (&parportlist_lock);
868 return result;
869 }
870
871 /**
872 * parport_claim - claim access to a parallel port device
873 * @dev: pointer to structure representing a device on the port
874 *
875 * This function will not block and so can be used from interrupt
876 * context. If parport_claim() succeeds in claiming access to
877 * the port it returns zero and the port is available to use. It
878 * may fail (returning non-zero) if the port is in use by another
879 * driver and that driver is not willing to relinquish control of
880 * the port.
881 **/
882
883 int parport_claim(struct pardevice *dev)
884 {
885 struct pardevice *oldcad;
886 struct parport *port = dev->port->physport;
887 unsigned long flags;
888
889 if (port->cad == dev) {
890 printk(KERN_INFO "%s: %s already owner\n",
891 dev->port->name,dev->name);
892 return 0;
893 }
894
895 /* Preempt any current device */
896 write_lock_irqsave (&port->cad_lock, flags);
897 if ((oldcad = port->cad) != NULL) {
898 if (oldcad->preempt) {
899 if (oldcad->preempt(oldcad->private))
900 goto blocked;
901 port->ops->save_state(port, dev->state);
902 } else
903 goto blocked;
904
905 if (port->cad != oldcad) {
906 /* I think we'll actually deadlock rather than
907 get here, but just in case.. */
908 printk(KERN_WARNING
909 "%s: %s released port when preempted!\n",
910 port->name, oldcad->name);
911 if (port->cad)
912 goto blocked;
913 }
914 }
915
916 /* Can't fail from now on, so mark ourselves as no longer waiting. */
917 if (dev->waiting & 1) {
918 dev->waiting = 0;
919
920 /* Take ourselves out of the wait list again. */
921 spin_lock_irq (&port->waitlist_lock);
922 if (dev->waitprev)
923 dev->waitprev->waitnext = dev->waitnext;
924 else
925 port->waithead = dev->waitnext;
926 if (dev->waitnext)
927 dev->waitnext->waitprev = dev->waitprev;
928 else
929 port->waittail = dev->waitprev;
930 spin_unlock_irq (&port->waitlist_lock);
931 dev->waitprev = dev->waitnext = NULL;
932 }
933
934 /* Now we do the change of devices */
935 port->cad = dev;
936
937 #ifdef CONFIG_PARPORT_1284
938 /* If it's a mux port, select it. */
939 if (dev->port->muxport >= 0) {
940 /* FIXME */
941 port->muxsel = dev->port->muxport;
942 }
943
944 /* If it's a daisy chain device, select it. */
945 if (dev->daisy >= 0) {
946 /* This could be lazier. */
947 if (!parport_daisy_select (port, dev->daisy,
948 IEEE1284_MODE_COMPAT))
949 port->daisy = dev->daisy;
950 }
951 #endif /* IEEE1284.3 support */
952
953 /* Restore control registers */
954 port->ops->restore_state(port, dev->state);
955 write_unlock_irqrestore(&port->cad_lock, flags);
956 dev->time = jiffies;
957 return 0;
958
959 blocked:
960 /* If this is the first time we tried to claim the port, register an
961 interest. This is only allowed for devices sleeping in
962 parport_claim_or_block(), or those with a wakeup function. */
963
964 /* The cad_lock is still held for writing here */
965 if (dev->waiting & 2 || dev->wakeup) {
966 spin_lock (&port->waitlist_lock);
967 if (test_and_set_bit(0, &dev->waiting) == 0) {
968 /* First add ourselves to the end of the wait list. */
969 dev->waitnext = NULL;
970 dev->waitprev = port->waittail;
971 if (port->waittail) {
972 port->waittail->waitnext = dev;
973 port->waittail = dev;
974 } else
975 port->waithead = port->waittail = dev;
976 }
977 spin_unlock (&port->waitlist_lock);
978 }
979 write_unlock_irqrestore (&port->cad_lock, flags);
980 return -EAGAIN;
981 }
982
983 /**
984 * parport_claim_or_block - claim access to a parallel port device
985 * @dev: pointer to structure representing a device on the port
986 *
987 * This behaves like parport_claim(), but will block if necessary
988 * to wait for the port to be free. A return value of 1
989 * indicates that it slept; 0 means that it succeeded without
990 * needing to sleep. A negative error code indicates failure.
991 **/
992
993 int parport_claim_or_block(struct pardevice *dev)
994 {
995 int r;
996
997 /* Signal to parport_claim() that we can wait even without a
998 wakeup function. */
999 dev->waiting = 2;
1000
1001 /* Try to claim the port. If this fails, we need to sleep. */
1002 r = parport_claim(dev);
1003 if (r == -EAGAIN) {
1004 unsigned long flags;
1005 #ifdef PARPORT_DEBUG_SHARING
1006 printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
1007 #endif
1008 save_flags (flags);
1009 cli();
1010 /* If dev->waiting is clear now, an interrupt
1011 gave us the port and we would deadlock if we slept. */
1012 if (dev->waiting) {
1013 sleep_on(&dev->wait_q);
1014 r = 1;
1015 } else {
1016 r = 0;
1017 #ifdef PARPORT_DEBUG_SHARING
1018 printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
1019 dev->name);
1020 #endif
1021 }
1022 restore_flags(flags);
1023 #ifdef PARPORT_DEBUG_SHARING
1024 if (dev->port->physport->cad != dev)
1025 printk(KERN_DEBUG "%s: exiting parport_claim_or_block "
1026 "but %s owns port!\n", dev->name,
1027 dev->port->physport->cad ?
1028 dev->port->physport->cad->name:"nobody");
1029 #endif
1030 }
1031 dev->waiting = 0;
1032 return r;
1033 }
1034
1035 /**
1036 * parport_release - give up access to a parallel port device
1037 * @dev: pointer to structure representing parallel port device
1038 *
1039 * This function cannot fail, but it should not be called without
1040 * the port claimed. Similarly, if the port is already claimed
1041 * you should not try claiming it again.
1042 **/
1043
1044 void parport_release(struct pardevice *dev)
1045 {
1046 struct parport *port = dev->port->physport;
1047 struct pardevice *pd;
1048 unsigned long flags;
1049
1050 /* Make sure that dev is the current device */
1051 write_lock_irqsave(&port->cad_lock, flags);
1052 if (port->cad != dev) {
1053 write_unlock_irqrestore (&port->cad_lock, flags);
1054 printk(KERN_WARNING "%s: %s tried to release parport "
1055 "when not owner\n", port->name, dev->name);
1056 return;
1057 }
1058
1059 #ifdef CONFIG_PARPORT_1284
1060 /* If this is on a mux port, deselect it. */
1061 if (dev->port->muxport >= 0) {
1062 /* FIXME */
1063 port->muxsel = -1;
1064 }
1065
1066 /* If this is a daisy device, deselect it. */
1067 if (dev->daisy >= 0) {
1068 parport_daisy_deselect_all (port);
1069 port->daisy = -1;
1070 }
1071 #endif
1072
1073 port->cad = NULL;
1074 write_unlock_irqrestore(&port->cad_lock, flags);
1075
1076 /* Save control registers */
1077 port->ops->save_state(port, dev->state);
1078
1079 /* If anybody is waiting, find out who's been there longest and
1080 then wake them up. (Note: no locking required) */
1081 /* !!! LOCKING IS NEEDED HERE */
1082 for (pd = port->waithead; pd; pd = pd->waitnext) {
1083 if (pd->waiting & 2) { /* sleeping in claim_or_block */
1084 parport_claim(pd);
1085 if (waitqueue_active(&pd->wait_q))
1086 wake_up(&pd->wait_q);
1087 return;
1088 } else if (pd->wakeup) {
1089 pd->wakeup(pd->private);
1090 if (dev->port->cad) /* racy but no matter */
1091 return;
1092 } else {
1093 printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
1094 }
1095 }
1096
1097 /* Nobody was waiting, so walk the list to see if anyone is
1098 interested in being woken up. (Note: no locking required) */
1099 /* !!! LOCKING IS NEEDED HERE */
1100 for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) {
1101 if (pd->wakeup && pd != dev)
1102 pd->wakeup(pd->private);
1103 }
1104 }
1105
1106 static int parport_parse_params (int nports, const char *str[], int val[],
1107 int automatic, int none, int nofifo)
1108 {
1109 unsigned int i;
1110 for (i = 0; i < nports && str[i]; i++) {
1111 if (!strncmp(str[i], "auto", 4))
1112 val[i] = automatic;
1113 else if (!strncmp(str[i], "none", 4))
1114 val[i] = none;
1115 else if (nofifo && !strncmp(str[i], "nofifo", 4))
1116 val[i] = nofifo;
1117 else {
1118 char *ep;
1119 unsigned long r = simple_strtoul(str[i], &ep, 0);
1120 if (ep != str[i])
1121 val[i] = r;
1122 else {
1123 printk(KERN_ERR "parport: bad specifier `%s'\n", str[i]);
1124 return -1;
1125 }
1126 }
1127 }
1128
1129 return 0;
1130 }
1131
1132 int parport_parse_irqs(int nports, const char *irqstr[], int irqval[])
1133 {
1134 return parport_parse_params (nports, irqstr, irqval, PARPORT_IRQ_AUTO,
1135 PARPORT_IRQ_NONE, 0);
1136 }
1137
1138 int parport_parse_dmas(int nports, const char *dmastr[], int dmaval[])
1139 {
1140 return parport_parse_params (nports, dmastr, dmaval, PARPORT_DMA_AUTO,
1141 PARPORT_DMA_NONE, PARPORT_DMA_NOFIFO);
1142 }
1143