usbvision-core.c 71 KB

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