xhci.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  1. #include "xhci.h"
  2. #include "internal.h"
  3. #include <common/hid.h>
  4. #include <common/kprint.h>
  5. #include <common/spinlock.h>
  6. #include <common/time.h>
  7. #include <debug/bug.h>
  8. #include <debug/traceback/traceback.h>
  9. #include <driver/interrupt/apic/apic.h>
  10. #include <exception/irq.h>
  11. #include <mm/mm.h>
  12. #include <mm/slab.h>
  13. // 由于xhci寄存器读取需要对齐,因此禁用GCC优化选项
  14. #pragma GCC optimize("O0")
  15. spinlock_t xhci_controller_init_lock = {0}; // xhci控制器初始化锁(在usb_init中被初始化)
  16. static int xhci_ctrl_count = 0; // xhci控制器计数
  17. static struct xhci_host_controller_t xhci_hc[XHCI_MAX_HOST_CONTROLLERS] = {0};
  18. void xhci_hc_irq_enable(uint64_t irq_num);
  19. void xhci_hc_irq_disable(uint64_t irq_num);
  20. uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg);
  21. void xhci_hc_irq_uninstall(uint64_t irq_num);
  22. static int xhci_hc_find_available_id();
  23. static int xhci_hc_stop(int id);
  24. static int xhci_hc_reset(int id);
  25. static int xhci_hc_stop_legacy(int id);
  26. static int xhci_hc_start_sched(int id);
  27. static int xhci_hc_stop_sched(int id);
  28. static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int version, uint32_t *offset,
  29. uint32_t *count, uint16_t *protocol_flag);
  30. static int xhci_hc_pair_ports(int id);
  31. static uint64_t xhci_create_ring(int trbs);
  32. static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr);
  33. void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs);
  34. static int xhci_hc_init_intr(int id);
  35. static int xhci_hc_start_ports(int id);
  36. static int xhci_send_command(int id, struct xhci_TRB_t *trb, const bool do_ring);
  37. static uint64_t xhci_initialize_slot(const int id, const int port, const int speed, const int max_packet);
  38. static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int port_id, const int ep_num,
  39. const int max_packet, const int max_burst, const int type, const int direction,
  40. const int speed, const int ep_interval);
  41. static int xhci_set_address(const int id, const uint64_t slot_vaddr, const int slot_id, const bool block);
  42. static int xhci_control_in(const int id, struct usb_request_packet_t *packet, void *target, const int port_id,
  43. const int max_packet);
  44. static int xhci_control_out(const int id, struct usb_request_packet_t *packet, void *target, const int slot_id,
  45. const int max_packet);
  46. static int xhci_setup_stage(struct xhci_ep_info_t *ep, const struct usb_request_packet_t *packet,
  47. const uint8_t direction);
  48. static int xhci_data_stage(struct xhci_ep_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size,
  49. uint8_t direction, const int max_packet, const uint64_t status_vaddr);
  50. static int xhci_status_stage(struct xhci_ep_info_t *ep, uint8_t direction, uint64_t status_buf_vaddr);
  51. static int xhci_wait_for_interrupt(const int id, uint64_t status_vaddr);
  52. static inline int xhci_get_desc(const int id, const int port_id, void *target, const uint16_t desc_type,
  53. const uint8_t desc_index, const uint16_t lang_id, const uint16_t length);
  54. static int xhci_get_config_desc(const int id, const int port_id, struct usb_config_desc *conf_desc);
  55. static inline int xhci_get_config_desc_full(const int id, const int port_id, const struct usb_config_desc *conf_desc,
  56. void *target);
  57. static int xhci_get_interface_desc(const void *in_buf, const uint8_t if_num, struct usb_interface_desc **if_desc);
  58. static inline int xhci_get_endpoint_desc(const struct usb_interface_desc *if_desc, const uint8_t ep_num,
  59. struct usb_endpoint_desc **ep_desc);
  60. static int xhci_get_descriptor(const int id, const int port_id, struct usb_device_desc *dev_desc);
  61. static int xhci_configure_port(const int id, const int port_id);
  62. static int xhci_configure_endpoint(const int id, const int port_id, const uint8_t ep_num, const uint8_t ep_type,
  63. struct usb_endpoint_desc *ep_desc);
  64. static int xhci_get_hid_report(int id, int port_id, int interface_number, void *ret_hid_report,
  65. uint32_t hid_report_len);
  66. static int xhci_get_hid_descriptor(int id, int port_id, const void *full_conf, int interface_number,
  67. struct usb_hid_desc **ret_hid_desc);
  68. hardware_intr_controller xhci_hc_intr_controller = {
  69. .enable = xhci_hc_irq_enable,
  70. .disable = xhci_hc_irq_disable,
  71. .install = xhci_hc_irq_install,
  72. .uninstall = xhci_hc_irq_uninstall,
  73. .ack = apic_local_apic_edge_ack,
  74. };
  75. /**
  76. * @brief 在controller数组之中寻找可用插槽
  77. *
  78. * 注意:该函数只能被获得init锁的进程所调用
  79. * @return int 可用id(无空位时返回-1)
  80. */
  81. static int xhci_hc_find_available_id()
  82. {
  83. if (unlikely(xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS))
  84. return -1;
  85. for (int i = 0; i < XHCI_MAX_HOST_CONTROLLERS; ++i)
  86. {
  87. if (xhci_hc[i].pci_dev_hdr == NULL)
  88. return i;
  89. }
  90. return -1;
  91. }
  92. /**
  93. * @brief 从指定地址读取trb
  94. *
  95. * @param trb 要存储到的trb的地址
  96. * @param address 待读取trb的地址
  97. */
  98. static __always_inline void xhci_get_trb(struct xhci_TRB_t *trb, const uint64_t address)
  99. {
  100. trb->param = __read8b(address);
  101. trb->status = __read4b(address + 8);
  102. trb->command = __read4b(address + 12);
  103. }
  104. /**
  105. * @brief 将给定的trb写入指定的地址
  106. *
  107. * @param trb 源trb
  108. * @param address 拷贝的目标地址
  109. */
  110. static __always_inline void xhci_set_trb(struct xhci_TRB_t *trb, const uint64_t address)
  111. {
  112. __write8b(address, trb->param);
  113. __write4b(address + 8, trb->status);
  114. __write4b(address + 12, trb->command);
  115. }
  116. /**
  117. * @brief 将ep结构体写入到设备上下文中的对应块内
  118. *
  119. * @param id 主机控制器id
  120. * @param slot_vaddr 设备上下文虚拟地址
  121. * @param ep_num ep结构体要写入到哪个块中(在设备上下文中的块号)
  122. * @param ep 源数据
  123. */
  124. static __always_inline void __write_ep(int id, uint64_t slot_vaddr, int ep_num, struct xhci_ep_context_t *ep)
  125. {
  126. memcpy((void *)(slot_vaddr + ep_num * xhci_hc[id].context_size), ep, sizeof(struct xhci_ep_context_t));
  127. }
  128. /**
  129. * @brief 从设备上下文中的对应块内读取数据到ep结构体
  130. *
  131. * @param id 主机控制器id
  132. * @param slot_vaddr 设备上下文虚拟地址
  133. * @param ep_num 要从哪个块中读取(在设备上下文中的块号)
  134. * @param ep 目标地址
  135. */
  136. static __always_inline void __read_from_ep(int id, uint64_t slot_vaddr, int ep_num, struct xhci_ep_context_t *ep)
  137. {
  138. memcpy(ep, (void *)(slot_vaddr + ep_num * xhci_hc[id].context_size), sizeof(struct xhci_ep_context_t));
  139. }
  140. /**
  141. * @brief 将slot上下文数组结构体写入插槽的上下文空间
  142. *
  143. * @param vaddr 目标地址
  144. * @param slot_ctx slot上下文数组
  145. */
  146. static __always_inline void __write_slot(const uint64_t vaddr, struct xhci_slot_context_t *slot_ctx)
  147. {
  148. memcpy((void *)vaddr, slot_ctx, sizeof(struct xhci_slot_context_t));
  149. }
  150. /**
  151. * @brief 从指定地址读取slot context
  152. *
  153. * @param slot_ctx 目标地址
  154. * @param slot_vaddr 源地址
  155. * @return __always_inline
  156. */
  157. static __always_inline void __read_from_slot(struct xhci_slot_context_t *slot_ctx, uint64_t slot_vaddr)
  158. {
  159. memcpy(slot_ctx, (void *)slot_vaddr, sizeof(struct xhci_slot_context_t));
  160. }
  161. /**
  162. * @brief 写入doorbell寄存器
  163. *
  164. * @param id 主机控制器id
  165. * @param slot_id usb控制器插槽id(0用作命令门铃,其他的用于具体的设备的门铃)
  166. * @param value endpoint
  167. */
  168. static __always_inline void __xhci_write_doorbell(const int id, const uint16_t slot_id, const uint32_t value)
  169. {
  170. // 确保写入门铃寄存器之前,所有的写操作均已完成
  171. io_mfence();
  172. xhci_write_cap_reg32(id, xhci_hc[id].db_offset + slot_id * sizeof(uint32_t), value);
  173. io_mfence();
  174. }
  175. /**
  176. * @brief 将trb写入指定的ring中,并更新下一个要写入的地址的值
  177. *
  178. * @param ep_info 端点信息结构体
  179. * @param trb 待写入的trb
  180. */
  181. static __always_inline void __xhci_write_trb(struct xhci_ep_info_t *ep_info, struct xhci_TRB_t *trb)
  182. {
  183. memcpy((void *)ep_info->current_ep_ring_vaddr, trb, sizeof(struct xhci_TRB_t));
  184. ep_info->current_ep_ring_vaddr += sizeof(struct xhci_TRB_t);
  185. struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)(ep_info->current_ep_ring_vaddr);
  186. // ring到头了,转换cycle,然后回到第一个trb
  187. if (unlikely(ptr->TRB_type == TRB_TYPE_LINK))
  188. {
  189. ptr->cycle = ep_info->current_ep_ring_cycle;
  190. ep_info->current_ep_ring_vaddr = ep_info->ep_ring_vbase;
  191. ep_info->current_ep_ring_cycle ^= 1;
  192. }
  193. }
  194. /**
  195. * @brief 获取设备上下文缓冲区的虚拟地址
  196. *
  197. * @param id 主机控制器id
  198. * @param port_id 端口id
  199. * @return 设备上下文缓冲区的虚拟地址
  200. */
  201. static __always_inline uint64_t xhci_get_device_context_vaddr(const int id, const int port_id)
  202. {
  203. return (uint64_t)phys_2_virt(
  204. __read8b(xhci_hc[id].dcbaap_vaddr + (xhci_hc[id].ports[port_id].slot_id * sizeof(uint64_t))));
  205. }
  206. /**
  207. * @brief 停止xhci主机控制器
  208. *
  209. * @param id 主机控制器id
  210. * @return int
  211. */
  212. static int xhci_hc_stop(int id)
  213. {
  214. // 判断是否已经停止
  215. if (unlikely((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 1))
  216. return 0;
  217. io_mfence();
  218. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00000000);
  219. io_mfence();
  220. char timeout = 17;
  221. while ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  222. {
  223. io_mfence();
  224. usleep(1000);
  225. if (--timeout == 0)
  226. return -ETIMEDOUT;
  227. }
  228. return 0;
  229. }
  230. /**
  231. * @brief reset xHCI主机控制器
  232. *
  233. * @param id 主机控制器id
  234. * @return int
  235. */
  236. static int xhci_hc_reset(int id)
  237. {
  238. int retval = 0;
  239. io_mfence();
  240. // 判断HCHalted是否置位
  241. if ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  242. {
  243. io_mfence();
  244. kdebug("stopping usb hc...");
  245. // 未置位,需要先尝试停止usb主机控制器
  246. retval = xhci_hc_stop(id);
  247. if (unlikely(retval))
  248. return retval;
  249. }
  250. int timeout = 500; // wait 500ms
  251. // reset
  252. uint32_t cmd = xhci_read_op_reg32(id, XHCI_OPS_USBCMD);
  253. io_mfence();
  254. cmd |= (1 << 1);
  255. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, cmd);
  256. io_mfence();
  257. io_mfence();
  258. while (xhci_read_op_reg32(id, XHCI_OPS_USBCMD) & (1 << 1))
  259. {
  260. io_mfence();
  261. usleep(1000);
  262. if (--timeout == 0)
  263. return -ETIMEDOUT;
  264. }
  265. return retval;
  266. }
  267. /**
  268. * @brief 停止指定xhci控制器的legacy support
  269. *
  270. * @param id 控制器id
  271. * @return int
  272. */
  273. static int xhci_hc_stop_legacy(int id)
  274. {
  275. uint64_t current_offset = xhci_hc[id].ext_caps_off;
  276. do
  277. {
  278. // 判断当前entry是否为legacy support entry
  279. if ((xhci_read_cap_reg32(id, current_offset) & 0xff) == XHCI_XECP_ID_LEGACY)
  280. {
  281. io_mfence();
  282. // 接管控制权
  283. xhci_write_cap_reg32(id, current_offset,
  284. xhci_read_cap_reg32(id, current_offset) | XHCI_XECP_LEGACY_OS_OWNED);
  285. io_mfence();
  286. // 等待响应完成
  287. int timeout = XHCI_XECP_LEGACY_TIMEOUT;
  288. while ((xhci_read_cap_reg32(id, current_offset) & XHCI_XECP_LEGACY_OWNING_MASK) !=
  289. XHCI_XECP_LEGACY_OS_OWNED)
  290. {
  291. io_mfence();
  292. usleep(1000);
  293. if (--timeout == 0)
  294. {
  295. kerror("The BIOS doesn't stop legacy support.");
  296. return -ETIMEDOUT;
  297. }
  298. }
  299. // 处理完成
  300. return 0;
  301. }
  302. io_mfence();
  303. // 读取下一个entry的偏移增加量
  304. int next_off = ((xhci_read_cap_reg32(id, current_offset) & 0xff00) >> 8) << 2;
  305. io_mfence();
  306. // 将指针跳转到下一个entry
  307. current_offset = next_off ? (current_offset + next_off) : 0;
  308. } while (current_offset);
  309. // 当前controller不存在legacy支持,也问题不大,不影响
  310. return 0;
  311. }
  312. /**
  313. * @brief 启用指定xhci控制器的调度
  314. *
  315. * @param id 控制器id
  316. * @return int
  317. */
  318. static int xhci_hc_start_sched(int id)
  319. {
  320. io_mfence();
  321. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, (1 << 0) | (1 << 2) | (1 << 3));
  322. io_mfence();
  323. usleep(100 * 1000);
  324. }
  325. /**
  326. * @brief 停止指定xhci控制器的调度
  327. *
  328. * @param id 控制器id
  329. * @return int
  330. */
  331. static int xhci_hc_stop_sched(int id)
  332. {
  333. io_mfence();
  334. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00);
  335. io_mfence();
  336. }
  337. /**
  338. * @brief 在Ex capability list中寻找符合指定的协议号的寄存器offset、count、flag信息
  339. *
  340. * @param id 主机控制器id
  341. * @param list_off 列表项位置距离控制器虚拟基地址的偏移量
  342. * @param version 要寻找的端口版本号(2或3)
  343. * @param offset 返回的 Compatible Port Offset
  344. * @param count 返回的 Compatible Port Count
  345. * @param protocol_flag 返回的与协议相关的flag
  346. * @return uint32_t 下一个列表项的偏移量
  347. */
  348. static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int version, uint32_t *offset,
  349. uint32_t *count, uint16_t *protocol_flag)
  350. {
  351. if (count)
  352. *count = 0;
  353. do
  354. {
  355. uint32_t dw0 = xhci_read_cap_reg32(id, list_off);
  356. io_mfence();
  357. uint32_t next_list_off = (dw0 >> 8) & 0xff;
  358. next_list_off = next_list_off ? (list_off + (next_list_off << 2)) : 0;
  359. if ((dw0 & 0xff) == XHCI_XECP_ID_PROTOCOL && ((dw0 & 0xff000000) >> 24) == version)
  360. {
  361. uint32_t dw2 = xhci_read_cap_reg32(id, list_off + 8);
  362. io_mfence();
  363. if (offset != NULL)
  364. *offset = (uint32_t)(dw2 & 0xff) - 1; // 使其转换为zero based
  365. if (count != NULL)
  366. *count = (uint32_t)((dw2 & 0xff00) >> 8);
  367. if (protocol_flag != NULL && version == 2)
  368. *protocol_flag = (uint16_t)((dw2 >> 16) & 0x0fff);
  369. return next_list_off;
  370. }
  371. list_off = next_list_off;
  372. } while (list_off);
  373. return 0;
  374. }
  375. /**
  376. * @brief 配对xhci主机控制器的usb2、usb3端口
  377. *
  378. * @param id 主机控制器id
  379. * @return int 返回码
  380. */
  381. static int xhci_hc_pair_ports(int id)
  382. {
  383. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  384. io_mfence();
  385. memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  386. io_mfence();
  387. // 从hcs1获取端口数量
  388. xhci_hc[id].port_num = hcs1.max_ports;
  389. // 找到所有的端口并标记其端口信息
  390. xhci_hc[id].port_num_u2 = 0;
  391. xhci_hc[id].port_num_u3 = 0;
  392. uint32_t next_off = xhci_hc[id].ext_caps_off;
  393. uint32_t offset, cnt;
  394. uint16_t protocol_flags = 0;
  395. // 寻找所有的usb2端口
  396. while (next_off)
  397. {
  398. io_mfence();
  399. next_off = xhci_hc_get_protocol_offset(id, next_off, 2, &offset, &cnt, &protocol_flags);
  400. io_mfence();
  401. if (cnt)
  402. {
  403. for (int i = 0; i < cnt; ++i)
  404. {
  405. io_mfence();
  406. xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u2++;
  407. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB2;
  408. io_mfence();
  409. // usb2 high speed only
  410. if (protocol_flags & 2)
  411. xhci_hc[id].ports[offset + i].flags |= XHCI_PROTOCOL_HSO;
  412. }
  413. }
  414. }
  415. // 寻找所有的usb3端口
  416. next_off = xhci_hc[id].ext_caps_off;
  417. while (next_off)
  418. {
  419. io_mfence();
  420. next_off = xhci_hc_get_protocol_offset(id, next_off, 3, &offset, &cnt, &protocol_flags);
  421. io_mfence();
  422. if (cnt)
  423. {
  424. for (int i = 0; i < cnt; ++i)
  425. {
  426. io_mfence();
  427. xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u3++;
  428. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB3;
  429. }
  430. }
  431. }
  432. // 将对应的USB2端口和USB3端口进行配对
  433. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  434. {
  435. for (int j = 0; j < xhci_hc[id].port_num; ++j)
  436. {
  437. if (unlikely(i == j))
  438. continue;
  439. io_mfence();
  440. if ((xhci_hc[id].ports[i].offset == xhci_hc[id].ports[j].offset) &&
  441. ((xhci_hc[id].ports[i].flags & XHCI_PROTOCOL_INFO) !=
  442. (xhci_hc[id].ports[j].flags & XHCI_PROTOCOL_INFO)))
  443. {
  444. xhci_hc[id].ports[i].paired_port_num = j;
  445. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_HAS_PAIR;
  446. io_mfence();
  447. xhci_hc[id].ports[j].paired_port_num = i;
  448. xhci_hc[id].ports[j].flags |= XHCI_PROTOCOL_HAS_PAIR;
  449. }
  450. }
  451. }
  452. // 标记所有的usb3、单独的usb2端口为激活状态
  453. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  454. {
  455. io_mfence();
  456. if (XHCI_PORT_IS_USB3(id, i) || (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))))
  457. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_ACTIVE;
  458. }
  459. kinfo("Found %d ports on root hub, usb2 ports:%d, usb3 ports:%d", xhci_hc[id].port_num, xhci_hc[id].port_num_u2,
  460. xhci_hc[id].port_num_u3);
  461. /*
  462. // 打印配对结果
  463. for (int i = 1; i <= xhci_hc[id].port_num; ++i)
  464. {
  465. if (XHCI_PORT_IS_USB3(id, i))
  466. {
  467. kdebug("USB3 port %d, offset=%d, pair with usb2 port %d, current port is %s", i,
  468. xhci_hc[id].ports[i].offset, xhci_hc[id].ports[i].paired_port_num, XHCI_PORT_IS_ACTIVE(id, i) ? "active" :
  469. "inactive");
  470. }
  471. else if (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))) // 单独的2.0接口
  472. {
  473. kdebug("Stand alone USB2 port %d, offset=%d, current port is %s", i, xhci_hc[id].ports[i].offset,
  474. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive");
  475. }
  476. else if (XHCI_PORT_IS_USB2(id, i))
  477. {
  478. kdebug("USB2 port %d, offset=%d, current port is %s, has pair=%s", i, xhci_hc[id].ports[i].offset,
  479. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive", XHCI_PORT_HAS_PAIR(id, i) ? "true" : "false");
  480. }
  481. }
  482. */
  483. return 0;
  484. }
  485. /**
  486. * @brief 创建ring,并将最后一个trb指向头一个trb
  487. *
  488. * @param trbs 要创建的trb数量
  489. * @return uint64_t trb数组的起始虚拟地址
  490. */
  491. static uint64_t xhci_create_ring(int trbs)
  492. {
  493. int total_size = trbs * sizeof(struct xhci_TRB_t);
  494. const uint64_t vaddr = (uint64_t)kmalloc(total_size, 0);
  495. io_mfence();
  496. memset((void *)vaddr, 0, total_size);
  497. io_mfence();
  498. // 设置最后一个trb为link trb
  499. xhci_TRB_set_link_cmd(vaddr + total_size - sizeof(struct xhci_TRB_t));
  500. io_mfence();
  501. return vaddr;
  502. }
  503. /**
  504. * @brief 创建新的event ring table和对应的ring segment
  505. *
  506. * @param trbs 包含的trb的数量
  507. * @param ret_ring_addr 返回的第一个event ring segment的基地址(虚拟)
  508. * @return uint64_t trb table的虚拟地址
  509. */
  510. static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr)
  511. {
  512. const uint64_t table_vaddr = (const uint64_t)kmalloc(64, 0); // table支持8个segment
  513. io_mfence();
  514. if (unlikely(table_vaddr == NULL))
  515. return -ENOMEM;
  516. memset((void *)table_vaddr, 0, 64);
  517. // 暂时只创建1个segment
  518. const uint64_t seg_vaddr = (const uint64_t)kmalloc(trbs * sizeof(struct xhci_TRB_t), 0);
  519. io_mfence();
  520. if (unlikely(seg_vaddr == NULL))
  521. return -ENOMEM;
  522. memset((void *)seg_vaddr, 0, trbs * sizeof(struct xhci_TRB_t));
  523. io_mfence();
  524. // 将segment地址和大小写入table
  525. *(uint64_t *)(table_vaddr) = virt_2_phys(seg_vaddr);
  526. *(uint64_t *)(table_vaddr + 8) = trbs;
  527. *ret_ring_addr = seg_vaddr;
  528. return table_vaddr;
  529. }
  530. void xhci_hc_irq_enable(uint64_t irq_num)
  531. {
  532. int cid = xhci_find_hcid_by_irq_num(irq_num);
  533. io_mfence();
  534. if (WARN_ON(cid == -1))
  535. return;
  536. io_mfence();
  537. pci_start_msi(xhci_hc[cid].pci_dev_hdr);
  538. io_mfence();
  539. xhci_hc_start_sched(cid);
  540. io_mfence();
  541. xhci_hc_start_ports(cid);
  542. }
  543. void xhci_hc_irq_disable(uint64_t irq_num)
  544. {
  545. int cid = xhci_find_hcid_by_irq_num(irq_num);
  546. io_mfence();
  547. if (WARN_ON(cid == -1))
  548. return;
  549. xhci_hc_stop_sched(cid);
  550. io_mfence();
  551. pci_disable_msi(xhci_hc[cid].pci_dev_hdr);
  552. io_mfence();
  553. }
  554. /**
  555. * @brief xhci中断的安装函数
  556. *
  557. * @param irq_num 要安装的中断向量号
  558. * @param arg 参数
  559. * @return uint64_t 错误码
  560. */
  561. uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg)
  562. {
  563. int cid = xhci_find_hcid_by_irq_num(irq_num);
  564. io_mfence();
  565. if (WARN_ON(cid == -1))
  566. return -EINVAL;
  567. struct xhci_hc_irq_install_info_t *info = (struct xhci_hc_irq_install_info_t *)arg;
  568. struct msi_desc_t msi_desc;
  569. memset(&msi_desc, 0, sizeof(struct msi_desc_t));
  570. io_mfence();
  571. msi_desc.irq_num = irq_num;
  572. msi_desc.msi_index = 0;
  573. msi_desc.pci_dev = (struct pci_device_structure_header_t *)xhci_hc[cid].pci_dev_hdr;
  574. msi_desc.assert = info->assert;
  575. msi_desc.edge_trigger = info->edge_trigger;
  576. msi_desc.processor = info->processor;
  577. msi_desc.pci.msi_attribute.is_64 = 1;
  578. msi_desc.pci.msi_attribute.is_msix = 1;
  579. io_mfence();
  580. int retval = pci_enable_msi(&msi_desc);
  581. return 0;
  582. }
  583. void xhci_hc_irq_uninstall(uint64_t irq_num)
  584. {
  585. // todo
  586. int cid = xhci_find_hcid_by_irq_num(irq_num);
  587. io_mfence();
  588. if (WARN_ON(cid == -1))
  589. return;
  590. xhci_hc_stop(cid);
  591. io_mfence();
  592. }
  593. /**
  594. * @brief xhci主机控制器的中断处理函数
  595. *
  596. * @param irq_num 中断向量号
  597. * @param cid 控制器号
  598. * @param regs 寄存器值
  599. */
  600. void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs)
  601. {
  602. // kdebug("USB irq received.");
  603. /*
  604. 写入usb status寄存器,以表明当前收到了中断,清除usb status寄存器中的EINT位
  605. 需要先清除这个位,再清除interrupter中的pending bit)
  606. */
  607. xhci_write_op_reg32(cid, XHCI_OPS_USBSTS, xhci_read_op_reg32(cid, XHCI_OPS_USBSTS));
  608. // 读取第0个usb interrupter的intr management寄存器
  609. const uint32_t iman0 = xhci_read_intr_reg32(cid, 0, XHCI_IR_MAN);
  610. uint64_t dequeue_reg = xhci_read_intr_reg64(cid, 0, XHCI_IR_DEQUEUE);
  611. if (((iman0 & 3) == 3) || (dequeue_reg & 8)) // 中断被启用,且pending不为0
  612. {
  613. // kdebug("to handle");
  614. // 写入1以清除该interrupter的pending bit
  615. xhci_write_intr_reg32(cid, 0, XHCI_IR_MAN, iman0 | 3);
  616. io_mfence();
  617. struct xhci_TRB_t event_trb, origin_trb; // event ring trb以及其对应的command trb
  618. uint64_t origin_vaddr;
  619. // 暂存当前trb的起始地址
  620. uint64_t last_event_ring_vaddr = xhci_hc[cid].current_event_ring_vaddr;
  621. xhci_get_trb(&event_trb, xhci_hc[cid].current_event_ring_vaddr);
  622. {
  623. struct xhci_TRB_cmd_complete_t *event_trb_ptr = (struct xhci_TRB_cmd_complete_t *)&event_trb;
  624. // kdebug("TRB_type=%d, comp_code=%d", event_trb_ptr->TRB_type, event_trb_ptr->code);
  625. }
  626. while ((event_trb.command & 1) == xhci_hc[cid].current_event_ring_cycle) // 循环处理处于当前周期的所有event ring
  627. {
  628. struct xhci_TRB_cmd_complete_t *event_trb_ptr = (struct xhci_TRB_cmd_complete_t *)&event_trb;
  629. // kdebug("TRB_type=%d, comp_code=%d", event_trb_ptr->TRB_type, event_trb_ptr->code);
  630. if ((event_trb.command & (1 << 2)) == 0) // 当前event trb不是由于short packet产生的
  631. {
  632. // kdebug("event_trb_ptr->code=%d", event_trb_ptr->code);
  633. // kdebug("event_trb_ptr->TRB_type=%d", event_trb_ptr->TRB_type);
  634. switch (event_trb_ptr->code) // 判断它的完成码
  635. {
  636. case TRB_COMP_TRB_SUCCESS: // trb执行成功,则将结果返回到对应的command ring的trb里面
  637. switch (event_trb_ptr->TRB_type) // 根据event trb类型的不同,采取不同的措施
  638. {
  639. case TRB_TYPE_COMMAND_COMPLETION: // 命令已经完成
  640. origin_vaddr = (uint64_t)phys_2_virt(event_trb.param);
  641. // 获取对应的command trb
  642. xhci_get_trb(&origin_trb, origin_vaddr);
  643. switch (((struct xhci_TRB_normal_t *)&origin_trb)->TRB_type)
  644. {
  645. case TRB_TYPE_ENABLE_SLOT: // 源命令为enable slot
  646. // 将slot id返回到命令TRB的command字段中
  647. origin_trb.command &= 0x00ffffff;
  648. origin_trb.command |= (event_trb.command & 0xff000000);
  649. origin_trb.status = event_trb.status;
  650. break;
  651. default:
  652. origin_trb.status = event_trb.status;
  653. break;
  654. }
  655. // 标记该命令已经执行完成
  656. origin_trb.status |= XHCI_IRQ_DONE;
  657. // 将command trb写入到表中
  658. xhci_set_trb(&origin_trb, origin_vaddr);
  659. // kdebug("set origin:%#018lx", origin_vaddr);
  660. break;
  661. }
  662. break;
  663. default:
  664. break;
  665. }
  666. }
  667. else // 当前TRB是由short packet产生的
  668. {
  669. switch (event_trb_ptr->TRB_type)
  670. {
  671. case TRB_TYPE_TRANS_EVENT: // 当前 event trb是 transfer event TRB
  672. // If SPD was encountered in this TD, comp_code will be SPD, else it should be SUCCESS
  673. // (specs 4.10.1.1)
  674. __write4b((uint64_t)phys_2_virt(event_trb.param),
  675. (event_trb.status | XHCI_IRQ_DONE)); // return code + bytes *not* transferred
  676. break;
  677. default:
  678. break;
  679. }
  680. }
  681. // 获取下一个event ring TRB
  682. last_event_ring_vaddr = xhci_hc[cid].current_event_ring_vaddr;
  683. xhci_hc[cid].current_event_ring_vaddr += sizeof(struct xhci_TRB_t);
  684. xhci_get_trb(&event_trb, xhci_hc[cid].current_event_ring_vaddr);
  685. if (((struct xhci_TRB_normal_t *)&event_trb)->TRB_type == TRB_TYPE_LINK)
  686. {
  687. xhci_hc[cid].current_event_ring_vaddr = xhci_hc[cid].event_ring_vaddr;
  688. xhci_get_trb(&event_trb, xhci_hc[cid].current_event_ring_vaddr);
  689. }
  690. }
  691. // 当前event ring cycle的TRB处理结束
  692. // 更新dequeue指针, 并清除event handler busy标志位
  693. xhci_write_intr_reg64(cid, 0, XHCI_IR_DEQUEUE, virt_2_phys(last_event_ring_vaddr) | (1 << 3));
  694. io_mfence();
  695. }
  696. }
  697. /**
  698. * @brief 重置端口
  699. *
  700. * @param id 控制器id
  701. * @param port 端口id
  702. * @return int
  703. */
  704. static int xhci_reset_port(const int id, const int port)
  705. {
  706. int retval = 0;
  707. // 相对于op寄存器基地址的偏移量
  708. uint64_t port_status_offset = XHCI_OPS_PRS + port * 16;
  709. io_mfence();
  710. // 检查端口电源状态
  711. if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
  712. {
  713. kdebug("port is power off, starting...");
  714. io_mfence();
  715. xhci_write_cap_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9));
  716. io_mfence();
  717. usleep(2000);
  718. // 检测端口是否被启用, 若未启用,则报错
  719. if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
  720. {
  721. kdebug("cannot power on %d", port);
  722. return -EAGAIN;
  723. }
  724. }
  725. // kdebug("port:%d, power check ok", port);
  726. io_mfence();
  727. // 确保端口的status被清0
  728. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
  729. // kdebug("to reset timeout;");
  730. io_mfence();
  731. // 重置当前端口
  732. if (XHCI_PORT_IS_USB3(id, port))
  733. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 31));
  734. else
  735. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 4));
  736. retval = -ETIMEDOUT;
  737. // kdebug("to wait reset timeout;");
  738. // 等待portsc的port reset change位被置位,说明reset完成
  739. int timeout = 100;
  740. while (timeout)
  741. {
  742. io_mfence();
  743. uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
  744. io_mfence();
  745. if (val & (1 << 21))
  746. break;
  747. // QEMU对usb的模拟有bug,因此需要检测这里
  748. #ifdef __QEMU_EMULATION__
  749. if (XHCI_PORT_IS_USB3(id, port) && (val & (1 << 31)) == 0)
  750. break;
  751. else if (XHCI_PORT_IS_USB2(id, port) && (val & (1 << 4)) == 0)
  752. break;
  753. #endif
  754. --timeout;
  755. usleep(500);
  756. }
  757. // kdebug("timeout= %d", timeout);
  758. if (timeout > 0)
  759. {
  760. // 等待恢复
  761. usleep(USB_TIME_RST_REC * 100);
  762. uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
  763. io_mfence();
  764. // kdebug("to check if reset ok, val=%#010lx", val);
  765. // 如果reset之后,enable bit仍然是1,那么说明reset成功
  766. if (val & (1 << 1))
  767. {
  768. // kdebug("reset ok");
  769. retval = 0;
  770. io_mfence();
  771. // 清除status change bit
  772. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
  773. io_mfence();
  774. }
  775. else
  776. retval = -1;
  777. }
  778. // kdebug("reset ok!");
  779. // 如果usb2端口成功reset,则处理该端口的active状态
  780. if (retval == 0 && XHCI_PORT_IS_USB2(id, port))
  781. {
  782. xhci_hc[id].ports[port].flags |= XHCI_PROTOCOL_ACTIVE;
  783. if (XHCI_PORT_HAS_PAIR(id, port)) // 如果有对应的usb3端口,则将usb3端口设置为未激活
  784. xhci_hc[id].ports[xhci_hc[id].ports[port].paired_port_num].flags &= ~(XHCI_PROTOCOL_ACTIVE);
  785. }
  786. // 如果usb3端口reset失败,则启用与之配对的usb2端口
  787. if (retval != 0 && XHCI_PORT_IS_USB3(id, port))
  788. {
  789. xhci_hc[id].ports[port].flags &= ~XHCI_PROTOCOL_ACTIVE;
  790. xhci_hc[id].ports[xhci_hc[id].ports[port].paired_port_num].flags |= XHCI_PROTOCOL_ACTIVE;
  791. }
  792. return retval;
  793. }
  794. /**
  795. * @brief 初始化设备slot的上下文,并将其写入dcbaap中的上下文index数组
  796. * - at this time, we don't know if the device is a hub or not, so we don't
  797. * set the slot->hub, ->mtt, ->ttt, ->etc, items.
  798. *
  799. * @param id 控制器id
  800. * @param port 端口号
  801. * @param speed 端口速度
  802. * @param max_packet 最大数据包大小
  803. * @return uint64_t 初始化好的设备上下文空间的虚拟地址
  804. */
  805. static uint64_t xhci_initialize_slot(const int id, const int port, const int speed, const int max_packet)
  806. {
  807. // 为所有的endpoint分配上下文空间
  808. // todo: 按需分配上下文空间
  809. uint64_t device_context_vaddr = (uint64_t)kzalloc(xhci_hc[id].context_size * 32, 0);
  810. // kdebug("slot id=%d, device_context_vaddr=%#018lx, port=%d", slot_id, device_context_vaddr, port);
  811. // 写到数组中
  812. __write8b(xhci_hc[id].dcbaap_vaddr + (xhci_hc[id].ports[port].slot_id * sizeof(uint64_t)),
  813. virt_2_phys(device_context_vaddr));
  814. struct xhci_slot_context_t slot_ctx = {0};
  815. slot_ctx.entries = 1;
  816. slot_ctx.speed = speed;
  817. slot_ctx.route_string = 0;
  818. slot_ctx.rh_port_num = port + 1; // 由于xhci控制器是1-base的,因此把驱动程序中存储的端口号加1,才是真实的端口号
  819. slot_ctx.max_exit_latency = 0; // 稍后会计算这个值
  820. slot_ctx.int_target = 0; // 当前全部使用第0个interrupter
  821. slot_ctx.slot_state = XHCI_SLOT_STATE_DISABLED_OR_ENABLED;
  822. slot_ctx.device_address = 0;
  823. // 将slot信息写入上下文空间
  824. __write_slot(device_context_vaddr, &slot_ctx);
  825. // 初始化控制端点
  826. xhci_initialize_ep(id, device_context_vaddr, port, XHCI_EP_CONTROL, max_packet, 0, USB_EP_CONTROL, 0, speed, 0);
  827. return device_context_vaddr;
  828. }
  829. /**
  830. * @brief 初始化endpoint
  831. *
  832. * @param id 控制器id
  833. * @param slot_vaddr slot上下文的虚拟地址
  834. * @param port_id 插槽id
  835. * @param ep_num 端点上下文在slot上下文区域内的编号
  836. * @param max_packet 最大数据包大小
  837. * @param type 端点类型
  838. * @param direction 传输方向
  839. * @param speed 传输速度
  840. * @param ep_interval 端点的连续请求间隔
  841. */
  842. static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int port_id, const int ep_num,
  843. const int max_packet, const int max_burst, const int type, const int direction,
  844. const int speed, const int ep_interval)
  845. {
  846. // 由于目前只实现获取设备的描述符,因此暂时只支持control ep
  847. if (type != USB_EP_CONTROL && type != USB_EP_INTERRUPT)
  848. return;
  849. struct xhci_ep_context_t ep_ctx = {0};
  850. memset(&ep_ctx, 0, sizeof(struct xhci_ep_context_t));
  851. xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase = xhci_create_ring(XHCI_TRBS_PER_RING);
  852. // 申请ep的 transfer ring
  853. ep_ctx.tr_dequeue_ptr = virt_2_phys(xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase);
  854. xhci_ep_set_dequeue_cycle_state(&ep_ctx, XHCI_TRB_CYCLE_ON);
  855. xhci_hc[id].ports[port_id].ep_info[ep_num].current_ep_ring_vaddr =
  856. xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase;
  857. xhci_hc[id].ports[port_id].ep_info[ep_num].current_ep_ring_cycle = xhci_ep_get_dequeue_cycle_state(&ep_ctx);
  858. // kdebug("ep_ctx.tr_dequeue_ptr = %#018lx", ep_ctx.tr_dequeue_ptr);
  859. // kdebug("xhci_hc[id].control_ep_info.current_ep_ring_cycle = %d",
  860. // xhci_hc[id].control_ep_info.current_ep_ring_cycle);
  861. kdebug("max_packet=%d, max_burst=%d", max_packet, max_burst);
  862. switch (type)
  863. {
  864. case USB_EP_CONTROL: // Control ep
  865. // 设置初始值
  866. ep_ctx.max_packet_size = max_packet;
  867. ep_ctx.linear_stream_array = 0;
  868. ep_ctx.max_primary_streams = 0;
  869. ep_ctx.mult = 0;
  870. ep_ctx.ep_state = XHCI_EP_STATE_DISABLED;
  871. ep_ctx.hid = 0;
  872. ep_ctx.ep_type = XHCI_EP_TYPE_CONTROL;
  873. ep_ctx.average_trb_len = 8; // 所有的control ep的该值均为8
  874. ep_ctx.err_cnt = 3;
  875. ep_ctx.max_burst_size = max_burst;
  876. ep_ctx.interval = ep_interval;
  877. break;
  878. case USB_EP_INTERRUPT:
  879. ep_ctx.max_packet_size = max_packet & 0x7ff;
  880. ep_ctx.max_burst_size = max_burst;
  881. ep_ctx.ep_state = XHCI_EP_STATE_DISABLED;
  882. ep_ctx.mult = 0;
  883. ep_ctx.err_cnt = 3;
  884. ep_ctx.max_esti_payload_hi = ((max_packet * (max_burst + 1)) >> 8) & 0xff;
  885. ep_ctx.max_esti_payload_lo = ((max_packet * (max_burst + 1))) & 0xff;
  886. ep_ctx.interval = ep_interval;
  887. ep_ctx.average_trb_len = 8; // todo: It's not sure how much to fill in this value
  888. // ep_ctx.ep_type = XHCI_EP_TYPE_INTR_IN;
  889. ep_ctx.ep_type = ((ep_num % 2) ? XHCI_EP_TYPE_INTR_IN : XHCI_EP_TYPE_INTR_OUT);
  890. break;
  891. default:
  892. break;
  893. }
  894. // 将ep的信息写入到slot上下文中对应的ep的块中
  895. __write_ep(id, slot_vaddr, ep_num, &ep_ctx);
  896. }
  897. /**
  898. * @brief 向usb控制器发送 address_device命令
  899. *
  900. * @param id 主机控制器id
  901. * @param slot_vaddr 插槽上下文的虚拟基地址
  902. * @param slot_id 插槽id
  903. * @param block 是否阻断 set address 信息向usb设备的传输
  904. * @return int 错误码
  905. */
  906. static int xhci_set_address(const int id, const uint64_t slot_vaddr, const int slot_id, const bool block)
  907. {
  908. int retval = 0;
  909. struct xhci_slot_context_t slot;
  910. struct xhci_ep_context_t ep;
  911. // 创建输入上下文缓冲区
  912. uint64_t input_ctx_buffer = (uint64_t)kzalloc(xhci_hc[id].context_size * 33, 0);
  913. // 置位input control context和slot context的add bit
  914. __write4b(input_ctx_buffer + 4, 0x3);
  915. // 拷贝slot上下文和control ep上下文到输入上下文中
  916. // __write_ep(id, input_ctx_buffer, 2, &ep_ctx);
  917. __read_from_slot(&slot, slot_vaddr);
  918. __read_from_ep(id, slot_vaddr, 1, &ep);
  919. ep.err_cnt = 3;
  920. kdebug("slot.slot_state=%d, speed=%d, root hub port num=%d", slot.slot_state, slot.speed, slot.rh_port_num);
  921. kdebug("ep.type=%d, max_packet=%d, dequeue_ptr=%#018lx", ep.ep_type, ep.max_packet_size, ep.tr_dequeue_ptr);
  922. __write_slot(input_ctx_buffer + xhci_hc[id].context_size, &slot);
  923. __write_ep(id, input_ctx_buffer, 2, &ep);
  924. struct xhci_TRB_normal_t trb = {0};
  925. trb.buf_paddr = virt_2_phys(input_ctx_buffer);
  926. trb.bei = (block ? 1 : 0);
  927. trb.TRB_type = TRB_TYPE_ADDRESS_DEVICE;
  928. trb.intr_target = 0;
  929. trb.cycle = xhci_hc[id].cmd_trb_cycle;
  930. trb.Reserved |= ((slot_id << 8) & 0xffff);
  931. retval = xhci_send_command(id, (struct xhci_TRB_t *)&trb, true);
  932. if (unlikely(retval != 0))
  933. {
  934. kerror("slotid:%d, address device failed", slot_id);
  935. goto failed;
  936. }
  937. struct xhci_TRB_cmd_complete_t *trb_done = (struct xhci_TRB_cmd_complete_t *)&trb;
  938. if (trb_done->code == TRB_COMP_TRB_SUCCESS) // 成功执行
  939. {
  940. // 如果要从控制器获取刚刚设置的设备地址的话,可以在这里读取slot context
  941. ksuccess("slot %d successfully addressed.", slot_id);
  942. retval = 0;
  943. }
  944. else
  945. retval = -EAGAIN;
  946. done:;
  947. failed:;
  948. kfree((void *)input_ctx_buffer);
  949. return retval;
  950. }
  951. /**
  952. * @brief 在指定的端点的ring中,写入一个setup stage TRB
  953. *
  954. * @param ep 端点信息结构体
  955. * @param packet usb请求包
  956. * @param direction 传输的方向
  957. * @return int 产生的TRB数量
  958. */
  959. static int xhci_setup_stage(struct xhci_ep_info_t *ep, const struct usb_request_packet_t *packet,
  960. const uint8_t direction)
  961. {
  962. // kdebug("ep->current_ep_ring_cycle=%d", ep->current_ep_ring_cycle);
  963. struct xhci_TRB_setup_stage_t trb = {0};
  964. trb.bmRequestType = packet->request_type;
  965. trb.bRequest = packet->request;
  966. trb.wValue = packet->value;
  967. trb.wIndex = packet->index;
  968. trb.wLength = packet->length;
  969. trb.transfer_legth = 8;
  970. trb.intr_target = 0; // 使用第0个interrupter
  971. trb.cycle = ep->current_ep_ring_cycle;
  972. trb.ioc = 0;
  973. trb.idt = 1;
  974. trb.TRB_type = TRB_TYPE_SETUP_STAGE;
  975. trb.trt = direction;
  976. // 将setup stage trb拷贝到ep的transfer ring中
  977. __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
  978. return 1;
  979. }
  980. /**
  981. * @brief 向指定的端点中写入data stage trb
  982. *
  983. * @param ep 端点信息结构体
  984. * @param buf_vaddr 数据缓冲区虚拟地址
  985. * @param trb_type trb类型
  986. * @param size 要传输的数据大小
  987. * @param direction 传输方向
  988. * @param max_packet 最大请求包大小
  989. * @param status_vaddr event data TRB的缓冲区(4字节,且地址按照16字节对齐)
  990. * @return int 产生的TRB数量
  991. */
  992. static int xhci_data_stage(struct xhci_ep_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size,
  993. uint8_t direction, const int max_packet, const uint64_t status_vaddr)
  994. {
  995. if (size == 0)
  996. return 0;
  997. int64_t remain_bytes = size;
  998. uint32_t remain_packets = (size + max_packet - 1) / max_packet;
  999. struct xhci_TRB_data_stage_t trb = {0};
  1000. int count_packets = 0;
  1001. // 分多个trb来执行
  1002. while (remain_bytes > 0)
  1003. {
  1004. --remain_packets;
  1005. trb.buf_paddr = virt_2_phys(buf_vaddr);
  1006. trb.intr_target = 0;
  1007. trb.TD_size = remain_packets;
  1008. trb.transfer_length = (remain_bytes < max_packet ? size : max_packet);
  1009. trb.dir = direction;
  1010. trb.TRB_type = trb_type;
  1011. trb.chain = 1;
  1012. trb.ent = (remain_packets == 0);
  1013. trb.cycle = ep->current_ep_ring_cycle;
  1014. trb.ioc = 0;
  1015. // 将data stage trb拷贝到ep的transfer ring中
  1016. __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
  1017. buf_vaddr += max_packet;
  1018. remain_bytes -= max_packet;
  1019. ++count_packets;
  1020. // 对于data stage trb而言,除了第一个trb以外,剩下的trb都是NORMAL的,并且dir是无用的
  1021. trb_type = TRB_TYPE_NORMAL;
  1022. direction = 0;
  1023. }
  1024. // 写入data event trb, 待完成后,完成信息将会存到status_vaddr指向的地址中
  1025. memset(&trb, 0, sizeof(struct xhci_TRB_data_stage_t *));
  1026. trb.buf_paddr = virt_2_phys(status_vaddr);
  1027. trb.intr_target = 0;
  1028. trb.cycle = ep->current_ep_ring_cycle;
  1029. trb.ioc = 1;
  1030. trb.TRB_type = TRB_TYPE_EVENT_DATA;
  1031. __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
  1032. return count_packets + 1;
  1033. }
  1034. /**
  1035. * @brief 填写xhci status stage TRB到control ep的transfer ring
  1036. *
  1037. * @param ep 端点信息结构体
  1038. * @param direction 方向:(h2d:0, d2h:1)
  1039. * @param status_buf_vaddr
  1040. * @return int 创建的TRB数量
  1041. */
  1042. static int xhci_status_stage(struct xhci_ep_info_t *ep, uint8_t direction, uint64_t status_buf_vaddr)
  1043. {
  1044. // kdebug("write status stage trb");
  1045. {
  1046. struct xhci_TRB_status_stage_t trb = {0};
  1047. // 写入status stage trb
  1048. trb.intr_target = 0;
  1049. trb.cycle = ep->current_ep_ring_cycle;
  1050. trb.ent = 0;
  1051. trb.ioc = 1;
  1052. trb.TRB_type = TRB_TYPE_STATUS_STAGE;
  1053. trb.dir = direction;
  1054. __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
  1055. }
  1056. {
  1057. // 写入event data TRB
  1058. struct xhci_TRB_data_stage_t trb = {0};
  1059. trb.buf_paddr = virt_2_phys(status_buf_vaddr);
  1060. trb.intr_target = 0;
  1061. trb.TRB_type = TRB_TYPE_EVENT_DATA;
  1062. trb.ioc = 1;
  1063. trb.cycle = ep->current_ep_ring_cycle;
  1064. __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
  1065. }
  1066. return 2;
  1067. }
  1068. /**
  1069. * @brief 等待状态数据被拷贝到status缓冲区中
  1070. *
  1071. * @param id 主机控制器id
  1072. * @param status_vaddr status 缓冲区
  1073. * @return int 错误码
  1074. */
  1075. static int xhci_wait_for_interrupt(const int id, uint64_t status_vaddr)
  1076. {
  1077. int timer = 500;
  1078. while (timer)
  1079. {
  1080. if (__read4b(status_vaddr) & XHCI_IRQ_DONE)
  1081. {
  1082. uint32_t status = __read4b(status_vaddr);
  1083. // 判断完成码
  1084. switch (xhci_get_comp_code(status))
  1085. {
  1086. case TRB_COMP_TRB_SUCCESS:
  1087. case TRB_COMP_SHORT_PACKET:
  1088. return 0;
  1089. break;
  1090. case TRB_COMP_STALL_ERROR:
  1091. case TRB_COMP_DATA_BUFFER_ERROR:
  1092. case TRB_COMP_BABBLE_DETECTION:
  1093. return -EINVAL;
  1094. default:
  1095. kerror("xhci wait interrupt: status=%#010x, complete_code=%d", status, xhci_get_comp_code(status));
  1096. return -EIO;
  1097. }
  1098. }
  1099. --timer;
  1100. usleep(1000);
  1101. }
  1102. kerror(" USB xHCI Interrupt wait timed out.");
  1103. return -ETIMEDOUT;
  1104. }
  1105. /**
  1106. * @brief 从指定插槽的control endpoint读取信息
  1107. *
  1108. * @param id 主机控制器id
  1109. * @param packet usb数据包
  1110. * @param target 读取到的信息存放到的位置
  1111. * @param port_id 端口id
  1112. * @param max_packet 最大数据包大小
  1113. * @return int 读取到的数据的大小
  1114. */
  1115. static int xhci_control_in(const int id, struct usb_request_packet_t *packet, void *target, const int port_id,
  1116. const int max_packet)
  1117. {
  1118. uint64_t status_buf_vaddr =
  1119. (uint64_t)kzalloc(16, 0); // 本来是要申请4bytes的buffer的,但是因为xhci控制器需要16bytes对齐,因此申请16bytes
  1120. uint64_t data_buf_vaddr = 0;
  1121. int retval = 0;
  1122. // 往control ep写入一个setup stage trb
  1123. xhci_setup_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], packet, XHCI_DIR_IN);
  1124. if (packet->length)
  1125. {
  1126. data_buf_vaddr = (uint64_t)kzalloc(packet->length, 0);
  1127. xhci_data_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], data_buf_vaddr, TRB_TYPE_DATA_STAGE,
  1128. packet->length, XHCI_DIR_IN_BIT, max_packet, status_buf_vaddr);
  1129. }
  1130. /*
  1131. QEMU doesn't quite handle SETUP/DATA/STATUS transactions correctly.
  1132. It will wait for the STATUS TRB before it completes the transfer.
  1133. Technically, you need to check for a good transfer before you send the
  1134. STATUS TRB. However, since QEMU doesn't update the status until after
  1135. the STATUS TRB, waiting here will not complete a successful transfer.
  1136. Bochs and real hardware handles this correctly, however QEMU does not.
  1137. If you are using QEMU, do not ring the doorbell here. Ring the doorbell
  1138. *after* you place the STATUS TRB on the ring.
  1139. (See bug report: https://bugs.launchpad.net/qemu/+bug/1859378 )
  1140. */
  1141. #ifndef __QEMU_EMULATION__
  1142. // 如果不是qemu虚拟机,则可以直接发起传输
  1143. // kdebug(" not qemu");
  1144. __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
  1145. retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
  1146. if (unlikely(retval != 0))
  1147. goto failed;
  1148. #endif
  1149. memset((void *)status_buf_vaddr, 0, 16);
  1150. xhci_status_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], XHCI_DIR_OUT_BIT, status_buf_vaddr);
  1151. __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
  1152. retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
  1153. if (unlikely(retval != 0))
  1154. goto failed;
  1155. // 将读取到的数据拷贝到目标区域
  1156. if (packet->length)
  1157. memcpy(target, (void *)data_buf_vaddr, packet->length);
  1158. retval = packet->length;
  1159. goto done;
  1160. failed:;
  1161. kdebug("wait 4 interrupt failed");
  1162. retval = 0;
  1163. done:;
  1164. // 释放内存
  1165. kfree((void *)status_buf_vaddr);
  1166. if (packet->length)
  1167. kfree((void *)data_buf_vaddr);
  1168. return retval;
  1169. }
  1170. /**
  1171. * @brief 向指定插槽的control ep输出信息
  1172. *
  1173. * @param id 主机控制器id
  1174. * @param packet usb数据包
  1175. * @param target 返回的数据存放的位置
  1176. * @param port_id 端口id
  1177. * @param max_packet 最大数据包大小
  1178. * @return int 读取到的数据的大小
  1179. */
  1180. static int xhci_control_out(const int id, struct usb_request_packet_t *packet, void *target, const int port_id,
  1181. const int max_packet)
  1182. {
  1183. uint64_t status_buf_vaddr = (uint64_t)kzalloc(16, 0);
  1184. uint64_t data_buf_vaddr = 0;
  1185. int retval = 0;
  1186. // 往control ep写入一个setup stage trb
  1187. xhci_setup_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], packet, XHCI_DIR_OUT);
  1188. if (packet->length)
  1189. {
  1190. data_buf_vaddr = (uint64_t)kzalloc(packet->length, 0);
  1191. xhci_data_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], data_buf_vaddr, TRB_TYPE_DATA_STAGE,
  1192. packet->length, XHCI_DIR_OUT_BIT, max_packet, status_buf_vaddr);
  1193. }
  1194. #ifndef __QEMU_EMULATION__
  1195. // 如果不是qemu虚拟机,则可以直接发起传输
  1196. __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
  1197. retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
  1198. if (unlikely(retval != 0))
  1199. goto failed;
  1200. #endif
  1201. memset((void *)status_buf_vaddr, 0, 16);
  1202. xhci_status_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], XHCI_DIR_IN_BIT, status_buf_vaddr);
  1203. __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
  1204. #ifndef __QEMU_EMULATION__
  1205. // qemu对于这个操作的处理有问题,status_buf并不会被修改。而真机不存在该问题
  1206. retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
  1207. #endif
  1208. if (unlikely(retval != 0))
  1209. goto failed;
  1210. // 将读取到的数据拷贝到目标区域
  1211. if (packet->length)
  1212. memcpy(target, (void *)data_buf_vaddr, packet->length);
  1213. retval = packet->length;
  1214. goto done;
  1215. failed:;
  1216. kdebug("wait 4 interrupt failed");
  1217. retval = 0;
  1218. done:;
  1219. // 释放内存
  1220. kfree((void *)status_buf_vaddr);
  1221. if (packet->length)
  1222. kfree((void *)data_buf_vaddr);
  1223. return retval;
  1224. }
  1225. /**
  1226. * @brief 获取描述符
  1227. *
  1228. * @param id 控制器号
  1229. * @param port_id 端口号
  1230. * @param target 获取到的数据要拷贝到的地址
  1231. * @param desc_type 描述符类型
  1232. * @param desc_index 描述符的索引号
  1233. * @param lang_id 语言id(默认为0)
  1234. * @param length 要传输的数据长度
  1235. * @return int 错误码
  1236. */
  1237. static inline int xhci_get_desc(const int id, const int port_id, void *target, const uint16_t desc_type,
  1238. const uint8_t desc_index, const uint16_t lang_id, const uint16_t length)
  1239. {
  1240. struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
  1241. int count;
  1242. BUG_ON(dev_desc == NULL);
  1243. // 设备端口没有对应的描述符
  1244. if (unlikely(dev_desc == NULL))
  1245. return -EINVAL;
  1246. uint8_t req_type = USB_REQ_TYPE_GET_REQUEST;
  1247. if (desc_type == USB_DT_HID_REPORT)
  1248. req_type = USB_REQ_TYPE_GET_INTERFACE_REQUEST;
  1249. DECLARE_USB_PACKET(ctrl_in_packet, req_type, USB_REQ_GET_DESCRIPTOR, (desc_type << 8) | desc_index, lang_id,
  1250. length);
  1251. count = xhci_control_in(id, &ctrl_in_packet, target, port_id, dev_desc->max_packet_size);
  1252. if (unlikely(count == 0))
  1253. return -EAGAIN;
  1254. return 0;
  1255. }
  1256. static inline int xhci_set_configuration(const int id, const int port_id, const uint8_t conf_value)
  1257. {
  1258. struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
  1259. int count;
  1260. BUG_ON(dev_desc == NULL);
  1261. // 设备端口没有对应的描述符
  1262. if (unlikely(dev_desc == NULL))
  1263. return -EINVAL;
  1264. DECLARE_USB_PACKET(ctrl_out_packet, USB_REQ_TYPE_SET_REQUEST, USB_REQ_SET_CONFIGURATION, conf_value & 0xff, 0, 0);
  1265. // kdebug("set conf: to control out");
  1266. count = xhci_control_out(id, &ctrl_out_packet, NULL, port_id, dev_desc->max_packet_size);
  1267. // kdebug("set conf: count=%d", count);
  1268. return 0;
  1269. }
  1270. /**
  1271. * @brief 获取usb 设备的config_desc
  1272. *
  1273. * @param id 主机控制器id
  1274. * @param port_id 端口id
  1275. * @param conf_desc 要获取的conf_desc
  1276. * @return int 错误码
  1277. */
  1278. static int xhci_get_config_desc(const int id, const int port_id, struct usb_config_desc *conf_desc)
  1279. {
  1280. if (unlikely(conf_desc == NULL))
  1281. return -EINVAL;
  1282. kdebug("to get conf for port %d", port_id);
  1283. int retval = xhci_get_desc(id, port_id, conf_desc, USB_DT_CONFIG, 0, 0, 9);
  1284. if (unlikely(retval != 0))
  1285. return retval;
  1286. kdebug("port %d got conf ok. type=%d, len=%d, total_len=%d, num_interfaces=%d, max_power=%dmA", port_id,
  1287. conf_desc->type, conf_desc->len, conf_desc->total_len, conf_desc->num_interfaces,
  1288. (xhci_get_port_speed(id, port_id) == XHCI_PORT_SPEED_SUPER) ? (conf_desc->max_power * 8)
  1289. : (conf_desc->max_power * 2));
  1290. return 0;
  1291. }
  1292. /**
  1293. * @brief 获取完整的config desc(包含conf、interface、endpoint)
  1294. *
  1295. * @param id 控制器id
  1296. * @param port_id 端口id
  1297. * @param conf_desc 之前已经获取好的config_desc
  1298. * @param target 最终结果要拷贝到的地址
  1299. * @return int 错误码
  1300. */
  1301. static inline int xhci_get_config_desc_full(const int id, const int port_id, const struct usb_config_desc *conf_desc,
  1302. void *target)
  1303. {
  1304. if (unlikely(conf_desc == NULL || target == NULL))
  1305. return -EINVAL;
  1306. return xhci_get_desc(id, port_id, target, USB_DT_CONFIG, 0, 0, conf_desc->total_len);
  1307. }
  1308. /**
  1309. * @brief 从完整的conf_desc数据中获取指定的interface_desc的指针
  1310. *
  1311. * @param in_buf 存储了完整的conf_desc的缓冲区
  1312. * @param if_num 接口号
  1313. * @param if_desc 返回的指向接口结构体的指针
  1314. * @return int 错误码
  1315. */
  1316. static int xhci_get_interface_desc(const void *in_buf, const uint8_t if_num, struct usb_interface_desc **if_desc)
  1317. {
  1318. if (unlikely(if_desc == NULL || in_buf == NULL))
  1319. return -EINVAL;
  1320. // 判断接口index是否合理
  1321. if (if_num >= ((struct usb_config_desc *)in_buf)->num_interfaces)
  1322. return -EINVAL;
  1323. uint32_t total_len = ((struct usb_config_desc *)in_buf)->total_len;
  1324. uint32_t pos = 0;
  1325. while (pos < total_len)
  1326. {
  1327. struct usb_interface_desc *ptr = (struct usb_interface_desc *)(in_buf + pos);
  1328. if (ptr->type != USB_DT_INTERFACE)
  1329. {
  1330. pos += ptr->len;
  1331. continue;
  1332. }
  1333. if (ptr->interface_number == if_num) // 找到目标interface desc
  1334. {
  1335. kdebug("get interface desc ok. interface_number=%d, num_endpoints=%d, class=%d, subclass=%d",
  1336. ptr->interface_number, ptr->num_endpoints, ptr->interface_class, ptr->interface_sub_class);
  1337. *if_desc = ptr;
  1338. return 0;
  1339. }
  1340. pos += ptr->len;
  1341. }
  1342. return -EINVAL;
  1343. }
  1344. /**
  1345. * @brief 获取端点描述符
  1346. *
  1347. * @param if_desc 接口描述符
  1348. * @param ep_num 端点号
  1349. * @param ep_desc 返回的指向端点描述符的指针
  1350. * @return int 错误码
  1351. */
  1352. static inline int xhci_get_endpoint_desc(const struct usb_interface_desc *if_desc, const uint8_t ep_num,
  1353. struct usb_endpoint_desc **ep_desc)
  1354. {
  1355. if (unlikely(if_desc == NULL || ep_desc == NULL))
  1356. return -EINVAL;
  1357. BUG_ON(ep_num >= if_desc->num_endpoints);
  1358. *ep_desc = (struct usb_endpoint_desc *)((uint64_t)(if_desc + 1) + ep_num * sizeof(struct usb_endpoint_desc));
  1359. kdebug("get endpoint desc: ep_addr=%d, max_packet=%d, attr=%#06x, interval=%d", (*ep_desc)->endpoint_addr,
  1360. (*ep_desc)->max_packet, (*ep_desc)->attributes, (*ep_desc)->interval);
  1361. return 0;
  1362. }
  1363. /**
  1364. * @brief 初始化设备端口,并获取端口的描述信息
  1365. *
  1366. * @param id 主机控制器id
  1367. * @param port_id 端口id
  1368. * @param dev_desc 设备描述符
  1369. * @return int 错误码
  1370. */
  1371. static int xhci_get_descriptor(const int id, const int port_id, struct usb_device_desc *dev_desc)
  1372. {
  1373. int retval = 0;
  1374. int count = 0;
  1375. if (unlikely(dev_desc == NULL))
  1376. return -EINVAL;
  1377. // 读取端口速度。 full=1, low=2, high=3, super=4
  1378. uint32_t speed = xhci_get_port_speed(id, port_id);
  1379. /*
  1380. * Some devices will only send the first 8 bytes of the device descriptor
  1381. * while in the default state. We must request the first 8 bytes, then reset
  1382. * the port, set address, then request all 18 bytes.
  1383. */
  1384. struct xhci_TRB_normal_t trb = {0};
  1385. trb.TRB_type = TRB_TYPE_ENABLE_SLOT;
  1386. // kdebug("to enable slot");
  1387. if (xhci_send_command(id, (struct xhci_TRB_t *)&trb, true) != 0)
  1388. {
  1389. kerror("portid:%d: send enable slot failed", port_id);
  1390. return -ETIMEDOUT;
  1391. }
  1392. // kdebug("send enable slot ok");
  1393. uint32_t slot_id = ((struct xhci_TRB_cmd_complete_t *)&trb)->slot_id;
  1394. int16_t max_packet;
  1395. if (slot_id != 0) // slot id不为0时,是合法的slot id
  1396. {
  1397. // 为不同速度的设备确定最大的数据包大小
  1398. switch (speed)
  1399. {
  1400. case XHCI_PORT_SPEED_LOW:
  1401. max_packet = 8;
  1402. break;
  1403. case XHCI_PORT_SPEED_FULL:
  1404. case XHCI_PORT_SPEED_HI:
  1405. max_packet = 64;
  1406. break;
  1407. case XHCI_PORT_SPEED_SUPER:
  1408. max_packet = 512;
  1409. break;
  1410. }
  1411. }
  1412. else
  1413. return -EAGAIN; // slot id 不合法
  1414. xhci_hc[id].ports[port_id].slot_id = slot_id;
  1415. // kdebug("speed=%d", speed);
  1416. // 初始化接口的上下文
  1417. uint64_t slot_vaddr = xhci_initialize_slot(id, port_id, speed, max_packet);
  1418. retval = xhci_set_address(id, slot_vaddr, slot_id, true);
  1419. // kdebug("set addr again");
  1420. // 再次发送 set_address命令
  1421. // kdebug("to set addr again");
  1422. retval = xhci_set_address(id, slot_vaddr, slot_id, false);
  1423. if (retval != 0)
  1424. return retval;
  1425. memset(dev_desc, 0, sizeof(struct usb_device_desc));
  1426. DECLARE_USB_PACKET(ctrl_in_packet, USB_REQ_TYPE_GET_REQUEST, USB_REQ_GET_DESCRIPTOR, (USB_DT_DEVICE << 8), 0, 18);
  1427. count = xhci_control_in(id, &ctrl_in_packet, dev_desc, port_id, max_packet);
  1428. if (unlikely(count == 0))
  1429. return -EAGAIN;
  1430. /*
  1431. TODO: if the dev_desc->max_packet was different than what we have as max_packet,
  1432. you would need to change it here and in the slot context by doing a
  1433. evaluate_slot_context call.
  1434. */
  1435. xhci_hc[id].ports[port_id].dev_desc = dev_desc;
  1436. // print the descriptor
  1437. printk(" Found USB Device:\n"
  1438. " port: %i\n"
  1439. " len: %i\n"
  1440. " type: %i\n"
  1441. " version: %01X.%02X\n"
  1442. " class: %i\n"
  1443. " subclass: %i\n"
  1444. " protocol: %i\n"
  1445. " max packet size: %i\n"
  1446. " vendor id: 0x%04X\n"
  1447. " product id: 0x%04X\n"
  1448. " release ver: %i%i.%i%i\n"
  1449. " manufacture index: %i (index to a string)\n"
  1450. " product index: %i\n"
  1451. " serial index: %i\n"
  1452. " number of configs: %i\n",
  1453. port_id, dev_desc->len, dev_desc->type, dev_desc->usb_version >> 8, dev_desc->usb_version & 0xFF,
  1454. dev_desc->_class, dev_desc->subclass, dev_desc->protocol, dev_desc->max_packet_size, dev_desc->vendor_id,
  1455. dev_desc->product_id, (dev_desc->device_rel & 0xF000) >> 12, (dev_desc->device_rel & 0x0F00) >> 8,
  1456. (dev_desc->device_rel & 0x00F0) >> 4, (dev_desc->device_rel & 0x000F) >> 0, dev_desc->manufacturer_index,
  1457. dev_desc->procuct_index, dev_desc->serial_index, dev_desc->config);
  1458. return 0;
  1459. }
  1460. /**
  1461. * @brief 启用xhci控制器的端口
  1462. *
  1463. * @param id 控制器id
  1464. * @return int
  1465. */
  1466. static int xhci_hc_start_ports(int id)
  1467. {
  1468. int cnt = 0;
  1469. // 注意,这两个循环应该不能合并到一起,因为可能存在usb2端口offset在前,usb3端口在后的情况,那样的话就会出错
  1470. // 循环启动所有的usb3端口
  1471. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  1472. {
  1473. if (XHCI_PORT_IS_USB3(id, i) && XHCI_PORT_IS_ACTIVE(id, i))
  1474. {
  1475. io_mfence();
  1476. // kdebug("to reset port %d, rflags=%#018lx", id, get_rflags());
  1477. int rst_ret = xhci_reset_port(id, i);
  1478. // kdebug("reset done!, val=%d", rst_ret);
  1479. // reset该端口
  1480. if (likely(rst_ret == 0)) // 如果端口reset成功,就获取它的描述符
  1481. // 否则,reset函数会把它给设置为未激活,并且标志配对的usb2端口是激活的
  1482. {
  1483. // kdebug("reset port %d ok", id);
  1484. struct usb_device_desc dev_desc = {0};
  1485. if (xhci_get_descriptor(id, i, &dev_desc) == 0)
  1486. {
  1487. xhci_configure_port(id, i);
  1488. ++cnt;
  1489. }
  1490. kdebug("usb3 port %d get desc ok", i);
  1491. }
  1492. }
  1493. }
  1494. kdebug("Active usb3 ports:%d", cnt);
  1495. // 循环启动所有的usb2端口
  1496. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  1497. {
  1498. if (XHCI_PORT_IS_USB2(id, i) && XHCI_PORT_IS_ACTIVE(id, i))
  1499. {
  1500. // kdebug("initializing usb2: %d", i);
  1501. // reset该端口
  1502. // kdebug("to reset port %d, rflags=%#018lx", i, get_rflags());
  1503. if (likely(xhci_reset_port(id, i) ==
  1504. 0)) // 如果端口reset成功,就获取它的描述符
  1505. // 否则,reset函数会把它给设置为未激活,并且标志配对的usb2端口是激活的
  1506. {
  1507. // kdebug("reset port %d ok", id);
  1508. struct usb_device_desc dev_desc = {0};
  1509. if (xhci_get_descriptor(id, i, &dev_desc) == 0)
  1510. {
  1511. xhci_configure_port(id, i);
  1512. ++cnt;
  1513. }
  1514. kdebug("USB2 port %d get desc ok", i);
  1515. }
  1516. }
  1517. }
  1518. kinfo("xHCI controller %d: Started %d ports.", id, cnt);
  1519. return 0;
  1520. }
  1521. /**
  1522. * @brief 发送HID设备的IDLE数据包
  1523. *
  1524. * @param id 主机控制器号
  1525. * @param port_id 端口号
  1526. * @param if_desc 接口结构体
  1527. * @return int
  1528. */
  1529. static int xhci_hid_set_idle(const int id, const int port_id, struct usb_interface_desc *if_desc)
  1530. {
  1531. struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
  1532. if (unlikely(dev_desc) == NULL)
  1533. {
  1534. BUG_ON(1);
  1535. return -EINVAL;
  1536. }
  1537. DECLARE_USB_PACKET(ctrl_out_packet, USB_REQ_TYPE_SET_CLASS_INTERFACE, 0x0a, 0, 0, 0);
  1538. xhci_control_out(id, &ctrl_out_packet, NULL, port_id, dev_desc->max_packet_size);
  1539. kdebug("xhci set idle done!");
  1540. return 0;
  1541. }
  1542. /**
  1543. * @brief 配置端点上下文,并发送configure endpoint命令
  1544. *
  1545. * @param id 主机控制器id
  1546. * @param port_id 端口号
  1547. * @param ep_num 端点号
  1548. * @param ep_type 端点类型
  1549. * @param ep_desc 端点描述符
  1550. * @return int 错误码
  1551. */
  1552. static int xhci_configure_endpoint(const int id, const int port_id, const uint8_t ep_num, const uint8_t ep_type,
  1553. struct usb_endpoint_desc *ep_desc)
  1554. {
  1555. int retval = 0;
  1556. uint64_t slot_context_vaddr = xhci_get_device_context_vaddr(id, port_id);
  1557. xhci_initialize_ep(id, slot_context_vaddr, port_id, ep_num, xhci_hc[id].ports[port_id].dev_desc->max_packet_size,
  1558. usb_get_max_burst_from_ep(ep_desc), ep_type, (ep_num % 2) ? XHCI_DIR_IN_BIT : XHCI_DIR_OUT_BIT,
  1559. xhci_get_port_speed(id, port_id), ep_desc->interval);
  1560. struct xhci_slot_context_t slot;
  1561. struct xhci_ep_context_t ep = {0};
  1562. // 创建输入上下文缓冲区
  1563. uint64_t input_ctx_buffer = (uint64_t)kzalloc(xhci_hc[id].context_size * 33, 0);
  1564. // 置位对应的add bit
  1565. __write4b(input_ctx_buffer + 4, (1 << ep_num) | 1);
  1566. __write4b(input_ctx_buffer + 0x1c, 1);
  1567. // 拷贝slot上下文
  1568. __read_from_slot(&slot, slot_context_vaddr);
  1569. // 设置该端口的最大端点号。注意,必须设置这里,否则会出错
  1570. slot.entries = (ep_num > slot.entries) ? ep_num : slot.entries;
  1571. __write_slot(input_ctx_buffer + xhci_hc[id].context_size, &slot);
  1572. // __write_ep(id, input_ctx_buffer, 2, &ep);
  1573. // kdebug("ep_num=%d", ep_num);
  1574. // 拷贝将要被配置的端点的信息
  1575. __read_from_ep(id, slot_context_vaddr, ep_num, &ep);
  1576. // kdebug("ep.tr_dequeue_ptr=%#018lx", ep.tr_dequeue_ptr);
  1577. ep.err_cnt = 3;
  1578. // 加一是因为input_context头部比slot_context多了一个input_control_ctx
  1579. __write_ep(id, input_ctx_buffer, ep_num + 1, &ep);
  1580. struct xhci_TRB_normal_t trb = {0};
  1581. trb.buf_paddr = virt_2_phys(input_ctx_buffer);
  1582. trb.TRB_type = TRB_TYPE_CONFIG_EP;
  1583. trb.cycle = xhci_hc[id].cmd_trb_cycle;
  1584. trb.Reserved |= (((uint16_t)xhci_hc[id].ports[port_id].slot_id) << 8) & 0xffff;
  1585. // kdebug("addr=%#018lx", ((struct xhci_TRB_t *)&trb)->param);
  1586. // kdebug("status=%#018lx", ((struct xhci_TRB_t *)&trb)->status);
  1587. // kdebug("command=%#018lx", ((struct xhci_TRB_t *)&trb)->command);
  1588. retval = xhci_send_command(id, (struct xhci_TRB_t *)&trb, true);
  1589. if (unlikely(retval != 0))
  1590. {
  1591. kerror("port_id:%d, configure endpoint %d failed", port_id, ep_num);
  1592. goto failed;
  1593. }
  1594. struct xhci_TRB_cmd_complete_t *trb_done = (struct xhci_TRB_cmd_complete_t *)&trb;
  1595. if (trb_done->code == TRB_COMP_TRB_SUCCESS) // 成功执行
  1596. {
  1597. // 如果要从控制器获取刚刚设置的设备地址的话,可以在这里读取slot context
  1598. ksuccess("port_id:%d, ep:%d successfully configured.", port_id, ep_num);
  1599. retval = 0;
  1600. }
  1601. else
  1602. retval = -EAGAIN;
  1603. done:;
  1604. failed:;
  1605. kfree((void *)input_ctx_buffer);
  1606. return retval;
  1607. }
  1608. /**
  1609. * @brief 配置连接在指定端口上的设备
  1610. *
  1611. * @param id 主机控制器id
  1612. * @param port_id 端口id
  1613. * @param full_conf 完整的config
  1614. * @return int 错误码
  1615. */
  1616. static int xhci_configure_port(const int id, const int port_id)
  1617. {
  1618. void *full_conf = NULL;
  1619. struct usb_interface_desc *if_desc = NULL;
  1620. struct usb_endpoint_desc *ep_desc = NULL;
  1621. int retval = 0;
  1622. // hint: 暂时只考虑对键盘的初始化
  1623. // 获取完整的config
  1624. {
  1625. struct usb_config_desc conf_desc = {0};
  1626. retval = xhci_get_config_desc(id, port_id, &conf_desc);
  1627. if (unlikely(retval != 0))
  1628. return retval;
  1629. full_conf = kzalloc(conf_desc.total_len, 0);
  1630. if (unlikely(full_conf == NULL))
  1631. return -ENOMEM;
  1632. retval = xhci_get_config_desc_full(id, port_id, &conf_desc, full_conf);
  1633. if (unlikely(retval != 0))
  1634. goto failed;
  1635. }
  1636. retval = xhci_get_interface_desc(full_conf, 0, &if_desc);
  1637. if (unlikely(retval != 0))
  1638. goto failed;
  1639. if (if_desc->interface_class == USB_CLASS_HID)
  1640. {
  1641. // 由于暂时只支持键盘,因此把键盘的驱动也写在这里
  1642. // todo: 分离usb键盘驱动
  1643. retval = xhci_get_endpoint_desc(if_desc, 0, &ep_desc);
  1644. if (unlikely(retval != 0))
  1645. goto failed;
  1646. // kdebug("to set conf, val=%#010lx", ((struct usb_config_desc *)full_conf)->value);
  1647. retval = xhci_set_configuration(id, port_id, ((struct usb_config_desc *)full_conf)->value);
  1648. if (unlikely(retval != 0))
  1649. goto failed;
  1650. // kdebug("set conf ok");
  1651. // configure endpoint
  1652. retval = xhci_configure_endpoint(id, port_id, ep_desc->endpoint_addr, USB_EP_INTERRUPT, ep_desc);
  1653. if (unlikely(retval != 0))
  1654. goto failed;
  1655. retval = xhci_hid_set_idle(id, port_id, if_desc);
  1656. if (unlikely(retval != 0))
  1657. goto failed;
  1658. struct usb_hid_desc *hid_desc = NULL;
  1659. uint32_t hid_desc_len = 0;
  1660. // 获取hid desc
  1661. retval = xhci_get_hid_descriptor(id, port_id, full_conf, if_desc->interface_number, &hid_desc);
  1662. if (unlikely(retval != 0))
  1663. goto failed;
  1664. // 获取hid report
  1665. void *hid_report_data = kzalloc(hid_desc->report_desc_len, 0);
  1666. if (unlikely(hid_report_data == NULL))
  1667. goto failed;
  1668. retval =
  1669. xhci_get_hid_report(id, port_id, if_desc->interface_number, hid_report_data, hid_desc->report_desc_len);
  1670. if (unlikely(retval != 0))
  1671. {
  1672. kfree(hid_report_data);
  1673. goto failed;
  1674. }
  1675. kdebug("to parse hid report");
  1676. // todo: parse hid report
  1677. hid_parse_report(hid_report_data, hid_desc->report_desc_len);
  1678. kdebug("parse hid report done");
  1679. kfree(hid_report_data);
  1680. }
  1681. goto out;
  1682. failed:;
  1683. kerror("failed at xhci_configure_port, retval=%d", retval);
  1684. out:;
  1685. kfree(full_conf);
  1686. return retval;
  1687. }
  1688. /**
  1689. * @brief 初始化xhci主机控制器的中断控制
  1690. *
  1691. * @param id 主机控制器id
  1692. * @return int 返回码
  1693. */
  1694. static int xhci_hc_init_intr(int id)
  1695. {
  1696. uint64_t retval = 0;
  1697. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  1698. struct xhci_caps_HCSPARAMS2_reg_t hcs2;
  1699. io_mfence();
  1700. memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  1701. io_mfence();
  1702. memcpy(&hcs2, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t));
  1703. io_mfence();
  1704. uint32_t max_segs = (1 << (uint32_t)(hcs2.ERST_Max));
  1705. uint32_t max_interrupters = hcs1.max_intrs;
  1706. // 创建 event ring
  1707. retval = xhci_create_event_ring(4096, &xhci_hc[id].event_ring_vaddr);
  1708. io_mfence();
  1709. if (unlikely((int64_t)(retval) == -ENOMEM))
  1710. return -ENOMEM;
  1711. xhci_hc[id].event_ring_table_vaddr = retval;
  1712. xhci_hc[id].current_event_ring_vaddr =
  1713. xhci_hc[id].event_ring_vaddr; // 设置驱动程序要读取的下一个event ring trb的地址
  1714. retval = 0;
  1715. xhci_hc[id].current_event_ring_cycle = 1;
  1716. // 写入第0个中断寄存器组
  1717. io_mfence();
  1718. xhci_write_intr_reg32(id, 0, XHCI_IR_MAN, 0x3); // 使能中断并清除pending位(这个pending位是写入1就清0的)
  1719. io_mfence();
  1720. xhci_write_intr_reg32(id, 0, XHCI_IR_MOD, 0); // 关闭中断管制
  1721. io_mfence();
  1722. xhci_write_intr_reg32(id, 0, XHCI_IR_TABLE_SIZE, 1); // 当前只有1个segment
  1723. io_mfence();
  1724. xhci_write_intr_reg64(id, 0, XHCI_IR_DEQUEUE,
  1725. virt_2_phys(xhci_hc[id].current_event_ring_vaddr) |
  1726. (1 << 3)); // 写入dequeue寄存器,并清除busy位(写1就会清除)
  1727. io_mfence();
  1728. xhci_write_intr_reg64(id, 0, XHCI_IR_TABLE_ADDR, virt_2_phys(xhci_hc[id].event_ring_table_vaddr)); // 写入table地址
  1729. io_mfence();
  1730. // 清除状态位
  1731. xhci_write_op_reg32(id, XHCI_OPS_USBSTS, (1 << 10) | (1 << 4) | (1 << 3) | (1 << 2));
  1732. io_mfence();
  1733. // 开启usb中断
  1734. // 注册中断处理程序
  1735. struct xhci_hc_irq_install_info_t install_info;
  1736. install_info.assert = 1;
  1737. install_info.edge_trigger = 1;
  1738. install_info.processor = 0; // 投递到bsp
  1739. char *buf = (char *)kmalloc(16, 0);
  1740. memset(buf, 0, 16);
  1741. sprintk(buf, "xHCI HC%d", id);
  1742. io_mfence();
  1743. irq_register(xhci_controller_irq_num[id], &install_info, &xhci_hc_irq_handler, id, &xhci_hc_intr_controller, buf);
  1744. io_mfence();
  1745. kfree(buf);
  1746. kdebug("xhci host controller %d: interrupt registered. irq num=%d", id, xhci_controller_irq_num[id]);
  1747. return 0;
  1748. }
  1749. /**
  1750. * @brief 往xhci控制器发送trb, 并将返回的数据存入原始的trb中
  1751. *
  1752. * @param id xhci控制器号
  1753. * @param trb 传输请求块
  1754. * @param do_ring 是否通知doorbell register
  1755. * @return int 错误码
  1756. */
  1757. static int xhci_send_command(int id, struct xhci_TRB_t *trb, const bool do_ring)
  1758. {
  1759. uint64_t origin_trb_vaddr = xhci_hc[id].cmd_trb_vaddr;
  1760. // 必须先写入参数和状态数据,最后写入command
  1761. __write8b(xhci_hc[id].cmd_trb_vaddr, trb->param); // 参数
  1762. __write4b(xhci_hc[id].cmd_trb_vaddr + 8, trb->status); // 状态
  1763. __write4b(xhci_hc[id].cmd_trb_vaddr + 12, trb->command | xhci_hc[id].cmd_trb_cycle); // 命令
  1764. xhci_hc[id].cmd_trb_vaddr += sizeof(struct xhci_TRB_t); // 跳转到下一个trb
  1765. {
  1766. // 如果下一个trb是link trb,则将下一个要操作的地址是设置为第一个trb
  1767. struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)xhci_hc[id].cmd_trb_vaddr;
  1768. if (ptr->TRB_type == TRB_TYPE_LINK)
  1769. {
  1770. ptr->cycle = xhci_hc[id].cmd_trb_cycle;
  1771. xhci_hc[id].cmd_trb_vaddr = xhci_hc[id].cmd_ring_vaddr;
  1772. xhci_hc[id].cmd_trb_cycle ^= 1;
  1773. }
  1774. }
  1775. if (do_ring) // 按响命令门铃
  1776. {
  1777. __xhci_write_doorbell(id, 0, 0);
  1778. // 等待中断产生
  1779. int timer = 400;
  1780. const uint32_t iman0 = xhci_read_intr_reg32(id, 0, XHCI_IR_MAN);
  1781. // Now wait for the interrupt to happen
  1782. // We use bit 31 of the command dword since it is reserved
  1783. while (timer && ((__read4b(origin_trb_vaddr + 8) & XHCI_IRQ_DONE) == 0))
  1784. {
  1785. usleep(1000);
  1786. --timer;
  1787. }
  1788. uint32_t x = xhci_read_cap_reg32(id, xhci_hc[id].rts_offset + 0x20);
  1789. if (timer == 0)
  1790. return -ETIMEDOUT;
  1791. else
  1792. {
  1793. xhci_get_trb(trb, origin_trb_vaddr);
  1794. trb->status &= (~XHCI_IRQ_DONE);
  1795. }
  1796. }
  1797. return 0;
  1798. }
  1799. /**
  1800. * @brief 获取接口的hid descriptor
  1801. *
  1802. * @param id 主机控制器号
  1803. * @param port_id 端口号
  1804. * @param full_conf 完整的cofig缓冲区
  1805. * @param interface_number 接口号
  1806. * @param ret_hid_desc 返回的指向hid描述符的指针
  1807. * @return int 错误码
  1808. */
  1809. static int xhci_get_hid_descriptor(int id, int port_id, const void *full_conf, int interface_number,
  1810. struct usb_hid_desc **ret_hid_desc)
  1811. {
  1812. if (unlikely(ret_hid_desc == NULL || full_conf == NULL))
  1813. return -EINVAL;
  1814. kdebug("to get hid_descriptor.");
  1815. // 判断接口index是否合理
  1816. if (interface_number >= ((struct usb_config_desc *)full_conf)->num_interfaces)
  1817. return -EINVAL;
  1818. uint32_t total_len = ((struct usb_config_desc *)full_conf)->total_len;
  1819. uint32_t pos = 0;
  1820. while (pos < total_len)
  1821. {
  1822. struct usb_hid_desc *ptr = (struct usb_hid_desc *)(full_conf + pos);
  1823. if (ptr->type != USB_DT_HID)
  1824. {
  1825. pos += ptr->len;
  1826. continue;
  1827. }
  1828. // 找到目标hid描述符
  1829. *ret_hid_desc = ptr;
  1830. kdebug("Found hid descriptor for port:%d, if:%d, report_desc_len=%d", port_id, interface_number,
  1831. ptr->report_desc_len);
  1832. return 0;
  1833. }
  1834. return -EINVAL;
  1835. }
  1836. /**
  1837. * @brief 发送get_hid_descriptor请求,将hid
  1838. *
  1839. * @param id 主机控制器号
  1840. * @param port_id 端口号
  1841. * @param interface_number 接口号
  1842. * @param ret_hid_report hid report要拷贝到的地址
  1843. * @param hid_report_len hid report的长度
  1844. * @return int 错误码
  1845. */
  1846. static int xhci_get_hid_report(int id, int port_id, int interface_number, void *ret_hid_report, uint32_t hid_report_len)
  1847. {
  1848. int retval = xhci_get_desc(id, port_id, ret_hid_report, USB_DT_HID_REPORT, 0, interface_number, hid_report_len);
  1849. if (unlikely(retval != 0))
  1850. kerror("xhci_get_hid_report failed: host_controller:%d, port:%d, interface %d", id, port_id, interface_number);
  1851. return retval;
  1852. }
  1853. /**
  1854. * @brief 初始化xhci控制器
  1855. *
  1856. * @param header 指定控制器的pci device头部
  1857. */
  1858. void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
  1859. {
  1860. if (xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS)
  1861. {
  1862. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  1863. return;
  1864. }
  1865. spin_lock(&xhci_controller_init_lock);
  1866. kinfo("Initializing xhci host controller: bus=%#02x, device=%#02x, func=%#02x, VendorID=%#04x, irq_line=%d, "
  1867. "irq_pin=%d",
  1868. dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, dev_hdr->header.Vendor_ID,
  1869. dev_hdr->Interrupt_Line, dev_hdr->Interrupt_PIN);
  1870. io_mfence();
  1871. int cid = xhci_hc_find_available_id();
  1872. if (cid < 0)
  1873. {
  1874. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  1875. goto failed_exceed_max;
  1876. }
  1877. memset(&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  1878. xhci_hc[cid].controller_id = cid;
  1879. xhci_hc[cid].pci_dev_hdr = dev_hdr;
  1880. io_mfence();
  1881. {
  1882. uint32_t tmp = pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4);
  1883. tmp |= 0x6;
  1884. // mem I/O access enable and bus master enable
  1885. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4, tmp);
  1886. }
  1887. io_mfence();
  1888. // 为当前控制器映射寄存器地址空间
  1889. xhci_hc[cid].vbase =
  1890. SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + 65536 * xhci_hc[cid].controller_id;
  1891. // kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf));
  1892. mm_map_phys_addr(xhci_hc[cid].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true);
  1893. io_mfence();
  1894. // 计算operational registers的地址
  1895. xhci_hc[cid].vbase_op = xhci_hc[cid].vbase + (xhci_read_cap_reg32(cid, XHCI_CAPS_CAPLENGTH) & 0xff);
  1896. io_mfence();
  1897. // 重置xhci控制器
  1898. FAIL_ON_TO(xhci_hc_reset(cid), failed);
  1899. io_mfence();
  1900. // 读取xhci控制寄存器
  1901. uint16_t iversion = *(uint16_t *)(xhci_hc[cid].vbase + XHCI_CAPS_HCIVERSION);
  1902. struct xhci_caps_HCCPARAMS1_reg_t hcc1;
  1903. struct xhci_caps_HCCPARAMS2_reg_t hcc2;
  1904. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  1905. struct xhci_caps_HCSPARAMS2_reg_t hcs2;
  1906. memcpy(&hcc1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS1), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  1907. memcpy(&hcc2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS2), sizeof(struct xhci_caps_HCCPARAMS2_reg_t));
  1908. memcpy(&hcs1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  1909. memcpy(&hcs2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t));
  1910. xhci_hc[cid].db_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_DBOFF) & (~0x3); // bits [1:0] reserved
  1911. io_mfence();
  1912. xhci_hc[cid].rts_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_RTSOFF) & (~0x1f); // bits [4:0] reserved.
  1913. io_mfence();
  1914. xhci_hc[cid].ext_caps_off = 1UL * (hcc1.xECP) * 4;
  1915. xhci_hc[cid].context_size = (hcc1.csz) ? 64 : 32;
  1916. if (iversion < 0x95)
  1917. kwarn("Unsupported/Unknowned xHCI controller version: %#06x. This may cause unexpected behavior.", iversion);
  1918. {
  1919. // Write to the FLADJ register incase the BIOS didn't
  1920. uint32_t tmp = pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x60);
  1921. tmp |= (0x20 << 8);
  1922. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x60, tmp);
  1923. }
  1924. // if it is a Panther Point device, make sure sockets are xHCI controlled.
  1925. if (((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0) & 0xffff) == 0x8086) &&
  1926. (((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0) >> 16) & 0xffff) ==
  1927. 0x1E31) &&
  1928. ((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 8) & 0xff) == 4))
  1929. {
  1930. kdebug("Is a Panther Point device");
  1931. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd8, 0xffffffff);
  1932. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd0, 0xffffffff);
  1933. }
  1934. io_mfence();
  1935. // 关闭legacy支持
  1936. FAIL_ON_TO(xhci_hc_stop_legacy(cid), failed);
  1937. io_mfence();
  1938. // 端口配对
  1939. FAIL_ON_TO(xhci_hc_pair_ports(cid), failed);
  1940. io_mfence();
  1941. // ========== 设置USB host controller =========
  1942. // 获取页面大小
  1943. xhci_hc[cid].page_size = (xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE) & 0xffff) << 12;
  1944. io_mfence();
  1945. // 获取设备上下文空间
  1946. xhci_hc[cid].dcbaap_vaddr = (uint64_t)kzalloc(2048, 0); // 分配2KB的设备上下文地址数组空间
  1947. io_mfence();
  1948. // kdebug("dcbaap_vaddr=%#018lx", xhci_hc[cid].dcbaap_vaddr);
  1949. if (unlikely(!xhci_is_aligned64(xhci_hc[cid].dcbaap_vaddr))) // 地址不是按照64byte对齐
  1950. {
  1951. kerror("dcbaap isn't 64 byte aligned.");
  1952. goto failed_free_dyn;
  1953. }
  1954. // 写入dcbaap
  1955. xhci_write_op_reg64(cid, XHCI_OPS_DCBAAP, virt_2_phys(xhci_hc[cid].dcbaap_vaddr));
  1956. io_mfence();
  1957. // 创建scratchpad buffer array
  1958. uint32_t max_scratchpad_buf = (((uint32_t)hcs2.max_scratchpad_buf_HI5) << 5) | hcs2.max_scratchpad_buf_LO5;
  1959. kdebug("max scratchpad buffer=%d", max_scratchpad_buf);
  1960. if (max_scratchpad_buf > 0)
  1961. {
  1962. xhci_hc[cid].scratchpad_buf_array_vaddr = (uint64_t)kzalloc(sizeof(uint64_t) * max_scratchpad_buf, 0);
  1963. __write8b(xhci_hc[cid].dcbaap_vaddr, virt_2_phys(xhci_hc[cid].scratchpad_buf_array_vaddr));
  1964. // 创建scratchpad buffers
  1965. for (int i = 0; i < max_scratchpad_buf; ++i)
  1966. {
  1967. uint64_t buf_vaddr = (uint64_t)kzalloc(xhci_hc[cid].page_size, 0);
  1968. __write8b(xhci_hc[cid].scratchpad_buf_array_vaddr, virt_2_phys(buf_vaddr));
  1969. }
  1970. }
  1971. // 创建command ring
  1972. xhci_hc[cid].cmd_ring_vaddr = xhci_create_ring(XHCI_CMND_RING_TRBS);
  1973. xhci_hc[cid].cmd_trb_vaddr = xhci_hc[cid].cmd_ring_vaddr;
  1974. if (unlikely(!xhci_is_aligned64(xhci_hc[cid].cmd_ring_vaddr))) // 地址不是按照64byte对齐
  1975. {
  1976. kerror("cmd ring isn't 64 byte aligned.");
  1977. goto failed_free_dyn;
  1978. }
  1979. // 设置初始cycle bit为1
  1980. xhci_hc[cid].cmd_trb_cycle = XHCI_TRB_CYCLE_ON;
  1981. io_mfence();
  1982. // 写入command ring控制寄存器
  1983. xhci_write_op_reg64(cid, XHCI_OPS_CRCR, virt_2_phys(xhci_hc[cid].cmd_ring_vaddr) | xhci_hc[cid].cmd_trb_cycle);
  1984. // 写入配置寄存器
  1985. uint32_t max_slots = hcs1.max_slots;
  1986. // kdebug("max slots = %d", max_slots);
  1987. io_mfence();
  1988. xhci_write_op_reg32(cid, XHCI_OPS_CONFIG, max_slots);
  1989. io_mfence();
  1990. // 写入设备通知控制寄存器
  1991. xhci_write_op_reg32(cid, XHCI_OPS_DNCTRL, (1 << 1)); // 目前只有N1被支持
  1992. io_mfence();
  1993. FAIL_ON_TO(xhci_hc_init_intr(cid), failed_free_dyn);
  1994. io_mfence();
  1995. ++xhci_ctrl_count;
  1996. io_mfence();
  1997. spin_unlock(&xhci_controller_init_lock);
  1998. io_mfence();
  1999. return;
  2000. failed_free_dyn:; // 释放动态申请的内存
  2001. if (xhci_hc[cid].dcbaap_vaddr)
  2002. kfree((void *)xhci_hc[cid].dcbaap_vaddr);
  2003. if (xhci_hc[cid].cmd_ring_vaddr)
  2004. kfree((void *)xhci_hc[cid].cmd_ring_vaddr);
  2005. if (xhci_hc[cid].event_ring_table_vaddr)
  2006. kfree((void *)xhci_hc[cid].event_ring_table_vaddr);
  2007. if (xhci_hc[cid].event_ring_vaddr)
  2008. kfree((void *)xhci_hc[cid].event_ring_vaddr);
  2009. failed:;
  2010. io_mfence();
  2011. // 取消地址映射
  2012. mm_unmap_addr(xhci_hc[cid].vbase, 65536);
  2013. io_mfence();
  2014. // 清空数组
  2015. memset((void *)&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  2016. failed_exceed_max:;
  2017. kerror("Failed to initialize controller: bus=%d, dev=%d, func=%d", dev_hdr->header.bus, dev_hdr->header.device,
  2018. dev_hdr->header.func);
  2019. spin_unlock(&xhci_controller_init_lock);
  2020. }