hub.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
  4. *
  5. * hub.c - virtual hub handling
  6. *
  7. * Copyright 2017 IBM Corporation
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/delay.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/prefetch.h>
  20. #include <linux/clk.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/regmap.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/bcd.h>
  27. #include <linux/version.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include "vhub.h"
  31. /* usb 2.0 hub device descriptor
  32. *
  33. * A few things we may want to improve here:
  34. *
  35. * - We may need to indicate TT support
  36. * - We may need a device qualifier descriptor
  37. * as devices can pretend to be usb1 or 2
  38. * - Make vid/did overridable
  39. * - make it look like usb1 if usb1 mode forced
  40. */
  41. #define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR)
  42. #define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL)
  43. enum {
  44. AST_VHUB_STR_INDEX_MAX = 4,
  45. AST_VHUB_STR_MANUF = 3,
  46. AST_VHUB_STR_PRODUCT = 2,
  47. AST_VHUB_STR_SERIAL = 1,
  48. };
  49. static const struct usb_device_descriptor ast_vhub_dev_desc = {
  50. .bLength = USB_DT_DEVICE_SIZE,
  51. .bDescriptorType = USB_DT_DEVICE,
  52. .bcdUSB = cpu_to_le16(0x0200),
  53. .bDeviceClass = USB_CLASS_HUB,
  54. .bDeviceSubClass = 0,
  55. .bDeviceProtocol = 1,
  56. .bMaxPacketSize0 = 64,
  57. .idVendor = cpu_to_le16(0x1d6b),
  58. .idProduct = cpu_to_le16(0x0107),
  59. .bcdDevice = cpu_to_le16(0x0100),
  60. .iManufacturer = AST_VHUB_STR_MANUF,
  61. .iProduct = AST_VHUB_STR_PRODUCT,
  62. .iSerialNumber = AST_VHUB_STR_SERIAL,
  63. .bNumConfigurations = 1,
  64. };
  65. static const struct usb_qualifier_descriptor ast_vhub_qual_desc = {
  66. .bLength = 0xA,
  67. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  68. .bcdUSB = cpu_to_le16(0x0200),
  69. .bDeviceClass = USB_CLASS_HUB,
  70. .bDeviceSubClass = 0,
  71. .bDeviceProtocol = 0,
  72. .bMaxPacketSize0 = 64,
  73. .bNumConfigurations = 1,
  74. .bRESERVED = 0,
  75. };
  76. /*
  77. * Configuration descriptor: same comments as above
  78. * regarding handling USB1 mode.
  79. */
  80. /*
  81. * We don't use sizeof() as Linux definition of
  82. * struct usb_endpoint_descriptor contains 2
  83. * extra bytes
  84. */
  85. #define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \
  86. USB_DT_INTERFACE_SIZE + \
  87. USB_DT_ENDPOINT_SIZE)
  88. static const struct ast_vhub_full_cdesc ast_vhub_conf_desc = {
  89. .cfg = {
  90. .bLength = USB_DT_CONFIG_SIZE,
  91. .bDescriptorType = USB_DT_CONFIG,
  92. .wTotalLength = cpu_to_le16(AST_VHUB_CONF_DESC_SIZE),
  93. .bNumInterfaces = 1,
  94. .bConfigurationValue = 1,
  95. .iConfiguration = 0,
  96. .bmAttributes = USB_CONFIG_ATT_ONE |
  97. USB_CONFIG_ATT_SELFPOWER |
  98. USB_CONFIG_ATT_WAKEUP,
  99. .bMaxPower = 0,
  100. },
  101. .intf = {
  102. .bLength = USB_DT_INTERFACE_SIZE,
  103. .bDescriptorType = USB_DT_INTERFACE,
  104. .bInterfaceNumber = 0,
  105. .bAlternateSetting = 0,
  106. .bNumEndpoints = 1,
  107. .bInterfaceClass = USB_CLASS_HUB,
  108. .bInterfaceSubClass = 0,
  109. .bInterfaceProtocol = 0,
  110. .iInterface = 0,
  111. },
  112. .ep = {
  113. .bLength = USB_DT_ENDPOINT_SIZE,
  114. .bDescriptorType = USB_DT_ENDPOINT,
  115. .bEndpointAddress = 0x81,
  116. .bmAttributes = USB_ENDPOINT_XFER_INT,
  117. .wMaxPacketSize = cpu_to_le16(1),
  118. .bInterval = 0x0c,
  119. },
  120. };
  121. #define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2)
  122. static const struct usb_hub_descriptor ast_vhub_hub_desc = {
  123. .bDescLength = AST_VHUB_HUB_DESC_SIZE,
  124. .bDescriptorType = USB_DT_HUB,
  125. .bNbrPorts = AST_VHUB_NUM_PORTS,
  126. .wHubCharacteristics = cpu_to_le16(HUB_CHAR_NO_LPSM),
  127. .bPwrOn2PwrGood = 10,
  128. .bHubContrCurrent = 0,
  129. .u.hs.DeviceRemovable[0] = 0,
  130. .u.hs.DeviceRemovable[1] = 0xff,
  131. };
  132. /*
  133. * These strings converted to UTF-16 must be smaller than
  134. * our EP0 buffer.
  135. */
  136. static const struct usb_string ast_vhub_str_array[] = {
  137. {
  138. .id = AST_VHUB_STR_SERIAL,
  139. .s = "00000000"
  140. },
  141. {
  142. .id = AST_VHUB_STR_PRODUCT,
  143. .s = "USB Virtual Hub"
  144. },
  145. {
  146. .id = AST_VHUB_STR_MANUF,
  147. .s = "Aspeed"
  148. },
  149. { }
  150. };
  151. static const struct usb_gadget_strings ast_vhub_strings = {
  152. .language = 0x0409,
  153. .strings = (struct usb_string *)ast_vhub_str_array
  154. };
  155. static int ast_vhub_hub_dev_status(struct ast_vhub_ep *ep,
  156. u16 wIndex, u16 wValue)
  157. {
  158. u8 st0;
  159. EPDBG(ep, "GET_STATUS(dev)\n");
  160. /*
  161. * Mark it as self-powered, I doubt the BMC is powered off
  162. * the USB bus ...
  163. */
  164. st0 = 1 << USB_DEVICE_SELF_POWERED;
  165. /*
  166. * Need to double check how remote wakeup actually works
  167. * on that chip and what triggers it.
  168. */
  169. if (ep->vhub->wakeup_en)
  170. st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
  171. return ast_vhub_simple_reply(ep, st0, 0);
  172. }
  173. static int ast_vhub_hub_ep_status(struct ast_vhub_ep *ep,
  174. u16 wIndex, u16 wValue)
  175. {
  176. int ep_num;
  177. u8 st0 = 0;
  178. ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
  179. EPDBG(ep, "GET_STATUS(ep%d)\n", ep_num);
  180. /* On the hub we have only EP 0 and 1 */
  181. if (ep_num == 1) {
  182. if (ep->vhub->ep1_stalled)
  183. st0 |= 1 << USB_ENDPOINT_HALT;
  184. } else if (ep_num != 0)
  185. return std_req_stall;
  186. return ast_vhub_simple_reply(ep, st0, 0);
  187. }
  188. static int ast_vhub_hub_dev_feature(struct ast_vhub_ep *ep,
  189. u16 wIndex, u16 wValue,
  190. bool is_set)
  191. {
  192. u32 val;
  193. EPDBG(ep, "%s_FEATURE(dev val=%02x)\n",
  194. is_set ? "SET" : "CLEAR", wValue);
  195. if (wValue == USB_DEVICE_REMOTE_WAKEUP) {
  196. ep->vhub->wakeup_en = is_set;
  197. EPDBG(ep, "Hub remote wakeup %s\n",
  198. is_set ? "enabled" : "disabled");
  199. return std_req_complete;
  200. }
  201. if (wValue == USB_DEVICE_TEST_MODE) {
  202. val = readl(ep->vhub->regs + AST_VHUB_CTRL);
  203. val &= ~GENMASK(10, 8);
  204. val |= VHUB_CTRL_SET_TEST_MODE((wIndex >> 8) & 0x7);
  205. writel(val, ep->vhub->regs + AST_VHUB_CTRL);
  206. return std_req_complete;
  207. }
  208. return std_req_stall;
  209. }
  210. static int ast_vhub_hub_ep_feature(struct ast_vhub_ep *ep,
  211. u16 wIndex, u16 wValue,
  212. bool is_set)
  213. {
  214. int ep_num;
  215. u32 reg;
  216. ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
  217. EPDBG(ep, "%s_FEATURE(ep%d val=%02x)\n",
  218. is_set ? "SET" : "CLEAR", ep_num, wValue);
  219. if (ep_num > 1)
  220. return std_req_stall;
  221. if (wValue != USB_ENDPOINT_HALT)
  222. return std_req_stall;
  223. if (ep_num == 0)
  224. return std_req_complete;
  225. EPDBG(ep, "%s stall on EP 1\n",
  226. is_set ? "setting" : "clearing");
  227. ep->vhub->ep1_stalled = is_set;
  228. reg = readl(ep->vhub->regs + AST_VHUB_EP1_CTRL);
  229. if (is_set) {
  230. reg |= VHUB_EP1_CTRL_STALL;
  231. } else {
  232. reg &= ~VHUB_EP1_CTRL_STALL;
  233. reg |= VHUB_EP1_CTRL_RESET_TOGGLE;
  234. }
  235. writel(reg, ep->vhub->regs + AST_VHUB_EP1_CTRL);
  236. return std_req_complete;
  237. }
  238. static int ast_vhub_rep_desc(struct ast_vhub_ep *ep,
  239. u8 desc_type, u16 len)
  240. {
  241. size_t dsize;
  242. struct ast_vhub *vhub = ep->vhub;
  243. EPDBG(ep, "GET_DESCRIPTOR(type:%d)\n", desc_type);
  244. /*
  245. * Copy first to EP buffer and send from there, so
  246. * we can do some in-place patching if needed. We know
  247. * the EP buffer is big enough but ensure that doesn't
  248. * change. We do that now rather than later after we
  249. * have checked sizes etc... to avoid a gcc bug where
  250. * it thinks len is constant and barfs about read
  251. * overflows in memcpy.
  252. */
  253. switch(desc_type) {
  254. case USB_DT_DEVICE:
  255. dsize = USB_DT_DEVICE_SIZE;
  256. memcpy(ep->buf, &vhub->vhub_dev_desc, dsize);
  257. BUILD_BUG_ON(dsize > sizeof(vhub->vhub_dev_desc));
  258. BUILD_BUG_ON(USB_DT_DEVICE_SIZE >= AST_VHUB_EP0_MAX_PACKET);
  259. break;
  260. case USB_DT_OTHER_SPEED_CONFIG:
  261. case USB_DT_CONFIG:
  262. dsize = AST_VHUB_CONF_DESC_SIZE;
  263. memcpy(ep->buf, &vhub->vhub_conf_desc, dsize);
  264. ((u8 *)ep->buf)[1] = desc_type;
  265. BUILD_BUG_ON(dsize > sizeof(vhub->vhub_conf_desc));
  266. BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
  267. break;
  268. case USB_DT_HUB:
  269. dsize = AST_VHUB_HUB_DESC_SIZE;
  270. memcpy(ep->buf, &vhub->vhub_hub_desc, dsize);
  271. BUILD_BUG_ON(dsize > sizeof(vhub->vhub_hub_desc));
  272. BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
  273. break;
  274. case USB_DT_DEVICE_QUALIFIER:
  275. dsize = sizeof(vhub->vhub_qual_desc);
  276. memcpy(ep->buf, &vhub->vhub_qual_desc, dsize);
  277. break;
  278. default:
  279. return std_req_stall;
  280. }
  281. /* Crop requested length */
  282. if (len > dsize)
  283. len = dsize;
  284. /* Shoot it from the EP buffer */
  285. return ast_vhub_reply(ep, NULL, len);
  286. }
  287. static struct usb_gadget_strings*
  288. ast_vhub_str_of_container(struct usb_gadget_string_container *container)
  289. {
  290. return (struct usb_gadget_strings *)container->stash;
  291. }
  292. static int ast_vhub_collect_languages(struct ast_vhub *vhub, void *buf,
  293. size_t size)
  294. {
  295. int rc, hdr_len, nlangs, max_langs;
  296. struct usb_gadget_strings *lang_str;
  297. struct usb_gadget_string_container *container;
  298. struct usb_string_descriptor *sdesc = buf;
  299. nlangs = 0;
  300. hdr_len = sizeof(struct usb_descriptor_header);
  301. max_langs = (size - hdr_len) / sizeof(sdesc->wData[0]);
  302. list_for_each_entry(container, &vhub->vhub_str_desc, list) {
  303. if (nlangs >= max_langs)
  304. break;
  305. lang_str = ast_vhub_str_of_container(container);
  306. sdesc->wData[nlangs++] = cpu_to_le16(lang_str->language);
  307. }
  308. rc = hdr_len + nlangs * sizeof(sdesc->wData[0]);
  309. sdesc->bLength = rc;
  310. sdesc->bDescriptorType = USB_DT_STRING;
  311. return rc;
  312. }
  313. static struct usb_gadget_strings *ast_vhub_lookup_string(struct ast_vhub *vhub,
  314. u16 lang_id)
  315. {
  316. struct usb_gadget_strings *lang_str;
  317. struct usb_gadget_string_container *container;
  318. list_for_each_entry(container, &vhub->vhub_str_desc, list) {
  319. lang_str = ast_vhub_str_of_container(container);
  320. if (lang_str->language == lang_id)
  321. return lang_str;
  322. }
  323. return NULL;
  324. }
  325. static int ast_vhub_rep_string(struct ast_vhub_ep *ep,
  326. u8 string_id, u16 lang_id,
  327. u16 len)
  328. {
  329. int rc;
  330. u8 buf[256];
  331. struct ast_vhub *vhub = ep->vhub;
  332. struct usb_gadget_strings *lang_str;
  333. if (string_id == 0) {
  334. rc = ast_vhub_collect_languages(vhub, buf, sizeof(buf));
  335. } else {
  336. lang_str = ast_vhub_lookup_string(vhub, lang_id);
  337. if (!lang_str)
  338. return std_req_stall;
  339. rc = usb_gadget_get_string(lang_str, string_id, buf);
  340. }
  341. if (rc < 0 || rc >= AST_VHUB_EP0_MAX_PACKET)
  342. return std_req_stall;
  343. /* Shoot it from the EP buffer */
  344. memcpy(ep->buf, buf, rc);
  345. return ast_vhub_reply(ep, NULL, min_t(u16, rc, len));
  346. }
  347. enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
  348. struct usb_ctrlrequest *crq)
  349. {
  350. struct ast_vhub *vhub = ep->vhub;
  351. u16 wValue, wIndex, wLength;
  352. wValue = le16_to_cpu(crq->wValue);
  353. wIndex = le16_to_cpu(crq->wIndex);
  354. wLength = le16_to_cpu(crq->wLength);
  355. /* First packet, grab speed */
  356. if (vhub->speed == USB_SPEED_UNKNOWN) {
  357. u32 ustat = readl(vhub->regs + AST_VHUB_USBSTS);
  358. if (ustat & VHUB_USBSTS_HISPEED)
  359. vhub->speed = USB_SPEED_HIGH;
  360. else
  361. vhub->speed = USB_SPEED_FULL;
  362. UDCDBG(vhub, "USB status=%08x speed=%s\n", ustat,
  363. vhub->speed == USB_SPEED_HIGH ? "high" : "full");
  364. }
  365. switch ((crq->bRequestType << 8) | crq->bRequest) {
  366. /* SET_ADDRESS */
  367. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  368. EPDBG(ep, "SET_ADDRESS: Got address %x\n", wValue);
  369. writel(wValue, vhub->regs + AST_VHUB_CONF);
  370. return std_req_complete;
  371. /* GET_STATUS */
  372. case DeviceRequest | USB_REQ_GET_STATUS:
  373. return ast_vhub_hub_dev_status(ep, wIndex, wValue);
  374. case InterfaceRequest | USB_REQ_GET_STATUS:
  375. return ast_vhub_simple_reply(ep, 0, 0);
  376. case EndpointRequest | USB_REQ_GET_STATUS:
  377. return ast_vhub_hub_ep_status(ep, wIndex, wValue);
  378. /* SET/CLEAR_FEATURE */
  379. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  380. return ast_vhub_hub_dev_feature(ep, wIndex, wValue, true);
  381. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  382. return ast_vhub_hub_dev_feature(ep, wIndex, wValue, false);
  383. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  384. return ast_vhub_hub_ep_feature(ep, wIndex, wValue, true);
  385. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  386. return ast_vhub_hub_ep_feature(ep, wIndex, wValue, false);
  387. /* GET/SET_CONFIGURATION */
  388. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  389. return ast_vhub_simple_reply(ep, 1);
  390. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  391. if (wValue != 1)
  392. return std_req_stall;
  393. return std_req_complete;
  394. /* GET_DESCRIPTOR */
  395. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  396. switch (wValue >> 8) {
  397. case USB_DT_DEVICE:
  398. case USB_DT_CONFIG:
  399. case USB_DT_DEVICE_QUALIFIER:
  400. case USB_DT_OTHER_SPEED_CONFIG:
  401. return ast_vhub_rep_desc(ep, wValue >> 8,
  402. wLength);
  403. case USB_DT_STRING:
  404. return ast_vhub_rep_string(ep, wValue & 0xff,
  405. wIndex, wLength);
  406. }
  407. return std_req_stall;
  408. /* GET/SET_INTERFACE */
  409. case DeviceRequest | USB_REQ_GET_INTERFACE:
  410. return ast_vhub_simple_reply(ep, 0);
  411. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  412. if (wValue != 0 || wIndex != 0)
  413. return std_req_stall;
  414. return std_req_complete;
  415. }
  416. return std_req_stall;
  417. }
  418. static void ast_vhub_update_hub_ep1(struct ast_vhub *vhub,
  419. unsigned int port)
  420. {
  421. /* Update HW EP1 response */
  422. u32 reg = readl(vhub->regs + AST_VHUB_EP1_STS_CHG);
  423. u32 pmask = (1 << (port + 1));
  424. if (vhub->ports[port].change)
  425. reg |= pmask;
  426. else
  427. reg &= ~pmask;
  428. writel(reg, vhub->regs + AST_VHUB_EP1_STS_CHG);
  429. }
  430. static void ast_vhub_change_port_stat(struct ast_vhub *vhub,
  431. unsigned int port,
  432. u16 clr_flags,
  433. u16 set_flags,
  434. bool set_c)
  435. {
  436. struct ast_vhub_port *p = &vhub->ports[port];
  437. u16 prev;
  438. /* Update port status */
  439. prev = p->status;
  440. p->status = (prev & ~clr_flags) | set_flags;
  441. DDBG(&p->dev, "port %d status %04x -> %04x (C=%d)\n",
  442. port + 1, prev, p->status, set_c);
  443. /* Update change bits if needed */
  444. if (set_c) {
  445. u16 chg = p->status ^ prev;
  446. /* Only these are relevant for change */
  447. chg &= USB_PORT_STAT_C_CONNECTION |
  448. USB_PORT_STAT_C_ENABLE |
  449. USB_PORT_STAT_C_SUSPEND |
  450. USB_PORT_STAT_C_OVERCURRENT |
  451. USB_PORT_STAT_C_RESET |
  452. USB_PORT_STAT_C_L1;
  453. /*
  454. * We only set USB_PORT_STAT_C_ENABLE if we are disabling
  455. * the port as per USB spec, otherwise MacOS gets upset
  456. */
  457. if (p->status & USB_PORT_STAT_ENABLE)
  458. chg &= ~USB_PORT_STAT_C_ENABLE;
  459. p->change = chg;
  460. ast_vhub_update_hub_ep1(vhub, port);
  461. }
  462. }
  463. static void ast_vhub_send_host_wakeup(struct ast_vhub *vhub)
  464. {
  465. u32 reg = readl(vhub->regs + AST_VHUB_CTRL);
  466. UDCDBG(vhub, "Waking up host !\n");
  467. reg |= VHUB_CTRL_MANUAL_REMOTE_WAKEUP;
  468. writel(reg, vhub->regs + AST_VHUB_CTRL);
  469. }
  470. void ast_vhub_device_connect(struct ast_vhub *vhub,
  471. unsigned int port, bool on)
  472. {
  473. if (on)
  474. ast_vhub_change_port_stat(vhub, port, 0,
  475. USB_PORT_STAT_CONNECTION, true);
  476. else
  477. ast_vhub_change_port_stat(vhub, port,
  478. USB_PORT_STAT_CONNECTION |
  479. USB_PORT_STAT_ENABLE,
  480. 0, true);
  481. /*
  482. * If the hub is set to wakup the host on connection events
  483. * then send a wakeup.
  484. */
  485. if (vhub->wakeup_en)
  486. ast_vhub_send_host_wakeup(vhub);
  487. }
  488. static void ast_vhub_wake_work(struct work_struct *work)
  489. {
  490. struct ast_vhub *vhub = container_of(work,
  491. struct ast_vhub,
  492. wake_work);
  493. unsigned long flags;
  494. unsigned int i;
  495. /*
  496. * Wake all sleeping ports. If a port is suspended by
  497. * the host suspend (without explicit state suspend),
  498. * we let the normal host wake path deal with it later.
  499. */
  500. spin_lock_irqsave(&vhub->lock, flags);
  501. for (i = 0; i < vhub->max_ports; i++) {
  502. struct ast_vhub_port *p = &vhub->ports[i];
  503. if (!(p->status & USB_PORT_STAT_SUSPEND))
  504. continue;
  505. ast_vhub_change_port_stat(vhub, i,
  506. USB_PORT_STAT_SUSPEND,
  507. 0, true);
  508. ast_vhub_dev_resume(&p->dev);
  509. }
  510. ast_vhub_send_host_wakeup(vhub);
  511. spin_unlock_irqrestore(&vhub->lock, flags);
  512. }
  513. void ast_vhub_hub_wake_all(struct ast_vhub *vhub)
  514. {
  515. /*
  516. * A device is trying to wake the world, because this
  517. * can recurse into the device, we break the call chain
  518. * using a work queue
  519. */
  520. schedule_work(&vhub->wake_work);
  521. }
  522. static void ast_vhub_port_reset(struct ast_vhub *vhub, u8 port)
  523. {
  524. struct ast_vhub_port *p = &vhub->ports[port];
  525. u16 set, clr, speed;
  526. /* First mark disabled */
  527. ast_vhub_change_port_stat(vhub, port,
  528. USB_PORT_STAT_ENABLE |
  529. USB_PORT_STAT_SUSPEND,
  530. USB_PORT_STAT_RESET,
  531. false);
  532. if (!p->dev.driver)
  533. return;
  534. /*
  535. * This will either "start" the port or reset the
  536. * device if already started...
  537. */
  538. ast_vhub_dev_reset(&p->dev);
  539. /* Grab the right speed */
  540. speed = p->dev.driver->max_speed;
  541. if (speed == USB_SPEED_UNKNOWN || speed > vhub->speed)
  542. speed = vhub->speed;
  543. switch (speed) {
  544. case USB_SPEED_LOW:
  545. set = USB_PORT_STAT_LOW_SPEED;
  546. clr = USB_PORT_STAT_HIGH_SPEED;
  547. break;
  548. case USB_SPEED_FULL:
  549. set = 0;
  550. clr = USB_PORT_STAT_LOW_SPEED |
  551. USB_PORT_STAT_HIGH_SPEED;
  552. break;
  553. case USB_SPEED_HIGH:
  554. set = USB_PORT_STAT_HIGH_SPEED;
  555. clr = USB_PORT_STAT_LOW_SPEED;
  556. break;
  557. default:
  558. UDCDBG(vhub, "Unsupported speed %d when"
  559. " connecting device\n",
  560. speed);
  561. return;
  562. }
  563. clr |= USB_PORT_STAT_RESET;
  564. set |= USB_PORT_STAT_ENABLE;
  565. /* This should ideally be delayed ... */
  566. ast_vhub_change_port_stat(vhub, port, clr, set, true);
  567. }
  568. static enum std_req_rc ast_vhub_set_port_feature(struct ast_vhub_ep *ep,
  569. u8 port, u16 feat)
  570. {
  571. struct ast_vhub *vhub = ep->vhub;
  572. struct ast_vhub_port *p;
  573. if (port == 0 || port > vhub->max_ports)
  574. return std_req_stall;
  575. port--;
  576. p = &vhub->ports[port];
  577. switch(feat) {
  578. case USB_PORT_FEAT_SUSPEND:
  579. if (!(p->status & USB_PORT_STAT_ENABLE))
  580. return std_req_complete;
  581. ast_vhub_change_port_stat(vhub, port,
  582. 0, USB_PORT_STAT_SUSPEND,
  583. false);
  584. ast_vhub_dev_suspend(&p->dev);
  585. return std_req_complete;
  586. case USB_PORT_FEAT_RESET:
  587. EPDBG(ep, "Port reset !\n");
  588. ast_vhub_port_reset(vhub, port);
  589. return std_req_complete;
  590. case USB_PORT_FEAT_POWER:
  591. /*
  592. * On Power-on, we mark the connected flag changed,
  593. * if there's a connected device, some hosts will
  594. * otherwise fail to detect it.
  595. */
  596. if (p->status & USB_PORT_STAT_CONNECTION) {
  597. p->change |= USB_PORT_STAT_C_CONNECTION;
  598. ast_vhub_update_hub_ep1(vhub, port);
  599. }
  600. return std_req_complete;
  601. case USB_PORT_FEAT_TEST:
  602. case USB_PORT_FEAT_INDICATOR:
  603. /* We don't do anything with these */
  604. return std_req_complete;
  605. }
  606. return std_req_stall;
  607. }
  608. static enum std_req_rc ast_vhub_clr_port_feature(struct ast_vhub_ep *ep,
  609. u8 port, u16 feat)
  610. {
  611. struct ast_vhub *vhub = ep->vhub;
  612. struct ast_vhub_port *p;
  613. if (port == 0 || port > vhub->max_ports)
  614. return std_req_stall;
  615. port--;
  616. p = &vhub->ports[port];
  617. switch(feat) {
  618. case USB_PORT_FEAT_ENABLE:
  619. ast_vhub_change_port_stat(vhub, port,
  620. USB_PORT_STAT_ENABLE |
  621. USB_PORT_STAT_SUSPEND, 0,
  622. false);
  623. ast_vhub_dev_suspend(&p->dev);
  624. return std_req_complete;
  625. case USB_PORT_FEAT_SUSPEND:
  626. if (!(p->status & USB_PORT_STAT_SUSPEND))
  627. return std_req_complete;
  628. ast_vhub_change_port_stat(vhub, port,
  629. USB_PORT_STAT_SUSPEND, 0,
  630. false);
  631. ast_vhub_dev_resume(&p->dev);
  632. return std_req_complete;
  633. case USB_PORT_FEAT_POWER:
  634. /* We don't do power control */
  635. return std_req_complete;
  636. case USB_PORT_FEAT_INDICATOR:
  637. /* We don't have indicators */
  638. return std_req_complete;
  639. case USB_PORT_FEAT_C_CONNECTION:
  640. case USB_PORT_FEAT_C_ENABLE:
  641. case USB_PORT_FEAT_C_SUSPEND:
  642. case USB_PORT_FEAT_C_OVER_CURRENT:
  643. case USB_PORT_FEAT_C_RESET:
  644. /* Clear state-change feature */
  645. p->change &= ~(1u << (feat - 16));
  646. ast_vhub_update_hub_ep1(vhub, port);
  647. return std_req_complete;
  648. }
  649. return std_req_stall;
  650. }
  651. static enum std_req_rc ast_vhub_get_port_stat(struct ast_vhub_ep *ep,
  652. u8 port)
  653. {
  654. struct ast_vhub *vhub = ep->vhub;
  655. u16 stat, chg;
  656. if (port == 0 || port > vhub->max_ports)
  657. return std_req_stall;
  658. port--;
  659. stat = vhub->ports[port].status;
  660. chg = vhub->ports[port].change;
  661. /* We always have power */
  662. stat |= USB_PORT_STAT_POWER;
  663. EPDBG(ep, " port status=%04x change=%04x\n", stat, chg);
  664. return ast_vhub_simple_reply(ep,
  665. stat & 0xff,
  666. stat >> 8,
  667. chg & 0xff,
  668. chg >> 8);
  669. }
  670. enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep,
  671. struct usb_ctrlrequest *crq)
  672. {
  673. u16 wValue, wIndex, wLength;
  674. wValue = le16_to_cpu(crq->wValue);
  675. wIndex = le16_to_cpu(crq->wIndex);
  676. wLength = le16_to_cpu(crq->wLength);
  677. switch ((crq->bRequestType << 8) | crq->bRequest) {
  678. case GetHubStatus:
  679. EPDBG(ep, "GetHubStatus\n");
  680. return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
  681. case GetPortStatus:
  682. EPDBG(ep, "GetPortStatus(%d)\n", wIndex & 0xff);
  683. return ast_vhub_get_port_stat(ep, wIndex & 0xf);
  684. case GetHubDescriptor:
  685. if (wValue != (USB_DT_HUB << 8))
  686. return std_req_stall;
  687. EPDBG(ep, "GetHubDescriptor(%d)\n", wIndex & 0xff);
  688. return ast_vhub_rep_desc(ep, USB_DT_HUB, wLength);
  689. case SetHubFeature:
  690. case ClearHubFeature:
  691. EPDBG(ep, "Get/SetHubFeature(%d)\n", wValue);
  692. /* No feature, just complete the requests */
  693. if (wValue == C_HUB_LOCAL_POWER ||
  694. wValue == C_HUB_OVER_CURRENT)
  695. return std_req_complete;
  696. return std_req_stall;
  697. case SetPortFeature:
  698. EPDBG(ep, "SetPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
  699. return ast_vhub_set_port_feature(ep, wIndex & 0xf, wValue);
  700. case ClearPortFeature:
  701. EPDBG(ep, "ClearPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
  702. return ast_vhub_clr_port_feature(ep, wIndex & 0xf, wValue);
  703. case ClearTTBuffer:
  704. case ResetTT:
  705. case StopTT:
  706. return std_req_complete;
  707. case GetTTState:
  708. return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
  709. default:
  710. EPDBG(ep, "Unknown class request\n");
  711. }
  712. return std_req_stall;
  713. }
  714. void ast_vhub_hub_suspend(struct ast_vhub *vhub)
  715. {
  716. unsigned int i;
  717. UDCDBG(vhub, "USB bus suspend\n");
  718. if (vhub->suspended)
  719. return;
  720. vhub->suspended = true;
  721. /*
  722. * Forward to unsuspended ports without changing
  723. * their connection status.
  724. */
  725. for (i = 0; i < vhub->max_ports; i++) {
  726. struct ast_vhub_port *p = &vhub->ports[i];
  727. if (!(p->status & USB_PORT_STAT_SUSPEND))
  728. ast_vhub_dev_suspend(&p->dev);
  729. }
  730. }
  731. void ast_vhub_hub_resume(struct ast_vhub *vhub)
  732. {
  733. unsigned int i;
  734. UDCDBG(vhub, "USB bus resume\n");
  735. if (!vhub->suspended)
  736. return;
  737. vhub->suspended = false;
  738. /*
  739. * Forward to unsuspended ports without changing
  740. * their connection status.
  741. */
  742. for (i = 0; i < vhub->max_ports; i++) {
  743. struct ast_vhub_port *p = &vhub->ports[i];
  744. if (!(p->status & USB_PORT_STAT_SUSPEND))
  745. ast_vhub_dev_resume(&p->dev);
  746. }
  747. }
  748. void ast_vhub_hub_reset(struct ast_vhub *vhub)
  749. {
  750. unsigned int i;
  751. UDCDBG(vhub, "USB bus reset\n");
  752. /*
  753. * Is the speed known ? If not we don't care, we aren't
  754. * initialized yet and ports haven't been enabled.
  755. */
  756. if (vhub->speed == USB_SPEED_UNKNOWN)
  757. return;
  758. /* We aren't suspended anymore obviously */
  759. vhub->suspended = false;
  760. /* No speed set */
  761. vhub->speed = USB_SPEED_UNKNOWN;
  762. /* Wakeup not enabled anymore */
  763. vhub->wakeup_en = false;
  764. /*
  765. * Clear all port status, disable gadgets and "suspend"
  766. * them. They will be woken up by a port reset.
  767. */
  768. for (i = 0; i < vhub->max_ports; i++) {
  769. struct ast_vhub_port *p = &vhub->ports[i];
  770. /* Only keep the connected flag */
  771. p->status &= USB_PORT_STAT_CONNECTION;
  772. p->change = 0;
  773. /* Suspend the gadget if any */
  774. ast_vhub_dev_suspend(&p->dev);
  775. }
  776. /* Cleanup HW */
  777. writel(0, vhub->regs + AST_VHUB_CONF);
  778. writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
  779. writel(VHUB_EP1_CTRL_RESET_TOGGLE |
  780. VHUB_EP1_CTRL_ENABLE,
  781. vhub->regs + AST_VHUB_EP1_CTRL);
  782. writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
  783. }
  784. static void ast_vhub_of_parse_dev_desc(struct ast_vhub *vhub,
  785. const struct device_node *vhub_np)
  786. {
  787. u16 id;
  788. u32 data;
  789. if (!of_property_read_u32(vhub_np, "vhub-vendor-id", &data)) {
  790. id = (u16)data;
  791. vhub->vhub_dev_desc.idVendor = cpu_to_le16(id);
  792. }
  793. if (!of_property_read_u32(vhub_np, "vhub-product-id", &data)) {
  794. id = (u16)data;
  795. vhub->vhub_dev_desc.idProduct = cpu_to_le16(id);
  796. }
  797. if (!of_property_read_u32(vhub_np, "vhub-device-revision", &data)) {
  798. id = (u16)data;
  799. vhub->vhub_dev_desc.bcdDevice = cpu_to_le16(id);
  800. }
  801. }
  802. static void ast_vhub_fixup_usb1_dev_desc(struct ast_vhub *vhub)
  803. {
  804. vhub->vhub_dev_desc.bcdUSB = cpu_to_le16(0x0100);
  805. vhub->vhub_dev_desc.bDeviceProtocol = 0;
  806. }
  807. static struct usb_gadget_string_container*
  808. ast_vhub_str_container_alloc(struct ast_vhub *vhub)
  809. {
  810. unsigned int size;
  811. struct usb_string *str_array;
  812. struct usb_gadget_strings *lang_str;
  813. struct usb_gadget_string_container *container;
  814. size = sizeof(*container);
  815. size += sizeof(struct usb_gadget_strings);
  816. size += sizeof(struct usb_string) * AST_VHUB_STR_INDEX_MAX;
  817. container = devm_kzalloc(&vhub->pdev->dev, size, GFP_KERNEL);
  818. if (!container)
  819. return ERR_PTR(-ENOMEM);
  820. lang_str = ast_vhub_str_of_container(container);
  821. str_array = (struct usb_string *)(lang_str + 1);
  822. lang_str->strings = str_array;
  823. return container;
  824. }
  825. static void ast_vhub_str_deep_copy(struct usb_gadget_strings *dest,
  826. const struct usb_gadget_strings *src)
  827. {
  828. struct usb_string *src_array = src->strings;
  829. struct usb_string *dest_array = dest->strings;
  830. dest->language = src->language;
  831. if (src_array && dest_array) {
  832. do {
  833. *dest_array = *src_array;
  834. dest_array++;
  835. src_array++;
  836. } while (src_array->s);
  837. }
  838. }
  839. static int ast_vhub_str_alloc_add(struct ast_vhub *vhub,
  840. const struct usb_gadget_strings *src_str)
  841. {
  842. struct usb_gadget_strings *dest_str;
  843. struct usb_gadget_string_container *container;
  844. container = ast_vhub_str_container_alloc(vhub);
  845. if (IS_ERR(container))
  846. return PTR_ERR(container);
  847. dest_str = ast_vhub_str_of_container(container);
  848. ast_vhub_str_deep_copy(dest_str, src_str);
  849. list_add_tail(&container->list, &vhub->vhub_str_desc);
  850. return 0;
  851. }
  852. static const struct {
  853. const char *name;
  854. u8 id;
  855. } str_id_map[] = {
  856. {"manufacturer", AST_VHUB_STR_MANUF},
  857. {"product", AST_VHUB_STR_PRODUCT},
  858. {"serial-number", AST_VHUB_STR_SERIAL},
  859. {},
  860. };
  861. static int ast_vhub_of_parse_str_desc(struct ast_vhub *vhub,
  862. const struct device_node *desc_np)
  863. {
  864. u32 langid;
  865. int ret = 0;
  866. int i, offset;
  867. const char *str;
  868. struct device_node *child;
  869. struct usb_string str_array[AST_VHUB_STR_INDEX_MAX];
  870. struct usb_gadget_strings lang_str = {
  871. .strings = (struct usb_string *)str_array,
  872. };
  873. for_each_child_of_node(desc_np, child) {
  874. if (of_property_read_u32(child, "reg", &langid))
  875. continue; /* no language identifier specified */
  876. if (!usb_validate_langid(langid))
  877. continue; /* invalid language identifier */
  878. lang_str.language = langid;
  879. for (i = offset = 0; str_id_map[i].name; i++) {
  880. str = of_get_property(child, str_id_map[i].name, NULL);
  881. if (str) {
  882. str_array[offset].s = str;
  883. str_array[offset].id = str_id_map[i].id;
  884. offset++;
  885. }
  886. }
  887. str_array[offset].id = 0;
  888. str_array[offset].s = NULL;
  889. ret = ast_vhub_str_alloc_add(vhub, &lang_str);
  890. if (ret) {
  891. of_node_put(child);
  892. break;
  893. }
  894. }
  895. return ret;
  896. }
  897. static int ast_vhub_init_desc(struct ast_vhub *vhub)
  898. {
  899. int ret;
  900. struct device_node *desc_np;
  901. const struct device_node *vhub_np = vhub->pdev->dev.of_node;
  902. /* Initialize vhub Device Descriptor. */
  903. memcpy(&vhub->vhub_dev_desc, &ast_vhub_dev_desc,
  904. sizeof(vhub->vhub_dev_desc));
  905. ast_vhub_of_parse_dev_desc(vhub, vhub_np);
  906. if (vhub->force_usb1)
  907. ast_vhub_fixup_usb1_dev_desc(vhub);
  908. /* Initialize vhub Configuration Descriptor. */
  909. memcpy(&vhub->vhub_conf_desc, &ast_vhub_conf_desc,
  910. sizeof(vhub->vhub_conf_desc));
  911. /* Initialize vhub Hub Descriptor. */
  912. memcpy(&vhub->vhub_hub_desc, &ast_vhub_hub_desc,
  913. sizeof(vhub->vhub_hub_desc));
  914. vhub->vhub_hub_desc.bNbrPorts = vhub->max_ports;
  915. /* Initialize vhub String Descriptors. */
  916. INIT_LIST_HEAD(&vhub->vhub_str_desc);
  917. desc_np = of_get_child_by_name(vhub_np, "vhub-strings");
  918. if (desc_np) {
  919. ret = ast_vhub_of_parse_str_desc(vhub, desc_np);
  920. of_node_put(desc_np);
  921. }
  922. else
  923. ret = ast_vhub_str_alloc_add(vhub, &ast_vhub_strings);
  924. /* Initialize vhub Qualifier Descriptor. */
  925. memcpy(&vhub->vhub_qual_desc, &ast_vhub_qual_desc,
  926. sizeof(vhub->vhub_qual_desc));
  927. return ret;
  928. }
  929. int ast_vhub_init_hub(struct ast_vhub *vhub)
  930. {
  931. vhub->speed = USB_SPEED_UNKNOWN;
  932. INIT_WORK(&vhub->wake_work, ast_vhub_wake_work);
  933. return ast_vhub_init_desc(vhub);
  934. }