asix_common.c 13.5 KB
Newer Older
1 2
/*
 * ASIX AX8817X based USB 2.0 Ethernet Devices
3
 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4
 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5
 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (c) 2002-2003 TiVo Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

C
Christian Riesch 已提交
23 24 25 26
#include "asix.h"

int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
		  u16 size, void *data)
27
{
A
Al Viro 已提交
28 29 30
	void *buf;
	int err = -ENOMEM;

31 32
	netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
		   cmd, value, index, size);
A
Al Viro 已提交
33 34 35 36 37 38

	buf = kmalloc(size, GFP_KERNEL);
	if (!buf)
		goto out;

	err = usb_control_msg(
39 40 41 42 43 44
		dev->udev,
		usb_rcvctrlpipe(dev->udev, 0),
		cmd,
		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		index,
A
Al Viro 已提交
45
		buf,
46 47
		size,
		USB_CTRL_GET_TIMEOUT);
R
Russ Dill 已提交
48
	if (err == size)
A
Al Viro 已提交
49
		memcpy(data, buf, size);
R
Russ Dill 已提交
50 51
	else if (err >= 0)
		err = -EINVAL;
A
Al Viro 已提交
52 53 54 55
	kfree(buf);

out:
	return err;
56 57
}

C
Christian Riesch 已提交
58 59
int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
		   u16 size, void *data)
60
{
A
Al Viro 已提交
61 62 63
	void *buf = NULL;
	int err = -ENOMEM;

64 65
	netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
		   cmd, value, index, size);
A
Al Viro 已提交
66 67

	if (data) {
J
Julia Lawall 已提交
68
		buf = kmemdup(data, size, GFP_KERNEL);
A
Al Viro 已提交
69 70 71 72 73
		if (!buf)
			goto out;
	}

	err = usb_control_msg(
74 75 76 77 78 79
		dev->udev,
		usb_sndctrlpipe(dev->udev, 0),
		cmd,
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		index,
A
Al Viro 已提交
80
		buf,
81 82
		size,
		USB_CTRL_SET_TIMEOUT);
A
Al Viro 已提交
83 84 85 86
	kfree(buf);

out:
	return err;
87 88
}

89
static void asix_async_cmd_callback(struct urb *urb)
90 91
{
	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
92
	int status = urb->status;
93

94
	if (status < 0)
95
		printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
96
			status);
97 98 99 100 101

	kfree(req);
	usb_free_urb(urb);
}

C
Christian Riesch 已提交
102 103
void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
			  u16 size, void *data)
104 105 106 107 108
{
	struct usb_ctrlrequest *req;
	int status;
	struct urb *urb;

109 110
	netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
		   cmd, value, index, size);
111 112 113

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb) {
114
		netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
115 116 117
		return;
	}

118 119
	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
	if (!req) {
120
		netdev_err(dev->net, "Failed to allocate memory for control request\n");
121 122 123 124 125 126
		usb_free_urb(urb);
		return;
	}

	req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
	req->bRequest = cmd;
O
Oliver Neukum 已提交
127 128 129
	req->wValue = cpu_to_le16(value);
	req->wIndex = cpu_to_le16(index);
	req->wLength = cpu_to_le16(size);
130 131 132 133 134 135

	usb_fill_control_urb(urb, dev->udev,
			     usb_sndctrlpipe(dev->udev, 0),
			     (void *)req, data, size,
			     asix_async_cmd_callback, req);

136 137
	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status < 0) {
138 139
		netdev_err(dev->net, "Error submitting the control message: status=%d\n",
			   status);
140 141 142 143 144
		kfree(req);
		usb_free_urb(urb);
	}
}

C
Christian Riesch 已提交
145
int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
146
{
147
	int offset = 0;
148

149 150 151 152
	while (offset + sizeof(u32) < skb->len) {
		struct sk_buff *ax_skb;
		u16 size;
		u32 header = get_unaligned_le32(skb->data + offset);
153

154
		offset += sizeof(u32);
155

156
		/* get the packet length */
157 158 159 160
		size = (u16) (header & 0x7ff);
		if (size != ((~header >> 16) & 0x07ff)) {
			netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
			return 0;
161 162
		}

163
		if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
164
		    (size + offset > skb->len)) {
165 166
			netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
				   size);
167 168
			return 0;
		}
169 170
		ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
		if (!ax_skb)
171 172
			return 0;

173 174 175
		skb_put(ax_skb, size);
		memcpy(ax_skb->data, skb->data + offset, size);
		usbnet_skb_return(dev, ax_skb);
176

177
		offset += (size + 1) & 0xfffe;
178 179
	}

180
	if (skb->len != offset) {
181 182
		netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
			   skb->len);
183 184 185 186 187
		return 0;
	}
	return 1;
}

C
Christian Riesch 已提交
188 189
struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
			      gfp_t flags)
190 191 192 193 194 195 196
{
	int padlen;
	int headroom = skb_headroom(skb);
	int tailroom = skb_tailroom(skb);
	u32 packet_len;
	u32 padbytes = 0xffff0000;

197
	padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
198

E
Eric Dumazet 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/* We need to push 4 bytes in front of frame (packet_len)
	 * and maybe add 4 bytes after the end (if padlen is 4)
	 *
	 * Avoid skb_copy_expand() expensive call, using following rules :
	 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
	 *   is false (and if we have 4 bytes of headroom)
	 * - We are allowed to put 4 bytes at tail if skb_cloned()
	 *   is false (and if we have 4 bytes of tailroom)
	 *
	 * TCP packets for example are cloned, but skb_header_release()
	 * was called in tcp stack, allowing us to use headroom for our needs.
	 */
	if (!skb_header_cloned(skb) &&
	    !(padlen && skb_cloned(skb)) &&
	    headroom + tailroom >= 4 + padlen) {
		/* following should not happen, but better be safe */
		if (headroom < 4 ||
		    tailroom < padlen) {
217
			skb->data = memmove(skb->head + 4, skb->data, skb->len);
218
			skb_set_tail_pointer(skb, skb->len);
219 220 221
		}
	} else {
		struct sk_buff *skb2;
E
Eric Dumazet 已提交
222

223 224 225 226 227 228 229
		skb2 = skb_copy_expand(skb, 4, padlen, flags);
		dev_kfree_skb_any(skb);
		skb = skb2;
		if (!skb)
			return NULL;
	}

E
Eric Dumazet 已提交
230
	packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
231
	skb_push(skb, 4);
232
	cpu_to_le32s(&packet_len);
233
	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
234

235
	if (padlen) {
236
		cpu_to_le32s(&padbytes);
237
		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
238 239 240 241 242
		skb_put(skb, sizeof(padbytes));
	}
	return skb;
}

C
Christian Riesch 已提交
243
int asix_set_sw_mii(struct usbnet *dev)
244 245 246 247
{
	int ret;
	ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
	if (ret < 0)
248
		netdev_err(dev->net, "Failed to enable software MII access\n");
249 250 251
	return ret;
}

C
Christian Riesch 已提交
252
int asix_set_hw_mii(struct usbnet *dev)
253 254 255 256
{
	int ret;
	ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
	if (ret < 0)
257
		netdev_err(dev->net, "Failed to enable hardware MII access\n");
258 259 260
	return ret;
}

261
int asix_read_phy_addr(struct usbnet *dev, int internal)
262
{
263
	int offset = (internal ? 1 : 0);
A
Al Viro 已提交
264 265
	u8 buf[2];
	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
266

267
	netdev_dbg(dev->net, "asix_get_phy_addr()\n");
268

A
Al Viro 已提交
269
	if (ret < 0) {
270
		netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
A
Al Viro 已提交
271
		goto out;
272
	}
273 274
	netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
		   *((__le16 *)buf));
275
	ret = buf[offset];
A
Al Viro 已提交
276 277

out:
278 279 280
	return ret;
}

281 282 283 284 285 286 287
int asix_get_phy_addr(struct usbnet *dev)
{
	/* return the address of the internal phy */
	return asix_read_phy_addr(dev, 1);
}


C
Christian Riesch 已提交
288
int asix_sw_reset(struct usbnet *dev, u8 flags)
289 290 291 292 293
{
	int ret;

        ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
	if (ret < 0)
294
		netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
295 296 297

	return ret;
}
298

C
Christian Riesch 已提交
299
u16 asix_read_rx_ctl(struct usbnet *dev)
300
{
A
Al Viro 已提交
301 302
	__le16 v;
	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
303

A
Al Viro 已提交
304
	if (ret < 0) {
305
		netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
A
Al Viro 已提交
306
		goto out;
307
	}
A
Al Viro 已提交
308 309
	ret = le16_to_cpu(v);
out:
310 311 312
	return ret;
}

C
Christian Riesch 已提交
313
int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
314 315 316
{
	int ret;

317
	netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
318 319
	ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
	if (ret < 0)
320 321
		netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
			   mode, ret);
322 323 324 325

	return ret;
}

C
Christian Riesch 已提交
326
u16 asix_read_medium_status(struct usbnet *dev)
327
{
A
Al Viro 已提交
328 329
	__le16 v;
	int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
330

A
Al Viro 已提交
331
	if (ret < 0) {
332 333
		netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
			   ret);
334
		return ret;	/* TODO: callers not checking for error ret */
335
	}
336 337 338

	return le16_to_cpu(v);

339 340
}

C
Christian Riesch 已提交
341
int asix_write_medium_mode(struct usbnet *dev, u16 mode)
342
{
343
	int ret;
344

345
	netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
346 347
	ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
	if (ret < 0)
348 349
		netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
			   mode, ret);
350

351 352
	return ret;
}
353

C
Christian Riesch 已提交
354
int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
355 356
{
	int ret;
357

358
	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
359 360
	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
	if (ret < 0)
361 362
		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
			   value, ret);
363

364 365 366 367
	if (sleep)
		msleep(sleep);

	return ret;
368 369
}

370 371 372
/*
 * AX88772 & AX88178 have a 16-bit RX_CTL value
 */
C
Christian Riesch 已提交
373
void asix_set_multicast(struct net_device *net)
374 375
{
	struct usbnet *dev = netdev_priv(net);
376
	struct asix_data *data = (struct asix_data *)&dev->data;
377
	u16 rx_ctl = AX_DEFAULT_RX_CTL;
378 379

	if (net->flags & IFF_PROMISC) {
380
		rx_ctl |= AX_RX_CTL_PRO;
381
	} else if (net->flags & IFF_ALLMULTI ||
382
		   netdev_mc_count(net) > AX_MAX_MCAST) {
383
		rx_ctl |= AX_RX_CTL_AMALL;
384
	} else if (netdev_mc_empty(net)) {
385 386 387 388 389 390
		/* just broadcast and directed */
	} else {
		/* We use the 20 byte dev->data
		 * for our 8 byte filter buffer
		 * to avoid allocating memory that
		 * is tricky to free later */
391
		struct netdev_hw_addr *ha;
392 393 394 395 396
		u32 crc_bits;

		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);

		/* Build the multicast hash filter. */
397 398
		netdev_for_each_mc_addr(ha, net) {
			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
399 400 401 402
			data->multi_filter[crc_bits >> 3] |=
			    1 << (crc_bits & 7);
		}

403
		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
404 405
				   AX_MCAST_FILTER_SIZE, data->multi_filter);

406
		rx_ctl |= AX_RX_CTL_AM;
407 408
	}

409
	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
410 411
}

C
Christian Riesch 已提交
412
int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
413 414
{
	struct usbnet *dev = netdev_priv(netdev);
A
Al Viro 已提交
415
	__le16 res;
416

417
	mutex_lock(&dev->phy_mutex);
418 419
	asix_set_sw_mii(dev);
	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
A
Al Viro 已提交
420
				(__u16)loc, 2, &res);
421
	asix_set_hw_mii(dev);
422
	mutex_unlock(&dev->phy_mutex);
423

424 425
	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
		   phy_id, loc, le16_to_cpu(res));
426

A
Al Viro 已提交
427
	return le16_to_cpu(res);
428 429
}

C
Christian Riesch 已提交
430
void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
431 432
{
	struct usbnet *dev = netdev_priv(netdev);
A
Al Viro 已提交
433
	__le16 res = cpu_to_le16(val);
434

435 436
	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
		   phy_id, loc, val);
437
	mutex_lock(&dev->phy_mutex);
438
	asix_set_sw_mii(dev);
A
Al Viro 已提交
439
	asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
440
	asix_set_hw_mii(dev);
441
	mutex_unlock(&dev->phy_mutex);
442 443
}

C
Christian Riesch 已提交
444
void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
445 446 447 448
{
	struct usbnet *dev = netdev_priv(net);
	u8 opt;

449
	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
450 451 452 453 454 455
		wolinfo->supported = 0;
		wolinfo->wolopts = 0;
		return;
	}
	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
	wolinfo->wolopts = 0;
456 457 458 459
	if (opt & AX_MONITOR_LINK)
		wolinfo->wolopts |= WAKE_PHY;
	if (opt & AX_MONITOR_MAGIC)
		wolinfo->wolopts |= WAKE_MAGIC;
460 461
}

C
Christian Riesch 已提交
462
int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
463 464 465 466 467 468 469 470 471
{
	struct usbnet *dev = netdev_priv(net);
	u8 opt = 0;

	if (wolinfo->wolopts & WAKE_PHY)
		opt |= AX_MONITOR_LINK;
	if (wolinfo->wolopts & WAKE_MAGIC)
		opt |= AX_MONITOR_MAGIC;

472
	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
A
Al Viro 已提交
473
			      opt, 0, 0, NULL) < 0)
474 475 476 477 478
		return -EINVAL;

	return 0;
}

C
Christian Riesch 已提交
479
int asix_get_eeprom_len(struct net_device *net)
480
{
481
	return AX_EEPROM_LEN;
482 483
}

C
Christian Riesch 已提交
484 485
int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
		    u8 *data)
486 487
{
	struct usbnet *dev = netdev_priv(net);
488 489
	u16 *eeprom_buff;
	int first_word, last_word;
490 491
	int i;

492
	if (eeprom->len == 0)
493 494 495 496
		return -EINVAL;

	eeprom->magic = AX_EEPROM_MAGIC;

497 498 499 500 501 502 503 504
	first_word = eeprom->offset >> 1;
	last_word = (eeprom->offset + eeprom->len - 1) >> 1;

	eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
			      GFP_KERNEL);
	if (!eeprom_buff)
		return -ENOMEM;

505
	/* ax8817x returns 2 bytes from eeprom on read */
506 507 508 509 510 511
	for (i = first_word; i <= last_word; i++) {
		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
				  &(eeprom_buff[i - first_word])) < 0) {
			kfree(eeprom_buff);
			return -EIO;
		}
512
	}
513 514 515

	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
	kfree(eeprom_buff);
516 517 518
	return 0;
}

C
Christian Riesch 已提交
519
void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
520 521 522
{
	/* Inherit standard device info */
	usbnet_get_drvinfo(net, info);
523
	strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
524
	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
525
	info->eedump_len = AX_EEPROM_LEN;
526 527
}

C
Christian Riesch 已提交
528
int asix_set_mac_address(struct net_device *net, void *p)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
{
	struct usbnet *dev = netdev_priv(net);
	struct asix_data *data = (struct asix_data *)&dev->data;
	struct sockaddr *addr = p;

	if (netif_running(net))
		return -EBUSY;
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);

	/* We use the 20 byte dev->data
	 * for our 6 byte mac buffer
	 * to avoid allocating memory that
	 * is tricky to free later */
	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
							data->mac_addr);

	return 0;
}