blob: 7956602554901297f86055cea142707f7eead769 [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
Dominik Brodowski7d16b652008-08-02 21:02:01 +020044#ifdef CONFIG_PCMCIA_DEBUG
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070045int ds_pc_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47module_param_named(pc_debug, ds_pc_debug, int, 0644);
48
49#define ds_dbg(lvl, fmt, arg...) do { \
Dominik Brodowskiac449d62008-08-02 18:33:56 +020050 if (ds_pc_debug > (lvl)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 printk(KERN_DEBUG "ds: " fmt , ## arg); \
52} while (0)
Dominik Brodowskiac449d62008-08-02 18:33:56 +020053#define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
54 if (ds_pc_debug > (lvl)) \
55 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
56} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define ds_dbg(lvl, fmt, arg...) do { } while (0)
Dominik Brodowskiac449d62008-08-02 18:33:56 +020059#define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Dominik Brodowskie7a480d2005-06-27 16:28:47 -070062spinlock_t pcmcia_dev_list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*====================================================================*/
65
66/* code which was in cs.c before */
67
68/* String tables for error messages */
69
70typedef struct lookup_t {
Dominik Brodowski79ba6752008-08-03 14:03:07 +020071 const int key;
72 const char *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073} lookup_t;
74
75static const lookup_t error_table[] = {
Dominik Brodowski610e2372008-08-03 11:58:53 +020076 { 0, "Operation succeeded" },
Dominik Brodowskid8b0a492008-08-03 10:56:13 +020077 { -EIO, "Input/Output error" },
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +020078 { -ENODEV, "No card present" },
Dominik Brodowskide6405e2008-08-03 10:47:59 +020079 { -EINVAL, "Bad parameter" },
Dominik Brodowski943f70f2008-08-03 11:23:42 +020080 { -EACCES, "Configuration locked" },
Dominik Brodowski610e2372008-08-03 11:58:53 +020081 { -EBUSY, "Resource in use" },
Dominik Brodowski635d19b2008-08-03 11:47:29 +020082 { -ENOSPC, "No more items" },
Dominik Brodowski610e2372008-08-03 11:58:53 +020083 { -ENOMEM, "Out of resource" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86
87static const lookup_t service_table[] = {
88 { AccessConfigurationRegister, "AccessConfigurationRegister" },
89 { AddSocketServices, "AddSocketServices" },
90 { AdjustResourceInfo, "AdjustResourceInfo" },
91 { CheckEraseQueue, "CheckEraseQueue" },
92 { CloseMemory, "CloseMemory" },
93 { DeregisterClient, "DeregisterClient" },
94 { DeregisterEraseQueue, "DeregisterEraseQueue" },
95 { GetCardServicesInfo, "GetCardServicesInfo" },
96 { GetClientInfo, "GetClientInfo" },
97 { GetConfigurationInfo, "GetConfigurationInfo" },
98 { GetEventMask, "GetEventMask" },
99 { GetFirstClient, "GetFirstClient" },
100 { GetFirstRegion, "GetFirstRegion" },
101 { GetFirstTuple, "GetFirstTuple" },
102 { GetNextClient, "GetNextClient" },
103 { GetNextRegion, "GetNextRegion" },
104 { GetNextTuple, "GetNextTuple" },
105 { GetStatus, "GetStatus" },
106 { GetTupleData, "GetTupleData" },
107 { MapMemPage, "MapMemPage" },
108 { ModifyConfiguration, "ModifyConfiguration" },
109 { ModifyWindow, "ModifyWindow" },
110 { OpenMemory, "OpenMemory" },
111 { ParseTuple, "ParseTuple" },
112 { ReadMemory, "ReadMemory" },
113 { RegisterClient, "RegisterClient" },
114 { RegisterEraseQueue, "RegisterEraseQueue" },
115 { RegisterMTD, "RegisterMTD" },
116 { ReleaseConfiguration, "ReleaseConfiguration" },
117 { ReleaseIO, "ReleaseIO" },
118 { ReleaseIRQ, "ReleaseIRQ" },
119 { ReleaseWindow, "ReleaseWindow" },
120 { RequestConfiguration, "RequestConfiguration" },
121 { RequestIO, "RequestIO" },
122 { RequestIRQ, "RequestIRQ" },
123 { RequestSocketMask, "RequestSocketMask" },
124 { RequestWindow, "RequestWindow" },
125 { ResetCard, "ResetCard" },
126 { SetEventMask, "SetEventMask" },
127 { ValidateCIS, "ValidateCIS" },
128 { WriteMemory, "WriteMemory" },
129 { BindDevice, "BindDevice" },
130 { BindMTD, "BindMTD" },
131 { ReportError, "ReportError" },
132 { SuspendCard, "SuspendCard" },
133 { ResumeCard, "ResumeCard" },
134 { EjectCard, "EjectCard" },
135 { InsertCard, "InsertCard" },
136 { ReplaceCIS, "ReplaceCIS" }
137};
138
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200139const char *pcmcia_error_func(int func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 for (i = 0; i < ARRAY_SIZE(service_table); i++)
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200144 if (service_table[i].key == func)
145 return service_table[i].msg;
146
147 return "Unknown service number";
148}
149EXPORT_SYMBOL(pcmcia_error_func);
150
151const char *pcmcia_error_ret(int ret)
152{
153 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 for (i = 0; i < ARRAY_SIZE(error_table); i++)
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200156 if (error_table[i].key == ret)
157 return error_table[i].msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dominik Brodowski79ba6752008-08-03 14:03:07 +0200159 return "unknown";
160}
161EXPORT_SYMBOL(pcmcia_error_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/*======================================================================*/
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700166
167static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
168{
169 struct pcmcia_device_id *did = p_drv->id_table;
170 unsigned int i;
171 u32 hash;
172
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100173 if (!p_drv->probe || !p_drv->remove)
Pavel Roskinba5bb6b2005-07-28 01:07:20 -0700174 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
175 "function\n", p_drv->drv.name);
Dominik Brodowski1e212f32005-07-07 17:59:00 -0700176
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700177 while (did && did->match_flags) {
178 for (i=0; i<4; i++) {
179 if (!did->prod_id[i])
180 continue;
181
182 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
183 if (hash == did->prod_id_hash[i])
184 continue;
185
186 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
187 "product string \"%s\": is 0x%x, should "
188 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
189 did->prod_id_hash[i], hash);
Dominik Brodowski5085cb22005-06-27 16:28:45 -0700190 printk(KERN_DEBUG "pcmcia: see "
191 "Documentation/pcmcia/devicetable.txt for "
192 "details\n");
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700193 }
194 did++;
195 }
196
197 return;
198}
199
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*======================================================================*/
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Bernhard Walle6179b552007-05-06 14:48:44 -0700204struct pcmcia_dynid {
205 struct list_head node;
206 struct pcmcia_device_id id;
207};
208
209/**
210 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
211 * @driver: target device driver
212 * @buf: buffer for scanning device ID data
213 * @count: input size
214 *
215 * Adds a new dynamic PCMCIA device ID to this driver,
216 * and causes the driver to probe for all devices again.
217 */
218static ssize_t
219pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
220{
221 struct pcmcia_dynid *dynid;
222 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
223 __u16 match_flags, manf_id, card_id;
224 __u8 func_id, function, device_no;
225 __u32 prod_id_hash[4] = {0, 0, 0, 0};
226 int fields=0;
227 int retval = 0;
228
229 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
230 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
231 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
232 if (fields < 6)
233 return -EINVAL;
234
235 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
236 if (!dynid)
237 return -ENOMEM;
238
239 INIT_LIST_HEAD(&dynid->node);
240 dynid->id.match_flags = match_flags;
241 dynid->id.manf_id = manf_id;
242 dynid->id.card_id = card_id;
243 dynid->id.func_id = func_id;
244 dynid->id.function = function;
245 dynid->id.device_no = device_no;
246 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
247
248 spin_lock(&pdrv->dynids.lock);
249 list_add_tail(&pdrv->dynids.list, &dynid->node);
250 spin_unlock(&pdrv->dynids.lock);
251
252 if (get_driver(&pdrv->drv)) {
253 retval = driver_attach(&pdrv->drv);
254 put_driver(&pdrv->drv);
255 }
256
257 if (retval)
258 return retval;
259 return count;
260}
261static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
262
263static void
264pcmcia_free_dynids(struct pcmcia_driver *drv)
265{
266 struct pcmcia_dynid *dynid, *n;
267
268 spin_lock(&drv->dynids.lock);
269 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
270 list_del(&dynid->node);
271 kfree(dynid);
272 }
273 spin_unlock(&drv->dynids.lock);
274}
275
276static int
277pcmcia_create_newid_file(struct pcmcia_driver *drv)
278{
279 int error = 0;
280 if (drv->probe != NULL)
Greg Kroah-Hartman2344c6d2007-11-28 12:23:18 -0800281 error = driver_create_file(&drv->drv, &driver_attr_new_id);
Bernhard Walle6179b552007-05-06 14:48:44 -0700282 return error;
283}
284
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/**
287 * pcmcia_register_driver - register a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800288 * @driver: the &driver being registered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *
290 * Registers a PCMCIA driver with the PCMCIA bus core.
291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292int pcmcia_register_driver(struct pcmcia_driver *driver)
293{
Bernhard Walle6179b552007-05-06 14:48:44 -0700294 int error;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!driver)
297 return -EINVAL;
298
Dominik Brodowski23a83bf2005-06-27 16:28:07 -0700299 pcmcia_check_driver(driver);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* initialize common fields */
302 driver->drv.bus = &pcmcia_bus_type;
303 driver->drv.owner = driver->owner;
Bernhard Walle6179b552007-05-06 14:48:44 -0700304 spin_lock_init(&driver->dynids.lock);
305 INIT_LIST_HEAD(&driver->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200307 ds_dbg(3, "registering driver %s\n", driver->drv.name);
308
Bernhard Walle6179b552007-05-06 14:48:44 -0700309 error = driver_register(&driver->drv);
310 if (error < 0)
311 return error;
312
313 error = pcmcia_create_newid_file(driver);
314 if (error)
315 driver_unregister(&driver->drv);
316
317 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319EXPORT_SYMBOL(pcmcia_register_driver);
320
321/**
322 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
Randy Dunlap78187862007-12-10 15:49:22 -0800323 * @driver: the &driver being unregistered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
325void pcmcia_unregister_driver(struct pcmcia_driver *driver)
326{
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200327 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 driver_unregister(&driver->drv);
Bernhard Walle6179b552007-05-06 14:48:44 -0700329 pcmcia_free_dynids(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331EXPORT_SYMBOL(pcmcia_unregister_driver);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/* pcmcia_device handling */
335
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700336struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct device *tmp_dev;
339 tmp_dev = get_device(&p_dev->dev);
340 if (!tmp_dev)
341 return NULL;
342 return to_pcmcia_dev(tmp_dev);
343}
344
Dominik Brodowskie7a480d2005-06-27 16:28:47 -0700345void pcmcia_put_dev(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 if (p_dev)
348 put_device(&p_dev->dev);
349}
350
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100351static void pcmcia_release_function(struct kref *ref)
352{
353 struct config_t *c = container_of(ref, struct config_t, ref);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200354 ds_dbg(1, "releasing config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100355 kfree(c);
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static void pcmcia_release_dev(struct device *dev)
359{
360 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200361 ds_dev_dbg(1, dev, "releasing device\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -0700362 pcmcia_put_socket(p_dev->socket);
Brice Goglinbd65a682005-09-09 13:03:29 -0700363 kfree(p_dev->devname);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100364 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kfree(p_dev);
366}
367
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500368static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100369{
370 if (!s->pcmcia_state.device_add_pending) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200371 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200372 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100373 s->pcmcia_state.device_add_pending = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500374 s->pcmcia_state.mfc_pfc = mfc;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100375 schedule_work(&s->device_add);
376 }
377 return;
378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380static int pcmcia_device_probe(struct device * dev)
381{
382 struct pcmcia_device *p_dev;
383 struct pcmcia_driver *p_drv;
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100384 struct pcmcia_device_id *did;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100385 struct pcmcia_socket *s;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400386 cistpl_config_t cis_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int ret = 0;
388
389 dev = get_device(dev);
390 if (!dev)
391 return -ENODEV;
392
393 p_dev = to_pcmcia_dev(dev);
394 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100395 s = p_dev->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Alan Coxcec5eb72008-09-22 15:58:14 +0100397 /* The PCMCIA code passes the match data in via dev->driver_data
398 * which is an ugly hack. Once the driver probe is called it may
399 * and often will overwrite the match data so we must save it first
400 *
401 * handle pseudo multifunction devices:
402 * there are at most two pseudo multifunction devices.
403 * if we're matching against the first, schedule a
404 * call which will then check whether there are two
405 * pseudo devices, and if not, add the second one.
406 */
407 did = p_dev->dev.driver_data;
408
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200409 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200410
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100411 if ((!p_drv->probe) || (!p_dev->function_config) ||
412 (!try_module_get(p_drv->owner))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret = -EINVAL;
414 goto put_dev;
415 }
416
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400417 /* set up some more device information */
418 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
419 &cis_config);
420 if (!ret) {
421 p_dev->conf.ConfigBase = cis_config.base;
422 p_dev->conf.Present = cis_config.rmask[0];
423 } else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200424 dev_printk(KERN_INFO, dev,
425 "pcmcia: could not parse base and rmask0 of CIS\n");
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400426 p_dev->conf.ConfigBase = 0;
427 p_dev->conf.Present = 0;
428 }
429
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100430 ret = p_drv->probe(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200431 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200432 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
433 p_drv->drv.name, ret);
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100434 goto put_module;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200435 }
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100436
Hugh Dickins4ceadbf2006-03-10 21:13:44 +0000437 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowski82d56e62006-01-27 19:15:02 +0100438 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500439 pcmcia_add_device_later(p_dev->socket, 0);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100440
Alan Coxcec5eb72008-09-22 15:58:14 +0100441put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (ret)
443 module_put(p_drv->owner);
Alan Coxcec5eb72008-09-22 15:58:14 +0100444put_dev:
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100445 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 put_device(dev);
447 return (ret);
448}
449
450
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100451/*
452 * Removes a PCMCIA card from the device tree and socket list.
453 */
454static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
455{
456 struct pcmcia_device *p_dev;
457 struct pcmcia_device *tmp;
458 unsigned long flags;
459
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200460 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
461 "pcmcia_card_remove(%d) %s\n", s->sock,
462 leftover ? leftover->devname : "");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100463
464 if (!leftover)
465 s->device_count = 0;
466 else
467 s->device_count = 1;
468
469 /* unregister all pcmcia_devices registered with this socket, except leftover */
470 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
471 if (p_dev == leftover)
472 continue;
473
474 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
475 list_del(&p_dev->socket_device_list);
476 p_dev->_removed=1;
477 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
478
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200479 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100480 device_unregister(&p_dev->dev);
481 }
482
483 return;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int pcmcia_device_remove(struct device * dev)
487{
488 struct pcmcia_device *p_dev;
489 struct pcmcia_driver *p_drv;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100490 struct pcmcia_device_id *did;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100491 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 p_dev = to_pcmcia_dev(dev);
494 p_drv = to_pcmcia_drv(dev->driver);
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100495
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200496 ds_dev_dbg(1, dev, "removing device\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200497
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100498 /* If we're removing the primary module driving a
499 * pseudo multi-function card, we need to unbind
500 * all devices
501 */
Andrew Morton80a55e92006-04-10 23:24:57 -0700502 did = p_dev->dev.driver_data;
Hugh Dickinsb2f51a12006-03-10 21:15:19 +0000503 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +0100504 (p_dev->socket->device_count != 0) &&
505 (p_dev->device_no == 0))
506 pcmcia_card_remove(p_dev->socket, p_dev);
507
508 /* detach the "instance" */
Dominik Brodowskif3990712005-11-14 21:25:23 +0100509 if (!p_drv)
510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Dominik Brodowskif3990712005-11-14 21:25:23 +0100512 if (p_drv->remove)
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100513 p_drv->remove(p_dev);
514
Dominik Brodowskia0aab142006-04-04 11:09:26 +0200515 p_dev->dev_node = NULL;
516
Dominik Brodowskif3990712005-11-14 21:25:23 +0100517 /* check for proper unloading */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100518 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200519 dev_printk(KERN_INFO, dev,
520 "pcmcia: driver %s did not release config properly\n",
521 p_drv->drv.name);
Dominik Brodowskif3990712005-11-14 21:25:23 +0100522
523 for (i = 0; i < MAX_WIN; i++)
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100524 if (p_dev->_win & CLIENT_WIN_REQ(i))
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200525 dev_printk(KERN_INFO, dev,
526 "pcmcia: driver %s did not release window properly\n",
527 p_drv->drv.name);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100528
Dominik Brodowskif3990712005-11-14 21:25:23 +0100529 /* references from pcmcia_probe_device */
530 pcmcia_put_dev(p_dev);
531 module_put(p_drv->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 return 0;
534}
535
536
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * pcmcia_device_query -- determine information about a pcmcia device
539 */
540static int pcmcia_device_query(struct pcmcia_device *p_dev)
541{
542 cistpl_manfid_t manf_id;
543 cistpl_funcid_t func_id;
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700544 cistpl_vers_1_t *vers1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned int i;
546
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700547 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
548 if (!vers1)
549 return -ENOMEM;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
552 CISTPL_MANFID, &manf_id)) {
553 p_dev->manf_id = manf_id.manf;
554 p_dev->card_id = manf_id.card;
555 p_dev->has_manf_id = 1;
556 p_dev->has_card_id = 1;
557 }
558
559 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
560 CISTPL_FUNCID, &func_id)) {
561 p_dev->func_id = func_id.func;
562 p_dev->has_func_id = 1;
563 } else {
564 /* rule of thumb: cards with no FUNCID, but with
565 * common memory device geometry information, are
566 * probably memory cards (from pcmcia-cs) */
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700567 cistpl_device_geo_t *devgeo;
568
569 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
570 if (!devgeo) {
571 kfree(vers1);
572 return -ENOMEM;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700575 CISTPL_DEVICE_GEO, devgeo)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200576 ds_dev_dbg(0, &p_dev->dev,
577 "mem device geometry probably means "
578 "FUNCID_MEMORY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 p_dev->func_id = CISTPL_FUNCID_MEMORY;
580 p_dev->has_func_id = 1;
581 }
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700582 kfree(devgeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700586 vers1)) {
587 for (i=0; i < vers1->ns; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 char *tmp;
589 unsigned int length;
590
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700591 tmp = vers1->str + vers1->ofs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 length = strlen(tmp) + 1;
Janos Farkas6e3d4f22006-03-31 01:04:57 +0200594 if ((length < 2) || (length > 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 continue;
596
597 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
598 GFP_KERNEL);
599 if (!p_dev->prod_id[i])
600 continue;
601
602 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
603 tmp, length);
604 }
605 }
606
Ingo Molnar76fa82f2005-09-09 13:03:21 -0700607 kfree(vers1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609}
610
611
612/* device_add_lock is needed to avoid double registration by cardmgr and kernel.
613 * Serializes pcmcia_device_add; will most likely be removed in future.
614 *
615 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
616 * won't work, this doesn't matter much at the moment: the driver core doesn't
617 * support it either.
618 */
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100619static DEFINE_MUTEX(device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dominik Brodowskidc109492005-06-27 16:28:50 -0700621struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100623 struct pcmcia_device *p_dev, *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unsigned long flags;
Brice Goglinbd65a682005-09-09 13:03:29 -0700625 int bus_id_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Dominik Brodowskidc109492005-06-27 16:28:50 -0700627 s = pcmcia_get_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!s)
629 return NULL;
630
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100631 mutex_lock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200633 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
634
Dominik Brodowski6cf5be52006-11-27 21:03:48 -0500635 /* max of 4 devices per card */
636 if (s->device_count == 4)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700637 goto err_put;
638
Dominik Brodowski8084b372005-12-11 21:18:26 +0100639 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!p_dev)
641 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dominik Brodowskidc109492005-06-27 16:28:50 -0700643 p_dev->socket = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 p_dev->device_no = (s->device_count++);
645 p_dev->func = function;
646
647 p_dev->dev.bus = &pcmcia_bus_type;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200648 p_dev->dev.parent = s->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 p_dev->dev.release = pcmcia_release_dev;
James Bottomley43d9f7f2007-10-16 01:23:58 -0700650 /* by default don't allow DMA */
651 p_dev->dma_mask = DMA_MASK_NONE;
652 p_dev->dev.dma_mask = &p_dev->dma_mask;
Brice Goglinbd65a682005-09-09 13:03:29 -0700653 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
654
655 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
656 if (!p_dev->devname)
657 goto err_free;
658 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200659 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100662
663 /*
664 * p_dev->function_config must be the same for all card functions.
665 * Note that this is serialized by the device_add_lock, so that
666 * only one such struct will be created.
667 */
668 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
669 if (p_dev->func == tmp_dev->func) {
670 p_dev->function_config = tmp_dev->function_config;
671 kref_get(&p_dev->function_config->ref);
672 }
673
674 /* Add to the list in pcmcia_bus_socket */
Komuro6171b882006-04-02 17:39:27 +0900675 list_add(&p_dev->socket_device_list, &s->devices_list);
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
678
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100679 if (!p_dev->function_config) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200680 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100681 p_dev->function_config = kzalloc(sizeof(struct config_t),
682 GFP_KERNEL);
683 if (!p_dev->function_config)
684 goto err_unreg;
685 kref_init(&p_dev->function_config->ref);
686 }
687
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200688 dev_printk(KERN_NOTICE, &p_dev->dev,
689 "pcmcia: registering new device %s\n",
690 p_dev->devname);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100691
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700692 pcmcia_device_query(p_dev);
693
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100694 if (device_register(&p_dev->dev))
695 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100697 mutex_unlock(&device_add_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return p_dev;
700
Dominik Brodowski360b65b2006-01-10 20:50:39 +0100701 err_unreg:
702 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
703 list_del(&p_dev->socket_device_list);
704 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 err_free:
Brice Goglinbd65a682005-09-09 13:03:29 -0700707 kfree(p_dev->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 kfree(p_dev);
709 s->device_count--;
710 err_put:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100711 mutex_unlock(&device_add_lock);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700712 pcmcia_put_socket(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 return NULL;
715}
716
717
718static int pcmcia_card_add(struct pcmcia_socket *s)
719{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 cistpl_longlink_mfc_t mfc;
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200721 unsigned int no_funcs, i, no_chains;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int ret = 0;
723
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200724 if (!(s->resource_setup_done)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200725 ds_dev_dbg(3, &s->dev,
726 "no resources available, delaying card_add\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200730 if (pcmcia_validate_mem(s)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200731 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200732 "delaying card_add\n");
Dominik Brodowskide759142005-09-28 19:41:56 +0200733 return -EAGAIN; /* try again, but later... */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200734 }
Dominik Brodowskide759142005-09-28 19:41:56 +0200735
Dominik Brodowskic5081d52008-06-19 20:12:34 +0200736 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
737 if (ret || !no_chains) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200738 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -ENODEV;
740 }
741
742 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
743 no_funcs = mfc.nfn;
744 else
745 no_funcs = 1;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500746 s->functions = no_funcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 for (i=0; i < no_funcs; i++)
Dominik Brodowskidc109492005-06-27 16:28:50 -0700749 pcmcia_device_add(s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 return (ret);
752}
753
754
David Howells4796b712006-12-06 15:01:18 +0000755static void pcmcia_delayed_add_device(struct work_struct *work)
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700756{
David Howellsc4028952006-11-22 14:57:56 +0000757 struct pcmcia_socket *s =
758 container_of(work, struct pcmcia_socket, device_add);
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200759 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500760 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
Dominik Brodowskib5e43912005-06-27 16:28:50 -0700761 s->pcmcia_state.device_add_pending = 0;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500762 s->pcmcia_state.mfc_pfc = 0;
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700763}
764
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700765static int pcmcia_requery(struct device *dev, void * _data)
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700766{
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700767 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200768 if (!p_dev->dev.driver) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200769 ds_dev_dbg(1, dev, "update device information\n");
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700770 pcmcia_device_query(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200771 }
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700772
773 return 0;
774}
775
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500776static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700777{
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500778 int no_devices = 0;
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400779 int ret = 0;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700780 unsigned long flags;
781
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100782 /* must be called with skt_mutex held */
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200783 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200784
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700785 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
Dominik Brodowskidc109492005-06-27 16:28:50 -0700786 if (list_empty(&skt->devices_list))
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500787 no_devices = 1;
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700788 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
789
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500790 /* If this is because of a CIS override, start over */
791 if (new_cis && !no_devices)
792 pcmcia_card_remove(skt, NULL);
793
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700794 /* if no devices were added for this socket yet because of
795 * missing resource information or other trouble, we need to
796 * do this now. */
Dominik Brodowski4ae1cbf2006-11-03 10:54:00 -0500797 if (no_devices || new_cis) {
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400798 ret = pcmcia_card_add(skt);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700799 if (ret)
800 return;
801 }
802
803 /* some device information might have changed because of a CIS
804 * update or because we can finally read it correctly... so
805 * determine it again, overwriting old values if necessary. */
806 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
807
808 /* we re-scan all devices, not just the ones connected to this
809 * socket. This does not matter, though. */
Dominik Brodowskif901b8c2006-10-25 19:56:55 -0400810 ret = bus_rescan_devices(&pcmcia_bus_type);
811 if (ret)
812 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700813}
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700814
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500815#ifdef CONFIG_PCMCIA_LOAD_CIS
816
817/**
818 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
Randy Dunlap78187862007-12-10 15:49:22 -0800819 * @dev: the pcmcia device which needs a CIS override
820 * @filename: requested filename in /lib/firmware/
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500821 *
822 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
823 * the one provided by the card is broken. The firmware files reside in
824 * /lib/firmware/ in userspace.
825 */
826static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
827{
828 struct pcmcia_socket *s = dev->socket;
829 const struct firmware *fw;
Dominik Brodowskifeda4f22008-06-24 15:45:55 +0200830 char path[FIRMWARE_NAME_MAX];
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500831 int ret = -ENOMEM;
832 int no_funcs;
833 int old_funcs;
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500834 cistpl_longlink_mfc_t mfc;
835
836 if (!filename)
837 return -EINVAL;
838
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200839 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500840
Dominik Brodowskifeda4f22008-06-24 15:45:55 +0200841 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200842 dev_printk(KERN_WARNING, &dev->dev,
843 "pcmcia: CIS filename is too long [%s]\n",
844 filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500845 return -EINVAL;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200846 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500847
Randy Dunlap52debb02008-02-04 22:27:38 -0800848 snprintf(path, sizeof(path), "%s", filename);
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500849
850 if (request_firmware(&fw, path, &dev->dev) == 0) {
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200851 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
852 ret = -EINVAL;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200853 dev_printk(KERN_ERR, &dev->dev,
854 "pcmcia: CIS override is too big\n");
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500855 goto release;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200856 }
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500857
Dominik Brodowski53efec92008-07-28 19:44:05 +0200858 if (!pcmcia_replace_cis(s, fw->data, fw->size))
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500859 ret = 0;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200860 else {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200861 dev_printk(KERN_ERR, &dev->dev,
862 "pcmcia: CIS override failed\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +0200863 goto release;
864 }
865
Dominik Brodowski1d2c9042006-11-06 21:52:16 -0500866
867 /* update information */
868 pcmcia_device_query(dev);
869
870 /* does this cis override add or remove functions? */
871 old_funcs = s->functions;
872
873 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
874 no_funcs = mfc.nfn;
875 else
876 no_funcs = 1;
877 s->functions = no_funcs;
878
879 if (old_funcs > no_funcs)
880 pcmcia_card_remove(s, dev);
881 else if (no_funcs > old_funcs)
882 pcmcia_add_device_later(s, 1);
883 }
884 release:
885 release_firmware(fw);
886
887 return (ret);
888}
889
890#else /* !CONFIG_PCMCIA_LOAD_CIS */
891
892static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
893{
894 return -ENODEV;
895}
896
897#endif
898
899
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700900static inline int pcmcia_devmatch(struct pcmcia_device *dev,
901 struct pcmcia_device_id *did)
902{
903 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
904 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
905 return 0;
906 }
907
908 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
909 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
910 return 0;
911 }
912
913 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
914 if (dev->func != did->function)
915 return 0;
916 }
917
918 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
919 if (!dev->prod_id[0])
920 return 0;
921 if (strcmp(did->prod_id[0], dev->prod_id[0]))
922 return 0;
923 }
924
925 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
926 if (!dev->prod_id[1])
927 return 0;
928 if (strcmp(did->prod_id[1], dev->prod_id[1]))
929 return 0;
930 }
931
932 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
933 if (!dev->prod_id[2])
934 return 0;
935 if (strcmp(did->prod_id[2], dev->prod_id[2]))
936 return 0;
937 }
938
939 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
940 if (!dev->prod_id[3])
941 return 0;
942 if (strcmp(did->prod_id[3], dev->prod_id[3]))
943 return 0;
944 }
945
946 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700947 if (dev->device_no != did->device_no)
948 return 0;
949 }
950
951 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
952 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
953 return 0;
954
955 /* if this is a pseudo-multi-function device,
956 * we need explicit matches */
957 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
958 return 0;
959 if (dev->device_no)
960 return 0;
961
962 /* also, FUNC_ID matching needs to be activated by userspace
963 * after it has re-checked that there is no possible module
964 * with a prod_id/manf_id/card_id match.
965 */
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200966 ds_dev_dbg(0, &dev->dev,
967 "skipping FUNC_ID match until userspace interaction\n");
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700968 if (!dev->allow_func_id_match)
969 return 0;
970 }
971
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700972 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200973 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
Dominik Brodowskidaa95172005-06-27 16:28:14 -0700974 if (!dev->socket->fake_cis)
975 pcmcia_load_firmware(dev, did->cisfile);
976
977 if (!dev->socket->fake_cis)
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700978 return 0;
Dominik Brodowskiea7b3882005-06-27 16:28:07 -0700979 }
980
Dominik Brodowskif602ff72005-06-27 16:28:09 -0700981 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
982 int i;
983 for (i=0; i<4; i++)
984 if (dev->prod_id[i])
985 return 0;
986 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
987 return 0;
988 }
989
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700990 dev->dev.driver_data = (void *) did;
991
992 return 1;
993}
994
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
997 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
998 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -0700999 struct pcmcia_device_id *did = p_drv->id_table;
Bernhard Walle6179b552007-05-06 14:48:44 -07001000 struct pcmcia_dynid *dynid;
1001
1002 /* match dynamic devices first */
1003 spin_lock(&p_drv->dynids.lock);
1004 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001005 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -07001006 if (pcmcia_devmatch(p_dev, &dynid->id)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001007 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
Bernhard Walle6179b552007-05-06 14:48:44 -07001008 spin_unlock(&p_drv->dynids.lock);
1009 return 1;
1010 }
1011 }
1012 spin_unlock(&p_drv->dynids.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dominik Brodowski0e0fad82006-01-15 01:14:31 +01001014#ifdef CONFIG_PCMCIA_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* matching by cardmgr */
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001016 if (p_dev->cardmgr == p_drv) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001017 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001019 }
Dominik Brodowski0e0fad82006-01-15 01:14:31 +01001020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001022 while (did && did->match_flags) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001023 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001024 if (pcmcia_devmatch(p_dev, did)) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001025 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001026 return 1;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001027 }
Dominik Brodowski1ad275e2005-06-27 16:28:06 -07001028 did++;
1029 }
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
1032}
1033
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001034#ifdef CONFIG_HOTPLUG
1035
Kay Sievers7eff2e72007-08-14 15:15:12 +02001036static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001037{
1038 struct pcmcia_device *p_dev;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001039 int i;
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001040 u32 hash[4] = { 0, 0, 0, 0};
1041
1042 if (!dev)
1043 return -ENODEV;
1044
1045 p_dev = to_pcmcia_dev(dev);
1046
1047 /* calculate hashes */
1048 for (i=0; i<4; i++) {
1049 if (!p_dev->prod_id[i])
1050 continue;
1051 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1052 }
1053
Kay Sievers7eff2e72007-08-14 15:15:12 +02001054 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001055 return -ENOMEM;
1056
Kay Sievers7eff2e72007-08-14 15:15:12 +02001057 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001058 return -ENOMEM;
1059
Kay Sievers7eff2e72007-08-14 15:15:12 +02001060 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
Kay Sievers312c0042005-11-16 09:00:00 +01001061 "pa%08Xpb%08Xpc%08Xpd%08X",
1062 p_dev->has_manf_id ? p_dev->manf_id : 0,
1063 p_dev->has_card_id ? p_dev->card_id : 0,
1064 p_dev->has_func_id ? p_dev->func_id : 0,
1065 p_dev->func,
1066 p_dev->device_no,
1067 hash[0],
1068 hash[1],
1069 hash[2],
1070 hash[3]))
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001071 return -ENOMEM;
1072
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001073 return 0;
1074}
1075
1076#else
1077
Kay Sievers7eff2e72007-08-14 15:15:12 +02001078static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Dominik Brodowski840c2ac2005-06-27 16:28:04 -07001079{
1080 return -ENODEV;
1081}
1082
1083#endif
1084
Alan Stern3f8df782007-07-12 16:57:22 -04001085/************************ runtime PM support ***************************/
1086
1087static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1088static int pcmcia_dev_resume(struct device *dev);
1089
1090static int runtime_suspend(struct device *dev)
1091{
1092 int rc;
1093
1094 down(&dev->sem);
1095 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1096 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001097 return rc;
1098}
1099
1100static void runtime_resume(struct device *dev)
1101{
1102 int rc;
1103
1104 down(&dev->sem);
1105 rc = pcmcia_dev_resume(dev);
1106 up(&dev->sem);
Alan Stern3f8df782007-07-12 16:57:22 -04001107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/************************ per-device sysfs output ***************************/
1110
1111#define pcmcia_device_attr(field, test, format) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001112static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{ \
1114 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1115 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1116}
1117
1118#define pcmcia_device_stringattr(name, field) \
Yani Ioannoue404e272005-05-17 06:42:58 -04001119static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{ \
1121 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1122 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1123}
1124
1125pcmcia_device_attr(func, socket, "0x%02x\n");
1126pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1127pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1128pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1129pcmcia_device_stringattr(prod_id1, prod_id[0]);
1130pcmcia_device_stringattr(prod_id2, prod_id[1]);
1131pcmcia_device_stringattr(prod_id3, prod_id[2]);
1132pcmcia_device_stringattr(prod_id4, prod_id[3]);
1133
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001134
1135static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1136{
1137 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1138
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001139 if (p_dev->suspended)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001140 return sprintf(buf, "off\n");
1141 else
1142 return sprintf(buf, "on\n");
1143}
1144
1145static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1146 const char *buf, size_t count)
1147{
1148 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1149 int ret = 0;
1150
1151 if (!count)
1152 return -EINVAL;
1153
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001154 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
Alan Stern3f8df782007-07-12 16:57:22 -04001155 ret = runtime_suspend(dev);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001156 else if (p_dev->suspended && !strncmp(buf, "on", 2))
Alan Stern3f8df782007-07-12 16:57:22 -04001157 runtime_resume(dev);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001158
1159 return ret ? ret : count;
1160}
1161
1162
Dominik Brodowski37045112005-06-30 02:58:47 -07001163static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001164{
1165 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1166 int i;
1167 u32 hash[4] = { 0, 0, 0, 0};
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001168
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001169 /* calculate hashes */
1170 for (i=0; i<4; i++) {
1171 if (!p_dev->prod_id[i])
1172 continue;
1173 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1174 }
1175 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1176 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1177 p_dev->has_manf_id ? p_dev->manf_id : 0,
1178 p_dev->has_card_id ? p_dev->card_id : 0,
1179 p_dev->has_func_id ? p_dev->func_id : 0,
1180 p_dev->func, p_dev->device_no,
1181 hash[0], hash[1], hash[2], hash[3]);
1182}
1183
1184static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1185 struct device_attribute *attr, const char *buf, size_t count)
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001186{
1187 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001188 int ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001189
1190 if (!count)
1191 return -EINVAL;
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001192
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001193 mutex_lock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001194 p_dev->allow_func_id_match = 1;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +01001195 mutex_unlock(&p_dev->socket->skt_mutex);
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001196
Dominik Brodowskif901b8c2006-10-25 19:56:55 -04001197 ret = bus_rescan_devices(&pcmcia_bus_type);
1198 if (ret)
1199 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1200 "allowing func_id matches\n");
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001201
1202 return count;
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static struct device_attribute pcmcia_dev_attrs[] = {
1206 __ATTR(function, 0444, func_show, NULL),
Dominik Brodowskidb1019c2006-01-10 19:19:37 +01001207 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 __ATTR_RO(func_id),
1209 __ATTR_RO(manf_id),
1210 __ATTR_RO(card_id),
1211 __ATTR_RO(prod_id1),
1212 __ATTR_RO(prod_id2),
1213 __ATTR_RO(prod_id3),
1214 __ATTR_RO(prod_id4),
Dominik Brodowski3248ff42005-06-27 16:29:01 -07001215 __ATTR_RO(modalias),
Dominik Brodowskia5b55772005-06-27 16:28:10 -07001216 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 __ATTR_NULL,
1218};
1219
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001220/* PM support, also needed for reset */
1221
1222static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1223{
1224 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1225 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001226 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001227
David Brownelld6b4fa62008-02-04 22:27:37 -08001228 if (p_dev->suspended)
1229 return 0;
1230
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001231 ds_dev_dbg(2, dev, "suspending\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001232
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001233 if (dev->driver)
1234 p_drv = to_pcmcia_drv(dev->driver);
1235
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001236 if (!p_drv)
1237 goto out;
1238
1239 if (p_drv->suspend) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001240 ret = p_drv->suspend(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001241 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001242 dev_printk(KERN_ERR, dev,
1243 "pcmcia: device %s (driver %s) did "
1244 "not want to go to sleep (%d)\n",
1245 p_dev->devname, p_drv->drv.name, ret);
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001246 goto out;
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001247 }
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001248 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001249
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001250 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001251 ds_dev_dbg(2, dev, "releasing configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001252 pcmcia_release_configuration(p_dev);
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001253 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001254
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001255 out:
1256 if (!ret)
1257 p_dev->suspended = 1;
1258 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001259}
1260
1261
1262static int pcmcia_dev_resume(struct device * dev)
1263{
1264 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1265 struct pcmcia_driver *p_drv = NULL;
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001266 int ret = 0;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001267
David Brownelld6b4fa62008-02-04 22:27:37 -08001268 if (!p_dev->suspended)
1269 return 0;
1270
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001271 ds_dev_dbg(2, dev, "resuming\n");
Dominik Brodowskid9d9ea02006-12-02 22:11:44 +02001272
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001273 if (dev->driver)
1274 p_drv = to_pcmcia_drv(dev->driver);
1275
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001276 if (!p_drv)
1277 goto out;
1278
1279 if (p_dev->device_no == p_dev->func) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001280 ds_dev_dbg(2, dev, "requesting configuration\n");
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001281 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1282 if (ret)
1283 goto out;
Dominik Brodowski8661bb52006-03-02 00:02:33 +01001284 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001285
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001286 if (p_drv->resume)
1287 ret = p_drv->resume(p_dev);
1288
Dominik Brodowskif6fbe012006-01-20 15:10:23 +01001289 out:
1290 if (!ret)
1291 p_dev->suspended = 0;
1292 return ret;
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001293}
1294
1295
1296static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1297{
1298 struct pcmcia_socket *skt = _data;
1299 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1300
Alan Stern3f8df782007-07-12 16:57:22 -04001301 if (p_dev->socket != skt || p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001302 return 0;
1303
Alan Stern3f8df782007-07-12 16:57:22 -04001304 return runtime_suspend(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001305}
1306
1307static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1308{
1309 struct pcmcia_socket *skt = _data;
1310 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1311
Alan Stern3f8df782007-07-12 16:57:22 -04001312 if (p_dev->socket != skt || !p_dev->suspended)
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001313 return 0;
1314
Alan Stern3f8df782007-07-12 16:57:22 -04001315 runtime_resume(dev);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001316
1317 return 0;
1318}
1319
1320static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1321{
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001322 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001323 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1324 return 0;
1325}
1326
1327static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1328{
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001329 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001330 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1331 pcmcia_bus_suspend_callback)) {
1332 pcmcia_bus_resume(skt);
1333 return -EIO;
1334 }
1335 return 0;
1336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/*======================================================================
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 The card status event handler.
1342
1343======================================================================*/
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345/* Normally, the event is passed to individual drivers after
1346 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1347 * is inversed to maintain historic compatibility.
1348 */
1349
1350static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1351{
Dominik Brodowskidc109492005-06-27 16:28:50 -07001352 struct pcmcia_socket *s = pcmcia_get_socket(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Florin Malita16174062006-05-24 21:21:31 -04001354 if (!s) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001355 dev_printk(KERN_ERR, &skt->dev,
1356 "PCMCIA obtaining reference to socket " \
1357 "failed, event 0x%x lost!\n", event);
Florin Malita16174062006-05-24 21:21:31 -04001358 return -ENODEV;
1359 }
1360
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001361 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1362 event, priority, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001364 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case CS_EVENT_CARD_REMOVAL:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001366 s->pcmcia_state.present = 0;
Dominik Brodowskid6ff5a82006-02-05 09:51:34 +01001367 pcmcia_card_remove(skt, NULL);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001368 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 case CS_EVENT_CARD_INSERTION:
Dominik Brodowskib5e43912005-06-27 16:28:50 -07001372 s->pcmcia_state.present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 pcmcia_card_add(skt);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001374 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
1376
1377 case CS_EVENT_EJECTION_REQUEST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
1379
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001380 case CS_EVENT_PM_SUSPEND:
1381 case CS_EVENT_PM_RESUME:
1382 case CS_EVENT_RESET_PHYSICAL:
1383 case CS_EVENT_CARD_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 default:
Dominik Brodowskidc109492005-06-27 16:28:50 -07001385 handle_event(skt, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387 }
1388
Dominik Brodowskidc109492005-06-27 16:28:50 -07001389 pcmcia_put_socket(s);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return 0;
1392} /* ds_event */
1393
1394
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001395struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1396{
1397 struct pcmcia_device *p_dev;
1398 struct pcmcia_device *ret = NULL;
1399
1400 p_dev = pcmcia_get_dev(_p_dev);
1401 if (!p_dev)
1402 return NULL;
1403
1404 if (!p_dev->socket->pcmcia_state.present)
1405 goto out;
1406
1407 if (p_dev->_removed)
1408 goto out;
1409
1410 if (p_dev->suspended)
1411 goto out;
1412
1413 ret = p_dev;
1414 out:
1415 pcmcia_put_dev(p_dev);
1416 return ret;
1417}
1418EXPORT_SYMBOL(pcmcia_dev_present);
1419
1420
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001421static struct pcmcia_callback pcmcia_bus_callback = {
1422 .owner = THIS_MODULE,
1423 .event = ds_event,
1424 .requery = pcmcia_bus_rescan,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001425 .suspend = pcmcia_bus_suspend,
1426 .resume = pcmcia_bus_resume,
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001427};
1428
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001429static int __devinit pcmcia_bus_add_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001430 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001432 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int ret;
1434
Dominik Brodowskidc109492005-06-27 16:28:50 -07001435 socket = pcmcia_get_socket(socket);
1436 if (!socket) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001437 dev_printk(KERN_ERR, dev,
1438 "PCMCIA obtaining reference to socket failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -ENODEV;
1440 }
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * Ugly. But we want to wait for the socket threads to have started up.
1444 * We really should let the drivers themselves drive some of this..
1445 */
1446 msleep(250);
1447
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001448#ifdef CONFIG_PCMCIA_IOCTL
Dominik Brodowskidc109492005-06-27 16:28:50 -07001449 init_waitqueue_head(&socket->queue);
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001450#endif
Dominik Brodowskidc109492005-06-27 16:28:50 -07001451 INIT_LIST_HEAD(&socket->devices_list);
David Howells4796b712006-12-06 15:01:18 +00001452 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
Dominik Brodowskidc109492005-06-27 16:28:50 -07001453 memset(&socket->pcmcia_state, 0, sizeof(u8));
1454 socket->device_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Dominik Brodowski90c6cdd2005-06-27 16:28:49 -07001456 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (ret) {
Dominik Brodowskiac449d62008-08-02 18:33:56 +02001458 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
Dominik Brodowskidc109492005-06-27 16:28:50 -07001459 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return (ret);
1461 }
1462
1463 return 0;
1464}
1465
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001466static void pcmcia_bus_remove_socket(struct device *dev,
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001467 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001469 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Dominik Brodowskidc109492005-06-27 16:28:50 -07001471 if (!socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return;
1473
Dominik Brodowskidc109492005-06-27 16:28:50 -07001474 socket->pcmcia_state.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 pccard_register_pcmcia(socket, NULL);
1476
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001477 /* unregister any unbound devices */
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001478 mutex_lock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001479 pcmcia_card_remove(socket, NULL);
Daniel Ritz8e4d9dc2006-11-25 11:09:17 -08001480 mutex_unlock(&socket->skt_mutex);
Daniel Ritzdfbc9e92006-11-18 22:19:34 -08001481
Dominik Brodowskidc109492005-06-27 16:28:50 -07001482 pcmcia_put_socket(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 return;
1485}
1486
1487
1488/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
Sam Ravnborged49f5d2008-05-01 04:34:50 -07001489static struct class_interface pcmcia_bus_interface __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 .class = &pcmcia_socket_class,
Greg Kroah-Hartman87373312006-09-12 17:00:10 +02001491 .add_dev = &pcmcia_bus_add_socket,
1492 .remove_dev = &pcmcia_bus_remove_socket,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493};
1494
1495
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001496struct bus_type pcmcia_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .name = "pcmcia",
Kay Sievers312c0042005-11-16 09:00:00 +01001498 .uevent = pcmcia_bus_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 .match = pcmcia_bus_match,
1500 .dev_attrs = pcmcia_dev_attrs,
Russell King1d0baa32006-01-05 14:40:58 +00001501 .probe = pcmcia_device_probe,
1502 .remove = pcmcia_device_remove,
Dominik Brodowski8e9e7932006-01-06 00:02:03 +01001503 .suspend = pcmcia_dev_suspend,
1504 .resume = pcmcia_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507
1508static int __init init_pcmcia_bus(void)
1509{
Randy Dunlapace7d472006-10-20 14:44:12 -07001510 int ret;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 spin_lock_init(&pcmcia_dev_list_lock);
1513
Randy Dunlapace7d472006-10-20 14:44:12 -07001514 ret = bus_register(&pcmcia_bus_type);
1515 if (ret < 0) {
1516 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1517 return ret;
1518 }
1519 ret = class_interface_register(&pcmcia_bus_interface);
1520 if (ret < 0) {
1521 printk(KERN_WARNING
1522 "pcmcia: class_interface_register error: %d\n", ret);
1523 bus_unregister(&pcmcia_bus_type);
1524 return ret;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001527 pcmcia_setup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return 0;
1530}
1531fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1532 * pcmcia_socket_class is already registered */
1533
1534
1535static void __exit exit_pcmcia_bus(void)
1536{
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001537 pcmcia_cleanup_ioctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Dominik Brodowskie7a480d2005-06-27 16:28:47 -07001539 class_interface_unregister(&pcmcia_bus_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 bus_unregister(&pcmcia_bus_type);
1542}
1543module_exit(exit_pcmcia_bus);
1544
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546MODULE_ALIAS("ds");