usbvision-core.c 72 KB

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