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