WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / usb / pegasus.c
blob32e1335c94ad03948079c24f961fe24ef360ed94
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
5 * ChangeLog:
6 * .... Most of the time spent on reading sources & docs.
7 * v0.2.x First official release for the Linux kernel.
8 * v0.3.0 Beutified and structured, some bugs fixed.
9 * v0.3.x URBifying bulk requests and bugfixing. First relatively
10 * stable release. Still can touch device's registers only
11 * from top-halves.
12 * v0.4.0 Control messages remained unurbified are now URBs.
13 * Now we can touch the HW at any time.
14 * v0.4.9 Control urbs again use process context to wait. Argh...
15 * Some long standing bugs (enable_net_traffic) fixed.
16 * Also nasty trick about resubmiting control urb from
17 * interrupt context used. Please let me know how it
18 * behaves. Pegasus II support added since this version.
19 * TODO: suppressing HCD warnings spewage on disconnect.
20 * v0.4.13 Ethernet address is now set at probe(), not at open()
21 * time as this seems to break dhcpd.
22 * v0.5.0 branch to 2.5.x kernels
23 * v0.5.1 ethtool support added
24 * v0.5.5 rx socket buffers are in a pool and the their allocation
25 * is out of the interrupt routine.
26 * ...
27 * v0.9.3 simplified [get|set]_register(s), async update registers
28 * logic revisited, receive skb_pool removed.
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 <linux/uaccess.h>
43 #include "pegasus.h"
46 * Version Information
48 #define DRIVER_VERSION "v0.9.3 (2013/04/25)"
49 #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
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)
57 #define CARRIER_CHECK_DELAY (2 * HZ)
59 static bool loopback;
60 static bool mii_mode;
61 static char *devid;
63 static struct usb_eth_dev usb_dev_id[] = {
64 #define PEGASUS_DEV(pn, vid, pid, flags) \
65 {.name = pn, .vendor = vid, .device = pid, .private = flags},
66 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
67 PEGASUS_DEV(pn, vid, pid, flags)
68 #include "pegasus.h"
69 #undef PEGASUS_DEV
70 #undef PEGASUS_DEV_CLASS
71 {NULL, 0, 0, 0},
72 {NULL, 0, 0, 0}
75 static struct usb_device_id pegasus_ids[] = {
76 #define PEGASUS_DEV(pn, vid, pid, flags) \
77 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
79 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
80 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
81 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
82 * case anyway, seeing as the pegasus is for "Wired" adaptors.
84 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
85 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
86 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
87 #include "pegasus.h"
88 #undef PEGASUS_DEV
89 #undef PEGASUS_DEV_CLASS
90 {},
94 MODULE_AUTHOR(DRIVER_AUTHOR);
95 MODULE_DESCRIPTION(DRIVER_DESC);
96 MODULE_LICENSE("GPL");
97 module_param(loopback, bool, 0);
98 module_param(mii_mode, bool, 0);
99 module_param(devid, charp, 0);
100 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
101 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
102 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
104 /* use ethtool to change the level for any given device */
105 static int msg_level = -1;
106 module_param(msg_level, int, 0);
107 MODULE_PARM_DESC(msg_level, "Override default message level");
109 MODULE_DEVICE_TABLE(usb, pegasus_ids);
110 static const struct net_device_ops pegasus_netdev_ops;
112 /*****/
114 static void async_ctrl_callback(struct urb *urb)
116 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
117 int status = urb->status;
119 if (status < 0)
120 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
121 kfree(req);
122 usb_free_urb(urb);
125 static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
127 return usb_control_msg_recv(pegasus->usb, 0, PEGASUS_REQ_GET_REGS,
128 PEGASUS_REQT_READ, 0, indx, data, size,
129 1000, GFP_NOIO);
132 static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
133 const void *data)
135 return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REGS,
136 PEGASUS_REQT_WRITE, 0, indx, data, size,
137 1000, GFP_NOIO);
141 * There is only one way to write to a single ADM8511 register and this is via
142 * specific control request. 'data' is ignored by the device, but it is here to
143 * not break the API.
145 static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
147 void *buf = &data;
149 return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REG,
150 PEGASUS_REQT_WRITE, data, indx, buf, 1,
151 1000, GFP_NOIO);
154 static int update_eth_regs_async(pegasus_t *pegasus)
156 int ret = -ENOMEM;
157 struct urb *async_urb;
158 struct usb_ctrlrequest *req;
160 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
161 if (req == NULL)
162 return ret;
164 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
165 if (async_urb == NULL) {
166 kfree(req);
167 return ret;
169 req->bRequestType = PEGASUS_REQT_WRITE;
170 req->bRequest = PEGASUS_REQ_SET_REGS;
171 req->wValue = cpu_to_le16(0);
172 req->wIndex = cpu_to_le16(EthCtrl0);
173 req->wLength = cpu_to_le16(3);
175 usb_fill_control_urb(async_urb, pegasus->usb,
176 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
177 pegasus->eth_regs, 3, async_ctrl_callback, req);
179 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
180 if (ret) {
181 if (ret == -ENODEV)
182 netif_device_detach(pegasus->net);
183 netif_err(pegasus, drv, pegasus->net,
184 "%s returned %d\n", __func__, ret);
186 return ret;
189 static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
191 int i;
192 __u8 data[4] = { phy, 0, 0, indx };
193 __le16 regdi;
194 int ret = -ETIMEDOUT;
196 if (cmd & PHY_WRITE) {
197 __le16 *t = (__le16 *) & data[1];
198 *t = cpu_to_le16(*regd);
200 set_register(p, PhyCtrl, 0);
201 set_registers(p, PhyAddr, sizeof(data), data);
202 set_register(p, PhyCtrl, (indx | cmd));
203 for (i = 0; i < REG_TIMEOUT; i++) {
204 ret = get_registers(p, PhyCtrl, 1, data);
205 if (ret < 0)
206 goto fail;
207 if (data[0] & PHY_DONE)
208 break;
210 if (i >= REG_TIMEOUT)
211 goto fail;
212 if (cmd & PHY_READ) {
213 ret = get_registers(p, PhyData, 2, &regdi);
214 *regd = le16_to_cpu(regdi);
215 return ret;
217 return 0;
218 fail:
219 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
220 return ret;
223 /* Returns non-negative int on success, error on failure */
224 static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
226 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
229 /* Returns zero on success, error on failure */
230 static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
232 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
235 static int mdio_read(struct net_device *dev, int phy_id, int loc)
237 pegasus_t *pegasus = netdev_priv(dev);
238 u16 res;
240 read_mii_word(pegasus, phy_id, loc, &res);
241 return (int)res;
244 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
246 pegasus_t *pegasus = netdev_priv(dev);
247 u16 data = val;
249 write_mii_word(pegasus, phy_id, loc, &data);
252 static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
254 int i;
255 __u8 tmp = 0;
256 __le16 retdatai;
257 int ret;
259 set_register(pegasus, EpromCtrl, 0);
260 set_register(pegasus, EpromOffset, index);
261 set_register(pegasus, EpromCtrl, EPROM_READ);
263 for (i = 0; i < REG_TIMEOUT; i++) {
264 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
265 if (tmp & EPROM_DONE)
266 break;
267 if (ret == -ESHUTDOWN)
268 goto fail;
270 if (i >= REG_TIMEOUT)
271 goto fail;
273 ret = get_registers(pegasus, EpromData, 2, &retdatai);
274 *retdata = le16_to_cpu(retdatai);
275 return ret;
277 fail:
278 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
279 return -ETIMEDOUT;
282 #ifdef PEGASUS_WRITE_EEPROM
283 static inline void enable_eprom_write(pegasus_t *pegasus)
285 __u8 tmp;
287 get_registers(pegasus, EthCtrl2, 1, &tmp);
288 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
291 static inline void disable_eprom_write(pegasus_t *pegasus)
293 __u8 tmp;
295 get_registers(pegasus, EthCtrl2, 1, &tmp);
296 set_register(pegasus, EpromCtrl, 0);
297 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
300 static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
302 int i;
303 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
304 int ret;
305 __le16 le_data = cpu_to_le16(data);
307 set_registers(pegasus, EpromOffset, 4, d);
308 enable_eprom_write(pegasus);
309 set_register(pegasus, EpromOffset, index);
310 set_registers(pegasus, EpromData, 2, &le_data);
311 set_register(pegasus, EpromCtrl, EPROM_WRITE);
313 for (i = 0; i < REG_TIMEOUT; i++) {
314 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
315 if (ret == -ESHUTDOWN)
316 goto fail;
317 if (tmp & EPROM_DONE)
318 break;
320 disable_eprom_write(pegasus);
321 if (i >= REG_TIMEOUT)
322 goto fail;
324 return ret;
326 fail:
327 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
328 return -ETIMEDOUT;
330 #endif /* PEGASUS_WRITE_EEPROM */
332 static inline int get_node_id(pegasus_t *pegasus, u8 *id)
334 int i, ret;
335 u16 w16;
337 for (i = 0; i < 3; i++) {
338 ret = read_eprom_word(pegasus, i, &w16);
339 if (ret < 0)
340 return ret;
341 ((__le16 *) id)[i] = cpu_to_le16(w16);
344 return 0;
347 static void set_ethernet_addr(pegasus_t *pegasus)
349 int ret;
350 u8 node_id[6];
352 if (pegasus->features & PEGASUS_II) {
353 ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
354 if (ret < 0)
355 goto err;
356 } else {
357 ret = get_node_id(pegasus, node_id);
358 if (ret < 0)
359 goto err;
360 ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
361 if (ret < 0)
362 goto err;
365 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
367 return;
368 err:
369 eth_hw_addr_random(pegasus->net);
370 dev_info(&pegasus->intf->dev, "software assigned MAC address.\n");
372 return;
375 static inline int reset_mac(pegasus_t *pegasus)
377 __u8 data = 0x8;
378 int i;
380 set_register(pegasus, EthCtrl1, data);
381 for (i = 0; i < REG_TIMEOUT; i++) {
382 get_registers(pegasus, EthCtrl1, 1, &data);
383 if (~data & 0x08) {
384 if (loopback)
385 break;
386 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
387 set_register(pegasus, Gpio1, 0x34);
388 else
389 set_register(pegasus, Gpio1, 0x26);
390 set_register(pegasus, Gpio0, pegasus->features);
391 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
392 break;
395 if (i == REG_TIMEOUT)
396 return -ETIMEDOUT;
398 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
399 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
400 set_register(pegasus, Gpio0, 0x24);
401 set_register(pegasus, Gpio0, 0x26);
403 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
404 __u16 auxmode;
405 read_mii_word(pegasus, 3, 0x1b, &auxmode);
406 auxmode |= 4;
407 write_mii_word(pegasus, 3, 0x1b, &auxmode);
410 return 0;
413 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
415 __u16 linkpart;
416 __u8 data[4];
417 pegasus_t *pegasus = netdev_priv(dev);
418 int ret;
420 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
421 data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
422 data[1] = 0;
423 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
424 data[1] |= 0x20; /* set full duplex */
425 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
426 data[1] |= 0x10; /* set 100 Mbps */
427 if (mii_mode)
428 data[1] = 0;
429 data[2] = loopback ? 0x09 : 0x01;
431 memcpy(pegasus->eth_regs, data, sizeof(data));
432 ret = set_registers(pegasus, EthCtrl0, 3, data);
434 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
435 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
436 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
437 u16 auxmode;
438 read_mii_word(pegasus, 0, 0x1b, &auxmode);
439 auxmode |= 4;
440 write_mii_word(pegasus, 0, 0x1b, &auxmode);
443 return ret;
446 static void read_bulk_callback(struct urb *urb)
448 pegasus_t *pegasus = urb->context;
449 struct net_device *net;
450 int rx_status, count = urb->actual_length;
451 int status = urb->status;
452 u8 *buf = urb->transfer_buffer;
453 __u16 pkt_len;
455 if (!pegasus)
456 return;
458 net = pegasus->net;
459 if (!netif_device_present(net) || !netif_running(net))
460 return;
462 switch (status) {
463 case 0:
464 break;
465 case -ETIME:
466 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
467 pegasus->flags &= ~PEGASUS_RX_BUSY;
468 break;
469 case -EPIPE: /* stall, or disconnect from TT */
470 /* FIXME schedule work to clear the halt */
471 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
472 return;
473 case -ENOENT:
474 case -ECONNRESET:
475 case -ESHUTDOWN:
476 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
477 return;
478 default:
479 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
480 goto goon;
483 if (count < 4)
484 goto goon;
486 rx_status = buf[count - 2];
487 if (rx_status & 0x1e) {
488 netif_dbg(pegasus, rx_err, net,
489 "RX packet error %x\n", rx_status);
490 net->stats.rx_errors++;
491 if (rx_status & 0x06) /* long or runt */
492 net->stats.rx_length_errors++;
493 if (rx_status & 0x08)
494 net->stats.rx_crc_errors++;
495 if (rx_status & 0x10) /* extra bits */
496 net->stats.rx_frame_errors++;
497 goto goon;
499 if (pegasus->chip == 0x8513) {
500 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
501 pkt_len &= 0x0fff;
502 pegasus->rx_skb->data += 2;
503 } else {
504 pkt_len = buf[count - 3] << 8;
505 pkt_len += buf[count - 4];
506 pkt_len &= 0xfff;
507 pkt_len -= 4;
511 * If the packet is unreasonably long, quietly drop it rather than
512 * kernel panicing by calling skb_put.
514 if (pkt_len > PEGASUS_MTU)
515 goto goon;
518 * at this point we are sure pegasus->rx_skb != NULL
519 * so we go ahead and pass up the packet.
521 skb_put(pegasus->rx_skb, pkt_len);
522 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
523 netif_rx(pegasus->rx_skb);
524 net->stats.rx_packets++;
525 net->stats.rx_bytes += pkt_len;
527 if (pegasus->flags & PEGASUS_UNPLUG)
528 return;
530 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
531 GFP_ATOMIC);
533 if (pegasus->rx_skb == NULL)
534 goto tl_sched;
535 goon:
536 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
537 usb_rcvbulkpipe(pegasus->usb, 1),
538 pegasus->rx_skb->data, PEGASUS_MTU,
539 read_bulk_callback, pegasus);
540 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
541 if (rx_status == -ENODEV)
542 netif_device_detach(pegasus->net);
543 else if (rx_status) {
544 pegasus->flags |= PEGASUS_RX_URB_FAIL;
545 goto tl_sched;
546 } else {
547 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
550 return;
552 tl_sched:
553 tasklet_schedule(&pegasus->rx_tl);
556 static void rx_fixup(unsigned long data)
558 pegasus_t *pegasus;
559 int status;
561 pegasus = (pegasus_t *) data;
562 if (pegasus->flags & PEGASUS_UNPLUG)
563 return;
565 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
566 if (pegasus->rx_skb)
567 goto try_again;
568 if (pegasus->rx_skb == NULL)
569 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
570 PEGASUS_MTU,
571 GFP_ATOMIC);
572 if (pegasus->rx_skb == NULL) {
573 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
574 tasklet_schedule(&pegasus->rx_tl);
575 return;
577 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
578 usb_rcvbulkpipe(pegasus->usb, 1),
579 pegasus->rx_skb->data, PEGASUS_MTU,
580 read_bulk_callback, pegasus);
581 try_again:
582 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
583 if (status == -ENODEV)
584 netif_device_detach(pegasus->net);
585 else if (status) {
586 pegasus->flags |= PEGASUS_RX_URB_FAIL;
587 tasklet_schedule(&pegasus->rx_tl);
588 } else {
589 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
593 static void write_bulk_callback(struct urb *urb)
595 pegasus_t *pegasus = urb->context;
596 struct net_device *net;
597 int status = urb->status;
599 if (!pegasus)
600 return;
602 net = pegasus->net;
604 if (!netif_device_present(net) || !netif_running(net))
605 return;
607 switch (status) {
608 case -EPIPE:
609 /* FIXME schedule_work() to clear the tx halt */
610 netif_stop_queue(net);
611 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
612 return;
613 case -ENOENT:
614 case -ECONNRESET:
615 case -ESHUTDOWN:
616 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
617 return;
618 default:
619 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
620 fallthrough;
621 case 0:
622 break;
625 netif_trans_update(net); /* prevent tx timeout */
626 netif_wake_queue(net);
629 static void intr_callback(struct urb *urb)
631 pegasus_t *pegasus = urb->context;
632 struct net_device *net;
633 int res, status = urb->status;
635 if (!pegasus)
636 return;
637 net = pegasus->net;
639 switch (status) {
640 case 0:
641 break;
642 case -ECONNRESET: /* unlink */
643 case -ENOENT:
644 case -ESHUTDOWN:
645 return;
646 default:
647 /* some Pegasus-I products report LOTS of data
648 * toggle errors... avoid log spamming
650 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
653 if (urb->actual_length >= 6) {
654 u8 *d = urb->transfer_buffer;
656 /* byte 0 == tx_status1, reg 2B */
657 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
658 |LATE_COL|JABBER_TIMEOUT)) {
659 net->stats.tx_errors++;
660 if (d[0] & TX_UNDERRUN)
661 net->stats.tx_fifo_errors++;
662 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
663 net->stats.tx_aborted_errors++;
664 if (d[0] & LATE_COL)
665 net->stats.tx_window_errors++;
668 /* d[5].LINK_STATUS lies on some adapters.
669 * d[0].NO_CARRIER kicks in only with failed TX.
670 * ... so monitoring with MII may be safest.
673 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
674 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
677 res = usb_submit_urb(urb, GFP_ATOMIC);
678 if (res == -ENODEV)
679 netif_device_detach(pegasus->net);
680 if (res)
681 netif_err(pegasus, timer, net,
682 "can't resubmit interrupt urb, %d\n", res);
685 static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
687 pegasus_t *pegasus = netdev_priv(net);
688 netif_warn(pegasus, timer, net, "tx timeout\n");
689 usb_unlink_urb(pegasus->tx_urb);
690 net->stats.tx_errors++;
693 static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
694 struct net_device *net)
696 pegasus_t *pegasus = netdev_priv(net);
697 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
698 int res;
699 __u16 l16 = skb->len;
701 netif_stop_queue(net);
703 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
704 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
705 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
706 usb_sndbulkpipe(pegasus->usb, 2),
707 pegasus->tx_buff, count,
708 write_bulk_callback, pegasus);
709 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
710 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
711 switch (res) {
712 case -EPIPE: /* stall, or disconnect from TT */
713 /* cleanup should already have been scheduled */
714 break;
715 case -ENODEV: /* disconnect() upcoming */
716 case -EPERM:
717 netif_device_detach(pegasus->net);
718 break;
719 default:
720 net->stats.tx_errors++;
721 netif_start_queue(net);
723 } else {
724 net->stats.tx_packets++;
725 net->stats.tx_bytes += skb->len;
727 dev_kfree_skb(skb);
729 return NETDEV_TX_OK;
732 static inline void disable_net_traffic(pegasus_t *pegasus)
734 __le16 tmp = cpu_to_le16(0);
736 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
739 static inline void get_interrupt_interval(pegasus_t *pegasus)
741 u16 data;
742 u8 interval;
744 read_eprom_word(pegasus, 4, &data);
745 interval = data >> 8;
746 if (pegasus->usb->speed != USB_SPEED_HIGH) {
747 if (interval < 0x80) {
748 netif_info(pegasus, timer, pegasus->net,
749 "intr interval changed from %ums to %ums\n",
750 interval, 0x80);
751 interval = 0x80;
752 data = (data & 0x00FF) | ((u16)interval << 8);
753 #ifdef PEGASUS_WRITE_EEPROM
754 write_eprom_word(pegasus, 4, data);
755 #endif
758 pegasus->intr_interval = interval;
761 static void set_carrier(struct net_device *net)
763 pegasus_t *pegasus = netdev_priv(net);
764 u16 tmp;
766 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
767 return;
769 if (tmp & BMSR_LSTATUS)
770 netif_carrier_on(net);
771 else
772 netif_carrier_off(net);
775 static void free_all_urbs(pegasus_t *pegasus)
777 usb_free_urb(pegasus->intr_urb);
778 usb_free_urb(pegasus->tx_urb);
779 usb_free_urb(pegasus->rx_urb);
782 static void unlink_all_urbs(pegasus_t *pegasus)
784 usb_kill_urb(pegasus->intr_urb);
785 usb_kill_urb(pegasus->tx_urb);
786 usb_kill_urb(pegasus->rx_urb);
789 static int alloc_urbs(pegasus_t *pegasus)
791 int res = -ENOMEM;
793 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
794 if (!pegasus->rx_urb) {
795 return res;
797 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
798 if (!pegasus->tx_urb) {
799 usb_free_urb(pegasus->rx_urb);
800 return res;
802 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
803 if (!pegasus->intr_urb) {
804 usb_free_urb(pegasus->tx_urb);
805 usb_free_urb(pegasus->rx_urb);
806 return res;
809 return 0;
812 static int pegasus_open(struct net_device *net)
814 pegasus_t *pegasus = netdev_priv(net);
815 int res=-ENOMEM;
817 if (pegasus->rx_skb == NULL)
818 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
819 PEGASUS_MTU,
820 GFP_KERNEL);
821 if (!pegasus->rx_skb)
822 goto exit;
824 res = set_registers(pegasus, EthID, 6, net->dev_addr);
826 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
827 usb_rcvbulkpipe(pegasus->usb, 1),
828 pegasus->rx_skb->data, PEGASUS_MTU,
829 read_bulk_callback, pegasus);
830 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
831 if (res == -ENODEV)
832 netif_device_detach(pegasus->net);
833 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
834 goto exit;
837 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
838 usb_rcvintpipe(pegasus->usb, 3),
839 pegasus->intr_buff, sizeof(pegasus->intr_buff),
840 intr_callback, pegasus, pegasus->intr_interval);
841 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
842 if (res == -ENODEV)
843 netif_device_detach(pegasus->net);
844 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
845 usb_kill_urb(pegasus->rx_urb);
846 goto exit;
848 res = enable_net_traffic(net, pegasus->usb);
849 if (res < 0) {
850 netif_dbg(pegasus, ifup, net,
851 "can't enable_net_traffic() - %d\n", res);
852 res = -EIO;
853 usb_kill_urb(pegasus->rx_urb);
854 usb_kill_urb(pegasus->intr_urb);
855 goto exit;
857 set_carrier(net);
858 netif_start_queue(net);
859 netif_dbg(pegasus, ifup, net, "open\n");
860 res = 0;
861 exit:
862 return res;
865 static int pegasus_close(struct net_device *net)
867 pegasus_t *pegasus = netdev_priv(net);
869 netif_stop_queue(net);
870 if (!(pegasus->flags & PEGASUS_UNPLUG))
871 disable_net_traffic(pegasus);
872 tasklet_kill(&pegasus->rx_tl);
873 unlink_all_urbs(pegasus);
875 return 0;
878 static void pegasus_get_drvinfo(struct net_device *dev,
879 struct ethtool_drvinfo *info)
881 pegasus_t *pegasus = netdev_priv(dev);
883 strlcpy(info->driver, driver_name, sizeof(info->driver));
884 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
885 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
888 /* also handles three patterns of some kind in hardware */
889 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
891 static void
892 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
894 pegasus_t *pegasus = netdev_priv(dev);
896 wol->supported = WAKE_MAGIC | WAKE_PHY;
897 wol->wolopts = pegasus->wolopts;
900 static int
901 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
903 pegasus_t *pegasus = netdev_priv(dev);
904 u8 reg78 = 0x04;
905 int ret;
907 if (wol->wolopts & ~WOL_SUPPORTED)
908 return -EINVAL;
910 if (wol->wolopts & WAKE_MAGIC)
911 reg78 |= 0x80;
912 if (wol->wolopts & WAKE_PHY)
913 reg78 |= 0x40;
914 /* FIXME this 0x10 bit still needs to get set in the chip... */
915 if (wol->wolopts)
916 pegasus->eth_regs[0] |= 0x10;
917 else
918 pegasus->eth_regs[0] &= ~0x10;
919 pegasus->wolopts = wol->wolopts;
921 ret = set_register(pegasus, WakeupControl, reg78);
922 if (!ret)
923 ret = device_set_wakeup_enable(&pegasus->usb->dev,
924 wol->wolopts);
925 return ret;
928 static inline void pegasus_reset_wol(struct net_device *dev)
930 struct ethtool_wolinfo wol;
932 memset(&wol, 0, sizeof wol);
933 (void) pegasus_set_wol(dev, &wol);
936 static int
937 pegasus_get_link_ksettings(struct net_device *dev,
938 struct ethtool_link_ksettings *ecmd)
940 pegasus_t *pegasus;
942 pegasus = netdev_priv(dev);
943 mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
944 return 0;
947 static int
948 pegasus_set_link_ksettings(struct net_device *dev,
949 const struct ethtool_link_ksettings *ecmd)
951 pegasus_t *pegasus = netdev_priv(dev);
952 return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
955 static int pegasus_nway_reset(struct net_device *dev)
957 pegasus_t *pegasus = netdev_priv(dev);
958 return mii_nway_restart(&pegasus->mii);
961 static u32 pegasus_get_link(struct net_device *dev)
963 pegasus_t *pegasus = netdev_priv(dev);
964 return mii_link_ok(&pegasus->mii);
967 static u32 pegasus_get_msglevel(struct net_device *dev)
969 pegasus_t *pegasus = netdev_priv(dev);
970 return pegasus->msg_enable;
973 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
975 pegasus_t *pegasus = netdev_priv(dev);
976 pegasus->msg_enable = v;
979 static const struct ethtool_ops ops = {
980 .get_drvinfo = pegasus_get_drvinfo,
981 .nway_reset = pegasus_nway_reset,
982 .get_link = pegasus_get_link,
983 .get_msglevel = pegasus_get_msglevel,
984 .set_msglevel = pegasus_set_msglevel,
985 .get_wol = pegasus_get_wol,
986 .set_wol = pegasus_set_wol,
987 .get_link_ksettings = pegasus_get_link_ksettings,
988 .set_link_ksettings = pegasus_set_link_ksettings,
991 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
993 __u16 *data = (__u16 *) &rq->ifr_ifru;
994 pegasus_t *pegasus = netdev_priv(net);
995 int res;
997 switch (cmd) {
998 case SIOCDEVPRIVATE:
999 data[0] = pegasus->phy;
1000 fallthrough;
1001 case SIOCDEVPRIVATE + 1:
1002 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1003 res = 0;
1004 break;
1005 case SIOCDEVPRIVATE + 2:
1006 if (!capable(CAP_NET_ADMIN))
1007 return -EPERM;
1008 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
1009 res = 0;
1010 break;
1011 default:
1012 res = -EOPNOTSUPP;
1014 return res;
1017 static void pegasus_set_multicast(struct net_device *net)
1019 pegasus_t *pegasus = netdev_priv(net);
1021 if (net->flags & IFF_PROMISC) {
1022 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1023 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1024 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1025 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1026 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1027 netif_dbg(pegasus, link, net, "set allmulti\n");
1028 } else {
1029 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1030 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1032 update_eth_regs_async(pegasus);
1035 static __u8 mii_phy_probe(pegasus_t *pegasus)
1037 int i;
1038 __u16 tmp;
1040 for (i = 0; i < 32; i++) {
1041 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1042 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1043 continue;
1044 else
1045 return i;
1048 return 0xff;
1051 static inline void setup_pegasus_II(pegasus_t *pegasus)
1053 __u8 data = 0xa5;
1055 set_register(pegasus, Reg1d, 0);
1056 set_register(pegasus, Reg7b, 1);
1057 msleep(100);
1058 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1059 set_register(pegasus, Reg7b, 0);
1060 else
1061 set_register(pegasus, Reg7b, 2);
1063 set_register(pegasus, 0x83, data);
1064 get_registers(pegasus, 0x83, 1, &data);
1066 if (data == 0xa5)
1067 pegasus->chip = 0x8513;
1068 else
1069 pegasus->chip = 0;
1071 set_register(pegasus, 0x80, 0xc0);
1072 set_register(pegasus, 0x83, 0xff);
1073 set_register(pegasus, 0x84, 0x01);
1075 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1076 set_register(pegasus, Reg81, 6);
1077 else
1078 set_register(pegasus, Reg81, 2);
1081 static void check_carrier(struct work_struct *work)
1083 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1084 set_carrier(pegasus->net);
1085 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1086 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1087 CARRIER_CHECK_DELAY);
1091 static int pegasus_blacklisted(struct usb_device *udev)
1093 struct usb_device_descriptor *udd = &udev->descriptor;
1095 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1096 * dongle which happens to have the same ID.
1098 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1099 (udd->idProduct == cpu_to_le16(0x0121)) &&
1100 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1101 (udd->bDeviceProtocol == 1))
1102 return 1;
1104 return 0;
1107 static int pegasus_probe(struct usb_interface *intf,
1108 const struct usb_device_id *id)
1110 struct usb_device *dev = interface_to_usbdev(intf);
1111 struct net_device *net;
1112 pegasus_t *pegasus;
1113 int dev_index = id - pegasus_ids;
1114 int res = -ENOMEM;
1116 if (pegasus_blacklisted(dev))
1117 return -ENODEV;
1119 net = alloc_etherdev(sizeof(struct pegasus));
1120 if (!net)
1121 goto out;
1123 pegasus = netdev_priv(net);
1124 pegasus->dev_index = dev_index;
1126 res = alloc_urbs(pegasus);
1127 if (res < 0) {
1128 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1129 goto out1;
1132 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1134 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1136 pegasus->intf = intf;
1137 pegasus->usb = dev;
1138 pegasus->net = net;
1141 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1142 net->netdev_ops = &pegasus_netdev_ops;
1143 net->ethtool_ops = &ops;
1144 pegasus->mii.dev = net;
1145 pegasus->mii.mdio_read = mdio_read;
1146 pegasus->mii.mdio_write = mdio_write;
1147 pegasus->mii.phy_id_mask = 0x1f;
1148 pegasus->mii.reg_num_mask = 0x1f;
1149 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
1150 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1152 pegasus->features = usb_dev_id[dev_index].private;
1153 get_interrupt_interval(pegasus);
1154 if (reset_mac(pegasus)) {
1155 dev_err(&intf->dev, "can't reset MAC\n");
1156 res = -EIO;
1157 goto out2;
1159 set_ethernet_addr(pegasus);
1160 if (pegasus->features & PEGASUS_II) {
1161 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1162 setup_pegasus_II(pegasus);
1164 pegasus->phy = mii_phy_probe(pegasus);
1165 if (pegasus->phy == 0xff) {
1166 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1167 pegasus->phy = 1;
1169 pegasus->mii.phy_id = pegasus->phy;
1170 usb_set_intfdata(intf, pegasus);
1171 SET_NETDEV_DEV(net, &intf->dev);
1172 pegasus_reset_wol(net);
1173 res = register_netdev(net);
1174 if (res)
1175 goto out3;
1176 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1177 CARRIER_CHECK_DELAY);
1178 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1179 usb_dev_id[dev_index].name, net->dev_addr);
1180 return 0;
1182 out3:
1183 usb_set_intfdata(intf, NULL);
1184 out2:
1185 free_all_urbs(pegasus);
1186 out1:
1187 free_netdev(net);
1188 out:
1189 return res;
1192 static void pegasus_disconnect(struct usb_interface *intf)
1194 struct pegasus *pegasus = usb_get_intfdata(intf);
1196 usb_set_intfdata(intf, NULL);
1197 if (!pegasus) {
1198 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1199 return;
1202 pegasus->flags |= PEGASUS_UNPLUG;
1203 cancel_delayed_work_sync(&pegasus->carrier_check);
1204 unregister_netdev(pegasus->net);
1205 unlink_all_urbs(pegasus);
1206 free_all_urbs(pegasus);
1207 if (pegasus->rx_skb != NULL) {
1208 dev_kfree_skb(pegasus->rx_skb);
1209 pegasus->rx_skb = NULL;
1211 free_netdev(pegasus->net);
1214 static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
1216 struct pegasus *pegasus = usb_get_intfdata(intf);
1218 netif_device_detach(pegasus->net);
1219 cancel_delayed_work_sync(&pegasus->carrier_check);
1220 if (netif_running(pegasus->net)) {
1221 usb_kill_urb(pegasus->rx_urb);
1222 usb_kill_urb(pegasus->intr_urb);
1224 return 0;
1227 static int pegasus_resume(struct usb_interface *intf)
1229 struct pegasus *pegasus = usb_get_intfdata(intf);
1231 netif_device_attach(pegasus->net);
1232 if (netif_running(pegasus->net)) {
1233 pegasus->rx_urb->status = 0;
1234 pegasus->rx_urb->actual_length = 0;
1235 read_bulk_callback(pegasus->rx_urb);
1237 pegasus->intr_urb->status = 0;
1238 pegasus->intr_urb->actual_length = 0;
1239 intr_callback(pegasus->intr_urb);
1241 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1242 CARRIER_CHECK_DELAY);
1243 return 0;
1246 static const struct net_device_ops pegasus_netdev_ops = {
1247 .ndo_open = pegasus_open,
1248 .ndo_stop = pegasus_close,
1249 .ndo_do_ioctl = pegasus_ioctl,
1250 .ndo_start_xmit = pegasus_start_xmit,
1251 .ndo_set_rx_mode = pegasus_set_multicast,
1252 .ndo_tx_timeout = pegasus_tx_timeout,
1253 .ndo_set_mac_address = eth_mac_addr,
1254 .ndo_validate_addr = eth_validate_addr,
1257 static struct usb_driver pegasus_driver = {
1258 .name = driver_name,
1259 .probe = pegasus_probe,
1260 .disconnect = pegasus_disconnect,
1261 .id_table = pegasus_ids,
1262 .suspend = pegasus_suspend,
1263 .resume = pegasus_resume,
1264 .disable_hub_initiated_lpm = 1,
1267 static void __init parse_id(char *id)
1269 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1270 char *token, *name = NULL;
1272 if ((token = strsep(&id, ":")) != NULL)
1273 name = token;
1274 /* name now points to a null terminated string*/
1275 if ((token = strsep(&id, ":")) != NULL)
1276 vendor_id = simple_strtoul(token, NULL, 16);
1277 if ((token = strsep(&id, ":")) != NULL)
1278 device_id = simple_strtoul(token, NULL, 16);
1279 flags = simple_strtoul(id, NULL, 16);
1280 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1281 driver_name, name, vendor_id, device_id, flags);
1283 if (vendor_id > 0x10000 || vendor_id == 0)
1284 return;
1285 if (device_id > 0x10000 || device_id == 0)
1286 return;
1288 for (i = 0; usb_dev_id[i].name; i++);
1289 usb_dev_id[i].name = name;
1290 usb_dev_id[i].vendor = vendor_id;
1291 usb_dev_id[i].device = device_id;
1292 usb_dev_id[i].private = flags;
1293 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1294 pegasus_ids[i].idVendor = vendor_id;
1295 pegasus_ids[i].idProduct = device_id;
1298 static int __init pegasus_init(void)
1300 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1301 if (devid)
1302 parse_id(devid);
1303 return usb_register(&pegasus_driver);
1306 static void __exit pegasus_exit(void)
1308 usb_deregister(&pegasus_driver);
1311 module_init(pegasus_init);
1312 module_exit(pegasus_exit);