blob: 2deb4c01539e8466ef97766e408ff0b6689e71b5 [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;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020060static char *devid=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static struct usb_eth_dev usb_dev_id[] = {
63#define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
65#include "pegasus.h"
66#undef PEGASUS_DEV
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020067 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 {NULL, 0, 0, 0}
69};
70
71static struct usb_device_id pegasus_ids[] = {
72#define PEGASUS_DEV(pn, vid, pid, flags) \
73 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
74#include "pegasus.h"
75#undef PEGASUS_DEV
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020076 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 {}
78};
79
80MODULE_AUTHOR(DRIVER_AUTHOR);
81MODULE_DESCRIPTION(DRIVER_DESC);
82MODULE_LICENSE("GPL");
83module_param(loopback, bool, 0);
84module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020085module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020088MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* use ethtool to change the level for any given device */
91static int msg_level = -1;
92module_param (msg_level, int, 0);
93MODULE_PARM_DESC (msg_level, "Override default message level");
94
95MODULE_DEVICE_TABLE(usb, pegasus_ids);
96
97static int update_eth_regs_async(pegasus_t *);
98/* Aargh!!! I _really_ hate such tweaks */
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))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200121 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 __FUNCTION__, urb->status);
123 }
124 pegasus->flags &= ~ETH_REGS_CHANGED;
125 wake_up(&pegasus->ctrl_wait);
126}
127
128static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
129 void *data)
130{
131 int ret;
132 char *buffer;
133 DECLARE_WAITQUEUE(wait, current);
134
135 buffer = kmalloc(size, GFP_KERNEL);
136 if (!buffer) {
137 if (netif_msg_drv(pegasus))
138 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
139 __FUNCTION__);
140 return -ENOMEM;
141 }
142 add_wait_queue(&pegasus->ctrl_wait, &wait);
143 set_current_state(TASK_UNINTERRUPTIBLE);
144 while (pegasus->flags & ETH_REGS_CHANGED)
145 schedule();
146 remove_wait_queue(&pegasus->ctrl_wait, &wait);
147 set_current_state(TASK_RUNNING);
148
149 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
150 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
151 pegasus->dr.wValue = cpu_to_le16(0);
152 pegasus->dr.wIndex = cpu_to_le16p(&indx);
153 pegasus->dr.wLength = cpu_to_le16p(&size);
154 pegasus->ctrl_urb->transfer_buffer_length = size;
155
156 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
157 usb_rcvctrlpipe(pegasus->usb, 0),
158 (char *) &pegasus->dr,
159 buffer, size, ctrl_callback, pegasus);
160
161 add_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_UNINTERRUPTIBLE);
163
164 /* using ATOMIC, we'd never wake up if we slept */
165 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
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,
Petko Manolov016534c2006-03-30 09:59:22 +0300265 tmp, 1, ctrl_callback, pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
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
Petko Manolov4a1728a2005-11-15 09:48:23 +0200316 set_register(pegasus, PhyCtrl, 0);
317 set_registers(pegasus, PhyAddr, sizeof (data), data);
318 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 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);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 if (netif_msg_drv(pegasus))
330 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
331
Petko Manolov4a1728a2005-11-15 09:48:23 +0200332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
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);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200352 set_register(pegasus, PhyCtrl, 0);
353 set_registers(pegasus, PhyAddr, sizeof(data), data);
354 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 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)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (netif_msg_drv(pegasus))
364 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200365 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
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
Petko Manolov4a1728a2005-11-15 09:48:23 +0200382 set_register(pegasus, EpromCtrl, 0);
383 set_register(pegasus, EpromOffset, index);
384 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
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);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 if (netif_msg_drv(pegasus))
398 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200399 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402#ifdef PEGASUS_WRITE_EEPROM
403static inline void enable_eprom_write(pegasus_t * pegasus)
404{
405 __u8 tmp;
406 int ret;
407
Petko Manolov4a1728a2005-11-15 09:48:23 +0200408 get_registers(pegasus, EthCtrl2, 1, &tmp);
409 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412static inline void disable_eprom_write(pegasus_t * pegasus)
413{
414 __u8 tmp;
415 int ret;
416
Petko Manolov4a1728a2005-11-15 09:48:23 +0200417 get_registers(pegasus, EthCtrl2, 1, &tmp);
418 set_register(pegasus, EpromCtrl, 0);
419 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
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
Petko Manolov4a1728a2005-11-15 09:48:23 +0200428 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200430 set_register(pegasus, EpromOffset, index);
431 set_registers(pegasus, EpromData, 2, &data);
432 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
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)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200441 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (netif_msg_drv(pegasus))
443 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200444 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 get_node_id(pegasus, node_id);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200464 set_registers(pegasus, EthID, sizeof (node_id), node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Petko Manolov4a1728a2005-11-15 09:48:23 +0200473 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200475 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (~data & 0x08) {
477 if (loopback & 1)
478 break;
479 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200480 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200482 set_register(pegasus, Gpio1, 0x26);
483 set_register(pegasus, Gpio0, pegasus->features);
484 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 }
487 }
488 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200489 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
492 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200493 set_register(pegasus, Gpio0, 0x24);
494 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
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 ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100527 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
529 u16 auxmode;
530 read_mii_word(pegasus, 0, 0x1b, &auxmode);
531 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
532 }
533
Petko Manolov4a1728a2005-11-15 09:48:23 +0200534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static void fill_skb_pool(pegasus_t * pegasus)
538{
539 int i;
540
541 for (i = 0; i < RX_SKBS; i++) {
542 if (pegasus->rx_pool[i])
543 continue;
544 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
545 /*
546 ** we give up if the allocation fail. the tasklet will be
547 ** rescheduled again anyway...
548 */
549 if (pegasus->rx_pool[i] == NULL)
550 return;
551 pegasus->rx_pool[i]->dev = pegasus->net;
552 skb_reserve(pegasus->rx_pool[i], 2);
553 }
554}
555
556static void free_skb_pool(pegasus_t * pegasus)
557{
558 int i;
559
560 for (i = 0; i < RX_SKBS; i++) {
561 if (pegasus->rx_pool[i]) {
562 dev_kfree_skb(pegasus->rx_pool[i]);
563 pegasus->rx_pool[i] = NULL;
564 }
565 }
566}
567
568static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
569{
570 int i;
571 struct sk_buff *skb;
572
573 for (i = 0; i < RX_SKBS; i++) {
574 if (likely(pegasus->rx_pool[i] != NULL)) {
575 skb = pegasus->rx_pool[i];
576 pegasus->rx_pool[i] = NULL;
577 return skb;
578 }
579 }
580 return NULL;
581}
582
583static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
584{
585 pegasus_t *pegasus = urb->context;
586 struct net_device *net;
587 int rx_status, count = urb->actual_length;
588 u8 *buf = urb->transfer_buffer;
589 __u16 pkt_len;
590
591 if (!pegasus)
592 return;
593
594 net = pegasus->net;
595 if (!netif_device_present(net) || !netif_running(net))
596 return;
597
598 switch (urb->status) {
599 case 0:
600 break;
601 case -ETIMEDOUT:
602 if (netif_msg_rx_err(pegasus))
603 pr_debug("%s: reset MAC\n", net->name);
604 pegasus->flags &= ~PEGASUS_RX_BUSY;
605 break;
606 case -EPIPE: /* stall, or disconnect from TT */
607 /* FIXME schedule work to clear the halt */
608 if (netif_msg_rx_err(pegasus))
609 printk(KERN_WARNING "%s: no rx stall recovery\n",
610 net->name);
611 return;
612 case -ENOENT:
613 case -ECONNRESET:
614 case -ESHUTDOWN:
615 if (netif_msg_ifdown(pegasus))
616 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
617 return;
618 default:
619 if (netif_msg_rx_err(pegasus))
620 pr_debug("%s: RX status %d\n", net->name, urb->status);
621 goto goon;
622 }
623
624 if (!count || count < 4)
625 goto goon;
626
627 rx_status = buf[count - 2];
628 if (rx_status & 0x1e) {
629 if (netif_msg_rx_err(pegasus))
630 pr_debug("%s: RX packet error %x\n",
631 net->name, rx_status);
632 pegasus->stats.rx_errors++;
633 if (rx_status & 0x06) // long or runt
634 pegasus->stats.rx_length_errors++;
635 if (rx_status & 0x08)
636 pegasus->stats.rx_crc_errors++;
637 if (rx_status & 0x10) // extra bits
638 pegasus->stats.rx_frame_errors++;
639 goto goon;
640 }
641 if (pegasus->chip == 0x8513) {
642 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
643 pkt_len &= 0x0fff;
644 pegasus->rx_skb->data += 2;
645 } else {
646 pkt_len = buf[count - 3] << 8;
647 pkt_len += buf[count - 4];
648 pkt_len &= 0xfff;
649 pkt_len -= 8;
650 }
651
652 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700653 * If the packet is unreasonably long, quietly drop it rather than
654 * kernel panicing by calling skb_put.
655 */
656 if (pkt_len > PEGASUS_MTU)
657 goto goon;
658
659 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * at this point we are sure pegasus->rx_skb != NULL
661 * so we go ahead and pass up the packet.
662 */
663 skb_put(pegasus->rx_skb, pkt_len);
664 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
665 netif_rx(pegasus->rx_skb);
666 pegasus->stats.rx_packets++;
667 pegasus->stats.rx_bytes += pkt_len;
668
669 if (pegasus->flags & PEGASUS_UNPLUG)
670 return;
671
672 spin_lock(&pegasus->rx_pool_lock);
673 pegasus->rx_skb = pull_skb(pegasus);
674 spin_unlock(&pegasus->rx_pool_lock);
675
676 if (pegasus->rx_skb == NULL)
677 goto tl_sched;
678goon:
679 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
680 usb_rcvbulkpipe(pegasus->usb, 1),
681 pegasus->rx_skb->data, PEGASUS_MTU + 8,
682 read_bulk_callback, pegasus);
683 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
684 pegasus->flags |= PEGASUS_RX_URB_FAIL;
685 goto tl_sched;
686 } else {
687 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
688 }
689
690 return;
691
692tl_sched:
693 tasklet_schedule(&pegasus->rx_tl);
694}
695
696static void rx_fixup(unsigned long data)
697{
698 pegasus_t *pegasus;
699 unsigned long flags;
700
701 pegasus = (pegasus_t *) data;
702 if (pegasus->flags & PEGASUS_UNPLUG)
703 return;
704
705 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
706 fill_skb_pool(pegasus);
707 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
708 if (pegasus->rx_skb)
709 goto try_again;
710 if (pegasus->rx_skb == NULL) {
711 pegasus->rx_skb = pull_skb(pegasus);
712 }
713 if (pegasus->rx_skb == NULL) {
714 if (netif_msg_rx_err(pegasus))
715 printk(KERN_WARNING "%s: low on memory\n",
716 pegasus->net->name);
717 tasklet_schedule(&pegasus->rx_tl);
718 goto done;
719 }
720 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
721 usb_rcvbulkpipe(pegasus->usb, 1),
722 pegasus->rx_skb->data, PEGASUS_MTU + 8,
723 read_bulk_callback, pegasus);
724try_again:
725 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
726 pegasus->flags |= PEGASUS_RX_URB_FAIL;
727 tasklet_schedule(&pegasus->rx_tl);
728 } else {
729 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
730 }
731done:
732 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
733}
734
735static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
736{
737 pegasus_t *pegasus = urb->context;
738 struct net_device *net = pegasus->net;
739
740 if (!pegasus)
741 return;
742
743 if (!netif_device_present(net) || !netif_running(net))
744 return;
745
746 switch (urb->status) {
747 case -EPIPE:
748 /* FIXME schedule_work() to clear the tx halt */
749 netif_stop_queue(net);
750 if (netif_msg_tx_err(pegasus))
751 printk(KERN_WARNING "%s: no tx stall recovery\n",
752 net->name);
753 return;
754 case -ENOENT:
755 case -ECONNRESET:
756 case -ESHUTDOWN:
757 if (netif_msg_ifdown(pegasus))
758 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
759 return;
760 default:
761 if (netif_msg_tx_err(pegasus))
762 pr_info("%s: TX status %d\n", net->name, urb->status);
763 /* FALL THROUGH */
764 case 0:
765 break;
766 }
767
768 net->trans_start = jiffies;
769 netif_wake_queue(net);
770}
771
772static void intr_callback(struct urb *urb, struct pt_regs *regs)
773{
774 pegasus_t *pegasus = urb->context;
775 struct net_device *net;
776 int status;
777
778 if (!pegasus)
779 return;
780 net = pegasus->net;
781
782 switch (urb->status) {
783 case 0:
784 break;
785 case -ECONNRESET: /* unlink */
786 case -ENOENT:
787 case -ESHUTDOWN:
788 return;
789 default:
790 /* some Pegasus-I products report LOTS of data
791 * toggle errors... avoid log spamming
792 */
793 if (netif_msg_timer(pegasus))
794 pr_debug("%s: intr status %d\n", net->name,
795 urb->status);
796 }
797
798 if (urb->actual_length >= 6) {
799 u8 * d = urb->transfer_buffer;
800
801 /* byte 0 == tx_status1, reg 2B */
802 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
803 |LATE_COL|JABBER_TIMEOUT)) {
804 pegasus->stats.tx_errors++;
805 if (d[0] & TX_UNDERRUN)
806 pegasus->stats.tx_fifo_errors++;
807 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
808 pegasus->stats.tx_aborted_errors++;
809 if (d[0] & LATE_COL)
810 pegasus->stats.tx_window_errors++;
811 }
812
813 /* d[5].LINK_STATUS lies on some adapters.
814 * d[0].NO_CARRIER kicks in only with failed TX.
815 * ... so monitoring with MII may be safest.
816 */
817 if (d[0] & NO_CARRIER)
818 netif_carrier_off(net);
819 else
820 netif_carrier_on(net);
821
822 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
823 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
824 }
825
826 status = usb_submit_urb(urb, SLAB_ATOMIC);
827 if (status && netif_msg_timer(pegasus))
828 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
829 net->name, status);
830}
831
832static void pegasus_tx_timeout(struct net_device *net)
833{
834 pegasus_t *pegasus = netdev_priv(net);
835 if (netif_msg_timer(pegasus))
836 printk(KERN_WARNING "%s: tx timeout\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 usb_unlink_urb(pegasus->tx_urb);
838 pegasus->stats.tx_errors++;
839}
840
841static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
842{
843 pegasus_t *pegasus = netdev_priv(net);
844 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
845 int res;
846 __u16 l16 = skb->len;
847
848 netif_stop_queue(net);
849
850 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
851 memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
852 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
853 usb_sndbulkpipe(pegasus->usb, 2),
854 pegasus->tx_buff, count,
855 write_bulk_callback, pegasus);
856 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
857 if (netif_msg_tx_err(pegasus))
858 printk(KERN_WARNING "%s: fail tx, %d\n",
859 net->name, res);
860 switch (res) {
861 case -EPIPE: /* stall, or disconnect from TT */
862 /* cleanup should already have been scheduled */
863 break;
864 case -ENODEV: /* disconnect() upcoming */
865 break;
866 default:
867 pegasus->stats.tx_errors++;
868 netif_start_queue(net);
869 }
870 } else {
871 pegasus->stats.tx_packets++;
872 pegasus->stats.tx_bytes += skb->len;
873 net->trans_start = jiffies;
874 }
875 dev_kfree_skb(skb);
876
877 return 0;
878}
879
880static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
881{
882 return &((pegasus_t *) netdev_priv(dev))->stats;
883}
884
885static inline void disable_net_traffic(pegasus_t * pegasus)
886{
887 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Petko Manolov4a1728a2005-11-15 09:48:23 +0200889 set_registers(pegasus, EthCtrl0, 2, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static inline void get_interrupt_interval(pegasus_t * pegasus)
893{
894 __u8 data[2];
895
896 read_eprom_word(pegasus, 4, (__u16 *) data);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700897 if (pegasus->usb->speed != USB_SPEED_HIGH) {
898 if (data[1] < 0x80) {
899 if (netif_msg_timer(pegasus))
900 dev_info(&pegasus->intf->dev, "intr interval "
901 "changed from %ums to %ums\n",
902 data[1], 0x80);
903 data[1] = 0x80;
904#ifdef PEGASUS_WRITE_EEPROM
905 write_eprom_word(pegasus, 4, *(__u16 *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 pegasus->intr_interval = data[1];
910}
911
912static void set_carrier(struct net_device *net)
913{
914 pegasus_t *pegasus = netdev_priv(net);
915 u16 tmp;
916
Kevin Vigora85a46f2005-09-22 00:49:24 -0700917 if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (tmp & BMSR_LSTATUS)
921 netif_carrier_on(net);
922 else
923 netif_carrier_off(net);
924}
925
926static void free_all_urbs(pegasus_t * pegasus)
927{
928 usb_free_urb(pegasus->intr_urb);
929 usb_free_urb(pegasus->tx_urb);
930 usb_free_urb(pegasus->rx_urb);
931 usb_free_urb(pegasus->ctrl_urb);
932}
933
934static void unlink_all_urbs(pegasus_t * pegasus)
935{
936 usb_kill_urb(pegasus->intr_urb);
937 usb_kill_urb(pegasus->tx_urb);
938 usb_kill_urb(pegasus->rx_urb);
939 usb_kill_urb(pegasus->ctrl_urb);
940}
941
942static int alloc_urbs(pegasus_t * pegasus)
943{
944 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
945 if (!pegasus->ctrl_urb) {
946 return 0;
947 }
948 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
949 if (!pegasus->rx_urb) {
950 usb_free_urb(pegasus->ctrl_urb);
951 return 0;
952 }
953 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
954 if (!pegasus->tx_urb) {
955 usb_free_urb(pegasus->rx_urb);
956 usb_free_urb(pegasus->ctrl_urb);
957 return 0;
958 }
959 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
960 if (!pegasus->intr_urb) {
961 usb_free_urb(pegasus->tx_urb);
962 usb_free_urb(pegasus->rx_urb);
963 usb_free_urb(pegasus->ctrl_urb);
964 return 0;
965 }
966
967 return 1;
968}
969
970static int pegasus_open(struct net_device *net)
971{
972 pegasus_t *pegasus = netdev_priv(net);
973 int res;
974
975 if (pegasus->rx_skb == NULL)
976 pegasus->rx_skb = pull_skb(pegasus);
977 /*
978 ** Note: no point to free the pool. it is empty :-)
979 */
980 if (!pegasus->rx_skb)
981 return -ENOMEM;
982
983 res = set_registers(pegasus, EthID, 6, net->dev_addr);
984
985 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
986 usb_rcvbulkpipe(pegasus->usb, 1),
987 pegasus->rx_skb->data, PEGASUS_MTU + 8,
988 read_bulk_callback, pegasus);
989 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
990 if (netif_msg_ifup(pegasus))
991 pr_debug("%s: failed rx_urb, %d", net->name, res);
992 goto exit;
993 }
994
995 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
996 usb_rcvintpipe(pegasus->usb, 3),
997 pegasus->intr_buff, sizeof (pegasus->intr_buff),
998 intr_callback, pegasus, pegasus->intr_interval);
999 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1000 if (netif_msg_ifup(pegasus))
1001 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1002 usb_kill_urb(pegasus->rx_urb);
1003 goto exit;
1004 }
1005 if ((res = enable_net_traffic(net, pegasus->usb))) {
1006 if (netif_msg_ifup(pegasus))
1007 pr_debug("%s: can't enable_net_traffic() - %d\n",
1008 net->name, res);
1009 res = -EIO;
1010 usb_kill_urb(pegasus->rx_urb);
1011 usb_kill_urb(pegasus->intr_urb);
1012 free_skb_pool(pegasus);
1013 goto exit;
1014 }
1015 set_carrier(net);
1016 netif_start_queue(net);
1017 if (netif_msg_ifup(pegasus))
1018 pr_debug("%s: open\n", net->name);
1019 res = 0;
1020exit:
1021 return res;
1022}
1023
1024static int pegasus_close(struct net_device *net)
1025{
1026 pegasus_t *pegasus = netdev_priv(net);
1027
1028 netif_stop_queue(net);
1029 if (!(pegasus->flags & PEGASUS_UNPLUG))
1030 disable_net_traffic(pegasus);
1031 tasklet_kill(&pegasus->rx_tl);
1032 unlink_all_urbs(pegasus);
1033
1034 return 0;
1035}
1036
1037static void pegasus_get_drvinfo(struct net_device *dev,
1038 struct ethtool_drvinfo *info)
1039{
1040 pegasus_t *pegasus = netdev_priv(dev);
1041 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1042 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1043 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1044}
1045
1046/* also handles three patterns of some kind in hardware */
1047#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1048
1049static void
1050pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1051{
1052 pegasus_t *pegasus = netdev_priv(dev);
1053
1054 wol->supported = WAKE_MAGIC | WAKE_PHY;
1055 wol->wolopts = pegasus->wolopts;
1056}
1057
1058static int
1059pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1060{
1061 pegasus_t *pegasus = netdev_priv(dev);
1062 u8 reg78 = 0x04;
1063
1064 if (wol->wolopts & ~WOL_SUPPORTED)
1065 return -EINVAL;
1066
1067 if (wol->wolopts & WAKE_MAGIC)
1068 reg78 |= 0x80;
1069 if (wol->wolopts & WAKE_PHY)
1070 reg78 |= 0x40;
1071 /* FIXME this 0x10 bit still needs to get set in the chip... */
1072 if (wol->wolopts)
1073 pegasus->eth_regs[0] |= 0x10;
1074 else
1075 pegasus->eth_regs[0] &= ~0x10;
1076 pegasus->wolopts = wol->wolopts;
1077 return set_register(pegasus, WakeupControl, reg78);
1078}
1079
1080static inline void pegasus_reset_wol(struct net_device *dev)
1081{
1082 struct ethtool_wolinfo wol;
1083
1084 memset(&wol, 0, sizeof wol);
1085 (void) pegasus_set_wol(dev, &wol);
1086}
1087
1088static int
1089pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1090{
1091 pegasus_t *pegasus;
1092
1093 if (in_atomic())
1094 return 0;
1095
1096 pegasus = netdev_priv(dev);
1097 mii_ethtool_gset(&pegasus->mii, ecmd);
1098
1099 return 0;
1100}
1101
1102static int
1103pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1104{
1105 pegasus_t *pegasus = netdev_priv(dev);
1106 return mii_ethtool_sset(&pegasus->mii, ecmd);
1107}
1108
1109static int pegasus_nway_reset(struct net_device *dev)
1110{
1111 pegasus_t *pegasus = netdev_priv(dev);
1112 return mii_nway_restart(&pegasus->mii);
1113}
1114
1115static u32 pegasus_get_link(struct net_device *dev)
1116{
1117 pegasus_t *pegasus = netdev_priv(dev);
1118 return mii_link_ok(&pegasus->mii);
1119}
1120
1121static u32 pegasus_get_msglevel(struct net_device *dev)
1122{
1123 pegasus_t *pegasus = netdev_priv(dev);
1124 return pegasus->msg_enable;
1125}
1126
1127static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1128{
1129 pegasus_t *pegasus = netdev_priv(dev);
1130 pegasus->msg_enable = v;
1131}
1132
1133static struct ethtool_ops ops = {
1134 .get_drvinfo = pegasus_get_drvinfo,
1135 .get_settings = pegasus_get_settings,
1136 .set_settings = pegasus_set_settings,
1137 .nway_reset = pegasus_nway_reset,
1138 .get_link = pegasus_get_link,
1139 .get_msglevel = pegasus_get_msglevel,
1140 .set_msglevel = pegasus_set_msglevel,
1141 .get_wol = pegasus_get_wol,
1142 .set_wol = pegasus_set_wol,
1143};
1144
1145static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1146{
1147 __u16 *data = (__u16 *) & rq->ifr_ifru;
1148 pegasus_t *pegasus = netdev_priv(net);
1149 int res;
1150
1151 switch (cmd) {
1152 case SIOCDEVPRIVATE:
1153 data[0] = pegasus->phy;
1154 case SIOCDEVPRIVATE + 1:
1155 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1156 res = 0;
1157 break;
1158 case SIOCDEVPRIVATE + 2:
1159 if (!capable(CAP_NET_ADMIN))
1160 return -EPERM;
1161 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1162 res = 0;
1163 break;
1164 default:
1165 res = -EOPNOTSUPP;
1166 }
1167 return res;
1168}
1169
1170static void pegasus_set_multicast(struct net_device *net)
1171{
1172 pegasus_t *pegasus = netdev_priv(net);
1173
1174 if (net->flags & IFF_PROMISC) {
1175 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1176 if (netif_msg_link(pegasus))
1177 pr_info("%s: Promiscuous mode enabled.\n", net->name);
YOSHIFUJI Hideakiae0a97b2005-05-25 16:07:04 +09001178 } else if (net->mc_count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 (net->flags & IFF_ALLMULTI)) {
1180 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1181 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1182 if (netif_msg_link(pegasus))
1183 pr_info("%s: set allmulti\n", net->name);
1184 } else {
1185 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1186 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1187 }
1188
1189 pegasus->flags |= ETH_REGS_CHANGE;
1190 ctrl_callback(pegasus->ctrl_urb, NULL);
1191}
1192
1193static __u8 mii_phy_probe(pegasus_t * pegasus)
1194{
1195 int i;
1196 __u16 tmp;
1197
1198 for (i = 0; i < 32; i++) {
1199 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1200 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1201 continue;
1202 else
1203 return i;
1204 }
1205
1206 return 0xff;
1207}
1208
1209static inline void setup_pegasus_II(pegasus_t * pegasus)
1210{
1211 __u8 data = 0xa5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Petko Manolov4a1728a2005-11-15 09:48:23 +02001213 set_register(pegasus, Reg1d, 0);
1214 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 mdelay(100);
1216 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001217 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001219 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Petko Manolov4a1728a2005-11-15 09:48:23 +02001221 set_register(pegasus, 0x83, data);
1222 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (data == 0xa5) {
1225 pegasus->chip = 0x8513;
1226 } else {
1227 pegasus->chip = 0;
1228 }
1229
Petko Manolov4a1728a2005-11-15 09:48:23 +02001230 set_register(pegasus, 0x80, 0xc0);
1231 set_register(pegasus, 0x83, 0xff);
1232 set_register(pegasus, 0x84, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001235 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001237 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240
1241static struct workqueue_struct *pegasus_workqueue = NULL;
1242#define CARRIER_CHECK_DELAY (2 * HZ)
1243
1244static void check_carrier(void *data)
1245{
1246 pegasus_t *pegasus = data;
1247 set_carrier(pegasus->net);
1248 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1249 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1250 CARRIER_CHECK_DELAY);
1251 }
1252}
1253
1254static int pegasus_probe(struct usb_interface *intf,
1255 const struct usb_device_id *id)
1256{
1257 struct usb_device *dev = interface_to_usbdev(intf);
1258 struct net_device *net;
1259 pegasus_t *pegasus;
1260 int dev_index = id - pegasus_ids;
1261 int res = -ENOMEM;
1262
1263 usb_get_dev(dev);
1264 net = alloc_etherdev(sizeof(struct pegasus));
1265 if (!net) {
1266 dev_err(&intf->dev, "can't allocate %s\n", "device");
1267 goto out;
1268 }
1269
1270 pegasus = netdev_priv(net);
1271 memset(pegasus, 0, sizeof (struct pegasus));
1272 pegasus->dev_index = dev_index;
1273 init_waitqueue_head(&pegasus->ctrl_wait);
1274
1275 if (!alloc_urbs(pegasus)) {
1276 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1277 goto out1;
1278 }
1279
1280 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1281
1282 INIT_WORK(&pegasus->carrier_check, check_carrier, pegasus);
1283
1284 pegasus->intf = intf;
1285 pegasus->usb = dev;
1286 pegasus->net = net;
1287 SET_MODULE_OWNER(net);
1288 net->open = pegasus_open;
1289 net->stop = pegasus_close;
1290 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1291 net->tx_timeout = pegasus_tx_timeout;
1292 net->do_ioctl = pegasus_ioctl;
1293 net->hard_start_xmit = pegasus_start_xmit;
1294 net->set_multicast_list = pegasus_set_multicast;
1295 net->get_stats = pegasus_netdev_stats;
1296 SET_ETHTOOL_OPS(net, &ops);
1297 pegasus->mii.dev = net;
1298 pegasus->mii.mdio_read = mdio_read;
1299 pegasus->mii.mdio_write = mdio_write;
1300 pegasus->mii.phy_id_mask = 0x1f;
1301 pegasus->mii.reg_num_mask = 0x1f;
1302 spin_lock_init(&pegasus->rx_pool_lock);
1303 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1304 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1305
1306 pegasus->features = usb_dev_id[dev_index].private;
1307 get_interrupt_interval(pegasus);
1308 if (reset_mac(pegasus)) {
1309 dev_err(&intf->dev, "can't reset MAC\n");
1310 res = -EIO;
1311 goto out2;
1312 }
1313 set_ethernet_addr(pegasus);
1314 fill_skb_pool(pegasus);
1315 if (pegasus->features & PEGASUS_II) {
1316 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1317 setup_pegasus_II(pegasus);
1318 }
1319 pegasus->phy = mii_phy_probe(pegasus);
1320 if (pegasus->phy == 0xff) {
1321 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1322 pegasus->phy = 1;
1323 }
1324 pegasus->mii.phy_id = pegasus->phy;
1325 usb_set_intfdata(intf, pegasus);
1326 SET_NETDEV_DEV(net, &intf->dev);
1327 pegasus_reset_wol(net);
1328 res = register_netdev(net);
1329 if (res)
1330 goto out3;
1331 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1332 CARRIER_CHECK_DELAY);
1333
1334 dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1335 net->name,
1336 usb_dev_id[dev_index].name,
1337 net->dev_addr [0], net->dev_addr [1],
1338 net->dev_addr [2], net->dev_addr [3],
1339 net->dev_addr [4], net->dev_addr [5]);
1340 return 0;
1341
1342out3:
1343 usb_set_intfdata(intf, NULL);
1344 free_skb_pool(pegasus);
1345out2:
1346 free_all_urbs(pegasus);
1347out1:
1348 free_netdev(net);
1349out:
1350 usb_put_dev(dev);
1351 return res;
1352}
1353
1354static void pegasus_disconnect(struct usb_interface *intf)
1355{
1356 struct pegasus *pegasus = usb_get_intfdata(intf);
1357
1358 usb_set_intfdata(intf, NULL);
1359 if (!pegasus) {
1360 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1361 return;
1362 }
1363
1364 pegasus->flags |= PEGASUS_UNPLUG;
1365 cancel_delayed_work(&pegasus->carrier_check);
1366 unregister_netdev(pegasus->net);
1367 usb_put_dev(interface_to_usbdev(intf));
Kevin Vigora85a46f2005-09-22 00:49:24 -07001368 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 free_all_urbs(pegasus);
1370 free_skb_pool(pegasus);
1371 if (pegasus->rx_skb)
1372 dev_kfree_skb(pegasus->rx_skb);
1373 free_netdev(pegasus->net);
1374}
1375
David Brownell27d72e82005-04-18 17:39:22 -07001376static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct pegasus *pegasus = usb_get_intfdata(intf);
1379
1380 netif_device_detach (pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001381 if (netif_running(pegasus->net)) {
1382 cancel_delayed_work(&pegasus->carrier_check);
1383
1384 usb_kill_urb(pegasus->rx_urb);
1385 usb_kill_urb(pegasus->intr_urb);
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388}
1389
1390static int pegasus_resume (struct usb_interface *intf)
1391{
1392 struct pegasus *pegasus = usb_get_intfdata(intf);
1393
1394 netif_device_attach (pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001395 if (netif_running(pegasus->net)) {
1396 pegasus->rx_urb->status = 0;
1397 pegasus->rx_urb->actual_length = 0;
Al Viro9727d042005-04-26 07:43:41 -07001398 read_bulk_callback(pegasus->rx_urb, NULL);
David Brownell27d72e82005-04-18 17:39:22 -07001399
1400 pegasus->intr_urb->status = 0;
1401 pegasus->intr_urb->actual_length = 0;
Al Viro9727d042005-04-26 07:43:41 -07001402 intr_callback(pegasus->intr_urb, NULL);
David Brownell27d72e82005-04-18 17:39:22 -07001403
1404 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1405 CARRIER_CHECK_DELAY);
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 0;
1408}
1409
1410static struct usb_driver pegasus_driver = {
1411 .name = driver_name,
1412 .probe = pegasus_probe,
1413 .disconnect = pegasus_disconnect,
1414 .id_table = pegasus_ids,
1415 .suspend = pegasus_suspend,
1416 .resume = pegasus_resume,
1417};
1418
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001419static void parse_id(char *id)
1420{
1421 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1422 char *token, *name=NULL;
1423
1424 if ((token = strsep(&id, ":")) != NULL)
1425 name = token;
1426 /* name now points to a null terminated string*/
1427 if ((token = strsep(&id, ":")) != NULL)
1428 vendor_id = simple_strtoul(token, NULL, 16);
1429 if ((token = strsep(&id, ":")) != NULL)
1430 device_id = simple_strtoul(token, NULL, 16);
1431 flags = simple_strtoul(id, NULL, 16);
1432 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1433 driver_name, name, vendor_id, device_id, flags);
1434
1435 if (vendor_id > 0x10000 || vendor_id == 0)
1436 return;
1437 if (device_id > 0x10000 || device_id == 0)
1438 return;
1439
1440 for (i=0; usb_dev_id[i].name; i++);
1441 usb_dev_id[i].name = name;
1442 usb_dev_id[i].vendor = vendor_id;
1443 usb_dev_id[i].device = device_id;
1444 usb_dev_id[i].private = flags;
1445 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1446 pegasus_ids[i].idVendor = vendor_id;
1447 pegasus_ids[i].idProduct = device_id;
1448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450static int __init pegasus_init(void)
1451{
1452 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001453 if (devid)
1454 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1456 if (!pegasus_workqueue)
1457 return -ENOMEM;
1458 return usb_register(&pegasus_driver);
1459}
1460
1461static void __exit pegasus_exit(void)
1462{
1463 destroy_workqueue(pegasus_workqueue);
1464 usb_deregister(&pegasus_driver);
1465}
1466
1467module_init(pegasus_init);
1468module_exit(pegasus_exit);