summaryrefslogtreecommitdiffstats
path: root/src/drivers/net/smscusb.c
blob: 60390ce31c564e0f4e3f7d529e2a6b293e9c96f0 (plain) (blame)
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
/*
 * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ipxe/usb.h>
#include <ipxe/usbnet.h>
#include <ipxe/ethernet.h>
#include <ipxe/profile.h>
#include "smscusb.h"

/** @file
 *
 * SMSC USB Ethernet drivers
 *
 */

/** Interrupt completion profiler */
static struct profiler smscusb_intr_profiler __profiler =
	{ .name = "smscusb.intr" };

/******************************************************************************
 *
 * Register access
 *
 ******************************************************************************
 */

/**
 * Write register (without byte-swapping)
 *
 * @v smscusb		Smscusb device
 * @v address		Register address
 * @v value		Register value
 * @ret rc		Return status code
 */
int smscusb_raw_writel ( struct smscusb_device *smscusb, unsigned int address,
			 uint32_t value ) {
	int rc;

	/* Write register */
	DBGCIO ( smscusb, "SMSCUSB %p [%03x] <= %08x\n",
		 smscusb, address, le32_to_cpu ( value ) );
	if ( ( rc = usb_control ( smscusb->usb, SMSCUSB_REGISTER_WRITE, 0,
				  address, &value, sizeof ( value ) ) ) != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p could not write %03x: %s\n",
		       smscusb, address, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Read register (without byte-swapping)
 *
 * @v smscusb		SMSC USB device
 * @v address		Register address
 * @ret value		Register value
 * @ret rc		Return status code
 */
int smscusb_raw_readl ( struct smscusb_device *smscusb, unsigned int address,
			uint32_t *value ) {
	int rc;

	/* Read register */
	if ( ( rc = usb_control ( smscusb->usb, SMSCUSB_REGISTER_READ, 0,
				  address, value, sizeof ( *value ) ) ) != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p could not read %03x: %s\n",
		       smscusb, address, strerror ( rc ) );
		return rc;
	}
	DBGCIO ( smscusb, "SMSCUSB %p [%03x] => %08x\n",
		 smscusb, address, le32_to_cpu ( *value ) );

	return 0;
}

/******************************************************************************
 *
 * EEPROM access
 *
 ******************************************************************************
 */

/**
 * Wait for EEPROM to become idle
 *
 * @v smscusb		SMSC USB device
 * @v e2p_base		E2P register base
 * @ret rc		Return status code
 */
static int smscusb_eeprom_wait ( struct smscusb_device *smscusb,
				 unsigned int e2p_base ) {
	uint32_t e2p_cmd;
	unsigned int i;
	int rc;

	/* Wait for EPC_BSY to become clear */
	for ( i = 0 ; i < SMSCUSB_EEPROM_MAX_WAIT_MS ; i++ ) {

		/* Read E2P_CMD and check EPC_BSY */
		if ( ( rc = smscusb_readl ( smscusb,
					    ( e2p_base + SMSCUSB_E2P_CMD ),
					    &e2p_cmd ) ) != 0 )
			return rc;
		if ( ! ( e2p_cmd & SMSCUSB_E2P_CMD_EPC_BSY ) )
			return 0;

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( smscusb, "SMSCUSB %p timed out waiting for EEPROM\n",
	       smscusb );
	return -ETIMEDOUT;
}

/**
 * Read byte from EEPROM
 *
 * @v smscusb		SMSC USB device
 * @v e2p_base		E2P register base
 * @v address		EEPROM address
 * @ret byte		Byte read, or negative error
 */
static int smscusb_eeprom_read_byte ( struct smscusb_device *smscusb,
				      unsigned int e2p_base,
				      unsigned int address ) {
	uint32_t e2p_cmd;
	uint32_t e2p_data;
	int rc;

	/* Wait for EEPROM to become idle */
	if ( ( rc = smscusb_eeprom_wait ( smscusb, e2p_base ) ) != 0 )
		return rc;

	/* Initiate read command */
	e2p_cmd = ( SMSCUSB_E2P_CMD_EPC_BSY | SMSCUSB_E2P_CMD_EPC_CMD_READ |
		    SMSCUSB_E2P_CMD_EPC_ADDR ( address ) );
	if ( ( rc = smscusb_writel ( smscusb, ( e2p_base + SMSCUSB_E2P_CMD ),
				     e2p_cmd ) ) != 0 )
		return rc;

	/* Wait for command to complete */
	if ( ( rc = smscusb_eeprom_wait ( smscusb, e2p_base ) ) != 0 )
		return rc;

	/* Read EEPROM data */
	if ( ( rc = smscusb_readl ( smscusb, ( e2p_base + SMSCUSB_E2P_DATA ),
				    &e2p_data ) ) != 0 )
		return rc;

	return SMSCUSB_E2P_DATA_GET ( e2p_data );
}

/**
 * Read data from EEPROM
 *
 * @v smscusb		SMSC USB device
 * @v e2p_base		E2P register base
 * @v address		EEPROM address
 * @v data		Data buffer
 * @v len		Length of data
 * @ret rc		Return status code
 */
static int smscusb_eeprom_read ( struct smscusb_device *smscusb,
				 unsigned int e2p_base, unsigned int address,
				 void *data, size_t len ) {
	uint8_t *bytes;
	int byte;

	/* Read bytes */
	for ( bytes = data ; len-- ; address++, bytes++ ) {
		byte = smscusb_eeprom_read_byte ( smscusb, e2p_base, address );
		if ( byte < 0 )
			return byte;
		*bytes = byte;
	}

	return 0;
}

/**
 * Fetch MAC address from EEPROM
 *
 * @v smscusb		SMSC USB device
 * @v e2p_base		E2P register base
 * @ret rc		Return status code
 */
int smscusb_eeprom_fetch_mac ( struct smscusb_device *smscusb,
			       unsigned int e2p_base ) {
	struct net_device *netdev = smscusb->netdev;
	int rc;

	/* Read MAC address from EEPROM */
	if ( ( rc = smscusb_eeprom_read ( smscusb, e2p_base, SMSCUSB_EEPROM_MAC,
					  netdev->hw_addr, ETH_ALEN ) ) != 0 )
		return rc;

	/* Check that EEPROM is physically present */
	if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
		DBGC ( smscusb, "SMSCUSB %p has no EEPROM MAC (%s)\n",
		       smscusb, eth_ntoa ( netdev->hw_addr ) );
		return -ENODEV;
	}

	DBGC ( smscusb, "SMSCUSB %p using EEPROM MAC %s\n",
	       smscusb, eth_ntoa ( netdev->hw_addr ) );
	return 0;
}

/******************************************************************************
 *
 * OTP access
 *
 ******************************************************************************
 */

/**
 * Power up OTP
 *
 * @v smscusb		SMSC USB device
 * @v otp_base		OTP register base
 * @ret rc		Return status code
 */
static int smscusb_otp_power_up ( struct smscusb_device *smscusb,
				  unsigned int otp_base ) {
	uint32_t otp_power;
	unsigned int i;
	int rc;

	/* Power up OTP */
	if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_POWER ),
				     0 ) ) != 0 )
		return rc;

	/* Wait for OTP_POWER_DOWN to become clear */
	for ( i = 0 ; i < SMSCUSB_OTP_MAX_WAIT_MS ; i++ ) {

		/* Read OTP_POWER and check OTP_POWER_DOWN */
		if ( ( rc = smscusb_readl ( smscusb,
					    ( otp_base + SMSCUSB_OTP_POWER ),
					    &otp_power ) ) != 0 )
			return rc;
		if ( ! ( otp_power & SMSCUSB_OTP_POWER_DOWN ) )
			return 0;

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( smscusb, "SMSCUSB %p timed out waiting for OTP power up\n",
	       smscusb );
	return -ETIMEDOUT;
}

/**
 * Wait for OTP to become idle
 *
 * @v smscusb		SMSC USB device
 * @v otp_base		OTP register base
 * @ret rc		Return status code
 */
static int smscusb_otp_wait ( struct smscusb_device *smscusb,
			      unsigned int otp_base ) {
	uint32_t otp_status;
	unsigned int i;
	int rc;

	/* Wait for OTP_STATUS_BUSY to become clear */
	for ( i = 0 ; i < SMSCUSB_OTP_MAX_WAIT_MS ; i++ ) {

		/* Read OTP_STATUS and check OTP_STATUS_BUSY */
		if ( ( rc = smscusb_readl ( smscusb,
					    ( otp_base + SMSCUSB_OTP_STATUS ),
					    &otp_status ) ) != 0 )
			return rc;
		if ( ! ( otp_status & SMSCUSB_OTP_STATUS_BUSY ) )
			return 0;

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( smscusb, "SMSCUSB %p timed out waiting for OTP\n",
	       smscusb );
	return -ETIMEDOUT;
}

/**
 * Read byte from OTP
 *
 * @v smscusb		SMSC USB device
 * @v otp_base		OTP register base
 * @v address		OTP address
 * @ret byte		Byte read, or negative error
 */
static int smscusb_otp_read_byte ( struct smscusb_device *smscusb,
				   unsigned int otp_base,
				   unsigned int address ) {
	uint8_t addrh = ( address >> 8 );
	uint8_t addrl = ( address >> 0 );
	uint32_t otp_data;
	int rc;

	/* Wait for OTP to become idle */
	if ( ( rc = smscusb_otp_wait ( smscusb, otp_base ) ) != 0 )
		return rc;

	/* Initiate read command */
	if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_ADDRH ),
				     addrh ) ) != 0 )
		return rc;
	if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_ADDRL ),
				     addrl ) ) != 0 )
		return rc;
	if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_CMD ),
				     SMSCUSB_OTP_CMD_READ ) ) != 0 )
		return rc;
	if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_GO ),
				     SMSCUSB_OTP_GO_GO ) ) != 0 )
		return rc;

	/* Wait for command to complete */
	if ( ( rc = smscusb_otp_wait ( smscusb, otp_base ) ) != 0 )
		return rc;

	/* Read OTP data */
	if ( ( rc = smscusb_readl ( smscusb, ( otp_base + SMSCUSB_OTP_DATA ),
				    &otp_data ) ) != 0 )
		return rc;

	return SMSCUSB_OTP_DATA_GET ( otp_data );
}

/**
 * Read data from OTP
 *
 * @v smscusb		SMSC USB device
 * @v otp_base		OTP register base
 * @v address		OTP address
 * @v data		Data buffer
 * @v len		Length of data
 * @ret rc		Return status code
 */
static int smscusb_otp_read ( struct smscusb_device *smscusb,
			      unsigned int otp_base, unsigned int address,
			      void *data, size_t len ) {
	uint8_t *bytes;
	int byte;
	int rc;

	/* Power up OTP */
	if ( ( rc = smscusb_otp_power_up ( smscusb, otp_base ) ) != 0 )
		return rc;

	/* Read bytes */
	for ( bytes = data ; len-- ; address++, bytes++ ) {
		byte = smscusb_otp_read_byte ( smscusb, otp_base, address );
		if ( byte < 0 )
			return byte;
		*bytes = byte;
	}

	return 0;
}

/**
 * Fetch MAC address from OTP
 *
 * @v smscusb		SMSC USB device
 * @v otp_base		OTP register base
 * @ret rc		Return status code
 */
int smscusb_otp_fetch_mac ( struct smscusb_device *smscusb,
			    unsigned int otp_base ) {
	struct net_device *netdev = smscusb->netdev;
	uint8_t signature;
	unsigned int address;
	int rc;

	/* Read OTP signature byte */
	if ( ( rc = smscusb_otp_read ( smscusb, otp_base, 0, &signature,
				       sizeof ( signature ) ) ) != 0 )
		return rc;

	/* Determine location of MAC address */
	switch ( signature ) {
	case SMSCUSB_OTP_1_SIG:
		address = SMSCUSB_OTP_1_MAC;
		break;
	case SMSCUSB_OTP_2_SIG:
		address = SMSCUSB_OTP_2_MAC;
		break;
	default:
		DBGC ( smscusb, "SMSCUSB %p unknown OTP signature %#02x\n",
		       smscusb, signature );
		return -ENOTSUP;
	}

	/* Read MAC address from OTP */
	if ( ( rc = smscusb_otp_read ( smscusb, otp_base, address,
				       netdev->hw_addr, ETH_ALEN ) ) != 0 )
		return rc;

	/* Check that OTP is valid */
	if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
		DBGC ( smscusb, "SMSCUSB %p has no layout %#02x OTP MAC (%s)\n",
		       smscusb, signature, eth_ntoa ( netdev->hw_addr ) );
		return -ENODEV;
	}

	DBGC ( smscusb, "SMSCUSB %p using layout %#02x OTP MAC %s\n",
	       smscusb, signature, eth_ntoa ( netdev->hw_addr ) );
	return 0;
}

/******************************************************************************
 *
 * MII access
 *
 ******************************************************************************
 */

/**
 * Wait for MII to become idle
 *
 * @v smscusb		SMSC USB device
 * @ret rc		Return status code
 */
static int smscusb_mii_wait ( struct smscusb_device *smscusb ) {
	unsigned int base = smscusb->mii_base;
	uint32_t mii_access;
	unsigned int i;
	int rc;

	/* Wait for MIIBZY to become clear */
	for ( i = 0 ; i < SMSCUSB_MII_MAX_WAIT_MS ; i++ ) {

		/* Read MII_ACCESS and check MIIBZY */
		if ( ( rc = smscusb_readl ( smscusb,
					    ( base + SMSCUSB_MII_ACCESS ),
					    &mii_access ) ) != 0 )
			return rc;
		if ( ! ( mii_access & SMSCUSB_MII_ACCESS_MIIBZY ) )
			return 0;

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( smscusb, "SMSCUSB %p timed out waiting for MII\n",
	       smscusb );
	return -ETIMEDOUT;
}

/**
 * Read from MII register
 *
 * @v mii		MII interface
 * @v reg		Register address
 * @ret value		Data read, or negative error
 */
static int smscusb_mii_read ( struct mii_interface *mii, unsigned int reg ) {
	struct smscusb_device *smscusb =
		container_of ( mii, struct smscusb_device, mii );
	unsigned int base = smscusb->mii_base;
	uint32_t mii_access;
	uint32_t mii_data;
	int rc;

	/* Wait for MII to become idle */
	if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
		return rc;

	/* Initiate read command */
	mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
		       SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
		       SMSCUSB_MII_ACCESS_MIIBZY );
	if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
				     mii_access ) ) != 0 )
		return rc;

	/* Wait for command to complete */
	if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
		return rc;

	/* Read MII data */
	if ( ( rc = smscusb_readl ( smscusb, ( base + SMSCUSB_MII_DATA ),
				    &mii_data ) ) != 0 )
		return rc;

	return SMSCUSB_MII_DATA_GET ( mii_data );
}

/**
 * Write to MII register
 *
 * @v mii		MII interface
 * @v reg		Register address
 * @v data		Data to write
 * @ret rc		Return status code
 */
static int smscusb_mii_write ( struct mii_interface *mii, unsigned int reg,
			       unsigned int data ) {
	struct smscusb_device *smscusb =
		container_of ( mii, struct smscusb_device, mii );
	unsigned int base = smscusb->mii_base;
	uint32_t mii_access;
	uint32_t mii_data;
	int rc;

	/* Wait for MII to become idle */
	if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
		return rc;

	/* Write MII data */
	mii_data = SMSCUSB_MII_DATA_SET ( data );
	if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_DATA ),
				     mii_data ) ) != 0 )
		return rc;

	/* Initiate write command */
	mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
		       SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
		       SMSCUSB_MII_ACCESS_MIIWNR |
		       SMSCUSB_MII_ACCESS_MIIBZY );
	if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
				     mii_access ) ) != 0 )
		return rc;

	/* Wait for command to complete */
	if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
		return rc;

	return 0;
}

/** MII operations */
struct mii_operations smscusb_mii_operations = {
	.read = smscusb_mii_read,
	.write = smscusb_mii_write,
};

/**
 * Check link status
 *
 * @v smscusb		SMSC USB device
 * @ret rc		Return status code
 */
int smscusb_mii_check_link ( struct smscusb_device *smscusb ) {
	struct net_device *netdev = smscusb->netdev;
	int intr;
	int rc;

	/* Read PHY interrupt source */
	intr = mii_read ( &smscusb->mii, smscusb->phy_source );
	if ( intr < 0 ) {
		rc = intr;
		DBGC ( smscusb, "SMSCUSB %p could not get PHY interrupt "
		       "source: %s\n", smscusb, strerror ( rc ) );
		return rc;
	}

	/* Acknowledge PHY interrupt */
	if ( ( rc = mii_write ( &smscusb->mii, smscusb->phy_source,
				intr ) ) != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p could not acknowledge PHY "
		       "interrupt: %s\n", smscusb, strerror ( rc ) );
		return rc;
	}

	/* Check link status */
	if ( ( rc = mii_check_link ( &smscusb->mii, netdev ) ) != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p could not check link: %s\n",
		       smscusb, strerror ( rc ) );
		return rc;
	}

	DBGC ( smscusb, "SMSCUSB %p link %s (intr %#04x)\n",
	       smscusb, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
	return 0;
}

/**
 * Enable PHY interrupts and update link status
 *
 * @v smscusb		SMSC USB device
 * @v phy_mask		PHY interrupt mask register
 * @v intrs		PHY interrupts to enable
 * @ret rc		Return status code
 */
int smscusb_mii_open ( struct smscusb_device *smscusb,
		       unsigned int phy_mask, unsigned int intrs ) {
	int rc;

	/* Enable PHY interrupts */
	if ( ( rc = mii_write ( &smscusb->mii, phy_mask, intrs ) ) != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p could not set PHY interrupt "
		       "mask: %s\n", smscusb, strerror ( rc ) );
		return rc;
	}

	/* Update link status */
	smscusb_mii_check_link ( smscusb );

	return 0;
}

/******************************************************************************
 *
 * Receive filtering
 *
 ******************************************************************************
 */

/**
 * Set receive address
 *
 * @v smscusb		SMSC USB device
 * @v addr_base		Receive address register base
 * @ret rc		Return status code
 */
int smscusb_set_address ( struct smscusb_device *smscusb,
			  unsigned int addr_base ) {
	struct net_device *netdev = smscusb->netdev;
	union smscusb_mac mac;
	int rc;

	/* Copy MAC address */
	memset ( &mac, 0, sizeof ( mac ) );
	memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );

	/* Write MAC address high register */
	if ( ( rc = smscusb_raw_writel ( smscusb,
					 ( addr_base + SMSCUSB_RX_ADDRH ),
					 mac.addr.h ) ) != 0 )
		return rc;

	/* Write MAC address low register */
	if ( ( rc = smscusb_raw_writel ( smscusb,
					 ( addr_base + SMSCUSB_RX_ADDRL ),
					 mac.addr.l ) ) != 0 )
		return rc;

	return 0;
}

/**
 * Set receive filter
 *
 * @v smscusb		SMSC USB device
 * @v filt_base		Receive filter register base
 * @ret rc		Return status code
 */
int smscusb_set_filter ( struct smscusb_device *smscusb,
			 unsigned int filt_base ) {
	struct net_device *netdev = smscusb->netdev;
	union smscusb_mac mac;
	int rc;

	/* Copy MAC address */
	memset ( &mac, 0, sizeof ( mac ) );
	memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
	mac.addr.h |= cpu_to_le32 ( SMSCUSB_ADDR_FILTH_VALID );

	/* Write MAC address perfect filter high register */
	if ( ( rc = smscusb_raw_writel ( smscusb,
					 ( filt_base + SMSCUSB_ADDR_FILTH(0) ),
					 mac.addr.h ) ) != 0 )
		return rc;

	/* Write MAC address perfect filter low register */
	if ( ( rc = smscusb_raw_writel ( smscusb,
					 ( filt_base + SMSCUSB_ADDR_FILTL(0) ),
					 mac.addr.l ) ) != 0 )
		return rc;

	return 0;
}

/******************************************************************************
 *
 * Endpoint operations
 *
 ******************************************************************************
 */

/**
 * Complete interrupt transfer
 *
 * @v ep		USB endpoint
 * @v iobuf		I/O buffer
 * @v rc		Completion status code
 */
static void smscusb_intr_complete ( struct usb_endpoint *ep,
				    struct io_buffer *iobuf, int rc ) {
	struct smscusb_device *smscusb =
		container_of ( ep, struct smscusb_device, usbnet.intr );
	struct net_device *netdev = smscusb->netdev;
	struct smscusb_interrupt *intr;

	/* Profile completions */
	profile_start ( &smscusb_intr_profiler );

	/* Ignore packets cancelled when the endpoint closes */
	if ( ! ep->open )
		goto done;

	/* Record USB errors against the network device */
	if ( rc != 0 ) {
		DBGC ( smscusb, "SMSCUSB %p interrupt failed: %s\n",
		       smscusb, strerror ( rc ) );
		DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
		netdev_rx_err ( netdev, NULL, rc );
		goto done;
	}

	/* Extract interrupt data */
	if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
		DBGC ( smscusb, "SMSCUSB %p malformed interrupt\n",
		       smscusb );
		DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
		netdev_rx_err ( netdev, NULL, rc );
		goto done;
	}
	intr = iobuf->data;

	/* Record interrupt status */
	smscusb->int_sts = le32_to_cpu ( intr->int_sts );
	profile_stop ( &smscusb_intr_profiler );

 done:
	/* Free I/O buffer */
	free_iob ( iobuf );
}

/** Interrupt endpoint operations */
struct usb_endpoint_driver_operations smscusb_intr_operations = {
	.complete = smscusb_intr_complete,
};

/**
 * Complete bulk OUT transfer
 *
 * @v ep		USB endpoint
 * @v iobuf		I/O buffer
 * @v rc		Completion status code
 */
static void smscusb_out_complete ( struct usb_endpoint *ep,
				   struct io_buffer *iobuf, int rc ) {
	struct smscusb_device *smscusb =
		container_of ( ep, struct smscusb_device, usbnet.out );
	struct net_device *netdev = smscusb->netdev;

	/* Report TX completion */
	netdev_tx_complete_err ( netdev, iobuf, rc );
}

/** Bulk OUT endpoint operations */
struct usb_endpoint_driver_operations smscusb_out_operations = {
	.complete = smscusb_out_complete,
};