staging: cxt1e1: fix checkpatch error 'assignment in if condition'
[linux.git] / drivers / staging / cxt1e1 / linux.c
1 /* Copyright (C) 2007-2008  One Stop Systems
2  * Copyright (C) 2003-2006  SBE, Inc.
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/types.h>
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
20 #include <linux/hdlc.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <asm/uaccess.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/skbuff.h>
26 #include "pmcc4_sysdep.h"
27 #include "sbecom_inline_linux.h"
28 #include "libsbew.h"
29 #include "pmcc4.h"
30 #include "pmcc4_ioctls.h"
31 #include "pmcc4_private.h"
32 #include "sbeproc.h"
33
34 /*******************************************************************************
35  * Error out early if we have compiler trouble.
36  *
37  *   (This section is included from the kernel's init/main.c as a friendly
38  *   spiderman recommendation...)
39  *
40  * Versions of gcc older than that listed below may actually compile and link
41  * okay, but the end product can have subtle run time bugs.  To avoid associated
42  * bogus bug reports, we flatly refuse to compile with a gcc that is known to be
43  * too old from the very beginning.
44  */
45 #if (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 2)
46 #error Sorry, your GCC is too old. It builds incorrect kernels.
47 #endif
48
49 #if __GNUC__ == 4 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ == 0
50 #warning gcc-4.1.0 is known to miscompile the kernel.  A different compiler version is recommended.
51 #endif
52
53 /*******************************************************************************/
54
55 #define CHANNAME "hdlc"
56
57 /*******************************************************************/
58 /* forward references */
59 status_t    c4_chan_work_init(mpi_t *, mch_t *);
60 void        musycc_wq_chan_restart(void *);
61 status_t __init c4_init(ci_t *, u_char *, u_char *);
62 status_t __init c4_init2(ci_t *);
63 ci_t       *__init c4_new(void *);
64 int __init  c4hw_attach_all(void);
65 void __init hdw_sn_get(hdw_info_t *, int);
66
67 #ifdef CONFIG_SBE_PMCC4_NCOMM
68 irqreturn_t c4_ebus_intr_th_handler(void *);
69
70 #endif
71 int         c4_frame_rw(ci_t *, struct sbecom_port_param *);
72 status_t    c4_get_port(ci_t *, int);
73 int         c4_loop_port(ci_t *, int, u_int8_t);
74 int         c4_musycc_rw(ci_t *, struct c4_musycc_param *);
75 int         c4_new_chan(ci_t *, int, int, void *);
76 status_t    c4_set_port(ci_t *, int);
77 int         c4_pld_rw(ci_t *, struct sbecom_port_param *);
78 void        cleanup_devs(void);
79 void        cleanup_ioremap(void);
80 status_t    musycc_chan_down(ci_t *, int);
81 irqreturn_t musycc_intr_th_handler(void *);
82 int         musycc_start_xmit(ci_t *, int, void *);
83
84 extern ci_t *CI;
85 extern struct s_hdw_info hdw_info[];
86
87 #if defined(CONFIG_SBE_HDLC_V7) || defined(CONFIG_SBE_WAN256T3_HDLC_V7) || \
88         defined(CONFIG_SBE_HDLC_V7_MODULE) || defined(CONFIG_SBE_WAN256T3_HDLC_V7_MODULE)
89 #define _v7_hdlc_  1
90 #else
91 #define _v7_hdlc_  0
92 #endif
93
94 #if _v7_hdlc_
95 #define V7(x) (x ## _v7)
96 extern int  hdlc_netif_rx_v7(hdlc_device *, struct sk_buff *);
97 extern int  register_hdlc_device_v7(hdlc_device *);
98 extern int  unregister_hdlc_device_v7(hdlc_device *);
99
100 #else
101 #define V7(x) x
102 #endif
103
104 int         error_flag;         /* module load error reporting */
105 int         cxt1e1_log_level = LOG_ERROR;
106 int         log_level_default = LOG_ERROR;
107 module_param(cxt1e1_log_level, int, 0444);
108
109 int         cxt1e1_max_mru = MUSYCC_MRU;
110 int         max_mru_default = MUSYCC_MRU;
111 module_param(cxt1e1_max_mru, int, 0444);
112
113 int         cxt1e1_max_mtu = MUSYCC_MTU;
114 int         max_mtu_default = MUSYCC_MTU;
115 module_param(cxt1e1_max_mtu, int, 0444);
116
117 int         max_txdesc_used = MUSYCC_TXDESC_MIN;
118 int         max_txdesc_default = MUSYCC_TXDESC_MIN;
119 module_param(max_txdesc_used, int, 0444);
120
121 int         max_rxdesc_used = MUSYCC_RXDESC_MIN;
122 int         max_rxdesc_default = MUSYCC_RXDESC_MIN;
123 module_param(max_rxdesc_used, int, 0444);
124
125 /****************************************************************************/
126 /****************************************************************************/
127 /****************************************************************************/
128
129 void *
130 getuserbychan(int channum)
131 {
132         mch_t      *ch;
133
134         ch = c4_find_chan(channum);
135         return ch ? ch->user : NULL;
136 }
137
138
139 char *
140 get_hdlc_name(hdlc_device *hdlc)
141 {
142         struct c4_priv *priv = hdlc->priv;
143         struct net_device *dev = getuserbychan(priv->channum);
144
145         return dev->name;
146 }
147
148
149 static status_t
150 mkret(int bsd)
151 {
152         if (bsd > 0)
153                 return -bsd;
154         else
155                 return bsd;
156 }
157
158 /***************************************************************************/
159 #include <linux/workqueue.h>
160
161 /***
162  * One workqueue (wq) per port (since musycc allows simultaneous group
163  * commands), with individual data for each channel:
164  *
165  *   mpi_t -> struct workqueue_struct *wq_port;  (dynamically allocated using
166  *                                               create_workqueue())
167  *
168  * With work structure (work) statically allocated for each channel:
169  *
170  *   mch_t -> struct work_struct ch_work;  (statically allocated using ???)
171  *
172  ***/
173
174
175 /*
176  * Called by the start transmit routine when a channel TX_ENABLE is to be
177  * issued.  This queues the transmission start request among other channels
178  * within a port's group.
179  */
180 void
181 c4_wk_chan_restart(mch_t *ch)
182 {
183         mpi_t      *pi = ch->up;
184
185 #ifdef RLD_RESTART_DEBUG
186         pr_info(">> %s: queueing Port %d Chan %d, mch_t @ %p\n",
187                 __func__, pi->portnum, ch->channum, ch);
188 #endif
189
190         /* create new entry w/in workqueue for this channel and let'er rip */
191
192         /** queue_work(struct workqueue_struct *queue,
193          **            struct work_struct *work);
194          **/
195         queue_work(pi->wq_port, &ch->ch_work);
196 }
197
198 status_t
199 c4_wk_chan_init(mpi_t *pi, mch_t *ch)
200 {
201         /*
202          * this will be used to restart a stopped channel
203          */
204
205         /** INIT_WORK(struct work_struct *work,
206          **           void (*function)(void *),
207          **           void *data);
208          **/
209         INIT_WORK(&ch->ch_work, (void *)musycc_wq_chan_restart);
210         return 0;                       /* success */
211 }
212
213 status_t
214 c4_wq_port_init(mpi_t *pi)
215 {
216
217         char        name[16], *np;  /* NOTE: name of the queue limited by system
218                                      * to 10 characters */
219
220         if (pi->wq_port)
221                 return 0;                   /* already initialized */
222
223         np = name;
224         memset(name, 0, 16);
225         sprintf(np, "%s%d", pi->up->devname, pi->portnum); /* IE pmcc4-01) */
226
227 #ifdef RLD_RESTART_DEBUG
228         pr_info(">> %s: creating workqueue <%s> for Port %d.\n",
229                 __func__, name, pi->portnum); /* RLD DEBUG */
230 #endif
231         pi->wq_port = create_singlethread_workqueue(name);
232         if (!pi->wq_port)
233                 return -ENOMEM;
234         return 0;                       /* success */
235 }
236
237 void
238 c4_wq_port_cleanup(mpi_t *pi)
239 {
240         /*
241          * PORT POINT: cannot call this if WQ is statically allocated w/in
242          * structure since it calls kfree(wq);
243          */
244         if (pi->wq_port)
245         {
246                 destroy_workqueue(pi->wq_port);        /* this also calls
247                                                         * flush_workqueue() */
248                 pi->wq_port = NULL;
249         }
250 }
251
252 /***************************************************************************/
253
254 irqreturn_t
255 c4_linux_interrupt(int irq, void *dev_instance)
256 {
257         struct net_device *ndev = dev_instance;
258
259         return musycc_intr_th_handler(netdev_priv(ndev));
260 }
261
262
263 #ifdef CONFIG_SBE_PMCC4_NCOMM
264 irqreturn_t
265 c4_ebus_interrupt(int irq, void *dev_instance)
266 {
267         struct net_device *ndev = dev_instance;
268
269         return c4_ebus_intr_th_handler(netdev_priv(ndev));
270 }
271 #endif
272
273
274 static int
275 void_open(struct net_device *ndev)
276 {
277         pr_info("%s: trying to open master device !\n", ndev->name);
278         return -1;
279 }
280
281
282 static int
283 chan_open(struct net_device *ndev)
284 {
285         hdlc_device *hdlc = dev_to_hdlc(ndev);
286         const struct c4_priv *priv = hdlc->priv;
287         int         ret;
288
289         ret = hdlc_open(ndev);
290         if (ret) {
291                 pr_info("hdlc_open failure, err %d.\n", ret);
292                 return ret;
293         }
294
295         ret = c4_chan_up(priv->ci, priv->channum);
296         if (ret)
297                 return -ret;
298         try_module_get(THIS_MODULE);
299         netif_start_queue(ndev);
300         return 0;                       /* no error = success */
301 }
302
303
304 static int
305 chan_close(struct net_device *ndev)
306 {
307         hdlc_device *hdlc = dev_to_hdlc(ndev);
308         const struct c4_priv *priv = hdlc->priv;
309
310         netif_stop_queue(ndev);
311         musycc_chan_down((ci_t *) 0, priv->channum);
312         hdlc_close(ndev);
313         module_put(THIS_MODULE);
314         return 0;
315 }
316
317
318 static int
319 chan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
320 {
321         return hdlc_ioctl(dev, ifr, cmd);
322 }
323
324
325 static int
326 chan_attach_noop(struct net_device *ndev, unsigned short foo_1,
327                  unsigned short foo_2)
328 {
329         /* our driver has nothing to do here, show's
330          * over, go home
331          */
332         return 0;
333 }
334
335
336 static struct net_device_stats *
337 chan_get_stats(struct net_device *ndev)
338 {
339         mch_t      *ch;
340         struct net_device_stats *nstats;
341         struct sbecom_chan_stats *stats;
342         int         channum;
343
344         {
345                 struct c4_priv *priv;
346
347                 priv = (struct c4_priv *)dev_to_hdlc(ndev)->priv;
348                 channum = priv->channum;
349         }
350
351         ch = c4_find_chan(channum);
352         if (ch == NULL)
353                 return NULL;
354
355         nstats = &ndev->stats;
356         stats = &ch->s;
357
358         memset(nstats, 0, sizeof(struct net_device_stats));
359         nstats->rx_packets = stats->rx_packets;
360         nstats->tx_packets = stats->tx_packets;
361         nstats->rx_bytes = stats->rx_bytes;
362         nstats->tx_bytes = stats->tx_bytes;
363         nstats->rx_errors = stats->rx_length_errors +
364                 stats->rx_over_errors +
365                 stats->rx_crc_errors +
366                 stats->rx_frame_errors +
367                 stats->rx_fifo_errors +
368                 stats->rx_missed_errors;
369         nstats->tx_errors = stats->tx_dropped +
370                 stats->tx_aborted_errors +
371                 stats->tx_fifo_errors;
372         nstats->rx_dropped = stats->rx_dropped;
373         nstats->tx_dropped = stats->tx_dropped;
374
375         nstats->rx_length_errors = stats->rx_length_errors;
376         nstats->rx_over_errors = stats->rx_over_errors;
377         nstats->rx_crc_errors = stats->rx_crc_errors;
378         nstats->rx_frame_errors = stats->rx_frame_errors;
379         nstats->rx_fifo_errors = stats->rx_fifo_errors;
380         nstats->rx_missed_errors = stats->rx_missed_errors;
381
382         nstats->tx_aborted_errors = stats->tx_aborted_errors;
383         nstats->tx_fifo_errors = stats->tx_fifo_errors;
384
385         return nstats;
386 }
387
388
389 static ci_t *
390 get_ci_by_dev(struct net_device *ndev)
391 {
392         return (ci_t *)(netdev_priv(ndev));
393 }
394
395
396 static int
397 c4_linux_xmit(struct sk_buff *skb, struct net_device *ndev)
398 {
399         const struct c4_priv *priv;
400         int         rval;
401
402         hdlc_device *hdlc = dev_to_hdlc(ndev);
403
404         priv = hdlc->priv;
405
406         rval = musycc_start_xmit(priv->ci, priv->channum, skb);
407         return rval;
408 }
409
410 static const struct net_device_ops chan_ops = {
411         .ndo_open       = chan_open,
412         .ndo_stop       = chan_close,
413         .ndo_start_xmit = c4_linux_xmit,
414         .ndo_do_ioctl   = chan_dev_ioctl,
415         .ndo_get_stats  = chan_get_stats,
416 };
417
418 static struct net_device *
419 create_chan(struct net_device *ndev, ci_t *ci,
420             struct sbecom_chan_param *cp)
421 {
422         hdlc_device *hdlc;
423         struct net_device *dev;
424         hdw_info_t *hi;
425         int         ret;
426
427         if (c4_find_chan(cp->channum))
428                 return NULL;                   /* channel already exists */
429
430         {
431                 struct c4_priv *priv;
432
433                 /* allocate then fill in private data structure */
434                 priv = OS_kmalloc(sizeof(struct c4_priv));
435                 if (!priv)
436                 {
437                         pr_warning("%s: no memory for net_device !\n",
438                                    ci->devname);
439                         return NULL;
440                 }
441                 dev = alloc_hdlcdev(priv);
442                 if (!dev)
443                 {
444                         pr_warning("%s: no memory for hdlc_device !\n",
445                                    ci->devname);
446                         OS_kfree(priv);
447                         return NULL;
448                 }
449                 priv->ci = ci;
450                 priv->channum = cp->channum;
451         }
452
453         hdlc = dev_to_hdlc(dev);
454
455         dev->base_addr = 0;             /* not I/O mapped */
456         dev->irq = ndev->irq;
457         dev->type = ARPHRD_RAWHDLC;
458         *dev->name = 0;                 /* default ifconfig name = "hdlc" */
459
460         hi = (hdw_info_t *)ci->hdw_info;
461         if (hi->mfg_info_sts == EEPROM_OK)
462         {
463                 switch (hi->promfmt)
464                 {
465                 case PROM_FORMAT_TYPE1:
466                         memcpy(dev->dev_addr,
467                                (FLD_TYPE1 *) (hi->mfg_info.pft1.Serial), 6);
468                         break;
469                 case PROM_FORMAT_TYPE2:
470                         memcpy(dev->dev_addr,
471                                (FLD_TYPE2 *) (hi->mfg_info.pft2.Serial), 6);
472                         break;
473                 default:
474                         memset(dev->dev_addr, 0, 6);
475                         break;
476                 }
477         } else
478         {
479                 memset(dev->dev_addr, 0, 6);
480         }
481
482         hdlc->xmit = c4_linux_xmit;
483
484         dev->netdev_ops = &chan_ops;
485         /*
486          * The native hdlc stack calls this 'attach' routine during
487          * hdlc_raw_ioctl(), passing parameters for line encoding and parity.
488          * Since hdlc_raw_ioctl() stack does not interrogate whether an 'attach'
489          * routine is actually registered or not, we supply a dummy routine which
490          * does nothing (since encoding and parity are setup for our driver via a
491          * special configuration application).
492          */
493
494         hdlc->attach = chan_attach_noop;
495
496         /* needed due to Ioctl calling sequence */
497         rtnl_unlock();
498         ret = register_hdlc_device(dev);
499         /* NOTE: <stats> setting must occur AFTER registration in order to "take" */
500         dev->tx_queue_len = MAX_DEFAULT_IFQLEN;
501
502         /* needed due to Ioctl calling sequence */
503         rtnl_lock();
504         if (ret)
505         {
506                 if (cxt1e1_log_level >= LOG_WARN)
507                         pr_info("%s: create_chan[%d] registration error = %d.\n",
508                                 ci->devname, cp->channum, ret);
509                 /* cleanup */
510                 free_netdev(dev);
511                 /* failed to register */
512                 return NULL;
513         }
514         return dev;
515 }
516
517
518 /* the idea here is to get port information and pass it back (using pointer) */
519 static status_t
520 do_get_port(struct net_device *ndev, void *data)
521 {
522         int         ret;
523         ci_t       *ci;             /* ci stands for card information */
524         struct sbecom_port_param pp;/* copy data to kernel land */
525
526         if (copy_from_user(&pp, data, sizeof(struct sbecom_port_param)))
527                 return -EFAULT;
528         if (pp.portnum >= MUSYCC_NPORTS)
529                 return -EFAULT;
530         ci = get_ci_by_dev(ndev);
531         if (!ci)
532                 return -EINVAL;             /* get card info */
533
534         ret = mkret(c4_get_port(ci, pp.portnum));
535         if (ret)
536                 return ret;
537         if (copy_to_user(data, &ci->port[pp.portnum].p,
538                          sizeof(struct sbecom_port_param)))
539                 return -EFAULT;
540         return 0;
541 }
542
543 /* this function copys the user data and then calls the real action function */
544 static status_t
545 do_set_port(struct net_device *ndev, void *data)
546 {
547         ci_t       *ci;             /* ci stands for card information */
548         struct sbecom_port_param pp;/* copy data to kernel land */
549
550         if (copy_from_user(&pp, data, sizeof(struct sbecom_port_param)))
551                 return -EFAULT;
552         if (pp.portnum >= MUSYCC_NPORTS)
553                 return -EFAULT;
554         ci = get_ci_by_dev(ndev);
555         if (!ci)
556                 return -EINVAL;             /* get card info */
557
558         if (pp.portnum >= ci->max_port) /* sanity check */
559                 return -ENXIO;
560
561         memcpy(&ci->port[pp.portnum].p, &pp, sizeof(struct sbecom_port_param));
562         return mkret(c4_set_port(ci, pp.portnum));
563 }
564
565 /* work the port loopback mode as per directed */
566 static status_t
567 do_port_loop(struct net_device *ndev, void *data)
568 {
569         struct sbecom_port_param pp;
570         ci_t       *ci;
571
572         if (copy_from_user(&pp, data, sizeof(struct sbecom_port_param)))
573                 return -EFAULT;
574         ci = get_ci_by_dev(ndev);
575         if (!ci)
576                 return -EINVAL;
577         return mkret(c4_loop_port(ci, pp.portnum, pp.port_mode));
578 }
579
580 /* set the specified register with the given value / or just read it */
581 static status_t
582 do_framer_rw(struct net_device *ndev, void *data)
583 {
584         struct sbecom_port_param pp;
585         ci_t       *ci;
586         int         ret;
587
588         if (copy_from_user(&pp, data, sizeof(struct sbecom_port_param)))
589                 return -EFAULT;
590         ci = get_ci_by_dev(ndev);
591         if (!ci)
592                 return -EINVAL;
593         ret = mkret(c4_frame_rw(ci, &pp));
594         if (ret)
595                 return ret;
596         if (copy_to_user(data, &pp, sizeof(struct sbecom_port_param)))
597                 return -EFAULT;
598         return 0;
599 }
600
601 /* set the specified register with the given value / or just read it */
602 static status_t
603 do_pld_rw(struct net_device *ndev, void *data)
604 {
605         struct sbecom_port_param pp;
606         ci_t       *ci;
607         int         ret;
608
609         if (copy_from_user(&pp, data, sizeof(struct sbecom_port_param)))
610                 return -EFAULT;
611         ci = get_ci_by_dev(ndev);
612         if (!ci)
613                 return -EINVAL;
614         ret = mkret(c4_pld_rw(ci, &pp));
615         if (ret)
616                 return ret;
617         if (copy_to_user(data, &pp, sizeof(struct sbecom_port_param)))
618                 return -EFAULT;
619         return 0;
620 }
621
622 /* set the specified register with the given value / or just read it */
623 static status_t
624 do_musycc_rw(struct net_device *ndev, void *data)
625 {
626         struct c4_musycc_param mp;
627         ci_t       *ci;
628         int         ret;
629
630         if (copy_from_user(&mp, data, sizeof(struct c4_musycc_param)))
631                 return -EFAULT;
632         ci = get_ci_by_dev(ndev);
633         if (!ci)
634                 return -EINVAL;
635         ret = mkret(c4_musycc_rw(ci, &mp));
636         if (ret)
637                 return ret;
638         if (copy_to_user(data, &mp, sizeof(struct c4_musycc_param)))
639                 return -EFAULT;
640         return 0;
641 }
642
643 static status_t
644 do_get_chan(struct net_device *ndev, void *data)
645 {
646         struct sbecom_chan_param cp;
647         int         ret;
648
649         if (copy_from_user(&cp, data,
650                                 sizeof(struct sbecom_chan_param)))
651                 return -EFAULT;
652
653         ret = mkret(c4_get_chan(cp.channum, &cp));
654         if (ret)
655                 return ret;
656
657         if (copy_to_user(data, &cp, sizeof(struct sbecom_chan_param)))
658                 return -EFAULT;
659         return 0;
660 }
661
662 static status_t
663 do_set_chan(struct net_device *ndev, void *data)
664 {
665         struct sbecom_chan_param cp;
666         int         ret;
667         ci_t       *ci;
668
669         if (copy_from_user(&cp, data, sizeof(struct sbecom_chan_param)))
670                 return -EFAULT;
671         ci = get_ci_by_dev(ndev);
672         if (!ci)
673                 return -EINVAL;
674         switch (ret = mkret(c4_set_chan(cp.channum, &cp)))
675         {
676         case 0:
677                 return 0;
678         default:
679                 return ret;
680         }
681 }
682
683 static status_t
684 do_create_chan(struct net_device *ndev, void *data)
685 {
686         ci_t       *ci;
687         struct net_device *dev;
688         struct sbecom_chan_param cp;
689         int         ret;
690
691         if (copy_from_user(&cp, data, sizeof(struct sbecom_chan_param)))
692                 return -EFAULT;
693         ci = get_ci_by_dev(ndev);
694         if (!ci)
695                 return -EINVAL;
696         dev = create_chan(ndev, ci, &cp);
697         if (!dev)
698                 return -EBUSY;
699         ret = mkret(c4_new_chan(ci, cp.port, cp.channum, dev));
700         if (ret)
701         {
702                 /* needed due to Ioctl calling sequence */
703                 rtnl_unlock();
704                 unregister_hdlc_device(dev);
705                 /* needed due to Ioctl calling sequence */
706                 rtnl_lock();
707                 free_netdev(dev);
708         }
709         return ret;
710 }
711
712 static status_t
713 do_get_chan_stats(struct net_device *ndev, void *data)
714 {
715         struct c4_chan_stats_wrap ccs;
716         int         ret;
717
718         if (copy_from_user(&ccs, data,
719                            sizeof(struct c4_chan_stats_wrap)))
720                 return -EFAULT;
721         switch (ret = mkret(c4_get_chan_stats(ccs.channum, &ccs.stats)))
722         {
723         case 0:
724                 break;
725         default:
726                 return ret;
727         }
728         if (copy_to_user(data, &ccs,
729                          sizeof(struct c4_chan_stats_wrap)))
730                 return -EFAULT;
731         return 0;
732 }
733 static status_t
734 do_set_loglevel(struct net_device *ndev, void *data)
735 {
736         unsigned int cxt1e1_log_level;
737
738         if (copy_from_user(&cxt1e1_log_level, data, sizeof(int)))
739                 return -EFAULT;
740         sbecom_set_loglevel(cxt1e1_log_level);
741         return 0;
742 }
743
744 static status_t
745 do_deluser(struct net_device *ndev, int lockit)
746 {
747         if (ndev->flags & IFF_UP)
748                 return -EBUSY;
749
750         {
751                 ci_t       *ci;
752                 mch_t      *ch;
753                 const struct c4_priv *priv;
754                 int         channum;
755
756                 priv = (struct c4_priv *)dev_to_hdlc(ndev)->priv;
757                 ci = priv->ci;
758                 channum = priv->channum;
759
760                 ch = c4_find_chan(channum);
761                 if (ch == NULL)
762                         return -ENOENT;
763                 ch->user = NULL;        /* will be freed, below */
764         }
765
766         /* needed if Ioctl calling sequence */
767         if (lockit)
768                 rtnl_unlock();
769         unregister_hdlc_device(ndev);
770         /* needed if Ioctl calling sequence */
771         if (lockit)
772                 rtnl_lock();
773         free_netdev(ndev);
774         return 0;
775 }
776
777 int
778 do_del_chan(struct net_device *musycc_dev, void *data)
779 {
780         struct sbecom_chan_param cp;
781         char        buf[sizeof(CHANNAME) + 3];
782         struct net_device *dev;
783         int         ret;
784
785         if (copy_from_user(&cp, data,
786                            sizeof(struct sbecom_chan_param)))
787                 return -EFAULT;
788         if (cp.channum > 999)
789                 return -EINVAL;
790         snprintf(buf, sizeof(buf), CHANNAME "%d", cp.channum);
791         dev = __dev_get_by_name(&init_net, buf);
792         if (!dev)
793                 return -ENODEV;
794         ret = do_deluser(dev, 1);
795         if (ret)
796                 return ret;
797         return c4_del_chan(cp.channum);
798 }
799 int c4_reset_board(void *);
800
801 int
802 do_reset(struct net_device *musycc_dev, void *data)
803 {
804         const struct c4_priv *priv;
805         int         i;
806
807         for (i = 0; i < 128; i++)
808         {
809                 struct net_device *ndev;
810                 char        buf[sizeof(CHANNAME) + 3];
811
812                 sprintf(buf, CHANNAME "%d", i);
813                 ndev = __dev_get_by_name(&init_net, buf);
814                 if (!ndev)
815                         continue;
816                 priv = dev_to_hdlc(ndev)->priv;
817
818                 if ((unsigned long) (priv->ci) ==
819                         (unsigned long) (netdev_priv(musycc_dev)))
820                 {
821                         ndev->flags &= ~IFF_UP;
822                         netif_stop_queue(ndev);
823                         do_deluser(ndev, 1);
824                 }
825         }
826         return 0;
827 }
828
829 int
830 do_reset_chan_stats(struct net_device *musycc_dev, void *data)
831 {
832         struct sbecom_chan_param cp;
833
834         if (copy_from_user(&cp, data,
835                            sizeof(struct sbecom_chan_param)))
836                 return -EFAULT;
837         return mkret(c4_del_chan_stats(cp.channum));
838 }
839
840 static status_t
841 c4_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
842 {
843         ci_t       *ci;
844         void       *data;
845         int         iocmd, iolen;
846         status_t    ret;
847         static struct data
848         {
849                 union
850                 {
851                         u_int8_t c;
852                         u_int32_t i;
853                         struct sbe_brd_info bip;
854                         struct sbe_drv_info dip;
855                         struct sbe_iid_info iip;
856                         struct sbe_brd_addr bap;
857                         struct sbecom_chan_stats stats;
858                         struct sbecom_chan_param param;
859                         struct temux_card_stats cards;
860                         struct sbecom_card_param cardp;
861                         struct sbecom_framer_param frp;
862                 } u;
863         } arg;
864
865
866         if (!capable(CAP_SYS_ADMIN))
867                 return -EPERM;
868         if (cmd != SIOCDEVPRIVATE + 15)
869                 return -EINVAL;
870         ci = get_ci_by_dev(ndev);
871         if (!ci)
872                 return -EINVAL;
873         if (ci->state != C_RUNNING)
874                 return -ENODEV;
875         if (copy_from_user(&iocmd, ifr->ifr_data, sizeof(iocmd)))
876                 return -EFAULT;
877 #if 0
878         if (copy_from_user(&len, ifr->ifr_data + sizeof(iocmd), sizeof(len)))
879                 return -EFAULT;
880 #endif
881
882 #if 0
883         pr_info("c4_ioctl: iocmd %x, dir %x type %x nr %x iolen %d.\n", iocmd,
884                 _IOC_DIR(iocmd), _IOC_TYPE(iocmd), _IOC_NR(iocmd),
885                 _IOC_SIZE(iocmd));
886 #endif
887         iolen = _IOC_SIZE(iocmd);
888         data = ifr->ifr_data + sizeof(iocmd);
889         if (copy_from_user(&arg, data, iolen))
890                 return -EFAULT;
891
892         ret = 0;
893         switch (iocmd)
894         {
895         case SBE_IOC_PORT_GET:
896                 //pr_info(">> SBE_IOC_PORT_GET Ioctl...\n");
897                 ret = do_get_port(ndev, data);
898                 break;
899         case SBE_IOC_PORT_SET:
900                 //pr_info(">> SBE_IOC_PORT_SET Ioctl...\n");
901                 ret = do_set_port(ndev, data);
902                 break;
903         case SBE_IOC_CHAN_GET:
904                 //pr_info(">> SBE_IOC_CHAN_GET Ioctl...\n");
905                 ret = do_get_chan(ndev, data);
906                 break;
907         case SBE_IOC_CHAN_SET:
908                 //pr_info(">> SBE_IOC_CHAN_SET Ioctl...\n");
909                 ret = do_set_chan(ndev, data);
910                 break;
911         case C4_DEL_CHAN:
912                 //pr_info(">> C4_DEL_CHAN Ioctl...\n");
913                 ret = do_del_chan(ndev, data);
914                 break;
915         case SBE_IOC_CHAN_NEW:
916                 ret = do_create_chan(ndev, data);
917                 break;
918         case SBE_IOC_CHAN_GET_STAT:
919                 ret = do_get_chan_stats(ndev, data);
920                 break;
921         case SBE_IOC_LOGLEVEL:
922                 ret = do_set_loglevel(ndev, data);
923                 break;
924         case SBE_IOC_RESET_DEV:
925                 ret = do_reset(ndev, data);
926                 break;
927         case SBE_IOC_CHAN_DEL_STAT:
928                 ret = do_reset_chan_stats(ndev, data);
929                 break;
930         case C4_LOOP_PORT:
931                 ret = do_port_loop(ndev, data);
932                 break;
933         case C4_RW_FRMR:
934                 ret = do_framer_rw(ndev, data);
935                 break;
936         case C4_RW_MSYC:
937                 ret = do_musycc_rw(ndev, data);
938                 break;
939         case C4_RW_PLD:
940                 ret = do_pld_rw(ndev, data);
941                 break;
942         case SBE_IOC_IID_GET:
943                 ret = (iolen == sizeof(struct sbe_iid_info)) ?
944                        c4_get_iidinfo(ci, &arg.u.iip) : -EFAULT;
945                 if (ret == 0)               /* no error, copy data */
946                         if (copy_to_user(data, &arg, iolen))
947                                 return -EFAULT;
948                 break;
949         default:
950                 //pr_info(">> c4_ioctl: EINVAL - unknown iocmd <%x>\n", iocmd);
951                 ret = -EINVAL;
952                 break;
953         }
954         return mkret(ret);
955 }
956
957 static const struct net_device_ops c4_ops = {
958         .ndo_open       = void_open,
959         .ndo_start_xmit = c4_linux_xmit,
960         .ndo_do_ioctl   = c4_ioctl,
961 };
962
963 static void c4_setup(struct net_device *dev)
964 {
965         dev->type = ARPHRD_VOID;
966         dev->netdev_ops = &c4_ops;
967 }
968
969 struct net_device *__init
970 c4_add_dev(hdw_info_t *hi, int brdno, unsigned long f0, unsigned long f1,
971            int irq0, int irq1)
972 {
973         struct net_device *ndev;
974         ci_t       *ci;
975
976         ndev = alloc_netdev(sizeof(ci_t), SBE_IFACETMPL, c4_setup);
977         if (!ndev)
978         {
979                 pr_warning("%s: no memory for struct net_device !\n",
980                            hi->devname);
981                 error_flag = ENOMEM;
982                 return NULL;
983         }
984         ci = (ci_t *)(netdev_priv(ndev));
985         ndev->irq = irq0;
986
987         ci->hdw_info = hi;
988         ci->state = C_INIT;         /* mark as hardware not available */
989         ci->next = c4_list;
990         c4_list = ci;
991         ci->brdno = ci->next ? ci->next->brdno + 1 : 0;
992
993         if (!CI)
994                 CI = ci;                    /* DEBUG, only board 0 usage */
995
996         strcpy(ci->devname, hi->devname);
997
998         /* tasklet */
999 #if defined(SBE_ISR_TASKLET)
1000         tasklet_init(&ci->ci_musycc_isr_tasklet,
1001                      (void (*) (unsigned long)) musycc_intr_bh_tasklet,
1002                      (unsigned long) ci);
1003
1004         if (atomic_read(&ci->ci_musycc_isr_tasklet.count) == 0)
1005                 tasklet_disable_nosync(&ci->ci_musycc_isr_tasklet);
1006 #elif defined(SBE_ISR_IMMEDIATE)
1007         ci->ci_musycc_isr_tq.routine = (void *)(unsigned long)musycc_intr_bh_tasklet;
1008         ci->ci_musycc_isr_tq.data = ci;
1009 #endif
1010
1011
1012         if (register_netdev(ndev) ||
1013                 (c4_init(ci, (u_char *) f0, (u_char *) f1) != SBE_DRVR_SUCCESS))
1014         {
1015                 OS_kfree(netdev_priv(ndev));
1016                 OS_kfree(ndev);
1017                 error_flag = ENODEV;
1018                 return NULL;
1019         }
1020         /*************************************************************
1021          *  int request_irq(unsigned int irq,
1022          *                  void (*handler)(int, void *, struct pt_regs *),
1023          *                  unsigned long flags, const char *dev_name, void *dev_id);
1024          *  wherein:
1025          *  irq      -> The interrupt number that is being requested.
1026          *  handler  -> Pointer to handling function being installed.
1027          *  flags    -> A bit mask of options related to interrupt management.
1028          *  dev_name -> String used in /proc/interrupts to show owner of interrupt.
1029          *  dev_id   -> Pointer (for shared interrupt lines) to point to its own
1030          *              private data area (to identify which device is interrupting).
1031          *
1032          *  extern void free_irq(unsigned int irq, void *dev_id);
1033          **************************************************************/
1034
1035         if (request_irq(irq0, &c4_linux_interrupt,
1036                         IRQF_SHARED,
1037                         ndev->name, ndev))
1038         {
1039                 pr_warning("%s: MUSYCC could not get irq: %d\n",
1040                            ndev->name, irq0);
1041                 unregister_netdev(ndev);
1042                 OS_kfree(netdev_priv(ndev));
1043                 OS_kfree(ndev);
1044                 error_flag = EIO;
1045                 return NULL;
1046         }
1047 #ifdef CONFIG_SBE_PMCC4_NCOMM
1048         if (request_irq(irq1, &c4_ebus_interrupt, IRQF_SHARED, ndev->name, ndev))
1049         {
1050                 pr_warning("%s: EBUS could not get irq: %d\n", hi->devname, irq1);
1051                 unregister_netdev(ndev);
1052                 free_irq(irq0, ndev);
1053                 OS_kfree(netdev_priv(ndev));
1054                 OS_kfree(ndev);
1055                 error_flag = EIO;
1056                 return NULL;
1057         }
1058 #endif
1059
1060         /* setup board identification information */
1061
1062         {
1063                 u_int32_t   tmp;
1064
1065                 /* also sets PROM format type (promfmt) for later usage */
1066                 hdw_sn_get(hi, brdno);
1067
1068                 switch (hi->promfmt)
1069                 {
1070                 case PROM_FORMAT_TYPE1:
1071                         memcpy(ndev->dev_addr,
1072                                (FLD_TYPE1 *) (hi->mfg_info.pft1.Serial), 6);
1073                         /* unaligned data acquisition */
1074                         memcpy(&tmp, (FLD_TYPE1 *) (hi->mfg_info.pft1.Id), 4);
1075                         ci->brd_id = cpu_to_be32(tmp);
1076                         break;
1077                 case PROM_FORMAT_TYPE2:
1078                         memcpy(ndev->dev_addr,
1079                                (FLD_TYPE2 *) (hi->mfg_info.pft2.Serial), 6);
1080                         /* unaligned data acquisition */
1081                         memcpy(&tmp, (FLD_TYPE2 *) (hi->mfg_info.pft2.Id), 4);
1082                         ci->brd_id = cpu_to_be32(tmp);
1083                         break;
1084                 default:
1085                         ci->brd_id = 0;
1086                         memset(ndev->dev_addr, 0, 6);
1087                         break;
1088                 }
1089
1090 #if 1
1091                 /* requires bid to be preset */
1092                 sbeid_set_hdwbid(ci);
1093 #else
1094                 /* requires hdw_bid to be preset */
1095                 sbeid_set_bdtype(ci);
1096 #endif
1097         }
1098
1099 #ifdef CONFIG_PROC_FS
1100         sbecom_proc_brd_init(ci);
1101 #endif
1102 #if defined(SBE_ISR_TASKLET)
1103         tasklet_enable(&ci->ci_musycc_isr_tasklet);
1104 #endif
1105
1106         error_flag = c4_init2(ci);
1107         if (error_flag != SBE_DRVR_SUCCESS) {
1108 #ifdef CONFIG_PROC_FS
1109                 sbecom_proc_brd_cleanup(ci);
1110 #endif
1111                 unregister_netdev(ndev);
1112                 free_irq(irq1, ndev);
1113                 free_irq(irq0, ndev);
1114                 OS_kfree(netdev_priv(ndev));
1115                 OS_kfree(ndev);
1116                 /* failure, error_flag is set */
1117                 return NULL;
1118         }
1119         return ndev;
1120 }
1121
1122 static int  __init
1123 c4_mod_init(void)
1124 {
1125         int         rtn;
1126
1127         rtn = c4hw_attach_all();
1128         if (rtn)
1129                 return -rtn; /* installation failure - see system log */
1130
1131         /* housekeeping notifications */
1132         if (cxt1e1_log_level != log_level_default)
1133                 pr_info("NOTE: driver parameter <cxt1e1_log_level> changed from default %d to %d.\n",
1134                         log_level_default, cxt1e1_log_level);
1135         if (cxt1e1_max_mru != max_mru_default)
1136                 pr_info("NOTE: driver parameter <cxt1e1_max_mru> changed from default %d to %d.\n",
1137                         max_mru_default, cxt1e1_max_mru);
1138         if (cxt1e1_max_mtu != max_mtu_default)
1139                 pr_info("NOTE: driver parameter <cxt1e1_max_mtu> changed from default %d to %d.\n",
1140                         max_mtu_default, cxt1e1_max_mtu);
1141         if (max_rxdesc_used != max_rxdesc_default)
1142         {
1143                 if (max_rxdesc_used > 2000)
1144                         max_rxdesc_used = 2000; /* out-of-bounds reset */
1145                 pr_info("NOTE: driver parameter <max_rxdesc_used> changed from default %d to %d.\n",
1146                         max_rxdesc_default, max_rxdesc_used);
1147         }
1148         if (max_txdesc_used != max_txdesc_default)
1149         {
1150                 if (max_txdesc_used > 1000)
1151                         max_txdesc_used = 1000; /* out-of-bounds reset */
1152                 pr_info("NOTE: driver parameter <max_txdesc_used> changed from default %d to %d.\n",
1153                         max_txdesc_default, max_txdesc_used);
1154         }
1155         return 0;                       /* installation success */
1156 }
1157
1158
1159  /*
1160   * find any still allocated hdlc registrations and unregister via call to
1161   * do_deluser()
1162   */
1163
1164 static void __exit
1165 cleanup_hdlc(void)
1166 {
1167         hdw_info_t *hi;
1168         ci_t       *ci;
1169         struct net_device *ndev;
1170         int         i, j, k;
1171
1172         for (i = 0, hi = hdw_info; i < MAX_BOARDS; i++, hi++)
1173         {
1174                 if (hi->ndev)               /* a board has been attached */
1175                 {
1176                         ci = (ci_t *)(netdev_priv(hi->ndev));
1177                         for (j = 0; j < ci->max_port; j++)
1178                                 for (k = 0; k < MUSYCC_NCHANS; k++) {
1179                                         ndev = ci->port[j].chan[k]->user;
1180                                         if (ndev)
1181                                         {
1182                                                 do_deluser(ndev, 0);
1183                                         }
1184                                 }
1185                 }
1186         }
1187 }
1188
1189
1190 static void __exit
1191 c4_mod_remove(void)
1192 {
1193         cleanup_hdlc();            /* delete any missed channels */
1194         cleanup_devs();
1195         c4_cleanup();
1196         cleanup_ioremap();
1197         pr_info("SBE - driver removed.\n");
1198 }
1199
1200 module_init(c4_mod_init);
1201 module_exit(c4_mod_remove);
1202
1203 MODULE_AUTHOR("SBE Technical Services <support@sbei.com>");
1204 MODULE_DESCRIPTION("wanPCI-CxT1E1 Generic HDLC WAN Driver module");
1205 #ifdef MODULE_LICENSE
1206 MODULE_LICENSE("GPL");
1207 #endif
1208
1209 /***  End-of-File  ***/