usbvision-core.c 72 KB

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