net: trans_start cleanups
[linux-2.6/cjktty.git] / drivers / net / usb / pegasus.c
blob1cd17d274a121f7119ccc3c6f566931adf769bd3
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 (net_ratelimit())
136 netif_dbg(pegasus, drv, pegasus->net,
137 "%s, status %d\n", __func__, status);
138 break;
140 pegasus->flags &= ~ETH_REGS_CHANGED;
141 wake_up(&pegasus->ctrl_wait);
144 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
145 void *data)
147 int ret;
148 char *buffer;
149 DECLARE_WAITQUEUE(wait, current);
151 buffer = kmalloc(size, GFP_KERNEL);
152 if (!buffer) {
153 netif_warn(pegasus, drv, pegasus->net,
154 "out of memory in %s\n", __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 (net_ratelimit())
185 netif_err(pegasus, drv, pegasus->net,
186 "%s, status %d\n", __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 netif_warn(pegasus, drv, pegasus->net,
209 "out of memory in %s\n", __func__);
210 return -ENOMEM;
212 memcpy(buffer, data, size);
214 add_wait_queue(&pegasus->ctrl_wait, &wait);
215 set_current_state(TASK_UNINTERRUPTIBLE);
216 while (pegasus->flags & ETH_REGS_CHANGED)
217 schedule();
218 remove_wait_queue(&pegasus->ctrl_wait, &wait);
219 set_current_state(TASK_RUNNING);
221 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
222 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
223 pegasus->dr.wValue = cpu_to_le16(0);
224 pegasus->dr.wIndex = cpu_to_le16(indx);
225 pegasus->dr.wLength = cpu_to_le16(size);
226 pegasus->ctrl_urb->transfer_buffer_length = size;
228 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
229 usb_sndctrlpipe(pegasus->usb, 0),
230 (char *) &pegasus->dr,
231 buffer, size, ctrl_callback, pegasus);
233 add_wait_queue(&pegasus->ctrl_wait, &wait);
234 set_current_state(TASK_UNINTERRUPTIBLE);
236 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
237 if (ret == -ENODEV)
238 netif_device_detach(pegasus->net);
239 netif_err(pegasus, drv, pegasus->net,
240 "%s, status %d\n", __func__, ret);
241 goto out;
244 schedule();
245 out:
246 remove_wait_queue(&pegasus->ctrl_wait, &wait);
247 kfree(buffer);
249 return ret;
252 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
254 int ret;
255 char *tmp;
256 DECLARE_WAITQUEUE(wait, current);
258 tmp = kmalloc(1, GFP_KERNEL);
259 if (!tmp) {
260 netif_warn(pegasus, drv, pegasus->net,
261 "out of memory in %s\n", __func__);
262 return -ENOMEM;
264 memcpy(tmp, &data, 1);
265 add_wait_queue(&pegasus->ctrl_wait, &wait);
266 set_current_state(TASK_UNINTERRUPTIBLE);
267 while (pegasus->flags & ETH_REGS_CHANGED)
268 schedule();
269 remove_wait_queue(&pegasus->ctrl_wait, &wait);
270 set_current_state(TASK_RUNNING);
272 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
273 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
274 pegasus->dr.wValue = cpu_to_le16(data);
275 pegasus->dr.wIndex = cpu_to_le16(indx);
276 pegasus->dr.wLength = cpu_to_le16(1);
277 pegasus->ctrl_urb->transfer_buffer_length = 1;
279 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
280 usb_sndctrlpipe(pegasus->usb, 0),
281 (char *) &pegasus->dr,
282 tmp, 1, ctrl_callback, pegasus);
284 add_wait_queue(&pegasus->ctrl_wait, &wait);
285 set_current_state(TASK_UNINTERRUPTIBLE);
287 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
288 if (ret == -ENODEV)
289 netif_device_detach(pegasus->net);
290 if (net_ratelimit())
291 netif_err(pegasus, drv, pegasus->net,
292 "%s, status %d\n", __func__, ret);
293 goto out;
296 schedule();
297 out:
298 remove_wait_queue(&pegasus->ctrl_wait, &wait);
299 kfree(tmp);
301 return ret;
304 static int update_eth_regs_async(pegasus_t * pegasus)
306 int ret;
308 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
309 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
310 pegasus->dr.wValue = cpu_to_le16(0);
311 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
312 pegasus->dr.wLength = cpu_to_le16(3);
313 pegasus->ctrl_urb->transfer_buffer_length = 3;
315 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
316 usb_sndctrlpipe(pegasus->usb, 0),
317 (char *) &pegasus->dr,
318 pegasus->eth_regs, 3, ctrl_callback, pegasus);
320 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
321 if (ret == -ENODEV)
322 netif_device_detach(pegasus->net);
323 netif_err(pegasus, drv, pegasus->net,
324 "%s, status %d\n", __func__, ret);
327 return ret;
330 /* Returns 0 on success, error on failure */
331 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
333 int i;
334 __u8 data[4] = { phy, 0, 0, indx };
335 __le16 regdi;
336 int ret;
338 set_register(pegasus, PhyCtrl, 0);
339 set_registers(pegasus, PhyAddr, sizeof (data), data);
340 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
341 for (i = 0; i < REG_TIMEOUT; i++) {
342 ret = get_registers(pegasus, PhyCtrl, 1, data);
343 if (ret == -ESHUTDOWN)
344 goto fail;
345 if (data[0] & PHY_DONE)
346 break;
349 if (i >= REG_TIMEOUT)
350 goto fail;
352 ret = get_registers(pegasus, PhyData, 2, &regdi);
353 *regd = le16_to_cpu(regdi);
354 return ret;
356 fail:
357 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
359 return ret;
362 static int mdio_read(struct net_device *dev, int phy_id, int loc)
364 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
365 u16 res;
367 read_mii_word(pegasus, phy_id, loc, &res);
368 return (int)res;
371 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
373 int i;
374 __u8 data[4] = { phy, 0, 0, indx };
375 int ret;
377 data[1] = (u8) regd;
378 data[2] = (u8) (regd >> 8);
379 set_register(pegasus, PhyCtrl, 0);
380 set_registers(pegasus, PhyAddr, sizeof(data), data);
381 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
382 for (i = 0; i < REG_TIMEOUT; i++) {
383 ret = get_registers(pegasus, PhyCtrl, 1, data);
384 if (ret == -ESHUTDOWN)
385 goto fail;
386 if (data[0] & PHY_DONE)
387 break;
390 if (i >= REG_TIMEOUT)
391 goto fail;
393 return ret;
395 fail:
396 netif_warn(pegasus, drv, pegasus->net, "%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 goto fail;
428 ret = get_registers(pegasus, EpromData, 2, &retdatai);
429 *retdata = le16_to_cpu(retdatai);
430 return ret;
432 fail:
433 netif_warn(pegasus, drv, pegasus->net, "%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 goto fail;
481 return ret;
483 fail:
484 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
485 return -ETIMEDOUT;
487 #endif /* PEGASUS_WRITE_EEPROM */
489 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
491 int i;
492 __u16 w16;
494 for (i = 0; i < 3; i++) {
495 read_eprom_word(pegasus, i, &w16);
496 ((__le16 *) id)[i] = cpu_to_le16(w16);
500 static void set_ethernet_addr(pegasus_t * pegasus)
502 __u8 node_id[6];
504 if (pegasus->features & PEGASUS_II) {
505 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
506 } else {
507 get_node_id(pegasus, node_id);
508 set_registers(pegasus, EthID, sizeof (node_id), node_id);
510 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
513 static inline int reset_mac(pegasus_t * pegasus)
515 __u8 data = 0x8;
516 int i;
518 set_register(pegasus, EthCtrl1, data);
519 for (i = 0; i < REG_TIMEOUT; i++) {
520 get_registers(pegasus, EthCtrl1, 1, &data);
521 if (~data & 0x08) {
522 if (loopback & 1)
523 break;
524 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
525 set_register(pegasus, Gpio1, 0x34);
526 else
527 set_register(pegasus, Gpio1, 0x26);
528 set_register(pegasus, Gpio0, pegasus->features);
529 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
530 break;
533 if (i == REG_TIMEOUT)
534 return -ETIMEDOUT;
536 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
537 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
538 set_register(pegasus, Gpio0, 0x24);
539 set_register(pegasus, Gpio0, 0x26);
541 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
542 __u16 auxmode;
543 read_mii_word(pegasus, 3, 0x1b, &auxmode);
544 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
547 return 0;
550 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
552 __u16 linkpart;
553 __u8 data[4];
554 pegasus_t *pegasus = netdev_priv(dev);
555 int ret;
557 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
558 data[0] = 0xc9;
559 data[1] = 0;
560 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
561 data[1] |= 0x20; /* set full duplex */
562 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
563 data[1] |= 0x10; /* set 100 Mbps */
564 if (mii_mode)
565 data[1] = 0;
566 data[2] = (loopback & 1) ? 0x09 : 0x01;
568 memcpy(pegasus->eth_regs, data, sizeof (data));
569 ret = set_registers(pegasus, EthCtrl0, 3, data);
571 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
572 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
573 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
574 u16 auxmode;
575 read_mii_word(pegasus, 0, 0x1b, &auxmode);
576 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
579 return ret;
582 static void fill_skb_pool(pegasus_t * pegasus)
584 int i;
586 for (i = 0; i < RX_SKBS; i++) {
587 if (pegasus->rx_pool[i])
588 continue;
589 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
591 ** we give up if the allocation fail. the tasklet will be
592 ** rescheduled again anyway...
594 if (pegasus->rx_pool[i] == NULL)
595 return;
596 skb_reserve(pegasus->rx_pool[i], 2);
600 static void free_skb_pool(pegasus_t * pegasus)
602 int i;
604 for (i = 0; i < RX_SKBS; i++) {
605 if (pegasus->rx_pool[i]) {
606 dev_kfree_skb(pegasus->rx_pool[i]);
607 pegasus->rx_pool[i] = NULL;
612 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
614 int i;
615 struct sk_buff *skb;
617 for (i = 0; i < RX_SKBS; i++) {
618 if (likely(pegasus->rx_pool[i] != NULL)) {
619 skb = pegasus->rx_pool[i];
620 pegasus->rx_pool[i] = NULL;
621 return skb;
624 return NULL;
627 static void read_bulk_callback(struct urb *urb)
629 pegasus_t *pegasus = urb->context;
630 struct net_device *net;
631 int rx_status, count = urb->actual_length;
632 int status = urb->status;
633 u8 *buf = urb->transfer_buffer;
634 __u16 pkt_len;
636 if (!pegasus)
637 return;
639 net = pegasus->net;
640 if (!netif_device_present(net) || !netif_running(net))
641 return;
643 switch (status) {
644 case 0:
645 break;
646 case -ETIME:
647 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
648 pegasus->flags &= ~PEGASUS_RX_BUSY;
649 break;
650 case -EPIPE: /* stall, or disconnect from TT */
651 /* FIXME schedule work to clear the halt */
652 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
653 return;
654 case -ENOENT:
655 case -ECONNRESET:
656 case -ESHUTDOWN:
657 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
658 return;
659 default:
660 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
661 goto goon;
664 if (!count || count < 4)
665 goto goon;
667 rx_status = buf[count - 2];
668 if (rx_status & 0x1e) {
669 netif_dbg(pegasus, rx_err, net,
670 "RX packet error %x\n", rx_status);
671 pegasus->stats.rx_errors++;
672 if (rx_status & 0x06) // long or runt
673 pegasus->stats.rx_length_errors++;
674 if (rx_status & 0x08)
675 pegasus->stats.rx_crc_errors++;
676 if (rx_status & 0x10) // extra bits
677 pegasus->stats.rx_frame_errors++;
678 goto goon;
680 if (pegasus->chip == 0x8513) {
681 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
682 pkt_len &= 0x0fff;
683 pegasus->rx_skb->data += 2;
684 } else {
685 pkt_len = buf[count - 3] << 8;
686 pkt_len += buf[count - 4];
687 pkt_len &= 0xfff;
688 pkt_len -= 8;
692 * If the packet is unreasonably long, quietly drop it rather than
693 * kernel panicing by calling skb_put.
695 if (pkt_len > PEGASUS_MTU)
696 goto goon;
699 * at this point we are sure pegasus->rx_skb != NULL
700 * so we go ahead and pass up the packet.
702 skb_put(pegasus->rx_skb, pkt_len);
703 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
704 netif_rx(pegasus->rx_skb);
705 pegasus->stats.rx_packets++;
706 pegasus->stats.rx_bytes += pkt_len;
708 if (pegasus->flags & PEGASUS_UNPLUG)
709 return;
711 spin_lock(&pegasus->rx_pool_lock);
712 pegasus->rx_skb = pull_skb(pegasus);
713 spin_unlock(&pegasus->rx_pool_lock);
715 if (pegasus->rx_skb == NULL)
716 goto tl_sched;
717 goon:
718 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
719 usb_rcvbulkpipe(pegasus->usb, 1),
720 pegasus->rx_skb->data, PEGASUS_MTU + 8,
721 read_bulk_callback, pegasus);
722 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
723 if (rx_status == -ENODEV)
724 netif_device_detach(pegasus->net);
725 else if (rx_status) {
726 pegasus->flags |= PEGASUS_RX_URB_FAIL;
727 goto tl_sched;
728 } else {
729 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
732 return;
734 tl_sched:
735 tasklet_schedule(&pegasus->rx_tl);
738 static void rx_fixup(unsigned long data)
740 pegasus_t *pegasus;
741 unsigned long flags;
742 int status;
744 pegasus = (pegasus_t *) data;
745 if (pegasus->flags & PEGASUS_UNPLUG)
746 return;
748 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
749 fill_skb_pool(pegasus);
750 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
751 if (pegasus->rx_skb)
752 goto try_again;
753 if (pegasus->rx_skb == NULL) {
754 pegasus->rx_skb = pull_skb(pegasus);
756 if (pegasus->rx_skb == NULL) {
757 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
758 tasklet_schedule(&pegasus->rx_tl);
759 goto done;
761 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
762 usb_rcvbulkpipe(pegasus->usb, 1),
763 pegasus->rx_skb->data, PEGASUS_MTU + 8,
764 read_bulk_callback, pegasus);
765 try_again:
766 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
767 if (status == -ENODEV)
768 netif_device_detach(pegasus->net);
769 else if (status) {
770 pegasus->flags |= PEGASUS_RX_URB_FAIL;
771 tasklet_schedule(&pegasus->rx_tl);
772 } else {
773 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
775 done:
776 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
779 static void write_bulk_callback(struct urb *urb)
781 pegasus_t *pegasus = urb->context;
782 struct net_device *net;
783 int status = urb->status;
785 if (!pegasus)
786 return;
788 net = pegasus->net;
790 if (!netif_device_present(net) || !netif_running(net))
791 return;
793 switch (status) {
794 case -EPIPE:
795 /* FIXME schedule_work() to clear the tx halt */
796 netif_stop_queue(net);
797 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
798 return;
799 case -ENOENT:
800 case -ECONNRESET:
801 case -ESHUTDOWN:
802 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
803 return;
804 default:
805 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
806 /* FALL THROUGH */
807 case 0:
808 break;
811 net->trans_start = jiffies; /* prevent tx timeout */
812 netif_wake_queue(net);
815 static void intr_callback(struct urb *urb)
817 pegasus_t *pegasus = urb->context;
818 struct net_device *net;
819 int res, status = urb->status;
821 if (!pegasus)
822 return;
823 net = pegasus->net;
825 switch (status) {
826 case 0:
827 break;
828 case -ECONNRESET: /* unlink */
829 case -ENOENT:
830 case -ESHUTDOWN:
831 return;
832 default:
833 /* some Pegasus-I products report LOTS of data
834 * toggle errors... avoid log spamming
836 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
839 if (urb->actual_length >= 6) {
840 u8 * d = urb->transfer_buffer;
842 /* byte 0 == tx_status1, reg 2B */
843 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
844 |LATE_COL|JABBER_TIMEOUT)) {
845 pegasus->stats.tx_errors++;
846 if (d[0] & TX_UNDERRUN)
847 pegasus->stats.tx_fifo_errors++;
848 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
849 pegasus->stats.tx_aborted_errors++;
850 if (d[0] & LATE_COL)
851 pegasus->stats.tx_window_errors++;
854 /* d[5].LINK_STATUS lies on some adapters.
855 * d[0].NO_CARRIER kicks in only with failed TX.
856 * ... so monitoring with MII may be safest.
859 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
860 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
863 res = usb_submit_urb(urb, GFP_ATOMIC);
864 if (res == -ENODEV)
865 netif_device_detach(pegasus->net);
866 if (res)
867 netif_err(pegasus, timer, net,
868 "can't resubmit interrupt urb, %d\n", res);
871 static void pegasus_tx_timeout(struct net_device *net)
873 pegasus_t *pegasus = netdev_priv(net);
874 netif_warn(pegasus, timer, net, "tx timeout\n");
875 usb_unlink_urb(pegasus->tx_urb);
876 pegasus->stats.tx_errors++;
879 static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
880 struct net_device *net)
882 pegasus_t *pegasus = netdev_priv(net);
883 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
884 int res;
885 __u16 l16 = skb->len;
887 netif_stop_queue(net);
889 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
890 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
891 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
892 usb_sndbulkpipe(pegasus->usb, 2),
893 pegasus->tx_buff, count,
894 write_bulk_callback, pegasus);
895 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
896 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
897 switch (res) {
898 case -EPIPE: /* stall, or disconnect from TT */
899 /* cleanup should already have been scheduled */
900 break;
901 case -ENODEV: /* disconnect() upcoming */
902 case -EPERM:
903 netif_device_detach(pegasus->net);
904 break;
905 default:
906 pegasus->stats.tx_errors++;
907 netif_start_queue(net);
909 } else {
910 pegasus->stats.tx_packets++;
911 pegasus->stats.tx_bytes += skb->len;
913 dev_kfree_skb(skb);
915 return NETDEV_TX_OK;
918 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
920 return &((pegasus_t *) netdev_priv(dev))->stats;
923 static inline void disable_net_traffic(pegasus_t * pegasus)
925 __le16 tmp = cpu_to_le16(0);
927 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
930 static inline void get_interrupt_interval(pegasus_t * pegasus)
932 u16 data;
933 u8 interval;
935 read_eprom_word(pegasus, 4, &data);
936 interval = data >> 8;
937 if (pegasus->usb->speed != USB_SPEED_HIGH) {
938 if (interval < 0x80) {
939 netif_info(pegasus, timer, pegasus->net,
940 "intr interval changed from %ums to %ums\n",
941 interval, 0x80);
942 interval = 0x80;
943 data = (data & 0x00FF) | ((u16)interval << 8);
944 #ifdef PEGASUS_WRITE_EEPROM
945 write_eprom_word(pegasus, 4, data);
946 #endif
949 pegasus->intr_interval = interval;
952 static void set_carrier(struct net_device *net)
954 pegasus_t *pegasus = netdev_priv(net);
955 u16 tmp;
957 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
958 return;
960 if (tmp & BMSR_LSTATUS)
961 netif_carrier_on(net);
962 else
963 netif_carrier_off(net);
966 static void free_all_urbs(pegasus_t * pegasus)
968 usb_free_urb(pegasus->intr_urb);
969 usb_free_urb(pegasus->tx_urb);
970 usb_free_urb(pegasus->rx_urb);
971 usb_free_urb(pegasus->ctrl_urb);
974 static void unlink_all_urbs(pegasus_t * pegasus)
976 usb_kill_urb(pegasus->intr_urb);
977 usb_kill_urb(pegasus->tx_urb);
978 usb_kill_urb(pegasus->rx_urb);
979 usb_kill_urb(pegasus->ctrl_urb);
982 static int alloc_urbs(pegasus_t * pegasus)
984 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
985 if (!pegasus->ctrl_urb) {
986 return 0;
988 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
989 if (!pegasus->rx_urb) {
990 usb_free_urb(pegasus->ctrl_urb);
991 return 0;
993 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
994 if (!pegasus->tx_urb) {
995 usb_free_urb(pegasus->rx_urb);
996 usb_free_urb(pegasus->ctrl_urb);
997 return 0;
999 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1000 if (!pegasus->intr_urb) {
1001 usb_free_urb(pegasus->tx_urb);
1002 usb_free_urb(pegasus->rx_urb);
1003 usb_free_urb(pegasus->ctrl_urb);
1004 return 0;
1007 return 1;
1010 static int pegasus_open(struct net_device *net)
1012 pegasus_t *pegasus = netdev_priv(net);
1013 int res;
1015 if (pegasus->rx_skb == NULL)
1016 pegasus->rx_skb = pull_skb(pegasus);
1018 ** Note: no point to free the pool. it is empty :-)
1020 if (!pegasus->rx_skb)
1021 return -ENOMEM;
1023 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1025 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1026 usb_rcvbulkpipe(pegasus->usb, 1),
1027 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1028 read_bulk_callback, pegasus);
1029 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1030 if (res == -ENODEV)
1031 netif_device_detach(pegasus->net);
1032 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
1033 goto exit;
1036 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1037 usb_rcvintpipe(pegasus->usb, 3),
1038 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1039 intr_callback, pegasus, pegasus->intr_interval);
1040 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1041 if (res == -ENODEV)
1042 netif_device_detach(pegasus->net);
1043 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
1044 usb_kill_urb(pegasus->rx_urb);
1045 goto exit;
1047 if ((res = enable_net_traffic(net, pegasus->usb))) {
1048 netif_dbg(pegasus, ifup, net,
1049 "can't enable_net_traffic() - %d\n", 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;
1056 set_carrier(net);
1057 netif_start_queue(net);
1058 netif_dbg(pegasus, ifup, net, "open\n");
1059 res = 0;
1060 exit:
1061 return res;
1064 static int pegasus_close(struct net_device *net)
1066 pegasus_t *pegasus = netdev_priv(net);
1068 netif_stop_queue(net);
1069 if (!(pegasus->flags & PEGASUS_UNPLUG))
1070 disable_net_traffic(pegasus);
1071 tasklet_kill(&pegasus->rx_tl);
1072 unlink_all_urbs(pegasus);
1074 return 0;
1077 static void pegasus_get_drvinfo(struct net_device *dev,
1078 struct ethtool_drvinfo *info)
1080 pegasus_t *pegasus = netdev_priv(dev);
1081 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1082 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1083 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1086 /* also handles three patterns of some kind in hardware */
1087 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1089 static void
1090 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1092 pegasus_t *pegasus = netdev_priv(dev);
1094 wol->supported = WAKE_MAGIC | WAKE_PHY;
1095 wol->wolopts = pegasus->wolopts;
1098 static int
1099 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1101 pegasus_t *pegasus = netdev_priv(dev);
1102 u8 reg78 = 0x04;
1104 if (wol->wolopts & ~WOL_SUPPORTED)
1105 return -EINVAL;
1107 if (wol->wolopts & WAKE_MAGIC)
1108 reg78 |= 0x80;
1109 if (wol->wolopts & WAKE_PHY)
1110 reg78 |= 0x40;
1111 /* FIXME this 0x10 bit still needs to get set in the chip... */
1112 if (wol->wolopts)
1113 pegasus->eth_regs[0] |= 0x10;
1114 else
1115 pegasus->eth_regs[0] &= ~0x10;
1116 pegasus->wolopts = wol->wolopts;
1117 return set_register(pegasus, WakeupControl, reg78);
1120 static inline void pegasus_reset_wol(struct net_device *dev)
1122 struct ethtool_wolinfo wol;
1124 memset(&wol, 0, sizeof wol);
1125 (void) pegasus_set_wol(dev, &wol);
1128 static int
1129 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1131 pegasus_t *pegasus;
1133 pegasus = netdev_priv(dev);
1134 mii_ethtool_gset(&pegasus->mii, ecmd);
1135 return 0;
1138 static int
1139 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1141 pegasus_t *pegasus = netdev_priv(dev);
1142 return mii_ethtool_sset(&pegasus->mii, ecmd);
1145 static int pegasus_nway_reset(struct net_device *dev)
1147 pegasus_t *pegasus = netdev_priv(dev);
1148 return mii_nway_restart(&pegasus->mii);
1151 static u32 pegasus_get_link(struct net_device *dev)
1153 pegasus_t *pegasus = netdev_priv(dev);
1154 return mii_link_ok(&pegasus->mii);
1157 static u32 pegasus_get_msglevel(struct net_device *dev)
1159 pegasus_t *pegasus = netdev_priv(dev);
1160 return pegasus->msg_enable;
1163 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1165 pegasus_t *pegasus = netdev_priv(dev);
1166 pegasus->msg_enable = v;
1169 static const struct ethtool_ops ops = {
1170 .get_drvinfo = pegasus_get_drvinfo,
1171 .get_settings = pegasus_get_settings,
1172 .set_settings = pegasus_set_settings,
1173 .nway_reset = pegasus_nway_reset,
1174 .get_link = pegasus_get_link,
1175 .get_msglevel = pegasus_get_msglevel,
1176 .set_msglevel = pegasus_set_msglevel,
1177 .get_wol = pegasus_get_wol,
1178 .set_wol = pegasus_set_wol,
1181 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1183 __u16 *data = (__u16 *) & rq->ifr_ifru;
1184 pegasus_t *pegasus = netdev_priv(net);
1185 int res;
1187 switch (cmd) {
1188 case SIOCDEVPRIVATE:
1189 data[0] = pegasus->phy;
1190 case SIOCDEVPRIVATE + 1:
1191 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1192 res = 0;
1193 break;
1194 case SIOCDEVPRIVATE + 2:
1195 if (!capable(CAP_NET_ADMIN))
1196 return -EPERM;
1197 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1198 res = 0;
1199 break;
1200 default:
1201 res = -EOPNOTSUPP;
1203 return res;
1206 static void pegasus_set_multicast(struct net_device *net)
1208 pegasus_t *pegasus = netdev_priv(net);
1210 if (net->flags & IFF_PROMISC) {
1211 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1212 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1213 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1214 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1215 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1216 netif_dbg(pegasus, link, net, "set allmulti\n");
1217 } else {
1218 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1219 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1222 pegasus->ctrl_urb->status = 0;
1224 pegasus->flags |= ETH_REGS_CHANGE;
1225 ctrl_callback(pegasus->ctrl_urb);
1228 static __u8 mii_phy_probe(pegasus_t * pegasus)
1230 int i;
1231 __u16 tmp;
1233 for (i = 0; i < 32; i++) {
1234 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1235 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1236 continue;
1237 else
1238 return i;
1241 return 0xff;
1244 static inline void setup_pegasus_II(pegasus_t * pegasus)
1246 __u8 data = 0xa5;
1248 set_register(pegasus, Reg1d, 0);
1249 set_register(pegasus, Reg7b, 1);
1250 mdelay(100);
1251 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1252 set_register(pegasus, Reg7b, 0);
1253 else
1254 set_register(pegasus, Reg7b, 2);
1256 set_register(pegasus, 0x83, data);
1257 get_registers(pegasus, 0x83, 1, &data);
1259 if (data == 0xa5) {
1260 pegasus->chip = 0x8513;
1261 } else {
1262 pegasus->chip = 0;
1265 set_register(pegasus, 0x80, 0xc0);
1266 set_register(pegasus, 0x83, 0xff);
1267 set_register(pegasus, 0x84, 0x01);
1269 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1270 set_register(pegasus, Reg81, 6);
1271 else
1272 set_register(pegasus, Reg81, 2);
1276 static int pegasus_count;
1277 static struct workqueue_struct *pegasus_workqueue = NULL;
1278 #define CARRIER_CHECK_DELAY (2 * HZ)
1280 static void check_carrier(struct work_struct *work)
1282 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1283 set_carrier(pegasus->net);
1284 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1285 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1286 CARRIER_CHECK_DELAY);
1290 static int pegasus_blacklisted(struct usb_device *udev)
1292 struct usb_device_descriptor *udd = &udev->descriptor;
1294 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1295 * dongle which happens to have the same ID.
1297 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1298 (udd->idProduct == cpu_to_le16(0x0121)) &&
1299 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1300 (udd->bDeviceProtocol == 1))
1301 return 1;
1303 return 0;
1306 /* we rely on probe() and remove() being serialized so we
1307 * don't need extra locking on pegasus_count.
1309 static void pegasus_dec_workqueue(void)
1311 pegasus_count--;
1312 if (pegasus_count == 0) {
1313 destroy_workqueue(pegasus_workqueue);
1314 pegasus_workqueue = NULL;
1318 static int pegasus_probe(struct usb_interface *intf,
1319 const struct usb_device_id *id)
1321 struct usb_device *dev = interface_to_usbdev(intf);
1322 struct net_device *net;
1323 pegasus_t *pegasus;
1324 int dev_index = id - pegasus_ids;
1325 int res = -ENOMEM;
1327 if (pegasus_blacklisted(dev))
1328 return -ENODEV;
1330 if (pegasus_count == 0) {
1331 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1332 if (!pegasus_workqueue)
1333 return -ENOMEM;
1335 pegasus_count++;
1337 usb_get_dev(dev);
1339 net = alloc_etherdev(sizeof(struct pegasus));
1340 if (!net) {
1341 dev_err(&intf->dev, "can't allocate %s\n", "device");
1342 goto out;
1345 pegasus = netdev_priv(net);
1346 pegasus->dev_index = dev_index;
1347 init_waitqueue_head(&pegasus->ctrl_wait);
1349 if (!alloc_urbs(pegasus)) {
1350 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1351 goto out1;
1354 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1356 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1358 pegasus->intf = intf;
1359 pegasus->usb = dev;
1360 pegasus->net = net;
1363 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1364 net->netdev_ops = &pegasus_netdev_ops;
1365 SET_ETHTOOL_OPS(net, &ops);
1366 pegasus->mii.dev = net;
1367 pegasus->mii.mdio_read = mdio_read;
1368 pegasus->mii.mdio_write = mdio_write;
1369 pegasus->mii.phy_id_mask = 0x1f;
1370 pegasus->mii.reg_num_mask = 0x1f;
1371 spin_lock_init(&pegasus->rx_pool_lock);
1372 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1373 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1375 pegasus->features = usb_dev_id[dev_index].private;
1376 get_interrupt_interval(pegasus);
1377 if (reset_mac(pegasus)) {
1378 dev_err(&intf->dev, "can't reset MAC\n");
1379 res = -EIO;
1380 goto out2;
1382 set_ethernet_addr(pegasus);
1383 fill_skb_pool(pegasus);
1384 if (pegasus->features & PEGASUS_II) {
1385 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1386 setup_pegasus_II(pegasus);
1388 pegasus->phy = mii_phy_probe(pegasus);
1389 if (pegasus->phy == 0xff) {
1390 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1391 pegasus->phy = 1;
1393 pegasus->mii.phy_id = pegasus->phy;
1394 usb_set_intfdata(intf, pegasus);
1395 SET_NETDEV_DEV(net, &intf->dev);
1396 pegasus_reset_wol(net);
1397 res = register_netdev(net);
1398 if (res)
1399 goto out3;
1400 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1401 CARRIER_CHECK_DELAY);
1403 dev_info(&intf->dev, "%s, %s, %pM\n",
1404 net->name,
1405 usb_dev_id[dev_index].name,
1406 net->dev_addr);
1407 return 0;
1409 out3:
1410 usb_set_intfdata(intf, NULL);
1411 free_skb_pool(pegasus);
1412 out2:
1413 free_all_urbs(pegasus);
1414 out1:
1415 free_netdev(net);
1416 out:
1417 usb_put_dev(dev);
1418 pegasus_dec_workqueue();
1419 return res;
1422 static void pegasus_disconnect(struct usb_interface *intf)
1424 struct pegasus *pegasus = usb_get_intfdata(intf);
1426 usb_set_intfdata(intf, NULL);
1427 if (!pegasus) {
1428 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1429 return;
1432 pegasus->flags |= PEGASUS_UNPLUG;
1433 cancel_delayed_work(&pegasus->carrier_check);
1434 unregister_netdev(pegasus->net);
1435 usb_put_dev(interface_to_usbdev(intf));
1436 unlink_all_urbs(pegasus);
1437 free_all_urbs(pegasus);
1438 free_skb_pool(pegasus);
1439 if (pegasus->rx_skb != NULL) {
1440 dev_kfree_skb(pegasus->rx_skb);
1441 pegasus->rx_skb = NULL;
1443 free_netdev(pegasus->net);
1444 pegasus_dec_workqueue();
1447 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1449 struct pegasus *pegasus = usb_get_intfdata(intf);
1451 netif_device_detach (pegasus->net);
1452 cancel_delayed_work(&pegasus->carrier_check);
1453 if (netif_running(pegasus->net)) {
1454 usb_kill_urb(pegasus->rx_urb);
1455 usb_kill_urb(pegasus->intr_urb);
1457 return 0;
1460 static int pegasus_resume (struct usb_interface *intf)
1462 struct pegasus *pegasus = usb_get_intfdata(intf);
1464 netif_device_attach (pegasus->net);
1465 if (netif_running(pegasus->net)) {
1466 pegasus->rx_urb->status = 0;
1467 pegasus->rx_urb->actual_length = 0;
1468 read_bulk_callback(pegasus->rx_urb);
1470 pegasus->intr_urb->status = 0;
1471 pegasus->intr_urb->actual_length = 0;
1472 intr_callback(pegasus->intr_urb);
1474 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1475 CARRIER_CHECK_DELAY);
1476 return 0;
1479 static const struct net_device_ops pegasus_netdev_ops = {
1480 .ndo_open = pegasus_open,
1481 .ndo_stop = pegasus_close,
1482 .ndo_do_ioctl = pegasus_ioctl,
1483 .ndo_start_xmit = pegasus_start_xmit,
1484 .ndo_set_multicast_list = pegasus_set_multicast,
1485 .ndo_get_stats = pegasus_netdev_stats,
1486 .ndo_tx_timeout = pegasus_tx_timeout,
1487 .ndo_change_mtu = eth_change_mtu,
1488 .ndo_set_mac_address = eth_mac_addr,
1489 .ndo_validate_addr = eth_validate_addr,
1492 static struct usb_driver pegasus_driver = {
1493 .name = driver_name,
1494 .probe = pegasus_probe,
1495 .disconnect = pegasus_disconnect,
1496 .id_table = pegasus_ids,
1497 .suspend = pegasus_suspend,
1498 .resume = pegasus_resume,
1501 static void __init parse_id(char *id)
1503 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1504 char *token, *name=NULL;
1506 if ((token = strsep(&id, ":")) != NULL)
1507 name = token;
1508 /* name now points to a null terminated string*/
1509 if ((token = strsep(&id, ":")) != NULL)
1510 vendor_id = simple_strtoul(token, NULL, 16);
1511 if ((token = strsep(&id, ":")) != NULL)
1512 device_id = simple_strtoul(token, NULL, 16);
1513 flags = simple_strtoul(id, NULL, 16);
1514 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1515 driver_name, name, vendor_id, device_id, flags);
1517 if (vendor_id > 0x10000 || vendor_id == 0)
1518 return;
1519 if (device_id > 0x10000 || device_id == 0)
1520 return;
1522 for (i=0; usb_dev_id[i].name; i++);
1523 usb_dev_id[i].name = name;
1524 usb_dev_id[i].vendor = vendor_id;
1525 usb_dev_id[i].device = device_id;
1526 usb_dev_id[i].private = flags;
1527 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1528 pegasus_ids[i].idVendor = vendor_id;
1529 pegasus_ids[i].idProduct = device_id;
1532 static int __init pegasus_init(void)
1534 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1535 if (devid)
1536 parse_id(devid);
1537 return usb_register(&pegasus_driver);
1540 static void __exit pegasus_exit(void)
1542 usb_deregister(&pegasus_driver);
1545 module_init(pegasus_init);
1546 module_exit(pegasus_exit);