uvc_driver.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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. for (i = 0; i < entity->bNrInPins; ++i)
  216. if (entity->baSourceID[i] == id)
  217. return entity;
  218. }
  219. return NULL;
  220. }
  221. static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
  222. {
  223. struct uvc_streaming *stream;
  224. list_for_each_entry(stream, &dev->streams, list) {
  225. if (stream->header.bTerminalLink == id)
  226. return stream;
  227. }
  228. return NULL;
  229. }
  230. /* ------------------------------------------------------------------------
  231. * Descriptors parsing
  232. */
  233. static int uvc_parse_format(struct uvc_device *dev,
  234. struct uvc_streaming *streaming, struct uvc_format *format,
  235. __u32 **intervals, unsigned char *buffer, int buflen)
  236. {
  237. struct usb_interface *intf = streaming->intf;
  238. struct usb_host_interface *alts = intf->cur_altsetting;
  239. struct uvc_format_desc *fmtdesc;
  240. struct uvc_frame *frame;
  241. const unsigned char *start = buffer;
  242. unsigned int interval;
  243. unsigned int i, n;
  244. __u8 ftype;
  245. format->type = buffer[2];
  246. format->index = buffer[3];
  247. switch (buffer[2]) {
  248. case UVC_VS_FORMAT_UNCOMPRESSED:
  249. case UVC_VS_FORMAT_FRAME_BASED:
  250. n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
  251. if (buflen < n) {
  252. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  253. "interface %d FORMAT error\n",
  254. dev->udev->devnum,
  255. alts->desc.bInterfaceNumber);
  256. return -EINVAL;
  257. }
  258. /* Find the format descriptor from its GUID. */
  259. fmtdesc = uvc_format_by_guid(&buffer[5]);
  260. if (fmtdesc != NULL) {
  261. strlcpy(format->name, fmtdesc->name,
  262. sizeof format->name);
  263. format->fcc = fmtdesc->fcc;
  264. } else {
  265. uvc_printk(KERN_INFO, "Unknown video format "
  266. UVC_GUID_FORMAT "\n",
  267. UVC_GUID_ARGS(&buffer[5]));
  268. snprintf(format->name, sizeof format->name,
  269. UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
  270. format->fcc = 0;
  271. }
  272. format->bpp = buffer[21];
  273. if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
  274. ftype = UVC_VS_FRAME_UNCOMPRESSED;
  275. } else {
  276. ftype = UVC_VS_FRAME_FRAME_BASED;
  277. if (buffer[27])
  278. format->flags = UVC_FMT_FLAG_COMPRESSED;
  279. }
  280. break;
  281. case UVC_VS_FORMAT_MJPEG:
  282. if (buflen < 11) {
  283. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  284. "interface %d FORMAT error\n",
  285. dev->udev->devnum,
  286. alts->desc.bInterfaceNumber);
  287. return -EINVAL;
  288. }
  289. strlcpy(format->name, "MJPEG", sizeof format->name);
  290. format->fcc = V4L2_PIX_FMT_MJPEG;
  291. format->flags = UVC_FMT_FLAG_COMPRESSED;
  292. format->bpp = 0;
  293. ftype = UVC_VS_FRAME_MJPEG;
  294. break;
  295. case UVC_VS_FORMAT_DV:
  296. if (buflen < 9) {
  297. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  298. "interface %d FORMAT error\n",
  299. dev->udev->devnum,
  300. alts->desc.bInterfaceNumber);
  301. return -EINVAL;
  302. }
  303. switch (buffer[8] & 0x7f) {
  304. case 0:
  305. strlcpy(format->name, "SD-DV", sizeof format->name);
  306. break;
  307. case 1:
  308. strlcpy(format->name, "SDL-DV", sizeof format->name);
  309. break;
  310. case 2:
  311. strlcpy(format->name, "HD-DV", sizeof format->name);
  312. break;
  313. default:
  314. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  315. "interface %d: unknown DV format %u\n",
  316. dev->udev->devnum,
  317. alts->desc.bInterfaceNumber, buffer[8]);
  318. return -EINVAL;
  319. }
  320. strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
  321. sizeof format->name);
  322. format->fcc = V4L2_PIX_FMT_DV;
  323. format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
  324. format->bpp = 0;
  325. ftype = 0;
  326. /* Create a dummy frame descriptor. */
  327. frame = &format->frame[0];
  328. memset(&format->frame[0], 0, sizeof format->frame[0]);
  329. frame->bFrameIntervalType = 1;
  330. frame->dwDefaultFrameInterval = 1;
  331. frame->dwFrameInterval = *intervals;
  332. *(*intervals)++ = 1;
  333. format->nframes = 1;
  334. break;
  335. case UVC_VS_FORMAT_MPEG2TS:
  336. case UVC_VS_FORMAT_STREAM_BASED:
  337. /* Not supported yet. */
  338. default:
  339. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  340. "interface %d unsupported format %u\n",
  341. dev->udev->devnum, alts->desc.bInterfaceNumber,
  342. buffer[2]);
  343. return -EINVAL;
  344. }
  345. uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
  346. buflen -= buffer[0];
  347. buffer += buffer[0];
  348. /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
  349. * based formats have frame descriptors.
  350. */
  351. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  352. buffer[2] == ftype) {
  353. frame = &format->frame[format->nframes];
  354. if (ftype != UVC_VS_FRAME_FRAME_BASED)
  355. n = buflen > 25 ? buffer[25] : 0;
  356. else
  357. n = buflen > 21 ? buffer[21] : 0;
  358. n = n ? n : 3;
  359. if (buflen < 26 + 4*n) {
  360. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  361. "interface %d FRAME error\n", dev->udev->devnum,
  362. alts->desc.bInterfaceNumber);
  363. return -EINVAL;
  364. }
  365. frame->bFrameIndex = buffer[3];
  366. frame->bmCapabilities = buffer[4];
  367. frame->wWidth = get_unaligned_le16(&buffer[5]);
  368. frame->wHeight = get_unaligned_le16(&buffer[7]);
  369. frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
  370. frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
  371. if (ftype != UVC_VS_FRAME_FRAME_BASED) {
  372. frame->dwMaxVideoFrameBufferSize =
  373. get_unaligned_le32(&buffer[17]);
  374. frame->dwDefaultFrameInterval =
  375. get_unaligned_le32(&buffer[21]);
  376. frame->bFrameIntervalType = buffer[25];
  377. } else {
  378. frame->dwMaxVideoFrameBufferSize = 0;
  379. frame->dwDefaultFrameInterval =
  380. get_unaligned_le32(&buffer[17]);
  381. frame->bFrameIntervalType = buffer[21];
  382. }
  383. frame->dwFrameInterval = *intervals;
  384. /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
  385. * completely. Observed behaviours range from setting the
  386. * value to 1.1x the actual frame size to hardwiring the
  387. * 16 low bits to 0. This results in a higher than necessary
  388. * memory usage as well as a wrong image size information. For
  389. * uncompressed formats this can be fixed by computing the
  390. * value from the frame size.
  391. */
  392. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
  393. frame->dwMaxVideoFrameBufferSize = format->bpp
  394. * frame->wWidth * frame->wHeight / 8;
  395. /* Some bogus devices report dwMinFrameInterval equal to
  396. * dwMaxFrameInterval and have dwFrameIntervalStep set to
  397. * zero. Setting all null intervals to 1 fixes the problem and
  398. * some other divisions by zero that could happen.
  399. */
  400. for (i = 0; i < n; ++i) {
  401. interval = get_unaligned_le32(&buffer[26+4*i]);
  402. *(*intervals)++ = interval ? interval : 1;
  403. }
  404. /* Make sure that the default frame interval stays between
  405. * the boundaries.
  406. */
  407. n -= frame->bFrameIntervalType ? 1 : 2;
  408. frame->dwDefaultFrameInterval =
  409. min(frame->dwFrameInterval[n],
  410. max(frame->dwFrameInterval[0],
  411. frame->dwDefaultFrameInterval));
  412. uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
  413. frame->wWidth, frame->wHeight,
  414. 10000000/frame->dwDefaultFrameInterval,
  415. (100000000/frame->dwDefaultFrameInterval)%10);
  416. format->nframes++;
  417. buflen -= buffer[0];
  418. buffer += buffer[0];
  419. }
  420. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  421. buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
  422. buflen -= buffer[0];
  423. buffer += buffer[0];
  424. }
  425. if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
  426. buffer[2] == UVC_VS_COLORFORMAT) {
  427. if (buflen < 6) {
  428. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  429. "interface %d COLORFORMAT error\n",
  430. dev->udev->devnum,
  431. alts->desc.bInterfaceNumber);
  432. return -EINVAL;
  433. }
  434. format->colorspace = uvc_colorspace(buffer[3]);
  435. buflen -= buffer[0];
  436. buffer += buffer[0];
  437. }
  438. return buffer - start;
  439. }
  440. static int uvc_parse_streaming(struct uvc_device *dev,
  441. struct usb_interface *intf)
  442. {
  443. struct uvc_streaming *streaming = NULL;
  444. struct uvc_format *format;
  445. struct uvc_frame *frame;
  446. struct usb_host_interface *alts = &intf->altsetting[0];
  447. unsigned char *_buffer, *buffer = alts->extra;
  448. int _buflen, buflen = alts->extralen;
  449. unsigned int nformats = 0, nframes = 0, nintervals = 0;
  450. unsigned int size, i, n, p;
  451. __u32 *interval;
  452. __u16 psize;
  453. int ret = -EINVAL;
  454. if (intf->cur_altsetting->desc.bInterfaceSubClass
  455. != UVC_SC_VIDEOSTREAMING) {
  456. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
  457. "video streaming interface\n", dev->udev->devnum,
  458. intf->altsetting[0].desc.bInterfaceNumber);
  459. return -EINVAL;
  460. }
  461. if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
  462. uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
  463. "claimed\n", dev->udev->devnum,
  464. intf->altsetting[0].desc.bInterfaceNumber);
  465. return -EINVAL;
  466. }
  467. streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
  468. if (streaming == NULL) {
  469. usb_driver_release_interface(&uvc_driver.driver, intf);
  470. return -EINVAL;
  471. }
  472. mutex_init(&streaming->mutex);
  473. streaming->dev = dev;
  474. streaming->intf = usb_get_intf(intf);
  475. streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
  476. /* The Pico iMage webcam has its class-specific interface descriptors
  477. * after the endpoint descriptors.
  478. */
  479. if (buflen == 0) {
  480. for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
  481. struct usb_host_endpoint *ep = &alts->endpoint[i];
  482. if (ep->extralen == 0)
  483. continue;
  484. if (ep->extralen > 2 &&
  485. ep->extra[1] == USB_DT_CS_INTERFACE) {
  486. uvc_trace(UVC_TRACE_DESCR, "trying extra data "
  487. "from endpoint %u.\n", i);
  488. buffer = alts->endpoint[i].extra;
  489. buflen = alts->endpoint[i].extralen;
  490. break;
  491. }
  492. }
  493. }
  494. /* Skip the standard interface descriptors. */
  495. while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
  496. buflen -= buffer[0];
  497. buffer += buffer[0];
  498. }
  499. if (buflen <= 2) {
  500. uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
  501. "interface descriptors found.\n");
  502. goto error;
  503. }
  504. /* Parse the header descriptor. */
  505. switch (buffer[2]) {
  506. case UVC_VS_OUTPUT_HEADER:
  507. streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  508. size = 9;
  509. break;
  510. case UVC_VS_INPUT_HEADER:
  511. streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  512. size = 13;
  513. break;
  514. default:
  515. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  516. "%d HEADER descriptor not found.\n", dev->udev->devnum,
  517. alts->desc.bInterfaceNumber);
  518. goto error;
  519. }
  520. p = buflen >= 4 ? buffer[3] : 0;
  521. n = buflen >= size ? buffer[size-1] : 0;
  522. if (buflen < size + p*n) {
  523. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  524. "interface %d HEADER descriptor is invalid.\n",
  525. dev->udev->devnum, alts->desc.bInterfaceNumber);
  526. goto error;
  527. }
  528. streaming->header.bNumFormats = p;
  529. streaming->header.bEndpointAddress = buffer[6];
  530. if (buffer[2] == UVC_VS_INPUT_HEADER) {
  531. streaming->header.bmInfo = buffer[7];
  532. streaming->header.bTerminalLink = buffer[8];
  533. streaming->header.bStillCaptureMethod = buffer[9];
  534. streaming->header.bTriggerSupport = buffer[10];
  535. streaming->header.bTriggerUsage = buffer[11];
  536. } else {
  537. streaming->header.bTerminalLink = buffer[7];
  538. }
  539. streaming->header.bControlSize = n;
  540. streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
  541. if (streaming->header.bmaControls == NULL) {
  542. ret = -ENOMEM;
  543. goto error;
  544. }
  545. memcpy(streaming->header.bmaControls, &buffer[size], p*n);
  546. buflen -= buffer[0];
  547. buffer += buffer[0];
  548. _buffer = buffer;
  549. _buflen = buflen;
  550. /* Count the format and frame descriptors. */
  551. while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
  552. switch (_buffer[2]) {
  553. case UVC_VS_FORMAT_UNCOMPRESSED:
  554. case UVC_VS_FORMAT_MJPEG:
  555. case UVC_VS_FORMAT_FRAME_BASED:
  556. nformats++;
  557. break;
  558. case UVC_VS_FORMAT_DV:
  559. /* DV format has no frame descriptor. We will create a
  560. * dummy frame descriptor with a dummy frame interval.
  561. */
  562. nformats++;
  563. nframes++;
  564. nintervals++;
  565. break;
  566. case UVC_VS_FORMAT_MPEG2TS:
  567. case UVC_VS_FORMAT_STREAM_BASED:
  568. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
  569. "interface %d FORMAT %u is not supported.\n",
  570. dev->udev->devnum,
  571. alts->desc.bInterfaceNumber, _buffer[2]);
  572. break;
  573. case UVC_VS_FRAME_UNCOMPRESSED:
  574. case UVC_VS_FRAME_MJPEG:
  575. nframes++;
  576. if (_buflen > 25)
  577. nintervals += _buffer[25] ? _buffer[25] : 3;
  578. break;
  579. case UVC_VS_FRAME_FRAME_BASED:
  580. nframes++;
  581. if (_buflen > 21)
  582. nintervals += _buffer[21] ? _buffer[21] : 3;
  583. break;
  584. }
  585. _buflen -= _buffer[0];
  586. _buffer += _buffer[0];
  587. }
  588. if (nformats == 0) {
  589. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  590. "%d has no supported formats defined.\n",
  591. dev->udev->devnum, alts->desc.bInterfaceNumber);
  592. goto error;
  593. }
  594. size = nformats * sizeof *format + nframes * sizeof *frame
  595. + nintervals * sizeof *interval;
  596. format = kzalloc(size, GFP_KERNEL);
  597. if (format == NULL) {
  598. ret = -ENOMEM;
  599. goto error;
  600. }
  601. frame = (struct uvc_frame *)&format[nformats];
  602. interval = (__u32 *)&frame[nframes];
  603. streaming->format = format;
  604. streaming->nformats = nformats;
  605. /* Parse the format descriptors. */
  606. while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
  607. switch (buffer[2]) {
  608. case UVC_VS_FORMAT_UNCOMPRESSED:
  609. case UVC_VS_FORMAT_MJPEG:
  610. case UVC_VS_FORMAT_DV:
  611. case UVC_VS_FORMAT_FRAME_BASED:
  612. format->frame = frame;
  613. ret = uvc_parse_format(dev, streaming, format,
  614. &interval, buffer, buflen);
  615. if (ret < 0)
  616. goto error;
  617. frame += format->nframes;
  618. format++;
  619. buflen -= ret;
  620. buffer += ret;
  621. continue;
  622. default:
  623. break;
  624. }
  625. buflen -= buffer[0];
  626. buffer += buffer[0];
  627. }
  628. if (buflen)
  629. uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
  630. "%d has %u bytes of trailing descriptor garbage.\n",
  631. dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
  632. /* Parse the alternate settings to find the maximum bandwidth. */
  633. for (i = 0; i < intf->num_altsetting; ++i) {
  634. struct usb_host_endpoint *ep;
  635. alts = &intf->altsetting[i];
  636. ep = uvc_find_endpoint(alts,
  637. streaming->header.bEndpointAddress);
  638. if (ep == NULL)
  639. continue;
  640. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  641. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  642. if (psize > streaming->maxpsize)
  643. streaming->maxpsize = psize;
  644. }
  645. list_add_tail(&streaming->list, &dev->streams);
  646. return 0;
  647. error:
  648. usb_driver_release_interface(&uvc_driver.driver, intf);
  649. usb_put_intf(intf);
  650. kfree(streaming->format);
  651. kfree(streaming->header.bmaControls);
  652. kfree(streaming);
  653. return ret;
  654. }
  655. static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
  656. unsigned int num_pads, unsigned int extra_size)
  657. {
  658. struct uvc_entity *entity;
  659. unsigned int num_inputs;
  660. unsigned int size;
  661. num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
  662. size = sizeof(*entity) + extra_size + num_inputs;
  663. entity = kzalloc(size, GFP_KERNEL);
  664. if (entity == NULL)
  665. return NULL;
  666. entity->id = id;
  667. entity->type = type;
  668. entity->bNrInPins = num_inputs;
  669. entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
  670. return entity;
  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 = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
  720. p + 1, 2*n);
  721. if (unit == NULL)
  722. return -ENOMEM;
  723. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  724. unit->extension.bNumControls = buffer[20];
  725. memcpy(unit->baSourceID, &buffer[22], p);
  726. unit->extension.bControlSize = buffer[22+p];
  727. unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
  728. unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
  729. + n;
  730. memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
  731. if (buffer[24+p+2*n] != 0)
  732. usb_string(udev, buffer[24+p+2*n], unit->name,
  733. sizeof unit->name);
  734. else
  735. sprintf(unit->name, "Extension %u", buffer[3]);
  736. list_add_tail(&unit->list, &dev->entities);
  737. handled = 1;
  738. break;
  739. }
  740. return handled;
  741. }
  742. static int uvc_parse_standard_control(struct uvc_device *dev,
  743. const unsigned char *buffer, int buflen)
  744. {
  745. struct usb_device *udev = dev->udev;
  746. struct uvc_entity *unit, *term;
  747. struct usb_interface *intf;
  748. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  749. unsigned int i, n, p, len;
  750. __u16 type;
  751. switch (buffer[2]) {
  752. case UVC_VC_HEADER:
  753. n = buflen >= 12 ? buffer[11] : 0;
  754. if (buflen < 12 || buflen < 12 + n) {
  755. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  756. "interface %d HEADER error\n", udev->devnum,
  757. alts->desc.bInterfaceNumber);
  758. return -EINVAL;
  759. }
  760. dev->uvc_version = get_unaligned_le16(&buffer[3]);
  761. dev->clock_frequency = get_unaligned_le32(&buffer[7]);
  762. /* Parse all USB Video Streaming interfaces. */
  763. for (i = 0; i < n; ++i) {
  764. intf = usb_ifnum_to_if(udev, buffer[12+i]);
  765. if (intf == NULL) {
  766. uvc_trace(UVC_TRACE_DESCR, "device %d "
  767. "interface %d doesn't exists\n",
  768. udev->devnum, i);
  769. continue;
  770. }
  771. uvc_parse_streaming(dev, intf);
  772. }
  773. break;
  774. case UVC_VC_INPUT_TERMINAL:
  775. if (buflen < 8) {
  776. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  777. "interface %d INPUT_TERMINAL error\n",
  778. udev->devnum, alts->desc.bInterfaceNumber);
  779. return -EINVAL;
  780. }
  781. /* Make sure the terminal type MSB is not null, otherwise it
  782. * could be confused with a unit.
  783. */
  784. type = get_unaligned_le16(&buffer[4]);
  785. if ((type & 0xff00) == 0) {
  786. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  787. "interface %d INPUT_TERMINAL %d has invalid "
  788. "type 0x%04x, skipping\n", udev->devnum,
  789. alts->desc.bInterfaceNumber,
  790. buffer[3], type);
  791. return 0;
  792. }
  793. n = 0;
  794. p = 0;
  795. len = 8;
  796. if (type == UVC_ITT_CAMERA) {
  797. n = buflen >= 15 ? buffer[14] : 0;
  798. len = 15;
  799. } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  800. n = buflen >= 9 ? buffer[8] : 0;
  801. p = buflen >= 10 + n ? buffer[9+n] : 0;
  802. len = 10;
  803. }
  804. if (buflen < len + n + p) {
  805. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  806. "interface %d INPUT_TERMINAL error\n",
  807. udev->devnum, alts->desc.bInterfaceNumber);
  808. return -EINVAL;
  809. }
  810. term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
  811. 1, n + p);
  812. if (term == NULL)
  813. return -ENOMEM;
  814. if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
  815. term->camera.bControlSize = n;
  816. term->camera.bmControls = (__u8 *)term + sizeof *term;
  817. term->camera.wObjectiveFocalLengthMin =
  818. get_unaligned_le16(&buffer[8]);
  819. term->camera.wObjectiveFocalLengthMax =
  820. get_unaligned_le16(&buffer[10]);
  821. term->camera.wOcularFocalLength =
  822. get_unaligned_le16(&buffer[12]);
  823. memcpy(term->camera.bmControls, &buffer[15], n);
  824. } else if (UVC_ENTITY_TYPE(term) ==
  825. UVC_ITT_MEDIA_TRANSPORT_INPUT) {
  826. term->media.bControlSize = n;
  827. term->media.bmControls = (__u8 *)term + sizeof *term;
  828. term->media.bTransportModeSize = p;
  829. term->media.bmTransportModes = (__u8 *)term
  830. + sizeof *term + n;
  831. memcpy(term->media.bmControls, &buffer[9], n);
  832. memcpy(term->media.bmTransportModes, &buffer[10+n], p);
  833. }
  834. if (buffer[7] != 0)
  835. usb_string(udev, buffer[7], term->name,
  836. sizeof term->name);
  837. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
  838. sprintf(term->name, "Camera %u", buffer[3]);
  839. else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
  840. sprintf(term->name, "Media %u", buffer[3]);
  841. else
  842. sprintf(term->name, "Input %u", buffer[3]);
  843. list_add_tail(&term->list, &dev->entities);
  844. break;
  845. case UVC_VC_OUTPUT_TERMINAL:
  846. if (buflen < 9) {
  847. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  848. "interface %d OUTPUT_TERMINAL error\n",
  849. udev->devnum, alts->desc.bInterfaceNumber);
  850. return -EINVAL;
  851. }
  852. /* Make sure the terminal type MSB is not null, otherwise it
  853. * could be confused with a unit.
  854. */
  855. type = get_unaligned_le16(&buffer[4]);
  856. if ((type & 0xff00) == 0) {
  857. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  858. "interface %d OUTPUT_TERMINAL %d has invalid "
  859. "type 0x%04x, skipping\n", udev->devnum,
  860. alts->desc.bInterfaceNumber, buffer[3], type);
  861. return 0;
  862. }
  863. term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
  864. 1, 0);
  865. if (term == NULL)
  866. return -ENOMEM;
  867. memcpy(term->baSourceID, &buffer[7], 1);
  868. if (buffer[8] != 0)
  869. usb_string(udev, buffer[8], term->name,
  870. sizeof term->name);
  871. else
  872. sprintf(term->name, "Output %u", buffer[3]);
  873. list_add_tail(&term->list, &dev->entities);
  874. break;
  875. case UVC_VC_SELECTOR_UNIT:
  876. p = buflen >= 5 ? buffer[4] : 0;
  877. if (buflen < 5 || buflen < 6 + p) {
  878. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  879. "interface %d SELECTOR_UNIT error\n",
  880. udev->devnum, alts->desc.bInterfaceNumber);
  881. return -EINVAL;
  882. }
  883. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
  884. if (unit == NULL)
  885. return -ENOMEM;
  886. memcpy(unit->baSourceID, &buffer[5], p);
  887. if (buffer[5+p] != 0)
  888. usb_string(udev, buffer[5+p], unit->name,
  889. sizeof unit->name);
  890. else
  891. sprintf(unit->name, "Selector %u", buffer[3]);
  892. list_add_tail(&unit->list, &dev->entities);
  893. break;
  894. case UVC_VC_PROCESSING_UNIT:
  895. n = buflen >= 8 ? buffer[7] : 0;
  896. p = dev->uvc_version >= 0x0110 ? 10 : 9;
  897. if (buflen < p + n) {
  898. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  899. "interface %d PROCESSING_UNIT error\n",
  900. udev->devnum, alts->desc.bInterfaceNumber);
  901. return -EINVAL;
  902. }
  903. unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
  904. if (unit == NULL)
  905. return -ENOMEM;
  906. memcpy(unit->baSourceID, &buffer[4], 1);
  907. unit->processing.wMaxMultiplier =
  908. get_unaligned_le16(&buffer[5]);
  909. unit->processing.bControlSize = buffer[7];
  910. unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
  911. memcpy(unit->processing.bmControls, &buffer[8], n);
  912. if (dev->uvc_version >= 0x0110)
  913. unit->processing.bmVideoStandards = buffer[9+n];
  914. if (buffer[8+n] != 0)
  915. usb_string(udev, buffer[8+n], unit->name,
  916. sizeof unit->name);
  917. else
  918. sprintf(unit->name, "Processing %u", buffer[3]);
  919. list_add_tail(&unit->list, &dev->entities);
  920. break;
  921. case UVC_VC_EXTENSION_UNIT:
  922. p = buflen >= 22 ? buffer[21] : 0;
  923. n = buflen >= 24 + p ? buffer[22+p] : 0;
  924. if (buflen < 24 + p + n) {
  925. uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
  926. "interface %d EXTENSION_UNIT error\n",
  927. udev->devnum, alts->desc.bInterfaceNumber);
  928. return -EINVAL;
  929. }
  930. unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
  931. if (unit == NULL)
  932. return -ENOMEM;
  933. memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
  934. unit->extension.bNumControls = buffer[20];
  935. memcpy(unit->baSourceID, &buffer[22], p);
  936. unit->extension.bControlSize = buffer[22+p];
  937. unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
  938. memcpy(unit->extension.bmControls, &buffer[23+p], n);
  939. if (buffer[23+p+n] != 0)
  940. usb_string(udev, buffer[23+p+n], unit->name,
  941. sizeof unit->name);
  942. else
  943. sprintf(unit->name, "Extension %u", buffer[3]);
  944. list_add_tail(&unit->list, &dev->entities);
  945. break;
  946. default:
  947. uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
  948. "descriptor (%u)\n", buffer[2]);
  949. break;
  950. }
  951. return 0;
  952. }
  953. static int uvc_parse_control(struct uvc_device *dev)
  954. {
  955. struct usb_host_interface *alts = dev->intf->cur_altsetting;
  956. unsigned char *buffer = alts->extra;
  957. int buflen = alts->extralen;
  958. int ret;
  959. /* Parse the default alternate setting only, as the UVC specification
  960. * defines a single alternate setting, the default alternate setting
  961. * zero.
  962. */
  963. while (buflen > 2) {
  964. if (uvc_parse_vendor_control(dev, buffer, buflen) ||
  965. buffer[1] != USB_DT_CS_INTERFACE)
  966. goto next_descriptor;
  967. if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
  968. return ret;
  969. next_descriptor:
  970. buflen -= buffer[0];
  971. buffer += buffer[0];
  972. }
  973. /* Check if the optional status endpoint is present. Built-in iSight
  974. * webcams have an interrupt endpoint but spit proprietary data that
  975. * don't conform to the UVC status endpoint messages. Don't try to
  976. * handle the interrupt endpoint for those cameras.
  977. */
  978. if (alts->desc.bNumEndpoints == 1 &&
  979. !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
  980. struct usb_host_endpoint *ep = &alts->endpoint[0];
  981. struct usb_endpoint_descriptor *desc = &ep->desc;
  982. if (usb_endpoint_is_int_in(desc) &&
  983. le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
  984. desc->bInterval != 0) {
  985. uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
  986. "(addr %02x).\n", desc->bEndpointAddress);
  987. dev->int_ep = ep;
  988. }
  989. }
  990. return 0;
  991. }
  992. /* ------------------------------------------------------------------------
  993. * UVC device scan
  994. */
  995. /*
  996. * Scan the UVC descriptors to locate a chain starting at an Output Terminal
  997. * and containing the following units:
  998. *
  999. * - one or more Output Terminals (USB Streaming or Display)
  1000. * - zero or one Processing Unit
  1001. * - zero, one or more single-input Selector Units
  1002. * - zero or one multiple-input Selector Units, provided all inputs are
  1003. * connected to input terminals
  1004. * - zero, one or mode single-input Extension Units
  1005. * - one or more Input Terminals (Camera, External or USB Streaming)
  1006. *
  1007. * The terminal and units must match on of the following structures:
  1008. *
  1009. * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
  1010. * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
  1011. * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
  1012. *
  1013. * +---------+ +---------+ -> OTT_*(0)
  1014. * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
  1015. * +---------+ +---------+ -> OTT_*(n)
  1016. *
  1017. * The Processing Unit and Extension Units can be in any order. Additional
  1018. * Extension Units connected to the main chain as single-unit branches are
  1019. * also supported. Single-input Selector Units are ignored.
  1020. */
  1021. static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
  1022. struct uvc_entity *entity)
  1023. {
  1024. switch (UVC_ENTITY_TYPE(entity)) {
  1025. case UVC_VC_EXTENSION_UNIT:
  1026. if (uvc_trace_param & UVC_TRACE_PROBE)
  1027. printk(" <- XU %d", entity->id);
  1028. if (entity->bNrInPins != 1) {
  1029. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
  1030. "than 1 input pin.\n", entity->id);
  1031. return -1;
  1032. }
  1033. break;
  1034. case UVC_VC_PROCESSING_UNIT:
  1035. if (uvc_trace_param & UVC_TRACE_PROBE)
  1036. printk(" <- PU %d", entity->id);
  1037. if (chain->processing != NULL) {
  1038. uvc_trace(UVC_TRACE_DESCR, "Found multiple "
  1039. "Processing Units in chain.\n");
  1040. return -1;
  1041. }
  1042. chain->processing = entity;
  1043. break;
  1044. case UVC_VC_SELECTOR_UNIT:
  1045. if (uvc_trace_param & UVC_TRACE_PROBE)
  1046. printk(" <- SU %d", entity->id);
  1047. /* Single-input selector units are ignored. */
  1048. if (entity->bNrInPins == 1)
  1049. break;
  1050. if (chain->selector != NULL) {
  1051. uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
  1052. "Units in chain.\n");
  1053. return -1;
  1054. }
  1055. chain->selector = entity;
  1056. break;
  1057. case UVC_ITT_VENDOR_SPECIFIC:
  1058. case UVC_ITT_CAMERA:
  1059. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1060. if (uvc_trace_param & UVC_TRACE_PROBE)
  1061. printk(" <- IT %d\n", entity->id);
  1062. break;
  1063. case UVC_TT_STREAMING:
  1064. if (UVC_ENTITY_IS_ITERM(entity)) {
  1065. if (uvc_trace_param & UVC_TRACE_PROBE)
  1066. printk(" <- IT %d\n", entity->id);
  1067. } else {
  1068. if (uvc_trace_param & UVC_TRACE_PROBE)
  1069. printk(" OT %d", entity->id);
  1070. }
  1071. break;
  1072. default:
  1073. uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
  1074. "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
  1075. return -1;
  1076. }
  1077. list_add_tail(&entity->chain, &chain->entities);
  1078. return 0;
  1079. }
  1080. static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
  1081. struct uvc_entity *entity, struct uvc_entity *prev)
  1082. {
  1083. struct uvc_entity *forward;
  1084. int found;
  1085. /* Forward scan */
  1086. forward = NULL;
  1087. found = 0;
  1088. while (1) {
  1089. forward = uvc_entity_by_reference(chain->dev, entity->id,
  1090. forward);
  1091. if (forward == NULL)
  1092. break;
  1093. if (forward == prev)
  1094. continue;
  1095. switch (UVC_ENTITY_TYPE(forward)) {
  1096. case UVC_VC_EXTENSION_UNIT:
  1097. if (forward->bNrInPins != 1) {
  1098. uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
  1099. "has more than 1 input pin.\n",
  1100. entity->id);
  1101. return -EINVAL;
  1102. }
  1103. list_add_tail(&forward->chain, &chain->entities);
  1104. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1105. if (!found)
  1106. printk(" (->");
  1107. printk(" XU %d", forward->id);
  1108. found = 1;
  1109. }
  1110. break;
  1111. case UVC_OTT_VENDOR_SPECIFIC:
  1112. case UVC_OTT_DISPLAY:
  1113. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1114. case UVC_TT_STREAMING:
  1115. if (UVC_ENTITY_IS_ITERM(forward)) {
  1116. uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
  1117. "terminal %u.\n", forward->id);
  1118. return -EINVAL;
  1119. }
  1120. list_add_tail(&forward->chain, &chain->entities);
  1121. if (uvc_trace_param & UVC_TRACE_PROBE) {
  1122. if (!found)
  1123. printk(" (->");
  1124. printk(" OT %d", forward->id);
  1125. found = 1;
  1126. }
  1127. break;
  1128. }
  1129. }
  1130. if (found)
  1131. printk(")");
  1132. return 0;
  1133. }
  1134. static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
  1135. struct uvc_entity **_entity)
  1136. {
  1137. struct uvc_entity *entity = *_entity;
  1138. struct uvc_entity *term;
  1139. int id = -EINVAL, i;
  1140. switch (UVC_ENTITY_TYPE(entity)) {
  1141. case UVC_VC_EXTENSION_UNIT:
  1142. case UVC_VC_PROCESSING_UNIT:
  1143. id = entity->baSourceID[0];
  1144. break;
  1145. case UVC_VC_SELECTOR_UNIT:
  1146. /* Single-input selector units are ignored. */
  1147. if (entity->bNrInPins == 1) {
  1148. id = entity->baSourceID[0];
  1149. break;
  1150. }
  1151. if (uvc_trace_param & UVC_TRACE_PROBE)
  1152. printk(" <- IT");
  1153. chain->selector = entity;
  1154. for (i = 0; i < entity->bNrInPins; ++i) {
  1155. id = entity->baSourceID[i];
  1156. term = uvc_entity_by_id(chain->dev, id);
  1157. if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
  1158. uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
  1159. "input %d isn't connected to an "
  1160. "input terminal\n", entity->id, i);
  1161. return -1;
  1162. }
  1163. if (uvc_trace_param & UVC_TRACE_PROBE)
  1164. printk(" %d", term->id);
  1165. list_add_tail(&term->chain, &chain->entities);
  1166. uvc_scan_chain_forward(chain, term, entity);
  1167. }
  1168. if (uvc_trace_param & UVC_TRACE_PROBE)
  1169. printk("\n");
  1170. id = 0;
  1171. break;
  1172. case UVC_ITT_VENDOR_SPECIFIC:
  1173. case UVC_ITT_CAMERA:
  1174. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  1175. case UVC_OTT_VENDOR_SPECIFIC:
  1176. case UVC_OTT_DISPLAY:
  1177. case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
  1178. case UVC_TT_STREAMING:
  1179. id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
  1180. break;
  1181. }
  1182. if (id <= 0) {
  1183. *_entity = NULL;
  1184. return id;
  1185. }
  1186. entity = uvc_entity_by_id(chain->dev, id);
  1187. if (entity == NULL) {
  1188. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1189. "unknown entity %d.\n", id);
  1190. return -EINVAL;
  1191. }
  1192. *_entity = entity;
  1193. return 0;
  1194. }
  1195. static int uvc_scan_chain(struct uvc_video_chain *chain,
  1196. struct uvc_entity *term)
  1197. {
  1198. struct uvc_entity *entity, *prev;
  1199. uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
  1200. entity = term;
  1201. prev = NULL;
  1202. while (entity != NULL) {
  1203. /* Entity must not be part of an existing chain */
  1204. if (entity->chain.next || entity->chain.prev) {
  1205. uvc_trace(UVC_TRACE_DESCR, "Found reference to "
  1206. "entity %d already in chain.\n", entity->id);
  1207. return -EINVAL;
  1208. }
  1209. /* Process entity */
  1210. if (uvc_scan_chain_entity(chain, entity) < 0)
  1211. return -EINVAL;
  1212. /* Forward scan */
  1213. if (uvc_scan_chain_forward(chain, entity, prev) < 0)
  1214. return -EINVAL;
  1215. /* Backward scan */
  1216. prev = entity;
  1217. if (uvc_scan_chain_backward(chain, &entity) < 0)
  1218. return -EINVAL;
  1219. }
  1220. return 0;
  1221. }
  1222. static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
  1223. char *buffer)
  1224. {
  1225. struct uvc_entity *term;
  1226. unsigned int nterms = 0;
  1227. char *p = buffer;
  1228. list_for_each_entry(term, terms, chain) {
  1229. if (!UVC_ENTITY_IS_TERM(term) ||
  1230. UVC_TERM_DIRECTION(term) != dir)
  1231. continue;
  1232. if (nterms)
  1233. p += sprintf(p, ",");
  1234. if (++nterms >= 4) {
  1235. p += sprintf(p, "...");
  1236. break;
  1237. }
  1238. p += sprintf(p, "%u", term->id);
  1239. }
  1240. return p - buffer;
  1241. }
  1242. static const char *uvc_print_chain(struct uvc_video_chain *chain)
  1243. {
  1244. static char buffer[43];
  1245. char *p = buffer;
  1246. p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
  1247. p += sprintf(p, " -> ");
  1248. uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
  1249. return buffer;
  1250. }
  1251. /*
  1252. * Scan the device for video chains and register video devices.
  1253. *
  1254. * Chains are scanned starting at their output terminals and walked backwards.
  1255. */
  1256. static int uvc_scan_device(struct uvc_device *dev)
  1257. {
  1258. struct uvc_video_chain *chain;
  1259. struct uvc_entity *term;
  1260. list_for_each_entry(term, &dev->entities, list) {
  1261. if (!UVC_ENTITY_IS_OTERM(term))
  1262. continue;
  1263. /* If the terminal is already included in a chain, skip it.
  1264. * This can happen for chains that have multiple output
  1265. * terminals, where all output terminals beside the first one
  1266. * will be inserted in the chain in forward scans.
  1267. */
  1268. if (term->chain.next || term->chain.prev)
  1269. continue;
  1270. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  1271. if (chain == NULL)
  1272. return -ENOMEM;
  1273. INIT_LIST_HEAD(&chain->entities);
  1274. mutex_init(&chain->ctrl_mutex);
  1275. chain->dev = dev;
  1276. if (uvc_scan_chain(chain, term) < 0) {
  1277. kfree(chain);
  1278. continue;
  1279. }
  1280. uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
  1281. uvc_print_chain(chain));
  1282. list_add_tail(&chain->list, &dev->chains);
  1283. }
  1284. if (list_empty(&dev->chains)) {
  1285. uvc_printk(KERN_INFO, "No valid video chain found.\n");
  1286. return -1;
  1287. }
  1288. return 0;
  1289. }
  1290. /* ------------------------------------------------------------------------
  1291. * Video device registration and unregistration
  1292. */
  1293. /*
  1294. * Delete the UVC device.
  1295. *
  1296. * Called by the kernel when the last reference to the uvc_device structure
  1297. * is released.
  1298. *
  1299. * As this function is called after or during disconnect(), all URBs have
  1300. * already been canceled by the USB core. There is no need to kill the
  1301. * interrupt URB manually.
  1302. */
  1303. static void uvc_delete(struct uvc_device *dev)
  1304. {
  1305. struct list_head *p, *n;
  1306. usb_put_intf(dev->intf);
  1307. usb_put_dev(dev->udev);
  1308. uvc_status_cleanup(dev);
  1309. uvc_ctrl_cleanup_device(dev);
  1310. list_for_each_safe(p, n, &dev->chains) {
  1311. struct uvc_video_chain *chain;
  1312. chain = list_entry(p, struct uvc_video_chain, list);
  1313. kfree(chain);
  1314. }
  1315. list_for_each_safe(p, n, &dev->entities) {
  1316. struct uvc_entity *entity;
  1317. entity = list_entry(p, struct uvc_entity, list);
  1318. kfree(entity);
  1319. }
  1320. list_for_each_safe(p, n, &dev->streams) {
  1321. struct uvc_streaming *streaming;
  1322. streaming = list_entry(p, struct uvc_streaming, list);
  1323. usb_driver_release_interface(&uvc_driver.driver,
  1324. streaming->intf);
  1325. usb_put_intf(streaming->intf);
  1326. kfree(streaming->format);
  1327. kfree(streaming->header.bmaControls);
  1328. kfree(streaming);
  1329. }
  1330. kfree(dev);
  1331. }
  1332. static void uvc_release(struct video_device *vdev)
  1333. {
  1334. struct uvc_streaming *stream = video_get_drvdata(vdev);
  1335. struct uvc_device *dev = stream->dev;
  1336. video_device_release(vdev);
  1337. /* Decrement the registered streams count and delete the device when it
  1338. * reaches zero.
  1339. */
  1340. if (atomic_dec_and_test(&dev->nstreams))
  1341. uvc_delete(dev);
  1342. }
  1343. /*
  1344. * Unregister the video devices.
  1345. */
  1346. static void uvc_unregister_video(struct uvc_device *dev)
  1347. {
  1348. struct uvc_streaming *stream;
  1349. /* Unregistering all video devices might result in uvc_delete() being
  1350. * called from inside the loop if there's no open file handle. To avoid
  1351. * that, increment the stream count before iterating over the streams
  1352. * and decrement it when done.
  1353. */
  1354. atomic_inc(&dev->nstreams);
  1355. list_for_each_entry(stream, &dev->streams, list) {
  1356. if (stream->vdev == NULL)
  1357. continue;
  1358. video_unregister_device(stream->vdev);
  1359. stream->vdev = NULL;
  1360. }
  1361. /* Decrement the stream count and call uvc_delete explicitly if there
  1362. * are no stream left.
  1363. */
  1364. if (atomic_dec_and_test(&dev->nstreams))
  1365. uvc_delete(dev);
  1366. }
  1367. static int uvc_register_video(struct uvc_device *dev,
  1368. struct uvc_streaming *stream)
  1369. {
  1370. struct video_device *vdev;
  1371. int ret;
  1372. /* Initialize the streaming interface with default streaming
  1373. * parameters.
  1374. */
  1375. ret = uvc_video_init(stream);
  1376. if (ret < 0) {
  1377. uvc_printk(KERN_ERR, "Failed to initialize the device "
  1378. "(%d).\n", ret);
  1379. return ret;
  1380. }
  1381. /* Register the device with V4L. */
  1382. vdev = video_device_alloc();
  1383. if (vdev == NULL) {
  1384. uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
  1385. ret);
  1386. return -ENOMEM;
  1387. }
  1388. /* We already hold a reference to dev->udev. The video device will be
  1389. * unregistered before the reference is released, so we don't need to
  1390. * get another one.
  1391. */
  1392. vdev->parent = &dev->intf->dev;
  1393. vdev->minor = -1;
  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. * Driver initialization and cleanup
  1588. */
  1589. /*
  1590. * The Logitech cameras listed below have their interface class set to
  1591. * VENDOR_SPEC because they don't announce themselves as UVC devices, even
  1592. * though they are compliant.
  1593. */
  1594. static struct usb_device_id uvc_ids[] = {
  1595. /* Genius eFace 2025 */
  1596. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1597. | USB_DEVICE_ID_MATCH_INT_INFO,
  1598. .idVendor = 0x0458,
  1599. .idProduct = 0x706e,
  1600. .bInterfaceClass = USB_CLASS_VIDEO,
  1601. .bInterfaceSubClass = 1,
  1602. .bInterfaceProtocol = 0,
  1603. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1604. /* Microsoft Lifecam NX-6000 */
  1605. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1606. | USB_DEVICE_ID_MATCH_INT_INFO,
  1607. .idVendor = 0x045e,
  1608. .idProduct = 0x00f8,
  1609. .bInterfaceClass = USB_CLASS_VIDEO,
  1610. .bInterfaceSubClass = 1,
  1611. .bInterfaceProtocol = 0,
  1612. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1613. /* Microsoft Lifecam VX-7000 */
  1614. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1615. | USB_DEVICE_ID_MATCH_INT_INFO,
  1616. .idVendor = 0x045e,
  1617. .idProduct = 0x0723,
  1618. .bInterfaceClass = USB_CLASS_VIDEO,
  1619. .bInterfaceSubClass = 1,
  1620. .bInterfaceProtocol = 0,
  1621. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1622. /* Logitech Quickcam Fusion */
  1623. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1624. | USB_DEVICE_ID_MATCH_INT_INFO,
  1625. .idVendor = 0x046d,
  1626. .idProduct = 0x08c1,
  1627. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1628. .bInterfaceSubClass = 1,
  1629. .bInterfaceProtocol = 0 },
  1630. /* Logitech Quickcam Orbit MP */
  1631. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1632. | USB_DEVICE_ID_MATCH_INT_INFO,
  1633. .idVendor = 0x046d,
  1634. .idProduct = 0x08c2,
  1635. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1636. .bInterfaceSubClass = 1,
  1637. .bInterfaceProtocol = 0 },
  1638. /* Logitech Quickcam Pro for Notebook */
  1639. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1640. | USB_DEVICE_ID_MATCH_INT_INFO,
  1641. .idVendor = 0x046d,
  1642. .idProduct = 0x08c3,
  1643. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1644. .bInterfaceSubClass = 1,
  1645. .bInterfaceProtocol = 0 },
  1646. /* Logitech Quickcam Pro 5000 */
  1647. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1648. | USB_DEVICE_ID_MATCH_INT_INFO,
  1649. .idVendor = 0x046d,
  1650. .idProduct = 0x08c5,
  1651. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1652. .bInterfaceSubClass = 1,
  1653. .bInterfaceProtocol = 0 },
  1654. /* Logitech Quickcam OEM Dell Notebook */
  1655. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1656. | USB_DEVICE_ID_MATCH_INT_INFO,
  1657. .idVendor = 0x046d,
  1658. .idProduct = 0x08c6,
  1659. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1660. .bInterfaceSubClass = 1,
  1661. .bInterfaceProtocol = 0 },
  1662. /* Logitech Quickcam OEM Cisco VT Camera II */
  1663. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1664. | USB_DEVICE_ID_MATCH_INT_INFO,
  1665. .idVendor = 0x046d,
  1666. .idProduct = 0x08c7,
  1667. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  1668. .bInterfaceSubClass = 1,
  1669. .bInterfaceProtocol = 0 },
  1670. /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
  1671. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1672. | USB_DEVICE_ID_MATCH_INT_INFO,
  1673. .idVendor = 0x058f,
  1674. .idProduct = 0x3820,
  1675. .bInterfaceClass = USB_CLASS_VIDEO,
  1676. .bInterfaceSubClass = 1,
  1677. .bInterfaceProtocol = 0,
  1678. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1679. /* Apple Built-In iSight */
  1680. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1681. | USB_DEVICE_ID_MATCH_INT_INFO,
  1682. .idVendor = 0x05ac,
  1683. .idProduct = 0x8501,
  1684. .bInterfaceClass = USB_CLASS_VIDEO,
  1685. .bInterfaceSubClass = 1,
  1686. .bInterfaceProtocol = 0,
  1687. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1688. | UVC_QUIRK_BUILTIN_ISIGHT },
  1689. /* Genesys Logic USB 2.0 PC Camera */
  1690. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1691. | USB_DEVICE_ID_MATCH_INT_INFO,
  1692. .idVendor = 0x05e3,
  1693. .idProduct = 0x0505,
  1694. .bInterfaceClass = USB_CLASS_VIDEO,
  1695. .bInterfaceSubClass = 1,
  1696. .bInterfaceProtocol = 0,
  1697. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1698. /* ViMicro Vega */
  1699. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1700. | USB_DEVICE_ID_MATCH_INT_INFO,
  1701. .idVendor = 0x0ac8,
  1702. .idProduct = 0x332d,
  1703. .bInterfaceClass = USB_CLASS_VIDEO,
  1704. .bInterfaceSubClass = 1,
  1705. .bInterfaceProtocol = 0,
  1706. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1707. /* ViMicro - Minoru3D */
  1708. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1709. | USB_DEVICE_ID_MATCH_INT_INFO,
  1710. .idVendor = 0x0ac8,
  1711. .idProduct = 0x3410,
  1712. .bInterfaceClass = USB_CLASS_VIDEO,
  1713. .bInterfaceSubClass = 1,
  1714. .bInterfaceProtocol = 0,
  1715. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1716. /* ViMicro Venus - Minoru3D */
  1717. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1718. | USB_DEVICE_ID_MATCH_INT_INFO,
  1719. .idVendor = 0x0ac8,
  1720. .idProduct = 0x3420,
  1721. .bInterfaceClass = USB_CLASS_VIDEO,
  1722. .bInterfaceSubClass = 1,
  1723. .bInterfaceProtocol = 0,
  1724. .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
  1725. /* MT6227 */
  1726. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1727. | USB_DEVICE_ID_MATCH_INT_INFO,
  1728. .idVendor = 0x0e8d,
  1729. .idProduct = 0x0004,
  1730. .bInterfaceClass = USB_CLASS_VIDEO,
  1731. .bInterfaceSubClass = 1,
  1732. .bInterfaceProtocol = 0,
  1733. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1734. | UVC_QUIRK_PROBE_DEF },
  1735. /* Syntek (HP Spartan) */
  1736. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1737. | USB_DEVICE_ID_MATCH_INT_INFO,
  1738. .idVendor = 0x174f,
  1739. .idProduct = 0x5212,
  1740. .bInterfaceClass = USB_CLASS_VIDEO,
  1741. .bInterfaceSubClass = 1,
  1742. .bInterfaceProtocol = 0,
  1743. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1744. /* Syntek (Samsung Q310) */
  1745. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1746. | USB_DEVICE_ID_MATCH_INT_INFO,
  1747. .idVendor = 0x174f,
  1748. .idProduct = 0x5931,
  1749. .bInterfaceClass = USB_CLASS_VIDEO,
  1750. .bInterfaceSubClass = 1,
  1751. .bInterfaceProtocol = 0,
  1752. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1753. /* Syntek (Asus F9SG) */
  1754. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1755. | USB_DEVICE_ID_MATCH_INT_INFO,
  1756. .idVendor = 0x174f,
  1757. .idProduct = 0x8a31,
  1758. .bInterfaceClass = USB_CLASS_VIDEO,
  1759. .bInterfaceSubClass = 1,
  1760. .bInterfaceProtocol = 0,
  1761. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1762. /* Syntek (Asus U3S) */
  1763. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1764. | USB_DEVICE_ID_MATCH_INT_INFO,
  1765. .idVendor = 0x174f,
  1766. .idProduct = 0x8a33,
  1767. .bInterfaceClass = USB_CLASS_VIDEO,
  1768. .bInterfaceSubClass = 1,
  1769. .bInterfaceProtocol = 0,
  1770. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1771. /* Syntek (JAOtech Smart Terminal) */
  1772. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1773. | USB_DEVICE_ID_MATCH_INT_INFO,
  1774. .idVendor = 0x174f,
  1775. .idProduct = 0x8a34,
  1776. .bInterfaceClass = USB_CLASS_VIDEO,
  1777. .bInterfaceSubClass = 1,
  1778. .bInterfaceProtocol = 0,
  1779. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1780. /* Lenovo Thinkpad SL400/SL500 */
  1781. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1782. | USB_DEVICE_ID_MATCH_INT_INFO,
  1783. .idVendor = 0x17ef,
  1784. .idProduct = 0x480b,
  1785. .bInterfaceClass = USB_CLASS_VIDEO,
  1786. .bInterfaceSubClass = 1,
  1787. .bInterfaceProtocol = 0,
  1788. .driver_info = UVC_QUIRK_STREAM_NO_FID },
  1789. /* Aveo Technology USB 2.0 Camera */
  1790. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1791. | USB_DEVICE_ID_MATCH_INT_INFO,
  1792. .idVendor = 0x1871,
  1793. .idProduct = 0x0306,
  1794. .bInterfaceClass = USB_CLASS_VIDEO,
  1795. .bInterfaceSubClass = 1,
  1796. .bInterfaceProtocol = 0,
  1797. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1798. | UVC_QUIRK_PROBE_EXTRAFIELDS },
  1799. /* Ecamm Pico iMage */
  1800. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1801. | USB_DEVICE_ID_MATCH_INT_INFO,
  1802. .idVendor = 0x18cd,
  1803. .idProduct = 0xcafe,
  1804. .bInterfaceClass = USB_CLASS_VIDEO,
  1805. .bInterfaceSubClass = 1,
  1806. .bInterfaceProtocol = 0,
  1807. .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
  1808. /* FSC WebCam V30S */
  1809. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1810. | USB_DEVICE_ID_MATCH_INT_INFO,
  1811. .idVendor = 0x18ec,
  1812. .idProduct = 0x3288,
  1813. .bInterfaceClass = USB_CLASS_VIDEO,
  1814. .bInterfaceSubClass = 1,
  1815. .bInterfaceProtocol = 0,
  1816. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1817. /* Bodelin ProScopeHR */
  1818. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1819. | USB_DEVICE_ID_MATCH_DEV_HI
  1820. | USB_DEVICE_ID_MATCH_INT_INFO,
  1821. .idVendor = 0x19ab,
  1822. .idProduct = 0x1000,
  1823. .bcdDevice_hi = 0x0126,
  1824. .bInterfaceClass = USB_CLASS_VIDEO,
  1825. .bInterfaceSubClass = 1,
  1826. .bInterfaceProtocol = 0,
  1827. .driver_info = UVC_QUIRK_STATUS_INTERVAL },
  1828. /* MSI StarCam 370i */
  1829. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1830. | USB_DEVICE_ID_MATCH_INT_INFO,
  1831. .idVendor = 0x1b3b,
  1832. .idProduct = 0x2951,
  1833. .bInterfaceClass = USB_CLASS_VIDEO,
  1834. .bInterfaceSubClass = 1,
  1835. .bInterfaceProtocol = 0,
  1836. .driver_info = UVC_QUIRK_PROBE_MINMAX },
  1837. /* SiGma Micro USB Web Camera */
  1838. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
  1839. | USB_DEVICE_ID_MATCH_INT_INFO,
  1840. .idVendor = 0x1c4f,
  1841. .idProduct = 0x3000,
  1842. .bInterfaceClass = USB_CLASS_VIDEO,
  1843. .bInterfaceSubClass = 1,
  1844. .bInterfaceProtocol = 0,
  1845. .driver_info = UVC_QUIRK_PROBE_MINMAX
  1846. | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
  1847. /* Generic USB Video Class */
  1848. { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
  1849. {}
  1850. };
  1851. MODULE_DEVICE_TABLE(usb, uvc_ids);
  1852. struct uvc_driver uvc_driver = {
  1853. .driver = {
  1854. .name = "uvcvideo",
  1855. .probe = uvc_probe,
  1856. .disconnect = uvc_disconnect,
  1857. .suspend = uvc_suspend,
  1858. .resume = uvc_resume,
  1859. .reset_resume = uvc_reset_resume,
  1860. .id_table = uvc_ids,
  1861. .supports_autosuspend = 1,
  1862. },
  1863. };
  1864. static int __init uvc_init(void)
  1865. {
  1866. int result;
  1867. INIT_LIST_HEAD(&uvc_driver.devices);
  1868. INIT_LIST_HEAD(&uvc_driver.controls);
  1869. mutex_init(&uvc_driver.ctrl_mutex);
  1870. uvc_ctrl_init();
  1871. result = usb_register(&uvc_driver.driver);
  1872. if (result == 0)
  1873. printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
  1874. return result;
  1875. }
  1876. static void __exit uvc_cleanup(void)
  1877. {
  1878. usb_deregister(&uvc_driver.driver);
  1879. }
  1880. module_init(uvc_init);
  1881. module_exit(uvc_cleanup);
  1882. module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
  1883. MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
  1884. module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
  1885. MODULE_PARM_DESC(quirks, "Forced device quirks");
  1886. module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
  1887. MODULE_PARM_DESC(trace, "Trace level bitmask");
  1888. module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
  1889. MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
  1890. MODULE_AUTHOR(DRIVER_AUTHOR);
  1891. MODULE_DESCRIPTION(DRIVER_DESC);
  1892. MODULE_LICENSE("GPL");
  1893. MODULE_VERSION(DRIVER_VERSION);