usbvision-core.c 72 KB

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