uvc_driver.c 59 KB

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