-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathax88796c_ioctl.c
More file actions
504 lines (423 loc) · 13.9 KB
/
Copy pathax88796c_ioctl.c
File metadata and controls
504 lines (423 loc) · 13.9 KB
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
/* ASIX AX88796C SPI Fast Ethernet Linux driver */
/*
* Copyright (c) 2010 ASIX Electronics Corporation
*
* This program is free software; you can distribute it and/or modify it
* under the terms of the GNU General Public License (Version 2) as
* published by the Free Software Foundation.
*
* This program is distributed in the hope 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
*
*/
/* INCLUDE FILE DECLARATIONS */
#include "ax88796c_main.h"
#include "ax88796c_spi.h"
#include "ax88796c_ioctl.h"
/* NAMING CONSTANT DECLARATIONS */
/* GLOBAL VARIABLES DECLARATIONS */
/* LOCAL VARIABLES DECLARATIONS */
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_check_power
* Purpose: Check AX88796C power saving status
* ----------------------------------------------------------------------------
*/
u8 ax88796c_check_power (PAX_PRIVATE ax_local)
{
struct spi_status ax_status;
/* Check media link status first */
if (netif_carrier_ok (ax_local->ndev) ||
(ax_local->ps_level == AX_PS_D0) ||
(ax_local->ps_level == AX_PS_D1)) {
return 0;
}
AX_READ_STATUS (&ax_local->ax_spi, &ax_status);
if (!(ax_status.status & AX_STATUS_READY)) {
return 1;
}
return 0;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_check_power_and_wake
* Purpose: Check AX88796C power saving status
* ----------------------------------------------------------------------------
*/
u8 ax88796c_check_power_and_wake (PAX_PRIVATE ax_local)
{
struct spi_status ax_status;
unsigned long start_time;
/* Check media link status first */
if (netif_carrier_ok (ax_local->ndev) ||
(ax_local->ps_level == AX_PS_D0) ||
(ax_local->ps_level == AX_PS_D1)) {
return 0;
}
AX_READ_STATUS (&ax_local->ax_spi, &ax_status);
if (!(ax_status.status & AX_STATUS_READY)) {
/* AX88796C in power saving mode */
AX_WAKEUP (&ax_local->ax_spi);
/* Check status */
start_time = jiffies;
do {
if (time_after (jiffies, start_time + HZ/2)) {
printk (KERN_ERR
"%s: timeout waiting for wakeup"
" from power saving\n",
ax_local->ndev->name);
break;
}
AX_READ_STATUS (&ax_local->ax_spi, &ax_status);
} while (!(ax_status.status & AX_STATUS_READY));
ax88796c_set_power_saving (ax_local, AX_PS_D0);
return 1;
}
return 0;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_set_power_saving
* Purpose: Set up AX88796C power saving
* ----------------------------------------------------------------------------
*/
void ax88796c_set_power_saving (PAX_PRIVATE ax_local, u8 ps_level)
{
u16 pmm;
if (ps_level == AX_PS_D1) {
pmm = PSCR_PS_D1;
} else if (ps_level == AX_PS_D2) {
pmm = PSCR_PS_D2;
} else {
pmm = PSCR_PS_D0;
}
AX_WRITE (&ax_local->ax_spi, (AX_READ (&ax_local->ax_spi, P0_PSCR)
& PSCR_PS_MASK) | pmm, P0_PSCR);
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_mdio_read
* Purpose:
* ----------------------------------------------------------------------------
*/
int ax88796c_mdio_read(struct net_device *ndev, int phy_id, int loc)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
unsigned long start_time;
AX_WRITE (&ax_local->ax_spi, MDIOCR_RADDR (loc)
| MDIOCR_FADDR (phy_id) | MDIOCR_READ, P2_MDIOCR);
start_time = jiffies;
while ((AX_READ (&ax_local->ax_spi, P2_MDIOCR) & MDIOCR_VALID) == 0) {
if (time_after (jiffies, start_time + HZ/100)) {
return -EBUSY;
}
}
return AX_READ (&ax_local->ax_spi, P2_MDIODR);
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_mdio_write
* Purpose:
* ----------------------------------------------------------------------------
*/
void
ax88796c_mdio_write(struct net_device *ndev, int phy_id, int loc, int val)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
unsigned long start_time;
AX_WRITE (&ax_local->ax_spi, val, P2_MDIODR);
AX_WRITE (&ax_local->ax_spi,
MDIOCR_RADDR (loc) | MDIOCR_FADDR (phy_id)
| MDIOCR_WRITE, P2_MDIOCR);
start_time = jiffies;
while ((AX_READ (&ax_local->ax_spi, P2_MDIOCR) & MDIOCR_VALID) == 0) {
if (time_after (jiffies, start_time + HZ/100)) {
return;
}
}
if (loc == MII_ADVERTISE) {
AX_WRITE (&ax_local->ax_spi, (BMCR_FULLDPLX | BMCR_ANRESTART |
BMCR_ANENABLE | BMCR_SPEED100), P2_MDIODR);
AX_WRITE (&ax_local->ax_spi, (MDIOCR_RADDR (MII_BMCR) |
MDIOCR_FADDR (phy_id) | MDIOCR_WRITE),
P2_MDIOCR);
start_time = jiffies;
while ((AX_READ (&ax_local->ax_spi, P2_MDIOCR) & MDIOCR_VALID) == 0) {
if (time_after (jiffies, start_time + HZ/100)) {
return;
}
}
}
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_set_csums
* Purpose:
* ----------------------------------------------------------------------------
*/
void ax88796c_set_csums (PAX_PRIVATE ax_local)
{
if (ax_local->checksum & AX_RX_CHECKSUM) {
AX_WRITE (&ax_local->ax_spi, COERCR0_DEFAULT, P4_COERCR0);
AX_WRITE (&ax_local->ax_spi, COERCR1_DEFAULT, P4_COERCR1);
} else {
AX_WRITE (&ax_local->ax_spi, 0, P4_COERCR0);
AX_WRITE (&ax_local->ax_spi, 0, P4_COERCR1);
}
if (ax_local->checksum & AX_TX_CHECKSUM) {
AX_WRITE (&ax_local->ax_spi, COETCR0_DEFAULT, P4_COETCR0);
AX_WRITE (&ax_local->ax_spi, COETCR1_TXPPPE, P4_COETCR1);
} else {
AX_WRITE (&ax_local->ax_spi, 0, P4_COETCR0);
AX_WRITE (&ax_local->ax_spi, 0, P4_COETCR1);
}
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_get_drvinfo
* Purpose:
* ----------------------------------------------------------------------------
*/
static void ax88796c_get_drvinfo (struct net_device *ndev,
struct ethtool_drvinfo *info)
{
/* Inherit standard device info */
strncpy (info->driver, DRV_NAME, sizeof info->driver);
strncpy (info->version, DRV_VERSION, sizeof info->version);
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_get_link
* Purpose:
* ----------------------------------------------------------------------------
*/
static u32 ax88796c_get_link(struct net_device *ndev)
{
u32 link;
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
u8 power;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
link = mii_link_ok (&ax_local->mii);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return link;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_get_msglevel
* Purpose:
* ----------------------------------------------------------------------------
*/
static u32 ax88796c_get_msglevel (struct net_device *ndev)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
return ax_local->msg_enable;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_set_msglevel
* Purpose:
* ----------------------------------------------------------------------------
*/
static void ax88796c_set_msglevel (struct net_device *ndev, u32 level)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
ax_local->msg_enable = level;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_get_settings
* Purpose:
* ----------------------------------------------------------------------------
*/
static int
ax88796c_get_settings (struct net_device *ndev, struct ethtool_cmd *cmd)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
int ret;
u8 power;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ret = mii_ethtool_gset(&ax_local->mii, cmd);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return ret;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_set_settings
* Purpose:
* ----------------------------------------------------------------------------
*/
static int
ax88796c_set_settings (struct net_device *ndev, struct ethtool_cmd *cmd)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
int ret;
u8 power;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ret = mii_ethtool_sset(&ax_local->mii, cmd);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return ret;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax_nway_reset
* Purpose:
* ----------------------------------------------------------------------------
*/
static int ax88796c_nway_reset(struct net_device *ndev)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
int ret;
u8 power;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ret = mii_nway_restart(&ax_local->mii);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return ret;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_ethtool_getmsglevel
* Purpose: Exported for Ethtool to query driver message level
* ----------------------------------------------------------------------------
*/
static u32 ax88796c_ethtool_getmsglevel (struct net_device *ndev)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
return ax_local->msg_enable;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_ethtool_setmsglevel
* Purpose: Exported for Ethtool to set driver message level
* ----------------------------------------------------------------------------
*/
static void ax88796c_ethtool_setmsglevel (struct net_device *ndev, u32 level)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
ax_local->msg_enable = level;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_get_tx_csum
* Purpose: Exported for Ethtool to query transmit checksum setting
* ----------------------------------------------------------------------------
*/
static u32 ax88796c_get_tx_csum (struct net_device *ndev)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
return (ax_local->checksum & AX_TX_CHECKSUM);
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_set_tx_csum
* Purpose: Exported for Ethtool to set transmit checksum setting
* ----------------------------------------------------------------------------
*/
static int ax88796c_set_tx_csum (struct net_device *ndev, u32 val)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
u8 power;
if (val)
ax_local->checksum |= AX_TX_CHECKSUM;
else
ax_local->checksum &= ~AX_TX_CHECKSUM;
if (val)
ndev->features |= NETIF_F_HW_CSUM;
else
ndev->features &= ~NETIF_F_HW_CSUM;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ax88796c_set_csums (ax_local);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return 0;
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_get_rx_csum
* Purpose: Exported for Ethtool to query receive checksum setting
* ----------------------------------------------------------------------------
*/
static u32 ax88796c_get_rx_csum (struct net_device *ndev)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
return (ax_local->checksum & AX_RX_CHECKSUM);
}
/*
* ----------------------------------------------------------------------------
* Function Name: ax88796c_set_rx_csum
* Purpose: Exported for Ethtool to set receive checksum setting
* ----------------------------------------------------------------------------
*/
static int ax88796c_set_rx_csum (struct net_device *ndev, u32 val)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
u8 power;
if (val)
ax_local->checksum |= AX_RX_CHECKSUM;
else
ax_local->checksum &= ~AX_RX_CHECKSUM;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ax88796c_set_csums (ax_local);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return 0;
}
#endif
struct ethtool_ops ax88796c_ethtool_ops = {
.get_drvinfo = ax88796c_get_drvinfo,
.get_link = ax88796c_get_link,
.get_msglevel = ax88796c_get_msglevel,
.set_msglevel = ax88796c_set_msglevel,
.get_settings = ax88796c_get_settings,
.set_settings = ax88796c_set_settings,
.nway_reset = ax88796c_nway_reset,
.get_msglevel = ax88796c_ethtool_getmsglevel,
.set_msglevel = ax88796c_ethtool_setmsglevel,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
.get_tx_csum = ax88796c_get_tx_csum,
.set_tx_csum = ax88796c_set_tx_csum,
.get_rx_csum = ax88796c_get_rx_csum,
.set_rx_csum = ax88796c_set_rx_csum,
#endif
};
/*
* ----------------------------------------------------------------------------
* Function Name: ax_ioctl
* Purpose:
* ----------------------------------------------------------------------------
*/
int ax88796c_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
{
PAX_PRIVATE ax_local = (PAX_PRIVATE)netdev_priv(ndev);
int ret;
u8 power;
down (&ax_local->spi_lock);
power = ax88796c_check_power_and_wake (ax_local);
ret = generic_mii_ioctl (&ax_local->mii, if_mii(ifr), cmd, NULL);
if (power)
ax88796c_set_power_saving (ax_local, ax_local->ps_level);
up (&ax_local->spi_lock);
return ret;
}