]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/usb/pegasus.c
USB: protect hcd.h from multiple inclusions
[net-next-2.6.git] / drivers / net / usb / pegasus.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ChangeLog:
9 * .... Most of the time spent on reading sources & docs.
10 * v0.2.x First official release for the Linux kernel.
11 * v0.3.0 Beutified and structured, some bugs fixed.
12 * v0.3.x URBifying bulk requests and bugfixing. First relatively
13 * stable release. Still can touch device's registers only
14 * from top-halves.
15 * v0.4.0 Control messages remained unurbified are now URBs.
16 * Now we can touch the HW at any time.
17 * v0.4.9 Control urbs again use process context to wait. Argh...
18 * Some long standing bugs (enable_net_traffic) fixed.
19 * Also nasty trick about resubmiting control urb from
20 * interrupt context used. Please let me know how it
21 * behaves. Pegasus II support added since this version.
22 * TODO: suppressing HCD warnings spewage on disconnect.
23 * v0.4.13 Ethernet address is now set at probe(), not at open()
24 * time as this seems to break dhcpd.
25 * v0.5.0 branch to 2.5.x kernels
26 * v0.5.1 ethtool support added
27 * v0.5.5 rx socket buffers are in a pool and the their allocation
28 * is out of the interrupt routine.
29 */
30
1da177e4
LT
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/ethtool.h>
38#include <linux/mii.h>
39#include <linux/usb.h>
40#include <linux/module.h>
41#include <asm/byteorder.h>
42#include <asm/uaccess.h>
43#include "pegasus.h"
44
45/*
46 * Version Information
47 */
37cf3477 48#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
1da177e4
LT
49#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
51
52static const char driver_name[] = "pegasus";
53
54#undef PEGASUS_WRITE_EEPROM
55#define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56 BMSR_100FULL | BMSR_ANEGCAPABLE)
57
58static int loopback = 0;
59static int mii_mode = 0;
a4f81a61 60static char *devid=NULL;
1da177e4
LT
61
62static struct usb_eth_dev usb_dev_id[] = {
63#define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
65#include "pegasus.h"
66#undef PEGASUS_DEV
a4f81a61 67 {NULL, 0, 0, 0},
1da177e4
LT
68 {NULL, 0, 0, 0}
69};
70
71static struct usb_device_id pegasus_ids[] = {
72#define PEGASUS_DEV(pn, vid, pid, flags) \
73 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
74#include "pegasus.h"
75#undef PEGASUS_DEV
a4f81a61 76 {},
1da177e4
LT
77 {}
78};
79
80MODULE_AUTHOR(DRIVER_AUTHOR);
81MODULE_DESCRIPTION(DRIVER_DESC);
82MODULE_LICENSE("GPL");
83module_param(loopback, bool, 0);
84module_param(mii_mode, bool, 0);
a4f81a61 85module_param(devid, charp, 0);
1da177e4
LT
86MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
a4f81a61 88MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
1da177e4
LT
89
90/* use ethtool to change the level for any given device */
91static int msg_level = -1;
92module_param (msg_level, int, 0);
93MODULE_PARM_DESC (msg_level, "Override default message level");
94
95MODULE_DEVICE_TABLE(usb, pegasus_ids);
96
97static int update_eth_regs_async(pegasus_t *);
98/* Aargh!!! I _really_ hate such tweaks */
7d12e780 99static void ctrl_callback(struct urb *urb)
1da177e4
LT
100{
101 pegasus_t *pegasus = urb->context;
c94cb314 102 int status = urb->status;
1da177e4
LT
103
104 if (!pegasus)
105 return;
106
c94cb314 107 switch (status) {
1da177e4
LT
108 case 0:
109 if (pegasus->flags & ETH_REGS_CHANGE) {
110 pegasus->flags &= ~ETH_REGS_CHANGE;
111 pegasus->flags |= ETH_REGS_CHANGED;
112 update_eth_regs_async(pegasus);
113 return;
114 }
115 break;
116 case -EINPROGRESS:
117 return;
118 case -ENOENT:
119 break;
120 default:
e000ea13 121 if (netif_msg_drv(pegasus) && printk_ratelimit())
4a1728a2 122 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
c94cb314 123 __func__, status);
1da177e4
LT
124 }
125 pegasus->flags &= ~ETH_REGS_CHANGED;
126 wake_up(&pegasus->ctrl_wait);
127}
128
129static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
130 void *data)
131{
132 int ret;
133 char *buffer;
134 DECLARE_WAITQUEUE(wait, current);
135
136 buffer = kmalloc(size, GFP_KERNEL);
137 if (!buffer) {
138 if (netif_msg_drv(pegasus))
139 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
b39d66a8 140 __func__);
1da177e4
LT
141 return -ENOMEM;
142 }
143 add_wait_queue(&pegasus->ctrl_wait, &wait);
144 set_current_state(TASK_UNINTERRUPTIBLE);
145 while (pegasus->flags & ETH_REGS_CHANGED)
146 schedule();
147 remove_wait_queue(&pegasus->ctrl_wait, &wait);
148 set_current_state(TASK_RUNNING);
149
150 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
151 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
152 pegasus->dr.wValue = cpu_to_le16(0);
153 pegasus->dr.wIndex = cpu_to_le16p(&indx);
154 pegasus->dr.wLength = cpu_to_le16p(&size);
155 pegasus->ctrl_urb->transfer_buffer_length = size;
156
157 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
158 usb_rcvctrlpipe(pegasus->usb, 0),
159 (char *) &pegasus->dr,
160 buffer, size, ctrl_callback, pegasus);
161
162 add_wait_queue(&pegasus->ctrl_wait, &wait);
163 set_current_state(TASK_UNINTERRUPTIBLE);
164
165 /* using ATOMIC, we'd never wake up if we slept */
166 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
10c8211c 167 set_current_state(TASK_RUNNING);
955a2608
DB
168 if (ret == -ENODEV)
169 netif_device_detach(pegasus->net);
e000ea13 170 if (netif_msg_drv(pegasus) && printk_ratelimit())
1da177e4 171 dev_err(&pegasus->intf->dev, "%s, status %d\n",
653c0316 172 __func__, ret);
1da177e4
LT
173 goto out;
174 }
175
176 schedule();
177out:
178 remove_wait_queue(&pegasus->ctrl_wait, &wait);
179 memcpy(data, buffer, size);
180 kfree(buffer);
181
182 return ret;
183}
184
185static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
186 void *data)
187{
188 int ret;
189 char *buffer;
190 DECLARE_WAITQUEUE(wait, current);
191
192 buffer = kmalloc(size, GFP_KERNEL);
193 if (!buffer) {
194 if (netif_msg_drv(pegasus))
195 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
653c0316 196 __func__);
1da177e4
LT
197 return -ENOMEM;
198 }
199 memcpy(buffer, data, size);
200
201 add_wait_queue(&pegasus->ctrl_wait, &wait);
202 set_current_state(TASK_UNINTERRUPTIBLE);
203 while (pegasus->flags & ETH_REGS_CHANGED)
204 schedule();
205 remove_wait_queue(&pegasus->ctrl_wait, &wait);
206 set_current_state(TASK_RUNNING);
207
208 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
209 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
210 pegasus->dr.wValue = cpu_to_le16(0);
211 pegasus->dr.wIndex = cpu_to_le16p(&indx);
212 pegasus->dr.wLength = cpu_to_le16p(&size);
213 pegasus->ctrl_urb->transfer_buffer_length = size;
214
215 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
216 usb_sndctrlpipe(pegasus->usb, 0),
217 (char *) &pegasus->dr,
218 buffer, size, ctrl_callback, pegasus);
219
220 add_wait_queue(&pegasus->ctrl_wait, &wait);
221 set_current_state(TASK_UNINTERRUPTIBLE);
222
223 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
955a2608
DB
224 if (ret == -ENODEV)
225 netif_device_detach(pegasus->net);
1da177e4
LT
226 if (netif_msg_drv(pegasus))
227 dev_err(&pegasus->intf->dev, "%s, status %d\n",
b39d66a8 228 __func__, ret);
1da177e4
LT
229 goto out;
230 }
231
232 schedule();
233out:
234 remove_wait_queue(&pegasus->ctrl_wait, &wait);
235 kfree(buffer);
236
237 return ret;
238}
239
240static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
241{
242 int ret;
243 char *tmp;
244 DECLARE_WAITQUEUE(wait, current);
245
246 tmp = kmalloc(1, GFP_KERNEL);
247 if (!tmp) {
248 if (netif_msg_drv(pegasus))
249 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
b39d66a8 250 __func__);
1da177e4
LT
251 return -ENOMEM;
252 }
253 memcpy(tmp, &data, 1);
254 add_wait_queue(&pegasus->ctrl_wait, &wait);
255 set_current_state(TASK_UNINTERRUPTIBLE);
256 while (pegasus->flags & ETH_REGS_CHANGED)
257 schedule();
258 remove_wait_queue(&pegasus->ctrl_wait, &wait);
259 set_current_state(TASK_RUNNING);
260
261 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
262 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
263 pegasus->dr.wValue = cpu_to_le16(data);
264 pegasus->dr.wIndex = cpu_to_le16p(&indx);
265 pegasus->dr.wLength = cpu_to_le16(1);
266 pegasus->ctrl_urb->transfer_buffer_length = 1;
267
268 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
269 usb_sndctrlpipe(pegasus->usb, 0),
270 (char *) &pegasus->dr,
016534cf 271 tmp, 1, ctrl_callback, pegasus);
1da177e4
LT
272
273 add_wait_queue(&pegasus->ctrl_wait, &wait);
274 set_current_state(TASK_UNINTERRUPTIBLE);
275
276 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
955a2608
DB
277 if (ret == -ENODEV)
278 netif_device_detach(pegasus->net);
e000ea13 279 if (netif_msg_drv(pegasus) && printk_ratelimit())
1da177e4 280 dev_err(&pegasus->intf->dev, "%s, status %d\n",
b39d66a8 281 __func__, ret);
1da177e4
LT
282 goto out;
283 }
284
285 schedule();
286out:
287 remove_wait_queue(&pegasus->ctrl_wait, &wait);
288 kfree(tmp);
289
290 return ret;
291}
292
293static int update_eth_regs_async(pegasus_t * pegasus)
294{
295 int ret;
296
297 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
298 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
299 pegasus->dr.wValue = 0;
300 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
301 pegasus->dr.wLength = cpu_to_le16(3);
302 pegasus->ctrl_urb->transfer_buffer_length = 3;
303
304 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
305 usb_sndctrlpipe(pegasus->usb, 0),
306 (char *) &pegasus->dr,
307 pegasus->eth_regs, 3, ctrl_callback, pegasus);
308
955a2608
DB
309 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
310 if (ret == -ENODEV)
311 netif_device_detach(pegasus->net);
1da177e4
LT
312 if (netif_msg_drv(pegasus))
313 dev_err(&pegasus->intf->dev, "%s, status %d\n",
b39d66a8 314 __func__, ret);
955a2608 315 }
1da177e4
LT
316
317 return ret;
318}
319
c43c49bd 320/* Returns 0 on success, error on failure */
1da177e4
LT
321static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
322{
323 int i;
324 __u8 data[4] = { phy, 0, 0, indx };
325 __le16 regdi;
326 int ret;
327
4a1728a2
PM
328 set_register(pegasus, PhyCtrl, 0);
329 set_registers(pegasus, PhyAddr, sizeof (data), data);
330 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
1da177e4
LT
331 for (i = 0; i < REG_TIMEOUT; i++) {
332 ret = get_registers(pegasus, PhyCtrl, 1, data);
7e713b82
DB
333 if (ret == -ESHUTDOWN)
334 goto fail;
1da177e4
LT
335 if (data[0] & PHY_DONE)
336 break;
337 }
338 if (i < REG_TIMEOUT) {
339 ret = get_registers(pegasus, PhyData, 2, &regdi);
340 *regd = le16_to_cpu(regdi);
4a1728a2 341 return ret;
1da177e4 342 }
7e713b82 343fail:
1da177e4 344 if (netif_msg_drv(pegasus))
b39d66a8 345 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
1da177e4 346
4a1728a2 347 return ret;
1da177e4
LT
348}
349
350static int mdio_read(struct net_device *dev, int phy_id, int loc)
351{
352 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
353 u16 res;
354
355 read_mii_word(pegasus, phy_id, loc, &res);
356 return (int)res;
357}
358
359static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
360{
361 int i;
362 __u8 data[4] = { phy, 0, 0, indx };
363 int ret;
364
365 data[1] = (u8) regd;
366 data[2] = (u8) (regd >> 8);
4a1728a2
PM
367 set_register(pegasus, PhyCtrl, 0);
368 set_registers(pegasus, PhyAddr, sizeof(data), data);
369 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
1da177e4
LT
370 for (i = 0; i < REG_TIMEOUT; i++) {
371 ret = get_registers(pegasus, PhyCtrl, 1, data);
7e713b82
DB
372 if (ret == -ESHUTDOWN)
373 goto fail;
1da177e4
LT
374 if (data[0] & PHY_DONE)
375 break;
376 }
377 if (i < REG_TIMEOUT)
4a1728a2 378 return ret;
1da177e4 379
7e713b82 380fail:
1da177e4 381 if (netif_msg_drv(pegasus))
b39d66a8 382 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
4a1728a2 383 return -ETIMEDOUT;
1da177e4
LT
384}
385
386static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
387{
388 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
389
390 write_mii_word(pegasus, phy_id, loc, val);
391}
392
393static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
394{
395 int i;
396 __u8 tmp;
397 __le16 retdatai;
398 int ret;
399
4a1728a2
PM
400 set_register(pegasus, EpromCtrl, 0);
401 set_register(pegasus, EpromOffset, index);
402 set_register(pegasus, EpromCtrl, EPROM_READ);
1da177e4
LT
403
404 for (i = 0; i < REG_TIMEOUT; i++) {
405 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
406 if (tmp & EPROM_DONE)
407 break;
7e713b82
DB
408 if (ret == -ESHUTDOWN)
409 goto fail;
1da177e4
LT
410 }
411 if (i < REG_TIMEOUT) {
412 ret = get_registers(pegasus, EpromData, 2, &retdatai);
413 *retdata = le16_to_cpu(retdatai);
4a1728a2 414 return ret;
1da177e4
LT
415 }
416
7e713b82 417fail:
1da177e4 418 if (netif_msg_drv(pegasus))
b39d66a8 419 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
4a1728a2 420 return -ETIMEDOUT;
1da177e4
LT
421}
422
423#ifdef PEGASUS_WRITE_EEPROM
424static inline void enable_eprom_write(pegasus_t * pegasus)
425{
426 __u8 tmp;
427 int ret;
428
4a1728a2
PM
429 get_registers(pegasus, EthCtrl2, 1, &tmp);
430 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
1da177e4
LT
431}
432
433static inline void disable_eprom_write(pegasus_t * pegasus)
434{
435 __u8 tmp;
436 int ret;
437
4a1728a2
PM
438 get_registers(pegasus, EthCtrl2, 1, &tmp);
439 set_register(pegasus, EpromCtrl, 0);
440 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
1da177e4
LT
441}
442
443static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
444{
445 int i;
446 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
447 int ret;
448
4a1728a2 449 set_registers(pegasus, EpromOffset, 4, d);
1da177e4 450 enable_eprom_write(pegasus);
4a1728a2
PM
451 set_register(pegasus, EpromOffset, index);
452 set_registers(pegasus, EpromData, 2, &data);
453 set_register(pegasus, EpromCtrl, EPROM_WRITE);
1da177e4
LT
454
455 for (i = 0; i < REG_TIMEOUT; i++) {
456 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
7e713b82
DB
457 if (ret == -ESHUTDOWN)
458 goto fail;
1da177e4
LT
459 if (tmp & EPROM_DONE)
460 break;
461 }
462 disable_eprom_write(pegasus);
463 if (i < REG_TIMEOUT)
4a1728a2 464 return ret;
7e713b82 465fail:
1da177e4 466 if (netif_msg_drv(pegasus))
b39d66a8 467 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
4a1728a2 468 return -ETIMEDOUT;
1da177e4
LT
469}
470#endif /* PEGASUS_WRITE_EEPROM */
471
472static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
473{
474 int i;
475 __u16 w16;
476
477 for (i = 0; i < 3; i++) {
478 read_eprom_word(pegasus, i, &w16);
479 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
480 }
481}
482
483static void set_ethernet_addr(pegasus_t * pegasus)
484{
485 __u8 node_id[6];
1da177e4 486
37cf3477
PM
487 if (pegasus->features & PEGASUS_II) {
488 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
489 } else {
490 get_node_id(pegasus, node_id);
491 set_registers(pegasus, EthID, sizeof (node_id), node_id);
492 }
1da177e4
LT
493 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
494}
495
496static inline int reset_mac(pegasus_t * pegasus)
497{
498 __u8 data = 0x8;
499 int i;
1da177e4 500
4a1728a2 501 set_register(pegasus, EthCtrl1, data);
1da177e4 502 for (i = 0; i < REG_TIMEOUT; i++) {
4a1728a2 503 get_registers(pegasus, EthCtrl1, 1, &data);
1da177e4
LT
504 if (~data & 0x08) {
505 if (loopback & 1)
506 break;
507 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
4a1728a2 508 set_register(pegasus, Gpio1, 0x34);
1da177e4 509 else
4a1728a2
PM
510 set_register(pegasus, Gpio1, 0x26);
511 set_register(pegasus, Gpio0, pegasus->features);
512 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
1da177e4
LT
513 break;
514 }
515 }
516 if (i == REG_TIMEOUT)
4a1728a2 517 return -ETIMEDOUT;
1da177e4
LT
518
519 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
520 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
4a1728a2
PM
521 set_register(pegasus, Gpio0, 0x24);
522 set_register(pegasus, Gpio0, 0x26);
1da177e4
LT
523 }
524 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
525 __u16 auxmode;
526 read_mii_word(pegasus, 3, 0x1b, &auxmode);
527 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
528 }
529
530 return 0;
531}
532
533static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
534{
535 __u16 linkpart;
536 __u8 data[4];
537 pegasus_t *pegasus = netdev_priv(dev);
538 int ret;
539
540 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
541 data[0] = 0xc9;
542 data[1] = 0;
543 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
544 data[1] |= 0x20; /* set full duplex */
545 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
546 data[1] |= 0x10; /* set 100 Mbps */
547 if (mii_mode)
548 data[1] = 0;
549 data[2] = (loopback & 1) ? 0x09 : 0x01;
550
551 memcpy(pegasus->eth_regs, data, sizeof (data));
552 ret = set_registers(pegasus, EthCtrl0, 3, data);
553
554 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
efafe6fb 555 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
1da177e4
LT
556 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
557 u16 auxmode;
558 read_mii_word(pegasus, 0, 0x1b, &auxmode);
559 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
560 }
561
4a1728a2 562 return ret;
1da177e4
LT
563}
564
565static void fill_skb_pool(pegasus_t * pegasus)
566{
567 int i;
568
569 for (i = 0; i < RX_SKBS; i++) {
570 if (pegasus->rx_pool[i])
571 continue;
572 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
573 /*
574 ** we give up if the allocation fail. the tasklet will be
575 ** rescheduled again anyway...
576 */
577 if (pegasus->rx_pool[i] == NULL)
578 return;
1da177e4
LT
579 skb_reserve(pegasus->rx_pool[i], 2);
580 }
581}
582
583static void free_skb_pool(pegasus_t * pegasus)
584{
585 int i;
586
587 for (i = 0; i < RX_SKBS; i++) {
588 if (pegasus->rx_pool[i]) {
589 dev_kfree_skb(pegasus->rx_pool[i]);
590 pegasus->rx_pool[i] = NULL;
591 }
592 }
593}
594
595static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
596{
597 int i;
598 struct sk_buff *skb;
599
600 for (i = 0; i < RX_SKBS; i++) {
601 if (likely(pegasus->rx_pool[i] != NULL)) {
602 skb = pegasus->rx_pool[i];
603 pegasus->rx_pool[i] = NULL;
604 return skb;
605 }
606 }
607 return NULL;
608}
609
7d12e780 610static void read_bulk_callback(struct urb *urb)
1da177e4
LT
611{
612 pegasus_t *pegasus = urb->context;
613 struct net_device *net;
614 int rx_status, count = urb->actual_length;
c94cb314 615 int status = urb->status;
1da177e4
LT
616 u8 *buf = urb->transfer_buffer;
617 __u16 pkt_len;
618
619 if (!pegasus)
620 return;
621
622 net = pegasus->net;
623 if (!netif_device_present(net) || !netif_running(net))
624 return;
625
c94cb314 626 switch (status) {
1da177e4
LT
627 case 0:
628 break;
38e2bfc9 629 case -ETIME:
1da177e4
LT
630 if (netif_msg_rx_err(pegasus))
631 pr_debug("%s: reset MAC\n", net->name);
632 pegasus->flags &= ~PEGASUS_RX_BUSY;
633 break;
634 case -EPIPE: /* stall, or disconnect from TT */
635 /* FIXME schedule work to clear the halt */
636 if (netif_msg_rx_err(pegasus))
637 printk(KERN_WARNING "%s: no rx stall recovery\n",
638 net->name);
639 return;
640 case -ENOENT:
641 case -ECONNRESET:
642 case -ESHUTDOWN:
643 if (netif_msg_ifdown(pegasus))
c94cb314 644 pr_debug("%s: rx unlink, %d\n", net->name, status);
1da177e4
LT
645 return;
646 default:
647 if (netif_msg_rx_err(pegasus))
c94cb314 648 pr_debug("%s: RX status %d\n", net->name, status);
1da177e4
LT
649 goto goon;
650 }
651
652 if (!count || count < 4)
653 goto goon;
654
655 rx_status = buf[count - 2];
656 if (rx_status & 0x1e) {
657 if (netif_msg_rx_err(pegasus))
658 pr_debug("%s: RX packet error %x\n",
659 net->name, rx_status);
660 pegasus->stats.rx_errors++;
661 if (rx_status & 0x06) // long or runt
662 pegasus->stats.rx_length_errors++;
663 if (rx_status & 0x08)
664 pegasus->stats.rx_crc_errors++;
665 if (rx_status & 0x10) // extra bits
666 pegasus->stats.rx_frame_errors++;
667 goto goon;
668 }
669 if (pegasus->chip == 0x8513) {
670 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
671 pkt_len &= 0x0fff;
672 pegasus->rx_skb->data += 2;
673 } else {
674 pkt_len = buf[count - 3] << 8;
675 pkt_len += buf[count - 4];
676 pkt_len &= 0xfff;
677 pkt_len -= 8;
678 }
679
a85a46f2
KV
680 /*
681 * If the packet is unreasonably long, quietly drop it rather than
682 * kernel panicing by calling skb_put.
683 */
684 if (pkt_len > PEGASUS_MTU)
685 goto goon;
686
1da177e4
LT
687 /*
688 * at this point we are sure pegasus->rx_skb != NULL
689 * so we go ahead and pass up the packet.
690 */
691 skb_put(pegasus->rx_skb, pkt_len);
692 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
693 netif_rx(pegasus->rx_skb);
694 pegasus->stats.rx_packets++;
695 pegasus->stats.rx_bytes += pkt_len;
696
697 if (pegasus->flags & PEGASUS_UNPLUG)
698 return;
699
700 spin_lock(&pegasus->rx_pool_lock);
701 pegasus->rx_skb = pull_skb(pegasus);
702 spin_unlock(&pegasus->rx_pool_lock);
703
704 if (pegasus->rx_skb == NULL)
705 goto tl_sched;
706goon:
707 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
708 usb_rcvbulkpipe(pegasus->usb, 1),
709 pegasus->rx_skb->data, PEGASUS_MTU + 8,
710 read_bulk_callback, pegasus);
955a2608
DB
711 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
712 if (rx_status == -ENODEV)
713 netif_device_detach(pegasus->net);
714 else if (rx_status) {
1da177e4
LT
715 pegasus->flags |= PEGASUS_RX_URB_FAIL;
716 goto tl_sched;
717 } else {
718 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
719 }
720
721 return;
722
723tl_sched:
724 tasklet_schedule(&pegasus->rx_tl);
725}
726
727static void rx_fixup(unsigned long data)
728{
729 pegasus_t *pegasus;
730 unsigned long flags;
955a2608 731 int status;
1da177e4
LT
732
733 pegasus = (pegasus_t *) data;
734 if (pegasus->flags & PEGASUS_UNPLUG)
735 return;
736
737 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
738 fill_skb_pool(pegasus);
739 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
740 if (pegasus->rx_skb)
741 goto try_again;
742 if (pegasus->rx_skb == NULL) {
743 pegasus->rx_skb = pull_skb(pegasus);
744 }
745 if (pegasus->rx_skb == NULL) {
746 if (netif_msg_rx_err(pegasus))
747 printk(KERN_WARNING "%s: low on memory\n",
748 pegasus->net->name);
749 tasklet_schedule(&pegasus->rx_tl);
750 goto done;
751 }
752 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
753 usb_rcvbulkpipe(pegasus->usb, 1),
754 pegasus->rx_skb->data, PEGASUS_MTU + 8,
755 read_bulk_callback, pegasus);
756try_again:
955a2608
DB
757 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
758 if (status == -ENODEV)
759 netif_device_detach(pegasus->net);
760 else if (status) {
1da177e4
LT
761 pegasus->flags |= PEGASUS_RX_URB_FAIL;
762 tasklet_schedule(&pegasus->rx_tl);
763 } else {
764 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
765 }
766done:
767 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
768}
769
7d12e780 770static void write_bulk_callback(struct urb *urb)
1da177e4
LT
771{
772 pegasus_t *pegasus = urb->context;
9351982b 773 struct net_device *net;
c94cb314 774 int status = urb->status;
1da177e4
LT
775
776 if (!pegasus)
777 return;
778
9351982b
MG
779 net = pegasus->net;
780
1da177e4
LT
781 if (!netif_device_present(net) || !netif_running(net))
782 return;
783
c94cb314 784 switch (status) {
1da177e4
LT
785 case -EPIPE:
786 /* FIXME schedule_work() to clear the tx halt */
787 netif_stop_queue(net);
788 if (netif_msg_tx_err(pegasus))
789 printk(KERN_WARNING "%s: no tx stall recovery\n",
790 net->name);
791 return;
792 case -ENOENT:
793 case -ECONNRESET:
794 case -ESHUTDOWN:
795 if (netif_msg_ifdown(pegasus))
c94cb314 796 pr_debug("%s: tx unlink, %d\n", net->name, status);
1da177e4
LT
797 return;
798 default:
799 if (netif_msg_tx_err(pegasus))
c94cb314 800 pr_info("%s: TX status %d\n", net->name, status);
1da177e4
LT
801 /* FALL THROUGH */
802 case 0:
803 break;
804 }
805
806 net->trans_start = jiffies;
807 netif_wake_queue(net);
808}
809
7d12e780 810static void intr_callback(struct urb *urb)
1da177e4
LT
811{
812 pegasus_t *pegasus = urb->context;
813 struct net_device *net;
c94cb314 814 int res, status = urb->status;
1da177e4
LT
815
816 if (!pegasus)
817 return;
818 net = pegasus->net;
819
c94cb314 820 switch (status) {
1da177e4
LT
821 case 0:
822 break;
823 case -ECONNRESET: /* unlink */
824 case -ENOENT:
825 case -ESHUTDOWN:
826 return;
827 default:
828 /* some Pegasus-I products report LOTS of data
829 * toggle errors... avoid log spamming
830 */
831 if (netif_msg_timer(pegasus))
832 pr_debug("%s: intr status %d\n", net->name,
c94cb314 833 status);
1da177e4
LT
834 }
835
836 if (urb->actual_length >= 6) {
837 u8 * d = urb->transfer_buffer;
838
839 /* byte 0 == tx_status1, reg 2B */
840 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
841 |LATE_COL|JABBER_TIMEOUT)) {
842 pegasus->stats.tx_errors++;
843 if (d[0] & TX_UNDERRUN)
844 pegasus->stats.tx_fifo_errors++;
845 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
846 pegasus->stats.tx_aborted_errors++;
847 if (d[0] & LATE_COL)
848 pegasus->stats.tx_window_errors++;
849 }
850
851 /* d[5].LINK_STATUS lies on some adapters.
852 * d[0].NO_CARRIER kicks in only with failed TX.
853 * ... so monitoring with MII may be safest.
854 */
1da177e4
LT
855
856 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
857 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
858 }
859
c94cb314
ON
860 res = usb_submit_urb(urb, GFP_ATOMIC);
861 if (res == -ENODEV)
955a2608 862 netif_device_detach(pegasus->net);
c94cb314 863 if (res && netif_msg_timer(pegasus))
1da177e4 864 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
c94cb314 865 net->name, res);
1da177e4
LT
866}
867
868static void pegasus_tx_timeout(struct net_device *net)
869{
870 pegasus_t *pegasus = netdev_priv(net);
871 if (netif_msg_timer(pegasus))
872 printk(KERN_WARNING "%s: tx timeout\n", net->name);
1da177e4
LT
873 usb_unlink_urb(pegasus->tx_urb);
874 pegasus->stats.tx_errors++;
875}
876
877static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
878{
879 pegasus_t *pegasus = netdev_priv(net);
880 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
881 int res;
882 __u16 l16 = skb->len;
883
884 netif_stop_queue(net);
885
886 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
d626f62b 887 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
1da177e4
LT
888 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
889 usb_sndbulkpipe(pegasus->usb, 2),
890 pegasus->tx_buff, count,
891 write_bulk_callback, pegasus);
892 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
893 if (netif_msg_tx_err(pegasus))
894 printk(KERN_WARNING "%s: fail tx, %d\n",
895 net->name, res);
896 switch (res) {
897 case -EPIPE: /* stall, or disconnect from TT */
898 /* cleanup should already have been scheduled */
899 break;
900 case -ENODEV: /* disconnect() upcoming */
955a2608 901 netif_device_detach(pegasus->net);
1da177e4
LT
902 break;
903 default:
904 pegasus->stats.tx_errors++;
905 netif_start_queue(net);
906 }
907 } else {
908 pegasus->stats.tx_packets++;
909 pegasus->stats.tx_bytes += skb->len;
910 net->trans_start = jiffies;
911 }
912 dev_kfree_skb(skb);
913
914 return 0;
915}
916
917static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
918{
919 return &((pegasus_t *) netdev_priv(dev))->stats;
920}
921
922static inline void disable_net_traffic(pegasus_t * pegasus)
923{
924 int tmp = 0;
1da177e4 925
4a1728a2 926 set_registers(pegasus, EthCtrl0, 2, &tmp);
1da177e4
LT
927}
928
929static inline void get_interrupt_interval(pegasus_t * pegasus)
930{
931 __u8 data[2];
932
933 read_eprom_word(pegasus, 4, (__u16 *) data);
a85a46f2
KV
934 if (pegasus->usb->speed != USB_SPEED_HIGH) {
935 if (data[1] < 0x80) {
936 if (netif_msg_timer(pegasus))
937 dev_info(&pegasus->intf->dev, "intr interval "
938 "changed from %ums to %ums\n",
939 data[1], 0x80);
940 data[1] = 0x80;
941#ifdef PEGASUS_WRITE_EEPROM
942 write_eprom_word(pegasus, 4, *(__u16 *) data);
1da177e4 943#endif
a85a46f2 944 }
1da177e4
LT
945 }
946 pegasus->intr_interval = data[1];
947}
948
949static void set_carrier(struct net_device *net)
950{
951 pegasus_t *pegasus = netdev_priv(net);
952 u16 tmp;
953
c43c49bd 954 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
1da177e4 955 return;
a85a46f2 956
1da177e4
LT
957 if (tmp & BMSR_LSTATUS)
958 netif_carrier_on(net);
959 else
960 netif_carrier_off(net);
961}
962
963static void free_all_urbs(pegasus_t * pegasus)
964{
965 usb_free_urb(pegasus->intr_urb);
966 usb_free_urb(pegasus->tx_urb);
967 usb_free_urb(pegasus->rx_urb);
968 usb_free_urb(pegasus->ctrl_urb);
969}
970
971static void unlink_all_urbs(pegasus_t * pegasus)
972{
973 usb_kill_urb(pegasus->intr_urb);
974 usb_kill_urb(pegasus->tx_urb);
975 usb_kill_urb(pegasus->rx_urb);
976 usb_kill_urb(pegasus->ctrl_urb);
977}
978
979static int alloc_urbs(pegasus_t * pegasus)
980{
981 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
982 if (!pegasus->ctrl_urb) {
983 return 0;
984 }
985 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
986 if (!pegasus->rx_urb) {
987 usb_free_urb(pegasus->ctrl_urb);
988 return 0;
989 }
990 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!pegasus->tx_urb) {
992 usb_free_urb(pegasus->rx_urb);
993 usb_free_urb(pegasus->ctrl_urb);
994 return 0;
995 }
996 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
997 if (!pegasus->intr_urb) {
998 usb_free_urb(pegasus->tx_urb);
999 usb_free_urb(pegasus->rx_urb);
1000 usb_free_urb(pegasus->ctrl_urb);
1001 return 0;
1002 }
1003
1004 return 1;
1005}
1006
1007static int pegasus_open(struct net_device *net)
1008{
1009 pegasus_t *pegasus = netdev_priv(net);
1010 int res;
1011
1012 if (pegasus->rx_skb == NULL)
1013 pegasus->rx_skb = pull_skb(pegasus);
1014 /*
1015 ** Note: no point to free the pool. it is empty :-)
1016 */
1017 if (!pegasus->rx_skb)
1018 return -ENOMEM;
1019
1020 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1021
1022 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1023 usb_rcvbulkpipe(pegasus->usb, 1),
1024 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1025 read_bulk_callback, pegasus);
1026 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
955a2608
DB
1027 if (res == -ENODEV)
1028 netif_device_detach(pegasus->net);
1da177e4
LT
1029 if (netif_msg_ifup(pegasus))
1030 pr_debug("%s: failed rx_urb, %d", net->name, res);
1031 goto exit;
1032 }
1033
1034 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1035 usb_rcvintpipe(pegasus->usb, 3),
1036 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1037 intr_callback, pegasus, pegasus->intr_interval);
1038 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
955a2608
DB
1039 if (res == -ENODEV)
1040 netif_device_detach(pegasus->net);
1da177e4
LT
1041 if (netif_msg_ifup(pegasus))
1042 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1043 usb_kill_urb(pegasus->rx_urb);
1044 goto exit;
1045 }
1046 if ((res = enable_net_traffic(net, pegasus->usb))) {
1047 if (netif_msg_ifup(pegasus))
1048 pr_debug("%s: can't enable_net_traffic() - %d\n",
1049 net->name, res);
1050 res = -EIO;
1051 usb_kill_urb(pegasus->rx_urb);
1052 usb_kill_urb(pegasus->intr_urb);
1053 free_skb_pool(pegasus);
1054 goto exit;
1055 }
1056 set_carrier(net);
1057 netif_start_queue(net);
1058 if (netif_msg_ifup(pegasus))
1059 pr_debug("%s: open\n", net->name);
1060 res = 0;
1061exit:
1062 return res;
1063}
1064
1065static int pegasus_close(struct net_device *net)
1066{
1067 pegasus_t *pegasus = netdev_priv(net);
1068
1069 netif_stop_queue(net);
1070 if (!(pegasus->flags & PEGASUS_UNPLUG))
1071 disable_net_traffic(pegasus);
1072 tasklet_kill(&pegasus->rx_tl);
1073 unlink_all_urbs(pegasus);
1074
1075 return 0;
1076}
1077
1078static void pegasus_get_drvinfo(struct net_device *dev,
1079 struct ethtool_drvinfo *info)
1080{
1081 pegasus_t *pegasus = netdev_priv(dev);
1082 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1083 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1084 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1085}
1086
1087/* also handles three patterns of some kind in hardware */
1088#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1089
1090static void
1091pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1092{
1093 pegasus_t *pegasus = netdev_priv(dev);
1094
1095 wol->supported = WAKE_MAGIC | WAKE_PHY;
1096 wol->wolopts = pegasus->wolopts;
1097}
1098
1099static int
1100pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1101{
1102 pegasus_t *pegasus = netdev_priv(dev);
1103 u8 reg78 = 0x04;
1104
1105 if (wol->wolopts & ~WOL_SUPPORTED)
1106 return -EINVAL;
1107
1108 if (wol->wolopts & WAKE_MAGIC)
1109 reg78 |= 0x80;
1110 if (wol->wolopts & WAKE_PHY)
1111 reg78 |= 0x40;
1112 /* FIXME this 0x10 bit still needs to get set in the chip... */
1113 if (wol->wolopts)
1114 pegasus->eth_regs[0] |= 0x10;
1115 else
1116 pegasus->eth_regs[0] &= ~0x10;
1117 pegasus->wolopts = wol->wolopts;
1118 return set_register(pegasus, WakeupControl, reg78);
1119}
1120
1121static inline void pegasus_reset_wol(struct net_device *dev)
1122{
1123 struct ethtool_wolinfo wol;
1124
1125 memset(&wol, 0, sizeof wol);
1126 (void) pegasus_set_wol(dev, &wol);
1127}
1128
1129static int
1130pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1131{
1132 pegasus_t *pegasus;
1133
1da177e4
LT
1134 pegasus = netdev_priv(dev);
1135 mii_ethtool_gset(&pegasus->mii, ecmd);
1da177e4
LT
1136 return 0;
1137}
1138
1139static int
1140pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1141{
1142 pegasus_t *pegasus = netdev_priv(dev);
1143 return mii_ethtool_sset(&pegasus->mii, ecmd);
1144}
1145
1146static int pegasus_nway_reset(struct net_device *dev)
1147{
1148 pegasus_t *pegasus = netdev_priv(dev);
1149 return mii_nway_restart(&pegasus->mii);
1150}
1151
1152static u32 pegasus_get_link(struct net_device *dev)
1153{
1154 pegasus_t *pegasus = netdev_priv(dev);
1155 return mii_link_ok(&pegasus->mii);
1156}
1157
1158static u32 pegasus_get_msglevel(struct net_device *dev)
1159{
1160 pegasus_t *pegasus = netdev_priv(dev);
1161 return pegasus->msg_enable;
1162}
1163
1164static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1165{
1166 pegasus_t *pegasus = netdev_priv(dev);
1167 pegasus->msg_enable = v;
1168}
1169
1170static struct ethtool_ops ops = {
1171 .get_drvinfo = pegasus_get_drvinfo,
1172 .get_settings = pegasus_get_settings,
1173 .set_settings = pegasus_set_settings,
1174 .nway_reset = pegasus_nway_reset,
1175 .get_link = pegasus_get_link,
1176 .get_msglevel = pegasus_get_msglevel,
1177 .set_msglevel = pegasus_set_msglevel,
1178 .get_wol = pegasus_get_wol,
1179 .set_wol = pegasus_set_wol,
1180};
1181
1182static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1183{
1184 __u16 *data = (__u16 *) & rq->ifr_ifru;
1185 pegasus_t *pegasus = netdev_priv(net);
1186 int res;
1187
1188 switch (cmd) {
1189 case SIOCDEVPRIVATE:
1190 data[0] = pegasus->phy;
1191 case SIOCDEVPRIVATE + 1:
1192 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1193 res = 0;
1194 break;
1195 case SIOCDEVPRIVATE + 2:
1196 if (!capable(CAP_NET_ADMIN))
1197 return -EPERM;
1198 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1199 res = 0;
1200 break;
1201 default:
1202 res = -EOPNOTSUPP;
1203 }
1204 return res;
1205}
1206
1207static void pegasus_set_multicast(struct net_device *net)
1208{
1209 pegasus_t *pegasus = netdev_priv(net);
1210
1211 if (net->flags & IFF_PROMISC) {
1212 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1213 if (netif_msg_link(pegasus))
1214 pr_info("%s: Promiscuous mode enabled.\n", net->name);
e000ea13 1215 } else if (net->mc_count || (net->flags & IFF_ALLMULTI)) {
1da177e4
LT
1216 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1217 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1218 if (netif_msg_link(pegasus))
cda2836d 1219 pr_debug("%s: set allmulti\n", net->name);
1da177e4
LT
1220 } else {
1221 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1222 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1223 }
1224
e000ea13
DB
1225 pegasus->ctrl_urb->status = 0;
1226
1da177e4 1227 pegasus->flags |= ETH_REGS_CHANGE;
7d12e780 1228 ctrl_callback(pegasus->ctrl_urb);
1da177e4
LT
1229}
1230
1231static __u8 mii_phy_probe(pegasus_t * pegasus)
1232{
1233 int i;
1234 __u16 tmp;
1235
1236 for (i = 0; i < 32; i++) {
1237 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1238 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1239 continue;
1240 else
1241 return i;
1242 }
1243
1244 return 0xff;
1245}
1246
1247static inline void setup_pegasus_II(pegasus_t * pegasus)
1248{
1249 __u8 data = 0xa5;
1da177e4 1250
4a1728a2
PM
1251 set_register(pegasus, Reg1d, 0);
1252 set_register(pegasus, Reg7b, 1);
1da177e4
LT
1253 mdelay(100);
1254 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
4a1728a2 1255 set_register(pegasus, Reg7b, 0);
1da177e4 1256 else
4a1728a2 1257 set_register(pegasus, Reg7b, 2);
1da177e4 1258
4a1728a2
PM
1259 set_register(pegasus, 0x83, data);
1260 get_registers(pegasus, 0x83, 1, &data);
1da177e4
LT
1261
1262 if (data == 0xa5) {
1263 pegasus->chip = 0x8513;
1264 } else {
1265 pegasus->chip = 0;
1266 }
1267
4a1728a2
PM
1268 set_register(pegasus, 0x80, 0xc0);
1269 set_register(pegasus, 0x83, 0xff);
1270 set_register(pegasus, 0x84, 0x01);
1da177e4
LT
1271
1272 if (pegasus->features & HAS_HOME_PNA && mii_mode)
4a1728a2 1273 set_register(pegasus, Reg81, 6);
1da177e4 1274 else
4a1728a2 1275 set_register(pegasus, Reg81, 2);
1da177e4
LT
1276}
1277
1278
cda2836d 1279static int pegasus_count;
1da177e4
LT
1280static struct workqueue_struct *pegasus_workqueue = NULL;
1281#define CARRIER_CHECK_DELAY (2 * HZ)
1282
c4028958 1283static void check_carrier(struct work_struct *work)
1da177e4 1284{
c4028958 1285 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1da177e4
LT
1286 set_carrier(pegasus->net);
1287 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1288 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1289 CARRIER_CHECK_DELAY);
1290 }
1291}
1292
4f63135e
BC
1293static int pegasus_blacklisted(struct usb_device *udev)
1294{
1295 struct usb_device_descriptor *udd = &udev->descriptor;
1296
1297 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1298 * dongle which happens to have the same ID.
1299 */
1300 if ((udd->idVendor == VENDOR_BELKIN && udd->idProduct == 0x0121) &&
1301 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1302 (udd->bDeviceProtocol == 1))
1303 return 1;
1304
1305 return 0;
1306}
1307
cda2836d
DB
1308/* we rely on probe() and remove() being serialized so we
1309 * don't need extra locking on pegasus_count.
1310 */
1311static void pegasus_dec_workqueue(void)
1312{
1313 pegasus_count--;
1314 if (pegasus_count == 0) {
1315 destroy_workqueue(pegasus_workqueue);
1316 pegasus_workqueue = NULL;
1317 }
1318}
1319
1da177e4
LT
1320static int pegasus_probe(struct usb_interface *intf,
1321 const struct usb_device_id *id)
1322{
1323 struct usb_device *dev = interface_to_usbdev(intf);
1324 struct net_device *net;
1325 pegasus_t *pegasus;
1326 int dev_index = id - pegasus_ids;
1327 int res = -ENOMEM;
1328
cda2836d
DB
1329 if (pegasus_blacklisted(dev))
1330 return -ENODEV;
4f63135e 1331
cda2836d
DB
1332 if (pegasus_count == 0) {
1333 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1334 if (!pegasus_workqueue)
1335 return -ENOMEM;
4f63135e 1336 }
cda2836d
DB
1337 pegasus_count++;
1338
1339 usb_get_dev(dev);
4f63135e 1340
1da177e4
LT
1341 net = alloc_etherdev(sizeof(struct pegasus));
1342 if (!net) {
1343 dev_err(&intf->dev, "can't allocate %s\n", "device");
1344 goto out;
1345 }
1346
1347 pegasus = netdev_priv(net);
1da177e4
LT
1348 pegasus->dev_index = dev_index;
1349 init_waitqueue_head(&pegasus->ctrl_wait);
1350
1351 if (!alloc_urbs(pegasus)) {
1352 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1353 goto out1;
1354 }
1355
1356 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1357
c4028958 1358 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1da177e4
LT
1359
1360 pegasus->intf = intf;
1361 pegasus->usb = dev;
1362 pegasus->net = net;
1da177e4
LT
1363 net->open = pegasus_open;
1364 net->stop = pegasus_close;
1365 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1366 net->tx_timeout = pegasus_tx_timeout;
1367 net->do_ioctl = pegasus_ioctl;
1368 net->hard_start_xmit = pegasus_start_xmit;
1369 net->set_multicast_list = pegasus_set_multicast;
1370 net->get_stats = pegasus_netdev_stats;
1371 SET_ETHTOOL_OPS(net, &ops);
1372 pegasus->mii.dev = net;
1373 pegasus->mii.mdio_read = mdio_read;
1374 pegasus->mii.mdio_write = mdio_write;
1375 pegasus->mii.phy_id_mask = 0x1f;
1376 pegasus->mii.reg_num_mask = 0x1f;
1377 spin_lock_init(&pegasus->rx_pool_lock);
1378 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1379 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1380
1381 pegasus->features = usb_dev_id[dev_index].private;
1382 get_interrupt_interval(pegasus);
1383 if (reset_mac(pegasus)) {
1384 dev_err(&intf->dev, "can't reset MAC\n");
1385 res = -EIO;
1386 goto out2;
1387 }
1388 set_ethernet_addr(pegasus);
1389 fill_skb_pool(pegasus);
1390 if (pegasus->features & PEGASUS_II) {
1391 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1392 setup_pegasus_II(pegasus);
1393 }
1394 pegasus->phy = mii_phy_probe(pegasus);
1395 if (pegasus->phy == 0xff) {
1396 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1397 pegasus->phy = 1;
1398 }
1399 pegasus->mii.phy_id = pegasus->phy;
1400 usb_set_intfdata(intf, pegasus);
1401 SET_NETDEV_DEV(net, &intf->dev);
1402 pegasus_reset_wol(net);
1403 res = register_netdev(net);
1404 if (res)
1405 goto out3;
1406 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1407 CARRIER_CHECK_DELAY);
1408
e174961c 1409 dev_info(&intf->dev, "%s, %s, %pM\n",
0795af57
JP
1410 net->name,
1411 usb_dev_id[dev_index].name,
e174961c 1412 net->dev_addr);
1da177e4
LT
1413 return 0;
1414
1415out3:
1416 usb_set_intfdata(intf, NULL);
1417 free_skb_pool(pegasus);
1418out2:
1419 free_all_urbs(pegasus);
1420out1:
1421 free_netdev(net);
1422out:
1423 usb_put_dev(dev);
cda2836d 1424 pegasus_dec_workqueue();
1da177e4
LT
1425 return res;
1426}
1427
1428static void pegasus_disconnect(struct usb_interface *intf)
1429{
1430 struct pegasus *pegasus = usb_get_intfdata(intf);
1431
1432 usb_set_intfdata(intf, NULL);
1433 if (!pegasus) {
1434 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1435 return;
1436 }
1437
1438 pegasus->flags |= PEGASUS_UNPLUG;
1439 cancel_delayed_work(&pegasus->carrier_check);
1440 unregister_netdev(pegasus->net);
1441 usb_put_dev(interface_to_usbdev(intf));
a85a46f2 1442 unlink_all_urbs(pegasus);
1da177e4
LT
1443 free_all_urbs(pegasus);
1444 free_skb_pool(pegasus);
4c13eb66 1445 if (pegasus->rx_skb != NULL) {
1da177e4 1446 dev_kfree_skb(pegasus->rx_skb);
4c13eb66
ACM
1447 pegasus->rx_skb = NULL;
1448 }
1da177e4 1449 free_netdev(pegasus->net);
cda2836d 1450 pegasus_dec_workqueue();
1da177e4
LT
1451}
1452
27d72e85 1453static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1454{
1455 struct pegasus *pegasus = usb_get_intfdata(intf);
1456
1457 netif_device_detach (pegasus->net);
7e713b82 1458 cancel_delayed_work(&pegasus->carrier_check);
27d72e85 1459 if (netif_running(pegasus->net)) {
27d72e85
DB
1460 usb_kill_urb(pegasus->rx_urb);
1461 usb_kill_urb(pegasus->intr_urb);
1462 }
1da177e4
LT
1463 return 0;
1464}
1465
1466static int pegasus_resume (struct usb_interface *intf)
1467{
1468 struct pegasus *pegasus = usb_get_intfdata(intf);
1469
1470 netif_device_attach (pegasus->net);
27d72e85
DB
1471 if (netif_running(pegasus->net)) {
1472 pegasus->rx_urb->status = 0;
1473 pegasus->rx_urb->actual_length = 0;
7d12e780 1474 read_bulk_callback(pegasus->rx_urb);
27d72e85
DB
1475
1476 pegasus->intr_urb->status = 0;
1477 pegasus->intr_urb->actual_length = 0;
7d12e780 1478 intr_callback(pegasus->intr_urb);
27d72e85 1479 }
7e713b82
DB
1480 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1481 CARRIER_CHECK_DELAY);
1da177e4
LT
1482 return 0;
1483}
1484
1485static struct usb_driver pegasus_driver = {
1486 .name = driver_name,
1487 .probe = pegasus_probe,
1488 .disconnect = pegasus_disconnect,
1489 .id_table = pegasus_ids,
1490 .suspend = pegasus_suspend,
1491 .resume = pegasus_resume,
1492};
1493
cda2836d 1494static void __init parse_id(char *id)
a4f81a61
Y
1495{
1496 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1497 char *token, *name=NULL;
1498
1499 if ((token = strsep(&id, ":")) != NULL)
1500 name = token;
1501 /* name now points to a null terminated string*/
1502 if ((token = strsep(&id, ":")) != NULL)
1503 vendor_id = simple_strtoul(token, NULL, 16);
1504 if ((token = strsep(&id, ":")) != NULL)
1505 device_id = simple_strtoul(token, NULL, 16);
1506 flags = simple_strtoul(id, NULL, 16);
1507 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1508 driver_name, name, vendor_id, device_id, flags);
1509
1510 if (vendor_id > 0x10000 || vendor_id == 0)
1511 return;
1512 if (device_id > 0x10000 || device_id == 0)
1513 return;
1514
1515 for (i=0; usb_dev_id[i].name; i++);
1516 usb_dev_id[i].name = name;
1517 usb_dev_id[i].vendor = vendor_id;
1518 usb_dev_id[i].device = device_id;
1519 usb_dev_id[i].private = flags;
1520 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1521 pegasus_ids[i].idVendor = vendor_id;
1522 pegasus_ids[i].idProduct = device_id;
1523}
1524
1da177e4
LT
1525static int __init pegasus_init(void)
1526{
1527 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
a4f81a61
Y
1528 if (devid)
1529 parse_id(devid);
1da177e4
LT
1530 return usb_register(&pegasus_driver);
1531}
1532
1533static void __exit pegasus_exit(void)
1534{
1da177e4
LT
1535 usb_deregister(&pegasus_driver);
1536}
1537
1538module_init(pegasus_init);
1539module_exit(pegasus_exit);