usbvision-core.c 72 KB

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