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