2 * CoreChip-sz SR9700 one chip USB 1.1 Ethernet Devices
4 * Author : Liu Junliang <liujunliang_ljl@163.com>
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/stddef.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/mii.h>
20 #include <linux/usb.h>
21 #include <linux/crc32.h>
22 #include <linux/usb/usbnet.h>
26 static int sr_read(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
30 err
= usbnet_read_cmd(dev
, SR_RD_REGS
, SR_REQ_RD_REG
, 0, reg
, data
,
32 if ((err
!= length
) && (err
>= 0))
37 static int sr_write(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
41 err
= usbnet_write_cmd(dev
, SR_WR_REGS
, SR_REQ_WR_REG
, 0, reg
, data
,
43 if ((err
>= 0) && (err
< length
))
48 static int sr_read_reg(struct usbnet
*dev
, u8 reg
, u8
*value
)
50 return sr_read(dev
, reg
, 1, value
);
53 static int sr_write_reg(struct usbnet
*dev
, u8 reg
, u8 value
)
55 return usbnet_write_cmd(dev
, SR_WR_REGS
, SR_REQ_WR_REG
,
59 static void sr_write_async(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
61 usbnet_write_cmd_async(dev
, SR_WR_REGS
, SR_REQ_WR_REG
,
62 0, reg
, data
, length
);
65 static void sr_write_reg_async(struct usbnet
*dev
, u8 reg
, u8 value
)
67 usbnet_write_cmd_async(dev
, SR_WR_REGS
, SR_REQ_WR_REG
,
71 static int wait_phy_eeprom_ready(struct usbnet
*dev
, int phy
)
75 for (i
= 0; i
< SR_SHARE_TIMEOUT
; i
++) {
80 ret
= sr_read_reg(dev
, SR_EPCR
, &tmp
);
85 if (!(tmp
& EPCR_ERRE
))
89 netdev_err(dev
->net
, "%s write timed out!\n", phy
? "phy" : "eeprom");
94 static int sr_share_read_word(struct usbnet
*dev
, int phy
, u8 reg
,
99 mutex_lock(&dev
->phy_mutex
);
101 sr_write_reg(dev
, SR_EPAR
, phy
? (reg
| EPAR_PHY_ADR
) : reg
);
102 sr_write_reg(dev
, SR_EPCR
, phy
? (EPCR_EPOS
| EPCR_ERPRR
) : EPCR_ERPRR
);
104 ret
= wait_phy_eeprom_ready(dev
, phy
);
108 sr_write_reg(dev
, SR_EPCR
, 0x0);
109 ret
= sr_read(dev
, SR_EPDR
, 2, value
);
111 netdev_dbg(dev
->net
, "read shared %d 0x%02x returned 0x%04x, %d\n",
112 phy
, reg
, *value
, ret
);
115 mutex_unlock(&dev
->phy_mutex
);
119 static int sr_share_write_word(struct usbnet
*dev
, int phy
, u8 reg
,
124 mutex_lock(&dev
->phy_mutex
);
126 ret
= sr_write(dev
, SR_EPDR
, 2, &value
);
130 sr_write_reg(dev
, SR_EPAR
, phy
? (reg
| EPAR_PHY_ADR
) : reg
);
131 sr_write_reg(dev
, SR_EPCR
, phy
? (EPCR_WEP
| EPCR_EPOS
| EPCR_ERPRW
) :
132 (EPCR_WEP
| EPCR_ERPRW
));
134 ret
= wait_phy_eeprom_ready(dev
, phy
);
138 sr_write_reg(dev
, SR_EPCR
, 0x0);
141 mutex_unlock(&dev
->phy_mutex
);
145 static int sr_read_eeprom_word(struct usbnet
*dev
, u8 offset
, void *value
)
147 return sr_share_read_word(dev
, 0, offset
, value
);
150 static int sr9700_get_eeprom_len(struct net_device
*netdev
)
152 return SR_EEPROM_LEN
;
155 static int sr9700_get_eeprom(struct net_device
*netdev
,
156 struct ethtool_eeprom
*eeprom
, u8
*data
)
158 struct usbnet
*dev
= netdev_priv(netdev
);
159 __le16
*buf
= (__le16
*)data
;
163 /* access is 16bit */
164 if ((eeprom
->offset
& 0x01) || (eeprom
->len
& 0x01))
167 for (i
= 0; i
< eeprom
->len
/ 2; i
++) {
168 ret
= sr_read_eeprom_word(dev
, eeprom
->offset
/ 2 + i
, buf
+ i
);
176 static int sr_mdio_read(struct net_device
*netdev
, int phy_id
, int loc
)
178 struct usbnet
*dev
= netdev_priv(netdev
);
183 netdev_dbg(netdev
, "Only internal phy supported\n");
187 /* Access NSR_LINKST bit for link status instead of MII_BMSR */
188 if (loc
== MII_BMSR
) {
191 sr_read_reg(dev
, SR_NSR
, &value
);
192 if (value
& NSR_LINKST
)
195 sr_share_read_word(dev
, 1, loc
, &res
);
197 res
= le16_to_cpu(res
) | BMSR_LSTATUS
;
199 res
= le16_to_cpu(res
) & ~BMSR_LSTATUS
;
201 netdev_dbg(netdev
, "sr_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
207 static void sr_mdio_write(struct net_device
*netdev
, int phy_id
, int loc
,
210 struct usbnet
*dev
= netdev_priv(netdev
);
211 __le16 res
= cpu_to_le16(val
);
214 netdev_dbg(netdev
, "Only internal phy supported\n");
218 netdev_dbg(netdev
, "sr_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
221 sr_share_write_word(dev
, 1, loc
, res
);
224 static u32
sr9700_get_link(struct net_device
*netdev
)
226 struct usbnet
*dev
= netdev_priv(netdev
);
230 /* Get the Link Status directly */
231 sr_read_reg(dev
, SR_NSR
, &value
);
232 if (value
& NSR_LINKST
)
238 static int sr9700_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
240 struct usbnet
*dev
= netdev_priv(netdev
);
242 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
245 static const struct ethtool_ops sr9700_ethtool_ops
= {
246 .get_drvinfo
= usbnet_get_drvinfo
,
247 .get_link
= sr9700_get_link
,
248 .get_msglevel
= usbnet_get_msglevel
,
249 .set_msglevel
= usbnet_set_msglevel
,
250 .get_eeprom_len
= sr9700_get_eeprom_len
,
251 .get_eeprom
= sr9700_get_eeprom
,
252 .nway_reset
= usbnet_nway_reset
,
253 .get_link_ksettings
= usbnet_get_link_ksettings
,
254 .set_link_ksettings
= usbnet_set_link_ksettings
,
257 static void sr9700_set_multicast(struct net_device
*netdev
)
259 struct usbnet
*dev
= netdev_priv(netdev
);
260 /* We use the 20 byte dev->data for our 8 byte filter buffer
261 * to avoid allocating memory that is tricky to free later
263 u8
*hashes
= (u8
*)&dev
->data
;
264 /* rx_ctl setting : enable, disable_long, disable_crc */
265 u8 rx_ctl
= RCR_RXEN
| RCR_DIS_CRC
| RCR_DIS_LONG
;
267 memset(hashes
, 0x00, SR_MCAST_SIZE
);
268 /* broadcast address */
269 hashes
[SR_MCAST_SIZE
- 1] |= SR_MCAST_ADDR_FLAG
;
270 if (netdev
->flags
& IFF_PROMISC
) {
272 } else if (netdev
->flags
& IFF_ALLMULTI
||
273 netdev_mc_count(netdev
) > SR_MCAST_MAX
) {
275 } else if (!netdev_mc_empty(netdev
)) {
276 struct netdev_hw_addr
*ha
;
278 netdev_for_each_mc_addr(ha
, netdev
) {
279 u32 crc
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
280 hashes
[crc
>> 3] |= 1 << (crc
& 0x7);
284 sr_write_async(dev
, SR_MAR
, SR_MCAST_SIZE
, hashes
);
285 sr_write_reg_async(dev
, SR_RCR
, rx_ctl
);
288 static int sr9700_set_mac_address(struct net_device
*netdev
, void *p
)
290 struct usbnet
*dev
= netdev_priv(netdev
);
291 struct sockaddr
*addr
= p
;
293 if (!is_valid_ether_addr(addr
->sa_data
)) {
294 netdev_err(netdev
, "not setting invalid mac address %pM\n",
299 memcpy(netdev
->dev_addr
, addr
->sa_data
, netdev
->addr_len
);
300 sr_write_async(dev
, SR_PAR
, 6, netdev
->dev_addr
);
305 static const struct net_device_ops sr9700_netdev_ops
= {
306 .ndo_open
= usbnet_open
,
307 .ndo_stop
= usbnet_stop
,
308 .ndo_start_xmit
= usbnet_start_xmit
,
309 .ndo_tx_timeout
= usbnet_tx_timeout
,
310 .ndo_change_mtu
= usbnet_change_mtu
,
311 .ndo_get_stats64
= usbnet_get_stats64
,
312 .ndo_validate_addr
= eth_validate_addr
,
313 .ndo_do_ioctl
= sr9700_ioctl
,
314 .ndo_set_rx_mode
= sr9700_set_multicast
,
315 .ndo_set_mac_address
= sr9700_set_mac_address
,
318 static int sr9700_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
320 struct net_device
*netdev
;
321 struct mii_if_info
*mii
;
324 ret
= usbnet_get_endpoints(dev
, intf
);
330 netdev
->netdev_ops
= &sr9700_netdev_ops
;
331 netdev
->ethtool_ops
= &sr9700_ethtool_ops
;
332 netdev
->hard_header_len
+= SR_TX_OVERHEAD
;
333 dev
->hard_mtu
= netdev
->mtu
+ netdev
->hard_header_len
;
334 /* bulkin buffer is preferably not less than 3K */
335 dev
->rx_urb_size
= 3072;
339 mii
->mdio_read
= sr_mdio_read
;
340 mii
->mdio_write
= sr_mdio_write
;
341 mii
->phy_id_mask
= 0x1f;
342 mii
->reg_num_mask
= 0x1f;
344 sr_write_reg(dev
, SR_NCR
, NCR_RST
);
348 * After Chip Power on, the Chip will reload the MAC from
349 * EEPROM automatically to PAR. In case there is no EEPROM externally,
350 * a default MAC address is stored in PAR for making chip work properly.
352 if (sr_read(dev
, SR_PAR
, ETH_ALEN
, netdev
->dev_addr
) < 0) {
353 netdev_err(netdev
, "Error reading MAC address\n");
358 /* power up and reset phy */
359 sr_write_reg(dev
, SR_PRR
, PRR_PHY_RST
);
360 /* at least 10ms, here 20ms for safe */
362 sr_write_reg(dev
, SR_PRR
, 0);
363 /* at least 1ms, here 2ms for reading right register */
366 /* receive broadcast packets */
367 sr9700_set_multicast(netdev
);
369 sr_mdio_write(netdev
, mii
->phy_id
, MII_BMCR
, BMCR_RESET
);
370 sr_mdio_write(netdev
, mii
->phy_id
, MII_ADVERTISE
, ADVERTISE_ALL
|
371 ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
);
372 mii_nway_restart(mii
);
378 static int sr9700_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
380 struct sk_buff
*sr_skb
;
383 /* skb content (packets) format :
389 * p0b0 p0b1 p0b2 p0b3 ...... p0b(n-4) p0b(n-3)...p0bn
392 * p0b0 : packet 0 byte 0
395 * b1: packet length (incl crc) low
396 * b2: packet length (incl crc) high
397 * b3..n-4: packet data
398 * bn-3..bn: ethernet packet crc
400 if (unlikely(skb
->len
< SR_RX_OVERHEAD
)) {
401 netdev_err(dev
->net
, "unexpected tiny rx frame\n");
405 /* one skb may contains multiple packets */
406 while (skb
->len
> SR_RX_OVERHEAD
) {
407 if (skb
->data
[0] != 0x40)
410 /* ignore the CRC length */
411 len
= (skb
->data
[1] | (skb
->data
[2] << 8)) - 4;
413 if (len
> ETH_FRAME_LEN
)
416 /* the last packet of current skb */
417 if (skb
->len
== (len
+ SR_RX_OVERHEAD
)) {
420 skb_set_tail_pointer(skb
, len
);
421 skb
->truesize
= len
+ sizeof(struct sk_buff
);
425 /* skb_clone is used for address align */
426 sr_skb
= skb_clone(skb
, GFP_ATOMIC
);
431 sr_skb
->data
= skb
->data
+ 3;
432 skb_set_tail_pointer(sr_skb
, len
);
433 sr_skb
->truesize
= len
+ sizeof(struct sk_buff
);
434 usbnet_skb_return(dev
, sr_skb
);
436 skb_pull(skb
, len
+ SR_RX_OVERHEAD
);
442 static struct sk_buff
*sr9700_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
447 /* SR9700 can only send out one ethernet packet at once.
449 * b0 b1 b2 b3 ...... b(n-4) b(n-3)...bn
452 * b1: packet length (incl crc) low
453 * b2: packet length (incl crc) high
454 * b3..n-4: packet data
455 * bn-3..bn: ethernet packet crc
460 if (skb_cow_head(skb
, SR_TX_OVERHEAD
)) {
461 dev_kfree_skb_any(skb
);
465 __skb_push(skb
, SR_TX_OVERHEAD
);
467 /* usbnet adds padding if length is a multiple of packet size
468 * if so, adjust length value in header
470 if ((skb
->len
% dev
->maxpacket
) == 0)
474 skb
->data
[1] = len
>> 8;
479 static void sr9700_status(struct usbnet
*dev
, struct urb
*urb
)
495 if (urb
->actual_length
< 8)
498 buf
= urb
->transfer_buffer
;
500 link
= !!(buf
[0] & 0x40);
501 if (netif_carrier_ok(dev
->net
) != link
) {
502 usbnet_link_change(dev
, link
, 1);
503 netdev_dbg(dev
->net
, "Link Status is: %d\n", link
);
507 static int sr9700_link_reset(struct usbnet
*dev
)
509 struct ethtool_cmd ecmd
;
511 mii_check_media(&dev
->mii
, 1, 1);
512 mii_ethtool_gset(&dev
->mii
, &ecmd
);
514 netdev_dbg(dev
->net
, "link_reset() speed: %d duplex: %d\n",
515 ecmd
.speed
, ecmd
.duplex
);
520 static const struct driver_info sr9700_driver_info
= {
521 .description
= "CoreChip SR9700 USB Ethernet",
524 .rx_fixup
= sr9700_rx_fixup
,
525 .tx_fixup
= sr9700_tx_fixup
,
526 .status
= sr9700_status
,
527 .link_reset
= sr9700_link_reset
,
528 .reset
= sr9700_link_reset
,
531 static const struct usb_device_id products
[] = {
533 USB_DEVICE(0x0fe6, 0x9700), /* SR9700 device */
534 .driver_info
= (unsigned long)&sr9700_driver_info
,
539 MODULE_DEVICE_TABLE(usb
, products
);
541 static struct usb_driver sr9700_usb_driver
= {
543 .id_table
= products
,
544 .probe
= usbnet_probe
,
545 .disconnect
= usbnet_disconnect
,
546 .suspend
= usbnet_suspend
,
547 .resume
= usbnet_resume
,
548 .disable_hub_initiated_lpm
= 1,
551 module_usb_driver(sr9700_usb_driver
);
553 MODULE_AUTHOR("liujl <liujunliang_ljl@163.com>");
554 MODULE_DESCRIPTION("SR9700 one chip USB 1.1 USB to Ethernet device from http://www.corechip-sz.com/");
555 MODULE_LICENSE("GPL");