usbvision-core.c 72 KB

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