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