usbvision-core.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  1. /*
  2. * usbvision-core.c - driver for NT100x USB video capture devices
  3. *
  4. *
  5. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  6. * Dwaine Garden <dwainegarden@rogers.com>
  7. *
  8. * This module is part of usbvision driver project.
  9. * Updates to driver completed by Dwaine P. Garden
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/list.h>
  28. #include <linux/timer.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/utsname.h>
  32. #include <linux/highmem.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/videodev.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/spinlock.h>
  39. #include <asm/io.h>
  40. #include <linux/videodev2.h>
  41. #include <linux/video_decoder.h>
  42. #include <linux/i2c.h>
  43. #include <media/saa7115.h>
  44. #include <media/v4l2-common.h>
  45. #include <media/tuner.h>
  46. #include <media/audiochip.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/workqueue.h>
  49. #ifdef CONFIG_KMOD
  50. #include <linux/kmod.h>
  51. #endif
  52. #include "usbvision.h"
  53. static unsigned int core_debug = 0;
  54. module_param(core_debug,int,0644);
  55. MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
  56. static unsigned int force_testpattern = 0;
  57. module_param(force_testpattern,int,0644);
  58. MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]");
  59. static int adjustCompression = 1; // Set the compression to be adaptive
  60. module_param(adjustCompression, int, 0444);
  61. MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)");
  62. static int SwitchSVideoInput = 0; // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
  63. module_param(SwitchSVideoInput, int, 0444);
  64. MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
  65. #define ENABLE_HEXDUMP 0 /* Enable if you need it */
  66. #ifdef USBVISION_DEBUG
  67. #define PDEBUG(level, fmt, args...) \
  68. if (core_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
  69. #else
  70. #define PDEBUG(level, fmt, args...) do {} while(0)
  71. #endif
  72. #define DBG_HEADER 1<<0
  73. #define DBG_IRQ 1<<1
  74. #define DBG_ISOC 1<<2
  75. #define DBG_PARSE 1<<3
  76. #define DBG_SCRATCH 1<<4
  77. static const int max_imgwidth = MAX_FRAME_WIDTH;
  78. static const int max_imgheight = MAX_FRAME_HEIGHT;
  79. static const int min_imgwidth = MIN_FRAME_WIDTH;
  80. static const int min_imgheight = MIN_FRAME_HEIGHT;
  81. /* The value of 'scratch_buf_size' affects quality of the picture
  82. * in many ways. Shorter buffers may cause loss of data when client
  83. * is too slow. Larger buffers are memory-consuming and take longer
  84. * to work with. This setting can be adjusted, but the default value
  85. * should be OK for most desktop users.
  86. */
  87. #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
  88. static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
  89. // Function prototypes
  90. static int usbvision_request_intra (struct usb_usbvision *usbvision);
  91. static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
  92. static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
  93. static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
  94. /*******************************/
  95. /* Memory management functions */
  96. /*******************************/
  97. /*
  98. * Here we want the physical address of the memory.
  99. * This is used when initializing the contents of the area.
  100. */
  101. void *usbvision_rvmalloc(unsigned long size)
  102. {
  103. void *mem;
  104. unsigned long adr;
  105. size = PAGE_ALIGN(size);
  106. mem = vmalloc_32(size);
  107. if (!mem)
  108. return NULL;
  109. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  110. adr = (unsigned long) mem;
  111. while (size > 0) {
  112. SetPageReserved(vmalloc_to_page((void *)adr));
  113. adr += PAGE_SIZE;
  114. size -= PAGE_SIZE;
  115. }
  116. return mem;
  117. }
  118. void usbvision_rvfree(void *mem, unsigned long size)
  119. {
  120. unsigned long adr;
  121. if (!mem)
  122. return;
  123. size = PAGE_ALIGN(size);
  124. adr = (unsigned long) mem;
  125. while ((long) size > 0) {
  126. ClearPageReserved(vmalloc_to_page((void *)adr));
  127. adr += PAGE_SIZE;
  128. size -= PAGE_SIZE;
  129. }
  130. vfree(mem);
  131. }
  132. #if ENABLE_HEXDUMP
  133. static void usbvision_hexdump(const unsigned char *data, int len)
  134. {
  135. char tmp[80];
  136. int i, k;
  137. for (i = k = 0; len > 0; i++, len--) {
  138. if (i > 0 && (i % 16 == 0)) {
  139. printk("%s\n", tmp);
  140. k = 0;
  141. }
  142. k += sprintf(&tmp[k], "%02x ", data[i]);
  143. }
  144. if (k > 0)
  145. printk("%s\n", tmp);
  146. }
  147. #endif
  148. /********************************
  149. * scratch ring buffer handling
  150. ********************************/
  151. int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */
  152. {
  153. int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
  154. if (len < 0) {
  155. len += scratch_buf_size;
  156. }
  157. PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
  158. return len;
  159. }
  160. /* This returns the free space left in the buffer */
  161. int scratch_free(struct usb_usbvision *usbvision)
  162. {
  163. int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
  164. if (free <= 0) {
  165. free += scratch_buf_size;
  166. }
  167. if (free) {
  168. free -= 1; /* at least one byte in the buffer must */
  169. /* left blank, otherwise there is no chance to differ between full and empty */
  170. }
  171. PDEBUG(DBG_SCRATCH, "return %d\n", free);
  172. return free;
  173. }
  174. /* This puts data into the buffer */
  175. int scratch_put(struct usb_usbvision *usbvision, unsigned char *data, int len)
  176. {
  177. int len_part;
  178. if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
  179. memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
  180. usbvision->scratch_write_ptr += len;
  181. }
  182. else {
  183. len_part = scratch_buf_size - usbvision->scratch_write_ptr;
  184. memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
  185. if (len == len_part) {
  186. usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
  187. }
  188. else {
  189. memcpy(usbvision->scratch, data + len_part, len - len_part);
  190. usbvision->scratch_write_ptr = len - len_part;
  191. }
  192. }
  193. PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
  194. return len;
  195. }
  196. /* This marks the write_ptr as position of new frame header */
  197. void scratch_mark_header(struct usb_usbvision *usbvision)
  198. {
  199. PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
  200. usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
  201. usbvision->scratch_write_ptr;
  202. usbvision->scratch_headermarker_write_ptr += 1;
  203. usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
  204. }
  205. /* This gets data from the buffer at the given "ptr" position */
  206. int scratch_get_extra(struct usb_usbvision *usbvision, unsigned char *data, int *ptr, int len)
  207. {
  208. int len_part;
  209. if (*ptr + len < scratch_buf_size) {
  210. memcpy(data, usbvision->scratch + *ptr, len);
  211. *ptr += len;
  212. }
  213. else {
  214. len_part = scratch_buf_size - *ptr;
  215. memcpy(data, usbvision->scratch + *ptr, len_part);
  216. if (len == len_part) {
  217. *ptr = 0; /* just set the y_ptr to zero */
  218. }
  219. else {
  220. memcpy(data + len_part, usbvision->scratch, len - len_part);
  221. *ptr = len - len_part;
  222. }
  223. }
  224. PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
  225. return len;
  226. }
  227. /* This sets the scratch extra read pointer */
  228. void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr, int len)
  229. {
  230. *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size;
  231. PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
  232. }
  233. /*This increments the scratch extra read pointer */
  234. void scratch_inc_extra_ptr(int *ptr, int len)
  235. {
  236. *ptr = (*ptr + len) % scratch_buf_size;
  237. PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
  238. }
  239. /* This gets data from the buffer */
  240. int scratch_get(struct usb_usbvision *usbvision, unsigned char *data, int len)
  241. {
  242. int len_part;
  243. if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
  244. memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
  245. usbvision->scratch_read_ptr += len;
  246. }
  247. else {
  248. len_part = scratch_buf_size - usbvision->scratch_read_ptr;
  249. memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
  250. if (len == len_part) {
  251. usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
  252. }
  253. else {
  254. memcpy(data + len_part, usbvision->scratch, len - len_part);
  255. usbvision->scratch_read_ptr = len - len_part;
  256. }
  257. }
  258. PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
  259. return len;
  260. }
  261. /* This sets read pointer to next header and returns it */
  262. int scratch_get_header(struct usb_usbvision *usbvision,struct usbvision_frame_header *header)
  263. {
  264. int errCode = 0;
  265. PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
  266. while (usbvision->scratch_headermarker_write_ptr -
  267. usbvision->scratch_headermarker_read_ptr != 0) {
  268. usbvision->scratch_read_ptr =
  269. usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
  270. usbvision->scratch_headermarker_read_ptr += 1;
  271. usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
  272. scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
  273. if ((header->magic_1 == USBVISION_MAGIC_1)
  274. && (header->magic_2 == USBVISION_MAGIC_2)
  275. && (header->headerLength == USBVISION_HEADER_LENGTH)) {
  276. errCode = USBVISION_HEADER_LENGTH;
  277. header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8);
  278. header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8);
  279. break;
  280. }
  281. }
  282. return errCode;
  283. }
  284. /*This removes len bytes of old data from the buffer */
  285. void scratch_rm_old(struct usb_usbvision *usbvision, int len)
  286. {
  287. usbvision->scratch_read_ptr += len;
  288. usbvision->scratch_read_ptr %= scratch_buf_size;
  289. PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
  290. }
  291. /*This resets the buffer - kills all data in it too */
  292. void scratch_reset(struct usb_usbvision *usbvision)
  293. {
  294. PDEBUG(DBG_SCRATCH, "\n");
  295. usbvision->scratch_read_ptr = 0;
  296. usbvision->scratch_write_ptr = 0;
  297. usbvision->scratch_headermarker_read_ptr = 0;
  298. usbvision->scratch_headermarker_write_ptr = 0;
  299. usbvision->isocstate = IsocState_NoFrame;
  300. }
  301. int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
  302. {
  303. usbvision->scratch = vmalloc(scratch_buf_size);
  304. scratch_reset(usbvision);
  305. if(usbvision->scratch == NULL) {
  306. err("%s: unable to allocate %d bytes for scratch",
  307. __FUNCTION__, scratch_buf_size);
  308. return -ENOMEM;
  309. }
  310. return 0;
  311. }
  312. void usbvision_scratch_free(struct usb_usbvision *usbvision)
  313. {
  314. if (usbvision->scratch != NULL) {
  315. vfree(usbvision->scratch);
  316. usbvision->scratch = NULL;
  317. }
  318. }
  319. /*
  320. * usbvision_testpattern()
  321. *
  322. * Procedure forms a test pattern (yellow grid on blue background).
  323. *
  324. * Parameters:
  325. * fullframe: if TRUE then entire frame is filled, otherwise the procedure
  326. * continues from the current scanline.
  327. * pmode 0: fill the frame with solid blue color (like on VCR or TV)
  328. * 1: Draw a colored grid
  329. *
  330. */
  331. void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe,
  332. int pmode)
  333. {
  334. static const char proc[] = "usbvision_testpattern";
  335. struct usbvision_frame *frame;
  336. unsigned char *f;
  337. int num_cell = 0;
  338. int scan_length = 0;
  339. static int num_pass = 0;
  340. if (usbvision == NULL) {
  341. printk(KERN_ERR "%s: usbvision == NULL\n", proc);
  342. return;
  343. }
  344. if (usbvision->curFrame == NULL) {
  345. printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc);
  346. return;
  347. }
  348. /* Grab the current frame */
  349. frame = usbvision->curFrame;
  350. /* Optionally start at the beginning */
  351. if (fullframe) {
  352. frame->curline = 0;
  353. frame->scanlength = 0;
  354. }
  355. /* Form every scan line */
  356. for (; frame->curline < frame->frmheight; frame->curline++) {
  357. int i;
  358. f = frame->data + (usbvision->curwidth * 3 * frame->curline);
  359. for (i = 0; i < usbvision->curwidth; i++) {
  360. unsigned char cb = 0x80;
  361. unsigned char cg = 0;
  362. unsigned char cr = 0;
  363. if (pmode == 1) {
  364. if (frame->curline % 32 == 0)
  365. cb = 0, cg = cr = 0xFF;
  366. else if (i % 32 == 0) {
  367. if (frame->curline % 32 == 1)
  368. num_cell++;
  369. cb = 0, cg = cr = 0xFF;
  370. } else {
  371. cb =
  372. ((num_cell * 7) +
  373. num_pass) & 0xFF;
  374. cg =
  375. ((num_cell * 5) +
  376. num_pass * 2) & 0xFF;
  377. cr =
  378. ((num_cell * 3) +
  379. num_pass * 3) & 0xFF;
  380. }
  381. } else {
  382. /* Just the blue screen */
  383. }
  384. *f++ = cb;
  385. *f++ = cg;
  386. *f++ = cr;
  387. scan_length += 3;
  388. }
  389. }
  390. frame->grabstate = FrameState_Done;
  391. frame->scanlength += scan_length;
  392. ++num_pass;
  393. }
  394. /*
  395. * usbvision_decompress_alloc()
  396. *
  397. * allocates intermediate buffer for decompression
  398. */
  399. int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
  400. {
  401. int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
  402. usbvision->IntraFrameBuffer = vmalloc(IFB_size);
  403. if (usbvision->IntraFrameBuffer == NULL) {
  404. err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
  405. return -ENOMEM;
  406. }
  407. return 0;
  408. }
  409. /*
  410. * usbvision_decompress_free()
  411. *
  412. * frees intermediate buffer for decompression
  413. */
  414. void usbvision_decompress_free(struct usb_usbvision *usbvision)
  415. {
  416. if (usbvision->IntraFrameBuffer != NULL) {
  417. vfree(usbvision->IntraFrameBuffer);
  418. usbvision->IntraFrameBuffer = NULL;
  419. }
  420. }
  421. /************************************************************
  422. * Here comes the data parsing stuff that is run as interrupt
  423. ************************************************************/
  424. /*
  425. * usbvision_find_header()
  426. *
  427. * Locate one of supported header markers in the scratch buffer.
  428. */
  429. static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
  430. {
  431. struct usbvision_frame *frame;
  432. int foundHeader = 0;
  433. frame = usbvision->curFrame;
  434. while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
  435. // found header in scratch
  436. PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
  437. frame->isocHeader.magic_2,
  438. frame->isocHeader.magic_1,
  439. frame->isocHeader.headerLength,
  440. frame->isocHeader.frameNum,
  441. frame->isocHeader.framePhase,
  442. frame->isocHeader.frameLatency,
  443. frame->isocHeader.dataFormat,
  444. frame->isocHeader.formatParam,
  445. frame->isocHeader.frameWidth,
  446. frame->isocHeader.frameHeight);
  447. if (usbvision->requestIntra) {
  448. if (frame->isocHeader.formatParam & 0x80) {
  449. foundHeader = 1;
  450. usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time
  451. usbvision_unrequest_intra(usbvision);
  452. break;
  453. }
  454. }
  455. else {
  456. foundHeader = 1;
  457. break;
  458. }
  459. }
  460. if (foundHeader) {
  461. frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
  462. frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
  463. frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
  464. }
  465. else { // no header found
  466. PDEBUG(DBG_HEADER, "skipping scratch data, no header");
  467. scratch_reset(usbvision);
  468. return ParseState_EndParse;
  469. }
  470. // found header
  471. if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) {
  472. //check isocHeader.frameNum for lost frames
  473. if (usbvision->lastIsocFrameNum >= 0) {
  474. if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) {
  475. // unexpected frame drop: need to request new intra frame
  476. PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum);
  477. usbvision_request_intra(usbvision);
  478. return ParseState_NextFrame;
  479. }
  480. }
  481. usbvision->lastIsocFrameNum = frame->isocHeader.frameNum;
  482. }
  483. usbvision->header_count++;
  484. frame->scanstate = ScanState_Lines;
  485. frame->curline = 0;
  486. if (force_testpattern) {
  487. usbvision_testpattern(usbvision, 1, 1);
  488. return ParseState_NextFrame;
  489. }
  490. return ParseState_Continue;
  491. }
  492. static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision,
  493. long *pcopylen)
  494. {
  495. volatile struct usbvision_frame *frame;
  496. unsigned char *f;
  497. int len;
  498. int i;
  499. unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
  500. unsigned char rv, gv, bv; // RGB components
  501. int clipmask_index, bytes_per_pixel;
  502. int stretch_bytes, clipmask_add;
  503. frame = usbvision->curFrame;
  504. f = frame->data + (frame->v4l2_linesize * frame->curline);
  505. /* Make sure there's enough data for the entire line */
  506. len = (frame->isocHeader.frameWidth * 2)+5;
  507. if (scratch_len(usbvision) < len) {
  508. PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
  509. return ParseState_Out;
  510. }
  511. if ((frame->curline + 1) >= frame->frmheight) {
  512. return ParseState_NextFrame;
  513. }
  514. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  515. stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
  516. clipmask_index = frame->curline * MAX_FRAME_WIDTH;
  517. clipmask_add = usbvision->stretch_width;
  518. for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) {
  519. scratch_get(usbvision, &yuyv[0], 4);
  520. if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  521. *f++ = yuyv[0]; // Y
  522. *f++ = yuyv[3]; // U
  523. }
  524. else {
  525. YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
  526. switch (frame->v4l2_format.format) {
  527. case V4L2_PIX_FMT_RGB565:
  528. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  529. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  530. break;
  531. case V4L2_PIX_FMT_RGB24:
  532. *f++ = bv;
  533. *f++ = gv;
  534. *f++ = rv;
  535. break;
  536. case V4L2_PIX_FMT_RGB32:
  537. *f++ = bv;
  538. *f++ = gv;
  539. *f++ = rv;
  540. f++;
  541. break;
  542. case V4L2_PIX_FMT_RGB555:
  543. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  544. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  545. break;
  546. }
  547. }
  548. clipmask_index += clipmask_add;
  549. f += stretch_bytes;
  550. if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  551. *f++ = yuyv[2]; // Y
  552. *f++ = yuyv[1]; // V
  553. }
  554. else {
  555. YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
  556. switch (frame->v4l2_format.format) {
  557. case V4L2_PIX_FMT_RGB565:
  558. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  559. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  560. break;
  561. case V4L2_PIX_FMT_RGB24:
  562. *f++ = bv;
  563. *f++ = gv;
  564. *f++ = rv;
  565. break;
  566. case V4L2_PIX_FMT_RGB32:
  567. *f++ = bv;
  568. *f++ = gv;
  569. *f++ = rv;
  570. f++;
  571. break;
  572. case V4L2_PIX_FMT_RGB555:
  573. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  574. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  575. break;
  576. }
  577. }
  578. clipmask_index += clipmask_add;
  579. f += stretch_bytes;
  580. }
  581. frame->curline += usbvision->stretch_height;
  582. *pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
  583. if (frame->curline >= frame->frmheight) {
  584. return ParseState_NextFrame;
  585. }
  586. else {
  587. return ParseState_Continue;
  588. }
  589. }
  590. /* The decompression routine */
  591. static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
  592. unsigned char *Decompressed, int *StartPos,
  593. int *BlockTypeStartPos, int Len)
  594. {
  595. int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen;
  596. unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator;
  597. Integrator = 0;
  598. Pos = *StartPos;
  599. BlockTypePos = *BlockTypeStartPos;
  600. MaxPos = 396; //Pos + Len;
  601. ExtraPos = Pos;
  602. BlockLen = 0;
  603. BlockByte = 0;
  604. BlockCode = 0;
  605. BlockType = 0;
  606. BlockTypeByte = 0;
  607. BlockTypeLen = 0;
  608. RestPixel = Len;
  609. for (Idx = 0; Idx < Len; Idx++) {
  610. if (BlockLen == 0) {
  611. if (BlockTypeLen==0) {
  612. BlockTypeByte = Compressed[BlockTypePos];
  613. BlockTypePos++;
  614. BlockTypeLen = 4;
  615. }
  616. BlockType = (BlockTypeByte & 0xC0) >> 6;
  617. //statistic:
  618. usbvision->ComprBlockTypes[BlockType]++;
  619. Pos = ExtraPos;
  620. if (BlockType == 0) {
  621. if(RestPixel >= 24) {
  622. Idx += 23;
  623. RestPixel -= 24;
  624. Integrator = Decompressed[Idx];
  625. } else {
  626. Idx += RestPixel - 1;
  627. RestPixel = 0;
  628. }
  629. } else {
  630. BlockCode = Compressed[Pos];
  631. Pos++;
  632. if (RestPixel >= 24) {
  633. BlockLen = 24;
  634. } else {
  635. BlockLen = RestPixel;
  636. }
  637. RestPixel -= BlockLen;
  638. ExtraPos = Pos + (BlockLen / 4);
  639. }
  640. BlockTypeByte <<= 2;
  641. BlockTypeLen -= 1;
  642. }
  643. if (BlockLen > 0) {
  644. if ((BlockLen%4) == 0) {
  645. BlockByte = Compressed[Pos];
  646. Pos++;
  647. }
  648. if (BlockType == 1) { //inter Block
  649. Integrator = Decompressed[Idx];
  650. }
  651. switch (BlockByte & 0xC0) {
  652. case 0x03<<6:
  653. Integrator += Compressed[ExtraPos];
  654. ExtraPos++;
  655. break;
  656. case 0x02<<6:
  657. Integrator += BlockCode;
  658. break;
  659. case 0x00:
  660. Integrator -= BlockCode;
  661. break;
  662. }
  663. Decompressed[Idx] = Integrator;
  664. BlockByte <<= 2;
  665. BlockLen -= 1;
  666. }
  667. }
  668. *StartPos = ExtraPos;
  669. *BlockTypeStartPos = BlockTypePos;
  670. return Idx;
  671. }
  672. /*
  673. * usbvision_parse_compress()
  674. *
  675. * Parse compressed frame from the scratch buffer, put
  676. * decoded RGB value into the current frame buffer and add the written
  677. * number of bytes (RGB) to the *pcopylen.
  678. *
  679. */
  680. static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
  681. long *pcopylen)
  682. {
  683. #define USBVISION_STRIP_MAGIC 0x5A
  684. #define USBVISION_STRIP_LEN_MAX 400
  685. #define USBVISION_STRIP_HEADER_LEN 3
  686. struct usbvision_frame *frame;
  687. unsigned char *f,*u = NULL ,*v = NULL;
  688. unsigned char StripData[USBVISION_STRIP_LEN_MAX];
  689. unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
  690. int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
  691. int clipmask_index, bytes_per_pixel, rc;
  692. int imageSize;
  693. unsigned char rv, gv, bv;
  694. static unsigned char *Y, *U, *V;
  695. frame = usbvision->curFrame;
  696. imageSize = frame->frmwidth * frame->frmheight;
  697. if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
  698. (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) { // this is a planar format
  699. //... v4l2_linesize not used here.
  700. f = frame->data + (frame->width * frame->curline);
  701. } else
  702. f = frame->data + (frame->v4l2_linesize * frame->curline);
  703. if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
  704. // get base of u and b planes add halfoffset
  705. u = frame->data
  706. + imageSize
  707. + (frame->frmwidth >>1) * frame->curline ;
  708. v = u + (imageSize >>1 );
  709. } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
  710. v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
  711. u = v + (imageSize >>2) ;
  712. }
  713. if (frame->curline == 0) {
  714. usbvision_adjust_compression(usbvision);
  715. }
  716. if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) {
  717. return ParseState_Out;
  718. }
  719. //get strip header without changing the scratch_read_ptr
  720. scratch_set_extra_ptr(usbvision, &StripPtr, 0);
  721. scratch_get_extra(usbvision, &StripHeader[0], &StripPtr,
  722. USBVISION_STRIP_HEADER_LEN);
  723. if (StripHeader[0] != USBVISION_STRIP_MAGIC) {
  724. // wrong strip magic
  725. usbvision->stripMagicErrors++;
  726. return ParseState_NextFrame;
  727. }
  728. if (frame->curline != (int)StripHeader[2]) {
  729. //line number missmatch error
  730. usbvision->stripLineNumberErrors++;
  731. }
  732. StripLen = 2 * (unsigned int)StripHeader[1];
  733. if (StripLen > USBVISION_STRIP_LEN_MAX) {
  734. // strip overrun
  735. // I think this never happens
  736. usbvision_request_intra(usbvision);
  737. }
  738. if (scratch_len(usbvision) < StripLen) {
  739. //there is not enough data for the strip
  740. return ParseState_Out;
  741. }
  742. if (usbvision->IntraFrameBuffer) {
  743. Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline;
  744. U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2);
  745. V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
  746. }
  747. else {
  748. return ParseState_NextFrame;
  749. }
  750. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  751. clipmask_index = frame->curline * MAX_FRAME_WIDTH;
  752. scratch_get(usbvision, StripData, StripLen);
  753. IdxEnd = frame->frmwidth;
  754. BlockTypePos = USBVISION_STRIP_HEADER_LEN;
  755. StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2;
  756. BlockPos = StartBlockPos;
  757. usbvision->BlockPos = BlockPos;
  758. if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) {
  759. //return ParseState_Continue;
  760. }
  761. if (StripLen > usbvision->maxStripLen) {
  762. usbvision->maxStripLen = StripLen;
  763. }
  764. if (frame->curline%2) {
  765. if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
  766. //return ParseState_Continue;
  767. }
  768. }
  769. else {
  770. if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
  771. //return ParseState_Continue;
  772. }
  773. }
  774. if (BlockPos > usbvision->comprBlockPos) {
  775. usbvision->comprBlockPos = BlockPos;
  776. }
  777. if (BlockPos > StripLen) {
  778. usbvision->stripLenErrors++;
  779. }
  780. for (Idx = 0; Idx < IdxEnd; Idx++) {
  781. if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  782. *f++ = Y[Idx];
  783. *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
  784. }
  785. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
  786. *f++ = Y[Idx];
  787. if ( Idx & 0x01)
  788. *u++ = U[Idx>>1] ;
  789. else
  790. *v++ = V[Idx>>1];
  791. }
  792. else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
  793. *f++ = Y [Idx];
  794. if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){
  795. /* only need do this for 1 in 4 pixels */
  796. /* intraframe buffer is YUV420 format */
  797. *u++ = U[Idx >>1];
  798. *v++ = V[Idx >>1];
  799. }
  800. }
  801. else {
  802. YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
  803. switch (frame->v4l2_format.format) {
  804. case V4L2_PIX_FMT_GREY:
  805. *f++ = Y[Idx];
  806. break;
  807. case V4L2_PIX_FMT_RGB555:
  808. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  809. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  810. break;
  811. case V4L2_PIX_FMT_RGB565:
  812. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  813. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  814. break;
  815. case V4L2_PIX_FMT_RGB24:
  816. *f++ = bv;
  817. *f++ = gv;
  818. *f++ = rv;
  819. break;
  820. case V4L2_PIX_FMT_RGB32:
  821. *f++ = bv;
  822. *f++ = gv;
  823. *f++ = rv;
  824. f++;
  825. break;
  826. }
  827. }
  828. clipmask_index++;
  829. }
  830. /* Deal with non-integer no. of bytes for YUV420P */
  831. if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
  832. *pcopylen += frame->v4l2_linesize;
  833. else
  834. *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
  835. frame->curline += 1;
  836. if (frame->curline >= frame->frmheight) {
  837. return ParseState_NextFrame;
  838. }
  839. else {
  840. return ParseState_Continue;
  841. }
  842. }
  843. /*
  844. * usbvision_parse_lines_420()
  845. *
  846. * Parse two lines from the scratch buffer, put
  847. * decoded RGB value into the current frame buffer and add the written
  848. * number of bytes (RGB) to the *pcopylen.
  849. *
  850. */
  851. static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision,
  852. long *pcopylen)
  853. {
  854. struct usbvision_frame *frame;
  855. unsigned char *f_even = NULL, *f_odd = NULL;
  856. unsigned int pixel_per_line, block;
  857. int pixel, block_split;
  858. int y_ptr, u_ptr, v_ptr, y_odd_offset;
  859. const int y_block_size = 128;
  860. const int uv_block_size = 64;
  861. const int sub_block_size = 32;
  862. const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
  863. const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4;
  864. unsigned char y[2], u, v; /* YUV components */
  865. int y_, u_, v_, vb, uvg, ur;
  866. int r_, g_, b_; /* RGB components */
  867. unsigned char g;
  868. int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
  869. int clipmask_add, stretch_bytes;
  870. frame = usbvision->curFrame;
  871. f_even = frame->data + (frame->v4l2_linesize * frame->curline);
  872. f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height;
  873. /* Make sure there's enough data for the entire line */
  874. /* In this mode usbvision transfer 3 bytes for every 2 pixels */
  875. /* I need two lines to decode the color */
  876. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  877. stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
  878. clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
  879. clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH;
  880. clipmask_add = usbvision->stretch_width;
  881. pixel_per_line = frame->isocHeader.frameWidth;
  882. if (scratch_len(usbvision) < (int)pixel_per_line * 3) {
  883. //printk(KERN_DEBUG "out of data, need %d\n", len);
  884. return ParseState_Out;
  885. }
  886. if ((frame->curline + 1) >= frame->frmheight) {
  887. return ParseState_NextFrame;
  888. }
  889. block_split = (pixel_per_line%y_block_size) ? 1 : 0; //are some blocks splitted into different lines?
  890. y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
  891. + block_split * uv_block_size;
  892. scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
  893. scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
  894. scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
  895. + (4 - block_split) * sub_block_size);
  896. for (block = 0; block < (pixel_per_line / sub_block_size);
  897. block++) {
  898. for (pixel = 0; pixel < sub_block_size; pixel +=2) {
  899. scratch_get(usbvision, &y[0], 2);
  900. scratch_get_extra(usbvision, &u, &u_ptr, 1);
  901. scratch_get_extra(usbvision, &v, &v_ptr, 1);
  902. //I don't use the YUV_TO_RGB macro for better performance
  903. v_ = v - 128;
  904. u_ = u - 128;
  905. vb = 132252 * v_;
  906. uvg= -53281 * u_ - 25625 * v_;
  907. ur = 104595 * u_;
  908. if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  909. *f_even++ = y[0];
  910. *f_even++ = v;
  911. }
  912. else {
  913. y_ = 76284 * (y[0] - 16);
  914. b_ = (y_ + vb) >> 16;
  915. g_ = (y_ + uvg)>> 16;
  916. r_ = (y_ + ur) >> 16;
  917. switch (frame->v4l2_format.format) {
  918. case V4L2_PIX_FMT_RGB565:
  919. g = LIMIT_RGB(g_);
  920. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  921. *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  922. break;
  923. case V4L2_PIX_FMT_RGB24:
  924. *f_even++ = LIMIT_RGB(b_);
  925. *f_even++ = LIMIT_RGB(g_);
  926. *f_even++ = LIMIT_RGB(r_);
  927. break;
  928. case V4L2_PIX_FMT_RGB32:
  929. *f_even++ = LIMIT_RGB(b_);
  930. *f_even++ = LIMIT_RGB(g_);
  931. *f_even++ = LIMIT_RGB(r_);
  932. f_even++;
  933. break;
  934. case V4L2_PIX_FMT_RGB555:
  935. g = LIMIT_RGB(g_);
  936. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  937. *f_even++ = (0x03 & ( g >> 6)) |
  938. (0x7C & (LIMIT_RGB(r_) >> 1));
  939. break;
  940. }
  941. }
  942. clipmask_even_index += clipmask_add;
  943. f_even += stretch_bytes;
  944. if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  945. *f_even++ = y[1];
  946. *f_even++ = u;
  947. }
  948. else {
  949. y_ = 76284 * (y[1] - 16);
  950. b_ = (y_ + vb) >> 16;
  951. g_ = (y_ + uvg)>> 16;
  952. r_ = (y_ + ur) >> 16;
  953. switch (frame->v4l2_format.format) {
  954. case V4L2_PIX_FMT_RGB565:
  955. g = LIMIT_RGB(g_);
  956. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  957. *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  958. break;
  959. case V4L2_PIX_FMT_RGB24:
  960. *f_even++ = LIMIT_RGB(b_);
  961. *f_even++ = LIMIT_RGB(g_);
  962. *f_even++ = LIMIT_RGB(r_);
  963. break;
  964. case V4L2_PIX_FMT_RGB32:
  965. *f_even++ = LIMIT_RGB(b_);
  966. *f_even++ = LIMIT_RGB(g_);
  967. *f_even++ = LIMIT_RGB(r_);
  968. f_even++;
  969. break;
  970. case V4L2_PIX_FMT_RGB555:
  971. g = LIMIT_RGB(g_);
  972. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  973. *f_even++ = (0x03 & ( g >> 6)) |
  974. (0x7C & (LIMIT_RGB(r_) >> 1));
  975. break;
  976. }
  977. }
  978. clipmask_even_index += clipmask_add;
  979. f_even += stretch_bytes;
  980. scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
  981. if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  982. *f_odd++ = y[0];
  983. *f_odd++ = v;
  984. }
  985. else {
  986. y_ = 76284 * (y[0] - 16);
  987. b_ = (y_ + vb) >> 16;
  988. g_ = (y_ + uvg)>> 16;
  989. r_ = (y_ + ur) >> 16;
  990. switch (frame->v4l2_format.format) {
  991. case V4L2_PIX_FMT_RGB565:
  992. g = LIMIT_RGB(g_);
  993. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  994. *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  995. break;
  996. case V4L2_PIX_FMT_RGB24:
  997. *f_odd++ = LIMIT_RGB(b_);
  998. *f_odd++ = LIMIT_RGB(g_);
  999. *f_odd++ = LIMIT_RGB(r_);
  1000. break;
  1001. case V4L2_PIX_FMT_RGB32:
  1002. *f_odd++ = LIMIT_RGB(b_);
  1003. *f_odd++ = LIMIT_RGB(g_);
  1004. *f_odd++ = LIMIT_RGB(r_);
  1005. f_odd++;
  1006. break;
  1007. case V4L2_PIX_FMT_RGB555:
  1008. g = LIMIT_RGB(g_);
  1009. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1010. *f_odd++ = (0x03 & ( g >> 6)) |
  1011. (0x7C & (LIMIT_RGB(r_) >> 1));
  1012. break;
  1013. }
  1014. }
  1015. clipmask_odd_index += clipmask_add;
  1016. f_odd += stretch_bytes;
  1017. if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1018. *f_odd++ = y[1];
  1019. *f_odd++ = u;
  1020. }
  1021. else {
  1022. y_ = 76284 * (y[1] - 16);
  1023. b_ = (y_ + vb) >> 16;
  1024. g_ = (y_ + uvg)>> 16;
  1025. r_ = (y_ + ur) >> 16;
  1026. switch (frame->v4l2_format.format) {
  1027. case V4L2_PIX_FMT_RGB565:
  1028. g = LIMIT_RGB(g_);
  1029. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  1030. *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  1031. break;
  1032. case V4L2_PIX_FMT_RGB24:
  1033. *f_odd++ = LIMIT_RGB(b_);
  1034. *f_odd++ = LIMIT_RGB(g_);
  1035. *f_odd++ = LIMIT_RGB(r_);
  1036. break;
  1037. case V4L2_PIX_FMT_RGB32:
  1038. *f_odd++ = LIMIT_RGB(b_);
  1039. *f_odd++ = LIMIT_RGB(g_);
  1040. *f_odd++ = LIMIT_RGB(r_);
  1041. f_odd++;
  1042. break;
  1043. case V4L2_PIX_FMT_RGB555:
  1044. g = LIMIT_RGB(g_);
  1045. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1046. *f_odd++ = (0x03 & ( g >> 6)) |
  1047. (0x7C & (LIMIT_RGB(r_) >> 1));
  1048. break;
  1049. }
  1050. }
  1051. clipmask_odd_index += clipmask_add;
  1052. f_odd += stretch_bytes;
  1053. }
  1054. scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size);
  1055. scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
  1056. * sub_block_size);
  1057. scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
  1058. * sub_block_size);
  1059. scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
  1060. * sub_block_size);
  1061. }
  1062. scratch_rm_old(usbvision, pixel_per_line * 3 / 2
  1063. + block_split * sub_block_size);
  1064. frame->curline += 2 * usbvision->stretch_height;
  1065. *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height;
  1066. if (frame->curline >= frame->frmheight)
  1067. return ParseState_NextFrame;
  1068. else
  1069. return ParseState_Continue;
  1070. }
  1071. /*
  1072. * usbvision_parse_data()
  1073. *
  1074. * Generic routine to parse the scratch buffer. It employs either
  1075. * usbvision_find_header() or usbvision_parse_lines() to do most
  1076. * of work.
  1077. *
  1078. */
  1079. static void usbvision_parse_data(struct usb_usbvision *usbvision)
  1080. {
  1081. struct usbvision_frame *frame;
  1082. enum ParseState newstate;
  1083. long copylen = 0;
  1084. unsigned long lock_flags;
  1085. frame = usbvision->curFrame;
  1086. PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
  1087. while (1) {
  1088. newstate = ParseState_Out;
  1089. if (scratch_len(usbvision)) {
  1090. if (frame->scanstate == ScanState_Scanning) {
  1091. newstate = usbvision_find_header(usbvision);
  1092. }
  1093. else if (frame->scanstate == ScanState_Lines) {
  1094. if (usbvision->isocMode == ISOC_MODE_YUV420) {
  1095. newstate = usbvision_parse_lines_420(usbvision, &copylen);
  1096. }
  1097. else if (usbvision->isocMode == ISOC_MODE_YUV422) {
  1098. newstate = usbvision_parse_lines_422(usbvision, &copylen);
  1099. }
  1100. else if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  1101. newstate = usbvision_parse_compress(usbvision, &copylen);
  1102. }
  1103. }
  1104. }
  1105. if (newstate == ParseState_Continue) {
  1106. continue;
  1107. }
  1108. else if ((newstate == ParseState_NextFrame) || (newstate == ParseState_Out)) {
  1109. break;
  1110. }
  1111. else {
  1112. return; /* ParseState_EndParse */
  1113. }
  1114. }
  1115. if (newstate == ParseState_NextFrame) {
  1116. frame->grabstate = FrameState_Done;
  1117. do_gettimeofday(&(frame->timestamp));
  1118. frame->sequence = usbvision->frame_num;
  1119. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  1120. list_move_tail(&(frame->frame), &usbvision->outqueue);
  1121. usbvision->curFrame = NULL;
  1122. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  1123. usbvision->frame_num++;
  1124. /* This will cause the process to request another frame. */
  1125. if (waitqueue_active(&usbvision->wait_frame)) {
  1126. PDEBUG(DBG_PARSE, "Wake up !");
  1127. wake_up_interruptible(&usbvision->wait_frame);
  1128. }
  1129. }
  1130. else
  1131. frame->grabstate = FrameState_Grabbing;
  1132. /* Update the frame's uncompressed length. */
  1133. frame->scanlength += copylen;
  1134. }
  1135. /*
  1136. * Make all of the blocks of data contiguous
  1137. */
  1138. static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
  1139. struct urb *urb)
  1140. {
  1141. unsigned char *packet_data;
  1142. int i, totlen = 0;
  1143. for (i = 0; i < urb->number_of_packets; i++) {
  1144. int packet_len = urb->iso_frame_desc[i].actual_length;
  1145. int packet_stat = urb->iso_frame_desc[i].status;
  1146. packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  1147. /* Detect and ignore errored packets */
  1148. if (packet_stat) { // packet_stat != 0 ?????????????
  1149. PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
  1150. usbvision->isocErrCount++;
  1151. continue;
  1152. }
  1153. /* Detect and ignore empty packets */
  1154. if (packet_len < 0) {
  1155. PDEBUG(DBG_ISOC, "error packet [%d]", i);
  1156. usbvision->isocSkipCount++;
  1157. continue;
  1158. }
  1159. else if (packet_len == 0) { /* Frame end ????? */
  1160. PDEBUG(DBG_ISOC, "null packet [%d]", i);
  1161. usbvision->isocstate=IsocState_NoFrame;
  1162. usbvision->isocSkipCount++;
  1163. continue;
  1164. }
  1165. else if (packet_len > usbvision->isocPacketSize) {
  1166. PDEBUG(DBG_ISOC, "packet[%d] > isocPacketSize", i);
  1167. usbvision->isocSkipCount++;
  1168. continue;
  1169. }
  1170. PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len);
  1171. if (usbvision->isocstate==IsocState_NoFrame) { //new frame begins
  1172. usbvision->isocstate=IsocState_InFrame;
  1173. scratch_mark_header(usbvision);
  1174. usbvision_measure_bandwidth(usbvision);
  1175. PDEBUG(DBG_ISOC, "packet with header");
  1176. }
  1177. /*
  1178. * If usbvision continues to feed us with data but there is no
  1179. * consumption (if, for example, V4L client fell asleep) we
  1180. * may overflow the buffer. We have to move old data over to
  1181. * free room for new data. This is bad for old data. If we
  1182. * just drop new data then it's bad for new data... choose
  1183. * your favorite evil here.
  1184. */
  1185. if (scratch_free(usbvision) < packet_len) {
  1186. usbvision->scratch_ovf_count++;
  1187. PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d",
  1188. scratch_len(usbvision), packet_len);
  1189. scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
  1190. }
  1191. /* Now we know that there is enough room in scratch buffer */
  1192. scratch_put(usbvision, packet_data, packet_len);
  1193. totlen += packet_len;
  1194. usbvision->isocDataCount += packet_len;
  1195. usbvision->isocPacketCount++;
  1196. }
  1197. #if ENABLE_HEXDUMP
  1198. if (totlen > 0) {
  1199. static int foo = 0;
  1200. if (foo < 1) {
  1201. printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen);
  1202. usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen);
  1203. ++foo;
  1204. }
  1205. }
  1206. #endif
  1207. return totlen;
  1208. }
  1209. static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs)
  1210. {
  1211. int errCode = 0;
  1212. int len;
  1213. struct usb_usbvision *usbvision = urb->context;
  1214. int i;
  1215. unsigned long startTime = jiffies;
  1216. struct usbvision_frame **f;
  1217. /* We don't want to do anything if we are about to be removed! */
  1218. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1219. return;
  1220. f = &usbvision->curFrame;
  1221. /* Manage streaming interruption */
  1222. if (usbvision->streaming == Stream_Interrupt) {
  1223. usbvision->streaming = Stream_Idle;
  1224. if ((*f)) {
  1225. (*f)->grabstate = FrameState_Ready;
  1226. (*f)->scanstate = ScanState_Scanning;
  1227. }
  1228. PDEBUG(DBG_IRQ, "stream interrupted");
  1229. wake_up_interruptible(&usbvision->wait_stream);
  1230. }
  1231. /* Copy the data received into our scratch buffer */
  1232. len = usbvision_compress_isochronous(usbvision, urb);
  1233. usbvision->isocUrbCount++;
  1234. usbvision->urb_length = len;
  1235. if (usbvision->streaming == Stream_On) {
  1236. /* If we collected enough data let's parse! */
  1237. if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH) { /* 12 == header_length */
  1238. /*If we don't have a frame we're current working on, complain */
  1239. if(!list_empty(&(usbvision->inqueue))) {
  1240. if (!(*f)) {
  1241. (*f) = list_entry(usbvision->inqueue.next,struct usbvision_frame, frame);
  1242. }
  1243. usbvision_parse_data(usbvision);
  1244. }
  1245. else {
  1246. PDEBUG(DBG_IRQ, "received data, but no one needs it");
  1247. scratch_reset(usbvision);
  1248. }
  1249. }
  1250. }
  1251. else {
  1252. PDEBUG(DBG_IRQ, "received data, but no one needs it");
  1253. scratch_reset(usbvision);
  1254. }
  1255. usbvision->timeInIrq += jiffies - startTime;
  1256. for (i = 0; i < USBVISION_URB_FRAMES; i++) {
  1257. urb->iso_frame_desc[i].status = 0;
  1258. urb->iso_frame_desc[i].actual_length = 0;
  1259. }
  1260. urb->status = 0;
  1261. urb->dev = usbvision->dev;
  1262. errCode = usb_submit_urb (urb, GFP_ATOMIC);
  1263. /* Disable this warning. By design of the driver. */
  1264. // if(errCode) {
  1265. // err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode);
  1266. // }
  1267. return;
  1268. }
  1269. /*************************************/
  1270. /* Low level usbvision access functions */
  1271. /*************************************/
  1272. /*
  1273. * usbvision_read_reg()
  1274. *
  1275. * return < 0 -> Error
  1276. * >= 0 -> Data
  1277. */
  1278. int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
  1279. {
  1280. int errCode = 0;
  1281. unsigned char buffer[1];
  1282. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1283. return -1;
  1284. errCode = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1),
  1285. USBVISION_OP_CODE,
  1286. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
  1287. 0, (__u16) reg, buffer, 1, HZ);
  1288. if (errCode < 0) {
  1289. err("%s: failed: error %d", __FUNCTION__, errCode);
  1290. return errCode;
  1291. }
  1292. return buffer[0];
  1293. }
  1294. /*
  1295. * usbvision_write_reg()
  1296. *
  1297. * return 1 -> Reg written
  1298. * 0 -> usbvision is not yet ready
  1299. * -1 -> Something went wrong
  1300. */
  1301. int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
  1302. unsigned char value)
  1303. {
  1304. int errCode = 0;
  1305. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1306. return 0;
  1307. errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1308. USBVISION_OP_CODE,
  1309. USB_DIR_OUT | USB_TYPE_VENDOR |
  1310. USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ);
  1311. if (errCode < 0) {
  1312. err("%s: failed: error %d", __FUNCTION__, errCode);
  1313. }
  1314. return errCode;
  1315. }
  1316. static void usbvision_ctrlUrb_complete(struct urb *urb, struct pt_regs *regs)
  1317. {
  1318. struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context;
  1319. PDEBUG(DBG_IRQ, "");
  1320. usbvision->ctrlUrbBusy = 0;
  1321. if (waitqueue_active(&usbvision->ctrlUrb_wq)) {
  1322. wake_up_interruptible(&usbvision->ctrlUrb_wq);
  1323. }
  1324. }
  1325. static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address,
  1326. unsigned char *data, int len)
  1327. {
  1328. int errCode = 0;
  1329. PDEBUG(DBG_IRQ, "");
  1330. if (len > 8) {
  1331. return -EFAULT;
  1332. }
  1333. // down(&usbvision->ctrlUrbLock);
  1334. if (usbvision->ctrlUrbBusy) {
  1335. // up(&usbvision->ctrlUrbLock);
  1336. return -EBUSY;
  1337. }
  1338. usbvision->ctrlUrbBusy = 1;
  1339. // up(&usbvision->ctrlUrbLock);
  1340. usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
  1341. usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE;
  1342. usbvision->ctrlUrbSetup.wValue = 0;
  1343. usbvision->ctrlUrbSetup.wIndex = cpu_to_le16(address);
  1344. usbvision->ctrlUrbSetup.wLength = cpu_to_le16(len);
  1345. usb_fill_control_urb (usbvision->ctrlUrb, usbvision->dev,
  1346. usb_sndctrlpipe(usbvision->dev, 1),
  1347. (unsigned char *)&usbvision->ctrlUrbSetup,
  1348. (void *)usbvision->ctrlUrbBuffer, len,
  1349. usbvision_ctrlUrb_complete,
  1350. (void *)usbvision);
  1351. memcpy(usbvision->ctrlUrbBuffer, data, len);
  1352. errCode = usb_submit_urb(usbvision->ctrlUrb, GFP_ATOMIC);
  1353. if (errCode < 0) {
  1354. // error in usb_submit_urb()
  1355. usbvision->ctrlUrbBusy = 0;
  1356. }
  1357. PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, errCode);
  1358. return errCode;
  1359. }
  1360. static int usbvision_init_compression(struct usb_usbvision *usbvision)
  1361. {
  1362. int errCode = 0;
  1363. usbvision->lastIsocFrameNum = -1;
  1364. usbvision->isocDataCount = 0;
  1365. usbvision->isocPacketCount = 0;
  1366. usbvision->isocSkipCount = 0;
  1367. usbvision->comprLevel = 50;
  1368. usbvision->lastComprLevel = -1;
  1369. usbvision->isocUrbCount = 0;
  1370. usbvision->requestIntra = 1;
  1371. usbvision->isocMeasureBandwidthCount = 0;
  1372. return errCode;
  1373. }
  1374. /* this function measures the used bandwidth since last call
  1375. * return: 0 : no error
  1376. * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize
  1377. */
  1378. static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision)
  1379. {
  1380. int errCode = 0;
  1381. if (usbvision->isocMeasureBandwidthCount < 2) { // this gives an average bandwidth of 3 frames
  1382. usbvision->isocMeasureBandwidthCount++;
  1383. return errCode;
  1384. }
  1385. if ((usbvision->isocPacketSize > 0) && (usbvision->isocPacketCount > 0)) {
  1386. usbvision->usedBandwidth = usbvision->isocDataCount /
  1387. (usbvision->isocPacketCount + usbvision->isocSkipCount) *
  1388. 100 / usbvision->isocPacketSize;
  1389. }
  1390. usbvision->isocMeasureBandwidthCount = 0;
  1391. usbvision->isocDataCount = 0;
  1392. usbvision->isocPacketCount = 0;
  1393. usbvision->isocSkipCount = 0;
  1394. return errCode;
  1395. }
  1396. static int usbvision_adjust_compression (struct usb_usbvision *usbvision)
  1397. {
  1398. int errCode = 0;
  1399. unsigned char buffer[6];
  1400. PDEBUG(DBG_IRQ, "");
  1401. if ((adjustCompression) && (usbvision->usedBandwidth > 0)) {
  1402. usbvision->comprLevel += (usbvision->usedBandwidth - 90) / 2;
  1403. RESTRICT_TO_RANGE(usbvision->comprLevel, 0, 100);
  1404. if (usbvision->comprLevel != usbvision->lastComprLevel) {
  1405. int distorsion;
  1406. if (usbvision->bridgeType == BRIDGE_NT1004 || usbvision->bridgeType == BRIDGE_NT1005) {
  1407. buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM Threshold 1
  1408. buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM Threshold 2
  1409. distorsion = 7 + 248 * usbvision->comprLevel / 100;
  1410. buffer[2] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (inter)
  1411. buffer[3] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (intra)
  1412. distorsion = 1 + 42 * usbvision->comprLevel / 100;
  1413. buffer[4] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (inter)
  1414. buffer[5] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (intra)
  1415. }
  1416. else { //BRIDGE_NT1003
  1417. buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM threshold 1
  1418. buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM threshold 2
  1419. distorsion = 2 + 253 * usbvision->comprLevel / 100;
  1420. buffer[2] = (unsigned char)(distorsion & 0xFF); // distorsion threshold bit0-7
  1421. buffer[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11
  1422. distorsion = 0 + 43 * usbvision->comprLevel / 100;
  1423. buffer[4] = (unsigned char)(distorsion & 0xFF); // maximum distorsion bit0-7
  1424. buffer[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8
  1425. }
  1426. errCode = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6);
  1427. if (errCode == 0){
  1428. PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0],
  1429. buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
  1430. usbvision->lastComprLevel = usbvision->comprLevel;
  1431. }
  1432. }
  1433. }
  1434. return errCode;
  1435. }
  1436. static int usbvision_request_intra (struct usb_usbvision *usbvision)
  1437. {
  1438. int errCode = 0;
  1439. unsigned char buffer[1];
  1440. PDEBUG(DBG_IRQ, "");
  1441. usbvision->requestIntra = 1;
  1442. buffer[0] = 1;
  1443. usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
  1444. return errCode;
  1445. }
  1446. static int usbvision_unrequest_intra (struct usb_usbvision *usbvision)
  1447. {
  1448. int errCode = 0;
  1449. unsigned char buffer[1];
  1450. PDEBUG(DBG_IRQ, "");
  1451. usbvision->requestIntra = 0;
  1452. buffer[0] = 0;
  1453. usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
  1454. return errCode;
  1455. }
  1456. /*******************************
  1457. * usbvision utility functions
  1458. *******************************/
  1459. int usbvision_power_off(struct usb_usbvision *usbvision)
  1460. {
  1461. int errCode = 0;
  1462. PDEBUG(DBG_FUNC, "");
  1463. errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
  1464. if (errCode == 1) {
  1465. usbvision->power = 0;
  1466. }
  1467. PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode!=1)?"ERROR":"power is off", errCode);
  1468. return errCode;
  1469. }
  1470. /*
  1471. * usbvision_set_video_format()
  1472. *
  1473. */
  1474. static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format)
  1475. {
  1476. static const char proc[] = "usbvision_set_video_format";
  1477. int rc;
  1478. unsigned char value[2];
  1479. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1480. return 0;
  1481. PDEBUG(DBG_FUNC, "isocMode %#02x", format);
  1482. if ((format != ISOC_MODE_YUV422)
  1483. && (format != ISOC_MODE_YUV420)
  1484. && (format != ISOC_MODE_COMPRESS)) {
  1485. printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420",
  1486. format);
  1487. format = ISOC_MODE_YUV420;
  1488. }
  1489. value[0] = 0x0A; //TODO: See the effect of the filter
  1490. value[1] = format;
  1491. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1492. USBVISION_OP_CODE,
  1493. USB_DIR_OUT | USB_TYPE_VENDOR |
  1494. USB_RECIP_ENDPOINT, 0,
  1495. (__u16) USBVISION_FILT_CONT, value, 2, HZ);
  1496. if (rc < 0) {
  1497. printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - "
  1498. "reconnect or reload driver.\n", proc, rc);
  1499. }
  1500. usbvision->isocMode = format;
  1501. return rc;
  1502. }
  1503. /*
  1504. * usbvision_set_output()
  1505. *
  1506. */
  1507. int usbvision_set_output(struct usb_usbvision *usbvision, int width,
  1508. int height)
  1509. {
  1510. int errCode = 0;
  1511. int UsbWidth, UsbHeight;
  1512. unsigned int frameRate=0, frameDrop=0;
  1513. unsigned char value[4];
  1514. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  1515. return 0;
  1516. }
  1517. if (width > MAX_USB_WIDTH) {
  1518. UsbWidth = width / 2;
  1519. usbvision->stretch_width = 2;
  1520. }
  1521. else {
  1522. UsbWidth = width;
  1523. usbvision->stretch_width = 1;
  1524. }
  1525. if (height > MAX_USB_HEIGHT) {
  1526. UsbHeight = height / 2;
  1527. usbvision->stretch_height = 2;
  1528. }
  1529. else {
  1530. UsbHeight = height;
  1531. usbvision->stretch_height = 1;
  1532. }
  1533. RESTRICT_TO_RANGE(UsbWidth, MIN_FRAME_WIDTH, MAX_USB_WIDTH);
  1534. UsbWidth &= ~(MIN_FRAME_WIDTH-1);
  1535. RESTRICT_TO_RANGE(UsbHeight, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT);
  1536. UsbHeight &= ~(1);
  1537. PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d",
  1538. UsbWidth, UsbHeight, width, height,
  1539. usbvision->stretch_width, usbvision->stretch_height);
  1540. /* I'll not rewrite the same values */
  1541. if ((UsbWidth != usbvision->curwidth) || (UsbHeight != usbvision->curheight)) {
  1542. value[0] = UsbWidth & 0xff; //LSB
  1543. value[1] = (UsbWidth >> 8) & 0x03; //MSB
  1544. value[2] = UsbHeight & 0xff; //LSB
  1545. value[3] = (UsbHeight >> 8) & 0x03; //MSB
  1546. errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1547. USBVISION_OP_CODE,
  1548. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
  1549. 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ);
  1550. if (errCode < 0) {
  1551. err("%s failed: error %d", __FUNCTION__, errCode);
  1552. return errCode;
  1553. }
  1554. usbvision->curwidth = usbvision->stretch_width * UsbWidth;
  1555. usbvision->curheight = usbvision->stretch_height * UsbHeight;
  1556. }
  1557. if (usbvision->isocMode == ISOC_MODE_YUV422) {
  1558. frameRate = (usbvision->isocPacketSize * 1000) / (UsbWidth * UsbHeight * 2);
  1559. }
  1560. else if (usbvision->isocMode == ISOC_MODE_YUV420) {
  1561. frameRate = (usbvision->isocPacketSize * 1000) / ((UsbWidth * UsbHeight * 12) / 8);
  1562. }
  1563. else {
  1564. frameRate = FRAMERATE_MAX;
  1565. }
  1566. if (usbvision->tvnorm->id & V4L2_STD_625_50) {
  1567. frameDrop = frameRate * 32 / 25 - 1;
  1568. }
  1569. else if (usbvision->tvnorm->id & V4L2_STD_525_60) {
  1570. frameDrop = frameRate * 32 / 30 - 1;
  1571. }
  1572. RESTRICT_TO_RANGE(frameDrop, FRAMERATE_MIN, FRAMERATE_MAX);
  1573. PDEBUG(DBG_FUNC, "frameRate %d fps, frameDrop %d", frameRate, frameDrop);
  1574. frameDrop = FRAMERATE_MAX; // We can allow the maximum here, because dropping is controlled
  1575. /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
  1576. => frameSkip = 4;
  1577. => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
  1578. frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
  1579. => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
  1580. => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
  1581. */
  1582. errCode = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frameDrop);
  1583. return errCode;
  1584. }
  1585. /*
  1586. * usbvision_frames_alloc
  1587. * allocate the maximum frames this driver can manage
  1588. */
  1589. int usbvision_frames_alloc(struct usb_usbvision *usbvision)
  1590. {
  1591. int i;
  1592. /* Allocate memory for the frame buffers */
  1593. usbvision->max_frame_size = MAX_FRAME_SIZE;
  1594. usbvision->fbuf_size = USBVISION_NUMFRAMES * usbvision->max_frame_size;
  1595. usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size);
  1596. if(usbvision->fbuf == NULL) {
  1597. err("%s: unable to allocate %d bytes for fbuf ",
  1598. __FUNCTION__, usbvision->fbuf_size);
  1599. return -ENOMEM;
  1600. }
  1601. spin_lock_init(&usbvision->queue_lock);
  1602. init_waitqueue_head(&usbvision->wait_frame);
  1603. init_waitqueue_head(&usbvision->wait_stream);
  1604. /* Allocate all buffers */
  1605. for (i = 0; i < USBVISION_NUMFRAMES; i++) {
  1606. usbvision->frame[i].index = i;
  1607. usbvision->frame[i].grabstate = FrameState_Unused;
  1608. usbvision->frame[i].data = usbvision->fbuf +
  1609. i * usbvision->max_frame_size;
  1610. /*
  1611. * Set default sizes for read operation.
  1612. */
  1613. usbvision->stretch_width = 1;
  1614. usbvision->stretch_height = 1;
  1615. usbvision->frame[i].width = usbvision->curwidth;
  1616. usbvision->frame[i].height = usbvision->curheight;
  1617. usbvision->frame[i].bytes_read = 0;
  1618. }
  1619. return 0;
  1620. }
  1621. /*
  1622. * usbvision_frames_free
  1623. * frees memory allocated for the frames
  1624. */
  1625. void usbvision_frames_free(struct usb_usbvision *usbvision)
  1626. {
  1627. /* Have to free all that memory */
  1628. if (usbvision->fbuf != NULL) {
  1629. usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
  1630. usbvision->fbuf = NULL;
  1631. }
  1632. }
  1633. /*
  1634. * usbvision_empty_framequeues()
  1635. * prepare queues for incoming and outgoing frames
  1636. */
  1637. void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
  1638. {
  1639. u32 i;
  1640. INIT_LIST_HEAD(&(usbvision->inqueue));
  1641. INIT_LIST_HEAD(&(usbvision->outqueue));
  1642. for (i = 0; i < USBVISION_NUMFRAMES; i++) {
  1643. usbvision->frame[i].grabstate = FrameState_Unused;
  1644. usbvision->frame[i].bytes_read = 0;
  1645. }
  1646. }
  1647. /*
  1648. * usbvision_stream_interrupt()
  1649. * stops streaming
  1650. */
  1651. int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
  1652. {
  1653. int ret = 0;
  1654. /* stop reading from the device */
  1655. usbvision->streaming = Stream_Interrupt;
  1656. ret = wait_event_timeout(usbvision->wait_stream,
  1657. (usbvision->streaming == Stream_Idle),
  1658. msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES));
  1659. return ret;
  1660. }
  1661. /*
  1662. * usbvision_set_compress_params()
  1663. *
  1664. */
  1665. static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
  1666. {
  1667. static const char proc[] = "usbvision_set_compresion_params: ";
  1668. int rc;
  1669. unsigned char value[6];
  1670. value[0] = 0x0F; // Intra-Compression cycle
  1671. value[1] = 0x01; // Reg.45 one line per strip
  1672. value[2] = 0x00; // Reg.46 Force intra mode on all new frames
  1673. value[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
  1674. value[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
  1675. value[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
  1676. //catched values for NT1004
  1677. // value[0] = 0xFF; // Never apply intra mode automatically
  1678. // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
  1679. // value[2] = 0x01; // Force intra mode on all new frames
  1680. // value[3] = 0x00; // Strip size 400 Bytes; do not force up
  1681. // value[4] = 0xA2; //
  1682. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1683. return 0;
  1684. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1685. USBVISION_OP_CODE,
  1686. USB_DIR_OUT | USB_TYPE_VENDOR |
  1687. USB_RECIP_ENDPOINT, 0,
  1688. (__u16) USBVISION_INTRA_CYC, value, 5, HZ);
  1689. if (rc < 0) {
  1690. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  1691. "reconnect or reload driver.\n", proc, rc);
  1692. return rc;
  1693. }
  1694. if (usbvision->bridgeType == BRIDGE_NT1004) {
  1695. value[0] = 20; // PCM Threshold 1
  1696. value[1] = 12; // PCM Threshold 2
  1697. value[2] = 255; // Distorsion Threshold inter
  1698. value[3] = 255; // Distorsion Threshold intra
  1699. value[4] = 43; // Max Distorsion inter
  1700. value[5] = 43; // Max Distorsion intra
  1701. }
  1702. else {
  1703. value[0] = 20; // PCM Threshold 1
  1704. value[1] = 12; // PCM Threshold 2
  1705. value[2] = 255; // Distorsion Threshold d7-d0
  1706. value[3] = 0; // Distorsion Threshold d11-d8
  1707. value[4] = 43; // Max Distorsion d7-d0
  1708. value[5] = 0; // Max Distorsion d8
  1709. }
  1710. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1711. return 0;
  1712. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1713. USBVISION_OP_CODE,
  1714. USB_DIR_OUT | USB_TYPE_VENDOR |
  1715. USB_RECIP_ENDPOINT, 0,
  1716. (__u16) USBVISION_PCM_THR1, value, 6, HZ);
  1717. if (rc < 0) {
  1718. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  1719. "reconnect or reload driver.\n", proc, rc);
  1720. return rc;
  1721. }
  1722. return rc;
  1723. }
  1724. /*
  1725. * usbvision_set_input()
  1726. *
  1727. * Set the input (saa711x, ...) size x y and other misc input params
  1728. * I've no idea if this parameters are right
  1729. *
  1730. */
  1731. int usbvision_set_input(struct usb_usbvision *usbvision)
  1732. {
  1733. static const char proc[] = "usbvision_set_input: ";
  1734. int rc;
  1735. unsigned char value[8];
  1736. unsigned char dvi_yuv_value;
  1737. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1738. return 0;
  1739. /* Set input format expected from decoder*/
  1740. if (usbvision_device_data[usbvision->DevModel].Vin_Reg1 >= 0) {
  1741. value[0] = usbvision_device_data[usbvision->DevModel].Vin_Reg1 & 0xff;
  1742. } else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
  1743. /* SAA7113 uses 8 bit output */
  1744. value[0] = USBVISION_8_422_SYNC;
  1745. } else {
  1746. /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
  1747. * as that is how saa7111 is configured */
  1748. value[0] = USBVISION_16_422_SYNC;
  1749. /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
  1750. }
  1751. rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]);
  1752. if (rc < 0) {
  1753. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  1754. "reconnect or reload driver.\n", proc, rc);
  1755. return rc;
  1756. }
  1757. if (usbvision->tvnorm->id & V4L2_STD_PAL) {
  1758. value[0] = 0xC0;
  1759. value[1] = 0x02; //0x02C0 -> 704 Input video line length
  1760. value[2] = 0x20;
  1761. value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
  1762. value[4] = 0x60;
  1763. value[5] = 0x00; //0x0060 -> 96 Input video h offset
  1764. value[6] = 0x16;
  1765. value[7] = 0x00; //0x0016 -> 22 Input video v offset
  1766. } else if (usbvision->tvnorm->id & V4L2_STD_SECAM) {
  1767. value[0] = 0xC0;
  1768. value[1] = 0x02; //0x02C0 -> 704 Input video line length
  1769. value[2] = 0x20;
  1770. value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
  1771. value[4] = 0x01;
  1772. value[5] = 0x00; //0x0001 -> 01 Input video h offset
  1773. value[6] = 0x01;
  1774. value[7] = 0x00; //0x0001 -> 01 Input video v offset
  1775. } else { /* V4L2_STD_NTSC */
  1776. value[0] = 0xD0;
  1777. value[1] = 0x02; //0x02D0 -> 720 Input video line length
  1778. value[2] = 0xF0;
  1779. value[3] = 0x00; //0x00F0 -> 240 Input video number of lines
  1780. value[4] = 0x50;
  1781. value[5] = 0x00; //0x0050 -> 80 Input video h offset
  1782. value[6] = 0x10;
  1783. value[7] = 0x00; //0x0010 -> 16 Input video v offset
  1784. }
  1785. if (usbvision_device_data[usbvision->DevModel].X_Offset >= 0) {
  1786. value[4]=usbvision_device_data[usbvision->DevModel].X_Offset & 0xff;
  1787. value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8;
  1788. }
  1789. if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) {
  1790. value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff;
  1791. value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8;
  1792. }
  1793. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1794. USBVISION_OP_CODE, /* USBVISION specific code */
  1795. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
  1796. (__u16) USBVISION_LXSIZE_I, value, 8, HZ);
  1797. if (rc < 0) {
  1798. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  1799. "reconnect or reload driver.\n", proc, rc);
  1800. return rc;
  1801. }
  1802. dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
  1803. if(usbvision_device_data[usbvision->DevModel].Dvi_yuv >= 0){
  1804. dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv & 0xff;
  1805. }
  1806. else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
  1807. /* This changes as the fine sync control changes. Further investigation necessary */
  1808. dvi_yuv_value = 0x06;
  1809. }
  1810. return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value));
  1811. }
  1812. /*
  1813. * usbvision_set_dram_settings()
  1814. *
  1815. * Set the buffer address needed by the usbvision dram to operate
  1816. * This values has been taken with usbsnoop.
  1817. *
  1818. */
  1819. static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
  1820. {
  1821. int rc;
  1822. unsigned char value[8];
  1823. if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  1824. value[0] = 0x42;
  1825. value[1] = 0x71;
  1826. value[2] = 0xff;
  1827. value[3] = 0x00;
  1828. value[4] = 0x98;
  1829. value[5] = 0xe0;
  1830. value[6] = 0x71;
  1831. value[7] = 0xff;
  1832. // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
  1833. // FDL: 0x00000-0x0E099 = 57498 Words
  1834. // VDW: 0x0E3FF-0x3FFFF
  1835. }
  1836. else {
  1837. value[0] = 0x42;
  1838. value[1] = 0x00;
  1839. value[2] = 0xff;
  1840. value[3] = 0x00;
  1841. value[4] = 0x00;
  1842. value[5] = 0x00;
  1843. value[6] = 0x00;
  1844. value[7] = 0xff;
  1845. }
  1846. /* These are the values of the address of the video buffer,
  1847. * they have to be loaded into the USBVISION_DRM_PRM1-8
  1848. *
  1849. * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
  1850. * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
  1851. * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
  1852. * Only used in compressed mode
  1853. * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
  1854. * Only used in compressed mode
  1855. * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
  1856. * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
  1857. */
  1858. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1859. return 0;
  1860. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  1861. USBVISION_OP_CODE, /* USBVISION specific code */
  1862. USB_DIR_OUT | USB_TYPE_VENDOR |
  1863. USB_RECIP_ENDPOINT, 0,
  1864. (__u16) USBVISION_DRM_PRM1, value, 8, HZ);
  1865. if (rc < 0) {
  1866. err("%sERROR=%d", __FUNCTION__, rc);
  1867. return rc;
  1868. }
  1869. /* Restart the video buffer logic */
  1870. if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR |
  1871. USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0)
  1872. return rc;
  1873. rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00);
  1874. return rc;
  1875. }
  1876. /*
  1877. * ()
  1878. *
  1879. * Power on the device, enables suspend-resume logic
  1880. * & reset the isoc End-Point
  1881. *
  1882. */
  1883. int usbvision_power_on(struct usb_usbvision *usbvision)
  1884. {
  1885. int errCode = 0;
  1886. PDEBUG(DBG_FUNC, "");
  1887. usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
  1888. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  1889. USBVISION_SSPND_EN | USBVISION_RES2);
  1890. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  1891. USBVISION_SSPND_EN | USBVISION_PWR_VID);
  1892. errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  1893. USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2);
  1894. if (errCode == 1) {
  1895. usbvision->power = 1;
  1896. }
  1897. PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode<0)?"ERROR":"power is on", errCode);
  1898. return errCode;
  1899. }
  1900. /*
  1901. * usbvision timer stuff
  1902. */
  1903. // to call usbvision_power_off from task queue
  1904. static void call_usbvision_power_off(void *_usbvision)
  1905. {
  1906. struct usb_usbvision *usbvision = _usbvision;
  1907. PDEBUG(DBG_FUNC, "");
  1908. down_interruptible(&usbvision->lock);
  1909. if(usbvision->user == 0) {
  1910. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  1911. usbvision_power_off(usbvision);
  1912. usbvision->initialized = 0;
  1913. }
  1914. up(&usbvision->lock);
  1915. }
  1916. static void usbvision_powerOffTimer(unsigned long data)
  1917. {
  1918. struct usb_usbvision *usbvision = (void *) data;
  1919. PDEBUG(DBG_FUNC, "");
  1920. del_timer(&usbvision->powerOffTimer);
  1921. INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off, usbvision);
  1922. (void) schedule_work(&usbvision->powerOffWork);
  1923. }
  1924. void usbvision_init_powerOffTimer(struct usb_usbvision *usbvision)
  1925. {
  1926. init_timer(&usbvision->powerOffTimer);
  1927. usbvision->powerOffTimer.data = (long) usbvision;
  1928. usbvision->powerOffTimer.function = usbvision_powerOffTimer;
  1929. }
  1930. void usbvision_set_powerOffTimer(struct usb_usbvision *usbvision)
  1931. {
  1932. mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
  1933. }
  1934. void usbvision_reset_powerOffTimer(struct usb_usbvision *usbvision)
  1935. {
  1936. if (timer_pending(&usbvision->powerOffTimer)) {
  1937. del_timer(&usbvision->powerOffTimer);
  1938. }
  1939. }
  1940. /*
  1941. * usbvision_begin_streaming()
  1942. * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
  1943. * idea about the rest
  1944. */
  1945. int usbvision_begin_streaming(struct usb_usbvision *usbvision)
  1946. {
  1947. int errCode = 0;
  1948. if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  1949. usbvision_init_compression(usbvision);
  1950. }
  1951. errCode = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID |
  1952. usbvision->Vin_Reg2_Preset);
  1953. return errCode;
  1954. }
  1955. /*
  1956. * usbvision_restart_isoc()
  1957. * Not sure yet if touching here PWR_REG make loose the config
  1958. */
  1959. int usbvision_restart_isoc(struct usb_usbvision *usbvision)
  1960. {
  1961. int ret;
  1962. if (
  1963. (ret =
  1964. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  1965. USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0)
  1966. return ret;
  1967. if (
  1968. (ret =
  1969. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  1970. USBVISION_SSPND_EN | USBVISION_PWR_VID |
  1971. USBVISION_RES2)) < 0)
  1972. return ret;
  1973. if (
  1974. (ret =
  1975. usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
  1976. USBVISION_KEEP_BLANK | USBVISION_NOHVALID |
  1977. usbvision->Vin_Reg2_Preset)) < 0) return ret;
  1978. /* TODO: schedule timeout */
  1979. while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) && 0x01) != 1);
  1980. return 0;
  1981. }
  1982. int usbvision_audio_off(struct usb_usbvision *usbvision)
  1983. {
  1984. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
  1985. printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n");
  1986. return -1;
  1987. }
  1988. usbvision->AudioMute = 0;
  1989. usbvision->AudioChannel = USBVISION_AUDIO_MUTE;
  1990. return 0;
  1991. }
  1992. int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
  1993. {
  1994. if (!usbvision->AudioMute) {
  1995. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) {
  1996. printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
  1997. return -1;
  1998. }
  1999. }
  2000. usbvision->AudioChannel = AudioChannel;
  2001. return 0;
  2002. }
  2003. int usbvision_setup(struct usb_usbvision *usbvision,int format)
  2004. {
  2005. usbvision_set_video_format(usbvision, format);
  2006. usbvision_set_dram_settings(usbvision);
  2007. usbvision_set_compress_params(usbvision);
  2008. usbvision_set_input(usbvision);
  2009. usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
  2010. usbvision_restart_isoc(usbvision);
  2011. /* cosas del PCM */
  2012. return USBVISION_IS_OPERATIONAL(usbvision);
  2013. }
  2014. int usbvision_sbuf_alloc(struct usb_usbvision *usbvision)
  2015. {
  2016. int i, errCode = 0;
  2017. const int sb_size = USBVISION_URB_FRAMES * USBVISION_MAX_ISOC_PACKET_SIZE;
  2018. /* Clean pointers so we know if we allocated something */
  2019. for (i = 0; i < USBVISION_NUMSBUF; i++)
  2020. usbvision->sbuf[i].data = NULL;
  2021. for (i = 0; i < USBVISION_NUMSBUF; i++) {
  2022. usbvision->sbuf[i].data = kzalloc(sb_size, GFP_KERNEL);
  2023. if (usbvision->sbuf[i].data == NULL) {
  2024. err("%s: unable to allocate %d bytes for sbuf", __FUNCTION__, sb_size);
  2025. errCode = -ENOMEM;
  2026. break;
  2027. }
  2028. }
  2029. return errCode;
  2030. }
  2031. void usbvision_sbuf_free(struct usb_usbvision *usbvision)
  2032. {
  2033. int i;
  2034. for (i = 0; i < USBVISION_NUMSBUF; i++) {
  2035. if (usbvision->sbuf[i].data != NULL) {
  2036. kfree(usbvision->sbuf[i].data);
  2037. usbvision->sbuf[i].data = NULL;
  2038. }
  2039. }
  2040. }
  2041. /*
  2042. * usbvision_init_isoc()
  2043. *
  2044. */
  2045. int usbvision_init_isoc(struct usb_usbvision *usbvision)
  2046. {
  2047. struct usb_device *dev = usbvision->dev;
  2048. int bufIdx, errCode, regValue;
  2049. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2050. return -EFAULT;
  2051. usbvision->curFrame = NULL;
  2052. scratch_reset(usbvision);
  2053. /* Alternate interface 1 is is the biggest frame size */
  2054. errCode = usb_set_interface(dev, usbvision->iface, usbvision->ifaceAltActive);
  2055. if (errCode < 0) {
  2056. usbvision->last_error = errCode;
  2057. return -EBUSY;
  2058. }
  2059. regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
  2060. usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1;
  2061. PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize);
  2062. usbvision->usb_bandwidth = regValue >> 1;
  2063. PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth);
  2064. /* We double buffer the Iso lists */
  2065. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  2066. int j, k;
  2067. struct urb *urb;
  2068. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2069. urb = usb_alloc_urb(USBVISION_URB_FRAMES);
  2070. #else
  2071. urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  2072. #endif
  2073. if (urb == NULL) {
  2074. err("%s: usb_alloc_urb() failed", __FUNCTION__);
  2075. return -ENOMEM;
  2076. }
  2077. usbvision->sbuf[bufIdx].urb = urb;
  2078. urb->dev = dev;
  2079. urb->context = usbvision;
  2080. urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp);
  2081. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2082. urb->transfer_flags = USB_ISO_ASAP;
  2083. #else
  2084. urb->transfer_flags = URB_ISO_ASAP;
  2085. urb->interval = 1;
  2086. #endif
  2087. urb->transfer_buffer = usbvision->sbuf[bufIdx].data;
  2088. urb->complete = usbvision_isocIrq;
  2089. urb->number_of_packets = USBVISION_URB_FRAMES;
  2090. urb->transfer_buffer_length =
  2091. usbvision->isocPacketSize * USBVISION_URB_FRAMES;
  2092. for (j = k = 0; j < USBVISION_URB_FRAMES; j++,
  2093. k += usbvision->isocPacketSize) {
  2094. urb->iso_frame_desc[j].offset = k;
  2095. urb->iso_frame_desc[j].length = usbvision->isocPacketSize;
  2096. }
  2097. }
  2098. /* Submit all URBs */
  2099. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  2100. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2101. errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb);
  2102. #else
  2103. errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb, GFP_KERNEL);
  2104. #endif
  2105. if (errCode) {
  2106. err("%s: usb_submit_urb(%d) failed: error %d", __FUNCTION__, bufIdx, errCode);
  2107. }
  2108. }
  2109. usbvision->streaming = Stream_Idle;
  2110. PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x", __FUNCTION__, usbvision->video_endp);
  2111. return 0;
  2112. }
  2113. /*
  2114. * usbvision_stop_isoc()
  2115. *
  2116. * This procedure stops streaming and deallocates URBs. Then it
  2117. * activates zero-bandwidth alt. setting of the video interface.
  2118. *
  2119. */
  2120. void usbvision_stop_isoc(struct usb_usbvision *usbvision)
  2121. {
  2122. int bufIdx, errCode, regValue;
  2123. if ((usbvision->streaming == Stream_Off) || (usbvision->dev == NULL))
  2124. return;
  2125. /* Unschedule all of the iso td's */
  2126. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  2127. usb_kill_urb(usbvision->sbuf[bufIdx].urb);
  2128. usb_free_urb(usbvision->sbuf[bufIdx].urb);
  2129. usbvision->sbuf[bufIdx].urb = NULL;
  2130. }
  2131. PDEBUG(DBG_ISOC, "%s: streaming=Stream_Off\n", __FUNCTION__);
  2132. usbvision->streaming = Stream_Off;
  2133. if (!usbvision->remove_pending) {
  2134. /* Set packet size to 0 */
  2135. errCode = usb_set_interface(usbvision->dev, usbvision->iface,
  2136. usbvision->ifaceAltInactive);
  2137. if (errCode < 0) {
  2138. err("%s: usb_set_interface() failed: error %d", __FUNCTION__, errCode);
  2139. usbvision->last_error = errCode;
  2140. }
  2141. regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
  2142. usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1;
  2143. PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize);
  2144. usbvision->usb_bandwidth = regValue >> 1;
  2145. PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth);
  2146. }
  2147. }
  2148. int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
  2149. {
  2150. int mode[4];
  2151. int audio[]= {1, 0, 0, 0};
  2152. struct v4l2_routing route;
  2153. //channel 0 is TV with audiochannel 1 (tuner mono)
  2154. //channel 1 is Composite with audio channel 0 (line in)
  2155. //channel 2 is S-Video with audio channel 0 (line in)
  2156. //channel 3 is additional video inputs to the device with audio channel 0 (line in)
  2157. RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
  2158. usbvision->ctl_input = channel;
  2159. route.input = SAA7115_COMPOSITE1;
  2160. call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
  2161. call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->ctl_input);
  2162. // set the new channel
  2163. // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
  2164. // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
  2165. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  2166. case CODEC_SAA7113:
  2167. if (SwitchSVideoInput) { // To handle problems with S-Video Input for some devices. Use SwitchSVideoInput parameter when loading the module.
  2168. mode[2] = 1;
  2169. }
  2170. else {
  2171. mode[2] = 7;
  2172. }
  2173. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  2174. mode[0] = 0; mode[1] = 2; mode[3] = 3; // Special for four input devices
  2175. }
  2176. else {
  2177. mode[0] = 0; mode[1] = 2; //modes for regular saa7113 devices
  2178. }
  2179. break;
  2180. case CODEC_SAA7111:
  2181. mode[0] = 0; mode[1] = 1; mode[2] = 7; //modes for saa7111
  2182. break;
  2183. default:
  2184. mode[0] = 0; mode[1] = 1; mode[2] = 7; //default modes
  2185. }
  2186. route.input = mode[channel];
  2187. call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
  2188. usbvision->channel = channel;
  2189. usbvision_set_audio(usbvision, audio[channel]);
  2190. return 0;
  2191. }
  2192. /*
  2193. * Overrides for Emacs so that we follow Linus's tabbing style.
  2194. * ---------------------------------------------------------------------------
  2195. * Local variables:
  2196. * c-basic-offset: 8
  2197. * End:
  2198. */