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;
265 if (link->state & DEV_CONFIG)
266 com20020_release(link);
269 pcmcia_deregister_client(link->handle);
271 /* Unlink device structure, free bits */
272 DEBUG(1,"unlinking...\n");
281 DEBUG(1,"unregister...\n");
283 unregister_netdev(dev);
286 * this is necessary because we register our IRQ separately
287 * from card services.
290 free_irq(dev->irq, dev);
291 /* ...but I/O ports are done automatically by card services */
294 DEBUG(1,"kfree...\n");
297 DEBUG(1,"kfree2...\n");
300 DEBUG(1,"kfree3...\n");
303 } /* com20020_detach */
305 /*======================================================================
307 com20020_config() is scheduled to run after a CARD_INSERTION event
308 is received, to configure the PCMCIA socket, and to make the
309 device available to the system.
311 ======================================================================*/
313 #define CS_CHECK(fn, ret) \
314 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
316 static void com20020_config(dev_link_t *link)
318 struct arcnet_local *lp;
319 client_handle_t handle;
322 com20020_dev_t *info;
323 struct net_device *dev;
324 int i, last_ret, last_fn;
328 handle = link->handle;
332 DEBUG(1,"config...\n");
334 DEBUG(0, "com20020_config(0x%p)\n", link);
336 tuple.Attributes = 0;
337 tuple.TupleData = buf;
338 tuple.TupleDataMax = 64;
339 tuple.TupleOffset = 0;
340 tuple.DesiredTuple = CISTPL_CONFIG;
341 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
342 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
343 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
344 link->conf.ConfigBase = parse.config.base;
347 link->state |= DEV_CONFIG;
349 DEBUG(1,"arcnet: baseport1 is %Xh\n", link->io.BasePort1);
351 if (!link->io.BasePort1)
353 for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10)
355 link->io.BasePort1 = ioaddr;
356 i = pcmcia_request_io(link->handle, &link->io);
362 i = pcmcia_request_io(link->handle, &link->io);
366 DEBUG(1,"arcnet: requestIO failed totally!\n");
370 ioaddr = dev->base_addr = link->io.BasePort1;
371 DEBUG(1,"arcnet: got ioaddr %Xh\n", ioaddr);
373 DEBUG(1,"arcnet: request IRQ %d (%Xh/%Xh)\n",
374 link->irq.AssignedIRQ,
375 link->irq.IRQInfo1, link->irq.IRQInfo2);
376 i = pcmcia_request_irq(link->handle, &link->irq);
379 DEBUG(1,"arcnet: requestIRQ failed totally!\n");
383 dev->irq = link->irq.AssignedIRQ;
385 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
387 if (com20020_check(dev))
394 lp->card_name = "PCMCIA COM20020";
395 lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
397 link->dev = &info->node;
398 link->state &= ~DEV_CONFIG_PENDING;
400 i = com20020_found(dev, 0); /* calls register_netdev */
403 DEBUG(1,KERN_NOTICE "com20020_cs: com20020_found() failed\n");
408 strcpy(info->node.dev_name, dev->name);
410 DEBUG(1,KERN_INFO "%s: port %#3lx, irq %d\n",
411 dev->name, dev->base_addr, dev->irq);
415 cs_error(link->handle, last_fn, last_ret);
417 DEBUG(1,"com20020_config failed...\n");
418 com20020_release(link);
419 } /* com20020_config */
421 /*======================================================================
423 After a card is removed, com20020_release() will unregister the net
424 device, and release the PCMCIA configuration. If the device is
425 still open, this will be postponed until it is closed.
427 ======================================================================*/
429 static void com20020_release(dev_link_t *link)
432 DEBUG(1,"release...\n");
434 DEBUG(0, "com20020_release(0x%p)\n", link);
436 pcmcia_release_configuration(link->handle);
437 pcmcia_release_io(link->handle, &link->io);
438 pcmcia_release_irq(link->handle, &link->irq);
440 link->state &= ~(DEV_CONFIG | DEV_RELEASE_PENDING);
443 /*======================================================================
445 The card status event handler. Mostly, this schedules other
446 stuff to run after an event is received. A CARD_REMOVAL event
447 also sets some flags to discourage the net drivers from trying
448 to talk to the card any more.
450 ======================================================================*/
452 static int com20020_event(event_t event, int priority,
453 event_callback_args_t *args)
455 dev_link_t *link = args->client_data;
456 com20020_dev_t *info = link->priv;
457 struct net_device *dev = info->dev;
459 DEBUG(1, "com20020_event(0x%06x)\n", event);
462 case CS_EVENT_CARD_REMOVAL:
463 link->state &= ~DEV_PRESENT;
464 if (link->state & DEV_CONFIG) {
465 netif_device_detach(dev);
466 link->state |= DEV_RELEASE_PENDING;
469 case CS_EVENT_CARD_INSERTION:
470 link->state |= DEV_PRESENT;
471 com20020_config(link);
473 case CS_EVENT_PM_SUSPEND:
474 link->state |= DEV_SUSPEND;
475 /* Fall through... */
476 case CS_EVENT_RESET_PHYSICAL:
477 if (link->state & DEV_CONFIG) {
479 netif_device_detach(dev);
481 pcmcia_release_configuration(link->handle);
484 case CS_EVENT_PM_RESUME:
485 link->state &= ~DEV_SUSPEND;
486 /* Fall through... */
487 case CS_EVENT_CARD_RESET:
488 if (link->state & DEV_CONFIG) {
489 pcmcia_request_configuration(link->handle, &link->conf);
491 int ioaddr = dev->base_addr;
492 struct arcnet_local *lp = dev->priv;
499 } /* com20020_event */
503 static struct pcmcia_driver com20020_cs_driver = {
504 .owner = THIS_MODULE,
506 .name = "com20020_cs",
508 .attach = com20020_attach,
509 .detach = com20020_detach,
512 static int __init init_com20020_cs(void)
514 return pcmcia_register_driver(&com20020_cs_driver);
517 static void __exit exit_com20020_cs(void)
519 pcmcia_unregister_driver(&com20020_cs_driver);
520 while (dev_list != NULL)
521 com20020_detach(dev_list);
524 module_init(init_com20020_cs);
525 module_exit(exit_com20020_cs);