Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[firefly-linux-kernel-4.4.55.git] / drivers / staging / wlags49_h2 / wl_cs.c
1 /*******************************************************************************
2  * Agere Systems Inc.
3  * Wireless device driver for Linux (wlags49).
4  *
5  * Copyright (c) 1998-2003 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  * Initially developed by TriplePoint, Inc.
10  *   http://www.triplepoint.com
11  *
12  *------------------------------------------------------------------------------
13  *
14  *   This file contains processing and initialization specific to Card Services
15  *   devices (PCMCIA, CF).
16  *
17  *------------------------------------------------------------------------------
18  *
19  * SOFTWARE LICENSE
20  *
21  * This software is provided subject to the following terms and conditions,
22  * which you should read carefully before using the software.  Using this
23  * software indicates your acceptance of these terms and conditions.  If you do
24  * not agree with these terms and conditions, do not use the software.
25  *
26  * Copyright (c) 2003 Agere Systems Inc.
27  * All rights reserved.
28  *
29  * Redistribution and use in source or binary forms, with or without
30  * modifications, are permitted provided that the following conditions are met:
31  *
32  * . Redistributions of source code must retain the above copyright notice, this
33  *    list of conditions and the following Disclaimer as comments in the code as
34  *    well as in the documentation and/or other materials provided with the
35  *    distribution.
36  *
37  * . Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following Disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  * . Neither the name of Agere Systems Inc. nor the names of the contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * Disclaimer
46  *
47  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
50  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
51  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
52  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
58  * DAMAGE.
59  *
60  ******************************************************************************/
61
62 /*******************************************************************************
63  *  include files
64  ******************************************************************************/
65 #include <wl_version.h>
66
67 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/ptrace.h>
70 #include <linux/ctype.h>
71 #include <linux/string.h>
72 #include <linux/timer.h>
73 #include <linux/interrupt.h>
74 #include <linux/in.h>
75 #include <linux/delay.h>
76 #include <asm/io.h>
77 #include <asm/system.h>
78 #include <asm/bitops.h>
79
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/if_arp.h>
84 #include <linux/ioport.h>
85 #include <linux/module.h>
86
87 #include <pcmcia/cistpl.h>
88 #include <pcmcia/cisreg.h>
89 #include <pcmcia/ciscode.h>
90 #include <pcmcia/ds.h>
91 #include <debug.h>
92
93 #include <hcf.h>
94 #include <dhf.h>
95 #include <hcfdef.h>
96
97 #include <wl_if.h>
98 #include <wl_internal.h>
99 #include <wl_util.h>
100 #include <wl_main.h>
101 #include <wl_netdev.h>
102 #include <wl_cs.h>
103 #include <wl_sysfs.h>
104
105
106 /*******************************************************************************
107  *  global definitions
108  ******************************************************************************/
109 #if DBG
110 extern dbg_info_t *DbgInfo;
111 #endif  /* DBG */
112
113
114 /*******************************************************************************
115  *      wl_adapter_attach()
116  *******************************************************************************
117  *
118  *  DESCRIPTION:
119  *
120  *      Creates an instance of the driver, allocating local data structures for
121  *  one device. The device is registered with Card Services.
122  *
123  *  PARAMETERS:
124  *
125  *      none
126  *
127  *  RETURNS:
128  *
129  *      pointer to an allocated dev_link_t structure
130  *      NULL on failure
131  *
132  ******************************************************************************/
133 static int wl_adapter_attach(struct pcmcia_device *link)
134 {
135         struct net_device   *dev;
136         struct wl_private   *lp;
137         /*--------------------------------------------------------------------*/
138
139         DBG_FUNC("wl_adapter_attach");
140         DBG_ENTER(DbgInfo);
141
142         dev = wl_device_alloc();
143         if (dev == NULL) {
144                 DBG_ERROR(DbgInfo, "wl_device_alloc returned NULL\n");
145                 return -ENOMEM;
146         }
147
148         link->resource[0]->end  = HCF_NUM_IO_PORTS;
149         link->resource[0]->flags= IO_DATA_PATH_WIDTH_16;
150         link->config_flags     |= CONF_ENABLE_IRQ;
151         link->config_index      = 5;
152         link->config_regs       = PRESENT_OPTION;
153
154         link->priv = dev;
155         lp = wl_priv(dev);
156         lp->link = link;
157
158         wl_adapter_insert(link);
159
160         DBG_LEAVE(DbgInfo);
161         return 0;
162 } /* wl_adapter_attach */
163 /*============================================================================*/
164
165
166
167 static void wl_adapter_detach(struct pcmcia_device *link)
168 {
169         struct net_device   *dev = link->priv;
170         /*--------------------------------------------------------------------*/
171
172         DBG_FUNC("wl_adapter_detach");
173         DBG_ENTER(DbgInfo);
174         DBG_PARAM(DbgInfo, "link", "0x%p", link);
175
176         wl_adapter_release(link);
177
178         if (dev) {
179                 unregister_wlags_sysfs(dev);
180                 unregister_netdev(dev);
181         }
182
183         wl_device_dealloc(dev);
184
185         DBG_LEAVE(DbgInfo);
186 } /* wl_adapter_detach */
187 /*============================================================================*/
188
189
190 void wl_adapter_release(struct pcmcia_device *link)
191 {
192         DBG_FUNC("wl_adapter_release");
193         DBG_ENTER(DbgInfo);
194         DBG_PARAM(DbgInfo, "link", "0x%p", link);
195
196         /* Stop hardware */
197         wl_remove(link->priv);
198
199         pcmcia_disable_device(link);
200
201         DBG_LEAVE(DbgInfo);
202 } /* wl_adapter_release */
203 /*============================================================================*/
204
205 static int wl_adapter_suspend(struct pcmcia_device *link)
206 {
207         struct net_device *dev = link->priv;
208
209         /* if (link->open) { */
210         netif_device_detach(dev);
211         wl_suspend(dev);
212         /* CHECK! pcmcia_release_configuration(link->handle); */
213         /* } */
214
215         return 0;
216 } /* wl_adapter_suspend */
217
218 static int wl_adapter_resume(struct pcmcia_device *link)
219 {
220         struct net_device *dev = link->priv;
221
222         wl_resume(dev);
223
224         netif_device_attach(dev);
225
226         return 0;
227 } /* wl_adapter_resume */
228
229 void wl_adapter_insert(struct pcmcia_device *link)
230 {
231         struct net_device *dev;
232         int ret;
233         /*--------------------------------------------------------------------*/
234
235         DBG_FUNC("wl_adapter_insert");
236         DBG_ENTER(DbgInfo);
237         DBG_PARAM(DbgInfo, "link", "0x%p", link);
238
239         dev     = link->priv;
240
241         /* Do we need to allocate an interrupt? */
242         link->config_flags |= CONF_ENABLE_IRQ;
243         link->io_lines = 6;
244
245         ret = pcmcia_request_io(link);
246         if (ret != 0)
247                 goto failed;
248
249         ret = pcmcia_request_irq(link, (void *) wl_isr);
250         if (ret != 0)
251                 goto failed;
252
253         ret = pcmcia_enable_device(link);
254         if (ret != 0)
255                 goto failed;
256
257         dev->irq        = link->irq;
258         dev->base_addr  = link->resource[0]->start;
259
260         SET_NETDEV_DEV(dev, &link->dev);
261         if (register_netdev(dev) != 0) {
262                 printk("%s: register_netdev() failed\n", MODULE_NAME);
263                 goto failed;
264         }
265
266         register_wlags_sysfs(dev);
267
268         printk(KERN_INFO "%s: Wireless, io_addr %#03lx, irq %d, mac_address"
269                 " %pM\n", dev->name, dev->base_addr, dev->irq, dev->dev_addr);
270
271         DBG_LEAVE(DbgInfo);
272         return;
273
274 failed:
275         wl_adapter_release(link);
276
277         DBG_LEAVE(DbgInfo);
278         return;
279 } /* wl_adapter_insert */
280 /*============================================================================*/
281
282
283 /*******************************************************************************
284  *      wl_adapter_open()
285  *******************************************************************************
286  *
287  *  DESCRIPTION:
288  *
289  *      Open the device.
290  *
291  *  PARAMETERS:
292  *
293  *      dev - a pointer to a net_device structure representing the network
294  *            device to open.
295  *
296  *  RETURNS:
297  *
298  *      0 on success
299  *      errno value otherwise
300  *
301  ******************************************************************************/
302 int wl_adapter_open(struct net_device *dev)
303 {
304         struct wl_private *lp = wl_priv(dev);
305         struct pcmcia_device *link = lp->link;
306         int result = 0;
307         int hcf_status = HCF_SUCCESS;
308         /*--------------------------------------------------------------------*/
309
310         DBG_FUNC("wl_adapter_open");
311         DBG_ENTER(DbgInfo);
312         DBG_PRINT("%s\n", VERSION_INFO);
313         DBG_PARAM(DbgInfo, "dev", "%s (0x%p)", dev->name, dev);
314
315         if (!pcmcia_dev_present(link)) {
316                 DBG_LEAVE(DbgInfo);
317                 return -ENODEV;
318         }
319
320         link->open++;
321
322         hcf_status = wl_open(dev);
323
324         if (hcf_status != HCF_SUCCESS) {
325                 link->open--;
326                 result = -ENODEV;
327         }
328
329         DBG_LEAVE(DbgInfo);
330         return result;
331 } /* wl_adapter_open */
332 /*============================================================================*/
333
334
335 /*******************************************************************************
336  *      wl_adapter_close()
337  *******************************************************************************
338  *
339  *  DESCRIPTION:
340  *
341  *      Close the device.
342  *
343  *  PARAMETERS:
344  *
345  *      dev - a pointer to a net_device structure representing the network
346  *            device to close.
347  *
348  *  RETURNS:
349  *
350  *      0 on success
351  *      errno value otherwise
352  *
353  ******************************************************************************/
354 int wl_adapter_close(struct net_device *dev)
355 {
356         struct wl_private *lp = wl_priv(dev);
357         struct pcmcia_device *link = lp->link;
358         /*--------------------------------------------------------------------*/
359
360         DBG_FUNC("wl_adapter_close");
361         DBG_ENTER(DbgInfo);
362         DBG_PARAM(DbgInfo, "dev", "%s (0x%p)", dev->name, dev);
363
364         if (link == NULL) {
365                 DBG_LEAVE(DbgInfo);
366                 return -ENODEV;
367         }
368
369         DBG_TRACE(DbgInfo, "%s: Shutting down adapter.\n", dev->name);
370         wl_close(dev);
371
372         link->open--;
373
374         DBG_LEAVE(DbgInfo);
375         return 0;
376 } /* wl_adapter_close */
377 /*============================================================================*/
378
379 static const struct pcmcia_device_id wl_adapter_ids[] = {
380 #if !((HCF_TYPE) & HCF_TYPE_HII5)
381         PCMCIA_DEVICE_MANF_CARD(0x0156, 0x0003),
382         PCMCIA_DEVICE_PROD_ID12("Agere Systems", "Wireless PC Card Model 0110",
383                                 0x33103a9b, 0xe175b0dd),
384 #else
385         PCMCIA_DEVICE_MANF_CARD(0x0156, 0x0004),
386         PCMCIA_DEVICE_PROD_ID12("Linksys", "WCF54G_Wireless-G_CompactFlash_Card",
387                                 0x0733cc81, 0x98a599e1),
388 #endif  /* (HCF_TYPE) & HCF_TYPE_HII5 */
389         PCMCIA_DEVICE_NULL,
390 };
391 MODULE_DEVICE_TABLE(pcmcia, wl_adapter_ids);
392
393 static struct pcmcia_driver wlags49_driver = {
394         .owner      = THIS_MODULE,
395         .name       = DRIVER_NAME,
396         .probe      = wl_adapter_attach,
397         .remove     = wl_adapter_detach,
398         .id_table   = wl_adapter_ids,
399         .suspend    = wl_adapter_suspend,
400         .resume     = wl_adapter_resume,
401 };
402
403
404
405 /*******************************************************************************
406  *      wl_adapter_init_module()
407  *******************************************************************************
408  *
409  *  DESCRIPTION:
410  *
411  *      Called by init_module() to perform PCMCIA driver initialization.
412  *
413  *  PARAMETERS:
414  *
415  *      N/A
416  *
417  *  RETURNS:
418  *
419  *      0 on success
420  *      -1 on error
421  *
422  ******************************************************************************/
423 int wl_adapter_init_module(void)
424 {
425         int ret;
426         /*--------------------------------------------------------------------*/
427
428         DBG_FUNC("wl_adapter_init_module");
429         DBG_ENTER(DbgInfo);
430         DBG_TRACE(DbgInfo, "wl_adapter_init_module() -- PCMCIA\n");
431
432         ret = pcmcia_register_driver(&wlags49_driver);
433
434         DBG_LEAVE(DbgInfo);
435         return ret;
436 } /* wl_adapter_init_module */
437 /*============================================================================*/
438
439
440 /*******************************************************************************
441  *      wl_adapter_cleanup_module()
442  *******************************************************************************
443  *
444  *  DESCRIPTION:
445  *
446  *      Called by cleanup_module() to perform driver uninitialization.
447  *
448  *  PARAMETERS:
449  *
450  *      N/A
451  *
452  *  RETURNS:
453  *
454  *      N/A
455  *
456  ******************************************************************************/
457 void wl_adapter_cleanup_module(void)
458 {
459         DBG_FUNC("wl_adapter_cleanup_module");
460         DBG_ENTER(DbgInfo);
461         DBG_TRACE(DbgInfo, "wl_adapter_cleanup_module() -- PCMCIA\n");
462
463
464         pcmcia_unregister_driver(&wlags49_driver);
465
466         DBG_LEAVE(DbgInfo);
467         return;
468 } /* wl_adapter_cleanup_module */
469 /*============================================================================*/
470
471
472 /*******************************************************************************
473  *      wl_adapter_is_open()
474  *******************************************************************************
475  *
476  *  DESCRIPTION:
477  *
478  *      Check with Card Services to determine if this device is open.
479  *
480  *  PARAMETERS:
481  *
482  *      dev - a pointer to the net_device structure whose open status will be
483  *            checked
484  *
485  *  RETURNS:
486  *
487  *      nonzero if device is open
488  *      0 otherwise
489  *
490  ******************************************************************************/
491 int wl_adapter_is_open(struct net_device *dev)
492 {
493         struct wl_private *lp = wl_priv(dev);
494         struct pcmcia_device *link = lp->link;
495
496         if (!pcmcia_dev_present(link))
497                 return 0;
498
499         return link->open;
500 } /* wl_adapter_is_open */
501 /*============================================================================*/