[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / usb / pegasus.c
blobed4a508ef26220d19a7bb4620be5c84eb43e9ca3
1 /*
2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
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.
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.
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"
46 * Version Information
48 #define DRIVER_VERSION "v0.6.14 (2006/09/27)"
49 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
52 static const char driver_name[] = "pegasus";
54 #undef PEGASUS_WRITE_EEPROM
55 #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56 BMSR_100FULL | BMSR_ANEGCAPABLE)
58 static int loopback = 0;
59 static int mii_mode = 0;
60 static char *devid=NULL;
62 static 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 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
66 PEGASUS_DEV(pn, vid, pid, flags)
67 #include "pegasus.h"
68 #undef PEGASUS_DEV
69 #undef PEGASUS_DEV_CLASS
70 {NULL, 0, 0, 0},
71 {NULL, 0, 0, 0}
74 static struct usb_device_id pegasus_ids[] = {
75 #define PEGASUS_DEV(pn, vid, pid, flags) \
76 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
78 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
79 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
80 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
81 * case anyway, seeing as the pegasus is for "Wired" adaptors.
83 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
84 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
85 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
86 #include "pegasus.h"
87 #undef PEGASUS_DEV
88 #undef PEGASUS_DEV_CLASS
89 {},
93 MODULE_AUTHOR(DRIVER_AUTHOR);
94 MODULE_DESCRIPTION(DRIVER_DESC);
95 MODULE_LICENSE("GPL");
96 module_param(loopback, bool, 0);
97 module_param(mii_mode, bool, 0);
98 module_param(devid, charp, 0);
99 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
100 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
101 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
103 /* use ethtool to change the level for any given device */
104 static int msg_level = -1;
105 module_param (msg_level, int, 0);
106 MODULE_PARM_DESC (msg_level, "Override default message level");
108 MODULE_DEVICE_TABLE(usb, pegasus_ids);
109 static const struct net_device_ops pegasus_netdev_ops;
111 static int update_eth_regs_async(pegasus_t *);
112 /* Aargh!!! I _really_ hate such tweaks */
113 static void ctrl_callback(struct urb *urb)
115 pegasus_t *pegasus = urb->context;
116 int status = urb->status;
118 if (!pegasus)
119 return;
121 switch (status) {
122 case 0:
123 if (pegasus->flags & ETH_REGS_CHANGE) {
124 pegasus->flags &= ~ETH_REGS_CHANGE;
125 pegasus->flags |= ETH_REGS_CHANGED;
126 update_eth_regs_async(pegasus);
127 return;
129 break;
130 case -EINPROGRESS:
131 return;
132 case -ENOENT:
133 break;
134 default:
135 if (netif_msg_drv(pegasus) && printk_ratelimit())
136 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
137 __func__, status);
139 pegasus->flags &= ~ETH_REGS_CHANGED;
140 wake_up(&pegasus->ctrl_wait);
143 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
144 void *data)
146 int ret;
147 char *buffer;
148 DECLARE_WAITQUEUE(wait, current);
150 buffer = kmalloc(size, GFP_KERNEL);
151 if (!buffer) {
152 if (netif_msg_drv(pegasus))
153 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
154 __func__);
155 return -ENOMEM;
157 add_wait_queue(&pegasus->ctrl_wait, &wait);
158 set_current_state(TASK_UNINTERRUPTIBLE);
159 while (pegasus->flags & ETH_REGS_CHANGED)
160 schedule();
161 remove_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_RUNNING);
164 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
165 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
166 pegasus->dr.wValue = cpu_to_le16(0);
167 pegasus->dr.wIndex = cpu_to_le16(indx);
168 pegasus->dr.wLength = cpu_to_le16(size);
169 pegasus->ctrl_urb->transfer_buffer_length = size;
171 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
172 usb_rcvctrlpipe(pegasus->usb, 0),
173 (char *) &pegasus->dr,
174 buffer, size, ctrl_callback, pegasus);
176 add_wait_queue(&pegasus->ctrl_wait, &wait);
177 set_current_state(TASK_UNINTERRUPTIBLE);
179 /* using ATOMIC, we'd never wake up if we slept */
180 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
181 set_current_state(TASK_RUNNING);
182 if (ret == -ENODEV)
183 netif_device_detach(pegasus->net);
184 if (netif_msg_drv(pegasus) && printk_ratelimit())
185 dev_err(&pegasus->intf->dev, "%s, status %d\n",
186 __func__, ret);
187 goto out;
190 schedule();
191 out:
192 remove_wait_queue(&pegasus->ctrl_wait, &wait);
193 memcpy(data, buffer, size);
194 kfree(buffer);
196 return ret;
199 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
200 void *data)
202 int ret;
203 char *buffer;
204 DECLARE_WAITQUEUE(wait, current);
206 buffer = kmalloc(size, GFP_KERNEL);
207 if (!buffer) {
208 if (netif_msg_drv(pegasus))
209 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
210 __func__);
211 return -ENOMEM;
213 memcpy(buffer, data, size);
215 add_wait_queue(&pegasus->ctrl_wait, &wait);
216 set_current_state(TASK_UNINTERRUPTIBLE);
217 while (pegasus->flags & ETH_REGS_CHANGED)
218 schedule();
219 remove_wait_queue(&pegasus->ctrl_wait, &wait);
220 set_current_state(TASK_RUNNING);
222 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
223 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
224 pegasus->dr.wValue = cpu_to_le16(0);
225 pegasus->dr.wIndex = cpu_to_le16(indx);
226 pegasus->dr.wLength = cpu_to_le16(size);
227 pegasus->ctrl_urb->transfer_buffer_length = size;
229 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
230 usb_sndctrlpipe(pegasus->usb, 0),
231 (char *) &pegasus->dr,
232 buffer, size, ctrl_callback, pegasus);
234 add_wait_queue(&pegasus->ctrl_wait, &wait);
235 set_current_state(TASK_UNINTERRUPTIBLE);
237 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
238 if (ret == -ENODEV)
239 netif_device_detach(pegasus->net);
240 if (netif_msg_drv(pegasus))
241 dev_err(&pegasus->intf->dev, "%s, status %d\n",
242 __func__, ret);
243 goto out;
246 schedule();
247 out:
248 remove_wait_queue(&pegasus->ctrl_wait, &wait);
249 kfree(buffer);
251 return ret;
254 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
256 int ret;
257 char *tmp;
258 DECLARE_WAITQUEUE(wait, current);
260 tmp = kmalloc(1, GFP_KERNEL);
261 if (!tmp) {
262 if (netif_msg_drv(pegasus))
263 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
264 __func__);
265 return -ENOMEM;
267 memcpy(tmp, &data, 1);
268 add_wait_queue(&pegasus->ctrl_wait, &wait);
269 set_current_state(TASK_UNINTERRUPTIBLE);
270 while (pegasus->flags & ETH_REGS_CHANGED)
271 schedule();
272 remove_wait_queue(&pegasus->ctrl_wait, &wait);
273 set_current_state(TASK_RUNNING);
275 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
276 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
277 pegasus->dr.wValue = cpu_to_le16(data);
278 pegasus->dr.wIndex = cpu_to_le16(indx);
279 pegasus->dr.wLength = cpu_to_le16(1);
280 pegasus->ctrl_urb->transfer_buffer_length = 1;
282 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
283 usb_sndctrlpipe(pegasus->usb, 0),
284 (char *) &pegasus->dr,
285 tmp, 1, ctrl_callback, pegasus);
287 add_wait_queue(&pegasus->ctrl_wait, &wait);
288 set_current_state(TASK_UNINTERRUPTIBLE);
290 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
291 if (ret == -ENODEV)
292 netif_device_detach(pegasus->net);
293 if (netif_msg_drv(pegasus) && printk_ratelimit())
294 dev_err(&pegasus->intf->dev, "%s, status %d\n",
295 __func__, ret);
296 goto out;
299 schedule();
300 out:
301 remove_wait_queue(&pegasus->ctrl_wait, &wait);
302 kfree(tmp);
304 return ret;
307 static int update_eth_regs_async(pegasus_t * pegasus)
309 int ret;
311 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
312 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
313 pegasus->dr.wValue = cpu_to_le16(0);
314 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
315 pegasus->dr.wLength = cpu_to_le16(3);
316 pegasus->ctrl_urb->transfer_buffer_length = 3;
318 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
319 usb_sndctrlpipe(pegasus->usb, 0),
320 (char *) &pegasus->dr,
321 pegasus->eth_regs, 3, ctrl_callback, pegasus);
323 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
324 if (ret == -ENODEV)
325 netif_device_detach(pegasus->net);
326 if (netif_msg_drv(pegasus))
327 dev_err(&pegasus->intf->dev, "%s, status %d\n",
328 __func__, ret);
331 return ret;
334 /* Returns 0 on success, error on failure */
335 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
337 int i;
338 __u8 data[4] = { phy, 0, 0, indx };
339 __le16 regdi;
340 int ret;
342 set_register(pegasus, PhyCtrl, 0);
343 set_registers(pegasus, PhyAddr, sizeof (data), data);
344 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
345 for (i = 0; i < REG_TIMEOUT; i++) {
346 ret = get_registers(pegasus, PhyCtrl, 1, data);
347 if (ret == -ESHUTDOWN)
348 goto fail;
349 if (data[0] & PHY_DONE)
350 break;
352 if (i < REG_TIMEOUT) {
353 ret = get_registers(pegasus, PhyData, 2, &regdi);
354 *regd = le16_to_cpu(regdi);
355 return ret;
357 fail:
358 if (netif_msg_drv(pegasus))
359 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
361 return ret;
364 static int mdio_read(struct net_device *dev, int phy_id, int loc)
366 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
367 u16 res;
369 read_mii_word(pegasus, phy_id, loc, &res);
370 return (int)res;
373 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
375 int i;
376 __u8 data[4] = { phy, 0, 0, indx };
377 int ret;
379 data[1] = (u8) regd;
380 data[2] = (u8) (regd >> 8);
381 set_register(pegasus, PhyCtrl, 0);
382 set_registers(pegasus, PhyAddr, sizeof(data), data);
383 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
384 for (i = 0; i < REG_TIMEOUT; i++) {
385 ret = get_registers(pegasus, PhyCtrl, 1, data);
386 if (ret == -ESHUTDOWN)
387 goto fail;
388 if (data[0] & PHY_DONE)
389 break;
391 if (i < REG_TIMEOUT)
392 return ret;
394 fail:
395 if (netif_msg_drv(pegasus))
396 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
397 return -ETIMEDOUT;
400 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
402 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
404 write_mii_word(pegasus, phy_id, loc, val);
407 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
409 int i;
410 __u8 tmp;
411 __le16 retdatai;
412 int ret;
414 set_register(pegasus, EpromCtrl, 0);
415 set_register(pegasus, EpromOffset, index);
416 set_register(pegasus, EpromCtrl, EPROM_READ);
418 for (i = 0; i < REG_TIMEOUT; i++) {
419 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
420 if (tmp & EPROM_DONE)
421 break;
422 if (ret == -ESHUTDOWN)
423 goto fail;
425 if (i < REG_TIMEOUT) {
426 ret = get_registers(pegasus, EpromData, 2, &retdatai);
427 *retdata = le16_to_cpu(retdatai);
428 return ret;
431 fail:
432 if (netif_msg_drv(pegasus))
433 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
434 return -ETIMEDOUT;
437 #ifdef PEGASUS_WRITE_EEPROM
438 static inline void enable_eprom_write(pegasus_t * pegasus)
440 __u8 tmp;
441 int ret;
443 get_registers(pegasus, EthCtrl2, 1, &tmp);
444 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
447 static inline void disable_eprom_write(pegasus_t * pegasus)
449 __u8 tmp;
450 int ret;
452 get_registers(pegasus, EthCtrl2, 1, &tmp);
453 set_register(pegasus, EpromCtrl, 0);
454 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
457 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
459 int i;
460 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
461 int ret;
462 __le16 le_data = cpu_to_le16(data);
464 set_registers(pegasus, EpromOffset, 4, d);
465 enable_eprom_write(pegasus);
466 set_register(pegasus, EpromOffset, index);
467 set_registers(pegasus, EpromData, 2, &le_data);
468 set_register(pegasus, EpromCtrl, EPROM_WRITE);
470 for (i = 0; i < REG_TIMEOUT; i++) {
471 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
472 if (ret == -ESHUTDOWN)
473 goto fail;
474 if (tmp & EPROM_DONE)
475 break;
477 disable_eprom_write(pegasus);
478 if (i < REG_TIMEOUT)
479 return ret;
480 fail:
481 if (netif_msg_drv(pegasus))
482 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
483 return -ETIMEDOUT;
485 #endif /* PEGASUS_WRITE_EEPROM */
487 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
489 int i;
490 __u16 w16;
492 for (i = 0; i < 3; i++) {
493 read_eprom_word(pegasus, i, &w16);
494 ((__le16 *) id)[i] = cpu_to_le16(w16);
498 static void set_ethernet_addr(pegasus_t * pegasus)
500 __u8 node_id[6];
502 if (pegasus->features & PEGASUS_II) {
503 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
504 } else {
505 get_node_id(pegasus, node_id);
506 set_registers(pegasus, EthID, sizeof (node_id), node_id);
508 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
511 static inline int reset_mac(pegasus_t * pegasus)
513 __u8 data = 0x8;
514 int i;
516 set_register(pegasus, EthCtrl1, data);
517 for (i = 0; i < REG_TIMEOUT; i++) {
518 get_registers(pegasus, EthCtrl1, 1, &data);
519 if (~data & 0x08) {
520 if (loopback & 1)
521 break;
522 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
523 set_register(pegasus, Gpio1, 0x34);
524 else
525 set_register(pegasus, Gpio1, 0x26);
526 set_register(pegasus, Gpio0, pegasus->features);
527 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
528 break;
531 if (i == REG_TIMEOUT)
532 return -ETIMEDOUT;
534 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
535 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
536 set_register(pegasus, Gpio0, 0x24);
537 set_register(pegasus, Gpio0, 0x26);
539 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
540 __u16 auxmode;
541 read_mii_word(pegasus, 3, 0x1b, &auxmode);
542 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
545 return 0;
548 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
550 __u16 linkpart;
551 __u8 data[4];
552 pegasus_t *pegasus = netdev_priv(dev);
553 int ret;
555 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
556 data[0] = 0xc9;
557 data[1] = 0;
558 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
559 data[1] |= 0x20; /* set full duplex */
560 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
561 data[1] |= 0x10; /* set 100 Mbps */
562 if (mii_mode)
563 data[1] = 0;
564 data[2] = (loopback & 1) ? 0x09 : 0x01;
566 memcpy(pegasus->eth_regs, data, sizeof (data));
567 ret = set_registers(pegasus, EthCtrl0, 3, data);
569 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
570 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
571 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
572 u16 auxmode;
573 read_mii_word(pegasus, 0, 0x1b, &auxmode);
574 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
577 return ret;
580 static void fill_skb_pool(pegasus_t * pegasus)
582 int i;
584 for (i = 0; i < RX_SKBS; i++) {
585 if (pegasus->rx_pool[i])
586 continue;
587 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
589 ** we give up if the allocation fail. the tasklet will be
590 ** rescheduled again anyway...
592 if (pegasus->rx_pool[i] == NULL)
593 return;
594 skb_reserve(pegasus->rx_pool[i], 2);
598 static void free_skb_pool(pegasus_t * pegasus)
600 int i;
602 for (i = 0; i < RX_SKBS; i++) {
603 if (pegasus->rx_pool[i]) {
604 dev_kfree_skb(pegasus->rx_pool[i]);
605 pegasus->rx_pool[i] = NULL;
610 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
612 int i;
613 struct sk_buff *skb;
615 for (i = 0; i < RX_SKBS; i++) {
616 if (likely(pegasus->rx_pool[i] != NULL)) {
617 skb = pegasus->rx_pool[i];
618 pegasus->rx_pool[i] = NULL;
619 return skb;
622 return NULL;
625 static void read_bulk_callback(struct urb *urb)
627 pegasus_t *pegasus = urb->context;
628 struct net_device *net;
629 int rx_status, count = urb->actual_length;
630 int status = urb->status;
631 u8 *buf = urb->transfer_buffer;
632 __u16 pkt_len;
634 if (!pegasus)
635 return;
637 net = pegasus->net;
638 if (!netif_device_present(net) || !netif_running(net))
639 return;
641 switch (status) {
642 case 0:
643 break;
644 case -ETIME:
645 if (netif_msg_rx_err(pegasus))
646 pr_debug("%s: reset MAC\n", net->name);
647 pegasus->flags &= ~PEGASUS_RX_BUSY;
648 break;
649 case -EPIPE: /* stall, or disconnect from TT */
650 /* FIXME schedule work to clear the halt */
651 if (netif_msg_rx_err(pegasus))
652 printk(KERN_WARNING "%s: no rx stall recovery\n",
653 net->name);
654 return;
655 case -ENOENT:
656 case -ECONNRESET:
657 case -ESHUTDOWN:
658 if (netif_msg_ifdown(pegasus))
659 pr_debug("%s: rx unlink, %d\n", net->name, status);
660 return;
661 default:
662 if (netif_msg_rx_err(pegasus))
663 pr_debug("%s: RX status %d\n", net->name, status);
664 goto goon;
667 if (!count || count < 4)
668 goto goon;
670 rx_status = buf[count - 2];
671 if (rx_status & 0x1e) {
672 if (netif_msg_rx_err(pegasus))
673 pr_debug("%s: RX packet error %x\n",
674 net->name, rx_status);
675 pegasus->stats.rx_errors++;
676 if (rx_status & 0x06) // long or runt
677 pegasus->stats.rx_length_errors++;
678 if (rx_status & 0x08)
679 pegasus->stats.rx_crc_errors++;
680 if (rx_status & 0x10) // extra bits
681 pegasus->stats.rx_frame_errors++;
682 goto goon;
684 if (pegasus->chip == 0x8513) {
685 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
686 pkt_len &= 0x0fff;
687 pegasus->rx_skb->data += 2;
688 } else {
689 pkt_len = buf[count - 3] << 8;
690 pkt_len += buf[count - 4];
691 pkt_len &= 0xfff;
692 pkt_len -= 8;
696 * If the packet is unreasonably long, quietly drop it rather than
697 * kernel panicing by calling skb_put.
699 if (pkt_len > PEGASUS_MTU)
700 goto goon;
703 * at this point we are sure pegasus->rx_skb != NULL
704 * so we go ahead and pass up the packet.
706 skb_put(pegasus->rx_skb, pkt_len);
707 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
708 netif_rx(pegasus->rx_skb);
709 pegasus->stats.rx_packets++;
710 pegasus->stats.rx_bytes += pkt_len;
712 if (pegasus->flags & PEGASUS_UNPLUG)
713 return;
715 spin_lock(&pegasus->rx_pool_lock);
716 pegasus->rx_skb = pull_skb(pegasus);
717 spin_unlock(&pegasus->rx_pool_lock);
719 if (pegasus->rx_skb == NULL)
720 goto tl_sched;
721 goon:
722 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
723 usb_rcvbulkpipe(pegasus->usb, 1),
724 pegasus->rx_skb->data, PEGASUS_MTU + 8,
725 read_bulk_callback, pegasus);
726 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
727 if (rx_status == -ENODEV)
728 netif_device_detach(pegasus->net);
729 else if (rx_status) {
730 pegasus->flags |= PEGASUS_RX_URB_FAIL;
731 goto tl_sched;
732 } else {
733 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
736 return;
738 tl_sched:
739 tasklet_schedule(&pegasus->rx_tl);
742 static void rx_fixup(unsigned long data)
744 pegasus_t *pegasus;
745 unsigned long flags;
746 int status;
748 pegasus = (pegasus_t *) data;
749 if (pegasus->flags & PEGASUS_UNPLUG)
750 return;
752 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
753 fill_skb_pool(pegasus);
754 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
755 if (pegasus->rx_skb)
756 goto try_again;
757 if (pegasus->rx_skb == NULL) {
758 pegasus->rx_skb = pull_skb(pegasus);
760 if (pegasus->rx_skb == NULL) {
761 if (netif_msg_rx_err(pegasus))
762 printk(KERN_WARNING "%s: low on memory\n",
763 pegasus->net->name);
764 tasklet_schedule(&pegasus->rx_tl);
765 goto done;
767 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
768 usb_rcvbulkpipe(pegasus->usb, 1),
769 pegasus->rx_skb->data, PEGASUS_MTU + 8,
770 read_bulk_callback, pegasus);
771 try_again:
772 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
773 if (status == -ENODEV)
774 netif_device_detach(pegasus->net);
775 else if (status) {
776 pegasus->flags |= PEGASUS_RX_URB_FAIL;
777 tasklet_schedule(&pegasus->rx_tl);
778 } else {
779 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
781 done:
782 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
785 static void write_bulk_callback(struct urb *urb)
787 pegasus_t *pegasus = urb->context;
788 struct net_device *net;
789 int status = urb->status;
791 if (!pegasus)
792 return;
794 net = pegasus->net;
796 if (!netif_device_present(net) || !netif_running(net))
797 return;
799 switch (status) {
800 case -EPIPE:
801 /* FIXME schedule_work() to clear the tx halt */
802 netif_stop_queue(net);
803 if (netif_msg_tx_err(pegasus))
804 printk(KERN_WARNING "%s: no tx stall recovery\n",
805 net->name);
806 return;
807 case -ENOENT:
808 case -ECONNRESET:
809 case -ESHUTDOWN:
810 if (netif_msg_ifdown(pegasus))
811 pr_debug("%s: tx unlink, %d\n", net->name, status);
812 return;
813 default:
814 if (netif_msg_tx_err(pegasus))
815 pr_info("%s: TX status %d\n", net->name, status);
816 /* FALL THROUGH */
817 case 0:
818 break;
821 net->trans_start = jiffies;
822 netif_wake_queue(net);
825 static void intr_callback(struct urb *urb)
827 pegasus_t *pegasus = urb->context;
828 struct net_device *net;
829 int res, status = urb->status;
831 if (!pegasus)
832 return;
833 net = pegasus->net;
835 switch (status) {
836 case 0:
837 break;
838 case -ECONNRESET: /* unlink */
839 case -ENOENT:
840 case -ESHUTDOWN:
841 return;
842 default:
843 /* some Pegasus-I products report LOTS of data
844 * toggle errors... avoid log spamming
846 if (netif_msg_timer(pegasus))
847 pr_debug("%s: intr status %d\n", net->name,
848 status);
851 if (urb->actual_length >= 6) {
852 u8 * d = urb->transfer_buffer;
854 /* byte 0 == tx_status1, reg 2B */
855 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
856 |LATE_COL|JABBER_TIMEOUT)) {
857 pegasus->stats.tx_errors++;
858 if (d[0] & TX_UNDERRUN)
859 pegasus->stats.tx_fifo_errors++;
860 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
861 pegasus->stats.tx_aborted_errors++;
862 if (d[0] & LATE_COL)
863 pegasus->stats.tx_window_errors++;
866 /* d[5].LINK_STATUS lies on some adapters.
867 * d[0].NO_CARRIER kicks in only with failed TX.
868 * ... so monitoring with MII may be safest.
871 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
872 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
875 res = usb_submit_urb(urb, GFP_ATOMIC);
876 if (res == -ENODEV)
877 netif_device_detach(pegasus->net);
878 if (res && netif_msg_timer(pegasus))
879 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
880 net->name, res);
883 static void pegasus_tx_timeout(struct net_device *net)
885 pegasus_t *pegasus = netdev_priv(net);
886 if (netif_msg_timer(pegasus))
887 printk(KERN_WARNING "%s: tx timeout\n", net->name);
888 usb_unlink_urb(pegasus->tx_urb);
889 pegasus->stats.tx_errors++;
892 static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
893 struct net_device *net)
895 pegasus_t *pegasus = netdev_priv(net);
896 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
897 int res;
898 __u16 l16 = skb->len;
900 netif_stop_queue(net);
902 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
903 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
904 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
905 usb_sndbulkpipe(pegasus->usb, 2),
906 pegasus->tx_buff, count,
907 write_bulk_callback, pegasus);
908 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
909 if (netif_msg_tx_err(pegasus))
910 printk(KERN_WARNING "%s: fail tx, %d\n",
911 net->name, res);
912 switch (res) {
913 case -EPIPE: /* stall, or disconnect from TT */
914 /* cleanup should already have been scheduled */
915 break;
916 case -ENODEV: /* disconnect() upcoming */
917 case -EPERM:
918 netif_device_detach(pegasus->net);
919 break;
920 default:
921 pegasus->stats.tx_errors++;
922 netif_start_queue(net);
924 } else {
925 pegasus->stats.tx_packets++;
926 pegasus->stats.tx_bytes += skb->len;
927 net->trans_start = jiffies;
929 dev_kfree_skb(skb);
931 return NETDEV_TX_OK;
934 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
936 return &((pegasus_t *) netdev_priv(dev))->stats;
939 static inline void disable_net_traffic(pegasus_t * pegasus)
941 __le16 tmp = cpu_to_le16(0);
943 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
946 static inline void get_interrupt_interval(pegasus_t * pegasus)
948 u16 data;
949 u8 interval;
951 read_eprom_word(pegasus, 4, &data);
952 interval = data >> 8;
953 if (pegasus->usb->speed != USB_SPEED_HIGH) {
954 if (interval < 0x80) {
955 if (netif_msg_timer(pegasus))
956 dev_info(&pegasus->intf->dev, "intr interval "
957 "changed from %ums to %ums\n",
958 interval, 0x80);
959 interval = 0x80;
960 data = (data & 0x00FF) | ((u16)interval << 8);
961 #ifdef PEGASUS_WRITE_EEPROM
962 write_eprom_word(pegasus, 4, data);
963 #endif
966 pegasus->intr_interval = interval;
969 static void set_carrier(struct net_device *net)
971 pegasus_t *pegasus = netdev_priv(net);
972 u16 tmp;
974 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
975 return;
977 if (tmp & BMSR_LSTATUS)
978 netif_carrier_on(net);
979 else
980 netif_carrier_off(net);
983 static void free_all_urbs(pegasus_t * pegasus)
985 usb_free_urb(pegasus->intr_urb);
986 usb_free_urb(pegasus->tx_urb);
987 usb_free_urb(pegasus->rx_urb);
988 usb_free_urb(pegasus->ctrl_urb);
991 static void unlink_all_urbs(pegasus_t * pegasus)
993 usb_kill_urb(pegasus->intr_urb);
994 usb_kill_urb(pegasus->tx_urb);
995 usb_kill_urb(pegasus->rx_urb);
996 usb_kill_urb(pegasus->ctrl_urb);
999 static int alloc_urbs(pegasus_t * pegasus)
1001 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1002 if (!pegasus->ctrl_urb) {
1003 return 0;
1005 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1006 if (!pegasus->rx_urb) {
1007 usb_free_urb(pegasus->ctrl_urb);
1008 return 0;
1010 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1011 if (!pegasus->tx_urb) {
1012 usb_free_urb(pegasus->rx_urb);
1013 usb_free_urb(pegasus->ctrl_urb);
1014 return 0;
1016 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1017 if (!pegasus->intr_urb) {
1018 usb_free_urb(pegasus->tx_urb);
1019 usb_free_urb(pegasus->rx_urb);
1020 usb_free_urb(pegasus->ctrl_urb);
1021 return 0;
1024 return 1;
1027 static int pegasus_open(struct net_device *net)
1029 pegasus_t *pegasus = netdev_priv(net);
1030 int res;
1032 if (pegasus->rx_skb == NULL)
1033 pegasus->rx_skb = pull_skb(pegasus);
1035 ** Note: no point to free the pool. it is empty :-)
1037 if (!pegasus->rx_skb)
1038 return -ENOMEM;
1040 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1042 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1043 usb_rcvbulkpipe(pegasus->usb, 1),
1044 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1045 read_bulk_callback, pegasus);
1046 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1047 if (res == -ENODEV)
1048 netif_device_detach(pegasus->net);
1049 if (netif_msg_ifup(pegasus))
1050 pr_debug("%s: failed rx_urb, %d", net->name, res);
1051 goto exit;
1054 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1055 usb_rcvintpipe(pegasus->usb, 3),
1056 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1057 intr_callback, pegasus, pegasus->intr_interval);
1058 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1059 if (res == -ENODEV)
1060 netif_device_detach(pegasus->net);
1061 if (netif_msg_ifup(pegasus))
1062 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1063 usb_kill_urb(pegasus->rx_urb);
1064 goto exit;
1066 if ((res = enable_net_traffic(net, pegasus->usb))) {
1067 if (netif_msg_ifup(pegasus))
1068 pr_debug("%s: can't enable_net_traffic() - %d\n",
1069 net->name, res);
1070 res = -EIO;
1071 usb_kill_urb(pegasus->rx_urb);
1072 usb_kill_urb(pegasus->intr_urb);
1073 free_skb_pool(pegasus);
1074 goto exit;
1076 set_carrier(net);
1077 netif_start_queue(net);
1078 if (netif_msg_ifup(pegasus))
1079 pr_debug("%s: open\n", net->name);
1080 res = 0;
1081 exit:
1082 return res;
1085 static int pegasus_close(struct net_device *net)
1087 pegasus_t *pegasus = netdev_priv(net);
1089 netif_stop_queue(net);
1090 if (!(pegasus->flags & PEGASUS_UNPLUG))
1091 disable_net_traffic(pegasus);
1092 tasklet_kill(&pegasus->rx_tl);
1093 unlink_all_urbs(pegasus);
1095 return 0;
1098 static void pegasus_get_drvinfo(struct net_device *dev,
1099 struct ethtool_drvinfo *info)
1101 pegasus_t *pegasus = netdev_priv(dev);
1102 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1103 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1104 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1107 /* also handles three patterns of some kind in hardware */
1108 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1110 static void
1111 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1113 pegasus_t *pegasus = netdev_priv(dev);
1115 wol->supported = WAKE_MAGIC | WAKE_PHY;
1116 wol->wolopts = pegasus->wolopts;
1119 static int
1120 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1122 pegasus_t *pegasus = netdev_priv(dev);
1123 u8 reg78 = 0x04;
1125 if (wol->wolopts & ~WOL_SUPPORTED)
1126 return -EINVAL;
1128 if (wol->wolopts & WAKE_MAGIC)
1129 reg78 |= 0x80;
1130 if (wol->wolopts & WAKE_PHY)
1131 reg78 |= 0x40;
1132 /* FIXME this 0x10 bit still needs to get set in the chip... */
1133 if (wol->wolopts)
1134 pegasus->eth_regs[0] |= 0x10;
1135 else
1136 pegasus->eth_regs[0] &= ~0x10;
1137 pegasus->wolopts = wol->wolopts;
1138 return set_register(pegasus, WakeupControl, reg78);
1141 static inline void pegasus_reset_wol(struct net_device *dev)
1143 struct ethtool_wolinfo wol;
1145 memset(&wol, 0, sizeof wol);
1146 (void) pegasus_set_wol(dev, &wol);
1149 static int
1150 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1152 pegasus_t *pegasus;
1154 pegasus = netdev_priv(dev);
1155 mii_ethtool_gset(&pegasus->mii, ecmd);
1156 return 0;
1159 static int
1160 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1162 pegasus_t *pegasus = netdev_priv(dev);
1163 return mii_ethtool_sset(&pegasus->mii, ecmd);
1166 static int pegasus_nway_reset(struct net_device *dev)
1168 pegasus_t *pegasus = netdev_priv(dev);
1169 return mii_nway_restart(&pegasus->mii);
1172 static u32 pegasus_get_link(struct net_device *dev)
1174 pegasus_t *pegasus = netdev_priv(dev);
1175 return mii_link_ok(&pegasus->mii);
1178 static u32 pegasus_get_msglevel(struct net_device *dev)
1180 pegasus_t *pegasus = netdev_priv(dev);
1181 return pegasus->msg_enable;
1184 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1186 pegasus_t *pegasus = netdev_priv(dev);
1187 pegasus->msg_enable = v;
1190 static const struct ethtool_ops ops = {
1191 .get_drvinfo = pegasus_get_drvinfo,
1192 .get_settings = pegasus_get_settings,
1193 .set_settings = pegasus_set_settings,
1194 .nway_reset = pegasus_nway_reset,
1195 .get_link = pegasus_get_link,
1196 .get_msglevel = pegasus_get_msglevel,
1197 .set_msglevel = pegasus_set_msglevel,
1198 .get_wol = pegasus_get_wol,
1199 .set_wol = pegasus_set_wol,
1202 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1204 __u16 *data = (__u16 *) & rq->ifr_ifru;
1205 pegasus_t *pegasus = netdev_priv(net);
1206 int res;
1208 switch (cmd) {
1209 case SIOCDEVPRIVATE:
1210 data[0] = pegasus->phy;
1211 case SIOCDEVPRIVATE + 1:
1212 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1213 res = 0;
1214 break;
1215 case SIOCDEVPRIVATE + 2:
1216 if (!capable(CAP_NET_ADMIN))
1217 return -EPERM;
1218 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1219 res = 0;
1220 break;
1221 default:
1222 res = -EOPNOTSUPP;
1224 return res;
1227 static void pegasus_set_multicast(struct net_device *net)
1229 pegasus_t *pegasus = netdev_priv(net);
1231 if (net->flags & IFF_PROMISC) {
1232 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1233 if (netif_msg_link(pegasus))
1234 pr_info("%s: Promiscuous mode enabled.\n", net->name);
1235 } else if (net->mc_count || (net->flags & IFF_ALLMULTI)) {
1236 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1237 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1238 if (netif_msg_link(pegasus))
1239 pr_debug("%s: set allmulti\n", net->name);
1240 } else {
1241 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1242 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1245 pegasus->ctrl_urb->status = 0;
1247 pegasus->flags |= ETH_REGS_CHANGE;
1248 ctrl_callback(pegasus->ctrl_urb);
1251 static __u8 mii_phy_probe(pegasus_t * pegasus)
1253 int i;
1254 __u16 tmp;
1256 for (i = 0; i < 32; i++) {
1257 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1258 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1259 continue;
1260 else
1261 return i;
1264 return 0xff;
1267 static inline void setup_pegasus_II(pegasus_t * pegasus)
1269 __u8 data = 0xa5;
1271 set_register(pegasus, Reg1d, 0);
1272 set_register(pegasus, Reg7b, 1);
1273 mdelay(100);
1274 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1275 set_register(pegasus, Reg7b, 0);
1276 else
1277 set_register(pegasus, Reg7b, 2);
1279 set_register(pegasus, 0x83, data);
1280 get_registers(pegasus, 0x83, 1, &data);
1282 if (data == 0xa5) {
1283 pegasus->chip = 0x8513;
1284 } else {
1285 pegasus->chip = 0;
1288 set_register(pegasus, 0x80, 0xc0);
1289 set_register(pegasus, 0x83, 0xff);
1290 set_register(pegasus, 0x84, 0x01);
1292 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1293 set_register(pegasus, Reg81, 6);
1294 else
1295 set_register(pegasus, Reg81, 2);
1299 static int pegasus_count;
1300 static struct workqueue_struct *pegasus_workqueue = NULL;
1301 #define CARRIER_CHECK_DELAY (2 * HZ)
1303 static void check_carrier(struct work_struct *work)
1305 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1306 set_carrier(pegasus->net);
1307 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1308 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1309 CARRIER_CHECK_DELAY);
1313 static int pegasus_blacklisted(struct usb_device *udev)
1315 struct usb_device_descriptor *udd = &udev->descriptor;
1317 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1318 * dongle which happens to have the same ID.
1320 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1321 (udd->idProduct == cpu_to_le16(0x0121)) &&
1322 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1323 (udd->bDeviceProtocol == 1))
1324 return 1;
1326 return 0;
1329 /* we rely on probe() and remove() being serialized so we
1330 * don't need extra locking on pegasus_count.
1332 static void pegasus_dec_workqueue(void)
1334 pegasus_count--;
1335 if (pegasus_count == 0) {
1336 destroy_workqueue(pegasus_workqueue);
1337 pegasus_workqueue = NULL;
1341 static int pegasus_probe(struct usb_interface *intf,
1342 const struct usb_device_id *id)
1344 struct usb_device *dev = interface_to_usbdev(intf);
1345 struct net_device *net;
1346 pegasus_t *pegasus;
1347 int dev_index = id - pegasus_ids;
1348 int res = -ENOMEM;
1350 if (pegasus_blacklisted(dev))
1351 return -ENODEV;
1353 if (pegasus_count == 0) {
1354 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1355 if (!pegasus_workqueue)
1356 return -ENOMEM;
1358 pegasus_count++;
1360 usb_get_dev(dev);
1362 net = alloc_etherdev(sizeof(struct pegasus));
1363 if (!net) {
1364 dev_err(&intf->dev, "can't allocate %s\n", "device");
1365 goto out;
1368 pegasus = netdev_priv(net);
1369 pegasus->dev_index = dev_index;
1370 init_waitqueue_head(&pegasus->ctrl_wait);
1372 if (!alloc_urbs(pegasus)) {
1373 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1374 goto out1;
1377 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1379 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1381 pegasus->intf = intf;
1382 pegasus->usb = dev;
1383 pegasus->net = net;
1386 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1387 net->netdev_ops = &pegasus_netdev_ops;
1388 SET_ETHTOOL_OPS(net, &ops);
1389 pegasus->mii.dev = net;
1390 pegasus->mii.mdio_read = mdio_read;
1391 pegasus->mii.mdio_write = mdio_write;
1392 pegasus->mii.phy_id_mask = 0x1f;
1393 pegasus->mii.reg_num_mask = 0x1f;
1394 spin_lock_init(&pegasus->rx_pool_lock);
1395 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1396 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1398 pegasus->features = usb_dev_id[dev_index].private;
1399 get_interrupt_interval(pegasus);
1400 if (reset_mac(pegasus)) {
1401 dev_err(&intf->dev, "can't reset MAC\n");
1402 res = -EIO;
1403 goto out2;
1405 set_ethernet_addr(pegasus);
1406 fill_skb_pool(pegasus);
1407 if (pegasus->features & PEGASUS_II) {
1408 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1409 setup_pegasus_II(pegasus);
1411 pegasus->phy = mii_phy_probe(pegasus);
1412 if (pegasus->phy == 0xff) {
1413 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1414 pegasus->phy = 1;
1416 pegasus->mii.phy_id = pegasus->phy;
1417 usb_set_intfdata(intf, pegasus);
1418 SET_NETDEV_DEV(net, &intf->dev);
1419 pegasus_reset_wol(net);
1420 res = register_netdev(net);
1421 if (res)
1422 goto out3;
1423 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1424 CARRIER_CHECK_DELAY);
1426 dev_info(&intf->dev, "%s, %s, %pM\n",
1427 net->name,
1428 usb_dev_id[dev_index].name,
1429 net->dev_addr);
1430 return 0;
1432 out3:
1433 usb_set_intfdata(intf, NULL);
1434 free_skb_pool(pegasus);
1435 out2:
1436 free_all_urbs(pegasus);
1437 out1:
1438 free_netdev(net);
1439 out:
1440 usb_put_dev(dev);
1441 pegasus_dec_workqueue();
1442 return res;
1445 static void pegasus_disconnect(struct usb_interface *intf)
1447 struct pegasus *pegasus = usb_get_intfdata(intf);
1449 usb_set_intfdata(intf, NULL);
1450 if (!pegasus) {
1451 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1452 return;
1455 pegasus->flags |= PEGASUS_UNPLUG;
1456 cancel_delayed_work(&pegasus->carrier_check);
1457 unregister_netdev(pegasus->net);
1458 usb_put_dev(interface_to_usbdev(intf));
1459 unlink_all_urbs(pegasus);
1460 free_all_urbs(pegasus);
1461 free_skb_pool(pegasus);
1462 if (pegasus->rx_skb != NULL) {
1463 dev_kfree_skb(pegasus->rx_skb);
1464 pegasus->rx_skb = NULL;
1466 free_netdev(pegasus->net);
1467 pegasus_dec_workqueue();
1470 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1472 struct pegasus *pegasus = usb_get_intfdata(intf);
1474 netif_device_detach (pegasus->net);
1475 cancel_delayed_work(&pegasus->carrier_check);
1476 if (netif_running(pegasus->net)) {
1477 usb_kill_urb(pegasus->rx_urb);
1478 usb_kill_urb(pegasus->intr_urb);
1480 return 0;
1483 static int pegasus_resume (struct usb_interface *intf)
1485 struct pegasus *pegasus = usb_get_intfdata(intf);
1487 netif_device_attach (pegasus->net);
1488 if (netif_running(pegasus->net)) {
1489 pegasus->rx_urb->status = 0;
1490 pegasus->rx_urb->actual_length = 0;
1491 read_bulk_callback(pegasus->rx_urb);
1493 pegasus->intr_urb->status = 0;
1494 pegasus->intr_urb->actual_length = 0;
1495 intr_callback(pegasus->intr_urb);
1497 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1498 CARRIER_CHECK_DELAY);
1499 return 0;
1502 static const struct net_device_ops pegasus_netdev_ops = {
1503 .ndo_open = pegasus_open,
1504 .ndo_stop = pegasus_close,
1505 .ndo_do_ioctl = pegasus_ioctl,
1506 .ndo_start_xmit = pegasus_start_xmit,
1507 .ndo_set_multicast_list = pegasus_set_multicast,
1508 .ndo_get_stats = pegasus_netdev_stats,
1509 .ndo_tx_timeout = pegasus_tx_timeout,
1510 .ndo_change_mtu = eth_change_mtu,
1511 .ndo_set_mac_address = eth_mac_addr,
1512 .ndo_validate_addr = eth_validate_addr,
1515 static struct usb_driver pegasus_driver = {
1516 .name = driver_name,
1517 .probe = pegasus_probe,
1518 .disconnect = pegasus_disconnect,
1519 .id_table = pegasus_ids,
1520 .suspend = pegasus_suspend,
1521 .resume = pegasus_resume,
1524 static void __init parse_id(char *id)
1526 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1527 char *token, *name=NULL;
1529 if ((token = strsep(&id, ":")) != NULL)
1530 name = token;
1531 /* name now points to a null terminated string*/
1532 if ((token = strsep(&id, ":")) != NULL)
1533 vendor_id = simple_strtoul(token, NULL, 16);
1534 if ((token = strsep(&id, ":")) != NULL)
1535 device_id = simple_strtoul(token, NULL, 16);
1536 flags = simple_strtoul(id, NULL, 16);
1537 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1538 driver_name, name, vendor_id, device_id, flags);
1540 if (vendor_id > 0x10000 || vendor_id == 0)
1541 return;
1542 if (device_id > 0x10000 || device_id == 0)
1543 return;
1545 for (i=0; usb_dev_id[i].name; i++);
1546 usb_dev_id[i].name = name;
1547 usb_dev_id[i].vendor = vendor_id;
1548 usb_dev_id[i].device = device_id;
1549 usb_dev_id[i].private = flags;
1550 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1551 pegasus_ids[i].idVendor = vendor_id;
1552 pegasus_ids[i].idProduct = device_id;
1555 static int __init pegasus_init(void)
1557 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1558 if (devid)
1559 parse_id(devid);
1560 return usb_register(&pegasus_driver);
1563 static void __exit pegasus_exit(void)
1565 usb_deregister(&pegasus_driver);
1568 module_init(pegasus_init);
1569 module_exit(pegasus_exit);