usbvision-core.c 71 KB

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