summaryrefslogtreecommitdiffstats
path: root/drivers/usb/plusb.c
blob: 0684f7819074f18f4fe875c52551b4375d323cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
/*****************************************************************************/

/*
 *      plusb.c  --  prolific pl-2301/pl-2302 driver.
 *
 *      Copyright (C) 2000  Deti Fliegl (deti@fliegl.de)
 *      Copyright (C) 2000  Pavel Machek (pavel@suse.cz)
 *      Copyright (C) 2000  Eric Z. Ayers (eric@compgen.com)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *  This driver creates a network interface (plusb0, plusb1, ...) that will
 *  send messages over a USB host-host cable based on the Prolific ASIC.
 *  It works a lot like plip or PP over an RS-232C null modem cable.
 *
 *  Expect speeds of around 330Kbytes/second over a UHCI host controller.
 *  OHCI should be faster.  Increase the MTU for faster transfers of large
 *  files (up-to 800Kbytes/second).  (16384 is a good size)
 *
 *  $Id: plusb.c,v 1.18 2000/02/14 10:38:58 fliegl Exp $
 *
 *  Changelog:
 *
 *    v0.1                deti
 *        Original Version of driver.
 *    v0.2     15 Sep 2000  pavel
 *        Patches to decrease latency by rescheduling the bottom half of
 *          interrupt code.
 *    v0.3     10 Oct 2000  eric
 *        Patches to work in v2.2 backport (v2.4 changes the way net_dev.name
 *          is allocated)
 *    v0.4     19 Oct 2000  eric
 *        Some more performance fixes.  Lock re-submitting urbs.
 *          Lower the number of sk_buff's to queue.
 *    v0.5     25 Oct 2000 eric
 *        Removed use of usb_bulk_msg() all together.  This caused
 *          the driver to block in an interrupt context.
 *        Consolidate read urb submission into read_urb_submit().
 *        Performance is the same as v0.4.
 *    v0.5.1   27 Oct 2000 eric
 *        Extra debugging messages to help diagnose problem with uchi.o stack.
 *    v0.5.2   27 Oct 2000 eric
 *        Set the 'start' flag for the network device in plusb_net_start()
 *         and plusb_net_stop() (doesn't help)
 *    v0.5.3   27 Oct 2000 pavel
 *        Commented out handlers when -EPIPE is received,
 *         (remove calls to usb_clear_halt()) Since the callback is in
 *         an interrupt context, it doesn't help, it just panics
 *         the kernel. (what do we do?)
 *        Under high load, dev_alloc_skb() fails, the read URB must
 *         be re-submitted.
 *        Added plusb_change_mtu() and increased the size of _BULK_DATA_LEN
 *    v0.5.4   31 Oct 2000 eric
 *        Fix race between plusb_net_xmit() and plusb_bulk_write_complete()
 *    v0.5.5    1 Nov 2000 eric
 *        Remove dev->start field, otherwise, it won't compile in 2.4
 *        Use dev_kfree_skb_any(). (important in 2.4 kernel)
 *    v0.5.6   2 Nov 2000 pavel,eric
 *        Add calls to netif_stop_queue() and netif_start_queue()
 *        Drop packets that come in while the free list is empty.
 *        (This version is being submitted after the release of 2.4-test10)
 *    v0.5.7   6 Nov 2000
 *        Fix to not re-submit the urb on error to help when cables
 *          are yanked (not tested)
 *
 *
 * KNOWN PROBLEMS: (Any suggestions greatfully accepted!)
 *
 *     2 Nov 2000
 *      - The shutdown for this may not be entirely clean.  Sometimes, the
 *         kernel will Oops when the cable is unplugged, or
 *         if the plusb module is removed.
 *      - If you ifdown a device and then ifup it again, the link will not
 *         always work.  You have to 'rmmod plusb ; modprobe plusb' on
 *         both machines to get it to work again.  Something must be wrong with
 *         plusb_net_open() and plusb_net_start() ?  Maybe
 *         the 'suspend' and 'resume' entry points need to be
 *         implemented?
 *      - Needs to handle -EPIPE correctly in bulk complete handlers.
 *         (replace usb_clear_halt() function with async urbs?)
 *      - I think this code relies too much on one spinlock and does
 *         too much in the interrupt handler.  The net1080 code is
 *         much more elegant, and should work for this chip.  Its
 *         only drawback is that it is going to be tough to backport
 *         it to v2.2.
 *      - Occasionally the device will hang under the 'uhci.o'
 *         driver.   The workaround is to ifdown the device and
 *         remove the modules, then re-insert them.  You may have
 *         better luck with the 'usb-uhci.o' driver.
 *      - After using ifconfig down ; ifconfig up, sometimes packets
 *         continue to be received, but there is a framing problem.
 *
 * FUTURE DIRECTIONS:
 *
 *     - Fix the known problems.
 *     - There isn't much functional difference between the net1080
 *        driver and this one.  It would be neat if the same driver
 *        could handle both types of chips.  Or if both drivers
 *        could handle both types of chips - this one is easier to
 *        backport to the 2.2 kernel.
 *     - Get rid of plusb_add_buf_tail and the single spinlock.
 *        Use a separate spinlock for the 2 lists, and use atomic
 *        operators for writeurb_submitted and readurb_submitted members.
 *
 *
 */

/*****************************************************************************/

#include <linux/module.h>
#include <linux/socket.h>
#include <linux/miscdevice.h>
#include <linux/list.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
//#define DEBUG 1
#include <linux/usb.h>

#if (LINUX_VERSION_CODE < 0x020300)
#define dev_kfree_skb_any dev_kfree_skb
#endif

/* Definitions formerly in plusb.h relocated. No need to export them -EZA */

#define _PLUSB_INTPIPE		0x1
#define _PLUSB_BULKOUTPIPE	0x2
#define _PLUSB_BULKINPIPE	0x3

#define _SKB_NUM		32

/* increase size of BULK_DATA_LEN so we can use bigger MTU's*/
#define _BULK_DATA_LEN		32768


typedef struct
{
	int connected;   /* indicates if this structure is active */
	struct usb_device *usbdev;
					 /* keep track of USB structure */
	int status;      /* Prolific status byte returned from interrupt */
	int in_bh;       /* flag to indicate that we are in the bulk handler */
	int opened;      /* flag to indicate that network dev is open    */

	spinlock_t lock; /* Lock for the buffer list. re-used for
						locking around submitting the readurb member.
					 */
	urb_t *inturb;   /* Read buffer for the interrupt callback */
	unsigned char *		interrupt_in_buffer;
			 /* holds data for the inturb*/
	urb_t *readurb;  /* Read buffer for the bulk data callback */
	unsigned char *		bulk_in_buffer;
					 /* kmalloc'ed data for the readurb */
	int readurb_submitted;
					 /* Flag to indicate that readurb already sent */
	urb_t *writeurb; /* Write buffer for the bulk data callback */
	int writeurb_submitted;
					 /* Flag to indicate that writeurb already sent */
	
	struct list_head tx_skb_list;
		 			 /* sk_buff's read from net device */
	struct list_head free_skb_list;
					/* free sk_buff list */
	struct net_device net_dev;
					/* handle to linux network device */
	struct net_device_stats net_stats;
					/* stats to return for ifconfig output */
} plusb_t,*pplusb_t;

/*
 * skb_list - queue of packets from the network driver to be delivered to USB
 */
typedef struct
{
	struct list_head skb_list;
	struct sk_buff *skb;
	int state;
	plusb_t *s;
} skb_list_t,*pskb_list_t;


/* --------------------------------------------------------------------- */

#define NRPLUSB 4

/*
 * Interrupt endpoint status byte, from Prolific PL-2301 docs
 * Check the 'download' link at www.prolifictech.com
 */
#define _PL_INT_RES1    0x80 /* reserved              */
#define _PL_INT_RES2    0x40 /* reserved              */
#define _PL_INT_RXD	_PL_INT_RES2  /* Read data ready - Not documented by Prolific, but seems to work! */
#define _PL_INT_TX_RDY	0x20 /* OK to transmit data   */
#define _PL_INT_RESET_O	0x10 /* reset output pipe     */
#define _PL_INT_RESET_I 0x08 /* reset input pipe      */
#define _PL_INT_TX_C    0x04 /* transmission complete */
#define _PL_INT_TX_REQ  0x02 /* transmission received */
#define _PL_INT_PEER_E  0x01 /* peer exists           */

/*-------------------------------------------------------------------*/

static plusb_t plusb[NRPLUSB];

static void plusb_write_bulk_complete(urb_t *purb);
static void plusb_read_bulk_complete(urb_t *purb);
static void plusb_int_complete(urb_t *purb);

/* --------------------------------------------------------------------- */

/*
 * plusb_add_buf_tail - Take the head of the src list and append it to
 *                      the tail of the dest list
 */
static int plusb_add_buf_tail (plusb_t *s, struct list_head *dst, struct list_head *src)
{
	unsigned long flags = 0;
	struct list_head *tmp;
	int ret = 0;

	spin_lock_irqsave (&s->lock, flags);

	if (list_empty (src)) {
		// no elements in source buffer
		ret = -1;
		goto err;
	}
	tmp = src->next;
	list_del (tmp);
	list_add_tail (tmp, dst);

  err:	spin_unlock_irqrestore (&s->lock, flags);
	return ret;
}
/*-------------------------------------------------------------------*/

/*
 * dequeue_next_skb - submit the first thing on the tx_skb_list to the
 * USB stack.  This function should be called each time we get a new
 * message to send to the other host, or each time a message is sucessfully
 * sent.
 */
static void dequeue_next_skb(char * func, plusb_t * s)
{
	skb_list_t * skb_list;
	unsigned long flags = 0;

	if (!s->connected)
		return;
	
	spin_lock_irqsave (&s->lock, flags);
	
	if (!list_empty (&s->tx_skb_list) && !s->writeurb_submitted) {
		int submit_ret;
		skb_list = list_entry (s->tx_skb_list.next, skb_list_t, skb_list);

		if (skb_list->skb) {
			s->writeurb_submitted = 1;
		
			/* Use the buffer inside the sk_buff directly. why copy? */
			FILL_BULK_URB_TO(s->writeurb, s->usbdev,
							 usb_sndbulkpipe(s->usbdev, _PLUSB_BULKOUTPIPE),
							 skb_list->skb->data, skb_list->skb->len,
							 plusb_write_bulk_complete, skb_list, 500);
			
			dbg ("%s: %s: submitting urb. skb_list %p", s->net_dev.name, func, skb_list);
		
			submit_ret = usb_submit_urb(s->writeurb);
			if (submit_ret) {
				s->writeurb_submitted = 0;
				printk (KERN_CRIT "%s: %s: can't submit writeurb: %d\n",
						s->net_dev.name, func, submit_ret);
			}
		} /* end if the skb value has been filled in */
	}
	
	spin_unlock_irqrestore (&s->lock, flags);	
}

/*
 * submit_read_urb - re-submit the read URB to the stack
 */
void submit_read_urb(char * func, plusb_t * s)
{
	unsigned long flags=0;

	if (!s->connected)
		return;
	
	spin_lock_irqsave (&s->lock, flags);
	
	if (!s->readurb_submitted) {
		int ret;
		s->readurb_submitted=1;
		s->readurb->dev=s->usbdev;
		ret = usb_submit_urb(s->readurb);
		if (ret) {
			printk (KERN_CRIT "%s: %s: error %d submitting read URB\n",
				   s->net_dev.name, func, ret);
			s->readurb_submitted=0;
		}
	}

	spin_unlock_irqrestore (&s->lock, flags);
	
}
/* --------------------------------------------------------------------- */

/*
 * plusb_net_xmit - callback from the network device driver for outgoing data
 *
 * Data has arrived to the network device from the local machine and needs
 * to be sent over the USB cable.  This is in an interrupt, so we don't
 * want to spend too much time in this function.
 *
 */
static int plusb_net_xmit(struct sk_buff *skb, struct net_device *dev)
{
	plusb_t *s=dev->priv;
	skb_list_t *skb_list;
	unsigned int flags;

	dbg("plusb_net_xmit: len:%d i:%d",skb->len,in_interrupt());

	if(!s->connected || !s->opened) {
		/*
		  NOTE: If we get to this point, you'll return the error
		  kernel: virtual device plusb0 asks to queue packet

		  Other things we could do:
		  1) just drop this packet
		  2) drop other packets in the queue
		*/
		return 1;
	}

	spin_lock_irqsave (&s->lock, flags);

	if  (list_empty(&s->free_skb_list)
	|| plusb_add_buf_tail (s, &s->tx_skb_list, &s->free_skb_list)) {
		/* The buffers on this side are full. DROP the packet
		   I think that this shouldn't happen with the correct
		   use of the netif_XXX functions -EZA
		 */
		dbg ("plusb: Free list is empty.");
		kfree_skb(skb);
		s->net_stats.tx_dropped++;
		spin_unlock_irqrestore (&s->lock, flags);
		return 0;
	}
	
	skb_list = list_entry (s->tx_skb_list.prev, skb_list_t, skb_list);
	skb_list->skb=skb;
	skb_list->state=1;
	skb_list->s=s;

	if (list_empty(&s->free_skb_list)) {
		/* apply "backpressure". Tell the net layer to stop sending
		   the driver packets.
		*/
		netif_stop_queue(dev);
	}
	
	spin_unlock_irqrestore (&s->lock, flags);
	
	/* If there is no write urb outstanding, pull the first thing
	   off of the list and submit it to the USB stack
	*/
	dequeue_next_skb("plusb_net_xmit", s);
	
	return 0;
}

/* --------------------------------------------------------------------- */

/*
 * plusb_write_bulk_complete () - callback after the data has been
 *   sent to the USB device, or a timeout occured.
 */
static void plusb_write_bulk_complete(urb_t *purb)
{
	skb_list_t * skb_list=purb->context;
	plusb_t *s=skb_list->s;

	dbg ("%s: plusb_write_bulk_complete: status:%d skb_list:%p\n",
		 s->net_dev.name, purb->status, skb_list);

	skb_list->state=0;

	if( purb->status == -EPIPE )
		printk(KERN_CRIT "%s: plusb_write_bulk_complete: got -EPIPE and don't know what to do!\n",
		     s->net_dev.name);
		
	if(!purb->status) {
		s->net_stats.tx_packets++;
		s->net_stats.tx_bytes+=skb_list->skb->len;
	}
	else {
		err ("%s: plusb_write_bulk_complete: returned ERROR status:%d\n",
			 s->net_dev.name, purb->status);

		s->net_stats.tx_errors++;
		s->net_stats.tx_aborted_errors++;
	}
	
	dbg("plusb_bh: dev_kfree_skb");

	/* NOTE: In 2.4 it's a problem to call dev_kfree_skb() in a hard IRQ:
	   Oct 28 23:42:14 bug kernel: Warning: kfree_skb on hard IRQ c023329a
	*/
	dev_kfree_skb_any(skb_list->skb);
	
	skb_list->skb = NULL;
	if (plusb_add_buf_tail (s, &s->free_skb_list, &s->tx_skb_list)) {
		err ("plusb: tx list empty. This shouldn't happen.");
	}

	purb->status = 0;
	s->writeurb_submitted = 0;
	
    netif_wake_queue((&s->net_dev));
	
	dequeue_next_skb("plusb_write_bulk_complete", s);

	
}

/*
 * plusb_read_bulk_complete - Callback for data arriving from the USB device
 *
 * This gets called back when a full 'urb' is received from the remote system.
 * This urb was allocated by this driver and is kept in the member: s->readurb
 *
 */
static void plusb_read_bulk_complete(urb_t *purb)
{
	
	plusb_t *s=purb->context;

	dbg("plusb_read_bulk_complete: status:%d length:%d", purb->status,purb->actual_length);
	
	if(!s->connected)
		return;

	if( purb->status == -EPIPE )
		printk(KERN_CRIT "%s: plusb_read_bulk_complete: got -EPIPE and I don't know what to do!\n",
		     s->net_dev.name);
	else if (!purb->status) {
		struct sk_buff *skb;
		unsigned char *dst;
		int len=purb->transfer_buffer_length;
		struct net_device_stats *stats=&s->net_stats;

		skb=dev_alloc_skb(len);

		if(!skb) {
			printk (KERN_CRIT "%s: plusb_read_bulk_complete: dev_alloc_skb(%d)=NULL, dropping frame\n", s->net_dev.name, len);
			stats->rx_dropped++;
		} else {
			dst=(char *)skb_put(skb, len);
			memcpy( dst, purb->transfer_buffer, len);
			
			skb->dev=&s->net_dev;
			skb->protocol=eth_type_trans(skb, skb->dev);
			stats->rx_packets++;
			stats->rx_bytes+=len;
			netif_rx(skb);
		}
		
	}
	
	s->readurb_submitted = 0;
	
	if (purb->status) {
		/* Give the system a chance to "catch its breath". Shortcut
		   re-submitting the read URB>  It will be re-submitted if
		   another interrupt comes back.  The problem scenario is that
		   the plub is pulled and the read returns an error.
		   You don't want to resumbit in this case.
		*/
		err ("%s: plusb_read_bulk_complete: returned status %d\n",
			 s->net_dev.name, purb->status);
		return;
	}


	purb->status=0;

	/* Keep it coming! resubmit the URB for reading.. Make sure
	   we aren't in contention with the interrupt callback.
	*/
	submit_read_urb("plusb_read_bulk_complete", s);
}

/* --------------------------------------------------------------------- */
/*
 * plusb_int_complete - USB driver callback for interrupt msg from the device
 *
 * Interrupts are scheduled to go off on a periodic basis (see FILL_INT_URB)
 * For the prolific device, this is basically just returning a register
 * filled with bits.  See the macro definitions for _PL_INT_XXX above.
 * Most of these bits are for implementing a machine-machine protocol
 * and can be set with a special message (described as the "Quicklink"
 * feature in the prolific documentation.)
 *
 * I don't think we need any of that to work as a network device. If a
 * message is lost, big deal - that's what UNIX networking expects from
 * the physical layer.
 *
 */
static void plusb_int_complete(urb_t *purb)
{
	plusb_t *s=purb->context;
	s->status=((unsigned char*)purb->transfer_buffer)[0]&255;
	
#if 0
	/* This isn't right because 0x20 is TX_RDY and
	   sometimes will not be set
	*/
	if((s->status&0x3f)!=0x20) {
		warn("invalid device status %02X", s->status);
		return;
	}
#endif	
	if(!s->connected)
		return;

	/* Don't turn this on unless you want to see the log flooded. */
#if 0
	printk("plusb_int_complete: PEER_E:%d TX_REQ:%d TX_C:%d RESET_IN:%d RESET_O: %d TX_RDY:%d RES1:%d RES2:%d\n",
	    s->status & _PL_INT_PEER_E  ? 1 : 0,
	    s->status & _PL_INT_TX_REQ  ? 1 : 0,
	    s->status & _PL_INT_TX_C    ? 1 : 0,
	    s->status & _PL_INT_RESET_I ? 1 : 0,
	    s->status & _PL_INT_RESET_O ? 1 : 0,
	    s->status & _PL_INT_TX_RDY  ? 1 : 0,
	    s->status & _PL_INT_RES1    ? 1 : 0,
	    s->status & _PL_INT_RES2    ? 1 : 0);
#endif

#if 1
	/* At first glance, this logic appears to not really be needed, but
	   it can help recover from intermittent problems where the
	   usb_submit_urb() fails in the read callback. -EZA
	*/
	
	/* Try to submit the read URB again. Make sure
	   we aren't in contention with the bulk read callback
	*/
	submit_read_urb ("plusb_int_complete", s);
	
	/* While we are at it, why not check to see if the
	   write urb should be re-submitted?
	*/
	dequeue_next_skb("plusb_int_complete", s);	
	
#endif

}

/* --------------------------------------------------------------------- */
/*
 * plusb_free_all - deallocate all memory kept for an instance of the device.
 */
static void plusb_free_all(plusb_t *s)
{
	struct list_head *skb;
	skb_list_t *skb_list;
	
	dbg("plusb_free_all");
	
	/* set a flag to tell all callbacks to cease and desist */
	s->connected = 0;

	/* If the interrupt handler is about to fire, let it finish up */
	run_task_queue(&tq_immediate);	

	if(s->inturb) {
		dbg("unlink inturb");
		usb_unlink_urb(s->inturb);
		dbg("free_urb inturb");
		usb_free_urb(s->inturb);
		s->inturb=NULL;
	}
	
	if(s->interrupt_in_buffer) {
		dbg("kfree s->interrupt_in_buffer");
		kfree(s->interrupt_in_buffer);
		s->interrupt_in_buffer=NULL;
	}

	if(s->readurb) {
		dbg("unlink readurb");
		usb_unlink_urb(s->readurb);
		dbg("free_urb readurb:");
		usb_free_urb(s->readurb);
		s->readurb=NULL;
	}

	if(s->bulk_in_buffer) {
		dbg("kfree s->bulk_in_buffer");
		kfree(s->bulk_in_buffer);
		s->bulk_in_buffer=NULL;
	}
	
	s->readurb_submitted = 0;
	
	if(s->writeurb) {
		dbg("unlink writeurb");
		usb_unlink_urb(s->writeurb);
		dbg("free_urb writeurb:");
		usb_free_urb(s->writeurb);
		s->writeurb=NULL;
	}

	s->writeurb_submitted = 0;
	
	while(!list_empty(&s->free_skb_list)) {
		skb=s->free_skb_list.next;
		list_del(skb);
		skb_list = list_entry (skb, skb_list_t, skb_list);
		kfree(skb_list);
	}

	while(!list_empty(&s->tx_skb_list)) {
		skb=s->tx_skb_list.next;
		list_del(skb);
		skb_list = list_entry (skb, skb_list_t, skb_list);
		if (skb_list->skb) {
			dbg ("Freeing SKB in queue");
			dev_kfree_skb_any(skb_list->skb);
			skb_list->skb = NULL;
		}
		kfree(skb_list);
	}
	
	s->in_bh=0;
	
	dbg("plusb_free_all: finished");	
}

/*-------------------------------------------------------------------*/
/*
 * plusb_alloc - allocate memory associated with one instance of the device
 */
static int plusb_alloc(plusb_t *s)
{
	int i;
	skb_list_t *skb;

	dbg("plusb_alloc");

	for(i=0 ; i < _SKB_NUM ; i++) {
		skb=kmalloc(sizeof(skb_list_t), GFP_KERNEL);
		if(!skb) {
			err("kmalloc for skb_list failed");
			goto reject;
		}
		memset(skb, 0, sizeof(skb_list_t));
		list_add(&skb->skb_list, &s->free_skb_list);
	}

	dbg("inturb allocation:");
	s->inturb=usb_alloc_urb(0);
	if(!s->inturb) {
		err("alloc_urb failed");
		goto reject;
	}

	dbg("bulk read urb allocation:");
	s->readurb=usb_alloc_urb(0);
	if(!s->readurb) {
		err("alloc_urb failed");
		goto reject;
	}
	
	dbg("bulk write urb allocation:");
	s->writeurb=usb_alloc_urb(0);
	if(!s->writeurb) {
		err("alloc_urb for writeurb failed");
		goto reject;
	}
	
	dbg("readurb/inturb init:");
	s->interrupt_in_buffer=kmalloc(64, GFP_KERNEL);
	if(!s->interrupt_in_buffer) {
		err("kmalloc failed");
		goto reject;
	}

	/* The original value of '10' makes this interrupt fire off a LOT.
	   It was set so low because the callback determined when to
	   sumbit the buld read URB. I've lowered it to 100 - the driver
	   doesn't depend on that logic anymore. -EZA
	*/
	FILL_INT_URB(s->inturb, s->usbdev,
		     usb_rcvintpipe (s->usbdev, _PLUSB_INTPIPE),
		     s->interrupt_in_buffer, 1,
		     plusb_int_complete, s, HZ);

	dbg("inturb submission:");
	if(usb_submit_urb(s->inturb)<0) {
		err("usb_submit_urb failed");
		goto reject;
	}
	
	dbg("readurb init:");
	s->bulk_in_buffer = kmalloc(_BULK_DATA_LEN, GFP_KERNEL);
	if (!s->bulk_in_buffer) {
		err("kmalloc %d bytes for bulk in buffer failed", _BULK_DATA_LEN);
	}

	FILL_BULK_URB(s->readurb, s->usbdev,
		      usb_rcvbulkpipe(s->usbdev, _PLUSB_BULKINPIPE),
		      s->bulk_in_buffer, _BULK_DATA_LEN,
		      plusb_read_bulk_complete, s);

	/* The write urb will be initialized inside the network
	   interrupt.
	*/

	/* get the bulk read going */
	submit_read_urb("plusb_alloc", s);

	dbg ("plusb_alloc: finished. readurb=%p writeurb=%p inturb=%p",
		s->readurb, s->writeurb, s->inturb);
	
	return 0;

  reject:
  	dbg("plusb_alloc: failed");
	
	plusb_free_all(s);
	return -ENOMEM;
}

/*-------------------------------------------------------------------*/

static int plusb_net_open(struct net_device *dev)
{
	plusb_t *s=dev->priv;
	
	dbg("plusb_net_open");
	
	if(plusb_alloc(s))
		return -ENOMEM;

	s->opened=1;
	
	MOD_INC_USE_COUNT;

	netif_start_queue(dev);

	dbg("plusb_net_open: success");
	
	return 0;
	
}

/* --------------------------------------------------------------------- */

static int plusb_net_stop(struct net_device *dev)
{
	plusb_t *s=dev->priv;

	netif_stop_queue(dev);
	
	dbg("plusb_net_stop");	
	
	s->opened=0;
	plusb_free_all(s);

	MOD_DEC_USE_COUNT;
	dbg("plusb_net_stop:finished");
	return 0;
}

/* --------------------------------------------------------------------- */

static struct net_device_stats *plusb_net_get_stats(struct net_device *dev)
{
	plusb_t *s=dev->priv;
	
	dbg("net_device_stats");
	
	return &s->net_stats;
}

/* --------------------------------------------------------------------- */

static plusb_t *plusb_find_struct (void)
{
	int u;

	for (u = 0; u < NRPLUSB; u++) {
		plusb_t *s = &plusb[u];
		if (!s->connected)
			return s;
	}
	return NULL;
}

/* --------------------------------------------------------------------- */

static void plusb_disconnect (struct usb_device *usbdev, void *ptr)
{
	plusb_t *s = ptr;

	dbg("plusb_disconnect");
	
	plusb_free_all(s);

	if(!s->opened && s->net_dev.name) {
		dbg("unregistering netdev: %s",s->net_dev.name);
		unregister_netdev(&s->net_dev);
		s->net_dev.name[0] = '\0';
#if (LINUX_VERSION_CODE < 0x020300)
		dbg("plusb_disconnect: About to free name");
 		kfree (s->net_dev.name);
 		s->net_dev.name = NULL;
#endif	
	}
	
	dbg("plusb_disconnect: finished");
	MOD_DEC_USE_COUNT;
}

/* --------------------------------------------------------------------- */

static int plusb_change_mtu(struct net_device *dev, int new_mtu)
{
	if ((new_mtu < 68) || (new_mtu > _BULK_DATA_LEN))
		return -EINVAL;

	printk("plusb: changing mtu to %d\n", new_mtu);
	dev->mtu = new_mtu;
	
	/* NOTE: Could we change the size of the READ URB here dynamically
	   to save kernel memory?
	*/
	return 0;
}

/* --------------------------------------------------------------------- */

int plusb_net_init(struct net_device *dev)
{
	dbg("plusb_net_init");
	
	dev->open=plusb_net_open;
	dev->stop=plusb_net_stop;
	dev->hard_start_xmit=plusb_net_xmit;
	dev->get_stats	= plusb_net_get_stats;
	ether_setup(dev);
	dev->change_mtu = plusb_change_mtu;
	/* Setting the default MTU to 16K gives good performance for
	   me, and keeps the ping latency low too.  Setting it up
	   to 32K made performance go down. -EZA
	   Pavel says it would be best not to do this...
	*/
	/*dev->mtu=16384; */
	dev->tx_queue_len = 0;	
	dev->flags = IFF_POINTOPOINT|IFF_NOARP;

	
	dbg("plusb_net_init: finished");
	return 0;
}

/* --------------------------------------------------------------------- */

static void *plusb_probe (struct usb_device *usbdev, unsigned int ifnum, const struct usb_device_id *id)
{
	plusb_t *s;

	dbg("plusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
	  usbdev->descriptor.idVendor, usbdev->descriptor.idProduct, ifnum);

	if (usbdev->descriptor.idVendor != 0x067b || usbdev->descriptor.idProduct > 0x1)
		return NULL;

	/* We don't handle multiple configurations */
	if (usbdev->descriptor.bNumConfigurations != 1)
		return NULL;

	s = plusb_find_struct ();
	if (!s)
		return NULL;

	s->usbdev = usbdev;

	if (usb_set_configuration (s->usbdev, usbdev->config[0].bConfigurationValue) < 0) {
		err("set_configuration failed");
		return NULL;
	}

	if (usb_set_interface (s->usbdev, 0, 0) < 0) {
		err("set_interface failed");
		return NULL;
	}

#if (LINUX_VERSION_CODE < 0x020300)
 	{
 		int i;
		
 		/* For Kernel version 2.2, the driver is responsible for
 		   allocating this memory. For version 2.4, the rules
 		   have apparently changed, but there is a nifty function
 		   'init_netdev' that might make this easier...  It's in 
 		   ../net/net_init.c - but can we get there from here?  (no)
		   -EZA
 		*/
 		
 		/* Find the device number... we seem to have lost it... -EZA */
 		for (i=0; i<NRPLUSB; i++) {
 			if (&plusb[i] == s)
 				break;
 		}
 	
 		if(!s->net_dev.name) {
 			s->net_dev.name = kmalloc(strlen("plusbXXXX"), GFP_KERNEL);
 			sprintf (s->net_dev.name, "plusb%d", i);
 			s->net_dev.init=plusb_net_init;
 			s->net_dev.priv=s;
 			
 			printk ("plusb_probe: Registering Device\n");	
 			if(!register_netdev(&s->net_dev))
 				info("registered: %s", s->net_dev.name);
 			else {
 				err("register_netdev failed");
 				s->net_dev.name[0] = '\0';
 			}
 			dbg ("plusb_probe: Connected!");
 		}
 	}
#else
 	/* Kernel version 2.3+ works a little bit differently than 2.2 */
	if(!s->net_dev.name[0]) {
		strcpy(s->net_dev.name, "plusb%d");
		s->net_dev.init=plusb_net_init;
		s->net_dev.priv=s;
		if(!register_netdev(&s->net_dev))
			info("registered: %s", s->net_dev.name);
		else {
			err("register_netdev failed");
			s->net_dev.name[0] = '\0';
		}
	}
#endif
	
	s->connected = 1;

	if(s->opened) {
		dbg("net device already allocated, restarting USB transfers");
		plusb_alloc(s);
	}

	info("bound to interface: %d dev: %p", ifnum, usbdev);
	MOD_INC_USE_COUNT;
	return s;
}
/* --------------------------------------------------------------------- */

static struct usb_driver plusb_driver =
{
	name: "plusb",
	probe: plusb_probe,
	disconnect: plusb_disconnect,
};

/* --------------------------------------------------------------------- */

static int __init plusb_init (void)
{
	unsigned u;
	dbg("plusb_init");
	
	/* initialize struct */
	for (u = 0; u < NRPLUSB; u++) {
		plusb_t *s = &plusb[u];
		memset (s, 0, sizeof (plusb_t));
		INIT_LIST_HEAD (&s->tx_skb_list);
		INIT_LIST_HEAD (&s->free_skb_list);
		spin_lock_init (&s->lock);
	}

	/* register misc device */
	usb_register (&plusb_driver);

	dbg("plusb_init: driver registered");

	return 0;
}

/* --------------------------------------------------------------------- */

static void __exit plusb_cleanup (void)
{
	unsigned u;

	dbg("plusb_cleanup");
	for (u = 0; u < NRPLUSB; u++) {
		plusb_t *s = &plusb[u];
#if (LINUX_VERSION_CODE < 0x020300)
		if(s->net_dev.name) {
			dbg("unregistering netdev: %s",s->net_dev.name);
			unregister_netdev(&s->net_dev);
			s->net_dev.name[0] = '\0';
			kfree (s->net_dev.name);
			s->net_dev.name = NULL;		
		}		
#else
		if(s->net_dev.name[0]) {
			dbg("unregistering netdev: %s",s->net_dev.name);
			unregister_netdev(&s->net_dev);
			s->net_dev.name[0] = '\0';
		}
#endif
	}
	usb_deregister (&plusb_driver);
	dbg("plusb_cleanup: finished");
}

/* --------------------------------------------------------------------- */

MODULE_AUTHOR ("Deti Fliegl, deti@fliegl.de");
MODULE_DESCRIPTION ("PL-2302 USB Interface Driver for Linux (c)2000");


module_init (plusb_init);
module_exit (plusb_cleanup);

/* --------------------------------------------------------------------- */