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