2 * PCMCIA high-level CIS access functions
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.
8 * Copyright (C) 1999 David A. Hinds
9 * Copyright (C) 2004-2009 Dominik Brodowski
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.
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
22 #include <pcmcia/cisreg.h>
23 #include <pcmcia/cistpl.h>
24 #include <pcmcia/ss.h>
25 #include <pcmcia/ds.h>
26 #include "cs_internal.h"
30 * pccard_read_tuple() - internal CIS tuple access
31 * @s: the struct pcmcia_socket where the card is inserted
32 * @function: the device function we loop for
33 * @code: which CIS code shall we look for?
34 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
36 * pccard_read_tuple() reads out one tuple and attempts to parse it
38 int pccard_read_tuple(struct pcmcia_socket
*s
, unsigned int function
,
39 cisdata_t code
, void *parse
)
45 buf
= kmalloc(256, GFP_KERNEL
);
47 dev_printk(KERN_WARNING
, &s
->dev
, "no memory to read tuple\n");
50 tuple
.DesiredTuple
= code
;
52 if (function
== BIND_FN_ALL
)
53 tuple
.Attributes
= TUPLE_RETURN_COMMON
;
54 ret
= pccard_get_first_tuple(s
, function
, &tuple
);
57 tuple
.TupleData
= buf
;
58 tuple
.TupleOffset
= 0;
59 tuple
.TupleDataMax
= 255;
60 ret
= pccard_get_tuple_data(s
, &tuple
);
63 ret
= pcmcia_parse_tuple(&tuple
, parse
);
71 * pccard_loop_tuple() - loop over tuples in the CIS
72 * @s: the struct pcmcia_socket where the card is inserted
73 * @function: the device function we loop for
74 * @code: which CIS code shall we look for?
75 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
76 * @priv_data: private data to be passed to the loop_tuple function.
77 * @loop_tuple: function to call for each CIS entry of type @function. IT
78 * gets passed the raw tuple, the paresed tuple (if @parse is
79 * set) and @priv_data.
81 * pccard_loop_tuple() loops over all CIS entries of type @function, and
82 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
83 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
85 int pccard_loop_tuple(struct pcmcia_socket
*s
, unsigned int function
,
86 cisdata_t code
, cisparse_t
*parse
, void *priv_data
,
87 int (*loop_tuple
) (tuple_t
*tuple
,
95 buf
= kzalloc(256, GFP_KERNEL
);
97 dev_printk(KERN_WARNING
, &s
->dev
, "no memory to read tuple\n");
101 tuple
.TupleData
= buf
;
102 tuple
.TupleDataMax
= 255;
103 tuple
.TupleOffset
= 0;
104 tuple
.DesiredTuple
= code
;
105 tuple
.Attributes
= 0;
107 ret
= pccard_get_first_tuple(s
, function
, &tuple
);
109 if (pccard_get_tuple_data(s
, &tuple
))
113 if (pcmcia_parse_tuple(&tuple
, parse
))
116 ret
= loop_tuple(&tuple
, parse
, priv_data
);
121 ret
= pccard_get_next_tuple(s
, function
, &tuple
);
128 struct pcmcia_cfg_mem
{
129 struct pcmcia_device
*p_dev
;
131 int (*conf_check
) (struct pcmcia_device
*p_dev
,
132 cistpl_cftable_entry_t
*cfg
,
133 cistpl_cftable_entry_t
*dflt
,
136 cistpl_cftable_entry_t dflt
;
140 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
142 * pcmcia_do_loop_config() is the internal callback for the call from
143 * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
144 * by a struct pcmcia_cfg_mem.
146 static int pcmcia_do_loop_config(tuple_t
*tuple
, cisparse_t
*parse
, void *priv
)
148 struct pcmcia_cfg_mem
*cfg_mem
= priv
;
149 struct pcmcia_device
*p_dev
= cfg_mem
->p_dev
;
150 cistpl_cftable_entry_t
*cfg
= &parse
->cftable_entry
;
151 cistpl_cftable_entry_t
*dflt
= &cfg_mem
->dflt
;
152 unsigned int flags
= p_dev
->config_flags
;
153 unsigned int vcc
= p_dev
->socket
->socket
.Vcc
;
155 dev_dbg(&p_dev
->dev
, "testing configuration %x, autoconf %x\n",
159 cfg_mem
->p_dev
->config_index
= cfg
->index
;
160 if (cfg
->flags
& CISTPL_CFTABLE_DEFAULT
)
161 cfg_mem
->dflt
= *cfg
;
163 /* check for matching Vcc? */
164 if (flags
& CONF_AUTO_CHECK_VCC
) {
165 if (cfg
->vcc
.present
& (1 << CISTPL_POWER_VNOM
)) {
166 if (vcc
!= cfg
->vcc
.param
[CISTPL_POWER_VNOM
] / 10000)
168 } else if (dflt
->vcc
.present
& (1 << CISTPL_POWER_VNOM
)) {
169 if (vcc
!= dflt
->vcc
.param
[CISTPL_POWER_VNOM
] / 10000)
175 if (flags
& CONF_AUTO_SET_VPP
) {
176 if (cfg
->vpp1
.present
& (1 << CISTPL_POWER_VNOM
))
177 p_dev
->vpp
= cfg
->vpp1
.param
[CISTPL_POWER_VNOM
] / 10000;
178 else if (dflt
->vpp1
.present
& (1 << CISTPL_POWER_VNOM
))
180 dflt
->vpp1
.param
[CISTPL_POWER_VNOM
] / 10000;
184 if ((flags
& CONF_AUTO_AUDIO
) && (cfg
->flags
& CISTPL_CFTABLE_AUDIO
))
185 p_dev
->config_flags
|= CONF_ENABLE_SPKR
;
187 return cfg_mem
->conf_check(cfg_mem
->p_dev
, cfg
, &cfg_mem
->dflt
,
192 * pcmcia_loop_config() - loop over configuration options
193 * @p_dev: the struct pcmcia_device which we need to loop for.
194 * @conf_check: function to call for each configuration option.
195 * It gets passed the struct pcmcia_device, the CIS data
196 * describing the configuration option, and private data
197 * being passed to pcmcia_loop_config()
198 * @priv_data: private data to be passed to the conf_check function.
200 * pcmcia_loop_config() loops over all configuration options, and calls
201 * the driver-specific conf_check() for each one, checking whether
202 * it is a valid one. Returns 0 on success or errorcode otherwise.
204 int pcmcia_loop_config(struct pcmcia_device
*p_dev
,
205 int (*conf_check
) (struct pcmcia_device
*p_dev
,
206 cistpl_cftable_entry_t
*cfg
,
207 cistpl_cftable_entry_t
*dflt
,
211 struct pcmcia_cfg_mem
*cfg_mem
;
214 cfg_mem
= kzalloc(sizeof(struct pcmcia_cfg_mem
), GFP_KERNEL
);
218 cfg_mem
->p_dev
= p_dev
;
219 cfg_mem
->conf_check
= conf_check
;
220 cfg_mem
->priv_data
= priv_data
;
222 ret
= pccard_loop_tuple(p_dev
->socket
, p_dev
->func
,
223 CISTPL_CFTABLE_ENTRY
, &cfg_mem
->parse
,
224 cfg_mem
, pcmcia_do_loop_config
);
229 EXPORT_SYMBOL(pcmcia_loop_config
);
232 struct pcmcia_loop_mem
{
233 struct pcmcia_device
*p_dev
;
235 int (*loop_tuple
) (struct pcmcia_device
*p_dev
,
241 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
243 * pcmcia_do_loop_tuple() is the internal callback for the call from
244 * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
245 * by a struct pcmcia_cfg_mem.
247 static int pcmcia_do_loop_tuple(tuple_t
*tuple
, cisparse_t
*parse
, void *priv
)
249 struct pcmcia_loop_mem
*loop
= priv
;
251 return loop
->loop_tuple(loop
->p_dev
, tuple
, loop
->priv_data
);
255 * pcmcia_loop_tuple() - loop over tuples in the CIS
256 * @p_dev: the struct pcmcia_device which we need to loop for.
257 * @code: which CIS code shall we look for?
258 * @priv_data: private data to be passed to the loop_tuple function.
259 * @loop_tuple: function to call for each CIS entry of type @function. IT
260 * gets passed the raw tuple and @priv_data.
262 * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
263 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
264 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
266 int pcmcia_loop_tuple(struct pcmcia_device
*p_dev
, cisdata_t code
,
267 int (*loop_tuple
) (struct pcmcia_device
*p_dev
,
272 struct pcmcia_loop_mem loop
= {
274 .loop_tuple
= loop_tuple
,
275 .priv_data
= priv_data
};
277 return pccard_loop_tuple(p_dev
->socket
, p_dev
->func
, code
, NULL
,
278 &loop
, pcmcia_do_loop_tuple
);
280 EXPORT_SYMBOL(pcmcia_loop_tuple
);
283 struct pcmcia_loop_get
{
289 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
291 * pcmcia_do_get_tuple() is the internal callback for the call from
292 * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
293 * the first tuple, return 0 unconditionally. Create a memory buffer large
294 * enough to hold the content of the tuple, and fill it with the tuple data.
295 * The caller is responsible to free the buffer.
297 static int pcmcia_do_get_tuple(struct pcmcia_device
*p_dev
, tuple_t
*tuple
,
300 struct pcmcia_loop_get
*get
= priv
;
302 *get
->buf
= kzalloc(tuple
->TupleDataLen
, GFP_KERNEL
);
304 get
->len
= tuple
->TupleDataLen
;
305 memcpy(*get
->buf
, tuple
->TupleData
, tuple
->TupleDataLen
);
307 dev_dbg(&p_dev
->dev
, "do_get_tuple: out of memory\n");
312 * pcmcia_get_tuple() - get first tuple from CIS
313 * @p_dev: the struct pcmcia_device which we need to loop for.
314 * @code: which CIS code shall we look for?
315 * @buf: pointer to store the buffer to.
317 * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
318 * It returns the buffer length (or zero). The caller is responsible to free
319 * the buffer passed in @buf.
321 size_t pcmcia_get_tuple(struct pcmcia_device
*p_dev
, cisdata_t code
,
324 struct pcmcia_loop_get get
= {
330 pcmcia_loop_tuple(p_dev
, code
, pcmcia_do_get_tuple
, &get
);
334 EXPORT_SYMBOL(pcmcia_get_tuple
);
338 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
340 * pcmcia_do_get_mac() is the internal callback for the call from
341 * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
342 * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
343 * to struct net_device->dev_addr[i].
345 static int pcmcia_do_get_mac(struct pcmcia_device
*p_dev
, tuple_t
*tuple
,
348 struct net_device
*dev
= priv
;
351 if (tuple
->TupleData
[0] != CISTPL_FUNCE_LAN_NODE_ID
)
353 if (tuple
->TupleDataLen
< ETH_ALEN
+ 2) {
354 dev_warn(&p_dev
->dev
, "Invalid CIS tuple length for "
359 if (tuple
->TupleData
[1] != ETH_ALEN
) {
360 dev_warn(&p_dev
->dev
, "Invalid header for LAN_NODE_ID\n");
363 for (i
= 0; i
< 6; i
++)
364 dev
->dev_addr
[i
] = tuple
->TupleData
[i
+2];
369 * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
370 * @p_dev: the struct pcmcia_device for which we want the address.
371 * @dev: a properly prepared struct net_device to store the info to.
373 * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
374 * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
375 * must be set up properly by the driver (see examples!).
377 int pcmcia_get_mac_from_cis(struct pcmcia_device
*p_dev
, struct net_device
*dev
)
379 return pcmcia_loop_tuple(p_dev
, CISTPL_FUNCE
, pcmcia_do_get_mac
, dev
);
381 EXPORT_SYMBOL(pcmcia_get_mac_from_cis
);