소스 검색

[ncm] Use generic USB network device framework

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 년 전
부모
커밋
320e786d3d
2개의 변경된 파일58개의 추가작업 그리고 235개의 파일을 삭제
  1. 56
    211
      src/drivers/net/ncm.c
  2. 2
    24
      src/drivers/net/ncm.h

+ 56
- 211
src/drivers/net/ncm.c 파일 보기

26
 #include <ipxe/if_ether.h>
26
 #include <ipxe/if_ether.h>
27
 #include <ipxe/profile.h>
27
 #include <ipxe/profile.h>
28
 #include <ipxe/usb.h>
28
 #include <ipxe/usb.h>
29
+#include <ipxe/usbnet.h>
29
 #include "ecm.h"
30
 #include "ecm.h"
30
 #include "ncm.h"
31
 #include "ncm.h"
31
 
32
 
67
  */
68
  */
68
 static void ncm_intr_complete ( struct usb_endpoint *ep,
69
 static void ncm_intr_complete ( struct usb_endpoint *ep,
69
 				struct io_buffer *iobuf, int rc ) {
70
 				struct io_buffer *iobuf, int rc ) {
70
-	struct ncm_device *ncm = container_of ( ep, struct ncm_device, intr );
71
+	struct ncm_device *ncm = container_of ( ep, struct ncm_device,
72
+						usbnet.intr );
71
 	struct net_device *netdev = ncm->netdev;
73
 	struct net_device *netdev = ncm->netdev;
72
 	struct usb_setup_packet *message;
74
 	struct usb_setup_packet *message;
73
 	size_t len = iob_len ( iobuf );
75
 	size_t len = iob_len ( iobuf );
137
 	.complete = ncm_intr_complete,
139
 	.complete = ncm_intr_complete,
138
 };
140
 };
139
 
141
 
140
-/**
141
- * Open communications interface
142
- *
143
- * @v ncm		CDC-NCM device
144
- * @ret rc		Return status code
145
- */
146
-static int ncm_comms_open ( struct ncm_device *ncm ) {
147
-	int rc;
148
-
149
-	/* Open interrupt endpoint */
150
-	if ( ( rc = usb_endpoint_open ( &ncm->intr ) ) != 0 ) {
151
-		DBGC ( ncm, "NCM %p could not open interrupt: %s\n",
152
-		       ncm, strerror ( rc ) );
153
-		goto err_open;
154
-	}
155
-
156
-	/* Refill interrupt endpoint */
157
-	usb_refill ( &ncm->intr );
158
-
159
-	return 0;
160
-
161
-	usb_endpoint_close ( &ncm->intr );
162
- err_open:
163
-	return rc;
164
-}
165
-
166
-/**
167
- * Close communications interface
168
- *
169
- * @v ncm		CDC-NCM device
170
- */
171
-static void ncm_comms_close ( struct ncm_device *ncm ) {
172
-
173
-	/* Close interrupt endpoint */
174
-	usb_endpoint_close ( &ncm->intr );
175
-}
176
-
177
 /******************************************************************************
142
 /******************************************************************************
178
  *
143
  *
179
  * CDC-NCM data interface
144
  * CDC-NCM data interface
214
 			count = NCM_IN_MIN_COUNT;
179
 			count = NCM_IN_MIN_COUNT;
215
 		if ( ( count * mtu ) > NCM_IN_MAX_SIZE )
180
 		if ( ( count * mtu ) > NCM_IN_MAX_SIZE )
216
 			continue;
181
 			continue;
217
-		usb_refill_init ( &ncm->in, mtu, count );
218
-		if ( ( rc = usb_prefill ( &ncm->in ) ) != 0 ) {
182
+		usb_refill_init ( &ncm->usbnet.in, mtu, count );
183
+		if ( ( rc = usb_prefill ( &ncm->usbnet.in ) ) != 0 ) {
219
 			DBGC ( ncm, "NCM %p could not prefill %dx %zd-byte "
184
 			DBGC ( ncm, "NCM %p could not prefill %dx %zd-byte "
220
 			       "buffers for bulk IN\n", ncm, count, mtu );
185
 			       "buffers for bulk IN\n", ncm, count, mtu );
221
 			continue;
186
 			continue;
239
  */
204
  */
240
 static void ncm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
205
 static void ncm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
241
 			      int rc ) {
206
 			      int rc ) {
242
-	struct ncm_device *ncm = container_of ( ep, struct ncm_device, in );
207
+	struct ncm_device *ncm = container_of ( ep, struct ncm_device,
208
+						usbnet.in );
243
 	struct net_device *netdev = ncm->netdev;
209
 	struct net_device *netdev = ncm->netdev;
244
 	struct ncm_transfer_header *nth;
210
 	struct ncm_transfer_header *nth;
245
 	struct ncm_datagram_pointer *ndp;
211
 	struct ncm_datagram_pointer *ndp;
355
 	}
321
 	}
356
 
322
 
357
 	/* Recycle I/O buffer */
323
 	/* Recycle I/O buffer */
358
-	usb_recycle ( &ncm->in, iobuf );
324
+	usb_recycle ( &ncm->usbnet.in, iobuf );
359
 	profile_stop ( &ncm_in_profiler );
325
 	profile_stop ( &ncm_in_profiler );
360
 
326
 
361
 	return;
327
 	return;
365
 	DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
331
 	DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
366
 	netdev_rx_err ( netdev, NULL, rc );
332
 	netdev_rx_err ( netdev, NULL, rc );
367
  ignore:
333
  ignore:
368
-	usb_recycle ( &ncm->in, iobuf );
334
+	usb_recycle ( &ncm->usbnet.in, iobuf );
369
 }
335
 }
370
 
336
 
371
 /** Bulk IN endpoint operations */
337
 /** Bulk IN endpoint operations */
411
 	memset ( &header->desc[1], 0, sizeof ( header->desc[1] ) );
377
 	memset ( &header->desc[1], 0, sizeof ( header->desc[1] ) );
412
 
378
 
413
 	/* Enqueue I/O buffer */
379
 	/* Enqueue I/O buffer */
414
-	if ( ( rc = usb_stream ( &ncm->out, iobuf, 0 ) ) != 0 )
380
+	if ( ( rc = usb_stream ( &ncm->usbnet.out, iobuf, 0 ) ) != 0 )
415
 		return rc;
381
 		return rc;
416
 
382
 
417
 	/* Increment sequence number */
383
 	/* Increment sequence number */
430
  */
396
  */
431
 static void ncm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
397
 static void ncm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
432
 			       int rc ) {
398
 			       int rc ) {
433
-	struct ncm_device *ncm = container_of ( ep, struct ncm_device, out );
399
+	struct ncm_device *ncm = container_of ( ep, struct ncm_device,
400
+						usbnet.out );
434
 	struct net_device *netdev = ncm->netdev;
401
 	struct net_device *netdev = ncm->netdev;
435
 
402
 
436
 	/* Report TX completion */
403
 	/* Report TX completion */
442
 	.complete = ncm_out_complete,
409
 	.complete = ncm_out_complete,
443
 };
410
 };
444
 
411
 
412
+/******************************************************************************
413
+ *
414
+ * Network device interface
415
+ *
416
+ ******************************************************************************
417
+ */
418
+
445
 /**
419
 /**
446
- * Open data interface
420
+ * Open network device
447
  *
421
  *
448
- * @v ncm		CDC-NCM device
422
+ * @v netdev		Network device
449
  * @ret rc		Return status code
423
  * @ret rc		Return status code
450
  */
424
  */
451
-static int ncm_data_open ( struct ncm_device *ncm ) {
425
+static int ncm_open ( struct net_device *netdev ) {
426
+	struct ncm_device *ncm = netdev->priv;
452
 	struct usb_device *usb = ncm->usb;
427
 	struct usb_device *usb = ncm->usb;
453
 	struct ncm_set_ntb_input_size size;
428
 	struct ncm_set_ntb_input_size size;
454
 	int rc;
429
 	int rc;
455
 
430
 
431
+	/* Reset sequence number */
432
+	ncm->sequence = 0;
433
+
456
 	/* Prefill I/O buffers */
434
 	/* Prefill I/O buffers */
457
 	if ( ( rc = ncm_in_prefill ( ncm ) ) != 0 )
435
 	if ( ( rc = ncm_in_prefill ( ncm ) ) != 0 )
458
 		goto err_prefill;
436
 		goto err_prefill;
459
 
437
 
460
 	/* Set maximum input size */
438
 	/* Set maximum input size */
461
 	memset ( &size, 0, sizeof ( size ) );
439
 	memset ( &size, 0, sizeof ( size ) );
462
-	size.mtu = cpu_to_le32 ( ncm->in.len );
463
-	if ( ( rc = usb_control ( usb, NCM_SET_NTB_INPUT_SIZE, 0, ncm->comms,
464
-				  &size, sizeof ( size ) ) ) != 0 ) {
440
+	size.mtu = cpu_to_le32 ( ncm->usbnet.in.len );
441
+	if ( ( rc = usb_control ( usb, NCM_SET_NTB_INPUT_SIZE, 0,
442
+				  ncm->usbnet.comms, &size,
443
+				  sizeof ( size ) ) ) != 0 ) {
465
 		DBGC ( ncm, "NCM %p could not set input size to %zd: %s\n",
444
 		DBGC ( ncm, "NCM %p could not set input size to %zd: %s\n",
466
-		       ncm, ncm->in.mtu, strerror ( rc ) );
445
+		       ncm, ncm->usbnet.in.len, strerror ( rc ) );
467
 		goto err_set_ntb_input_size;
446
 		goto err_set_ntb_input_size;
468
 	}
447
 	}
469
 
448
 
470
-	/* Select alternate setting for data interface */
471
-	if ( ( rc = usb_set_interface ( usb, ncm->data,
472
-					NCM_DATA_ALTERNATE ) ) != 0 ) {
473
-		DBGC ( ncm, "NCM %p could not set alternate interface: %s\n",
474
-		       ncm, strerror ( rc ) );
475
-		goto err_set_interface;
476
-	}
477
-
478
-	/* Open bulk IN endpoint */
479
-	if ( ( rc = usb_endpoint_open ( &ncm->in ) ) != 0 ) {
480
-		DBGC ( ncm, "NCM %p could not open bulk IN: %s\n",
481
-		       ncm, strerror ( rc ) );
482
-		goto err_open_in;
483
-	}
484
-
485
-	/* Open bulk OUT endpoint */
486
-	if ( ( rc = usb_endpoint_open ( &ncm->out ) ) != 0 ) {
487
-		DBGC ( ncm, "NCM %p could not open bulk OUT: %s\n",
449
+	/* Open USB network device */
450
+	if ( ( rc = usbnet_open ( &ncm->usbnet ) ) != 0 ) {
451
+		DBGC ( ncm, "NCM %p could not open: %s\n",
488
 		       ncm, strerror ( rc ) );
452
 		       ncm, strerror ( rc ) );
489
-		goto err_open_out;
490
-	}
491
-
492
-	/* Refill bulk IN endpoint */
493
-	if ( ( rc = usb_refill ( &ncm->in ) ) != 0 ) {
494
-		DBGC ( ncm, "NCM %p could not refill bulk IN: %s\n",
495
-		       ncm, strerror ( rc ) );
496
-		goto err_refill;
453
+		goto err_open;
497
 	}
454
 	}
498
 
455
 
499
 	return 0;
456
 	return 0;
500
 
457
 
501
- err_refill:
502
-	usb_endpoint_close ( &ncm->out );
503
- err_open_out:
504
-	usb_endpoint_close ( &ncm->in );
505
- err_open_in:
506
-	usb_set_interface ( usb, ncm->data, 0 );
507
- err_set_interface:
458
+	usbnet_close ( &ncm->usbnet );
459
+ err_open:
508
  err_set_ntb_input_size:
460
  err_set_ntb_input_size:
509
-	usb_flush ( &ncm->in );
461
+	usb_flush ( &ncm->usbnet.in );
510
  err_prefill:
462
  err_prefill:
511
 	return rc;
463
 	return rc;
512
 }
464
 }
513
 
465
 
514
-/**
515
- * Close data interface
516
- *
517
- * @v ncm		CDC-NCM device
518
- */
519
-static void ncm_data_close ( struct ncm_device *ncm ) {
520
-	struct usb_device *usb = ncm->usb;
521
-
522
-	/* Close endpoints */
523
-	usb_endpoint_close ( &ncm->out );
524
-	usb_endpoint_close ( &ncm->in );
525
-
526
-	/* Reset data interface */
527
-	usb_set_interface ( usb, ncm->data, 0 );
528
-}
529
-
530
-/******************************************************************************
531
- *
532
- * Network device interface
533
- *
534
- ******************************************************************************
535
- */
536
-
537
-/**
538
- * Open network device
539
- *
540
- * @v netdev		Network device
541
- * @ret rc		Return status code
542
- */
543
-static int ncm_open ( struct net_device *netdev ) {
544
-	struct ncm_device *ncm = netdev->priv;
545
-	int rc;
546
-
547
-	/* Reset sequence number */
548
-	ncm->sequence = 0;
549
-
550
-	/* Open communications interface */
551
-	if ( ( rc = ncm_comms_open ( ncm ) ) != 0 )
552
-		goto err_comms_open;
553
-
554
-	/* Open data interface */
555
-	if ( ( rc = ncm_data_open ( ncm ) ) != 0 )
556
-		goto err_data_open;
557
-
558
-	return 0;
559
-
560
-	ncm_data_close ( ncm );
561
- err_data_open:
562
-	ncm_comms_close ( ncm );
563
- err_comms_open:
564
-	return rc;
565
-}
566
-
567
 /**
466
 /**
568
  * Close network device
467
  * Close network device
569
  *
468
  *
572
 static void ncm_close ( struct net_device *netdev ) {
471
 static void ncm_close ( struct net_device *netdev ) {
573
 	struct ncm_device *ncm = netdev->priv;
472
 	struct ncm_device *ncm = netdev->priv;
574
 
473
 
575
-	/* Close data interface */
576
-	ncm_data_close ( ncm );
577
-
578
-	/* Close communications interface */
579
-	ncm_comms_close ( ncm );
474
+	/* Close USB network device */
475
+	usbnet_close ( &ncm->usbnet );
580
 }
476
 }
581
 
477
 
582
 /**
478
 /**
610
 	/* Poll USB bus */
506
 	/* Poll USB bus */
611
 	usb_poll ( ncm->bus );
507
 	usb_poll ( ncm->bus );
612
 
508
 
613
-	/* Refill interrupt endpoint */
614
-	if ( ( rc = usb_refill ( &ncm->intr ) ) != 0 )
509
+	/* Refill endpoints */
510
+	if ( ( rc = usbnet_refill ( &ncm->usbnet ) ) != 0 )
615
 		netdev_rx_err ( netdev, NULL, rc );
511
 		netdev_rx_err ( netdev, NULL, rc );
616
 
512
 
617
-	/* Refill bulk IN endpoint */
618
-	if ( ( rc = usb_refill ( &ncm->in ) ) != 0 )
619
-		netdev_rx_err ( netdev, NULL, rc );
620
 }
513
 }
621
 
514
 
622
 /** CDC-NCM network device operations */
515
 /** CDC-NCM network device operations */
647
 	struct net_device *netdev;
540
 	struct net_device *netdev;
648
 	struct ncm_device *ncm;
541
 	struct ncm_device *ncm;
649
 	struct usb_interface_descriptor *comms;
542
 	struct usb_interface_descriptor *comms;
650
-	struct usb_interface_descriptor *data;
651
 	struct ecm_ethernet_descriptor *ethernet;
543
 	struct ecm_ethernet_descriptor *ethernet;
652
 	struct ncm_ntb_parameters params;
544
 	struct ncm_ntb_parameters params;
653
 	int rc;
545
 	int rc;
665
 	ncm->usb = usb;
557
 	ncm->usb = usb;
666
 	ncm->bus = usb->port->hub->bus;
558
 	ncm->bus = usb->port->hub->bus;
667
 	ncm->netdev = netdev;
559
 	ncm->netdev = netdev;
668
-	usb_endpoint_init ( &ncm->intr, usb, &ncm_intr_operations );
669
-	usb_endpoint_init ( &ncm->in, usb, &ncm_in_operations );
670
-	usb_endpoint_init ( &ncm->out, usb, &ncm_out_operations );
671
-	usb_refill_init ( &ncm->intr, 0, NCM_INTR_COUNT );
560
+	usbnet_init ( &ncm->usbnet, func, &ncm_intr_operations,
561
+		      &ncm_in_operations, &ncm_out_operations );
562
+	usb_refill_init ( &ncm->usbnet.intr, 0, NCM_INTR_COUNT );
672
 	DBGC ( ncm, "NCM %p on %s\n", ncm, func->name );
563
 	DBGC ( ncm, "NCM %p on %s\n", ncm, func->name );
673
 
564
 
674
-	/* Identify interfaces */
675
-	if ( func->count < NCM_INTERFACE_COUNT ) {
676
-		DBGC ( ncm, "NCM %p has only %d interfaces\n",
677
-		       ncm, func->count );
678
-		rc = -EINVAL;
679
-		goto err_count;
680
-	}
681
-	ncm->comms = func->interface[NCM_INTERFACE_COMMS];
682
-	ncm->data = func->interface[NCM_INTERFACE_DATA];
683
-
684
-	/* Locate communications interface descriptor */
685
-	comms = usb_interface_descriptor ( config, ncm->comms, 0 );
686
-	if ( ! comms ) {
687
-		DBGC ( ncm, "NCM %p has no communications interface\n", ncm );
688
-		rc = -EINVAL;
689
-		goto err_comms;
690
-	}
691
-
692
-	/* Locate data interface descriptor */
693
-	data = usb_interface_descriptor ( config, ncm->data,
694
-					  NCM_DATA_ALTERNATE );
695
-	if ( ! data ) {
696
-		DBGC ( ncm, "NCM %p has no data interface\n", ncm );
697
-		rc = -EINVAL;
698
-		goto err_data;
699
-	}
700
-
701
-	/* Describe interrupt endpoint */
702
-	if ( ( rc = usb_endpoint_described ( &ncm->intr, config, comms,
703
-					     USB_INTERRUPT, 0 ) ) != 0 ) {
704
-		DBGC ( ncm, "NCM %p could not describe interrupt endpoint: "
705
-		       "%s\n", ncm, strerror ( rc ) );
706
-		goto err_interrupt;
707
-	}
708
-
709
-	/* Describe bulk IN endpoint */
710
-	if ( ( rc = usb_endpoint_described ( &ncm->in, config, data,
711
-					     USB_BULK_IN, 0 ) ) != 0 ) {
712
-		DBGC ( ncm, "NCM %p could not describe bulk IN endpoint: "
713
-		       "%s\n", ncm, strerror ( rc ) );
714
-		goto err_bulk_in;
715
-	}
716
-
717
-	/* Describe bulk OUT endpoint */
718
-	if ( ( rc = usb_endpoint_described ( &ncm->out, config, data,
719
-					     USB_BULK_OUT, 0 ) ) != 0 ) {
720
-		DBGC ( ncm, "NCM %p could not describe bulk OUT endpoint: "
721
-		       "%s\n", ncm, strerror ( rc ) );
722
-		goto err_bulk_out;
565
+	/* Describe USB network device */
566
+	if ( ( rc = usbnet_describe ( &ncm->usbnet, config ) ) != 0 ) {
567
+		DBGC ( ncm, "NCM %p could not describe: %s\n",
568
+		       ncm, strerror ( rc ) );
569
+		goto err_describe;
723
 	}
570
 	}
724
 
571
 
725
 	/* Locate Ethernet descriptor */
572
 	/* Locate Ethernet descriptor */
573
+	comms = usb_interface_descriptor ( config, ncm->usbnet.comms, 0 );
574
+	assert ( comms != NULL );
726
 	ethernet = ecm_ethernet_descriptor ( config, comms );
575
 	ethernet = ecm_ethernet_descriptor ( config, comms );
727
 	if ( ! ethernet ) {
576
 	if ( ! ethernet ) {
728
 		DBGC ( ncm, "NCM %p has no Ethernet descriptor\n", ncm );
577
 		DBGC ( ncm, "NCM %p has no Ethernet descriptor\n", ncm );
738
 	}
587
 	}
739
 
588
 
740
 	/* Get NTB parameters */
589
 	/* Get NTB parameters */
741
-	if ( ( rc = usb_control ( usb, NCM_GET_NTB_PARAMETERS, 0, ncm->comms,
742
-				  &params, sizeof ( params ) ) ) != 0 ) {
590
+	if ( ( rc = usb_control ( usb, NCM_GET_NTB_PARAMETERS, 0,
591
+				  ncm->usbnet.comms, &params,
592
+				  sizeof ( params ) ) ) != 0 ) {
743
 		DBGC ( ncm, "NCM %p could not get NTB parameters: %s\n",
593
 		DBGC ( ncm, "NCM %p could not get NTB parameters: %s\n",
744
 		       ncm, strerror ( rc ) );
594
 		       ncm, strerror ( rc ) );
745
 		goto err_ntb_parameters;
595
 		goto err_ntb_parameters;
771
  err_ntb_parameters:
621
  err_ntb_parameters:
772
  err_fetch_mac:
622
  err_fetch_mac:
773
  err_ethernet:
623
  err_ethernet:
774
- err_bulk_out:
775
- err_bulk_in:
776
- err_interrupt:
777
- err_data:
778
- err_comms:
779
- err_count:
624
+ err_describe:
780
 	netdev_nullify ( netdev );
625
 	netdev_nullify ( netdev );
781
 	netdev_put ( netdev );
626
 	netdev_put ( netdev );
782
  err_alloc:
627
  err_alloc:

+ 2
- 24
src/drivers/net/ncm.h 파일 보기

18
 /** CDC-NCM subclass */
18
 /** CDC-NCM subclass */
19
 #define USB_SUBCLASS_CDC_NCM 0x0d
19
 #define USB_SUBCLASS_CDC_NCM 0x0d
20
 
20
 
21
-/** CDC-NCM interfaces */
22
-enum ncm_interfaces {
23
-	/** Communications interface */
24
-	NCM_INTERFACE_COMMS = 0,
25
-	/** Data interface */
26
-	NCM_INTERFACE_DATA,
27
-	NCM_INTERFACE_COUNT
28
-};
29
-
30
-/** Alternate setting for CDC-NCM data interface */
31
-#define NCM_DATA_ALTERNATE 1
32
-
33
 /** Get NTB parameters */
21
 /** Get NTB parameters */
34
 #define NCM_GET_NTB_PARAMETERS						\
22
 #define NCM_GET_NTB_PARAMETERS						\
35
 	( USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
23
 	( USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
147
 	struct usb_bus *bus;
135
 	struct usb_bus *bus;
148
 	/** Network device */
136
 	/** Network device */
149
 	struct net_device *netdev;
137
 	struct net_device *netdev;
150
-
151
-	/** Communications interface */
152
-	unsigned int comms;
153
-	/** Data interface */
154
-	unsigned int data;
138
+	/** USB network device */
139
+	struct usbnet_device usbnet;
155
 
140
 
156
 	/** Maximum supported NTB input size */
141
 	/** Maximum supported NTB input size */
157
 	size_t mtu;
142
 	size_t mtu;
159
 	uint16_t sequence;
144
 	uint16_t sequence;
160
 	/** Alignment padding required on transmitted packets */
145
 	/** Alignment padding required on transmitted packets */
161
 	size_t padding;
146
 	size_t padding;
162
-
163
-	/** Interrupt endpoint */
164
-	struct usb_endpoint intr;
165
-	/** Bulk IN endpoint */
166
-	struct usb_endpoint in;
167
-	/** Bulk OUT endpoint */
168
-	struct usb_endpoint out;
169
 };
147
 };
170
 
148
 
171
 /** Bulk IN ring minimum buffer count
149
 /** Bulk IN ring minimum buffer count

Loading…
취소
저장