blob: 015686ff48255fedbf0705111f9abc2afd99e63b [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Original driver code supplied by Multi-Tech
4 *
5 * Changes
Alan Cox8eb04cf2008-11-11 14:48:44 +00006 * 1/9/98 alan@lxorguk.ukuu.org.uk
7 * Merge to 2.0.x kernel tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Obtain and use official major/minors
9 * Loader switched to a misc device
10 * (fixed range check bug as a side effect)
11 * Printk clean up
Alan Cox8eb04cf2008-11-11 14:48:44 +000012 * 9/12/98 alan@lxorguk.ukuu.org.uk
13 * Rough port to 2.1.x
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * 10/6/99 sameer Merged the ISA and PCI drivers to
16 * a new unified driver.
17 *
18 * 3/9/99 sameer Added support for ISI4616 cards.
19 *
20 * 16/9/99 sameer We do not force RTS low anymore.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080021 * This is to prevent the firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * from getting confused.
23 *
24 * 26/10/99 sameer Cosmetic changes:The driver now
25 * dumps the Port Count information
26 * along with I/O address and IRQ.
27 *
28 * 13/12/99 sameer Fixed the problem with IRQ sharing.
29 *
30 * 10/5/00 sameer Fixed isicom_shutdown_board()
31 * to not lower DTR on all the ports
Jiri Slabyd8d16e42006-01-09 20:54:21 -080032 * when the last port on the card is
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * closed.
34 *
35 * 10/5/00 sameer Signal mask setup command added
Jiri Slabyd8d16e42006-01-09 20:54:21 -080036 * to isicom_setup_port and
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * isicom_shutdown_port.
38 *
39 * 24/5/00 sameer The driver is now SMP aware.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080040 *
41 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
Jiri Slabyd8d16e42006-01-09 20:54:21 -080043 *
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * 03/01/01 anil .s Added support for resetting the
46 * internal modems on ISI cards.
47 *
48 * 08/02/01 anil .s Upgraded the driver for kernel
49 * 2.4.x
50 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080051 * 11/04/01 Kevin Fixed firmware load problem with
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * ISIHP-4X card
Jiri Slabyd8d16e42006-01-09 20:54:21 -080053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * 30/04/01 anil .s Fixed the remote login through
55 * ISI port problem. Now the link
56 * does not go down before password
57 * prompt.
58 *
59 * 03/05/01 anil .s Fixed the problem with IRQ sharing
60 * among ISI-PCI cards.
61 *
62 * 03/05/01 anil .s Added support to display the version
Jiri Slabyd8d16e42006-01-09 20:54:21 -080063 * info during insmod as well as module
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * listing by lsmod.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080065 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * 10/05/01 anil .s Done the modifications to the source
67 * file and Install script so that the
68 * same installation can be used for
69 * 2.2.x and 2.4.x kernel.
70 *
71 * 06/06/01 anil .s Now we drop both dtr and rts during
72 * shutdown_port as well as raise them
73 * during isicom_config_port.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * 09/06/01 acme@conectiva.com.br use capable, not suser, do
76 * restore_flags on failure in
77 * isicom_send_break, verify put_user
78 * result
79 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080080 * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
81 * Baud index extended to 21
82 *
83 * 20/03/03 ranjeeth Made to work for Linux Advanced server.
84 * Taken care of license warning.
85 *
86 * 10/12/03 Ravindra Made to work for Fedora Core 1 of
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Red Hat Distribution
88 *
89 * 06/01/05 Alan Cox Merged the ISI and base kernel strands
90 * into a single 2.6 driver
91 *
92 * ***********************************************************
93 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080094 * To use this driver you also need the support package. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * can find this in RPM format on
96 * ftp://ftp.linux.org.uk/pub/linux/alan
Jiri Slabyd8d16e42006-01-09 20:54:21 -080097 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * You can find the original tools for this direct from Multitech
99 * ftp://ftp.multitech.com/ISI-Cards/
100 *
Lucas De Marchi970e2482012-03-30 13:37:16 -0700101 * Having installed the cards the module options (/etc/modprobe.d/)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
103 * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
104 *
105 * Omit those entries for boards you don't have installed.
106 *
107 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Merge testing
109 * 64-bit verification
110 */
111
Joe Perchesdb91340b2009-12-09 12:34:16 -0800112#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <linux/module.h>
Jiri Slabye65c1db2006-01-09 20:54:25 -0800115#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <linux/kernel.h>
117#include <linux/tty.h>
Alan Cox33f0f882006-01-09 20:54:13 -0800118#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#include <linux/termios.h>
120#include <linux/fs.h>
121#include <linux/sched.h>
122#include <linux/serial.h>
123#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/interrupt.h>
125#include <linux/timer.h>
126#include <linux/delay.h>
127#include <linux/ioport.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900128#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Alan Cox251b8dd2008-04-30 00:54:12 -0700130#include <linux/uaccess.h>
131#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#include <linux/pci.h>
134
135#include <linux/isicom.h>
136
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800137#define InterruptTheCard(base) outw(0, (base) + 0xc)
138#define ClearInterrupt(base) inw((base) + 0x0a)
139
140#ifdef DEBUG
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800141#define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
142#else
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800143#define isicom_paranoia_check(a, b, c) 0
144#endif
145
Jiri Slaby9ac09482006-01-09 20:54:24 -0800146static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500147static void isicom_remove(struct pci_dev *);
Jiri Slaby9ac09482006-01-09 20:54:24 -0800148
Arvind Yadav763653d2017-07-23 15:31:02 +0530149static const struct pci_device_id isicom_pci_tbl[] = {
Jiri Slaby9ac09482006-01-09 20:54:24 -0800150 { PCI_DEVICE(VENDOR_ID, 0x2028) },
151 { PCI_DEVICE(VENDOR_ID, 0x2051) },
152 { PCI_DEVICE(VENDOR_ID, 0x2052) },
153 { PCI_DEVICE(VENDOR_ID, 0x2053) },
154 { PCI_DEVICE(VENDOR_ID, 0x2054) },
155 { PCI_DEVICE(VENDOR_ID, 0x2055) },
156 { PCI_DEVICE(VENDOR_ID, 0x2056) },
157 { PCI_DEVICE(VENDOR_ID, 0x2057) },
158 { PCI_DEVICE(VENDOR_ID, 0x2058) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 { 0 }
160};
161MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
162
Jiri Slaby9ac09482006-01-09 20:54:24 -0800163static struct pci_driver isicom_driver = {
164 .name = "isicom",
165 .id_table = isicom_pci_tbl,
166 .probe = isicom_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -0500167 .remove = isicom_remove
Jiri Slaby9ac09482006-01-09 20:54:24 -0800168};
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int prev_card = 3; /* start servicing isi_card[0] */
171static struct tty_driver *isicom_normal;
172
Kees Cook24ed9602017-08-28 11:28:21 -0700173static void isicom_tx(struct timer_list *unused);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800174static void isicom_start(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Kees Cook1d27e3e2017-10-04 16:27:04 -0700176static DEFINE_TIMER(tx, isicom_tx);
Jiri Slaby34b55b82006-12-08 02:38:55 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* baud index mappings from linux defns to isi */
179
180static signed char linuxb_to_isib[] = {
Jiri Slaby7edc1362007-02-10 01:44:54 -0800181 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
184struct isi_board {
Jiri Slaby8070e352006-01-09 20:54:22 -0800185 unsigned long base;
Jiri Slaby4969b3a2007-07-17 04:05:18 -0700186 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned char port_count;
188 unsigned short status;
Jiri Slabya547dfe2006-01-09 20:54:26 -0800189 unsigned short port_status; /* each bit for each port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned short shift_count;
Alan Cox251b8dd2008-04-30 00:54:12 -0700191 struct isi_port *ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 signed char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
194 unsigned long flags;
Jiri Slaby938a7022006-12-08 02:38:52 -0800195 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198struct isi_port {
199 unsigned short magic;
Alan Coxf1d03222008-07-16 21:55:45 +0100200 struct tty_port port;
Jiri Slaby8070e352006-01-09 20:54:22 -0800201 u16 channel;
202 u16 status;
Alan Cox251b8dd2008-04-30 00:54:12 -0700203 struct isi_board *card;
Alan Cox251b8dd2008-04-30 00:54:12 -0700204 unsigned char *xmit_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int xmit_head;
206 int xmit_tail;
207 int xmit_cnt;
208};
209
210static struct isi_board isi_card[BOARD_COUNT];
211static struct isi_port isi_ports[PORT_COUNT];
212
213/*
214 * Locking functions for card level locking. We need to own both
215 * the kernel lock for the card and have the card in a position that
216 * it wants to talk.
217 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800218
Denys Vlasenko810e20e2015-10-27 17:39:58 +0100219static int WaitTillCardIsFree(unsigned long base)
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800220{
221 unsigned int count = 0;
222 unsigned int a = in_atomic(); /* do we run under spinlock? */
223
224 while (!(inw(base + 0xe) & 0x1) && count++ < 100)
225 if (a)
226 mdelay(1);
227 else
228 msleep(1);
229
230 return !(inw(base + 0xe) & 0x1);
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int lock_card(struct isi_board *card)
234{
Jiri Slaby8070e352006-01-09 20:54:22 -0800235 unsigned long base = card->base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700236 unsigned int retries, a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700238 for (retries = 0; retries < 10; retries++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 spin_lock_irqsave(&card->card_lock, card->flags);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700240 for (a = 0; a < 10; a++) {
241 if (inw(base + 0xe) & 0x1)
242 return 1;
243 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700245 spin_unlock_irqrestore(&card->card_lock, card->flags);
246 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Joe Perchese620e542014-11-09 22:46:35 -0800248 pr_warn("Failed to lock Card (0x%lx)\n", card->base);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800249
Adrian Bunk04187262006-06-30 18:23:04 +0200250 return 0; /* Failed to acquire the card! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static void unlock_card(struct isi_board *card)
254{
255 spin_unlock_irqrestore(&card->card_lock, card->flags);
256}
257
258/*
259 * ISI Card specific ops ...
260 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800261
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800262/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800263static void raise_dtr(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800265 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800266 unsigned long base = card->base;
267 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800269 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return;
271
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800272 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 outw(0x0504, base);
274 InterruptTheCard(base);
275 port->status |= ISI_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800278/* card->lock HAS to be held */
Denys Vlasenko9428d712015-10-27 18:46:34 +0100279static void drop_dtr(struct isi_port *port)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800280{
281 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800282 unsigned long base = card->base;
283 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800285 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return;
287
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800288 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 outw(0x0404, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800290 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 port->status &= ~ISI_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800294/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800295static inline void raise_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800297 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800298 unsigned long base = card->base;
299 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800301 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
303
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800304 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 outw(0x0a04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800306 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 port->status |= ISI_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800309
310/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800311static inline void drop_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800313 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800314 unsigned long base = card->base;
315 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800317 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
319
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800320 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 outw(0x0804, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800322 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 port->status &= ~ISI_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800326/* card->lock MUST NOT be held */
Alan Cox5d951fb2009-01-02 13:45:19 +0000327
Alan Coxfcc8ac12009-06-11 12:24:17 +0100328static void isicom_dtr_rts(struct tty_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Alan Cox5d951fb2009-01-02 13:45:19 +0000330 struct isi_port *ip = container_of(port, struct isi_port, port);
331 struct isi_board *card = ip->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800332 unsigned long base = card->base;
Alan Cox5d951fb2009-01-02 13:45:19 +0000333 u16 channel = ip->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (!lock_card(card))
336 return;
337
Alan Coxfcc8ac12009-06-11 12:24:17 +0100338 if (on) {
339 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
340 outw(0x0f04, base);
341 InterruptTheCard(base);
342 ip->status |= (ISI_DTR | ISI_RTS);
343 } else {
344 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
345 outw(0x0C04, base);
346 InterruptTheCard(base);
347 ip->status &= ~(ISI_DTR | ISI_RTS);
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unlock_card(card);
350}
351
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800352/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800353static void drop_dtr_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800355 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800356 unsigned long base = card->base;
357 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800359 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800362 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 outw(0x0c04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800364 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 port->status &= ~(ISI_RTS | ISI_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * ISICOM Driver specific routines ...
370 *
371 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800372
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800373static inline int __isicom_paranoia_check(struct isi_port const *port,
374 char *name, const char *routine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!port) {
Joe Perchese620e542014-11-09 22:46:35 -0800377 pr_warn("Warning: bad isicom magic for dev %s in %s\n",
378 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 1;
380 }
381 if (port->magic != ISICOM_MAGIC) {
Joe Perchese620e542014-11-09 22:46:35 -0800382 pr_warn("Warning: NULL isicom port for dev %s in %s\n",
383 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 1;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800385 }
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800391 * Transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
393 * We shovel data into the card buffers on a regular basis. The card
394 * will do the rest of the work for us.
395 */
396
Kees Cook24ed9602017-08-28 11:28:21 -0700397static void isicom_tx(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jiri Slaby4969b3a2007-07-17 04:05:18 -0700399 unsigned long flags, base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700400 unsigned int retries;
Jiri Slaby4969b3a2007-07-17 04:05:18 -0700401 short count = (BOARD_COUNT-1), card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 short txcount, wrd, residue, word_count, cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800403 struct isi_port *port;
404 struct tty_struct *tty;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* find next active board */
407 card = (prev_card + 1) & 0x0003;
Alan Cox251b8dd2008-04-30 00:54:12 -0700408 while (count-- > 0) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800409 if (isi_card[card].status & BOARD_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800411 card = (card + 1) & 0x0003;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 if (!(isi_card[card].status & BOARD_ACTIVE))
414 goto sched_again;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 prev_card = card;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 count = isi_card[card].port_count;
419 port = isi_card[card].ports;
420 base = isi_card[card].base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700421
422 spin_lock_irqsave(&isi_card[card].card_lock, flags);
423 for (retries = 0; retries < 100; retries++) {
424 if (inw(base + 0xe) & 0x1)
425 break;
426 udelay(2);
427 }
428 if (retries >= 100)
429 goto unlock;
430
Alan Coxd450b5a2008-10-13 10:39:58 +0100431 tty = tty_port_tty_get(&port->port);
432 if (tty == NULL)
433 goto put_unlock;
434
Alan Cox251b8dd2008-04-30 00:54:12 -0700435 for (; count > 0; count--, port++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* port not active or tx disabled to force flow control */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700437 if (!tty_port_initialized(&port->port) ||
438 !(port->status & ISI_TXOK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 continue;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700442 if (txcount <= 0 || tty->stopped || tty->hw_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 continue;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700444
445 if (!(inw(base + 0x02) & (1 << port->channel)))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800446 continue;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700447
Joe Perchesdb91340b2009-12-09 12:34:16 -0800448 pr_debug("txing %d bytes, port%d.\n",
449 txcount, port->channel + 1);
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800450 outw((port->channel << isi_card[card].shift_count) | txcount,
451 base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 residue = NO;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800453 wrd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 while (1) {
Jiri Slabya547dfe2006-01-09 20:54:26 -0800455 cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
456 - port->xmit_tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (residue == YES) {
458 residue = NO;
459 if (cnt > 0) {
Alan Coxf1d03222008-07-16 21:55:45 +0100460 wrd |= (port->port.xmit_buf[port->xmit_tail]
Jiri Slabya547dfe2006-01-09 20:54:26 -0800461 << 8);
462 port->xmit_tail = (port->xmit_tail + 1)
463 & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 port->xmit_cnt--;
465 txcount--;
466 cnt--;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800467 outw(wrd, base);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800468 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 outw(wrd, base);
470 break;
471 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800472 }
Alan Cox251b8dd2008-04-30 00:54:12 -0700473 if (cnt <= 0)
474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 word_count = cnt >> 1;
Alan Coxf1d03222008-07-16 21:55:45 +0100476 outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800477 port->xmit_tail = (port->xmit_tail
478 + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 txcount -= (word_count << 1);
480 port->xmit_cnt -= (word_count << 1);
481 if (cnt & 0x0001) {
482 residue = YES;
Alan Coxf1d03222008-07-16 21:55:45 +0100483 wrd = port->port.xmit_buf[port->xmit_tail];
Jiri Slabya547dfe2006-01-09 20:54:26 -0800484 port->xmit_tail = (port->xmit_tail + 1)
485 & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 port->xmit_cnt--;
487 txcount--;
488 }
489 }
490
491 InterruptTheCard(base);
492 if (port->xmit_cnt <= 0)
493 port->status &= ~ISI_TXOK;
494 if (port->xmit_cnt <= WAKEUP_CHARS)
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800495 tty_wakeup(tty);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Alan Coxd450b5a2008-10-13 10:39:58 +0100498put_unlock:
499 tty_kref_put(tty);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700500unlock:
501 spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800502 /* schedule another tx for hopefully in about 10ms */
503sched_again:
Jiri Slaby34b55b82006-12-08 02:38:55 -0800504 mod_timer(&tx, jiffies + msecs_to_jiffies(10));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800508 * Main interrupt handler routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800510
David Howells7d12e782006-10-05 14:55:46 +0100511static irqreturn_t isicom_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jiri Slaby8070e352006-01-09 20:54:22 -0800513 struct isi_board *card = dev_id;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800514 struct isi_port *port;
515 struct tty_struct *tty;
Jiri Slaby8070e352006-01-09 20:54:22 -0800516 unsigned long base;
517 u16 header, word_count, count, channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 short byte_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800519 unsigned char *rp;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!card || !(card->status & FIRMWARE_LOADED))
522 return IRQ_NONE;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 base = card->base;
Jiri Slabycb4a10c2007-02-10 01:44:53 -0800525
526 /* did the card interrupt us? */
527 if (!(inw(base + 0x0e) & 0x02))
528 return IRQ_NONE;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 spin_lock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800531
Jiri Slaby18234f82006-12-08 02:38:51 -0800532 /*
533 * disable any interrupts from the PCI card and lower the
534 * interrupt line
535 */
536 outw(0x8000, base+0x04);
537 ClearInterrupt(base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 inw(base); /* get the dummy word out */
540 header = inw(base);
541 channel = (header & 0x7800) >> card->shift_count;
542 byte_count = header & 0xff;
543
544 if (channel + 1 > card->port_count) {
Joe Perchese620e542014-11-09 22:46:35 -0800545 pr_warn("%s(0x%lx): %d(channel) > port_count\n",
546 __func__, base, channel + 1);
Jiri Slaby18234f82006-12-08 02:38:51 -0800547 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 spin_unlock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800549 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 port = card->ports + channel;
Peter Hurleyd41861c2016-04-09 17:53:25 -0700552 if (!tty_port_initialized(&port->port)) {
Jiri Slaby18234f82006-12-08 02:38:51 -0800553 outw(0x0000, base+0x04); /* enable interrupts */
Jiri Slaby174f1302007-02-10 01:44:52 -0800554 spin_unlock(&card->card_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800556 }
557
Alan Coxd450b5a2008-10-13 10:39:58 +0100558 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (tty == NULL) {
560 word_count = byte_count >> 1;
Alan Cox251b8dd2008-04-30 00:54:12 -0700561 while (byte_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 inw(base);
563 byte_count -= 2;
564 }
565 if (byte_count & 0x01)
566 inw(base);
Jiri Slaby18234f82006-12-08 02:38:51 -0800567 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 spin_unlock(&card->card_lock);
569 return IRQ_HANDLED;
570 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (header & 0x8000) { /* Status Packet */
573 header = inw(base);
Alan Cox251b8dd2008-04-30 00:54:12 -0700574 switch (header & 0xff) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800575 case 0: /* Change in EIA signals */
Peter Hurley2d686552016-04-09 17:53:23 -0700576 if (tty_port_check_carrier(&port->port)) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800577 if (port->status & ISI_DCD) {
578 if (!(header & ISI_DCD)) {
579 /* Carrier has been lost */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800580 pr_debug("%s: DCD->low.\n",
581 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 port->status &= ~ISI_DCD;
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800583 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800585 } else if (header & ISI_DCD) {
586 /* Carrier has been detected */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800587 pr_debug("%s: DCD->high.\n",
588 __func__);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800589 port->status |= ISI_DCD;
Alan Coxf1d03222008-07-16 21:55:45 +0100590 wake_up_interruptible(&port->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800592 } else {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800593 if (header & ISI_DCD)
594 port->status |= ISI_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800596 port->status &= ~ISI_DCD;
597 }
598
Huang Shijief21ec3d2012-08-22 22:13:36 -0400599 if (tty_port_cts_enabled(&port->port)) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100600 if (tty->hw_stopped) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800601 if (header & ISI_CTS) {
Jiri Slaby7342c592012-11-15 09:49:48 +0100602 tty->hw_stopped = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800603 /* start tx ing */
Jiri Slabya547dfe2006-01-09 20:54:26 -0800604 port->status |= (ISI_TXOK
605 | ISI_CTS);
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800606 tty_wakeup(tty);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800607 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800608 } else if (!(header & ISI_CTS)) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100609 tty->hw_stopped = 1;
Jiri Slabya547dfe2006-01-09 20:54:26 -0800610 /* stop tx ing */
611 port->status &= ~(ISI_TXOK | ISI_CTS);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800612 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800613 } else {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800614 if (header & ISI_CTS)
615 port->status |= ISI_CTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800617 port->status &= ~ISI_CTS;
618 }
619
620 if (header & ISI_DSR)
621 port->status |= ISI_DSR;
622 else
623 port->status &= ~ISI_DSR;
624
625 if (header & ISI_RI)
626 port->status |= ISI_RI;
627 else
628 port->status &= ~ISI_RI;
629
630 break;
631
Jiri Slabya547dfe2006-01-09 20:54:26 -0800632 case 1: /* Received Break !!! */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100633 tty_insert_flip_char(&port->port, 0, TTY_BREAK);
Alan Coxf1d03222008-07-16 21:55:45 +0100634 if (port->port.flags & ASYNC_SAK)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800635 do_SAK(tty);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100636 tty_flip_buffer_push(&port->port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800637 break;
638
639 case 2: /* Statistics */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800640 pr_debug("%s: stats!!!\n", __func__);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800641 break;
642
643 default:
Joe Perchesdb91340b2009-12-09 12:34:16 -0800644 pr_debug("%s: Unknown code in status packet.\n",
645 __func__);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800646 break;
647 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800648 } else { /* Data Packet */
Jiri Slaby2f693352013-01-03 15:53:02 +0100649 count = tty_prepare_flip_string(&port->port, &rp,
650 byte_count & ~1);
Joe Perchesdb91340b2009-12-09 12:34:16 -0800651 pr_debug("%s: Can rx %d of %d bytes.\n",
652 __func__, count, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 word_count = count >> 1;
Alan Cox33f0f882006-01-09 20:54:13 -0800654 insw(base, rp, word_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 byte_count -= (word_count << 1);
656 if (count & 0x0001) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100657 tty_insert_flip_char(&port->port, inw(base) & 0xff,
Jiri Slabya547dfe2006-01-09 20:54:26 -0800658 TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 byte_count -= 2;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (byte_count > 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -0800662 pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
663 __func__, base, channel + 1);
Alan Cox251b8dd2008-04-30 00:54:12 -0700664 /* drain out unread xtra data */
665 while (byte_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 inw(base);
667 byte_count -= 2;
668 }
669 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100670 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Jiri Slaby18234f82006-12-08 02:38:51 -0800672 outw(0x0000, base+0x04); /* enable interrupts */
Jiri Slaby174f1302007-02-10 01:44:52 -0800673 spin_unlock(&card->card_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +0100674 tty_kref_put(tty);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Alan Coxd450b5a2008-10-13 10:39:58 +0100679static void isicom_config_port(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Alan Coxd450b5a2008-10-13 10:39:58 +0100681 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800682 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned long baud;
Jiri Slaby8070e352006-01-09 20:54:22 -0800684 unsigned long base = card->base;
685 u16 channel_setup, channel = port->channel,
686 shift_count = card->shift_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned char flow_ctrl;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800688
Alan Cox251b8dd2008-04-30 00:54:12 -0700689 /* FIXME: Switch to new tty baud API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 baud = C_BAUD(tty);
691 if (baud & CBAUDEX) {
692 baud &= ~CBAUDEX;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
695 * then the card is programmed for 57.6Kbps or 115Kbps
696 * respectively.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800697 */
698
Jiri Slaby7edc1362007-02-10 01:44:54 -0800699 /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
700 if (baud < 1 || baud > 4)
Alan Coxadc8d742012-07-14 15:31:47 +0100701 tty->termios.c_cflag &= ~CBAUDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else
703 baud += 15;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (baud == 15) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800706
707 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * by the set_serial_info ioctl ... this is done by
709 * the 'setserial' utility.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800710 */
711
Alan Coxf1d03222008-07-16 21:55:45 +0100712 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800713 baud++; /* 57.6 Kbps */
Alan Coxf1d03222008-07-16 21:55:45 +0100714 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Alan Cox251b8dd2008-04-30 00:54:12 -0700715 baud += 2; /* 115 Kbps */
Alan Coxf1d03222008-07-16 21:55:45 +0100716 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby7edc1362007-02-10 01:44:54 -0800717 baud += 3; /* 230 kbps*/
Alan Coxf1d03222008-07-16 21:55:45 +0100718 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby7edc1362007-02-10 01:44:54 -0800719 baud += 4; /* 460 kbps*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 if (linuxb_to_isib[baud] == -1) {
722 /* hang up */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800723 drop_dtr(port);
724 return;
Alan Cox251b8dd2008-04-30 00:54:12 -0700725 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 raise_dtr(port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800727
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800728 if (WaitTillCardIsFree(base) == 0) {
Alan Cox251b8dd2008-04-30 00:54:12 -0700729 outw(0x8000 | (channel << shift_count) | 0x03, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
731 channel_setup = 0;
Alan Cox251b8dd2008-04-30 00:54:12 -0700732 switch (C_CSIZE(tty)) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800733 case CS5:
734 channel_setup |= ISICOM_CS5;
735 break;
736 case CS6:
737 channel_setup |= ISICOM_CS6;
738 break;
739 case CS7:
740 channel_setup |= ISICOM_CS7;
741 break;
742 case CS8:
743 channel_setup |= ISICOM_CS8;
744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (C_CSTOPB(tty))
748 channel_setup |= ISICOM_2SB;
749 if (C_PARENB(tty)) {
750 channel_setup |= ISICOM_EVPAR;
751 if (C_PARODD(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800752 channel_setup |= ISICOM_ODPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800754 outw(channel_setup, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 InterruptTheCard(base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800756 }
Peter Hurley2d686552016-04-09 17:53:23 -0700757 tty_port_set_check_carrier(&port->port, !C_CLOCAL(tty));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* flow control settings ...*/
760 flow_ctrl = 0;
Peter Hurley5604a982016-04-09 17:53:21 -0700761 tty_port_set_cts_flow(&port->port, C_CRTSCTS(tty));
762 if (C_CRTSCTS(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 flow_ctrl |= ISICOM_CTSRTS;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800764 if (I_IXON(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
766 if (I_IXOFF(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800767 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
768
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800769 if (WaitTillCardIsFree(base) == 0) {
Alan Cox251b8dd2008-04-30 00:54:12 -0700770 outw(0x8000 | (channel << shift_count) | 0x04, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 outw(flow_ctrl << 8 | 0x05, base);
772 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
773 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* rx enabled -> enable port for rx on the card */
777 if (C_CREAD(tty)) {
778 card->port_status |= (1 << channel);
779 outw(card->port_status, base + 0x02);
780 }
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800783/* open et all */
784
785static inline void isicom_setup_board(struct isi_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 int channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800788 struct isi_port *port;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800789
Alan Coxbaaa08a2009-11-30 13:17:24 +0000790 bp->count++;
Alan Cox6ed847d2009-11-30 13:17:30 +0000791 if (!(bp->status & BOARD_INIT)) {
792 port = bp->ports;
793 for (channel = 0; channel < bp->port_count; channel++, port++)
794 drop_dtr_rts(port);
795 }
796 bp->status |= BOARD_ACTIVE | BOARD_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800798
Alan Cox6ed847d2009-11-30 13:17:30 +0000799/* Activate and thus setup board are protected from races against shutdown
800 by the tty_port mutex */
801
Alan Coxbaaa08a2009-11-30 13:17:24 +0000802static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Alan Coxbaaa08a2009-11-30 13:17:24 +0000804 struct isi_port *port = container_of(tport, struct isi_port, port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800805 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800807
Alan Coxbaaa08a2009-11-30 13:17:24 +0000808 if (tty_port_alloc_xmit_buf(tport) < 0)
Alan Coxf1d03222008-07-16 21:55:45 +0100809 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxbaaa08a2009-11-30 13:17:24 +0000812 isicom_setup_board(card);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* discard any residual data */
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800817 if (WaitTillCardIsFree(card->base) == 0) {
818 outw(0x8000 | (port->channel << card->shift_count) | 0x02,
819 card->base);
820 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
821 InterruptTheCard(card->base);
822 }
Alan Coxd450b5a2008-10-13 10:39:58 +0100823 isicom_config_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800825
826 return 0;
827}
828
Alan Cox31f35932009-01-02 13:45:05 +0000829static int isicom_carrier_raised(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Alan Cox31f35932009-01-02 13:45:05 +0000831 struct isi_port *ip = container_of(port, struct isi_port, port);
832 return (ip->status & ISI_DCD)?1 : 0;
833}
834
Alan Cox11d85d72009-09-19 13:13:27 -0700835static struct tty_port *isicom_find_port(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800837 struct isi_port *port;
838 struct isi_board *card;
Jiri Slaby17c4edf2006-12-08 02:38:57 -0800839 unsigned int board;
Alan Cox11d85d72009-09-19 13:13:27 -0700840 int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 board = BOARD(line);
843 card = &isi_card[board];
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!(card->status & FIRMWARE_LOADED))
Alan Cox11d85d72009-09-19 13:13:27 -0700846 return NULL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* open on a port greater than the port count for the card !!! */
849 if (line > ((board * 16) + card->port_count - 1))
Alan Cox11d85d72009-09-19 13:13:27 -0700850 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800852 port = &isi_ports[line];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (isicom_paranoia_check(port, tty->name, "isicom_open"))
Alan Cox11d85d72009-09-19 13:13:27 -0700854 return NULL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800855
Alan Cox11d85d72009-09-19 13:13:27 -0700856 return &port->port;
857}
Alan Coxbaaa08a2009-11-30 13:17:24 +0000858
Alan Cox11d85d72009-09-19 13:13:27 -0700859static int isicom_open(struct tty_struct *tty, struct file *filp)
860{
861 struct isi_port *port;
Alan Cox11d85d72009-09-19 13:13:27 -0700862 struct tty_port *tport;
Alan Cox11d85d72009-09-19 13:13:27 -0700863
864 tport = isicom_find_port(tty);
865 if (tport == NULL)
866 return -ENODEV;
867 port = container_of(tport, struct isi_port, port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800868
Alan Coxa2d1e352010-04-23 16:01:18 +0100869 tty->driver_data = port;
Alan Coxbaaa08a2009-11-30 13:17:24 +0000870 return tty_port_open(tport, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/* close et all */
874
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800875/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800876static void isicom_shutdown_port(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800878 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (--card->count < 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -0800881 pr_debug("%s: bad board(0x%lx) count %d.\n",
882 __func__, card->base, card->count);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800883 card->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Alan Coxbaaa08a2009-11-30 13:17:24 +0000885 /* last port was closed, shutdown that board too */
Alan Cox6ed847d2009-11-30 13:17:30 +0000886 if (!card->count)
887 card->status &= BOARD_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Alan Cox978e5952008-04-30 00:53:59 -0700890static void isicom_flush_buffer(struct tty_struct *tty)
891{
892 struct isi_port *port = tty->driver_data;
893 struct isi_board *card = port->card;
894 unsigned long flags;
895
896 if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
897 return;
898
899 spin_lock_irqsave(&card->card_lock, flags);
900 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
901 spin_unlock_irqrestore(&card->card_lock, flags);
902
903 tty_wakeup(tty);
904}
905
Alan Coxbaaa08a2009-11-30 13:17:24 +0000906static void isicom_shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Alan Cox6f6412b2009-09-19 13:13:21 -0700908 struct isi_port *ip = container_of(port, struct isi_port, port);
909 struct isi_board *card = ip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800911
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800912 /* indicate to the card that no more data can be received
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 on this port */
914 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxbaaa08a2009-11-30 13:17:24 +0000915 card->port_status &= ~(1 << ip->channel);
916 outw(card->port_status, card->base + 0x02);
Alan Coxa6614992009-01-02 13:46:50 +0000917 isicom_shutdown_port(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 spin_unlock_irqrestore(&card->card_lock, flags);
Alan Cox2493c0c2009-11-30 13:17:46 +0000919 tty_port_free_xmit_buf(port);
Alan Cox6f6412b2009-09-19 13:13:21 -0700920}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800921
Alan Cox6f6412b2009-09-19 13:13:21 -0700922static void isicom_close(struct tty_struct *tty, struct file *filp)
923{
924 struct isi_port *ip = tty->driver_data;
Alan Coxa2d1e352010-04-23 16:01:18 +0100925 struct tty_port *port;
926
927 if (ip == NULL)
928 return;
929
930 port = &ip->port;
Alan Cox6f6412b2009-09-19 13:13:21 -0700931 if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
932 return;
Alan Coxbaaa08a2009-11-30 13:17:24 +0000933 tty_port_close(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/* write et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800937static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
938 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jiri Slaby8070e352006-01-09 20:54:22 -0800940 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800941 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned long flags;
943 int cnt, total = 0;
944
945 if (isicom_paranoia_check(port, tty->name, "isicom_write"))
946 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 spin_lock_irqsave(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800949
Alan Cox251b8dd2008-04-30 00:54:12 -0700950 while (1) {
Jiri Slabya547dfe2006-01-09 20:54:26 -0800951 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
952 - 1, SERIAL_XMIT_SIZE - port->xmit_head));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800953 if (cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800955
Alan Coxf1d03222008-07-16 21:55:45 +0100956 memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800957 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
958 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 port->xmit_cnt += cnt;
960 buf += cnt;
961 count -= cnt;
962 total += cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
965 port->status |= ISI_TXOK;
966 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800967 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/* put_char et all */
Alan Coxf34d7a52008-04-30 00:54:13 -0700971static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jiri Slaby8070e352006-01-09 20:54:22 -0800973 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800974 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
Alan Coxf34d7a52008-04-30 00:54:13 -0700978 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700981 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
982 spin_unlock_irqrestore(&card->card_lock, flags);
983 return 0;
984 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800985
Alan Coxf1d03222008-07-16 21:55:45 +0100986 port->port.xmit_buf[port->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
988 port->xmit_cnt++;
989 spin_unlock_irqrestore(&card->card_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700990 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/* flush_chars et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800994static void isicom_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Jiri Slaby8070e352006-01-09 20:54:22 -0800996 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
999 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001000
Jiri Slabya547dfe2006-01-09 20:54:26 -08001001 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
Alan Coxf1d03222008-07-16 21:55:45 +01001002 !port->port.xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* this tells the transmitter to consider this port for
1006 data output to the card ... that's the best we can do. */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001007 port->status |= ISI_TXOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010/* write_room et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001011static int isicom_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Jiri Slaby8070e352006-01-09 20:54:22 -08001013 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int free;
1015
1016 if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1017 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1020 if (free < 0)
1021 free = 0;
1022 return free;
1023}
1024
1025/* chars_in_buffer et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001026static int isicom_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Jiri Slaby8070e352006-01-09 20:54:22 -08001028 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1030 return 0;
1031 return port->xmit_cnt;
1032}
1033
1034/* ioctl et all */
Alan Cox6d889722008-07-22 11:18:21 +01001035static int isicom_send_break(struct tty_struct *tty, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Alan Cox6d889722008-07-22 11:18:21 +01001037 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001038 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -08001039 unsigned long base = card->base;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001040
Alan Cox6d889722008-07-22 11:18:21 +01001041 if (length == -1)
1042 return -EOPNOTSUPP;
1043
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001044 if (!lock_card(card))
Alan Cox6d889722008-07-22 11:18:21 +01001045 return -EINVAL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1048 outw((length & 0xff) << 8 | 0x00, base);
Arnd Bergmannf3e2d562015-01-13 22:59:47 +01001049 outw((length & 0xff00u), base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 InterruptTheCard(base);
1051
1052 unlock_card(card);
Alan Cox6d889722008-07-22 11:18:21 +01001053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Alan Cox60b33c12011-02-14 16:26:14 +00001056static int isicom_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Jiri Slaby8070e352006-01-09 20:54:22 -08001058 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* just send the port status */
Jiri Slaby8070e352006-01-09 20:54:22 -08001060 u16 status = port->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1063 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1066 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1067 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1068 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1069 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1070 ((status & ISI_RI ) ? TIOCM_RI : 0);
1071}
1072
Alan Cox20b9d172011-02-14 16:26:50 +00001073static int isicom_tiocmset(struct tty_struct *tty,
1074 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Jiri Slaby8070e352006-01-09 20:54:22 -08001076 struct isi_port *port = tty->driver_data;
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001077 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1080 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001081
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001082 spin_lock_irqsave(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (set & TIOCM_RTS)
1084 raise_rts(port);
1085 if (set & TIOCM_DTR)
1086 raise_dtr(port);
1087
1088 if (clear & TIOCM_RTS)
1089 drop_rts(port);
1090 if (clear & TIOCM_DTR)
1091 drop_dtr(port);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001092 spin_unlock_irqrestore(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Alan Coxd450b5a2008-10-13 10:39:58 +01001097static int isicom_set_serial_info(struct tty_struct *tty,
1098 struct serial_struct __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Alan Coxd450b5a2008-10-13 10:39:58 +01001100 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct serial_struct newinfo;
1102 int reconfig_port;
1103
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001104 if (copy_from_user(&newinfo, info, sizeof(newinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EFAULT;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001106
Alan Coxec82db12010-06-01 22:52:44 +02001107 mutex_lock(&port->port.mutex);
Alan Coxf1d03222008-07-16 21:55:45 +01001108 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001109 (newinfo.flags & ASYNC_SPD_MASK));
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!capable(CAP_SYS_ADMIN)) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001112 if ((newinfo.close_delay != port->port.close_delay) ||
1113 (newinfo.closing_wait != port->port.closing_wait) ||
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001114 ((newinfo.flags & ~ASYNC_USR_MASK) !=
Alan Coxf1d03222008-07-16 21:55:45 +01001115 (port->port.flags & ~ASYNC_USR_MASK))) {
Alan Coxec82db12010-06-01 22:52:44 +02001116 mutex_unlock(&port->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -EPERM;
Alan Cox1eac4942008-04-30 00:53:19 -07001118 }
Alan Coxf1d03222008-07-16 21:55:45 +01001119 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 (newinfo.flags & ASYNC_USR_MASK));
Alan Cox251b8dd2008-04-30 00:54:12 -07001121 } else {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001122 port->port.close_delay = newinfo.close_delay;
1123 port->port.closing_wait = newinfo.closing_wait;
Alan Coxf1d03222008-07-16 21:55:45 +01001124 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 (newinfo.flags & ASYNC_FLAGS));
1126 }
1127 if (reconfig_port) {
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001128 unsigned long flags;
1129 spin_lock_irqsave(&port->card->card_lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001130 isicom_config_port(tty);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001131 spin_unlock_irqrestore(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Alan Coxec82db12010-06-01 22:52:44 +02001133 mutex_unlock(&port->port.mutex);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001134 return 0;
1135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001137static int isicom_get_serial_info(struct isi_port *port,
1138 struct serial_struct __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct serial_struct out_info;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001141
Alan Coxec82db12010-06-01 22:52:44 +02001142 mutex_lock(&port->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 memset(&out_info, 0, sizeof(out_info));
1144/* out_info.type = ? */
1145 out_info.line = port - isi_ports;
1146 out_info.port = port->card->base;
1147 out_info.irq = port->card->irq;
Alan Coxf1d03222008-07-16 21:55:45 +01001148 out_info.flags = port->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/* out_info.baud_base = ? */
Alan Cox44b7d1b2008-07-16 21:57:18 +01001150 out_info.close_delay = port->port.close_delay;
1151 out_info.closing_wait = port->port.closing_wait;
Alan Coxec82db12010-06-01 22:52:44 +02001152 mutex_unlock(&port->port.mutex);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001153 if (copy_to_user(info, &out_info, sizeof(out_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return -EFAULT;
1155 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Alan Cox6caa76b2011-02-14 16:27:22 +00001158static int isicom_ioctl(struct tty_struct *tty,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001159 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Jiri Slaby8070e352006-01-09 20:54:22 -08001161 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1165 return -ENODEV;
1166
Alan Cox251b8dd2008-04-30 00:54:12 -07001167 switch (cmd) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001168 case TIOCGSERIAL:
1169 return isicom_get_serial_info(port, argp);
1170
1171 case TIOCSSERIAL:
Alan Coxd450b5a2008-10-13 10:39:58 +01001172 return isicom_set_serial_info(tty, argp);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001173
1174 default:
1175 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 return 0;
1178}
1179
1180/* set_termios et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001181static void isicom_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001182 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jiri Slaby8070e352006-01-09 20:54:22 -08001184 struct isi_port *port = tty->driver_data;
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001185 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1188 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001189
Alan Coxadc8d742012-07-14 15:31:47 +01001190 if (tty->termios.c_cflag == old_termios->c_cflag &&
1191 tty->termios.c_iflag == old_termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001193
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001194 spin_lock_irqsave(&port->card->card_lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001195 isicom_config_port(tty);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001196 spin_unlock_irqrestore(&port->card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001197
Peter Hurley9db276f2016-01-10 20:36:15 -08001198 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tty->hw_stopped = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001200 isicom_start(tty);
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204/* throttle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001205static void isicom_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Jiri Slaby8070e352006-01-09 20:54:22 -08001207 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001208 struct isi_board *card = port->card;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1211 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* tell the card that this port cannot handle any more data for now */
1214 card->port_status &= ~(1 << port->channel);
1215 outw(card->port_status, card->base + 0x02);
1216}
1217
1218/* unthrottle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001219static void isicom_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Jiri Slaby8070e352006-01-09 20:54:22 -08001221 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001222 struct isi_board *card = port->card;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1225 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* tell the card that this port is ready to accept more data */
1228 card->port_status |= (1 << port->channel);
1229 outw(card->port_status, card->base + 0x02);
1230}
1231
1232/* stop et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001233static void isicom_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Jiri Slaby8070e352006-01-09 20:54:22 -08001235 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1238 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* this tells the transmitter not to consider this port for
1241 data output to the card. */
1242 port->status &= ~ISI_TXOK;
1243}
1244
1245/* start et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001246static void isicom_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Jiri Slaby8070e352006-01-09 20:54:22 -08001248 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1251 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* this tells the transmitter to consider this port for
1254 data output to the card. */
1255 port->status |= ISI_TXOK;
1256}
1257
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001258static void isicom_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Jiri Slaby8070e352006-01-09 20:54:22 -08001260 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1263 return;
Alan Cox3e616962009-01-02 13:45:26 +00001264 tty_port_hangup(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jiri Slaby9ac09482006-01-09 20:54:24 -08001268/*
1269 * Driver init and deinit functions
1270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001272static const struct tty_operations isicom_ops = {
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001273 .open = isicom_open,
1274 .close = isicom_close,
1275 .write = isicom_write,
1276 .put_char = isicom_put_char,
1277 .flush_chars = isicom_flush_chars,
1278 .write_room = isicom_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 .chars_in_buffer = isicom_chars_in_buffer,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001280 .ioctl = isicom_ioctl,
1281 .set_termios = isicom_set_termios,
1282 .throttle = isicom_throttle,
1283 .unthrottle = isicom_unthrottle,
1284 .stop = isicom_stop,
1285 .start = isicom_start,
1286 .hangup = isicom_hangup,
1287 .flush_buffer = isicom_flush_buffer,
1288 .tiocmget = isicom_tiocmget,
1289 .tiocmset = isicom_tiocmset,
Alan Cox6d889722008-07-22 11:18:21 +01001290 .break_ctl = isicom_send_break,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291};
1292
Alan Cox31f35932009-01-02 13:45:05 +00001293static const struct tty_port_operations isicom_port_ops = {
1294 .carrier_raised = isicom_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001295 .dtr_rts = isicom_dtr_rts,
Alan Coxbaaa08a2009-11-30 13:17:24 +00001296 .activate = isicom_activate,
1297 .shutdown = isicom_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +00001298};
1299
Bill Pemberton9671f092012-11-19 13:21:50 -05001300static int reset_card(struct pci_dev *pdev,
Jiri Slaby9ac09482006-01-09 20:54:24 -08001301 const unsigned int card, unsigned int *signature)
1302{
1303 struct isi_board *board = pci_get_drvdata(pdev);
1304 unsigned long base = board->base;
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001305 unsigned int sig, portcount = 0;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001306 int retval = 0;
1307
1308 dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
1309 base);
1310
1311 inw(base + 0x8);
1312
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001313 msleep(10);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001314
1315 outw(0, base + 0x8); /* Reset */
1316
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001317 msleep(1000);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001318
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001319 sig = inw(base + 0x4) & 0xff;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001320
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001321 if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
1322 sig != 0xee) {
1323 dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
1324 "bad I/O Port Address 0x%lx).\n", card + 1, base);
1325 dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
Jiri Slaby18234f82006-12-08 02:38:51 -08001326 retval = -EIO;
1327 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001330 msleep(10);
1331
1332 portcount = inw(base + 0x2);
Julia Lawall07fb6f22008-03-04 14:29:17 -08001333 if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001334 portcount != 8 && portcount != 16)) {
Joe Perches898eb712007-10-18 03:06:30 -07001335 dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001336 card + 1);
1337 retval = -EIO;
1338 goto end;
1339 }
1340
1341 switch (sig) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001342 case 0xa5:
1343 case 0xbb:
1344 case 0xdd:
Jiri Slaby18234f82006-12-08 02:38:51 -08001345 board->port_count = (portcount == 4) ? 4 : 8;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001346 board->shift_count = 12;
1347 break;
1348 case 0xcc:
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001349 case 0xee:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001350 board->port_count = 16;
1351 board->shift_count = 11;
1352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001354 dev_info(&pdev->dev, "-Done\n");
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001355 *signature = sig;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001356
1357end:
1358 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Bill Pemberton9671f092012-11-19 13:21:50 -05001361static int load_firmware(struct pci_dev *pdev,
Jiri Slabye65c1db2006-01-09 20:54:25 -08001362 const unsigned int index, const unsigned int signature)
1363{
1364 struct isi_board *board = pci_get_drvdata(pdev);
1365 const struct firmware *fw;
1366 unsigned long base = board->base;
1367 unsigned int a;
1368 u16 word_count, status;
1369 int retval = -EIO;
1370 char *name;
1371 u8 *data;
1372
1373 struct stframe {
1374 u16 addr;
1375 u16 count;
1376 u8 data[0];
1377 } *frame;
1378
1379 switch (signature) {
1380 case 0xa5:
1381 name = "isi608.bin";
1382 break;
1383 case 0xbb:
1384 name = "isi608em.bin";
1385 break;
1386 case 0xcc:
1387 name = "isi616em.bin";
1388 break;
1389 case 0xdd:
1390 name = "isi4608.bin";
1391 break;
1392 case 0xee:
1393 name = "isi4616.bin";
1394 break;
1395 default:
1396 dev_err(&pdev->dev, "Unknown signature.\n");
1397 goto end;
Alan Cox251b8dd2008-04-30 00:54:12 -07001398 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001399
1400 retval = request_firmware(&fw, name, &pdev->dev);
1401 if (retval)
1402 goto end;
1403
Jiri Slabye4e04082006-09-30 23:27:50 -07001404 retval = -EIO;
1405
Jiri Slabye65c1db2006-01-09 20:54:25 -08001406 for (frame = (struct stframe *)fw->data;
1407 frame < (struct stframe *)(fw->data + fw->size);
Jiri Slabye4e04082006-09-30 23:27:50 -07001408 frame = (struct stframe *)((u8 *)(frame + 1) +
1409 frame->count)) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001410 if (WaitTillCardIsFree(base))
1411 goto errrelfw;
1412
1413 outw(0xf0, base); /* start upload sequence */
1414 outw(0x00, base);
1415 outw(frame->addr, base); /* lsb of address */
1416
1417 word_count = frame->count / 2 + frame->count % 2;
1418 outw(word_count, base);
1419 InterruptTheCard(base);
1420
1421 udelay(100); /* 0x2f */
1422
1423 if (WaitTillCardIsFree(base))
1424 goto errrelfw;
1425
Alan Cox251b8dd2008-04-30 00:54:12 -07001426 status = inw(base + 0x4);
1427 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001428 dev_warn(&pdev->dev, "Card%d rejected load header:\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001429 "Address:0x%x\n"
1430 "Count:0x%x\n"
1431 "Status:0x%x\n",
1432 index + 1, frame->addr, frame->count, status);
Jiri Slabye65c1db2006-01-09 20:54:25 -08001433 goto errrelfw;
1434 }
1435 outsw(base, frame->data, word_count);
1436
1437 InterruptTheCard(base);
1438
1439 udelay(50); /* 0x0f */
1440
1441 if (WaitTillCardIsFree(base))
1442 goto errrelfw;
1443
Alan Cox251b8dd2008-04-30 00:54:12 -07001444 status = inw(base + 0x4);
1445 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001446 dev_err(&pdev->dev, "Card%d got out of sync.Card "
1447 "Status:0x%x\n", index + 1, status);
1448 goto errrelfw;
1449 }
Alan Cox251b8dd2008-04-30 00:54:12 -07001450 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001451
Jiri Slabye65c1db2006-01-09 20:54:25 -08001452/* XXX: should we test it by reading it back and comparing with original like
1453 * in load firmware package? */
Jiri Slabye4e04082006-09-30 23:27:50 -07001454 for (frame = (struct stframe *)fw->data;
1455 frame < (struct stframe *)(fw->data + fw->size);
1456 frame = (struct stframe *)((u8 *)(frame + 1) +
1457 frame->count)) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001458 if (WaitTillCardIsFree(base))
1459 goto errrelfw;
1460
1461 outw(0xf1, base); /* start download sequence */
1462 outw(0x00, base);
1463 outw(frame->addr, base); /* lsb of address */
1464
1465 word_count = (frame->count >> 1) + frame->count % 2;
1466 outw(word_count + 1, base);
1467 InterruptTheCard(base);
1468
1469 udelay(50); /* 0xf */
1470
1471 if (WaitTillCardIsFree(base))
1472 goto errrelfw;
1473
Alan Cox251b8dd2008-04-30 00:54:12 -07001474 status = inw(base + 0x4);
1475 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001476 dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001477 "Address:0x%x\n"
1478 "Count:0x%x\n"
1479 "Status: 0x%x\n",
1480 index + 1, frame->addr, frame->count, status);
Jiri Slabye65c1db2006-01-09 20:54:25 -08001481 goto errrelfw;
1482 }
1483
1484 data = kmalloc(word_count * 2, GFP_KERNEL);
Jiri Slabyf0671372006-12-08 02:38:54 -08001485 if (data == NULL) {
1486 dev_err(&pdev->dev, "Card%d, firmware upload "
1487 "failed, not enough memory\n", index + 1);
1488 goto errrelfw;
1489 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001490 inw(base);
1491 insw(base, data, word_count);
1492 InterruptTheCard(base);
1493
1494 for (a = 0; a < frame->count; a++)
1495 if (data[a] != frame->data[a]) {
1496 kfree(data);
1497 dev_err(&pdev->dev, "Card%d, firmware upload "
1498 "failed\n", index + 1);
1499 goto errrelfw;
1500 }
1501 kfree(data);
1502
1503 udelay(50); /* 0xf */
1504
1505 if (WaitTillCardIsFree(base))
1506 goto errrelfw;
1507
Alan Cox251b8dd2008-04-30 00:54:12 -07001508 status = inw(base + 0x4);
1509 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001510 dev_err(&pdev->dev, "Card%d verify got out of sync. "
1511 "Card Status:0x%x\n", index + 1, status);
1512 goto errrelfw;
1513 }
1514 }
1515
Jiri Slabye4e04082006-09-30 23:27:50 -07001516 /* xfer ctrl */
1517 if (WaitTillCardIsFree(base))
1518 goto errrelfw;
1519
1520 outw(0xf2, base);
1521 outw(0x800, base);
1522 outw(0x0, base);
1523 outw(0x0, base);
1524 InterruptTheCard(base);
1525 outw(0x0, base + 0x4); /* for ISI4608 cards */
1526
Jiri Slabye65c1db2006-01-09 20:54:25 -08001527 board->status |= FIRMWARE_LOADED;
1528 retval = 0;
1529
1530errrelfw:
1531 release_firmware(fw);
1532end:
1533 return retval;
1534}
1535
Jiri Slaby9ac09482006-01-09 20:54:24 -08001536/*
1537 * Insmod can set static symbols so keep these static
1538 */
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001539static unsigned int card_count;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001540
Bill Pemberton9671f092012-11-19 13:21:50 -05001541static int isicom_probe(struct pci_dev *pdev,
Jiri Slaby9ac09482006-01-09 20:54:24 -08001542 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Jiri Slaby9653a692009-06-17 16:27:48 -07001544 unsigned int uninitialized_var(signature), index;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001545 int retval = -EPERM;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001546 struct isi_board *board = NULL;
1547
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001548 if (card_count >= BOARD_COUNT)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001549 goto err;
1550
Jiri Slabye1e57702008-07-16 21:52:56 +01001551 retval = pci_enable_device(pdev);
1552 if (retval) {
1553 dev_err(&pdev->dev, "failed to enable\n");
1554 goto err;
1555 }
1556
Jiri Slaby9ac09482006-01-09 20:54:24 -08001557 dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
1558
1559 /* allot the first empty slot in the array */
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001560 for (index = 0; index < BOARD_COUNT; index++) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001561 if (isi_card[index].base == 0) {
1562 board = &isi_card[index];
1563 break;
1564 }
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001565 }
1566 if (index == BOARD_COUNT) {
1567 retval = -ENODEV;
1568 goto err_disable;
1569 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001570
Jiri Slaby938a7022006-12-08 02:38:52 -08001571 board->index = index;
Jiri Slaby4969b3a2007-07-17 04:05:18 -07001572 board->base = pci_resource_start(pdev, 3);
1573 board->irq = pdev->irq;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001574 card_count++;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001575
1576 pci_set_drvdata(pdev, board);
1577
Jiri Slaby78028da2006-12-08 02:38:53 -08001578 retval = pci_request_region(pdev, 3, ISICOM_NAME);
1579 if (retval) {
Jiri Slaby09a4a112006-12-08 02:38:49 -08001580 dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
1581 "will be disabled.\n", board->base, board->base + 15,
1582 index + 1);
1583 retval = -EBUSY;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001584 goto errdec;
Alan Cox251b8dd2008-04-30 00:54:12 -07001585 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001586
Jiri Slaby09a4a112006-12-08 02:38:49 -08001587 retval = request_irq(board->irq, isicom_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001588 IRQF_SHARED, ISICOM_NAME, board);
Jiri Slaby09a4a112006-12-08 02:38:49 -08001589 if (retval < 0) {
1590 dev_err(&pdev->dev, "Could not install handler at Irq %d. "
1591 "Card%d will be disabled.\n", board->irq, index + 1);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001592 goto errunrr;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001593 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001594
1595 retval = reset_card(pdev, index, &signature);
1596 if (retval < 0)
1597 goto errunri;
1598
Jiri Slabye65c1db2006-01-09 20:54:25 -08001599 retval = load_firmware(pdev, index, signature);
1600 if (retval < 0)
1601 goto errunri;
1602
Jiri Slabyd0f59142012-11-15 09:49:55 +01001603 for (index = 0; index < board->port_count; index++) {
1604 struct tty_port *tport = &board->ports[index].port;
1605 tty_port_init(tport);
1606 tport->ops = &isicom_port_ops;
1607 tport->close_delay = 50 * HZ/100;
1608 tport->closing_wait = 3000 * HZ/100;
1609 tty_port_register_device(tport, isicom_normal,
1610 board->index * 16 + index, &pdev->dev);
1611 }
Jiri Slaby938a7022006-12-08 02:38:52 -08001612
Jiri Slaby9ac09482006-01-09 20:54:24 -08001613 return 0;
1614
1615errunri:
1616 free_irq(board->irq, board);
1617errunrr:
Jiri Slaby78028da2006-12-08 02:38:53 -08001618 pci_release_region(pdev, 3);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001619errdec:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001620 board->base = 0;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001621 card_count--;
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001622err_disable:
Jiri Slabye1e57702008-07-16 21:52:56 +01001623 pci_disable_device(pdev);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001624err:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001625 return retval;
1626}
1627
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001628static void isicom_remove(struct pci_dev *pdev)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001629{
1630 struct isi_board *board = pci_get_drvdata(pdev);
Jiri Slaby938a7022006-12-08 02:38:52 -08001631 unsigned int i;
1632
Jiri Slabyd0f59142012-11-15 09:49:55 +01001633 for (i = 0; i < board->port_count; i++) {
Jiri Slaby938a7022006-12-08 02:38:52 -08001634 tty_unregister_device(isicom_normal, board->index * 16 + i);
Jiri Slabyd0f59142012-11-15 09:49:55 +01001635 tty_port_destroy(&board->ports[i].port);
1636 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001637
1638 free_irq(board->irq, board);
Jiri Slaby78028da2006-12-08 02:38:53 -08001639 pci_release_region(pdev, 3);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001640 board->base = 0;
1641 card_count--;
Jiri Slabye1e57702008-07-16 21:52:56 +01001642 pci_disable_device(pdev);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001643}
1644
Jiri Slabyca262002006-12-08 02:38:50 -08001645static int __init isicom_init(void)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001646{
1647 int retval, idx, channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001648 struct isi_port *port;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001649
Alan Cox251b8dd2008-04-30 00:54:12 -07001650 for (idx = 0; idx < BOARD_COUNT; idx++) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001651 port = &isi_ports[idx * 16];
1652 isi_card[idx].ports = port;
1653 spin_lock_init(&isi_card[idx].card_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 for (channel = 0; channel < 16; channel++, port++) {
1655 port->magic = ISICOM_MAGIC;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001656 port->card = &isi_card[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 port->channel = channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001658 port->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* . . . */
Alan Cox251b8dd2008-04-30 00:54:12 -07001660 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001661 isi_card[idx].base = 0;
1662 isi_card[idx].irq = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001663 }
1664
Jiri Slaby09a4a112006-12-08 02:38:49 -08001665 /* tty driver structure initialization */
1666 isicom_normal = alloc_tty_driver(PORT_COUNT);
1667 if (!isicom_normal) {
1668 retval = -ENOMEM;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001669 goto error;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001670 }
1671
Jiri Slaby09a4a112006-12-08 02:38:49 -08001672 isicom_normal->name = "ttyM";
1673 isicom_normal->major = ISICOM_NMAJOR;
1674 isicom_normal->minor_start = 0;
1675 isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
1676 isicom_normal->subtype = SERIAL_TYPE_NORMAL;
1677 isicom_normal->init_termios = tty_std_termios;
1678 isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1679 CLOCAL;
Jiri Slaby938a7022006-12-08 02:38:52 -08001680 isicom_normal->flags = TTY_DRIVER_REAL_RAW |
Alan Cox6d889722008-07-22 11:18:21 +01001681 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001682 tty_set_operations(isicom_normal, &isicom_ops);
1683
1684 retval = tty_register_driver(isicom_normal);
1685 if (retval) {
Joe Perchesdb91340b2009-12-09 12:34:16 -08001686 pr_debug("Couldn't register the dialin driver\n");
Jiri Slaby09a4a112006-12-08 02:38:49 -08001687 goto err_puttty;
1688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Jiri Slaby9ac09482006-01-09 20:54:24 -08001690 retval = pci_register_driver(&isicom_driver);
1691 if (retval < 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -08001692 pr_err("Unable to register pci driver.\n");
Jiri Slaby09a4a112006-12-08 02:38:49 -08001693 goto err_unrtty;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jiri Slaby34b55b82006-12-08 02:38:55 -08001696 mod_timer(&tx, jiffies + 1);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001699err_unrtty:
1700 tty_unregister_driver(isicom_normal);
1701err_puttty:
1702 put_tty_driver(isicom_normal);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001703error:
1704 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707static void __exit isicom_exit(void)
1708{
Jiri Slabye3273252007-07-17 04:05:18 -07001709 del_timer_sync(&tx);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001710
Jiri Slaby9ac09482006-01-09 20:54:24 -08001711 pci_unregister_driver(&isicom_driver);
Jiri Slaby09a4a112006-12-08 02:38:49 -08001712 tty_unregister_driver(isicom_normal);
1713 put_tty_driver(isicom_normal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Jiri Slabyca262002006-12-08 02:38:50 -08001716module_init(isicom_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717module_exit(isicom_exit);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001718
1719MODULE_AUTHOR("MultiTech");
1720MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1721MODULE_LICENSE("GPL");
Ben Hutchingse6c4ef92010-01-13 23:34:18 +00001722MODULE_FIRMWARE("isi608.bin");
1723MODULE_FIRMWARE("isi608em.bin");
1724MODULE_FIRMWARE("isi616em.bin");
1725MODULE_FIRMWARE("isi4608.bin");
1726MODULE_FIRMWARE("isi4616.bin");