blob: 2cb22c8d3357fbe4922a54a613c9aec116265029 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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/module.h>
22#include <linux/string.h>
23#include <linux/threads.h>
24#include <linux/parport.h>
25#include <linux/delay.h>
26#include <linux/errno.h>
27#include <linux/interrupt.h>
28#include <linux/ioport.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/sched.h>
32#include <linux/kmod.h>
33
34#include <linux/spinlock.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/irq.h>
37
38#undef PARPORT_PARANOID
39
40#define PARPORT_DEFAULT_TIMESLICE (HZ/5)
41
42unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
43int parport_default_spintime = DEFAULT_SPIN_TIME;
44
45static LIST_HEAD(portlist);
46static DEFINE_SPINLOCK(parportlist_lock);
47
48/* list of all allocated ports, sorted by ->number */
49static LIST_HEAD(all_ports);
50static DEFINE_SPINLOCK(full_list_lock);
51
52static LIST_HEAD(drivers);
53
Ingo Molnar14cc3e22006-03-26 01:37:14 -080054static DEFINE_MUTEX(registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* What you can do to a port that's gone away.. */
57static void dead_write_lines (struct parport *p, unsigned char b){}
58static unsigned char dead_read_lines (struct parport *p) { return 0; }
59static unsigned char dead_frob_lines (struct parport *p, unsigned char b,
60 unsigned char c) { return 0; }
61static void dead_onearg (struct parport *p){}
62static void dead_initstate (struct pardevice *d, struct parport_state *s) { }
63static void dead_state (struct parport *p, struct parport_state *s) { }
64static size_t dead_write (struct parport *p, const void *b, size_t l, int f)
65{ return 0; }
66static size_t dead_read (struct parport *p, void *b, size_t l, int f)
67{ return 0; }
68static struct parport_operations dead_ops = {
69 .write_data = dead_write_lines, /* data */
70 .read_data = dead_read_lines,
71
72 .write_control = dead_write_lines, /* control */
73 .read_control = dead_read_lines,
74 .frob_control = dead_frob_lines,
75
76 .read_status = dead_read_lines, /* status */
77
78 .enable_irq = dead_onearg, /* enable_irq */
79 .disable_irq = dead_onearg, /* disable_irq */
80
81 .data_forward = dead_onearg, /* data_forward */
82 .data_reverse = dead_onearg, /* data_reverse */
83
84 .init_state = dead_initstate, /* init_state */
85 .save_state = dead_state,
86 .restore_state = dead_state,
87
88 .epp_write_data = dead_write, /* epp */
89 .epp_read_data = dead_read,
90 .epp_write_addr = dead_write,
91 .epp_read_addr = dead_read,
92
93 .ecp_write_data = dead_write, /* ecp */
94 .ecp_read_data = dead_read,
95 .ecp_write_addr = dead_write,
96
97 .compat_write_data = dead_write, /* compat */
98 .nibble_read_data = dead_read, /* nibble */
99 .byte_read_data = dead_read, /* byte */
100
101 .owner = NULL,
102};
103
104/* Call attach(port) for each registered driver. */
105static void attach_driver_chain(struct parport *port)
106{
107 /* caller has exclusive registration_lock */
108 struct parport_driver *drv;
109 list_for_each_entry(drv, &drivers, list)
110 drv->attach(port);
111}
112
113/* Call detach(port) for each registered driver. */
114static void detach_driver_chain(struct parport *port)
115{
116 struct parport_driver *drv;
117 /* caller has exclusive registration_lock */
118 list_for_each_entry(drv, &drivers, list)
119 drv->detach (port);
120}
121
122/* Ask kmod for some lowlevel drivers. */
123static void get_lowlevel_driver (void)
124{
125 /* There is no actual module called this: you should set
126 * up an alias for modutils. */
127 request_module ("parport_lowlevel");
128}
129
130/**
131 * parport_register_driver - register a parallel port device driver
132 * @drv: structure describing the driver
133 *
134 * This can be called by a parallel port device driver in order
135 * to receive notifications about ports being found in the
136 * system, as well as ports no longer available.
137 *
138 * The @drv structure is allocated by the caller and must not be
139 * deallocated until after calling parport_unregister_driver().
140 *
141 * The driver's attach() function may block. The port that
142 * attach() is given will be valid for the duration of the
143 * callback, but if the driver wants to take a copy of the
144 * pointer it must call parport_get_port() to do so. Calling
145 * parport_register_device() on that port will do this for you.
146 *
147 * The driver's detach() function may block. The port that
148 * detach() is given will be valid for the duration of the
149 * callback, but if the driver wants to take a copy of the
150 * pointer it must call parport_get_port() to do so.
151 *
152 * Returns 0 on success. Currently it always succeeds.
153 **/
154
155int parport_register_driver (struct parport_driver *drv)
156{
157 struct parport *port;
158
159 if (list_empty(&portlist))
160 get_lowlevel_driver ();
161
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800162 mutex_lock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 list_for_each_entry(port, &portlist, list)
164 drv->attach(port);
165 list_add(&drv->list, &drivers);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800166 mutex_unlock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return 0;
169}
170
171/**
172 * parport_unregister_driver - deregister a parallel port device driver
173 * @drv: structure describing the driver that was given to
174 * parport_register_driver()
175 *
176 * This should be called by a parallel port device driver that
177 * has registered itself using parport_register_driver() when it
178 * is about to be unloaded.
179 *
180 * When it returns, the driver's attach() routine will no longer
181 * be called, and for each port that attach() was called for, the
182 * detach() routine will have been called.
183 *
184 * All the driver's attach() and detach() calls are guaranteed to have
185 * finished by the time this function returns.
186 **/
187
188void parport_unregister_driver (struct parport_driver *drv)
189{
190 struct parport *port;
191
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800192 mutex_lock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 list_del_init(&drv->list);
194 list_for_each_entry(port, &portlist, list)
195 drv->detach(port);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800196 mutex_unlock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199static void free_port (struct parport *port)
200{
201 int d;
202 spin_lock(&full_list_lock);
203 list_del(&port->full_list);
204 spin_unlock(&full_list_lock);
205 for (d = 0; d < 5; d++) {
Jesper Juhl6044ec82005-11-07 01:01:32 -0800206 kfree(port->probe_info[d].class_name);
207 kfree(port->probe_info[d].mfr);
208 kfree(port->probe_info[d].model);
209 kfree(port->probe_info[d].cmdset);
210 kfree(port->probe_info[d].description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 kfree(port->name);
214 kfree(port);
215}
216
217/**
218 * parport_get_port - increment a port's reference count
219 * @port: the port
220 *
Randy Dunlap7d469722006-06-25 05:49:29 -0700221 * This ensures that a struct parport pointer remains valid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * until the matching parport_put_port() call.
223 **/
224
225struct parport *parport_get_port (struct parport *port)
226{
227 atomic_inc (&port->ref_count);
228 return port;
229}
230
231/**
232 * parport_put_port - decrement a port's reference count
233 * @port: the port
234 *
235 * This should be called once for each call to parport_get_port(),
236 * once the port is no longer needed.
237 **/
238
239void parport_put_port (struct parport *port)
240{
241 if (atomic_dec_and_test (&port->ref_count))
242 /* Can destroy it now. */
243 free_port (port);
244
245 return;
246}
247
248/**
249 * parport_register_port - register a parallel port
250 * @base: base I/O address
251 * @irq: IRQ line
252 * @dma: DMA channel
253 * @ops: pointer to the port driver's port operations structure
254 *
255 * When a parallel port (lowlevel) driver finds a port that
256 * should be made available to parallel port device drivers, it
257 * should call parport_register_port(). The @base, @irq, and
258 * @dma parameters are for the convenience of port drivers, and
259 * for ports where they aren't meaningful needn't be set to
260 * anything special. They can be altered afterwards by adjusting
261 * the relevant members of the parport structure that is returned
262 * and represents the port. They should not be tampered with
263 * after calling parport_announce_port, however.
264 *
265 * If there are parallel port device drivers in the system that
266 * have registered themselves using parport_register_driver(),
267 * they are not told about the port at this time; that is done by
268 * parport_announce_port().
269 *
270 * The @ops structure is allocated by the caller, and must not be
271 * deallocated before calling parport_remove_port().
272 *
273 * If there is no memory to allocate a new parport structure,
274 * this function will return %NULL.
275 **/
276
277struct parport *parport_register_port(unsigned long base, int irq, int dma,
278 struct parport_operations *ops)
279{
280 struct list_head *l;
281 struct parport *tmp;
282 int num;
283 int device;
284 char *name;
285
286 tmp = kmalloc(sizeof(struct parport), GFP_KERNEL);
287 if (!tmp) {
288 printk(KERN_WARNING "parport: memory squeeze\n");
289 return NULL;
290 }
291
292 /* Init our structure */
293 memset(tmp, 0, sizeof(struct parport));
294 tmp->base = base;
295 tmp->irq = irq;
296 tmp->dma = dma;
297 tmp->muxport = tmp->daisy = tmp->muxsel = -1;
298 tmp->modes = 0;
299 INIT_LIST_HEAD(&tmp->list);
300 tmp->devices = tmp->cad = NULL;
301 tmp->flags = 0;
302 tmp->ops = ops;
303 tmp->physport = tmp;
304 memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info));
305 rwlock_init(&tmp->cad_lock);
306 spin_lock_init(&tmp->waitlist_lock);
307 spin_lock_init(&tmp->pardevice_lock);
308 tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
309 tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
310 init_MUTEX_LOCKED (&tmp->ieee1284.irq); /* actually a semaphore at 0 */
311 tmp->spintime = parport_default_spintime;
312 atomic_set (&tmp->ref_count, 1);
313 INIT_LIST_HEAD(&tmp->full_list);
314
315 name = kmalloc(15, GFP_KERNEL);
316 if (!name) {
317 printk(KERN_ERR "parport: memory squeeze\n");
318 kfree(tmp);
319 return NULL;
320 }
321 /* Search for the lowest free parport number. */
322
323 spin_lock(&full_list_lock);
324 for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
325 struct parport *p = list_entry(l, struct parport, full_list);
326 if (p->number != num)
327 break;
328 }
329 tmp->portnum = tmp->number = num;
330 list_add_tail(&tmp->full_list, l);
331 spin_unlock(&full_list_lock);
332
333 /*
334 * Now that the portnum is known finish doing the Init.
335 */
336 sprintf(name, "parport%d", tmp->portnum = tmp->number);
337 tmp->name = name;
338
339 for (device = 0; device < 5; device++)
340 /* assume the worst */
341 tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
342
343 tmp->waithead = tmp->waittail = NULL;
344
345 return tmp;
346}
347
348/**
349 * parport_announce_port - tell device drivers about a parallel port
350 * @port: parallel port to announce
351 *
352 * After a port driver has registered a parallel port with
353 * parport_register_port, and performed any necessary
354 * initialisation or adjustments, it should call
355 * parport_announce_port() in order to notify all device drivers
356 * that have called parport_register_driver(). Their attach()
357 * functions will be called, with @port as the parameter.
358 **/
359
360void parport_announce_port (struct parport *port)
361{
362 int i;
363
364#ifdef CONFIG_PARPORT_1284
365 /* Analyse the IEEE1284.3 topology of the port. */
366 parport_daisy_init(port);
367#endif
368
369 parport_proc_register(port);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800370 mutex_lock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_lock_irq(&parportlist_lock);
372 list_add_tail(&port->list, &portlist);
373 for (i = 1; i < 3; i++) {
374 struct parport *slave = port->slaves[i-1];
375 if (slave)
376 list_add_tail(&slave->list, &portlist);
377 }
378 spin_unlock_irq(&parportlist_lock);
379
380 /* Let drivers know that new port(s) has arrived. */
381 attach_driver_chain (port);
382 for (i = 1; i < 3; i++) {
383 struct parport *slave = port->slaves[i-1];
384 if (slave)
385 attach_driver_chain(slave);
386 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800387 mutex_unlock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
391 * parport_remove_port - deregister a parallel port
392 * @port: parallel port to deregister
393 *
394 * When a parallel port driver is forcibly unloaded, or a
395 * parallel port becomes inaccessible, the port driver must call
396 * this function in order to deal with device drivers that still
397 * want to use it.
398 *
399 * The parport structure associated with the port has its
400 * operations structure replaced with one containing 'null'
401 * operations that return errors or just don't do anything.
402 *
403 * Any drivers that have registered themselves using
404 * parport_register_driver() are notified that the port is no
405 * longer accessible by having their detach() routines called
406 * with @port as the parameter.
407 **/
408
409void parport_remove_port(struct parport *port)
410{
411 int i;
412
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800413 mutex_lock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Spread the word. */
416 detach_driver_chain (port);
417
418#ifdef CONFIG_PARPORT_1284
419 /* Forget the IEEE1284.3 topology of the port. */
420 parport_daisy_fini(port);
421 for (i = 1; i < 3; i++) {
422 struct parport *slave = port->slaves[i-1];
423 if (!slave)
424 continue;
425 detach_driver_chain(slave);
426 parport_daisy_fini(slave);
427 }
428#endif
429
430 port->ops = &dead_ops;
431 spin_lock(&parportlist_lock);
432 list_del_init(&port->list);
433 for (i = 1; i < 3; i++) {
434 struct parport *slave = port->slaves[i-1];
435 if (slave)
436 list_del_init(&slave->list);
437 }
438 spin_unlock(&parportlist_lock);
439
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800440 mutex_unlock(&registration_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 parport_proc_unregister(port);
443
444 for (i = 1; i < 3; i++) {
445 struct parport *slave = port->slaves[i-1];
446 if (slave)
447 parport_put_port(slave);
448 }
449}
450
451/**
452 * parport_register_device - register a device on a parallel port
453 * @port: port to which the device is attached
454 * @name: a name to refer to the device
455 * @pf: preemption callback
456 * @kf: kick callback (wake-up)
457 * @irq_func: interrupt handler
458 * @flags: registration flags
459 * @handle: data for callback functions
460 *
461 * This function, called by parallel port device drivers,
462 * declares that a device is connected to a port, and tells the
463 * system all it needs to know.
464 *
465 * The @name is allocated by the caller and must not be
466 * deallocated until the caller calls @parport_unregister_device
467 * for that device.
468 *
469 * The preemption callback function, @pf, is called when this
470 * device driver has claimed access to the port but another
471 * device driver wants to use it. It is given @handle as its
472 * parameter, and should return zero if it is willing for the
473 * system to release the port to another driver on its behalf.
474 * If it wants to keep control of the port it should return
475 * non-zero, and no action will be taken. It is good manners for
476 * the driver to try to release the port at the earliest
477 * opportunity after its preemption callback rejects a preemption
478 * attempt. Note that if a preemption callback is happy for
479 * preemption to go ahead, there is no need to release the port;
480 * it is done automatically. This function may not block, as it
481 * may be called from interrupt context. If the device driver
482 * does not support preemption, @pf can be %NULL.
483 *
484 * The wake-up ("kick") callback function, @kf, is called when
485 * the port is available to be claimed for exclusive access; that
486 * is, parport_claim() is guaranteed to succeed when called from
487 * inside the wake-up callback function. If the driver wants to
488 * claim the port it should do so; otherwise, it need not take
489 * any action. This function may not block, as it may be called
490 * from interrupt context. If the device driver does not want to
491 * be explicitly invited to claim the port in this way, @kf can
492 * be %NULL.
493 *
494 * The interrupt handler, @irq_func, is called when an interrupt
495 * arrives from the parallel port. Note that if a device driver
496 * wants to use interrupts it should use parport_enable_irq(),
497 * and can also check the irq member of the parport structure
498 * representing the port.
499 *
500 * The parallel port (lowlevel) driver is the one that has called
501 * request_irq() and whose interrupt handler is called first.
502 * This handler does whatever needs to be done to the hardware to
503 * acknowledge the interrupt (for PC-style ports there is nothing
504 * special to be done). It then tells the IEEE 1284 code about
505 * the interrupt, which may involve reacting to an IEEE 1284
506 * event depending on the current IEEE 1284 phase. After this,
507 * it calls @irq_func. Needless to say, @irq_func will be called
508 * from interrupt context, and may not block.
509 *
510 * The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
511 * so should only be used when sharing the port with other device
512 * drivers is impossible and would lead to incorrect behaviour.
513 * Use it sparingly! Normally, @flags will be zero.
514 *
515 * This function returns a pointer to a structure that represents
516 * the device on the port, or %NULL if there is not enough memory
517 * to allocate space for that structure.
518 **/
519
520struct pardevice *
521parport_register_device(struct parport *port, const char *name,
522 int (*pf)(void *), void (*kf)(void *),
523 void (*irq_func)(int, void *, struct pt_regs *),
524 int flags, void *handle)
525{
526 struct pardevice *tmp;
527
528 if (port->physport->flags & PARPORT_FLAG_EXCL) {
529 /* An exclusive device is registered. */
530 printk (KERN_DEBUG "%s: no more devices allowed\n",
531 port->name);
532 return NULL;
533 }
534
535 if (flags & PARPORT_DEV_LURK) {
536 if (!pf || !kf) {
537 printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
538 return NULL;
539 }
540 }
541
542 /* We up our own module reference count, and that of the port
543 on which a device is to be registered, to ensure that
544 neither of us gets unloaded while we sleep in (e.g.)
545 kmalloc.
546 */
547 if (!try_module_get(port->ops->owner)) {
548 return NULL;
549 }
550
551 parport_get_port (port);
552
553 tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
554 if (tmp == NULL) {
555 printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
556 goto out;
557 }
558
559 tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
560 if (tmp->state == NULL) {
561 printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
562 goto out_free_pardevice;
563 }
564
565 tmp->name = name;
566 tmp->port = port;
567 tmp->daisy = -1;
568 tmp->preempt = pf;
569 tmp->wakeup = kf;
570 tmp->private = handle;
571 tmp->flags = flags;
572 tmp->irq_func = irq_func;
573 tmp->waiting = 0;
574 tmp->timeout = 5 * HZ;
575
576 /* Chain this onto the list */
577 tmp->prev = NULL;
578 /*
579 * This function must not run from an irq handler so we don' t need
580 * to clear irq on the local CPU. -arca
581 */
582 spin_lock(&port->physport->pardevice_lock);
583
584 if (flags & PARPORT_DEV_EXCL) {
585 if (port->physport->devices) {
586 spin_unlock (&port->physport->pardevice_lock);
587 printk (KERN_DEBUG
588 "%s: cannot grant exclusive access for "
589 "device %s\n", port->name, name);
590 goto out_free_all;
591 }
592 port->flags |= PARPORT_FLAG_EXCL;
593 }
594
595 tmp->next = port->physport->devices;
596 wmb(); /* Make sure that tmp->next is written before it's
597 added to the list; see comments marked 'no locking
598 required' */
599 if (port->physport->devices)
600 port->physport->devices->prev = tmp;
601 port->physport->devices = tmp;
602 spin_unlock(&port->physport->pardevice_lock);
603
604 init_waitqueue_head(&tmp->wait_q);
605 tmp->timeslice = parport_default_timeslice;
606 tmp->waitnext = tmp->waitprev = NULL;
607
608 /*
609 * This has to be run as last thing since init_state may need other
610 * pardevice fields. -arca
611 */
612 port->ops->init_state(tmp, tmp->state);
613 parport_device_proc_register(tmp);
614 return tmp;
615
616 out_free_all:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800617 kfree(tmp->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 out_free_pardevice:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800619 kfree(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 out:
621 parport_put_port (port);
622 module_put(port->ops->owner);
623
624 return NULL;
625}
626
627/**
628 * parport_unregister_device - deregister a device on a parallel port
629 * @dev: pointer to structure representing device
630 *
631 * This undoes the effect of parport_register_device().
632 **/
633
634void parport_unregister_device(struct pardevice *dev)
635{
636 struct parport *port;
637
638#ifdef PARPORT_PARANOID
639 if (dev == NULL) {
640 printk(KERN_ERR "parport_unregister_device: passed NULL\n");
641 return;
642 }
643#endif
644
645 parport_device_proc_unregister(dev);
646
647 port = dev->port->physport;
648
649 if (port->cad == dev) {
650 printk(KERN_DEBUG "%s: %s forgot to release port\n",
651 port->name, dev->name);
652 parport_release (dev);
653 }
654
655 spin_lock(&port->pardevice_lock);
656 if (dev->next)
657 dev->next->prev = dev->prev;
658 if (dev->prev)
659 dev->prev->next = dev->next;
660 else
661 port->devices = dev->next;
662
663 if (dev->flags & PARPORT_DEV_EXCL)
664 port->flags &= ~PARPORT_FLAG_EXCL;
665
666 spin_unlock(&port->pardevice_lock);
667
668 /* Make sure we haven't left any pointers around in the wait
669 * list. */
670 spin_lock (&port->waitlist_lock);
671 if (dev->waitprev || dev->waitnext || port->waithead == dev) {
672 if (dev->waitprev)
673 dev->waitprev->waitnext = dev->waitnext;
674 else
675 port->waithead = dev->waitnext;
676 if (dev->waitnext)
677 dev->waitnext->waitprev = dev->waitprev;
678 else
679 port->waittail = dev->waitprev;
680 }
681 spin_unlock (&port->waitlist_lock);
682
683 kfree(dev->state);
684 kfree(dev);
685
686 module_put(port->ops->owner);
687 parport_put_port (port);
688}
689
690/**
691 * parport_find_number - find a parallel port by number
692 * @number: parallel port number
693 *
694 * This returns the parallel port with the specified number, or
695 * %NULL if there is none.
696 *
697 * There is an implicit parport_get_port() done already; to throw
698 * away the reference to the port that parport_find_number()
699 * gives you, use parport_put_port().
700 */
701
702struct parport *parport_find_number (int number)
703{
704 struct parport *port, *result = NULL;
705
706 if (list_empty(&portlist))
707 get_lowlevel_driver ();
708
709 spin_lock (&parportlist_lock);
710 list_for_each_entry(port, &portlist, list) {
711 if (port->number == number) {
712 result = parport_get_port (port);
713 break;
714 }
715 }
716 spin_unlock (&parportlist_lock);
717 return result;
718}
719
720/**
721 * parport_find_base - find a parallel port by base address
722 * @base: base I/O address
723 *
724 * This returns the parallel port with the specified base
725 * address, or %NULL if there is none.
726 *
727 * There is an implicit parport_get_port() done already; to throw
728 * away the reference to the port that parport_find_base()
729 * gives you, use parport_put_port().
730 */
731
732struct parport *parport_find_base (unsigned long base)
733{
734 struct parport *port, *result = NULL;
735
736 if (list_empty(&portlist))
737 get_lowlevel_driver ();
738
739 spin_lock (&parportlist_lock);
740 list_for_each_entry(port, &portlist, list) {
741 if (port->base == base) {
742 result = parport_get_port (port);
743 break;
744 }
745 }
746 spin_unlock (&parportlist_lock);
747 return result;
748}
749
750/**
751 * parport_claim - claim access to a parallel port device
752 * @dev: pointer to structure representing a device on the port
753 *
754 * This function will not block and so can be used from interrupt
755 * context. If parport_claim() succeeds in claiming access to
756 * the port it returns zero and the port is available to use. It
757 * may fail (returning non-zero) if the port is in use by another
758 * driver and that driver is not willing to relinquish control of
759 * the port.
760 **/
761
762int parport_claim(struct pardevice *dev)
763{
764 struct pardevice *oldcad;
765 struct parport *port = dev->port->physport;
766 unsigned long flags;
767
768 if (port->cad == dev) {
769 printk(KERN_INFO "%s: %s already owner\n",
770 dev->port->name,dev->name);
771 return 0;
772 }
773
774 /* Preempt any current device */
775 write_lock_irqsave (&port->cad_lock, flags);
776 if ((oldcad = port->cad) != NULL) {
777 if (oldcad->preempt) {
778 if (oldcad->preempt(oldcad->private))
779 goto blocked;
780 port->ops->save_state(port, dev->state);
781 } else
782 goto blocked;
783
784 if (port->cad != oldcad) {
785 /* I think we'll actually deadlock rather than
786 get here, but just in case.. */
787 printk(KERN_WARNING
788 "%s: %s released port when preempted!\n",
789 port->name, oldcad->name);
790 if (port->cad)
791 goto blocked;
792 }
793 }
794
795 /* Can't fail from now on, so mark ourselves as no longer waiting. */
796 if (dev->waiting & 1) {
797 dev->waiting = 0;
798
799 /* Take ourselves out of the wait list again. */
800 spin_lock_irq (&port->waitlist_lock);
801 if (dev->waitprev)
802 dev->waitprev->waitnext = dev->waitnext;
803 else
804 port->waithead = dev->waitnext;
805 if (dev->waitnext)
806 dev->waitnext->waitprev = dev->waitprev;
807 else
808 port->waittail = dev->waitprev;
809 spin_unlock_irq (&port->waitlist_lock);
810 dev->waitprev = dev->waitnext = NULL;
811 }
812
813 /* Now we do the change of devices */
814 port->cad = dev;
815
816#ifdef CONFIG_PARPORT_1284
817 /* If it's a mux port, select it. */
818 if (dev->port->muxport >= 0) {
819 /* FIXME */
820 port->muxsel = dev->port->muxport;
821 }
822
823 /* If it's a daisy chain device, select it. */
824 if (dev->daisy >= 0) {
825 /* This could be lazier. */
826 if (!parport_daisy_select (port, dev->daisy,
827 IEEE1284_MODE_COMPAT))
828 port->daisy = dev->daisy;
829 }
830#endif /* IEEE1284.3 support */
831
832 /* Restore control registers */
833 port->ops->restore_state(port, dev->state);
834 write_unlock_irqrestore(&port->cad_lock, flags);
835 dev->time = jiffies;
836 return 0;
837
838blocked:
839 /* If this is the first time we tried to claim the port, register an
840 interest. This is only allowed for devices sleeping in
841 parport_claim_or_block(), or those with a wakeup function. */
842
843 /* The cad_lock is still held for writing here */
844 if (dev->waiting & 2 || dev->wakeup) {
845 spin_lock (&port->waitlist_lock);
846 if (test_and_set_bit(0, &dev->waiting) == 0) {
847 /* First add ourselves to the end of the wait list. */
848 dev->waitnext = NULL;
849 dev->waitprev = port->waittail;
850 if (port->waittail) {
851 port->waittail->waitnext = dev;
852 port->waittail = dev;
853 } else
854 port->waithead = port->waittail = dev;
855 }
856 spin_unlock (&port->waitlist_lock);
857 }
858 write_unlock_irqrestore (&port->cad_lock, flags);
859 return -EAGAIN;
860}
861
862/**
863 * parport_claim_or_block - claim access to a parallel port device
864 * @dev: pointer to structure representing a device on the port
865 *
866 * This behaves like parport_claim(), but will block if necessary
867 * to wait for the port to be free. A return value of 1
868 * indicates that it slept; 0 means that it succeeded without
869 * needing to sleep. A negative error code indicates failure.
870 **/
871
872int parport_claim_or_block(struct pardevice *dev)
873{
874 int r;
875
876 /* Signal to parport_claim() that we can wait even without a
877 wakeup function. */
878 dev->waiting = 2;
879
880 /* Try to claim the port. If this fails, we need to sleep. */
881 r = parport_claim(dev);
882 if (r == -EAGAIN) {
883#ifdef PARPORT_DEBUG_SHARING
884 printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
885#endif
886 /*
887 * FIXME!!! Use the proper locking for dev->waiting,
888 * and make this use the "wait_event_interruptible()"
889 * interfaces. The cli/sti that used to be here
890 * did nothing.
891 *
892 * See also parport_release()
893 */
894
895 /* If dev->waiting is clear now, an interrupt
896 gave us the port and we would deadlock if we slept. */
897 if (dev->waiting) {
898 interruptible_sleep_on (&dev->wait_q);
899 if (signal_pending (current)) {
900 return -EINTR;
901 }
902 r = 1;
903 } else {
904 r = 0;
905#ifdef PARPORT_DEBUG_SHARING
906 printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
907 dev->name);
908#endif
909 }
910
911#ifdef PARPORT_DEBUG_SHARING
912 if (dev->port->physport->cad != dev)
913 printk(KERN_DEBUG "%s: exiting parport_claim_or_block "
914 "but %s owns port!\n", dev->name,
915 dev->port->physport->cad ?
916 dev->port->physport->cad->name:"nobody");
917#endif
918 }
919 dev->waiting = 0;
920 return r;
921}
922
923/**
924 * parport_release - give up access to a parallel port device
925 * @dev: pointer to structure representing parallel port device
926 *
927 * This function cannot fail, but it should not be called without
928 * the port claimed. Similarly, if the port is already claimed
929 * you should not try claiming it again.
930 **/
931
932void parport_release(struct pardevice *dev)
933{
934 struct parport *port = dev->port->physport;
935 struct pardevice *pd;
936 unsigned long flags;
937
938 /* Make sure that dev is the current device */
939 write_lock_irqsave(&port->cad_lock, flags);
940 if (port->cad != dev) {
941 write_unlock_irqrestore (&port->cad_lock, flags);
942 printk(KERN_WARNING "%s: %s tried to release parport "
943 "when not owner\n", port->name, dev->name);
944 return;
945 }
946
947#ifdef CONFIG_PARPORT_1284
948 /* If this is on a mux port, deselect it. */
949 if (dev->port->muxport >= 0) {
950 /* FIXME */
951 port->muxsel = -1;
952 }
953
954 /* If this is a daisy device, deselect it. */
955 if (dev->daisy >= 0) {
956 parport_daisy_deselect_all (port);
957 port->daisy = -1;
958 }
959#endif
960
961 port->cad = NULL;
962 write_unlock_irqrestore(&port->cad_lock, flags);
963
964 /* Save control registers */
965 port->ops->save_state(port, dev->state);
966
967 /* If anybody is waiting, find out who's been there longest and
968 then wake them up. (Note: no locking required) */
969 /* !!! LOCKING IS NEEDED HERE */
970 for (pd = port->waithead; pd; pd = pd->waitnext) {
971 if (pd->waiting & 2) { /* sleeping in claim_or_block */
972 parport_claim(pd);
973 if (waitqueue_active(&pd->wait_q))
974 wake_up_interruptible(&pd->wait_q);
975 return;
976 } else if (pd->wakeup) {
977 pd->wakeup(pd->private);
978 if (dev->port->cad) /* racy but no matter */
979 return;
980 } else {
981 printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
982 }
983 }
984
985 /* Nobody was waiting, so walk the list to see if anyone is
986 interested in being woken up. (Note: no locking required) */
987 /* !!! LOCKING IS NEEDED HERE */
988 for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) {
989 if (pd->wakeup && pd != dev)
990 pd->wakeup(pd->private);
991 }
992}
993
994/* Exported symbols for modules. */
995
996EXPORT_SYMBOL(parport_claim);
997EXPORT_SYMBOL(parport_claim_or_block);
998EXPORT_SYMBOL(parport_release);
999EXPORT_SYMBOL(parport_register_port);
1000EXPORT_SYMBOL(parport_announce_port);
1001EXPORT_SYMBOL(parport_remove_port);
1002EXPORT_SYMBOL(parport_register_driver);
1003EXPORT_SYMBOL(parport_unregister_driver);
1004EXPORT_SYMBOL(parport_register_device);
1005EXPORT_SYMBOL(parport_unregister_device);
Marko Kohtala94b82092006-01-06 00:19:51 -08001006EXPORT_SYMBOL(parport_get_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007EXPORT_SYMBOL(parport_put_port);
1008EXPORT_SYMBOL(parport_find_number);
1009EXPORT_SYMBOL(parport_find_base);
1010
1011MODULE_LICENSE("GPL");