1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	Driver for ZyDAS zd1201 based wireless USB devices.
4  *
5  *	Copyright (c) 2004, 2005 Jeroen Vreeken (pe1rxq@amsat.org)
6  *
7  *	Parts of this driver have been derived from a wlan-ng version
8  *	modified by ZyDAS. They also made documentation available, thanks!
9  *	Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/slab.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/wireless.h>
18 #include <net/cfg80211.h>
19 #include <net/iw_handler.h>
20 #include <linux/string.h>
21 #include <linux/if_arp.h>
22 #include <linux/firmware.h>
23 #include "zd1201.h"
24 
25 static const struct usb_device_id zd1201_table[] = {
26 	{USB_DEVICE(0x0586, 0x3400)}, /* Peabird Wireless USB Adapter */
27 	{USB_DEVICE(0x0ace, 0x1201)}, /* ZyDAS ZD1201 Wireless USB Adapter */
28 	{USB_DEVICE(0x050d, 0x6051)}, /* Belkin F5D6051 usb  adapter */
29 	{USB_DEVICE(0x0db0, 0x6823)}, /* MSI UB11B usb  adapter */
30 	{USB_DEVICE(0x1044, 0x8004)}, /* Gigabyte GN-WLBZ101 */
31 	{USB_DEVICE(0x1044, 0x8005)}, /* GIGABYTE GN-WLBZ201 usb adapter */
32 	{}
33 };
34 
35 static int ap;	/* Are we an AP or a normal station? */
36 
37 #define ZD1201_VERSION	"0.15"
38 
39 MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
40 MODULE_DESCRIPTION("Driver for ZyDAS ZD1201 based USB Wireless adapters");
41 MODULE_VERSION(ZD1201_VERSION);
42 MODULE_LICENSE("GPL");
43 module_param(ap, int, 0);
44 MODULE_PARM_DESC(ap, "If non-zero Access Point firmware will be loaded");
45 MODULE_DEVICE_TABLE(usb, zd1201_table);
46 
47 
48 static int zd1201_fw_upload(struct usb_device *dev, int apfw)
49 {
50 	const struct firmware *fw_entry;
51 	const char *data;
52 	unsigned long len;
53 	int err;
54 	unsigned char ret;
55 	char *buf;
56 	char *fwfile;
57 
58 	if (apfw)
59 		fwfile = "zd1201-ap.fw";
60 	else
61 		fwfile = "zd1201.fw";
62 
63 	err = request_firmware(&fw_entry, fwfile, &dev->dev);
64 	if (err) {
65 		dev_err(&dev->dev, "Failed to load %s firmware file!\n", fwfile);
66 		dev_err(&dev->dev, "Make sure the hotplug firmware loader is installed.\n");
67 		dev_err(&dev->dev, "Goto http://linux-lc100020.sourceforge.net for more info.\n");
68 		return err;
69 	}
70 
71 	data = fw_entry->data;
72         len = fw_entry->size;
73 
74 	buf = kmalloc(1024, GFP_ATOMIC);
75 	if (!buf) {
76 		err = -ENOMEM;
77 		goto exit;
78 	}
79 
80 	while (len > 0) {
81 		int translen = (len > 1024) ? 1024 : len;
82 		memcpy(buf, data, translen);
83 
84 		err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0,
85 		    USB_DIR_OUT | 0x40, 0, 0, buf, translen,
86 		    ZD1201_FW_TIMEOUT);
87 		if (err < 0)
88 			goto exit;
89 
90 		len -= translen;
91 		data += translen;
92 	}
93 
94 	err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x2,
95 	    USB_DIR_OUT | 0x40, 0, 0, NULL, 0, ZD1201_FW_TIMEOUT);
96 	if (err < 0)
97 		goto exit;
98 
99 	err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x4,
100 	    USB_DIR_IN | 0x40, 0, 0, buf, sizeof(ret), ZD1201_FW_TIMEOUT);
101 	if (err < 0)
102 		goto exit;
103 
104 	memcpy(&ret, buf, sizeof(ret));
105 
106 	if (ret & 0x80) {
107 		err = -EIO;
108 		goto exit;
109 	}
110 
111 	err = 0;
112 exit:
113 	kfree(buf);
114 	release_firmware(fw_entry);
115 	return err;
116 }
117 
118 MODULE_FIRMWARE("zd1201-ap.fw");
119 MODULE_FIRMWARE("zd1201.fw");
120 
121 static void zd1201_usbfree(struct urb *urb)
122 {
123 	struct zd1201 *zd = urb->context;
124 
125 	switch(urb->status) {
126 		case -EILSEQ:
127 		case -ENODEV:
128 		case -ETIME:
129 		case -ENOENT:
130 		case -EPIPE:
131 		case -EOVERFLOW:
132 		case -ESHUTDOWN:
133 			dev_warn(&zd->usb->dev, "%s: urb failed: %d\n",
134 			    zd->dev->name, urb->status);
135 	}
136 
137 	kfree(urb->transfer_buffer);
138 	usb_free_urb(urb);
139 }
140 
141 /* cmdreq message:
142 	u32 type
143 	u16 cmd
144 	u16 parm0
145 	u16 parm1
146 	u16 parm2
147 	u8  pad[4]
148 
149 	total: 4 + 2 + 2 + 2 + 2 + 4 = 16
150 */
151 static int zd1201_docmd(struct zd1201 *zd, int cmd, int parm0,
152 			int parm1, int parm2)
153 {
154 	unsigned char *command;
155 	int ret;
156 	struct urb *urb;
157 
158 	command = kmalloc(16, GFP_ATOMIC);
159 	if (!command)
160 		return -ENOMEM;
161 
162 	*((__le32*)command) = cpu_to_le32(ZD1201_USB_CMDREQ);
163 	*((__le16*)&command[4]) = cpu_to_le16(cmd);
164 	*((__le16*)&command[6]) = cpu_to_le16(parm0);
165 	*((__le16*)&command[8]) = cpu_to_le16(parm1);
166 	*((__le16*)&command[10])= cpu_to_le16(parm2);
167 
168 	urb = usb_alloc_urb(0, GFP_ATOMIC);
169 	if (!urb) {
170 		kfree(command);
171 		return -ENOMEM;
172 	}
173 	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
174 			  command, 16, zd1201_usbfree, zd);
175 	ret = usb_submit_urb(urb, GFP_ATOMIC);
176 	if (ret) {
177 		kfree(command);
178 		usb_free_urb(urb);
179 	}
180 
181 	return ret;
182 }
183 
184 /* Callback after sending out a packet */
185 static void zd1201_usbtx(struct urb *urb)
186 {
187 	struct zd1201 *zd = urb->context;
188 	netif_wake_queue(zd->dev);
189 }
190 
191 /* Incoming data */
192 static void zd1201_usbrx(struct urb *urb)
193 {
194 	struct zd1201 *zd = urb->context;
195 	int free = 0;
196 	unsigned char *data = urb->transfer_buffer;
197 	struct sk_buff *skb;
198 	unsigned char type;
199 
200 	if (!zd)
201 		return;
202 
203 	switch(urb->status) {
204 		case -EILSEQ:
205 		case -ENODEV:
206 		case -ETIME:
207 		case -ENOENT:
208 		case -EPIPE:
209 		case -EOVERFLOW:
210 		case -ESHUTDOWN:
211 			dev_warn(&zd->usb->dev, "%s: rx urb failed: %d\n",
212 			    zd->dev->name, urb->status);
213 			free = 1;
214 			goto exit;
215 	}
216 
217 	if (urb->status != 0 || urb->actual_length == 0)
218 		goto resubmit;
219 
220 	type = data[0];
221 	if (type == ZD1201_PACKET_EVENTSTAT || type == ZD1201_PACKET_RESOURCE) {
222 		memcpy(zd->rxdata, data, urb->actual_length);
223 		zd->rxlen = urb->actual_length;
224 		zd->rxdatas = 1;
225 		wake_up(&zd->rxdataq);
226 	}
227 	/* Info frame */
228 	if (type == ZD1201_PACKET_INQUIRE) {
229 		int i = 0;
230 		unsigned short infotype, copylen;
231 		infotype = le16_to_cpu(*(__le16*)&data[6]);
232 
233 		if (infotype == ZD1201_INF_LINKSTATUS) {
234 			short linkstatus;
235 
236 			linkstatus = le16_to_cpu(*(__le16*)&data[8]);
237 			switch(linkstatus) {
238 				case 1:
239 					netif_carrier_on(zd->dev);
240 					break;
241 				case 2:
242 					netif_carrier_off(zd->dev);
243 					break;
244 				case 3:
245 					netif_carrier_off(zd->dev);
246 					break;
247 				case 4:
248 					netif_carrier_on(zd->dev);
249 					break;
250 				default:
251 					netif_carrier_off(zd->dev);
252 			}
253 			goto resubmit;
254 		}
255 		if (infotype == ZD1201_INF_ASSOCSTATUS) {
256 			short status = le16_to_cpu(*(__le16*)(data+8));
257 			int event;
258 			union iwreq_data wrqu;
259 
260 			switch (status) {
261 				case ZD1201_ASSOCSTATUS_STAASSOC:
262 				case ZD1201_ASSOCSTATUS_REASSOC:
263 					event = IWEVREGISTERED;
264 					break;
265 				case ZD1201_ASSOCSTATUS_DISASSOC:
266 				case ZD1201_ASSOCSTATUS_ASSOCFAIL:
267 				case ZD1201_ASSOCSTATUS_AUTHFAIL:
268 				default:
269 					event = IWEVEXPIRED;
270 			}
271 			memcpy(wrqu.addr.sa_data, data+10, ETH_ALEN);
272 			wrqu.addr.sa_family = ARPHRD_ETHER;
273 
274 			/* Send event to user space */
275 			wireless_send_event(zd->dev, event, &wrqu, NULL);
276 
277 			goto resubmit;
278 		}
279 		if (infotype == ZD1201_INF_AUTHREQ) {
280 			union iwreq_data wrqu;
281 
282 			memcpy(wrqu.addr.sa_data, data+8, ETH_ALEN);
283 			wrqu.addr.sa_family = ARPHRD_ETHER;
284 			/* There isn't a event that trully fits this request.
285 			   We assume that userspace will be smart enough to
286 			   see a new station being expired and sends back a
287 			   authstation ioctl to authorize it. */
288 			wireless_send_event(zd->dev, IWEVEXPIRED, &wrqu, NULL);
289 			goto resubmit;
290 		}
291 		/* Other infotypes are handled outside this handler */
292 		zd->rxlen = 0;
293 		while (i < urb->actual_length) {
294 			copylen = le16_to_cpu(*(__le16*)&data[i+2]);
295 			/* Sanity check, sometimes we get junk */
296 			if (copylen+zd->rxlen > sizeof(zd->rxdata))
297 				break;
298 			memcpy(zd->rxdata+zd->rxlen, data+i+4, copylen);
299 			zd->rxlen += copylen;
300 			i += 64;
301 		}
302 		if (i >= urb->actual_length) {
303 			zd->rxdatas = 1;
304 			wake_up(&zd->rxdataq);
305 		}
306 		goto  resubmit;
307 	}
308 	/* Actual data */
309 	if (data[urb->actual_length-1] == ZD1201_PACKET_RXDATA) {
310 		int datalen = urb->actual_length-1;
311 		unsigned short len, fc, seq;
312 
313 		len = ntohs(*(__be16 *)&data[datalen-2]);
314 		if (len>datalen)
315 			len=datalen;
316 		fc = le16_to_cpu(*(__le16 *)&data[datalen-16]);
317 		seq = le16_to_cpu(*(__le16 *)&data[datalen-24]);
318 
319 		if (zd->monitor) {
320 			if (datalen < 24)
321 				goto resubmit;
322 			if (!(skb = dev_alloc_skb(datalen+24)))
323 				goto resubmit;
324 
325 			skb_put_data(skb, &data[datalen - 16], 2);
326 			skb_put_data(skb, &data[datalen - 2], 2);
327 			skb_put_data(skb, &data[datalen - 14], 6);
328 			skb_put_data(skb, &data[datalen - 22], 6);
329 			skb_put_data(skb, &data[datalen - 8], 6);
330 			skb_put_data(skb, &data[datalen - 24], 2);
331 			skb_put_data(skb, data, len);
332 			skb->protocol = eth_type_trans(skb, zd->dev);
333 			zd->dev->stats.rx_packets++;
334 			zd->dev->stats.rx_bytes += skb->len;
335 			netif_rx(skb);
336 			goto resubmit;
337 		}
338 
339 		if ((seq & IEEE80211_SCTL_FRAG) ||
340 		    (fc & IEEE80211_FCTL_MOREFRAGS)) {
341 			struct zd1201_frag *frag = NULL;
342 			char *ptr;
343 
344 			if (datalen<14)
345 				goto resubmit;
346 			if ((seq & IEEE80211_SCTL_FRAG) == 0) {
347 				frag = kmalloc(sizeof(*frag), GFP_ATOMIC);
348 				if (!frag)
349 					goto resubmit;
350 				skb = dev_alloc_skb(IEEE80211_MAX_DATA_LEN +14+2);
351 				if (!skb) {
352 					kfree(frag);
353 					goto resubmit;
354 				}
355 				frag->skb = skb;
356 				frag->seq = seq & IEEE80211_SCTL_SEQ;
357 				skb_reserve(skb, 2);
358 				skb_put_data(skb, &data[datalen - 14], 12);
359 				skb_put_data(skb, &data[6], 2);
360 				skb_put_data(skb, data + 8, len);
361 				hlist_add_head(&frag->fnode, &zd->fraglist);
362 				goto resubmit;
363 			}
364 			hlist_for_each_entry(frag, &zd->fraglist, fnode)
365 				if (frag->seq == (seq&IEEE80211_SCTL_SEQ))
366 					break;
367 			if (!frag)
368 				goto resubmit;
369 			skb = frag->skb;
370 			ptr = skb_put(skb, len);
371 			if (ptr)
372 				memcpy(ptr, data+8, len);
373 			if (fc & IEEE80211_FCTL_MOREFRAGS)
374 				goto resubmit;
375 			hlist_del_init(&frag->fnode);
376 			kfree(frag);
377 		} else {
378 			if (datalen<14)
379 				goto resubmit;
380 			skb = dev_alloc_skb(len + 14 + 2);
381 			if (!skb)
382 				goto resubmit;
383 			skb_reserve(skb, 2);
384 			skb_put_data(skb, &data[datalen - 14], 12);
385 			skb_put_data(skb, &data[6], 2);
386 			skb_put_data(skb, data + 8, len);
387 		}
388 		skb->protocol = eth_type_trans(skb, zd->dev);
389 		zd->dev->stats.rx_packets++;
390 		zd->dev->stats.rx_bytes += skb->len;
391 		netif_rx(skb);
392 	}
393 resubmit:
394 	memset(data, 0, ZD1201_RXSIZE);
395 
396 	urb->status = 0;
397 	urb->dev = zd->usb;
398 	if(usb_submit_urb(urb, GFP_ATOMIC))
399 		free = 1;
400 
401 exit:
402 	if (free) {
403 		zd->rxlen = 0;
404 		zd->rxdatas = 1;
405 		wake_up(&zd->rxdataq);
406 		kfree(urb->transfer_buffer);
407 	}
408 }
409 
410 static int zd1201_getconfig(struct zd1201 *zd, int rid, void *riddata,
411 	unsigned int riddatalen)
412 {
413 	int err;
414 	int i = 0;
415 	int code;
416 	int rid_fid;
417 	int length;
418 	unsigned char *pdata;
419 
420 	zd->rxdatas = 0;
421 	err = zd1201_docmd(zd, ZD1201_CMDCODE_ACCESS, rid, 0, 0);
422 	if (err)
423 		return err;
424 
425 	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
426 	if (!zd->rxlen)
427 		return -EIO;
428 
429 	code = le16_to_cpu(*(__le16*)(&zd->rxdata[4]));
430 	rid_fid = le16_to_cpu(*(__le16*)(&zd->rxdata[6]));
431 	length = le16_to_cpu(*(__le16*)(&zd->rxdata[8]));
432 	if (length > zd->rxlen)
433 		length = zd->rxlen-6;
434 
435 	/* If access bit is not on, then error */
436 	if ((code & ZD1201_ACCESSBIT) != ZD1201_ACCESSBIT || rid_fid != rid )
437 		return -EINVAL;
438 
439 	/* Not enough buffer for allocating data */
440 	if (riddatalen != (length - 4)) {
441 		dev_dbg(&zd->usb->dev, "riddatalen mismatches, expected=%u, (packet=%u) length=%u, rid=0x%04X, rid_fid=0x%04X\n",
442 		    riddatalen, zd->rxlen, length, rid, rid_fid);
443 		return -ENODATA;
444 	}
445 
446 	zd->rxdatas = 0;
447 	/* Issue SetRxRid commnd */
448 	err = zd1201_docmd(zd, ZD1201_CMDCODE_SETRXRID, rid, 0, length);
449 	if (err)
450 		return err;
451 
452 	/* Receive RID record from resource packets */
453 	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
454 	if (!zd->rxlen)
455 		return -EIO;
456 
457 	if (zd->rxdata[zd->rxlen - 1] != ZD1201_PACKET_RESOURCE) {
458 		dev_dbg(&zd->usb->dev, "Packet type mismatch: 0x%x not 0x3\n",
459 		    zd->rxdata[zd->rxlen-1]);
460 		return -EINVAL;
461 	}
462 
463 	/* Set the data pointer and received data length */
464 	pdata = zd->rxdata;
465 	length = zd->rxlen;
466 
467 	do {
468 		int actual_length;
469 
470 		actual_length = (length > 64) ? 64 : length;
471 
472 		if (pdata[0] != 0x3) {
473 			dev_dbg(&zd->usb->dev, "Rx Resource packet type error: %02X\n",
474 			    pdata[0]);
475 			return -EINVAL;
476 		}
477 
478 		if (actual_length != 64) {
479 			/* Trim the last packet type byte */
480 			actual_length--;
481 		}
482 
483 		/* Skip the 4 bytes header (RID length and RID) */
484 		if (i == 0) {
485 			pdata += 8;
486 			actual_length -= 8;
487 		} else {
488 			pdata += 4;
489 			actual_length -= 4;
490 		}
491 
492 		memcpy(riddata, pdata, actual_length);
493 		riddata += actual_length;
494 		pdata += actual_length;
495 		length -= 64;
496 		i++;
497 	} while (length > 0);
498 
499 	return 0;
500 }
501 
502 /*
503  *	resreq:
504  *		byte	type
505  *		byte	sequence
506  *		u16	reserved
507  *		byte	data[12]
508  *	total: 16
509  */
510 static int zd1201_setconfig(struct zd1201 *zd, int rid, const void *buf, int len, int wait)
511 {
512 	int err;
513 	unsigned char *request;
514 	int reqlen;
515 	char seq=0;
516 	struct urb *urb;
517 	gfp_t gfp_mask = wait ? GFP_NOIO : GFP_ATOMIC;
518 
519 	len += 4;			/* first 4 are for header */
520 
521 	zd->rxdatas = 0;
522 	zd->rxlen = 0;
523 	for (seq=0; len > 0; seq++) {
524 		request = kzalloc(16, gfp_mask);
525 		if (!request)
526 			return -ENOMEM;
527 		urb = usb_alloc_urb(0, gfp_mask);
528 		if (!urb) {
529 			kfree(request);
530 			return -ENOMEM;
531 		}
532 		reqlen = len>12 ? 12 : len;
533 		request[0] = ZD1201_USB_RESREQ;
534 		request[1] = seq;
535 		request[2] = 0;
536 		request[3] = 0;
537 		if (request[1] == 0) {
538 			/* add header */
539 			*(__le16*)&request[4] = cpu_to_le16((len-2+1)/2);
540 			*(__le16*)&request[6] = cpu_to_le16(rid);
541 			memcpy(request+8, buf, reqlen-4);
542 			buf += reqlen-4;
543 		} else {
544 			memcpy(request+4, buf, reqlen);
545 			buf += reqlen;
546 		}
547 
548 		len -= reqlen;
549 
550 		usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb,
551 		    zd->endp_out2), request, 16, zd1201_usbfree, zd);
552 		err = usb_submit_urb(urb, gfp_mask);
553 		if (err)
554 			goto err;
555 	}
556 
557 	request = kmalloc(16, gfp_mask);
558 	if (!request)
559 		return -ENOMEM;
560 	urb = usb_alloc_urb(0, gfp_mask);
561 	if (!urb) {
562 		kfree(request);
563 		return -ENOMEM;
564 	}
565 	*((__le32*)request) = cpu_to_le32(ZD1201_USB_CMDREQ);
566 	*((__le16*)&request[4]) =
567 	    cpu_to_le16(ZD1201_CMDCODE_ACCESS|ZD1201_ACCESSBIT);
568 	*((__le16*)&request[6]) = cpu_to_le16(rid);
569 	*((__le16*)&request[8]) = cpu_to_le16(0);
570 	*((__le16*)&request[10]) = cpu_to_le16(0);
571 	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
572 	     request, 16, zd1201_usbfree, zd);
573 	err = usb_submit_urb(urb, gfp_mask);
574 	if (err)
575 		goto err;
576 
577 	if (wait) {
578 		wait_event_interruptible(zd->rxdataq, zd->rxdatas);
579 		if (!zd->rxlen || le16_to_cpu(*(__le16*)&zd->rxdata[6]) != rid) {
580 			dev_dbg(&zd->usb->dev, "wrong or no RID received\n");
581 		}
582 	}
583 
584 	return 0;
585 err:
586 	kfree(request);
587 	usb_free_urb(urb);
588 	return err;
589 }
590 
591 static inline int zd1201_getconfig16(struct zd1201 *zd, int rid, short *val)
592 {
593 	int err;
594 	__le16 zdval;
595 
596 	err = zd1201_getconfig(zd, rid, &zdval, sizeof(__le16));
597 	if (err)
598 		return err;
599 	*val = le16_to_cpu(zdval);
600 	return 0;
601 }
602 
603 static inline int zd1201_setconfig16(struct zd1201 *zd, int rid, short val)
604 {
605 	__le16 zdval = cpu_to_le16(val);
606 	return (zd1201_setconfig(zd, rid, &zdval, sizeof(__le16), 1));
607 }
608 
609 static int zd1201_drvr_start(struct zd1201 *zd)
610 {
611 	int err, i;
612 	short max;
613 	__le16 zdmax;
614 	unsigned char *buffer;
615 
616 	buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
617 	if (!buffer)
618 		return -ENOMEM;
619 
620 	usb_fill_bulk_urb(zd->rx_urb, zd->usb,
621 	    usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
622 	    zd1201_usbrx, zd);
623 
624 	err = usb_submit_urb(zd->rx_urb, GFP_KERNEL);
625 	if (err)
626 		goto err_buffer;
627 
628 	err = zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
629 	if (err)
630 		goto err_urb;
631 
632 	err = zd1201_getconfig(zd, ZD1201_RID_CNFMAXTXBUFFERNUMBER, &zdmax,
633 	    sizeof(__le16));
634 	if (err)
635 		goto err_urb;
636 
637 	max = le16_to_cpu(zdmax);
638 	for (i=0; i<max; i++) {
639 		err = zd1201_docmd(zd, ZD1201_CMDCODE_ALLOC, 1514, 0, 0);
640 		if (err)
641 			goto err_urb;
642 	}
643 
644 	return 0;
645 
646 err_urb:
647 	usb_kill_urb(zd->rx_urb);
648 	return err;
649 err_buffer:
650 	kfree(buffer);
651 	return err;
652 }
653 
654 /*	Magic alert: The firmware doesn't seem to like the MAC state being
655  *	toggled in promisc (aka monitor) mode.
656  *	(It works a number of times, but will halt eventually)
657  *	So we turn it of before disabling and on after enabling if needed.
658  */
659 static int zd1201_enable(struct zd1201 *zd)
660 {
661 	int err;
662 
663 	if (zd->mac_enabled)
664 		return 0;
665 
666 	err = zd1201_docmd(zd, ZD1201_CMDCODE_ENABLE, 0, 0, 0);
667 	if (!err)
668 		zd->mac_enabled = 1;
669 
670 	if (zd->monitor)
671 		err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 1);
672 
673 	return err;
674 }
675 
676 static int zd1201_disable(struct zd1201 *zd)
677 {
678 	int err;
679 
680 	if (!zd->mac_enabled)
681 		return 0;
682 	if (zd->monitor) {
683 		err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
684 		if (err)
685 			return err;
686 	}
687 
688 	err = zd1201_docmd(zd, ZD1201_CMDCODE_DISABLE, 0, 0, 0);
689 	if (!err)
690 		zd->mac_enabled = 0;
691 	return err;
692 }
693 
694 static int zd1201_mac_reset(struct zd1201 *zd)
695 {
696 	if (!zd->mac_enabled)
697 		return 0;
698 	zd1201_disable(zd);
699 	return zd1201_enable(zd);
700 }
701 
702 static int zd1201_join(struct zd1201 *zd, char *essid, int essidlen)
703 {
704 	int err, val;
705 	char buf[IW_ESSID_MAX_SIZE+2];
706 
707 	err = zd1201_disable(zd);
708 	if (err)
709 		return err;
710 
711 	val = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
712 	val |= ZD1201_CNFAUTHENTICATION_SHAREDKEY;
713 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, val);
714 	if (err)
715 		return err;
716 
717 	*(__le16 *)buf = cpu_to_le16(essidlen);
718 	memcpy(buf+2, essid, essidlen);
719 	if (!zd->ap) {	/* Normal station */
720 		err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
721 		    IW_ESSID_MAX_SIZE+2, 1);
722 		if (err)
723 			return err;
724 	} else {	/* AP */
725 		err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNSSID, buf,
726 		    IW_ESSID_MAX_SIZE+2, 1);
727 		if (err)
728 			return err;
729 	}
730 
731 	err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
732 	    zd->dev->dev_addr, zd->dev->addr_len, 1);
733 	if (err)
734 		return err;
735 
736 	err = zd1201_enable(zd);
737 	if (err)
738 		return err;
739 
740 	msleep(100);
741 	return 0;
742 }
743 
744 static int zd1201_net_open(struct net_device *dev)
745 {
746 	struct zd1201 *zd = netdev_priv(dev);
747 
748 	/* Start MAC with wildcard if no essid set */
749 	if (!zd->mac_enabled)
750 		zd1201_join(zd, zd->essid, zd->essidlen);
751 	netif_start_queue(dev);
752 
753 	return 0;
754 }
755 
756 static int zd1201_net_stop(struct net_device *dev)
757 {
758 	netif_stop_queue(dev);
759 	return 0;
760 }
761 
762 /*
763 	RFC 1042 encapsulates Ethernet frames in 802.11 frames
764 	by prefixing them with 0xaa, 0xaa, 0x03) followed by a SNAP OID of 0
765 	(0x00, 0x00, 0x00). Zd requires an additional padding, copy
766 	of ethernet addresses, length of the standard RFC 1042 packet
767 	and a command byte (which is nul for tx).
768 
769 	tx frame (from Wlan NG):
770 	RFC 1042:
771 		llc		0xAA 0xAA 0x03 (802.2 LLC)
772 		snap		0x00 0x00 0x00 (Ethernet encapsulated)
773 		type		2 bytes, Ethernet type field
774 		payload		(minus eth header)
775 	Zydas specific:
776 		padding		1B if (skb->len+8+1)%64==0
777 		Eth MAC addr	12 bytes, Ethernet MAC addresses
778 		length		2 bytes, RFC 1042 packet length
779 				(llc+snap+type+payload)
780 		zd		1 null byte, zd1201 packet type
781  */
782 static netdev_tx_t zd1201_hard_start_xmit(struct sk_buff *skb,
783 						struct net_device *dev)
784 {
785 	struct zd1201 *zd = netdev_priv(dev);
786 	unsigned char *txbuf = zd->txdata;
787 	int txbuflen, pad = 0, err;
788 	struct urb *urb = zd->tx_urb;
789 
790 	if (!zd->mac_enabled || zd->monitor) {
791 		dev->stats.tx_dropped++;
792 		kfree_skb(skb);
793 		return NETDEV_TX_OK;
794 	}
795 	netif_stop_queue(dev);
796 
797 	txbuflen = skb->len + 8 + 1;
798 	if (txbuflen%64 == 0) {
799 		pad = 1;
800 		txbuflen++;
801 	}
802 	txbuf[0] = 0xAA;
803 	txbuf[1] = 0xAA;
804 	txbuf[2] = 0x03;
805 	txbuf[3] = 0x00;	/* rfc1042 */
806 	txbuf[4] = 0x00;
807 	txbuf[5] = 0x00;
808 
809 	skb_copy_from_linear_data_offset(skb, 12, txbuf + 6, skb->len - 12);
810 	if (pad)
811 		txbuf[skb->len-12+6]=0;
812 	skb_copy_from_linear_data(skb, txbuf + skb->len - 12 + 6 + pad, 12);
813 	*(__be16*)&txbuf[skb->len+6+pad] = htons(skb->len-12+6);
814 	txbuf[txbuflen-1] = 0;
815 
816 	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out),
817 	    txbuf, txbuflen, zd1201_usbtx, zd);
818 
819 	err = usb_submit_urb(zd->tx_urb, GFP_ATOMIC);
820 	if (err) {
821 		dev->stats.tx_errors++;
822 		netif_start_queue(dev);
823 	} else {
824 		dev->stats.tx_packets++;
825 		dev->stats.tx_bytes += skb->len;
826 	}
827 	kfree_skb(skb);
828 
829 	return NETDEV_TX_OK;
830 }
831 
832 static void zd1201_tx_timeout(struct net_device *dev, unsigned int txqueue)
833 {
834 	struct zd1201 *zd = netdev_priv(dev);
835 
836 	if (!zd)
837 		return;
838 	dev_warn(&zd->usb->dev, "%s: TX timeout, shooting down urb\n",
839 	    dev->name);
840 	usb_unlink_urb(zd->tx_urb);
841 	dev->stats.tx_errors++;
842 	/* Restart the timeout to quiet the watchdog: */
843 	netif_trans_update(dev); /* prevent tx timeout */
844 }
845 
846 static int zd1201_set_mac_address(struct net_device *dev, void *p)
847 {
848 	struct sockaddr *addr = p;
849 	struct zd1201 *zd = netdev_priv(dev);
850 	int err;
851 
852 	if (!zd)
853 		return -ENODEV;
854 
855 	err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
856 	    addr->sa_data, dev->addr_len, 1);
857 	if (err)
858 		return err;
859 	eth_hw_addr_set(dev, addr->sa_data);
860 
861 	return zd1201_mac_reset(zd);
862 }
863 
864 static struct iw_statistics *zd1201_get_wireless_stats(struct net_device *dev)
865 {
866 	struct zd1201 *zd = netdev_priv(dev);
867 
868 	return &zd->iwstats;
869 }
870 
871 static void zd1201_set_multicast(struct net_device *dev)
872 {
873 	struct zd1201 *zd = netdev_priv(dev);
874 	struct netdev_hw_addr *ha;
875 	unsigned char reqbuf[ETH_ALEN*ZD1201_MAXMULTI];
876 	int i;
877 
878 	if (netdev_mc_count(dev) > ZD1201_MAXMULTI)
879 		return;
880 
881 	i = 0;
882 	netdev_for_each_mc_addr(ha, dev)
883 		memcpy(reqbuf + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
884 	zd1201_setconfig(zd, ZD1201_RID_CNFGROUPADDRESS, reqbuf,
885 			 netdev_mc_count(dev) * ETH_ALEN, 0);
886 }
887 
888 static int zd1201_config_commit(struct net_device *dev,
889     struct iw_request_info *info, struct iw_point *data, char *essid)
890 {
891 	struct zd1201 *zd = netdev_priv(dev);
892 
893 	return zd1201_mac_reset(zd);
894 }
895 
896 static int zd1201_get_name(struct net_device *dev,
897     struct iw_request_info *info, char *name, char *extra)
898 {
899 	strcpy(name, "IEEE 802.11b");
900 	return 0;
901 }
902 
903 static int zd1201_set_freq(struct net_device *dev,
904     struct iw_request_info *info, struct iw_freq *freq, char *extra)
905 {
906 	struct zd1201 *zd = netdev_priv(dev);
907 	short channel = 0;
908 	int err;
909 
910 	if (freq->e == 0)
911 		channel = freq->m;
912 	else
913 		channel = ieee80211_frequency_to_channel(freq->m);
914 
915 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, channel);
916 	if (err)
917 		return err;
918 
919 	zd1201_mac_reset(zd);
920 
921 	return 0;
922 }
923 
924 static int zd1201_get_freq(struct net_device *dev,
925     struct iw_request_info *info, struct iw_freq *freq, char *extra)
926 {
927 	struct zd1201 *zd = netdev_priv(dev);
928 	short channel;
929 	int err;
930 
931 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, &channel);
932 	if (err)
933 		return err;
934 	freq->e = 0;
935 	freq->m = channel;
936 
937 	return 0;
938 }
939 
940 static int zd1201_set_mode(struct net_device *dev,
941     struct iw_request_info *info, __u32 *mode, char *extra)
942 {
943 	struct zd1201 *zd = netdev_priv(dev);
944 	short porttype, monitor = 0;
945 	unsigned char buffer[IW_ESSID_MAX_SIZE+2];
946 	int err;
947 
948 	if (zd->ap) {
949 		if (*mode != IW_MODE_MASTER)
950 			return -EINVAL;
951 		return 0;
952 	}
953 
954 	err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
955 	if (err)
956 		return err;
957 	zd->dev->type = ARPHRD_ETHER;
958 	switch(*mode) {
959 		case IW_MODE_MONITOR:
960 			monitor = 1;
961 			zd->dev->type = ARPHRD_IEEE80211;
962 			/* Make sure we are no longer associated with by
963 			   setting an 'impossible' essid.
964 			   (otherwise we mess up firmware)
965 			 */
966 			zd1201_join(zd, "\0-*#\0", 5);
967 			/* Put port in pIBSS */
968 			fallthrough;
969 		case 8: /* No pseudo-IBSS in wireless extensions (yet) */
970 			porttype = ZD1201_PORTTYPE_PSEUDOIBSS;
971 			break;
972 		case IW_MODE_ADHOC:
973 			porttype = ZD1201_PORTTYPE_IBSS;
974 			break;
975 		case IW_MODE_INFRA:
976 			porttype = ZD1201_PORTTYPE_BSS;
977 			break;
978 		default:
979 			return -EINVAL;
980 	}
981 
982 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
983 	if (err)
984 		return err;
985 	if (zd->monitor && !monitor) {
986 			zd1201_disable(zd);
987 			*(__le16 *)buffer = cpu_to_le16(zd->essidlen);
988 			memcpy(buffer+2, zd->essid, zd->essidlen);
989 			err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID,
990 			    buffer, IW_ESSID_MAX_SIZE+2, 1);
991 			if (err)
992 				return err;
993 	}
994 	zd->monitor = monitor;
995 	/* If monitor mode is set we don't actually turn it on here since it
996 	 * is done during mac reset anyway (see zd1201_mac_enable).
997 	 */
998 	zd1201_mac_reset(zd);
999 
1000 	return 0;
1001 }
1002 
1003 static int zd1201_get_mode(struct net_device *dev,
1004     struct iw_request_info *info, __u32 *mode, char *extra)
1005 {
1006 	struct zd1201 *zd = netdev_priv(dev);
1007 	short porttype;
1008 	int err;
1009 
1010 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPORTTYPE, &porttype);
1011 	if (err)
1012 		return err;
1013 	switch(porttype) {
1014 		case ZD1201_PORTTYPE_IBSS:
1015 			*mode = IW_MODE_ADHOC;
1016 			break;
1017 		case ZD1201_PORTTYPE_BSS:
1018 			*mode = IW_MODE_INFRA;
1019 			break;
1020 		case ZD1201_PORTTYPE_WDS:
1021 			*mode = IW_MODE_REPEAT;
1022 			break;
1023 		case ZD1201_PORTTYPE_PSEUDOIBSS:
1024 			*mode = 8;/* No Pseudo-IBSS... */
1025 			break;
1026 		case ZD1201_PORTTYPE_AP:
1027 			*mode = IW_MODE_MASTER;
1028 			break;
1029 		default:
1030 			dev_dbg(&zd->usb->dev, "Unknown porttype: %d\n",
1031 			    porttype);
1032 			*mode = IW_MODE_AUTO;
1033 	}
1034 	if (zd->monitor)
1035 		*mode = IW_MODE_MONITOR;
1036 
1037 	return 0;
1038 }
1039 
1040 static int zd1201_get_range(struct net_device *dev,
1041     struct iw_request_info *info, struct iw_point *wrq, char *extra)
1042 {
1043 	struct iw_range *range = (struct iw_range *)extra;
1044 
1045 	wrq->length = sizeof(struct iw_range);
1046 	memset(range, 0, sizeof(struct iw_range));
1047 	range->we_version_compiled = WIRELESS_EXT;
1048 	range->we_version_source = WIRELESS_EXT;
1049 
1050 	range->max_qual.qual = 128;
1051 	range->max_qual.level = 128;
1052 	range->max_qual.noise = 128;
1053 	range->max_qual.updated = 7;
1054 
1055 	range->encoding_size[0] = 5;
1056 	range->encoding_size[1] = 13;
1057 	range->num_encoding_sizes = 2;
1058 	range->max_encoding_tokens = ZD1201_NUMKEYS;
1059 
1060 	range->num_bitrates = 4;
1061 	range->bitrate[0] = 1000000;
1062 	range->bitrate[1] = 2000000;
1063 	range->bitrate[2] = 5500000;
1064 	range->bitrate[3] = 11000000;
1065 
1066 	range->min_rts = 0;
1067 	range->min_frag = ZD1201_FRAGMIN;
1068 	range->max_rts = ZD1201_RTSMAX;
1069 	range->min_frag = ZD1201_FRAGMAX;
1070 
1071 	return 0;
1072 }
1073 
1074 /*	Little bit of magic here: we only get the quality if we poll
1075  *	for it, and we never get an actual request to trigger such
1076  *	a poll. Therefore we 'assume' that the user will soon ask for
1077  *	the stats after asking the bssid.
1078  */
1079 static int zd1201_get_wap(struct net_device *dev,
1080     struct iw_request_info *info, struct sockaddr *ap_addr, char *extra)
1081 {
1082 	struct zd1201 *zd = netdev_priv(dev);
1083 	unsigned char buffer[6];
1084 
1085 	if (!zd1201_getconfig(zd, ZD1201_RID_COMMSQUALITY, buffer, 6)) {
1086 		/* Unfortunately the quality and noise reported is useless.
1087 		   they seem to be accumulators that increase until you
1088 		   read them, unless we poll on a fixed interval we can't
1089 		   use them
1090 		 */
1091 		/*zd->iwstats.qual.qual = le16_to_cpu(((__le16 *)buffer)[0]);*/
1092 		zd->iwstats.qual.level = le16_to_cpu(((__le16 *)buffer)[1]);
1093 		/*zd->iwstats.qual.noise = le16_to_cpu(((__le16 *)buffer)[2]);*/
1094 		zd->iwstats.qual.updated = 2;
1095 	}
1096 
1097 	return zd1201_getconfig(zd, ZD1201_RID_CURRENTBSSID, ap_addr->sa_data, 6);
1098 }
1099 
1100 static int zd1201_set_scan(struct net_device *dev,
1101     struct iw_request_info *info, struct iw_point *srq, char *extra)
1102 {
1103 	/* We do everything in get_scan */
1104 	return 0;
1105 }
1106 
1107 static int zd1201_get_scan(struct net_device *dev,
1108     struct iw_request_info *info, struct iw_point *srq, char *extra)
1109 {
1110 	struct zd1201 *zd = netdev_priv(dev);
1111 	int err, i, j, enabled_save;
1112 	struct iw_event iwe;
1113 	char *cev = extra;
1114 	char *end_buf = extra + IW_SCAN_MAX_DATA;
1115 
1116 	/* No scanning in AP mode */
1117 	if (zd->ap)
1118 		return -EOPNOTSUPP;
1119 
1120 	/* Scan doesn't seem to work if disabled */
1121 	enabled_save = zd->mac_enabled;
1122 	zd1201_enable(zd);
1123 
1124 	zd->rxdatas = 0;
1125 	err = zd1201_docmd(zd, ZD1201_CMDCODE_INQUIRE,
1126 	     ZD1201_INQ_SCANRESULTS, 0, 0);
1127 	if (err)
1128 		return err;
1129 
1130 	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
1131 	if (!zd->rxlen)
1132 		return -EIO;
1133 
1134 	if (le16_to_cpu(*(__le16*)&zd->rxdata[2]) != ZD1201_INQ_SCANRESULTS)
1135 		return -EIO;
1136 
1137 	for(i=8; i<zd->rxlen; i+=62) {
1138 		iwe.cmd = SIOCGIWAP;
1139 		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1140 		memcpy(iwe.u.ap_addr.sa_data, zd->rxdata+i+6, 6);
1141 		cev = iwe_stream_add_event(info, cev, end_buf,
1142 					   &iwe, IW_EV_ADDR_LEN);
1143 
1144 		iwe.cmd = SIOCGIWESSID;
1145 		iwe.u.data.length = zd->rxdata[i+16];
1146 		iwe.u.data.flags = 1;
1147 		cev = iwe_stream_add_point(info, cev, end_buf,
1148 					   &iwe, zd->rxdata+i+18);
1149 
1150 		iwe.cmd = SIOCGIWMODE;
1151 		if (zd->rxdata[i+14]&0x01)
1152 			iwe.u.mode = IW_MODE_MASTER;
1153 		else
1154 			iwe.u.mode = IW_MODE_ADHOC;
1155 		cev = iwe_stream_add_event(info, cev, end_buf,
1156 					   &iwe, IW_EV_UINT_LEN);
1157 
1158 		iwe.cmd = SIOCGIWFREQ;
1159 		iwe.u.freq.m = zd->rxdata[i+0];
1160 		iwe.u.freq.e = 0;
1161 		cev = iwe_stream_add_event(info, cev, end_buf,
1162 					   &iwe, IW_EV_FREQ_LEN);
1163 
1164 		iwe.cmd = SIOCGIWRATE;
1165 		iwe.u.bitrate.fixed = 0;
1166 		iwe.u.bitrate.disabled = 0;
1167 		for (j=0; j<10; j++) if (zd->rxdata[i+50+j]) {
1168 			iwe.u.bitrate.value = (zd->rxdata[i+50+j]&0x7f)*500000;
1169 			cev = iwe_stream_add_event(info, cev, end_buf,
1170 						   &iwe, IW_EV_PARAM_LEN);
1171 		}
1172 
1173 		iwe.cmd = SIOCGIWENCODE;
1174 		iwe.u.data.length = 0;
1175 		if (zd->rxdata[i+14]&0x10)
1176 			iwe.u.data.flags = IW_ENCODE_ENABLED;
1177 		else
1178 			iwe.u.data.flags = IW_ENCODE_DISABLED;
1179 		cev = iwe_stream_add_point(info, cev, end_buf, &iwe, NULL);
1180 
1181 		iwe.cmd = IWEVQUAL;
1182 		iwe.u.qual.qual = zd->rxdata[i+4];
1183 		iwe.u.qual.noise= zd->rxdata[i+2]/10-100;
1184 		iwe.u.qual.level = (256+zd->rxdata[i+4]*100)/255-100;
1185 		iwe.u.qual.updated = 7;
1186 		cev = iwe_stream_add_event(info, cev, end_buf,
1187 					   &iwe, IW_EV_QUAL_LEN);
1188 	}
1189 
1190 	if (!enabled_save)
1191 		zd1201_disable(zd);
1192 
1193 	srq->length = cev - extra;
1194 	srq->flags = 0;
1195 
1196 	return 0;
1197 }
1198 
1199 static int zd1201_set_essid(struct net_device *dev,
1200     struct iw_request_info *info, struct iw_point *data, char *essid)
1201 {
1202 	struct zd1201 *zd = netdev_priv(dev);
1203 
1204 	if (data->length > IW_ESSID_MAX_SIZE)
1205 		return -EINVAL;
1206 	if (data->length < 1)
1207 		data->length = 1;
1208 	zd->essidlen = data->length;
1209 	memset(zd->essid, 0, IW_ESSID_MAX_SIZE+1);
1210 	memcpy(zd->essid, essid, data->length);
1211 	return zd1201_join(zd, zd->essid, zd->essidlen);
1212 }
1213 
1214 static int zd1201_get_essid(struct net_device *dev,
1215     struct iw_request_info *info, struct iw_point *data, char *essid)
1216 {
1217 	struct zd1201 *zd = netdev_priv(dev);
1218 
1219 	memcpy(essid, zd->essid, zd->essidlen);
1220 	data->flags = 1;
1221 	data->length = zd->essidlen;
1222 
1223 	return 0;
1224 }
1225 
1226 static int zd1201_get_nick(struct net_device *dev, struct iw_request_info *info,
1227     struct iw_point *data, char *nick)
1228 {
1229 	strcpy(nick, "zd1201");
1230 	data->flags = 1;
1231 	data->length = strlen(nick);
1232 	return 0;
1233 }
1234 
1235 static int zd1201_set_rate(struct net_device *dev,
1236     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1237 {
1238 	struct zd1201 *zd = netdev_priv(dev);
1239 	short rate;
1240 	int err;
1241 
1242 	switch (rrq->value) {
1243 		case 1000000:
1244 			rate = ZD1201_RATEB1;
1245 			break;
1246 		case 2000000:
1247 			rate = ZD1201_RATEB2;
1248 			break;
1249 		case 5500000:
1250 			rate = ZD1201_RATEB5;
1251 			break;
1252 		case 11000000:
1253 		default:
1254 			rate = ZD1201_RATEB11;
1255 			break;
1256 	}
1257 	if (!rrq->fixed) { /* Also enable all lower bitrates */
1258 		rate |= rate-1;
1259 	}
1260 
1261 	err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL, rate);
1262 	if (err)
1263 		return err;
1264 
1265 	return zd1201_mac_reset(zd);
1266 }
1267 
1268 static int zd1201_get_rate(struct net_device *dev,
1269     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1270 {
1271 	struct zd1201 *zd = netdev_priv(dev);
1272 	short rate;
1273 	int err;
1274 
1275 	err = zd1201_getconfig16(zd, ZD1201_RID_CURRENTTXRATE, &rate);
1276 	if (err)
1277 		return err;
1278 
1279 	switch(rate) {
1280 		case 1:
1281 			rrq->value = 1000000;
1282 			break;
1283 		case 2:
1284 			rrq->value = 2000000;
1285 			break;
1286 		case 5:
1287 			rrq->value = 5500000;
1288 			break;
1289 		case 11:
1290 			rrq->value = 11000000;
1291 			break;
1292 		default:
1293 			rrq->value = 0;
1294 	}
1295 	rrq->fixed = 0;
1296 	rrq->disabled = 0;
1297 
1298 	return 0;
1299 }
1300 
1301 static int zd1201_set_rts(struct net_device *dev, struct iw_request_info *info,
1302     struct iw_param *rts, char *extra)
1303 {
1304 	struct zd1201 *zd = netdev_priv(dev);
1305 	int err;
1306 	short val = rts->value;
1307 
1308 	if (rts->disabled || !rts->fixed)
1309 		val = ZD1201_RTSMAX;
1310 	if (val > ZD1201_RTSMAX)
1311 		return -EINVAL;
1312 	if (val < 0)
1313 		return -EINVAL;
1314 
1315 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, val);
1316 	if (err)
1317 		return err;
1318 	return zd1201_mac_reset(zd);
1319 }
1320 
1321 static int zd1201_get_rts(struct net_device *dev, struct iw_request_info *info,
1322     struct iw_param *rts, char *extra)
1323 {
1324 	struct zd1201 *zd = netdev_priv(dev);
1325 	short rtst;
1326 	int err;
1327 
1328 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, &rtst);
1329 	if (err)
1330 		return err;
1331 	rts->value = rtst;
1332 	rts->disabled = (rts->value == ZD1201_RTSMAX);
1333 	rts->fixed = 1;
1334 
1335 	return 0;
1336 }
1337 
1338 static int zd1201_set_frag(struct net_device *dev, struct iw_request_info *info,
1339     struct iw_param *frag, char *extra)
1340 {
1341 	struct zd1201 *zd = netdev_priv(dev);
1342 	int err;
1343 	short val = frag->value;
1344 
1345 	if (frag->disabled || !frag->fixed)
1346 		val = ZD1201_FRAGMAX;
1347 	if (val > ZD1201_FRAGMAX)
1348 		return -EINVAL;
1349 	if (val < ZD1201_FRAGMIN)
1350 		return -EINVAL;
1351 	if (val & 1)
1352 		return -EINVAL;
1353 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, val);
1354 	if (err)
1355 		return err;
1356 	return zd1201_mac_reset(zd);
1357 }
1358 
1359 static int zd1201_get_frag(struct net_device *dev, struct iw_request_info *info,
1360     struct iw_param *frag, char *extra)
1361 {
1362 	struct zd1201 *zd = netdev_priv(dev);
1363 	short fragt;
1364 	int err;
1365 
1366 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, &fragt);
1367 	if (err)
1368 		return err;
1369 	frag->value = fragt;
1370 	frag->disabled = (frag->value == ZD1201_FRAGMAX);
1371 	frag->fixed = 1;
1372 
1373 	return 0;
1374 }
1375 
1376 static int zd1201_set_retry(struct net_device *dev,
1377     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1378 {
1379 	return 0;
1380 }
1381 
1382 static int zd1201_get_retry(struct net_device *dev,
1383     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1384 {
1385 	return 0;
1386 }
1387 
1388 static int zd1201_set_encode(struct net_device *dev,
1389     struct iw_request_info *info, struct iw_point *erq, char *key)
1390 {
1391 	struct zd1201 *zd = netdev_priv(dev);
1392 	short i;
1393 	int err, rid;
1394 
1395 	if (erq->length > ZD1201_MAXKEYLEN)
1396 		return -EINVAL;
1397 
1398 	i = (erq->flags & IW_ENCODE_INDEX)-1;
1399 	if (i == -1) {
1400 		err = zd1201_getconfig16(zd,ZD1201_RID_CNFDEFAULTKEYID,&i);
1401 		if (err)
1402 			return err;
1403 	} else {
1404 		err = zd1201_setconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, i);
1405 		if (err)
1406 			return err;
1407 	}
1408 
1409 	if (i < 0 || i >= ZD1201_NUMKEYS)
1410 		return -EINVAL;
1411 
1412 	rid = ZD1201_RID_CNFDEFAULTKEY0 + i;
1413 	err = zd1201_setconfig(zd, rid, key, erq->length, 1);
1414 	if (err)
1415 		return err;
1416 	zd->encode_keylen[i] = erq->length;
1417 	memcpy(zd->encode_keys[i], key, erq->length);
1418 
1419 	i=0;
1420 	if (!(erq->flags & IW_ENCODE_DISABLED & IW_ENCODE_MODE)) {
1421 		i |= 0x01;
1422 		zd->encode_enabled = 1;
1423 	} else
1424 		zd->encode_enabled = 0;
1425 	if (erq->flags & IW_ENCODE_RESTRICTED & IW_ENCODE_MODE) {
1426 		i |= 0x02;
1427 		zd->encode_restricted = 1;
1428 	} else
1429 		zd->encode_restricted = 0;
1430 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFWEBFLAGS, i);
1431 	if (err)
1432 		return err;
1433 
1434 	if (zd->encode_enabled)
1435 		i = ZD1201_CNFAUTHENTICATION_SHAREDKEY;
1436 	else
1437 		i = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
1438 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, i);
1439 	if (err)
1440 		return err;
1441 
1442 	return zd1201_mac_reset(zd);
1443 }
1444 
1445 static int zd1201_get_encode(struct net_device *dev,
1446     struct iw_request_info *info, struct iw_point *erq, char *key)
1447 {
1448 	struct zd1201 *zd = netdev_priv(dev);
1449 	short i;
1450 	int err;
1451 
1452 	if (zd->encode_enabled)
1453 		erq->flags = IW_ENCODE_ENABLED;
1454 	else
1455 		erq->flags = IW_ENCODE_DISABLED;
1456 	if (zd->encode_restricted)
1457 		erq->flags |= IW_ENCODE_RESTRICTED;
1458 	else
1459 		erq->flags |= IW_ENCODE_OPEN;
1460 
1461 	i = (erq->flags & IW_ENCODE_INDEX) -1;
1462 	if (i == -1) {
1463 		err = zd1201_getconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, &i);
1464 		if (err)
1465 			return err;
1466 	}
1467 	if (i<0 || i>= ZD1201_NUMKEYS)
1468 		return -EINVAL;
1469 
1470 	erq->flags |= i+1;
1471 
1472 	erq->length = zd->encode_keylen[i];
1473 	memcpy(key, zd->encode_keys[i], erq->length);
1474 
1475 	return 0;
1476 }
1477 
1478 static int zd1201_set_power(struct net_device *dev,
1479     struct iw_request_info *info, struct iw_param *vwrq, char *extra)
1480 {
1481 	struct zd1201 *zd = netdev_priv(dev);
1482 	short enabled, duration, level;
1483 	int err;
1484 
1485 	enabled = vwrq->disabled ? 0 : 1;
1486 	if (enabled) {
1487 		if (vwrq->flags & IW_POWER_PERIOD) {
1488 			duration = vwrq->value;
1489 			err = zd1201_setconfig16(zd,
1490 			    ZD1201_RID_CNFMAXSLEEPDURATION, duration);
1491 			if (err)
1492 				return err;
1493 			goto out;
1494 		}
1495 		if (vwrq->flags & IW_POWER_TIMEOUT) {
1496 			err = zd1201_getconfig16(zd,
1497 			    ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
1498 			if (err)
1499 				return err;
1500 			level = vwrq->value * 4 / duration;
1501 			if (level > 4)
1502 				level = 4;
1503 			if (level < 0)
1504 				level = 0;
1505 			err = zd1201_setconfig16(zd, ZD1201_RID_CNFPMEPS,
1506 			    level);
1507 			if (err)
1508 				return err;
1509 			goto out;
1510 		}
1511 		return -EINVAL;
1512 	}
1513 out:
1514 	return zd1201_setconfig16(zd, ZD1201_RID_CNFPMENABLED, enabled);
1515 }
1516 
1517 static int zd1201_get_power(struct net_device *dev,
1518     struct iw_request_info *info, struct iw_param *vwrq, char *extra)
1519 {
1520 	struct zd1201 *zd = netdev_priv(dev);
1521 	short enabled, level, duration;
1522 	int err;
1523 
1524 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMENABLED, &enabled);
1525 	if (err)
1526 		return err;
1527 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMEPS, &level);
1528 	if (err)
1529 		return err;
1530 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
1531 	if (err)
1532 		return err;
1533 	vwrq->disabled = enabled ? 0 : 1;
1534 	if (vwrq->flags & IW_POWER_TYPE) {
1535 		if (vwrq->flags & IW_POWER_PERIOD) {
1536 			vwrq->value = duration;
1537 			vwrq->flags = IW_POWER_PERIOD;
1538 		} else {
1539 			vwrq->value = duration * level / 4;
1540 			vwrq->flags = IW_POWER_TIMEOUT;
1541 		}
1542 	}
1543 	if (vwrq->flags & IW_POWER_MODE) {
1544 		if (enabled && level)
1545 			vwrq->flags = IW_POWER_UNICAST_R;
1546 		else
1547 			vwrq->flags = IW_POWER_ALL_R;
1548 	}
1549 
1550 	return 0;
1551 }
1552 
1553 
1554 static const iw_handler zd1201_iw_handler[] =
1555 {
1556 	(iw_handler) zd1201_config_commit,	/* SIOCSIWCOMMIT */
1557 	(iw_handler) zd1201_get_name,    	/* SIOCGIWNAME */
1558 	(iw_handler) NULL,			/* SIOCSIWNWID */
1559 	(iw_handler) NULL,			/* SIOCGIWNWID */
1560 	(iw_handler) zd1201_set_freq,		/* SIOCSIWFREQ */
1561 	(iw_handler) zd1201_get_freq,		/* SIOCGIWFREQ */
1562 	(iw_handler) zd1201_set_mode,		/* SIOCSIWMODE */
1563 	(iw_handler) zd1201_get_mode,		/* SIOCGIWMODE */
1564 	(iw_handler) NULL,                  	/* SIOCSIWSENS */
1565 	(iw_handler) NULL,           		/* SIOCGIWSENS */
1566 	(iw_handler) NULL,			/* SIOCSIWRANGE */
1567 	(iw_handler) zd1201_get_range,           /* SIOCGIWRANGE */
1568 	(iw_handler) NULL,			/* SIOCSIWPRIV */
1569 	(iw_handler) NULL,			/* SIOCGIWPRIV */
1570 	(iw_handler) NULL,			/* SIOCSIWSTATS */
1571 	(iw_handler) NULL,			/* SIOCGIWSTATS */
1572 	(iw_handler) NULL,			/* SIOCSIWSPY */
1573 	(iw_handler) NULL,			/* SIOCGIWSPY */
1574 	(iw_handler) NULL,			/* -- hole -- */
1575 	(iw_handler) NULL,			/* -- hole -- */
1576 	(iw_handler) NULL/*zd1201_set_wap*/,		/* SIOCSIWAP */
1577 	(iw_handler) zd1201_get_wap,		/* SIOCGIWAP */
1578 	(iw_handler) NULL,			/* -- hole -- */
1579 	(iw_handler) NULL,       		/* SIOCGIWAPLIST */
1580 	(iw_handler) zd1201_set_scan,		/* SIOCSIWSCAN */
1581 	(iw_handler) zd1201_get_scan,		/* SIOCGIWSCAN */
1582 	(iw_handler) zd1201_set_essid,		/* SIOCSIWESSID */
1583 	(iw_handler) zd1201_get_essid,		/* SIOCGIWESSID */
1584 	(iw_handler) NULL,         		/* SIOCSIWNICKN */
1585 	(iw_handler) zd1201_get_nick, 		/* SIOCGIWNICKN */
1586 	(iw_handler) NULL,			/* -- hole -- */
1587 	(iw_handler) NULL,			/* -- hole -- */
1588 	(iw_handler) zd1201_set_rate,		/* SIOCSIWRATE */
1589 	(iw_handler) zd1201_get_rate,		/* SIOCGIWRATE */
1590 	(iw_handler) zd1201_set_rts,		/* SIOCSIWRTS */
1591 	(iw_handler) zd1201_get_rts,		/* SIOCGIWRTS */
1592 	(iw_handler) zd1201_set_frag,		/* SIOCSIWFRAG */
1593 	(iw_handler) zd1201_get_frag,		/* SIOCGIWFRAG */
1594 	(iw_handler) NULL,         		/* SIOCSIWTXPOW */
1595 	(iw_handler) NULL,          		/* SIOCGIWTXPOW */
1596 	(iw_handler) zd1201_set_retry,		/* SIOCSIWRETRY */
1597 	(iw_handler) zd1201_get_retry,		/* SIOCGIWRETRY */
1598 	(iw_handler) zd1201_set_encode,		/* SIOCSIWENCODE */
1599 	(iw_handler) zd1201_get_encode,		/* SIOCGIWENCODE */
1600 	(iw_handler) zd1201_set_power,		/* SIOCSIWPOWER */
1601 	(iw_handler) zd1201_get_power,		/* SIOCGIWPOWER */
1602 };
1603 
1604 static int zd1201_set_hostauth(struct net_device *dev,
1605     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1606 {
1607 	struct zd1201 *zd = netdev_priv(dev);
1608 
1609 	if (!zd->ap)
1610 		return -EOPNOTSUPP;
1611 
1612 	return zd1201_setconfig16(zd, ZD1201_RID_CNFHOSTAUTH, rrq->value);
1613 }
1614 
1615 static int zd1201_get_hostauth(struct net_device *dev,
1616     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1617 {
1618 	struct zd1201 *zd = netdev_priv(dev);
1619 	short hostauth;
1620 	int err;
1621 
1622 	if (!zd->ap)
1623 		return -EOPNOTSUPP;
1624 
1625 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFHOSTAUTH, &hostauth);
1626 	if (err)
1627 		return err;
1628 	rrq->value = hostauth;
1629 	rrq->fixed = 1;
1630 
1631 	return 0;
1632 }
1633 
1634 static int zd1201_auth_sta(struct net_device *dev,
1635     struct iw_request_info *info, struct sockaddr *sta, char *extra)
1636 {
1637 	struct zd1201 *zd = netdev_priv(dev);
1638 	unsigned char buffer[10];
1639 
1640 	if (!zd->ap)
1641 		return -EOPNOTSUPP;
1642 
1643 	memcpy(buffer, sta->sa_data, ETH_ALEN);
1644 	*(short*)(buffer+6) = 0;	/* 0==success, 1==failure */
1645 	*(short*)(buffer+8) = 0;
1646 
1647 	return zd1201_setconfig(zd, ZD1201_RID_AUTHENTICATESTA, buffer, 10, 1);
1648 }
1649 
1650 static int zd1201_set_maxassoc(struct net_device *dev,
1651     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1652 {
1653 	struct zd1201 *zd = netdev_priv(dev);
1654 
1655 	if (!zd->ap)
1656 		return -EOPNOTSUPP;
1657 
1658 	return zd1201_setconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, rrq->value);
1659 }
1660 
1661 static int zd1201_get_maxassoc(struct net_device *dev,
1662     struct iw_request_info *info, struct iw_param *rrq, char *extra)
1663 {
1664 	struct zd1201 *zd = netdev_priv(dev);
1665 	short maxassoc;
1666 	int err;
1667 
1668 	if (!zd->ap)
1669 		return -EOPNOTSUPP;
1670 
1671 	err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, &maxassoc);
1672 	if (err)
1673 		return err;
1674 	rrq->value = maxassoc;
1675 	rrq->fixed = 1;
1676 
1677 	return 0;
1678 }
1679 
1680 static const iw_handler zd1201_private_handler[] = {
1681 	(iw_handler) zd1201_set_hostauth,	/* ZD1201SIWHOSTAUTH */
1682 	(iw_handler) zd1201_get_hostauth,	/* ZD1201GIWHOSTAUTH */
1683 	(iw_handler) zd1201_auth_sta,		/* ZD1201SIWAUTHSTA */
1684 	(iw_handler) NULL,			/* nothing to get */
1685 	(iw_handler) zd1201_set_maxassoc,	/* ZD1201SIMAXASSOC */
1686 	(iw_handler) zd1201_get_maxassoc,	/* ZD1201GIMAXASSOC */
1687 };
1688 
1689 static const struct iw_priv_args zd1201_private_args[] = {
1690 	{ ZD1201SIWHOSTAUTH, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1691 	    IW_PRIV_TYPE_NONE, "sethostauth" },
1692 	{ ZD1201GIWHOSTAUTH, IW_PRIV_TYPE_NONE,
1693 	    IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostauth" },
1694 	{ ZD1201SIWAUTHSTA, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1,
1695 	    IW_PRIV_TYPE_NONE, "authstation" },
1696 	{ ZD1201SIWMAXASSOC, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1697 	    IW_PRIV_TYPE_NONE, "setmaxassoc" },
1698 	{ ZD1201GIWMAXASSOC, IW_PRIV_TYPE_NONE,
1699 	    IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmaxassoc" },
1700 };
1701 
1702 static const struct iw_handler_def zd1201_iw_handlers = {
1703 	.num_standard 		= ARRAY_SIZE(zd1201_iw_handler),
1704 	.num_private 		= ARRAY_SIZE(zd1201_private_handler),
1705 	.num_private_args 	= ARRAY_SIZE(zd1201_private_args),
1706 	.standard 		= (iw_handler *)zd1201_iw_handler,
1707 	.private 		= (iw_handler *)zd1201_private_handler,
1708 	.private_args 		= (struct iw_priv_args *) zd1201_private_args,
1709 	.get_wireless_stats	= zd1201_get_wireless_stats,
1710 };
1711 
1712 static const struct net_device_ops zd1201_netdev_ops = {
1713 	.ndo_open		= zd1201_net_open,
1714 	.ndo_stop		= zd1201_net_stop,
1715 	.ndo_start_xmit		= zd1201_hard_start_xmit,
1716 	.ndo_tx_timeout		= zd1201_tx_timeout,
1717 	.ndo_set_rx_mode	= zd1201_set_multicast,
1718 	.ndo_set_mac_address	= zd1201_set_mac_address,
1719 	.ndo_validate_addr	= eth_validate_addr,
1720 };
1721 
1722 static int zd1201_probe(struct usb_interface *interface,
1723 			const struct usb_device_id *id)
1724 {
1725 	struct zd1201 *zd;
1726 	struct net_device *dev;
1727 	struct usb_device *usb;
1728 	int err;
1729 	short porttype;
1730 	char buf[IW_ESSID_MAX_SIZE+2];
1731 	u8 addr[ETH_ALEN];
1732 
1733 	usb = interface_to_usbdev(interface);
1734 
1735 	dev = alloc_etherdev(sizeof(*zd));
1736 	if (!dev)
1737 		return -ENOMEM;
1738 	zd = netdev_priv(dev);
1739 	zd->dev = dev;
1740 
1741 	zd->ap = ap;
1742 	zd->usb = usb;
1743 	zd->removed = 0;
1744 	init_waitqueue_head(&zd->rxdataq);
1745 	INIT_HLIST_HEAD(&zd->fraglist);
1746 
1747 	err = zd1201_fw_upload(usb, zd->ap);
1748 	if (err) {
1749 		dev_err(&usb->dev, "zd1201 firmware upload failed: %d\n", err);
1750 		goto err_zd;
1751 	}
1752 
1753 	zd->endp_in = 1;
1754 	zd->endp_out = 1;
1755 	zd->endp_out2 = 2;
1756 	zd->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1757 	zd->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1758 	if (!zd->rx_urb || !zd->tx_urb) {
1759 		err = -ENOMEM;
1760 		goto err_zd;
1761 	}
1762 
1763 	mdelay(100);
1764 	err = zd1201_drvr_start(zd);
1765 	if (err)
1766 		goto err_zd;
1767 
1768 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXDATALEN, 2312);
1769 	if (err)
1770 		goto err_start;
1771 
1772 	err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL,
1773 	    ZD1201_RATEB1 | ZD1201_RATEB2 | ZD1201_RATEB5 | ZD1201_RATEB11);
1774 	if (err)
1775 		goto err_start;
1776 
1777 	dev->netdev_ops = &zd1201_netdev_ops;
1778 	dev->wireless_handlers = &zd1201_iw_handlers;
1779 	dev->watchdog_timeo = ZD1201_TX_TIMEOUT;
1780 	strcpy(dev->name, "wlan%d");
1781 
1782 	err = zd1201_getconfig(zd, ZD1201_RID_CNFOWNMACADDR, addr, ETH_ALEN);
1783 	if (err)
1784 		goto err_start;
1785 	eth_hw_addr_set(dev, addr);
1786 
1787 	/* Set wildcard essid to match zd->essid */
1788 	*(__le16 *)buf = cpu_to_le16(0);
1789 	err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
1790 	    IW_ESSID_MAX_SIZE+2, 1);
1791 	if (err)
1792 		goto err_start;
1793 
1794 	if (zd->ap)
1795 		porttype = ZD1201_PORTTYPE_AP;
1796 	else
1797 		porttype = ZD1201_PORTTYPE_BSS;
1798 	err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
1799 	if (err)
1800 		goto err_start;
1801 
1802 	SET_NETDEV_DEV(dev, &usb->dev);
1803 
1804 	err = register_netdev(dev);
1805 	if (err)
1806 		goto err_start;
1807 	dev_info(&usb->dev, "%s: ZD1201 USB Wireless interface\n",
1808 	    dev->name);
1809 
1810 	usb_set_intfdata(interface, zd);
1811 	zd1201_enable(zd);	/* zd1201 likes to startup enabled, */
1812 	zd1201_disable(zd);	/* interfering with all the wifis in range */
1813 	return 0;
1814 
1815 err_start:
1816 	/* Leave the device in reset state */
1817 	zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
1818 err_zd:
1819 	usb_free_urb(zd->tx_urb);
1820 	usb_free_urb(zd->rx_urb);
1821 	free_netdev(dev);
1822 	return err;
1823 }
1824 
1825 static void zd1201_disconnect(struct usb_interface *interface)
1826 {
1827 	struct zd1201 *zd = usb_get_intfdata(interface);
1828 	struct hlist_node *node2;
1829 	struct zd1201_frag *frag;
1830 
1831 	if (!zd)
1832 		return;
1833 	usb_set_intfdata(interface, NULL);
1834 
1835 	hlist_for_each_entry_safe(frag, node2, &zd->fraglist, fnode) {
1836 		hlist_del_init(&frag->fnode);
1837 		kfree_skb(frag->skb);
1838 		kfree(frag);
1839 	}
1840 
1841 	if (zd->tx_urb) {
1842 		usb_kill_urb(zd->tx_urb);
1843 		usb_free_urb(zd->tx_urb);
1844 	}
1845 	if (zd->rx_urb) {
1846 		usb_kill_urb(zd->rx_urb);
1847 		usb_free_urb(zd->rx_urb);
1848 	}
1849 
1850 	if (zd->dev) {
1851 		unregister_netdev(zd->dev);
1852 		free_netdev(zd->dev);
1853 	}
1854 }
1855 
1856 #ifdef CONFIG_PM
1857 
1858 static int zd1201_suspend(struct usb_interface *interface,
1859 			   pm_message_t message)
1860 {
1861 	struct zd1201 *zd = usb_get_intfdata(interface);
1862 
1863 	netif_device_detach(zd->dev);
1864 
1865 	zd->was_enabled = zd->mac_enabled;
1866 
1867 	if (zd->was_enabled)
1868 		return zd1201_disable(zd);
1869 	else
1870 		return 0;
1871 }
1872 
1873 static int zd1201_resume(struct usb_interface *interface)
1874 {
1875 	struct zd1201 *zd = usb_get_intfdata(interface);
1876 
1877 	if (!zd || !zd->dev)
1878 		return -ENODEV;
1879 
1880 	netif_device_attach(zd->dev);
1881 
1882 	if (zd->was_enabled)
1883 		return zd1201_enable(zd);
1884 	else
1885 		return 0;
1886 }
1887 
1888 #else
1889 
1890 #define zd1201_suspend NULL
1891 #define zd1201_resume  NULL
1892 
1893 #endif
1894 
1895 static struct usb_driver zd1201_usb = {
1896 	.name = "zd1201",
1897 	.probe = zd1201_probe,
1898 	.disconnect = zd1201_disconnect,
1899 	.id_table = zd1201_table,
1900 	.suspend = zd1201_suspend,
1901 	.resume = zd1201_resume,
1902 	.disable_hub_initiated_lpm = 1,
1903 };
1904 
1905 module_usb_driver(zd1201_usb);
1906