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