uvc_driver.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. /*
  2. * uvc_driver.c -- USB Video Class driver
  3. *
  4. * Copyright (C) 2005-2009
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. /*
  14. * This driver aims to support video input and ouput devices compliant with the
  15. * 'USB Video Class' specification.
  16. *
  17. * The driver doesn't support the deprecated v4l1 interface. It implements the
  18. * mmap capture method only, and doesn't do any image format conversion in
  19. * software. If your user-space application doesn't support YUYV or MJPEG, fix
  20. * it :-). Please note that the MJPEG data have been stripped from their
  21. * Huffman tables (DHT marker), you will need to add it back if your JPEG
  22. * codec can't handle MJPEG data.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/module.h>
  27. #include <linux/usb.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/wait.h>
  31. #include <asm/atomic.h>
  32. #include <asm/unaligned.h>
  33. #include <media/v4l2-common.h>
  34. #include "uvcvideo.h"
  35. #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
  36. #define DRIVER_DESC "USB Video Class driver"
  37. #ifndef DRIVER_VERSION
  38. #define DRIVER_VERSION "v0.1.0"
  39. #endif
  40. unsigned int uvc_no_drop_param;
  41. static unsigned int uvc_quirks_param;
  42. unsigned int uvc_trace_param;
  43. unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
  44. /* ------------------------------------------------------------------------
  45. * Video formats
  46. */
  47. static struct uvc_format_desc uvc_fmts[] = {
  48. {
  49. .name = "YUV 4:2:2 (YUYV)",
  50. .guid = UVC_GUID_FORMAT_YUY2,
  51. .fcc = V4L2_PIX_FMT_YUYV,
  52. },
  53. {
  54. .name = "YUV 4:2:0 (NV12)",
  55. .guid = UVC_GUID_FORMAT_NV12,
  56. .fcc = V4L2_PIX_FMT_NV12,
  57. },
  58. {
  59. .name = "MJPEG",
  60. .guid = UVC_GUID_FORMAT_MJPEG,
  61. .fcc = V4L2_PIX_FMT_MJPEG,
  62. },
  63. {
  64. .name = "YVU 4:2:0 (YV12)",
  65. .guid = UVC_GUID_FORMAT_YV12,
  66. .fcc = V4L2_PIX_FMT_YVU420,
  67. },
  68. {
  69. .name = "YUV 4:2:0 (I420)",
  70. .guid = UVC_GUID_FORMAT_I420,
  71. .fcc = V4L2_PIX_FMT_YUV420,
  72. },
  73. {
  74. .name = "YUV 4:2:2 (UYVY)",
  75. .guid = UVC_GUID_FORMAT_UYVY,
  76. .fcc = V4L2_PIX_FMT_UYVY,
  77. },
  78. {
  79. .name = "Greyscale",
  80. .guid = UVC_GUID_FORMAT_Y800,
  81. .fcc = V4L2_PIX_FMT_GREY,
  82. },
  83. {
  84. .name = "RGB Bayer",
  85. .guid = UVC_GUID_FORMAT_BY8,
  86. .fcc = V4L2_PIX_FMT_SBGGR8,
  87. },
  88. };
  89. /* ------------------------------------------------------------------------
  90. * Utility functions
  91. */
  92. struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
  93. __u8 epaddr)
  94. {
  95. struct usb_host_endpoint *ep;
  96. unsigned int i;
  97. for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
  98. ep = &alts->endpoint[i];
  99. if (ep->desc.bEndpointAddress == epaddr)
  100. return ep;
  101. }
  102. return NULL;
  103. }
  104. static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
  105. {
  106. unsigned int len = ARRAY_SIZE(uvc_fmts);
  107. unsigned int i;
  108. for (i = 0; i < len; ++i) {
  109. if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
  110. return &uvc_fmts[i];
  111. }
  112. return NULL;
  113. }
  114. static __u32 uvc_colorspace(const __u8 primaries)
  115. {
  116. static const __u8 colorprimaries[] = {
  117. 0,
  118. V4L2_COLORSPACE_SRGB,
  119. V4L2_COLORSPACE_470_SYSTEM_M,
  120. V4L2_COLORSPACE_470_SYSTEM_BG,
  121. V4L2_COLORSPACE_SMPTE170M,
  122. V4L2_COLORSPACE_SMPTE240M,
  123. };
  124. if (primaries < ARRAY_SIZE(colorprimaries))
  125. return colorprimaries[primaries];
  126. return 0;
  127. }
  128. /* Simplify a fraction using a simple continued fraction decomposition. The
  129. * idea here is to convert fractions such as 333333/10000000 to 1/30 using
  130. * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
  131. * arbitrary parameters to remove non-significative terms from the simple
  132. * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
  133. * respectively seems to give nice results.
  134. */
  135. void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
  136. unsigned int n_terms, unsigned int threshold)
  137. {
  138. uint32_t *an;
  139. uint32_t x, y, r;
  140. unsigned int i, n;
  141. an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
  142. if (an == NULL)
  143. return;
  144. /* Convert the fraction to a simple continued fraction. See
  145. * http://mathforum.org/dr.math/faq/faq.fractions.html
  146. * Stop if the current term is bigger than or equal to the given
  147. * threshold.
  148. */
  149. x = *numerator;
  150. y = *denominator;
  151. for (n = 0; n < n_terms && y != 0; ++n) {
  152. an[n] = x / y;
  153. if (an[n] >= threshold) {
  154. if (n < 2)
  155. n++;
  156. break;
  157. }
  158. r = x - an[n] * y;
  159. x = y;
  160. y = r;
  161. }
  162. /* Expand the simple continued fraction back to an integer fraction. */
  163. x = 0;
  164. y = 1;
  165. for (i = n; i > 0; --i) {
  166. r = y;
  167. y = an[i-1] * y + x;
  168. x = r;
  169. }
  170. *numerator = y;
  171. *denominator = x;
  172. kfree(an);
  173. }
  174. /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
  175. * to compute numerator / denominator * 10000000 using 32 bit fixed point
  176. * arithmetic only.
  177. */
  178. uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
  179. {
  180. uint32_t multiplier;
  181. /* Saturate the result if the operation would overflow. */
  182. if (denominator == 0 ||
  183. numerator/denominator >= ((uint32_t)-1)/10000000)
  184. return (uint32_t)-1;
  185. /* Divide both the denominator and the multiplier by two until
  186. * numerator * multiplier doesn't overflow. If anyone knows a better
  187. * algorithm please let me know.
  188. */
  189. multiplier = 10000000;
  190. while (numerator > ((uint32_t)-1)/multiplier) {
  191. multiplier /= 2;
  192. denominator /= 2;
  193. }
  194. return denominator ? numerator * multiplier / denominator : 0;
  195. }
  196. /* ------------------------------------------------------------------------
  197. * Terminal and unit management
  198. */
  199. static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
  200. {
  201. struct uvc_entity *entity;
  202. list_for_each_entry(entity, &dev->entities, list) {
  203. if (entity->id == id)
  204. return entity;
  205. }
  206. return NULL;
  207. }
  208. static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
  209. int id, struct uvc_entity *entity)
  210. {
  211. unsigned int i;
  212. if (entity == NULL)
  213. entity = list_entry(&dev->entities, struct uvc_entity, list);
  214. list_for_each_entry_continue(entity, &dev->entities, list) {
  215. switch (UVC_ENTITY_TYPE(entity)) {
  216. case UVC_TT_STREAMING:
  217. if (entity->output.bSourceID == id)
  218. return entity;
  219. break;
  220. case UVC_VC_PROCESSING_UNIT:
  221. if (entity->processing.bSourceID == id)
  222. return entity;
  223. break;
  224. case UVC_VC_SELECTOR_UNIT:
  225. for (i = 0; i < entity->selector.bNrInPins; ++i)
  226. if (entity->selector.baSourceID[i] == id)
  227. return entity;
  228. break;
  229. case UVC_VC_EXTENSION_UNIT:
  230. for (i = 0; i < entity->extension.bNrInPins; ++i)
  231. if (entity->extension.baSourceID[i] == id)
  232. return entity;
  233. break;
  234. }
  235. }
  236. return NULL;
  237. }
  238. static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
  239. {
  240. struct uvc_streaming *stream;
  241. list_for_each_entry(stream, &dev->streams, list) {
  242. if (stream->header.bTerminalLink == id)
  243. return stream;
  244. }
  245. return NULL;
  246. }
  247. /* ------------------------------------------------------------------------
  248. * Descriptors parsing
  249. */
  250. static int uvc_parse_format(struct uvc_device *dev,
  251. struct uvc_streaming *streaming, struct uvc_format *format,
  252. __u32 **intervals, unsigned char *buffer, int buflen)
  253. {
  254. struct usb_interface *intf = streaming->intf;
  255. struct usb_host_interface *alts = intf->cur_altsetting;
  256. struct uvc_format_desc *fmtdesc;
  257. struct uvc_frame *frame;
  258. const unsigned char *start = buffer;
  259. unsigned int interval;
  260. unsigned int i, n;
  261. __u8 ftype;
  262. format->type = buffer[2];
  263. format->index = buffer[3];
  264. switch (buffer[2]) {
  265. case UVC_VS_FORMAT_UNCOMPRESSED:
  266. case UVC_VS_FORMAT_FRAME_BASED:
  267. n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
  268. if (buflen < n) {
  269. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  270. "interface %d FORMAT error\n",
  271. dev->udev->devnum,
  272. alts->desc.bInterfaceNumber);
  273. return -EINVAL;
  274. }
  275. /* Find the format descriptor from its GUID. */
  276. fmtdesc = uvc_format_by_guid(&buffer[5]);
  277. if (fmtdesc != NULL) {
  278. strlcpy(format->name, fmtdesc->name,
  279. sizeof format->name);
  280. format->fcc = fmtdesc->fcc;
  281. } else {
  282. uvc_printk(KERN_INFO, "Unknown video format "
  283. UVC_GUID_FORMAT "\n",
  284. UVC_GUID_ARGS(&buffer[5]));
  285. snprintf(format->name, sizeof format->name,
  286. UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
  287. format->fcc = 0;
  288. }
  289. format->bpp = buffer[21];
  290. if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
  291. ftype = UVC_VS_FRAME_UNCOMPRESSED;
  292. } else {
  293. ftype = UVC_VS_FRAME_FRAME_BASED;
  294. if (buffer[27])
  295. format->flags = UVC_FMT_FLAG_COMPRESSED;
  296. }
  297. break;
  298. case UVC_VS_FORMAT_MJPEG:
  299. if (buflen < 11) {
  300. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  301. "interface %d FORMAT error\n",
  302. dev->udev->devnum,
  303. alts->desc.bInterfaceNumber);
  304. return -EINVAL;
  305. }
  306. strlcpy(format->name, "MJPEG", sizeof format->name);
  307. format->fcc = V4L2_PIX_FMT_MJPEG;
  308. format->flags = UVC_FMT_FLAG_COMPRESSED;
  309. format->bpp = 0;
  310. ftype = UVC_VS_FRAME_MJPEG;
  311. break;
  312. case UVC_VS_FORMAT_DV:
  313. if (buflen < 9) {
  314. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  315. "interface %d FORMAT error\n",
  316. dev->udev->devnum,
  317. alts->desc.bInterfaceNumber);
  318. return -EINVAL;
  319. }
  320. switch (buffer[8] & 0x7f) {
  321. case 0:
  322. strlcpy(format->name, "SD-DV", sizeof format->name);
  323. break;
  324. case 1:
  325. strlcpy(format->name, "SDL-DV", sizeof format->name);
  326. break;
  327. case 2:
  328. strlcpy(format->name, "HD-DV", sizeof format->name);
  329. break;
  330. default:
  331. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  332. "interface %d: unknown DV format %u\n",
  333. dev->udev->devnum,
  334. alts->desc.bInterfaceNumber, buffer[8]);
  335. return -EINVAL;
  336. }
  337. strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
  338. sizeof format->name);
  339. format->fcc = V4L2_PIX_FMT_DV;
  340. format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
  341. format->bpp = 0;
  342. ftype = 0;
  343. /* Create a dummy frame descriptor. */
  344. frame = &format->frame[0];
  345. memset(&format->frame[0], 0, sizeof format->frame[0]);
  346. frame->bFrameIntervalType = 1;
  347. frame->dwDefaultFrameInterval = 1;
  348. frame->dwFrameInterval = *intervals;
  349. *(*intervals)++ = 1;
  350. format->nframes = 1;
  351. break;
  352. case UVC_VS_FORMAT_MPEG2TS:
  353. case UVC_VS_FORMAT_STREAM_BASED:
  354. /* Not supported yet. */
  355. default:
  356. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  357. "interface %d unsupported format %u\n",
  358. dev->udev->devnum, alts->desc.bInterfaceNumber,
  359. buffer[2]);
  360. return -EINVAL;
  361. }
  362. uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
  363. buflen -= buffer[0];
  364. buffer += buffer[0];
  365. /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
  366. * based formats have frame descriptors.
  367. */
  368. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  369. buffer[2] == ftype) {
  370. frame = &format->frame[format->nframes];
  371. if (ftype != UVC_VS_FRAME_FRAME_BASED)
  372. n = buflen > 25 ? buffer[25] : 0;
  373. else
  374. n = buflen > 21 ? buffer[21] : 0;
  375. n = n ? n : 3;
  376. if (buflen < 26 + 4*n) {
  377. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  378. "interface %d FRAME error\n", dev->udev->devnum,
  379. alts->desc.bInterfaceNumber);
  380. return -EINVAL;
  381. }
  382. frame->bFrameIndex = buffer[3];
  383. frame->bmCapabilities = buffer[4];
  384. frame->wWidth = get_unaligned_le16(&buffer[5]);
  385. frame->wHeight = get_unaligned_le16(&buffer[7]);
  386. frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
  387. frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
  388. if (ftype != UVC_VS_FRAME_FRAME_BASED) {
  389. frame->dwMaxVideoFrameBufferSize =
  390. get_unaligned_le32(&buffer[17]);
  391. frame->dwDefaultFrameInterval =
  392. get_unaligned_le32(&buffer[21]);
  393. frame->bFrameIntervalType = buffer[25];
  394. } else {
  395. frame->dwMaxVideoFrameBufferSize = 0;
  396. frame->dwDefaultFrameInterval =
  397. get_unaligned_le32(&buffer[17]);
  398. frame->bFrameIntervalType = buffer[21];
  399. }
  400. frame->dwFrameInterval = *intervals;
  401. /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
  402. * completely. Observed behaviours range from setting the
  403. * value to 1.1x the actual frame size to hardwiring the
  404. * 16 low bits to 0. This results in a higher than necessary
  405. * memory usage as well as a wrong image size information. For
  406. * uncompressed formats this can be fixed by computing the
  407. * value from the frame size.
  408. */
  409. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
  410. frame->dwMaxVideoFrameBufferSize = format->bpp
  411. * frame->wWidth * frame->wHeight / 8;
  412. /* Some bogus devices report dwMinFrameInterval equal to
  413. * dwMaxFrameInterval and have dwFrameIntervalStep set to
  414. * zero. Setting all null intervals to 1 fixes the problem and
  415. * some other divisions by zero that could happen.
  416. */
  417. for (i = 0; i < n; ++i) {
  418. interval = get_unaligned_le32(&buffer[26+4*i]);
  419. *(*intervals)++ = interval ? interval : 1;
  420. }
  421. /* Make sure that the default frame interval stays between
  422. * the boundaries.
  423. */
  424. n -= frame->bFrameIntervalType ? 1 : 2;
  425. frame->dwDefaultFrameInterval =
  426. min(frame->dwFrameInterval[n],
  427. max(frame->dwFrameInterval[0],
  428. frame->dwDefaultFrameInterval));
  429. uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
  430. frame->wWidth, frame->wHeight,
  431. 10000000/frame->dwDefaultFrameInterval,
  432. (100000000/frame->dwDefaultFrameInterval)%10);
  433. format->nframes++;
  434. buflen -= buffer[0];
  435. buffer += buffer[0];
  436. }
  437. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  438. buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
  439. buflen -= buffer[0];
  440. buffer += buffer[0];
  441. }
  442. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  443. buffer[2] == UVC_VS_COLORFORMAT) {
  444. if (buflen < 6) {
  445. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  446. "interface %d COLORFORMAT error\n",
  447. dev->udev->devnum,
  448. alts->desc.bInterfaceNumber);
  449. return -EINVAL;
  450. }
  451. format->colorspace = uvc_colorspace(buffer[3]);
  452. buflen -= buffer[0];
  453. buffer += buffer[0];
  454. }
  455. return buffer - start;
  456. }
  457. static int uvc_parse_streaming(struct uvc_device *dev,
  458. struct usb_interface *intf)
  459. {
  460. struct uvc_streaming *streaming = NULL;
  461. struct uvc_format *format;
  462. struct uvc_frame *frame;
  463. struct usb_host_interface *alts = &intf->altsetting[0];
  464. unsigned char *_buffer, *buffer = alts->extra;
  465. int _buflen, buflen = alts->extralen;
  466. unsigned int nformats = 0, nframes = 0, nintervals = 0;
  467. unsigned int size, i, n, p;
  468. __u32 *interval;
  469. __u16 psize;
  470. int ret = -EINVAL;
  471. if (intf->cur_altsetting->desc.bInterfaceSubClass
  472. != UVC_SC_VIDEOSTREAMING) {
  473. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
  474. "video streaming interface\n", dev->udev->devnum,
  475. intf->altsetting[0].desc.bInterfaceNumber);
  476. return -EINVAL;
  477. }
  478. if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
  479. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
  480. "claimed\n", dev->udev->devnum,
  481. intf->altsetting[0].desc.bInterfaceNumber);
  482. return -EINVAL;
  483. }
  484. streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
  485. if (streaming == NULL) {
  486. usb_driver_release_interface(&uvc_driver.driver, intf);
  487. return -EINVAL;
  488. }
  489. mutex_init(&streaming->mutex);
  490. streaming->dev = dev;
  491. streaming->intf = usb_get_intf(intf);
  492. streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  493. /* The Pico iMage webcam has its class-specific interface descriptors
  494. * after the endpoint descriptors.
  495. */
  496. if (buflen == 0) {
  497. for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
  498. struct usb_host_endpoint *ep = &alts->endpoint[i];
  499. if (ep->extralen == 0)
  500. continue;
  501. if (ep->extralen > 2 &&
  502. ep->extra[1] == USB_DT_CS_INTERFACE) {
  503. uvc_trace(UVC_TRACE_DESCR, "trying extra data "
  504. "from endpoint %u.\n", i);
  505. buffer = alts->endpoint[i].extra;
  506. buflen = alts->endpoint[i].extralen;
  507. break;
  508. }
  509. }
  510. }
  511. /* Skip the standard interface descriptors. */
  512. while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
  513. buflen -= buffer[0];
  514. buffer += buffer[0];
  515. }
  516. if (buflen <= 2) {
  517. uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
  518. "interface descriptors found.\n");
  519. goto error;
  520. }
  521. /* Parse the header descriptor. */
  522. switch (buffer[2]) {
  523. case UVC_VS_OUTPUT_HEADER:
  524. streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  525. size = 9;
  526. break;
  527. case UVC_VS_INPUT_HEADER:
  528. streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  529. size = 13;
  530. break;
  531. default:
  532. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  533. "%d HEADER descriptor not found.\n", dev->udev->devnum,
  534. alts->desc.bInterfaceNumber);
  535. goto error;
  536. }
  537. p = buflen >= 4 ? buffer[3] : 0;
  538. n = buflen >= size ? buffer[size-1] : 0;
  539. if (buflen < size + p*n) {
  540. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  541. "interface %d HEADER descriptor is invalid.\n",
  542. dev->udev->devnum, alts->desc.bInterfaceNumber);
  543. goto error;
  544. }
  545. streaming->header.bNumFormats = p;
  546. streaming->header.bEndpointAddress = buffer[6];
  547. if (buffer[2] == UVC_VS_INPUT_HEADER) {
  548. streaming->header.bmInfo = buffer[7];
  549. streaming->header.bTerminalLink = buffer[8];
  550. streaming->header.bStillCaptureMethod = buffer[9];
  551. streaming->header.bTriggerSupport = buffer[10];
  552. streaming->header.bTriggerUsage = buffer[11];
  553. } else {
  554. streaming->header.bTerminalLink = buffer[7];
  555. }
  556. streaming->header.bControlSize = n;
  557. streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
  558. if (streaming->header.bmaControls == NULL) {
  559. ret = -ENOMEM;
  560. goto error;
  561. }
  562. memcpy(streaming->header.bmaControls, &buffer[size], p*n);
  563. buflen -= buffer[0];
  564. buffer += buffer[0];
  565. _buffer = buffer;
  566. _buflen = buflen;
  567. /* Count the format and frame descriptors. */
  568. while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
  569. switch (_buffer[2]) {
  570. case UVC_VS_FORMAT_UNCOMPRESSED:
  571. case UVC_VS_FORMAT_MJPEG:
  572. case UVC_VS_FORMAT_FRAME_BASED:
  573. nformats++;
  574. break;
  575. case UVC_VS_FORMAT_DV:
  576. /* DV format has no frame descriptor. We will create a
  577. * dummy frame descriptor with a dummy frame interval.
  578. */
  579. nformats++;
  580. nframes++;
  581. nintervals++;
  582. break;
  583. case UVC_VS_FORMAT_MPEG2TS:
  584. case UVC_VS_FORMAT_STREAM_BASED:
  585. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  586. "interface %d FORMAT %u is not supported.\n",
  587. dev->udev->devnum,
  588. alts->desc.bInterfaceNumber, _buffer[2]);
  589. break;
  590. case UVC_VS_FRAME_UNCOMPRESSED:
  591. case UVC_VS_FRAME_MJPEG:
  592. nframes++;
  593. if (_buflen > 25)
  594. nintervals += _buffer[25] ? _buffer[25] : 3;
  595. break;
  596. case UVC_VS_FRAME_FRAME_BASED:
  597. nframes++;
  598. if (_buflen > 21)
  599. nintervals += _buffer[21] ? _buffer[21] : 3;
  600. break;
  601. }
  602. _buflen -= _buffer[0];
  603. _buffer += _buffer[0];
  604. }
  605. if (nformats == 0) {
  606. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  607. "%d has no supported formats defined.\n",
  608. dev->udev->devnum, alts->desc.bInterfaceNumber);
  609. goto error;
  610. }
  611. size = nformats * sizeof *format + nframes * sizeof *frame
  612. + nintervals * sizeof *interval;
  613. format = kzalloc(size, GFP_KERNEL);
  614. if (format == NULL) {
  615. ret = -ENOMEM;
  616. goto error;
  617. }
  618. frame = (struct uvc_frame *)&format[nformats];
  619. interval = (__u32 *)&frame[nframes];
  620. streaming->format = format;
  621. streaming->nformats = nformats;
  622. /* Parse the format descriptors. */
  623. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
  624. switch (buffer[2]) {
  625. case UVC_VS_FORMAT_UNCOMPRESSED:
  626. case UVC_VS_FORMAT_MJPEG:
  627. case UVC_VS_FORMAT_DV:
  628. case UVC_VS_FORMAT_FRAME_BASED:
  629. format->frame = frame;
  630. ret = uvc_parse_format(dev, streaming, format,
  631. &interval, buffer, buflen);
  632. if (ret < 0)
  633. goto error;
  634. frame += format->nframes;
  635. format++;
  636. buflen -= ret;
  637. buffer += ret;
  638. continue;
  639. default:
  640. break;
  641. }
  642. buflen -= buffer[0];
  643. buffer += buffer[0];
  644. }
  645. if (buflen)
  646. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  647. "%d has %u bytes of trailing descriptor garbage.\n",
  648. dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
  649. /* Parse the alternate settings to find the maximum bandwidth. */
  650. for (i = 0; i < intf->num_altsetting; ++i) {
  651. struct usb_host_endpoint *ep;
  652. alts = &intf->altsetting[i];
  653. ep = uvc_find_endpoint(alts,
  654. streaming->header.bEndpointAddress);
  655. if (ep == NULL)
  656. continue;
  657. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  658. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  659. if (psize > streaming->maxpsize)
  660. streaming->maxpsize = psize;
  661. }
  662. list_add_tail(&streaming->list, &dev->streams);
  663. return 0;
  664. error:
  665. usb_driver_release_interface(&uvc_driver.driver, intf);
  666. usb_put_intf(intf);
  667. kfree(streaming->format);
  668. kfree(streaming->header.bmaControls);
  669. kfree(streaming);
  670. return ret;
  671. }
  672. /* Parse vendor-specific extensions. */
  673. static int uvc_parse_vendor_control(struct uvc_device *dev,
  674. const unsigned char *buffer, int buflen)
  675. {
  676. struct usb_device *udev = dev->udev;
  677. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  678. struct uvc_entity *unit;
  679. unsigned int n, p;
  680. int handled = 0;
  681. switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
  682. case 0x046d: /* Logitech */
  683. if (buffer[1] != 0x41 || buffer[2] != 0x01)
  684. break;
  685. /* Logitech implements several vendor specific functions
  686. * through vendor specific extension units (LXU).
  687. *
  688. * The LXU descriptors are similar to XU descriptors
  689. * (see "USB Device Video Class for Video Devices", section
  690. * 3.7.2.6 "Extension Unit Descriptor") with the following
  691. * differences:
  692. *
  693. * ----------------------------------------------------------
  694. * 0 bLength 1 Number
  695. * Size of this descriptor, in bytes: 24+p+n*2
  696. * ----------------------------------------------------------
  697. * 23+p+n bmControlsType N Bitmap
  698. * Individual bits in the set are defined:
  699. * 0: Absolute
  700. * 1: Relative
  701. *
  702. * This bitset is mapped exactly the same as bmControls.
  703. * ----------------------------------------------------------
  704. * 23+p+n*2 bReserved 1 Boolean
  705. * ----------------------------------------------------------
  706. * 24+p+n*2 iExtension 1 Index
  707. * Index of a string descriptor that describes this
  708. * extension unit.
  709. * ----------------------------------------------------------
  710. */
  711. p = buflen >= 22 ? buffer[21] : 0;
  712. n = buflen >= 25 + p ? buffer[22+p] : 0;
  713. if (buflen < 25 + p + 2*n) {
  714. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  715. "interface %d EXTENSION_UNIT error\n",
  716. udev->devnum, alts->desc.bInterfaceNumber);
  717. break;
  718. }
  719. unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
  720. if (unit == NULL)
  721. return -ENOMEM;
  722. unit->id = buffer[3];
  723. unit->type = UVC_VC_EXTENSION_UNIT;
  724. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  725. unit->extension.bNumControls = buffer[20];
  726. unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
  727. unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
  728. memcpy(unit->extension.baSourceID, &buffer[22], p);
  729. unit->extension.bControlSize = buffer[22+p];
  730. unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
  731. unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
  732. + p + n;
  733. memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
  734. if (buffer[24+p+2*n] != 0)
  735. usb_string(udev, buffer[24+p+2*n], unit->name,
  736. sizeof unit->name);
  737. else
  738. sprintf(unit->name, "Extension %u", buffer[3]);
  739. list_add_tail(&unit->list, &dev->entities);
  740. handled = 1;
  741. break;
  742. }
  743. return handled;
  744. }
  745. static int uvc_parse_standard_control(struct uvc_device *dev,
  746. const unsigned char *buffer, int buflen)
  747. {
  748. struct usb_device *udev = dev->udev;
  749. struct uvc_entity *unit, *term;
  750. struct usb_interface *intf;
  751. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  752. unsigned int i, n, p, len;
  753. __u16 type;
  754. switch (buffer[2]) {
  755. case UVC_VC_HEADER:
  756. n = buflen >= 12 ? buffer[11] : 0;
  757. if (buflen < 12 || buflen < 12 + n) {
  758. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  759. "interface %d HEADER error\n", udev->devnum,
  760. alts->desc.bInterfaceNumber);
  761. return -EINVAL;
  762. }
  763. dev->uvc_version = get_unaligned_le16(&buffer[3]);
  764. dev->clock_frequency = get_unaligned_le32(&buffer[7]);
  765. /* Parse all USB Video Streaming interfaces. */
  766. for (i = 0; i < n; ++i) {
  767. intf = usb_ifnum_to_if(udev, buffer[12+i]);
  768. if (intf == NULL) {
  769. uvc_trace(UVC_TRACE_DESCR, "device %d "
  770. "interface %d doesn't exists\n",
  771. udev->devnum, i);
  772. continue;
  773. }
  774. uvc_parse_streaming(dev, intf);
  775. }
  776. break;
  777. case UVC_VC_INPUT_TERMINAL:
  778. if (buflen < 8) {
  779. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  780. "interface %d INPUT_TERMINAL error\n",
  781. udev->devnum, alts->desc.bInterfaceNumber);
  782. return -EINVAL;
  783. }
  784. /* Make sure the terminal type MSB is not null, otherwise it
  785. * could be confused with a unit.
  786. */
  787. type = get_unaligned_le16(&buffer[4]);
  788. if ((type & 0xff00) == 0) {
  789. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  790. "interface %d INPUT_TERMINAL %d has invalid "
  791. "type 0x%04x, skipping\n", udev->devnum,
  792. alts->desc.bInterfaceNumber,
  793. buffer[3], type);
  794. return 0;
  795. }
  796. n = 0;
  797. p = 0;
  798. len = 8;
  799. if (type == UVC_ITT_CAMERA) {
  800. n = buflen >= 15 ? buffer[14] : 0;
  801. len = 15;
  802. } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  803. n = buflen >= 9 ? buffer[8] : 0;
  804. p = buflen >= 10 + n ? buffer[9+n] : 0;
  805. len = 10;
  806. }
  807. if (buflen < len + n + p) {
  808. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  809. "interface %d INPUT_TERMINAL error\n",
  810. udev->devnum, alts->desc.bInterfaceNumber);
  811. return -EINVAL;
  812. }
  813. term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
  814. if (term == NULL)
  815. return -ENOMEM;
  816. term->id = buffer[3];
  817. term->type = type | UVC_TERM_INPUT;
  818. if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
  819. term->camera.bControlSize = n;
  820. term->camera.bmControls = (__u8 *)term + sizeof *term;
  821. term->camera.wObjectiveFocalLengthMin =
  822. get_unaligned_le16(&buffer[8]);
  823. term->camera.wObjectiveFocalLengthMax =
  824. get_unaligned_le16(&buffer[10]);
  825. term->camera.wOcularFocalLength =
  826. get_unaligned_le16(&buffer[12]);
  827. memcpy(term->camera.bmControls, &buffer[15], n);
  828. } else if (UVC_ENTITY_TYPE(term) ==
  829. UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  830. term->media.bControlSize = n;
  831. term->media.bmControls = (__u8 *)term + sizeof *term;
  832. term->media.bTransportModeSize = p;
  833. term->media.bmTransportModes = (__u8 *)term
  834. + sizeof *term + n;
  835. memcpy(term->media.bmControls, &buffer[9], n);
  836. memcpy(term->media.bmTransportModes, &buffer[10+n], p);
  837. }
  838. if (buffer[7] != 0)
  839. usb_string(udev, buffer[7], term->name,
  840. sizeof term->name);
  841. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
  842. sprintf(term->name, "Camera %u", buffer[3]);
  843. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
  844. sprintf(term->name, "Media %u", buffer[3]);
  845. else
  846. sprintf(term->name, "Input %u", buffer[3]);
  847. list_add_tail(&term->list, &dev->entities);
  848. break;
  849. case UVC_VC_OUTPUT_TERMINAL:
  850. if (buflen < 9) {
  851. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  852. "interface %d OUTPUT_TERMINAL error\n",
  853. udev->devnum, alts->desc.bInterfaceNumber);
  854. return -EINVAL;
  855. }
  856. /* Make sure the terminal type MSB is not null, otherwise it
  857. * could be confused with a unit.
  858. */
  859. type = get_unaligned_le16(&buffer[4]);
  860. if ((type & 0xff00) == 0) {
  861. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  862. "interface %d OUTPUT_TERMINAL %d has invalid "
  863. "type 0x%04x, skipping\n", udev->devnum,
  864. alts->desc.bInterfaceNumber, buffer[3], type);
  865. return 0;
  866. }
  867. term = kzalloc(sizeof *term, GFP_KERNEL);
  868. if (term == NULL)
  869. return -ENOMEM;
  870. term->id = buffer[3];
  871. term->type = type | UVC_TERM_OUTPUT;
  872. term->output.bSourceID = buffer[7];
  873. if (buffer[8] != 0)
  874. usb_string(udev, buffer[8], term->name,
  875. sizeof term->name);
  876. else
  877. sprintf(term->name, "Output %u", buffer[3]);
  878. list_add_tail(&term->list, &dev->entities);
  879. break;
  880. case UVC_VC_SELECTOR_UNIT:
  881. p = buflen >= 5 ? buffer[4] : 0;
  882. if (buflen < 5 || buflen < 6 + p) {
  883. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  884. "interface %d SELECTOR_UNIT error\n",
  885. udev->devnum, alts->desc.bInterfaceNumber);
  886. return -EINVAL;
  887. }
  888. unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
  889. if (unit == NULL)
  890. return -ENOMEM;
  891. unit->id = buffer[3];
  892. unit->type = buffer[2];
  893. unit->selector.bNrInPins = buffer[4];
  894. unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
  895. memcpy(unit->selector.baSourceID, &buffer[5], p);
  896. if (buffer[5+p] != 0)
  897. usb_string(udev, buffer[5+p], unit->name,
  898. sizeof unit->name);
  899. else
  900. sprintf(unit->name, "Selector %u", buffer[3]);
  901. list_add_tail(&unit->list, &dev->entities);
  902. break;
  903. case UVC_VC_PROCESSING_UNIT:
  904. n = buflen >= 8 ? buffer[7] : 0;
  905. p = dev->uvc_version >= 0x0110 ? 10 : 9;
  906. if (buflen < p + n) {
  907. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  908. "interface %d PROCESSING_UNIT error\n",
  909. udev->devnum, alts->desc.bInterfaceNumber);
  910. return -EINVAL;
  911. }
  912. unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
  913. if (unit == NULL)
  914. return -ENOMEM;
  915. unit->id = buffer[3];
  916. unit->type = buffer[2];
  917. unit->processing.bSourceID = buffer[4];
  918. unit->processing.wMaxMultiplier =
  919. get_unaligned_le16(&buffer[5]);
  920. unit->processing.bControlSize = buffer[7];
  921. unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
  922. memcpy(unit->processing.bmControls, &buffer[8], n);
  923. if (dev->uvc_version >= 0x0110)
  924. unit->processing.bmVideoStandards = buffer[9+n];
  925. if (buffer[8+n] != 0)
  926. usb_string(udev, buffer[8+n], unit->name,
  927. sizeof unit->name);
  928. else
  929. sprintf(unit->name, "Processing %u", buffer[3]);
  930. list_add_tail(&unit->list, &dev->entities);
  931. break;
  932. case UVC_VC_EXTENSION_UNIT:
  933. p = buflen >= 22 ? buffer[21] : 0;
  934. n = buflen >= 24 + p ? buffer[22+p] : 0;
  935. if (buflen < 24 + p + n) {
  936. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  937. "interface %d EXTENSION_UNIT error\n",
  938. udev->devnum, alts->desc.bInterfaceNumber);
  939. return -EINVAL;
  940. }
  941. unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
  942. if (unit == NULL)
  943. return -ENOMEM;
  944. unit->id = buffer[3];
  945. unit->type = buffer[2];
  946. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  947. unit->extension.bNumControls = buffer[20];
  948. unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
  949. unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
  950. memcpy(unit->extension.baSourceID, &buffer[22], p);
  951. unit->extension.bControlSize = buffer[22+p];
  952. unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
  953. memcpy(unit->extension.bmControls, &buffer[23+p], n);
  954. if (buffer[23+p+n] != 0)
  955. usb_string(udev, buffer[23+p+n], unit->name,
  956. sizeof unit->name);
  957. else
  958. sprintf(unit->name, "Extension %u", buffer[3]);
  959. list_add_tail(&unit->list, &dev->entities);
  960. break;
  961. default:
  962. uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
  963. "descriptor (%u)\n", buffer[2]);
  964. break;
  965. }
  966. return 0;
  967. }
  968. static int uvc_parse_control(struct uvc_device *dev)
  969. {
  970. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  971. unsigned char *buffer = alts->extra;
  972. int buflen = alts->extralen;
  973. int ret;
  974. /* Parse the default alternate setting only, as the UVC specification
  975. * defines a single alternate setting, the default alternate setting
  976. * zero.
  977. */
  978. while (buflen > 2) {
  979. if (uvc_parse_vendor_control(dev, buffer, buflen) ||
  980. buffer[1] != USB_DT_CS_INTERFACE)
  981. goto next_descriptor;
  982. if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
  983. return ret;
  984. next_descriptor:
  985. buflen -= buffer[0];
  986. buffer += buffer[0];
  987. }
  988. /* Check if the optional status endpoint is present. Built-in iSight
  989. * webcams have an interrupt endpoint but spit proprietary data that
  990. * don't conform to the UVC status endpoint messages. Don't try to
  991. * handle the interrupt endpoint for those cameras.
  992. */
  993. if (alts->desc.bNumEndpoints == 1 &&
  994. !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
  995. struct usb_host_endpoint *ep = &alts->endpoint[0];
  996. struct usb_endpoint_descriptor *desc = &ep->desc;
  997. if (usb_endpoint_is_int_in(desc) &&
  998. le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
  999. desc->bInterval != 0) {
  1000. uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
  1001. "(addr %02x).\n", desc->bEndpointAddress);
  1002. dev->int_ep = ep;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. /* ------------------------------------------------------------------------
  1008. * UVC device scan
  1009. */
  1010. /*
  1011. * Scan the UVC descriptors to locate a chain starting at an Output Terminal
  1012. * and containing the following units:
  1013. *
  1014. * - one or more Output Terminals (USB Streaming or Display)
  1015. * - zero or one Processing Unit
  1016. * - zero, one or more single-input Selector Units
  1017. * - zero or one multiple-input Selector Units, provided all inputs are
  1018. * connected to input terminals
  1019. * - zero, one or mode single-input Extension Units
  1020. * - one or more Input Terminals (Camera, External or USB Streaming)
  1021. *
  1022. * The terminal and units must match on of the following structures:
  1023. *
  1024. * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
  1025. * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
  1026. * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
  1027. *
  1028. * +---------+ +---------+ -> OTT_*(0)
  1029. * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
  1030. * +---------+ +---------+ -> OTT_*(n)
  1031. *
  1032. * The Processing Unit and Extension Units can be in any order. Additional
  1033. * Extension Units connected to the main chain as single-unit branches are
  1034. * also supported. Single-input Selector Units are ignored.
  1035. */
  1036. static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
  1037. struct uvc_entity *entity)
  1038. {
  1039. switch (UVC_ENTITY_TYPE(entity)) {
  1040. case UVC_VC_EXTENSION_UNIT:
  1041. if (uvc_trace_param & UVC_TRACE_PROBE)
  1042. printk(" <- XU %d", entity->id);
  1043. if (entity->extension.bNrInPins != 1) {
  1044. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
  1045. "than 1 input pin.\n", entity->id);
  1046. return -1;
  1047. }
  1048. list_add_tail(&entity->chain, &chain->extensions);
  1049. break;
  1050. case UVC_VC_PROCESSING_UNIT:
  1051. if (uvc_trace_param & UVC_TRACE_PROBE)
  1052. printk(" <- PU %d", entity->id);
  1053. if (chain->processing != NULL) {
  1054. uvc_trace(UVC_TRACE_DESCR, "Found multiple "
  1055. "Processing Units in chain.\n");
  1056. return -1;
  1057. }
  1058. chain->processing = entity;
  1059. break;
  1060. case UVC_VC_SELECTOR_UNIT:
  1061. if (uvc_trace_param & UVC_TRACE_PROBE)
  1062. printk(" <- SU %d", entity->id);
  1063. /* Single-input selector units are ignored. */
  1064. if (entity->selector.bNrInPins == 1)
  1065. break;
  1066. if (chain->selector != NULL) {
  1067. uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
  1068. "Units in chain.\n");
  1069. return -1;
  1070. }
  1071. chain->selector = entity;
  1072. break;
  1073. case UVC_ITT_VENDOR_SPECIFIC:
  1074. case UVC_ITT_CAMERA:
  1075. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1076. if (uvc_trace_param & UVC_TRACE_PROBE)
  1077. printk(" <- IT %d\n", entity->id);
  1078. list_add_tail(&entity->chain, &chain->iterms);
  1079. break;
  1080. case UVC_TT_STREAMING:
  1081. if (uvc_trace_param & UVC_TRACE_PROBE)
  1082. printk(" <- IT %d\n", entity->id);
  1083. if (!UVC_ENTITY_IS_ITERM(entity)) {
  1084. uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
  1085. "terminal %u.\n", entity->id);
  1086. return -1;
  1087. }
  1088. list_add_tail(&entity->chain, &chain->iterms);
  1089. break;
  1090. default:
  1091. uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
  1092. "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
  1093. return -1;
  1094. }
  1095. return 0;
  1096. }
  1097. static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
  1098. struct uvc_entity *entity, struct uvc_entity *prev)
  1099. {
  1100. struct uvc_entity *forward;
  1101. int found;
  1102. /* Forward scan */
  1103. forward = NULL;
  1104. found = 0;
  1105. while (1) {
  1106. forward = uvc_entity_by_reference(chain->dev, entity->id,
  1107. forward);
  1108. if (forward == NULL)
  1109. break;
  1110. if (forward == prev)
  1111. continue;
  1112. switch (UVC_ENTITY_TYPE(forward)) {
  1113. case UVC_VC_EXTENSION_UNIT:
  1114. if (forward->extension.bNrInPins != 1) {
  1115. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
  1116. "has more than 1 input pin.\n",
  1117. entity->id);
  1118. return -EINVAL;
  1119. }
  1120. list_add_tail(&forward->chain, &chain->extensions);
  1121. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1122. if (!found)
  1123. printk(" (->");
  1124. printk(" XU %d", forward->id);
  1125. found = 1;
  1126. }
  1127. break;
  1128. case UVC_OTT_VENDOR_SPECIFIC:
  1129. case UVC_OTT_DISPLAY:
  1130. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1131. case UVC_TT_STREAMING:
  1132. if (UVC_ENTITY_IS_ITERM(forward)) {
  1133. uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
  1134. "terminal %u.\n", forward->id);
  1135. return -EINVAL;
  1136. }
  1137. list_add_tail(&forward->chain, &chain->oterms);
  1138. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1139. if (!found)
  1140. printk(" (->");
  1141. printk(" OT %d", forward->id);
  1142. found = 1;
  1143. }
  1144. break;
  1145. }
  1146. }
  1147. if (found)
  1148. printk(")");
  1149. return 0;
  1150. }
  1151. static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
  1152. struct uvc_entity *entity)
  1153. {
  1154. struct uvc_entity *term;
  1155. int id = -1, i;
  1156. switch (UVC_ENTITY_TYPE(entity)) {
  1157. case UVC_VC_EXTENSION_UNIT:
  1158. id = entity->extension.baSourceID[0];
  1159. break;
  1160. case UVC_VC_PROCESSING_UNIT:
  1161. id = entity->processing.bSourceID;
  1162. break;
  1163. case UVC_VC_SELECTOR_UNIT:
  1164. /* Single-input selector units are ignored. */
  1165. if (entity->selector.bNrInPins == 1) {
  1166. id = entity->selector.baSourceID[0];
  1167. break;
  1168. }
  1169. if (uvc_trace_param & UVC_TRACE_PROBE)
  1170. printk(" <- IT");
  1171. chain->selector = entity;
  1172. for (i = 0; i < entity->selector.bNrInPins; ++i) {
  1173. id = entity->selector.baSourceID[i];
  1174. term = uvc_entity_by_id(chain->dev, id);
  1175. if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
  1176. uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
  1177. "input %d isn't connected to an "
  1178. "input terminal\n", entity->id, i);
  1179. return -1;
  1180. }
  1181. if (uvc_trace_param & UVC_TRACE_PROBE)
  1182. printk(" %d", term->id);
  1183. list_add_tail(&term->chain, &chain->iterms);
  1184. uvc_scan_chain_forward(chain, term, entity);
  1185. }
  1186. if (uvc_trace_param & UVC_TRACE_PROBE)
  1187. printk("\n");
  1188. id = 0;
  1189. break;
  1190. }
  1191. return id;
  1192. }
  1193. static int uvc_scan_chain(struct uvc_video_chain *chain,
  1194. struct uvc_entity *oterm)
  1195. {
  1196. struct uvc_entity *entity, *prev;
  1197. int id;
  1198. entity = oterm;
  1199. list_add_tail(&entity->chain, &chain->oterms);
  1200. uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
  1201. id = entity->output.bSourceID;
  1202. while (id != 0) {
  1203. prev = entity;
  1204. entity = uvc_entity_by_id(chain->dev, id);
  1205. if (entity == NULL) {
  1206. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1207. "unknown entity %d.\n", id);
  1208. return -EINVAL;
  1209. }
  1210. if (entity->chain.next || entity->chain.prev) {
  1211. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1212. "entity %d already in chain.\n", id);
  1213. return -EINVAL;
  1214. }
  1215. /* Process entity */
  1216. if (uvc_scan_chain_entity(chain, entity) < 0)
  1217. return -EINVAL;
  1218. /* Forward scan */
  1219. if (uvc_scan_chain_forward(chain, entity, prev) < 0)
  1220. return -EINVAL;
  1221. /* Stop when a terminal is found. */
  1222. if (UVC_ENTITY_IS_TERM(entity))
  1223. break;
  1224. /* Backward scan */
  1225. id = uvc_scan_chain_backward(chain, entity);
  1226. if (id < 0)
  1227. return id;
  1228. }
  1229. return 0;
  1230. }
  1231. static unsigned int uvc_print_terms(struct list_head *terms, char *buffer)
  1232. {
  1233. struct uvc_entity *term;
  1234. unsigned int nterms = 0;
  1235. char *p = buffer;
  1236. list_for_each_entry(term, terms, chain) {
  1237. p += sprintf(p, "%u", term->id);
  1238. if (term->chain.next != terms) {
  1239. p += sprintf(p, ",");
  1240. if (++nterms >= 4) {
  1241. p += sprintf(p, "...");
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. return p - buffer;
  1247. }
  1248. static const char *uvc_print_chain(struct uvc_video_chain *chain)
  1249. {
  1250. static char buffer[43];
  1251. char *p = buffer;
  1252. p += uvc_print_terms(&chain->iterms, p);
  1253. p += sprintf(p, " -> ");
  1254. uvc_print_terms(&chain->oterms, p);
  1255. return buffer;
  1256. }
  1257. /*
  1258. * Scan the device for video chains and register video devices.
  1259. *
  1260. * Chains are scanned starting at their output terminals and walked backwards.
  1261. */
  1262. static int uvc_scan_device(struct uvc_device *dev)
  1263. {
  1264. struct uvc_video_chain *chain;
  1265. struct uvc_entity *term;
  1266. list_for_each_entry(term, &dev->entities, list) {
  1267. if (!UVC_ENTITY_IS_OTERM(term))
  1268. continue;
  1269. /* If the terminal is already included in a chain, skip it.
  1270. * This can happen for chains that have multiple output
  1271. * terminals, where all output terminals beside the first one
  1272. * will be inserted in the chain in forward scans.
  1273. */
  1274. if (term->chain.next || term->chain.prev)
  1275. continue;
  1276. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  1277. if (chain == NULL)
  1278. return -ENOMEM;
  1279. INIT_LIST_HEAD(&chain->iterms);
  1280. INIT_LIST_HEAD(&chain->oterms);
  1281. INIT_LIST_HEAD(&chain->extensions);
  1282. mutex_init(&chain->ctrl_mutex);
  1283. chain->dev = dev;
  1284. if (uvc_scan_chain(chain, term) < 0) {
  1285. kfree(chain);
  1286. continue;
  1287. }
  1288. uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
  1289. uvc_print_chain(chain));
  1290. list_add_tail(&chain->list, &dev->chains);
  1291. }
  1292. if (list_empty(&dev->chains)) {
  1293. uvc_printk(KERN_INFO, "No valid video chain found.\n");
  1294. return -1;
  1295. }
  1296. return 0;
  1297. }
  1298. /* ------------------------------------------------------------------------
  1299. * Video device registration and unregistration
  1300. */
  1301. /*
  1302. * Delete the UVC device.
  1303. *
  1304. * Called by the kernel when the last reference to the uvc_device structure
  1305. * is released.
  1306. *
  1307. * As this function is called after or during disconnect(), all URBs have
  1308. * already been canceled by the USB core. There is no need to kill the
  1309. * interrupt URB manually.
  1310. */
  1311. static void uvc_delete(struct uvc_device *dev)
  1312. {
  1313. struct list_head *p, *n;
  1314. usb_put_intf(dev->intf);
  1315. usb_put_dev(dev->udev);
  1316. uvc_status_cleanup(dev);
  1317. uvc_ctrl_cleanup_device(dev);
  1318. list_for_each_safe(p, n, &dev->chains) {
  1319. struct uvc_video_chain *chain;
  1320. chain = list_entry(p, struct uvc_video_chain, list);
  1321. kfree(chain);
  1322. }
  1323. list_for_each_safe(p, n, &dev->entities) {
  1324. struct uvc_entity *entity;
  1325. entity = list_entry(p, struct uvc_entity, list);
  1326. kfree(entity);
  1327. }
  1328. list_for_each_safe(p, n, &dev->streams) {
  1329. struct uvc_streaming *streaming;
  1330. streaming = list_entry(p, struct uvc_streaming, list);
  1331. usb_driver_release_interface(&uvc_driver.driver,
  1332. streaming->intf);
  1333. usb_put_intf(streaming->intf);
  1334. kfree(streaming->format);
  1335. kfree(streaming->header.bmaControls);
  1336. kfree(streaming);
  1337. }
  1338. kfree(dev);
  1339. }
  1340. static void uvc_release(struct video_device *vdev)
  1341. {
  1342. struct uvc_streaming *stream = video_get_drvdata(vdev);
  1343. struct uvc_device *dev = stream->dev;
  1344. video_device_release(vdev);
  1345. /* Decrement the registered streams count and delete the device when it
  1346. * reaches zero.
  1347. */
  1348. if (atomic_dec_and_test(&dev->nstreams))
  1349. uvc_delete(dev);
  1350. }
  1351. /*
  1352. * Unregister the video devices.
  1353. */
  1354. static void uvc_unregister_video(struct uvc_device *dev)
  1355. {
  1356. struct uvc_streaming *stream;
  1357. /* Unregistering all video devices might result in uvc_delete() being
  1358. * called from inside the loop if there's no open file handle. To avoid
  1359. * that, increment the stream count before iterating over the streams
  1360. * and decrement it when done.
  1361. */
  1362. atomic_inc(&dev->nstreams);
  1363. list_for_each_entry(stream, &dev->streams, list) {
  1364. if (stream->vdev == NULL)
  1365. continue;
  1366. video_unregister_device(stream->vdev);
  1367. stream->vdev = NULL;
  1368. }
  1369. /* Decrement the stream count and call uvc_delete explicitly if there
  1370. * are no stream left.
  1371. */
  1372. if (atomic_dec_and_test(&dev->nstreams))
  1373. uvc_delete(dev);
  1374. }
  1375. static int uvc_register_video(struct uvc_device *dev,
  1376. struct uvc_streaming *stream)
  1377. {
  1378. struct video_device *vdev;
  1379. int ret;
  1380. /* Initialize the streaming interface with default streaming
  1381. * parameters.
  1382. */
  1383. ret = uvc_video_init(stream);
  1384. if (ret < 0) {
  1385. uvc_printk(KERN_ERR, "Failed to initialize the device "
  1386. "(%d).\n", ret);
  1387. return ret;
  1388. }
  1389. /* Register the device with V4L. */
  1390. vdev = video_device_alloc();
  1391. if (vdev == NULL) {
  1392. uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
  1393. ret);
  1394. return -ENOMEM;
  1395. }
  1396. /* We already hold a reference to dev->udev. The video device will be
  1397. * unregistered before the reference is released, so we don't need to
  1398. * get another one.
  1399. */
  1400. vdev->parent = &dev->intf->dev;
  1401. vdev->minor = -1;
  1402. vdev->fops = &uvc_fops;
  1403. vdev->release = uvc_release;
  1404. strlcpy(vdev->name, dev->name, sizeof vdev->name);
  1405. /* Set the driver data before calling video_register_device, otherwise
  1406. * uvc_v4l2_open might race us.
  1407. */
  1408. stream->vdev = vdev;
  1409. video_set_drvdata(vdev, stream);
  1410. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1411. if (ret < 0) {
  1412. uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
  1413. ret);
  1414. stream->vdev = NULL;
  1415. video_device_release(vdev);
  1416. return ret;
  1417. }
  1418. atomic_inc(&dev->nstreams);
  1419. return 0;
  1420. }
  1421. /*
  1422. * Register all video devices in all chains.
  1423. */
  1424. static int uvc_register_terms(struct uvc_device *dev,
  1425. struct uvc_video_chain *chain, struct list_head *terms)
  1426. {
  1427. struct uvc_streaming *stream;
  1428. struct uvc_entity *term;
  1429. int ret;
  1430. list_for_each_entry(term, terms, chain) {
  1431. if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
  1432. continue;
  1433. stream = uvc_stream_by_id(dev, term->id);
  1434. if (stream == NULL) {
  1435. uvc_printk(KERN_INFO, "No streaming interface found "
  1436. "for terminal %u.", term->id);
  1437. continue;
  1438. }
  1439. stream->chain = chain;
  1440. ret = uvc_register_video(dev, stream);
  1441. if (ret < 0)
  1442. return ret;
  1443. }
  1444. return 0;
  1445. }
  1446. static int uvc_register_chains(struct uvc_device *dev)
  1447. {
  1448. struct uvc_video_chain *chain;
  1449. int ret;
  1450. list_for_each_entry(chain, &dev->chains, list) {
  1451. ret = uvc_register_terms(dev, chain, &chain->iterms);
  1452. if (ret < 0)
  1453. return ret;
  1454. ret = uvc_register_terms(dev, chain, &chain->oterms);
  1455. if (ret < 0)
  1456. return ret;
  1457. }
  1458. return 0;
  1459. }
  1460. /* ------------------------------------------------------------------------
  1461. * USB probe, disconnect, suspend and resume
  1462. */
  1463. static int uvc_probe(struct usb_interface *intf,
  1464. const struct usb_device_id *id)
  1465. {
  1466. struct usb_device *udev = interface_to_usbdev(intf);
  1467. struct uvc_device *dev;
  1468. int ret;
  1469. if (id->idVendor && id->idProduct)
  1470. uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
  1471. "(%04x:%04x)\n", udev->devpath, id->idVendor,
  1472. id->idProduct);
  1473. else
  1474. uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
  1475. udev->devpath);
  1476. /* Allocate memory for the device and initialize it. */
  1477. if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
  1478. return -ENOMEM;
  1479. INIT_LIST_HEAD(&dev->entities);
  1480. INIT_LIST_HEAD(&dev->chains);
  1481. INIT_LIST_HEAD(&dev->streams);
  1482. atomic_set(&dev->nstreams, 0);
  1483. atomic_set(&dev->users, 0);
  1484. dev->udev = usb_get_dev(udev);
  1485. dev->intf = usb_get_intf(intf);
  1486. dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1487. dev->quirks = id->driver_info | uvc_quirks_param;
  1488. if (udev->product != NULL)
  1489. strlcpy(dev->name, udev->product, sizeof dev->name);
  1490. else
  1491. snprintf(dev->name, sizeof dev->name,
  1492. "UVC Camera (%04x:%04x)",
  1493. le16_to_cpu(udev->descriptor.idVendor),
  1494. le16_to_cpu(udev->descriptor.idProduct));
  1495. /* Parse the Video Class control descriptor. */
  1496. if (uvc_parse_control(dev) < 0) {
  1497. uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
  1498. "descriptors.\n");
  1499. goto error;
  1500. }
  1501. uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
  1502. dev->uvc_version >> 8, dev->uvc_version & 0xff,
  1503. udev->product ? udev->product : "<unnamed>",
  1504. le16_to_cpu(udev->descriptor.idVendor),
  1505. le16_to_cpu(udev->descriptor.idProduct));
  1506. if (uvc_quirks_param != 0) {
  1507. uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
  1508. "parameter for testing purpose.\n", uvc_quirks_param);
  1509. uvc_printk(KERN_INFO, "Please report required quirks to the "
  1510. "linux-uvc-devel mailing list.\n");
  1511. }
  1512. /* Initialize controls. */
  1513. if (uvc_ctrl_init_device(dev) < 0)
  1514. goto error;
  1515. /* Scan the device for video chains. */
  1516. if (uvc_scan_device(dev) < 0)
  1517. goto error;
  1518. /* Register video devices. */
  1519. if (uvc_register_chains(dev) < 0)
  1520. goto error;
  1521. /* Save our data pointer in the interface data. */
  1522. usb_set_intfdata(intf, dev);
  1523. /* Initialize the interrupt URB. */
  1524. if ((ret = uvc_status_init(dev)) < 0) {
  1525. uvc_printk(KERN_INFO, "Unable to initialize the status "
  1526. "endpoint (%d), status interrupt will not be "
  1527. "supported.\n", ret);
  1528. }
  1529. uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
  1530. return 0;
  1531. error:
  1532. uvc_unregister_video(dev);
  1533. return -ENODEV;
  1534. }
  1535. static void uvc_disconnect(struct usb_interface *intf)
  1536. {
  1537. struct uvc_device *dev = usb_get_intfdata(intf);
  1538. /* Set the USB interface data to NULL. This can be done outside the
  1539. * lock, as there's no other reader.
  1540. */
  1541. usb_set_intfdata(intf, NULL);
  1542. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1543. UVC_SC_VIDEOSTREAMING)
  1544. return;
  1545. dev->state |= UVC_DEV_DISCONNECTED;
  1546. uvc_unregister_video(dev);
  1547. }
  1548. static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
  1549. {
  1550. struct uvc_device *dev = usb_get_intfdata(intf);
  1551. struct uvc_streaming *stream;
  1552. uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
  1553. intf->cur_altsetting->desc.bInterfaceNumber);
  1554. /* Controls are cached on the fly so they don't need to be saved. */
  1555. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1556. UVC_SC_VIDEOCONTROL)
  1557. return uvc_status_suspend(dev);
  1558. list_for_each_entry(stream, &dev->streams, list) {
  1559. if (stream->intf == intf)
  1560. return uvc_video_suspend(stream);
  1561. }
  1562. uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
  1563. "mismatch.\n");
  1564. return -EINVAL;
  1565. }
  1566. static int __uvc_resume(struct usb_interface *intf, int reset)
  1567. {
  1568. struct uvc_device *dev = usb_get_intfdata(intf);
  1569. struct uvc_streaming *stream;
  1570. uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
  1571. intf->cur_altsetting->desc.bInterfaceNumber);
  1572. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1573. UVC_SC_VIDEOCONTROL) {
  1574. if (reset) {
  1575. int ret = uvc_ctrl_resume_device(dev);
  1576. if (ret < 0)
  1577. return ret;
  1578. }
  1579. return uvc_status_resume(dev);
  1580. }
  1581. list_for_each_entry(stream, &dev->streams, list) {
  1582. if (stream->intf == intf)
  1583. return uvc_video_resume(stream);
  1584. }
  1585. uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
  1586. "mismatch.\n");
  1587. return -EINVAL;
  1588. }
  1589. static int uvc_resume(struct usb_interface *intf)
  1590. {
  1591. return __uvc_resume(intf, 0);
  1592. }
  1593. static int uvc_reset_resume(struct usb_interface *intf)
  1594. {
  1595. return __uvc_resume(intf, 1);
  1596. }
  1597. /* ------------------------------------------------------------------------
  1598. * Driver initialization and cleanup
  1599. */
  1600. /*
  1601. * The Logitech cameras listed below have their interface class set to
  1602. * VENDOR_SPEC because they don't announce themselves as UVC devices, even
  1603. * though they are compliant.
  1604. */
  1605. static struct usb_device_id uvc_ids[] = {
  1606. /* Genius eFace 2025 */
  1607. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1608. | USB_DEVICE_ID_MATCH_INT_INFO,
  1609. .idVendor = 0x0458,
  1610. .idProduct = 0x706e,
  1611. .bInterfaceClass = USB_CLASS_VIDEO,
  1612. .bInterfaceSubClass = 1,
  1613. .bInterfaceProtocol = 0,
  1614. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1615. /* Microsoft Lifecam NX-6000 */
  1616. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1617. | USB_DEVICE_ID_MATCH_INT_INFO,
  1618. .idVendor = 0x045e,
  1619. .idProduct = 0x00f8,
  1620. .bInterfaceClass = USB_CLASS_VIDEO,
  1621. .bInterfaceSubClass = 1,
  1622. .bInterfaceProtocol = 0,
  1623. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1624. /* Microsoft Lifecam VX-7000 */
  1625. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1626. | USB_DEVICE_ID_MATCH_INT_INFO,
  1627. .idVendor = 0x045e,
  1628. .idProduct = 0x0723,
  1629. .bInterfaceClass = USB_CLASS_VIDEO,
  1630. .bInterfaceSubClass = 1,
  1631. .bInterfaceProtocol = 0,
  1632. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1633. /* Logitech Quickcam Fusion */
  1634. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1635. | USB_DEVICE_ID_MATCH_INT_INFO,
  1636. .idVendor = 0x046d,
  1637. .idProduct = 0x08c1,
  1638. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1639. .bInterfaceSubClass = 1,
  1640. .bInterfaceProtocol = 0 },
  1641. /* Logitech Quickcam Orbit MP */
  1642. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1643. | USB_DEVICE_ID_MATCH_INT_INFO,
  1644. .idVendor = 0x046d,
  1645. .idProduct = 0x08c2,
  1646. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1647. .bInterfaceSubClass = 1,
  1648. .bInterfaceProtocol = 0 },
  1649. /* Logitech Quickcam Pro for Notebook */
  1650. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1651. | USB_DEVICE_ID_MATCH_INT_INFO,
  1652. .idVendor = 0x046d,
  1653. .idProduct = 0x08c3,
  1654. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1655. .bInterfaceSubClass = 1,
  1656. .bInterfaceProtocol = 0 },
  1657. /* Logitech Quickcam Pro 5000 */
  1658. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1659. | USB_DEVICE_ID_MATCH_INT_INFO,
  1660. .idVendor = 0x046d,
  1661. .idProduct = 0x08c5,
  1662. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1663. .bInterfaceSubClass = 1,
  1664. .bInterfaceProtocol = 0 },
  1665. /* Logitech Quickcam OEM Dell Notebook */
  1666. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1667. | USB_DEVICE_ID_MATCH_INT_INFO,
  1668. .idVendor = 0x046d,
  1669. .idProduct = 0x08c6,
  1670. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1671. .bInterfaceSubClass = 1,
  1672. .bInterfaceProtocol = 0 },
  1673. /* Logitech Quickcam OEM Cisco VT Camera II */
  1674. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1675. | USB_DEVICE_ID_MATCH_INT_INFO,
  1676. .idVendor = 0x046d,
  1677. .idProduct = 0x08c7,
  1678. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1679. .bInterfaceSubClass = 1,
  1680. .bInterfaceProtocol = 0 },
  1681. /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
  1682. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1683. | USB_DEVICE_ID_MATCH_INT_INFO,
  1684. .idVendor = 0x058f,
  1685. .idProduct = 0x3820,
  1686. .bInterfaceClass = USB_CLASS_VIDEO,
  1687. .bInterfaceSubClass = 1,
  1688. .bInterfaceProtocol = 0,
  1689. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1690. /* Apple Built-In iSight */
  1691. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1692. | USB_DEVICE_ID_MATCH_INT_INFO,
  1693. .idVendor = 0x05ac,
  1694. .idProduct = 0x8501,
  1695. .bInterfaceClass = USB_CLASS_VIDEO,
  1696. .bInterfaceSubClass = 1,
  1697. .bInterfaceProtocol = 0,
  1698. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1699. | UVC_QUIRK_BUILTIN_ISIGHT },
  1700. /* Genesys Logic USB 2.0 PC Camera */
  1701. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1702. | USB_DEVICE_ID_MATCH_INT_INFO,
  1703. .idVendor = 0x05e3,
  1704. .idProduct = 0x0505,
  1705. .bInterfaceClass = USB_CLASS_VIDEO,
  1706. .bInterfaceSubClass = 1,
  1707. .bInterfaceProtocol = 0,
  1708. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1709. /* ViMicro Vega */
  1710. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1711. | USB_DEVICE_ID_MATCH_INT_INFO,
  1712. .idVendor = 0x0ac8,
  1713. .idProduct = 0x332d,
  1714. .bInterfaceClass = USB_CLASS_VIDEO,
  1715. .bInterfaceSubClass = 1,
  1716. .bInterfaceProtocol = 0,
  1717. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1718. /* ViMicro - Minoru3D */
  1719. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1720. | USB_DEVICE_ID_MATCH_INT_INFO,
  1721. .idVendor = 0x0ac8,
  1722. .idProduct = 0x3410,
  1723. .bInterfaceClass = USB_CLASS_VIDEO,
  1724. .bInterfaceSubClass = 1,
  1725. .bInterfaceProtocol = 0,
  1726. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1727. /* ViMicro Venus - Minoru3D */
  1728. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1729. | USB_DEVICE_ID_MATCH_INT_INFO,
  1730. .idVendor = 0x0ac8,
  1731. .idProduct = 0x3420,
  1732. .bInterfaceClass = USB_CLASS_VIDEO,
  1733. .bInterfaceSubClass = 1,
  1734. .bInterfaceProtocol = 0,
  1735. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1736. /* MT6227 */
  1737. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1738. | USB_DEVICE_ID_MATCH_INT_INFO,
  1739. .idVendor = 0x0e8d,
  1740. .idProduct = 0x0004,
  1741. .bInterfaceClass = USB_CLASS_VIDEO,
  1742. .bInterfaceSubClass = 1,
  1743. .bInterfaceProtocol = 0,
  1744. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1745. | UVC_QUIRK_PROBE_DEF },
  1746. /* Syntek (HP Spartan) */
  1747. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1748. | USB_DEVICE_ID_MATCH_INT_INFO,
  1749. .idVendor = 0x174f,
  1750. .idProduct = 0x5212,
  1751. .bInterfaceClass = USB_CLASS_VIDEO,
  1752. .bInterfaceSubClass = 1,
  1753. .bInterfaceProtocol = 0,
  1754. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1755. /* Syntek (Samsung Q310) */
  1756. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1757. | USB_DEVICE_ID_MATCH_INT_INFO,
  1758. .idVendor = 0x174f,
  1759. .idProduct = 0x5931,
  1760. .bInterfaceClass = USB_CLASS_VIDEO,
  1761. .bInterfaceSubClass = 1,
  1762. .bInterfaceProtocol = 0,
  1763. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1764. /* Syntek (Asus F9SG) */
  1765. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1766. | USB_DEVICE_ID_MATCH_INT_INFO,
  1767. .idVendor = 0x174f,
  1768. .idProduct = 0x8a31,
  1769. .bInterfaceClass = USB_CLASS_VIDEO,
  1770. .bInterfaceSubClass = 1,
  1771. .bInterfaceProtocol = 0,
  1772. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1773. /* Syntek (Asus U3S) */
  1774. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1775. | USB_DEVICE_ID_MATCH_INT_INFO,
  1776. .idVendor = 0x174f,
  1777. .idProduct = 0x8a33,
  1778. .bInterfaceClass = USB_CLASS_VIDEO,
  1779. .bInterfaceSubClass = 1,
  1780. .bInterfaceProtocol = 0,
  1781. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1782. /* Syntek (JAOtech Smart Terminal) */
  1783. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1784. | USB_DEVICE_ID_MATCH_INT_INFO,
  1785. .idVendor = 0x174f,
  1786. .idProduct = 0x8a34,
  1787. .bInterfaceClass = USB_CLASS_VIDEO,
  1788. .bInterfaceSubClass = 1,
  1789. .bInterfaceProtocol = 0,
  1790. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1791. /* Lenovo Thinkpad SL400/SL500 */
  1792. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1793. | USB_DEVICE_ID_MATCH_INT_INFO,
  1794. .idVendor = 0x17ef,
  1795. .idProduct = 0x480b,
  1796. .bInterfaceClass = USB_CLASS_VIDEO,
  1797. .bInterfaceSubClass = 1,
  1798. .bInterfaceProtocol = 0,
  1799. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1800. /* Aveo Technology USB 2.0 Camera */
  1801. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1802. | USB_DEVICE_ID_MATCH_INT_INFO,
  1803. .idVendor = 0x1871,
  1804. .idProduct = 0x0306,
  1805. .bInterfaceClass = USB_CLASS_VIDEO,
  1806. .bInterfaceSubClass = 1,
  1807. .bInterfaceProtocol = 0,
  1808. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1809. | UVC_QUIRK_PROBE_EXTRAFIELDS },
  1810. /* Ecamm Pico iMage */
  1811. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1812. | USB_DEVICE_ID_MATCH_INT_INFO,
  1813. .idVendor = 0x18cd,
  1814. .idProduct = 0xcafe,
  1815. .bInterfaceClass = USB_CLASS_VIDEO,
  1816. .bInterfaceSubClass = 1,
  1817. .bInterfaceProtocol = 0,
  1818. .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
  1819. /* FSC WebCam V30S */
  1820. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1821. | USB_DEVICE_ID_MATCH_INT_INFO,
  1822. .idVendor = 0x18ec,
  1823. .idProduct = 0x3288,
  1824. .bInterfaceClass = USB_CLASS_VIDEO,
  1825. .bInterfaceSubClass = 1,
  1826. .bInterfaceProtocol = 0,
  1827. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1828. /* Bodelin ProScopeHR */
  1829. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1830. | USB_DEVICE_ID_MATCH_DEV_HI
  1831. | USB_DEVICE_ID_MATCH_INT_INFO,
  1832. .idVendor = 0x19ab,
  1833. .idProduct = 0x1000,
  1834. .bcdDevice_hi = 0x0126,
  1835. .bInterfaceClass = USB_CLASS_VIDEO,
  1836. .bInterfaceSubClass = 1,
  1837. .bInterfaceProtocol = 0,
  1838. .driver_info = UVC_QUIRK_STATUS_INTERVAL },
  1839. /* MSI StarCam 370i */
  1840. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1841. | USB_DEVICE_ID_MATCH_INT_INFO,
  1842. .idVendor = 0x1b3b,
  1843. .idProduct = 0x2951,
  1844. .bInterfaceClass = USB_CLASS_VIDEO,
  1845. .bInterfaceSubClass = 1,
  1846. .bInterfaceProtocol = 0,
  1847. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1848. /* SiGma Micro USB Web Camera */
  1849. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1850. | USB_DEVICE_ID_MATCH_INT_INFO,
  1851. .idVendor = 0x1c4f,
  1852. .idProduct = 0x3000,
  1853. .bInterfaceClass = USB_CLASS_VIDEO,
  1854. .bInterfaceSubClass = 1,
  1855. .bInterfaceProtocol = 0,
  1856. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1857. | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
  1858. /* Generic USB Video Class */
  1859. { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
  1860. {}
  1861. };
  1862. MODULE_DEVICE_TABLE(usb, uvc_ids);
  1863. struct uvc_driver uvc_driver = {
  1864. .driver = {
  1865. .name = "uvcvideo",
  1866. .probe = uvc_probe,
  1867. .disconnect = uvc_disconnect,
  1868. .suspend = uvc_suspend,
  1869. .resume = uvc_resume,
  1870. .reset_resume = uvc_reset_resume,
  1871. .id_table = uvc_ids,
  1872. .supports_autosuspend = 1,
  1873. },
  1874. };
  1875. static int __init uvc_init(void)
  1876. {
  1877. int result;
  1878. INIT_LIST_HEAD(&uvc_driver.devices);
  1879. INIT_LIST_HEAD(&uvc_driver.controls);
  1880. mutex_init(&uvc_driver.ctrl_mutex);
  1881. uvc_ctrl_init();
  1882. result = usb_register(&uvc_driver.driver);
  1883. if (result == 0)
  1884. printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
  1885. return result;
  1886. }
  1887. static void __exit uvc_cleanup(void)
  1888. {
  1889. usb_deregister(&uvc_driver.driver);
  1890. }
  1891. module_init(uvc_init);
  1892. module_exit(uvc_cleanup);
  1893. module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
  1894. MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
  1895. module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
  1896. MODULE_PARM_DESC(quirks, "Forced device quirks");
  1897. module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
  1898. MODULE_PARM_DESC(trace, "Trace level bitmask");
  1899. module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
  1900. MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
  1901. MODULE_AUTHOR(DRIVER_AUTHOR);
  1902. MODULE_DESCRIPTION(DRIVER_DESC);
  1903. MODULE_LICENSE("GPL");
  1904. MODULE_VERSION(DRIVER_VERSION);