2 * This file define the new driver API for Wireless Extensions
6 * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
7 * Copyright (c) 2001 Jean Tourrilhes, All Rights Reserved.
13 /************************** DOCUMENTATION **************************/
15 * Initial driver API (1996 -> onward) :
16 * -----------------------------------
17 * The initial API just sends the IOCTL request received from user space
18 * to the driver (via the driver ioctl handler). The driver has to
19 * handle all the rest...
21 * The initial API also defines a specific handler in struct net_device
22 * to handle wireless statistics.
24 * The initial APIs served us well and has proven a reasonably good design.
25 * However, there is a few shortcommings :
26 * o No events, everything is a request to the driver.
27 * o Large ioctl function in driver with gigantic switch statement
28 * (i.e. spaghetti code).
29 * o Driver has to mess up with copy_to/from_user, and in many cases
30 * does it unproperly. Common mistakes are :
31 * * buffer overflows (no checks or off by one checks)
32 * * call copy_to/from_user with irq disabled
33 * o The user space interface is tied to ioctl because of the use
36 * New driver API (2001 -> onward) :
37 * -------------------------------
38 * The new driver API is just a bunch of standard functions (handlers),
39 * each handling a specific Wireless Extension. The driver just export
40 * the list of handler it supports, and those will be called apropriately.
42 * I tried to keep the main advantage of the previous API (simplicity,
43 * efficiency and light weight), and also I provide a good dose of backward
44 * compatibility (most structures are the same, driver can use both API
45 * simultaneously, ...).
46 * Hopefully, I've also addressed the shortcomming of the initial API.
48 * The advantage of the new API are :
49 * o Handling of Extensions in driver broken in small contained functions
50 * o Tighter checks of ioctl before calling the driver
51 * o Flexible commit strategy (at least, the start of it)
52 * o Backward compatibility (can be mixed with old API)
53 * o Driver doesn't have to worry about memory and user-space issues
54 * The last point is important for the following reasons :
55 * o You are now able to call the new driver API from any API you
56 * want (including from within other parts of the kernel).
57 * o Common mistakes are avoided (buffer overflow, user space copy
58 * with irq disabled and so on).
60 * The Drawback of the new API are :
61 * o bloat (especially kernel)
62 * o need to migrate existing drivers to new API
63 * My initial testing shows that the new API adds around 3kB to the kernel
64 * and save between 0 and 5kB from a typical driver.
65 * Also, as all structures and data types are unchanged, the migration is
66 * quite straightforward (but tedious).
70 * The new driver API is defined below in this file. User space should
71 * not be aware of what's happening down there...
73 * A new kernel wrapper is in charge of validating the IOCTLs and calling
74 * the appropriate driver handler. This is implemented in :
75 * # net/core/wireless.c
77 * The driver export the list of handlers in :
78 * # include/linux/netdevice.h (one place)
80 * The new driver API is available for WIRELESS_EXT >= 13.
81 * Good luck with migration to the new API ;-)
84 /* ---------------------- THE IMPLEMENTATION ---------------------- */
86 * Some of the choice I've made are pretty controversials. Defining an
87 * API is very much weighting compromises. This goes into some of the
88 * details and the thinking behind the implementation.
90 * Implementation goals :
91 * --------------------
92 * The implementation goals were as follow :
93 * o Obvious : you should not need a PhD to understand what's happening,
94 * the benefit is easier maintainance.
95 * o Flexible : it should accomodate a wide variety of driver
96 * implementations and be as flexible as the old API.
97 * o Lean : it should be efficient memory wise to minimise the impact
98 * on kernel footprint.
99 * o Transparent to user space : the large number of user space
100 * applications that use Wireless Extensions should not need
103 * Array of functions versus Struct of functions
104 * ---------------------------------------------
105 * 1) Having an array of functions allow the kernel code to access the
106 * handler in a single lookup, which is much more efficient (think hash
108 * 2) The only drawback is that driver writer may put their handler in
109 * the wrong slot. This is trivial to test (I set the frequency, the
110 * bitrate changes). Once the handler is in the proper slot, it will be
111 * there forever, because the array is only extended at the end.
112 * 3) Backward/forward compatibility : adding new handler just require
113 * extending the array, so you can put newer driver in older kernel
114 * without having to patch the kernel code (and vice versa).
116 * All handler are of the same generic type
117 * ----------------------------------------
118 * That's a feature !!!
119 * 1) Having a generic handler allow to have generic code, which is more
120 * efficient. If each of the handler was individually typed I would need
121 * to add a big switch in the kernel (== more bloat). This solution is
122 * more scalable, adding new Wireless Extensions doesn't add new code.
123 * 2) You can use the same handler in different slots of the array. For
124 * hardware, it may be more efficient or logical to handle multiple
125 * Wireless Extensions with a single function, and the API allow you to
126 * do that. (An example would be a single record on the card to control
127 * both bitrate and frequency, the handler would read the old record,
128 * modify it according to info->cmd and rewrite it).
130 * Functions prototype uses union iwreq_data
131 * -----------------------------------------
132 * Some would have prefered functions defined this way :
133 * static int mydriver_ioctl_setrate(struct net_device *dev,
134 * long rate, int auto)
135 * 1) The kernel code doesn't "validate" the content of iwreq_data, and
136 * can't do it (different hardware may have different notion of what a
137 * valid frequency is), so we don't pretend that we do it.
138 * 2) The above form is not extendable. If I want to add a flag (for
139 * example to distinguish setting max rate and basic rate), I would
140 * break the prototype. Using iwreq_data is more flexible.
141 * 3) Also, the above form is not generic (see above).
142 * 4) I don't expect driver developper using the wrong field of the
143 * union (Doh !), so static typechecking doesn't add much value.
144 * 5) Lastly, you can skip the union by doing :
145 * static int mydriver_ioctl_setrate(struct net_device *dev,
146 * struct iw_request_info *info,
147 * struct iw_param *rrq,
149 * And then adding the handler in the array like this :
150 * (iw_handler) mydriver_ioctl_setrate, // SIOCSIWRATE
152 * Using functions and not a registry
153 * ----------------------------------
154 * Another implementation option would have been for every instance to
155 * define a registry (a struct containing all the Wireless Extensions)
156 * and only have a function to commit the registry to the hardware.
157 * 1) This approach can be emulated by the current code, but not
159 * 2) Some drivers don't keep any configuration in the driver, for them
160 * adding such a registry would be a significant bloat.
161 * 3) The code to translate from Wireless Extension to native format is
162 * needed anyway, so it would not reduce significantely the amount of code.
163 * 4) The current approach only selectively translate Wireless Extensions
164 * to native format and only selectively set, whereas the registry approach
165 * would require to translate all WE and set all parameters for any single
167 * 5) For many Wireless Extensions, the GET operation return the current
168 * dynamic value, not the value that was set.
170 * This header is <net/iw_handler.h>
171 * ---------------------------------
172 * 1) This header is kernel space only and should not be exported to
173 * user space. Headers in "include/linux/" are exported, headers in
174 * "include/net/" are not.
176 * Mixed 32/64 bit issues
177 * ----------------------
178 * The Wireless Extensions are designed to be 64 bit clean, by using only
179 * datatypes with explicit storage size.
180 * There are some issues related to kernel and user space using different
181 * memory model, and in particular 64bit kernel with 32bit user space.
182 * The problem is related to struct iw_point, that contains a pointer
183 * that *may* need to be translated.
184 * This is quite messy. The new API doesn't solve this problem (it can't),
185 * but is a step in the right direction :
186 * 1) Meta data about each ioctl is easily available, so we know what type
187 * of translation is needed.
188 * 2) The move of data between kernel and user space is only done in a single
189 * place in the kernel, so adding specific hooks in there is possible.
190 * 3) In the long term, it allows to move away from using ioctl as the
193 * So many comments and so few code
194 * --------------------------------
195 * That's a feature. Comments won't bloat the resulting kernel binary.
198 /***************************** INCLUDES *****************************/
200 #include <linux/wireless.h> /* IOCTL user space API */
202 /***************************** VERSION *****************************/
204 * This constant is used to know which version of the driver API is
205 * available. Hopefully, this will be pretty stable and no changes
207 * I just plan to increment with each new version.
209 #define IW_HANDLER_VERSION 2
211 /**************************** CONSTANTS ****************************/
213 /* Special error message for the driver to indicate that we
214 * should do a commit after return from the iw_handler */
215 #define EIWCOMMIT EINPROGRESS
217 /* Flags available in struct iw_request_info */
218 #define IW_REQUEST_FLAG_NONE 0x0000 /* No flag so far */
220 /* Type of headers we know about (basically union iwreq_data) */
221 #define IW_HEADER_TYPE_NULL 0 /* Not available */
222 #define IW_HEADER_TYPE_CHAR 2 /* char [IFNAMSIZ] */
223 #define IW_HEADER_TYPE_UINT 4 /* __u32 */
224 #define IW_HEADER_TYPE_FREQ 5 /* struct iw_freq */
225 #define IW_HEADER_TYPE_POINT 6 /* struct iw_point */
226 #define IW_HEADER_TYPE_PARAM 7 /* struct iw_param */
227 #define IW_HEADER_TYPE_ADDR 8 /* struct sockaddr */
230 /* Most are not implemented. I just use them as a reminder of some
231 * cool features we might need one day ;-) */
232 #define IW_DESCR_FLAG_NONE 0x0000 /* Obvious */
233 /* Wrapper level flags */
234 #define IW_DESCR_FLAG_DUMP 0x0001 /* Not part of the dump command */
235 #define IW_DESCR_FLAG_EVENT 0x0002 /* Generate an event on SET */
236 #define IW_DESCR_FLAG_RESTRICT 0x0004 /* GET request is ROOT only */
237 /* Driver level flags */
238 #define IW_DESCR_FLAG_WAIT 0x0100 /* Wait for driver event */
240 /****************************** TYPES ******************************/
242 /* ----------------------- WIRELESS HANDLER ----------------------- */
244 * A wireless handler is just a standard function, that looks like the
246 * We also define there how a handler list look like... As the Wireless
247 * Extension space is quite dense, we use a simple array, which is faster
248 * (that's the perfect hash table ;-).
252 * Meta data about the request passed to the iw_handler.
253 * Most handlers can safely ignore what's in there.
254 * The 'cmd' field might come handy if you want to use the same handler
255 * for multiple command...
256 * This struct is also my long term insurance. I can add new fields here
257 * without breaking the prototype of iw_handler...
259 struct iw_request_info
261 __u16 cmd
; /* Wireless Extension command */
262 __u16 flags
; /* More to come ;-) */
266 * This is how a function handling a Wireless Extension should look
267 * like (both get and set, standard and private).
269 typedef int (*iw_handler
)(struct net_device
*dev
, struct iw_request_info
*info
,
270 union iwreq_data
*wrqu
, char *extra
);
273 * This define all the handler that the driver export.
274 * As you need only one per driver type, please use a static const
275 * shared by all driver instances... Same for the members...
276 * This will be linked from net_device in <linux/netdevice.h>
278 struct iw_handler_def
280 /* Number of handlers defined (more precisely, index of the
281 * last defined handler + 1) */
284 /* Number of private arg description */
285 __u16 num_private_args
;
287 /* Array of handlers for standard ioctls
288 * We will call dev->wireless_handlers->standard[ioctl - SIOCSIWNAME]
290 iw_handler
* standard
;
292 /* Array of handlers for private ioctls
293 * Will call dev->wireless_handlers->private[ioctl - SIOCIWFIRSTPRIV]
295 iw_handler
* private;
297 /* Arguments of private handler. This one is just a list, so you
298 * can put it in any order you want and should not leave holes...
299 * We will automatically export that to user space... */
300 struct iw_priv_args
* private_args
;
302 /* In the long term, get_wireless_stats will move from
303 * 'struct net_device' to here, to minimise bloat. */
306 /* ----------------------- WIRELESS EVENTS ----------------------- */
308 * Currently we don't support events, so let's just plan for the
315 // How do we define short header ? We don't want a flag on length.
316 // Probably a flag on event ? Highest bit to zero...
319 __u16 length
; /* Lenght of this stuff */
320 __u16 event
; /* Wireless IOCTL */
321 union iwreq_data header
; /* IOCTL fixed payload */
322 char extra
[0]; /* Optional IOCTL data */
325 /* ---------------------- IOCTL DESCRIPTION ---------------------- */
327 * One of the main goal of the new interface is to deal entirely with
328 * user space/kernel space memory move.
329 * For that, we need to know :
330 * o if iwreq is a pointer or contain the full data
331 * o what is the size of the data to copy
333 * For private IOCTLs, we use the same rules as used by iwpriv and
334 * defined in struct iw_priv_args.
336 * For standard IOCTLs, things are quite different and we need to
337 * use the stuctures below. Actually, this struct is also more
338 * efficient, but that's another story...
342 * Describe how a standard IOCTL looks like.
344 struct iw_ioctl_description
346 __u8 header_type
; /* NULL, iw_point or other */
347 __u8 token_type
; /* Future */
348 __u16 token_size
; /* Granularity of payload */
349 __u16 min_tokens
; /* Min acceptable token number */
350 __u16 max_tokens
; /* Max acceptable token number */
351 __u32 flags
; /* Special handling of the request */
354 /* Need to think of short header translation table. Later. */
356 /**************************** PROTOTYPES ****************************/
358 * Functions part of the Wireless Extensions (defined in net/core/wireless.c).
359 * Those may be called only within the kernel.
362 /* First : function strictly used inside the kernel */
364 /* Handle /proc/net/wireless, called in net/code/dev.c */
365 extern int dev_get_wireless_info(char * buffer
, char **start
, off_t offset
,
368 /* Handle IOCTLs, called in net/code/dev.c */
369 extern int wireless_process_ioctl(struct ifreq
*ifr
, unsigned int cmd
);
371 /* Second : functions that may be called by driver modules */
374 #endif /* _LINUX_WIRELESS_H */