GIT fb40209d18ccf3ea0bf648406354ab218f6e9166 git+ssh://master.kernel.org/pub/scm/linux/kernel/git/jikos/ipwireless_cs.git#ipw-devel commit Author: David Sterba Date: Mon Jun 4 17:09:31 2007 +0200 ipwireless_cs: Whitespace and codingstyle cleanups in hw Reformat acc to CodingStyle - long comments - func prototypes - delcarations Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 831ede0cf995341eb0487ef60828a1c478fc4394 Author: David Sterba Date: Fri Jun 1 17:12:13 2007 +0200 ipwireless_cs: Minor clenups in network code (non intrusive changes) - use C99 struct intializeres - group defines together - func prototype formatting Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 0d2488340d567832531e5f814514afdd1390b0fa Author: David Sterba Date: Fri Jun 1 12:17:13 2007 +0200 ipwireless_cs: Fix sparse warning: different address space Fix warnings like drivers/char/pcmcia/ipwireless_cs_network.c:179:7: warning: incorrect type in argument 1 (different address spaces) drivers/char/pcmcia/ipwireless_cs_network.c:179:7: expected void [noderef] * drivers/char/pcmcia/ipwireless_cs_network.c:179:7: got int * and use __user tag for get_user/put_user. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 2f8a582d660ef9a272d6d0f3918a4628ee35e090 Author: David Sterba Date: Fri Jun 1 12:17:11 2007 +0200 ipwireless_cs: Fix sparse warnings: different address spaces Fix warnings like drivers/char/pcmcia/ipwireless_cs_hardware.c:1067:36: warning: incorrect type in argument 1 (different address spaces) drivers/char/pcmcia/ipwireless_cs_hardware.c:1067:36: expected void [noderef] * drivers/char/pcmcia/ipwireless_cs_hardware.c:1067:36: got unsigned short *MemTX when using mmapped io Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 4c198f8ff95fc98faad3701ddff0790437f05cbb Author: David Sterba Date: Fri Jun 1 12:11:32 2007 +0200 ipwireless_cs: Remove callback typedefs Remove all callback typedefs and use the type inline. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 6544273b0a476c440da65dc751412b84effe5d59 Author: David Sterba Date: Fri Jun 1 12:11:30 2007 +0200 ipwireless_cs: Network code refactoring Refactor ppp_blocked, substitue variable by function, adjust packet-sent notifier. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit d1475e3c48474ff50a71d921283c237f5bf35142 Author: David Sterba Date: Fri Jun 1 12:11:28 2007 +0200 ipwireless_cs: Remove macro hiding __iomem Remove macro hiding __iomem, use it directly. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 525305b3951109b9b93d13136cef2dd0244d8ae9 Author: David Sterba Date: Thu May 24 10:58:31 2007 +0200 ipwireless_cs: Cleanup timig stats Group timing statistic together. Remove ifdefs. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 60d2fcb331a34c55ed0bcd2f6189ed957d244832 Author: David Sterba Date: Wed May 23 09:41:51 2007 +0200 ipwireless_cs: Convert binary semaphore to mutex Convert tty binary semaphore to mutex. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit e8e2edf6808d2e08059f483c52c8472cffcd0337 Author: David Sterba Date: Tue May 22 13:47:48 2007 +0200 ipwireless_cs: code clenaups - spinlocks Convert trivial cases of spin_{un}lock to use flag-preserving versions. Use long for the flags. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 28408acd64d26bb97f1b7ca2e563b88846eec270 Author: David Sterba Date: Tue May 22 11:52:32 2007 +0200 ipwireless_cs: more code cleanups codingstyle cleanups: - long lines - prototype argument names - CodingStyle Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 884efb463ba055bd747abb493ab13dd2fefa1e63 Author: David Sterba Date: Mon May 21 12:29:19 2007 +0200 ipwireless_cs: type/whitespace cleanups - rename u_char/u_short/u_long/u_int types - struct typedef renamed and struct used - minor whitespace changes Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 01cb3fa414735a1529e22e00f7d71501b8470062 Author: Randy Dunlap Date: Wed Mar 21 19:21:39 2007 -0700 ipwireless_cs: use ANSI function declaration Fix function declaration: drivers/char/pcmcia/ipwireless_cs_tty.c:730:29: warning: non-ANSI function declaration of function 'ipwireless_tty_release' Signed-off-by: Randy Dunlap Signed-off-by: Jiri Kosina commit 4b53f3600292722b5c2348f87ffb1d8ad565d670 Author: Michal Rokos Date: Thu Mar 22 10:40:42 2007 +0100 ipwireless_cs: enable autoload of module This makes kernel to load ipwireless_cs module when card is inserted. Signed-off-by: Michal Rokos Signed-off-by: Jiri Kosina commit 24b5d2cd81a98baed86d5245383259fd7d249b7d Author: Jiri Kosina Date: Wed Mar 14 15:16:17 2007 +0100 ipwireless_cs: Remove ipw_hardware_t.in_process_context_loop Conditional workqueue scheduling is absolutely unnecessary and might be racy. Remove it. Signed-off-by: Jiri Kosina commit 486f44c42f27698472a34678a764d05bab594bb0 Author: Jiri Kosina Date: Wed Mar 14 14:02:42 2007 +0100 ipwireless_cs: update driver version Signed-off-by: Jiri Kosina commit c75854d0b7214e43d8a8bb7c40f12551e7cc1136 Author: Jiri Kosina Date: Wed Mar 14 13:38:21 2007 +0100 Revert "ipwireless_cs: make RAS the default channel" This reverts commit d5cbcf3c66d72e5fd8a68dbcd6e148e4abb15ed1. This commit might cause oops due to NULL tty in ipwireless_tty_received() Signed-off-by: Jiri Kosina commit 21764e606902b9088d3ffafdbf6b40f676720c22 Author: Stephen Blackheath Date: Wed Mar 14 13:36:32 2007 +0100 ipwireless_cs: queue ctrl packets along with data packets This patch ensures that the CTRL events are queued along with the DATA events so they are guaranteed to be processed in the correct sequence. This fixes a bug of non-deterministic processing of CTRL and DATA packets in interleaved (and bad) order. Signed-off-by: Stephen Blackheath Signed-off-by: Jiri Kosina commit 3c1569c3657192ee0165f54ecb176f1dd507eb5d Author: Jiri Kosina Date: Wed Feb 7 23:27:46 2007 +0100 ipwireless_cs: remove debugging output The debugging output is no more needed, stripping the packet header works well. Signed-off-by: Jiri Kosina commit 985d6ddf81bdbdce7c08d236846453c4cc33f06f Author: Jiri Kosina Date: Wed Feb 7 17:10:07 2007 +0100 ipwireless_cs: Fix 'unsupported protocol ff03' from pppd Some versions of firmware stuff packets with 0xff 0x03 bytes (which are required on sent packet, but not always present on received packets). Detect such situation and skip the offending bytes. Signed-off-by: Jiri Kosina commit c63d9a73adfbf0deab55f3989a393d825db51e18 Author: Jiri Kosina Date: Thu Feb 1 13:40:36 2007 +0100 ipwireless_cs: convert close_lock to mutex Convert close_lock from semaphore to mutex. Signed-off-by: Jiri Kosina commit 5c07172b10d9a1c2725030cdf26602ca335e606c Author: Jiri Kosina Date: Thu Feb 1 11:58:46 2007 +0100 ipwireless_cs: don't cast ppp_channel.private ppp_channel.private is already void* and therefore shouldn't be cast. Signed-off-by: Jiri Kosina commit 283ee6cc344a51e393622bcc5666bc0b3d96809c Author: Jiri Kosina Date: Thu Feb 1 11:50:48 2007 +0100 ipwireless_cs: fix comments Remove some overcommenting. Signed-off-by: Jiri Kosina commit 95b4587dcd2e51045eca42d466af80f783c56d3d Author: Jiri Kosina Date: Thu Feb 1 11:22:57 2007 +0100 ipwireless_cs: don't zero-initialize static vars Remove zero-initialization of static variables. Signed-off-by: Jiri Kosina commit c0ac22eb83afd4e19b38b75a2b372cec1e519bfe Author: Jiri Kosina Date: Thu Feb 1 11:19:58 2007 +0100 ipwireless_cs: change return value of create_packet() to void* create_packet() is used to create various types of packets. So let it return void* and avoid casting on all callsites. Signed-off-by: Jiri Kosina commit 66469b1fde2c58609773693308f99a3696b33ae1 Author: Adrian Bunk Date: Tue Jan 30 14:37:36 2007 +0100 drivers/char/pcmcia/ipwireless_cs_*: cleanups - proper prototypes for global functions in header files - make the following needlessly global function static: - ipwireless_cs_hardware.c: data_type() - make the following needlessly global struct static: - ipwireless_cs_tty.c: ipw_tty_driver - #if 0 the following unused global functions: - ipwireless_cs_hardware.c: ipwireless_get_config() - ipwireless_cs_tty.c: ipw_flush_buffer() Signed-off-by: Adrian Bunk Signed-off-by: Andrew Morton Signed-off-by: Jiri Kosina commit ed5e0eacb2f7dca8cf6b9b90762f7053953fe323 Author: Jiri Kosina Date: Mon Jan 29 16:15:16 2007 +0100 ipwireless_cs: fix some kmalloc-related coding style isssues - removed casts of kmalloc retval - kmalloc+memeset -> kzalloc Signed-off-by: Jiri Kosina commit 11fdd6b31451e075dc355fa3a5b12eab61248102 Author: Jiri Kosina Date: Tue Jan 23 13:12:09 2007 +0100 ipwireless_cs: Fix stack overflow in debug mode (no. 2) when ipwireless_cs_debug == 1, there is a stack overflow in do_receive_packet(). Fix it by allocating proper space for every byte in the packet. Signed-off-by: Jiri Kosina commit e43840780f05ac2384bd1c377e0f17ac1dfde4b0 Author: Jiri Kosina Date: Mon Jan 22 15:36:33 2007 +0100 ipwireless_cs: Fix stack overflow in debug mode when ipwireless_cs_debug == 1, there is a stack overflow in do_send_fragment(). Fix it by allocating proper space for every byte in the packet fragment. Signed-off-by: Jiri Kosina commit c20c39a879b622244301f347915b377a3dc29ea8 Author: Jiri Kosina Date: Mon Jan 22 15:24:01 2007 +0100 ipwireless_cs: significant code cleanups - interrupt handling splitted into multiple functions - handling of setup packet cleaned up and splitted to be readable - more CodingStyle work Signed-off-by: Jiri Kosina commit 88793b27bfb9e726f9aabae733139a5754a953e1 Author: Jiri Kosina Date: Mon Jan 22 11:51:45 2007 +0100 ipwireless_cs: convert the driver to use in-kernel linked lists Move away from own implementation of linked list in favor of list.h Signed-off-by: Jiri Kosina commit 46403d9b87339894d8a3135abd2caf1f8f1b9fa2 Author: Jiri Kosina Date: Thu Jan 18 14:49:15 2007 +0100 ipwireless_cs: small code cleanups Fixed //-style comments, unified conditions that belong together Signed-off-by: Jiri Kosina commit d5cbcf3c66d72e5fd8a68dbcd6e148e4abb15ed1 Author: Jiri Kosina Date: Thu Jan 18 11:07:40 2007 +0100 ipwireless_cs: make RAS the default channel RAS could be used completely instead of DIALLER channel. This also makes secondary channel unneeded. Signed-off-by: Jiri Kosina commit 94c4bc31025689023353af4806af8eb413b088fd Author: Jiri Kosina Date: Mon Jan 15 15:09:11 2007 +0100 ipwireless_cs: make the V3 card ppp connections work V3 card requires the dial commands to be sent on RAS channel and not DIAL channel - when being sent on DIAL channel, the PPP framer on the card is not configured properly. Signed-off-by: Jiri Kosina commit 53c2ed60d5fd87701a1670d7458ffc7c692742bd Author: Jiri Kosina Date: Fri Jan 12 16:18:18 2007 +0100 ipwireless-cs: some code cleanup The code doesn't comply with CodingStyle too much. Push it a bit in the right direction. Signed-off-by: Jiri Kosina commit 9a71c461fb9552ff8cc00bd383a9142d34bc30e7 Author: Jiri Kosina Date: Fri Jan 12 13:31:50 2007 +0100 ipwireless_cs: initial code merge into the tree Signed-off-by: Jiri Kosina drivers/char/pcmcia/Kconfig | 8 drivers/char/pcmcia/Makefile | 4 drivers/char/pcmcia/ipwireless_cs_hardware.c | 1685 ++++++++++++++++++++ drivers/char/pcmcia/ipwireless_cs_hardware.h | 61 + drivers/char/pcmcia/ipwireless_cs_main.c | 478 ++++++ drivers/char/pcmcia/ipwireless_cs_main.h | 72 + drivers/char/pcmcia/ipwireless_cs_network.c | 499 ++++++ drivers/char/pcmcia/ipwireless_cs_network.h | 51 + drivers/char/pcmcia/ipwireless_cs_setup_protocol.h | 122 + drivers/char/pcmcia/ipwireless_cs_tty.c | 798 +++++++++ drivers/char/pcmcia/ipwireless_cs_tty.h | 45 + 11 files changed, 3823 insertions(+), 0 deletions(-) diff --git a/drivers/char/pcmcia/Kconfig b/drivers/char/pcmcia/Kconfig index f25facd..48dbd86 100644 --- a/drivers/char/pcmcia/Kconfig +++ b/drivers/char/pcmcia/Kconfig @@ -42,6 +42,14 @@ config CARDMAN_4040 PC/SC ifdhandler provided from the Omnikey homepage (http://www.omnikey.com/), or a current development version of OpenCT (http://www.opensc.org/). +config IPWIRELESS_CS + tristate "IPWireless 3G UMTS PCMCIA card support" + depends on PCMCIA + select PPP + help + This is a driver for 3G UMTS PCMCIA card from IPWireless company. In + some countries (for example Czech Republic, T-Mobile ISP) this card + is shipped for service called UMTS 4G. endmenu diff --git a/drivers/char/pcmcia/Makefile b/drivers/char/pcmcia/Makefile index 0aae209..f59c399 100644 --- a/drivers/char/pcmcia/Makefile +++ b/drivers/char/pcmcia/Makefile @@ -4,6 +4,10 @@ # # Makefile for the Linux PCMCIA char device drivers. # +ipwireless_cs-objs := ipwireless_cs_hardware.o ipwireless_cs_main.o ipwireless_cs_network.o ipwireless_cs_tty.o + obj-$(CONFIG_SYNCLINK_CS) += synclink_cs.o obj-$(CONFIG_CARDMAN_4000) += cm4000_cs.o obj-$(CONFIG_CARDMAN_4040) += cm4040_cs.o +obj-$(CONFIG_IPWIRELESS_CS) += ipwireless_cs.o + diff --git a/drivers/char/pcmcia/ipwireless_cs_hardware.c b/drivers/char/pcmcia/ipwireless_cs_hardware.c new file mode 100644 index 0000000..2cd4ae0 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_hardware.c @@ -0,0 +1,1685 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipwireless_cs_hardware.h" +#include "ipwireless_cs_setup_protocol.h" +#include "ipwireless_cs_network.h" +#include "ipwireless_cs_main.h" + +/* Function prototypes */ +static void do_setup_hardware(struct ipw_hardware *ipw); +static void handle_received_SETUP_packet(struct ipw_hardware *ipw, + unsigned int address, + unsigned char *data, int len, + int is_last); +static void ipwireless_setup_timer(unsigned long data); +static void do_io(struct ipw_hardware *hw); +static void do_close_hardware(struct ipw_hardware *hw); +static int is_card_present(struct ipw_hardware *hw); +static void handle_received_CTRL_packet(struct ipw_hardware *hw, + unsigned int channel_idx, unsigned char* data, int len); + +/*#define TIMING_DIAGNOSTICS*/ + +#ifdef TIMING_DIAGNOSTICS + +static struct timing_stats { + unsigned long last_report_time; + unsigned long read_time; + unsigned long write_time; + unsigned long read_bytes; + unsigned long write_bytes; + unsigned long start_time; +}; + +static void start_timing(void) +{ + timing_stats.start_time = jiffies; +} + +static void end_read_timing(unsigned length) +{ + timing_stats.read_time += (jiffies - start_time); + timing_stats.read_bytes += length + 2; + report_timing(); +} + +static void end_write_timing(unsigned length) +{ + timing_stats.write_time += (jiffies - start_time); + timing_stats.write_bytes += length + 2; + report_timing(); +} + +static void report_timing(void) +{ + unsigned long since = jiffies - timing_stats.last_report_time; + + /* If it's been more than one second... */ + if (since >= HZ) { + int first = (timing_stats.last_report_time == 0); + + timing_stats.last_report_time = jiffies; + if (!first) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": %u us elapsed - read %lu bytes in %u us, " + "wrote %lu bytes in %u us\n", + jiffies_to_usecs(since), + timing_stats.read_bytes, + jiffies_to_usecs(timing_stats.read_time), + timing_stats.write_bytes, + jiffies_to_usecs(timing_stats.write_time)); + } + timing_stats.read_time = timing_stats.write_time = 0; + timing_stats.read_bytes = timing_stats.write_bytes = 0; + } +} +#else +static void start_timing(void) { } +static void end_read_timing(unsigned length) { } +static void end_write_timing(unsigned length) { } +#endif + +/* Imported IPW definitions */ + +#define LL_MTU_V1 318 +#define LL_MTU_V2 250 +#define LL_MTU_MAX (LL_MTU_V1>LL_MTU_V2?LL_MTU_V1:LL_MTU_V2) + +#define PRIO_DATA 2 +#define PRIO_CTRL 1 +#define PRIO_SETUP 0 + +/* Addresses */ +#define ADDR_SETUP_PROT 0 + +/* Protocol ids */ +enum { + /* Identifier for the Com Data protocol */ + TL_PROTOCOLID_COM_DATA = 0, + + /* Identifier for the Com Control protocol */ + TL_PROTOCOLID_COM_CTRL = 1, + + /* Identifier for the Setup protocol */ + TL_PROTOCOLID_SETUP = 2 +}; + +/* Number of bytes in NL packet header (can not do + * sizeof(NLPacketHeader) since it's a bitfield) */ +#define NL_FIRST_PACKET_HEADER_SIZE 3 + +/* Number of bytes in NL packet header (can not do + * sizeof(NLPacketHeader) since it's a bitfield) */ +#define NL_FOLLOWING_PACKET_HEADER_SIZE 1 + +struct NLFirstPacketHeader { +#if defined(__BIG_ENDIAN) + unsigned int firstLast:2; + unsigned int address:3; + unsigned int protocol:3; +#else + unsigned int protocol:3; + unsigned int address:3; + unsigned int firstLast:2; +#endif + unsigned char length_lsb; + unsigned char length_msb; +} __attribute__ ((__packed__)); + +struct NLPacketHeader { +#if defined(__BIG_ENDIAN) + unsigned int firstLast:2; + unsigned int address:3; + unsigned int protocol:3; +#else + unsigned int protocol:3; + unsigned int address:3; + unsigned int firstLast:2; +#endif +} __attribute__ ((__packed__)); + +/* Value of 'firstLast' above */ +#define NL_INTERMEDIATE_PACKET 0x0 +#define NL_LAST_PACKET 0x1 +#define NL_FIRST_PACKET 0x2 + +typedef union NLPacket { + /* Network packet header of the first packet (yes, a special case!!!!) */ + struct NLFirstPacketHeader hdrFirst; + /* Network packet header of the following packets (if any) */ + struct NLPacketHeader hdr; + /* Complete network packet (header + data) */ + unsigned char rawpkt[LL_MTU_MAX]; +} __attribute__ ((__packed__)) NLPacket; + +#define HW_VERSION_UNKNOWN -1 +#define HW_VERSION_1 1 +#define HW_VERSION_2 2 + +/* IPW I/O ports */ +#define IOIER 0x00 /* Interrupt Enable Register */ +#define IOIR 0x02 /* Interrupt Source/ACK register */ +#define IODCR 0x04 /* Data Control Register */ +#define IODRR 0x06 /* Data Read Register */ +#define IODWR 0x08 /* Data Write Register */ +#define IOESR 0x0A /* Embedded Driver Status Register */ +#define IORXR 0x0C /* Rx Fifo Register (Host to Embedded) */ +#define IOTXR 0x0E /* Tx Fifo Register (Embedded to Host) */ + +/* I/O ports and bit definitions for version 1 of the hardware */ + +/* IER bits*/ +#define IER_RXENABLED ((unsigned short) 0x1) +#define IER_TXENABLED ((unsigned short) 0x2) + +/* ISR bits */ +#define IR_RXINTR ((unsigned short) 0x1) +#define IR_TXINTR ((unsigned short) 0x2) + +/* DCR bits */ +#define DCR_RXDONE ((unsigned short) 0x1) +#define DCR_TXDONE ((unsigned short) 0x2) +#define DCR_RXRESET ((unsigned short) 0x4) +#define DCR_TXRESET ((unsigned short) 0x8) + +/* I/O ports and bit definitions for version 2 of the hardware */ + +struct MEMCCR { + unsigned short PCCOR; /* Configuration Option Register */ + unsigned short PCCSR; /* Configuration and Status Register */ + unsigned short PCPRR; /* Pin Replacemant Register */ + unsigned short PCSCR; /* Socket and Copy Register */ + unsigned short PCESR; /* Extendend Status Register */ + unsigned short PCIOB; /* I/O Base Register */ +}; + +struct MEMINFREG { + unsigned short MemTX_OLD; /* TX Register (R/W) */ + unsigned short Pad1; + unsigned short MemRXDone; /* RXDone Register (R/W) */ + unsigned short Pad2; + unsigned short MemRX; /* RX Register (R/W) */ + unsigned short Pad3; + unsigned short MemPCIntAck; /* PC interrupt Ack Register (W) */ + unsigned short Pad4; + unsigned long MemCardPresent; /* Mask for Host to check (R) for + CARD_PRESENT_VALUE */ + unsigned short MemTX_NEW; /* TX2 (new) Register (R/W) */ +}; + +#define IODMADPR 0x00 /* DMA Data Port Register (R/W) */ + +#define CARD_PRESENT_VALUE ((unsigned long)0xBEEFCAFEUL) + +#define MEMTX_TX 0x0001 +#define MEMRX_RX 0x0001 +#define MEMRX_RX_DONE 0x0001 +#define MEMRX_PCINTACKK 0x0001 +#define MEMRX_MEMSPURIOUSINT 0x0001 + +#define NL_NUM_OF_PRIORITIES 3 +#define NL_NUM_OF_PROTOCOLS 3 +#define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS + +struct ipw_assembler { + struct ipw_rx_packet *packet; +}; + +struct ipw_hardware { + unsigned int base_port; + short hwVersion; + unsigned short llMTU; + struct ipw_config config; + spinlock_t spinlock; + + int initializing; + int init_loops; + struct timer_list setup_timer; + + int tx_ready; + struct list_head tx_queue[NL_NUM_OF_PRIORITIES]; + /* True if any packets are queued for transmission */ + int tx_queued; + + int rx_bytes_queued; + struct list_head rx_queue; + /* Pool of rx_packet structures that are not currently used. */ + struct list_head rx_pool; + /* True if reception of data is blocked while userspace processes it. */ + int blocking_rx; + /* True if there is RX data ready on the hardware. */ + int rx_ready; + unsigned short last_memtx_serial; + /* + * Newer versions of the V2 card firmware send serial numbers in the + * MemTX register. 'serial_number_detected' is set true when we detect + * a non-zero serial number (indicating the new firmware). Thereafter, + * the driver can safely ignore the Timer Recovery re-sends to avoid + * out-of-sync problems. + */ + int serial_number_detected; + struct work_struct work_rx; + + /* True if we are to send the set-up data to the hardware. */ + int to_setup; + + /* Card has been removed */ + int removed; + /* Saved irq value when we disable the interrupt. */ + int irq; + /* True if some thread is performing hardware I/O */ + int doing_io; + /* True if this driver is shutting down. */ + int shutting_down; + /* Modem control lines */ + unsigned int control_lines[NL_NUM_OF_ADDRESSES]; + struct ipw_assembler packet_assembler[NL_NUM_OF_ADDRESSES]; + + struct tasklet_struct tasklet; + int tasklet_pending; + + /* The handle for the network layer, for the sending of events to it. */ + struct ipw_network *network; + struct MEMINFREG __iomem *memInfReg; + struct MEMCCR __iomem *memCCR; + int bad_interrupt_count; + void (*reboot_callback) (void *data); + void *reboot_callback_data; + + unsigned short __iomem *MemTX; +}; + +/* + * Packet info structure for tx packets. + * Note: not all the fields defined here are required for all protocols + */ +struct ipw_tx_packet { + struct list_head queue; + /* channel idx + 1 */ + unsigned char dest_addr; + /* SETUP, CTRL or DATA */ + unsigned char protocol; + /* Length of data block, which starts at the end of this structure */ + unsigned short length; + /* Function to call upon packet completion. */ + void (*packet_sent_callback) (struct ipw_hardware *ipw, + struct ipw_tx_packet *packet); + /* Sending state */ + /* Offset of where we've sent up to so far */ + unsigned long offset; + /* Count of packet fragments, starting at 0 */ + int fragment_count; + + void (*external_callback) (void *cb, unsigned int length); + void *external_callback_data; +}; + +/* Signals from DTE */ +typedef enum ComCtrl_DTESignal { + ComCtrl_RTS = 0, + ComCtrl_DTR = 1 +} ComCtrl_DTESignal; + +/* Signals from DCE */ +typedef enum ComCtrl_DCESignal { + ComCtrl_CTS = 2, + ComCtrl_DCD = 3, + ComCtrl_DSR = 4, + ComCtrl_RI = 5 +} ComCtrl_DCESignal; + +struct ipw_control_packet_body { + /* ComCtrl_DTESignal or ComCtrl_DCESignal */ + unsigned char sigNo; + /* ComCtrl_SET(0) or ComCtrl_CLEAR(1) */ + unsigned char value; +} __attribute__ ((__packed__)); + +struct ipw_control_packet { + struct ipw_tx_packet packet; + struct ipw_control_packet_body body; +}; + +struct ipw_rx_packet { + struct list_head queue; + unsigned int capacity; + unsigned int length; + unsigned int protocol; + unsigned int channel_idx; +}; + +#ifdef IPWIRELESS_STATE_DEBUG +int ipwireless_dump_hardware_state(char *p, struct ipw_hardware *hw) +{ + int idx = 0; + + idx += sprintf(p + idx, "debug: initializing=%d\n", hw->initializing); + idx += sprintf(p + idx, "debug: tx_ready=%d\n", hw->tx_ready); + idx += sprintf(p + idx, "debug: tx_queued=%d\n", hw->tx_queued); + idx += sprintf(p + idx, "debug: rx_ready=%d\n", hw->rx_ready); + idx += sprintf(p + idx, "debug: rx_bytes_queued=%d\n", + hw->rx_bytes_queued); + idx += sprintf(p + idx, "debug: blocking_rx=%d\n", hw->blocking_rx); + idx += sprintf(p + idx, "debug: removed=%d\n", hw->removed); + idx += sprintf(p + idx, "debug: doing_io=%d\n", hw->doing_io); + idx += sprintf(p + idx, "debug: tasklet_pending=%d\n", + hw->tasklet_pending); + idx += sprintf(p + idx, "debug: hardware.shutting_down=%d\n", + hw->shutting_down); + return idx; +} +#endif + +static char *data_type(const unsigned char *buf, unsigned length) +{ + struct NLPacketHeader *hdr = (struct NLPacketHeader *) buf; + + if (length == 0) + return " "; + + if (hdr->firstLast & NL_FIRST_PACKET) { + if (hdr->protocol == TL_PROTOCOLID_COM_DATA) + return "DATA "; + if (hdr->protocol == TL_PROTOCOLID_COM_CTRL) + return "CTRL "; + if (hdr->protocol == TL_PROTOCOLID_SETUP) + return "SETUP"; + return "???? "; + } else + return " "; +} + +#define DUMP_MAX_BYTES 64 + +/* + * Send a fragment of a packet. + */ +static int do_send_fragment(struct ipw_hardware *hw, const unsigned char *data, + unsigned length) +{ + int i; + + start_timing(); + + /* If length is 0, then there is no work to do - return success. */ + if (length == 0) + return 0; + + /* If length is greater than our MTU, then we fail. */ + if (length > hw->llMTU) + return -1; + + if (ipwireless_cs_debug) { + char buf[DUMP_MAX_BYTES * 4 + 16]; + int i; + + buf[0] = 0; + for (i = 0; i < length && i < DUMP_MAX_BYTES; i++) + sprintf(buf + strlen(buf), " %x ", (unsigned int) data[i]); + if (i > DUMP_MAX_BYTES) + strcat(buf, "..."); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": send fragment %s%s\n", data_type(data, length), buf); + } + + if (hw->hwVersion == HW_VERSION_1) { + outw((unsigned short) length, hw->base_port + IODWR); + + for (i = 0; i < length; i += 2) { + unsigned short d = data[i]; + + if (i + 1 < length) + d |= (((unsigned short) data[i + 1]) << 8); + outw(d, hw->base_port + IODWR); + } + + outw(DCR_TXDONE, hw->base_port + IODCR); + } else if (hw->hwVersion == HW_VERSION_2) { + outw((unsigned short) length, hw->base_port + IODMADPR); + + for (i = 0; i < length; i += 2) { + unsigned short d = data[i]; + + if (i + 1 < length) + d |= (((unsigned short) data[i + 1]) << 8); + outw(d, hw->base_port + IODMADPR); + } + while ((i & 3) != 2) { + outw((unsigned short) 0xDEAD, hw->base_port + IODMADPR); + i += 2; + } + iowrite16(MEMRX_RX, &hw->memInfReg->MemRX); + } + + end_write_timing(length); + + return 0; +} + +/* Called with spinlock in force. It unlocks, then re-locks it. */ +static int do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet) +{ + unsigned short fragment_data_len; + unsigned short data_left = packet->length - packet->offset; + unsigned short header_size; + NLPacket pkt; + + spin_unlock_irq(&hw->spinlock); + header_size = + (packet->fragment_count == 0) + ? NL_FIRST_PACKET_HEADER_SIZE + : NL_FOLLOWING_PACKET_HEADER_SIZE; + fragment_data_len = (unsigned short) hw->llMTU - header_size; + if (data_left < fragment_data_len) + fragment_data_len = data_left; + + pkt.hdrFirst.protocol = packet->protocol; + pkt.hdrFirst.address = packet->dest_addr; + pkt.hdrFirst.firstLast = 0; + + /* First packet? */ + if (packet->fragment_count == 0) { + pkt.hdrFirst.firstLast |= NL_FIRST_PACKET; + pkt.hdrFirst.length_lsb = (unsigned char) packet->length; + pkt.hdrFirst.length_msb = (unsigned char) (packet->length >> 8); + } + + memcpy(pkt.rawpkt + header_size, + ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) + + packet->offset, fragment_data_len); + packet->offset += fragment_data_len; + packet->fragment_count++; + + /* Last packet? (May also be first packet.) */ + if (packet->offset == packet->length) + pkt.hdrFirst.firstLast |= NL_LAST_PACKET; + do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len); + + /* If this packet has unsent data, then re-queue it. */ + if (packet->offset < packet->length) { + spin_lock_irq(&hw->spinlock); + /* + * Re-queue it at the head of the highest priority queue so it goes before + * all other packets + */ + list_add(&packet->queue, &hw->tx_queue[0]); + } else { + (*packet->packet_sent_callback) (hw, packet); + spin_lock_irq(&hw->spinlock); + } + + return 0; +} + +/* + * Free the specified packet. Can be used as a 'packet_sent_callback' + * to free the packet once it has been sent. + */ +static void free_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet) +{ + kfree(packet); +} + +/* + * Free the specified packet. Can be used as a 'packet_sent_callback' + * to free the packet once it has been sent. + */ +static void free_packet_and_callback(struct ipw_hardware *hw, + struct ipw_tx_packet *packet) +{ + if (packet->external_callback) + (*packet->external_callback) (packet->external_callback_data, + packet->length); + + kfree(packet); +} + +/* + * If 'packet' is NULL, then this function allocates a new packet, setting its + * length to 0 and ensuring it has the specified minimum amount of free space. + * + * If 'packet' is not NULL, then this function enlarges it if it doesn't + * have the specified minimum amount of free space. + */ +static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw, + struct ipw_rx_packet *packet, + int minimum_free_space) +{ + unsigned long flags; + + if (!packet) { + /* + * If this is the first fragment, then we will need to fetch a + * packet to put it in. + */ + spin_lock_irqsave(&hw->spinlock, flags); + /* If we have one in our pool, then pull it out. */ + if (!list_empty(&hw->rx_pool)) { + packet = list_entry(hw->rx_pool.next, + struct ipw_rx_packet, queue); + list_del_init(&packet->queue); + spin_unlock_irqrestore(&hw->spinlock, flags); + } else { + /* Otherwise allocate a new one. */ + static int min_capacity = 256; + int new_capacity; + + spin_unlock_irqrestore(&hw->spinlock, flags); + new_capacity = + minimum_free_space > min_capacity + ? minimum_free_space + : min_capacity; + packet = kmalloc(sizeof(struct ipw_rx_packet) + + new_capacity, GFP_ATOMIC); + packet->capacity = new_capacity; + } + packet->length = 0; + } + + /* + * If this packet does not have sufficient capacity for the data we + * want to add, then make it bigger. + */ + if (packet->length + minimum_free_space > packet->capacity) { + struct ipw_rx_packet *old_packet = packet; + + packet = kmalloc(sizeof(struct ipw_rx_packet) + + old_packet->length + minimum_free_space, + GFP_ATOMIC); + memcpy(packet, old_packet, + sizeof(struct ipw_rx_packet) + old_packet->length); + packet->capacity = old_packet->length + minimum_free_space; + kfree(old_packet); + } + + return packet; +} + +static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet) +{ + static int ticker; + + /* + * Every now and again we release one instead of pushing it back onto + * the pool. This isn't perfectly efficient, but it prevents us + * wasting memory. + */ + if ((ticker++ & 0x3f) == 0) + kfree(packet); + else + list_add_tail(&packet->queue, &hw->rx_pool); +} + +static void queue_received_packet(struct ipw_hardware *hw, + unsigned int protocol, unsigned int address, + unsigned char *data, int length, int is_last) +{ + unsigned int channel_idx = address - 1; + struct ipw_rx_packet *packet = NULL; + + /* Discard packet if channel index is out of range. */ + if (channel_idx >= NL_NUM_OF_ADDRESSES) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": data packet has bad address %d\n", address); + return; + } + + if (protocol == TL_PROTOCOLID_COM_DATA) { + struct ipw_assembler *assem = &hw->packet_assembler[channel_idx]; + + /* + * Create a new packet, or if this assembler already contains one, then + * make larger by 'length' bytes. + */ + assem->packet = pool_allocate(hw, assem->packet, length); + assem->packet->protocol = protocol; + assem->packet->channel_idx = channel_idx; + + /* Append this packet data onto existing data. */ + memcpy((unsigned char*)assem->packet + sizeof(struct ipw_rx_packet) + + assem->packet->length, data, length); + assem->packet->length += length; + if (is_last) { + packet = assem->packet; + assem->packet = NULL; + /* Count queued DATA bytes only */ + hw->rx_bytes_queued += packet->length; + } + } + else { + /* If it's a CTRL packet, don't assemble fragments - just queue it. */ + packet = pool_allocate(hw, NULL, length); + packet->protocol = protocol; + packet->channel_idx = channel_idx; + memcpy((unsigned char*)packet + sizeof(struct ipw_rx_packet), data, length); + packet->length = length; + } + + + /* + * If this is the last packet, then send the assembled packet on to the + * network layer. + */ + if (packet) { + unsigned long flags; + + spin_lock_irqsave(&hw->spinlock, flags); + list_add_tail(&packet->queue, &hw->rx_queue); + /* Block reception of incoming packets if queue is full. */ + hw->blocking_rx = hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE; + + spin_unlock_irqrestore(&hw->spinlock, flags); + schedule_work(&hw->work_rx); + } +} + +static void do_receive_data_work(struct work_struct *work_rx) +{ + struct ipw_hardware *hw = + container_of(work_rx, struct ipw_hardware, work_rx); + struct list_head *p, *q; + + spin_lock_irq(&hw->spinlock); + list_for_each_safe(p, q, &hw->rx_queue) { + struct ipw_rx_packet *packet = + list_entry(p, struct ipw_rx_packet, queue); + + if (hw->shutting_down) + break; + list_del_init(p); + + /* + * Note: ipwireless_network_packet_received must be called in a + * process context (i.e. via schedule_work) because the tty + * output code can sleep in the tty_flip_buffer_push call. + */ + if (packet->protocol == TL_PROTOCOLID_COM_DATA) { + if (hw->network != NULL) { + /* If the network hasn't been disconnected. */ + spin_unlock_irq(&hw->spinlock); + ipwireless_network_packet_received( + hw->network, packet->channel_idx, + (unsigned char*)packet + + sizeof(struct ipw_rx_packet), + packet->length); + spin_lock_irq(&hw->spinlock); + } + /* Count queued DATA bytes only */ + hw->rx_bytes_queued -= packet->length; + } else { + spin_unlock_irq(&hw->spinlock); + handle_received_CTRL_packet(hw, packet->channel_idx, + (unsigned char*)packet + + sizeof(struct ipw_rx_packet), + packet->length); + spin_lock_irq(&hw->spinlock); + } + pool_free(hw, packet); + /* + * Unblock reception of incoming packets if queue is no longer + * full. + */ + hw->blocking_rx = hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE; + if (hw->shutting_down) + break; + + do_io(hw); + } + spin_unlock_irq(&hw->spinlock); +} + +static void handle_received_CTRL_packet(struct ipw_hardware *hw, + unsigned int channel_idx, + unsigned char *data, int len) +{ + struct ipw_control_packet_body *body = + (struct ipw_control_packet_body *) data; + unsigned int changed_mask; + + if (len != sizeof(struct ipw_control_packet_body)) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": control packet was %d bytes - wrong size!\n", + len); + return; + } + + switch (body->sigNo) { + case ComCtrl_CTS: + changed_mask = IPW_CONTROL_LINE_CTS; + break; + case ComCtrl_DCD: + changed_mask = IPW_CONTROL_LINE_DCD; + break; + case ComCtrl_DSR: + changed_mask = IPW_CONTROL_LINE_DSR; + break; + case ComCtrl_RI: + changed_mask = IPW_CONTROL_LINE_RI; + break; + default: + changed_mask = 0; + } + + if (changed_mask != 0) { + if (body->value) + hw->control_lines[channel_idx] |= changed_mask; + else + hw->control_lines[channel_idx] &= ~changed_mask; + if (hw->network) + ipwireless_network_notify_control_line_change( + hw->network, + channel_idx, + hw->control_lines[channel_idx], + changed_mask); + } +} + +static void handle_received_packet(struct ipw_hardware *hw, NLPacket *packet, + unsigned short len) +{ + unsigned int protocol = packet->hdr.protocol; + unsigned int address = packet->hdr.address; + unsigned int header_length; + unsigned char *data; + unsigned int data_len; + int is_last = packet->hdr.firstLast & NL_LAST_PACKET; + + if (packet->hdr.firstLast & NL_FIRST_PACKET) + header_length = NL_FIRST_PACKET_HEADER_SIZE; + else + header_length = NL_FOLLOWING_PACKET_HEADER_SIZE; + + data = packet->rawpkt + header_length; + data_len = len - header_length; + switch (protocol) { + case TL_PROTOCOLID_COM_DATA: + case TL_PROTOCOLID_COM_CTRL: + queue_received_packet(hw, protocol, address, data, data_len, + is_last); + break; + case TL_PROTOCOLID_SETUP: + handle_received_SETUP_packet(hw, address, data, data_len, + is_last); + break; + } +} + +/* + * Retrieve a packet from the IPW hardware. + */ +static void do_receive_packet(struct ipw_hardware *hw) +{ + unsigned short len; + unsigned int i; + unsigned char pkt[LL_MTU_MAX]; + + start_timing(); + + if (hw->hwVersion == HW_VERSION_1) { + len = inw(hw->base_port + IODRR); + if (len > hw->llMTU) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": received a packet of %d bytes - " + "longer than the MTU!\n", len); + outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR); + return; + } + + for (i = 0; i < len; i += 2) { + unsigned short data = inw(hw->base_port + IODRR); + + pkt[i] = (unsigned char) data; + pkt[i + 1] = (unsigned char) (data >> 8); + } + } else { + len = inw(hw->base_port + IODMADPR); + if (len > hw->llMTU) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": received a packet of %d bytes - " + "longer than the MTU!\n", len); + iowrite16(MEMRX_PCINTACKK, &hw->memInfReg->MemPCIntAck); + return; + } + + for (i = 0; i < len; i += 2) { + unsigned short data = inw(hw->base_port + IODMADPR); + + pkt[i] = (unsigned char) data; + pkt[i + 1] = (unsigned char) (data >> 8); + } + + while ((i & 3) != 2) { + inw(hw->base_port + IODMADPR); + i += 2; + } + } + + if (ipwireless_cs_debug) { + char buf[DUMP_MAX_BYTES * 4 + 16]; + int i; + + buf[0] = 0; + for (i = 0; i < len && i < DUMP_MAX_BYTES; i++) + sprintf(buf + strlen(buf), " %x ", + (unsigned int) pkt[i]); + if (i > DUMP_MAX_BYTES) + strcat(buf, "..."); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": recv fragment %s%s\n", data_type(pkt, len), buf); + } + + handle_received_packet(hw, (NLPacket *) pkt, len); + /* ACK reading */ + if (hw->hwVersion == HW_VERSION_1) + outw(DCR_RXDONE, hw->base_port + IODCR); + else + iowrite16(MEMRX_PCINTACKK, &hw->memInfReg->MemPCIntAck); + + end_read_timing(len); +} + +/* + * Send queued packets. Must be called with spin_lock locked. + * May be called either from a interrupt tasklet, or from a process context. + */ +static void do_io(struct ipw_hardware *hw) +{ + int no_data_sent = 0; + + /* + * If another thread is already performing I/O on the hardware, then we + * need not do it on this thread, so exit. + */ + if (hw->doing_io || hw->shutting_down) + return; + + /* Tell other threads we have exclusive access to the hardware. */ + hw->doing_io = 1; + while (1) { + int no_data_sent_last_time = no_data_sent; + + no_data_sent = 0; + + /* If we are to send setup data to the hardware... */ + if (hw->to_setup == 1) { + hw->to_setup = 2; + spin_unlock_irq(&hw->spinlock); + do_setup_hardware(hw); + spin_lock_irq(&hw->spinlock); + } else + /* If there is incoming data to be retrieved, retrieve it. */ + if (hw->rx_ready && !hw->blocking_rx) { + hw->rx_ready--; + spin_unlock_irq(&hw->spinlock); + do_receive_packet(hw); + spin_lock_irq(&hw->spinlock); + } else if (hw->tx_queued && hw->tx_ready != 0) { + int another_packet_to_send = 0; + int priority; + + /* + * If we're initializing, don't send anything of higher + * priority than PRIO_SETUP. The network layer + * therefore need not care about hardware + * initialization - any of its stuff will simply be + * queued until setup is complete. + */ + int priority_limit = (hw->to_setup || hw->initializing + ? PRIO_SETUP + 1 : + NL_NUM_OF_PRIORITIES); + + hw->tx_ready--; + + no_data_sent = 1; + for (priority = 0; priority < priority_limit; priority++) + if (!list_empty (&hw->tx_queue[priority])) { + struct list_head *p = + hw->tx_queue[priority].next; + struct ipw_tx_packet *packet = + list_entry(p, struct ipw_tx_packet, queue); + + list_del_init(p); + + no_data_sent = 0; + + /* + * do_send_packet() unlocks, then + * re-locks the spinlock. + */ + do_send_packet(hw, packet); + break; + } + + /* + * Now that we've sent (or not sent) a packet, see if + * there's any more to send after that (include all + * data, including non-setup data if we are setting up. + */ + for (priority = 0; priority < NL_NUM_OF_PRIORITIES; priority++) + if (!list_empty (&hw->tx_queue[priority])) { + another_packet_to_send = 1; + break; + } + + /* + * If there are no more packets queued to be sent after + * the present one... + */ + if (!another_packet_to_send) + hw->tx_queued = 0; + + /* + * The logic above would produce an endless loop when + * hw->to_setup is non-zero, and there is non-setup + * data (>PRIO_SETUP) queued, but no PRIO_SETUP data. + * The if below protects against this. + */ + if (no_data_sent && no_data_sent_last_time) + break; + } + /* If we've run out of things to do, then stop looping. */ + else + break; + } + hw->doing_io = 0; +} + +static void ipwireless_do_tasklet(unsigned long hw_) +{ + struct ipw_hardware *hw = (struct ipw_hardware *) hw_; + + spin_lock_irq(&hw->spinlock); + do_io(hw); + hw->tasklet_pending = 0; + spin_unlock_irq(&hw->spinlock); +} + +/*! + * @return true if the card is physically present. + */ +static int is_card_present(struct ipw_hardware *hw) +{ + if (hw->hwVersion == HW_VERSION_1) + return inw(hw->base_port + IOIR) != (unsigned short) 0xFFFF; + else + return ioread32(&hw->memInfReg->MemCardPresent) == + CARD_PRESENT_VALUE; +} + +static irqreturn_t ipwireless_handle_v1_interrupt(int irq, + struct ipw_hardware *hw) +{ + unsigned short irqn; + unsigned short ack; + + irqn = inw(hw->base_port + IOIR); + + if (irqn == (unsigned short) 0xFFFF) { + if (++hw->bad_interrupt_count >= 100) { + /* + * It is necessary to disable the interrupt at this + * point, or the kernel hangs, interrupting repeatedly + * forever. + */ + hw->irq = irq; + hw->removed = 1; + disable_irq_nosync(irq); + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": Mr. Fluffy is not happy!\n"); + } + return IRQ_HANDLED; + } else if (irqn != 0) { + ack = 0; + /* Transmit complete. */ + if (irqn & IR_TXINTR) { + hw->tx_ready++; + ack |= IR_TXINTR; + } + + /* Received data */ + if (irqn & IR_RXINTR) { + ack |= IR_RXINTR; + hw->rx_ready++; + } + if (ack != 0) { + outw(ack, hw->base_port + IOIR); + + /* + * Perform the I/O retrieval in a tasklet, because the + * ppp_generic may be called from a tasklet, but not + * from a hardware interrupt. + */ + if (!hw->tasklet_pending) { + hw->tasklet_pending = 1; + tasklet_schedule(&hw->tasklet); + } + } + return IRQ_HANDLED; + } else + return IRQ_NONE; + +} + +static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq, + struct ipw_hardware *hw) +{ + int tx = 0; + int rx = 0; + int rx_repeat = 0; + unsigned short memtx = ioread16(hw->MemTX); + unsigned short memtx_serial; + unsigned short memrxdone = ioread16(&hw->memInfReg->MemRXDone); + + /* check whether the interrupt was generated by ipwireless card */ + if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) + return IRQ_NONE; + + /* + * See if the card is physically present. Note that while it is + * powering up, it appears not to be present. + */ + if (ioread32(&hw->memInfReg->MemCardPresent) != CARD_PRESENT_VALUE) { + unsigned short csr = ioread16(&hw->memCCR->PCCSR); + + csr &= 0xfffd; + iowrite16(csr, &hw->memCCR->PCCSR); + return IRQ_HANDLED; + } + + memtx_serial = memtx & (unsigned short) 0xff00; + if (memtx & MEMTX_TX) { + iowrite16(0, hw->MemTX); + + if (hw->serial_number_detected) { + if (memtx_serial != hw->last_memtx_serial) { + hw->last_memtx_serial = memtx_serial; + hw->rx_ready++; + rx = 1; + } else + /* Ignore 'Timer Recovery' duplicates. */ + rx_repeat = 1; + } else { + /* + * If a non-zero serial number is seen, then enable + * serial number checking. + */ + if (memtx_serial != 0) + hw->serial_number_detected = 1; + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": MemTX serial number detected\n"); + + hw->rx_ready++; + rx = 1; + } + } + if (memrxdone & MEMRX_RX_DONE) { + iowrite16(0, &hw->memInfReg->MemRXDone); + hw->tx_ready++; + tx = 1; + } + if (tx) + iowrite16(MEMRX_PCINTACKK, &hw->memInfReg->MemPCIntAck); + + /* Acknowledge interrupt at PCMCIA level. */ + iowrite16(ioread16(&hw->memCCR->PCCSR) & 0xfffd, &hw->memCCR->PCCSR); + + if (tx || rx) { + /* + * Perform the I/O retrieval in a tasklet, because the + * ppp_generic may be called from a tasklet, but not from a + * hardware interrupt. + */ + if (!hw->tasklet_pending) { + hw->tasklet_pending = 1; + tasklet_schedule(&hw->tasklet); + } + } else if (!rx_repeat) { + if (hw->MemTX == &hw->memInfReg->MemTX_NEW) { + printk(KERN_WARNING IPWIRELESS_PCCARD_NAME + ": No valid MemTX value - switching " + "to the old MemTX register address.\n"); + hw->MemTX = &hw->memInfReg->MemTX_OLD; + goto out; + } else { + printk(KERN_WARNING IPWIRELESS_PCCARD_NAME + ": spurious interrupt!\n"); + hw->irq = irq; + hw->removed = 1; + disable_irq_nosync(irq); + } + } +out: + return IRQ_HANDLED; +} + +irqreturn_t ipwireless_interrupt(int irq, void *dev_id, struct pt_regs *regs) +{ + struct ipw_hardware *hw = (struct ipw_hardware *) dev_id; + + if (hw->hwVersion == HW_VERSION_1) + return ipwireless_handle_v1_interrupt(irq, hw); + else + return ipwireless_handle_v2_v3_interrupt(irq, hw); +} + +static void send_packet(struct ipw_hardware *hw, int priority, + struct ipw_tx_packet *packet) +{ + struct list_head *p = &hw->tx_queue[priority]; + + spin_lock_irq(&hw->spinlock); + list_add_tail(&packet->queue, p); + hw->tx_queued = 1; + /* + * If we happen to have got here as a result of processing from the + * do_io() loop, then do_io() will detect this situation and do + * nothing. + */ + do_io(hw); + spin_unlock_irq(&hw->spinlock); +} + +static void *create_packet(int packet_size, + unsigned char dest_addr, + unsigned char protocol, + void (*packet_sent_callback) (struct ipw_hardware * + ipw, + struct ipw_tx_packet * + packet)) +{ + struct ipw_tx_packet *packet = kzalloc(packet_size, GFP_ATOMIC); + + INIT_LIST_HEAD(&packet->queue); + packet->dest_addr = dest_addr; + packet->protocol = protocol; + packet->packet_sent_callback = packet_sent_callback; + packet->length = packet_size - sizeof(struct ipw_tx_packet); + return packet; +} + +void ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx, + unsigned char *data, unsigned int length, + void (*callback) (void *cb, unsigned int length), + void *callback_data) +{ + struct ipw_tx_packet *packet; + unsigned int flags; + + local_irq_save(flags); + packet = create_packet(sizeof(struct ipw_tx_packet) + length, + (unsigned char) (channel_idx + 1), + TL_PROTOCOLID_COM_DATA, + free_packet_and_callback); + packet->external_callback = callback; + packet->external_callback_data = callback_data; + memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data, length); + send_packet(hw, PRIO_DATA, packet); + local_irq_restore(flags); +} + +static void setControlLine(struct ipw_hardware *hw, int gfp_flag, + unsigned int channel_idx, int line, int state) +{ + struct ipw_control_packet *packet; + + packet = create_packet(sizeof(struct ipw_control_packet), + (unsigned char) (channel_idx + 1), + TL_PROTOCOLID_COM_CTRL, free_packet); + packet->packet.length = sizeof(struct ipw_control_packet_body); + packet->body.sigNo = (unsigned char) line; + packet->body.value = (unsigned char) (state == 0 ? 0 : 1); + send_packet(hw, PRIO_CTRL, &packet->packet); +} + +static void setDTR(struct ipw_hardware *hw, int priority, + unsigned int channel_idx, int state) +{ + if (state != 0) + hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR; + else + hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR; + + setControlLine(hw, priority, channel_idx, ComCtrl_DTR, state); +} + +static void setRTS(struct ipw_hardware *hw, int priority, + unsigned int channel_idx, int state) +{ + if (state != 0) + hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS; + else + hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS; + + setControlLine(hw, priority, channel_idx, ComCtrl_RTS, state); +} + +void ipwireless_setDTR(struct ipw_hardware *hw, unsigned int channel_idx, + int state) +{ + setDTR(hw, PRIO_CTRL, channel_idx, state); +} + +void ipwireless_setRTS(struct ipw_hardware *hw, unsigned int channel_idx, + int state) +{ + setRTS(hw, PRIO_CTRL, channel_idx, state); +} + +typedef struct ipw_setup_get_version_query_packet_t { + struct ipw_tx_packet packet; + TlSetupGetVersionQry body; +} ipw_setup_get_version_query_packet_t; + +typedef struct ipw_setup_config_packet_t { + struct ipw_tx_packet packet; + TlSetupConfigMsg body; +} ipw_setup_config_packet_t; + +typedef struct ipw_setup_config_done_packet_t { + struct ipw_tx_packet packet; + TlSetupConfigDoneMsg body; +} ipw_setup_config_done_packet_t; + +typedef struct ipw_setup_open_packet_t { + struct ipw_tx_packet packet; + TlSetupOpenMsg body; +} ipw_setup_open_packet_t; + +typedef struct ipw_setup_info_packet_t { + struct ipw_tx_packet packet; + TlSetupInfoMsg body; +} ipw_setup_info_packet_t; + +typedef struct { + struct ipw_tx_packet packet; + TlSetupRebootMsgAck body; +} ipw_setup_reboot_msg_ack_t; + +/* This handles the actual initialization of the card */ +static void __handle_setup_get_version_rsp(struct ipw_hardware *hw) +{ + ipw_setup_config_packet_t *config_packet; + ipw_setup_config_done_packet_t *config_done_packet; + ipw_setup_open_packet_t *open_packet; + ipw_setup_info_packet_t *info_packet; + int port; + unsigned int channel_idx; + + /* generate config packet */ + for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) { + config_packet = create_packet(sizeof(ipw_setup_config_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + config_packet->packet. length = sizeof(TlSetupConfigMsg); + config_packet->body.sigNo = TL_SETUP_SIGNO_CONFIG_MSG; + config_packet->body.portNo = port; + config_packet->body.prioData = PRIO_DATA; + config_packet->body.prioCtrl = PRIO_CTRL; + send_packet(hw, PRIO_SETUP, &config_packet->packet); + } + config_done_packet = create_packet(sizeof(ipw_setup_config_done_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + config_done_packet->packet.length = sizeof(TlSetupConfigDoneMsg); + config_done_packet->body.sigNo = TL_SETUP_SIGNO_CONFIG_DONE_MSG; + send_packet(hw, PRIO_SETUP, &config_done_packet->packet); + + /* generate open packet */ + for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) { + open_packet = create_packet(sizeof(ipw_setup_open_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + open_packet->packet.length = sizeof(TlSetupOpenMsg); + open_packet->body.sigNo = TL_SETUP_SIGNO_OPEN_MSG; + open_packet->body.portNo = port; + send_packet(hw, PRIO_SETUP, &open_packet->packet); + } + for (channel_idx = 0; channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) { + setDTR(hw, PRIO_SETUP, channel_idx, + (hw->control_lines[channel_idx] & IPW_CONTROL_LINE_DTR) != 0); + setRTS(hw, PRIO_SETUP, channel_idx, + (hw->control_lines [channel_idx] & IPW_CONTROL_LINE_RTS) != 0); + } + /* + * For NDIS we assume that we are using sync PPP frames, for COM async. + * This driver uses NDIS mode too. We don't bother with translation + * from async -> sync PPP. + */ + info_packet = create_packet(sizeof(ipw_setup_info_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + info_packet->packet.length = sizeof(TlSetupInfoMsg); + info_packet->body.sigNo = TL_SETUP_SIGNO_INFO_MSG; + info_packet->body.driverType = NDISWAN_DRIVER; + info_packet->body.majorVersion = NDISWAN_DRIVER_MAJOR_VERSION; + info_packet->body.minorVersion = NDISWAN_DRIVER_MINOR_VERSION; + send_packet(hw, PRIO_SETUP, &info_packet->packet); + + /* Initialization is now complete, so we clear the 'to_setup' flag */ + hw->to_setup = 0; +} + +static void handle_setup_get_version_rsp(struct ipw_hardware *hw, + unsigned char vers_no) +{ + del_timer(&hw->setup_timer); + hw->initializing = 0; + printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n"); + + if (vers_no == TL_SETUP_VERSION) + __handle_setup_get_version_rsp(hw); + else + printk(KERN_ERR + IPWIRELESS_PCCARD_NAME + ": invalid hardware version no %u\n", + (unsigned int) vers_no); +} + +static void handle_received_SETUP_packet(struct ipw_hardware *hw, + unsigned int address, + unsigned char *data, int len, + int is_last) +{ + SETUP_RX_MSG *rx_msg = (SETUP_RX_MSG *) data; + + if (address != ADDR_SETUP_PROT) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": setup packet has bad address %d\n", address); + return; + } + + switch (rx_msg->sigNo) { + case TL_SETUP_SIGNO_GET_VERSION_RSP: + if (hw->to_setup) + handle_setup_get_version_rsp(hw, + rx_msg->VersRspMsg.version); + break; + + case TL_SETUP_SIGNO_OPEN_MSG: + if (ipwireless_cs_debug) { + unsigned int channel_idx = rx_msg->OpenMsg.portNo - 1; + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": OPEN_MSG [channel %u] reply received\n", + channel_idx); + } + break; + + case TL_SETUP_SIGNO_INFO_MSG_ACK: + if (ipwireless_cs_debug) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": card successfully configured as driver type NDISWAN\n"); + break; + + case TL_SETUP_SIGNO_REBOOT_MSG: + if (hw->to_setup) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": Ignoring REBOOT message since setup has not completed\n"); + else { + ipw_setup_reboot_msg_ack_t *packet; + + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": Acknowledging REBOOT message\n"); + packet = create_packet(sizeof(ipw_setup_reboot_msg_ack_t), + ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP, free_packet); + packet->packet.length = sizeof(TlSetupRebootMsgAck); + packet->body.sigNo = TL_SETUP_SIGNO_REBOOT_MSG_ACK; + send_packet(hw, PRIO_SETUP, &packet->packet); + if (hw->reboot_callback != NULL) + (hw->reboot_callback) (hw->reboot_callback_data); + } + break; + + default: + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": unknown setup message %u received\n", + (unsigned int) rx_msg->sigNo); + } +} + +static void do_setup_hardware(struct ipw_hardware *hw) +{ + ipw_setup_get_version_query_packet_t *ver_packet; + + if (hw->hwVersion == HW_VERSION_1) { + /* Reset RX FIFO */ + outw(DCR_RXRESET, hw->base_port + IODCR); + /* SB: Reset TX FIFO */ + outw(DCR_TXRESET, hw->base_port + IODCR); + + /* Enable TX and RX interrupts. */ + outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER); + } else { + /* + * Set INTRACK bit (bit 0), which means we must explicitly + * acknowledge interrupts by clearing bit 2 of PCCSR. + */ + unsigned short csr = ioread16(&hw->memCCR->PCCSR); + + csr |= 1; + iowrite16(csr, &hw->memCCR->PCCSR); + } + + ver_packet = create_packet(sizeof(ipw_setup_get_version_query_packet_t), + ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP, free_packet); + ver_packet->body.sigNo = TL_SETUP_SIGNO_GET_VERSION_QRY; + ver_packet->packet.length = sizeof(TlSetupGetVersionQry); + + send_packet(hw, PRIO_SETUP, &ver_packet->packet); + + /* + * The response to this version request is handled in + * handle_received_SETUP_packet + */ +} + +static void do_close_hardware(struct ipw_hardware *hw) +{ + unsigned int irqn; + + if (hw->hwVersion == HW_VERSION_1) { + /* Disable TX and RX interrupts. */ + outw(0, hw->base_port + IOIER); + + /* Acknowledge any outstanding interrupt requests */ + irqn = inw(hw->base_port + IOIR); + if (irqn & IR_TXINTR) + outw(IR_TXINTR, hw->base_port + IOIR); + if (irqn & IR_RXINTR) + outw(IR_RXINTR, hw->base_port + IOIR); + } +} + +struct ipw_hardware *ipwireless_hardware_create(struct ipw_config *config) +{ + int i; + struct ipw_hardware *hw = + kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL); + + if (!hw) + return NULL; + + hw->initializing = 1; + hw->tx_ready = 1; + memcpy(&hw->config, config, sizeof(struct ipw_config)); + for (i = 0; i < NL_NUM_OF_PRIORITIES; i++) + INIT_LIST_HEAD(&hw->tx_queue[i]); + + hw->rx_bytes_queued = 0; + INIT_LIST_HEAD(&hw->rx_queue); + INIT_LIST_HEAD(&hw->rx_pool); + + spin_lock_init(&hw->spinlock); + tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw); + + init_timer(&hw->setup_timer); + hw->setup_timer.function = ipwireless_setup_timer; + hw->setup_timer.data = (unsigned long) hw; + + INIT_WORK(&hw->work_rx, do_receive_data_work); + + hw->last_memtx_serial = (unsigned short) 0xffff; + + return hw; +} + +void ipwireless_init_hardware1(struct ipw_hardware *hw, + unsigned int base_port, + void __iomem *attr_memory, + void __iomem *common_memory, + int isV2Card, + void (*reboot_callback) (void *data), + void *reboot_callback_data) +{ + hw->bad_interrupt_count = 0; + if (hw->removed) { + hw->removed = 0; + enable_irq(hw->irq); + } + hw->base_port = base_port; + hw->hwVersion = isV2Card ? HW_VERSION_2 : HW_VERSION_1; + hw->llMTU = hw->hwVersion == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2; + hw->memCCR = (struct MEMCCR __iomem *) + ((unsigned short __iomem *) attr_memory + 0x200); + hw->memInfReg = (struct MEMINFREG __iomem *) common_memory; + hw->MemTX = &hw->memInfReg->MemTX_NEW; + hw->reboot_callback = reboot_callback; + hw->reboot_callback_data = reboot_callback_data; +} + +void ipwireless_init_hardware2(struct ipw_hardware *hw) +{ + hw->initializing = 1; + hw->init_loops = 0; + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": waiting for card to start up...\n"); + ipwireless_setup_timer((unsigned long) hw); +} + +static void ipwireless_setup_timer(unsigned long data) +{ + struct ipw_hardware *hw = (struct ipw_hardware *) data; + + hw->init_loops++; + /* Give up after a certain number of retries */ + if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": card failed to start up!\n"); + hw->initializing = 0; + } else { + unsigned long flags; + + /* Do not attempt to write to the board if it is not present. */ + if (is_card_present(hw)) { + /* + * Perform the I/O retrieval in a tasklet, because + * ppp_generic may be called from a tasklet, but not + * from a hardware interrupt. + */ + hw->to_setup = 1; + hw->tx_ready = 1; + spin_lock_irqsave(&hw->spinlock, flags); + if (!hw->tasklet_pending) { + hw->tasklet_pending = 1; + tasklet_schedule(&hw->tasklet); + } + spin_unlock_irqrestore(&hw->spinlock, flags); + } + + hw->setup_timer.expires = + jiffies + (unsigned long) TL_SETUP_VERSION_QRY_TMO * HZ / 1000UL; + add_timer(&hw->setup_timer); + } +} + +/* + * Stop any interrupts from executing so that, once this function returns, + * other layers of the driver can be sure they won't get any more callbacks. + * Thus must be called on a proper process context. + */ +void ipwireless_stop_interrupts(struct ipw_hardware *hw) +{ + if (!hw->shutting_down) { + /* Tell everyone we are going down. */ + hw->shutting_down = 1; + del_timer(&hw->setup_timer); + + /* Prevent the hardware from sending any more interrupts */ + do_close_hardware(hw); + } +} + +void ipwireless_hardware_free(struct ipw_hardware *hw) +{ + int i; + struct list_head *p, *q; + + ipwireless_stop_interrupts(hw); + + flush_scheduled_work(); + + for (i = 0; i < NL_NUM_OF_ADDRESSES; i++) + if (hw->packet_assembler[i].packet != NULL) + kfree(hw->packet_assembler[i].packet); + + for (i = 0; i < NL_NUM_OF_PRIORITIES; i++) + list_for_each_safe(p, q, &hw->tx_queue[i]){ + struct ipw_tx_packet *packet = + list_entry(p, struct ipw_tx_packet, queue); + + list_del_init(p); + free_packet(hw, packet); + } + + list_for_each_safe(p, q, &hw->rx_queue) { + struct ipw_rx_packet *packet = + list_entry(p, struct ipw_rx_packet, queue); + + list_del_init(p); + kfree(packet); + } + + list_for_each_safe(p, q, &hw->rx_pool) { + struct ipw_rx_packet *packet = + list_entry(p, struct ipw_rx_packet, queue); + + list_del_init(p); + kfree(packet); + } + kfree(hw); +} + +/* + * Associate the specified network with this hardware, so it will receive events + * from it. + */ +void ipwireless_associate_network(struct ipw_hardware *hw, + struct ipw_network *network) +{ + hw->network = network; +} diff --git a/drivers/char/pcmcia/ipwireless_cs_hardware.h b/drivers/char/pcmcia/ipwireless_cs_hardware.h new file mode 100644 index 0000000..00d3ebf --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_hardware.h @@ -0,0 +1,61 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#ifndef _IPWIRELESS_CS_HARDWARE_H_ +#define _IPWIRELESS_CS_HARDWARE_H_ + +#include +#include +#include + +#define IPW_CONTROL_LINE_CTS 0x0001 +#define IPW_CONTROL_LINE_DCD 0x0002 +#define IPW_CONTROL_LINE_DSR 0x0004 +#define IPW_CONTROL_LINE_RI 0x0008 +#define IPW_CONTROL_LINE_DTR 0x0010 +#define IPW_CONTROL_LINE_RTS 0x0020 + +struct ipw_config; +struct ipw_hardware; +struct ipw_network; + +struct ipw_hardware *ipwireless_hardware_create(struct ipw_config *cfg); +void ipwireless_hardware_free(struct ipw_hardware *hw); +irqreturn_t ipwireless_interrupt(int irq, void *dev_id, struct pt_regs *regs); +void ipwireless_setDTR(struct ipw_hardware *hw, unsigned int channel_idx, + int state); +void ipwireless_setRTS(struct ipw_hardware *hw, unsigned int channel_idx, + int state); +void ipwireless_send_packet(struct ipw_hardware *hw, + unsigned int channel_idx, + unsigned char *data, + unsigned int length, + void (*packet_sent_callback) (void *cb, + unsigned int length), + void *sent_cb_data); +void ipwireless_associate_network(struct ipw_hardware *hw, + struct ipw_network *net); +void ipwireless_stop_interrupts(struct ipw_hardware *hw); +void ipwireless_init_hardware1(struct ipw_hardware *hw, + unsigned int base_port, + void __iomem *attr_memory, + void __iomem *common_memory, + int isV2Card, + void (*reboot_cb) (void *data), + void *reboot_cb_data); +void ipwireless_init_hardware2(struct ipw_hardware *hw); +void ipwireless_sleep(unsigned int tenths); +int ipwireless_dump_hardware_state(char *p, struct ipw_hardware *hw); + +#endif diff --git a/drivers/char/pcmcia/ipwireless_cs_main.c b/drivers/char/pcmcia/ipwireless_cs_main.c new file mode 100644 index 0000000..8bde951 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_main.c @@ -0,0 +1,478 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#include "ipwireless_cs_hardware.h" +#include "ipwireless_cs_network.h" +#include "ipwireless_cs_main.h" +#include "ipwireless_cs_tty.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +static struct ipw_config default_config = { + "*99#" /* phone number */ +}; + +static struct pcmcia_device_id ipw_ids[] = { + PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100), + PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200), + PCMCIA_DEVICE_NULL +}; +MODULE_DEVICE_TABLE(pcmcia, ipw_ids); + +static void ipwireless_detach(struct pcmcia_device *link); + +/* Options configurable through the proc filesystem. */ +__u16 ipwireless_cs_debug; +__u16 ipwireless_cs_vendor; +__u16 ipwireless_cs_loopback; +__u16 ipwireless_cs_out_queue = 1; +static int major; + +module_param(major, int, 0); +MODULE_PARM_DESC(major, "ttyIPWp major number [0]"); + +static char *drv_name = IPWIRELESS_PCCARD_NAME; +static struct pcmcia_device *dev_list; + +/* Executes in process context. */ +static void signalled_reboot_work(struct work_struct *work_reboot) +{ + struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev, work_reboot); + struct pcmcia_device *link = ipw->link; + int ret = pccard_reset_card(link->socket); + + if (ret != CS_SUCCESS) + cs_error(link, ResetCard, ret); +} + +static void signalled_reboot_callback(void *callback_data) +{ + struct ipw_dev *ipw = (struct ipw_dev *) callback_data; + + /* Delegate to process context. */ + schedule_work(&ipw->work_reboot); +} + +static int config_ipwireless(struct ipw_dev * ipw) +{ + struct pcmcia_device *link = ipw->link; + int ret; + config_info_t conf; + tuple_t tuple; + unsigned short buf[64]; + cisparse_t parse; + unsigned short cor_value; + win_req_t reqAM; + win_req_t reqCM; + memreq_t memAM; + memreq_t memCM; + + ipw->isV2Card = 0; + + tuple.Attributes = 0; + tuple.TupleData = (cisdata_t *) buf; + tuple.TupleDataMax = sizeof(buf); + tuple.TupleOffset = 0; + + tuple.DesiredTuple = RETURN_FIRST_TUPLE; + + ret = pcmcia_get_first_tuple(link, &tuple); + + while (ret == 0) { + ret = pcmcia_get_tuple_data(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetTupleData, ret); + goto exit0; + } + ret = pcmcia_get_next_tuple(link, &tuple); + } + + tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; + + ret = pcmcia_get_first_tuple(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetFirstTuple, ret); + goto exit0; + } + + ret = pcmcia_get_tuple_data(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetTupleData, ret); + goto exit0; + } + + ret = pcmcia_parse_tuple(link, &tuple, &parse); + + if (ret != CS_SUCCESS) { + cs_error(link, ParseTuple, ret); + goto exit0; + } + + link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; + link->io.BasePort1 = parse.cftable_entry.io.win[0].base; + link->io.NumPorts1 = parse.cftable_entry.io.win[0].len; + link->io.IOAddrLines = 16; + + link->irq.IRQInfo1 = parse.cftable_entry.irq.IRQInfo1; + link->irq.IRQInfo2 = parse.cftable_entry.irq.IRQInfo2; + + /* 0x40 causes it to generate level mode interrupts. */ + /* 0x04 enables IREQ pin. */ + cor_value = parse.cftable_entry.index | 0x44; + link->conf.ConfigIndex = cor_value; + + /* IRQ and I/O settings */ + tuple.DesiredTuple = CISTPL_CONFIG; + + ret = pcmcia_get_first_tuple(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetFirstTuple, ret); + goto exit0; + } + + ret = pcmcia_get_tuple_data(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetTupleData, ret); + goto exit0; + } + + ret = pcmcia_parse_tuple(link, &tuple, &parse); + + if (ret != CS_SUCCESS) { + cs_error(link, GetTupleData, ret); + goto exit0; + } + link->conf.Attributes = CONF_ENABLE_IRQ; + link->conf.ConfigBase = parse.config.base; + link->conf.Present = parse.config.rmask[0]; + link->conf.IntType = INT_MEMORY_AND_IO; + + link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT; + link->irq.Handler = ipwireless_interrupt; + link->irq.Instance = ipw->hardware; + + ret = pcmcia_request_io(link, &link->io); + + if (ret != CS_SUCCESS) { + cs_error(link, RequestIO, ret); + goto exit0; + } + + /* memory settings */ + + tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; + + ret = pcmcia_get_first_tuple(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetFirstTuple, ret); + goto exit1; + } + + ret = pcmcia_get_tuple_data(link, &tuple); + + if (ret != CS_SUCCESS) { + cs_error(link, GetTupleData, ret); + goto exit1; + } + + ret = pcmcia_parse_tuple(link, &tuple, &parse); + + if (ret != CS_SUCCESS) { + cs_error(link, ParseTuple, ret); + goto exit1; + } + + if (parse.cftable_entry.mem.nwin > 0) { + reqCM.Attributes = + WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE; + reqCM.Base = parse.cftable_entry.mem.win[0].host_addr; + reqCM.Size = parse.cftable_entry.mem.win[0].len; + if (reqCM.Size < 0x1000) + reqCM.Size = 0x1000; + reqCM.AccessSpeed = 0; + + ret = pcmcia_request_window(&link, &reqCM, &ipw->handleCM); + + if (ret != CS_SUCCESS) { + cs_error(link, RequestWindow, ret); + goto exit1; + } + + memCM.CardOffset = parse.cftable_entry.mem.win[0].card_addr; + memCM.Page = 0; + + ret = pcmcia_map_mem_page(ipw->handleCM, &memCM); + + if (ret != CS_SUCCESS) { + cs_error(link, MapMemPage, ret); + goto exit1; + } + + ipw->isV2Card = + parse.cftable_entry.mem.win[0].len == 0x100; + + ipw->commonMemory = ioremap(reqCM.Base, reqCM.Size); + + reqAM.Attributes = + WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE; + reqAM.Base = 0; + reqAM.Size = 0; /* this used to be 0x1000 */ + reqAM.AccessSpeed = 0; + + ret = pcmcia_request_window(&link, &reqAM, &ipw->handleAM); + + if (ret != CS_SUCCESS) { + cs_error(link, RequestWindow, ret); + goto exit2; + } + + memAM.CardOffset = 0; + memAM.Page = 0; + + ret = pcmcia_map_mem_page(ipw->handleAM, &memAM); + + if (ret != CS_SUCCESS) { + cs_error(link, MapMemPage, ret); + goto exit2; + } + + ipw->attrMemory = ioremap(reqAM.Base, reqAM.Size); + } + + INIT_WORK(&ipw->work_reboot, signalled_reboot_work); + + ipwireless_init_hardware1(ipw->hardware, link->io.BasePort1, + ipw->attrMemory, ipw->commonMemory, + ipw->isV2Card, signalled_reboot_callback, + ipw); + + ret = pcmcia_request_irq(link, &link->irq); + + if (ret != CS_SUCCESS) { + cs_error(link, RequestIRQ, ret); + goto exit3; + } + + /* Look up current Vcc */ + + ret = pcmcia_get_configuration_info(link, &conf); + + if (ret != CS_SUCCESS) { + cs_error(link, GetConfigurationInfo, ret); + goto exit4; + } + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n", + ipw->isV2Card ? "V2/V3" : "V1"); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": I/O ports 0x%04x-0x%04x, irq %d\n", + (unsigned int) link->io.BasePort1, + (unsigned int) (link->io.BasePort1 + link->io.NumPorts1 - + 1), (unsigned int) link->irq.AssignedIRQ); + if (ipw->attrMemory && ipw->commonMemory) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": attr memory 0x%08lx-0x%08lx, " + "common memory 0x%08lx-0x%08lx\n", + (unsigned long)reqAM.Base, + (unsigned long)reqAM.Base + (unsigned long)reqAM.Size - + (unsigned long) 1, (unsigned long) reqCM.Base, + (unsigned long)reqCM.Base + (unsigned long)reqCM.Size - + (unsigned long) 1); + } + + ipw->network = ipwireless_network_create(ipw->hardware); + ipw->tty = ipwireless_tty_create(link, ipw->hardware, + ipw->network, ipw->nodes); + + ipwireless_init_hardware2(ipw->hardware); + + /* + * Do the RequestConfiguration last, because it enables interrupts. + * Then we don't get any interrupts before we're ready for them. + */ + ret = pcmcia_request_configuration(link, &link->conf); + + if (ret != CS_SUCCESS) { + cs_error(link, RequestConfiguration, ret); + goto exit4; + } + + link->dev_node = &ipw->nodes[0]; + + return 0; + +exit4: + pcmcia_disable_device(link); +exit3: + if (ipw->attrMemory) { + iounmap(ipw->attrMemory); + pcmcia_release_window(ipw->handleAM); + pcmcia_disable_device(link); + } +exit2: + if (ipw->commonMemory) { + iounmap(ipw->commonMemory); + pcmcia_release_window(ipw->handleCM); + } +exit1: + pcmcia_disable_device(link); +exit0: + return -1; +} + +static void release_ipwireless(struct ipw_dev * ipw) +{ + struct pcmcia_device *link = ipw->link; + + pcmcia_disable_device(link); + + if (ipw->commonMemory) + iounmap(ipw->commonMemory); + if (ipw->attrMemory) + iounmap(ipw->attrMemory); + if (ipw->commonMemory) + pcmcia_release_window(ipw->handleCM); + if (ipw->attrMemory) + pcmcia_release_window(ipw->handleAM); + pcmcia_disable_device(link); +} + +/* + * ipwireless_attach() creates an "instance" of the driver, allocating + * local data structures for one device (one interface). The device + * is registered with Card Services. + * + * The pcmcia_device structure is initialized, but we don't actually + * configure the card at this point -- we wait until we receive a + * card insertion event. + */ +static int ipwireless_attach(struct pcmcia_device *link) +{ + struct ipw_dev *ipw; + int ret; + + ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL); + + ipw->link = link; + link->priv = link->irq.Instance = ipw; + + /* Link this device into our device list. */ + link->dev_node = &ipw->nodes[0]; + dev_list = link; + + ipw->hardware = ipwireless_hardware_create(&default_config); + /* RegisterClient will call config_ipwireless */ + + ret = config_ipwireless(ipw); + + if (ret != 0) { + cs_error(link, RegisterClient, ret); + ipwireless_detach(link); + return ret; + } + + return 0; +} + +/* + * This deletes a driver "instance". The device is de-registered with + * Card Services. If it has been released, all local data structures + * are freed. Otherwise, the structures will be freed when the device + * is released. + */ +static void ipwireless_detach(struct pcmcia_device *link) +{ + struct ipw_dev *ipw = (struct ipw_dev *) link->priv; + + release_ipwireless(ipw); + + /* Break the link with Card Services */ + if (link) + pcmcia_disable_device(link); + + if (ipw->tty != NULL) + ipwireless_tty_free(ipw->tty); + if (ipw->network != NULL) + ipwireless_network_free(ipw->network); + if (ipw->hardware != NULL) + ipwireless_hardware_free(ipw->hardware); + kfree(ipw); +} + +static struct pcmcia_driver me; + +/* + * Module insertion : initialisation of the module. + * Register the card with cardmgr... + */ +static int __init init_ipwireless_cs(void) +{ + int ret; + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME " " + IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n"); + me.owner = THIS_MODULE; + me.probe = ipwireless_attach; + me.remove = ipwireless_detach; + me.drv.name = drv_name; + me.id_table = ipw_ids; + pcmcia_register_driver(&me); + + ret = ipwireless_tty_init(major); + if (ret != 0) + pcmcia_unregister_driver(&me); + return ret; +} + +/* + * Module removal + */ +static void __exit exit_ipwireless_cs(void) +{ + ipwireless_tty_release(); + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME " " + IPWIRELESS_PCMCIA_VERSION " removed\n"); + + pcmcia_unregister_driver(&me); +} + +module_init(init_ipwireless_cs); +module_exit(exit_ipwireless_cs); + +MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR); +MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION); +MODULE_LICENSE("GPL"); diff --git a/drivers/char/pcmcia/ipwireless_cs_main.h b/drivers/char/pcmcia/ipwireless_cs_main.h new file mode 100644 index 0000000..a1a3cdc --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_main.h @@ -0,0 +1,72 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#ifndef _IPWIRELESS_CS_H_ +#define _IPWIRELESS_CS_H_ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipwireless_cs_hardware.h" + +#define IPWIRELESS_PCCARD_NAME "ipwireless_cs" +#define IPWIRELESS_PCMCIA_VERSION "1.0.15-jikos2" +#define IPWIRELESS_PCMCIA_AUTHOR "Stephen Blackheath, Ben Martel and Jiri Kosina" + +#define IPWIRELESS_TX_QUEUE_SIZE 262144 +#define IPWIRELESS_RX_QUEUE_SIZE 262144 + +#define IPWIRELESS_STATE_DEBUG + +struct ipw_hardware; +struct ipw_network; +struct ipw_tty; + +struct ipw_config { + char phone_number[129]; +}; + +struct ipw_dev { + struct pcmcia_device *link; + int isV2Card; + window_handle_t handleAM; + void __iomem *attrMemory; + window_handle_t handleCM; + void __iomem *commonMemory; + dev_node_t nodes[4]; + /* Reference to attribute memory, containing CIS data */ + void *attribute_memory; + + /* Hardware context */ + struct ipw_hardware *hardware; + /* Network layer context */ + struct ipw_network *network; + /* TTY device context */ + struct ipw_tty *tty; + struct work_struct work_reboot; +}; + +/* Options configurable through the proc filesystem. */ +extern __u16 ipwireless_cs_debug; +extern __u16 ipwireless_cs_vendor; +extern __u16 ipwireless_cs_loopback; +extern __u16 ipwireless_cs_out_queue; + +#endif diff --git a/drivers/char/pcmcia/ipwireless_cs_network.c b/drivers/char/pcmcia/ipwireless_cs_network.c new file mode 100644 index 0000000..3b3fea5 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_network.c @@ -0,0 +1,499 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipwireless_cs_network.h" +#include "ipwireless_cs_hardware.h" +#include "ipwireless_cs_main.h" +#include "ipwireless_cs_tty.h" + +#define MAX_OUTGOING_PACKETS_QUEUED ipwireless_cs_out_queue +#define MAX_ASSOCIATED_TTYS 2 + +#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP) + +struct ipw_network { + /* Hardware context, used for calls to hardware layer. */ + struct ipw_hardware *hardware; + /* Context for kernel 'generic_ppp' functionality */ + struct ppp_channel *ppp_channel; + /* tty context connected with IPW console */ + struct ipw_tty *associated_ttys[NO_OF_IPW_CHANNELS][MAX_ASSOCIATED_TTYS]; + /* Number of packets queued up in hardware module. */ + int outgoing_packets_queued; + /* Spinlock to avoid interrupts during shutdown */ + spinlock_t spinlock; + struct mutex close_lock; + + /* reply_buffer fields are used for processing replies to AT commands */ + char reply_buffer[512]; + int reply_buffer_len; + int reply_buffer_lfs; + + unsigned int flags; + unsigned int rbits; + u32 xaccm[8]; + u32 raccm; + int mru; + + int shutting_down; + unsigned int ras_control_lines; + + struct work_struct work_go_online; + struct work_struct work_go_offline; +}; + +static inline int is_ppp_blocked(const struct ipw_network *network) +{ + return network->outgoing_packets_queued >= MAX_OUTGOING_PACKETS_QUEUED; +} + +#ifdef IPWIRELESS_STATE_DEBUG +int ipwireless_dump_network_state(char *p, struct ipw_network * network) +{ + int idx = 0; + + idx += sprintf(p + idx, "debug: ppp blocked=%d\n", + is_ppp_blocked(network)); + idx += sprintf(p + idx, "debug: outgoing_packets_queued=%d\n", + network->outgoing_packets_queued); + idx += sprintf(p + idx, "debug: network.shutting_down=%d\n", + network->shutting_down); + return idx; +} +#endif + +static void notify_packet_sent(void *callback_data, unsigned int packet_length) +{ + struct ipw_network *network = (struct ipw_network *) callback_data; + unsigned long flags; + int wake_ppp = 0; + + spin_lock_irqsave(&network->spinlock, flags); + if (is_ppp_blocked(network)) + wake_ppp = 1; + network->outgoing_packets_queued--; + spin_unlock_irqrestore(&network->spinlock, flags); + + if (wake_ppp) { + ppp_output_wakeup(network->ppp_channel); + if (ipwireless_cs_debug) + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": ppp unblocked\n"); + } +} + +/* + * Called by the ppp system when it has a packet to send to the hardware. + * May change 'ppp_blocked' status. + */ +static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel, + struct sk_buff *skb) +{ + struct ipw_network *network = ppp_channel->private; + unsigned long flags; + + spin_lock_irqsave(&network->spinlock, flags); + if (network->outgoing_packets_queued < MAX_OUTGOING_PACKETS_QUEUED) { + unsigned char *buf; + static unsigned char header[] = { 0xff, 0x03 }; + + network->outgoing_packets_queued++; + spin_unlock_irqrestore(&network->spinlock, flags); + + /* + * If we have the requested amount of headroom in the skb we + * were handed, then we can add the header efficiently. + */ + if (skb_headroom(skb) >= 2) { + memcpy(skb_push(skb, 2), header, 2); + ipwireless_send_packet(network->hardware, + IPW_CHANNEL_RAS, skb->data, + skb->len, + notify_packet_sent, + network); + } else { + /* Otherwise (rarely) we do it inefficiently. */ + buf = kmalloc(skb->len + 2, GFP_ATOMIC); + memcpy(buf + 2, skb->data, skb->len); + memcpy(buf, header, 2); + ipwireless_send_packet(network->hardware, + IPW_CHANNEL_RAS, buf, + skb->len + 2, + notify_packet_sent, + network); + kfree(buf); + } + kfree_skb(skb); + return 1; + } else { + spin_unlock_irqrestore(&network->spinlock, flags); + if (ipwireless_cs_debug) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": ppp blocked\n"); + return 0; + } +} + +/* Handle an ioctl call that has come in via ppp. */ +static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel, + unsigned int cmd, unsigned long arg) +{ + struct ipw_network *network = ppp_channel->private; + int err, val; + u32 accm[8]; + int __user *user_arg = (int __user *) arg; + + err = -EFAULT; + switch (cmd) { + case PPPIOCGFLAGS: + val = network->flags | network->rbits; + if (put_user(val, user_arg)) + break; + err = 0; + break; + + case PPPIOCSFLAGS: + if (get_user(val, user_arg)) + break; + network->flags = val & ~SC_RCV_BITS; + network->rbits = val & SC_RCV_BITS; + err = 0; + break; + + case PPPIOCGASYNCMAP: + if (put_user(network->xaccm[0], user_arg)) + break; + err = 0; + break; + + case PPPIOCSASYNCMAP: + if (get_user(network->xaccm[0], user_arg)) + break; + err = 0; + break; + + case PPPIOCGRASYNCMAP: + if (put_user(network->raccm, user_arg)) + break; + err = 0; + break; + + case PPPIOCSRASYNCMAP: + if (get_user(network->raccm, user_arg)) + break; + err = 0; + break; + + case PPPIOCGXASYNCMAP: + if (copy_to_user((void __user *) arg, network->xaccm, + sizeof(network->xaccm))) + break; + err = 0; + break; + + case PPPIOCSXASYNCMAP: + if (copy_from_user(accm, (void __user *) arg, sizeof(accm))) + break; + accm[2] &= ~0x40000000U; /* can't escape 0x5e */ + accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */ + memcpy(network->xaccm, accm, sizeof(network->xaccm)); + err = 0; + break; + + case PPPIOCGMRU: + if (put_user(network->mru, user_arg)) + break; + err = 0; + break; + + case PPPIOCSMRU: + if (get_user(val, user_arg)) + break; + if (val < PPP_MRU) + val = PPP_MRU; + network->mru = val; + err = 0; + break; + + default: + err = -ENOTTY; + } + + return err; +} + +static struct ppp_channel_ops ipwireless_ppp_channel_ops = { + .start_xmit = ipwireless_ppp_start_xmit, + .ioctl = ipwireless_ppp_ioctl +}; + +static void do_go_online(struct work_struct *work_go_online) +{ + struct ipw_network *network = + container_of(work_go_online, struct ipw_network, + work_go_online); + unsigned long flags; + + spin_lock_irqsave(&network->spinlock, flags); + if (!network->ppp_channel) { + struct ppp_channel *channel; + + spin_unlock_irqrestore(&network->spinlock, flags); + channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL); + channel->private = network; + channel->mtu = 16384; /* Wild guess */ + channel->hdrlen = 2; + channel->ops = &ipwireless_ppp_channel_ops; + + network->flags = 0; + network->rbits = 0; + network->mru = PPP_MRU; + { + int i; + + for (i = 0; i < 8; i++) + network->xaccm[i] = 0; + } + network->xaccm[0] = ~0U; + network->xaccm[3] = 0x60000000U; + network->raccm = ~0U; + ppp_register_channel(channel); + spin_lock_irqsave(&network->spinlock, flags); + network->ppp_channel = channel; + } + spin_unlock_irqrestore(&network->spinlock, flags); +} + +static void do_go_offline(struct work_struct *work_go_offline) +{ + struct ipw_network *network = + container_of(work_go_offline, struct ipw_network, + work_go_offline); + unsigned long flags; + + mutex_lock(&network->close_lock); + spin_lock_irqsave(&network->spinlock, flags); + if (network->ppp_channel != NULL) { + struct ppp_channel *channel = network->ppp_channel; + + network->ppp_channel = NULL; + spin_unlock_irqrestore(&network->spinlock, flags); + mutex_unlock(&network->close_lock); + ppp_unregister_channel(channel); + } else { + spin_unlock_irqrestore(&network->spinlock, flags); + mutex_unlock(&network->close_lock); + } +} + +void ipwireless_network_notify_control_line_change(struct ipw_network *network, + unsigned int channel_idx, + unsigned int control_lines, + unsigned int changed_mask) +{ + int i; + + if (channel_idx == IPW_CHANNEL_RAS) + network->ras_control_lines = control_lines; + + for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) { + struct ipw_tty *tty = + network->associated_ttys[channel_idx][i]; + + /* + * If it's associated with a tty (other than the RAS channel + * when we're online), then send the data to that tty. The RAS + * channel's data is handled above - it always goes through + * ppp_generic. + */ + if (tty) + ipwireless_tty_notify_control_line_change(tty, + channel_idx, + control_lines, + changed_mask); + } +} + +/* + * some versions of firmware stuff packets with 0xff 0x03 bytes (which are + * required on sent packet, but not always present on received packets) + */ +static struct sk_buff *ipwireless_packet_received_skb(unsigned char *data, + unsigned int length) +{ + struct sk_buff *skb; + int offset; + + if (length <= 2) + offset = 0; + else if (data[0] == 0xff && data[1] == 0x03) + offset = 2; + else + offset = 0; + + skb = dev_alloc_skb(length + (4 - offset)); + skb_reserve(skb, 2); + memcpy(skb_put(skb, length - offset), data + offset, length - offset); + return skb; +} + +void ipwireless_network_packet_received(struct ipw_network *network, + unsigned int channel_idx, + unsigned char *data, + unsigned int length) +{ + int i; + unsigned long flags; + + for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) { + struct ipw_tty *tty = network->associated_ttys[channel_idx][i]; + + /* + * If it's associated with a tty (other than the RAS channel + * when we're online), then send the data to that tty. The RAS + * channel's data is handled above - it always goes through + * ppp_generic. + */ + if (tty && channel_idx == IPW_CHANNEL_RAS + && (network->ras_control_lines & IPW_CONTROL_LINE_DCD) != 0 + && ipwireless_tty_is_modem(tty)) { + /* + * If data came in on the RAS channel and this tty is + * the modem tty, and we are online, then we send it to + * the PPP layer. + */ + mutex_lock(&network->close_lock); + spin_lock_irqsave(&network->spinlock, flags); + if (network->ppp_channel != NULL) { + struct sk_buff *skb; + + spin_unlock_irqrestore(&network->spinlock, flags); + + /* Send the data to the ppp_generic module. */ + skb = ipwireless_packet_received_skb(data, length); + ppp_input(network->ppp_channel, skb); + } else + spin_unlock_irqrestore(&network->spinlock, flags); + mutex_unlock(&network->close_lock); + } + /* Otherwise we send it out the tty. */ + else + ipwireless_tty_received(tty, data, length); + } +} + +struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw) +{ + struct ipw_network *network = + kzalloc(sizeof(struct ipw_network), GFP_ATOMIC); + + spin_lock_init(&network->spinlock); + mutex_init(&network->close_lock); + + network->hardware = hw; + + INIT_WORK(&network->work_go_online, do_go_online); + INIT_WORK(&network->work_go_offline, do_go_offline); + + ipwireless_associate_network(hw, network); + + return network; +} + +void ipwireless_network_free(struct ipw_network *network) +{ + network->shutting_down = 1; + + ipwireless_ppp_close(network); + flush_scheduled_work(); + + ipwireless_stop_interrupts(network->hardware); + ipwireless_associate_network(network->hardware, NULL); + + kfree(network); +} + +void ipwireless_associate_network_tty(struct ipw_network *network, + unsigned int channel_idx, + struct ipw_tty *tty) +{ + int i; + + for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) + if (network->associated_ttys[channel_idx][i] == NULL) { + network->associated_ttys[channel_idx][i] = tty; + break; + } +} + +void ipwireless_disassociate_network_ttys(struct ipw_network *network, + unsigned int channel_idx) +{ + int i; + + for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) + network->associated_ttys[channel_idx][i] = NULL; +} + +void ipwireless_ppp_open(struct ipw_network *network) +{ + if (ipwireless_cs_debug) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n"); + schedule_work(&network->work_go_online); +} + +void ipwireless_ppp_close(struct ipw_network *network) +{ + /* Disconnect from the wireless network. */ + if (ipwireless_cs_debug) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n"); + schedule_work(&network->work_go_offline); +} + +int ipwireless_ppp_channel_index(struct ipw_network *network) +{ + int ret = -1; + unsigned long flags; + + spin_lock_irqsave(&network->spinlock, flags); + if (network->ppp_channel != NULL) + ret = ppp_channel_index(network->ppp_channel); + spin_unlock_irqrestore(&network->spinlock, flags); + + return ret; +} + +int ipwireless_ppp_unit_number(struct ipw_network *network) +{ + int ret = -1; + unsigned long flags; + + spin_lock_irqsave(&network->spinlock, flags); + if (network->ppp_channel != NULL) + ret = ppp_unit_number(network->ppp_channel); + spin_unlock_irqrestore(&network->spinlock, flags); + + return ret; +} diff --git a/drivers/char/pcmcia/ipwireless_cs_network.h b/drivers/char/pcmcia/ipwireless_cs_network.h new file mode 100644 index 0000000..4fbb6a4 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_network.h @@ -0,0 +1,51 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#ifndef _IPWIRELESS_CS_NETWORK_H_ +#define _IPWIRELESS_CS_NETWORK_H_ + +#include + +struct ipw_network; +struct ipw_tty; +struct ipw_hardware; + +/* Definitions of the different channels on the PCMCIA UE */ +#define IPW_CHANNEL_RAS 0 +#define IPW_CHANNEL_DIALLER 1 +#define IPW_CHANNEL_CONSOLE 2 +#define NO_OF_IPW_CHANNELS 5 + +void ipwireless_network_notify_control_line_change(struct ipw_network *net, + unsigned int channel_idx, unsigned int control_lines, + unsigned int control_mask); +void ipwireless_network_packet_received(struct ipw_network *net, + unsigned int channel_idx, unsigned char *data, + unsigned int length); +struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw); +void ipwireless_network_free(struct ipw_network *net); +void ipwireless_associate_network_tty(struct ipw_network *net, + unsigned int channel_idx, struct ipw_tty *tty); +void ipwireless_disassociate_network_ttys(struct ipw_network *net, + unsigned int channel_idx); + +void ipwireless_ppp_open(struct ipw_network *net); + +void ipwireless_ppp_close(struct ipw_network *net); +int ipwireless_ppp_channel_index(struct ipw_network *net); +int ipwireless_ppp_unit_number(struct ipw_network *net); + +int ipwireless_dump_network_state(char *p, struct ipw_network *net); + +#endif diff --git a/drivers/char/pcmcia/ipwireless_cs_setup_protocol.h b/drivers/char/pcmcia/ipwireless_cs_setup_protocol.h new file mode 100644 index 0000000..bfae338 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_setup_protocol.h @@ -0,0 +1,122 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#ifndef _IPWIRELESS_CS_SETUP_PROTOCOL_H_ +#define _IPWIRELESS_CS_SETUP_PROTOCOL_H_ + +#include + +/* Version of the setup protocol and transport protocols */ +#define TL_SETUP_VERSION 1 + +#define TL_SETUP_VERSION_QRY_TMO 1000 +#define TL_SETUP_MAX_VERSION_QRY 30 + +/* Message numbers 0-9 are obsoleted. The must not be reused! */ +#define TL_SETUP_SIGNO_GET_VERSION_QRY 10 +#define TL_SETUP_SIGNO_GET_VERSION_RSP 11 +#define TL_SETUP_SIGNO_CONFIG_MSG 12 +#define TL_SETUP_SIGNO_CONFIG_DONE_MSG 13 +#define TL_SETUP_SIGNO_OPEN_MSG 14 +#define TL_SETUP_SIGNO_CLOSE_MSG 15 + +#define TL_SETUP_SIGNO_INFO_MSG 20 +#define TL_SETUP_SIGNO_INFO_MSG_ACK 21 + +#define TL_SETUP_SIGNO_REBOOT_MSG 22 +#define TL_SETUP_SIGNO_REBOOT_MSG_ACK 23 + +/* Syncronous start-messages */ +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_GET_VERSION_QRY */ +} __attribute__ ((__packed__)) TlSetupGetVersionQry; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_GET_VERSION_RSP */ + unsigned char version; /* TL_SETUP_VERSION */ +} __attribute__ ((__packed__)) TlSetupGetVersionRsp; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_CONFIG_MSG */ + unsigned char portNo; + unsigned char prioData; + unsigned char prioCtrl; +} __attribute__ ((__packed__)) TlSetupConfigMsg; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_CONFIG_DONE_MSG */ +} __attribute__ ((__packed__)) TlSetupConfigDoneMsg; + +/* Asyncronous messages */ +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_OPEN_MSG */ + unsigned char portNo; +} __attribute__ ((__packed__)) TlSetupOpenMsg; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_CLOSE_MSG */ + unsigned char portNo; +} __attribute__ ((__packed__)) TlSetupCloseMsg; + +/* Driver type - for use in TlSetupInfoMsg.driverType */ +#define COMM_DRIVER 0 +#define NDISWAN_DRIVER 1 +#define NDISWAN_DRIVER_MAJOR_VERSION 2 +#define NDISWAN_DRIVER_MINOR_VERSION 0 + +/* + * It should not matter when this message comes over as we just store the + * results and send the ACK. + */ +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_INFO_MSG */ + unsigned char driverType; + unsigned char majorVersion; + unsigned char minorVersion; +} __attribute__ ((__packed__)) TlSetupInfoMsg; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_INFO_MSG_ACK */ +} __attribute__ ((__packed__)) TlSetupInfoMsgAck; + +typedef struct { + unsigned char sigNo; /* TL_SETUP_SIGNO_REBOOT_MSG_ACK */ +} __attribute__ ((__packed__)) TlSetupRebootMsgAck; + +/* Define a union of all the msgs that the driver can receive from the card.*/ +typedef union { + unsigned char sigNo; + TlSetupGetVersionRsp VersRspMsg; + TlSetupOpenMsg OpenMsg; + TlSetupCloseMsg CloseMsg; + TlSetupInfoMsg InfoMsg; + TlSetupInfoMsgAck InfoMsgAck; +} __attribute__ ((__packed__)) SETUP_RX_MSG, *PSETUP_RX_MSG; + +/* + * Define a union of all the msgs that the driver can send to the card. This + * will then represent the max msg that can be sent by the driver to the card + */ +typedef union { + unsigned char sigNo; + TlSetupGetVersionQry QryMsg; + TlSetupConfigMsg ConfigMsg; + TlSetupConfigDoneMsg ConfigDoneMsg; + TlSetupOpenMsg OpenMsg; + TlSetupCloseMsg CloseMsg; + TlSetupInfoMsg InfoMsg; + TlSetupRebootMsgAck RebootMsgAck; +} __attribute__ ((__packed__)) SETUP_TX_MSG, *PSETUP_TX_MSG; + +#endif /* _IPWIRELESS_CS_SETUP_PROTOCOL_H_ */ diff --git a/drivers/char/pcmcia/ipwireless_cs_tty.c b/drivers/char/pcmcia/ipwireless_cs_tty.c new file mode 100644 index 0000000..71d4cdf --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_tty.c @@ -0,0 +1,798 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipwireless_cs_tty.h" +#include "ipwireless_cs_network.h" +#include "ipwireless_cs_hardware.h" +#include "ipwireless_cs_main.h" + +#define IPWIRELESS_PCMCIA_START 0 +#define IPWIRELESS_PCMCIA_MINORS 32 + +#define TTYTYPE_MODEM 0 +#define TTYTYPE_MONITOR 1 +#define TTYTYPE_CONSOLE 2 +#define TTYTYPE_RAS_RAW 3 + +struct ipw_tty { + int index; + struct ipw_hardware *hardware; + unsigned int channel_idx; + unsigned int secondary_channel_idx; + int tty_type; + struct ipw_network *network; + struct tty_struct *linux_tty; + int open_count; + struct pcmcia_device *link; + unsigned int control_lines; + struct mutex ipw_tty_mutex; + int tx_bytes_queued; + int closing; +}; + +static int actual_major; + +static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS]; + +static char *tty_type_name(int tty_type) +{ + static char *channel_names[] = { + "modem", + "monitor", + "console", + "RAS-raw" + }; + + return channel_names[tty_type]; +} + +static void report_registering(struct ipw_tty *tty) +{ + char *iftype = tty_type_name(tty->tty_type); + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": registering %s device ttyIPWp%d\n", iftype, tty->index); +} + +static void report_deregistering(struct ipw_tty *tty) +{ + char *iftype = tty_type_name(tty->tty_type); + + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": deregistering %s device ttyIPWp%d\n", iftype, + tty->index); +} + +static struct ipw_tty *get_tty(int minor) +{ + if (minor < IPWIRELESS_PCMCIA_START + || minor >= IPWIRELESS_PCMCIA_START + IPWIRELESS_PCMCIA_MINORS) + return NULL; + else { + int minor_offset = minor - IPWIRELESS_PCMCIA_START; + + /* + * The 'console' channel is only available when 'vendor' mode + * is enabled. + */ + if (!ipwireless_cs_vendor && minor_offset >= 16 && minor_offset < 24) + return NULL; + + /* + * The 'ras_raw' channel is only available when 'loopback' mode + * is enabled. + */ + if (!ipwireless_cs_loopback && minor_offset >= 24) + return NULL; + + return ttys[minor_offset]; + } +} + +static int ipw_open(struct tty_struct *linux_tty, struct file *filp) +{ + int minor = linux_tty->index; + struct ipw_tty *tty = get_tty(minor); + + if (!tty) + return -ENODEV; + + mutex_lock(&tty->ipw_tty_mutex); + + if (tty->closing) { + mutex_unlock(&tty->ipw_tty_mutex); + return -ENODEV; + } + if (tty->open_count == 0) + tty->tx_bytes_queued = 0; + + tty->link->open++; + tty->open_count++; + + tty->linux_tty = linux_tty; + linux_tty->driver_data = tty; + linux_tty->low_latency = 1; + + /* Have the console default to RTS and DTR on */ + if (tty->channel_idx == IPW_CHANNEL_CONSOLE) { + ipwireless_setRTS(tty->hardware, tty->channel_idx, 1); + ipwireless_setDTR(tty->hardware, tty->channel_idx, 1); + } + if (tty->tty_type == TTYTYPE_MODEM) + ipwireless_ppp_open(tty->network); + + mutex_unlock(&tty->ipw_tty_mutex); + + return 0; +} + +static void do_ipw_close(struct ipw_tty *tty) +{ + tty->open_count--; + tty->link->open--; + + if (tty->open_count == 0) { + struct tty_struct *linux_tty = tty->linux_tty; + + if (linux_tty != NULL) { + tty->linux_tty = NULL; + linux_tty->driver_data = NULL; + + if (tty->tty_type == TTYTYPE_MODEM) + ipwireless_ppp_close(tty->network); + } + } +} + +static void ipw_hangup(struct tty_struct *linux_tty) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + + if (!tty) + return; + + mutex_lock(&tty->ipw_tty_mutex); + if (tty->open_count == 0) { + mutex_unlock(&tty->ipw_tty_mutex); + return; + } + + do_ipw_close(tty); + + mutex_unlock(&tty->ipw_tty_mutex); +} + +static void ipw_close(struct tty_struct *linux_tty, struct file *filp) +{ + ipw_hangup(linux_tty); +} + +/* Take data received from hardware, and send it out the tty */ +void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data, + unsigned int length) +{ + struct tty_struct *linux_tty; + int i; + int work = 0; + + mutex_lock(&tty->ipw_tty_mutex); + linux_tty = tty->linux_tty; + if (linux_tty == NULL) { + mutex_unlock(&tty->ipw_tty_mutex); + return; + } + + if (!tty->open_count) { + mutex_unlock(&tty->ipw_tty_mutex); + return; + } + mutex_unlock(&tty->ipw_tty_mutex); + + tty_buffer_request_room(linux_tty, length); + for (i = 0; i < length; ++i) + work += tty_insert_flip_char(linux_tty, data[i], 0); + + if (work) + tty_flip_buffer_push(linux_tty); +} + +static void ipw_write_packet_sent_callback(void *callback_data, + unsigned int packet_length) +{ + struct ipw_tty *tty = (struct ipw_tty *) callback_data; + + /* + * Packet has been sent, so we subtract the number of bytes from our + * tally of outstanding TX bytes. + */ + tty->tx_bytes_queued -= packet_length; +} + +static int ipw_write(struct tty_struct *linux_tty, + const unsigned char *buf, int count) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + int room; + + if (!tty) + return -ENODEV; + + mutex_lock(&tty->ipw_tty_mutex); + if (!tty->open_count) { + mutex_unlock(&tty->ipw_tty_mutex); + return -EINVAL; + } + + room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued; + if (room < 0) + room = 0; + /* Don't allow caller to write any more than we have room for */ + if (count > room) + count = room; + + if (count == 0) { + mutex_unlock(&tty->ipw_tty_mutex); + return 0; + } + + tty->tx_bytes_queued += count; + ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS, + (unsigned char *) buf, count, + ipw_write_packet_sent_callback, tty); + mutex_unlock(&tty->ipw_tty_mutex); + + return count; +} + +static int ipw_write_room(struct tty_struct *linux_tty) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + int room; + + if (!tty) + return -ENODEV; + + if (!tty->open_count) + return -EINVAL; + + room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued; + if (room < 0) + room = 0; + + return room; +} + +static int ipwireless_get_serial_info(struct ipw_tty * tty, + struct serial_struct __user * retinfo) +{ + struct serial_struct tmp; + + if (!retinfo) + return (-EFAULT); + + memset(&tmp, 0, sizeof(tmp)); + tmp.type = PORT_UNKNOWN; + tmp.line = tty->index; + tmp.port = 0; + tmp.irq = 0; + tmp.flags = 0; + tmp.baud_base = 115200; + tmp.close_delay = 0; + tmp.closing_wait = 0; + tmp.custom_divisor = 0; + tmp.hub6 = 0; + if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) + return -EFAULT; + + return 0; +} + +static int ppp_ioctl(struct tty_struct *linux_tty, struct file *file, + unsigned int cmd, unsigned long arg) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + + if (!tty) + return -ENODEV; + + if (!tty->open_count) + return -EINVAL; + + switch (cmd) { + case TIOCGSERIAL: + return ipwireless_get_serial_info(tty, (void __user *) arg); + + case TIOCSSERIAL: + return 0; /* Keeps the PCMCIA scripts happy. */ + } + + if (tty->tty_type == TTYTYPE_MODEM) { + switch (cmd) { + case PPPIOCGCHAN: + { + int chan = ipwireless_ppp_channel_index(tty->network); + + if (chan < 0) + return -ENODEV; + if (put_user(chan, (int __user *) arg)) + return -EFAULT; + } + return 0; + + case PPPIOCGUNIT: + { + int unit = ipwireless_ppp_unit_number(tty->network); + + if (unit < 0) + return -ENODEV; + if (put_user(unit, (int __user *) arg)) + return -EFAULT; + } + return 0; + + case TCGETS: + case TCGETA: + return n_tty_ioctl(linux_tty, file, cmd, arg); + + case TCFLSH: + return n_tty_ioctl(linux_tty, file, cmd, arg); + + case FIONREAD: + { + int val = 0; + + if (put_user(val, (int __user *) arg)) + return -EFAULT; + } + return 0; + } + } + + return -ENOIOCTLCMD; +} + +static int ipw_chars_in_buffer(struct tty_struct *linux_tty) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + + if (!tty) + return -ENODEV; + + if (!tty->open_count) + return -EINVAL; + + return tty->tx_bytes_queued; +} + +static int get_control_lines(struct ipw_tty * tty) +{ + unsigned int my = tty->control_lines; + unsigned int out = 0; + + if (my & IPW_CONTROL_LINE_RTS) + out |= TIOCM_RTS; + if (my & IPW_CONTROL_LINE_DTR) + out |= TIOCM_DTR; + if (my & IPW_CONTROL_LINE_CTS) + out |= TIOCM_CTS; + if (my & IPW_CONTROL_LINE_DSR) + out |= TIOCM_DSR; + if (my & IPW_CONTROL_LINE_DCD) + out |= TIOCM_CD; + + return out; +} + +static int set_control_lines(struct ipw_tty * tty, unsigned int set, + unsigned int clear) +{ + if (set & TIOCM_RTS) { + ipwireless_setRTS(tty->hardware, tty->channel_idx, 1); + if (tty->secondary_channel_idx != -1) + ipwireless_setRTS(tty->hardware, + tty->secondary_channel_idx, 1); + } + if (set & TIOCM_DTR) { + ipwireless_setDTR(tty->hardware, tty->channel_idx, 1); + if (tty->secondary_channel_idx != -1) + ipwireless_setDTR(tty->hardware, + tty->secondary_channel_idx, 1); + } + if (clear & TIOCM_RTS) { + ipwireless_setRTS(tty->hardware, tty->channel_idx, 0); + if (tty->secondary_channel_idx != -1) + ipwireless_setRTS(tty->hardware, + tty->secondary_channel_idx, 0); + } + if (clear & TIOCM_DTR) { + ipwireless_setDTR(tty->hardware, tty->channel_idx, 0); + if (tty->secondary_channel_idx != -1) + ipwireless_setDTR(tty->hardware, + tty->secondary_channel_idx, 0); + } + return 0; +} + +static int ipw_tiocmget(struct tty_struct *linux_tty, struct file *file) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + + if (!tty) + return -ENODEV; + + if (!tty->open_count) + return -EINVAL; + + return get_control_lines(tty); +} + +static int +ipw_tiocmset(struct tty_struct *linux_tty, struct file *file, + unsigned int set, unsigned int clear) +{ + struct ipw_tty *tty = (struct ipw_tty *) linux_tty->driver_data; + + if (!tty) + return -ENODEV; + + if (!tty->open_count) + return -EINVAL; + + return set_control_lines(tty, set, clear); +} + + +static int +ipw_ioctl(struct tty_struct *linux_tty, struct file *file, + unsigned int cmd, unsigned long arg) +{ + return ppp_ioctl(linux_tty, file, cmd, arg); +} + +static void add_tty(int first, int *j, dev_node_t ** nodesp, + struct pcmcia_device *link, + struct ipw_hardware *hardware, + struct ipw_network *network, int channel_idx, + int secondary_channel_idx, int tty_type) +{ + ttys[*j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL); + ttys[*j]->index = *j; + ttys[*j]->hardware = hardware; + ttys[*j]->channel_idx = channel_idx; + ttys[*j]->secondary_channel_idx = secondary_channel_idx; + ttys[*j]->network = network; + ttys[*j]->tty_type = tty_type; + ttys[*j]->link = link; + mutex_init(&ttys[*j]->ipw_tty_mutex); + + ipwireless_associate_network_tty(network, channel_idx, ttys[*j]); + + if (secondary_channel_idx != -1) + ipwireless_associate_network_tty(network, + secondary_channel_idx, + ttys[*j]); + if (nodesp != NULL) { + if (!first) + (*nodesp)[-1].next = *nodesp; + sprintf((*nodesp)->dev_name, "ttyIPWp%d", *j); + (*nodesp)->major = actual_major; + (*nodesp)->minor = (*j) + IPWIRELESS_PCMCIA_START; + (*nodesp)++; + } + if (get_tty((*j) + IPWIRELESS_PCMCIA_START) == ttys[*j]) + report_registering(ttys[*j]); + (*j) += 8; +} + +struct ipw_tty *ipwireless_tty_create(struct pcmcia_device *link, + struct ipw_hardware *hardware, + struct ipw_network *network, + dev_node_t * nodes) +{ + int i; + + for (i = 0; i < 8; i++) { + int allfree = 1; + + { + int j; + + for (j = i; j < IPWIRELESS_PCMCIA_MINORS; j += 8) + if (ttys[j] != NULL) { + allfree = 0; + break; + } + } + if (allfree) { + int j = i; + + add_tty(1, &j, &nodes, link, hardware, network, + IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS, + TTYTYPE_MODEM); + add_tty(0, &j, &nodes, link, hardware, network, + IPW_CHANNEL_DIALLER, -1, TTYTYPE_MONITOR); + /* + * NULL: Don't report the existence of CONSOLE and + * RAS_RAW channels to the PCMCIA subsystem, since they + * are only there under certain modes. This prevents + * and unpleasant error message appearing in the log + * when it fails to open them. + */ + add_tty(0, &j, NULL, link, hardware, network, + IPW_CHANNEL_CONSOLE, -1, TTYTYPE_CONSOLE); + add_tty(0, &j, NULL, link, hardware, network, + IPW_CHANNEL_RAS, -1, TTYTYPE_RAS_RAW); + + return ttys[i]; + } + } + return NULL; +} + +/*! + * Must be called before ipwireless_network_free(). + */ +void ipwireless_tty_free(struct ipw_tty *tty) +{ + int j; + struct ipw_network *network = ttys[tty->index]->network; + + for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS; j += 8) { + struct ipw_tty *tty = ttys[j]; + + if (tty) { + mutex_lock(&tty->ipw_tty_mutex); + if (get_tty(j + IPWIRELESS_PCMCIA_START) == tty) + report_deregistering(tty); + tty->closing = 1; + if (tty->linux_tty != NULL) { + mutex_unlock(&tty->ipw_tty_mutex); + tty_hangup(tty->linux_tty); + /* Wait till the tty_hangup has really completed. */ + flush_scheduled_work(); + mutex_lock(&tty->ipw_tty_mutex); + } + while (tty->open_count) + do_ipw_close(tty); + ipwireless_disassociate_network_ttys(network, + tty->channel_idx); + ttys[j] = NULL; + mutex_unlock(&tty->ipw_tty_mutex); + kfree(tty); + } + } +} + +static struct tty_operations tty_ops = { + .open = ipw_open, + .close = ipw_close, + .hangup = ipw_hangup, + .write = ipw_write, + .write_room = ipw_write_room, + .ioctl = ipw_ioctl, + .chars_in_buffer = ipw_chars_in_buffer, + .tiocmget = ipw_tiocmget, + .tiocmset = ipw_tiocmset, +}; + +static struct tty_driver *ipw_tty_driver; + +static int proc_read(char *page, char **start, off_t off, int count, + int *eof, void *data) +{ + int i, j; + char *p = page; + int len; + + if (off == 0) { + p += sprintf(p, "driver: %s\nversion: %s\n\n", + IPWIRELESS_PCCARD_NAME, + IPWIRELESS_PCMCIA_VERSION); + for (i = 0; i < 8; i++) + for (j = i; j < IPWIRELESS_PCMCIA_MINORS; j += 8) + if (ttys[j] != NULL && get_tty (ttys[j]->index + IPWIRELESS_PCMCIA_START) == ttys[j]) + p += sprintf(p, "port: /dev/ttyIPWp%d %s %d\n", + j, tty_type_name (ttys[j]->tty_type), ttys[j]->open_count); +#ifdef IPWIRELESS_STATE_DEBUG + *p++ = '\n'; + for (i = 0; i < 8; i++) + if (ttys[i] != NULL) { + p += ipwireless_dump_hardware_state(p, ttys[i]->hardware); + if (ttys[i]->network != NULL) + p += ipwireless_dump_network_state(p, ttys[i]->network); + break; + } +#endif + } + + len = (p - page); + if (len <= off + count) + *eof = 1; + *start = page + off; + len -= off; + if (len > count) + len = count; + if (len < 0) + len = 0; + + return len; +} + +static int proc_write(struct file *file, const char __user * buffer, + unsigned long count, void *data) +{ + char *command = kmalloc(count + 1, GFP_KERNEL); + int i, lineLen; + struct ipw_tty *could_open[IPWIRELESS_PCMCIA_MINORS]; + + if (copy_from_user(command, buffer, count)) { + kfree(command); + return -EFAULT; + } + command[count] = '\0'; + + for (i = 0; i < IPWIRELESS_PCMCIA_MINORS; ++i) + could_open[i] = get_tty(i + IPWIRELESS_PCMCIA_START); + + for (i = 0; i < count; i += lineLen + 1) { + char *cmd; + + lineLen = 0; + while (command[i + lineLen] != '\n' + && command[i + lineLen] != '\0') + lineLen++; + command[i + lineLen] = '\0'; + + cmd = command + i; + + if (strncmp(cmd, "debug=", 6) == 0) { + ipwireless_cs_debug = + (int) simple_strtol(cmd + 6, NULL, 10); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": set debug=%d\n", ipwireless_cs_debug); + } else if (strncmp(cmd, "vendor=", 7) == 0) { + ipwireless_cs_vendor = + (int) simple_strtol(cmd + 7, NULL, 10); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": set vendor=%d\n", ipwireless_cs_vendor); + } else if (strncmp(cmd, "loopback=", 9) == 0) { + ipwireless_cs_loopback = + (int) simple_strtol(cmd + 9, NULL, 10); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": set loopback=%d\n", + ipwireless_cs_loopback); + } else if (strncmp(cmd, "out_queue=", 10) == 0) { + ipwireless_cs_out_queue = + (int) simple_strtol(cmd + 10, NULL, 10); + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": set out_queue=%d\n", + ipwireless_cs_out_queue); + } + } + kfree(command); + + /* Report any created or destroyed ttys */ + for (i = 0; i < IPWIRELESS_PCMCIA_MINORS; ++i) { + struct ipw_tty *can_open = get_tty(i + IPWIRELESS_PCMCIA_START); + + if (can_open && !could_open[i]) + report_registering(can_open); + if (!can_open && could_open[i]) + report_deregistering(could_open[i]); + } + return count; +} + +int ipwireless_tty_init(int major) +{ + int result; + + ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS); + if (!ipw_tty_driver) + return -ENOMEM; + + ipw_tty_driver->owner = THIS_MODULE; + ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME; + ipw_tty_driver->name = "ttyIPWp"; + ipw_tty_driver->major = major; + ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START; + ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; + ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL; + ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW; + ipw_tty_driver->init_termios = tty_std_termios; + ipw_tty_driver->init_termios.c_cflag = + B9600 | CS8 | CREAD | HUPCL | CLOCAL; + tty_set_operations(ipw_tty_driver, &tty_ops); + result = tty_register_driver(ipw_tty_driver); + if (result) { + printk(KERN_ERR IPWIRELESS_PCCARD_NAME + ": failed to register tty driver\n"); + put_tty_driver(ipw_tty_driver); + return result; + } + + actual_major = ipw_tty_driver->major; + + { + struct proc_dir_entry *proc_ent; + + proc_ent = + create_proc_entry(IPWIRELESS_PCCARD_NAME, + S_IRUGO | S_IWUSR | S_IWGRP, + &proc_root); + if (proc_ent != NULL) { + proc_ent->read_proc = proc_read; + proc_ent->write_proc = proc_write; + } + } + + return 0; +} + +void ipwireless_tty_release(void) +{ + int ret, loops = 0; + + remove_proc_entry(IPWIRELESS_PCCARD_NAME, &proc_root); + + while (1) { + ret = tty_unregister_driver(ipw_tty_driver); + put_tty_driver(ipw_tty_driver); + if (ret != 0) { + if (loops == 0) + printk(KERN_ERR IPWIRELESS_PCCARD_NAME + ": tty_unregister_driver failed %d - " + "retrying...\n", ret); + } else + break; + set_current_state(TASK_INTERRUPTIBLE); + schedule_timeout(HZ / 5); + loops++; + } +} + +int ipwireless_tty_is_modem(struct ipw_tty *tty) +{ + return tty->tty_type == TTYTYPE_MODEM; +} + +void +ipwireless_tty_notify_control_line_change(struct ipw_tty *tty, + unsigned int channel_idx, + unsigned int control_lines, + unsigned int changed_mask) +{ + tty->control_lines = (tty->control_lines & ~changed_mask) + | (control_lines & changed_mask); +} + diff --git a/drivers/char/pcmcia/ipwireless_cs_tty.h b/drivers/char/pcmcia/ipwireless_cs_tty.h new file mode 100644 index 0000000..cd58e4d --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_tty.h @@ -0,0 +1,45 @@ +/* + * IPWireless 3G PCMCIA Network Driver + * + * Original code + * by Stephen Blackheath , + * Ben Martel + * + * Copyrighted as follows: + * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) + * + * Various driver changes and rewrites, port to new kernels + * Copyright (C) 2006-2007 Jiri Kosina + */ + +#ifndef _IPWIRELESS_CS_TTY_H_ +#define _IPWIRELESS_CS_TTY_H_ + +#include +#include + +#include +#include +#include +#include + +struct ipw_tty; +struct ipw_network; +struct ipw_hardware; + +int ipwireless_tty_init(int); + +void ipwireless_tty_release(void); + +struct ipw_tty *ipwireless_tty_create(struct pcmcia_device *link, + struct ipw_hardware *hw, struct ipw_network *net, + dev_node_t *nodes); +void ipwireless_tty_free(struct ipw_tty *tty); +void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data, + unsigned int length); +int ipwireless_tty_is_modem(struct ipw_tty *tty); +void ipwireless_tty_notify_control_line_change(struct ipw_tty *tty, + unsigned int channel_idx, unsigned int control_lines, + unsigned int changed_mask); + +#endif