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