diff options
author | Dominik Brodowski <linux@dominikbrodowski.net> | 2009-10-18 17:32:33 -0400 |
---|---|---|
committer | Dominik Brodowski <linux@dominikbrodowski.net> | 2009-11-08 12:06:54 -0500 |
commit | 91284224da5b15ec6c2b45e10fa5eccd1c92a204 (patch) | |
tree | e070902f5119b2eacf79d91cd36de98e3ae8423c /drivers/pcmcia/pcmcia_resource.c | |
parent | af757923a92e6e9dbfdb6b0264be14c564e1c466 (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/pcmcia/pcmcia_resource.c')
-rw-r--r-- | drivers/pcmcia/pcmcia_resource.c | 217 |
1 files changed, 185 insertions, 32 deletions
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 | ||
887 | struct pcmcia_cfg_mem { | 888 | struct 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 | */ | ||
907 | static 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 | } |
961 | EXPORT_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)) | 964 | struct 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 | */ | ||
979 | static 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 | */ | ||
998 | int 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 | }; | ||
1012 | EXPORT_SYMBOL(pcmcia_loop_tuple); | ||
953 | 1013 | ||
954 | next_entry: | 1014 | |
955 | ret = pcmcia_get_next_tuple(p_dev, tuple); | 1015 | struct 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 | */ | ||
1029 | static 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 | */ | ||
1052 | size_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 | }; | ||
1065 | EXPORT_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 | */ | ||
1076 | static 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 | */ | ||
1108 | int 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 | }; | ||
1112 | EXPORT_SYMBOL(pcmcia_get_mac_from_cis); | ||
957 | 1113 | ||
958 | return ret; | ||
959 | } | ||
960 | EXPORT_SYMBOL(pcmcia_loop_config); | ||