blob: 09699054b54f66e65b5c168a8dec868229831147 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Petko Manolov323b3492013-04-25 22:41:50 +00002 * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ChangeLog:
9 * .... Most of the time spent on reading sources & docs.
10 * v0.2.x First official release for the Linux kernel.
11 * v0.3.0 Beutified and structured, some bugs fixed.
12 * v0.3.x URBifying bulk requests and bugfixing. First relatively
13 * stable release. Still can touch device's registers only
14 * from top-halves.
15 * v0.4.0 Control messages remained unurbified are now URBs.
16 * Now we can touch the HW at any time.
17 * v0.4.9 Control urbs again use process context to wait. Argh...
18 * Some long standing bugs (enable_net_traffic) fixed.
19 * Also nasty trick about resubmiting control urb from
20 * interrupt context used. Please let me know how it
21 * behaves. Pegasus II support added since this version.
22 * TODO: suppressing HCD warnings spewage on disconnect.
23 * v0.4.13 Ethernet address is now set at probe(), not at open()
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +000024 * time as this seems to break dhcpd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * v0.5.0 branch to 2.5.x kernels
26 * v0.5.1 ethtool support added
27 * v0.5.5 rx socket buffers are in a pool and the their allocation
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +000028 * is out of the interrupt routine.
Petko Manolov323b3492013-04-25 22:41:50 +000029 * ...
30 * v0.9.3 simplified [get|set]_register(s), async update registers
31 * logic revisited, receive skb_pool removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/delay.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/ethtool.h>
41#include <linux/mii.h>
42#include <linux/usb.h>
43#include <linux/module.h>
44#include <asm/byteorder.h>
45#include <asm/uaccess.h>
46#include "pegasus.h"
47
48/*
49 * Version Information
50 */
Petko Manolov323b3492013-04-25 22:41:50 +000051#define DRIVER_VERSION "v0.9.3 (2013/04/25)"
52#define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
54
55static const char driver_name[] = "pegasus";
56
57#undef PEGASUS_WRITE_EEPROM
58#define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
59 BMSR_100FULL | BMSR_ANEGCAPABLE)
60
Rusty Russelleb939922011-12-19 14:08:01 +000061static bool loopback;
62static bool mii_mode;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +000063static char *devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static struct usb_eth_dev usb_dev_id[] = {
66#define PEGASUS_DEV(pn, vid, pid, flags) \
67 {.name = pn, .vendor = vid, .device = pid, .private = flags},
Chris Rankinab854b22009-10-13 00:32:02 -070068#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
69 PEGASUS_DEV(pn, vid, pid, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "pegasus.h"
71#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070072#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020073 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {NULL, 0, 0, 0}
75};
76
77static struct usb_device_id pegasus_ids[] = {
78#define PEGASUS_DEV(pn, vid, pid, flags) \
79 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
Chris Rankinab854b22009-10-13 00:32:02 -070080/*
81 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
82 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
83 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
84 * case anyway, seeing as the pegasus is for "Wired" adaptors.
85 */
86#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
87 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
88 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include "pegasus.h"
90#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070091#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020092 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 {}
94};
95
96MODULE_AUTHOR(DRIVER_AUTHOR);
97MODULE_DESCRIPTION(DRIVER_DESC);
98MODULE_LICENSE("GPL");
99module_param(loopback, bool, 0);
100module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +0200101module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
103MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +0200104MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* use ethtool to change the level for any given device */
107static int msg_level = -1;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000108module_param(msg_level, int, 0);
109MODULE_PARM_DESC(msg_level, "Override default message level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111MODULE_DEVICE_TABLE(usb, pegasus_ids);
Oliver Neukum8cb89572009-01-08 11:22:25 -0800112static const struct net_device_ops pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Petko Manolov323b3492013-04-25 22:41:50 +0000114/*****/
115
116static void async_ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Petko Manolov323b3492013-04-25 22:41:50 +0000118 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800119 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Petko Manolov323b3492013-04-25 22:41:50 +0000121 if (status < 0)
122 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
123 kfree(req);
124 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Petko Manolov323b3492013-04-25 22:41:50 +0000127static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Petko Manolov323b3492013-04-25 22:41:50 +0000131 ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
132 PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
133 indx, data, size, 1000);
134 if (ret < 0)
135 netif_dbg(pegasus, drv, pegasus->net,
136 "%s returned %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ret;
138}
139
Petko Manolov323b3492013-04-25 22:41:50 +0000140static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Petko Manolov323b3492013-04-25 22:41:50 +0000144 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
145 PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
146 indx, data, size, 100);
147 if (ret < 0)
148 netif_dbg(pegasus, drv, pegasus->net,
149 "%s returned %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ret;
151}
152
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000153static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Petko Manolov323b3492013-04-25 22:41:50 +0000157 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
158 PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
159 indx, &data, 1, 1000);
160 if (ret < 0)
161 netif_dbg(pegasus, drv, pegasus->net,
162 "%s returned %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return ret;
164}
165
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000166static int update_eth_regs_async(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Petko Manolov323b3492013-04-25 22:41:50 +0000168 int ret = -ENOMEM;
169 struct urb *async_urb;
170 struct usb_ctrlrequest *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Petko Manolov323b3492013-04-25 22:41:50 +0000172 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
173 if (req == NULL)
174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Petko Manolov323b3492013-04-25 22:41:50 +0000176 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
177 if (async_urb == NULL) {
178 kfree(req);
179 return ret;
180 }
181 req->bRequestType = PEGASUS_REQT_WRITE;
182 req->bRequest = PEGASUS_REQ_SET_REGS;
183 req->wValue = cpu_to_le16(0);
184 req->wIndex = cpu_to_le16(EthCtrl0);
185 req->wLength = cpu_to_le16(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Petko Manolov323b3492013-04-25 22:41:50 +0000187 usb_fill_control_urb(async_urb, pegasus->usb,
188 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
189 pegasus->eth_regs, 3, async_ctrl_callback, req);
190
191 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
192 if (ret) {
David Brownell955a2602006-05-26 10:17:03 -0700193 if (ret == -ENODEV)
194 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000195 netif_err(pegasus, drv, pegasus->net,
Petko Manolov323b3492013-04-25 22:41:50 +0000196 "%s returned %d\n", __func__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return ret;
199}
200
Petko Manolov2bd64702013-04-25 22:41:36 +0000201static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204 __u8 data[4] = { phy, 0, 0, indx };
205 __le16 regdi;
Petko Manolov2bd64702013-04-25 22:41:36 +0000206 int ret = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Petko Manolov2bd64702013-04-25 22:41:36 +0000208 if (cmd & PHY_WRITE) {
209 __le16 *t = (__le16 *) & data[1];
210 *t = cpu_to_le16(*regd);
211 }
212 set_register(p, PhyCtrl, 0);
213 set_registers(p, PhyAddr, sizeof(data), data);
214 set_register(p, PhyCtrl, (indx | cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov2bd64702013-04-25 22:41:36 +0000216 ret = get_registers(p, PhyCtrl, 1, data);
217 if (ret < 0)
David Brownell7e713b82006-05-01 14:02:45 -0700218 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (data[0] & PHY_DONE)
220 break;
221 }
Joe Perchesa475f602010-02-17 10:30:24 +0000222 if (i >= REG_TIMEOUT)
223 goto fail;
Petko Manolov2bd64702013-04-25 22:41:36 +0000224 if (cmd & PHY_READ) {
225 ret = get_registers(p, PhyData, 2, &regdi);
226 *regd = le16_to_cpu(regdi);
227 return ret;
228 }
229 return 0;
David Brownell7e713b82006-05-01 14:02:45 -0700230fail:
Petko Manolov2bd64702013-04-25 22:41:36 +0000231 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Petko Manolov2bd64702013-04-25 22:41:36 +0000235/* Returns non-negative int on success, error on failure */
236static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
237{
238 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
239}
240
241/* Returns zero on success, error on failure */
242static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
243{
244 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static int mdio_read(struct net_device *dev, int phy_id, int loc)
248{
Joe Perches8739cfe2010-11-15 11:12:29 +0000249 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u16 res;
251
252 read_mii_word(pegasus, phy_id, loc, &res);
253 return (int)res;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
257{
Joe Perches8739cfe2010-11-15 11:12:29 +0000258 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Petko Manolov2bd64702013-04-25 22:41:36 +0000260 write_mii_word(pegasus, phy_id, loc, (__u16 *)&val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000263static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 int i;
266 __u8 tmp;
267 __le16 retdatai;
268 int ret;
269
Petko Manolov4a1728a2005-11-15 09:48:23 +0200270 set_register(pegasus, EpromCtrl, 0);
271 set_register(pegasus, EpromOffset, index);
272 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 for (i = 0; i < REG_TIMEOUT; i++) {
275 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
276 if (tmp & EPROM_DONE)
277 break;
David Brownell7e713b82006-05-01 14:02:45 -0700278 if (ret == -ESHUTDOWN)
279 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Joe Perchesa475f602010-02-17 10:30:24 +0000281 if (i >= REG_TIMEOUT)
282 goto fail;
283
284 ret = get_registers(pegasus, EpromData, 2, &retdatai);
285 *retdata = le16_to_cpu(retdatai);
286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Brownell7e713b82006-05-01 14:02:45 -0700288fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000289 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200290 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293#ifdef PEGASUS_WRITE_EEPROM
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000294static inline void enable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Petko Manolov4a1728a2005-11-15 09:48:23 +0200298 get_registers(pegasus, EthCtrl2, 1, &tmp);
299 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000302static inline void disable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Petko Manolov4a1728a2005-11-15 09:48:23 +0200306 get_registers(pegasus, EthCtrl2, 1, &tmp);
307 set_register(pegasus, EpromCtrl, 0);
308 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000311static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 int i;
314 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
315 int ret;
Michael Buesche3453f62009-06-18 07:03:47 +0000316 __le16 le_data = cpu_to_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Petko Manolov4a1728a2005-11-15 09:48:23 +0200318 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200320 set_register(pegasus, EpromOffset, index);
Michael Buesche3453f62009-06-18 07:03:47 +0000321 set_registers(pegasus, EpromData, 2, &le_data);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200322 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 for (i = 0; i < REG_TIMEOUT; i++) {
325 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700326 if (ret == -ESHUTDOWN)
327 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (tmp & EPROM_DONE)
329 break;
330 }
331 disable_eprom_write(pegasus);
Joe Perchesa475f602010-02-17 10:30:24 +0000332 if (i >= REG_TIMEOUT)
333 goto fail;
334
335 return ret;
336
David Brownell7e713b82006-05-01 14:02:45 -0700337fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000338 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200339 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341#endif /* PEGASUS_WRITE_EEPROM */
342
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000343static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 int i;
346 __u16 w16;
347
348 for (i = 0; i < 3; i++) {
349 read_eprom_word(pegasus, i, &w16);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700350 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352}
353
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000354static void set_ethernet_addr(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Petko Manolov37cf3472006-09-27 14:25:37 -0700358 if (pegasus->features & PEGASUS_II) {
359 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
360 } else {
361 get_node_id(pegasus, node_id);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000362 set_registers(pegasus, EthID, sizeof(node_id), node_id);
Petko Manolov37cf3472006-09-27 14:25:37 -0700363 }
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000364 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000367static inline int reset_mac(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 __u8 data = 0x8;
370 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Petko Manolov4a1728a2005-11-15 09:48:23 +0200372 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200374 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (~data & 0x08) {
Dan Carpenter681f1622011-12-23 00:44:36 +0000376 if (loopback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200379 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200381 set_register(pegasus, Gpio1, 0x26);
382 set_register(pegasus, Gpio0, pegasus->features);
383 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 }
386 }
387 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200388 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
391 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200392 set_register(pegasus, Gpio0, 0x24);
393 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
396 __u16 auxmode;
397 read_mii_word(pegasus, 3, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000398 auxmode |= 4;
399 write_mii_word(pegasus, 3, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 return 0;
403}
404
405static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
406{
407 __u16 linkpart;
408 __u8 data[4];
409 pegasus_t *pegasus = netdev_priv(dev);
410 int ret;
411
412 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
413 data[0] = 0xc9;
414 data[1] = 0;
415 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
416 data[1] |= 0x20; /* set full duplex */
417 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
418 data[1] |= 0x10; /* set 100 Mbps */
419 if (mii_mode)
420 data[1] = 0;
Dan Carpenter681f1622011-12-23 00:44:36 +0000421 data[2] = loopback ? 0x09 : 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000423 memcpy(pegasus->eth_regs, data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ret = set_registers(pegasus, EthCtrl0, 3, data);
425
426 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100427 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
429 u16 auxmode;
430 read_mii_word(pegasus, 0, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000431 auxmode |= 4;
432 write_mii_word(pegasus, 0, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Petko Manolov4a1728a2005-11-15 09:48:23 +0200435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
David Howells7d12e782006-10-05 14:55:46 +0100438static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 pegasus_t *pegasus = urb->context;
441 struct net_device *net;
442 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800443 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 u8 *buf = urb->transfer_buffer;
445 __u16 pkt_len;
446
447 if (!pegasus)
448 return;
449
450 net = pegasus->net;
451 if (!netif_device_present(net) || !netif_running(net))
452 return;
453
Oliver Neukumc94cb312008-12-18 23:00:59 -0800454 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case 0:
456 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700457 case -ETIME:
Joe Perchesa475f602010-02-17 10:30:24 +0000458 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 pegasus->flags &= ~PEGASUS_RX_BUSY;
460 break;
461 case -EPIPE: /* stall, or disconnect from TT */
462 /* FIXME schedule work to clear the halt */
Joe Perchesa475f602010-02-17 10:30:24 +0000463 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
465 case -ENOENT:
466 case -ECONNRESET:
467 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000468 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return;
470 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000471 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto goon;
473 }
474
475 if (!count || count < 4)
476 goto goon;
477
478 rx_status = buf[count - 2];
479 if (rx_status & 0x1e) {
Joe Perchesa475f602010-02-17 10:30:24 +0000480 netif_dbg(pegasus, rx_err, net,
481 "RX packet error %x\n", rx_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 pegasus->stats.rx_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000483 if (rx_status & 0x06) /* long or runt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 pegasus->stats.rx_length_errors++;
485 if (rx_status & 0x08)
486 pegasus->stats.rx_crc_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000487 if (rx_status & 0x10) /* extra bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 pegasus->stats.rx_frame_errors++;
489 goto goon;
490 }
491 if (pegasus->chip == 0x8513) {
492 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
493 pkt_len &= 0x0fff;
494 pegasus->rx_skb->data += 2;
495 } else {
496 pkt_len = buf[count - 3] << 8;
497 pkt_len += buf[count - 4];
498 pkt_len &= 0xfff;
499 pkt_len -= 8;
500 }
501
502 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700503 * If the packet is unreasonably long, quietly drop it rather than
504 * kernel panicing by calling skb_put.
505 */
506 if (pkt_len > PEGASUS_MTU)
507 goto goon;
508
509 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * at this point we are sure pegasus->rx_skb != NULL
511 * so we go ahead and pass up the packet.
512 */
513 skb_put(pegasus->rx_skb, pkt_len);
514 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
515 netif_rx(pegasus->rx_skb);
516 pegasus->stats.rx_packets++;
517 pegasus->stats.rx_bytes += pkt_len;
518
519 if (pegasus->flags & PEGASUS_UNPLUG)
520 return;
521
Petko Manolov313a58e2013-04-25 22:41:21 +0000522 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
523 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (pegasus->rx_skb == NULL)
526 goto tl_sched;
527goon:
528 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
529 usb_rcvbulkpipe(pegasus->usb, 1),
530 pegasus->rx_skb->data, PEGASUS_MTU + 8,
531 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700532 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
533 if (rx_status == -ENODEV)
534 netif_device_detach(pegasus->net);
535 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 pegasus->flags |= PEGASUS_RX_URB_FAIL;
537 goto tl_sched;
538 } else {
539 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
540 }
541
542 return;
543
544tl_sched:
545 tasklet_schedule(&pegasus->rx_tl);
546}
547
548static void rx_fixup(unsigned long data)
549{
550 pegasus_t *pegasus;
David Brownell955a2602006-05-26 10:17:03 -0700551 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 pegasus = (pegasus_t *) data;
554 if (pegasus->flags & PEGASUS_UNPLUG)
555 return;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
558 if (pegasus->rx_skb)
559 goto try_again;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000560 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000561 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
562 PEGASUS_MTU,
563 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (pegasus->rx_skb == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000565 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 tasklet_schedule(&pegasus->rx_tl);
Petko Manolov313a58e2013-04-25 22:41:21 +0000567 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
570 usb_rcvbulkpipe(pegasus->usb, 1),
571 pegasus->rx_skb->data, PEGASUS_MTU + 8,
572 read_bulk_callback, pegasus);
573try_again:
David Brownell955a2602006-05-26 10:17:03 -0700574 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
575 if (status == -ENODEV)
576 netif_device_detach(pegasus->net);
577 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 pegasus->flags |= PEGASUS_RX_URB_FAIL;
579 tasklet_schedule(&pegasus->rx_tl);
580 } else {
581 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
David Howells7d12e782006-10-05 14:55:46 +0100585static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800588 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800589 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (!pegasus)
592 return;
593
Micah Gruber93519822007-07-23 16:05:52 +0800594 net = pegasus->net;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!netif_device_present(net) || !netif_running(net))
597 return;
598
Oliver Neukumc94cb312008-12-18 23:00:59 -0800599 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 case -EPIPE:
601 /* FIXME schedule_work() to clear the tx halt */
602 netif_stop_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000603 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return;
605 case -ENOENT:
606 case -ECONNRESET:
607 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000608 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return;
610 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000611 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* FALL THROUGH */
613 case 0:
614 break;
615 }
616
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700617 net->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 netif_wake_queue(net);
619}
620
David Howells7d12e782006-10-05 14:55:46 +0100621static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 pegasus_t *pegasus = urb->context;
624 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800625 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (!pegasus)
628 return;
629 net = pegasus->net;
630
Oliver Neukumc94cb312008-12-18 23:00:59 -0800631 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 case 0:
633 break;
634 case -ECONNRESET: /* unlink */
635 case -ENOENT:
636 case -ESHUTDOWN:
637 return;
638 default:
639 /* some Pegasus-I products report LOTS of data
640 * toggle errors... avoid log spamming
641 */
Joe Perchesa475f602010-02-17 10:30:24 +0000642 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 if (urb->actual_length >= 6) {
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000646 u8 *d = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* byte 0 == tx_status1, reg 2B */
649 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
650 |LATE_COL|JABBER_TIMEOUT)) {
651 pegasus->stats.tx_errors++;
652 if (d[0] & TX_UNDERRUN)
653 pegasus->stats.tx_fifo_errors++;
654 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
655 pegasus->stats.tx_aborted_errors++;
656 if (d[0] & LATE_COL)
657 pegasus->stats.tx_window_errors++;
658 }
659
660 /* d[5].LINK_STATUS lies on some adapters.
661 * d[0].NO_CARRIER kicks in only with failed TX.
662 * ... so monitoring with MII may be safest.
663 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
666 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
667 }
668
Oliver Neukumc94cb312008-12-18 23:00:59 -0800669 res = usb_submit_urb(urb, GFP_ATOMIC);
670 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700671 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000672 if (res)
673 netif_err(pegasus, timer, net,
674 "can't resubmit interrupt urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677static void pegasus_tx_timeout(struct net_device *net)
678{
679 pegasus_t *pegasus = netdev_priv(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000680 netif_warn(pegasus, timer, net, "tx timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 usb_unlink_urb(pegasus->tx_urb);
682 pegasus->stats.tx_errors++;
683}
684
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000685static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
686 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 pegasus_t *pegasus = netdev_priv(net);
689 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
690 int res;
691 __u16 l16 = skb->len;
692
693 netif_stop_queue(net);
694
695 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300696 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
698 usb_sndbulkpipe(pegasus->usb, 2),
699 pegasus->tx_buff, count,
700 write_bulk_callback, pegasus);
701 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +0000702 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 switch (res) {
704 case -EPIPE: /* stall, or disconnect from TT */
705 /* cleanup should already have been scheduled */
706 break;
707 case -ENODEV: /* disconnect() upcoming */
Oliver Neukum9dd014e2009-04-17 01:40:19 -0700708 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700709 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 default:
712 pegasus->stats.tx_errors++;
713 netif_start_queue(net);
714 }
715 } else {
716 pegasus->stats.tx_packets++;
717 pegasus->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 dev_kfree_skb(skb);
720
Patrick McHardy6ed10652009-06-23 06:03:08 +0000721 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
725{
726 return &((pegasus_t *) netdev_priv(dev))->stats;
727}
728
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000729static inline void disable_net_traffic(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Michael Buesche3453f62009-06-18 07:03:47 +0000731 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Michael Buesche3453f62009-06-18 07:03:47 +0000733 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000736static inline void get_interrupt_interval(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Michael Buesche3453f62009-06-18 07:03:47 +0000738 u16 data;
739 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Michael Buesche3453f62009-06-18 07:03:47 +0000741 read_eprom_word(pegasus, 4, &data);
742 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700743 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000744 if (interval < 0x80) {
Joe Perchesa475f602010-02-17 10:30:24 +0000745 netif_info(pegasus, timer, pegasus->net,
746 "intr interval changed from %ums to %ums\n",
747 interval, 0x80);
Michael Buesche3453f62009-06-18 07:03:47 +0000748 interval = 0x80;
749 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700750#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000751 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Michael Buesche3453f62009-06-18 07:03:47 +0000755 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static void set_carrier(struct net_device *net)
759{
760 pegasus_t *pegasus = netdev_priv(net);
761 u16 tmp;
762
Dan Williamsc43c49b2007-04-24 10:20:06 -0400763 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (tmp & BMSR_LSTATUS)
767 netif_carrier_on(net);
768 else
769 netif_carrier_off(net);
770}
771
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000772static void free_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 usb_free_urb(pegasus->intr_urb);
775 usb_free_urb(pegasus->tx_urb);
776 usb_free_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000779static void unlink_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 usb_kill_urb(pegasus->intr_urb);
782 usb_kill_urb(pegasus->tx_urb);
783 usb_kill_urb(pegasus->rx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000786static int alloc_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Petko Manolov323b3492013-04-25 22:41:50 +0000788 int res = -ENOMEM;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
791 if (!pegasus->rx_urb) {
Petko Manolov323b3492013-04-25 22:41:50 +0000792 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
795 if (!pegasus->tx_urb) {
796 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000797 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
800 if (!pegasus->intr_urb) {
801 usb_free_urb(pegasus->tx_urb);
802 usb_free_urb(pegasus->rx_urb);
Petko Manolov323b3492013-04-25 22:41:50 +0000803 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
Petko Manolov323b3492013-04-25 22:41:50 +0000806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static int pegasus_open(struct net_device *net)
810{
811 pegasus_t *pegasus = netdev_priv(net);
Petko Manolov323b3492013-04-25 22:41:50 +0000812 int res=-ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000815 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
816 PEGASUS_MTU,
817 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!pegasus->rx_skb)
Petko Manolov323b3492013-04-25 22:41:50 +0000819 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 res = set_registers(pegasus, EthID, 6, net->dev_addr);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
824 usb_rcvbulkpipe(pegasus->usb, 1),
825 pegasus->rx_skb->data, PEGASUS_MTU + 8,
826 read_bulk_callback, pegasus);
827 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700828 if (res == -ENODEV)
829 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000830 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto exit;
832 }
833
834 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
835 usb_rcvintpipe(pegasus->usb, 3),
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000836 pegasus->intr_buff, sizeof(pegasus->intr_buff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 intr_callback, pegasus, pegasus->intr_interval);
838 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700839 if (res == -ENODEV)
840 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000841 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 usb_kill_urb(pegasus->rx_urb);
843 goto exit;
844 }
Petko Manolov323b3492013-04-25 22:41:50 +0000845 res = enable_net_traffic(net, pegasus->usb);
846 if (res < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000847 netif_dbg(pegasus, ifup, net,
848 "can't enable_net_traffic() - %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 res = -EIO;
850 usb_kill_urb(pegasus->rx_urb);
851 usb_kill_urb(pegasus->intr_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto exit;
853 }
854 set_carrier(net);
855 netif_start_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000856 netif_dbg(pegasus, ifup, net, "open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 res = 0;
858exit:
859 return res;
860}
861
862static int pegasus_close(struct net_device *net)
863{
864 pegasus_t *pegasus = netdev_priv(net);
865
866 netif_stop_queue(net);
867 if (!(pegasus->flags & PEGASUS_UNPLUG))
868 disable_net_traffic(pegasus);
869 tasklet_kill(&pegasus->rx_tl);
870 unlink_all_urbs(pegasus);
871
872 return 0;
873}
874
875static void pegasus_get_drvinfo(struct net_device *dev,
876 struct ethtool_drvinfo *info)
877{
878 pegasus_t *pegasus = netdev_priv(dev);
Jiri Pirko7826d432013-01-06 00:44:26 +0000879
880 strlcpy(info->driver, driver_name, sizeof(info->driver));
881 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000882 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/* also handles three patterns of some kind in hardware */
886#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
887
888static void
889pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
890{
891 pegasus_t *pegasus = netdev_priv(dev);
892
893 wol->supported = WAKE_MAGIC | WAKE_PHY;
894 wol->wolopts = pegasus->wolopts;
895}
896
897static int
898pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
899{
900 pegasus_t *pegasus = netdev_priv(dev);
901 u8 reg78 = 0x04;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000902 int ret;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (wol->wolopts & ~WOL_SUPPORTED)
905 return -EINVAL;
906
907 if (wol->wolopts & WAKE_MAGIC)
908 reg78 |= 0x80;
909 if (wol->wolopts & WAKE_PHY)
910 reg78 |= 0x40;
911 /* FIXME this 0x10 bit still needs to get set in the chip... */
912 if (wol->wolopts)
913 pegasus->eth_regs[0] |= 0x10;
914 else
915 pegasus->eth_regs[0] &= ~0x10;
916 pegasus->wolopts = wol->wolopts;
Ming Lei4fbc5b22013-01-19 01:32:01 +0000917
918 ret = set_register(pegasus, WakeupControl, reg78);
919 if (!ret)
920 ret = device_set_wakeup_enable(&pegasus->usb->dev,
921 wol->wolopts);
922 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925static inline void pegasus_reset_wol(struct net_device *dev)
926{
927 struct ethtool_wolinfo wol;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 memset(&wol, 0, sizeof wol);
930 (void) pegasus_set_wol(dev, &wol);
931}
932
933static int
934pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
935{
936 pegasus_t *pegasus;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 pegasus = netdev_priv(dev);
939 mii_ethtool_gset(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
943static int
944pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
945{
946 pegasus_t *pegasus = netdev_priv(dev);
947 return mii_ethtool_sset(&pegasus->mii, ecmd);
948}
949
950static int pegasus_nway_reset(struct net_device *dev)
951{
952 pegasus_t *pegasus = netdev_priv(dev);
953 return mii_nway_restart(&pegasus->mii);
954}
955
956static u32 pegasus_get_link(struct net_device *dev)
957{
958 pegasus_t *pegasus = netdev_priv(dev);
959 return mii_link_ok(&pegasus->mii);
960}
961
962static u32 pegasus_get_msglevel(struct net_device *dev)
963{
964 pegasus_t *pegasus = netdev_priv(dev);
965 return pegasus->msg_enable;
966}
967
968static void pegasus_set_msglevel(struct net_device *dev, u32 v)
969{
970 pegasus_t *pegasus = netdev_priv(dev);
971 pegasus->msg_enable = v;
972}
973
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700974static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 .get_drvinfo = pegasus_get_drvinfo,
976 .get_settings = pegasus_get_settings,
977 .set_settings = pegasus_set_settings,
978 .nway_reset = pegasus_nway_reset,
979 .get_link = pegasus_get_link,
980 .get_msglevel = pegasus_get_msglevel,
981 .set_msglevel = pegasus_set_msglevel,
982 .get_wol = pegasus_get_wol,
983 .set_wol = pegasus_set_wol,
984};
985
986static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
987{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000988 __u16 *data = (__u16 *) &rq->ifr_ifru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 pegasus_t *pegasus = netdev_priv(net);
990 int res;
991
992 switch (cmd) {
993 case SIOCDEVPRIVATE:
994 data[0] = pegasus->phy;
995 case SIOCDEVPRIVATE + 1:
996 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
997 res = 0;
998 break;
999 case SIOCDEVPRIVATE + 2:
1000 if (!capable(CAP_NET_ADMIN))
1001 return -EPERM;
Petko Manolov2bd64702013-04-25 22:41:36 +00001002 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 res = 0;
1004 break;
1005 default:
1006 res = -EOPNOTSUPP;
1007 }
1008 return res;
1009}
1010
1011static void pegasus_set_multicast(struct net_device *net)
1012{
1013 pegasus_t *pegasus = netdev_priv(net);
1014
1015 if (net->flags & IFF_PROMISC) {
1016 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001017 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001018 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1020 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001021 netif_dbg(pegasus, link, net, "set allmulti\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1024 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1025 }
Petko Manolov323b3492013-04-25 22:41:50 +00001026 update_eth_regs_async(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001029static __u8 mii_phy_probe(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 int i;
1032 __u16 tmp;
1033
1034 for (i = 0; i < 32; i++) {
1035 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1036 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1037 continue;
1038 else
1039 return i;
1040 }
1041
1042 return 0xff;
1043}
1044
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001045static inline void setup_pegasus_II(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 __u8 data = 0xa5;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001048
Petko Manolov4a1728a2005-11-15 09:48:23 +02001049 set_register(pegasus, Reg1d, 0);
1050 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 mdelay(100);
1052 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001053 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001055 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Petko Manolov4a1728a2005-11-15 09:48:23 +02001057 set_register(pegasus, 0x83, data);
1058 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001060 if (data == 0xa5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 pegasus->chip = 0x8513;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001062 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 pegasus->chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Petko Manolov4a1728a2005-11-15 09:48:23 +02001065 set_register(pegasus, 0x80, 0xc0);
1066 set_register(pegasus, 0x83, 0xff);
1067 set_register(pegasus, 0x84, 0x01);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001070 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001072 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075
David Brownellcda28362008-11-16 00:36:08 -08001076static int pegasus_count;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001077static struct workqueue_struct *pegasus_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078#define CARRIER_CHECK_DELAY (2 * HZ)
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)) {
1085 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1086 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
David Brownellcda28362008-11-16 00:36:08 -08001106/* we rely on probe() and remove() being serialized so we
1107 * don't need extra locking on pegasus_count.
1108 */
1109static void pegasus_dec_workqueue(void)
1110{
1111 pegasus_count--;
1112 if (pegasus_count == 0) {
1113 destroy_workqueue(pegasus_workqueue);
1114 pegasus_workqueue = NULL;
1115 }
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static int pegasus_probe(struct usb_interface *intf,
1119 const struct usb_device_id *id)
1120{
1121 struct usb_device *dev = interface_to_usbdev(intf);
1122 struct net_device *net;
1123 pegasus_t *pegasus;
1124 int dev_index = id - pegasus_ids;
1125 int res = -ENOMEM;
1126
David Brownellcda28362008-11-16 00:36:08 -08001127 if (pegasus_blacklisted(dev))
1128 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001129
David Brownellcda28362008-11-16 00:36:08 -08001130 if (pegasus_count == 0) {
1131 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1132 if (!pegasus_workqueue)
1133 return -ENOMEM;
Ben Collins4f631352008-07-30 12:39:02 -07001134 }
David Brownellcda28362008-11-16 00:36:08 -08001135 pegasus_count++;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 net = alloc_etherdev(sizeof(struct pegasus));
Joe Perches41de8d42012-01-29 13:47:52 +00001138 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 pegasus->dev_index = dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Petko Manolov323b3492013-04-25 22:41:50 +00001144 res = alloc_urbs(pegasus);
1145 if (res < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1147 goto out1;
1148 }
1149
1150 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1151
David Howellsc4028952006-11-22 14:57:56 +00001152 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 pegasus->intf = intf;
1155 pegasus->usb = dev;
1156 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001157
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001160 net->netdev_ops = &pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 SET_ETHTOOL_OPS(net, &ops);
1162 pegasus->mii.dev = net;
1163 pegasus->mii.mdio_read = mdio_read;
1164 pegasus->mii.mdio_write = mdio_write;
1165 pegasus->mii.phy_id_mask = 0x1f;
1166 pegasus->mii.reg_num_mask = 0x1f;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001167 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1169
1170 pegasus->features = usb_dev_id[dev_index].private;
1171 get_interrupt_interval(pegasus);
1172 if (reset_mac(pegasus)) {
1173 dev_err(&intf->dev, "can't reset MAC\n");
1174 res = -EIO;
1175 goto out2;
1176 }
1177 set_ethernet_addr(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (pegasus->features & PEGASUS_II) {
1179 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1180 setup_pegasus_II(pegasus);
1181 }
1182 pegasus->phy = mii_phy_probe(pegasus);
1183 if (pegasus->phy == 0xff) {
1184 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1185 pegasus->phy = 1;
1186 }
1187 pegasus->mii.phy_id = pegasus->phy;
1188 usb_set_intfdata(intf, pegasus);
1189 SET_NETDEV_DEV(net, &intf->dev);
1190 pegasus_reset_wol(net);
1191 res = register_netdev(net);
1192 if (res)
1193 goto out3;
1194 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
Petko Manolov323b3492013-04-25 22:41:50 +00001195 CARRIER_CHECK_DELAY);
1196 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1197 usb_dev_id[dev_index].name, net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199
1200out3:
1201 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202out2:
1203 free_all_urbs(pegasus);
1204out1:
1205 free_netdev(net);
1206out:
David Brownellcda28362008-11-16 00:36:08 -08001207 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return res;
1209}
1210
1211static void pegasus_disconnect(struct usb_interface *intf)
1212{
1213 struct pegasus *pegasus = usb_get_intfdata(intf);
1214
1215 usb_set_intfdata(intf, NULL);
1216 if (!pegasus) {
1217 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1218 return;
1219 }
1220
1221 pegasus->flags |= PEGASUS_UNPLUG;
1222 cancel_delayed_work(&pegasus->carrier_check);
1223 unregister_netdev(pegasus->net);
Kevin Vigora85a46f2005-09-22 00:49:24 -07001224 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 free_all_urbs(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001226 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001228 pegasus->rx_skb = NULL;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 free_netdev(pegasus->net);
David Brownellcda28362008-11-16 00:36:08 -08001231 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001234static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 struct pegasus *pegasus = usb_get_intfdata(intf);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001237
1238 netif_device_detach(pegasus->net);
David Brownell7e713b82006-05-01 14:02:45 -07001239 cancel_delayed_work(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001240 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001241 usb_kill_urb(pegasus->rx_urb);
1242 usb_kill_urb(pegasus->intr_urb);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001247static int pegasus_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct pegasus *pegasus = usb_get_intfdata(intf);
1250
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001251 netif_device_attach(pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001252 if (netif_running(pegasus->net)) {
1253 pegasus->rx_urb->status = 0;
1254 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001255 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001256
1257 pegasus->intr_urb->status = 0;
1258 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001259 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001260 }
David Brownell7e713b82006-05-01 14:02:45 -07001261 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1262 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
Oliver Neukum8cb89572009-01-08 11:22:25 -08001266static const struct net_device_ops pegasus_netdev_ops = {
1267 .ndo_open = pegasus_open,
1268 .ndo_stop = pegasus_close,
1269 .ndo_do_ioctl = pegasus_ioctl,
1270 .ndo_start_xmit = pegasus_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001271 .ndo_set_rx_mode = pegasus_set_multicast,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001272 .ndo_get_stats = pegasus_netdev_stats,
1273 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001274 .ndo_change_mtu = eth_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00001275 .ndo_set_mac_address = eth_mac_addr,
1276 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001277};
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279static struct usb_driver pegasus_driver = {
1280 .name = driver_name,
1281 .probe = pegasus_probe,
1282 .disconnect = pegasus_disconnect,
1283 .id_table = pegasus_ids,
1284 .suspend = pegasus_suspend,
1285 .resume = pegasus_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001286 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287};
1288
David Brownellcda28362008-11-16 00:36:08 -08001289static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001290{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001291 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1292 char *token, *name = NULL;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001293
1294 if ((token = strsep(&id, ":")) != NULL)
1295 name = token;
1296 /* name now points to a null terminated string*/
1297 if ((token = strsep(&id, ":")) != NULL)
1298 vendor_id = simple_strtoul(token, NULL, 16);
1299 if ((token = strsep(&id, ":")) != NULL)
1300 device_id = simple_strtoul(token, NULL, 16);
1301 flags = simple_strtoul(id, NULL, 16);
1302 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 +00001303 driver_name, name, vendor_id, device_id, flags);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001304
1305 if (vendor_id > 0x10000 || vendor_id == 0)
1306 return;
1307 if (device_id > 0x10000 || device_id == 0)
1308 return;
1309
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001310 for (i = 0; usb_dev_id[i].name; i++);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001311 usb_dev_id[i].name = name;
1312 usb_dev_id[i].vendor = vendor_id;
1313 usb_dev_id[i].device = device_id;
1314 usb_dev_id[i].private = flags;
1315 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1316 pegasus_ids[i].idVendor = vendor_id;
1317 pegasus_ids[i].idProduct = device_id;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320static int __init pegasus_init(void)
1321{
1322 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001323 if (devid)
1324 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return usb_register(&pegasus_driver);
1326}
1327
1328static void __exit pegasus_exit(void)
1329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 usb_deregister(&pegasus_driver);
1331}
1332
1333module_init(pegasus_init);
1334module_exit(pegasus_exit);