net: sealevel: remove redundant blank lines
[linux-2.6-microblaze.git] / drivers / net / wan / sealevel.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      Sealevel Systems 4021 driver.
4  *
5  *      (c) Copyright 1999, 2001 Alan Cox
6  *      (c) Copyright 2001 Red Hat Inc.
7  *      Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/delay.h>
20 #include <linux/hdlc.h>
21 #include <linux/ioport.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <net/arp.h>
25
26 #include <asm/irq.h>
27 #include <asm/io.h>
28 #include <asm/dma.h>
29 #include <asm/byteorder.h>
30 #include "z85230.h"
31
32 struct slvl_device
33 {
34         struct z8530_channel *chan;
35         int channel;
36 };
37
38 struct slvl_board
39 {
40         struct slvl_device dev[2];
41         struct z8530_dev board;
42         int iobase;
43 };
44
45 /*
46  *      Network driver support routines
47  */
48
49 static inline struct slvl_device* dev_to_chan(struct net_device *dev)
50 {
51         return (struct slvl_device *)dev_to_hdlc(dev)->priv;
52 }
53
54 /*
55  *      Frame receive. Simple for our card as we do HDLC and there
56  *      is no funny garbage involved
57  */
58
59 static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
60 {
61         /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
62         skb_trim(skb, skb->len - 2);
63         skb->protocol = hdlc_type_trans(skb, c->netdevice);
64         skb_reset_mac_header(skb);
65         skb->dev = c->netdevice;
66         netif_rx(skb);
67 }
68
69 /*
70  *      We've been placed in the UP state
71  */
72
73 static int sealevel_open(struct net_device *d)
74 {
75         struct slvl_device *slvl = dev_to_chan(d);
76         int err = -1;
77         int unit = slvl->channel;
78
79         /*
80          *      Link layer up.
81          */
82
83         switch (unit) {
84                 case 0:
85                         err = z8530_sync_dma_open(d, slvl->chan);
86                         break;
87                 case 1:
88                         err = z8530_sync_open(d, slvl->chan);
89                         break;
90         }
91
92         if (err)
93                 return err;
94
95         err = hdlc_open(d);
96         if (err) {
97                 switch (unit) {
98                         case 0:
99                                 z8530_sync_dma_close(d, slvl->chan);
100                                 break;
101                         case 1:
102                                 z8530_sync_close(d, slvl->chan);
103                                 break;
104                 }
105                 return err;
106         }
107
108         slvl->chan->rx_function = sealevel_input;
109
110         /*
111          *      Go go go
112          */
113         netif_start_queue(d);
114         return 0;
115 }
116
117 static int sealevel_close(struct net_device *d)
118 {
119         struct slvl_device *slvl = dev_to_chan(d);
120         int unit = slvl->channel;
121
122         /*
123          *      Discard new frames
124          */
125
126         slvl->chan->rx_function = z8530_null_rx;
127
128         hdlc_close(d);
129         netif_stop_queue(d);
130
131         switch (unit) {
132                 case 0:
133                         z8530_sync_dma_close(d, slvl->chan);
134                         break;
135                 case 1:
136                         z8530_sync_close(d, slvl->chan);
137                         break;
138         }
139         return 0;
140 }
141
142 static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
143 {
144         /* struct slvl_device *slvl=dev_to_chan(d);
145            z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
146         return hdlc_ioctl(d, ifr, cmd);
147 }
148
149 /*
150  *      Passed network frames, fire them downwind.
151  */
152
153 static netdev_tx_t sealevel_queue_xmit(struct sk_buff *skb,
154                                              struct net_device *d)
155 {
156         return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
157 }
158
159 static int sealevel_attach(struct net_device *dev, unsigned short encoding,
160                            unsigned short parity)
161 {
162         if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
163                 return 0;
164         return -EINVAL;
165 }
166
167 static const struct net_device_ops sealevel_ops = {
168         .ndo_open       = sealevel_open,
169         .ndo_stop       = sealevel_close,
170         .ndo_start_xmit = hdlc_start_xmit,
171         .ndo_do_ioctl   = sealevel_ioctl,
172 };
173
174 static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
175 {
176         struct net_device *dev = alloc_hdlcdev(sv);
177         if (!dev)
178                 return -1;
179
180         dev_to_hdlc(dev)->attach = sealevel_attach;
181         dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
182         dev->netdev_ops = &sealevel_ops;
183         dev->base_addr = iobase;
184         dev->irq = irq;
185
186         if (register_hdlc_device(dev)) {
187                 pr_err("unable to register HDLC device\n");
188                 free_netdev(dev);
189                 return -1;
190         }
191
192         sv->chan->netdevice = dev;
193         return 0;
194 }
195
196 /*
197  *      Allocate and setup Sealevel board.
198  */
199
200 static __init struct slvl_board *slvl_init(int iobase, int irq,
201                                            int txdma, int rxdma, int slow)
202 {
203         struct z8530_dev *dev;
204         struct slvl_board *b;
205
206         /*
207          *      Get the needed I/O space
208          */
209
210         if (!request_region(iobase, 8, "Sealevel 4021")) {
211                 pr_warn("I/O 0x%X already in use\n", iobase);
212                 return NULL;
213         }
214
215         b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
216         if (!b)
217                 goto err_kzalloc;
218
219         b->dev[0].chan = &b->board.chanA;
220         b->dev[0].channel = 0;
221
222         b->dev[1].chan = &b->board.chanB;
223         b->dev[1].channel = 1;
224
225         dev = &b->board;
226
227         /*
228          *      Stuff in the I/O addressing
229          */
230
231         dev->active = 0;
232
233         b->iobase = iobase;
234
235         /*
236          *      Select 8530 delays for the old board
237          */
238
239         if (slow)
240                 iobase |= Z8530_PORT_SLEEP;
241
242         dev->chanA.ctrlio = iobase + 1;
243         dev->chanA.dataio = iobase;
244         dev->chanB.ctrlio = iobase + 3;
245         dev->chanB.dataio = iobase + 2;
246
247         dev->chanA.irqs = &z8530_nop;
248         dev->chanB.irqs = &z8530_nop;
249
250         /*
251          *      Assert DTR enable DMA
252          */
253
254         outb(3 | (1 << 7), b->iobase + 4);
255
256         /* We want a fast IRQ for this device. Actually we'd like an even faster
257            IRQ ;) - This is one driver RtLinux is made for */
258
259         if (request_irq(irq, z8530_interrupt, 0,
260                         "SeaLevel", dev) < 0) {
261                 pr_warn("IRQ %d already in use\n", irq);
262                 goto err_request_irq;
263         }
264
265         dev->irq = irq;
266         dev->chanA.private = &b->dev[0];
267         dev->chanB.private = &b->dev[1];
268         dev->chanA.dev = dev;
269         dev->chanB.dev = dev;
270
271         dev->chanA.txdma = 3;
272         dev->chanA.rxdma = 1;
273         if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
274                 goto err_dma_tx;
275
276         if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
277                 goto err_dma_rx;
278
279         disable_irq(irq);
280
281         /*
282          *      Begin normal initialise
283          */
284
285         if (z8530_init(dev) != 0) {
286                 pr_err("Z8530 series device not found\n");
287                 enable_irq(irq);
288                 goto free_hw;
289         }
290         if (dev->type == Z85C30) {
291                 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
292                 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
293         } else {
294                 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
295                 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
296         }
297
298         /*
299          *      Now we can take the IRQ
300          */
301
302         enable_irq(irq);
303
304         if (slvl_setup(&b->dev[0], iobase, irq))
305                 goto free_hw;
306         if (slvl_setup(&b->dev[1], iobase, irq))
307                 goto free_netdev0;
308
309         z8530_describe(dev, "I/O", iobase);
310         dev->active = 1;
311         return b;
312
313 free_netdev0:
314         unregister_hdlc_device(b->dev[0].chan->netdevice);
315         free_netdev(b->dev[0].chan->netdevice);
316 free_hw:
317         free_dma(dev->chanA.rxdma);
318 err_dma_rx:
319         free_dma(dev->chanA.txdma);
320 err_dma_tx:
321         free_irq(irq, dev);
322 err_request_irq:
323         kfree(b);
324 err_kzalloc:
325         release_region(iobase, 8);
326         return NULL;
327 }
328
329 static void __exit slvl_shutdown(struct slvl_board *b)
330 {
331         int u;
332
333         z8530_shutdown(&b->board);
334
335         for (u = 0; u < 2; u++) {
336                 struct net_device *d = b->dev[u].chan->netdevice;
337                 unregister_hdlc_device(d);
338                 free_netdev(d);
339         }
340
341         free_irq(b->board.irq, &b->board);
342         free_dma(b->board.chanA.rxdma);
343         free_dma(b->board.chanA.txdma);
344         /* DMA off on the card, drop DTR */
345         outb(0, b->iobase);
346         release_region(b->iobase, 8);
347         kfree(b);
348 }
349
350 static int io=0x238;
351 static int txdma=1;
352 static int rxdma=3;
353 static int irq=5;
354 static bool slow=false;
355
356 module_param_hw(io, int, ioport, 0);
357 MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
358 module_param_hw(txdma, int, dma, 0);
359 MODULE_PARM_DESC(txdma, "Transmit DMA channel");
360 module_param_hw(rxdma, int, dma, 0);
361 MODULE_PARM_DESC(rxdma, "Receive DMA channel");
362 module_param_hw(irq, int, irq, 0);
363 MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
364 module_param(slow, bool, 0);
365 MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
366
367 MODULE_AUTHOR("Alan Cox");
368 MODULE_LICENSE("GPL");
369 MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
370
371 static struct slvl_board *slvl_unit;
372
373 static int __init slvl_init_module(void)
374 {
375         slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
376
377         return slvl_unit ? 0 : -ENODEV;
378 }
379
380 static void __exit slvl_cleanup_module(void)
381 {
382         if (slvl_unit)
383                 slvl_shutdown(slvl_unit);
384 }
385
386 module_init(slvl_init_module);
387 module_exit(slvl_cleanup_module);