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