blob: d1ed68a11e70192ef320c1c15c52c060b952cd4b [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()
24 * time as this seems to break dhcpd.
25 * 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
28 * is out of the interrupt routine.
29 */
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
58static int loopback = 0;
59static int mii_mode = 0;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020060static char *devid=NULL;
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},
65#include "pegasus.h"
66#undef PEGASUS_DEV
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020067 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 {NULL, 0, 0, 0}
69};
70
71static struct usb_device_id pegasus_ids[] = {
72#define PEGASUS_DEV(pn, vid, pid, flags) \
73 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
74#include "pegasus.h"
75#undef PEGASUS_DEV
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020076 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 {}
78};
79
80MODULE_AUTHOR(DRIVER_AUTHOR);
81MODULE_DESCRIPTION(DRIVER_DESC);
82MODULE_LICENSE("GPL");
83module_param(loopback, bool, 0);
84module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020085module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020088MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* use ethtool to change the level for any given device */
91static int msg_level = -1;
92module_param (msg_level, int, 0);
93MODULE_PARM_DESC (msg_level, "Override default message level");
94
95MODULE_DEVICE_TABLE(usb, pegasus_ids);
96
97static int update_eth_regs_async(pegasus_t *);
98/* Aargh!!! I _really_ hate such tweaks */
David Howells7d12e782006-10-05 14:55:46 +010099static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 pegasus_t *pegasus = urb->context;
102
103 if (!pegasus)
104 return;
105
106 switch (urb->status) {
107 case 0:
108 if (pegasus->flags & ETH_REGS_CHANGE) {
109 pegasus->flags &= ~ETH_REGS_CHANGE;
110 pegasus->flags |= ETH_REGS_CHANGED;
111 update_eth_regs_async(pegasus);
112 return;
113 }
114 break;
115 case -EINPROGRESS:
116 return;
117 case -ENOENT:
118 break;
119 default:
120 if (netif_msg_drv(pegasus))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200121 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 __FUNCTION__, urb->status);
123 }
124 pegasus->flags &= ~ETH_REGS_CHANGED;
125 wake_up(&pegasus->ctrl_wait);
126}
127
128static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
129 void *data)
130{
131 int ret;
132 char *buffer;
133 DECLARE_WAITQUEUE(wait, current);
134
135 buffer = kmalloc(size, GFP_KERNEL);
136 if (!buffer) {
137 if (netif_msg_drv(pegasus))
138 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
139 __FUNCTION__);
140 return -ENOMEM;
141 }
142 add_wait_queue(&pegasus->ctrl_wait, &wait);
143 set_current_state(TASK_UNINTERRUPTIBLE);
144 while (pegasus->flags & ETH_REGS_CHANGED)
145 schedule();
146 remove_wait_queue(&pegasus->ctrl_wait, &wait);
147 set_current_state(TASK_RUNNING);
148
149 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
150 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
151 pegasus->dr.wValue = cpu_to_le16(0);
152 pegasus->dr.wIndex = cpu_to_le16p(&indx);
153 pegasus->dr.wLength = cpu_to_le16p(&size);
154 pegasus->ctrl_urb->transfer_buffer_length = size;
155
156 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
157 usb_rcvctrlpipe(pegasus->usb, 0),
158 (char *) &pegasus->dr,
159 buffer, size, ctrl_callback, pegasus);
160
161 add_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_UNINTERRUPTIBLE);
163
164 /* using ATOMIC, we'd never wake up if we slept */
165 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
Oliver Neukum10c82112006-11-23 15:40:17 +0100166 set_current_state(TASK_RUNNING);
David Brownell955a2602006-05-26 10:17:03 -0700167 if (ret == -ENODEV)
168 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (netif_msg_drv(pegasus))
170 dev_err(&pegasus->intf->dev, "%s, status %d\n",
171 __FUNCTION__, ret);
172 goto out;
173 }
174
175 schedule();
176out:
177 remove_wait_queue(&pegasus->ctrl_wait, &wait);
178 memcpy(data, buffer, size);
179 kfree(buffer);
180
181 return ret;
182}
183
184static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
185 void *data)
186{
187 int ret;
188 char *buffer;
189 DECLARE_WAITQUEUE(wait, current);
190
191 buffer = kmalloc(size, GFP_KERNEL);
192 if (!buffer) {
193 if (netif_msg_drv(pegasus))
194 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
195 __FUNCTION__);
196 return -ENOMEM;
197 }
198 memcpy(buffer, data, size);
199
200 add_wait_queue(&pegasus->ctrl_wait, &wait);
201 set_current_state(TASK_UNINTERRUPTIBLE);
202 while (pegasus->flags & ETH_REGS_CHANGED)
203 schedule();
204 remove_wait_queue(&pegasus->ctrl_wait, &wait);
205 set_current_state(TASK_RUNNING);
206
207 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
208 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
209 pegasus->dr.wValue = cpu_to_le16(0);
210 pegasus->dr.wIndex = cpu_to_le16p(&indx);
211 pegasus->dr.wLength = cpu_to_le16p(&size);
212 pegasus->ctrl_urb->transfer_buffer_length = size;
213
214 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
215 usb_sndctrlpipe(pegasus->usb, 0),
216 (char *) &pegasus->dr,
217 buffer, size, ctrl_callback, pegasus);
218
219 add_wait_queue(&pegasus->ctrl_wait, &wait);
220 set_current_state(TASK_UNINTERRUPTIBLE);
221
222 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700223 if (ret == -ENODEV)
224 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (netif_msg_drv(pegasus))
226 dev_err(&pegasus->intf->dev, "%s, status %d\n",
227 __FUNCTION__, ret);
228 goto out;
229 }
230
231 schedule();
232out:
233 remove_wait_queue(&pegasus->ctrl_wait, &wait);
234 kfree(buffer);
235
236 return ret;
237}
238
239static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
240{
241 int ret;
242 char *tmp;
243 DECLARE_WAITQUEUE(wait, current);
244
245 tmp = kmalloc(1, GFP_KERNEL);
246 if (!tmp) {
247 if (netif_msg_drv(pegasus))
248 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
249 __FUNCTION__);
250 return -ENOMEM;
251 }
252 memcpy(tmp, &data, 1);
253 add_wait_queue(&pegasus->ctrl_wait, &wait);
254 set_current_state(TASK_UNINTERRUPTIBLE);
255 while (pegasus->flags & ETH_REGS_CHANGED)
256 schedule();
257 remove_wait_queue(&pegasus->ctrl_wait, &wait);
258 set_current_state(TASK_RUNNING);
259
260 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
261 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
262 pegasus->dr.wValue = cpu_to_le16(data);
263 pegasus->dr.wIndex = cpu_to_le16p(&indx);
264 pegasus->dr.wLength = cpu_to_le16(1);
265 pegasus->ctrl_urb->transfer_buffer_length = 1;
266
267 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
268 usb_sndctrlpipe(pegasus->usb, 0),
269 (char *) &pegasus->dr,
Petko Manolov016534c2006-03-30 09:59:22 +0300270 tmp, 1, ctrl_callback, pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 add_wait_queue(&pegasus->ctrl_wait, &wait);
273 set_current_state(TASK_UNINTERRUPTIBLE);
274
275 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700276 if (ret == -ENODEV)
277 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (netif_msg_drv(pegasus))
279 dev_err(&pegasus->intf->dev, "%s, status %d\n",
280 __FUNCTION__, ret);
281 goto out;
282 }
283
284 schedule();
285out:
286 remove_wait_queue(&pegasus->ctrl_wait, &wait);
287 kfree(tmp);
288
289 return ret;
290}
291
292static int update_eth_regs_async(pegasus_t * pegasus)
293{
294 int ret;
295
296 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
297 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
298 pegasus->dr.wValue = 0;
299 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
300 pegasus->dr.wLength = cpu_to_le16(3);
301 pegasus->ctrl_urb->transfer_buffer_length = 3;
302
303 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
304 usb_sndctrlpipe(pegasus->usb, 0),
305 (char *) &pegasus->dr,
306 pegasus->eth_regs, 3, ctrl_callback, pegasus);
307
David Brownell955a2602006-05-26 10:17:03 -0700308 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
309 if (ret == -ENODEV)
310 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (netif_msg_drv(pegasus))
312 dev_err(&pegasus->intf->dev, "%s, status %d\n",
313 __FUNCTION__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return ret;
317}
318
Dan Williamsc43c49b2007-04-24 10:20:06 -0400319/* Returns 0 on success, error on failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
321{
322 int i;
323 __u8 data[4] = { phy, 0, 0, indx };
324 __le16 regdi;
325 int ret;
326
Petko Manolov4a1728a2005-11-15 09:48:23 +0200327 set_register(pegasus, PhyCtrl, 0);
328 set_registers(pegasus, PhyAddr, sizeof (data), data);
329 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 for (i = 0; i < REG_TIMEOUT; i++) {
331 ret = get_registers(pegasus, PhyCtrl, 1, data);
David Brownell7e713b82006-05-01 14:02:45 -0700332 if (ret == -ESHUTDOWN)
333 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (data[0] & PHY_DONE)
335 break;
336 }
337 if (i < REG_TIMEOUT) {
338 ret = get_registers(pegasus, PhyData, 2, &regdi);
339 *regd = le16_to_cpu(regdi);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
David Brownell7e713b82006-05-01 14:02:45 -0700342fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (netif_msg_drv(pegasus))
Petko Manolov37cf3472006-09-27 14:25:37 -0700344 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Petko Manolov4a1728a2005-11-15 09:48:23 +0200346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349static int mdio_read(struct net_device *dev, int phy_id, int loc)
350{
351 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
352 u16 res;
353
354 read_mii_word(pegasus, phy_id, loc, &res);
355 return (int)res;
356}
357
358static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
359{
360 int i;
361 __u8 data[4] = { phy, 0, 0, indx };
362 int ret;
363
364 data[1] = (u8) regd;
365 data[2] = (u8) (regd >> 8);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200366 set_register(pegasus, PhyCtrl, 0);
367 set_registers(pegasus, PhyAddr, sizeof(data), data);
368 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 for (i = 0; i < REG_TIMEOUT; i++) {
370 ret = get_registers(pegasus, PhyCtrl, 1, data);
David Brownell7e713b82006-05-01 14:02:45 -0700371 if (ret == -ESHUTDOWN)
372 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (data[0] & PHY_DONE)
374 break;
375 }
376 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
David Brownell7e713b82006-05-01 14:02:45 -0700379fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (netif_msg_drv(pegasus))
Petko Manolov37cf3472006-09-27 14:25:37 -0700381 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200382 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
386{
387 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
388
389 write_mii_word(pegasus, phy_id, loc, val);
390}
391
392static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
393{
394 int i;
395 __u8 tmp;
396 __le16 retdatai;
397 int ret;
398
Petko Manolov4a1728a2005-11-15 09:48:23 +0200399 set_register(pegasus, EpromCtrl, 0);
400 set_register(pegasus, EpromOffset, index);
401 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 for (i = 0; i < REG_TIMEOUT; i++) {
404 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
405 if (tmp & EPROM_DONE)
406 break;
David Brownell7e713b82006-05-01 14:02:45 -0700407 if (ret == -ESHUTDOWN)
408 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 if (i < REG_TIMEOUT) {
411 ret = get_registers(pegasus, EpromData, 2, &retdatai);
412 *retdata = le16_to_cpu(retdatai);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
David Brownell7e713b82006-05-01 14:02:45 -0700416fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (netif_msg_drv(pegasus))
Petko Manolov37cf3472006-09-27 14:25:37 -0700418 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200419 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422#ifdef PEGASUS_WRITE_EEPROM
423static inline void enable_eprom_write(pegasus_t * pegasus)
424{
425 __u8 tmp;
426 int ret;
427
Petko Manolov4a1728a2005-11-15 09:48:23 +0200428 get_registers(pegasus, EthCtrl2, 1, &tmp);
429 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static inline void disable_eprom_write(pegasus_t * pegasus)
433{
434 __u8 tmp;
435 int ret;
436
Petko Manolov4a1728a2005-11-15 09:48:23 +0200437 get_registers(pegasus, EthCtrl2, 1, &tmp);
438 set_register(pegasus, EpromCtrl, 0);
439 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
443{
444 int i;
445 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
446 int ret;
447
Petko Manolov4a1728a2005-11-15 09:48:23 +0200448 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200450 set_register(pegasus, EpromOffset, index);
451 set_registers(pegasus, EpromData, 2, &data);
452 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 for (i = 0; i < REG_TIMEOUT; i++) {
455 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700456 if (ret == -ESHUTDOWN)
457 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (tmp & EPROM_DONE)
459 break;
460 }
461 disable_eprom_write(pegasus);
462 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200463 return ret;
David Brownell7e713b82006-05-01 14:02:45 -0700464fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (netif_msg_drv(pegasus))
Petko Manolov37cf3472006-09-27 14:25:37 -0700466 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200467 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469#endif /* PEGASUS_WRITE_EEPROM */
470
471static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
472{
473 int i;
474 __u16 w16;
475
476 for (i = 0; i < 3; i++) {
477 read_eprom_word(pegasus, i, &w16);
478 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
479 }
480}
481
482static void set_ethernet_addr(pegasus_t * pegasus)
483{
484 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Petko Manolov37cf3472006-09-27 14:25:37 -0700486 if (pegasus->features & PEGASUS_II) {
487 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
488 } else {
489 get_node_id(pegasus, node_id);
490 set_registers(pegasus, EthID, sizeof (node_id), node_id);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
493}
494
495static inline int reset_mac(pegasus_t * pegasus)
496{
497 __u8 data = 0x8;
498 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Petko Manolov4a1728a2005-11-15 09:48:23 +0200500 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200502 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (~data & 0x08) {
504 if (loopback & 1)
505 break;
506 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200507 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200509 set_register(pegasus, Gpio1, 0x26);
510 set_register(pegasus, Gpio0, pegasus->features);
511 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 }
514 }
515 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200516 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
519 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200520 set_register(pegasus, Gpio0, 0x24);
521 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
524 __u16 auxmode;
525 read_mii_word(pegasus, 3, 0x1b, &auxmode);
526 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
527 }
528
529 return 0;
530}
531
532static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
533{
534 __u16 linkpart;
535 __u8 data[4];
536 pegasus_t *pegasus = netdev_priv(dev);
537 int ret;
538
539 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
540 data[0] = 0xc9;
541 data[1] = 0;
542 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
543 data[1] |= 0x20; /* set full duplex */
544 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
545 data[1] |= 0x10; /* set 100 Mbps */
546 if (mii_mode)
547 data[1] = 0;
548 data[2] = (loopback & 1) ? 0x09 : 0x01;
549
550 memcpy(pegasus->eth_regs, data, sizeof (data));
551 ret = set_registers(pegasus, EthCtrl0, 3, data);
552
553 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100554 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
556 u16 auxmode;
557 read_mii_word(pegasus, 0, 0x1b, &auxmode);
558 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
559 }
560
Petko Manolov4a1728a2005-11-15 09:48:23 +0200561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static void fill_skb_pool(pegasus_t * pegasus)
565{
566 int i;
567
568 for (i = 0; i < RX_SKBS; i++) {
569 if (pegasus->rx_pool[i])
570 continue;
571 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
572 /*
573 ** we give up if the allocation fail. the tasklet will be
574 ** rescheduled again anyway...
575 */
576 if (pegasus->rx_pool[i] == NULL)
577 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 skb_reserve(pegasus->rx_pool[i], 2);
579 }
580}
581
582static void free_skb_pool(pegasus_t * pegasus)
583{
584 int i;
585
586 for (i = 0; i < RX_SKBS; i++) {
587 if (pegasus->rx_pool[i]) {
588 dev_kfree_skb(pegasus->rx_pool[i]);
589 pegasus->rx_pool[i] = NULL;
590 }
591 }
592}
593
594static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
595{
596 int i;
597 struct sk_buff *skb;
598
599 for (i = 0; i < RX_SKBS; i++) {
600 if (likely(pegasus->rx_pool[i] != NULL)) {
601 skb = pegasus->rx_pool[i];
602 pegasus->rx_pool[i] = NULL;
603 return skb;
604 }
605 }
606 return NULL;
607}
608
David Howells7d12e782006-10-05 14:55:46 +0100609static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 pegasus_t *pegasus = urb->context;
612 struct net_device *net;
613 int rx_status, count = urb->actual_length;
614 u8 *buf = urb->transfer_buffer;
615 __u16 pkt_len;
616
617 if (!pegasus)
618 return;
619
620 net = pegasus->net;
621 if (!netif_device_present(net) || !netif_running(net))
622 return;
623
624 switch (urb->status) {
625 case 0:
626 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700627 case -ETIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (netif_msg_rx_err(pegasus))
629 pr_debug("%s: reset MAC\n", net->name);
630 pegasus->flags &= ~PEGASUS_RX_BUSY;
631 break;
632 case -EPIPE: /* stall, or disconnect from TT */
633 /* FIXME schedule work to clear the halt */
634 if (netif_msg_rx_err(pegasus))
635 printk(KERN_WARNING "%s: no rx stall recovery\n",
636 net->name);
637 return;
638 case -ENOENT:
639 case -ECONNRESET:
640 case -ESHUTDOWN:
641 if (netif_msg_ifdown(pegasus))
642 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
643 return;
644 default:
645 if (netif_msg_rx_err(pegasus))
646 pr_debug("%s: RX status %d\n", net->name, urb->status);
647 goto goon;
648 }
649
650 if (!count || count < 4)
651 goto goon;
652
653 rx_status = buf[count - 2];
654 if (rx_status & 0x1e) {
655 if (netif_msg_rx_err(pegasus))
656 pr_debug("%s: RX packet error %x\n",
657 net->name, rx_status);
658 pegasus->stats.rx_errors++;
659 if (rx_status & 0x06) // long or runt
660 pegasus->stats.rx_length_errors++;
661 if (rx_status & 0x08)
662 pegasus->stats.rx_crc_errors++;
663 if (rx_status & 0x10) // extra bits
664 pegasus->stats.rx_frame_errors++;
665 goto goon;
666 }
667 if (pegasus->chip == 0x8513) {
668 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
669 pkt_len &= 0x0fff;
670 pegasus->rx_skb->data += 2;
671 } else {
672 pkt_len = buf[count - 3] << 8;
673 pkt_len += buf[count - 4];
674 pkt_len &= 0xfff;
675 pkt_len -= 8;
676 }
677
678 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700679 * If the packet is unreasonably long, quietly drop it rather than
680 * kernel panicing by calling skb_put.
681 */
682 if (pkt_len > PEGASUS_MTU)
683 goto goon;
684
685 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * at this point we are sure pegasus->rx_skb != NULL
687 * so we go ahead and pass up the packet.
688 */
689 skb_put(pegasus->rx_skb, pkt_len);
690 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
691 netif_rx(pegasus->rx_skb);
692 pegasus->stats.rx_packets++;
693 pegasus->stats.rx_bytes += pkt_len;
694
695 if (pegasus->flags & PEGASUS_UNPLUG)
696 return;
697
698 spin_lock(&pegasus->rx_pool_lock);
699 pegasus->rx_skb = pull_skb(pegasus);
700 spin_unlock(&pegasus->rx_pool_lock);
701
702 if (pegasus->rx_skb == NULL)
703 goto tl_sched;
704goon:
705 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
706 usb_rcvbulkpipe(pegasus->usb, 1),
707 pegasus->rx_skb->data, PEGASUS_MTU + 8,
708 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700709 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
710 if (rx_status == -ENODEV)
711 netif_device_detach(pegasus->net);
712 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 pegasus->flags |= PEGASUS_RX_URB_FAIL;
714 goto tl_sched;
715 } else {
716 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
717 }
718
719 return;
720
721tl_sched:
722 tasklet_schedule(&pegasus->rx_tl);
723}
724
725static void rx_fixup(unsigned long data)
726{
727 pegasus_t *pegasus;
728 unsigned long flags;
David Brownell955a2602006-05-26 10:17:03 -0700729 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 pegasus = (pegasus_t *) data;
732 if (pegasus->flags & PEGASUS_UNPLUG)
733 return;
734
735 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
736 fill_skb_pool(pegasus);
737 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
738 if (pegasus->rx_skb)
739 goto try_again;
740 if (pegasus->rx_skb == NULL) {
741 pegasus->rx_skb = pull_skb(pegasus);
742 }
743 if (pegasus->rx_skb == NULL) {
744 if (netif_msg_rx_err(pegasus))
745 printk(KERN_WARNING "%s: low on memory\n",
746 pegasus->net->name);
747 tasklet_schedule(&pegasus->rx_tl);
748 goto done;
749 }
750 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
751 usb_rcvbulkpipe(pegasus->usb, 1),
752 pegasus->rx_skb->data, PEGASUS_MTU + 8,
753 read_bulk_callback, pegasus);
754try_again:
David Brownell955a2602006-05-26 10:17:03 -0700755 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
756 if (status == -ENODEV)
757 netif_device_detach(pegasus->net);
758 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 pegasus->flags |= PEGASUS_RX_URB_FAIL;
760 tasklet_schedule(&pegasus->rx_tl);
761 } else {
762 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
763 }
764done:
765 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
766}
767
David Howells7d12e782006-10-05 14:55:46 +0100768static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800771 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (!pegasus)
774 return;
775
Micah Gruber93519822007-07-23 16:05:52 +0800776 net = pegasus->net;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!netif_device_present(net) || !netif_running(net))
779 return;
780
781 switch (urb->status) {
782 case -EPIPE:
783 /* FIXME schedule_work() to clear the tx halt */
784 netif_stop_queue(net);
785 if (netif_msg_tx_err(pegasus))
786 printk(KERN_WARNING "%s: no tx stall recovery\n",
787 net->name);
788 return;
789 case -ENOENT:
790 case -ECONNRESET:
791 case -ESHUTDOWN:
792 if (netif_msg_ifdown(pegasus))
793 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
794 return;
795 default:
796 if (netif_msg_tx_err(pegasus))
797 pr_info("%s: TX status %d\n", net->name, urb->status);
798 /* FALL THROUGH */
799 case 0:
800 break;
801 }
802
803 net->trans_start = jiffies;
804 netif_wake_queue(net);
805}
806
David Howells7d12e782006-10-05 14:55:46 +0100807static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 pegasus_t *pegasus = urb->context;
810 struct net_device *net;
811 int status;
812
813 if (!pegasus)
814 return;
815 net = pegasus->net;
816
817 switch (urb->status) {
818 case 0:
819 break;
820 case -ECONNRESET: /* unlink */
821 case -ENOENT:
822 case -ESHUTDOWN:
823 return;
824 default:
825 /* some Pegasus-I products report LOTS of data
826 * toggle errors... avoid log spamming
827 */
828 if (netif_msg_timer(pegasus))
829 pr_debug("%s: intr status %d\n", net->name,
830 urb->status);
831 }
832
833 if (urb->actual_length >= 6) {
834 u8 * d = urb->transfer_buffer;
835
836 /* byte 0 == tx_status1, reg 2B */
837 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
838 |LATE_COL|JABBER_TIMEOUT)) {
839 pegasus->stats.tx_errors++;
840 if (d[0] & TX_UNDERRUN)
841 pegasus->stats.tx_fifo_errors++;
842 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
843 pegasus->stats.tx_aborted_errors++;
844 if (d[0] & LATE_COL)
845 pegasus->stats.tx_window_errors++;
846 }
847
848 /* d[5].LINK_STATUS lies on some adapters.
849 * d[0].NO_CARRIER kicks in only with failed TX.
850 * ... so monitoring with MII may be safest.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
854 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
855 }
856
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800857 status = usb_submit_urb(urb, GFP_ATOMIC);
David Brownell955a2602006-05-26 10:17:03 -0700858 if (status == -ENODEV)
859 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (status && netif_msg_timer(pegasus))
861 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
862 net->name, status);
863}
864
865static void pegasus_tx_timeout(struct net_device *net)
866{
867 pegasus_t *pegasus = netdev_priv(net);
868 if (netif_msg_timer(pegasus))
869 printk(KERN_WARNING "%s: tx timeout\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 usb_unlink_urb(pegasus->tx_urb);
871 pegasus->stats.tx_errors++;
872}
873
874static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
875{
876 pegasus_t *pegasus = netdev_priv(net);
877 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
878 int res;
879 __u16 l16 = skb->len;
880
881 netif_stop_queue(net);
882
883 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300884 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
886 usb_sndbulkpipe(pegasus->usb, 2),
887 pegasus->tx_buff, count,
888 write_bulk_callback, pegasus);
889 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
890 if (netif_msg_tx_err(pegasus))
891 printk(KERN_WARNING "%s: fail tx, %d\n",
892 net->name, res);
893 switch (res) {
894 case -EPIPE: /* stall, or disconnect from TT */
895 /* cleanup should already have been scheduled */
896 break;
897 case -ENODEV: /* disconnect() upcoming */
David Brownell955a2602006-05-26 10:17:03 -0700898 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
900 default:
901 pegasus->stats.tx_errors++;
902 netif_start_queue(net);
903 }
904 } else {
905 pegasus->stats.tx_packets++;
906 pegasus->stats.tx_bytes += skb->len;
907 net->trans_start = jiffies;
908 }
909 dev_kfree_skb(skb);
910
911 return 0;
912}
913
914static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
915{
916 return &((pegasus_t *) netdev_priv(dev))->stats;
917}
918
919static inline void disable_net_traffic(pegasus_t * pegasus)
920{
921 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Petko Manolov4a1728a2005-11-15 09:48:23 +0200923 set_registers(pegasus, EthCtrl0, 2, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static inline void get_interrupt_interval(pegasus_t * pegasus)
927{
928 __u8 data[2];
929
930 read_eprom_word(pegasus, 4, (__u16 *) data);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700931 if (pegasus->usb->speed != USB_SPEED_HIGH) {
932 if (data[1] < 0x80) {
933 if (netif_msg_timer(pegasus))
934 dev_info(&pegasus->intf->dev, "intr interval "
935 "changed from %ums to %ums\n",
936 data[1], 0x80);
937 data[1] = 0x80;
938#ifdef PEGASUS_WRITE_EEPROM
939 write_eprom_word(pegasus, 4, *(__u16 *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 pegasus->intr_interval = data[1];
944}
945
946static void set_carrier(struct net_device *net)
947{
948 pegasus_t *pegasus = netdev_priv(net);
949 u16 tmp;
950
Dan Williamsc43c49b2007-04-24 10:20:06 -0400951 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (tmp & BMSR_LSTATUS)
955 netif_carrier_on(net);
956 else
957 netif_carrier_off(net);
958}
959
960static void free_all_urbs(pegasus_t * pegasus)
961{
962 usb_free_urb(pegasus->intr_urb);
963 usb_free_urb(pegasus->tx_urb);
964 usb_free_urb(pegasus->rx_urb);
965 usb_free_urb(pegasus->ctrl_urb);
966}
967
968static void unlink_all_urbs(pegasus_t * pegasus)
969{
970 usb_kill_urb(pegasus->intr_urb);
971 usb_kill_urb(pegasus->tx_urb);
972 usb_kill_urb(pegasus->rx_urb);
973 usb_kill_urb(pegasus->ctrl_urb);
974}
975
976static int alloc_urbs(pegasus_t * pegasus)
977{
978 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
979 if (!pegasus->ctrl_urb) {
980 return 0;
981 }
982 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
983 if (!pegasus->rx_urb) {
984 usb_free_urb(pegasus->ctrl_urb);
985 return 0;
986 }
987 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
988 if (!pegasus->tx_urb) {
989 usb_free_urb(pegasus->rx_urb);
990 usb_free_urb(pegasus->ctrl_urb);
991 return 0;
992 }
993 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
994 if (!pegasus->intr_urb) {
995 usb_free_urb(pegasus->tx_urb);
996 usb_free_urb(pegasus->rx_urb);
997 usb_free_urb(pegasus->ctrl_urb);
998 return 0;
999 }
1000
1001 return 1;
1002}
1003
1004static int pegasus_open(struct net_device *net)
1005{
1006 pegasus_t *pegasus = netdev_priv(net);
1007 int res;
1008
1009 if (pegasus->rx_skb == NULL)
1010 pegasus->rx_skb = pull_skb(pegasus);
1011 /*
1012 ** Note: no point to free the pool. it is empty :-)
1013 */
1014 if (!pegasus->rx_skb)
1015 return -ENOMEM;
1016
1017 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1018
1019 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1020 usb_rcvbulkpipe(pegasus->usb, 1),
1021 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1022 read_bulk_callback, pegasus);
1023 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -07001024 if (res == -ENODEV)
1025 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (netif_msg_ifup(pegasus))
1027 pr_debug("%s: failed rx_urb, %d", net->name, res);
1028 goto exit;
1029 }
1030
1031 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1032 usb_rcvintpipe(pegasus->usb, 3),
1033 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1034 intr_callback, pegasus, pegasus->intr_interval);
1035 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -07001036 if (res == -ENODEV)
1037 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (netif_msg_ifup(pegasus))
1039 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1040 usb_kill_urb(pegasus->rx_urb);
1041 goto exit;
1042 }
1043 if ((res = enable_net_traffic(net, pegasus->usb))) {
1044 if (netif_msg_ifup(pegasus))
1045 pr_debug("%s: can't enable_net_traffic() - %d\n",
1046 net->name, res);
1047 res = -EIO;
1048 usb_kill_urb(pegasus->rx_urb);
1049 usb_kill_urb(pegasus->intr_urb);
1050 free_skb_pool(pegasus);
1051 goto exit;
1052 }
1053 set_carrier(net);
1054 netif_start_queue(net);
1055 if (netif_msg_ifup(pegasus))
1056 pr_debug("%s: open\n", net->name);
1057 res = 0;
1058exit:
1059 return res;
1060}
1061
1062static int pegasus_close(struct net_device *net)
1063{
1064 pegasus_t *pegasus = netdev_priv(net);
1065
1066 netif_stop_queue(net);
1067 if (!(pegasus->flags & PEGASUS_UNPLUG))
1068 disable_net_traffic(pegasus);
1069 tasklet_kill(&pegasus->rx_tl);
1070 unlink_all_urbs(pegasus);
1071
1072 return 0;
1073}
1074
1075static void pegasus_get_drvinfo(struct net_device *dev,
1076 struct ethtool_drvinfo *info)
1077{
1078 pegasus_t *pegasus = netdev_priv(dev);
1079 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1080 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1081 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1082}
1083
1084/* also handles three patterns of some kind in hardware */
1085#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1086
1087static void
1088pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1089{
1090 pegasus_t *pegasus = netdev_priv(dev);
1091
1092 wol->supported = WAKE_MAGIC | WAKE_PHY;
1093 wol->wolopts = pegasus->wolopts;
1094}
1095
1096static int
1097pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1098{
1099 pegasus_t *pegasus = netdev_priv(dev);
1100 u8 reg78 = 0x04;
1101
1102 if (wol->wolopts & ~WOL_SUPPORTED)
1103 return -EINVAL;
1104
1105 if (wol->wolopts & WAKE_MAGIC)
1106 reg78 |= 0x80;
1107 if (wol->wolopts & WAKE_PHY)
1108 reg78 |= 0x40;
1109 /* FIXME this 0x10 bit still needs to get set in the chip... */
1110 if (wol->wolopts)
1111 pegasus->eth_regs[0] |= 0x10;
1112 else
1113 pegasus->eth_regs[0] &= ~0x10;
1114 pegasus->wolopts = wol->wolopts;
1115 return set_register(pegasus, WakeupControl, reg78);
1116}
1117
1118static inline void pegasus_reset_wol(struct net_device *dev)
1119{
1120 struct ethtool_wolinfo wol;
1121
1122 memset(&wol, 0, sizeof wol);
1123 (void) pegasus_set_wol(dev, &wol);
1124}
1125
1126static int
1127pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1128{
1129 pegasus_t *pegasus;
1130
1131 if (in_atomic())
1132 return 0;
1133
1134 pegasus = netdev_priv(dev);
1135 mii_ethtool_gset(&pegasus->mii, ecmd);
1136
1137 return 0;
1138}
1139
1140static int
1141pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1142{
1143 pegasus_t *pegasus = netdev_priv(dev);
1144 return mii_ethtool_sset(&pegasus->mii, ecmd);
1145}
1146
1147static int pegasus_nway_reset(struct net_device *dev)
1148{
1149 pegasus_t *pegasus = netdev_priv(dev);
1150 return mii_nway_restart(&pegasus->mii);
1151}
1152
1153static u32 pegasus_get_link(struct net_device *dev)
1154{
1155 pegasus_t *pegasus = netdev_priv(dev);
1156 return mii_link_ok(&pegasus->mii);
1157}
1158
1159static u32 pegasus_get_msglevel(struct net_device *dev)
1160{
1161 pegasus_t *pegasus = netdev_priv(dev);
1162 return pegasus->msg_enable;
1163}
1164
1165static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1166{
1167 pegasus_t *pegasus = netdev_priv(dev);
1168 pegasus->msg_enable = v;
1169}
1170
1171static struct ethtool_ops ops = {
1172 .get_drvinfo = pegasus_get_drvinfo,
1173 .get_settings = pegasus_get_settings,
1174 .set_settings = pegasus_set_settings,
1175 .nway_reset = pegasus_nway_reset,
1176 .get_link = pegasus_get_link,
1177 .get_msglevel = pegasus_get_msglevel,
1178 .set_msglevel = pegasus_set_msglevel,
1179 .get_wol = pegasus_get_wol,
1180 .set_wol = pegasus_set_wol,
1181};
1182
1183static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1184{
1185 __u16 *data = (__u16 *) & rq->ifr_ifru;
1186 pegasus_t *pegasus = netdev_priv(net);
1187 int res;
1188
1189 switch (cmd) {
1190 case SIOCDEVPRIVATE:
1191 data[0] = pegasus->phy;
1192 case SIOCDEVPRIVATE + 1:
1193 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1194 res = 0;
1195 break;
1196 case SIOCDEVPRIVATE + 2:
1197 if (!capable(CAP_NET_ADMIN))
1198 return -EPERM;
1199 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1200 res = 0;
1201 break;
1202 default:
1203 res = -EOPNOTSUPP;
1204 }
1205 return res;
1206}
1207
1208static void pegasus_set_multicast(struct net_device *net)
1209{
1210 pegasus_t *pegasus = netdev_priv(net);
1211
1212 if (net->flags & IFF_PROMISC) {
1213 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1214 if (netif_msg_link(pegasus))
1215 pr_info("%s: Promiscuous mode enabled.\n", net->name);
YOSHIFUJI Hideakiae0a97b2005-05-25 16:07:04 +09001216 } else if (net->mc_count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 (net->flags & IFF_ALLMULTI)) {
1218 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1219 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1220 if (netif_msg_link(pegasus))
1221 pr_info("%s: set allmulti\n", net->name);
1222 } else {
1223 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1224 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1225 }
1226
1227 pegasus->flags |= ETH_REGS_CHANGE;
David Howells7d12e782006-10-05 14:55:46 +01001228 ctrl_callback(pegasus->ctrl_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static __u8 mii_phy_probe(pegasus_t * pegasus)
1232{
1233 int i;
1234 __u16 tmp;
1235
1236 for (i = 0; i < 32; i++) {
1237 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1238 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1239 continue;
1240 else
1241 return i;
1242 }
1243
1244 return 0xff;
1245}
1246
1247static inline void setup_pegasus_II(pegasus_t * pegasus)
1248{
1249 __u8 data = 0xa5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Petko Manolov4a1728a2005-11-15 09:48:23 +02001251 set_register(pegasus, Reg1d, 0);
1252 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 mdelay(100);
1254 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001255 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001257 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Petko Manolov4a1728a2005-11-15 09:48:23 +02001259 set_register(pegasus, 0x83, data);
1260 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (data == 0xa5) {
1263 pegasus->chip = 0x8513;
1264 } else {
1265 pegasus->chip = 0;
1266 }
1267
Petko Manolov4a1728a2005-11-15 09:48:23 +02001268 set_register(pegasus, 0x80, 0xc0);
1269 set_register(pegasus, 0x83, 0xff);
1270 set_register(pegasus, 0x84, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001273 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001275 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278
1279static struct workqueue_struct *pegasus_workqueue = NULL;
1280#define CARRIER_CHECK_DELAY (2 * HZ)
1281
David Howellsc4028952006-11-22 14:57:56 +00001282static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
David Howellsc4028952006-11-22 14:57:56 +00001284 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 set_carrier(pegasus->net);
1286 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1287 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1288 CARRIER_CHECK_DELAY);
1289 }
1290}
1291
1292static int pegasus_probe(struct usb_interface *intf,
1293 const struct usb_device_id *id)
1294{
1295 struct usb_device *dev = interface_to_usbdev(intf);
1296 struct net_device *net;
1297 pegasus_t *pegasus;
1298 int dev_index = id - pegasus_ids;
1299 int res = -ENOMEM;
Joe Perches0795af52007-10-03 17:59:30 -07001300 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 usb_get_dev(dev);
1303 net = alloc_etherdev(sizeof(struct pegasus));
1304 if (!net) {
1305 dev_err(&intf->dev, "can't allocate %s\n", "device");
1306 goto out;
1307 }
1308
1309 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 pegasus->dev_index = dev_index;
1311 init_waitqueue_head(&pegasus->ctrl_wait);
1312
1313 if (!alloc_urbs(pegasus)) {
1314 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1315 goto out1;
1316 }
1317
1318 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1319
David Howellsc4028952006-11-22 14:57:56 +00001320 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 pegasus->intf = intf;
1323 pegasus->usb = dev;
1324 pegasus->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 net->open = pegasus_open;
1326 net->stop = pegasus_close;
1327 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1328 net->tx_timeout = pegasus_tx_timeout;
1329 net->do_ioctl = pegasus_ioctl;
1330 net->hard_start_xmit = pegasus_start_xmit;
1331 net->set_multicast_list = pegasus_set_multicast;
1332 net->get_stats = pegasus_netdev_stats;
1333 SET_ETHTOOL_OPS(net, &ops);
1334 pegasus->mii.dev = net;
1335 pegasus->mii.mdio_read = mdio_read;
1336 pegasus->mii.mdio_write = mdio_write;
1337 pegasus->mii.phy_id_mask = 0x1f;
1338 pegasus->mii.reg_num_mask = 0x1f;
1339 spin_lock_init(&pegasus->rx_pool_lock);
1340 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1341 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1342
1343 pegasus->features = usb_dev_id[dev_index].private;
1344 get_interrupt_interval(pegasus);
1345 if (reset_mac(pegasus)) {
1346 dev_err(&intf->dev, "can't reset MAC\n");
1347 res = -EIO;
1348 goto out2;
1349 }
1350 set_ethernet_addr(pegasus);
1351 fill_skb_pool(pegasus);
1352 if (pegasus->features & PEGASUS_II) {
1353 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1354 setup_pegasus_II(pegasus);
1355 }
1356 pegasus->phy = mii_phy_probe(pegasus);
1357 if (pegasus->phy == 0xff) {
1358 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1359 pegasus->phy = 1;
1360 }
1361 pegasus->mii.phy_id = pegasus->phy;
1362 usb_set_intfdata(intf, pegasus);
1363 SET_NETDEV_DEV(net, &intf->dev);
1364 pegasus_reset_wol(net);
1365 res = register_netdev(net);
1366 if (res)
1367 goto out3;
1368 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1369 CARRIER_CHECK_DELAY);
1370
Joe Perches0795af52007-10-03 17:59:30 -07001371 dev_info(&intf->dev, "%s, %s, %s\n",
1372 net->name,
1373 usb_dev_id[dev_index].name,
1374 print_mac(mac, net->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return 0;
1376
1377out3:
1378 usb_set_intfdata(intf, NULL);
1379 free_skb_pool(pegasus);
1380out2:
1381 free_all_urbs(pegasus);
1382out1:
1383 free_netdev(net);
1384out:
1385 usb_put_dev(dev);
1386 return res;
1387}
1388
1389static void pegasus_disconnect(struct usb_interface *intf)
1390{
1391 struct pegasus *pegasus = usb_get_intfdata(intf);
1392
1393 usb_set_intfdata(intf, NULL);
1394 if (!pegasus) {
1395 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1396 return;
1397 }
1398
1399 pegasus->flags |= PEGASUS_UNPLUG;
1400 cancel_delayed_work(&pegasus->carrier_check);
1401 unregister_netdev(pegasus->net);
1402 usb_put_dev(interface_to_usbdev(intf));
Kevin Vigora85a46f2005-09-22 00:49:24 -07001403 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 free_all_urbs(pegasus);
1405 free_skb_pool(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001406 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001408 pegasus->rx_skb = NULL;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 free_netdev(pegasus->net);
1411}
1412
David Brownell27d72e82005-04-18 17:39:22 -07001413static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct pegasus *pegasus = usb_get_intfdata(intf);
1416
1417 netif_device_detach (pegasus->net);
David Brownell7e713b82006-05-01 14:02:45 -07001418 cancel_delayed_work(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001419 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001420 usb_kill_urb(pegasus->rx_urb);
1421 usb_kill_urb(pegasus->intr_urb);
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return 0;
1424}
1425
1426static int pegasus_resume (struct usb_interface *intf)
1427{
1428 struct pegasus *pegasus = usb_get_intfdata(intf);
1429
1430 netif_device_attach (pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001431 if (netif_running(pegasus->net)) {
1432 pegasus->rx_urb->status = 0;
1433 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001434 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001435
1436 pegasus->intr_urb->status = 0;
1437 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001438 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001439 }
David Brownell7e713b82006-05-01 14:02:45 -07001440 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1441 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return 0;
1443}
1444
1445static struct usb_driver pegasus_driver = {
1446 .name = driver_name,
1447 .probe = pegasus_probe,
1448 .disconnect = pegasus_disconnect,
1449 .id_table = pegasus_ids,
1450 .suspend = pegasus_suspend,
1451 .resume = pegasus_resume,
1452};
1453
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001454static void parse_id(char *id)
1455{
1456 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1457 char *token, *name=NULL;
1458
1459 if ((token = strsep(&id, ":")) != NULL)
1460 name = token;
1461 /* name now points to a null terminated string*/
1462 if ((token = strsep(&id, ":")) != NULL)
1463 vendor_id = simple_strtoul(token, NULL, 16);
1464 if ((token = strsep(&id, ":")) != NULL)
1465 device_id = simple_strtoul(token, NULL, 16);
1466 flags = simple_strtoul(id, NULL, 16);
1467 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1468 driver_name, name, vendor_id, device_id, flags);
1469
1470 if (vendor_id > 0x10000 || vendor_id == 0)
1471 return;
1472 if (device_id > 0x10000 || device_id == 0)
1473 return;
1474
1475 for (i=0; usb_dev_id[i].name; i++);
1476 usb_dev_id[i].name = name;
1477 usb_dev_id[i].vendor = vendor_id;
1478 usb_dev_id[i].device = device_id;
1479 usb_dev_id[i].private = flags;
1480 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1481 pegasus_ids[i].idVendor = vendor_id;
1482 pegasus_ids[i].idProduct = device_id;
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static int __init pegasus_init(void)
1486{
1487 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001488 if (devid)
1489 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1491 if (!pegasus_workqueue)
1492 return -ENOMEM;
1493 return usb_register(&pegasus_driver);
1494}
1495
1496static void __exit pegasus_exit(void)
1497{
1498 destroy_workqueue(pegasus_workqueue);
1499 usb_deregister(&pegasus_driver);
1500}
1501
1502module_init(pegasus_init);
1503module_exit(pegasus_exit);