adk.c 11.8 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5
/*
 * File      : adk.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2011, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
M
Ming, Bai 已提交
19 20 21 22 23 24 25 26 27 28
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-12-12     Yi Qiu      first version
 */

#include <rtthread.h>
#include <drivers/usb_host.h>
#include "adk.h"

29
#ifdef RT_USBH_ADK
M
Ming, Bai 已提交
30 31 32 33 34 35 36 37 38

static struct uclass_driver adk_driver;
static const char* _adk_manufacturer = RT_NULL;
static const char* _adk_model = RT_NULL;
static const char* _adk_description = RT_NULL;
static const char* _adk_version = RT_NULL;
static const char* _adk_uri = RT_NULL;
static const char* _adk_serial = RT_NULL;

39
rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
M
Ming, Bai 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    const char* description, const char* _version, const char* uri, 
    const char* serial)
{
    _adk_manufacturer = manufacturer;
    _adk_model = model;
    _adk_description = description;
    _adk_version = _version;
    _adk_uri = uri;
    _adk_serial = serial;

    return RT_EOK;
}

#ifdef RT_USING_MODULE
#include <rtm.h>

56
RTM_EXPORT(rt_usbh_adk_set_string);
M
Ming, Bai 已提交
57 58 59 60 61
#endif

/**
 * This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
 *
62
 * @param intf the interface instance.
M
Ming, Bai 已提交
63 64 65 66 67
 * @duration the idle period of requesting data.
 * @report_id the report id
 * 
 * @return the error code, RT_EOK on successfully.
*/
68
static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
M
Ming, Bai 已提交
69 70
{
    struct ureqest setup;
71
    uinst_t device;    
M
Ming, Bai 已提交
72 73 74
    int timeout = 100;
            
        /* parameter check */
75 76
    RT_ASSERT(intf != RT_NULL);
    RT_ASSERT(intf->device != RT_NULL);
M
Ming, Bai 已提交
77

78
    device = intf->device;
M
Ming, Bai 已提交
79 80 81 82 83 84 85 86

    setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR | 
        USB_REQ_TYPE_DEVICE;
    setup.request = USB_REQ_GET_PROTOCOL;
    setup.index = 0;
    setup.length = 2;
    setup.value = 0;

87
    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2, 
M
Ming, Bai 已提交
88 89 90 91 92 93 94
        timeout) == 0) return RT_EOK;
    else return -RT_FALSE;    
}

/**
 * This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
 *
95
 * @param intf the interface instance.
M
Ming, Bai 已提交
96 97 98 99 100
 * @duration the idle period of requesting data.
 * @report_id the report id
 * 
 * @return the error code, RT_EOK on successfully.
*/
101
static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, 
M
Ming, Bai 已提交
102 103 104
    const char* str)
{
    struct ureqest setup;
105
    uinst_t device;    
M
Ming, Bai 已提交
106 107 108
    int timeout = 100;
            
        /* parameter check */
109 110
    RT_ASSERT(intf != RT_NULL);
    RT_ASSERT(intf->device != RT_NULL);
M
Ming, Bai 已提交
111

112
    device = intf->device;
M
Ming, Bai 已提交
113 114 115 116 117 118 119 120

    setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | 
        USB_REQ_TYPE_DEVICE;
    setup.request = USB_REQ_SEND_STRING;
    setup.index = index;
    setup.length = rt_strlen(str) + 1;
    setup.value = 0;

121
    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str, 
M
Ming, Bai 已提交
122 123 124 125 126 127 128
        rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
    else return -RT_FALSE;   
}

/**
 * This function will do USB_REQ_START request to set idle period to the usb adk device
 *
129
 * @param intf the interface instance.
M
Ming, Bai 已提交
130 131 132 133 134
 * @duration the idle period of requesting data.
 * @report_id the report id
 * 
 * @return the error code, RT_EOK on successfully.
*/
135
static rt_err_t rt_usbh_adk_start(struct uintf* intf)
M
Ming, Bai 已提交
136 137
{
    struct ureqest setup;
138
    uinst_t device;    
M
Ming, Bai 已提交
139 140 141
    int timeout = 100;
            
        /* parameter check */
142 143
    RT_ASSERT(intf != RT_NULL);
    RT_ASSERT(intf->device != RT_NULL);
M
Ming, Bai 已提交
144

145
    device = intf->device;
M
Ming, Bai 已提交
146 147 148 149 150 151 152 153

    setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | 
        USB_REQ_TYPE_DEVICE;
    setup.request = USB_REQ_START;
    setup.index = 0;
    setup.length = 0;
    setup.value = 0;

154
    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
M
Ming, Bai 已提交
155 156 157 158 159 160 161
        timeout) == 0) return RT_EOK;
    else return -RT_FALSE;   
}

/**
 * This function will read data from usb adk device
 *
162
 * @param intf the interface instance.
M
Ming, Bai 已提交
163 164 165
 * 
 * @return the error code, RT_EOK on successfully.
*/
166
static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer, 
M
Ming, Bai 已提交
167 168
    rt_size_t size)
{
169
    uadk_t adk;
M
Ming, Bai 已提交
170
    rt_size_t length;
171
    struct uintf* intf;
M
Ming, Bai 已提交
172 173 174 175 176

    /* check parameter */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(buffer != RT_NULL);

177 178
    intf = (struct uintf*)device->user_data;
    adk = (uadk_t)intf->user_data;
M
Ming, Bai 已提交
179

180
    length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in, 
M
Ming, Bai 已提交
181 182 183 184 185 186 187 188 189
        buffer, size, 300);
    
    return length;

}

/**
 * This function will write data to usb adk device
 *
190
 * @param intf the interface instance.
M
Ming, Bai 已提交
191 192 193
 * 
 * @return the error code, RT_EOK on successfully.
*/
194
static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer, 
M
Ming, Bai 已提交
195 196
    rt_size_t size)
{
197
    uadk_t adk;
M
Ming, Bai 已提交
198
    rt_size_t length;
199
    struct uintf* intf;
M
Ming, Bai 已提交
200 201 202

    RT_ASSERT(buffer != RT_NULL);    

203 204
    intf = (struct uintf*)device->user_data;
    adk = (uadk_t)intf->user_data;
M
Ming, Bai 已提交
205

206
    length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out, 
M
Ming, Bai 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
        (void*)buffer, size, 300);
    
    return length;
}

/**
 * This function will run adk class driver when usb device is detected and identified
 *  as a adk class device, it will continue the enumulate process.
 *
 * @param arg the argument.
 * 
 * @return the error code, RT_EOK on successfully.
 */
220
static rt_err_t rt_usbh_adk_enable(void* arg)
M
Ming, Bai 已提交
221 222
{
    int i = 0;
223 224
    uadk_t adk;
    struct uintf* intf = (struct uintf*)arg;
M
Ming, Bai 已提交
225 226 227 228 229
    udev_desc_t dev_desc;
    rt_uint16_t protocol;
    rt_err_t ret;    
    
    /* parameter check */
230
    if(intf == RT_NULL)
M
Ming, Bai 已提交
231 232 233 234 235
    {
        rt_kprintf("the interface is not available\n");
        return -RT_EIO;
    }

236
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n"));
M
Ming, Bai 已提交
237
        
238
    dev_desc = &intf->device->dev_desc;
M
Ming, Bai 已提交
239 240 241 242
    if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID && 
        (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID || 
        dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
    {
243
        if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
M
Ming, Bai 已提交
244 245 246 247 248 249 250
    
        RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n"));        
    }
    else
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n"));        
        
251
        if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK)
M
Ming, Bai 已提交
252
        {
253
            rt_kprintf("rt_usbh_adk_get_protocol failed\n");
M
Ming, Bai 已提交
254 255 256 257 258 259 260 261 262
            return ret;
        }

        if(protocol != 1) 
        {
            rt_kprintf("read protocol failed\n");
            return -RT_ERROR;
        }       

263
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
264
            ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
265
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
266
            ACCESSORY_STRING_MODEL, _adk_model);
267
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
268
            ACCESSORY_STRING_DESCRIPTION, _adk_description);
269
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
270
            ACCESSORY_STRING_VERSION, _adk_version);
271
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
272
            ACCESSORY_STRING_URI, _adk_uri);        
273
        rt_usbh_adk_send_string(intf, 
M
Ming, Bai 已提交
274 275 276 277 278 279 280 281 282
            ACCESSORY_STRING_SERIAL, _adk_serial);            

        RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer));
        RT_DEBUG_LOG(RT_DEBUG_USB, ("model %s\n", _adk_model));
        RT_DEBUG_LOG(RT_DEBUG_USB, ("description %s\n", _adk_description));
        RT_DEBUG_LOG(RT_DEBUG_USB, ("version %s\n", _adk_version));
        RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri));       
        RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _adk_serial));               
        
283
        if((ret = rt_usbh_adk_start(intf)) != RT_EOK)
M
Ming, Bai 已提交
284
        {
285
            rt_kprintf("rt_usbh_adk_start failed\n");
M
Ming, Bai 已提交
286 287 288 289 290 291
            return ret;
        }        

        return RT_EOK;
    }
    
292 293
    adk = rt_malloc(sizeof(struct uadkinst));
    RT_ASSERT(adk != RT_NULL);
M
Ming, Bai 已提交
294 295

    /* initilize the data structure */
296 297
    rt_memset(adk, 0, sizeof(struct uadkinst));    
    intf->user_data = (void*)adk;
M
Ming, Bai 已提交
298

299
    for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
M
Ming, Bai 已提交
300 301 302 303
    {        
        uep_desc_t ep_desc;
        
        /* get endpoint descriptor from interface descriptor */
304
        rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
M
Ming, Bai 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318
        if(ep_desc == RT_NULL)
        {
            rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
            return -RT_ERROR;
        }
        
        /* the endpoint type of adk class should be BULK */    
        if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
            continue;
        
        /* allocate pipes according to the endpoint type */
        if(ep_desc->bEndpointAddress & USB_DIR_IN)
        {
            /* allocate an in pipe for the adk instance */
319 320
            ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in, 
                intf, ep_desc, RT_NULL);
M
Ming, Bai 已提交
321 322 323 324 325
            if(ret != RT_EOK) return ret;
        }
        else
        {        
            /* allocate an output pipe for the adk instance */
326 327
            ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out, 
                intf, ep_desc, RT_NULL);            
M
Ming, Bai 已提交
328 329 330 331 332
            if(ret != RT_EOK) return ret;
        }
    }

    /* check pipes infomation */
333
    if(adk->pipe_in == RT_NULL || adk->pipe_out == RT_NULL)
M
Ming, Bai 已提交
334 335 336 337 338 339
    {
        rt_kprintf("pipe error, unsupported device\n");
        return -RT_ERROR;
    }    

    /* set configuration */
340
    ret = rt_usbh_set_configure(intf->device, 1);
M
Ming, Bai 已提交
341 342 343
    if(ret != RT_EOK) return ret;

    /* register adk device */
344 345 346 347 348 349 350 351 352 353
    adk->device.type  = RT_Device_Class_Char;                         
    adk->device.init = RT_NULL;         
    adk->device.open = RT_NULL;         
    adk->device.close = RT_NULL;                 
    adk->device.read = rt_usbh_adk_read;
    adk->device.write = rt_usbh_adk_write;
    adk->device.control = RT_NULL;
    adk->device.user_data = (void*)intf;

    rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
M
Ming, Bai 已提交
354 355 356 357 358 359 360 361 362 363 364 365
    
    return RT_EOK;
}

/**
 * This function will be invoked when usb device plug out is detected and it would clean 
 * and release all hub class related resources.
 *
 * @param arg the argument.
 * 
 * @return the error code, RT_EOK on successfully.
 */
366
static rt_err_t rt_usbh_adk_disable(void* arg)
M
Ming, Bai 已提交
367
{
368 369
    uadk_t adk;
    struct uintf* intf = (struct uintf*)arg;
M
Ming, Bai 已提交
370

371
    RT_ASSERT(intf != RT_NULL);
M
Ming, Bai 已提交
372

373
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n"));
M
Ming, Bai 已提交
374

375 376
    adk = (uadk_t)intf->user_data;
    if(adk == RT_NULL) 
M
Ming, Bai 已提交
377
    {
378
        rt_free(intf);    
M
Ming, Bai 已提交
379 380 381
        return RT_EOK;
    }
    
382 383
    if(adk->pipe_in != RT_NULL)
        rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
M
Ming, Bai 已提交
384

385 386
    if(adk->pipe_out != RT_NULL)
        rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_out);
M
Ming, Bai 已提交
387 388

    /* unregister adk device */
389
    rt_device_unregister(&adk->device);
M
Ming, Bai 已提交
390 391

    /* free adk instance */
392 393 394 395
    if(adk != RT_NULL) 
    {
        rt_free(adk);
    }
M
Ming, Bai 已提交
396 397
    
    /* free interface instance */
398
    rt_free(intf);
M
Ming, Bai 已提交
399 400 401 402 403 404 405 406 407 408

    return RT_EOK;
}

/**
 * This function will register adk class driver to the usb class driver manager.
 * and it should be invoked in the usb system initialization.
 * 
 * @return the error code, RT_EOK on successfully.
 */
409
ucd_t rt_usbh_class_driver_adk(void)
M
Ming, Bai 已提交
410 411 412
{
    adk_driver.class_code = USB_CLASS_ADK;
    
413 414
    adk_driver.enable = rt_usbh_adk_enable;
    adk_driver.disable = rt_usbh_adk_disable;
M
Ming, Bai 已提交
415 416 417 418 419 420

    return &adk_driver;
}

#endif