16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
27 #include <linux/slab.h>
46 static void pcmcia_check_driver(
struct pcmcia_driver *p_drv)
52 if (!p_drv->probe || !p_drv->remove)
54 "function\n", p_drv->name);
57 for (i = 0; i < 4; i++) {
61 hash =
crc32(0, did->prod_id[i],
strlen(did->prod_id[i]));
62 if (hash == did->prod_id_hash[i])
66 "product string \"%s\": is 0x%x, should "
67 "be 0x%x\n", p_drv->name, did->prod_id[i],
68 did->prod_id_hash[i], hash);
70 "Documentation/pcmcia/devicetable.txt for "
101 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
102 __u16 match_flags, manf_id, card_id;
104 __u32 prod_id_hash[4] = {0, 0, 0, 0};
108 fields =
sscanf(buf,
"%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
109 &match_flags, &manf_id, &card_id, &func_id, &
function, &device_no,
110 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
118 dynid->
id.match_flags = match_flags;
119 dynid->
id.manf_id = manf_id;
120 dynid->
id.card_id = card_id;
122 dynid->
id.function =
function;
123 dynid->
id.device_no = device_no;
124 memcpy(dynid->
id.prod_id_hash, prod_id_hash,
sizeof(
__u32) * 4);
139 pcmcia_free_dynids(
struct pcmcia_driver *drv)
152 pcmcia_create_newid_file(
struct pcmcia_driver *drv)
155 if (drv->probe !=
NULL)
161 pcmcia_remove_newid_file(
struct pcmcia_driver *drv)
179 pcmcia_check_driver(driver);
183 driver->drv.owner = driver->owner;
184 driver->drv.name = driver->name;
186 INIT_LIST_HEAD(&driver->dynids.list);
188 pr_debug(
"registering driver %s\n", driver->name);
194 error = pcmcia_create_newid_file(driver);
208 pr_debug(
"unregistering driver %s\n", driver->name);
209 pcmcia_remove_newid_file(driver);
211 pcmcia_free_dynids(driver);
218 static struct pcmcia_device *pcmcia_get_dev(
struct pcmcia_device *p_dev)
224 return to_pcmcia_dev(tmp_dev);
227 static void pcmcia_put_dev(
struct pcmcia_device *p_dev)
233 static void pcmcia_release_function(
struct kref *ref)
240 static void pcmcia_release_dev(
struct device *
dev)
242 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
244 dev_dbg(dev,
"releasing device\n");
246 for (i = 0; i < 4; i++)
247 kfree(p_dev->prod_id[i]);
248 kfree(p_dev->devname);
249 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
254 static int pcmcia_device_probe(
struct device *dev)
256 struct pcmcia_device *p_dev;
257 struct pcmcia_driver *p_drv;
266 p_dev = to_pcmcia_dev(dev);
267 p_drv = to_pcmcia_drv(dev->
driver);
270 dev_dbg(dev,
"trying to bind to %s\n", p_drv->
name);
272 if ((!p_drv->probe) || (!p_dev->function_config) ||
273 (!try_module_get(p_drv->owner))) {
282 p_dev->config_base = cis_config.
base;
283 p_dev->config_regs = cis_config.
rmask[0];
284 dev_dbg(dev,
"base %x, regs %x", p_dev->config_base,
288 "pcmcia: could not parse base and rmask0 of CIS\n");
289 p_dev->config_base = 0;
290 p_dev->config_regs = 0;
293 ret = p_drv->probe(p_dev);
295 dev_dbg(dev,
"binding to %s failed with %d\n",
299 dev_dbg(dev,
"%s bound: Vpp %d.%d, idx %x, IRQ %d", p_drv->
name,
300 p_dev->vpp/10, p_dev->vpp%10, p_dev->config_index, p_dev->irq);
301 dev_dbg(dev,
"resources: ioport %pR %pR iomem %pR %pR %pR",
302 p_dev->resource[0], p_dev->resource[1], p_dev->resource[2],
303 p_dev->resource[3], p_dev->resource[4]);
306 if ((s->pcmcia_pfc) &&
307 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
313 module_put(p_drv->owner);
324 static void pcmcia_card_remove(
struct pcmcia_socket *s,
struct pcmcia_device *leftover)
326 struct pcmcia_device *p_dev;
327 struct pcmcia_device *
tmp;
330 "pcmcia_card_remove(%d) %s\n", s->
sock,
331 leftover ? leftover->devname :
"");
342 if (p_dev == leftover)
346 list_del(&p_dev->socket_device_list);
349 dev_dbg(&p_dev->dev,
"unregistering device\n");
356 static int pcmcia_device_remove(
struct device *dev)
358 struct pcmcia_device *p_dev;
359 struct pcmcia_driver *p_drv;
362 p_dev = to_pcmcia_dev(dev);
363 p_drv = to_pcmcia_drv(dev->
driver);
365 dev_dbg(dev,
"removing device\n");
371 if ((p_dev->socket->pcmcia_pfc) &&
372 (p_dev->socket->device_count > 0) &&
373 (p_dev->device_no == 0))
374 pcmcia_card_remove(p_dev->socket, p_dev);
381 p_drv->remove(p_dev);
384 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
386 "pcmcia: driver %s did not release config properly\n",
392 "pcmcia: driver %s did not release window properly\n",
396 pcmcia_put_dev(p_dev);
397 module_put(p_drv->owner);
406 static int pcmcia_device_query(
struct pcmcia_device *p_dev)
420 p_dev->manf_id = manf_id.
manf;
421 p_dev->card_id = manf_id.
card;
422 p_dev->has_manf_id = 1;
423 p_dev->has_card_id = 1;
430 p_dev->func_id = func_id.
func;
431 p_dev->has_func_id = 1;
447 "mem device geometry probably means "
451 p_dev->has_func_id = 1;
460 for (i = 0; i <
min_t(
unsigned int, 4, vers1->
ns); i++) {
465 tmp = vers1->
str + vers1->
ofs[
i];
468 if ((length < 2) || (length > 255))
475 new =
strncpy(
new, tmp, length);
477 tmp = p_dev->prod_id[
i];
478 p_dev->prod_id[
i] =
new;
489 static struct pcmcia_device *pcmcia_device_add(
struct pcmcia_socket *s,
490 unsigned int function)
492 struct pcmcia_device *p_dev, *tmp_dev;
499 pr_debug(
"adding device to %d, function %d\n", s->
sock,
function);
501 p_dev = kzalloc(
sizeof(
struct pcmcia_device),
GFP_KERNEL);
506 p_dev->device_no = (s->device_count++);
510 if ((p_dev->device_no >= 2) && (
function == 0))
514 if (p_dev->device_no >= 4)
518 p_dev->func =
function;
521 p_dev->dev.parent = s->
dev.parent;
522 p_dev->dev.release = pcmcia_release_dev;
525 p_dev->dev.dma_mask = &p_dev->dma_mask;
526 dev_set_name(&p_dev->dev,
"%d.%d", p_dev->socket->sock, p_dev->device_no);
527 if (!dev_name(&p_dev->dev))
532 dev_dbg(&p_dev->dev,
"devname is %s\n", p_dev->devname);
543 p_dev->function_config = tmp_dev->function_config;
544 p_dev->irq = tmp_dev->irq;
545 kref_get(&p_dev->function_config->ref);
549 list_add(&p_dev->socket_device_list, &s->devices_list);
553 "IRQ setup failed -- device might not work\n");
555 if (!p_dev->function_config) {
557 dev_dbg(&p_dev->dev,
"creating config_t\n");
563 p_dev->function_config =
c;
566 c->
io[
i].name = p_dev->devname;
570 c->
mem[
i].name = p_dev->devname;
575 p_dev->resource[i] = &p_dev->function_config->io[i];
577 p_dev->resource[i] = &p_dev->function_config->mem[i-MAX_IO_WIN];
582 "pcmcia: registering new device %s (IRQ: %d)\n",
583 p_dev->devname, p_dev->irq);
585 pcmcia_device_query(p_dev);
594 list_del(&p_dev->socket_device_list);
602 for (i = 0; i < 4; i++)
603 kfree(p_dev->prod_id[i]);
604 kfree(p_dev->devname);
616 unsigned int no_funcs,
i, no_chains;
622 "no resources available, delaying card_add\n");
628 dev_dbg(&s->
dev,
"validating mem resources failed, "
629 "delaying card_add\n");
636 if (ret || !no_chains) {
637 dev_dbg(&s->
dev,
"invalid CIS or invalid resources\n");
647 for (i = 0; i < no_funcs; i++)
648 pcmcia_device_add(s, i);
654 static int pcmcia_requery_callback(
struct device *dev,
void * _data)
656 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
657 if (!p_dev->dev.driver) {
658 dev_dbg(dev,
"update device information\n");
659 pcmcia_device_query(p_dev);
683 int old_funcs, new_funcs;
694 if (old_funcs != new_funcs) {
696 pcmcia_card_remove(s,
NULL);
706 has_pfc = s->pcmcia_pfc;
709 pcmcia_device_add(s, 0);
718 #ifdef CONFIG_PCMCIA_LOAD_CIS
729 static int pcmcia_load_firmware(
struct pcmcia_device *dev,
char *
filename)
735 int old_funcs, new_funcs = 1;
740 dev_dbg(&dev->dev,
"trying to load CIS file %s\n", filename);
746 "pcmcia: CIS override is too big\n");
754 "pcmcia: CIS override failed\n");
764 if (old_funcs != new_funcs)
768 pcmcia_device_query(dev);
781 static inline int pcmcia_load_firmware(
struct pcmcia_device *dev,
char * filename)
789 static inline int pcmcia_devmatch(
struct pcmcia_device *dev,
793 if ((!dev->has_manf_id) || (dev->manf_id != did->
manf_id))
798 if ((!dev->has_card_id) || (dev->card_id != did->
card_id))
808 if (!dev->prod_id[0])
810 if (
strcmp(did->prod_id[0], dev->prod_id[0]))
815 if (!dev->prod_id[1])
817 if (
strcmp(did->prod_id[1], dev->prod_id[1]))
822 if (!dev->prod_id[2])
824 if (
strcmp(did->prod_id[2], dev->prod_id[2]))
829 if (!dev->prod_id[3])
831 if (
strcmp(did->prod_id[3], dev->prod_id[3]))
836 dev_dbg(&dev->dev,
"this is a pseudo-multi-function device\n");
838 dev->socket->pcmcia_pfc = 1;
847 if ((!dev->has_func_id) || (dev->func_id != did->
func_id))
852 if (dev->socket->pcmcia_pfc)
862 ret = dev->allow_func_id_match;
867 "skipping FUNC_ID match until userspace ACK\n");
873 dev_dbg(&dev->dev,
"device needs a fake CIS\n");
874 if (!dev->socket->fake_cis)
875 if (pcmcia_load_firmware(dev, did->
cisfile))
881 for (i = 0; i < 4; i++)
884 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
894 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
895 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
902 dev_dbg(dev,
"trying to match to %s\n", drv->
name);
903 if (pcmcia_devmatch(p_dev, &dynid->
id)) {
912 dev_dbg(dev,
"trying to match to %s\n", drv->
name);
913 if (pcmcia_devmatch(p_dev, did)) {
923 #ifdef CONFIG_HOTPLUG
927 struct pcmcia_device *p_dev;
929 u32 hash[4] = { 0, 0, 0, 0};
934 p_dev = to_pcmcia_dev(dev);
937 for (i = 0; i < 4; i++) {
938 if (!p_dev->prod_id[i])
940 hash[
i] =
crc32(0, p_dev->prod_id[i],
strlen(p_dev->prod_id[i]));
949 if (
add_uevent_var(env,
"MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
950 "pa%08Xpb%08Xpc%08Xpd%08X",
951 p_dev->has_manf_id ? p_dev->manf_id : 0,
952 p_dev->has_card_id ? p_dev->card_id : 0,
953 p_dev->has_func_id ? p_dev->func_id : 0,
977 static int pcmcia_dev_resume(
struct device *dev);
979 static int runtime_suspend(
struct device *dev)
989 static int runtime_resume(
struct device *dev)
994 rc = pcmcia_dev_resume(dev);
1001 #define pcmcia_device_attr(field, test, format) \
1002 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1004 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1005 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
1008 #define pcmcia_device_stringattr(name, field) \
1009 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1011 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1012 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
1027 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1031 for (i = 0; i < PCMCIA_NUM_RESOURCES; i++)
1032 str +=
sprintf(str,
"%pr\n", p_dev->resource[i]);
1039 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1041 if (p_dev->suspended)
1048 const char *buf,
size_t count)
1050 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1056 if ((!p_dev->suspended) && !
strncmp(buf,
"off", 3))
1057 ret = runtime_suspend(dev);
1058 else if (p_dev->suspended && !
strncmp(buf,
"on", 2))
1059 ret = runtime_resume(dev);
1061 return ret ? ret :
count;
1067 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1069 u32 hash[4] = { 0, 0, 0, 0};
1072 for (i = 0; i < 4; i++) {
1073 if (!p_dev->prod_id[i])
1075 hash[
i] =
crc32(0, p_dev->prod_id[i],
1076 strlen(p_dev->prod_id[i]));
1078 return sprintf(buf,
"pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1079 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1080 p_dev->has_manf_id ? p_dev->manf_id : 0,
1081 p_dev->has_card_id ? p_dev->card_id : 0,
1082 p_dev->has_func_id ? p_dev->func_id : 0,
1083 p_dev->func, p_dev->device_no,
1084 hash[0], hash[1], hash[2], hash[3]);
1087 static ssize_t pcmcia_store_allow_func_id_match(
struct device *dev,
1090 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1096 p_dev->allow_func_id_match = 1;
1105 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1115 __ATTR(allow_func_id_match, 0200,
NULL, pcmcia_store_allow_func_id_match),
1123 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1124 struct pcmcia_driver *p_drv =
NULL;
1128 if (p_dev->suspended) {
1132 p_dev->suspended = 1;
1138 p_drv = to_pcmcia_drv(dev->
driver);
1143 if (p_drv->suspend) {
1144 ret = p_drv->suspend(p_dev);
1147 "pcmcia: device %s (driver %s) did "
1148 "not want to go to sleep (%d)\n",
1149 p_dev->devname, p_drv->
name, ret);
1151 p_dev->suspended = 0;
1157 if (p_dev->device_no == p_dev->func) {
1158 dev_dbg(dev,
"releasing configuration\n");
1167 static int pcmcia_dev_resume(
struct device *dev)
1169 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1170 struct pcmcia_driver *p_drv =
NULL;
1174 if (!p_dev->suspended) {
1178 p_dev->suspended = 0;
1184 p_drv = to_pcmcia_drv(dev->
driver);
1189 if (p_dev->device_no == p_dev->func) {
1190 dev_dbg(dev,
"requesting configuration\n");
1197 ret = p_drv->resume(p_dev);
1204 static int pcmcia_bus_suspend_callback(
struct device *dev,
void * _data)
1207 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1209 if (p_dev->socket != skt || p_dev->suspended)
1212 return runtime_suspend(dev);
1215 static int pcmcia_bus_resume_callback(
struct device *dev,
void * _data)
1218 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1220 if (p_dev->socket != skt || !p_dev->suspended)
1223 runtime_resume(dev);
1239 pcmcia_bus_suspend_callback)) {
1240 pcmcia_bus_resume(skt);
1249 pcmcia_card_remove(skt,
NULL);
1264 skt->pcmcia_pfc = 0;
1268 pcmcia_card_add(skt);
1273 static int pcmcia_bus_early_resume(
struct pcmcia_socket *skt)
1278 dev_dbg(&skt->
dev,
"cis mismatch - different card\n");
1281 pcmcia_bus_remove(skt);
1291 pcmcia_bus_add(skt);
1305 struct pcmcia_device *p_dev;
1306 struct pcmcia_device *ret =
NULL;
1308 p_dev = pcmcia_get_dev(_p_dev);
1315 pcmcia_put_dev(p_dev);
1323 .add = pcmcia_bus_add,
1324 .remove = pcmcia_bus_remove,
1325 .requery = pcmcia_requery,
1327 .suspend = pcmcia_bus_suspend,
1328 .early_resume = pcmcia_bus_early_resume,
1329 .resume = pcmcia_bus_resume,
1341 "PCMCIA obtaining reference to socket failed\n");
1347 dev_printk(
KERN_ERR, dev,
"PCMCIA registration failed\n");
1352 INIT_LIST_HEAD(&socket->devices_list);
1353 socket->pcmcia_pfc = 0;
1354 socket->device_count = 0;
1359 dev_printk(
KERN_ERR, dev,
"PCMCIA registration failed\n");
1367 static void pcmcia_bus_remove_socket(
struct device *dev,
1379 pcmcia_card_remove(socket,
NULL);
1394 .add_dev = &pcmcia_bus_add_socket,
1395 .remove_dev = &pcmcia_bus_remove_socket,
1401 .uevent = pcmcia_bus_uevent,
1402 .match = pcmcia_bus_match,
1403 .dev_attrs = pcmcia_dev_attrs,
1404 .probe = pcmcia_device_probe,
1405 .remove = pcmcia_device_remove,
1406 .suspend = pcmcia_dev_suspend,
1407 .resume = pcmcia_dev_resume,
1411 static int __init init_pcmcia_bus(
void)
1423 "pcmcia: class_interface_register error: %d\n", ret);
1434 static void __exit exit_pcmcia_bus(
void)