uvc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include "shared.h"
  2. #include "webcam.h"
  3. #include "uvc.h"
  4. /****************************
  5. * UTILS *
  6. ****************************/
  7. static int r5u870_uvc_req( struct driverInstance* this, int cmd, u8 valhi, u8 vallow, u8 idxhi, u8 idxlow, u8* buf, int len ){
  8. int out, res, stres;
  9. int tries = 5;
  10. u8 stbuf[1];
  11. out = (cmd == UVC_SET_CUR) ? 1 : 0;
  12. retry:
  13. //TODO: Base our other retry control message off this one.
  14. res = r5u870_control_msg(
  15. this,
  16. out,
  17. 1,
  18. cmd,
  19. (valhi << 8) | vallow,
  20. (idxhi << 8) | idxlow,
  21. buf,
  22. len
  23. );
  24. if( res != -EPIPE ) goto complete;
  25. stres = r5u870_control_msg(
  26. this,
  27. 0,
  28. 1,
  29. UVC_GET_CUR,
  30. UVC_VC_REQUEST_ERROR_CODE_CONTROL << 8,
  31. this->vh_ctrl_ifnum,
  32. stbuf,
  33. sizeof(stbuf)
  34. );
  35. if (((stres == -EPIPE) && --tries) || ((stres == 1) && (stbuf[0] == 1) && --tries)){
  36. msleep(5);
  37. printk( KERN_ERR "[%s] uvc_req: retrying - EPIPE/stres error.\n", KBUILD_MODNAME );
  38. goto retry;
  39. }
  40. if( stres != 1 ){
  41. printk( KERN_ERR "[%s] uvc_req: status req failed: %d\n", KBUILD_MODNAME, stres );
  42. goto complete;
  43. }else printk( KERN_ERR "[%s] uvc_req: status %d\n", KBUILD_MODNAME, stbuf[0] );
  44. complete:
  45. if( res < 0 ) printk( KERN_ERR "[%s] uvc_req %02x/%02x%02x/%02x%02x failed: %d\n", KBUILD_MODNAME, cmd, valhi, vallow, idxhi, idxlow, res );
  46. return res;
  47. }
  48. static int usbcam_claim_interface( struct driverInstance* this, int interfaceNumber ){
  49. struct usb_interface* interface;
  50. struct claimed_interface* interfaceCapsule;
  51. int res;
  52. interface = usb_ifnum_to_if( this->usbDevice, interfaceNumber );
  53. if( !interface ){
  54. printk( KERN_ERR "[%s] %s: interface %d does not exist\n", KBUILD_MODNAME, __FUNCTION__, interfaceNumber );
  55. return -ENODEV;
  56. }
  57. res = usb_driver_claim_interface( this->usbDeviceDriver, interface, NULL );
  58. if( res ) return res;
  59. interfaceCapsule = devm_kzalloc( this->dev, sizeof( struct claimed_interface ), GFP_KERNEL );
  60. if( !interfaceCapsule ){
  61. usb_driver_release_interface( this->usbDeviceDriver, interface );
  62. return -ENOMEM;
  63. }
  64. INIT_LIST_HEAD( &interfaceCapsule->listItem );// ???
  65. interfaceCapsule->interface = usb_get_intf( interface );
  66. usb_set_intfdata( interface, this );
  67. list_add_tail( &interfaceCapsule->listItem, &this->claimedInterfacesList );
  68. return res;
  69. }
  70. /****************************
  71. * PACKET PROCESSING *
  72. ****************************/
  73. int r5u870_decide_pkt_uvc( struct driverInstance* this, int pktstatus, int pktlen, const u8* pktdata, int* start ){
  74. if( !pktlen ){
  75. this->vh_emptypkts++;
  76. if ( this->vh_emptypkts >= R5U870_EMPTYPKT_GIVE_UP ){
  77. if( printk_ratelimit() ) printk( KERN_ERR "[%s] capture abort: too many empty pkts\n", KBUILD_MODNAME );
  78. return -EIO;
  79. }
  80. if ( this->vh_frame_accum < 0 ) return -EPIPE;
  81. return -EAGAIN;
  82. }
  83. this->vh_emptypkts = 0;
  84. if( this->vh_frame_accum < 0 ){
  85. if( pktdata[1] & 2 ) this->vh_frame_accum = 0;
  86. return -EPIPE;
  87. }
  88. if( ( pktdata[0] < 2 ) || ( pktdata[0] > pktlen ) ){
  89. if( printk_ratelimit() ) printk( KERN_ERR "[%s] capture abort: hdrlen=%d pktlen=%d\n", KBUILD_MODNAME, pktdata[0], pktlen );
  90. return -EIO;
  91. }
  92. this->vh_frame_accum += pktlen - pktdata[0];
  93. if( this->vh_frame_accum > this->webcamCurrentPixelformat.sizeimage ){
  94. if( printk_ratelimit() ) printk( KERN_ERR "[%s] frame abort: accum=%d\n", KBUILD_MODNAME, this->vh_frame_accum );
  95. this->vh_frame_accum = -1;
  96. return -EPIPE;
  97. }
  98. *start = pktdata[0];
  99. if( pktdata[1] & 2 ){
  100. if( this->vh_frame_accum < this->webcamCurrentPixelformat.sizeimage ){
  101. if( printk_ratelimit() ) printk( KERN_WARNING "[%s] short frame (exp:%d got:%d)\n", KBUILD_MODNAME, this->webcamCurrentPixelformat.sizeimage, this->vh_frame_accum );
  102. }
  103. this->vh_frame_accum = 0;
  104. return 0;
  105. }
  106. return -EAGAIN;
  107. }
  108. //UVC capture is controlled by changing the altsetting
  109. int r5u870_cap_stop_uvc( struct driverInstance* this ){
  110. return 0;
  111. }
  112. /****************************
  113. * FORMATS *
  114. ****************************/
  115. //r5u870_set_fmt_uvc
  116. int webcamApplyFormat_uvc( struct driverInstance* this ){
  117. unsigned char buf[26];
  118. int res;
  119. memset( buf, 0, sizeof(buf) );
  120. buf[2] = this->webcamCurrentFormat->formatId;
  121. buf[3] = this->webcamCurrentResolution->resolutionId;
  122. *(__le32 *) &buf[4] = cpu_to_le32( this->webcamCurrentResolution->rw_interval );
  123. printk( KERN_NOTICE "[%s] set_format: fmtidx:%d frameidx:%d %dx%d ival:%d\n",
  124. KBUILD_MODNAME,
  125. this->webcamCurrentFormat->formatId,
  126. this->webcamCurrentResolution->resolutionId,
  127. this->webcamCurrentResolution->width,
  128. this->webcamCurrentResolution->height,
  129. this->webcamCurrentResolution->rw_interval );
  130. res = r5u870_uvc_req( this, UVC_SET_CUR, UVC_VS_PROBE_CONTROL, 0, 0, this->urbstreamInterfaceNumber, buf, sizeof(buf) );
  131. if( res != sizeof(buf) ){
  132. printk( KERN_ERR "[%s] probe_control set_cur1: short write %d\n", KBUILD_MODNAME, res );
  133. return -EIO;
  134. }
  135. res = r5u870_uvc_req( this, UVC_GET_CUR, UVC_VS_PROBE_CONTROL, 0, 0, this->urbstreamInterfaceNumber, buf, sizeof(buf) );
  136. if ( res != sizeof(buf) ){
  137. printk( KERN_ERR "[%s] probe_control get_cur: short read %d\n", KBUILD_MODNAME, res );
  138. return -EIO;
  139. }
  140. if( buf[2] != this->webcamCurrentFormat->formatId ){
  141. printk( KERN_ERR "[%s] probe_control get_cur: got fmt %d\n", KBUILD_MODNAME, buf[2] );
  142. return -EIO;
  143. }
  144. if( buf[3] != this->webcamCurrentResolution->resolutionId ){
  145. printk( KERN_ERR "[%s] probe_control get_cur: got frame idx %d\n", KBUILD_MODNAME, buf[3] );
  146. return -EIO;
  147. }
  148. res = r5u870_uvc_req( this, UVC_SET_CUR, UVC_VS_COMMIT_CONTROL, 0, 0, this->urbstreamInterfaceNumber, buf, sizeof(buf) );
  149. if ( res != sizeof(buf) ){
  150. printk( KERN_ERR "[%s] commit_control set_cur2: short write %d\n", KBUILD_MODNAME, res );
  151. return -EIO;
  152. }
  153. this->urbstreamMinPacketSize = le32_to_cpup((__le32 *) &buf[22]);
  154. return 0;
  155. }
  156. static int r5u870_uvc_add_resolution( struct driverInstance* this, struct r5u870_pixelformat* fmtp, int width, int height, int reqbw, int frameidx, int interval ){
  157. int i;
  158. struct r5u870_resolution* resp;
  159. if( !width || !height ){
  160. printk( KERN_ERR "[%s] invalid resolution descriptor %d at %dx%d\n", KBUILD_MODNAME, frameidx, width, height );
  161. return -EINVAL;
  162. }
  163. resp = NULL;
  164. for( i = 0; i < fmtp->resolutionTableAlloc; i++ ){
  165. if( !fmtp->resolutionTable[i].width ){
  166. resp = (struct r5u870_resolution*) &fmtp->resolutionTable[i];
  167. break;
  168. }
  169. }
  170. if( !resp ){
  171. printk( KERN_NOTICE "[%s] no space for resolution descriptor %d at %dx%d\n", KBUILD_MODNAME, frameidx, width, height );
  172. return 0;
  173. }
  174. resp->width = width;
  175. resp->height = height;
  176. resp->resolutionId = frameidx;
  177. resp->requiredBandwidth = reqbw;
  178. resp->rw_interval = interval;
  179. printk( KERN_NOTICE "[%s] Found resolution %d: %dx%d ival %d (%d B/s)\n", KBUILD_MODNAME, frameidx, width, height, interval, reqbw );
  180. return 0;
  181. }
  182. static int r5u870_uvc_add_fmt( struct driverInstance* this, const u8* guid, int fmtidx, int nresolutions, struct r5u870_pixelformat** new_fmt ){
  183. const struct r5u870_uvc_fmtinfo* formatTemplate = NULL;
  184. struct r5u870_pixelformat* newFormats;
  185. struct r5u870_pixelformat* newFormatPointer;
  186. int i;
  187. for( i = 0; r5u870_uvc_fmts[i].fi_name != NULL; i++ ){
  188. if( !memcmp(r5u870_uvc_fmts[i].fi_guid, guid, 16) ){
  189. formatTemplate = &r5u870_uvc_fmts[i];
  190. break;
  191. }
  192. }
  193. if( formatTemplate == NULL ){
  194. printk( KERN_NOTICE "[%s] unknown format "
  195. "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
  196. "%02x%02x%02x%02x%02x%02x\n", KBUILD_MODNAME,
  197. guid[0], guid[1], guid[2], guid[3],
  198. guid[4], guid[5], guid[6], guid[7],
  199. guid[8], guid[9], guid[10], guid[11],
  200. guid[12], guid[13], guid[14], guid[15]
  201. );
  202. return -ENOENT;
  203. }
  204. newFormats = devm_kzalloc( this->dev, (this->webcamFormatsCount+1) * sizeof( struct r5u870_pixelformat ), GFP_KERNEL );
  205. if( !newFormats )return -ENOMEM;
  206. if( this->webcamFormatsCount ) memcpy( newFormats, this->webcamFormats, this->webcamFormatsCount * sizeof(struct r5u870_pixelformat) );
  207. newFormatPointer = &newFormats[this->webcamFormatsCount];
  208. strlcpy( newFormatPointer->description, formatTemplate->fi_name, sizeof(newFormatPointer->description) );
  209. newFormatPointer->pixelformat = formatTemplate->fi_v4l_id;
  210. newFormatPointer->formatId = fmtidx;
  211. newFormatPointer->resolutionTable = devm_kzalloc( this->dev, (nresolutions+1) * sizeof( struct r5u870_resolution ), GFP_KERNEL );
  212. if( !newFormatPointer->resolutionTable ) return -ENOMEM;
  213. newFormatPointer->resolutionTableAlloc = nresolutions;
  214. /*
  215. Memory leak?
  216. If there is more than one format, then this function will be called
  217. more than once. In this case, the variable this->webcamFormats contains
  218. a pointer to memory, which was allocated the previous time. If this
  219. memory is not freed now, the pointer to it will be lost. I don't have a
  220. UVC camera to test it. Perhaps this is not a mistake and was conceived
  221. so and I did not fully understand the idea. Anyway, I use devm_kzalloc to
  222. allocate memory, so all memory is freed anyway when the driver is unloaded.
  223. */
  224. //void devm_kfree( struct device* dev, void* p );
  225. this->webcamFormats = newFormats;
  226. this->webcamFormatsCount++;
  227. if( new_fmt ) *new_fmt = newFormatPointer;
  228. printk( KERN_NOTICE "[%s] Found format %d: %c%c%c%c (%d frames)\n",
  229. KBUILD_MODNAME,
  230. fmtidx,
  231. newFormatPointer->pixelformat & 0xff,
  232. ( newFormatPointer->pixelformat >> 8) & 0xff,
  233. ( newFormatPointer->pixelformat >> 16) & 0xff,
  234. ( newFormatPointer->pixelformat >> 24) & 0xff,
  235. nresolutions
  236. );
  237. return 0;
  238. }
  239. /****************************
  240. * CONTROLS *
  241. ****************************/
  242. static int r5u870_uvc_ctrl_req( struct driverInstance* this, const struct webcam_control* ctrlp, int req, int* value ){
  243. u8 buf[4];
  244. int size, i, val, res;
  245. size = ctrlp->size;
  246. if( req == UVC_GET_INFO ) size = 1;
  247. if( size > sizeof(buf) ){
  248. printk( KERN_ERR "[%s] Control ID %d is too large, %d bytes\n", KBUILD_MODNAME, ctrlp->reg, size );
  249. return -EINVAL;
  250. }
  251. memset( buf, 0, sizeof(buf) );
  252. if( req != UVC_SET_CUR ){
  253. res = r5u870_uvc_req( this, req, ctrlp->reg, 0, ctrlp->unit, this->vh_ctrl_ifnum, buf, size );
  254. if( res < 0 ) return res;
  255. if( res != size ){
  256. printk( KERN_ERR "[%s] short read for UVC control %d\n", KBUILD_MODNAME, ctrlp->reg );
  257. return -EIO;
  258. }
  259. val = 0;
  260. switch( size ){
  261. case 1:
  262. val = buf[0];
  263. break;
  264. case 2:
  265. val = le16_to_cpu(*(__le16 *) buf);
  266. break;
  267. case 4:
  268. val = le32_to_cpu(*(__le32 *) buf);
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. *value = val;
  274. return 0;
  275. }
  276. val = *value;
  277. for( i = 0; i < size; i++, val >>= 8 ) buf[i] = val & 0xff;
  278. res = r5u870_uvc_req( this, UVC_SET_CUR, ctrlp->reg, 0, ctrlp->unit, this->vh_ctrl_ifnum, buf, size );
  279. if( res < 0 ) return res;
  280. if( res != size ){
  281. printk( KERN_ERR "[%s] short write for UVC control %d\n", KBUILD_MODNAME, ctrlp->reg );
  282. return -EIO;
  283. }
  284. return 0;
  285. }
  286. //r5u870_set_ctrl_uvc replaced with
  287. static int webcamSetControlCallback_uvc( struct v4l2_ctrl* control ){
  288. struct driverInstance* this = container_of( control->handler, struct driverInstance, webcamControlHandler );
  289. struct webcam_control* webcamControl;
  290. int val;
  291. int res = 0;
  292. if( !control->priv ){
  293. printk( KERN_ERR "[%s] %s: control->priv == NULL\n", KBUILD_MODNAME, __FUNCTION__ );
  294. return -EINVAL;
  295. }
  296. webcamControl = control->priv;
  297. if( !(webcamControl->info & 2) ){
  298. if( printk_ratelimit() ) printk( KERN_WARNING "[%s] control %s <= %d [set not supported]\n", KBUILD_MODNAME, webcamControl->control.name, control->val );
  299. return -EINVAL;
  300. }
  301. val = control->val;
  302. res = r5u870_uvc_ctrl_req( this, webcamControl, UVC_SET_CUR, &val );
  303. if( res ) printk( KERN_ERR "[%s] FAILED set %s/uvc %02x <= %d\n", KBUILD_MODNAME, webcamControl->control.name, webcamControl->reg, control->val );
  304. return res;
  305. }
  306. //Initialize a control structure based on a UVC descriptor array,
  307. //and a bit within a supported control bitmap.
  308. static int r5u870_uvc_init_ctrl( struct driverInstance* this, int unit, int bit_offset ){
  309. const struct r5u870_uvc_ctrlinfo* cip = NULL;
  310. struct webcam_control* ctrlp;
  311. int i, res, val;
  312. for( i = 0; r5u870_uvc_proc_ctrls[i].ci_name != NULL; i++ ){
  313. if( r5u870_uvc_proc_ctrls[i].ci_bm_index == bit_offset ){
  314. cip = &r5u870_uvc_proc_ctrls[i];
  315. break;
  316. }
  317. }
  318. if( !cip ) return -ENOENT;
  319. ctrlp = devm_kzalloc( this->dev, sizeof( struct webcam_control ), GFP_KERNEL );
  320. if( !ctrlp ) return -ENOMEM;
  321. ctrlp->control.ops = &webcamControlOps_uvc;
  322. ctrlp->control.id = cip->ci_v4l_id;
  323. ctrlp->control.type = cip->ci_v4l_type;
  324. ctrlp->control.name = cip->ci_name;
  325. ctrlp->control.flags = cip->ci_v4l_flags;
  326. if( ctrlp->control.type != V4L2_CTRL_TYPE_MENU ) ctrlp->control.step = 1;
  327. ctrlp->control.qmenu = cip->ci_menu_names;
  328. ctrlp->reg = cip->ci_reg;
  329. ctrlp->unit = unit;
  330. ctrlp->size = cip->ci_size;
  331. res = r5u870_uvc_ctrl_req( this, ctrlp, UVC_GET_INFO, &val );
  332. if( res ) return res;
  333. ctrlp->info = val;
  334. if( cip->ci_min_force ) ctrlp->control.min = cip->ci_min;
  335. else{
  336. res = r5u870_uvc_ctrl_req( this, ctrlp, UVC_GET_MIN, &val );
  337. if( res < 0 ) return res;
  338. ctrlp->control.min = val;
  339. }
  340. if( cip->ci_max_force ) ctrlp->control.max = cip->ci_max;
  341. else{
  342. res = r5u870_uvc_ctrl_req( this, ctrlp, UVC_GET_MAX, &val );
  343. if ( res < 0 ) return res;
  344. ctrlp->control.max = val;
  345. }
  346. if( cip->ci_def_force ) ctrlp->control.def = cip->ci_def;
  347. else{
  348. res = r5u870_uvc_ctrl_req( this, ctrlp, UVC_GET_DEF, &val );
  349. if( res ) return res;
  350. ctrlp->control.def = val;
  351. }
  352. v4l2_ctrl_new_custom( &this->webcamControlHandler, &ctrlp->control, ctrlp );
  353. if( this->webcamControlHandler.error ){
  354. res = this->webcamControlHandler.error;
  355. v4l2_ctrl_handler_free( &this->webcamControlHandler );
  356. printk( KERN_ERR "[%s] add uvc custom control failed\n", KBUILD_MODNAME );
  357. return res;
  358. }
  359. printk( KERN_NOTICE "[%s] Found UVC control %s [%lld,%lld] def:%lld info:%02x\n",
  360. KBUILD_MODNAME,
  361. ctrlp->control.name,
  362. ctrlp->control.min,
  363. ctrlp->control.max,
  364. ctrlp->control.def,
  365. ctrlp->info
  366. );
  367. return 0;
  368. }
  369. static int r5u870_uvc_add_ctrls( struct driverInstance* this, int unit, const u8* bits, int nbits ){
  370. int i, res;
  371. //TODO: Find a beautiful way to calculate the number of controls
  372. v4l2_ctrl_handler_init( &this->webcamControlHandler, 8 );
  373. for( i = 0; i < nbits; i++ ){
  374. if( !test_bit(i, (const unsigned long *) bits) ) continue;
  375. res = r5u870_uvc_init_ctrl( this, unit, i );
  376. if( res == -ENOENT ) printk( KERN_NOTICE "[%s] unit %d ctrl %d not recognized\n", KBUILD_MODNAME, unit, i );
  377. else if( res ) return res;
  378. }
  379. return 0;
  380. }
  381. /****************************
  382. * UVC PARSE *
  383. ****************************/
  384. static int r5u870_uvc_parse_vs( struct driverInstance* this, int ifnum ){
  385. struct usb_interface* intf;
  386. struct usb_host_interface* aintf;
  387. struct r5u870_pixelformat* curfmt = NULL;
  388. u8* desc;
  389. int dlen, rlen;
  390. int wid, hgt, reqbw, ival;
  391. int res;
  392. intf = usb_ifnum_to_if( this->usbDevice, ifnum );
  393. if( !intf ) return -EINVAL;
  394. aintf = intf->cur_altsetting;
  395. for( desc = aintf->extra, rlen = aintf->extralen; rlen > 2; rlen -= desc[0], desc += desc[0] ){
  396. dlen = desc[0];
  397. if( dlen < 2) return -EINVAL;
  398. if( desc[1] != USB_DT_CS_INTERFACE ) continue;
  399. if( dlen < 3 ) return -EINVAL;
  400. switch( desc[2] ){
  401. case UVC_VS_INPUT_HEADER:
  402. if( dlen < 7 ){
  403. printk( KERN_ERR "[%s] VS_INPUT_HEADER too short at %d bytes\n", KBUILD_MODNAME, dlen );
  404. return -EINVAL;
  405. }
  406. this->urbstreamEndpoint = desc[6] & 0xf;
  407. break;
  408. case UVC_VS_FORMAT_UNCOMPRESSED:
  409. if( dlen < 21 ) {
  410. printk( KERN_ERR "[%s] VS_FORMAT_UNCOMP too short at %d bytes\n", KBUILD_MODNAME, dlen);
  411. break;
  412. }
  413. res = r5u870_uvc_add_fmt( this, &desc[5], desc[3], desc[4], &curfmt );
  414. if( res ){
  415. if( res != -ENOENT ) return res;
  416. curfmt = NULL;
  417. }
  418. break;
  419. case UVC_VS_FRAME_UNCOMPRESSED:
  420. if( dlen < 26 ){
  421. printk( KERN_ERR "[%s] VS_FRAME_UNCOMP too short at %d bytes\n", KBUILD_MODNAME, dlen );
  422. break;
  423. }
  424. if( !curfmt ){
  425. printk( KERN_NOTICE "[%s] VS_FRAME_UNCOMP not following VS_FORMAT_UNCOMP\n", KBUILD_MODNAME );
  426. break;
  427. }
  428. wid = desc[5] | (desc[6]<<8);
  429. hgt = desc[7] | (desc[8]<<8);
  430. reqbw = desc[13] | (desc[14]<<8) | (desc[15]<<16) | (desc[16]<<24);
  431. reqbw = (reqbw + 7) / 8;
  432. ival = le32_to_cpup((__le32 *) &desc[21]);
  433. res = r5u870_uvc_add_resolution( this, curfmt, wid, hgt, reqbw, desc[3], ival );
  434. if( res ) return res;
  435. break;
  436. }
  437. }
  438. return 0;
  439. }
  440. int r5u870_uvc_parse_vc( struct driverInstance* this ){
  441. struct usb_host_interface *aintf = this->usbInterface->cur_altsetting;
  442. u8 *desc;
  443. int dlen, rlen, count;
  444. int i, res;
  445. this->vh_ctrl_ifnum = aintf->desc.bInterfaceNumber;
  446. for( desc = aintf->extra, rlen = aintf->extralen; rlen > 2; rlen -= desc[0], desc += desc[0] ){
  447. dlen = desc[0];
  448. if ( dlen < 2 ) return -EINVAL;
  449. if ( desc[1] != USB_DT_CS_INTERFACE ) continue;
  450. if ( dlen < 3 ) return -EINVAL;
  451. switch( desc[2] ){
  452. case UVC_VC_HEADER:
  453. count = (dlen < 12) ? 0 : desc[11];
  454. if( dlen < (12 + count) ){
  455. printk( KERN_ERR "[%s] VC_HEADER too short at %d bytes\n", KBUILD_MODNAME, dlen );
  456. return -EINVAL;
  457. }
  458. for( i = 0; i < count; i++ ){
  459. res = usbcam_claim_interface( this, desc[12 + i] );
  460. if( res ) printk( KERN_ERR "[%s] interface %d already claimed\n", KBUILD_MODNAME, desc[12 + i] );
  461. this->urbstreamInterfaceNumber = desc[12 + i];
  462. res = r5u870_uvc_parse_vs( this, desc[12 + i] );
  463. if( res ) return res;
  464. }
  465. break;
  466. case UVC_VC_PROCESSING_UNIT:
  467. count = ( dlen < 8 ) ? 0 : desc[7];
  468. if( dlen < (8 + count) ){
  469. printk( KERN_ERR "[%s] VC_PROCESSING_UNIT too short at %d bytes\n", KBUILD_MODNAME, dlen );
  470. return -EINVAL;
  471. }
  472. res = r5u870_uvc_add_ctrls( this, desc[3], &desc[8], desc[7] * 8 );
  473. if( res ) return res;
  474. break;
  475. }
  476. }
  477. return 0;
  478. }