blob: 55a47b33ff34b74a66a93041c3bfb66d00af53f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Original driver code supplied by Multi-Tech
8 *
9 * Changes
10 * 1/9/98 alan@redhat.com Merge to 2.0.x kernel tree
11 * Obtain and use official major/minors
12 * Loader switched to a misc device
13 * (fixed range check bug as a side effect)
14 * Printk clean up
15 * 9/12/98 alan@redhat.com Rough port to 2.1.x
16 *
17 * 10/6/99 sameer Merged the ISA and PCI drivers to
18 * a new unified driver.
19 *
20 * 3/9/99 sameer Added support for ISI4616 cards.
21 *
22 * 16/9/99 sameer We do not force RTS low anymore.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080023 * This is to prevent the firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * from getting confused.
25 *
26 * 26/10/99 sameer Cosmetic changes:The driver now
27 * dumps the Port Count information
28 * along with I/O address and IRQ.
29 *
30 * 13/12/99 sameer Fixed the problem with IRQ sharing.
31 *
32 * 10/5/00 sameer Fixed isicom_shutdown_board()
33 * to not lower DTR on all the ports
Jiri Slabyd8d16e42006-01-09 20:54:21 -080034 * when the last port on the card is
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * closed.
36 *
37 * 10/5/00 sameer Signal mask setup command added
Jiri Slabyd8d16e42006-01-09 20:54:21 -080038 * to isicom_setup_port and
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * isicom_shutdown_port.
40 *
41 * 24/5/00 sameer The driver is now SMP aware.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080042 *
43 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
Jiri Slabyd8d16e42006-01-09 20:54:21 -080045 *
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * 03/01/01 anil .s Added support for resetting the
48 * internal modems on ISI cards.
49 *
50 * 08/02/01 anil .s Upgraded the driver for kernel
51 * 2.4.x
52 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080053 * 11/04/01 Kevin Fixed firmware load problem with
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * ISIHP-4X card
Jiri Slabyd8d16e42006-01-09 20:54:21 -080055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * 30/04/01 anil .s Fixed the remote login through
57 * ISI port problem. Now the link
58 * does not go down before password
59 * prompt.
60 *
61 * 03/05/01 anil .s Fixed the problem with IRQ sharing
62 * among ISI-PCI cards.
63 *
64 * 03/05/01 anil .s Added support to display the version
Jiri Slabyd8d16e42006-01-09 20:54:21 -080065 * info during insmod as well as module
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * listing by lsmod.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * 10/05/01 anil .s Done the modifications to the source
69 * file and Install script so that the
70 * same installation can be used for
71 * 2.2.x and 2.4.x kernel.
72 *
73 * 06/06/01 anil .s Now we drop both dtr and rts during
74 * shutdown_port as well as raise them
75 * during isicom_config_port.
Jiri Slabyd8d16e42006-01-09 20:54:21 -080076 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * 09/06/01 acme@conectiva.com.br use capable, not suser, do
78 * restore_flags on failure in
79 * isicom_send_break, verify put_user
80 * result
81 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080082 * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
83 * Baud index extended to 21
84 *
85 * 20/03/03 ranjeeth Made to work for Linux Advanced server.
86 * Taken care of license warning.
87 *
88 * 10/12/03 Ravindra Made to work for Fedora Core 1 of
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Red Hat Distribution
90 *
91 * 06/01/05 Alan Cox Merged the ISI and base kernel strands
92 * into a single 2.6 driver
93 *
94 * ***********************************************************
95 *
Jiri Slabyd8d16e42006-01-09 20:54:21 -080096 * To use this driver you also need the support package. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * can find this in RPM format on
98 * ftp://ftp.linux.org.uk/pub/linux/alan
Jiri Slabyd8d16e42006-01-09 20:54:21 -080099 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * You can find the original tools for this direct from Multitech
101 * ftp://ftp.multitech.com/ISI-Cards/
102 *
103 * Having installed the cards the module options (/etc/modprobe.conf)
104 *
105 * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
106 *
107 * Omit those entries for boards you don't have installed.
108 *
109 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * Merge testing
111 * 64-bit verification
112 */
113
114#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>
128
129#include <asm/uaccess.h>
130#include <asm/io.h>
131#include <asm/system.h>
132
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
141#define pr_dbg(str...) printk(KERN_DEBUG "ISICOM: " str)
142#define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
143#else
144#define pr_dbg(str...) do { } while (0)
145#define isicom_paranoia_check(a, b, c) 0
146#endif
147
Jiri Slaby9ac09482006-01-09 20:54:24 -0800148static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
149static void __devexit isicom_remove(struct pci_dev *);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct pci_device_id isicom_pci_tbl[] = {
Jiri Slaby9ac09482006-01-09 20:54:24 -0800152 { PCI_DEVICE(VENDOR_ID, 0x2028) },
153 { PCI_DEVICE(VENDOR_ID, 0x2051) },
154 { PCI_DEVICE(VENDOR_ID, 0x2052) },
155 { PCI_DEVICE(VENDOR_ID, 0x2053) },
156 { PCI_DEVICE(VENDOR_ID, 0x2054) },
157 { PCI_DEVICE(VENDOR_ID, 0x2055) },
158 { PCI_DEVICE(VENDOR_ID, 0x2056) },
159 { PCI_DEVICE(VENDOR_ID, 0x2057) },
160 { PCI_DEVICE(VENDOR_ID, 0x2058) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 { 0 }
162};
163MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
164
Jiri Slaby9ac09482006-01-09 20:54:24 -0800165static struct pci_driver isicom_driver = {
166 .name = "isicom",
167 .id_table = isicom_pci_tbl,
168 .probe = isicom_probe,
169 .remove = __devexit_p(isicom_remove)
170};
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int prev_card = 3; /* start servicing isi_card[0] */
173static struct tty_driver *isicom_normal;
174
175static struct timer_list tx;
176static char re_schedule = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static void isicom_tx(unsigned long _data);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800179static void isicom_start(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* baud index mappings from linux defns to isi */
182
183static signed char linuxb_to_isib[] = {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800184 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187struct isi_board {
Jiri Slaby8070e352006-01-09 20:54:22 -0800188 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned char irq;
190 unsigned char port_count;
191 unsigned short status;
192 unsigned short port_status; /* each bit represents a single port */
193 unsigned short shift_count;
194 struct isi_port * ports;
195 signed char count;
196 unsigned char isa;
197 spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
198 unsigned long flags;
199};
200
201struct isi_port {
202 unsigned short magic;
203 unsigned int flags;
204 int count;
205 int blocked_open;
206 int close_delay;
Jiri Slaby8070e352006-01-09 20:54:22 -0800207 u16 channel;
208 u16 status;
209 u16 closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct isi_board * card;
211 struct tty_struct * tty;
212 wait_queue_head_t close_wait;
213 wait_queue_head_t open_wait;
214 struct work_struct hangup_tq;
215 struct work_struct bh_tqueue;
216 unsigned char * xmit_buf;
217 int xmit_head;
218 int xmit_tail;
219 int xmit_cnt;
220};
221
222static struct isi_board isi_card[BOARD_COUNT];
223static struct isi_port isi_ports[PORT_COUNT];
224
225/*
226 * Locking functions for card level locking. We need to own both
227 * the kernel lock for the card and have the card in a position that
228 * it wants to talk.
229 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int lock_card(struct isi_board *card)
232{
233 char retries;
Jiri Slaby8070e352006-01-09 20:54:22 -0800234 unsigned long base = card->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 for (retries = 0; retries < 100; retries++) {
237 spin_lock_irqsave(&card->card_lock, card->flags);
238 if (inw(base + 0xe) & 0x1) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800239 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 } else {
241 spin_unlock_irqrestore(&card->card_lock, card->flags);
242 udelay(1000); /* 1ms */
243 }
244 }
Jiri Slaby8070e352006-01-09 20:54:22 -0800245 printk(KERN_WARNING "ISICOM: Failed to lock Card (0x%lx)\n", card->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0; /* Failed to aquire the card! */
247}
248
249static int lock_card_at_interrupt(struct isi_board *card)
250{
251 unsigned char retries;
Jiri Slaby8070e352006-01-09 20:54:22 -0800252 unsigned long base = card->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 for (retries = 0; retries < 200; retries++) {
255 spin_lock_irqsave(&card->card_lock, card->flags);
256
257 if (inw(base + 0xe) & 0x1)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800258 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else
260 spin_unlock_irqrestore(&card->card_lock, card->flags);
261 }
262 /* Failing in interrupt is an acceptable event */
263 return 0; /* Failed to aquire the card! */
264}
265
266static void unlock_card(struct isi_board *card)
267{
268 spin_unlock_irqrestore(&card->card_lock, card->flags);
269}
270
271/*
272 * ISI Card specific ops ...
273 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800274
275static void raise_dtr(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800277 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
281 if (!lock_card(card))
282 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(0x0504, base);
286 InterruptTheCard(base);
287 port->status |= ISI_DTR;
288 unlock_card(card);
289}
290
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800291static inline void drop_dtr(struct isi_port *port)
292{
293 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
297 if (!lock_card(card))
298 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(0x0404, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800302 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 port->status &= ~ISI_DTR;
304 unlock_card(card);
305}
306
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800307static inline void raise_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
313 if (!lock_card(card))
314 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(0x0a04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800318 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 port->status |= ISI_RTS;
320 unlock_card(card);
321}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800322static inline void drop_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800324 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800325 unsigned long base = card->base;
326 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (!lock_card(card))
329 return;
330
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800331 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 outw(0x0804, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800333 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 port->status &= ~ISI_RTS;
335 unlock_card(card);
336}
337
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800338static inline void raise_dtr_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800340 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800341 unsigned long base = card->base;
342 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!lock_card(card))
345 return;
346
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800347 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 outw(0x0f04, base);
349 InterruptTheCard(base);
350 port->status |= (ISI_DTR | ISI_RTS);
351 unlock_card(card);
352}
353
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800354static void drop_dtr_rts(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800356 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800357 unsigned long base = card->base;
358 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!lock_card(card))
361 return;
362
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800363 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 outw(0x0c04, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800365 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 port->status &= ~(ISI_RTS | ISI_DTR);
367 unlock_card(card);
368}
369
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800370static inline void kill_queue(struct isi_port *port, short queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800372 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -0800373 unsigned long base = card->base;
374 u16 channel = port->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!lock_card(card))
377 return;
378
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800379 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 outw((queue << 8) | 0x06, base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800381 InterruptTheCard(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unlock_card(card);
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * ISICOM Driver specific routines ...
387 *
388 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800389
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800390static inline int __isicom_paranoia_check(struct isi_port const *port,
391 char *name, const char *routine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!port) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800394 printk(KERN_WARNING "ISICOM: Warning: bad isicom magic for "
395 "dev %s in %s.\n", name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 1;
397 }
398 if (port->magic != ISICOM_MAGIC) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800399 printk(KERN_WARNING "ISICOM: Warning: NULL isicom port for "
400 "dev %s in %s.\n", name, routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 1;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800402 }
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800408 * Transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
410 * We shovel data into the card buffers on a regular basis. The card
411 * will do the rest of the work for us.
412 */
413
414static void isicom_tx(unsigned long _data)
415{
416 short count = (BOARD_COUNT-1), card, base;
417 short txcount, wrd, residue, word_count, cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800418 struct isi_port *port;
419 struct tty_struct *tty;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* find next active board */
422 card = (prev_card + 1) & 0x0003;
423 while(count-- > 0) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800424 if (isi_card[card].status & BOARD_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800426 card = (card + 1) & 0x0003;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 if (!(isi_card[card].status & BOARD_ACTIVE))
429 goto sched_again;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 prev_card = card;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 count = isi_card[card].port_count;
434 port = isi_card[card].ports;
435 base = isi_card[card].base;
436 for (;count > 0;count--, port++) {
437 if (!lock_card_at_interrupt(&isi_card[card]))
438 continue;
439 /* port not active or tx disabled to force flow control */
440 if (!(port->flags & ASYNC_INITIALIZED) ||
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800441 !(port->status & ISI_TXOK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unlock_card(&isi_card[card]);
443 continue;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tty = port->tty;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800446
447
448 if (tty == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unlock_card(&isi_card[card]);
450 continue;
451 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
454 if (txcount <= 0 || tty->stopped || tty->hw_stopped) {
455 unlock_card(&isi_card[card]);
456 continue;
457 }
458 if (!(inw(base + 0x02) & (1 << port->channel))) {
459 unlock_card(&isi_card[card]);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800460 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800462 pr_dbg("txing %d bytes, port%d.\n", txcount,
463 port->channel + 1);
464 outw((port->channel << isi_card[card].shift_count) | txcount,
465 base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 residue = NO;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800467 wrd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 while (1) {
469 cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE - port->xmit_tail));
470 if (residue == YES) {
471 residue = NO;
472 if (cnt > 0) {
473 wrd |= (port->xmit_buf[port->xmit_tail] << 8);
474 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
475 port->xmit_cnt--;
476 txcount--;
477 cnt--;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800478 outw(wrd, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 else {
481 outw(wrd, base);
482 break;
483 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (cnt <= 0) break;
486 word_count = cnt >> 1;
487 outsw(base, port->xmit_buf+port->xmit_tail, word_count);
488 port->xmit_tail = (port->xmit_tail + (word_count << 1)) &
489 (SERIAL_XMIT_SIZE - 1);
490 txcount -= (word_count << 1);
491 port->xmit_cnt -= (word_count << 1);
492 if (cnt & 0x0001) {
493 residue = YES;
494 wrd = port->xmit_buf[port->xmit_tail];
495 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
496 port->xmit_cnt--;
497 txcount--;
498 }
499 }
500
501 InterruptTheCard(base);
502 if (port->xmit_cnt <= 0)
503 port->status &= ~ISI_TXOK;
504 if (port->xmit_cnt <= WAKEUP_CHARS)
505 schedule_work(&port->bh_tqueue);
506 unlock_card(&isi_card[card]);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800509 /* schedule another tx for hopefully in about 10ms */
510sched_again:
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800511 if (!re_schedule) {
512 re_schedule = 2;
513 return;
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 init_timer(&tx);
517 tx.expires = jiffies + HZ/100;
518 tx.data = 0;
519 tx.function = isicom_tx;
520 add_timer(&tx);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800521
522 return;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/* Interrupt handlers */
526
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800527
528static void isicom_bottomhalf(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800530 struct isi_port *port = (struct isi_port *) data;
531 struct tty_struct *tty = port->tty;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!tty)
534 return;
535
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800536 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 wake_up_interruptible(&tty->write_wait);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800541 * Main interrupt handler routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800543
Jiri Slaby8070e352006-01-09 20:54:22 -0800544static irqreturn_t isicom_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Jiri Slaby8070e352006-01-09 20:54:22 -0800546 struct isi_board *card = dev_id;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800547 struct isi_port *port;
548 struct tty_struct *tty;
Jiri Slaby8070e352006-01-09 20:54:22 -0800549 unsigned long base;
550 u16 header, word_count, count, channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 short byte_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800552 unsigned char *rp;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!card || !(card->status & FIRMWARE_LOADED))
555 return IRQ_NONE;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 base = card->base;
558 spin_lock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (card->isa == NO) {
561 /*
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800562 * disable any interrupts from the PCI card and lower the
563 * interrupt line
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
565 outw(0x8000, base+0x04);
566 ClearInterrupt(base);
567 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 inw(base); /* get the dummy word out */
570 header = inw(base);
571 channel = (header & 0x7800) >> card->shift_count;
572 byte_count = header & 0xff;
573
574 if (channel + 1 > card->port_count) {
Jiri Slaby8070e352006-01-09 20:54:22 -0800575 printk(KERN_WARNING "ISICOM: isicom_interrupt(0x%lx): %d(channel) > port_count.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 base, channel+1);
577 if (card->isa)
578 ClearInterrupt(base);
579 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800580 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_unlock(&card->card_lock);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800582 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 port = card->ports + channel;
585 if (!(port->flags & ASYNC_INITIALIZED)) {
586 if (card->isa)
587 ClearInterrupt(base);
588 else
589 outw(0x0000, base+0x04); /* enable interrupts */
590 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800591 }
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 tty = port->tty;
594 if (tty == NULL) {
595 word_count = byte_count >> 1;
596 while(byte_count > 1) {
597 inw(base);
598 byte_count -= 2;
599 }
600 if (byte_count & 0x01)
601 inw(base);
602 if (card->isa == YES)
603 ClearInterrupt(base);
604 else
605 outw(0x0000, base+0x04); /* enable interrupts */
606 spin_unlock(&card->card_lock);
607 return IRQ_HANDLED;
608 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (header & 0x8000) { /* Status Packet */
611 header = inw(base);
612 switch(header & 0xff) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800613 case 0: /* Change in EIA signals */
614
615 if (port->flags & ASYNC_CHECK_CD) {
616 if (port->status & ISI_DCD) {
617 if (!(header & ISI_DCD)) {
618 /* Carrier has been lost */
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800619 pr_dbg("interrupt: DCD->low.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 port->status &= ~ISI_DCD;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800621 schedule_work(&port->hangup_tq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624 else {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800625 if (header & ISI_DCD) {
626 /* Carrier has been detected */
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800627 pr_dbg("interrupt: DCD->high.\n");
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800628 port->status |= ISI_DCD;
629 wake_up_interruptible(&port->open_wait);
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800632 }
633 else {
634 if (header & ISI_DCD)
635 port->status |= ISI_DCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800637 port->status &= ~ISI_DCD;
638 }
639
640 if (port->flags & ASYNC_CTS_FLOW) {
641 if (port->tty->hw_stopped) {
642 if (header & ISI_CTS) {
643 port->tty->hw_stopped = 0;
644 /* start tx ing */
645 port->status |= (ISI_TXOK | ISI_CTS);
646 schedule_work(&port->bh_tqueue);
647 }
648 }
649 else {
650 if (!(header & ISI_CTS)) {
651 port->tty->hw_stopped = 1;
652 /* stop tx ing */
653 port->status &= ~(ISI_TXOK | ISI_CTS);
654 }
655 }
656 }
657 else {
658 if (header & ISI_CTS)
659 port->status |= ISI_CTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800661 port->status &= ~ISI_CTS;
662 }
663
664 if (header & ISI_DSR)
665 port->status |= ISI_DSR;
666 else
667 port->status &= ~ISI_DSR;
668
669 if (header & ISI_RI)
670 port->status |= ISI_RI;
671 else
672 port->status &= ~ISI_RI;
673
674 break;
675
676 case 1: /* Received Break !!! */
677 tty_insert_flip_char(tty, 0, TTY_BREAK);
678 if (port->flags & ASYNC_SAK)
679 do_SAK(tty);
680 tty_flip_buffer_push(tty);
681 break;
682
683 case 2: /* Statistics */
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800684 pr_dbg("isicom_interrupt: stats!!!.\n");
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800685 break;
686
687 default:
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800688 pr_dbg("Intr: Unknown code in status packet.\n");
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800689 break;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 else { /* Data Packet */
Alan Cox33f0f882006-01-09 20:54:13 -0800693
694 count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800695 pr_dbg("Intr: Can rx %d of %d bytes.\n", count, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 word_count = count >> 1;
Alan Cox33f0f882006-01-09 20:54:13 -0800697 insw(base, rp, word_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 byte_count -= (word_count << 1);
699 if (count & 0x0001) {
Alan Cox33f0f882006-01-09 20:54:13 -0800700 tty_insert_flip_char(tty, inw(base) & 0xff, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 byte_count -= 2;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (byte_count > 0) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800704 pr_dbg("Intr(0x%lx:%d): Flip buffer overflow! dropping "
705 "bytes...\n", base, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 while(byte_count > 0) { /* drain out unread xtra data */
707 inw(base);
708 byte_count -= 2;
709 }
710 }
Alan Cox33f0f882006-01-09 20:54:13 -0800711 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 if (card->isa == YES)
714 ClearInterrupt(base);
715 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800716 outw(0x0000, base+0x04); /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return IRQ_HANDLED;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800720static void isicom_config_port(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800722 struct isi_board *card = port->card;
723 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long baud;
Jiri Slaby8070e352006-01-09 20:54:22 -0800725 unsigned long base = card->base;
726 u16 channel_setup, channel = port->channel,
727 shift_count = card->shift_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned char flow_ctrl;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!(tty = port->tty) || !tty->termios)
731 return;
732 baud = C_BAUD(tty);
733 if (baud & CBAUDEX) {
734 baud &= ~CBAUDEX;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
737 * then the card is programmed for 57.6Kbps or 115Kbps
738 * respectively.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800739 */
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (baud < 1 || baud > 2)
742 port->tty->termios->c_cflag &= ~CBAUDEX;
743 else
744 baud += 15;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (baud == 15) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800747
748 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * by the set_serial_info ioctl ... this is done by
750 * the 'setserial' utility.
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800751 */
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800754 baud++; /* 57.6 Kbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800756 baud +=2; /* 115 Kbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 if (linuxb_to_isib[baud] == -1) {
759 /* hang up */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800760 drop_dtr(port);
761 return;
762 }
763 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 raise_dtr(port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (lock_card(card)) {
767 outw(0x8000 | (channel << shift_count) |0x03, base);
768 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
769 channel_setup = 0;
770 switch(C_CSIZE(tty)) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800771 case CS5:
772 channel_setup |= ISICOM_CS5;
773 break;
774 case CS6:
775 channel_setup |= ISICOM_CS6;
776 break;
777 case CS7:
778 channel_setup |= ISICOM_CS7;
779 break;
780 case CS8:
781 channel_setup |= ISICOM_CS8;
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (C_CSTOPB(tty))
786 channel_setup |= ISICOM_2SB;
787 if (C_PARENB(tty)) {
788 channel_setup |= ISICOM_EVPAR;
789 if (C_PARODD(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800790 channel_setup |= ISICOM_ODPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800792 outw(channel_setup, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 InterruptTheCard(base);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800794 unlock_card(card);
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (C_CLOCAL(tty))
797 port->flags &= ~ASYNC_CHECK_CD;
798 else
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800799 port->flags |= ASYNC_CHECK_CD;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* flow control settings ...*/
802 flow_ctrl = 0;
803 port->flags &= ~ASYNC_CTS_FLOW;
804 if (C_CRTSCTS(tty)) {
805 port->flags |= ASYNC_CTS_FLOW;
806 flow_ctrl |= ISICOM_CTSRTS;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800807 }
808 if (I_IXON(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
810 if (I_IXOFF(tty))
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800811 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (lock_card(card)) {
814 outw(0x8000 | (channel << shift_count) |0x04, base);
815 outw(flow_ctrl << 8 | 0x05, base);
816 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
817 InterruptTheCard(base);
818 unlock_card(card);
819 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* rx enabled -> enable port for rx on the card */
822 if (C_CREAD(tty)) {
823 card->port_status |= (1 << channel);
824 outw(card->port_status, base + 0x02);
825 }
826}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800828/* open et all */
829
830static inline void isicom_setup_board(struct isi_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 int channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800833 struct isi_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 spin_lock_irqsave(&bp->card_lock, flags);
837 if (bp->status & BOARD_ACTIVE) {
838 spin_unlock_irqrestore(&bp->card_lock, flags);
839 return;
840 }
841 port = bp->ports;
842 bp->status |= BOARD_ACTIVE;
843 spin_unlock_irqrestore(&bp->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800844 for (channel = 0; channel < bp->port_count; channel++, port++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 drop_dtr_rts(port);
846 return;
847}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800848
849static int isicom_setup_port(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800851 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (port->flags & ASYNC_INITIALIZED) {
855 return 0;
856 }
857 if (!port->xmit_buf) {
858 unsigned long page;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!(page = get_zeroed_page(GFP_KERNEL)))
861 return -ENOMEM;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (port->xmit_buf) {
864 free_page(page);
865 return -ERESTARTSYS;
866 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800867 port->xmit_buf = (unsigned char *) page;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 spin_lock_irqsave(&card->card_lock, flags);
871 if (port->tty)
872 clear_bit(TTY_IO_ERROR, &port->tty->flags);
873 if (port->count == 1)
874 card->count++;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* discard any residual data */
879 kill_queue(port, ISICOM_KILLTX | ISICOM_KILLRX);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 isicom_config_port(port);
882 port->flags |= ASYNC_INITIALIZED;
883 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800884
885 return 0;
886}
887
888static int block_til_ready(struct tty_struct *tty, struct file *filp, struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800890 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int do_clocal = 0, retval;
892 unsigned long flags;
893 DECLARE_WAITQUEUE(wait, current);
894
895 /* block if port is in the process of being closed */
896
897 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800898 pr_dbg("block_til_ready: close in progress.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 interruptible_sleep_on(&port->close_wait);
900 if (port->flags & ASYNC_HUP_NOTIFY)
901 return -EAGAIN;
902 else
903 return -ERESTARTSYS;
904 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* if non-blocking mode is set ... */
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -0800909 pr_dbg("block_til_ready: non-block mode.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800911 return 0;
912 }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (C_CLOCAL(tty))
915 do_clocal = 1;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800916
917 /* block waiting for DCD to be asserted, and while
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 callout dev is busy */
919 retval = 0;
920 add_wait_queue(&port->open_wait, &wait);
921
922 spin_lock_irqsave(&card->card_lock, flags);
923 if (!tty_hung_up_p(filp))
924 port->count--;
925 port->blocked_open++;
926 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 while (1) {
929 raise_dtr_rts(port);
930
931 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800932 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (port->flags & ASYNC_HUP_NOTIFY)
934 retval = -EAGAIN;
935 else
936 retval = -ERESTARTSYS;
937 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800940 (do_clocal || (port->status & ISI_DCD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (signal_pending(current)) {
944 retval = -ERESTARTSYS;
945 break;
946 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800947 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 set_current_state(TASK_RUNNING);
950 remove_wait_queue(&port->open_wait, &wait);
951 spin_lock_irqsave(&card->card_lock, flags);
952 if (!tty_hung_up_p(filp))
953 port->count++;
954 port->blocked_open--;
955 spin_unlock_irqrestore(&card->card_lock, flags);
956 if (retval)
957 return retval;
958 port->flags |= ASYNC_NORMAL_ACTIVE;
959 return 0;
960}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800961
962static int isicom_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800964 struct isi_port *port;
965 struct isi_board *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned int line, board;
967 int error;
968
969 line = tty->index;
970 if (line < 0 || line > PORT_COUNT-1)
971 return -ENODEV;
972 board = BOARD(line);
973 card = &isi_card[board];
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!(card->status & FIRMWARE_LOADED))
976 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* open on a port greater than the port count for the card !!! */
979 if (line > ((board * 16) + card->port_count - 1))
980 return -ENODEV;
981
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800982 port = &isi_ports[line];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (isicom_paranoia_check(port, tty->name, "isicom_open"))
984 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800985
986 isicom_setup_board(card);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 port->count++;
989 tty->driver_data = port;
990 port->tty = tty;
991 if ((error = isicom_setup_port(port))!=0)
992 return error;
993 if ((error = block_til_ready(tty, filp, port))!=0)
994 return error;
995
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Jiri Slabyd8d16e42006-01-09 20:54:21 -0800998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* close et all */
1000
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001001static inline void isicom_shutdown_board(struct isi_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 unsigned long flags;
1004
1005 spin_lock_irqsave(&bp->card_lock, flags);
1006 if (bp->status & BOARD_ACTIVE) {
1007 bp->status &= ~BOARD_ACTIVE;
1008 }
1009 spin_unlock_irqrestore(&bp->card_lock, flags);
1010}
1011
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001012static void isicom_shutdown_port(struct isi_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001014 struct isi_board *card = port->card;
1015 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tty = port->tty;
1019
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001020 spin_lock_irqsave(&card->card_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!(port->flags & ASYNC_INITIALIZED)) {
1022 spin_unlock_irqrestore(&card->card_lock, flags);
1023 return;
1024 }
1025 if (port->xmit_buf) {
1026 free_page((unsigned long) port->xmit_buf);
1027 port->xmit_buf = NULL;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 port->flags &= ~ASYNC_INITIALIZED;
1030 /* 3rd October 2000 : Vinayak P Risbud */
1031 port->tty = NULL;
1032 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*Fix done by Anil .S on 30-04-2001
1035 remote login through isi port has dtr toggle problem
1036 due to which the carrier drops before the password prompt
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001037 appears on the remote end. Now we drop the dtr only if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 HUPCL(Hangup on close) flag is set for the tty*/
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001039
1040 if (C_HUPCL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* drop dtr on this port */
1042 drop_dtr(port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001043
1044 /* any other port uninits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (tty)
1046 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (--card->count < 0) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001049 pr_dbg("isicom_shutdown_port: bad board(0x%lx) count %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 card->base, card->count);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001051 card->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* last port was closed , shutdown that boad too */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001055 if (C_HUPCL(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!card->count)
1057 isicom_shutdown_board(card);
1058 }
1059}
1060
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001061static void isicom_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jiri Slaby8070e352006-01-09 20:54:22 -08001063 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001064 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!port)
1068 return;
1069 if (isicom_paranoia_check(port, tty->name, "isicom_close"))
1070 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001071
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001072 pr_dbg("Close start!!!.\n");
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_lock_irqsave(&card->card_lock, flags);
1075 if (tty_hung_up_p(filp)) {
1076 spin_unlock_irqrestore(&card->card_lock, flags);
1077 return;
1078 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (tty->count == 1 && port->count != 1) {
Jiri Slaby8070e352006-01-09 20:54:22 -08001081 printk(KERN_WARNING "ISICOM:(0x%lx) isicom_close: bad port count"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 "tty->count = 1 port count = %d.\n",
1083 card->base, port->count);
1084 port->count = 1;
1085 }
1086 if (--port->count < 0) {
Jiri Slaby8070e352006-01-09 20:54:22 -08001087 printk(KERN_WARNING "ISICOM:(0x%lx) isicom_close: bad port count for"
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001088 "channel%d = %d", card->base, port->channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 port->count);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001090 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (port->count) {
1094 spin_unlock_irqrestore(&card->card_lock, flags);
1095 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 port->flags |= ASYNC_CLOSING;
1098 tty->closing = 1;
1099 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1102 tty_wait_until_sent(tty, port->closing_wait);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001103 /* indicate to the card that no more data can be received
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 on this port */
1105 spin_lock_irqsave(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001106 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 card->port_status &= ~(1 << port->channel);
1108 outw(card->port_status, card->base + 0x02);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 isicom_shutdown_port(port);
1111 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (tty->driver->flush_buffer)
1114 tty->driver->flush_buffer(tty);
1115 tty_ldisc_flush(tty);
1116
1117 spin_lock_irqsave(&card->card_lock, flags);
1118 tty->closing = 0;
1119
1120 if (port->blocked_open) {
1121 spin_unlock_irqrestore(&card->card_lock, flags);
1122 if (port->close_delay) {
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001123 pr_dbg("scheduling until time out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1125 }
1126 spin_lock_irqsave(&card->card_lock, flags);
1127 wake_up_interruptible(&port->open_wait);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1130 wake_up_interruptible(&port->close_wait);
1131 spin_unlock_irqrestore(&card->card_lock, flags);
1132}
1133
1134/* write et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001135static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
1136 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Jiri Slaby8070e352006-01-09 20:54:22 -08001138 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001139 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned long flags;
1141 int cnt, total = 0;
1142
1143 if (isicom_paranoia_check(port, tty->name, "isicom_write"))
1144 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001145
Jiri Slaby9ac09482006-01-09 20:54:24 -08001146 if (!tty || !port->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 spin_lock_irqsave(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001150
1151 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001153 SERIAL_XMIT_SIZE - port->xmit_head));
1154 if (cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1158 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1159 port->xmit_cnt += cnt;
1160 buf += cnt;
1161 count -= cnt;
1162 total += cnt;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1165 port->status |= ISI_TXOK;
1166 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001167 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/* put_char et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001171static void isicom_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Jiri Slaby8070e352006-01-09 20:54:22 -08001173 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001174 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
1178 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!tty || !port->xmit_buf)
1181 return;
1182
1183 spin_lock_irqsave(&card->card_lock, flags);
1184 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1185 spin_unlock_irqrestore(&card->card_lock, flags);
1186 return;
1187 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 port->xmit_buf[port->xmit_head++] = ch;
1190 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1191 port->xmit_cnt++;
1192 spin_unlock_irqrestore(&card->card_lock, flags);
1193}
1194
1195/* flush_chars et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001196static void isicom_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jiri Slaby8070e352006-01-09 20:54:22 -08001198 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1201 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped || !port->xmit_buf)
1204 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* this tells the transmitter to consider this port for
1207 data output to the card ... that's the best we can do. */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001208 port->status |= ISI_TXOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/* write_room et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001212static int isicom_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Jiri Slaby8070e352006-01-09 20:54:22 -08001214 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int free;
1216
1217 if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1218 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1221 if (free < 0)
1222 free = 0;
1223 return free;
1224}
1225
1226/* chars_in_buffer et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001227static int isicom_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Jiri Slaby8070e352006-01-09 20:54:22 -08001229 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1231 return 0;
1232 return port->xmit_cnt;
1233}
1234
1235/* ioctl et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001236static inline void isicom_send_break(struct isi_port *port, unsigned long length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001238 struct isi_board *card = port->card;
Jiri Slaby8070e352006-01-09 20:54:22 -08001239 unsigned long base = card->base;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001240
1241 if (!lock_card(card))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1245 outw((length & 0xff) << 8 | 0x00, base);
1246 outw((length & 0xff00), base);
1247 InterruptTheCard(base);
1248
1249 unlock_card(card);
1250}
1251
1252static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
1253{
Jiri Slaby8070e352006-01-09 20:54:22 -08001254 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* just send the port status */
Jiri Slaby8070e352006-01-09 20:54:22 -08001256 u16 status = port->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1259 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1262 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1263 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1264 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1265 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1266 ((status & ISI_RI ) ? TIOCM_RI : 0);
1267}
1268
1269static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001270 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Jiri Slaby8070e352006-01-09 20:54:22 -08001272 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1275 return -ENODEV;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (set & TIOCM_RTS)
1278 raise_rts(port);
1279 if (set & TIOCM_DTR)
1280 raise_dtr(port);
1281
1282 if (clear & TIOCM_RTS)
1283 drop_rts(port);
1284 if (clear & TIOCM_DTR)
1285 drop_dtr(port);
1286
1287 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001290static int isicom_set_serial_info(struct isi_port *port,
1291 struct serial_struct __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 struct serial_struct newinfo;
1294 int reconfig_port;
1295
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001296 if (copy_from_user(&newinfo, info, sizeof(newinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EFAULT;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001298
1299 reconfig_port = ((port->flags & ASYNC_SPD_MASK) !=
1300 (newinfo.flags & ASYNC_SPD_MASK));
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!capable(CAP_SYS_ADMIN)) {
1303 if ((newinfo.close_delay != port->close_delay) ||
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001304 (newinfo.closing_wait != port->closing_wait) ||
1305 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1306 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EPERM;
1308 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1309 (newinfo.flags & ASYNC_USR_MASK));
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 else {
1312 port->close_delay = newinfo.close_delay;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001313 port->closing_wait = newinfo.closing_wait;
1314 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 (newinfo.flags & ASYNC_FLAGS));
1316 }
1317 if (reconfig_port) {
1318 isicom_config_port(port);
1319 }
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001320 return 0;
1321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001323static int isicom_get_serial_info(struct isi_port *port,
1324 struct serial_struct __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct serial_struct out_info;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 memset(&out_info, 0, sizeof(out_info));
1329/* out_info.type = ? */
1330 out_info.line = port - isi_ports;
1331 out_info.port = port->card->base;
1332 out_info.irq = port->card->irq;
1333 out_info.flags = port->flags;
1334/* out_info.baud_base = ? */
1335 out_info.close_delay = port->close_delay;
1336 out_info.closing_wait = port->closing_wait;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001337 if (copy_to_user(info, &out_info, sizeof(out_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EFAULT;
1339 return 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001342static int isicom_ioctl(struct tty_struct *tty, struct file *filp,
1343 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Jiri Slaby8070e352006-01-09 20:54:22 -08001345 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 void __user *argp = (void __user *)arg;
1347 int retval;
1348
1349 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1350 return -ENODEV;
1351
1352 switch(cmd) {
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001353 case TCSBRK:
1354 retval = tty_check_change(tty);
1355 if (retval)
1356 return retval;
1357 tty_wait_until_sent(tty, 0);
1358 if (!arg)
1359 isicom_send_break(port, HZ/4);
1360 return 0;
1361
1362 case TCSBRKP:
1363 retval = tty_check_change(tty);
1364 if (retval)
1365 return retval;
1366 tty_wait_until_sent(tty, 0);
1367 isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1368 return 0;
1369
1370 case TIOCGSOFTCAR:
1371 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1372
1373 case TIOCSSOFTCAR:
1374 if (get_user(arg, (unsigned long __user *) argp))
1375 return -EFAULT;
1376 tty->termios->c_cflag =
1377 ((tty->termios->c_cflag & ~CLOCAL) |
1378 (arg ? CLOCAL : 0));
1379 return 0;
1380
1381 case TIOCGSERIAL:
1382 return isicom_get_serial_info(port, argp);
1383
1384 case TIOCSSERIAL:
1385 return isicom_set_serial_info(port, argp);
1386
1387 default:
1388 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 return 0;
1391}
1392
1393/* set_termios et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001394static void isicom_set_termios(struct tty_struct *tty,
1395 struct termios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Jiri Slaby8070e352006-01-09 20:54:22 -08001397 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1400 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (tty->termios->c_cflag == old_termios->c_cflag &&
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001403 tty->termios->c_iflag == old_termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 isicom_config_port(port);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001409 !(tty->termios->c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 tty->hw_stopped = 0;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001411 isicom_start(tty);
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415/* throttle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001416static void isicom_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jiri Slaby8070e352006-01-09 20:54:22 -08001418 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001419 struct isi_board *card = port->card;
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1422 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* tell the card that this port cannot handle any more data for now */
1425 card->port_status &= ~(1 << port->channel);
1426 outw(card->port_status, card->base + 0x02);
1427}
1428
1429/* unthrottle et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001430static void isicom_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Jiri Slaby8070e352006-01-09 20:54:22 -08001432 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001433 struct isi_board *card = port->card;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1436 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* tell the card that this port is ready to accept more data */
1439 card->port_status |= (1 << port->channel);
1440 outw(card->port_status, card->base + 0x02);
1441}
1442
1443/* stop et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001444static void isicom_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Jiri Slaby8070e352006-01-09 20:54:22 -08001446 struct isi_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1449 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* this tells the transmitter not to consider this port for
1452 data output to the card. */
1453 port->status &= ~ISI_TXOK;
1454}
1455
1456/* start et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001457static void isicom_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Jiri Slaby8070e352006-01-09 20:54:22 -08001459 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1462 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /* this tells the transmitter to consider this port for
1465 data output to the card. */
1466 port->status |= ISI_TXOK;
1467}
1468
1469/* hangup et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001470static void do_isicom_hangup(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Jiri Slaby8070e352006-01-09 20:54:22 -08001472 struct isi_port *port = data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001473 struct tty_struct *tty;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 tty = port->tty;
1476 if (tty)
1477 tty_hangup(tty);
1478}
1479
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001480static void isicom_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Jiri Slaby8070e352006-01-09 20:54:22 -08001482 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1485 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 isicom_shutdown_port(port);
1488 port->count = 0;
1489 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1490 port->tty = NULL;
1491 wake_up_interruptible(&port->open_wait);
1492}
1493
1494/* flush_buffer et all */
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001495static void isicom_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Jiri Slaby8070e352006-01-09 20:54:22 -08001497 struct isi_port *port = tty->driver_data;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001498 struct isi_board *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 unsigned long flags;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
1502 return;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 spin_lock_irqsave(&card->card_lock, flags);
1505 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1506 spin_unlock_irqrestore(&card->card_lock, flags);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 wake_up_interruptible(&tty->write_wait);
1509 tty_wakeup(tty);
1510}
1511
Jiri Slaby9ac09482006-01-09 20:54:24 -08001512/*
1513 * Driver init and deinit functions
1514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jiri Slaby9ac09482006-01-09 20:54:24 -08001516static int __devinit isicom_register_ioregion(struct pci_dev *pdev,
1517 const unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Jiri Slaby9ac09482006-01-09 20:54:24 -08001519 struct isi_board *board = pci_get_drvdata(pdev);
1520
1521 if (!board->base)
1522 return -EINVAL;
1523
1524 if (!request_region(board->base, 16, ISICOM_NAME)) {
1525 dev_dbg(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
1526 "will be disabled.\n", board->base, board->base + 15,
1527 index + 1);
1528 return -EBUSY;
1529 }
1530
1531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Jiri Slaby9ac09482006-01-09 20:54:24 -08001534static void isicom_unregister_ioregion(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Jiri Slaby9ac09482006-01-09 20:54:24 -08001536 struct isi_board *board = pci_get_drvdata(pdev);
1537
1538 if (!board->base)
1539 return;
1540
1541 release_region(board->base, 16);
1542 dev_dbg(&pdev->dev, "I/O Region 0x%lx-0x%lx released.\n",
1543 board->base, board->base + 15);
1544 board->base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547static struct tty_operations isicom_ops = {
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001548 .open = isicom_open,
1549 .close = isicom_close,
1550 .write = isicom_write,
1551 .put_char = isicom_put_char,
1552 .flush_chars = isicom_flush_chars,
1553 .write_room = isicom_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .chars_in_buffer = isicom_chars_in_buffer,
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001555 .ioctl = isicom_ioctl,
1556 .set_termios = isicom_set_termios,
1557 .throttle = isicom_throttle,
1558 .unthrottle = isicom_unthrottle,
1559 .stop = isicom_stop,
1560 .start = isicom_start,
1561 .hangup = isicom_hangup,
1562 .flush_buffer = isicom_flush_buffer,
1563 .tiocmget = isicom_tiocmget,
1564 .tiocmset = isicom_tiocmset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565};
1566
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001567static int __devinit isicom_register_tty_driver(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001569 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /* tty driver structure initialization */
1572 isicom_normal = alloc_tty_driver(PORT_COUNT);
1573 if (!isicom_normal)
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001574 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001576 isicom_normal->owner = THIS_MODULE;
1577 isicom_normal->name = "ttyM";
1578 isicom_normal->devfs_name = "isicom/";
1579 isicom_normal->major = ISICOM_NMAJOR;
1580 isicom_normal->minor_start = 0;
1581 isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
1582 isicom_normal->subtype = SERIAL_TYPE_NORMAL;
1583 isicom_normal->init_termios = tty_std_termios;
1584 isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1585 CLOCAL;
1586 isicom_normal->flags = TTY_DRIVER_REAL_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 tty_set_operations(isicom_normal, &isicom_ops);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001588
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001589 if ((error = tty_register_driver(isicom_normal))) {
1590 pr_dbg("Couldn't register the dialin driver, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 error);
1592 put_tty_driver(isicom_normal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001594end:
1595 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001598static void isicom_unregister_tty_driver(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Jiri Slabyaaa246e2006-01-09 20:54:23 -08001600 int error;
1601
1602 if ((error = tty_unregister_driver(isicom_normal)))
1603 pr_dbg("couldn't unregister normal driver, error=%d.\n", error);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 put_tty_driver(isicom_normal);
1606}
1607
Jiri Slaby9ac09482006-01-09 20:54:24 -08001608static int __devinit isicom_register_isr(struct pci_dev *pdev,
1609 const unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Jiri Slaby9ac09482006-01-09 20:54:24 -08001611 struct isi_board *board = pci_get_drvdata(pdev);
1612 unsigned long irqflags = SA_INTERRUPT;
1613 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jiri Slaby9ac09482006-01-09 20:54:24 -08001615 if (!board->base)
1616 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jiri Slaby9ac09482006-01-09 20:54:24 -08001618 if (board->isa == NO)
1619 irqflags |= SA_SHIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Jiri Slaby9ac09482006-01-09 20:54:24 -08001621 retval = request_irq(board->irq, isicom_interrupt, irqflags,
1622 ISICOM_NAME, board);
1623 if (retval < 0)
1624 dev_warn(&pdev->dev, "Could not install handler at Irq %d. "
1625 "Card%d will be disabled.\n", board->irq, index + 1);
1626 else
1627 retval = 0;
1628end:
1629 return retval;
1630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Jiri Slaby9ac09482006-01-09 20:54:24 -08001632static int __devinit reset_card(struct pci_dev *pdev,
1633 const unsigned int card, unsigned int *signature)
1634{
1635 struct isi_board *board = pci_get_drvdata(pdev);
1636 unsigned long base = board->base;
1637 unsigned int portcount = 0;
1638 int retval = 0;
1639
1640 dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
1641 base);
1642
1643 inw(base + 0x8);
1644
1645 mdelay(10);
1646
1647 outw(0, base + 0x8); /* Reset */
1648
1649 msleep(3000);
1650
1651 *signature = inw(base + 0x4) & 0xff;
1652
1653 if (board->isa == YES) {
1654 if (!(inw(base + 0xe) & 0x1) || (inw(base + 0x2))) {
1655 dev_dbg(&pdev->dev, "base+0x2=0x%lx, base+0xe=0x%lx\n",
1656 inw(base + 0x2), inw(base + 0xe));
1657 dev_err(&pdev->dev, "ISILoad:ISA Card%d reset failure "
1658 "(Possible bad I/O Port Address 0x%lx).\n",
1659 card + 1, base);
1660 retval = -EIO;
1661 goto end;
1662 }
1663 } else {
1664 portcount = inw(base + 0x2);
1665 if (!(inw(base + 0xe) & 0x1) || ((portcount != 0) &&
1666 (portcount != 4) && (portcount != 8))) {
1667 dev_dbg(&pdev->dev, "base+0x2=0x%lx, base+0xe=0x%lx\n",
1668 inw(base + 0x2), inw(base + 0xe));
1669 dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure "
1670 "(Possible bad I/O Port Address 0x%lx).\n",
1671 card + 1, base);
1672 retval = -EIO;
1673 goto end;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jiri Slaby9ac09482006-01-09 20:54:24 -08001677 switch (*signature) {
1678 case 0xa5:
1679 case 0xbb:
1680 case 0xdd:
1681 board->port_count = (board->isa == NO && portcount == 4) ? 4 :
1682 8;
1683 board->shift_count = 12;
1684 break;
1685 case 0xcc:
1686 board->port_count = 16;
1687 board->shift_count = 11;
1688 break;
1689 default:
1690 dev_warn(&pdev->dev, "ISILoad:Card%d reset failure (Possible "
1691 "bad I/O Port Address 0x%lx).\n", card + 1, base);
1692 dev_dbg(&pdev->dev, "Sig=0x%lx\n", signature);
1693 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Jiri Slaby9ac09482006-01-09 20:54:24 -08001695 dev_info(&pdev->dev, "-Done\n");
1696
1697end:
1698 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Jiri Slabye65c1db2006-01-09 20:54:25 -08001701static inline int WaitTillCardIsFree(u16 base)
1702{
1703 unsigned long count = 0;
1704
1705 while (!(inw(base + 0xe) & 0x1) && count++ < 100)
1706 msleep(5);
1707
1708 return !(inw(base + 0xe) & 0x1);
1709}
1710
1711static int __devinit load_firmware(struct pci_dev *pdev,
1712 const unsigned int index, const unsigned int signature)
1713{
1714 struct isi_board *board = pci_get_drvdata(pdev);
1715 const struct firmware *fw;
1716 unsigned long base = board->base;
1717 unsigned int a;
1718 u16 word_count, status;
1719 int retval = -EIO;
1720 char *name;
1721 u8 *data;
1722
1723 struct stframe {
1724 u16 addr;
1725 u16 count;
1726 u8 data[0];
1727 } *frame;
1728
1729 switch (signature) {
1730 case 0xa5:
1731 name = "isi608.bin";
1732 break;
1733 case 0xbb:
1734 name = "isi608em.bin";
1735 break;
1736 case 0xcc:
1737 name = "isi616em.bin";
1738 break;
1739 case 0xdd:
1740 name = "isi4608.bin";
1741 break;
1742 case 0xee:
1743 name = "isi4616.bin";
1744 break;
1745 default:
1746 dev_err(&pdev->dev, "Unknown signature.\n");
1747 goto end;
1748 }
1749
1750 retval = request_firmware(&fw, name, &pdev->dev);
1751 if (retval)
1752 goto end;
1753
1754 for (frame = (struct stframe *)fw->data;
1755 frame < (struct stframe *)(fw->data + fw->size);
1756 frame++) {
1757 if (WaitTillCardIsFree(base))
1758 goto errrelfw;
1759
1760 outw(0xf0, base); /* start upload sequence */
1761 outw(0x00, base);
1762 outw(frame->addr, base); /* lsb of address */
1763
1764 word_count = frame->count / 2 + frame->count % 2;
1765 outw(word_count, base);
1766 InterruptTheCard(base);
1767
1768 udelay(100); /* 0x2f */
1769
1770 if (WaitTillCardIsFree(base))
1771 goto errrelfw;
1772
1773 if ((status = inw(base + 0x4)) != 0) {
1774 dev_warn(&pdev->dev, "Card%d rejected load header:\n"
1775 "Address:0x%x\nCount:0x%x\nStatus:0x%x\n",
1776 index + 1, frame->addr, frame->count, status);
1777 goto errrelfw;
1778 }
1779 outsw(base, frame->data, word_count);
1780
1781 InterruptTheCard(base);
1782
1783 udelay(50); /* 0x0f */
1784
1785 if (WaitTillCardIsFree(base))
1786 goto errrelfw;
1787
1788 if ((status = inw(base + 0x4)) != 0) {
1789 dev_err(&pdev->dev, "Card%d got out of sync.Card "
1790 "Status:0x%x\n", index + 1, status);
1791 goto errrelfw;
1792 }
1793 }
1794
1795 retval = -EIO;
1796
1797 if (WaitTillCardIsFree(base))
1798 goto errrelfw;
1799
1800 outw(0xf2, base);
1801 outw(0x800, base);
1802 outw(0x0, base);
1803 outw(0x0, base);
1804 InterruptTheCard(base);
1805 outw(0x0, base + 0x4); /* for ISI4608 cards */
1806
1807/* XXX: should we test it by reading it back and comparing with original like
1808 * in load firmware package? */
1809 for (frame = (struct stframe*)fw->data;
1810 frame < (struct stframe*)(fw->data + fw->size);
1811 frame++) {
1812 if (WaitTillCardIsFree(base))
1813 goto errrelfw;
1814
1815 outw(0xf1, base); /* start download sequence */
1816 outw(0x00, base);
1817 outw(frame->addr, base); /* lsb of address */
1818
1819 word_count = (frame->count >> 1) + frame->count % 2;
1820 outw(word_count + 1, base);
1821 InterruptTheCard(base);
1822
1823 udelay(50); /* 0xf */
1824
1825 if (WaitTillCardIsFree(base))
1826 goto errrelfw;
1827
1828 if ((status = inw(base + 0x4)) != 0) {
1829 dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
1830 "Address:0x%x\nCount:0x%x\nStatus: 0x%x\n",
1831 index + 1, frame->addr, frame->count, status);
1832 goto errrelfw;
1833 }
1834
1835 data = kmalloc(word_count * 2, GFP_KERNEL);
1836 inw(base);
1837 insw(base, data, word_count);
1838 InterruptTheCard(base);
1839
1840 for (a = 0; a < frame->count; a++)
1841 if (data[a] != frame->data[a]) {
1842 kfree(data);
1843 dev_err(&pdev->dev, "Card%d, firmware upload "
1844 "failed\n", index + 1);
1845 goto errrelfw;
1846 }
1847 kfree(data);
1848
1849 udelay(50); /* 0xf */
1850
1851 if (WaitTillCardIsFree(base))
1852 goto errrelfw;
1853
1854 if ((status = inw(base + 0x4)) != 0) {
1855 dev_err(&pdev->dev, "Card%d verify got out of sync. "
1856 "Card Status:0x%x\n", index + 1, status);
1857 goto errrelfw;
1858 }
1859 }
1860
1861 board->status |= FIRMWARE_LOADED;
1862 retval = 0;
1863
1864errrelfw:
1865 release_firmware(fw);
1866end:
1867 return retval;
1868}
1869
Jiri Slaby9ac09482006-01-09 20:54:24 -08001870/*
1871 * Insmod can set static symbols so keep these static
1872 */
1873static int io[4];
1874static int irq[4];
1875static int card;
1876
1877static int __devinit isicom_probe(struct pci_dev *pdev,
1878 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Jiri Slaby9ac09482006-01-09 20:54:24 -08001880 unsigned int ioaddr, signature, index;
1881 int retval = -EPERM;
1882 u8 pciirq;
1883 struct isi_board *board = NULL;
1884
1885 if (card >= BOARD_COUNT)
1886 goto err;
1887
1888 ioaddr = pci_resource_start(pdev, 3);
1889 /* i.e at offset 0x1c in the PCI configuration register space. */
1890 pciirq = pdev->irq;
1891 dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
1892
1893 /* allot the first empty slot in the array */
1894 for (index = 0; index < BOARD_COUNT; index++)
1895 if (isi_card[index].base == 0) {
1896 board = &isi_card[index];
1897 break;
1898 }
1899
1900 board->base = ioaddr;
1901 board->irq = pciirq;
1902 board->isa = NO;
1903 card++;
1904
1905 pci_set_drvdata(pdev, board);
1906
1907 retval = isicom_register_ioregion(pdev, index);
1908 if (retval < 0)
1909 goto err;
1910
1911 retval = isicom_register_isr(pdev, index);
1912 if (retval < 0)
1913 goto errunrr;
1914
1915 retval = reset_card(pdev, index, &signature);
1916 if (retval < 0)
1917 goto errunri;
1918
Jiri Slabye65c1db2006-01-09 20:54:25 -08001919 retval = load_firmware(pdev, index, signature);
1920 if (retval < 0)
1921 goto errunri;
1922
Jiri Slaby9ac09482006-01-09 20:54:24 -08001923 return 0;
1924
1925errunri:
1926 free_irq(board->irq, board);
1927errunrr:
1928 isicom_unregister_ioregion(pdev);
1929err:
1930 board->base = 0;
1931 return retval;
1932}
1933
1934static void __devexit isicom_remove(struct pci_dev *pdev)
1935{
1936 struct isi_board *board = pci_get_drvdata(pdev);
1937
1938 free_irq(board->irq, board);
1939 isicom_unregister_ioregion(pdev);
1940}
1941
1942static int __devinit isicom_setup(void)
1943{
1944 int retval, idx, channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001945 struct isi_port *port;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001946
Jiri Slaby9ac09482006-01-09 20:54:24 -08001947 card = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 memset(isi_ports, 0, sizeof(isi_ports));
Jiri Slaby9ac09482006-01-09 20:54:24 -08001949
1950 for(idx = 0; idx < BOARD_COUNT; idx++) {
1951 port = &isi_ports[idx * 16];
1952 isi_card[idx].ports = port;
1953 spin_lock_init(&isi_card[idx].card_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 for (channel = 0; channel < 16; channel++, port++) {
1955 port->magic = ISICOM_MAGIC;
Jiri Slaby9ac09482006-01-09 20:54:24 -08001956 port->card = &isi_card[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 port->channel = channel;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001958 port->close_delay = 50 * HZ/100;
1959 port->closing_wait = 3000 * HZ/100;
1960 INIT_WORK(&port->hangup_tq, do_isicom_hangup, port);
1961 INIT_WORK(&port->bh_tqueue, isicom_bottomhalf, port);
1962 port->status = 0;
1963 init_waitqueue_head(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 init_waitqueue_head(&port->close_wait);
1965 /* . . . */
Jiri Slaby9ac09482006-01-09 20:54:24 -08001966 }
1967 isi_card[idx].base = 0;
1968 isi_card[idx].irq = 0;
1969
1970 if (!io[idx])
1971 continue;
1972
1973 if (irq[idx] == 2 || irq[idx] == 3 || irq[idx] == 4 ||
1974 irq[idx] == 5 || irq[idx] == 7 ||
1975 irq[idx] == 10 || irq[idx] == 11 ||
1976 irq[idx] == 12 || irq[idx] == 15) {
1977 printk(KERN_ERR "ISICOM: ISA not supported yet.\n");
1978 retval = -EINVAL;
1979 goto error;
1980 } else
1981 printk(KERN_ERR "ISICOM: Irq %d unsupported. "
1982 "Disabling Card%d...\n", irq[idx], idx + 1);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001983 }
1984
Jiri Slaby9ac09482006-01-09 20:54:24 -08001985 retval = isicom_register_tty_driver();
1986 if (retval < 0)
1987 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jiri Slaby9ac09482006-01-09 20:54:24 -08001989 retval = pci_register_driver(&isicom_driver);
1990 if (retval < 0) {
1991 printk(KERN_ERR "ISICOM: Unable to register pci driver.\n");
1992 goto errtty;
Jiri Slabyd8d16e42006-01-09 20:54:21 -08001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 init_timer(&tx);
1996 tx.expires = jiffies + 1;
1997 tx.data = 0;
1998 tx.function = isicom_tx;
1999 re_schedule = 1;
2000 add_timer(&tx);
Jiri Slabyd8d16e42006-01-09 20:54:21 -08002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
Jiri Slaby9ac09482006-01-09 20:54:24 -08002003errtty:
2004 isicom_unregister_tty_driver();
2005error:
2006 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009static void __exit isicom_exit(void)
2010{
Jiri Slabyaaa246e2006-01-09 20:54:23 -08002011 unsigned int index = 0;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 re_schedule = 0;
Jiri Slabyaaa246e2006-01-09 20:54:23 -08002014
2015 while (re_schedule != 2 && index++ < 100)
2016 msleep(10);
2017
Jiri Slaby9ac09482006-01-09 20:54:24 -08002018 pci_unregister_driver(&isicom_driver);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08002019 isicom_unregister_tty_driver();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
2022module_init(isicom_setup);
2023module_exit(isicom_exit);
Jiri Slabyaaa246e2006-01-09 20:54:23 -08002024
2025MODULE_AUTHOR("MultiTech");
2026MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
2027MODULE_LICENSE("GPL");
2028module_param_array(io, int, NULL, 0);
2029MODULE_PARM_DESC(io, "I/O ports for the cards");
2030module_param_array(irq, int, NULL, 0);
2031MODULE_PARM_DESC(irq, "Interrupts for the cards");