card.c 9.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/*
 * card.c - contains functions for managing groups of PnP devices
 *
 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/slab.h>

#ifdef CONFIG_PNP_DEBUG
	#define DEBUG
#else
	#undef DEBUG
#endif

#include <linux/pnp.h>
#include "base.h"

LIST_HEAD(pnp_cards);
LIST_HEAD(pnp_card_drivers);


static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
{
	const struct pnp_card_device_id * drv_id = drv->id_table;
	while (*drv_id->id){
		if (compare_pnp_id(card->id,drv_id->id)) {
			int i = 0;
			for (;;) {
				int found;
				struct pnp_dev *dev;
				if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
					return drv_id;
				found = 0;
				card_for_each_dev(card, dev) {
					if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
						found = 1;
						break;
					}
				}
				if (! found)
					break;
				i++;
			}
		}
		drv_id++;
	}
	return NULL;
}

static void card_remove(struct pnp_dev * dev)
{
	dev->card_link = NULL;
}
 
static void card_remove_first(struct pnp_dev * dev)
{
	struct pnp_card_driver * drv = to_pnp_card_driver(dev->driver);
	if (!dev->card || !drv)
		return;
	if (drv->remove)
		drv->remove(dev->card_link);
	drv->link.remove = &card_remove;
	kfree(dev->card_link);
	card_remove(dev);
}

static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
{
	const struct pnp_card_device_id *id = match_card(drv,card);
	if (id) {
		struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
		if (!clink)
			return 0;
		clink->card = card;
		clink->driver = drv;
		if (drv->probe) {
			if (drv->probe(clink, id)>=0)
				return 1;
			else {
				struct pnp_dev * dev;
				card_for_each_dev(card, dev) {
					if (dev->card_link == clink)
						pnp_release_card_device(dev);
				}
				kfree(clink);
			}
		} else
			return 1;
	}
	return 0;
}

/**
 * pnp_add_card_id - adds an EISA id to the specified card
 * @id: pointer to a pnp_id structure
 * @card: pointer to the desired card
 *
 */

int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
{
	struct pnp_id * ptr;
	if (!id)
		return -EINVAL;
	if (!card)
		return -EINVAL;
	id->next = NULL;
	ptr = card->id;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = id;
	else
		card->id = id;
	return 0;
}

static void pnp_free_card_ids(struct pnp_card * card)
{
	struct pnp_id * id;
	struct pnp_id *next;
	if (!card)
		return;
	id = card->id;
	while (id) {
		next = id->next;
		kfree(id);
		id = next;
	}
}

static void pnp_release_card(struct device *dmdev)
{
	struct pnp_card * card = to_pnp_card(dmdev);
	pnp_free_card_ids(card);
	kfree(card);
}


143
static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152
{
	char *str = buf;
	struct pnp_card *card = to_pnp_card(dmdev);
	str += sprintf(str,"%s\n", card->name);
	return (str - buf);
}

static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);

153
static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
	char *str = buf;
	struct pnp_card *card = to_pnp_card(dmdev);
	struct pnp_id * pos = card->id;

	while (pos) {
		str += sprintf(str,"%s\n", pos->id);
		pos = pos->next;
	}
	return (str - buf);
}

static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);

static int pnp_interface_attach_card(struct pnp_card *card)
{
	device_create_file(&card->dev,&dev_attr_name);
	device_create_file(&card->dev,&dev_attr_card_id);
	return 0;
}

/**
 * pnp_add_card - adds a PnP card to the PnP Layer
 * @card: pointer to the card to add
 */

int pnp_add_card(struct pnp_card * card)
{
	int error;
	struct list_head * pos, * temp;
	if (!card || !card->protocol)
		return -EINVAL;

	sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
	card->dev.parent = &card->protocol->dev;
	card->dev.bus = NULL;
	card->dev.release = &pnp_release_card;
	error = device_register(&card->dev);

	if (error == 0) {
		pnp_interface_attach_card(card);
		spin_lock(&pnp_lock);
		list_add_tail(&card->global_list, &pnp_cards);
		list_add_tail(&card->protocol_list, &card->protocol->cards);
		spin_unlock(&pnp_lock);

		/* we wait until now to add devices in order to ensure the drivers
		 * will be able to use all of the related devices on the card
		 * without waiting any unresonable length of time */
		list_for_each(pos,&card->devices){
			struct pnp_dev *dev = card_to_pnp_dev(pos);
			__pnp_add_device(dev);
		}

		/* match with card drivers */
		list_for_each_safe(pos,temp,&pnp_card_drivers){
			struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
			card_probe(card,drv);
		}
	} else
		pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
	return error;
}

/**
 * pnp_remove_card - removes a PnP card from the PnP Layer
 * @card: pointer to the card to remove
 */

void pnp_remove_card(struct pnp_card * card)
{
	struct list_head *pos, *temp;
	if (!card)
		return;
	device_unregister(&card->dev);
	spin_lock(&pnp_lock);
	list_del(&card->global_list);
	list_del(&card->protocol_list);
	spin_unlock(&pnp_lock);
	list_for_each_safe(pos,temp,&card->devices){
		struct pnp_dev *dev = card_to_pnp_dev(pos);
		pnp_remove_card_device(dev);
	}
}

/**
 * pnp_add_card_device - adds a device to the specified card
 * @card: pointer to the card to add to
 * @dev: pointer to the device to add
 */

int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
{
	if (!card || !dev || !dev->protocol)
		return -EINVAL;
	dev->dev.parent = &card->dev;
	dev->card_link = NULL;
	snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
		 card->number,dev->number);
	spin_lock(&pnp_lock);
	dev->card = card;
	list_add_tail(&dev->card_list, &card->devices);
	spin_unlock(&pnp_lock);
	return 0;
}

/**
 * pnp_remove_card_device- removes a device from the specified card
 * @dev: pointer to the device to remove
 */

void pnp_remove_card_device(struct pnp_dev * dev)
{
	spin_lock(&pnp_lock);
	dev->card = NULL;
	list_del(&dev->card_list);
	spin_unlock(&pnp_lock);
	__pnp_remove_device(dev);
}

/**
 * pnp_request_card_device - Searches for a PnP device under the specified card
M
Martin Waitz 已提交
276
 * @clink: pointer to the card link, cannot be NULL
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
 * @id: pointer to a PnP ID structure that explains the rules for finding the device
 * @from: Starting place to search from. If NULL it will start from the begining.
 */

struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
{
	struct list_head * pos;
	struct pnp_dev * dev;
	struct pnp_card_driver * drv;
	struct pnp_card * card;
	if (!clink || !id)
		goto done;
	card = clink->card;
	drv = clink->driver;
	if (!from) {
		pos = card->devices.next;
	} else {
		if (from->card != card)
			goto done;
		pos = from->card_list.next;
	}
	while (pos != &card->devices) {
		dev = card_to_pnp_dev(pos);
		if ((!dev->card_link) && compare_pnp_id(dev->id,id))
			goto found;
		pos = pos->next;
	}

done:
	return NULL;

found:
	down_write(&dev->dev.bus->subsys.rwsem);
	dev->card_link = clink;
	dev->dev.driver = &drv->link.driver;
	if (drv->link.driver.probe) {
		if (drv->link.driver.probe(&dev->dev)) {
			dev->dev.driver = NULL;
315 316
			dev->card_link = NULL;
			up_write(&dev->dev.bus->subsys.rwsem);
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
			return NULL;
		}
	}
	device_bind_driver(&dev->dev);
	up_write(&dev->dev.bus->subsys.rwsem);

	return dev;
}

/**
 * pnp_release_card_device - call this when the driver no longer needs the device
 * @dev: pointer to the PnP device stucture
 */

void pnp_release_card_device(struct pnp_dev * dev)
{
	struct pnp_card_driver * drv = dev->card_link->driver;
	if (!drv)
		return;
	down_write(&dev->dev.bus->subsys.rwsem);
	drv->link.remove = &card_remove;
	device_release_driver(&dev->dev);
	drv->link.remove = &card_remove_first;
	up_write(&dev->dev.bus->subsys.rwsem);
}

/**
 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
 * @drv: pointer to the driver to register
 */

int pnp_register_card_driver(struct pnp_card_driver * drv)
{
	int count = 0;
	struct list_head *pos, *temp;

	drv->link.name = drv->name;
	drv->link.id_table = NULL;	/* this will disable auto matching */
	drv->link.flags = drv->flags;
	drv->link.probe = NULL;
	drv->link.remove = &card_remove_first;

	spin_lock(&pnp_lock);
	list_add_tail(&drv->global_list, &pnp_card_drivers);
	spin_unlock(&pnp_lock);
	pnp_register_driver(&drv->link);

	list_for_each_safe(pos,temp,&pnp_cards){
		struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
		count += card_probe(card,drv);
	}
	return count;
}

/**
 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
 * @drv: pointer to the driver to unregister
 */

void pnp_unregister_card_driver(struct pnp_card_driver * drv)
{
	spin_lock(&pnp_lock);
	list_del(&drv->global_list);
	spin_unlock(&pnp_lock);
	pnp_unregister_driver(&drv->link);
}

EXPORT_SYMBOL(pnp_add_card);
EXPORT_SYMBOL(pnp_remove_card);
EXPORT_SYMBOL(pnp_add_card_device);
EXPORT_SYMBOL(pnp_remove_card_device);
EXPORT_SYMBOL(pnp_add_card_id);
EXPORT_SYMBOL(pnp_request_card_device);
EXPORT_SYMBOL(pnp_release_card_device);
EXPORT_SYMBOL(pnp_register_card_driver);
EXPORT_SYMBOL(pnp_unregister_card_driver);