blob: 352b4dcee61c34da8cc293330a549dc15e833461 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
3 *
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.
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>
42#include <asm/uaccess.h>
43#include "pegasus.h"
44
45/*
46 * Version Information
47 */
Petko Manolov37cf3472006-09-27 14:25:37 -070048#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50#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)
57
Rusty Russelleb939922011-12-19 14:08:01 +000058static bool loopback;
59static bool mii_mode;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +000060static char *devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static struct usb_eth_dev usb_dev_id[] = {
63#define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
Chris Rankinab854b22009-10-13 00:32:02 -070065#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
66 PEGASUS_DEV(pn, vid, pid, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "pegasus.h"
68#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070069#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020070 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 {NULL, 0, 0, 0}
72};
73
74static struct usb_device_id pegasus_ids[] = {
75#define PEGASUS_DEV(pn, vid, pid, flags) \
76 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
Chris Rankinab854b22009-10-13 00:32:02 -070077/*
78 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
79 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
80 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
81 * case anyway, seeing as the pegasus is for "Wired" adaptors.
82 */
83#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
84 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
85 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "pegasus.h"
87#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070088#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020089 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 {}
91};
92
93MODULE_AUTHOR(DRIVER_AUTHOR);
94MODULE_DESCRIPTION(DRIVER_DESC);
95MODULE_LICENSE("GPL");
96module_param(loopback, bool, 0);
97module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020098module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
100MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +0200101MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* use ethtool to change the level for any given device */
104static int msg_level = -1;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000105module_param(msg_level, int, 0);
106MODULE_PARM_DESC(msg_level, "Override default message level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108MODULE_DEVICE_TABLE(usb, pegasus_ids);
Oliver Neukum8cb89572009-01-08 11:22:25 -0800109static const struct net_device_ops pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int update_eth_regs_async(pegasus_t *);
112/* Aargh!!! I _really_ hate such tweaks */
David Howells7d12e782006-10-05 14:55:46 +0100113static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 pegasus_t *pegasus = urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800116 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (!pegasus)
119 return;
120
Oliver Neukumc94cb312008-12-18 23:00:59 -0800121 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 case 0:
123 if (pegasus->flags & ETH_REGS_CHANGE) {
124 pegasus->flags &= ~ETH_REGS_CHANGE;
125 pegasus->flags |= ETH_REGS_CHANGED;
126 update_eth_regs_async(pegasus);
127 return;
128 }
129 break;
130 case -EINPROGRESS:
131 return;
132 case -ENOENT:
133 break;
134 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000135 if (net_ratelimit())
136 netif_dbg(pegasus, drv, pegasus->net,
137 "%s, status %d\n", __func__, status);
138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 pegasus->flags &= ~ETH_REGS_CHANGED;
141 wake_up(&pegasus->ctrl_wait);
142}
143
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000144static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 void *data)
146{
147 int ret;
148 char *buffer;
149 DECLARE_WAITQUEUE(wait, current);
150
151 buffer = kmalloc(size, GFP_KERNEL);
Joe Perches14f8dc42013-02-07 11:46:27 +0000152 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -ENOMEM;
Joe Perches14f8dc42013-02-07 11:46:27 +0000154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 add_wait_queue(&pegasus->ctrl_wait, &wait);
156 set_current_state(TASK_UNINTERRUPTIBLE);
157 while (pegasus->flags & ETH_REGS_CHANGED)
158 schedule();
159 remove_wait_queue(&pegasus->ctrl_wait, &wait);
160 set_current_state(TASK_RUNNING);
161
162 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
163 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
164 pegasus->dr.wValue = cpu_to_le16(0);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700165 pegasus->dr.wIndex = cpu_to_le16(indx);
166 pegasus->dr.wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pegasus->ctrl_urb->transfer_buffer_length = size;
168
169 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
170 usb_rcvctrlpipe(pegasus->usb, 0),
171 (char *) &pegasus->dr,
172 buffer, size, ctrl_callback, pegasus);
173
174 add_wait_queue(&pegasus->ctrl_wait, &wait);
175 set_current_state(TASK_UNINTERRUPTIBLE);
176
177 /* using ATOMIC, we'd never wake up if we slept */
178 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
Oliver Neukum10c82112006-11-23 15:40:17 +0100179 set_current_state(TASK_RUNNING);
David Brownell955a2602006-05-26 10:17:03 -0700180 if (ret == -ENODEV)
181 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000182 if (net_ratelimit())
183 netif_err(pegasus, drv, pegasus->net,
184 "%s, status %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 }
187
188 schedule();
189out:
190 remove_wait_queue(&pegasus->ctrl_wait, &wait);
191 memcpy(data, buffer, size);
192 kfree(buffer);
193
194 return ret;
195}
196
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000197static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 void *data)
199{
200 int ret;
201 char *buffer;
202 DECLARE_WAITQUEUE(wait, current);
203
Julia Lawall175c0442010-05-15 11:18:58 +0000204 buffer = kmemdup(data, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!buffer) {
Joe Perchesa475f602010-02-17 10:30:24 +0000206 netif_warn(pegasus, drv, pegasus->net,
207 "out of memory in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOMEM;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 add_wait_queue(&pegasus->ctrl_wait, &wait);
212 set_current_state(TASK_UNINTERRUPTIBLE);
213 while (pegasus->flags & ETH_REGS_CHANGED)
214 schedule();
215 remove_wait_queue(&pegasus->ctrl_wait, &wait);
216 set_current_state(TASK_RUNNING);
217
218 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
219 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
220 pegasus->dr.wValue = cpu_to_le16(0);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700221 pegasus->dr.wIndex = cpu_to_le16(indx);
222 pegasus->dr.wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 pegasus->ctrl_urb->transfer_buffer_length = size;
224
225 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
226 usb_sndctrlpipe(pegasus->usb, 0),
227 (char *) &pegasus->dr,
228 buffer, size, ctrl_callback, pegasus);
229
230 add_wait_queue(&pegasus->ctrl_wait, &wait);
231 set_current_state(TASK_UNINTERRUPTIBLE);
232
233 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700234 if (ret == -ENODEV)
235 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000236 netif_err(pegasus, drv, pegasus->net,
237 "%s, status %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out;
239 }
240
241 schedule();
242out:
243 remove_wait_queue(&pegasus->ctrl_wait, &wait);
244 kfree(buffer);
245
246 return ret;
247}
248
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000249static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 int ret;
252 char *tmp;
253 DECLARE_WAITQUEUE(wait, current);
254
Julia Lawall175c0442010-05-15 11:18:58 +0000255 tmp = kmemdup(&data, 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!tmp) {
Joe Perchesa475f602010-02-17 10:30:24 +0000257 netif_warn(pegasus, drv, pegasus->net,
258 "out of memory in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -ENOMEM;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 add_wait_queue(&pegasus->ctrl_wait, &wait);
262 set_current_state(TASK_UNINTERRUPTIBLE);
263 while (pegasus->flags & ETH_REGS_CHANGED)
264 schedule();
265 remove_wait_queue(&pegasus->ctrl_wait, &wait);
266 set_current_state(TASK_RUNNING);
267
268 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
269 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
270 pegasus->dr.wValue = cpu_to_le16(data);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700271 pegasus->dr.wIndex = cpu_to_le16(indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pegasus->dr.wLength = cpu_to_le16(1);
273 pegasus->ctrl_urb->transfer_buffer_length = 1;
274
275 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
276 usb_sndctrlpipe(pegasus->usb, 0),
277 (char *) &pegasus->dr,
Petko Manolov016534c2006-03-30 09:59:22 +0300278 tmp, 1, ctrl_callback, pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 add_wait_queue(&pegasus->ctrl_wait, &wait);
281 set_current_state(TASK_UNINTERRUPTIBLE);
282
283 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700284 if (ret == -ENODEV)
285 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000286 if (net_ratelimit())
287 netif_err(pegasus, drv, pegasus->net,
288 "%s, status %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto out;
290 }
291
292 schedule();
293out:
294 remove_wait_queue(&pegasus->ctrl_wait, &wait);
295 kfree(tmp);
296
297 return ret;
298}
299
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000300static int update_eth_regs_async(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 int ret;
303
304 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
305 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
Michael Buesche3453f62009-06-18 07:03:47 +0000306 pegasus->dr.wValue = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
308 pegasus->dr.wLength = cpu_to_le16(3);
309 pegasus->ctrl_urb->transfer_buffer_length = 3;
310
311 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
312 usb_sndctrlpipe(pegasus->usb, 0),
313 (char *) &pegasus->dr,
314 pegasus->eth_regs, 3, ctrl_callback, pegasus);
315
David Brownell955a2602006-05-26 10:17:03 -0700316 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
317 if (ret == -ENODEV)
318 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000319 netif_err(pegasus, drv, pegasus->net,
320 "%s, status %d\n", __func__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 return ret;
324}
325
Petko Manolov2bd64702013-04-25 22:41:36 +0000326static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 int i;
329 __u8 data[4] = { phy, 0, 0, indx };
330 __le16 regdi;
Petko Manolov2bd64702013-04-25 22:41:36 +0000331 int ret = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Petko Manolov2bd64702013-04-25 22:41:36 +0000333 if (cmd & PHY_WRITE) {
334 __le16 *t = (__le16 *) & data[1];
335 *t = cpu_to_le16(*regd);
336 }
337 set_register(p, PhyCtrl, 0);
338 set_registers(p, PhyAddr, sizeof(data), data);
339 set_register(p, PhyCtrl, (indx | cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov2bd64702013-04-25 22:41:36 +0000341 ret = get_registers(p, PhyCtrl, 1, data);
342 if (ret < 0)
David Brownell7e713b82006-05-01 14:02:45 -0700343 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (data[0] & PHY_DONE)
345 break;
346 }
Joe Perchesa475f602010-02-17 10:30:24 +0000347 if (i >= REG_TIMEOUT)
348 goto fail;
Petko Manolov2bd64702013-04-25 22:41:36 +0000349 if (cmd & PHY_READ) {
350 ret = get_registers(p, PhyData, 2, &regdi);
351 *regd = le16_to_cpu(regdi);
352 return ret;
353 }
354 return 0;
David Brownell7e713b82006-05-01 14:02:45 -0700355fail:
Petko Manolov2bd64702013-04-25 22:41:36 +0000356 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Petko Manolov2bd64702013-04-25 22:41:36 +0000360/* Returns non-negative int on success, error on failure */
361static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
362{
363 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
364}
365
366/* Returns zero on success, error on failure */
367static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
368{
369 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static int mdio_read(struct net_device *dev, int phy_id, int loc)
373{
Joe Perches8739cfe2010-11-15 11:12:29 +0000374 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 u16 res;
376
377 read_mii_word(pegasus, phy_id, loc, &res);
378 return (int)res;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
382{
Joe Perches8739cfe2010-11-15 11:12:29 +0000383 pegasus_t *pegasus = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Petko Manolov2bd64702013-04-25 22:41:36 +0000385 write_mii_word(pegasus, phy_id, loc, (__u16 *)&val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000388static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 int i;
391 __u8 tmp;
392 __le16 retdatai;
393 int ret;
394
Petko Manolov4a1728a2005-11-15 09:48:23 +0200395 set_register(pegasus, EpromCtrl, 0);
396 set_register(pegasus, EpromOffset, index);
397 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 for (i = 0; i < REG_TIMEOUT; i++) {
400 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
401 if (tmp & EPROM_DONE)
402 break;
David Brownell7e713b82006-05-01 14:02:45 -0700403 if (ret == -ESHUTDOWN)
404 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Joe Perchesa475f602010-02-17 10:30:24 +0000406 if (i >= REG_TIMEOUT)
407 goto fail;
408
409 ret = get_registers(pegasus, EpromData, 2, &retdatai);
410 *retdata = le16_to_cpu(retdatai);
411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Brownell7e713b82006-05-01 14:02:45 -0700413fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000414 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200415 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418#ifdef PEGASUS_WRITE_EEPROM
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000419static inline void enable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Petko Manolov4a1728a2005-11-15 09:48:23 +0200423 get_registers(pegasus, EthCtrl2, 1, &tmp);
424 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000427static inline void disable_eprom_write(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 __u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Petko Manolov4a1728a2005-11-15 09:48:23 +0200431 get_registers(pegasus, EthCtrl2, 1, &tmp);
432 set_register(pegasus, EpromCtrl, 0);
433 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000436static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int i;
439 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
440 int ret;
Michael Buesche3453f62009-06-18 07:03:47 +0000441 __le16 le_data = cpu_to_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Petko Manolov4a1728a2005-11-15 09:48:23 +0200443 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200445 set_register(pegasus, EpromOffset, index);
Michael Buesche3453f62009-06-18 07:03:47 +0000446 set_registers(pegasus, EpromData, 2, &le_data);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200447 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 for (i = 0; i < REG_TIMEOUT; i++) {
450 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700451 if (ret == -ESHUTDOWN)
452 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (tmp & EPROM_DONE)
454 break;
455 }
456 disable_eprom_write(pegasus);
Joe Perchesa475f602010-02-17 10:30:24 +0000457 if (i >= REG_TIMEOUT)
458 goto fail;
459
460 return ret;
461
David Brownell7e713b82006-05-01 14:02:45 -0700462fail:
Joe Perchesa475f602010-02-17 10:30:24 +0000463 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200464 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466#endif /* PEGASUS_WRITE_EEPROM */
467
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000468static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int i;
471 __u16 w16;
472
473 for (i = 0; i < 3; i++) {
474 read_eprom_word(pegasus, i, &w16);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700475 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477}
478
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000479static void set_ethernet_addr(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Petko Manolov37cf3472006-09-27 14:25:37 -0700483 if (pegasus->features & PEGASUS_II) {
484 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
485 } else {
486 get_node_id(pegasus, node_id);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000487 set_registers(pegasus, EthID, sizeof(node_id), node_id);
Petko Manolov37cf3472006-09-27 14:25:37 -0700488 }
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000489 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000492static inline int reset_mac(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 __u8 data = 0x8;
495 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Petko Manolov4a1728a2005-11-15 09:48:23 +0200497 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200499 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (~data & 0x08) {
Dan Carpenter681f1622011-12-23 00:44:36 +0000501 if (loopback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200504 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200506 set_register(pegasus, Gpio1, 0x26);
507 set_register(pegasus, Gpio0, pegasus->features);
508 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510 }
511 }
512 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200513 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
516 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200517 set_register(pegasus, Gpio0, 0x24);
518 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
521 __u16 auxmode;
522 read_mii_word(pegasus, 3, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000523 auxmode |= 4;
524 write_mii_word(pegasus, 3, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
527 return 0;
528}
529
530static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
531{
532 __u16 linkpart;
533 __u8 data[4];
534 pegasus_t *pegasus = netdev_priv(dev);
535 int ret;
536
537 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
538 data[0] = 0xc9;
539 data[1] = 0;
540 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
541 data[1] |= 0x20; /* set full duplex */
542 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
543 data[1] |= 0x10; /* set 100 Mbps */
544 if (mii_mode)
545 data[1] = 0;
Dan Carpenter681f1622011-12-23 00:44:36 +0000546 data[2] = loopback ? 0x09 : 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000548 memcpy(pegasus->eth_regs, data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ret = set_registers(pegasus, EthCtrl0, 3, data);
550
551 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100552 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
554 u16 auxmode;
555 read_mii_word(pegasus, 0, 0x1b, &auxmode);
Petko Manolov2bd64702013-04-25 22:41:36 +0000556 auxmode |= 4;
557 write_mii_word(pegasus, 0, 0x1b, &auxmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Petko Manolov4a1728a2005-11-15 09:48:23 +0200560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
David Howells7d12e782006-10-05 14:55:46 +0100563static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 pegasus_t *pegasus = urb->context;
566 struct net_device *net;
567 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800568 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 u8 *buf = urb->transfer_buffer;
570 __u16 pkt_len;
571
572 if (!pegasus)
573 return;
574
575 net = pegasus->net;
576 if (!netif_device_present(net) || !netif_running(net))
577 return;
578
Oliver Neukumc94cb312008-12-18 23:00:59 -0800579 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 case 0:
581 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700582 case -ETIME:
Joe Perchesa475f602010-02-17 10:30:24 +0000583 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 pegasus->flags &= ~PEGASUS_RX_BUSY;
585 break;
586 case -EPIPE: /* stall, or disconnect from TT */
587 /* FIXME schedule work to clear the halt */
Joe Perchesa475f602010-02-17 10:30:24 +0000588 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590 case -ENOENT:
591 case -ECONNRESET:
592 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000593 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000596 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto goon;
598 }
599
600 if (!count || count < 4)
601 goto goon;
602
603 rx_status = buf[count - 2];
604 if (rx_status & 0x1e) {
Joe Perchesa475f602010-02-17 10:30:24 +0000605 netif_dbg(pegasus, rx_err, net,
606 "RX packet error %x\n", rx_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 pegasus->stats.rx_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000608 if (rx_status & 0x06) /* long or runt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 pegasus->stats.rx_length_errors++;
610 if (rx_status & 0x08)
611 pegasus->stats.rx_crc_errors++;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000612 if (rx_status & 0x10) /* extra bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 pegasus->stats.rx_frame_errors++;
614 goto goon;
615 }
616 if (pegasus->chip == 0x8513) {
617 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
618 pkt_len &= 0x0fff;
619 pegasus->rx_skb->data += 2;
620 } else {
621 pkt_len = buf[count - 3] << 8;
622 pkt_len += buf[count - 4];
623 pkt_len &= 0xfff;
624 pkt_len -= 8;
625 }
626
627 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700628 * If the packet is unreasonably long, quietly drop it rather than
629 * kernel panicing by calling skb_put.
630 */
631 if (pkt_len > PEGASUS_MTU)
632 goto goon;
633
634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * at this point we are sure pegasus->rx_skb != NULL
636 * so we go ahead and pass up the packet.
637 */
638 skb_put(pegasus->rx_skb, pkt_len);
639 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
640 netif_rx(pegasus->rx_skb);
641 pegasus->stats.rx_packets++;
642 pegasus->stats.rx_bytes += pkt_len;
643
644 if (pegasus->flags & PEGASUS_UNPLUG)
645 return;
646
Petko Manolov313a58e2013-04-25 22:41:21 +0000647 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
648 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (pegasus->rx_skb == NULL)
651 goto tl_sched;
652goon:
653 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
654 usb_rcvbulkpipe(pegasus->usb, 1),
655 pegasus->rx_skb->data, PEGASUS_MTU + 8,
656 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700657 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
658 if (rx_status == -ENODEV)
659 netif_device_detach(pegasus->net);
660 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 pegasus->flags |= PEGASUS_RX_URB_FAIL;
662 goto tl_sched;
663 } else {
664 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
665 }
666
667 return;
668
669tl_sched:
670 tasklet_schedule(&pegasus->rx_tl);
671}
672
673static void rx_fixup(unsigned long data)
674{
675 pegasus_t *pegasus;
David Brownell955a2602006-05-26 10:17:03 -0700676 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 pegasus = (pegasus_t *) data;
679 if (pegasus->flags & PEGASUS_UNPLUG)
680 return;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
683 if (pegasus->rx_skb)
684 goto try_again;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000685 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000686 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
687 PEGASUS_MTU,
688 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (pegasus->rx_skb == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000690 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 tasklet_schedule(&pegasus->rx_tl);
Petko Manolov313a58e2013-04-25 22:41:21 +0000692 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
695 usb_rcvbulkpipe(pegasus->usb, 1),
696 pegasus->rx_skb->data, PEGASUS_MTU + 8,
697 read_bulk_callback, pegasus);
698try_again:
David Brownell955a2602006-05-26 10:17:03 -0700699 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
700 if (status == -ENODEV)
701 netif_device_detach(pegasus->net);
702 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 pegasus->flags |= PEGASUS_RX_URB_FAIL;
704 tasklet_schedule(&pegasus->rx_tl);
705 } else {
706 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
David Howells7d12e782006-10-05 14:55:46 +0100710static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800713 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800714 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (!pegasus)
717 return;
718
Micah Gruber93519822007-07-23 16:05:52 +0800719 net = pegasus->net;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!netif_device_present(net) || !netif_running(net))
722 return;
723
Oliver Neukumc94cb312008-12-18 23:00:59 -0800724 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case -EPIPE:
726 /* FIXME schedule_work() to clear the tx halt */
727 netif_stop_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000728 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730 case -ENOENT:
731 case -ECONNRESET:
732 case -ESHUTDOWN:
Joe Perchesa475f602010-02-17 10:30:24 +0000733 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return;
735 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000736 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* FALL THROUGH */
738 case 0:
739 break;
740 }
741
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700742 net->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 netif_wake_queue(net);
744}
745
David Howells7d12e782006-10-05 14:55:46 +0100746static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 pegasus_t *pegasus = urb->context;
749 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800750 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (!pegasus)
753 return;
754 net = pegasus->net;
755
Oliver Neukumc94cb312008-12-18 23:00:59 -0800756 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case 0:
758 break;
759 case -ECONNRESET: /* unlink */
760 case -ENOENT:
761 case -ESHUTDOWN:
762 return;
763 default:
764 /* some Pegasus-I products report LOTS of data
765 * toggle errors... avoid log spamming
766 */
Joe Perchesa475f602010-02-17 10:30:24 +0000767 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 if (urb->actual_length >= 6) {
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000771 u8 *d = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* byte 0 == tx_status1, reg 2B */
774 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
775 |LATE_COL|JABBER_TIMEOUT)) {
776 pegasus->stats.tx_errors++;
777 if (d[0] & TX_UNDERRUN)
778 pegasus->stats.tx_fifo_errors++;
779 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
780 pegasus->stats.tx_aborted_errors++;
781 if (d[0] & LATE_COL)
782 pegasus->stats.tx_window_errors++;
783 }
784
785 /* d[5].LINK_STATUS lies on some adapters.
786 * d[0].NO_CARRIER kicks in only with failed TX.
787 * ... so monitoring with MII may be safest.
788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
791 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
792 }
793
Oliver Neukumc94cb312008-12-18 23:00:59 -0800794 res = usb_submit_urb(urb, GFP_ATOMIC);
795 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700796 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000797 if (res)
798 netif_err(pegasus, timer, net,
799 "can't resubmit interrupt urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802static void pegasus_tx_timeout(struct net_device *net)
803{
804 pegasus_t *pegasus = netdev_priv(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000805 netif_warn(pegasus, timer, net, "tx timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 usb_unlink_urb(pegasus->tx_urb);
807 pegasus->stats.tx_errors++;
808}
809
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000810static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
811 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 pegasus_t *pegasus = netdev_priv(net);
814 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
815 int res;
816 __u16 l16 = skb->len;
817
818 netif_stop_queue(net);
819
820 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300821 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
823 usb_sndbulkpipe(pegasus->usb, 2),
824 pegasus->tx_buff, count,
825 write_bulk_callback, pegasus);
826 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +0000827 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 switch (res) {
829 case -EPIPE: /* stall, or disconnect from TT */
830 /* cleanup should already have been scheduled */
831 break;
832 case -ENODEV: /* disconnect() upcoming */
Oliver Neukum9dd014e2009-04-17 01:40:19 -0700833 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700834 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
836 default:
837 pegasus->stats.tx_errors++;
838 netif_start_queue(net);
839 }
840 } else {
841 pegasus->stats.tx_packets++;
842 pegasus->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 dev_kfree_skb(skb);
845
Patrick McHardy6ed10652009-06-23 06:03:08 +0000846 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
850{
851 return &((pegasus_t *) netdev_priv(dev))->stats;
852}
853
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000854static inline void disable_net_traffic(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Michael Buesche3453f62009-06-18 07:03:47 +0000856 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Michael Buesche3453f62009-06-18 07:03:47 +0000858 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000861static inline void get_interrupt_interval(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Michael Buesche3453f62009-06-18 07:03:47 +0000863 u16 data;
864 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Michael Buesche3453f62009-06-18 07:03:47 +0000866 read_eprom_word(pegasus, 4, &data);
867 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700868 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000869 if (interval < 0x80) {
Joe Perchesa475f602010-02-17 10:30:24 +0000870 netif_info(pegasus, timer, pegasus->net,
871 "intr interval changed from %ums to %ums\n",
872 interval, 0x80);
Michael Buesche3453f62009-06-18 07:03:47 +0000873 interval = 0x80;
874 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700875#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000876 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Michael Buesche3453f62009-06-18 07:03:47 +0000880 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void set_carrier(struct net_device *net)
884{
885 pegasus_t *pegasus = netdev_priv(net);
886 u16 tmp;
887
Dan Williamsc43c49b2007-04-24 10:20:06 -0400888 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (tmp & BMSR_LSTATUS)
892 netif_carrier_on(net);
893 else
894 netif_carrier_off(net);
895}
896
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000897static void free_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 usb_free_urb(pegasus->intr_urb);
900 usb_free_urb(pegasus->tx_urb);
901 usb_free_urb(pegasus->rx_urb);
902 usb_free_urb(pegasus->ctrl_urb);
903}
904
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000905static void unlink_all_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 usb_kill_urb(pegasus->intr_urb);
908 usb_kill_urb(pegasus->tx_urb);
909 usb_kill_urb(pegasus->rx_urb);
910 usb_kill_urb(pegasus->ctrl_urb);
911}
912
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000913static int alloc_urbs(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000916 if (!pegasus->ctrl_urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
919 if (!pegasus->rx_urb) {
920 usb_free_urb(pegasus->ctrl_urb);
921 return 0;
922 }
923 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
924 if (!pegasus->tx_urb) {
925 usb_free_urb(pegasus->rx_urb);
926 usb_free_urb(pegasus->ctrl_urb);
927 return 0;
928 }
929 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
930 if (!pegasus->intr_urb) {
931 usb_free_urb(pegasus->tx_urb);
932 usb_free_urb(pegasus->rx_urb);
933 usb_free_urb(pegasus->ctrl_urb);
934 return 0;
935 }
936
937 return 1;
938}
939
940static int pegasus_open(struct net_device *net)
941{
942 pegasus_t *pegasus = netdev_priv(net);
943 int res;
944
945 if (pegasus->rx_skb == NULL)
Petko Manolov313a58e2013-04-25 22:41:21 +0000946 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
947 PEGASUS_MTU,
948 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!pegasus->rx_skb)
950 return -ENOMEM;
951
952 res = set_registers(pegasus, EthID, 6, net->dev_addr);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
955 usb_rcvbulkpipe(pegasus->usb, 1),
956 pegasus->rx_skb->data, PEGASUS_MTU + 8,
957 read_bulk_callback, pegasus);
958 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700959 if (res == -ENODEV)
960 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000961 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto exit;
963 }
964
965 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
966 usb_rcvintpipe(pegasus->usb, 3),
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +0000967 pegasus->intr_buff, sizeof(pegasus->intr_buff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 intr_callback, pegasus, pegasus->intr_interval);
969 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -0700970 if (res == -ENODEV)
971 netif_device_detach(pegasus->net);
Joe Perchesa475f602010-02-17 10:30:24 +0000972 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 usb_kill_urb(pegasus->rx_urb);
974 goto exit;
975 }
976 if ((res = enable_net_traffic(net, pegasus->usb))) {
Joe Perchesa475f602010-02-17 10:30:24 +0000977 netif_dbg(pegasus, ifup, net,
978 "can't enable_net_traffic() - %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 res = -EIO;
980 usb_kill_urb(pegasus->rx_urb);
981 usb_kill_urb(pegasus->intr_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto exit;
983 }
984 set_carrier(net);
985 netif_start_queue(net);
Joe Perchesa475f602010-02-17 10:30:24 +0000986 netif_dbg(pegasus, ifup, net, "open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 res = 0;
988exit:
989 return res;
990}
991
992static int pegasus_close(struct net_device *net)
993{
994 pegasus_t *pegasus = netdev_priv(net);
995
996 netif_stop_queue(net);
997 if (!(pegasus->flags & PEGASUS_UNPLUG))
998 disable_net_traffic(pegasus);
999 tasklet_kill(&pegasus->rx_tl);
1000 unlink_all_urbs(pegasus);
1001
1002 return 0;
1003}
1004
1005static void pegasus_get_drvinfo(struct net_device *dev,
1006 struct ethtool_drvinfo *info)
1007{
1008 pegasus_t *pegasus = netdev_priv(dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001009
1010 strlcpy(info->driver, driver_name, sizeof(info->driver));
1011 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001012 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/* also handles three patterns of some kind in hardware */
1016#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1017
1018static void
1019pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1020{
1021 pegasus_t *pegasus = netdev_priv(dev);
1022
1023 wol->supported = WAKE_MAGIC | WAKE_PHY;
1024 wol->wolopts = pegasus->wolopts;
1025}
1026
1027static int
1028pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1029{
1030 pegasus_t *pegasus = netdev_priv(dev);
1031 u8 reg78 = 0x04;
Ming Lei4fbc5b22013-01-19 01:32:01 +00001032 int ret;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (wol->wolopts & ~WOL_SUPPORTED)
1035 return -EINVAL;
1036
1037 if (wol->wolopts & WAKE_MAGIC)
1038 reg78 |= 0x80;
1039 if (wol->wolopts & WAKE_PHY)
1040 reg78 |= 0x40;
1041 /* FIXME this 0x10 bit still needs to get set in the chip... */
1042 if (wol->wolopts)
1043 pegasus->eth_regs[0] |= 0x10;
1044 else
1045 pegasus->eth_regs[0] &= ~0x10;
1046 pegasus->wolopts = wol->wolopts;
Ming Lei4fbc5b22013-01-19 01:32:01 +00001047
1048 ret = set_register(pegasus, WakeupControl, reg78);
1049 if (!ret)
1050 ret = device_set_wakeup_enable(&pegasus->usb->dev,
1051 wol->wolopts);
1052 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055static inline void pegasus_reset_wol(struct net_device *dev)
1056{
1057 struct ethtool_wolinfo wol;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 memset(&wol, 0, sizeof wol);
1060 (void) pegasus_set_wol(dev, &wol);
1061}
1062
1063static int
1064pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1065{
1066 pegasus_t *pegasus;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 pegasus = netdev_priv(dev);
1069 mii_ethtool_gset(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071}
1072
1073static int
1074pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1075{
1076 pegasus_t *pegasus = netdev_priv(dev);
1077 return mii_ethtool_sset(&pegasus->mii, ecmd);
1078}
1079
1080static int pegasus_nway_reset(struct net_device *dev)
1081{
1082 pegasus_t *pegasus = netdev_priv(dev);
1083 return mii_nway_restart(&pegasus->mii);
1084}
1085
1086static u32 pegasus_get_link(struct net_device *dev)
1087{
1088 pegasus_t *pegasus = netdev_priv(dev);
1089 return mii_link_ok(&pegasus->mii);
1090}
1091
1092static u32 pegasus_get_msglevel(struct net_device *dev)
1093{
1094 pegasus_t *pegasus = netdev_priv(dev);
1095 return pegasus->msg_enable;
1096}
1097
1098static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1099{
1100 pegasus_t *pegasus = netdev_priv(dev);
1101 pegasus->msg_enable = v;
1102}
1103
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001104static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .get_drvinfo = pegasus_get_drvinfo,
1106 .get_settings = pegasus_get_settings,
1107 .set_settings = pegasus_set_settings,
1108 .nway_reset = pegasus_nway_reset,
1109 .get_link = pegasus_get_link,
1110 .get_msglevel = pegasus_get_msglevel,
1111 .set_msglevel = pegasus_set_msglevel,
1112 .get_wol = pegasus_get_wol,
1113 .set_wol = pegasus_set_wol,
1114};
1115
1116static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1117{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001118 __u16 *data = (__u16 *) &rq->ifr_ifru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 pegasus_t *pegasus = netdev_priv(net);
1120 int res;
1121
1122 switch (cmd) {
1123 case SIOCDEVPRIVATE:
1124 data[0] = pegasus->phy;
1125 case SIOCDEVPRIVATE + 1:
1126 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1127 res = 0;
1128 break;
1129 case SIOCDEVPRIVATE + 2:
1130 if (!capable(CAP_NET_ADMIN))
1131 return -EPERM;
Petko Manolov2bd64702013-04-25 22:41:36 +00001132 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 res = 0;
1134 break;
1135 default:
1136 res = -EOPNOTSUPP;
1137 }
1138 return res;
1139}
1140
1141static void pegasus_set_multicast(struct net_device *net)
1142{
1143 pegasus_t *pegasus = netdev_priv(net);
1144
1145 if (net->flags & IFF_PROMISC) {
1146 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001147 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001148 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1150 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
Joe Perchesa475f602010-02-17 10:30:24 +00001151 netif_dbg(pegasus, link, net, "set allmulti\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 } else {
1153 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1154 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1155 }
1156
David Brownelle000ea12008-09-02 11:34:24 -07001157 pegasus->ctrl_urb->status = 0;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 pegasus->flags |= ETH_REGS_CHANGE;
David Howells7d12e782006-10-05 14:55:46 +01001160 ctrl_callback(pegasus->ctrl_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001163static __u8 mii_phy_probe(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int i;
1166 __u16 tmp;
1167
1168 for (i = 0; i < 32; i++) {
1169 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1170 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1171 continue;
1172 else
1173 return i;
1174 }
1175
1176 return 0xff;
1177}
1178
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001179static inline void setup_pegasus_II(pegasus_t *pegasus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 __u8 data = 0xa5;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001182
Petko Manolov4a1728a2005-11-15 09:48:23 +02001183 set_register(pegasus, Reg1d, 0);
1184 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 mdelay(100);
1186 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001187 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001189 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Petko Manolov4a1728a2005-11-15 09:48:23 +02001191 set_register(pegasus, 0x83, data);
1192 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001194 if (data == 0xa5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 pegasus->chip = 0x8513;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001196 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 pegasus->chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Petko Manolov4a1728a2005-11-15 09:48:23 +02001199 set_register(pegasus, 0x80, 0xc0);
1200 set_register(pegasus, 0x83, 0xff);
1201 set_register(pegasus, 0x84, 0x01);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001204 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001206 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209
David Brownellcda28362008-11-16 00:36:08 -08001210static int pegasus_count;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001211static struct workqueue_struct *pegasus_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212#define CARRIER_CHECK_DELAY (2 * HZ)
1213
David Howellsc4028952006-11-22 14:57:56 +00001214static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
David Howellsc4028952006-11-22 14:57:56 +00001216 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 set_carrier(pegasus->net);
1218 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1219 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1220 CARRIER_CHECK_DELAY);
1221 }
1222}
1223
Ben Collins4f631352008-07-30 12:39:02 -07001224static int pegasus_blacklisted(struct usb_device *udev)
1225{
1226 struct usb_device_descriptor *udd = &udev->descriptor;
1227
1228 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1229 * dongle which happens to have the same ID.
1230 */
Michael Buesche3453f62009-06-18 07:03:47 +00001231 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1232 (udd->idProduct == cpu_to_le16(0x0121)) &&
Ben Collins4f631352008-07-30 12:39:02 -07001233 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1234 (udd->bDeviceProtocol == 1))
1235 return 1;
1236
1237 return 0;
1238}
1239
David Brownellcda28362008-11-16 00:36:08 -08001240/* we rely on probe() and remove() being serialized so we
1241 * don't need extra locking on pegasus_count.
1242 */
1243static void pegasus_dec_workqueue(void)
1244{
1245 pegasus_count--;
1246 if (pegasus_count == 0) {
1247 destroy_workqueue(pegasus_workqueue);
1248 pegasus_workqueue = NULL;
1249 }
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static int pegasus_probe(struct usb_interface *intf,
1253 const struct usb_device_id *id)
1254{
1255 struct usb_device *dev = interface_to_usbdev(intf);
1256 struct net_device *net;
1257 pegasus_t *pegasus;
1258 int dev_index = id - pegasus_ids;
1259 int res = -ENOMEM;
1260
David Brownellcda28362008-11-16 00:36:08 -08001261 if (pegasus_blacklisted(dev))
1262 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001263
David Brownellcda28362008-11-16 00:36:08 -08001264 if (pegasus_count == 0) {
1265 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1266 if (!pegasus_workqueue)
1267 return -ENOMEM;
Ben Collins4f631352008-07-30 12:39:02 -07001268 }
David Brownellcda28362008-11-16 00:36:08 -08001269 pegasus_count++;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 net = alloc_etherdev(sizeof(struct pegasus));
Joe Perches41de8d42012-01-29 13:47:52 +00001272 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 pegasus->dev_index = dev_index;
1277 init_waitqueue_head(&pegasus->ctrl_wait);
1278
1279 if (!alloc_urbs(pegasus)) {
1280 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1281 goto out1;
1282 }
1283
1284 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1285
David Howellsc4028952006-11-22 14:57:56 +00001286 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 pegasus->intf = intf;
1289 pegasus->usb = dev;
1290 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001291
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001294 net->netdev_ops = &pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 SET_ETHTOOL_OPS(net, &ops);
1296 pegasus->mii.dev = net;
1297 pegasus->mii.mdio_read = mdio_read;
1298 pegasus->mii.mdio_write = mdio_write;
1299 pegasus->mii.phy_id_mask = 0x1f;
1300 pegasus->mii.reg_num_mask = 0x1f;
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001301 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1303
1304 pegasus->features = usb_dev_id[dev_index].private;
1305 get_interrupt_interval(pegasus);
1306 if (reset_mac(pegasus)) {
1307 dev_err(&intf->dev, "can't reset MAC\n");
1308 res = -EIO;
1309 goto out2;
1310 }
1311 set_ethernet_addr(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (pegasus->features & PEGASUS_II) {
1313 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1314 setup_pegasus_II(pegasus);
1315 }
1316 pegasus->phy = mii_phy_probe(pegasus);
1317 if (pegasus->phy == 0xff) {
1318 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1319 pegasus->phy = 1;
1320 }
1321 pegasus->mii.phy_id = pegasus->phy;
1322 usb_set_intfdata(intf, pegasus);
1323 SET_NETDEV_DEV(net, &intf->dev);
1324 pegasus_reset_wol(net);
1325 res = register_netdev(net);
1326 if (res)
1327 goto out3;
1328 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1329 CARRIER_CHECK_DELAY);
1330
Johannes Berge1749612008-10-27 15:59:26 -07001331 dev_info(&intf->dev, "%s, %s, %pM\n",
Joe Perches0795af52007-10-03 17:59:30 -07001332 net->name,
1333 usb_dev_id[dev_index].name,
Johannes Berge1749612008-10-27 15:59:26 -07001334 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return 0;
1336
1337out3:
1338 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339out2:
1340 free_all_urbs(pegasus);
1341out1:
1342 free_netdev(net);
1343out:
David Brownellcda28362008-11-16 00:36:08 -08001344 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return res;
1346}
1347
1348static void pegasus_disconnect(struct usb_interface *intf)
1349{
1350 struct pegasus *pegasus = usb_get_intfdata(intf);
1351
1352 usb_set_intfdata(intf, NULL);
1353 if (!pegasus) {
1354 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1355 return;
1356 }
1357
1358 pegasus->flags |= PEGASUS_UNPLUG;
1359 cancel_delayed_work(&pegasus->carrier_check);
1360 unregister_netdev(pegasus->net);
Kevin Vigora85a46f2005-09-22 00:49:24 -07001361 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 free_all_urbs(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001363 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001365 pegasus->rx_skb = NULL;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 free_netdev(pegasus->net);
David Brownellcda28362008-11-16 00:36:08 -08001368 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001371static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct pegasus *pegasus = usb_get_intfdata(intf);
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001374
1375 netif_device_detach(pegasus->net);
David Brownell7e713b82006-05-01 14:02:45 -07001376 cancel_delayed_work(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001377 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001378 usb_kill_urb(pegasus->rx_urb);
1379 usb_kill_urb(pegasus->intr_urb);
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382}
1383
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001384static int pegasus_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct pegasus *pegasus = usb_get_intfdata(intf);
1387
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001388 netif_device_attach(pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001389 if (netif_running(pegasus->net)) {
1390 pegasus->rx_urb->status = 0;
1391 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001392 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001393
1394 pegasus->intr_urb->status = 0;
1395 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001396 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001397 }
David Brownell7e713b82006-05-01 14:02:45 -07001398 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1399 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
1401}
1402
Oliver Neukum8cb89572009-01-08 11:22:25 -08001403static const struct net_device_ops pegasus_netdev_ops = {
1404 .ndo_open = pegasus_open,
1405 .ndo_stop = pegasus_close,
1406 .ndo_do_ioctl = pegasus_ioctl,
1407 .ndo_start_xmit = pegasus_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001408 .ndo_set_rx_mode = pegasus_set_multicast,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001409 .ndo_get_stats = pegasus_netdev_stats,
1410 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001411 .ndo_change_mtu = eth_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00001412 .ndo_set_mac_address = eth_mac_addr,
1413 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001414};
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static struct usb_driver pegasus_driver = {
1417 .name = driver_name,
1418 .probe = pegasus_probe,
1419 .disconnect = pegasus_disconnect,
1420 .id_table = pegasus_ids,
1421 .suspend = pegasus_suspend,
1422 .resume = pegasus_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001423 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424};
1425
David Brownellcda28362008-11-16 00:36:08 -08001426static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001427{
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001428 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1429 char *token, *name = NULL;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001430
1431 if ((token = strsep(&id, ":")) != NULL)
1432 name = token;
1433 /* name now points to a null terminated string*/
1434 if ((token = strsep(&id, ":")) != NULL)
1435 vendor_id = simple_strtoul(token, NULL, 16);
1436 if ((token = strsep(&id, ":")) != NULL)
1437 device_id = simple_strtoul(token, NULL, 16);
1438 flags = simple_strtoul(id, NULL, 16);
1439 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 +00001440 driver_name, name, vendor_id, device_id, flags);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001441
1442 if (vendor_id > 0x10000 || vendor_id == 0)
1443 return;
1444 if (device_id > 0x10000 || device_id == 0)
1445 return;
1446
Nicolas Kaiser5a9dbfe2010-06-26 06:58:54 +00001447 for (i = 0; usb_dev_id[i].name; i++);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001448 usb_dev_id[i].name = name;
1449 usb_dev_id[i].vendor = vendor_id;
1450 usb_dev_id[i].device = device_id;
1451 usb_dev_id[i].private = flags;
1452 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1453 pegasus_ids[i].idVendor = vendor_id;
1454 pegasus_ids[i].idProduct = device_id;
1455}
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457static int __init pegasus_init(void)
1458{
1459 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001460 if (devid)
1461 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return usb_register(&pegasus_driver);
1463}
1464
1465static void __exit pegasus_exit(void)
1466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 usb_deregister(&pegasus_driver);
1468}
1469
1470module_init(pegasus_init);
1471module_exit(pegasus_exit);