2 * ASIX AX88172A based USB 2.0 Ethernet Devices
3 * Copyright (C) 2012 OMICRON electronics GmbH
5 * Supports external PHYs via phylib. Based on the driver for the
6 * AX88772. Original copyrights follow:
8 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
9 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
10 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
11 * Copyright (c) 2002-2003 TiVo Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <linux/phy.h>
30 struct ax88172a_private
{
32 struct phy_device
*phydev
;
37 struct asix_rx_fixup_info rx_fixup_info
;
40 /* MDIO read and write wrappers for phylib */
41 static int asix_mdio_bus_read(struct mii_bus
*bus
, int phy_id
, int regnum
)
43 return asix_mdio_read(((struct usbnet
*)bus
->priv
)->net
, phy_id
,
47 static int asix_mdio_bus_write(struct mii_bus
*bus
, int phy_id
, int regnum
,
50 asix_mdio_write(((struct usbnet
*)bus
->priv
)->net
, phy_id
, regnum
, val
);
54 static int ax88172a_ioctl(struct net_device
*net
, struct ifreq
*rq
, int cmd
)
56 if (!netif_running(net
))
62 return phy_mii_ioctl(net
->phydev
, rq
, cmd
);
65 /* set MAC link settings according to information from phylib */
66 static void ax88172a_adjust_link(struct net_device
*netdev
)
68 struct phy_device
*phydev
= netdev
->phydev
;
69 struct usbnet
*dev
= netdev_priv(netdev
);
70 struct ax88172a_private
*priv
= dev
->driver_priv
;
74 mode
= AX88772_MEDIUM_DEFAULT
;
76 if (phydev
->duplex
== DUPLEX_HALF
)
77 mode
&= ~AX_MEDIUM_FD
;
79 if (phydev
->speed
!= SPEED_100
)
80 mode
&= ~AX_MEDIUM_PS
;
83 if (mode
!= priv
->oldmode
) {
84 asix_write_medium_mode(dev
, mode
);
86 netdev_dbg(netdev
, "speed %u duplex %d, setting mode to 0x%04x\n",
87 phydev
->speed
, phydev
->duplex
, mode
);
88 phy_print_status(phydev
);
92 static void ax88172a_status(struct usbnet
*dev
, struct urb
*urb
)
94 /* link changes are detected by polling the phy */
97 /* use phylib infrastructure */
98 static int ax88172a_init_mdio(struct usbnet
*dev
)
100 struct ax88172a_private
*priv
= dev
->driver_priv
;
103 priv
->mdio
= mdiobus_alloc();
105 netdev_err(dev
->net
, "Could not allocate MDIO bus\n");
109 priv
->mdio
->priv
= (void *)dev
;
110 priv
->mdio
->read
= &asix_mdio_bus_read
;
111 priv
->mdio
->write
= &asix_mdio_bus_write
;
112 priv
->mdio
->name
= "Asix MDIO Bus";
113 /* mii bus name is usb-<usb bus number>-<usb device number> */
114 snprintf(priv
->mdio
->id
, MII_BUS_ID_SIZE
, "usb-%03d:%03d",
115 dev
->udev
->bus
->busnum
, dev
->udev
->devnum
);
117 ret
= mdiobus_register(priv
->mdio
);
119 netdev_err(dev
->net
, "Could not register MDIO bus\n");
123 netdev_info(dev
->net
, "registered mdio bus %s\n", priv
->mdio
->id
);
127 mdiobus_free(priv
->mdio
);
131 static void ax88172a_remove_mdio(struct usbnet
*dev
)
133 struct ax88172a_private
*priv
= dev
->driver_priv
;
135 netdev_info(dev
->net
, "deregistering mdio bus %s\n", priv
->mdio
->id
);
136 mdiobus_unregister(priv
->mdio
);
137 mdiobus_free(priv
->mdio
);
140 static const struct net_device_ops ax88172a_netdev_ops
= {
141 .ndo_open
= usbnet_open
,
142 .ndo_stop
= usbnet_stop
,
143 .ndo_start_xmit
= usbnet_start_xmit
,
144 .ndo_tx_timeout
= usbnet_tx_timeout
,
145 .ndo_change_mtu
= usbnet_change_mtu
,
146 .ndo_set_mac_address
= asix_set_mac_address
,
147 .ndo_validate_addr
= eth_validate_addr
,
148 .ndo_do_ioctl
= ax88172a_ioctl
,
149 .ndo_set_rx_mode
= asix_set_multicast
,
152 static int ax88172a_get_settings(struct net_device
*net
,
153 struct ethtool_cmd
*cmd
)
158 return phy_ethtool_gset(net
->phydev
, cmd
);
161 static int ax88172a_set_settings(struct net_device
*net
,
162 struct ethtool_cmd
*cmd
)
167 return phy_ethtool_sset(net
->phydev
, cmd
);
170 static int ax88172a_nway_reset(struct net_device
*net
)
175 return phy_start_aneg(net
->phydev
);
178 static const struct ethtool_ops ax88172a_ethtool_ops
= {
179 .get_drvinfo
= asix_get_drvinfo
,
180 .get_link
= usbnet_get_link
,
181 .get_msglevel
= usbnet_get_msglevel
,
182 .set_msglevel
= usbnet_set_msglevel
,
183 .get_wol
= asix_get_wol
,
184 .set_wol
= asix_set_wol
,
185 .get_eeprom_len
= asix_get_eeprom_len
,
186 .get_eeprom
= asix_get_eeprom
,
187 .set_eeprom
= asix_set_eeprom
,
188 .get_settings
= ax88172a_get_settings
,
189 .set_settings
= ax88172a_set_settings
,
190 .nway_reset
= ax88172a_nway_reset
,
193 static int ax88172a_reset_phy(struct usbnet
*dev
, int embd_phy
)
197 ret
= asix_sw_reset(dev
, AX_SWRESET_IPPD
);
202 ret
= asix_sw_reset(dev
, AX_SWRESET_CLEAR
);
208 ret
= asix_sw_reset(dev
, embd_phy
? AX_SWRESET_IPRL
: AX_SWRESET_IPPD
);
219 static int ax88172a_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
223 struct ax88172a_private
*priv
;
225 usbnet_get_endpoints(dev
, intf
);
227 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
231 dev
->driver_priv
= priv
;
233 /* Get the MAC address */
234 ret
= asix_read_cmd(dev
, AX_CMD_READ_NODE_ID
, 0, 0, ETH_ALEN
, buf
);
236 netdev_err(dev
->net
, "Failed to read MAC address: %d\n", ret
);
239 memcpy(dev
->net
->dev_addr
, buf
, ETH_ALEN
);
241 dev
->net
->netdev_ops
= &ax88172a_netdev_ops
;
242 dev
->net
->ethtool_ops
= &ax88172a_ethtool_ops
;
244 /* are we using the internal or the external phy? */
245 ret
= asix_read_cmd(dev
, AX_CMD_SW_PHY_STATUS
, 0, 0, 1, buf
);
247 netdev_err(dev
->net
, "Failed to read software interface selection register: %d\n",
252 netdev_dbg(dev
->net
, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf
[0]);
253 switch (buf
[0] & AX_PHY_SELECT_MASK
) {
254 case AX_PHY_SELECT_INTERNAL
:
255 netdev_dbg(dev
->net
, "use internal phy\n");
256 priv
->use_embdphy
= 1;
258 case AX_PHY_SELECT_EXTERNAL
:
259 netdev_dbg(dev
->net
, "use external phy\n");
260 priv
->use_embdphy
= 0;
263 netdev_err(dev
->net
, "Interface mode not supported by driver\n");
268 priv
->phy_addr
= asix_read_phy_addr(dev
, priv
->use_embdphy
);
269 ax88172a_reset_phy(dev
, priv
->use_embdphy
);
271 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
272 if (dev
->driver_info
->flags
& FLAG_FRAMING_AX
) {
273 /* hard_mtu is still the default - the device does not support
275 dev
->rx_urb_size
= 2048;
279 ret
= ax88172a_init_mdio(dev
);
290 static int ax88172a_stop(struct usbnet
*dev
)
292 struct ax88172a_private
*priv
= dev
->driver_priv
;
294 netdev_dbg(dev
->net
, "Stopping interface\n");
297 netdev_info(dev
->net
, "Disconnecting from phy %s\n",
299 phy_stop(priv
->phydev
);
300 phy_disconnect(priv
->phydev
);
306 static void ax88172a_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
308 struct ax88172a_private
*priv
= dev
->driver_priv
;
310 ax88172a_remove_mdio(dev
);
314 static int ax88172a_reset(struct usbnet
*dev
)
316 struct asix_data
*data
= (struct asix_data
*)&dev
->data
;
317 struct ax88172a_private
*priv
= dev
->driver_priv
;
321 ax88172a_reset_phy(dev
, priv
->use_embdphy
);
324 rx_ctl
= asix_read_rx_ctl(dev
);
325 netdev_dbg(dev
->net
, "RX_CTL is 0x%04x after software reset\n", rx_ctl
);
326 ret
= asix_write_rx_ctl(dev
, 0x0000);
330 rx_ctl
= asix_read_rx_ctl(dev
);
331 netdev_dbg(dev
->net
, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl
);
335 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_IPG0
,
336 AX88772_IPG0_DEFAULT
| AX88772_IPG1_DEFAULT
,
337 AX88772_IPG2_DEFAULT
, 0, NULL
);
339 netdev_err(dev
->net
, "Write IPG,IPG1,IPG2 failed: %d\n", ret
);
343 /* Rewrite MAC address */
344 memcpy(data
->mac_addr
, dev
->net
->dev_addr
, ETH_ALEN
);
345 ret
= asix_write_cmd(dev
, AX_CMD_WRITE_NODE_ID
, 0, 0, ETH_ALEN
,
350 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
351 ret
= asix_write_rx_ctl(dev
, AX_DEFAULT_RX_CTL
);
355 rx_ctl
= asix_read_rx_ctl(dev
);
356 netdev_dbg(dev
->net
, "RX_CTL is 0x%04x after all initializations\n",
359 rx_ctl
= asix_read_medium_status(dev
);
360 netdev_dbg(dev
->net
, "Medium Status is 0x%04x after all initializations\n",
364 snprintf(priv
->phy_name
, 20, PHY_ID_FMT
,
365 priv
->mdio
->id
, priv
->phy_addr
);
367 priv
->phydev
= phy_connect(dev
->net
, priv
->phy_name
,
368 &ax88172a_adjust_link
,
369 PHY_INTERFACE_MODE_MII
);
370 if (IS_ERR(priv
->phydev
)) {
371 netdev_err(dev
->net
, "Could not connect to PHY device %s\n",
373 ret
= PTR_ERR(priv
->phydev
);
377 netdev_info(dev
->net
, "Connected to phy %s\n", priv
->phy_name
);
379 /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
380 * bit of the PHY. Bring the PHY up again.
382 genphy_resume(priv
->phydev
);
383 phy_start(priv
->phydev
);
392 static int ax88172a_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
394 struct ax88172a_private
*dp
= dev
->driver_priv
;
395 struct asix_rx_fixup_info
*rx
= &dp
->rx_fixup_info
;
397 return asix_rx_fixup_internal(dev
, skb
, rx
);
400 const struct driver_info ax88172a_info
= {
401 .description
= "ASIX AX88172A USB 2.0 Ethernet",
402 .bind
= ax88172a_bind
,
403 .reset
= ax88172a_reset
,
404 .stop
= ax88172a_stop
,
405 .unbind
= ax88172a_unbind
,
406 .status
= ax88172a_status
,
407 .flags
= FLAG_ETHER
| FLAG_FRAMING_AX
| FLAG_LINK_INTR
|
409 .rx_fixup
= ax88172a_rx_fixup
,
410 .tx_fixup
= asix_tx_fixup
,