1/* 2 * PCMCIA high-level CIS access functions 3 * 4 * The initial developer of the original code is David A. Hinds 5 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds 6 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 7 * 8 * Copyright (C) 1999 David A. Hinds 9 * Copyright (C) 2004-2009 Dominik Brodowski 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 * 15 */ 16 17#include <linux/slab.h> 18#include <linux/module.h> 19#include <linux/kernel.h> 20#include <linux/netdevice.h> 21 22#include <pcmcia/cisreg.h> 23#include <pcmcia/cistpl.h> 24#include <pcmcia/ss.h> 25#include <pcmcia/cs.h> 26#include <pcmcia/ds.h> 27#include "cs_internal.h" 28 29 30/** 31 * pccard_read_tuple() - internal CIS tuple access 32 * @s: the struct pcmcia_socket where the card is inserted 33 * @function: the device function we loop for 34 * @code: which CIS code shall we look for? 35 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse) 36 * 37 * pccard_read_tuple() reads out one tuple and attempts to parse it 38 */ 39int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, 40 cisdata_t code, void *parse) 41{ 42 tuple_t tuple; 43 cisdata_t *buf; 44 int ret; 45 46 buf = kmalloc(256, GFP_KERNEL); 47 if (buf == NULL) { 48 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); 49 return -ENOMEM; 50 } 51 tuple.DesiredTuple = code; 52 tuple.Attributes = 0; 53 if (function == BIND_FN_ALL) 54 tuple.Attributes = TUPLE_RETURN_COMMON; 55 ret = pccard_get_first_tuple(s, function, &tuple); 56 if (ret != 0) 57 goto done; 58 tuple.TupleData = buf; 59 tuple.TupleOffset = 0; 60 tuple.TupleDataMax = 255; 61 ret = pccard_get_tuple_data(s, &tuple); 62 if (ret != 0) 63 goto done; 64 ret = pcmcia_parse_tuple(&tuple, parse); 65done: 66 kfree(buf); 67 return ret; 68} 69 70 71/** 72 * pccard_loop_tuple() - loop over tuples in the CIS 73 * @s: the struct pcmcia_socket where the card is inserted 74 * @function: the device function we loop for 75 * @code: which CIS code shall we look for? 76 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse) 77 * @priv_data: private data to be passed to the loop_tuple function. 78 * @loop_tuple: function to call for each CIS entry of type @function. IT 79 * gets passed the raw tuple, the paresed tuple (if @parse is 80 * set) and @priv_data. 81 * 82 * pccard_loop_tuple() loops over all CIS entries of type @function, and 83 * calls the @loop_tuple function for each entry. If the call to @loop_tuple 84 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise. 85 */ 86int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function, 87 cisdata_t code, cisparse_t *parse, void *priv_data, 88 int (*loop_tuple) (tuple_t *tuple, 89 cisparse_t *parse, 90 void *priv_data)) 91{ 92 tuple_t tuple; 93 cisdata_t *buf; 94 int ret; 95 96 buf = kzalloc(256, GFP_KERNEL); 97 if (buf == NULL) { 98 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); 99 return -ENOMEM; 100 } 101 102 tuple.TupleData = buf; 103 tuple.TupleDataMax = 255; 104 tuple.TupleOffset = 0; 105 tuple.DesiredTuple = code; 106 tuple.Attributes = 0; 107 108 ret = pccard_get_first_tuple(s, function, &tuple); 109 while (!ret) { 110 if (pccard_get_tuple_data(s, &tuple)) 111 goto next_entry; 112 113 if (parse) 114 if (pcmcia_parse_tuple(&tuple, parse)) 115 goto next_entry; 116 117 ret = loop_tuple(&tuple, parse, priv_data); 118 if (!ret) 119 break; 120 121next_entry: 122 ret = pccard_get_next_tuple(s, function, &tuple); 123 } 124 125 kfree(buf); 126 return ret; 127} 128 129struct pcmcia_cfg_mem { 130 struct pcmcia_device *p_dev; 131 void *priv_data; 132 int (*conf_check) (struct pcmcia_device *p_dev, 133 cistpl_cftable_entry_t *cfg, 134 cistpl_cftable_entry_t *dflt, 135 unsigned int vcc, 136 void *priv_data); 137 cisparse_t parse; 138 cistpl_cftable_entry_t dflt; 139}; 140 141/** 142 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config() 143 * 144 * pcmcia_do_loop_config() is the internal callback for the call from 145 * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred 146 * by a struct pcmcia_cfg_mem. 147 */ 148static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv) 149{ 150 cistpl_cftable_entry_t *cfg = &parse->cftable_entry; 151 struct pcmcia_cfg_mem *cfg_mem = priv; 152 153 /* default values */ 154 cfg_mem->p_dev->conf.ConfigIndex = cfg->index; 155 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) 156 cfg_mem->dflt = *cfg; 157 158 return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt, 159 cfg_mem->p_dev->socket->socket.Vcc, 160 cfg_mem->priv_data); 161} 162 163/** 164 * pcmcia_loop_config() - loop over configuration options 165 * @p_dev: the struct pcmcia_device which we need to loop for. 166 * @conf_check: function to call for each configuration option. 167 * It gets passed the struct pcmcia_device, the CIS data 168 * describing the configuration option, and private data 169 * being passed to pcmcia_loop_config() 170 * @priv_data: private data to be passed to the conf_check function. 171 * 172 * pcmcia_loop_config() loops over all configuration options, and calls 173 * the driver-specific conf_check() for each one, checking whether 174 * it is a valid one. Returns 0 on success or errorcode otherwise. 175 */ 176int pcmcia_loop_config(struct pcmcia_device *p_dev, 177 int (*conf_check) (struct pcmcia_device *p_dev, 178 cistpl_cftable_entry_t *cfg, 179 cistpl_cftable_entry_t *dflt, 180 unsigned int vcc, 181 void *priv_data), 182 void *priv_data) 183{ 184 struct pcmcia_cfg_mem *cfg_mem; 185 int ret; 186 187 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL); 188 if (cfg_mem == NULL) 189 return -ENOMEM; 190 191 cfg_mem->p_dev = p_dev; 192 cfg_mem->conf_check = conf_check; 193 cfg_mem->priv_data = priv_data; 194 195 ret = pccard_loop_tuple(p_dev->socket, p_dev->func, 196 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse, 197 cfg_mem, pcmcia_do_loop_config); 198 199 kfree(cfg_mem); 200 return ret; 201} 202EXPORT_SYMBOL(pcmcia_loop_config); 203 204 205struct pcmcia_loop_mem { 206 struct pcmcia_device *p_dev; 207 void *priv_data; 208 int (*loop_tuple) (struct pcmcia_device *p_dev, 209 tuple_t *tuple, 210 void *priv_data); 211}; 212 213/** 214 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config() 215 * 216 * pcmcia_do_loop_tuple() is the internal callback for the call from 217 * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred 218 * by a struct pcmcia_cfg_mem. 219 */ 220static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv) 221{ 222 struct pcmcia_loop_mem *loop = priv; 223 224 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data); 225}; 226 227/** 228 * pcmcia_loop_tuple() - loop over tuples in the CIS 229 * @p_dev: the struct pcmcia_device which we need to loop for. 230 * @code: which CIS code shall we look for? 231 * @priv_data: private data to be passed to the loop_tuple function. 232 * @loop_tuple: function to call for each CIS entry of type @function. IT 233 * gets passed the raw tuple and @priv_data. 234 * 235 * pcmcia_loop_tuple() loops over all CIS entries of type @function, and 236 * calls the @loop_tuple function for each entry. If the call to @loop_tuple 237 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise. 238 */ 239int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code, 240 int (*loop_tuple) (struct pcmcia_device *p_dev, 241 tuple_t *tuple, 242 void *priv_data), 243 void *priv_data) 244{ 245 struct pcmcia_loop_mem loop = { 246 .p_dev = p_dev, 247 .loop_tuple = loop_tuple, 248 .priv_data = priv_data}; 249 250 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL, 251 &loop, pcmcia_do_loop_tuple); 252} 253EXPORT_SYMBOL(pcmcia_loop_tuple); 254 255 256struct pcmcia_loop_get { 257 size_t len; 258 cisdata_t **buf; 259}; 260 261/** 262 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple() 263 * 264 * pcmcia_do_get_tuple() is the internal callback for the call from 265 * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in 266 * the first tuple, return 0 unconditionally. Create a memory buffer large 267 * enough to hold the content of the tuple, and fill it with the tuple data. 268 * The caller is responsible to free the buffer. 269 */ 270static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple, 271 void *priv) 272{ 273 struct pcmcia_loop_get *get = priv; 274 275 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL); 276 if (*get->buf) { 277 get->len = tuple->TupleDataLen; 278 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen); 279 } else 280 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n"); 281 return 0; 282} 283 284/** 285 * pcmcia_get_tuple() - get first tuple from CIS 286 * @p_dev: the struct pcmcia_device which we need to loop for. 287 * @code: which CIS code shall we look for? 288 * @buf: pointer to store the buffer to. 289 * 290 * pcmcia_get_tuple() gets the content of the first CIS entry of type @code. 291 * It returns the buffer length (or zero). The caller is responsible to free 292 * the buffer passed in @buf. 293 */ 294size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code, 295 unsigned char **buf) 296{ 297 struct pcmcia_loop_get get = { 298 .len = 0, 299 .buf = buf, 300 }; 301 302 *get.buf = NULL; 303 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get); 304 305 return get.len; 306} 307EXPORT_SYMBOL(pcmcia_get_tuple); 308 309 310/** 311 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis() 312 * 313 * pcmcia_do_get_mac() is the internal callback for the call from 314 * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the 315 * tuple contains a proper LAN_NODE_ID of length 6, and copy the data 316 * to struct net_device->dev_addr[i]. 317 */ 318static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple, 319 void *priv) 320{ 321 struct net_device *dev = priv; 322 int i; 323 324 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID) 325 return -EINVAL; 326 if (tuple->TupleDataLen < ETH_ALEN + 2) { 327 dev_warn(&p_dev->dev, "Invalid CIS tuple length for " 328 "LAN_NODE_ID\n"); 329 return -EINVAL; 330 } 331 332 if (tuple->TupleData[1] != ETH_ALEN) { 333 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n"); 334 return -EINVAL; 335 } 336 for (i = 0; i < 6; i++) 337 dev->dev_addr[i] = tuple->TupleData[i+2]; 338 return 0; 339} 340 341/** 342 * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE 343 * @p_dev: the struct pcmcia_device for which we want the address. 344 * @dev: a properly prepared struct net_device to store the info to. 345 * 346 * pcmcia_get_mac_from_cis() reads out the hardware MAC address from 347 * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which 348 * must be set up properly by the driver (see examples!). 349 */ 350int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev) 351{ 352 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev); 353} 354EXPORT_SYMBOL(pcmcia_get_mac_from_cis); 355