blob: 7ae82446b93aed59f3dd646f5df5767ab3feb74f [file] [log] [blame]
Peter Korsgaardd0374f42007-02-16 17:03:54 +01001/*
2 * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices
3 *
4 * Peter Korsgaard <jacmet@sunsite.dk>
5 *
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
9 */
10
11//#define DEBUG
12
13#include <linux/module.h>
14#include <linux/sched.h>
Randy Dunlap686149f2007-03-16 18:33:13 -070015#include <linux/stddef.h>
Peter Korsgaardd0374f42007-02-16 17:03:54 +010016#include <linux/init.h>
17#include <linux/netdevice.h>
18#include <linux/etherdevice.h>
19#include <linux/ethtool.h>
20#include <linux/mii.h>
21#include <linux/usb.h>
22#include <linux/crc32.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020023#include <linux/usb/usbnet.h>
Peter Korsgaardd0374f42007-02-16 17:03:54 +010024
25/* datasheet:
Wu Fengguang98658bc2009-01-06 10:56:07 -080026 http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
Peter Korsgaardd0374f42007-02-16 17:03:54 +010027*/
28
29/* control requests */
30#define DM_READ_REGS 0x00
31#define DM_WRITE_REGS 0x01
32#define DM_READ_MEMS 0x02
33#define DM_WRITE_REG 0x03
34#define DM_WRITE_MEMS 0x05
35#define DM_WRITE_MEM 0x07
36
37/* registers */
38#define DM_NET_CTRL 0x00
39#define DM_RX_CTRL 0x05
40#define DM_SHARED_CTRL 0x0b
41#define DM_SHARED_ADDR 0x0c
42#define DM_SHARED_DATA 0x0d /* low + high */
43#define DM_PHY_ADDR 0x10 /* 6 bytes */
44#define DM_MCAST_ADDR 0x16 /* 8 bytes */
45#define DM_GPR_CTRL 0x1e
46#define DM_GPR_DATA 0x1f
47
48#define DM_MAX_MCAST 64
49#define DM_MCAST_SIZE 8
50#define DM_EEPROM_LEN 256
51#define DM_TX_OVERHEAD 2 /* 2 byte header */
52#define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
53#define DM_TIMEOUT 1000
54
55
56static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
57{
Peter Korsgaard16d78bc252008-07-14 09:07:32 +020058 void *buf;
59 int err = -ENOMEM;
60
Peter Korsgaardd0374f42007-02-16 17:03:54 +010061 devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
Peter Korsgaard16d78bc252008-07-14 09:07:32 +020062
63 buf = kmalloc(length, GFP_KERNEL);
64 if (!buf)
65 goto out;
66
67 err = usb_control_msg(dev->udev,
68 usb_rcvctrlpipe(dev->udev, 0),
69 DM_READ_REGS,
70 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
71 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
72 if (err == length)
73 memcpy(data, buf, length);
74 else if (err >= 0)
75 err = -EINVAL;
76 kfree(buf);
77
78 out:
79 return err;
Peter Korsgaardd0374f42007-02-16 17:03:54 +010080}
81
82static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
83{
84 return dm_read(dev, reg, 1, value);
85}
86
87static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
88{
Peter Korsgaard16d78bc252008-07-14 09:07:32 +020089 void *buf = NULL;
90 int err = -ENOMEM;
91
Peter Korsgaardd0374f42007-02-16 17:03:54 +010092 devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
Peter Korsgaard16d78bc252008-07-14 09:07:32 +020093
94 if (data) {
95 buf = kmalloc(length, GFP_KERNEL);
96 if (!buf)
97 goto out;
98 memcpy(buf, data, length);
99 }
100
101 err = usb_control_msg(dev->udev,
102 usb_sndctrlpipe(dev->udev, 0),
103 DM_WRITE_REGS,
104 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
105 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
106 kfree(buf);
107 if (err >= 0 && err < length)
108 err = -EINVAL;
109 out:
110 return err;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100111}
112
113static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
114{
115 devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
116 return usb_control_msg(dev->udev,
117 usb_sndctrlpipe(dev->udev, 0),
118 DM_WRITE_REG,
119 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
Randy Dunlap686149f2007-03-16 18:33:13 -0700120 value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100121}
122
123static void dm_write_async_callback(struct urb *urb)
124{
125 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800126 int status = urb->status;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100127
Oliver Neukumc94cb312008-12-18 23:00:59 -0800128 if (status < 0)
Peter Korsgaard77b69012007-11-15 11:01:02 +0100129 printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800130 status);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100131
132 kfree(req);
133 usb_free_urb(urb);
134}
135
Peter Korsgaardba734f32007-11-15 11:10:12 +0100136static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
137 u16 length, void *data)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100138{
139 struct usb_ctrlrequest *req;
140 struct urb *urb;
141 int status;
142
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100143 urb = usb_alloc_urb(0, GFP_ATOMIC);
144 if (!urb) {
Peter Korsgaardba734f32007-11-15 11:10:12 +0100145 deverr(dev, "Error allocating URB in dm_write_async_helper!");
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100146 return;
147 }
148
149 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
150 if (!req) {
151 deverr(dev, "Failed to allocate memory for control request");
152 usb_free_urb(urb);
153 return;
154 }
155
156 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
Peter Korsgaardba734f32007-11-15 11:10:12 +0100157 req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG;
158 req->wValue = cpu_to_le16(value);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100159 req->wIndex = cpu_to_le16(reg);
160 req->wLength = cpu_to_le16(length);
161
162 usb_fill_control_urb(urb, dev->udev,
163 usb_sndctrlpipe(dev->udev, 0),
164 (void *)req, data, length,
165 dm_write_async_callback, req);
166
167 status = usb_submit_urb(urb, GFP_ATOMIC);
168 if (status < 0) {
169 deverr(dev, "Error submitting the control message: status=%d",
170 status);
171 kfree(req);
172 usb_free_urb(urb);
173 }
174}
175
Peter Korsgaardba734f32007-11-15 11:10:12 +0100176static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
177{
178 devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
179
180 dm_write_async_helper(dev, reg, 0, length, data);
181}
182
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100183static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
184{
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100185 devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
186 reg, value);
187
Peter Korsgaardba734f32007-11-15 11:10:12 +0100188 dm_write_async_helper(dev, reg, value, 0, NULL);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100189}
190
Al Viroeca1ad82008-03-16 22:21:54 +0000191static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100192{
193 int ret, i;
194
195 mutex_lock(&dev->phy_mutex);
196
197 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
198 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
199
200 for (i = 0; i < DM_TIMEOUT; i++) {
201 u8 tmp;
202
203 udelay(1);
204 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
205 if (ret < 0)
206 goto out;
207
208 /* ready */
209 if ((tmp & 1) == 0)
210 break;
211 }
212
213 if (i == DM_TIMEOUT) {
214 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
215 ret = -EIO;
216 goto out;
217 }
218
219 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
220 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
221
222 devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
223 phy, reg, *value, ret);
224
225 out:
226 mutex_unlock(&dev->phy_mutex);
227 return ret;
228}
229
Al Viroeca1ad82008-03-16 22:21:54 +0000230static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100231{
232 int ret, i;
233
234 mutex_lock(&dev->phy_mutex);
235
236 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
237 if (ret < 0)
238 goto out;
239
240 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
241 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
242
243 for (i = 0; i < DM_TIMEOUT; i++) {
244 u8 tmp;
245
246 udelay(1);
247 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
248 if (ret < 0)
249 goto out;
250
251 /* ready */
252 if ((tmp & 1) == 0)
253 break;
254 }
255
256 if (i == DM_TIMEOUT) {
257 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
258 ret = -EIO;
259 goto out;
260 }
261
262 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
263
264out:
265 mutex_unlock(&dev->phy_mutex);
266 return ret;
267}
268
269static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
270{
271 return dm_read_shared_word(dev, 0, offset, value);
272}
273
274
275
276static int dm9601_get_eeprom_len(struct net_device *dev)
277{
278 return DM_EEPROM_LEN;
279}
280
281static int dm9601_get_eeprom(struct net_device *net,
282 struct ethtool_eeprom *eeprom, u8 * data)
283{
284 struct usbnet *dev = netdev_priv(net);
Al Viroeca1ad82008-03-16 22:21:54 +0000285 __le16 *ebuf = (__le16 *) data;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100286 int i;
287
288 /* access is 16bit */
289 if ((eeprom->offset % 2) || (eeprom->len % 2))
290 return -EINVAL;
291
292 for (i = 0; i < eeprom->len / 2; i++) {
293 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
294 &ebuf[i]) < 0)
295 return -EINVAL;
296 }
297 return 0;
298}
299
300static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
301{
302 struct usbnet *dev = netdev_priv(netdev);
303
Al Viroeca1ad82008-03-16 22:21:54 +0000304 __le16 res;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100305
306 if (phy_id) {
307 devdbg(dev, "Only internal phy supported");
308 return 0;
309 }
310
311 dm_read_shared_word(dev, 1, loc, &res);
312
313 devdbg(dev,
314 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
315 phy_id, loc, le16_to_cpu(res));
316
317 return le16_to_cpu(res);
318}
319
320static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
321 int val)
322{
323 struct usbnet *dev = netdev_priv(netdev);
Al Viroeca1ad82008-03-16 22:21:54 +0000324 __le16 res = cpu_to_le16(val);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100325
326 if (phy_id) {
327 devdbg(dev, "Only internal phy supported");
328 return;
329 }
330
331 devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
332 phy_id, loc, val);
333
334 dm_write_shared_word(dev, 1, loc, res);
335}
336
337static void dm9601_get_drvinfo(struct net_device *net,
338 struct ethtool_drvinfo *info)
339{
340 /* Inherit standard device info */
341 usbnet_get_drvinfo(net, info);
342 info->eedump_len = DM_EEPROM_LEN;
343}
344
345static u32 dm9601_get_link(struct net_device *net)
346{
347 struct usbnet *dev = netdev_priv(net);
348
349 return mii_link_ok(&dev->mii);
350}
351
352static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
353{
354 struct usbnet *dev = netdev_priv(net);
355
356 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
357}
358
359static struct ethtool_ops dm9601_ethtool_ops = {
360 .get_drvinfo = dm9601_get_drvinfo,
361 .get_link = dm9601_get_link,
362 .get_msglevel = usbnet_get_msglevel,
363 .set_msglevel = usbnet_set_msglevel,
364 .get_eeprom_len = dm9601_get_eeprom_len,
365 .get_eeprom = dm9601_get_eeprom,
366 .get_settings = usbnet_get_settings,
367 .set_settings = usbnet_set_settings,
368 .nway_reset = usbnet_nway_reset,
369};
370
371static void dm9601_set_multicast(struct net_device *net)
372{
373 struct usbnet *dev = netdev_priv(net);
374 /* We use the 20 byte dev->data for our 8 byte filter buffer
375 * to avoid allocating memory that is tricky to free later */
376 u8 *hashes = (u8 *) & dev->data;
Peter Korsgaard33edded2008-03-18 23:16:54 +0100377 u8 rx_ctl = 0x31;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100378
379 memset(hashes, 0x00, DM_MCAST_SIZE);
380 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
381
382 if (net->flags & IFF_PROMISC) {
383 rx_ctl |= 0x02;
384 } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
385 rx_ctl |= 0x04;
386 } else if (net->mc_count) {
387 struct dev_mc_list *mc_list = net->mc_list;
388 int i;
389
Peter Korsgaard47df9762008-03-28 22:25:29 +0100390 for (i = 0; i < net->mc_count; i++, mc_list = mc_list->next) {
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100391 u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
392 hashes[crc >> 3] |= 1 << (crc & 0x7);
393 }
394 }
395
396 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
397 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
398}
399
Wu Fengguang4b9f8ec2009-01-06 18:52:58 +0000400static void __dm9601_set_mac_address(struct usbnet *dev)
401{
402 dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
403}
404
Peter Korsgaard753dcfe2008-10-24 09:08:27 +0200405static int dm9601_set_mac_address(struct net_device *net, void *p)
406{
407 struct sockaddr *addr = p;
408 struct usbnet *dev = netdev_priv(net);
409
Wu Fengguangf52deb02009-01-08 10:47:01 -0800410 if (!is_valid_ether_addr(addr->sa_data)) {
411 dev_err(&net->dev, "not setting invalid mac address %pM\n",
412 addr->sa_data);
Peter Korsgaard753dcfe2008-10-24 09:08:27 +0200413 return -EINVAL;
Wu Fengguangf52deb02009-01-08 10:47:01 -0800414 }
Peter Korsgaard753dcfe2008-10-24 09:08:27 +0200415
416 memcpy(net->dev_addr, addr->sa_data, net->addr_len);
Wu Fengguang4b9f8ec2009-01-06 18:52:58 +0000417 __dm9601_set_mac_address(dev);
Peter Korsgaard753dcfe2008-10-24 09:08:27 +0200418
419 return 0;
420}
421
Stephen Hemmingerfe85ff82009-03-20 19:35:56 +0000422static const struct net_device_ops dm9601_netdev_ops = {
423 .ndo_open = usbnet_open,
424 .ndo_stop = usbnet_stop,
425 .ndo_start_xmit = usbnet_start_xmit,
426 .ndo_tx_timeout = usbnet_tx_timeout,
427 .ndo_change_mtu = usbnet_change_mtu,
428 .ndo_validate_addr = eth_validate_addr,
429 .ndo_do_ioctl = dm9601_ioctl,
430 .ndo_set_multicast_list = dm9601_set_multicast,
431 .ndo_set_mac_address = dm9601_set_mac_address,
432};
433
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100434static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
435{
436 int ret;
Wu Fengguang20f10aa2009-01-06 10:55:10 -0800437 u8 mac[ETH_ALEN];
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100438
439 ret = usbnet_get_endpoints(dev, intf);
440 if (ret)
441 goto out;
442
Stephen Hemmingerfe85ff82009-03-20 19:35:56 +0000443 dev->net->netdev_ops = &dm9601_netdev_ops;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100444 dev->net->ethtool_ops = &dm9601_ethtool_ops;
445 dev->net->hard_header_len += DM_TX_OVERHEAD;
446 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Peter Korsgaardf662fe52007-10-01 13:36:07 +0200447 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100448
449 dev->mii.dev = dev->net;
450 dev->mii.mdio_read = dm9601_mdio_read;
451 dev->mii.mdio_write = dm9601_mdio_write;
452 dev->mii.phy_id_mask = 0x1f;
453 dev->mii.reg_num_mask = 0x1f;
454
455 /* reset */
Peter Korsgaardb8f595862007-06-30 23:07:42 +0200456 dm_write_reg(dev, DM_NET_CTRL, 1);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100457 udelay(20);
458
459 /* read MAC */
Wu Fengguang20f10aa2009-01-06 10:55:10 -0800460 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) {
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100461 printk(KERN_ERR "Error reading MAC address\n");
462 ret = -ENODEV;
463 goto out;
464 }
465
Wu Fengguang20f10aa2009-01-06 10:55:10 -0800466 /*
467 * Overwrite the auto-generated address only with good ones.
468 */
469 if (is_valid_ether_addr(mac))
470 memcpy(dev->net->dev_addr, mac, ETH_ALEN);
Wu Fengguangf52deb02009-01-08 10:47:01 -0800471 else {
472 printk(KERN_WARNING
473 "dm9601: No valid MAC address in EEPROM, using %pM\n",
474 dev->net->dev_addr);
Wu Fengguang4b9f8ec2009-01-06 18:52:58 +0000475 __dm9601_set_mac_address(dev);
Wu Fengguangf52deb02009-01-08 10:47:01 -0800476 }
Wu Fengguang20f10aa2009-01-06 10:55:10 -0800477
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100478 /* power up phy */
479 dm_write_reg(dev, DM_GPR_CTRL, 1);
480 dm_write_reg(dev, DM_GPR_DATA, 0);
481
482 /* receive broadcast packets */
483 dm9601_set_multicast(dev->net);
484
485 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
486 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
487 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
488 mii_nway_restart(&dev->mii);
489
490out:
491 return ret;
492}
493
494static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
495{
496 u8 status;
497 int len;
498
499 /* format:
Peter Korsgaard61189c72009-05-13 10:09:53 +0000500 b1: rx status
501 b2: packet length (incl crc) low
502 b3: packet length (incl crc) high
503 b4..n-4: packet data
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100504 bn-3..bn: ethernet crc
505 */
506
507 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
508 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
509 return 0;
510 }
511
512 status = skb->data[0];
513 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
514
515 if (unlikely(status & 0xbf)) {
516 if (status & 0x01) dev->stats.rx_fifo_errors++;
517 if (status & 0x02) dev->stats.rx_crc_errors++;
518 if (status & 0x04) dev->stats.rx_frame_errors++;
519 if (status & 0x20) dev->stats.rx_missed_errors++;
520 if (status & 0x90) dev->stats.rx_length_errors++;
521 return 0;
522 }
523
524 skb_pull(skb, 3);
525 skb_trim(skb, len);
526
527 return 1;
528}
529
530static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
531 gfp_t flags)
532{
533 int len;
534
535 /* format:
Peter Korsgaard61189c72009-05-13 10:09:53 +0000536 b1: packet length low
537 b2: packet length high
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100538 b3..n: packet data
539 */
540
Peter Korsgaard23de5592007-06-27 08:14:50 +0200541 len = skb->len;
542
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100543 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
544 struct sk_buff *skb2;
545
546 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
547 dev_kfree_skb_any(skb);
548 skb = skb2;
549 if (!skb)
550 return NULL;
551 }
552
553 __skb_push(skb, DM_TX_OVERHEAD);
554
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100555 /* usbnet adds padding if length is a multiple of packet size
556 if so, adjust length value in header */
Peter Korsgaard23de5592007-06-27 08:14:50 +0200557 if ((skb->len % dev->maxpacket) == 0)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100558 len++;
559
560 skb->data[0] = len;
561 skb->data[1] = len >> 8;
562
563 return skb;
564}
565
566static void dm9601_status(struct usbnet *dev, struct urb *urb)
567{
568 int link;
569 u8 *buf;
570
571 /* format:
572 b0: net status
573 b1: tx status 1
574 b2: tx status 2
575 b3: rx status
576 b4: rx overflow
577 b5: rx count
578 b6: tx count
579 b7: gpr
580 */
581
582 if (urb->actual_length < 8)
583 return;
584
585 buf = urb->transfer_buffer;
586
587 link = !!(buf[0] & 0x40);
588 if (netif_carrier_ok(dev->net) != link) {
589 if (link) {
590 netif_carrier_on(dev->net);
591 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
592 }
593 else
594 netif_carrier_off(dev->net);
595 devdbg(dev, "Link Status is: %d", link);
596 }
597}
598
599static int dm9601_link_reset(struct usbnet *dev)
600{
601 struct ethtool_cmd ecmd;
602
603 mii_check_media(&dev->mii, 1, 1);
604 mii_ethtool_gset(&dev->mii, &ecmd);
605
606 devdbg(dev, "link_reset() speed: %d duplex: %d",
607 ecmd.speed, ecmd.duplex);
608
609 return 0;
610}
611
612static const struct driver_info dm9601_info = {
613 .description = "Davicom DM9601 USB Ethernet",
614 .flags = FLAG_ETHER,
615 .bind = dm9601_bind,
616 .rx_fixup = dm9601_rx_fixup,
617 .tx_fixup = dm9601_tx_fixup,
618 .status = dm9601_status,
619 .link_reset = dm9601_link_reset,
620 .reset = dm9601_link_reset,
621};
622
623static const struct usb_device_id products[] = {
624 {
YOSHIFUJI Hideaki6438ac22007-03-02 20:33:55 -0500625 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
626 .driver_info = (unsigned long)&dm9601_info,
627 },
628 {
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100629 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
630 .driver_info = (unsigned long)&dm9601_info,
631 },
Jon Dowland6dc477f2007-03-11 20:03:03 +0000632 {
633 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
634 .driver_info = (unsigned long)&dm9601_info,
635 },
636 {
637 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
638 .driver_info = (unsigned long)&dm9601_info,
639 },
Peter Korsgaarda06da752007-10-30 14:23:47 +0100640 {
641 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
642 .driver_info = (unsigned long)&dm9601_info,
643 },
Peter Korsgaardb47b4b22008-03-18 23:16:53 +0100644 {
645 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
646 .driver_info = (unsigned long)&dm9601_info,
647 },
Peter Korsgaarda1a69c82009-03-03 23:48:16 -0800648 {
649 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
650 .driver_info = (unsigned long)&dm9601_info,
651 },
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100652 {}, // END
653};
654
655MODULE_DEVICE_TABLE(usb, products);
656
657static struct usb_driver dm9601_driver = {
658 .name = "dm9601",
659 .id_table = products,
660 .probe = usbnet_probe,
661 .disconnect = usbnet_disconnect,
662 .suspend = usbnet_suspend,
663 .resume = usbnet_resume,
664};
665
666static int __init dm9601_init(void)
667{
668 return usb_register(&dm9601_driver);
669}
670
671static void __exit dm9601_exit(void)
672{
673 usb_deregister(&dm9601_driver);
674}
675
676module_init(dm9601_init);
677module_exit(dm9601_exit);
678
679MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
680MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
681MODULE_LICENSE("GPL");