usbvision-core.c 72 KB

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