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