usbvision-core.c 72 KB

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