xref: /openbmc/linux/drivers/parport/share.c (revision 3381df09)
1 /*
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/module.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/signal.h>
31 #include <linux/kmod.h>
32 #include <linux/device.h>
33 
34 #include <linux/spinlock.h>
35 #include <linux/mutex.h>
36 #include <asm/irq.h>
37 
38 #undef PARPORT_PARANOID
39 
40 #define PARPORT_DEFAULT_TIMESLICE	(HZ/5)
41 
42 unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
43 int parport_default_spintime =  DEFAULT_SPIN_TIME;
44 
45 static LIST_HEAD(portlist);
46 static DEFINE_SPINLOCK(parportlist_lock);
47 
48 /* list of all allocated ports, sorted by ->number */
49 static LIST_HEAD(all_ports);
50 static DEFINE_SPINLOCK(full_list_lock);
51 
52 static LIST_HEAD(drivers);
53 
54 static DEFINE_MUTEX(registration_lock);
55 
56 /* What you can do to a port that's gone away.. */
57 static void dead_write_lines(struct parport *p, unsigned char b){}
58 static unsigned char dead_read_lines(struct parport *p) { return 0; }
59 static unsigned char dead_frob_lines(struct parport *p, unsigned char b,
60 			     unsigned char c) { return 0; }
61 static void dead_onearg(struct parport *p){}
62 static void dead_initstate(struct pardevice *d, struct parport_state *s) { }
63 static void dead_state(struct parport *p, struct parport_state *s) { }
64 static size_t dead_write(struct parport *p, const void *b, size_t l, int f)
65 { return 0; }
66 static size_t dead_read(struct parport *p, void *b, size_t l, int f)
67 { return 0; }
68 static 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 static struct device_type parport_device_type = {
105 	.name = "parport",
106 };
107 
108 static int is_parport(struct device *dev)
109 {
110 	return dev->type == &parport_device_type;
111 }
112 
113 static int parport_probe(struct device *dev)
114 {
115 	struct parport_driver *drv;
116 
117 	if (is_parport(dev))
118 		return -ENODEV;
119 
120 	drv = to_parport_driver(dev->driver);
121 	if (!drv->probe) {
122 		/* if driver has not defined a custom probe */
123 		struct pardevice *par_dev = to_pardevice(dev);
124 
125 		if (strcmp(par_dev->name, drv->name))
126 			return -ENODEV;
127 		return 0;
128 	}
129 	/* if driver defined its own probe */
130 	return drv->probe(to_pardevice(dev));
131 }
132 
133 static struct bus_type parport_bus_type = {
134 	.name = "parport",
135 	.probe = parport_probe,
136 };
137 
138 int parport_bus_init(void)
139 {
140 	return bus_register(&parport_bus_type);
141 }
142 
143 void parport_bus_exit(void)
144 {
145 	bus_unregister(&parport_bus_type);
146 }
147 
148 /*
149  * iterates through all the drivers registered with the bus and sends the port
150  * details to the match_port callback of the driver, so that the driver can
151  * know about the new port that just registered with the bus and decide if it
152  * wants to use this new port.
153  */
154 static int driver_check(struct device_driver *dev_drv, void *_port)
155 {
156 	struct parport *port = _port;
157 	struct parport_driver *drv = to_parport_driver(dev_drv);
158 
159 	if (drv->match_port)
160 		drv->match_port(port);
161 	return 0;
162 }
163 
164 /* Call attach(port) for each registered driver. */
165 static void attach_driver_chain(struct parport *port)
166 {
167 	/* caller has exclusive registration_lock */
168 	struct parport_driver *drv;
169 
170 	list_for_each_entry(drv, &drivers, list)
171 		drv->attach(port);
172 
173 	/*
174 	 * call the driver_check function of the drivers registered in
175 	 * new device model
176 	 */
177 
178 	bus_for_each_drv(&parport_bus_type, NULL, port, driver_check);
179 }
180 
181 static int driver_detach(struct device_driver *_drv, void *_port)
182 {
183 	struct parport *port = _port;
184 	struct parport_driver *drv = to_parport_driver(_drv);
185 
186 	if (drv->detach)
187 		drv->detach(port);
188 	return 0;
189 }
190 
191 /* Call detach(port) for each registered driver. */
192 static void detach_driver_chain(struct parport *port)
193 {
194 	struct parport_driver *drv;
195 	/* caller has exclusive registration_lock */
196 	list_for_each_entry(drv, &drivers, list)
197 		drv->detach(port);
198 
199 	/*
200 	 * call the detach function of the drivers registered in
201 	 * new device model
202 	 */
203 
204 	bus_for_each_drv(&parport_bus_type, NULL, port, driver_detach);
205 }
206 
207 /* Ask kmod for some lowlevel drivers. */
208 static void get_lowlevel_driver(void)
209 {
210 	/*
211 	 * There is no actual module called this: you should set
212 	 * up an alias for modutils.
213 	 */
214 	request_module("parport_lowlevel");
215 }
216 
217 /*
218  * iterates through all the devices connected to the bus and sends the device
219  * details to the match_port callback of the driver, so that the driver can
220  * know what are all the ports that are connected to the bus and choose the
221  * port to which it wants to register its device.
222  */
223 static int port_check(struct device *dev, void *dev_drv)
224 {
225 	struct parport_driver *drv = dev_drv;
226 
227 	/* only send ports, do not send other devices connected to bus */
228 	if (is_parport(dev))
229 		drv->match_port(to_parport_dev(dev));
230 	return 0;
231 }
232 
233 /*
234  * Iterates through all the devices connected to the bus and return 1
235  * if the device is a parallel port.
236  */
237 
238 static int port_detect(struct device *dev, void *dev_drv)
239 {
240 	if (is_parport(dev))
241 		return 1;
242 	return 0;
243 }
244 
245 /**
246  *	parport_register_driver - register a parallel port device driver
247  *	@drv: structure describing the driver
248  *	@owner: owner module of drv
249  *	@mod_name: module name string
250  *
251  *	This can be called by a parallel port device driver in order
252  *	to receive notifications about ports being found in the
253  *	system, as well as ports no longer available.
254  *
255  *	If devmodel is true then the new device model is used
256  *	for registration.
257  *
258  *	The @drv structure is allocated by the caller and must not be
259  *	deallocated until after calling parport_unregister_driver().
260  *
261  *	If using the non device model:
262  *	The driver's attach() function may block.  The port that
263  *	attach() is given will be valid for the duration of the
264  *	callback, but if the driver wants to take a copy of the
265  *	pointer it must call parport_get_port() to do so.  Calling
266  *	parport_register_device() on that port will do this for you.
267  *
268  *	The driver's detach() function may block.  The port that
269  *	detach() is given will be valid for the duration of the
270  *	callback, but if the driver wants to take a copy of the
271  *	pointer it must call parport_get_port() to do so.
272  *
273  *
274  *	Returns 0 on success. The non device model will always succeeds.
275  *	but the new device model can fail and will return the error code.
276  **/
277 
278 int __parport_register_driver(struct parport_driver *drv, struct module *owner,
279 			      const char *mod_name)
280 {
281 	if (drv->devmodel) {
282 		/* using device model */
283 		int ret;
284 
285 		/* initialize common driver fields */
286 		drv->driver.name = drv->name;
287 		drv->driver.bus = &parport_bus_type;
288 		drv->driver.owner = owner;
289 		drv->driver.mod_name = mod_name;
290 		ret = driver_register(&drv->driver);
291 		if (ret)
292 			return ret;
293 
294 		/*
295 		 * check if bus has any parallel port registered, if
296 		 * none is found then load the lowlevel driver.
297 		 */
298 		ret = bus_for_each_dev(&parport_bus_type, NULL, NULL,
299 				       port_detect);
300 		if (!ret)
301 			get_lowlevel_driver();
302 
303 		mutex_lock(&registration_lock);
304 		if (drv->match_port)
305 			bus_for_each_dev(&parport_bus_type, NULL, drv,
306 					 port_check);
307 		mutex_unlock(&registration_lock);
308 	} else {
309 		struct parport *port;
310 
311 		drv->devmodel = false;
312 
313 		if (list_empty(&portlist))
314 			get_lowlevel_driver();
315 		mutex_lock(&registration_lock);
316 		list_for_each_entry(port, &portlist, list)
317 			drv->attach(port);
318 		list_add(&drv->list, &drivers);
319 		mutex_unlock(&registration_lock);
320 	}
321 
322 	return 0;
323 }
324 EXPORT_SYMBOL(__parport_register_driver);
325 
326 static int port_detach(struct device *dev, void *_drv)
327 {
328 	struct parport_driver *drv = _drv;
329 
330 	if (is_parport(dev) && drv->detach)
331 		drv->detach(to_parport_dev(dev));
332 
333 	return 0;
334 }
335 
336 /**
337  *	parport_unregister_driver - deregister a parallel port device driver
338  *	@drv: structure describing the driver that was given to
339  *	      parport_register_driver()
340  *
341  *	This should be called by a parallel port device driver that
342  *	has registered itself using parport_register_driver() when it
343  *	is about to be unloaded.
344  *
345  *	When it returns, the driver's attach() routine will no longer
346  *	be called, and for each port that attach() was called for, the
347  *	detach() routine will have been called.
348  *
349  *	All the driver's attach() and detach() calls are guaranteed to have
350  *	finished by the time this function returns.
351  **/
352 
353 void parport_unregister_driver(struct parport_driver *drv)
354 {
355 	struct parport *port;
356 
357 	mutex_lock(&registration_lock);
358 	if (drv->devmodel) {
359 		bus_for_each_dev(&parport_bus_type, NULL, drv, port_detach);
360 		driver_unregister(&drv->driver);
361 	} else {
362 		list_del_init(&drv->list);
363 		list_for_each_entry(port, &portlist, list)
364 			drv->detach(port);
365 	}
366 	mutex_unlock(&registration_lock);
367 }
368 EXPORT_SYMBOL(parport_unregister_driver);
369 
370 static void free_port(struct device *dev)
371 {
372 	int d;
373 	struct parport *port = to_parport_dev(dev);
374 
375 	spin_lock(&full_list_lock);
376 	list_del(&port->full_list);
377 	spin_unlock(&full_list_lock);
378 	for (d = 0; d < 5; d++) {
379 		kfree(port->probe_info[d].class_name);
380 		kfree(port->probe_info[d].mfr);
381 		kfree(port->probe_info[d].model);
382 		kfree(port->probe_info[d].cmdset);
383 		kfree(port->probe_info[d].description);
384 	}
385 
386 	kfree(port->name);
387 	kfree(port);
388 }
389 
390 /**
391  *	parport_get_port - increment a port's reference count
392  *	@port: the port
393  *
394  *	This ensures that a struct parport pointer remains valid
395  *	until the matching parport_put_port() call.
396  **/
397 
398 struct parport *parport_get_port(struct parport *port)
399 {
400 	struct device *dev = get_device(&port->bus_dev);
401 
402 	return to_parport_dev(dev);
403 }
404 EXPORT_SYMBOL(parport_get_port);
405 
406 void parport_del_port(struct parport *port)
407 {
408 	device_unregister(&port->bus_dev);
409 }
410 EXPORT_SYMBOL(parport_del_port);
411 
412 /**
413  *	parport_put_port - decrement a port's reference count
414  *	@port: the port
415  *
416  *	This should be called once for each call to parport_get_port(),
417  *	once the port is no longer needed. When the reference count reaches
418  *	zero (port is no longer used), free_port is called.
419  **/
420 
421 void parport_put_port(struct parport *port)
422 {
423 	put_device(&port->bus_dev);
424 }
425 EXPORT_SYMBOL(parport_put_port);
426 
427 /**
428  *	parport_register_port - register a parallel port
429  *	@base: base I/O address
430  *	@irq: IRQ line
431  *	@dma: DMA channel
432  *	@ops: pointer to the port driver's port operations structure
433  *
434  *	When a parallel port (lowlevel) driver finds a port that
435  *	should be made available to parallel port device drivers, it
436  *	should call parport_register_port().  The @base, @irq, and
437  *	@dma parameters are for the convenience of port drivers, and
438  *	for ports where they aren't meaningful needn't be set to
439  *	anything special.  They can be altered afterwards by adjusting
440  *	the relevant members of the parport structure that is returned
441  *	and represents the port.  They should not be tampered with
442  *	after calling parport_announce_port, however.
443  *
444  *	If there are parallel port device drivers in the system that
445  *	have registered themselves using parport_register_driver(),
446  *	they are not told about the port at this time; that is done by
447  *	parport_announce_port().
448  *
449  *	The @ops structure is allocated by the caller, and must not be
450  *	deallocated before calling parport_remove_port().
451  *
452  *	If there is no memory to allocate a new parport structure,
453  *	this function will return %NULL.
454  **/
455 
456 struct parport *parport_register_port(unsigned long base, int irq, int dma,
457 				      struct parport_operations *ops)
458 {
459 	struct list_head *l;
460 	struct parport *tmp;
461 	int num;
462 	int device;
463 	char *name;
464 	int ret;
465 
466 	tmp = kzalloc(sizeof(struct parport), GFP_KERNEL);
467 	if (!tmp)
468 		return NULL;
469 
470 	/* Init our structure */
471 	tmp->base = base;
472 	tmp->irq = irq;
473 	tmp->dma = dma;
474 	tmp->muxport = tmp->daisy = tmp->muxsel = -1;
475 	tmp->modes = 0;
476 	INIT_LIST_HEAD(&tmp->list);
477 	tmp->devices = tmp->cad = NULL;
478 	tmp->flags = 0;
479 	tmp->ops = ops;
480 	tmp->physport = tmp;
481 	memset(tmp->probe_info, 0, 5 * sizeof(struct parport_device_info));
482 	rwlock_init(&tmp->cad_lock);
483 	spin_lock_init(&tmp->waitlist_lock);
484 	spin_lock_init(&tmp->pardevice_lock);
485 	tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
486 	tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
487 	sema_init(&tmp->ieee1284.irq, 0);
488 	tmp->spintime = parport_default_spintime;
489 	atomic_set(&tmp->ref_count, 1);
490 	INIT_LIST_HEAD(&tmp->full_list);
491 
492 	name = kmalloc(15, GFP_KERNEL);
493 	if (!name) {
494 		kfree(tmp);
495 		return NULL;
496 	}
497 	/* Search for the lowest free parport number. */
498 
499 	spin_lock(&full_list_lock);
500 	for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
501 		struct parport *p = list_entry(l, struct parport, full_list);
502 		if (p->number != num)
503 			break;
504 	}
505 	tmp->portnum = tmp->number = num;
506 	list_add_tail(&tmp->full_list, l);
507 	spin_unlock(&full_list_lock);
508 
509 	/*
510 	 * Now that the portnum is known finish doing the Init.
511 	 */
512 	sprintf(name, "parport%d", tmp->portnum = tmp->number);
513 	tmp->name = name;
514 	tmp->bus_dev.bus = &parport_bus_type;
515 	tmp->bus_dev.release = free_port;
516 	dev_set_name(&tmp->bus_dev, name);
517 	tmp->bus_dev.type = &parport_device_type;
518 
519 	for (device = 0; device < 5; device++)
520 		/* assume the worst */
521 		tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
522 
523 	tmp->waithead = tmp->waittail = NULL;
524 
525 	ret = device_register(&tmp->bus_dev);
526 	if (ret) {
527 		put_device(&tmp->bus_dev);
528 		return NULL;
529 	}
530 
531 	return tmp;
532 }
533 EXPORT_SYMBOL(parport_register_port);
534 
535 /**
536  *	parport_announce_port - tell device drivers about a parallel port
537  *	@port: parallel port to announce
538  *
539  *	After a port driver has registered a parallel port with
540  *	parport_register_port, and performed any necessary
541  *	initialisation or adjustments, it should call
542  *	parport_announce_port() in order to notify all device drivers
543  *	that have called parport_register_driver().  Their attach()
544  *	functions will be called, with @port as the parameter.
545  **/
546 
547 void parport_announce_port(struct parport *port)
548 {
549 	int i;
550 
551 #ifdef CONFIG_PARPORT_1284
552 	/* Analyse the IEEE1284.3 topology of the port. */
553 	parport_daisy_init(port);
554 #endif
555 
556 	if (!port->dev)
557 		printk(KERN_WARNING "%s: fix this legacy no-device port driver!\n",
558 		       port->name);
559 
560 	parport_proc_register(port);
561 	mutex_lock(&registration_lock);
562 	spin_lock_irq(&parportlist_lock);
563 	list_add_tail(&port->list, &portlist);
564 	for (i = 1; i < 3; i++) {
565 		struct parport *slave = port->slaves[i-1];
566 		if (slave)
567 			list_add_tail(&slave->list, &portlist);
568 	}
569 	spin_unlock_irq(&parportlist_lock);
570 
571 	/* Let drivers know that new port(s) has arrived. */
572 	attach_driver_chain(port);
573 	for (i = 1; i < 3; i++) {
574 		struct parport *slave = port->slaves[i-1];
575 		if (slave)
576 			attach_driver_chain(slave);
577 	}
578 	mutex_unlock(&registration_lock);
579 }
580 EXPORT_SYMBOL(parport_announce_port);
581 
582 /**
583  *	parport_remove_port - deregister a parallel port
584  *	@port: parallel port to deregister
585  *
586  *	When a parallel port driver is forcibly unloaded, or a
587  *	parallel port becomes inaccessible, the port driver must call
588  *	this function in order to deal with device drivers that still
589  *	want to use it.
590  *
591  *	The parport structure associated with the port has its
592  *	operations structure replaced with one containing 'null'
593  *	operations that return errors or just don't do anything.
594  *
595  *	Any drivers that have registered themselves using
596  *	parport_register_driver() are notified that the port is no
597  *	longer accessible by having their detach() routines called
598  *	with @port as the parameter.
599  **/
600 
601 void parport_remove_port(struct parport *port)
602 {
603 	int i;
604 
605 	mutex_lock(&registration_lock);
606 
607 	/* Spread the word. */
608 	detach_driver_chain(port);
609 
610 #ifdef CONFIG_PARPORT_1284
611 	/* Forget the IEEE1284.3 topology of the port. */
612 	parport_daisy_fini(port);
613 	for (i = 1; i < 3; i++) {
614 		struct parport *slave = port->slaves[i-1];
615 		if (!slave)
616 			continue;
617 		detach_driver_chain(slave);
618 		parport_daisy_fini(slave);
619 	}
620 #endif
621 
622 	port->ops = &dead_ops;
623 	spin_lock(&parportlist_lock);
624 	list_del_init(&port->list);
625 	for (i = 1; i < 3; i++) {
626 		struct parport *slave = port->slaves[i-1];
627 		if (slave)
628 			list_del_init(&slave->list);
629 	}
630 	spin_unlock(&parportlist_lock);
631 
632 	mutex_unlock(&registration_lock);
633 
634 	parport_proc_unregister(port);
635 
636 	for (i = 1; i < 3; i++) {
637 		struct parport *slave = port->slaves[i-1];
638 		if (slave)
639 			parport_put_port(slave);
640 	}
641 }
642 EXPORT_SYMBOL(parport_remove_port);
643 
644 /**
645  *	parport_register_device - register a device on a parallel port
646  *	@port: port to which the device is attached
647  *	@name: a name to refer to the device
648  *	@pf: preemption callback
649  *	@kf: kick callback (wake-up)
650  *	@irq_func: interrupt handler
651  *	@flags: registration flags
652  *	@handle: data for callback functions
653  *
654  *	This function, called by parallel port device drivers,
655  *	declares that a device is connected to a port, and tells the
656  *	system all it needs to know.
657  *
658  *	The @name is allocated by the caller and must not be
659  *	deallocated until the caller calls @parport_unregister_device
660  *	for that device.
661  *
662  *	The preemption callback function, @pf, is called when this
663  *	device driver has claimed access to the port but another
664  *	device driver wants to use it.  It is given @handle as its
665  *	parameter, and should return zero if it is willing for the
666  *	system to release the port to another driver on its behalf.
667  *	If it wants to keep control of the port it should return
668  *	non-zero, and no action will be taken.  It is good manners for
669  *	the driver to try to release the port at the earliest
670  *	opportunity after its preemption callback rejects a preemption
671  *	attempt.  Note that if a preemption callback is happy for
672  *	preemption to go ahead, there is no need to release the port;
673  *	it is done automatically.  This function may not block, as it
674  *	may be called from interrupt context.  If the device driver
675  *	does not support preemption, @pf can be %NULL.
676  *
677  *	The wake-up ("kick") callback function, @kf, is called when
678  *	the port is available to be claimed for exclusive access; that
679  *	is, parport_claim() is guaranteed to succeed when called from
680  *	inside the wake-up callback function.  If the driver wants to
681  *	claim the port it should do so; otherwise, it need not take
682  *	any action.  This function may not block, as it may be called
683  *	from interrupt context.  If the device driver does not want to
684  *	be explicitly invited to claim the port in this way, @kf can
685  *	be %NULL.
686  *
687  *	The interrupt handler, @irq_func, is called when an interrupt
688  *	arrives from the parallel port.  Note that if a device driver
689  *	wants to use interrupts it should use parport_enable_irq(),
690  *	and can also check the irq member of the parport structure
691  *	representing the port.
692  *
693  *	The parallel port (lowlevel) driver is the one that has called
694  *	request_irq() and whose interrupt handler is called first.
695  *	This handler does whatever needs to be done to the hardware to
696  *	acknowledge the interrupt (for PC-style ports there is nothing
697  *	special to be done).  It then tells the IEEE 1284 code about
698  *	the interrupt, which may involve reacting to an IEEE 1284
699  *	event depending on the current IEEE 1284 phase.  After this,
700  *	it calls @irq_func.  Needless to say, @irq_func will be called
701  *	from interrupt context, and may not block.
702  *
703  *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
704  *	so should only be used when sharing the port with other device
705  *	drivers is impossible and would lead to incorrect behaviour.
706  *	Use it sparingly!  Normally, @flags will be zero.
707  *
708  *	This function returns a pointer to a structure that represents
709  *	the device on the port, or %NULL if there is not enough memory
710  *	to allocate space for that structure.
711  **/
712 
713 struct pardevice *
714 parport_register_device(struct parport *port, const char *name,
715 			int (*pf)(void *), void (*kf)(void *),
716 			void (*irq_func)(void *),
717 			int flags, void *handle)
718 {
719 	struct pardevice *tmp;
720 
721 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
722 		/* An exclusive device is registered. */
723 		printk(KERN_DEBUG "%s: no more devices allowed\n",
724 			port->name);
725 		return NULL;
726 	}
727 
728 	if (flags & PARPORT_DEV_LURK) {
729 		if (!pf || !kf) {
730 			printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
731 			return NULL;
732 		}
733 	}
734 
735 	if (flags & PARPORT_DEV_EXCL) {
736 		if (port->physport->devices) {
737 			/*
738 			 * If a device is already registered and this new
739 			 * device wants exclusive access, then no need to
740 			 * continue as we can not grant exclusive access to
741 			 * this device.
742 			 */
743 			pr_err("%s: cannot grant exclusive access for device %s\n",
744 			       port->name, name);
745 			return NULL;
746 		}
747 	}
748 
749 	/*
750 	 * We up our own module reference count, and that of the port
751 	 * on which a device is to be registered, to ensure that
752 	 * neither of us gets unloaded while we sleep in (e.g.)
753 	 * kmalloc.
754 	 */
755 	if (!try_module_get(port->ops->owner))
756 		return NULL;
757 
758 	parport_get_port(port);
759 
760 	tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
761 	if (!tmp)
762 		goto out;
763 
764 	tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
765 	if (!tmp->state)
766 		goto out_free_pardevice;
767 
768 	tmp->name = name;
769 	tmp->port = port;
770 	tmp->daisy = -1;
771 	tmp->preempt = pf;
772 	tmp->wakeup = kf;
773 	tmp->private = handle;
774 	tmp->flags = flags;
775 	tmp->irq_func = irq_func;
776 	tmp->waiting = 0;
777 	tmp->timeout = 5 * HZ;
778 	tmp->devmodel = false;
779 
780 	/* Chain this onto the list */
781 	tmp->prev = NULL;
782 	/*
783 	 * This function must not run from an irq handler so we don' t need
784 	 * to clear irq on the local CPU. -arca
785 	 */
786 	spin_lock(&port->physport->pardevice_lock);
787 
788 	if (flags & PARPORT_DEV_EXCL) {
789 		if (port->physport->devices) {
790 			spin_unlock(&port->physport->pardevice_lock);
791 			printk(KERN_DEBUG
792 				"%s: cannot grant exclusive access for device %s\n",
793 				port->name, name);
794 			goto out_free_all;
795 		}
796 		port->flags |= PARPORT_FLAG_EXCL;
797 	}
798 
799 	tmp->next = port->physport->devices;
800 	wmb(); /*
801 		* Make sure that tmp->next is written before it's
802 		* added to the list; see comments marked 'no locking
803 		* required'
804 		*/
805 	if (port->physport->devices)
806 		port->physport->devices->prev = tmp;
807 	port->physport->devices = tmp;
808 	spin_unlock(&port->physport->pardevice_lock);
809 
810 	init_waitqueue_head(&tmp->wait_q);
811 	tmp->timeslice = parport_default_timeslice;
812 	tmp->waitnext = tmp->waitprev = NULL;
813 
814 	/*
815 	 * This has to be run as last thing since init_state may need other
816 	 * pardevice fields. -arca
817 	 */
818 	port->ops->init_state(tmp, tmp->state);
819 	if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) {
820 		port->proc_device = tmp;
821 		parport_device_proc_register(tmp);
822 	}
823 	return tmp;
824 
825  out_free_all:
826 	kfree(tmp->state);
827  out_free_pardevice:
828 	kfree(tmp);
829  out:
830 	parport_put_port(port);
831 	module_put(port->ops->owner);
832 
833 	return NULL;
834 }
835 EXPORT_SYMBOL(parport_register_device);
836 
837 static void free_pardevice(struct device *dev)
838 {
839 	struct pardevice *par_dev = to_pardevice(dev);
840 
841 	kfree(par_dev->name);
842 	kfree(par_dev);
843 }
844 
845 struct pardevice *
846 parport_register_dev_model(struct parport *port, const char *name,
847 			   const struct pardev_cb *par_dev_cb, int id)
848 {
849 	struct pardevice *par_dev;
850 	int ret;
851 	char *devname;
852 
853 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
854 		/* An exclusive device is registered. */
855 		pr_err("%s: no more devices allowed\n", port->name);
856 		return NULL;
857 	}
858 
859 	if (par_dev_cb->flags & PARPORT_DEV_LURK) {
860 		if (!par_dev_cb->preempt || !par_dev_cb->wakeup) {
861 			pr_info("%s: refused to register lurking device (%s) without callbacks\n",
862 				port->name, name);
863 			return NULL;
864 		}
865 	}
866 
867 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
868 		if (port->physport->devices) {
869 			/*
870 			 * If a device is already registered and this new
871 			 * device wants exclusive access, then no need to
872 			 * continue as we can not grant exclusive access to
873 			 * this device.
874 			 */
875 			pr_err("%s: cannot grant exclusive access for device %s\n",
876 			       port->name, name);
877 			return NULL;
878 		}
879 	}
880 
881 	if (!try_module_get(port->ops->owner))
882 		return NULL;
883 
884 	parport_get_port(port);
885 
886 	par_dev = kzalloc(sizeof(*par_dev), GFP_KERNEL);
887 	if (!par_dev)
888 		goto err_put_port;
889 
890 	par_dev->state = kzalloc(sizeof(*par_dev->state), GFP_KERNEL);
891 	if (!par_dev->state)
892 		goto err_put_par_dev;
893 
894 	devname = kstrdup(name, GFP_KERNEL);
895 	if (!devname)
896 		goto err_free_par_dev;
897 
898 	par_dev->name = devname;
899 	par_dev->port = port;
900 	par_dev->daisy = -1;
901 	par_dev->preempt = par_dev_cb->preempt;
902 	par_dev->wakeup = par_dev_cb->wakeup;
903 	par_dev->private = par_dev_cb->private;
904 	par_dev->flags = par_dev_cb->flags;
905 	par_dev->irq_func = par_dev_cb->irq_func;
906 	par_dev->waiting = 0;
907 	par_dev->timeout = 5 * HZ;
908 
909 	par_dev->dev.parent = &port->bus_dev;
910 	par_dev->dev.bus = &parport_bus_type;
911 	ret = dev_set_name(&par_dev->dev, "%s.%d", devname, id);
912 	if (ret)
913 		goto err_free_devname;
914 	par_dev->dev.release = free_pardevice;
915 	par_dev->devmodel = true;
916 	ret = device_register(&par_dev->dev);
917 	if (ret) {
918 		kfree(par_dev->state);
919 		put_device(&par_dev->dev);
920 		goto err_put_port;
921 	}
922 
923 	/* Chain this onto the list */
924 	par_dev->prev = NULL;
925 	/*
926 	 * This function must not run from an irq handler so we don' t need
927 	 * to clear irq on the local CPU. -arca
928 	 */
929 	spin_lock(&port->physport->pardevice_lock);
930 
931 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
932 		if (port->physport->devices) {
933 			spin_unlock(&port->physport->pardevice_lock);
934 			pr_debug("%s: cannot grant exclusive access for device %s\n",
935 				 port->name, name);
936 			kfree(par_dev->state);
937 			device_unregister(&par_dev->dev);
938 			goto err_put_port;
939 		}
940 		port->flags |= PARPORT_FLAG_EXCL;
941 	}
942 
943 	par_dev->next = port->physport->devices;
944 	wmb();	/*
945 		 * Make sure that tmp->next is written before it's
946 		 * added to the list; see comments marked 'no locking
947 		 * required'
948 		 */
949 	if (port->physport->devices)
950 		port->physport->devices->prev = par_dev;
951 	port->physport->devices = par_dev;
952 	spin_unlock(&port->physport->pardevice_lock);
953 
954 	init_waitqueue_head(&par_dev->wait_q);
955 	par_dev->timeslice = parport_default_timeslice;
956 	par_dev->waitnext = NULL;
957 	par_dev->waitprev = NULL;
958 
959 	/*
960 	 * This has to be run as last thing since init_state may need other
961 	 * pardevice fields. -arca
962 	 */
963 	port->ops->init_state(par_dev, par_dev->state);
964 	if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) {
965 		port->proc_device = par_dev;
966 		parport_device_proc_register(par_dev);
967 	}
968 
969 	return par_dev;
970 
971 err_free_devname:
972 	kfree(devname);
973 err_free_par_dev:
974 	kfree(par_dev->state);
975 err_put_par_dev:
976 	if (!par_dev->devmodel)
977 		kfree(par_dev);
978 err_put_port:
979 	parport_put_port(port);
980 	module_put(port->ops->owner);
981 
982 	return NULL;
983 }
984 EXPORT_SYMBOL(parport_register_dev_model);
985 
986 /**
987  *	parport_unregister_device - deregister a device on a parallel port
988  *	@dev: pointer to structure representing device
989  *
990  *	This undoes the effect of parport_register_device().
991  **/
992 
993 void parport_unregister_device(struct pardevice *dev)
994 {
995 	struct parport *port;
996 
997 #ifdef PARPORT_PARANOID
998 	if (!dev) {
999 		printk(KERN_ERR "parport_unregister_device: passed NULL\n");
1000 		return;
1001 	}
1002 #endif
1003 
1004 	port = dev->port->physport;
1005 
1006 	if (port->proc_device == dev) {
1007 		port->proc_device = NULL;
1008 		clear_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags);
1009 		parport_device_proc_unregister(dev);
1010 	}
1011 
1012 	if (port->cad == dev) {
1013 		printk(KERN_DEBUG "%s: %s forgot to release port\n",
1014 		       port->name, dev->name);
1015 		parport_release(dev);
1016 	}
1017 
1018 	spin_lock(&port->pardevice_lock);
1019 	if (dev->next)
1020 		dev->next->prev = dev->prev;
1021 	if (dev->prev)
1022 		dev->prev->next = dev->next;
1023 	else
1024 		port->devices = dev->next;
1025 
1026 	if (dev->flags & PARPORT_DEV_EXCL)
1027 		port->flags &= ~PARPORT_FLAG_EXCL;
1028 
1029 	spin_unlock(&port->pardevice_lock);
1030 
1031 	/*
1032 	 * Make sure we haven't left any pointers around in the wait
1033 	 * list.
1034 	 */
1035 	spin_lock_irq(&port->waitlist_lock);
1036 	if (dev->waitprev || dev->waitnext || port->waithead == dev) {
1037 		if (dev->waitprev)
1038 			dev->waitprev->waitnext = dev->waitnext;
1039 		else
1040 			port->waithead = dev->waitnext;
1041 		if (dev->waitnext)
1042 			dev->waitnext->waitprev = dev->waitprev;
1043 		else
1044 			port->waittail = dev->waitprev;
1045 	}
1046 	spin_unlock_irq(&port->waitlist_lock);
1047 
1048 	kfree(dev->state);
1049 	if (dev->devmodel)
1050 		device_unregister(&dev->dev);
1051 	else
1052 		kfree(dev);
1053 
1054 	module_put(port->ops->owner);
1055 	parport_put_port(port);
1056 }
1057 EXPORT_SYMBOL(parport_unregister_device);
1058 
1059 /**
1060  *	parport_find_number - find a parallel port by number
1061  *	@number: parallel port number
1062  *
1063  *	This returns the parallel port with the specified number, or
1064  *	%NULL if there is none.
1065  *
1066  *	There is an implicit parport_get_port() done already; to throw
1067  *	away the reference to the port that parport_find_number()
1068  *	gives you, use parport_put_port().
1069  */
1070 
1071 struct parport *parport_find_number(int number)
1072 {
1073 	struct parport *port, *result = NULL;
1074 
1075 	if (list_empty(&portlist))
1076 		get_lowlevel_driver();
1077 
1078 	spin_lock(&parportlist_lock);
1079 	list_for_each_entry(port, &portlist, list) {
1080 		if (port->number == number) {
1081 			result = parport_get_port(port);
1082 			break;
1083 		}
1084 	}
1085 	spin_unlock(&parportlist_lock);
1086 	return result;
1087 }
1088 EXPORT_SYMBOL(parport_find_number);
1089 
1090 /**
1091  *	parport_find_base - find a parallel port by base address
1092  *	@base: base I/O address
1093  *
1094  *	This returns the parallel port with the specified base
1095  *	address, or %NULL if there is none.
1096  *
1097  *	There is an implicit parport_get_port() done already; to throw
1098  *	away the reference to the port that parport_find_base()
1099  *	gives you, use parport_put_port().
1100  */
1101 
1102 struct parport *parport_find_base(unsigned long base)
1103 {
1104 	struct parport *port, *result = NULL;
1105 
1106 	if (list_empty(&portlist))
1107 		get_lowlevel_driver();
1108 
1109 	spin_lock(&parportlist_lock);
1110 	list_for_each_entry(port, &portlist, list) {
1111 		if (port->base == base) {
1112 			result = parport_get_port(port);
1113 			break;
1114 		}
1115 	}
1116 	spin_unlock(&parportlist_lock);
1117 	return result;
1118 }
1119 EXPORT_SYMBOL(parport_find_base);
1120 
1121 /**
1122  *	parport_claim - claim access to a parallel port device
1123  *	@dev: pointer to structure representing a device on the port
1124  *
1125  *	This function will not block and so can be used from interrupt
1126  *	context.  If parport_claim() succeeds in claiming access to
1127  *	the port it returns zero and the port is available to use.  It
1128  *	may fail (returning non-zero) if the port is in use by another
1129  *	driver and that driver is not willing to relinquish control of
1130  *	the port.
1131  **/
1132 
1133 int parport_claim(struct pardevice *dev)
1134 {
1135 	struct pardevice *oldcad;
1136 	struct parport *port = dev->port->physport;
1137 	unsigned long flags;
1138 
1139 	if (port->cad == dev) {
1140 		printk(KERN_INFO "%s: %s already owner\n",
1141 		       dev->port->name,dev->name);
1142 		return 0;
1143 	}
1144 
1145 	/* Preempt any current device */
1146 	write_lock_irqsave(&port->cad_lock, flags);
1147 	oldcad = port->cad;
1148 	if (oldcad) {
1149 		if (oldcad->preempt) {
1150 			if (oldcad->preempt(oldcad->private))
1151 				goto blocked;
1152 			port->ops->save_state(port, dev->state);
1153 		} else
1154 			goto blocked;
1155 
1156 		if (port->cad != oldcad) {
1157 			/*
1158 			 * I think we'll actually deadlock rather than
1159 			 * get here, but just in case..
1160 			 */
1161 			printk(KERN_WARNING
1162 			       "%s: %s released port when preempted!\n",
1163 			       port->name, oldcad->name);
1164 			if (port->cad)
1165 				goto blocked;
1166 		}
1167 	}
1168 
1169 	/* Can't fail from now on, so mark ourselves as no longer waiting.  */
1170 	if (dev->waiting & 1) {
1171 		dev->waiting = 0;
1172 
1173 		/* Take ourselves out of the wait list again.  */
1174 		spin_lock_irq(&port->waitlist_lock);
1175 		if (dev->waitprev)
1176 			dev->waitprev->waitnext = dev->waitnext;
1177 		else
1178 			port->waithead = dev->waitnext;
1179 		if (dev->waitnext)
1180 			dev->waitnext->waitprev = dev->waitprev;
1181 		else
1182 			port->waittail = dev->waitprev;
1183 		spin_unlock_irq(&port->waitlist_lock);
1184 		dev->waitprev = dev->waitnext = NULL;
1185 	}
1186 
1187 	/* Now we do the change of devices */
1188 	port->cad = dev;
1189 
1190 #ifdef CONFIG_PARPORT_1284
1191 	/* If it's a mux port, select it. */
1192 	if (dev->port->muxport >= 0) {
1193 		/* FIXME */
1194 		port->muxsel = dev->port->muxport;
1195 	}
1196 
1197 	/* If it's a daisy chain device, select it. */
1198 	if (dev->daisy >= 0) {
1199 		/* This could be lazier. */
1200 		if (!parport_daisy_select(port, dev->daisy,
1201 					   IEEE1284_MODE_COMPAT))
1202 			port->daisy = dev->daisy;
1203 	}
1204 #endif /* IEEE1284.3 support */
1205 
1206 	/* Restore control registers */
1207 	port->ops->restore_state(port, dev->state);
1208 	write_unlock_irqrestore(&port->cad_lock, flags);
1209 	dev->time = jiffies;
1210 	return 0;
1211 
1212 blocked:
1213 	/*
1214 	 * If this is the first time we tried to claim the port, register an
1215 	 * interest.  This is only allowed for devices sleeping in
1216 	 * parport_claim_or_block(), or those with a wakeup function.
1217 	 */
1218 
1219 	/* The cad_lock is still held for writing here */
1220 	if (dev->waiting & 2 || dev->wakeup) {
1221 		spin_lock(&port->waitlist_lock);
1222 		if (test_and_set_bit(0, &dev->waiting) == 0) {
1223 			/* First add ourselves to the end of the wait list. */
1224 			dev->waitnext = NULL;
1225 			dev->waitprev = port->waittail;
1226 			if (port->waittail) {
1227 				port->waittail->waitnext = dev;
1228 				port->waittail = dev;
1229 			} else
1230 				port->waithead = port->waittail = dev;
1231 		}
1232 		spin_unlock(&port->waitlist_lock);
1233 	}
1234 	write_unlock_irqrestore(&port->cad_lock, flags);
1235 	return -EAGAIN;
1236 }
1237 EXPORT_SYMBOL(parport_claim);
1238 
1239 /**
1240  *	parport_claim_or_block - claim access to a parallel port device
1241  *	@dev: pointer to structure representing a device on the port
1242  *
1243  *	This behaves like parport_claim(), but will block if necessary
1244  *	to wait for the port to be free.  A return value of 1
1245  *	indicates that it slept; 0 means that it succeeded without
1246  *	needing to sleep.  A negative error code indicates failure.
1247  **/
1248 
1249 int parport_claim_or_block(struct pardevice *dev)
1250 {
1251 	int r;
1252 
1253 	/*
1254 	 * Signal to parport_claim() that we can wait even without a
1255 	 * wakeup function.
1256 	 */
1257 	dev->waiting = 2;
1258 
1259 	/* Try to claim the port.  If this fails, we need to sleep.  */
1260 	r = parport_claim(dev);
1261 	if (r == -EAGAIN) {
1262 #ifdef PARPORT_DEBUG_SHARING
1263 		printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
1264 #endif
1265 		/*
1266 		 * FIXME!!! Use the proper locking for dev->waiting,
1267 		 * and make this use the "wait_event_interruptible()"
1268 		 * interfaces. The cli/sti that used to be here
1269 		 * did nothing.
1270 		 *
1271 		 * See also parport_release()
1272 		 */
1273 
1274 		/*
1275 		 * If dev->waiting is clear now, an interrupt
1276 		 * gave us the port and we would deadlock if we slept.
1277 		 */
1278 		if (dev->waiting) {
1279 			wait_event_interruptible(dev->wait_q,
1280 						 !dev->waiting);
1281 			if (signal_pending(current))
1282 				return -EINTR;
1283 			r = 1;
1284 		} else {
1285 			r = 0;
1286 #ifdef PARPORT_DEBUG_SHARING
1287 			printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
1288 			       dev->name);
1289 #endif
1290 		}
1291 
1292 #ifdef PARPORT_DEBUG_SHARING
1293 		if (dev->port->physport->cad != dev)
1294 			printk(KERN_DEBUG "%s: exiting parport_claim_or_block but %s owns port!\n",
1295 			       dev->name, dev->port->physport->cad ?
1296 			       dev->port->physport->cad->name:"nobody");
1297 #endif
1298 	}
1299 	dev->waiting = 0;
1300 	return r;
1301 }
1302 EXPORT_SYMBOL(parport_claim_or_block);
1303 
1304 /**
1305  *	parport_release - give up access to a parallel port device
1306  *	@dev: pointer to structure representing parallel port device
1307  *
1308  *	This function cannot fail, but it should not be called without
1309  *	the port claimed.  Similarly, if the port is already claimed
1310  *	you should not try claiming it again.
1311  **/
1312 
1313 void parport_release(struct pardevice *dev)
1314 {
1315 	struct parport *port = dev->port->physport;
1316 	struct pardevice *pd;
1317 	unsigned long flags;
1318 
1319 	/* Make sure that dev is the current device */
1320 	write_lock_irqsave(&port->cad_lock, flags);
1321 	if (port->cad != dev) {
1322 		write_unlock_irqrestore(&port->cad_lock, flags);
1323 		printk(KERN_WARNING "%s: %s tried to release parport when not owner\n",
1324 		       port->name, dev->name);
1325 		return;
1326 	}
1327 
1328 #ifdef CONFIG_PARPORT_1284
1329 	/* If this is on a mux port, deselect it. */
1330 	if (dev->port->muxport >= 0) {
1331 		/* FIXME */
1332 		port->muxsel = -1;
1333 	}
1334 
1335 	/* If this is a daisy device, deselect it. */
1336 	if (dev->daisy >= 0) {
1337 		parport_daisy_deselect_all(port);
1338 		port->daisy = -1;
1339 	}
1340 #endif
1341 
1342 	port->cad = NULL;
1343 	write_unlock_irqrestore(&port->cad_lock, flags);
1344 
1345 	/* Save control registers */
1346 	port->ops->save_state(port, dev->state);
1347 
1348 	/*
1349 	 * If anybody is waiting, find out who's been there longest and
1350 	 * then wake them up. (Note: no locking required)
1351 	 */
1352 	/* !!! LOCKING IS NEEDED HERE */
1353 	for (pd = port->waithead; pd; pd = pd->waitnext) {
1354 		if (pd->waiting & 2) { /* sleeping in claim_or_block */
1355 			parport_claim(pd);
1356 			if (waitqueue_active(&pd->wait_q))
1357 				wake_up_interruptible(&pd->wait_q);
1358 			return;
1359 		} else if (pd->wakeup) {
1360 			pd->wakeup(pd->private);
1361 			if (dev->port->cad) /* racy but no matter */
1362 				return;
1363 		} else {
1364 			printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
1365 		}
1366 	}
1367 
1368 	/*
1369 	 * Nobody was waiting, so walk the list to see if anyone is
1370 	 * interested in being woken up. (Note: no locking required)
1371 	 */
1372 	/* !!! LOCKING IS NEEDED HERE */
1373 	for (pd = port->devices; !port->cad && pd; pd = pd->next) {
1374 		if (pd->wakeup && pd != dev)
1375 			pd->wakeup(pd->private);
1376 	}
1377 }
1378 EXPORT_SYMBOL(parport_release);
1379 
1380 irqreturn_t parport_irq_handler(int irq, void *dev_id)
1381 {
1382 	struct parport *port = dev_id;
1383 
1384 	parport_generic_irq(port);
1385 
1386 	return IRQ_HANDLED;
1387 }
1388 EXPORT_SYMBOL(parport_irq_handler);
1389 
1390 MODULE_LICENSE("GPL");
1391