blob: f6ff1218b3f39dfb3fa8f08a9703c15c3a0ab15b [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"
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070035#include "ds_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*====================================================================*/
38
39/* Module parameters */
40
41MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
42MODULE_DESCRIPTION("PCMCIA Driver Services");
43MODULE_LICENSE("GPL");
44
Dominik Brodowski7d16b652008-08-02 21:02:01 +020045#ifdef CONFIG_PCMCIA_DEBUG
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070046int ds_pc_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48module_param_named(pc_debug, ds_pc_debug, int, 0644);
49
50#define ds_dbg(lvl, fmt, arg...) do { \
Dominik Brodowskiac449d62008-08-02 18:33:56 +020051 if (ds_pc_debug > (lvl)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 printk(KERN_DEBUG "ds: " fmt , ## arg); \
53} while (0)
Dominik Brodowskiac449d62008-08-02 18:33:56 +020054#define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
55 if (ds_pc_debug > (lvl)) \
56 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
57} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
59#define ds_dbg(lvl, fmt, arg...) do { } while (0)
Dominik Brodowskiac449d62008-08-02 18:33:56 +020060#define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070063spinlock_t pcmcia_dev_list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*====================================================================*/
66
67/* code which was in cs.c before */
68
69/* String tables for error messages */
70
71typedef struct lookup_t {
72 int key;
73 char *msg;
74} lookup_t;
75
76static const lookup_t error_table[] = {
77 { CS_SUCCESS, "Operation succeeded" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 { CS_BAD_ATTRIBUTE, "Bad attribute", },
79 { CS_BAD_BASE, "Bad base address" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 { CS_BAD_IRQ, "Bad IRQ" },
81 { CS_BAD_OFFSET, "Bad offset" },
82 { CS_BAD_PAGE, "Bad page number" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 { CS_BAD_SIZE, "Bad size" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 { CS_BAD_TYPE, "Bad type" },
Dominik Brodowskid8b0a492008-08-03 10:56:13 +020085 { -EIO, "Input/Output error" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 { CS_NO_CARD, "No card present" },
Dominik Brodowskide6405e2008-08-03 10:47:59 +020087 { -EINVAL, "Bad parameter" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 { CS_GENERAL_FAILURE, "General failure" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 { CS_BAD_ARGS, "Bad arguments" },
90 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
91 { CS_IN_USE, "Resource in use" },
92 { CS_NO_MORE_ITEMS, "No more items" },
93 { CS_OUT_OF_RESOURCE, "Out of resource" },
94 { CS_BAD_HANDLE, "Bad handle" },
95 { CS_BAD_TUPLE, "Bad CIS tuple" }
96};
97
98
99static const lookup_t service_table[] = {
100 { AccessConfigurationRegister, "AccessConfigurationRegister" },
101 { AddSocketServices, "AddSocketServices" },
102 { AdjustResourceInfo, "AdjustResourceInfo" },
103 { CheckEraseQueue, "CheckEraseQueue" },
104 { CloseMemory, "CloseMemory" },
105 { DeregisterClient, "DeregisterClient" },
106 { DeregisterEraseQueue, "DeregisterEraseQueue" },
107 { GetCardServicesInfo, "GetCardServicesInfo" },
108 { GetClientInfo, "GetClientInfo" },
109 { GetConfigurationInfo, "GetConfigurationInfo" },
110 { GetEventMask, "GetEventMask" },
111 { GetFirstClient, "GetFirstClient" },
112 { GetFirstRegion, "GetFirstRegion" },
113 { GetFirstTuple, "GetFirstTuple" },
114 { GetNextClient, "GetNextClient" },
115 { GetNextRegion, "GetNextRegion" },
116 { GetNextTuple, "GetNextTuple" },
117 { GetStatus, "GetStatus" },
118 { GetTupleData, "GetTupleData" },
119 { MapMemPage, "MapMemPage" },
120 { ModifyConfiguration, "ModifyConfiguration" },
121 { ModifyWindow, "ModifyWindow" },
122 { OpenMemory, "OpenMemory" },
123 { ParseTuple, "ParseTuple" },
124 { ReadMemory, "ReadMemory" },
125 { RegisterClient, "RegisterClient" },
126 { RegisterEraseQueue, "RegisterEraseQueue" },
127 { RegisterMTD, "RegisterMTD" },
128 { ReleaseConfiguration, "ReleaseConfiguration" },
129 { ReleaseIO, "ReleaseIO" },
130 { ReleaseIRQ, "ReleaseIRQ" },
131 { ReleaseWindow, "ReleaseWindow" },
132 { RequestConfiguration, "RequestConfiguration" },
133 { RequestIO, "RequestIO" },
134 { RequestIRQ, "RequestIRQ" },
135 { RequestSocketMask, "RequestSocketMask" },
136 { RequestWindow, "RequestWindow" },
137 { ResetCard, "ResetCard" },
138 { SetEventMask, "SetEventMask" },
139 { ValidateCIS, "ValidateCIS" },
140 { WriteMemory, "WriteMemory" },
141 { BindDevice, "BindDevice" },
142 { BindMTD, "BindMTD" },
143 { ReportError, "ReportError" },
144 { SuspendCard, "SuspendCard" },
145 { ResumeCard, "ResumeCard" },
146 { EjectCard, "EjectCard" },
147 { InsertCard, "InsertCard" },
148 { ReplaceCIS, "ReplaceCIS" }
149};
150
151
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700152static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int i;
155 char *serv;
156
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700157 if (!p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 printk(KERN_NOTICE);
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700159 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 for (i = 0; i < ARRAY_SIZE(service_table); i++)
163 if (service_table[i].key == err->func)
164 break;
165 if (i < ARRAY_SIZE(service_table))
166 serv = service_table[i].msg;
167 else
168 serv = "Unknown service number";
169
170 for (i = 0; i < ARRAY_SIZE(error_table); i++)
171 if (error_table[i].key == err->retcode)
172 break;
173 if (i < ARRAY_SIZE(error_table))
174 printk("%s: %s\n", serv, error_table[i].msg);
175 else
176 printk("%s: Unknown error code %#x\n", serv, err->retcode);
177
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179} /* report_error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* end of code which was in cs.c before */
182
183/*======================================================================*/
184
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700185void cs_error(struct pcmcia_device *p_dev, int func, int ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 error_info_t err = { func, ret };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700188 pcmcia_report_error(p_dev, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190EXPORT_SYMBOL(cs_error);
191
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700192
193static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
194{
195 struct pcmcia_device_id *did = p_drv->id_table;
196 unsigned int i;
197 u32 hash;
198
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100199 if (!p_drv->probe || !p_drv->remove)
Pavel Roskinba5bb6b2005-07-28 01:07:20 -0700200 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
201 "function\n", p_drv->drv.name);
Dominik Brodowski1e212f32005-07-07 17:59:00 -0700202
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700203 while (did && did->match_flags) {
204 for (i=0; i<4; i++) {
205 if (!did->prod_id[i])
206 continue;
207
208 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
209 if (hash == did->prod_id_hash[i])
210 continue;
211
212 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
213 "product string \"%s\": is 0x%x, should "
214 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
215 did->prod_id_hash[i], hash);
Dominik Brodowski5085cb22005-06-27 16:28:45 -0700216 printk(KERN_DEBUG "pcmcia: see "
217 "Documentation/pcmcia/devicetable.txt for "
218 "details\n");
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700219 }
220 did++;
221 }
222
223 return;
224}
225
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*======================================================================*/
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Bernhard Walle6179b552007-05-06 14:48:44 -0700230struct pcmcia_dynid {
231 struct list_head node;
232 struct pcmcia_device_id id;
233};
234
235/**
236 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
237 * @driver: target device driver
238 * @buf: buffer for scanning device ID data
239 * @count: input size
240 *
241 * Adds a new dynamic PCMCIA device ID to this driver,
242 * and causes the driver to probe for all devices again.
243 */
244static ssize_t
245pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
246{
247 struct pcmcia_dynid *dynid;
248 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
249 __u16 match_flags, manf_id, card_id;
250 __u8 func_id, function, device_no;
251 __u32 prod_id_hash[4] = {0, 0, 0, 0};
252 int fields=0;
253 int retval = 0;
254
255 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
256 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
257 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
258 if (fields < 6)
259 return -EINVAL;
260
261 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
262 if (!dynid)
263 return -ENOMEM;
264
265 INIT_LIST_HEAD(&dynid->node);
266 dynid->id.match_flags = match_flags;
267 dynid->id.manf_id = manf_id;
268 dynid->id.card_id = card_id;
269 dynid->id.func_id = func_id;
270 dynid->id.function = function;
271 dynid->id.device_no = device_no;
272 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
273
274 spin_lock(&pdrv->dynids.lock);
275 list_add_tail(&pdrv->dynids.list, &dynid->node);
276 spin_unlock(&pdrv->dynids.lock);
277
278 if (get_driver(&pdrv->drv)) {
279 retval = driver_attach(&pdrv->drv);
280 put_driver(&pdrv->drv);
281 }
282
283 if (retval)
284 return retval;
285 return count;
286}
287static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
288
289static void
290pcmcia_free_dynids(struct pcmcia_driver *drv)
291{
292 struct pcmcia_dynid *dynid, *n;
293
294 spin_lock(&drv->dynids.lock);
295 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
296 list_del(&dynid->node);
297 kfree(dynid);
298 }
299 spin_unlock(&drv->dynids.lock);
300}
301
302static int
303pcmcia_create_newid_file(struct pcmcia_driver *drv)
304{
305 int error = 0;
306 if (drv->probe != NULL)
Greg Kroah-Hartman2344c6d2007-11-28 12:23:18 -0800307 error = driver_create_file(&drv->drv, &driver_attr_new_id);
Bernhard Walle6179b552007-05-06 14:48:44 -0700308 return error;
309}
310
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/**
313 * pcmcia_register_driver - register a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800314 * @driver: the &driver being registered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
316 * Registers a PCMCIA driver with the PCMCIA bus core.
317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318int pcmcia_register_driver(struct pcmcia_driver *driver)
319{
Bernhard Walle6179b552007-05-06 14:48:44 -0700320 int error;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (!driver)
323 return -EINVAL;
324
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700325 pcmcia_check_driver(driver);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* initialize common fields */
328 driver->drv.bus = &pcmcia_bus_type;
329 driver->drv.owner = driver->owner;
Bernhard Walle6179b552007-05-06 14:48:44 -0700330 spin_lock_init(&driver->dynids.lock);
331 INIT_LIST_HEAD(&driver->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200333 ds_dbg(3, "registering driver %s\n", driver->drv.name);
334
Bernhard Walle6179b552007-05-06 14:48:44 -0700335 error = driver_register(&driver->drv);
336 if (error < 0)
337 return error;
338
339 error = pcmcia_create_newid_file(driver);
340 if (error)
341 driver_unregister(&driver->drv);
342
343 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345EXPORT_SYMBOL(pcmcia_register_driver);
346
347/**
348 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800349 * @driver: the &driver being unregistered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351void pcmcia_unregister_driver(struct pcmcia_driver *driver)
352{
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200353 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 driver_unregister(&driver->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700355 pcmcia_free_dynids(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357EXPORT_SYMBOL(pcmcia_unregister_driver);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360/* pcmcia_device handling */
361
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700362struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct device *tmp_dev;
365 tmp_dev = get_device(&p_dev->dev);
366 if (!tmp_dev)
367 return NULL;
368 return to_pcmcia_dev(tmp_dev);
369}
370
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700371void pcmcia_put_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 if (p_dev)
374 put_device(&p_dev->dev);
375}
376
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100377static void pcmcia_release_function(struct kref *ref)
378{
379 struct config_t *c = container_of(ref, struct config_t, ref);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200380 ds_dbg(1, "releasing config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100381 kfree(c);
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void pcmcia_release_dev(struct device *dev)
385{
386 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200387 ds_dev_dbg(1, dev, "releasing device\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -0700388 pcmcia_put_socket(p_dev->socket);
Brice Goglinbd65a682005-09-09 13:03:29 -0700389 kfree(p_dev->devname);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100390 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 kfree(p_dev);
392}
393
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500394static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100395{
396 if (!s->pcmcia_state.device_add_pending) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200397 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200398 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100399 s->pcmcia_state.device_add_pending = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500400 s->pcmcia_state.mfc_pfc = mfc;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100401 schedule_work(&s->device_add);
402 }
403 return;
404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406static int pcmcia_device_probe(struct device * dev)
407{
408 struct pcmcia_device *p_dev;
409 struct pcmcia_driver *p_drv;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100410 struct pcmcia_device_id *did;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100411 struct pcmcia_socket *s;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400412 cistpl_config_t cis_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int ret = 0;
414
415 dev = get_device(dev);
416 if (!dev)
417 return -ENODEV;
418
419 p_dev = to_pcmcia_dev(dev);
420 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100421 s = p_dev->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200423 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200424
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100425 if ((!p_drv->probe) || (!p_dev->function_config) ||
426 (!try_module_get(p_drv->owner))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ret = -EINVAL;
428 goto put_dev;
429 }
430
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400431 /* set up some more device information */
432 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
433 &cis_config);
434 if (!ret) {
435 p_dev->conf.ConfigBase = cis_config.base;
436 p_dev->conf.Present = cis_config.rmask[0];
437 } else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200438 dev_printk(KERN_INFO, dev,
439 "pcmcia: could not parse base and rmask0 of CIS\n");
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400440 p_dev->conf.ConfigBase = 0;
441 p_dev->conf.Present = 0;
442 }
443
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100444 ret = p_drv->probe(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200445 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200446 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
447 p_drv->drv.name, ret);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100448 goto put_module;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200449 }
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100450
451 /* handle pseudo multifunction devices:
452 * there are at most two pseudo multifunction devices.
453 * if we're matching against the first, schedule a
454 * call which will then check whether there are two
455 * pseudo devices, and if not, add the second one.
456 */
Andrew Morton80a55e92006-04-10 23:24:57 -0700457 did = p_dev->dev.driver_data;
Hugh Dickins4ceadbf2006-03-10 21:13:44 +0000458 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100459 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500460 pcmcia_add_device_later(p_dev->socket, 0);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100461
462 put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ret)
464 module_put(p_drv->owner);
465 put_dev:
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100466 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 put_device(dev);
468 return (ret);
469}
470
471
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100472/*
473 * Removes a PCMCIA card from the device tree and socket list.
474 */
475static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
476{
477 struct pcmcia_device *p_dev;
478 struct pcmcia_device *tmp;
479 unsigned long flags;
480
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200481 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
482 "pcmcia_card_remove(%d) %s\n", s->sock,
483 leftover ? leftover->devname : "");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100484
485 if (!leftover)
486 s->device_count = 0;
487 else
488 s->device_count = 1;
489
490 /* unregister all pcmcia_devices registered with this socket, except leftover */
491 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
492 if (p_dev == leftover)
493 continue;
494
495 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
496 list_del(&p_dev->socket_device_list);
497 p_dev->_removed=1;
498 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
499
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200500 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100501 device_unregister(&p_dev->dev);
502 }
503
504 return;
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static int pcmcia_device_remove(struct device * dev)
508{
509 struct pcmcia_device *p_dev;
510 struct pcmcia_driver *p_drv;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100511 struct pcmcia_device_id *did;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100512 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 p_dev = to_pcmcia_dev(dev);
515 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100516
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200517 ds_dev_dbg(1, dev, "removing device\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200518
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100519 /* If we're removing the primary module driving a
520 * pseudo multi-function card, we need to unbind
521 * all devices
522 */
Andrew Morton80a55e92006-04-10 23:24:57 -0700523 did = p_dev->dev.driver_data;
Hugh Dickinsb2f51a12006-03-10 21:15:19 +0000524 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100525 (p_dev->socket->device_count != 0) &&
526 (p_dev->device_no == 0))
527 pcmcia_card_remove(p_dev->socket, p_dev);
528
529 /* detach the "instance" */
Dominik Brodowskif3990712005-11-14 21:25:23 +0100530 if (!p_drv)
531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Dominik Brodowskif3990712005-11-14 21:25:23 +0100533 if (p_drv->remove)
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100534 p_drv->remove(p_dev);
535
Dominik Brodowskia0aab142006-04-04 11:09:26 +0200536 p_dev->dev_node = NULL;
537
Dominik Brodowskif3990712005-11-14 21:25:23 +0100538 /* check for proper unloading */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100539 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200540 dev_printk(KERN_INFO, dev,
541 "pcmcia: driver %s did not release config properly\n",
542 p_drv->drv.name);
Dominik Brodowskif3990712005-11-14 21:25:23 +0100543
544 for (i = 0; i < MAX_WIN; i++)
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100545 if (p_dev->_win & CLIENT_WIN_REQ(i))
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200546 dev_printk(KERN_INFO, dev,
547 "pcmcia: driver %s did not release window properly\n",
548 p_drv->drv.name);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100549
Dominik Brodowskif3990712005-11-14 21:25:23 +0100550 /* references from pcmcia_probe_device */
551 pcmcia_put_dev(p_dev);
552 module_put(p_drv->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return 0;
555}
556
557
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100558/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * pcmcia_device_query -- determine information about a pcmcia device
560 */
561static int pcmcia_device_query(struct pcmcia_device *p_dev)
562{
563 cistpl_manfid_t manf_id;
564 cistpl_funcid_t func_id;
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700565 cistpl_vers_1_t *vers1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 unsigned int i;
567
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700568 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
569 if (!vers1)
570 return -ENOMEM;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
573 CISTPL_MANFID, &manf_id)) {
574 p_dev->manf_id = manf_id.manf;
575 p_dev->card_id = manf_id.card;
576 p_dev->has_manf_id = 1;
577 p_dev->has_card_id = 1;
578 }
579
580 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
581 CISTPL_FUNCID, &func_id)) {
582 p_dev->func_id = func_id.func;
583 p_dev->has_func_id = 1;
584 } else {
585 /* rule of thumb: cards with no FUNCID, but with
586 * common memory device geometry information, are
587 * probably memory cards (from pcmcia-cs) */
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700588 cistpl_device_geo_t *devgeo;
589
590 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
591 if (!devgeo) {
592 kfree(vers1);
593 return -ENOMEM;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700596 CISTPL_DEVICE_GEO, devgeo)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200597 ds_dev_dbg(0, &p_dev->dev,
598 "mem device geometry probably means "
599 "FUNCID_MEMORY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 p_dev->func_id = CISTPL_FUNCID_MEMORY;
601 p_dev->has_func_id = 1;
602 }
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700603 kfree(devgeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700607 vers1)) {
608 for (i=0; i < vers1->ns; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 char *tmp;
610 unsigned int length;
611
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700612 tmp = vers1->str + vers1->ofs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 length = strlen(tmp) + 1;
Janos Farkas6e3d4f22006-03-31 01:04:57 +0200615 if ((length < 2) || (length > 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 continue;
617
618 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
619 GFP_KERNEL);
620 if (!p_dev->prod_id[i])
621 continue;
622
623 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
624 tmp, length);
625 }
626 }
627
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700628 kfree(vers1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
631
632
633/* device_add_lock is needed to avoid double registration by cardmgr and kernel.
634 * Serializes pcmcia_device_add; will most likely be removed in future.
635 *
636 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
637 * won't work, this doesn't matter much at the moment: the driver core doesn't
638 * support it either.
639 */
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100640static DEFINE_MUTEX(device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dominik Brodowskidc109492005-06-27 16:28:50 -0700642struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100644 struct pcmcia_device *p_dev, *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned long flags;
Brice Goglinbd65a682005-09-09 13:03:29 -0700646 int bus_id_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Dominik Brodowskidc109492005-06-27 16:28:50 -0700648 s = pcmcia_get_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!s)
650 return NULL;
651
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100652 mutex_lock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200654 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
655
Dominik Brodowski6cf5be52006-11-27 21:03:48 -0500656 /* max of 4 devices per card */
657 if (s->device_count == 4)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700658 goto err_put;
659
Dominik Brodowski8084b372005-12-11 21:18:26 +0100660 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!p_dev)
662 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dominik Brodowskidc109492005-06-27 16:28:50 -0700664 p_dev->socket = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 p_dev->device_no = (s->device_count++);
666 p_dev->func = function;
667
668 p_dev->dev.bus = &pcmcia_bus_type;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200669 p_dev->dev.parent = s->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 p_dev->dev.release = pcmcia_release_dev;
James Bottomley43d9f7f2007-10-16 01:23:58 -0700671 /* by default don't allow DMA */
672 p_dev->dma_mask = DMA_MASK_NONE;
673 p_dev->dev.dma_mask = &p_dev->dma_mask;
Brice Goglinbd65a682005-09-09 13:03:29 -0700674 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
675
676 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
677 if (!p_dev->devname)
678 goto err_free;
679 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200680 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100683
684 /*
685 * p_dev->function_config must be the same for all card functions.
686 * Note that this is serialized by the device_add_lock, so that
687 * only one such struct will be created.
688 */
689 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
690 if (p_dev->func == tmp_dev->func) {
691 p_dev->function_config = tmp_dev->function_config;
692 kref_get(&p_dev->function_config->ref);
693 }
694
695 /* Add to the list in pcmcia_bus_socket */
Komuro6171b882006-04-02 17:39:27 +0900696 list_add(&p_dev->socket_device_list, &s->devices_list);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
699
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100700 if (!p_dev->function_config) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200701 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100702 p_dev->function_config = kzalloc(sizeof(struct config_t),
703 GFP_KERNEL);
704 if (!p_dev->function_config)
705 goto err_unreg;
706 kref_init(&p_dev->function_config->ref);
707 }
708
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200709 dev_printk(KERN_NOTICE, &p_dev->dev,
710 "pcmcia: registering new device %s\n",
711 p_dev->devname);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100712
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700713 pcmcia_device_query(p_dev);
714
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100715 if (device_register(&p_dev->dev))
716 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100718 mutex_unlock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 return p_dev;
721
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100722 err_unreg:
723 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
724 list_del(&p_dev->socket_device_list);
725 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 err_free:
Brice Goglinbd65a682005-09-09 13:03:29 -0700728 kfree(p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(p_dev);
730 s->device_count--;
731 err_put:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100732 mutex_unlock(&device_add_lock);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700733 pcmcia_put_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 return NULL;
736}
737
738
739static int pcmcia_card_add(struct pcmcia_socket *s)
740{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 cistpl_longlink_mfc_t mfc;
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200742 unsigned int no_funcs, i, no_chains;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int ret = 0;
744
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200745 if (!(s->resource_setup_done)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200746 ds_dev_dbg(3, &s->dev,
747 "no resources available, delaying card_add\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200751 if (pcmcia_validate_mem(s)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200752 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200753 "delaying card_add\n");
Dominik Brodowskide759142005-09-28 19:41:56 +0200754 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200755 }
Dominik Brodowskide759142005-09-28 19:41:56 +0200756
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200757 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
758 if (ret || !no_chains) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200759 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -ENODEV;
761 }
762
763 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
764 no_funcs = mfc.nfn;
765 else
766 no_funcs = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500767 s->functions = no_funcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 for (i=0; i < no_funcs; i++)
Dominik Brodowskidc109492005-06-27 16:28:50 -0700770 pcmcia_device_add(s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 return (ret);
773}
774
775
David Howells4796b712006-12-06 15:01:18 +0000776static void pcmcia_delayed_add_device(struct work_struct *work)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700777{
David Howellsc4028952006-11-22 14:57:56 +0000778 struct pcmcia_socket *s =
779 container_of(work, struct pcmcia_socket, device_add);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200780 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500781 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
Dominik Brodowskib5e43912005-06-27 16:28:50 -0700782 s->pcmcia_state.device_add_pending = 0;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500783 s->pcmcia_state.mfc_pfc = 0;
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700784}
785
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700786static int pcmcia_requery(struct device *dev, void * _data)
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700787{
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700788 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200789 if (!p_dev->dev.driver) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200790 ds_dev_dbg(1, dev, "update device information\n");
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700791 pcmcia_device_query(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200792 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700793
794 return 0;
795}
796
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500797static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700798{
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500799 int no_devices = 0;
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400800 int ret = 0;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700801 unsigned long flags;
802
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100803 /* must be called with skt_mutex held */
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200804 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200805
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700806 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700807 if (list_empty(&skt->devices_list))
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500808 no_devices = 1;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700809 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
810
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500811 /* If this is because of a CIS override, start over */
812 if (new_cis && !no_devices)
813 pcmcia_card_remove(skt, NULL);
814
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700815 /* if no devices were added for this socket yet because of
816 * missing resource information or other trouble, we need to
817 * do this now. */
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500818 if (no_devices || new_cis) {
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400819 ret = pcmcia_card_add(skt);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700820 if (ret)
821 return;
822 }
823
824 /* some device information might have changed because of a CIS
825 * update or because we can finally read it correctly... so
826 * determine it again, overwriting old values if necessary. */
827 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
828
829 /* we re-scan all devices, not just the ones connected to this
830 * socket. This does not matter, though. */
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400831 ret = bus_rescan_devices(&pcmcia_bus_type);
832 if (ret)
833 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700834}
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700835
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500836#ifdef CONFIG_PCMCIA_LOAD_CIS
837
838/**
839 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
Randy Dunlap78187862007-12-10 15:49:22 -0800840 * @dev: the pcmcia device which needs a CIS override
841 * @filename: requested filename in /lib/firmware/
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500842 *
843 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
844 * the one provided by the card is broken. The firmware files reside in
845 * /lib/firmware/ in userspace.
846 */
847static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
848{
849 struct pcmcia_socket *s = dev->socket;
850 const struct firmware *fw;
Dominik Brodowskifeda4f22008-06-24 15:45:55 +0200851 char path[FIRMWARE_NAME_MAX];
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500852 int ret = -ENOMEM;
853 int no_funcs;
854 int old_funcs;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500855 cistpl_longlink_mfc_t mfc;
856
857 if (!filename)
858 return -EINVAL;
859
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200860 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500861
Dominik Brodowskifeda4f22008-06-24 15:45:55 +0200862 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200863 dev_printk(KERN_WARNING, &dev->dev,
864 "pcmcia: CIS filename is too long [%s]\n",
865 filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500866 return -EINVAL;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200867 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500868
Randy Dunlap52debb02008-02-04 22:27:38 -0800869 snprintf(path, sizeof(path), "%s", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500870
871 if (request_firmware(&fw, path, &dev->dev) == 0) {
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200872 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
873 ret = -EINVAL;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200874 dev_printk(KERN_ERR, &dev->dev,
875 "pcmcia: CIS override is too big\n");
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500876 goto release;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200877 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500878
Dominik Brodowski53efec92008-07-28 19:44:05 +0200879 if (!pcmcia_replace_cis(s, fw->data, fw->size))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500880 ret = 0;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200881 else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200882 dev_printk(KERN_ERR, &dev->dev,
883 "pcmcia: CIS override failed\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200884 goto release;
885 }
886
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500887
888 /* update information */
889 pcmcia_device_query(dev);
890
891 /* does this cis override add or remove functions? */
892 old_funcs = s->functions;
893
894 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
895 no_funcs = mfc.nfn;
896 else
897 no_funcs = 1;
898 s->functions = no_funcs;
899
900 if (old_funcs > no_funcs)
901 pcmcia_card_remove(s, dev);
902 else if (no_funcs > old_funcs)
903 pcmcia_add_device_later(s, 1);
904 }
905 release:
906 release_firmware(fw);
907
908 return (ret);
909}
910
911#else /* !CONFIG_PCMCIA_LOAD_CIS */
912
913static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
914{
915 return -ENODEV;
916}
917
918#endif
919
920
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700921static inline int pcmcia_devmatch(struct pcmcia_device *dev,
922 struct pcmcia_device_id *did)
923{
924 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
925 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
926 return 0;
927 }
928
929 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
930 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
931 return 0;
932 }
933
934 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
935 if (dev->func != did->function)
936 return 0;
937 }
938
939 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
940 if (!dev->prod_id[0])
941 return 0;
942 if (strcmp(did->prod_id[0], dev->prod_id[0]))
943 return 0;
944 }
945
946 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
947 if (!dev->prod_id[1])
948 return 0;
949 if (strcmp(did->prod_id[1], dev->prod_id[1]))
950 return 0;
951 }
952
953 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
954 if (!dev->prod_id[2])
955 return 0;
956 if (strcmp(did->prod_id[2], dev->prod_id[2]))
957 return 0;
958 }
959
960 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
961 if (!dev->prod_id[3])
962 return 0;
963 if (strcmp(did->prod_id[3], dev->prod_id[3]))
964 return 0;
965 }
966
967 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700968 if (dev->device_no != did->device_no)
969 return 0;
970 }
971
972 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
973 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
974 return 0;
975
976 /* if this is a pseudo-multi-function device,
977 * we need explicit matches */
978 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
979 return 0;
980 if (dev->device_no)
981 return 0;
982
983 /* also, FUNC_ID matching needs to be activated by userspace
984 * after it has re-checked that there is no possible module
985 * with a prod_id/manf_id/card_id match.
986 */
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200987 ds_dev_dbg(0, &dev->dev,
988 "skipping FUNC_ID match until userspace interaction\n");
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700989 if (!dev->allow_func_id_match)
990 return 0;
991 }
992
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700993 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200994 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700995 if (!dev->socket->fake_cis)
996 pcmcia_load_firmware(dev, did->cisfile);
997
998 if (!dev->socket->fake_cis)
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700999 return 0;
Dominik Brodowskiea7b3882005-06-27 16:28:07 -07001000 }
1001
Dominik Brodowskif602ff72005-06-27 16:28:09 -07001002 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
1003 int i;
1004 for (i=0; i<4; i++)
1005 if (dev->prod_id[i])
1006 return 0;
1007 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
1008 return 0;
1009 }
1010
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001011 dev->dev.driver_data = (void *) did;
1012
1013 return 1;
1014}
1015
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1018 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1019 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001020 struct pcmcia_device_id *did = p_drv->id_table;
Bernhard Walle6179b552007-05-06 14:48:44 -07001021 struct pcmcia_dynid *dynid;
1022
1023 /* match dynamic devices first */
1024 spin_lock(&p_drv->dynids.lock);
1025 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001026 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -07001027 if (pcmcia_devmatch(p_dev, &dynid->id)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001028 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -07001029 spin_unlock(&p_drv->dynids.lock);
1030 return 1;
1031 }
1032 }
1033 spin_unlock(&p_drv->dynids.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dominik Brodowski0e0fad82006-01-15 01:14:31 +01001035#ifdef CONFIG_PCMCIA_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /* matching by cardmgr */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001037 if (p_dev->cardmgr == p_drv) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001038 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001040 }
Dominik Brodowski0e0fad82006-01-15 01:14:31 +01001041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001043 while (did && did->match_flags) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001044 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001045 if (pcmcia_devmatch(p_dev, did)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001046 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001047 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001048 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001049 did++;
1050 }
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053}
1054
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001055#ifdef CONFIG_HOTPLUG
1056
Kay Sievers7eff2e72007-08-14 15:15:12 +02001057static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001058{
1059 struct pcmcia_device *p_dev;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001060 int i;
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001061 u32 hash[4] = { 0, 0, 0, 0};
1062
1063 if (!dev)
1064 return -ENODEV;
1065
1066 p_dev = to_pcmcia_dev(dev);
1067
1068 /* calculate hashes */
1069 for (i=0; i<4; i++) {
1070 if (!p_dev->prod_id[i])
1071 continue;
1072 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1073 }
1074
Kay Sievers7eff2e72007-08-14 15:15:12 +02001075 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001076 return -ENOMEM;
1077
Kay Sievers7eff2e72007-08-14 15:15:12 +02001078 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001079 return -ENOMEM;
1080
Kay Sievers7eff2e72007-08-14 15:15:12 +02001081 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
Kay Sievers312c0042005-11-16 09:00:00 +01001082 "pa%08Xpb%08Xpc%08Xpd%08X",
1083 p_dev->has_manf_id ? p_dev->manf_id : 0,
1084 p_dev->has_card_id ? p_dev->card_id : 0,
1085 p_dev->has_func_id ? p_dev->func_id : 0,
1086 p_dev->func,
1087 p_dev->device_no,
1088 hash[0],
1089 hash[1],
1090 hash[2],
1091 hash[3]))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001092 return -ENOMEM;
1093
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001094 return 0;
1095}
1096
1097#else
1098
Kay Sievers7eff2e72007-08-14 15:15:12 +02001099static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001100{
1101 return -ENODEV;
1102}
1103
1104#endif
1105
Alan Stern3f8df782007-07-12 16:57:22 -04001106/************************ runtime PM support ***************************/
1107
1108static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1109static int pcmcia_dev_resume(struct device *dev);
1110
1111static int runtime_suspend(struct device *dev)
1112{
1113 int rc;
1114
1115 down(&dev->sem);
1116 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1117 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001118 return rc;
1119}
1120
1121static void runtime_resume(struct device *dev)
1122{
1123 int rc;
1124
1125 down(&dev->sem);
1126 rc = pcmcia_dev_resume(dev);
1127 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/************************ per-device sysfs output ***************************/
1131
1132#define pcmcia_device_attr(field, test, format) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001133static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{ \
1135 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1136 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1137}
1138
1139#define pcmcia_device_stringattr(name, field) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001140static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{ \
1142 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1143 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1144}
1145
1146pcmcia_device_attr(func, socket, "0x%02x\n");
1147pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1148pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1149pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1150pcmcia_device_stringattr(prod_id1, prod_id[0]);
1151pcmcia_device_stringattr(prod_id2, prod_id[1]);
1152pcmcia_device_stringattr(prod_id3, prod_id[2]);
1153pcmcia_device_stringattr(prod_id4, prod_id[3]);
1154
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001155
1156static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1157{
1158 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1159
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001160 if (p_dev->suspended)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001161 return sprintf(buf, "off\n");
1162 else
1163 return sprintf(buf, "on\n");
1164}
1165
1166static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1167 const char *buf, size_t count)
1168{
1169 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1170 int ret = 0;
1171
1172 if (!count)
1173 return -EINVAL;
1174
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001175 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
Alan Stern3f8df782007-07-12 16:57:22 -04001176 ret = runtime_suspend(dev);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001177 else if (p_dev->suspended && !strncmp(buf, "on", 2))
Alan Stern3f8df782007-07-12 16:57:22 -04001178 runtime_resume(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001179
1180 return ret ? ret : count;
1181}
1182
1183
Dominik Brodowski37045112005-06-30 02:58:47 -07001184static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001185{
1186 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1187 int i;
1188 u32 hash[4] = { 0, 0, 0, 0};
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001189
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001190 /* calculate hashes */
1191 for (i=0; i<4; i++) {
1192 if (!p_dev->prod_id[i])
1193 continue;
1194 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1195 }
1196 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1197 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1198 p_dev->has_manf_id ? p_dev->manf_id : 0,
1199 p_dev->has_card_id ? p_dev->card_id : 0,
1200 p_dev->has_func_id ? p_dev->func_id : 0,
1201 p_dev->func, p_dev->device_no,
1202 hash[0], hash[1], hash[2], hash[3]);
1203}
1204
1205static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1206 struct device_attribute *attr, const char *buf, size_t count)
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001207{
1208 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001209 int ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001210
1211 if (!count)
1212 return -EINVAL;
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001213
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001214 mutex_lock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001215 p_dev->allow_func_id_match = 1;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001216 mutex_unlock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001217
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001218 ret = bus_rescan_devices(&pcmcia_bus_type);
1219 if (ret)
1220 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1221 "allowing func_id matches\n");
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001222
1223 return count;
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static struct device_attribute pcmcia_dev_attrs[] = {
1227 __ATTR(function, 0444, func_show, NULL),
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001228 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 __ATTR_RO(func_id),
1230 __ATTR_RO(manf_id),
1231 __ATTR_RO(card_id),
1232 __ATTR_RO(prod_id1),
1233 __ATTR_RO(prod_id2),
1234 __ATTR_RO(prod_id3),
1235 __ATTR_RO(prod_id4),
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001236 __ATTR_RO(modalias),
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001237 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 __ATTR_NULL,
1239};
1240
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001241/* PM support, also needed for reset */
1242
1243static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1244{
1245 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1246 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001247 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001248
David Brownelld6b4fa62008-02-04 22:27:37 -08001249 if (p_dev->suspended)
1250 return 0;
1251
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001252 ds_dev_dbg(2, dev, "suspending\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001253
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001254 if (dev->driver)
1255 p_drv = to_pcmcia_drv(dev->driver);
1256
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001257 if (!p_drv)
1258 goto out;
1259
1260 if (p_drv->suspend) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001261 ret = p_drv->suspend(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001262 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001263 dev_printk(KERN_ERR, dev,
1264 "pcmcia: device %s (driver %s) did "
1265 "not want to go to sleep (%d)\n",
1266 p_dev->devname, p_drv->drv.name, ret);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001267 goto out;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001268 }
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001269 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001270
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001271 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001272 ds_dev_dbg(2, dev, "releasing configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001273 pcmcia_release_configuration(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001274 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001275
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001276 out:
1277 if (!ret)
1278 p_dev->suspended = 1;
1279 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001280}
1281
1282
1283static int pcmcia_dev_resume(struct device * dev)
1284{
1285 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1286 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001287 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001288
David Brownelld6b4fa62008-02-04 22:27:37 -08001289 if (!p_dev->suspended)
1290 return 0;
1291
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001292 ds_dev_dbg(2, dev, "resuming\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001293
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001294 if (dev->driver)
1295 p_drv = to_pcmcia_drv(dev->driver);
1296
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001297 if (!p_drv)
1298 goto out;
1299
1300 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001301 ds_dev_dbg(2, dev, "requesting configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001302 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1303 if (ret)
1304 goto out;
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001305 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001306
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001307 if (p_drv->resume)
1308 ret = p_drv->resume(p_dev);
1309
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001310 out:
1311 if (!ret)
1312 p_dev->suspended = 0;
1313 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001314}
1315
1316
1317static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1318{
1319 struct pcmcia_socket *skt = _data;
1320 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1321
Alan Stern3f8df782007-07-12 16:57:22 -04001322 if (p_dev->socket != skt || p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001323 return 0;
1324
Alan Stern3f8df782007-07-12 16:57:22 -04001325 return runtime_suspend(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001326}
1327
1328static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1329{
1330 struct pcmcia_socket *skt = _data;
1331 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1332
Alan Stern3f8df782007-07-12 16:57:22 -04001333 if (p_dev->socket != skt || !p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001334 return 0;
1335
Alan Stern3f8df782007-07-12 16:57:22 -04001336 runtime_resume(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001337
1338 return 0;
1339}
1340
1341static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1342{
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001343 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001344 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1345 return 0;
1346}
1347
1348static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1349{
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001350 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001351 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1352 pcmcia_bus_suspend_callback)) {
1353 pcmcia_bus_resume(skt);
1354 return -EIO;
1355 }
1356 return 0;
1357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360/*======================================================================
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 The card status event handler.
1363
1364======================================================================*/
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/* Normally, the event is passed to individual drivers after
1367 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1368 * is inversed to maintain historic compatibility.
1369 */
1370
1371static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1372{
Dominik Brodowskidc109492005-06-27 16:28:50 -07001373 struct pcmcia_socket *s = pcmcia_get_socket(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Florin Malita16174062006-05-24 21:21:31 -04001375 if (!s) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001376 dev_printk(KERN_ERR, &skt->dev,
1377 "PCMCIA obtaining reference to socket " \
1378 "failed, event 0x%x lost!\n", event);
Florin Malita16174062006-05-24 21:21:31 -04001379 return -ENODEV;
1380 }
1381
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001382 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1383 event, priority, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001385 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 case CS_EVENT_CARD_REMOVAL:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001387 s->pcmcia_state.present = 0;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +01001388 pcmcia_card_remove(skt, NULL);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001389 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 case CS_EVENT_CARD_INSERTION:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001393 s->pcmcia_state.present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 pcmcia_card_add(skt);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001395 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397
1398 case CS_EVENT_EJECTION_REQUEST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
1400
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001401 case CS_EVENT_PM_SUSPEND:
1402 case CS_EVENT_PM_RESUME:
1403 case CS_EVENT_RESET_PHYSICAL:
1404 case CS_EVENT_CARD_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 default:
Dominik Brodowskidc109492005-06-27 16:28:50 -07001406 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408 }
1409
Dominik Brodowskidc109492005-06-27 16:28:50 -07001410 pcmcia_put_socket(s);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
1413} /* ds_event */
1414
1415
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001416struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1417{
1418 struct pcmcia_device *p_dev;
1419 struct pcmcia_device *ret = NULL;
1420
1421 p_dev = pcmcia_get_dev(_p_dev);
1422 if (!p_dev)
1423 return NULL;
1424
1425 if (!p_dev->socket->pcmcia_state.present)
1426 goto out;
1427
1428 if (p_dev->_removed)
1429 goto out;
1430
1431 if (p_dev->suspended)
1432 goto out;
1433
1434 ret = p_dev;
1435 out:
1436 pcmcia_put_dev(p_dev);
1437 return ret;
1438}
1439EXPORT_SYMBOL(pcmcia_dev_present);
1440
1441
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001442static struct pcmcia_callback pcmcia_bus_callback = {
1443 .owner = THIS_MODULE,
1444 .event = ds_event,
1445 .requery = pcmcia_bus_rescan,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001446 .suspend = pcmcia_bus_suspend,
1447 .resume = pcmcia_bus_resume,
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001448};
1449
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001450static int __devinit pcmcia_bus_add_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001451 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001453 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int ret;
1455
Dominik Brodowskidc109492005-06-27 16:28:50 -07001456 socket = pcmcia_get_socket(socket);
1457 if (!socket) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001458 dev_printk(KERN_ERR, dev,
1459 "PCMCIA obtaining reference to socket failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -ENODEV;
1461 }
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
1464 * Ugly. But we want to wait for the socket threads to have started up.
1465 * We really should let the drivers themselves drive some of this..
1466 */
1467 msleep(250);
1468
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001469#ifdef CONFIG_PCMCIA_IOCTL
Dominik Brodowskidc109492005-06-27 16:28:50 -07001470 init_waitqueue_head(&socket->queue);
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001471#endif
Dominik Brodowskidc109492005-06-27 16:28:50 -07001472 INIT_LIST_HEAD(&socket->devices_list);
David Howells4796b712006-12-06 15:01:18 +00001473 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001474 memset(&socket->pcmcia_state, 0, sizeof(u8));
1475 socket->device_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001477 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001479 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -07001480 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return (ret);
1482 }
1483
1484 return 0;
1485}
1486
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001487static void pcmcia_bus_remove_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001488 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001490 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Dominik Brodowskidc109492005-06-27 16:28:50 -07001492 if (!socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return;
1494
Dominik Brodowskidc109492005-06-27 16:28:50 -07001495 socket->pcmcia_state.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 pccard_register_pcmcia(socket, NULL);
1497
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001498 /* unregister any unbound devices */
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001499 mutex_lock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001500 pcmcia_card_remove(socket, NULL);
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001501 mutex_unlock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001502
Dominik Brodowskidc109492005-06-27 16:28:50 -07001503 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 return;
1506}
1507
1508
1509/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
Sam Ravnborged49f5d2008-05-01 04:34:50 -07001510static struct class_interface pcmcia_bus_interface __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .class = &pcmcia_socket_class,
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001512 .add_dev = &pcmcia_bus_add_socket,
1513 .remove_dev = &pcmcia_bus_remove_socket,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514};
1515
1516
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001517struct bus_type pcmcia_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .name = "pcmcia",
Kay Sievers312c0042005-11-16 09:00:00 +01001519 .uevent = pcmcia_bus_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 .match = pcmcia_bus_match,
1521 .dev_attrs = pcmcia_dev_attrs,
Russell King1d0baa32006-01-05 14:40:58 +00001522 .probe = pcmcia_device_probe,
1523 .remove = pcmcia_device_remove,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001524 .suspend = pcmcia_dev_suspend,
1525 .resume = pcmcia_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528
1529static int __init init_pcmcia_bus(void)
1530{
Randy Dunlapace7d472006-10-20 14:44:12 -07001531 int ret;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 spin_lock_init(&pcmcia_dev_list_lock);
1534
Randy Dunlapace7d472006-10-20 14:44:12 -07001535 ret = bus_register(&pcmcia_bus_type);
1536 if (ret < 0) {
1537 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1538 return ret;
1539 }
1540 ret = class_interface_register(&pcmcia_bus_interface);
1541 if (ret < 0) {
1542 printk(KERN_WARNING
1543 "pcmcia: class_interface_register error: %d\n", ret);
1544 bus_unregister(&pcmcia_bus_type);
1545 return ret;
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001548 pcmcia_setup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 return 0;
1551}
1552fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1553 * pcmcia_socket_class is already registered */
1554
1555
1556static void __exit exit_pcmcia_bus(void)
1557{
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001558 pcmcia_cleanup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001560 class_interface_unregister(&pcmcia_bus_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 bus_unregister(&pcmcia_bus_type);
1563}
1564module_exit(exit_pcmcia_bus);
1565
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567MODULE_ALIAS("ds");