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