blob: 059699f6363d028b7a4ef1cb3d9e07a62c2a053a [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 Brodowski7b24e792010-07-11 10:26:53 +020013 * (C) 2003 - 2010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <pcmcia/cistpl.h>
30#include <pcmcia/ds.h>
31#include <pcmcia/ss.h>
32
33#include "cs_internal.h"
34
35/*====================================================================*/
36
37/* Module parameters */
38
39MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
40MODULE_DESCRIPTION("PCMCIA Driver Services");
41MODULE_LICENSE("GPL");
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*====================================================================*/
45
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070046static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
47{
Joe Perchese9fb13b2011-05-03 19:29:00 -070048 const struct pcmcia_device_id *did = p_drv->id_table;
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070049 unsigned int i;
50 u32 hash;
51
Dominik Brodowskif8cfa612005-11-14 21:25:51 +010052 if (!p_drv->probe || !p_drv->remove)
Pavel Roskinba5bb6b2005-07-28 01:07:20 -070053 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
Dominik Brodowski2e9b9812010-08-08 11:36:26 +020054 "function\n", p_drv->name);
Dominik Brodowski1e212f32005-07-07 17:59:00 -070055
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070056 while (did && did->match_flags) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010057 for (i = 0; i < 4; i++) {
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070058 if (!did->prod_id[i])
59 continue;
60
61 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
62 if (hash == did->prod_id_hash[i])
63 continue;
64
65 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
66 "product string \"%s\": is 0x%x, should "
Dominik Brodowski2e9b9812010-08-08 11:36:26 +020067 "be 0x%x\n", p_drv->name, did->prod_id[i],
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070068 did->prod_id_hash[i], hash);
Dominik Brodowski5085cb22005-06-27 16:28:45 -070069 printk(KERN_DEBUG "pcmcia: see "
70 "Documentation/pcmcia/devicetable.txt for "
71 "details\n");
Dominik Brodowski23a83bf2005-06-27 16:28:07 -070072 }
73 did++;
74 }
75
76 return;
77}
78
Dominik Brodowskidaa95172005-06-27 16:28:14 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*======================================================================*/
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Bernhard Walle6179b552007-05-06 14:48:44 -070083struct pcmcia_dynid {
84 struct list_head node;
85 struct pcmcia_device_id id;
86};
87
88/**
89 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
90 * @driver: target device driver
91 * @buf: buffer for scanning device ID data
92 * @count: input size
93 *
94 * Adds a new dynamic PCMCIA device ID to this driver,
95 * and causes the driver to probe for all devices again.
96 */
97static ssize_t
98pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
99{
100 struct pcmcia_dynid *dynid;
101 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
102 __u16 match_flags, manf_id, card_id;
103 __u8 func_id, function, device_no;
104 __u32 prod_id_hash[4] = {0, 0, 0, 0};
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100105 int fields = 0;
Bernhard Walle6179b552007-05-06 14:48:44 -0700106 int retval = 0;
107
108 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
109 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
110 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
111 if (fields < 6)
112 return -EINVAL;
113
114 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
115 if (!dynid)
116 return -ENOMEM;
117
Bernhard Walle6179b552007-05-06 14:48:44 -0700118 dynid->id.match_flags = match_flags;
119 dynid->id.manf_id = manf_id;
120 dynid->id.card_id = card_id;
121 dynid->id.func_id = func_id;
122 dynid->id.function = function;
123 dynid->id.device_no = device_no;
124 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
125
Dominik Brodowski3f565232010-01-16 13:06:40 +0100126 mutex_lock(&pdrv->dynids.lock);
Wolfram Sangb4b3d7b2009-07-20 10:58:59 +0200127 list_add_tail(&dynid->node, &pdrv->dynids.list);
Dominik Brodowski3f565232010-01-16 13:06:40 +0100128 mutex_unlock(&pdrv->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700129
Alan Sterncef9bc52012-01-24 13:34:41 -0500130 retval = driver_attach(&pdrv->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700131
132 if (retval)
133 return retval;
134 return count;
135}
136static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
137
138static void
139pcmcia_free_dynids(struct pcmcia_driver *drv)
140{
141 struct pcmcia_dynid *dynid, *n;
142
Dominik Brodowski3f565232010-01-16 13:06:40 +0100143 mutex_lock(&drv->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700144 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
145 list_del(&dynid->node);
146 kfree(dynid);
147 }
Dominik Brodowski3f565232010-01-16 13:06:40 +0100148 mutex_unlock(&drv->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700149}
150
151static int
152pcmcia_create_newid_file(struct pcmcia_driver *drv)
153{
154 int error = 0;
155 if (drv->probe != NULL)
Greg Kroah-Hartman2344c6d2007-11-28 12:23:18 -0800156 error = driver_create_file(&drv->drv, &driver_attr_new_id);
Bernhard Walle6179b552007-05-06 14:48:44 -0700157 return error;
158}
159
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/**
162 * pcmcia_register_driver - register a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800163 * @driver: the &driver being registered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * Registers a PCMCIA driver with the PCMCIA bus core.
166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167int pcmcia_register_driver(struct pcmcia_driver *driver)
168{
Bernhard Walle6179b552007-05-06 14:48:44 -0700169 int error;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!driver)
172 return -EINVAL;
173
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700174 pcmcia_check_driver(driver);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* initialize common fields */
177 driver->drv.bus = &pcmcia_bus_type;
178 driver->drv.owner = driver->owner;
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200179 driver->drv.name = driver->name;
Dominik Brodowski3f565232010-01-16 13:06:40 +0100180 mutex_init(&driver->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700181 INIT_LIST_HEAD(&driver->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200183 pr_debug("registering driver %s\n", driver->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200184
Bernhard Walle6179b552007-05-06 14:48:44 -0700185 error = driver_register(&driver->drv);
186 if (error < 0)
187 return error;
188
189 error = pcmcia_create_newid_file(driver);
190 if (error)
191 driver_unregister(&driver->drv);
192
193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195EXPORT_SYMBOL(pcmcia_register_driver);
196
197/**
198 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800199 * @driver: the &driver being unregistered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201void pcmcia_unregister_driver(struct pcmcia_driver *driver)
202{
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200203 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 driver_unregister(&driver->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700205 pcmcia_free_dynids(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207EXPORT_SYMBOL(pcmcia_unregister_driver);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/* pcmcia_device handling */
211
Dominik Brodowski5716d412010-07-11 09:51:14 +0200212static struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct device *tmp_dev;
215 tmp_dev = get_device(&p_dev->dev);
216 if (!tmp_dev)
217 return NULL;
218 return to_pcmcia_dev(tmp_dev);
219}
220
Dominik Brodowski5716d412010-07-11 09:51:14 +0200221static void pcmcia_put_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 if (p_dev)
224 put_device(&p_dev->dev);
225}
226
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100227static void pcmcia_release_function(struct kref *ref)
228{
229 struct config_t *c = container_of(ref, struct config_t, ref);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200230 pr_debug("releasing config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100231 kfree(c);
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void pcmcia_release_dev(struct device *dev)
235{
236 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowski44961a02010-01-24 12:11:02 +0100237 int i;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200238 dev_dbg(dev, "releasing device\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -0700239 pcmcia_put_socket(p_dev->socket);
Dominik Brodowski44961a02010-01-24 12:11:02 +0100240 for (i = 0; i < 4; i++)
241 kfree(p_dev->prod_id[i]);
Brice Goglinbd65a682005-09-09 13:03:29 -0700242 kfree(p_dev->devname);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100243 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 kfree(p_dev);
245}
246
247
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100248static int pcmcia_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct pcmcia_device *p_dev;
251 struct pcmcia_driver *p_drv;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100252 struct pcmcia_socket *s;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400253 cistpl_config_t cis_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int ret = 0;
255
256 dev = get_device(dev);
257 if (!dev)
258 return -ENODEV;
259
260 p_dev = to_pcmcia_dev(dev);
261 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100262 s = p_dev->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200264 dev_dbg(dev, "trying to bind to %s\n", p_drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200265
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100266 if ((!p_drv->probe) || (!p_dev->function_config) ||
267 (!try_module_get(p_drv->owner))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ret = -EINVAL;
269 goto put_dev;
270 }
271
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400272 /* set up some more device information */
273 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
274 &cis_config);
275 if (!ret) {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200276 p_dev->config_base = cis_config.base;
277 p_dev->config_regs = cis_config.rmask[0];
Dominik Brodowski1cc745d2010-08-01 11:21:14 +0200278 dev_dbg(dev, "base %x, regs %x", p_dev->config_base,
279 p_dev->config_regs);
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400280 } else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200281 dev_printk(KERN_INFO, dev,
282 "pcmcia: could not parse base and rmask0 of CIS\n");
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200283 p_dev->config_base = 0;
284 p_dev->config_regs = 0;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400285 }
286
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100287 ret = p_drv->probe(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200288 if (ret) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200289 dev_dbg(dev, "binding to %s failed with %d\n",
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200290 p_drv->name, ret);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100291 goto put_module;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200292 }
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200293 dev_dbg(dev, "%s bound: Vpp %d.%d, idx %x, IRQ %d", p_drv->name,
Dominik Brodowski1cc745d2010-08-01 11:21:14 +0200294 p_dev->vpp/10, p_dev->vpp%10, p_dev->config_index, p_dev->irq);
295 dev_dbg(dev, "resources: ioport %pR %pR iomem %pR %pR %pR",
296 p_dev->resource[0], p_dev->resource[1], p_dev->resource[2],
297 p_dev->resource[3], p_dev->resource[4]);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100298
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100299 mutex_lock(&s->ops_mutex);
Dominik Brodowskice3f9d72010-07-21 14:43:05 +0200300 if ((s->pcmcia_pfc) &&
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100301 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100302 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100303 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100304
Alan Coxcec5eb72008-09-22 15:58:14 +0100305put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (ret)
307 module_put(p_drv->owner);
Alan Coxcec5eb72008-09-22 15:58:14 +0100308put_dev:
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100309 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 put_device(dev);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100315/*
316 * Removes a PCMCIA card from the device tree and socket list.
317 */
318static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
319{
320 struct pcmcia_device *p_dev;
321 struct pcmcia_device *tmp;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100322
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200323 dev_dbg(leftover ? &leftover->dev : &s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200324 "pcmcia_card_remove(%d) %s\n", s->sock,
325 leftover ? leftover->devname : "");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100326
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100327 mutex_lock(&s->ops_mutex);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100328 if (!leftover)
329 s->device_count = 0;
330 else
331 s->device_count = 1;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100332 mutex_unlock(&s->ops_mutex);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100333
334 /* unregister all pcmcia_devices registered with this socket, except leftover */
335 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
336 if (p_dev == leftover)
337 continue;
338
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100339 mutex_lock(&s->ops_mutex);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100340 list_del(&p_dev->socket_device_list);
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100341 mutex_unlock(&s->ops_mutex);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100342
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200343 dev_dbg(&p_dev->dev, "unregistering device\n");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100344 device_unregister(&p_dev->dev);
345 }
346
347 return;
348}
349
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100350static int pcmcia_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct pcmcia_device *p_dev;
353 struct pcmcia_driver *p_drv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100354 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 p_dev = to_pcmcia_dev(dev);
357 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100358
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200359 dev_dbg(dev, "removing device\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200360
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100361 /* If we're removing the primary module driving a
362 * pseudo multi-function card, we need to unbind
363 * all devices
364 */
Dominik Brodowskice3f9d72010-07-21 14:43:05 +0200365 if ((p_dev->socket->pcmcia_pfc) &&
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100366 (p_dev->socket->device_count > 0) &&
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100367 (p_dev->device_no == 0))
368 pcmcia_card_remove(p_dev->socket, p_dev);
369
370 /* detach the "instance" */
Dominik Brodowskif3990712005-11-14 21:25:23 +0100371 if (!p_drv)
372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dominik Brodowskif3990712005-11-14 21:25:23 +0100374 if (p_drv->remove)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100375 p_drv->remove(p_dev);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100376
Dominik Brodowskif3990712005-11-14 21:25:23 +0100377 /* check for proper unloading */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100378 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200379 dev_printk(KERN_INFO, dev,
380 "pcmcia: driver %s did not release config properly\n",
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200381 p_drv->name);
Dominik Brodowskif3990712005-11-14 21:25:23 +0100382
383 for (i = 0; i < MAX_WIN; i++)
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100384 if (p_dev->_win & CLIENT_WIN_REQ(i))
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200385 dev_printk(KERN_INFO, dev,
386 "pcmcia: driver %s did not release window properly\n",
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200387 p_drv->name);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100388
Dominik Brodowskif3990712005-11-14 21:25:23 +0100389 /* references from pcmcia_probe_device */
390 pcmcia_put_dev(p_dev);
391 module_put(p_drv->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return 0;
394}
395
396
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * pcmcia_device_query -- determine information about a pcmcia device
399 */
400static int pcmcia_device_query(struct pcmcia_device *p_dev)
401{
402 cistpl_manfid_t manf_id;
403 cistpl_funcid_t func_id;
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700404 cistpl_vers_1_t *vers1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned int i;
406
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700407 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
408 if (!vers1)
409 return -ENOMEM;
410
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200411 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 CISTPL_MANFID, &manf_id)) {
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100413 mutex_lock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 p_dev->manf_id = manf_id.manf;
415 p_dev->card_id = manf_id.card;
416 p_dev->has_manf_id = 1;
417 p_dev->has_card_id = 1;
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100418 mutex_unlock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
422 CISTPL_FUNCID, &func_id)) {
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100423 mutex_lock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 p_dev->func_id = func_id.func;
425 p_dev->has_func_id = 1;
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100426 mutex_unlock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
428 /* rule of thumb: cards with no FUNCID, but with
429 * common memory device geometry information, are
430 * probably memory cards (from pcmcia-cs) */
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700431 cistpl_device_geo_t *devgeo;
432
433 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
434 if (!devgeo) {
435 kfree(vers1);
436 return -ENOMEM;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700439 CISTPL_DEVICE_GEO, devgeo)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200440 dev_dbg(&p_dev->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200441 "mem device geometry probably means "
442 "FUNCID_MEMORY\n");
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100443 mutex_lock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 p_dev->func_id = CISTPL_FUNCID_MEMORY;
445 p_dev->has_func_id = 1;
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100446 mutex_unlock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700448 kfree(devgeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200451 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700452 vers1)) {
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100453 mutex_lock(&p_dev->socket->ops_mutex);
Dominik Brodowskic5e09522009-10-19 00:04:25 +0200454 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 char *tmp;
456 unsigned int length;
Dominik Brodowski44961a02010-01-24 12:11:02 +0100457 char *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700459 tmp = vers1->str + vers1->ofs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 length = strlen(tmp) + 1;
Janos Farkas6e3d4f22006-03-31 01:04:57 +0200462 if ((length < 2) || (length > 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 continue;
464
Dominik Brodowski44961a02010-01-24 12:11:02 +0100465 new = kmalloc(sizeof(char) * length, GFP_KERNEL);
466 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 continue;
468
Dominik Brodowski44961a02010-01-24 12:11:02 +0100469 new = strncpy(new, tmp, length);
470
471 tmp = p_dev->prod_id[i];
472 p_dev->prod_id[i] = new;
473 kfree(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100475 mutex_unlock(&p_dev->socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700478 kfree(vers1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
482
Dominik Brodowski5716d412010-07-11 09:51:14 +0200483static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,
484 unsigned int function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100486 struct pcmcia_device *p_dev, *tmp_dev;
Dominik Brodowski44961a02010-01-24 12:11:02 +0100487 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dominik Brodowskidc109492005-06-27 16:28:50 -0700489 s = pcmcia_get_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!s)
491 return NULL;
492
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200493 pr_debug("adding device to %d, function %d\n", s->sock, function);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200494
Dominik Brodowski8084b372005-12-11 21:18:26 +0100495 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!p_dev)
497 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100499 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 p_dev->device_no = (s->device_count++);
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100501 mutex_unlock(&s->ops_mutex);
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100502
Dominik Brodowski7d7ba8d2010-03-24 10:49:14 +0100503 /* max of 2 PFC devices */
504 if ((p_dev->device_no >= 2) && (function == 0))
505 goto err_free;
506
507 /* max of 4 devices overall */
508 if (p_dev->device_no >= 4)
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100509 goto err_free;
510
511 p_dev->socket = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 p_dev->func = function;
513
514 p_dev->dev.bus = &pcmcia_bus_type;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200515 p_dev->dev.parent = s->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 p_dev->dev.release = pcmcia_release_dev;
James Bottomley43d9f7f2007-10-16 01:23:58 -0700517 /* by default don't allow DMA */
518 p_dev->dma_mask = DMA_MASK_NONE;
519 p_dev->dev.dma_mask = &p_dev->dma_mask;
Kay Sievers25096982008-11-01 11:46:06 +0100520 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
521 if (!dev_name(&p_dev->dev))
522 goto err_free;
523 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
Brice Goglinbd65a682005-09-09 13:03:29 -0700524 if (!p_dev->devname)
525 goto err_free;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200526 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100528 mutex_lock(&s->ops_mutex);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100529
530 /*
531 * p_dev->function_config must be the same for all card functions.
Dominik Brodowskia60f22c2010-03-07 09:22:51 +0100532 * Note that this is serialized by ops_mutex, so that only one
533 * such struct will be created.
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100534 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100535 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
536 if (p_dev->func == tmp_dev->func) {
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100537 p_dev->function_config = tmp_dev->function_config;
Komuro3e879f62008-11-02 19:33:24 +0900538 p_dev->irq = tmp_dev->irq;
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100539 kref_get(&p_dev->function_config->ref);
540 }
541
542 /* Add to the list in pcmcia_bus_socket */
Komuro6171b882006-04-02 17:39:27 +0900543 list_add(&p_dev->socket_device_list, &s->devices_list);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100544
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200545 if (pcmcia_setup_irq(p_dev))
546 dev_warn(&p_dev->dev,
547 "IRQ setup failed -- device might not work\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100549 if (!p_dev->function_config) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200550 config_t *c;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200551 dev_dbg(&p_dev->dev, "creating config_t\n");
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200552 c = kzalloc(sizeof(struct config_t), GFP_KERNEL);
553 if (!c) {
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200554 mutex_unlock(&s->ops_mutex);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100555 goto err_unreg;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200556 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200557 p_dev->function_config = c;
558 kref_init(&c->ref);
559 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200560 c->io[i].name = p_dev->devname;
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200561 c->io[i].flags = IORESOURCE_IO;
562 }
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200563 for (i = 0; i< MAX_WIN; i++) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200564 c->mem[i].name = p_dev->devname;
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200565 c->mem[i].flags = IORESOURCE_MEM;
566 }
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100567 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200568 for (i = 0; i < MAX_IO_WIN; i++)
569 p_dev->resource[i] = &p_dev->function_config->io[i];
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200570 for (; i < (MAX_IO_WIN + MAX_WIN); i++)
571 p_dev->resource[i] = &p_dev->function_config->mem[i-MAX_IO_WIN];
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200572
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200573 mutex_unlock(&s->ops_mutex);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100574
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200575 dev_printk(KERN_NOTICE, &p_dev->dev,
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200576 "pcmcia: registering new device %s (IRQ: %d)\n",
Dominik Brodowskieb141202010-03-07 12:21:16 +0100577 p_dev->devname, p_dev->irq);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100578
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700579 pcmcia_device_query(p_dev);
580
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100581 if (device_register(&p_dev->dev))
582 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return p_dev;
585
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100586 err_unreg:
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100587 mutex_lock(&s->ops_mutex);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100588 list_del(&p_dev->socket_device_list);
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100589 mutex_unlock(&s->ops_mutex);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 err_free:
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100592 mutex_lock(&s->ops_mutex);
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100593 s->device_count--;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100594 mutex_unlock(&s->ops_mutex);
Dominik Brodowskie6e4f392010-01-16 01:34:06 +0100595
Dominik Brodowski44961a02010-01-24 12:11:02 +0100596 for (i = 0; i < 4; i++)
597 kfree(p_dev->prod_id[i]);
Brice Goglinbd65a682005-09-09 13:03:29 -0700598 kfree(p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err_put:
Dominik Brodowskidc109492005-06-27 16:28:50 -0700601 pcmcia_put_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 return NULL;
604}
605
606
607static int pcmcia_card_add(struct pcmcia_socket *s)
608{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 cistpl_longlink_mfc_t mfc;
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200610 unsigned int no_funcs, i, no_chains;
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100611 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100613 mutex_lock(&s->ops_mutex);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200614 if (!(s->resource_setup_done)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200615 dev_dbg(&s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200616 "no resources available, delaying card_add\n");
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100617 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200621 if (pcmcia_validate_mem(s)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200622 dev_dbg(&s->dev, "validating mem resources failed, "
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200623 "delaying card_add\n");
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100624 mutex_unlock(&s->ops_mutex);
Dominik Brodowskide759142005-09-28 19:41:56 +0200625 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200626 }
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100627 mutex_unlock(&s->ops_mutex);
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
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100648static int pcmcia_requery_callback(struct device *dev, void * _data)
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700649{
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700650 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200651 if (!p_dev->dev.driver) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200652 dev_dbg(dev, "update device information\n");
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700653 pcmcia_device_query(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200654 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700655
656 return 0;
657}
658
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100659
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100660static void pcmcia_requery(struct pcmcia_socket *s)
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700661{
Dominik Brodowski04de0812010-04-20 14:49:01 +0200662 int has_pfc;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700663
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100664 if (s->functions == 0) {
665 pcmcia_card_add(s);
666 return;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700667 }
668
669 /* some device information might have changed because of a CIS
670 * update or because we can finally read it correctly... so
671 * determine it again, overwriting old values if necessary. */
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100672 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery_callback);
673
674 /* if the CIS changed, we need to check whether the number of
675 * functions changed. */
676 if (s->fake_cis) {
677 int old_funcs, new_funcs;
678 cistpl_longlink_mfc_t mfc;
679
680 /* does this cis override add or remove functions? */
681 old_funcs = s->functions;
682
683 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
684 &mfc))
685 new_funcs = mfc.nfn;
686 else
687 new_funcs = 1;
Dominik Brodowskib1095af2010-04-08 20:10:21 +0200688 if (old_funcs != new_funcs) {
689 /* we need to re-start */
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100690 pcmcia_card_remove(s, NULL);
Dominik Brodowskib83156b2010-06-07 18:31:17 +0200691 s->functions = 0;
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100692 pcmcia_card_add(s);
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100693 }
694 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700695
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100696 /* If the PCMCIA device consists of two pseudo devices,
697 * call pcmcia_device_add() -- which will fail if both
698 * devices are already registered. */
699 mutex_lock(&s->ops_mutex);
Dominik Brodowskice3f9d72010-07-21 14:43:05 +0200700 has_pfc = s->pcmcia_pfc;
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100701 mutex_unlock(&s->ops_mutex);
702 if (has_pfc)
703 pcmcia_device_add(s, 0);
704
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700705 /* we re-scan all devices, not just the ones connected to this
706 * socket. This does not matter, though. */
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100707 if (bus_rescan_devices(&pcmcia_bus_type))
708 dev_warn(&s->dev, "rescanning the bus failed\n");
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700709}
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700710
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100711
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500712#ifdef CONFIG_PCMCIA_LOAD_CIS
713
714/**
715 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
Randy Dunlap78187862007-12-10 15:49:22 -0800716 * @dev: the pcmcia device which needs a CIS override
717 * @filename: requested filename in /lib/firmware/
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500718 *
719 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
720 * the one provided by the card is broken. The firmware files reside in
721 * /lib/firmware/ in userspace.
722 */
723static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
724{
725 struct pcmcia_socket *s = dev->socket;
726 const struct firmware *fw;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500727 int ret = -ENOMEM;
Dominik Brodowskib1095af2010-04-08 20:10:21 +0200728 cistpl_longlink_mfc_t mfc;
729 int old_funcs, new_funcs = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500730
731 if (!filename)
732 return -EINVAL;
733
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200734 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500735
Samuel Ortized62ace2009-05-27 00:49:35 +0200736 if (request_firmware(&fw, filename, &dev->dev) == 0) {
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200737 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
738 ret = -EINVAL;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200739 dev_printk(KERN_ERR, &dev->dev,
740 "pcmcia: CIS override is too big\n");
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500741 goto release;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200742 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500743
Dominik Brodowski53efec92008-07-28 19:44:05 +0200744 if (!pcmcia_replace_cis(s, fw->data, fw->size))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500745 ret = 0;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200746 else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200747 dev_printk(KERN_ERR, &dev->dev,
748 "pcmcia: CIS override failed\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200749 goto release;
750 }
751
Dominik Brodowskib1095af2010-04-08 20:10:21 +0200752 /* we need to re-start if the number of functions changed */
753 old_funcs = s->functions;
754 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
755 &mfc))
756 new_funcs = mfc.nfn;
757
758 if (old_funcs != new_funcs)
759 ret = -EBUSY;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500760
761 /* update information */
762 pcmcia_device_query(dev);
763
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100764 /* requery (as number of functions might have changed) */
765 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500766 }
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,
Joe Perchese9fb13b2011-05-03 19:29:00 -0700784 const struct pcmcia_device_id *did)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700785{
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) {
Alexander Kurz83bf6f12010-04-25 13:44:35 +0400830 dev_dbg(&dev->dev, "this is a pseudo-multi-function device\n");
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100831 mutex_lock(&dev->socket->ops_mutex);
Dominik Brodowskice3f9d72010-07-21 14:43:05 +0200832 dev->socket->pcmcia_pfc = 1;
Dominik Brodowskiaa584ca2010-01-24 14:36:59 +0100833 mutex_unlock(&dev->socket->ops_mutex);
Alexander Kurz83bf6f12010-04-25 13:44:35 +0400834 if (dev->device_no != did->device_no)
835 return 0;
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700836 }
837
838 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100839 int ret;
840
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700841 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
842 return 0;
843
844 /* if this is a pseudo-multi-function device,
845 * we need explicit matches */
Dominik Brodowskice3f9d72010-07-21 14:43:05 +0200846 if (dev->socket->pcmcia_pfc)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700847 return 0;
848 if (dev->device_no)
849 return 0;
850
851 /* also, FUNC_ID matching needs to be activated by userspace
852 * after it has re-checked that there is no possible module
853 * with a prod_id/manf_id/card_id match.
854 */
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100855 mutex_lock(&dev->socket->ops_mutex);
856 ret = dev->allow_func_id_match;
857 mutex_unlock(&dev->socket->ops_mutex);
858
859 if (!ret) {
860 dev_dbg(&dev->dev,
861 "skipping FUNC_ID match until userspace ACK\n");
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700862 return 0;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100863 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700864 }
865
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700866 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200867 dev_dbg(&dev->dev, "device needs a fake CIS\n");
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700868 if (!dev->socket->fake_cis)
Dominik Brodowskib1095af2010-04-08 20:10:21 +0200869 if (pcmcia_load_firmware(dev, did->cisfile))
870 return 0;
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700871 }
872
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700873 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
874 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100875 for (i = 0; i < 4; i++)
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700876 if (dev->prod_id[i])
877 return 0;
878 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
879 return 0;
880 }
881
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700882 return 1;
883}
884
885
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100886static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
887{
888 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
889 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
Joe Perchese9fb13b2011-05-03 19:29:00 -0700890 const struct pcmcia_device_id *did = p_drv->id_table;
Bernhard Walle6179b552007-05-06 14:48:44 -0700891 struct pcmcia_dynid *dynid;
892
893 /* match dynamic devices first */
Dominik Brodowski3f565232010-01-16 13:06:40 +0100894 mutex_lock(&p_drv->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700895 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200896 dev_dbg(dev, "trying to match to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -0700897 if (pcmcia_devmatch(p_dev, &dynid->id)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200898 dev_dbg(dev, "matched to %s\n", drv->name);
Dominik Brodowski3f565232010-01-16 13:06:40 +0100899 mutex_unlock(&p_drv->dynids.lock);
Bernhard Walle6179b552007-05-06 14:48:44 -0700900 return 1;
901 }
902 }
Dominik Brodowski3f565232010-01-16 13:06:40 +0100903 mutex_unlock(&p_drv->dynids.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700905 while (did && did->match_flags) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200906 dev_dbg(dev, "trying to match to %s\n", drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200907 if (pcmcia_devmatch(p_dev, did)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200908 dev_dbg(dev, "matched to %s\n", drv->name);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700909 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200910 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700911 did++;
912 }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915}
916
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700917#ifdef CONFIG_HOTPLUG
918
Kay Sievers7eff2e72007-08-14 15:15:12 +0200919static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700920{
921 struct pcmcia_device *p_dev;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200922 int i;
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700923 u32 hash[4] = { 0, 0, 0, 0};
924
925 if (!dev)
926 return -ENODEV;
927
928 p_dev = to_pcmcia_dev(dev);
929
930 /* calculate hashes */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100931 for (i = 0; i < 4; i++) {
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700932 if (!p_dev->prod_id[i])
933 continue;
934 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
935 }
936
Kay Sievers7eff2e72007-08-14 15:15:12 +0200937 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700938 return -ENOMEM;
939
Kay Sievers7eff2e72007-08-14 15:15:12 +0200940 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700941 return -ENOMEM;
942
Kay Sievers7eff2e72007-08-14 15:15:12 +0200943 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
Kay Sievers312c0042005-11-16 09:00:00 +0100944 "pa%08Xpb%08Xpc%08Xpd%08X",
945 p_dev->has_manf_id ? p_dev->manf_id : 0,
946 p_dev->has_card_id ? p_dev->card_id : 0,
947 p_dev->has_func_id ? p_dev->func_id : 0,
948 p_dev->func,
949 p_dev->device_no,
950 hash[0],
951 hash[1],
952 hash[2],
953 hash[3]))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700954 return -ENOMEM;
955
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700956 return 0;
957}
958
959#else
960
Kay Sievers7eff2e72007-08-14 15:15:12 +0200961static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -0700962{
963 return -ENODEV;
964}
965
966#endif
967
Alan Stern3f8df782007-07-12 16:57:22 -0400968/************************ runtime PM support ***************************/
969
970static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
971static int pcmcia_dev_resume(struct device *dev);
972
973static int runtime_suspend(struct device *dev)
974{
975 int rc;
976
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800977 device_lock(dev);
Alan Stern3f8df782007-07-12 16:57:22 -0400978 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800979 device_unlock(dev);
Alan Stern3f8df782007-07-12 16:57:22 -0400980 return rc;
981}
982
Dominik Brodowski933a8382009-12-29 18:21:39 +0100983static int runtime_resume(struct device *dev)
Alan Stern3f8df782007-07-12 16:57:22 -0400984{
985 int rc;
986
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800987 device_lock(dev);
Alan Stern3f8df782007-07-12 16:57:22 -0400988 rc = pcmcia_dev_resume(dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800989 device_unlock(dev);
Dominik Brodowski933a8382009-12-29 18:21:39 +0100990 return rc;
Alan Stern3f8df782007-07-12 16:57:22 -0400991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/************************ per-device sysfs output ***************************/
994
995#define pcmcia_device_attr(field, test, format) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400996static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{ \
998 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100999 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002#define pcmcia_device_stringattr(name, field) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001003static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{ \
1005 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001006 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009pcmcia_device_attr(func, socket, "0x%02x\n");
1010pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1011pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1012pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1013pcmcia_device_stringattr(prod_id1, prod_id[0]);
1014pcmcia_device_stringattr(prod_id2, prod_id[1]);
1015pcmcia_device_stringattr(prod_id3, prod_id[2]);
1016pcmcia_device_stringattr(prod_id4, prod_id[3]);
1017
Dominik Brodowski8f677ea2010-07-25 11:10:29 +02001018static ssize_t pcmcia_show_resources(struct device *dev,
1019 struct device_attribute *attr, char *buf)
1020{
1021 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1022 char *str = buf;
1023 int i;
1024
1025 for (i = 0; i < PCMCIA_NUM_RESOURCES; i++)
1026 str += sprintf(str, "%pr\n", p_dev->resource[i]);
1027
1028 return str - buf;
1029}
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001030
1031static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1032{
1033 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1034
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001035 if (p_dev->suspended)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001036 return sprintf(buf, "off\n");
1037 else
1038 return sprintf(buf, "on\n");
1039}
1040
1041static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1042 const char *buf, size_t count)
1043{
1044 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1045 int ret = 0;
1046
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001047 if (!count)
1048 return -EINVAL;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001049
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001050 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
Alan Stern3f8df782007-07-12 16:57:22 -04001051 ret = runtime_suspend(dev);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001052 else if (p_dev->suspended && !strncmp(buf, "on", 2))
Dominik Brodowski933a8382009-12-29 18:21:39 +01001053 ret = runtime_resume(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001054
1055 return ret ? ret : count;
1056}
1057
1058
Dominik Brodowski37045112005-06-30 02:58:47 -07001059static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001060{
1061 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1062 int i;
1063 u32 hash[4] = { 0, 0, 0, 0};
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001064
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001065 /* calculate hashes */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001066 for (i = 0; i < 4; i++) {
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001067 if (!p_dev->prod_id[i])
1068 continue;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001069 hash[i] = crc32(0, p_dev->prod_id[i],
1070 strlen(p_dev->prod_id[i]));
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001071 }
1072 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1073 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1074 p_dev->has_manf_id ? p_dev->manf_id : 0,
1075 p_dev->has_card_id ? p_dev->card_id : 0,
1076 p_dev->has_func_id ? p_dev->func_id : 0,
1077 p_dev->func, p_dev->device_no,
1078 hash[0], hash[1], hash[2], hash[3]);
1079}
1080
1081static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1082 struct device_attribute *attr, const char *buf, size_t count)
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001083{
1084 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001085
1086 if (!count)
1087 return -EINVAL;
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001088
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001089 mutex_lock(&p_dev->socket->ops_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001090 p_dev->allow_func_id_match = 1;
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001091 mutex_unlock(&p_dev->socket->ops_mutex);
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +01001092 pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001093
1094 return count;
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097static struct device_attribute pcmcia_dev_attrs[] = {
1098 __ATTR(function, 0444, func_show, NULL),
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001099 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
Dominik Brodowski8f677ea2010-07-25 11:10:29 +02001100 __ATTR(resources, 0444, pcmcia_show_resources, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 __ATTR_RO(func_id),
1102 __ATTR_RO(manf_id),
1103 __ATTR_RO(card_id),
1104 __ATTR_RO(prod_id1),
1105 __ATTR_RO(prod_id2),
1106 __ATTR_RO(prod_id3),
1107 __ATTR_RO(prod_id4),
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001108 __ATTR_RO(modalias),
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001109 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 __ATTR_NULL,
1111};
1112
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001113/* PM support, also needed for reset */
1114
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001115static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001116{
1117 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1118 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001119 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001120
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001121 mutex_lock(&p_dev->socket->ops_mutex);
1122 if (p_dev->suspended) {
1123 mutex_unlock(&p_dev->socket->ops_mutex);
David Brownelld6b4fa62008-02-04 22:27:37 -08001124 return 0;
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001125 }
1126 p_dev->suspended = 1;
1127 mutex_unlock(&p_dev->socket->ops_mutex);
David Brownelld6b4fa62008-02-04 22:27:37 -08001128
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001129 dev_dbg(dev, "suspending\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001130
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001131 if (dev->driver)
1132 p_drv = to_pcmcia_drv(dev->driver);
1133
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001134 if (!p_drv)
1135 goto out;
1136
1137 if (p_drv->suspend) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001138 ret = p_drv->suspend(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001139 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001140 dev_printk(KERN_ERR, dev,
1141 "pcmcia: device %s (driver %s) did "
1142 "not want to go to sleep (%d)\n",
Dominik Brodowski2e9b9812010-08-08 11:36:26 +02001143 p_dev->devname, p_drv->name, ret);
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001144 mutex_lock(&p_dev->socket->ops_mutex);
1145 p_dev->suspended = 0;
1146 mutex_unlock(&p_dev->socket->ops_mutex);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001147 goto out;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001148 }
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001149 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001150
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001151 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001152 dev_dbg(dev, "releasing configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001153 pcmcia_release_configuration(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001154 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001155
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001156 out:
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001157 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001158}
1159
1160
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001161static int pcmcia_dev_resume(struct device *dev)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001162{
1163 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001164 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001165 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001166
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001167 mutex_lock(&p_dev->socket->ops_mutex);
1168 if (!p_dev->suspended) {
1169 mutex_unlock(&p_dev->socket->ops_mutex);
David Brownelld6b4fa62008-02-04 22:27:37 -08001170 return 0;
Dominik Brodowski94a819f2010-01-17 18:31:34 +01001171 }
1172 p_dev->suspended = 0;
1173 mutex_unlock(&p_dev->socket->ops_mutex);
David Brownelld6b4fa62008-02-04 22:27:37 -08001174
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001175 dev_dbg(dev, "resuming\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001176
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001177 if (dev->driver)
1178 p_drv = to_pcmcia_drv(dev->driver);
1179
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001180 if (!p_drv)
1181 goto out;
1182
1183 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001184 dev_dbg(dev, "requesting configuration\n");
Dominik Brodowski1ac71e52010-07-29 19:27:09 +02001185 ret = pcmcia_enable_device(p_dev);
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001186 if (ret)
1187 goto out;
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001188 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001189
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001190 if (p_drv->resume)
1191 ret = p_drv->resume(p_dev);
1192
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001193 out:
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001194 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001195}
1196
1197
1198static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1199{
1200 struct pcmcia_socket *skt = _data;
1201 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1202
Alan Stern3f8df782007-07-12 16:57:22 -04001203 if (p_dev->socket != skt || p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001204 return 0;
1205
Alan Stern3f8df782007-07-12 16:57:22 -04001206 return runtime_suspend(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001207}
1208
1209static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1210{
1211 struct pcmcia_socket *skt = _data;
1212 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1213
Alan Stern3f8df782007-07-12 16:57:22 -04001214 if (p_dev->socket != skt || !p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001215 return 0;
1216
Alan Stern3f8df782007-07-12 16:57:22 -04001217 runtime_resume(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001218
1219 return 0;
1220}
1221
1222static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1223{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001224 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001225 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1226 return 0;
1227}
1228
1229static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1230{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001231 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001232 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1233 pcmcia_bus_suspend_callback)) {
1234 pcmcia_bus_resume(skt);
1235 return -EIO;
1236 }
1237 return 0;
1238}
1239
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001240static int pcmcia_bus_remove(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001242 atomic_set(&skt->present, 0);
1243 pcmcia_card_remove(skt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001245 mutex_lock(&skt->ops_mutex);
1246 destroy_cis_cache(skt);
1247 pcmcia_cleanup_irq(skt);
1248 mutex_unlock(&skt->ops_mutex);
1249
1250 return 0;
1251}
1252
1253static int pcmcia_bus_add(struct pcmcia_socket *skt)
1254{
1255 atomic_set(&skt->present, 1);
1256
1257 mutex_lock(&skt->ops_mutex);
Dominik Brodowskice3f9d72010-07-21 14:43:05 +02001258 skt->pcmcia_pfc = 0;
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001259 destroy_cis_cache(skt); /* to be on the safe side... */
1260 mutex_unlock(&skt->ops_mutex);
1261
1262 pcmcia_card_add(skt);
1263
1264 return 0;
1265}
1266
1267static int pcmcia_bus_early_resume(struct pcmcia_socket *skt)
1268{
1269 if (!verify_cis_cache(skt)) {
1270 pcmcia_put_socket(skt);
1271 return 0;
Florin Malita16174062006-05-24 21:21:31 -04001272 }
1273
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001274 dev_dbg(&skt->dev, "cis mismatch - different card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001276 /* first, remove the card */
1277 pcmcia_bus_remove(skt);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001278
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001279 mutex_lock(&skt->ops_mutex);
1280 destroy_cis_cache(skt);
1281 kfree(skt->fake_cis);
1282 skt->fake_cis = NULL;
1283 skt->functions = 0;
1284 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001286 /* now, add the new card */
1287 pcmcia_bus_add(skt);
1288 return 0;
1289}
Dominik Brodowski88b060d2010-01-02 14:14:23 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Dominik Brodowski04de0812010-04-20 14:49:01 +02001292/*
1293 * NOTE: This is racy. There's no guarantee the card will still be
1294 * physically present, even if the call to this function returns
1295 * non-NULL. Furthermore, the device driver most likely is unbound
1296 * almost immediately, so the timeframe where pcmcia_dev_present
1297 * returns NULL is probably really really small.
1298 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001299struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001300{
1301 struct pcmcia_device *p_dev;
1302 struct pcmcia_device *ret = NULL;
1303
1304 p_dev = pcmcia_get_dev(_p_dev);
1305 if (!p_dev)
1306 return NULL;
1307
Dominik Brodowski04de0812010-04-20 14:49:01 +02001308 if (atomic_read(&p_dev->socket->present) != 0)
1309 ret = p_dev;
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001310
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001311 pcmcia_put_dev(p_dev);
1312 return ret;
1313}
1314EXPORT_SYMBOL(pcmcia_dev_present);
1315
1316
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001317static struct pcmcia_callback pcmcia_bus_callback = {
1318 .owner = THIS_MODULE,
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001319 .add = pcmcia_bus_add,
1320 .remove = pcmcia_bus_remove,
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +01001321 .requery = pcmcia_requery,
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001322 .validate = pccard_validate_cis,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001323 .suspend = pcmcia_bus_suspend,
Dominik Brodowski7b24e792010-07-11 10:26:53 +02001324 .early_resume = pcmcia_bus_early_resume,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001325 .resume = pcmcia_bus_resume,
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001326};
1327
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001328static int __devinit pcmcia_bus_add_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001329 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001331 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int ret;
1333
Dominik Brodowskidc109492005-06-27 16:28:50 -07001334 socket = pcmcia_get_socket(socket);
1335 if (!socket) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001336 dev_printk(KERN_ERR, dev,
1337 "PCMCIA obtaining reference to socket failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -ENODEV;
1339 }
1340
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001341 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
1342 if (ret) {
1343 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1344 pcmcia_put_socket(socket);
1345 return ret;
1346 }
1347
Dominik Brodowskidc109492005-06-27 16:28:50 -07001348 INIT_LIST_HEAD(&socket->devices_list);
Dominik Brodowskice3f9d72010-07-21 14:43:05 +02001349 socket->pcmcia_pfc = 0;
Dominik Brodowskidc109492005-06-27 16:28:50 -07001350 socket->device_count = 0;
Dominik Brodowskie4f1ac22010-06-19 14:33:56 +02001351 atomic_set(&socket->present, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001353 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001355 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -07001356 pcmcia_put_socket(socket);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
1360 return 0;
1361}
1362
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001363static void pcmcia_bus_remove_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001364 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001366 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Dominik Brodowskidc109492005-06-27 16:28:50 -07001368 if (!socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
1370
1371 pccard_register_pcmcia(socket, NULL);
1372
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001373 /* unregister any unbound devices */
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001374 mutex_lock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001375 pcmcia_card_remove(socket, NULL);
Dominik Brodowski180c33e2010-01-02 17:34:09 +01001376 release_cis_mem(socket);
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001377 mutex_unlock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001378
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001379 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1380
Dominik Brodowskidc109492005-06-27 16:28:50 -07001381 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 return;
1384}
1385
1386
1387/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
Sam Ravnborged49f5d2008-05-01 04:34:50 -07001388static struct class_interface pcmcia_bus_interface __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 .class = &pcmcia_socket_class,
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001390 .add_dev = &pcmcia_bus_add_socket,
1391 .remove_dev = &pcmcia_bus_remove_socket,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393
1394
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001395struct bus_type pcmcia_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 .name = "pcmcia",
Kay Sievers312c0042005-11-16 09:00:00 +01001397 .uevent = pcmcia_bus_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 .match = pcmcia_bus_match,
1399 .dev_attrs = pcmcia_dev_attrs,
Russell King1d0baa32006-01-05 14:40:58 +00001400 .probe = pcmcia_device_probe,
1401 .remove = pcmcia_device_remove,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001402 .suspend = pcmcia_dev_suspend,
1403 .resume = pcmcia_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406
1407static int __init init_pcmcia_bus(void)
1408{
Randy Dunlapace7d472006-10-20 14:44:12 -07001409 int ret;
1410
Randy Dunlapace7d472006-10-20 14:44:12 -07001411 ret = bus_register(&pcmcia_bus_type);
1412 if (ret < 0) {
1413 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1414 return ret;
1415 }
1416 ret = class_interface_register(&pcmcia_bus_interface);
1417 if (ret < 0) {
1418 printk(KERN_WARNING
1419 "pcmcia: class_interface_register error: %d\n", ret);
1420 bus_unregister(&pcmcia_bus_type);
1421 return ret;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425}
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001426fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * pcmcia_socket_class is already registered */
1428
1429
1430static void __exit exit_pcmcia_bus(void)
1431{
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001432 class_interface_unregister(&pcmcia_bus_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 bus_unregister(&pcmcia_bus_type);
1435}
1436module_exit(exit_pcmcia_bus);
1437
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439MODULE_ALIAS("ds");