uvc_driver.c 56 KB

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