You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
传说中的 URB<br />
有人问,怎么写个驱动写这么久啊?有完没完啊?此水何时休?此恨何时已?<br />
的确,一路走来,大家都不容易,但既然已经走到今天,我们能做的也只有是坚持下去.十年之前,我不认识<br />
你,你不属于我,但十年之后我依然记得那一年(1997年),我的一个中学校友的那一句:不管前面是地雷阵还<br />
是万丈深渊,我(们)都将一往无前,义无反顾,鞠躬尽瘁,死而后已.这个人叫朱镕基,毕业于长沙市一中.<br />
usb_stor_acquire_resources,从名字上来看,获取资源.什么资源?网名为爬上墙头等红杏的朋友不禁<br />
表示出了一些好奇,之前不是申请了一大堆内存了吗?写个usb 设备驱动程序怎么那么麻烦啊?不是专门为<br />
usb mass storage 设备准备了一个struct us_data 这么一个结构体了吗?不是说故事已经到高潮了吗?<br />
周润发说得好,我还刚上路呢.没错,如果你以为看到这里你已经对usb 设备驱动程序有了足够的认识,认<br />
为接下来的代码已经没有必要再分析了,那么,我只想说,上帝创造世界的计划中,未必包括使你会写usb 设<br />
备驱动程序.<br />
的确,别看usb_stor_acquire_resources 的代码不多,每一行都有每一行的故事.本节我们只讲其中的<br />
一行代码,没错,就是一行代码,因为我们需要隆重推出一个名词,一个响当当的名字,她就是传说中的urb,全<br />
称usb request block.usb 设备需要通信,要传递数据,就需要使用urb,确切的说,应该是usb 设备驱动程<br />
序使用urb.实际上,作为usb 设备驱动,它本身并不能直接操纵数据的传输,在usb 这个大观园里,外接设备<br />
永远都是配角,真正的核心只是usb core,而真正负责调度的是usb host controller,这个您通常看不见的<br />
usb 主机控制器芯片,他俨然是usb 大观园中的大管家.设备驱动要发送信息,所需要做的是建立一个urb<br />
数<br />
据结构,并把这个数据结构交给核心层,而核心层会为所有设备统一完成调度,而设备在提交了urb 之<br />
_________后需要<br />
做的,只是等待,等待,在那漫漫长夜中等待.别急,我们慢慢来.<br />
760 行,一条赋值语句,等号左边,us->current_urb,等号右边,usb_alloc_urb 函数被调用.如果说<br />
struct us_data 是usb mass storage 中的主角,那么struct urb 将毫无争议的成为整个usb 子系统中<br />
的主角. Linux 中所有的usb 设备驱动,都必然也必须要使用urb.那么urb 究竟长成什么样呢?她是如芙<br />
蓉<br />
姐姐那么婀娜多姿呢,又亦或是如林志玲那般无公害性感呢?在include/linux/usb.h 中能找到她的靓照:<br />
64<br />
614 /**<br />
615 * struct urb - USB Request Block<br />
616 * @urb_list: For use by current owner <strong>of</strong> the URB.<br />
617 * @pipe: Holds endpoint number, direction, type, and more.<br />
618 * Create these values with the eight macros available;<br />
619 * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl"<br />
620 * (control), "bulk", "int" (interrupt), or "iso" (isochronous).<br />
621 * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint<br />
622 * numbers range from zero to fifteen. Note that "in" endpoint two<br />
623 * is a different endpoint (and pipe) from "out" endpoint two.<br />
624 * The current configuration controls the existence, type, and<br />
625 * maximum packet size <strong>of</strong> any given endpoint.<br />
626 * @dev: Identifies the USB device to perform the request.<br />
627 * @status: This is read in non-iso completion functions to get the<br />
628 * status <strong>of</strong> the particular request. ISO requests only use it<br />
629 * to tell whether the URB was unlinked; detailed status for