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
)
33 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
34 RTL815x_REQ_GET_REGS
, RTL815x_REQT_READ
,
35 index
, MCU_TYPE_PLA
, &ocp_data
, sizeof(ocp_data
),
40 data
= __le32_to_cpu(ocp_data
);
47 static int pla_write_word(struct usb_device
*udev
, u16 index
, u32 data
)
51 u16 byen
= BYTE_EN_WORD
;
64 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
65 RTL815x_REQ_GET_REGS
, RTL815x_REQT_READ
,
66 index
, MCU_TYPE_PLA
, &ocp_data
, sizeof(ocp_data
),
71 data
|= __le32_to_cpu(ocp_data
) & ~mask
;
72 ocp_data
= __cpu_to_le32(data
);
74 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
75 RTL815x_REQ_SET_REGS
, RTL815x_REQT_WRITE
,
76 index
, MCU_TYPE_PLA
| byen
, &ocp_data
,
77 sizeof(ocp_data
), 500);
82 static int ocp_reg_read(struct usbnet
*dev
, u16 addr
)
84 u16 ocp_base
, ocp_index
;
87 ocp_base
= addr
& 0xf000;
88 ret
= pla_write_word(dev
->udev
, OCP_BASE
, ocp_base
);
92 ocp_index
= (addr
& 0x0fff) | 0xb000;
93 ret
= pla_read_word(dev
->udev
, ocp_index
);
99 static int ocp_reg_write(struct usbnet
*dev
, u16 addr
, u16 data
)
101 u16 ocp_base
, ocp_index
;
104 ocp_base
= addr
& 0xf000;
105 ret
= pla_write_word(dev
->udev
, OCP_BASE
, ocp_base
);
109 ocp_index
= (addr
& 0x0fff) | 0xb000;
110 ret
= pla_write_word(dev
->udev
, ocp_index
, data
);
116 static int r815x_mdio_read(struct net_device
*netdev
, int phy_id
, int reg
)
118 struct usbnet
*dev
= netdev_priv(netdev
);
120 if (phy_id
!= R815x_PHY_ID
)
123 return ocp_reg_read(dev
, BASE_MII
+ reg
* 2);
127 void r815x_mdio_write(struct net_device
*netdev
, int phy_id
, int reg
, int val
)
129 struct usbnet
*dev
= netdev_priv(netdev
);
131 if (phy_id
!= R815x_PHY_ID
)
134 ocp_reg_write(dev
, BASE_MII
+ reg
* 2, val
);
137 static int r8153_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
141 status
= usbnet_cdc_bind(dev
, intf
);
145 dev
->mii
.dev
= dev
->net
;
146 dev
->mii
.mdio_read
= r815x_mdio_read
;
147 dev
->mii
.mdio_write
= r815x_mdio_write
;
148 dev
->mii
.phy_id_mask
= 0x3f;
149 dev
->mii
.reg_num_mask
= 0x1f;
150 dev
->mii
.phy_id
= R815x_PHY_ID
;
151 dev
->mii
.supports_gmii
= 1;
156 static int r8152_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
160 status
= usbnet_cdc_bind(dev
, intf
);
164 dev
->mii
.dev
= dev
->net
;
165 dev
->mii
.mdio_read
= r815x_mdio_read
;
166 dev
->mii
.mdio_write
= r815x_mdio_write
;
167 dev
->mii
.phy_id_mask
= 0x3f;
168 dev
->mii
.reg_num_mask
= 0x1f;
169 dev
->mii
.phy_id
= R815x_PHY_ID
;
170 dev
->mii
.supports_gmii
= 0;
175 static const struct driver_info r8152_info
= {
176 .description
= "RTL8152 ECM Device",
177 .flags
= FLAG_ETHER
| FLAG_POINTTOPOINT
,
179 .unbind
= usbnet_cdc_unbind
,
180 .status
= usbnet_cdc_status
,
181 .manage_power
= usbnet_manage_power
,
184 static const struct driver_info r8153_info
= {
185 .description
= "RTL8153 ECM Device",
186 .flags
= FLAG_ETHER
| FLAG_POINTTOPOINT
,
188 .unbind
= usbnet_cdc_unbind
,
189 .status
= usbnet_cdc_status
,
190 .manage_power
= usbnet_manage_power
,
193 static const struct usb_device_id products
[] = {
195 USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID
, 0x8152, USB_CLASS_COMM
,
196 USB_CDC_SUBCLASS_ETHERNET
, USB_CDC_PROTO_NONE
),
197 #if defined(CONFIG_USB_RTL8152) || defined(CONFIG_USB_RTL8152_MODULE)
200 .driver_info
= (unsigned long) &r8152_info
,
205 USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID
, 0x8153, USB_CLASS_COMM
,
206 USB_CDC_SUBCLASS_ETHERNET
, USB_CDC_PROTO_NONE
),
207 #if defined(CONFIG_USB_RTL8153) || defined(CONFIG_USB_RTL8153_MODULE)
210 .driver_info
= (unsigned long) &r8153_info
,
216 MODULE_DEVICE_TABLE(usb
, products
);
218 static struct usb_driver r815x_driver
= {
220 .id_table
= products
,
221 .probe
= usbnet_probe
,
222 .disconnect
= usbnet_disconnect
,
223 .suspend
= usbnet_suspend
,
224 .resume
= usbnet_resume
,
225 .reset_resume
= usbnet_resume
,
226 .supports_autosuspend
= 1,
227 .disable_hub_initiated_lpm
= 1,
230 module_usb_driver(r815x_driver
);
232 MODULE_AUTHOR("Hayes Wang");
233 MODULE_DESCRIPTION("Realtek USB ECM device");
234 MODULE_LICENSE("GPL");