go7007-fw.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. /*
  14. * This file contains code to generate a firmware image for the GO7007SB
  15. * encoder. Much of the firmware is read verbatim from a file, but some of
  16. * it concerning bitrate control and other things that can be configured at
  17. * run-time are generated dynamically. Note that the format headers
  18. * generated here do not affect the functioning of the encoder; they are
  19. * merely parroted back to the host at the start of each frame.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/time.h>
  23. #include <linux/mm.h>
  24. #include <linux/device.h>
  25. #include <linux/i2c.h>
  26. #include <linux/firmware.h>
  27. #include <linux/slab.h>
  28. #include <asm/byteorder.h>
  29. #include "go7007-priv.h"
  30. #define GO7007_FW_NAME "go7007/go7007tv.bin"
  31. /* Constants used in the source firmware image to describe code segments */
  32. #define FLAG_MODE_MJPEG (1)
  33. #define FLAG_MODE_MPEG1 (1<<1)
  34. #define FLAG_MODE_MPEG2 (1<<2)
  35. #define FLAG_MODE_MPEG4 (1<<3)
  36. #define FLAG_MODE_H263 (1<<4)
  37. #define FLAG_MODE_ALL (FLAG_MODE_MJPEG | FLAG_MODE_MPEG1 | \
  38. FLAG_MODE_MPEG2 | FLAG_MODE_MPEG4 | \
  39. FLAG_MODE_H263)
  40. #define FLAG_SPECIAL (1<<8)
  41. #define SPECIAL_FRM_HEAD 0
  42. #define SPECIAL_BRC_CTRL 1
  43. #define SPECIAL_CONFIG 2
  44. #define SPECIAL_SEQHEAD 3
  45. #define SPECIAL_AV_SYNC 4
  46. #define SPECIAL_FINAL 5
  47. #define SPECIAL_AUDIO 6
  48. #define SPECIAL_MODET 7
  49. /* Little data class for creating MPEG headers bit-by-bit */
  50. struct code_gen {
  51. unsigned char *p; /* destination */
  52. u32 a; /* collects bits at the top of the variable */
  53. int b; /* bit position of most recently-written bit */
  54. int len; /* written out so far */
  55. };
  56. #define CODE_GEN(name, dest) struct code_gen name = { dest, 0, 32, 0 }
  57. #define CODE_ADD(name, val, length) do { \
  58. name.b -= (length); \
  59. name.a |= (val) << name.b; \
  60. while (name.b <= 24) { \
  61. *name.p = name.a >> 24; \
  62. ++name.p; \
  63. name.a <<= 8; \
  64. name.b += 8; \
  65. name.len += 8; \
  66. } \
  67. } while (0)
  68. #define CODE_LENGTH(name) (name.len + (32 - name.b))
  69. /* Tables for creating the bitrate control data */
  70. static const s16 converge_speed_ip[101] = {
  71. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  72. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  73. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  74. 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
  76. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
  77. 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
  78. 9, 10, 10, 11, 12, 13, 14, 15, 16, 17,
  79. 19, 20, 22, 23, 25, 27, 30, 32, 35, 38,
  80. 41, 45, 49, 53, 58, 63, 69, 76, 83, 91,
  81. 100
  82. };
  83. static const s16 converge_speed_ipb[101] = {
  84. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  85. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  86. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
  87. 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
  88. 6, 6, 6, 7, 7, 7, 7, 8, 8, 9,
  89. 9, 9, 10, 10, 11, 12, 12, 13, 14, 14,
  90. 15, 16, 17, 18, 19, 20, 22, 23, 25, 26,
  91. 28, 30, 32, 34, 37, 40, 42, 46, 49, 53,
  92. 57, 61, 66, 71, 77, 83, 90, 97, 106, 115,
  93. 125, 135, 147, 161, 175, 191, 209, 228, 249, 273,
  94. 300
  95. };
  96. static const s16 LAMBDA_table[4][101] = {
  97. { 16, 16, 16, 16, 17, 17, 17, 18, 18, 18,
  98. 19, 19, 19, 20, 20, 20, 21, 21, 22, 22,
  99. 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
  100. 27, 27, 28, 28, 29, 29, 30, 31, 31, 32,
  101. 32, 33, 33, 34, 35, 35, 36, 37, 37, 38,
  102. 39, 39, 40, 41, 42, 42, 43, 44, 45, 46,
  103. 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
  104. 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
  105. 67, 68, 69, 70, 72, 73, 74, 76, 77, 78,
  106. 80, 81, 83, 84, 86, 87, 89, 90, 92, 94,
  107. 96
  108. },
  109. {
  110. 20, 20, 20, 21, 21, 21, 22, 22, 23, 23,
  111. 23, 24, 24, 25, 25, 26, 26, 27, 27, 28,
  112. 28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
  113. 34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
  114. 40, 41, 42, 43, 43, 44, 45, 46, 47, 48,
  115. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  116. 58, 59, 60, 61, 62, 64, 65, 66, 67, 68,
  117. 70, 71, 72, 73, 75, 76, 78, 79, 80, 82,
  118. 83, 85, 86, 88, 90, 91, 93, 95, 96, 98,
  119. 100, 102, 103, 105, 107, 109, 111, 113, 115, 117,
  120. 120
  121. },
  122. {
  123. 24, 24, 24, 25, 25, 26, 26, 27, 27, 28,
  124. 28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
  125. 34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
  126. 41, 41, 42, 43, 44, 44, 45, 46, 47, 48,
  127. 49, 50, 50, 51, 52, 53, 54, 55, 56, 57,
  128. 58, 59, 60, 62, 63, 64, 65, 66, 67, 69,
  129. 70, 71, 72, 74, 75, 76, 78, 79, 81, 82,
  130. 84, 85, 87, 88, 90, 92, 93, 95, 97, 98,
  131. 100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
  132. 120, 122, 124, 127, 129, 131, 134, 136, 138, 141,
  133. 144
  134. },
  135. {
  136. 32, 32, 33, 33, 34, 34, 35, 36, 36, 37,
  137. 38, 38, 39, 40, 41, 41, 42, 43, 44, 44,
  138. 45, 46, 47, 48, 49, 50, 50, 51, 52, 53,
  139. 54, 55, 56, 57, 58, 59, 60, 62, 63, 64,
  140. 65, 66, 67, 69, 70, 71, 72, 74, 75, 76,
  141. 78, 79, 81, 82, 84, 85, 87, 88, 90, 92,
  142. 93, 95, 97, 98, 100, 102, 104, 106, 108, 110,
  143. 112, 114, 116, 118, 120, 122, 124, 127, 129, 131,
  144. 134, 136, 139, 141, 144, 146, 149, 152, 154, 157,
  145. 160, 163, 166, 169, 172, 175, 178, 181, 185, 188,
  146. 192
  147. }
  148. };
  149. /* MPEG blank frame generation tables */
  150. enum mpeg_frame_type {
  151. PFRAME,
  152. BFRAME_PRE,
  153. BFRAME_POST,
  154. BFRAME_BIDIR,
  155. BFRAME_EMPTY
  156. };
  157. static const u32 addrinctab[33][2] = {
  158. { 0x01, 1 }, { 0x03, 3 }, { 0x02, 3 }, { 0x03, 4 },
  159. { 0x02, 4 }, { 0x03, 5 }, { 0x02, 5 }, { 0x07, 7 },
  160. { 0x06, 7 }, { 0x0b, 8 }, { 0x0a, 8 }, { 0x09, 8 },
  161. { 0x08, 8 }, { 0x07, 8 }, { 0x06, 8 }, { 0x17, 10 },
  162. { 0x16, 10 }, { 0x15, 10 }, { 0x14, 10 }, { 0x13, 10 },
  163. { 0x12, 10 }, { 0x23, 11 }, { 0x22, 11 }, { 0x21, 11 },
  164. { 0x20, 11 }, { 0x1f, 11 }, { 0x1e, 11 }, { 0x1d, 11 },
  165. { 0x1c, 11 }, { 0x1b, 11 }, { 0x1a, 11 }, { 0x19, 11 },
  166. { 0x18, 11 }
  167. };
  168. /* Standard JPEG tables */
  169. static const u8 default_intra_quant_table[] = {
  170. 8, 16, 19, 22, 26, 27, 29, 34,
  171. 16, 16, 22, 24, 27, 29, 34, 37,
  172. 19, 22, 26, 27, 29, 34, 34, 38,
  173. 22, 22, 26, 27, 29, 34, 37, 40,
  174. 22, 26, 27, 29, 32, 35, 40, 48,
  175. 26, 27, 29, 32, 35, 40, 48, 58,
  176. 26, 27, 29, 34, 38, 46, 56, 69,
  177. 27, 29, 35, 38, 46, 56, 69, 83
  178. };
  179. static const u8 bits_dc_luminance[] = {
  180. 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
  181. };
  182. static const u8 val_dc_luminance[] = {
  183. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  184. };
  185. static const u8 bits_dc_chrominance[] = {
  186. 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
  187. };
  188. static const u8 val_dc_chrominance[] = {
  189. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  190. };
  191. static const u8 bits_ac_luminance[] = {
  192. 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
  193. };
  194. static const u8 val_ac_luminance[] = {
  195. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  196. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  197. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  198. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  199. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  200. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  201. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  202. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  203. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  204. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  205. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  206. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  207. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  208. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  209. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  210. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  211. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  212. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  213. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  214. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  215. 0xf9, 0xfa
  216. };
  217. static const u8 bits_ac_chrominance[] = {
  218. 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
  219. };
  220. static const u8 val_ac_chrominance[] = {
  221. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  222. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  223. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  224. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  225. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  226. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  227. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  228. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  229. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  230. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  231. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  232. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  233. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  234. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  235. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  236. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  237. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  238. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  239. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  240. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  241. 0xf9, 0xfa
  242. };
  243. /* Zig-zag mapping for quant table
  244. *
  245. * OK, let's do this mapping on the actual table above so it doesn't have
  246. * to be done on the fly.
  247. */
  248. static const int zz[64] = {
  249. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  250. 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  251. 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  252. 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  253. };
  254. static int copy_packages(__le16 *dest, u16 *src, int pkg_cnt, int space)
  255. {
  256. int i, cnt = pkg_cnt * 32;
  257. if (space < cnt)
  258. return -1;
  259. for (i = 0; i < cnt; ++i)
  260. dest[i] = cpu_to_le16p(src + i);
  261. return cnt;
  262. }
  263. static int mjpeg_frame_header(struct go7007 *go, unsigned char *buf, int q)
  264. {
  265. int i, p = 0;
  266. buf[p++] = 0xff;
  267. buf[p++] = 0xd8;
  268. buf[p++] = 0xff;
  269. buf[p++] = 0xdb;
  270. buf[p++] = 0;
  271. buf[p++] = 2 + 65;
  272. buf[p++] = 0;
  273. buf[p++] = default_intra_quant_table[0];
  274. for (i = 1; i < 64; ++i)
  275. /* buf[p++] = (default_intra_quant_table[i] * q) >> 3; */
  276. buf[p++] = (default_intra_quant_table[zz[i]] * q) >> 3;
  277. buf[p++] = 0xff;
  278. buf[p++] = 0xc0;
  279. buf[p++] = 0;
  280. buf[p++] = 17;
  281. buf[p++] = 8;
  282. buf[p++] = go->height >> 8;
  283. buf[p++] = go->height & 0xff;
  284. buf[p++] = go->width >> 8;
  285. buf[p++] = go->width & 0xff;
  286. buf[p++] = 3;
  287. buf[p++] = 1;
  288. buf[p++] = 0x22;
  289. buf[p++] = 0;
  290. buf[p++] = 2;
  291. buf[p++] = 0x11;
  292. buf[p++] = 0;
  293. buf[p++] = 3;
  294. buf[p++] = 0x11;
  295. buf[p++] = 0;
  296. buf[p++] = 0xff;
  297. buf[p++] = 0xc4;
  298. buf[p++] = 418 >> 8;
  299. buf[p++] = 418 & 0xff;
  300. buf[p++] = 0x00;
  301. memcpy(buf + p, bits_dc_luminance + 1, 16);
  302. p += 16;
  303. memcpy(buf + p, val_dc_luminance, sizeof(val_dc_luminance));
  304. p += sizeof(val_dc_luminance);
  305. buf[p++] = 0x01;
  306. memcpy(buf + p, bits_dc_chrominance + 1, 16);
  307. p += 16;
  308. memcpy(buf + p, val_dc_chrominance, sizeof(val_dc_chrominance));
  309. p += sizeof(val_dc_chrominance);
  310. buf[p++] = 0x10;
  311. memcpy(buf + p, bits_ac_luminance + 1, 16);
  312. p += 16;
  313. memcpy(buf + p, val_ac_luminance, sizeof(val_ac_luminance));
  314. p += sizeof(val_ac_luminance);
  315. buf[p++] = 0x11;
  316. memcpy(buf + p, bits_ac_chrominance + 1, 16);
  317. p += 16;
  318. memcpy(buf + p, val_ac_chrominance, sizeof(val_ac_chrominance));
  319. p += sizeof(val_ac_chrominance);
  320. buf[p++] = 0xff;
  321. buf[p++] = 0xda;
  322. buf[p++] = 0;
  323. buf[p++] = 12;
  324. buf[p++] = 3;
  325. buf[p++] = 1;
  326. buf[p++] = 0x00;
  327. buf[p++] = 2;
  328. buf[p++] = 0x11;
  329. buf[p++] = 3;
  330. buf[p++] = 0x11;
  331. buf[p++] = 0;
  332. buf[p++] = 63;
  333. buf[p++] = 0;
  334. return p;
  335. }
  336. static int gen_mjpeghdr_to_package(struct go7007 *go, __le16 *code, int space)
  337. {
  338. u8 *buf;
  339. u16 mem = 0x3e00;
  340. unsigned int addr = 0x19;
  341. int size = 0, i, off = 0, chunk;
  342. buf = kzalloc(4096, GFP_KERNEL);
  343. if (buf == NULL)
  344. return -ENOMEM;
  345. for (i = 1; i < 32; ++i) {
  346. mjpeg_frame_header(go, buf + size, i);
  347. size += 80;
  348. }
  349. chunk = mjpeg_frame_header(go, buf + size, 1);
  350. memmove(buf + size, buf + size + 80, chunk - 80);
  351. size += chunk - 80;
  352. for (i = 0; i < size; i += chunk * 2) {
  353. if (space - off < 32) {
  354. off = -1;
  355. goto done;
  356. }
  357. code[off + 1] = __cpu_to_le16(0x8000 | mem);
  358. chunk = 28;
  359. if (mem + chunk > 0x4000)
  360. chunk = 0x4000 - mem;
  361. if (i + 2 * chunk > size)
  362. chunk = (size - i) / 2;
  363. if (chunk < 28) {
  364. code[off] = __cpu_to_le16(0x4000 | chunk);
  365. code[off + 31] = __cpu_to_le16(addr++);
  366. mem = 0x3e00;
  367. } else {
  368. code[off] = __cpu_to_le16(0x1000 | 28);
  369. code[off + 31] = 0;
  370. mem += 28;
  371. }
  372. memcpy(&code[off + 2], buf + i, chunk * 2);
  373. off += 32;
  374. }
  375. done:
  376. kfree(buf);
  377. return off;
  378. }
  379. static int mpeg1_frame_header(struct go7007 *go, unsigned char *buf,
  380. int modulo, int pict_struct, enum mpeg_frame_type frame)
  381. {
  382. int i, j, mb_code, mb_len;
  383. int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
  384. CODE_GEN(c, buf + 6);
  385. switch (frame) {
  386. case PFRAME:
  387. mb_code = 0x1;
  388. mb_len = 3;
  389. break;
  390. case BFRAME_PRE:
  391. mb_code = 0x2;
  392. mb_len = 4;
  393. break;
  394. case BFRAME_POST:
  395. mb_code = 0x2;
  396. mb_len = 3;
  397. break;
  398. case BFRAME_BIDIR:
  399. mb_code = 0x2;
  400. mb_len = 2;
  401. break;
  402. default: /* keep the compiler happy */
  403. mb_code = mb_len = 0;
  404. break;
  405. }
  406. CODE_ADD(c, frame == PFRAME ? 0x2 : 0x3, 13);
  407. CODE_ADD(c, 0xffff, 16);
  408. CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 0x7 : 0x4, 4);
  409. if (frame != PFRAME)
  410. CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 0x7 : 0x4, 4);
  411. else
  412. CODE_ADD(c, 0, 4); /* Is this supposed to be here?? */
  413. CODE_ADD(c, 0, 3); /* What is this?? */
  414. /* Byte-align with zeros */
  415. j = 8 - (CODE_LENGTH(c) % 8);
  416. if (j != 8)
  417. CODE_ADD(c, 0, j);
  418. if (go->format == V4L2_PIX_FMT_MPEG2) {
  419. CODE_ADD(c, 0x1, 24);
  420. CODE_ADD(c, 0xb5, 8);
  421. CODE_ADD(c, 0x844, 12);
  422. CODE_ADD(c, frame == PFRAME ? 0xff : 0x44, 8);
  423. if (go->interlace_coding) {
  424. CODE_ADD(c, pict_struct, 4);
  425. if (go->dvd_mode)
  426. CODE_ADD(c, 0x000, 11);
  427. else
  428. CODE_ADD(c, 0x200, 11);
  429. } else {
  430. CODE_ADD(c, 0x3, 4);
  431. CODE_ADD(c, 0x20c, 11);
  432. }
  433. /* Byte-align with zeros */
  434. j = 8 - (CODE_LENGTH(c) % 8);
  435. if (j != 8)
  436. CODE_ADD(c, 0, j);
  437. }
  438. for (i = 0; i < rows; ++i) {
  439. CODE_ADD(c, 1, 24);
  440. CODE_ADD(c, i + 1, 8);
  441. CODE_ADD(c, 0x2, 6);
  442. CODE_ADD(c, 0x1, 1);
  443. CODE_ADD(c, mb_code, mb_len);
  444. if (go->interlace_coding) {
  445. CODE_ADD(c, 0x1, 2);
  446. CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
  447. }
  448. if (frame == BFRAME_BIDIR) {
  449. CODE_ADD(c, 0x3, 2);
  450. if (go->interlace_coding)
  451. CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
  452. }
  453. CODE_ADD(c, 0x3, 2);
  454. for (j = (go->width >> 4) - 2; j >= 33; j -= 33)
  455. CODE_ADD(c, 0x8, 11);
  456. CODE_ADD(c, addrinctab[j][0], addrinctab[j][1]);
  457. CODE_ADD(c, mb_code, mb_len);
  458. if (go->interlace_coding) {
  459. CODE_ADD(c, 0x1, 2);
  460. CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
  461. }
  462. if (frame == BFRAME_BIDIR) {
  463. CODE_ADD(c, 0x3, 2);
  464. if (go->interlace_coding)
  465. CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
  466. }
  467. CODE_ADD(c, 0x3, 2);
  468. /* Byte-align with zeros */
  469. j = 8 - (CODE_LENGTH(c) % 8);
  470. if (j != 8)
  471. CODE_ADD(c, 0, j);
  472. }
  473. i = CODE_LENGTH(c) + 4 * 8;
  474. buf[2] = 0x00;
  475. buf[3] = 0x00;
  476. buf[4] = 0x01;
  477. buf[5] = 0x00;
  478. return i;
  479. }
  480. static int mpeg1_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
  481. {
  482. int i, aspect_ratio, picture_rate;
  483. CODE_GEN(c, buf + 6);
  484. if (go->format == V4L2_PIX_FMT_MPEG1) {
  485. switch (go->aspect_ratio) {
  486. case GO7007_RATIO_4_3:
  487. aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
  488. break;
  489. case GO7007_RATIO_16_9:
  490. aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
  491. break;
  492. default:
  493. aspect_ratio = 1;
  494. break;
  495. }
  496. } else {
  497. switch (go->aspect_ratio) {
  498. case GO7007_RATIO_4_3:
  499. aspect_ratio = 2;
  500. break;
  501. case GO7007_RATIO_16_9:
  502. aspect_ratio = 3;
  503. break;
  504. default:
  505. aspect_ratio = 1;
  506. break;
  507. }
  508. }
  509. switch (go->sensor_framerate) {
  510. case 24000:
  511. picture_rate = 1;
  512. break;
  513. case 24024:
  514. picture_rate = 2;
  515. break;
  516. case 25025:
  517. picture_rate = go->interlace_coding ? 6 : 3;
  518. break;
  519. case 30000:
  520. picture_rate = go->interlace_coding ? 7 : 4;
  521. break;
  522. case 30030:
  523. picture_rate = go->interlace_coding ? 8 : 5;
  524. break;
  525. default:
  526. picture_rate = 5; /* 30 fps seems like a reasonable default */
  527. break;
  528. }
  529. CODE_ADD(c, go->width, 12);
  530. CODE_ADD(c, go->height, 12);
  531. CODE_ADD(c, aspect_ratio, 4);
  532. CODE_ADD(c, picture_rate, 4);
  533. CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 20000 : 0x3ffff, 18);
  534. CODE_ADD(c, 1, 1);
  535. CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 112 : 20, 10);
  536. CODE_ADD(c, 0, 3);
  537. /* Byte-align with zeros */
  538. i = 8 - (CODE_LENGTH(c) % 8);
  539. if (i != 8)
  540. CODE_ADD(c, 0, i);
  541. if (go->format == V4L2_PIX_FMT_MPEG2) {
  542. CODE_ADD(c, 0x1, 24);
  543. CODE_ADD(c, 0xb5, 8);
  544. CODE_ADD(c, 0x148, 12);
  545. if (go->interlace_coding)
  546. CODE_ADD(c, 0x20001, 20);
  547. else
  548. CODE_ADD(c, 0xa0001, 20);
  549. CODE_ADD(c, 0, 16);
  550. /* Byte-align with zeros */
  551. i = 8 - (CODE_LENGTH(c) % 8);
  552. if (i != 8)
  553. CODE_ADD(c, 0, i);
  554. if (ext) {
  555. CODE_ADD(c, 0x1, 24);
  556. CODE_ADD(c, 0xb52, 12);
  557. CODE_ADD(c, go->standard == GO7007_STD_NTSC ? 2 : 1, 3);
  558. CODE_ADD(c, 0x105, 9);
  559. CODE_ADD(c, 0x505, 16);
  560. CODE_ADD(c, go->width, 14);
  561. CODE_ADD(c, 1, 1);
  562. CODE_ADD(c, go->height, 14);
  563. /* Byte-align with zeros */
  564. i = 8 - (CODE_LENGTH(c) % 8);
  565. if (i != 8)
  566. CODE_ADD(c, 0, i);
  567. }
  568. }
  569. i = CODE_LENGTH(c) + 4 * 8;
  570. buf[0] = i & 0xff;
  571. buf[1] = i >> 8;
  572. buf[2] = 0x00;
  573. buf[3] = 0x00;
  574. buf[4] = 0x01;
  575. buf[5] = 0xb3;
  576. return i;
  577. }
  578. static int gen_mpeg1hdr_to_package(struct go7007 *go,
  579. __le16 *code, int space, int *framelen)
  580. {
  581. u8 *buf;
  582. u16 mem = 0x3e00;
  583. unsigned int addr = 0x19;
  584. int i, off = 0, chunk;
  585. buf = kzalloc(5120, GFP_KERNEL);
  586. if (buf == NULL)
  587. return -ENOMEM;
  588. framelen[0] = mpeg1_frame_header(go, buf, 0, 1, PFRAME);
  589. if (go->interlace_coding)
  590. framelen[0] += mpeg1_frame_header(go, buf + framelen[0] / 8,
  591. 0, 2, PFRAME);
  592. buf[0] = framelen[0] & 0xff;
  593. buf[1] = framelen[0] >> 8;
  594. i = 368;
  595. framelen[1] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_PRE);
  596. if (go->interlace_coding)
  597. framelen[1] += mpeg1_frame_header(go, buf + i + framelen[1] / 8,
  598. 0, 2, BFRAME_PRE);
  599. buf[i] = framelen[1] & 0xff;
  600. buf[i + 1] = framelen[1] >> 8;
  601. i += 1632;
  602. framelen[2] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_POST);
  603. if (go->interlace_coding)
  604. framelen[2] += mpeg1_frame_header(go, buf + i + framelen[2] / 8,
  605. 0, 2, BFRAME_POST);
  606. buf[i] = framelen[2] & 0xff;
  607. buf[i + 1] = framelen[2] >> 8;
  608. i += 1432;
  609. framelen[3] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_BIDIR);
  610. if (go->interlace_coding)
  611. framelen[3] += mpeg1_frame_header(go, buf + i + framelen[3] / 8,
  612. 0, 2, BFRAME_BIDIR);
  613. buf[i] = framelen[3] & 0xff;
  614. buf[i + 1] = framelen[3] >> 8;
  615. i += 1632 + 16;
  616. mpeg1_sequence_header(go, buf + i, 0);
  617. i += 40;
  618. for (i = 0; i < 5120; i += chunk * 2) {
  619. if (space - off < 32) {
  620. off = -1;
  621. goto done;
  622. }
  623. code[off + 1] = __cpu_to_le16(0x8000 | mem);
  624. chunk = 28;
  625. if (mem + chunk > 0x4000)
  626. chunk = 0x4000 - mem;
  627. if (i + 2 * chunk > 5120)
  628. chunk = (5120 - i) / 2;
  629. if (chunk < 28) {
  630. code[off] = __cpu_to_le16(0x4000 | chunk);
  631. code[off + 31] = __cpu_to_le16(addr);
  632. if (mem + chunk == 0x4000) {
  633. mem = 0x3e00;
  634. ++addr;
  635. }
  636. } else {
  637. code[off] = __cpu_to_le16(0x1000 | 28);
  638. code[off + 31] = 0;
  639. mem += 28;
  640. }
  641. memcpy(&code[off + 2], buf + i, chunk * 2);
  642. off += 32;
  643. }
  644. done:
  645. kfree(buf);
  646. return off;
  647. }
  648. static int vti_bitlen(struct go7007 *go)
  649. {
  650. unsigned int i, max_time_incr = go->sensor_framerate / go->fps_scale;
  651. for (i = 31; (max_time_incr & ((1 << i) - 1)) == max_time_incr; --i)
  652. ;
  653. return i + 1;
  654. }
  655. static int mpeg4_frame_header(struct go7007 *go, unsigned char *buf,
  656. int modulo, enum mpeg_frame_type frame)
  657. {
  658. int i;
  659. CODE_GEN(c, buf + 6);
  660. int mb_count = (go->width >> 4) * (go->height >> 4);
  661. CODE_ADD(c, frame == PFRAME ? 0x1 : 0x2, 2);
  662. if (modulo)
  663. CODE_ADD(c, 0x1, 1);
  664. CODE_ADD(c, 0x1, 2);
  665. CODE_ADD(c, 0, vti_bitlen(go));
  666. CODE_ADD(c, 0x3, 2);
  667. if (frame == PFRAME)
  668. CODE_ADD(c, 0, 1);
  669. CODE_ADD(c, 0xc, 11);
  670. if (frame != PFRAME)
  671. CODE_ADD(c, 0x4, 3);
  672. if (frame != BFRAME_EMPTY) {
  673. for (i = 0; i < mb_count; ++i) {
  674. switch (frame) {
  675. case PFRAME:
  676. CODE_ADD(c, 0x1, 1);
  677. break;
  678. case BFRAME_PRE:
  679. CODE_ADD(c, 0x47, 8);
  680. break;
  681. case BFRAME_POST:
  682. CODE_ADD(c, 0x27, 7);
  683. break;
  684. case BFRAME_BIDIR:
  685. CODE_ADD(c, 0x5f, 8);
  686. break;
  687. case BFRAME_EMPTY: /* keep compiler quiet */
  688. break;
  689. }
  690. }
  691. }
  692. /* Byte-align with a zero followed by ones */
  693. i = 8 - (CODE_LENGTH(c) % 8);
  694. CODE_ADD(c, 0, 1);
  695. CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
  696. i = CODE_LENGTH(c) + 4 * 8;
  697. buf[0] = i & 0xff;
  698. buf[1] = i >> 8;
  699. buf[2] = 0x00;
  700. buf[3] = 0x00;
  701. buf[4] = 0x01;
  702. buf[5] = 0xb6;
  703. return i;
  704. }
  705. static int mpeg4_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
  706. {
  707. const unsigned char head[] = { 0x00, 0x00, 0x01, 0xb0, go->pali,
  708. 0x00, 0x00, 0x01, 0xb5, 0x09,
  709. 0x00, 0x00, 0x01, 0x00,
  710. 0x00, 0x00, 0x01, 0x20, };
  711. int i, aspect_ratio;
  712. int fps = go->sensor_framerate / go->fps_scale;
  713. CODE_GEN(c, buf + 2 + sizeof(head));
  714. switch (go->aspect_ratio) {
  715. case GO7007_RATIO_4_3:
  716. aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
  717. break;
  718. case GO7007_RATIO_16_9:
  719. aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
  720. break;
  721. default:
  722. aspect_ratio = 1;
  723. break;
  724. }
  725. memcpy(buf + 2, head, sizeof(head));
  726. CODE_ADD(c, 0x191, 17);
  727. CODE_ADD(c, aspect_ratio, 4);
  728. CODE_ADD(c, 0x1, 4);
  729. CODE_ADD(c, fps, 16);
  730. CODE_ADD(c, 0x3, 2);
  731. CODE_ADD(c, 1001, vti_bitlen(go));
  732. CODE_ADD(c, 1, 1);
  733. CODE_ADD(c, go->width, 13);
  734. CODE_ADD(c, 1, 1);
  735. CODE_ADD(c, go->height, 13);
  736. CODE_ADD(c, 0x2830, 14);
  737. /* Byte-align */
  738. i = 8 - (CODE_LENGTH(c) % 8);
  739. CODE_ADD(c, 0, 1);
  740. CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
  741. i = CODE_LENGTH(c) + sizeof(head) * 8;
  742. buf[0] = i & 0xff;
  743. buf[1] = i >> 8;
  744. return i;
  745. }
  746. static int gen_mpeg4hdr_to_package(struct go7007 *go,
  747. __le16 *code, int space, int *framelen)
  748. {
  749. u8 *buf;
  750. u16 mem = 0x3e00;
  751. unsigned int addr = 0x19;
  752. int i, off = 0, chunk;
  753. buf = kzalloc(5120, GFP_KERNEL);
  754. if (buf == NULL)
  755. return -ENOMEM;
  756. framelen[0] = mpeg4_frame_header(go, buf, 0, PFRAME);
  757. i = 368;
  758. framelen[1] = mpeg4_frame_header(go, buf + i, 0, BFRAME_PRE);
  759. i += 1632;
  760. framelen[2] = mpeg4_frame_header(go, buf + i, 0, BFRAME_POST);
  761. i += 1432;
  762. framelen[3] = mpeg4_frame_header(go, buf + i, 0, BFRAME_BIDIR);
  763. i += 1632;
  764. mpeg4_frame_header(go, buf + i, 0, BFRAME_EMPTY);
  765. i += 16;
  766. mpeg4_sequence_header(go, buf + i, 0);
  767. i += 40;
  768. for (i = 0; i < 5120; i += chunk * 2) {
  769. if (space - off < 32) {
  770. off = -1;
  771. goto done;
  772. }
  773. code[off + 1] = __cpu_to_le16(0x8000 | mem);
  774. chunk = 28;
  775. if (mem + chunk > 0x4000)
  776. chunk = 0x4000 - mem;
  777. if (i + 2 * chunk > 5120)
  778. chunk = (5120 - i) / 2;
  779. if (chunk < 28) {
  780. code[off] = __cpu_to_le16(0x4000 | chunk);
  781. code[off + 31] = __cpu_to_le16(addr);
  782. if (mem + chunk == 0x4000) {
  783. mem = 0x3e00;
  784. ++addr;
  785. }
  786. } else {
  787. code[off] = __cpu_to_le16(0x1000 | 28);
  788. code[off + 31] = 0;
  789. mem += 28;
  790. }
  791. memcpy(&code[off + 2], buf + i, chunk * 2);
  792. off += 32;
  793. }
  794. mem = 0x3e00;
  795. addr = go->ipb ? 0x14f9 : 0x0af9;
  796. memset(buf, 0, 5120);
  797. framelen[4] = mpeg4_frame_header(go, buf, 1, PFRAME);
  798. i = 368;
  799. framelen[5] = mpeg4_frame_header(go, buf + i, 1, BFRAME_PRE);
  800. i += 1632;
  801. framelen[6] = mpeg4_frame_header(go, buf + i, 1, BFRAME_POST);
  802. i += 1432;
  803. framelen[7] = mpeg4_frame_header(go, buf + i, 1, BFRAME_BIDIR);
  804. i += 1632;
  805. mpeg4_frame_header(go, buf + i, 1, BFRAME_EMPTY);
  806. i += 16;
  807. for (i = 0; i < 5120; i += chunk * 2) {
  808. if (space - off < 32) {
  809. off = -1;
  810. goto done;
  811. }
  812. code[off + 1] = __cpu_to_le16(0x8000 | mem);
  813. chunk = 28;
  814. if (mem + chunk > 0x4000)
  815. chunk = 0x4000 - mem;
  816. if (i + 2 * chunk > 5120)
  817. chunk = (5120 - i) / 2;
  818. if (chunk < 28) {
  819. code[off] = __cpu_to_le16(0x4000 | chunk);
  820. code[off + 31] = __cpu_to_le16(addr);
  821. if (mem + chunk == 0x4000) {
  822. mem = 0x3e00;
  823. ++addr;
  824. }
  825. } else {
  826. code[off] = __cpu_to_le16(0x1000 | 28);
  827. code[off + 31] = 0;
  828. mem += 28;
  829. }
  830. memcpy(&code[off + 2], buf + i, chunk * 2);
  831. off += 32;
  832. }
  833. done:
  834. kfree(buf);
  835. return off;
  836. }
  837. static int brctrl_to_package(struct go7007 *go,
  838. __le16 *code, int space, int *framelen)
  839. {
  840. int converge_speed = 0;
  841. int lambda = (go->format == V4L2_PIX_FMT_MJPEG || go->dvd_mode) ?
  842. 100 : 0;
  843. int peak_rate = 6 * go->bitrate / 5;
  844. int vbv_buffer = go->format == V4L2_PIX_FMT_MJPEG ?
  845. go->bitrate :
  846. (go->dvd_mode ? 900000 : peak_rate);
  847. int fps = go->sensor_framerate / go->fps_scale;
  848. int q = 0;
  849. /* Bizarre math below depends on rounding errors in division */
  850. u32 sgop_expt_addr = go->bitrate / 32 * (go->ipb ? 3 : 1) * 1001 / fps;
  851. u32 sgop_peak_addr = peak_rate / 32 * 1001 / fps;
  852. u32 total_expt_addr = go->bitrate / 32 * 1000 / fps * (fps / 1000);
  853. u32 vbv_alert_addr = vbv_buffer * 3 / (4 * 32);
  854. u32 cplx[] = {
  855. q > 0 ? sgop_expt_addr * q :
  856. 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
  857. q > 0 ? sgop_expt_addr * q :
  858. 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
  859. q > 0 ? sgop_expt_addr * q :
  860. 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
  861. q > 0 ? sgop_expt_addr * q :
  862. 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
  863. };
  864. u32 calc_q = q > 0 ? q : cplx[0] / sgop_expt_addr;
  865. u16 pack[] = {
  866. 0x200e, 0x0000,
  867. 0xBF20, go->ipb ? converge_speed_ipb[converge_speed]
  868. : converge_speed_ip[converge_speed],
  869. 0xBF21, go->ipb ? 2 : 0,
  870. 0xBF22, go->ipb ? LAMBDA_table[0][lambda / 2 + 50]
  871. : 32767,
  872. 0xBF23, go->ipb ? LAMBDA_table[1][lambda] : 32767,
  873. 0xBF24, 32767,
  874. 0xBF25, lambda > 99 ? 32767 : LAMBDA_table[3][lambda],
  875. 0xBF26, sgop_expt_addr & 0x0000FFFF,
  876. 0xBF27, sgop_expt_addr >> 16,
  877. 0xBF28, sgop_peak_addr & 0x0000FFFF,
  878. 0xBF29, sgop_peak_addr >> 16,
  879. 0xBF2A, vbv_alert_addr & 0x0000FFFF,
  880. 0xBF2B, vbv_alert_addr >> 16,
  881. 0xBF2C, 0,
  882. 0xBF2D, 0,
  883. 0, 0,
  884. 0x200e, 0x0000,
  885. 0xBF2E, vbv_alert_addr & 0x0000FFFF,
  886. 0xBF2F, vbv_alert_addr >> 16,
  887. 0xBF30, cplx[0] & 0x0000FFFF,
  888. 0xBF31, cplx[0] >> 16,
  889. 0xBF32, cplx[1] & 0x0000FFFF,
  890. 0xBF33, cplx[1] >> 16,
  891. 0xBF34, cplx[2] & 0x0000FFFF,
  892. 0xBF35, cplx[2] >> 16,
  893. 0xBF36, cplx[3] & 0x0000FFFF,
  894. 0xBF37, cplx[3] >> 16,
  895. 0xBF38, 0,
  896. 0xBF39, 0,
  897. 0xBF3A, total_expt_addr & 0x0000FFFF,
  898. 0xBF3B, total_expt_addr >> 16,
  899. 0, 0,
  900. 0x200e, 0x0000,
  901. 0xBF3C, total_expt_addr & 0x0000FFFF,
  902. 0xBF3D, total_expt_addr >> 16,
  903. 0xBF3E, 0,
  904. 0xBF3F, 0,
  905. 0xBF48, 0,
  906. 0xBF49, 0,
  907. 0xBF4A, calc_q < 4 ? 4 : (calc_q > 124 ? 124 : calc_q),
  908. 0xBF4B, 4,
  909. 0xBF4C, 0,
  910. 0xBF4D, 0,
  911. 0xBF4E, 0,
  912. 0xBF4F, 0,
  913. 0xBF50, 0,
  914. 0xBF51, 0,
  915. 0, 0,
  916. 0x200e, 0x0000,
  917. 0xBF40, sgop_expt_addr & 0x0000FFFF,
  918. 0xBF41, sgop_expt_addr >> 16,
  919. 0xBF42, 0,
  920. 0xBF43, 0,
  921. 0xBF44, 0,
  922. 0xBF45, 0,
  923. 0xBF46, (go->width >> 4) * (go->height >> 4),
  924. 0xBF47, 0,
  925. 0xBF64, 0,
  926. 0xBF65, 0,
  927. 0xBF18, framelen[4],
  928. 0xBF19, framelen[5],
  929. 0xBF1A, framelen[6],
  930. 0xBF1B, framelen[7],
  931. 0, 0,
  932. #if 0
  933. /* Remove once we don't care about matching */
  934. 0x200e, 0x0000,
  935. 0xBF56, 4,
  936. 0xBF57, 0,
  937. 0xBF58, 5,
  938. 0xBF59, 0,
  939. 0xBF5A, 6,
  940. 0xBF5B, 0,
  941. 0xBF5C, 8,
  942. 0xBF5D, 0,
  943. 0xBF5E, 1,
  944. 0xBF5F, 0,
  945. 0xBF60, 1,
  946. 0xBF61, 0,
  947. 0xBF62, 0,
  948. 0xBF63, 0,
  949. 0, 0,
  950. #else
  951. 0x2008, 0x0000,
  952. 0xBF56, 4,
  953. 0xBF57, 0,
  954. 0xBF58, 5,
  955. 0xBF59, 0,
  956. 0xBF5A, 6,
  957. 0xBF5B, 0,
  958. 0xBF5C, 8,
  959. 0xBF5D, 0,
  960. 0, 0,
  961. 0, 0,
  962. 0, 0,
  963. 0, 0,
  964. 0, 0,
  965. 0, 0,
  966. 0, 0,
  967. #endif
  968. 0x200e, 0x0000,
  969. 0xBF10, 0,
  970. 0xBF11, 0,
  971. 0xBF12, 0,
  972. 0xBF13, 0,
  973. 0xBF14, 0,
  974. 0xBF15, 0,
  975. 0xBF16, 0,
  976. 0xBF17, 0,
  977. 0xBF7E, 0,
  978. 0xBF7F, 1,
  979. 0xBF52, framelen[0],
  980. 0xBF53, framelen[1],
  981. 0xBF54, framelen[2],
  982. 0xBF55, framelen[3],
  983. 0, 0,
  984. };
  985. return copy_packages(code, pack, 6, space);
  986. }
  987. static int config_package(struct go7007 *go, __le16 *code, int space)
  988. {
  989. int fps = go->sensor_framerate / go->fps_scale / 1000;
  990. int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
  991. int brc_window_size = fps;
  992. int q_min = 2, q_max = 31;
  993. int THACCoeffSet0 = 0;
  994. u16 pack[] = {
  995. 0x200e, 0x0000,
  996. 0xc002, 0x14b4,
  997. 0xc003, 0x28b4,
  998. 0xc004, 0x3c5a,
  999. 0xdc05, 0x2a77,
  1000. 0xc6c3, go->format == V4L2_PIX_FMT_MPEG4 ? 0 :
  1001. (go->format == V4L2_PIX_FMT_H263 ? 0 : 1),
  1002. 0xc680, go->format == V4L2_PIX_FMT_MPEG4 ? 0xf1 :
  1003. (go->format == V4L2_PIX_FMT_H263 ? 0x61 :
  1004. 0xd3),
  1005. 0xc780, 0x0140,
  1006. 0xe009, 0x0001,
  1007. 0xc60f, 0x0008,
  1008. 0xd4ff, 0x0002,
  1009. 0xe403, 2340,
  1010. 0xe406, 75,
  1011. 0xd411, 0x0001,
  1012. 0xd410, 0xa1d6,
  1013. 0x0001, 0x2801,
  1014. 0x200d, 0x0000,
  1015. 0xe402, 0x018b,
  1016. 0xe401, 0x8b01,
  1017. 0xd472, (go->board_info->sensor_flags &
  1018. GO7007_SENSOR_TV) &&
  1019. (!go->interlace_coding) ?
  1020. 0x01b0 : 0x0170,
  1021. 0xd475, (go->board_info->sensor_flags &
  1022. GO7007_SENSOR_TV) &&
  1023. (!go->interlace_coding) ?
  1024. 0x0008 : 0x0009,
  1025. 0xc404, go->interlace_coding ? 0x44 :
  1026. (go->format == V4L2_PIX_FMT_MPEG4 ? 0x11 :
  1027. (go->format == V4L2_PIX_FMT_MPEG1 ? 0x02 :
  1028. (go->format == V4L2_PIX_FMT_MPEG2 ? 0x04 :
  1029. (go->format == V4L2_PIX_FMT_H263 ? 0x08 :
  1030. 0x20)))),
  1031. 0xbf0a, (go->format == V4L2_PIX_FMT_MPEG4 ? 8 :
  1032. (go->format == V4L2_PIX_FMT_MPEG1 ? 1 :
  1033. (go->format == V4L2_PIX_FMT_MPEG2 ? 2 :
  1034. (go->format == V4L2_PIX_FMT_H263 ? 4 : 16)))) |
  1035. ((go->repeat_seqhead ? 1 : 0) << 6) |
  1036. ((go->dvd_mode ? 1 : 0) << 9) |
  1037. ((go->gop_header_enable ? 1 : 0) << 10),
  1038. 0xbf0b, 0,
  1039. 0xdd5a, go->ipb ? 0x14 : 0x0a,
  1040. 0xbf0c, 0,
  1041. 0xbf0d, 0,
  1042. 0xc683, THACCoeffSet0,
  1043. 0xc40a, (go->width << 4) | rows,
  1044. 0xe01a, go->board_info->hpi_buffer_cap,
  1045. 0, 0,
  1046. 0, 0,
  1047. 0x2008, 0,
  1048. 0xe402, 0x88,
  1049. 0xe401, 0x8f01,
  1050. 0xbf6a, 0,
  1051. 0xbf6b, 0,
  1052. 0xbf6c, 0,
  1053. 0xbf6d, 0,
  1054. 0xbf6e, 0,
  1055. 0xbf6f, 0,
  1056. 0, 0,
  1057. 0, 0,
  1058. 0, 0,
  1059. 0, 0,
  1060. 0, 0,
  1061. 0, 0,
  1062. 0, 0,
  1063. 0x200e, 0,
  1064. 0xbf66, brc_window_size,
  1065. 0xbf67, 0,
  1066. 0xbf68, q_min,
  1067. 0xbf69, q_max,
  1068. 0xbfe0, 0,
  1069. 0xbfe1, 0,
  1070. 0xbfe2, 0,
  1071. 0xbfe3, go->ipb ? 3 : 1,
  1072. 0xc031, go->board_info->sensor_flags &
  1073. GO7007_SENSOR_VBI ? 1 : 0,
  1074. 0xc01c, 0x1f,
  1075. 0xdd8c, 0x15,
  1076. 0xdd94, 0x15,
  1077. 0xdd88, go->ipb ? 0x1401 : 0x0a01,
  1078. 0xdd90, go->ipb ? 0x1401 : 0x0a01,
  1079. 0, 0,
  1080. 0x200e, 0,
  1081. 0xbfe4, 0,
  1082. 0xbfe5, 0,
  1083. 0xbfe6, 0,
  1084. 0xbfe7, fps << 8,
  1085. 0xbfe8, 0x3a00,
  1086. 0xbfe9, 0,
  1087. 0xbfea, 0,
  1088. 0xbfeb, 0,
  1089. 0xbfec, (go->interlace_coding ? 1 << 15 : 0) |
  1090. (go->modet_enable ? 0xa : 0) |
  1091. (go->board_info->sensor_flags &
  1092. GO7007_SENSOR_VBI ? 1 : 0),
  1093. 0xbfed, 0,
  1094. 0xbfee, 0,
  1095. 0xbfef, 0,
  1096. 0xbff0, go->board_info->sensor_flags &
  1097. GO7007_SENSOR_TV ? 0xf060 : 0xb060,
  1098. 0xbff1, 0,
  1099. 0, 0,
  1100. };
  1101. return copy_packages(code, pack, 5, space);
  1102. }
  1103. static int seqhead_to_package(struct go7007 *go, __le16 *code, int space,
  1104. int (*sequence_header_func)(struct go7007 *go,
  1105. unsigned char *buf, int ext))
  1106. {
  1107. int vop_time_increment_bitlength = vti_bitlen(go);
  1108. int fps = go->sensor_framerate / go->fps_scale *
  1109. (go->interlace_coding ? 2 : 1);
  1110. unsigned char buf[40] = { };
  1111. int len = sequence_header_func(go, buf, 1);
  1112. u16 pack[] = {
  1113. 0x2006, 0,
  1114. 0xbf08, fps,
  1115. 0xbf09, 0,
  1116. 0xbff2, vop_time_increment_bitlength,
  1117. 0xbff3, (1 << vop_time_increment_bitlength) - 1,
  1118. 0xbfe6, 0,
  1119. 0xbfe7, (fps / 1000) << 8,
  1120. 0, 0,
  1121. 0, 0,
  1122. 0, 0,
  1123. 0, 0,
  1124. 0, 0,
  1125. 0, 0,
  1126. 0, 0,
  1127. 0, 0,
  1128. 0, 0,
  1129. 0x2007, 0,
  1130. 0xc800, buf[2] << 8 | buf[3],
  1131. 0xc801, buf[4] << 8 | buf[5],
  1132. 0xc802, buf[6] << 8 | buf[7],
  1133. 0xc803, buf[8] << 8 | buf[9],
  1134. 0xc406, 64,
  1135. 0xc407, len - 64,
  1136. 0xc61b, 1,
  1137. 0, 0,
  1138. 0, 0,
  1139. 0, 0,
  1140. 0, 0,
  1141. 0, 0,
  1142. 0, 0,
  1143. 0, 0,
  1144. 0, 0,
  1145. 0x200e, 0,
  1146. 0xc808, buf[10] << 8 | buf[11],
  1147. 0xc809, buf[12] << 8 | buf[13],
  1148. 0xc80a, buf[14] << 8 | buf[15],
  1149. 0xc80b, buf[16] << 8 | buf[17],
  1150. 0xc80c, buf[18] << 8 | buf[19],
  1151. 0xc80d, buf[20] << 8 | buf[21],
  1152. 0xc80e, buf[22] << 8 | buf[23],
  1153. 0xc80f, buf[24] << 8 | buf[25],
  1154. 0xc810, buf[26] << 8 | buf[27],
  1155. 0xc811, buf[28] << 8 | buf[29],
  1156. 0xc812, buf[30] << 8 | buf[31],
  1157. 0xc813, buf[32] << 8 | buf[33],
  1158. 0xc814, buf[34] << 8 | buf[35],
  1159. 0xc815, buf[36] << 8 | buf[37],
  1160. 0, 0,
  1161. 0, 0,
  1162. 0, 0,
  1163. };
  1164. return copy_packages(code, pack, 3, space);
  1165. }
  1166. static int relative_prime(int big, int little)
  1167. {
  1168. int remainder;
  1169. while (little != 0) {
  1170. remainder = big % little;
  1171. big = little;
  1172. little = remainder;
  1173. }
  1174. return big;
  1175. }
  1176. static int avsync_to_package(struct go7007 *go, __le16 *code, int space)
  1177. {
  1178. int arate = go->board_info->audio_rate * 1001 * go->fps_scale;
  1179. int ratio = arate / go->sensor_framerate;
  1180. int adjratio = ratio * 215 / 100;
  1181. int rprime = relative_prime(go->sensor_framerate,
  1182. arate % go->sensor_framerate);
  1183. int f1 = (arate % go->sensor_framerate) / rprime;
  1184. int f2 = (go->sensor_framerate - arate % go->sensor_framerate) / rprime;
  1185. u16 pack[] = {
  1186. 0x200e, 0,
  1187. 0xbf98, (u16)((-adjratio) & 0xffff),
  1188. 0xbf99, (u16)((-adjratio) >> 16),
  1189. 0xbf92, 0,
  1190. 0xbf93, 0,
  1191. 0xbff4, f1 > f2 ? f1 : f2,
  1192. 0xbff5, f1 < f2 ? f1 : f2,
  1193. 0xbff6, f1 < f2 ? ratio : ratio + 1,
  1194. 0xbff7, f1 > f2 ? ratio : ratio + 1,
  1195. 0xbff8, 0,
  1196. 0xbff9, 0,
  1197. 0xbffa, adjratio & 0xffff,
  1198. 0xbffb, adjratio >> 16,
  1199. 0xbf94, 0,
  1200. 0xbf95, 0,
  1201. 0, 0,
  1202. };
  1203. return copy_packages(code, pack, 1, space);
  1204. }
  1205. static int final_package(struct go7007 *go, __le16 *code, int space)
  1206. {
  1207. int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
  1208. u16 pack[] = {
  1209. 0x8000,
  1210. 0,
  1211. 0,
  1212. 0,
  1213. 0,
  1214. 0,
  1215. 0,
  1216. 2,
  1217. ((go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
  1218. (!go->interlace_coding) ?
  1219. (1 << 14) | (1 << 9) : 0) |
  1220. ((go->encoder_subsample ? 1 : 0) << 8) |
  1221. (go->board_info->sensor_flags &
  1222. GO7007_SENSOR_CONFIG_MASK),
  1223. ((go->encoder_v_halve ? 1 : 0) << 14) |
  1224. (go->encoder_v_halve ? rows << 9 : rows << 8) |
  1225. (go->encoder_h_halve ? 1 << 6 : 0) |
  1226. (go->encoder_h_halve ? go->width >> 3 : go->width >> 4),
  1227. (1 << 15) | (go->encoder_v_offset << 6) |
  1228. (1 << 7) | (go->encoder_h_offset >> 2),
  1229. (1 << 6),
  1230. 0,
  1231. 0,
  1232. ((go->fps_scale - 1) << 8) |
  1233. (go->board_info->sensor_flags & GO7007_SENSOR_TV ?
  1234. (1 << 7) : 0) |
  1235. 0x41,
  1236. go->ipb ? 0xd4c : 0x36b,
  1237. (rows << 8) | (go->width >> 4),
  1238. go->format == V4L2_PIX_FMT_MPEG4 ? 0x0404 : 0,
  1239. (1 << 15) | ((go->interlace_coding ? 1 : 0) << 13) |
  1240. ((go->closed_gop ? 1 : 0) << 12) |
  1241. ((go->format == V4L2_PIX_FMT_MPEG4 ? 1 : 0) << 11) |
  1242. /* (1 << 9) | */
  1243. ((go->ipb ? 3 : 0) << 7) |
  1244. ((go->modet_enable ? 1 : 0) << 2) |
  1245. ((go->dvd_mode ? 1 : 0) << 1) | 1,
  1246. (go->format == V4L2_PIX_FMT_MPEG1 ? 0x89a0 :
  1247. (go->format == V4L2_PIX_FMT_MPEG2 ? 0x89a0 :
  1248. (go->format == V4L2_PIX_FMT_MJPEG ? 0x89a0 :
  1249. (go->format == V4L2_PIX_FMT_MPEG4 ? 0x8920 :
  1250. (go->format == V4L2_PIX_FMT_H263 ? 0x8920 : 0))))),
  1251. go->ipb ? 0x1f15 : 0x1f0b,
  1252. go->ipb ? 0x0015 : 0x000b,
  1253. go->ipb ? 0xa800 : 0x5800,
  1254. 0xffff,
  1255. 0x0020 + 0x034b * 0,
  1256. 0x0020 + 0x034b * 1,
  1257. 0x0020 + 0x034b * 2,
  1258. 0x0020 + 0x034b * 3,
  1259. 0x0020 + 0x034b * 4,
  1260. 0x0020 + 0x034b * 5,
  1261. go->ipb ? (go->gop_size / 3) : go->gop_size,
  1262. (go->height >> 4) * (go->width >> 4) * 110 / 100,
  1263. };
  1264. return copy_packages(code, pack, 1, space);
  1265. }
  1266. static int audio_to_package(struct go7007 *go, __le16 *code, int space)
  1267. {
  1268. int clock_config = ((go->board_info->audio_flags &
  1269. GO7007_AUDIO_I2S_MASTER ? 1 : 0) << 11) |
  1270. ((go->board_info->audio_flags &
  1271. GO7007_AUDIO_OKI_MODE ? 1 : 0) << 8) |
  1272. (((go->board_info->audio_bclk_div / 4) - 1) << 4) |
  1273. (go->board_info->audio_main_div - 1);
  1274. u16 pack[] = {
  1275. 0x200d, 0,
  1276. 0x9002, 0,
  1277. 0x9002, 0,
  1278. 0x9031, 0,
  1279. 0x9032, 0,
  1280. 0x9033, 0,
  1281. 0x9034, 0,
  1282. 0x9035, 0,
  1283. 0x9036, 0,
  1284. 0x9037, 0,
  1285. 0x9040, 0,
  1286. 0x9000, clock_config,
  1287. 0x9001, (go->board_info->audio_flags & 0xffff) |
  1288. (1 << 9),
  1289. 0x9000, ((go->board_info->audio_flags &
  1290. GO7007_AUDIO_I2S_MASTER ?
  1291. 1 : 0) << 10) |
  1292. clock_config,
  1293. 0, 0,
  1294. 0, 0,
  1295. 0x2005, 0,
  1296. 0x9041, 0,
  1297. 0x9042, 256,
  1298. 0x9043, 0,
  1299. 0x9044, 16,
  1300. 0x9045, 16,
  1301. 0, 0,
  1302. 0, 0,
  1303. 0, 0,
  1304. 0, 0,
  1305. 0, 0,
  1306. 0, 0,
  1307. 0, 0,
  1308. 0, 0,
  1309. 0, 0,
  1310. 0, 0,
  1311. };
  1312. return copy_packages(code, pack, 2, space);
  1313. }
  1314. static int modet_to_package(struct go7007 *go, __le16 *code, int space)
  1315. {
  1316. bool has_modet0 = go->modet[0].enable;
  1317. bool has_modet1 = go->modet[1].enable;
  1318. bool has_modet2 = go->modet[2].enable;
  1319. bool has_modet3 = go->modet[3].enable;
  1320. int ret, mb, i, addr, cnt = 0;
  1321. u16 pack[32];
  1322. u16 thresholds[] = {
  1323. 0x200e, 0,
  1324. 0xbf82, has_modet0 ? go->modet[0].pixel_threshold : 32767,
  1325. 0xbf83, has_modet1 ? go->modet[1].pixel_threshold : 32767,
  1326. 0xbf84, has_modet2 ? go->modet[2].pixel_threshold : 32767,
  1327. 0xbf85, has_modet3 ? go->modet[3].pixel_threshold : 32767,
  1328. 0xbf86, has_modet0 ? go->modet[0].motion_threshold : 32767,
  1329. 0xbf87, has_modet1 ? go->modet[1].motion_threshold : 32767,
  1330. 0xbf88, has_modet2 ? go->modet[2].motion_threshold : 32767,
  1331. 0xbf89, has_modet3 ? go->modet[3].motion_threshold : 32767,
  1332. 0xbf8a, has_modet0 ? go->modet[0].mb_threshold : 32767,
  1333. 0xbf8b, has_modet1 ? go->modet[1].mb_threshold : 32767,
  1334. 0xbf8c, has_modet2 ? go->modet[2].mb_threshold : 32767,
  1335. 0xbf8d, has_modet3 ? go->modet[3].mb_threshold : 32767,
  1336. 0xbf8e, 0,
  1337. 0xbf8f, 0,
  1338. 0, 0,
  1339. };
  1340. ret = copy_packages(code, thresholds, 1, space);
  1341. if (ret < 0)
  1342. return -1;
  1343. cnt += ret;
  1344. addr = 0xbac0;
  1345. memset(pack, 0, 64);
  1346. i = 0;
  1347. for (mb = 0; mb < 1624; ++mb) {
  1348. pack[i * 2 + 3] <<= 2;
  1349. pack[i * 2 + 3] |= go->modet_map[mb];
  1350. if (mb % 8 != 7)
  1351. continue;
  1352. pack[i * 2 + 2] = addr++;
  1353. ++i;
  1354. if (i == 10 || mb == 1623) {
  1355. pack[0] = 0x2000 | i;
  1356. ret = copy_packages(code + cnt, pack, 1, space - cnt);
  1357. if (ret < 0)
  1358. return -1;
  1359. cnt += ret;
  1360. i = 0;
  1361. memset(pack, 0, 64);
  1362. }
  1363. pack[i * 2 + 3] = 0;
  1364. }
  1365. memset(pack, 0, 64);
  1366. i = 0;
  1367. for (addr = 0xbb90; addr < 0xbbfa; ++addr) {
  1368. pack[i * 2 + 2] = addr;
  1369. pack[i * 2 + 3] = 0;
  1370. ++i;
  1371. if (i == 10 || addr == 0xbbf9) {
  1372. pack[0] = 0x2000 | i;
  1373. ret = copy_packages(code + cnt, pack, 1, space - cnt);
  1374. if (ret < 0)
  1375. return -1;
  1376. cnt += ret;
  1377. i = 0;
  1378. memset(pack, 0, 64);
  1379. }
  1380. }
  1381. return cnt;
  1382. }
  1383. static noinline_for_stack int do_special(struct go7007 *go, u16 type,
  1384. __le16 *code, int space, int *framelen)
  1385. {
  1386. switch (type) {
  1387. case SPECIAL_FRM_HEAD:
  1388. switch (go->format) {
  1389. case V4L2_PIX_FMT_MJPEG:
  1390. return gen_mjpeghdr_to_package(go, code, space);
  1391. case V4L2_PIX_FMT_MPEG1:
  1392. case V4L2_PIX_FMT_MPEG2:
  1393. return gen_mpeg1hdr_to_package(go, code, space,
  1394. framelen);
  1395. case V4L2_PIX_FMT_MPEG4:
  1396. return gen_mpeg4hdr_to_package(go, code, space,
  1397. framelen);
  1398. default:
  1399. break;
  1400. }
  1401. break;
  1402. case SPECIAL_BRC_CTRL:
  1403. return brctrl_to_package(go, code, space, framelen);
  1404. case SPECIAL_CONFIG:
  1405. return config_package(go, code, space);
  1406. case SPECIAL_SEQHEAD:
  1407. switch (go->format) {
  1408. case V4L2_PIX_FMT_MPEG1:
  1409. case V4L2_PIX_FMT_MPEG2:
  1410. return seqhead_to_package(go, code, space,
  1411. mpeg1_sequence_header);
  1412. case V4L2_PIX_FMT_MPEG4:
  1413. return seqhead_to_package(go, code, space,
  1414. mpeg4_sequence_header);
  1415. default:
  1416. return 0;
  1417. }
  1418. case SPECIAL_AV_SYNC:
  1419. return avsync_to_package(go, code, space);
  1420. case SPECIAL_FINAL:
  1421. return final_package(go, code, space);
  1422. case SPECIAL_AUDIO:
  1423. return audio_to_package(go, code, space);
  1424. case SPECIAL_MODET:
  1425. return modet_to_package(go, code, space);
  1426. }
  1427. dev_err(go->dev,
  1428. "firmware file contains unsupported feature %04x\n", type);
  1429. return -1;
  1430. }
  1431. int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen)
  1432. {
  1433. const struct firmware *fw_entry;
  1434. __le16 *code, *src;
  1435. int framelen[8] = { }; /* holds the lengths of empty frame templates */
  1436. int codespace = 64 * 1024, i = 0, srclen, chunk_len, chunk_flags;
  1437. int mode_flag;
  1438. int ret;
  1439. switch (go->format) {
  1440. case V4L2_PIX_FMT_MJPEG:
  1441. mode_flag = FLAG_MODE_MJPEG;
  1442. break;
  1443. case V4L2_PIX_FMT_MPEG1:
  1444. mode_flag = FLAG_MODE_MPEG1;
  1445. break;
  1446. case V4L2_PIX_FMT_MPEG2:
  1447. mode_flag = FLAG_MODE_MPEG2;
  1448. break;
  1449. case V4L2_PIX_FMT_MPEG4:
  1450. mode_flag = FLAG_MODE_MPEG4;
  1451. break;
  1452. default:
  1453. return -1;
  1454. }
  1455. if (request_firmware(&fw_entry, GO7007_FW_NAME, go->dev))
  1456. return -1;
  1457. code = kcalloc(codespace, 2, GFP_KERNEL);
  1458. if (code == NULL)
  1459. goto fw_failed;
  1460. src = (__le16 *)fw_entry->data;
  1461. srclen = fw_entry->size / 2;
  1462. while (srclen >= 2) {
  1463. chunk_flags = __le16_to_cpu(src[0]);
  1464. chunk_len = __le16_to_cpu(src[1]);
  1465. if (chunk_len + 2 > srclen) {
  1466. dev_err(go->dev,
  1467. "firmware file \"%s\" appears to be corrupted\n",
  1468. GO7007_FW_NAME);
  1469. goto fw_failed;
  1470. }
  1471. if (chunk_flags & mode_flag) {
  1472. if (chunk_flags & FLAG_SPECIAL) {
  1473. ret = do_special(go, __le16_to_cpu(src[2]),
  1474. &code[i], codespace - i, framelen);
  1475. if (ret < 0) {
  1476. dev_err(go->dev,
  1477. "insufficient memory for firmware construction\n");
  1478. goto fw_failed;
  1479. }
  1480. i += ret;
  1481. } else {
  1482. if (codespace - i < chunk_len) {
  1483. dev_err(go->dev,
  1484. "insufficient memory for firmware construction\n");
  1485. goto fw_failed;
  1486. }
  1487. memcpy(&code[i], &src[2], chunk_len * 2);
  1488. i += chunk_len;
  1489. }
  1490. }
  1491. srclen -= chunk_len + 2;
  1492. src += chunk_len + 2;
  1493. }
  1494. release_firmware(fw_entry);
  1495. *fw = (u8 *)code;
  1496. *fwlen = i * 2;
  1497. return 0;
  1498. fw_failed:
  1499. kfree(code);
  1500. release_firmware(fw_entry);
  1501. return -1;
  1502. }
  1503. MODULE_FIRMWARE(GO7007_FW_NAME);