usbvision-core.c 74 KB

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