]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/usb/pegasus.c
vxlan: Fix TCPv6 segmentation.
[mirror_ubuntu-bionic-kernel.git] / drivers / net / usb / pegasus.c
CommitLineData
1da177e4 1/*
323b3496 2 * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
1da177e4
LT
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()
5a9dbfe0 24 * time as this seems to break dhcpd.
1da177e4
LT
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
5a9dbfe0 28 * is out of the interrupt routine.
323b3496
PM
29 * ...
30 * v0.9.3 simplified [get|set]_register(s), async update registers
31 * logic revisited, receive skb_pool removed.
1da177e4
LT
32 */
33
1da177e4
LT
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/delay.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/ethtool.h>
41#include <linux/mii.h>
42#include <linux/usb.h>
43#include <linux/module.h>
44#include <asm/byteorder.h>
45#include <asm/uaccess.h>
46#include "pegasus.h"
47
48/*
49 * Version Information
50 */
323b3496
PM
51#define DRIVER_VERSION "v0.9.3 (2013/04/25)"
52#define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
1da177e4
LT
53#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
54
55static const char driver_name[] = "pegasus";
56
57#undef PEGASUS_WRITE_EEPROM
58#define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
59 BMSR_100FULL | BMSR_ANEGCAPABLE)
60
eb939922
RR
61static bool loopback;
62static bool mii_mode;
5a9dbfe0 63static char *devid;
1da177e4
LT
64
65static struct usb_eth_dev usb_dev_id[] = {
66#define PEGASUS_DEV(pn, vid, pid, flags) \
67 {.name = pn, .vendor = vid, .device = pid, .private = flags},
ab854b24
CR
68#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
69 PEGASUS_DEV(pn, vid, pid, flags)
1da177e4
LT
70#include "pegasus.h"
71#undef PEGASUS_DEV
ab854b24 72#undef PEGASUS_DEV_CLASS
a4f81a61 73 {NULL, 0, 0, 0},
1da177e4
LT
74 {NULL, 0, 0, 0}
75};
76
77static struct usb_device_id pegasus_ids[] = {
78#define PEGASUS_DEV(pn, vid, pid, flags) \
79 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
ab854b24
CR
80/*
81 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
82 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
83 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
84 * case anyway, seeing as the pegasus is for "Wired" adaptors.
85 */
86#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
87 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
88 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
1da177e4
LT
89#include "pegasus.h"
90#undef PEGASUS_DEV
ab854b24 91#undef PEGASUS_DEV_CLASS
a4f81a61 92 {},
1da177e4
LT
93 {}
94};
95
96MODULE_AUTHOR(DRIVER_AUTHOR);
97MODULE_DESCRIPTION(DRIVER_DESC);
98MODULE_LICENSE("GPL");
99module_param(loopback, bool, 0);
100module_param(mii_mode, bool, 0);
a4f81a61 101module_param(devid, charp, 0);
1da177e4
LT
102MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
103MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
a4f81a61 104MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
1da177e4
LT
105
106/* use ethtool to change the level for any given device */
107static int msg_level = -1;
5a9dbfe0
NK
108module_param(msg_level, int, 0);
109MODULE_PARM_DESC(msg_level, "Override default message level");
1da177e4
LT
110
111MODULE_DEVICE_TABLE(usb, pegasus_ids);
8cb89571 112static const struct net_device_ops pegasus_netdev_ops;
1da177e4 113
323b3496
PM
114/*****/
115
116static void async_ctrl_callback(struct urb *urb)
1da177e4 117{
323b3496 118 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
c94cb314 119 int status = urb->status;
1da177e4 120
323b3496
PM
121 if (status < 0)
122 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
123 kfree(req);
124 usb_free_urb(urb);
1da177e4
LT
125}
126
323b3496 127static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
1da177e4
LT
128{
129 int ret;
1da177e4 130
323b3496
PM
131 ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
132 PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
133 indx, data, size, 1000);
134 if (ret < 0)
135 netif_dbg(pegasus, drv, pegasus->net,
136 "%s returned %d\n", __func__, ret);
1da177e4
LT
137 return ret;
138}
139
323b3496 140static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
1da177e4
LT
141{
142 int ret;
1da177e4 143
323b3496
PM
144 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
145 PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
146 indx, data, size, 100);
147 if (ret < 0)
148 netif_dbg(pegasus, drv, pegasus->net,
149 "%s returned %d\n", __func__, ret);
1da177e4
LT
150 return ret;
151}
152
5a9dbfe0 153static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
1da177e4
LT
154{
155 int ret;
1da177e4 156
323b3496
PM
157 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
158 PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
159 indx, &data, 1, 1000);
160 if (ret < 0)
161 netif_dbg(pegasus, drv, pegasus->net,
162 "%s returned %d\n", __func__, ret);
1da177e4
LT
163 return ret;
164}
165
5a9dbfe0 166static int update_eth_regs_async(pegasus_t *pegasus)
1da177e4 167{
323b3496
PM
168 int ret = -ENOMEM;
169 struct urb *async_urb;
170 struct usb_ctrlrequest *req;
1da177e4 171
323b3496
PM
172 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
173 if (req == NULL)
174 return ret;
1da177e4 175
323b3496
PM
176 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
177 if (async_urb == NULL) {
178 kfree(req);
179 return ret;
180 }
181 req->bRequestType = PEGASUS_REQT_WRITE;
182 req->bRequest = PEGASUS_REQ_SET_REGS;
183 req->wValue = cpu_to_le16(0);
184 req->wIndex = cpu_to_le16(EthCtrl0);
185 req->wLength = cpu_to_le16(3);
186
187 usb_fill_control_urb(async_urb, pegasus->usb,
188 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
189 pegasus->eth_regs, 3, async_ctrl_callback, req);
190
191 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
192 if (ret) {
955a2608
DB
193 if (ret == -ENODEV)
194 netif_device_detach(pegasus->net);
a475f603 195 netif_err(pegasus, drv, pegasus->net,
323b3496 196 "%s returned %d\n", __func__, ret);
955a2608 197 }
1da177e4
LT
198 return ret;
199}
200
2bd64701 201static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
1da177e4
LT
202{
203 int i;
204 __u8 data[4] = { phy, 0, 0, indx };
205 __le16 regdi;
2bd64701 206 int ret = -ETIMEDOUT;
1da177e4 207
2bd64701
PM
208 if (cmd & PHY_WRITE) {
209 __le16 *t = (__le16 *) & data[1];
210 *t = cpu_to_le16(*regd);
211 }
212 set_register(p, PhyCtrl, 0);
213 set_registers(p, PhyAddr, sizeof(data), data);
214 set_register(p, PhyCtrl, (indx | cmd));
1da177e4 215 for (i = 0; i < REG_TIMEOUT; i++) {
2bd64701
PM
216 ret = get_registers(p, PhyCtrl, 1, data);
217 if (ret < 0)
7e713b82 218 goto fail;
1da177e4
LT
219 if (data[0] & PHY_DONE)
220 break;
221 }
a475f603
JP
222 if (i >= REG_TIMEOUT)
223 goto fail;
2bd64701
PM
224 if (cmd & PHY_READ) {
225 ret = get_registers(p, PhyData, 2, &regdi);
226 *regd = le16_to_cpu(regdi);
227 return ret;
228 }
229 return 0;
230fail:
231 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
a475f603 232 return ret;
2bd64701 233}
a475f603 234
2bd64701
PM
235/* Returns non-negative int on success, error on failure */
236static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
237{
238 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
239}
1da177e4 240
2bd64701
PM
241/* Returns zero on success, error on failure */
242static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
243{
244 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
1da177e4
LT
245}
246
247static int mdio_read(struct net_device *dev, int phy_id, int loc)
248{
8739cfef 249 pegasus_t *pegasus = netdev_priv(dev);
1da177e4
LT
250 u16 res;
251
252 read_mii_word(pegasus, phy_id, loc, &res);
253 return (int)res;
254}
255
1da177e4
LT
256static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
257{
8739cfef 258 pegasus_t *pegasus = netdev_priv(dev);
1da177e4 259
2bd64701 260 write_mii_word(pegasus, phy_id, loc, (__u16 *)&val);
1da177e4
LT
261}
262
5a9dbfe0 263static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
1da177e4
LT
264{
265 int i;
266 __u8 tmp;
267 __le16 retdatai;
268 int ret;
269
4a1728a2
PM
270 set_register(pegasus, EpromCtrl, 0);
271 set_register(pegasus, EpromOffset, index);
272 set_register(pegasus, EpromCtrl, EPROM_READ);
1da177e4
LT
273
274 for (i = 0; i < REG_TIMEOUT; i++) {
275 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
276 if (tmp & EPROM_DONE)
277 break;
7e713b82
DB
278 if (ret == -ESHUTDOWN)
279 goto fail;
1da177e4 280 }
a475f603
JP
281 if (i >= REG_TIMEOUT)
282 goto fail;
283
284 ret = get_registers(pegasus, EpromData, 2, &retdatai);
285 *retdata = le16_to_cpu(retdatai);
286 return ret;
1da177e4 287
7e713b82 288fail:
a475f603 289 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
4a1728a2 290 return -ETIMEDOUT;
1da177e4
LT
291}
292
293#ifdef PEGASUS_WRITE_EEPROM
5a9dbfe0 294static inline void enable_eprom_write(pegasus_t *pegasus)
1da177e4
LT
295{
296 __u8 tmp;
1da177e4 297
4a1728a2
PM
298 get_registers(pegasus, EthCtrl2, 1, &tmp);
299 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
1da177e4
LT
300}
301
5a9dbfe0 302static inline void disable_eprom_write(pegasus_t *pegasus)
1da177e4
LT
303{
304 __u8 tmp;
1da177e4 305
4a1728a2
PM
306 get_registers(pegasus, EthCtrl2, 1, &tmp);
307 set_register(pegasus, EpromCtrl, 0);
308 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
1da177e4
LT
309}
310
5a9dbfe0 311static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
1da177e4
LT
312{
313 int i;
314 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
315 int ret;
e3453f63 316 __le16 le_data = cpu_to_le16(data);
1da177e4 317
4a1728a2 318 set_registers(pegasus, EpromOffset, 4, d);
1da177e4 319 enable_eprom_write(pegasus);
4a1728a2 320 set_register(pegasus, EpromOffset, index);
e3453f63 321 set_registers(pegasus, EpromData, 2, &le_data);
4a1728a2 322 set_register(pegasus, EpromCtrl, EPROM_WRITE);
1da177e4
LT
323
324 for (i = 0; i < REG_TIMEOUT; i++) {
325 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
7e713b82
DB
326 if (ret == -ESHUTDOWN)
327 goto fail;
1da177e4
LT
328 if (tmp & EPROM_DONE)
329 break;
330 }
331 disable_eprom_write(pegasus);
a475f603
JP
332 if (i >= REG_TIMEOUT)
333 goto fail;
334
335 return ret;
336
7e713b82 337fail:
a475f603 338 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
4a1728a2 339 return -ETIMEDOUT;
1da177e4
LT
340}
341#endif /* PEGASUS_WRITE_EEPROM */
342
5a9dbfe0 343static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
1da177e4
LT
344{
345 int i;
346 __u16 w16;
347
348 for (i = 0; i < 3; i++) {
349 read_eprom_word(pegasus, i, &w16);
da2bbdcc 350 ((__le16 *) id)[i] = cpu_to_le16(w16);
1da177e4
LT
351 }
352}
353
5a9dbfe0 354static void set_ethernet_addr(pegasus_t *pegasus)
1da177e4
LT
355{
356 __u8 node_id[6];
1da177e4 357
37cf3477
PM
358 if (pegasus->features & PEGASUS_II) {
359 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
360 } else {
361 get_node_id(pegasus, node_id);
5a9dbfe0 362 set_registers(pegasus, EthID, sizeof(node_id), node_id);
37cf3477 363 }
5a9dbfe0 364 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
1da177e4
LT
365}
366
5a9dbfe0 367static inline int reset_mac(pegasus_t *pegasus)
1da177e4
LT
368{
369 __u8 data = 0x8;
370 int i;
1da177e4 371
4a1728a2 372 set_register(pegasus, EthCtrl1, data);
1da177e4 373 for (i = 0; i < REG_TIMEOUT; i++) {
4a1728a2 374 get_registers(pegasus, EthCtrl1, 1, &data);
1da177e4 375 if (~data & 0x08) {
681f1623 376 if (loopback)
1da177e4
LT
377 break;
378 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
4a1728a2 379 set_register(pegasus, Gpio1, 0x34);
1da177e4 380 else
4a1728a2
PM
381 set_register(pegasus, Gpio1, 0x26);
382 set_register(pegasus, Gpio0, pegasus->features);
383 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
1da177e4
LT
384 break;
385 }
386 }
387 if (i == REG_TIMEOUT)
4a1728a2 388 return -ETIMEDOUT;
1da177e4
LT
389
390 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
391 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
4a1728a2
PM
392 set_register(pegasus, Gpio0, 0x24);
393 set_register(pegasus, Gpio0, 0x26);
1da177e4
LT
394 }
395 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
396 __u16 auxmode;
397 read_mii_word(pegasus, 3, 0x1b, &auxmode);
2bd64701
PM
398 auxmode |= 4;
399 write_mii_word(pegasus, 3, 0x1b, &auxmode);
1da177e4
LT
400 }
401
402 return 0;
403}
404
405static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
406{
407 __u16 linkpart;
408 __u8 data[4];
409 pegasus_t *pegasus = netdev_priv(dev);
410 int ret;
411
412 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
413 data[0] = 0xc9;
414 data[1] = 0;
415 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
416 data[1] |= 0x20; /* set full duplex */
417 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
418 data[1] |= 0x10; /* set 100 Mbps */
419 if (mii_mode)
420 data[1] = 0;
681f1623 421 data[2] = loopback ? 0x09 : 0x01;
1da177e4 422
5a9dbfe0 423 memcpy(pegasus->eth_regs, data, sizeof(data));
1da177e4
LT
424 ret = set_registers(pegasus, EthCtrl0, 3, data);
425
426 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
efafe6fb 427 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
1da177e4
LT
428 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
429 u16 auxmode;
430 read_mii_word(pegasus, 0, 0x1b, &auxmode);
2bd64701
PM
431 auxmode |= 4;
432 write_mii_word(pegasus, 0, 0x1b, &auxmode);
1da177e4
LT
433 }
434
4a1728a2 435 return ret;
1da177e4
LT
436}
437
7d12e780 438static void read_bulk_callback(struct urb *urb)
1da177e4
LT
439{
440 pegasus_t *pegasus = urb->context;
441 struct net_device *net;
442 int rx_status, count = urb->actual_length;
c94cb314 443 int status = urb->status;
1da177e4
LT
444 u8 *buf = urb->transfer_buffer;
445 __u16 pkt_len;
446
447 if (!pegasus)
448 return;
449
450 net = pegasus->net;
451 if (!netif_device_present(net) || !netif_running(net))
452 return;
453
c94cb314 454 switch (status) {
1da177e4
LT
455 case 0:
456 break;
38e2bfc9 457 case -ETIME:
a475f603 458 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
1da177e4
LT
459 pegasus->flags &= ~PEGASUS_RX_BUSY;
460 break;
461 case -EPIPE: /* stall, or disconnect from TT */
462 /* FIXME schedule work to clear the halt */
a475f603 463 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
1da177e4
LT
464 return;
465 case -ENOENT:
466 case -ECONNRESET:
467 case -ESHUTDOWN:
a475f603 468 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
1da177e4
LT
469 return;
470 default:
a475f603 471 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
1da177e4
LT
472 goto goon;
473 }
474
475 if (!count || count < 4)
476 goto goon;
477
478 rx_status = buf[count - 2];
479 if (rx_status & 0x1e) {
a475f603
JP
480 netif_dbg(pegasus, rx_err, net,
481 "RX packet error %x\n", rx_status);
1da177e4 482 pegasus->stats.rx_errors++;
5a9dbfe0 483 if (rx_status & 0x06) /* long or runt */
1da177e4
LT
484 pegasus->stats.rx_length_errors++;
485 if (rx_status & 0x08)
486 pegasus->stats.rx_crc_errors++;
5a9dbfe0 487 if (rx_status & 0x10) /* extra bits */
1da177e4
LT
488 pegasus->stats.rx_frame_errors++;
489 goto goon;
490 }
491 if (pegasus->chip == 0x8513) {
492 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
493 pkt_len &= 0x0fff;
494 pegasus->rx_skb->data += 2;
495 } else {
496 pkt_len = buf[count - 3] << 8;
497 pkt_len += buf[count - 4];
498 pkt_len &= 0xfff;
499 pkt_len -= 8;
500 }
501
a85a46f2
KV
502 /*
503 * If the packet is unreasonably long, quietly drop it rather than
504 * kernel panicing by calling skb_put.
505 */
506 if (pkt_len > PEGASUS_MTU)
507 goto goon;
508
1da177e4
LT
509 /*
510 * at this point we are sure pegasus->rx_skb != NULL
511 * so we go ahead and pass up the packet.
512 */
513 skb_put(pegasus->rx_skb, pkt_len);
514 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
515 netif_rx(pegasus->rx_skb);
516 pegasus->stats.rx_packets++;
517 pegasus->stats.rx_bytes += pkt_len;
518
519 if (pegasus->flags & PEGASUS_UNPLUG)
520 return;
521
313a58e4
PM
522 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
523 GFP_ATOMIC);
1da177e4
LT
524
525 if (pegasus->rx_skb == NULL)
526 goto tl_sched;
527goon:
528 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
529 usb_rcvbulkpipe(pegasus->usb, 1),
530 pegasus->rx_skb->data, PEGASUS_MTU + 8,
531 read_bulk_callback, pegasus);
955a2608
DB
532 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
533 if (rx_status == -ENODEV)
534 netif_device_detach(pegasus->net);
535 else if (rx_status) {
1da177e4
LT
536 pegasus->flags |= PEGASUS_RX_URB_FAIL;
537 goto tl_sched;
538 } else {
539 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
540 }
541
542 return;
543
544tl_sched:
545 tasklet_schedule(&pegasus->rx_tl);
546}
547
548static void rx_fixup(unsigned long data)
549{
550 pegasus_t *pegasus;
955a2608 551 int status;
1da177e4
LT
552
553 pegasus = (pegasus_t *) data;
554 if (pegasus->flags & PEGASUS_UNPLUG)
555 return;
556
1da177e4
LT
557 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
558 if (pegasus->rx_skb)
559 goto try_again;
5a9dbfe0 560 if (pegasus->rx_skb == NULL)
313a58e4
PM
561 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
562 PEGASUS_MTU,
563 GFP_ATOMIC);
1da177e4 564 if (pegasus->rx_skb == NULL) {
a475f603 565 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
1da177e4 566 tasklet_schedule(&pegasus->rx_tl);
313a58e4 567 return;
1da177e4
LT
568 }
569 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
570 usb_rcvbulkpipe(pegasus->usb, 1),
571 pegasus->rx_skb->data, PEGASUS_MTU + 8,
572 read_bulk_callback, pegasus);
573try_again:
955a2608
DB
574 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
575 if (status == -ENODEV)
576 netif_device_detach(pegasus->net);
577 else if (status) {
1da177e4
LT
578 pegasus->flags |= PEGASUS_RX_URB_FAIL;
579 tasklet_schedule(&pegasus->rx_tl);
580 } else {
581 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
582 }
1da177e4
LT
583}
584
7d12e780 585static void write_bulk_callback(struct urb *urb)
1da177e4
LT
586{
587 pegasus_t *pegasus = urb->context;
9351982b 588 struct net_device *net;
c94cb314 589 int status = urb->status;
1da177e4
LT
590
591 if (!pegasus)
592 return;
593
9351982b
MG
594 net = pegasus->net;
595
1da177e4
LT
596 if (!netif_device_present(net) || !netif_running(net))
597 return;
598
c94cb314 599 switch (status) {
1da177e4
LT
600 case -EPIPE:
601 /* FIXME schedule_work() to clear the tx halt */
602 netif_stop_queue(net);
a475f603 603 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
1da177e4
LT
604 return;
605 case -ENOENT:
606 case -ECONNRESET:
607 case -ESHUTDOWN:
a475f603 608 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
1da177e4
LT
609 return;
610 default:
a475f603 611 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
1da177e4
LT
612 /* FALL THROUGH */
613 case 0:
614 break;
615 }
616
1ae5dc34 617 net->trans_start = jiffies; /* prevent tx timeout */
1da177e4
LT
618 netif_wake_queue(net);
619}
620
7d12e780 621static void intr_callback(struct urb *urb)
1da177e4
LT
622{
623 pegasus_t *pegasus = urb->context;
624 struct net_device *net;
c94cb314 625 int res, status = urb->status;
1da177e4
LT
626
627 if (!pegasus)
628 return;
629 net = pegasus->net;
630
c94cb314 631 switch (status) {
1da177e4
LT
632 case 0:
633 break;
634 case -ECONNRESET: /* unlink */
635 case -ENOENT:
636 case -ESHUTDOWN:
637 return;
638 default:
639 /* some Pegasus-I products report LOTS of data
640 * toggle errors... avoid log spamming
641 */
a475f603 642 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
1da177e4
LT
643 }
644
645 if (urb->actual_length >= 6) {
5a9dbfe0 646 u8 *d = urb->transfer_buffer;
1da177e4
LT
647
648 /* byte 0 == tx_status1, reg 2B */
649 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
650 |LATE_COL|JABBER_TIMEOUT)) {
651 pegasus->stats.tx_errors++;
652 if (d[0] & TX_UNDERRUN)
653 pegasus->stats.tx_fifo_errors++;
654 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
655 pegasus->stats.tx_aborted_errors++;
656 if (d[0] & LATE_COL)
657 pegasus->stats.tx_window_errors++;
658 }
659
660 /* d[5].LINK_STATUS lies on some adapters.
661 * d[0].NO_CARRIER kicks in only with failed TX.
662 * ... so monitoring with MII may be safest.
663 */
1da177e4
LT
664
665 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
666 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
667 }
668
c94cb314
ON
669 res = usb_submit_urb(urb, GFP_ATOMIC);
670 if (res == -ENODEV)
955a2608 671 netif_device_detach(pegasus->net);
a475f603
JP
672 if (res)
673 netif_err(pegasus, timer, net,
674 "can't resubmit interrupt urb, %d\n", res);
1da177e4
LT
675}
676
677static void pegasus_tx_timeout(struct net_device *net)
678{
679 pegasus_t *pegasus = netdev_priv(net);
a475f603 680 netif_warn(pegasus, timer, net, "tx timeout\n");
1da177e4
LT
681 usb_unlink_urb(pegasus->tx_urb);
682 pegasus->stats.tx_errors++;
683}
684
25a79c41
SH
685static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
686 struct net_device *net)
1da177e4
LT
687{
688 pegasus_t *pegasus = netdev_priv(net);
689 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
690 int res;
691 __u16 l16 = skb->len;
692
693 netif_stop_queue(net);
694
695 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
d626f62b 696 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
1da177e4
LT
697 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
698 usb_sndbulkpipe(pegasus->usb, 2),
699 pegasus->tx_buff, count,
700 write_bulk_callback, pegasus);
701 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
a475f603 702 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
1da177e4
LT
703 switch (res) {
704 case -EPIPE: /* stall, or disconnect from TT */
705 /* cleanup should already have been scheduled */
706 break;
707 case -ENODEV: /* disconnect() upcoming */
9dd014eb 708 case -EPERM:
955a2608 709 netif_device_detach(pegasus->net);
1da177e4
LT
710 break;
711 default:
712 pegasus->stats.tx_errors++;
713 netif_start_queue(net);
714 }
715 } else {
716 pegasus->stats.tx_packets++;
717 pegasus->stats.tx_bytes += skb->len;
1da177e4
LT
718 }
719 dev_kfree_skb(skb);
720
6ed10654 721 return NETDEV_TX_OK;
1da177e4
LT
722}
723
724static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
725{
726 return &((pegasus_t *) netdev_priv(dev))->stats;
727}
728
5a9dbfe0 729static inline void disable_net_traffic(pegasus_t *pegasus)
1da177e4 730{
e3453f63 731 __le16 tmp = cpu_to_le16(0);
1da177e4 732
e3453f63 733 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
1da177e4
LT
734}
735
5a9dbfe0 736static inline void get_interrupt_interval(pegasus_t *pegasus)
1da177e4 737{
e3453f63
MB
738 u16 data;
739 u8 interval;
1da177e4 740
e3453f63
MB
741 read_eprom_word(pegasus, 4, &data);
742 interval = data >> 8;
a85a46f2 743 if (pegasus->usb->speed != USB_SPEED_HIGH) {
e3453f63 744 if (interval < 0x80) {
a475f603
JP
745 netif_info(pegasus, timer, pegasus->net,
746 "intr interval changed from %ums to %ums\n",
747 interval, 0x80);
e3453f63
MB
748 interval = 0x80;
749 data = (data & 0x00FF) | ((u16)interval << 8);
a85a46f2 750#ifdef PEGASUS_WRITE_EEPROM
e3453f63 751 write_eprom_word(pegasus, 4, data);
1da177e4 752#endif
a85a46f2 753 }
1da177e4 754 }
e3453f63 755 pegasus->intr_interval = interval;
1da177e4
LT
756}
757
758static void set_carrier(struct net_device *net)
759{
760 pegasus_t *pegasus = netdev_priv(net);
761 u16 tmp;
762
c43c49bd 763 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
1da177e4 764 return;
a85a46f2 765
1da177e4
LT
766 if (tmp & BMSR_LSTATUS)
767 netif_carrier_on(net);
768 else
769 netif_carrier_off(net);
770}
771
5a9dbfe0 772static void free_all_urbs(pegasus_t *pegasus)
1da177e4
LT
773{
774 usb_free_urb(pegasus->intr_urb);
775 usb_free_urb(pegasus->tx_urb);
776 usb_free_urb(pegasus->rx_urb);
1da177e4
LT
777}
778
5a9dbfe0 779static void unlink_all_urbs(pegasus_t *pegasus)
1da177e4
LT
780{
781 usb_kill_urb(pegasus->intr_urb);
782 usb_kill_urb(pegasus->tx_urb);
783 usb_kill_urb(pegasus->rx_urb);
1da177e4
LT
784}
785
5a9dbfe0 786static int alloc_urbs(pegasus_t *pegasus)
1da177e4 787{
323b3496
PM
788 int res = -ENOMEM;
789
1da177e4
LT
790 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
791 if (!pegasus->rx_urb) {
323b3496 792 return res;
1da177e4
LT
793 }
794 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
795 if (!pegasus->tx_urb) {
796 usb_free_urb(pegasus->rx_urb);
323b3496 797 return res;
1da177e4
LT
798 }
799 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
800 if (!pegasus->intr_urb) {
801 usb_free_urb(pegasus->tx_urb);
802 usb_free_urb(pegasus->rx_urb);
323b3496 803 return res;
1da177e4
LT
804 }
805
323b3496 806 return 0;
1da177e4
LT
807}
808
809static int pegasus_open(struct net_device *net)
810{
811 pegasus_t *pegasus = netdev_priv(net);
323b3496 812 int res=-ENOMEM;
1da177e4
LT
813
814 if (pegasus->rx_skb == NULL)
313a58e4
PM
815 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
816 PEGASUS_MTU,
817 GFP_KERNEL);
1da177e4 818 if (!pegasus->rx_skb)
323b3496 819 goto exit;
1da177e4
LT
820
821 res = set_registers(pegasus, EthID, 6, net->dev_addr);
5a9dbfe0 822
1da177e4
LT
823 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
824 usb_rcvbulkpipe(pegasus->usb, 1),
825 pegasus->rx_skb->data, PEGASUS_MTU + 8,
826 read_bulk_callback, pegasus);
827 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
955a2608
DB
828 if (res == -ENODEV)
829 netif_device_detach(pegasus->net);
a475f603 830 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
1da177e4
LT
831 goto exit;
832 }
833
834 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
835 usb_rcvintpipe(pegasus->usb, 3),
5a9dbfe0 836 pegasus->intr_buff, sizeof(pegasus->intr_buff),
1da177e4
LT
837 intr_callback, pegasus, pegasus->intr_interval);
838 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
955a2608
DB
839 if (res == -ENODEV)
840 netif_device_detach(pegasus->net);
a475f603 841 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
1da177e4
LT
842 usb_kill_urb(pegasus->rx_urb);
843 goto exit;
844 }
323b3496
PM
845 res = enable_net_traffic(net, pegasus->usb);
846 if (res < 0) {
a475f603
JP
847 netif_dbg(pegasus, ifup, net,
848 "can't enable_net_traffic() - %d\n", res);
1da177e4
LT
849 res = -EIO;
850 usb_kill_urb(pegasus->rx_urb);
851 usb_kill_urb(pegasus->intr_urb);
1da177e4
LT
852 goto exit;
853 }
854 set_carrier(net);
855 netif_start_queue(net);
a475f603 856 netif_dbg(pegasus, ifup, net, "open\n");
1da177e4
LT
857 res = 0;
858exit:
859 return res;
860}
861
862static int pegasus_close(struct net_device *net)
863{
864 pegasus_t *pegasus = netdev_priv(net);
865
866 netif_stop_queue(net);
867 if (!(pegasus->flags & PEGASUS_UNPLUG))
868 disable_net_traffic(pegasus);
869 tasklet_kill(&pegasus->rx_tl);
870 unlink_all_urbs(pegasus);
871
872 return 0;
873}
874
875static void pegasus_get_drvinfo(struct net_device *dev,
876 struct ethtool_drvinfo *info)
877{
878 pegasus_t *pegasus = netdev_priv(dev);
7826d43f
JP
879
880 strlcpy(info->driver, driver_name, sizeof(info->driver));
881 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
5a9dbfe0 882 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
1da177e4
LT
883}
884
885/* also handles three patterns of some kind in hardware */
886#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
887
888static void
889pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
890{
891 pegasus_t *pegasus = netdev_priv(dev);
892
893 wol->supported = WAKE_MAGIC | WAKE_PHY;
894 wol->wolopts = pegasus->wolopts;
895}
896
897static int
898pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
899{
900 pegasus_t *pegasus = netdev_priv(dev);
901 u8 reg78 = 0x04;
4fbc5b20 902 int ret;
5a9dbfe0 903
1da177e4
LT
904 if (wol->wolopts & ~WOL_SUPPORTED)
905 return -EINVAL;
906
907 if (wol->wolopts & WAKE_MAGIC)
908 reg78 |= 0x80;
909 if (wol->wolopts & WAKE_PHY)
910 reg78 |= 0x40;
911 /* FIXME this 0x10 bit still needs to get set in the chip... */
912 if (wol->wolopts)
913 pegasus->eth_regs[0] |= 0x10;
914 else
915 pegasus->eth_regs[0] &= ~0x10;
916 pegasus->wolopts = wol->wolopts;
4fbc5b20
ML
917
918 ret = set_register(pegasus, WakeupControl, reg78);
919 if (!ret)
920 ret = device_set_wakeup_enable(&pegasus->usb->dev,
921 wol->wolopts);
922 return ret;
1da177e4
LT
923}
924
925static inline void pegasus_reset_wol(struct net_device *dev)
926{
927 struct ethtool_wolinfo wol;
5a9dbfe0 928
1da177e4
LT
929 memset(&wol, 0, sizeof wol);
930 (void) pegasus_set_wol(dev, &wol);
931}
932
933static int
934pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
935{
936 pegasus_t *pegasus;
937
1da177e4
LT
938 pegasus = netdev_priv(dev);
939 mii_ethtool_gset(&pegasus->mii, ecmd);
1da177e4
LT
940 return 0;
941}
942
943static int
944pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
945{
946 pegasus_t *pegasus = netdev_priv(dev);
947 return mii_ethtool_sset(&pegasus->mii, ecmd);
948}
949
950static int pegasus_nway_reset(struct net_device *dev)
951{
952 pegasus_t *pegasus = netdev_priv(dev);
953 return mii_nway_restart(&pegasus->mii);
954}
955
956static u32 pegasus_get_link(struct net_device *dev)
957{
958 pegasus_t *pegasus = netdev_priv(dev);
959 return mii_link_ok(&pegasus->mii);
960}
961
962static u32 pegasus_get_msglevel(struct net_device *dev)
963{
964 pegasus_t *pegasus = netdev_priv(dev);
965 return pegasus->msg_enable;
966}
967
968static void pegasus_set_msglevel(struct net_device *dev, u32 v)
969{
970 pegasus_t *pegasus = netdev_priv(dev);
971 pegasus->msg_enable = v;
972}
973
0fc0b732 974static const struct ethtool_ops ops = {
1da177e4
LT
975 .get_drvinfo = pegasus_get_drvinfo,
976 .get_settings = pegasus_get_settings,
977 .set_settings = pegasus_set_settings,
978 .nway_reset = pegasus_nway_reset,
979 .get_link = pegasus_get_link,
980 .get_msglevel = pegasus_get_msglevel,
981 .set_msglevel = pegasus_set_msglevel,
982 .get_wol = pegasus_get_wol,
983 .set_wol = pegasus_set_wol,
984};
985
986static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
987{
5a9dbfe0 988 __u16 *data = (__u16 *) &rq->ifr_ifru;
1da177e4
LT
989 pegasus_t *pegasus = netdev_priv(net);
990 int res;
991
992 switch (cmd) {
993 case SIOCDEVPRIVATE:
994 data[0] = pegasus->phy;
995 case SIOCDEVPRIVATE + 1:
996 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
997 res = 0;
998 break;
999 case SIOCDEVPRIVATE + 2:
1000 if (!capable(CAP_NET_ADMIN))
1001 return -EPERM;
2bd64701 1002 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
1da177e4
LT
1003 res = 0;
1004 break;
1005 default:
1006 res = -EOPNOTSUPP;
1007 }
1008 return res;
1009}
1010
1011static void pegasus_set_multicast(struct net_device *net)
1012{
1013 pegasus_t *pegasus = netdev_priv(net);
1014
1015 if (net->flags & IFF_PROMISC) {
1016 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
a475f603 1017 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
4cd24eaf 1018 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1da177e4
LT
1019 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1020 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
a475f603 1021 netif_dbg(pegasus, link, net, "set allmulti\n");
1da177e4
LT
1022 } else {
1023 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1024 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1025 }
323b3496 1026 update_eth_regs_async(pegasus);
1da177e4
LT
1027}
1028
5a9dbfe0 1029static __u8 mii_phy_probe(pegasus_t *pegasus)
1da177e4
LT
1030{
1031 int i;
1032 __u16 tmp;
1033
1034 for (i = 0; i < 32; i++) {
1035 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1036 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1037 continue;
1038 else
1039 return i;
1040 }
1041
1042 return 0xff;
1043}
1044
5a9dbfe0 1045static inline void setup_pegasus_II(pegasus_t *pegasus)
1da177e4
LT
1046{
1047 __u8 data = 0xa5;
5a9dbfe0 1048
4a1728a2
PM
1049 set_register(pegasus, Reg1d, 0);
1050 set_register(pegasus, Reg7b, 1);
1da177e4
LT
1051 mdelay(100);
1052 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
4a1728a2 1053 set_register(pegasus, Reg7b, 0);
1da177e4 1054 else
4a1728a2 1055 set_register(pegasus, Reg7b, 2);
1da177e4 1056
4a1728a2
PM
1057 set_register(pegasus, 0x83, data);
1058 get_registers(pegasus, 0x83, 1, &data);
1da177e4 1059
5a9dbfe0 1060 if (data == 0xa5)
1da177e4 1061 pegasus->chip = 0x8513;
5a9dbfe0 1062 else
1da177e4 1063 pegasus->chip = 0;
1da177e4 1064
4a1728a2
PM
1065 set_register(pegasus, 0x80, 0xc0);
1066 set_register(pegasus, 0x83, 0xff);
1067 set_register(pegasus, 0x84, 0x01);
5a9dbfe0 1068
1da177e4 1069 if (pegasus->features & HAS_HOME_PNA && mii_mode)
4a1728a2 1070 set_register(pegasus, Reg81, 6);
1da177e4 1071 else
4a1728a2 1072 set_register(pegasus, Reg81, 2);
1da177e4
LT
1073}
1074
1075
cda2836d 1076static int pegasus_count;
5a9dbfe0 1077static struct workqueue_struct *pegasus_workqueue;
1da177e4
LT
1078#define CARRIER_CHECK_DELAY (2 * HZ)
1079
c4028958 1080static void check_carrier(struct work_struct *work)
1da177e4 1081{
c4028958 1082 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1da177e4
LT
1083 set_carrier(pegasus->net);
1084 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1085 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1086 CARRIER_CHECK_DELAY);
1087 }
1088}
1089
4f63135e
BC
1090static int pegasus_blacklisted(struct usb_device *udev)
1091{
1092 struct usb_device_descriptor *udd = &udev->descriptor;
1093
1094 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1095 * dongle which happens to have the same ID.
1096 */
e3453f63
MB
1097 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1098 (udd->idProduct == cpu_to_le16(0x0121)) &&
4f63135e
BC
1099 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1100 (udd->bDeviceProtocol == 1))
1101 return 1;
1102
1103 return 0;
1104}
1105
cda2836d
DB
1106/* we rely on probe() and remove() being serialized so we
1107 * don't need extra locking on pegasus_count.
1108 */
1109static void pegasus_dec_workqueue(void)
1110{
1111 pegasus_count--;
1112 if (pegasus_count == 0) {
1113 destroy_workqueue(pegasus_workqueue);
1114 pegasus_workqueue = NULL;
1115 }
1116}
1117
1da177e4
LT
1118static int pegasus_probe(struct usb_interface *intf,
1119 const struct usb_device_id *id)
1120{
1121 struct usb_device *dev = interface_to_usbdev(intf);
1122 struct net_device *net;
1123 pegasus_t *pegasus;
1124 int dev_index = id - pegasus_ids;
1125 int res = -ENOMEM;
1126
cda2836d
DB
1127 if (pegasus_blacklisted(dev))
1128 return -ENODEV;
4f63135e 1129
cda2836d
DB
1130 if (pegasus_count == 0) {
1131 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1132 if (!pegasus_workqueue)
1133 return -ENOMEM;
4f63135e 1134 }
cda2836d
DB
1135 pegasus_count++;
1136
1da177e4 1137 net = alloc_etherdev(sizeof(struct pegasus));
41de8d4c 1138 if (!net)
1da177e4 1139 goto out;
1da177e4
LT
1140
1141 pegasus = netdev_priv(net);
1da177e4 1142 pegasus->dev_index = dev_index;
1da177e4 1143
323b3496
PM
1144 res = alloc_urbs(pegasus);
1145 if (res < 0) {
1da177e4
LT
1146 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1147 goto out1;
1148 }
1149
1150 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1151
c4028958 1152 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1da177e4
LT
1153
1154 pegasus->intf = intf;
1155 pegasus->usb = dev;
1156 pegasus->net = net;
8cb89571
ON
1157
1158
1da177e4 1159 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
8cb89571 1160 net->netdev_ops = &pegasus_netdev_ops;
1da177e4
LT
1161 SET_ETHTOOL_OPS(net, &ops);
1162 pegasus->mii.dev = net;
1163 pegasus->mii.mdio_read = mdio_read;
1164 pegasus->mii.mdio_write = mdio_write;
1165 pegasus->mii.phy_id_mask = 0x1f;
1166 pegasus->mii.reg_num_mask = 0x1f;
5a9dbfe0 1167 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
1da177e4
LT
1168 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1169
1170 pegasus->features = usb_dev_id[dev_index].private;
1171 get_interrupt_interval(pegasus);
1172 if (reset_mac(pegasus)) {
1173 dev_err(&intf->dev, "can't reset MAC\n");
1174 res = -EIO;
1175 goto out2;
1176 }
1177 set_ethernet_addr(pegasus);
1da177e4
LT
1178 if (pegasus->features & PEGASUS_II) {
1179 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1180 setup_pegasus_II(pegasus);
1181 }
1182 pegasus->phy = mii_phy_probe(pegasus);
1183 if (pegasus->phy == 0xff) {
1184 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1185 pegasus->phy = 1;
1186 }
1187 pegasus->mii.phy_id = pegasus->phy;
1188 usb_set_intfdata(intf, pegasus);
1189 SET_NETDEV_DEV(net, &intf->dev);
1190 pegasus_reset_wol(net);
1191 res = register_netdev(net);
1192 if (res)
1193 goto out3;
1194 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
323b3496
PM
1195 CARRIER_CHECK_DELAY);
1196 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1197 usb_dev_id[dev_index].name, net->dev_addr);
1da177e4
LT
1198 return 0;
1199
1200out3:
1201 usb_set_intfdata(intf, NULL);
1da177e4
LT
1202out2:
1203 free_all_urbs(pegasus);
1204out1:
1205 free_netdev(net);
1206out:
cda2836d 1207 pegasus_dec_workqueue();
1da177e4
LT
1208 return res;
1209}
1210
1211static void pegasus_disconnect(struct usb_interface *intf)
1212{
1213 struct pegasus *pegasus = usb_get_intfdata(intf);
1214
1215 usb_set_intfdata(intf, NULL);
1216 if (!pegasus) {
1217 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1218 return;
1219 }
1220
1221 pegasus->flags |= PEGASUS_UNPLUG;
1222 cancel_delayed_work(&pegasus->carrier_check);
1223 unregister_netdev(pegasus->net);
a85a46f2 1224 unlink_all_urbs(pegasus);
1da177e4 1225 free_all_urbs(pegasus);
4c13eb66 1226 if (pegasus->rx_skb != NULL) {
1da177e4 1227 dev_kfree_skb(pegasus->rx_skb);
4c13eb66
ACM
1228 pegasus->rx_skb = NULL;
1229 }
1da177e4 1230 free_netdev(pegasus->net);
cda2836d 1231 pegasus_dec_workqueue();
1da177e4
LT
1232}
1233
5a9dbfe0 1234static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1235{
1236 struct pegasus *pegasus = usb_get_intfdata(intf);
5a9dbfe0
NK
1237
1238 netif_device_detach(pegasus->net);
7e713b82 1239 cancel_delayed_work(&pegasus->carrier_check);
27d72e85 1240 if (netif_running(pegasus->net)) {
27d72e85
DB
1241 usb_kill_urb(pegasus->rx_urb);
1242 usb_kill_urb(pegasus->intr_urb);
1243 }
1da177e4
LT
1244 return 0;
1245}
1246
5a9dbfe0 1247static int pegasus_resume(struct usb_interface *intf)
1da177e4
LT
1248{
1249 struct pegasus *pegasus = usb_get_intfdata(intf);
1250
5a9dbfe0 1251 netif_device_attach(pegasus->net);
27d72e85
DB
1252 if (netif_running(pegasus->net)) {
1253 pegasus->rx_urb->status = 0;
1254 pegasus->rx_urb->actual_length = 0;
7d12e780 1255 read_bulk_callback(pegasus->rx_urb);
27d72e85
DB
1256
1257 pegasus->intr_urb->status = 0;
1258 pegasus->intr_urb->actual_length = 0;
7d12e780 1259 intr_callback(pegasus->intr_urb);
27d72e85 1260 }
7e713b82
DB
1261 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1262 CARRIER_CHECK_DELAY);
1da177e4
LT
1263 return 0;
1264}
1265
8cb89571
ON
1266static const struct net_device_ops pegasus_netdev_ops = {
1267 .ndo_open = pegasus_open,
1268 .ndo_stop = pegasus_close,
1269 .ndo_do_ioctl = pegasus_ioctl,
1270 .ndo_start_xmit = pegasus_start_xmit,
afc4b13d 1271 .ndo_set_rx_mode = pegasus_set_multicast,
8cb89571
ON
1272 .ndo_get_stats = pegasus_netdev_stats,
1273 .ndo_tx_timeout = pegasus_tx_timeout,
635ecaa7 1274 .ndo_change_mtu = eth_change_mtu,
240c102d
BH
1275 .ndo_set_mac_address = eth_mac_addr,
1276 .ndo_validate_addr = eth_validate_addr,
8cb89571
ON
1277};
1278
1da177e4
LT
1279static struct usb_driver pegasus_driver = {
1280 .name = driver_name,
1281 .probe = pegasus_probe,
1282 .disconnect = pegasus_disconnect,
1283 .id_table = pegasus_ids,
1284 .suspend = pegasus_suspend,
1285 .resume = pegasus_resume,
e1f12eb6 1286 .disable_hub_initiated_lpm = 1,
1da177e4
LT
1287};
1288
cda2836d 1289static void __init parse_id(char *id)
a4f81a61 1290{
5a9dbfe0
NK
1291 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1292 char *token, *name = NULL;
a4f81a61
Y
1293
1294 if ((token = strsep(&id, ":")) != NULL)
1295 name = token;
1296 /* name now points to a null terminated string*/
1297 if ((token = strsep(&id, ":")) != NULL)
1298 vendor_id = simple_strtoul(token, NULL, 16);
1299 if ((token = strsep(&id, ":")) != NULL)
1300 device_id = simple_strtoul(token, NULL, 16);
1301 flags = simple_strtoul(id, NULL, 16);
1302 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
5a9dbfe0 1303 driver_name, name, vendor_id, device_id, flags);
a4f81a61
Y
1304
1305 if (vendor_id > 0x10000 || vendor_id == 0)
1306 return;
1307 if (device_id > 0x10000 || device_id == 0)
1308 return;
1309
5a9dbfe0 1310 for (i = 0; usb_dev_id[i].name; i++);
a4f81a61
Y
1311 usb_dev_id[i].name = name;
1312 usb_dev_id[i].vendor = vendor_id;
1313 usb_dev_id[i].device = device_id;
1314 usb_dev_id[i].private = flags;
1315 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1316 pegasus_ids[i].idVendor = vendor_id;
1317 pegasus_ids[i].idProduct = device_id;
1318}
1319
1da177e4
LT
1320static int __init pegasus_init(void)
1321{
1322 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
a4f81a61
Y
1323 if (devid)
1324 parse_id(devid);
1da177e4
LT
1325 return usb_register(&pegasus_driver);
1326}
1327
1328static void __exit pegasus_exit(void)
1329{
1da177e4
LT
1330 usb_deregister(&pegasus_driver);
1331}
1332
1333module_init(pegasus_init);
1334module_exit(pegasus_exit);