usbvision-core.c 72 KB

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