⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 share.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
 *	If there is no memory to allocate a new parport structure, *	this function will return %NULL. **/struct parport *parport_register_port(unsigned long base, int irq, int dma,				      struct parport_operations *ops){	struct parport *tmp;	int portnum;	int device;	char *name;	tmp = kmalloc(sizeof(struct parport), GFP_KERNEL);	if (!tmp) {		printk(KERN_WARNING "parport: memory squeeze\n");		return NULL;	}	/* Search for the lowest free parport number. */	spin_lock_irq (&parportlist_lock);	for (portnum = 0; ; portnum++) {		struct parport *itr = portlist;		while (itr) {			if (itr->number == portnum)				/* No good, already used. */				break;			else				itr = itr->next;		}		if (itr == NULL)			/* Got to the end of the list. */			break;	}	spin_unlock_irq (&parportlist_lock);		/* Init our structure */ 	memset(tmp, 0, sizeof(struct parport));	tmp->base = base;	tmp->irq = irq;	tmp->dma = dma;	tmp->muxport = tmp->daisy = tmp->muxsel = -1;	tmp->modes = 0; 	tmp->next = NULL;	tmp->devices = tmp->cad = NULL;	tmp->flags = 0;	tmp->ops = ops;	tmp->portnum = tmp->number = portnum;	tmp->physport = tmp;	memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info));	tmp->cad_lock = RW_LOCK_UNLOCKED;	spin_lock_init(&tmp->waitlist_lock);	spin_lock_init(&tmp->pardevice_lock);	tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;	tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;	init_MUTEX_LOCKED (&tmp->ieee1284.irq); /* actually a semaphore at 0 */	tmp->spintime = parport_default_spintime;	atomic_set (&tmp->ref_count, 1);	name = kmalloc(15, GFP_KERNEL);	if (!name) {		printk(KERN_ERR "parport: memory squeeze\n");		kfree(tmp);		return NULL;	}	sprintf(name, "parport%d", portnum);	tmp->name = name;	/*	 * Chain the entry to our list.	 *	 * This function must not run from an irq handler so we don' t need	 * to clear irq on the local CPU. -arca	 */	spin_lock(&parportlist_lock);	/* We are locked against anyone else performing alterations, but	 * because of parport_enumerate people can still _read_ the list	 * while we are changing it; so be careful..	 *	 * It's okay to have portlist_tail a little bit out of sync	 * since it's only used for changing the list, not for reading	 * from it.	 */	if (portlist_tail)		portlist_tail->next = tmp;	portlist_tail = tmp;	if (!portlist)		portlist = tmp;	spin_unlock(&parportlist_lock);	for (device = 0; device < 5; device++)		/* assume the worst */		tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;	tmp->waithead = tmp->waittail = NULL;	return tmp;}/** *	parport_announce_port - tell device drivers about a parallel port *	@port: parallel port to announce * *	After a port driver has registered a parallel port with *	parport_register_port, and performed any necessary *	initialisation or adjustments, it should call *	parport_announce_port() in order to notify all device drivers *	that have called parport_register_driver().  Their attach() *	functions will be called, with @port as the parameter. **/void parport_announce_port (struct parport *port){#ifdef CONFIG_PARPORT_1284	/* Analyse the IEEE1284.3 topology of the port. */	if (parport_daisy_init (port) == 0) {		/* No devices were detected.  Perhaps they are in some                   funny state; let's try to reset them and see if                   they wake up. */		parport_daisy_fini (port);		parport_write_control (port, PARPORT_CONTROL_SELECT);		udelay (50);		parport_write_control (port,				       PARPORT_CONTROL_SELECT |				       PARPORT_CONTROL_INIT);		udelay (50);		parport_daisy_init (port);	}#endif	/* Let drivers know that a new port has arrived. */	attach_driver_chain (port);}/** *	parport_unregister_port - deregister a parallel port *	@port: parallel port to deregister * *	When a parallel port driver is forcibly unloaded, or a *	parallel port becomes inaccessible, the port driver must call *	this function in order to deal with device drivers that still *	want to use it. * *	The parport structure associated with the port has its *	operations structure replaced with one containing 'null' *	operations that return errors or just don't do anything. * *	Any drivers that have registered themselves using *	parport_register_driver() are notified that the port is no *	longer accessible by having their detach() routines called *	with @port as the parameter. **/void parport_unregister_port(struct parport *port){	struct parport *p;	port->ops = &dead_ops;	/* Spread the word. */	detach_driver_chain (port);#ifdef CONFIG_PARPORT_1284	/* Forget the IEEE1284.3 topology of the port. */	parport_daisy_fini (port);#endif	spin_lock(&parportlist_lock);	/* We are protected from other people changing the list, but	 * they can still see it (using parport_enumerate).  So be	 * careful about the order of writes.. */	if (portlist == port) {		if ((portlist = port->next) == NULL)			portlist_tail = NULL;	} else {		for (p = portlist; (p != NULL) && (p->next != port); 		     p=p->next);		if (p) {			if ((p->next = port->next) == NULL)				portlist_tail = p;		}		else printk (KERN_WARNING			     "%s not found in port list!\n", port->name);	}	spin_unlock(&parportlist_lock);	/* Yes, parport_enumerate _is_ unsafe.  Don't use it. */	parport_put_port (port);}/** *	parport_register_device - register a device on a parallel port *	@port: port to which the device is attached *	@name: a name to refer to the device *	@pf: preemption callback *	@kf: kick callback (wake-up) *	@irq_func: interrupt handler *	@flags: registration flags *	@handle: data for callback functions * *	This function, called by parallel port device drivers, *	declares that a device is connected to a port, and tells the *	system all it needs to know. * *	The @name is allocated by the caller and must not be *	deallocated until the caller calls @parport_unregister_device *	for that device. * *	The preemption callback function, @pf, is called when this *	device driver has claimed access to the port but another *	device driver wants to use it.  It is given @handle as its *	parameter, and should return zero if it is willing for the *	system to release the port to another driver on its behalf. *	If it wants to keep control of the port it should return *	non-zero, and no action will be taken.  It is good manners for *	the driver to try to release the port at the earliest *	opportunity after its preemption callback rejects a preemption *	attempt.  Note that if a preemption callback is happy for *	preemption to go ahead, there is no need to release the port; *	it is done automatically.  This function may not block, as it *	may be called from interrupt context.  If the device driver *	does not support preemption, @pf can be %NULL. * *	The wake-up ("kick") callback function, @kf, is called when *	the port is available to be claimed for exclusive access; that *	is, parport_claim() is guaranteed to succeed when called from *	inside the wake-up callback function.  If the driver wants to *	claim the port it should do so; otherwise, it need not take *	any action.  This function may not block, as it may be called *	from interrupt context.  If the device driver does not want to *	be explicitly invited to claim the port in this way, @kf can *	be %NULL. * *	The interrupt handler, @irq_func, is called when an interrupt *	arrives from the parallel port.  Note that if a device driver *	wants to use interrupts it should use parport_enable_irq(), *	and can also check the irq member of the parport structure *	representing the port. * *	The parallel port (lowlevel) driver is the one that has called *	request_irq() and whose interrupt handler is called first. *	This handler does whatever needs to be done to the hardware to *	acknowledge the interrupt (for PC-style ports there is nothing *	special to be done).  It then tells the IEEE 1284 code about *	the interrupt, which may involve reacting to an IEEE 1284 *	event depending on the current IEEE 1284 phase.  After this, *	it calls @irq_func.  Needless to say, @irq_func will be called *	from interrupt context, and may not block. * *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and *	so should only be used when sharing the port with other device *	drivers is impossible and would lead to incorrect behaviour. *	Use it sparingly!  Normally, @flags will be zero. * *	This function returns a pointer to a structure that represents *	the device on the port, or %NULL if there is not enough memory *	to allocate space for that structure. **/struct pardevice *parport_register_device(struct parport *port, const char *name,			int (*pf)(void *), void (*kf)(void *),			void (*irq_func)(int, void *, struct pt_regs *), 			int flags, void *handle){	struct pardevice *tmp;	if (port->physport->flags & PARPORT_FLAG_EXCL) {		/* An exclusive device is registered. */		printk (KERN_DEBUG "%s: no more devices allowed\n",			port->name);		return NULL;	}	if (flags & PARPORT_DEV_LURK) {		if (!pf || !kf) {			printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);			return NULL;		}	}	/* We up our own module reference count, and that of the port           on which a device is to be registered, to ensure that           neither of us gets unloaded while we sleep in (e.g.)           kmalloc.  To be absolutely safe, we have to require that           our caller doesn't sleep in between parport_enumerate and           parport_register_device.. */	inc_parport_count();	port->ops->inc_use_count();	parport_get_port (port);	tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);	if (tmp == NULL) {		printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);		goto out;	}	tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);	if (tmp->state == NULL) {		printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);		goto out_free_pardevice;	}	tmp->name = name;	tmp->port = port;	tmp->daisy = -1;	tmp->preempt = pf;	tmp->wakeup = kf;	tmp->private = handle;	tmp->flags = flags;	tmp->irq_func = irq_func;	tmp->waiting = 0;	tmp->timeout = 5 * HZ;	/* Chain this onto the list */	tmp->prev = NULL;	/*	 * This function must not run from an irq handler so we don' t need	 * to clear irq on the local CPU. -arca	 */	spin_lock(&port->physport->pardevice_lock);	if (flags & PARPORT_DEV_EXCL) {		if (port->physport->devices) {			spin_unlock (&port->physport->pardevice_lock);			printk (KERN_DEBUG				"%s: cannot grant exclusive access for "				"device %s\n", port->name, name);			goto out_free_all;		}		port->flags |= PARPORT_FLAG_EXCL;	}	tmp->next = port->physport->devices;	wmb(); /* Make sure that tmp->next is written before it's                  added to the list; see comments marked 'no locking                  required' */	if (port->physport->devices)		port->physport->devices->prev = tmp;	port->physport->devices = tmp;	spin_unlock(&port->physport->pardevice_lock);	init_waitqueue_head(&tmp->wait_q);	tmp->timeslice = parport_default_timeslice;	tmp->waitnext = tmp->waitprev = NULL;	/*	 * This has to be run as last thing since init_state may need other	 * pardevice fields. -arca	 */	port->ops->init_state(tmp, tmp->state);	parport_device_proc_register(tmp);	return tmp; out_free_all:	kfree (tmp->state); out_free_pardevice:	kfree (tmp); out:	dec_parport_count();	port->ops->dec_use_count();	parport_put_port (port);	return NULL;}/** *	parport_unregister_device - deregister a device on a parallel port *	@dev: pointer to structure representing device

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -