Library to allo USB PTP device to be hosted by the mbed platform

Dependents:   class_project_main

Committer:
jakowisp
Date:
Fri Aug 23 00:52:52 2013 +0000
Revision:
0:98cf19bcd828
Child:
1:71c0e9dc153d
Fix for a buffer overrun and refactored the Tranactoion code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 0:98cf19bcd828 1 /* mbed USBHost Library
jakowisp 0:98cf19bcd828 2 * Copyright (c) 2006-2013 ARM Limited
jakowisp 0:98cf19bcd828 3 *
jakowisp 0:98cf19bcd828 4 * Licensed under the Apache License, Version 2.0 (the "License");
jakowisp 0:98cf19bcd828 5 * you may not use this file except in compliance with the License.
jakowisp 0:98cf19bcd828 6 * You may obtain a copy of the License at
jakowisp 0:98cf19bcd828 7 *
jakowisp 0:98cf19bcd828 8 * http://www.apache.org/licenses/LICENSE-2.0
jakowisp 0:98cf19bcd828 9 *
jakowisp 0:98cf19bcd828 10 * Unless required by applicable law or agreed to in writing, software
jakowisp 0:98cf19bcd828 11 * distributed under the License is distributed on an "AS IS" BASIS,
jakowisp 0:98cf19bcd828 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jakowisp 0:98cf19bcd828 13 * See the License for the specific language governing permissions and
jakowisp 0:98cf19bcd828 14 * limitations under the License.
jakowisp 0:98cf19bcd828 15 */
jakowisp 0:98cf19bcd828 16
jakowisp 0:98cf19bcd828 17 #ifndef USBHOSTPTP_H
jakowisp 0:98cf19bcd828 18 #define USBHOSTPTP_H
jakowisp 0:98cf19bcd828 19
jakowisp 0:98cf19bcd828 20 #include "USBHostConf.h"
jakowisp 0:98cf19bcd828 21 #include "USBHost.h"
jakowisp 0:98cf19bcd828 22 #include "PIMA15740_types.h"
jakowisp 0:98cf19bcd828 23
jakowisp 0:98cf19bcd828 24 //#define USDBPTPDEBUG 1
jakowisp 0:98cf19bcd828 25
jakowisp 0:98cf19bcd828 26 #ifdef USDBPTPDEBUG
jakowisp 0:98cf19bcd828 27 #define PTPTRACE(s) (Notify((s))
jakowisp 0:98cf19bcd828 28 #define PTPTRACE2(s,r) (Message((s),(r))
jakowisp 0:98cf19bcd828 29 #else
jakowisp 0:98cf19bcd828 30 #define PTPTRACE(s) ((void)0)
jakowisp 0:98cf19bcd828 31 #define PTPTRACE2(s,r) (wait_us(1))
jakowisp 0:98cf19bcd828 32 #endif
jakowisp 0:98cf19bcd828 33
jakowisp 0:98cf19bcd828 34 /**
jakowisp 0:98cf19bcd828 35 * USB PTP definitions
jakowisp 0:98cf19bcd828 36 *
jakowisp 0:98cf19bcd828 37 */
jakowisp 0:98cf19bcd828 38 #define USB_PTP_CLASS_CODE 0x06
jakowisp 0:98cf19bcd828 39 #define USB_PTP_CLASS_REQUEST_CANCELREQEUST 0x64
jakowisp 0:98cf19bcd828 40 #define USB_PTP_CLASS_REQUEST_GET_EXTENDED_EVENT_DATA 0x65
jakowisp 0:98cf19bcd828 41 #define USB_PTP_CLASS_REQUEST_DEVICE_RESET 0x66
jakowisp 0:98cf19bcd828 42 #define USB_PTP_CLASS_REQUEST_GET_DEVICE_STATUS 0x67
jakowisp 0:98cf19bcd828 43
jakowisp 0:98cf19bcd828 44
jakowisp 0:98cf19bcd828 45 typedef void (*DataHandler)(void *object, uint8_t *bufffer, uint16_t length);
jakowisp 0:98cf19bcd828 46
jakowisp 0:98cf19bcd828 47 /**
jakowisp 0:98cf19bcd828 48 * USBHostPTP class
jakowisp 0:98cf19bcd828 49 *
jakowisp 0:98cf19bcd828 50 */
jakowisp 0:98cf19bcd828 51 class USBHostPTP : public IUSBEnumerator {
jakowisp 0:98cf19bcd828 52 public:
jakowisp 0:98cf19bcd828 53 struct OperFlags
jakowisp 0:98cf19bcd828 54 {
jakowisp 0:98cf19bcd828 55 uint16_t opParams : 3; // 7 - maximum number of operation parameters
jakowisp 0:98cf19bcd828 56 uint16_t rsParams : 3; // 7 - maximum number of response parameters
jakowisp 0:98cf19bcd828 57 uint16_t txOperation : 1; // I->R operation if the flag is set
jakowisp 0:98cf19bcd828 58 uint16_t dataStage : 1; // operation has data stage if the flag is set
jakowisp 0:98cf19bcd828 59 uint16_t typeOfVoid : 2; // 0 - NULL, 1 - PTPReadParser/PTPDataSupplyer, 2 - WRITEPARSER, 3 - buffer pointer
jakowisp 0:98cf19bcd828 60 uint16_t dataSize : 6; // size of data buffer (64 bytes maximum)
jakowisp 0:98cf19bcd828 61 };
jakowisp 0:98cf19bcd828 62
jakowisp 0:98cf19bcd828 63 /**
jakowisp 0:98cf19bcd828 64 * Constructor
jakowisp 0:98cf19bcd828 65 *
jakowisp 0:98cf19bcd828 66 * @param None
jakowisp 0:98cf19bcd828 67 */
jakowisp 0:98cf19bcd828 68 USBHostPTP(void);
jakowisp 0:98cf19bcd828 69
jakowisp 0:98cf19bcd828 70 /**
jakowisp 0:98cf19bcd828 71 * Check if a PTP device is connected
jakowisp 0:98cf19bcd828 72 *
jakowisp 0:98cf19bcd828 73 * @return true if a PTP device is connected
jakowisp 0:98cf19bcd828 74 */
jakowisp 0:98cf19bcd828 75 bool connected();
jakowisp 0:98cf19bcd828 76
jakowisp 0:98cf19bcd828 77 /**
jakowisp 0:98cf19bcd828 78 * Try to connect to a PTP device
jakowisp 0:98cf19bcd828 79 *
jakowisp 0:98cf19bcd828 80 * @return true if connection was successful
jakowisp 0:98cf19bcd828 81 */
jakowisp 0:98cf19bcd828 82 bool connect();
jakowisp 0:98cf19bcd828 83
jakowisp 0:98cf19bcd828 84 bool CancelRequest(unsigned int TransactionID);
jakowisp 0:98cf19bcd828 85 bool DeviceResetRequest(void);
jakowisp 0:98cf19bcd828 86 bool GetDeviceStatus(void);
jakowisp 0:98cf19bcd828 87 bool GetExtendedEventData(void);
jakowisp 0:98cf19bcd828 88
jakowisp 0:98cf19bcd828 89 uint16_t Transaction(uint16_t operationCode, OperFlags *flags, uint32_t *params = NULL, void *pVoid = NULL);
jakowisp 0:98cf19bcd828 90 uint32_t transactionCnt;
jakowisp 0:98cf19bcd828 91 uint32_t sessionID;
jakowisp 0:98cf19bcd828 92
jakowisp 0:98cf19bcd828 93 DeviceInfoStruct deviceInfo;
jakowisp 0:98cf19bcd828 94 ObjectInfoStruct objectInfo;
jakowisp 0:98cf19bcd828 95
jakowisp 0:98cf19bcd828 96 uint32_t dataLeftToTransfer;
jakowisp 0:98cf19bcd828 97 uint32_t totalDataToTransfer;
jakowisp 0:98cf19bcd828 98
jakowisp 0:98cf19bcd828 99 // Simple PTP operation which has no data stage. Any number of uint32_t params can be supplied.
jakowisp 0:98cf19bcd828 100 uint16_t Operation(uint16_t opcode, uint8_t nparams = 0, uint32_t *params = NULL);
jakowisp 0:98cf19bcd828 101 uint16_t OpenSession();
jakowisp 0:98cf19bcd828 102 uint16_t CloseSession();
jakowisp 0:98cf19bcd828 103 uint16_t GetDeviceInfo();
jakowisp 0:98cf19bcd828 104
jakowisp 0:98cf19bcd828 105 uint16_t PowerDown();
jakowisp 0:98cf19bcd828 106 uint16_t SelfTest(uint16_t type = 0);
jakowisp 0:98cf19bcd828 107 uint16_t GetNumObjects(uint32_t *retval, uint32_t storage_id=0xffffffff, uint16_t format=0x0000, uint32_t assoc=0x0000);
jakowisp 0:98cf19bcd828 108 uint16_t GetObjectHandles(uint32_t storage_id=0xffffffff, uint16_t format=0x0000, uint16_t assoc=0x0000, void *parser=NULL);
jakowisp 0:98cf19bcd828 109 uint16_t GetObjectInfo(uint32_t handle);
jakowisp 0:98cf19bcd828 110 uint16_t GetThumb(uint32_t handle, void *parser=NULL);
jakowisp 0:98cf19bcd828 111 uint16_t GetObject(uint32_t handle, void *parser=NULL);
jakowisp 0:98cf19bcd828 112 uint16_t GetStorageIDs(void *parser=NULL);
jakowisp 0:98cf19bcd828 113 uint16_t GetStorageInfo(uint32_t storage_id, void *parser=NULL);
jakowisp 0:98cf19bcd828 114 uint16_t CopyObject(uint32_t handle, uint32_t storage_id, uint32_t parent, uint32_t *new_handle);
jakowisp 0:98cf19bcd828 115 uint16_t DeleteObject(uint32_t handle, uint16_t format);
jakowisp 0:98cf19bcd828 116 uint16_t SetObjectProtection(uint32_t handle, uint16_t attrib);
jakowisp 0:98cf19bcd828 117 uint16_t MoveObject(uint32_t handle, uint32_t storage_id, uint32_t parent);
jakowisp 0:98cf19bcd828 118 uint16_t GetDevicePropDesc(const uint16_t pcode, void *parser=NULL);
jakowisp 0:98cf19bcd828 119 uint16_t GetDevicePropValue(const uint16_t pcode, void *parser=NULL);
jakowisp 0:98cf19bcd828 120 uint16_t SetDevicePropValue(uint16_t pcode, uint32_t val);
jakowisp 0:98cf19bcd828 121 uint16_t ResetDevicePropValue(const uint16_t pcode);
jakowisp 0:98cf19bcd828 122 uint16_t FormatStore(uint32_t storage_id, uint32_t fsformat);
jakowisp 0:98cf19bcd828 123 uint16_t InitiateCapture(uint32_t storage_id, uint16_t format);
jakowisp 0:98cf19bcd828 124 uint16_t InitiateOpenCapture(uint32_t storage_id, uint16_t format);
jakowisp 0:98cf19bcd828 125 uint16_t TerminateOpenCapture(uint32_t trans_id);
jakowisp 0:98cf19bcd828 126 void DumpDeviceInfo(void);
jakowisp 0:98cf19bcd828 127 void DumpObjectInfo(void);
jakowisp 0:98cf19bcd828 128
jakowisp 0:98cf19bcd828 129 protected:
jakowisp 0:98cf19bcd828 130 //From IUSBEnumerator
jakowisp 0:98cf19bcd828 131 virtual void setVidPid(uint16_t vid, uint16_t pid);
jakowisp 0:98cf19bcd828 132 virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
jakowisp 0:98cf19bcd828 133 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
jakowisp 0:98cf19bcd828 134
jakowisp 0:98cf19bcd828 135 //This is a generic function should belong to some other class or namespance.
jakowisp 0:98cf19bcd828 136 void DumpBuffer(uint8_t *buffer,uint32_t length);
jakowisp 0:98cf19bcd828 137
jakowisp 0:98cf19bcd828 138 private:
jakowisp 0:98cf19bcd828 139 USBHost * host;
jakowisp 0:98cf19bcd828 140 USBDeviceConnected * pointerToDevice;
jakowisp 0:98cf19bcd828 141 bool deviceFound;
jakowisp 0:98cf19bcd828 142 bool deviceConnected;
jakowisp 0:98cf19bcd828 143 USBEndpoint * bulk_in;
jakowisp 0:98cf19bcd828 144 USBEndpoint * bulk_out;
jakowisp 0:98cf19bcd828 145 USBEndpoint * int_in;
jakowisp 0:98cf19bcd828 146 uint8_t numberOfEndpoints;
jakowisp 0:98cf19bcd828 147
jakowisp 0:98cf19bcd828 148 PIMAContainer commandContainer;
jakowisp 0:98cf19bcd828 149 PIMAContainer responseContainer;
jakowisp 0:98cf19bcd828 150 PIMAContainer dataContainer;
jakowisp 0:98cf19bcd828 151 PIMAContainer eventContainer;
jakowisp 0:98cf19bcd828 152 uint8_t buffer[1024];
jakowisp 0:98cf19bcd828 153 int ptp_intf;
jakowisp 0:98cf19bcd828 154
jakowisp 0:98cf19bcd828 155 void initializeClassData();
jakowisp 0:98cf19bcd828 156 int checkResult(uint8_t res, USBEndpoint * ep);
jakowisp 0:98cf19bcd828 157 bool IsCommandSupported(uint16_t opcode);
jakowisp 0:98cf19bcd828 158 bool IsPropertySupported(uint16_t code);
jakowisp 0:98cf19bcd828 159 bool CheckValueInArray(PIMAArray *array, uint16_t code);
jakowisp 0:98cf19bcd828 160 bool CheckEvent(void);
jakowisp 0:98cf19bcd828 161 static void ParseDeviceInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length);
jakowisp 0:98cf19bcd828 162 static void ParseObjectInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length);
jakowisp 0:98cf19bcd828 163 };
jakowisp 0:98cf19bcd828 164
jakowisp 0:98cf19bcd828 165 #endif
jakowisp 0:98cf19bcd828 166
jakowisp 0:98cf19bcd828 167
jakowisp 0:98cf19bcd828 168
jakowisp 0:98cf19bcd828 169