usbvision-core.c 72 KB

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