blob: a978c57b6b2bcf25c95d6be7d414c3b1ca2629d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7* PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
8* serial I/O controllers.
9*
10* DESCRIPTION: Mainline code for the device driver
11*
12*******************************************************************************/
13// ToDo:
14//
15// Fix the immediate DSS_NOW problem.
16// Work over the channel stats return logic in ip2_ipl_ioctl so they
17// make sense for all 256 possible channels and so the user space
18// utilities will compile and work properly.
19//
20// Done:
21//
22// 1.2.14 /\/\|=mhw=|\/\/
23// Added bounds checking to ip2_ipl_ioctl to avoid potential terroristic acts.
24// Changed the definition of ip2trace to be more consistent with kernel style
25// Thanks to Andreas Dilger <adilger@turbolabs.com> for these updates
26//
27// 1.2.13 /\/\|=mhw=|\/\/
28// DEVFS: Renamed ttf/{n} to tts/F{n} and cuf/{n} to cua/F{n} to conform
29// to agreed devfs serial device naming convention.
30//
31// 1.2.12 /\/\|=mhw=|\/\/
32// Cleaned up some remove queue cut and paste errors
33//
34// 1.2.11 /\/\|=mhw=|\/\/
35// Clean up potential NULL pointer dereferences
36// Clean up devfs registration
37// Add kernel command line parsing for io and irq
Adrian Bunk9c4b5622006-01-19 18:07:10 +010038// Compile defaults for io and irq are now set in ip2.c not ip2.h!
Linus Torvalds1da177e2005-04-16 15:20:36 -070039// Reworked poll_only hack for explicit parameter setting
40// You must now EXPLICITLY set poll_only = 1 or set all irqs to 0
41// Merged ip2_loadmain and old_ip2_init
42// Converted all instances of interruptible_sleep_on into queue calls
43// Most of these had no race conditions but better to clean up now
44//
45// 1.2.10 /\/\|=mhw=|\/\/
46// Fixed the bottom half interrupt handler and enabled USE_IQI
47// to split the interrupt handler into a formal top-half / bottom-half
48// Fixed timing window on high speed processors that queued messages to
49// the outbound mail fifo faster than the board could handle.
50//
51// 1.2.9
52// Four box EX was barfing on >128k kmalloc, made structure smaller by
53// reducing output buffer size
54//
55// 1.2.8
56// Device file system support (MHW)
57//
58// 1.2.7
59// Fixed
60// Reload of ip2 without unloading ip2main hangs system on cat of /proc/modules
61//
62// 1.2.6
63//Fixes DCD problems
64// DCD was not reported when CLOCAL was set on call to TIOCMGET
65//
66//Enhancements:
67// TIOCMGET requests and waits for status return
68// No DSS interrupts enabled except for DCD when needed
69//
70// For internal use only
71//
72//#define IP2DEBUG_INIT
73//#define IP2DEBUG_OPEN
74//#define IP2DEBUG_WRITE
75//#define IP2DEBUG_READ
76//#define IP2DEBUG_IOCTL
77//#define IP2DEBUG_IPL
78
79//#define IP2DEBUG_TRACE
80//#define DEBUG_FIFO
81
82/************/
83/* Includes */
84/************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <linux/ctype.h>
87#include <linux/string.h>
88#include <linux/fcntl.h>
89#include <linux/errno.h>
90#include <linux/module.h>
91#include <linux/signal.h>
92#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/timer.h>
94#include <linux/interrupt.h>
95#include <linux/pci.h>
96#include <linux/mm.h>
97#include <linux/slab.h>
98#include <linux/major.h>
99#include <linux/wait.h>
100#include <linux/device.h>
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600101#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#include <linux/tty.h>
104#include <linux/tty_flip.h>
105#include <linux/termios.h>
106#include <linux/tty_driver.h>
107#include <linux/serial.h>
108#include <linux/ptrace.h>
109#include <linux/ioport.h>
110
111#include <linux/cdk.h>
112#include <linux/comstats.h>
113#include <linux/delay.h>
114#include <linux/bitops.h>
115
116#include <asm/system.h>
117#include <asm/io.h>
118#include <asm/irq.h>
119
120#include <linux/vmalloc.h>
121#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123#include <asm/uaccess.h>
124
Adrian Bunk9c4b5622006-01-19 18:07:10 +0100125#include "ip2types.h"
126#include "ip2trace.h"
127#include "ip2ioctl.h"
128#include "ip2.h"
129#include "i2ellis.h"
130#include "i2lib.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*****************
133 * /proc/ip2mem *
134 *****************/
135
136#include <linux/proc_fs.h>
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -0700137#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -0700139static const struct file_operations ip2mem_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int ip2_read_proc(char *, char **, off_t, int, int *, void * );
141
142/********************/
143/* Type Definitions */
144/********************/
145
146/*************/
147/* Constants */
148/*************/
149
150/* String constants to identify ourselves */
151static char *pcName = "Computone IntelliPort Plus multiport driver";
152static char *pcVersion = "1.2.14";
153
154/* String constants for port names */
155static char *pcDriver_name = "ip2";
156static char *pcIpl = "ip2ipl";
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158// cheezy kludge or genius - you decide?
159int ip2_loadmain(int *, int *, unsigned char *, int);
160static unsigned char *Fip_firmware;
161static int Fip_firmware_size;
162
163/***********************/
164/* Function Prototypes */
165/***********************/
166
167/* Global module entry functions */
168
169/* Private (static) functions */
170static int ip2_open(PTTY, struct file *);
171static void ip2_close(PTTY, struct file *);
Alan Coxd9e39532006-01-09 20:54:20 -0800172static int ip2_write(PTTY, const unsigned char *, int);
Alan Coxf34d7a52008-04-30 00:54:13 -0700173static int ip2_putchar(PTTY, unsigned char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void ip2_flush_chars(PTTY);
175static int ip2_write_room(PTTY);
176static int ip2_chars_in_buf(PTTY);
177static void ip2_flush_buffer(PTTY);
178static int ip2_ioctl(PTTY, struct file *, UINT, ULONG);
Alan Cox606d0992006-12-08 02:38:45 -0800179static void ip2_set_termios(PTTY, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void ip2_set_line_discipline(PTTY);
181static void ip2_throttle(PTTY);
182static void ip2_unthrottle(PTTY);
183static void ip2_stop(PTTY);
184static void ip2_start(PTTY);
185static void ip2_hangup(PTTY);
186static int ip2_tiocmget(struct tty_struct *tty, struct file *file);
187static int ip2_tiocmset(struct tty_struct *tty, struct file *file,
188 unsigned int set, unsigned int clear);
189
190static void set_irq(int, int);
David Howellsc4028952006-11-22 14:57:56 +0000191static void ip2_interrupt_bh(struct work_struct *work);
David Howells7d12e782006-10-05 14:55:46 +0100192static irqreturn_t ip2_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void ip2_poll(unsigned long arg);
194static inline void service_all_boards(void);
David Howellsc4028952006-11-22 14:57:56 +0000195static void do_input(struct work_struct *);
196static void do_status(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static void ip2_wait_until_sent(PTTY,int);
199
Alan Cox606d0992006-12-08 02:38:45 -0800200static void set_params (i2ChanStrPtr, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int get_serial_info(i2ChanStrPtr, struct serial_struct __user *);
202static int set_serial_info(i2ChanStrPtr, struct serial_struct __user *);
203
204static ssize_t ip2_ipl_read(struct file *, char __user *, size_t, loff_t *);
205static ssize_t ip2_ipl_write(struct file *, const char __user *, size_t, loff_t *);
206static int ip2_ipl_ioctl(struct inode *, struct file *, UINT, ULONG);
207static int ip2_ipl_open(struct inode *, struct file *);
208
209static int DumpTraceBuffer(char __user *, int);
210static int DumpFifoBuffer( char __user *, int);
211
212static void ip2_init_board(int);
213static unsigned short find_eisa_board(int);
214
215/***************/
216/* Static Data */
217/***************/
218
219static struct tty_driver *ip2_tty_driver;
220
221/* Here, then is a table of board pointers which the interrupt routine should
222 * scan through to determine who it must service.
223 */
224static unsigned short i2nBoards; // Number of boards here
225
226static i2eBordStrPtr i2BoardPtrTable[IP2_MAX_BOARDS];
227
228static i2ChanStrPtr DevTable[IP2_MAX_PORTS];
229//DevTableMem just used to save addresses for kfree
230static void *DevTableMem[IP2_MAX_BOARDS];
231
232/* This is the driver descriptor for the ip2ipl device, which is used to
233 * download the loadware to the boards.
234 */
Arjan van de Ven62322d22006-07-03 00:24:21 -0700235static const struct file_operations ip2_ipl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .owner = THIS_MODULE,
237 .read = ip2_ipl_read,
238 .write = ip2_ipl_write,
239 .ioctl = ip2_ipl_ioctl,
240 .open = ip2_ipl_open,
241};
242
243static unsigned long irq_counter = 0;
244static unsigned long bh_counter = 0;
245
246// Use immediate queue to service interrupts
247#define USE_IQI
248//#define USE_IQ // PCI&2.2 needs work
249
250/* The timer_list entry for our poll routine. If interrupt operation is not
251 * selected, the board is serviced periodically to see if anything needs doing.
252 */
253#define POLL_TIMEOUT (jiffies + 1)
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700254static DEFINE_TIMER(PollTimer, ip2_poll, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static char TimerOn;
256
257#ifdef IP2DEBUG_TRACE
258/* Trace (debug) buffer data */
259#define TRACEMAX 1000
260static unsigned long tracebuf[TRACEMAX];
261static int tracestuff;
262static int tracestrip;
263static int tracewrap;
264#endif
265
266/**********/
267/* Macros */
268/**********/
269
270#if defined(MODULE) && defined(IP2DEBUG_OPEN)
271#define DBG_CNT(s) printk(KERN_DEBUG "(%s): [%x] refc=%d, ttyc=%d, modc=%x -> %s\n", \
272 tty->name,(pCh->flags),ip2_tty_driver->refcount, \
273 tty->count,/*GET_USE_COUNT(module)*/0,s)
274#else
275#define DBG_CNT(s)
276#endif
277
278/********/
279/* Code */
280/********/
281
Adrian Bunk9c4b5622006-01-19 18:07:10 +0100282#include "i2ellis.c" /* Extremely low-level interface services */
283#include "i2cmd.c" /* Standard loadware command definitions */
284#include "i2lib.c" /* High level interface services */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* Configuration area for modprobe */
287
288MODULE_AUTHOR("Doug McNash");
289MODULE_DESCRIPTION("Computone IntelliPort Plus Driver");
290
291static int poll_only = 0;
292
293static int Eisa_irq;
294static int Eisa_slot;
295
296static int iindx;
297static char rirqs[IP2_MAX_BOARDS];
298static int Valid_Irqs[] = { 3, 4, 5, 7, 10, 11, 12, 15, 0};
299
300/* for sysfs class support */
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800301static struct class *ip2_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303// Some functions to keep track of what irq's we have
304
Randy Dunlap673e3212006-06-25 05:48:39 -0700305static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306is_valid_irq(int irq)
307{
308 int *i = Valid_Irqs;
309
310 while ((*i != 0) && (*i != irq)) {
311 i++;
312 }
313 return (*i);
314}
315
Randy Dunlap673e3212006-06-25 05:48:39 -0700316static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317mark_requested_irq( char irq )
318{
319 rirqs[iindx++] = irq;
320}
321
322#ifdef MODULE
Randy Dunlap673e3212006-06-25 05:48:39 -0700323static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324clear_requested_irq( char irq )
325{
326 int i;
327 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
328 if (rirqs[i] == irq) {
329 rirqs[i] = 0;
330 return 1;
331 }
332 }
333 return 0;
334}
335#endif
336
Randy Dunlap673e3212006-06-25 05:48:39 -0700337static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338have_requested_irq( char irq )
339{
340 // array init to zeros so 0 irq will not be requested as a side effect
341 int i;
342 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
343 if (rirqs[i] == irq)
344 return 1;
345 }
346 return 0;
347}
348
349/******************************************************************************/
350/* Function: init_module() */
351/* Parameters: None */
352/* Returns: Success (0) */
353/* */
354/* Description: */
355/* This is a required entry point for an installable module. It simply calls */
356/* the driver initialisation function and returns what it returns. */
357/******************************************************************************/
358#ifdef MODULE
Jon Schindler83e422b2008-04-30 00:53:53 -0700359static int __init
360ip2_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362#ifdef IP2DEBUG_INIT
363 printk (KERN_DEBUG "Loading module ...\n" );
364#endif
365 return 0;
366}
Jon Schindler83e422b2008-04-30 00:53:53 -0700367module_init(ip2_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#endif /* MODULE */
369
370/******************************************************************************/
371/* Function: cleanup_module() */
372/* Parameters: None */
373/* Returns: Nothing */
374/* */
375/* Description: */
376/* This is a required entry point for an installable module. It has to return */
377/* the device and the driver to a passive state. It should not be necessary */
378/* to reset the board fully, especially as the loadware is downloaded */
379/* externally rather than in the driver. We just want to disable the board */
380/* and clear the loadware to a reset state. To allow this there has to be a */
381/* way to detect whether the board has the loadware running at init time to */
382/* handle subsequent installations of the driver. All memory allocated by the */
383/* driver should be returned since it may be unloaded from memory. */
384/******************************************************************************/
385#ifdef MODULE
Jon Schindler83e422b2008-04-30 00:53:53 -0700386void __exit
387ip2_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 int err;
390 int i;
391
392#ifdef IP2DEBUG_INIT
393 printk (KERN_DEBUG "Unloading %s: version %s\n", pcName, pcVersion );
394#endif
395 /* Stop poll timer if we had one. */
396 if ( TimerOn ) {
397 del_timer ( &PollTimer );
398 TimerOn = 0;
399 }
400
401 /* Reset the boards we have. */
402 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
403 if ( i2BoardPtrTable[i] ) {
404 iiReset( i2BoardPtrTable[i] );
405 }
406 }
407
408 /* The following is done at most once, if any boards were installed. */
409 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
410 if ( i2BoardPtrTable[i] ) {
411 iiResetDelay( i2BoardPtrTable[i] );
412 /* free io addresses and Tibet */
413 release_region( ip2config.addr[i], 8 );
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700414 device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR, 4 * i));
415 device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR, 4 * i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 /* Disable and remove interrupt handler. */
418 if ( (ip2config.irq[i] > 0) && have_requested_irq(ip2config.irq[i]) ) {
419 free_irq ( ip2config.irq[i], (void *)&pcName);
420 clear_requested_irq( ip2config.irq[i]);
421 }
422 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800423 class_destroy(ip2_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if ( ( err = tty_unregister_driver ( ip2_tty_driver ) ) ) {
425 printk(KERN_ERR "IP2: failed to unregister tty driver (%d)\n", err);
426 }
427 put_tty_driver(ip2_tty_driver);
Akinobu Mita68fc4fa2007-07-19 01:47:50 -0700428 unregister_chrdev(IP2_IPL_MAJOR, pcIpl);
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700429 remove_proc_entry("ip2mem", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 // free memory
432 for (i = 0; i < IP2_MAX_BOARDS; i++) {
433 void *pB;
434#ifdef CONFIG_PCI
435 if (ip2config.type[i] == PCI && ip2config.pci_dev[i]) {
436 pci_disable_device(ip2config.pci_dev[i]);
Alan Cox1aff0ec2006-09-30 23:27:59 -0700437 pci_dev_put(ip2config.pci_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ip2config.pci_dev[i] = NULL;
439 }
440#endif
441 if ((pB = i2BoardPtrTable[i]) != 0 ) {
442 kfree ( pB );
443 i2BoardPtrTable[i] = NULL;
444 }
445 if ((DevTableMem[i]) != NULL ) {
446 kfree ( DevTableMem[i] );
447 DevTableMem[i] = NULL;
448 }
449 }
450
451 /* Cleanup the iiEllis subsystem. */
452 iiEllisCleanup();
453#ifdef IP2DEBUG_INIT
454 printk (KERN_DEBUG "IP2 Unloaded\n" );
455#endif
456}
Jon Schindler83e422b2008-04-30 00:53:53 -0700457module_exit(ip2_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif /* MODULE */
459
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700460static const struct tty_operations ip2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .open = ip2_open,
462 .close = ip2_close,
463 .write = ip2_write,
464 .put_char = ip2_putchar,
465 .flush_chars = ip2_flush_chars,
466 .write_room = ip2_write_room,
467 .chars_in_buffer = ip2_chars_in_buf,
468 .flush_buffer = ip2_flush_buffer,
469 .ioctl = ip2_ioctl,
470 .throttle = ip2_throttle,
471 .unthrottle = ip2_unthrottle,
472 .set_termios = ip2_set_termios,
473 .set_ldisc = ip2_set_line_discipline,
474 .stop = ip2_stop,
475 .start = ip2_start,
476 .hangup = ip2_hangup,
477 .read_proc = ip2_read_proc,
478 .tiocmget = ip2_tiocmget,
479 .tiocmset = ip2_tiocmset,
480};
481
482/******************************************************************************/
483/* Function: ip2_loadmain() */
484/* Parameters: irq, io from command line of insmod et. al. */
485/* pointer to fip firmware and firmware size for boards */
486/* Returns: Success (0) */
487/* */
488/* Description: */
489/* This was the required entry point for all drivers (now in ip2.c) */
490/* It performs all */
491/* initialisation of the devices and driver structures, and registers itself */
492/* with the relevant kernel modules. */
493/******************************************************************************/
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700494/* IRQF_DISABLED - if set blocks all interrupts else only this line */
495/* IRQF_SHARED - for shared irq PCI or maybe EISA only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/* SA_RANDOM - can be source for cert. random number generators */
497#define IP2_SA_FLAGS 0
498
499int
500ip2_loadmain(int *iop, int *irqp, unsigned char *firmware, int firmsize)
501{
502 int i, j, box;
503 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 static int loaded;
505 i2eBordStrPtr pB = NULL;
506 int rc = -1;
Alan Cox1aff0ec2006-09-30 23:27:59 -0700507 static struct pci_dev *pci_dev_i = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 ip2trace (ITRC_NO_PORT, ITRC_INIT, ITRC_ENTER, 0 );
510
511 /* process command line arguments to modprobe or
512 insmod i.e. iop & irqp */
513 /* irqp and iop should ALWAYS be specified now... But we check
514 them individually just to be sure, anyways... */
515 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
516 if (iop) {
517 ip2config.addr[i] = iop[i];
518 if (irqp) {
519 if( irqp[i] >= 0 ) {
520 ip2config.irq[i] = irqp[i];
521 } else {
522 ip2config.irq[i] = 0;
523 }
524 // This is a little bit of a hack. If poll_only=1 on command
525 // line back in ip2.c OR all IRQs on all specified boards are
526 // explicitly set to 0, then drop to poll only mode and override
527 // PCI or EISA interrupts. This superceeds the old hack of
528 // triggering if all interrupts were zero (like da default).
529 // Still a hack but less prone to random acts of terrorism.
530 //
531 // What we really should do, now that the IRQ default is set
532 // to -1, is to use 0 as a hard coded, do not probe.
533 //
534 // /\/\|=mhw=|\/\/
535 poll_only |= irqp[i];
536 }
537 }
538 }
539 poll_only = !poll_only;
540
541 Fip_firmware = firmware;
542 Fip_firmware_size = firmsize;
543
544 /* Announce our presence */
545 printk( KERN_INFO "%s version %s\n", pcName, pcVersion );
546
547 // ip2 can be unloaded and reloaded for no good reason
548 // we can't let that happen here or bad things happen
549 // second load hoses board but not system - fixme later
550 if (loaded) {
551 printk( KERN_INFO "Still loaded\n" );
552 return 0;
553 }
554 loaded++;
555
556 ip2_tty_driver = alloc_tty_driver(IP2_MAX_PORTS);
557 if (!ip2_tty_driver)
558 return -ENOMEM;
559
560 /* Initialise the iiEllis subsystem. */
561 iiEllisInit();
562
563 /* Initialize arrays. */
564 memset( i2BoardPtrTable, 0, sizeof i2BoardPtrTable );
565 memset( DevTable, 0, sizeof DevTable );
566
567 /* Initialise all the boards we can find (up to the maximum). */
568 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
569 switch ( ip2config.addr[i] ) {
570 case 0: /* skip this slot even if card is present */
571 break;
572 default: /* ISA */
573 /* ISA address must be specified */
574 if ( (ip2config.addr[i] < 0x100) || (ip2config.addr[i] > 0x3f8) ) {
575 printk ( KERN_ERR "IP2: Bad ISA board %d address %x\n",
576 i, ip2config.addr[i] );
577 ip2config.addr[i] = 0;
578 } else {
579 ip2config.type[i] = ISA;
580
581 /* Check for valid irq argument, set for polling if invalid */
582 if (ip2config.irq[i] && !is_valid_irq(ip2config.irq[i])) {
583 printk(KERN_ERR "IP2: Bad IRQ(%d) specified\n",ip2config.irq[i]);
584 ip2config.irq[i] = 0;// 0 is polling and is valid in that sense
585 }
586 }
587 break;
588 case PCI:
589#ifdef CONFIG_PCI
590 {
Andrew Mortonad4a5bb2007-07-31 00:39:41 -0700591 int status;
592
Alan Cox1aff0ec2006-09-30 23:27:59 -0700593 pci_dev_i = pci_get_device(PCI_VENDOR_ID_COMPUTONE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 PCI_DEVICE_ID_COMPUTONE_IP2EX, pci_dev_i);
595 if (pci_dev_i != NULL) {
596 unsigned int addr;
597
598 if (pci_enable_device(pci_dev_i)) {
599 printk( KERN_ERR "IP2: can't enable PCI device at %s\n",
600 pci_name(pci_dev_i));
601 break;
602 }
603 ip2config.type[i] = PCI;
Alan Cox1aff0ec2006-09-30 23:27:59 -0700604 ip2config.pci_dev[i] = pci_dev_get(pci_dev_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 status =
606 pci_read_config_dword(pci_dev_i, PCI_BASE_ADDRESS_1, &addr);
607 if ( addr & 1 ) {
608 ip2config.addr[i]=(USHORT)(addr&0xfffe);
609 } else {
610 printk( KERN_ERR "IP2: PCI I/O address error\n");
611 }
612
613// If the PCI BIOS assigned it, lets try and use it. If we
614// can't acquire it or it screws up, deal with it then.
615
616// if (!is_valid_irq(pci_irq)) {
617// printk( KERN_ERR "IP2: Bad PCI BIOS IRQ(%d)\n",pci_irq);
618// pci_irq = 0;
619// }
620 ip2config.irq[i] = pci_dev_i->irq;
621 } else { // ann error
622 ip2config.addr[i] = 0;
Jeff Garzik2b0172e2007-10-16 23:30:31 -0700623 printk(KERN_ERR "IP2: PCI board %d not found\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 }
626#else
627 printk( KERN_ERR "IP2: PCI card specified but PCI support not\n");
628 printk( KERN_ERR "IP2: configured in this kernel.\n");
629 printk( KERN_ERR "IP2: Recompile kernel with CONFIG_PCI defined!\n");
630#endif /* CONFIG_PCI */
631 break;
632 case EISA:
633 if ( (ip2config.addr[i] = find_eisa_board( Eisa_slot + 1 )) != 0) {
634 /* Eisa_irq set as side effect, boo */
635 ip2config.type[i] = EISA;
636 }
637 ip2config.irq[i] = Eisa_irq;
638 break;
639 } /* switch */
640 } /* for */
Alan Cox1aff0ec2006-09-30 23:27:59 -0700641 if (pci_dev_i)
642 pci_dev_put(pci_dev_i);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
645 if ( ip2config.addr[i] ) {
Mariusz Kozlowski978550b2007-10-16 23:26:45 -0700646 pB = kzalloc(sizeof(i2eBordStr), GFP_KERNEL);
647 if (pB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 i2BoardPtrTable[i] = pB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 iiSetAddress( pB, ip2config.addr[i], ii2DelayTimer );
650 iiReset( pB );
651 } else {
652 printk(KERN_ERR "IP2: board memory allocation error\n");
653 }
654 }
655 }
656 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
657 if ( ( pB = i2BoardPtrTable[i] ) != NULL ) {
658 iiResetDelay( pB );
659 break;
660 }
661 }
662 for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
663 if ( i2BoardPtrTable[i] != NULL ) {
664 ip2_init_board( i );
665 }
666 }
667
668 ip2trace (ITRC_NO_PORT, ITRC_INIT, 2, 0 );
669
670 ip2_tty_driver->owner = THIS_MODULE;
671 ip2_tty_driver->name = "ttyF";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ip2_tty_driver->driver_name = pcDriver_name;
673 ip2_tty_driver->major = IP2_TTY_MAJOR;
674 ip2_tty_driver->minor_start = 0;
675 ip2_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
676 ip2_tty_driver->subtype = SERIAL_TYPE_NORMAL;
677 ip2_tty_driver->init_termios = tty_std_termios;
678 ip2_tty_driver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700679 ip2_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 tty_set_operations(ip2_tty_driver, &ip2_ops);
681
682 ip2trace (ITRC_NO_PORT, ITRC_INIT, 3, 0 );
683
684 /* Register the tty devices. */
685 if ( ( err = tty_register_driver ( ip2_tty_driver ) ) ) {
686 printk(KERN_ERR "IP2: failed to register tty driver (%d)\n", err);
687 put_tty_driver(ip2_tty_driver);
688 return -EINVAL;
689 } else
690 /* Register the IPL driver. */
691 if ( ( err = register_chrdev ( IP2_IPL_MAJOR, pcIpl, &ip2_ipl ) ) ) {
692 printk(KERN_ERR "IP2: failed to register IPL device (%d)\n", err );
693 } else {
694 /* create the sysfs class */
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800695 ip2_class = class_create(THIS_MODULE, "ip2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (IS_ERR(ip2_class)) {
697 err = PTR_ERR(ip2_class);
698 goto out_chrdev;
699 }
700 }
701 /* Register the read_procmem thing */
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -0700702 if (!proc_create("ip2mem",0,NULL,&ip2mem_proc_fops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 printk(KERN_ERR "IP2: failed to register read_procmem\n");
704 } else {
705
706 ip2trace (ITRC_NO_PORT, ITRC_INIT, 4, 0 );
707 /* Register the interrupt handler or poll handler, depending upon the
708 * specified interrupt.
709 */
710
711 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
712 if ( 0 == ip2config.addr[i] ) {
713 continue;
714 }
715
716 if ( NULL != ( pB = i2BoardPtrTable[i] ) ) {
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700717 device_create(ip2_class, NULL,
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700718 MKDEV(IP2_IPL_MAJOR, 4 * i),
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700719 "ipl%d", i);
720 device_create(ip2_class, NULL,
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700721 MKDEV(IP2_IPL_MAJOR, 4 * i + 1),
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700722 "stat%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 for ( box = 0; box < ABS_MAX_BOXES; ++box )
725 {
726 for ( j = 0; j < ABS_BIGGEST_BOX; ++j )
727 {
728 if ( pB->i2eChannelMap[box] & (1 << j) )
729 {
730 tty_register_device(ip2_tty_driver,
731 j + ABS_BIGGEST_BOX *
732 (box+i*ABS_MAX_BOXES), NULL);
733 }
734 }
735 }
736 }
737
738 if (poll_only) {
739// Poll only forces driver to only use polling and
740// to ignore the probed PCI or EISA interrupts.
741 ip2config.irq[i] = CIR_POLL;
742 }
743 if ( ip2config.irq[i] == CIR_POLL ) {
744retry:
745 if (!TimerOn) {
746 PollTimer.expires = POLL_TIMEOUT;
747 add_timer ( &PollTimer );
748 TimerOn = 1;
749 printk( KERN_INFO "IP2: polling\n");
750 }
751 } else {
752 if (have_requested_irq(ip2config.irq[i]))
753 continue;
754 rc = request_irq( ip2config.irq[i], ip2_interrupt,
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700755 IP2_SA_FLAGS | (ip2config.type[i] == PCI ? IRQF_SHARED : 0),
Jeff Garzik6bd3bd62007-10-19 15:38:40 -0400756 pcName, i2BoardPtrTable[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (rc) {
758 printk(KERN_ERR "IP2: an request_irq failed: error %d\n",rc);
759 ip2config.irq[i] = CIR_POLL;
760 printk( KERN_INFO "IP2: Polling %ld/sec.\n",
761 (POLL_TIMEOUT - jiffies));
762 goto retry;
763 }
764 mark_requested_irq(ip2config.irq[i]);
765 /* Initialise the interrupt handler bottom half (aka slih). */
766 }
767 }
768 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
769 if ( i2BoardPtrTable[i] ) {
770 set_irq( i, ip2config.irq[i] ); /* set and enable board interrupt */
771 }
772 }
773 }
774 ip2trace (ITRC_NO_PORT, ITRC_INIT, ITRC_RETURN, 0 );
775 goto out;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777out_chrdev:
778 unregister_chrdev(IP2_IPL_MAJOR, "ip2");
779out:
780 return err;
781}
782
783EXPORT_SYMBOL(ip2_loadmain);
784
785/******************************************************************************/
786/* Function: ip2_init_board() */
787/* Parameters: Index of board in configuration structure */
788/* Returns: Success (0) */
789/* */
790/* Description: */
791/* This function initializes the specified board. The loadware is copied to */
792/* the board, the channel structures are initialized, and the board details */
793/* are reported on the console. */
794/******************************************************************************/
Randy Dunlap673e3212006-06-25 05:48:39 -0700795static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796ip2_init_board( int boardnum )
797{
798 int i;
799 int nports = 0, nboxes = 0;
800 i2ChanStrPtr pCh;
801 i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
802
803 if ( !iiInitialize ( pB ) ) {
804 printk ( KERN_ERR "IP2: Failed to initialize board at 0x%x, error %d\n",
805 pB->i2eBase, pB->i2eError );
806 goto err_initialize;
807 }
808 printk(KERN_INFO "IP2: Board %d: addr=0x%x irq=%d\n", boardnum + 1,
809 ip2config.addr[boardnum], ip2config.irq[boardnum] );
810
811 if (!request_region( ip2config.addr[boardnum], 8, pcName )) {
812 printk(KERN_ERR "IP2: bad addr=0x%x\n", ip2config.addr[boardnum]);
813 goto err_initialize;
814 }
815
816 if ( iiDownloadAll ( pB, (loadHdrStrPtr)Fip_firmware, 1, Fip_firmware_size )
817 != II_DOWN_GOOD ) {
818 printk ( KERN_ERR "IP2: failed to download loadware\n" );
819 goto err_release_region;
820 } else {
821 printk ( KERN_INFO "IP2: fv=%d.%d.%d lv=%d.%d.%d\n",
822 pB->i2ePom.e.porVersion,
823 pB->i2ePom.e.porRevision,
824 pB->i2ePom.e.porSubRev, pB->i2eLVersion,
825 pB->i2eLRevision, pB->i2eLSub );
826 }
827
828 switch ( pB->i2ePom.e.porID & ~POR_ID_RESERVED ) {
829
830 default:
831 printk( KERN_ERR "IP2: Unknown board type, ID = %x\n",
832 pB->i2ePom.e.porID );
833 nports = 0;
834 goto err_release_region;
835 break;
836
837 case POR_ID_II_4: /* IntelliPort-II, ISA-4 (4xRJ45) */
838 printk ( KERN_INFO "IP2: ISA-4\n" );
839 nports = 4;
840 break;
841
842 case POR_ID_II_8: /* IntelliPort-II, 8-port using standard brick. */
843 printk ( KERN_INFO "IP2: ISA-8 std\n" );
844 nports = 8;
845 break;
846
847 case POR_ID_II_8R: /* IntelliPort-II, 8-port using RJ11's (no CTS) */
848 printk ( KERN_INFO "IP2: ISA-8 RJ11\n" );
849 nports = 8;
850 break;
851
852 case POR_ID_FIIEX: /* IntelliPort IIEX */
853 {
854 int portnum = IP2_PORTS_PER_BOARD * boardnum;
855 int box;
856
857 for( box = 0; box < ABS_MAX_BOXES; ++box ) {
858 if ( pB->i2eChannelMap[box] != 0 ) {
859 ++nboxes;
860 }
861 for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
862 if ( pB->i2eChannelMap[box] & 1<< i ) {
863 ++nports;
864 }
865 }
866 }
867 DevTableMem[boardnum] = pCh =
868 kmalloc( sizeof(i2ChanStr) * nports, GFP_KERNEL );
869 if ( !pCh ) {
870 printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
871 goto err_release_region;
872 }
873 if ( !i2InitChannels( pB, nports, pCh ) ) {
874 printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
875 kfree ( pCh );
876 goto err_release_region;
877 }
878 pB->i2eChannelPtr = &DevTable[portnum];
879 pB->i2eChannelCnt = ABS_MOST_PORTS;
880
881 for( box = 0; box < ABS_MAX_BOXES; ++box, portnum += ABS_BIGGEST_BOX ) {
882 for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
883 if ( pB->i2eChannelMap[box] & (1 << i) ) {
884 DevTable[portnum + i] = pCh;
885 pCh->port_index = portnum + i;
886 pCh++;
887 }
888 }
889 }
890 printk(KERN_INFO "IP2: EX box=%d ports=%d %d bit\n",
891 nboxes, nports, pB->i2eDataWidth16 ? 16 : 8 );
892 }
893 goto ex_exit;
894 }
895 DevTableMem[boardnum] = pCh =
896 kmalloc ( sizeof (i2ChanStr) * nports, GFP_KERNEL );
897 if ( !pCh ) {
898 printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
899 goto err_release_region;
900 }
901 pB->i2eChannelPtr = pCh;
902 pB->i2eChannelCnt = nports;
903 if ( !i2InitChannels( pB, nports, pCh ) ) {
904 printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
905 kfree ( pCh );
906 goto err_release_region;
907 }
908 pB->i2eChannelPtr = &DevTable[IP2_PORTS_PER_BOARD * boardnum];
909
910 for( i = 0; i < pB->i2eChannelCnt; ++i ) {
911 DevTable[IP2_PORTS_PER_BOARD * boardnum + i] = pCh;
912 pCh->port_index = (IP2_PORTS_PER_BOARD * boardnum) + i;
913 pCh++;
914 }
915ex_exit:
David Howellsc4028952006-11-22 14:57:56 +0000916 INIT_WORK(&pB->tqueue_interrupt, ip2_interrupt_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return;
918
919err_release_region:
920 release_region(ip2config.addr[boardnum], 8);
921err_initialize:
922 kfree ( pB );
923 i2BoardPtrTable[boardnum] = NULL;
924 return;
925}
926
927/******************************************************************************/
928/* Function: find_eisa_board ( int start_slot ) */
929/* Parameters: First slot to check */
930/* Returns: Address of EISA IntelliPort II controller */
931/* */
932/* Description: */
933/* This function searches for an EISA IntelliPort controller, starting */
934/* from the specified slot number. If the motherboard is not identified as an */
935/* EISA motherboard, or no valid board ID is selected it returns 0. Otherwise */
936/* it returns the base address of the controller. */
937/******************************************************************************/
Randy Dunlap673e3212006-06-25 05:48:39 -0700938static unsigned short
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939find_eisa_board( int start_slot )
940{
941 int i, j;
942 unsigned int idm = 0;
943 unsigned int idp = 0;
944 unsigned int base = 0;
945 unsigned int value;
946 int setup_address;
947 int setup_irq;
948 int ismine = 0;
949
950 /*
951 * First a check for an EISA motherboard, which we do by comparing the
952 * EISA ID registers for the system board and the first couple of slots.
953 * No slot ID should match the system board ID, but on an ISA or PCI
954 * machine the odds are that an empty bus will return similar values for
955 * each slot.
956 */
957 i = 0x0c80;
958 value = (inb(i) << 24) + (inb(i+1) << 16) + (inb(i+2) << 8) + inb(i+3);
959 for( i = 0x1c80; i <= 0x4c80; i += 0x1000 ) {
960 j = (inb(i)<<24)+(inb(i+1)<<16)+(inb(i+2)<<8)+inb(i+3);
961 if ( value == j )
962 return 0;
963 }
964
965 /*
966 * OK, so we are inclined to believe that this is an EISA machine. Find
967 * an IntelliPort controller.
968 */
969 for( i = start_slot; i < 16; i++ ) {
970 base = i << 12;
971 idm = (inb(base + 0xc80) << 8) | (inb(base + 0xc81) & 0xff);
972 idp = (inb(base + 0xc82) << 8) | (inb(base + 0xc83) & 0xff);
973 ismine = 0;
974 if ( idm == 0x0e8e ) {
975 if ( idp == 0x0281 || idp == 0x0218 ) {
976 ismine = 1;
977 } else if ( idp == 0x0282 || idp == 0x0283 ) {
978 ismine = 3; /* Can do edge-trigger */
979 }
980 if ( ismine ) {
981 Eisa_slot = i;
982 break;
983 }
984 }
985 }
986 if ( !ismine )
987 return 0;
988
989 /* It's some sort of EISA card, but at what address is it configured? */
990
991 setup_address = base + 0xc88;
992 value = inb(base + 0xc86);
993 setup_irq = (value & 8) ? Valid_Irqs[value & 7] : 0;
994
995 if ( (ismine & 2) && !(value & 0x10) ) {
996 ismine = 1; /* Could be edging, but not */
997 }
998
999 if ( Eisa_irq == 0 ) {
1000 Eisa_irq = setup_irq;
1001 } else if ( Eisa_irq != setup_irq ) {
1002 printk ( KERN_ERR "IP2: EISA irq mismatch between EISA controllers\n" );
1003 }
1004
1005#ifdef IP2DEBUG_INIT
1006printk(KERN_DEBUG "Computone EISA board in slot %d, I.D. 0x%x%x, Address 0x%x",
1007 base >> 12, idm, idp, setup_address);
1008 if ( Eisa_irq ) {
1009 printk(KERN_DEBUG ", Interrupt %d %s\n",
1010 setup_irq, (ismine & 2) ? "(edge)" : "(level)");
1011 } else {
1012 printk(KERN_DEBUG ", (polled)\n");
1013 }
1014#endif
1015 return setup_address;
1016}
1017
1018/******************************************************************************/
1019/* Function: set_irq() */
1020/* Parameters: index to board in board table */
1021/* IRQ to use */
1022/* Returns: Success (0) */
1023/* */
1024/* Description: */
1025/******************************************************************************/
1026static void
1027set_irq( int boardnum, int boardIrq )
1028{
1029 unsigned char tempCommand[16];
1030 i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
1031 unsigned long flags;
1032
1033 /*
1034 * Notify the boards they may generate interrupts. This is done by
1035 * sending an in-line command to channel 0 on each board. This is why
1036 * the channels have to be defined already. For each board, if the
1037 * interrupt has never been defined, we must do so NOW, directly, since
1038 * board will not send flow control or even give an interrupt until this
1039 * is done. If polling we must send 0 as the interrupt parameter.
1040 */
1041
1042 // We will get an interrupt here at the end of this function
1043
1044 iiDisableMailIrq(pB);
1045
1046 /* We build up the entire packet header. */
1047 CHANNEL_OF(tempCommand) = 0;
1048 PTYPE_OF(tempCommand) = PTYPE_INLINE;
1049 CMD_COUNT_OF(tempCommand) = 2;
1050 (CMD_OF(tempCommand))[0] = CMDVALUE_IRQ;
1051 (CMD_OF(tempCommand))[1] = boardIrq;
1052 /*
1053 * Write to FIFO; don't bother to adjust fifo capacity for this, since
1054 * board will respond almost immediately after SendMail hit.
1055 */
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001056 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 iiWriteBuf(pB, tempCommand, 4);
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001058 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 pB->i2eUsingIrq = boardIrq;
1060 pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
1061
1062 /* Need to update number of boards before you enable mailbox int */
1063 ++i2nBoards;
1064
1065 CHANNEL_OF(tempCommand) = 0;
1066 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1067 CMD_COUNT_OF(tempCommand) = 6;
1068 (CMD_OF(tempCommand))[0] = 88; // SILO
1069 (CMD_OF(tempCommand))[1] = 64; // chars
1070 (CMD_OF(tempCommand))[2] = 32; // ms
1071
1072 (CMD_OF(tempCommand))[3] = 28; // MAX_BLOCK
1073 (CMD_OF(tempCommand))[4] = 64; // chars
1074
1075 (CMD_OF(tempCommand))[5] = 87; // HW_TEST
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001076 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 iiWriteBuf(pB, tempCommand, 8);
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001078 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 CHANNEL_OF(tempCommand) = 0;
1081 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1082 CMD_COUNT_OF(tempCommand) = 1;
1083 (CMD_OF(tempCommand))[0] = 84; /* get BOX_IDS */
1084 iiWriteBuf(pB, tempCommand, 3);
1085
1086#ifdef XXX
1087 // enable heartbeat for test porpoises
1088 CHANNEL_OF(tempCommand) = 0;
1089 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1090 CMD_COUNT_OF(tempCommand) = 2;
1091 (CMD_OF(tempCommand))[0] = 44; /* get ping */
1092 (CMD_OF(tempCommand))[1] = 200; /* 200 ms */
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001093 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 iiWriteBuf(pB, tempCommand, 4);
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001095 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#endif
1097
1098 iiEnableMailIrq(pB);
1099 iiSendPendingMail(pB);
1100}
1101
1102/******************************************************************************/
1103/* Interrupt Handler Section */
1104/******************************************************************************/
1105
1106static inline void
1107service_all_boards(void)
1108{
1109 int i;
1110 i2eBordStrPtr pB;
1111
1112 /* Service every board on the list */
1113 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
1114 pB = i2BoardPtrTable[i];
1115 if ( pB ) {
1116 i2ServiceBoard( pB );
1117 }
1118 }
1119}
1120
1121
1122/******************************************************************************/
David Howellsc4028952006-11-22 14:57:56 +00001123/* Function: ip2_interrupt_bh(work) */
1124/* Parameters: work - pointer to the board structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* Returns: Nothing */
1126/* */
1127/* Description: */
1128/* Service the board in a bottom half interrupt handler and then */
1129/* reenable the board's interrupts if it has an IRQ number */
1130/* */
1131/******************************************************************************/
1132static void
David Howellsc4028952006-11-22 14:57:56 +00001133ip2_interrupt_bh(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
David Howellsc4028952006-11-22 14:57:56 +00001135 i2eBordStrPtr pB = container_of(work, i2eBordStr, tqueue_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136// pB better well be set or we have a problem! We can only get
1137// here from the IMMEDIATE queue. Here, we process the boards.
1138// Checking pB doesn't cost much and it saves us from the sanity checkers.
1139
1140 bh_counter++;
1141
1142 if ( pB ) {
1143 i2ServiceBoard( pB );
1144 if( pB->i2eUsingIrq ) {
1145// Re-enable his interrupts
1146 iiEnableMailIrq(pB);
1147 }
1148 }
1149}
1150
1151
1152/******************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +01001153/* Function: ip2_interrupt(int irq, void *dev_id) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* Parameters: irq - interrupt number */
1155/* pointer to optional device ID structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/* Returns: Nothing */
1157/* */
1158/* Description: */
1159/* */
1160/* Our task here is simply to identify each board which needs servicing. */
1161/* If we are queuing then, queue it to be serviced, and disable its irq */
1162/* mask otherwise process the board directly. */
1163/* */
1164/* We could queue by IRQ but that just complicates things on both ends */
1165/* with very little gain in performance (how many instructions does */
1166/* it take to iterate on the immediate queue). */
1167/* */
1168/* */
1169/******************************************************************************/
Jeff Garzikf3518e42007-10-19 15:24:59 -04001170static void
1171ip2_irq_work(i2eBordStrPtr pB)
1172{
1173#ifdef USE_IQI
1174 if (NO_MAIL_HERE != ( pB->i2eStartMail = iiGetMail(pB))) {
1175// Disable his interrupt (will be enabled when serviced)
1176// This is mostly to protect from reentrancy.
1177 iiDisableMailIrq(pB);
1178
1179// Park the board on the immediate queue for processing.
1180 schedule_work(&pB->tqueue_interrupt);
1181
1182// Make sure the immediate queue is flagged to fire.
1183 }
1184#else
1185
1186// We are using immediate servicing here. This sucks and can
1187// cause all sorts of havoc with ppp and others. The failsafe
1188// check on iiSendPendingMail could also throw a hairball.
1189
1190 i2ServiceBoard( pB );
1191
1192#endif /* USE_IQI */
1193}
1194
Jeff Garzik6bd3bd62007-10-19 15:38:40 -04001195static void
1196ip2_polled_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 int i;
1199 i2eBordStrPtr pB;
Jeff Garzik6bd3bd62007-10-19 15:38:40 -04001200 const int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 ip2trace (ITRC_NO_PORT, ITRC_INTR, 99, 1, irq );
1203
1204 /* Service just the boards on the list using this irq */
1205 for( i = 0; i < i2nBoards; ++i ) {
1206 pB = i2BoardPtrTable[i];
1207
1208// Only process those boards which match our IRQ.
1209// IRQ = 0 for polled boards, we won't poll "IRQ" boards
1210
1211 if ( pB && (pB->i2eUsingIrq == irq) ) {
Jeff Garzikf3518e42007-10-19 15:24:59 -04001212 ip2_irq_work(pB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 }
1215
1216 ++irq_counter;
1217
1218 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
Jeff Garzik6bd3bd62007-10-19 15:38:40 -04001219}
1220
1221static irqreturn_t
1222ip2_interrupt(int irq, void *dev_id)
1223{
1224 i2eBordStrPtr pB = dev_id;
1225
1226 ip2trace (ITRC_NO_PORT, ITRC_INTR, 99, 1, pB->i2eUsingIrq );
1227
1228 ip2_irq_work(pB);
1229
1230 ++irq_counter;
1231
1232 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1233 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/******************************************************************************/
1237/* Function: ip2_poll(unsigned long arg) */
1238/* Parameters: ? */
1239/* Returns: Nothing */
1240/* */
1241/* Description: */
1242/* This function calls the library routine i2ServiceBoard for each board in */
1243/* the board table. This is used instead of the interrupt routine when polled */
1244/* mode is specified. */
1245/******************************************************************************/
1246static void
1247ip2_poll(unsigned long arg)
1248{
1249 ip2trace (ITRC_NO_PORT, ITRC_INTR, 100, 0 );
1250
1251 TimerOn = 0; // it's the truth but not checked in service
1252
1253 // Just polled boards, IRQ = 0 will hit all non-interrupt boards.
1254 // It will NOT poll boards handled by hard interrupts.
Joe Perches8dfba4d2008-02-03 17:11:42 +02001255 // The issue of queued BH interrupts is handled in ip2_interrupt().
Jeff Garzik6bd3bd62007-10-19 15:38:40 -04001256 ip2_polled_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 PollTimer.expires = POLL_TIMEOUT;
1259 add_timer( &PollTimer );
1260 TimerOn = 1;
1261
1262 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1263}
1264
David Howellsc4028952006-11-22 14:57:56 +00001265static void do_input(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
David Howellsc4028952006-11-22 14:57:56 +00001267 i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned long flags;
1269
1270 ip2trace(CHANN, ITRC_INPUT, 21, 0 );
1271
1272 // Data input
1273 if ( pCh->pTTY != NULL ) {
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001274 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (!pCh->throttled && (pCh->Ibuf_stuff != pCh->Ibuf_strip)) {
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001276 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 i2Input( pCh );
1278 } else
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001279 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
1281 ip2trace(CHANN, ITRC_INPUT, 22, 0 );
1282
1283 i2InputFlush( pCh );
1284 }
1285}
1286
1287// code duplicated from n_tty (ldisc)
1288static inline void isig(int sig, struct tty_struct *tty, int flush)
1289{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001290 if (tty->pgrp)
1291 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (flush || !L_NOFLSH(tty)) {
1293 if ( tty->ldisc.flush_buffer )
1294 tty->ldisc.flush_buffer(tty);
1295 i2InputFlush( tty->driver_data );
1296 }
1297}
1298
David Howellsc4028952006-11-22 14:57:56 +00001299static void do_status(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
David Howellsc4028952006-11-22 14:57:56 +00001301 i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int status;
1303
1304 status = i2GetStatus( pCh, (I2_BRK|I2_PAR|I2_FRA|I2_OVR) );
1305
1306 ip2trace (CHANN, ITRC_STATUS, 21, 1, status );
1307
1308 if (pCh->pTTY && (status & (I2_BRK|I2_PAR|I2_FRA|I2_OVR)) ) {
1309 if ( (status & I2_BRK) ) {
1310 // code duplicated from n_tty (ldisc)
1311 if (I_IGNBRK(pCh->pTTY))
1312 goto skip_this;
1313 if (I_BRKINT(pCh->pTTY)) {
1314 isig(SIGINT, pCh->pTTY, 1);
1315 goto skip_this;
1316 }
1317 wake_up_interruptible(&pCh->pTTY->read_wait);
1318 }
1319#ifdef NEVER_HAPPENS_AS_SETUP_XXX
1320 // and can't work because we don't know the_char
1321 // as the_char is reported on a separate path
1322 // The intelligent board does this stuff as setup
1323 {
1324 char brkf = TTY_NORMAL;
1325 unsigned char brkc = '\0';
1326 unsigned char tmp;
1327 if ( (status & I2_BRK) ) {
1328 brkf = TTY_BREAK;
1329 brkc = '\0';
1330 }
1331 else if (status & I2_PAR) {
1332 brkf = TTY_PARITY;
1333 brkc = the_char;
1334 } else if (status & I2_FRA) {
1335 brkf = TTY_FRAME;
1336 brkc = the_char;
1337 } else if (status & I2_OVR) {
1338 brkf = TTY_OVERRUN;
1339 brkc = the_char;
1340 }
1341 tmp = pCh->pTTY->real_raw;
1342 pCh->pTTY->real_raw = 0;
1343 pCh->pTTY->ldisc.receive_buf( pCh->pTTY, &brkc, &brkf, 1 );
1344 pCh->pTTY->real_raw = tmp;
1345 }
1346#endif /* NEVER_HAPPENS_AS_SETUP_XXX */
1347 }
1348skip_this:
1349
1350 if ( status & (I2_DDCD | I2_DDSR | I2_DCTS | I2_DRI) ) {
1351 wake_up_interruptible(&pCh->delta_msr_wait);
1352
1353 if ( (pCh->flags & ASYNC_CHECK_CD) && (status & I2_DDCD) ) {
1354 if ( status & I2_DCD ) {
1355 if ( pCh->wopen ) {
1356 wake_up_interruptible ( &pCh->open_wait );
1357 }
1358 } else {
1359 if (pCh->pTTY && (!(pCh->pTTY->termios->c_cflag & CLOCAL)) ) {
1360 tty_hangup( pCh->pTTY );
1361 }
1362 }
1363 }
1364 }
1365
1366 ip2trace (CHANN, ITRC_STATUS, 26, 0 );
1367}
1368
1369/******************************************************************************/
1370/* Device Open/Close/Ioctl Entry Point Section */
1371/******************************************************************************/
1372
1373/******************************************************************************/
1374/* Function: open_sanity_check() */
1375/* Parameters: Pointer to tty structure */
1376/* Pointer to file structure */
1377/* Returns: Success or failure */
1378/* */
1379/* Description: */
1380/* Verifies the structure magic numbers and cross links. */
1381/******************************************************************************/
1382#ifdef IP2DEBUG_OPEN
1383static void
1384open_sanity_check( i2ChanStrPtr pCh, i2eBordStrPtr pBrd )
1385{
1386 if ( pBrd->i2eValid != I2E_MAGIC ) {
1387 printk(KERN_ERR "IP2: invalid board structure\n" );
1388 } else if ( pBrd != pCh->pMyBord ) {
1389 printk(KERN_ERR "IP2: board structure pointer mismatch (%p)\n",
1390 pCh->pMyBord );
1391 } else if ( pBrd->i2eChannelCnt < pCh->port_index ) {
1392 printk(KERN_ERR "IP2: bad device index (%d)\n", pCh->port_index );
1393 } else if (&((i2ChanStrPtr)pBrd->i2eChannelPtr)[pCh->port_index] != pCh) {
1394 } else {
1395 printk(KERN_INFO "IP2: all pointers check out!\n" );
1396 }
1397}
1398#endif
1399
1400
1401/******************************************************************************/
1402/* Function: ip2_open() */
1403/* Parameters: Pointer to tty structure */
1404/* Pointer to file structure */
1405/* Returns: Success or failure */
1406/* */
1407/* Description: (MANDATORY) */
1408/* A successful device open has to run a gauntlet of checks before it */
1409/* completes. After some sanity checking and pointer setup, the function */
1410/* blocks until all conditions are satisfied. It then initialises the port to */
1411/* the default characteristics and returns. */
1412/******************************************************************************/
1413static int
1414ip2_open( PTTY tty, struct file *pFile )
1415{
1416 wait_queue_t wait;
1417 int rc = 0;
1418 int do_clocal = 0;
1419 i2ChanStrPtr pCh = DevTable[tty->index];
1420
1421 ip2trace (tty->index, ITRC_OPEN, ITRC_ENTER, 0 );
1422
1423 if ( pCh == NULL ) {
1424 return -ENODEV;
1425 }
1426 /* Setup pointer links in device and tty structures */
1427 pCh->pTTY = tty;
1428 tty->driver_data = pCh;
1429
1430#ifdef IP2DEBUG_OPEN
1431 printk(KERN_DEBUG \
1432 "IP2:open(tty=%p,pFile=%p):dev=%s,ch=%d,idx=%d\n",
1433 tty, pFile, tty->name, pCh->infl.hd.i2sChannel, pCh->port_index);
1434 open_sanity_check ( pCh, pCh->pMyBord );
1435#endif
1436
1437 i2QueueCommands(PTYPE_INLINE, pCh, 100, 3, CMD_DTRUP,CMD_RTSUP,CMD_DCD_REP);
1438 pCh->dataSetOut |= (I2_DTR | I2_RTS);
1439 serviceOutgoingFifo( pCh->pMyBord );
1440
1441 /* Block here until the port is ready (per serial and istallion) */
1442 /*
1443 * 1. If the port is in the middle of closing wait for the completion
1444 * and then return the appropriate error.
1445 */
1446 init_waitqueue_entry(&wait, current);
1447 add_wait_queue(&pCh->close_wait, &wait);
1448 set_current_state( TASK_INTERRUPTIBLE );
1449
1450 if ( tty_hung_up_p(pFile) || ( pCh->flags & ASYNC_CLOSING )) {
1451 if ( pCh->flags & ASYNC_CLOSING ) {
1452 schedule();
1453 }
1454 if ( tty_hung_up_p(pFile) ) {
1455 set_current_state( TASK_RUNNING );
1456 remove_wait_queue(&pCh->close_wait, &wait);
1457 return( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS;
1458 }
1459 }
1460 set_current_state( TASK_RUNNING );
1461 remove_wait_queue(&pCh->close_wait, &wait);
1462
1463 /*
1464 * 3. Handle a non-blocking open of a normal port.
1465 */
1466 if ( (pFile->f_flags & O_NONBLOCK) || (tty->flags & (1<<TTY_IO_ERROR) )) {
1467 pCh->flags |= ASYNC_NORMAL_ACTIVE;
1468 goto noblock;
1469 }
1470 /*
1471 * 4. Now loop waiting for the port to be free and carrier present
1472 * (if required).
1473 */
1474 if ( tty->termios->c_cflag & CLOCAL )
1475 do_clocal = 1;
1476
1477#ifdef IP2DEBUG_OPEN
1478 printk(KERN_DEBUG "OpenBlock: do_clocal = %d\n", do_clocal);
1479#endif
1480
1481 ++pCh->wopen;
1482
1483 init_waitqueue_entry(&wait, current);
1484 add_wait_queue(&pCh->open_wait, &wait);
1485
1486 for(;;) {
1487 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
1488 pCh->dataSetOut |= (I2_DTR | I2_RTS);
1489 set_current_state( TASK_INTERRUPTIBLE );
1490 serviceOutgoingFifo( pCh->pMyBord );
1491 if ( tty_hung_up_p(pFile) ) {
1492 set_current_state( TASK_RUNNING );
1493 remove_wait_queue(&pCh->open_wait, &wait);
1494 return ( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EBUSY : -ERESTARTSYS;
1495 }
1496 if (!(pCh->flags & ASYNC_CLOSING) &&
1497 (do_clocal || (pCh->dataSetIn & I2_DCD) )) {
1498 rc = 0;
1499 break;
1500 }
1501
1502#ifdef IP2DEBUG_OPEN
1503 printk(KERN_DEBUG "ASYNC_CLOSING = %s\n",
1504 (pCh->flags & ASYNC_CLOSING)?"True":"False");
1505 printk(KERN_DEBUG "OpenBlock: waiting for CD or signal\n");
1506#endif
1507 ip2trace (CHANN, ITRC_OPEN, 3, 2, 0,
1508 (pCh->flags & ASYNC_CLOSING) );
1509 /* check for signal */
1510 if (signal_pending(current)) {
1511 rc = (( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS);
1512 break;
1513 }
1514 schedule();
1515 }
1516 set_current_state( TASK_RUNNING );
1517 remove_wait_queue(&pCh->open_wait, &wait);
1518
1519 --pCh->wopen; //why count?
1520
1521 ip2trace (CHANN, ITRC_OPEN, 4, 0 );
1522
1523 if (rc != 0 ) {
1524 return rc;
1525 }
1526 pCh->flags |= ASYNC_NORMAL_ACTIVE;
1527
1528noblock:
1529
1530 /* first open - Assign termios structure to port */
1531 if ( tty->count == 1 ) {
1532 i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1533 /* Now we must send the termios settings to the loadware */
1534 set_params( pCh, NULL );
1535 }
1536
1537 /*
1538 * Now set any i2lib options. These may go away if the i2lib code ends
1539 * up rolled into the mainline.
1540 */
1541 pCh->channelOptions |= CO_NBLOCK_WRITE;
1542
1543#ifdef IP2DEBUG_OPEN
1544 printk (KERN_DEBUG "IP2: open completed\n" );
1545#endif
1546 serviceOutgoingFifo( pCh->pMyBord );
1547
1548 ip2trace (CHANN, ITRC_OPEN, ITRC_RETURN, 0 );
1549
1550 return 0;
1551}
1552
1553/******************************************************************************/
1554/* Function: ip2_close() */
1555/* Parameters: Pointer to tty structure */
1556/* Pointer to file structure */
1557/* Returns: Nothing */
1558/* */
1559/* Description: */
1560/* */
1561/* */
1562/******************************************************************************/
1563static void
1564ip2_close( PTTY tty, struct file *pFile )
1565{
1566 i2ChanStrPtr pCh = tty->driver_data;
1567
1568 if ( !pCh ) {
1569 return;
1570 }
1571
1572 ip2trace (CHANN, ITRC_CLOSE, ITRC_ENTER, 0 );
1573
1574#ifdef IP2DEBUG_OPEN
1575 printk(KERN_DEBUG "IP2:close %s:\n",tty->name);
1576#endif
1577
1578 if ( tty_hung_up_p ( pFile ) ) {
1579
1580 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 2 );
1581
1582 return;
1583 }
1584 if ( tty->count > 1 ) { /* not the last close */
1585
1586 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 3 );
1587
1588 return;
1589 }
1590 pCh->flags |= ASYNC_CLOSING; // last close actually
1591
1592 tty->closing = 1;
1593
1594 if (pCh->ClosingWaitTime != ASYNC_CLOSING_WAIT_NONE) {
1595 /*
1596 * Before we drop DTR, make sure the transmitter has completely drained.
1597 * This uses an timeout, after which the close
1598 * completes.
1599 */
1600 ip2_wait_until_sent(tty, pCh->ClosingWaitTime );
1601 }
1602 /*
1603 * At this point we stop accepting input. Here we flush the channel
1604 * input buffer which will allow the board to send up more data. Any
1605 * additional input is tossed at interrupt/poll time.
1606 */
1607 i2InputFlush( pCh );
1608
1609 /* disable DSS reporting */
1610 i2QueueCommands(PTYPE_INLINE, pCh, 100, 4,
1611 CMD_DCD_NREP, CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1612 if ( !tty || (tty->termios->c_cflag & HUPCL) ) {
1613 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
1614 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1615 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1616 }
1617
1618 serviceOutgoingFifo ( pCh->pMyBord );
1619
Alan Coxf34d7a52008-04-30 00:54:13 -07001620 tty_ldisc_flush(tty);
Alan Coxa6fc8192008-04-30 00:54:18 -07001621 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 tty->closing = 0;
1623
1624 pCh->pTTY = NULL;
1625
1626 if (pCh->wopen) {
1627 if (pCh->ClosingDelay) {
1628 msleep_interruptible(jiffies_to_msecs(pCh->ClosingDelay));
1629 }
1630 wake_up_interruptible(&pCh->open_wait);
1631 }
1632
1633 pCh->flags &=~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1634 wake_up_interruptible(&pCh->close_wait);
1635
1636#ifdef IP2DEBUG_OPEN
1637 DBG_CNT("ip2_close: after wakeups--");
1638#endif
1639
1640
1641 ip2trace (CHANN, ITRC_CLOSE, ITRC_RETURN, 1, 1 );
1642
1643 return;
1644}
1645
1646/******************************************************************************/
1647/* Function: ip2_hangup() */
1648/* Parameters: Pointer to tty structure */
1649/* Returns: Nothing */
1650/* */
1651/* Description: */
1652/* */
1653/* */
1654/******************************************************************************/
1655static void
1656ip2_hangup ( PTTY tty )
1657{
1658 i2ChanStrPtr pCh = tty->driver_data;
1659
1660 if( !pCh ) {
1661 return;
1662 }
1663
1664 ip2trace (CHANN, ITRC_HANGUP, ITRC_ENTER, 0 );
1665
1666 ip2_flush_buffer(tty);
1667
1668 /* disable DSS reporting */
1669
1670 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_DCD_NREP);
1671 i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1672 if ( (tty->termios->c_cflag & HUPCL) ) {
1673 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 2, CMD_RTSDN, CMD_DTRDN);
1674 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1675 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1676 }
1677 i2QueueCommands(PTYPE_INLINE, pCh, 1, 3,
1678 CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1679 serviceOutgoingFifo ( pCh->pMyBord );
1680
1681 wake_up_interruptible ( &pCh->delta_msr_wait );
1682
1683 pCh->flags &= ~ASYNC_NORMAL_ACTIVE;
1684 pCh->pTTY = NULL;
1685 wake_up_interruptible ( &pCh->open_wait );
1686
1687 ip2trace (CHANN, ITRC_HANGUP, ITRC_RETURN, 0 );
1688}
1689
1690/******************************************************************************/
1691/******************************************************************************/
1692/* Device Output Section */
1693/******************************************************************************/
1694/******************************************************************************/
1695
1696/******************************************************************************/
1697/* Function: ip2_write() */
1698/* Parameters: Pointer to tty structure */
1699/* Flag denoting data is in user (1) or kernel (0) space */
1700/* Pointer to data */
1701/* Number of bytes to write */
1702/* Returns: Number of bytes actually written */
1703/* */
1704/* Description: (MANDATORY) */
1705/* */
1706/* */
1707/******************************************************************************/
1708static int
Alan Coxd9e39532006-01-09 20:54:20 -08001709ip2_write( PTTY tty, const unsigned char *pData, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 i2ChanStrPtr pCh = tty->driver_data;
1712 int bytesSent = 0;
1713 unsigned long flags;
1714
1715 ip2trace (CHANN, ITRC_WRITE, ITRC_ENTER, 2, count, -1 );
1716
1717 /* Flush out any buffered data left over from ip2_putchar() calls. */
1718 ip2_flush_chars( tty );
1719
1720 /* This is the actual move bit. Make sure it does what we need!!!!! */
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001721 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Al Virof061c582006-10-11 17:45:47 +01001722 bytesSent = i2Output( pCh, pData, count);
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001723 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 ip2trace (CHANN, ITRC_WRITE, ITRC_RETURN, 1, bytesSent );
1726
1727 return bytesSent > 0 ? bytesSent : 0;
1728}
1729
1730/******************************************************************************/
1731/* Function: ip2_putchar() */
1732/* Parameters: Pointer to tty structure */
1733/* Character to write */
1734/* Returns: Nothing */
1735/* */
1736/* Description: */
1737/* */
1738/* */
1739/******************************************************************************/
Alan Coxf34d7a52008-04-30 00:54:13 -07001740static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741ip2_putchar( PTTY tty, unsigned char ch )
1742{
1743 i2ChanStrPtr pCh = tty->driver_data;
1744 unsigned long flags;
1745
1746// ip2trace (CHANN, ITRC_PUTC, ITRC_ENTER, 1, ch );
1747
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001748 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 pCh->Pbuf[pCh->Pbuf_stuff++] = ch;
1750 if ( pCh->Pbuf_stuff == sizeof pCh->Pbuf ) {
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001751 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ip2_flush_chars( tty );
1753 } else
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001754 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -07001755 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757// ip2trace (CHANN, ITRC_PUTC, ITRC_RETURN, 1, ch );
1758}
1759
1760/******************************************************************************/
1761/* Function: ip2_flush_chars() */
1762/* Parameters: Pointer to tty structure */
1763/* Returns: Nothing */
1764/* */
1765/* Description: */
1766/* */
1767/******************************************************************************/
1768static void
1769ip2_flush_chars( PTTY tty )
1770{
1771 int strip;
1772 i2ChanStrPtr pCh = tty->driver_data;
1773 unsigned long flags;
1774
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001775 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if ( pCh->Pbuf_stuff ) {
1777
1778// ip2trace (CHANN, ITRC_PUTC, 10, 1, strip );
1779
1780 //
1781 // We may need to restart i2Output if it does not fullfill this request
1782 //
Al Virof061c582006-10-11 17:45:47 +01001783 strip = i2Output( pCh, pCh->Pbuf, pCh->Pbuf_stuff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if ( strip != pCh->Pbuf_stuff ) {
1785 memmove( pCh->Pbuf, &pCh->Pbuf[strip], pCh->Pbuf_stuff - strip );
1786 }
1787 pCh->Pbuf_stuff -= strip;
1788 }
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001789 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/******************************************************************************/
1793/* Function: ip2_write_room() */
1794/* Parameters: Pointer to tty structure */
1795/* Returns: Number of bytes that the driver can accept */
1796/* */
1797/* Description: */
1798/* */
1799/******************************************************************************/
1800static int
1801ip2_write_room ( PTTY tty )
1802{
1803 int bytesFree;
1804 i2ChanStrPtr pCh = tty->driver_data;
1805 unsigned long flags;
1806
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001807 read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 bytesFree = i2OutputFree( pCh ) - pCh->Pbuf_stuff;
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001809 read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 ip2trace (CHANN, ITRC_WRITE, 11, 1, bytesFree );
1812
1813 return ((bytesFree > 0) ? bytesFree : 0);
1814}
1815
1816/******************************************************************************/
1817/* Function: ip2_chars_in_buf() */
1818/* Parameters: Pointer to tty structure */
1819/* Returns: Number of bytes queued for transmission */
1820/* */
1821/* Description: */
1822/* */
1823/* */
1824/******************************************************************************/
1825static int
1826ip2_chars_in_buf ( PTTY tty )
1827{
1828 i2ChanStrPtr pCh = tty->driver_data;
1829 int rc;
1830 unsigned long flags;
1831
1832 ip2trace (CHANN, ITRC_WRITE, 12, 1, pCh->Obuf_char_count + pCh->Pbuf_stuff );
1833
1834#ifdef IP2DEBUG_WRITE
1835 printk (KERN_DEBUG "IP2: chars in buffer = %d (%d,%d)\n",
1836 pCh->Obuf_char_count + pCh->Pbuf_stuff,
1837 pCh->Obuf_char_count, pCh->Pbuf_stuff );
1838#endif
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001839 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rc = pCh->Obuf_char_count;
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001841 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1842 read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 rc += pCh->Pbuf_stuff;
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001844 read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return rc;
1846}
1847
1848/******************************************************************************/
1849/* Function: ip2_flush_buffer() */
1850/* Parameters: Pointer to tty structure */
1851/* Returns: Nothing */
1852/* */
1853/* Description: */
1854/* */
1855/* */
1856/******************************************************************************/
1857static void
1858ip2_flush_buffer( PTTY tty )
1859{
1860 i2ChanStrPtr pCh = tty->driver_data;
1861 unsigned long flags;
1862
1863 ip2trace (CHANN, ITRC_FLUSH, ITRC_ENTER, 0 );
1864
1865#ifdef IP2DEBUG_WRITE
1866 printk (KERN_DEBUG "IP2: flush buffer\n" );
1867#endif
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001868 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 pCh->Pbuf_stuff = 0;
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001870 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 i2FlushOutput( pCh );
1872 ip2_owake(tty);
1873
1874 ip2trace (CHANN, ITRC_FLUSH, ITRC_RETURN, 0 );
1875
1876}
1877
1878/******************************************************************************/
1879/* Function: ip2_wait_until_sent() */
1880/* Parameters: Pointer to tty structure */
1881/* Timeout for wait. */
1882/* Returns: Nothing */
1883/* */
1884/* Description: */
1885/* This function is used in place of the normal tty_wait_until_sent, which */
1886/* only waits for the driver buffers to be empty (or rather, those buffers */
1887/* reported by chars_in_buffer) which doesn't work for IP2 due to the */
1888/* indeterminate number of bytes buffered on the board. */
1889/******************************************************************************/
1890static void
1891ip2_wait_until_sent ( PTTY tty, int timeout )
1892{
1893 int i = jiffies;
1894 i2ChanStrPtr pCh = tty->driver_data;
1895
1896 tty_wait_until_sent(tty, timeout );
1897 if ( (i = timeout - (jiffies -i)) > 0)
1898 i2DrainOutput( pCh, i );
1899}
1900
1901/******************************************************************************/
1902/******************************************************************************/
1903/* Device Input Section */
1904/******************************************************************************/
1905/******************************************************************************/
1906
1907/******************************************************************************/
1908/* Function: ip2_throttle() */
1909/* Parameters: Pointer to tty structure */
1910/* Returns: Nothing */
1911/* */
1912/* Description: */
1913/* */
1914/* */
1915/******************************************************************************/
1916static void
1917ip2_throttle ( PTTY tty )
1918{
1919 i2ChanStrPtr pCh = tty->driver_data;
1920
1921#ifdef IP2DEBUG_READ
1922 printk (KERN_DEBUG "IP2: throttle\n" );
1923#endif
1924 /*
1925 * Signal the poll/interrupt handlers not to forward incoming data to
1926 * the line discipline. This will cause the buffers to fill up in the
1927 * library and thus cause the library routines to send the flow control
1928 * stuff.
1929 */
1930 pCh->throttled = 1;
1931}
1932
1933/******************************************************************************/
1934/* Function: ip2_unthrottle() */
1935/* Parameters: Pointer to tty structure */
1936/* Returns: Nothing */
1937/* */
1938/* Description: */
1939/* */
1940/* */
1941/******************************************************************************/
1942static void
1943ip2_unthrottle ( PTTY tty )
1944{
1945 i2ChanStrPtr pCh = tty->driver_data;
1946 unsigned long flags;
1947
1948#ifdef IP2DEBUG_READ
1949 printk (KERN_DEBUG "IP2: unthrottle\n" );
1950#endif
1951
1952 /* Pass incoming data up to the line discipline again. */
1953 pCh->throttled = 0;
1954 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
1955 serviceOutgoingFifo( pCh->pMyBord );
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001956 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if ( pCh->Ibuf_stuff != pCh->Ibuf_strip ) {
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001958 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959#ifdef IP2DEBUG_READ
1960 printk (KERN_DEBUG "i2Input called from unthrottle\n" );
1961#endif
1962 i2Input( pCh );
1963 } else
Jiri Slabycf1c63c2008-04-30 00:53:54 -07001964 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967static void
1968ip2_start ( PTTY tty )
1969{
1970 i2ChanStrPtr pCh = DevTable[tty->index];
1971
1972 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
1973 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_UNSUSPEND);
1974 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_RESUME);
1975#ifdef IP2DEBUG_WRITE
1976 printk (KERN_DEBUG "IP2: start tx\n" );
1977#endif
1978}
1979
1980static void
1981ip2_stop ( PTTY tty )
1982{
1983 i2ChanStrPtr pCh = DevTable[tty->index];
1984
1985 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_SUSPEND);
1986#ifdef IP2DEBUG_WRITE
1987 printk (KERN_DEBUG "IP2: stop tx\n" );
1988#endif
1989}
1990
1991/******************************************************************************/
1992/* Device Ioctl Section */
1993/******************************************************************************/
1994
1995static int ip2_tiocmget(struct tty_struct *tty, struct file *file)
1996{
1997 i2ChanStrPtr pCh = DevTable[tty->index];
Alan Coxd9e39532006-01-09 20:54:20 -08001998#ifdef ENABLE_DSSNOW
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 wait_queue_t wait;
Alan Coxd9e39532006-01-09 20:54:20 -08002000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 if (pCh == NULL)
2003 return -ENODEV;
2004
2005/*
2006 FIXME - the following code is causing a NULL pointer dereference in
2007 2.3.51 in an interrupt handler. It's suppose to prompt the board
2008 to return the DSS signal status immediately. Why doesn't it do
2009 the same thing in 2.2.14?
2010*/
2011
2012/* This thing is still busted in the 1.2.12 driver on 2.4.x
2013 and even hoses the serial console so the oops can be trapped.
2014 /\/\|=mhw=|\/\/ */
2015
2016#ifdef ENABLE_DSSNOW
2017 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DSS_NOW);
2018
2019 init_waitqueue_entry(&wait, current);
2020 add_wait_queue(&pCh->dss_now_wait, &wait);
2021 set_current_state( TASK_INTERRUPTIBLE );
2022
2023 serviceOutgoingFifo( pCh->pMyBord );
2024
2025 schedule();
2026
2027 set_current_state( TASK_RUNNING );
2028 remove_wait_queue(&pCh->dss_now_wait, &wait);
2029
2030 if (signal_pending(current)) {
2031 return -EINTR;
2032 }
2033#endif
2034 return ((pCh->dataSetOut & I2_RTS) ? TIOCM_RTS : 0)
2035 | ((pCh->dataSetOut & I2_DTR) ? TIOCM_DTR : 0)
2036 | ((pCh->dataSetIn & I2_DCD) ? TIOCM_CAR : 0)
2037 | ((pCh->dataSetIn & I2_RI) ? TIOCM_RNG : 0)
2038 | ((pCh->dataSetIn & I2_DSR) ? TIOCM_DSR : 0)
2039 | ((pCh->dataSetIn & I2_CTS) ? TIOCM_CTS : 0);
2040}
2041
2042static int ip2_tiocmset(struct tty_struct *tty, struct file *file,
2043 unsigned int set, unsigned int clear)
2044{
2045 i2ChanStrPtr pCh = DevTable[tty->index];
2046
2047 if (pCh == NULL)
2048 return -ENODEV;
2049
2050 if (set & TIOCM_RTS) {
2051 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSUP);
2052 pCh->dataSetOut |= I2_RTS;
2053 }
2054 if (set & TIOCM_DTR) {
2055 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRUP);
2056 pCh->dataSetOut |= I2_DTR;
2057 }
2058
2059 if (clear & TIOCM_RTS) {
2060 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSDN);
2061 pCh->dataSetOut &= ~I2_RTS;
2062 }
2063 if (clear & TIOCM_DTR) {
2064 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRDN);
2065 pCh->dataSetOut &= ~I2_DTR;
2066 }
2067 serviceOutgoingFifo( pCh->pMyBord );
2068 return 0;
2069}
2070
2071/******************************************************************************/
2072/* Function: ip2_ioctl() */
2073/* Parameters: Pointer to tty structure */
2074/* Pointer to file structure */
2075/* Command */
2076/* Argument */
2077/* Returns: Success or failure */
2078/* */
2079/* Description: */
2080/* */
2081/* */
2082/******************************************************************************/
2083static int
2084ip2_ioctl ( PTTY tty, struct file *pFile, UINT cmd, ULONG arg )
2085{
2086 wait_queue_t wait;
2087 i2ChanStrPtr pCh = DevTable[tty->index];
Alan Coxd9e39532006-01-09 20:54:20 -08002088 i2eBordStrPtr pB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct async_icount cprev, cnow; /* kernel counter temps */
2090 struct serial_icounter_struct __user *p_cuser;
2091 int rc = 0;
2092 unsigned long flags;
2093 void __user *argp = (void __user *)arg;
2094
Alan Coxd9e39532006-01-09 20:54:20 -08002095 if ( pCh == NULL )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return -ENODEV;
Alan Coxd9e39532006-01-09 20:54:20 -08002097
2098 pB = pCh->pMyBord;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 ip2trace (CHANN, ITRC_IOCTL, ITRC_ENTER, 2, cmd, arg );
2101
2102#ifdef IP2DEBUG_IOCTL
2103 printk(KERN_DEBUG "IP2: ioctl cmd (%x), arg (%lx)\n", cmd, arg );
2104#endif
2105
2106 switch(cmd) {
2107 case TIOCGSERIAL:
2108
2109 ip2trace (CHANN, ITRC_IOCTL, 2, 1, rc );
2110
2111 rc = get_serial_info(pCh, argp);
2112 if (rc)
2113 return rc;
2114 break;
2115
2116 case TIOCSSERIAL:
2117
2118 ip2trace (CHANN, ITRC_IOCTL, 3, 1, rc );
2119
2120 rc = set_serial_info(pCh, argp);
2121 if (rc)
2122 return rc;
2123 break;
2124
2125 case TCXONC:
2126 rc = tty_check_change(tty);
2127 if (rc)
2128 return rc;
2129 switch (arg) {
2130 case TCOOFF:
2131 //return -ENOIOCTLCMD;
2132 break;
2133 case TCOON:
2134 //return -ENOIOCTLCMD;
2135 break;
2136 case TCIOFF:
2137 if (STOP_CHAR(tty) != __DISABLED_CHAR) {
2138 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2139 CMD_XMIT_NOW(STOP_CHAR(tty)));
2140 }
2141 break;
2142 case TCION:
2143 if (START_CHAR(tty) != __DISABLED_CHAR) {
2144 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2145 CMD_XMIT_NOW(START_CHAR(tty)));
2146 }
2147 break;
2148 default:
2149 return -EINVAL;
2150 }
2151 return 0;
2152
2153 case TCSBRK: /* SVID version: non-zero arg --> no break */
2154 rc = tty_check_change(tty);
2155
2156 ip2trace (CHANN, ITRC_IOCTL, 4, 1, rc );
2157
2158 if (!rc) {
2159 ip2_wait_until_sent(tty,0);
2160 if (!arg) {
2161 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SEND_BRK(250));
2162 serviceOutgoingFifo( pCh->pMyBord );
2163 }
2164 }
2165 break;
2166
2167 case TCSBRKP: /* support for POSIX tcsendbreak() */
2168 rc = tty_check_change(tty);
2169
2170 ip2trace (CHANN, ITRC_IOCTL, 5, 1, rc );
2171
2172 if (!rc) {
2173 ip2_wait_until_sent(tty,0);
2174 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2175 CMD_SEND_BRK(arg ? arg*100 : 250));
2176 serviceOutgoingFifo ( pCh->pMyBord );
2177 }
2178 break;
2179
2180 case TIOCGSOFTCAR:
2181
2182 ip2trace (CHANN, ITRC_IOCTL, 6, 1, rc );
2183
2184 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
2185 if (rc)
2186 return rc;
2187 break;
2188
2189 case TIOCSSOFTCAR:
2190
2191 ip2trace (CHANN, ITRC_IOCTL, 7, 1, rc );
2192
2193 rc = get_user(arg,(unsigned long __user *) argp);
2194 if (rc)
2195 return rc;
2196 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL)
2197 | (arg ? CLOCAL : 0));
2198
2199 break;
2200
2201 /*
2202 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change - mask
2203 * passed in arg for lines of interest (use |'ed TIOCM_RNG/DSR/CD/CTS
2204 * for masking). Caller should use TIOCGICOUNT to see which one it was
2205 */
2206 case TIOCMIWAIT:
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002207 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 cprev = pCh->icount; /* note the counters on entry */
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002209 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 4,
2211 CMD_DCD_REP, CMD_CTS_REP, CMD_DSR_REP, CMD_RI_REP);
2212 init_waitqueue_entry(&wait, current);
2213 add_wait_queue(&pCh->delta_msr_wait, &wait);
2214 set_current_state( TASK_INTERRUPTIBLE );
2215
2216 serviceOutgoingFifo( pCh->pMyBord );
2217 for(;;) {
2218 ip2trace (CHANN, ITRC_IOCTL, 10, 0 );
2219
2220 schedule();
2221
2222 ip2trace (CHANN, ITRC_IOCTL, 11, 0 );
2223
2224 /* see if a signal did it */
2225 if (signal_pending(current)) {
2226 rc = -ERESTARTSYS;
2227 break;
2228 }
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002229 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 cnow = pCh->icount; /* atomic copy */
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002231 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2233 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2234 rc = -EIO; /* no change => rc */
2235 break;
2236 }
2237 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2238 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2239 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2240 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2241 rc = 0;
2242 break;
2243 }
2244 cprev = cnow;
2245 }
2246 set_current_state( TASK_RUNNING );
2247 remove_wait_queue(&pCh->delta_msr_wait, &wait);
2248
2249 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 3,
2250 CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
2251 if ( ! (pCh->flags & ASYNC_CHECK_CD)) {
2252 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DCD_NREP);
2253 }
2254 serviceOutgoingFifo( pCh->pMyBord );
2255 return rc;
2256 break;
2257
2258 /*
2259 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2260 * Return: write counters to the user passed counter struct
2261 * NB: both 1->0 and 0->1 transitions are counted except for RI where
2262 * only 0->1 is counted. The controller is quite capable of counting
2263 * both, but this done to preserve compatibility with the standard
2264 * serial driver.
2265 */
2266 case TIOCGICOUNT:
2267 ip2trace (CHANN, ITRC_IOCTL, 11, 1, rc );
2268
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002269 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 cnow = pCh->icount;
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002271 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 p_cuser = argp;
2273 rc = put_user(cnow.cts, &p_cuser->cts);
2274 rc = put_user(cnow.dsr, &p_cuser->dsr);
2275 rc = put_user(cnow.rng, &p_cuser->rng);
2276 rc = put_user(cnow.dcd, &p_cuser->dcd);
2277 rc = put_user(cnow.rx, &p_cuser->rx);
2278 rc = put_user(cnow.tx, &p_cuser->tx);
2279 rc = put_user(cnow.frame, &p_cuser->frame);
2280 rc = put_user(cnow.overrun, &p_cuser->overrun);
2281 rc = put_user(cnow.parity, &p_cuser->parity);
2282 rc = put_user(cnow.brk, &p_cuser->brk);
2283 rc = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
2284 break;
2285
2286 /*
2287 * The rest are not supported by this driver. By returning -ENOIOCTLCMD they
2288 * will be passed to the line discipline for it to handle.
2289 */
2290 case TIOCSERCONFIG:
2291 case TIOCSERGWILD:
2292 case TIOCSERGETLSR:
2293 case TIOCSERSWILD:
2294 case TIOCSERGSTRUCT:
2295 case TIOCSERGETMULTI:
2296 case TIOCSERSETMULTI:
2297
2298 default:
2299 ip2trace (CHANN, ITRC_IOCTL, 12, 0 );
2300
2301 rc = -ENOIOCTLCMD;
2302 break;
2303 }
2304
2305 ip2trace (CHANN, ITRC_IOCTL, ITRC_RETURN, 0 );
2306
2307 return rc;
2308}
2309
2310/******************************************************************************/
2311/* Function: GetSerialInfo() */
2312/* Parameters: Pointer to channel structure */
2313/* Pointer to old termios structure */
2314/* Returns: Nothing */
2315/* */
2316/* Description: */
2317/* This is to support the setserial command, and requires processing of the */
2318/* standard Linux serial structure. */
2319/******************************************************************************/
2320static int
2321get_serial_info ( i2ChanStrPtr pCh, struct serial_struct __user *retinfo )
2322{
2323 struct serial_struct tmp;
2324
2325 memset ( &tmp, 0, sizeof(tmp) );
2326 tmp.type = pCh->pMyBord->channelBtypes.bid_value[(pCh->port_index & (IP2_PORTS_PER_BOARD-1))/16];
2327 if (BID_HAS_654(tmp.type)) {
2328 tmp.type = PORT_16650;
2329 } else {
2330 tmp.type = PORT_CIRRUS;
2331 }
2332 tmp.line = pCh->port_index;
2333 tmp.port = pCh->pMyBord->i2eBase;
2334 tmp.irq = ip2config.irq[pCh->port_index/64];
2335 tmp.flags = pCh->flags;
2336 tmp.baud_base = pCh->BaudBase;
2337 tmp.close_delay = pCh->ClosingDelay;
2338 tmp.closing_wait = pCh->ClosingWaitTime;
2339 tmp.custom_divisor = pCh->BaudDivisor;
2340 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
2341}
2342
2343/******************************************************************************/
2344/* Function: SetSerialInfo() */
2345/* Parameters: Pointer to channel structure */
2346/* Pointer to old termios structure */
2347/* Returns: Nothing */
2348/* */
2349/* Description: */
2350/* This function provides support for setserial, which uses the TIOCSSERIAL */
2351/* ioctl. Not all setserial parameters are relevant. If the user attempts to */
2352/* change the IRQ, address or type of the port the ioctl fails. */
2353/******************************************************************************/
2354static int
2355set_serial_info( i2ChanStrPtr pCh, struct serial_struct __user *new_info )
2356{
2357 struct serial_struct ns;
2358 int old_flags, old_baud_divisor;
2359
2360 if (copy_from_user(&ns, new_info, sizeof (ns)))
2361 return -EFAULT;
2362
2363 /*
2364 * We don't allow setserial to change IRQ, board address, type or baud
2365 * base. Also line nunber as such is meaningless but we use it for our
2366 * array index so it is fixed also.
2367 */
2368 if ( (ns.irq != ip2config.irq[pCh->port_index])
2369 || ((int) ns.port != ((int) (pCh->pMyBord->i2eBase)))
2370 || (ns.baud_base != pCh->BaudBase)
2371 || (ns.line != pCh->port_index) ) {
2372 return -EINVAL;
2373 }
2374
2375 old_flags = pCh->flags;
2376 old_baud_divisor = pCh->BaudDivisor;
2377
2378 if ( !capable(CAP_SYS_ADMIN) ) {
2379 if ( ( ns.close_delay != pCh->ClosingDelay ) ||
2380 ( (ns.flags & ~ASYNC_USR_MASK) !=
2381 (pCh->flags & ~ASYNC_USR_MASK) ) ) {
2382 return -EPERM;
2383 }
2384
2385 pCh->flags = (pCh->flags & ~ASYNC_USR_MASK) |
2386 (ns.flags & ASYNC_USR_MASK);
2387 pCh->BaudDivisor = ns.custom_divisor;
2388 } else {
2389 pCh->flags = (pCh->flags & ~ASYNC_FLAGS) |
2390 (ns.flags & ASYNC_FLAGS);
2391 pCh->BaudDivisor = ns.custom_divisor;
2392 pCh->ClosingDelay = ns.close_delay * HZ/100;
2393 pCh->ClosingWaitTime = ns.closing_wait * HZ/100;
2394 }
2395
2396 if ( ( (old_flags & ASYNC_SPD_MASK) != (pCh->flags & ASYNC_SPD_MASK) )
2397 || (old_baud_divisor != pCh->BaudDivisor) ) {
2398 // Invalidate speed and reset parameters
2399 set_params( pCh, NULL );
2400 }
2401
2402 return 0;
2403}
2404
2405/******************************************************************************/
2406/* Function: ip2_set_termios() */
2407/* Parameters: Pointer to tty structure */
2408/* Pointer to old termios structure */
2409/* Returns: Nothing */
2410/* */
2411/* Description: */
2412/* */
2413/* */
2414/******************************************************************************/
2415static void
Alan Cox606d0992006-12-08 02:38:45 -08002416ip2_set_termios( PTTY tty, struct ktermios *old_termios )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 i2ChanStrPtr pCh = (i2ChanStrPtr)tty->driver_data;
2419
2420#ifdef IP2DEBUG_IOCTL
2421 printk (KERN_DEBUG "IP2: set termios %p\n", old_termios );
2422#endif
2423
2424 set_params( pCh, old_termios );
2425}
2426
2427/******************************************************************************/
2428/* Function: ip2_set_line_discipline() */
2429/* Parameters: Pointer to tty structure */
2430/* Returns: Nothing */
2431/* */
2432/* Description: Does nothing */
2433/* */
2434/* */
2435/******************************************************************************/
2436static void
2437ip2_set_line_discipline ( PTTY tty )
2438{
2439#ifdef IP2DEBUG_IOCTL
2440 printk (KERN_DEBUG "IP2: set line discipline\n" );
2441#endif
2442
2443 ip2trace (((i2ChanStrPtr)tty->driver_data)->port_index, ITRC_IOCTL, 16, 0 );
2444
2445}
2446
2447/******************************************************************************/
2448/* Function: SetLine Characteristics() */
2449/* Parameters: Pointer to channel structure */
2450/* Returns: Nothing */
2451/* */
2452/* Description: */
2453/* This routine is called to update the channel structure with the new line */
2454/* characteristics, and send the appropriate commands to the board when they */
2455/* change. */
2456/******************************************************************************/
2457static void
Alan Cox606d0992006-12-08 02:38:45 -08002458set_params( i2ChanStrPtr pCh, struct ktermios *o_tios )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 tcflag_t cflag, iflag, lflag;
2461 char stop_char, start_char;
Alan Cox606d0992006-12-08 02:38:45 -08002462 struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 lflag = pCh->pTTY->termios->c_lflag;
2465 cflag = pCh->pTTY->termios->c_cflag;
2466 iflag = pCh->pTTY->termios->c_iflag;
2467
2468 if (o_tios == NULL) {
2469 dummy.c_lflag = ~lflag;
2470 dummy.c_cflag = ~cflag;
2471 dummy.c_iflag = ~iflag;
2472 o_tios = &dummy;
2473 }
2474
2475 {
2476 switch ( cflag & CBAUD ) {
2477 case B0:
2478 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
2479 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
2480 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
2481 pCh->pTTY->termios->c_cflag |= (CBAUD & o_tios->c_cflag);
2482 goto service_it;
2483 break;
2484 case B38400:
2485 /*
2486 * This is the speed that is overloaded with all the other high
2487 * speeds, depending upon the flag settings.
2488 */
2489 if ( ( pCh->flags & ASYNC_SPD_MASK ) == ASYNC_SPD_HI ) {
2490 pCh->speed = CBR_57600;
2491 } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI ) {
2492 pCh->speed = CBR_115200;
2493 } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST ) {
2494 pCh->speed = CBR_C1;
2495 } else {
2496 pCh->speed = CBR_38400;
2497 }
2498 break;
2499 case B50: pCh->speed = CBR_50; break;
2500 case B75: pCh->speed = CBR_75; break;
2501 case B110: pCh->speed = CBR_110; break;
2502 case B134: pCh->speed = CBR_134; break;
2503 case B150: pCh->speed = CBR_150; break;
2504 case B200: pCh->speed = CBR_200; break;
2505 case B300: pCh->speed = CBR_300; break;
2506 case B600: pCh->speed = CBR_600; break;
2507 case B1200: pCh->speed = CBR_1200; break;
2508 case B1800: pCh->speed = CBR_1800; break;
2509 case B2400: pCh->speed = CBR_2400; break;
2510 case B4800: pCh->speed = CBR_4800; break;
2511 case B9600: pCh->speed = CBR_9600; break;
2512 case B19200: pCh->speed = CBR_19200; break;
2513 case B57600: pCh->speed = CBR_57600; break;
2514 case B115200: pCh->speed = CBR_115200; break;
2515 case B153600: pCh->speed = CBR_153600; break;
2516 case B230400: pCh->speed = CBR_230400; break;
2517 case B307200: pCh->speed = CBR_307200; break;
2518 case B460800: pCh->speed = CBR_460800; break;
2519 case B921600: pCh->speed = CBR_921600; break;
2520 default: pCh->speed = CBR_9600; break;
2521 }
2522 if ( pCh->speed == CBR_C1 ) {
2523 // Process the custom speed parameters.
2524 int bps = pCh->BaudBase / pCh->BaudDivisor;
2525 if ( bps == 921600 ) {
2526 pCh->speed = CBR_921600;
2527 } else {
2528 bps = bps/10;
2529 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_BAUD_DEF1(bps) );
2530 }
2531 }
2532 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_SETBAUD(pCh->speed));
2533
2534 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
2535 pCh->dataSetOut |= (I2_DTR | I2_RTS);
2536 }
2537 if ( (CSTOPB & cflag) ^ (CSTOPB & o_tios->c_cflag))
2538 {
2539 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1,
2540 CMD_SETSTOP( ( cflag & CSTOPB ) ? CST_2 : CST_1));
2541 }
2542 if (((PARENB|PARODD) & cflag) ^ ((PARENB|PARODD) & o_tios->c_cflag))
2543 {
2544 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1,
2545 CMD_SETPAR(
2546 (cflag & PARENB ? (cflag & PARODD ? CSP_OD : CSP_EV) : CSP_NP)
2547 )
2548 );
2549 }
2550 /* byte size and parity */
2551 if ( (CSIZE & cflag)^(CSIZE & o_tios->c_cflag))
2552 {
2553 int datasize;
2554 switch ( cflag & CSIZE ) {
2555 case CS5: datasize = CSZ_5; break;
2556 case CS6: datasize = CSZ_6; break;
2557 case CS7: datasize = CSZ_7; break;
2558 case CS8: datasize = CSZ_8; break;
2559 default: datasize = CSZ_5; break; /* as per serial.c */
2560 }
2561 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1, CMD_SETBITS(datasize) );
2562 }
2563 /* Process CTS flow control flag setting */
2564 if ( (cflag & CRTSCTS) ) {
2565 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2566 2, CMD_CTSFL_ENAB, CMD_RTSFL_ENAB);
2567 } else {
2568 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2569 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
2570 }
2571 //
2572 // Process XON/XOFF flow control flags settings
2573 //
2574 stop_char = STOP_CHAR(pCh->pTTY);
2575 start_char = START_CHAR(pCh->pTTY);
2576
2577 //////////// can't be \000
2578 if (stop_char == __DISABLED_CHAR )
2579 {
2580 stop_char = ~__DISABLED_CHAR;
2581 }
2582 if (start_char == __DISABLED_CHAR )
2583 {
2584 start_char = ~__DISABLED_CHAR;
2585 }
2586 /////////////////////////////////
2587
2588 if ( o_tios->c_cc[VSTART] != start_char )
2589 {
2590 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXON(start_char));
2591 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXON(start_char));
2592 }
2593 if ( o_tios->c_cc[VSTOP] != stop_char )
2594 {
2595 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXOFF(stop_char));
2596 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXOFF(stop_char));
2597 }
2598 if (stop_char == __DISABLED_CHAR )
2599 {
2600 stop_char = ~__DISABLED_CHAR; //TEST123
2601 goto no_xoff;
2602 }
2603 if ((iflag & (IXOFF))^(o_tios->c_iflag & (IXOFF)))
2604 {
2605 if ( iflag & IXOFF ) { // Enable XOFF output flow control
2606 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_XON));
2607 } else { // Disable XOFF output flow control
2608no_xoff:
2609 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_NONE));
2610 }
2611 }
2612 if (start_char == __DISABLED_CHAR )
2613 {
2614 goto no_xon;
2615 }
2616 if ((iflag & (IXON|IXANY)) ^ (o_tios->c_iflag & (IXON|IXANY)))
2617 {
2618 if ( iflag & IXON ) {
2619 if ( iflag & IXANY ) { // Enable XON/XANY output flow control
2620 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XANY));
2621 } else { // Enable XON output flow control
2622 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XON));
2623 }
2624 } else { // Disable XON output flow control
2625no_xon:
2626 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_NONE));
2627 }
2628 }
2629 if ( (iflag & ISTRIP) ^ ( o_tios->c_iflag & (ISTRIP)) )
2630 {
2631 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2632 CMD_ISTRIP_OPT((iflag & ISTRIP ? 1 : 0)));
2633 }
2634 if ( (iflag & INPCK) ^ ( o_tios->c_iflag & (INPCK)) )
2635 {
2636 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2637 CMD_PARCHK((iflag & INPCK) ? CPK_ENAB : CPK_DSAB));
2638 }
2639
2640 if ( (iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR))
2641 ^ ( o_tios->c_iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR)) )
2642 {
2643 char brkrpt = 0;
2644 char parrpt = 0;
2645
2646 if ( iflag & IGNBRK ) { /* Ignore breaks altogether */
2647 /* Ignore breaks altogether */
2648 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_NREP);
2649 } else {
2650 if ( iflag & BRKINT ) {
2651 if ( iflag & PARMRK ) {
2652 brkrpt = 0x0a; // exception an inline triple
2653 } else {
2654 brkrpt = 0x1a; // exception and NULL
2655 }
2656 brkrpt |= 0x04; // flush input
2657 } else {
2658 if ( iflag & PARMRK ) {
2659 brkrpt = 0x0b; //POSIX triple \0377 \0 \0
2660 } else {
2661 brkrpt = 0x01; // Null only
2662 }
2663 }
2664 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_REP(brkrpt));
2665 }
2666
2667 if (iflag & IGNPAR) {
2668 parrpt = 0x20;
2669 /* would be 2 for not cirrus bug */
2670 /* would be 0x20 cept for cirrus bug */
2671 } else {
2672 if ( iflag & PARMRK ) {
2673 /*
2674 * Replace error characters with 3-byte sequence (\0377,\0,char)
2675 */
2676 parrpt = 0x04 ;
2677 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_ISTRIP_OPT((char)0));
2678 } else {
2679 parrpt = 0x03;
2680 }
2681 }
2682 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SET_ERROR(parrpt));
2683 }
2684 if (cflag & CLOCAL) {
2685 // Status reporting fails for DCD if this is off
2686 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_NREP);
2687 pCh->flags &= ~ASYNC_CHECK_CD;
2688 } else {
2689 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_REP);
2690 pCh->flags |= ASYNC_CHECK_CD;
2691 }
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693service_it:
2694 i2DrainOutput( pCh, 100 );
2695}
2696
2697/******************************************************************************/
2698/* IPL Device Section */
2699/******************************************************************************/
2700
2701/******************************************************************************/
2702/* Function: ip2_ipl_read() */
2703/* Parameters: Pointer to device inode */
2704/* Pointer to file structure */
2705/* Pointer to data */
2706/* Number of bytes to read */
2707/* Returns: Success or failure */
2708/* */
2709/* Description: Ugly */
2710/* */
2711/* */
2712/******************************************************************************/
2713
2714static
2715ssize_t
2716ip2_ipl_read(struct file *pFile, char __user *pData, size_t count, loff_t *off )
2717{
Josef Sipeka7113a92006-12-08 02:36:55 -08002718 unsigned int minor = iminor(pFile->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 int rc = 0;
2720
2721#ifdef IP2DEBUG_IPL
2722 printk (KERN_DEBUG "IP2IPL: read %p, %d bytes\n", pData, count );
2723#endif
2724
2725 switch( minor ) {
2726 case 0: // IPL device
2727 rc = -EINVAL;
2728 break;
2729 case 1: // Status dump
2730 rc = -EINVAL;
2731 break;
2732 case 2: // Ping device
2733 rc = -EINVAL;
2734 break;
2735 case 3: // Trace device
2736 rc = DumpTraceBuffer ( pData, count );
2737 break;
2738 case 4: // Trace device
2739 rc = DumpFifoBuffer ( pData, count );
2740 break;
2741 default:
2742 rc = -ENODEV;
2743 break;
2744 }
2745 return rc;
2746}
2747
2748static int
2749DumpFifoBuffer ( char __user *pData, int count )
2750{
2751#ifdef DEBUG_FIFO
2752 int rc;
2753 rc = copy_to_user(pData, DBGBuf, count);
2754
2755 printk(KERN_DEBUG "Last index %d\n", I );
2756
2757 return count;
2758#endif /* DEBUG_FIFO */
2759 return 0;
2760}
2761
2762static int
2763DumpTraceBuffer ( char __user *pData, int count )
2764{
2765#ifdef IP2DEBUG_TRACE
2766 int rc;
2767 int dumpcount;
2768 int chunk;
2769 int *pIndex = (int __user *)pData;
2770
2771 if ( count < (sizeof(int) * 6) ) {
2772 return -EIO;
2773 }
2774 rc = put_user(tracewrap, pIndex );
2775 rc = put_user(TRACEMAX, ++pIndex );
2776 rc = put_user(tracestrip, ++pIndex );
2777 rc = put_user(tracestuff, ++pIndex );
2778 pData += sizeof(int) * 6;
2779 count -= sizeof(int) * 6;
2780
2781 dumpcount = tracestuff - tracestrip;
2782 if ( dumpcount < 0 ) {
2783 dumpcount += TRACEMAX;
2784 }
2785 if ( dumpcount > count ) {
2786 dumpcount = count;
2787 }
2788 chunk = TRACEMAX - tracestrip;
2789 if ( dumpcount > chunk ) {
2790 rc = copy_to_user(pData, &tracebuf[tracestrip],
2791 chunk * sizeof(tracebuf[0]) );
2792 pData += chunk * sizeof(tracebuf[0]);
2793 tracestrip = 0;
2794 chunk = dumpcount - chunk;
2795 } else {
2796 chunk = dumpcount;
2797 }
2798 rc = copy_to_user(pData, &tracebuf[tracestrip],
2799 chunk * sizeof(tracebuf[0]) );
2800 tracestrip += chunk;
2801 tracewrap = 0;
2802
2803 rc = put_user(tracestrip, ++pIndex );
2804 rc = put_user(tracestuff, ++pIndex );
2805
2806 return dumpcount;
2807#else
2808 return 0;
2809#endif
2810}
2811
2812/******************************************************************************/
2813/* Function: ip2_ipl_write() */
2814/* Parameters: */
2815/* Pointer to file structure */
2816/* Pointer to data */
2817/* Number of bytes to write */
2818/* Returns: Success or failure */
2819/* */
2820/* Description: */
2821/* */
2822/* */
2823/******************************************************************************/
2824static ssize_t
2825ip2_ipl_write(struct file *pFile, const char __user *pData, size_t count, loff_t *off)
2826{
2827#ifdef IP2DEBUG_IPL
2828 printk (KERN_DEBUG "IP2IPL: write %p, %d bytes\n", pData, count );
2829#endif
2830 return 0;
2831}
2832
2833/******************************************************************************/
2834/* Function: ip2_ipl_ioctl() */
2835/* Parameters: Pointer to device inode */
2836/* Pointer to file structure */
2837/* Command */
2838/* Argument */
2839/* Returns: Success or failure */
2840/* */
2841/* Description: */
2842/* */
2843/* */
2844/******************************************************************************/
2845static int
2846ip2_ipl_ioctl ( struct inode *pInode, struct file *pFile, UINT cmd, ULONG arg )
2847{
2848 unsigned int iplminor = iminor(pInode);
2849 int rc = 0;
2850 void __user *argp = (void __user *)arg;
2851 ULONG __user *pIndex = argp;
2852 i2eBordStrPtr pB = i2BoardPtrTable[iplminor / 4];
2853 i2ChanStrPtr pCh;
2854
2855#ifdef IP2DEBUG_IPL
2856 printk (KERN_DEBUG "IP2IPL: ioctl cmd %d, arg %ld\n", cmd, arg );
2857#endif
2858
2859 switch ( iplminor ) {
2860 case 0: // IPL device
2861 rc = -EINVAL;
2862 break;
2863 case 1: // Status dump
2864 case 5:
2865 case 9:
2866 case 13:
2867 switch ( cmd ) {
2868 case 64: /* Driver - ip2stat */
2869 rc = put_user(ip2_tty_driver->refcount, pIndex++ );
2870 rc = put_user(irq_counter, pIndex++ );
2871 rc = put_user(bh_counter, pIndex++ );
2872 break;
2873
2874 case 65: /* Board - ip2stat */
2875 if ( pB ) {
2876 rc = copy_to_user(argp, pB, sizeof(i2eBordStr));
Jiri Slabycf1c63c2008-04-30 00:53:54 -07002877 rc = put_user(inb(pB->i2eStatus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 (ULONG __user *)(arg + (ULONG)(&pB->i2eStatus) - (ULONG)pB ) );
2879 } else {
2880 rc = -ENODEV;
2881 }
2882 break;
2883
2884 default:
2885 if (cmd < IP2_MAX_PORTS) {
2886 pCh = DevTable[cmd];
2887 if ( pCh )
2888 {
2889 rc = copy_to_user(argp, pCh, sizeof(i2ChanStr));
2890 } else {
2891 rc = -ENODEV;
2892 }
2893 } else {
2894 rc = -EINVAL;
2895 }
2896 }
2897 break;
2898
2899 case 2: // Ping device
2900 rc = -EINVAL;
2901 break;
2902 case 3: // Trace device
Andrew Mortondef93912006-02-03 03:04:47 -08002903 /*
2904 * akpm: This used to write a whole bunch of function addresses
2905 * to userspace, which generated lots of put_user() warnings.
2906 * I killed it all. Just return "success" and don't do
2907 * anything.
2908 */
2909 if (cmd == 1)
2910 rc = 0;
2911 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
2914
2915 default:
2916 rc = -ENODEV;
2917 break;
2918 }
2919 return rc;
2920}
2921
2922/******************************************************************************/
2923/* Function: ip2_ipl_open() */
2924/* Parameters: Pointer to device inode */
2925/* Pointer to file structure */
2926/* Returns: Success or failure */
2927/* */
2928/* Description: */
2929/* */
2930/* */
2931/******************************************************************************/
2932static int
2933ip2_ipl_open( struct inode *pInode, struct file *pFile )
2934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936#ifdef IP2DEBUG_IPL
2937 printk (KERN_DEBUG "IP2IPL: open\n" );
2938#endif
Jonathan Corbetf2b98572008-05-18 15:32:43 -06002939 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return 0;
2941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943static int
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002944proc_ip2mem_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 i2eBordStrPtr pB;
2947 i2ChanStrPtr pCh;
2948 PTTY tty;
2949 int i;
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#define FMTLINE "%3d: 0x%08x 0x%08x 0%011o 0%011o\n"
2952#define FMTLIN2 " 0x%04x 0x%04x tx flow 0x%x\n"
2953#define FMTLIN3 " 0x%04x 0x%04x rc flow\n"
2954
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002955 seq_printf(m,"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
2958 pB = i2BoardPtrTable[i];
2959 if ( pB ) {
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002960 seq_printf(m,"board %d:\n",i);
2961 seq_printf(m,"\tFifo rem: %d mty: %x outM %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 pB->i2eFifoRemains,pB->i2eWaitingForEmptyFifo,pB->i2eOutMailWaiting);
2963 }
2964 }
2965
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002966 seq_printf(m,"#: tty flags, port flags, cflags, iflags\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 for (i=0; i < IP2_MAX_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 pCh = DevTable[i];
2969 if (pCh) {
2970 tty = pCh->pTTY;
2971 if (tty && tty->count) {
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002972 seq_printf(m,FMTLINE,i,(int)tty->flags,pCh->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 tty->termios->c_cflag,tty->termios->c_iflag);
2974
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002975 seq_printf(m,FMTLIN2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 pCh->outfl.asof,pCh->outfl.room,pCh->channelNeeds);
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002977 seq_printf(m,FMTLIN3,pCh->infl.asof,pCh->infl.room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979 }
2980 }
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
Alexey Dobriyan4a5cdb52008-04-29 01:01:55 -07002984static int proc_ip2mem_open(struct inode *inode, struct file *file)
2985{
2986 return single_open(file, proc_ip2mem_show, NULL);
2987}
2988
2989static const struct file_operations ip2mem_proc_fops = {
2990 .owner = THIS_MODULE,
2991 .open = proc_ip2mem_open,
2992 .read = seq_read,
2993 .llseek = seq_lseek,
2994 .release = single_release,
2995};
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997/*
2998 * This is the handler for /proc/tty/driver/ip2
2999 *
3000 * This stretch of code has been largely plagerized from at least three
3001 * different sources including ip2mkdev.c and a couple of other drivers.
3002 * The bugs are all mine. :-) =mhw=
3003 */
3004static int ip2_read_proc(char *page, char **start, off_t off,
3005 int count, int *eof, void *data)
3006{
3007 int i, j, box;
3008 int len = 0;
3009 int boxes = 0;
3010 int ports = 0;
3011 int tports = 0;
3012 off_t begin = 0;
3013 i2eBordStrPtr pB;
3014
3015 len += sprintf(page, "ip2info: 1.0 driver: %s\n", pcVersion );
3016 len += sprintf(page+len, "Driver: SMajor=%d CMajor=%d IMajor=%d MaxBoards=%d MaxBoxes=%d MaxPorts=%d\n",
3017 IP2_TTY_MAJOR, IP2_CALLOUT_MAJOR, IP2_IPL_MAJOR,
3018 IP2_MAX_BOARDS, ABS_MAX_BOXES, ABS_BIGGEST_BOX);
3019
3020 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
3021 /* This need to be reset for a board by board count... */
3022 boxes = 0;
3023 pB = i2BoardPtrTable[i];
3024 if( pB ) {
3025 switch( pB->i2ePom.e.porID & ~POR_ID_RESERVED )
3026 {
3027 case POR_ID_FIIEX:
3028 len += sprintf( page+len, "Board %d: EX ports=", i );
3029 for( box = 0; box < ABS_MAX_BOXES; ++box )
3030 {
3031 ports = 0;
3032
3033 if( pB->i2eChannelMap[box] != 0 ) ++boxes;
3034 for( j = 0; j < ABS_BIGGEST_BOX; ++j )
3035 {
3036 if( pB->i2eChannelMap[box] & 1<< j ) {
3037 ++ports;
3038 }
3039 }
3040 len += sprintf( page+len, "%d,", ports );
3041 tports += ports;
3042 }
3043
3044 --len; /* Backup over that last comma */
3045
3046 len += sprintf( page+len, " boxes=%d width=%d", boxes, pB->i2eDataWidth16 ? 16 : 8 );
3047 break;
3048
3049 case POR_ID_II_4:
3050 len += sprintf(page+len, "Board %d: ISA-4 ports=4 boxes=1", i );
3051 tports = ports = 4;
3052 break;
3053
3054 case POR_ID_II_8:
3055 len += sprintf(page+len, "Board %d: ISA-8-std ports=8 boxes=1", i );
3056 tports = ports = 8;
3057 break;
3058
3059 case POR_ID_II_8R:
3060 len += sprintf(page+len, "Board %d: ISA-8-RJ11 ports=8 boxes=1", i );
3061 tports = ports = 8;
3062 break;
3063
3064 default:
3065 len += sprintf(page+len, "Board %d: unknown", i );
3066 /* Don't try and probe for minor numbers */
3067 tports = ports = 0;
3068 }
3069
3070 } else {
3071 /* Don't try and probe for minor numbers */
3072 len += sprintf(page+len, "Board %d: vacant", i );
3073 tports = ports = 0;
3074 }
3075
3076 if( tports ) {
3077 len += sprintf(page+len, " minors=" );
3078
3079 for ( box = 0; box < ABS_MAX_BOXES; ++box )
3080 {
3081 for ( j = 0; j < ABS_BIGGEST_BOX; ++j )
3082 {
3083 if ( pB->i2eChannelMap[box] & (1 << j) )
3084 {
3085 len += sprintf (page+len,"%d,",
3086 j + ABS_BIGGEST_BOX *
3087 (box+i*ABS_MAX_BOXES));
3088 }
3089 }
3090 }
3091
3092 page[ len - 1 ] = '\n'; /* Overwrite that last comma */
3093 } else {
3094 len += sprintf (page+len,"\n" );
3095 }
3096
3097 if (len+begin > off+count)
3098 break;
3099 if (len+begin < off) {
3100 begin += len;
3101 len = 0;
3102 }
3103 }
3104
3105 if (i >= IP2_MAX_BOARDS)
3106 *eof = 1;
3107 if (off >= len+begin)
3108 return 0;
3109
3110 *start = page + (off-begin);
3111 return ((count < begin+len-off) ? count : begin+len-off);
3112 }
3113
3114/******************************************************************************/
3115/* Function: ip2trace() */
3116/* Parameters: Value to add to trace buffer */
3117/* Returns: Nothing */
3118/* */
3119/* Description: */
3120/* */
3121/* */
3122/******************************************************************************/
3123#ifdef IP2DEBUG_TRACE
3124void
3125ip2trace (unsigned short pn, unsigned char cat, unsigned char label, unsigned long codes, ...)
3126{
3127 long flags;
3128 unsigned long *pCode = &codes;
3129 union ip2breadcrumb bc;
3130 i2ChanStrPtr pCh;
3131
3132
3133 tracebuf[tracestuff++] = jiffies;
3134 if ( tracestuff == TRACEMAX ) {
3135 tracestuff = 0;
3136 }
3137 if ( tracestuff == tracestrip ) {
3138 if ( ++tracestrip == TRACEMAX ) {
3139 tracestrip = 0;
3140 }
3141 ++tracewrap;
3142 }
3143
3144 bc.hdr.port = 0xff & pn;
3145 bc.hdr.cat = cat;
3146 bc.hdr.codes = (unsigned char)( codes & 0xff );
3147 bc.hdr.label = label;
3148 tracebuf[tracestuff++] = bc.value;
3149
3150 for (;;) {
3151 if ( tracestuff == TRACEMAX ) {
3152 tracestuff = 0;
3153 }
3154 if ( tracestuff == tracestrip ) {
3155 if ( ++tracestrip == TRACEMAX ) {
3156 tracestrip = 0;
3157 }
3158 ++tracewrap;
3159 }
3160
3161 if ( !codes-- )
3162 break;
3163
3164 tracebuf[tracestuff++] = *++pCode;
3165 }
3166}
3167#endif
3168
3169
3170MODULE_LICENSE("GPL");
Chuck Shorteb0e71c2006-07-10 04:43:59 -07003171
3172static struct pci_device_id ip2main_pci_tbl[] __devinitdata = {
3173 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_IP2EX) },
3174 { }
3175};
3176
3177MODULE_DEVICE_TABLE(pci, ip2main_pci_tbl);