blob: e04a43e89f6b99fbee6f275bc0fdabf3e293360a [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;
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800222
223 while (!(inw(base + 0xe) & 0x1) && count++ < 100)
Jia-Ju Baife8e7ac2017-12-12 21:24:56 +0800224 mdelay(1);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800225
226 return !(inw(base + 0xe) & 0x1);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int lock_card(struct isi_board *card)
230{
Jiri Slaby8070e352006-01-09 20:54:22 -0800231 unsigned long base = card->base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700232 unsigned int retries, a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700234 for (retries = 0; retries < 10; retries++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_lock_irqsave(&card->card_lock, card->flags);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700236 for (a = 0; a < 10; a++) {
237 if (inw(base + 0xe) & 0x1)
238 return 1;
239 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700241 spin_unlock_irqrestore(&card->card_lock, card->flags);
242 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Joe Perchese620e542014-11-09 22:46:35 -0800244 pr_warn("Failed to lock Card (0x%lx)\n", card->base);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800245
Adrian Bunk04187262006-06-30 18:23:04 +0200246 return 0; /* Failed to acquire the card! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static void unlock_card(struct isi_board *card)
250{
251 spin_unlock_irqrestore(&card->card_lock, card->flags);
252}
253
254/*
255 * ISI Card specific ops ...
256 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800257
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800258/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800259static void raise_dtr(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800261 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800262 unsigned long base = card->base;
263 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800265 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return;
267
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800268 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 outw(0x0504, base);
270 InterruptTheCard(base);
271 port->status |= ISI_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800274/* card->lock HAS to be held */
Denys Vlasenko9428d712015-10-27 18:46:34 +0100275static void drop_dtr(struct isi_port *port)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800276{
277 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800278 unsigned long base = card->base;
279 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800281 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800284 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 outw(0x0404, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800286 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 port->status &= ~ISI_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800290/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800291static inline void raise_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800293 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800294 unsigned long base = card->base;
295 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800297 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return;
299
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800300 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 outw(0x0a04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800302 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 port->status |= ISI_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800305
306/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800307static inline void drop_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800309 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800310 unsigned long base = card->base;
311 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800313 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return;
315
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800316 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 outw(0x0804, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800318 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 port->status &= ~ISI_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800322/* card->lock MUST NOT be held */
Alan Cox5d951fb2009-01-02 13:45:19 +0000323
Alan Coxfcc8ac12009-06-11 12:24:17 +0100324static void isicom_dtr_rts(struct tty_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Alan Cox5d951fb2009-01-02 13:45:19 +0000326 struct isi_port *ip = container_of(port, struct isi_port, port);
327 struct isi_board *card = ip->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800328 unsigned long base = card->base;
Alan Cox5d951fb2009-01-02 13:45:19 +0000329 u16 channel = ip->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (!lock_card(card))
332 return;
333
Alan Coxfcc8ac12009-06-11 12:24:17 +0100334 if (on) {
335 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
336 outw(0x0f04, base);
337 InterruptTheCard(base);
338 ip->status |= (ISI_DTR | ISI_RTS);
339 } else {
340 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
341 outw(0x0C04, base);
342 InterruptTheCard(base);
343 ip->status &= ~(ISI_DTR | ISI_RTS);
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unlock_card(card);
346}
347
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800348/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800349static void drop_dtr_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800351 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800352 unsigned long base = card->base;
353 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800355 if (WaitTillCardIsFree(base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800358 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 outw(0x0c04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800360 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 port->status &= ~(ISI_RTS | ISI_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
365 * ISICOM Driver specific routines ...
366 *
367 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800368
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800369static inline int __isicom_paranoia_check(struct isi_port const *port,
370 char *name, const char *routine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!port) {
Joe Perchese620e542014-11-09 22:46:35 -0800373 pr_warn("Warning: bad isicom magic for dev %s in %s\n",
374 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 1;
376 }
377 if (port->magic != ISICOM_MAGIC) {
Joe Perchese620e542014-11-09 22:46:35 -0800378 pr_warn("Warning: NULL isicom port for dev %s in %s\n",
379 name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 1;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800381 }
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800387 * Transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
389 * We shovel data into the card buffers on a regular basis. The card
390 * will do the rest of the work for us.
391 */
392
Kees Cook24ed9602017-08-28 11:28:21 -0700393static void isicom_tx(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jiri Slaby4969b3a2007-07-17 04:05:18 -0700395 unsigned long flags, base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700396 unsigned int retries;
Jiri Slaby4969b3a2007-07-17 04:05:18 -0700397 short count = (BOARD_COUNT-1), card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 short txcount, wrd, residue, word_count, cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800399 struct isi_port *port;
400 struct tty_struct *tty;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* find next active board */
403 card = (prev_card + 1) & 0x0003;
Alan Cox251b8dd2008-04-30 00:54:12 -0700404 while (count-- > 0) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800405 if (isi_card[card].status & BOARD_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800407 card = (card + 1) & 0x0003;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 if (!(isi_card[card].status & BOARD_ACTIVE))
410 goto sched_again;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 prev_card = card;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 count = isi_card[card].port_count;
415 port = isi_card[card].ports;
416 base = isi_card[card].base;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700417
418 spin_lock_irqsave(&isi_card[card].card_lock, flags);
419 for (retries = 0; retries < 100; retries++) {
420 if (inw(base + 0xe) & 0x1)
421 break;
422 udelay(2);
423 }
424 if (retries >= 100)
425 goto unlock;
426
Alan Coxd450b5a2008-10-13 10:39:58 +0100427 tty = tty_port_tty_get(&port->port);
428 if (tty == NULL)
429 goto put_unlock;
430
Alan Cox251b8dd2008-04-30 00:54:12 -0700431 for (; count > 0; count--, port++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* port not active or tx disabled to force flow control */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700433 if (!tty_port_initialized(&port->port) ||
434 !(port->status & ISI_TXOK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 continue;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700438 if (txcount <= 0 || tty->stopped || tty->hw_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 continue;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700440
441 if (!(inw(base + 0x02) & (1 << port->channel)))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800442 continue;
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700443
Joe Perchesdb91340b2009-12-09 12:34:16 -0800444 pr_debug("txing %d bytes, port%d.\n",
445 txcount, port->channel + 1);
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800446 outw((port->channel << isi_card[card].shift_count) | txcount,
447 base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 residue = NO;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800449 wrd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 while (1) {
Jiri Slabya547dfe2006-01-09 20:54:26 -0800451 cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
452 - port->xmit_tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (residue == YES) {
454 residue = NO;
455 if (cnt > 0) {
Alan Coxf1d03222008-07-16 21:55:45 +0100456 wrd |= (port->port.xmit_buf[port->xmit_tail]
Jiri Slabya547dfe2006-01-09 20:54:26 -0800457 << 8);
458 port->xmit_tail = (port->xmit_tail + 1)
459 & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 port->xmit_cnt--;
461 txcount--;
462 cnt--;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800463 outw(wrd, base);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800464 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 outw(wrd, base);
466 break;
467 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800468 }
Alan Cox251b8dd2008-04-30 00:54:12 -0700469 if (cnt <= 0)
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 word_count = cnt >> 1;
Alan Coxf1d03222008-07-16 21:55:45 +0100472 outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800473 port->xmit_tail = (port->xmit_tail
474 + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 txcount -= (word_count << 1);
476 port->xmit_cnt -= (word_count << 1);
477 if (cnt & 0x0001) {
478 residue = YES;
Alan Coxf1d03222008-07-16 21:55:45 +0100479 wrd = port->port.xmit_buf[port->xmit_tail];
Jiri Slabya547dfe2006-01-09 20:54:26 -0800480 port->xmit_tail = (port->xmit_tail + 1)
481 & (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 port->xmit_cnt--;
483 txcount--;
484 }
485 }
486
487 InterruptTheCard(base);
488 if (port->xmit_cnt <= 0)
489 port->status &= ~ISI_TXOK;
490 if (port->xmit_cnt <= WAKEUP_CHARS)
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800491 tty_wakeup(tty);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Alan Coxd450b5a2008-10-13 10:39:58 +0100494put_unlock:
495 tty_kref_put(tty);
Jiri Slaby5b21f9d2007-07-17 04:05:17 -0700496unlock:
497 spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800498 /* schedule another tx for hopefully in about 10ms */
499sched_again:
Jiri Slaby34b55b82006-12-08 02:38:55 -0800500 mod_timer(&tx, jiffies + msecs_to_jiffies(10));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800504 * Main interrupt handler routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800506
David Howells7d12e782006-10-05 14:55:46 +0100507static irqreturn_t isicom_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jiri Slaby8070e352006-01-09 20:54:22 -0800509 struct isi_board *card = dev_id;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800510 struct isi_port *port;
511 struct tty_struct *tty;
Jiri Slaby8070e352006-01-09 20:54:22 -0800512 unsigned long base;
513 u16 header, word_count, count, channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 short byte_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800515 unsigned char *rp;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!card || !(card->status & FIRMWARE_LOADED))
518 return IRQ_NONE;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 base = card->base;
Jiri Slabycb4a10c2007-02-10 01:44:53 -0800521
522 /* did the card interrupt us? */
523 if (!(inw(base + 0x0e) & 0x02))
524 return IRQ_NONE;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 spin_lock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800527
Jiri Slaby18234f82006-12-08 02:38:51 -0800528 /*
529 * disable any interrupts from the PCI card and lower the
530 * interrupt line
531 */
532 outw(0x8000, base+0x04);
533 ClearInterrupt(base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 inw(base); /* get the dummy word out */
536 header = inw(base);
537 channel = (header & 0x7800) >> card->shift_count;
538 byte_count = header & 0xff;
539
540 if (channel + 1 > card->port_count) {
Joe Perchese620e542014-11-09 22:46:35 -0800541 pr_warn("%s(0x%lx): %d(channel) > port_count\n",
542 __func__, base, channel + 1);
Jiri Slaby18234f82006-12-08 02:38:51 -0800543 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_unlock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800545 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 port = card->ports + channel;
Peter Hurleyd41861c2016-04-09 17:53:25 -0700548 if (!tty_port_initialized(&port->port)) {
Jiri Slaby18234f82006-12-08 02:38:51 -0800549 outw(0x0000, base+0x04); /* enable interrupts */
Jiri Slaby174f1302007-02-10 01:44:52 -0800550 spin_unlock(&card->card_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800552 }
553
Alan Coxd450b5a2008-10-13 10:39:58 +0100554 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (tty == NULL) {
556 word_count = byte_count >> 1;
Alan Cox251b8dd2008-04-30 00:54:12 -0700557 while (byte_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 inw(base);
559 byte_count -= 2;
560 }
561 if (byte_count & 0x01)
562 inw(base);
Jiri Slaby18234f82006-12-08 02:38:51 -0800563 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_unlock(&card->card_lock);
565 return IRQ_HANDLED;
566 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (header & 0x8000) { /* Status Packet */
569 header = inw(base);
Alan Cox251b8dd2008-04-30 00:54:12 -0700570 switch (header & 0xff) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800571 case 0: /* Change in EIA signals */
Peter Hurley2d686552016-04-09 17:53:23 -0700572 if (tty_port_check_carrier(&port->port)) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800573 if (port->status & ISI_DCD) {
574 if (!(header & ISI_DCD)) {
575 /* Carrier has been lost */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800576 pr_debug("%s: DCD->low.\n",
577 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 port->status &= ~ISI_DCD;
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800579 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800581 } else if (header & ISI_DCD) {
582 /* Carrier has been detected */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800583 pr_debug("%s: DCD->high.\n",
584 __func__);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800585 port->status |= ISI_DCD;
Alan Coxf1d03222008-07-16 21:55:45 +0100586 wake_up_interruptible(&port->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800588 } else {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800589 if (header & ISI_DCD)
590 port->status |= ISI_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800592 port->status &= ~ISI_DCD;
593 }
594
Huang Shijief21ec3d2012-08-22 22:13:36 -0400595 if (tty_port_cts_enabled(&port->port)) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100596 if (tty->hw_stopped) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800597 if (header & ISI_CTS) {
Jiri Slaby7342c592012-11-15 09:49:48 +0100598 tty->hw_stopped = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800599 /* start tx ing */
Jiri Slabya547dfe2006-01-09 20:54:26 -0800600 port->status |= (ISI_TXOK
601 | ISI_CTS);
Jiri Slaby0aa5de82007-02-10 01:44:47 -0800602 tty_wakeup(tty);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800603 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800604 } else if (!(header & ISI_CTS)) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100605 tty->hw_stopped = 1;
Jiri Slabya547dfe2006-01-09 20:54:26 -0800606 /* stop tx ing */
607 port->status &= ~(ISI_TXOK | ISI_CTS);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800608 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800609 } else {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800610 if (header & ISI_CTS)
611 port->status |= ISI_CTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800613 port->status &= ~ISI_CTS;
614 }
615
616 if (header & ISI_DSR)
617 port->status |= ISI_DSR;
618 else
619 port->status &= ~ISI_DSR;
620
621 if (header & ISI_RI)
622 port->status |= ISI_RI;
623 else
624 port->status &= ~ISI_RI;
625
626 break;
627
Jiri Slabya547dfe2006-01-09 20:54:26 -0800628 case 1: /* Received Break !!! */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100629 tty_insert_flip_char(&port->port, 0, TTY_BREAK);
Alan Coxf1d03222008-07-16 21:55:45 +0100630 if (port->port.flags & ASYNC_SAK)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800631 do_SAK(tty);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100632 tty_flip_buffer_push(&port->port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800633 break;
634
635 case 2: /* Statistics */
Joe Perchesdb91340b2009-12-09 12:34:16 -0800636 pr_debug("%s: stats!!!\n", __func__);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800637 break;
638
639 default:
Joe Perchesdb91340b2009-12-09 12:34:16 -0800640 pr_debug("%s: Unknown code in status packet.\n",
641 __func__);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800642 break;
643 }
Jiri Slabya547dfe2006-01-09 20:54:26 -0800644 } else { /* Data Packet */
Jiri Slaby2f693352013-01-03 15:53:02 +0100645 count = tty_prepare_flip_string(&port->port, &rp,
646 byte_count & ~1);
Joe Perchesdb91340b2009-12-09 12:34:16 -0800647 pr_debug("%s: Can rx %d of %d bytes.\n",
648 __func__, count, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 word_count = count >> 1;
Alan Cox33f0f882006-01-09 20:54:13 -0800650 insw(base, rp, word_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 byte_count -= (word_count << 1);
652 if (count & 0x0001) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100653 tty_insert_flip_char(&port->port, inw(base) & 0xff,
Jiri Slabya547dfe2006-01-09 20:54:26 -0800654 TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 byte_count -= 2;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (byte_count > 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -0800658 pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
659 __func__, base, channel + 1);
Alan Cox251b8dd2008-04-30 00:54:12 -0700660 /* drain out unread xtra data */
661 while (byte_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 inw(base);
663 byte_count -= 2;
664 }
665 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100666 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Jiri Slaby18234f82006-12-08 02:38:51 -0800668 outw(0x0000, base+0x04); /* enable interrupts */
Jiri Slaby174f1302007-02-10 01:44:52 -0800669 spin_unlock(&card->card_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +0100670 tty_kref_put(tty);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alan Coxd450b5a2008-10-13 10:39:58 +0100675static void isicom_config_port(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Alan Coxd450b5a2008-10-13 10:39:58 +0100677 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800678 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long baud;
Jiri Slaby8070e352006-01-09 20:54:22 -0800680 unsigned long base = card->base;
681 u16 channel_setup, channel = port->channel,
682 shift_count = card->shift_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned char flow_ctrl;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800684
Alan Cox251b8dd2008-04-30 00:54:12 -0700685 /* FIXME: Switch to new tty baud API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 baud = C_BAUD(tty);
687 if (baud & CBAUDEX) {
688 baud &= ~CBAUDEX;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
691 * then the card is programmed for 57.6Kbps or 115Kbps
692 * respectively.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800693 */
694
Jiri Slaby7edc1362007-02-10 01:44:54 -0800695 /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
696 if (baud < 1 || baud > 4)
Alan Coxadc8d742012-07-14 15:31:47 +0100697 tty->termios.c_cflag &= ~CBAUDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 else
699 baud += 15;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (baud == 15) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800702
703 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * by the set_serial_info ioctl ... this is done by
705 * the 'setserial' utility.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800706 */
707
Alan Coxf1d03222008-07-16 21:55:45 +0100708 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800709 baud++; /* 57.6 Kbps */
Alan Coxf1d03222008-07-16 21:55:45 +0100710 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Alan Cox251b8dd2008-04-30 00:54:12 -0700711 baud += 2; /* 115 Kbps */
Alan Coxf1d03222008-07-16 21:55:45 +0100712 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby7edc1362007-02-10 01:44:54 -0800713 baud += 3; /* 230 kbps*/
Alan Coxf1d03222008-07-16 21:55:45 +0100714 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby7edc1362007-02-10 01:44:54 -0800715 baud += 4; /* 460 kbps*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 if (linuxb_to_isib[baud] == -1) {
718 /* hang up */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800719 drop_dtr(port);
720 return;
Alan Cox251b8dd2008-04-30 00:54:12 -0700721 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 raise_dtr(port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800723
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800724 if (WaitTillCardIsFree(base) == 0) {
Alan Cox251b8dd2008-04-30 00:54:12 -0700725 outw(0x8000 | (channel << shift_count) | 0x03, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
727 channel_setup = 0;
Alan Cox251b8dd2008-04-30 00:54:12 -0700728 switch (C_CSIZE(tty)) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800729 case CS5:
730 channel_setup |= ISICOM_CS5;
731 break;
732 case CS6:
733 channel_setup |= ISICOM_CS6;
734 break;
735 case CS7:
736 channel_setup |= ISICOM_CS7;
737 break;
738 case CS8:
739 channel_setup |= ISICOM_CS8;
740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (C_CSTOPB(tty))
744 channel_setup |= ISICOM_2SB;
745 if (C_PARENB(tty)) {
746 channel_setup |= ISICOM_EVPAR;
747 if (C_PARODD(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800748 channel_setup |= ISICOM_ODPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800750 outw(channel_setup, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 InterruptTheCard(base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800752 }
Peter Hurley2d686552016-04-09 17:53:23 -0700753 tty_port_set_check_carrier(&port->port, !C_CLOCAL(tty));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* flow control settings ...*/
756 flow_ctrl = 0;
Peter Hurley5604a982016-04-09 17:53:21 -0700757 tty_port_set_cts_flow(&port->port, C_CRTSCTS(tty));
758 if (C_CRTSCTS(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 flow_ctrl |= ISICOM_CTSRTS;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800760 if (I_IXON(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
762 if (I_IXOFF(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800763 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
764
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800765 if (WaitTillCardIsFree(base) == 0) {
Alan Cox251b8dd2008-04-30 00:54:12 -0700766 outw(0x8000 | (channel << shift_count) | 0x04, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 outw(flow_ctrl << 8 | 0x05, base);
768 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
769 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* rx enabled -> enable port for rx on the card */
773 if (C_CREAD(tty)) {
774 card->port_status |= (1 << channel);
775 outw(card->port_status, base + 0x02);
776 }
777}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800779/* open et all */
780
781static inline void isicom_setup_board(struct isi_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 int channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800784 struct isi_port *port;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800785
Alan Coxbaaa08a2009-11-30 13:17:24 +0000786 bp->count++;
Alan Cox6ed847d2009-11-30 13:17:30 +0000787 if (!(bp->status & BOARD_INIT)) {
788 port = bp->ports;
789 for (channel = 0; channel < bp->port_count; channel++, port++)
790 drop_dtr_rts(port);
791 }
792 bp->status |= BOARD_ACTIVE | BOARD_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800794
Alan Cox6ed847d2009-11-30 13:17:30 +0000795/* Activate and thus setup board are protected from races against shutdown
796 by the tty_port mutex */
797
Alan Coxbaaa08a2009-11-30 13:17:24 +0000798static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Alan Coxbaaa08a2009-11-30 13:17:24 +0000800 struct isi_port *port = container_of(tport, struct isi_port, port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800801 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800803
Alan Coxbaaa08a2009-11-30 13:17:24 +0000804 if (tty_port_alloc_xmit_buf(tport) < 0)
Alan Coxf1d03222008-07-16 21:55:45 +0100805 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxbaaa08a2009-11-30 13:17:24 +0000808 isicom_setup_board(card);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* discard any residual data */
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800813 if (WaitTillCardIsFree(card->base) == 0) {
814 outw(0x8000 | (port->channel << card->shift_count) | 0x02,
815 card->base);
816 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
817 InterruptTheCard(card->base);
818 }
Alan Coxd450b5a2008-10-13 10:39:58 +0100819 isicom_config_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800821
822 return 0;
823}
824
Alan Cox31f35932009-01-02 13:45:05 +0000825static int isicom_carrier_raised(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Alan Cox31f35932009-01-02 13:45:05 +0000827 struct isi_port *ip = container_of(port, struct isi_port, port);
828 return (ip->status & ISI_DCD)?1 : 0;
829}
830
Alan Cox11d85d72009-09-19 13:13:27 -0700831static struct tty_port *isicom_find_port(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800833 struct isi_port *port;
834 struct isi_board *card;
Jiri Slaby17c4edf2006-12-08 02:38:57 -0800835 unsigned int board;
Alan Cox11d85d72009-09-19 13:13:27 -0700836 int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 board = BOARD(line);
839 card = &isi_card[board];
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!(card->status & FIRMWARE_LOADED))
Alan Cox11d85d72009-09-19 13:13:27 -0700842 return NULL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* open on a port greater than the port count for the card !!! */
845 if (line > ((board * 16) + card->port_count - 1))
Alan Cox11d85d72009-09-19 13:13:27 -0700846 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800848 port = &isi_ports[line];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (isicom_paranoia_check(port, tty->name, "isicom_open"))
Alan Cox11d85d72009-09-19 13:13:27 -0700850 return NULL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800851
Alan Cox11d85d72009-09-19 13:13:27 -0700852 return &port->port;
853}
Alan Coxbaaa08a2009-11-30 13:17:24 +0000854
Alan Cox11d85d72009-09-19 13:13:27 -0700855static int isicom_open(struct tty_struct *tty, struct file *filp)
856{
857 struct isi_port *port;
Alan Cox11d85d72009-09-19 13:13:27 -0700858 struct tty_port *tport;
Alan Cox11d85d72009-09-19 13:13:27 -0700859
860 tport = isicom_find_port(tty);
861 if (tport == NULL)
862 return -ENODEV;
863 port = container_of(tport, struct isi_port, port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800864
Alan Coxa2d1e352010-04-23 16:01:18 +0100865 tty->driver_data = port;
Alan Coxbaaa08a2009-11-30 13:17:24 +0000866 return tty_port_open(tport, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/* close et all */
870
Jiri Slabycfe7c09a2006-12-29 16:48:00 -0800871/* card->lock HAS to be held */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800872static void isicom_shutdown_port(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800874 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (--card->count < 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -0800877 pr_debug("%s: bad board(0x%lx) count %d.\n",
878 __func__, card->base, card->count);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800879 card->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Alan Coxbaaa08a2009-11-30 13:17:24 +0000881 /* last port was closed, shutdown that board too */
Alan Cox6ed847d2009-11-30 13:17:30 +0000882 if (!card->count)
883 card->status &= BOARD_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Alan Cox978e5952008-04-30 00:53:59 -0700886static void isicom_flush_buffer(struct tty_struct *tty)
887{
888 struct isi_port *port = tty->driver_data;
889 struct isi_board *card = port->card;
890 unsigned long flags;
891
892 if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
893 return;
894
895 spin_lock_irqsave(&card->card_lock, flags);
896 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
897 spin_unlock_irqrestore(&card->card_lock, flags);
898
899 tty_wakeup(tty);
900}
901
Alan Coxbaaa08a2009-11-30 13:17:24 +0000902static void isicom_shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Alan Cox6f6412b2009-09-19 13:13:21 -0700904 struct isi_port *ip = container_of(port, struct isi_port, port);
905 struct isi_board *card = ip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800907
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800908 /* indicate to the card that no more data can be received
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 on this port */
910 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxbaaa08a2009-11-30 13:17:24 +0000911 card->port_status &= ~(1 << ip->channel);
912 outw(card->port_status, card->base + 0x02);
Alan Coxa6614992009-01-02 13:46:50 +0000913 isicom_shutdown_port(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 spin_unlock_irqrestore(&card->card_lock, flags);
Alan Cox2493c0c2009-11-30 13:17:46 +0000915 tty_port_free_xmit_buf(port);
Alan Cox6f6412b2009-09-19 13:13:21 -0700916}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800917
Alan Cox6f6412b2009-09-19 13:13:21 -0700918static void isicom_close(struct tty_struct *tty, struct file *filp)
919{
920 struct isi_port *ip = tty->driver_data;
Alan Coxa2d1e352010-04-23 16:01:18 +0100921 struct tty_port *port;
922
923 if (ip == NULL)
924 return;
925
926 port = &ip->port;
Alan Cox6f6412b2009-09-19 13:13:21 -0700927 if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
928 return;
Alan Coxbaaa08a2009-11-30 13:17:24 +0000929 tty_port_close(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/* write et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800933static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
934 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jiri Slaby8070e352006-01-09 20:54:22 -0800936 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800937 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 unsigned long flags;
939 int cnt, total = 0;
940
941 if (isicom_paranoia_check(port, tty->name, "isicom_write"))
942 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_lock_irqsave(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800945
Alan Cox251b8dd2008-04-30 00:54:12 -0700946 while (1) {
Jiri Slabya547dfe2006-01-09 20:54:26 -0800947 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
948 - 1, SERIAL_XMIT_SIZE - port->xmit_head));
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800949 if (cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800951
Alan Coxf1d03222008-07-16 21:55:45 +0100952 memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
Jiri Slabya547dfe2006-01-09 20:54:26 -0800953 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
954 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 port->xmit_cnt += cnt;
956 buf += cnt;
957 count -= cnt;
958 total += cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
961 port->status |= ISI_TXOK;
962 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800963 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/* put_char et all */
Alan Coxf34d7a52008-04-30 00:54:13 -0700967static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Jiri Slaby8070e352006-01-09 20:54:22 -0800969 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800970 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
Alan Coxf34d7a52008-04-30 00:54:13 -0700974 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_lock_irqsave(&card->card_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700977 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
978 spin_unlock_irqrestore(&card->card_lock, flags);
979 return 0;
980 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800981
Alan Coxf1d03222008-07-16 21:55:45 +0100982 port->port.xmit_buf[port->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
984 port->xmit_cnt++;
985 spin_unlock_irqrestore(&card->card_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/* flush_chars et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800990static void isicom_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jiri Slaby8070e352006-01-09 20:54:22 -0800992 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
995 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800996
Jiri Slabya547dfe2006-01-09 20:54:26 -0800997 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
Alan Coxf1d03222008-07-16 21:55:45 +0100998 !port->port.xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* this tells the transmitter to consider this port for
1002 data output to the card ... that's the best we can do. */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001003 port->status |= ISI_TXOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/* write_room et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001007static int isicom_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Jiri Slaby8070e352006-01-09 20:54:22 -08001009 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int free;
1011
1012 if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1013 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1016 if (free < 0)
1017 free = 0;
1018 return free;
1019}
1020
1021/* chars_in_buffer et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001022static int isicom_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Jiri Slaby8070e352006-01-09 20:54:22 -08001024 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1026 return 0;
1027 return port->xmit_cnt;
1028}
1029
1030/* ioctl et all */
Alan Cox6d889722008-07-22 11:18:21 +01001031static int isicom_send_break(struct tty_struct *tty, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Alan Cox6d889722008-07-22 11:18:21 +01001033 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001034 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -08001035 unsigned long base = card->base;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001036
Alan Cox6d889722008-07-22 11:18:21 +01001037 if (length == -1)
1038 return -EOPNOTSUPP;
1039
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001040 if (!lock_card(card))
Alan Cox6d889722008-07-22 11:18:21 +01001041 return -EINVAL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1044 outw((length & 0xff) << 8 | 0x00, base);
Arnd Bergmannf3e2d562015-01-13 22:59:47 +01001045 outw((length & 0xff00u), base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 InterruptTheCard(base);
1047
1048 unlock_card(card);
Alan Cox6d889722008-07-22 11:18:21 +01001049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Alan Cox60b33c12011-02-14 16:26:14 +00001052static int isicom_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Jiri Slaby8070e352006-01-09 20:54:22 -08001054 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* just send the port status */
Jiri Slaby8070e352006-01-09 20:54:22 -08001056 u16 status = port->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1059 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1062 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1063 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1064 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1065 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1066 ((status & ISI_RI ) ? TIOCM_RI : 0);
1067}
1068
Alan Cox20b9d172011-02-14 16:26:50 +00001069static int isicom_tiocmset(struct tty_struct *tty,
1070 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jiri Slaby8070e352006-01-09 20:54:22 -08001072 struct isi_port *port = tty->driver_data;
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001073 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1076 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001077
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001078 spin_lock_irqsave(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (set & TIOCM_RTS)
1080 raise_rts(port);
1081 if (set & TIOCM_DTR)
1082 raise_dtr(port);
1083
1084 if (clear & TIOCM_RTS)
1085 drop_rts(port);
1086 if (clear & TIOCM_DTR)
1087 drop_dtr(port);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001088 spin_unlock_irqrestore(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alan Coxd450b5a2008-10-13 10:39:58 +01001093static int isicom_set_serial_info(struct tty_struct *tty,
Al Virob60f38c2018-09-11 22:00:37 -04001094 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Alan Coxd450b5a2008-10-13 10:39:58 +01001096 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int reconfig_port;
1098
Al Virob60f38c2018-09-11 22:00:37 -04001099 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1100 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001101
Alan Coxec82db12010-06-01 22:52:44 +02001102 mutex_lock(&port->port.mutex);
Alan Coxf1d03222008-07-16 21:55:45 +01001103 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
Al Virob60f38c2018-09-11 22:00:37 -04001104 (ss->flags & ASYNC_SPD_MASK));
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (!capable(CAP_SYS_ADMIN)) {
Al Virob60f38c2018-09-11 22:00:37 -04001107 if ((ss->close_delay != port->port.close_delay) ||
1108 (ss->closing_wait != port->port.closing_wait) ||
1109 ((ss->flags & ~ASYNC_USR_MASK) !=
Alan Coxf1d03222008-07-16 21:55:45 +01001110 (port->port.flags & ~ASYNC_USR_MASK))) {
Alan Coxec82db12010-06-01 22:52:44 +02001111 mutex_unlock(&port->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EPERM;
Alan Cox1eac4942008-04-30 00:53:19 -07001113 }
Alan Coxf1d03222008-07-16 21:55:45 +01001114 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
Al Virob60f38c2018-09-11 22:00:37 -04001115 (ss->flags & ASYNC_USR_MASK));
Alan Cox251b8dd2008-04-30 00:54:12 -07001116 } else {
Al Virob60f38c2018-09-11 22:00:37 -04001117 port->port.close_delay = ss->close_delay;
1118 port->port.closing_wait = ss->closing_wait;
Alan Coxf1d03222008-07-16 21:55:45 +01001119 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
Al Virob60f38c2018-09-11 22:00:37 -04001120 (ss->flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122 if (reconfig_port) {
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001123 unsigned long flags;
1124 spin_lock_irqsave(&port->card->card_lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001125 isicom_config_port(tty);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001126 spin_unlock_irqrestore(&port->card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Alan Coxec82db12010-06-01 22:52:44 +02001128 mutex_unlock(&port->port.mutex);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001129 return 0;
1130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Al Virob60f38c2018-09-11 22:00:37 -04001132static int isicom_get_serial_info(struct tty_struct *tty,
1133 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jiri Slaby8070e352006-01-09 20:54:22 -08001135 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1138 return -ENODEV;
1139
Al Virob60f38c2018-09-11 22:00:37 -04001140 mutex_lock(&port->port.mutex);
1141/* ss->type = ? */
1142 ss->line = port - isi_ports;
1143 ss->port = port->card->base;
1144 ss->irq = port->card->irq;
1145 ss->flags = port->port.flags;
1146/* ss->baud_base = ? */
1147 ss->close_delay = port->port.close_delay;
1148 ss->closing_wait = port->port.closing_wait;
1149 mutex_unlock(&port->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
1153/* set_termios et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001154static void isicom_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001155 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jiri Slaby8070e352006-01-09 20:54:22 -08001157 struct isi_port *port = tty->driver_data;
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001158 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1161 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001162
Alan Coxadc8d742012-07-14 15:31:47 +01001163 if (tty->termios.c_cflag == old_termios->c_cflag &&
1164 tty->termios.c_iflag == old_termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001166
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001167 spin_lock_irqsave(&port->card->card_lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001168 isicom_config_port(tty);
Jiri Slabycfe7c09a2006-12-29 16:48:00 -08001169 spin_unlock_irqrestore(&port->card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001170
Peter Hurley9db276f2016-01-10 20:36:15 -08001171 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 tty->hw_stopped = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001173 isicom_start(tty);
1174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/* throttle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001178static void isicom_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jiri Slaby8070e352006-01-09 20:54:22 -08001180 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001181 struct isi_board *card = port->card;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1184 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* tell the card that this port cannot handle any more data for now */
1187 card->port_status &= ~(1 << port->channel);
1188 outw(card->port_status, card->base + 0x02);
1189}
1190
1191/* unthrottle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001192static void isicom_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jiri Slaby8070e352006-01-09 20:54:22 -08001194 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001195 struct isi_board *card = port->card;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1198 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* tell the card that this port is ready to accept more data */
1201 card->port_status |= (1 << port->channel);
1202 outw(card->port_status, card->base + 0x02);
1203}
1204
1205/* stop et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001206static void isicom_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Jiri Slaby8070e352006-01-09 20:54:22 -08001208 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1211 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* this tells the transmitter not to consider this port for
1214 data output to the card. */
1215 port->status &= ~ISI_TXOK;
1216}
1217
1218/* start et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001219static void isicom_start(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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1224 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* this tells the transmitter to consider this port for
1227 data output to the card. */
1228 port->status |= ISI_TXOK;
1229}
1230
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001231static void isicom_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Jiri Slaby8070e352006-01-09 20:54:22 -08001233 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1236 return;
Alan Cox3e616962009-01-02 13:45:26 +00001237 tty_port_hangup(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jiri Slaby9ac09482006-01-09 20:54:24 -08001241/*
1242 * Driver init and deinit functions
1243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001245static const struct tty_operations isicom_ops = {
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001246 .open = isicom_open,
1247 .close = isicom_close,
1248 .write = isicom_write,
1249 .put_char = isicom_put_char,
1250 .flush_chars = isicom_flush_chars,
1251 .write_room = isicom_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .chars_in_buffer = isicom_chars_in_buffer,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001253 .set_termios = isicom_set_termios,
1254 .throttle = isicom_throttle,
1255 .unthrottle = isicom_unthrottle,
1256 .stop = isicom_stop,
1257 .start = isicom_start,
1258 .hangup = isicom_hangup,
1259 .flush_buffer = isicom_flush_buffer,
1260 .tiocmget = isicom_tiocmget,
1261 .tiocmset = isicom_tiocmset,
Alan Cox6d889722008-07-22 11:18:21 +01001262 .break_ctl = isicom_send_break,
Al Virob60f38c2018-09-11 22:00:37 -04001263 .get_serial = isicom_get_serial_info,
1264 .set_serial = isicom_set_serial_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265};
1266
Alan Cox31f35932009-01-02 13:45:05 +00001267static const struct tty_port_operations isicom_port_ops = {
1268 .carrier_raised = isicom_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001269 .dtr_rts = isicom_dtr_rts,
Alan Coxbaaa08a2009-11-30 13:17:24 +00001270 .activate = isicom_activate,
1271 .shutdown = isicom_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +00001272};
1273
Bill Pemberton9671f092012-11-19 13:21:50 -05001274static int reset_card(struct pci_dev *pdev,
Jiri Slaby9ac09482006-01-09 20:54:24 -08001275 const unsigned int card, unsigned int *signature)
1276{
1277 struct isi_board *board = pci_get_drvdata(pdev);
1278 unsigned long base = board->base;
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001279 unsigned int sig, portcount = 0;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001280 int retval = 0;
1281
1282 dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
1283 base);
1284
1285 inw(base + 0x8);
1286
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001287 msleep(10);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001288
1289 outw(0, base + 0x8); /* Reset */
1290
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001291 msleep(1000);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001292
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001293 sig = inw(base + 0x4) & 0xff;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001294
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001295 if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
1296 sig != 0xee) {
1297 dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
1298 "bad I/O Port Address 0x%lx).\n", card + 1, base);
1299 dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
Jiri Slaby18234f82006-12-08 02:38:51 -08001300 retval = -EIO;
1301 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001304 msleep(10);
1305
1306 portcount = inw(base + 0x2);
Julia Lawall07fb6f22008-03-04 14:29:17 -08001307 if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001308 portcount != 8 && portcount != 16)) {
Joe Perches898eb712007-10-18 03:06:30 -07001309 dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001310 card + 1);
1311 retval = -EIO;
1312 goto end;
1313 }
1314
1315 switch (sig) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001316 case 0xa5:
1317 case 0xbb:
1318 case 0xdd:
Jiri Slaby18234f82006-12-08 02:38:51 -08001319 board->port_count = (portcount == 4) ? 4 : 8;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001320 board->shift_count = 12;
1321 break;
1322 case 0xcc:
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001323 case 0xee:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001324 board->port_count = 16;
1325 board->shift_count = 11;
1326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001328 dev_info(&pdev->dev, "-Done\n");
Jiri Slabyf0a0ba62007-02-10 01:44:52 -08001329 *signature = sig;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001330
1331end:
1332 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Bill Pemberton9671f092012-11-19 13:21:50 -05001335static int load_firmware(struct pci_dev *pdev,
Jiri Slabye65c1db2006-01-09 20:54:25 -08001336 const unsigned int index, const unsigned int signature)
1337{
1338 struct isi_board *board = pci_get_drvdata(pdev);
1339 const struct firmware *fw;
1340 unsigned long base = board->base;
1341 unsigned int a;
1342 u16 word_count, status;
1343 int retval = -EIO;
1344 char *name;
1345 u8 *data;
1346
1347 struct stframe {
1348 u16 addr;
1349 u16 count;
1350 u8 data[0];
1351 } *frame;
1352
1353 switch (signature) {
1354 case 0xa5:
1355 name = "isi608.bin";
1356 break;
1357 case 0xbb:
1358 name = "isi608em.bin";
1359 break;
1360 case 0xcc:
1361 name = "isi616em.bin";
1362 break;
1363 case 0xdd:
1364 name = "isi4608.bin";
1365 break;
1366 case 0xee:
1367 name = "isi4616.bin";
1368 break;
1369 default:
1370 dev_err(&pdev->dev, "Unknown signature.\n");
1371 goto end;
Alan Cox251b8dd2008-04-30 00:54:12 -07001372 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001373
1374 retval = request_firmware(&fw, name, &pdev->dev);
1375 if (retval)
1376 goto end;
1377
Jiri Slabye4e04082006-09-30 23:27:50 -07001378 retval = -EIO;
1379
Jiri Slabye65c1db2006-01-09 20:54:25 -08001380 for (frame = (struct stframe *)fw->data;
1381 frame < (struct stframe *)(fw->data + fw->size);
Jiri Slabye4e04082006-09-30 23:27:50 -07001382 frame = (struct stframe *)((u8 *)(frame + 1) +
1383 frame->count)) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001384 if (WaitTillCardIsFree(base))
1385 goto errrelfw;
1386
1387 outw(0xf0, base); /* start upload sequence */
1388 outw(0x00, base);
1389 outw(frame->addr, base); /* lsb of address */
1390
1391 word_count = frame->count / 2 + frame->count % 2;
1392 outw(word_count, base);
1393 InterruptTheCard(base);
1394
1395 udelay(100); /* 0x2f */
1396
1397 if (WaitTillCardIsFree(base))
1398 goto errrelfw;
1399
Alan Cox251b8dd2008-04-30 00:54:12 -07001400 status = inw(base + 0x4);
1401 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001402 dev_warn(&pdev->dev, "Card%d rejected load header:\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001403 "Address:0x%x\n"
1404 "Count:0x%x\n"
1405 "Status:0x%x\n",
1406 index + 1, frame->addr, frame->count, status);
Jiri Slabye65c1db2006-01-09 20:54:25 -08001407 goto errrelfw;
1408 }
1409 outsw(base, frame->data, word_count);
1410
1411 InterruptTheCard(base);
1412
1413 udelay(50); /* 0x0f */
1414
1415 if (WaitTillCardIsFree(base))
1416 goto errrelfw;
1417
Alan Cox251b8dd2008-04-30 00:54:12 -07001418 status = inw(base + 0x4);
1419 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001420 dev_err(&pdev->dev, "Card%d got out of sync.Card "
1421 "Status:0x%x\n", index + 1, status);
1422 goto errrelfw;
1423 }
Alan Cox251b8dd2008-04-30 00:54:12 -07001424 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001425
Jiri Slabye65c1db2006-01-09 20:54:25 -08001426/* XXX: should we test it by reading it back and comparing with original like
1427 * in load firmware package? */
Jiri Slabye4e04082006-09-30 23:27:50 -07001428 for (frame = (struct stframe *)fw->data;
1429 frame < (struct stframe *)(fw->data + fw->size);
1430 frame = (struct stframe *)((u8 *)(frame + 1) +
1431 frame->count)) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001432 if (WaitTillCardIsFree(base))
1433 goto errrelfw;
1434
1435 outw(0xf1, base); /* start download sequence */
1436 outw(0x00, base);
1437 outw(frame->addr, base); /* lsb of address */
1438
1439 word_count = (frame->count >> 1) + frame->count % 2;
1440 outw(word_count + 1, base);
1441 InterruptTheCard(base);
1442
1443 udelay(50); /* 0xf */
1444
1445 if (WaitTillCardIsFree(base))
1446 goto errrelfw;
1447
Alan Cox251b8dd2008-04-30 00:54:12 -07001448 status = inw(base + 0x4);
1449 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001450 dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001451 "Address:0x%x\n"
1452 "Count:0x%x\n"
1453 "Status: 0x%x\n",
1454 index + 1, frame->addr, frame->count, status);
Jiri Slabye65c1db2006-01-09 20:54:25 -08001455 goto errrelfw;
1456 }
1457
Kees Cook6da2ec52018-06-12 13:55:00 -07001458 data = kmalloc_array(word_count, 2, GFP_KERNEL);
Jiri Slabyf0671372006-12-08 02:38:54 -08001459 if (data == NULL) {
1460 dev_err(&pdev->dev, "Card%d, firmware upload "
1461 "failed, not enough memory\n", index + 1);
1462 goto errrelfw;
1463 }
Jiri Slabye65c1db2006-01-09 20:54:25 -08001464 inw(base);
1465 insw(base, data, word_count);
1466 InterruptTheCard(base);
1467
1468 for (a = 0; a < frame->count; a++)
1469 if (data[a] != frame->data[a]) {
1470 kfree(data);
1471 dev_err(&pdev->dev, "Card%d, firmware upload "
1472 "failed\n", index + 1);
1473 goto errrelfw;
1474 }
1475 kfree(data);
1476
1477 udelay(50); /* 0xf */
1478
1479 if (WaitTillCardIsFree(base))
1480 goto errrelfw;
1481
Alan Cox251b8dd2008-04-30 00:54:12 -07001482 status = inw(base + 0x4);
1483 if (status != 0) {
Jiri Slabye65c1db2006-01-09 20:54:25 -08001484 dev_err(&pdev->dev, "Card%d verify got out of sync. "
1485 "Card Status:0x%x\n", index + 1, status);
1486 goto errrelfw;
1487 }
1488 }
1489
Jiri Slabye4e04082006-09-30 23:27:50 -07001490 /* xfer ctrl */
1491 if (WaitTillCardIsFree(base))
1492 goto errrelfw;
1493
1494 outw(0xf2, base);
1495 outw(0x800, base);
1496 outw(0x0, base);
1497 outw(0x0, base);
1498 InterruptTheCard(base);
1499 outw(0x0, base + 0x4); /* for ISI4608 cards */
1500
Jiri Slabye65c1db2006-01-09 20:54:25 -08001501 board->status |= FIRMWARE_LOADED;
1502 retval = 0;
1503
1504errrelfw:
1505 release_firmware(fw);
1506end:
1507 return retval;
1508}
1509
Jiri Slaby9ac09482006-01-09 20:54:24 -08001510/*
1511 * Insmod can set static symbols so keep these static
1512 */
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001513static unsigned int card_count;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001514
Bill Pemberton9671f092012-11-19 13:21:50 -05001515static int isicom_probe(struct pci_dev *pdev,
Jiri Slaby9ac09482006-01-09 20:54:24 -08001516 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jiri Slaby9653a692009-06-17 16:27:48 -07001518 unsigned int uninitialized_var(signature), index;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001519 int retval = -EPERM;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001520 struct isi_board *board = NULL;
1521
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001522 if (card_count >= BOARD_COUNT)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001523 goto err;
1524
Jiri Slabye1e57702008-07-16 21:52:56 +01001525 retval = pci_enable_device(pdev);
1526 if (retval) {
1527 dev_err(&pdev->dev, "failed to enable\n");
1528 goto err;
1529 }
1530
Jiri Slaby9ac09482006-01-09 20:54:24 -08001531 dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
1532
1533 /* allot the first empty slot in the array */
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001534 for (index = 0; index < BOARD_COUNT; index++) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001535 if (isi_card[index].base == 0) {
1536 board = &isi_card[index];
1537 break;
1538 }
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001539 }
1540 if (index == BOARD_COUNT) {
1541 retval = -ENODEV;
1542 goto err_disable;
1543 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001544
Jiri Slaby938a7022006-12-08 02:38:52 -08001545 board->index = index;
Jiri Slaby4969b3a2007-07-17 04:05:18 -07001546 board->base = pci_resource_start(pdev, 3);
1547 board->irq = pdev->irq;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001548 card_count++;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001549
1550 pci_set_drvdata(pdev, board);
1551
Jiri Slaby78028da2006-12-08 02:38:53 -08001552 retval = pci_request_region(pdev, 3, ISICOM_NAME);
1553 if (retval) {
Jiri Slaby09a4a112006-12-08 02:38:49 -08001554 dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
1555 "will be disabled.\n", board->base, board->base + 15,
1556 index + 1);
1557 retval = -EBUSY;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001558 goto errdec;
Alan Cox251b8dd2008-04-30 00:54:12 -07001559 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001560
Jiri Slaby09a4a112006-12-08 02:38:49 -08001561 retval = request_irq(board->irq, isicom_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001562 IRQF_SHARED, ISICOM_NAME, board);
Jiri Slaby09a4a112006-12-08 02:38:49 -08001563 if (retval < 0) {
1564 dev_err(&pdev->dev, "Could not install handler at Irq %d. "
1565 "Card%d will be disabled.\n", board->irq, index + 1);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001566 goto errunrr;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001567 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001568
1569 retval = reset_card(pdev, index, &signature);
1570 if (retval < 0)
1571 goto errunri;
1572
Jiri Slabye65c1db2006-01-09 20:54:25 -08001573 retval = load_firmware(pdev, index, signature);
1574 if (retval < 0)
1575 goto errunri;
1576
Jiri Slabyd0f59142012-11-15 09:49:55 +01001577 for (index = 0; index < board->port_count; index++) {
1578 struct tty_port *tport = &board->ports[index].port;
1579 tty_port_init(tport);
1580 tport->ops = &isicom_port_ops;
1581 tport->close_delay = 50 * HZ/100;
1582 tport->closing_wait = 3000 * HZ/100;
1583 tty_port_register_device(tport, isicom_normal,
1584 board->index * 16 + index, &pdev->dev);
1585 }
Jiri Slaby938a7022006-12-08 02:38:52 -08001586
Jiri Slaby9ac09482006-01-09 20:54:24 -08001587 return 0;
1588
1589errunri:
1590 free_irq(board->irq, board);
1591errunrr:
Jiri Slaby78028da2006-12-08 02:38:53 -08001592 pci_release_region(pdev, 3);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001593errdec:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001594 board->base = 0;
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001595 card_count--;
Dan Carpenter26e1e8d2010-05-07 10:30:41 +02001596err_disable:
Jiri Slabye1e57702008-07-16 21:52:56 +01001597 pci_disable_device(pdev);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001598err:
Jiri Slaby9ac09482006-01-09 20:54:24 -08001599 return retval;
1600}
1601
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001602static void isicom_remove(struct pci_dev *pdev)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001603{
1604 struct isi_board *board = pci_get_drvdata(pdev);
Jiri Slaby938a7022006-12-08 02:38:52 -08001605 unsigned int i;
1606
Jiri Slabyd0f59142012-11-15 09:49:55 +01001607 for (i = 0; i < board->port_count; i++) {
Jiri Slaby938a7022006-12-08 02:38:52 -08001608 tty_unregister_device(isicom_normal, board->index * 16 + i);
Jiri Slabyd0f59142012-11-15 09:49:55 +01001609 tty_port_destroy(&board->ports[i].port);
1610 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001611
1612 free_irq(board->irq, board);
Jiri Slaby78028da2006-12-08 02:38:53 -08001613 pci_release_region(pdev, 3);
Jiri Slaby1ed0c0b2007-02-10 01:44:55 -08001614 board->base = 0;
1615 card_count--;
Jiri Slabye1e57702008-07-16 21:52:56 +01001616 pci_disable_device(pdev);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001617}
1618
Jiri Slabyca262002006-12-08 02:38:50 -08001619static int __init isicom_init(void)
Jiri Slaby9ac09482006-01-09 20:54:24 -08001620{
1621 int retval, idx, channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001622 struct isi_port *port;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001623
Alan Cox251b8dd2008-04-30 00:54:12 -07001624 for (idx = 0; idx < BOARD_COUNT; idx++) {
Jiri Slaby9ac09482006-01-09 20:54:24 -08001625 port = &isi_ports[idx * 16];
1626 isi_card[idx].ports = port;
1627 spin_lock_init(&isi_card[idx].card_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 for (channel = 0; channel < 16; channel++, port++) {
1629 port->magic = ISICOM_MAGIC;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001630 port->card = &isi_card[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 port->channel = channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001632 port->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* . . . */
Alan Cox251b8dd2008-04-30 00:54:12 -07001634 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001635 isi_card[idx].base = 0;
1636 isi_card[idx].irq = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001637 }
1638
Jiri Slaby09a4a112006-12-08 02:38:49 -08001639 /* tty driver structure initialization */
1640 isicom_normal = alloc_tty_driver(PORT_COUNT);
1641 if (!isicom_normal) {
1642 retval = -ENOMEM;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001643 goto error;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001644 }
1645
Jiri Slaby09a4a112006-12-08 02:38:49 -08001646 isicom_normal->name = "ttyM";
1647 isicom_normal->major = ISICOM_NMAJOR;
1648 isicom_normal->minor_start = 0;
1649 isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
1650 isicom_normal->subtype = SERIAL_TYPE_NORMAL;
1651 isicom_normal->init_termios = tty_std_termios;
1652 isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1653 CLOCAL;
Jiri Slaby938a7022006-12-08 02:38:52 -08001654 isicom_normal->flags = TTY_DRIVER_REAL_RAW |
Alan Cox6d889722008-07-22 11:18:21 +01001655 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001656 tty_set_operations(isicom_normal, &isicom_ops);
1657
1658 retval = tty_register_driver(isicom_normal);
1659 if (retval) {
Joe Perchesdb91340b2009-12-09 12:34:16 -08001660 pr_debug("Couldn't register the dialin driver\n");
Jiri Slaby09a4a112006-12-08 02:38:49 -08001661 goto err_puttty;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jiri Slaby9ac09482006-01-09 20:54:24 -08001664 retval = pci_register_driver(&isicom_driver);
1665 if (retval < 0) {
Joe Perchesdb91340b2009-12-09 12:34:16 -08001666 pr_err("Unable to register pci driver.\n");
Jiri Slaby09a4a112006-12-08 02:38:49 -08001667 goto err_unrtty;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jiri Slaby34b55b82006-12-08 02:38:55 -08001670 mod_timer(&tx, jiffies + 1);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 0;
Jiri Slaby09a4a112006-12-08 02:38:49 -08001673err_unrtty:
1674 tty_unregister_driver(isicom_normal);
1675err_puttty:
1676 put_tty_driver(isicom_normal);
Jiri Slaby9ac09482006-01-09 20:54:24 -08001677error:
1678 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681static void __exit isicom_exit(void)
1682{
Jiri Slabye3273252007-07-17 04:05:18 -07001683 del_timer_sync(&tx);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001684
Jiri Slaby9ac09482006-01-09 20:54:24 -08001685 pci_unregister_driver(&isicom_driver);
Jiri Slaby09a4a112006-12-08 02:38:49 -08001686 tty_unregister_driver(isicom_normal);
1687 put_tty_driver(isicom_normal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Jiri Slabyca262002006-12-08 02:38:50 -08001690module_init(isicom_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691module_exit(isicom_exit);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001692
1693MODULE_AUTHOR("MultiTech");
1694MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1695MODULE_LICENSE("GPL");
Ben Hutchingse6c4ef92010-01-13 23:34:18 +00001696MODULE_FIRMWARE("isi608.bin");
1697MODULE_FIRMWARE("isi608em.bin");
1698MODULE_FIRMWARE("isi616em.bin");
1699MODULE_FIRMWARE("isi4608.bin");
1700MODULE_FIRMWARE("isi4616.bin");