GIT e1ddb69d5b35c00c7b7dd6105db334df40afb61d git+ssh://master.kernel.org/pub/scm/linux/kernel/git/jikos/ipwireless_cs.git#ipw-devel commit Author: David Sterba Date: Tue Aug 21 09:10:43 2007 +0200 ipwireless_cs: Use is_card_present Use is_card_present in interrupt handlers. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 58fedd31e1d586330f71f009ef735fd0a7fa03e4 Author: David Sterba Date: Tue Aug 21 09:10:37 2007 +0200 ipwireless_cs: Remove packet assembler structs Remove packet assembler struct, it only hides pointer indirection. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 649080b63b65456b4a81e41db3a2f2f920b42dc3 Author: David Sterba Date: Tue Aug 21 09:10:32 2007 +0200 ipwireless_cs: Remove one pointless goto Remove one pointless goto. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit a815b7a8cfc165b325a3466e33cd133bc0f17530 Author: David Sterba Date: Tue Aug 21 09:10:27 2007 +0200 ipwireless_cs: Save irq flags with spinlock Save irq flags with spinlock in recv workqueue handler. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 140f4a2f60a19c15a3183545f393f26ab73d7b3a Author: David Sterba Date: Tue Aug 21 09:10:22 2007 +0200 ipwireless_cs: Misc cleanups Misc cleanups and trivial changes: * rename do_io * remove redundant comments from obvious code * more explanatory comments * remove duplicite variables * minor code cleanups Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 5027ffb7f9d03f9a7f67633bdc008549fac0e3f8 Author: David Sterba Date: Mon Aug 20 17:07:45 2007 +0200 ipwireless_cs: Split do_io callsites into send or recv part Split do_io callsites into send or recv part and don't call do_io. Remove recursion protection, not possible anymore. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 7ab6ddf645b2b71d809bbe89e3d250d5efd69f22 Author: David Sterba Date: Mon Aug 20 17:07:39 2007 +0200 ipwireless_cs: Send: get maximum send priority via parameter Send: get maximum send priority via parameter. Callsite knows the priority. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit d14d675f93e190646648d88462eab4cea2a57b22 Author: David Sterba Date: Mon Aug 20 17:07:34 2007 +0200 ipwireless_cs: Put send code into own function Put send code of do_io to own function. Kill the demonic while loop and let send func return if more is to be sent. Setup code is left there. It is needed to call both send and receive, else packets will stay unsent. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit a588d7f7637e925c8054702db917684d8e9254ef Author: David Sterba Date: Mon Aug 20 17:07:29 2007 +0200 ipwireless_cs: Put recving into func Put do_io recving code into own function. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 891ce4e469e4cf1971ee30a015222e4e5b2093df Author: David Sterba Date: Mon Aug 20 17:07:24 2007 +0200 ipwireless_cs: Split do_io() send and receive Split send and receive functionality inside do_io. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 876767a5a411483a34c3f18289cc928c237426e2 Author: David Sterba Date: Thu Aug 9 13:37:27 2007 +0200 ipwireless_cs: Use memset Use memset instead of zeroing loop. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit e29f12897359edf232422dbcdb1b391cf6c5cf18 Author: Jiri Kosina Date: Thu Aug 9 13:37:13 2007 +0200 ipwireless_cs: Cleanups spotted by checkpatch.pl As suggested by checkpatch.pl Signed-off-by: Jiri Kosina commit 972d36494bda5db82ea269ff22a349af6a486cf9 Author: Jiri Kosina Date: Thu Jul 12 17:04:31 2007 +0200 Revert "ipwireless_cs: Network code refactoring" This reverts commit 19076b803e12ad37af19e8dd3a738891f0462cb5. commit c31465b216e67441fc9efbff18280351e8ad293e Author: Jiri Kosina Date: Thu Jul 12 17:04:14 2007 +0200 Revert "ipwireless_cs: Use is_ppp_blocked" This reverts commit 809098ed9786838ffc2d126af8d36551917de0c0. commit 65e4bb02f6535f486ff6600201637cad52a9f061 Author: Jiri Kosina Date: Thu Jun 28 00:09:29 2007 +0200 ipwireless_cs: Fix whitespace damage Remove trailing whitespace. Signed-off-by: Jiri Kosina commit 16c1c7a98feb707318e6dee2e6b60c0d95578c80 Author: David Sterba Date: Mon Jun 18 16:29:12 2007 +0200 ipwireless_cs: Minor cleanup: merge hw interrupt ack Merge pcmcia interrupt acknowledgement calls. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 5329d2a273eb2a51cfcbaed4e2d15d9bf67414bb Author: David Sterba Date: Fri Jun 15 13:59:22 2007 +0200 ipwireless_cs: Move tasklet scheduling to own func Merge tasklet scheduling code to func from all callsites. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 949f73144c302805257bf69368beff8d19872ed6 Author: David Sterba Date: Fri Jun 15 13:59:17 2007 +0200 ipwireless_cs: Rename initializing funcs Rename initializing funcs to match versioning naming scheme, update callers. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit ceee838717c27bf727a2baa63ff86e8dc917e62e Author: David Sterba Date: Fri Jun 15 13:59:12 2007 +0200 ipwireless_cs: Remove unnecessary prototypes Remove unnecessary prototypes for static functions. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 809098ed9786838ffc2d126af8d36551917de0c0 Author: David Sterba Date: Wed Jun 6 15:03:28 2007 +0200 ipwireless_cs: Use is_ppp_blocked Forgotten usage of is_ppp_blocked after previous refactoring Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 7f7e3ce3c6d49611087e422bb90c847f4d83b783 Author: David Sterba Date: Wed Jun 6 11:59:18 2007 +0200 ipwireless_cs: Uncamelize variables in hw Rename variables from camel notation. Pick better name when possible. Signed-off-by: David Sterba Signed-off-by: Jiri Kosina commit 051ee1e9cc1fd869b6804a2dc10e7fa51a5342a2 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 c8646283efcf9b9cbde523978eaf1eada7709445 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 2ffa059b04d460f8ec3ca3396c2d1327494826d4 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 f9f36294c0c198d0de3311ad718b0f294c296f01 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 b9411cc109ae121c937de0eb19b22d211cc57629 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 19076b803e12ad37af19e8dd3a738891f0462cb5 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 18faa2c9f0f31b1236f4275de1a9a853155a71dd 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 282abf6f45261bf663b450838d60885fccf8024d 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 b5a2470e0cbd017525d9ca356d57c4b3eb9752e8 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 944bfe5449dc0cadb8c222d20ec0f819c1dc8703 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 51e3c96d32a67497666b3caafa85b6faac7dd2c4 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 b62dc6a70ad11f4f60274af7b1cb1567489c8191 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 d33b119cb1bdf6c6b869c43901fbd734ba5ddf38 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 25a81464c6c9daf8208f85bcb53e60fffd67c4e3 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 dc8dbb79a4bd215b20f045ec019b6c637045fd31 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 e0d380b5b0cbddab13d1a76a5d99e875cbd28413 Author: Jiri Kosina Date: Wed Mar 14 14:02:42 2007 +0100 ipwireless_cs: update driver version Signed-off-by: Jiri Kosina commit 4976021a4f5c07fb53cff7f50327725146d055ec 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 214e011e47787c11101e736f00381690eca19aa8 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 8afbb76752b7be2b13fd7d452abd894d7a16c81e 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 2b795ff9e4325443aa8a0b67fe7acf63cbb46f51 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 3b28985ff1356dcd4d0d3e7812b90e2661caf37f 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 8105a5aacbd08d907923743966d4b4c30bf6a04d 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 c7b00d4edd0c8b885bc547c4ec6cb24907ea5a25 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 f7a057c39baca1523c324a0396b442731faa0d9a 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 385b2f81f451184fbd9d3b0875653d6150c70d3d 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 13b489baff531dd59520502966b4855291ed44b0 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 bc200ccbf5153067f30ec7a306770c186e84932b 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 daf86d0c8add3b7b734db82af3b465f4fda2bc84 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 0860433c8a046a89bece88a9af8c9122fb568217 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 9e8db899c78da28dbb252f282b01cfedcc120c5b 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 188b1182866c84ed387eb94b438cddbf363d842a 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 80bf42eccb04f7ce6d3dceb9084bfac24dbdde8c 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 b61a07a813491f4f936b878d2aa71f226ddc480d 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 4b317e8409803a5e7a9a7aded56cdd7a9aa2441e 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 3b1e6f0685f6f752adb42ffd1a83421c64516f73 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 40f584f6802a5438df158d24b9c545a9ce7680ac 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 | 1697 ++++++++++++++++++++ drivers/char/pcmcia/ipwireless_cs_hardware.h | 61 + drivers/char/pcmcia/ipwireless_cs_main.c | 483 ++++++ drivers/char/pcmcia/ipwireless_cs_main.h | 72 + drivers/char/pcmcia/ipwireless_cs_network.c | 498 ++++++ drivers/char/pcmcia/ipwireless_cs_network.h | 51 + drivers/char/pcmcia/ipwireless_cs_setup_protocol.h | 122 + drivers/char/pcmcia/ipwireless_cs_tty.c | 809 ++++++++++ drivers/char/pcmcia/ipwireless_cs_tty.h | 45 + 11 files changed, 3850 insertions(+), 0 deletions(-) diff --git a/drivers/char/pcmcia/Kconfig b/drivers/char/pcmcia/Kconfig index f25facd..2db3aac 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..076c783 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_hardware.c @@ -0,0 +1,1697 @@ +/* + * 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 int get_packets_from_hw(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(nl_packet_header) since it's a bitfield) */ +#define NL_FIRST_PACKET_HEADER_SIZE 3 + +/* Number of bytes in NL packet header (can not do + * sizeof(nl_packet_header) since it's a bitfield) */ +#define NL_FOLLOWING_PACKET_HEADER_SIZE 1 + +struct nl_first_paket_header { +#if defined(__BIG_ENDIAN) + unsigned int packet_rank:2; + unsigned int address:3; + unsigned int protocol:3; +#else + unsigned int protocol:3; + unsigned int address:3; + unsigned int packet_rank:2; +#endif + unsigned char length_lsb; + unsigned char length_msb; +} __attribute__ ((__packed__)); + +struct nl_packet_header { +#if defined(__BIG_ENDIAN) + unsigned int packet_rank:2; + unsigned int address:3; + unsigned int protocol:3; +#else + unsigned int protocol:3; + unsigned int address:3; + unsigned int packet_rank:2; +#endif +} __attribute__ ((__packed__)); + +/* Value of 'packet_rank' above */ +#define NL_INTERMEDIATE_PACKET 0x0 +#define NL_LAST_PACKET 0x1 +#define NL_FIRST_PACKET 0x2 + +union nl_packet { + /* Network packet header of the first packet (a special case) */ + struct nl_first_paket_header hdrFirst; + /* Network packet header of the following packets (if any) */ + struct nl_packet_header hdr; + /* Complete network packet (header + data) */ + unsigned char rawpkt[LL_MTU_MAX]; +} __attribute__ ((__packed__)); + +#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 memreg_tx_old; /* TX Register (R/W) */ + unsigned short pad1; + unsigned short memreg_rx_done; /* RXDone Register (R/W) */ + unsigned short pad2; + unsigned short memreg_rx; /* RX Register (R/W) */ + unsigned short pad3; + unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */ + unsigned short pad4; + unsigned long memreg_card_present;/* Mask for Host to check (R) for + * CARD_PRESENT_VALUE */ + unsigned short memreg_tx_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_hardware { + unsigned int base_port; + short hw_version; + unsigned short ll_mtu; + 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 this driver is shutting down. */ + int shutting_down; + /* Modem control lines */ + unsigned int control_lines[NL_NUM_OF_ADDRESSES]; + struct ipw_rx_packet* 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 *memory_info_regs; + struct MEMCCR __iomem *memregs_CCR; + int bad_interrupt_count; + void (*reboot_callback) (void *data); + void *reboot_callback_data; + + unsigned short __iomem *memreg_tx; +}; + +/* + * 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 */ +enum ComCtrl_DTESignal { + ComCtrl_RTS = 0, + ComCtrl_DTR = 1 +}; + +/* Signals from DCE */ +enum ComCtrl_DCESignal { + ComCtrl_CTS = 2, + ComCtrl_DCD = 3, + ComCtrl_DSR = 4, + ComCtrl_RI = 5 +}; + +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: 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 nl_packet_header *hdr = (struct nl_packet_header *) buf; + + if (length == 0) + return " "; + + if (hdr->packet_rank & NL_FIRST_PACKET) { + switch(hdr->protocol) { + case TL_PROTOCOLID_COM_DATA: return "DATA "; + case TL_PROTOCOLID_COM_CTRL: return "CTRL "; + case TL_PROTOCOLID_SETUP: return "SETUP"; + default: 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 == 0) + return 0; + + if (length > hw->ll_mtu) + return -1; + + if (ipwireless_cs_debug) { + char buf[DUMP_MAX_BYTES * 4 + 16]; + + 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->hw_version == 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->hw_version == 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->memory_info_regs->memreg_rx); + } + + 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; + union nl_packet 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->ll_mtu - 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.packet_rank = 0; + + /* First packet? */ + if (packet->fragment_count == 0) { + pkt.hdrFirst.packet_rank |= 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.packet_rank |= 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_rx_packet **assem = + &hw->packet_assembler[channel_idx]; + + /* + * Create a new packet, or assembler already contains one + * enlarge it by 'length' bytes. + */ + (*assem) = pool_allocate(hw, *assem, length); + (*assem)->protocol = protocol; + (*assem)->channel_idx = channel_idx; + + /* Append this packet data onto existing data. */ + memcpy((unsigned char *)(*assem) + + sizeof(struct ipw_rx_packet) + + (*assem)->length, data, length); + (*assem)->length += length; + if (is_last) { + packet = *assem; + *assem = NULL; + /* Count queued DATA bytes only */ + hw->rx_bytes_queued += packet->length; + } + } else { + /* If it's a CTRL packet, don't assemble, 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); + } +} + +/* + * Workqueue callback + */ +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; + unsigned long flags; + + spin_lock_irqsave(&hw->spinlock, flags); + 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_irqrestore(&hw->spinlock, flags); + ipwireless_network_packet_received( + hw->network, + packet->channel_idx, + (unsigned char *)packet + + sizeof(struct ipw_rx_packet), + packet->length); + spin_lock_irqsave(&hw->spinlock, flags); + } + /* Count queued DATA bytes only */ + hw->rx_bytes_queued -= packet->length; + } else { + spin_unlock_irqrestore(&hw->spinlock, flags); + handle_received_CTRL_packet(hw, packet->channel_idx, + (unsigned char *)packet + + sizeof(struct ipw_rx_packet), + packet->length); + spin_lock_irqsave(&hw->spinlock, flags); + } + 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; + + get_packets_from_hw(hw); + } + spin_unlock_irqrestore(&hw->spinlock, flags); +} + +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, union nl_packet *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.packet_rank & NL_LAST_PACKET; + + if (packet->hdr.packet_rank & 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->hw_version == HW_VERSION_1) { + len = inw(hw->base_port + IODRR); + if (len > hw->ll_mtu) { + 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->ll_mtu) { + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": received a packet of %d bytes - " + "longer than the MTU!\n", len); + iowrite16(MEMRX_PCINTACKK, + &hw->memory_info_regs->memreg_pc_interrupt_ack); + 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, (union nl_packet *) pkt, len); + /* ACK reading */ + if (hw->hw_version == HW_VERSION_1) + outw(DCR_RXDONE, hw->base_port + IODCR); + else + iowrite16(MEMRX_PCINTACKK, + &hw->memory_info_regs->memreg_pc_interrupt_ack); + + end_read_timing(len); +} + +static int get_current_packet_priority(struct ipw_hardware *hw) +{ + /* + * 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. + */ + return (hw->to_setup || hw->initializing + ? PRIO_SETUP + 1 : + NL_NUM_OF_PRIORITIES); +} + +/* + * TODO: remove intra-func spinlock + * + * @return 1 if something has been received from hw + */ +static int get_packets_from_hw(struct ipw_hardware* hw) +{ + int received = 0; + + while (hw->rx_ready && !hw->blocking_rx) { + received = 1; + hw->rx_ready--; + spin_unlock_irq(&hw->spinlock); + do_receive_packet(hw); + spin_lock_irq(&hw->spinlock); + } + + return received; +} + +/* + * Send pending packet up to given priority, prioritize SETUP data until + * hardware is fully setup. + * + * @return 1 if more packets can be sent + */ +static int send_pending_packet(struct ipw_hardware *hw, int priority_limit) +{ + int more_to_send = 0; + + if (hw->tx_queued && hw->tx_ready != 0) { + int priority; + + hw->tx_ready--; + + 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); + + /* + * 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])) { + more_to_send = 1; + break; + } + + if (!more_to_send) + hw->tx_queued = 0; + } + + return more_to_send; +} + +/* + * Send and receive all queued packets. Must be called with spin_lock locked. + * May be called either from a interrupt tasklet, or from a process context. + */ +static void process_send_and_recv_queues(struct ipw_hardware *hw) +{ + if (hw->shutting_down) + return; + + /* 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); + send_pending_packet(hw, PRIO_SETUP + 1); + get_packets_from_hw(hw); + } else { + int priority_limit = get_current_packet_priority(hw); + int again = 0; + + do { + again = send_pending_packet(hw, priority_limit); + again |= get_packets_from_hw(hw); + }while(again); + } +} + +static void ipwireless_do_tasklet(unsigned long hw_) +{ + struct ipw_hardware *hw = (struct ipw_hardware *) hw_; + + spin_lock_irq(&hw->spinlock); + process_send_and_recv_queues(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->hw_version == HW_VERSION_1) + return inw(hw->base_port + IOIR) != (unsigned short) 0xFFFF; + else + return ioread32(&hw->memory_info_regs->memreg_card_present) == + CARD_PRESENT_VALUE; +} + +/* + * Schedule IO retrieval to be performed in tasklet, because ppp_generic may be + * called from a tasklet but not from a hardware interrupt. + */ +static void ipw_schedule_tasklet(struct ipw_hardware *hw) +{ + if (!hw->tasklet_pending) { + hw->tasklet_pending = 1; + tasklet_schedule(&hw->tasklet); + } +} + +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); + + /* Check if card is present */ + 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); + ipw_schedule_tasklet(hw); + } + return IRQ_HANDLED; + } else + return IRQ_NONE; + +} + +static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw) +{ + unsigned short csr = ioread16(&hw->memregs_CCR->PCCSR); + + csr &= 0xfffd; + iowrite16(csr, &hw->memregs_CCR->PCCSR); +} + +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->memreg_tx); + unsigned short memtx_serial; + unsigned short memrxdone = + ioread16(&hw->memory_info_regs->memreg_rx_done); + + /* 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 (!is_card_present(hw)) { + acknowledge_pcmcia_interrupt(hw); + return IRQ_HANDLED; + } + + memtx_serial = memtx & (unsigned short) 0xff00; + if (memtx & MEMTX_TX) { + iowrite16(0, hw->memreg_tx); + + 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 + ": memreg_tx serial num detected\n"); + + hw->rx_ready++; + rx = 1; + } + } + if (memrxdone & MEMRX_RX_DONE) { + iowrite16(0, &hw->memory_info_regs->memreg_rx_done); + hw->tx_ready++; + tx = 1; + } + if (tx) + iowrite16(MEMRX_PCINTACKK, + &hw->memory_info_regs->memreg_pc_interrupt_ack); + + acknowledge_pcmcia_interrupt(hw); + + if (tx || rx) + ipw_schedule_tasklet(hw); + else if (!rx_repeat) { + if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) { + printk(KERN_WARNING IPWIRELESS_PCCARD_NAME + ": No valid memreg_tx val - switching" + "to the old register address.\n"); + hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old; + } else { + printk(KERN_WARNING IPWIRELESS_PCCARD_NAME + ": spurious interrupt!\n"); + hw->irq = irq; + hw->removed = 1; + disable_irq_nosync(irq); + } + } + + 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->hw_version == HW_VERSION_1) + return ipwireless_handle_v1_interrupt(irq, hw); + else + return ipwireless_handle_v2_v3_interrupt(irq, hw); +} + +static void flush_packets_to_hw(struct ipw_hardware *hw) +{ + int priority_limit = get_current_packet_priority(hw); + + while(send_pending_packet(hw, priority_limit)); +} + +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; + flush_packets_to_hw(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; + + /* + * NOTE: This is due to wild spinlocking accross function boundaries + */ + 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); +} + +struct ipw_setup_get_version_query_packet_t { + struct ipw_tx_packet packet; + struct TlSetupGetVersionQry body; +}; + +struct ipw_setup_config_packet_t { + struct ipw_tx_packet packet; + struct TlSetupConfigMsg body; +}; + +struct ipw_setup_config_done_packet_t { + struct ipw_tx_packet packet; + struct TlSetupConfigDoneMsg body; +}; + +struct ipw_setup_open_packet_t { + struct ipw_tx_packet packet; + struct TlSetupOpenMsg body; +}; + +typedef struct ipw_setup_info_packet_t { + struct ipw_tx_packet packet; + struct TlSetupInfoMsg body; +} ipw_setup_info_packet_t; + +struct ipw_setup_reboot_msg_ack_t { + struct ipw_tx_packet packet; + struct TlSetupRebootMsgAck body; +}; + +/* This handles the actual initialization of the card */ +static void __handle_setup_get_version_rsp(struct ipw_hardware *hw) +{ + struct ipw_setup_config_packet_t *config_packet; + struct ipw_setup_config_done_packet_t *config_done_packet; + struct 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(struct ipw_setup_config_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + config_packet->packet. length = sizeof(struct 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(struct ipw_setup_config_done_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + config_done_packet->packet.length = sizeof(struct 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(struct ipw_setup_open_packet_t), + ADDR_SETUP_PROT, + TL_PROTOCOLID_SETUP, + free_packet); + open_packet->packet.length = sizeof(struct 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(struct 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 NDISWAN\n"); + break; + + case TL_SETUP_SIGNO_REBOOT_MSG: + if (hw->to_setup) + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": Setup not completed - ignoring reboot msg\n"); + else { + struct ipw_setup_reboot_msg_ack_t *packet; + + printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME + ": Acknowledging REBOOT message\n"); + packet = create_packet( + sizeof(struct ipw_setup_reboot_msg_ack_t), + ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP, + free_packet); + packet->packet.length = sizeof(struct 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) +{ + struct ipw_setup_get_version_query_packet_t *ver_packet; + + if (hw->hw_version == 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->memregs_CCR->PCCSR); + + csr |= 1; + iowrite16(csr, &hw->memregs_CCR->PCCSR); + } + + ver_packet = create_packet(sizeof(struct 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(struct 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->hw_version == 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_hardware_v1(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->hw_version = isV2Card ? HW_VERSION_2 : HW_VERSION_1; + hw->ll_mtu = hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2; + hw->memregs_CCR = (struct MEMCCR __iomem *) + ((unsigned short __iomem *) attr_memory + 0x200); + hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory; + hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new; + hw->reboot_callback = reboot_callback; + hw->reboot_callback_data = reboot_callback_data; +} + +void ipwireless_init_hardware_v2_v3(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 { + /* Do not attempt to write to the board if it is not present. */ + if (is_card_present(hw)) { + unsigned long flags; + + hw->to_setup = 1; + hw->tx_ready = 1; + spin_lock_irqsave(&hw->spinlock, flags); + ipw_schedule_tasklet(hw); + 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] != NULL) + kfree(hw->packet_assembler[i]); + + 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..8a4b509 --- /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_hardware_v1(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_hardware_v2_v3(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..a708df4 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_main.c @@ -0,0 +1,483 @@ +/* + * 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_hardware_v1(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_hardware_v2_v3(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..905ceb1 --- /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..6a43ed4 --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_network.c @@ -0,0 +1,498 @@ +/* + * 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]; + /* True if ppp needs waking up once we're ready to xmit */ + int ppp_blocked; + /* 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; +}; + + +#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", + network->ppp_blocked); + 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; + + spin_lock_irqsave(&network->spinlock, flags); + network->outgoing_packets_queued--; + if (network->ppp_channel != NULL) { + if (network->ppp_blocked) { + network->ppp_blocked = 0; + spin_unlock_irqrestore(&network->spinlock, flags); + ppp_output_wakeup(network->ppp_channel); + if (ipwireless_cs_debug) + printk(KERN_INFO IPWIRELESS_PCCARD_NAME + ": ppp unblocked\n"); + } else + spin_unlock_irqrestore(&network->spinlock, flags); + } else + spin_unlock_irqrestore(&network->spinlock, flags); +} + +/* + * Called by the ppp system when it has a packet to send to the hardware. + */ +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 { + /* + * Otherwise reject the packet, and flag that the ppp system + * needs to be unblocked once we are ready to send. + * */ + network->ppp_blocked = 1; + spin_unlock_irqrestore(&network->spinlock, flags); + 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; + memset(network->xaccm, 0, sizeof(network->xaccm)); + 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..ee20cdb --- /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 */ +struct TlSetupGetVersionQry { + unsigned char sigNo; /* TL_SETUP_SIGNO_GET_VERSION_QRY */ +} __attribute__ ((__packed__)); + +struct TlSetupGetVersionRsp { + unsigned char sigNo; /* TL_SETUP_SIGNO_GET_VERSION_RSP */ + unsigned char version; /* TL_SETUP_VERSION */ +} __attribute__ ((__packed__)); + +struct TlSetupConfigMsg { + unsigned char sigNo; /* TL_SETUP_SIGNO_CONFIG_MSG */ + unsigned char portNo; + unsigned char prioData; + unsigned char prioCtrl; +} __attribute__ ((__packed__)); + +struct TlSetupConfigDoneMsg { + unsigned char sigNo; /* TL_SETUP_SIGNO_CONFIG_DONE_MSG */ +} __attribute__ ((__packed__)); + +/* Asyncronous messages */ +struct TlSetupOpenMsg { + unsigned char sigNo; /* TL_SETUP_SIGNO_OPEN_MSG */ + unsigned char portNo; +} __attribute__ ((__packed__)); + +struct TlSetupCloseMsg { + unsigned char sigNo; /* TL_SETUP_SIGNO_CLOSE_MSG */ + unsigned char portNo; +} __attribute__ ((__packed__)); + +/* 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. + */ +struct TlSetupInfoMsg { + unsigned char sigNo; /* TL_SETUP_SIGNO_INFO_MSG */ + unsigned char driverType; + unsigned char majorVersion; + unsigned char minorVersion; +} __attribute__ ((__packed__)); + +struct TlSetupInfoMsgAck { + unsigned char sigNo; /* TL_SETUP_SIGNO_INFO_MSG_ACK */ +} __attribute__ ((__packed__)); + +struct TlSetupRebootMsgAck { + unsigned char sigNo; /* TL_SETUP_SIGNO_REBOOT_MSG_ACK */ +} __attribute__ ((__packed__)); + +/* Define a union of all the msgs that the driver can receive from the card.*/ +typedef union { + unsigned char sigNo; + struct TlSetupGetVersionRsp VersRspMsg; + struct TlSetupOpenMsg OpenMsg; + struct TlSetupCloseMsg CloseMsg; + struct TlSetupInfoMsg InfoMsg; + struct 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; + struct TlSetupGetVersionQry QryMsg; + struct TlSetupConfigMsg ConfigMsg; + struct TlSetupConfigDoneMsg ConfigDoneMsg; + struct TlSetupOpenMsg OpenMsg; + struct TlSetupCloseMsg CloseMsg; + struct TlSetupInfoMsg InfoMsg; + struct 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..162cedd --- /dev/null +++ b/drivers/char/pcmcia/ipwireless_cs_tty.c @@ -0,0 +1,809 @@ +/* + * 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 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