uvc_driver.c 59 KB

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