aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/atmdev.h
blob: c1da539f5e28a965702c39d21375923e8271f67f (plain) (blame)
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
/* atmdev.h - ATM device driver declarations and various related items */
#ifndef LINUX_ATMDEV_H
#define LINUX_ATMDEV_H


#include <linux/wait.h> /* wait_queue_head_t */
#include <linux/time.h> /* struct timeval */
#include <linux/net.h>
#include <linux/bug.h>
#include <linux/skbuff.h> /* struct sk_buff */
#include <linux/uio.h>
#include <net/sock.h>
#include <linux/atomic.h>
#include <uapi/linux/atmdev.h>

#ifdef CONFIG_PROC_FS
#include <linux/proc_fs.h>

extern struct proc_dir_entry *atm_proc_root;
#endif

#ifdef CONFIG_COMPAT
#include <linux/compat.h>
struct compat_atm_iobuf {
	int length;
	compat_uptr_t buffer;
};
#endif

struct k_atm_aal_stats {
#define __HANDLE_ITEM(i) atomic_t i
	__AAL_STAT_ITEMS
#undef __HANDLE_ITEM
};


struct k_atm_dev_stats {
	struct k_atm_aal_stats aal0;
	struct k_atm_aal_stats aal34;
	struct k_atm_aal_stats aal5;
};

struct device;

enum {
	ATM_VF_ADDR,		/* Address is in use. Set by anybody, cleared
				   by device driver. */
	ATM_VF_READY,		/* VC is ready to transfer data. Set by device
				   driver, cleared by anybody. */
	ATM_VF_PARTIAL,		/* resources are bound to PVC (partial PVC
				   setup), controlled by socket layer */
	ATM_VF_REGIS,		/* registered with demon, controlled by SVC
				   socket layer */
	ATM_VF_BOUND,		/* local SAP is set, controlled by SVC socket
				   layer */
	ATM_VF_RELEASED,	/* demon has indicated/requested release,
				   controlled by SVC socket layer */
	ATM_VF_HASQOS,		/* QOS parameters have been set */
	ATM_VF_LISTEN,		/* socket is used for listening */
	ATM_VF_META,		/* SVC socket isn't used for normal data
				   traffic and doesn't depend on signaling
				   to be available */
	ATM_VF_SESSION,		/* VCC is p2mp session control descriptor */
	ATM_VF_HASSAP,		/* SAP has been set */
	ATM_VF_CLOSE,		/* asynchronous close - treat like VF_RELEASED*/
	ATM_VF_WAITING,		/* waiting for reply from sigd */
	ATM_VF_IS_CLIP,		/* in use by CLIP protocol */
};


#define ATM_VF2VS(flags) \
    (test_bit(ATM_VF_READY,&(flags)) ? ATM_VS_CONNECTED : \
     test_bit(ATM_VF_RELEASED,&(flags)) ? ATM_VS_CLOSING : \
     test_bit(ATM_VF_LISTEN,&(flags)) ? ATM_VS_LISTEN : \
     test_bit(ATM_VF_REGIS,&(flags)) ? ATM_VS_INUSE : \
     test_bit(ATM_VF_BOUND,&(flags)) ? ATM_VS_BOUND : ATM_VS_IDLE)


enum {
	ATM_DF_REMOVED,		/* device was removed from atm_devs list */
};


#define ATM_PHY_SIG_LOST    0	/* no carrier/light */
#define ATM_PHY_SIG_UNKNOWN 1	/* carrier/light status is unknown */
#define ATM_PHY_SIG_FOUND   2	/* carrier/light okay */

#define ATM_ATMOPT_CLP	1	/* set CLP bit */

struct atm_vcc {
	/* struct sock has to be the first member of atm_vcc */
	struct sock	sk;
	unsigned long	flags;		/* VCC flags (ATM_VF_*) */
	short		vpi;		/* VPI and VCI (types must be equal */
					/* with sockaddr) */
	int 		vci;
	unsigned long	aal_options;	/* AAL layer options */
	unsigned long	atm_options;	/* ATM layer options */
	struct atm_dev	*dev;		/* device back pointer */
	struct atm_qos	qos;		/* QOS */
	struct atm_sap	sap;		/* SAP */
	void (*release_cb)(struct atm_vcc *vcc); /* release_sock callback */
	void (*push)(struct atm_vcc *vcc,struct sk_buff *skb);
	void (*pop)(struct atm_vcc *vcc,struct sk_buff *skb); /* optional */
	int (*push_oam)(struct atm_vcc *vcc,void *cell);
	int (*send)(struct atm_vcc *vcc,struct sk_buff *skb);
	void		*dev_data;	/* per-device data */
	void		*proto_data;	/* per-protocol data */
	struct k_atm_aal_stats *stats;	/* pointer to AAL stats group */
	struct module *owner;		/* owner of ->push function */
	/* SVC part --- may move later ------------------------------------- */
	short		itf;		/* interface number */
	struct sockaddr_atmsvc local;
	struct sockaddr_atmsvc remote;
	/* Multipoint part ------------------------------------------------- */
	struct atm_vcc	*session;	/* session VCC descriptor */
	/* Other stuff ----------------------------------------------------- */
	void		*user_back;	/* user backlink - not touched by */
					/* native ATM stack. Currently used */
					/* by CLIP and sch_atm. */
};

static inline struct atm_vcc *atm_sk(struct sock *sk)
{
	return (struct atm_vcc *)sk;
}

static inline struct atm_vcc *ATM_SD(struct socket *sock)
{
	return atm_sk(sock->sk);
}

static inline struct sock *sk_atm(struct atm_vcc *vcc)
{
	return (struct sock *)vcc;
}

struct atm_dev_addr {
	struct sockaddr_atmsvc addr;	/* ATM address */
	struct list_head entry;		/* next address */
};

enum atm_addr_type_t { ATM_ADDR_LOCAL, ATM_ADDR_LECS };

struct atm_dev {
	const struct atmdev_ops *ops;	/* device operations; NULL if unused */
	const struct atmphy_ops *phy;	/* PHY operations, may be undefined */
					/* (NULL) */
	const char	*type;		/* device type name */
	int		number;		/* device index */
	void		*dev_data;	/* per-device data */
	void		*phy_data;	/* private PHY date */
	unsigned long	flags;		/* device flags (ATM_DF_*) */
	struct list_head local;		/* local ATM addresses */
	struct list_head lecs;		/* LECS ATM addresses learned via ILMI */
	unsigned char	esi[ESI_LEN];	/* ESI ("MAC" addr) */
	struct atm_cirange ci_range;	/* VPI/VCI range */
	struct k_atm_dev_stats stats;	/* statistics */
	char		signal;		/* signal status (ATM_PHY_SIG_*) */
	int		link_rate;	/* link rate (default: OC3) */
	atomic_t	refcnt;		/* reference count */
	spinlock_t	lock;		/* protect internal members */
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *proc_entry; /* proc entry */
	char *proc_name;		/* proc entry name */
#endif
	struct device class_dev;	/* sysfs device */
	struct list_head dev_list;	/* linkage */
};

 
/* OF: send_Oam Flags */

#define ATM_OF_IMMED  1		/* Attempt immediate delivery */
#define ATM_OF_INRATE 2		/* Attempt in-rate delivery */


/*
 * ioctl, getsockopt, and setsockopt are optional and can be set to NULL.
 */

struct atmdev_ops { /* only send is required */
	void (*dev_close)(struct atm_dev *dev);
	int (*open)(struct atm_vcc *vcc);
	void (*close)(struct atm_vcc *vcc);
	int (*ioctl)(struct atm_dev *dev,unsigned int cmd,void __user *arg);
#ifdef CONFIG_COMPAT
	int (*compat_ioctl)(struct atm_dev *dev,unsigned int cmd,
			    void __user *arg);
#endif
	int (*getsockopt)(struct atm_vcc *vcc,int level,int optname,
	    void __user *optval,int optlen);
	int (*setsockopt)(struct atm_vcc *vcc,int level,int optname,
	    void __user *optval,unsigned int optlen);
	int (*send)(struct atm_vcc *vcc,struct sk_buff *skb);
	int (*send_oam)(struct atm_vcc *vcc,void *cell,int flags);
	void (*phy_put)(struct atm_dev *dev,unsigned char value,
	    unsigned long addr);
	unsigned char (*phy_get)(struct atm_dev *dev,unsigned long addr);
	int (*change_qos)(struct atm_vcc *vcc,struct atm_qos *qos,int flags);
	int (*proc_read)(struct atm_dev *dev,loff_t *pos,char *page);
	struct module *owner;
};

struct atmphy_ops {
	int (*start)(struct atm_dev *dev);
	int (*ioctl)(struct atm_dev *dev,unsigned int cmd,void __user *arg);
	void (*interrupt)(struct atm_dev *dev);
	int (*stop)(struct atm_dev *dev);
};

struct atm_skb_data {
	struct atm_vcc	*vcc;		/* ATM VCC */
	unsigned long	atm_options;	/* ATM layer options */
};

#define VCC_HTABLE_SIZE 32

extern struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
extern rwlock_t vcc_sklist_lock;

#define ATM_SKB(skb) (((struct atm_skb_data *) (skb)->cb))

struct atm_dev *atm_dev_register(const char *type, struct device *parent,
				 const struct atmdev_ops *ops,
				 int number, /* -1 == pick first available */
				 unsigned long *flags);
struct atm_dev *atm_dev_lookup(int number);
void atm_dev_deregister(struct atm_dev *dev);

/* atm_dev_signal_change
 *
 * Propagate lower layer signal change in atm_dev->signal to netdevice.
 * The event will be sent via a notifier call chain.
 */
void atm_dev_signal_change(struct atm_dev *dev, char signal);

void vcc_insert_socket(struct sock *sk);

void atm_dev_release_vccs(struct atm_dev *dev);


static inline void atm_force_charge(struct atm_vcc *vcc,int truesize)
{
	atomic_add(truesize, &sk_atm(vcc)->sk_rmem_alloc);
}


static inline void atm_return(struct atm_vcc *vcc,int truesize)
{
	atomic_sub(truesize, &sk_atm(vcc)->sk_rmem_alloc);
}


static inline int atm_may_send(struct atm_vcc *vcc,unsigned int size)
{
	return (size + atomic_read(&sk_atm(vcc)->sk_wmem_alloc)) <
	       sk_atm(vcc)->sk_sndbuf;
}


static inline void atm_dev_hold(struct atm_dev *dev)
{
	atomic_inc(&dev->refcnt);
}


static inline void atm_dev_put(struct atm_dev *dev)
{
	if (atomic_dec_and_test(&dev->refcnt)) {
		BUG_ON(!test_bit(ATM_DF_REMOVED, &dev->flags));
		if (dev->ops->dev_close)
			dev->ops->dev_close(dev);
		put_device(&dev->class_dev);
	}
}


int atm_charge(struct atm_vcc *vcc,int truesize);
struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc,int pdu_size,
    gfp_t gfp_flags);
int atm_pcr_goal(const struct atm_trafprm *tp);

void vcc_release_async(struct atm_vcc *vcc, int reply);

struct atm_ioctl {
	struct module *owner;
	/* A module reference is kept if appropriate over this call.
	 * Return -ENOIOCTLCMD if you don't handle it. */
	int (*ioctl)(struct socket *, unsigned int cmd, unsigned long arg);
	struct list_head list;
};

/**
 * register_atm_ioctl - register handler for ioctl operations
 *
 * Special (non-device) handlers of ioctl's should
 * register here. If you're a normal device, you should
 * set .ioctl in your atmdev_ops instead.
 */
void register_atm_ioctl(struct atm_ioctl *);

/**
 * deregister_atm_ioctl - remove the ioctl handler
 */
void deregister_atm_ioctl(struct atm_ioctl *);


/* register_atmdevice_notifier - register atm_dev notify events
 *
 * Clients like br2684 will register notify events
 * Currently we notify of signal found/lost
 */
int register_atmdevice_notifier(struct notifier_block *nb);
void unregister_atmdevice_notifier(struct notifier_block *nb);

#endif
5 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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
















                                                                              
                                                                      
































                                                                              
                          
                          
                          





                                 
                                                                     
























































































                                                                                        






















                                                                             
                                                             















































































































































































                                                                               
                                                                  













                                                        
                                  
















































































































































































































                                                                         
                                      





































                                                                         
















































                                                                     


                                                          







































































                                                                        
                                        

                                                            
                                                  






                                                                            
                                       









































                                                                              

                                                      
 

                 







                                                   
                                         
































                                                                 
                                              



































































                                                                             



                                                              



































                                                               
                                   

















































































































                                                                               







































                                                                          

                                                                         






















                                                          



























                                                                    
/*****************************************************************************
*
* Filename:      stir4200.c
* Version:       0.4
* Description:   Irda SigmaTel USB Dongle
* Status:        Experimental
* Author:        Stephen Hemminger <shemminger@osdl.org>
*
*  	Based on earlier driver by Paul Stewart <stewart@parc.com>
*
*	Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
*	Copyright (C) 2001, Dag Brattli <dag@brattli.net>
*	Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
*	Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
*
*	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.
*
*	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 dongle does no framing, and requires polling to receive the
 * data.  The STIr4200 has bulk in and out endpoints just like
 * usr-irda devices, but the data it sends and receives is raw; like
 * irtty, it needs to call the wrap and unwrap functions to add and
 * remove SOF/BOF and escape characters to/from the frame.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/usb.h>
#include <linux/crc32.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <net/irda/irda.h>
#include <net/irda/irda_device.h>
#include <net/irda/wrapper.h>
#include <net/irda/crc.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>

MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
MODULE_LICENSE("GPL");

static int qos_mtt_bits = 0x07;	/* 1 ms or more */
module_param(qos_mtt_bits, int, 0);
MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");

static int rx_sensitivity = 1;	/* FIR 0..4, SIR 0..6 */
module_param(rx_sensitivity, int, 0);
MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");

static int tx_power = 0;	/* 0 = highest ... 3 = lowest */
module_param(tx_power, int, 0);
MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");

#define STIR_IRDA_HEADER  	4
#define CTRL_TIMEOUT		100	   /* milliseconds */
#define TRANSMIT_TIMEOUT	200	   /* milliseconds */
#define STIR_FIFO_SIZE		4096
#define FIFO_REGS_SIZE		3

enum FirChars {
	FIR_CE   = 0x7d,
	FIR_XBOF = 0x7f,
	FIR_EOF  = 0x7e,
};

enum StirRequests {
	REQ_WRITE_REG =		0x00,
	REQ_READ_REG =		0x01,
	REQ_READ_ROM =		0x02,
	REQ_WRITE_SINGLE =	0x03,
};

/* Register offsets */
enum StirRegs {
	REG_RSVD=0,
	REG_MODE,
	REG_PDCLK,
	REG_CTRL1,
	REG_CTRL2,
	REG_FIFOCTL,
	REG_FIFOLSB,
	REG_FIFOMSB,
	REG_DPLL,
	REG_IRDIG,
	REG_TEST=15,
};

enum StirModeMask {
	MODE_FIR = 0x80,
	MODE_SIR = 0x20,
	MODE_ASK = 0x10,
	MODE_FASTRX = 0x08,
	MODE_FFRSTEN = 0x04,
	MODE_NRESET = 0x02,
	MODE_2400 = 0x01,
};

enum StirPdclkMask {
	PDCLK_4000000 = 0x02,
	PDCLK_115200 = 0x09,
	PDCLK_57600 = 0x13,
	PDCLK_38400 = 0x1D,
	PDCLK_19200 = 0x3B,
	PDCLK_9600 = 0x77,
	PDCLK_2400 = 0xDF,
};

enum StirCtrl1Mask {
	CTRL1_SDMODE = 0x80,
	CTRL1_RXSLOW = 0x40,
	CTRL1_TXPWD = 0x10,
	CTRL1_RXPWD = 0x08,
	CTRL1_SRESET = 0x01,
};

enum StirCtrl2Mask {
	CTRL2_SPWIDTH = 0x08,
	CTRL2_REVID = 0x03,
};

enum StirFifoCtlMask {
	FIFOCTL_EOF = 0x80,
	FIFOCTL_UNDER = 0x40,
	FIFOCTL_OVER = 0x20,
	FIFOCTL_DIR = 0x10,
	FIFOCTL_CLR = 0x08,
	FIFOCTL_EMPTY = 0x04,
};

enum StirDiagMask {
	IRDIG_RXHIGH = 0x80,
	IRDIG_RXLOW = 0x40,
};

enum StirTestMask {
	TEST_PLLDOWN = 0x80,
	TEST_LOOPIR = 0x40,
	TEST_LOOPUSB = 0x20,
	TEST_TSTENA = 0x10,
	TEST_TSTOSC = 0x0F,
};

struct stir_cb {
        struct usb_device *usbdev;      /* init: probe_irda */
        struct net_device *netdev;      /* network layer */
        struct irlap_cb   *irlap;       /* The link layer we are binded to */
        struct net_device_stats stats;	/* network statistics */
        struct qos_info   qos;
	unsigned 	  speed;	/* Current speed */

        struct task_struct *thread;     /* transmit thread */

	struct sk_buff	  *tx_pending;
	void		  *io_buf;	/* transmit/receive buffer */
	__u8		  *fifo_status;

	iobuff_t  	  rx_buff;	/* receive unwrap state machine */
	struct timeval	  rx_time;
	int		  receiving;
	struct urb	 *rx_urb;
};


/* These are the currently known USB ids */
static struct usb_device_id dongles[] = {
    /* SigmaTel, Inc,  STIr4200 IrDA/USB Bridge */
    { USB_DEVICE(0x066f, 0x4200) },
    { }
};

MODULE_DEVICE_TABLE(usb, dongles);

/* Send control message to set dongle register */
static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
{
	struct usb_device *dev = stir->usbdev;

	pr_debug("%s: write reg %d = 0x%x\n",
		 stir->netdev->name, reg, value);
	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
			       REQ_WRITE_SINGLE,
			       USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
			       value, reg, NULL, 0,
			       CTRL_TIMEOUT);
}

/* Send control message to read multiple registers */
static inline int read_reg(struct stir_cb *stir, __u16 reg,
		    __u8 *data, __u16 count)
{
	struct usb_device *dev = stir->usbdev;

	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
			       REQ_READ_REG,
			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			       0, reg, data, count,
			       CTRL_TIMEOUT);
}

static inline int isfir(u32 speed)
{
	return (speed == 4000000);
}

/*
 * Prepare a FIR IrDA frame for transmission to the USB dongle.  The
 * FIR transmit frame is documented in the datasheet.  It consists of
 * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
 * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
 * then the data escaped as follows:
 *
 *    0x7D -> 0x7D 0x5D
 *    0x7E -> 0x7D 0x5E
 *    0x7F -> 0x7D 0x5F
 *
 * Then, 4 bytes of little endian (stuffed) FCS follow, then two
 * trailing EOF bytes of 0x7E.
 */
static inline __u8 *stuff_fir(__u8 *p, __u8 c)
{
	switch(c) {
	case 0x7d:
	case 0x7e:
	case 0x7f:
		*p++ = 0x7d;
		c ^= IRDA_TRANS;
		/* fall through */
	default:
		*p++ = c;
	}
	return p;
}

/* Take raw data in skb and put it wrapped into buf */
static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
{
	__u8 *ptr = buf;
	__u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
	__u16 wraplen;
	int i;

	/* Header */
	buf[0] = 0x55;
	buf[1] = 0xAA;

	ptr = buf + STIR_IRDA_HEADER;
	memset(ptr, 0x7f, 16);
	ptr += 16;

	/* BOF */
	*ptr++  = 0x7e;
	*ptr++  = 0x7e;

	/* Address / Control / Information */
	for (i = 0; i < skb->len; i++)
		ptr = stuff_fir(ptr, skb->data[i]);

	/* FCS */
	ptr = stuff_fir(ptr, fcs & 0xff);
	ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
	ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
	ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);

	/* EOFs */
	*ptr++ = 0x7e;
	*ptr++ = 0x7e;

	/* Total length, minus the header */
	wraplen = (ptr - buf) - STIR_IRDA_HEADER;
	buf[2] = wraplen & 0xff;
	buf[3] = (wraplen >> 8) & 0xff;

	return wraplen + STIR_IRDA_HEADER;
}

static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
{
	__u16 wraplen;

	wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
				 STIR_FIFO_SIZE - STIR_IRDA_HEADER);
	buf[0] = 0x55;
	buf[1] = 0xAA;
	buf[2] = wraplen & 0xff;
	buf[3] = (wraplen >> 8) & 0xff;

	return wraplen + STIR_IRDA_HEADER;
}

/*
 * Frame is fully formed in the rx_buff so check crc
 * and pass up to irlap
 * setup for next receive
 */
static void fir_eof(struct stir_cb *stir)
{
	iobuff_t *rx_buff = &stir->rx_buff;
	int len = rx_buff->len - 4;
	struct sk_buff *skb, *nskb;
	__u32 fcs;

	if (unlikely(len <= 0)) {
		pr_debug("%s: short frame len %d\n",
			 stir->netdev->name, len);

		++stir->stats.rx_errors;
		++stir->stats.rx_length_errors;
		return;
	}

	fcs = ~(crc32_le(~0, rx_buff->data, len));
	if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {
		pr_debug("crc error calc 0x%x len %d\n", fcs, len);
		stir->stats.rx_errors++;
		stir->stats.rx_crc_errors++;
		return;
	}

	/* if frame is short then just copy it */
	if (len < IRDA_RX_COPY_THRESHOLD) {
		nskb = dev_alloc_skb(len + 1);
		if (unlikely(!nskb)) {
			++stir->stats.rx_dropped;
			return;
		}
		skb_reserve(nskb, 1);
		skb = nskb;
		skb_copy_to_linear_data(nskb, rx_buff->data, len);
	} else {
		nskb = dev_alloc_skb(rx_buff->truesize);
		if (unlikely(!nskb)) {
			++stir->stats.rx_dropped;
			return;
		}
		skb_reserve(nskb, 1);
		skb = rx_buff->skb;
		rx_buff->skb = nskb;
		rx_buff->head = nskb->data;
	}

	skb_put(skb, len);

	skb_reset_mac_header(skb);
	skb->protocol = htons(ETH_P_IRDA);
	skb->dev = stir->netdev;

	netif_rx(skb);

	stir->stats.rx_packets++;
	stir->stats.rx_bytes += len;

	rx_buff->data = rx_buff->head;
	rx_buff->len = 0;
}

/* Unwrap FIR stuffed data and bump it to IrLAP */
static void stir_fir_chars(struct stir_cb *stir,
			    const __u8 *bytes, int len)
{
	iobuff_t *rx_buff = &stir->rx_buff;
	int	i;

	for (i = 0; i < len; i++) {
		__u8	byte = bytes[i];

		switch(rx_buff->state) {
		case OUTSIDE_FRAME:
			/* ignore garbage till start of frame */
			if (unlikely(byte != FIR_EOF))
				continue;
			/* Now receiving frame */
			rx_buff->state = BEGIN_FRAME;

			/* Time to initialize receive buffer */
			rx_buff->data = rx_buff->head;
			rx_buff->len = 0;
			continue;

		case LINK_ESCAPE:
			if (byte == FIR_EOF) {
				pr_debug("%s: got EOF after escape\n",
					 stir->netdev->name);
				goto frame_error;
			}
			rx_buff->state = INSIDE_FRAME;
			byte ^= IRDA_TRANS;
			break;

		case BEGIN_FRAME:
			/* ignore multiple BOF/EOF */
			if (byte == FIR_EOF)
				continue;
			rx_buff->state = INSIDE_FRAME;
			rx_buff->in_frame = TRUE;

			/* fall through */
		case INSIDE_FRAME:
			switch(byte) {
			case FIR_CE:
				rx_buff->state = LINK_ESCAPE;
				continue;
			case FIR_XBOF:
				/* 0x7f is not used in this framing */
				pr_debug("%s: got XBOF without escape\n",
					 stir->netdev->name);
				goto frame_error;
			case FIR_EOF:
				rx_buff->state = OUTSIDE_FRAME;
				rx_buff->in_frame = FALSE;
				fir_eof(stir);
				continue;
			}
			break;
		}

		/* add byte to rx buffer */
		if (unlikely(rx_buff->len >= rx_buff->truesize)) {
			pr_debug("%s: fir frame exceeds %d\n",
				 stir->netdev->name, rx_buff->truesize);
			++stir->stats.rx_over_errors;
			goto error_recovery;
		}

		rx_buff->data[rx_buff->len++] = byte;
		continue;

	frame_error:
		++stir->stats.rx_frame_errors;

	error_recovery:
		++stir->stats.rx_errors;
		rx_buff->state = OUTSIDE_FRAME;
		rx_buff->in_frame = FALSE;
	}
}

/* Unwrap SIR stuffed data and bump it up to IrLAP */
static void stir_sir_chars(struct stir_cb *stir,
			    const __u8 *bytes, int len)
{
	int i;

	for (i = 0; i < len; i++)
		async_unwrap_char(stir->netdev, &stir->stats,
				  &stir->rx_buff, bytes[i]);
}

static inline void unwrap_chars(struct stir_cb *stir,
				const __u8 *bytes, int length)
{
	if (isfir(stir->speed))
		stir_fir_chars(stir, bytes, length);
	else
		stir_sir_chars(stir, bytes, length);
}

/* Mode parameters for each speed */
static const struct {
	unsigned speed;
	__u8 pdclk;
} stir_modes[] = {
        { 2400,    PDCLK_2400 },
        { 9600,    PDCLK_9600 },
        { 19200,   PDCLK_19200 },
        { 38400,   PDCLK_38400 },
        { 57600,   PDCLK_57600 },
        { 115200,  PDCLK_115200 },
        { 4000000, PDCLK_4000000 },
};


/*
 * Setup chip for speed.
 *  Called at startup to initialize the chip
 *  and on speed changes.
 *
 * Note: Write multiple registers doesn't appear to work
 */
static int change_speed(struct stir_cb *stir, unsigned speed)
{
	int i, err;
	__u8 mode;

	for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
		if (speed == stir_modes[i].speed)
			goto found;
	}

	warn("%s: invalid speed %d", stir->netdev->name, speed);
	return -EINVAL;

 found:
	pr_debug("speed change from %d to %d\n", stir->speed, speed);

	/* Reset modulator */
	err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
	if (err)
		goto out;

	/* Undocumented magic to tweak the DPLL */
	err = write_reg(stir, REG_DPLL, 0x15);
	if (err)
		goto out;

	/* Set clock */
	err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
	if (err)
		goto out;

	mode = MODE_NRESET | MODE_FASTRX;
	if (isfir(speed))
		mode |= MODE_FIR | MODE_FFRSTEN;
	else
		mode |= MODE_SIR;

	if (speed == 2400)
		mode |= MODE_2400;

	err = write_reg(stir, REG_MODE, mode);
	if (err)
		goto out;

	/* This resets TEMIC style transceiver if any. */
	err = write_reg(stir, REG_CTRL1,
			CTRL1_SDMODE | (tx_power & 3) << 1);
	if (err)
		goto out;

	err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
	if (err)
		goto out;

	/* Reset sensitivity */
	err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
 out:
	stir->speed = speed;
	return err;
}

/*
 * Called from net/core when new frame is available.
 */
static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
{
	struct stir_cb *stir = netdev_priv(netdev);

	netif_stop_queue(netdev);

	/* the IRDA wrapping routines don't deal with non linear skb */
	SKB_LINEAR_ASSERT(skb);

	skb = xchg(&stir->tx_pending, skb);
        wake_up_process(stir->thread);
	
	/* this should never happen unless stop/wakeup problem */
	if (unlikely(skb)) {
		WARN_ON(1);
		dev_kfree_skb(skb);
	}

	return 0;
}

/*
 * Wait for the transmit FIFO to have space for next data
 *
 * If space < 0 then wait till FIFO completely drains.
 * FYI: can take up to 13 seconds at 2400baud.
 */
static int fifo_txwait(struct stir_cb *stir, int space)
{
	int err;
	unsigned long count, status;

	/* Read FIFO status and count */
	for(;;) {
		err = read_reg(stir, REG_FIFOCTL, stir->fifo_status, 
				   FIFO_REGS_SIZE);
		if (unlikely(err != FIFO_REGS_SIZE)) {
			warn("%s: FIFO register read error: %d", 
			     stir->netdev->name, err);

			return err;
		}

		status = stir->fifo_status[0];
		count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8 
			| stir->fifo_status[1];

		pr_debug("fifo status 0x%lx count %lu\n", status, count);

		/* is fifo receiving already, or empty */
		if (!(status & FIFOCTL_DIR)
		    || (status & FIFOCTL_EMPTY))
			return 0;

		if (signal_pending(current))
			return -EINTR;

		/* shutting down? */
		if (!netif_running(stir->netdev)
		    || !netif_device_present(stir->netdev))
			return -ESHUTDOWN;

		/* only waiting for some space */
		if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
			return 0;

		/* estimate transfer time for remaining chars */
		msleep((count * 8000) / stir->speed);
	}
			
	err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
	if (err) 
		return err;
	err = write_reg(stir, REG_FIFOCTL, 0);
	if (err)
		return err;

	return 0;
}


/* Wait for turnaround delay before starting transmit.  */
static void turnaround_delay(const struct stir_cb *stir, long us)
{
	long ticks;
	struct timeval now;

	if (us <= 0)
		return;

	do_gettimeofday(&now);
	if (now.tv_sec - stir->rx_time.tv_sec > 0)
		us -= USEC_PER_SEC;
	us -= now.tv_usec - stir->rx_time.tv_usec;
	if (us < 10)
		return;

	ticks = us / (1000000 / HZ);
	if (ticks > 0)
		schedule_timeout_interruptible(1 + ticks);
	else
		udelay(us);
}

/*
 * Start receiver by submitting a request to the receive pipe.
 * If nothing is available it will return after rx_interval.
 */
static int receive_start(struct stir_cb *stir)
{
	/* reset state */
	stir->receiving = 1;

	stir->rx_buff.in_frame = FALSE;
	stir->rx_buff.state = OUTSIDE_FRAME;

	stir->rx_urb->status = 0;
	return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
}

/* Stop all pending receive Urb's */
static void receive_stop(struct stir_cb *stir)
{
	stir->receiving = 0;
	usb_kill_urb(stir->rx_urb);

	if (stir->rx_buff.in_frame) 
		stir->stats.collisions++;
}
/*
 * Wrap data in socket buffer and send it.
 */
static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
{
	unsigned wraplen;
	int first_frame = 0;

	/* if receiving, need to turnaround */
	if (stir->receiving) {
		receive_stop(stir);
		turnaround_delay(stir, irda_get_mtt(skb));
		first_frame = 1;
	}

	if (isfir(stir->speed))
		wraplen = wrap_fir_skb(skb, stir->io_buf);
	else
		wraplen = wrap_sir_skb(skb, stir->io_buf);
		
	/* check for space available in fifo */
	if (!first_frame)
		fifo_txwait(stir, wraplen);

	stir->stats.tx_packets++;
	stir->stats.tx_bytes += skb->len;
	stir->netdev->trans_start = jiffies;
	pr_debug("send %d (%d)\n", skb->len, wraplen);

	if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
			 stir->io_buf, wraplen,
			 NULL, TRANSMIT_TIMEOUT))
		stir->stats.tx_errors++;
}

/*
 * Transmit state machine thread
 */
static int stir_transmit_thread(void *arg)
{
	struct stir_cb *stir = arg;
	struct net_device *dev = stir->netdev;
	struct sk_buff *skb;

        while (!kthread_should_stop()) {
#ifdef CONFIG_PM
		/* if suspending, then power off and wait */
		if (unlikely(freezing(current))) {
			if (stir->receiving)
				receive_stop(stir);
			else
				fifo_txwait(stir, -1);

			write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);

			refrigerator();

			if (change_speed(stir, stir->speed))
				break;
		}
#endif

		/* if something to send? */
		skb = xchg(&stir->tx_pending, NULL);
		if (skb) {
			unsigned new_speed = irda_get_next_speed(skb);
			netif_wake_queue(dev);

			if (skb->len > 0)
				stir_send(stir, skb);
			dev_kfree_skb(skb);

			if ((new_speed != -1) && (stir->speed != new_speed)) {
				if (fifo_txwait(stir, -1) ||
				    change_speed(stir, new_speed))
					break;
			}
			continue;
		}

		/* nothing to send? start receiving */
		if (!stir->receiving 
		    && irda_device_txqueue_empty(dev)) {
			/* Wait otherwise chip gets confused. */
			if (fifo_txwait(stir, -1))
				break;

			if (unlikely(receive_start(stir))) {
				if (net_ratelimit())
					info("%s: receive usb submit failed",
					     stir->netdev->name);
				stir->receiving = 0;
				msleep(10);
				continue;
			}
		}

		/* sleep if nothing to send */
                set_current_state(TASK_INTERRUPTIBLE);
                schedule();

	}
        return 0;
}


/*
 * USB bulk receive completion callback.
 * Wakes up every ms (usb round trip) with wrapped 
 * data.
 */
static void stir_rcv_irq(struct urb *urb)
{
	struct stir_cb *stir = urb->context;
	int err;

	/* in process of stopping, just drop data */
	if (!netif_running(stir->netdev))
		return;

	/* unlink, shutdown, unplug, other nasties */
	if (urb->status != 0) 
		return;

	if (urb->actual_length > 0) {
		pr_debug("receive %d\n", urb->actual_length);
		unwrap_chars(stir, urb->transfer_buffer,
			     urb->actual_length);
		
		stir->netdev->last_rx = jiffies;
		do_gettimeofday(&stir->rx_time);
	}

	/* kernel thread is stopping receiver don't resubmit */
	if (!stir->receiving)
		return;

	/* resubmit existing urb */
	err = usb_submit_urb(urb, GFP_ATOMIC);

	/* in case of error, the kernel thread will restart us */
	if (err) {
		warn("%s: usb receive submit error: %d",
			stir->netdev->name, err);
		stir->receiving = 0;
		wake_up_process(stir->thread);
	}
}

/*
 * Function stir_net_open (dev)
 *
 *    Network device is taken up. Usually this is done by "ifconfig irda0 up"
 */
static int stir_net_open(struct net_device *netdev)
{
	struct stir_cb *stir = netdev_priv(netdev);
	int err;
	char hwname[16];

	err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
	if (err)
		goto err_out1;
	err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
	if (err)
		goto err_out1;

	err = change_speed(stir, 9600);
	if (err)
		goto err_out1;

	err = -ENOMEM;

	/* Initialize for SIR/FIR to copy data directly into skb.  */
	stir->receiving = 0;
	stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
	stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
	if (!stir->rx_buff.skb) 
		goto err_out1;

	skb_reserve(stir->rx_buff.skb, 1);
	stir->rx_buff.head = stir->rx_buff.skb->data;
	do_gettimeofday(&stir->rx_time);

	stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!stir->rx_urb) 
		goto err_out2;

	stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
	if (!stir->io_buf)
		goto err_out3;

	usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
			  usb_rcvbulkpipe(stir->usbdev, 2),
			  stir->io_buf, STIR_FIFO_SIZE,
			  stir_rcv_irq, stir);

	stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
	if (!stir->fifo_status) 
		goto err_out4;
		
	/*
	 * Now that everything should be initialized properly,
	 * Open new IrLAP layer instance to take care of us...
	 * Note : will send immediately a speed change...
	 */
	sprintf(hwname, "usb#%d", stir->usbdev->devnum);
	stir->irlap = irlap_open(netdev, &stir->qos, hwname);
	if (!stir->irlap) {
		err("stir4200: irlap_open failed");
		goto err_out5;
	}

	/** Start kernel thread for transmit.  */
	stir->thread = kthread_run(stir_transmit_thread, stir,
				   "%s", stir->netdev->name);
        if (IS_ERR(stir->thread)) {
                err = PTR_ERR(stir->thread);
		err("stir4200: unable to start kernel thread");
		goto err_out6;
	}

	netif_start_queue(netdev);

	return 0;

 err_out6:
	irlap_close(stir->irlap);
 err_out5:
	kfree(stir->fifo_status);
 err_out4:
	kfree(stir->io_buf);
 err_out3:
	usb_free_urb(stir->rx_urb);
 err_out2:
	kfree_skb(stir->rx_buff.skb);
 err_out1:
	return err;
}

/*
 * Function stir_net_close (stir)
 *
 *    Network device is taken down. Usually this is done by
 *    "ifconfig irda0 down"
 */
static int stir_net_close(struct net_device *netdev)
{
	struct stir_cb *stir = netdev_priv(netdev);

	/* Stop transmit processing */
	netif_stop_queue(netdev);

	/* Kill transmit thread */
	kthread_stop(stir->thread);
	kfree(stir->fifo_status);

	/* Mop up receive urb's */
	usb_kill_urb(stir->rx_urb);
	
	kfree(stir->io_buf);
	usb_free_urb(stir->rx_urb);
	kfree_skb(stir->rx_buff.skb);

	/* Stop and remove instance of IrLAP */
	if (stir->irlap)
		irlap_close(stir->irlap);

	stir->irlap = NULL;

	return 0;
}

/*
 * IOCTLs : Extra out-of-band network commands...
 */
static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
{
	struct if_irda_req *irq = (struct if_irda_req *) rq;
	struct stir_cb *stir = netdev_priv(netdev);
	int ret = 0;

	switch (cmd) {
	case SIOCSBANDWIDTH: /* Set bandwidth */
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;

		/* Check if the device is still there */
		if (netif_device_present(stir->netdev))
			ret = change_speed(stir, irq->ifr_baudrate);
		break;

	case SIOCSMEDIABUSY: /* Set media busy */
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;

		/* Check if the IrDA stack is still there */
		if (netif_running(stir->netdev))
			irda_device_set_media_busy(stir->netdev, TRUE);
		break;

	case SIOCGRECEIVING:
		/* Only approximately true */
		irq->ifr_receiving = stir->receiving;
		break;

	default:
		ret = -EOPNOTSUPP;
	}

	return ret;
}

/*
 * Get device stats (for /proc/net/dev and ifconfig)
 */
static struct net_device_stats *stir_net_get_stats(struct net_device *netdev)
{
	struct stir_cb *stir = netdev_priv(netdev);
	return &stir->stats;
}

/*
 * This routine is called by the USB subsystem for each new device
 * in the system. We need to check if the device is ours, and in
 * this case start handling it.
 * Note : it might be worth protecting this function by a global
 * spinlock... Or not, because maybe USB already deal with that...
 */
static int stir_probe(struct usb_interface *intf,
		      const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct stir_cb *stir = NULL;
	struct net_device *net;
	int ret = -ENOMEM;

	/* Allocate network device container. */
	net = alloc_irdadev(sizeof(*stir));
	if(!net)
		goto err_out1;

	SET_MODULE_OWNER(net);
	SET_NETDEV_DEV(net, &intf->dev);
	stir = netdev_priv(net);
	stir->netdev = net;
	stir->usbdev = dev;

	ret = usb_reset_configuration(dev);
	if (ret != 0) {
		err("stir4200: usb reset configuration failed");
		goto err_out2;
	}

	printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
		"Vendor: %x, Product: %x\n",
	       dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
	       le16_to_cpu(dev->descriptor.idProduct));

	/* Initialize QoS for this device */
	irda_init_max_qos_capabilies(&stir->qos);

	/* That's the Rx capability. */
	stir->qos.baud_rate.bits       &= IR_2400 | IR_9600 | IR_19200 |
					 IR_38400 | IR_57600 | IR_115200 |
					 (IR_4000000 << 8);
	stir->qos.min_turn_time.bits   &= qos_mtt_bits;
	irda_qos_bits_to_value(&stir->qos);

	/* Override the network functions we need to use */
	net->hard_start_xmit = stir_hard_xmit;
	net->open            = stir_net_open;
	net->stop            = stir_net_close;
	net->get_stats	     = stir_net_get_stats;
	net->do_ioctl        = stir_net_ioctl;

	ret = register_netdev(net);
	if (ret != 0)
		goto err_out2;

	info("IrDA: Registered SigmaTel device %s", net->name);

	usb_set_intfdata(intf, stir);

	return 0;

err_out2:
	free_netdev(net);
err_out1:
	return ret;
}

/*
 * The current device is removed, the USB layer tell us to shut it down...
 */
static void stir_disconnect(struct usb_interface *intf)
{
	struct stir_cb *stir = usb_get_intfdata(intf);

	if (!stir)
		return;

	unregister_netdev(stir->netdev);
	free_netdev(stir->netdev);

	usb_set_intfdata(intf, NULL);
}

#ifdef CONFIG_PM
/* USB suspend, so power off the transmitter/receiver */
static int stir_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct stir_cb *stir = usb_get_intfdata(intf);

	netif_device_detach(stir->netdev);
	return 0;
}

/* Coming out of suspend, so reset hardware */
static int stir_resume(struct usb_interface *intf)
{
	struct stir_cb *stir = usb_get_intfdata(intf);

	netif_device_attach(stir->netdev);

	/* receiver restarted when send thread wakes up */
	return 0;
}
#endif

/*
 * USB device callbacks
 */
static struct usb_driver irda_driver = {
	.name		= "stir4200",
	.probe		= stir_probe,
	.disconnect	= stir_disconnect,
	.id_table	= dongles,
#ifdef CONFIG_PM
	.suspend	= stir_suspend,
	.resume		= stir_resume,
#endif
};

/*
 * Module insertion
 */
static int __init stir_init(void)
{
	return usb_register(&irda_driver);
}
module_init(stir_init);

/*
 * Module removal
 */
static void __exit stir_cleanup(void)
{
	/* Deregister the driver and remove all pending instances */
	usb_deregister(&irda_driver);
}
module_exit(stir_cleanup);