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