go7007-fw.c 40 KB

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