]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/usb/sr9800.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide
[karo-tx-linux.git] / drivers / net / usb / sr9800.c
1 /* CoreChip-sz SR9800 one chip USB 2.0 Ethernet Devices
2  *
3  * Author : Liu Junliang <liujunliang_ljl@163.com>
4  *
5  * Based on asix_common.c, asix_devices.c
6  *
7  * This file is licensed under the terms of the GNU General Public License
8  * version 2.  This program is licensed "as is" without any warranty of any
9  * kind, whether express or implied.*
10  */
11
12 #include <linux/module.h>
13 #include <linux/kmod.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/workqueue.h>
19 #include <linux/mii.h>
20 #include <linux/usb.h>
21 #include <linux/crc32.h>
22 #include <linux/usb/usbnet.h>
23 #include <linux/slab.h>
24 #include <linux/if_vlan.h>
25
26 #include "sr9800.h"
27
28 static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
29                             u16 size, void *data)
30 {
31         int err;
32
33         err = usbnet_read_cmd(dev, cmd, SR_REQ_RD_REG, value, index,
34                               data, size);
35         if ((err != size) && (err >= 0))
36                 err = -EINVAL;
37
38         return err;
39 }
40
41 static int sr_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
42                              u16 size, void *data)
43 {
44         int err;
45
46         err = usbnet_write_cmd(dev, cmd, SR_REQ_WR_REG, value, index,
47                               data, size);
48         if ((err != size) && (err >= 0))
49                 err = -EINVAL;
50
51         return err;
52 }
53
54 static void
55 sr_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
56                    u16 size, void *data)
57 {
58         usbnet_write_cmd_async(dev, cmd, SR_REQ_WR_REG, value, index, data,
59                                size);
60 }
61
62 static int sr_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
63 {
64         int offset = 0;
65
66         /* This check is no longer done by usbnet */
67         if (skb->len < dev->net->hard_header_len)
68                 return 0;
69
70         while (offset + sizeof(u32) < skb->len) {
71                 struct sk_buff *sr_skb;
72                 u16 size;
73                 u32 header = get_unaligned_le32(skb->data + offset);
74
75                 offset += sizeof(u32);
76                 /* get the packet length */
77                 size = (u16) (header & 0x7ff);
78                 if (size != ((~header >> 16) & 0x07ff)) {
79                         netdev_err(dev->net, "%s : Bad Header Length\n",
80                                    __func__);
81                         return 0;
82                 }
83
84                 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
85                     (size + offset > skb->len)) {
86                         netdev_err(dev->net, "%s : Bad RX Length %d\n",
87                                    __func__, size);
88                         return 0;
89                 }
90                 sr_skb = netdev_alloc_skb_ip_align(dev->net, size);
91                 if (!sr_skb)
92                         return 0;
93
94                 skb_put(sr_skb, size);
95                 memcpy(sr_skb->data, skb->data + offset, size);
96                 usbnet_skb_return(dev, sr_skb);
97
98                 offset += (size + 1) & 0xfffe;
99         }
100
101         if (skb->len != offset) {
102                 netdev_err(dev->net, "%s : Bad SKB Length %d\n", __func__,
103                            skb->len);
104                 return 0;
105         }
106
107         return 1;
108 }
109
110 static struct sk_buff *sr_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
111                                         gfp_t flags)
112 {
113         int headroom = skb_headroom(skb);
114         int tailroom = skb_tailroom(skb);
115         u32 padbytes = 0xffff0000;
116         u32 packet_len;
117         int padlen;
118
119         padlen = ((skb->len + 4) % (dev->maxpacket - 1)) ? 0 : 4;
120
121         if ((!skb_cloned(skb)) && ((headroom + tailroom) >= (4 + padlen))) {
122                 if ((headroom < 4) || (tailroom < padlen)) {
123                         skb->data = memmove(skb->head + 4, skb->data,
124                                             skb->len);
125                         skb_set_tail_pointer(skb, skb->len);
126                 }
127         } else {
128                 struct sk_buff *skb2;
129                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
130                 dev_kfree_skb_any(skb);
131                 skb = skb2;
132                 if (!skb)
133                         return NULL;
134         }
135
136         skb_push(skb, 4);
137         packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
138         cpu_to_le32s(&packet_len);
139         skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
140
141         if (padlen) {
142                 cpu_to_le32s(&padbytes);
143                 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
144                 skb_put(skb, sizeof(padbytes));
145         }
146
147         usbnet_set_skb_tx_stats(skb, 1, 0);
148         return skb;
149 }
150
151 static void sr_status(struct usbnet *dev, struct urb *urb)
152 {
153         struct sr9800_int_data *event;
154         int link;
155
156         if (urb->actual_length < 8)
157                 return;
158
159         event = urb->transfer_buffer;
160         link = event->link & 0x01;
161         if (netif_carrier_ok(dev->net) != link) {
162                 usbnet_link_change(dev, link, 1);
163                 netdev_dbg(dev->net, "Link Status is: %d\n", link);
164         }
165
166         return;
167 }
168
169 static inline int sr_set_sw_mii(struct usbnet *dev)
170 {
171         int ret;
172
173         ret = sr_write_cmd(dev, SR_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
174         if (ret < 0)
175                 netdev_err(dev->net, "Failed to enable software MII access\n");
176         return ret;
177 }
178
179 static inline int sr_set_hw_mii(struct usbnet *dev)
180 {
181         int ret;
182
183         ret = sr_write_cmd(dev, SR_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
184         if (ret < 0)
185                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
186         return ret;
187 }
188
189 static inline int sr_get_phy_addr(struct usbnet *dev)
190 {
191         u8 buf[2];
192         int ret;
193
194         ret = sr_read_cmd(dev, SR_CMD_READ_PHY_ID, 0, 0, 2, buf);
195         if (ret < 0) {
196                 netdev_err(dev->net, "%s : Error reading PHYID register:%02x\n",
197                            __func__, ret);
198                 goto out;
199         }
200         netdev_dbg(dev->net, "%s : returning 0x%04x\n", __func__,
201                    *((__le16 *)buf));
202
203         ret = buf[1];
204
205 out:
206         return ret;
207 }
208
209 static int sr_sw_reset(struct usbnet *dev, u8 flags)
210 {
211         int ret;
212
213         ret = sr_write_cmd(dev, SR_CMD_SW_RESET, flags, 0, 0, NULL);
214         if (ret < 0)
215                 netdev_err(dev->net, "Failed to send software reset:%02x\n",
216                            ret);
217
218         return ret;
219 }
220
221 static u16 sr_read_rx_ctl(struct usbnet *dev)
222 {
223         __le16 v;
224         int ret;
225
226         ret = sr_read_cmd(dev, SR_CMD_READ_RX_CTL, 0, 0, 2, &v);
227         if (ret < 0) {
228                 netdev_err(dev->net, "Error reading RX_CTL register:%02x\n",
229                            ret);
230                 goto out;
231         }
232
233         ret = le16_to_cpu(v);
234 out:
235         return ret;
236 }
237
238 static int sr_write_rx_ctl(struct usbnet *dev, u16 mode)
239 {
240         int ret;
241
242         netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
243         ret = sr_write_cmd(dev, SR_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
244         if (ret < 0)
245                 netdev_err(dev->net,
246                            "Failed to write RX_CTL mode to 0x%04x:%02x\n",
247                            mode, ret);
248
249         return ret;
250 }
251
252 static u16 sr_read_medium_status(struct usbnet *dev)
253 {
254         __le16 v;
255         int ret;
256
257         ret = sr_read_cmd(dev, SR_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
258         if (ret < 0) {
259                 netdev_err(dev->net,
260                            "Error reading Medium Status register:%02x\n", ret);
261                 return ret;     /* TODO: callers not checking for error ret */
262         }
263
264         return le16_to_cpu(v);
265 }
266
267 static int sr_write_medium_mode(struct usbnet *dev, u16 mode)
268 {
269         int ret;
270
271         netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode);
272         ret = sr_write_cmd(dev, SR_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
273         if (ret < 0)
274                 netdev_err(dev->net,
275                            "Failed to write Medium Mode mode to 0x%04x:%02x\n",
276                            mode, ret);
277         return ret;
278 }
279
280 static int sr_write_gpio(struct usbnet *dev, u16 value, int sleep)
281 {
282         int ret;
283
284         netdev_dbg(dev->net, "%s : value = 0x%04x\n", __func__, value);
285         ret = sr_write_cmd(dev, SR_CMD_WRITE_GPIOS, value, 0, 0, NULL);
286         if (ret < 0)
287                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x:%02x\n",
288                            value, ret);
289         if (sleep)
290                 msleep(sleep);
291
292         return ret;
293 }
294
295 /* SR9800 have a 16-bit RX_CTL value */
296 static void sr_set_multicast(struct net_device *net)
297 {
298         struct usbnet *dev = netdev_priv(net);
299         struct sr_data *data = (struct sr_data *)&dev->data;
300         u16 rx_ctl = SR_DEFAULT_RX_CTL;
301
302         if (net->flags & IFF_PROMISC) {
303                 rx_ctl |= SR_RX_CTL_PRO;
304         } else if (net->flags & IFF_ALLMULTI ||
305                    netdev_mc_count(net) > SR_MAX_MCAST) {
306                 rx_ctl |= SR_RX_CTL_AMALL;
307         } else if (netdev_mc_empty(net)) {
308                 /* just broadcast and directed */
309         } else {
310                 /* We use the 20 byte dev->data
311                  * for our 8 byte filter buffer
312                  * to avoid allocating memory that
313                  * is tricky to free later
314                  */
315                 struct netdev_hw_addr *ha;
316                 u32 crc_bits;
317
318                 memset(data->multi_filter, 0, SR_MCAST_FILTER_SIZE);
319
320                 /* Build the multicast hash filter. */
321                 netdev_for_each_mc_addr(ha, net) {
322                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
323                         data->multi_filter[crc_bits >> 3] |=
324                             1 << (crc_bits & 7);
325                 }
326
327                 sr_write_cmd_async(dev, SR_CMD_WRITE_MULTI_FILTER, 0, 0,
328                                    SR_MCAST_FILTER_SIZE, data->multi_filter);
329
330                 rx_ctl |= SR_RX_CTL_AM;
331         }
332
333         sr_write_cmd_async(dev, SR_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
334 }
335
336 static int sr_mdio_read(struct net_device *net, int phy_id, int loc)
337 {
338         struct usbnet *dev = netdev_priv(net);
339         __le16 res;
340
341         mutex_lock(&dev->phy_mutex);
342         sr_set_sw_mii(dev);
343         sr_read_cmd(dev, SR_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, &res);
344         sr_set_hw_mii(dev);
345         mutex_unlock(&dev->phy_mutex);
346
347         netdev_dbg(dev->net,
348                    "%s : phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", __func__,
349                    phy_id, loc, le16_to_cpu(res));
350
351         return le16_to_cpu(res);
352 }
353
354 static void
355 sr_mdio_write(struct net_device *net, int phy_id, int loc, int val)
356 {
357         struct usbnet *dev = netdev_priv(net);
358         __le16 res = cpu_to_le16(val);
359
360         netdev_dbg(dev->net,
361                    "%s : phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", __func__,
362                    phy_id, loc, val);
363         mutex_lock(&dev->phy_mutex);
364         sr_set_sw_mii(dev);
365         sr_write_cmd(dev, SR_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
366         sr_set_hw_mii(dev);
367         mutex_unlock(&dev->phy_mutex);
368 }
369
370 /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
371 static u32 sr_get_phyid(struct usbnet *dev)
372 {
373         int phy_reg;
374         u32 phy_id;
375         int i;
376
377         /* Poll for the rare case the FW or phy isn't ready yet.  */
378         for (i = 0; i < 100; i++) {
379                 phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
380                 if (phy_reg != 0 && phy_reg != 0xFFFF)
381                         break;
382                 mdelay(1);
383         }
384
385         if (phy_reg <= 0 || phy_reg == 0xFFFF)
386                 return 0;
387
388         phy_id = (phy_reg & 0xffff) << 16;
389
390         phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
391         if (phy_reg < 0)
392                 return 0;
393
394         phy_id |= (phy_reg & 0xffff);
395
396         return phy_id;
397 }
398
399 static void
400 sr_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
401 {
402         struct usbnet *dev = netdev_priv(net);
403         u8 opt;
404
405         if (sr_read_cmd(dev, SR_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
406                 wolinfo->supported = 0;
407                 wolinfo->wolopts = 0;
408                 return;
409         }
410         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
411         wolinfo->wolopts = 0;
412         if (opt & SR_MONITOR_LINK)
413                 wolinfo->wolopts |= WAKE_PHY;
414         if (opt & SR_MONITOR_MAGIC)
415                 wolinfo->wolopts |= WAKE_MAGIC;
416 }
417
418 static int
419 sr_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
420 {
421         struct usbnet *dev = netdev_priv(net);
422         u8 opt = 0;
423
424         if (wolinfo->wolopts & WAKE_PHY)
425                 opt |= SR_MONITOR_LINK;
426         if (wolinfo->wolopts & WAKE_MAGIC)
427                 opt |= SR_MONITOR_MAGIC;
428
429         if (sr_write_cmd(dev, SR_CMD_WRITE_MONITOR_MODE,
430                          opt, 0, 0, NULL) < 0)
431                 return -EINVAL;
432
433         return 0;
434 }
435
436 static int sr_get_eeprom_len(struct net_device *net)
437 {
438         struct usbnet *dev = netdev_priv(net);
439         struct sr_data *data = (struct sr_data *)&dev->data;
440
441         return data->eeprom_len;
442 }
443
444 static int sr_get_eeprom(struct net_device *net,
445                               struct ethtool_eeprom *eeprom, u8 *data)
446 {
447         struct usbnet *dev = netdev_priv(net);
448         __le16 *ebuf = (__le16 *)data;
449         int ret;
450         int i;
451
452         /* Crude hack to ensure that we don't overwrite memory
453          * if an odd length is supplied
454          */
455         if (eeprom->len % 2)
456                 return -EINVAL;
457
458         eeprom->magic = SR_EEPROM_MAGIC;
459
460         /* sr9800 returns 2 bytes from eeprom on read */
461         for (i = 0; i < eeprom->len / 2; i++) {
462                 ret = sr_read_cmd(dev, SR_CMD_READ_EEPROM, eeprom->offset + i,
463                                   0, 2, &ebuf[i]);
464                 if (ret < 0)
465                         return -EINVAL;
466         }
467         return 0;
468 }
469
470 static void sr_get_drvinfo(struct net_device *net,
471                                  struct ethtool_drvinfo *info)
472 {
473         /* Inherit standard device info */
474         usbnet_get_drvinfo(net, info);
475         strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
476         strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
477 }
478
479 static u32 sr_get_link(struct net_device *net)
480 {
481         struct usbnet *dev = netdev_priv(net);
482
483         return mii_link_ok(&dev->mii);
484 }
485
486 static int sr_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
487 {
488         struct usbnet *dev = netdev_priv(net);
489
490         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
491 }
492
493 static int sr_set_mac_address(struct net_device *net, void *p)
494 {
495         struct usbnet *dev = netdev_priv(net);
496         struct sr_data *data = (struct sr_data *)&dev->data;
497         struct sockaddr *addr = p;
498
499         if (netif_running(net))
500                 return -EBUSY;
501         if (!is_valid_ether_addr(addr->sa_data))
502                 return -EADDRNOTAVAIL;
503
504         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
505
506         /* We use the 20 byte dev->data
507          * for our 6 byte mac buffer
508          * to avoid allocating memory that
509          * is tricky to free later
510          */
511         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
512         sr_write_cmd_async(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
513                            data->mac_addr);
514
515         return 0;
516 }
517
518 static const struct ethtool_ops sr9800_ethtool_ops = {
519         .get_drvinfo    = sr_get_drvinfo,
520         .get_link       = sr_get_link,
521         .get_msglevel   = usbnet_get_msglevel,
522         .set_msglevel   = usbnet_set_msglevel,
523         .get_wol        = sr_get_wol,
524         .set_wol        = sr_set_wol,
525         .get_eeprom_len = sr_get_eeprom_len,
526         .get_eeprom     = sr_get_eeprom,
527         .get_settings   = usbnet_get_settings,
528         .set_settings   = usbnet_set_settings,
529         .nway_reset     = usbnet_nway_reset,
530 };
531
532 static int sr9800_link_reset(struct usbnet *dev)
533 {
534         struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
535         u16 mode;
536
537         mii_check_media(&dev->mii, 1, 1);
538         mii_ethtool_gset(&dev->mii, &ecmd);
539         mode = SR9800_MEDIUM_DEFAULT;
540
541         if (ethtool_cmd_speed(&ecmd) != SPEED_100)
542                 mode &= ~SR_MEDIUM_PS;
543
544         if (ecmd.duplex != DUPLEX_FULL)
545                 mode &= ~SR_MEDIUM_FD;
546
547         netdev_dbg(dev->net, "%s : speed: %u duplex: %d mode: 0x%04x\n",
548                    __func__, ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
549
550         sr_write_medium_mode(dev, mode);
551
552         return 0;
553 }
554
555
556 static int sr9800_set_default_mode(struct usbnet *dev)
557 {
558         u16 rx_ctl;
559         int ret;
560
561         sr_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
562         sr_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
563                       ADVERTISE_ALL | ADVERTISE_CSMA);
564         mii_nway_restart(&dev->mii);
565
566         ret = sr_write_medium_mode(dev, SR9800_MEDIUM_DEFAULT);
567         if (ret < 0)
568                 goto out;
569
570         ret = sr_write_cmd(dev, SR_CMD_WRITE_IPG012,
571                                 SR9800_IPG0_DEFAULT | SR9800_IPG1_DEFAULT,
572                                 SR9800_IPG2_DEFAULT, 0, NULL);
573         if (ret < 0) {
574                 netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
575                 goto out;
576         }
577
578         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
579         ret = sr_write_rx_ctl(dev, SR_DEFAULT_RX_CTL);
580         if (ret < 0)
581                 goto out;
582
583         rx_ctl = sr_read_rx_ctl(dev);
584         netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
585                    rx_ctl);
586
587         rx_ctl = sr_read_medium_status(dev);
588         netdev_dbg(dev->net, "Medium Status:0x%04x after all initializations\n",
589                    rx_ctl);
590
591         return 0;
592 out:
593         return ret;
594 }
595
596 static int sr9800_reset(struct usbnet *dev)
597 {
598         struct sr_data *data = (struct sr_data *)&dev->data;
599         int ret, embd_phy;
600         u16 rx_ctl;
601
602         ret = sr_write_gpio(dev,
603                         SR_GPIO_RSE | SR_GPIO_GPO_2 | SR_GPIO_GPO2EN, 5);
604         if (ret < 0)
605                 goto out;
606
607         embd_phy = ((sr_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
608
609         ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
610         if (ret < 0) {
611                 netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
612                 goto out;
613         }
614
615         ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_PRL);
616         if (ret < 0)
617                 goto out;
618
619         msleep(150);
620
621         ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
622         if (ret < 0)
623                 goto out;
624
625         msleep(150);
626
627         if (embd_phy) {
628                 ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
629                 if (ret < 0)
630                         goto out;
631         } else {
632                 ret = sr_sw_reset(dev, SR_SWRESET_PRTE);
633                 if (ret < 0)
634                         goto out;
635         }
636
637         msleep(150);
638         rx_ctl = sr_read_rx_ctl(dev);
639         netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
640         ret = sr_write_rx_ctl(dev, 0x0000);
641         if (ret < 0)
642                 goto out;
643
644         rx_ctl = sr_read_rx_ctl(dev);
645         netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
646
647         ret = sr_sw_reset(dev, SR_SWRESET_PRL);
648         if (ret < 0)
649                 goto out;
650
651         msleep(150);
652
653         ret = sr_sw_reset(dev, SR_SWRESET_IPRL | SR_SWRESET_PRL);
654         if (ret < 0)
655                 goto out;
656
657         msleep(150);
658
659         ret = sr9800_set_default_mode(dev);
660         if (ret < 0)
661                 goto out;
662
663         /* Rewrite MAC address */
664         memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
665         ret = sr_write_cmd(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
666                                                         data->mac_addr);
667         if (ret < 0)
668                 goto out;
669
670         return 0;
671
672 out:
673         return ret;
674 }
675
676 static const struct net_device_ops sr9800_netdev_ops = {
677         .ndo_open               = usbnet_open,
678         .ndo_stop               = usbnet_stop,
679         .ndo_start_xmit         = usbnet_start_xmit,
680         .ndo_tx_timeout         = usbnet_tx_timeout,
681         .ndo_change_mtu         = usbnet_change_mtu,
682         .ndo_set_mac_address    = sr_set_mac_address,
683         .ndo_validate_addr      = eth_validate_addr,
684         .ndo_do_ioctl           = sr_ioctl,
685         .ndo_set_rx_mode        = sr_set_multicast,
686 };
687
688 static int sr9800_phy_powerup(struct usbnet *dev)
689 {
690         int ret;
691
692         /* set the embedded Ethernet PHY in power-down state */
693         ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_IPRL);
694         if (ret < 0) {
695                 netdev_err(dev->net, "Failed to power down PHY : %d\n", ret);
696                 return ret;
697         }
698         msleep(20);
699
700         /* set the embedded Ethernet PHY in power-up state */
701         ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
702         if (ret < 0) {
703                 netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
704                 return ret;
705         }
706         msleep(600);
707
708         /* set the embedded Ethernet PHY in reset state */
709         ret = sr_sw_reset(dev, SR_SWRESET_CLEAR);
710         if (ret < 0) {
711                 netdev_err(dev->net, "Failed to power up PHY: %d\n", ret);
712                 return ret;
713         }
714         msleep(20);
715
716         /* set the embedded Ethernet PHY in power-up state */
717         ret = sr_sw_reset(dev, SR_SWRESET_IPRL);
718         if (ret < 0) {
719                 netdev_err(dev->net, "Failed to reset PHY: %d\n", ret);
720                 return ret;
721         }
722
723         return 0;
724 }
725
726 static int sr9800_bind(struct usbnet *dev, struct usb_interface *intf)
727 {
728         struct sr_data *data = (struct sr_data *)&dev->data;
729         u16 led01_mux, led23_mux;
730         int ret, embd_phy;
731         u32 phyid;
732         u16 rx_ctl;
733
734         data->eeprom_len = SR9800_EEPROM_LEN;
735
736         usbnet_get_endpoints(dev, intf);
737
738         /* LED Setting Rule :
739          * AABB:CCDD
740          * AA : MFA0(LED0)
741          * BB : MFA1(LED1)
742          * CC : MFA2(LED2), Reserved for SR9800
743          * DD : MFA3(LED3), Reserved for SR9800
744          */
745         led01_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_LINK;
746         led23_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_TX_ACTIVE;
747         ret = sr_write_cmd(dev, SR_CMD_LED_MUX, led01_mux, led23_mux, 0, NULL);
748         if (ret < 0) {
749                         netdev_err(dev->net, "set LINK LED failed : %d\n", ret);
750                         goto out;
751         }
752
753         /* Get the MAC address */
754         ret = sr_read_cmd(dev, SR_CMD_READ_NODE_ID, 0, 0, ETH_ALEN,
755                           dev->net->dev_addr);
756         if (ret < 0) {
757                 netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
758                 return ret;
759         }
760         netdev_dbg(dev->net, "mac addr : %pM\n", dev->net->dev_addr);
761
762         /* Initialize MII structure */
763         dev->mii.dev = dev->net;
764         dev->mii.mdio_read = sr_mdio_read;
765         dev->mii.mdio_write = sr_mdio_write;
766         dev->mii.phy_id_mask = 0x1f;
767         dev->mii.reg_num_mask = 0x1f;
768         dev->mii.phy_id = sr_get_phy_addr(dev);
769
770         dev->net->netdev_ops = &sr9800_netdev_ops;
771         dev->net->ethtool_ops = &sr9800_ethtool_ops;
772
773         embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
774         /* Reset the PHY to normal operation mode */
775         ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
776         if (ret < 0) {
777                 netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
778                 return ret;
779         }
780
781         /* Init PHY routine */
782         ret = sr9800_phy_powerup(dev);
783         if (ret < 0)
784                 goto out;
785
786         rx_ctl = sr_read_rx_ctl(dev);
787         netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
788         ret = sr_write_rx_ctl(dev, 0x0000);
789         if (ret < 0)
790                 goto out;
791
792         rx_ctl = sr_read_rx_ctl(dev);
793         netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
794
795         /* Read PHYID register *AFTER* the PHY was reset properly */
796         phyid = sr_get_phyid(dev);
797         netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
798
799         /* medium mode setting */
800         ret = sr9800_set_default_mode(dev);
801         if (ret < 0)
802                 goto out;
803
804         if (dev->udev->speed == USB_SPEED_HIGH) {
805                 ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
806                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].byte_cnt,
807                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].threshold,
808                         0, NULL);
809                 if (ret < 0) {
810                         netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
811                         goto out;
812                 }
813                 dev->rx_urb_size =
814                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].size;
815         } else {
816                 ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE,
817                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].byte_cnt,
818                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].threshold,
819                         0, NULL);
820                 if (ret < 0) {
821                         netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret);
822                         goto out;
823                 }
824                 dev->rx_urb_size =
825                         SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].size;
826         }
827         netdev_dbg(dev->net, "%s : setting rx_urb_size with : %zu\n", __func__,
828                    dev->rx_urb_size);
829         return 0;
830
831 out:
832         return ret;
833 }
834
835 static const struct driver_info sr9800_driver_info = {
836         .description    = "CoreChip SR9800 USB 2.0 Ethernet",
837         .bind           = sr9800_bind,
838         .status         = sr_status,
839         .link_reset     = sr9800_link_reset,
840         .reset          = sr9800_reset,
841         .flags          = DRIVER_FLAG,
842         .rx_fixup       = sr_rx_fixup,
843         .tx_fixup       = sr_tx_fixup,
844 };
845
846 static const struct usb_device_id       products[] = {
847         {
848                 USB_DEVICE(0x0fe6, 0x9800),     /* SR9800 Device  */
849                 .driver_info = (unsigned long) &sr9800_driver_info,
850         },
851         {},             /* END */
852 };
853
854 MODULE_DEVICE_TABLE(usb, products);
855
856 static struct usb_driver sr_driver = {
857         .name           = DRIVER_NAME,
858         .id_table       = products,
859         .probe          = usbnet_probe,
860         .suspend        = usbnet_suspend,
861         .resume         = usbnet_resume,
862         .disconnect     = usbnet_disconnect,
863         .supports_autosuspend = 1,
864 };
865
866 module_usb_driver(sr_driver);
867
868 MODULE_AUTHOR("Liu Junliang <liujunliang_ljl@163.com");
869 MODULE_VERSION(DRIVER_VERSION);
870 MODULE_DESCRIPTION("SR9800 USB 2.0 USB2NET Dev : http://www.corechip-sz.com");
871 MODULE_LICENSE("GPL");