Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 3 | * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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() |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 21 | * time as this seems to break dhcpd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 25 | * is out of the interrupt routine. |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 26 | * ... |
| 27 | * v0.9.3 simplified [get|set]_register(s), async update registers |
| 28 | * logic revisited, receive skb_pool removed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "pegasus.h" |
| 44 | |
| 45 | /* |
| 46 | * Version Information |
| 47 | */ |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 48 | #define DRIVER_VERSION "v0.9.3 (2013/04/25)" |
| 49 | #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver" |
| 51 | |
| 52 | static const char driver_name[] = "pegasus"; |
| 53 | |
| 54 | #undef PEGASUS_WRITE_EEPROM |
| 55 | #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \ |
| 56 | BMSR_100FULL | BMSR_ANEGCAPABLE) |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 57 | #define CARRIER_CHECK_DELAY (2 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 59 | static bool loopback; |
| 60 | static bool mii_mode; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 61 | static char *devid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 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}, |
Chris Rankin | ab854b2 | 2009-10-13 00:32:02 -0700 | [diff] [blame] | 66 | #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \ |
| 67 | PEGASUS_DEV(pn, vid, pid, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include "pegasus.h" |
| 69 | #undef PEGASUS_DEV |
Chris Rankin | ab854b2 | 2009-10-13 00:32:02 -0700 | [diff] [blame] | 70 | #undef PEGASUS_DEV_CLASS |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 71 | {NULL, 0, 0, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | {NULL, 0, 0, 0} |
| 73 | }; |
| 74 | |
| 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}, |
Chris Rankin | ab854b2 | 2009-10-13 00:32:02 -0700 | [diff] [blame] | 78 | /* |
| 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. |
| 83 | */ |
| 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}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #include "pegasus.h" |
| 88 | #undef PEGASUS_DEV |
Chris Rankin | ab854b2 | 2009-10-13 00:32:02 -0700 | [diff] [blame] | 89 | #undef PEGASUS_DEV_CLASS |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 90 | {}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | {} |
| 92 | }; |
| 93 | |
| 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); |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 99 | module_param(devid, charp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 102 | MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* use ethtool to change the level for any given device */ |
| 105 | static int msg_level = -1; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 106 | module_param(msg_level, int, 0); |
| 107 | MODULE_PARM_DESC(msg_level, "Override default message level"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | MODULE_DEVICE_TABLE(usb, pegasus_ids); |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 110 | static const struct net_device_ops pegasus_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 112 | /*****/ |
| 113 | |
| 114 | static void async_ctrl_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 116 | struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 117 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 119 | if (status < 0) |
| 120 | dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status); |
| 121 | kfree(req); |
| 122 | usb_free_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 125 | static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 127 | u8 *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 130 | buf = kmalloc(size, GFP_NOIO); |
| 131 | if (!buf) |
| 132 | return -ENOMEM; |
| 133 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 134 | ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0), |
| 135 | PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0, |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 136 | indx, buf, size, 1000); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 137 | if (ret < 0) |
| 138 | netif_dbg(pegasus, drv, pegasus->net, |
| 139 | "%s returned %d\n", __func__, ret); |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 140 | else if (ret <= size) |
| 141 | memcpy(data, buf, ret); |
| 142 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 146 | static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size, |
| 147 | const void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 149 | u8 *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 152 | buf = kmemdup(data, size, GFP_NOIO); |
| 153 | if (!buf) |
| 154 | return -ENOMEM; |
| 155 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 156 | ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0), |
| 157 | PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0, |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 158 | indx, buf, size, 100); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | netif_dbg(pegasus, drv, pegasus->net, |
| 161 | "%s returned %d\n", __func__, ret); |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 162 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return ret; |
| 164 | } |
| 165 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 166 | static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 168 | u8 *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 171 | buf = kmemdup(&data, 1, GFP_NOIO); |
| 172 | if (!buf) |
| 173 | return -ENOMEM; |
| 174 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 175 | ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0), |
| 176 | PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data, |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 177 | indx, buf, 1, 1000); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 178 | if (ret < 0) |
| 179 | netif_dbg(pegasus, drv, pegasus->net, |
| 180 | "%s returned %d\n", __func__, ret); |
Ben Hutchings | 5593523 | 2017-02-04 16:56:03 +0000 | [diff] [blame] | 181 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return ret; |
| 183 | } |
| 184 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 185 | static int update_eth_regs_async(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 187 | int ret = -ENOMEM; |
| 188 | struct urb *async_urb; |
| 189 | struct usb_ctrlrequest *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 191 | req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); |
| 192 | if (req == NULL) |
| 193 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 195 | async_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 196 | if (async_urb == NULL) { |
| 197 | kfree(req); |
| 198 | return ret; |
| 199 | } |
| 200 | req->bRequestType = PEGASUS_REQT_WRITE; |
| 201 | req->bRequest = PEGASUS_REQ_SET_REGS; |
| 202 | req->wValue = cpu_to_le16(0); |
| 203 | req->wIndex = cpu_to_le16(EthCtrl0); |
| 204 | req->wLength = cpu_to_le16(3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 206 | usb_fill_control_urb(async_urb, pegasus->usb, |
| 207 | usb_sndctrlpipe(pegasus->usb, 0), (void *)req, |
| 208 | pegasus->eth_regs, 3, async_ctrl_callback, req); |
| 209 | |
| 210 | ret = usb_submit_urb(async_urb, GFP_ATOMIC); |
| 211 | if (ret) { |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 212 | if (ret == -ENODEV) |
| 213 | netif_device_detach(pegasus->net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 214 | netif_err(pegasus, drv, pegasus->net, |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 215 | "%s returned %d\n", __func__, ret); |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 220 | static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | int i; |
| 223 | __u8 data[4] = { phy, 0, 0, indx }; |
| 224 | __le16 regdi; |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 225 | int ret = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 227 | if (cmd & PHY_WRITE) { |
| 228 | __le16 *t = (__le16 *) & data[1]; |
| 229 | *t = cpu_to_le16(*regd); |
| 230 | } |
| 231 | set_register(p, PhyCtrl, 0); |
| 232 | set_registers(p, PhyAddr, sizeof(data), data); |
| 233 | set_register(p, PhyCtrl, (indx | cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | for (i = 0; i < REG_TIMEOUT; i++) { |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 235 | ret = get_registers(p, PhyCtrl, 1, data); |
| 236 | if (ret < 0) |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 237 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (data[0] & PHY_DONE) |
| 239 | break; |
| 240 | } |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 241 | if (i >= REG_TIMEOUT) |
| 242 | goto fail; |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 243 | if (cmd & PHY_READ) { |
| 244 | ret = get_registers(p, PhyData, 2, ®di); |
| 245 | *regd = le16_to_cpu(regdi); |
| 246 | return ret; |
| 247 | } |
| 248 | return 0; |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 249 | fail: |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 250 | netif_dbg(p, drv, p->net, "%s failed\n", __func__); |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 251 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 254 | /* Returns non-negative int on success, error on failure */ |
| 255 | static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd) |
| 256 | { |
| 257 | return __mii_op(pegasus, phy, indx, regd, PHY_READ); |
| 258 | } |
| 259 | |
| 260 | /* Returns zero on success, error on failure */ |
| 261 | static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd) |
| 262 | { |
| 263 | return __mii_op(pegasus, phy, indx, regd, PHY_WRITE); |
| 264 | } |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | static int mdio_read(struct net_device *dev, int phy_id, int loc) |
| 267 | { |
Joe Perches | 8739cfe | 2010-11-15 11:12:29 +0000 | [diff] [blame] | 268 | pegasus_t *pegasus = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | u16 res; |
| 270 | |
| 271 | read_mii_word(pegasus, phy_id, loc, &res); |
| 272 | return (int)res; |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | static void mdio_write(struct net_device *dev, int phy_id, int loc, int val) |
| 276 | { |
Joe Perches | 8739cfe | 2010-11-15 11:12:29 +0000 | [diff] [blame] | 277 | pegasus_t *pegasus = netdev_priv(dev); |
Dan Carpenter | 3d64fc7 | 2013-05-02 20:44:20 +0000 | [diff] [blame] | 278 | u16 data = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Dan Carpenter | 3d64fc7 | 2013-05-02 20:44:20 +0000 | [diff] [blame] | 280 | write_mii_word(pegasus, phy_id, loc, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 283 | static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | int i; |
Denis Kirjanov | 224c049 | 2019-07-30 15:13:57 +0200 | [diff] [blame] | 286 | __u8 tmp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | __le16 retdatai; |
| 288 | int ret; |
| 289 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 290 | set_register(pegasus, EpromCtrl, 0); |
| 291 | set_register(pegasus, EpromOffset, index); |
| 292 | set_register(pegasus, EpromCtrl, EPROM_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | for (i = 0; i < REG_TIMEOUT; i++) { |
| 295 | ret = get_registers(pegasus, EpromCtrl, 1, &tmp); |
| 296 | if (tmp & EPROM_DONE) |
| 297 | break; |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 298 | if (ret == -ESHUTDOWN) |
| 299 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 301 | if (i >= REG_TIMEOUT) |
| 302 | goto fail; |
| 303 | |
| 304 | ret = get_registers(pegasus, EpromData, 2, &retdatai); |
| 305 | *retdata = le16_to_cpu(retdatai); |
| 306 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 308 | fail: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 309 | netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__); |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 310 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | #ifdef PEGASUS_WRITE_EEPROM |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 314 | static inline void enable_eprom_write(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | __u8 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 318 | get_registers(pegasus, EthCtrl2, 1, &tmp); |
| 319 | set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 322 | static inline void disable_eprom_write(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | __u8 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 326 | get_registers(pegasus, EthCtrl2, 1, &tmp); |
| 327 | set_register(pegasus, EpromCtrl, 0); |
| 328 | set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 331 | static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | int i; |
| 334 | __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE }; |
| 335 | int ret; |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 336 | __le16 le_data = cpu_to_le16(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 338 | set_registers(pegasus, EpromOffset, 4, d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | enable_eprom_write(pegasus); |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 340 | set_register(pegasus, EpromOffset, index); |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 341 | set_registers(pegasus, EpromData, 2, &le_data); |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 342 | set_register(pegasus, EpromCtrl, EPROM_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | for (i = 0; i < REG_TIMEOUT; i++) { |
| 345 | ret = get_registers(pegasus, EpromCtrl, 1, &tmp); |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 346 | if (ret == -ESHUTDOWN) |
| 347 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (tmp & EPROM_DONE) |
| 349 | break; |
| 350 | } |
| 351 | disable_eprom_write(pegasus); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 352 | if (i >= REG_TIMEOUT) |
| 353 | goto fail; |
| 354 | |
| 355 | return ret; |
| 356 | |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 357 | fail: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 358 | netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__); |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 359 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | #endif /* PEGASUS_WRITE_EEPROM */ |
| 362 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 363 | static inline void get_node_id(pegasus_t *pegasus, __u8 *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | int i; |
| 366 | __u16 w16; |
| 367 | |
| 368 | for (i = 0; i < 3; i++) { |
| 369 | read_eprom_word(pegasus, i, &w16); |
Harvey Harrison | da2bbdc | 2008-10-29 14:25:51 -0700 | [diff] [blame] | 370 | ((__le16 *) id)[i] = cpu_to_le16(w16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 374 | static void set_ethernet_addr(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | __u8 node_id[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Petko Manolov | 37cf347 | 2006-09-27 14:25:37 -0700 | [diff] [blame] | 378 | if (pegasus->features & PEGASUS_II) { |
| 379 | get_registers(pegasus, 0x10, sizeof(node_id), node_id); |
| 380 | } else { |
| 381 | get_node_id(pegasus, node_id); |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 382 | set_registers(pegasus, EthID, sizeof(node_id), node_id); |
Petko Manolov | 37cf347 | 2006-09-27 14:25:37 -0700 | [diff] [blame] | 383 | } |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 384 | memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 387 | static inline int reset_mac(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
| 389 | __u8 data = 0x8; |
| 390 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 392 | set_register(pegasus, EthCtrl1, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | for (i = 0; i < REG_TIMEOUT; i++) { |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 394 | get_registers(pegasus, EthCtrl1, 1, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (~data & 0x08) { |
Dan Carpenter | 681f162 | 2011-12-23 00:44:36 +0000 | [diff] [blame] | 396 | if (loopback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | if (mii_mode && (pegasus->features & HAS_HOME_PNA)) |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 399 | set_register(pegasus, Gpio1, 0x34); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | else |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 401 | set_register(pegasus, Gpio1, 0x26); |
| 402 | set_register(pegasus, Gpio0, pegasus->features); |
| 403 | set_register(pegasus, Gpio0, DEFAULT_GPIO_SET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | } |
| 407 | if (i == REG_TIMEOUT) |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 408 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS || |
| 411 | usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) { |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 412 | set_register(pegasus, Gpio0, 0x24); |
| 413 | set_register(pegasus, Gpio0, 0x26); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) { |
| 416 | __u16 auxmode; |
| 417 | read_mii_word(pegasus, 3, 0x1b, &auxmode); |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 418 | auxmode |= 4; |
| 419 | write_mii_word(pegasus, 3, 0x1b, &auxmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int enable_net_traffic(struct net_device *dev, struct usb_device *usb) |
| 426 | { |
| 427 | __u16 linkpart; |
| 428 | __u8 data[4]; |
| 429 | pegasus_t *pegasus = netdev_priv(dev); |
| 430 | int ret; |
| 431 | |
| 432 | read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart); |
Petko Manolov | 1a8deec | 2016-04-27 14:24:50 +0300 | [diff] [blame] | 433 | data[0] = 0xc8; /* TX & RX enable, append status, no CRC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | data[1] = 0; |
| 435 | if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL)) |
| 436 | data[1] |= 0x20; /* set full duplex */ |
| 437 | if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF)) |
| 438 | data[1] |= 0x10; /* set 100 Mbps */ |
| 439 | if (mii_mode) |
| 440 | data[1] = 0; |
Dan Carpenter | 681f162 | 2011-12-23 00:44:36 +0000 | [diff] [blame] | 441 | data[2] = loopback ? 0x09 : 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 443 | memcpy(pegasus->eth_regs, data, sizeof(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | ret = set_registers(pegasus, EthCtrl0, 3, data); |
| 445 | |
| 446 | if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS || |
Malte Doersam | efafe6f | 2006-01-28 17:48:33 +0100 | [diff] [blame] | 447 | usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) { |
| 449 | u16 auxmode; |
| 450 | read_mii_word(pegasus, 0, 0x1b, &auxmode); |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 451 | auxmode |= 4; |
| 452 | write_mii_word(pegasus, 0, 0x1b, &auxmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 455 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 458 | static void read_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | pegasus_t *pegasus = urb->context; |
| 461 | struct net_device *net; |
| 462 | int rx_status, count = urb->actual_length; |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 463 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | u8 *buf = urb->transfer_buffer; |
| 465 | __u16 pkt_len; |
| 466 | |
| 467 | if (!pegasus) |
| 468 | return; |
| 469 | |
| 470 | net = pegasus->net; |
| 471 | if (!netif_device_present(net) || !netif_running(net)) |
| 472 | return; |
| 473 | |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 474 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | case 0: |
| 476 | break; |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 477 | case -ETIME: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 478 | netif_dbg(pegasus, rx_err, net, "reset MAC\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | pegasus->flags &= ~PEGASUS_RX_BUSY; |
| 480 | break; |
| 481 | case -EPIPE: /* stall, or disconnect from TT */ |
| 482 | /* FIXME schedule work to clear the halt */ |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 483 | netif_warn(pegasus, rx_err, net, "no rx stall recovery\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | return; |
| 485 | case -ENOENT: |
| 486 | case -ECONNRESET: |
| 487 | case -ESHUTDOWN: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 488 | netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return; |
| 490 | default: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 491 | netif_dbg(pegasus, rx_err, net, "RX status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | goto goon; |
| 493 | } |
| 494 | |
xypron.glpk@gmx.de | bbf178e | 2016-05-18 20:40:51 +0200 | [diff] [blame] | 495 | if (count < 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | goto goon; |
| 497 | |
| 498 | rx_status = buf[count - 2]; |
| 499 | if (rx_status & 0x1e) { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 500 | netif_dbg(pegasus, rx_err, net, |
| 501 | "RX packet error %x\n", rx_status); |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 502 | net->stats.rx_errors++; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 503 | if (rx_status & 0x06) /* long or runt */ |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 504 | net->stats.rx_length_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (rx_status & 0x08) |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 506 | net->stats.rx_crc_errors++; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 507 | if (rx_status & 0x10) /* extra bits */ |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 508 | net->stats.rx_frame_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | goto goon; |
| 510 | } |
| 511 | if (pegasus->chip == 0x8513) { |
| 512 | pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer); |
| 513 | pkt_len &= 0x0fff; |
| 514 | pegasus->rx_skb->data += 2; |
| 515 | } else { |
| 516 | pkt_len = buf[count - 3] << 8; |
| 517 | pkt_len += buf[count - 4]; |
| 518 | pkt_len &= 0xfff; |
Petko Manolov | 1a8deec | 2016-04-27 14:24:50 +0300 | [diff] [blame] | 519 | pkt_len -= 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 523 | * If the packet is unreasonably long, quietly drop it rather than |
| 524 | * kernel panicing by calling skb_put. |
| 525 | */ |
| 526 | if (pkt_len > PEGASUS_MTU) |
| 527 | goto goon; |
| 528 | |
| 529 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | * at this point we are sure pegasus->rx_skb != NULL |
| 531 | * so we go ahead and pass up the packet. |
| 532 | */ |
| 533 | skb_put(pegasus->rx_skb, pkt_len); |
| 534 | pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net); |
| 535 | netif_rx(pegasus->rx_skb); |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 536 | net->stats.rx_packets++; |
| 537 | net->stats.rx_bytes += pkt_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | if (pegasus->flags & PEGASUS_UNPLUG) |
| 540 | return; |
| 541 | |
Petko Manolov | 313a58e | 2013-04-25 22:41:21 +0000 | [diff] [blame] | 542 | pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU, |
| 543 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | if (pegasus->rx_skb == NULL) |
| 546 | goto tl_sched; |
| 547 | goon: |
| 548 | usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb, |
| 549 | usb_rcvbulkpipe(pegasus->usb, 1), |
Petko Manolov | b7302ca | 2016-04-27 14:24:49 +0300 | [diff] [blame] | 550 | pegasus->rx_skb->data, PEGASUS_MTU, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | read_bulk_callback, pegasus); |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 552 | rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC); |
| 553 | if (rx_status == -ENODEV) |
| 554 | netif_device_detach(pegasus->net); |
| 555 | else if (rx_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | pegasus->flags |= PEGASUS_RX_URB_FAIL; |
| 557 | goto tl_sched; |
| 558 | } else { |
| 559 | pegasus->flags &= ~PEGASUS_RX_URB_FAIL; |
| 560 | } |
| 561 | |
| 562 | return; |
| 563 | |
| 564 | tl_sched: |
| 565 | tasklet_schedule(&pegasus->rx_tl); |
| 566 | } |
| 567 | |
| 568 | static void rx_fixup(unsigned long data) |
| 569 | { |
| 570 | pegasus_t *pegasus; |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 571 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | pegasus = (pegasus_t *) data; |
| 574 | if (pegasus->flags & PEGASUS_UNPLUG) |
| 575 | return; |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (pegasus->flags & PEGASUS_RX_URB_FAIL) |
| 578 | if (pegasus->rx_skb) |
| 579 | goto try_again; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 580 | if (pegasus->rx_skb == NULL) |
Petko Manolov | 313a58e | 2013-04-25 22:41:21 +0000 | [diff] [blame] | 581 | pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, |
| 582 | PEGASUS_MTU, |
| 583 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | if (pegasus->rx_skb == NULL) { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 585 | netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | tasklet_schedule(&pegasus->rx_tl); |
Petko Manolov | 313a58e | 2013-04-25 22:41:21 +0000 | [diff] [blame] | 587 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb, |
| 590 | usb_rcvbulkpipe(pegasus->usb, 1), |
Petko Manolov | b7302ca | 2016-04-27 14:24:49 +0300 | [diff] [blame] | 591 | pegasus->rx_skb->data, PEGASUS_MTU, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | read_bulk_callback, pegasus); |
| 593 | try_again: |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 594 | status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC); |
| 595 | if (status == -ENODEV) |
| 596 | netif_device_detach(pegasus->net); |
| 597 | else if (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | pegasus->flags |= PEGASUS_RX_URB_FAIL; |
| 599 | tasklet_schedule(&pegasus->rx_tl); |
| 600 | } else { |
| 601 | pegasus->flags &= ~PEGASUS_RX_URB_FAIL; |
| 602 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 605 | static void write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | pegasus_t *pegasus = urb->context; |
Micah Gruber | 9351982 | 2007-07-23 16:05:52 +0800 | [diff] [blame] | 608 | struct net_device *net; |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 609 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | if (!pegasus) |
| 612 | return; |
| 613 | |
Micah Gruber | 9351982 | 2007-07-23 16:05:52 +0800 | [diff] [blame] | 614 | net = pegasus->net; |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (!netif_device_present(net) || !netif_running(net)) |
| 617 | return; |
| 618 | |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 619 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | case -EPIPE: |
| 621 | /* FIXME schedule_work() to clear the tx halt */ |
| 622 | netif_stop_queue(net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 623 | netif_warn(pegasus, tx_err, net, "no tx stall recovery\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | return; |
| 625 | case -ENOENT: |
| 626 | case -ECONNRESET: |
| 627 | case -ESHUTDOWN: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 628 | netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return; |
| 630 | default: |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 631 | netif_info(pegasus, tx_err, net, "TX status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* FALL THROUGH */ |
| 633 | case 0: |
| 634 | break; |
| 635 | } |
| 636 | |
Florian Westphal | 860e953 | 2016-05-03 16:33:13 +0200 | [diff] [blame] | 637 | netif_trans_update(net); /* prevent tx timeout */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | netif_wake_queue(net); |
| 639 | } |
| 640 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 641 | static void intr_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
| 643 | pegasus_t *pegasus = urb->context; |
| 644 | struct net_device *net; |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 645 | int res, status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | if (!pegasus) |
| 648 | return; |
| 649 | net = pegasus->net; |
| 650 | |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 651 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | case 0: |
| 653 | break; |
| 654 | case -ECONNRESET: /* unlink */ |
| 655 | case -ENOENT: |
| 656 | case -ESHUTDOWN: |
| 657 | return; |
| 658 | default: |
| 659 | /* some Pegasus-I products report LOTS of data |
| 660 | * toggle errors... avoid log spamming |
| 661 | */ |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 662 | netif_dbg(pegasus, timer, net, "intr status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (urb->actual_length >= 6) { |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 666 | u8 *d = urb->transfer_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | /* byte 0 == tx_status1, reg 2B */ |
| 669 | if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL |
| 670 | |LATE_COL|JABBER_TIMEOUT)) { |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 671 | net->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (d[0] & TX_UNDERRUN) |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 673 | net->stats.tx_fifo_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT)) |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 675 | net->stats.tx_aborted_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (d[0] & LATE_COL) |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 677 | net->stats.tx_window_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* d[5].LINK_STATUS lies on some adapters. |
| 681 | * d[0].NO_CARRIER kicks in only with failed TX. |
| 682 | * ... so monitoring with MII may be safest. |
| 683 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | /* bytes 3-4 == rx_lostpkt, reg 2E/2F */ |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 686 | net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 689 | res = usb_submit_urb(urb, GFP_ATOMIC); |
| 690 | if (res == -ENODEV) |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 691 | netif_device_detach(pegasus->net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 692 | if (res) |
| 693 | netif_err(pegasus, timer, net, |
| 694 | "can't resubmit interrupt urb, %d\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Michael S. Tsirkin | 0290bd2 | 2019-12-10 09:23:51 -0500 | [diff] [blame] | 697 | static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
| 699 | pegasus_t *pegasus = netdev_priv(net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 700 | netif_warn(pegasus, timer, net, "tx timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | usb_unlink_urb(pegasus->tx_urb); |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 702 | net->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 705 | static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb, |
| 706 | struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
| 708 | pegasus_t *pegasus = netdev_priv(net); |
| 709 | int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3; |
| 710 | int res; |
| 711 | __u16 l16 = skb->len; |
| 712 | |
| 713 | netif_stop_queue(net); |
| 714 | |
| 715 | ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16); |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 716 | skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb, |
| 718 | usb_sndbulkpipe(pegasus->usb, 2), |
| 719 | pegasus->tx_buff, count, |
| 720 | write_bulk_callback, pegasus); |
| 721 | if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 722 | netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | switch (res) { |
| 724 | case -EPIPE: /* stall, or disconnect from TT */ |
| 725 | /* cleanup should already have been scheduled */ |
| 726 | break; |
| 727 | case -ENODEV: /* disconnect() upcoming */ |
Oliver Neukum | 9dd014e | 2009-04-17 01:40:19 -0700 | [diff] [blame] | 728 | case -EPERM: |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 729 | netif_device_detach(pegasus->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | break; |
| 731 | default: |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 732 | net->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | netif_start_queue(net); |
| 734 | } |
| 735 | } else { |
Tobias Klauser | 82d8293 | 2017-04-07 10:17:39 +0200 | [diff] [blame] | 736 | net->stats.tx_packets++; |
| 737 | net->stats.tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | dev_kfree_skb(skb); |
| 740 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 741 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 744 | static inline void disable_net_traffic(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 746 | __le16 tmp = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 748 | set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 751 | static inline void get_interrupt_interval(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 753 | u16 data; |
| 754 | u8 interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 756 | read_eprom_word(pegasus, 4, &data); |
| 757 | interval = data >> 8; |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 758 | if (pegasus->usb->speed != USB_SPEED_HIGH) { |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 759 | if (interval < 0x80) { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 760 | netif_info(pegasus, timer, pegasus->net, |
| 761 | "intr interval changed from %ums to %ums\n", |
| 762 | interval, 0x80); |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 763 | interval = 0x80; |
| 764 | data = (data & 0x00FF) | ((u16)interval << 8); |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 765 | #ifdef PEGASUS_WRITE_EEPROM |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 766 | write_eprom_word(pegasus, 4, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | #endif |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 770 | pegasus->intr_interval = interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void set_carrier(struct net_device *net) |
| 774 | { |
| 775 | pegasus_t *pegasus = netdev_priv(net); |
| 776 | u16 tmp; |
| 777 | |
Dan Williams | c43c49b | 2007-04-24 10:20:06 -0400 | [diff] [blame] | 778 | if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return; |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (tmp & BMSR_LSTATUS) |
| 782 | netif_carrier_on(net); |
| 783 | else |
| 784 | netif_carrier_off(net); |
| 785 | } |
| 786 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 787 | static void free_all_urbs(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | usb_free_urb(pegasus->intr_urb); |
| 790 | usb_free_urb(pegasus->tx_urb); |
| 791 | usb_free_urb(pegasus->rx_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 794 | static void unlink_all_urbs(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
| 796 | usb_kill_urb(pegasus->intr_urb); |
| 797 | usb_kill_urb(pegasus->tx_urb); |
| 798 | usb_kill_urb(pegasus->rx_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 801 | static int alloc_urbs(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 803 | int res = -ENOMEM; |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 806 | if (!pegasus->rx_urb) { |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 807 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 810 | if (!pegasus->tx_urb) { |
| 811 | usb_free_urb(pegasus->rx_urb); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 812 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 815 | if (!pegasus->intr_urb) { |
| 816 | usb_free_urb(pegasus->tx_urb); |
| 817 | usb_free_urb(pegasus->rx_urb); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 818 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 821 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static int pegasus_open(struct net_device *net) |
| 825 | { |
| 826 | pegasus_t *pegasus = netdev_priv(net); |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 827 | int res=-ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | if (pegasus->rx_skb == NULL) |
Petko Manolov | 313a58e | 2013-04-25 22:41:21 +0000 | [diff] [blame] | 830 | pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, |
| 831 | PEGASUS_MTU, |
| 832 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | if (!pegasus->rx_skb) |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 834 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | res = set_registers(pegasus, EthID, 6, net->dev_addr); |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb, |
| 839 | usb_rcvbulkpipe(pegasus->usb, 1), |
Petko Manolov | b7302ca | 2016-04-27 14:24:49 +0300 | [diff] [blame] | 840 | pegasus->rx_skb->data, PEGASUS_MTU, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | read_bulk_callback, pegasus); |
| 842 | if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) { |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 843 | if (res == -ENODEV) |
| 844 | netif_device_detach(pegasus->net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 845 | netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | goto exit; |
| 847 | } |
| 848 | |
| 849 | usb_fill_int_urb(pegasus->intr_urb, pegasus->usb, |
| 850 | usb_rcvintpipe(pegasus->usb, 3), |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 851 | pegasus->intr_buff, sizeof(pegasus->intr_buff), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | intr_callback, pegasus, pegasus->intr_interval); |
| 853 | if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) { |
David Brownell | 955a260 | 2006-05-26 10:17:03 -0700 | [diff] [blame] | 854 | if (res == -ENODEV) |
| 855 | netif_device_detach(pegasus->net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 856 | netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | usb_kill_urb(pegasus->rx_urb); |
| 858 | goto exit; |
| 859 | } |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 860 | res = enable_net_traffic(net, pegasus->usb); |
| 861 | if (res < 0) { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 862 | netif_dbg(pegasus, ifup, net, |
| 863 | "can't enable_net_traffic() - %d\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | res = -EIO; |
| 865 | usb_kill_urb(pegasus->rx_urb); |
| 866 | usb_kill_urb(pegasus->intr_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | goto exit; |
| 868 | } |
| 869 | set_carrier(net); |
| 870 | netif_start_queue(net); |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 871 | netif_dbg(pegasus, ifup, net, "open\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | res = 0; |
| 873 | exit: |
| 874 | return res; |
| 875 | } |
| 876 | |
| 877 | static int pegasus_close(struct net_device *net) |
| 878 | { |
| 879 | pegasus_t *pegasus = netdev_priv(net); |
| 880 | |
| 881 | netif_stop_queue(net); |
| 882 | if (!(pegasus->flags & PEGASUS_UNPLUG)) |
| 883 | disable_net_traffic(pegasus); |
| 884 | tasklet_kill(&pegasus->rx_tl); |
| 885 | unlink_all_urbs(pegasus); |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static void pegasus_get_drvinfo(struct net_device *dev, |
| 891 | struct ethtool_drvinfo *info) |
| 892 | { |
| 893 | pegasus_t *pegasus = netdev_priv(dev); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 894 | |
| 895 | strlcpy(info->driver, driver_name, sizeof(info->driver)); |
| 896 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 897 | usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* also handles three patterns of some kind in hardware */ |
| 901 | #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY) |
| 902 | |
| 903 | static void |
| 904 | pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 905 | { |
| 906 | pegasus_t *pegasus = netdev_priv(dev); |
| 907 | |
| 908 | wol->supported = WAKE_MAGIC | WAKE_PHY; |
| 909 | wol->wolopts = pegasus->wolopts; |
| 910 | } |
| 911 | |
| 912 | static int |
| 913 | pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 914 | { |
| 915 | pegasus_t *pegasus = netdev_priv(dev); |
| 916 | u8 reg78 = 0x04; |
Ming Lei | 4fbc5b2 | 2013-01-19 01:32:01 +0000 | [diff] [blame] | 917 | int ret; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (wol->wolopts & ~WOL_SUPPORTED) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | if (wol->wolopts & WAKE_MAGIC) |
| 923 | reg78 |= 0x80; |
| 924 | if (wol->wolopts & WAKE_PHY) |
| 925 | reg78 |= 0x40; |
| 926 | /* FIXME this 0x10 bit still needs to get set in the chip... */ |
| 927 | if (wol->wolopts) |
| 928 | pegasus->eth_regs[0] |= 0x10; |
| 929 | else |
| 930 | pegasus->eth_regs[0] &= ~0x10; |
| 931 | pegasus->wolopts = wol->wolopts; |
Ming Lei | 4fbc5b2 | 2013-01-19 01:32:01 +0000 | [diff] [blame] | 932 | |
| 933 | ret = set_register(pegasus, WakeupControl, reg78); |
| 934 | if (!ret) |
| 935 | ret = device_set_wakeup_enable(&pegasus->usb->dev, |
| 936 | wol->wolopts); |
| 937 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static inline void pegasus_reset_wol(struct net_device *dev) |
| 941 | { |
| 942 | struct ethtool_wolinfo wol; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | memset(&wol, 0, sizeof wol); |
| 945 | (void) pegasus_set_wol(dev, &wol); |
| 946 | } |
| 947 | |
| 948 | static int |
Philippe Reynes | 71dbc341 | 2017-03-17 23:34:04 +0100 | [diff] [blame] | 949 | pegasus_get_link_ksettings(struct net_device *dev, |
| 950 | struct ethtool_link_ksettings *ecmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
| 952 | pegasus_t *pegasus; |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | pegasus = netdev_priv(dev); |
Philippe Reynes | 71dbc341 | 2017-03-17 23:34:04 +0100 | [diff] [blame] | 955 | mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int |
Philippe Reynes | 71dbc341 | 2017-03-17 23:34:04 +0100 | [diff] [blame] | 960 | pegasus_set_link_ksettings(struct net_device *dev, |
| 961 | const struct ethtool_link_ksettings *ecmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | { |
| 963 | pegasus_t *pegasus = netdev_priv(dev); |
Philippe Reynes | 71dbc341 | 2017-03-17 23:34:04 +0100 | [diff] [blame] | 964 | return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static int pegasus_nway_reset(struct net_device *dev) |
| 968 | { |
| 969 | pegasus_t *pegasus = netdev_priv(dev); |
| 970 | return mii_nway_restart(&pegasus->mii); |
| 971 | } |
| 972 | |
| 973 | static u32 pegasus_get_link(struct net_device *dev) |
| 974 | { |
| 975 | pegasus_t *pegasus = netdev_priv(dev); |
| 976 | return mii_link_ok(&pegasus->mii); |
| 977 | } |
| 978 | |
| 979 | static u32 pegasus_get_msglevel(struct net_device *dev) |
| 980 | { |
| 981 | pegasus_t *pegasus = netdev_priv(dev); |
| 982 | return pegasus->msg_enable; |
| 983 | } |
| 984 | |
| 985 | static void pegasus_set_msglevel(struct net_device *dev, u32 v) |
| 986 | { |
| 987 | pegasus_t *pegasus = netdev_priv(dev); |
| 988 | pegasus->msg_enable = v; |
| 989 | } |
| 990 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 991 | static const struct ethtool_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | .get_drvinfo = pegasus_get_drvinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | .nway_reset = pegasus_nway_reset, |
| 994 | .get_link = pegasus_get_link, |
| 995 | .get_msglevel = pegasus_get_msglevel, |
| 996 | .set_msglevel = pegasus_set_msglevel, |
| 997 | .get_wol = pegasus_get_wol, |
| 998 | .set_wol = pegasus_set_wol, |
Philippe Reynes | 71dbc341 | 2017-03-17 23:34:04 +0100 | [diff] [blame] | 999 | .get_link_ksettings = pegasus_get_link_ksettings, |
| 1000 | .set_link_ksettings = pegasus_set_link_ksettings, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | }; |
| 1002 | |
| 1003 | static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd) |
| 1004 | { |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1005 | __u16 *data = (__u16 *) &rq->ifr_ifru; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | pegasus_t *pegasus = netdev_priv(net); |
| 1007 | int res; |
| 1008 | |
| 1009 | switch (cmd) { |
| 1010 | case SIOCDEVPRIVATE: |
| 1011 | data[0] = pegasus->phy; |
Gustavo A. R. Silva | 209d6e7 | 2019-02-08 13:07:29 -0600 | [diff] [blame] | 1012 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | case SIOCDEVPRIVATE + 1: |
| 1014 | read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]); |
| 1015 | res = 0; |
| 1016 | break; |
| 1017 | case SIOCDEVPRIVATE + 2: |
| 1018 | if (!capable(CAP_NET_ADMIN)) |
| 1019 | return -EPERM; |
Petko Manolov | 2bd6470 | 2013-04-25 22:41:36 +0000 | [diff] [blame] | 1020 | write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | res = 0; |
| 1022 | break; |
| 1023 | default: |
| 1024 | res = -EOPNOTSUPP; |
| 1025 | } |
| 1026 | return res; |
| 1027 | } |
| 1028 | |
| 1029 | static void pegasus_set_multicast(struct net_device *net) |
| 1030 | { |
| 1031 | pegasus_t *pegasus = netdev_priv(net); |
| 1032 | |
| 1033 | if (net->flags & IFF_PROMISC) { |
| 1034 | pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS; |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1035 | netif_info(pegasus, link, net, "Promiscuous mode enabled\n"); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 1036 | } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST; |
| 1038 | pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS; |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1039 | netif_dbg(pegasus, link, net, "set allmulti\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } else { |
| 1041 | pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST; |
| 1042 | pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS; |
| 1043 | } |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 1044 | update_eth_regs_async(pegasus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1047 | static __u8 mii_phy_probe(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
| 1049 | int i; |
| 1050 | __u16 tmp; |
| 1051 | |
| 1052 | for (i = 0; i < 32; i++) { |
| 1053 | read_mii_word(pegasus, i, MII_BMSR, &tmp); |
| 1054 | if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0) |
| 1055 | continue; |
| 1056 | else |
| 1057 | return i; |
| 1058 | } |
| 1059 | |
| 1060 | return 0xff; |
| 1061 | } |
| 1062 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1063 | static inline void setup_pegasus_II(pegasus_t *pegasus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | { |
| 1065 | __u8 data = 0xa5; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1066 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1067 | set_register(pegasus, Reg1d, 0); |
| 1068 | set_register(pegasus, Reg7b, 1); |
Jia-Ju Bai | 6dff5ad | 2018-07-27 16:36:29 +0800 | [diff] [blame] | 1069 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | if ((pegasus->features & HAS_HOME_PNA) && mii_mode) |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1071 | set_register(pegasus, Reg7b, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | else |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1073 | set_register(pegasus, Reg7b, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1075 | set_register(pegasus, 0x83, data); |
| 1076 | get_registers(pegasus, 0x83, 1, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1078 | if (data == 0xa5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | pegasus->chip = 0x8513; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1080 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | pegasus->chip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1083 | set_register(pegasus, 0x80, 0xc0); |
| 1084 | set_register(pegasus, 0x83, 0xff); |
| 1085 | set_register(pegasus, 0x84, 0x01); |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (pegasus->features & HAS_HOME_PNA && mii_mode) |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1088 | set_register(pegasus, Reg81, 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | else |
Petko Manolov | 4a1728a | 2005-11-15 09:48:23 +0200 | [diff] [blame] | 1090 | set_register(pegasus, Reg81, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1093 | static void check_carrier(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1095 | pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | set_carrier(pegasus->net); |
| 1097 | if (!(pegasus->flags & PEGASUS_UNPLUG)) { |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 1098 | queue_delayed_work(system_long_wq, &pegasus->carrier_check, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | CARRIER_CHECK_DELAY); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Ben Collins | 4f63135 | 2008-07-30 12:39:02 -0700 | [diff] [blame] | 1103 | static int pegasus_blacklisted(struct usb_device *udev) |
| 1104 | { |
| 1105 | struct usb_device_descriptor *udd = &udev->descriptor; |
| 1106 | |
| 1107 | /* Special quirk to keep the driver from handling the Belkin Bluetooth |
| 1108 | * dongle which happens to have the same ID. |
| 1109 | */ |
Michael Buesch | e3453f6 | 2009-06-18 07:03:47 +0000 | [diff] [blame] | 1110 | if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) && |
| 1111 | (udd->idProduct == cpu_to_le16(0x0121)) && |
Ben Collins | 4f63135 | 2008-07-30 12:39:02 -0700 | [diff] [blame] | 1112 | (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) && |
| 1113 | (udd->bDeviceProtocol == 1)) |
| 1114 | return 1; |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | static int pegasus_probe(struct usb_interface *intf, |
| 1120 | const struct usb_device_id *id) |
| 1121 | { |
| 1122 | struct usb_device *dev = interface_to_usbdev(intf); |
| 1123 | struct net_device *net; |
| 1124 | pegasus_t *pegasus; |
| 1125 | int dev_index = id - pegasus_ids; |
| 1126 | int res = -ENOMEM; |
| 1127 | |
David Brownell | cda2836 | 2008-11-16 00:36:08 -0800 | [diff] [blame] | 1128 | if (pegasus_blacklisted(dev)) |
| 1129 | return -ENODEV; |
Ben Collins | 4f63135 | 2008-07-30 12:39:02 -0700 | [diff] [blame] | 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | net = alloc_etherdev(sizeof(struct pegasus)); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1132 | if (!net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | pegasus = netdev_priv(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | pegasus->dev_index = dev_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 1138 | res = alloc_urbs(pegasus); |
| 1139 | if (res < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | dev_err(&intf->dev, "can't allocate %s\n", "urbs"); |
| 1141 | goto out1; |
| 1142 | } |
| 1143 | |
| 1144 | tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus); |
| 1145 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1146 | INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | pegasus->intf = intf; |
| 1149 | pegasus->usb = dev; |
| 1150 | pegasus->net = net; |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 1151 | |
| 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | net->watchdog_timeo = PEGASUS_TX_TIMEOUT; |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 1154 | net->netdev_ops = &pegasus_netdev_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1155 | net->ethtool_ops = &ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | pegasus->mii.dev = net; |
| 1157 | pegasus->mii.mdio_read = mdio_read; |
| 1158 | pegasus->mii.mdio_write = mdio_write; |
| 1159 | pegasus->mii.phy_id_mask = 0x1f; |
| 1160 | pegasus->mii.reg_num_mask = 0x1f; |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1161 | pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); |
| 1163 | |
| 1164 | pegasus->features = usb_dev_id[dev_index].private; |
| 1165 | get_interrupt_interval(pegasus); |
| 1166 | if (reset_mac(pegasus)) { |
| 1167 | dev_err(&intf->dev, "can't reset MAC\n"); |
| 1168 | res = -EIO; |
| 1169 | goto out2; |
| 1170 | } |
| 1171 | set_ethernet_addr(pegasus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (pegasus->features & PEGASUS_II) { |
| 1173 | dev_info(&intf->dev, "setup Pegasus II specific registers\n"); |
| 1174 | setup_pegasus_II(pegasus); |
| 1175 | } |
| 1176 | pegasus->phy = mii_phy_probe(pegasus); |
| 1177 | if (pegasus->phy == 0xff) { |
| 1178 | dev_warn(&intf->dev, "can't locate MII phy, using default\n"); |
| 1179 | pegasus->phy = 1; |
| 1180 | } |
| 1181 | pegasus->mii.phy_id = pegasus->phy; |
| 1182 | usb_set_intfdata(intf, pegasus); |
| 1183 | SET_NETDEV_DEV(net, &intf->dev); |
| 1184 | pegasus_reset_wol(net); |
| 1185 | res = register_netdev(net); |
| 1186 | if (res) |
| 1187 | goto out3; |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 1188 | queue_delayed_work(system_long_wq, &pegasus->carrier_check, |
Petko Manolov | 323b349 | 2013-04-25 22:41:50 +0000 | [diff] [blame] | 1189 | CARRIER_CHECK_DELAY); |
| 1190 | dev_info(&intf->dev, "%s, %s, %pM\n", net->name, |
| 1191 | usb_dev_id[dev_index].name, net->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | return 0; |
| 1193 | |
| 1194 | out3: |
| 1195 | usb_set_intfdata(intf, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | out2: |
| 1197 | free_all_urbs(pegasus); |
| 1198 | out1: |
| 1199 | free_netdev(net); |
| 1200 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | return res; |
| 1202 | } |
| 1203 | |
| 1204 | static void pegasus_disconnect(struct usb_interface *intf) |
| 1205 | { |
| 1206 | struct pegasus *pegasus = usb_get_intfdata(intf); |
| 1207 | |
| 1208 | usb_set_intfdata(intf, NULL); |
| 1209 | if (!pegasus) { |
| 1210 | dev_dbg(&intf->dev, "unregistering non-bound device?\n"); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | pegasus->flags |= PEGASUS_UNPLUG; |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 1215 | cancel_delayed_work_sync(&pegasus->carrier_check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | unregister_netdev(pegasus->net); |
Kevin Vigor | a85a46f | 2005-09-22 00:49:24 -0700 | [diff] [blame] | 1217 | unlink_all_urbs(pegasus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | free_all_urbs(pegasus); |
Arnaldo Carvalho de Melo | 4c13eb6 | 2007-04-25 17:40:23 -0700 | [diff] [blame] | 1219 | if (pegasus->rx_skb != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | dev_kfree_skb(pegasus->rx_skb); |
Arnaldo Carvalho de Melo | 4c13eb6 | 2007-04-25 17:40:23 -0700 | [diff] [blame] | 1221 | pegasus->rx_skb = NULL; |
| 1222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | free_netdev(pegasus->net); |
| 1224 | } |
| 1225 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1226 | static int pegasus_suspend(struct usb_interface *intf, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | { |
| 1228 | struct pegasus *pegasus = usb_get_intfdata(intf); |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1229 | |
| 1230 | netif_device_detach(pegasus->net); |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 1231 | cancel_delayed_work_sync(&pegasus->carrier_check); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1232 | if (netif_running(pegasus->net)) { |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1233 | usb_kill_urb(pegasus->rx_urb); |
| 1234 | usb_kill_urb(pegasus->intr_urb); |
| 1235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |
| 1238 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1239 | static int pegasus_resume(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { |
| 1241 | struct pegasus *pegasus = usb_get_intfdata(intf); |
| 1242 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1243 | netif_device_attach(pegasus->net); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1244 | if (netif_running(pegasus->net)) { |
| 1245 | pegasus->rx_urb->status = 0; |
| 1246 | pegasus->rx_urb->actual_length = 0; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1247 | read_bulk_callback(pegasus->rx_urb); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1248 | |
| 1249 | pegasus->intr_urb->status = 0; |
| 1250 | pegasus->intr_urb->actual_length = 0; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1251 | intr_callback(pegasus->intr_urb); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1252 | } |
Petko Manolov | 2f5107c | 2020-04-02 17:33:29 +0300 | [diff] [blame^] | 1253 | queue_delayed_work(system_long_wq, &pegasus->carrier_check, |
David Brownell | 7e713b8 | 2006-05-01 14:02:45 -0700 | [diff] [blame] | 1254 | CARRIER_CHECK_DELAY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
| 1257 | |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 1258 | static const struct net_device_ops pegasus_netdev_ops = { |
| 1259 | .ndo_open = pegasus_open, |
| 1260 | .ndo_stop = pegasus_close, |
| 1261 | .ndo_do_ioctl = pegasus_ioctl, |
| 1262 | .ndo_start_xmit = pegasus_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1263 | .ndo_set_rx_mode = pegasus_set_multicast, |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 1264 | .ndo_tx_timeout = pegasus_tx_timeout, |
Ben Hutchings | 240c102 | 2009-07-09 17:54:35 +0000 | [diff] [blame] | 1265 | .ndo_set_mac_address = eth_mac_addr, |
| 1266 | .ndo_validate_addr = eth_validate_addr, |
Oliver Neukum | 8cb8957 | 2009-01-08 11:22:25 -0800 | [diff] [blame] | 1267 | }; |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | static struct usb_driver pegasus_driver = { |
| 1270 | .name = driver_name, |
| 1271 | .probe = pegasus_probe, |
| 1272 | .disconnect = pegasus_disconnect, |
| 1273 | .id_table = pegasus_ids, |
| 1274 | .suspend = pegasus_suspend, |
| 1275 | .resume = pegasus_resume, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 1276 | .disable_hub_initiated_lpm = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | }; |
| 1278 | |
David Brownell | cda2836 | 2008-11-16 00:36:08 -0800 | [diff] [blame] | 1279 | static void __init parse_id(char *id) |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 1280 | { |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1281 | unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0; |
| 1282 | char *token, *name = NULL; |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 1283 | |
| 1284 | if ((token = strsep(&id, ":")) != NULL) |
| 1285 | name = token; |
| 1286 | /* name now points to a null terminated string*/ |
| 1287 | if ((token = strsep(&id, ":")) != NULL) |
| 1288 | vendor_id = simple_strtoul(token, NULL, 16); |
| 1289 | if ((token = strsep(&id, ":")) != NULL) |
| 1290 | device_id = simple_strtoul(token, NULL, 16); |
| 1291 | flags = simple_strtoul(id, NULL, 16); |
| 1292 | pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n", |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1293 | driver_name, name, vendor_id, device_id, flags); |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 1294 | |
| 1295 | if (vendor_id > 0x10000 || vendor_id == 0) |
| 1296 | return; |
| 1297 | if (device_id > 0x10000 || device_id == 0) |
| 1298 | return; |
| 1299 | |
Nicolas Kaiser | 5a9dbfe | 2010-06-26 06:58:54 +0000 | [diff] [blame] | 1300 | for (i = 0; usb_dev_id[i].name; i++); |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 1301 | usb_dev_id[i].name = name; |
| 1302 | usb_dev_id[i].vendor = vendor_id; |
| 1303 | usb_dev_id[i].device = device_id; |
| 1304 | usb_dev_id[i].private = flags; |
| 1305 | pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
| 1306 | pegasus_ids[i].idVendor = vendor_id; |
| 1307 | pegasus_ids[i].idProduct = device_id; |
| 1308 | } |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | static int __init pegasus_init(void) |
| 1311 | { |
| 1312 | pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION); |
A.YOSHIYAMA | a4f81a6 | 2005-11-15 09:55:18 +0200 | [diff] [blame] | 1313 | if (devid) |
| 1314 | parse_id(devid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | return usb_register(&pegasus_driver); |
| 1316 | } |
| 1317 | |
| 1318 | static void __exit pegasus_exit(void) |
| 1319 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | usb_deregister(&pegasus_driver); |
| 1321 | } |
| 1322 | |
| 1323 | module_init(pegasus_init); |
| 1324 | module_exit(pegasus_exit); |