uvc_driver.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724
  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. /* Make sure the terminal type MSB is not null, otherwise it
  886. * could be confused with a unit.
  887. */
  888. type = get_unaligned_le16(&buffer[4]);
  889. if ((type & 0xff00) == 0) {
  890. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  891. "interface %d INPUT_TERMINAL %d has invalid "
  892. "type 0x%04x, skipping\n", udev->devnum,
  893. alts->desc.bInterfaceNumber,
  894. buffer[3], type);
  895. return 0;
  896. }
  897. n = 0;
  898. p = 0;
  899. len = 8;
  900. if (type == UVC_ITT_CAMERA) {
  901. n = buflen >= 15 ? buffer[14] : 0;
  902. len = 15;
  903. } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  904. n = buflen >= 9 ? buffer[8] : 0;
  905. p = buflen >= 10 + n ? buffer[9+n] : 0;
  906. len = 10;
  907. }
  908. if (buflen < len + n + p) {
  909. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  910. "interface %d INPUT_TERMINAL error\n",
  911. udev->devnum, alts->desc.bInterfaceNumber);
  912. return -EINVAL;
  913. }
  914. term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
  915. 1, n + p);
  916. if (term == NULL)
  917. return -ENOMEM;
  918. if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
  919. term->camera.bControlSize = n;
  920. term->camera.bmControls = (__u8 *)term + sizeof *term;
  921. term->camera.wObjectiveFocalLengthMin =
  922. get_unaligned_le16(&buffer[8]);
  923. term->camera.wObjectiveFocalLengthMax =
  924. get_unaligned_le16(&buffer[10]);
  925. term->camera.wOcularFocalLength =
  926. get_unaligned_le16(&buffer[12]);
  927. memcpy(term->camera.bmControls, &buffer[15], n);
  928. } else if (UVC_ENTITY_TYPE(term) ==
  929. UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  930. term->media.bControlSize = n;
  931. term->media.bmControls = (__u8 *)term + sizeof *term;
  932. term->media.bTransportModeSize = p;
  933. term->media.bmTransportModes = (__u8 *)term
  934. + sizeof *term + n;
  935. memcpy(term->media.bmControls, &buffer[9], n);
  936. memcpy(term->media.bmTransportModes, &buffer[10+n], p);
  937. }
  938. if (buffer[7] != 0)
  939. usb_string(udev, buffer[7], term->name,
  940. sizeof term->name);
  941. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
  942. sprintf(term->name, "Camera %u", buffer[3]);
  943. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
  944. sprintf(term->name, "Media %u", buffer[3]);
  945. else
  946. sprintf(term->name, "Input %u", buffer[3]);
  947. list_add_tail(&term->list, &dev->entities);
  948. break;
  949. case UVC_VC_OUTPUT_TERMINAL:
  950. if (buflen < 9) {
  951. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  952. "interface %d OUTPUT_TERMINAL error\n",
  953. udev->devnum, alts->desc.bInterfaceNumber);
  954. return -EINVAL;
  955. }
  956. /* Make sure the terminal type MSB is not null, otherwise it
  957. * could be confused with a unit.
  958. */
  959. type = get_unaligned_le16(&buffer[4]);
  960. if ((type & 0xff00) == 0) {
  961. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  962. "interface %d OUTPUT_TERMINAL %d has invalid "
  963. "type 0x%04x, skipping\n", udev->devnum,
  964. alts->desc.bInterfaceNumber, buffer[3], type);
  965. return 0;
  966. }
  967. term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
  968. 1, 0);
  969. if (term == NULL)
  970. return -ENOMEM;
  971. memcpy(term->baSourceID, &buffer[7], 1);
  972. if (buffer[8] != 0)
  973. usb_string(udev, buffer[8], term->name,
  974. sizeof term->name);
  975. else
  976. sprintf(term->name, "Output %u", buffer[3]);
  977. list_add_tail(&term->list, &dev->entities);
  978. break;
  979. case UVC_VC_SELECTOR_UNIT:
  980. p = buflen >= 5 ? buffer[4] : 0;
  981. if (buflen < 5 || buflen < 6 + p) {
  982. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  983. "interface %d SELECTOR_UNIT error\n",
  984. udev->devnum, alts->desc.bInterfaceNumber);
  985. return -EINVAL;
  986. }
  987. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
  988. if (unit == NULL)
  989. return -ENOMEM;
  990. memcpy(unit->baSourceID, &buffer[5], p);
  991. if (buffer[5+p] != 0)
  992. usb_string(udev, buffer[5+p], unit->name,
  993. sizeof unit->name);
  994. else
  995. sprintf(unit->name, "Selector %u", buffer[3]);
  996. list_add_tail(&unit->list, &dev->entities);
  997. break;
  998. case UVC_VC_PROCESSING_UNIT:
  999. n = buflen >= 8 ? buffer[7] : 0;
  1000. p = dev->uvc_version >= 0x0110 ? 10 : 9;
  1001. if (buflen < p + n) {
  1002. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  1003. "interface %d PROCESSING_UNIT error\n",
  1004. udev->devnum, alts->desc.bInterfaceNumber);
  1005. return -EINVAL;
  1006. }
  1007. unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
  1008. if (unit == NULL)
  1009. return -ENOMEM;
  1010. memcpy(unit->baSourceID, &buffer[4], 1);
  1011. unit->processing.wMaxMultiplier =
  1012. get_unaligned_le16(&buffer[5]);
  1013. unit->processing.bControlSize = buffer[7];
  1014. unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
  1015. memcpy(unit->processing.bmControls, &buffer[8], n);
  1016. if (dev->uvc_version >= 0x0110)
  1017. unit->processing.bmVideoStandards = buffer[9+n];
  1018. if (buffer[8+n] != 0)
  1019. usb_string(udev, buffer[8+n], unit->name,
  1020. sizeof unit->name);
  1021. else
  1022. sprintf(unit->name, "Processing %u", buffer[3]);
  1023. list_add_tail(&unit->list, &dev->entities);
  1024. break;
  1025. case UVC_VC_EXTENSION_UNIT:
  1026. p = buflen >= 22 ? buffer[21] : 0;
  1027. n = buflen >= 24 + p ? buffer[22+p] : 0;
  1028. if (buflen < 24 + p + n) {
  1029. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  1030. "interface %d EXTENSION_UNIT error\n",
  1031. udev->devnum, alts->desc.bInterfaceNumber);
  1032. return -EINVAL;
  1033. }
  1034. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
  1035. if (unit == NULL)
  1036. return -ENOMEM;
  1037. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  1038. unit->extension.bNumControls = buffer[20];
  1039. memcpy(unit->baSourceID, &buffer[22], p);
  1040. unit->extension.bControlSize = buffer[22+p];
  1041. unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
  1042. memcpy(unit->extension.bmControls, &buffer[23+p], n);
  1043. if (buffer[23+p+n] != 0)
  1044. usb_string(udev, buffer[23+p+n], unit->name,
  1045. sizeof unit->name);
  1046. else
  1047. sprintf(unit->name, "Extension %u", buffer[3]);
  1048. list_add_tail(&unit->list, &dev->entities);
  1049. break;
  1050. default:
  1051. uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
  1052. "descriptor (%u)\n", buffer[2]);
  1053. break;
  1054. }
  1055. return 0;
  1056. }
  1057. static int uvc_parse_control(struct uvc_device *dev)
  1058. {
  1059. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  1060. unsigned char *buffer = alts->extra;
  1061. int buflen = alts->extralen;
  1062. int ret;
  1063. /* Parse the default alternate setting only, as the UVC specification
  1064. * defines a single alternate setting, the default alternate setting
  1065. * zero.
  1066. */
  1067. while (buflen > 2) {
  1068. if (uvc_parse_vendor_control(dev, buffer, buflen) ||
  1069. buffer[1] != USB_DT_CS_INTERFACE)
  1070. goto next_descriptor;
  1071. if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
  1072. return ret;
  1073. next_descriptor:
  1074. buflen -= buffer[0];
  1075. buffer += buffer[0];
  1076. }
  1077. /* Check if the optional status endpoint is present. Built-in iSight
  1078. * webcams have an interrupt endpoint but spit proprietary data that
  1079. * don't conform to the UVC status endpoint messages. Don't try to
  1080. * handle the interrupt endpoint for those cameras.
  1081. */
  1082. if (alts->desc.bNumEndpoints == 1 &&
  1083. !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
  1084. struct usb_host_endpoint *ep = &alts->endpoint[0];
  1085. struct usb_endpoint_descriptor *desc = &ep->desc;
  1086. if (usb_endpoint_is_int_in(desc) &&
  1087. le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
  1088. desc->bInterval != 0) {
  1089. uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
  1090. "(addr %02x).\n", desc->bEndpointAddress);
  1091. dev->int_ep = ep;
  1092. }
  1093. }
  1094. return 0;
  1095. }
  1096. /* ------------------------------------------------------------------------
  1097. * UVC device scan
  1098. */
  1099. /*
  1100. * Scan the UVC descriptors to locate a chain starting at an Output Terminal
  1101. * and containing the following units:
  1102. *
  1103. * - one or more Output Terminals (USB Streaming or Display)
  1104. * - zero or one Processing Unit
  1105. * - zero, one or more single-input Selector Units
  1106. * - zero or one multiple-input Selector Units, provided all inputs are
  1107. * connected to input terminals
  1108. * - zero, one or mode single-input Extension Units
  1109. * - one or more Input Terminals (Camera, External or USB Streaming)
  1110. *
  1111. * The terminal and units must match on of the following structures:
  1112. *
  1113. * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
  1114. * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
  1115. * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
  1116. *
  1117. * +---------+ +---------+ -> OTT_*(0)
  1118. * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
  1119. * +---------+ +---------+ -> OTT_*(n)
  1120. *
  1121. * The Processing Unit and Extension Units can be in any order. Additional
  1122. * Extension Units connected to the main chain as single-unit branches are
  1123. * also supported. Single-input Selector Units are ignored.
  1124. */
  1125. static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
  1126. struct uvc_entity *entity)
  1127. {
  1128. switch (UVC_ENTITY_TYPE(entity)) {
  1129. case UVC_VC_EXTENSION_UNIT:
  1130. if (uvc_trace_param & UVC_TRACE_PROBE)
  1131. printk(" <- XU %d", entity->id);
  1132. if (entity->bNrInPins != 1) {
  1133. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
  1134. "than 1 input pin.\n", entity->id);
  1135. return -1;
  1136. }
  1137. break;
  1138. case UVC_VC_PROCESSING_UNIT:
  1139. if (uvc_trace_param & UVC_TRACE_PROBE)
  1140. printk(" <- PU %d", entity->id);
  1141. if (chain->processing != NULL) {
  1142. uvc_trace(UVC_TRACE_DESCR, "Found multiple "
  1143. "Processing Units in chain.\n");
  1144. return -1;
  1145. }
  1146. chain->processing = entity;
  1147. break;
  1148. case UVC_VC_SELECTOR_UNIT:
  1149. if (uvc_trace_param & UVC_TRACE_PROBE)
  1150. printk(" <- SU %d", entity->id);
  1151. /* Single-input selector units are ignored. */
  1152. if (entity->bNrInPins == 1)
  1153. break;
  1154. if (chain->selector != NULL) {
  1155. uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
  1156. "Units in chain.\n");
  1157. return -1;
  1158. }
  1159. chain->selector = entity;
  1160. break;
  1161. case UVC_ITT_VENDOR_SPECIFIC:
  1162. case UVC_ITT_CAMERA:
  1163. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1164. if (uvc_trace_param & UVC_TRACE_PROBE)
  1165. printk(" <- IT %d\n", entity->id);
  1166. break;
  1167. case UVC_OTT_VENDOR_SPECIFIC:
  1168. case UVC_OTT_DISPLAY:
  1169. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1170. if (uvc_trace_param & UVC_TRACE_PROBE)
  1171. printk(" OT %d", entity->id);
  1172. break;
  1173. case UVC_TT_STREAMING:
  1174. if (UVC_ENTITY_IS_ITERM(entity)) {
  1175. if (uvc_trace_param & UVC_TRACE_PROBE)
  1176. printk(" <- IT %d\n", entity->id);
  1177. } else {
  1178. if (uvc_trace_param & UVC_TRACE_PROBE)
  1179. printk(" OT %d", entity->id);
  1180. }
  1181. break;
  1182. default:
  1183. uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
  1184. "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
  1185. return -1;
  1186. }
  1187. list_add_tail(&entity->chain, &chain->entities);
  1188. return 0;
  1189. }
  1190. static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
  1191. struct uvc_entity *entity, struct uvc_entity *prev)
  1192. {
  1193. struct uvc_entity *forward;
  1194. int found;
  1195. /* Forward scan */
  1196. forward = NULL;
  1197. found = 0;
  1198. while (1) {
  1199. forward = uvc_entity_by_reference(chain->dev, entity->id,
  1200. forward);
  1201. if (forward == NULL)
  1202. break;
  1203. if (forward == prev)
  1204. continue;
  1205. switch (UVC_ENTITY_TYPE(forward)) {
  1206. case UVC_VC_EXTENSION_UNIT:
  1207. if (forward->bNrInPins != 1) {
  1208. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
  1209. "has more than 1 input pin.\n",
  1210. entity->id);
  1211. return -EINVAL;
  1212. }
  1213. list_add_tail(&forward->chain, &chain->entities);
  1214. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1215. if (!found)
  1216. printk(" (->");
  1217. printk(" XU %d", forward->id);
  1218. found = 1;
  1219. }
  1220. break;
  1221. case UVC_OTT_VENDOR_SPECIFIC:
  1222. case UVC_OTT_DISPLAY:
  1223. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1224. case UVC_TT_STREAMING:
  1225. if (UVC_ENTITY_IS_ITERM(forward)) {
  1226. uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
  1227. "terminal %u.\n", forward->id);
  1228. return -EINVAL;
  1229. }
  1230. list_add_tail(&forward->chain, &chain->entities);
  1231. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1232. if (!found)
  1233. printk(" (->");
  1234. printk(" OT %d", forward->id);
  1235. found = 1;
  1236. }
  1237. break;
  1238. }
  1239. }
  1240. if (found)
  1241. printk(")");
  1242. return 0;
  1243. }
  1244. static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
  1245. struct uvc_entity **_entity)
  1246. {
  1247. struct uvc_entity *entity = *_entity;
  1248. struct uvc_entity *term;
  1249. int id = -EINVAL, i;
  1250. switch (UVC_ENTITY_TYPE(entity)) {
  1251. case UVC_VC_EXTENSION_UNIT:
  1252. case UVC_VC_PROCESSING_UNIT:
  1253. id = entity->baSourceID[0];
  1254. break;
  1255. case UVC_VC_SELECTOR_UNIT:
  1256. /* Single-input selector units are ignored. */
  1257. if (entity->bNrInPins == 1) {
  1258. id = entity->baSourceID[0];
  1259. break;
  1260. }
  1261. if (uvc_trace_param & UVC_TRACE_PROBE)
  1262. printk(" <- IT");
  1263. chain->selector = entity;
  1264. for (i = 0; i < entity->bNrInPins; ++i) {
  1265. id = entity->baSourceID[i];
  1266. term = uvc_entity_by_id(chain->dev, id);
  1267. if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
  1268. uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
  1269. "input %d isn't connected to an "
  1270. "input terminal\n", entity->id, i);
  1271. return -1;
  1272. }
  1273. if (uvc_trace_param & UVC_TRACE_PROBE)
  1274. printk(" %d", term->id);
  1275. list_add_tail(&term->chain, &chain->entities);
  1276. uvc_scan_chain_forward(chain, term, entity);
  1277. }
  1278. if (uvc_trace_param & UVC_TRACE_PROBE)
  1279. printk("\n");
  1280. id = 0;
  1281. break;
  1282. case UVC_ITT_VENDOR_SPECIFIC:
  1283. case UVC_ITT_CAMERA:
  1284. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1285. case UVC_OTT_VENDOR_SPECIFIC:
  1286. case UVC_OTT_DISPLAY:
  1287. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1288. case UVC_TT_STREAMING:
  1289. id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
  1290. break;
  1291. }
  1292. if (id <= 0) {
  1293. *_entity = NULL;
  1294. return id;
  1295. }
  1296. entity = uvc_entity_by_id(chain->dev, id);
  1297. if (entity == NULL) {
  1298. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1299. "unknown entity %d.\n", id);
  1300. return -EINVAL;
  1301. }
  1302. *_entity = entity;
  1303. return 0;
  1304. }
  1305. static int uvc_scan_chain(struct uvc_video_chain *chain,
  1306. struct uvc_entity *term)
  1307. {
  1308. struct uvc_entity *entity, *prev;
  1309. uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
  1310. entity = term;
  1311. prev = NULL;
  1312. while (entity != NULL) {
  1313. /* Entity must not be part of an existing chain */
  1314. if (entity->chain.next || entity->chain.prev) {
  1315. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1316. "entity %d already in chain.\n", entity->id);
  1317. return -EINVAL;
  1318. }
  1319. /* Process entity */
  1320. if (uvc_scan_chain_entity(chain, entity) < 0)
  1321. return -EINVAL;
  1322. /* Forward scan */
  1323. if (uvc_scan_chain_forward(chain, entity, prev) < 0)
  1324. return -EINVAL;
  1325. /* Backward scan */
  1326. prev = entity;
  1327. if (uvc_scan_chain_backward(chain, &entity) < 0)
  1328. return -EINVAL;
  1329. }
  1330. return 0;
  1331. }
  1332. static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
  1333. char *buffer)
  1334. {
  1335. struct uvc_entity *term;
  1336. unsigned int nterms = 0;
  1337. char *p = buffer;
  1338. list_for_each_entry(term, terms, chain) {
  1339. if (!UVC_ENTITY_IS_TERM(term) ||
  1340. UVC_TERM_DIRECTION(term) != dir)
  1341. continue;
  1342. if (nterms)
  1343. p += sprintf(p, ",");
  1344. if (++nterms >= 4) {
  1345. p += sprintf(p, "...");
  1346. break;
  1347. }
  1348. p += sprintf(p, "%u", term->id);
  1349. }
  1350. return p - buffer;
  1351. }
  1352. static const char *uvc_print_chain(struct uvc_video_chain *chain)
  1353. {
  1354. static char buffer[43];
  1355. char *p = buffer;
  1356. p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
  1357. p += sprintf(p, " -> ");
  1358. uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
  1359. return buffer;
  1360. }
  1361. static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
  1362. {
  1363. struct uvc_video_chain *chain;
  1364. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  1365. if (chain == NULL)
  1366. return NULL;
  1367. INIT_LIST_HEAD(&chain->entities);
  1368. mutex_init(&chain->ctrl_mutex);
  1369. chain->dev = dev;
  1370. v4l2_prio_init(&chain->prio);
  1371. return chain;
  1372. }
  1373. /*
  1374. * Fallback heuristic for devices that don't connect units and terminals in a
  1375. * valid chain.
  1376. *
  1377. * Some devices have invalid baSourceID references, causing uvc_scan_chain()
  1378. * to fail, but if we just take the entities we can find and put them together
  1379. * in the most sensible chain we can think of, turns out they do work anyway.
  1380. * Note: This heuristic assumes there is a single chain.
  1381. *
  1382. * At the time of writing, devices known to have such a broken chain are
  1383. * - Acer Integrated Camera (5986:055a)
  1384. * - Realtek rtl157a7 (0bda:57a7)
  1385. */
  1386. static int uvc_scan_fallback(struct uvc_device *dev)
  1387. {
  1388. struct uvc_video_chain *chain;
  1389. struct uvc_entity *iterm = NULL;
  1390. struct uvc_entity *oterm = NULL;
  1391. struct uvc_entity *entity;
  1392. struct uvc_entity *prev;
  1393. /*
  1394. * Start by locating the input and output terminals. We only support
  1395. * devices with exactly one of each for now.
  1396. */
  1397. list_for_each_entry(entity, &dev->entities, list) {
  1398. if (UVC_ENTITY_IS_ITERM(entity)) {
  1399. if (iterm)
  1400. return -EINVAL;
  1401. iterm = entity;
  1402. }
  1403. if (UVC_ENTITY_IS_OTERM(entity)) {
  1404. if (oterm)
  1405. return -EINVAL;
  1406. oterm = entity;
  1407. }
  1408. }
  1409. if (iterm == NULL || oterm == NULL)
  1410. return -EINVAL;
  1411. /* Allocate the chain and fill it. */
  1412. chain = uvc_alloc_chain(dev);
  1413. if (chain == NULL)
  1414. return -ENOMEM;
  1415. if (uvc_scan_chain_entity(chain, oterm) < 0)
  1416. goto error;
  1417. prev = oterm;
  1418. /*
  1419. * Add all Processing and Extension Units with two pads. The order
  1420. * doesn't matter much, use reverse list traversal to connect units in
  1421. * UVC descriptor order as we build the chain from output to input. This
  1422. * leads to units appearing in the order meant by the manufacturer for
  1423. * the cameras known to require this heuristic.
  1424. */
  1425. list_for_each_entry_reverse(entity, &dev->entities, list) {
  1426. if (entity->type != UVC_VC_PROCESSING_UNIT &&
  1427. entity->type != UVC_VC_EXTENSION_UNIT)
  1428. continue;
  1429. if (entity->num_pads != 2)
  1430. continue;
  1431. if (uvc_scan_chain_entity(chain, entity) < 0)
  1432. goto error;
  1433. prev->baSourceID[0] = entity->id;
  1434. prev = entity;
  1435. }
  1436. if (uvc_scan_chain_entity(chain, iterm) < 0)
  1437. goto error;
  1438. prev->baSourceID[0] = iterm->id;
  1439. list_add_tail(&chain->list, &dev->chains);
  1440. uvc_trace(UVC_TRACE_PROBE,
  1441. "Found a video chain by fallback heuristic (%s).\n",
  1442. uvc_print_chain(chain));
  1443. return 0;
  1444. error:
  1445. kfree(chain);
  1446. return -EINVAL;
  1447. }
  1448. /*
  1449. * Scan the device for video chains and register video devices.
  1450. *
  1451. * Chains are scanned starting at their output terminals and walked backwards.
  1452. */
  1453. static int uvc_scan_device(struct uvc_device *dev)
  1454. {
  1455. struct uvc_video_chain *chain;
  1456. struct uvc_entity *term;
  1457. list_for_each_entry(term, &dev->entities, list) {
  1458. if (!UVC_ENTITY_IS_OTERM(term))
  1459. continue;
  1460. /* If the terminal is already included in a chain, skip it.
  1461. * This can happen for chains that have multiple output
  1462. * terminals, where all output terminals beside the first one
  1463. * will be inserted in the chain in forward scans.
  1464. */
  1465. if (term->chain.next || term->chain.prev)
  1466. continue;
  1467. chain = uvc_alloc_chain(dev);
  1468. if (chain == NULL)
  1469. return -ENOMEM;
  1470. term->flags |= UVC_ENTITY_FLAG_DEFAULT;
  1471. if (uvc_scan_chain(chain, term) < 0) {
  1472. kfree(chain);
  1473. continue;
  1474. }
  1475. uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
  1476. uvc_print_chain(chain));
  1477. list_add_tail(&chain->list, &dev->chains);
  1478. }
  1479. if (list_empty(&dev->chains))
  1480. uvc_scan_fallback(dev);
  1481. if (list_empty(&dev->chains)) {
  1482. uvc_printk(KERN_INFO, "No valid video chain found.\n");
  1483. return -1;
  1484. }
  1485. return 0;
  1486. }
  1487. /* ------------------------------------------------------------------------
  1488. * Video device registration and unregistration
  1489. */
  1490. /*
  1491. * Delete the UVC device.
  1492. *
  1493. * Called by the kernel when the last reference to the uvc_device structure
  1494. * is released.
  1495. *
  1496. * As this function is called after or during disconnect(), all URBs have
  1497. * already been canceled by the USB core. There is no need to kill the
  1498. * interrupt URB manually.
  1499. */
  1500. static void uvc_delete(struct uvc_device *dev)
  1501. {
  1502. struct list_head *p, *n;
  1503. uvc_status_cleanup(dev);
  1504. uvc_ctrl_cleanup_device(dev);
  1505. usb_put_intf(dev->intf);
  1506. usb_put_dev(dev->udev);
  1507. if (dev->vdev.dev)
  1508. v4l2_device_unregister(&dev->vdev);
  1509. #ifdef CONFIG_MEDIA_CONTROLLER
  1510. if (media_devnode_is_registered(dev->mdev.devnode))
  1511. media_device_unregister(&dev->mdev);
  1512. media_device_cleanup(&dev->mdev);
  1513. #endif
  1514. list_for_each_safe(p, n, &dev->chains) {
  1515. struct uvc_video_chain *chain;
  1516. chain = list_entry(p, struct uvc_video_chain, list);
  1517. kfree(chain);
  1518. }
  1519. list_for_each_safe(p, n, &dev->entities) {
  1520. struct uvc_entity *entity;
  1521. entity = list_entry(p, struct uvc_entity, list);
  1522. #ifdef CONFIG_MEDIA_CONTROLLER
  1523. uvc_mc_cleanup_entity(entity);
  1524. #endif
  1525. kfree(entity);
  1526. }
  1527. list_for_each_safe(p, n, &dev->streams) {
  1528. struct uvc_streaming *streaming;
  1529. streaming = list_entry(p, struct uvc_streaming, list);
  1530. usb_driver_release_interface(&uvc_driver.driver,
  1531. streaming->intf);
  1532. usb_put_intf(streaming->intf);
  1533. kfree(streaming->format);
  1534. kfree(streaming->header.bmaControls);
  1535. kfree(streaming);
  1536. }
  1537. kfree(dev);
  1538. }
  1539. static void uvc_release(struct video_device *vdev)
  1540. {
  1541. struct uvc_streaming *stream = video_get_drvdata(vdev);
  1542. struct uvc_device *dev = stream->dev;
  1543. /* Decrement the registered streams count and delete the device when it
  1544. * reaches zero.
  1545. */
  1546. if (atomic_dec_and_test(&dev->nstreams))
  1547. uvc_delete(dev);
  1548. }
  1549. /*
  1550. * Unregister the video devices.
  1551. */
  1552. static void uvc_unregister_video(struct uvc_device *dev)
  1553. {
  1554. struct uvc_streaming *stream;
  1555. /* Unregistering all video devices might result in uvc_delete() being
  1556. * called from inside the loop if there's no open file handle. To avoid
  1557. * that, increment the stream count before iterating over the streams
  1558. * and decrement it when done.
  1559. */
  1560. atomic_inc(&dev->nstreams);
  1561. list_for_each_entry(stream, &dev->streams, list) {
  1562. if (!video_is_registered(&stream->vdev))
  1563. continue;
  1564. video_unregister_device(&stream->vdev);
  1565. uvc_debugfs_cleanup_stream(stream);
  1566. }
  1567. /* Decrement the stream count and call uvc_delete explicitly if there
  1568. * are no stream left.
  1569. */
  1570. if (atomic_dec_and_test(&dev->nstreams))
  1571. uvc_delete(dev);
  1572. }
  1573. static int uvc_register_video(struct uvc_device *dev,
  1574. struct uvc_streaming *stream)
  1575. {
  1576. struct video_device *vdev = &stream->vdev;
  1577. int ret;
  1578. /* Initialize the video buffers queue. */
  1579. ret = uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
  1580. if (ret)
  1581. return ret;
  1582. /* Initialize the streaming interface with default streaming
  1583. * parameters.
  1584. */
  1585. ret = uvc_video_init(stream);
  1586. if (ret < 0) {
  1587. uvc_printk(KERN_ERR, "Failed to initialize the device "
  1588. "(%d).\n", ret);
  1589. return ret;
  1590. }
  1591. uvc_debugfs_init_stream(stream);
  1592. /* Register the device with V4L. */
  1593. /* We already hold a reference to dev->udev. The video device will be
  1594. * unregistered before the reference is released, so we don't need to
  1595. * get another one.
  1596. */
  1597. vdev->v4l2_dev = &dev->vdev;
  1598. vdev->fops = &uvc_fops;
  1599. vdev->ioctl_ops = &uvc_ioctl_ops;
  1600. vdev->release = uvc_release;
  1601. vdev->prio = &stream->chain->prio;
  1602. if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1603. vdev->vfl_dir = VFL_DIR_TX;
  1604. strlcpy(vdev->name, dev->name, sizeof vdev->name);
  1605. /* Set the driver data before calling video_register_device, otherwise
  1606. * uvc_v4l2_open might race us.
  1607. */
  1608. video_set_drvdata(vdev, stream);
  1609. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1610. if (ret < 0) {
  1611. uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
  1612. ret);
  1613. return ret;
  1614. }
  1615. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1616. stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE;
  1617. else
  1618. stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
  1619. atomic_inc(&dev->nstreams);
  1620. return 0;
  1621. }
  1622. /*
  1623. * Register all video devices in all chains.
  1624. */
  1625. static int uvc_register_terms(struct uvc_device *dev,
  1626. struct uvc_video_chain *chain)
  1627. {
  1628. struct uvc_streaming *stream;
  1629. struct uvc_entity *term;
  1630. int ret;
  1631. list_for_each_entry(term, &chain->entities, chain) {
  1632. if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
  1633. continue;
  1634. stream = uvc_stream_by_id(dev, term->id);
  1635. if (stream == NULL) {
  1636. uvc_printk(KERN_INFO, "No streaming interface found "
  1637. "for terminal %u.", term->id);
  1638. continue;
  1639. }
  1640. stream->chain = chain;
  1641. ret = uvc_register_video(dev, stream);
  1642. if (ret < 0)
  1643. return ret;
  1644. term->vdev = &stream->vdev;
  1645. }
  1646. return 0;
  1647. }
  1648. static int uvc_register_chains(struct uvc_device *dev)
  1649. {
  1650. struct uvc_video_chain *chain;
  1651. int ret;
  1652. list_for_each_entry(chain, &dev->chains, list) {
  1653. ret = uvc_register_terms(dev, chain);
  1654. if (ret < 0)
  1655. return ret;
  1656. #ifdef CONFIG_MEDIA_CONTROLLER
  1657. ret = uvc_mc_register_entities(chain);
  1658. if (ret < 0) {
  1659. uvc_printk(KERN_INFO, "Failed to register entites "
  1660. "(%d).\n", ret);
  1661. }
  1662. #endif
  1663. }
  1664. return 0;
  1665. }
  1666. /* ------------------------------------------------------------------------
  1667. * USB probe, disconnect, suspend and resume
  1668. */
  1669. static int uvc_probe(struct usb_interface *intf,
  1670. const struct usb_device_id *id)
  1671. {
  1672. struct usb_device *udev = interface_to_usbdev(intf);
  1673. struct uvc_device *dev;
  1674. int ret;
  1675. if (id->idVendor && id->idProduct)
  1676. uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
  1677. "(%04x:%04x)\n", udev->devpath, id->idVendor,
  1678. id->idProduct);
  1679. else
  1680. uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
  1681. udev->devpath);
  1682. /* Allocate memory for the device and initialize it. */
  1683. if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
  1684. return -ENOMEM;
  1685. INIT_LIST_HEAD(&dev->entities);
  1686. INIT_LIST_HEAD(&dev->chains);
  1687. INIT_LIST_HEAD(&dev->streams);
  1688. atomic_set(&dev->nstreams, 0);
  1689. atomic_set(&dev->nmappings, 0);
  1690. mutex_init(&dev->lock);
  1691. dev->udev = usb_get_dev(udev);
  1692. dev->intf = usb_get_intf(intf);
  1693. dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1694. dev->quirks = (uvc_quirks_param == -1)
  1695. ? id->driver_info : uvc_quirks_param;
  1696. if (udev->product != NULL)
  1697. strlcpy(dev->name, udev->product, sizeof dev->name);
  1698. else
  1699. snprintf(dev->name, sizeof dev->name,
  1700. "UVC Camera (%04x:%04x)",
  1701. le16_to_cpu(udev->descriptor.idVendor),
  1702. le16_to_cpu(udev->descriptor.idProduct));
  1703. /* Parse the Video Class control descriptor. */
  1704. if (uvc_parse_control(dev) < 0) {
  1705. uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
  1706. "descriptors.\n");
  1707. goto error;
  1708. }
  1709. uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
  1710. dev->uvc_version >> 8, dev->uvc_version & 0xff,
  1711. udev->product ? udev->product : "<unnamed>",
  1712. le16_to_cpu(udev->descriptor.idVendor),
  1713. le16_to_cpu(udev->descriptor.idProduct));
  1714. if (dev->quirks != id->driver_info) {
  1715. uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
  1716. "parameter for testing purpose.\n", dev->quirks);
  1717. uvc_printk(KERN_INFO, "Please report required quirks to the "
  1718. "linux-uvc-devel mailing list.\n");
  1719. }
  1720. /* Initialize the media device and register the V4L2 device. */
  1721. #ifdef CONFIG_MEDIA_CONTROLLER
  1722. dev->mdev.dev = &intf->dev;
  1723. strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
  1724. if (udev->serial)
  1725. strlcpy(dev->mdev.serial, udev->serial,
  1726. sizeof(dev->mdev.serial));
  1727. strcpy(dev->mdev.bus_info, udev->devpath);
  1728. dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
  1729. dev->mdev.driver_version = LINUX_VERSION_CODE;
  1730. media_device_init(&dev->mdev);
  1731. dev->vdev.mdev = &dev->mdev;
  1732. #endif
  1733. if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
  1734. goto error;
  1735. /* Initialize controls. */
  1736. if (uvc_ctrl_init_device(dev) < 0)
  1737. goto error;
  1738. /* Scan the device for video chains. */
  1739. if (uvc_scan_device(dev) < 0)
  1740. goto error;
  1741. /* Register video device nodes. */
  1742. if (uvc_register_chains(dev) < 0)
  1743. goto error;
  1744. #ifdef CONFIG_MEDIA_CONTROLLER
  1745. /* Register the media device node */
  1746. if (media_device_register(&dev->mdev) < 0)
  1747. goto error;
  1748. #endif
  1749. /* Save our data pointer in the interface data. */
  1750. usb_set_intfdata(intf, dev);
  1751. /* Initialize the interrupt URB. */
  1752. if ((ret = uvc_status_init(dev)) < 0) {
  1753. uvc_printk(KERN_INFO, "Unable to initialize the status "
  1754. "endpoint (%d), status interrupt will not be "
  1755. "supported.\n", ret);
  1756. }
  1757. uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
  1758. usb_enable_autosuspend(udev);
  1759. return 0;
  1760. error:
  1761. uvc_unregister_video(dev);
  1762. return -ENODEV;
  1763. }
  1764. static void uvc_disconnect(struct usb_interface *intf)
  1765. {
  1766. struct uvc_device *dev = usb_get_intfdata(intf);
  1767. /* Set the USB interface data to NULL. This can be done outside the
  1768. * lock, as there's no other reader.
  1769. */
  1770. usb_set_intfdata(intf, NULL);
  1771. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1772. UVC_SC_VIDEOSTREAMING)
  1773. return;
  1774. uvc_unregister_video(dev);
  1775. }
  1776. static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
  1777. {
  1778. struct uvc_device *dev = usb_get_intfdata(intf);
  1779. struct uvc_streaming *stream;
  1780. uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
  1781. intf->cur_altsetting->desc.bInterfaceNumber);
  1782. /* Controls are cached on the fly so they don't need to be saved. */
  1783. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1784. UVC_SC_VIDEOCONTROL) {
  1785. mutex_lock(&dev->lock);
  1786. if (dev->users)
  1787. uvc_status_stop(dev);
  1788. mutex_unlock(&dev->lock);
  1789. return 0;
  1790. }
  1791. list_for_each_entry(stream, &dev->streams, list) {
  1792. if (stream->intf == intf)
  1793. return uvc_video_suspend(stream);
  1794. }
  1795. uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
  1796. "mismatch.\n");
  1797. return -EINVAL;
  1798. }
  1799. static int __uvc_resume(struct usb_interface *intf, int reset)
  1800. {
  1801. struct uvc_device *dev = usb_get_intfdata(intf);
  1802. struct uvc_streaming *stream;
  1803. int ret = 0;
  1804. uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
  1805. intf->cur_altsetting->desc.bInterfaceNumber);
  1806. if (intf->cur_altsetting->desc.bInterfaceSubClass ==
  1807. UVC_SC_VIDEOCONTROL) {
  1808. if (reset) {
  1809. ret = uvc_ctrl_restore_values(dev);
  1810. if (ret < 0)
  1811. return ret;
  1812. }
  1813. mutex_lock(&dev->lock);
  1814. if (dev->users)
  1815. ret = uvc_status_start(dev, GFP_NOIO);
  1816. mutex_unlock(&dev->lock);
  1817. return ret;
  1818. }
  1819. list_for_each_entry(stream, &dev->streams, list) {
  1820. if (stream->intf == intf) {
  1821. ret = uvc_video_resume(stream, reset);
  1822. if (ret < 0)
  1823. uvc_queue_streamoff(&stream->queue,
  1824. stream->queue.queue.type);
  1825. return ret;
  1826. }
  1827. }
  1828. uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
  1829. "mismatch.\n");
  1830. return -EINVAL;
  1831. }
  1832. static int uvc_resume(struct usb_interface *intf)
  1833. {
  1834. return __uvc_resume(intf, 0);
  1835. }
  1836. static int uvc_reset_resume(struct usb_interface *intf)
  1837. {
  1838. return __uvc_resume(intf, 1);
  1839. }
  1840. /* ------------------------------------------------------------------------
  1841. * Module parameters
  1842. */
  1843. static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
  1844. {
  1845. if (uvc_clock_param == CLOCK_MONOTONIC)
  1846. return sprintf(buffer, "CLOCK_MONOTONIC");
  1847. else
  1848. return sprintf(buffer, "CLOCK_REALTIME");
  1849. }
  1850. static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
  1851. {
  1852. if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
  1853. val += strlen("clock_");
  1854. if (strcasecmp(val, "monotonic") == 0)
  1855. uvc_clock_param = CLOCK_MONOTONIC;
  1856. else if (strcasecmp(val, "realtime") == 0)
  1857. uvc_clock_param = CLOCK_REALTIME;
  1858. else
  1859. return -EINVAL;
  1860. return 0;
  1861. }
  1862. module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
  1863. &uvc_clock_param, S_IRUGO|S_IWUSR);
  1864. MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
  1865. module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR);
  1866. MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
  1867. module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
  1868. MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
  1869. module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
  1870. MODULE_PARM_DESC(quirks, "Forced device quirks");
  1871. module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
  1872. MODULE_PARM_DESC(trace, "Trace level bitmask");
  1873. module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
  1874. MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
  1875. /* ------------------------------------------------------------------------
  1876. * Driver initialization and cleanup
  1877. */
  1878. /*
  1879. * The Logitech cameras listed below have their interface class set to
  1880. * VENDOR_SPEC because they don't announce themselves as UVC devices, even
  1881. * though they are compliant.
  1882. */
  1883. static struct usb_device_id uvc_ids[] = {
  1884. /* LogiLink Wireless Webcam */
  1885. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1886. | USB_DEVICE_ID_MATCH_INT_INFO,
  1887. .idVendor = 0x0416,
  1888. .idProduct = 0xa91a,
  1889. .bInterfaceClass = USB_CLASS_VIDEO,
  1890. .bInterfaceSubClass = 1,
  1891. .bInterfaceProtocol = 0,
  1892. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1893. /* Genius eFace 2025 */
  1894. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1895. | USB_DEVICE_ID_MATCH_INT_INFO,
  1896. .idVendor = 0x0458,
  1897. .idProduct = 0x706e,
  1898. .bInterfaceClass = USB_CLASS_VIDEO,
  1899. .bInterfaceSubClass = 1,
  1900. .bInterfaceProtocol = 0,
  1901. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1902. /* Microsoft Lifecam NX-6000 */
  1903. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1904. | USB_DEVICE_ID_MATCH_INT_INFO,
  1905. .idVendor = 0x045e,
  1906. .idProduct = 0x00f8,
  1907. .bInterfaceClass = USB_CLASS_VIDEO,
  1908. .bInterfaceSubClass = 1,
  1909. .bInterfaceProtocol = 0,
  1910. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1911. /* Microsoft Lifecam NX-3000 */
  1912. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1913. | USB_DEVICE_ID_MATCH_INT_INFO,
  1914. .idVendor = 0x045e,
  1915. .idProduct = 0x0721,
  1916. .bInterfaceClass = USB_CLASS_VIDEO,
  1917. .bInterfaceSubClass = 1,
  1918. .bInterfaceProtocol = 0,
  1919. .driver_info = UVC_QUIRK_PROBE_DEF },
  1920. /* Microsoft Lifecam VX-7000 */
  1921. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1922. | USB_DEVICE_ID_MATCH_INT_INFO,
  1923. .idVendor = 0x045e,
  1924. .idProduct = 0x0723,
  1925. .bInterfaceClass = USB_CLASS_VIDEO,
  1926. .bInterfaceSubClass = 1,
  1927. .bInterfaceProtocol = 0,
  1928. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1929. /* Logitech Quickcam Fusion */
  1930. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1931. | USB_DEVICE_ID_MATCH_INT_INFO,
  1932. .idVendor = 0x046d,
  1933. .idProduct = 0x08c1,
  1934. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1935. .bInterfaceSubClass = 1,
  1936. .bInterfaceProtocol = 0 },
  1937. /* Logitech Quickcam Orbit MP */
  1938. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1939. | USB_DEVICE_ID_MATCH_INT_INFO,
  1940. .idVendor = 0x046d,
  1941. .idProduct = 0x08c2,
  1942. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1943. .bInterfaceSubClass = 1,
  1944. .bInterfaceProtocol = 0 },
  1945. /* Logitech Quickcam Pro for Notebook */
  1946. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1947. | USB_DEVICE_ID_MATCH_INT_INFO,
  1948. .idVendor = 0x046d,
  1949. .idProduct = 0x08c3,
  1950. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1951. .bInterfaceSubClass = 1,
  1952. .bInterfaceProtocol = 0 },
  1953. /* Logitech Quickcam Pro 5000 */
  1954. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1955. | USB_DEVICE_ID_MATCH_INT_INFO,
  1956. .idVendor = 0x046d,
  1957. .idProduct = 0x08c5,
  1958. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1959. .bInterfaceSubClass = 1,
  1960. .bInterfaceProtocol = 0 },
  1961. /* Logitech Quickcam OEM Dell Notebook */
  1962. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1963. | USB_DEVICE_ID_MATCH_INT_INFO,
  1964. .idVendor = 0x046d,
  1965. .idProduct = 0x08c6,
  1966. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1967. .bInterfaceSubClass = 1,
  1968. .bInterfaceProtocol = 0 },
  1969. /* Logitech Quickcam OEM Cisco VT Camera II */
  1970. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1971. | USB_DEVICE_ID_MATCH_INT_INFO,
  1972. .idVendor = 0x046d,
  1973. .idProduct = 0x08c7,
  1974. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1975. .bInterfaceSubClass = 1,
  1976. .bInterfaceProtocol = 0 },
  1977. /* Logitech HD Pro Webcam C920 */
  1978. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1979. | USB_DEVICE_ID_MATCH_INT_INFO,
  1980. .idVendor = 0x046d,
  1981. .idProduct = 0x082d,
  1982. .bInterfaceClass = USB_CLASS_VIDEO,
  1983. .bInterfaceSubClass = 1,
  1984. .bInterfaceProtocol = 0,
  1985. .driver_info = UVC_QUIRK_RESTORE_CTRLS_ON_INIT },
  1986. /* Chicony CNF7129 (Asus EEE 100HE) */
  1987. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1988. | USB_DEVICE_ID_MATCH_INT_INFO,
  1989. .idVendor = 0x04f2,
  1990. .idProduct = 0xb071,
  1991. .bInterfaceClass = USB_CLASS_VIDEO,
  1992. .bInterfaceSubClass = 1,
  1993. .bInterfaceProtocol = 0,
  1994. .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
  1995. /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
  1996. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1997. | USB_DEVICE_ID_MATCH_INT_INFO,
  1998. .idVendor = 0x058f,
  1999. .idProduct = 0x3820,
  2000. .bInterfaceClass = USB_CLASS_VIDEO,
  2001. .bInterfaceSubClass = 1,
  2002. .bInterfaceProtocol = 0,
  2003. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2004. /* Dell XPS m1530 */
  2005. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2006. | USB_DEVICE_ID_MATCH_INT_INFO,
  2007. .idVendor = 0x05a9,
  2008. .idProduct = 0x2640,
  2009. .bInterfaceClass = USB_CLASS_VIDEO,
  2010. .bInterfaceSubClass = 1,
  2011. .bInterfaceProtocol = 0,
  2012. .driver_info = UVC_QUIRK_PROBE_DEF },
  2013. /* Dell SP2008WFP Monitor */
  2014. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2015. | USB_DEVICE_ID_MATCH_INT_INFO,
  2016. .idVendor = 0x05a9,
  2017. .idProduct = 0x2641,
  2018. .bInterfaceClass = USB_CLASS_VIDEO,
  2019. .bInterfaceSubClass = 1,
  2020. .bInterfaceProtocol = 0,
  2021. .driver_info = UVC_QUIRK_PROBE_DEF },
  2022. /* Dell Alienware X51 */
  2023. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2024. | USB_DEVICE_ID_MATCH_INT_INFO,
  2025. .idVendor = 0x05a9,
  2026. .idProduct = 0x2643,
  2027. .bInterfaceClass = USB_CLASS_VIDEO,
  2028. .bInterfaceSubClass = 1,
  2029. .bInterfaceProtocol = 0,
  2030. .driver_info = UVC_QUIRK_PROBE_DEF },
  2031. /* Dell Studio Hybrid 140g (OmniVision webcam) */
  2032. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2033. | USB_DEVICE_ID_MATCH_INT_INFO,
  2034. .idVendor = 0x05a9,
  2035. .idProduct = 0x264a,
  2036. .bInterfaceClass = USB_CLASS_VIDEO,
  2037. .bInterfaceSubClass = 1,
  2038. .bInterfaceProtocol = 0,
  2039. .driver_info = UVC_QUIRK_PROBE_DEF },
  2040. /* Dell XPS M1330 (OmniVision OV7670 webcam) */
  2041. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2042. | USB_DEVICE_ID_MATCH_INT_INFO,
  2043. .idVendor = 0x05a9,
  2044. .idProduct = 0x7670,
  2045. .bInterfaceClass = USB_CLASS_VIDEO,
  2046. .bInterfaceSubClass = 1,
  2047. .bInterfaceProtocol = 0,
  2048. .driver_info = UVC_QUIRK_PROBE_DEF },
  2049. /* Apple Built-In iSight */
  2050. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2051. | USB_DEVICE_ID_MATCH_INT_INFO,
  2052. .idVendor = 0x05ac,
  2053. .idProduct = 0x8501,
  2054. .bInterfaceClass = USB_CLASS_VIDEO,
  2055. .bInterfaceSubClass = 1,
  2056. .bInterfaceProtocol = 0,
  2057. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2058. | UVC_QUIRK_BUILTIN_ISIGHT },
  2059. /* Foxlink ("HP Webcam" on HP Mini 5103) */
  2060. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2061. | USB_DEVICE_ID_MATCH_INT_INFO,
  2062. .idVendor = 0x05c8,
  2063. .idProduct = 0x0403,
  2064. .bInterfaceClass = USB_CLASS_VIDEO,
  2065. .bInterfaceSubClass = 1,
  2066. .bInterfaceProtocol = 0,
  2067. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2068. /* Genesys Logic USB 2.0 PC Camera */
  2069. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2070. | USB_DEVICE_ID_MATCH_INT_INFO,
  2071. .idVendor = 0x05e3,
  2072. .idProduct = 0x0505,
  2073. .bInterfaceClass = USB_CLASS_VIDEO,
  2074. .bInterfaceSubClass = 1,
  2075. .bInterfaceProtocol = 0,
  2076. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2077. /* Hercules Classic Silver */
  2078. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2079. | USB_DEVICE_ID_MATCH_INT_INFO,
  2080. .idVendor = 0x06f8,
  2081. .idProduct = 0x300c,
  2082. .bInterfaceClass = USB_CLASS_VIDEO,
  2083. .bInterfaceSubClass = 1,
  2084. .bInterfaceProtocol = 0,
  2085. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2086. /* ViMicro Vega */
  2087. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2088. | USB_DEVICE_ID_MATCH_INT_INFO,
  2089. .idVendor = 0x0ac8,
  2090. .idProduct = 0x332d,
  2091. .bInterfaceClass = USB_CLASS_VIDEO,
  2092. .bInterfaceSubClass = 1,
  2093. .bInterfaceProtocol = 0,
  2094. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2095. /* ViMicro - Minoru3D */
  2096. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2097. | USB_DEVICE_ID_MATCH_INT_INFO,
  2098. .idVendor = 0x0ac8,
  2099. .idProduct = 0x3410,
  2100. .bInterfaceClass = USB_CLASS_VIDEO,
  2101. .bInterfaceSubClass = 1,
  2102. .bInterfaceProtocol = 0,
  2103. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2104. /* ViMicro Venus - Minoru3D */
  2105. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2106. | USB_DEVICE_ID_MATCH_INT_INFO,
  2107. .idVendor = 0x0ac8,
  2108. .idProduct = 0x3420,
  2109. .bInterfaceClass = USB_CLASS_VIDEO,
  2110. .bInterfaceSubClass = 1,
  2111. .bInterfaceProtocol = 0,
  2112. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  2113. /* Ophir Optronics - SPCAM 620U */
  2114. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2115. | USB_DEVICE_ID_MATCH_INT_INFO,
  2116. .idVendor = 0x0bd3,
  2117. .idProduct = 0x0555,
  2118. .bInterfaceClass = USB_CLASS_VIDEO,
  2119. .bInterfaceSubClass = 1,
  2120. .bInterfaceProtocol = 0,
  2121. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2122. /* MT6227 */
  2123. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2124. | USB_DEVICE_ID_MATCH_INT_INFO,
  2125. .idVendor = 0x0e8d,
  2126. .idProduct = 0x0004,
  2127. .bInterfaceClass = USB_CLASS_VIDEO,
  2128. .bInterfaceSubClass = 1,
  2129. .bInterfaceProtocol = 0,
  2130. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2131. | UVC_QUIRK_PROBE_DEF },
  2132. /* IMC Networks (Medion Akoya) */
  2133. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2134. | USB_DEVICE_ID_MATCH_INT_INFO,
  2135. .idVendor = 0x13d3,
  2136. .idProduct = 0x5103,
  2137. .bInterfaceClass = USB_CLASS_VIDEO,
  2138. .bInterfaceSubClass = 1,
  2139. .bInterfaceProtocol = 0,
  2140. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2141. /* JMicron USB2.0 XGA WebCam */
  2142. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2143. | USB_DEVICE_ID_MATCH_INT_INFO,
  2144. .idVendor = 0x152d,
  2145. .idProduct = 0x0310,
  2146. .bInterfaceClass = USB_CLASS_VIDEO,
  2147. .bInterfaceSubClass = 1,
  2148. .bInterfaceProtocol = 0,
  2149. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2150. /* Syntek (HP Spartan) */
  2151. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2152. | USB_DEVICE_ID_MATCH_INT_INFO,
  2153. .idVendor = 0x174f,
  2154. .idProduct = 0x5212,
  2155. .bInterfaceClass = USB_CLASS_VIDEO,
  2156. .bInterfaceSubClass = 1,
  2157. .bInterfaceProtocol = 0,
  2158. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2159. /* Syntek (Samsung Q310) */
  2160. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2161. | USB_DEVICE_ID_MATCH_INT_INFO,
  2162. .idVendor = 0x174f,
  2163. .idProduct = 0x5931,
  2164. .bInterfaceClass = USB_CLASS_VIDEO,
  2165. .bInterfaceSubClass = 1,
  2166. .bInterfaceProtocol = 0,
  2167. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2168. /* Syntek (Packard Bell EasyNote MX52 */
  2169. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2170. | USB_DEVICE_ID_MATCH_INT_INFO,
  2171. .idVendor = 0x174f,
  2172. .idProduct = 0x8a12,
  2173. .bInterfaceClass = USB_CLASS_VIDEO,
  2174. .bInterfaceSubClass = 1,
  2175. .bInterfaceProtocol = 0,
  2176. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2177. /* Syntek (Asus F9SG) */
  2178. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2179. | USB_DEVICE_ID_MATCH_INT_INFO,
  2180. .idVendor = 0x174f,
  2181. .idProduct = 0x8a31,
  2182. .bInterfaceClass = USB_CLASS_VIDEO,
  2183. .bInterfaceSubClass = 1,
  2184. .bInterfaceProtocol = 0,
  2185. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2186. /* Syntek (Asus U3S) */
  2187. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2188. | USB_DEVICE_ID_MATCH_INT_INFO,
  2189. .idVendor = 0x174f,
  2190. .idProduct = 0x8a33,
  2191. .bInterfaceClass = USB_CLASS_VIDEO,
  2192. .bInterfaceSubClass = 1,
  2193. .bInterfaceProtocol = 0,
  2194. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2195. /* Syntek (JAOtech Smart Terminal) */
  2196. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2197. | USB_DEVICE_ID_MATCH_INT_INFO,
  2198. .idVendor = 0x174f,
  2199. .idProduct = 0x8a34,
  2200. .bInterfaceClass = USB_CLASS_VIDEO,
  2201. .bInterfaceSubClass = 1,
  2202. .bInterfaceProtocol = 0,
  2203. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2204. /* Miricle 307K */
  2205. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2206. | USB_DEVICE_ID_MATCH_INT_INFO,
  2207. .idVendor = 0x17dc,
  2208. .idProduct = 0x0202,
  2209. .bInterfaceClass = USB_CLASS_VIDEO,
  2210. .bInterfaceSubClass = 1,
  2211. .bInterfaceProtocol = 0,
  2212. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2213. /* Lenovo Thinkpad SL400/SL500 */
  2214. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2215. | USB_DEVICE_ID_MATCH_INT_INFO,
  2216. .idVendor = 0x17ef,
  2217. .idProduct = 0x480b,
  2218. .bInterfaceClass = USB_CLASS_VIDEO,
  2219. .bInterfaceSubClass = 1,
  2220. .bInterfaceProtocol = 0,
  2221. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  2222. /* Aveo Technology USB 2.0 Camera */
  2223. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2224. | USB_DEVICE_ID_MATCH_INT_INFO,
  2225. .idVendor = 0x1871,
  2226. .idProduct = 0x0306,
  2227. .bInterfaceClass = USB_CLASS_VIDEO,
  2228. .bInterfaceSubClass = 1,
  2229. .bInterfaceProtocol = 0,
  2230. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2231. | UVC_QUIRK_PROBE_EXTRAFIELDS },
  2232. /* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
  2233. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2234. | USB_DEVICE_ID_MATCH_INT_INFO,
  2235. .idVendor = 0x1871,
  2236. .idProduct = 0x0516,
  2237. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  2238. .bInterfaceSubClass = 1,
  2239. .bInterfaceProtocol = 0 },
  2240. /* Ecamm Pico iMage */
  2241. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2242. | USB_DEVICE_ID_MATCH_INT_INFO,
  2243. .idVendor = 0x18cd,
  2244. .idProduct = 0xcafe,
  2245. .bInterfaceClass = USB_CLASS_VIDEO,
  2246. .bInterfaceSubClass = 1,
  2247. .bInterfaceProtocol = 0,
  2248. .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
  2249. /* Manta MM-353 Plako */
  2250. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2251. | USB_DEVICE_ID_MATCH_INT_INFO,
  2252. .idVendor = 0x18ec,
  2253. .idProduct = 0x3188,
  2254. .bInterfaceClass = USB_CLASS_VIDEO,
  2255. .bInterfaceSubClass = 1,
  2256. .bInterfaceProtocol = 0,
  2257. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2258. /* FSC WebCam V30S */
  2259. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2260. | USB_DEVICE_ID_MATCH_INT_INFO,
  2261. .idVendor = 0x18ec,
  2262. .idProduct = 0x3288,
  2263. .bInterfaceClass = USB_CLASS_VIDEO,
  2264. .bInterfaceSubClass = 1,
  2265. .bInterfaceProtocol = 0,
  2266. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2267. /* Arkmicro unbranded */
  2268. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2269. | USB_DEVICE_ID_MATCH_INT_INFO,
  2270. .idVendor = 0x18ec,
  2271. .idProduct = 0x3290,
  2272. .bInterfaceClass = USB_CLASS_VIDEO,
  2273. .bInterfaceSubClass = 1,
  2274. .bInterfaceProtocol = 0,
  2275. .driver_info = UVC_QUIRK_PROBE_DEF },
  2276. /* The Imaging Source USB CCD cameras */
  2277. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2278. | USB_DEVICE_ID_MATCH_INT_INFO,
  2279. .idVendor = 0x199e,
  2280. .idProduct = 0x8102,
  2281. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  2282. .bInterfaceSubClass = 1,
  2283. .bInterfaceProtocol = 0 },
  2284. /* Bodelin ProScopeHR */
  2285. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2286. | USB_DEVICE_ID_MATCH_DEV_HI
  2287. | USB_DEVICE_ID_MATCH_INT_INFO,
  2288. .idVendor = 0x19ab,
  2289. .idProduct = 0x1000,
  2290. .bcdDevice_hi = 0x0126,
  2291. .bInterfaceClass = USB_CLASS_VIDEO,
  2292. .bInterfaceSubClass = 1,
  2293. .bInterfaceProtocol = 0,
  2294. .driver_info = UVC_QUIRK_STATUS_INTERVAL },
  2295. /* MSI StarCam 370i */
  2296. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2297. | USB_DEVICE_ID_MATCH_INT_INFO,
  2298. .idVendor = 0x1b3b,
  2299. .idProduct = 0x2951,
  2300. .bInterfaceClass = USB_CLASS_VIDEO,
  2301. .bInterfaceSubClass = 1,
  2302. .bInterfaceProtocol = 0,
  2303. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  2304. /* SiGma Micro USB Web Camera */
  2305. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2306. | USB_DEVICE_ID_MATCH_INT_INFO,
  2307. .idVendor = 0x1c4f,
  2308. .idProduct = 0x3000,
  2309. .bInterfaceClass = USB_CLASS_VIDEO,
  2310. .bInterfaceSubClass = 1,
  2311. .bInterfaceProtocol = 0,
  2312. .driver_info = UVC_QUIRK_PROBE_MINMAX
  2313. | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
  2314. /* Oculus VR Positional Tracker DK2 */
  2315. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  2316. | USB_DEVICE_ID_MATCH_INT_INFO,
  2317. .idVendor = 0x2833,
  2318. .idProduct = 0x0201,
  2319. .bInterfaceClass = USB_CLASS_VIDEO,
  2320. .bInterfaceSubClass = 1,
  2321. .bInterfaceProtocol = 0,
  2322. .driver_info = UVC_QUIRK_FORCE_Y8 },
  2323. /* Generic USB Video Class */
  2324. { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
  2325. { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
  2326. {}
  2327. };
  2328. MODULE_DEVICE_TABLE(usb, uvc_ids);
  2329. struct uvc_driver uvc_driver = {
  2330. .driver = {
  2331. .name = "uvcvideo",
  2332. .probe = uvc_probe,
  2333. .disconnect = uvc_disconnect,
  2334. .suspend = uvc_suspend,
  2335. .resume = uvc_resume,
  2336. .reset_resume = uvc_reset_resume,
  2337. .id_table = uvc_ids,
  2338. .supports_autosuspend = 1,
  2339. },
  2340. };
  2341. static int __init uvc_init(void)
  2342. {
  2343. int ret;
  2344. uvc_debugfs_init();
  2345. ret = usb_register(&uvc_driver.driver);
  2346. if (ret < 0) {
  2347. uvc_debugfs_cleanup();
  2348. return ret;
  2349. }
  2350. printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
  2351. return 0;
  2352. }
  2353. static void __exit uvc_cleanup(void)
  2354. {
  2355. usb_deregister(&uvc_driver.driver);
  2356. uvc_debugfs_cleanup();
  2357. }
  2358. module_init(uvc_init);
  2359. module_exit(uvc_cleanup);
  2360. MODULE_AUTHOR(DRIVER_AUTHOR);
  2361. MODULE_DESCRIPTION(DRIVER_DESC);
  2362. MODULE_LICENSE("GPL");
  2363. MODULE_VERSION(DRIVER_VERSION);