uvc_driver.c 58 KB

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