uvc_driver.c 61 KB

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