Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / base / transport_class.c
blob4d08ee711e14bcc051fd042999f091c20ab70849
1 /*
2 * transport_class.c - implementation of generic transport classes
3 * using attribute_containers
5 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
7 * This file is licensed under GPLv2
9 * The basic idea here is to allow any "device controller" (which
10 * would most often be a Host Bus Adapter to use the services of one
11 * or more tranport classes for performing transport specific
12 * services. Transport specific services are things that the generic
13 * command layer doesn't want to know about (speed settings, line
14 * condidtioning, etc), but which the user might be interested in.
15 * Thus, the HBA's use the routines exported by the transport classes
16 * to perform these functions. The transport classes export certain
17 * values to the user via sysfs using attribute containers.
19 * Note: because not every HBA will care about every transport
20 * attribute, there's a many to one relationship that goes like this:
22 * transport class<-----attribute container<----class device
24 * Usually the attribute container is per-HBA, but the design doesn't
25 * mandate that. Although most of the services will be specific to
26 * the actual external storage connection used by the HBA, the generic
27 * transport class is framed entirely in terms of generic devices to
28 * allow it to be used by any physical HBA in the system.
30 #include <linux/attribute_container.h>
31 #include <linux/transport_class.h>
33 /**
34 * transport_class_register - register an initial transport class
36 * @tclass: a pointer to the transport class structure to be initialised
38 * The transport class contains an embedded class which is used to
39 * identify it. The caller should initialise this structure with
40 * zeros and then generic class must have been initialised with the
41 * actual transport class unique name. There's a macro
42 * DECLARE_TRANSPORT_CLASS() to do this (declared classes still must
43 * be registered).
45 * Returns 0 on success or error on failure.
47 int transport_class_register(struct transport_class *tclass)
49 return class_register(&tclass->class);
51 EXPORT_SYMBOL_GPL(transport_class_register);
53 /**
54 * transport_class_unregister - unregister a previously registered class
56 * @tclass: The transport class to unregister
58 * Must be called prior to deallocating the memory for the transport
59 * class.
61 void transport_class_unregister(struct transport_class *tclass)
63 class_unregister(&tclass->class);
65 EXPORT_SYMBOL_GPL(transport_class_unregister);
67 static int anon_transport_dummy_function(struct transport_container *tc,
68 struct device *dev,
69 struct class_device *cdev)
71 /* do nothing */
72 return 0;
75 /**
76 * anon_transport_class_register - register an anonymous class
78 * @atc: The anon transport class to register
80 * The anonymous transport class contains both a transport class and a
81 * container. The idea of an anonymous class is that it never
82 * actually has any device attributes associated with it (and thus
83 * saves on container storage). So it can only be used for triggering
84 * events. Use prezero and then use DECLARE_ANON_TRANSPORT_CLASS() to
85 * initialise the anon transport class storage.
87 int anon_transport_class_register(struct anon_transport_class *atc)
89 int error;
90 atc->container.class = &atc->tclass.class;
91 attribute_container_set_no_classdevs(&atc->container);
92 error = attribute_container_register(&atc->container);
93 if (error)
94 return error;
95 atc->tclass.setup = anon_transport_dummy_function;
96 atc->tclass.remove = anon_transport_dummy_function;
97 return 0;
99 EXPORT_SYMBOL_GPL(anon_transport_class_register);
102 * anon_transport_class_unregister - unregister an anon class
104 * @atc: Pointer to the anon transport class to unregister
106 * Must be called prior to deallocating the memory for the anon
107 * transport class.
109 void anon_transport_class_unregister(struct anon_transport_class *atc)
111 attribute_container_unregister(&atc->container);
113 EXPORT_SYMBOL_GPL(anon_transport_class_unregister);
115 static int transport_setup_classdev(struct attribute_container *cont,
116 struct device *dev,
117 struct class_device *classdev)
119 struct transport_class *tclass = class_to_transport_class(cont->class);
120 struct transport_container *tcont = attribute_container_to_transport_container(cont);
122 if (tclass->setup)
123 tclass->setup(tcont, dev, classdev);
125 return 0;
129 <<<<<<< HEAD:drivers/base/transport_class.c
130 * transport_setup_device - declare a new dev for transport class association
131 * but don't make it visible yet.
133 =======
134 * transport_setup_device - declare a new dev for transport class association but don't make it visible yet.
135 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/base/transport_class.c
136 * @dev: the generic device representing the entity being added
138 * Usually, dev represents some component in the HBA system (either
139 * the HBA itself or a device remote across the HBA bus). This
140 * routine is simply a trigger point to see if any set of transport
141 * classes wishes to associate with the added device. This allocates
142 * storage for the class device and initialises it, but does not yet
143 * add it to the system or add attributes to it (you do this with
144 * transport_add_device). If you have no need for a separate setup
145 * and add operations, use transport_register_device (see
146 * transport_class.h).
149 void transport_setup_device(struct device *dev)
151 attribute_container_add_device(dev, transport_setup_classdev);
153 EXPORT_SYMBOL_GPL(transport_setup_device);
155 static int transport_add_class_device(struct attribute_container *cont,
156 struct device *dev,
157 struct class_device *classdev)
159 int error = attribute_container_add_class_device(classdev);
160 struct transport_container *tcont =
161 attribute_container_to_transport_container(cont);
163 if (!error && tcont->statistics)
164 error = sysfs_create_group(&classdev->kobj, tcont->statistics);
166 return error;
171 * transport_add_device - declare a new dev for transport class association
173 * @dev: the generic device representing the entity being added
175 * Usually, dev represents some component in the HBA system (either
176 * the HBA itself or a device remote across the HBA bus). This
177 * routine is simply a trigger point used to add the device to the
178 * system and register attributes for it.
181 void transport_add_device(struct device *dev)
183 attribute_container_device_trigger(dev, transport_add_class_device);
185 EXPORT_SYMBOL_GPL(transport_add_device);
187 static int transport_configure(struct attribute_container *cont,
188 struct device *dev,
189 struct class_device *cdev)
191 struct transport_class *tclass = class_to_transport_class(cont->class);
192 struct transport_container *tcont = attribute_container_to_transport_container(cont);
194 if (tclass->configure)
195 tclass->configure(tcont, dev, cdev);
197 return 0;
201 * transport_configure_device - configure an already set up device
203 * @dev: generic device representing device to be configured
205 * The idea of configure is simply to provide a point within the setup
206 * process to allow the transport class to extract information from a
207 * device after it has been setup. This is used in SCSI because we
208 * have to have a setup device to begin using the HBA, but after we
209 * send the initial inquiry, we use configure to extract the device
210 * parameters. The device need not have been added to be configured.
212 void transport_configure_device(struct device *dev)
214 attribute_container_device_trigger(dev, transport_configure);
216 EXPORT_SYMBOL_GPL(transport_configure_device);
218 static int transport_remove_classdev(struct attribute_container *cont,
219 struct device *dev,
220 struct class_device *classdev)
222 struct transport_container *tcont =
223 attribute_container_to_transport_container(cont);
224 struct transport_class *tclass = class_to_transport_class(cont->class);
226 if (tclass->remove)
227 tclass->remove(tcont, dev, classdev);
229 if (tclass->remove != anon_transport_dummy_function) {
230 if (tcont->statistics)
231 sysfs_remove_group(&classdev->kobj, tcont->statistics);
232 attribute_container_class_device_del(classdev);
235 return 0;
240 * transport_remove_device - remove the visibility of a device
242 * @dev: generic device to remove
244 * This call removes the visibility of the device (to the user from
245 * sysfs), but does not destroy it. To eliminate a device entirely
246 * you must also call transport_destroy_device. If you don't need to
247 * do remove and destroy as separate operations, use
248 * transport_unregister_device() (see transport_class.h) which will
249 * perform both calls for you.
251 void transport_remove_device(struct device *dev)
253 attribute_container_device_trigger(dev, transport_remove_classdev);
255 EXPORT_SYMBOL_GPL(transport_remove_device);
257 static void transport_destroy_classdev(struct attribute_container *cont,
258 struct device *dev,
259 struct class_device *classdev)
261 struct transport_class *tclass = class_to_transport_class(cont->class);
263 if (tclass->remove != anon_transport_dummy_function)
264 class_device_put(classdev);
269 * transport_destroy_device - destroy a removed device
271 * @dev: device to eliminate from the transport class.
273 * This call triggers the elimination of storage associated with the
274 * transport classdev. Note: all it really does is relinquish a
275 * reference to the classdev. The memory will not be freed until the
276 * last reference goes to zero. Note also that the classdev retains a
277 * reference count on dev, so dev too will remain for as long as the
278 * transport class device remains around.
280 void transport_destroy_device(struct device *dev)
282 attribute_container_remove_device(dev, transport_destroy_classdev);
284 EXPORT_SYMBOL_GPL(transport_destroy_device);