]> Pileus Git - ~andy/linux/blob - drivers/staging/dgnc/dgnc_driver.c
Merge tag 'for-linus-20130909' of git://git.infradead.org/linux-mtd
[~andy/linux] / drivers / staging / dgnc / dgnc_driver.c
1 /*
2  * Copyright 2003 Digi International (www.digi.com)
3  *      Scott H Kilau <Scott_Kilau at digi dot com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  * PURPOSE.  See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *
20  *      NOTE TO LINUX KERNEL HACKERS:  DO NOT REFORMAT THIS CODE!
21  *
22  *      This is shared code between Digi's CVS archive and the
23  *      Linux Kernel sources.
24  *      Changing the source just for reformatting needlessly breaks
25  *      our CVS diff history.
26  *
27  *      Send any bug fixes/changes to:  Eng.Linux at digi dot com.
28  *      Thank you.
29  *
30  */
31
32
33 #include <linux/kernel.h>
34 #include <linux/version.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/slab.h>
38
39 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
40 #include <linux/sched.h>
41 #endif
42
43 #include "dgnc_driver.h"
44 #include "dgnc_pci.h"
45 #include "dpacompat.h"
46 #include "dgnc_mgmt.h"
47 #include "dgnc_tty.h"
48 #include "dgnc_trace.h"
49 #include "dgnc_cls.h"
50 #include "dgnc_neo.h"
51 #include "dgnc_sysfs.h"
52
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Digi International, http://www.digi.com");
55 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
56 MODULE_SUPPORTED_DEVICE("dgnc");
57
58 /*
59  * insmod command line overrideable parameters
60  *
61  * NOTE: we use a set of macros to create the variables, which allows
62  * us to specify the variable type, name, initial value, and description.
63  */
64 PARM_INT(debug,         0x00,           0644,   "Driver debugging level");
65 PARM_INT(rawreadok,     1,              0644,   "Bypass flip buffers on input");
66 PARM_INT(trcbuf_size,   0x100000,       0644,   "Debugging trace buffer size.");
67
68 /**************************************************************************
69  *
70  * protos for this file
71  *
72  */
73 static int              dgnc_start(void);
74 static int              dgnc_finalize_board_init(struct board_t *brd);
75 static void             dgnc_init_globals(void);
76 static int              dgnc_found_board(struct pci_dev *pdev, int id);
77 static void             dgnc_cleanup_board(struct board_t *brd);
78 static void             dgnc_poll_handler(ulong dummy);
79 static int              dgnc_init_pci(void);
80 static int              dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
81 static void             dgnc_remove_one(struct pci_dev *dev);
82 static int              dgnc_probe1(struct pci_dev *pdev, int card_type);
83 static void             dgnc_do_remap(struct board_t *brd);
84
85 /* Driver load/unload functions */
86 int             dgnc_init_module(void);
87 void            dgnc_cleanup_module(void);
88
89 module_init(dgnc_init_module);
90 module_exit(dgnc_cleanup_module);
91
92
93 /*
94  * File operations permitted on Control/Management major.
95  */
96 static struct file_operations dgnc_BoardFops =
97 {
98         .owner          =       THIS_MODULE,
99         .unlocked_ioctl =       dgnc_mgmt_ioctl,
100         .open           =       dgnc_mgmt_open,
101         .release        =       dgnc_mgmt_close
102 };
103
104
105 /*
106  * Globals
107  */
108 uint                    dgnc_NumBoards;
109 struct board_t          *dgnc_Board[MAXBOARDS];
110 DEFINE_SPINLOCK(dgnc_global_lock);
111 int                     dgnc_driver_state = DRIVER_INITIALIZED;
112 ulong                   dgnc_poll_counter;
113 uint                    dgnc_Major;
114 int                     dgnc_poll_tick = 20;    /* Poll interval - 20 ms */
115
116 /*
117  * Static vars.
118  */
119 static uint             dgnc_Major_Control_Registered = FALSE;
120 static uint             dgnc_driver_start = FALSE;
121
122 static struct class *dgnc_class;
123
124 /*
125  * Poller stuff
126  */
127 static                  DEFINE_SPINLOCK(dgnc_poll_lock);        /* Poll scheduling lock */
128 static ulong            dgnc_poll_time;                         /* Time of next poll */
129 static uint             dgnc_poll_stop;                         /* Used to tell poller to stop */
130 static struct timer_list dgnc_poll_timer;
131
132
133 static struct pci_device_id dgnc_pci_tbl[] = {
134         {       DIGI_VID, PCI_DEVICE_CLASSIC_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       0 },
135         {       DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   1 },
136         {       DIGI_VID, PCI_DEVICE_CLASSIC_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       2 },
137         {       DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   3 },
138         {       DIGI_VID, PCI_DEVICE_NEO_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,           4 },
139         {       DIGI_VID, PCI_DEVICE_NEO_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,           5 },
140         {       DIGI_VID, PCI_DEVICE_NEO_2DB9_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,        6 },
141         {       DIGI_VID, PCI_DEVICE_NEO_2DB9PRI_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,     7 },
142         {       DIGI_VID, PCI_DEVICE_NEO_2RJ45_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       8 },
143         {       DIGI_VID, PCI_DEVICE_NEO_2RJ45PRI_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,    9 },
144         {       DIGI_VID, PCI_DEVICE_NEO_1_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       10 },
145         {       DIGI_VID, PCI_DEVICE_NEO_1_422_485_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   11 },
146         {       DIGI_VID, PCI_DEVICE_NEO_2_422_485_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   12 },
147         {       DIGI_VID, PCI_DEVICE_NEO_EXPRESS_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,    13 },
148         {       DIGI_VID, PCI_DEVICE_NEO_EXPRESS_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   14 },
149         {       DIGI_VID, PCI_DEVICE_NEO_EXPRESS_4RJ45_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       15 },
150         {       DIGI_VID, PCI_DEVICE_NEO_EXPRESS_8RJ45_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,       16 },
151         {0,}                                            /* 0 terminated list. */
152 };
153 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
154
155 struct board_id {
156         uchar *name;
157         uint maxports;
158         unsigned int is_pci_express;
159 };
160
161 static struct board_id dgnc_Ids[] =
162 {
163         {       PCI_DEVICE_CLASSIC_4_PCI_NAME,          4,      0       },
164         {       PCI_DEVICE_CLASSIC_4_422_PCI_NAME,      4,      0       },
165         {       PCI_DEVICE_CLASSIC_8_PCI_NAME,          8,      0       },
166         {       PCI_DEVICE_CLASSIC_8_422_PCI_NAME,      8,      0       },
167         {       PCI_DEVICE_NEO_4_PCI_NAME,              4,      0       },
168         {       PCI_DEVICE_NEO_8_PCI_NAME,              8,      0       },
169         {       PCI_DEVICE_NEO_2DB9_PCI_NAME,           2,      0       },
170         {       PCI_DEVICE_NEO_2DB9PRI_PCI_NAME,        2,      0       },
171         {       PCI_DEVICE_NEO_2RJ45_PCI_NAME,          2,      0       },
172         {       PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME,       2,      0       },
173         {       PCI_DEVICE_NEO_1_422_PCI_NAME,          1,      0       },
174         {       PCI_DEVICE_NEO_1_422_485_PCI_NAME,      1,      0       },
175         {       PCI_DEVICE_NEO_2_422_485_PCI_NAME,      2,      0       },
176         {       PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME,      8,      1       },
177         {       PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME,      4,      1       },
178         {       PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME,  4,      1       },
179         {       PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME,  8,      1       },
180         {       NULL,                                   0,      0       }
181 };
182
183 static struct pci_driver dgnc_driver = {
184         .name           = "dgnc",
185         .probe          = dgnc_init_one,
186         .id_table       = dgnc_pci_tbl,
187         .remove         = dgnc_remove_one,
188 };
189
190
191 char *dgnc_state_text[] = {
192         "Board Failed",
193         "Board Found",
194         "Board READY",
195 };
196
197 char *dgnc_driver_state_text[] = {
198         "Driver Initialized",
199         "Driver Ready."
200 };
201
202
203
204 /************************************************************************
205  *
206  * Driver load/unload functions
207  *
208  ************************************************************************/
209
210
211 /*
212  * init_module()
213  *
214  * Module load.  This is where it all starts.
215  */
216 int dgnc_init_module(void)
217 {
218         int rc = 0;
219
220         APR(("%s, Digi International Part Number %s\n", DG_NAME, DG_PART));
221
222         /*
223          * Initialize global stuff
224          */
225         rc = dgnc_start();
226
227         if (rc < 0) {
228                 return(rc);
229         }
230
231         /*
232          * Find and configure all the cards
233          */
234         rc = dgnc_init_pci();
235
236         /*
237          * If something went wrong in the scan, bail out of driver.
238          */
239         if (rc < 0) {
240                 /* Only unregister the pci driver if it was actually registered. */
241                 if (dgnc_NumBoards)
242                         pci_unregister_driver(&dgnc_driver);
243                 else
244                         printk("WARNING: dgnc driver load failed.  No Digi Neo or Classic boards found.\n");
245
246                 dgnc_cleanup_module();
247         }
248         else {
249                 dgnc_create_driver_sysfiles(&dgnc_driver);
250         }
251
252         DPR_INIT(("Finished init_module. Returning %d\n", rc));
253         return (rc);
254 }
255
256
257 /*
258  * Start of driver.
259  */
260 static int dgnc_start(void)
261 {
262         int rc = 0;
263         unsigned long flags;
264
265         if (dgnc_driver_start == FALSE) {
266
267                 dgnc_driver_start = TRUE;
268
269                 /* make sure that the globals are init'd before we do anything else */
270                 dgnc_init_globals();
271
272                 dgnc_NumBoards = 0;
273
274                 APR(("For the tools package or updated drivers please visit http://www.digi.com\n"));
275
276                 /*
277                  * Register our base character device into the kernel.
278                  * This allows the download daemon to connect to the downld device
279                  * before any of the boards are init'ed.
280                  */
281                 if (!dgnc_Major_Control_Registered) {
282                         /*
283                          * Register management/dpa devices
284                          */
285                         rc = register_chrdev(0, "dgnc", &dgnc_BoardFops);
286                         if (rc <= 0) {
287                                 APR(("Can't register dgnc driver device (%d)\n", rc));
288                                 rc = -ENXIO;
289                                 return(rc);
290                         }
291                         dgnc_Major = rc;
292
293                         dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
295                         device_create_drvdata(dgnc_class, NULL,
296                                 MKDEV(dgnc_Major, 0),
297                                 NULL, "dgnc_mgmt");
298 #else
299                         device_create(dgnc_class, NULL,
300                                 MKDEV(dgnc_Major, 0),
301                                 NULL, "dgnc_mgmt");
302 #endif
303
304                         dgnc_Major_Control_Registered = TRUE;
305                 }
306
307                 /*
308                  * Init any global tty stuff.
309                  */
310                 rc = dgnc_tty_preinit();
311
312                 if (rc < 0) {
313                         APR(("tty preinit - not enough memory (%d)\n", rc));
314                         return(rc);
315                 }
316
317                 /* Start the poller */
318                 DGNC_LOCK(dgnc_poll_lock, flags);
319                 init_timer(&dgnc_poll_timer);
320                 dgnc_poll_timer.function = dgnc_poll_handler;
321                 dgnc_poll_timer.data = 0;
322                 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
323                 dgnc_poll_timer.expires = dgnc_poll_time;
324                 DGNC_UNLOCK(dgnc_poll_lock, flags);
325
326                 add_timer(&dgnc_poll_timer);
327
328                 dgnc_driver_state = DRIVER_READY;
329         }
330
331         return(rc);
332 }
333
334 /*
335  * Register pci driver, and return how many boards we have.
336  */
337 static int dgnc_init_pci(void)
338 {
339         return pci_register_driver(&dgnc_driver);
340 }
341
342
343 /* returns count (>= 0), or negative on error */
344 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
345 {
346         int rc;
347
348         /* wake up and enable device */
349         rc = pci_enable_device(pdev);
350
351         if (rc < 0) {
352                 rc = -EIO;
353         } else {
354                 rc = dgnc_probe1(pdev, ent->driver_data);
355                 if (rc == 0) {
356                         dgnc_NumBoards++;
357                         DPR_INIT(("Incrementing numboards to %d\n", dgnc_NumBoards));
358                 }
359         }
360         return rc;
361 }
362
363 static int dgnc_probe1(struct pci_dev *pdev, int card_type)
364 {
365         return dgnc_found_board(pdev, card_type);
366 }
367
368
369 static void dgnc_remove_one(struct pci_dev *dev)
370 {
371         /* Do Nothing */
372 }
373
374 /*
375  * dgnc_cleanup_module()
376  *
377  * Module unload.  This is where it all ends.
378  */
379 void dgnc_cleanup_module(void)
380 {
381         int i;
382         ulong lock_flags;
383
384         DGNC_LOCK(dgnc_poll_lock, lock_flags);
385         dgnc_poll_stop = 1;
386         DGNC_UNLOCK(dgnc_poll_lock, lock_flags);
387
388         /* Turn off poller right away. */
389         del_timer_sync(&dgnc_poll_timer);
390
391         dgnc_remove_driver_sysfiles(&dgnc_driver);
392
393         if (dgnc_Major_Control_Registered) {
394                 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
395                 class_destroy(dgnc_class);
396                 unregister_chrdev(dgnc_Major, "dgnc");
397         }
398
399         for (i = 0; i < dgnc_NumBoards; ++i) {
400                 dgnc_remove_ports_sysfiles(dgnc_Board[i]);
401                 dgnc_tty_uninit(dgnc_Board[i]);
402                 dgnc_cleanup_board(dgnc_Board[i]);
403         }
404
405         dgnc_tty_post_uninit();
406
407 #if defined(DGNC_TRACER)
408         /* last thing, make sure we release the tracebuffer */
409         dgnc_tracer_free();
410 #endif
411         if (dgnc_NumBoards)
412                 pci_unregister_driver(&dgnc_driver);
413 }
414
415
416 /*
417  * dgnc_cleanup_board()
418  *
419  * Free all the memory associated with a board
420  */
421 static void dgnc_cleanup_board(struct board_t *brd)
422 {
423         int i = 0;
424
425         if(!brd || brd->magic != DGNC_BOARD_MAGIC)
426                 return;
427
428         switch (brd->device) {
429         case PCI_DEVICE_CLASSIC_4_DID:
430         case PCI_DEVICE_CLASSIC_8_DID:
431         case PCI_DEVICE_CLASSIC_4_422_DID:
432         case PCI_DEVICE_CLASSIC_8_422_DID:
433
434                 /* Tell card not to interrupt anymore. */
435                 outb(0, brd->iobase + 0x4c);
436                 break;
437
438         default:
439                 break;
440         }
441
442         if (brd->irq)
443                 free_irq(brd->irq, brd);
444
445         tasklet_kill(&brd->helper_tasklet);
446
447         if (brd->re_map_membase) {
448                 iounmap(brd->re_map_membase);
449                 brd->re_map_membase = NULL;
450         }
451
452         if (brd->msgbuf_head) {
453                 unsigned long flags;
454
455                 DGNC_LOCK(dgnc_global_lock, flags);
456                 brd->msgbuf = NULL;
457                 printk(brd->msgbuf_head);
458                 kfree(brd->msgbuf_head);
459                 brd->msgbuf_head = NULL;
460                 DGNC_UNLOCK(dgnc_global_lock, flags);
461         }
462
463         /* Free all allocated channels structs */
464         for (i = 0; i < MAXPORTS ; i++) {
465                 if (brd->channels[i]) {
466                         if (brd->channels[i]->ch_rqueue)
467                                 kfree(brd->channels[i]->ch_rqueue);
468                         if (brd->channels[i]->ch_equeue)
469                                 kfree(brd->channels[i]->ch_equeue);
470                         if (brd->channels[i]->ch_wqueue)
471                                 kfree(brd->channels[i]->ch_wqueue);
472
473                         kfree(brd->channels[i]);
474                         brd->channels[i] = NULL;
475                 }
476         }
477
478         if (brd->flipbuf)
479                 kfree(brd->flipbuf);
480
481         dgnc_Board[brd->boardnum] = NULL;
482
483         kfree(brd);
484 }
485
486
487 /*
488  * dgnc_found_board()
489  *
490  * A board has been found, init it.
491  */
492 static int dgnc_found_board(struct pci_dev *pdev, int id)
493 {
494         struct board_t *brd;
495         unsigned int pci_irq;
496         int i = 0;
497         int rc = 0;
498         unsigned long flags;
499
500         /* get the board structure and prep it */
501         brd = dgnc_Board[dgnc_NumBoards] =
502         (struct board_t *) kzalloc(sizeof(struct board_t), GFP_KERNEL);
503         if (!brd) {
504                 APR(("memory allocation for board structure failed\n"));
505                 return(-ENOMEM);
506         }
507
508         /* make a temporary message buffer for the boot messages */
509         brd->msgbuf = brd->msgbuf_head =
510                 (char *) kzalloc(sizeof(char) * 8192, GFP_KERNEL);
511         if (!brd->msgbuf) {
512                 kfree(brd);
513                 APR(("memory allocation for board msgbuf failed\n"));
514                 return(-ENOMEM);
515         }
516
517         /* store the info for the board we've found */
518         brd->magic = DGNC_BOARD_MAGIC;
519         brd->boardnum = dgnc_NumBoards;
520         brd->vendor = dgnc_pci_tbl[id].vendor;
521         brd->device = dgnc_pci_tbl[id].device;
522         brd->pdev = pdev;
523         brd->pci_bus = pdev->bus->number;
524         brd->pci_slot = PCI_SLOT(pdev->devfn);
525         brd->name = dgnc_Ids[id].name;
526         brd->maxports = dgnc_Ids[id].maxports;
527         if (dgnc_Ids[i].is_pci_express)
528                 brd->bd_flags |= BD_IS_PCI_EXPRESS;
529         brd->dpastatus = BD_NOFEP;
530         init_waitqueue_head(&brd->state_wait);
531
532         DGNC_SPINLOCK_INIT(brd->bd_lock);
533         DGNC_SPINLOCK_INIT(brd->bd_intr_lock);
534
535         brd->state              = BOARD_FOUND;
536
537         for (i = 0; i < MAXPORTS; i++) {
538                 brd->channels[i] = NULL;
539         }
540
541         /* store which card & revision we have */
542         pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
543         pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
544         pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
545
546         pci_irq = pdev->irq;
547         brd->irq = pci_irq;
548
549
550         switch(brd->device) {
551
552         case PCI_DEVICE_CLASSIC_4_DID:
553         case PCI_DEVICE_CLASSIC_8_DID:
554         case PCI_DEVICE_CLASSIC_4_422_DID:
555         case PCI_DEVICE_CLASSIC_8_422_DID:
556
557                 brd->dpatype = T_CLASSIC | T_PCIBUS;
558
559                 DPR_INIT(("dgnc_found_board - Classic.\n"));
560
561                 /*
562                  * For PCI ClassicBoards
563                  * PCI Local Address (i.e. "resource" number) space
564                  * 0    PLX Memory Mapped Config
565                  * 1    PLX I/O Mapped Config
566                  * 2    I/O Mapped UARTs and Status
567                  * 3    Memory Mapped VPD
568                  * 4    Memory Mapped UARTs and Status
569                  */
570
571
572                 /* get the PCI Base Address Registers */
573                 brd->membase = pci_resource_start(pdev, 4);
574
575                 if (!brd->membase) {
576                         APR(("card has no PCI IO resources, failing board.\n"));
577                         return -ENODEV;
578                 }
579
580                 brd->membase_end = pci_resource_end(pdev, 4);
581
582                 if (brd->membase & 1)
583                         brd->membase &= ~3;
584                 else
585                         brd->membase &= ~15;
586
587                 brd->iobase     = pci_resource_start(pdev, 1);
588                 brd->iobase_end = pci_resource_end(pdev, 1);
589                 brd->iobase     = ((unsigned int) (brd->iobase)) & 0xFFFE;
590
591                 /* Assign the board_ops struct */
592                 brd->bd_ops = &dgnc_cls_ops;
593
594                 brd->bd_uart_offset = 0x8;
595                 brd->bd_dividend = 921600;
596
597                 dgnc_do_remap(brd);
598
599                 /* Get and store the board VPD, if it exists */
600                 brd->bd_ops->vpd(brd);
601
602                 /*
603                  * Enable Local Interrupt 1               (0x1),
604                  * Local Interrupt 1 Polarity Active high (0x2),
605                  * Enable PCI interrupt                   (0x40)
606                  */
607                 outb(0x43, brd->iobase + 0x4c);
608
609                 break;
610
611
612         case PCI_DEVICE_NEO_4_DID:
613         case PCI_DEVICE_NEO_8_DID:
614         case PCI_DEVICE_NEO_2DB9_DID:
615         case PCI_DEVICE_NEO_2DB9PRI_DID:
616         case PCI_DEVICE_NEO_2RJ45_DID:
617         case PCI_DEVICE_NEO_2RJ45PRI_DID:
618         case PCI_DEVICE_NEO_1_422_DID:
619         case PCI_DEVICE_NEO_1_422_485_DID:
620         case PCI_DEVICE_NEO_2_422_485_DID:
621         case PCI_DEVICE_NEO_EXPRESS_8_DID:
622         case PCI_DEVICE_NEO_EXPRESS_4_DID:
623         case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
624         case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
625
626                 /*
627                  * This chip is set up 100% when we get to it.
628                  * No need to enable global interrupts or anything.
629                  */
630                 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
631                         brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
632                 else
633                         brd->dpatype = T_NEO | T_PCIBUS;
634
635                 DPR_INIT(("dgnc_found_board - NEO.\n"));
636
637                 /* get the PCI Base Address Registers */
638                 brd->membase     = pci_resource_start(pdev, 0);
639                 brd->membase_end = pci_resource_end(pdev, 0);
640
641                 if (brd->membase & 1)
642                         brd->membase &= ~3;
643                 else
644                         brd->membase &= ~15;
645
646                 /* Assign the board_ops struct */
647                 brd->bd_ops = &dgnc_neo_ops;
648
649                 brd->bd_uart_offset = 0x200;
650                 brd->bd_dividend = 921600;
651
652                 dgnc_do_remap(brd);
653
654                 if (brd->re_map_membase) {
655
656                         /* After remap is complete, we need to read and store the dvid */
657                         brd->dvid = readb(brd->re_map_membase + 0x8D);
658
659                         /* Get and store the board VPD, if it exists */
660                         brd->bd_ops->vpd(brd);
661                 }
662                 break;
663
664         default:
665                 APR(("Did not find any compatible Neo or Classic PCI boards in system.\n"));
666                 return (-ENXIO);
667
668         }
669
670         /*
671          * Do tty device initialization.
672          */
673
674         rc = dgnc_tty_register(brd);
675         if (rc < 0) {
676                 dgnc_tty_uninit(brd);
677                 APR(("Can't register tty devices (%d)\n", rc));
678                 brd->state = BOARD_FAILED;
679                 brd->dpastatus = BD_NOFEP;
680                 goto failed;
681         }
682
683         rc = dgnc_finalize_board_init(brd);
684         if (rc < 0) {
685                 APR(("Can't finalize board init (%d)\n", rc));
686                 brd->state = BOARD_FAILED;
687                 brd->dpastatus = BD_NOFEP;
688
689                 goto failed;
690         }
691
692         rc = dgnc_tty_init(brd);
693         if (rc < 0) {
694                 dgnc_tty_uninit(brd);
695                 APR(("Can't init tty devices (%d)\n", rc));
696                 brd->state = BOARD_FAILED;
697                 brd->dpastatus = BD_NOFEP;
698
699                 goto failed;
700         }
701
702         brd->state = BOARD_READY;
703         brd->dpastatus = BD_RUNNING;
704
705         dgnc_create_ports_sysfiles(brd);
706
707         /* init our poll helper tasklet */
708         tasklet_init(&brd->helper_tasklet, brd->bd_ops->tasklet, (unsigned long) brd);
709
710         DPR_INIT(("dgnc_scan(%d) - printing out the msgbuf\n", i));
711         DGNC_LOCK(dgnc_global_lock, flags);
712         brd->msgbuf = NULL;
713         printk(brd->msgbuf_head);
714         kfree(brd->msgbuf_head);
715         brd->msgbuf_head = NULL;
716         DGNC_UNLOCK(dgnc_global_lock, flags);
717
718         /*
719          * allocate flip buffer for board.
720          *
721          * Okay to malloc with GFP_KERNEL, we are not at interrupt
722          * context, and there are no locks held.
723          */
724         brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
725
726         wake_up_interruptible(&brd->state_wait);
727
728         return(0);
729
730 failed:
731
732         return (-ENXIO);
733
734 }
735
736
737 static int dgnc_finalize_board_init(struct board_t *brd) {
738         int rc = 0;
739
740         DPR_INIT(("dgnc_finalize_board_init() - start\n"));
741
742         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
743                 return(-ENODEV);
744
745         DPR_INIT(("dgnc_finalize_board_init() - start #2\n"));
746
747         if (brd->irq) {
748                 rc = request_irq(brd->irq, brd->bd_ops->intr, IRQF_SHARED, "DGNC", brd);
749
750                 if (rc) {
751                         printk("Failed to hook IRQ %d\n",brd->irq);
752                         brd->state = BOARD_FAILED;
753                         brd->dpastatus = BD_NOFEP;
754                         rc = -ENODEV;
755                 } else {
756                         DPR_INIT(("Requested and received usage of IRQ %d\n", brd->irq));
757                 }
758         }
759         return(rc);
760 }
761
762 /*
763  * Remap PCI memory.
764  */
765 static void dgnc_do_remap(struct board_t *brd)
766 {
767
768         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
769                 return;
770
771         brd->re_map_membase = ioremap(brd->membase, 0x1000);
772
773         DPR_INIT(("remapped mem: 0x%p\n", brd->re_map_membase));
774 }
775
776
777 /*****************************************************************************
778 *
779 * Function:
780 *
781 *    dgnc_poll_handler
782 *
783 * Author:
784 *
785 *    Scott H Kilau
786 *
787 * Parameters:
788 *
789 *    dummy -- ignored
790 *
791 * Return Values:
792 *
793 *    none
794 *
795 * Description:
796 *
797 *    As each timer expires, it determines (a) whether the "transmit"
798 *    waiter needs to be woken up, and (b) whether the poller needs to
799 *    be rescheduled.
800 *
801 ******************************************************************************/
802
803 static void dgnc_poll_handler(ulong dummy)
804 {
805         struct board_t *brd;
806         unsigned long lock_flags;
807         int i;
808         unsigned long new_time;
809
810         dgnc_poll_counter++;
811
812         /*
813          * Do not start the board state machine until
814          * driver tells us its up and running, and has
815          * everything it needs.
816          */
817         if (dgnc_driver_state != DRIVER_READY) {
818                 goto schedule_poller;
819         }
820
821         /* Go thru each board, kicking off a tasklet for each if needed */
822         for (i = 0; i < dgnc_NumBoards; i++) {
823                 brd = dgnc_Board[i];
824
825                 DGNC_LOCK(brd->bd_lock, lock_flags);
826
827                 /* If board is in a failed state, don't bother scheduling a tasklet */
828                 if (brd->state == BOARD_FAILED) {
829                         DGNC_UNLOCK(brd->bd_lock, lock_flags);
830                         continue;
831                 }
832
833                 /* Schedule a poll helper task */
834                 tasklet_schedule(&brd->helper_tasklet);
835
836                 DGNC_UNLOCK(brd->bd_lock, lock_flags);
837         }
838
839 schedule_poller:
840
841         /*
842          * Schedule ourself back at the nominal wakeup interval.
843          */
844         DGNC_LOCK(dgnc_poll_lock, lock_flags);
845         dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
846
847         new_time = dgnc_poll_time - jiffies;
848
849         if ((ulong) new_time >= 2 * dgnc_poll_tick) {
850                 dgnc_poll_time = jiffies +  dgnc_jiffies_from_ms(dgnc_poll_tick);
851         }
852
853         init_timer(&dgnc_poll_timer);
854         dgnc_poll_timer.function = dgnc_poll_handler;
855         dgnc_poll_timer.data = 0;
856         dgnc_poll_timer.expires = dgnc_poll_time;
857         DGNC_UNLOCK(dgnc_poll_lock, lock_flags);
858
859         if (!dgnc_poll_stop)
860                 add_timer(&dgnc_poll_timer);
861 }
862
863 /*
864  * dgnc_init_globals()
865  *
866  * This is where we initialize the globals from the static insmod
867  * configuration variables.  These are declared near the head of
868  * this file.
869  */
870 static void dgnc_init_globals(void)
871 {
872         int i = 0;
873
874         dgnc_rawreadok          = rawreadok;
875         dgnc_trcbuf_size        = trcbuf_size;
876         dgnc_debug              = debug;
877
878         for (i = 0; i < MAXBOARDS; i++) {
879                 dgnc_Board[i] = NULL;
880         }
881
882         init_timer(&dgnc_poll_timer);
883 }
884
885
886 /************************************************************************
887  *
888  * Utility functions
889  *
890  ************************************************************************/
891
892 /*
893  * dgnc_ms_sleep()
894  *
895  * Put the driver to sleep for x ms's
896  *
897  * Returns 0 if timed out, !0 (showing signal) if interrupted by a signal.
898  */
899 int dgnc_ms_sleep(ulong ms)
900 {
901         current->state = TASK_INTERRUPTIBLE;
902         schedule_timeout((ms * HZ) / 1000);
903         return (signal_pending(current));
904 }
905
906
907
908 /*
909  *      dgnc_ioctl_name() : Returns a text version of each ioctl value.
910  */
911 char *dgnc_ioctl_name(int cmd)
912 {
913         switch(cmd) {
914
915         case TCGETA:            return("TCGETA");
916         case TCGETS:            return("TCGETS");
917         case TCSETA:            return("TCSETA");
918         case TCSETS:            return("TCSETS");
919         case TCSETAW:           return("TCSETAW");
920         case TCSETSW:           return("TCSETSW");
921         case TCSETAF:           return("TCSETAF");
922         case TCSETSF:           return("TCSETSF");
923         case TCSBRK:            return("TCSBRK");
924         case TCXONC:            return("TCXONC");
925         case TCFLSH:            return("TCFLSH");
926         case TIOCGSID:          return("TIOCGSID");
927
928         case TIOCGETD:          return("TIOCGETD");
929         case TIOCSETD:          return("TIOCSETD");
930         case TIOCGWINSZ:        return("TIOCGWINSZ");
931         case TIOCSWINSZ:        return("TIOCSWINSZ");
932
933         case TIOCMGET:          return("TIOCMGET");
934         case TIOCMSET:          return("TIOCMSET");
935         case TIOCMBIS:          return("TIOCMBIS");
936         case TIOCMBIC:          return("TIOCMBIC");
937
938         /* from digi.h */
939         case DIGI_SETA:         return("DIGI_SETA");
940         case DIGI_SETAW:        return("DIGI_SETAW");
941         case DIGI_SETAF:        return("DIGI_SETAF");
942         case DIGI_SETFLOW:      return("DIGI_SETFLOW");
943         case DIGI_SETAFLOW:     return("DIGI_SETAFLOW");
944         case DIGI_GETFLOW:      return("DIGI_GETFLOW");
945         case DIGI_GETAFLOW:     return("DIGI_GETAFLOW");
946         case DIGI_GETA:         return("DIGI_GETA");
947         case DIGI_GEDELAY:      return("DIGI_GEDELAY");
948         case DIGI_SEDELAY:      return("DIGI_SEDELAY");
949         case DIGI_GETCUSTOMBAUD: return("DIGI_GETCUSTOMBAUD");
950         case DIGI_SETCUSTOMBAUD: return("DIGI_SETCUSTOMBAUD");
951         case TIOCMODG:          return("TIOCMODG");
952         case TIOCMODS:          return("TIOCMODS");
953         case TIOCSDTR:          return("TIOCSDTR");
954         case TIOCCDTR:          return("TIOCCDTR");
955
956         default:                return("unknown");
957         }
958 }