summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb/udbp.c
blob: a7bcac0b8426f5fa1f5f0d5855c517b9d6908fbc (plain)
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
/*
 * Copyright (c) 1996-2000 Whistle Communications, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of author nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

/* Driver for arbitrary double bulk pipe devices.
 * The driver assumes that there will be the same driver on the other side.
 *
 * XXX Some more information on what the framing of the IP packets looks like.
 *
 * To take full advantage of bulk transmission, packets should be chosen
 * between 1k and 5k in size (1k to make sure the sending side starts
 * straming, and <5k to avoid overflowing the system with small TDs).
 */


/* probe/attach/detach:
 *  Connect the driver to the hardware and netgraph
 *
 * udbp_setup_out_transfer(sc);
 *  Setup an outbound transfer. Only one transmit can be active at the same
 *  time.
 *  XXX If it is required that the driver is able to queue multiple requests
 *      let me know. That is slightly difficult, due to the fact that we
 *	cannot call usbd_alloc_xfer in int context.
 *
 * udbp_setup_in_transfer(sc)
 *  Prepare an in transfer that will be waiting for data to come in. It
 *  is submitted and sits there until data is available.
 *  The callback resubmits a new transfer on completion.
 *
 *  The reason we submit a bulk in transfer is that USB does not know about
 *  interrupts. The bulk transfer continuously polls the device for data.
 *  While the device has no data available, the device NAKs the TDs. As soon
 *  as there is data, the transfer happens and the data comes flowing in.
 *
 *  In case you were wondering, interrupt transfers happen exactly that way.
 *  It therefore doesn't make sense to use the interrupt pipe to signal
 *  'data ready' and then schedule a bulk transfer to fetch it. That would
 *  incur a 2ms delay at least, without reducing bandwidth requirements.
 *  
 */
 


#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ctype.h>
#include <sys/errno.h>
#include <net/if.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbhid.h>

#include <dev/usb/usbdevs.h>


#include <netgraph/ng_message.h>
#include <netgraph/ng_parse.h>
#include <dev/usb/udbp.h>
#include <netgraph/netgraph.h>

#ifdef UDBP_DEBUG
#define DPRINTF(x)	if (udbpdebug) logprintf x
#define DPRINTFN(n,x)	if (udbpdebug>(n)) logprintf x
int	udbpdebug = 9;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

#define MS_TO_TICKS(ms) ((ms) * hz / 1000)                            

#define UDBP_TIMEOUT	2000	/* timeout on outbound transfers, in msecs */
#define UDBP_BUFFERSIZE	2048	/* maximum number of bytes in one transfer */


struct udbp_softc {
	device_t		sc_dev;		/* base device */
	usbd_interface_handle	sc_iface;

	usbd_pipe_handle	sc_bulkin_pipe;
	int			sc_bulkin;
	usbd_xfer_handle	sc_bulkin_xfer;
	void 			*sc_bulkin_buffer;
	int			sc_bulkin_bufferlen;
	int			sc_bulkin_datalen;

	usbd_pipe_handle	sc_bulkout_pipe;
	int			sc_bulkout;
	usbd_xfer_handle	sc_bulkout_xfer;
	void 			*sc_bulkout_buffer;
	int			sc_bulkout_bufferlen;
	int			sc_bulkout_datalen;

	int			flags;
#	define			DISCONNECTED		0x01
#	define			OUT_BUSY		0x02
#	define			NETGRAPH_INITIALISED	0x04
	node_p		node;		/* back pointer to node */
	hook_p  	hook;		/* pointer to the hook */
	u_int   	packets_in;	/* packets in from downstream */
	u_int   	packets_out;	/* packets out towards downstream */
	struct	ifqueue	xmitq_hipri;	/* hi-priority transmit queue */
	struct	ifqueue	xmitq;		/* low-priority transmit queue */

};
typedef struct udbp_softc *udbp_p;



Static ng_constructor_t	ng_udbp_constructor;
Static ng_rcvmsg_t	ng_udbp_rcvmsg;
Static ng_shutdown_t	ng_udbp_rmnode;
Static ng_newhook_t	ng_udbp_newhook;
Static ng_connect_t	ng_udbp_connect;
Static ng_rcvdata_t	ng_udbp_rcvdata;
Static ng_disconnect_t	ng_udbp_disconnect;

/* Parse type for struct ngudbpstat */
Static const struct ng_parse_struct_info
	ng_udbp_stat_type_info = NG_UDBP_STATS_TYPE_INFO;
Static const struct ng_parse_type ng_udbp_stat_type = {
	&ng_parse_struct_type,
	&ng_udbp_stat_type_info
};

/* List of commands and how to convert arguments to/from ASCII */
Static const struct ng_cmdlist ng_udbp_cmdlist[] = {
	{
	  NGM_UDBP_COOKIE,
	  NGM_UDBP_GET_STATUS,
	  "getstatus",
	  NULL,
	  &ng_udbp_stat_type,
	},
	{
	  NGM_UDBP_COOKIE,
	  NGM_UDBP_SET_FLAG,
	  "setflag",
	  &ng_parse_int32_type,
	  NULL
	},
	{ 0 }
};

/* Netgraph node type descriptor */
Static struct ng_type ng_udbp_typestruct = {
	NG_ABI_VERSION,
	NG_UDBP_NODE_TYPE,
	NULL,
	ng_udbp_constructor,
	ng_udbp_rcvmsg,
	ng_udbp_rmnode,
	ng_udbp_newhook,
	NULL,
	ng_udbp_connect,
	ng_udbp_rcvdata,
	ng_udbp_disconnect,
	ng_udbp_cmdlist
};

Static int udbp_setup_in_transfer	(udbp_p sc);
Static void udbp_in_transfer_cb		(usbd_xfer_handle xfer,
					usbd_private_handle priv,
					usbd_status err);

Static int udbp_setup_out_transfer	(udbp_p sc);
Static void udbp_out_transfer_cb	(usbd_xfer_handle xfer,
					usbd_private_handle priv,
					usbd_status err);

USB_DECLARE_DRIVER(udbp);

USB_MATCH(udbp)
{
	USB_MATCH_START(udbp, uaa);
	usb_interface_descriptor_t *id;
	if (!uaa->iface)
	  return (UMATCH_NONE);
	id = usbd_get_interface_descriptor(uaa->iface);

	/* XXX Julian, add the id of the device if you have one to test
	 * things with. run 'usbdevs -v' and note the 3 ID's that appear.
	 * The Vendor Id and Product Id are in hex and the Revision Id is in
	 * bcd. But as usual if the revision is 0x101 then you should compare
	 * the revision id in the device descriptor with 0x101
	 * Or go search the file usbdevs.h. Maybe the device is already in
	 * there.
	 */
	if ((uaa->vendor == USB_VENDOR_NETCHIP &&
	     uaa->product == USB_PRODUCT_NETCHIP_TURBOCONNECT))
		return(UMATCH_VENDOR_PRODUCT);

	if ((uaa->vendor == USB_VENDOR_PROLIFIC &&
	     (uaa->product == USB_PRODUCT_PROLIFIC_PL2301 ||
	      uaa->product == USB_PRODUCT_PROLIFIC_PL2302)))
		return(UMATCH_VENDOR_PRODUCT);
	
	if ((uaa->vendor == USB_VENDOR_ANCHOR &&
	     uaa->product == USB_PRODUCT_ANCHOR_EZLINK))
		return(UMATCH_VENDOR_PRODUCT);
	
	return (UMATCH_NONE);
}

USB_ATTACH(udbp)
{
	USB_ATTACH_START(udbp, sc, uaa);
	usbd_interface_handle iface = uaa->iface;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
	usbd_status err;
	char devinfo[1024];
	int i;
	static int ngudbp_done_init=0;

	sc->flags |= DISCONNECTED;
	/* fetch the interface handle for the first interface */
	(void) usbd_device2interface_handle(uaa->device, 0, &iface);
	id = usbd_get_interface_descriptor(iface);
	usbd_devinfo(uaa->device, 0, devinfo);
	USB_ATTACH_SETUP;
	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);

	/* Find the two first bulk endpoints */
	for (i = 0 ; i < id->bNumEndpoints; i++) {
		ed = usbd_interface2endpoint_descriptor(iface, i);
		if (!ed) {
			printf("%s: could not read endpoint descriptor\n",
			       USBDEVNAME(sc->sc_dev));
			USB_ATTACH_ERROR_RETURN;
		}

		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
			ed_bulkin = ed;
		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
		        ed_bulkout = ed;
		}

		if (ed_bulkin && ed_bulkout)	/* found all we need */
			break;
	}

	/* Verify that we goething sensible */
	if (ed_bulkin == NULL || ed_bulkout == NULL) {
		printf("%s: bulk-in and/or bulk-out endpoint not found\n",
			USBDEVNAME(sc->sc_dev));
		USB_ATTACH_ERROR_RETURN;
	}

	if (ed_bulkin->wMaxPacketSize[0] != ed_bulkout->wMaxPacketSize[0] ||
	   ed_bulkin->wMaxPacketSize[1] != ed_bulkout->wMaxPacketSize[1]) {
		printf("%s: bulk-in and bulk-out have different packet sizes %d %d %d %d\n",
			USBDEVNAME(sc->sc_dev),
		       ed_bulkin->wMaxPacketSize[0],
		       ed_bulkout->wMaxPacketSize[0],
		       ed_bulkin->wMaxPacketSize[1],
		       ed_bulkout->wMaxPacketSize[1]);
		USB_ATTACH_ERROR_RETURN;
	}

	sc->sc_bulkin = ed_bulkin->bEndpointAddress;
	sc->sc_bulkout = ed_bulkout->bEndpointAddress;

	DPRINTF(("%s: Bulk-in: 0x%02x, bulk-out 0x%02x, packet size = %d\n",
		USBDEVNAME(sc->sc_dev), sc->sc_bulkin, sc->sc_bulkout,
		ed_bulkin->wMaxPacketSize[0]));

	/* Allocate the in transfer struct */
	sc->sc_bulkin_xfer = usbd_alloc_xfer(uaa->device);
	if (!sc->sc_bulkin_xfer) {
		goto bad;
	}
	sc->sc_bulkout_xfer = usbd_alloc_xfer(uaa->device);
	if (!sc->sc_bulkout_xfer) {
		goto bad;
	}
	sc->sc_bulkin_buffer = malloc(UDBP_BUFFERSIZE, M_USBDEV, M_WAITOK);
	if (!sc->sc_bulkin_buffer) {
		goto bad;
	}
	sc->sc_bulkout_buffer = malloc(UDBP_BUFFERSIZE, M_USBDEV, M_WAITOK);
	if (!sc->sc_bulkout_xfer || !sc->sc_bulkout_buffer) {
		goto bad;
	}
	sc->sc_bulkin_bufferlen = UDBP_BUFFERSIZE;
	sc->sc_bulkout_bufferlen = UDBP_BUFFERSIZE;

	/* We have decided on which endpoints to use, now open the pipes */
	err = usbd_open_pipe(iface, sc->sc_bulkin,
				USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
	if (err) {
		printf("%s: cannot open bulk-in pipe (addr %d)\n",
			USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
		goto bad;
	}
	err = usbd_open_pipe(iface, sc->sc_bulkout,
				USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
	if (err) {
		printf("%s: cannot open bulk-out pipe (addr %d)\n",
			USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
		goto bad;
	}

	if (!ngudbp_done_init){
		ngudbp_done_init=1;	
		if (ng_newtype(&ng_udbp_typestruct)) {
			printf("ngudbp install failed\n");
			goto bad;
		}
	}

	if ((err = ng_make_node_common(&ng_udbp_typestruct, &sc->node)) == 0) {
		char	nodename[128];
		sprintf(nodename, "%s", USBDEVNAME(sc->sc_dev));
		if ((err = ng_name_node(sc->node, nodename))) {
			NG_NODE_UNREF(sc->node);
			sc->node = NULL;
			goto bad;
		} else {
			NG_NODE_SET_PRIVATE(sc->node, sc);
			sc->xmitq.ifq_maxlen = IFQ_MAXLEN;
			sc->xmitq_hipri.ifq_maxlen = IFQ_MAXLEN;
			mtx_init(&sc->xmitq.ifq_mtx, "usb_xmitq", MTX_DEF);
			mtx_init(&sc->xmitq_hipri.ifq_mtx,
					"usb_xmitq_hipri", MTX_DEF);
		}
	}
	sc->flags = NETGRAPH_INITIALISED;
	/* sc->flags &= ~DISCONNECTED; */ /* XXX */


	/* the device is now operational */


	/* schedule the first incoming xfer */
	err = udbp_setup_in_transfer(sc);
	if (err) {
		goto bad;
	}
	USB_ATTACH_SUCCESS_RETURN;
bad:
#if 0 /* probably done in udbp_detach() */
		if (sc->sc_bulkout_buffer) {
			FREE(sc->sc_bulkout_buffer, M_USBDEV);
		}
		if (sc->sc_bulkin_buffer) {
			FREE(sc->sc_bulkin_buffer, M_USBDEV);
		}
		if (sc->sc_bulkout_xfer) {
			usbd_free_xfer(sc->sc_bulkout_xfer);
		}
		if (sc->sc_bulkin_xfer) {
			usbd_free_xfer(sc->sc_bulkin_xfer);
		}
#endif
		udbp_detach(self);
		USB_ATTACH_ERROR_RETURN;
}


USB_DETACH(udbp)
{
	USB_DETACH_START(udbp, sc);

	sc->flags |= DISCONNECTED;

	DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));

	if (sc->sc_bulkin_pipe) {
		usbd_abort_pipe(sc->sc_bulkin_pipe);
		usbd_close_pipe(sc->sc_bulkin_pipe);
	}
	if (sc->sc_bulkout_pipe) {
		usbd_abort_pipe(sc->sc_bulkout_pipe);
		usbd_close_pipe(sc->sc_bulkout_pipe);
	}

	if (sc->flags & NETGRAPH_INITIALISED) {
		ng_rmnode_self(sc->node);
		NG_NODE_SET_PRIVATE(sc->node, NULL);
		NG_NODE_UNREF(sc->node);
		sc->node = NULL;	/* Paranoid */
	}

	if (sc->sc_bulkin_xfer)
		usbd_free_xfer(sc->sc_bulkin_xfer);
	if (sc->sc_bulkout_xfer)
		usbd_free_xfer(sc->sc_bulkout_xfer);

	if (sc->sc_bulkin_buffer)
		free(sc->sc_bulkin_buffer, M_USBDEV);
	if (sc->sc_bulkout_buffer)
		free(sc->sc_bulkout_buffer, M_USBDEV);
	return 0;
}


Static int
udbp_setup_in_transfer(udbp_p sc)
{
	void *priv = sc;	/* XXX this should probably be some pointer to
				 * struct describing the transfer (mbuf?)
				 * See also below.
				 */
	usbd_status err;

	/* XXX
	 * How should we arrange for 2 extra bytes at the start of the
	 * packet?
	 */

	/* Initialise a USB transfer and then schedule it */

	(void) usbd_setup_xfer( sc->sc_bulkin_xfer,
				sc->sc_bulkin_pipe,
				priv,
				sc->sc_bulkin_buffer,
				sc->sc_bulkin_bufferlen,
				USBD_SHORT_XFER_OK,
				USBD_NO_TIMEOUT,
				udbp_in_transfer_cb);

	err = usbd_transfer(sc->sc_bulkin_xfer);
	if (err && err != USBD_IN_PROGRESS) {
		DPRINTF(("%s: failed to setup in-transfer, %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
		return(err);
	}

	return (USBD_NORMAL_COMPLETION);
}

Static void
udbp_in_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
			usbd_status err)
{
	udbp_p 		sc = priv;		/* XXX see priv above */
	int		s;
	int		len;
	struct		mbuf *m;

	if (err) {
		if (err != USBD_CANCELLED) {
			DPRINTF(("%s: bulk-out transfer failed: %s\n",
				USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
		} else {
			/* USBD_CANCELLED happens at unload of the driver */
			return;
		}

		/* Transfer has failed, packet is not received */
	} else {

		len = xfer->actlen;
		
		s = splimp(); /* block network stuff too */
		if (sc->hook) {
			/* get packet from device and send on */
			m = m_devget(sc->sc_bulkin_buffer, len, 0, NULL, NULL);
	    		NG_SEND_DATA_ONLY(err, sc->hook, m);
		}
		splx(s);
	
	}
	/* schedule the next in transfer */
	udbp_setup_in_transfer(sc);
}


Static int
udbp_setup_out_transfer(udbp_p sc)
{
	void *priv = sc;	/* XXX this should probably be some pointer to
				 * struct describing the transfer (mbuf?)
				 * See also below.
				 */
	int pktlen;
	usbd_status err;
	int s, s1;
	struct mbuf *m;


	s = splusb();
	if (sc->flags & OUT_BUSY)
		panic("out transfer already in use, we should add queuing");
	sc->flags |= OUT_BUSY;
	splx(s);
	s1 = splimp(); /* Queueing happens at splnet */
	IF_DEQUEUE(&sc->xmitq_hipri, m);
	if (m == NULL) {
		IF_DEQUEUE(&sc->xmitq, m);
	}
	splx(s1);

	if (!m) {
		sc->flags &= ~OUT_BUSY;
		return (USBD_NORMAL_COMPLETION);
	}

	pktlen = m->m_pkthdr.len;
	if (pktlen > sc->sc_bulkout_bufferlen) {
		printf("%s: Packet too large, %d > %d\n",
			USBDEVNAME(sc->sc_dev), pktlen,
			sc->sc_bulkout_bufferlen);
		return (USBD_IOERROR);
	}

	m_copydata(m, 0, pktlen, sc->sc_bulkout_buffer);
	m_freem(m);

	/* Initialise a USB transfer and then schedule it */

	(void) usbd_setup_xfer( sc->sc_bulkout_xfer,
				sc->sc_bulkout_pipe,
				priv,
				sc->sc_bulkout_buffer,
				pktlen,
				USBD_SHORT_XFER_OK,
				UDBP_TIMEOUT,
				udbp_out_transfer_cb);

	err = usbd_transfer(sc->sc_bulkout_xfer);
	if (err && err != USBD_IN_PROGRESS) {
		DPRINTF(("%s: failed to setup out-transfer, %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
		return(err);
	}

	return (USBD_NORMAL_COMPLETION);
}

Static void
udbp_out_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
			usbd_status err)
{
	udbp_p sc = priv;		/* XXX see priv above */
	int s;

	if (err) {
		DPRINTF(("%s: bulk-out transfer failed: %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
		/* Transfer has failed, packet is not transmitted */
		/* XXX Invalidate packet */
		return;
	}

	/* packet has been transmitted */

	s = splusb();			/* mark the buffer available */
	sc->flags &= ~OUT_BUSY;
	udbp_setup_out_transfer(sc);
	splx(s);
}

DRIVER_MODULE(udbp, uhub, udbp_driver, udbp_devclass, usbd_driver_load, 0);


/***********************************************************************
 * Start of Netgraph methods
 **********************************************************************/

/*
 * If this is a device node so this work is done in the attach()
 * routine and the constructor will return EINVAL as you should not be able
 * to create nodes that depend on hardware (unless you can add the hardware :)
 */
Static int
ng_udbp_constructor(node_p node)
{
	return (EINVAL);
}

/*
 * Give our ok for a hook to be added...
 * If we are not running this might kick a device into life.
 * Possibly decode information out of the hook name.
 * Add the hook's private info to the hook structure.
 * (if we had some). In this example, we assume that there is a
 * an array of structs, called 'channel' in the private info,
 * one for each active channel. The private
 * pointer of each hook points to the appropriate UDBP_hookinfo struct
 * so that the source of an input packet is easily identified.
 */
Static int
ng_udbp_newhook(node_p node, hook_p hook, const char *name)
{
	const udbp_p sc = NG_NODE_PRIVATE(node);

#if 0
	/* Possibly start up the device if it's not already going */
	if ((sc->flags & SCF_RUNNING) == 0) {
		ng_udbp_start_hardware(sc);
	}
#endif

	if (strcmp(name, NG_UDBP_HOOK_NAME) == 0) {
		sc->hook = hook;
		NG_HOOK_SET_PRIVATE(hook, NULL);
	} else {
		return (EINVAL);	/* not a hook we know about */
	}
	return(0);
}

/*
 * Get a netgraph control message.
 * Check it is one we understand. If needed, send a response.
 * We could save the address for an async action later, but don't here.
 * Always free the message.
 * The response should be in a malloc'd region that the caller can 'free'.
 * A response is not required.
 * Theoretically you could respond defferently to old message types if
 * the cookie in the header didn't match what we consider to be current
 * (so that old userland programs could continue to work).
 */
Static int
ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
	const udbp_p sc = NG_NODE_PRIVATE(node);
	struct ng_mesg *resp = NULL;
	int error = 0;
	struct ng_mesg *msg;

	NGI_GET_MSG(item, msg);
	/* Deal with message according to cookie and command */
	switch (msg->header.typecookie) {
	case NGM_UDBP_COOKIE: 
		switch (msg->header.cmd) {
		case NGM_UDBP_GET_STATUS:
		    {
			struct ngudbpstat *stats;

			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
			if (!resp) {
				error = ENOMEM;
				break;
			}
			stats = (struct ngudbpstat *) resp->data;
			stats->packets_in = sc->packets_in;
			stats->packets_out = sc->packets_out;
			break;
		    }
		case NGM_UDBP_SET_FLAG:
			if (msg->header.arglen != sizeof(u_int32_t)) {
				error = EINVAL;
				break;
			}
			sc->flags = *((u_int32_t *) msg->data);
			break;
		default:
			error = EINVAL;		/* unknown command */
			break;
		}
		break;
	default:
		error = EINVAL;			/* unknown cookie type */
		break;
	}

	/* Take care of synchronous response, if any */
	NG_RESPOND_MSG(error, node, item, resp);
	NG_FREE_MSG(msg);
	return(error);
}

/*
 * Accept data from the hook and queue it for output.
 */
Static int
ng_udbp_rcvdata(hook_p hook, item_p item)
{
	const udbp_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
	int error;
	struct ifqueue	*xmitq_p;
	int	s;
	struct mbuf *m;
	meta_p meta;

	NGI_GET_M(item, m);
	NGI_GET_META(item, meta);
	NG_FREE_ITEM(item);
	/* 
	 * Now queue the data for when it can be sent
	 */
	if (meta && meta->priority > 0) {
		xmitq_p = (&sc->xmitq_hipri);
	} else {
		xmitq_p = (&sc->xmitq);
	}
	s = splusb();
	IF_LOCK(xmitq_p);
	if (_IF_QFULL(xmitq_p)) {
		_IF_DROP(xmitq_p);
		IF_UNLOCK(xmitq_p);
		splx(s);
		error = ENOBUFS;
		goto bad;
	}
	_IF_ENQUEUE(xmitq_p, m);
	IF_UNLOCK(xmitq_p);
	if (!(sc->flags & OUT_BUSY))
		udbp_setup_out_transfer(sc);
	splx(s);
	return (0);

bad:	/*
         * It was an error case.
	 * check if we need to free the mbuf, and then return the error
	 */
	NG_FREE_M(m);
	NG_FREE_META(meta);
	return (error);
}

/*
 * Do local shutdown processing..
 * We are a persistant device, we refuse to go away, and
 * only remove our links and reset ourself.
 */
Static int
ng_udbp_rmnode(node_p node)
{
	const udbp_p sc = NG_NODE_PRIVATE(node);
	int err;

	if (sc->flags & DISCONNECTED) {
		/* 
		 * WE are really going away.. hardware must have gone.
		 * Assume that the hardware drive part will clear up the 
		 * sc, in fact it may already have done so..
		 * In which case we may have just segfaulted..XXX
		 */
		return (0);
	}

	/* stolen from attach routine */
	/* Drain the queues */
	IF_DRAIN(&sc->xmitq_hipri);
	IF_DRAIN(&sc->xmitq);

	sc->packets_in = 0;		/* reset stats */
	sc->packets_out = 0;
	NG_NODE_UNREF(node);			/* forget it ever existed */

	if ((err = ng_make_node_common(&ng_udbp_typestruct, &sc->node)) == 0) {
		char	nodename[128];
		sprintf(nodename, "%s", USBDEVNAME(sc->sc_dev));
		if ((err = ng_name_node(sc->node, nodename))) {
			NG_NODE_UNREF(sc->node); /* out damned spot! */
			sc->flags &= ~NETGRAPH_INITIALISED;
			sc->node = NULL;
		} else {
			NG_NODE_SET_PRIVATE(sc->node, sc);
		}
	}
	return (err);
}

/*
 * This is called once we've already connected a new hook to the other node.
 * It gives us a chance to balk at the last minute.
 */
Static int
ng_udbp_connect(hook_p hook)
{
	/* probably not at splnet, force outward queueing */
	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
	/* be really amiable and just say "YUP that's OK by me! " */
	return (0);
}

/*
 * Dook disconnection
 *
 * For this type, removal of the last link destroys the node
 */
Static int
ng_udbp_disconnect(hook_p hook)
{
	const udbp_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
	sc->hook = NULL;

	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
		ng_rmnode_self(NG_HOOK_NODE(hook));
	return (0);
}

OpenPOWER on IntegriCloud