xref: /linux/drivers/pcmcia/ds.c (revision 273b281fa22c293963ee3e6eec418f5dda2dbc83)
1 /*
2  * ds.c -- 16-bit PCMCIA core support
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * The initial developer of the original code is David A. Hinds
9  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
10  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
11  *
12  * (C) 1999		David A. Hinds
13  * (C) 2003 - 2006	Dominik Brodowski
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/crc32.h>
24 #include <linux/firmware.h>
25 #include <linux/kref.h>
26 #include <linux/dma-mapping.h>
27 
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/ds.h>
32 #include <pcmcia/ss.h>
33 
34 #include "cs_internal.h"
35 
36 /*====================================================================*/
37 
38 /* Module parameters */
39 
40 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
41 MODULE_DESCRIPTION("PCMCIA Driver Services");
42 MODULE_LICENSE("GPL");
43 
44 
45 spinlock_t pcmcia_dev_list_lock;
46 
47 /*====================================================================*/
48 
49 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
50 {
51 	struct pcmcia_device_id *did = p_drv->id_table;
52 	unsigned int i;
53 	u32 hash;
54 
55 	if (!p_drv->probe || !p_drv->remove)
56 		printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
57 		       "function\n", p_drv->drv.name);
58 
59 	while (did && did->match_flags) {
60 		for (i=0; i<4; i++) {
61 			if (!did->prod_id[i])
62 				continue;
63 
64 			hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
65 			if (hash == did->prod_id_hash[i])
66 				continue;
67 
68 			printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
69 			       "product string \"%s\": is 0x%x, should "
70 			       "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
71 			       did->prod_id_hash[i], hash);
72 			printk(KERN_DEBUG "pcmcia: see "
73 				"Documentation/pcmcia/devicetable.txt for "
74 				"details\n");
75 		}
76 		did++;
77 	}
78 
79 	return;
80 }
81 
82 
83 /*======================================================================*/
84 
85 
86 struct pcmcia_dynid {
87 	struct list_head 		node;
88 	struct pcmcia_device_id 	id;
89 };
90 
91 /**
92  * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
93  * @driver: target device driver
94  * @buf: buffer for scanning device ID data
95  * @count: input size
96  *
97  * Adds a new dynamic PCMCIA device ID to this driver,
98  * and causes the driver to probe for all devices again.
99  */
100 static ssize_t
101 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
102 {
103 	struct pcmcia_dynid *dynid;
104 	struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
105 	__u16 match_flags, manf_id, card_id;
106 	__u8 func_id, function, device_no;
107 	__u32 prod_id_hash[4] = {0, 0, 0, 0};
108 	int fields=0;
109 	int retval = 0;
110 
111 	fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
112 			&match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
113 			&prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
114 	if (fields < 6)
115 		return -EINVAL;
116 
117 	dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
118 	if (!dynid)
119 		return -ENOMEM;
120 
121 	dynid->id.match_flags = match_flags;
122 	dynid->id.manf_id = manf_id;
123 	dynid->id.card_id = card_id;
124 	dynid->id.func_id = func_id;
125 	dynid->id.function = function;
126 	dynid->id.device_no = device_no;
127 	memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
128 
129 	spin_lock(&pdrv->dynids.lock);
130 	list_add_tail(&dynid->node, &pdrv->dynids.list);
131 	spin_unlock(&pdrv->dynids.lock);
132 
133 	if (get_driver(&pdrv->drv)) {
134 		retval = driver_attach(&pdrv->drv);
135 		put_driver(&pdrv->drv);
136 	}
137 
138 	if (retval)
139 		return retval;
140 	return count;
141 }
142 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
143 
144 static void
145 pcmcia_free_dynids(struct pcmcia_driver *drv)
146 {
147 	struct pcmcia_dynid *dynid, *n;
148 
149 	spin_lock(&drv->dynids.lock);
150 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
151 		list_del(&dynid->node);
152 		kfree(dynid);
153 	}
154 	spin_unlock(&drv->dynids.lock);
155 }
156 
157 static int
158 pcmcia_create_newid_file(struct pcmcia_driver *drv)
159 {
160 	int error = 0;
161 	if (drv->probe != NULL)
162 		error = driver_create_file(&drv->drv, &driver_attr_new_id);
163 	return error;
164 }
165 
166 
167 /**
168  * pcmcia_register_driver - register a PCMCIA driver with the bus core
169  * @driver: the &driver being registered
170  *
171  * Registers a PCMCIA driver with the PCMCIA bus core.
172  */
173 int pcmcia_register_driver(struct pcmcia_driver *driver)
174 {
175 	int error;
176 
177 	if (!driver)
178 		return -EINVAL;
179 
180 	pcmcia_check_driver(driver);
181 
182 	/* initialize common fields */
183 	driver->drv.bus = &pcmcia_bus_type;
184 	driver->drv.owner = driver->owner;
185 	spin_lock_init(&driver->dynids.lock);
186 	INIT_LIST_HEAD(&driver->dynids.list);
187 
188 	pr_debug("registering driver %s\n", driver->drv.name);
189 
190 	error = driver_register(&driver->drv);
191 	if (error < 0)
192 		return error;
193 
194 	error = pcmcia_create_newid_file(driver);
195 	if (error)
196 		driver_unregister(&driver->drv);
197 
198 	return error;
199 }
200 EXPORT_SYMBOL(pcmcia_register_driver);
201 
202 /**
203  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
204  * @driver: the &driver being unregistered
205  */
206 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
207 {
208 	pr_debug("unregistering driver %s\n", driver->drv.name);
209 	driver_unregister(&driver->drv);
210 	pcmcia_free_dynids(driver);
211 }
212 EXPORT_SYMBOL(pcmcia_unregister_driver);
213 
214 
215 /* pcmcia_device handling */
216 
217 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
218 {
219 	struct device *tmp_dev;
220 	tmp_dev = get_device(&p_dev->dev);
221 	if (!tmp_dev)
222 		return NULL;
223 	return to_pcmcia_dev(tmp_dev);
224 }
225 
226 void pcmcia_put_dev(struct pcmcia_device *p_dev)
227 {
228 	if (p_dev)
229 		put_device(&p_dev->dev);
230 }
231 
232 static void pcmcia_release_function(struct kref *ref)
233 {
234 	struct config_t *c = container_of(ref, struct config_t, ref);
235 	pr_debug("releasing config_t\n");
236 	kfree(c);
237 }
238 
239 static void pcmcia_release_dev(struct device *dev)
240 {
241 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
242 	dev_dbg(dev, "releasing device\n");
243 	pcmcia_put_socket(p_dev->socket);
244 	kfree(p_dev->devname);
245 	kref_put(&p_dev->function_config->ref, pcmcia_release_function);
246 	kfree(p_dev);
247 }
248 
249 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
250 {
251 	if (!s->pcmcia_state.device_add_pending) {
252 		dev_dbg(&s->dev, "scheduling to add %s secondary"
253 		       " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
254 		s->pcmcia_state.device_add_pending = 1;
255 		s->pcmcia_state.mfc_pfc = mfc;
256 		schedule_work(&s->device_add);
257 	}
258 	return;
259 }
260 
261 static int pcmcia_device_probe(struct device * dev)
262 {
263 	struct pcmcia_device *p_dev;
264 	struct pcmcia_driver *p_drv;
265 	struct pcmcia_device_id *did;
266 	struct pcmcia_socket *s;
267 	cistpl_config_t cis_config;
268 	int ret = 0;
269 
270 	dev = get_device(dev);
271 	if (!dev)
272 		return -ENODEV;
273 
274 	p_dev = to_pcmcia_dev(dev);
275 	p_drv = to_pcmcia_drv(dev->driver);
276 	s = p_dev->socket;
277 
278 	/* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
279 	 * which is an ugly hack. Once the driver probe is called it may
280 	 * and often will overwrite the match data so we must save it first
281 	 *
282 	 * handle pseudo multifunction devices:
283 	 * there are at most two pseudo multifunction devices.
284 	 * if we're matching against the first, schedule a
285 	 * call which will then check whether there are two
286 	 * pseudo devices, and if not, add the second one.
287 	 */
288 	did = dev_get_drvdata(&p_dev->dev);
289 
290 	dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
291 
292 	if ((!p_drv->probe) || (!p_dev->function_config) ||
293 	    (!try_module_get(p_drv->owner))) {
294 		ret = -EINVAL;
295 		goto put_dev;
296 	}
297 
298 	/* set up some more device information */
299 	ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
300 				&cis_config);
301 	if (!ret) {
302 		p_dev->conf.ConfigBase = cis_config.base;
303 		p_dev->conf.Present = cis_config.rmask[0];
304 	} else {
305 		dev_printk(KERN_INFO, dev,
306 			   "pcmcia: could not parse base and rmask0 of CIS\n");
307 		p_dev->conf.ConfigBase = 0;
308 		p_dev->conf.Present = 0;
309 	}
310 
311 	ret = p_drv->probe(p_dev);
312 	if (ret) {
313 		dev_dbg(dev, "binding to %s failed with %d\n",
314 			   p_drv->drv.name, ret);
315 		goto put_module;
316 	}
317 
318 	if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
319 	    (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
320 		pcmcia_add_device_later(p_dev->socket, 0);
321 
322 put_module:
323 	if (ret)
324 		module_put(p_drv->owner);
325 put_dev:
326 	if (ret)
327 		put_device(dev);
328 	return (ret);
329 }
330 
331 
332 /*
333  * Removes a PCMCIA card from the device tree and socket list.
334  */
335 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
336 {
337 	struct pcmcia_device	*p_dev;
338 	struct pcmcia_device	*tmp;
339 	unsigned long		flags;
340 
341 	dev_dbg(leftover ? &leftover->dev : &s->dev,
342 		   "pcmcia_card_remove(%d) %s\n", s->sock,
343 		   leftover ? leftover->devname : "");
344 
345 	if (!leftover)
346 		s->device_count = 0;
347 	else
348 		s->device_count = 1;
349 
350 	/* unregister all pcmcia_devices registered with this socket, except leftover */
351 	list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
352 		if (p_dev == leftover)
353 			continue;
354 
355 		spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
356 		list_del(&p_dev->socket_device_list);
357 		p_dev->_removed=1;
358 		spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
359 
360 		dev_dbg(&p_dev->dev, "unregistering device\n");
361 		device_unregister(&p_dev->dev);
362 	}
363 
364 	return;
365 }
366 
367 static int pcmcia_device_remove(struct device * dev)
368 {
369 	struct pcmcia_device *p_dev;
370 	struct pcmcia_driver *p_drv;
371 	struct pcmcia_device_id *did;
372 	int i;
373 
374 	p_dev = to_pcmcia_dev(dev);
375 	p_drv = to_pcmcia_drv(dev->driver);
376 
377 	dev_dbg(dev, "removing device\n");
378 
379 	/* If we're removing the primary module driving a
380 	 * pseudo multi-function card, we need to unbind
381 	 * all devices
382 	 */
383 	did = dev_get_drvdata(&p_dev->dev);
384 	if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
385 	    (p_dev->socket->device_count != 0) &&
386 	    (p_dev->device_no == 0))
387 		pcmcia_card_remove(p_dev->socket, p_dev);
388 
389 	/* detach the "instance" */
390 	if (!p_drv)
391 		return 0;
392 
393 	if (p_drv->remove)
394 	       	p_drv->remove(p_dev);
395 
396 	p_dev->dev_node = NULL;
397 
398 	/* check for proper unloading */
399 	if (p_dev->_irq || p_dev->_io || p_dev->_locked)
400 		dev_printk(KERN_INFO, dev,
401 			"pcmcia: driver %s did not release config properly\n",
402 			p_drv->drv.name);
403 
404 	for (i = 0; i < MAX_WIN; i++)
405 		if (p_dev->_win & CLIENT_WIN_REQ(i))
406 			dev_printk(KERN_INFO, dev,
407 			  "pcmcia: driver %s did not release window properly\n",
408 			   p_drv->drv.name);
409 
410 	/* references from pcmcia_probe_device */
411 	pcmcia_put_dev(p_dev);
412 	module_put(p_drv->owner);
413 
414 	return 0;
415 }
416 
417 
418 /*
419  * pcmcia_device_query -- determine information about a pcmcia device
420  */
421 static int pcmcia_device_query(struct pcmcia_device *p_dev)
422 {
423 	cistpl_manfid_t manf_id;
424 	cistpl_funcid_t func_id;
425 	cistpl_vers_1_t	*vers1;
426 	unsigned int i;
427 
428 	vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
429 	if (!vers1)
430 		return -ENOMEM;
431 
432 	if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
433 			       CISTPL_MANFID, &manf_id)) {
434 		p_dev->manf_id = manf_id.manf;
435 		p_dev->card_id = manf_id.card;
436 		p_dev->has_manf_id = 1;
437 		p_dev->has_card_id = 1;
438 	}
439 
440 	if (!pccard_read_tuple(p_dev->socket, p_dev->func,
441 			       CISTPL_FUNCID, &func_id)) {
442 		p_dev->func_id = func_id.func;
443 		p_dev->has_func_id = 1;
444 	} else {
445 		/* rule of thumb: cards with no FUNCID, but with
446 		 * common memory device geometry information, are
447 		 * probably memory cards (from pcmcia-cs) */
448 		cistpl_device_geo_t *devgeo;
449 
450 		devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
451 		if (!devgeo) {
452 			kfree(vers1);
453 			return -ENOMEM;
454 		}
455 		if (!pccard_read_tuple(p_dev->socket, p_dev->func,
456 				      CISTPL_DEVICE_GEO, devgeo)) {
457 			dev_dbg(&p_dev->dev,
458 				   "mem device geometry probably means "
459 				   "FUNCID_MEMORY\n");
460 			p_dev->func_id = CISTPL_FUNCID_MEMORY;
461 			p_dev->has_func_id = 1;
462 		}
463 		kfree(devgeo);
464 	}
465 
466 	if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
467 			       vers1)) {
468 		for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
469 			char *tmp;
470 			unsigned int length;
471 
472 			tmp = vers1->str + vers1->ofs[i];
473 
474 			length = strlen(tmp) + 1;
475 			if ((length < 2) || (length > 255))
476 				continue;
477 
478 			p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
479 						    GFP_KERNEL);
480 			if (!p_dev->prod_id[i])
481 				continue;
482 
483 			p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
484 						    tmp, length);
485 		}
486 	}
487 
488 	kfree(vers1);
489 	return 0;
490 }
491 
492 
493 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
494  * Serializes pcmcia_device_add; will most likely be removed in future.
495  *
496  * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
497  * won't work, this doesn't matter much at the moment: the driver core doesn't
498  * support it either.
499  */
500 static DEFINE_MUTEX(device_add_lock);
501 
502 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
503 {
504 	struct pcmcia_device *p_dev, *tmp_dev;
505 	unsigned long flags;
506 
507 	s = pcmcia_get_socket(s);
508 	if (!s)
509 		return NULL;
510 
511 	mutex_lock(&device_add_lock);
512 
513 	pr_debug("adding device to %d, function %d\n", s->sock, function);
514 
515 	/* max of 4 devices per card */
516 	if (s->device_count == 4)
517 		goto err_put;
518 
519 	p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
520 	if (!p_dev)
521 		goto err_put;
522 
523 	p_dev->socket = s;
524 	p_dev->device_no = (s->device_count++);
525 	p_dev->func   = function;
526 
527 	p_dev->dev.bus = &pcmcia_bus_type;
528 	p_dev->dev.parent = s->dev.parent;
529 	p_dev->dev.release = pcmcia_release_dev;
530 	/* by default don't allow DMA */
531 	p_dev->dma_mask = DMA_MASK_NONE;
532 	p_dev->dev.dma_mask = &p_dev->dma_mask;
533 	dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
534 	if (!dev_name(&p_dev->dev))
535 		goto err_free;
536 	p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
537 	if (!p_dev->devname)
538 		goto err_free;
539 	dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
540 
541 	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
542 
543 	/*
544 	 * p_dev->function_config must be the same for all card functions.
545 	 * Note that this is serialized by the device_add_lock, so that
546 	 * only one such struct will be created.
547 	 */
548         list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
549                 if (p_dev->func == tmp_dev->func) {
550 			p_dev->function_config = tmp_dev->function_config;
551 			p_dev->io = tmp_dev->io;
552 			p_dev->irq = tmp_dev->irq;
553 			kref_get(&p_dev->function_config->ref);
554 		}
555 
556 	/* Add to the list in pcmcia_bus_socket */
557 	list_add(&p_dev->socket_device_list, &s->devices_list);
558 
559 	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
560 
561 	if (!p_dev->function_config) {
562 		dev_dbg(&p_dev->dev, "creating config_t\n");
563 		p_dev->function_config = kzalloc(sizeof(struct config_t),
564 						 GFP_KERNEL);
565 		if (!p_dev->function_config)
566 			goto err_unreg;
567 		kref_init(&p_dev->function_config->ref);
568 	}
569 
570 	dev_printk(KERN_NOTICE, &p_dev->dev,
571 		   "pcmcia: registering new device %s\n",
572 		   p_dev->devname);
573 
574 	pcmcia_device_query(p_dev);
575 
576 	if (device_register(&p_dev->dev))
577 		goto err_unreg;
578 
579 	mutex_unlock(&device_add_lock);
580 
581 	return p_dev;
582 
583  err_unreg:
584 	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
585 	list_del(&p_dev->socket_device_list);
586 	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
587 
588  err_free:
589 	kfree(p_dev->devname);
590 	kfree(p_dev);
591 	s->device_count--;
592  err_put:
593 	mutex_unlock(&device_add_lock);
594 	pcmcia_put_socket(s);
595 
596 	return NULL;
597 }
598 
599 
600 static int pcmcia_card_add(struct pcmcia_socket *s)
601 {
602 	cistpl_longlink_mfc_t mfc;
603 	unsigned int no_funcs, i, no_chains;
604 	int ret = 0;
605 
606 	if (!(s->resource_setup_done)) {
607 		dev_dbg(&s->dev,
608 			   "no resources available, delaying card_add\n");
609 		return -EAGAIN; /* try again, but later... */
610 	}
611 
612 	if (pcmcia_validate_mem(s)) {
613 		dev_dbg(&s->dev, "validating mem resources failed, "
614 		       "delaying card_add\n");
615 		return -EAGAIN; /* try again, but later... */
616 	}
617 
618 	ret = pccard_validate_cis(s, &no_chains);
619 	if (ret || !no_chains) {
620 		dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
621 		return -ENODEV;
622 	}
623 
624 	if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
625 		no_funcs = mfc.nfn;
626 	else
627 		no_funcs = 1;
628 	s->functions = no_funcs;
629 
630 	for (i=0; i < no_funcs; i++)
631 		pcmcia_device_add(s, i);
632 
633 	return (ret);
634 }
635 
636 
637 static void pcmcia_delayed_add_device(struct work_struct *work)
638 {
639 	struct pcmcia_socket *s =
640 		container_of(work, struct pcmcia_socket, device_add);
641 	dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
642 	pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
643 	s->pcmcia_state.device_add_pending = 0;
644 	s->pcmcia_state.mfc_pfc = 0;
645 }
646 
647 static int pcmcia_requery(struct device *dev, void * _data)
648 {
649 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
650 	if (!p_dev->dev.driver) {
651 		dev_dbg(dev, "update device information\n");
652 		pcmcia_device_query(p_dev);
653 	}
654 
655 	return 0;
656 }
657 
658 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
659 {
660 	int no_devices = 0;
661 	int ret = 0;
662 	unsigned long flags;
663 
664 	/* must be called with skt_mutex held */
665 	dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
666 
667 	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
668 	if (list_empty(&skt->devices_list))
669 		no_devices = 1;
670 	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
671 
672 	/* If this is because of a CIS override, start over */
673 	if (new_cis && !no_devices)
674 		pcmcia_card_remove(skt, NULL);
675 
676 	/* if no devices were added for this socket yet because of
677 	 * missing resource information or other trouble, we need to
678 	 * do this now. */
679 	if (no_devices || new_cis) {
680 		ret = pcmcia_card_add(skt);
681 		if (ret)
682 			return;
683 	}
684 
685 	/* some device information might have changed because of a CIS
686 	 * update or because we can finally read it correctly... so
687 	 * determine it again, overwriting old values if necessary. */
688 	bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
689 
690 	/* we re-scan all devices, not just the ones connected to this
691 	 * socket. This does not matter, though. */
692 	ret = bus_rescan_devices(&pcmcia_bus_type);
693 	if (ret)
694 		printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
695 }
696 
697 #ifdef CONFIG_PCMCIA_LOAD_CIS
698 
699 /**
700  * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
701  * @dev: the pcmcia device which needs a CIS override
702  * @filename: requested filename in /lib/firmware/
703  *
704  * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
705  * the one provided by the card is broken. The firmware files reside in
706  * /lib/firmware/ in userspace.
707  */
708 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
709 {
710 	struct pcmcia_socket *s = dev->socket;
711 	const struct firmware *fw;
712 	int ret = -ENOMEM;
713 	int no_funcs;
714 	int old_funcs;
715 	cistpl_longlink_mfc_t mfc;
716 
717 	if (!filename)
718 		return -EINVAL;
719 
720 	dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
721 
722 	if (request_firmware(&fw, filename, &dev->dev) == 0) {
723 		if (fw->size >= CISTPL_MAX_CIS_SIZE) {
724 			ret = -EINVAL;
725 			dev_printk(KERN_ERR, &dev->dev,
726 				   "pcmcia: CIS override is too big\n");
727 			goto release;
728 		}
729 
730 		if (!pcmcia_replace_cis(s, fw->data, fw->size))
731 			ret = 0;
732 		else {
733 			dev_printk(KERN_ERR, &dev->dev,
734 				   "pcmcia: CIS override failed\n");
735 			goto release;
736 		}
737 
738 
739 		/* update information */
740 		pcmcia_device_query(dev);
741 
742 		/* does this cis override add or remove functions? */
743 		old_funcs = s->functions;
744 
745 		if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
746 			no_funcs = mfc.nfn;
747 		else
748 			no_funcs = 1;
749 		s->functions = no_funcs;
750 
751 		if (old_funcs > no_funcs)
752 			pcmcia_card_remove(s, dev);
753 		else if (no_funcs > old_funcs)
754 			pcmcia_add_device_later(s, 1);
755 	}
756  release:
757 	release_firmware(fw);
758 
759 	return (ret);
760 }
761 
762 #else /* !CONFIG_PCMCIA_LOAD_CIS */
763 
764 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
765 {
766 	return -ENODEV;
767 }
768 
769 #endif
770 
771 
772 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
773 				  struct pcmcia_device_id *did)
774 {
775 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
776 		if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
777 			return 0;
778 	}
779 
780 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
781 		if ((!dev->has_card_id) || (dev->card_id != did->card_id))
782 			return 0;
783 	}
784 
785 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
786 		if (dev->func != did->function)
787 			return 0;
788 	}
789 
790 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
791 		if (!dev->prod_id[0])
792 			return 0;
793 		if (strcmp(did->prod_id[0], dev->prod_id[0]))
794 			return 0;
795 	}
796 
797 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
798 		if (!dev->prod_id[1])
799 			return 0;
800 		if (strcmp(did->prod_id[1], dev->prod_id[1]))
801 			return 0;
802 	}
803 
804 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
805 		if (!dev->prod_id[2])
806 			return 0;
807 		if (strcmp(did->prod_id[2], dev->prod_id[2]))
808 			return 0;
809 	}
810 
811 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
812 		if (!dev->prod_id[3])
813 			return 0;
814 		if (strcmp(did->prod_id[3], dev->prod_id[3]))
815 			return 0;
816 	}
817 
818 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
819 		if (dev->device_no != did->device_no)
820 			return 0;
821 	}
822 
823 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
824 		if ((!dev->has_func_id) || (dev->func_id != did->func_id))
825 			return 0;
826 
827 		/* if this is a pseudo-multi-function device,
828 		 * we need explicit matches */
829 		if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
830 			return 0;
831 		if (dev->device_no)
832 			return 0;
833 
834 		/* also, FUNC_ID matching needs to be activated by userspace
835 		 * after it has re-checked that there is no possible module
836 		 * with a prod_id/manf_id/card_id match.
837 		 */
838 		dev_dbg(&dev->dev,
839 			"skipping FUNC_ID match until userspace interaction\n");
840 		if (!dev->allow_func_id_match)
841 			return 0;
842 	}
843 
844 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
845 		dev_dbg(&dev->dev, "device needs a fake CIS\n");
846 		if (!dev->socket->fake_cis)
847 			pcmcia_load_firmware(dev, did->cisfile);
848 
849 		if (!dev->socket->fake_cis)
850 			return 0;
851 	}
852 
853 	if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
854 		int i;
855 		for (i=0; i<4; i++)
856 			if (dev->prod_id[i])
857 				return 0;
858 		if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
859 			return 0;
860 	}
861 
862 	dev_set_drvdata(&dev->dev, did);
863 
864 	return 1;
865 }
866 
867 
868 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
869 	struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
870 	struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
871 	struct pcmcia_device_id *did = p_drv->id_table;
872 	struct pcmcia_dynid *dynid;
873 
874 	/* match dynamic devices first */
875 	spin_lock(&p_drv->dynids.lock);
876 	list_for_each_entry(dynid, &p_drv->dynids.list, node) {
877 		dev_dbg(dev, "trying to match to %s\n", drv->name);
878 		if (pcmcia_devmatch(p_dev, &dynid->id)) {
879 			dev_dbg(dev, "matched to %s\n", drv->name);
880 			spin_unlock(&p_drv->dynids.lock);
881 			return 1;
882 		}
883 	}
884 	spin_unlock(&p_drv->dynids.lock);
885 
886 #ifdef CONFIG_PCMCIA_IOCTL
887 	/* matching by cardmgr */
888 	if (p_dev->cardmgr == p_drv) {
889 		dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
890 		return 1;
891 	}
892 #endif
893 
894 	while (did && did->match_flags) {
895 		dev_dbg(dev, "trying to match to %s\n", drv->name);
896 		if (pcmcia_devmatch(p_dev, did)) {
897 			dev_dbg(dev, "matched to %s\n", drv->name);
898 			return 1;
899 		}
900 		did++;
901 	}
902 
903 	return 0;
904 }
905 
906 #ifdef CONFIG_HOTPLUG
907 
908 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
909 {
910 	struct pcmcia_device *p_dev;
911 	int i;
912 	u32 hash[4] = { 0, 0, 0, 0};
913 
914 	if (!dev)
915 		return -ENODEV;
916 
917 	p_dev = to_pcmcia_dev(dev);
918 
919 	/* calculate hashes */
920 	for (i=0; i<4; i++) {
921 		if (!p_dev->prod_id[i])
922 			continue;
923 		hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
924 	}
925 
926 	if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
927 		return -ENOMEM;
928 
929 	if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
930 		return -ENOMEM;
931 
932 	if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
933 			   "pa%08Xpb%08Xpc%08Xpd%08X",
934 			   p_dev->has_manf_id ? p_dev->manf_id : 0,
935 			   p_dev->has_card_id ? p_dev->card_id : 0,
936 			   p_dev->has_func_id ? p_dev->func_id : 0,
937 			   p_dev->func,
938 			   p_dev->device_no,
939 			   hash[0],
940 			   hash[1],
941 			   hash[2],
942 			   hash[3]))
943 		return -ENOMEM;
944 
945 	return 0;
946 }
947 
948 #else
949 
950 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
951 {
952 	return -ENODEV;
953 }
954 
955 #endif
956 
957 /************************ runtime PM support ***************************/
958 
959 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
960 static int pcmcia_dev_resume(struct device *dev);
961 
962 static int runtime_suspend(struct device *dev)
963 {
964 	int rc;
965 
966 	down(&dev->sem);
967 	rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
968 	up(&dev->sem);
969 	return rc;
970 }
971 
972 static void runtime_resume(struct device *dev)
973 {
974 	int rc;
975 
976 	down(&dev->sem);
977 	rc = pcmcia_dev_resume(dev);
978 	up(&dev->sem);
979 }
980 
981 /************************ per-device sysfs output ***************************/
982 
983 #define pcmcia_device_attr(field, test, format)				\
984 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf)		\
985 {									\
986 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);		\
987 	return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
988 }
989 
990 #define pcmcia_device_stringattr(name, field)					\
991 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf)		\
992 {									\
993 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);		\
994 	return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
995 }
996 
997 pcmcia_device_attr(func, socket, "0x%02x\n");
998 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
999 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1000 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1001 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1002 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1003 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1004 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1005 
1006 
1007 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1008 {
1009 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1010 
1011 	if (p_dev->suspended)
1012 		return sprintf(buf, "off\n");
1013 	else
1014 		return sprintf(buf, "on\n");
1015 }
1016 
1017 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1018 				     const char *buf, size_t count)
1019 {
1020 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1021 	int ret = 0;
1022 
1023         if (!count)
1024                 return -EINVAL;
1025 
1026 	if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1027 		ret = runtime_suspend(dev);
1028 	else if (p_dev->suspended && !strncmp(buf, "on", 2))
1029 		runtime_resume(dev);
1030 
1031 	return ret ? ret : count;
1032 }
1033 
1034 
1035 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1036 {
1037 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1038 	int i;
1039 	u32 hash[4] = { 0, 0, 0, 0};
1040 
1041 	/* calculate hashes */
1042 	for (i=0; i<4; i++) {
1043 		if (!p_dev->prod_id[i])
1044 			continue;
1045 		hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1046 	}
1047 	return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1048 				"pa%08Xpb%08Xpc%08Xpd%08X\n",
1049 				p_dev->has_manf_id ? p_dev->manf_id : 0,
1050 				p_dev->has_card_id ? p_dev->card_id : 0,
1051 				p_dev->has_func_id ? p_dev->func_id : 0,
1052 				p_dev->func, p_dev->device_no,
1053 				hash[0], hash[1], hash[2], hash[3]);
1054 }
1055 
1056 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1057 		struct device_attribute *attr, const char *buf, size_t count)
1058 {
1059 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1060 	int ret;
1061 
1062 	if (!count)
1063 		return -EINVAL;
1064 
1065 	mutex_lock(&p_dev->socket->skt_mutex);
1066 	p_dev->allow_func_id_match = 1;
1067 	mutex_unlock(&p_dev->socket->skt_mutex);
1068 
1069 	ret = bus_rescan_devices(&pcmcia_bus_type);
1070 	if (ret)
1071 		printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1072 		       "allowing func_id matches\n");
1073 
1074 	return count;
1075 }
1076 
1077 static struct device_attribute pcmcia_dev_attrs[] = {
1078 	__ATTR(function, 0444, func_show, NULL),
1079 	__ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1080 	__ATTR_RO(func_id),
1081 	__ATTR_RO(manf_id),
1082 	__ATTR_RO(card_id),
1083 	__ATTR_RO(prod_id1),
1084 	__ATTR_RO(prod_id2),
1085 	__ATTR_RO(prod_id3),
1086 	__ATTR_RO(prod_id4),
1087 	__ATTR_RO(modalias),
1088 	__ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1089 	__ATTR_NULL,
1090 };
1091 
1092 /* PM support, also needed for reset */
1093 
1094 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1095 {
1096 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1097 	struct pcmcia_driver *p_drv = NULL;
1098 	int ret = 0;
1099 
1100 	if (p_dev->suspended)
1101 		return 0;
1102 
1103 	dev_dbg(dev, "suspending\n");
1104 
1105 	if (dev->driver)
1106 		p_drv = to_pcmcia_drv(dev->driver);
1107 
1108 	if (!p_drv)
1109 		goto out;
1110 
1111 	if (p_drv->suspend) {
1112 		ret = p_drv->suspend(p_dev);
1113 		if (ret) {
1114 			dev_printk(KERN_ERR, dev,
1115 				   "pcmcia: device %s (driver %s) did "
1116 				   "not want to go to sleep (%d)\n",
1117 				   p_dev->devname, p_drv->drv.name, ret);
1118 			goto out;
1119 		}
1120 	}
1121 
1122 	if (p_dev->device_no == p_dev->func) {
1123 		dev_dbg(dev, "releasing configuration\n");
1124 		pcmcia_release_configuration(p_dev);
1125 	}
1126 
1127  out:
1128 	if (!ret)
1129 		p_dev->suspended = 1;
1130 	return ret;
1131 }
1132 
1133 
1134 static int pcmcia_dev_resume(struct device * dev)
1135 {
1136 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1137         struct pcmcia_driver *p_drv = NULL;
1138 	int ret = 0;
1139 
1140 	if (!p_dev->suspended)
1141 		return 0;
1142 
1143 	dev_dbg(dev, "resuming\n");
1144 
1145 	if (dev->driver)
1146 		p_drv = to_pcmcia_drv(dev->driver);
1147 
1148 	if (!p_drv)
1149 		goto out;
1150 
1151 	if (p_dev->device_no == p_dev->func) {
1152 		dev_dbg(dev, "requesting configuration\n");
1153 		ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1154 		if (ret)
1155 			goto out;
1156 	}
1157 
1158 	if (p_drv->resume)
1159 		ret = p_drv->resume(p_dev);
1160 
1161  out:
1162 	if (!ret)
1163 		p_dev->suspended = 0;
1164 	return ret;
1165 }
1166 
1167 
1168 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1169 {
1170 	struct pcmcia_socket *skt = _data;
1171 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1172 
1173 	if (p_dev->socket != skt || p_dev->suspended)
1174 		return 0;
1175 
1176 	return runtime_suspend(dev);
1177 }
1178 
1179 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1180 {
1181 	struct pcmcia_socket *skt = _data;
1182 	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1183 
1184 	if (p_dev->socket != skt || !p_dev->suspended)
1185 		return 0;
1186 
1187 	runtime_resume(dev);
1188 
1189 	return 0;
1190 }
1191 
1192 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1193 {
1194 	dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
1195 	bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1196 	return 0;
1197 }
1198 
1199 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1200 {
1201 	dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
1202 	if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1203 			     pcmcia_bus_suspend_callback)) {
1204 		pcmcia_bus_resume(skt);
1205 		return -EIO;
1206 	}
1207 	return 0;
1208 }
1209 
1210 
1211 /*======================================================================
1212 
1213     The card status event handler.
1214 
1215 ======================================================================*/
1216 
1217 /* Normally, the event is passed to individual drivers after
1218  * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1219  * is inversed to maintain historic compatibility.
1220  */
1221 
1222 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1223 {
1224 	struct pcmcia_socket *s = pcmcia_get_socket(skt);
1225 
1226 	if (!s) {
1227 		dev_printk(KERN_ERR, &skt->dev,
1228 			   "PCMCIA obtaining reference to socket "	\
1229 			   "failed, event 0x%x lost!\n", event);
1230 		return -ENODEV;
1231 	}
1232 
1233 	dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1234 		   event, priority, skt);
1235 
1236 	switch (event) {
1237 	case CS_EVENT_CARD_REMOVAL:
1238 		s->pcmcia_state.present = 0;
1239 		pcmcia_card_remove(skt, NULL);
1240 		handle_event(skt, event);
1241 		break;
1242 
1243 	case CS_EVENT_CARD_INSERTION:
1244 		s->pcmcia_state.present = 1;
1245 		pcmcia_card_add(skt);
1246 		handle_event(skt, event);
1247 		break;
1248 
1249 	case CS_EVENT_EJECTION_REQUEST:
1250 		break;
1251 
1252 	case CS_EVENT_PM_SUSPEND:
1253 	case CS_EVENT_PM_RESUME:
1254 	case CS_EVENT_RESET_PHYSICAL:
1255 	case CS_EVENT_CARD_RESET:
1256 	default:
1257 		handle_event(skt, event);
1258 		break;
1259     }
1260 
1261     pcmcia_put_socket(s);
1262 
1263     return 0;
1264 } /* ds_event */
1265 
1266 
1267 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1268 {
1269 	struct pcmcia_device *p_dev;
1270 	struct pcmcia_device *ret = NULL;
1271 
1272 	p_dev = pcmcia_get_dev(_p_dev);
1273 	if (!p_dev)
1274 		return NULL;
1275 
1276 	if (!p_dev->socket->pcmcia_state.present)
1277 		goto out;
1278 
1279 	if (p_dev->_removed)
1280 		goto out;
1281 
1282 	if (p_dev->suspended)
1283 		goto out;
1284 
1285 	ret = p_dev;
1286  out:
1287 	pcmcia_put_dev(p_dev);
1288 	return ret;
1289 }
1290 EXPORT_SYMBOL(pcmcia_dev_present);
1291 
1292 
1293 static struct pcmcia_callback pcmcia_bus_callback = {
1294 	.owner = THIS_MODULE,
1295 	.event = ds_event,
1296 	.requery = pcmcia_bus_rescan,
1297 	.suspend = pcmcia_bus_suspend,
1298 	.resume = pcmcia_bus_resume,
1299 };
1300 
1301 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1302 					   struct class_interface *class_intf)
1303 {
1304 	struct pcmcia_socket *socket = dev_get_drvdata(dev);
1305 	int ret;
1306 
1307 	socket = pcmcia_get_socket(socket);
1308 	if (!socket) {
1309 		dev_printk(KERN_ERR, dev,
1310 			   "PCMCIA obtaining reference to socket failed\n");
1311 		return -ENODEV;
1312 	}
1313 
1314 	/*
1315 	 * Ugly. But we want to wait for the socket threads to have started up.
1316 	 * We really should let the drivers themselves drive some of this..
1317 	 */
1318 	msleep(250);
1319 
1320 #ifdef CONFIG_PCMCIA_IOCTL
1321 	init_waitqueue_head(&socket->queue);
1322 #endif
1323 	INIT_LIST_HEAD(&socket->devices_list);
1324 	INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1325 	memset(&socket->pcmcia_state, 0, sizeof(u8));
1326 	socket->device_count = 0;
1327 
1328 	ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1329 	if (ret) {
1330 		dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1331 		pcmcia_put_socket(socket);
1332 		return (ret);
1333 	}
1334 
1335 	return 0;
1336 }
1337 
1338 static void pcmcia_bus_remove_socket(struct device *dev,
1339 				     struct class_interface *class_intf)
1340 {
1341 	struct pcmcia_socket *socket = dev_get_drvdata(dev);
1342 
1343 	if (!socket)
1344 		return;
1345 
1346 	socket->pcmcia_state.dead = 1;
1347 	pccard_register_pcmcia(socket, NULL);
1348 
1349 	/* unregister any unbound devices */
1350 	mutex_lock(&socket->skt_mutex);
1351 	pcmcia_card_remove(socket, NULL);
1352 	mutex_unlock(&socket->skt_mutex);
1353 
1354 	pcmcia_put_socket(socket);
1355 
1356 	return;
1357 }
1358 
1359 
1360 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1361 static struct class_interface pcmcia_bus_interface __refdata = {
1362 	.class = &pcmcia_socket_class,
1363 	.add_dev = &pcmcia_bus_add_socket,
1364 	.remove_dev = &pcmcia_bus_remove_socket,
1365 };
1366 
1367 
1368 struct bus_type pcmcia_bus_type = {
1369 	.name = "pcmcia",
1370 	.uevent = pcmcia_bus_uevent,
1371 	.match = pcmcia_bus_match,
1372 	.dev_attrs = pcmcia_dev_attrs,
1373 	.probe = pcmcia_device_probe,
1374 	.remove = pcmcia_device_remove,
1375 	.suspend = pcmcia_dev_suspend,
1376 	.resume = pcmcia_dev_resume,
1377 };
1378 
1379 
1380 static int __init init_pcmcia_bus(void)
1381 {
1382 	int ret;
1383 
1384 	spin_lock_init(&pcmcia_dev_list_lock);
1385 
1386 	ret = bus_register(&pcmcia_bus_type);
1387 	if (ret < 0) {
1388 		printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1389 		return ret;
1390 	}
1391 	ret = class_interface_register(&pcmcia_bus_interface);
1392 	if (ret < 0) {
1393 		printk(KERN_WARNING
1394 			"pcmcia: class_interface_register error: %d\n", ret);
1395 		bus_unregister(&pcmcia_bus_type);
1396 		return ret;
1397 	}
1398 
1399 	pcmcia_setup_ioctl();
1400 
1401 	return 0;
1402 }
1403 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1404 			       * pcmcia_socket_class is already registered */
1405 
1406 
1407 static void __exit exit_pcmcia_bus(void)
1408 {
1409 	pcmcia_cleanup_ioctl();
1410 
1411 	class_interface_unregister(&pcmcia_bus_interface);
1412 
1413 	bus_unregister(&pcmcia_bus_type);
1414 }
1415 module_exit(exit_pcmcia_bus);
1416 
1417 
1418 MODULE_ALIAS("ds");
1419