uvc_driver.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  1. /*
  2. * uvc_driver.c -- USB Video Class driver
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  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. #include <linux/atomic.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/wait.h>
  22. #include <linux/version.h>
  23. #include <asm/unaligned.h>
  24. #include <media/v4l2-common.h>
  25. #include "uvcvideo.h"
  26. #define DRIVER_AUTHOR "Laurent Pinchart " \
  27. "<laurent.pinchart@ideasonboard.com>"
  28. #define DRIVER_DESC "USB Video Class driver"
  29. unsigned int uvc_clock_param = CLOCK_MONOTONIC;
  30. unsigned int uvc_hw_timestamps_param;
  31. unsigned int uvc_no_drop_param;
  32. static unsigned int uvc_quirks_param = -1;
  33. unsigned int uvc_trace_param;
  34. unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
  35. /* ------------------------------------------------------------------------
  36. * Video formats
  37. */
  38. static struct uvc_format_desc uvc_fmts[] = {
  39. {
  40. .name = "YUV 4:2:2 (YUYV)",
  41. .guid = UVC_GUID_FORMAT_YUY2,
  42. .fcc = V4L2_PIX_FMT_YUYV,
  43. },
  44. {
  45. .name = "YUV 4:2:2 (YUYV)",
  46. .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
  47. .fcc = V4L2_PIX_FMT_YUYV,
  48. },
  49. {
  50. .name = "YUV 4:2:0 (NV12)",
  51. .guid = UVC_GUID_FORMAT_NV12,
  52. .fcc = V4L2_PIX_FMT_NV12,
  53. },
  54. {
  55. .name = "MJPEG",
  56. .guid = UVC_GUID_FORMAT_MJPEG,
  57. .fcc = V4L2_PIX_FMT_MJPEG,
  58. },
  59. {
  60. .name = "YVU 4:2:0 (YV12)",
  61. .guid = UVC_GUID_FORMAT_YV12,
  62. .fcc = V4L2_PIX_FMT_YVU420,
  63. },
  64. {
  65. .name = "YUV 4:2:0 (I420)",
  66. .guid = UVC_GUID_FORMAT_I420,
  67. .fcc = V4L2_PIX_FMT_YUV420,
  68. },
  69. {
  70. .name = "YUV 4:2:0 (M420)",
  71. .guid = UVC_GUID_FORMAT_M420,
  72. .fcc = V4L2_PIX_FMT_M420,
  73. },
  74. {
  75. .name = "YUV 4:2:2 (UYVY)",
  76. .guid = UVC_GUID_FORMAT_UYVY,
  77. .fcc = V4L2_PIX_FMT_UYVY,
  78. },
  79. {
  80. .name = "Greyscale 8-bit (Y800)",
  81. .guid = UVC_GUID_FORMAT_Y800,
  82. .fcc = V4L2_PIX_FMT_GREY,
  83. },
  84. {
  85. .name = "Greyscale 8-bit (Y8 )",
  86. .guid = UVC_GUID_FORMAT_Y8,
  87. .fcc = V4L2_PIX_FMT_GREY,
  88. },
  89. {
  90. .name = "Greyscale 10-bit (Y10 )",
  91. .guid = UVC_GUID_FORMAT_Y10,
  92. .fcc = V4L2_PIX_FMT_Y10,
  93. },
  94. {
  95. .name = "Greyscale 12-bit (Y12 )",
  96. .guid = UVC_GUID_FORMAT_Y12,
  97. .fcc = V4L2_PIX_FMT_Y12,
  98. },
  99. {
  100. .name = "Greyscale 16-bit (Y16 )",
  101. .guid = UVC_GUID_FORMAT_Y16,
  102. .fcc = V4L2_PIX_FMT_Y16,
  103. },
  104. {
  105. .name = "BGGR Bayer (BY8 )",
  106. .guid = UVC_GUID_FORMAT_BY8,
  107. .fcc = V4L2_PIX_FMT_SBGGR8,
  108. },
  109. {
  110. .name = "BGGR Bayer (BA81)",
  111. .guid = UVC_GUID_FORMAT_BA81,
  112. .fcc = V4L2_PIX_FMT_SBGGR8,
  113. },
  114. {
  115. .name = "GBRG Bayer (GBRG)",
  116. .guid = UVC_GUID_FORMAT_GBRG,
  117. .fcc = V4L2_PIX_FMT_SGBRG8,
  118. },
  119. {
  120. .name = "GRBG Bayer (GRBG)",
  121. .guid = UVC_GUID_FORMAT_GRBG,
  122. .fcc = V4L2_PIX_FMT_SGRBG8,
  123. },
  124. {
  125. .name = "RGGB Bayer (RGGB)",
  126. .guid = UVC_GUID_FORMAT_RGGB,
  127. .fcc = V4L2_PIX_FMT_SRGGB8,
  128. },
  129. {
  130. .name = "RGB565",
  131. .guid = UVC_GUID_FORMAT_RGBP,
  132. .fcc = V4L2_PIX_FMT_RGB565,
  133. },
  134. {
  135. .name = "BGR 8:8:8 (BGR3)",
  136. .guid = UVC_GUID_FORMAT_BGR3,
  137. .fcc = V4L2_PIX_FMT_BGR24,
  138. },
  139. {
  140. .name = "H.264",
  141. .guid = UVC_GUID_FORMAT_H264,
  142. .fcc = V4L2_PIX_FMT_H264,
  143. },
  144. {
  145. .name = "Greyscale 8 L/R (Y8I)",
  146. .guid = UVC_GUID_FORMAT_Y8I,
  147. .fcc = V4L2_PIX_FMT_Y8I,
  148. },
  149. {
  150. .name = "Greyscale 12 L/R (Y12I)",
  151. .guid = UVC_GUID_FORMAT_Y12I,
  152. .fcc = V4L2_PIX_FMT_Y12I,
  153. },
  154. {
  155. .name = "Depth data 16-bit (Z16)",
  156. .guid = UVC_GUID_FORMAT_Z16,
  157. .fcc = V4L2_PIX_FMT_Z16,
  158. },
  159. {
  160. .name = "Bayer 10-bit (SRGGB10P)",
  161. .guid = UVC_GUID_FORMAT_RW10,
  162. .fcc = V4L2_PIX_FMT_SRGGB10P,
  163. },
  164. };
  165. /* ------------------------------------------------------------------------
  166. * Utility functions
  167. */
  168. struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
  169. __u8 epaddr)
  170. {
  171. struct usb_host_endpoint *ep;
  172. unsigned int i;
  173. for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
  174. ep = &alts->endpoint[i];
  175. if (ep->desc.bEndpointAddress == epaddr)
  176. return ep;
  177. }
  178. return NULL;
  179. }
  180. static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
  181. {
  182. unsigned int len = ARRAY_SIZE(uvc_fmts);
  183. unsigned int i;
  184. for (i = 0; i < len; ++i) {
  185. if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
  186. return &uvc_fmts[i];
  187. }
  188. return NULL;
  189. }
  190. static __u32 uvc_colorspace(const __u8 primaries)
  191. {
  192. static const __u8 colorprimaries[] = {
  193. 0,
  194. V4L2_COLORSPACE_SRGB,
  195. V4L2_COLORSPACE_470_SYSTEM_M,
  196. V4L2_COLORSPACE_470_SYSTEM_BG,
  197. V4L2_COLORSPACE_SMPTE170M,
  198. V4L2_COLORSPACE_SMPTE240M,
  199. };
  200. if (primaries < ARRAY_SIZE(colorprimaries))
  201. return colorprimaries[primaries];
  202. return 0;
  203. }
  204. /* Simplify a fraction using a simple continued fraction decomposition. The
  205. * idea here is to convert fractions such as 333333/10000000 to 1/30 using
  206. * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
  207. * arbitrary parameters to remove non-significative terms from the simple
  208. * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
  209. * respectively seems to give nice results.
  210. */
  211. void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
  212. unsigned int n_terms, unsigned int threshold)
  213. {
  214. uint32_t *an;
  215. uint32_t x, y, r;
  216. unsigned int i, n;
  217. an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
  218. if (an == NULL)
  219. return;
  220. /* Convert the fraction to a simple continued fraction. See
  221. * http://mathforum.org/dr.math/faq/faq.fractions.html
  222. * Stop if the current term is bigger than or equal to the given
  223. * threshold.
  224. */
  225. x = *numerator;
  226. y = *denominator;
  227. for (n = 0; n < n_terms && y != 0; ++n) {
  228. an[n] = x / y;
  229. if (an[n] >= threshold) {
  230. if (n < 2)
  231. n++;
  232. break;
  233. }
  234. r = x - an[n] * y;
  235. x = y;
  236. y = r;
  237. }
  238. /* Expand the simple continued fraction back to an integer fraction. */
  239. x = 0;
  240. y = 1;
  241. for (i = n; i > 0; --i) {
  242. r = y;
  243. y = an[i-1] * y + x;
  244. x = r;
  245. }
  246. *numerator = y;
  247. *denominator = x;
  248. kfree(an);
  249. }
  250. /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
  251. * to compute numerator / denominator * 10000000 using 32 bit fixed point
  252. * arithmetic only.
  253. */
  254. uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
  255. {
  256. uint32_t multiplier;
  257. /* Saturate the result if the operation would overflow. */
  258. if (denominator == 0 ||
  259. numerator/denominator >= ((uint32_t)-1)/10000000)
  260. return (uint32_t)-1;
  261. /* Divide both the denominator and the multiplier by two until
  262. * numerator * multiplier doesn't overflow. If anyone knows a better
  263. * algorithm please let me know.
  264. */
  265. multiplier = 10000000;
  266. while (numerator > ((uint32_t)-1)/multiplier) {
  267. multiplier /= 2;
  268. denominator /= 2;
  269. }
  270. return denominator ? numerator * multiplier / denominator : 0;
  271. }
  272. /* ------------------------------------------------------------------------
  273. * Terminal and unit management
  274. */
  275. struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
  276. {
  277. struct uvc_entity *entity;
  278. list_for_each_entry(entity, &dev->entities, list) {
  279. if (entity->id == id)
  280. return entity;
  281. }
  282. return NULL;
  283. }
  284. static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
  285. int id, struct uvc_entity *entity)
  286. {
  287. unsigned int i;
  288. if (entity == NULL)
  289. entity = list_entry(&dev->entities, struct uvc_entity, list);
  290. list_for_each_entry_continue(entity, &dev->entities, list) {
  291. for (i = 0; i < entity->bNrInPins; ++i)
  292. if (entity->baSourceID[i] == id)
  293. return entity;
  294. }
  295. return NULL;
  296. }
  297. static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
  298. {
  299. struct uvc_streaming *stream;
  300. list_for_each_entry(stream, &dev->streams, list) {
  301. if (stream->header.bTerminalLink == id)
  302. return stream;
  303. }
  304. return NULL;
  305. }
  306. /* ------------------------------------------------------------------------
  307. * Descriptors parsing
  308. */
  309. static int uvc_parse_format(struct uvc_device *dev,
  310. struct uvc_streaming *streaming, struct uvc_format *format,
  311. __u32 **intervals, unsigned char *buffer, int buflen)
  312. {
  313. struct usb_interface *intf = streaming->intf;
  314. struct usb_host_interface *alts = intf->cur_altsetting;
  315. struct uvc_format_desc *fmtdesc;
  316. struct uvc_frame *frame;
  317. const unsigned char *start = buffer;
  318. unsigned int width_multiplier = 1;
  319. unsigned int interval;
  320. unsigned int i, n;
  321. __u8 ftype;
  322. format->type = buffer[2];
  323. format->index = buffer[3];
  324. switch (buffer[2]) {
  325. case UVC_VS_FORMAT_UNCOMPRESSED:
  326. case UVC_VS_FORMAT_FRAME_BASED:
  327. n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
  328. if (buflen < n) {
  329. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  330. "interface %d FORMAT error\n",
  331. dev->udev->devnum,
  332. alts->desc.bInterfaceNumber);
  333. return -EINVAL;
  334. }
  335. /* Find the format descriptor from its GUID. */
  336. fmtdesc = uvc_format_by_guid(&buffer[5]);
  337. if (fmtdesc != NULL) {
  338. strlcpy(format->name, fmtdesc->name,
  339. sizeof format->name);
  340. format->fcc = fmtdesc->fcc;
  341. } else {
  342. uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
  343. &buffer[5]);
  344. snprintf(format->name, sizeof(format->name), "%pUl\n",
  345. &buffer[5]);
  346. format->fcc = 0;
  347. }
  348. format->bpp = buffer[21];
  349. /* Some devices report a format that doesn't match what they
  350. * really send.
  351. */
  352. if (dev->quirks & UVC_QUIRK_FORCE_Y8) {
  353. if (format->fcc == V4L2_PIX_FMT_YUYV) {
  354. strlcpy(format->name, "Greyscale 8-bit (Y8 )",
  355. sizeof(format->name));
  356. format->fcc = V4L2_PIX_FMT_GREY;
  357. format->bpp = 8;
  358. width_multiplier = 2;
  359. }
  360. }
  361. if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
  362. ftype = UVC_VS_FRAME_UNCOMPRESSED;
  363. } else {
  364. ftype = UVC_VS_FRAME_FRAME_BASED;
  365. if (buffer[27])
  366. format->flags = UVC_FMT_FLAG_COMPRESSED;
  367. }
  368. break;
  369. case UVC_VS_FORMAT_MJPEG:
  370. if (buflen < 11) {
  371. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  372. "interface %d FORMAT error\n",
  373. dev->udev->devnum,
  374. alts->desc.bInterfaceNumber);
  375. return -EINVAL;
  376. }
  377. strlcpy(format->name, "MJPEG", sizeof format->name);
  378. format->fcc = V4L2_PIX_FMT_MJPEG;
  379. format->flags = UVC_FMT_FLAG_COMPRESSED;
  380. format->bpp = 0;
  381. ftype = UVC_VS_FRAME_MJPEG;
  382. break;
  383. case UVC_VS_FORMAT_DV:
  384. if (buflen < 9) {
  385. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  386. "interface %d FORMAT error\n",
  387. dev->udev->devnum,
  388. alts->desc.bInterfaceNumber);
  389. return -EINVAL;
  390. }
  391. switch (buffer[8] & 0x7f) {
  392. case 0:
  393. strlcpy(format->name, "SD-DV", sizeof format->name);
  394. break;
  395. case 1:
  396. strlcpy(format->name, "SDL-DV", sizeof format->name);
  397. break;
  398. case 2:
  399. strlcpy(format->name, "HD-DV", sizeof format->name);
  400. break;
  401. default:
  402. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  403. "interface %d: unknown DV format %u\n",
  404. dev->udev->devnum,
  405. alts->desc.bInterfaceNumber, buffer[8]);
  406. return -EINVAL;
  407. }
  408. strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
  409. sizeof format->name);
  410. format->fcc = V4L2_PIX_FMT_DV;
  411. format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
  412. format->bpp = 0;
  413. ftype = 0;
  414. /* Create a dummy frame descriptor. */
  415. frame = &format->frame[0];
  416. memset(&format->frame[0], 0, sizeof format->frame[0]);
  417. frame->bFrameIntervalType = 1;
  418. frame->dwDefaultFrameInterval = 1;
  419. frame->dwFrameInterval = *intervals;
  420. *(*intervals)++ = 1;
  421. format->nframes = 1;
  422. break;
  423. case UVC_VS_FORMAT_MPEG2TS:
  424. case UVC_VS_FORMAT_STREAM_BASED:
  425. /* Not supported yet. */
  426. default:
  427. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  428. "interface %d unsupported format %u\n",
  429. dev->udev->devnum, alts->desc.bInterfaceNumber,
  430. buffer[2]);
  431. return -EINVAL;
  432. }
  433. uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
  434. buflen -= buffer[0];
  435. buffer += buffer[0];
  436. /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
  437. * based formats have frame descriptors.
  438. */
  439. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  440. buffer[2] == ftype) {
  441. frame = &format->frame[format->nframes];
  442. if (ftype != UVC_VS_FRAME_FRAME_BASED)
  443. n = buflen > 25 ? buffer[25] : 0;
  444. else
  445. n = buflen > 21 ? buffer[21] : 0;
  446. n = n ? n : 3;
  447. if (buflen < 26 + 4*n) {
  448. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  449. "interface %d FRAME error\n", dev->udev->devnum,
  450. alts->desc.bInterfaceNumber);
  451. return -EINVAL;
  452. }
  453. frame->bFrameIndex = buffer[3];
  454. frame->bmCapabilities = buffer[4];
  455. frame->wWidth = get_unaligned_le16(&buffer[5])
  456. * width_multiplier;
  457. frame->wHeight = get_unaligned_le16(&buffer[7]);
  458. frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
  459. frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
  460. if (ftype != UVC_VS_FRAME_FRAME_BASED) {
  461. frame->dwMaxVideoFrameBufferSize =
  462. get_unaligned_le32(&buffer[17]);
  463. frame->dwDefaultFrameInterval =
  464. get_unaligned_le32(&buffer[21]);
  465. frame->bFrameIntervalType = buffer[25];
  466. } else {
  467. frame->dwMaxVideoFrameBufferSize = 0;
  468. frame->dwDefaultFrameInterval =
  469. get_unaligned_le32(&buffer[17]);
  470. frame->bFrameIntervalType = buffer[21];
  471. }
  472. frame->dwFrameInterval = *intervals;
  473. /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
  474. * completely. Observed behaviours range from setting the
  475. * value to 1.1x the actual frame size to hardwiring the
  476. * 16 low bits to 0. This results in a higher than necessary
  477. * memory usage as well as a wrong image size information. For
  478. * uncompressed formats this can be fixed by computing the
  479. * value from the frame size.
  480. */
  481. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
  482. frame->dwMaxVideoFrameBufferSize = format->bpp
  483. * frame->wWidth * frame->wHeight / 8;
  484. /* Some bogus devices report dwMinFrameInterval equal to
  485. * dwMaxFrameInterval and have dwFrameIntervalStep set to
  486. * zero. Setting all null intervals to 1 fixes the problem and
  487. * some other divisions by zero that could happen.
  488. */
  489. for (i = 0; i < n; ++i) {
  490. interval = get_unaligned_le32(&buffer[26+4*i]);
  491. *(*intervals)++ = interval ? interval : 1;
  492. }
  493. /* Make sure that the default frame interval stays between
  494. * the boundaries.
  495. */
  496. n -= frame->bFrameIntervalType ? 1 : 2;
  497. frame->dwDefaultFrameInterval =
  498. min(frame->dwFrameInterval[n],
  499. max(frame->dwFrameInterval[0],
  500. frame->dwDefaultFrameInterval));
  501. if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
  502. frame->bFrameIntervalType = 1;
  503. frame->dwFrameInterval[0] =
  504. frame->dwDefaultFrameInterval;
  505. }
  506. uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
  507. frame->wWidth, frame->wHeight,
  508. 10000000/frame->dwDefaultFrameInterval,
  509. (100000000/frame->dwDefaultFrameInterval)%10);
  510. format->nframes++;
  511. buflen -= buffer[0];
  512. buffer += buffer[0];
  513. }
  514. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  515. buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
  516. buflen -= buffer[0];
  517. buffer += buffer[0];
  518. }
  519. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  520. buffer[2] == UVC_VS_COLORFORMAT) {
  521. if (buflen < 6) {
  522. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  523. "interface %d COLORFORMAT error\n",
  524. dev->udev->devnum,
  525. alts->desc.bInterfaceNumber);
  526. return -EINVAL;
  527. }
  528. format->colorspace = uvc_colorspace(buffer[3]);
  529. buflen -= buffer[0];
  530. buffer += buffer[0];
  531. }
  532. return buffer - start;
  533. }
  534. static int uvc_parse_streaming(struct uvc_device *dev,
  535. struct usb_interface *intf)
  536. {
  537. struct uvc_streaming *streaming = NULL;
  538. struct uvc_format *format;
  539. struct uvc_frame *frame;
  540. struct usb_host_interface *alts = &intf->altsetting[0];
  541. unsigned char *_buffer, *buffer = alts->extra;
  542. int _buflen, buflen = alts->extralen;
  543. unsigned int nformats = 0, nframes = 0, nintervals = 0;
  544. unsigned int size, i, n, p;
  545. __u32 *interval;
  546. __u16 psize;
  547. int ret = -EINVAL;
  548. if (intf->cur_altsetting->desc.bInterfaceSubClass
  549. != UVC_SC_VIDEOSTREAMING) {
  550. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
  551. "video streaming interface\n", dev->udev->devnum,
  552. intf->altsetting[0].desc.bInterfaceNumber);
  553. return -EINVAL;
  554. }
  555. if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
  556. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
  557. "claimed\n", dev->udev->devnum,
  558. intf->altsetting[0].desc.bInterfaceNumber);
  559. return -EINVAL;
  560. }
  561. streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
  562. if (streaming == NULL) {
  563. usb_driver_release_interface(&uvc_driver.driver, intf);
  564. return -EINVAL;
  565. }
  566. mutex_init(&streaming->mutex);
  567. streaming->dev = dev;
  568. streaming->intf = usb_get_intf(intf);
  569. streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  570. /* The Pico iMage webcam has its class-specific interface descriptors
  571. * after the endpoint descriptors.
  572. */
  573. if (buflen == 0) {
  574. for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
  575. struct usb_host_endpoint *ep = &alts->endpoint[i];
  576. if (ep->extralen == 0)
  577. continue;
  578. if (ep->extralen > 2 &&
  579. ep->extra[1] == USB_DT_CS_INTERFACE) {
  580. uvc_trace(UVC_TRACE_DESCR, "trying extra data "
  581. "from endpoint %u.\n", i);
  582. buffer = alts->endpoint[i].extra;
  583. buflen = alts->endpoint[i].extralen;
  584. break;
  585. }
  586. }
  587. }
  588. /* Skip the standard interface descriptors. */
  589. while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
  590. buflen -= buffer[0];
  591. buffer += buffer[0];
  592. }
  593. if (buflen <= 2) {
  594. uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
  595. "interface descriptors found.\n");
  596. goto error;
  597. }
  598. /* Parse the header descriptor. */
  599. switch (buffer[2]) {
  600. case UVC_VS_OUTPUT_HEADER:
  601. streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  602. size = 9;
  603. break;
  604. case UVC_VS_INPUT_HEADER:
  605. streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  606. size = 13;
  607. break;
  608. default:
  609. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  610. "%d HEADER descriptor not found.\n", dev->udev->devnum,
  611. alts->desc.bInterfaceNumber);
  612. goto error;
  613. }
  614. p = buflen >= 4 ? buffer[3] : 0;
  615. n = buflen >= size ? buffer[size-1] : 0;
  616. if (buflen < size + p*n) {
  617. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  618. "interface %d HEADER descriptor is invalid.\n",
  619. dev->udev->devnum, alts->desc.bInterfaceNumber);
  620. goto error;
  621. }
  622. streaming->header.bNumFormats = p;
  623. streaming->header.bEndpointAddress = buffer[6];
  624. if (buffer[2] == UVC_VS_INPUT_HEADER) {
  625. streaming->header.bmInfo = buffer[7];
  626. streaming->header.bTerminalLink = buffer[8];
  627. streaming->header.bStillCaptureMethod = buffer[9];
  628. streaming->header.bTriggerSupport = buffer[10];
  629. streaming->header.bTriggerUsage = buffer[11];
  630. } else {
  631. streaming->header.bTerminalLink = buffer[7];
  632. }
  633. streaming->header.bControlSize = n;
  634. streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
  635. GFP_KERNEL);
  636. if (streaming->header.bmaControls == NULL) {
  637. ret = -ENOMEM;
  638. goto error;
  639. }
  640. buflen -= buffer[0];
  641. buffer += buffer[0];
  642. _buffer = buffer;
  643. _buflen = buflen;
  644. /* Count the format and frame descriptors. */
  645. while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
  646. switch (_buffer[2]) {
  647. case UVC_VS_FORMAT_UNCOMPRESSED:
  648. case UVC_VS_FORMAT_MJPEG:
  649. case UVC_VS_FORMAT_FRAME_BASED:
  650. nformats++;
  651. break;
  652. case UVC_VS_FORMAT_DV:
  653. /* DV format has no frame descriptor. We will create a
  654. * dummy frame descriptor with a dummy frame interval.
  655. */
  656. nformats++;
  657. nframes++;
  658. nintervals++;
  659. break;
  660. case UVC_VS_FORMAT_MPEG2TS:
  661. case UVC_VS_FORMAT_STREAM_BASED:
  662. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  663. "interface %d FORMAT %u is not supported.\n",
  664. dev->udev->devnum,
  665. alts->desc.bInterfaceNumber, _buffer[2]);
  666. break;
  667. case UVC_VS_FRAME_UNCOMPRESSED:
  668. case UVC_VS_FRAME_MJPEG:
  669. nframes++;
  670. if (_buflen > 25)
  671. nintervals += _buffer[25] ? _buffer[25] : 3;
  672. break;
  673. case UVC_VS_FRAME_FRAME_BASED:
  674. nframes++;
  675. if (_buflen > 21)
  676. nintervals += _buffer[21] ? _buffer[21] : 3;
  677. break;
  678. }
  679. _buflen -= _buffer[0];
  680. _buffer += _buffer[0];
  681. }
  682. if (nformats == 0) {
  683. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  684. "%d has no supported formats defined.\n",
  685. dev->udev->devnum, alts->desc.bInterfaceNumber);
  686. goto error;
  687. }
  688. size = nformats * sizeof *format + nframes * sizeof *frame
  689. + nintervals * sizeof *interval;
  690. format = kzalloc(size, GFP_KERNEL);
  691. if (format == NULL) {
  692. ret = -ENOMEM;
  693. goto error;
  694. }
  695. frame = (struct uvc_frame *)&format[nformats];
  696. interval = (__u32 *)&frame[nframes];
  697. streaming->format = format;
  698. streaming->nformats = nformats;
  699. /* Parse the format descriptors. */
  700. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
  701. switch (buffer[2]) {
  702. case UVC_VS_FORMAT_UNCOMPRESSED:
  703. case UVC_VS_FORMAT_MJPEG:
  704. case UVC_VS_FORMAT_DV:
  705. case UVC_VS_FORMAT_FRAME_BASED:
  706. format->frame = frame;
  707. ret = uvc_parse_format(dev, streaming, format,
  708. &interval, buffer, buflen);
  709. if (ret < 0)
  710. goto error;
  711. frame += format->nframes;
  712. format++;
  713. buflen -= ret;
  714. buffer += ret;
  715. continue;
  716. default:
  717. break;
  718. }
  719. buflen -= buffer[0];
  720. buffer += buffer[0];
  721. }
  722. if (buflen)
  723. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  724. "%d has %u bytes of trailing descriptor garbage.\n",
  725. dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
  726. /* Parse the alternate settings to find the maximum bandwidth. */
  727. for (i = 0; i < intf->num_altsetting; ++i) {
  728. struct usb_host_endpoint *ep;
  729. alts = &intf->altsetting[i];
  730. ep = uvc_find_endpoint(alts,
  731. streaming->header.bEndpointAddress);
  732. if (ep == NULL)
  733. continue;
  734. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  735. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  736. if (psize > streaming->maxpsize)
  737. streaming->maxpsize = psize;
  738. }
  739. list_add_tail(&streaming->list, &dev->streams);
  740. return 0;
  741. error:
  742. usb_driver_release_interface(&uvc_driver.driver, intf);
  743. usb_put_intf(intf);
  744. kfree(streaming->format);
  745. kfree(streaming->header.bmaControls);
  746. kfree(streaming);
  747. return ret;
  748. }
  749. static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
  750. unsigned int num_pads, unsigned int extra_size)
  751. {
  752. struct uvc_entity *entity;
  753. unsigned int num_inputs;
  754. unsigned int size;
  755. unsigned int i;
  756. extra_size = ALIGN(extra_size, sizeof(*entity->pads));
  757. num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
  758. size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
  759. + num_inputs;
  760. entity = kzalloc(size, GFP_KERNEL);
  761. if (entity == NULL)
  762. return NULL;
  763. entity->id = id;
  764. entity->type = type;
  765. entity->num_links = 0;
  766. entity->num_pads = num_pads;
  767. entity->pads = ((void *)(entity + 1)) + extra_size;
  768. for (i = 0; i < num_inputs; ++i)
  769. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  770. if (!UVC_ENTITY_IS_OTERM(entity))
  771. entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
  772. entity->bNrInPins = num_inputs;
  773. entity->baSourceID = (__u8 *)(&entity->pads[num_pads]);
  774. return entity;
  775. }
  776. /* Parse vendor-specific extensions. */
  777. static int uvc_parse_vendor_control(struct uvc_device *dev,
  778. const unsigned char *buffer, int buflen)
  779. {
  780. struct usb_device *udev = dev->udev;
  781. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  782. struct uvc_entity *unit;
  783. unsigned int n, p;
  784. int handled = 0;
  785. switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
  786. case 0x046d: /* Logitech */
  787. if (buffer[1] != 0x41 || buffer[2] != 0x01)
  788. break;
  789. /* Logitech implements several vendor specific functions
  790. * through vendor specific extension units (LXU).
  791. *
  792. * The LXU descriptors are similar to XU descriptors
  793. * (see "USB Device Video Class for Video Devices", section
  794. * 3.7.2.6 "Extension Unit Descriptor") with the following
  795. * differences:
  796. *
  797. * ----------------------------------------------------------
  798. * 0 bLength 1 Number
  799. * Size of this descriptor, in bytes: 24+p+n*2
  800. * ----------------------------------------------------------
  801. * 23+p+n bmControlsType N Bitmap
  802. * Individual bits in the set are defined:
  803. * 0: Absolute
  804. * 1: Relative
  805. *
  806. * This bitset is mapped exactly the same as bmControls.
  807. * ----------------------------------------------------------
  808. * 23+p+n*2 bReserved 1 Boolean
  809. * ----------------------------------------------------------
  810. * 24+p+n*2 iExtension 1 Index
  811. * Index of a string descriptor that describes this
  812. * extension unit.
  813. * ----------------------------------------------------------
  814. */
  815. p = buflen >= 22 ? buffer[21] : 0;
  816. n = buflen >= 25 + p ? buffer[22+p] : 0;
  817. if (buflen < 25 + p + 2*n) {
  818. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  819. "interface %d EXTENSION_UNIT error\n",
  820. udev->devnum, alts->desc.bInterfaceNumber);
  821. break;
  822. }
  823. unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
  824. p + 1, 2*n);
  825. if (unit == NULL)
  826. return -ENOMEM;
  827. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  828. unit->extension.bNumControls = buffer[20];
  829. memcpy(unit->baSourceID, &buffer[22], p);
  830. unit->extension.bControlSize = buffer[22+p];
  831. unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
  832. unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
  833. + n;
  834. memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
  835. if (buffer[24+p+2*n] != 0)
  836. usb_string(udev, buffer[24+p+2*n], unit->name,
  837. sizeof unit->name);
  838. else
  839. sprintf(unit->name, "Extension %u", buffer[3]);
  840. list_add_tail(&unit->list, &dev->entities);
  841. handled = 1;
  842. break;
  843. }
  844. return handled;
  845. }
  846. static int uvc_parse_standard_control(struct uvc_device *dev,
  847. const unsigned char *buffer, int buflen)
  848. {
  849. struct usb_device *udev = dev->udev;
  850. struct uvc_entity *unit, *term;
  851. struct usb_interface *intf;
  852. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  853. unsigned int i, n, p, len;
  854. __u16 type;
  855. switch (buffer[2]) {
  856. case UVC_VC_HEADER:
  857. n = buflen >= 12 ? buffer[11] : 0;
  858. if (buflen < 12 + n) {
  859. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  860. "interface %d HEADER error\n", udev->devnum,
  861. alts->desc.bInterfaceNumber);
  862. return -EINVAL;
  863. }
  864. dev->uvc_version = get_unaligned_le16(&buffer[3]);
  865. dev->clock_frequency = get_unaligned_le32(&buffer[7]);
  866. /* Parse all USB Video Streaming interfaces. */
  867. for (i = 0; i < n; ++i) {
  868. intf = usb_ifnum_to_if(udev, buffer[12+i]);
  869. if (intf == NULL) {
  870. uvc_trace(UVC_TRACE_DESCR, "device %d "
  871. "interface %d doesn't exists\n",
  872. udev->devnum, i);
  873. continue;
  874. }
  875. uvc_parse_streaming(dev, intf);
  876. }
  877. break;
  878. case UVC_VC_INPUT_TERMINAL:
  879. if (buflen < 8) {
  880. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  881. "interface %d INPUT_TERMINAL error\n",
  882. udev->devnum, alts->desc.bInterfaceNumber);
  883. return -EINVAL;
  884. }
  885. /*
  886. * Reject invalid terminal types that would cause issues:
  887. *
  888. * - The high byte must be non-zero, otherwise it would be
  889. * confused with a unit.
  890. *
  891. * - Bit 15 must be 0, as we use it internally as a terminal
  892. * direction flag.
  893. *
  894. * Other unknown types are accepted.
  895. */
  896. type = get_unaligned_le16(&buffer[4]);
  897. if ((type & 0x7f00) == 0 || (type & 0x8000) != 0) {
  898. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  899. "interface %d INPUT_TERMINAL %d has invalid "
  900. "type 0x%04x, skipping\n", udev->devnum,
  901. alts->desc.bInterfaceNumber,
  902. buffer[3], type);
  903. return 0;
  904. }
  905. n = 0;
  906. p = 0;
  907. len = 8;
  908. if (type == UVC_ITT_CAMERA) {
  909. n = buflen >= 15 ? buffer[14] : 0;
  910. len = 15;
  911. } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  912. n = buflen >= 9 ? buffer[8] : 0;
  913. p = buflen >= 10 + n ? buffer[9+n] : 0;
  914. len = 10;
  915. }
  916. if (buflen < len + n + p) {
  917. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  918. "interface %d INPUT_TERMINAL error\n",
  919. udev->devnum, alts->desc.bInterfaceNumber);
  920. return -EINVAL;
  921. }
  922. term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
  923. 1, n + p);
  924. if (term == NULL)
  925. return -ENOMEM;
  926. if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
  927. term->camera.bControlSize = n;
  928. term->camera.bmControls = (__u8 *)term + sizeof *term;
  929. term->camera.wObjectiveFocalLengthMin =
  930. get_unaligned_le16(&buffer[8]);
  931. term->camera.wObjectiveFocalLengthMax =
  932. get_unaligned_le16(&buffer[10]);
  933. term->camera.wOcularFocalLength =
  934. get_unaligned_le16(&buffer[12]);
  935. memcpy(term->camera.bmControls, &buffer[15], n);
  936. } else if (UVC_ENTITY_TYPE(term) ==
  937. UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  938. term->media.bControlSize = n;
  939. term->media.bmControls = (__u8 *)term + sizeof *term;
  940. term->media.bTransportModeSize = p;
  941. term->media.bmTransportModes = (__u8 *)term
  942. + sizeof *term + n;
  943. memcpy(term->media.bmControls, &buffer[9], n);
  944. memcpy(term->media.bmTransportModes, &buffer[10+n], p);
  945. }
  946. if (buffer[7] != 0)
  947. usb_string(udev, buffer[7], term->name,
  948. sizeof term->name);
  949. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
  950. sprintf(term->name, "Camera %u", buffer[3]);
  951. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
  952. sprintf(term->name, "Media %u", buffer[3]);
  953. else
  954. sprintf(term->name, "Input %u", buffer[3]);
  955. list_add_tail(&term->list, &dev->entities);
  956. break;
  957. case UVC_VC_OUTPUT_TERMINAL:
  958. if (buflen < 9) {
  959. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  960. "interface %d OUTPUT_TERMINAL error\n",
  961. udev->devnum, alts->desc.bInterfaceNumber);
  962. return -EINVAL;
  963. }
  964. /* Make sure the terminal type MSB is not null, otherwise it
  965. * could be confused with a unit.
  966. */
  967. type = get_unaligned_le16(&buffer[4]);
  968. if ((type & 0xff00) == 0) {
  969. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  970. "interface %d OUTPUT_TERMINAL %d has invalid "
  971. "type 0x%04x, skipping\n", udev->devnum,
  972. alts->desc.bInterfaceNumber, buffer[3], type);
  973. return 0;
  974. }
  975. term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
  976. 1, 0);
  977. if (term == NULL)
  978. return -ENOMEM;
  979. memcpy(term->baSourceID, &buffer[7], 1);
  980. if (buffer[8] != 0)
  981. usb_string(udev, buffer[8], term->name,
  982. sizeof term->name);
  983. else
  984. sprintf(term->name, "Output %u", buffer[3]);
  985. list_add_tail(&term->list, &dev->entities);
  986. break;
  987. case UVC_VC_SELECTOR_UNIT:
  988. p = buflen >= 5 ? buffer[4] : 0;
  989. if (buflen < 5 || buflen < 6 + p) {
  990. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  991. "interface %d SELECTOR_UNIT error\n",
  992. udev->devnum, alts->desc.bInterfaceNumber);
  993. return -EINVAL;
  994. }
  995. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
  996. if (unit == NULL)
  997. return -ENOMEM;
  998. memcpy(unit->baSourceID, &buffer[5], p);
  999. if (buffer[5+p] != 0)
  1000. usb_string(udev, buffer[5+p], unit->name,
  1001. sizeof unit->name);
  1002. else
  1003. sprintf(unit->name, "Selector %u", buffer[3]);
  1004. list_add_tail(&unit->list, &dev->entities);
  1005. break;
  1006. case UVC_VC_PROCESSING_UNIT:
  1007. n = buflen >= 8 ? buffer[7] : 0;
  1008. p = dev->uvc_version >= 0x0110 ? 10 : 9;
  1009. if (buflen < p + n) {
  1010. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  1011. "interface %d PROCESSING_UNIT error\n",
  1012. udev->devnum, alts->desc.bInterfaceNumber);
  1013. return -EINVAL;
  1014. }
  1015. unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
  1016. if (unit == NULL)
  1017. return -ENOMEM;
  1018. memcpy(unit->baSourceID, &buffer[4], 1);
  1019. unit->processing.wMaxMultiplier =
  1020. get_unaligned_le16(&buffer[5]);
  1021. unit->processing.bControlSize = buffer[7];
  1022. unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
  1023. memcpy(unit->processing.bmControls, &buffer[8], n);
  1024. if (dev->uvc_version >= 0x0110)
  1025. unit->processing.bmVideoStandards = buffer[9+n];
  1026. if (buffer[8+n] != 0)
  1027. usb_string(udev, buffer[8+n], unit->name,
  1028. sizeof unit->name);
  1029. else
  1030. sprintf(unit->name, "Processing %u", buffer[3]);
  1031. list_add_tail(&unit->list, &dev->entities);
  1032. break;
  1033. case UVC_VC_EXTENSION_UNIT:
  1034. p = buflen >= 22 ? buffer[21] : 0;
  1035. n = buflen >= 24 + p ? buffer[22+p] : 0;
  1036. if (buflen < 24 + p + n) {
  1037. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  1038. "interface %d EXTENSION_UNIT error\n",
  1039. udev->devnum, alts->desc.bInterfaceNumber);
  1040. return -EINVAL;
  1041. }
  1042. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
  1043. if (unit == NULL)
  1044. return -ENOMEM;
  1045. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  1046. unit->extension.bNumControls = buffer[20];
  1047. memcpy(unit->baSourceID, &buffer[22], p);
  1048. unit->extension.bControlSize = buffer[22+p];
  1049. unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
  1050. memcpy(unit->extension.bmControls, &buffer[23+p], n);
  1051. if (buffer[23+p+n] != 0)
  1052. usb_string(udev, buffer[23+p+n], unit->name,
  1053. sizeof unit->name);
  1054. else
  1055. sprintf(unit->name, "Extension %u", buffer[3]);
  1056. list_add_tail(&unit->list, &dev->entities);
  1057. break;
  1058. default:
  1059. uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
  1060. "descriptor (%u)\n", buffer[2]);
  1061. break;
  1062. }
  1063. return 0;
  1064. }
  1065. static int uvc_parse_control(struct uvc_device *dev)
  1066. {
  1067. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  1068. unsigned char *buffer = alts->extra;
  1069. int buflen = alts->extralen;
  1070. int ret;
  1071. /* Parse the default alternate setting only, as the UVC specification
  1072. * defines a single alternate setting, the default alternate setting
  1073. * zero.
  1074. */
  1075. while (buflen > 2) {
  1076. if (uvc_parse_vendor_control(dev, buffer, buflen) ||
  1077. buffer[1] != USB_DT_CS_INTERFACE)
  1078. goto next_descriptor;
  1079. if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
  1080. return ret;
  1081. next_descriptor:
  1082. buflen -= buffer[0];
  1083. buffer += buffer[0];
  1084. }
  1085. /* Check if the optional status endpoint is present. Built-in iSight
  1086. * webcams have an interrupt endpoint but spit proprietary data that
  1087. * don't conform to the UVC status endpoint messages. Don't try to
  1088. * handle the interrupt endpoint for those cameras.
  1089. */
  1090. if (alts->desc.bNumEndpoints == 1 &&
  1091. !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
  1092. struct usb_host_endpoint *ep = &alts->endpoint[0];
  1093. struct usb_endpoint_descriptor *desc = &ep->desc;
  1094. if (usb_endpoint_is_int_in(desc) &&
  1095. le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
  1096. desc->bInterval != 0) {
  1097. uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
  1098. "(addr %02x).\n", desc->bEndpointAddress);
  1099. dev->int_ep = ep;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. /* ------------------------------------------------------------------------
  1105. * UVC device scan
  1106. */
  1107. /*
  1108. * Scan the UVC descriptors to locate a chain starting at an Output Terminal
  1109. * and containing the following units:
  1110. *
  1111. * - one or more Output Terminals (USB Streaming or Display)
  1112. * - zero or one Processing Unit
  1113. * - zero, one or more single-input Selector Units
  1114. * - zero or one multiple-input Selector Units, provided all inputs are
  1115. * connected to input terminals
  1116. * - zero, one or mode single-input Extension Units
  1117. * - one or more Input Terminals (Camera, External or USB Streaming)
  1118. *
  1119. * The terminal and units must match on of the following structures:
  1120. *
  1121. * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
  1122. * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
  1123. * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
  1124. *
  1125. * +---------+ +---------+ -> OTT_*(0)
  1126. * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
  1127. * +---------+ +---------+ -> OTT_*(n)
  1128. *
  1129. * The Processing Unit and Extension Units can be in any order. Additional
  1130. * Extension Units connected to the main chain as single-unit branches are
  1131. * also supported. Single-input Selector Units are ignored.
  1132. */
  1133. static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
  1134. struct uvc_entity *entity)
  1135. {
  1136. switch (UVC_ENTITY_TYPE(entity)) {
  1137. case UVC_VC_EXTENSION_UNIT:
  1138. if (uvc_trace_param & UVC_TRACE_PROBE)
  1139. printk(" <- XU %d", entity->id);
  1140. if (entity->bNrInPins != 1) {
  1141. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
  1142. "than 1 input pin.\n", entity->id);
  1143. return -1;
  1144. }
  1145. break;
  1146. case UVC_VC_PROCESSING_UNIT:
  1147. if (uvc_trace_param & UVC_TRACE_PROBE)
  1148. printk(" <- PU %d", entity->id);
  1149. if (chain->processing != NULL) {
  1150. uvc_trace(UVC_TRACE_DESCR, "Found multiple "
  1151. "Processing Units in chain.\n");
  1152. return -1;
  1153. }
  1154. chain->processing = entity;
  1155. break;
  1156. case UVC_VC_SELECTOR_UNIT:
  1157. if (uvc_trace_param & UVC_TRACE_PROBE)
  1158. printk(" <- SU %d", entity->id);
  1159. /* Single-input selector units are ignored. */
  1160. if (entity->bNrInPins == 1)
  1161. break;
  1162. if (chain->selector != NULL) {
  1163. uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
  1164. "Units in chain.\n");
  1165. return -1;
  1166. }
  1167. chain->selector = entity;
  1168. break;
  1169. case UVC_ITT_VENDOR_SPECIFIC:
  1170. case UVC_ITT_CAMERA:
  1171. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1172. if (uvc_trace_param & UVC_TRACE_PROBE)
  1173. printk(" <- IT %d\n", entity->id);
  1174. break;
  1175. case UVC_OTT_VENDOR_SPECIFIC:
  1176. case UVC_OTT_DISPLAY:
  1177. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1178. if (uvc_trace_param & UVC_TRACE_PROBE)
  1179. printk(" OT %d", entity->id);
  1180. break;
  1181. case UVC_TT_STREAMING:
  1182. if (UVC_ENTITY_IS_ITERM(entity)) {
  1183. if (uvc_trace_param & UVC_TRACE_PROBE)
  1184. printk(" <- IT %d\n", entity->id);
  1185. } else {
  1186. if (uvc_trace_param & UVC_TRACE_PROBE)
  1187. printk(" OT %d", entity->id);
  1188. }
  1189. break;
  1190. default:
  1191. uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
  1192. "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
  1193. return -1;
  1194. }
  1195. list_add_tail(&entity->chain, &chain->entities);
  1196. return 0;
  1197. }
  1198. static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
  1199. struct uvc_entity *entity, struct uvc_entity *prev)
  1200. {
  1201. struct uvc_entity *forward;
  1202. int found;
  1203. /* Forward scan */
  1204. forward = NULL;
  1205. found = 0;
  1206. while (1) {
  1207. forward = uvc_entity_by_reference(chain->dev, entity->id,
  1208. forward);
  1209. if (forward == NULL)
  1210. break;
  1211. if (forward == prev)
  1212. continue;
  1213. switch (UVC_ENTITY_TYPE(forward)) {
  1214. case UVC_VC_EXTENSION_UNIT:
  1215. if (forward->bNrInPins != 1) {
  1216. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
  1217. "has more than 1 input pin.\n",
  1218. entity->id);
  1219. return -EINVAL;
  1220. }
  1221. list_add_tail(&forward->chain, &chain->entities);
  1222. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1223. if (!found)
  1224. printk(" (->");
  1225. printk(" XU %d", forward->id);
  1226. found = 1;
  1227. }
  1228. break;
  1229. case UVC_OTT_VENDOR_SPECIFIC:
  1230. case UVC_OTT_DISPLAY:
  1231. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1232. case UVC_TT_STREAMING:
  1233. if (UVC_ENTITY_IS_ITERM(forward)) {
  1234. uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
  1235. "terminal %u.\n", forward->id);
  1236. return -EINVAL;
  1237. }
  1238. list_add_tail(&forward->chain, &chain->entities);
  1239. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1240. if (!found)
  1241. printk(" (->");
  1242. printk(" OT %d", forward->id);
  1243. found = 1;
  1244. }
  1245. break;
  1246. }
  1247. }
  1248. if (found)
  1249. printk(")");
  1250. return 0;
  1251. }
  1252. static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
  1253. struct uvc_entity **_entity)
  1254. {
  1255. struct uvc_entity *entity = *_entity;
  1256. struct uvc_entity *term;
  1257. int id = -EINVAL, i;
  1258. switch (UVC_ENTITY_TYPE(entity)) {
  1259. case UVC_VC_EXTENSION_UNIT:
  1260. case UVC_VC_PROCESSING_UNIT:
  1261. id = entity->baSourceID[0];
  1262. break;
  1263. case UVC_VC_SELECTOR_UNIT:
  1264. /* Single-input selector units are ignored. */
  1265. if (entity->bNrInPins == 1) {
  1266. id = entity->baSourceID[0];
  1267. break;
  1268. }
  1269. if (uvc_trace_param & UVC_TRACE_PROBE)
  1270. printk(" <- IT");
  1271. chain->selector = entity;
  1272. for (i = 0; i < entity->bNrInPins; ++i) {
  1273. id = entity->baSourceID[i];
  1274. term = uvc_entity_by_id(chain->dev, id);
  1275. if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
  1276. uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
  1277. "input %d isn't connected to an "
  1278. "input terminal\n", entity->id, i);
  1279. return -1;
  1280. }
  1281. if (uvc_trace_param & UVC_TRACE_PROBE)
  1282. printk(" %d", term->id);
  1283. list_add_tail(&term->chain, &chain->entities);
  1284. uvc_scan_chain_forward(chain, term, entity);
  1285. }
  1286. if (uvc_trace_param & UVC_TRACE_PROBE)
  1287. printk("\n");
  1288. id = 0;
  1289. break;
  1290. case UVC_ITT_VENDOR_SPECIFIC:
  1291. case UVC_ITT_CAMERA:
  1292. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1293. case UVC_OTT_VENDOR_SPECIFIC:
  1294. case UVC_OTT_DISPLAY:
  1295. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1296. case UVC_TT_STREAMING:
  1297. id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
  1298. break;
  1299. }
  1300. if (id <= 0) {
  1301. *_entity = NULL;
  1302. return id;
  1303. }
  1304. entity = uvc_entity_by_id(chain->dev, id);
  1305. if (entity == NULL) {
  1306. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1307. "unknown entity %d.\n", id);
  1308. return -EINVAL;
  1309. }
  1310. *_entity = entity;
  1311. return 0;
  1312. }
  1313. static int uvc_scan_chain(struct uvc_video_chain *chain,
  1314. struct uvc_entity *term)
  1315. {
  1316. struct uvc_entity *entity, *prev;
  1317. uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
  1318. entity = term;
  1319. prev = NULL;
  1320. while (entity != NULL) {
  1321. /* Entity must not be part of an existing chain */
  1322. if (entity->chain.next || entity->chain.prev) {
  1323. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1324. "entity %d already in chain.\n", entity->id);
  1325. return -EINVAL;
  1326. }
  1327. /* Process entity */
  1328. if (uvc_scan_chain_entity(chain, entity) < 0)
  1329. return -EINVAL;
  1330. /* Forward scan */
  1331. if (uvc_scan_chain_forward(chain, entity, prev) < 0)
  1332. return -EINVAL;
  1333. /* Backward scan */
  1334. prev = entity;
  1335. if (uvc_scan_chain_backward(chain, &entity) < 0)
  1336. return -EINVAL;
  1337. }
  1338. return 0;
  1339. }
  1340. static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
  1341. char *buffer)
  1342. {
  1343. struct uvc_entity *term;
  1344. unsigned int nterms = 0;
  1345. char *p = buffer;
  1346. list_for_each_entry(term, terms, chain) {
  1347. if (!UVC_ENTITY_IS_TERM(term) ||
  1348. UVC_TERM_DIRECTION(term) != dir)
  1349. continue;
  1350. if (nterms)
  1351. p += sprintf(p, ",");
  1352. if (++nterms >= 4) {
  1353. p += sprintf(p, "...");
  1354. break;
  1355. }
  1356. p += sprintf(p, "%u", term->id);
  1357. }
  1358. return p - buffer;
  1359. }
  1360. static const char *uvc_print_chain(struct uvc_video_chain *chain)
  1361. {
  1362. static char buffer[43];
  1363. char *p = buffer;
  1364. p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
  1365. p += sprintf(p, " -> ");
  1366. uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
  1367. return buffer;
  1368. }
  1369. static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
  1370. {
  1371. struct uvc_video_chain *chain;
  1372. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  1373. if (chain == NULL)
  1374. return NULL;
  1375. INIT_LIST_HEAD(&chain->entities);
  1376. mutex_init(&chain->ctrl_mutex);
  1377. chain->dev = dev;
  1378. v4l2_prio_init(&chain->prio);
  1379. return chain;
  1380. }
  1381. /*
  1382. * Fallback heuristic for devices that don't connect units and terminals in a
  1383. * valid chain.
  1384. *
  1385. * Some devices have invalid baSourceID references, causing uvc_scan_chain()
  1386. * to fail, but if we just take the entities we can find and put them together
  1387. * in the most sensible chain we can think of, turns out they do work anyway.
  1388. * Note: This heuristic assumes there is a single chain.
  1389. *
  1390. * At the time of writing, devices known to have such a broken chain are
  1391. * - Acer Integrated Camera (5986:055a)
  1392. * - Realtek rtl157a7 (0bda:57a7)
  1393. */
  1394. static int uvc_scan_fallback(struct uvc_device *dev)
  1395. {
  1396. struct uvc_video_chain *chain;
  1397. struct uvc_entity *iterm = NULL;
  1398. struct uvc_entity *oterm = NULL;
  1399. struct uvc_entity *entity;
  1400. struct uvc_entity *prev;
  1401. /*
  1402. * Start by locating the input and output terminals. We only support
  1403. * devices with exactly one of each for now.
  1404. */
  1405. list_for_each_entry(entity, &dev->entities, list) {
  1406. if (UVC_ENTITY_IS_ITERM(entity)) {
  1407. if (iterm)
  1408. return -EINVAL;
  1409. iterm = entity;
  1410. }
  1411. if (UVC_ENTITY_IS_OTERM(entity)) {
  1412. if (oterm)
  1413. return -EINVAL;
  1414. oterm = entity;
  1415. }
  1416. }
  1417. if (iterm == NULL || oterm == NULL)
  1418. return -EINVAL;
  1419. /* Allocate the chain and fill it. */
  1420. chain = uvc_alloc_chain(dev);
  1421. if (chain == NULL)
  1422. return -ENOMEM;
  1423. if (uvc_scan_chain_entity(chain, oterm) < 0)
  1424. goto error;
  1425. prev = oterm;
  1426. /*
  1427. * Add all Processing and Extension Units with two pads. The order
  1428. * doesn't matter much, use reverse list traversal to connect units in
  1429. * UVC descriptor order as we build the chain from output to input. This
  1430. * leads to units appearing in the order meant by the manufacturer for
  1431. * the cameras known to require this heuristic.
  1432. */
  1433. list_for_each_entry_reverse(entity, &dev->entities, list) {
  1434. if (entity->type != UVC_VC_PROCESSING_UNIT &&
  1435. entity->type != UVC_VC_EXTENSION_UNIT)
  1436. continue;
  1437. if (entity->num_pads != 2)
  1438. continue;
  1439. if (uvc_scan_chain_entity(chain, entity) < 0)
  1440. goto error;
  1441. prev->baSourceID[0] = entity->id;
  1442. prev = entity;
  1443. }
  1444. if (uvc_scan_chain_entity(chain, iterm) < 0)
  1445. goto error;
  1446. prev->baSourceID[0] = iterm->id;
  1447. list_add_tail(&chain->list, &dev->chains);
  1448. uvc_trace(UVC_TRACE_PROBE,
  1449. "Found a video chain by fallback heuristic (%s).\n",
  1450. uvc_print_chain(chain));
  1451. return 0;
  1452. error:
  1453. kfree(chain);
  1454. return -EINVAL;
  1455. }
  1456. /*
  1457. * Scan the device for video chains and register video devices.
  1458. *
  1459. * Chains are scanned starting at their output terminals and walked backwards.
  1460. */
  1461. static int uvc_scan_device(struct uvc_device *dev)
  1462. {
  1463. struct uvc_video_chain *chain;
  1464. struct uvc_entity *term;
  1465. list_for_each_entry(term, &dev->entities, list) {
  1466. if (!UVC_ENTITY_IS_OTERM(term))
  1467. continue;
  1468. /* If the terminal is already included in a chain, skip it.
  1469. * This can happen for chains that have multiple output
  1470. * terminals, where all output terminals beside the first one
  1471. * will be inserted in the chain in forward scans.
  1472. */
  1473. if (term->chain.next || term->chain.prev)
  1474. continue;
  1475. chain = uvc_alloc_chain(dev);
  1476. if (chain == NULL)
  1477. return -ENOMEM;
  1478. term->flags |= UVC_ENTITY_FLAG_DEFAULT;
  1479. if (uvc_scan_chain(chain, term) < 0) {
  1480. kfree(chain);
  1481. continue;
  1482. }
  1483. uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
  1484. uvc_print_chain(chain));
  1485. list_add_tail(&chain->list, &dev->chains);
  1486. }
  1487. if (list_empty(&dev->chains))
  1488. uvc_scan_fallback(dev);
  1489. if (list_empty(&dev->chains)) {
  1490. uvc_printk(KERN_INFO, "No valid video chain found.\n");
  1491. return -1;
  1492. }
  1493. return 0;
  1494. }
  1495. /* ------------------------------------------------------------------------
  1496. * Video device registration and unregistration
  1497. */
  1498. /*
  1499. * Delete the UVC device.
  1500. *
  1501. * Called by the kernel when the last reference to the uvc_device structure
  1502. * is released.
  1503. *
  1504. * As this function is called after or during disconnect(), all URBs have
  1505. * already been canceled by the USB core. There is no need to kill the
  1506. * interrupt URB manually.
  1507. */
  1508. static void uvc_delete(struct uvc_device *dev)
  1509. {
  1510. struct list_head *p, *n;
  1511. uvc_status_cleanup(dev);
  1512. uvc_ctrl_cleanup_device(dev);
  1513. usb_put_intf(dev->intf);
  1514. usb_put_dev(dev->udev);
  1515. if (dev->vdev.dev)
  1516. v4l2_device_unregister(&dev->vdev);
  1517. #ifdef CONFIG_MEDIA_CONTROLLER
  1518. if (media_devnode_is_registered(&dev->mdev.devnode))
  1519. media_device_unregister(&dev->mdev);
  1520. #endif
  1521. list_for_each_safe(p, n, &dev->chains) {
  1522. struct uvc_video_chain *chain;
  1523. chain = list_entry(p, struct uvc_video_chain, list);
  1524. kfree(chain);
  1525. }
  1526. list_for_each_safe(p, n, &dev->entities) {
  1527. struct uvc_entity *entity;
  1528. entity = list_entry(p, struct uvc_entity, list);
  1529. #ifdef CONFIG_MEDIA_CONTROLLER
  1530. uvc_mc_cleanup_entity(entity);
  1531. #endif
  1532. kfree(entity);
  1533. }
  1534. list_for_each_safe(p, n, &dev->streams) {
  1535. struct uvc_streaming *streaming;
  1536. streaming = list_entry(p, struct uvc_streaming, list);
  1537. usb_driver_release_interface(&uvc_driver.driver,
  1538. streaming->intf);
  1539. usb_put_intf(streaming->intf);
  1540. kfree(streaming->format);
  1541. kfree(streaming->header.bmaControls);
  1542. kfree(streaming);
  1543. }
  1544. kfree(dev);
  1545. }
  1546. static void uvc_release(struct video_device *vdev)
  1547. {
  1548. struct uvc_streaming *stream = video_get_drvdata(vdev);
  1549. struct uvc_device *dev = stream->dev;
  1550. /* Decrement the registered streams count and delete the device when it
  1551. * reaches zero.
  1552. */
  1553. if (atomic_dec_and_test(&dev->nstreams))
  1554. uvc_delete(dev);
  1555. }
  1556. /*
  1557. * Unregister the video devices.
  1558. */
  1559. static void uvc_unregister_video(struct uvc_device *dev)
  1560. {
  1561. struct uvc_streaming *stream;
  1562. /* Unregistering all video devices might result in uvc_delete() being
  1563. * called from inside the loop if there's no open file handle. To avoid
  1564. * that, increment the stream count before iterating over the streams
  1565. * and decrement it when done.
  1566. */
  1567. atomic_inc(&dev->nstreams);
  1568. list_for_each_entry(stream, &dev->streams, list) {
  1569. if (!video_is_registered(&stream->vdev))
  1570. continue;
  1571. video_unregister_device(&stream->vdev);
  1572. uvc_debugfs_cleanup_stream(stream);
  1573. }
  1574. /* Decrement the stream count and call uvc_delete explicitly if there
  1575. * are no stream left.
  1576. */
  1577. if (atomic_dec_and_test(&dev->nstreams))
  1578. uvc_delete(dev);
  1579. }
  1580. static int uvc_register_video(struct uvc_device *dev,
  1581. struct uvc_streaming *stream)
  1582. {
  1583. struct video_device *vdev = &stream->vdev;
  1584. int ret;
  1585. /* Initialize the video buffers queue. */
  1586. ret = uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
  1587. if (ret)
  1588. return ret;
  1589. /* Initialize the streaming interface with default streaming
  1590. * parameters.
  1591. */
  1592. ret = uvc_video_init(stream);
  1593. if (ret < 0) {
  1594. uvc_printk(KERN_ERR, "Failed to initialize the device "
  1595. "(%d).\n", ret);
  1596. return ret;
  1597. }
  1598. uvc_debugfs_init_stream(stream);
  1599. /* Register the device with V4L. */
  1600. /* We already hold a reference to dev->udev. The video device will be
  1601. * unregistered before the reference is released, so we don't need to
  1602. * get another one.
  1603. */
  1604. vdev->v4l2_dev = &dev->vdev;
  1605. vdev->fops = &uvc_fops;
  1606. vdev->ioctl_ops = &uvc_ioctl_ops;
  1607. vdev->release = uvc_release;
  1608. vdev->prio = &stream->chain->prio;
  1609. if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1610. vdev->vfl_dir = VFL_DIR_TX;
  1611. strlcpy(vdev->name, dev->name, sizeof vdev->name);
  1612. /* Set the driver data before calling video_register_device, otherwise
  1613. * uvc_v4l2_open might race us.
  1614. */
  1615. video_set_drvdata(vdev, stream);
  1616. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1617. if (ret < 0) {
  1618. uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
  1619. ret);
  1620. return ret;
  1621. }
  1622. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1623. stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE;
  1624. else
  1625. stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
  1626. atomic_inc(&dev->nstreams);
  1627. return 0;
  1628. }
  1629. /*
  1630. * Register all video devices in all chains.
  1631. */
  1632. static int uvc_register_terms(struct uvc_device *dev,
  1633. struct uvc_video_chain *chain)
  1634. {
  1635. struct uvc_streaming *stream;
  1636. struct uvc_entity *term;
  1637. int ret;
  1638. list_for_each_entry(term, &chain->entities, chain) {
  1639. if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
  1640. continue;
  1641. stream = uvc_stream_by_id(dev, term->id);
  1642. if (stream == NULL) {
  1643. uvc_printk(KERN_INFO, "No streaming interface found "
  1644. "for terminal %u.", term->id);
  1645. continue;
  1646. }
  1647. stream->chain = chain;
  1648. ret = uvc_register_video(dev, stream);
  1649. if (ret < 0)
  1650. return ret;
  1651. term->vdev = &stream->vdev;
  1652. }
  1653. return 0;
  1654. }
  1655. static int uvc_register_chains(struct uvc_device *dev)
  1656. {
  1657. struct uvc_video_chain *chain;
  1658. int ret;
  1659. list_for_each_entry(chain, &dev->chains, list) {
  1660. ret = uvc_register_terms(dev, chain);
  1661. if (ret < 0)
  1662. return ret;
  1663. #ifdef CONFIG_MEDIA_CONTROLLER
  1664. ret = uvc_mc_register_entities(chain);
  1665. if (ret < 0) {
  1666. uvc_printk(KERN_INFO, "Failed to register entites "
  1667. "(%d).\n", ret);
  1668. }
  1669. #endif
  1670. }
  1671. return 0;
  1672. }
  1673. /* ------------------------------------------------------------------------
  1674. * USB probe, disconnect, suspend and resume
  1675. */
  1676. static int uvc_probe(struct usb_interface *intf,
  1677. const struct usb_device_id *id)
  1678. {
  1679. struct usb_device *udev = interface_to_usbdev(intf);
  1680. struct uvc_device *dev;
  1681. int ret;
  1682. if (id->idVendor && id->idProduct)
  1683. uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
  1684. "(%04x:%04x)\n", udev->devpath, id->idVendor,
  1685. id->idProduct);
  1686. else
  1687. uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
  1688. udev->devpath);
  1689. /* Allocate memory for the device and initialize it. */
  1690. if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
  1691. return -ENOMEM;
  1692. INIT_LIST_HEAD(&dev->entities);
  1693. INIT_LIST_HEAD(&dev->chains);
  1694. INIT_LIST_HEAD(&dev->streams);
  1695. atomic_set(&dev->nstreams, 0);
  1696. atomic_set(&dev->nmappings, 0);
  1697. mutex_init(&dev->lock);
  1698. dev->udev = usb_get_dev(udev);
  1699. dev->intf = usb_get_intf(intf);
  1700. dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1701. dev->quirks = (uvc_quirks_param == -1)
  1702. ? id->driver_info : uvc_quirks_param;
  1703. if (udev->product != NULL)
  1704. strlcpy(dev->name, udev->product, sizeof dev->name);
  1705. else
  1706. snprintf(dev->name, sizeof dev->name,
  1707. "UVC Camera (%04x:%04x)",
  1708. le16_to_cpu(udev->descriptor.idVendor),
  1709. le16_to_cpu(udev->descriptor.idProduct));
  1710. /* Parse the Video Class control descriptor. */
  1711. if (uvc_parse_control(dev) < 0) {
  1712. uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
  1713. "descriptors.\n");
  1714. goto error;
  1715. }
  1716. uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
  1717. dev->uvc_version >> 8, dev->uvc_version & 0xff,
  1718. udev->product ? udev->product : "<unnamed>",
  1719. le16_to_cpu(udev->descriptor.idVendor),
  1720. le16_to_cpu(udev->descriptor.idProduct));
  1721. if (dev->quirks != id->driver_info) {
  1722. uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
  1723. "parameter for testing purpose.\n", dev->quirks);
  1724. uvc_printk(KERN_INFO, "Please report required quirks to the "
  1725. "linux-uvc-devel mailing list.\n");
  1726. }
  1727. /* Register the media and V4L2 devices. */
  1728. #ifdef CONFIG_MEDIA_CONTROLLER
  1729. dev->mdev.dev = &intf->dev;
  1730. strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
  1731. if (udev->serial)
  1732. strlcpy(dev->mdev.serial, udev->serial,
  1733. sizeof(dev->mdev.serial));
  1734. strcpy(dev->mdev.bus_info, udev->devpath);
  1735. dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
  1736. dev->mdev.driver_version = LINUX_VERSION_CODE;
  1737. if (media_device_register(&dev->mdev) < 0)
  1738. goto error;
  1739. dev->vdev.mdev = &dev->mdev;
  1740. #endif
  1741. if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
  1742. goto error;
  1743. /* Initialize controls. */
  1744. if (uvc_ctrl_init_device(dev) < 0)
  1745. goto error;
  1746. /* Scan the device for video chains. */
  1747. if (uvc_scan_device(dev) < 0)
  1748. goto error;
  1749. /* Register video device nodes. */
  1750. if (uvc_register_chains(dev) < 0)
  1751. goto error;
  1752. /* Save our data pointer in the interface data. */
  1753. usb_set_intfdata(intf, dev);
  1754. /* Initialize the interrupt URB. */
  1755. if ((ret = uvc_status_init(dev)) < 0) {
  1756. uvc_printk(KERN_INFO, "Unable to initialize the status "
  1757. "endpoint (%d), status interrupt will not be "
  1758. "supported.\n", ret);
  1759. }
  1760. uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
  1761. usb_enable_autosuspend(udev);
  1762. return 0;
  1763. error:
  1764. uvc_unregister_video(dev);
  1765. return -ENODEV;
  1766. }
  1767. static void uvc_disconnect(struct usb_interface *intf)
  1768. {
  1769. struct uvc_device *dev = usb_get_intfdata(intf);
  1770. /* Set the USB interface data to NULL. This can be done outside the
  1771. * lock, as there's no other reader.
  1772. */
  1773. usb_set_intfdata(intf, NULL);
  1774. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1775. UVC_SC_VIDEOSTREAMING)
  1776. return;
  1777. uvc_unregister_video(dev);
  1778. }
  1779. static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
  1780. {
  1781. struct uvc_device *dev = usb_get_intfdata(intf);
  1782. struct uvc_streaming *stream;
  1783. uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
  1784. intf->cur_altsetting->desc.bInterfaceNumber);
  1785. /* Controls are cached on the fly so they don't need to be saved. */
  1786. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1787. UVC_SC_VIDEOCONTROL) {
  1788. mutex_lock(&dev->lock);
  1789. if (dev->users)
  1790. uvc_status_stop(dev);
  1791. mutex_unlock(&dev->lock);
  1792. return 0;
  1793. }
  1794. list_for_each_entry(stream, &dev->streams, list) {
  1795. if (stream->intf == intf)
  1796. return uvc_video_suspend(stream);
  1797. }
  1798. uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
  1799. "mismatch.\n");
  1800. return -EINVAL;
  1801. }
  1802. static int __uvc_resume(struct usb_interface *intf, int reset)
  1803. {
  1804. struct uvc_device *dev = usb_get_intfdata(intf);
  1805. struct uvc_streaming *stream;
  1806. int ret = 0;
  1807. uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
  1808. intf->cur_altsetting->desc.bInterfaceNumber);
  1809. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1810. UVC_SC_VIDEOCONTROL) {
  1811. if (reset) {
  1812. ret = uvc_ctrl_restore_values(dev);
  1813. if (ret < 0)
  1814. return ret;
  1815. }
  1816. mutex_lock(&dev->lock);
  1817. if (dev->users)
  1818. ret = uvc_status_start(dev, GFP_NOIO);
  1819. mutex_unlock(&dev->lock);
  1820. return ret;
  1821. }
  1822. list_for_each_entry(stream, &dev->streams, list) {
  1823. if (stream->intf == intf) {
  1824. ret = uvc_video_resume(stream, reset);
  1825. if (ret < 0)
  1826. uvc_queue_streamoff(&stream->queue,
  1827. stream->queue.queue.type);
  1828. return ret;
  1829. }
  1830. }
  1831. uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
  1832. "mismatch.\n");
  1833. return -EINVAL;
  1834. }
  1835. static int uvc_resume(struct usb_interface *intf)
  1836. {
  1837. return __uvc_resume(intf, 0);
  1838. }
  1839. static int uvc_reset_resume(struct usb_interface *intf)
  1840. {
  1841. return __uvc_resume(intf, 1);
  1842. }
  1843. /* ------------------------------------------------------------------------
  1844. * Module parameters
  1845. */
  1846. static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
  1847. {
  1848. if (uvc_clock_param == CLOCK_MONOTONIC)
  1849. return sprintf(buffer, "CLOCK_MONOTONIC");
  1850. else
  1851. return sprintf(buffer, "CLOCK_REALTIME");
  1852. }
  1853. static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
  1854. {
  1855. if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
  1856. val += strlen("clock_");
  1857. if (strcasecmp(val, "monotonic") == 0)
  1858. uvc_clock_param = CLOCK_MONOTONIC;
  1859. else if (strcasecmp(val, "realtime") == 0)
  1860. uvc_clock_param = CLOCK_REALTIME;
  1861. else
  1862. return -EINVAL;
  1863. return 0;
  1864. }
  1865. module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
  1866. &uvc_clock_param, S_IRUGO|S_IWUSR);
  1867. MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
  1868. module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR);
  1869. MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
  1870. module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
  1871. MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
  1872. module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
  1873. MODULE_PARM_DESC(quirks, "Forced device quirks");
  1874. module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
  1875. MODULE_PARM_DESC(trace, "Trace level bitmask");
  1876. module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
  1877. MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
  1878. /* ------------------------------------------------------------------------
  1879. * Driver initialization and cleanup
  1880. */
  1881. /*
  1882. * The Logitech cameras listed below have their interface class set to
  1883. * VENDOR_SPEC because they don't announce themselves as UVC devices, even
  1884. * though they are compliant.
  1885. */
  1886. static struct usb_device_id uvc_ids[] = {
  1887. /* LogiLink Wireless Webcam */
  1888. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1889. | USB_DEVICE_ID_MATCH_INT_INFO,
  1890. .idVendor = 0x0416,
  1891. .idProduct = 0xa91a,
  1892. .bInterfaceClass = USB_CLASS_VIDEO,
  1893. .bInterfaceSubClass = 1,
  1894. .bInterfaceProtocol = 0,
  1895. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1896. /* Genius eFace 2025 */
  1897. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1898. | USB_DEVICE_ID_MATCH_INT_INFO,
  1899. .idVendor = 0x0458,
  1900. .idProduct = 0x706e,
  1901. .bInterfaceClass = USB_CLASS_VIDEO,
  1902. .bInterfaceSubClass = 1,
  1903. .bInterfaceProtocol = 0,
  1904. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1905. /* Microsoft Lifecam NX-6000 */
  1906. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1907. | USB_DEVICE_ID_MATCH_INT_INFO,
  1908. .idVendor = 0x045e,
  1909. .idProduct = 0x00f8,
  1910. .bInterfaceClass = USB_CLASS_VIDEO,
  1911. .bInterfaceSubClass = 1,
  1912. .bInterfaceProtocol = 0,
  1913. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1914. /* Microsoft Lifecam NX-3000 */
  1915. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1916. | USB_DEVICE_ID_MATCH_INT_INFO,
  1917. .idVendor = 0x045e,
  1918. .idProduct = 0x0721,
  1919. .bInterfaceClass = USB_CLASS_VIDEO,
  1920. .bInterfaceSubClass = 1,
  1921. .bInterfaceProtocol = 0,
  1922. .driver_info = UVC_QUIRK_PROBE_DEF },
  1923. /* Microsoft Lifecam VX-7000 */
  1924. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1925. | USB_DEVICE_ID_MATCH_INT_INFO,
  1926. .idVendor = 0x045e,
  1927. .idProduct = 0x0723,
  1928. .bInterfaceClass = USB_CLASS_VIDEO,
  1929. .bInterfaceSubClass = 1,
  1930. .bInterfaceProtocol = 0,
  1931. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1932. /* Logitech Quickcam Fusion */
  1933. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1934. | USB_DEVICE_ID_MATCH_INT_INFO,
  1935. .idVendor = 0x046d,
  1936. .idProduct = 0x08c1,
  1937. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1938. .bInterfaceSubClass = 1,
  1939. .bInterfaceProtocol = 0 },
  1940. /* Logitech Quickcam Orbit MP */
  1941. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1942. | USB_DEVICE_ID_MATCH_INT_INFO,
  1943. .idVendor = 0x046d,
  1944. .idProduct = 0x08c2,
  1945. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1946. .bInterfaceSubClass = 1,
  1947. .bInterfaceProtocol = 0 },
  1948. /* Logitech Quickcam Pro for Notebook */
  1949. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1950. | USB_DEVICE_ID_MATCH_INT_INFO,
  1951. .idVendor = 0x046d,
  1952. .idProduct = 0x08c3,
  1953. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1954. .bInterfaceSubClass = 1,
  1955. .bInterfaceProtocol = 0 },
  1956. /* Logitech Quickcam Pro 5000 */
  1957. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1958. | USB_DEVICE_ID_MATCH_INT_INFO,
  1959. .idVendor = 0x046d,
  1960. .idProduct = 0x08c5,
  1961. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1962. .bInterfaceSubClass = 1,
  1963. .bInterfaceProtocol = 0 },
  1964. /* Logitech Quickcam OEM Dell Notebook */
  1965. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1966. | USB_DEVICE_ID_MATCH_INT_INFO,
  1967. .idVendor = 0x046d,
  1968. .idProduct = 0x08c6,
  1969. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1970. .bInterfaceSubClass = 1,
  1971. .bInterfaceProtocol = 0 },
  1972. /* Logitech Quickcam OEM Cisco VT Camera II */
  1973. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1974. | USB_DEVICE_ID_MATCH_INT_INFO,
  1975. .idVendor = 0x046d,
  1976. .idProduct = 0x08c7,
  1977. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1978. .bInterfaceSubClass = 1,
  1979. .bInterfaceProtocol = 0 },
  1980. /* Logitech HD Pro Webcam C920 */
  1981. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1982. | USB_DEVICE_ID_MATCH_INT_INFO,
  1983. .idVendor = 0x046d,
  1984. .idProduct = 0x082d,
  1985. .bInterfaceClass = USB_CLASS_VIDEO,
  1986. .bInterfaceSubClass = 1,
  1987. .bInterfaceProtocol = 0,
  1988. .driver_info = UVC_QUIRK_RESTORE_CTRLS_ON_INIT },
  1989. /* Chicony CNF7129 (Asus EEE 100HE) */
  1990. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1991. | USB_DEVICE_ID_MATCH_INT_INFO,
  1992. .idVendor = 0x04f2,
  1993. .idProduct = 0xb071,
  1994. .bInterfaceClass = USB_CLASS_VIDEO,
  1995. .bInterfaceSubClass = 1,
  1996. .bInterfaceProtocol = 0,
  1997. .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
  1998. /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
  1999. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2000. | USB_DEVICE_ID_MATCH_INT_INFO,
  2001. .idVendor = 0x058f,
  2002. .idProduct = 0x3820,
  2003. .bInterfaceClass = USB_CLASS_VIDEO,
  2004. .bInterfaceSubClass = 1,
  2005. .bInterfaceProtocol = 0,
  2006. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2007. /* Dell XPS m1530 */
  2008. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2009. | USB_DEVICE_ID_MATCH_INT_INFO,
  2010. .idVendor = 0x05a9,
  2011. .idProduct = 0x2640,
  2012. .bInterfaceClass = USB_CLASS_VIDEO,
  2013. .bInterfaceSubClass = 1,
  2014. .bInterfaceProtocol = 0,
  2015. .driver_info = UVC_QUIRK_PROBE_DEF },
  2016. /* Dell SP2008WFP Monitor */
  2017. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2018. | USB_DEVICE_ID_MATCH_INT_INFO,
  2019. .idVendor = 0x05a9,
  2020. .idProduct = 0x2641,
  2021. .bInterfaceClass = USB_CLASS_VIDEO,
  2022. .bInterfaceSubClass = 1,
  2023. .bInterfaceProtocol = 0,
  2024. .driver_info = UVC_QUIRK_PROBE_DEF },
  2025. /* Dell Alienware X51 */
  2026. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2027. | USB_DEVICE_ID_MATCH_INT_INFO,
  2028. .idVendor = 0x05a9,
  2029. .idProduct = 0x2643,
  2030. .bInterfaceClass = USB_CLASS_VIDEO,
  2031. .bInterfaceSubClass = 1,
  2032. .bInterfaceProtocol = 0,
  2033. .driver_info = UVC_QUIRK_PROBE_DEF },
  2034. /* Dell Studio Hybrid 140g (OmniVision webcam) */
  2035. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2036. | USB_DEVICE_ID_MATCH_INT_INFO,
  2037. .idVendor = 0x05a9,
  2038. .idProduct = 0x264a,
  2039. .bInterfaceClass = USB_CLASS_VIDEO,
  2040. .bInterfaceSubClass = 1,
  2041. .bInterfaceProtocol = 0,
  2042. .driver_info = UVC_QUIRK_PROBE_DEF },
  2043. /* Dell XPS M1330 (OmniVision OV7670 webcam) */
  2044. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2045. | USB_DEVICE_ID_MATCH_INT_INFO,
  2046. .idVendor = 0x05a9,
  2047. .idProduct = 0x7670,
  2048. .bInterfaceClass = USB_CLASS_VIDEO,
  2049. .bInterfaceSubClass = 1,
  2050. .bInterfaceProtocol = 0,
  2051. .driver_info = UVC_QUIRK_PROBE_DEF },
  2052. /* Apple Built-In iSight */
  2053. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2054. | USB_DEVICE_ID_MATCH_INT_INFO,
  2055. .idVendor = 0x05ac,
  2056. .idProduct = 0x8501,
  2057. .bInterfaceClass = USB_CLASS_VIDEO,
  2058. .bInterfaceSubClass = 1,
  2059. .bInterfaceProtocol = 0,
  2060. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2061. | UVC_QUIRK_BUILTIN_ISIGHT },
  2062. /* Foxlink ("HP Webcam" on HP Mini 5103) */
  2063. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2064. | USB_DEVICE_ID_MATCH_INT_INFO,
  2065. .idVendor = 0x05c8,
  2066. .idProduct = 0x0403,
  2067. .bInterfaceClass = USB_CLASS_VIDEO,
  2068. .bInterfaceSubClass = 1,
  2069. .bInterfaceProtocol = 0,
  2070. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2071. /* Genesys Logic USB 2.0 PC Camera */
  2072. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2073. | USB_DEVICE_ID_MATCH_INT_INFO,
  2074. .idVendor = 0x05e3,
  2075. .idProduct = 0x0505,
  2076. .bInterfaceClass = USB_CLASS_VIDEO,
  2077. .bInterfaceSubClass = 1,
  2078. .bInterfaceProtocol = 0,
  2079. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2080. /* Hercules Classic Silver */
  2081. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2082. | USB_DEVICE_ID_MATCH_INT_INFO,
  2083. .idVendor = 0x06f8,
  2084. .idProduct = 0x300c,
  2085. .bInterfaceClass = USB_CLASS_VIDEO,
  2086. .bInterfaceSubClass = 1,
  2087. .bInterfaceProtocol = 0,
  2088. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2089. /* ViMicro Vega */
  2090. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2091. | USB_DEVICE_ID_MATCH_INT_INFO,
  2092. .idVendor = 0x0ac8,
  2093. .idProduct = 0x332d,
  2094. .bInterfaceClass = USB_CLASS_VIDEO,
  2095. .bInterfaceSubClass = 1,
  2096. .bInterfaceProtocol = 0,
  2097. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2098. /* ViMicro - Minoru3D */
  2099. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2100. | USB_DEVICE_ID_MATCH_INT_INFO,
  2101. .idVendor = 0x0ac8,
  2102. .idProduct = 0x3410,
  2103. .bInterfaceClass = USB_CLASS_VIDEO,
  2104. .bInterfaceSubClass = 1,
  2105. .bInterfaceProtocol = 0,
  2106. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2107. /* ViMicro Venus - Minoru3D */
  2108. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2109. | USB_DEVICE_ID_MATCH_INT_INFO,
  2110. .idVendor = 0x0ac8,
  2111. .idProduct = 0x3420,
  2112. .bInterfaceClass = USB_CLASS_VIDEO,
  2113. .bInterfaceSubClass = 1,
  2114. .bInterfaceProtocol = 0,
  2115. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2116. /* Ophir Optronics - SPCAM 620U */
  2117. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2118. | USB_DEVICE_ID_MATCH_INT_INFO,
  2119. .idVendor = 0x0bd3,
  2120. .idProduct = 0x0555,
  2121. .bInterfaceClass = USB_CLASS_VIDEO,
  2122. .bInterfaceSubClass = 1,
  2123. .bInterfaceProtocol = 0,
  2124. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2125. /* MT6227 */
  2126. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2127. | USB_DEVICE_ID_MATCH_INT_INFO,
  2128. .idVendor = 0x0e8d,
  2129. .idProduct = 0x0004,
  2130. .bInterfaceClass = USB_CLASS_VIDEO,
  2131. .bInterfaceSubClass = 1,
  2132. .bInterfaceProtocol = 0,
  2133. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2134. | UVC_QUIRK_PROBE_DEF },
  2135. /* IMC Networks (Medion Akoya) */
  2136. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2137. | USB_DEVICE_ID_MATCH_INT_INFO,
  2138. .idVendor = 0x13d3,
  2139. .idProduct = 0x5103,
  2140. .bInterfaceClass = USB_CLASS_VIDEO,
  2141. .bInterfaceSubClass = 1,
  2142. .bInterfaceProtocol = 0,
  2143. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2144. /* JMicron USB2.0 XGA WebCam */
  2145. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2146. | USB_DEVICE_ID_MATCH_INT_INFO,
  2147. .idVendor = 0x152d,
  2148. .idProduct = 0x0310,
  2149. .bInterfaceClass = USB_CLASS_VIDEO,
  2150. .bInterfaceSubClass = 1,
  2151. .bInterfaceProtocol = 0,
  2152. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2153. /* Syntek (HP Spartan) */
  2154. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2155. | USB_DEVICE_ID_MATCH_INT_INFO,
  2156. .idVendor = 0x174f,
  2157. .idProduct = 0x5212,
  2158. .bInterfaceClass = USB_CLASS_VIDEO,
  2159. .bInterfaceSubClass = 1,
  2160. .bInterfaceProtocol = 0,
  2161. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2162. /* Syntek (Samsung Q310) */
  2163. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2164. | USB_DEVICE_ID_MATCH_INT_INFO,
  2165. .idVendor = 0x174f,
  2166. .idProduct = 0x5931,
  2167. .bInterfaceClass = USB_CLASS_VIDEO,
  2168. .bInterfaceSubClass = 1,
  2169. .bInterfaceProtocol = 0,
  2170. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2171. /* Syntek (Packard Bell EasyNote MX52 */
  2172. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2173. | USB_DEVICE_ID_MATCH_INT_INFO,
  2174. .idVendor = 0x174f,
  2175. .idProduct = 0x8a12,
  2176. .bInterfaceClass = USB_CLASS_VIDEO,
  2177. .bInterfaceSubClass = 1,
  2178. .bInterfaceProtocol = 0,
  2179. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2180. /* Syntek (Asus F9SG) */
  2181. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2182. | USB_DEVICE_ID_MATCH_INT_INFO,
  2183. .idVendor = 0x174f,
  2184. .idProduct = 0x8a31,
  2185. .bInterfaceClass = USB_CLASS_VIDEO,
  2186. .bInterfaceSubClass = 1,
  2187. .bInterfaceProtocol = 0,
  2188. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2189. /* Syntek (Asus U3S) */
  2190. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2191. | USB_DEVICE_ID_MATCH_INT_INFO,
  2192. .idVendor = 0x174f,
  2193. .idProduct = 0x8a33,
  2194. .bInterfaceClass = USB_CLASS_VIDEO,
  2195. .bInterfaceSubClass = 1,
  2196. .bInterfaceProtocol = 0,
  2197. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2198. /* Syntek (JAOtech Smart Terminal) */
  2199. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2200. | USB_DEVICE_ID_MATCH_INT_INFO,
  2201. .idVendor = 0x174f,
  2202. .idProduct = 0x8a34,
  2203. .bInterfaceClass = USB_CLASS_VIDEO,
  2204. .bInterfaceSubClass = 1,
  2205. .bInterfaceProtocol = 0,
  2206. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2207. /* Miricle 307K */
  2208. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2209. | USB_DEVICE_ID_MATCH_INT_INFO,
  2210. .idVendor = 0x17dc,
  2211. .idProduct = 0x0202,
  2212. .bInterfaceClass = USB_CLASS_VIDEO,
  2213. .bInterfaceSubClass = 1,
  2214. .bInterfaceProtocol = 0,
  2215. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2216. /* Lenovo Thinkpad SL400/SL500 */
  2217. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2218. | USB_DEVICE_ID_MATCH_INT_INFO,
  2219. .idVendor = 0x17ef,
  2220. .idProduct = 0x480b,
  2221. .bInterfaceClass = USB_CLASS_VIDEO,
  2222. .bInterfaceSubClass = 1,
  2223. .bInterfaceProtocol = 0,
  2224. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2225. /* Aveo Technology USB 2.0 Camera */
  2226. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2227. | USB_DEVICE_ID_MATCH_INT_INFO,
  2228. .idVendor = 0x1871,
  2229. .idProduct = 0x0306,
  2230. .bInterfaceClass = USB_CLASS_VIDEO,
  2231. .bInterfaceSubClass = 1,
  2232. .bInterfaceProtocol = 0,
  2233. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2234. | UVC_QUIRK_PROBE_EXTRAFIELDS },
  2235. /* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
  2236. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2237. | USB_DEVICE_ID_MATCH_INT_INFO,
  2238. .idVendor = 0x1871,
  2239. .idProduct = 0x0516,
  2240. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  2241. .bInterfaceSubClass = 1,
  2242. .bInterfaceProtocol = 0 },
  2243. /* Ecamm Pico iMage */
  2244. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2245. | USB_DEVICE_ID_MATCH_INT_INFO,
  2246. .idVendor = 0x18cd,
  2247. .idProduct = 0xcafe,
  2248. .bInterfaceClass = USB_CLASS_VIDEO,
  2249. .bInterfaceSubClass = 1,
  2250. .bInterfaceProtocol = 0,
  2251. .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
  2252. /* Manta MM-353 Plako */
  2253. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2254. | USB_DEVICE_ID_MATCH_INT_INFO,
  2255. .idVendor = 0x18ec,
  2256. .idProduct = 0x3188,
  2257. .bInterfaceClass = USB_CLASS_VIDEO,
  2258. .bInterfaceSubClass = 1,
  2259. .bInterfaceProtocol = 0,
  2260. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2261. /* FSC WebCam V30S */
  2262. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2263. | USB_DEVICE_ID_MATCH_INT_INFO,
  2264. .idVendor = 0x18ec,
  2265. .idProduct = 0x3288,
  2266. .bInterfaceClass = USB_CLASS_VIDEO,
  2267. .bInterfaceSubClass = 1,
  2268. .bInterfaceProtocol = 0,
  2269. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2270. /* Arkmicro unbranded */
  2271. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2272. | USB_DEVICE_ID_MATCH_INT_INFO,
  2273. .idVendor = 0x18ec,
  2274. .idProduct = 0x3290,
  2275. .bInterfaceClass = USB_CLASS_VIDEO,
  2276. .bInterfaceSubClass = 1,
  2277. .bInterfaceProtocol = 0,
  2278. .driver_info = UVC_QUIRK_PROBE_DEF },
  2279. /* The Imaging Source USB CCD cameras */
  2280. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2281. | USB_DEVICE_ID_MATCH_INT_INFO,
  2282. .idVendor = 0x199e,
  2283. .idProduct = 0x8102,
  2284. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  2285. .bInterfaceSubClass = 1,
  2286. .bInterfaceProtocol = 0 },
  2287. /* Bodelin ProScopeHR */
  2288. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2289. | USB_DEVICE_ID_MATCH_DEV_HI
  2290. | USB_DEVICE_ID_MATCH_INT_INFO,
  2291. .idVendor = 0x19ab,
  2292. .idProduct = 0x1000,
  2293. .bcdDevice_hi = 0x0126,
  2294. .bInterfaceClass = USB_CLASS_VIDEO,
  2295. .bInterfaceSubClass = 1,
  2296. .bInterfaceProtocol = 0,
  2297. .driver_info = UVC_QUIRK_STATUS_INTERVAL },
  2298. /* MSI StarCam 370i */
  2299. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2300. | USB_DEVICE_ID_MATCH_INT_INFO,
  2301. .idVendor = 0x1b3b,
  2302. .idProduct = 0x2951,
  2303. .bInterfaceClass = USB_CLASS_VIDEO,
  2304. .bInterfaceSubClass = 1,
  2305. .bInterfaceProtocol = 0,
  2306. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2307. /* SiGma Micro USB Web Camera */
  2308. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2309. | USB_DEVICE_ID_MATCH_INT_INFO,
  2310. .idVendor = 0x1c4f,
  2311. .idProduct = 0x3000,
  2312. .bInterfaceClass = USB_CLASS_VIDEO,
  2313. .bInterfaceSubClass = 1,
  2314. .bInterfaceProtocol = 0,
  2315. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2316. | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
  2317. /* Oculus VR Positional Tracker DK2 */
  2318. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2319. | USB_DEVICE_ID_MATCH_INT_INFO,
  2320. .idVendor = 0x2833,
  2321. .idProduct = 0x0201,
  2322. .bInterfaceClass = USB_CLASS_VIDEO,
  2323. .bInterfaceSubClass = 1,
  2324. .bInterfaceProtocol = 0,
  2325. .driver_info = UVC_QUIRK_FORCE_Y8 },
  2326. /* Generic USB Video Class */
  2327. { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
  2328. {}
  2329. };
  2330. MODULE_DEVICE_TABLE(usb, uvc_ids);
  2331. struct uvc_driver uvc_driver = {
  2332. .driver = {
  2333. .name = "uvcvideo",
  2334. .probe = uvc_probe,
  2335. .disconnect = uvc_disconnect,
  2336. .suspend = uvc_suspend,
  2337. .resume = uvc_resume,
  2338. .reset_resume = uvc_reset_resume,
  2339. .id_table = uvc_ids,
  2340. .supports_autosuspend = 1,
  2341. },
  2342. };
  2343. static int __init uvc_init(void)
  2344. {
  2345. int ret;
  2346. uvc_debugfs_init();
  2347. ret = usb_register(&uvc_driver.driver);
  2348. if (ret < 0) {
  2349. uvc_debugfs_cleanup();
  2350. return ret;
  2351. }
  2352. printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
  2353. return 0;
  2354. }
  2355. static void __exit uvc_cleanup(void)
  2356. {
  2357. usb_deregister(&uvc_driver.driver);
  2358. uvc_debugfs_cleanup();
  2359. }
  2360. module_init(uvc_init);
  2361. module_exit(uvc_cleanup);
  2362. MODULE_AUTHOR(DRIVER_AUTHOR);
  2363. MODULE_DESCRIPTION(DRIVER_DESC);
  2364. MODULE_LICENSE("GPL");
  2365. MODULE_VERSION(DRIVER_VERSION);