summaryrefslogtreecommitdiff
path: root/include/kernel/udcd.h
blob: 824383360a5edcf905f8139b96784030c86af429 (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
#ifndef _PSP2_KERNEL_UDCD_H_
#define _PSP2_KERNEL_UDCD_H_

#include <psp2kern/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/** Enumeration for Device and/or Interface Class codes
 */
typedef enum SceUdcdUsbClass {
	USB_CLASS_PER_INTERFACE       = 0x00,  //!< for DeviceClass
	USB_CLASS_AUDIO               = 0x01,
	USB_CLASS_COMM                = 0x02,
	USB_CLASS_HID                 = 0x03,
	USB_CLASS_PTP                 = 0x06,
	USB_CLASS_PRINTER             = 0x07,
	USB_CLASS_MASS_STORAGE        = 0x08,
	USB_CLASS_HUB                 = 0x09,
	USB_CLASS_DATA                = 0x0A,
	USB_CLASS_VENDOR_SPEC         = 0xFF
} SceUdcdUsbClass;

/** Enumeration for descriptor types
 */
typedef enum SceUdcdUsbDt {
	USB_DT_DEVICE         = 0x01,
	USB_DT_CONFIG         = 0x02,
	USB_DT_STRING         = 0x03,
	USB_DT_INTERFACE      = 0x04,
	USB_DT_ENDPOINT       = 0x05
} SceUdcdUsbDt;

/** Enumeration for standard requests
 */
typedef enum SceUdcdUsbReq {
	USB_REQ_GET_STATUS      = 0x00,
	USB_REQ_CLEAR_FEATURE   = 0x01,
	USB_REQ_SET_FEATURE     = 0x03,
	USB_REQ_SET_ADDRESS     = 0x05,
	USB_REQ_GET_DESCRIPTOR  = 0x06,
	USB_REQ_SET_DESCRIPTOR  = 0x07,
	USB_REQ_GET_CONFIG      = 0x08,
	USB_REQ_SET_CONFIG      = 0x09,
	USB_REQ_GET_INTERFACE   = 0x0A,
	USB_REQ_SET_INTERFACE   = 0x0B,
	USB_REQ_SYNC_FRAME      = 0x0C
} SceUdcdUsbReq;

/**
 * Descriptor sizes per descriptor type
 */
#define USB_DT_DEVICE_SIZE		18
#define USB_DT_CONFIG_SIZE		9
#define USB_DT_INTERFACE_SIZE		9
#define USB_DT_ENDPOINT_SIZE		7
#define USB_DT_ENDPOINT_AUDIO_SIZE	9	//!< Audio extension
#define USB_DT_HUB_NONVAR_SIZE		7

/** Control message request type bitmask
 */
#define USB_CTRLTYPE_DIR_MASK		0x80
#define USB_CTRLTYPE_DIR_HOST2DEVICE	(0 << 7)
#define USB_CTRLTYPE_DIR_DEVICE2HOST	(1 << 7)
#define USB_CTRLTYPE_TYPE_MASK		0x60
#define USB_CTRLTYPE_TYPE_STANDARD	(0 << 5)
#define USB_CTRLTYPE_TYPE_CLASS		(1 << 5)
#define USB_CTRLTYPE_TYPE_VENDOR	(2 << 5)
#define USB_CTRLTYPE_TYPE_RESERVED	(3 << 5)
#define USB_CTRLTYPE_REC_MASK		0x1f
#define USB_CTRLTYPE_REC_DEVICE		0
#define USB_CTRLTYPE_REC_INTERFACE	1
#define USB_CTRLTYPE_REC_ENDPOINT	2
#define USB_CTRLTYPE_REC_OTHER		3

/** Endpoint types and masks
 */
#define USB_ENDPOINT_ADDRESS_MASK       0x0f    /* in bEndpointAddress */
#define USB_ENDPOINT_DIR_MASK           0x80

#define USB_FEATURE_ENDPOINT_HALT	0

#define USB_ENDPOINT_IN			0x80
#define USB_ENDPOINT_OUT		0x00

#define USB_ENDPOINT_TYPE_MASK          0x03    /* in bmAttributes */
#define USB_ENDPOINT_TYPE_CONTROL       0
#define USB_ENDPOINT_TYPE_ISOCHRONOUS   1
#define USB_ENDPOINT_TYPE_BULK          2
#define USB_ENDPOINT_TYPE_INTERRUPT     3

/* HID constants. Not part of chapter 9 */

/** Class-Specific Requests
 */
typedef enum SceUdcdHidRequest {
	HID_REQUEST_GET_REPORT     = 0x01,
	HID_REQUEST_GET_IDLE       = 0x02,
	HID_REQUEST_GET_PROTOCOL   = 0x03,
	HID_REQUEST_SET_REPORT     = 0x09,
	HID_REQUEST_SET_IDLE       = 0x0A,
	HID_REQUEST_SET_PROTOCOL   = 0x0B
} SceUdcdHidRequest;

/** Class Descriptor Types
 */
typedef enum SceUdcdHidDescriptor {
	HID_DESCRIPTOR_HID     = 0x21,
	HID_DESCRIPTOR_REPORT  = 0x22,
	HID_DESRIPTOR_PHY      = 0x23
} SceUdcdHidDescriptor;

/** Protocol Selection
 */
typedef enum SceUdcdProtocol {
	BOOT_PROTOCOL  = 0x00,
	RPT_PROTOCOL   = 0x01
} SceUdcdProtocol;

/** HID Interface Class Code
 */
#define HID_INTF			0x03

/** HID Interface Class SubClass Codes
 */
#define BOOT_INTF_SUBCLASS		0x01

/** HID Interface Class Protocol Codes
 */
typedef enum SceUdcdHidProtocol {
	HID_PROTOCOL_NONE      = 0x00,
	HID_PROTOCOL_KEYBOARD  = 0x01,
	HID_PROTOCOL_MOUSE     = 0x02
} SceUdcdHidProtocol;

/** USB Status
 */
typedef enum SceUdcdStatus {
	SCE_UDCD_STATUS_CONNECTION_NEW          = 0x0001,
	SCE_UDCD_STATUS_CONNECTION_ESTABLISHED  = 0x0002,
	SCE_UDCD_STATUS_CONNECTION_SUSPENDED    = 0x0004,
	SCE_UDCD_STATUS_CABLE_DISCONNECTED      = 0x0010,
	SCE_UDCD_STATUS_CABLE_CONNECTED         = 0x0020,
	SCE_UDCD_STATUS_DEACTIVATED             = 0x0100,
	SCE_UDCD_STATUS_ACTIVATED               = 0x0200,
	SCE_UDCD_STATUS_IS_CHARGING             = 0x0400,
	SCE_UDCD_STATUS_USE_USB_CHARGING        = 0x0800,
	SCE_UDCD_STATUS_UNKNOWN_1000            = 0x1000,
	SCE_UDCD_STATUS_UNKNOWN_2000            = 0x2000
} SceUdcdStatus;

/** USB Driver status
 */
typedef enum SceUdcdStatusDriver {
	SCE_UDCD_STATUS_DRIVER_STARTED         = 0x01,
	SCE_UDCD_STATUS_DRIVER_REGISTERED      = 0x02
} SceUdcdStatusDriver;

/** USB limits
 */
#define SCE_UDCD_MAX_INTERFACES     8
#define SCE_UDCD_MAX_ENDPOINTS      9
#define SCE_UDCD_MAX_ALTERNATE      2

typedef enum SceUdcdRetcode {
	SCE_UDCD_RETCODE_CANCEL              = -1,
	SCE_UDCD_RETCODE_CANCEL_ALL          = -2,
	SCE_UDCD_RETCODE_CANCEL_TRANSMISSION = -3,
	SCE_UDCD_RETCODE_SUCCESS             =  0,
	SCE_UDCD_RETCODE_SEND                =  1,
	SCE_UDCD_RETCODE_RECV                =  2
} SceUdcdRetcode;

/** Error codes
 */
typedef enum SceUdcdErrorCode {
	SCE_UDCD_ERROR_ILLEGAL_CONTEXT          = 0x80000030,
	SCE_UDCD_ERROR_INVALID_POINTER          = 0x80000103,
	SCE_UDCD_ERROR_INVALID_FLAG             = 0x80000105,
	SCE_UDCD_ERROR_INVALID_VALUE            = 0x800001FE,
	SCE_UDCD_ERROR_ALREADY_DONE             = 0x80243001,
	SCE_UDCD_ERROR_INVALID_ARGUMENT         = 0x80243002,
	SCE_UDCD_ERROR_ARGUMENT_EXCEEDED_LIMIT  = 0x80243003,
	SCE_UDCD_ERROR_MEMORY_EXHAUSTED         = 0x80243004,
	SCE_UDCD_ERROR_DRIVER_NOT_FOUND         = 0x80243005,
	SCE_UDCD_ERROR_DRIVER_IN_PROGRESS       = 0x80243006,
	SCE_UDCD_ERROR_BUS_DRIVER_NOT_STARTED   = 0x80243007,
	SCE_UDCD_ERROR_WAIT_TIMEOUT             = 0x80243008,
	SCE_UDCD_ERROR_WAIT_CANCEL              = 0x80243009,
	SCE_UDCD_ERROR_USBDRIVER_INVALID_DRIVER = 0x80243200,
	SCE_UDCD_ERROR_USBDRIVER_INVALID_NAME   = 0x80243201,
	SCE_UDCD_ERROR_USBDRIVER_INVALID_FUNCS  = 0x80243202
} SceUdcdErrorCode;

/**  USB string descriptor
 */
typedef struct SceUdcdStringDescriptor {
	unsigned char bLength;
	unsigned char bDescriptorType;
	short         bString[31];
} SceUdcdStringDescriptor;

/**  USB device descriptor
 */
typedef struct SceUdcdDeviceDescriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short bcdUSB;
	unsigned char  bDeviceClass;
	unsigned char  bDeviceSubClass;
	unsigned char  bDeviceProtocol;
	unsigned char  bMaxPacketSize0;
	unsigned short idVendor;
	unsigned short idProduct;
	unsigned short bcdDevice;
	unsigned char  iManufacturer;
	unsigned char  iProduct;
	unsigned char  iSerialNumber;
	unsigned char  bNumConfigurations;
} __attribute__ ((aligned(4))) SceUdcdDeviceDescriptor;

/**  USB device qualifier descriptor
 */
typedef struct SceUdcdDeviceQualifierDescriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short bcdUSB;
	unsigned char  bDeviceClass;
	unsigned char  bDeviceSubClass;
	unsigned char  bDeviceProtocol;
	unsigned char  bMaxPacketSize0;
	unsigned char  bNumConfigurations;
	unsigned char  bReserved;
} __attribute__ ((aligned(4))) SceUdcdDeviceQualifierDescriptor;

/**  USB configuration descriptor
 */
typedef struct SceUdcdConfigDescriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short wTotalLength;
	unsigned char  bNumInterfaces;
	unsigned char  bConfigurationValue;
	unsigned char  iConfiguration;
	unsigned char  bmAttributes;
	unsigned char  bMaxPower;
	struct SceUdcdInterfaceSettings *settings;
	unsigned char *extra;                       //!< Extra descriptors
	int extraLength;
} SceUdcdConfigDescriptor;

/**  USB driver interfaces structure
 */
typedef struct SceUdcdInterfaceSettings {
	struct SceUdcdInterfaceDescriptor *descriptors; //!< Pointers to the individual interface descriptors
	unsigned int alternateSetting;
	unsigned int numDescriptors;                    //!< Number of interface descriptors
} SceUdcdInterfaceSettings;

/**  USB Interface descriptor
 */
typedef struct SceUdcdInterfaceDescriptor {
	unsigned char bLength;
	unsigned char bDescriptorType;
	unsigned char bInterfaceNumber;
	unsigned char bAlternateSetting;
	unsigned char bNumEndpoints;
	unsigned char bInterfaceClass;
	unsigned char bInterfaceSubClass;
	unsigned char bInterfaceProtocol;
	unsigned char iInterface;
	struct SceUdcdEndpointDescriptor *endpoints;
	unsigned char *extra;                         //!< Extra descriptors
	int extraLength;
} SceUdcdInterfaceDescriptor;

/**  USB endpoint descriptor
 */
typedef struct SceUdcdEndpointDescriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned char  bEndpointAddress;
	unsigned char  bmAttributes;
	unsigned short wMaxPacketSize;
	unsigned char  bInterval;
	unsigned char *extra;              //!< Extra descriptors
	int extraLength;
} SceUdcdEndpointDescriptor;

/**  USB driver interface
 */
typedef struct SceUdcdInterface {
	int expectNumber;     //!< Expectant interface number (interface number or -1)
	int interfaceNumber;  //!< End interface
	int numInterfaces;    //!< Number of interfaces
} SceUdcdInterface;

/**  USB driver endpoint
 */
typedef struct SceUdcdEndpoint {
	int direction;            //!< USB_ENDPOINT_IN /  USB_ENDPOINT_OUT
	int driverEndpointNumber; //!< Driver Endpoint number (must be filled in sequentially)
	int endpointNumber;       //!< Endpoint number (Filled in by the bus driver)
	int transmittedBytes;     //!< Number of transmitted bytes
} SceUdcdEndpoint;

/**  USB driver configuration
 */
typedef struct SceUdcdConfiguration {
	SceUdcdConfigDescriptor    *configDescriptors;     //!< Pointer to the configuration descriptors
	SceUdcdInterfaceSettings   *settings;              //!< USB driver interface settings
	SceUdcdInterfaceDescriptor *interfaceDescriptors;  //!< Pointer to the first interface descriptor
	SceUdcdEndpointDescriptor  *endpointDescriptors;   //!< Pointer to the first endpoint descriptor
} SceUdcdConfiguration;

/**  USB EP0 Device Request
 */
typedef struct SceUdcdEP0DeviceRequest {
	unsigned char  bmRequestType;
	unsigned char  bRequest;
	unsigned short wValue;
	unsigned short wIndex;
	unsigned short wLength;
} SceUdcdEP0DeviceRequest;

/**  USB driver structure
 */
typedef struct SceUdcdDriver {
	const char *driverName;                         //!< Name of the USB driver
	int numEndpoints;                               //!< Number of endpoints in this driver (including default control)
	SceUdcdEndpoint *endpoints;                     //!< List of endpoint structures (used when calling other functions)
	SceUdcdInterface *interface;                    //!< Interface list
	SceUdcdDeviceDescriptor *descriptor_hi;         //!< Pointer to hi-speed device descriptor
	SceUdcdConfiguration *configuration_hi;         //!< Pointer to hi-speed device configuration
	SceUdcdDeviceDescriptor *descriptor;            //!< Pointer to full-speed device descriptor
	SceUdcdConfiguration *configuration;            //!< Pointer to full-speed device configuration
	SceUdcdStringDescriptor *stringDescriptors; //!< Default string descriptors
	SceUdcdStringDescriptor *stringDescriptorProduct; //!< Product string descriptor (iProduct has to be 2)
	SceUdcdStringDescriptor *stringDescriptorSerial; //!< Serial string descriptor (iSerial has to be 3)
	int (*processRequest)(int recipient, int arg /* endpoint number or interface number */, SceUdcdEP0DeviceRequest *req, void *user_data); //!< Received a control request
	int (*changeSetting)(int interfaceNumber, int alternateSetting, int bus); //!< Change alternate setting
	int (*attach)(int usb_version, void *user_data);                                  //!< Configuration set (attach) function
	void (*detach)(void *user_data);                                            //!< Configuration unset (detach) function
	void (*configure)(int usb_version, int desc_count, SceUdcdInterfaceSettings *settings, void *user_data); //!< Configure the device
	int (*start)(int size, void *args, void *user_data); //!< Function called when the driver is started
	int (*stop)(int size, void *args, void *user_data);  //!< Function called when the driver is stopped
	void *user_data;                   //!< User data
	int bus;                   //!< USB bus
	struct SceUdcdDriver *link;          //!< Link to next USB driver in the chain, set to NULL
} SceUdcdDriver;

/**  USB device request
 */
typedef struct SceUdcdDeviceRequest {
	SceUdcdEndpoint *endpoint;                            //!< Pointer to the endpoint to queue request on
	void *data;                                           //!< Pointer to the data buffer to use in the request
	unsigned int unk;                                     //!< Unknown data
	int  size;                                            //!< Size of the data buffer
	int  isControlRequest;                                //!< Is a control request?
	void (*onComplete)(struct SceUdcdDeviceRequest *req); //!< Pointer to the function to call on completion
	int  transmitted;                                     //!< Number of transmitted bytes
	int  returnCode;                                      //!< Return code of the request (See ::SceUdcdRetcode)
	struct SceUdcdDeviceRequest *next;                    //!< Link pointer to next request used by the driver, set it to NULL
	void *unused;                                         //!< An unused value (maybe an argument)
	void *physicalAddress;                                //!< Physical address
} SceUdcdDeviceRequest;

/**  USB driver name
 */
typedef struct SceUdcdDriverName {
	int  size;
	char name[32];
	int  flags;
} __attribute__ ((aligned(16))) SceUdcdDriverName;

/**  USB device information
 */
typedef struct SceUdcdDeviceInfo {
	unsigned char info[64];
} SceUdcdDeviceInfo;

/**
  * Start a USB driver.
  *
  * @param driverName - Name of the USB driver to start
  * @param size - Size of arguments to pass to USB driver start
  * @param args - Arguments to pass to USB driver start
  *
  * @return 0 on success, < 0 on error.
  */
int sceUdcdStart(const char *driverName, int size, void *args);

/**
  * Stop a USB driver.
  *
  * @param driverName - Name of the USB driver to stop
  * @param size - Size of arguments to pass to USB driver start
  * @param args - Arguments to pass to USB driver start
  *
  * @return 0 on success, < 0 on error.
  */
int sceUdcdStop(const char *driverName, int size, void *args);

/**
  * Activate a USB driver.
  *
  * @param pid - Product ID for the default USB Driver
  *
  * @return 0 on success, < 0 on error.
  */
int sceUdcdActivate(unsigned int productId);

/**
  * Deactivate USB driver.
  *
  * @return 0 on success, < 0 on error.
  */
int sceUdcdDeactivate(void);

/**
  * Get USB state
  *
  * @return One or more ::SceUdcdStatus.
  */
int sceUdcdGetDeviceState(void);

/**
 * Get device information
 *
 * @param[out] devInfo - Device information
 *
 * @return 0 on success, < 0 on error.
*/
int sceUdcdGetDeviceInfo(SceUdcdDeviceInfo *devInfo);

/**
  * Get state of a specific USB driver
  *
  * @param driverName - name of USB driver to get status from
  *
  * @return SCE_UDCD_STATUS_DRIVER_STARTED  if the driver has been started, SCE_UDCD_STATUS_DRIVER_REGISTERED if it is stopped
  */
int sceUdcdGetDrvState(const char *driverName);

/**
 *  Get the list of drivers
 *  @param flags - One or more ::SceUdcdStatusDriver
 *  @param list - points to the output list
 *  @param size - number of entries in the output list
 *  @return the number of drivers in the output or < 0 in case of error
 */
int sceUdcdGetDrvList(unsigned int flags, SceUdcdDriverName *list, int size);

/**
 * Wait for USB state
 * @param state - combination of states (returned by ::sceUdcdGetDeviceState)
 * @param waitMode - one of the ::SceEventFlagWaitTypes
 * @param timeout - pointer to timeout
 * @return the usb state or < 0 in case of error
 */
int sceUdcdWaitState(unsigned int state, unsigned int waitMode, SceUInt *timeout);

/**
 * Cancel a pending sceUdcdWaitState
 * @return 0 on success
 */
int sceUdcdWaitCancel(void);

/**
 * Register a USB driver.
 *
 * @param drv - Pointer to a filled out USB driver
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdRegister(SceUdcdDriver *drv);

/**
 * Register a USB driver to a specific USB bus.
 *
 * @param drv - Pointer to a filled out USB driver
 * @param bus - The USB bus index (usually 2)
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdRegisterToBus(SceUdcdDriver *drv, int bus);

/**
 * Unregister a USB driver
 *
 * @param drv - Pointer to a filled out USB driver
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdUnregister(SceUdcdDriver *drv);

/**
 * Clear the FIFO on an endpoint
 *
 * @param endp - The endpoint to clear
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdClearFIFO(SceUdcdEndpoint *endp);

/**
 * Cancel any pending requests on an endpoint.
 *
 * @param endp - The endpoint to cancel
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdReqCancelAll(SceUdcdEndpoint *endp);

/**
 * Stall an endpoint
 *
 * @param endp - The endpoint to stall
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdStall(SceUdcdEndpoint *endp);

/**
 * Queue a send request(IN from host pov)
 *
 * @param req - Pointer to a filled out ::SceUdcdDeviceRequest structure.
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdReqSend(SceUdcdDeviceRequest *req);

/**
 * Queue a receive request(OUT from host pov)
 *
 * @param req - Pointer to a filled out ::SceUdcdDeviceRequest structure
 *
 * @return 0 on success, < 0 on error
 */
int sceUdcdReqRecv(SceUdcdDeviceRequest *req);

#ifdef __cplusplus
}
#endif

#endif /* _PSP2_KERNEL_UDCD_H_ */