blob: 26b4e48bf91ff50e573e70788d03cd6740a843a8 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Petko Manolov323b3492013-04-25 22:41:50 +00003 * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Kaiser5a9dbfe2010-06-26 06:58:54 +000021 * time as this seems to break dhcpd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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 Kaiser5a9dbfe2010-06-26 06:58:54 +000025 * is out of the interrupt routine.
Petko Manolov323b3492013-04-25 22:41:50 +000026 * ...
27 * v0.9.3 simplified [get|set]_register(s), async update registers
28 * logic revisited, receive skb_pool removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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 Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "pegasus.h"
44
45/*
46 * Version Information
47 */
Petko Manolov323b3492013-04-25 22:41:50 +000048#define DRIVER_VERSION "v0.9.3 (2013/04/25)"
49#define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
51
52static 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 Manolov2f5107c2020-04-02 17:33:29 +030057#define CARRIER_CHECK_DELAY (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Rusty Russelleb939922011-12-19 14:08:01 +000059static bool loopback;
60static bool mii_mode;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +000061static char *devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63static 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 Rankinab854b22009-10-13 00:32:02 -070066#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
67 PEGASUS_DEV(pn, vid, pid, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "pegasus.h"
69#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070070#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020071 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 {NULL, 0, 0, 0}
73};
74
75static 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 Rankinab854b22009-10-13 00:32:02 -070078/*
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 Torvalds1da177e2005-04-16 15:20:36 -070087#include "pegasus.h"
88#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070089#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020090 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 {}
92};
93
94MODULE_AUTHOR(DRIVER_AUTHOR);
95MODULE_DESCRIPTION(DRIVER_DESC);
96MODULE_LICENSE("GPL");
97module_param(loopback, bool, 0);
98module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020099module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
101MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +0200102MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/* use ethtool to change the level for any given device */
105static int msg_level = -1;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000106module_param(msg_level, int, 0);
107MODULE_PARM_DESC(msg_level, "Override default message level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109MODULE_DEVICE_TABLE(usb, pegasus_ids);
Oliver Neukum8cb89572009-01-08 11:22:25 -0800110static const struct net_device_ops pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Petko Manolov323b3492013-04-25 22:41:50 +0000112/*****/
113
114static void async_ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Petko Manolov323b3492013-04-25 22:41:50 +0000116 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800117 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Petko Manolov323b3492013-04-25 22:41:50 +0000119 if (status < 0)
120 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
121 kfree(req);
122 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Petko Manolov323b3492013-04-25 22:41:50 +0000125static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Petko Manolovfb58cf42020-09-27 15:49:08 +0300127 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 Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Ben Hutchings55935232017-02-04 16:56:03 +0000132static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
133 const void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Petko Manolovfb58cf42020-09-27 15:49:08 +0300135 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 Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Petko Manolovfb58cf42020-09-27 15:49:08 +0300140/*
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 Kaiser5a9dbfe2010-06-26 06:58:54 +0000145static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Petko Manolovfb58cf42020-09-27 15:49:08 +0300147 void *buf = &data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Petko Manolovfb58cf42020-09-27 15:49:08 +0300149 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 Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000154static int update_eth_regs_async(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Petko Manolov323b3492013-04-25 22:41:50 +0000156 int ret = -ENOMEM;
157 struct urb *async_urb;
158 struct usb_ctrlrequest *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Petko Manolov323b3492013-04-25 22:41:50 +0000160 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
161 if (req == NULL)
162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Petko Manolov323b3492013-04-25 22:41:50 +0000164 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 Torvalds1da177e2005-04-16 15:20:36 -0700174
Petko Manolov323b3492013-04-25 22:41:50 +0000175 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 Brownell955a2602006-05-26 10:17:03 -0700181 if (ret == -ENODEV)
182 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000183 netif_err(pegasus, drv, pegasus->net,
Petko Manolov323b3492013-04-25 22:41:50 +0000184 "%s returned %d\n", __func__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return ret;
187}
188
Petko Manolov2bd64702013-04-25 22:41:36 +0000189static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int i;
192 __u8 data[4] = { phy, 0, 0, indx };
193 __le16 regdi;
Petko Manolov2bd64702013-04-25 22:41:36 +0000194 int ret = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Petko Manolov2bd64702013-04-25 22:41:36 +0000196 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 Torvalds1da177e2005-04-16 15:20:36 -0700203 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov2bd64702013-04-25 22:41:36 +0000204 ret = get_registers(p, PhyCtrl, 1, data);
205 if (ret < 0)
David Brownell7e713b82006-05-01 14:02:45 -0700206 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (data[0] & PHY_DONE)
208 break;
209 }
Joe Perchesa475f602010-02-17 10:30:24 +0000210 if (i >= REG_TIMEOUT)
211 goto fail;
Petko Manolov2bd64702013-04-25 22:41:36 +0000212 if (cmd & PHY_READ) {
213 ret = get_registers(p, PhyData, 2, &regdi);
214 *regd = le16_to_cpu(regdi);
215 return ret;
216 }
217 return 0;
David Brownell7e713b82006-05-01 14:02:45 -0700218fail:
Petko Manolov2bd64702013-04-25 22:41:36 +0000219 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Petko Manolov2bd64702013-04-25 22:41:36 +0000223/* Returns non-negative int on success, error on failure */
224static 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 */
230static 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 Torvalds1da177e2005-04-16 15:20:36 -0700235static int mdio_read(struct net_device *dev, int phy_id, int loc)
236{
Joe Perches8739cfe2010-11-15 11:12:29 +0000237 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 u16 res;
239
240 read_mii_word(pegasus, phy_id, loc, &res);
241 return (int)res;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
245{
Joe Perches8739cfe2010-11-15 11:12:29 +0000246 pegasus_t *pegasus = netdev_priv(dev);
Dan Carpenter3d64fc72013-05-02 20:44:20 +0000247 u16 data = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Carpenter3d64fc72013-05-02 20:44:20 +0000249 write_mii_word(pegasus, phy_id, loc, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000252static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int i;
Denis Kirjanov224c0492019-07-30 15:13:57 +0200255 __u8 tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 __le16 retdatai;
257 int ret;
258
Petko Manolov4a1728a2005-11-15 09:48:23 +0200259 set_register(pegasus, EpromCtrl, 0);
260 set_register(pegasus, EpromOffset, index);
261 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 for (i = 0; i < REG_TIMEOUT; i++) {
264 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
265 if (tmp & EPROM_DONE)
266 break;
David Brownell7e713b82006-05-01 14:02:45 -0700267 if (ret == -ESHUTDOWN)
268 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Joe Perchesa475f602010-02-17 10:30:24 +0000270 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 Torvalds1da177e2005-04-16 15:20:36 -0700276
David Brownell7e713b82006-05-01 14:02:45 -0700277fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000278 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200279 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282#ifdef PEGASUS_WRITE_EEPROM
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000283static inline void enable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Petko Manolov4a1728a2005-11-15 09:48:23 +0200287 get_registers(pegasus, EthCtrl2, 1, &tmp);
288 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000291static inline void disable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Petko Manolov4a1728a2005-11-15 09:48:23 +0200295 get_registers(pegasus, EthCtrl2, 1, &tmp);
296 set_register(pegasus, EpromCtrl, 0);
297 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000300static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 int i;
303 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
304 int ret;
Michael Buesche3453f62009-06-18 07:03:47 +0000305 __le16 le_data = cpu_to_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Petko Manolov4a1728a2005-11-15 09:48:23 +0200307 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200309 set_register(pegasus, EpromOffset, index);
Michael Buesche3453f62009-06-18 07:03:47 +0000310 set_registers(pegasus, EpromData, 2, &le_data);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200311 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 for (i = 0; i < REG_TIMEOUT; i++) {
314 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700315 if (ret == -ESHUTDOWN)
316 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (tmp & EPROM_DONE)
318 break;
319 }
320 disable_eprom_write(pegasus);
Joe Perchesa475f602010-02-17 10:30:24 +0000321 if (i >= REG_TIMEOUT)
322 goto fail;
323
324 return ret;
325
David Brownell7e713b82006-05-01 14:02:45 -0700326fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000327 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200328 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330#endif /* PEGASUS_WRITE_EEPROM */
331
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000332static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int i;
335 __u16 w16;
336
337 for (i = 0; i < 3; i++) {
338 read_eprom_word(pegasus, i, &w16);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700339 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341}
342
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000343static void set_ethernet_addr(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Petko Manolov37cf3472006-09-27 14:25:37 -0700347 if (pegasus->features & PEGASUS_II) {
348 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
349 } else {
350 get_node_id(pegasus, node_id);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000351 set_registers(pegasus, EthID, sizeof(node_id), node_id);
Petko Manolov37cf3472006-09-27 14:25:37 -0700352 }
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000353 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000356static inline int reset_mac(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 __u8 data = 0x8;
359 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Petko Manolov4a1728a2005-11-15 09:48:23 +0200361 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200363 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (~data & 0x08) {
Dan Carpenter681f1622011-12-23 00:44:36 +0000365 if (loopback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 break;
367 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200368 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200370 set_register(pegasus, Gpio1, 0x26);
371 set_register(pegasus, Gpio0, pegasus->features);
372 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 }
375 }
376 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200377 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
380 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200381 set_register(pegasus, Gpio0, 0x24);
382 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
385 __u16 auxmode;
386 read_mii_word(pegasus, 3, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000387 auxmode |= 4;
388 write_mii_word(pegasus, 3, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 return 0;
392}
393
394static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
395{
396 __u16 linkpart;
397 __u8 data[4];
398 pegasus_t *pegasus = netdev_priv(dev);
399 int ret;
400
401 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
Petko Manolov1a8deec2016-04-27 14:24:50 +0300402 data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 data[1] = 0;
404 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
405 data[1] |= 0x20; /* set full duplex */
406 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
407 data[1] |= 0x10; /* set 100 Mbps */
408 if (mii_mode)
409 data[1] = 0;
Dan Carpenter681f1622011-12-23 00:44:36 +0000410 data[2] = loopback ? 0x09 : 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000412 memcpy(pegasus->eth_regs, data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret = set_registers(pegasus, EthCtrl0, 3, data);
414
415 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100416 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
418 u16 auxmode;
419 read_mii_word(pegasus, 0, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000420 auxmode |= 4;
421 write_mii_word(pegasus, 0, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Petko Manolov4a1728a2005-11-15 09:48:23 +0200424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
David Howells7d12e782006-10-05 14:55:46 +0100427static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 pegasus_t *pegasus = urb->context;
430 struct net_device *net;
431 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800432 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 u8 *buf = urb->transfer_buffer;
434 __u16 pkt_len;
435
436 if (!pegasus)
437 return;
438
439 net = pegasus->net;
440 if (!netif_device_present(net) || !netif_running(net))
441 return;
442
Oliver Neukumc94cb312008-12-18 23:00:59 -0800443 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 case 0:
445 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700446 case -ETIME:
Joe Perchesa475f602010-02-17 10:30:24 +0000447 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 pegasus->flags &= ~PEGASUS_RX_BUSY;
449 break;
450 case -EPIPE: /* stall, or disconnect from TT */
451 /* FIXME schedule work to clear the halt */
Joe Perchesa475f602010-02-17 10:30:24 +0000452 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return;
454 case -ENOENT:
455 case -ECONNRESET:
456 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000457 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return;
459 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000460 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto goon;
462 }
463
xypron.glpk@gmx.debbf178e2016-05-18 20:40:51 +0200464 if (count < 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto goon;
466
467 rx_status = buf[count - 2];
468 if (rx_status & 0x1e) {
Joe Perchesa475f602010-02-17 10:30:24 +0000469 netif_dbg(pegasus, rx_err, net,
470 "RX packet error %x\n", rx_status);
Tobias Klauser82d82932017-04-07 10:17:39 +0200471 net->stats.rx_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000472 if (rx_status & 0x06) /* long or runt */
Tobias Klauser82d82932017-04-07 10:17:39 +0200473 net->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (rx_status & 0x08)
Tobias Klauser82d82932017-04-07 10:17:39 +0200475 net->stats.rx_crc_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000476 if (rx_status & 0x10) /* extra bits */
Tobias Klauser82d82932017-04-07 10:17:39 +0200477 net->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto goon;
479 }
480 if (pegasus->chip == 0x8513) {
481 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
482 pkt_len &= 0x0fff;
483 pegasus->rx_skb->data += 2;
484 } else {
485 pkt_len = buf[count - 3] << 8;
486 pkt_len += buf[count - 4];
487 pkt_len &= 0xfff;
Petko Manolov1a8deec2016-04-27 14:24:50 +0300488 pkt_len -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700492 * If the packet is unreasonably long, quietly drop it rather than
493 * kernel panicing by calling skb_put.
494 */
495 if (pkt_len > PEGASUS_MTU)
496 goto goon;
497
498 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * at this point we are sure pegasus->rx_skb != NULL
500 * so we go ahead and pass up the packet.
501 */
502 skb_put(pegasus->rx_skb, pkt_len);
503 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
504 netif_rx(pegasus->rx_skb);
Tobias Klauser82d82932017-04-07 10:17:39 +0200505 net->stats.rx_packets++;
506 net->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (pegasus->flags & PEGASUS_UNPLUG)
509 return;
510
Petko Manolov313a58e2013-04-25 22:41:21 +0000511 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
512 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (pegasus->rx_skb == NULL)
515 goto tl_sched;
516goon:
517 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
518 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300519 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700521 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
522 if (rx_status == -ENODEV)
523 netif_device_detach(pegasus->net);
524 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 pegasus->flags |= PEGASUS_RX_URB_FAIL;
526 goto tl_sched;
527 } else {
528 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
529 }
530
531 return;
532
533tl_sched:
534 tasklet_schedule(&pegasus->rx_tl);
535}
536
537static void rx_fixup(unsigned long data)
538{
539 pegasus_t *pegasus;
David Brownell955a2602006-05-26 10:17:03 -0700540 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 pegasus = (pegasus_t *) data;
543 if (pegasus->flags & PEGASUS_UNPLUG)
544 return;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
547 if (pegasus->rx_skb)
548 goto try_again;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000549 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000550 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
551 PEGASUS_MTU,
552 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (pegasus->rx_skb == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000554 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 tasklet_schedule(&pegasus->rx_tl);
Petko Manolov313a58e2013-04-25 22:41:21 +0000556 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
559 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300560 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 read_bulk_callback, pegasus);
562try_again:
David Brownell955a2602006-05-26 10:17:03 -0700563 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
564 if (status == -ENODEV)
565 netif_device_detach(pegasus->net);
566 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 pegasus->flags |= PEGASUS_RX_URB_FAIL;
568 tasklet_schedule(&pegasus->rx_tl);
569 } else {
570 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
David Howells7d12e782006-10-05 14:55:46 +0100574static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800577 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800578 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (!pegasus)
581 return;
582
Micah Gruber93519822007-07-23 16:05:52 +0800583 net = pegasus->net;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!netif_device_present(net) || !netif_running(net))
586 return;
587
Oliver Neukumc94cb312008-12-18 23:00:59 -0800588 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 case -EPIPE:
590 /* FIXME schedule_work() to clear the tx halt */
591 netif_stop_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000592 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return;
594 case -ENOENT:
595 case -ECONNRESET:
596 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000597 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
599 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000600 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500601 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 case 0:
603 break;
604 }
605
Florian Westphal860e9532016-05-03 16:33:13 +0200606 netif_trans_update(net); /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 netif_wake_queue(net);
608}
609
David Howells7d12e782006-10-05 14:55:46 +0100610static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 pegasus_t *pegasus = urb->context;
613 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800614 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (!pegasus)
617 return;
618 net = pegasus->net;
619
Oliver Neukumc94cb312008-12-18 23:00:59 -0800620 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 case 0:
622 break;
623 case -ECONNRESET: /* unlink */
624 case -ENOENT:
625 case -ESHUTDOWN:
626 return;
627 default:
628 /* some Pegasus-I products report LOTS of data
629 * toggle errors... avoid log spamming
630 */
Joe Perchesa475f602010-02-17 10:30:24 +0000631 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 if (urb->actual_length >= 6) {
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000635 u8 *d = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* byte 0 == tx_status1, reg 2B */
638 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
639 |LATE_COL|JABBER_TIMEOUT)) {
Tobias Klauser82d82932017-04-07 10:17:39 +0200640 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (d[0] & TX_UNDERRUN)
Tobias Klauser82d82932017-04-07 10:17:39 +0200642 net->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
Tobias Klauser82d82932017-04-07 10:17:39 +0200644 net->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (d[0] & LATE_COL)
Tobias Klauser82d82932017-04-07 10:17:39 +0200646 net->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 /* d[5].LINK_STATUS lies on some adapters.
650 * d[0].NO_CARRIER kicks in only with failed TX.
651 * ... so monitoring with MII may be safest.
652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
Tobias Klauser82d82932017-04-07 10:17:39 +0200655 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
Oliver Neukumc94cb312008-12-18 23:00:59 -0800658 res = usb_submit_urb(urb, GFP_ATOMIC);
659 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700660 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000661 if (res)
662 netif_err(pegasus, timer, net,
663 "can't resubmit interrupt urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -0500666static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 pegasus_t *pegasus = netdev_priv(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000669 netif_warn(pegasus, timer, net, "tx timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 usb_unlink_urb(pegasus->tx_urb);
Tobias Klauser82d82932017-04-07 10:17:39 +0200671 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000674static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
675 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 pegasus_t *pegasus = netdev_priv(net);
678 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
679 int res;
680 __u16 l16 = skb->len;
681
682 netif_stop_queue(net);
683
684 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300685 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
687 usb_sndbulkpipe(pegasus->usb, 2),
688 pegasus->tx_buff, count,
689 write_bulk_callback, pegasus);
690 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +0000691 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 switch (res) {
693 case -EPIPE: /* stall, or disconnect from TT */
694 /* cleanup should already have been scheduled */
695 break;
696 case -ENODEV: /* disconnect() upcoming */
Oliver Neukum9dd014e2009-04-17 01:40:19 -0700697 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700698 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
700 default:
Tobias Klauser82d82932017-04-07 10:17:39 +0200701 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 netif_start_queue(net);
703 }
704 } else {
Tobias Klauser82d82932017-04-07 10:17:39 +0200705 net->stats.tx_packets++;
706 net->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 dev_kfree_skb(skb);
709
Patrick McHardy6ed10652009-06-23 06:03:08 +0000710 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000713static inline void disable_net_traffic(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Michael Buesche3453f62009-06-18 07:03:47 +0000715 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Michael Buesche3453f62009-06-18 07:03:47 +0000717 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000720static inline void get_interrupt_interval(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Michael Buesche3453f62009-06-18 07:03:47 +0000722 u16 data;
723 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Michael Buesche3453f62009-06-18 07:03:47 +0000725 read_eprom_word(pegasus, 4, &data);
726 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700727 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000728 if (interval < 0x80) {
Joe Perchesa475f602010-02-17 10:30:24 +0000729 netif_info(pegasus, timer, pegasus->net,
730 "intr interval changed from %ums to %ums\n",
731 interval, 0x80);
Michael Buesche3453f62009-06-18 07:03:47 +0000732 interval = 0x80;
733 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700734#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000735 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Michael Buesche3453f62009-06-18 07:03:47 +0000739 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static void set_carrier(struct net_device *net)
743{
744 pegasus_t *pegasus = netdev_priv(net);
745 u16 tmp;
746
Dan Williamsc43c49b2007-04-24 10:20:06 -0400747 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (tmp & BMSR_LSTATUS)
751 netif_carrier_on(net);
752 else
753 netif_carrier_off(net);
754}
755
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000756static void free_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 usb_free_urb(pegasus->intr_urb);
759 usb_free_urb(pegasus->tx_urb);
760 usb_free_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000763static void unlink_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 usb_kill_urb(pegasus->intr_urb);
766 usb_kill_urb(pegasus->tx_urb);
767 usb_kill_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000770static int alloc_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Petko Manolov323b3492013-04-25 22:41:50 +0000772 int res = -ENOMEM;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
775 if (!pegasus->rx_urb) {
Petko Manolov323b3492013-04-25 22:41:50 +0000776 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
779 if (!pegasus->tx_urb) {
780 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000781 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
784 if (!pegasus->intr_urb) {
785 usb_free_urb(pegasus->tx_urb);
786 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000787 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Petko Manolov323b3492013-04-25 22:41:50 +0000790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static int pegasus_open(struct net_device *net)
794{
795 pegasus_t *pegasus = netdev_priv(net);
Petko Manolov323b3492013-04-25 22:41:50 +0000796 int res=-ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000799 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
800 PEGASUS_MTU,
801 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!pegasus->rx_skb)
Petko Manolov323b3492013-04-25 22:41:50 +0000803 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 res = set_registers(pegasus, EthID, 6, net->dev_addr);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
808 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300809 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 read_bulk_callback, pegasus);
811 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700812 if (res == -ENODEV)
813 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000814 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto exit;
816 }
817
818 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
819 usb_rcvintpipe(pegasus->usb, 3),
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000820 pegasus->intr_buff, sizeof(pegasus->intr_buff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 intr_callback, pegasus, pegasus->intr_interval);
822 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700823 if (res == -ENODEV)
824 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000825 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 usb_kill_urb(pegasus->rx_urb);
827 goto exit;
828 }
Petko Manolov323b3492013-04-25 22:41:50 +0000829 res = enable_net_traffic(net, pegasus->usb);
830 if (res < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000831 netif_dbg(pegasus, ifup, net,
832 "can't enable_net_traffic() - %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 res = -EIO;
834 usb_kill_urb(pegasus->rx_urb);
835 usb_kill_urb(pegasus->intr_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto exit;
837 }
838 set_carrier(net);
839 netif_start_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000840 netif_dbg(pegasus, ifup, net, "open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 res = 0;
842exit:
843 return res;
844}
845
846static int pegasus_close(struct net_device *net)
847{
848 pegasus_t *pegasus = netdev_priv(net);
849
850 netif_stop_queue(net);
851 if (!(pegasus->flags & PEGASUS_UNPLUG))
852 disable_net_traffic(pegasus);
853 tasklet_kill(&pegasus->rx_tl);
854 unlink_all_urbs(pegasus);
855
856 return 0;
857}
858
859static void pegasus_get_drvinfo(struct net_device *dev,
860 struct ethtool_drvinfo *info)
861{
862 pegasus_t *pegasus = netdev_priv(dev);
Jiri Pirko7826d432013-01-06 00:44:26 +0000863
864 strlcpy(info->driver, driver_name, sizeof(info->driver));
865 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000866 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869/* also handles three patterns of some kind in hardware */
870#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
871
872static void
873pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
874{
875 pegasus_t *pegasus = netdev_priv(dev);
876
877 wol->supported = WAKE_MAGIC | WAKE_PHY;
878 wol->wolopts = pegasus->wolopts;
879}
880
881static int
882pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
883{
884 pegasus_t *pegasus = netdev_priv(dev);
885 u8 reg78 = 0x04;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000886 int ret;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (wol->wolopts & ~WOL_SUPPORTED)
889 return -EINVAL;
890
891 if (wol->wolopts & WAKE_MAGIC)
892 reg78 |= 0x80;
893 if (wol->wolopts & WAKE_PHY)
894 reg78 |= 0x40;
895 /* FIXME this 0x10 bit still needs to get set in the chip... */
896 if (wol->wolopts)
897 pegasus->eth_regs[0] |= 0x10;
898 else
899 pegasus->eth_regs[0] &= ~0x10;
900 pegasus->wolopts = wol->wolopts;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000901
902 ret = set_register(pegasus, WakeupControl, reg78);
903 if (!ret)
904 ret = device_set_wakeup_enable(&pegasus->usb->dev,
905 wol->wolopts);
906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static inline void pegasus_reset_wol(struct net_device *dev)
910{
911 struct ethtool_wolinfo wol;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 memset(&wol, 0, sizeof wol);
914 (void) pegasus_set_wol(dev, &wol);
915}
916
917static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100918pegasus_get_link_ksettings(struct net_device *dev,
919 struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 pegasus_t *pegasus;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100924 mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
928static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100929pegasus_set_link_ksettings(struct net_device *dev,
930 const struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 pegasus_t *pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100933 return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936static int pegasus_nway_reset(struct net_device *dev)
937{
938 pegasus_t *pegasus = netdev_priv(dev);
939 return mii_nway_restart(&pegasus->mii);
940}
941
942static u32 pegasus_get_link(struct net_device *dev)
943{
944 pegasus_t *pegasus = netdev_priv(dev);
945 return mii_link_ok(&pegasus->mii);
946}
947
948static u32 pegasus_get_msglevel(struct net_device *dev)
949{
950 pegasus_t *pegasus = netdev_priv(dev);
951 return pegasus->msg_enable;
952}
953
954static void pegasus_set_msglevel(struct net_device *dev, u32 v)
955{
956 pegasus_t *pegasus = netdev_priv(dev);
957 pegasus->msg_enable = v;
958}
959
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700960static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 .get_drvinfo = pegasus_get_drvinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 .nway_reset = pegasus_nway_reset,
963 .get_link = pegasus_get_link,
964 .get_msglevel = pegasus_get_msglevel,
965 .set_msglevel = pegasus_set_msglevel,
966 .get_wol = pegasus_get_wol,
967 .set_wol = pegasus_set_wol,
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100968 .get_link_ksettings = pegasus_get_link_ksettings,
969 .set_link_ksettings = pegasus_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970};
971
972static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
973{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000974 __u16 *data = (__u16 *) &rq->ifr_ifru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 pegasus_t *pegasus = netdev_priv(net);
976 int res;
977
978 switch (cmd) {
979 case SIOCDEVPRIVATE:
980 data[0] = pegasus->phy;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500981 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case SIOCDEVPRIVATE + 1:
983 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
984 res = 0;
985 break;
986 case SIOCDEVPRIVATE + 2:
987 if (!capable(CAP_NET_ADMIN))
988 return -EPERM;
Petko Manolov2bd64702013-04-25 22:41:36 +0000989 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 res = 0;
991 break;
992 default:
993 res = -EOPNOTSUPP;
994 }
995 return res;
996}
997
998static void pegasus_set_multicast(struct net_device *net)
999{
1000 pegasus_t *pegasus = netdev_priv(net);
1001
1002 if (net->flags & IFF_PROMISC) {
1003 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001004 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001005 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1007 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001008 netif_dbg(pegasus, link, net, "set allmulti\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
1010 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1011 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1012 }
Petko Manolov323b3492013-04-25 22:41:50 +00001013 update_eth_regs_async(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001016static __u8 mii_phy_probe(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 int i;
1019 __u16 tmp;
1020
1021 for (i = 0; i < 32; i++) {
1022 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1023 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1024 continue;
1025 else
1026 return i;
1027 }
1028
1029 return 0xff;
1030}
1031
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001032static inline void setup_pegasus_II(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 __u8 data = 0xa5;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001035
Petko Manolov4a1728a2005-11-15 09:48:23 +02001036 set_register(pegasus, Reg1d, 0);
1037 set_register(pegasus, Reg7b, 1);
Jia-Ju Bai6dff5ad2018-07-27 16:36:29 +08001038 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001040 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001042 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Petko Manolov4a1728a2005-11-15 09:48:23 +02001044 set_register(pegasus, 0x83, data);
1045 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001047 if (data == 0xa5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 pegasus->chip = 0x8513;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001049 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 pegasus->chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Petko Manolov4a1728a2005-11-15 09:48:23 +02001052 set_register(pegasus, 0x80, 0xc0);
1053 set_register(pegasus, 0x83, 0xff);
1054 set_register(pegasus, 0x84, 0x01);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001057 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001059 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
David Howellsc4028952006-11-22 14:57:56 +00001062static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
David Howellsc4028952006-11-22 14:57:56 +00001064 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 set_carrier(pegasus->net);
1066 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
Petko Manolov2f5107c2020-04-02 17:33:29 +03001067 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 CARRIER_CHECK_DELAY);
1069 }
1070}
1071
Ben Collins4f631352008-07-30 12:39:02 -07001072static int pegasus_blacklisted(struct usb_device *udev)
1073{
1074 struct usb_device_descriptor *udd = &udev->descriptor;
1075
1076 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1077 * dongle which happens to have the same ID.
1078 */
Michael Buesche3453f62009-06-18 07:03:47 +00001079 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1080 (udd->idProduct == cpu_to_le16(0x0121)) &&
Ben Collins4f631352008-07-30 12:39:02 -07001081 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1082 (udd->bDeviceProtocol == 1))
1083 return 1;
1084
1085 return 0;
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static int pegasus_probe(struct usb_interface *intf,
1089 const struct usb_device_id *id)
1090{
1091 struct usb_device *dev = interface_to_usbdev(intf);
1092 struct net_device *net;
1093 pegasus_t *pegasus;
1094 int dev_index = id - pegasus_ids;
1095 int res = -ENOMEM;
1096
David Brownellcda28362008-11-16 00:36:08 -08001097 if (pegasus_blacklisted(dev))
1098 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 net = alloc_etherdev(sizeof(struct pegasus));
Joe Perches41de8d42012-01-29 13:47:52 +00001101 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 pegasus->dev_index = dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Petko Manolov323b3492013-04-25 22:41:50 +00001107 res = alloc_urbs(pegasus);
1108 if (res < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1110 goto out1;
1111 }
1112
1113 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1114
David Howellsc4028952006-11-22 14:57:56 +00001115 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 pegasus->intf = intf;
1118 pegasus->usb = dev;
1119 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001120
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001123 net->netdev_ops = &pegasus_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001124 net->ethtool_ops = &ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 pegasus->mii.dev = net;
1126 pegasus->mii.mdio_read = mdio_read;
1127 pegasus->mii.mdio_write = mdio_write;
1128 pegasus->mii.phy_id_mask = 0x1f;
1129 pegasus->mii.reg_num_mask = 0x1f;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001130 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1132
1133 pegasus->features = usb_dev_id[dev_index].private;
1134 get_interrupt_interval(pegasus);
1135 if (reset_mac(pegasus)) {
1136 dev_err(&intf->dev, "can't reset MAC\n");
1137 res = -EIO;
1138 goto out2;
1139 }
1140 set_ethernet_addr(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (pegasus->features & PEGASUS_II) {
1142 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1143 setup_pegasus_II(pegasus);
1144 }
1145 pegasus->phy = mii_phy_probe(pegasus);
1146 if (pegasus->phy == 0xff) {
1147 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1148 pegasus->phy = 1;
1149 }
1150 pegasus->mii.phy_id = pegasus->phy;
1151 usb_set_intfdata(intf, pegasus);
1152 SET_NETDEV_DEV(net, &intf->dev);
1153 pegasus_reset_wol(net);
1154 res = register_netdev(net);
1155 if (res)
1156 goto out3;
Petko Manolov2f5107c2020-04-02 17:33:29 +03001157 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Petko Manolov323b3492013-04-25 22:41:50 +00001158 CARRIER_CHECK_DELAY);
1159 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1160 usb_dev_id[dev_index].name, net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162
1163out3:
1164 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165out2:
1166 free_all_urbs(pegasus);
1167out1:
1168 free_netdev(net);
1169out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return res;
1171}
1172
1173static void pegasus_disconnect(struct usb_interface *intf)
1174{
1175 struct pegasus *pegasus = usb_get_intfdata(intf);
1176
1177 usb_set_intfdata(intf, NULL);
1178 if (!pegasus) {
1179 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1180 return;
1181 }
1182
1183 pegasus->flags |= PEGASUS_UNPLUG;
Petko Manolov2f5107c2020-04-02 17:33:29 +03001184 cancel_delayed_work_sync(&pegasus->carrier_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 unregister_netdev(pegasus->net);
Kevin Vigora85a46f2005-09-22 00:49:24 -07001186 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 free_all_urbs(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001188 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001190 pegasus->rx_skb = NULL;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 free_netdev(pegasus->net);
1193}
1194
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001195static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct pegasus *pegasus = usb_get_intfdata(intf);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001198
1199 netif_device_detach(pegasus->net);
Petko Manolov2f5107c2020-04-02 17:33:29 +03001200 cancel_delayed_work_sync(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001201 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001202 usb_kill_urb(pegasus->rx_urb);
1203 usb_kill_urb(pegasus->intr_urb);
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001208static int pegasus_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct pegasus *pegasus = usb_get_intfdata(intf);
1211
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001212 netif_device_attach(pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001213 if (netif_running(pegasus->net)) {
1214 pegasus->rx_urb->status = 0;
1215 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001216 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001217
1218 pegasus->intr_urb->status = 0;
1219 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001220 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001221 }
Petko Manolov2f5107c2020-04-02 17:33:29 +03001222 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
David Brownell7e713b82006-05-01 14:02:45 -07001223 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 0;
1225}
1226
Oliver Neukum8cb89572009-01-08 11:22:25 -08001227static const struct net_device_ops pegasus_netdev_ops = {
1228 .ndo_open = pegasus_open,
1229 .ndo_stop = pegasus_close,
1230 .ndo_do_ioctl = pegasus_ioctl,
1231 .ndo_start_xmit = pegasus_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001232 .ndo_set_rx_mode = pegasus_set_multicast,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001233 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001234 .ndo_set_mac_address = eth_mac_addr,
1235 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001236};
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static struct usb_driver pegasus_driver = {
1239 .name = driver_name,
1240 .probe = pegasus_probe,
1241 .disconnect = pegasus_disconnect,
1242 .id_table = pegasus_ids,
1243 .suspend = pegasus_suspend,
1244 .resume = pegasus_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001245 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246};
1247
David Brownellcda28362008-11-16 00:36:08 -08001248static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001249{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001250 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1251 char *token, *name = NULL;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001252
1253 if ((token = strsep(&id, ":")) != NULL)
1254 name = token;
1255 /* name now points to a null terminated string*/
1256 if ((token = strsep(&id, ":")) != NULL)
1257 vendor_id = simple_strtoul(token, NULL, 16);
1258 if ((token = strsep(&id, ":")) != NULL)
1259 device_id = simple_strtoul(token, NULL, 16);
1260 flags = simple_strtoul(id, NULL, 16);
1261 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001262 driver_name, name, vendor_id, device_id, flags);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001263
1264 if (vendor_id > 0x10000 || vendor_id == 0)
1265 return;
1266 if (device_id > 0x10000 || device_id == 0)
1267 return;
1268
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001269 for (i = 0; usb_dev_id[i].name; i++);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001270 usb_dev_id[i].name = name;
1271 usb_dev_id[i].vendor = vendor_id;
1272 usb_dev_id[i].device = device_id;
1273 usb_dev_id[i].private = flags;
1274 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1275 pegasus_ids[i].idVendor = vendor_id;
1276 pegasus_ids[i].idProduct = device_id;
1277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279static int __init pegasus_init(void)
1280{
1281 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001282 if (devid)
1283 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return usb_register(&pegasus_driver);
1285}
1286
1287static void __exit pegasus_exit(void)
1288{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 usb_deregister(&pegasus_driver);
1290}
1291
1292module_init(pegasus_init);
1293module_exit(pegasus_exit);