1/*
2 * Linux ARCnet driver - COM20020 PCMCIA support
3 *
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.
9 *
10 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
11 *  for sponsoring the further development of this driver.
12 *
13 * **********************
14 *
15 * The original copyright of skeleton.c was as follows:
16 *
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.
22 *
23 * **********************
24 * Changes:
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 * **********************
29 *
30 * For more details, see drivers/net/arcnet.c
31 *
32 * **********************
33 */
34
35#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
36
37#include <linux/kernel.h>
38#include <linux/ptrace.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/timer.h>
42#include <linux/delay.h>
43#include <linux/module.h>
44#include <linux/netdevice.h>
45#include <linux/io.h>
46#include <pcmcia/cistpl.h>
47#include <pcmcia/ds.h>
48
49#include "arcdevice.h"
50#include "com20020.h"
51
52static void regdump(struct net_device *dev)
53{
54#ifdef DEBUG
55	int ioaddr = dev->base_addr;
56	int count;
57
58	netdev_dbg(dev, "register dump:\n");
59	for (count = 0; count < 16; count++) {
60		if (!(count % 16))
61			pr_cont("%04X:", ioaddr + count);
62		pr_cont(" %02X", arcnet_inb(ioaddr, count));
63	}
64	pr_cont("\n");
65
66	netdev_dbg(dev, "buffer0 dump:\n");
67	/* set up the address register */
68	count = 0;
69	arcnet_outb((count >> 8) | RDDATAflag | AUTOINCflag,
70		    ioaddr, COM20020_REG_W_ADDR_HI);
71	arcnet_outb(count & 0xff, ioaddr, COM20020_REG_W_ADDR_LO);
72
73	for (count = 0; count < 256 + 32; count++) {
74		if (!(count % 16))
75			pr_cont("%04X:", count);
76
77		/* copy the data */
78		pr_cont(" %02X", arcnet_inb(ioaddr, COM20020_REG_RW_MEMDATA));
79	}
80	pr_cont("\n");
81#endif
82}
83
84/*====================================================================*/
85
86/* Parameters that can be set with 'insmod' */
87
88static int node;
89static int timeout = 3;
90static int backplane;
91static int clockp;
92static int clockm;
93
94module_param(node, int, 0);
95module_param(timeout, int, 0);
96module_param(backplane, int, 0);
97module_param(clockp, int, 0);
98module_param(clockm, int, 0);
99
100MODULE_LICENSE("GPL");
101
102/*====================================================================*/
103
104static int com20020_config(struct pcmcia_device *link);
105static void com20020_release(struct pcmcia_device *link);
106
107static void com20020_detach(struct pcmcia_device *p_dev);
108
109/*====================================================================*/
110
111static int com20020_probe(struct pcmcia_device *p_dev)
112{
113	struct com20020_dev *info;
114	struct net_device *dev;
115	struct arcnet_local *lp;
116	int ret = -ENOMEM;
117
118	dev_dbg(&p_dev->dev, "com20020_attach()\n");
119
120	/* Create new network device */
121	info = kzalloc(sizeof(*info), GFP_KERNEL);
122	if (!info)
123		goto fail_alloc_info;
124
125	dev = alloc_arcdev("");
126	if (!dev)
127		goto fail_alloc_dev;
128
129	lp = netdev_priv(dev);
130	lp->timeout = timeout;
131	lp->backplane = backplane;
132	lp->clockp = clockp;
133	lp->clockm = clockm & 3;
134	lp->hw.owner = THIS_MODULE;
135
136	/* fill in our module parameters as defaults */
137	arcnet_set_addr(dev, node);
138
139	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
140	p_dev->resource[0]->end = 16;
141	p_dev->config_flags |= CONF_ENABLE_IRQ;
142
143	info->dev = dev;
144	p_dev->priv = info;
145
146	ret = com20020_config(p_dev);
147	if (ret)
148		goto fail_config;
149
150	return 0;
151
152fail_config:
153	free_arcdev(dev);
154fail_alloc_dev:
155	kfree(info);
156fail_alloc_info:
157	return ret;
158} /* com20020_attach */
159
160static void com20020_detach(struct pcmcia_device *link)
161{
162	struct com20020_dev *info = link->priv;
163	struct net_device *dev = info->dev;
164
165	dev_dbg(&link->dev, "detach...\n");
166
167	dev_dbg(&link->dev, "com20020_detach\n");
168
169	dev_dbg(&link->dev, "unregister...\n");
170
171	unregister_netdev(dev);
172
173	/* this is necessary because we register our IRQ separately
174	 * from card services.
175	 */
176	if (dev->irq)
177		free_irq(dev->irq, dev);
178
179	com20020_release(link);
180
181	/* Unlink device structure, free bits */
182	dev_dbg(&link->dev, "unlinking...\n");
183	if (link->priv) {
184		dev = info->dev;
185		if (dev) {
186			dev_dbg(&link->dev, "kfree...\n");
187			free_arcdev(dev);
188		}
189		dev_dbg(&link->dev, "kfree2...\n");
190		kfree(info);
191	}
192
193} /* com20020_detach */
194
195static int com20020_config(struct pcmcia_device *link)
196{
197	struct arcnet_local *lp;
198	struct com20020_dev *info;
199	struct net_device *dev;
200	int i, ret;
201	int ioaddr;
202
203	info = link->priv;
204	dev = info->dev;
205
206	dev_dbg(&link->dev, "config...\n");
207
208	dev_dbg(&link->dev, "com20020_config\n");
209
210	dev_dbg(&link->dev, "baseport1 is %Xh\n",
211		(unsigned int)link->resource[0]->start);
212
213	i = -ENODEV;
214	link->io_lines = 16;
215
216	if (!link->resource[0]->start) {
217		for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10) {
218			link->resource[0]->start = ioaddr;
219			i = pcmcia_request_io(link);
220			if (i == 0)
221				break;
222		}
223	} else {
224		i = pcmcia_request_io(link);
225	}
226
227	if (i != 0) {
228		dev_dbg(&link->dev, "requestIO failed totally!\n");
229		goto failed;
230	}
231
232	ioaddr = dev->base_addr = link->resource[0]->start;
233	dev_dbg(&link->dev, "got ioaddr %Xh\n", ioaddr);
234
235	dev_dbg(&link->dev, "request IRQ %d\n",
236		link->irq);
237	if (!link->irq) {
238		dev_dbg(&link->dev, "requestIRQ failed totally!\n");
239		goto failed;
240	}
241
242	dev->irq = link->irq;
243
244	ret = pcmcia_enable_device(link);
245	if (ret)
246		goto failed;
247
248	if (com20020_check(dev)) {
249		regdump(dev);
250		goto failed;
251	}
252
253	lp = netdev_priv(dev);
254	lp->card_name = "PCMCIA COM20020";
255	lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
256
257	SET_NETDEV_DEV(dev, &link->dev);
258
259	i = com20020_found(dev, 0);	/* calls register_netdev */
260
261	if (i != 0) {
262		dev_notice(&link->dev,
263			   "com20020_found() failed\n");
264		goto failed;
265	}
266
267	netdev_dbg(dev, "port %#3lx, irq %d\n",
268		   dev->base_addr, dev->irq);
269	return 0;
270
271failed:
272	dev_dbg(&link->dev, "com20020_config failed...\n");
273	com20020_release(link);
274	return -ENODEV;
275} /* com20020_config */
276
277static void com20020_release(struct pcmcia_device *link)
278{
279	dev_dbg(&link->dev, "com20020_release\n");
280	pcmcia_disable_device(link);
281}
282
283static int com20020_suspend(struct pcmcia_device *link)
284{
285	struct com20020_dev *info = link->priv;
286	struct net_device *dev = info->dev;
287
288	if (link->open)
289		netif_device_detach(dev);
290
291	return 0;
292}
293
294static int com20020_resume(struct pcmcia_device *link)
295{
296	struct com20020_dev *info = link->priv;
297	struct net_device *dev = info->dev;
298
299	if (link->open) {
300		int ioaddr = dev->base_addr;
301		struct arcnet_local *lp = netdev_priv(dev);
302
303		arcnet_outb(lp->config | 0x80, ioaddr, COM20020_REG_W_CONFIG);
304		udelay(5);
305		arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
306	}
307
308	return 0;
309}
310
311static const struct pcmcia_device_id com20020_ids[] = {
312	PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.",
313				"PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
314	PCMCIA_DEVICE_PROD_ID12("SoHard AG",
315				"SH ARC PCMCIA", 0xf8991729, 0x69dff0c7),
316	PCMCIA_DEVICE_NULL
317};
318MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
319
320static struct pcmcia_driver com20020_cs_driver = {
321	.owner		= THIS_MODULE,
322	.name		= "com20020_cs",
323	.probe		= com20020_probe,
324	.remove		= com20020_detach,
325	.id_table	= com20020_ids,
326	.suspend	= com20020_suspend,
327	.resume		= com20020_resume,
328};
329module_pcmcia_driver(com20020_cs_driver);
330