1 #include <linux/module.h>
2 #include <linux/netdevice.h>
5 #include <linux/usb/cdc.h>
6 #include <linux/usb/usbnet.h>
8 #define RTL815x_REQT_READ 0xc0
9 #define RTL815x_REQT_WRITE 0x40
10 #define RTL815x_REQ_GET_REGS 0x05
11 #define RTL815x_REQ_SET_REGS 0x05
13 #define MCU_TYPE_PLA 0x0100
14 #define OCP_BASE 0xe86c
15 #define BASE_MII 0xa400
17 #define BYTE_EN_DWORD 0xff
18 #define BYTE_EN_WORD 0x33
19 #define BYTE_EN_BYTE 0x11
21 #define R815x_PHY_ID 32
22 #define REALTEK_VENDOR_ID 0x0bda
25 static int pla_read_word(struct usb_device *udev, u16 index)
31 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
37 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
38 RTL815x_REQ_GET_REGS, RTL815x_REQT_READ,
39 index, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
43 ret = __le32_to_cpu(*tmp);
52 static int pla_write_word(struct usb_device *udev, u16 index, u32 data)
56 u16 byen = BYTE_EN_WORD;
60 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
73 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
74 RTL815x_REQ_GET_REGS, RTL815x_REQT_READ,
75 index, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
79 data |= __le32_to_cpu(*tmp) & ~mask;
80 *tmp = __cpu_to_le32(data);
82 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
83 RTL815x_REQ_SET_REGS, RTL815x_REQT_WRITE,
84 index, MCU_TYPE_PLA | byen, tmp, sizeof(*tmp),
92 static int ocp_reg_read(struct usbnet *dev, u16 addr)
94 u16 ocp_base, ocp_index;
97 ocp_base = addr & 0xf000;
98 ret = pla_write_word(dev->udev, OCP_BASE, ocp_base);
102 ocp_index = (addr & 0x0fff) | 0xb000;
103 ret = pla_read_word(dev->udev, ocp_index);
109 static int ocp_reg_write(struct usbnet *dev, u16 addr, u16 data)
111 u16 ocp_base, ocp_index;
114 ocp_base = addr & 0xf000;
115 ret = pla_write_word(dev->udev, OCP_BASE, ocp_base);
119 ocp_index = (addr & 0x0fff) | 0xb000;
120 ret = pla_write_word(dev->udev, ocp_index, data);
126 static int r815x_mdio_read(struct net_device *netdev, int phy_id, int reg)
128 struct usbnet *dev = netdev_priv(netdev);
131 if (phy_id != R815x_PHY_ID)
134 if (usb_autopm_get_interface(dev->intf) < 0)
137 ret = ocp_reg_read(dev, BASE_MII + reg * 2);
139 usb_autopm_put_interface(dev->intf);
144 void r815x_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
146 struct usbnet *dev = netdev_priv(netdev);
148 if (phy_id != R815x_PHY_ID)
151 if (usb_autopm_get_interface(dev->intf) < 0)
154 ocp_reg_write(dev, BASE_MII + reg * 2, val);
156 usb_autopm_put_interface(dev->intf);
159 static int r8153_bind(struct usbnet *dev, struct usb_interface *intf)
163 status = usbnet_cdc_bind(dev, intf);
167 dev->mii.dev = dev->net;
168 dev->mii.mdio_read = r815x_mdio_read;
169 dev->mii.mdio_write = r815x_mdio_write;
170 dev->mii.phy_id_mask = 0x3f;
171 dev->mii.reg_num_mask = 0x1f;
172 dev->mii.phy_id = R815x_PHY_ID;
173 dev->mii.supports_gmii = 1;
178 static int r8152_bind(struct usbnet *dev, struct usb_interface *intf)
182 status = usbnet_cdc_bind(dev, intf);
186 dev->mii.dev = dev->net;
187 dev->mii.mdio_read = r815x_mdio_read;
188 dev->mii.mdio_write = r815x_mdio_write;
189 dev->mii.phy_id_mask = 0x3f;
190 dev->mii.reg_num_mask = 0x1f;
191 dev->mii.phy_id = R815x_PHY_ID;
192 dev->mii.supports_gmii = 0;
197 static const struct driver_info r8152_info = {
198 .description = "RTL8152 ECM Device",
199 .flags = FLAG_ETHER | FLAG_POINTTOPOINT,
201 .unbind = usbnet_cdc_unbind,
202 .status = usbnet_cdc_status,
203 .manage_power = usbnet_manage_power,
206 static const struct driver_info r8153_info = {
207 .description = "RTL8153 ECM Device",
208 .flags = FLAG_ETHER | FLAG_POINTTOPOINT,
210 .unbind = usbnet_cdc_unbind,
211 .status = usbnet_cdc_status,
212 .manage_power = usbnet_manage_power,
215 static const struct usb_device_id products[] = {
217 USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID, 0x8152, USB_CLASS_COMM,
218 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
219 #if defined(CONFIG_USB_RTL8152) || defined(CONFIG_USB_RTL8152_MODULE)
222 .driver_info = (unsigned long) &r8152_info,
227 USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID, 0x8153, USB_CLASS_COMM,
228 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
229 #if defined(CONFIG_USB_RTL8153) || defined(CONFIG_USB_RTL8153_MODULE)
232 .driver_info = (unsigned long) &r8153_info,
238 MODULE_DEVICE_TABLE(usb, products);
240 static struct usb_driver r815x_driver = {
242 .id_table = products,
243 .probe = usbnet_probe,
244 .disconnect = usbnet_disconnect,
245 .suspend = usbnet_suspend,
246 .resume = usbnet_resume,
247 .reset_resume = usbnet_resume,
248 .supports_autosuspend = 1,
249 .disable_hub_initiated_lpm = 1,
252 module_usb_driver(r815x_driver);
254 MODULE_AUTHOR("Hayes Wang");
255 MODULE_DESCRIPTION("Realtek USB ECM device");
256 MODULE_LICENSE("GPL");