Initial commit
[wrt350n-kernel.git] / drivers / char / pcmcia / ipwireless / hardware.c
blob1f978ff87fa8a2ddeece885d0773de41f071558f
1 /*
2 * IPWireless 3G PCMCIA Network Driver
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
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>
25 #include "hardware.h"
26 #include "setup_protocol.h"
27 #include "network.h"
28 #include "main.h"
30 static void ipw_send_setup_packet(struct ipw_hardware *hw);
31 static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
32 unsigned int address,
33 unsigned char *data, int len,
34 int is_last);
35 static void ipwireless_setup_timer(unsigned long data);
36 static void handle_received_CTRL_packet(struct ipw_hardware *hw,
37 unsigned int channel_idx, unsigned char *data, int len);
39 /*#define TIMING_DIAGNOSTICS*/
41 #ifdef TIMING_DIAGNOSTICS
43 static 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;
52 static void start_timing(void)
54 timing_stats.start_time = jiffies;
57 static void end_read_timing(unsigned length)
59 timing_stats.read_time += (jiffies - start_time);
60 timing_stats.read_bytes += length + 2;
61 report_timing();
64 static void end_write_timing(unsigned length)
66 timing_stats.write_time += (jiffies - start_time);
67 timing_stats.write_bytes += length + 2;
68 report_timing();
71 static void report_timing(void)
73 unsigned long since = jiffies - timing_stats.last_report_time;
75 /* If it's been more than one second... */
76 if (since >= HZ) {
77 int first = (timing_stats.last_report_time == 0);
79 timing_stats.last_report_time = jiffies;
80 if (!first)
81 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
82 ": %u us elapsed - read %lu bytes in %u us, "
83 "wrote %lu bytes in %u us\n",
84 jiffies_to_usecs(since),
85 timing_stats.read_bytes,
86 jiffies_to_usecs(timing_stats.read_time),
87 timing_stats.write_bytes,
88 jiffies_to_usecs(timing_stats.write_time));
90 timing_stats.read_time = 0;
91 timing_stats.write_time = 0;
92 timing_stats.read_bytes = 0;
93 timing_stats.write_bytes = 0;
96 #else
97 static void start_timing(void) { }
98 static void end_read_timing(unsigned length) { }
99 static void end_write_timing(unsigned length) { }
100 #endif
102 /* Imported IPW definitions */
104 #define LL_MTU_V1 318
105 #define LL_MTU_V2 250
106 #define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
108 #define PRIO_DATA 2
109 #define PRIO_CTRL 1
110 #define PRIO_SETUP 0
112 /* Addresses */
113 #define ADDR_SETUP_PROT 0
115 /* Protocol ids */
116 enum {
117 /* Identifier for the Com Data protocol */
118 TL_PROTOCOLID_COM_DATA = 0,
120 /* Identifier for the Com Control protocol */
121 TL_PROTOCOLID_COM_CTRL = 1,
123 /* Identifier for the Setup protocol */
124 TL_PROTOCOLID_SETUP = 2
127 /* Number of bytes in NL packet header (cannot do
128 * sizeof(nl_packet_header) since it's a bitfield) */
129 #define NL_FIRST_PACKET_HEADER_SIZE 3
131 /* Number of bytes in NL packet header (cannot do
132 * sizeof(nl_packet_header) since it's a bitfield) */
133 #define NL_FOLLOWING_PACKET_HEADER_SIZE 1
135 struct nl_first_packet_header {
136 #if defined(__BIG_ENDIAN_BITFIELD)
137 unsigned char packet_rank:2;
138 unsigned char address:3;
139 unsigned char protocol:3;
140 #else
141 unsigned char protocol:3;
142 unsigned char address:3;
143 unsigned char packet_rank:2;
144 #endif
145 unsigned char length_lsb;
146 unsigned char length_msb;
149 struct nl_packet_header {
150 #if defined(__BIG_ENDIAN_BITFIELD)
151 unsigned char packet_rank:2;
152 unsigned char address:3;
153 unsigned char protocol:3;
154 #else
155 unsigned char protocol:3;
156 unsigned char address:3;
157 unsigned char packet_rank:2;
158 #endif
161 /* Value of 'packet_rank' above */
162 #define NL_INTERMEDIATE_PACKET 0x0
163 #define NL_LAST_PACKET 0x1
164 #define NL_FIRST_PACKET 0x2
166 union nl_packet {
167 /* Network packet header of the first packet (a special case) */
168 struct nl_first_packet_header hdr_first;
169 /* Network packet header of the following packets (if any) */
170 struct nl_packet_header hdr;
171 /* Complete network packet (header + data) */
172 unsigned char rawpkt[LL_MTU_MAX];
173 } __attribute__ ((__packed__));
175 #define HW_VERSION_UNKNOWN -1
176 #define HW_VERSION_1 1
177 #define HW_VERSION_2 2
179 /* IPW I/O ports */
180 #define IOIER 0x00 /* Interrupt Enable Register */
181 #define IOIR 0x02 /* Interrupt Source/ACK register */
182 #define IODCR 0x04 /* Data Control Register */
183 #define IODRR 0x06 /* Data Read Register */
184 #define IODWR 0x08 /* Data Write Register */
185 #define IOESR 0x0A /* Embedded Driver Status Register */
186 #define IORXR 0x0C /* Rx Fifo Register (Host to Embedded) */
187 #define IOTXR 0x0E /* Tx Fifo Register (Embedded to Host) */
189 /* I/O ports and bit definitions for version 1 of the hardware */
191 /* IER bits*/
192 #define IER_RXENABLED 0x1
193 #define IER_TXENABLED 0x2
195 /* ISR bits */
196 #define IR_RXINTR 0x1
197 #define IR_TXINTR 0x2
199 /* DCR bits */
200 #define DCR_RXDONE 0x1
201 #define DCR_TXDONE 0x2
202 #define DCR_RXRESET 0x4
203 #define DCR_TXRESET 0x8
205 /* I/O ports and bit definitions for version 2 of the hardware */
207 struct MEMCCR {
208 unsigned short reg_config_option; /* PCCOR: Configuration Option Register */
209 unsigned short reg_config_and_status; /* PCCSR: Configuration and Status Register */
210 unsigned short reg_pin_replacement; /* PCPRR: Pin Replacemant Register */
211 unsigned short reg_socket_and_copy; /* PCSCR: Socket and Copy Register */
212 unsigned short reg_ext_status; /* PCESR: Extendend Status Register */
213 unsigned short reg_io_base; /* PCIOB: I/O Base Register */
216 struct MEMINFREG {
217 unsigned short memreg_tx_old; /* TX Register (R/W) */
218 unsigned short pad1;
219 unsigned short memreg_rx_done; /* RXDone Register (R/W) */
220 unsigned short pad2;
221 unsigned short memreg_rx; /* RX Register (R/W) */
222 unsigned short pad3;
223 unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */
224 unsigned short pad4;
225 unsigned long memreg_card_present;/* Mask for Host to check (R) for
226 * CARD_PRESENT_VALUE */
227 unsigned short memreg_tx_new; /* TX2 (new) Register (R/W) */
230 #define IODMADPR 0x00 /* DMA Data Port Register (R/W) */
232 #define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
234 #define MEMTX_TX 0x0001
235 #define MEMRX_RX 0x0001
236 #define MEMRX_RX_DONE 0x0001
237 #define MEMRX_PCINTACKK 0x0001
238 #define MEMRX_MEMSPURIOUSINT 0x0001
240 #define NL_NUM_OF_PRIORITIES 3
241 #define NL_NUM_OF_PROTOCOLS 3
242 #define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS
244 struct ipw_hardware {
245 unsigned int base_port;
246 short hw_version;
247 unsigned short ll_mtu;
248 spinlock_t spinlock;
250 int initializing;
251 int init_loops;
252 struct timer_list setup_timer;
254 int tx_ready;
255 struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
256 /* True if any packets are queued for transmission */
257 int tx_queued;
259 int rx_bytes_queued;
260 struct list_head rx_queue;
261 /* Pool of rx_packet structures that are not currently used. */
262 struct list_head rx_pool;
263 int rx_pool_size;
264 /* True if reception of data is blocked while userspace processes it. */
265 int blocking_rx;
266 /* True if there is RX data ready on the hardware. */
267 int rx_ready;
268 unsigned short last_memtx_serial;
270 * Newer versions of the V2 card firmware send serial numbers in the
271 * MemTX register. 'serial_number_detected' is set true when we detect
272 * a non-zero serial number (indicating the new firmware). Thereafter,
273 * the driver can safely ignore the Timer Recovery re-sends to avoid
274 * out-of-sync problems.
276 int serial_number_detected;
277 struct work_struct work_rx;
279 /* True if we are to send the set-up data to the hardware. */
280 int to_setup;
282 /* Card has been removed */
283 int removed;
284 /* Saved irq value when we disable the interrupt. */
285 int irq;
286 /* True if this driver is shutting down. */
287 int shutting_down;
288 /* Modem control lines */
289 unsigned int control_lines[NL_NUM_OF_ADDRESSES];
290 struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
292 struct tasklet_struct tasklet;
294 /* The handle for the network layer, for the sending of events to it. */
295 struct ipw_network *network;
296 struct MEMINFREG __iomem *memory_info_regs;
297 struct MEMCCR __iomem *memregs_CCR;
298 void (*reboot_callback) (void *data);
299 void *reboot_callback_data;
301 unsigned short __iomem *memreg_tx;
305 * Packet info structure for tx packets.
306 * Note: not all the fields defined here are required for all protocols
308 struct ipw_tx_packet {
309 struct list_head queue;
310 /* channel idx + 1 */
311 unsigned char dest_addr;
312 /* SETUP, CTRL or DATA */
313 unsigned char protocol;
314 /* Length of data block, which starts at the end of this structure */
315 unsigned short length;
316 /* Sending state */
317 /* Offset of where we've sent up to so far */
318 unsigned long offset;
319 /* Count of packet fragments, starting at 0 */
320 int fragment_count;
322 /* Called after packet is sent and before is freed */
323 void (*packet_callback) (void *cb_data, unsigned int packet_length);
324 void *callback_data;
327 /* Signals from DTE */
328 #define COMCTRL_RTS 0
329 #define COMCTRL_DTR 1
331 /* Signals from DCE */
332 #define COMCTRL_CTS 2
333 #define COMCTRL_DCD 3
334 #define COMCTRL_DSR 4
335 #define COMCTRL_RI 5
337 struct ipw_control_packet_body {
338 /* DTE signal or DCE signal */
339 unsigned char sig_no;
340 /* 0: set signal, 1: clear signal */
341 unsigned char value;
342 } __attribute__ ((__packed__));
344 struct ipw_control_packet {
345 struct ipw_tx_packet header;
346 struct ipw_control_packet_body body;
349 struct ipw_rx_packet {
350 struct list_head queue;
351 unsigned int capacity;
352 unsigned int length;
353 unsigned int protocol;
354 unsigned int channel_idx;
357 #ifdef IPWIRELESS_STATE_DEBUG
358 int ipwireless_dump_hardware_state(char *p, size_t limit,
359 struct ipw_hardware *hw)
361 return snprintf(p, limit,
362 "debug: initializing=%d\n"
363 "debug: tx_ready=%d\n"
364 "debug: tx_queued=%d\n"
365 "debug: rx_ready=%d\n"
366 "debug: rx_bytes_queued=%d\n"
367 "debug: blocking_rx=%d\n"
368 "debug: removed=%d\n"
369 "debug: hardware.shutting_down=%d\n"
370 "debug: to_setup=%d\n",
371 hw->initializing,
372 hw->tx_ready,
373 hw->tx_queued,
374 hw->rx_ready,
375 hw->rx_bytes_queued,
376 hw->blocking_rx,
377 hw->removed,
378 hw->shutting_down,
379 hw->to_setup);
381 #endif
383 static char *data_type(const unsigned char *buf, unsigned length)
385 struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
387 if (length == 0)
388 return " ";
390 if (hdr->packet_rank & NL_FIRST_PACKET) {
391 switch (hdr->protocol) {
392 case TL_PROTOCOLID_COM_DATA: return "DATA ";
393 case TL_PROTOCOLID_COM_CTRL: return "CTRL ";
394 case TL_PROTOCOLID_SETUP: return "SETUP";
395 default: return "???? ";
397 } else
398 return " ";
401 #define DUMP_MAX_BYTES 64
403 static void dump_data_bytes(const char *type, const unsigned char *data,
404 unsigned length)
406 char prefix[56];
408 sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
409 type, data_type(data, length));
410 print_hex_dump_bytes(prefix, 0, (void *)data,
411 length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
414 static int do_send_fragment(struct ipw_hardware *hw, const unsigned char *data,
415 unsigned length)
417 int i;
418 unsigned long flags;
420 start_timing();
422 if (length == 0)
423 return 0;
425 if (length > hw->ll_mtu)
426 return -1;
428 if (ipwireless_debug)
429 dump_data_bytes("send", data, length);
431 spin_lock_irqsave(&hw->spinlock, flags);
433 if (hw->hw_version == HW_VERSION_1) {
434 outw((unsigned short) length, hw->base_port + IODWR);
436 for (i = 0; i < length; i += 2) {
437 unsigned short d = data[i];
438 __le16 raw_data;
440 if (likely(i + 1 < length))
441 d |= data[i + 1] << 8;
442 raw_data = cpu_to_le16(d);
443 outw(raw_data, hw->base_port + IODWR);
446 outw(DCR_TXDONE, hw->base_port + IODCR);
447 } else if (hw->hw_version == HW_VERSION_2) {
448 outw((unsigned short) length, hw->base_port + IODMADPR);
450 for (i = 0; i < length; i += 2) {
451 unsigned short d = data[i];
452 __le16 raw_data;
454 if ((i + 1 < length))
455 d |= data[i + 1] << 8;
456 raw_data = cpu_to_le16(d);
457 outw(raw_data, hw->base_port + IODMADPR);
459 while ((i & 3) != 2) {
460 outw((unsigned short) 0xDEAD, hw->base_port + IODMADPR);
461 i += 2;
463 writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
466 spin_unlock_irqrestore(&hw->spinlock, flags);
468 end_write_timing(length);
470 return 0;
473 static int do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
475 unsigned short fragment_data_len;
476 unsigned short data_left = packet->length - packet->offset;
477 unsigned short header_size;
478 union nl_packet pkt;
480 header_size =
481 (packet->fragment_count == 0)
482 ? NL_FIRST_PACKET_HEADER_SIZE
483 : NL_FOLLOWING_PACKET_HEADER_SIZE;
484 fragment_data_len = hw->ll_mtu - header_size;
485 if (data_left < fragment_data_len)
486 fragment_data_len = data_left;
488 pkt.hdr_first.protocol = packet->protocol;
489 pkt.hdr_first.address = packet->dest_addr;
490 pkt.hdr_first.packet_rank = 0;
492 /* First packet? */
493 if (packet->fragment_count == 0) {
494 pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
495 pkt.hdr_first.length_lsb = (unsigned char) packet->length;
496 pkt.hdr_first.length_msb =
497 (unsigned char) (packet->length >> 8);
500 memcpy(pkt.rawpkt + header_size,
501 ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
502 packet->offset, fragment_data_len);
503 packet->offset += fragment_data_len;
504 packet->fragment_count++;
506 /* Last packet? (May also be first packet.) */
507 if (packet->offset == packet->length)
508 pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
509 do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
511 /* If this packet has unsent data, then re-queue it. */
512 if (packet->offset < packet->length) {
514 * Re-queue it at the head of the highest priority queue so
515 * it goes before all other packets
517 unsigned long flags;
519 spin_lock_irqsave(&hw->spinlock, flags);
520 list_add(&packet->queue, &hw->tx_queue[0]);
521 spin_unlock_irqrestore(&hw->spinlock, flags);
522 } else {
523 if (packet->packet_callback)
524 packet->packet_callback(packet->callback_data,
525 packet->length);
526 kfree(packet);
529 return 0;
532 static void ipw_setup_hardware(struct ipw_hardware *hw)
534 unsigned long flags;
536 spin_lock_irqsave(&hw->spinlock, flags);
537 if (hw->hw_version == HW_VERSION_1) {
538 /* Reset RX FIFO */
539 outw(DCR_RXRESET, hw->base_port + IODCR);
540 /* SB: Reset TX FIFO */
541 outw(DCR_TXRESET, hw->base_port + IODCR);
543 /* Enable TX and RX interrupts. */
544 outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
545 } else {
547 * Set INTRACK bit (bit 0), which means we must explicitly
548 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
550 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
552 csr |= 1;
553 writew(csr, &hw->memregs_CCR->reg_config_and_status);
555 spin_unlock_irqrestore(&hw->spinlock, flags);
559 * If 'packet' is NULL, then this function allocates a new packet, setting its
560 * length to 0 and ensuring it has the specified minimum amount of free space.
562 * If 'packet' is not NULL, then this function enlarges it if it doesn't
563 * have the specified minimum amount of free space.
566 static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
567 struct ipw_rx_packet *packet,
568 int minimum_free_space)
571 if (!packet) {
572 unsigned long flags;
575 * If this is the first fragment, then we will need to fetch a
576 * packet to put it in.
578 spin_lock_irqsave(&hw->spinlock, flags);
579 /* If we have one in our pool, then pull it out. */
580 if (!list_empty(&hw->rx_pool)) {
581 packet = list_first_entry(&hw->rx_pool,
582 struct ipw_rx_packet, queue);
583 list_del(&packet->queue);
584 hw->rx_pool_size--;
585 spin_unlock_irqrestore(&hw->spinlock, flags);
586 } else {
587 /* Otherwise allocate a new one. */
588 static int min_capacity = 256;
589 int new_capacity;
591 spin_unlock_irqrestore(&hw->spinlock, flags);
592 new_capacity =
593 minimum_free_space > min_capacity
594 ? minimum_free_space
595 : min_capacity;
596 packet = kmalloc(sizeof(struct ipw_rx_packet)
597 + new_capacity, GFP_ATOMIC);
598 if (!packet)
599 return NULL;
600 packet->capacity = new_capacity;
602 packet->length = 0;
606 * If this packet does not have sufficient capacity for the data we
607 * want to add, then make it bigger.
609 if (packet->length + minimum_free_space > packet->capacity) {
610 struct ipw_rx_packet *old_packet = packet;
612 packet = kmalloc(sizeof(struct ipw_rx_packet) +
613 old_packet->length + minimum_free_space,
614 GFP_ATOMIC);
615 if (!packet)
616 return NULL;
617 memcpy(packet, old_packet,
618 sizeof(struct ipw_rx_packet)
619 + old_packet->length);
620 packet->capacity = old_packet->length + minimum_free_space;
621 kfree(old_packet);
624 return packet;
627 static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
629 if (hw->rx_pool_size > 6)
630 kfree(packet);
631 else {
632 hw->rx_pool_size++;
633 list_add_tail(&packet->queue, &hw->rx_pool);
637 static void queue_received_packet(struct ipw_hardware *hw,
638 unsigned int protocol, unsigned int address,
639 unsigned char *data, int length, int is_last)
641 unsigned int channel_idx = address - 1;
642 struct ipw_rx_packet *packet = NULL;
643 unsigned long flags;
645 /* Discard packet if channel index is out of range. */
646 if (channel_idx >= NL_NUM_OF_ADDRESSES) {
647 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
648 ": data packet has bad address %u\n", address);
649 return;
653 * ->packet_assembler is safe to touch unlocked, this is the only place
655 if (protocol == TL_PROTOCOLID_COM_DATA) {
656 struct ipw_rx_packet **assem =
657 &hw->packet_assembler[channel_idx];
660 * Create a new packet, or assembler already contains one
661 * enlarge it by 'length' bytes.
663 (*assem) = pool_allocate(hw, *assem, length);
664 if (!(*assem)) {
665 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
666 ": no memory for incomming data packet, dropped!\n");
667 return;
669 (*assem)->protocol = protocol;
670 (*assem)->channel_idx = channel_idx;
672 /* Append this packet data onto existing data. */
673 memcpy((unsigned char *)(*assem) +
674 sizeof(struct ipw_rx_packet)
675 + (*assem)->length, data, length);
676 (*assem)->length += length;
677 if (is_last) {
678 packet = *assem;
679 *assem = NULL;
680 /* Count queued DATA bytes only */
681 spin_lock_irqsave(&hw->spinlock, flags);
682 hw->rx_bytes_queued += packet->length;
683 spin_unlock_irqrestore(&hw->spinlock, flags);
685 } else {
686 /* If it's a CTRL packet, don't assemble, just queue it. */
687 packet = pool_allocate(hw, NULL, length);
688 if (!packet) {
689 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
690 ": no memory for incomming ctrl packet, dropped!\n");
691 return;
693 packet->protocol = protocol;
694 packet->channel_idx = channel_idx;
695 memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
696 data, length);
697 packet->length = length;
701 * If this is the last packet, then send the assembled packet on to the
702 * network layer.
704 if (packet) {
705 spin_lock_irqsave(&hw->spinlock, flags);
706 list_add_tail(&packet->queue, &hw->rx_queue);
707 /* Block reception of incoming packets if queue is full. */
708 hw->blocking_rx =
709 hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
711 spin_unlock_irqrestore(&hw->spinlock, flags);
712 schedule_work(&hw->work_rx);
717 * Workqueue callback
719 static void ipw_receive_data_work(struct work_struct *work_rx)
721 struct ipw_hardware *hw =
722 container_of(work_rx, struct ipw_hardware, work_rx);
723 unsigned long flags;
725 spin_lock_irqsave(&hw->spinlock, flags);
726 while (!list_empty(&hw->rx_queue)) {
727 struct ipw_rx_packet *packet =
728 list_first_entry(&hw->rx_queue,
729 struct ipw_rx_packet, queue);
731 if (hw->shutting_down)
732 break;
733 list_del(&packet->queue);
736 * Note: ipwireless_network_packet_received must be called in a
737 * process context (i.e. via schedule_work) because the tty
738 * output code can sleep in the tty_flip_buffer_push call.
740 if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
741 if (hw->network != NULL) {
742 /* If the network hasn't been disconnected. */
743 spin_unlock_irqrestore(&hw->spinlock, flags);
745 * This must run unlocked due to tty processing
746 * and mutex locking
748 ipwireless_network_packet_received(
749 hw->network,
750 packet->channel_idx,
751 (unsigned char *)packet
752 + sizeof(struct ipw_rx_packet),
753 packet->length);
754 spin_lock_irqsave(&hw->spinlock, flags);
756 /* Count queued DATA bytes only */
757 hw->rx_bytes_queued -= packet->length;
758 } else {
760 * This is safe to be called locked, callchain does
761 * not block
763 handle_received_CTRL_packet(hw, packet->channel_idx,
764 (unsigned char *)packet
765 + sizeof(struct ipw_rx_packet),
766 packet->length);
768 pool_free(hw, packet);
770 * Unblock reception of incoming packets if queue is no longer
771 * full.
773 hw->blocking_rx =
774 hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
775 if (hw->shutting_down)
776 break;
778 spin_unlock_irqrestore(&hw->spinlock, flags);
781 static void handle_received_CTRL_packet(struct ipw_hardware *hw,
782 unsigned int channel_idx,
783 unsigned char *data, int len)
785 struct ipw_control_packet_body *body =
786 (struct ipw_control_packet_body *) data;
787 unsigned int changed_mask;
789 if (len != sizeof(struct ipw_control_packet_body)) {
790 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
791 ": control packet was %d bytes - wrong size!\n",
792 len);
793 return;
796 switch (body->sig_no) {
797 case COMCTRL_CTS:
798 changed_mask = IPW_CONTROL_LINE_CTS;
799 break;
800 case COMCTRL_DCD:
801 changed_mask = IPW_CONTROL_LINE_DCD;
802 break;
803 case COMCTRL_DSR:
804 changed_mask = IPW_CONTROL_LINE_DSR;
805 break;
806 case COMCTRL_RI:
807 changed_mask = IPW_CONTROL_LINE_RI;
808 break;
809 default:
810 changed_mask = 0;
813 if (changed_mask != 0) {
814 if (body->value)
815 hw->control_lines[channel_idx] |= changed_mask;
816 else
817 hw->control_lines[channel_idx] &= ~changed_mask;
818 if (hw->network)
819 ipwireless_network_notify_control_line_change(
820 hw->network,
821 channel_idx,
822 hw->control_lines[channel_idx],
823 changed_mask);
827 static void handle_received_packet(struct ipw_hardware *hw,
828 union nl_packet *packet,
829 unsigned short len)
831 unsigned int protocol = packet->hdr.protocol;
832 unsigned int address = packet->hdr.address;
833 unsigned int header_length;
834 unsigned char *data;
835 unsigned int data_len;
836 int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
838 if (packet->hdr.packet_rank & NL_FIRST_PACKET)
839 header_length = NL_FIRST_PACKET_HEADER_SIZE;
840 else
841 header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
843 data = packet->rawpkt + header_length;
844 data_len = len - header_length;
845 switch (protocol) {
846 case TL_PROTOCOLID_COM_DATA:
847 case TL_PROTOCOLID_COM_CTRL:
848 queue_received_packet(hw, protocol, address, data, data_len,
849 is_last);
850 break;
851 case TL_PROTOCOLID_SETUP:
852 handle_received_SETUP_packet(hw, address, data, data_len,
853 is_last);
854 break;
858 static void acknowledge_data_read(struct ipw_hardware *hw)
860 if (hw->hw_version == HW_VERSION_1)
861 outw(DCR_RXDONE, hw->base_port + IODCR);
862 else
863 writew(MEMRX_PCINTACKK,
864 &hw->memory_info_regs->memreg_pc_interrupt_ack);
868 * Retrieve a packet from the IPW hardware.
870 static void do_receive_packet(struct ipw_hardware *hw)
872 unsigned len;
873 unsigned int i;
874 unsigned char pkt[LL_MTU_MAX];
876 start_timing();
878 if (hw->hw_version == HW_VERSION_1) {
879 len = inw(hw->base_port + IODRR);
880 if (len > hw->ll_mtu) {
881 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
882 ": received a packet of %u bytes - "
883 "longer than the MTU!\n", len);
884 outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
885 return;
888 for (i = 0; i < len; i += 2) {
889 __le16 raw_data = inw(hw->base_port + IODRR);
890 unsigned short data = le16_to_cpu(raw_data);
892 pkt[i] = (unsigned char) data;
893 pkt[i + 1] = (unsigned char) (data >> 8);
895 } else {
896 len = inw(hw->base_port + IODMADPR);
897 if (len > hw->ll_mtu) {
898 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
899 ": received a packet of %u bytes - "
900 "longer than the MTU!\n", len);
901 writew(MEMRX_PCINTACKK,
902 &hw->memory_info_regs->memreg_pc_interrupt_ack);
903 return;
906 for (i = 0; i < len; i += 2) {
907 __le16 raw_data = inw(hw->base_port + IODMADPR);
908 unsigned short data = le16_to_cpu(raw_data);
910 pkt[i] = (unsigned char) data;
911 pkt[i + 1] = (unsigned char) (data >> 8);
914 while ((i & 3) != 2) {
915 inw(hw->base_port + IODMADPR);
916 i += 2;
920 acknowledge_data_read(hw);
922 if (ipwireless_debug)
923 dump_data_bytes("recv", pkt, len);
925 handle_received_packet(hw, (union nl_packet *) pkt, len);
927 end_read_timing(len);
930 static int get_current_packet_priority(struct ipw_hardware *hw)
933 * If we're initializing, don't send anything of higher priority than
934 * PRIO_SETUP. The network layer therefore need not care about
935 * hardware initialization - any of its stuff will simply be queued
936 * until setup is complete.
938 return (hw->to_setup || hw->initializing
939 ? PRIO_SETUP + 1 :
940 NL_NUM_OF_PRIORITIES);
944 * return 1 if something has been received from hw
946 static int get_packets_from_hw(struct ipw_hardware *hw)
948 int received = 0;
949 unsigned long flags;
951 spin_lock_irqsave(&hw->spinlock, flags);
952 while (hw->rx_ready && !hw->blocking_rx) {
953 received = 1;
954 hw->rx_ready--;
955 spin_unlock_irqrestore(&hw->spinlock, flags);
957 do_receive_packet(hw);
959 spin_lock_irqsave(&hw->spinlock, flags);
961 spin_unlock_irqrestore(&hw->spinlock, flags);
963 return received;
967 * Send pending packet up to given priority, prioritize SETUP data until
968 * hardware is fully setup.
970 * return 1 if more packets can be sent
972 static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
974 int more_to_send = 0;
975 unsigned long flags;
977 spin_lock_irqsave(&hw->spinlock, flags);
978 if (hw->tx_queued && hw->tx_ready != 0) {
979 int priority;
980 struct ipw_tx_packet *packet = NULL;
982 hw->tx_ready--;
984 /* Pick a packet */
985 for (priority = 0; priority < priority_limit; priority++) {
986 if (!list_empty(&hw->tx_queue[priority])) {
987 packet = list_first_entry(
988 &hw->tx_queue[priority],
989 struct ipw_tx_packet,
990 queue);
992 list_del(&packet->queue);
994 break;
997 if (!packet) {
998 hw->tx_queued = 0;
999 spin_unlock_irqrestore(&hw->spinlock, flags);
1000 return 0;
1002 spin_unlock_irqrestore(&hw->spinlock, flags);
1004 /* Send */
1005 do_send_packet(hw, packet);
1007 /* Check if more to send */
1008 spin_lock_irqsave(&hw->spinlock, flags);
1009 for (priority = 0; priority < priority_limit; priority++)
1010 if (!list_empty(&hw->tx_queue[priority])) {
1011 more_to_send = 1;
1012 break;
1015 if (!more_to_send)
1016 hw->tx_queued = 0;
1018 spin_unlock_irqrestore(&hw->spinlock, flags);
1020 return more_to_send;
1024 * Send and receive all queued packets.
1026 static void ipwireless_do_tasklet(unsigned long hw_)
1028 struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1029 unsigned long flags;
1031 spin_lock_irqsave(&hw->spinlock, flags);
1032 if (hw->shutting_down) {
1033 spin_unlock_irqrestore(&hw->spinlock, flags);
1034 return;
1037 if (hw->to_setup == 1) {
1039 * Initial setup data sent to hardware
1041 hw->to_setup = 2;
1042 spin_unlock_irqrestore(&hw->spinlock, flags);
1044 ipw_setup_hardware(hw);
1045 ipw_send_setup_packet(hw);
1047 send_pending_packet(hw, PRIO_SETUP + 1);
1048 get_packets_from_hw(hw);
1049 } else {
1050 int priority_limit = get_current_packet_priority(hw);
1051 int again;
1053 spin_unlock_irqrestore(&hw->spinlock, flags);
1055 do {
1056 again = send_pending_packet(hw, priority_limit);
1057 again |= get_packets_from_hw(hw);
1058 } while (again);
1063 * return true if the card is physically present.
1065 static int is_card_present(struct ipw_hardware *hw)
1067 if (hw->hw_version == HW_VERSION_1)
1068 return inw(hw->base_port + IOIR) != 0xFFFF;
1069 else
1070 return readl(&hw->memory_info_regs->memreg_card_present) ==
1071 CARD_PRESENT_VALUE;
1074 static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1075 struct ipw_hardware *hw)
1077 unsigned short irqn;
1079 irqn = inw(hw->base_port + IOIR);
1081 /* Check if card is present */
1082 if (irqn == 0xFFFF)
1083 return IRQ_NONE;
1084 else if (irqn != 0) {
1085 unsigned short ack = 0;
1086 unsigned long flags;
1088 /* Transmit complete. */
1089 if (irqn & IR_TXINTR) {
1090 ack |= IR_TXINTR;
1091 spin_lock_irqsave(&hw->spinlock, flags);
1092 hw->tx_ready++;
1093 spin_unlock_irqrestore(&hw->spinlock, flags);
1095 /* Received data */
1096 if (irqn & IR_RXINTR) {
1097 ack |= IR_RXINTR;
1098 spin_lock_irqsave(&hw->spinlock, flags);
1099 hw->rx_ready++;
1100 spin_unlock_irqrestore(&hw->spinlock, flags);
1102 if (ack != 0) {
1103 outw(ack, hw->base_port + IOIR);
1104 tasklet_schedule(&hw->tasklet);
1106 return IRQ_HANDLED;
1108 return IRQ_NONE;
1111 static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1113 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1115 csr &= 0xfffd;
1116 writew(csr, &hw->memregs_CCR->reg_config_and_status);
1119 static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1120 struct ipw_hardware *hw)
1122 int tx = 0;
1123 int rx = 0;
1124 int rx_repeat = 0;
1125 int try_mem_tx_old;
1126 unsigned long flags;
1128 do {
1130 unsigned short memtx = readw(hw->memreg_tx);
1131 unsigned short memtx_serial;
1132 unsigned short memrxdone =
1133 readw(&hw->memory_info_regs->memreg_rx_done);
1135 try_mem_tx_old = 0;
1137 /* check whether the interrupt was generated by ipwireless card */
1138 if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1140 /* check if the card uses memreg_tx_old register */
1141 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1142 memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1143 if (memtx & MEMTX_TX) {
1144 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1145 ": Using memreg_tx_old\n");
1146 hw->memreg_tx =
1147 &hw->memory_info_regs->memreg_tx_old;
1148 } else {
1149 return IRQ_NONE;
1151 } else {
1152 return IRQ_NONE;
1157 * See if the card is physically present. Note that while it is
1158 * powering up, it appears not to be present.
1160 if (!is_card_present(hw)) {
1161 acknowledge_pcmcia_interrupt(hw);
1162 return IRQ_HANDLED;
1165 memtx_serial = memtx & (unsigned short) 0xff00;
1166 if (memtx & MEMTX_TX) {
1167 writew(memtx_serial, hw->memreg_tx);
1169 if (hw->serial_number_detected) {
1170 if (memtx_serial != hw->last_memtx_serial) {
1171 hw->last_memtx_serial = memtx_serial;
1172 spin_lock_irqsave(&hw->spinlock, flags);
1173 hw->rx_ready++;
1174 spin_unlock_irqrestore(&hw->spinlock, flags);
1175 rx = 1;
1176 } else
1177 /* Ignore 'Timer Recovery' duplicates. */
1178 rx_repeat = 1;
1179 } else {
1181 * If a non-zero serial number is seen, then enable
1182 * serial number checking.
1184 if (memtx_serial != 0) {
1185 hw->serial_number_detected = 1;
1186 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1187 ": memreg_tx serial num detected\n");
1189 spin_lock_irqsave(&hw->spinlock, flags);
1190 hw->rx_ready++;
1191 spin_unlock_irqrestore(&hw->spinlock, flags);
1193 rx = 1;
1196 if (memrxdone & MEMRX_RX_DONE) {
1197 writew(0, &hw->memory_info_regs->memreg_rx_done);
1198 spin_lock_irqsave(&hw->spinlock, flags);
1199 hw->tx_ready++;
1200 spin_unlock_irqrestore(&hw->spinlock, flags);
1201 tx = 1;
1203 if (tx)
1204 writew(MEMRX_PCINTACKK,
1205 &hw->memory_info_regs->memreg_pc_interrupt_ack);
1207 acknowledge_pcmcia_interrupt(hw);
1209 if (tx || rx)
1210 tasklet_schedule(&hw->tasklet);
1211 else if (!rx_repeat) {
1212 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1213 if (hw->serial_number_detected)
1214 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1215 ": spurious interrupt - new_tx mode\n");
1216 else {
1217 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1218 ": no valid memreg_tx value - "
1219 "switching to the old memreg_tx\n");
1220 hw->memreg_tx =
1221 &hw->memory_info_regs->memreg_tx_old;
1222 try_mem_tx_old = 1;
1224 } else
1225 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1226 ": spurious interrupt - old_tx mode\n");
1229 } while (try_mem_tx_old == 1);
1231 return IRQ_HANDLED;
1234 irqreturn_t ipwireless_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1236 struct ipw_hardware *hw = dev_id;
1238 if (hw->hw_version == HW_VERSION_1)
1239 return ipwireless_handle_v1_interrupt(irq, hw);
1240 else
1241 return ipwireless_handle_v2_v3_interrupt(irq, hw);
1244 static void flush_packets_to_hw(struct ipw_hardware *hw)
1246 int priority_limit;
1247 unsigned long flags;
1249 spin_lock_irqsave(&hw->spinlock, flags);
1250 priority_limit = get_current_packet_priority(hw);
1251 spin_unlock_irqrestore(&hw->spinlock, flags);
1253 while (send_pending_packet(hw, priority_limit));
1256 static void send_packet(struct ipw_hardware *hw, int priority,
1257 struct ipw_tx_packet *packet)
1259 unsigned long flags;
1261 spin_lock_irqsave(&hw->spinlock, flags);
1262 list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1263 hw->tx_queued = 1;
1264 spin_unlock_irqrestore(&hw->spinlock, flags);
1266 flush_packets_to_hw(hw);
1269 /* Create data packet, non-atomic allocation */
1270 static void *alloc_data_packet(int data_size,
1271 unsigned char dest_addr,
1272 unsigned char protocol)
1274 struct ipw_tx_packet *packet = kzalloc(
1275 sizeof(struct ipw_tx_packet) + data_size,
1276 GFP_ATOMIC);
1278 if (!packet)
1279 return NULL;
1281 INIT_LIST_HEAD(&packet->queue);
1282 packet->dest_addr = dest_addr;
1283 packet->protocol = protocol;
1284 packet->length = data_size;
1286 return packet;
1289 static void *alloc_ctrl_packet(int header_size,
1290 unsigned char dest_addr,
1291 unsigned char protocol,
1292 unsigned char sig_no)
1295 * sig_no is located right after ipw_tx_packet struct in every
1296 * CTRL or SETUP packets, we can use ipw_control_packet as a
1297 * common struct
1299 struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1301 if (!packet)
1302 return NULL;
1304 INIT_LIST_HEAD(&packet->header.queue);
1305 packet->header.dest_addr = dest_addr;
1306 packet->header.protocol = protocol;
1307 packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1308 packet->body.sig_no = sig_no;
1310 return packet;
1313 int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1314 unsigned char *data, unsigned int length,
1315 void (*callback) (void *cb, unsigned int length),
1316 void *callback_data)
1318 struct ipw_tx_packet *packet;
1320 packet = alloc_data_packet(length,
1321 (unsigned char) (channel_idx + 1),
1322 TL_PROTOCOLID_COM_DATA);
1323 if (!packet)
1324 return -ENOMEM;
1325 packet->packet_callback = callback;
1326 packet->callback_data = callback_data;
1327 memcpy((unsigned char *) packet +
1328 sizeof(struct ipw_tx_packet), data, length);
1330 send_packet(hw, PRIO_DATA, packet);
1331 return 0;
1334 static int set_control_line(struct ipw_hardware *hw, int prio,
1335 unsigned int channel_idx, int line, int state)
1337 struct ipw_control_packet *packet;
1338 int protocolid = TL_PROTOCOLID_COM_CTRL;
1340 if (prio == PRIO_SETUP)
1341 protocolid = TL_PROTOCOLID_SETUP;
1343 packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1344 (unsigned char) (channel_idx + 1),
1345 protocolid, line);
1346 if (!packet)
1347 return -ENOMEM;
1348 packet->header.length = sizeof(struct ipw_control_packet_body);
1349 packet->body.value = (unsigned char) (state == 0 ? 0 : 1);
1350 send_packet(hw, prio, &packet->header);
1351 return 0;
1355 static int set_DTR(struct ipw_hardware *hw, int priority,
1356 unsigned int channel_idx, int state)
1358 if (state != 0)
1359 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1360 else
1361 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1363 return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1366 static int set_RTS(struct ipw_hardware *hw, int priority,
1367 unsigned int channel_idx, int state)
1369 if (state != 0)
1370 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1371 else
1372 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1374 return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1377 int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1378 int state)
1380 return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1383 int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1384 int state)
1386 return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1389 struct ipw_setup_get_version_query_packet {
1390 struct ipw_tx_packet header;
1391 struct tl_setup_get_version_qry body;
1394 struct ipw_setup_config_packet {
1395 struct ipw_tx_packet header;
1396 struct tl_setup_config_msg body;
1399 struct ipw_setup_config_done_packet {
1400 struct ipw_tx_packet header;
1401 struct tl_setup_config_done_msg body;
1404 struct ipw_setup_open_packet {
1405 struct ipw_tx_packet header;
1406 struct tl_setup_open_msg body;
1409 struct ipw_setup_info_packet {
1410 struct ipw_tx_packet header;
1411 struct tl_setup_info_msg body;
1414 struct ipw_setup_reboot_msg_ack {
1415 struct ipw_tx_packet header;
1416 struct TlSetupRebootMsgAck body;
1419 /* This handles the actual initialization of the card */
1420 static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1422 struct ipw_setup_config_packet *config_packet;
1423 struct ipw_setup_config_done_packet *config_done_packet;
1424 struct ipw_setup_open_packet *open_packet;
1425 struct ipw_setup_info_packet *info_packet;
1426 int port;
1427 unsigned int channel_idx;
1429 /* generate config packet */
1430 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1431 config_packet = alloc_ctrl_packet(
1432 sizeof(struct ipw_setup_config_packet),
1433 ADDR_SETUP_PROT,
1434 TL_PROTOCOLID_SETUP,
1435 TL_SETUP_SIGNO_CONFIG_MSG);
1436 if (!config_packet)
1437 goto exit_nomem;
1438 config_packet->header.length = sizeof(struct tl_setup_config_msg);
1439 config_packet->body.port_no = port;
1440 config_packet->body.prio_data = PRIO_DATA;
1441 config_packet->body.prio_ctrl = PRIO_CTRL;
1442 send_packet(hw, PRIO_SETUP, &config_packet->header);
1444 config_done_packet = alloc_ctrl_packet(
1445 sizeof(struct ipw_setup_config_done_packet),
1446 ADDR_SETUP_PROT,
1447 TL_PROTOCOLID_SETUP,
1448 TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1449 if (!config_done_packet)
1450 goto exit_nomem;
1451 config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1452 send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1454 /* generate open packet */
1455 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1456 open_packet = alloc_ctrl_packet(
1457 sizeof(struct ipw_setup_open_packet),
1458 ADDR_SETUP_PROT,
1459 TL_PROTOCOLID_SETUP,
1460 TL_SETUP_SIGNO_OPEN_MSG);
1461 if (!open_packet)
1462 goto exit_nomem;
1463 open_packet->header.length = sizeof(struct tl_setup_open_msg);
1464 open_packet->body.port_no = port;
1465 send_packet(hw, PRIO_SETUP, &open_packet->header);
1467 for (channel_idx = 0;
1468 channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1469 int ret;
1471 ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1472 (hw->control_lines[channel_idx] &
1473 IPW_CONTROL_LINE_DTR) != 0);
1474 if (ret) {
1475 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1476 ": error setting DTR (%d)\n", ret);
1477 return;
1480 set_RTS(hw, PRIO_SETUP, channel_idx,
1481 (hw->control_lines [channel_idx] &
1482 IPW_CONTROL_LINE_RTS) != 0);
1483 if (ret) {
1484 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1485 ": error setting RTS (%d)\n", ret);
1486 return;
1490 * For NDIS we assume that we are using sync PPP frames, for COM async.
1491 * This driver uses NDIS mode too. We don't bother with translation
1492 * from async -> sync PPP.
1494 info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1495 ADDR_SETUP_PROT,
1496 TL_PROTOCOLID_SETUP,
1497 TL_SETUP_SIGNO_INFO_MSG);
1498 if (!info_packet)
1499 goto exit_nomem;
1500 info_packet->header.length = sizeof(struct tl_setup_info_msg);
1501 info_packet->body.driver_type = NDISWAN_DRIVER;
1502 info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1503 info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1504 send_packet(hw, PRIO_SETUP, &info_packet->header);
1506 /* Initialization is now complete, so we clear the 'to_setup' flag */
1507 hw->to_setup = 0;
1509 return;
1511 exit_nomem:
1512 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1513 ": not enough memory to alloc control packet\n");
1514 hw->to_setup = -1;
1517 static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1518 unsigned char vers_no)
1520 del_timer(&hw->setup_timer);
1521 hw->initializing = 0;
1522 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1524 if (vers_no == TL_SETUP_VERSION)
1525 __handle_setup_get_version_rsp(hw);
1526 else
1527 printk(KERN_ERR
1528 IPWIRELESS_PCCARD_NAME
1529 ": invalid hardware version no %u\n",
1530 (unsigned int) vers_no);
1533 static void ipw_send_setup_packet(struct ipw_hardware *hw)
1535 struct ipw_setup_get_version_query_packet *ver_packet;
1537 ver_packet = alloc_ctrl_packet(
1538 sizeof(struct ipw_setup_get_version_query_packet),
1539 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1540 TL_SETUP_SIGNO_GET_VERSION_QRY);
1541 ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1544 * Response is handled in handle_received_SETUP_packet
1546 send_packet(hw, PRIO_SETUP, &ver_packet->header);
1549 static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1550 unsigned int address,
1551 unsigned char *data, int len,
1552 int is_last)
1554 union ipw_setup_rx_msg *rx_msg = (union ipw_setup_rx_msg *) data;
1556 if (address != ADDR_SETUP_PROT) {
1557 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1558 ": setup packet has bad address %d\n", address);
1559 return;
1562 switch (rx_msg->sig_no) {
1563 case TL_SETUP_SIGNO_GET_VERSION_RSP:
1564 if (hw->to_setup)
1565 handle_setup_get_version_rsp(hw,
1566 rx_msg->version_rsp_msg.version);
1567 break;
1569 case TL_SETUP_SIGNO_OPEN_MSG:
1570 if (ipwireless_debug) {
1571 unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1573 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1574 ": OPEN_MSG [channel %u] reply received\n",
1575 channel_idx);
1577 break;
1579 case TL_SETUP_SIGNO_INFO_MSG_ACK:
1580 if (ipwireless_debug)
1581 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1582 ": card successfully configured as NDISWAN\n");
1583 break;
1585 case TL_SETUP_SIGNO_REBOOT_MSG:
1586 if (hw->to_setup)
1587 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1588 ": Setup not completed - ignoring reboot msg\n");
1589 else {
1590 struct ipw_setup_reboot_msg_ack *packet;
1592 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1593 ": Acknowledging REBOOT message\n");
1594 packet = alloc_ctrl_packet(
1595 sizeof(struct ipw_setup_reboot_msg_ack),
1596 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1597 TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1598 packet->header.length =
1599 sizeof(struct TlSetupRebootMsgAck);
1600 send_packet(hw, PRIO_SETUP, &packet->header);
1601 if (hw->reboot_callback)
1602 hw->reboot_callback(hw->reboot_callback_data);
1604 break;
1606 default:
1607 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1608 ": unknown setup message %u received\n",
1609 (unsigned int) rx_msg->sig_no);
1613 static void do_close_hardware(struct ipw_hardware *hw)
1615 unsigned int irqn;
1617 if (hw->hw_version == HW_VERSION_1) {
1618 /* Disable TX and RX interrupts. */
1619 outw(0, hw->base_port + IOIER);
1621 /* Acknowledge any outstanding interrupt requests */
1622 irqn = inw(hw->base_port + IOIR);
1623 if (irqn & IR_TXINTR)
1624 outw(IR_TXINTR, hw->base_port + IOIR);
1625 if (irqn & IR_RXINTR)
1626 outw(IR_RXINTR, hw->base_port + IOIR);
1628 synchronize_irq(hw->irq);
1632 struct ipw_hardware *ipwireless_hardware_create(void)
1634 int i;
1635 struct ipw_hardware *hw =
1636 kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1638 if (!hw)
1639 return NULL;
1641 hw->irq = -1;
1642 hw->initializing = 1;
1643 hw->tx_ready = 1;
1644 hw->rx_bytes_queued = 0;
1645 hw->rx_pool_size = 0;
1646 hw->last_memtx_serial = (unsigned short) 0xffff;
1647 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1648 INIT_LIST_HEAD(&hw->tx_queue[i]);
1650 INIT_LIST_HEAD(&hw->rx_queue);
1651 INIT_LIST_HEAD(&hw->rx_pool);
1652 spin_lock_init(&hw->spinlock);
1653 tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1654 INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1655 setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1656 (unsigned long) hw);
1658 return hw;
1661 void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1662 unsigned int base_port,
1663 void __iomem *attr_memory,
1664 void __iomem *common_memory,
1665 int is_v2_card,
1666 void (*reboot_callback) (void *data),
1667 void *reboot_callback_data)
1669 if (hw->removed) {
1670 hw->removed = 0;
1671 enable_irq(hw->irq);
1673 hw->base_port = base_port;
1674 hw->hw_version = is_v2_card ? HW_VERSION_2 : HW_VERSION_1;
1675 hw->ll_mtu = hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2;
1676 hw->memregs_CCR = (struct MEMCCR __iomem *)
1677 ((unsigned short __iomem *) attr_memory + 0x200);
1678 hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1679 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1680 hw->reboot_callback = reboot_callback;
1681 hw->reboot_callback_data = reboot_callback_data;
1684 void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1686 hw->initializing = 1;
1687 hw->init_loops = 0;
1688 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1689 ": waiting for card to start up...\n");
1690 ipwireless_setup_timer((unsigned long) hw);
1693 static void ipwireless_setup_timer(unsigned long data)
1695 struct ipw_hardware *hw = (struct ipw_hardware *) data;
1697 hw->init_loops++;
1699 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1700 hw->hw_version == HW_VERSION_2 &&
1701 hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1702 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1703 ": failed to startup using TX2, trying TX\n");
1705 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1706 hw->init_loops = 0;
1708 /* Give up after a certain number of retries */
1709 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1710 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1711 ": card failed to start up!\n");
1712 hw->initializing = 0;
1713 } else {
1714 /* Do not attempt to write to the board if it is not present. */
1715 if (is_card_present(hw)) {
1716 unsigned long flags;
1718 spin_lock_irqsave(&hw->spinlock, flags);
1719 hw->to_setup = 1;
1720 hw->tx_ready = 1;
1721 spin_unlock_irqrestore(&hw->spinlock, flags);
1722 tasklet_schedule(&hw->tasklet);
1725 mod_timer(&hw->setup_timer,
1726 jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1731 * Stop any interrupts from executing so that, once this function returns,
1732 * other layers of the driver can be sure they won't get any more callbacks.
1733 * Thus must be called on a proper process context.
1735 void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1737 if (!hw->shutting_down) {
1738 /* Tell everyone we are going down. */
1739 hw->shutting_down = 1;
1740 del_timer(&hw->setup_timer);
1742 /* Prevent the hardware from sending any more interrupts */
1743 do_close_hardware(hw);
1747 void ipwireless_hardware_free(struct ipw_hardware *hw)
1749 int i;
1750 struct ipw_rx_packet *rp, *rq;
1751 struct ipw_tx_packet *tp, *tq;
1753 ipwireless_stop_interrupts(hw);
1755 flush_scheduled_work();
1757 for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1758 if (hw->packet_assembler[i] != NULL)
1759 kfree(hw->packet_assembler[i]);
1761 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1762 list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1763 list_del(&tp->queue);
1764 kfree(tp);
1767 list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1768 list_del(&rp->queue);
1769 kfree(rp);
1772 list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1773 list_del(&rp->queue);
1774 kfree(rp);
1776 kfree(hw);
1780 * Associate the specified network with this hardware, so it will receive events
1781 * from it.
1783 void ipwireless_associate_network(struct ipw_hardware *hw,
1784 struct ipw_network *network)
1786 hw->network = network;