usbvision-core.c 73 KB

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