1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315 |
- /*
- * File : core.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2012, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2012-10-01 Yi Qiu first version
- * 2012-12-12 heyuanjie87 change endpoint and class handler
- */
- #include <rtthread.h>
- #include <rtdevice.h>
- static rt_list_t device_list;
- /**
- * This function will handle get_device_descriptor request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
- {
- rt_size_t size;
-
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
- /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
- size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
- USB_DESC_LENGTH_DEVICE : setup->length;
- /* send device descriptor to endpoint 0 */
- dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&device->dev_desc,
- size);
- return RT_EOK;
- }
- /**
- * This function will handle get_config_descriptor request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
- {
- rt_size_t size;
- ucfg_desc_t cfg_desc;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
- cfg_desc = &device->curr_cfg->cfg_desc;
- size = (setup->length > cfg_desc->wTotalLength) ?
- cfg_desc->wTotalLength : setup->length;
- /* send configuration descriptor to endpoint 0 */
- dcd_ep_write(device->dcd, 0, (rt_uint8_t*)cfg_desc, size);
-
- return RT_EOK;
- }
- /**
- * This function will handle get_string_descriptor request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful, -RT_ERROR on invalid request.
- */
- static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
- {
- struct ustring_descriptor str_desc;
- rt_uint8_t index, i;
- rt_uint32_t len;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
- str_desc.type = USB_DESC_TYPE_STRING;
- index = setup->value & 0xFF;
- if(index > USB_STRING_INTERFACE_INDEX)
- {
- rt_kprintf("unknown string index\n");
- dcd_ep_stall(device->dcd, 0);
- return -RT_ERROR;
- }
- if(index == 0)
- {
- str_desc.bLength = 4;
- str_desc.String[0] = 0x09;
- str_desc.String[1] = 0x04;
- }
- else
- {
- len = rt_strlen(device->str[index]);
- str_desc.bLength = len*2 + 2;
- for(i=0; i<len; i++)
- {
- str_desc.String[i*2] = device->str[index][i];
- str_desc.String[i*2 + 1] = 0;
- }
- }
- if(setup->length == 0xFF)
- len = str_desc.bLength;
- else
- len = setup->length;
- /* send string descriptor to endpoint 0 */
- dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&str_desc, len);
- return RT_EOK;
- }
- /**
- * This function will handle get_descriptor request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
- {
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
-
- if(setup->request_type == USB_REQ_TYPE_DIR_IN)
- {
- switch(setup->value >> 8)
- {
- case USB_DESC_TYPE_DEVICE:
- _get_device_descriptor(device, setup);
- break;
- case USB_DESC_TYPE_CONFIGURATION:
- _get_config_descriptor(device, setup);
- break;
- case USB_DESC_TYPE_STRING:
- _get_string_descriptor(device, setup);
- break;
- case USB_DESC_TYPE_DEVICEQUALIFIER:
- dcd_ep_stall(device->dcd, 0);
- break;
- default:
- rt_kprintf("unsupported descriptor request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
- }
- else
- {
- rt_kprintf("request direction error\n");
- dcd_ep_stall(device->dcd, 0);
- }
- return RT_EOK;
- }
- /**
- * This function will handle get_interface request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
- {
- rt_uint8_t value;
- uintf_t intf;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));
- /* find the specified interface and its alternate setting */
- intf = rt_usbd_find_interface(device, setup->index & 0xFF);
- value = intf->curr_setting->intf_desc->bAlternateSetting;
- /* send the interface alternate setting to endpoint 0*/
- dcd_ep_write(device->dcd, 0, &value, 1);
- return RT_EOK;
- }
- /**
- * This function will handle set_interface request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
- {
- uintf_t intf;
- uep_t ep;
- struct rt_list_node* i;
- ualtsetting_t setting;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
- /* find the specified interface */
- intf = rt_usbd_find_interface(device, setup->index & 0xFF);
- /* set alternate setting to the interface */
- rt_usbd_set_altsetting(intf, setup->value & 0xFF);
- setting = intf->curr_setting;
- /* start all endpoints of the interface alternate setting */
- for(i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
- {
- ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
- dcd_ep_stop(device->dcd, ep);
- dcd_ep_run(device->dcd, ep);
- }
-
- return RT_EOK;
- }
- /**
- * This function will handle get_config request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _get_config(struct udevice* device, ureq_t setup)
- {
- rt_uint8_t value;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_ASSERT(device->curr_cfg != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
- /* get current configuration */
- value = device->curr_cfg->cfg_desc.bConfigurationValue;
- /* write the current configuration to endpoint 0 */
- dcd_ep_write(device->dcd, 0, &value, 1);
- return RT_EOK;
- }
- /**
- * This function will handle set_config request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _set_config(struct udevice* device, ureq_t setup)
- {
- struct rt_list_node *i, *j, *k;
- uconfig_t cfg;
- uintf_t intf;
- ualtsetting_t setting;
- uep_t ep;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
- /* set current configuration */
- rt_usbd_set_config(device, setup->value);
- cfg = device->curr_cfg;
- for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
- {
- /* run all classes and their endpoints in the configuration */
- uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
- for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
- {
- intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
- setting = intf->curr_setting;
- for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
- {
- ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
- /* first stop then start endpoint */
- dcd_ep_stop(device->dcd, ep);
- dcd_ep_run(device->dcd, ep);
- }
- }
- /* after running all endpoints, then run class */
- if(cls->ops->run != RT_NULL)
- cls->ops->run(device, cls);
- }
-
- /* issue status stage */
- rt_device_control((rt_device_t)device->dcd, CONTROL_SEND_STATUS, RT_NULL);
-
- return RT_EOK;
- }
- /**
- * This function will handle set_address request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _set_address(struct udevice* device, ureq_t setup)
- {
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
-
- RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
- /* set address in device control driver */
- dcd_set_address(device->dcd, setup->value);
- /* issue status stage */
- rt_device_control((rt_device_t)device->dcd, CONTROL_SEND_STATUS, RT_NULL);
- return RT_EOK;
- }
- /**
- * This function will handle standard request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful.
- */
- static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
- {
- udcd_t dcd;
- rt_uint16_t value = 0;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- dcd = device->dcd;
- switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
- {
- case USB_REQ_TYPE_DEVICE:
- switch(setup->request)
- {
- case USB_REQ_GET_STATUS:
- dcd_ep_write(device->dcd, 0, &value, 2);
- break;
- case USB_REQ_CLEAR_FEATURE:
- dcd_clear_feature(dcd, setup->value);
- break;
- case USB_REQ_SET_FEATURE:
- dcd_set_feature(dcd, setup->value);
- break;
- case USB_REQ_SET_ADDRESS:
- _set_address(device, setup);
- break;
- case USB_REQ_GET_DESCRIPTOR:
- _get_descriptor(device, setup);
- break;
- case USB_REQ_SET_DESCRIPTOR:
- dcd_ep_stall(dcd, 0);
- break;
- case USB_REQ_GET_CONFIGURATION:
- _get_config(device, setup);
- break;
- case USB_REQ_SET_CONFIGURATION:
- _set_config(device, setup);
- break;
- default:
- rt_kprintf("unknown device request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
- break;
- case USB_REQ_TYPE_INTERFACE:
- switch(setup->request)
- {
- case USB_REQ_GET_INTERFACE:
- _get_interface(device, setup);
- break;
- case USB_REQ_SET_INTERFACE:
- _set_interface(device, setup);
- break;
- default:
- rt_kprintf("unknown interface request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
- break;
- case USB_REQ_TYPE_ENDPOINT:
- switch(setup->request)
- {
- case USB_REQ_GET_STATUS:
- /* TODO */
- dcd_ep_write(dcd, 0, &value, 2);
- break;
- case USB_REQ_CLEAR_FEATURE:
- dcd_clear_feature(dcd, setup->value);
- break;
- case USB_REQ_SET_FEATURE:
- dcd_set_feature(dcd, setup->value);
- break;
- case USB_REQ_SYNCH_FRAME:
- break;
- default:
- rt_kprintf("unknown endpoint request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
- break;
- case USB_REQ_TYPE_OTHER:
- rt_kprintf("unknown other type request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- default:
- rt_kprintf("unknown type request\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
- return RT_EOK;
- }
- /**
- * This function will handle class request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful, -RT_ERROR on invalid request.
- */
- static rt_err_t _class_request(udevice_t device, ureq_t setup)
- {
- uintf_t intf;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- /* verify request value */
- if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
- {
- dcd_ep_stall(device->dcd, 0);
- return -RT_ERROR;
- }
- switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
- {
- case USB_REQ_TYPE_INTERFACE:
- intf = rt_usbd_find_interface(device, setup->index & 0xFF);
- intf->handler(device, setup);
- break;
- case USB_REQ_TYPE_ENDPOINT:
- break;
- default:
- rt_kprintf("unknown class request type\n");
- dcd_ep_stall(device->dcd, 0);
- break;
- }
-
- return RT_EOK;
- }
- /**
- * This function will handle setup request.
- *
- * @param device the usb device object.
- * @param setup the setup request.
- *
- * @return RT_EOK on successful, -RT_ERROR on invalid request.
- */
- static rt_err_t _setup_request(udevice_t device, ureq_t setup)
- {
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(setup != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n", setup->request_type));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
- RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
-
- switch((setup->request_type & USB_REQ_TYPE_MASK))
- {
- case USB_REQ_TYPE_STANDARD:
- _standard_request(device, setup);
- break;
- case USB_REQ_TYPE_CLASS:
- _class_request(device, setup);
- break;
- case USB_REQ_TYPE_VENDOR:
- rt_kprintf("vendor type request\n");
- break;
- default:
- rt_kprintf("unknown setup request type\n");
- dcd_ep_stall(device->dcd, 0);
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- /**
- * This function will notity sof event to all of class.
- *
- * @param device the usb device object.
- *
- * @return RT_EOK.
- */
- rt_err_t _sof_notify(udevice_t device)
- {
- struct rt_list_node *i;
- uclass_t cls;
-
- RT_ASSERT(device != RT_NULL);
- /* to notity every class that sof event comes */
- for (i=device->curr_cfg->cls_list.next;
- i!=&device->curr_cfg->cls_list; i=i->next)
- {
- cls = (uclass_t)rt_list_entry(i, struct uclass, list);
- if(cls->ops->sof_handler != RT_NULL)
- cls->ops->sof_handler(device, cls);
- }
- return RT_EOK;
- }
- /**
- * This function will create an usb device object.
- *
- * @param ustring the usb string array to contain string descriptor.
- *
- * @return an usb device object on success, RT_NULL on fail.
- */
- udevice_t rt_usbd_device_create(const char** ustring)
- {
- udevice_t udevice;
- /* parameter check */
- RT_ASSERT(ustring != RT_NULL);
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));
- /* allocate memory for the object */
- udevice = rt_malloc(sizeof(struct udevice));
- if(udevice == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- rt_memset(udevice, 0, sizeof(struct udevice));
-
- /* set string descriptor array to the device object */
- udevice->str = ustring;
- /* to initialize configuration list */
- rt_list_init(&udevice->cfg_list);
- /* insert the device object to device list */
- rt_list_insert_after(&device_list, &udevice->list);
- return udevice;
- }
- /**
- * This function will set an usb controller driver to a device.
- *
- * @param device the usb device object.
- * @param dcd the usb device controller driver.
- *
- * @return RT_EOK on successful.
- */
- rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
- {
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(dcd != RT_NULL);
- /* set usb device controller driver to the device */
- device->dcd = dcd;
- return RT_EOK;
- }
- /**
- * This function will set an usb device descriptor to a device.
- *
- * @param device the usb device object.
- * @param dev_desc the usb device descriptor.
- *
- * @return RT_EOK on successful.
- */
- rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
- {
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(dev_desc != RT_NULL);
-
- /* copy the usb device descriptor to the device */
- rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
-
- return RT_EOK;
- }
- /**
- * This function will create an usb configuration object.
- *
- * @param none.
- *
- * @return an usb configuration object.
- */
- uconfig_t rt_usbd_config_create(void)
- {
- uconfig_t cfg;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));
- /* allocate memory for the object */
- cfg = rt_malloc(sizeof(struct uconfig));
- if(cfg == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- rt_memset(cfg, 0, sizeof(struct uconfig));
- /* set default value */
- cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
- cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
- cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
- cfg->cfg_desc.bmAttributes = 0xC0;
- cfg->cfg_desc.MaxPower = 0x32;
- /* to initialize class object list */
- rt_list_init(&cfg->cls_list);
- return cfg;
- }
- /**
- * This function will create an usb interface object.
- *
- * @param device the usb device object.
- * @handler the callback handler of object
- *
- * @return an usb interface object on success, RT_NULL on fail.
- */
- uintf_t rt_usbd_interface_create(udevice_t device,
- rt_err_t (*handler)(struct udevice*, ureq_t setup))
- {
- uintf_t intf;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
-
- /* allocate memory for the object */
- intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
- if(intf == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- intf->intf_num = device->nr_intf;
- device->nr_intf++;
- intf->handler = handler;
- intf->curr_setting = RT_NULL;
- /* to initialize the alternate setting object list */
- rt_list_init(&intf->setting_list);
- return intf;
- }
- /**
- * This function will create an usb alternate setting object.
- *
- * @param intf_desc the interface descriptor.
- * @desc_size the size of the interface descriptor.
- *
- * @return an usb alternate setting object on success, RT_NULL on fail.
- */
- ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
- {
- ualtsetting_t setting;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));
- /* parameter check */
- RT_ASSERT(desc_size > 0);
- /* allocate memory for the object */
- setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
- if(setting == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- /* allocate memory for the desc */
- setting->desc = rt_malloc(desc_size);
- if (setting->desc == RT_NULL)
- {
- rt_kprintf("alloc desc memery failed\n");
- rt_free(setting);
- return RT_NULL;
- }
- setting->desc_size = desc_size;
- setting->intf_desc = RT_NULL;
- /* to initialize endpoint list */
- rt_list_init(&setting->ep_list);
- return setting;
- }
- /**
- * This function will config an desc in alternate setting object.
- *
- * @param setting the altsetting to be config.
- * @param desc use it to init desc in setting.
- * @param intf_pos the offset of interface descriptor in desc.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
- {
- RT_ASSERT(setting != RT_NULL);
- RT_ASSERT(setting->desc !=RT_NULL);
-
- rt_memcpy(setting->desc, desc, setting->desc_size);
- setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
-
- return RT_EOK;
- }
- /**
- * This function will create an usb class object.
- *
- * @param device the usb device object.
- * @param dev_desc the device descriptor.
- * @param ops the operation set.
- *
- * @return an usb class object on success, RT_NULL on fail.
- */
- uclass_t rt_usbd_class_create(udevice_t device, udev_desc_t dev_desc,
- uclass_ops_t ops)
- {
- uclass_t cls;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(dev_desc != RT_NULL);
- /* allocate memory for the object */
- cls = (uclass_t)rt_malloc(sizeof(struct uclass));
- if(cls == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- cls->dev_desc = dev_desc;
- cls->ops = ops;
- cls->device = device;
- /* to initialize interface list */
- rt_list_init(&cls->intf_list);
- return cls;
- }
- /**
- * This function will create an usb endpoint object.
- *
- * @param ep_desc the endpoint descriptor.
- * @handler the callback handler of object
- *
- * @return an usb endpoint object on success, RT_NULL on fail.
- */
- uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
- {
- uep_t ep;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
- /* parameter check */
- RT_ASSERT(ep_desc != RT_NULL);
- /* allocate memory for the object */
- ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
- if(ep == RT_NULL)
- {
- rt_kprintf("alloc memery failed\n");
- return RT_NULL;
- }
- ep->ep_desc = ep_desc;
- ep->handler = handler;
- ep->buffer = RT_NULL;
- return ep;
- }
- /**
- * This function will find an usb device object.
- *
- * @dcd usd device controller driver.
- *
- * @return an usb device object on found or RT_NULL on not found.
- */
- udevice_t rt_usbd_find_device(udcd_t dcd)
- {
- struct rt_list_node* node;
- udevice_t device;
-
- /* parameter check */
- RT_ASSERT(dcd != RT_NULL);
-
- /* search a device in the the device list */
- for (node = device_list.next; node != &device_list; node = node->next)
- {
- device = (udevice_t)rt_list_entry(node, struct udevice, list);
- if(device->dcd == dcd) return device;
- }
- rt_kprintf("can't find device\n");
- return RT_NULL;
- }
- /**
- * This function will find an usb configuration object.
- *
- * @param device the usb device object.
- * @param value the configuration number.
- *
- * @return an usb configuration object on found or RT_NULL on not found.
- */
- uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
- {
- struct rt_list_node* node;
- uconfig_t cfg = RT_NULL;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
- /* search a configration in the the device */
- for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
- {
- cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
- if(cfg->cfg_desc.bConfigurationValue == value) return cfg;
- }
- rt_kprintf("can't find configuration %d\n", value);
- return RT_NULL;
- }
- /**
- * This function will find an usb interface object.
- *
- * @param device the usb device object.
- * @param value the interface number.
- *
- * @return an usb configuration object on found or RT_NULL on not found.
- */
- uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value)
- {
- struct rt_list_node *i, *j;
- uclass_t cls;
- uintf_t intf;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(value < device->nr_intf);
- /* search an interface in the current configuration */
- for (i=device->curr_cfg->cls_list.next;
- i!=&device->curr_cfg->cls_list; i=i->next)
- {
- cls = (uclass_t)rt_list_entry(i, struct uclass, list);
- for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
- {
- intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
- if(intf->intf_num == value)
- return intf;
- }
- }
- rt_kprintf("can't find interface %d\n", value);
- return RT_NULL;
- }
- /**
- * This function will find an usb interface alternate setting object.
- *
- * @param device the usb device object.
- * @param value the alternate setting number.
- *
- * @return an usb interface alternate setting object on found or RT_NULL on not found.
- */
- ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
- {
- struct rt_list_node *i;
- ualtsetting_t setting;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
- /* parameter check */
- RT_ASSERT(intf != RT_NULL);
- if(intf->curr_setting != RT_NULL)
- {
- /* if the value equal to the current alternate setting, then do not search */
- if(intf->curr_setting->intf_desc->bAlternateSetting == value)
- return intf->curr_setting;
- }
-
- /* search a setting in the alternate setting list */
- for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
- {
- setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
- if(setting->intf_desc->bAlternateSetting == value)
- return setting;
- }
- rt_kprintf("can't find alternate setting %d\n", value);
- return RT_NULL;
- }
- /**
- * This function will find an usb endpoint object.
- *
- * @param device the usb device object.
- * @param ep_addr endpoint address.
- *
- * @return an usb endpoint object on found or RT_NULL on not found.
- */
- uep_t rt_usbd_find_endpoint(udevice_t device, uclass_t* pcls, rt_uint8_t ep_addr)
- {
- uep_t ep;
- struct rt_list_node *i, *j, *k;
- uclass_t cls;
- uintf_t intf;
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
-
- /* search a endpoint in the current configuration */
- for (i=device->curr_cfg->cls_list.next;
- i!=&device->curr_cfg->cls_list; i=i->next)
- {
- cls = (uclass_t)rt_list_entry(i, struct uclass, list);
- for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
- {
- intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
- for(k=intf->curr_setting->ep_list.next;
- k!=&intf->curr_setting->ep_list; k=k->next)
- {
- ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
- if(ep->ep_desc->bEndpointAddress == ep_addr)
- {
- if (pcls != RT_NULL)
- *pcls = cls;
- return ep;
- }
- }
- }
- }
- rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
- return RT_NULL;
- }
- /**
- * This function will add a configuration to an usb device.
- *
- * @param device the usb device object.
- * @param cfg the configuration object.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
- {
- struct rt_list_node *i, *j, *k;
- uclass_t cls;
- uintf_t intf;
- uep_t ep;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(cfg != RT_NULL);
- /* set configuration number to the configuration descriptor */
- cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
- device->dev_desc.bNumConfigurations++;
- for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
- {
- cls = (uclass_t)rt_list_entry(i, struct uclass, list);
- for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
- {
- intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
- cfg->cfg_desc.bNumInterfaces++;
-
- /* allocate address for every endpoint in the interface alternate setting */
- for(k=intf->curr_setting->ep_list.next;
- k!=&intf->curr_setting->ep_list; k=k->next)
- {
- ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
- dcd_ep_alloc(device->dcd, ep);
- }
- /* construct complete configuration descriptor */
- rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength -
- USB_DESC_LENGTH_CONFIG], (void*)intf->curr_setting->desc,
- intf->curr_setting->desc_size);
- cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
- }
- }
- /* insert the configuration to the list */
- rt_list_insert_after(&device->cfg_list, &cfg->list);
- return RT_EOK;
- }
- /**
- * This function will add a class to a configuration.
- *
- * @param cfg the configuration object.
- * @param cls the class object.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
- {
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
- /* parameter check */
- RT_ASSERT(cfg != RT_NULL);
- RT_ASSERT(cls != RT_NULL);
- /* insert the class to the list */
- rt_list_insert_after(&cfg->cls_list, &cls->list);
-
- return RT_EOK;
- }
- /**
- * This function will add an interface to a class.
- *
- * @param cls the class object.
- * @param intf the interface object.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
- {
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
- /* parameter check */
- RT_ASSERT(cls != RT_NULL);
- RT_ASSERT(intf != RT_NULL);
- /* insert the interface to the list */
- rt_list_insert_after(&cls->intf_list, &intf->list);
- return RT_EOK;
- }
- /**
- * This function will add an alternate setting to an interface.
- *
- * @param intf the interface object.
- * @param setting the alternate setting object.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
- {
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
- /* parameter check */
- RT_ASSERT(intf != RT_NULL);
- RT_ASSERT(setting != RT_NULL);
- setting->intf_desc->bInterfaceNumber = intf->intf_num;
- /* insert the alternate setting to the list */
- rt_list_insert_after(&intf->setting_list, &setting->list);
- return RT_EOK;
- }
- /**
- * This function will add an endpoint to an alternate setting.
- *
- * @param setting the alternate setting object.
- * @param ep the endpoint object.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
- {
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
- /* parameter check */
- RT_ASSERT(setting != RT_NULL);
- RT_ASSERT(ep != RT_NULL);
- /* insert the endpoint to the list */
- rt_list_insert_after(&setting->ep_list, &ep->list);
- return RT_EOK;
- }
- /**
- * This function will set an alternate setting for an interface.
- *
- * @param intf_desc the interface descriptor.
- * @param value the alternate setting number.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
- {
- ualtsetting_t setting;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
- /* parameter check */
- RT_ASSERT(intf != RT_NULL);
- RT_ASSERT(setting != RT_NULL);
- /* find an alternate setting */
- setting = rt_usbd_find_altsetting(intf, value);
- /* set as current alternate setting */
- intf->curr_setting = setting;
- return RT_EOK;
- }
- /**
- * This function will set a configuration for an usb device.
- *
- * @param device the usb device object.
- * @param value the configuration number.
- *
- * @return RT_EOK.
- */
- rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
- {
- uconfig_t cfg;
- RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
- /* parameter check */
- RT_ASSERT(device != RT_NULL);
- RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
- /* find a configuration */
- cfg = rt_usbd_find_config(device, value);
- /* set as current configuration */
- device->curr_cfg = cfg;
- return RT_TRUE;
- }
- static struct rt_messagequeue *usb_mq;
- /**
- * This function is the main entry of usb device thread, it is in charge of
- * processing all messages received from the usb message buffer.
- *
- * @param parameter the parameter of the usb device thread.
- *
- * @return none.
- */
- static void rt_usbd_thread_entry(void* parameter)
- {
- while(1)
- {
- struct udev_msg msg;
- udevice_t device;
- uclass_t cls;
- uep_t ep;
-
- /* receive message */
- if(rt_mq_recv(usb_mq, &msg, sizeof(struct udev_msg), RT_WAITING_FOREVER)
- != RT_EOK ) continue;
-
- switch (msg.type)
- {
- case USB_MSG_SETUP_NOTIFY:
- device = rt_usbd_find_device(msg.dcd);
- if(device != RT_NULL)
- _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
- else
- rt_kprintf("invalid usb device\n");
- break;
- case USB_MSG_DATA_NOTIFY:
- ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
- if(ep != RT_NULL)
- ep->handler(device, cls, msg.content.ep_msg.size);
- else
- rt_kprintf("invalid endpoint\n");
- break;
- case USB_MSG_SOF:
- device = rt_usbd_find_device(msg.dcd);
- if(device != RT_NULL)
- _sof_notify(device);
- else
- rt_kprintf("invalid usb device\n");
- break;
- default:
- break;
- }
- }
- }
- /**
- * This function will post an message to usb message queue,
- *
- * @param msg the message to be posted
- * @param size the size of the message .
- *
- * @return the error code, RT_EOK on successfully.
- */
- rt_err_t rt_usbd_post_event(struct udev_msg* msg, rt_size_t size)
- {
- RT_ASSERT(msg != RT_NULL);
- /* send message to usb message queue */
- return rt_mq_send(usb_mq, (void*)msg, size);
- }
- /**
- * This function will initialize usb device thread.
- *
- * @return none.
- *
- */
- rt_err_t rt_usbd_core_init(void)
- {
- rt_thread_t thread;
-
- rt_list_init(&device_list);
-
- /* create an usb message queue */
- usb_mq = rt_mq_create("usbd", 32, 16, RT_IPC_FLAG_FIFO);
-
- /* create usb device thread */
- thread = rt_thread_create("usbd", rt_usbd_thread_entry, RT_NULL,
- 2048, 8, 20);
- if(thread != RT_NULL)
- {
- /* startup usb device thread */
- rt_thread_startup(thread);
- }
-
- return RT_EOK;
- }
|