blob: ed4a508ef26220d19a7bb4620be5c84eb43e9ca3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ChangeLog:
9 * .... Most of the time spent on reading sources & docs.
10 * v0.2.x First official release for the Linux kernel.
11 * v0.3.0 Beutified and structured, some bugs fixed.
12 * v0.3.x URBifying bulk requests and bugfixing. First relatively
13 * stable release. Still can touch device's registers only
14 * from top-halves.
15 * v0.4.0 Control messages remained unurbified are now URBs.
16 * Now we can touch the HW at any time.
17 * v0.4.9 Control urbs again use process context to wait. Argh...
18 * Some long standing bugs (enable_net_traffic) fixed.
19 * Also nasty trick about resubmiting control urb from
20 * interrupt context used. Please let me know how it
21 * behaves. Pegasus II support added since this version.
22 * TODO: suppressing HCD warnings spewage on disconnect.
23 * v0.4.13 Ethernet address is now set at probe(), not at open()
24 * time as this seems to break dhcpd.
25 * v0.5.0 branch to 2.5.x kernels
26 * v0.5.1 ethtool support added
27 * v0.5.5 rx socket buffers are in a pool and the their allocation
28 * is out of the interrupt routine.
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/ethtool.h>
38#include <linux/mii.h>
39#include <linux/usb.h>
40#include <linux/module.h>
41#include <asm/byteorder.h>
42#include <asm/uaccess.h>
43#include "pegasus.h"
44
45/*
46 * Version Information
47 */
Petko Manolov37cf3472006-09-27 14:25:37 -070048#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
51
52static const char driver_name[] = "pegasus";
53
54#undef PEGASUS_WRITE_EEPROM
55#define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56 BMSR_100FULL | BMSR_ANEGCAPABLE)
57
58static int loopback = 0;
59static int mii_mode = 0;
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020060static char *devid=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static struct usb_eth_dev usb_dev_id[] = {
63#define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
Chris Rankinab854b22009-10-13 00:32:02 -070065#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
66 PEGASUS_DEV(pn, vid, pid, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "pegasus.h"
68#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070069#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020070 {NULL, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 {NULL, 0, 0, 0}
72};
73
74static struct usb_device_id pegasus_ids[] = {
75#define PEGASUS_DEV(pn, vid, pid, flags) \
76 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
Chris Rankinab854b22009-10-13 00:32:02 -070077/*
78 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
79 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
80 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
81 * case anyway, seeing as the pegasus is for "Wired" adaptors.
82 */
83#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
84 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
85 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "pegasus.h"
87#undef PEGASUS_DEV
Chris Rankinab854b22009-10-13 00:32:02 -070088#undef PEGASUS_DEV_CLASS
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020089 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 {}
91};
92
93MODULE_AUTHOR(DRIVER_AUTHOR);
94MODULE_DESCRIPTION(DRIVER_DESC);
95MODULE_LICENSE("GPL");
96module_param(loopback, bool, 0);
97module_param(mii_mode, bool, 0);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +020098module_param(devid, charp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
100MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +0200101MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* use ethtool to change the level for any given device */
104static int msg_level = -1;
105module_param (msg_level, int, 0);
106MODULE_PARM_DESC (msg_level, "Override default message level");
107
108MODULE_DEVICE_TABLE(usb, pegasus_ids);
Oliver Neukum8cb89572009-01-08 11:22:25 -0800109static const struct net_device_ops pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int update_eth_regs_async(pegasus_t *);
112/* Aargh!!! I _really_ hate such tweaks */
David Howells7d12e782006-10-05 14:55:46 +0100113static void ctrl_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 pegasus_t *pegasus = urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800116 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (!pegasus)
119 return;
120
Oliver Neukumc94cb312008-12-18 23:00:59 -0800121 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 case 0:
123 if (pegasus->flags & ETH_REGS_CHANGE) {
124 pegasus->flags &= ~ETH_REGS_CHANGE;
125 pegasus->flags |= ETH_REGS_CHANGED;
126 update_eth_regs_async(pegasus);
127 return;
128 }
129 break;
130 case -EINPROGRESS:
131 return;
132 case -ENOENT:
133 break;
134 default:
David Brownelle000ea12008-09-02 11:34:24 -0700135 if (netif_msg_drv(pegasus) && printk_ratelimit())
Petko Manolov4a1728a2005-11-15 09:48:23 +0200136 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800137 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 pegasus->flags &= ~ETH_REGS_CHANGED;
140 wake_up(&pegasus->ctrl_wait);
141}
142
143static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
144 void *data)
145{
146 int ret;
147 char *buffer;
148 DECLARE_WAITQUEUE(wait, current);
149
150 buffer = kmalloc(size, GFP_KERNEL);
151 if (!buffer) {
152 if (netif_msg_drv(pegasus))
153 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700154 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -ENOMEM;
156 }
157 add_wait_queue(&pegasus->ctrl_wait, &wait);
158 set_current_state(TASK_UNINTERRUPTIBLE);
159 while (pegasus->flags & ETH_REGS_CHANGED)
160 schedule();
161 remove_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_RUNNING);
163
164 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
165 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
166 pegasus->dr.wValue = cpu_to_le16(0);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700167 pegasus->dr.wIndex = cpu_to_le16(indx);
168 pegasus->dr.wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 pegasus->ctrl_urb->transfer_buffer_length = size;
170
171 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
172 usb_rcvctrlpipe(pegasus->usb, 0),
173 (char *) &pegasus->dr,
174 buffer, size, ctrl_callback, pegasus);
175
176 add_wait_queue(&pegasus->ctrl_wait, &wait);
177 set_current_state(TASK_UNINTERRUPTIBLE);
178
179 /* using ATOMIC, we'd never wake up if we slept */
180 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
Oliver Neukum10c82112006-11-23 15:40:17 +0100181 set_current_state(TASK_RUNNING);
David Brownell955a2602006-05-26 10:17:03 -0700182 if (ret == -ENODEV)
183 netif_device_detach(pegasus->net);
David Brownelle000ea12008-09-02 11:34:24 -0700184 if (netif_msg_drv(pegasus) && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dev_err(&pegasus->intf->dev, "%s, status %d\n",
Harvey Harrison653c0312008-10-20 16:00:08 -0700186 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out;
188 }
189
190 schedule();
191out:
192 remove_wait_queue(&pegasus->ctrl_wait, &wait);
193 memcpy(data, buffer, size);
194 kfree(buffer);
195
196 return ret;
197}
198
199static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
200 void *data)
201{
202 int ret;
203 char *buffer;
204 DECLARE_WAITQUEUE(wait, current);
205
206 buffer = kmalloc(size, GFP_KERNEL);
207 if (!buffer) {
208 if (netif_msg_drv(pegasus))
209 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
Harvey Harrison653c0312008-10-20 16:00:08 -0700210 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOMEM;
212 }
213 memcpy(buffer, data, size);
214
215 add_wait_queue(&pegasus->ctrl_wait, &wait);
216 set_current_state(TASK_UNINTERRUPTIBLE);
217 while (pegasus->flags & ETH_REGS_CHANGED)
218 schedule();
219 remove_wait_queue(&pegasus->ctrl_wait, &wait);
220 set_current_state(TASK_RUNNING);
221
222 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
223 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
224 pegasus->dr.wValue = cpu_to_le16(0);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700225 pegasus->dr.wIndex = cpu_to_le16(indx);
226 pegasus->dr.wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 pegasus->ctrl_urb->transfer_buffer_length = size;
228
229 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
230 usb_sndctrlpipe(pegasus->usb, 0),
231 (char *) &pegasus->dr,
232 buffer, size, ctrl_callback, pegasus);
233
234 add_wait_queue(&pegasus->ctrl_wait, &wait);
235 set_current_state(TASK_UNINTERRUPTIBLE);
236
237 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700238 if (ret == -ENODEV)
239 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (netif_msg_drv(pegasus))
241 dev_err(&pegasus->intf->dev, "%s, status %d\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700242 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
244 }
245
246 schedule();
247out:
248 remove_wait_queue(&pegasus->ctrl_wait, &wait);
249 kfree(buffer);
250
251 return ret;
252}
253
254static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
255{
256 int ret;
257 char *tmp;
258 DECLARE_WAITQUEUE(wait, current);
259
260 tmp = kmalloc(1, GFP_KERNEL);
261 if (!tmp) {
262 if (netif_msg_drv(pegasus))
263 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700264 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -ENOMEM;
266 }
267 memcpy(tmp, &data, 1);
268 add_wait_queue(&pegasus->ctrl_wait, &wait);
269 set_current_state(TASK_UNINTERRUPTIBLE);
270 while (pegasus->flags & ETH_REGS_CHANGED)
271 schedule();
272 remove_wait_queue(&pegasus->ctrl_wait, &wait);
273 set_current_state(TASK_RUNNING);
274
275 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
276 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
277 pegasus->dr.wValue = cpu_to_le16(data);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700278 pegasus->dr.wIndex = cpu_to_le16(indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 pegasus->dr.wLength = cpu_to_le16(1);
280 pegasus->ctrl_urb->transfer_buffer_length = 1;
281
282 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
283 usb_sndctrlpipe(pegasus->usb, 0),
284 (char *) &pegasus->dr,
Petko Manolov016534c2006-03-30 09:59:22 +0300285 tmp, 1, ctrl_callback, pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 add_wait_queue(&pegasus->ctrl_wait, &wait);
288 set_current_state(TASK_UNINTERRUPTIBLE);
289
290 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
David Brownell955a2602006-05-26 10:17:03 -0700291 if (ret == -ENODEV)
292 netif_device_detach(pegasus->net);
David Brownelle000ea12008-09-02 11:34:24 -0700293 if (netif_msg_drv(pegasus) && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dev_err(&pegasus->intf->dev, "%s, status %d\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700295 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto out;
297 }
298
299 schedule();
300out:
301 remove_wait_queue(&pegasus->ctrl_wait, &wait);
302 kfree(tmp);
303
304 return ret;
305}
306
307static int update_eth_regs_async(pegasus_t * pegasus)
308{
309 int ret;
310
311 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
312 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
Michael Buesche3453f62009-06-18 07:03:47 +0000313 pegasus->dr.wValue = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
315 pegasus->dr.wLength = cpu_to_le16(3);
316 pegasus->ctrl_urb->transfer_buffer_length = 3;
317
318 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
319 usb_sndctrlpipe(pegasus->usb, 0),
320 (char *) &pegasus->dr,
321 pegasus->eth_regs, 3, ctrl_callback, pegasus);
322
David Brownell955a2602006-05-26 10:17:03 -0700323 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
324 if (ret == -ENODEV)
325 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (netif_msg_drv(pegasus))
327 dev_err(&pegasus->intf->dev, "%s, status %d\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700328 __func__, ret);
David Brownell955a2602006-05-26 10:17:03 -0700329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 return ret;
332}
333
Dan Williamsc43c49b2007-04-24 10:20:06 -0400334/* Returns 0 on success, error on failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
336{
337 int i;
338 __u8 data[4] = { phy, 0, 0, indx };
339 __le16 regdi;
340 int ret;
341
Petko Manolov4a1728a2005-11-15 09:48:23 +0200342 set_register(pegasus, PhyCtrl, 0);
343 set_registers(pegasus, PhyAddr, sizeof (data), data);
344 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 for (i = 0; i < REG_TIMEOUT; i++) {
346 ret = get_registers(pegasus, PhyCtrl, 1, data);
David Brownell7e713b82006-05-01 14:02:45 -0700347 if (ret == -ESHUTDOWN)
348 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (data[0] & PHY_DONE)
350 break;
351 }
352 if (i < REG_TIMEOUT) {
353 ret = get_registers(pegasus, PhyData, 2, &regdi);
354 *regd = le16_to_cpu(regdi);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
David Brownell7e713b82006-05-01 14:02:45 -0700357fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (netif_msg_drv(pegasus))
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700359 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Petko Manolov4a1728a2005-11-15 09:48:23 +0200361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static int mdio_read(struct net_device *dev, int phy_id, int loc)
365{
366 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
367 u16 res;
368
369 read_mii_word(pegasus, phy_id, loc, &res);
370 return (int)res;
371}
372
373static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
374{
375 int i;
376 __u8 data[4] = { phy, 0, 0, indx };
377 int ret;
378
379 data[1] = (u8) regd;
380 data[2] = (u8) (regd >> 8);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200381 set_register(pegasus, PhyCtrl, 0);
382 set_registers(pegasus, PhyAddr, sizeof(data), data);
383 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (i = 0; i < REG_TIMEOUT; i++) {
385 ret = get_registers(pegasus, PhyCtrl, 1, data);
David Brownell7e713b82006-05-01 14:02:45 -0700386 if (ret == -ESHUTDOWN)
387 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (data[0] & PHY_DONE)
389 break;
390 }
391 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200392 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Brownell7e713b82006-05-01 14:02:45 -0700394fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (netif_msg_drv(pegasus))
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700396 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200397 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
401{
402 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
403
404 write_mii_word(pegasus, phy_id, loc, val);
405}
406
407static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
408{
409 int i;
410 __u8 tmp;
411 __le16 retdatai;
412 int ret;
413
Petko Manolov4a1728a2005-11-15 09:48:23 +0200414 set_register(pegasus, EpromCtrl, 0);
415 set_register(pegasus, EpromOffset, index);
416 set_register(pegasus, EpromCtrl, EPROM_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 for (i = 0; i < REG_TIMEOUT; i++) {
419 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
420 if (tmp & EPROM_DONE)
421 break;
David Brownell7e713b82006-05-01 14:02:45 -0700422 if (ret == -ESHUTDOWN)
423 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 if (i < REG_TIMEOUT) {
426 ret = get_registers(pegasus, EpromData, 2, &retdatai);
427 *retdata = le16_to_cpu(retdatai);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
David Brownell7e713b82006-05-01 14:02:45 -0700431fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (netif_msg_drv(pegasus))
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700433 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200434 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437#ifdef PEGASUS_WRITE_EEPROM
438static inline void enable_eprom_write(pegasus_t * pegasus)
439{
440 __u8 tmp;
441 int ret;
442
Petko Manolov4a1728a2005-11-15 09:48:23 +0200443 get_registers(pegasus, EthCtrl2, 1, &tmp);
444 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447static inline void disable_eprom_write(pegasus_t * pegasus)
448{
449 __u8 tmp;
450 int ret;
451
Petko Manolov4a1728a2005-11-15 09:48:23 +0200452 get_registers(pegasus, EthCtrl2, 1, &tmp);
453 set_register(pegasus, EpromCtrl, 0);
454 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
458{
459 int i;
460 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
461 int ret;
Michael Buesche3453f62009-06-18 07:03:47 +0000462 __le16 le_data = cpu_to_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Petko Manolov4a1728a2005-11-15 09:48:23 +0200464 set_registers(pegasus, EpromOffset, 4, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 enable_eprom_write(pegasus);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200466 set_register(pegasus, EpromOffset, index);
Michael Buesche3453f62009-06-18 07:03:47 +0000467 set_registers(pegasus, EpromData, 2, &le_data);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200468 set_register(pegasus, EpromCtrl, EPROM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 for (i = 0; i < REG_TIMEOUT; i++) {
471 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
David Brownell7e713b82006-05-01 14:02:45 -0700472 if (ret == -ESHUTDOWN)
473 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (tmp & EPROM_DONE)
475 break;
476 }
477 disable_eprom_write(pegasus);
478 if (i < REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200479 return ret;
David Brownell7e713b82006-05-01 14:02:45 -0700480fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (netif_msg_drv(pegasus))
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700482 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
Petko Manolov4a1728a2005-11-15 09:48:23 +0200483 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485#endif /* PEGASUS_WRITE_EEPROM */
486
487static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
488{
489 int i;
490 __u16 w16;
491
492 for (i = 0; i < 3; i++) {
493 read_eprom_word(pegasus, i, &w16);
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700494 ((__le16 *) id)[i] = cpu_to_le16(w16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496}
497
498static void set_ethernet_addr(pegasus_t * pegasus)
499{
500 __u8 node_id[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Petko Manolov37cf3472006-09-27 14:25:37 -0700502 if (pegasus->features & PEGASUS_II) {
503 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
504 } else {
505 get_node_id(pegasus, node_id);
506 set_registers(pegasus, EthID, sizeof (node_id), node_id);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
509}
510
511static inline int reset_mac(pegasus_t * pegasus)
512{
513 __u8 data = 0x8;
514 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Petko Manolov4a1728a2005-11-15 09:48:23 +0200516 set_register(pegasus, EthCtrl1, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 for (i = 0; i < REG_TIMEOUT; i++) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200518 get_registers(pegasus, EthCtrl1, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (~data & 0x08) {
520 if (loopback & 1)
521 break;
522 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
Petko Manolov4a1728a2005-11-15 09:48:23 +0200523 set_register(pegasus, Gpio1, 0x34);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
Petko Manolov4a1728a2005-11-15 09:48:23 +0200525 set_register(pegasus, Gpio1, 0x26);
526 set_register(pegasus, Gpio0, pegasus->features);
527 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 }
530 }
531 if (i == REG_TIMEOUT)
Petko Manolov4a1728a2005-11-15 09:48:23 +0200532 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
535 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
Petko Manolov4a1728a2005-11-15 09:48:23 +0200536 set_register(pegasus, Gpio0, 0x24);
537 set_register(pegasus, Gpio0, 0x26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
540 __u16 auxmode;
541 read_mii_word(pegasus, 3, 0x1b, &auxmode);
542 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
543 }
544
545 return 0;
546}
547
548static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
549{
550 __u16 linkpart;
551 __u8 data[4];
552 pegasus_t *pegasus = netdev_priv(dev);
553 int ret;
554
555 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
556 data[0] = 0xc9;
557 data[1] = 0;
558 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
559 data[1] |= 0x20; /* set full duplex */
560 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
561 data[1] |= 0x10; /* set 100 Mbps */
562 if (mii_mode)
563 data[1] = 0;
564 data[2] = (loopback & 1) ? 0x09 : 0x01;
565
566 memcpy(pegasus->eth_regs, data, sizeof (data));
567 ret = set_registers(pegasus, EthCtrl0, 3, data);
568
569 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
Malte Doersamefafe6f2006-01-28 17:48:33 +0100570 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
572 u16 auxmode;
573 read_mii_word(pegasus, 0, 0x1b, &auxmode);
574 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
575 }
576
Petko Manolov4a1728a2005-11-15 09:48:23 +0200577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580static void fill_skb_pool(pegasus_t * pegasus)
581{
582 int i;
583
584 for (i = 0; i < RX_SKBS; i++) {
585 if (pegasus->rx_pool[i])
586 continue;
587 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
588 /*
589 ** we give up if the allocation fail. the tasklet will be
590 ** rescheduled again anyway...
591 */
592 if (pegasus->rx_pool[i] == NULL)
593 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 skb_reserve(pegasus->rx_pool[i], 2);
595 }
596}
597
598static void free_skb_pool(pegasus_t * pegasus)
599{
600 int i;
601
602 for (i = 0; i < RX_SKBS; i++) {
603 if (pegasus->rx_pool[i]) {
604 dev_kfree_skb(pegasus->rx_pool[i]);
605 pegasus->rx_pool[i] = NULL;
606 }
607 }
608}
609
610static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
611{
612 int i;
613 struct sk_buff *skb;
614
615 for (i = 0; i < RX_SKBS; i++) {
616 if (likely(pegasus->rx_pool[i] != NULL)) {
617 skb = pegasus->rx_pool[i];
618 pegasus->rx_pool[i] = NULL;
619 return skb;
620 }
621 }
622 return NULL;
623}
624
David Howells7d12e782006-10-05 14:55:46 +0100625static void read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 pegasus_t *pegasus = urb->context;
628 struct net_device *net;
629 int rx_status, count = urb->actual_length;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800630 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 u8 *buf = urb->transfer_buffer;
632 __u16 pkt_len;
633
634 if (!pegasus)
635 return;
636
637 net = pegasus->net;
638 if (!netif_device_present(net) || !netif_running(net))
639 return;
640
Oliver Neukumc94cb312008-12-18 23:00:59 -0800641 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 case 0:
643 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700644 case -ETIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (netif_msg_rx_err(pegasus))
646 pr_debug("%s: reset MAC\n", net->name);
647 pegasus->flags &= ~PEGASUS_RX_BUSY;
648 break;
649 case -EPIPE: /* stall, or disconnect from TT */
650 /* FIXME schedule work to clear the halt */
651 if (netif_msg_rx_err(pegasus))
652 printk(KERN_WARNING "%s: no rx stall recovery\n",
653 net->name);
654 return;
655 case -ENOENT:
656 case -ECONNRESET:
657 case -ESHUTDOWN:
658 if (netif_msg_ifdown(pegasus))
Oliver Neukumc94cb312008-12-18 23:00:59 -0800659 pr_debug("%s: rx unlink, %d\n", net->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return;
661 default:
662 if (netif_msg_rx_err(pegasus))
Oliver Neukumc94cb312008-12-18 23:00:59 -0800663 pr_debug("%s: RX status %d\n", net->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto goon;
665 }
666
667 if (!count || count < 4)
668 goto goon;
669
670 rx_status = buf[count - 2];
671 if (rx_status & 0x1e) {
672 if (netif_msg_rx_err(pegasus))
673 pr_debug("%s: RX packet error %x\n",
674 net->name, rx_status);
675 pegasus->stats.rx_errors++;
676 if (rx_status & 0x06) // long or runt
677 pegasus->stats.rx_length_errors++;
678 if (rx_status & 0x08)
679 pegasus->stats.rx_crc_errors++;
680 if (rx_status & 0x10) // extra bits
681 pegasus->stats.rx_frame_errors++;
682 goto goon;
683 }
684 if (pegasus->chip == 0x8513) {
685 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
686 pkt_len &= 0x0fff;
687 pegasus->rx_skb->data += 2;
688 } else {
689 pkt_len = buf[count - 3] << 8;
690 pkt_len += buf[count - 4];
691 pkt_len &= 0xfff;
692 pkt_len -= 8;
693 }
694
695 /*
Kevin Vigora85a46f2005-09-22 00:49:24 -0700696 * If the packet is unreasonably long, quietly drop it rather than
697 * kernel panicing by calling skb_put.
698 */
699 if (pkt_len > PEGASUS_MTU)
700 goto goon;
701
702 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * at this point we are sure pegasus->rx_skb != NULL
704 * so we go ahead and pass up the packet.
705 */
706 skb_put(pegasus->rx_skb, pkt_len);
707 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
708 netif_rx(pegasus->rx_skb);
709 pegasus->stats.rx_packets++;
710 pegasus->stats.rx_bytes += pkt_len;
711
712 if (pegasus->flags & PEGASUS_UNPLUG)
713 return;
714
715 spin_lock(&pegasus->rx_pool_lock);
716 pegasus->rx_skb = pull_skb(pegasus);
717 spin_unlock(&pegasus->rx_pool_lock);
718
719 if (pegasus->rx_skb == NULL)
720 goto tl_sched;
721goon:
722 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
723 usb_rcvbulkpipe(pegasus->usb, 1),
724 pegasus->rx_skb->data, PEGASUS_MTU + 8,
725 read_bulk_callback, pegasus);
David Brownell955a2602006-05-26 10:17:03 -0700726 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
727 if (rx_status == -ENODEV)
728 netif_device_detach(pegasus->net);
729 else if (rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 pegasus->flags |= PEGASUS_RX_URB_FAIL;
731 goto tl_sched;
732 } else {
733 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
734 }
735
736 return;
737
738tl_sched:
739 tasklet_schedule(&pegasus->rx_tl);
740}
741
742static void rx_fixup(unsigned long data)
743{
744 pegasus_t *pegasus;
745 unsigned long flags;
David Brownell955a2602006-05-26 10:17:03 -0700746 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 pegasus = (pegasus_t *) data;
749 if (pegasus->flags & PEGASUS_UNPLUG)
750 return;
751
752 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
753 fill_skb_pool(pegasus);
754 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
755 if (pegasus->rx_skb)
756 goto try_again;
757 if (pegasus->rx_skb == NULL) {
758 pegasus->rx_skb = pull_skb(pegasus);
759 }
760 if (pegasus->rx_skb == NULL) {
761 if (netif_msg_rx_err(pegasus))
762 printk(KERN_WARNING "%s: low on memory\n",
763 pegasus->net->name);
764 tasklet_schedule(&pegasus->rx_tl);
765 goto done;
766 }
767 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
768 usb_rcvbulkpipe(pegasus->usb, 1),
769 pegasus->rx_skb->data, PEGASUS_MTU + 8,
770 read_bulk_callback, pegasus);
771try_again:
David Brownell955a2602006-05-26 10:17:03 -0700772 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
773 if (status == -ENODEV)
774 netif_device_detach(pegasus->net);
775 else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 pegasus->flags |= PEGASUS_RX_URB_FAIL;
777 tasklet_schedule(&pegasus->rx_tl);
778 } else {
779 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
780 }
781done:
782 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
783}
784
David Howells7d12e782006-10-05 14:55:46 +0100785static void write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 pegasus_t *pegasus = urb->context;
Micah Gruber93519822007-07-23 16:05:52 +0800788 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800789 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!pegasus)
792 return;
793
Micah Gruber93519822007-07-23 16:05:52 +0800794 net = pegasus->net;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!netif_device_present(net) || !netif_running(net))
797 return;
798
Oliver Neukumc94cb312008-12-18 23:00:59 -0800799 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case -EPIPE:
801 /* FIXME schedule_work() to clear the tx halt */
802 netif_stop_queue(net);
803 if (netif_msg_tx_err(pegasus))
804 printk(KERN_WARNING "%s: no tx stall recovery\n",
805 net->name);
806 return;
807 case -ENOENT:
808 case -ECONNRESET:
809 case -ESHUTDOWN:
810 if (netif_msg_ifdown(pegasus))
Oliver Neukumc94cb312008-12-18 23:00:59 -0800811 pr_debug("%s: tx unlink, %d\n", net->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813 default:
814 if (netif_msg_tx_err(pegasus))
Oliver Neukumc94cb312008-12-18 23:00:59 -0800815 pr_info("%s: TX status %d\n", net->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* FALL THROUGH */
817 case 0:
818 break;
819 }
820
821 net->trans_start = jiffies;
822 netif_wake_queue(net);
823}
824
David Howells7d12e782006-10-05 14:55:46 +0100825static void intr_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 pegasus_t *pegasus = urb->context;
828 struct net_device *net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800829 int res, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (!pegasus)
832 return;
833 net = pegasus->net;
834
Oliver Neukumc94cb312008-12-18 23:00:59 -0800835 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case 0:
837 break;
838 case -ECONNRESET: /* unlink */
839 case -ENOENT:
840 case -ESHUTDOWN:
841 return;
842 default:
843 /* some Pegasus-I products report LOTS of data
844 * toggle errors... avoid log spamming
845 */
846 if (netif_msg_timer(pegasus))
847 pr_debug("%s: intr status %d\n", net->name,
Oliver Neukumc94cb312008-12-18 23:00:59 -0800848 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 if (urb->actual_length >= 6) {
852 u8 * d = urb->transfer_buffer;
853
854 /* byte 0 == tx_status1, reg 2B */
855 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
856 |LATE_COL|JABBER_TIMEOUT)) {
857 pegasus->stats.tx_errors++;
858 if (d[0] & TX_UNDERRUN)
859 pegasus->stats.tx_fifo_errors++;
860 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
861 pegasus->stats.tx_aborted_errors++;
862 if (d[0] & LATE_COL)
863 pegasus->stats.tx_window_errors++;
864 }
865
866 /* d[5].LINK_STATUS lies on some adapters.
867 * d[0].NO_CARRIER kicks in only with failed TX.
868 * ... so monitoring with MII may be safest.
869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
872 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
873 }
874
Oliver Neukumc94cb312008-12-18 23:00:59 -0800875 res = usb_submit_urb(urb, GFP_ATOMIC);
876 if (res == -ENODEV)
David Brownell955a2602006-05-26 10:17:03 -0700877 netif_device_detach(pegasus->net);
Oliver Neukumc94cb312008-12-18 23:00:59 -0800878 if (res && netif_msg_timer(pegasus))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800880 net->name, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void pegasus_tx_timeout(struct net_device *net)
884{
885 pegasus_t *pegasus = netdev_priv(net);
886 if (netif_msg_timer(pegasus))
887 printk(KERN_WARNING "%s: tx timeout\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 usb_unlink_urb(pegasus->tx_urb);
889 pegasus->stats.tx_errors++;
890}
891
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000892static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
893 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 pegasus_t *pegasus = netdev_priv(net);
896 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
897 int res;
898 __u16 l16 = skb->len;
899
900 netif_stop_queue(net);
901
902 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300903 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
905 usb_sndbulkpipe(pegasus->usb, 2),
906 pegasus->tx_buff, count,
907 write_bulk_callback, pegasus);
908 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
909 if (netif_msg_tx_err(pegasus))
910 printk(KERN_WARNING "%s: fail tx, %d\n",
911 net->name, res);
912 switch (res) {
913 case -EPIPE: /* stall, or disconnect from TT */
914 /* cleanup should already have been scheduled */
915 break;
916 case -ENODEV: /* disconnect() upcoming */
Oliver Neukum9dd014e2009-04-17 01:40:19 -0700917 case -EPERM:
David Brownell955a2602006-05-26 10:17:03 -0700918 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 default:
921 pegasus->stats.tx_errors++;
922 netif_start_queue(net);
923 }
924 } else {
925 pegasus->stats.tx_packets++;
926 pegasus->stats.tx_bytes += skb->len;
927 net->trans_start = jiffies;
928 }
929 dev_kfree_skb(skb);
930
Patrick McHardy6ed10652009-06-23 06:03:08 +0000931 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
935{
936 return &((pegasus_t *) netdev_priv(dev))->stats;
937}
938
939static inline void disable_net_traffic(pegasus_t * pegasus)
940{
Michael Buesche3453f62009-06-18 07:03:47 +0000941 __le16 tmp = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Michael Buesche3453f62009-06-18 07:03:47 +0000943 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946static inline void get_interrupt_interval(pegasus_t * pegasus)
947{
Michael Buesche3453f62009-06-18 07:03:47 +0000948 u16 data;
949 u8 interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Michael Buesche3453f62009-06-18 07:03:47 +0000951 read_eprom_word(pegasus, 4, &data);
952 interval = data >> 8;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700953 if (pegasus->usb->speed != USB_SPEED_HIGH) {
Michael Buesche3453f62009-06-18 07:03:47 +0000954 if (interval < 0x80) {
Kevin Vigora85a46f2005-09-22 00:49:24 -0700955 if (netif_msg_timer(pegasus))
956 dev_info(&pegasus->intf->dev, "intr interval "
957 "changed from %ums to %ums\n",
Michael Buesche3453f62009-06-18 07:03:47 +0000958 interval, 0x80);
959 interval = 0x80;
960 data = (data & 0x00FF) | ((u16)interval << 8);
Kevin Vigora85a46f2005-09-22 00:49:24 -0700961#ifdef PEGASUS_WRITE_EEPROM
Michael Buesche3453f62009-06-18 07:03:47 +0000962 write_eprom_word(pegasus, 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#endif
Kevin Vigora85a46f2005-09-22 00:49:24 -0700964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Michael Buesche3453f62009-06-18 07:03:47 +0000966 pegasus->intr_interval = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969static void set_carrier(struct net_device *net)
970{
971 pegasus_t *pegasus = netdev_priv(net);
972 u16 tmp;
973
Dan Williamsc43c49b2007-04-24 10:20:06 -0400974 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return;
Kevin Vigora85a46f2005-09-22 00:49:24 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (tmp & BMSR_LSTATUS)
978 netif_carrier_on(net);
979 else
980 netif_carrier_off(net);
981}
982
983static void free_all_urbs(pegasus_t * pegasus)
984{
985 usb_free_urb(pegasus->intr_urb);
986 usb_free_urb(pegasus->tx_urb);
987 usb_free_urb(pegasus->rx_urb);
988 usb_free_urb(pegasus->ctrl_urb);
989}
990
991static void unlink_all_urbs(pegasus_t * pegasus)
992{
993 usb_kill_urb(pegasus->intr_urb);
994 usb_kill_urb(pegasus->tx_urb);
995 usb_kill_urb(pegasus->rx_urb);
996 usb_kill_urb(pegasus->ctrl_urb);
997}
998
999static int alloc_urbs(pegasus_t * pegasus)
1000{
1001 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1002 if (!pegasus->ctrl_urb) {
1003 return 0;
1004 }
1005 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1006 if (!pegasus->rx_urb) {
1007 usb_free_urb(pegasus->ctrl_urb);
1008 return 0;
1009 }
1010 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1011 if (!pegasus->tx_urb) {
1012 usb_free_urb(pegasus->rx_urb);
1013 usb_free_urb(pegasus->ctrl_urb);
1014 return 0;
1015 }
1016 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1017 if (!pegasus->intr_urb) {
1018 usb_free_urb(pegasus->tx_urb);
1019 usb_free_urb(pegasus->rx_urb);
1020 usb_free_urb(pegasus->ctrl_urb);
1021 return 0;
1022 }
1023
1024 return 1;
1025}
1026
1027static int pegasus_open(struct net_device *net)
1028{
1029 pegasus_t *pegasus = netdev_priv(net);
1030 int res;
1031
1032 if (pegasus->rx_skb == NULL)
1033 pegasus->rx_skb = pull_skb(pegasus);
1034 /*
1035 ** Note: no point to free the pool. it is empty :-)
1036 */
1037 if (!pegasus->rx_skb)
1038 return -ENOMEM;
1039
1040 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1041
1042 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1043 usb_rcvbulkpipe(pegasus->usb, 1),
1044 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1045 read_bulk_callback, pegasus);
1046 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -07001047 if (res == -ENODEV)
1048 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (netif_msg_ifup(pegasus))
1050 pr_debug("%s: failed rx_urb, %d", net->name, res);
1051 goto exit;
1052 }
1053
1054 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1055 usb_rcvintpipe(pegasus->usb, 3),
1056 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1057 intr_callback, pegasus, pegasus->intr_interval);
1058 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
David Brownell955a2602006-05-26 10:17:03 -07001059 if (res == -ENODEV)
1060 netif_device_detach(pegasus->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (netif_msg_ifup(pegasus))
1062 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1063 usb_kill_urb(pegasus->rx_urb);
1064 goto exit;
1065 }
1066 if ((res = enable_net_traffic(net, pegasus->usb))) {
1067 if (netif_msg_ifup(pegasus))
1068 pr_debug("%s: can't enable_net_traffic() - %d\n",
1069 net->name, res);
1070 res = -EIO;
1071 usb_kill_urb(pegasus->rx_urb);
1072 usb_kill_urb(pegasus->intr_urb);
1073 free_skb_pool(pegasus);
1074 goto exit;
1075 }
1076 set_carrier(net);
1077 netif_start_queue(net);
1078 if (netif_msg_ifup(pegasus))
1079 pr_debug("%s: open\n", net->name);
1080 res = 0;
1081exit:
1082 return res;
1083}
1084
1085static int pegasus_close(struct net_device *net)
1086{
1087 pegasus_t *pegasus = netdev_priv(net);
1088
1089 netif_stop_queue(net);
1090 if (!(pegasus->flags & PEGASUS_UNPLUG))
1091 disable_net_traffic(pegasus);
1092 tasklet_kill(&pegasus->rx_tl);
1093 unlink_all_urbs(pegasus);
1094
1095 return 0;
1096}
1097
1098static void pegasus_get_drvinfo(struct net_device *dev,
1099 struct ethtool_drvinfo *info)
1100{
1101 pegasus_t *pegasus = netdev_priv(dev);
1102 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1103 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1104 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1105}
1106
1107/* also handles three patterns of some kind in hardware */
1108#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1109
1110static void
1111pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1112{
1113 pegasus_t *pegasus = netdev_priv(dev);
1114
1115 wol->supported = WAKE_MAGIC | WAKE_PHY;
1116 wol->wolopts = pegasus->wolopts;
1117}
1118
1119static int
1120pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1121{
1122 pegasus_t *pegasus = netdev_priv(dev);
1123 u8 reg78 = 0x04;
1124
1125 if (wol->wolopts & ~WOL_SUPPORTED)
1126 return -EINVAL;
1127
1128 if (wol->wolopts & WAKE_MAGIC)
1129 reg78 |= 0x80;
1130 if (wol->wolopts & WAKE_PHY)
1131 reg78 |= 0x40;
1132 /* FIXME this 0x10 bit still needs to get set in the chip... */
1133 if (wol->wolopts)
1134 pegasus->eth_regs[0] |= 0x10;
1135 else
1136 pegasus->eth_regs[0] &= ~0x10;
1137 pegasus->wolopts = wol->wolopts;
1138 return set_register(pegasus, WakeupControl, reg78);
1139}
1140
1141static inline void pegasus_reset_wol(struct net_device *dev)
1142{
1143 struct ethtool_wolinfo wol;
1144
1145 memset(&wol, 0, sizeof wol);
1146 (void) pegasus_set_wol(dev, &wol);
1147}
1148
1149static int
1150pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1151{
1152 pegasus_t *pegasus;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 pegasus = netdev_priv(dev);
1155 mii_ethtool_gset(&pegasus->mii, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157}
1158
1159static int
1160pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1161{
1162 pegasus_t *pegasus = netdev_priv(dev);
1163 return mii_ethtool_sset(&pegasus->mii, ecmd);
1164}
1165
1166static int pegasus_nway_reset(struct net_device *dev)
1167{
1168 pegasus_t *pegasus = netdev_priv(dev);
1169 return mii_nway_restart(&pegasus->mii);
1170}
1171
1172static u32 pegasus_get_link(struct net_device *dev)
1173{
1174 pegasus_t *pegasus = netdev_priv(dev);
1175 return mii_link_ok(&pegasus->mii);
1176}
1177
1178static u32 pegasus_get_msglevel(struct net_device *dev)
1179{
1180 pegasus_t *pegasus = netdev_priv(dev);
1181 return pegasus->msg_enable;
1182}
1183
1184static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1185{
1186 pegasus_t *pegasus = netdev_priv(dev);
1187 pegasus->msg_enable = v;
1188}
1189
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001190static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 .get_drvinfo = pegasus_get_drvinfo,
1192 .get_settings = pegasus_get_settings,
1193 .set_settings = pegasus_set_settings,
1194 .nway_reset = pegasus_nway_reset,
1195 .get_link = pegasus_get_link,
1196 .get_msglevel = pegasus_get_msglevel,
1197 .set_msglevel = pegasus_set_msglevel,
1198 .get_wol = pegasus_get_wol,
1199 .set_wol = pegasus_set_wol,
1200};
1201
1202static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1203{
1204 __u16 *data = (__u16 *) & rq->ifr_ifru;
1205 pegasus_t *pegasus = netdev_priv(net);
1206 int res;
1207
1208 switch (cmd) {
1209 case SIOCDEVPRIVATE:
1210 data[0] = pegasus->phy;
1211 case SIOCDEVPRIVATE + 1:
1212 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1213 res = 0;
1214 break;
1215 case SIOCDEVPRIVATE + 2:
1216 if (!capable(CAP_NET_ADMIN))
1217 return -EPERM;
1218 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1219 res = 0;
1220 break;
1221 default:
1222 res = -EOPNOTSUPP;
1223 }
1224 return res;
1225}
1226
1227static void pegasus_set_multicast(struct net_device *net)
1228{
1229 pegasus_t *pegasus = netdev_priv(net);
1230
1231 if (net->flags & IFF_PROMISC) {
1232 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1233 if (netif_msg_link(pegasus))
1234 pr_info("%s: Promiscuous mode enabled.\n", net->name);
David Brownelle000ea12008-09-02 11:34:24 -07001235 } else if (net->mc_count || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1237 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1238 if (netif_msg_link(pegasus))
David Brownellcda28362008-11-16 00:36:08 -08001239 pr_debug("%s: set allmulti\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 } else {
1241 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1242 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1243 }
1244
David Brownelle000ea12008-09-02 11:34:24 -07001245 pegasus->ctrl_urb->status = 0;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 pegasus->flags |= ETH_REGS_CHANGE;
David Howells7d12e782006-10-05 14:55:46 +01001248 ctrl_callback(pegasus->ctrl_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251static __u8 mii_phy_probe(pegasus_t * pegasus)
1252{
1253 int i;
1254 __u16 tmp;
1255
1256 for (i = 0; i < 32; i++) {
1257 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1258 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1259 continue;
1260 else
1261 return i;
1262 }
1263
1264 return 0xff;
1265}
1266
1267static inline void setup_pegasus_II(pegasus_t * pegasus)
1268{
1269 __u8 data = 0xa5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Petko Manolov4a1728a2005-11-15 09:48:23 +02001271 set_register(pegasus, Reg1d, 0);
1272 set_register(pegasus, Reg7b, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 mdelay(100);
1274 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001275 set_register(pegasus, Reg7b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001277 set_register(pegasus, Reg7b, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Petko Manolov4a1728a2005-11-15 09:48:23 +02001279 set_register(pegasus, 0x83, data);
1280 get_registers(pegasus, 0x83, 1, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (data == 0xa5) {
1283 pegasus->chip = 0x8513;
1284 } else {
1285 pegasus->chip = 0;
1286 }
1287
Petko Manolov4a1728a2005-11-15 09:48:23 +02001288 set_register(pegasus, 0x80, 0xc0);
1289 set_register(pegasus, 0x83, 0xff);
1290 set_register(pegasus, 0x84, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (pegasus->features & HAS_HOME_PNA && mii_mode)
Petko Manolov4a1728a2005-11-15 09:48:23 +02001293 set_register(pegasus, Reg81, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 else
Petko Manolov4a1728a2005-11-15 09:48:23 +02001295 set_register(pegasus, Reg81, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298
David Brownellcda28362008-11-16 00:36:08 -08001299static int pegasus_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static struct workqueue_struct *pegasus_workqueue = NULL;
1301#define CARRIER_CHECK_DELAY (2 * HZ)
1302
David Howellsc4028952006-11-22 14:57:56 +00001303static void check_carrier(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
David Howellsc4028952006-11-22 14:57:56 +00001305 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 set_carrier(pegasus->net);
1307 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1308 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1309 CARRIER_CHECK_DELAY);
1310 }
1311}
1312
Ben Collins4f631352008-07-30 12:39:02 -07001313static int pegasus_blacklisted(struct usb_device *udev)
1314{
1315 struct usb_device_descriptor *udd = &udev->descriptor;
1316
1317 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1318 * dongle which happens to have the same ID.
1319 */
Michael Buesche3453f62009-06-18 07:03:47 +00001320 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1321 (udd->idProduct == cpu_to_le16(0x0121)) &&
Ben Collins4f631352008-07-30 12:39:02 -07001322 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1323 (udd->bDeviceProtocol == 1))
1324 return 1;
1325
1326 return 0;
1327}
1328
David Brownellcda28362008-11-16 00:36:08 -08001329/* we rely on probe() and remove() being serialized so we
1330 * don't need extra locking on pegasus_count.
1331 */
1332static void pegasus_dec_workqueue(void)
1333{
1334 pegasus_count--;
1335 if (pegasus_count == 0) {
1336 destroy_workqueue(pegasus_workqueue);
1337 pegasus_workqueue = NULL;
1338 }
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341static int pegasus_probe(struct usb_interface *intf,
1342 const struct usb_device_id *id)
1343{
1344 struct usb_device *dev = interface_to_usbdev(intf);
1345 struct net_device *net;
1346 pegasus_t *pegasus;
1347 int dev_index = id - pegasus_ids;
1348 int res = -ENOMEM;
1349
David Brownellcda28362008-11-16 00:36:08 -08001350 if (pegasus_blacklisted(dev))
1351 return -ENODEV;
Ben Collins4f631352008-07-30 12:39:02 -07001352
David Brownellcda28362008-11-16 00:36:08 -08001353 if (pegasus_count == 0) {
1354 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1355 if (!pegasus_workqueue)
1356 return -ENOMEM;
Ben Collins4f631352008-07-30 12:39:02 -07001357 }
David Brownellcda28362008-11-16 00:36:08 -08001358 pegasus_count++;
1359
1360 usb_get_dev(dev);
Ben Collins4f631352008-07-30 12:39:02 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 net = alloc_etherdev(sizeof(struct pegasus));
1363 if (!net) {
1364 dev_err(&intf->dev, "can't allocate %s\n", "device");
1365 goto out;
1366 }
1367
1368 pegasus = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 pegasus->dev_index = dev_index;
1370 init_waitqueue_head(&pegasus->ctrl_wait);
1371
1372 if (!alloc_urbs(pegasus)) {
1373 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1374 goto out1;
1375 }
1376
1377 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1378
David Howellsc4028952006-11-22 14:57:56 +00001379 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 pegasus->intf = intf;
1382 pegasus->usb = dev;
1383 pegasus->net = net;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001384
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
Oliver Neukum8cb89572009-01-08 11:22:25 -08001387 net->netdev_ops = &pegasus_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 SET_ETHTOOL_OPS(net, &ops);
1389 pegasus->mii.dev = net;
1390 pegasus->mii.mdio_read = mdio_read;
1391 pegasus->mii.mdio_write = mdio_write;
1392 pegasus->mii.phy_id_mask = 0x1f;
1393 pegasus->mii.reg_num_mask = 0x1f;
1394 spin_lock_init(&pegasus->rx_pool_lock);
1395 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1396 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1397
1398 pegasus->features = usb_dev_id[dev_index].private;
1399 get_interrupt_interval(pegasus);
1400 if (reset_mac(pegasus)) {
1401 dev_err(&intf->dev, "can't reset MAC\n");
1402 res = -EIO;
1403 goto out2;
1404 }
1405 set_ethernet_addr(pegasus);
1406 fill_skb_pool(pegasus);
1407 if (pegasus->features & PEGASUS_II) {
1408 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1409 setup_pegasus_II(pegasus);
1410 }
1411 pegasus->phy = mii_phy_probe(pegasus);
1412 if (pegasus->phy == 0xff) {
1413 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1414 pegasus->phy = 1;
1415 }
1416 pegasus->mii.phy_id = pegasus->phy;
1417 usb_set_intfdata(intf, pegasus);
1418 SET_NETDEV_DEV(net, &intf->dev);
1419 pegasus_reset_wol(net);
1420 res = register_netdev(net);
1421 if (res)
1422 goto out3;
1423 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1424 CARRIER_CHECK_DELAY);
1425
Johannes Berge1749612008-10-27 15:59:26 -07001426 dev_info(&intf->dev, "%s, %s, %pM\n",
Joe Perches0795af52007-10-03 17:59:30 -07001427 net->name,
1428 usb_dev_id[dev_index].name,
Johannes Berge1749612008-10-27 15:59:26 -07001429 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431
1432out3:
1433 usb_set_intfdata(intf, NULL);
1434 free_skb_pool(pegasus);
1435out2:
1436 free_all_urbs(pegasus);
1437out1:
1438 free_netdev(net);
1439out:
1440 usb_put_dev(dev);
David Brownellcda28362008-11-16 00:36:08 -08001441 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return res;
1443}
1444
1445static void pegasus_disconnect(struct usb_interface *intf)
1446{
1447 struct pegasus *pegasus = usb_get_intfdata(intf);
1448
1449 usb_set_intfdata(intf, NULL);
1450 if (!pegasus) {
1451 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1452 return;
1453 }
1454
1455 pegasus->flags |= PEGASUS_UNPLUG;
1456 cancel_delayed_work(&pegasus->carrier_check);
1457 unregister_netdev(pegasus->net);
1458 usb_put_dev(interface_to_usbdev(intf));
Kevin Vigora85a46f2005-09-22 00:49:24 -07001459 unlink_all_urbs(pegasus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 free_all_urbs(pegasus);
1461 free_skb_pool(pegasus);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001462 if (pegasus->rx_skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 dev_kfree_skb(pegasus->rx_skb);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001464 pegasus->rx_skb = NULL;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 free_netdev(pegasus->net);
David Brownellcda28362008-11-16 00:36:08 -08001467 pegasus_dec_workqueue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
David Brownell27d72e82005-04-18 17:39:22 -07001470static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct pegasus *pegasus = usb_get_intfdata(intf);
1473
1474 netif_device_detach (pegasus->net);
David Brownell7e713b82006-05-01 14:02:45 -07001475 cancel_delayed_work(&pegasus->carrier_check);
David Brownell27d72e82005-04-18 17:39:22 -07001476 if (netif_running(pegasus->net)) {
David Brownell27d72e82005-04-18 17:39:22 -07001477 usb_kill_urb(pegasus->rx_urb);
1478 usb_kill_urb(pegasus->intr_urb);
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
1483static int pegasus_resume (struct usb_interface *intf)
1484{
1485 struct pegasus *pegasus = usb_get_intfdata(intf);
1486
1487 netif_device_attach (pegasus->net);
David Brownell27d72e82005-04-18 17:39:22 -07001488 if (netif_running(pegasus->net)) {
1489 pegasus->rx_urb->status = 0;
1490 pegasus->rx_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001491 read_bulk_callback(pegasus->rx_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001492
1493 pegasus->intr_urb->status = 0;
1494 pegasus->intr_urb->actual_length = 0;
David Howells7d12e782006-10-05 14:55:46 +01001495 intr_callback(pegasus->intr_urb);
David Brownell27d72e82005-04-18 17:39:22 -07001496 }
David Brownell7e713b82006-05-01 14:02:45 -07001497 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1498 CARRIER_CHECK_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500}
1501
Oliver Neukum8cb89572009-01-08 11:22:25 -08001502static const struct net_device_ops pegasus_netdev_ops = {
1503 .ndo_open = pegasus_open,
1504 .ndo_stop = pegasus_close,
1505 .ndo_do_ioctl = pegasus_ioctl,
1506 .ndo_start_xmit = pegasus_start_xmit,
1507 .ndo_set_multicast_list = pegasus_set_multicast,
1508 .ndo_get_stats = pegasus_netdev_stats,
1509 .ndo_tx_timeout = pegasus_tx_timeout,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001510 .ndo_change_mtu = eth_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00001511 .ndo_set_mac_address = eth_mac_addr,
1512 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum8cb89572009-01-08 11:22:25 -08001513};
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515static struct usb_driver pegasus_driver = {
1516 .name = driver_name,
1517 .probe = pegasus_probe,
1518 .disconnect = pegasus_disconnect,
1519 .id_table = pegasus_ids,
1520 .suspend = pegasus_suspend,
1521 .resume = pegasus_resume,
1522};
1523
David Brownellcda28362008-11-16 00:36:08 -08001524static void __init parse_id(char *id)
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001525{
1526 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1527 char *token, *name=NULL;
1528
1529 if ((token = strsep(&id, ":")) != NULL)
1530 name = token;
1531 /* name now points to a null terminated string*/
1532 if ((token = strsep(&id, ":")) != NULL)
1533 vendor_id = simple_strtoul(token, NULL, 16);
1534 if ((token = strsep(&id, ":")) != NULL)
1535 device_id = simple_strtoul(token, NULL, 16);
1536 flags = simple_strtoul(id, NULL, 16);
1537 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1538 driver_name, name, vendor_id, device_id, flags);
1539
1540 if (vendor_id > 0x10000 || vendor_id == 0)
1541 return;
1542 if (device_id > 0x10000 || device_id == 0)
1543 return;
1544
1545 for (i=0; usb_dev_id[i].name; i++);
1546 usb_dev_id[i].name = name;
1547 usb_dev_id[i].vendor = vendor_id;
1548 usb_dev_id[i].device = device_id;
1549 usb_dev_id[i].private = flags;
1550 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1551 pegasus_ids[i].idVendor = vendor_id;
1552 pegasus_ids[i].idProduct = device_id;
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555static int __init pegasus_init(void)
1556{
1557 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
A.YOSHIYAMAa4f81a62005-11-15 09:55:18 +02001558 if (devid)
1559 parse_id(devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return usb_register(&pegasus_driver);
1561}
1562
1563static void __exit pegasus_exit(void)
1564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 usb_deregister(&pegasus_driver);
1566}
1567
1568module_init(pegasus_init);
1569module_exit(pegasus_exit);