blob: d8c7adabf201a892d018224f3b8ce7c7387b9624 [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 Manolov4a1728a2005-11-15 09:48:23 +020048#define DRIVER_VERSION "v0.6.13 (2005/11/13)"
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static struct usb_eth_dev usb_dev_id[] = {
62#define PEGASUS_DEV(pn, vid, pid, flags) \
63 {.name = pn, .vendor = vid, .device = pid, .private = flags},
64#include "pegasus.h"
65#undef PEGASUS_DEV
66 {NULL, 0, 0, 0}
67};
68
69static struct usb_device_id pegasus_ids[] = {
70#define PEGASUS_DEV(pn, vid, pid, flags) \
71 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
72#include "pegasus.h"
73#undef PEGASUS_DEV
74 {}
75};
76
77MODULE_AUTHOR(DRIVER_AUTHOR);
78MODULE_DESCRIPTION(DRIVER_DESC);
79MODULE_LICENSE("GPL");
80module_param(loopback, bool, 0);
81module_param(mii_mode, bool, 0);
82MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
83MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
84
85/* use ethtool to change the level for any given device */
86static int msg_level = -1;
87module_param (msg_level, int, 0);
88MODULE_PARM_DESC (msg_level, "Override default message level");
89
90MODULE_DEVICE_TABLE(usb, pegasus_ids);
91
92static int update_eth_regs_async(pegasus_t *);
93/* Aargh!!! I _really_ hate such tweaks */
94static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
95{
96 pegasus_t *pegasus = urb->context;
97
98 if (!pegasus)
99 return;
100
101 switch (urb->status) {
102 case 0:
103 if (pegasus->flags & ETH_REGS_CHANGE) {
104 pegasus->flags &= ~ETH_REGS_CHANGE;
105 pegasus->flags |= ETH_REGS_CHANGED;
106 update_eth_regs_async(pegasus);
107 return;
108 }
109 break;
110 case -EINPROGRESS:
111 return;
112 case -ENOENT:
113 break;
114 default:
115 if (netif_msg_drv(pegasus))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200116 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 __FUNCTION__, urb->status);
118 }
119 pegasus->flags &= ~ETH_REGS_CHANGED;
120 wake_up(&pegasus->ctrl_wait);
121}
122
123static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
124 void *data)
125{
126 int ret;
127 char *buffer;
128 DECLARE_WAITQUEUE(wait, current);
129
130 buffer = kmalloc(size, GFP_KERNEL);
131 if (!buffer) {
132 if (netif_msg_drv(pegasus))
133 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
134 __FUNCTION__);
135 return -ENOMEM;
136 }
137 add_wait_queue(&pegasus->ctrl_wait, &wait);
138 set_current_state(TASK_UNINTERRUPTIBLE);
139 while (pegasus->flags & ETH_REGS_CHANGED)
140 schedule();
141 remove_wait_queue(&pegasus->ctrl_wait, &wait);
142 set_current_state(TASK_RUNNING);
143
144 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
145 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
146 pegasus->dr.wValue = cpu_to_le16(0);
147 pegasus->dr.wIndex = cpu_to_le16p(&indx);
148 pegasus->dr.wLength = cpu_to_le16p(&size);
149 pegasus->ctrl_urb->transfer_buffer_length = size;
150
151 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
152 usb_rcvctrlpipe(pegasus->usb, 0),
153 (char *) &pegasus->dr,
154 buffer, size, ctrl_callback, pegasus);
155
156 add_wait_queue(&pegasus->ctrl_wait, &wait);
157 set_current_state(TASK_UNINTERRUPTIBLE);
158
159 /* using ATOMIC, we'd never wake up if we slept */
160 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
161 if (netif_msg_drv(pegasus))
162 dev_err(&pegasus->intf->dev, "%s, status %d\n",
163 __FUNCTION__, ret);
164 goto out;
165 }
166
167 schedule();
168out:
169 remove_wait_queue(&pegasus->ctrl_wait, &wait);
170 memcpy(data, buffer, size);
171 kfree(buffer);
172
173 return ret;
174}
175
176static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
177 void *data)
178{
179 int ret;
180 char *buffer;
181 DECLARE_WAITQUEUE(wait, current);
182
183 buffer = kmalloc(size, GFP_KERNEL);
184 if (!buffer) {
185 if (netif_msg_drv(pegasus))
186 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
187 __FUNCTION__);
188 return -ENOMEM;
189 }
190 memcpy(buffer, data, size);
191
192 add_wait_queue(&pegasus->ctrl_wait, &wait);
193 set_current_state(TASK_UNINTERRUPTIBLE);
194 while (pegasus->flags & ETH_REGS_CHANGED)
195 schedule();
196 remove_wait_queue(&pegasus->ctrl_wait, &wait);
197 set_current_state(TASK_RUNNING);
198
199 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
200 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
201 pegasus->dr.wValue = cpu_to_le16(0);
202 pegasus->dr.wIndex = cpu_to_le16p(&indx);
203 pegasus->dr.wLength = cpu_to_le16p(&size);
204 pegasus->ctrl_urb->transfer_buffer_length = size;
205
206 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
207 usb_sndctrlpipe(pegasus->usb, 0),
208 (char *) &pegasus->dr,
209 buffer, size, ctrl_callback, pegasus);
210
211 add_wait_queue(&pegasus->ctrl_wait, &wait);
212 set_current_state(TASK_UNINTERRUPTIBLE);
213
214 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
215 if (netif_msg_drv(pegasus))
216 dev_err(&pegasus->intf->dev, "%s, status %d\n",
217 __FUNCTION__, ret);
218 goto out;
219 }
220
221 schedule();
222out:
223 remove_wait_queue(&pegasus->ctrl_wait, &wait);
224 kfree(buffer);
225
226 return ret;
227}
228
229static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
230{
231 int ret;
232 char *tmp;
233 DECLARE_WAITQUEUE(wait, current);
234
235 tmp = kmalloc(1, GFP_KERNEL);
236 if (!tmp) {
237 if (netif_msg_drv(pegasus))
238 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
239 __FUNCTION__);
240 return -ENOMEM;
241 }
242 memcpy(tmp, &data, 1);
243 add_wait_queue(&pegasus->ctrl_wait, &wait);
244 set_current_state(TASK_UNINTERRUPTIBLE);
245 while (pegasus->flags & ETH_REGS_CHANGED)
246 schedule();
247 remove_wait_queue(&pegasus->ctrl_wait, &wait);
248 set_current_state(TASK_RUNNING);
249
250 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
251 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
252 pegasus->dr.wValue = cpu_to_le16(data);
253 pegasus->dr.wIndex = cpu_to_le16p(&indx);
254 pegasus->dr.wLength = cpu_to_le16(1);
255 pegasus->ctrl_urb->transfer_buffer_length = 1;
256
257 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
258 usb_sndctrlpipe(pegasus->usb, 0),
259 (char *) &pegasus->dr,
260 &tmp, 1, ctrl_callback, pegasus);
261
262 add_wait_queue(&pegasus->ctrl_wait, &wait);
263 set_current_state(TASK_UNINTERRUPTIBLE);
264
265 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
266 if (netif_msg_drv(pegasus))
267 dev_err(&pegasus->intf->dev, "%s, status %d\n",
268 __FUNCTION__, ret);
269 goto out;
270 }
271
272 schedule();
273out:
274 remove_wait_queue(&pegasus->ctrl_wait, &wait);
275 kfree(tmp);
276
277 return ret;
278}
279
280static int update_eth_regs_async(pegasus_t * pegasus)
281{
282 int ret;
283
284 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
285 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
286 pegasus->dr.wValue = 0;
287 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
288 pegasus->dr.wLength = cpu_to_le16(3);
289 pegasus->ctrl_urb->transfer_buffer_length = 3;
290
291 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
292 usb_sndctrlpipe(pegasus->usb, 0),
293 (char *) &pegasus->dr,
294 pegasus->eth_regs, 3, ctrl_callback, pegasus);
295
296 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC)))
297 if (netif_msg_drv(pegasus))
298 dev_err(&pegasus->intf->dev, "%s, status %d\n",
299 __FUNCTION__, ret);
300
301 return ret;
302}
303
304static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
305{
306 int i;
307 __u8 data[4] = { phy, 0, 0, indx };
308 __le16 regdi;
309 int ret;
310
Petko Manolov4a1728a2005-11-15 09:48:23 +0200311 set_register(pegasus, PhyCtrl, 0);
312 set_registers(pegasus, PhyAddr, sizeof (data), data);
313 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 for (i = 0; i < REG_TIMEOUT; i++) {
315 ret = get_registers(pegasus, PhyCtrl, 1, data);
316 if (data[0] & PHY_DONE)
317 break;
318 }
319 if (i < REG_TIMEOUT) {
320 ret = get_registers(pegasus, PhyData, 2, &regdi);
321 *regd = le16_to_cpu(regdi);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 if (netif_msg_drv(pegasus))
325 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
326
Petko Manolov4a1728a2005-11-15 09:48:23 +0200327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330static int mdio_read(struct net_device *dev, int phy_id, int loc)
331{
332 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
333 u16 res;
334
335 read_mii_word(pegasus, phy_id, loc, &res);
336 return (int)res;
337}
338
339static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
340{
341 int i;
342 __u8 data[4] = { phy, 0, 0, indx };
343 int ret;
344
345 data[1] = (u8) regd;
346 data[2] = (u8) (regd >> 8);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200347 set_register(pegasus, PhyCtrl, 0);
348 set_registers(pegasus, PhyAddr, sizeof(data), data);
349 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 for (i = 0; i < REG_TIMEOUT; i++) {
351 ret = get_registers(pegasus, PhyCtrl, 1, data);
352 if (data[0] & PHY_DONE)
353 break;
354 }
355 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (netif_msg_drv(pegasus))
359 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200360 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
364{
365 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
366
367 write_mii_word(pegasus, phy_id, loc, val);
368}
369
370static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
371{
372 int i;
373 __u8 tmp;
374 __le16 retdatai;
375 int ret;
376
Petko Manolov4a1728a2005-11-15 09:48:23 +0200377 set_register(pegasus, EpromCtrl, 0);
378 set_register(pegasus, EpromOffset, index);
379 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 for (i = 0; i < REG_TIMEOUT; i++) {
382 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
383 if (tmp & EPROM_DONE)
384 break;
385 }
386 if (i < REG_TIMEOUT) {
387 ret = get_registers(pegasus, EpromData, 2, &retdatai);
388 *retdata = le16_to_cpu(retdatai);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 if (netif_msg_drv(pegasus))
393 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200394 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397#ifdef PEGASUS_WRITE_EEPROM
398static inline void enable_eprom_write(pegasus_t * pegasus)
399{
400 __u8 tmp;
401 int ret;
402
Petko Manolov4a1728a2005-11-15 09:48:23 +0200403 get_registers(pegasus, EthCtrl2, 1, &tmp);
404 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static inline void disable_eprom_write(pegasus_t * pegasus)
408{
409 __u8 tmp;
410 int ret;
411
Petko Manolov4a1728a2005-11-15 09:48:23 +0200412 get_registers(pegasus, EthCtrl2, 1, &tmp);
413 set_register(pegasus, EpromCtrl, 0);
414 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
418{
419 int i;
420 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
421 int ret;
422
Petko Manolov4a1728a2005-11-15 09:48:23 +0200423 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200425 set_register(pegasus, EpromOffset, index);
426 set_registers(pegasus, EpromData, 2, &data);
427 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 for (i = 0; i < REG_TIMEOUT; i++) {
430 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
431 if (tmp & EPROM_DONE)
432 break;
433 }
434 disable_eprom_write(pegasus);
435 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (netif_msg_drv(pegasus))
438 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200439 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441#endif /* PEGASUS_WRITE_EEPROM */
442
443static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
444{
445 int i;
446 __u16 w16;
447
448 for (i = 0; i < 3; i++) {
449 read_eprom_word(pegasus, i, &w16);
450 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
451 }
452}
453
454static void set_ethernet_addr(pegasus_t * pegasus)
455{
456 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 get_node_id(pegasus, node_id);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200459 set_registers(pegasus, EthID, sizeof (node_id), node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
461}
462
463static inline int reset_mac(pegasus_t * pegasus)
464{
465 __u8 data = 0x8;
466 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Petko Manolov4a1728a2005-11-15 09:48:23 +0200468 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200470 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (~data & 0x08) {
472 if (loopback & 1)
473 break;
474 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200475 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200477 set_register(pegasus, Gpio1, 0x26);
478 set_register(pegasus, Gpio0, pegasus->features);
479 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 }
482 }
483 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200484 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
487 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200488 set_register(pegasus, Gpio0, 0x24);
489 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
492 __u16 auxmode;
493 read_mii_word(pegasus, 3, 0x1b, &auxmode);
494 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
495 }
496
497 return 0;
498}
499
500static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
501{
502 __u16 linkpart;
503 __u8 data[4];
504 pegasus_t *pegasus = netdev_priv(dev);
505 int ret;
506
507 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
508 data[0] = 0xc9;
509 data[1] = 0;
510 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
511 data[1] |= 0x20; /* set full duplex */
512 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
513 data[1] |= 0x10; /* set 100 Mbps */
514 if (mii_mode)
515 data[1] = 0;
516 data[2] = (loopback & 1) ? 0x09 : 0x01;
517
518 memcpy(pegasus->eth_regs, data, sizeof (data));
519 ret = set_registers(pegasus, EthCtrl0, 3, data);
520
521 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
522 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
523 u16 auxmode;
524 read_mii_word(pegasus, 0, 0x1b, &auxmode);
525 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
526 }
527
Petko Manolov4a1728a2005-11-15 09:48:23 +0200528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static void fill_skb_pool(pegasus_t * pegasus)
532{
533 int i;
534
535 for (i = 0; i < RX_SKBS; i++) {
536 if (pegasus->rx_pool[i])
537 continue;
538 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
539 /*
540 ** we give up if the allocation fail. the tasklet will be
541 ** rescheduled again anyway...
542 */
543 if (pegasus->rx_pool[i] == NULL)
544 return;
545 pegasus->rx_pool[i]->dev = pegasus->net;
546 skb_reserve(pegasus->rx_pool[i], 2);
547 }
548}
549
550static void free_skb_pool(pegasus_t * pegasus)
551{
552 int i;
553
554 for (i = 0; i < RX_SKBS; i++) {
555 if (pegasus->rx_pool[i]) {
556 dev_kfree_skb(pegasus->rx_pool[i]);
557 pegasus->rx_pool[i] = NULL;
558 }
559 }
560}
561
562static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
563{
564 int i;
565 struct sk_buff *skb;
566
567 for (i = 0; i < RX_SKBS; i++) {
568 if (likely(pegasus->rx_pool[i] != NULL)) {
569 skb = pegasus->rx_pool[i];
570 pegasus->rx_pool[i] = NULL;
571 return skb;
572 }
573 }
574 return NULL;
575}
576
577static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
578{
579 pegasus_t *pegasus = urb->context;
580 struct net_device *net;
581 int rx_status, count = urb->actual_length;
582 u8 *buf = urb->transfer_buffer;
583 __u16 pkt_len;
584
585 if (!pegasus)
586 return;
587
588 net = pegasus->net;
589 if (!netif_device_present(net) || !netif_running(net))
590 return;
591
592 switch (urb->status) {
593 case 0:
594 break;
595 case -ETIMEDOUT:
596 if (netif_msg_rx_err(pegasus))
597 pr_debug("%s: reset MAC\n", net->name);
598 pegasus->flags &= ~PEGASUS_RX_BUSY;
599 break;
600 case -EPIPE: /* stall, or disconnect from TT */
601 /* FIXME schedule work to clear the halt */
602 if (netif_msg_rx_err(pegasus))
603 printk(KERN_WARNING "%s: no rx stall recovery\n",
604 net->name);
605 return;
606 case -ENOENT:
607 case -ECONNRESET:
608 case -ESHUTDOWN:
609 if (netif_msg_ifdown(pegasus))
610 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
611 return;
612 default:
613 if (netif_msg_rx_err(pegasus))
614 pr_debug("%s: RX status %d\n", net->name, urb->status);
615 goto goon;
616 }
617
618 if (!count || count < 4)
619 goto goon;
620
621 rx_status = buf[count - 2];
622 if (rx_status & 0x1e) {
623 if (netif_msg_rx_err(pegasus))
624 pr_debug("%s: RX packet error %x\n",
625 net->name, rx_status);
626 pegasus->stats.rx_errors++;
627 if (rx_status & 0x06) // long or runt
628 pegasus->stats.rx_length_errors++;
629 if (rx_status & 0x08)
630 pegasus->stats.rx_crc_errors++;
631 if (rx_status & 0x10) // extra bits
632 pegasus->stats.rx_frame_errors++;
633 goto goon;
634 }
635 if (pegasus->chip == 0x8513) {
636 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
637 pkt_len &= 0x0fff;
638 pegasus->rx_skb->data += 2;
639 } else {
640 pkt_len = buf[count - 3] << 8;
641 pkt_len += buf[count - 4];
642 pkt_len &= 0xfff;
643 pkt_len -= 8;
644 }
645
646 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700647 * If the packet is unreasonably long, quietly drop it rather than
648 * kernel panicing by calling skb_put.
649 */
650 if (pkt_len > PEGASUS_MTU)
651 goto goon;
652
653 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * at this point we are sure pegasus->rx_skb != NULL
655 * so we go ahead and pass up the packet.
656 */
657 skb_put(pegasus->rx_skb, pkt_len);
658 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
659 netif_rx(pegasus->rx_skb);
660 pegasus->stats.rx_packets++;
661 pegasus->stats.rx_bytes += pkt_len;
662
663 if (pegasus->flags & PEGASUS_UNPLUG)
664 return;
665
666 spin_lock(&pegasus->rx_pool_lock);
667 pegasus->rx_skb = pull_skb(pegasus);
668 spin_unlock(&pegasus->rx_pool_lock);
669
670 if (pegasus->rx_skb == NULL)
671 goto tl_sched;
672goon:
673 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
674 usb_rcvbulkpipe(pegasus->usb, 1),
675 pegasus->rx_skb->data, PEGASUS_MTU + 8,
676 read_bulk_callback, pegasus);
677 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
678 pegasus->flags |= PEGASUS_RX_URB_FAIL;
679 goto tl_sched;
680 } else {
681 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
682 }
683
684 return;
685
686tl_sched:
687 tasklet_schedule(&pegasus->rx_tl);
688}
689
690static void rx_fixup(unsigned long data)
691{
692 pegasus_t *pegasus;
693 unsigned long flags;
694
695 pegasus = (pegasus_t *) data;
696 if (pegasus->flags & PEGASUS_UNPLUG)
697 return;
698
699 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
700 fill_skb_pool(pegasus);
701 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
702 if (pegasus->rx_skb)
703 goto try_again;
704 if (pegasus->rx_skb == NULL) {
705 pegasus->rx_skb = pull_skb(pegasus);
706 }
707 if (pegasus->rx_skb == NULL) {
708 if (netif_msg_rx_err(pegasus))
709 printk(KERN_WARNING "%s: low on memory\n",
710 pegasus->net->name);
711 tasklet_schedule(&pegasus->rx_tl);
712 goto done;
713 }
714 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
715 usb_rcvbulkpipe(pegasus->usb, 1),
716 pegasus->rx_skb->data, PEGASUS_MTU + 8,
717 read_bulk_callback, pegasus);
718try_again:
719 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
720 pegasus->flags |= PEGASUS_RX_URB_FAIL;
721 tasklet_schedule(&pegasus->rx_tl);
722 } else {
723 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
724 }
725done:
726 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
727}
728
729static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
730{
731 pegasus_t *pegasus = urb->context;
732 struct net_device *net = pegasus->net;
733
734 if (!pegasus)
735 return;
736
737 if (!netif_device_present(net) || !netif_running(net))
738 return;
739
740 switch (urb->status) {
741 case -EPIPE:
742 /* FIXME schedule_work() to clear the tx halt */
743 netif_stop_queue(net);
744 if (netif_msg_tx_err(pegasus))
745 printk(KERN_WARNING "%s: no tx stall recovery\n",
746 net->name);
747 return;
748 case -ENOENT:
749 case -ECONNRESET:
750 case -ESHUTDOWN:
751 if (netif_msg_ifdown(pegasus))
752 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
753 return;
754 default:
755 if (netif_msg_tx_err(pegasus))
756 pr_info("%s: TX status %d\n", net->name, urb->status);
757 /* FALL THROUGH */
758 case 0:
759 break;
760 }
761
762 net->trans_start = jiffies;
763 netif_wake_queue(net);
764}
765
766static void intr_callback(struct urb *urb, struct pt_regs *regs)
767{
768 pegasus_t *pegasus = urb->context;
769 struct net_device *net;
770 int status;
771
772 if (!pegasus)
773 return;
774 net = pegasus->net;
775
776 switch (urb->status) {
777 case 0:
778 break;
779 case -ECONNRESET: /* unlink */
780 case -ENOENT:
781 case -ESHUTDOWN:
782 return;
783 default:
784 /* some Pegasus-I products report LOTS of data
785 * toggle errors... avoid log spamming
786 */
787 if (netif_msg_timer(pegasus))
788 pr_debug("%s: intr status %d\n", net->name,
789 urb->status);
790 }
791
792 if (urb->actual_length >= 6) {
793 u8 * d = urb->transfer_buffer;
794
795 /* byte 0 == tx_status1, reg 2B */
796 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
797 |LATE_COL|JABBER_TIMEOUT)) {
798 pegasus->stats.tx_errors++;
799 if (d[0] & TX_UNDERRUN)
800 pegasus->stats.tx_fifo_errors++;
801 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
802 pegasus->stats.tx_aborted_errors++;
803 if (d[0] & LATE_COL)
804 pegasus->stats.tx_window_errors++;
805 }
806
807 /* d[5].LINK_STATUS lies on some adapters.
808 * d[0].NO_CARRIER kicks in only with failed TX.
809 * ... so monitoring with MII may be safest.
810 */
811 if (d[0] & NO_CARRIER)
812 netif_carrier_off(net);
813 else
814 netif_carrier_on(net);
815
816 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
817 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
818 }
819
820 status = usb_submit_urb(urb, SLAB_ATOMIC);
821 if (status && netif_msg_timer(pegasus))
822 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
823 net->name, status);
824}
825
826static void pegasus_tx_timeout(struct net_device *net)
827{
828 pegasus_t *pegasus = netdev_priv(net);
829 if (netif_msg_timer(pegasus))
830 printk(KERN_WARNING "%s: tx timeout\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 usb_unlink_urb(pegasus->tx_urb);
832 pegasus->stats.tx_errors++;
833}
834
835static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
836{
837 pegasus_t *pegasus = netdev_priv(net);
838 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
839 int res;
840 __u16 l16 = skb->len;
841
842 netif_stop_queue(net);
843
844 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
845 memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
846 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
847 usb_sndbulkpipe(pegasus->usb, 2),
848 pegasus->tx_buff, count,
849 write_bulk_callback, pegasus);
850 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
851 if (netif_msg_tx_err(pegasus))
852 printk(KERN_WARNING "%s: fail tx, %d\n",
853 net->name, res);
854 switch (res) {
855 case -EPIPE: /* stall, or disconnect from TT */
856 /* cleanup should already have been scheduled */
857 break;
858 case -ENODEV: /* disconnect() upcoming */
859 break;
860 default:
861 pegasus->stats.tx_errors++;
862 netif_start_queue(net);
863 }
864 } else {
865 pegasus->stats.tx_packets++;
866 pegasus->stats.tx_bytes += skb->len;
867 net->trans_start = jiffies;
868 }
869 dev_kfree_skb(skb);
870
871 return 0;
872}
873
874static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
875{
876 return &((pegasus_t *) netdev_priv(dev))->stats;
877}
878
879static inline void disable_net_traffic(pegasus_t * pegasus)
880{
881 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Petko Manolov4a1728a2005-11-15 09:48:23 +0200883 set_registers(pegasus, EthCtrl0, 2, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static inline void get_interrupt_interval(pegasus_t * pegasus)
887{
888 __u8 data[2];
889
890 read_eprom_word(pegasus, 4, (__u16 *) data);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700891 if (pegasus->usb->speed != USB_SPEED_HIGH) {
892 if (data[1] < 0x80) {
893 if (netif_msg_timer(pegasus))
894 dev_info(&pegasus->intf->dev, "intr interval "
895 "changed from %ums to %ums\n",
896 data[1], 0x80);
897 data[1] = 0x80;
898#ifdef PEGASUS_WRITE_EEPROM
899 write_eprom_word(pegasus, 4, *(__u16 *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 pegasus->intr_interval = data[1];
904}
905
906static void set_carrier(struct net_device *net)
907{
908 pegasus_t *pegasus = netdev_priv(net);
909 u16 tmp;
910
Kevin Vigora85a46f2005-09-22 00:49:24 -0700911 if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (tmp & BMSR_LSTATUS)
915 netif_carrier_on(net);
916 else
917 netif_carrier_off(net);
918}
919
920static void free_all_urbs(pegasus_t * pegasus)
921{
922 usb_free_urb(pegasus->intr_urb);
923 usb_free_urb(pegasus->tx_urb);
924 usb_free_urb(pegasus->rx_urb);
925 usb_free_urb(pegasus->ctrl_urb);
926}
927
928static void unlink_all_urbs(pegasus_t * pegasus)
929{
930 usb_kill_urb(pegasus->intr_urb);
931 usb_kill_urb(pegasus->tx_urb);
932 usb_kill_urb(pegasus->rx_urb);
933 usb_kill_urb(pegasus->ctrl_urb);
934}
935
936static int alloc_urbs(pegasus_t * pegasus)
937{
938 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
939 if (!pegasus->ctrl_urb) {
940 return 0;
941 }
942 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
943 if (!pegasus->rx_urb) {
944 usb_free_urb(pegasus->ctrl_urb);
945 return 0;
946 }
947 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
948 if (!pegasus->tx_urb) {
949 usb_free_urb(pegasus->rx_urb);
950 usb_free_urb(pegasus->ctrl_urb);
951 return 0;
952 }
953 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
954 if (!pegasus->intr_urb) {
955 usb_free_urb(pegasus->tx_urb);
956 usb_free_urb(pegasus->rx_urb);
957 usb_free_urb(pegasus->ctrl_urb);
958 return 0;
959 }
960
961 return 1;
962}
963
964static int pegasus_open(struct net_device *net)
965{
966 pegasus_t *pegasus = netdev_priv(net);
967 int res;
968
969 if (pegasus->rx_skb == NULL)
970 pegasus->rx_skb = pull_skb(pegasus);
971 /*
972 ** Note: no point to free the pool. it is empty :-)
973 */
974 if (!pegasus->rx_skb)
975 return -ENOMEM;
976
977 res = set_registers(pegasus, EthID, 6, net->dev_addr);
978
979 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
980 usb_rcvbulkpipe(pegasus->usb, 1),
981 pegasus->rx_skb->data, PEGASUS_MTU + 8,
982 read_bulk_callback, pegasus);
983 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
984 if (netif_msg_ifup(pegasus))
985 pr_debug("%s: failed rx_urb, %d", net->name, res);
986 goto exit;
987 }
988
989 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
990 usb_rcvintpipe(pegasus->usb, 3),
991 pegasus->intr_buff, sizeof (pegasus->intr_buff),
992 intr_callback, pegasus, pegasus->intr_interval);
993 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
994 if (netif_msg_ifup(pegasus))
995 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
996 usb_kill_urb(pegasus->rx_urb);
997 goto exit;
998 }
999 if ((res = enable_net_traffic(net, pegasus->usb))) {
1000 if (netif_msg_ifup(pegasus))
1001 pr_debug("%s: can't enable_net_traffic() - %d\n",
1002 net->name, res);
1003 res = -EIO;
1004 usb_kill_urb(pegasus->rx_urb);
1005 usb_kill_urb(pegasus->intr_urb);
1006 free_skb_pool(pegasus);
1007 goto exit;
1008 }
1009 set_carrier(net);
1010 netif_start_queue(net);
1011 if (netif_msg_ifup(pegasus))
1012 pr_debug("%s: open\n", net->name);
1013 res = 0;
1014exit:
1015 return res;
1016}
1017
1018static int pegasus_close(struct net_device *net)
1019{
1020 pegasus_t *pegasus = netdev_priv(net);
1021
1022 netif_stop_queue(net);
1023 if (!(pegasus->flags & PEGASUS_UNPLUG))
1024 disable_net_traffic(pegasus);
1025 tasklet_kill(&pegasus->rx_tl);
1026 unlink_all_urbs(pegasus);
1027
1028 return 0;
1029}
1030
1031static void pegasus_get_drvinfo(struct net_device *dev,
1032 struct ethtool_drvinfo *info)
1033{
1034 pegasus_t *pegasus = netdev_priv(dev);
1035 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1036 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1037 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1038}
1039
1040/* also handles three patterns of some kind in hardware */
1041#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1042
1043static void
1044pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1045{
1046 pegasus_t *pegasus = netdev_priv(dev);
1047
1048 wol->supported = WAKE_MAGIC | WAKE_PHY;
1049 wol->wolopts = pegasus->wolopts;
1050}
1051
1052static int
1053pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1054{
1055 pegasus_t *pegasus = netdev_priv(dev);
1056 u8 reg78 = 0x04;
1057
1058 if (wol->wolopts & ~WOL_SUPPORTED)
1059 return -EINVAL;
1060
1061 if (wol->wolopts & WAKE_MAGIC)
1062 reg78 |= 0x80;
1063 if (wol->wolopts & WAKE_PHY)
1064 reg78 |= 0x40;
1065 /* FIXME this 0x10 bit still needs to get set in the chip... */
1066 if (wol->wolopts)
1067 pegasus->eth_regs[0] |= 0x10;
1068 else
1069 pegasus->eth_regs[0] &= ~0x10;
1070 pegasus->wolopts = wol->wolopts;
1071 return set_register(pegasus, WakeupControl, reg78);
1072}
1073
1074static inline void pegasus_reset_wol(struct net_device *dev)
1075{
1076 struct ethtool_wolinfo wol;
1077
1078 memset(&wol, 0, sizeof wol);
1079 (void) pegasus_set_wol(dev, &wol);
1080}
1081
1082static int
1083pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1084{
1085 pegasus_t *pegasus;
1086
1087 if (in_atomic())
1088 return 0;
1089
1090 pegasus = netdev_priv(dev);
1091 mii_ethtool_gset(&pegasus->mii, ecmd);
1092
1093 return 0;
1094}
1095
1096static int
1097pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1098{
1099 pegasus_t *pegasus = netdev_priv(dev);
1100 return mii_ethtool_sset(&pegasus->mii, ecmd);
1101}
1102
1103static int pegasus_nway_reset(struct net_device *dev)
1104{
1105 pegasus_t *pegasus = netdev_priv(dev);
1106 return mii_nway_restart(&pegasus->mii);
1107}
1108
1109static u32 pegasus_get_link(struct net_device *dev)
1110{
1111 pegasus_t *pegasus = netdev_priv(dev);
1112 return mii_link_ok(&pegasus->mii);
1113}
1114
1115static u32 pegasus_get_msglevel(struct net_device *dev)
1116{
1117 pegasus_t *pegasus = netdev_priv(dev);
1118 return pegasus->msg_enable;
1119}
1120
1121static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1122{
1123 pegasus_t *pegasus = netdev_priv(dev);
1124 pegasus->msg_enable = v;
1125}
1126
1127static struct ethtool_ops ops = {
1128 .get_drvinfo = pegasus_get_drvinfo,
1129 .get_settings = pegasus_get_settings,
1130 .set_settings = pegasus_set_settings,
1131 .nway_reset = pegasus_nway_reset,
1132 .get_link = pegasus_get_link,
1133 .get_msglevel = pegasus_get_msglevel,
1134 .set_msglevel = pegasus_set_msglevel,
1135 .get_wol = pegasus_get_wol,
1136 .set_wol = pegasus_set_wol,
1137};
1138
1139static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1140{
1141 __u16 *data = (__u16 *) & rq->ifr_ifru;
1142 pegasus_t *pegasus = netdev_priv(net);
1143 int res;
1144
1145 switch (cmd) {
1146 case SIOCDEVPRIVATE:
1147 data[0] = pegasus->phy;
1148 case SIOCDEVPRIVATE + 1:
1149 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1150 res = 0;
1151 break;
1152 case SIOCDEVPRIVATE + 2:
1153 if (!capable(CAP_NET_ADMIN))
1154 return -EPERM;
1155 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1156 res = 0;
1157 break;
1158 default:
1159 res = -EOPNOTSUPP;
1160 }
1161 return res;
1162}
1163
1164static void pegasus_set_multicast(struct net_device *net)
1165{
1166 pegasus_t *pegasus = netdev_priv(net);
1167
1168 if (net->flags & IFF_PROMISC) {
1169 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1170 if (netif_msg_link(pegasus))
1171 pr_info("%s: Promiscuous mode enabled.\n", net->name);
YOSHIFUJI Hideakiae0a97b2005-05-25 16:07:04 +09001172 } else if (net->mc_count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 (net->flags & IFF_ALLMULTI)) {
1174 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1175 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1176 if (netif_msg_link(pegasus))
1177 pr_info("%s: set allmulti\n", net->name);
1178 } else {
1179 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1180 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1181 }
1182
1183 pegasus->flags |= ETH_REGS_CHANGE;
1184 ctrl_callback(pegasus->ctrl_urb, NULL);
1185}
1186
1187static __u8 mii_phy_probe(pegasus_t * pegasus)
1188{
1189 int i;
1190 __u16 tmp;
1191
1192 for (i = 0; i < 32; i++) {
1193 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1194 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1195 continue;
1196 else
1197 return i;
1198 }
1199
1200 return 0xff;
1201}
1202
1203static inline void setup_pegasus_II(pegasus_t * pegasus)
1204{
1205 __u8 data = 0xa5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Petko Manolov4a1728a2005-11-15 09:48:23 +02001207 set_register(pegasus, Reg1d, 0);
1208 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 mdelay(100);
1210 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001211 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001213 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Petko Manolov4a1728a2005-11-15 09:48:23 +02001215 set_register(pegasus, 0x83, data);
1216 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (data == 0xa5) {
1219 pegasus->chip = 0x8513;
1220 } else {
1221 pegasus->chip = 0;
1222 }
1223
Petko Manolov4a1728a2005-11-15 09:48:23 +02001224 set_register(pegasus, 0x80, 0xc0);
1225 set_register(pegasus, 0x83, 0xff);
1226 set_register(pegasus, 0x84, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001229 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001231 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234
1235static struct workqueue_struct *pegasus_workqueue = NULL;
1236#define CARRIER_CHECK_DELAY (2 * HZ)
1237
1238static void check_carrier(void *data)
1239{
1240 pegasus_t *pegasus = data;
1241 set_carrier(pegasus->net);
1242 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1243 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1244 CARRIER_CHECK_DELAY);
1245 }
1246}
1247
1248static int pegasus_probe(struct usb_interface *intf,
1249 const struct usb_device_id *id)
1250{
1251 struct usb_device *dev = interface_to_usbdev(intf);
1252 struct net_device *net;
1253 pegasus_t *pegasus;
1254 int dev_index = id - pegasus_ids;
1255 int res = -ENOMEM;
1256
1257 usb_get_dev(dev);
1258 net = alloc_etherdev(sizeof(struct pegasus));
1259 if (!net) {
1260 dev_err(&intf->dev, "can't allocate %s\n", "device");
1261 goto out;
1262 }
1263
1264 pegasus = netdev_priv(net);
1265 memset(pegasus, 0, sizeof (struct pegasus));
1266 pegasus->dev_index = dev_index;
1267 init_waitqueue_head(&pegasus->ctrl_wait);
1268
1269 if (!alloc_urbs(pegasus)) {
1270 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1271 goto out1;
1272 }
1273
1274 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1275
1276 INIT_WORK(&pegasus->carrier_check, check_carrier, pegasus);
1277
1278 pegasus->intf = intf;
1279 pegasus->usb = dev;
1280 pegasus->net = net;
1281 SET_MODULE_OWNER(net);
1282 net->open = pegasus_open;
1283 net->stop = pegasus_close;
1284 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1285 net->tx_timeout = pegasus_tx_timeout;
1286 net->do_ioctl = pegasus_ioctl;
1287 net->hard_start_xmit = pegasus_start_xmit;
1288 net->set_multicast_list = pegasus_set_multicast;
1289 net->get_stats = pegasus_netdev_stats;
1290 SET_ETHTOOL_OPS(net, &ops);
1291 pegasus->mii.dev = net;
1292 pegasus->mii.mdio_read = mdio_read;
1293 pegasus->mii.mdio_write = mdio_write;
1294 pegasus->mii.phy_id_mask = 0x1f;
1295 pegasus->mii.reg_num_mask = 0x1f;
1296 spin_lock_init(&pegasus->rx_pool_lock);
1297 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1298 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1299
1300 pegasus->features = usb_dev_id[dev_index].private;
1301 get_interrupt_interval(pegasus);
1302 if (reset_mac(pegasus)) {
1303 dev_err(&intf->dev, "can't reset MAC\n");
1304 res = -EIO;
1305 goto out2;
1306 }
1307 set_ethernet_addr(pegasus);
1308 fill_skb_pool(pegasus);
1309 if (pegasus->features & PEGASUS_II) {
1310 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1311 setup_pegasus_II(pegasus);
1312 }
1313 pegasus->phy = mii_phy_probe(pegasus);
1314 if (pegasus->phy == 0xff) {
1315 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1316 pegasus->phy = 1;
1317 }
1318 pegasus->mii.phy_id = pegasus->phy;
1319 usb_set_intfdata(intf, pegasus);
1320 SET_NETDEV_DEV(net, &intf->dev);
1321 pegasus_reset_wol(net);
1322 res = register_netdev(net);
1323 if (res)
1324 goto out3;
1325 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1326 CARRIER_CHECK_DELAY);
1327
1328 dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1329 net->name,
1330 usb_dev_id[dev_index].name,
1331 net->dev_addr [0], net->dev_addr [1],
1332 net->dev_addr [2], net->dev_addr [3],
1333 net->dev_addr [4], net->dev_addr [5]);
1334 return 0;
1335
1336out3:
1337 usb_set_intfdata(intf, NULL);
1338 free_skb_pool(pegasus);
1339out2:
1340 free_all_urbs(pegasus);
1341out1:
1342 free_netdev(net);
1343out:
1344 usb_put_dev(dev);
1345 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);
1361 usb_put_dev(interface_to_usbdev(intf));
Kevin Vigora85a46f2005-09-22 00:49:24 -07001362 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 free_all_urbs(pegasus);
1364 free_skb_pool(pegasus);
1365 if (pegasus->rx_skb)
1366 dev_kfree_skb(pegasus->rx_skb);
1367 free_netdev(pegasus->net);
1368}
1369
David Brownell27d72e82005-04-18 17:39:22 -07001370static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct pegasus *pegasus = usb_get_intfdata(intf);
1373
1374 netif_device_detach (pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001375 if (netif_running(pegasus->net)) {
1376 cancel_delayed_work(&pegasus->carrier_check);
1377
1378 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
1384static int pegasus_resume (struct usb_interface *intf)
1385{
1386 struct pegasus *pegasus = usb_get_intfdata(intf);
1387
1388 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;
Al Viro9727d042005-04-26 07:43:41 -07001392 read_bulk_callback(pegasus->rx_urb, NULL);
David Brownell27d72e82005-04-18 17:39:22 -07001393
1394 pegasus->intr_urb->status = 0;
1395 pegasus->intr_urb->actual_length = 0;
Al Viro9727d042005-04-26 07:43:41 -07001396 intr_callback(pegasus->intr_urb, NULL);
David Brownell27d72e82005-04-18 17:39:22 -07001397
1398 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1399 CARRIER_CHECK_DELAY);
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 0;
1402}
1403
1404static struct usb_driver pegasus_driver = {
1405 .name = driver_name,
1406 .probe = pegasus_probe,
1407 .disconnect = pegasus_disconnect,
1408 .id_table = pegasus_ids,
1409 .suspend = pegasus_suspend,
1410 .resume = pegasus_resume,
1411};
1412
1413static int __init pegasus_init(void)
1414{
1415 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1416 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1417 if (!pegasus_workqueue)
1418 return -ENOMEM;
1419 return usb_register(&pegasus_driver);
1420}
1421
1422static void __exit pegasus_exit(void)
1423{
1424 destroy_workqueue(pegasus_workqueue);
1425 usb_deregister(&pegasus_driver);
1426}
1427
1428module_init(pegasus_init);
1429module_exit(pegasus_exit);