/* CoreChip-sz SR9800 one chip USB 2.0 Ethernet Devices
*
* Author : Liu Junliang <liujunliang_ljl@163.com>
*
* Based on asix_common.c, asix_devices.c
*
* This file is licensed under the terms of the GNU General Public License
* version 2. This program is licensed "as is" without any warranty of any
* kind, whether express or implied.*
*/
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/workqueue.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/crc32.h>
#include <linux/usb/usbnet.h>
#include <linux/slab.h>
#include <linux/if_vlan.h>
#include "sr9800.h"
static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
u16 size, void *data)
{
int err;
err = usbnet_read_cmd(dev, cmd, SR_REQ_RD_REG, value, index,
data, size);
if ((err != size) && (err >= 0))
err = -EINVAL;
return err;
}
static int sr_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
u16 size, void *data)
{
int err;
err = usbnet_write_cmd(dev, cmd, SR_REQ_WR_REG, value, index,
data, size);
if ((err != size) && (err >= 0))
err = -EINVAL;
return err;
}
static void
sr_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
u16 size, void *data)
{
usbnet_write_cmd_async(dev, cmd, SR_REQ_WR_REG, value, index, data,
size);
}
static int sr_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
int offset = 0;
/* This check is no longer done by usbnet */
if (skb->len < dev->net->hard_header_len)
return 0;
while (offset + sizeof(u32) < skb->len) {
struct sk_buff *sr_skb;
u16 size;
u32 header = get_unaligned_le32(skb->data + offset);
offset += sizeof(u32);
/* get the packet length */
size = (u16) (header & 0x7ff);
if (size != ((~header >> 16) & 0x07ff)) {
netdev_err(dev->net, "%s : Bad Header Length\n",
__func__);
return 0;
}
if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
(size + offset > skb->len)) {
netdev_err(dev->net, "%s : Bad RX Length %d\n",
__func__, size);
return 0;
}
sr_skb = netdev_alloc_skb_ip_align(dev->net, size);
if (!sr_skb)
return 0;
skb_put(sr_skb, size);
memcpy(sr_skb->data, skb->data + offset, size);
usbnet_skb_return(dev, sr_skb);
offset += (size + 1) & 0xfffe;
}
if (skb->len != offset) {
netdev_err(dev->net, "%s : Bad SKB Length %d\n", __func__,
skb->len);
return 0;
}
return 1;
}
static struct sk_buff *sr_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
gfp_t flags)
{
int headroom = skb_headroom(skb);
int tailroom = skb_tailroom(skb);
u32 padbytes = 0xffff0000;
u32 packet_len;
int padlen;
padlen = ((skb->len + 4) % (dev->maxpacket - 1)) ? 0 : 4;
if ((!skb_cloned(skb)) && ((headroom + tailroom) >= (4 + padlen))) {
if ((headroom < 4) || (tailroom < padlen)) {
skb->data = memmove(skb->head + 4, skb->data,
skb->len);
skb_set_tail_pointer(skb, skb->len);
}
} else {
struct sk_buff *skb2;
skb2 = skb_copy_expand(skb, 4, padlen, flags);
dev_kfree_skb_any(skb);
skb = skb2;
if (!skb)
return NULL;
}
skb_push(skb, 4);
packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
cpu_to_le32s(&packet_len);
skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
if (padlen) {
cpu_to_le32s(&padbytes);
memcpy(