2 * SBE 2T3E3 synchronous serial card driver for Linux
4 * Copyright (C) 2009-2010 Krzysztof Halasa <khc@pm.waw.pl>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License
8 * as published by the Free Software Foundation.
10 * This code is based on a driver written by SBE Inc.
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/netdevice.h>
17 #include <linux/pci.h>
18 #include <linux/hdlc.h>
19 #include <linux/if_arp.h>
20 #include <linux/interrupt.h>
23 static void check_leds(unsigned long arg)
25 struct card *card = (struct card *)arg;
26 struct channel *channel0 = &card->channels[0];
29 update_led(channel0, ++blinker);
30 if (has_two_ports(channel0->pdev))
31 update_led(&card->channels[1], blinker);
33 card->timer.expires = jiffies + HZ / 10;
34 add_timer(&card->timer);
37 static void t3e3_remove_channel(struct channel *channel)
39 struct pci_dev *pdev = channel->pdev;
40 struct net_device *dev = channel->dev;
42 /* system hangs if board asserts irq while module is unloaded */
43 cpld_stop_intr(channel);
44 free_irq(dev->irq, dev);
45 dc_drop_descriptor_list(channel);
46 unregister_hdlc_device(dev);
48 pci_release_regions(pdev);
49 pci_disable_device(pdev);
50 pci_set_drvdata(pdev, NULL);
53 static int __devinit t3e3_init_channel(struct channel *channel, struct pci_dev *pdev, struct card *card)
55 struct net_device *dev;
59 err = pci_enable_device(pdev);
63 err = pci_request_regions(pdev, "SBE 2T3E3");
67 dev = alloc_hdlcdev(channel);
69 printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
73 t3e3_sc_init(channel);
74 dev_to_priv(dev) = channel;
79 channel->addr = pci_resource_start(pdev, 0);
80 if (pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_2T3E3_P1)
85 if (setup_device(dev, channel))
88 pci_read_config_dword(channel->pdev, 0x40, &val); /* mask sleep mode */
89 pci_write_config_dword(channel->pdev, 0x40, val & 0x3FFFFFFF);
91 pci_read_config_byte(channel->pdev, PCI_CACHE_LINE_SIZE, &channel->h.cache_size);
92 pci_read_config_dword(channel->pdev, PCI_COMMAND, &channel->h.command);
95 if (request_irq(dev->irq, &t3e3_intr, IRQF_SHARED, dev->name, dev)) {
96 printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
100 pci_set_drvdata(pdev, channel);
104 pci_release_regions(pdev);
106 pci_disable_device(pdev);
110 static void __devexit t3e3_remove_card(struct pci_dev *pdev)
112 struct channel *channel0 = pci_get_drvdata(pdev);
113 struct card *card = channel0->card;
115 del_timer(&card->timer);
116 if (has_two_ports(channel0->pdev)) {
117 t3e3_remove_channel(&card->channels[1]);
118 pci_dev_put(card->channels[1].pdev);
120 t3e3_remove_channel(channel0);
124 static int __devinit t3e3_init_card(struct pci_dev *pdev, const struct pci_device_id *ent)
126 /* pdev points to channel #0 */
127 struct pci_dev *pdev1 = NULL;
129 int channels = 1, err;
131 if (has_two_ports(pdev)) {
132 while ((pdev1 = pci_get_subsys(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
133 PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P1,
135 if (pdev1->bus == pdev->bus &&
136 pdev1->devfn == pdev->devfn + 8 /* next device on the same bus */)
137 break; /* found the second channel */
140 printk(KERN_ERR "SBE 2T3E3" ": Can't find the second channel\n");
144 /* holds the reference for pdev1 */
147 card = kzalloc(sizeof(struct card) + channels * sizeof(struct channel), GFP_KERNEL);
149 printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
153 spin_lock_init(&card->bootrom_lock);
154 card->bootrom_addr = pci_resource_start(pdev, 0);
156 err = t3e3_init_channel(&card->channels[0], pdev, card);
161 err = t3e3_init_channel(&card->channels[1], pdev1, card);
163 t3e3_remove_channel(&card->channels[0]);
168 /* start LED timer */
169 init_timer(&card->timer);
170 card->timer.function = check_leds;
171 card->timer.expires = jiffies + HZ / 10;
172 card->timer.data = (unsigned long)card;
173 add_timer(&card->timer);
181 static struct pci_device_id t3e3_pci_tbl[] __devinitdata = {
182 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
183 PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_T3E3, 0, 0, 0 },
184 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
185 PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P0, 0, 0, 0 },
186 /* channel 1 will be initialized after channel 0 */
190 static struct pci_driver t3e3_pci_driver = {
192 .id_table = t3e3_pci_tbl,
193 .probe = t3e3_init_card,
194 .remove = t3e3_remove_card,
197 static int __init t3e3_init_module(void)
199 return pci_register_driver(&t3e3_pci_driver);
202 static void __exit t3e3_cleanup_module(void)
204 pci_unregister_driver(&t3e3_pci_driver);
207 module_init(t3e3_init_module);
208 module_exit(t3e3_cleanup_module);
209 MODULE_LICENSE("GPL");
210 MODULE_DEVICE_TABLE(pci, t3e3_pci_tbl);