blob: 814ea3228ca20646ba5226df7c258d05669db85f [file] [log] [blame]
David Sterba099dc4f2008-02-07 10:57:12 +01001/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23#include <linux/slab.h>
24
25#include "hardware.h"
26#include "setup_protocol.h"
27#include "network.h"
28#include "main.h"
29
30static void ipw_send_setup_packet(struct ipw_hardware *hw);
31static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
32 unsigned int address,
David Sterbaff3e9902008-07-28 16:53:11 +020033 const unsigned char *data, int len,
David Sterba099dc4f2008-02-07 10:57:12 +010034 int is_last);
35static void ipwireless_setup_timer(unsigned long data);
36static void handle_received_CTRL_packet(struct ipw_hardware *hw,
David Sterbaff3e9902008-07-28 16:53:11 +020037 unsigned int channel_idx, const unsigned char *data, int len);
David Sterba099dc4f2008-02-07 10:57:12 +010038
39/*#define TIMING_DIAGNOSTICS*/
40
41#ifdef TIMING_DIAGNOSTICS
42
43static struct timing_stats {
44 unsigned long last_report_time;
45 unsigned long read_time;
46 unsigned long write_time;
47 unsigned long read_bytes;
48 unsigned long write_bytes;
49 unsigned long start_time;
50};
51
52static void start_timing(void)
53{
54 timing_stats.start_time = jiffies;
55}
56
57static void end_read_timing(unsigned length)
58{
59 timing_stats.read_time += (jiffies - start_time);
60 timing_stats.read_bytes += length + 2;
61 report_timing();
62}
63
64static void end_write_timing(unsigned length)
65{
66 timing_stats.write_time += (jiffies - start_time);
67 timing_stats.write_bytes += length + 2;
68 report_timing();
69}
70
71static void report_timing(void)
72{
73 unsigned long since = jiffies - timing_stats.last_report_time;
74
75 /* If it's been more than one second... */
76 if (since >= HZ) {
77 int first = (timing_stats.last_report_time == 0);
78
79 timing_stats.last_report_time = jiffies;
80 if (!first)
81 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
David Sterba622e7132008-07-28 16:52:55 +020082 ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
David Sterba099dc4f2008-02-07 10:57:12 +010083 jiffies_to_usecs(since),
84 timing_stats.read_bytes,
85 jiffies_to_usecs(timing_stats.read_time),
86 timing_stats.write_bytes,
87 jiffies_to_usecs(timing_stats.write_time));
88
89 timing_stats.read_time = 0;
90 timing_stats.write_time = 0;
91 timing_stats.read_bytes = 0;
92 timing_stats.write_bytes = 0;
93 }
94}
95#else
96static void start_timing(void) { }
97static void end_read_timing(unsigned length) { }
98static void end_write_timing(unsigned length) { }
99#endif
100
101/* Imported IPW definitions */
102
103#define LL_MTU_V1 318
104#define LL_MTU_V2 250
105#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
106
107#define PRIO_DATA 2
108#define PRIO_CTRL 1
109#define PRIO_SETUP 0
110
111/* Addresses */
112#define ADDR_SETUP_PROT 0
113
114/* Protocol ids */
115enum {
116 /* Identifier for the Com Data protocol */
117 TL_PROTOCOLID_COM_DATA = 0,
118
119 /* Identifier for the Com Control protocol */
120 TL_PROTOCOLID_COM_CTRL = 1,
121
122 /* Identifier for the Setup protocol */
123 TL_PROTOCOLID_SETUP = 2
124};
125
126/* Number of bytes in NL packet header (cannot do
127 * sizeof(nl_packet_header) since it's a bitfield) */
128#define NL_FIRST_PACKET_HEADER_SIZE 3
129
130/* Number of bytes in NL packet header (cannot do
131 * sizeof(nl_packet_header) since it's a bitfield) */
132#define NL_FOLLOWING_PACKET_HEADER_SIZE 1
133
134struct nl_first_packet_header {
David Sterba099dc4f2008-02-07 10:57:12 +0100135 unsigned char protocol:3;
136 unsigned char address:3;
137 unsigned char packet_rank:2;
David Sterba099dc4f2008-02-07 10:57:12 +0100138 unsigned char length_lsb;
139 unsigned char length_msb;
140};
141
142struct nl_packet_header {
David Sterba099dc4f2008-02-07 10:57:12 +0100143 unsigned char protocol:3;
144 unsigned char address:3;
145 unsigned char packet_rank:2;
David Sterba099dc4f2008-02-07 10:57:12 +0100146};
147
148/* Value of 'packet_rank' above */
149#define NL_INTERMEDIATE_PACKET 0x0
150#define NL_LAST_PACKET 0x1
151#define NL_FIRST_PACKET 0x2
152
153union nl_packet {
154 /* Network packet header of the first packet (a special case) */
155 struct nl_first_packet_header hdr_first;
156 /* Network packet header of the following packets (if any) */
157 struct nl_packet_header hdr;
158 /* Complete network packet (header + data) */
159 unsigned char rawpkt[LL_MTU_MAX];
160} __attribute__ ((__packed__));
161
162#define HW_VERSION_UNKNOWN -1
163#define HW_VERSION_1 1
164#define HW_VERSION_2 2
165
166/* IPW I/O ports */
167#define IOIER 0x00 /* Interrupt Enable Register */
168#define IOIR 0x02 /* Interrupt Source/ACK register */
169#define IODCR 0x04 /* Data Control Register */
170#define IODRR 0x06 /* Data Read Register */
171#define IODWR 0x08 /* Data Write Register */
172#define IOESR 0x0A /* Embedded Driver Status Register */
173#define IORXR 0x0C /* Rx Fifo Register (Host to Embedded) */
174#define IOTXR 0x0E /* Tx Fifo Register (Embedded to Host) */
175
176/* I/O ports and bit definitions for version 1 of the hardware */
177
178/* IER bits*/
179#define IER_RXENABLED 0x1
180#define IER_TXENABLED 0x2
181
182/* ISR bits */
183#define IR_RXINTR 0x1
184#define IR_TXINTR 0x2
185
186/* DCR bits */
187#define DCR_RXDONE 0x1
188#define DCR_TXDONE 0x2
189#define DCR_RXRESET 0x4
190#define DCR_TXRESET 0x8
191
192/* I/O ports and bit definitions for version 2 of the hardware */
193
194struct MEMCCR {
195 unsigned short reg_config_option; /* PCCOR: Configuration Option Register */
196 unsigned short reg_config_and_status; /* PCCSR: Configuration and Status Register */
197 unsigned short reg_pin_replacement; /* PCPRR: Pin Replacemant Register */
198 unsigned short reg_socket_and_copy; /* PCSCR: Socket and Copy Register */
199 unsigned short reg_ext_status; /* PCESR: Extendend Status Register */
200 unsigned short reg_io_base; /* PCIOB: I/O Base Register */
201};
202
203struct MEMINFREG {
204 unsigned short memreg_tx_old; /* TX Register (R/W) */
205 unsigned short pad1;
206 unsigned short memreg_rx_done; /* RXDone Register (R/W) */
207 unsigned short pad2;
208 unsigned short memreg_rx; /* RX Register (R/W) */
209 unsigned short pad3;
210 unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */
211 unsigned short pad4;
212 unsigned long memreg_card_present;/* Mask for Host to check (R) for
213 * CARD_PRESENT_VALUE */
214 unsigned short memreg_tx_new; /* TX2 (new) Register (R/W) */
215};
216
David Sterba099dc4f2008-02-07 10:57:12 +0100217#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
218
219#define MEMTX_TX 0x0001
220#define MEMRX_RX 0x0001
221#define MEMRX_RX_DONE 0x0001
222#define MEMRX_PCINTACKK 0x0001
David Sterba099dc4f2008-02-07 10:57:12 +0100223
224#define NL_NUM_OF_PRIORITIES 3
225#define NL_NUM_OF_PROTOCOLS 3
226#define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS
227
228struct ipw_hardware {
229 unsigned int base_port;
230 short hw_version;
231 unsigned short ll_mtu;
David Sterba63c4dbd2008-07-28 16:52:44 +0200232 spinlock_t lock;
David Sterba099dc4f2008-02-07 10:57:12 +0100233
234 int initializing;
235 int init_loops;
236 struct timer_list setup_timer;
237
David Sterbaeb4e5452008-06-06 10:56:35 +0200238 /* Flag if hw is ready to send next packet */
David Sterba099dc4f2008-02-07 10:57:12 +0100239 int tx_ready;
David Sterbaeb4e5452008-06-06 10:56:35 +0200240 /* Count of pending packets to be sent */
David Sterba099dc4f2008-02-07 10:57:12 +0100241 int tx_queued;
David Sterbaeb4e5452008-06-06 10:56:35 +0200242 struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
David Sterba099dc4f2008-02-07 10:57:12 +0100243
244 int rx_bytes_queued;
245 struct list_head rx_queue;
246 /* Pool of rx_packet structures that are not currently used. */
247 struct list_head rx_pool;
248 int rx_pool_size;
249 /* True if reception of data is blocked while userspace processes it. */
250 int blocking_rx;
251 /* True if there is RX data ready on the hardware. */
252 int rx_ready;
253 unsigned short last_memtx_serial;
254 /*
255 * Newer versions of the V2 card firmware send serial numbers in the
256 * MemTX register. 'serial_number_detected' is set true when we detect
257 * a non-zero serial number (indicating the new firmware). Thereafter,
258 * the driver can safely ignore the Timer Recovery re-sends to avoid
259 * out-of-sync problems.
260 */
261 int serial_number_detected;
262 struct work_struct work_rx;
263
264 /* True if we are to send the set-up data to the hardware. */
265 int to_setup;
266
267 /* Card has been removed */
268 int removed;
269 /* Saved irq value when we disable the interrupt. */
270 int irq;
271 /* True if this driver is shutting down. */
272 int shutting_down;
273 /* Modem control lines */
274 unsigned int control_lines[NL_NUM_OF_ADDRESSES];
275 struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
276
277 struct tasklet_struct tasklet;
278
279 /* The handle for the network layer, for the sending of events to it. */
280 struct ipw_network *network;
281 struct MEMINFREG __iomem *memory_info_regs;
282 struct MEMCCR __iomem *memregs_CCR;
283 void (*reboot_callback) (void *data);
284 void *reboot_callback_data;
285
286 unsigned short __iomem *memreg_tx;
287};
288
289/*
290 * Packet info structure for tx packets.
291 * Note: not all the fields defined here are required for all protocols
292 */
293struct ipw_tx_packet {
294 struct list_head queue;
295 /* channel idx + 1 */
296 unsigned char dest_addr;
297 /* SETUP, CTRL or DATA */
298 unsigned char protocol;
299 /* Length of data block, which starts at the end of this structure */
300 unsigned short length;
301 /* Sending state */
302 /* Offset of where we've sent up to so far */
303 unsigned long offset;
304 /* Count of packet fragments, starting at 0 */
305 int fragment_count;
306
307 /* Called after packet is sent and before is freed */
308 void (*packet_callback) (void *cb_data, unsigned int packet_length);
309 void *callback_data;
310};
311
312/* Signals from DTE */
313#define COMCTRL_RTS 0
314#define COMCTRL_DTR 1
315
316/* Signals from DCE */
317#define COMCTRL_CTS 2
318#define COMCTRL_DCD 3
319#define COMCTRL_DSR 4
320#define COMCTRL_RI 5
321
322struct ipw_control_packet_body {
323 /* DTE signal or DCE signal */
324 unsigned char sig_no;
325 /* 0: set signal, 1: clear signal */
326 unsigned char value;
327} __attribute__ ((__packed__));
328
329struct ipw_control_packet {
330 struct ipw_tx_packet header;
331 struct ipw_control_packet_body body;
332};
333
334struct ipw_rx_packet {
335 struct list_head queue;
336 unsigned int capacity;
337 unsigned int length;
338 unsigned int protocol;
339 unsigned int channel_idx;
340};
341
David Sterba099dc4f2008-02-07 10:57:12 +0100342static char *data_type(const unsigned char *buf, unsigned length)
343{
344 struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
345
346 if (length == 0)
347 return " ";
348
349 if (hdr->packet_rank & NL_FIRST_PACKET) {
350 switch (hdr->protocol) {
351 case TL_PROTOCOLID_COM_DATA: return "DATA ";
352 case TL_PROTOCOLID_COM_CTRL: return "CTRL ";
353 case TL_PROTOCOLID_SETUP: return "SETUP";
354 default: return "???? ";
355 }
356 } else
357 return " ";
358}
359
360#define DUMP_MAX_BYTES 64
361
362static void dump_data_bytes(const char *type, const unsigned char *data,
363 unsigned length)
364{
365 char prefix[56];
366
367 sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
368 type, data_type(data, length));
369 print_hex_dump_bytes(prefix, 0, (void *)data,
370 length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
371}
372
David Sterbad54c2752008-07-28 16:53:00 +0200373static void swap_packet_bitfield_to_le(unsigned char *data)
374{
375#ifdef __BIG_ENDIAN_BITFIELD
376 unsigned char tmp = *data, ret = 0;
377
378 /*
379 * transform bits from aa.bbb.ccc to ccc.bbb.aa
380 */
381 ret |= tmp & 0xc0 >> 6;
382 ret |= tmp & 0x38 >> 1;
383 ret |= tmp & 0x07 << 5;
384 *data = ret & 0xff;
385#endif
386}
387
388static void swap_packet_bitfield_from_le(unsigned char *data)
389{
390#ifdef __BIG_ENDIAN_BITFIELD
391 unsigned char tmp = *data, ret = 0;
392
393 /*
394 * transform bits from ccc.bbb.aa to aa.bbb.ccc
395 */
396 ret |= tmp & 0xe0 >> 5;
397 ret |= tmp & 0x1c << 1;
398 ret |= tmp & 0x03 << 6;
399 *data = ret & 0xff;
400#endif
401}
402
David Sterba93110f62008-07-28 16:53:05 +0200403static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
David Sterba099dc4f2008-02-07 10:57:12 +0100404 unsigned length)
405{
David Sterbad4c0deb2008-07-28 16:52:33 +0200406 unsigned i;
David Sterba099dc4f2008-02-07 10:57:12 +0100407 unsigned long flags;
408
409 start_timing();
David Sterba93110f62008-07-28 16:53:05 +0200410 BUG_ON(length > hw->ll_mtu);
David Sterba099dc4f2008-02-07 10:57:12 +0100411
412 if (ipwireless_debug)
413 dump_data_bytes("send", data, length);
414
David Sterba63c4dbd2008-07-28 16:52:44 +0200415 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100416
David Sterbaeb4e5452008-06-06 10:56:35 +0200417 hw->tx_ready = 0;
David Sterbad54c2752008-07-28 16:53:00 +0200418 swap_packet_bitfield_to_le(data);
David Sterbaeb4e5452008-06-06 10:56:35 +0200419
David Sterba099dc4f2008-02-07 10:57:12 +0100420 if (hw->hw_version == HW_VERSION_1) {
421 outw((unsigned short) length, hw->base_port + IODWR);
422
423 for (i = 0; i < length; i += 2) {
424 unsigned short d = data[i];
425 __le16 raw_data;
426
David Sterbad4c0deb2008-07-28 16:52:33 +0200427 if (i + 1 < length)
David Sterba099dc4f2008-02-07 10:57:12 +0100428 d |= data[i + 1] << 8;
429 raw_data = cpu_to_le16(d);
430 outw(raw_data, hw->base_port + IODWR);
431 }
432
433 outw(DCR_TXDONE, hw->base_port + IODCR);
434 } else if (hw->hw_version == HW_VERSION_2) {
David Sterba2e713162008-07-28 16:52:39 +0200435 outw((unsigned short) length, hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100436
437 for (i = 0; i < length; i += 2) {
438 unsigned short d = data[i];
439 __le16 raw_data;
440
David Sterbad4c0deb2008-07-28 16:52:33 +0200441 if (i + 1 < length)
David Sterba099dc4f2008-02-07 10:57:12 +0100442 d |= data[i + 1] << 8;
443 raw_data = cpu_to_le16(d);
David Sterba2e713162008-07-28 16:52:39 +0200444 outw(raw_data, hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100445 }
446 while ((i & 3) != 2) {
David Sterba2e713162008-07-28 16:52:39 +0200447 outw((unsigned short) 0xDEAD, hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100448 i += 2;
449 }
450 writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
451 }
452
David Sterba63c4dbd2008-07-28 16:52:44 +0200453 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100454
455 end_write_timing(length);
David Sterba099dc4f2008-02-07 10:57:12 +0100456}
457
David Sterba93110f62008-07-28 16:53:05 +0200458static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
David Sterba099dc4f2008-02-07 10:57:12 +0100459{
460 unsigned short fragment_data_len;
461 unsigned short data_left = packet->length - packet->offset;
462 unsigned short header_size;
463 union nl_packet pkt;
464
465 header_size =
466 (packet->fragment_count == 0)
467 ? NL_FIRST_PACKET_HEADER_SIZE
468 : NL_FOLLOWING_PACKET_HEADER_SIZE;
469 fragment_data_len = hw->ll_mtu - header_size;
470 if (data_left < fragment_data_len)
471 fragment_data_len = data_left;
472
David Sterbad54c2752008-07-28 16:53:00 +0200473 /*
474 * hdr_first is now in machine bitfield order, which will be swapped
475 * to le just before it goes to hw
476 */
David Sterba099dc4f2008-02-07 10:57:12 +0100477 pkt.hdr_first.protocol = packet->protocol;
478 pkt.hdr_first.address = packet->dest_addr;
479 pkt.hdr_first.packet_rank = 0;
480
481 /* First packet? */
482 if (packet->fragment_count == 0) {
483 pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
484 pkt.hdr_first.length_lsb = (unsigned char) packet->length;
485 pkt.hdr_first.length_msb =
486 (unsigned char) (packet->length >> 8);
487 }
488
489 memcpy(pkt.rawpkt + header_size,
490 ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
491 packet->offset, fragment_data_len);
492 packet->offset += fragment_data_len;
493 packet->fragment_count++;
494
495 /* Last packet? (May also be first packet.) */
496 if (packet->offset == packet->length)
497 pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
498 do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
499
500 /* If this packet has unsent data, then re-queue it. */
501 if (packet->offset < packet->length) {
502 /*
503 * Re-queue it at the head of the highest priority queue so
504 * it goes before all other packets
505 */
506 unsigned long flags;
507
David Sterba63c4dbd2008-07-28 16:52:44 +0200508 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100509 list_add(&packet->queue, &hw->tx_queue[0]);
David Sterbaeb4e5452008-06-06 10:56:35 +0200510 hw->tx_queued++;
David Sterba63c4dbd2008-07-28 16:52:44 +0200511 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100512 } else {
513 if (packet->packet_callback)
514 packet->packet_callback(packet->callback_data,
515 packet->length);
516 kfree(packet);
517 }
David Sterba099dc4f2008-02-07 10:57:12 +0100518}
519
520static void ipw_setup_hardware(struct ipw_hardware *hw)
521{
522 unsigned long flags;
523
David Sterba63c4dbd2008-07-28 16:52:44 +0200524 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100525 if (hw->hw_version == HW_VERSION_1) {
526 /* Reset RX FIFO */
527 outw(DCR_RXRESET, hw->base_port + IODCR);
528 /* SB: Reset TX FIFO */
529 outw(DCR_TXRESET, hw->base_port + IODCR);
530
531 /* Enable TX and RX interrupts. */
532 outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
533 } else {
534 /*
535 * Set INTRACK bit (bit 0), which means we must explicitly
536 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
537 */
538 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
539
540 csr |= 1;
541 writew(csr, &hw->memregs_CCR->reg_config_and_status);
542 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200543 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100544}
545
546/*
547 * If 'packet' is NULL, then this function allocates a new packet, setting its
548 * length to 0 and ensuring it has the specified minimum amount of free space.
549 *
550 * If 'packet' is not NULL, then this function enlarges it if it doesn't
551 * have the specified minimum amount of free space.
552 *
553 */
554static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
555 struct ipw_rx_packet *packet,
556 int minimum_free_space)
557{
558
559 if (!packet) {
560 unsigned long flags;
561
David Sterba63c4dbd2008-07-28 16:52:44 +0200562 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100563 if (!list_empty(&hw->rx_pool)) {
564 packet = list_first_entry(&hw->rx_pool,
565 struct ipw_rx_packet, queue);
566 list_del(&packet->queue);
567 hw->rx_pool_size--;
David Sterba63c4dbd2008-07-28 16:52:44 +0200568 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100569 } else {
David Sterba099dc4f2008-02-07 10:57:12 +0100570 static int min_capacity = 256;
571 int new_capacity;
572
David Sterba63c4dbd2008-07-28 16:52:44 +0200573 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100574 new_capacity =
David Sterbad4c0deb2008-07-28 16:52:33 +0200575 (minimum_free_space > min_capacity
576 ? minimum_free_space
577 : min_capacity);
David Sterba099dc4f2008-02-07 10:57:12 +0100578 packet = kmalloc(sizeof(struct ipw_rx_packet)
579 + new_capacity, GFP_ATOMIC);
580 if (!packet)
581 return NULL;
582 packet->capacity = new_capacity;
583 }
584 packet->length = 0;
585 }
586
David Sterba099dc4f2008-02-07 10:57:12 +0100587 if (packet->length + minimum_free_space > packet->capacity) {
588 struct ipw_rx_packet *old_packet = packet;
589
590 packet = kmalloc(sizeof(struct ipw_rx_packet) +
591 old_packet->length + minimum_free_space,
592 GFP_ATOMIC);
Darren Jenkins43f77e92008-07-12 13:47:49 -0700593 if (!packet) {
594 kfree(old_packet);
David Sterba099dc4f2008-02-07 10:57:12 +0100595 return NULL;
Darren Jenkins43f77e92008-07-12 13:47:49 -0700596 }
David Sterba099dc4f2008-02-07 10:57:12 +0100597 memcpy(packet, old_packet,
598 sizeof(struct ipw_rx_packet)
599 + old_packet->length);
600 packet->capacity = old_packet->length + minimum_free_space;
601 kfree(old_packet);
602 }
603
604 return packet;
605}
606
607static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
608{
609 if (hw->rx_pool_size > 6)
610 kfree(packet);
611 else {
612 hw->rx_pool_size++;
613 list_add_tail(&packet->queue, &hw->rx_pool);
614 }
615}
616
617static void queue_received_packet(struct ipw_hardware *hw,
David Sterbaff3e9902008-07-28 16:53:11 +0200618 unsigned int protocol,
619 unsigned int address,
620 const unsigned char *data, int length,
621 int is_last)
David Sterba099dc4f2008-02-07 10:57:12 +0100622{
623 unsigned int channel_idx = address - 1;
624 struct ipw_rx_packet *packet = NULL;
625 unsigned long flags;
626
627 /* Discard packet if channel index is out of range. */
628 if (channel_idx >= NL_NUM_OF_ADDRESSES) {
629 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
630 ": data packet has bad address %u\n", address);
631 return;
632 }
633
634 /*
635 * ->packet_assembler is safe to touch unlocked, this is the only place
636 */
637 if (protocol == TL_PROTOCOLID_COM_DATA) {
638 struct ipw_rx_packet **assem =
639 &hw->packet_assembler[channel_idx];
640
641 /*
642 * Create a new packet, or assembler already contains one
643 * enlarge it by 'length' bytes.
644 */
645 (*assem) = pool_allocate(hw, *assem, length);
646 if (!(*assem)) {
647 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
648 ": no memory for incomming data packet, dropped!\n");
649 return;
650 }
651 (*assem)->protocol = protocol;
652 (*assem)->channel_idx = channel_idx;
653
654 /* Append this packet data onto existing data. */
655 memcpy((unsigned char *)(*assem) +
656 sizeof(struct ipw_rx_packet)
657 + (*assem)->length, data, length);
658 (*assem)->length += length;
659 if (is_last) {
660 packet = *assem;
661 *assem = NULL;
662 /* Count queued DATA bytes only */
David Sterba63c4dbd2008-07-28 16:52:44 +0200663 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100664 hw->rx_bytes_queued += packet->length;
David Sterba63c4dbd2008-07-28 16:52:44 +0200665 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100666 }
667 } else {
668 /* If it's a CTRL packet, don't assemble, just queue it. */
669 packet = pool_allocate(hw, NULL, length);
670 if (!packet) {
671 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
672 ": no memory for incomming ctrl packet, dropped!\n");
673 return;
674 }
675 packet->protocol = protocol;
676 packet->channel_idx = channel_idx;
677 memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
678 data, length);
679 packet->length = length;
680 }
681
682 /*
683 * If this is the last packet, then send the assembled packet on to the
684 * network layer.
685 */
686 if (packet) {
David Sterba63c4dbd2008-07-28 16:52:44 +0200687 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100688 list_add_tail(&packet->queue, &hw->rx_queue);
689 /* Block reception of incoming packets if queue is full. */
690 hw->blocking_rx =
David Sterbad4c0deb2008-07-28 16:52:33 +0200691 (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
David Sterba099dc4f2008-02-07 10:57:12 +0100692
David Sterba63c4dbd2008-07-28 16:52:44 +0200693 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100694 schedule_work(&hw->work_rx);
695 }
696}
697
698/*
699 * Workqueue callback
700 */
701static void ipw_receive_data_work(struct work_struct *work_rx)
702{
703 struct ipw_hardware *hw =
704 container_of(work_rx, struct ipw_hardware, work_rx);
705 unsigned long flags;
706
David Sterba63c4dbd2008-07-28 16:52:44 +0200707 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100708 while (!list_empty(&hw->rx_queue)) {
709 struct ipw_rx_packet *packet =
710 list_first_entry(&hw->rx_queue,
711 struct ipw_rx_packet, queue);
712
713 if (hw->shutting_down)
714 break;
715 list_del(&packet->queue);
716
717 /*
718 * Note: ipwireless_network_packet_received must be called in a
719 * process context (i.e. via schedule_work) because the tty
720 * output code can sleep in the tty_flip_buffer_push call.
721 */
722 if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
723 if (hw->network != NULL) {
724 /* If the network hasn't been disconnected. */
David Sterba63c4dbd2008-07-28 16:52:44 +0200725 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100726 /*
727 * This must run unlocked due to tty processing
728 * and mutex locking
729 */
730 ipwireless_network_packet_received(
731 hw->network,
732 packet->channel_idx,
733 (unsigned char *)packet
734 + sizeof(struct ipw_rx_packet),
735 packet->length);
David Sterba63c4dbd2008-07-28 16:52:44 +0200736 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100737 }
738 /* Count queued DATA bytes only */
739 hw->rx_bytes_queued -= packet->length;
740 } else {
741 /*
742 * This is safe to be called locked, callchain does
743 * not block
744 */
745 handle_received_CTRL_packet(hw, packet->channel_idx,
746 (unsigned char *)packet
747 + sizeof(struct ipw_rx_packet),
748 packet->length);
749 }
750 pool_free(hw, packet);
751 /*
752 * Unblock reception of incoming packets if queue is no longer
753 * full.
754 */
755 hw->blocking_rx =
756 hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
757 if (hw->shutting_down)
758 break;
759 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200760 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100761}
762
763static void handle_received_CTRL_packet(struct ipw_hardware *hw,
764 unsigned int channel_idx,
David Sterbaff3e9902008-07-28 16:53:11 +0200765 const unsigned char *data, int len)
David Sterba099dc4f2008-02-07 10:57:12 +0100766{
David Sterbaff3e9902008-07-28 16:53:11 +0200767 const struct ipw_control_packet_body *body =
768 (const struct ipw_control_packet_body *) data;
David Sterba099dc4f2008-02-07 10:57:12 +0100769 unsigned int changed_mask;
770
771 if (len != sizeof(struct ipw_control_packet_body)) {
772 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
773 ": control packet was %d bytes - wrong size!\n",
774 len);
775 return;
776 }
777
778 switch (body->sig_no) {
779 case COMCTRL_CTS:
780 changed_mask = IPW_CONTROL_LINE_CTS;
781 break;
782 case COMCTRL_DCD:
783 changed_mask = IPW_CONTROL_LINE_DCD;
784 break;
785 case COMCTRL_DSR:
786 changed_mask = IPW_CONTROL_LINE_DSR;
787 break;
788 case COMCTRL_RI:
789 changed_mask = IPW_CONTROL_LINE_RI;
790 break;
791 default:
792 changed_mask = 0;
793 }
794
795 if (changed_mask != 0) {
796 if (body->value)
797 hw->control_lines[channel_idx] |= changed_mask;
798 else
799 hw->control_lines[channel_idx] &= ~changed_mask;
800 if (hw->network)
801 ipwireless_network_notify_control_line_change(
802 hw->network,
803 channel_idx,
804 hw->control_lines[channel_idx],
805 changed_mask);
806 }
807}
808
809static void handle_received_packet(struct ipw_hardware *hw,
David Sterbaff3e9902008-07-28 16:53:11 +0200810 const union nl_packet *packet,
David Sterba099dc4f2008-02-07 10:57:12 +0100811 unsigned short len)
812{
813 unsigned int protocol = packet->hdr.protocol;
814 unsigned int address = packet->hdr.address;
815 unsigned int header_length;
David Sterbaff3e9902008-07-28 16:53:11 +0200816 const unsigned char *data;
David Sterba099dc4f2008-02-07 10:57:12 +0100817 unsigned int data_len;
818 int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
819
820 if (packet->hdr.packet_rank & NL_FIRST_PACKET)
821 header_length = NL_FIRST_PACKET_HEADER_SIZE;
822 else
823 header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
824
825 data = packet->rawpkt + header_length;
826 data_len = len - header_length;
827 switch (protocol) {
828 case TL_PROTOCOLID_COM_DATA:
829 case TL_PROTOCOLID_COM_CTRL:
830 queue_received_packet(hw, protocol, address, data, data_len,
831 is_last);
832 break;
833 case TL_PROTOCOLID_SETUP:
834 handle_received_SETUP_packet(hw, address, data, data_len,
835 is_last);
836 break;
837 }
838}
839
840static void acknowledge_data_read(struct ipw_hardware *hw)
841{
842 if (hw->hw_version == HW_VERSION_1)
843 outw(DCR_RXDONE, hw->base_port + IODCR);
844 else
845 writew(MEMRX_PCINTACKK,
846 &hw->memory_info_regs->memreg_pc_interrupt_ack);
847}
848
849/*
850 * Retrieve a packet from the IPW hardware.
851 */
852static void do_receive_packet(struct ipw_hardware *hw)
853{
854 unsigned len;
David Sterbad4c0deb2008-07-28 16:52:33 +0200855 unsigned i;
David Sterba099dc4f2008-02-07 10:57:12 +0100856 unsigned char pkt[LL_MTU_MAX];
857
858 start_timing();
859
860 if (hw->hw_version == HW_VERSION_1) {
861 len = inw(hw->base_port + IODRR);
862 if (len > hw->ll_mtu) {
863 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
David Sterba622e7132008-07-28 16:52:55 +0200864 ": received a packet of %u bytes - longer than the MTU!\n", len);
David Sterba099dc4f2008-02-07 10:57:12 +0100865 outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
866 return;
867 }
868
869 for (i = 0; i < len; i += 2) {
870 __le16 raw_data = inw(hw->base_port + IODRR);
871 unsigned short data = le16_to_cpu(raw_data);
872
873 pkt[i] = (unsigned char) data;
874 pkt[i + 1] = (unsigned char) (data >> 8);
875 }
876 } else {
David Sterba2e713162008-07-28 16:52:39 +0200877 len = inw(hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100878 if (len > hw->ll_mtu) {
879 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
David Sterba622e7132008-07-28 16:52:55 +0200880 ": received a packet of %u bytes - longer than the MTU!\n", len);
David Sterba099dc4f2008-02-07 10:57:12 +0100881 writew(MEMRX_PCINTACKK,
882 &hw->memory_info_regs->memreg_pc_interrupt_ack);
883 return;
884 }
885
886 for (i = 0; i < len; i += 2) {
David Sterba2e713162008-07-28 16:52:39 +0200887 __le16 raw_data = inw(hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100888 unsigned short data = le16_to_cpu(raw_data);
889
890 pkt[i] = (unsigned char) data;
891 pkt[i + 1] = (unsigned char) (data >> 8);
892 }
893
894 while ((i & 3) != 2) {
David Sterba2e713162008-07-28 16:52:39 +0200895 inw(hw->base_port);
David Sterba099dc4f2008-02-07 10:57:12 +0100896 i += 2;
897 }
898 }
899
900 acknowledge_data_read(hw);
901
David Sterbad54c2752008-07-28 16:53:00 +0200902 swap_packet_bitfield_from_le(pkt);
903
David Sterba099dc4f2008-02-07 10:57:12 +0100904 if (ipwireless_debug)
905 dump_data_bytes("recv", pkt, len);
906
907 handle_received_packet(hw, (union nl_packet *) pkt, len);
908
909 end_read_timing(len);
910}
911
912static int get_current_packet_priority(struct ipw_hardware *hw)
913{
914 /*
915 * If we're initializing, don't send anything of higher priority than
916 * PRIO_SETUP. The network layer therefore need not care about
917 * hardware initialization - any of its stuff will simply be queued
918 * until setup is complete.
919 */
920 return (hw->to_setup || hw->initializing
David Sterbad4c0deb2008-07-28 16:52:33 +0200921 ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
David Sterba099dc4f2008-02-07 10:57:12 +0100922}
923
924/*
925 * return 1 if something has been received from hw
926 */
927static int get_packets_from_hw(struct ipw_hardware *hw)
928{
929 int received = 0;
930 unsigned long flags;
931
David Sterba63c4dbd2008-07-28 16:52:44 +0200932 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100933 while (hw->rx_ready && !hw->blocking_rx) {
934 received = 1;
935 hw->rx_ready--;
David Sterba63c4dbd2008-07-28 16:52:44 +0200936 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100937
938 do_receive_packet(hw);
939
David Sterba63c4dbd2008-07-28 16:52:44 +0200940 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100941 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200942 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100943
944 return received;
945}
946
947/*
948 * Send pending packet up to given priority, prioritize SETUP data until
949 * hardware is fully setup.
950 *
951 * return 1 if more packets can be sent
952 */
953static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
954{
955 int more_to_send = 0;
956 unsigned long flags;
957
David Sterba63c4dbd2008-07-28 16:52:44 +0200958 spin_lock_irqsave(&hw->lock, flags);
David Sterbaeb4e5452008-06-06 10:56:35 +0200959 if (hw->tx_queued && hw->tx_ready) {
David Sterba099dc4f2008-02-07 10:57:12 +0100960 int priority;
961 struct ipw_tx_packet *packet = NULL;
962
David Sterba099dc4f2008-02-07 10:57:12 +0100963 /* Pick a packet */
964 for (priority = 0; priority < priority_limit; priority++) {
965 if (!list_empty(&hw->tx_queue[priority])) {
966 packet = list_first_entry(
967 &hw->tx_queue[priority],
968 struct ipw_tx_packet,
969 queue);
970
David Sterbaeb4e5452008-06-06 10:56:35 +0200971 hw->tx_queued--;
David Sterba099dc4f2008-02-07 10:57:12 +0100972 list_del(&packet->queue);
973
974 break;
975 }
976 }
977 if (!packet) {
978 hw->tx_queued = 0;
David Sterba63c4dbd2008-07-28 16:52:44 +0200979 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100980 return 0;
981 }
David Sterbaeb4e5452008-06-06 10:56:35 +0200982
David Sterba63c4dbd2008-07-28 16:52:44 +0200983 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100984
985 /* Send */
986 do_send_packet(hw, packet);
987
988 /* Check if more to send */
David Sterba63c4dbd2008-07-28 16:52:44 +0200989 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100990 for (priority = 0; priority < priority_limit; priority++)
991 if (!list_empty(&hw->tx_queue[priority])) {
992 more_to_send = 1;
993 break;
994 }
995
996 if (!more_to_send)
997 hw->tx_queued = 0;
998 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200999 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001000
1001 return more_to_send;
1002}
1003
1004/*
1005 * Send and receive all queued packets.
1006 */
1007static void ipwireless_do_tasklet(unsigned long hw_)
1008{
1009 struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1010 unsigned long flags;
1011
David Sterba63c4dbd2008-07-28 16:52:44 +02001012 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001013 if (hw->shutting_down) {
David Sterba63c4dbd2008-07-28 16:52:44 +02001014 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001015 return;
1016 }
1017
1018 if (hw->to_setup == 1) {
1019 /*
1020 * Initial setup data sent to hardware
1021 */
1022 hw->to_setup = 2;
David Sterba63c4dbd2008-07-28 16:52:44 +02001023 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001024
1025 ipw_setup_hardware(hw);
1026 ipw_send_setup_packet(hw);
1027
1028 send_pending_packet(hw, PRIO_SETUP + 1);
1029 get_packets_from_hw(hw);
1030 } else {
1031 int priority_limit = get_current_packet_priority(hw);
1032 int again;
1033
David Sterba63c4dbd2008-07-28 16:52:44 +02001034 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001035
1036 do {
1037 again = send_pending_packet(hw, priority_limit);
1038 again |= get_packets_from_hw(hw);
1039 } while (again);
1040 }
1041}
1042
1043/*
1044 * return true if the card is physically present.
1045 */
1046static int is_card_present(struct ipw_hardware *hw)
1047{
1048 if (hw->hw_version == HW_VERSION_1)
1049 return inw(hw->base_port + IOIR) != 0xFFFF;
1050 else
1051 return readl(&hw->memory_info_regs->memreg_card_present) ==
1052 CARD_PRESENT_VALUE;
1053}
1054
1055static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1056 struct ipw_hardware *hw)
1057{
1058 unsigned short irqn;
1059
1060 irqn = inw(hw->base_port + IOIR);
1061
1062 /* Check if card is present */
1063 if (irqn == 0xFFFF)
1064 return IRQ_NONE;
1065 else if (irqn != 0) {
1066 unsigned short ack = 0;
1067 unsigned long flags;
1068
1069 /* Transmit complete. */
1070 if (irqn & IR_TXINTR) {
1071 ack |= IR_TXINTR;
David Sterba63c4dbd2008-07-28 16:52:44 +02001072 spin_lock_irqsave(&hw->lock, flags);
David Sterbaeb4e5452008-06-06 10:56:35 +02001073 hw->tx_ready = 1;
David Sterba63c4dbd2008-07-28 16:52:44 +02001074 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001075 }
1076 /* Received data */
1077 if (irqn & IR_RXINTR) {
1078 ack |= IR_RXINTR;
David Sterba63c4dbd2008-07-28 16:52:44 +02001079 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001080 hw->rx_ready++;
David Sterba63c4dbd2008-07-28 16:52:44 +02001081 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001082 }
1083 if (ack != 0) {
1084 outw(ack, hw->base_port + IOIR);
1085 tasklet_schedule(&hw->tasklet);
1086 }
1087 return IRQ_HANDLED;
1088 }
1089 return IRQ_NONE;
1090}
1091
1092static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1093{
1094 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1095
1096 csr &= 0xfffd;
1097 writew(csr, &hw->memregs_CCR->reg_config_and_status);
1098}
1099
1100static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1101 struct ipw_hardware *hw)
1102{
1103 int tx = 0;
1104 int rx = 0;
1105 int rx_repeat = 0;
1106 int try_mem_tx_old;
1107 unsigned long flags;
1108
1109 do {
1110
1111 unsigned short memtx = readw(hw->memreg_tx);
1112 unsigned short memtx_serial;
1113 unsigned short memrxdone =
1114 readw(&hw->memory_info_regs->memreg_rx_done);
1115
1116 try_mem_tx_old = 0;
1117
1118 /* check whether the interrupt was generated by ipwireless card */
1119 if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1120
1121 /* check if the card uses memreg_tx_old register */
1122 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1123 memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1124 if (memtx & MEMTX_TX) {
1125 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1126 ": Using memreg_tx_old\n");
1127 hw->memreg_tx =
1128 &hw->memory_info_regs->memreg_tx_old;
1129 } else {
1130 return IRQ_NONE;
1131 }
David Sterbad4c0deb2008-07-28 16:52:33 +02001132 } else
David Sterba099dc4f2008-02-07 10:57:12 +01001133 return IRQ_NONE;
David Sterba099dc4f2008-02-07 10:57:12 +01001134 }
1135
1136 /*
1137 * See if the card is physically present. Note that while it is
1138 * powering up, it appears not to be present.
1139 */
1140 if (!is_card_present(hw)) {
1141 acknowledge_pcmcia_interrupt(hw);
1142 return IRQ_HANDLED;
1143 }
1144
1145 memtx_serial = memtx & (unsigned short) 0xff00;
1146 if (memtx & MEMTX_TX) {
1147 writew(memtx_serial, hw->memreg_tx);
1148
1149 if (hw->serial_number_detected) {
1150 if (memtx_serial != hw->last_memtx_serial) {
1151 hw->last_memtx_serial = memtx_serial;
David Sterba63c4dbd2008-07-28 16:52:44 +02001152 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001153 hw->rx_ready++;
David Sterba63c4dbd2008-07-28 16:52:44 +02001154 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001155 rx = 1;
1156 } else
1157 /* Ignore 'Timer Recovery' duplicates. */
1158 rx_repeat = 1;
1159 } else {
1160 /*
1161 * If a non-zero serial number is seen, then enable
1162 * serial number checking.
1163 */
1164 if (memtx_serial != 0) {
1165 hw->serial_number_detected = 1;
1166 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1167 ": memreg_tx serial num detected\n");
1168
David Sterba63c4dbd2008-07-28 16:52:44 +02001169 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001170 hw->rx_ready++;
David Sterba63c4dbd2008-07-28 16:52:44 +02001171 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001172 }
1173 rx = 1;
1174 }
1175 }
1176 if (memrxdone & MEMRX_RX_DONE) {
1177 writew(0, &hw->memory_info_regs->memreg_rx_done);
David Sterba63c4dbd2008-07-28 16:52:44 +02001178 spin_lock_irqsave(&hw->lock, flags);
David Sterbaeb4e5452008-06-06 10:56:35 +02001179 hw->tx_ready = 1;
David Sterba63c4dbd2008-07-28 16:52:44 +02001180 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001181 tx = 1;
1182 }
1183 if (tx)
1184 writew(MEMRX_PCINTACKK,
1185 &hw->memory_info_regs->memreg_pc_interrupt_ack);
1186
1187 acknowledge_pcmcia_interrupt(hw);
1188
1189 if (tx || rx)
1190 tasklet_schedule(&hw->tasklet);
1191 else if (!rx_repeat) {
1192 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1193 if (hw->serial_number_detected)
1194 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1195 ": spurious interrupt - new_tx mode\n");
1196 else {
1197 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
David Sterba622e7132008-07-28 16:52:55 +02001198 ": no valid memreg_tx value - switching to the old memreg_tx\n");
David Sterba099dc4f2008-02-07 10:57:12 +01001199 hw->memreg_tx =
1200 &hw->memory_info_regs->memreg_tx_old;
1201 try_mem_tx_old = 1;
1202 }
1203 } else
1204 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1205 ": spurious interrupt - old_tx mode\n");
1206 }
1207
1208 } while (try_mem_tx_old == 1);
1209
1210 return IRQ_HANDLED;
1211}
1212
David Sterba2fc55772008-07-28 16:52:49 +02001213irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
David Sterba099dc4f2008-02-07 10:57:12 +01001214{
1215 struct ipw_hardware *hw = dev_id;
1216
1217 if (hw->hw_version == HW_VERSION_1)
1218 return ipwireless_handle_v1_interrupt(irq, hw);
1219 else
1220 return ipwireless_handle_v2_v3_interrupt(irq, hw);
1221}
1222
1223static void flush_packets_to_hw(struct ipw_hardware *hw)
1224{
1225 int priority_limit;
1226 unsigned long flags;
1227
David Sterba63c4dbd2008-07-28 16:52:44 +02001228 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001229 priority_limit = get_current_packet_priority(hw);
David Sterba63c4dbd2008-07-28 16:52:44 +02001230 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001231
1232 while (send_pending_packet(hw, priority_limit));
1233}
1234
1235static void send_packet(struct ipw_hardware *hw, int priority,
1236 struct ipw_tx_packet *packet)
1237{
1238 unsigned long flags;
1239
David Sterba63c4dbd2008-07-28 16:52:44 +02001240 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001241 list_add_tail(&packet->queue, &hw->tx_queue[priority]);
David Sterbaeb4e5452008-06-06 10:56:35 +02001242 hw->tx_queued++;
David Sterba63c4dbd2008-07-28 16:52:44 +02001243 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001244
1245 flush_packets_to_hw(hw);
1246}
1247
1248/* Create data packet, non-atomic allocation */
1249static void *alloc_data_packet(int data_size,
1250 unsigned char dest_addr,
1251 unsigned char protocol)
1252{
1253 struct ipw_tx_packet *packet = kzalloc(
1254 sizeof(struct ipw_tx_packet) + data_size,
1255 GFP_ATOMIC);
1256
1257 if (!packet)
1258 return NULL;
1259
1260 INIT_LIST_HEAD(&packet->queue);
1261 packet->dest_addr = dest_addr;
1262 packet->protocol = protocol;
1263 packet->length = data_size;
1264
1265 return packet;
1266}
1267
1268static void *alloc_ctrl_packet(int header_size,
1269 unsigned char dest_addr,
1270 unsigned char protocol,
1271 unsigned char sig_no)
1272{
1273 /*
1274 * sig_no is located right after ipw_tx_packet struct in every
1275 * CTRL or SETUP packets, we can use ipw_control_packet as a
1276 * common struct
1277 */
1278 struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1279
1280 if (!packet)
1281 return NULL;
1282
1283 INIT_LIST_HEAD(&packet->header.queue);
1284 packet->header.dest_addr = dest_addr;
1285 packet->header.protocol = protocol;
1286 packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1287 packet->body.sig_no = sig_no;
1288
1289 return packet;
1290}
1291
1292int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
David Sterbaff3e9902008-07-28 16:53:11 +02001293 const unsigned char *data, unsigned int length,
David Sterba099dc4f2008-02-07 10:57:12 +01001294 void (*callback) (void *cb, unsigned int length),
1295 void *callback_data)
1296{
1297 struct ipw_tx_packet *packet;
1298
David Sterbad4c0deb2008-07-28 16:52:33 +02001299 packet = alloc_data_packet(length, (channel_idx + 1),
1300 TL_PROTOCOLID_COM_DATA);
David Sterba099dc4f2008-02-07 10:57:12 +01001301 if (!packet)
1302 return -ENOMEM;
1303 packet->packet_callback = callback;
1304 packet->callback_data = callback_data;
David Sterbad4c0deb2008-07-28 16:52:33 +02001305 memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1306 length);
David Sterba099dc4f2008-02-07 10:57:12 +01001307
1308 send_packet(hw, PRIO_DATA, packet);
1309 return 0;
1310}
1311
1312static int set_control_line(struct ipw_hardware *hw, int prio,
1313 unsigned int channel_idx, int line, int state)
1314{
1315 struct ipw_control_packet *packet;
1316 int protocolid = TL_PROTOCOLID_COM_CTRL;
1317
1318 if (prio == PRIO_SETUP)
1319 protocolid = TL_PROTOCOLID_SETUP;
1320
1321 packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
David Sterbad4c0deb2008-07-28 16:52:33 +02001322 (channel_idx + 1), protocolid, line);
David Sterba099dc4f2008-02-07 10:57:12 +01001323 if (!packet)
1324 return -ENOMEM;
1325 packet->header.length = sizeof(struct ipw_control_packet_body);
David Sterbad4c0deb2008-07-28 16:52:33 +02001326 packet->body.value = (state == 0 ? 0 : 1);
David Sterba099dc4f2008-02-07 10:57:12 +01001327 send_packet(hw, prio, &packet->header);
1328 return 0;
1329}
1330
1331
1332static int set_DTR(struct ipw_hardware *hw, int priority,
1333 unsigned int channel_idx, int state)
1334{
1335 if (state != 0)
1336 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1337 else
1338 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1339
1340 return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1341}
1342
1343static int set_RTS(struct ipw_hardware *hw, int priority,
1344 unsigned int channel_idx, int state)
1345{
1346 if (state != 0)
1347 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1348 else
1349 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1350
1351 return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1352}
1353
1354int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1355 int state)
1356{
1357 return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1358}
1359
1360int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1361 int state)
1362{
1363 return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1364}
1365
1366struct ipw_setup_get_version_query_packet {
1367 struct ipw_tx_packet header;
1368 struct tl_setup_get_version_qry body;
1369};
1370
1371struct ipw_setup_config_packet {
1372 struct ipw_tx_packet header;
1373 struct tl_setup_config_msg body;
1374};
1375
1376struct ipw_setup_config_done_packet {
1377 struct ipw_tx_packet header;
1378 struct tl_setup_config_done_msg body;
1379};
1380
1381struct ipw_setup_open_packet {
1382 struct ipw_tx_packet header;
1383 struct tl_setup_open_msg body;
1384};
1385
1386struct ipw_setup_info_packet {
1387 struct ipw_tx_packet header;
1388 struct tl_setup_info_msg body;
1389};
1390
1391struct ipw_setup_reboot_msg_ack {
1392 struct ipw_tx_packet header;
1393 struct TlSetupRebootMsgAck body;
1394};
1395
1396/* This handles the actual initialization of the card */
1397static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1398{
1399 struct ipw_setup_config_packet *config_packet;
1400 struct ipw_setup_config_done_packet *config_done_packet;
1401 struct ipw_setup_open_packet *open_packet;
1402 struct ipw_setup_info_packet *info_packet;
1403 int port;
1404 unsigned int channel_idx;
1405
1406 /* generate config packet */
1407 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1408 config_packet = alloc_ctrl_packet(
1409 sizeof(struct ipw_setup_config_packet),
1410 ADDR_SETUP_PROT,
1411 TL_PROTOCOLID_SETUP,
1412 TL_SETUP_SIGNO_CONFIG_MSG);
1413 if (!config_packet)
1414 goto exit_nomem;
1415 config_packet->header.length = sizeof(struct tl_setup_config_msg);
1416 config_packet->body.port_no = port;
1417 config_packet->body.prio_data = PRIO_DATA;
1418 config_packet->body.prio_ctrl = PRIO_CTRL;
1419 send_packet(hw, PRIO_SETUP, &config_packet->header);
1420 }
1421 config_done_packet = alloc_ctrl_packet(
1422 sizeof(struct ipw_setup_config_done_packet),
1423 ADDR_SETUP_PROT,
1424 TL_PROTOCOLID_SETUP,
1425 TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1426 if (!config_done_packet)
1427 goto exit_nomem;
1428 config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1429 send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1430
1431 /* generate open packet */
1432 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1433 open_packet = alloc_ctrl_packet(
1434 sizeof(struct ipw_setup_open_packet),
1435 ADDR_SETUP_PROT,
1436 TL_PROTOCOLID_SETUP,
1437 TL_SETUP_SIGNO_OPEN_MSG);
1438 if (!open_packet)
1439 goto exit_nomem;
1440 open_packet->header.length = sizeof(struct tl_setup_open_msg);
1441 open_packet->body.port_no = port;
1442 send_packet(hw, PRIO_SETUP, &open_packet->header);
1443 }
1444 for (channel_idx = 0;
1445 channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1446 int ret;
1447
1448 ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1449 (hw->control_lines[channel_idx] &
1450 IPW_CONTROL_LINE_DTR) != 0);
1451 if (ret) {
1452 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1453 ": error setting DTR (%d)\n", ret);
1454 return;
1455 }
1456
1457 set_RTS(hw, PRIO_SETUP, channel_idx,
1458 (hw->control_lines [channel_idx] &
1459 IPW_CONTROL_LINE_RTS) != 0);
1460 if (ret) {
1461 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1462 ": error setting RTS (%d)\n", ret);
1463 return;
1464 }
1465 }
1466 /*
1467 * For NDIS we assume that we are using sync PPP frames, for COM async.
1468 * This driver uses NDIS mode too. We don't bother with translation
1469 * from async -> sync PPP.
1470 */
1471 info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1472 ADDR_SETUP_PROT,
1473 TL_PROTOCOLID_SETUP,
1474 TL_SETUP_SIGNO_INFO_MSG);
1475 if (!info_packet)
1476 goto exit_nomem;
1477 info_packet->header.length = sizeof(struct tl_setup_info_msg);
1478 info_packet->body.driver_type = NDISWAN_DRIVER;
1479 info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1480 info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1481 send_packet(hw, PRIO_SETUP, &info_packet->header);
1482
1483 /* Initialization is now complete, so we clear the 'to_setup' flag */
1484 hw->to_setup = 0;
1485
1486 return;
1487
1488exit_nomem:
1489 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1490 ": not enough memory to alloc control packet\n");
1491 hw->to_setup = -1;
1492}
1493
1494static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1495 unsigned char vers_no)
1496{
1497 del_timer(&hw->setup_timer);
1498 hw->initializing = 0;
1499 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1500
1501 if (vers_no == TL_SETUP_VERSION)
1502 __handle_setup_get_version_rsp(hw);
1503 else
David Sterba622e7132008-07-28 16:52:55 +02001504 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
David Sterba099dc4f2008-02-07 10:57:12 +01001505 ": invalid hardware version no %u\n",
1506 (unsigned int) vers_no);
1507}
1508
1509static void ipw_send_setup_packet(struct ipw_hardware *hw)
1510{
1511 struct ipw_setup_get_version_query_packet *ver_packet;
1512
1513 ver_packet = alloc_ctrl_packet(
1514 sizeof(struct ipw_setup_get_version_query_packet),
1515 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1516 TL_SETUP_SIGNO_GET_VERSION_QRY);
1517 ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1518
1519 /*
1520 * Response is handled in handle_received_SETUP_packet
1521 */
1522 send_packet(hw, PRIO_SETUP, &ver_packet->header);
1523}
1524
1525static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1526 unsigned int address,
David Sterbaff3e9902008-07-28 16:53:11 +02001527 const unsigned char *data, int len,
David Sterba099dc4f2008-02-07 10:57:12 +01001528 int is_last)
1529{
David Sterbaff3e9902008-07-28 16:53:11 +02001530 const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
David Sterba099dc4f2008-02-07 10:57:12 +01001531
1532 if (address != ADDR_SETUP_PROT) {
1533 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1534 ": setup packet has bad address %d\n", address);
1535 return;
1536 }
1537
1538 switch (rx_msg->sig_no) {
1539 case TL_SETUP_SIGNO_GET_VERSION_RSP:
1540 if (hw->to_setup)
1541 handle_setup_get_version_rsp(hw,
1542 rx_msg->version_rsp_msg.version);
1543 break;
1544
1545 case TL_SETUP_SIGNO_OPEN_MSG:
1546 if (ipwireless_debug) {
1547 unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1548
1549 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1550 ": OPEN_MSG [channel %u] reply received\n",
1551 channel_idx);
1552 }
1553 break;
1554
1555 case TL_SETUP_SIGNO_INFO_MSG_ACK:
1556 if (ipwireless_debug)
1557 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1558 ": card successfully configured as NDISWAN\n");
1559 break;
1560
1561 case TL_SETUP_SIGNO_REBOOT_MSG:
1562 if (hw->to_setup)
1563 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1564 ": Setup not completed - ignoring reboot msg\n");
1565 else {
1566 struct ipw_setup_reboot_msg_ack *packet;
1567
1568 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1569 ": Acknowledging REBOOT message\n");
1570 packet = alloc_ctrl_packet(
1571 sizeof(struct ipw_setup_reboot_msg_ack),
1572 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1573 TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1574 packet->header.length =
1575 sizeof(struct TlSetupRebootMsgAck);
1576 send_packet(hw, PRIO_SETUP, &packet->header);
1577 if (hw->reboot_callback)
1578 hw->reboot_callback(hw->reboot_callback_data);
1579 }
1580 break;
1581
1582 default:
1583 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1584 ": unknown setup message %u received\n",
1585 (unsigned int) rx_msg->sig_no);
1586 }
1587}
1588
1589static void do_close_hardware(struct ipw_hardware *hw)
1590{
1591 unsigned int irqn;
1592
1593 if (hw->hw_version == HW_VERSION_1) {
1594 /* Disable TX and RX interrupts. */
1595 outw(0, hw->base_port + IOIER);
1596
1597 /* Acknowledge any outstanding interrupt requests */
1598 irqn = inw(hw->base_port + IOIR);
1599 if (irqn & IR_TXINTR)
1600 outw(IR_TXINTR, hw->base_port + IOIR);
1601 if (irqn & IR_RXINTR)
1602 outw(IR_RXINTR, hw->base_port + IOIR);
1603
1604 synchronize_irq(hw->irq);
1605 }
1606}
1607
1608struct ipw_hardware *ipwireless_hardware_create(void)
1609{
1610 int i;
1611 struct ipw_hardware *hw =
1612 kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1613
1614 if (!hw)
1615 return NULL;
1616
1617 hw->irq = -1;
1618 hw->initializing = 1;
1619 hw->tx_ready = 1;
1620 hw->rx_bytes_queued = 0;
1621 hw->rx_pool_size = 0;
1622 hw->last_memtx_serial = (unsigned short) 0xffff;
1623 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1624 INIT_LIST_HEAD(&hw->tx_queue[i]);
1625
1626 INIT_LIST_HEAD(&hw->rx_queue);
1627 INIT_LIST_HEAD(&hw->rx_pool);
David Sterba63c4dbd2008-07-28 16:52:44 +02001628 spin_lock_init(&hw->lock);
David Sterba099dc4f2008-02-07 10:57:12 +01001629 tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1630 INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1631 setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1632 (unsigned long) hw);
1633
1634 return hw;
1635}
1636
1637void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1638 unsigned int base_port,
1639 void __iomem *attr_memory,
1640 void __iomem *common_memory,
1641 int is_v2_card,
1642 void (*reboot_callback) (void *data),
1643 void *reboot_callback_data)
1644{
1645 if (hw->removed) {
1646 hw->removed = 0;
1647 enable_irq(hw->irq);
1648 }
1649 hw->base_port = base_port;
David Sterbad4c0deb2008-07-28 16:52:33 +02001650 hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1651 hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
David Sterba099dc4f2008-02-07 10:57:12 +01001652 hw->memregs_CCR = (struct MEMCCR __iomem *)
1653 ((unsigned short __iomem *) attr_memory + 0x200);
1654 hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1655 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1656 hw->reboot_callback = reboot_callback;
1657 hw->reboot_callback_data = reboot_callback_data;
1658}
1659
1660void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1661{
1662 hw->initializing = 1;
1663 hw->init_loops = 0;
1664 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1665 ": waiting for card to start up...\n");
1666 ipwireless_setup_timer((unsigned long) hw);
1667}
1668
1669static void ipwireless_setup_timer(unsigned long data)
1670{
1671 struct ipw_hardware *hw = (struct ipw_hardware *) data;
1672
1673 hw->init_loops++;
1674
1675 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1676 hw->hw_version == HW_VERSION_2 &&
1677 hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1678 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1679 ": failed to startup using TX2, trying TX\n");
1680
1681 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1682 hw->init_loops = 0;
1683 }
1684 /* Give up after a certain number of retries */
1685 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1686 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1687 ": card failed to start up!\n");
1688 hw->initializing = 0;
1689 } else {
1690 /* Do not attempt to write to the board if it is not present. */
1691 if (is_card_present(hw)) {
1692 unsigned long flags;
1693
David Sterba63c4dbd2008-07-28 16:52:44 +02001694 spin_lock_irqsave(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001695 hw->to_setup = 1;
1696 hw->tx_ready = 1;
David Sterba63c4dbd2008-07-28 16:52:44 +02001697 spin_unlock_irqrestore(&hw->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +01001698 tasklet_schedule(&hw->tasklet);
1699 }
1700
1701 mod_timer(&hw->setup_timer,
1702 jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1703 }
1704}
1705
1706/*
1707 * Stop any interrupts from executing so that, once this function returns,
1708 * other layers of the driver can be sure they won't get any more callbacks.
1709 * Thus must be called on a proper process context.
1710 */
1711void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1712{
1713 if (!hw->shutting_down) {
1714 /* Tell everyone we are going down. */
1715 hw->shutting_down = 1;
1716 del_timer(&hw->setup_timer);
1717
1718 /* Prevent the hardware from sending any more interrupts */
1719 do_close_hardware(hw);
1720 }
1721}
1722
1723void ipwireless_hardware_free(struct ipw_hardware *hw)
1724{
1725 int i;
1726 struct ipw_rx_packet *rp, *rq;
1727 struct ipw_tx_packet *tp, *tq;
1728
1729 ipwireless_stop_interrupts(hw);
1730
1731 flush_scheduled_work();
1732
1733 for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1734 if (hw->packet_assembler[i] != NULL)
1735 kfree(hw->packet_assembler[i]);
1736
1737 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1738 list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1739 list_del(&tp->queue);
1740 kfree(tp);
1741 }
1742
1743 list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1744 list_del(&rp->queue);
1745 kfree(rp);
1746 }
1747
1748 list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1749 list_del(&rp->queue);
1750 kfree(rp);
1751 }
1752 kfree(hw);
1753}
1754
1755/*
1756 * Associate the specified network with this hardware, so it will receive events
1757 * from it.
1758 */
1759void ipwireless_associate_network(struct ipw_hardware *hw,
1760 struct ipw_network *network)
1761{
1762 hw->network = network;
1763}