/*
* QF9700 one chip USB 1.1 ethernet devices
*
* Author : jokeliujl <jokeliu@163.com>
* Date : 2010-10-01
*
* 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.
*/
//#define DEBUG
#include <linux/module.h>
#include <asm/delay.h>
#include <linux/sched.h>
#include <linux/stddef.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/crc32.h>
#include <linux/usb/usbnet.h>
#include "qf9700.h"
/* ------------------------------------------------------------------------------------------ */
/* qf9700 mac and phy operations */
/* qf9700 read some registers from MAC */
static int qf_read(struct usbnet *dev, u8 reg, u16 length, void *data)
{
void *buf;
int err = -ENOMEM;
devdbg(dev, "qf_read() reg=0x%02x length=%d", reg, length);
buf = kmalloc(length, GFP_KERNEL);
if (!buf)
goto out;
err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
QF_RD_REGS, QF_REQ_RD_REG,
0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
if (err == length)
memcpy(data, buf, length);
else if (err >= 0)
err = -EINVAL;
kfree(buf);
out:
return err;
}
/* qf9700 write some registers to MAC */
static int qf_write(struct usbnet *dev, u8 reg, u16 length, void *data)
{
void *buf = NULL;
int err = -ENOMEM;
devdbg(dev, "qf_write() reg=0x%02x, length=%d", reg, length);
if (data) {
buf = kmalloc(length, GFP_KERNEL);
if (!buf)
goto out;
memcpy(buf, data, length);
}
err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
QF_WR_REGS, QF_REQ_WR_REG,
0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
kfree(buf);
if (err >= 0 && err < length)
err = -EINVAL;
out:
return err;
}
/* qf9700 read one register from MAC */
static int qf_read_reg(struct usbnet *dev, u8 reg, u8 *value)
{
return qf_read(dev, reg, 1, value);
}
/* qf9700 write one register to MAC */
static int qf_write_reg(struct usbnet *dev, u8 reg, u8 value)
{
devdbg(dev, "qf_write_reg() reg=0x%02x, value=0x%02x", reg, value);
return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
QF_WR_REG, QF_REQ_WR_REG,
value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
}
/* async mode for writing registers or reg blocks */
static void qf_write_async_callback(struct urb *urb)
{
struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
if (urb->status < 0)
printk(KERN_DEBUG "qf_write_async_callback() failed with %d\n", urb->status);
kfree(req);
usb_free_urb(urb);
}
static void qf_write_async_helper(struct usbnet *dev, u8 reg, u8 value, u16 length, void *data)
{
struct usb_ctrlrequest *req;
struct urb *urb;
int status;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb) {
deverr(dev, "Error allocating URB in qf_write_async_helper!");
return;
}
req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
if (!req) {
deverr(dev, "Failed to allocate memory for control request");
usb_free_urb(urb);
return;
}
req->bRequestType = QF_REQ_WR_REG;
req->bRequest = length ? QF_WR_REGS : QF_WR_REG;
req->wValue = cpu_to_le16(value);
req->wIndex = cpu_to_le16(reg);
req->wLength = cpu_to_le16(length);
usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
(void *)req, data, length,
qf_write_async_callback, req);
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status < 0) {
deverr(dev, "Error submitting the control message: status=%d",
status);
kfree(req);
usb_free_urb(urb);
}
return;
}
static void qf_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
{
devdbg(dev, "qf_write_async() reg=0x%02x length=%d", reg, length);
qf_write_async_helper(dev, reg, 0, length, data);
}
static void qf_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
{
devdbg(dev, "qf_write_reg_async() reg=0x%02x value=0x%02x", reg, value);
qf_write_async_helper(dev, reg, value, 0, NULL);
}
/* qf9700 read one word from phy or eeprom */
static int qf_share_read_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
{
int ret, i;
mutex_lock(&dev->phy_mutex);
qf_write_reg(dev, EPAR, phy ? (reg | 0x40) : reg);
qf_write_reg(dev, EPCR, phy ? 0xc : 0x4);
for (i = 0; i < QF_SHARE_TIMEOUT; i++) {
u8 tmp;
udelay(1);
ret = qf_read_reg(dev, EPCR, &tmp);
if (ret < 0)
goto out;
/* ready */
if ((tmp & 1) == 0)
break;
}
if (i >= QF_SHARE_TIMEOUT) {
deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
ret = -EIO;
goto out;
}
qf_write_reg(dev, EPCR, 0x0);
ret = qf_read(dev, EPDR, 2, value);
devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
phy, reg, *value, ret);
out:
mutex_unlock(&dev->phy_mutex);
return ret;
}
/* write one word to phy or eeprom */
static int qf_share_write_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
{
int ret, i;
mutex_lock(&dev->phy_mutex);
ret = qf_write(dev, EPDR, 2, &value);
if (ret < 0)
goto out;
qf_write_reg(dev, EPAR, phy ? (reg | 0x40) : reg);
qf_write_reg(dev, EPCR, phy ? 0x1a : 0x12);
for (i = 0; i < QF_SHARE_TIMEOUT; i++) {
u8 tmp;
udelay(1);
ret = qf_read_reg(dev, EPCR, &tmp);
if (ret < 0)
goto out;
/* ready */
if ((tmp & 1) == 0)
break;
}
if (i >= QF_SHARE_TIMEOUT) {
deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
ret = -EIO;
goto out;
}
qf_write_reg(dev, EPCR, 0x0);
out:
mutex_unlock(&dev->phy_mutex);
return ret;
}
static int qf_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
{
return qf_share_read_word(dev, 0, offset, value);
}
static int qf9700_get_eeprom_len(struct net_device *dev)
{
return QF_EEPROM_LEN;
}
/* get qf9700 eeprom information */
static int qf9700_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, u8 * data)
{
struct usbnet *dev = netdev_priv(net);
__le16 *ebuf = (__le16 *) data;
int i;
/* access is 16bit */
if ((eeprom->offset % 2) || (eeprom->len % 2))
return -EINVAL;
for (i = 0; i < eeprom->len / 2; i++) {
if (qf_read_eeprom_word(dev, eeprom->offset / 2 + i, &ebuf[i]) < 0)
return -EINVAL;
}
return 0;
}
/* qf9700 mii-phy register read by word */
static int qf9700_mdio_read(struct net_device *netdev, int phy_id, int loc)
{
struct usbnet *dev = netdev_priv(netdev);
__le16 res;
if (phy_id) {
devdbg(dev, "Only internal phy supported");
return 0;
}
qf_share_read_word(dev, 1, loc, &res);
devdbg(dev,
"qf9700_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
phy_id, loc, le16_to_cpu(res));
return le16_to_cpu(res);
}
/* qf9700 mii-phy register write by word */
static void qf9700_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
{
struct usbnet *dev = netdev_priv(netdev);
__le16 res = cpu_to_le16(val);
if (phy_id) {
devdbg(dev, "Only internal phy supported");
return;
}
devdbg(dev,"qf9700_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
phy_id, loc, val);
qf_share_write_word(dev, 1, loc, res);
}
/*-------------------------------------------------------------------------------------------*/
static void qf9700_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
{
/* Inherit standard device info */
usbnet_get_drvinfo(net, info);
info->eedump_len = QF_EEPROM_LEN;
}
static u32 qf9700_get_link(struct net_device *net)
{
struct usbnet *dev = netdev_priv(net);
return mii_link_ok(&dev->mii);
}
static int qf9700_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
{
struct usbnet *dev = netdev_priv(net);
return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
}
static struct ethtool_ops qf9700_ethtool_ops = {
.get_drvinfo = qf9700_get_drvinfo,
.get_link = qf9700_get_link,
.get_msglevel = usbnet_get_msglevel,
.set_msglevel = usbnet_set_msglevel,
.get_eeprom_len = qf9700_get_eeprom_len,
.get_eeprom = qf9700_