usb_alloc_dev

/**
 * usb_alloc_dev - usb device constructor (usbcore-internal)
 * @parent: hub to which device is connected; null to allocate a root hub
 * @bus: bus used to access the device
 * @port1: one-based index of port; ignored for root hubs
 * Context: !in_interrupt()
 *
 * Only hub drivers (including virtual root hub drivers for host
 * controllers) should ever call this.
 *
 * This call may not be used in a non-sleeping context.
 */
struct usb_device *usb_alloc_dev(struct usb_device *parent,
				 struct usb_bus *bus, unsigned port1)
{
	struct usb_device *dev;
	struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
	unsigned root_hub = 0;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;

	if (!usb_get_hcd(bus_to_hcd(bus))) {
		kfree(dev);
		return NULL;
	}
	/* Root hubs aren't true devices, so don't allocate HCD resources */
	if (usb_hcd->driver->alloc_dev && parent &&
		!usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
		usb_put_hcd(bus_to_hcd(bus));
		kfree(dev);
		return NULL;
	}

	device_initialize(&dev->dev);
	dev->dev.bus = &usb_bus_type;
	dev->dev.type = &usb_device_type;
	dev->dev.groups = usb_device_groups;
	dev->dev.dma_mask = bus->controller->dma_mask;
	set_dev_node(&dev->dev, dev_to_node(bus->controller));
	dev->state = USB_STATE_ATTACHED;/*设置usb设备状态已连接*/
	atomic_set(&dev->urbnum, 0);

    /*初始化usb_device结构体里面的端点0的urb_list*/
	INIT_LIST_HEAD(&dev->ep0.urb_list);
	dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
	dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
	/* ep0 maxpacket comes later, from device descriptor */
	usb_enable_endpoint(dev, &dev->ep0, false);/*打开端点0*/
	dev->can_submit = 1;/*可以提交*/

	/* Save readable and stable topology id, distinguishing devices
	 * by location for diagnostics, tools, driver model, etc.  The
	 * string is a path along hub ports, from the root.  Each device's
	 * dev->devpath will be stable until USB is re-cabled, and hubs
	 * are often labeled with these port numbers.  The name isn't
	 * as stable:  bus->busnum changes easily from modprobe order,
	 * cardbus or pci hotplugging, and so on.
	 */
     /*判断你的usb设备是不是连接到Root Hub上面*/
	if (unlikely(!parent)) {
		dev->devpath[0] = '0';
		dev->route = 0;

		dev->dev.parent = bus->controller;
		dev_set_name(&dev->dev, "usb%d", bus->busnum);
		root_hub = 1;
	} else {
		/* match any labeling on the hubs; it's one-based */
		if (parent->devpath[0] == '0') {
			snprintf(dev->devpath, sizeof dev->devpath,
				"%d", port1);
			/* Root ports are not counted in route string */
			dev->route = 0;
		} else {
			snprintf(dev->devpath, sizeof dev->devpath,
				"%s.%d", parent->devpath, port1);
			/* Route string assumes hubs have less than 16 ports */
			if (port1 < 15)
				dev->route = parent->route +
					(port1 << ((parent->level - 1)*4));
			else
				dev->route = parent->route +
					(15 << ((parent->level - 1)*4));
		}

		dev->dev.parent = &parent->dev;
		dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);

		/* hub driver sets up TT records */
	}

	dev->portnum = port1;
	dev->bus = bus;
	dev->parent = parent;
	INIT_LIST_HEAD(&dev->filelist);/*初始化一个队列*/

#ifdef	CONFIG_PM
	pm_runtime_set_autosuspend_delay(&dev->dev,
			usb_autosuspend_delay * 1000);
	dev->connect_time = jiffies;
	dev->active_duration = -jiffies;
#endif
	if (root_hub)	/* Root hub always ok [and always wired] */
		dev->authorized = 1;
	else {
		dev->authorized = usb_hcd->authorized_default;
		dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
	}
	return dev;
}

当有usb设备连接到主机都会调用这个函数;为usb设备分配内存;并初始化相应的值;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[ 12%] Building CXX object usb_cam/CMakeFiles/v4l_driver.dir/src/camera_driver.cpp.o /root/catkin_uc/src/usb_cam/src/camera_driver.cpp: In destructor ‘virtual usb_cam::AbstractV4LUSBCam::~AbstractV4LUSBCam()’: /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:239:5: error: ‘av_packet_free’ was not declared in this scope av_packet_free(&avpkt); ^~~~~~~~~~~~~~ /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:239:5: note: suggested alternative: ‘av_packet_ref’ av_packet_free(&avpkt); ^~~~~~~~~~~~~~ av_packet_ref /root/catkin_uc/src/usb_cam/src/camera_driver.cpp: In static member function ‘static bool usb_cam::AbstractV4LUSBCam::init_decoder()’: /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:379:13: error: ‘av_packet_alloc’ was not declared in this scope avpkt = av_packet_alloc(); ^~~~~~~~~~~~~~~ /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:379:13: note: suggested alternative: ‘av_image_alloc’ avpkt = av_packet_alloc(); ^~~~~~~~~~~~~~~ av_image_alloc /root/catkin_uc/src/usb_cam/src/camera_driver.cpp: In static member function ‘static bool usb_cam::AbstractV4LUSBCam::decode_ffmpeg(const void*, int, usb_cam::camera_image_t*)’: /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:779:8: error: ‘avcodec_send_packet’ was not declared in this scope if(avcodec_send_packet(avcodec_context, avpkt) < 0) ^~~~~~~~~~~~~~~~~~~ /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:779:8: note: suggested alternative: ‘av_append_packet’ if(avcodec_send_packet(avcodec_context, avpkt) < 0) ^~~~~~~~~~~~~~~~~~~ av_append_packet /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:792:9: error: ‘avcodec_receive_frame’ was not declared in this scope if (avcodec_receive_frame(avcodec_context, avframe_camera) < 0) ^~~~~~~~~~~~~~~~~~~~~ /root/catkin_uc/src/usb_cam/src/camera_driver.cpp:792:9: note: suggested alternative: ‘avcodec_free_frame’ if (avcodec_receive_frame(avcodec_context, avframe_camera) < 0) ^~~~~~~~~~~~~~~~~~~~~ avcodec_free_frame usb_cam/CMakeFiles/v4l_driver.dir/build.make:110: recipe for target 'usb_cam/CMakeFiles/v4l_driver.dir/src/camera_driver.cpp.o' failed make[2]: *** [usb_cam/CMakeFiles/v4l_driver.dir/src/camera_driver.cpp.o] Error 1 CMakeFiles/Makefile2:656: recipe for target 'usb_cam/CMakeFiles/v4l_driver.dir/all' failed make[1]: *** [usb_cam/CMakeFiles/v4l_driver.dir/all] Error 2 Makefile:140: recipe for target 'all' failed make: *** [all] Error 2 Invoking "make -j4 -l4" failed
最新发布
07-16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值