uvc_driver.c 58 KB

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