blob: 5b069aeaf17a34449f8ad071c6ebeed0c4acc080 [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
49/* code which was in cs.c before */
50
51/* String tables for error messages */
52
53typedef struct lookup_t {
Dominik Brodowski79ba6752008-08-03 14:03:07 +020054 const int key;
55 const char *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056} lookup_t;
57
58static const lookup_t error_table[] = {
Dominik Brodowski610e2372008-08-03 11:58:53 +020059 { 0, "Operation succeeded" },
Dominik Brodowskid8b0a492008-08-03 10:56:13 +020060 { -EIO, "Input/Output error" },
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +020061 { -ENODEV, "No card present" },
Dominik Brodowskide6405e2008-08-03 10:47:59 +020062 { -EINVAL, "Bad parameter" },
Dominik Brodowski943f70f2008-08-03 11:23:42 +020063 { -EACCES, "Configuration locked" },
Dominik Brodowski610e2372008-08-03 11:58:53 +020064 { -EBUSY, "Resource in use" },
Dominik Brodowski635d19b2008-08-03 11:47:29 +020065 { -ENOSPC, "No more items" },
Dominik Brodowski610e2372008-08-03 11:58:53 +020066 { -ENOMEM, "Out of resource" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69
70static const lookup_t service_table[] = {
71 { AccessConfigurationRegister, "AccessConfigurationRegister" },
72 { AddSocketServices, "AddSocketServices" },
73 { AdjustResourceInfo, "AdjustResourceInfo" },
74 { CheckEraseQueue, "CheckEraseQueue" },
75 { CloseMemory, "CloseMemory" },
76 { DeregisterClient, "DeregisterClient" },
77 { DeregisterEraseQueue, "DeregisterEraseQueue" },
78 { GetCardServicesInfo, "GetCardServicesInfo" },
79 { GetClientInfo, "GetClientInfo" },
80 { GetConfigurationInfo, "GetConfigurationInfo" },
81 { GetEventMask, "GetEventMask" },
82 { GetFirstClient, "GetFirstClient" },
83 { GetFirstRegion, "GetFirstRegion" },
84 { GetFirstTuple, "GetFirstTuple" },
85 { GetNextClient, "GetNextClient" },
86 { GetNextRegion, "GetNextRegion" },
87 { GetNextTuple, "GetNextTuple" },
88 { GetStatus, "GetStatus" },
89 { GetTupleData, "GetTupleData" },
90 { MapMemPage, "MapMemPage" },
91 { ModifyConfiguration, "ModifyConfiguration" },
92 { ModifyWindow, "ModifyWindow" },
93 { OpenMemory, "OpenMemory" },
94 { ParseTuple, "ParseTuple" },
95 { ReadMemory, "ReadMemory" },
96 { RegisterClient, "RegisterClient" },
97 { RegisterEraseQueue, "RegisterEraseQueue" },
98 { RegisterMTD, "RegisterMTD" },
99 { ReleaseConfiguration, "ReleaseConfiguration" },
100 { ReleaseIO, "ReleaseIO" },
101 { ReleaseIRQ, "ReleaseIRQ" },
102 { ReleaseWindow, "ReleaseWindow" },
103 { RequestConfiguration, "RequestConfiguration" },
104 { RequestIO, "RequestIO" },
105 { RequestIRQ, "RequestIRQ" },
106 { RequestSocketMask, "RequestSocketMask" },
107 { RequestWindow, "RequestWindow" },
108 { ResetCard, "ResetCard" },
109 { SetEventMask, "SetEventMask" },
110 { ValidateCIS, "ValidateCIS" },
111 { WriteMemory, "WriteMemory" },
112 { BindDevice, "BindDevice" },
113 { BindMTD, "BindMTD" },
114 { ReportError, "ReportError" },
115 { SuspendCard, "SuspendCard" },
116 { ResumeCard, "ResumeCard" },
117 { EjectCard, "EjectCard" },
118 { InsertCard, "InsertCard" },
119 { ReplaceCIS, "ReplaceCIS" }
120};
121
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200122const char *pcmcia_error_func(int func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 for (i = 0; i < ARRAY_SIZE(service_table); i++)
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200127 if (service_table[i].key == func)
128 return service_table[i].msg;
129
130 return "Unknown service number";
131}
132EXPORT_SYMBOL(pcmcia_error_func);
133
134const char *pcmcia_error_ret(int ret)
135{
136 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 for (i = 0; i < ARRAY_SIZE(error_table); i++)
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200139 if (error_table[i].key == ret)
140 return error_table[i].msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200142 return "unknown";
143}
144EXPORT_SYMBOL(pcmcia_error_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/*======================================================================*/
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700149
150static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
151{
152 struct pcmcia_device_id *did = p_drv->id_table;
153 unsigned int i;
154 u32 hash;
155
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100156 if (!p_drv->probe || !p_drv->remove)
Pavel Roskinba5bb6b2005-07-28 01:07:20 -0700157 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
158 "function\n", p_drv->drv.name);
Dominik Brodowski1e212f32005-07-07 17:59:00 -0700159
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700160 while (did && did->match_flags) {
161 for (i=0; i<4; i++) {
162 if (!did->prod_id[i])
163 continue;
164
165 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
166 if (hash == did->prod_id_hash[i])
167 continue;
168
169 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
170 "product string \"%s\": is 0x%x, should "
171 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
172 did->prod_id_hash[i], hash);
Dominik Brodowski5085cb22005-06-27 16:28:45 -0700173 printk(KERN_DEBUG "pcmcia: see "
174 "Documentation/pcmcia/devicetable.txt for "
175 "details\n");
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700176 }
177 did++;
178 }
179
180 return;
181}
182
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*======================================================================*/
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Bernhard Walle6179b552007-05-06 14:48:44 -0700187struct pcmcia_dynid {
188 struct list_head node;
189 struct pcmcia_device_id id;
190};
191
192/**
193 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
194 * @driver: target device driver
195 * @buf: buffer for scanning device ID data
196 * @count: input size
197 *
198 * Adds a new dynamic PCMCIA device ID to this driver,
199 * and causes the driver to probe for all devices again.
200 */
201static ssize_t
202pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
203{
204 struct pcmcia_dynid *dynid;
205 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
206 __u16 match_flags, manf_id, card_id;
207 __u8 func_id, function, device_no;
208 __u32 prod_id_hash[4] = {0, 0, 0, 0};
209 int fields=0;
210 int retval = 0;
211
212 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
213 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
214 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
215 if (fields < 6)
216 return -EINVAL;
217
218 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
219 if (!dynid)
220 return -ENOMEM;
221
Bernhard Walle6179b552007-05-06 14:48:44 -0700222 dynid->id.match_flags = match_flags;
223 dynid->id.manf_id = manf_id;
224 dynid->id.card_id = card_id;
225 dynid->id.func_id = func_id;
226 dynid->id.function = function;
227 dynid->id.device_no = device_no;
228 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
229
230 spin_lock(&pdrv->dynids.lock);
Wolfram Sangb4b3d7b2009-07-20 10:58:59 +0200231 list_add_tail(&dynid->node, &pdrv->dynids.list);
Bernhard Walle6179b552007-05-06 14:48:44 -0700232 spin_unlock(&pdrv->dynids.lock);
233
234 if (get_driver(&pdrv->drv)) {
235 retval = driver_attach(&pdrv->drv);
236 put_driver(&pdrv->drv);
237 }
238
239 if (retval)
240 return retval;
241 return count;
242}
243static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
244
245static void
246pcmcia_free_dynids(struct pcmcia_driver *drv)
247{
248 struct pcmcia_dynid *dynid, *n;
249
250 spin_lock(&drv->dynids.lock);
251 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
252 list_del(&dynid->node);
253 kfree(dynid);
254 }
255 spin_unlock(&drv->dynids.lock);
256}
257
258static int
259pcmcia_create_newid_file(struct pcmcia_driver *drv)
260{
261 int error = 0;
262 if (drv->probe != NULL)
Greg Kroah-Hartman2344c6d2007-11-28 12:23:18 -0800263 error = driver_create_file(&drv->drv, &driver_attr_new_id);
Bernhard Walle6179b552007-05-06 14:48:44 -0700264 return error;
265}
266
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/**
269 * pcmcia_register_driver - register a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800270 * @driver: the &driver being registered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
272 * Registers a PCMCIA driver with the PCMCIA bus core.
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274int pcmcia_register_driver(struct pcmcia_driver *driver)
275{
Bernhard Walle6179b552007-05-06 14:48:44 -0700276 int error;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!driver)
279 return -EINVAL;
280
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700281 pcmcia_check_driver(driver);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* initialize common fields */
284 driver->drv.bus = &pcmcia_bus_type;
285 driver->drv.owner = driver->owner;
Bernhard Walle6179b552007-05-06 14:48:44 -0700286 spin_lock_init(&driver->dynids.lock);
287 INIT_LIST_HEAD(&driver->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200289 pr_debug("registering driver %s\n", driver->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200290
Bernhard Walle6179b552007-05-06 14:48:44 -0700291 error = driver_register(&driver->drv);
292 if (error < 0)
293 return error;
294
295 error = pcmcia_create_newid_file(driver);
296 if (error)
297 driver_unregister(&driver->drv);
298
299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301EXPORT_SYMBOL(pcmcia_register_driver);
302
303/**
304 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800305 * @driver: the &driver being unregistered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307void pcmcia_unregister_driver(struct pcmcia_driver *driver)
308{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200309 pr_debug("unregistering driver %s\n", driver->drv.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 driver_unregister(&driver->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700311 pcmcia_free_dynids(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313EXPORT_SYMBOL(pcmcia_unregister_driver);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316/* pcmcia_device handling */
317
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700318struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct device *tmp_dev;
321 tmp_dev = get_device(&p_dev->dev);
322 if (!tmp_dev)
323 return NULL;
324 return to_pcmcia_dev(tmp_dev);
325}
326
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700327void pcmcia_put_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 if (p_dev)
330 put_device(&p_dev->dev);
331}
332
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100333static void pcmcia_release_function(struct kref *ref)
334{
335 struct config_t *c = container_of(ref, struct config_t, ref);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200336 pr_debug("releasing config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100337 kfree(c);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static void pcmcia_release_dev(struct device *dev)
341{
342 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200343 dev_dbg(dev, "releasing device\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -0700344 pcmcia_put_socket(p_dev->socket);
Brice Goglinbd65a682005-09-09 13:03:29 -0700345 kfree(p_dev->devname);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100346 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 kfree(p_dev);
348}
349
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500350static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100351{
352 if (!s->pcmcia_state.device_add_pending) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200353 dev_dbg(&s->dev, "scheduling to add %s secondary"
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200354 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100355 s->pcmcia_state.device_add_pending = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500356 s->pcmcia_state.mfc_pfc = mfc;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100357 schedule_work(&s->device_add);
358 }
359 return;
360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362static int pcmcia_device_probe(struct device * dev)
363{
364 struct pcmcia_device *p_dev;
365 struct pcmcia_driver *p_drv;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100366 struct pcmcia_device_id *did;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100367 struct pcmcia_socket *s;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400368 cistpl_config_t cis_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int ret = 0;
370
371 dev = get_device(dev);
372 if (!dev)
373 return -ENODEV;
374
375 p_dev = to_pcmcia_dev(dev);
376 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100377 s = p_dev->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700379 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
Alan Coxcec5eb72008-09-22 15:58:14 +0100380 * which is an ugly hack. Once the driver probe is called it may
381 * and often will overwrite the match data so we must save it first
382 *
383 * handle pseudo multifunction devices:
384 * there are at most two pseudo multifunction devices.
385 * if we're matching against the first, schedule a
386 * call which will then check whether there are two
387 * pseudo devices, and if not, add the second one.
388 */
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700389 did = dev_get_drvdata(&p_dev->dev);
Alan Coxcec5eb72008-09-22 15:58:14 +0100390
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200391 dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200392
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100393 if ((!p_drv->probe) || (!p_dev->function_config) ||
394 (!try_module_get(p_drv->owner))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ret = -EINVAL;
396 goto put_dev;
397 }
398
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400399 /* set up some more device information */
400 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
401 &cis_config);
402 if (!ret) {
403 p_dev->conf.ConfigBase = cis_config.base;
404 p_dev->conf.Present = cis_config.rmask[0];
405 } else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200406 dev_printk(KERN_INFO, dev,
407 "pcmcia: could not parse base and rmask0 of CIS\n");
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400408 p_dev->conf.ConfigBase = 0;
409 p_dev->conf.Present = 0;
410 }
411
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100412 ret = p_drv->probe(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200413 if (ret) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200414 dev_dbg(dev, "binding to %s failed with %d\n",
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200415 p_drv->drv.name, ret);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100416 goto put_module;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200417 }
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100418
Hugh Dickins4ceadbf2006-03-10 21:13:44 +0000419 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100420 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500421 pcmcia_add_device_later(p_dev->socket, 0);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100422
Alan Coxcec5eb72008-09-22 15:58:14 +0100423put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (ret)
425 module_put(p_drv->owner);
Alan Coxcec5eb72008-09-22 15:58:14 +0100426put_dev:
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100427 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 put_device(dev);
429 return (ret);
430}
431
432
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100433/*
434 * Removes a PCMCIA card from the device tree and socket list.
435 */
436static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
437{
438 struct pcmcia_device *p_dev;
439 struct pcmcia_device *tmp;
440 unsigned long flags;
441
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200442 dev_dbg(leftover ? &leftover->dev : &s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200443 "pcmcia_card_remove(%d) %s\n", s->sock,
444 leftover ? leftover->devname : "");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100445
446 if (!leftover)
447 s->device_count = 0;
448 else
449 s->device_count = 1;
450
451 /* unregister all pcmcia_devices registered with this socket, except leftover */
452 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
453 if (p_dev == leftover)
454 continue;
455
456 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
457 list_del(&p_dev->socket_device_list);
458 p_dev->_removed=1;
459 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
460
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200461 dev_dbg(&p_dev->dev, "unregistering device\n");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100462 device_unregister(&p_dev->dev);
463 }
464
465 return;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int pcmcia_device_remove(struct device * dev)
469{
470 struct pcmcia_device *p_dev;
471 struct pcmcia_driver *p_drv;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100472 struct pcmcia_device_id *did;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100473 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 p_dev = to_pcmcia_dev(dev);
476 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100477
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200478 dev_dbg(dev, "removing device\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200479
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100480 /* If we're removing the primary module driving a
481 * pseudo multi-function card, we need to unbind
482 * all devices
483 */
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700484 did = dev_get_drvdata(&p_dev->dev);
Hugh Dickinsb2f51a12006-03-10 21:15:19 +0000485 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100486 (p_dev->socket->device_count != 0) &&
487 (p_dev->device_no == 0))
488 pcmcia_card_remove(p_dev->socket, p_dev);
489
490 /* detach the "instance" */
Dominik Brodowskif3990712005-11-14 21:25:23 +0100491 if (!p_drv)
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dominik Brodowskif3990712005-11-14 21:25:23 +0100494 if (p_drv->remove)
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100495 p_drv->remove(p_dev);
496
Dominik Brodowskia0aab142006-04-04 11:09:26 +0200497 p_dev->dev_node = NULL;
498
Dominik Brodowskif3990712005-11-14 21:25:23 +0100499 /* check for proper unloading */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100500 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200501 dev_printk(KERN_INFO, dev,
502 "pcmcia: driver %s did not release config properly\n",
503 p_drv->drv.name);
Dominik Brodowskif3990712005-11-14 21:25:23 +0100504
505 for (i = 0; i < MAX_WIN; i++)
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100506 if (p_dev->_win & CLIENT_WIN_REQ(i))
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200507 dev_printk(KERN_INFO, dev,
508 "pcmcia: driver %s did not release window properly\n",
509 p_drv->drv.name);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100510
Dominik Brodowskif3990712005-11-14 21:25:23 +0100511 /* references from pcmcia_probe_device */
512 pcmcia_put_dev(p_dev);
513 module_put(p_drv->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return 0;
516}
517
518
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100519/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * pcmcia_device_query -- determine information about a pcmcia device
521 */
522static int pcmcia_device_query(struct pcmcia_device *p_dev)
523{
524 cistpl_manfid_t manf_id;
525 cistpl_funcid_t func_id;
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700526 cistpl_vers_1_t *vers1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned int i;
528
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700529 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
530 if (!vers1)
531 return -ENOMEM;
532
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200533 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 CISTPL_MANFID, &manf_id)) {
535 p_dev->manf_id = manf_id.manf;
536 p_dev->card_id = manf_id.card;
537 p_dev->has_manf_id = 1;
538 p_dev->has_card_id = 1;
539 }
540
541 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
542 CISTPL_FUNCID, &func_id)) {
543 p_dev->func_id = func_id.func;
544 p_dev->has_func_id = 1;
545 } else {
546 /* rule of thumb: cards with no FUNCID, but with
547 * common memory device geometry information, are
548 * probably memory cards (from pcmcia-cs) */
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700549 cistpl_device_geo_t *devgeo;
550
551 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
552 if (!devgeo) {
553 kfree(vers1);
554 return -ENOMEM;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700557 CISTPL_DEVICE_GEO, devgeo)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200558 dev_dbg(&p_dev->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200559 "mem device geometry probably means "
560 "FUNCID_MEMORY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 p_dev->func_id = CISTPL_FUNCID_MEMORY;
562 p_dev->has_func_id = 1;
563 }
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700564 kfree(devgeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200567 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700568 vers1)) {
Dominik Brodowskic5e09522009-10-19 00:04:25 +0200569 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 char *tmp;
571 unsigned int length;
572
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700573 tmp = vers1->str + vers1->ofs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 length = strlen(tmp) + 1;
Janos Farkas6e3d4f22006-03-31 01:04:57 +0200576 if ((length < 2) || (length > 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 continue;
578
579 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
580 GFP_KERNEL);
581 if (!p_dev->prod_id[i])
582 continue;
583
584 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
585 tmp, length);
586 }
587 }
588
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700589 kfree(vers1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593
594/* device_add_lock is needed to avoid double registration by cardmgr and kernel.
595 * Serializes pcmcia_device_add; will most likely be removed in future.
596 *
597 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
598 * won't work, this doesn't matter much at the moment: the driver core doesn't
599 * support it either.
600 */
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100601static DEFINE_MUTEX(device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dominik Brodowskidc109492005-06-27 16:28:50 -0700603struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100605 struct pcmcia_device *p_dev, *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long flags;
607
Dominik Brodowskidc109492005-06-27 16:28:50 -0700608 s = pcmcia_get_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!s)
610 return NULL;
611
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100612 mutex_lock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200614 pr_debug("adding device to %d, function %d\n", s->sock, function);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200615
Dominik Brodowski6cf5be52006-11-27 21:03:48 -0500616 /* max of 4 devices per card */
617 if (s->device_count == 4)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700618 goto err_put;
619
Dominik Brodowski8084b372005-12-11 21:18:26 +0100620 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!p_dev)
622 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Dominik Brodowskidc109492005-06-27 16:28:50 -0700624 p_dev->socket = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 p_dev->device_no = (s->device_count++);
626 p_dev->func = function;
627
628 p_dev->dev.bus = &pcmcia_bus_type;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200629 p_dev->dev.parent = s->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 p_dev->dev.release = pcmcia_release_dev;
James Bottomley43d9f7f2007-10-16 01:23:58 -0700631 /* by default don't allow DMA */
632 p_dev->dma_mask = DMA_MASK_NONE;
633 p_dev->dev.dma_mask = &p_dev->dma_mask;
Kay Sievers25096982008-11-01 11:46:06 +0100634 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
635 if (!dev_name(&p_dev->dev))
636 goto err_free;
637 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
Brice Goglinbd65a682005-09-09 13:03:29 -0700638 if (!p_dev->devname)
639 goto err_free;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200640 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100643
644 /*
645 * p_dev->function_config must be the same for all card functions.
646 * Note that this is serialized by the device_add_lock, so that
647 * only one such struct will be created.
648 */
649 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
650 if (p_dev->func == tmp_dev->func) {
651 p_dev->function_config = tmp_dev->function_config;
Komuro3e879f62008-11-02 19:33:24 +0900652 p_dev->io = tmp_dev->io;
653 p_dev->irq = tmp_dev->irq;
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100654 kref_get(&p_dev->function_config->ref);
655 }
656
657 /* Add to the list in pcmcia_bus_socket */
Komuro6171b882006-04-02 17:39:27 +0900658 list_add(&p_dev->socket_device_list, &s->devices_list);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
661
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100662 if (!p_dev->function_config) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200663 dev_dbg(&p_dev->dev, "creating config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100664 p_dev->function_config = kzalloc(sizeof(struct config_t),
665 GFP_KERNEL);
666 if (!p_dev->function_config)
667 goto err_unreg;
668 kref_init(&p_dev->function_config->ref);
669 }
670
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200671 dev_printk(KERN_NOTICE, &p_dev->dev,
672 "pcmcia: registering new device %s\n",
673 p_dev->devname);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100674
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700675 pcmcia_device_query(p_dev);
676
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100677 if (device_register(&p_dev->dev))
678 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100680 mutex_unlock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return p_dev;
683
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100684 err_unreg:
685 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
686 list_del(&p_dev->socket_device_list);
687 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 err_free:
Brice Goglinbd65a682005-09-09 13:03:29 -0700690 kfree(p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 kfree(p_dev);
692 s->device_count--;
693 err_put:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100694 mutex_unlock(&device_add_lock);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700695 pcmcia_put_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 return NULL;
698}
699
700
701static int pcmcia_card_add(struct pcmcia_socket *s)
702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 cistpl_longlink_mfc_t mfc;
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200704 unsigned int no_funcs, i, no_chains;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int ret = 0;
706
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200707 if (!(s->resource_setup_done)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200708 dev_dbg(&s->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200709 "no resources available, delaying card_add\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200713 if (pcmcia_validate_mem(s)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200714 dev_dbg(&s->dev, "validating mem resources failed, "
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200715 "delaying card_add\n");
Dominik Brodowskide759142005-09-28 19:41:56 +0200716 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200717 }
Dominik Brodowskide759142005-09-28 19:41:56 +0200718
Dominik Brodowski84897fc2009-10-18 23:51:09 +0200719 ret = pccard_validate_cis(s, &no_chains);
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200720 if (ret || !no_chains) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200721 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENODEV;
723 }
724
725 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
726 no_funcs = mfc.nfn;
727 else
728 no_funcs = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500729 s->functions = no_funcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 for (i=0; i < no_funcs; i++)
Dominik Brodowskidc109492005-06-27 16:28:50 -0700732 pcmcia_device_add(s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 return (ret);
735}
736
737
David Howells4796b712006-12-06 15:01:18 +0000738static void pcmcia_delayed_add_device(struct work_struct *work)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700739{
David Howellsc4028952006-11-22 14:57:56 +0000740 struct pcmcia_socket *s =
741 container_of(work, struct pcmcia_socket, device_add);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200742 dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500743 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
Dominik Brodowskib5e43912005-06-27 16:28:50 -0700744 s->pcmcia_state.device_add_pending = 0;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500745 s->pcmcia_state.mfc_pfc = 0;
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700746}
747
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700748static int pcmcia_requery(struct device *dev, void * _data)
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700749{
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700750 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200751 if (!p_dev->dev.driver) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200752 dev_dbg(dev, "update device information\n");
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700753 pcmcia_device_query(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200754 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700755
756 return 0;
757}
758
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500759static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700760{
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500761 int no_devices = 0;
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400762 int ret = 0;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700763 unsigned long flags;
764
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100765 /* must be called with skt_mutex held */
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200766 dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200767
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700768 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700769 if (list_empty(&skt->devices_list))
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500770 no_devices = 1;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700771 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
772
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500773 /* If this is because of a CIS override, start over */
774 if (new_cis && !no_devices)
775 pcmcia_card_remove(skt, NULL);
776
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700777 /* if no devices were added for this socket yet because of
778 * missing resource information or other trouble, we need to
779 * do this now. */
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500780 if (no_devices || new_cis) {
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400781 ret = pcmcia_card_add(skt);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700782 if (ret)
783 return;
784 }
785
786 /* some device information might have changed because of a CIS
787 * update or because we can finally read it correctly... so
788 * determine it again, overwriting old values if necessary. */
789 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
790
791 /* we re-scan all devices, not just the ones connected to this
792 * socket. This does not matter, though. */
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400793 ret = bus_rescan_devices(&pcmcia_bus_type);
794 if (ret)
795 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700796}
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700797
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500798#ifdef CONFIG_PCMCIA_LOAD_CIS
799
800/**
801 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
Randy Dunlap78187862007-12-10 15:49:22 -0800802 * @dev: the pcmcia device which needs a CIS override
803 * @filename: requested filename in /lib/firmware/
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500804 *
805 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
806 * the one provided by the card is broken. The firmware files reside in
807 * /lib/firmware/ in userspace.
808 */
809static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
810{
811 struct pcmcia_socket *s = dev->socket;
812 const struct firmware *fw;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500813 int ret = -ENOMEM;
814 int no_funcs;
815 int old_funcs;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500816 cistpl_longlink_mfc_t mfc;
817
818 if (!filename)
819 return -EINVAL;
820
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200821 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500822
Samuel Ortized62ace2009-05-27 00:49:35 +0200823 if (request_firmware(&fw, filename, &dev->dev) == 0) {
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200824 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
825 ret = -EINVAL;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200826 dev_printk(KERN_ERR, &dev->dev,
827 "pcmcia: CIS override is too big\n");
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500828 goto release;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200829 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500830
Dominik Brodowski53efec92008-07-28 19:44:05 +0200831 if (!pcmcia_replace_cis(s, fw->data, fw->size))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500832 ret = 0;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200833 else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200834 dev_printk(KERN_ERR, &dev->dev,
835 "pcmcia: CIS override failed\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200836 goto release;
837 }
838
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500839
840 /* update information */
841 pcmcia_device_query(dev);
842
843 /* does this cis override add or remove functions? */
844 old_funcs = s->functions;
845
846 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
847 no_funcs = mfc.nfn;
848 else
849 no_funcs = 1;
850 s->functions = no_funcs;
851
852 if (old_funcs > no_funcs)
853 pcmcia_card_remove(s, dev);
854 else if (no_funcs > old_funcs)
855 pcmcia_add_device_later(s, 1);
856 }
857 release:
858 release_firmware(fw);
859
860 return (ret);
861}
862
863#else /* !CONFIG_PCMCIA_LOAD_CIS */
864
865static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
866{
867 return -ENODEV;
868}
869
870#endif
871
872
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700873static inline int pcmcia_devmatch(struct pcmcia_device *dev,
874 struct pcmcia_device_id *did)
875{
876 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
877 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
878 return 0;
879 }
880
881 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
882 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
883 return 0;
884 }
885
886 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
887 if (dev->func != did->function)
888 return 0;
889 }
890
891 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
892 if (!dev->prod_id[0])
893 return 0;
894 if (strcmp(did->prod_id[0], dev->prod_id[0]))
895 return 0;
896 }
897
898 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
899 if (!dev->prod_id[1])
900 return 0;
901 if (strcmp(did->prod_id[1], dev->prod_id[1]))
902 return 0;
903 }
904
905 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
906 if (!dev->prod_id[2])
907 return 0;
908 if (strcmp(did->prod_id[2], dev->prod_id[2]))
909 return 0;
910 }
911
912 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
913 if (!dev->prod_id[3])
914 return 0;
915 if (strcmp(did->prod_id[3], dev->prod_id[3]))
916 return 0;
917 }
918
919 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700920 if (dev->device_no != did->device_no)
921 return 0;
922 }
923
924 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
925 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
926 return 0;
927
928 /* if this is a pseudo-multi-function device,
929 * we need explicit matches */
930 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
931 return 0;
932 if (dev->device_no)
933 return 0;
934
935 /* also, FUNC_ID matching needs to be activated by userspace
936 * after it has re-checked that there is no possible module
937 * with a prod_id/manf_id/card_id match.
938 */
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200939 dev_dbg(&dev->dev,
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200940 "skipping FUNC_ID match until userspace interaction\n");
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700941 if (!dev->allow_func_id_match)
942 return 0;
943 }
944
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700945 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200946 dev_dbg(&dev->dev, "device needs a fake CIS\n");
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700947 if (!dev->socket->fake_cis)
948 pcmcia_load_firmware(dev, did->cisfile);
949
950 if (!dev->socket->fake_cis)
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700951 return 0;
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700952 }
953
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700954 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
955 int i;
956 for (i=0; i<4; i++)
957 if (dev->prod_id[i])
958 return 0;
959 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
960 return 0;
961 }
962
Greg Kroah-Hartmanf3e7a7b2009-04-30 14:43:31 -0700963 dev_set_drvdata(&dev->dev, did);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700964
965 return 1;
966}
967
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
970 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
971 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700972 struct pcmcia_device_id *did = p_drv->id_table;
Bernhard Walle6179b552007-05-06 14:48:44 -0700973 struct pcmcia_dynid *dynid;
974
975 /* match dynamic devices first */
976 spin_lock(&p_drv->dynids.lock);
977 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200978 dev_dbg(dev, "trying to match to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -0700979 if (pcmcia_devmatch(p_dev, &dynid->id)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200980 dev_dbg(dev, "matched to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -0700981 spin_unlock(&p_drv->dynids.lock);
982 return 1;
983 }
984 }
985 spin_unlock(&p_drv->dynids.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dominik Brodowski0e0fad82006-01-15 01:14:31 +0100987#ifdef CONFIG_PCMCIA_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* matching by cardmgr */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200989 if (p_dev->cardmgr == p_drv) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200990 dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200992 }
Dominik Brodowski0e0fad82006-01-15 01:14:31 +0100993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700995 while (did && did->match_flags) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200996 dev_dbg(dev, "trying to match to %s\n", drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200997 if (pcmcia_devmatch(p_dev, did)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200998 dev_dbg(dev, "matched to %s\n", drv->name);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700999 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001000 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001001 did++;
1002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001007#ifdef CONFIG_HOTPLUG
1008
Kay Sievers7eff2e72007-08-14 15:15:12 +02001009static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001010{
1011 struct pcmcia_device *p_dev;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001012 int i;
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001013 u32 hash[4] = { 0, 0, 0, 0};
1014
1015 if (!dev)
1016 return -ENODEV;
1017
1018 p_dev = to_pcmcia_dev(dev);
1019
1020 /* calculate hashes */
1021 for (i=0; i<4; i++) {
1022 if (!p_dev->prod_id[i])
1023 continue;
1024 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1025 }
1026
Kay Sievers7eff2e72007-08-14 15:15:12 +02001027 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001028 return -ENOMEM;
1029
Kay Sievers7eff2e72007-08-14 15:15:12 +02001030 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001031 return -ENOMEM;
1032
Kay Sievers7eff2e72007-08-14 15:15:12 +02001033 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
Kay Sievers312c0042005-11-16 09:00:00 +01001034 "pa%08Xpb%08Xpc%08Xpd%08X",
1035 p_dev->has_manf_id ? p_dev->manf_id : 0,
1036 p_dev->has_card_id ? p_dev->card_id : 0,
1037 p_dev->has_func_id ? p_dev->func_id : 0,
1038 p_dev->func,
1039 p_dev->device_no,
1040 hash[0],
1041 hash[1],
1042 hash[2],
1043 hash[3]))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001044 return -ENOMEM;
1045
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001046 return 0;
1047}
1048
1049#else
1050
Kay Sievers7eff2e72007-08-14 15:15:12 +02001051static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001052{
1053 return -ENODEV;
1054}
1055
1056#endif
1057
Alan Stern3f8df782007-07-12 16:57:22 -04001058/************************ runtime PM support ***************************/
1059
1060static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1061static int pcmcia_dev_resume(struct device *dev);
1062
1063static int runtime_suspend(struct device *dev)
1064{
1065 int rc;
1066
1067 down(&dev->sem);
1068 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1069 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001070 return rc;
1071}
1072
1073static void runtime_resume(struct device *dev)
1074{
1075 int rc;
1076
1077 down(&dev->sem);
1078 rc = pcmcia_dev_resume(dev);
1079 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/************************ per-device sysfs output ***************************/
1083
1084#define pcmcia_device_attr(field, test, format) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001085static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{ \
1087 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1088 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1089}
1090
1091#define pcmcia_device_stringattr(name, field) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001092static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{ \
1094 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1095 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1096}
1097
1098pcmcia_device_attr(func, socket, "0x%02x\n");
1099pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1100pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1101pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1102pcmcia_device_stringattr(prod_id1, prod_id[0]);
1103pcmcia_device_stringattr(prod_id2, prod_id[1]);
1104pcmcia_device_stringattr(prod_id3, prod_id[2]);
1105pcmcia_device_stringattr(prod_id4, prod_id[3]);
1106
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001107
1108static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1109{
1110 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1111
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001112 if (p_dev->suspended)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001113 return sprintf(buf, "off\n");
1114 else
1115 return sprintf(buf, "on\n");
1116}
1117
1118static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1119 const char *buf, size_t count)
1120{
1121 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1122 int ret = 0;
1123
1124 if (!count)
1125 return -EINVAL;
1126
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001127 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
Alan Stern3f8df782007-07-12 16:57:22 -04001128 ret = runtime_suspend(dev);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001129 else if (p_dev->suspended && !strncmp(buf, "on", 2))
Alan Stern3f8df782007-07-12 16:57:22 -04001130 runtime_resume(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001131
1132 return ret ? ret : count;
1133}
1134
1135
Dominik Brodowski37045112005-06-30 02:58:47 -07001136static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001137{
1138 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1139 int i;
1140 u32 hash[4] = { 0, 0, 0, 0};
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001141
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001142 /* calculate hashes */
1143 for (i=0; i<4; i++) {
1144 if (!p_dev->prod_id[i])
1145 continue;
1146 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1147 }
1148 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1149 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1150 p_dev->has_manf_id ? p_dev->manf_id : 0,
1151 p_dev->has_card_id ? p_dev->card_id : 0,
1152 p_dev->has_func_id ? p_dev->func_id : 0,
1153 p_dev->func, p_dev->device_no,
1154 hash[0], hash[1], hash[2], hash[3]);
1155}
1156
1157static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1158 struct device_attribute *attr, const char *buf, size_t count)
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001159{
1160 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001161 int ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001162
1163 if (!count)
1164 return -EINVAL;
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001165
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001166 mutex_lock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001167 p_dev->allow_func_id_match = 1;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001168 mutex_unlock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001169
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001170 ret = bus_rescan_devices(&pcmcia_bus_type);
1171 if (ret)
1172 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1173 "allowing func_id matches\n");
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001174
1175 return count;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static struct device_attribute pcmcia_dev_attrs[] = {
1179 __ATTR(function, 0444, func_show, NULL),
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001180 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 __ATTR_RO(func_id),
1182 __ATTR_RO(manf_id),
1183 __ATTR_RO(card_id),
1184 __ATTR_RO(prod_id1),
1185 __ATTR_RO(prod_id2),
1186 __ATTR_RO(prod_id3),
1187 __ATTR_RO(prod_id4),
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001188 __ATTR_RO(modalias),
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001189 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 __ATTR_NULL,
1191};
1192
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001193/* PM support, also needed for reset */
1194
1195static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1196{
1197 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1198 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001199 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001200
David Brownelld6b4fa62008-02-04 22:27:37 -08001201 if (p_dev->suspended)
1202 return 0;
1203
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001204 dev_dbg(dev, "suspending\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001205
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001206 if (dev->driver)
1207 p_drv = to_pcmcia_drv(dev->driver);
1208
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001209 if (!p_drv)
1210 goto out;
1211
1212 if (p_drv->suspend) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001213 ret = p_drv->suspend(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001214 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001215 dev_printk(KERN_ERR, dev,
1216 "pcmcia: device %s (driver %s) did "
1217 "not want to go to sleep (%d)\n",
1218 p_dev->devname, p_drv->drv.name, ret);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001219 goto out;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001220 }
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001221 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001222
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001223 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001224 dev_dbg(dev, "releasing configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001225 pcmcia_release_configuration(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001226 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001227
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001228 out:
1229 if (!ret)
1230 p_dev->suspended = 1;
1231 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001232}
1233
1234
1235static int pcmcia_dev_resume(struct device * dev)
1236{
1237 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1238 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001239 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001240
David Brownelld6b4fa62008-02-04 22:27:37 -08001241 if (!p_dev->suspended)
1242 return 0;
1243
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001244 dev_dbg(dev, "resuming\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001245
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001246 if (dev->driver)
1247 p_drv = to_pcmcia_drv(dev->driver);
1248
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001249 if (!p_drv)
1250 goto out;
1251
1252 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001253 dev_dbg(dev, "requesting configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001254 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1255 if (ret)
1256 goto out;
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001257 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001258
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001259 if (p_drv->resume)
1260 ret = p_drv->resume(p_dev);
1261
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001262 out:
1263 if (!ret)
1264 p_dev->suspended = 0;
1265 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001266}
1267
1268
1269static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1270{
1271 struct pcmcia_socket *skt = _data;
1272 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1273
Alan Stern3f8df782007-07-12 16:57:22 -04001274 if (p_dev->socket != skt || p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001275 return 0;
1276
Alan Stern3f8df782007-07-12 16:57:22 -04001277 return runtime_suspend(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001278}
1279
1280static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1281{
1282 struct pcmcia_socket *skt = _data;
1283 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1284
Alan Stern3f8df782007-07-12 16:57:22 -04001285 if (p_dev->socket != skt || !p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001286 return 0;
1287
Alan Stern3f8df782007-07-12 16:57:22 -04001288 runtime_resume(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001289
1290 return 0;
1291}
1292
1293static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1294{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001295 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001296 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1297 return 0;
1298}
1299
1300static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1301{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001302 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001303 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1304 pcmcia_bus_suspend_callback)) {
1305 pcmcia_bus_resume(skt);
1306 return -EIO;
1307 }
1308 return 0;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312/*======================================================================
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 The card status event handler.
1315
1316======================================================================*/
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/* Normally, the event is passed to individual drivers after
1319 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1320 * is inversed to maintain historic compatibility.
1321 */
1322
1323static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1324{
Dominik Brodowskidc109492005-06-27 16:28:50 -07001325 struct pcmcia_socket *s = pcmcia_get_socket(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Florin Malita16174062006-05-24 21:21:31 -04001327 if (!s) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001328 dev_printk(KERN_ERR, &skt->dev,
1329 "PCMCIA obtaining reference to socket " \
1330 "failed, event 0x%x lost!\n", event);
Florin Malita16174062006-05-24 21:21:31 -04001331 return -ENODEV;
1332 }
1333
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001334 dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001335 event, priority, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001337 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case CS_EVENT_CARD_REMOVAL:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001339 s->pcmcia_state.present = 0;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +01001340 pcmcia_card_remove(skt, NULL);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001341 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 case CS_EVENT_CARD_INSERTION:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001345 s->pcmcia_state.present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 pcmcia_card_add(skt);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001347 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349
1350 case CS_EVENT_EJECTION_REQUEST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
1352
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001353 case CS_EVENT_PM_SUSPEND:
1354 case CS_EVENT_PM_RESUME:
1355 case CS_EVENT_RESET_PHYSICAL:
1356 case CS_EVENT_CARD_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 default:
Dominik Brodowskidc109492005-06-27 16:28:50 -07001358 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 }
1361
Dominik Brodowskidc109492005-06-27 16:28:50 -07001362 pcmcia_put_socket(s);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365} /* ds_event */
1366
1367
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001368struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1369{
1370 struct pcmcia_device *p_dev;
1371 struct pcmcia_device *ret = NULL;
1372
1373 p_dev = pcmcia_get_dev(_p_dev);
1374 if (!p_dev)
1375 return NULL;
1376
1377 if (!p_dev->socket->pcmcia_state.present)
1378 goto out;
1379
1380 if (p_dev->_removed)
1381 goto out;
1382
1383 if (p_dev->suspended)
1384 goto out;
1385
1386 ret = p_dev;
1387 out:
1388 pcmcia_put_dev(p_dev);
1389 return ret;
1390}
1391EXPORT_SYMBOL(pcmcia_dev_present);
1392
1393
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001394static struct pcmcia_callback pcmcia_bus_callback = {
1395 .owner = THIS_MODULE,
1396 .event = ds_event,
1397 .requery = pcmcia_bus_rescan,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001398 .suspend = pcmcia_bus_suspend,
1399 .resume = pcmcia_bus_resume,
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001400};
1401
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001402static int __devinit pcmcia_bus_add_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001403 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001405 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int ret;
1407
Dominik Brodowskidc109492005-06-27 16:28:50 -07001408 socket = pcmcia_get_socket(socket);
1409 if (!socket) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001410 dev_printk(KERN_ERR, dev,
1411 "PCMCIA obtaining reference to socket failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -ENODEV;
1413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /*
1416 * Ugly. But we want to wait for the socket threads to have started up.
1417 * We really should let the drivers themselves drive some of this..
1418 */
1419 msleep(250);
1420
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001421#ifdef CONFIG_PCMCIA_IOCTL
Dominik Brodowskidc109492005-06-27 16:28:50 -07001422 init_waitqueue_head(&socket->queue);
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001423#endif
Dominik Brodowskidc109492005-06-27 16:28:50 -07001424 INIT_LIST_HEAD(&socket->devices_list);
David Howells4796b712006-12-06 15:01:18 +00001425 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001426 memset(&socket->pcmcia_state, 0, sizeof(u8));
1427 socket->device_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001429 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001431 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -07001432 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return (ret);
1434 }
1435
1436 return 0;
1437}
1438
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001439static void pcmcia_bus_remove_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001440 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001442 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Dominik Brodowskidc109492005-06-27 16:28:50 -07001444 if (!socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return;
1446
Dominik Brodowskidc109492005-06-27 16:28:50 -07001447 socket->pcmcia_state.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 pccard_register_pcmcia(socket, NULL);
1449
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001450 /* unregister any unbound devices */
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001451 mutex_lock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001452 pcmcia_card_remove(socket, NULL);
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001453 mutex_unlock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001454
Dominik Brodowskidc109492005-06-27 16:28:50 -07001455 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 return;
1458}
1459
1460
1461/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
Sam Ravnborged49f5d2008-05-01 04:34:50 -07001462static struct class_interface pcmcia_bus_interface __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .class = &pcmcia_socket_class,
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001464 .add_dev = &pcmcia_bus_add_socket,
1465 .remove_dev = &pcmcia_bus_remove_socket,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466};
1467
1468
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001469struct bus_type pcmcia_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .name = "pcmcia",
Kay Sievers312c0042005-11-16 09:00:00 +01001471 .uevent = pcmcia_bus_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 .match = pcmcia_bus_match,
1473 .dev_attrs = pcmcia_dev_attrs,
Russell King1d0baa32006-01-05 14:40:58 +00001474 .probe = pcmcia_device_probe,
1475 .remove = pcmcia_device_remove,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001476 .suspend = pcmcia_dev_suspend,
1477 .resume = pcmcia_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480
1481static int __init init_pcmcia_bus(void)
1482{
Randy Dunlapace7d472006-10-20 14:44:12 -07001483 int ret;
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 spin_lock_init(&pcmcia_dev_list_lock);
1486
Randy Dunlapace7d472006-10-20 14:44:12 -07001487 ret = bus_register(&pcmcia_bus_type);
1488 if (ret < 0) {
1489 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1490 return ret;
1491 }
1492 ret = class_interface_register(&pcmcia_bus_interface);
1493 if (ret < 0) {
1494 printk(KERN_WARNING
1495 "pcmcia: class_interface_register error: %d\n", ret);
1496 bus_unregister(&pcmcia_bus_type);
1497 return ret;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001500 pcmcia_setup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return 0;
1503}
1504fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1505 * pcmcia_socket_class is already registered */
1506
1507
1508static void __exit exit_pcmcia_bus(void)
1509{
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001510 pcmcia_cleanup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001512 class_interface_unregister(&pcmcia_bus_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 bus_unregister(&pcmcia_bus_type);
1515}
1516module_exit(exit_pcmcia_bus);
1517
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519MODULE_ALIAS("ds");