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