/* * Init */ static int __init usb_init(void) { int retval; if (nousb) { pr_info("%s: USB support disabled\n", usbcore_name); return 0; } retval = bus_register(&usb_bus_type); if (retval) goto bus_register_failed; retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb); if (retval) goto bus_notifier_failed; retval = usb_host_init(); if (retval) goto host_init_failed; retval = usb_major_init(); if (retval) goto major_init_failed; retval = usb_register(&usbfs_driver); if (retval) goto driver_register_failed; retval = usb_devio_init(); if (retval) goto usb_devio_init_failed; retval = usbfs_init(); if (retval) goto fs_init_failed; retval = usb_hub_init(); if (retval) goto hub_init_failed; retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); if (!retval) goto out; usb_hub_cleanup(); hub_init_failed: usbfs_cleanup(); fs_init_failed: usb_devio_cleanup(); usb_devio_init_failed: usb_deregister(&usbfs_driver); driver_register_failed: usb_major_cleanup(); major_init_failed: usb_host_cleanup(); host_init_failed: bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); bus_notifier_failed: bus_unregister(&usb_bus_type); bus_register_failed: out: return retval; }
/* usb_init */ void usb_init(void) { usb_status status = USB_OK; status = usb_host_init(CONTROLLER_ID, &g_video_camera.host_handle); if (status != USB_OK) { USB_PRINTF("\r\nUSB Host Initialization failed! STATUS: 0x%x", status); return; } /* ** since we are going to act as the host driver, register the driver ** information for wanted class/subclass/protocols */ status = usb_host_register_driver_info(g_video_camera.host_handle, (void *)DriverInfoTable); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } status = usb_host_register_unsupported_device_notify(g_video_camera.host_handle, usb_host_video_unsupported_device_event); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } g_video_camera.video_camera_control_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_control_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_camera_stream_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_stream_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_command_ptr = (video_command_t*)OS_Mem_alloc_zero(sizeof(video_command_t)); if (g_video_camera.video_command_ptr == NULL) { USB_PRINTF("\r\nOS_Mem_alloc_zero failed!\r\n"); return; } g_video_camera.stream_interface_alternate = 0; time_init(); USB_PRINTF("Video camera starting...\r\n"); }
/* * Init */ static int __init usb_init(void) { int retval; if (nousb) { pr_info ("%s: USB support disabled\n", usbcore_name); return 0; } //总线对象注册 retval = bus_register(&usb_bus_type); if (retval) goto out; //usb-host retval = usb_host_init(); if (retval) goto host_init_failed; retval = usb_major_init(); if (retval) goto major_init_failed; //usbfs注册 retval = usb_register(&usbfs_driver); if (retval) goto driver_register_failed; retval = usbdev_init(); if (retval) goto usbdevice_init_failed; retval = usbfs_init(); if (retval) goto fs_init_failed; //初始化hub模块:注册hub驱动、创建hub端口检测线程 retval = usb_hub_init(); if (retval) goto hub_init_failed; retval = driver_register(&usb_generic_driver); if (!retval) goto out; usb_hub_cleanup(); hub_init_failed: usbfs_cleanup(); fs_init_failed: usbdev_cleanup(); usbdevice_init_failed: usb_deregister(&usbfs_driver); driver_register_failed: usb_major_cleanup(); major_init_failed: usb_host_cleanup(); host_init_failed: bus_unregister(&usb_bus_type); out: return retval; }
int main(int argc, char* argv[]) { printf("CTS USB Accessory Tester\n"); struct usb_host_context* context = usb_host_init(); if (!context) { fprintf(stderr, "usb_host_init failed"); return 1; } // this will never return so it is safe to pass thiz directly usb_host_run(context, usb_device_added, usb_device_removed, NULL, NULL); return 0; }
static int test_list_devices(struct harness_t *harness_p) { BTASSERT(usb_host_init(&usb, &usb_device[0], host_devices, membersof(host_devices)) == 0); BTASSERT(usb_host_start(&usb) == 0); std_printf(FSTR("ADDRESS CLASS VENDOR PRODUCT\r\n")); BTASSERT(usb_host_stop(&usb) == 0); return (0); }
static int storage_init(fat16_read_t *read_p, fat16_write_t *write_p, void **arg_pp) { struct usb_host_device_t *device_p; union usb_message_t message; std_printf(FSTR("USB storage.\r\n")); /* Initialize the USB host driver. */ usb_host_init(&usb, &usb_device[0], host_devices, membersof(host_devices)); usb_host_class_mass_storage_init(&mass_storage, &usb, mass_storage_devices, membersof(mass_storage_devices)); usb_host_class_mass_storage_start(&mass_storage); /* Start the USB driver. */ usb_host_start(&usb); chan_read(&usb.control, &message, sizeof(message)); std_printf(FSTR("The USB control thread read a message of type %d\r\n"), message.header.type); if (message.header.type != USB_MESSAGE_TYPE_ADD) { std_printf(FSTR("bad message type %d\r\n"), message.header.type); return (-1); } device_p = usb_host_device_open(&usb, message.add.device); *read_p = read_block; *write_p = write_block; *arg_pp = device_p; return (0); }
int lsusb_main(int argc, char **argv) { struct usb_host_context *ctx; if (argc == 2 && !strcmp(argv[1], "-v")) verbose = 1; ctx = usb_host_init(); if (!ctx) { perror("usb_host_init:"); return 1; } usb_host_run(ctx, lsusb_device_added, lsusb_device_removed, lsusb_discovery_done, NULL); usb_host_cleanup(ctx); return 0; }
/** * bus event initialization * * @param [in] flash * @param [in] bb baseband * @param [in] mcdr core dump * * @return a valid bus_ev_hdle_t pointer if succeed * @return NULL otherwise */ bus_ev_hdle_t *bus_ev_init(link_t *flash, link_t *bb, link_t *mcdr) { bool err = false; bool usb = false; bus_ev_t *bus_events = NULL; ASSERT(flash != NULL); ASSERT(bb != NULL); ASSERT(mcdr != NULL); bus_events = calloc(1, sizeof(bus_ev_t)); if (!bus_events) { LOG_ERROR("memory allocation failed"); goto err; } bus_events->wd_fd = CLOSED_FD; if (flash->type == E_LINK_USB) { usb = true; if ((flash->usb.pid != 0) && (flash->usb.vid != 0)) { bus_events->modem_flash_pid = flash->usb.pid; bus_events->modem_flash_vid = flash->usb.vid; } else { LOG_ERROR("wrong PID/VID for the flashing interface"); err = true; } } if (bb->type == E_LINK_USB) { usb = true; if ((bb->usb.pid != 0) && (bb->usb.vid != 0)) { bus_events->modem_bb_pid = bb->usb.pid; bus_events->modem_bb_vid = bb->usb.vid; } else { LOG_ERROR("wrong PID/VID for the baseband interface"); err = true; } } if (mcdr->type == E_LINK_USB) { usb = true; if ((mcdr->usb.pid != 0) && (mcdr->usb.vid != 0)) { bus_events->mcdr_bb_pid = mcdr->usb.pid; bus_events->mcdr_bb_vid = mcdr->usb.vid; } else { LOG_ERROR("wrong PID/VID for the core dump interface"); err = true; } } if (usb && !err) { if ((bus_events->ctx = usb_host_init()) == NULL) goto err; /* @TODO: handle errors */ usb_host_load(bus_events->ctx, device_added_cb, device_rmed_cb, NULL, &bus_events->cli_ctx); /* when calling usb_host_load, there's a call to find_existing_devices * which triggers added_cb events so, there's been events ... maybe. */ bus_ev_hdle_events((bus_ev_hdle_t *)bus_events); } return (bus_ev_hdle_t *)bus_events; err: bus_ev_dispose((bus_ev_hdle_t *)bus_events); return NULL; }
int hu_usb_run () { // Android specific C level libusbhost API requires thread for it's processing, and calling of the added/removed/disc-done callbacks struct usb_host_context * context = usb_host_init (); usb_host_run (context, usb_device_added, usb_device_removed, usb_discovery_done, (void * ) 0); usb_host_cleanup (context); return (0); }
void APP_init(void) { usb_status status = USB_OK; uint32_t opt = 0; status = usb_host_init(CONTROLLER_ID, usb_host_board_init, &g_host_handle); if (status != USB_OK) { USB_PRINTF("\r\nUSB Host Initialization failed! STATUS: 0x%x", status); return; } /* ** since we are going to act as the host driver, register the driver ** information for wanted class/subclass/protocols */ status = usb_host_register_driver_info(g_host_handle, (void *) DriverInfoTable); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } status = usb_host_register_unsupported_device_notify(g_host_handle, usb_host_hid_unsupported_device_event); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } mouse_usb_event = OS_Event_create(0);/* manually clear */ if (mouse_usb_event == NULL) { USB_PRINTF("mouse_usb_event create failed\r\n"); return; } mouse_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t)); if (mouse_hid_com == NULL) { USB_PRINTF("mouse_hid_com create failed\r\n"); return; } kbd_usb_event = OS_Event_create(0);/* manually clear */ if (kbd_usb_event == NULL) { USB_PRINTF("kbd_usb_event create failed\r\n"); return; } OS_Event_set(kbd_usb_event, USB_EVEN_INIT); kbd_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t)); if (kbd_hid_com == NULL) { USB_PRINTF("kbd_hid_com create failed\r\n"); return; } if ((uint32_t) OS_TASK_ERROR == OS_Task_create(USB_KEYBOARD_TASK_ADDRESS, (void*) g_host_handle, (uint32_t) USB_KEYBOARD_TASK_PRIORITY, USB_KEYBOARD_TASK_STACKSIZE, USB_KEYBOARD_TASK_NAME, &opt)) { USB_PRINTF("keyboard task create failed\r\n"); return; } if ((uint32_t) OS_TASK_ERROR == OS_Task_create(USB_MOUSE_TASK_ADDRESS, (void*) g_host_handle, (uint32_t) USB_MOUSE_TASK_PRIORITY, USB_MOUSE_TASK_STACKSIZE, USB_MOUSE_TASK_NAME, &opt)) { USB_PRINTF("mouse task create failed\r\n"); return; } time_init(); USB_PRINTF("\fUSB HID Mouse+Keyboard\r\nWaiting for USB Mouse or Keyboard to be attached...\r\n"); }