uvc_driver.c 59 KB

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