uvc_driver.c 60 KB

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