blob: bcb9ef103427c3850bc69db238d147166fdde1e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dominik Brodowski8661bb52006-03-02 00:02:33 +010013 * (C) 2003 - 2006 Dominik Brodowski
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Dominik Brodowski3b659fb2005-06-27 16:28:51 -070017#include <linux/module.h>
18#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/list.h>
21#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/workqueue.h>
Dominik Brodowski840c2ac2005-06-27 16:28:04 -070023#include <linux/crc32.h>
Dominik Brodowskidaa95172005-06-27 16:28:14 -070024#include <linux/firmware.h>
Dominik Brodowski360b65b2006-01-10 20:50:39 +010025#include <linux/kref.h>
James Bottomley43d9f7f2007-10-16 01:23:58 -070026#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <pcmcia/cs_types.h>
29#include <pcmcia/cs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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
40MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
41MODULE_DESCRIPTION("PCMCIA Driver Services");
42MODULE_LICENSE("GPL");
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070045spinlock_t pcmcia_dev_list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*====================================================================*/
48
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070049static 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
Dominik Brodowskif8cfa612005-11-14 21:25:51 +010055 if (!p_drv->probe || !p_drv->remove)
Pavel Roskinba5bb6b2005-07-28 01:07:20 -070056 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
57 "function\n", p_drv->drv.name);
Dominik Brodowski1e212f32005-07-07 17:59:00 -070058
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070059 while (did && did->match_flags) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010060 for (i = 0; i < 4; i++) {
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070061 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);
Dominik Brodowski5085cb22005-06-27 16:28:45 -070072 printk(KERN_DEBUG "pcmcia: see "
73 "Documentation/pcmcia/devicetable.txt for "
74 "details\n");
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070075 }
76 did++;
77 }
78
79 return;
80}
81
Dominik Brodowskidaa95172005-06-27 16:28:14 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*======================================================================*/
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Bernhard Walle6179b552007-05-06 14:48:44 -070086struct 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 */
100static ssize_t
101pcmcia_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};
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100108 int fields = 0;
Bernhard Walle6179b552007-05-06 14:48:44 -0700109 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
Bernhard Walle6179b552007-05-06 14:48:44 -0700121 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);
Wolfram Sangb4b3d7b2009-07-20 10:58:59 +0200130 list_add_tail(&dynid->node, &pdrv->dynids.list);
Bernhard Walle6179b552007-05-06 14:48:44 -0700131 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}
142static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
143
144static void
145pcmcia_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
157static int
158pcmcia_create_newid_file(struct pcmcia_driver *drv)
159{
160 int error = 0;
161 if (drv->probe != NULL)
Greg Kroah-Hartman2344c6d2007-11-28 12:23:18 -0800162 error = driver_create_file(&drv->drv, &driver_attr_new_id);
Bernhard Walle6179b552007-05-06 14:48:44 -0700163 return error;
164}
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * pcmcia_register_driver - register a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800169 * @driver: the &driver being registered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * Registers a PCMCIA driver with the PCMCIA bus core.
172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173int pcmcia_register_driver(struct pcmcia_driver *driver)
174{
Bernhard Walle6179b552007-05-06 14:48:44 -0700175 int error;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!driver)
178 return -EINVAL;
179
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700180 pcmcia_check_driver(driver);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* initialize common fields */
183 driver->drv.bus = &pcmcia_bus_type;
184 driver->drv.owner = driver->owner;
Bernhard Walle6179b552007-05-06 14:48:44 -0700185 spin_lock_init(&driver->dynids.lock);
186 INIT_LIST_HEAD(&driver->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200188 pr_debug("registering driver %s\n", driver->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200189
Bernhard Walle6179b552007-05-06 14:48:44 -0700190 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200EXPORT_SYMBOL(pcmcia_register_driver);
201
202/**
203 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800204 * @driver: the &driver being unregistered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206void pcmcia_unregister_driver(struct pcmcia_driver *driver)
207{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200208 pr_debug("unregistering driver %s\n", driver->drv.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 driver_unregister(&driver->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700210 pcmcia_free_dynids(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212EXPORT_SYMBOL(pcmcia_unregister_driver);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215/* pcmcia_device handling */
216
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100217struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
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
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700226void pcmcia_put_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 if (p_dev)
229 put_device(&p_dev->dev);
230}
231
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100232static void pcmcia_release_function(struct kref *ref)
233{
234 struct config_t *c = container_of(ref, struct config_t, ref);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200235 pr_debug("releasing config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100236 kfree(c);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static void pcmcia_release_dev(struct device *dev)
240{
241 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200242 dev_dbg(dev, "releasing device\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -0700243 pcmcia_put_socket(p_dev->socket);
Brice Goglinbd65a682005-09-09 13:03:29 -0700244 kfree(p_dev->devname);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100245 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 kfree(p_dev);
247}
248
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500249static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100250{
251 if (!s->pcmcia_state.device_add_pending) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200252 dev_dbg(&s->dev, "scheduling to add %s secondary"
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200253 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100254 s->pcmcia_state.device_add_pending = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500255 s->pcmcia_state.mfc_pfc = mfc;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100256 schedule_work(&s->device_add);
257 }
258 return;
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100261static int pcmcia_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct pcmcia_device *p_dev;
264 struct pcmcia_driver *p_drv;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100265 struct pcmcia_device_id *did;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100266 struct pcmcia_socket *s;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400267 cistpl_config_t cis_config;
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100268 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int ret = 0;
270
271 dev = get_device(dev);
272 if (!dev)
273 return -ENODEV;
274
275 p_dev = to_pcmcia_dev(dev);
276 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100277 s = p_dev->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700279 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
Alan Coxcec5eb72008-09-22 15:58:14 +0100280 * which is an ugly hack. Once the driver probe is called it may
281 * and often will overwrite the match data so we must save it first
282 *
283 * handle pseudo multifunction devices:
284 * there are at most two pseudo multifunction devices.
285 * if we're matching against the first, schedule a
286 * call which will then check whether there are two
287 * pseudo devices, and if not, add the second one.
288 */
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700289 did = dev_get_drvdata(&p_dev->dev);
Alan Coxcec5eb72008-09-22 15:58:14 +0100290
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200291 dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200292
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100293 if ((!p_drv->probe) || (!p_dev->function_config) ||
294 (!try_module_get(p_drv->owner))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ret = -EINVAL;
296 goto put_dev;
297 }
298
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400299 /* set up some more device information */
300 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
301 &cis_config);
302 if (!ret) {
303 p_dev->conf.ConfigBase = cis_config.base;
304 p_dev->conf.Present = cis_config.rmask[0];
305 } else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200306 dev_printk(KERN_INFO, dev,
307 "pcmcia: could not parse base and rmask0 of CIS\n");
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400308 p_dev->conf.ConfigBase = 0;
309 p_dev->conf.Present = 0;
310 }
311
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100312 ret = p_drv->probe(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200313 if (ret) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200314 dev_dbg(dev, "binding to %s failed with %d\n",
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200315 p_drv->drv.name, ret);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100316 goto put_module;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200317 }
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100318
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100319 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Hugh Dickins4ceadbf2006-03-10 21:13:44 +0000320 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100321 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500322 pcmcia_add_device_later(p_dev->socket, 0);
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100323 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100324
Alan Coxcec5eb72008-09-22 15:58:14 +0100325put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (ret)
327 module_put(p_drv->owner);
Alan Coxcec5eb72008-09-22 15:58:14 +0100328put_dev:
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100329 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 put_device(dev);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100335/*
336 * Removes a PCMCIA card from the device tree and socket list.
337 */
338static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
339{
340 struct pcmcia_device *p_dev;
341 struct pcmcia_device *tmp;
342 unsigned long flags;
343
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200344 dev_dbg(leftover ? &leftover->dev : &s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200345 "pcmcia_card_remove(%d) %s\n", s->sock,
346 leftover ? leftover->devname : "");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100347
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100348 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100349 if (!leftover)
350 s->device_count = 0;
351 else
352 s->device_count = 1;
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100353 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100354
355 /* unregister all pcmcia_devices registered with this socket, except leftover */
356 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
357 if (p_dev == leftover)
358 continue;
359
360 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
361 list_del(&p_dev->socket_device_list);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100362 p_dev->_removed = 1;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100363 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
364
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200365 dev_dbg(&p_dev->dev, "unregistering device\n");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100366 device_unregister(&p_dev->dev);
367 }
368
369 return;
370}
371
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100372static int pcmcia_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct pcmcia_device *p_dev;
375 struct pcmcia_driver *p_drv;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100376 struct pcmcia_device_id *did;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100377 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 p_dev = to_pcmcia_dev(dev);
380 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100381
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200382 dev_dbg(dev, "removing device\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200383
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100384 /* If we're removing the primary module driving a
385 * pseudo multi-function card, we need to unbind
386 * all devices
387 */
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700388 did = dev_get_drvdata(&p_dev->dev);
Hugh Dickinsb2f51a12006-03-10 21:15:19 +0000389 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100390 (p_dev->socket->device_count > 0) &&
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100391 (p_dev->device_no == 0))
392 pcmcia_card_remove(p_dev->socket, p_dev);
393
394 /* detach the "instance" */
Dominik Brodowskif3990712005-11-14 21:25:23 +0100395 if (!p_drv)
396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dominik Brodowskif3990712005-11-14 21:25:23 +0100398 if (p_drv->remove)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100399 p_drv->remove(p_dev);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100400
Dominik Brodowskia0aab142006-04-04 11:09:26 +0200401 p_dev->dev_node = NULL;
402
Dominik Brodowskif3990712005-11-14 21:25:23 +0100403 /* check for proper unloading */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100404 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200405 dev_printk(KERN_INFO, dev,
406 "pcmcia: driver %s did not release config properly\n",
407 p_drv->drv.name);
Dominik Brodowskif3990712005-11-14 21:25:23 +0100408
409 for (i = 0; i < MAX_WIN; i++)
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100410 if (p_dev->_win & CLIENT_WIN_REQ(i))
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200411 dev_printk(KERN_INFO, dev,
412 "pcmcia: driver %s did not release window properly\n",
413 p_drv->drv.name);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100414
Dominik Brodowskif3990712005-11-14 21:25:23 +0100415 /* references from pcmcia_probe_device */
416 pcmcia_put_dev(p_dev);
417 module_put(p_drv->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return 0;
420}
421
422
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * pcmcia_device_query -- determine information about a pcmcia device
425 */
426static int pcmcia_device_query(struct pcmcia_device *p_dev)
427{
428 cistpl_manfid_t manf_id;
429 cistpl_funcid_t func_id;
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700430 cistpl_vers_1_t *vers1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned int i;
432
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700433 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
434 if (!vers1)
435 return -ENOMEM;
436
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200437 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 CISTPL_MANFID, &manf_id)) {
439 p_dev->manf_id = manf_id.manf;
440 p_dev->card_id = manf_id.card;
441 p_dev->has_manf_id = 1;
442 p_dev->has_card_id = 1;
443 }
444
445 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
446 CISTPL_FUNCID, &func_id)) {
447 p_dev->func_id = func_id.func;
448 p_dev->has_func_id = 1;
449 } else {
450 /* rule of thumb: cards with no FUNCID, but with
451 * common memory device geometry information, are
452 * probably memory cards (from pcmcia-cs) */
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700453 cistpl_device_geo_t *devgeo;
454
455 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
456 if (!devgeo) {
457 kfree(vers1);
458 return -ENOMEM;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700461 CISTPL_DEVICE_GEO, devgeo)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200462 dev_dbg(&p_dev->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200463 "mem device geometry probably means "
464 "FUNCID_MEMORY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 p_dev->func_id = CISTPL_FUNCID_MEMORY;
466 p_dev->has_func_id = 1;
467 }
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700468 kfree(devgeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200471 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700472 vers1)) {
Dominik Brodowskic5e09522009-10-19 00:04:25 +0200473 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 char *tmp;
475 unsigned int length;
476
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700477 tmp = vers1->str + vers1->ofs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 length = strlen(tmp) + 1;
Janos Farkas6e3d4f22006-03-31 01:04:57 +0200480 if ((length < 2) || (length > 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 continue;
482
483 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
484 GFP_KERNEL);
485 if (!p_dev->prod_id[i])
486 continue;
487
488 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
489 tmp, length);
490 }
491 }
492
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700493 kfree(vers1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
497
498/* device_add_lock is needed to avoid double registration by cardmgr and kernel.
499 * Serializes pcmcia_device_add; will most likely be removed in future.
500 *
501 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
502 * won't work, this doesn't matter much at the moment: the driver core doesn't
503 * support it either.
504 */
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100505static DEFINE_MUTEX(device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100507struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100509 struct pcmcia_device *p_dev, *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned long flags;
511
Dominik Brodowskidc109492005-06-27 16:28:50 -0700512 s = pcmcia_get_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!s)
514 return NULL;
515
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100516 mutex_lock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200518 pr_debug("adding device to %d, function %d\n", s->sock, function);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200519
Dominik Brodowski8084b372005-12-11 21:18:26 +0100520 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!p_dev)
522 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100524 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 p_dev->device_no = (s->device_count++);
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100526 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
527
528 /* max of 4 devices per card */
529 if (p_dev->device_no >= 4)
530 goto err_free;
531
532 p_dev->socket = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 p_dev->func = function;
534
535 p_dev->dev.bus = &pcmcia_bus_type;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200536 p_dev->dev.parent = s->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 p_dev->dev.release = pcmcia_release_dev;
James Bottomley43d9f7f2007-10-16 01:23:58 -0700538 /* by default don't allow DMA */
539 p_dev->dma_mask = DMA_MASK_NONE;
540 p_dev->dev.dma_mask = &p_dev->dma_mask;
Kay Sievers25096982008-11-01 11:46:06 +0100541 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
542 if (!dev_name(&p_dev->dev))
543 goto err_free;
544 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
Brice Goglinbd65a682005-09-09 13:03:29 -0700545 if (!p_dev->devname)
546 goto err_free;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200547 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100550
551 /*
552 * p_dev->function_config must be the same for all card functions.
553 * Note that this is serialized by the device_add_lock, so that
554 * only one such struct will be created.
555 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100556 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
557 if (p_dev->func == tmp_dev->func) {
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100558 p_dev->function_config = tmp_dev->function_config;
Komuro3e879f62008-11-02 19:33:24 +0900559 p_dev->io = tmp_dev->io;
560 p_dev->irq = tmp_dev->irq;
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100561 kref_get(&p_dev->function_config->ref);
562 }
563
564 /* Add to the list in pcmcia_bus_socket */
Komuro6171b882006-04-02 17:39:27 +0900565 list_add(&p_dev->socket_device_list, &s->devices_list);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
568
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100569 if (!p_dev->function_config) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200570 dev_dbg(&p_dev->dev, "creating config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100571 p_dev->function_config = kzalloc(sizeof(struct config_t),
572 GFP_KERNEL);
573 if (!p_dev->function_config)
574 goto err_unreg;
575 kref_init(&p_dev->function_config->ref);
576 }
577
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200578 dev_printk(KERN_NOTICE, &p_dev->dev,
579 "pcmcia: registering new device %s\n",
580 p_dev->devname);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100581
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700582 pcmcia_device_query(p_dev);
583
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100584 if (device_register(&p_dev->dev))
585 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100587 mutex_unlock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 return p_dev;
590
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100591 err_unreg:
592 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
593 list_del(&p_dev->socket_device_list);
594 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err_free:
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100597 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
598 s->device_count--;
599 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
600
Brice Goglinbd65a682005-09-09 13:03:29 -0700601 kfree(p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 kfree(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 err_put:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100604 mutex_unlock(&device_add_lock);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700605 pcmcia_put_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 return NULL;
608}
609
610
611static int pcmcia_card_add(struct pcmcia_socket *s)
612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 cistpl_longlink_mfc_t mfc;
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200614 unsigned int no_funcs, i, no_chains;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int ret = 0;
616
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200617 if (!(s->resource_setup_done)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200618 dev_dbg(&s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200619 "no resources available, delaying card_add\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200623 if (pcmcia_validate_mem(s)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200624 dev_dbg(&s->dev, "validating mem resources failed, "
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200625 "delaying card_add\n");
Dominik Brodowskide759142005-09-28 19:41:56 +0200626 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200627 }
Dominik Brodowskide759142005-09-28 19:41:56 +0200628
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200629 ret = pccard_validate_cis(s, &no_chains);
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200630 if (ret || !no_chains) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200631 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENODEV;
633 }
634
635 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
636 no_funcs = mfc.nfn;
637 else
638 no_funcs = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500639 s->functions = no_funcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100641 for (i = 0; i < no_funcs; i++)
Dominik Brodowskidc109492005-06-27 16:28:50 -0700642 pcmcia_device_add(s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647
David Howells4796b712006-12-06 15:01:18 +0000648static void pcmcia_delayed_add_device(struct work_struct *work)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700649{
David Howellsc4028952006-11-22 14:57:56 +0000650 struct pcmcia_socket *s =
651 container_of(work, struct pcmcia_socket, device_add);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200652 dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500653 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
Dominik Brodowskib5e43912005-06-27 16:28:50 -0700654 s->pcmcia_state.device_add_pending = 0;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500655 s->pcmcia_state.mfc_pfc = 0;
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700656}
657
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700658static int pcmcia_requery(struct device *dev, void * _data)
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700659{
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700660 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200661 if (!p_dev->dev.driver) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200662 dev_dbg(dev, "update device information\n");
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700663 pcmcia_device_query(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200664 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700665
666 return 0;
667}
668
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500669static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700670{
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500671 int no_devices = 0;
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400672 int ret = 0;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700673 unsigned long flags;
674
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100675 /* must be called with skt_mutex held */
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200676 dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200677
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700678 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700679 if (list_empty(&skt->devices_list))
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500680 no_devices = 1;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700681 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
682
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500683 /* If this is because of a CIS override, start over */
684 if (new_cis && !no_devices)
685 pcmcia_card_remove(skt, NULL);
686
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700687 /* if no devices were added for this socket yet because of
688 * missing resource information or other trouble, we need to
689 * do this now. */
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500690 if (no_devices || new_cis) {
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400691 ret = pcmcia_card_add(skt);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700692 if (ret)
693 return;
694 }
695
696 /* some device information might have changed because of a CIS
697 * update or because we can finally read it correctly... so
698 * determine it again, overwriting old values if necessary. */
699 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
700
701 /* we re-scan all devices, not just the ones connected to this
702 * socket. This does not matter, though. */
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400703 ret = bus_rescan_devices(&pcmcia_bus_type);
704 if (ret)
705 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700706}
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700707
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500708#ifdef CONFIG_PCMCIA_LOAD_CIS
709
710/**
711 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
Randy Dunlap78187862007-12-10 15:49:22 -0800712 * @dev: the pcmcia device which needs a CIS override
713 * @filename: requested filename in /lib/firmware/
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500714 *
715 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
716 * the one provided by the card is broken. The firmware files reside in
717 * /lib/firmware/ in userspace.
718 */
719static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
720{
721 struct pcmcia_socket *s = dev->socket;
722 const struct firmware *fw;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500723 int ret = -ENOMEM;
724 int no_funcs;
725 int old_funcs;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500726 cistpl_longlink_mfc_t mfc;
727
728 if (!filename)
729 return -EINVAL;
730
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200731 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500732
Samuel Ortized62ace2009-05-27 00:49:35 +0200733 if (request_firmware(&fw, filename, &dev->dev) == 0) {
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200734 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
735 ret = -EINVAL;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200736 dev_printk(KERN_ERR, &dev->dev,
737 "pcmcia: CIS override is too big\n");
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500738 goto release;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200739 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500740
Dominik Brodowski53efec92008-07-28 19:44:05 +0200741 if (!pcmcia_replace_cis(s, fw->data, fw->size))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500742 ret = 0;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200743 else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200744 dev_printk(KERN_ERR, &dev->dev,
745 "pcmcia: CIS override failed\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200746 goto release;
747 }
748
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500749
750 /* update information */
751 pcmcia_device_query(dev);
752
753 /* does this cis override add or remove functions? */
754 old_funcs = s->functions;
755
756 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
757 no_funcs = mfc.nfn;
758 else
759 no_funcs = 1;
760 s->functions = no_funcs;
761
762 if (old_funcs > no_funcs)
763 pcmcia_card_remove(s, dev);
764 else if (no_funcs > old_funcs)
765 pcmcia_add_device_later(s, 1);
766 }
767 release:
768 release_firmware(fw);
769
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100770 return ret;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500771}
772
773#else /* !CONFIG_PCMCIA_LOAD_CIS */
774
775static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
776{
777 return -ENODEV;
778}
779
780#endif
781
782
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700783static inline int pcmcia_devmatch(struct pcmcia_device *dev,
784 struct pcmcia_device_id *did)
785{
786 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
787 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
788 return 0;
789 }
790
791 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
792 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
793 return 0;
794 }
795
796 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
797 if (dev->func != did->function)
798 return 0;
799 }
800
801 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
802 if (!dev->prod_id[0])
803 return 0;
804 if (strcmp(did->prod_id[0], dev->prod_id[0]))
805 return 0;
806 }
807
808 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
809 if (!dev->prod_id[1])
810 return 0;
811 if (strcmp(did->prod_id[1], dev->prod_id[1]))
812 return 0;
813 }
814
815 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
816 if (!dev->prod_id[2])
817 return 0;
818 if (strcmp(did->prod_id[2], dev->prod_id[2]))
819 return 0;
820 }
821
822 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
823 if (!dev->prod_id[3])
824 return 0;
825 if (strcmp(did->prod_id[3], dev->prod_id[3]))
826 return 0;
827 }
828
829 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700830 if (dev->device_no != did->device_no)
831 return 0;
832 }
833
834 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
835 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
836 return 0;
837
838 /* if this is a pseudo-multi-function device,
839 * we need explicit matches */
840 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
841 return 0;
842 if (dev->device_no)
843 return 0;
844
845 /* also, FUNC_ID matching needs to be activated by userspace
846 * after it has re-checked that there is no possible module
847 * with a prod_id/manf_id/card_id match.
848 */
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200849 dev_dbg(&dev->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200850 "skipping FUNC_ID match until userspace interaction\n");
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700851 if (!dev->allow_func_id_match)
852 return 0;
853 }
854
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700855 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200856 dev_dbg(&dev->dev, "device needs a fake CIS\n");
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700857 if (!dev->socket->fake_cis)
858 pcmcia_load_firmware(dev, did->cisfile);
859
860 if (!dev->socket->fake_cis)
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700861 return 0;
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700862 }
863
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700864 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
865 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100866 for (i = 0; i < 4; i++)
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700867 if (dev->prod_id[i])
868 return 0;
869 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
870 return 0;
871 }
872
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700873 dev_set_drvdata(&dev->dev, did);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700874
875 return 1;
876}
877
878
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100879static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
880{
881 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
882 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700883 struct pcmcia_device_id *did = p_drv->id_table;
Bernhard Walle6179b552007-05-06 14:48:44 -0700884 struct pcmcia_dynid *dynid;
885
886 /* match dynamic devices first */
887 spin_lock(&p_drv->dynids.lock);
888 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200889 dev_dbg(dev, "trying to match to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -0700890 if (pcmcia_devmatch(p_dev, &dynid->id)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200891 dev_dbg(dev, "matched to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -0700892 spin_unlock(&p_drv->dynids.lock);
893 return 1;
894 }
895 }
896 spin_unlock(&p_drv->dynids.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Dominik Brodowski0e0fad82006-01-15 01:14:31 +0100898#ifdef CONFIG_PCMCIA_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* matching by cardmgr */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200900 if (p_dev->cardmgr == p_drv) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200901 dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200903 }
Dominik Brodowski0e0fad82006-01-15 01:14:31 +0100904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700906 while (did && did->match_flags) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200907 dev_dbg(dev, "trying to match to %s\n", drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200908 if (pcmcia_devmatch(p_dev, did)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200909 dev_dbg(dev, "matched to %s\n", drv->name);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700910 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200911 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700912 did++;
913 }
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700918#ifdef CONFIG_HOTPLUG
919
Kay Sievers7eff2e72007-08-14 15:15:12 +0200920static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700921{
922 struct pcmcia_device *p_dev;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200923 int i;
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700924 u32 hash[4] = { 0, 0, 0, 0};
925
926 if (!dev)
927 return -ENODEV;
928
929 p_dev = to_pcmcia_dev(dev);
930
931 /* calculate hashes */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100932 for (i = 0; i < 4; i++) {
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700933 if (!p_dev->prod_id[i])
934 continue;
935 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
936 }
937
Kay Sievers7eff2e72007-08-14 15:15:12 +0200938 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700939 return -ENOMEM;
940
Kay Sievers7eff2e72007-08-14 15:15:12 +0200941 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700942 return -ENOMEM;
943
Kay Sievers7eff2e72007-08-14 15:15:12 +0200944 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
Kay Sievers312c0042005-11-16 09:00:00 +0100945 "pa%08Xpb%08Xpc%08Xpd%08X",
946 p_dev->has_manf_id ? p_dev->manf_id : 0,
947 p_dev->has_card_id ? p_dev->card_id : 0,
948 p_dev->has_func_id ? p_dev->func_id : 0,
949 p_dev->func,
950 p_dev->device_no,
951 hash[0],
952 hash[1],
953 hash[2],
954 hash[3]))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700955 return -ENOMEM;
956
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700957 return 0;
958}
959
960#else
961
Kay Sievers7eff2e72007-08-14 15:15:12 +0200962static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700963{
964 return -ENODEV;
965}
966
967#endif
968
Alan Stern3f8df782007-07-12 16:57:22 -0400969/************************ runtime PM support ***************************/
970
971static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
972static int pcmcia_dev_resume(struct device *dev);
973
974static int runtime_suspend(struct device *dev)
975{
976 int rc;
977
978 down(&dev->sem);
979 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
980 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -0400981 return rc;
982}
983
Dominik Brodowski933a8382009-12-29 18:21:39 +0100984static int runtime_resume(struct device *dev)
Alan Stern3f8df782007-07-12 16:57:22 -0400985{
986 int rc;
987
988 down(&dev->sem);
989 rc = pcmcia_dev_resume(dev);
990 up(&dev->sem);
Dominik Brodowski933a8382009-12-29 18:21:39 +0100991 return rc;
Alan Stern3f8df782007-07-12 16:57:22 -0400992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/************************ per-device sysfs output ***************************/
995
996#define pcmcia_device_attr(field, test, format) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400997static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{ \
999 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001000 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003#define pcmcia_device_stringattr(name, field) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001004static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{ \
1006 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001007 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010pcmcia_device_attr(func, socket, "0x%02x\n");
1011pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1012pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1013pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1014pcmcia_device_stringattr(prod_id1, prod_id[0]);
1015pcmcia_device_stringattr(prod_id2, prod_id[1]);
1016pcmcia_device_stringattr(prod_id3, prod_id[2]);
1017pcmcia_device_stringattr(prod_id4, prod_id[3]);
1018
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001019
1020static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1021{
1022 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1023
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001024 if (p_dev->suspended)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001025 return sprintf(buf, "off\n");
1026 else
1027 return sprintf(buf, "on\n");
1028}
1029
1030static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1031 const char *buf, size_t count)
1032{
1033 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1034 int ret = 0;
1035
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001036 if (!count)
1037 return -EINVAL;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001038
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001039 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
Alan Stern3f8df782007-07-12 16:57:22 -04001040 ret = runtime_suspend(dev);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001041 else if (p_dev->suspended && !strncmp(buf, "on", 2))
Dominik Brodowski933a8382009-12-29 18:21:39 +01001042 ret = runtime_resume(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001043
1044 return ret ? ret : count;
1045}
1046
1047
Dominik Brodowski37045112005-06-30 02:58:47 -07001048static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001049{
1050 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1051 int i;
1052 u32 hash[4] = { 0, 0, 0, 0};
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001053
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001054 /* calculate hashes */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001055 for (i = 0; i < 4; i++) {
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001056 if (!p_dev->prod_id[i])
1057 continue;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001058 hash[i] = crc32(0, p_dev->prod_id[i],
1059 strlen(p_dev->prod_id[i]));
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001060 }
1061 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1062 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1063 p_dev->has_manf_id ? p_dev->manf_id : 0,
1064 p_dev->has_card_id ? p_dev->card_id : 0,
1065 p_dev->has_func_id ? p_dev->func_id : 0,
1066 p_dev->func, p_dev->device_no,
1067 hash[0], hash[1], hash[2], hash[3]);
1068}
1069
1070static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1071 struct device_attribute *attr, const char *buf, size_t count)
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001072{
1073 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001074 int ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001075
1076 if (!count)
1077 return -EINVAL;
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001078
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001079 mutex_lock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001080 p_dev->allow_func_id_match = 1;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001081 mutex_unlock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001082
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001083 ret = bus_rescan_devices(&pcmcia_bus_type);
1084 if (ret)
1085 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1086 "allowing func_id matches\n");
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001087
1088 return count;
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static struct device_attribute pcmcia_dev_attrs[] = {
1092 __ATTR(function, 0444, func_show, NULL),
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001093 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 __ATTR_RO(func_id),
1095 __ATTR_RO(manf_id),
1096 __ATTR_RO(card_id),
1097 __ATTR_RO(prod_id1),
1098 __ATTR_RO(prod_id2),
1099 __ATTR_RO(prod_id3),
1100 __ATTR_RO(prod_id4),
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001101 __ATTR_RO(modalias),
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001102 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 __ATTR_NULL,
1104};
1105
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001106/* PM support, also needed for reset */
1107
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001108static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001109{
1110 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1111 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001112 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001113
David Brownelld6b4fa62008-02-04 22:27:37 -08001114 if (p_dev->suspended)
1115 return 0;
1116
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001117 dev_dbg(dev, "suspending\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001118
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001119 if (dev->driver)
1120 p_drv = to_pcmcia_drv(dev->driver);
1121
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001122 if (!p_drv)
1123 goto out;
1124
1125 if (p_drv->suspend) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001126 ret = p_drv->suspend(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001127 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001128 dev_printk(KERN_ERR, dev,
1129 "pcmcia: device %s (driver %s) did "
1130 "not want to go to sleep (%d)\n",
1131 p_dev->devname, p_drv->drv.name, ret);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001132 goto out;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001133 }
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001134 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001135
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001136 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001137 dev_dbg(dev, "releasing configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001138 pcmcia_release_configuration(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001139 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001140
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001141 out:
1142 if (!ret)
1143 p_dev->suspended = 1;
1144 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001145}
1146
1147
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001148static int pcmcia_dev_resume(struct device *dev)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001149{
1150 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001151 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001152 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001153
David Brownelld6b4fa62008-02-04 22:27:37 -08001154 if (!p_dev->suspended)
1155 return 0;
1156
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001157 dev_dbg(dev, "resuming\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001158
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001159 if (dev->driver)
1160 p_drv = to_pcmcia_drv(dev->driver);
1161
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001162 if (!p_drv)
1163 goto out;
1164
1165 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001166 dev_dbg(dev, "requesting configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001167 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1168 if (ret)
1169 goto out;
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001170 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001171
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001172 if (p_drv->resume)
1173 ret = p_drv->resume(p_dev);
1174
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001175 out:
1176 if (!ret)
1177 p_dev->suspended = 0;
1178 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001179}
1180
1181
1182static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1183{
1184 struct pcmcia_socket *skt = _data;
1185 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1186
Alan Stern3f8df782007-07-12 16:57:22 -04001187 if (p_dev->socket != skt || p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001188 return 0;
1189
Alan Stern3f8df782007-07-12 16:57:22 -04001190 return runtime_suspend(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001191}
1192
1193static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1194{
1195 struct pcmcia_socket *skt = _data;
1196 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1197
Alan Stern3f8df782007-07-12 16:57:22 -04001198 if (p_dev->socket != skt || !p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001199 return 0;
1200
Alan Stern3f8df782007-07-12 16:57:22 -04001201 runtime_resume(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001202
1203 return 0;
1204}
1205
1206static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1207{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001208 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001209 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1210 return 0;
1211}
1212
1213static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1214{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001215 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001216 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1217 pcmcia_bus_suspend_callback)) {
1218 pcmcia_bus_resume(skt);
1219 return -EIO;
1220 }
1221 return 0;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225/*======================================================================
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 The card status event handler.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229======================================================================*/
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/* Normally, the event is passed to individual drivers after
1232 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1233 * is inversed to maintain historic compatibility.
1234 */
1235
1236static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1237{
Dominik Brodowskidc109492005-06-27 16:28:50 -07001238 struct pcmcia_socket *s = pcmcia_get_socket(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Florin Malita16174062006-05-24 21:21:31 -04001240 if (!s) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001241 dev_printk(KERN_ERR, &skt->dev,
1242 "PCMCIA obtaining reference to socket " \
1243 "failed, event 0x%x lost!\n", event);
Florin Malita16174062006-05-24 21:21:31 -04001244 return -ENODEV;
1245 }
1246
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001247 dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001248 event, priority, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001250 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case CS_EVENT_CARD_REMOVAL:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001252 s->pcmcia_state.present = 0;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +01001253 pcmcia_card_remove(skt, NULL);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001254 handle_event(skt, event);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001255 mutex_lock(&s->ops_mutex);
Dominik Brodowski180c33e2010-01-02 17:34:09 +01001256 destroy_cis_cache(s);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001257 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case CS_EVENT_CARD_INSERTION:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001261 s->pcmcia_state.present = 1;
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001262 mutex_lock(&s->ops_mutex);
Dominik Brodowski180c33e2010-01-02 17:34:09 +01001263 destroy_cis_cache(s); /* to be on the safe side... */
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001264 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 pcmcia_card_add(skt);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001266 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268
1269 case CS_EVENT_EJECTION_REQUEST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
1271
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001272 case CS_EVENT_PM_RESUME:
Dominik Brodowski88b060d2010-01-02 14:14:23 +01001273 if (verify_cis_cache(skt) != 0) {
1274 dev_dbg(&skt->dev, "cis mismatch - different card\n");
1275 /* first, remove the card */
1276 ds_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001277 mutex_lock(&s->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +01001278 destroy_cis_cache(skt);
1279 kfree(skt->fake_cis);
1280 skt->fake_cis = NULL;
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001281 mutex_unlock(&s->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +01001282 /* now, add the new card */
1283 ds_event(skt, CS_EVENT_CARD_INSERTION,
1284 CS_EVENT_PRI_LOW);
1285 }
1286 handle_event(skt, event);
1287 break;
1288
1289 case CS_EVENT_PM_SUSPEND:
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001290 case CS_EVENT_RESET_PHYSICAL:
1291 case CS_EVENT_CARD_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 default:
Dominik Brodowskidc109492005-06-27 16:28:50 -07001293 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
1295 }
1296
Dominik Brodowskidc109492005-06-27 16:28:50 -07001297 pcmcia_put_socket(s);
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300} /* ds_event */
1301
1302
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001303struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001304{
1305 struct pcmcia_device *p_dev;
1306 struct pcmcia_device *ret = NULL;
1307
1308 p_dev = pcmcia_get_dev(_p_dev);
1309 if (!p_dev)
1310 return NULL;
1311
1312 if (!p_dev->socket->pcmcia_state.present)
1313 goto out;
1314
1315 if (p_dev->_removed)
1316 goto out;
1317
1318 if (p_dev->suspended)
1319 goto out;
1320
1321 ret = p_dev;
1322 out:
1323 pcmcia_put_dev(p_dev);
1324 return ret;
1325}
1326EXPORT_SYMBOL(pcmcia_dev_present);
1327
1328
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001329static struct pcmcia_callback pcmcia_bus_callback = {
1330 .owner = THIS_MODULE,
1331 .event = ds_event,
1332 .requery = pcmcia_bus_rescan,
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001333 .validate = pccard_validate_cis,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001334 .suspend = pcmcia_bus_suspend,
1335 .resume = pcmcia_bus_resume,
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001336};
1337
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001338static int __devinit pcmcia_bus_add_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001339 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001341 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int ret;
1343
Dominik Brodowskidc109492005-06-27 16:28:50 -07001344 socket = pcmcia_get_socket(socket);
1345 if (!socket) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001346 dev_printk(KERN_ERR, dev,
1347 "PCMCIA obtaining reference to socket failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENODEV;
1349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Ugly. But we want to wait for the socket threads to have started up.
1353 * We really should let the drivers themselves drive some of this..
1354 */
1355 msleep(250);
1356
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001357 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
1358 if (ret) {
1359 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1360 pcmcia_put_socket(socket);
1361 return ret;
1362 }
1363
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001364#ifdef CONFIG_PCMCIA_IOCTL
Dominik Brodowskidc109492005-06-27 16:28:50 -07001365 init_waitqueue_head(&socket->queue);
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001366#endif
Dominik Brodowskidc109492005-06-27 16:28:50 -07001367 INIT_LIST_HEAD(&socket->devices_list);
David Howells4796b712006-12-06 15:01:18 +00001368 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001369 memset(&socket->pcmcia_state, 0, sizeof(u8));
1370 socket->device_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001372 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001374 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -07001375 pcmcia_put_socket(socket);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 return 0;
1380}
1381
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001382static void pcmcia_bus_remove_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001383 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001385 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Dominik Brodowskidc109492005-06-27 16:28:50 -07001387 if (!socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return;
1389
Dominik Brodowskidc109492005-06-27 16:28:50 -07001390 socket->pcmcia_state.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 pccard_register_pcmcia(socket, NULL);
1392
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001393 /* unregister any unbound devices */
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001394 mutex_lock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001395 pcmcia_card_remove(socket, NULL);
Dominik Brodowski180c33e2010-01-02 17:34:09 +01001396 release_cis_mem(socket);
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001397 mutex_unlock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001398
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001399 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1400
Dominik Brodowskidc109492005-06-27 16:28:50 -07001401 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 return;
1404}
1405
1406
1407/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
Sam Ravnborged49f5d2008-05-01 04:34:50 -07001408static struct class_interface pcmcia_bus_interface __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 .class = &pcmcia_socket_class,
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001410 .add_dev = &pcmcia_bus_add_socket,
1411 .remove_dev = &pcmcia_bus_remove_socket,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412};
1413
1414
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001415struct bus_type pcmcia_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 .name = "pcmcia",
Kay Sievers312c0042005-11-16 09:00:00 +01001417 .uevent = pcmcia_bus_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .match = pcmcia_bus_match,
1419 .dev_attrs = pcmcia_dev_attrs,
Russell King1d0baa32006-01-05 14:40:58 +00001420 .probe = pcmcia_device_probe,
1421 .remove = pcmcia_device_remove,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001422 .suspend = pcmcia_dev_suspend,
1423 .resume = pcmcia_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426
1427static int __init init_pcmcia_bus(void)
1428{
Randy Dunlapace7d472006-10-20 14:44:12 -07001429 int ret;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 spin_lock_init(&pcmcia_dev_list_lock);
1432
Randy Dunlapace7d472006-10-20 14:44:12 -07001433 ret = bus_register(&pcmcia_bus_type);
1434 if (ret < 0) {
1435 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1436 return ret;
1437 }
1438 ret = class_interface_register(&pcmcia_bus_interface);
1439 if (ret < 0) {
1440 printk(KERN_WARNING
1441 "pcmcia: class_interface_register error: %d\n", ret);
1442 bus_unregister(&pcmcia_bus_type);
1443 return ret;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001446 pcmcia_setup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return 0;
1449}
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001450fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * pcmcia_socket_class is already registered */
1452
1453
1454static void __exit exit_pcmcia_bus(void)
1455{
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001456 pcmcia_cleanup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001458 class_interface_unregister(&pcmcia_bus_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 bus_unregister(&pcmcia_bus_type);
1461}
1462module_exit(exit_pcmcia_bus);
1463
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465MODULE_ALIAS("ds");