blob: 9a907182569cffe21b8c8bedb903d676daa05684 [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
Petko Manolovf30e25a2020-10-02 10:56:04 +0300332static inline int get_node_id(pegasus_t *pegasus, u8 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Petko Manolovf30e25a2020-10-02 10:56:04 +0300334 int i, ret;
335 u16 w16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 for (i = 0; i < 3; i++) {
Petko Manolovf30e25a2020-10-02 10:56:04 +0300338 ret = read_eprom_word(pegasus, i, &w16);
339 if (ret < 0)
340 return ret;
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700341 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Petko Manolovf30e25a2020-10-02 10:56:04 +0300343
344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000347static void set_ethernet_addr(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Petko Manolovf30e25a2020-10-02 10:56:04 +0300349 int ret;
350 u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Petko Manolov37cf3472006-09-27 14:25:37 -0700352 if (pegasus->features & PEGASUS_II) {
Petko Manolovf30e25a2020-10-02 10:56:04 +0300353 ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
354 if (ret < 0)
355 goto err;
Petko Manolov37cf3472006-09-27 14:25:37 -0700356 } else {
Petko Manolovf30e25a2020-10-02 10:56:04 +0300357 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 Manolov37cf3472006-09-27 14:25:37 -0700363 }
Petko Manolovf30e25a2020-10-02 10:56:04 +0300364
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000365 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
Petko Manolovf30e25a2020-10-02 10:56:04 +0300366
367 return;
368err:
369 eth_hw_addr_random(pegasus->net);
370 dev_info(&pegasus->intf->dev, "software assigned MAC address.\n");
371
372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000375static inline int reset_mac(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 __u8 data = 0x8;
378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Petko Manolov4a1728a2005-11-15 09:48:23 +0200380 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200382 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (~data & 0x08) {
Dan Carpenter681f1622011-12-23 00:44:36 +0000384 if (loopback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200387 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200389 set_register(pegasus, Gpio1, 0x26);
390 set_register(pegasus, Gpio0, pegasus->features);
391 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
393 }
394 }
395 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200396 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
399 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200400 set_register(pegasus, Gpio0, 0x24);
401 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
404 __u16 auxmode;
405 read_mii_word(pegasus, 3, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000406 auxmode |= 4;
407 write_mii_word(pegasus, 3, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 return 0;
411}
412
413static 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 Manolov1a8deec2016-04-27 14:24:50 +0300421 data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 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 Carpenter681f1622011-12-23 00:44:36 +0000429 data[2] = loopback ? 0x09 : 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000431 memcpy(pegasus->eth_regs, data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ret = set_registers(pegasus, EthCtrl0, 3, data);
433
434 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100435 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
437 u16 auxmode;
438 read_mii_word(pegasus, 0, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000439 auxmode |= 4;
440 write_mii_word(pegasus, 0, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Petko Manolov4a1728a2005-11-15 09:48:23 +0200443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
David Howells7d12e782006-10-05 14:55:46 +0100446static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 pegasus_t *pegasus = urb->context;
449 struct net_device *net;
450 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800451 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 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 Neukumc94cb312008-12-18 23:00:59 -0800462 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 case 0:
464 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700465 case -ETIME:
Joe Perchesa475f602010-02-17 10:30:24 +0000466 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 pegasus->flags &= ~PEGASUS_RX_BUSY;
468 break;
469 case -EPIPE: /* stall, or disconnect from TT */
470 /* FIXME schedule work to clear the halt */
Joe Perchesa475f602010-02-17 10:30:24 +0000471 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
473 case -ENOENT:
474 case -ECONNRESET:
475 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000476 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return;
478 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000479 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto goon;
481 }
482
xypron.glpk@gmx.debbf178e2016-05-18 20:40:51 +0200483 if (count < 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto goon;
485
486 rx_status = buf[count - 2];
487 if (rx_status & 0x1e) {
Joe Perchesa475f602010-02-17 10:30:24 +0000488 netif_dbg(pegasus, rx_err, net,
489 "RX packet error %x\n", rx_status);
Tobias Klauser82d82932017-04-07 10:17:39 +0200490 net->stats.rx_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000491 if (rx_status & 0x06) /* long or runt */
Tobias Klauser82d82932017-04-07 10:17:39 +0200492 net->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (rx_status & 0x08)
Tobias Klauser82d82932017-04-07 10:17:39 +0200494 net->stats.rx_crc_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000495 if (rx_status & 0x10) /* extra bits */
Tobias Klauser82d82932017-04-07 10:17:39 +0200496 net->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 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 Manolov1a8deec2016-04-27 14:24:50 +0300507 pkt_len -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700511 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700518 * 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 Klauser82d82932017-04-07 10:17:39 +0200524 net->stats.rx_packets++;
525 net->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (pegasus->flags & PEGASUS_UNPLUG)
528 return;
529
Petko Manolov313a58e2013-04-25 22:41:21 +0000530 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
531 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (pegasus->rx_skb == NULL)
534 goto tl_sched;
535goon:
536 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
537 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300538 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700540 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 Torvalds1da177e2005-04-16 15:20:36 -0700544 pegasus->flags |= PEGASUS_RX_URB_FAIL;
545 goto tl_sched;
546 } else {
547 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
548 }
549
550 return;
551
552tl_sched:
553 tasklet_schedule(&pegasus->rx_tl);
554}
555
Emil Renner Berthing23a64c52021-01-31 00:47:28 +0100556static void rx_fixup(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Emil Renner Berthing23a64c52021-01-31 00:47:28 +0100558 pegasus_t *pegasus = from_tasklet(pegasus, t, rx_tl);
David Brownell955a2602006-05-26 10:17:03 -0700559 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (pegasus->flags & PEGASUS_UNPLUG)
562 return;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
565 if (pegasus->rx_skb)
566 goto try_again;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000567 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000568 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
569 PEGASUS_MTU,
570 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (pegasus->rx_skb == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000572 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 tasklet_schedule(&pegasus->rx_tl);
Petko Manolov313a58e2013-04-25 22:41:21 +0000574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
577 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300578 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 read_bulk_callback, pegasus);
580try_again:
David Brownell955a2602006-05-26 10:17:03 -0700581 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
582 if (status == -ENODEV)
583 netif_device_detach(pegasus->net);
584 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pegasus->flags |= PEGASUS_RX_URB_FAIL;
586 tasklet_schedule(&pegasus->rx_tl);
587 } else {
588 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
David Howells7d12e782006-10-05 14:55:46 +0100592static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800595 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800596 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (!pegasus)
599 return;
600
Micah Gruber93519822007-07-23 16:05:52 +0800601 net = pegasus->net;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!netif_device_present(net) || !netif_running(net))
604 return;
605
Oliver Neukumc94cb312008-12-18 23:00:59 -0800606 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case -EPIPE:
608 /* FIXME schedule_work() to clear the tx halt */
609 netif_stop_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000610 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return;
612 case -ENOENT:
613 case -ECONNRESET:
614 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000615 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
617 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000618 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500619 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 case 0:
621 break;
622 }
623
Florian Westphal860e9532016-05-03 16:33:13 +0200624 netif_trans_update(net); /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 netif_wake_queue(net);
626}
627
David Howells7d12e782006-10-05 14:55:46 +0100628static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 pegasus_t *pegasus = urb->context;
631 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800632 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (!pegasus)
635 return;
636 net = pegasus->net;
637
Oliver Neukumc94cb312008-12-18 23:00:59 -0800638 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 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 Perchesa475f602010-02-17 10:30:24 +0000649 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 if (urb->actual_length >= 6) {
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000653 u8 *d = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* byte 0 == tx_status1, reg 2B */
656 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
657 |LATE_COL|JABBER_TIMEOUT)) {
Tobias Klauser82d82932017-04-07 10:17:39 +0200658 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (d[0] & TX_UNDERRUN)
Tobias Klauser82d82932017-04-07 10:17:39 +0200660 net->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
Tobias Klauser82d82932017-04-07 10:17:39 +0200662 net->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (d[0] & LATE_COL)
Tobias Klauser82d82932017-04-07 10:17:39 +0200664 net->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
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 Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
Tobias Klauser82d82932017-04-07 10:17:39 +0200673 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Oliver Neukumc94cb312008-12-18 23:00:59 -0800676 res = usb_submit_urb(urb, GFP_ATOMIC);
677 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700678 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000679 if (res)
680 netif_err(pegasus, timer, net,
681 "can't resubmit interrupt urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -0500684static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 pegasus_t *pegasus = netdev_priv(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000687 netif_warn(pegasus, timer, net, "tx timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 usb_unlink_urb(pegasus->tx_urb);
Tobias Klauser82d82932017-04-07 10:17:39 +0200689 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000692static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
693 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
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 Melod626f622007-03-27 18:55:52 -0300703 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 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 Perchesa475f602010-02-17 10:30:24 +0000709 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 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 Neukum9dd014e2009-04-17 01:40:19 -0700715 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700716 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
718 default:
Tobias Klauser82d82932017-04-07 10:17:39 +0200719 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 netif_start_queue(net);
721 }
722 } else {
Tobias Klauser82d82932017-04-07 10:17:39 +0200723 net->stats.tx_packets++;
724 net->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 dev_kfree_skb(skb);
727
Patrick McHardy6ed10652009-06-23 06:03:08 +0000728 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000731static inline void disable_net_traffic(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Michael Buesche3453f62009-06-18 07:03:47 +0000733 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Michael Buesche3453f62009-06-18 07:03:47 +0000735 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000738static inline void get_interrupt_interval(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Michael Buesche3453f62009-06-18 07:03:47 +0000740 u16 data;
741 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Michael Buesche3453f62009-06-18 07:03:47 +0000743 read_eprom_word(pegasus, 4, &data);
744 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700745 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000746 if (interval < 0x80) {
Joe Perchesa475f602010-02-17 10:30:24 +0000747 netif_info(pegasus, timer, pegasus->net,
748 "intr interval changed from %ums to %ums\n",
749 interval, 0x80);
Michael Buesche3453f62009-06-18 07:03:47 +0000750 interval = 0x80;
751 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700752#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000753 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Michael Buesche3453f62009-06-18 07:03:47 +0000757 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760static void set_carrier(struct net_device *net)
761{
762 pegasus_t *pegasus = netdev_priv(net);
763 u16 tmp;
764
Dan Williamsc43c49b2007-04-24 10:20:06 -0400765 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (tmp & BMSR_LSTATUS)
769 netif_carrier_on(net);
770 else
771 netif_carrier_off(net);
772}
773
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000774static void free_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 usb_free_urb(pegasus->intr_urb);
777 usb_free_urb(pegasus->tx_urb);
778 usb_free_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000781static void unlink_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 usb_kill_urb(pegasus->intr_urb);
784 usb_kill_urb(pegasus->tx_urb);
785 usb_kill_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000788static int alloc_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Petko Manolov323b3492013-04-25 22:41:50 +0000790 int res = -ENOMEM;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
793 if (!pegasus->rx_urb) {
Petko Manolov323b3492013-04-25 22:41:50 +0000794 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
797 if (!pegasus->tx_urb) {
798 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000799 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
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 Manolov323b3492013-04-25 22:41:50 +0000805 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Petko Manolov323b3492013-04-25 22:41:50 +0000808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811static int pegasus_open(struct net_device *net)
812{
813 pegasus_t *pegasus = netdev_priv(net);
Petko Manolov323b3492013-04-25 22:41:50 +0000814 int res=-ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000817 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
818 PEGASUS_MTU,
819 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!pegasus->rx_skb)
Petko Manolov323b3492013-04-25 22:41:50 +0000821 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 res = set_registers(pegasus, EthID, 6, net->dev_addr);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
826 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300827 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 read_bulk_callback, pegasus);
829 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700830 if (res == -ENODEV)
831 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000832 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto exit;
834 }
835
836 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
837 usb_rcvintpipe(pegasus->usb, 3),
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000838 pegasus->intr_buff, sizeof(pegasus->intr_buff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 intr_callback, pegasus, pegasus->intr_interval);
840 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700841 if (res == -ENODEV)
842 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000843 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 usb_kill_urb(pegasus->rx_urb);
845 goto exit;
846 }
Petko Manolov323b3492013-04-25 22:41:50 +0000847 res = enable_net_traffic(net, pegasus->usb);
848 if (res < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000849 netif_dbg(pegasus, ifup, net,
850 "can't enable_net_traffic() - %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 res = -EIO;
852 usb_kill_urb(pegasus->rx_urb);
853 usb_kill_urb(pegasus->intr_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto exit;
855 }
856 set_carrier(net);
857 netif_start_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000858 netif_dbg(pegasus, ifup, net, "open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 res = 0;
860exit:
861 return res;
862}
863
864static 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
877static void pegasus_get_drvinfo(struct net_device *dev,
878 struct ethtool_drvinfo *info)
879{
880 pegasus_t *pegasus = netdev_priv(dev);
Jiri Pirko7826d432013-01-06 00:44:26 +0000881
882 strlcpy(info->driver, driver_name, sizeof(info->driver));
883 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000884 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/* also handles three patterns of some kind in hardware */
888#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
889
890static void
891pegasus_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
899static int
900pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
901{
902 pegasus_t *pegasus = netdev_priv(dev);
903 u8 reg78 = 0x04;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000904 int ret;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 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 Lei4fbc5b22013-01-19 01:32:01 +0000919
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 Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927static inline void pegasus_reset_wol(struct net_device *dev)
928{
929 struct ethtool_wolinfo wol;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 memset(&wol, 0, sizeof wol);
932 (void) pegasus_set_wol(dev, &wol);
933}
934
935static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100936pegasus_get_link_ksettings(struct net_device *dev,
937 struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 pegasus_t *pegasus;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100942 mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}
945
946static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100947pegasus_set_link_ksettings(struct net_device *dev,
948 const struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 pegasus_t *pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100951 return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static 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
960static 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
966static u32 pegasus_get_msglevel(struct net_device *dev)
967{
968 pegasus_t *pegasus = netdev_priv(dev);
969 return pegasus->msg_enable;
970}
971
972static 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 Hemminger0fc0b732009-09-02 01:03:33 -0700978static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 .get_drvinfo = pegasus_get_drvinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 .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 Reynes71dbc3412017-03-17 23:34:04 +0100986 .get_link_ksettings = pegasus_get_link_ksettings,
987 .set_link_ksettings = pegasus_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988};
989
990static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
991{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000992 __u16 *data = (__u16 *) &rq->ifr_ifru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 pegasus_t *pegasus = netdev_priv(net);
994 int res;
995
996 switch (cmd) {
997 case SIOCDEVPRIVATE:
998 data[0] = pegasus->phy;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500999 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 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 Manolov2bd64702013-04-25 22:41:36 +00001007 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 res = 0;
1009 break;
1010 default:
1011 res = -EOPNOTSUPP;
1012 }
1013 return res;
1014}
1015
1016static 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 Perchesa475f602010-02-17 10:30:24 +00001022 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001023 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1025 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001026 netif_dbg(pegasus, link, net, "set allmulti\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
1028 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1029 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1030 }
Petko Manolov323b3492013-04-25 22:41:50 +00001031 update_eth_regs_async(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001034static __u8 mii_phy_probe(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
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 Kaiser5a9dbfe2010-06-26 06:58:54 +00001050static inline void setup_pegasus_II(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 __u8 data = 0xa5;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001053
Petko Manolov4a1728a2005-11-15 09:48:23 +02001054 set_register(pegasus, Reg1d, 0);
1055 set_register(pegasus, Reg7b, 1);
Jia-Ju Bai6dff5ad2018-07-27 16:36:29 +08001056 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001058 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001060 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Petko Manolov4a1728a2005-11-15 09:48:23 +02001062 set_register(pegasus, 0x83, data);
1063 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001065 if (data == 0xa5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 pegasus->chip = 0x8513;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001067 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 pegasus->chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Petko Manolov4a1728a2005-11-15 09:48:23 +02001070 set_register(pegasus, 0x80, 0xc0);
1071 set_register(pegasus, 0x83, 0xff);
1072 set_register(pegasus, 0x84, 0x01);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001075 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001077 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
David Howellsc4028952006-11-22 14:57:56 +00001080static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
David Howellsc4028952006-11-22 14:57:56 +00001082 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 set_carrier(pegasus->net);
1084 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
Petko Manolov2f5107c2020-04-02 17:33:29 +03001085 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 CARRIER_CHECK_DELAY);
1087 }
1088}
1089
Ben Collins4f631352008-07-30 12:39:02 -07001090static 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 Buesche3453f62009-06-18 07:03:47 +00001097 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1098 (udd->idProduct == cpu_to_le16(0x0121)) &&
Ben Collins4f631352008-07-30 12:39:02 -07001099 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1100 (udd->bDeviceProtocol == 1))
1101 return 1;
1102
1103 return 0;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static 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 Brownellcda28362008-11-16 00:36:08 -08001115 if (pegasus_blacklisted(dev))
1116 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 net = alloc_etherdev(sizeof(struct pegasus));
Joe Perches41de8d42012-01-29 13:47:52 +00001119 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 pegasus->dev_index = dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Petko Manolov323b3492013-04-25 22:41:50 +00001125 res = alloc_urbs(pegasus);
1126 if (res < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1128 goto out1;
1129 }
1130
Emil Renner Berthing23a64c52021-01-31 00:47:28 +01001131 tasklet_setup(&pegasus->rx_tl, rx_fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David Howellsc4028952006-11-22 14:57:56 +00001133 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 pegasus->intf = intf;
1136 pegasus->usb = dev;
1137 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001138
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001141 net->netdev_ops = &pegasus_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001142 net->ethtool_ops = &ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 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 Kaiser5a9dbfe2010-06-26 06:58:54 +00001148 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 | 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 Torvalds1da177e2005-04-16 15:20:36 -07001159 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 Manolov2f5107c2020-04-02 17:33:29 +03001175 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Petko Manolov323b3492013-04-25 22:41:50 +00001176 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 Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180
1181out3:
1182 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183out2:
1184 free_all_urbs(pegasus);
1185out1:
1186 free_netdev(net);
1187out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return res;
1189}
1190
1191static 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 Manolov2f5107c2020-04-02 17:33:29 +03001202 cancel_delayed_work_sync(&pegasus->carrier_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unregister_netdev(pegasus->net);
Kevin Vigora85a46f2005-09-22 00:49:24 -07001204 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 free_all_urbs(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001206 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001208 pegasus->rx_skb = NULL;
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 free_netdev(pegasus->net);
1211}
1212
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001213static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct pegasus *pegasus = usb_get_intfdata(intf);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001216
1217 netif_device_detach(pegasus->net);
Petko Manolov2f5107c2020-04-02 17:33:29 +03001218 cancel_delayed_work_sync(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001219 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001220 usb_kill_urb(pegasus->rx_urb);
1221 usb_kill_urb(pegasus->intr_urb);
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224}
1225
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001226static int pegasus_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct pegasus *pegasus = usb_get_intfdata(intf);
1229
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001230 netif_device_attach(pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001231 if (netif_running(pegasus->net)) {
1232 pegasus->rx_urb->status = 0;
1233 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001234 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001235
1236 pegasus->intr_urb->status = 0;
1237 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001238 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001239 }
Petko Manolov2f5107c2020-04-02 17:33:29 +03001240 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
David Brownell7e713b82006-05-01 14:02:45 -07001241 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Oliver Neukum8cb89572009-01-08 11:22:25 -08001245static 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 Pirkoafc4b132011-08-16 06:29:01 +00001250 .ndo_set_rx_mode = pegasus_set_multicast,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001251 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001252 .ndo_set_mac_address = eth_mac_addr,
1253 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001254};
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256static 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 Sharpe1f12eb2012-04-23 10:08:51 -07001263 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264};
1265
David Brownellcda28362008-11-16 00:36:08 -08001266static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001267{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001268 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1269 char *token, *name = NULL;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001270
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 Kaiser5a9dbfe2010-06-26 06:58:54 +00001280 driver_name, name, vendor_id, device_id, flags);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001281
1282 if (vendor_id > 0x10000 || vendor_id == 0)
1283 return;
1284 if (device_id > 0x10000 || device_id == 0)
1285 return;
1286
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001287 for (i = 0; usb_dev_id[i].name; i++);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001288 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 Torvalds1da177e2005-04-16 15:20:36 -07001297static int __init pegasus_init(void)
1298{
1299 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001300 if (devid)
1301 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return usb_register(&pegasus_driver);
1303}
1304
1305static void __exit pegasus_exit(void)
1306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 usb_deregister(&pegasus_driver);
1308}
1309
1310module_init(pegasus_init);
1311module_exit(pegasus_exit);