summaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/ep0.c
blob: b3214882f76b5ad0c03f62a482fe7f7149661d91 (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
/*
 * (C) Copyright 2003
 * Gerry Hamel, geh@ti.com, Texas Instruments
 *
 * (C) Copyright 2006
 * Bryan O'Donoghue, deckard@CodeHermit.ie
 *
 * Based on
 * linux/drivers/usbd/ep0.c
 *
 * Copyright (c) 2000, 2001, 2002 Lineo
 * Copyright (c) 2001 Hewlett Packard
 *
 * By:
 *	Stuart Lynne <sl@lineo.com>,
 *	Tom Rushworth <tbr@lineo.com>,
 *	Bruce Balden <balden@lineo.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/*
 * This is the builtin ep0 control function. It implements all required functionality
 * for responding to control requests (SETUP packets).
 *
 * XXX
 *
 * Currently we do not pass any SETUP packets (or other) to the configured
 * function driver. This may need to change.
 *
 * XXX
 *
 * As alluded to above, a simple callback cdc_recv_setup has been implemented
 * in the usb_device data structure to facilicate passing
 * Common Device Class packets to a function driver.
 *
 * XXX
 */

#include <common.h>
#include <usbdevice.h>

#if 0
#define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
#define dbg_ep0(lvl,fmt,args...)
#endif

/* EP0 Configuration Set ********************************************************************* */


/**
 * ep0_get_status - fill in URB data with appropriate status
 * @device:
 * @urb:
 * @index:
 * @requesttype:
 *
 */
static int ep0_get_status (struct usb_device_instance *device,
			   struct urb *urb, int index, int requesttype)
{
	char *cp;

	urb->actual_length = 2;
	cp = (char*)urb->buffer;
	cp[0] = cp[1] = 0;

	switch (requesttype) {
	case USB_REQ_RECIPIENT_DEVICE:
		cp[0] = USB_STATUS_SELFPOWERED;
		break;
	case USB_REQ_RECIPIENT_INTERFACE:
		break;
	case USB_REQ_RECIPIENT_ENDPOINT:
		cp[0] = usbd_endpoint_halted (device, index);
		break;
	case USB_REQ_RECIPIENT_OTHER:
		urb->actual_length = 0;
	default:
		break;
	}
	dbg_ep0 (2, "%02x %02x", cp[0], cp[1]);
	return 0;
}

/**
 * ep0_get_one
 * @device:
 * @urb:
 * @result:
 *
 * Set a single byte value in the urb send buffer. Return non-zero to signal
 * a request error.
 */
static int ep0_get_one (struct usb_device_instance *device, struct urb *urb,
			__u8 result)
{
	urb->actual_length = 1;	/* XXX 2? */
	((char *) urb->buffer)[0] = result;
	return 0;
}

/**
 * copy_config
 * @urb: pointer to urb
 * @data: pointer to configuration data
 * @length: length of data
 *
 * Copy configuration data to urb transfer buffer if there is room for it.
 */
void copy_config (struct urb *urb, void *data, int max_length,
			 int max_buf)
{
	int available;
	int length;

	/*dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p", */
	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, data); */

	if (!data) {
		dbg_ep0 (1, "data is NULL");
		return;
	}
	length = max_length;

	if (length > max_length) {
		dbg_ep0 (1, "length: %d >= max_length: %d", length,
			 max_length);
		return;
	}
	/*dbg_ep0(1, "   actual: %d buf: %d max_buf: %d max_length: %d length: %d", */
	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, length); */

	if ((available =
	     /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) {
		return;
	}
	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */

	if (length > available) {
		length = available;
	}
	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */

	memcpy (urb->buffer + urb->actual_length, data, length);
	urb->actual_length += length;

	dbg_ep0 (3,
		 "copy_config: <- actual: %d buf: %d max_buf: %d max_length: %d available: %d",
		 urb->actual_length, urb->buffer_length, max_buf, max_length,
		 available);
}

/**
 * ep0_get_descriptor
 * @device:
 * @urb:
 * @max:
 * @descriptor_type:
 * @index:
 *
 * Called by ep0_rx_process for a get descriptor device command. Determine what
 * descriptor is being requested, copy to send buffer. Return zero if ok to send,
 * return non-zero to signal a request error.
 */
static int ep0_get_descriptor (struct usb_device_instance *device,
			       struct urb *urb, int max, int descriptor_type,
			       int index)
{
	int port = 0;		/* XXX compound device */

	/*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */

	if (!urb || !urb->buffer || !urb->buffer_length
	    || (urb->buffer_length < 255)) {
		dbg_ep0 (2, "invalid urb %p", urb);
		return -1L;
	}

	/* setup tx urb */
	urb->actual_length = 0;

	dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));

	switch (descriptor_type) {
	case USB_DESCRIPTOR_TYPE_DEVICE:
		{
			struct usb_device_descriptor *device_descriptor;
			if (!
			    (device_descriptor =
			     usbd_device_device_descriptor (device, port))) {
				return -1;
			}
			/* copy descriptor for this device */
			copy_config (urb, device_descriptor,
				     sizeof (struct usb_device_descriptor),
				     max);

			/* correct the correct control endpoint 0 max packet size into the descriptor */
			device_descriptor =
				(struct usb_device_descriptor *) urb->buffer;

		}
		dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length);
		break;

	case USB_DESCRIPTOR_TYPE_CONFIGURATION:
		{
			struct usb_configuration_descriptor
				*configuration_descriptor;
			struct usb_device_descriptor *device_descriptor;
			if (!
			    (device_descriptor =
			     usbd_device_device_descriptor (device, port))) {
				return -1;
			}
			/*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */
			if (index >= device_descriptor->bNumConfigurations) {
				dbg_ep0 (0, "index too large: %d >= %d", index,
					 device_descriptor->
					 bNumConfigurations);
				return -1;
			}

			if (!
			    (configuration_descriptor =
			     usbd_device_configuration_descriptor (device,
								   port,
								   index))) {
				dbg_ep0 (0,
					 "usbd_device_configuration_descriptor failed: %d",
					 index);
				return -1;
			}
			dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength));
			copy_config (urb, configuration_descriptor,

					cpu_to_le16(configuration_descriptor->wTotalLength),
				     max);
		}

		break;

	case USB_DESCRIPTOR_TYPE_STRING:
		{
			struct usb_string_descriptor *string_descriptor;
			if (!(string_descriptor = usbd_get_string (index))) {
				serial_printf("Invalid string index %d\n", index);
				return -1;
			}
			dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength);
			copy_config (urb, string_descriptor, string_descriptor->bLength, max);
		}
		break;
	case USB_DESCRIPTOR_TYPE_INTERFACE:
	serial_printf("USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n");
		return -1;
	case USB_DESCRIPTOR_TYPE_ENDPOINT:
		serial_printf("USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n");
		return -1;
	case USB_DESCRIPTOR_TYPE_HID:
		{
			serial_printf("USB_DESCRIPTOR_TYPE_HID - error not implemented\n");
			return -1;	/* unsupported at this time */
#if 0
			int bNumInterface =
				le16_to_cpu (urb->device_request.wIndex);
			int bAlternateSetting = 0;
			int class = 0;
			struct usb_class_descriptor *class_descriptor;

			if (!(class_descriptor =
			      usbd_device_class_descriptor_index (device,
								  port, 0,
								  bNumInterface,
								  bAlternateSetting,
								  class))
			    || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {
				dbg_ep0 (3, "[%d] interface is not HID",
					 bNumInterface);
				return -1;
			}
			/* copy descriptor for this class */
			copy_config (urb, class_descriptor,
				     class_descriptor->descriptor.hid.bLength,
				     max);
#endif
		}
		break;
	case USB_DESCRIPTOR_TYPE_REPORT:
		{
			serial_printf("USB_DESCRIPTOR_TYPE_REPORT - error not implemented\n");
			return -1;	/* unsupported at this time */
#if 0
			int bNumInterface =
				le16_to_cpu (urb->device_request.wIndex);
			int bAlternateSetting = 0;
			int class = 0;
			struct usb_class_report_descriptor *report_descriptor;

			if (!(report_descriptor =
			      usbd_device_class_report_descriptor_index
			      (device, port, 0, bNumInterface,
			       bAlternateSetting, class))
			    || report_descriptor->bDescriptorType !=
			    USB_DT_REPORT) {
				dbg_ep0 (3, "[%d] descriptor is not REPORT",
					 bNumInterface);
				return -1;
			}
			/* copy report descriptor for this class */
			/*copy_config(urb, &report_descriptor->bData[0], report_descriptor->wLength, max); */
			if (max - urb->actual_length > 0) {
				int length =
					MIN (report_descriptor->wLength,
					     max - urb->actual_length);
				memcpy (urb->buffer + urb->actual_length,
					&report_descriptor->bData[0], length);
				urb->actual_length += length;
			}
#endif
		}
		break;
	case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
#if defined(CONFIG_USBD_HS)
		{
			struct usb_qualifier_descriptor *qualifier_descriptor =
				device->qualifier_descriptor;

			if (!qualifier_descriptor)
				return -1;

			/* copy descriptor for this device */
			copy_config(urb, qualifier_descriptor,
					sizeof(struct usb_qualifier_descriptor),
					max);

		}
		dbg_ep0(3, "copied qualifier descriptor, actual_length: 0x%x",
				urb->actual_length);
#else
		return -1;
#endif
		break;

	default:
		return -1;
	}


	dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d tx_packetSize: %2d",
		 urb->buffer, urb->buffer_length, urb->actual_length,
		 device->bus->endpoint_array[0].tx_packetSize);
/*
    if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {
	dbg_ep0(0, "adding null byte");
	urb->buffer[urb->actual_length++] = 0;
	dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",
		urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);
    }
*/
	return 0;

}

/**
 * ep0_recv_setup - called to indicate URB has been received
 * @urb: pointer to struct urb
 *
 * Check if this is a setup packet, process the device request, put results
 * back into the urb and return zero or non-zero to indicate success (DATA)
 * or failure (STALL).
 *
 */
int ep0_recv_setup (struct urb *urb)
{
	/*struct usb_device_request *request = urb->buffer; */
	/*struct usb_device_instance *device = urb->device; */

	struct usb_device_request *request;
	struct usb_device_instance *device;
	int address;

	dbg_ep0 (0, "entering ep0_recv_setup()");
	if (!urb || !urb->device) {
		dbg_ep0 (3, "invalid URB %p", urb);
		return -1;
	}

	request = &urb->device_request;
	device = urb->device;

	dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);


	/*dbg_ep0(2, "-       -       -       -       -       -       -       -       -       -"); */

	dbg_ep0 (2,
		 "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s",
		 request->bmRequestType, request->bRequest,
		 le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex),
		 le16_to_cpu (request->wLength),
		 USBD_DEVICE_REQUESTS (request->bRequest));

	/* handle USB Standard Request (c.f. USB Spec table 9-2) */
	if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) {
		if(device->device_state <= STATE_CONFIGURED){
			/*	Attempt to handle a CDC specific request if we are
			 *	in the configured state.
			 */
			return device->cdc_recv_setup(request,urb);
		}
		dbg_ep0 (1, "non standard request: %x",
			 request->bmRequestType & USB_REQ_TYPE_MASK);
		return -1;	/* Stall here */
	}

	switch (device->device_state) {
	case STATE_CREATED:
	case STATE_ATTACHED:
	case STATE_POWERED:
		/* It actually is important to allow requests in these states,
		 * Windows will request descriptors before assigning an
		 * address to the client.
		 */

		/*dbg_ep0 (1, "request %s not allowed in this state: %s", */
		/*                USBD_DEVICE_REQUESTS(request->bRequest), */
		/*                usbd_device_states[device->device_state]); */
		/*return -1; */
		break;

	case STATE_INIT:
	case STATE_DEFAULT:
		switch (request->bRequest) {
		case USB_REQ_GET_STATUS:
		case USB_REQ_GET_INTERFACE:
		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
		case USB_REQ_CLEAR_FEATURE:
		case USB_REQ_SET_FEATURE:
		case USB_REQ_SET_DESCRIPTOR:
			/* case USB_REQ_SET_CONFIGURATION: */
		case USB_REQ_SET_INTERFACE:
			dbg_ep0 (1,
				 "request %s not allowed in DEFAULT state: %s",
				 USBD_DEVICE_REQUESTS (request->bRequest),
				 usbd_device_states[device->device_state]);
			return -1;

		case USB_REQ_SET_CONFIGURATION:
		case USB_REQ_SET_ADDRESS:
		case USB_REQ_GET_DESCRIPTOR:
		case USB_REQ_GET_CONFIGURATION:
			break;
		}
	case STATE_ADDRESSED:
	case STATE_CONFIGURED:
		break;
	case STATE_UNKNOWN:
		dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s",
			 USBD_DEVICE_REQUESTS (request->bRequest),
			 usbd_device_states[device->device_state]);
		return -1;
	}

	/* handle all requests that return data (direction bit set on bm RequestType) */
	if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {

		dbg_ep0 (3, "Device-to-Host");

		switch (request->bRequest) {

		case USB_REQ_GET_STATUS:
			return ep0_get_status (device, urb, request->wIndex,
					       request->bmRequestType &
					       USB_REQ_RECIPIENT_MASK);

		case USB_REQ_GET_DESCRIPTOR:
			return ep0_get_descriptor (device, urb,
						   le16_to_cpu (request->wLength),
						   le16_to_cpu (request->wValue) >> 8,
						   le16_to_cpu (request->wValue) & 0xff);

		case USB_REQ_GET_CONFIGURATION:
			serial_printf("get config %d\n", device->configuration);
			return ep0_get_one (device, urb,
					    device->configuration);

		case USB_REQ_GET_INTERFACE:
			return ep0_get_one (device, urb, device->alternate);

		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
			return -1;

		case USB_REQ_CLEAR_FEATURE:
		case USB_REQ_SET_FEATURE:
		case USB_REQ_SET_ADDRESS:
		case USB_REQ_SET_DESCRIPTOR:
		case USB_REQ_SET_CONFIGURATION:
		case USB_REQ_SET_INTERFACE:
			return -1;
		}
	}
	/* handle the requests that do not return data */
	else {


		/*dbg_ep0(3, "Host-to-Device"); */
		switch (request->bRequest) {

		case USB_REQ_CLEAR_FEATURE:
		case USB_REQ_SET_FEATURE:
			dbg_ep0 (0, "Host-to-Device");
			switch (request->
				bmRequestType & USB_REQ_RECIPIENT_MASK) {
			case USB_REQ_RECIPIENT_DEVICE:
				/* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */
				/* XXX fall through for now as we do not support either */
			case USB_REQ_RECIPIENT_INTERFACE:
			case USB_REQ_RECIPIENT_OTHER:
				dbg_ep0 (0, "request %s not",
					 USBD_DEVICE_REQUESTS (request->bRequest));
			default:
				return -1;

			case USB_REQ_RECIPIENT_ENDPOINT:
				dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue));
				if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) {
					/*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */
					/*                    request->bRequest == USB_REQ_SET_FEATURE); */
					/* NEED TO IMPLEMENT THIS!!! */
					return -1;
				} else {
					dbg_ep0 (1, "request %s bad wValue: %04x",
						 USBD_DEVICE_REQUESTS
						 (request->bRequest),
						 le16_to_cpu (request->wValue));
					return -1;
				}
			}

		case USB_REQ_SET_ADDRESS:
			/* check if this is a re-address, reset first if it is (this shouldn't be possible) */
			if (device->device_state != STATE_DEFAULT) {
				dbg_ep0 (1, "set_address: %02x state: %s",
					 le16_to_cpu (request->wValue),
					 usbd_device_states[device->device_state]);
				return -1;
			}
			address = le16_to_cpu (request->wValue);
			if ((address & 0x7f) != address) {
				dbg_ep0 (1, "invalid address %04x %04x",
					 address, address & 0x7f);
				return -1;
			}
			device->address = address;

			/*dbg_ep0(2, "address: %d %d %d", */
			/*        request->wValue, le16_to_cpu(request->wValue), device->address); */

			return 0;

		case USB_REQ_SET_DESCRIPTOR:	/* XXX should we support this? */
			dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
			return -1;

		case USB_REQ_SET_CONFIGURATION:
			/* c.f. 9.4.7 - the top half of wValue is reserved */
			device->configuration = le16_to_cpu(request->wValue) & 0xff;

			/* reset interface and alternate settings */
			device->interface = device->alternate = 0;

			/*dbg_ep0(2, "set configuration: %d", device->configuration); */
			/*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
			return 0;

		case USB_REQ_SET_INTERFACE:
			device->interface = le16_to_cpu (request->wIndex);
			device->alternate = le16_to_cpu (request->wValue);
			/*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */
			serial_printf ("DEVICE_SET_INTERFACE.. event?\n");
			return 0;

		case USB_REQ_GET_STATUS:
		case USB_REQ_GET_DESCRIPTOR:
		case USB_REQ_GET_CONFIGURATION:
		case USB_REQ_GET_INTERFACE:
		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
			return -1;
		}
	}
	return -1;
}
OpenPOWER on IntegriCloud