blob: 0ef7e1f443e333ffd49cab9161ccb55543a7a20e [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{
Ben Hutchings55935232017-02-04 16:56:03 +0000127 u8 *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ben Hutchings55935232017-02-04 16:56:03 +0000130 buf = kmalloc(size, GFP_NOIO);
131 if (!buf)
132 return -ENOMEM;
133
Petko Manolov323b3492013-04-25 22:41:50 +0000134 ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
135 PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
Ben Hutchings55935232017-02-04 16:56:03 +0000136 indx, buf, size, 1000);
Petko Manolov323b3492013-04-25 22:41:50 +0000137 if (ret < 0)
138 netif_dbg(pegasus, drv, pegasus->net,
139 "%s returned %d\n", __func__, ret);
Ben Hutchings55935232017-02-04 16:56:03 +0000140 else if (ret <= size)
141 memcpy(data, buf, ret);
142 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return ret;
144}
145
Ben Hutchings55935232017-02-04 16:56:03 +0000146static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
147 const void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Ben Hutchings55935232017-02-04 16:56:03 +0000149 u8 *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ben Hutchings55935232017-02-04 16:56:03 +0000152 buf = kmemdup(data, size, GFP_NOIO);
153 if (!buf)
154 return -ENOMEM;
155
Petko Manolov323b3492013-04-25 22:41:50 +0000156 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
157 PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
Ben Hutchings55935232017-02-04 16:56:03 +0000158 indx, buf, size, 100);
Petko Manolov323b3492013-04-25 22:41:50 +0000159 if (ret < 0)
160 netif_dbg(pegasus, drv, pegasus->net,
161 "%s returned %d\n", __func__, ret);
Ben Hutchings55935232017-02-04 16:56:03 +0000162 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return ret;
164}
165
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000166static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Ben Hutchings55935232017-02-04 16:56:03 +0000168 u8 *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Ben Hutchings55935232017-02-04 16:56:03 +0000171 buf = kmemdup(&data, 1, GFP_NOIO);
172 if (!buf)
173 return -ENOMEM;
174
Petko Manolov323b3492013-04-25 22:41:50 +0000175 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
176 PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
Ben Hutchings55935232017-02-04 16:56:03 +0000177 indx, buf, 1, 1000);
Petko Manolov323b3492013-04-25 22:41:50 +0000178 if (ret < 0)
179 netif_dbg(pegasus, drv, pegasus->net,
180 "%s returned %d\n", __func__, ret);
Ben Hutchings55935232017-02-04 16:56:03 +0000181 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ret;
183}
184
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000185static int update_eth_regs_async(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Petko Manolov323b3492013-04-25 22:41:50 +0000187 int ret = -ENOMEM;
188 struct urb *async_urb;
189 struct usb_ctrlrequest *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Petko Manolov323b3492013-04-25 22:41:50 +0000191 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
192 if (req == NULL)
193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Petko Manolov323b3492013-04-25 22:41:50 +0000195 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
196 if (async_urb == NULL) {
197 kfree(req);
198 return ret;
199 }
200 req->bRequestType = PEGASUS_REQT_WRITE;
201 req->bRequest = PEGASUS_REQ_SET_REGS;
202 req->wValue = cpu_to_le16(0);
203 req->wIndex = cpu_to_le16(EthCtrl0);
204 req->wLength = cpu_to_le16(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Petko Manolov323b3492013-04-25 22:41:50 +0000206 usb_fill_control_urb(async_urb, pegasus->usb,
207 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
208 pegasus->eth_regs, 3, async_ctrl_callback, req);
209
210 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
211 if (ret) {
David Brownell955a2602006-05-26 10:17:03 -0700212 if (ret == -ENODEV)
213 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000214 netif_err(pegasus, drv, pegasus->net,
Petko Manolov323b3492013-04-25 22:41:50 +0000215 "%s returned %d\n", __func__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ret;
218}
219
Petko Manolov2bd64702013-04-25 22:41:36 +0000220static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int i;
223 __u8 data[4] = { phy, 0, 0, indx };
224 __le16 regdi;
Petko Manolov2bd64702013-04-25 22:41:36 +0000225 int ret = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Petko Manolov2bd64702013-04-25 22:41:36 +0000227 if (cmd & PHY_WRITE) {
228 __le16 *t = (__le16 *) & data[1];
229 *t = cpu_to_le16(*regd);
230 }
231 set_register(p, PhyCtrl, 0);
232 set_registers(p, PhyAddr, sizeof(data), data);
233 set_register(p, PhyCtrl, (indx | cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov2bd64702013-04-25 22:41:36 +0000235 ret = get_registers(p, PhyCtrl, 1, data);
236 if (ret < 0)
David Brownell7e713b82006-05-01 14:02:45 -0700237 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (data[0] & PHY_DONE)
239 break;
240 }
Joe Perchesa475f602010-02-17 10:30:24 +0000241 if (i >= REG_TIMEOUT)
242 goto fail;
Petko Manolov2bd64702013-04-25 22:41:36 +0000243 if (cmd & PHY_READ) {
244 ret = get_registers(p, PhyData, 2, &regdi);
245 *regd = le16_to_cpu(regdi);
246 return ret;
247 }
248 return 0;
David Brownell7e713b82006-05-01 14:02:45 -0700249fail:
Petko Manolov2bd64702013-04-25 22:41:36 +0000250 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Petko Manolov2bd64702013-04-25 22:41:36 +0000254/* Returns non-negative int on success, error on failure */
255static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
256{
257 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
258}
259
260/* Returns zero on success, error on failure */
261static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
262{
263 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int mdio_read(struct net_device *dev, int phy_id, int loc)
267{
Joe Perches8739cfe2010-11-15 11:12:29 +0000268 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 u16 res;
270
271 read_mii_word(pegasus, phy_id, loc, &res);
272 return (int)res;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
276{
Joe Perches8739cfe2010-11-15 11:12:29 +0000277 pegasus_t *pegasus = netdev_priv(dev);
Dan Carpenter3d64fc72013-05-02 20:44:20 +0000278 u16 data = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dan Carpenter3d64fc72013-05-02 20:44:20 +0000280 write_mii_word(pegasus, phy_id, loc, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000283static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int i;
Denis Kirjanov224c0492019-07-30 15:13:57 +0200286 __u8 tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __le16 retdatai;
288 int ret;
289
Petko Manolov4a1728a2005-11-15 09:48:23 +0200290 set_register(pegasus, EpromCtrl, 0);
291 set_register(pegasus, EpromOffset, index);
292 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 for (i = 0; i < REG_TIMEOUT; i++) {
295 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
296 if (tmp & EPROM_DONE)
297 break;
David Brownell7e713b82006-05-01 14:02:45 -0700298 if (ret == -ESHUTDOWN)
299 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Joe Perchesa475f602010-02-17 10:30:24 +0000301 if (i >= REG_TIMEOUT)
302 goto fail;
303
304 ret = get_registers(pegasus, EpromData, 2, &retdatai);
305 *retdata = le16_to_cpu(retdatai);
306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Brownell7e713b82006-05-01 14:02:45 -0700308fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000309 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200310 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313#ifdef PEGASUS_WRITE_EEPROM
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000314static inline void enable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Petko Manolov4a1728a2005-11-15 09:48:23 +0200318 get_registers(pegasus, EthCtrl2, 1, &tmp);
319 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000322static inline void disable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Petko Manolov4a1728a2005-11-15 09:48:23 +0200326 get_registers(pegasus, EthCtrl2, 1, &tmp);
327 set_register(pegasus, EpromCtrl, 0);
328 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000331static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int i;
334 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
335 int ret;
Michael Buesche3453f62009-06-18 07:03:47 +0000336 __le16 le_data = cpu_to_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Petko Manolov4a1728a2005-11-15 09:48:23 +0200338 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200340 set_register(pegasus, EpromOffset, index);
Michael Buesche3453f62009-06-18 07:03:47 +0000341 set_registers(pegasus, EpromData, 2, &le_data);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200342 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 for (i = 0; i < REG_TIMEOUT; i++) {
345 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700346 if (ret == -ESHUTDOWN)
347 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (tmp & EPROM_DONE)
349 break;
350 }
351 disable_eprom_write(pegasus);
Joe Perchesa475f602010-02-17 10:30:24 +0000352 if (i >= REG_TIMEOUT)
353 goto fail;
354
355 return ret;
356
David Brownell7e713b82006-05-01 14:02:45 -0700357fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000358 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200359 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361#endif /* PEGASUS_WRITE_EEPROM */
362
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000363static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 int i;
366 __u16 w16;
367
368 for (i = 0; i < 3; i++) {
369 read_eprom_word(pegasus, i, &w16);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700370 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372}
373
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000374static void set_ethernet_addr(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Petko Manolov37cf3472006-09-27 14:25:37 -0700378 if (pegasus->features & PEGASUS_II) {
379 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
380 } else {
381 get_node_id(pegasus, node_id);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000382 set_registers(pegasus, EthID, sizeof(node_id), node_id);
Petko Manolov37cf3472006-09-27 14:25:37 -0700383 }
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000384 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000387static inline int reset_mac(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 __u8 data = 0x8;
390 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Petko Manolov4a1728a2005-11-15 09:48:23 +0200392 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200394 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (~data & 0x08) {
Dan Carpenter681f1622011-12-23 00:44:36 +0000396 if (loopback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200399 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200401 set_register(pegasus, Gpio1, 0x26);
402 set_register(pegasus, Gpio0, pegasus->features);
403 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 }
406 }
407 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200408 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
411 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200412 set_register(pegasus, Gpio0, 0x24);
413 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
416 __u16 auxmode;
417 read_mii_word(pegasus, 3, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000418 auxmode |= 4;
419 write_mii_word(pegasus, 3, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 return 0;
423}
424
425static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
426{
427 __u16 linkpart;
428 __u8 data[4];
429 pegasus_t *pegasus = netdev_priv(dev);
430 int ret;
431
432 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
Petko Manolov1a8deec2016-04-27 14:24:50 +0300433 data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 data[1] = 0;
435 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
436 data[1] |= 0x20; /* set full duplex */
437 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
438 data[1] |= 0x10; /* set 100 Mbps */
439 if (mii_mode)
440 data[1] = 0;
Dan Carpenter681f1622011-12-23 00:44:36 +0000441 data[2] = loopback ? 0x09 : 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000443 memcpy(pegasus->eth_regs, data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ret = set_registers(pegasus, EthCtrl0, 3, data);
445
446 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100447 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
449 u16 auxmode;
450 read_mii_word(pegasus, 0, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000451 auxmode |= 4;
452 write_mii_word(pegasus, 0, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
Petko Manolov4a1728a2005-11-15 09:48:23 +0200455 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David Howells7d12e782006-10-05 14:55:46 +0100458static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 pegasus_t *pegasus = urb->context;
461 struct net_device *net;
462 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800463 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 u8 *buf = urb->transfer_buffer;
465 __u16 pkt_len;
466
467 if (!pegasus)
468 return;
469
470 net = pegasus->net;
471 if (!netif_device_present(net) || !netif_running(net))
472 return;
473
Oliver Neukumc94cb312008-12-18 23:00:59 -0800474 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 case 0:
476 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700477 case -ETIME:
Joe Perchesa475f602010-02-17 10:30:24 +0000478 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 pegasus->flags &= ~PEGASUS_RX_BUSY;
480 break;
481 case -EPIPE: /* stall, or disconnect from TT */
482 /* FIXME schedule work to clear the halt */
Joe Perchesa475f602010-02-17 10:30:24 +0000483 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
485 case -ENOENT:
486 case -ECONNRESET:
487 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000488 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return;
490 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000491 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto goon;
493 }
494
xypron.glpk@gmx.debbf178e2016-05-18 20:40:51 +0200495 if (count < 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto goon;
497
498 rx_status = buf[count - 2];
499 if (rx_status & 0x1e) {
Joe Perchesa475f602010-02-17 10:30:24 +0000500 netif_dbg(pegasus, rx_err, net,
501 "RX packet error %x\n", rx_status);
Tobias Klauser82d82932017-04-07 10:17:39 +0200502 net->stats.rx_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000503 if (rx_status & 0x06) /* long or runt */
Tobias Klauser82d82932017-04-07 10:17:39 +0200504 net->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (rx_status & 0x08)
Tobias Klauser82d82932017-04-07 10:17:39 +0200506 net->stats.rx_crc_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000507 if (rx_status & 0x10) /* extra bits */
Tobias Klauser82d82932017-04-07 10:17:39 +0200508 net->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto goon;
510 }
511 if (pegasus->chip == 0x8513) {
512 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
513 pkt_len &= 0x0fff;
514 pegasus->rx_skb->data += 2;
515 } else {
516 pkt_len = buf[count - 3] << 8;
517 pkt_len += buf[count - 4];
518 pkt_len &= 0xfff;
Petko Manolov1a8deec2016-04-27 14:24:50 +0300519 pkt_len -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700523 * If the packet is unreasonably long, quietly drop it rather than
524 * kernel panicing by calling skb_put.
525 */
526 if (pkt_len > PEGASUS_MTU)
527 goto goon;
528
529 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * at this point we are sure pegasus->rx_skb != NULL
531 * so we go ahead and pass up the packet.
532 */
533 skb_put(pegasus->rx_skb, pkt_len);
534 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
535 netif_rx(pegasus->rx_skb);
Tobias Klauser82d82932017-04-07 10:17:39 +0200536 net->stats.rx_packets++;
537 net->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (pegasus->flags & PEGASUS_UNPLUG)
540 return;
541
Petko Manolov313a58e2013-04-25 22:41:21 +0000542 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
543 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (pegasus->rx_skb == NULL)
546 goto tl_sched;
547goon:
548 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
549 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300550 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700552 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
553 if (rx_status == -ENODEV)
554 netif_device_detach(pegasus->net);
555 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 pegasus->flags |= PEGASUS_RX_URB_FAIL;
557 goto tl_sched;
558 } else {
559 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
560 }
561
562 return;
563
564tl_sched:
565 tasklet_schedule(&pegasus->rx_tl);
566}
567
568static void rx_fixup(unsigned long data)
569{
570 pegasus_t *pegasus;
David Brownell955a2602006-05-26 10:17:03 -0700571 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 pegasus = (pegasus_t *) data;
574 if (pegasus->flags & PEGASUS_UNPLUG)
575 return;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
578 if (pegasus->rx_skb)
579 goto try_again;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000580 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000581 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
582 PEGASUS_MTU,
583 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (pegasus->rx_skb == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000585 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 tasklet_schedule(&pegasus->rx_tl);
Petko Manolov313a58e2013-04-25 22:41:21 +0000587 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
590 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300591 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 read_bulk_callback, pegasus);
593try_again:
David Brownell955a2602006-05-26 10:17:03 -0700594 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
595 if (status == -ENODEV)
596 netif_device_detach(pegasus->net);
597 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 pegasus->flags |= PEGASUS_RX_URB_FAIL;
599 tasklet_schedule(&pegasus->rx_tl);
600 } else {
601 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
David Howells7d12e782006-10-05 14:55:46 +0100605static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800608 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800609 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (!pegasus)
612 return;
613
Micah Gruber93519822007-07-23 16:05:52 +0800614 net = pegasus->net;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!netif_device_present(net) || !netif_running(net))
617 return;
618
Oliver Neukumc94cb312008-12-18 23:00:59 -0800619 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 case -EPIPE:
621 /* FIXME schedule_work() to clear the tx halt */
622 netif_stop_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000623 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return;
625 case -ENOENT:
626 case -ECONNRESET:
627 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000628 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return;
630 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000631 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* FALL THROUGH */
633 case 0:
634 break;
635 }
636
Florian Westphal860e9532016-05-03 16:33:13 +0200637 netif_trans_update(net); /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 netif_wake_queue(net);
639}
640
David Howells7d12e782006-10-05 14:55:46 +0100641static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 pegasus_t *pegasus = urb->context;
644 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800645 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (!pegasus)
648 return;
649 net = pegasus->net;
650
Oliver Neukumc94cb312008-12-18 23:00:59 -0800651 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 case 0:
653 break;
654 case -ECONNRESET: /* unlink */
655 case -ENOENT:
656 case -ESHUTDOWN:
657 return;
658 default:
659 /* some Pegasus-I products report LOTS of data
660 * toggle errors... avoid log spamming
661 */
Joe Perchesa475f602010-02-17 10:30:24 +0000662 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 if (urb->actual_length >= 6) {
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000666 u8 *d = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* byte 0 == tx_status1, reg 2B */
669 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
670 |LATE_COL|JABBER_TIMEOUT)) {
Tobias Klauser82d82932017-04-07 10:17:39 +0200671 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (d[0] & TX_UNDERRUN)
Tobias Klauser82d82932017-04-07 10:17:39 +0200673 net->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
Tobias Klauser82d82932017-04-07 10:17:39 +0200675 net->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (d[0] & LATE_COL)
Tobias Klauser82d82932017-04-07 10:17:39 +0200677 net->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 /* d[5].LINK_STATUS lies on some adapters.
681 * d[0].NO_CARRIER kicks in only with failed TX.
682 * ... so monitoring with MII may be safest.
683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
Tobias Klauser82d82932017-04-07 10:17:39 +0200686 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
Oliver Neukumc94cb312008-12-18 23:00:59 -0800689 res = usb_submit_urb(urb, GFP_ATOMIC);
690 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700691 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000692 if (res)
693 netif_err(pegasus, timer, net,
694 "can't resubmit interrupt urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -0500697static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 pegasus_t *pegasus = netdev_priv(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000700 netif_warn(pegasus, timer, net, "tx timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 usb_unlink_urb(pegasus->tx_urb);
Tobias Klauser82d82932017-04-07 10:17:39 +0200702 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000705static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
706 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 pegasus_t *pegasus = netdev_priv(net);
709 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
710 int res;
711 __u16 l16 = skb->len;
712
713 netif_stop_queue(net);
714
715 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300716 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
718 usb_sndbulkpipe(pegasus->usb, 2),
719 pegasus->tx_buff, count,
720 write_bulk_callback, pegasus);
721 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +0000722 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 switch (res) {
724 case -EPIPE: /* stall, or disconnect from TT */
725 /* cleanup should already have been scheduled */
726 break;
727 case -ENODEV: /* disconnect() upcoming */
Oliver Neukum9dd014e2009-04-17 01:40:19 -0700728 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700729 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731 default:
Tobias Klauser82d82932017-04-07 10:17:39 +0200732 net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 netif_start_queue(net);
734 }
735 } else {
Tobias Klauser82d82932017-04-07 10:17:39 +0200736 net->stats.tx_packets++;
737 net->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 dev_kfree_skb(skb);
740
Patrick McHardy6ed10652009-06-23 06:03:08 +0000741 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000744static inline void disable_net_traffic(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Michael Buesche3453f62009-06-18 07:03:47 +0000746 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Michael Buesche3453f62009-06-18 07:03:47 +0000748 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000751static inline void get_interrupt_interval(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Michael Buesche3453f62009-06-18 07:03:47 +0000753 u16 data;
754 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Michael Buesche3453f62009-06-18 07:03:47 +0000756 read_eprom_word(pegasus, 4, &data);
757 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700758 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000759 if (interval < 0x80) {
Joe Perchesa475f602010-02-17 10:30:24 +0000760 netif_info(pegasus, timer, pegasus->net,
761 "intr interval changed from %ums to %ums\n",
762 interval, 0x80);
Michael Buesche3453f62009-06-18 07:03:47 +0000763 interval = 0x80;
764 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700765#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000766 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Michael Buesche3453f62009-06-18 07:03:47 +0000770 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773static void set_carrier(struct net_device *net)
774{
775 pegasus_t *pegasus = netdev_priv(net);
776 u16 tmp;
777
Dan Williamsc43c49b2007-04-24 10:20:06 -0400778 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (tmp & BMSR_LSTATUS)
782 netif_carrier_on(net);
783 else
784 netif_carrier_off(net);
785}
786
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000787static void free_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 usb_free_urb(pegasus->intr_urb);
790 usb_free_urb(pegasus->tx_urb);
791 usb_free_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000794static void unlink_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 usb_kill_urb(pegasus->intr_urb);
797 usb_kill_urb(pegasus->tx_urb);
798 usb_kill_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000801static int alloc_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Petko Manolov323b3492013-04-25 22:41:50 +0000803 int res = -ENOMEM;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
806 if (!pegasus->rx_urb) {
Petko Manolov323b3492013-04-25 22:41:50 +0000807 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
810 if (!pegasus->tx_urb) {
811 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000812 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
815 if (!pegasus->intr_urb) {
816 usb_free_urb(pegasus->tx_urb);
817 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000818 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Petko Manolov323b3492013-04-25 22:41:50 +0000821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824static int pegasus_open(struct net_device *net)
825{
826 pegasus_t *pegasus = netdev_priv(net);
Petko Manolov323b3492013-04-25 22:41:50 +0000827 int res=-ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000830 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
831 PEGASUS_MTU,
832 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!pegasus->rx_skb)
Petko Manolov323b3492013-04-25 22:41:50 +0000834 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 res = set_registers(pegasus, EthID, 6, net->dev_addr);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
839 usb_rcvbulkpipe(pegasus->usb, 1),
Petko Manolovb7302ca2016-04-27 14:24:49 +0300840 pegasus->rx_skb->data, PEGASUS_MTU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 read_bulk_callback, pegasus);
842 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700843 if (res == -ENODEV)
844 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000845 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto exit;
847 }
848
849 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
850 usb_rcvintpipe(pegasus->usb, 3),
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000851 pegasus->intr_buff, sizeof(pegasus->intr_buff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 intr_callback, pegasus, pegasus->intr_interval);
853 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700854 if (res == -ENODEV)
855 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000856 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 usb_kill_urb(pegasus->rx_urb);
858 goto exit;
859 }
Petko Manolov323b3492013-04-25 22:41:50 +0000860 res = enable_net_traffic(net, pegasus->usb);
861 if (res < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000862 netif_dbg(pegasus, ifup, net,
863 "can't enable_net_traffic() - %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 res = -EIO;
865 usb_kill_urb(pegasus->rx_urb);
866 usb_kill_urb(pegasus->intr_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto exit;
868 }
869 set_carrier(net);
870 netif_start_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000871 netif_dbg(pegasus, ifup, net, "open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 res = 0;
873exit:
874 return res;
875}
876
877static int pegasus_close(struct net_device *net)
878{
879 pegasus_t *pegasus = netdev_priv(net);
880
881 netif_stop_queue(net);
882 if (!(pegasus->flags & PEGASUS_UNPLUG))
883 disable_net_traffic(pegasus);
884 tasklet_kill(&pegasus->rx_tl);
885 unlink_all_urbs(pegasus);
886
887 return 0;
888}
889
890static void pegasus_get_drvinfo(struct net_device *dev,
891 struct ethtool_drvinfo *info)
892{
893 pegasus_t *pegasus = netdev_priv(dev);
Jiri Pirko7826d432013-01-06 00:44:26 +0000894
895 strlcpy(info->driver, driver_name, sizeof(info->driver));
896 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000897 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/* also handles three patterns of some kind in hardware */
901#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
902
903static void
904pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
905{
906 pegasus_t *pegasus = netdev_priv(dev);
907
908 wol->supported = WAKE_MAGIC | WAKE_PHY;
909 wol->wolopts = pegasus->wolopts;
910}
911
912static int
913pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
914{
915 pegasus_t *pegasus = netdev_priv(dev);
916 u8 reg78 = 0x04;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000917 int ret;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (wol->wolopts & ~WOL_SUPPORTED)
920 return -EINVAL;
921
922 if (wol->wolopts & WAKE_MAGIC)
923 reg78 |= 0x80;
924 if (wol->wolopts & WAKE_PHY)
925 reg78 |= 0x40;
926 /* FIXME this 0x10 bit still needs to get set in the chip... */
927 if (wol->wolopts)
928 pegasus->eth_regs[0] |= 0x10;
929 else
930 pegasus->eth_regs[0] &= ~0x10;
931 pegasus->wolopts = wol->wolopts;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000932
933 ret = set_register(pegasus, WakeupControl, reg78);
934 if (!ret)
935 ret = device_set_wakeup_enable(&pegasus->usb->dev,
936 wol->wolopts);
937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static inline void pegasus_reset_wol(struct net_device *dev)
941{
942 struct ethtool_wolinfo wol;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 memset(&wol, 0, sizeof wol);
945 (void) pegasus_set_wol(dev, &wol);
946}
947
948static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100949pegasus_get_link_ksettings(struct net_device *dev,
950 struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 pegasus_t *pegasus;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100955 mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957}
958
959static int
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100960pegasus_set_link_ksettings(struct net_device *dev,
961 const struct ethtool_link_ksettings *ecmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 pegasus_t *pegasus = netdev_priv(dev);
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100964 return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967static int pegasus_nway_reset(struct net_device *dev)
968{
969 pegasus_t *pegasus = netdev_priv(dev);
970 return mii_nway_restart(&pegasus->mii);
971}
972
973static u32 pegasus_get_link(struct net_device *dev)
974{
975 pegasus_t *pegasus = netdev_priv(dev);
976 return mii_link_ok(&pegasus->mii);
977}
978
979static u32 pegasus_get_msglevel(struct net_device *dev)
980{
981 pegasus_t *pegasus = netdev_priv(dev);
982 return pegasus->msg_enable;
983}
984
985static void pegasus_set_msglevel(struct net_device *dev, u32 v)
986{
987 pegasus_t *pegasus = netdev_priv(dev);
988 pegasus->msg_enable = v;
989}
990
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700991static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 .get_drvinfo = pegasus_get_drvinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 .nway_reset = pegasus_nway_reset,
994 .get_link = pegasus_get_link,
995 .get_msglevel = pegasus_get_msglevel,
996 .set_msglevel = pegasus_set_msglevel,
997 .get_wol = pegasus_get_wol,
998 .set_wol = pegasus_set_wol,
Philippe Reynes71dbc3412017-03-17 23:34:04 +0100999 .get_link_ksettings = pegasus_get_link_ksettings,
1000 .set_link_ksettings = pegasus_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001};
1002
1003static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1004{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001005 __u16 *data = (__u16 *) &rq->ifr_ifru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 pegasus_t *pegasus = netdev_priv(net);
1007 int res;
1008
1009 switch (cmd) {
1010 case SIOCDEVPRIVATE:
1011 data[0] = pegasus->phy;
Gustavo A. R. Silva209d6e72019-02-08 13:07:29 -06001012 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case SIOCDEVPRIVATE + 1:
1014 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1015 res = 0;
1016 break;
1017 case SIOCDEVPRIVATE + 2:
1018 if (!capable(CAP_NET_ADMIN))
1019 return -EPERM;
Petko Manolov2bd64702013-04-25 22:41:36 +00001020 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 res = 0;
1022 break;
1023 default:
1024 res = -EOPNOTSUPP;
1025 }
1026 return res;
1027}
1028
1029static void pegasus_set_multicast(struct net_device *net)
1030{
1031 pegasus_t *pegasus = netdev_priv(net);
1032
1033 if (net->flags & IFF_PROMISC) {
1034 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001035 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001036 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1038 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001039 netif_dbg(pegasus, link, net, "set allmulti\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 } else {
1041 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1042 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1043 }
Petko Manolov323b3492013-04-25 22:41:50 +00001044 update_eth_regs_async(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001047static __u8 mii_phy_probe(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int i;
1050 __u16 tmp;
1051
1052 for (i = 0; i < 32; i++) {
1053 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1054 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1055 continue;
1056 else
1057 return i;
1058 }
1059
1060 return 0xff;
1061}
1062
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001063static inline void setup_pegasus_II(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 __u8 data = 0xa5;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001066
Petko Manolov4a1728a2005-11-15 09:48:23 +02001067 set_register(pegasus, Reg1d, 0);
1068 set_register(pegasus, Reg7b, 1);
Jia-Ju Bai6dff5ad2018-07-27 16:36:29 +08001069 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001071 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001073 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Petko Manolov4a1728a2005-11-15 09:48:23 +02001075 set_register(pegasus, 0x83, data);
1076 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001078 if (data == 0xa5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 pegasus->chip = 0x8513;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001080 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 pegasus->chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Petko Manolov4a1728a2005-11-15 09:48:23 +02001083 set_register(pegasus, 0x80, 0xc0);
1084 set_register(pegasus, 0x83, 0xff);
1085 set_register(pegasus, 0x84, 0x01);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001088 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001090 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
David Howellsc4028952006-11-22 14:57:56 +00001093static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
David Howellsc4028952006-11-22 14:57:56 +00001095 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 set_carrier(pegasus->net);
1097 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
Petko Manolov2f5107c2020-04-02 17:33:29 +03001098 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 CARRIER_CHECK_DELAY);
1100 }
1101}
1102
Ben Collins4f631352008-07-30 12:39:02 -07001103static int pegasus_blacklisted(struct usb_device *udev)
1104{
1105 struct usb_device_descriptor *udd = &udev->descriptor;
1106
1107 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1108 * dongle which happens to have the same ID.
1109 */
Michael Buesche3453f62009-06-18 07:03:47 +00001110 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1111 (udd->idProduct == cpu_to_le16(0x0121)) &&
Ben Collins4f631352008-07-30 12:39:02 -07001112 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1113 (udd->bDeviceProtocol == 1))
1114 return 1;
1115
1116 return 0;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static int pegasus_probe(struct usb_interface *intf,
1120 const struct usb_device_id *id)
1121{
1122 struct usb_device *dev = interface_to_usbdev(intf);
1123 struct net_device *net;
1124 pegasus_t *pegasus;
1125 int dev_index = id - pegasus_ids;
1126 int res = -ENOMEM;
1127
David Brownellcda28362008-11-16 00:36:08 -08001128 if (pegasus_blacklisted(dev))
1129 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 net = alloc_etherdev(sizeof(struct pegasus));
Joe Perches41de8d42012-01-29 13:47:52 +00001132 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 pegasus->dev_index = dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Petko Manolov323b3492013-04-25 22:41:50 +00001138 res = alloc_urbs(pegasus);
1139 if (res < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1141 goto out1;
1142 }
1143
1144 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1145
David Howellsc4028952006-11-22 14:57:56 +00001146 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 pegasus->intf = intf;
1149 pegasus->usb = dev;
1150 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001151
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001154 net->netdev_ops = &pegasus_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001155 net->ethtool_ops = &ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 pegasus->mii.dev = net;
1157 pegasus->mii.mdio_read = mdio_read;
1158 pegasus->mii.mdio_write = mdio_write;
1159 pegasus->mii.phy_id_mask = 0x1f;
1160 pegasus->mii.reg_num_mask = 0x1f;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001161 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1163
1164 pegasus->features = usb_dev_id[dev_index].private;
1165 get_interrupt_interval(pegasus);
1166 if (reset_mac(pegasus)) {
1167 dev_err(&intf->dev, "can't reset MAC\n");
1168 res = -EIO;
1169 goto out2;
1170 }
1171 set_ethernet_addr(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (pegasus->features & PEGASUS_II) {
1173 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1174 setup_pegasus_II(pegasus);
1175 }
1176 pegasus->phy = mii_phy_probe(pegasus);
1177 if (pegasus->phy == 0xff) {
1178 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1179 pegasus->phy = 1;
1180 }
1181 pegasus->mii.phy_id = pegasus->phy;
1182 usb_set_intfdata(intf, pegasus);
1183 SET_NETDEV_DEV(net, &intf->dev);
1184 pegasus_reset_wol(net);
1185 res = register_netdev(net);
1186 if (res)
1187 goto out3;
Petko Manolov2f5107c2020-04-02 17:33:29 +03001188 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
Petko Manolov323b3492013-04-25 22:41:50 +00001189 CARRIER_CHECK_DELAY);
1190 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1191 usb_dev_id[dev_index].name, net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return 0;
1193
1194out3:
1195 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196out2:
1197 free_all_urbs(pegasus);
1198out1:
1199 free_netdev(net);
1200out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return res;
1202}
1203
1204static void pegasus_disconnect(struct usb_interface *intf)
1205{
1206 struct pegasus *pegasus = usb_get_intfdata(intf);
1207
1208 usb_set_intfdata(intf, NULL);
1209 if (!pegasus) {
1210 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1211 return;
1212 }
1213
1214 pegasus->flags |= PEGASUS_UNPLUG;
Petko Manolov2f5107c2020-04-02 17:33:29 +03001215 cancel_delayed_work_sync(&pegasus->carrier_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unregister_netdev(pegasus->net);
Kevin Vigora85a46f2005-09-22 00:49:24 -07001217 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 free_all_urbs(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001219 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001221 pegasus->rx_skb = NULL;
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 free_netdev(pegasus->net);
1224}
1225
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001226static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct pegasus *pegasus = usb_get_intfdata(intf);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001229
1230 netif_device_detach(pegasus->net);
Petko Manolov2f5107c2020-04-02 17:33:29 +03001231 cancel_delayed_work_sync(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001232 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001233 usb_kill_urb(pegasus->rx_urb);
1234 usb_kill_urb(pegasus->intr_urb);
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
1237}
1238
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001239static int pegasus_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct pegasus *pegasus = usb_get_intfdata(intf);
1242
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001243 netif_device_attach(pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001244 if (netif_running(pegasus->net)) {
1245 pegasus->rx_urb->status = 0;
1246 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001247 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001248
1249 pegasus->intr_urb->status = 0;
1250 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001251 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001252 }
Petko Manolov2f5107c2020-04-02 17:33:29 +03001253 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
David Brownell7e713b82006-05-01 14:02:45 -07001254 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256}
1257
Oliver Neukum8cb89572009-01-08 11:22:25 -08001258static const struct net_device_ops pegasus_netdev_ops = {
1259 .ndo_open = pegasus_open,
1260 .ndo_stop = pegasus_close,
1261 .ndo_do_ioctl = pegasus_ioctl,
1262 .ndo_start_xmit = pegasus_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001263 .ndo_set_rx_mode = pegasus_set_multicast,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001264 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001265 .ndo_set_mac_address = eth_mac_addr,
1266 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001267};
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static struct usb_driver pegasus_driver = {
1270 .name = driver_name,
1271 .probe = pegasus_probe,
1272 .disconnect = pegasus_disconnect,
1273 .id_table = pegasus_ids,
1274 .suspend = pegasus_suspend,
1275 .resume = pegasus_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001276 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277};
1278
David Brownellcda28362008-11-16 00:36:08 -08001279static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001280{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001281 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1282 char *token, *name = NULL;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001283
1284 if ((token = strsep(&id, ":")) != NULL)
1285 name = token;
1286 /* name now points to a null terminated string*/
1287 if ((token = strsep(&id, ":")) != NULL)
1288 vendor_id = simple_strtoul(token, NULL, 16);
1289 if ((token = strsep(&id, ":")) != NULL)
1290 device_id = simple_strtoul(token, NULL, 16);
1291 flags = simple_strtoul(id, NULL, 16);
1292 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001293 driver_name, name, vendor_id, device_id, flags);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001294
1295 if (vendor_id > 0x10000 || vendor_id == 0)
1296 return;
1297 if (device_id > 0x10000 || device_id == 0)
1298 return;
1299
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001300 for (i = 0; usb_dev_id[i].name; i++);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001301 usb_dev_id[i].name = name;
1302 usb_dev_id[i].vendor = vendor_id;
1303 usb_dev_id[i].device = device_id;
1304 usb_dev_id[i].private = flags;
1305 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1306 pegasus_ids[i].idVendor = vendor_id;
1307 pegasus_ids[i].idProduct = device_id;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310static int __init pegasus_init(void)
1311{
1312 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001313 if (devid)
1314 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return usb_register(&pegasus_driver);
1316}
1317
1318static void __exit pegasus_exit(void)
1319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 usb_deregister(&pegasus_driver);
1321}
1322
1323module_init(pegasus_init);
1324module_exit(pegasus_exit);