uvc_driver.c 59 KB

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