aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDominik Brodowski <linux@dominikbrodowski.net>2009-10-18 17:32:33 -0400
committerDominik Brodowski <linux@dominikbrodowski.net>2009-11-08 12:06:54 -0500
commit91284224da5b15ec6c2b45e10fa5eccd1c92a204 (patch)
treee070902f5119b2eacf79d91cd36de98e3ae8423c /drivers
parentaf757923a92e6e9dbfdb6b0264be14c564e1c466 (diff)
pcmcia: add new CIS access helpers
As a replacement to pcmcia_get_{first,next}_tuple() and pcmcia_get_tuple_data(), three new -- and easier to use -- functions are added: - pcmcia_get_tuple() to get the very first CIS entry of one type. - pcmcia_loop_tuple() to loop over all CIS entries of one type. - pcmcia_get_mac_from_cis() to read out the hardware MAC address from CISTPL_FUNCE. Only a handful of drivers need these functions anyway, as most CIS access is already handled by pcmcia_loop_config(), which now shares the same backed (pccard_loop_tuple()) with pcmcia_loop_tuple(). A pcmcia_get_mac_from_cis() bug noted by Komuro <komurojun-mbn@nifty.com> has been fixed in this revision. Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/pcmcia/cistpl.c61
-rw-r--r--drivers/pcmcia/cs_internal.h7
-rw-r--r--drivers/pcmcia/pcmcia_resource.c217
-rw-r--r--drivers/pcmcia/rsrc_mgr.c1
4 files changed, 254 insertions, 32 deletions
diff --git a/drivers/pcmcia/cistpl.c b/drivers/pcmcia/cistpl.c
index 6c4a4fc83630..24dd3c1eac0b 100644
--- a/drivers/pcmcia/cistpl.c
+++ b/drivers/pcmcia/cistpl.c
@@ -1482,6 +1482,67 @@ done:
1482} 1482}
1483EXPORT_SYMBOL(pccard_read_tuple); 1483EXPORT_SYMBOL(pccard_read_tuple);
1484 1484
1485
1486/**
1487 * pccard_loop_tuple() - loop over tuples in the CIS
1488 * @s: the struct pcmcia_socket where the card is inserted
1489 * @function: the device function we loop for
1490 * @code: which CIS code shall we look for?
1491 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
1492 * @priv_data: private data to be passed to the loop_tuple function.
1493 * @loop_tuple: function to call for each CIS entry of type @function. IT
1494 * gets passed the raw tuple, the paresed tuple (if @parse is
1495 * set) and @priv_data.
1496 *
1497 * pccard_loop_tuple() loops over all CIS entries of type @function, and
1498 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
1499 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
1500 */
1501int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
1502 cisdata_t code, cisparse_t *parse, void *priv_data,
1503 int (*loop_tuple) (tuple_t *tuple,
1504 cisparse_t *parse,
1505 void *priv_data))
1506{
1507 tuple_t tuple;
1508 cisdata_t *buf;
1509 int ret;
1510
1511 buf = kzalloc(256, GFP_KERNEL);
1512 if (buf == NULL) {
1513 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
1514 return -ENOMEM;
1515 }
1516
1517 tuple.TupleData = buf;
1518 tuple.TupleDataMax = 255;
1519 tuple.TupleOffset = 0;
1520 tuple.DesiredTuple = code;
1521 tuple.Attributes = 0;
1522
1523 ret = pccard_get_first_tuple(s, function, &tuple);
1524 while (!ret) {
1525 if (pccard_get_tuple_data(s, &tuple))
1526 goto next_entry;
1527
1528 if (parse)
1529 if (pcmcia_parse_tuple(&tuple, parse))
1530 goto next_entry;
1531
1532 ret = loop_tuple(&tuple, parse, priv_data);
1533 if (!ret)
1534 break;
1535
1536next_entry:
1537 ret = pccard_get_next_tuple(s, function, &tuple);
1538 }
1539
1540 kfree(buf);
1541 return ret;
1542}
1543EXPORT_SYMBOL(pccard_loop_tuple);
1544
1545
1485/*====================================================================== 1546/*======================================================================
1486 1547
1487 This tries to determine if a card has a sensible CIS. It returns 1548 This tries to determine if a card has a sensible CIS. It returns
diff --git a/drivers/pcmcia/cs_internal.h b/drivers/pcmcia/cs_internal.h
index 1f4098f1354d..06a14c951e92 100644
--- a/drivers/pcmcia/cs_internal.h
+++ b/drivers/pcmcia/cs_internal.h
@@ -199,6 +199,13 @@ int pcmcia_replace_cis(struct pcmcia_socket *s,
199 const u8 *data, const size_t len); 199 const u8 *data, const size_t len);
200int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *count); 200int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *count);
201 201
202/* loop over CIS entries */
203int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
204 cisdata_t code, cisparse_t *parse, void *priv_data,
205 int (*loop_tuple) (tuple_t *tuple,
206 cisparse_t *parse,
207 void *priv_data));
208
202/* rsrc_mgr.c */ 209/* rsrc_mgr.c */
203int pcmcia_validate_mem(struct pcmcia_socket *s); 210int pcmcia_validate_mem(struct pcmcia_socket *s);
204struct resource *pcmcia_find_io_region(unsigned long base, 211struct resource *pcmcia_find_io_region(unsigned long base,
diff --git a/drivers/pcmcia/pcmcia_resource.c b/drivers/pcmcia/pcmcia_resource.c
index d919e96c0afd..0bfb05aa8f85 100644
--- a/drivers/pcmcia/pcmcia_resource.c
+++ b/drivers/pcmcia/pcmcia_resource.c
@@ -20,6 +20,7 @@
20#include <linux/delay.h> 20#include <linux/delay.h>
21#include <linux/pci.h> 21#include <linux/pci.h>
22#include <linux/device.h> 22#include <linux/device.h>
23#include <linux/netdevice.h>
23 24
24#include <pcmcia/cs_types.h> 25#include <pcmcia/cs_types.h>
25#include <pcmcia/ss.h> 26#include <pcmcia/ss.h>
@@ -885,13 +886,40 @@ EXPORT_SYMBOL(pcmcia_disable_device);
885 886
886 887
887struct pcmcia_cfg_mem { 888struct pcmcia_cfg_mem {
888 tuple_t tuple; 889 struct pcmcia_device *p_dev;
890 void *priv_data;
891 int (*conf_check) (struct pcmcia_device *p_dev,
892 cistpl_cftable_entry_t *cfg,
893 cistpl_cftable_entry_t *dflt,
894 unsigned int vcc,
895 void *priv_data);
889 cisparse_t parse; 896 cisparse_t parse;
890 u8 buf[256];
891 cistpl_cftable_entry_t dflt; 897 cistpl_cftable_entry_t dflt;
892}; 898};
893 899
894/** 900/**
901 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
902 *
903 * pcmcia_do_loop_config() is the internal callback for the call from
904 * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
905 * by a struct pcmcia_cfg_mem.
906 */
907static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
908{
909 cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
910 struct pcmcia_cfg_mem *cfg_mem = priv;
911
912 /* default values */
913 cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
914 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
915 cfg_mem->dflt = *cfg;
916
917 return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
918 cfg_mem->p_dev->socket->socket.Vcc,
919 cfg_mem->priv_data);
920}
921
922/**
895 * pcmcia_loop_config() - loop over configuration options 923 * pcmcia_loop_config() - loop over configuration options
896 * @p_dev: the struct pcmcia_device which we need to loop for. 924 * @p_dev: the struct pcmcia_device which we need to loop for.
897 * @conf_check: function to call for each configuration option. 925 * @conf_check: function to call for each configuration option.
@@ -913,48 +941,173 @@ int pcmcia_loop_config(struct pcmcia_device *p_dev,
913 void *priv_data) 941 void *priv_data)
914{ 942{
915 struct pcmcia_cfg_mem *cfg_mem; 943 struct pcmcia_cfg_mem *cfg_mem;
916
917 tuple_t *tuple;
918 int ret; 944 int ret;
919 unsigned int vcc;
920 945
921 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL); 946 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
922 if (cfg_mem == NULL) 947 if (cfg_mem == NULL)
923 return -ENOMEM; 948 return -ENOMEM;
924 949
925 /* get the current Vcc setting */ 950 cfg_mem->p_dev = p_dev;
926 vcc = p_dev->socket->socket.Vcc; 951 cfg_mem->conf_check = conf_check;
952 cfg_mem->priv_data = priv_data;
927 953
928 tuple = &cfg_mem->tuple; 954 ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
929 tuple->TupleData = cfg_mem->buf; 955 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
930 tuple->TupleDataMax = 255; 956 cfg_mem, pcmcia_do_loop_config);
931 tuple->TupleOffset = 0;
932 tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
933 tuple->Attributes = 0;
934 957
935 ret = pcmcia_get_first_tuple(p_dev, tuple); 958 kfree(cfg_mem);
936 while (!ret) { 959 return ret;
937 cistpl_cftable_entry_t *cfg = &cfg_mem->parse.cftable_entry; 960}
961EXPORT_SYMBOL(pcmcia_loop_config);
938 962
939 if (pcmcia_get_tuple_data(p_dev, tuple))
940 goto next_entry;
941 963
942 if (pcmcia_parse_tuple(tuple, &cfg_mem->parse)) 964struct pcmcia_loop_mem {
943 goto next_entry; 965 struct pcmcia_device *p_dev;
966 void *priv_data;
967 int (*loop_tuple) (struct pcmcia_device *p_dev,
968 tuple_t *tuple,
969 void *priv_data);
970};
944 971
945 /* default values */ 972/**
946 p_dev->conf.ConfigIndex = cfg->index; 973 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
947 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) 974 *
948 cfg_mem->dflt = *cfg; 975 * pcmcia_do_loop_tuple() is the internal callback for the call from
976 * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
977 * by a struct pcmcia_cfg_mem.
978 */
979static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
980{
981 struct pcmcia_loop_mem *loop = priv;
949 982
950 ret = conf_check(p_dev, cfg, &cfg_mem->dflt, vcc, priv_data); 983 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
951 if (!ret) 984};
952 break; 985
986/**
987 * pcmcia_loop_tuple() - loop over tuples in the CIS
988 * @p_dev: the struct pcmcia_device which we need to loop for.
989 * @code: which CIS code shall we look for?
990 * @priv_data: private data to be passed to the loop_tuple function.
991 * @loop_tuple: function to call for each CIS entry of type @function. IT
992 * gets passed the raw tuple and @priv_data.
993 *
994 * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
995 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
996 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
997 */
998int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
999 int (*loop_tuple) (struct pcmcia_device *p_dev,
1000 tuple_t *tuple,
1001 void *priv_data),
1002 void *priv_data)
1003{
1004 struct pcmcia_loop_mem loop = {
1005 .p_dev = p_dev,
1006 .loop_tuple = loop_tuple,
1007 .priv_data = priv_data};
1008
1009 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
1010 &loop, pcmcia_do_loop_tuple);
1011};
1012EXPORT_SYMBOL(pcmcia_loop_tuple);
953 1013
954next_entry: 1014
955 ret = pcmcia_get_next_tuple(p_dev, tuple); 1015struct pcmcia_loop_get {
1016 size_t len;
1017 cisdata_t **buf;
1018};
1019
1020/**
1021 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
1022 *
1023 * pcmcia_do_get_tuple() is the internal callback for the call from
1024 * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
1025 * the first tuple, return 0 unconditionally. Create a memory buffer large
1026 * enough to hold the content of the tuple, and fill it with the tuple data.
1027 * The caller is responsible to free the buffer.
1028 */
1029static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
1030 void *priv)
1031{
1032 struct pcmcia_loop_get *get = priv;
1033
1034 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1035 if (*get->buf) {
1036 get->len = tuple->TupleDataLen;
1037 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
956 } 1038 }
1039 return 0;
1040};
1041
1042/**
1043 * pcmcia_get_tuple() - get first tuple from CIS
1044 * @p_dev: the struct pcmcia_device which we need to loop for.
1045 * @code: which CIS code shall we look for?
1046 * @buf: pointer to store the buffer to.
1047 *
1048 * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
1049 * It returns the buffer length (or zero). The caller is responsible to free
1050 * the buffer passed in @buf.
1051 */
1052size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1053 unsigned char **buf)
1054{
1055 struct pcmcia_loop_get get = {
1056 .len = 0,
1057 .buf = buf,
1058 };
1059
1060 *get.buf = NULL;
1061 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1062
1063 return get.len;
1064};
1065EXPORT_SYMBOL(pcmcia_get_tuple);
1066
1067
1068/**
1069 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
1070 *
1071 * pcmcia_do_get_mac() is the internal callback for the call from
1072 * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
1073 * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
1074 * to struct net_device->dev_addr[i].
1075 */
1076static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1077 void *priv)
1078{
1079 struct net_device *dev = priv;
1080 int i;
1081
1082 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1083 return -EINVAL;
1084 if (tuple->TupleDataLen < ETH_ALEN + 2) {
1085 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1086 "LAN_NODE_ID\n");
1087 return -EINVAL;
1088 }
1089
1090 if (tuple->TupleData[1] != ETH_ALEN) {
1091 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1092 return -EINVAL;
1093 }
1094 for (i = 0; i < 6; i++)
1095 dev->dev_addr[i] = tuple->TupleData[i+2];
1096 return 0;
1097};
1098
1099/**
1100 * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
1101 * @p_dev: the struct pcmcia_device for which we want the address.
1102 * @dev: a properly prepared struct net_device to store the info to.
1103 *
1104 * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
1105 * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
1106 * must be set up properly by the driver (see examples!).
1107 */
1108int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1109{
1110 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1111};
1112EXPORT_SYMBOL(pcmcia_get_mac_from_cis);
957 1113
958 return ret;
959}
960EXPORT_SYMBOL(pcmcia_loop_config);
diff --git a/drivers/pcmcia/rsrc_mgr.c b/drivers/pcmcia/rsrc_mgr.c
index e592e0e0d7ed..de0e770ce6a3 100644
--- a/drivers/pcmcia/rsrc_mgr.c
+++ b/drivers/pcmcia/rsrc_mgr.c
@@ -18,6 +18,7 @@
18#include <pcmcia/cs_types.h> 18#include <pcmcia/cs_types.h>
19#include <pcmcia/ss.h> 19#include <pcmcia/ss.h>
20#include <pcmcia/cs.h> 20#include <pcmcia/cs.h>
21#include <pcmcia/cistpl.h>
21#include "cs_internal.h" 22#include "cs_internal.h"
22 23
23 24