aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/base/driver.c
blob: 8ae0f63602e0730a71647cb571cbf933d76ceec8 (plain) (tree)
1
2
3
4
5
6
7




                                                    

                                                         




                                        





                         
                                                       
 
                                             







                                                      

 
   




                                                                   
  
                                                                    
   

                                                                           
 
                            
                           

                      


                               
                                                        
                                                                     
                                                 
                                      
                            

                     
                                          
 
   
                                                                         
                            











                                                                        


                                                                               






                            
                                                        
                                                                       








                                                        


                                                     
   

                                                     

                  
                
                                                                      
            


                                
                                      

   


                                                     
   

                                                      
 
                
                                                              
 
                                      

   
                                                             


                                             









                                                                        
                







                                                 


                                                           



                                   

                                                 
   
                                                           
 








                                                  
 
                              

   

                                            
   
                                          
 
                                   
 
                              
 







                                                             
                                                                             

                                                
                                                                         














                                                                      
                                                                     

 
   

                                             
  


                                                             
   
                                              
 
                
                                    
 

                             

                                                


                                                                              








                                                                            






                                                  
 
                                   
 
   

                                                 
  
                                                             
   
                                                 
 



                                                           
                                               
                               
 
                                     

   


                                                    
  

                                                          
  
                                                               


                                                                         
                                                                      





                                    

                    
                               
/*
 * driver.c - centralized device driver management
 *
 * Copyright (c) 2002-3 Patrick Mochel
 * Copyright (c) 2002-3 Open Source Development Labs
 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
 * Copyright (c) 2007 Novell Inc.
 *
 * This file is released under the GPLv2
 *
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/string.h>
#include "base.h"

static struct device *next_device(struct klist_iter *i)
{
	struct klist_node *n = klist_next(i);
	struct device *dev = NULL;
	struct device_private *dev_prv;

	if (n) {
		dev_prv = to_device_private_driver(n);
		dev = dev_prv->device;
	}
	return dev;
}

/**
 * driver_for_each_device - Iterator for devices bound to a driver.
 * @drv: Driver we're iterating.
 * @start: Device to begin with
 * @data: Data to pass to the callback.
 * @fn: Function to call for each device.
 *
 * Iterate over the @drv's list of devices calling @fn for each one.
 */
int driver_for_each_device(struct device_driver *drv, struct device *start,
			   void *data, int (*fn)(struct device *, void *))
{
	struct klist_iter i;
	struct device *dev;
	int error = 0;

	if (!drv)
		return -EINVAL;

	klist_iter_init_node(&drv->p->klist_devices, &i,
			     start ? &start->p->knode_driver : NULL);
	while ((dev = next_device(&i)) && !error)
		error = fn(dev, data);
	klist_iter_exit(&i);
	return error;
}
EXPORT_SYMBOL_GPL(driver_for_each_device);

/**
 * driver_find_device - device iterator for locating a particular device.
 * @drv: The device's driver
 * @start: Device to begin with
 * @data: Data to pass to match function
 * @match: Callback function to check device
 *
 * This is similar to the driver_for_each_device() function above, but
 * it returns a reference to a device that is 'found' for later use, as
 * determined by the @match callback.
 *
 * The callback should return 0 if the device doesn't match and non-zero
 * if it does.  If the callback returns non-zero, this function will
 * return to the caller and not iterate over any more devices.
 */
struct device *driver_find_device(struct device_driver *drv,
				  struct device *start, void *data,
				  int (*match)(struct device *dev, void *data))
{
	struct klist_iter i;
	struct device *dev;

	if (!drv)
		return NULL;

	klist_iter_init_node(&drv->p->klist_devices, &i,
			     (start ? &start->p->knode_driver : NULL));
	while ((dev = next_device(&i)))
		if (match(dev, data) && get_device(dev))
			break;
	klist_iter_exit(&i);
	return dev;
}
EXPORT_SYMBOL_GPL(driver_find_device);

/**
 * driver_create_file - create sysfs file for driver.
 * @drv: driver.
 * @attr: driver attribute descriptor.
 */
int driver_create_file(struct device_driver *drv,
		       struct driver_attribute *attr)
{
	int error;
	if (drv)
		error = sysfs_create_file(&drv->p->kobj, &attr->attr);
	else
		error = -EINVAL;
	return error;
}
EXPORT_SYMBOL_GPL(driver_create_file);

/**
 * driver_remove_file - remove sysfs file for driver.
 * @drv: driver.
 * @attr: driver attribute descriptor.
 */
void driver_remove_file(struct device_driver *drv,
			struct driver_attribute *attr)
{
	if (drv)
		sysfs_remove_file(&drv->p->kobj, &attr->attr);
}
EXPORT_SYMBOL_GPL(driver_remove_file);

/**
 * driver_add_kobj - add a kobject below the specified driver
 * @drv: requesting device driver
 * @kobj: kobject to add below this driver
 * @fmt: format string that names the kobject
 *
 * You really don't want to do this, this is only here due to one looney
 * iseries driver, go poke those developers if you are annoyed about
 * this...
 */
int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
		    const char *fmt, ...)
{
	va_list args;
	char *name;
	int ret;

	va_start(args, fmt);
	name = kvasprintf(GFP_KERNEL, fmt, args);
	va_end(args);

	if (!name)
		return -ENOMEM;

	ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
	kfree(name);
	return ret;
}
EXPORT_SYMBOL_GPL(driver_add_kobj);

/**
 * get_driver - increment driver reference count.
 * @drv: driver.
 */
struct device_driver *get_driver(struct device_driver *drv)
{
	if (drv) {
		struct driver_private *priv;
		struct kobject *kobj;

		kobj = kobject_get(&drv->p->kobj);
		priv = to_driver(kobj);
		return priv->driver;
	}
	return NULL;
}
EXPORT_SYMBOL_GPL(get_driver);

/**
 * put_driver - decrement driver's refcount.
 * @drv: driver.
 */
void put_driver(struct device_driver *drv)
{
	kobject_put(&drv->p->kobj);
}
EXPORT_SYMBOL_GPL(put_driver);

static int driver_add_groups(struct device_driver *drv,
			     struct attribute_group **groups)
{
	int error = 0;
	int i;

	if (groups) {
		for (i = 0; groups[i]; i++) {
			error = sysfs_create_group(&drv->p->kobj, groups[i]);
			if (error) {
				while (--i >= 0)
					sysfs_remove_group(&drv->p->kobj,
							   groups[i]);
				break;
			}
		}
	}
	return error;
}

static void driver_remove_groups(struct device_driver *drv,
				 struct attribute_group **groups)
{
	int i;

	if (groups)
		for (i = 0; groups[i]; i++)
			sysfs_remove_group(&drv->p->kobj, groups[i]);
}

/**
 * driver_register - register driver with bus
 * @drv: driver to register
 *
 * We pass off most of the work to the bus_add_driver() call,
 * since most of the things we have to do deal with the bus
 * structures.
 */
int driver_register(struct device_driver *drv)
{
	int ret;
	struct device_driver *other;

	BUG_ON(!drv->bus->p);

	if ((drv->bus->probe && drv->probe) ||
	    (drv->bus->remove && drv->remove) ||
	    (drv->bus->shutdown && drv->shutdown))
		printk(KERN_WARNING "Driver '%s' needs updating - please use "
			"bus_type methods\n", drv->name);

	other = driver_find(drv->name, drv->bus);
	if (other) {
		put_driver(other);
		printk(KERN_ERR "Error: Driver '%s' is already registered, "
			"aborting...\n", drv->name);
		return -EEXIST;
	}

	ret = bus_add_driver(drv);
	if (ret)
		return ret;
	ret = driver_add_groups(drv, drv->groups);
	if (ret)
		bus_remove_driver(drv);
	return ret;
}
EXPORT_SYMBOL_GPL(driver_register);

/**
 * driver_unregister - remove driver from system.
 * @drv: driver.
 *
 * Again, we pass off most of the work to the bus-level call.
 */
void driver_unregister(struct device_driver *drv)
{
	if (!drv || !drv->p) {
		WARN(1, "Unexpected driver unregister!\n");
		return;
	}
	driver_remove_groups(drv, drv->groups);
	bus_remove_driver(drv);
}
EXPORT_SYMBOL_GPL(driver_unregister);

/**
 * driver_find - locate driver on a bus by its name.
 * @name: name of the driver.
 * @bus: bus to scan for the driver.
 *
 * Call kset_find_obj() to iterate over list of drivers on
 * a bus to find driver by name. Return driver if found.
 *
 * Note that kset_find_obj increments driver's reference count.
 */
struct device_driver *driver_find(const char *name, struct bus_type *bus)
{
	struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
	struct driver_private *priv;

	if (k) {
		priv = to_driver(k);
		return priv->driver;
	}
	return NULL;
}
EXPORT_SYMBOL_GPL(driver_find);
eout for normal commands */ int long_timeout; /* timeout for commands known to take long time*/ /* Mode characteristics */ struct st_modedef modes[ST_NBR_MODES]; int current_mode; /* Status variables */ int partition; int new_partition; int nbr_partitions; /* zero until partition support enabled */ struct st_partstat ps[ST_NBR_PARTITIONS]; unsigned char dirty; unsigned char ready; unsigned char write_prot; unsigned char drv_write_prot; unsigned char in_use; unsigned char blksize_changed; unsigned char density_changed; unsigned char compression_changed; unsigned char drv_buffer; unsigned char density; unsigned char door_locked; unsigned char rew_at_close; unsigned char inited; int block_size; int min_block; int max_block; int recover_count; /* from tape opening */ int abort_count; int write_count; int read_count; int recover_erreg; /* from last status call */ /* * OnStream specific data */ int os_fw_rev; /* the firmware revision * 10000 */ unsigned char raw; /* flag OnStream raw access (32.5KB block size) */ unsigned char poll; /* flag that this drive needs polling (IDE|firmware) */ unsigned char frame_in_buffer; /* flag that the frame as per frame_seq_number * has been read into STp->buffer and is valid */ int frame_seq_number; /* logical frame number */ int logical_blk_num; /* logical block number */ unsigned first_frame_position; /* physical frame to be transferred to/from host */ unsigned last_frame_position; /* physical frame to be transferd to/from tape */ int cur_frames; /* current number of frames in internal buffer */ int max_frames; /* max number of frames in internal buffer */ char application_sig[5]; /* application signature */ unsigned char fast_open; /* flag that reminds us we didn't check headers at open */ unsigned short wrt_pass_cntr; /* write pass counter */ int update_frame_cntr; /* update frame counter */ int onstream_write_error; /* write error recovery active */ int header_ok; /* header frame verified ok */ int linux_media; /* reading linux-specifc media */ int linux_media_version; os_header_t * header_cache; /* cache is kept for filemark positions */ int filemark_cnt; int first_mark_ppos; int last_mark_ppos; int last_mark_lbn; /* storing log_blk_num of last mark is extends ADR spec */ int first_data_ppos; int eod_frame_ppos; int eod_frame_lfa; int write_type; /* used in write error recovery */ int read_error_frame; /* used in read error recovery */ unsigned long cmd_start_time; unsigned long max_cmd_time; #if DEBUG unsigned char write_pending; int nbr_finished; int nbr_waits; unsigned char last_cmnd[6]; unsigned char last_sense[16]; #endif struct gendisk *drive; } ; /* scsi tape command */ struct osst_request { unsigned char cmd[MAX_COMMAND_SIZE]; unsigned char sense[SCSI_SENSE_BUFFERSIZE]; int result; struct osst_tape *stp; struct completion *waiting; }; /* Values of write_type */ #define OS_WRITE_DATA 0 #define OS_WRITE_EOD 1 #define OS_WRITE_NEW_MARK 2 #define OS_WRITE_LAST_MARK 3 #define OS_WRITE_HEADER 4 #define OS_WRITE_FILLER 5 /* Additional rw state */ #define OS_WRITING_COMPLETE 3