usbvision-core.c 71 KB

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