-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax_main.c
2620 lines (2172 loc) · 59.5 KB
/
ax_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*******************************************************************************
* Copyright (c) 2022 ASIX Electronic Corporation All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
#include "ax_main.h"
#include "ax88179_178a.h"
#include "ax88179a_772d.h"
#ifdef ENABLE_PTP_FUNC
#include "ax_ptp.h"
#endif
#ifdef ENABLE_MACSEC_FUNC
#include "ax_macsec.h"
#endif
#ifdef ENABLE_AUTODETACH_FUNC
static int autodetach = -1;
module_param(autodetach, int, 0);
MODULE_PARM_DESC(autodetach, "Autodetach configuration");
#endif
static int bctrl = -1;
module_param(bctrl, int, 0);
MODULE_PARM_DESC(bctrl, "RX Bulk Control");
static int blwt = -1;
module_param(blwt, int, 0);
MODULE_PARM_DESC(blwt, "RX Bulk Timer Low");
static int bhit = -1;
module_param(bhit, int, 0);
MODULE_PARM_DESC(bhit, "RX Bulk Timer High");
static int bsize = -1;
module_param(bsize, int, 0);
MODULE_PARM_DESC(bsize, "RX Bulk Queue Size");
static int bifg = -1;
module_param(bifg, int, 0);
MODULE_PARM_DESC(bifg, "RX Bulk Inter Frame Gap");
static int
ax_submit_rx(struct ax_device *netdev, struct rx_desc *desc, gfp_t mem_flags);
static void ax_set_carrier(struct ax_device *axdev);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
void ax_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
{
struct ax_device *axdev = netdev_priv(net);
strscpy(info->driver, MODULENAME, sizeof(info->driver));
strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
usb_make_path(axdev->udev, info->bus_info, sizeof(info->bus_info));
sprintf(info->fw_version, "v%d.%d.%d.%d",
axdev->fw_version[0], axdev->fw_version[1],
axdev->fw_version[2], axdev->fw_version[3]);
}
#else
static size_t ax_strscpy(char *dest, const char *src, size_t size)
{
size_t len = strnlen(src, size) + 1;
if(len > size) {
if (size)
dest[0] = '\0';
return 0;
}
memcpy(dest, src, len);
return len;
}
void ax_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
{
struct ax_device *axdev = netdev_priv(net);
ax_strscpy(info->driver, MODULENAME, sizeof(info->driver));
ax_strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
usb_make_path(axdev->udev, info->bus_info, sizeof(info->bus_info));
sprintf(info->fw_version, "v%d.%d.%d.%d",
axdev->fw_version[0], axdev->fw_version[1],
axdev->fw_version[2], axdev->fw_version[3]);
}
#endif
#if KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE
int ax_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
{
struct ax_device *axdev = netdev_priv(netdev);
int ret;
if (!axdev->mii.mdio_read)
return -EOPNOTSUPP;
ret = usb_autopm_get_interface(axdev->intf);
if (ret < 0)
return ret;
mutex_lock(&axdev->control);
mii_ethtool_gset(&axdev->mii, cmd);
mutex_unlock(&axdev->control);
usb_autopm_put_interface(axdev->intf);
return 0;
}
int ax_set_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
{
struct ax_device *axdev = netdev_priv(netdev);
int ret;
ret = usb_autopm_get_interface(axdev->intf);
if (ret < 0)
return ret;
mutex_lock(&axdev->control);
mii_ethtool_sset(&axdev->mii, cmd);
mutex_unlock(&axdev->control);
usb_autopm_put_interface(axdev->intf);
return 0;
}
#else
int ax_get_link_ksettings(struct net_device *netdev,
struct ethtool_link_ksettings *cmd)
{
struct ax_device *axdev = netdev_priv(netdev);
int ret;
if (!axdev->mii.mdio_read)
return -EOPNOTSUPP;
ret = usb_autopm_get_interface(axdev->intf);
if (ret < 0)
return ret;
mutex_lock(&axdev->control);
mii_ethtool_get_link_ksettings(&axdev->mii, cmd);
#ifdef ENABLE_AX88279
printk("============AX88279==========");
if (axdev->chip_version == AX_VERSION_AX88279) {
#if KERNEL_VERSION(5, 0, 0) <= LINUX_VERSION_CODE
linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.supported, 1);
linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.advertising,
1);
linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.lp_advertising,
1);
#else
__set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.supported);
__set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.advertising);
__set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
cmd->link_modes.lp_advertising);
#endif
if (axdev->intr_link_info.eth_speed == ETHER_LINK_2500)
cmd->base.speed = SPEED_2500;
}
#endif
mutex_unlock(&axdev->control);
usb_autopm_put_interface(axdev->intf);
return 0;
}
int ax_set_link_ksettings(struct net_device *netdev,
const struct ethtool_link_ksettings *cmd)
{
struct ax_device *axdev = netdev_priv(netdev);
int ret;
ret = usb_autopm_get_interface(axdev->intf);
if (ret < 0)
return ret;
mutex_lock(&axdev->control);
mii_ethtool_set_link_ksettings(&axdev->mii, cmd);
mutex_unlock(&axdev->control);
usb_autopm_put_interface(axdev->intf);
return 0;
}
#endif
u32 ax_get_msglevel(struct net_device *netdev)
{
struct ax_device *axdev = netdev_priv(netdev);
return axdev->msg_enable;
}
void ax_set_msglevel(struct net_device *netdev, u32 value)
{
struct ax_device *axdev = netdev_priv(netdev);
axdev->msg_enable = value;
}
void ax_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wolinfo)
{
struct ax_device *axdev = netdev_priv(netdev);
u8 reg8;
int ret;
ret = ax_read_cmd(axdev, AX_ACCESS_MAC, AX_MONITOR_MODE,
1, 1, ®8, 0);
if (ret < 0) {
wolinfo->supported = 0;
wolinfo->wolopts = 0;
return;
}
wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
if (reg8 & AX_MONITOR_MODE_RWLC)
wolinfo->wolopts |= WAKE_PHY;
if (reg8 & AX_MONITOR_MODE_RWMP)
wolinfo->wolopts |= WAKE_MAGIC;
}
int ax_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wolinfo)
{
struct ax_device *axdev = netdev_priv(netdev);
u8 reg8 = 0;
int ret;
if (wolinfo->wolopts & WAKE_PHY)
reg8 |= AX_MONITOR_MODE_RWLC;
else
reg8 &= ~AX_MONITOR_MODE_RWLC;
if (wolinfo->wolopts & WAKE_MAGIC)
reg8 |= AX_MONITOR_MODE_RWMP;
else
reg8 &= ~AX_MONITOR_MODE_RWMP;
ret = ax_write_cmd(axdev, AX_ACCESS_MAC, AX_MONITOR_MODE, 1, 1, ®8);
if (ret < 0)
return ret;
return 0;
}
static const char ax_gstrings[][ETH_GSTRING_LEN] = {
"tx_packets",
"rx_packets",
"tx_bytes",
"rx_bytes",
"tx_dropped",
"rx_length_errors",
"rx_crc_errors",
"rx_dropped",
"buikin_complete",
"bulkin_error",
"bulkout_complete",
"bulkout_error",
"bulkint_complete",
"bulkint_error",
#ifdef ENABLE_QUEUE_PRIORITY
"ep5_count",
"ep3_count",
#endif
#ifdef ENABLE_MACSEC_FUNC
"macsec_rx_in_pkts",
"macsec_rx_out_pkts",
"macsec_rx_ain_dec_pkts",
"macsec_rx_ain_byp_pkts",
"macsec_rx_ain_drp_pkts",
"macsec_rx_icv_fail_pkts",
"macsec_rx_icv_pass_pkts",
"macsec_rx_ctl",
"macsec_rx_untag",
"macsec_rx_sc",
"macsec_rx_nosc",
"macsec_rx_sc_null",
"macsec_rx_sc_untag",
"macsec_rx_sc_invalid_tag",
"macsec_rx_sc_nosc",
"macsec_rx_sc_dis",
"macsec_rx_sc_dec",
"macsec_tx_in_pkts",
"macsec_tx_out_pkts",
"macsec_tx_untag_pkts",
"macsec_tx_too_long_pkts",
"macsec_tx_enc_pkts",
"macsec_tx_byp_pkts",
"macsec_tx_drp_pkts",
"macsec_tx_ctl",
"macsec_tx_sc",
"macsec_tx_nosc",
#endif
};
int ax_get_sset_count(struct net_device *netdev, int sset)
{
#ifdef ENABLE_MACSEC_FUNC
struct ax_device *axdev = netdev_priv(netdev);
#endif
switch (sset) {
case ETH_SS_STATS:
#ifdef ENABLE_MACSEC_FUNC
if (axdev->chip_version >= AX_VERSION_AX88279)
return ARRAY_SIZE(ax_gstrings);
else
return ARRAY_SIZE(ax_gstrings) - 27;
#else
return ARRAY_SIZE(ax_gstrings);
#endif
default:
return -EOPNOTSUPP;
}
}
void ax_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
{
switch (stringset) {
case ETH_SS_STATS:
memcpy(data, ax_gstrings, sizeof(ax_gstrings));
break;
}
}
void ax_get_ethtool_stats(struct net_device *netdev,
struct ethtool_stats *stats, u64 *data)
{
struct net_device_stats *net_stats = ax_get_stats(netdev);
struct ax_device *axdev = netdev_priv(netdev);
u64 *temp = data;
*temp++ = net_stats->tx_packets;
*temp++ = net_stats->rx_packets;
*temp++ = net_stats->tx_bytes;
*temp++ = net_stats->rx_bytes;
*temp++ = net_stats->tx_dropped;
*temp++ = net_stats->rx_length_errors;
*temp++ = net_stats->rx_crc_errors;
*temp++ = net_stats->rx_dropped;
*temp++ = axdev->bulkin_complete;
*temp++ = axdev->bulkin_error;
*temp++ = axdev->bulkout_complete;
*temp++ = axdev->bulkout_error;
*temp++ = axdev->bulkint_complete;
*temp++ = axdev->bulkint_error;
#ifdef ENABLE_QUEUE_PRIORITY
*temp++ = axdev->ep5_count;
*temp++ = axdev->ep3_count;
#endif
#ifdef ENABLE_AX88279
#ifdef ENABLE_MACSEC_FUNC
if (axdev->chip_version >= AX_VERSION_AX88279) {
u32 *macsec_dbg_count;
int i;
ax_get_rx_dbg_count(axdev);
ax_get_tx_dbg_count(axdev);
macsec_dbg_count = (u32 *)&axdev->macsec_cfg->rx_dbg_count;
for (i = 0; i < (AX_MACSEC_RX_DBG_CNT_SIZE / sizeof(u32)); i++)
*temp++ = macsec_dbg_count[i];
macsec_dbg_count = (u32 *)&axdev->macsec_cfg->tx_dbg_count;
for (i = 0; i < (AX_MACSEC_TX_DBG_CNT_SIZE / sizeof(u32)); i++)
*temp++ = macsec_dbg_count[i];
}
#endif
#endif
}
void ax_get_pauseparam(struct net_device *netdev,
struct ethtool_pauseparam *pause)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 bmcr, lcladv, rmtadv;
u8 cap;
if (usb_autopm_get_interface(axdev->intf) < 0)
return;
bmcr = ax_mdio_read(netdev, axdev->mii.phy_id, MII_BMCR);
lcladv = ax_mdio_read(netdev, axdev->mii.phy_id, MII_ADVERTISE);
rmtadv = ax_mdio_read(netdev, axdev->mii.phy_id, MII_LPA);
usb_autopm_put_interface(axdev->intf);
if (!(bmcr & BMCR_ANENABLE)) {
pause->autoneg = 0;
pause->rx_pause = 0;
pause->tx_pause = 0;
return;
}
pause->autoneg = 1;
cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
if (cap & FLOW_CTRL_RX)
pause->rx_pause = 1;
if (cap & FLOW_CTRL_TX)
pause->tx_pause = 1;
}
int ax_set_pauseparam(struct net_device *netdev,
struct ethtool_pauseparam *pause)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 old, new1, bmcr;
u8 cap = 0;
int ret;
ret = usb_autopm_get_interface(axdev->intf);
if (ret < 0)
return ret;
mutex_lock(&axdev->control);
bmcr = ax_mdio_read(netdev, axdev->mii.phy_id, MII_BMCR);
if (pause->autoneg && !(bmcr & BMCR_ANENABLE)) {
ret = -EINVAL;
goto out;
}
if (pause->rx_pause)
cap |= FLOW_CTRL_RX;
if (pause->tx_pause)
cap |= FLOW_CTRL_TX;
old = ax_mdio_read(netdev, axdev->mii.phy_id, MII_ADVERTISE);
new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) |
mii_advertise_flowctrl(cap);
if (old != new1)
ax_mdio_write(netdev, axdev->mii.phy_id, MII_ADVERTISE, new1);
mii_nway_restart(&axdev->mii);
out:
mutex_unlock(&axdev->control);
usb_autopm_put_interface(axdev->intf);
return ret;
}
int ax_get_regs_len(struct net_device *netdev)
{
return 256;
}
void ax_get_regs(struct net_device *netdev,
struct ethtool_regs *regs, void *buf)
{
u8 *data = (u8 *)buf;
int i;
struct ax_device *axdev = netdev_priv(netdev);
for (i = 0; i < 256; i++)
ax_read_cmd(axdev, AX_ACCESS_MAC, i, 1, 1, &data[i], 0);
}
static int __ax_usb_read_cmd(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, void *data, u16 size)
{
void *buf = NULL;
int err = -ENOMEM;
if (size) {
buf = kzalloc(size, GFP_KERNEL);
if (!buf)
goto out;
}
err = usb_control_msg(axdev->udev, usb_rcvctrlpipe(axdev->udev, 0),
cmd, reqtype, value, index, buf, size,
USB_CTRL_GET_TIMEOUT);
if (err > 0 && err <= size) {
if (data)
memcpy(data, buf, err);
else
netdev_dbg(axdev->netdev,
"Huh? Data requested but thrown away.\n");
}
kfree(buf);
out:
return err;
}
static int __ax_usb_write_cmd(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, const void *data, u16 size)
{
void *buf = NULL;
int err = -ENOMEM;
if (data) {
buf = kmemdup(data, size, GFP_KERNEL);
if (!buf)
goto out;
} else {
if (size) {
WARN_ON_ONCE(1);
err = -EINVAL;
goto out;
}
}
err = usb_control_msg(axdev->udev, usb_sndctrlpipe(axdev->udev, 0),
cmd, reqtype, value, index, buf, size,
USB_CTRL_SET_TIMEOUT);
kfree(buf);
out:
return err;
}
static int __ax_read_cmd(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, void *data, u16 size)
{
int ret;
if (usb_autopm_get_interface(axdev->intf) < 0)
return -ENODEV;
ret = __ax_usb_read_cmd(axdev, cmd, reqtype, value, index,
data, size);
usb_autopm_put_interface(axdev->intf);
return ret;
}
static int __ax_write_cmd(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, const void *data, u16 size)
{
int ret;
if (usb_autopm_get_interface(axdev->intf) < 0)
return -ENODEV;
ret = __ax_usb_write_cmd(axdev, cmd, reqtype, value, index,
data, size);
usb_autopm_put_interface(axdev->intf);
return ret;
}
static int __ax_read_cmd_nopm(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, void *data, u16 size)
{
return __ax_usb_read_cmd(axdev, cmd, reqtype, value, index,
data, size);
}
static int __ax_write_cmd_nopm(struct ax_device *axdev, u8 cmd, u8 reqtype,
u16 value, u16 index, const void *data,
u16 size)
{
return __ax_usb_write_cmd(axdev, cmd, reqtype, value, index,
data, size);
}
static int __asix_read_cmd(struct ax_device *axdev, u8 cmd, u16 value,
u16 index, u16 size, void *data, int in_pm)
{
int ret;
_usb_read_function fn;
if (!in_pm)
fn = __ax_read_cmd;
else
fn = __ax_read_cmd_nopm;
ret = fn(axdev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
USB_RECIP_DEVICE, value, index, data, size);
if (unlikely(ret < 0))
dev_warn(&axdev->intf->dev,
"Failed to read reg %04X_%04X_%04X_%04X (err %d)",
cmd, value, index, size, ret);
return ret;
}
static int __asix_write_cmd(struct ax_device *axdev, u8 cmd, u16 value,
u16 index, u16 size, void *data, int in_pm)
{
int ret;
_usb_write_function fn;
if (!in_pm)
fn = __ax_write_cmd;
else
fn = __ax_write_cmd_nopm;
ret = fn(axdev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
USB_RECIP_DEVICE, value, index, data, size);
if (unlikely(ret < 0))
dev_warn(&axdev->intf->dev,
"Failed to write reg %04X_%04X_%04X_%04X (err %d)",
cmd, value, index, size, ret);
return ret;
}
int ax_read_cmd_nopm(struct ax_device *dev, u8 cmd, u16 value,
u16 index, u16 size, void *data, int eflag)
{
int ret;
if (eflag && (size == 2)) {
u16 buf = 0;
ret = __asix_read_cmd(dev, cmd, value, index, size, &buf, 1);
le16_to_cpus(&buf);
*((u16 *)data) = buf;
} else if (eflag && (size == 4)) {
u32 buf = 0;
ret = __asix_read_cmd(dev, cmd, value, index, size, &buf, 1);
le32_to_cpus(&buf);
*((u32 *)data) = buf;
} else {
ret = __asix_read_cmd(dev, cmd, value, index, size, data, 1);
}
return ret;
}
int ax_write_cmd_nopm(struct ax_device *dev, u8 cmd, u16 value,
u16 index, u16 size, void *data)
{
int ret;
if (size == 2) {
u16 buf = 0;
buf = *((u16 *)data);
cpu_to_le16s(&buf);
ret = __asix_write_cmd(dev, cmd, value, index,
size, &buf, 1);
} else {
ret = __asix_write_cmd(dev, cmd, value, index,
size, data, 1);
}
return ret;
}
int ax_read_cmd(struct ax_device *dev, u8 cmd, u16 value, u16 index, u16 size,
void *data, int eflag)
{
int ret;
if (eflag && (size == 2)) {
u16 buf = 0;
ret = __asix_read_cmd(dev, cmd, value, index, size, &buf, 0);
le16_to_cpus(&buf);
*((u16 *)data) = buf;
} else if (eflag && (size == 4)) {
u32 buf = 0;
ret = __asix_read_cmd(dev, cmd, value, index, size, &buf, 0);
le32_to_cpus(&buf);
*((u32 *)data) = buf;
} else {
ret = __asix_read_cmd(dev, cmd, value, index, size, data, 0);
}
return ret;
}
int ax_write_cmd(struct ax_device *dev, u8 cmd, u16 value, u16 index, u16 size,
void *data)
{
int ret;
if (size == 2) {
u16 buf = 0;
buf = *((u16 *)data);
cpu_to_le16s(&buf);
ret = __asix_write_cmd(dev, cmd, value, index,
size, &buf, 0);
} else {
ret = __asix_write_cmd(dev, cmd, value, index,
size, data, 0);
}
return ret;
}
#if KERNEL_VERSION(2, 6, 20) > LINUX_VERSION_CODE
static void ax_async_write_callback(struct urb *urb, struct pt_regs *regs)
#else
static void ax_async_write_callback(struct urb *urb)
#endif
{
struct _async_cmd_handle *asyncdata = (typeof(asyncdata))urb->context;
if (urb->status < 0)
dev_err(&asyncdata->axdev->intf->dev,
"ax_async_write_callback() failed with %d",
urb->status);
kfree(asyncdata->req);
kfree(asyncdata);
usb_free_urb(urb);
}
int ax_write_cmd_async(struct ax_device *axdev, u8 cmd, u16 value, u16 index,
u16 size, void *data)
{
struct usb_ctrlrequest *req = NULL;
int status = 0;
struct urb *urb = NULL;
void *buf = NULL;
struct _async_cmd_handle *asyncdata = NULL;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (urb == NULL) {
netdev_err(axdev->netdev,
"Error allocating URB in %s!", __func__);
return -ENOMEM;
}
req = kzalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
if (!req) {
usb_free_urb(urb);
return -ENOMEM;
}
asyncdata = kzalloc(sizeof(struct _async_cmd_handle), GFP_ATOMIC);
if (asyncdata == NULL) {
kfree(req);
usb_free_urb(urb);
return -ENOMEM;
}
asyncdata->req = req;
asyncdata->axdev = axdev;
if (size == 2) {
asyncdata->rxctl = *((u16 *)data);
cpu_to_le16s(&asyncdata->rxctl);
buf = &asyncdata->rxctl;
} else {
memcpy(asyncdata->m_filter, data, size);
buf = asyncdata->m_filter;
}
req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
req->bRequest = cmd;
req->wValue = cpu_to_le16(value);
req->wIndex = cpu_to_le16(index);
req->wLength = cpu_to_le16(size);
usb_fill_control_urb(urb, axdev->udev, usb_sndctrlpipe(axdev->udev, 0),
(void *)req, buf, size, ax_async_write_callback,
asyncdata);
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status < 0) {
netdev_err(axdev->netdev,
"Error submitting the control message: status=%d",
status);
kfree(req);
kfree(asyncdata);
usb_free_urb(urb);
}
return status;
}
int ax_mmd_read(struct net_device *netdev, int dev_addr, int reg)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 res = 0;
ax_read_cmd(axdev, AX88179A_PHY_CLAUSE45, (__u16)dev_addr,
(__u16)reg, 2, &res, 1);
return res;
}
void ax_mmd_write(struct net_device *netdev, int dev_addr, int reg, int val)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 res = (u16)val;
ax_write_cmd(axdev, AX88179A_PHY_CLAUSE45, (__u16)dev_addr,
(__u16)reg, 2, &res);
}
int ax_mdio_read(struct net_device *netdev, int phy_id, int reg)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 res;
ax_read_cmd_nopm(axdev, AX_ACCESS_PHY, phy_id, (__u16)reg, 2, &res, 1);
return res;
}
void ax_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
{
struct ax_device *axdev = netdev_priv(netdev);
u16 res = (u16)val;
ax_write_cmd_nopm(axdev, AX_ACCESS_PHY, phy_id, (__u16)reg, 2, &res);
}
static void ax_set_unplug(struct ax_device *axdev)
{
if (axdev->udev->state == USB_STATE_NOTATTACHED)
set_bit(AX_UNPLUG, &axdev->flags);
}
static int ax_check_tx_queue_not_empty(struct ax_device *axdev)
{
int i;
for (i = (AX_TX_QUEUE_SIZE - 1); i >= 0; i--)
if (!skb_queue_empty(&axdev->tx_queue[i]))
return i;
return -1;
}
static bool ax_check_tx_queue_len(struct ax_device *axdev)
{
int i;
for (i = 0; i < AX_TX_QUEUE_SIZE; i++)
if (skb_queue_len(&axdev->tx_queue[i]) > axdev->tx_qlen)
return true;
return false;
}
static void ax_read_bulk_callback(struct urb *urb)
{
struct net_device *netdev;
int status = urb->status;
struct rx_desc *desc;
struct ax_device *axdev;
desc = urb->context;
if (!desc)
return;
axdev = desc->context;
if (!axdev)
return;
if (test_bit(AX_UNPLUG, &axdev->flags) ||
!test_bit(AX_ENABLE, &axdev->flags))
return;
netdev = axdev->netdev;
if (!netif_carrier_ok(netdev))
return;
usb_mark_last_busy(axdev->udev);
if (status)
axdev->bulkin_error++;
else
axdev->bulkin_complete++;
switch (status) {
case 0:
if (urb->actual_length < ETH_ZLEN)
break;
spin_lock(&axdev->rx_lock);
list_add_tail(&desc->list, &axdev->rx_done);
spin_unlock(&axdev->rx_lock);
#ifdef ENABLE_RX_TASKLET
tasklet_schedule(&axdev->rx_tl);
#else
napi_schedule(&axdev->napi);
#endif
return;
case -ESHUTDOWN:
ax_set_unplug(axdev);
netif_device_detach(axdev->netdev);
return;
case -ENOENT:
return;
case -ETIME:
if (net_ratelimit())
netif_err(axdev, rx_err, netdev,
"maybe reset is needed?\n");
break;
default:
if (net_ratelimit())
netif_err(axdev, rx_err, netdev,
"RX status %d\n", status);
break;
}
ax_submit_rx(axdev, desc, GFP_ATOMIC);
}
void ax_write_bulk_callback(struct urb *urb)
{
struct net_device_stats *stats;
struct net_device *netdev;
struct tx_desc *desc;
struct ax_device *axdev;
int status = urb->status;
desc = urb->context;
if (!desc)
return;
axdev = desc->context;
if (!axdev)
return;
#ifdef ENABLE_PTP_FUNC
if (test_and_clear_bit(AX_TX_TIMESTAMPS, &desc->flags))
ax_ptp_ts_read_cmd_async(axdev);
#endif
netdev = axdev->netdev;
stats = ax_get_stats(netdev);
if (status)
axdev->bulkout_error++;
else
axdev->bulkout_complete++;
if (status) {
if (net_ratelimit())
netif_warn(axdev, tx_err, netdev,
"TX status %d\n", status);
stats->tx_errors += desc->skb_num;
} else {
stats->tx_packets += desc->skb_num;
stats->tx_bytes += desc->skb_len;
}
spin_lock(&axdev->tx_lock);
list_add_tail(&desc->list, &axdev->tx_free);
spin_unlock(&axdev->tx_lock);
usb_autopm_put_interface_async(axdev->intf);
if (!netif_carrier_ok(netdev))
return;
if (!test_bit(AX_ENABLE, &axdev->flags))
return;
if (test_bit(AX_UNPLUG, &axdev->flags))
return;
if (ax_check_tx_queue_not_empty(axdev) >= 0)
#ifdef ENABLE_TX_TASKLET
tasklet_schedule(&axdev->tx_tl);
#else
napi_schedule(&axdev->napi);
#endif
}
static void ax_intr_callback(struct urb *urb)
{
struct ax_device *axdev;
struct ax_device_int_data *event = NULL;
int status = urb->status;
int res;