MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / net / Zydas / zdusb.c
blobfa47b2edeb3252b39ed00ead39fdc456d474b936
1 /* src/zdusb.c
3 * Implements the functions of the ZyDAS zd1211 MAC
5 * Copyright (C) 2004 ZyDAS Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
10 * The contents of this file are subject to the Mozilla Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * Alternatively, the contents of this file may be used under the
21 * terms of the GNU Public License version 2 (the "GPL"), in which
22 * case the provisions of the GPL are applicable instead of the
23 * above. If you wish to allow the use of your version of this file
24 * only under the terms of the GPL and not to allow others to use
25 * your version of this file under the MPL, indicate your decision
26 * by deleting the provisions above and replace them with the notice
27 * and other provisions required by the GPL. If you do not delete
28 * the provisions above, a recipient may use your version of this
29 * file under either the MPL or the GPL.
31 * -------------------------------------------------------------------- */
33 #include <linux/version.h>
35 #ifdef MODVERSIONS
36 #include <linux/modversions.h>
37 #endif
39 #include <linux/module.h>
41 #include <linux/usb.h>
43 #include "zd1205.h"
44 #include "zdusb.h"
45 #include "zddebug.h"
46 #include "zdversion.h"
47 #include "zd1211.h"
49 #define ZD1211_DBG_LEVEL 1
51 MODULE_AUTHOR("Yarco Yang");
52 MODULE_DESCRIPTION("ZyDAS 802.11b/g USB Wireless LAN adapter");
53 MODULE_LICENSE("GPL");
55 #ifdef ZD1211
56 static const char driver_name[] = "zd1211";
57 #elif defined(ZD1211B)
58 static const char driver_name[] = "zd1211b";
59 #endif
62 /* table of devices that work with this driver */
63 static struct usb_device_id zd1211_ids [] = {
64 #ifdef ZD1211B
65 { USB_DEVICE(VENDOR_ZYDAS, 0x1215) },
66 { USB_DEVICE(VENDOR_ZYDAS, 0xA215) },
67 { USB_DEVICE(0x0053, 0x5301) },
68 { USB_DEVICE(0x0053, 0x5302) },
69 { USB_DEVICE(0x2019, 0x5303) }, //Add, 2006.04.17
70 { USB_DEVICE(0x050D, 0x4050) },
71 { USB_DEVICE(0x050D, 0x705C) },
72 { USB_DEVICE(0x0586, 0x340F) },
73 { USB_DEVICE(0x079B, 0x0062) },
74 { USB_DEVICE(0x083A, 0x4505) },
75 { USB_DEVICE(0x083A, 0xE501) },
76 { USB_DEVICE(0x0BAF, 0x0121) },
77 { USB_DEVICE(0x0CDE, 0x001A) },
78 { USB_DEVICE(0x0DF6, 0x9075) },
79 { USB_DEVICE(0x0F88, 0x3014) },
80 { USB_DEVICE(0x1233, 0x0471) },
81 { USB_DEVICE(0x1582, 0x6003) },
82 #elif defined(ZD1211)
83 { USB_DEVICE(VENDOR_ZYDAS, PRODUCT_1211) },
84 { USB_DEVICE(VENDOR_ZYDAS, PRODUCT_A211) },
85 { USB_DEVICE(VENDOR_ZYXEL, PRODUCT_G220) },
86 { USB_DEVICE(VENDOR_3COM, PRODUCT_A727) },
87 { USB_DEVICE(0x2019, 0xc008) },
88 { USB_DEVICE(0x2019, 0xc009) },
89 { USB_DEVICE(0x079b, 0x004a) },
90 { USB_DEVICE(0x07b8, 0x6001) },
91 { USB_DEVICE(0x0b3b, 0x1630) },
92 { USB_DEVICE(0x0b3b, 0x5630) },
93 { USB_DEVICE(0x0b3b, 0x6630) },
94 { USB_DEVICE(0x0cde, 0x0011) },
95 { USB_DEVICE(0x0df6, 0x9071) },
96 { USB_DEVICE(0x126f, 0xa006) },
97 { USB_DEVICE(0x129b, 0x1666) },
98 { USB_DEVICE(0x1435, 0x0711) },
99 { USB_DEVICE(0x0DF6, 0x9071) },
100 { USB_DEVICE(0x0105, 0x145F) },
102 #endif
103 { } /* Terminating entry */
107 MODULE_DEVICE_TABLE(usb, zd1211_ids);
108 int zd1211_FirstLoad = 1;
109 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
110 module_param(zd1211_FirstLoad, int, S_IRUGO);
111 #else
112 MODULE_PARM(zd1211_FirstLoad, "i");
113 #endif
116 extern struct net_device *g_dev;
117 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
118 static void *zd1211_probe(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id)
119 #else
120 static int zd1211_probe(struct usb_interface *interface, const struct usb_device_id *id)
121 #endif
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
124 struct usb_interface *interface = &dev->actconfig->interface[ifnum];
125 #else
126 struct usb_device *dev = interface_to_usbdev(interface);
127 #endif
129 struct net_device *net = NULL;
130 struct zd1205_private *macp = NULL;
131 int vendor_id, product_id;
132 int dev_index = id - zd1211_ids;
133 int result = 0;
135 //char serial_number[30];
136 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
137 //Drivers for USB interfaces should normally record such references in
138 //their probe() methods, when they bind to an interface, and release
139 //them by calling usb_put_dev(), in their disconnect() methods.
140 //A pointer to the device with the incremented reference counter is returned.
141 usb_get_dev(dev);
142 #endif
144 vendor_id = zd_le16_to_cpu(dev->descriptor.idVendor);
145 product_id = zd_le16_to_cpu(dev->descriptor.idProduct);
147 #if 1
148 printk(KERN_NOTICE "vendor_id = %04x\n", vendor_id);
149 printk(KERN_NOTICE "product_id = %04x\n", product_id);
151 if (dev->speed == USB_SPEED_HIGH)
152 printk(KERN_NOTICE "USB 2.0 Host\n");
153 else
154 printk(KERN_NOTICE "USB 1.1 Host\n");
155 #endif
157 //memset(serial_number, 0, 30);
158 //usb_string(dev, dev->descriptor.iSerialNumber, serial_number, 29);
159 //printk("Device serial number is %s\n", serial_number);
161 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
162 if (usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
163 printk(KERN_ERR "usb_set_configuration() failed\n");
164 result = -EIO;
165 goto fail;
167 #endif
169 #if 1
170 //kernel 2.6
171 if (!(macp = kmalloc(sizeof(struct zd1205_private), GFP_KERNEL))) {
172 printk(KERN_ERR "out of memory allocating device structure\n");
173 result = -ENOMEM;
174 goto fail;
177 memset(macp, 0, sizeof(struct zd1205_private));
178 #endif
181 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
182 usb_inc_dev_use(dev);
183 #endif
185 net = alloc_etherdev(0); //kernel 2.6
186 //net = alloc_etherdev(sizeof (struct zd1205_private)); //kernel 2.4
188 if (!net) {
189 printk(KERN_ERR "zd1211: Not able to alloc etherdev struct\n");
190 result = -ENOMEM;
191 goto fail1;
194 g_dev = net; //save this for CBs use
195 //macp = net->priv; //kernel 2.4
196 net->priv = macp; //kernel 2.6
197 macp->device = net;
198 macp->usb = dev;
199 SET_MODULE_OWNER(net);
200 macp->dev_index = dev_index;
201 macp->release = zd_le16_to_cpu(dev->descriptor.bcdDevice);
202 printk(KERN_NOTICE "Release Ver = %04x\n", macp->release);
203 macp->flags = 0;
204 macp->dbg_flag = ZD1211_DBG_LEVEL;
206 /* set up the endpoint information */
207 /* check out the endpoints */
208 macp->interface = interface;
210 init_waitqueue_head(&macp->regSet_wait);
211 init_waitqueue_head(&macp->iorwRsp_wait);
212 init_waitqueue_head(&macp->term_wait);
213 init_waitqueue_head(&macp->msdelay);
215 if (!zd1211_alloc_all_urbs(macp)){
216 printk("Calling zd1211_alloc_all_urbs fails\n");
217 result = -ENOMEM;
218 goto fail2;
221 //zd1211_DownLoadUSBCode(macp, "WS11Uext.bin", NULL, cFIRMWARE_EXT_CODE);
222 //If the driver was removed and reinstall without unplug the device.
223 //You can tell the driver not to download the firmware again by issing
224 //insmod zd1211b zd1211_FirstDown=0. Redownload causes device crash
225 if(zd1211_FirstLoad)
227 if (zd1211_Download_IncludeFile(macp) != 0){
228 printk(KERN_ERR "zd1211_Download_IncludeFile failed\n");
229 result = -EIO;
230 goto fail3;
234 //to enable firmware
235 //#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
236 // if (usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
237 //#else
238 // if (usb_set_interface(dev, interface->altsetting[0].desc.bInterfaceNumber, 0)){
239 // Use the lowest USBD API to issue set_configuration command.
240 if ((usb_control_msg(dev, usb_sndctrlpipe(dev,0),USB_REQ_SET_CONFIGURATION,0, 1, 0, NULL, 0, HZ))<0)
242 //#endif
243 printk(KERN_ERR "usb_set_configuration() failed\n");
244 result = -EIO;
245 goto fail3;
248 set_bit(ZD1211_RUNNING, &macp->flags);
249 macp->bUSBDeveiceAttached = 1;
251 if (!zd1211_InitSetup(net, macp))
253 printk("Calling zd1211_InitSetup fails\n");
254 result = -EIO;
255 goto fail3;
257 else
259 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
260 usb_set_intfdata(interface, macp);
261 SET_NETDEV_DEV(net, &interface->dev);
262 //defer_kevent(macp, KEVENT_REGISTER_NET);
263 #endif
267 #if 1 //don't register net
268 if (register_netdev(net) != 0)
270 printk("register_netdev fails\n");
271 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
272 usb_set_intfdata(interface, NULL);
273 #endif
274 goto fail3;
276 #endif
279 goto done;
281 fail3:
282 zd1211_free_all_urbs(macp);
284 fail2:
285 free_netdev(net); //kernel 2.6
286 //kfree(net);
288 fail1:
289 kfree(macp);
291 fail:
292 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
293 usb_put_dev(dev);
294 #endif
295 macp = NULL;
296 goto exit;
297 done:
298 netif_carrier_off(macp->device);
299 exit:
300 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
301 return macp;
302 #else
303 return result;
304 #endif
307 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
308 static void zd1211_disconnect(struct usb_device *dev, void *ptr)
309 #else
310 static void zd1211_disconnect(struct usb_interface *interface)
311 #endif
313 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
314 struct zd1205_private *macp = (struct zd1205_private *) usb_get_intfdata(interface);
315 #else
316 struct zd1205_private *macp = (struct zd1205_private *)ptr;
317 #endif
319 if (!macp) {
320 printk(KERN_ERR "unregistering non-existant device\n");
321 return;
324 set_bit(ZD1211_UNPLUG, &macp->flags);
325 macp->bUSBDeveiceAttached = 0;
327 if (macp->driver_isolated){
328 if (macp->device->flags & IFF_UP)
329 zd1205_close(macp->device);
332 unregister_netdev(macp->device);
334 //assuming we used keventd, it must quiesce too
335 flush_scheduled_work();
336 mdelay(1000);
337 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
338 usb_dec_dev_use(dev);
339 #else
340 usb_put_dev(interface_to_usbdev(interface));
341 #endif
343 mdelay(1);
344 zd1211_unlink_all_urbs(macp);
345 mdelay(1);
346 zd1211_free_all_urbs(macp);
347 mdelay(1);
348 zd1205_clear_structs(macp->device);
349 kfree(macp);
350 macp = NULL;
351 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
352 usb_set_intfdata(interface, NULL);
353 #endif
354 //ZEXIT(0);
357 static struct usb_driver zd1211_driver = {
358 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
359 //.owner = THIS_MODULE,
360 #endif
361 .name = driver_name,
362 .probe = zd1211_probe,
363 .disconnect = zd1211_disconnect,
364 .id_table = zd1211_ids,
368 int __init zd1211_init(void)
370 printk(KERN_NOTICE "%s - version %s\n", DRIVER_NAME, VERSIONID);
371 return usb_register(&zd1211_driver);
374 void __exit zd1211_exit(void)
376 usb_deregister(&zd1211_driver);
379 module_init(zd1211_init);
380 module_exit(zd1211_exit);