uvc_driver.c 53 KB

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