2 * Linux ARCnet driver - COM20020 PCMCIA support
4 * Written 1994-1999 by Avery Pennarun,
5 * based on an ISA version by David Woodhouse.
6 * Derived from ibmtr_cs.c by Steve Kipisz (pcmcia-cs 3.1.4)
7 * which was derived from pcnet_cs.c by David Hinds.
8 * Some additional portions derived from skeleton.c by Donald Becker.
10 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
11 * for sponsoring the further development of this driver.
13 * **********************
15 * The original copyright of skeleton.c was as follows:
17 * skeleton.c Written 1993 by Donald Becker.
18 * Copyright 1993 United States Government as represented by the
19 * Director, National Security Agency. This software may only be used
20 * and distributed according to the terms of the GNU General Public License as
21 * modified by SRC, incorporated herein by reference.
23 * **********************
25 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/08/2000
26 * - reorganize kmallocs in com20020_attach, checking all for failure
27 * and releasing the previous allocations if one fails
28 * **********************
30 * For more details, see drivers/net/arcnet.c
32 * **********************
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/ptrace.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/netdevice.h>
43 #include <linux/arcdevice.h>
44 #include <linux/com20020.h>
46 #include <pcmcia/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/ds.h>
53 #include <asm/system.h>
55 #define VERSION "arcnet: COM20020 PCMCIA support loaded.\n"
59 static int pc_debug = PCMCIA_DEBUG;
60 MODULE_PARM(pc_debug, "i");
61 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
63 static void regdump(struct net_device *dev)
65 int ioaddr = dev->base_addr;
68 printk("com20020 register dump:\n");
69 for (count = ioaddr; count < ioaddr + 16; count++)
72 printk("\n%04X: ", count);
73 printk("%02X ", inb(count));
77 printk("buffer0 dump:\n");
78 /* set up the address register */
80 outb((count >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
81 outb(count & 0xff, _ADDR_LO);
83 for (count = 0; count < 256+32; count++)
86 printk("\n%04X: ", count);
89 printk("%02X ", inb(_MEMDATA));
96 #define DEBUG(n, args...) do { } while (0)
97 static inline void regdump(struct net_device *dev) { }
102 /*====================================================================*/
104 /* Parameters that can be set with 'insmod' */
107 static int timeout = 3;
108 static int backplane;
112 MODULE_PARM(node, "i");
113 MODULE_PARM(timeout, "i");
114 MODULE_PARM(backplane, "i");
115 MODULE_PARM(clockp, "i");
116 MODULE_PARM(clockm, "i");
118 /* Bit map of interrupts to choose from */
119 static u_int irq_mask = 0xdeb8;
120 static int irq_list[4] = { -1 };
122 MODULE_PARM(irq_mask, "i");
123 MODULE_PARM(irq_list, "1-4i");
124 MODULE_LICENSE("GPL");
126 /*====================================================================*/
128 static void com20020_config(dev_link_t *link);
129 static void com20020_release(dev_link_t *link);
130 static int com20020_event(event_t event, int priority,
131 event_callback_args_t *args);
133 static dev_info_t dev_info = "com20020_cs";
135 static dev_link_t *com20020_attach(void);
136 static void com20020_detach(dev_link_t *);
138 static dev_link_t *dev_list;
140 /*====================================================================*/
142 typedef struct com20020_dev_t {
143 struct net_device *dev;
147 /*======================================================================
149 com20020_attach() creates an "instance" of the driver, allocating
150 local data structures for one device. The device is registered
153 ======================================================================*/
155 static dev_link_t *com20020_attach(void)
157 client_reg_t client_reg;
159 com20020_dev_t *info;
160 struct net_device *dev;
162 struct arcnet_local *lp;
164 DEBUG(0, "com20020_attach()\n");
166 /* Create new network device */
167 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
171 info = kmalloc(sizeof(struct com20020_dev_t), GFP_KERNEL);
173 goto fail_alloc_info;
175 dev = alloc_arcdev("");
179 memset(info, 0, sizeof(struct com20020_dev_t));
180 memset(link, 0, sizeof(struct dev_link_t));
182 lp->timeout = timeout;
183 lp->backplane = backplane;
185 lp->clockm = clockm & 3;
186 lp->hw.owner = THIS_MODULE;
188 /* fill in our module parameters as defaults */
189 dev->dev_addr[0] = node;
191 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
192 link->io.NumPorts1 = 16;
193 link->io.IOAddrLines = 16;
194 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
195 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
196 if (irq_list[0] == -1)
197 link->irq.IRQInfo2 = irq_mask;
199 for (i = 0; i < 4; i++)
200 link->irq.IRQInfo2 |= 1 << irq_list[i];
201 link->conf.Attributes = CONF_ENABLE_IRQ;
203 link->conf.IntType = INT_MEMORY_AND_IO;
204 link->conf.Present = PRESENT_OPTION;
207 link->irq.Instance = info->dev = dev;
210 /* Register with Card Services */
211 link->next = dev_list;
213 client_reg.dev_info = &dev_info;
214 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
215 client_reg.EventMask =
216 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
217 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
218 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
219 client_reg.event_handler = &com20020_event;
220 client_reg.Version = 0x0210;
221 client_reg.event_callback_args.client_data = link;
222 ret = pcmcia_register_client(&link->handle, &client_reg);
224 cs_error(link->handle, RegisterClient, ret);
225 com20020_detach(link);
236 } /* com20020_attach */
238 /*======================================================================
240 This deletes a driver "instance". The device is de-registered
241 with Card Services. If it has been released, all local data
242 structures are freed. Otherwise, the structures will be freed
243 when the device is released.
245 ======================================================================*/
247 static void com20020_detach(dev_link_t *link)
249 struct com20020_dev_t *info = link->priv;
251 struct net_device *dev;
253 DEBUG(1,"detach...\n");
255 DEBUG(0, "com20020_detach(0x%p)\n", link);
257 /* Locate device structure */
258 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
259 if (*linkp == link) break;
266 DEBUG(1,"unregister...\n");
268 unregister_netdev(dev);
271 * this is necessary because we register our IRQ separately
272 * from card services.
275 free_irq(dev->irq, dev);
278 if (link->state & DEV_CONFIG)
279 com20020_release(link);
282 pcmcia_deregister_client(link->handle);
284 /* Unlink device structure, free bits */
285 DEBUG(1,"unlinking...\n");
292 DEBUG(1,"kfree...\n");
295 DEBUG(1,"kfree2...\n");
298 DEBUG(1,"kfree3...\n");
301 } /* com20020_detach */
303 /*======================================================================
305 com20020_config() is scheduled to run after a CARD_INSERTION event
306 is received, to configure the PCMCIA socket, and to make the
307 device available to the system.
309 ======================================================================*/
311 #define CS_CHECK(fn, ret) \
312 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
314 static void com20020_config(dev_link_t *link)
316 struct arcnet_local *lp;
317 client_handle_t handle;
320 com20020_dev_t *info;
321 struct net_device *dev;
322 int i, last_ret, last_fn;
326 handle = link->handle;
330 DEBUG(1,"config...\n");
332 DEBUG(0, "com20020_config(0x%p)\n", link);
334 tuple.Attributes = 0;
335 tuple.TupleData = buf;
336 tuple.TupleDataMax = 64;
337 tuple.TupleOffset = 0;
338 tuple.DesiredTuple = CISTPL_CONFIG;
339 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
340 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
341 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
342 link->conf.ConfigBase = parse.config.base;
345 link->state |= DEV_CONFIG;
347 DEBUG(1,"arcnet: baseport1 is %Xh\n", link->io.BasePort1);
349 if (!link->io.BasePort1)
351 for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10)
353 link->io.BasePort1 = ioaddr;
354 i = pcmcia_request_io(link->handle, &link->io);
360 i = pcmcia_request_io(link->handle, &link->io);
364 DEBUG(1,"arcnet: requestIO failed totally!\n");
368 ioaddr = dev->base_addr = link->io.BasePort1;
369 DEBUG(1,"arcnet: got ioaddr %Xh\n", ioaddr);
371 DEBUG(1,"arcnet: request IRQ %d (%Xh/%Xh)\n",
372 link->irq.AssignedIRQ,
373 link->irq.IRQInfo1, link->irq.IRQInfo2);
374 i = pcmcia_request_irq(link->handle, &link->irq);
377 DEBUG(1,"arcnet: requestIRQ failed totally!\n");
381 dev->irq = link->irq.AssignedIRQ;
383 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
385 if (com20020_check(dev))
392 lp->card_name = "PCMCIA COM20020";
393 lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
395 link->dev = &info->node;
396 link->state &= ~DEV_CONFIG_PENDING;
398 i = com20020_found(dev, 0); /* calls register_netdev */
401 DEBUG(1,KERN_NOTICE "com20020_cs: com20020_found() failed\n");
406 strcpy(info->node.dev_name, dev->name);
408 DEBUG(1,KERN_INFO "%s: port %#3lx, irq %d\n",
409 dev->name, dev->base_addr, dev->irq);
413 cs_error(link->handle, last_fn, last_ret);
415 DEBUG(1,"com20020_config failed...\n");
416 com20020_release(link);
417 } /* com20020_config */
419 /*======================================================================
421 After a card is removed, com20020_release() will unregister the net
422 device, and release the PCMCIA configuration. If the device is
423 still open, this will be postponed until it is closed.
425 ======================================================================*/
427 static void com20020_release(dev_link_t *link)
430 DEBUG(1,"release...\n");
432 DEBUG(0, "com20020_release(0x%p)\n", link);
434 pcmcia_release_configuration(link->handle);
435 pcmcia_release_io(link->handle, &link->io);
436 pcmcia_release_irq(link->handle, &link->irq);
438 link->state &= ~(DEV_CONFIG | DEV_RELEASE_PENDING);
441 /*======================================================================
443 The card status event handler. Mostly, this schedules other
444 stuff to run after an event is received. A CARD_REMOVAL event
445 also sets some flags to discourage the net drivers from trying
446 to talk to the card any more.
448 ======================================================================*/
450 static int com20020_event(event_t event, int priority,
451 event_callback_args_t *args)
453 dev_link_t *link = args->client_data;
454 com20020_dev_t *info = link->priv;
455 struct net_device *dev = info->dev;
457 DEBUG(1, "com20020_event(0x%06x)\n", event);
460 case CS_EVENT_CARD_REMOVAL:
461 link->state &= ~DEV_PRESENT;
462 if (link->state & DEV_CONFIG)
463 netif_device_detach(dev);
465 case CS_EVENT_CARD_INSERTION:
466 link->state |= DEV_PRESENT;
467 com20020_config(link);
469 case CS_EVENT_PM_SUSPEND:
470 link->state |= DEV_SUSPEND;
471 /* Fall through... */
472 case CS_EVENT_RESET_PHYSICAL:
473 if (link->state & DEV_CONFIG) {
475 netif_device_detach(dev);
477 pcmcia_release_configuration(link->handle);
480 case CS_EVENT_PM_RESUME:
481 link->state &= ~DEV_SUSPEND;
482 /* Fall through... */
483 case CS_EVENT_CARD_RESET:
484 if (link->state & DEV_CONFIG) {
485 pcmcia_request_configuration(link->handle, &link->conf);
487 int ioaddr = dev->base_addr;
488 struct arcnet_local *lp = dev->priv;
495 } /* com20020_event */
499 static struct pcmcia_driver com20020_cs_driver = {
500 .owner = THIS_MODULE,
502 .name = "com20020_cs",
504 .attach = com20020_attach,
505 .detach = com20020_detach,
508 static int __init init_com20020_cs(void)
510 return pcmcia_register_driver(&com20020_cs_driver);
513 static void __exit exit_com20020_cs(void)
515 pcmcia_unregister_driver(&com20020_cs_driver);
516 while (dev_list != NULL)
517 com20020_detach(dev_list);
520 module_init(init_com20020_cs);
521 module_exit(exit_com20020_cs);