usbh_video.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_video.h"
  8. #undef USB_DBG_TAG
  9. #define USB_DBG_TAG "usbh_video"
  10. #include "usb_log.h"
  11. #define DEV_FORMAT "/dev/video%d"
  12. /* general descriptor field offsets */
  13. #define DESC_bLength 0 /** Length offset */
  14. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  15. #define DESC_bDescriptorSubType 2 /** Descriptor subtype offset */
  16. #define DESC_bNumFormats 3 /** Descriptor numformat offset */
  17. #define DESC_bNumFrameDescriptors 4 /** Descriptor numframe offset */
  18. #define DESC_bFormatIndex 3 /** Descriptor format index offset */
  19. #define DESC_bFrameIndex 3 /** Descriptor frame index offset */
  20. /* interface descriptor field offsets */
  21. #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
  22. #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
  23. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_video_buf[128];
  24. static const char *format_type[] = { "uncompressed", "mjpeg" };
  25. static struct usbh_video g_video_class[CONFIG_USBHOST_MAX_VIDEO_CLASS];
  26. static uint32_t g_devinuse = 0;
  27. static struct usbh_video *usbh_video_class_alloc(void)
  28. {
  29. uint8_t devno;
  30. for (devno = 0; devno < CONFIG_USBHOST_MAX_VIDEO_CLASS; devno++) {
  31. if ((g_devinuse & (1U << devno)) == 0) {
  32. g_devinuse |= (1U << devno);
  33. memset(&g_video_class[devno], 0, sizeof(struct usbh_video));
  34. g_video_class[devno].minor = devno;
  35. return &g_video_class[devno];
  36. }
  37. }
  38. return NULL;
  39. }
  40. static void usbh_video_class_free(struct usbh_video *video_class)
  41. {
  42. uint8_t devno = video_class->minor;
  43. if (devno < 32) {
  44. g_devinuse &= ~(1U << devno);
  45. }
  46. memset(video_class, 0, sizeof(struct usbh_video));
  47. }
  48. int usbh_video_get(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
  49. {
  50. struct usb_setup_packet *setup;
  51. int ret;
  52. uint8_t retry;
  53. if (!video_class || !video_class->hport) {
  54. return -USB_ERR_INVAL;
  55. }
  56. setup = video_class->hport->setup;
  57. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  58. setup->bRequest = request;
  59. setup->wValue = cs << 8;
  60. setup->wIndex = (entity_id << 8) | intf;
  61. setup->wLength = len;
  62. retry = 0;
  63. while (1) {
  64. ret = usbh_control_transfer(video_class->hport, setup, g_video_buf);
  65. if (ret > 0) {
  66. break;
  67. }
  68. retry++;
  69. if (retry == 3) {
  70. return ret;
  71. }
  72. }
  73. if (buf) {
  74. memcpy(buf, g_video_buf, len);
  75. }
  76. return ret;
  77. }
  78. int usbh_video_set(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
  79. {
  80. struct usb_setup_packet *setup;
  81. int ret;
  82. if (!video_class || !video_class->hport) {
  83. return -USB_ERR_INVAL;
  84. }
  85. setup = video_class->hport->setup;
  86. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  87. setup->bRequest = request;
  88. setup->wValue = cs << 8;
  89. setup->wIndex = (entity_id << 8) | intf;
  90. setup->wLength = len;
  91. memcpy(g_video_buf, buf, len);
  92. ret = usbh_control_transfer(video_class->hport, setup, g_video_buf);
  93. usb_osal_msleep(50);
  94. return ret;
  95. }
  96. int usbh_videostreaming_get_cur_probe(struct usbh_video *video_class)
  97. {
  98. return usbh_video_get(video_class, VIDEO_REQUEST_GET_CUR, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, (uint8_t *)&video_class->probe, 26);
  99. }
  100. int usbh_videostreaming_set_cur_probe(struct usbh_video *video_class, uint8_t formatindex, uint8_t frameindex)
  101. {
  102. video_class->probe.bFormatIndex = formatindex;
  103. video_class->probe.bFrameIndex = frameindex;
  104. video_class->probe.dwMaxPayloadTransferSize = 0;
  105. video_class->probe.dwFrameInterval = 333333;
  106. return usbh_video_set(video_class, VIDEO_REQUEST_SET_CUR, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, (uint8_t *)&video_class->probe, 26);
  107. }
  108. int usbh_videostreaming_set_cur_commit(struct usbh_video *video_class, uint8_t formatindex, uint8_t frameindex)
  109. {
  110. memcpy(&video_class->commit, &video_class->probe, sizeof(struct video_probe_and_commit_controls));
  111. video_class->commit.bFormatIndex = formatindex;
  112. video_class->commit.bFrameIndex = frameindex;
  113. video_class->commit.dwFrameInterval = 333333;
  114. return usbh_video_set(video_class, VIDEO_REQUEST_SET_CUR, video_class->data_intf, 0x00, VIDEO_VS_COMMIT_CONTROL, (uint8_t *)&video_class->commit, 26);
  115. }
  116. int usbh_video_open(struct usbh_video *video_class,
  117. uint8_t format_type,
  118. uint16_t wWidth,
  119. uint16_t wHeight,
  120. uint8_t altsetting)
  121. {
  122. struct usb_setup_packet *setup;
  123. struct usb_endpoint_descriptor *ep_desc;
  124. uint8_t mult;
  125. uint16_t mps;
  126. int ret;
  127. bool found = false;
  128. uint8_t formatidx = 0;
  129. uint8_t frameidx = 0;
  130. uint8_t step;
  131. if (!video_class || !video_class->hport) {
  132. return -USB_ERR_INVAL;
  133. }
  134. setup = video_class->hport->setup;
  135. if (video_class->is_opened) {
  136. return 0;
  137. }
  138. for (uint8_t i = 0; i < video_class->num_of_formats; i++) {
  139. if (format_type == video_class->format[i].format_type) {
  140. formatidx = i + 1;
  141. for (uint8_t j = 0; j < video_class->format[i].num_of_frames; j++) {
  142. if ((wWidth == video_class->format[i].frame[j].wWidth) &&
  143. (wHeight == video_class->format[i].frame[j].wHeight)) {
  144. frameidx = j + 1;
  145. found = true;
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. if (found == false) {
  152. return -USB_ERR_NODEV;
  153. }
  154. if (altsetting > (video_class->num_of_intf_altsettings - 1)) {
  155. return -USB_ERR_INVAL;
  156. }
  157. /* Open video step:
  158. * Get CUR request (probe)
  159. * Set CUR request (probe)
  160. * Get CUR request (probe)
  161. * Get MAX request (probe)
  162. * Get MIN request (probe)
  163. * Get CUR request (probe)
  164. * Set CUR request (commit)
  165. *
  166. */
  167. step = 0;
  168. ret = usbh_videostreaming_get_cur_probe(video_class);
  169. if (ret < 0) {
  170. goto errout;
  171. }
  172. step = 1;
  173. ret = usbh_videostreaming_set_cur_probe(video_class, formatidx, frameidx);
  174. if (ret < 0) {
  175. goto errout;
  176. }
  177. step = 2;
  178. ret = usbh_videostreaming_get_cur_probe(video_class);
  179. if (ret < 0) {
  180. goto errout;
  181. }
  182. step = 3;
  183. ret = usbh_video_get(video_class, VIDEO_REQUEST_GET_MAX, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, NULL, 26);
  184. if (ret < 0) {
  185. goto errout;
  186. }
  187. step = 4;
  188. ret = usbh_video_get(video_class, VIDEO_REQUEST_GET_MIN, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, NULL, 26);
  189. if (ret < 0) {
  190. goto errout;
  191. }
  192. step = 5;
  193. ret = usbh_videostreaming_set_cur_probe(video_class, formatidx, frameidx);
  194. if (ret < 0) {
  195. goto errout;
  196. }
  197. step = 6;
  198. ret = usbh_videostreaming_get_cur_probe(video_class);
  199. if (ret < 0) {
  200. goto errout;
  201. }
  202. step = 7;
  203. ret = usbh_videostreaming_set_cur_commit(video_class, formatidx, frameidx);
  204. if (ret < 0) {
  205. goto errout;
  206. }
  207. step = 8;
  208. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  209. setup->bRequest = USB_REQUEST_SET_INTERFACE;
  210. setup->wValue = altsetting;
  211. setup->wIndex = video_class->data_intf;
  212. setup->wLength = 0;
  213. ret = usbh_control_transfer(video_class->hport, setup, NULL);
  214. if (ret < 0) {
  215. goto errout;
  216. }
  217. ep_desc = &video_class->hport->config.intf[video_class->data_intf].altsetting[altsetting].ep[0].ep_desc;
  218. mult = (ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT;
  219. mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
  220. if (ep_desc->bEndpointAddress & 0x80) {
  221. video_class->isoin_mps = mps * (mult + 1);
  222. USBH_EP_INIT(video_class->isoin, ep_desc);
  223. } else {
  224. video_class->isoout_mps = mps * (mult + 1);
  225. USBH_EP_INIT(video_class->isoout, ep_desc);
  226. }
  227. USB_LOG_INFO("Open video and select formatidx:%u, frameidx:%u, altsetting:%u\r\n", formatidx, frameidx, altsetting);
  228. video_class->is_opened = true;
  229. video_class->current_format = format_type;
  230. return ret;
  231. errout:
  232. USB_LOG_ERR("Fail to open video in step %u\r\n", step);
  233. return ret;
  234. }
  235. int usbh_video_close(struct usbh_video *video_class)
  236. {
  237. struct usb_setup_packet *setup;
  238. int ret = 0;
  239. if (!video_class || !video_class->hport) {
  240. return -USB_ERR_INVAL;
  241. }
  242. setup = video_class->hport->setup;
  243. USB_LOG_INFO("Close video device\r\n");
  244. video_class->is_opened = false;
  245. if (video_class->isoin) {
  246. video_class->isoin = NULL;
  247. }
  248. if (video_class->isoout) {
  249. video_class->isoout = NULL;
  250. }
  251. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  252. setup->bRequest = USB_REQUEST_SET_INTERFACE;
  253. setup->wValue = 0;
  254. setup->wIndex = video_class->data_intf;
  255. setup->wLength = 0;
  256. ret = usbh_control_transfer(video_class->hport, setup, NULL);
  257. if (ret < 0) {
  258. return ret;
  259. }
  260. return ret;
  261. }
  262. void usbh_video_list_info(struct usbh_video *video_class)
  263. {
  264. struct usb_endpoint_descriptor *ep_desc;
  265. uint8_t mult;
  266. uint16_t mps;
  267. USB_LOG_INFO("============= Video device information ===================\r\n");
  268. USB_LOG_RAW("bcdVDC:%04x\r\n", video_class->bcdVDC);
  269. USB_LOG_RAW("Num of altsettings:%u\r\n", video_class->num_of_intf_altsettings);
  270. for (uint8_t i = 0; i < video_class->num_of_intf_altsettings; i++) {
  271. if (i == 0) {
  272. USB_LOG_RAW("Ingore altsetting 0\r\n");
  273. continue;
  274. }
  275. ep_desc = &video_class->hport->config.intf[video_class->data_intf].altsetting[i].ep[0].ep_desc;
  276. mult = (ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT;
  277. mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
  278. USB_LOG_RAW("Altsetting:%u, Ep=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n",
  279. i,
  280. ep_desc->bEndpointAddress,
  281. ep_desc->bmAttributes,
  282. mps,
  283. ep_desc->bInterval,
  284. mult);
  285. }
  286. USB_LOG_RAW("bNumFormats:%u\r\n", video_class->num_of_formats);
  287. for (uint8_t i = 0; i < video_class->num_of_formats; i++) {
  288. USB_LOG_RAW(" FormatIndex:%u\r\n", i + 1);
  289. USB_LOG_RAW(" FormatType:%s\r\n", format_type[video_class->format[i].format_type]);
  290. USB_LOG_RAW(" bNumFrames:%u\r\n", video_class->format[i].num_of_frames);
  291. USB_LOG_RAW(" Resolution:\r\n");
  292. for (uint8_t j = 0; j < video_class->format[i].num_of_frames; j++) {
  293. USB_LOG_RAW(" FrameIndex:%u\r\n", j + 1);
  294. USB_LOG_RAW(" wWidth: %d, wHeight: %d\r\n",
  295. video_class->format[i].frame[j].wWidth,
  296. video_class->format[i].frame[j].wHeight);
  297. }
  298. }
  299. USB_LOG_INFO("============= Video device information ===================\r\n");
  300. }
  301. static int usbh_video_ctrl_connect(struct usbh_hubport *hport, uint8_t intf)
  302. {
  303. int ret;
  304. uint8_t cur_iface = 0xff;
  305. // uint8_t cur_alt_setting = 0xff;
  306. uint8_t frame_index = 0xff;
  307. uint8_t format_index = 0xff;
  308. uint8_t num_of_frames = 0xff;
  309. uint8_t *p;
  310. struct usbh_video *video_class = usbh_video_class_alloc();
  311. if (video_class == NULL) {
  312. USB_LOG_ERR("Fail to alloc video_class\r\n");
  313. return -USB_ERR_NOMEM;
  314. }
  315. video_class->hport = hport;
  316. video_class->ctrl_intf = intf;
  317. video_class->data_intf = intf + 1;
  318. video_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
  319. hport->config.intf[intf].priv = video_class;
  320. ret = usbh_video_close(video_class);
  321. if (ret < 0) {
  322. USB_LOG_ERR("Fail to close video device\r\n");
  323. return ret;
  324. }
  325. p = hport->raw_config_desc;
  326. while (p[DESC_bLength]) {
  327. switch (p[DESC_bDescriptorType]) {
  328. case USB_DESCRIPTOR_TYPE_INTERFACE:
  329. cur_iface = p[INTF_DESC_bInterfaceNumber];
  330. //cur_alt_setting = p[INTF_DESC_bAlternateSetting];
  331. break;
  332. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  333. //ep_desc = (struct usb_endpoint_descriptor *)p;
  334. break;
  335. case VIDEO_CS_INTERFACE_DESCRIPTOR_TYPE:
  336. if (cur_iface == video_class->ctrl_intf) {
  337. switch (p[DESC_bDescriptorSubType]) {
  338. case VIDEO_VC_HEADER_DESCRIPTOR_SUBTYPE:
  339. video_class->bcdVDC = ((uint16_t)p[4] << 8) | (uint16_t)p[3];
  340. break;
  341. case VIDEO_VC_INPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
  342. break;
  343. case VIDEO_VC_OUTPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
  344. break;
  345. case VIDEO_VC_PROCESSING_UNIT_DESCRIPTOR_SUBTYPE:
  346. break;
  347. default:
  348. break;
  349. }
  350. } else if (cur_iface == video_class->data_intf) {
  351. switch (p[DESC_bDescriptorSubType]) {
  352. case VIDEO_VS_INPUT_HEADER_DESCRIPTOR_SUBTYPE:
  353. video_class->num_of_formats = p[DESC_bNumFormats];
  354. break;
  355. case VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_SUBTYPE:
  356. format_index = p[DESC_bFormatIndex];
  357. num_of_frames = p[DESC_bNumFrameDescriptors];
  358. video_class->format[format_index - 1].num_of_frames = num_of_frames;
  359. video_class->format[format_index - 1].format_type = USBH_VIDEO_FORMAT_UNCOMPRESSED;
  360. break;
  361. case VIDEO_VS_FORMAT_MJPEG_DESCRIPTOR_SUBTYPE:
  362. format_index = p[DESC_bFormatIndex];
  363. num_of_frames = p[DESC_bNumFrameDescriptors];
  364. video_class->format[format_index - 1].num_of_frames = num_of_frames;
  365. video_class->format[format_index - 1].format_type = USBH_VIDEO_FORMAT_MJPEG;
  366. break;
  367. case VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_SUBTYPE:
  368. frame_index = p[DESC_bFrameIndex];
  369. video_class->format[format_index - 1].frame[frame_index - 1].wWidth = ((struct video_cs_if_vs_frame_uncompressed_descriptor *)p)->wWidth;
  370. video_class->format[format_index - 1].frame[frame_index - 1].wHeight = ((struct video_cs_if_vs_frame_uncompressed_descriptor *)p)->wHeight;
  371. break;
  372. case VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_SUBTYPE:
  373. frame_index = p[DESC_bFrameIndex];
  374. video_class->format[format_index - 1].frame[frame_index - 1].wWidth = ((struct video_cs_if_vs_frame_mjpeg_descriptor *)p)->wWidth;
  375. video_class->format[format_index - 1].frame[frame_index - 1].wHeight = ((struct video_cs_if_vs_frame_mjpeg_descriptor *)p)->wHeight;
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. break;
  382. default:
  383. break;
  384. }
  385. /* skip to next descriptor */
  386. p += p[DESC_bLength];
  387. }
  388. usbh_video_list_info(video_class);
  389. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, video_class->minor);
  390. USB_LOG_INFO("Register Video Class:%s\r\n", hport->config.intf[intf].devname);
  391. usbh_video_run(video_class);
  392. return ret;
  393. }
  394. static int usbh_video_ctrl_disconnect(struct usbh_hubport *hport, uint8_t intf)
  395. {
  396. int ret = 0;
  397. struct usbh_video *video_class = (struct usbh_video *)hport->config.intf[intf].priv;
  398. if (video_class) {
  399. if (video_class->isoin) {
  400. }
  401. if (video_class->isoout) {
  402. }
  403. if (hport->config.intf[intf].devname[0] != '\0') {
  404. USB_LOG_INFO("Unregister Video Class:%s\r\n", hport->config.intf[intf].devname);
  405. usbh_video_stop(video_class);
  406. }
  407. usbh_video_class_free(video_class);
  408. }
  409. return ret;
  410. }
  411. static int usbh_video_streaming_connect(struct usbh_hubport *hport, uint8_t intf)
  412. {
  413. (void)hport;
  414. (void)intf;
  415. return 0;
  416. }
  417. static int usbh_video_streaming_disconnect(struct usbh_hubport *hport, uint8_t intf)
  418. {
  419. (void)hport;
  420. (void)intf;
  421. return 0;
  422. }
  423. __WEAK void usbh_video_run(struct usbh_video *video_class)
  424. {
  425. (void)video_class;
  426. }
  427. __WEAK void usbh_video_stop(struct usbh_video *video_class)
  428. {
  429. (void)video_class;
  430. }
  431. const struct usbh_class_driver video_ctrl_class_driver = {
  432. .driver_name = "video_ctrl",
  433. .connect = usbh_video_ctrl_connect,
  434. .disconnect = usbh_video_ctrl_disconnect
  435. };
  436. const struct usbh_class_driver video_streaming_class_driver = {
  437. .driver_name = "video_streaming",
  438. .connect = usbh_video_streaming_connect,
  439. .disconnect = usbh_video_streaming_disconnect
  440. };
  441. CLASS_INFO_DEFINE const struct usbh_class_info video_ctrl_class_info = {
  442. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  443. .bInterfaceClass = USB_DEVICE_CLASS_VIDEO,
  444. .bInterfaceSubClass = VIDEO_SC_VIDEOCONTROL,
  445. .bInterfaceProtocol = VIDEO_PC_PROTOCOL_UNDEFINED,
  446. .id_table = NULL,
  447. .class_driver = &video_ctrl_class_driver
  448. };
  449. CLASS_INFO_DEFINE const struct usbh_class_info video_streaming_class_info = {
  450. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  451. .bInterfaceClass = USB_DEVICE_CLASS_VIDEO,
  452. .bInterfaceSubClass = VIDEO_SC_VIDEOSTREAMING,
  453. .bInterfaceProtocol = VIDEO_PC_PROTOCOL_UNDEFINED,
  454. .id_table = NULL,
  455. .class_driver = &video_streaming_class_driver
  456. };