Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

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 
 16 #include <linux/kernel.h>
 17 #include <linux/module.h>
 18 #include <linux/pci.h>
 19 #include <linux/slab.h>
 20 #include <linux/sched.h>
 21 #include "dgnc_driver.h"
 22 #include "dgnc_pci.h"
 23 #include "dgnc_mgmt.h"
 24 #include "dgnc_tty.h"
 25 #include "dgnc_cls.h"
 26 #include "dgnc_neo.h"
 27 
 28 MODULE_LICENSE("GPL");
 29 MODULE_AUTHOR("Digi International, http://www.digi.com");
 30 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
 31 MODULE_SUPPORTED_DEVICE("dgnc");
 32 
 33 /* File operations permitted on Control/Management major. */
 34 
 35 static const struct file_operations dgnc_board_fops = {
 36         .owner          =       THIS_MODULE,
 37         .unlocked_ioctl =       dgnc_mgmt_ioctl,
 38         .open           =       dgnc_mgmt_open,
 39         .release        =       dgnc_mgmt_close
 40 };
 41 
 42 /* Globals */
 43 
 44 uint                    dgnc_num_boards;
 45 struct dgnc_board               *dgnc_board[MAXBOARDS];
 46 DEFINE_SPINLOCK(dgnc_global_lock);
 47 DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
 48 uint                    dgnc_major;
 49 int                     dgnc_poll_tick = 20;    /* Poll interval - 20 ms */
 50 
 51 /* Static vars. */
 52 
 53 static struct class *dgnc_class;
 54 
 55 /* Poller stuff */
 56 
 57 static ulong            dgnc_poll_time; /* Time of next poll */
 58 static uint             dgnc_poll_stop; /* Used to tell poller to stop */
 59 static struct timer_list dgnc_poll_timer;
 60 
 61 static const struct pci_device_id dgnc_pci_tbl[] = {
 62         {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_DID),     .driver_data = 0},
 63         {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID), .driver_data = 1},
 64         {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_DID),     .driver_data = 2},
 65         {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID), .driver_data = 3},
 66         {0,}
 67 };
 68 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
 69 
 70 struct board_id {
 71         unsigned char *name;
 72         uint maxports;
 73         unsigned int is_pci_express;
 74 };
 75 
 76 static const struct board_id dgnc_ids[] = {
 77         {       PCI_DEVICE_CLASSIC_4_PCI_NAME,          4,      0       },
 78         {       PCI_DEVICE_CLASSIC_4_422_PCI_NAME,      4,      0       },
 79         {       PCI_DEVICE_CLASSIC_8_PCI_NAME,          8,      0       },
 80         {       PCI_DEVICE_CLASSIC_8_422_PCI_NAME,      8,      0       },
 81         {       PCI_DEVICE_NEO_4_PCI_NAME,              4,      0       },
 82         {       PCI_DEVICE_NEO_8_PCI_NAME,              8,      0       },
 83         {       PCI_DEVICE_NEO_2DB9_PCI_NAME,           2,      0       },
 84         {       PCI_DEVICE_NEO_2DB9PRI_PCI_NAME,        2,      0       },
 85         {       PCI_DEVICE_NEO_2RJ45_PCI_NAME,          2,      0       },
 86         {       PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME,       2,      0       },
 87         {       PCI_DEVICE_NEO_1_422_PCI_NAME,          1,      0       },
 88         {       PCI_DEVICE_NEO_1_422_485_PCI_NAME,      1,      0       },
 89         {       PCI_DEVICE_NEO_2_422_485_PCI_NAME,      2,      0       },
 90         {       PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME,      8,      1       },
 91         {       PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME,      4,      1       },
 92         {       PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME,  4,      1       },
 93         {       PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME,  8,      1       },
 94         {       NULL,                                   0,      0       }
 95 };
 96 
 97 /* Remap PCI memory. */
 98 
 99 static int dgnc_do_remap(struct dgnc_board *brd)
100 {
101         int rc = 0;
102 
103         brd->re_map_membase = ioremap(brd->membase, 0x1000);
104         if (!brd->re_map_membase)
105                 rc = -ENOMEM;
106 
107         return rc;
108 }
109 
110 /*
111  * dgnc_found_board()
112  *
113  * A board has been found, init it.
114  */
115 static struct dgnc_board *dgnc_found_board(struct pci_dev *pdev, int id)
116 {
117         struct dgnc_board *brd;
118         unsigned int pci_irq;
119         int i = 0;
120         int rc = 0;
121 
122         /* get the board structure and prep it */
123         brd = kzalloc(sizeof(*brd), GFP_KERNEL);
124         if (!brd)
125                 return ERR_PTR(-ENOMEM);
126 
127         /* store the info for the board we've found */
128         brd->magic = DGNC_BOARD_MAGIC;
129         brd->boardnum = dgnc_num_boards;
130         brd->vendor = dgnc_pci_tbl[id].vendor;
131         brd->device = dgnc_pci_tbl[id].device;
132         brd->pdev = pdev;
133         brd->pci_bus = pdev->bus->number;
134         brd->pci_slot = PCI_SLOT(pdev->devfn);
135         brd->name = dgnc_ids[id].name;
136         brd->maxports = dgnc_ids[id].maxports;
137         if (dgnc_ids[i].is_pci_express)
138                 brd->bd_flags |= BD_IS_PCI_EXPRESS;
139         brd->dpastatus = BD_NOFEP;
140         init_waitqueue_head(&brd->state_wait);
141 
142         spin_lock_init(&brd->bd_lock);
143         spin_lock_init(&brd->bd_intr_lock);
144 
145         brd->state              = BOARD_FOUND;
146 
147         /* store which card & revision we have */
148         pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
149         pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
150         pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
151 
152         pci_irq = pdev->irq;
153         brd->irq = pci_irq;
154 
155         switch (brd->device) {
156         case PCI_DEVICE_CLASSIC_4_DID:
157         case PCI_DEVICE_CLASSIC_8_DID:
158         case PCI_DEVICE_CLASSIC_4_422_DID:
159         case PCI_DEVICE_CLASSIC_8_422_DID:
160 
161                 brd->dpatype = T_CLASSIC | T_PCIBUS;
162 
163                 /*
164                  * For PCI ClassicBoards
165                  * PCI Local Address (i.e. "resource" number) space
166                  * 0    PLX Memory Mapped Config
167                  * 1    PLX I/O Mapped Config
168                  * 2    I/O Mapped UARTs and Status
169                  * 3    Memory Mapped VPD
170                  * 4    Memory Mapped UARTs and Status
171                  */
172 
173                 /* get the PCI Base Address Registers */
174                 brd->membase = pci_resource_start(pdev, 4);
175 
176                 if (!brd->membase) {
177                         dev_err(&brd->pdev->dev,
178                                 "Card has no PCI IO resources, failing.\n");
179                         rc = -ENODEV;
180                         goto failed;
181                 }
182 
183                 brd->membase_end = pci_resource_end(pdev, 4);
184 
185                 if (brd->membase & 1)
186                         brd->membase &= ~3;
187                 else
188                         brd->membase &= ~15;
189 
190                 brd->iobase     = pci_resource_start(pdev, 1);
191                 brd->iobase_end = pci_resource_end(pdev, 1);
192                 brd->iobase     = ((unsigned int)(brd->iobase)) & 0xFFFE;
193 
194                 /* Assign the board_ops struct */
195                 brd->bd_ops = &dgnc_cls_ops;
196 
197                 brd->bd_uart_offset = 0x8;
198                 brd->bd_dividend = 921600;
199 
200                 rc = dgnc_do_remap(brd);
201 
202                 if (rc < 0)
203                         goto failed;
204 
205                 /* Get and store the board VPD, if it exists */
206                 brd->bd_ops->vpd(brd);
207 
208                 /*
209                  * Enable Local Interrupt 1               (0x1),
210                  * Local Interrupt 1 Polarity Active high (0x2),
211                  * Enable PCI interrupt                   (0x40)
212                  */
213                 outb(0x43, brd->iobase + 0x4c);
214 
215                 break;
216 
217         case PCI_DEVICE_NEO_4_DID:
218         case PCI_DEVICE_NEO_8_DID:
219         case PCI_DEVICE_NEO_2DB9_DID:
220         case PCI_DEVICE_NEO_2DB9PRI_DID:
221         case PCI_DEVICE_NEO_2RJ45_DID:
222         case PCI_DEVICE_NEO_2RJ45PRI_DID:
223         case PCI_DEVICE_NEO_1_422_DID:
224         case PCI_DEVICE_NEO_1_422_485_DID:
225         case PCI_DEVICE_NEO_2_422_485_DID:
226         case PCI_DEVICE_NEO_EXPRESS_8_DID:
227         case PCI_DEVICE_NEO_EXPRESS_4_DID:
228         case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
229         case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
230 
231                 /*
232                  * This chip is set up 100% when we get to it.
233                  * No need to enable global interrupts or anything.
234                  */
235                 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
236                         brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
237                 else
238                         brd->dpatype = T_NEO | T_PCIBUS;
239 
240                 /* get the PCI Base Address Registers */
241                 brd->membase     = pci_resource_start(pdev, 0);
242                 brd->membase_end = pci_resource_end(pdev, 0);
243 
244                 if (brd->membase & 1)
245                         brd->membase &= ~3;
246                 else
247                         brd->membase &= ~15;
248 
249                 /* Assign the board_ops struct */
250                 brd->bd_ops = &dgnc_neo_ops;
251 
252                 brd->bd_uart_offset = 0x200;
253                 brd->bd_dividend = 921600;
254 
255                 rc = dgnc_do_remap(brd);
256 
257                 if (rc < 0)
258                         goto failed;
259 
260                 /* Read and store the dvid after remapping */
261                 brd->dvid = readb(brd->re_map_membase + 0x8D);
262 
263                 /* Get and store the board VPD, if it exists */
264                 brd->bd_ops->vpd(brd);
265 
266                 break;
267 
268         default:
269                 dev_err(&brd->pdev->dev,
270                         "Didn't find any compatible Neo/Classic PCI boards.\n");
271                 rc = -ENXIO;
272                 goto failed;
273         }
274 
275         /* init our poll helper tasklet */
276         tasklet_init(&brd->helper_tasklet,
277                      brd->bd_ops->tasklet,
278                      (unsigned long)brd);
279 
280         wake_up_interruptible(&brd->state_wait);
281 
282         return brd;
283 
284 failed:
285         kfree(brd);
286 
287         return ERR_PTR(rc);
288 }
289 
290 static int dgnc_request_irq(struct dgnc_board *brd)
291 {
292         int rc = 0;
293 
294         if (brd->irq) {
295                 rc = request_irq(brd->irq, brd->bd_ops->intr,
296                                  IRQF_SHARED, "DGNC", brd);
297 
298                 if (rc) {
299                         dev_err(&brd->pdev->dev,
300                                 "Failed to hook IRQ %d\n", brd->irq);
301                         brd->state = BOARD_FAILED;
302                         brd->dpastatus = BD_NOFEP;
303                         rc = -ENODEV;
304                 }
305         }
306         return rc;
307 }
308 
309 static void dgnc_free_irq(struct dgnc_board *brd)
310 {
311         if (brd->irq)
312                 free_irq(brd->irq, brd);
313 }
314 
315 /*
316  * Function:
317  *
318  *    dgnc_poll_handler
319  *
320  * Author:
321  *
322  *    Scott H Kilau
323  *
324  * Parameters:
325  *
326  *    dummy -- ignored
327  *
328  * Return Values:
329  *
330  *    none
331  *
332  * Description:
333  *
334  *    As each timer expires, it determines (a) whether the "transmit"
335  *    waiter needs to be woken up, and (b) whether the poller needs to
336  *    be rescheduled.
337  */
338 
339 static void dgnc_poll_handler(ulong dummy)
340 {
341         struct dgnc_board *brd;
342         unsigned long flags;
343         int i;
344         unsigned long new_time;
345 
346         /* Go thru each board, kicking off a tasklet for each if needed */
347         for (i = 0; i < dgnc_num_boards; i++) {
348                 brd = dgnc_board[i];
349 
350                 spin_lock_irqsave(&brd->bd_lock, flags);
351 
352                 /* If board is in a failed state don't schedule a tasklet */
353                 if (brd->state == BOARD_FAILED) {
354                         spin_unlock_irqrestore(&brd->bd_lock, flags);
355                         continue;
356                 }
357 
358                 /* Schedule a poll helper task */
359                 tasklet_schedule(&brd->helper_tasklet);
360 
361                 spin_unlock_irqrestore(&brd->bd_lock, flags);
362         }
363 
364         /* Schedule ourself back at the nominal wakeup interval. */
365 
366         spin_lock_irqsave(&dgnc_poll_lock, flags);
367         dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
368 
369         new_time = dgnc_poll_time - jiffies;
370 
371         if ((ulong)new_time >= 2 * dgnc_poll_tick)
372                 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
373 
374         setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
375         dgnc_poll_timer.expires = dgnc_poll_time;
376         spin_unlock_irqrestore(&dgnc_poll_lock, flags);
377 
378         if (!dgnc_poll_stop)
379                 add_timer(&dgnc_poll_timer);
380 }
381 
382 /* returns count (>= 0), or negative on error */
383 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
384 {
385         int rc;
386         struct dgnc_board *brd;
387 
388         /* wake up and enable device */
389         rc = pci_enable_device(pdev);
390 
391         if (rc)
392                 return -EIO;
393 
394         brd = dgnc_found_board(pdev, ent->driver_data);
395         if (IS_ERR(brd))
396                 return PTR_ERR(brd);
397 
398         /* Do tty device initialization. */
399 
400         rc = dgnc_tty_register(brd);
401         if (rc < 0) {
402                 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
403                 goto failed;
404         }
405 
406         rc = dgnc_request_irq(brd);
407         if (rc < 0) {
408                 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
409                 goto unregister_tty;
410         }
411 
412         rc = dgnc_tty_init(brd);
413         if (rc < 0) {
414                 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
415                 goto free_irq;
416         }
417 
418         brd->state = BOARD_READY;
419         brd->dpastatus = BD_RUNNING;
420 
421         dgnc_board[dgnc_num_boards++] = brd;
422 
423         return 0;
424 
425 free_irq:
426         dgnc_free_irq(brd);
427 unregister_tty:
428         dgnc_tty_unregister(brd);
429 
430 failed:
431         kfree(brd);
432 
433         return rc;
434 }
435 
436 static struct pci_driver dgnc_driver = {
437         .name           = "dgnc",
438         .probe          = dgnc_init_one,
439         .id_table       = dgnc_pci_tbl,
440 };
441 
442 /* Start of driver. */
443 
444 static int dgnc_start(void)
445 {
446         int rc = 0;
447         unsigned long flags;
448         struct device *dev;
449 
450         /* make sure timer is initialized before we do anything else */
451         init_timer(&dgnc_poll_timer);
452 
453         /*
454          * Register our base character device into the kernel.
455          * This allows the download daemon to connect to the downld device
456          * before any of the boards are init'ed.
457          *
458          * Register management/dpa devices
459          */
460         rc = register_chrdev(0, "dgnc", &dgnc_board_fops);
461         if (rc < 0) {
462                 pr_err(DRVSTR ": Can't register dgnc driver device (%d)\n", rc);
463                 return rc;
464         }
465         dgnc_major = rc;
466 
467         dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
468         if (IS_ERR(dgnc_class)) {
469                 rc = PTR_ERR(dgnc_class);
470                 pr_err(DRVSTR ": Can't create dgnc_mgmt class (%d)\n", rc);
471                 goto failed_class;
472         }
473 
474         dev = device_create(dgnc_class, NULL,
475                             MKDEV(dgnc_major, 0),
476                         NULL, "dgnc_mgmt");
477         if (IS_ERR(dev)) {
478                 rc = PTR_ERR(dev);
479                 pr_err(DRVSTR ": Can't create device (%d)\n", rc);
480                 goto failed_device;
481         }
482 
483         /* Start the poller */
484         spin_lock_irqsave(&dgnc_poll_lock, flags);
485         setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
486         dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
487         dgnc_poll_timer.expires = dgnc_poll_time;
488         spin_unlock_irqrestore(&dgnc_poll_lock, flags);
489 
490         add_timer(&dgnc_poll_timer);
491 
492         return 0;
493 
494 failed_device:
495         class_destroy(dgnc_class);
496 failed_class:
497         unregister_chrdev(dgnc_major, "dgnc");
498         return rc;
499 }
500 
501 /*
502  * dgnc_cleanup_board()
503  *
504  * Free all the memory associated with a board
505  */
506 static void dgnc_cleanup_board(struct dgnc_board *brd)
507 {
508         int i = 0;
509 
510         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
511                 return;
512 
513         switch (brd->device) {
514         case PCI_DEVICE_CLASSIC_4_DID:
515         case PCI_DEVICE_CLASSIC_8_DID:
516         case PCI_DEVICE_CLASSIC_4_422_DID:
517         case PCI_DEVICE_CLASSIC_8_422_DID:
518 
519                 /* Tell card not to interrupt anymore. */
520                 outb(0, brd->iobase + 0x4c);
521                 break;
522 
523         default:
524                 break;
525         }
526 
527         if (brd->irq)
528                 free_irq(brd->irq, brd);
529 
530         tasklet_kill(&brd->helper_tasklet);
531 
532         if (brd->re_map_membase) {
533                 iounmap(brd->re_map_membase);
534                 brd->re_map_membase = NULL;
535         }
536 
537         /* Free all allocated channels structs */
538         for (i = 0; i < MAXPORTS ; i++) {
539                 if (brd->channels[i]) {
540                         kfree(brd->channels[i]->ch_rqueue);
541                         kfree(brd->channels[i]->ch_equeue);
542                         kfree(brd->channels[i]->ch_wqueue);
543                         kfree(brd->channels[i]);
544                         brd->channels[i] = NULL;
545                 }
546         }
547 
548         dgnc_board[brd->boardnum] = NULL;
549 
550         kfree(brd);
551 }
552 
553 /* Driver load/unload functions */
554 
555 static void cleanup(void)
556 {
557         int i;
558         unsigned long flags;
559 
560         spin_lock_irqsave(&dgnc_poll_lock, flags);
561         dgnc_poll_stop = 1;
562         spin_unlock_irqrestore(&dgnc_poll_lock, flags);
563 
564         /* Turn off poller right away. */
565         del_timer_sync(&dgnc_poll_timer);
566 
567         device_destroy(dgnc_class, MKDEV(dgnc_major, 0));
568         class_destroy(dgnc_class);
569         unregister_chrdev(dgnc_major, "dgnc");
570 
571         for (i = 0; i < dgnc_num_boards; ++i) {
572                 dgnc_cleanup_tty(dgnc_board[i]);
573                 dgnc_cleanup_board(dgnc_board[i]);
574         }
575 }
576 
577 /*
578  * dgnc_cleanup_module()
579  *
580  * Module unload.  This is where it all ends.
581  */
582 static void __exit dgnc_cleanup_module(void)
583 {
584         cleanup();
585         pci_unregister_driver(&dgnc_driver);
586 }
587 
588 /*
589  * init_module()
590  *
591  * Module load.  This is where it all starts.
592  */
593 static int __init dgnc_init_module(void)
594 {
595         int rc;
596 
597         /* Initialize global stuff */
598 
599         rc = dgnc_start();
600 
601         if (rc < 0)
602                 return rc;
603 
604         /* Find and configure all the cards */
605 
606         rc = pci_register_driver(&dgnc_driver);
607         if (rc) {
608                 pr_warn("WARNING: dgnc driver load failed.  No Digi Neo or Classic boards found.\n");
609                 cleanup();
610                 return rc;
611         }
612 
613         return 0;
614 }
615 
616 module_init(dgnc_init_module);
617 module_exit(dgnc_cleanup_module);
618 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us