cx231xx-video.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768
  1. /*
  2. cx231xx-video.c - driver for Conexant Cx23100/101/102
  3. USB video capture devices
  4. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  5. Based on em28xx driver
  6. Based on cx23885 driver
  7. Based on cx88 driver
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/usb.h>
  26. #include <linux/i2c.h>
  27. #include <linux/version.h>
  28. #include <linux/mm.h>
  29. #include <linux/mutex.h>
  30. #include <linux/slab.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/msp3400.h>
  35. #include <media/tuner.h>
  36. #include "dvb_frontend.h"
  37. #include "cx231xx.h"
  38. #include "cx231xx-vbi.h"
  39. #define CX231XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
  40. #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
  41. #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
  42. #define cx231xx_videodbg(fmt, arg...) do {\
  43. if (video_debug) \
  44. printk(KERN_INFO "%s %s :"fmt, \
  45. dev->name, __func__ , ##arg); } while (0)
  46. static unsigned int isoc_debug;
  47. module_param(isoc_debug, int, 0644);
  48. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  49. #define cx231xx_isocdbg(fmt, arg...) \
  50. do {\
  51. if (isoc_debug) { \
  52. printk(KERN_INFO "%s %s :"fmt, \
  53. dev->name, __func__ , ##arg); \
  54. } \
  55. } while (0)
  56. MODULE_AUTHOR(DRIVER_AUTHOR);
  57. MODULE_DESCRIPTION(DRIVER_DESC);
  58. MODULE_LICENSE("GPL");
  59. static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  60. static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  61. static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  63. module_param_array(card, int, NULL, 0444);
  64. module_param_array(video_nr, int, NULL, 0444);
  65. module_param_array(vbi_nr, int, NULL, 0444);
  66. module_param_array(radio_nr, int, NULL, 0444);
  67. MODULE_PARM_DESC(card, "card type");
  68. MODULE_PARM_DESC(video_nr, "video device numbers");
  69. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  70. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  71. static unsigned int video_debug;
  72. module_param(video_debug, int, 0644);
  73. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  74. /* supported video standards */
  75. static struct cx231xx_fmt format[] = {
  76. {
  77. .name = "16bpp YUY2, 4:2:2, packed",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. .reg = 0,
  81. },
  82. };
  83. /* supported controls */
  84. /* Common to all boards */
  85. /* ------------------------------------------------------------------- */
  86. static const struct v4l2_queryctrl no_ctl = {
  87. .name = "42",
  88. .flags = V4L2_CTRL_FLAG_DISABLED,
  89. };
  90. static struct cx231xx_ctrl cx231xx_ctls[] = {
  91. /* --- video --- */
  92. {
  93. .v = {
  94. .id = V4L2_CID_BRIGHTNESS,
  95. .name = "Brightness",
  96. .minimum = 0x00,
  97. .maximum = 0xff,
  98. .step = 1,
  99. .default_value = 0x7f,
  100. .type = V4L2_CTRL_TYPE_INTEGER,
  101. },
  102. .off = 128,
  103. .reg = LUMA_CTRL,
  104. .mask = 0x00ff,
  105. .shift = 0,
  106. }, {
  107. .v = {
  108. .id = V4L2_CID_CONTRAST,
  109. .name = "Contrast",
  110. .minimum = 0,
  111. .maximum = 0xff,
  112. .step = 1,
  113. .default_value = 0x3f,
  114. .type = V4L2_CTRL_TYPE_INTEGER,
  115. },
  116. .off = 0,
  117. .reg = LUMA_CTRL,
  118. .mask = 0xff00,
  119. .shift = 8,
  120. }, {
  121. .v = {
  122. .id = V4L2_CID_HUE,
  123. .name = "Hue",
  124. .minimum = 0,
  125. .maximum = 0xff,
  126. .step = 1,
  127. .default_value = 0x7f,
  128. .type = V4L2_CTRL_TYPE_INTEGER,
  129. },
  130. .off = 128,
  131. .reg = CHROMA_CTRL,
  132. .mask = 0xff0000,
  133. .shift = 16,
  134. }, {
  135. /* strictly, this only describes only U saturation.
  136. * V saturation is handled specially through code.
  137. */
  138. .v = {
  139. .id = V4L2_CID_SATURATION,
  140. .name = "Saturation",
  141. .minimum = 0,
  142. .maximum = 0xff,
  143. .step = 1,
  144. .default_value = 0x7f,
  145. .type = V4L2_CTRL_TYPE_INTEGER,
  146. },
  147. .off = 0,
  148. .reg = CHROMA_CTRL,
  149. .mask = 0x00ff,
  150. .shift = 0,
  151. }, {
  152. /* --- audio --- */
  153. .v = {
  154. .id = V4L2_CID_AUDIO_MUTE,
  155. .name = "Mute",
  156. .minimum = 0,
  157. .maximum = 1,
  158. .default_value = 1,
  159. .type = V4L2_CTRL_TYPE_BOOLEAN,
  160. },
  161. .reg = PATH1_CTL1,
  162. .mask = (0x1f << 24),
  163. .shift = 24,
  164. }, {
  165. .v = {
  166. .id = V4L2_CID_AUDIO_VOLUME,
  167. .name = "Volume",
  168. .minimum = 0,
  169. .maximum = 0x3f,
  170. .step = 1,
  171. .default_value = 0x3f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .reg = PATH1_VOL_CTL,
  175. .mask = 0xff,
  176. .shift = 0,
  177. }
  178. };
  179. static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
  180. static const u32 cx231xx_user_ctrls[] = {
  181. V4L2_CID_USER_CLASS,
  182. V4L2_CID_BRIGHTNESS,
  183. V4L2_CID_CONTRAST,
  184. V4L2_CID_SATURATION,
  185. V4L2_CID_HUE,
  186. V4L2_CID_AUDIO_VOLUME,
  187. #if 0
  188. V4L2_CID_AUDIO_BALANCE,
  189. #endif
  190. V4L2_CID_AUDIO_MUTE,
  191. 0
  192. };
  193. static const u32 *ctrl_classes[] = {
  194. cx231xx_user_ctrls,
  195. NULL
  196. };
  197. /* ------------------------------------------------------------------
  198. Video buffer and parser functions
  199. ------------------------------------------------------------------*/
  200. /*
  201. * Announces that a buffer were filled and request the next
  202. */
  203. static inline void buffer_filled(struct cx231xx *dev,
  204. struct cx231xx_dmaqueue *dma_q,
  205. struct cx231xx_buffer *buf)
  206. {
  207. /* Advice that buffer was filled */
  208. cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  209. buf->vb.state = VIDEOBUF_DONE;
  210. buf->vb.field_count++;
  211. do_gettimeofday(&buf->vb.ts);
  212. if (dev->USE_ISO)
  213. dev->video_mode.isoc_ctl.buf = NULL;
  214. else
  215. dev->video_mode.bulk_ctl.buf = NULL;
  216. list_del(&buf->vb.queue);
  217. wake_up(&buf->vb.done);
  218. }
  219. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  220. {
  221. char *errmsg = "Unknown";
  222. switch (status) {
  223. case -ENOENT:
  224. errmsg = "unlinked synchronuously";
  225. break;
  226. case -ECONNRESET:
  227. errmsg = "unlinked asynchronuously";
  228. break;
  229. case -ENOSR:
  230. errmsg = "Buffer error (overrun)";
  231. break;
  232. case -EPIPE:
  233. errmsg = "Stalled (device not responding)";
  234. break;
  235. case -EOVERFLOW:
  236. errmsg = "Babble (bad cable?)";
  237. break;
  238. case -EPROTO:
  239. errmsg = "Bit-stuff error (bad cable?)";
  240. break;
  241. case -EILSEQ:
  242. errmsg = "CRC/Timeout (could be anything)";
  243. break;
  244. case -ETIME:
  245. errmsg = "Device does not respond";
  246. break;
  247. }
  248. if (packet < 0) {
  249. cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  250. } else {
  251. cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
  252. packet, status, errmsg);
  253. }
  254. }
  255. /*
  256. * video-buf generic routine to get the next available buffer
  257. */
  258. static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
  259. struct cx231xx_buffer **buf)
  260. {
  261. struct cx231xx_video_mode *vmode =
  262. container_of(dma_q, struct cx231xx_video_mode, vidq);
  263. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  264. char *outp;
  265. if (list_empty(&dma_q->active)) {
  266. cx231xx_isocdbg("No active queue to serve\n");
  267. if (dev->USE_ISO)
  268. dev->video_mode.isoc_ctl.buf = NULL;
  269. else
  270. dev->video_mode.bulk_ctl.buf = NULL;
  271. *buf = NULL;
  272. return;
  273. }
  274. /* Get the next buffer */
  275. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  276. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  277. outp = videobuf_to_vmalloc(&(*buf)->vb);
  278. memset(outp, 0, (*buf)->vb.size);
  279. if (dev->USE_ISO)
  280. dev->video_mode.isoc_ctl.buf = *buf;
  281. else
  282. dev->video_mode.bulk_ctl.buf = *buf;
  283. return;
  284. }
  285. /*
  286. * Controls the isoc copy of each urb packet
  287. */
  288. static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  289. {
  290. struct cx231xx_buffer *buf;
  291. struct cx231xx_dmaqueue *dma_q = urb->context;
  292. unsigned char *outp = NULL;
  293. int i, rc = 1;
  294. unsigned char *p_buffer;
  295. u32 bytes_parsed = 0, buffer_size = 0;
  296. u8 sav_eav = 0;
  297. if (!dev)
  298. return 0;
  299. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  300. return 0;
  301. if (urb->status < 0) {
  302. print_err_status(dev, -1, urb->status);
  303. if (urb->status == -ENOENT)
  304. return 0;
  305. }
  306. buf = dev->video_mode.isoc_ctl.buf;
  307. if (buf != NULL)
  308. outp = videobuf_to_vmalloc(&buf->vb);
  309. for (i = 0; i < urb->number_of_packets; i++) {
  310. int status = urb->iso_frame_desc[i].status;
  311. if (status < 0) {
  312. print_err_status(dev, i, status);
  313. if (urb->iso_frame_desc[i].status != -EPROTO)
  314. continue;
  315. }
  316. if (urb->iso_frame_desc[i].actual_length <= 0) {
  317. /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
  318. continue;
  319. }
  320. if (urb->iso_frame_desc[i].actual_length >
  321. dev->video_mode.max_pkt_size) {
  322. cx231xx_isocdbg("packet bigger than packet size");
  323. continue;
  324. }
  325. /* get buffer pointer and length */
  326. p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  327. buffer_size = urb->iso_frame_desc[i].actual_length;
  328. bytes_parsed = 0;
  329. if (dma_q->is_partial_line) {
  330. /* Handle the case of a partial line */
  331. sav_eav = dma_q->last_sav;
  332. } else {
  333. /* Check for a SAV/EAV overlapping
  334. the buffer boundary */
  335. sav_eav =
  336. cx231xx_find_boundary_SAV_EAV(p_buffer,
  337. dma_q->partial_buf,
  338. &bytes_parsed);
  339. }
  340. sav_eav &= 0xF0;
  341. /* Get the first line if we have some portion of an SAV/EAV from
  342. the last buffer or a partial line */
  343. if (sav_eav) {
  344. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  345. sav_eav, /* SAV/EAV */
  346. p_buffer + bytes_parsed, /* p_buffer */
  347. buffer_size - bytes_parsed);/* buf size */
  348. }
  349. /* Now parse data that is completely in this buffer */
  350. /* dma_q->is_partial_line = 0; */
  351. while (bytes_parsed < buffer_size) {
  352. u32 bytes_used = 0;
  353. sav_eav = cx231xx_find_next_SAV_EAV(
  354. p_buffer + bytes_parsed, /* p_buffer */
  355. buffer_size - bytes_parsed, /* buf size */
  356. &bytes_used);/* bytes used to get SAV/EAV */
  357. bytes_parsed += bytes_used;
  358. sav_eav &= 0xF0;
  359. if (sav_eav && (bytes_parsed < buffer_size)) {
  360. bytes_parsed += cx231xx_get_video_line(dev,
  361. dma_q, sav_eav, /* SAV/EAV */
  362. p_buffer + bytes_parsed,/* p_buffer */
  363. buffer_size - bytes_parsed);/*buf size*/
  364. }
  365. }
  366. /* Save the last four bytes of the buffer so we can check the
  367. buffer boundary condition next time */
  368. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  369. bytes_parsed = 0;
  370. }
  371. return rc;
  372. }
  373. static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
  374. {
  375. struct cx231xx_buffer *buf;
  376. struct cx231xx_dmaqueue *dma_q = urb->context;
  377. unsigned char *outp = NULL;
  378. int rc = 1;
  379. unsigned char *p_buffer;
  380. u32 bytes_parsed = 0, buffer_size = 0;
  381. u8 sav_eav = 0;
  382. if (!dev)
  383. return 0;
  384. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  385. return 0;
  386. if (urb->status < 0) {
  387. print_err_status(dev, -1, urb->status);
  388. if (urb->status == -ENOENT)
  389. return 0;
  390. }
  391. buf = dev->video_mode.bulk_ctl.buf;
  392. if (buf != NULL)
  393. outp = videobuf_to_vmalloc(&buf->vb);
  394. if (1) {
  395. /* get buffer pointer and length */
  396. p_buffer = urb->transfer_buffer;
  397. buffer_size = urb->actual_length;
  398. bytes_parsed = 0;
  399. if (dma_q->is_partial_line) {
  400. /* Handle the case of a partial line */
  401. sav_eav = dma_q->last_sav;
  402. } else {
  403. /* Check for a SAV/EAV overlapping
  404. the buffer boundary */
  405. sav_eav =
  406. cx231xx_find_boundary_SAV_EAV(p_buffer,
  407. dma_q->partial_buf,
  408. &bytes_parsed);
  409. }
  410. sav_eav &= 0xF0;
  411. /* Get the first line if we have some portion of an SAV/EAV from
  412. the last buffer or a partial line */
  413. if (sav_eav) {
  414. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  415. sav_eav, /* SAV/EAV */
  416. p_buffer + bytes_parsed, /* p_buffer */
  417. buffer_size - bytes_parsed);/* buf size */
  418. }
  419. /* Now parse data that is completely in this buffer */
  420. /* dma_q->is_partial_line = 0; */
  421. while (bytes_parsed < buffer_size) {
  422. u32 bytes_used = 0;
  423. sav_eav = cx231xx_find_next_SAV_EAV(
  424. p_buffer + bytes_parsed, /* p_buffer */
  425. buffer_size - bytes_parsed, /* buf size */
  426. &bytes_used);/* bytes used to get SAV/EAV */
  427. bytes_parsed += bytes_used;
  428. sav_eav &= 0xF0;
  429. if (sav_eav && (bytes_parsed < buffer_size)) {
  430. bytes_parsed += cx231xx_get_video_line(dev,
  431. dma_q, sav_eav, /* SAV/EAV */
  432. p_buffer + bytes_parsed,/* p_buffer */
  433. buffer_size - bytes_parsed);/*buf size*/
  434. }
  435. }
  436. /* Save the last four bytes of the buffer so we can check the
  437. buffer boundary condition next time */
  438. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  439. bytes_parsed = 0;
  440. }
  441. return rc;
  442. }
  443. u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
  444. u32 *p_bytes_used)
  445. {
  446. u32 bytes_used;
  447. u8 boundary_bytes[8];
  448. u8 sav_eav = 0;
  449. *p_bytes_used = 0;
  450. /* Create an array of the last 4 bytes of the last buffer and the first
  451. 4 bytes of the current buffer. */
  452. memcpy(boundary_bytes, partial_buf, 4);
  453. memcpy(boundary_bytes + 4, p_buffer, 4);
  454. /* Check for the SAV/EAV in the boundary buffer */
  455. sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
  456. &bytes_used);
  457. if (sav_eav) {
  458. /* found a boundary SAV/EAV. Updates the bytes used to reflect
  459. only those used in the new buffer */
  460. *p_bytes_used = bytes_used - 4;
  461. }
  462. return sav_eav;
  463. }
  464. u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
  465. {
  466. u32 i;
  467. u8 sav_eav = 0;
  468. /*
  469. * Don't search if the buffer size is less than 4. It causes a page
  470. * fault since buffer_size - 4 evaluates to a large number in that
  471. * case.
  472. */
  473. if (buffer_size < 4) {
  474. *p_bytes_used = buffer_size;
  475. return 0;
  476. }
  477. for (i = 0; i < (buffer_size - 3); i++) {
  478. if ((p_buffer[i] == 0xFF) &&
  479. (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
  480. *p_bytes_used = i + 4;
  481. sav_eav = p_buffer[i + 3];
  482. return sav_eav;
  483. }
  484. }
  485. *p_bytes_used = buffer_size;
  486. return 0;
  487. }
  488. u32 cx231xx_get_video_line(struct cx231xx *dev,
  489. struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
  490. u8 *p_buffer, u32 buffer_size)
  491. {
  492. u32 bytes_copied = 0;
  493. int current_field = -1;
  494. switch (sav_eav) {
  495. case SAV_ACTIVE_VIDEO_FIELD1:
  496. /* looking for skipped line which occurred in PAL 720x480 mode.
  497. In this case, there will be no active data contained
  498. between the SAV and EAV */
  499. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  500. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  501. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  502. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  503. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  504. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  505. return bytes_copied;
  506. current_field = 1;
  507. break;
  508. case SAV_ACTIVE_VIDEO_FIELD2:
  509. /* looking for skipped line which occurred in PAL 720x480 mode.
  510. In this case, there will be no active data contained between
  511. the SAV and EAV */
  512. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  513. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  514. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  515. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  516. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  517. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  518. return bytes_copied;
  519. current_field = 2;
  520. break;
  521. }
  522. dma_q->last_sav = sav_eav;
  523. bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
  524. buffer_size, current_field);
  525. return bytes_copied;
  526. }
  527. u32 cx231xx_copy_video_line(struct cx231xx *dev,
  528. struct cx231xx_dmaqueue *dma_q, u8 *p_line,
  529. u32 length, int field_number)
  530. {
  531. u32 bytes_to_copy;
  532. struct cx231xx_buffer *buf;
  533. u32 _line_size = dev->width * 2;
  534. if (dma_q->current_field != field_number)
  535. cx231xx_reset_video_buffer(dev, dma_q);
  536. /* get the buffer pointer */
  537. if (dev->USE_ISO)
  538. buf = dev->video_mode.isoc_ctl.buf;
  539. else
  540. buf = dev->video_mode.bulk_ctl.buf;
  541. /* Remember the field number for next time */
  542. dma_q->current_field = field_number;
  543. bytes_to_copy = dma_q->bytes_left_in_line;
  544. if (bytes_to_copy > length)
  545. bytes_to_copy = length;
  546. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  547. dma_q->bytes_left_in_line -= bytes_to_copy;
  548. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
  549. 0 : 1;
  550. return 0;
  551. }
  552. dma_q->is_partial_line = 1;
  553. /* If we don't have a buffer, just return the number of bytes we would
  554. have copied if we had a buffer. */
  555. if (!buf) {
  556. dma_q->bytes_left_in_line -= bytes_to_copy;
  557. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
  558. ? 0 : 1;
  559. return bytes_to_copy;
  560. }
  561. /* copy the data to video buffer */
  562. cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
  563. dma_q->pos += bytes_to_copy;
  564. dma_q->bytes_left_in_line -= bytes_to_copy;
  565. if (dma_q->bytes_left_in_line == 0) {
  566. dma_q->bytes_left_in_line = _line_size;
  567. dma_q->lines_completed++;
  568. dma_q->is_partial_line = 0;
  569. if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
  570. buffer_filled(dev, dma_q, buf);
  571. dma_q->pos = 0;
  572. buf = NULL;
  573. dma_q->lines_completed = 0;
  574. }
  575. }
  576. return bytes_to_copy;
  577. }
  578. void cx231xx_reset_video_buffer(struct cx231xx *dev,
  579. struct cx231xx_dmaqueue *dma_q)
  580. {
  581. struct cx231xx_buffer *buf;
  582. /* handle the switch from field 1 to field 2 */
  583. if (dma_q->current_field == 1) {
  584. if (dma_q->lines_completed >= dma_q->lines_per_field)
  585. dma_q->field1_done = 1;
  586. else
  587. dma_q->field1_done = 0;
  588. }
  589. if (dev->USE_ISO)
  590. buf = dev->video_mode.isoc_ctl.buf;
  591. else
  592. buf = dev->video_mode.bulk_ctl.buf;
  593. if (buf == NULL) {
  594. u8 *outp = NULL;
  595. /* first try to get the buffer */
  596. get_next_buf(dma_q, &buf);
  597. if (buf)
  598. outp = videobuf_to_vmalloc(&buf->vb);
  599. dma_q->pos = 0;
  600. dma_q->field1_done = 0;
  601. dma_q->current_field = -1;
  602. }
  603. /* reset the counters */
  604. dma_q->bytes_left_in_line = dev->width << 1;
  605. dma_q->lines_completed = 0;
  606. }
  607. int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  608. u8 *p_buffer, u32 bytes_to_copy)
  609. {
  610. u8 *p_out_buffer = NULL;
  611. u32 current_line_bytes_copied = 0;
  612. struct cx231xx_buffer *buf;
  613. u32 _line_size = dev->width << 1;
  614. void *startwrite;
  615. int offset, lencopy;
  616. if (dev->USE_ISO)
  617. buf = dev->video_mode.isoc_ctl.buf;
  618. else
  619. buf = dev->video_mode.bulk_ctl.buf;
  620. if (buf == NULL)
  621. return -1;
  622. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  623. current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
  624. /* Offset field 2 one line from the top of the buffer */
  625. offset = (dma_q->current_field == 1) ? 0 : _line_size;
  626. /* Offset for field 2 */
  627. startwrite = p_out_buffer + offset;
  628. /* lines already completed in the current field */
  629. startwrite += (dma_q->lines_completed * _line_size * 2);
  630. /* bytes already completed in the current line */
  631. startwrite += current_line_bytes_copied;
  632. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  633. bytes_to_copy : dma_q->bytes_left_in_line;
  634. if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
  635. return 0;
  636. /* The below copies the UYVY data straight into video buffer */
  637. cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
  638. return 0;
  639. }
  640. void cx231xx_swab(u16 *from, u16 *to, u16 len)
  641. {
  642. u16 i;
  643. if (len <= 0)
  644. return;
  645. for (i = 0; i < len / 2; i++)
  646. to[i] = (from[i] << 8) | (from[i] >> 8);
  647. }
  648. u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
  649. {
  650. u8 buffer_complete = 0;
  651. /* Dual field stream */
  652. buffer_complete = ((dma_q->current_field == 2) &&
  653. (dma_q->lines_completed >= dma_q->lines_per_field) &&
  654. dma_q->field1_done);
  655. return buffer_complete;
  656. }
  657. /* ------------------------------------------------------------------
  658. Videobuf operations
  659. ------------------------------------------------------------------*/
  660. static int
  661. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  662. {
  663. struct cx231xx_fh *fh = vq->priv_data;
  664. struct cx231xx *dev = fh->dev;
  665. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
  666. if (0 == *count)
  667. *count = CX231XX_DEF_BUF;
  668. if (*count < CX231XX_MIN_BUF)
  669. *count = CX231XX_MIN_BUF;
  670. return 0;
  671. }
  672. /* This is called *without* dev->slock held; please keep it that way */
  673. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  674. {
  675. struct cx231xx_fh *fh = vq->priv_data;
  676. struct cx231xx *dev = fh->dev;
  677. unsigned long flags = 0;
  678. if (in_interrupt())
  679. BUG();
  680. /* We used to wait for the buffer to finish here, but this didn't work
  681. because, as we were keeping the state as VIDEOBUF_QUEUED,
  682. videobuf_queue_cancel marked it as finished for us.
  683. (Also, it could wedge forever if the hardware was misconfigured.)
  684. This should be safe; by the time we get here, the buffer isn't
  685. queued anymore. If we ever start marking the buffers as
  686. VIDEOBUF_ACTIVE, it won't be, though.
  687. */
  688. spin_lock_irqsave(&dev->video_mode.slock, flags);
  689. if (dev->USE_ISO) {
  690. if (dev->video_mode.isoc_ctl.buf == buf)
  691. dev->video_mode.isoc_ctl.buf = NULL;
  692. } else {
  693. if (dev->video_mode.bulk_ctl.buf == buf)
  694. dev->video_mode.bulk_ctl.buf = NULL;
  695. }
  696. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  697. videobuf_vmalloc_free(&buf->vb);
  698. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  699. }
  700. static int
  701. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  702. enum v4l2_field field)
  703. {
  704. struct cx231xx_fh *fh = vq->priv_data;
  705. struct cx231xx_buffer *buf =
  706. container_of(vb, struct cx231xx_buffer, vb);
  707. struct cx231xx *dev = fh->dev;
  708. int rc = 0, urb_init = 0;
  709. /* The only currently supported format is 16 bits/pixel */
  710. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  711. + 7) >> 3;
  712. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  713. return -EINVAL;
  714. buf->vb.width = dev->width;
  715. buf->vb.height = dev->height;
  716. buf->vb.field = field;
  717. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  718. rc = videobuf_iolock(vq, &buf->vb, NULL);
  719. if (rc < 0)
  720. goto fail;
  721. }
  722. if (dev->USE_ISO) {
  723. if (!dev->video_mode.isoc_ctl.num_bufs)
  724. urb_init = 1;
  725. } else {
  726. if (!dev->video_mode.bulk_ctl.num_bufs)
  727. urb_init = 1;
  728. }
  729. /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
  730. urb_init, dev->video_mode.max_pkt_size);*/
  731. if (urb_init) {
  732. dev->mode_tv = 0;
  733. if (dev->USE_ISO)
  734. rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  735. CX231XX_NUM_BUFS,
  736. dev->video_mode.max_pkt_size,
  737. cx231xx_isoc_copy);
  738. else
  739. rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
  740. CX231XX_NUM_BUFS,
  741. dev->video_mode.max_pkt_size,
  742. cx231xx_bulk_copy);
  743. if (rc < 0)
  744. goto fail;
  745. }
  746. buf->vb.state = VIDEOBUF_PREPARED;
  747. return 0;
  748. fail:
  749. free_buffer(vq, buf);
  750. return rc;
  751. }
  752. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  753. {
  754. struct cx231xx_buffer *buf =
  755. container_of(vb, struct cx231xx_buffer, vb);
  756. struct cx231xx_fh *fh = vq->priv_data;
  757. struct cx231xx *dev = fh->dev;
  758. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  759. buf->vb.state = VIDEOBUF_QUEUED;
  760. list_add_tail(&buf->vb.queue, &vidq->active);
  761. }
  762. static void buffer_release(struct videobuf_queue *vq,
  763. struct videobuf_buffer *vb)
  764. {
  765. struct cx231xx_buffer *buf =
  766. container_of(vb, struct cx231xx_buffer, vb);
  767. struct cx231xx_fh *fh = vq->priv_data;
  768. struct cx231xx *dev = (struct cx231xx *)fh->dev;
  769. cx231xx_isocdbg("cx231xx: called buffer_release\n");
  770. free_buffer(vq, buf);
  771. }
  772. static struct videobuf_queue_ops cx231xx_video_qops = {
  773. .buf_setup = buffer_setup,
  774. .buf_prepare = buffer_prepare,
  775. .buf_queue = buffer_queue,
  776. .buf_release = buffer_release,
  777. };
  778. /********************* v4l2 interface **************************************/
  779. void video_mux(struct cx231xx *dev, int index)
  780. {
  781. dev->video_input = index;
  782. dev->ctl_ainput = INPUT(index)->amux;
  783. cx231xx_set_video_input_mux(dev, index);
  784. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  785. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  786. cx231xx_info("video_mux : %d\n", index);
  787. /* do mode control overrides if required */
  788. cx231xx_do_mode_ctrl_overrides(dev);
  789. }
  790. /* Usage lock check functions */
  791. static int res_get(struct cx231xx_fh *fh)
  792. {
  793. struct cx231xx *dev = fh->dev;
  794. int rc = 0;
  795. /* This instance already has stream_on */
  796. if (fh->stream_on)
  797. return rc;
  798. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  799. if (dev->stream_on)
  800. return -EBUSY;
  801. dev->stream_on = 1;
  802. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  803. if (dev->vbi_stream_on)
  804. return -EBUSY;
  805. dev->vbi_stream_on = 1;
  806. } else
  807. return -EINVAL;
  808. fh->stream_on = 1;
  809. return rc;
  810. }
  811. static int res_check(struct cx231xx_fh *fh)
  812. {
  813. return fh->stream_on;
  814. }
  815. static void res_free(struct cx231xx_fh *fh)
  816. {
  817. struct cx231xx *dev = fh->dev;
  818. fh->stream_on = 0;
  819. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  820. dev->stream_on = 0;
  821. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  822. dev->vbi_stream_on = 0;
  823. }
  824. static int check_dev(struct cx231xx *dev)
  825. {
  826. if (dev->state & DEV_DISCONNECTED) {
  827. cx231xx_errdev("v4l2 ioctl: device not present\n");
  828. return -ENODEV;
  829. }
  830. if (dev->state & DEV_MISCONFIGURED) {
  831. cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
  832. "close and open it again\n");
  833. return -EIO;
  834. }
  835. return 0;
  836. }
  837. static void get_scale(struct cx231xx *dev,
  838. unsigned int width, unsigned int height,
  839. unsigned int *hscale, unsigned int *vscale)
  840. {
  841. unsigned int maxw = norm_maxw(dev);
  842. unsigned int maxh = norm_maxh(dev);
  843. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  844. if (*hscale >= 0x4000)
  845. *hscale = 0x3fff;
  846. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  847. if (*vscale >= 0x4000)
  848. *vscale = 0x3fff;
  849. }
  850. /* ------------------------------------------------------------------
  851. IOCTL vidioc handling
  852. ------------------------------------------------------------------*/
  853. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  854. struct v4l2_format *f)
  855. {
  856. struct cx231xx_fh *fh = priv;
  857. struct cx231xx *dev = fh->dev;
  858. mutex_lock(&dev->lock);
  859. f->fmt.pix.width = dev->width;
  860. f->fmt.pix.height = dev->height;
  861. f->fmt.pix.pixelformat = dev->format->fourcc;
  862. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  863. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  864. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  865. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  866. mutex_unlock(&dev->lock);
  867. return 0;
  868. }
  869. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  870. {
  871. unsigned int i;
  872. for (i = 0; i < ARRAY_SIZE(format); i++)
  873. if (format[i].fourcc == fourcc)
  874. return &format[i];
  875. return NULL;
  876. }
  877. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  878. struct v4l2_format *f)
  879. {
  880. struct cx231xx_fh *fh = priv;
  881. struct cx231xx *dev = fh->dev;
  882. unsigned int width = f->fmt.pix.width;
  883. unsigned int height = f->fmt.pix.height;
  884. unsigned int maxw = norm_maxw(dev);
  885. unsigned int maxh = norm_maxh(dev);
  886. unsigned int hscale, vscale;
  887. struct cx231xx_fmt *fmt;
  888. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  889. if (!fmt) {
  890. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  891. f->fmt.pix.pixelformat);
  892. return -EINVAL;
  893. }
  894. /* width must even because of the YUYV format
  895. height must be even because of interlacing */
  896. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  897. get_scale(dev, width, height, &hscale, &vscale);
  898. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  899. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  900. f->fmt.pix.width = width;
  901. f->fmt.pix.height = height;
  902. f->fmt.pix.pixelformat = fmt->fourcc;
  903. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  904. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  905. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  906. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  907. return 0;
  908. }
  909. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  910. struct v4l2_format *f)
  911. {
  912. struct cx231xx_fh *fh = priv;
  913. struct cx231xx *dev = fh->dev;
  914. int rc;
  915. struct cx231xx_fmt *fmt;
  916. struct v4l2_mbus_framefmt mbus_fmt;
  917. rc = check_dev(dev);
  918. if (rc < 0)
  919. return rc;
  920. mutex_lock(&dev->lock);
  921. vidioc_try_fmt_vid_cap(file, priv, f);
  922. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  923. if (!fmt) {
  924. rc = -EINVAL;
  925. goto out;
  926. }
  927. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  928. cx231xx_errdev("%s queue busy\n", __func__);
  929. rc = -EBUSY;
  930. goto out;
  931. }
  932. if (dev->stream_on && !fh->stream_on) {
  933. cx231xx_errdev("%s device in use by another fh\n", __func__);
  934. rc = -EBUSY;
  935. goto out;
  936. }
  937. /* set new image size */
  938. dev->width = f->fmt.pix.width;
  939. dev->height = f->fmt.pix.height;
  940. dev->format = fmt;
  941. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  942. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  943. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  944. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  945. /* Set the correct alternate setting for this resolution */
  946. cx231xx_resolution_set(dev);
  947. out:
  948. mutex_unlock(&dev->lock);
  949. return rc;
  950. }
  951. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  952. {
  953. struct cx231xx_fh *fh = priv;
  954. struct cx231xx *dev = fh->dev;
  955. *id = dev->norm;
  956. return 0;
  957. }
  958. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  959. {
  960. struct cx231xx_fh *fh = priv;
  961. struct cx231xx *dev = fh->dev;
  962. struct v4l2_format f;
  963. int rc;
  964. rc = check_dev(dev);
  965. if (rc < 0)
  966. return rc;
  967. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  968. mutex_lock(&dev->lock);
  969. dev->norm = *norm;
  970. /* Adjusts width/height, if needed */
  971. f.fmt.pix.width = dev->width;
  972. f.fmt.pix.height = dev->height;
  973. vidioc_try_fmt_vid_cap(file, priv, &f);
  974. /* set new image size */
  975. dev->width = f.fmt.pix.width;
  976. dev->height = f.fmt.pix.height;
  977. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  978. call_all(dev, core, s_std, dev->norm);
  979. mutex_unlock(&dev->lock);
  980. cx231xx_resolution_set(dev);
  981. /* do mode control overrides */
  982. cx231xx_do_mode_ctrl_overrides(dev);
  983. return 0;
  984. }
  985. static const char *iname[] = {
  986. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  987. [CX231XX_VMUX_SVIDEO] = "S-Video",
  988. [CX231XX_VMUX_TELEVISION] = "Television",
  989. [CX231XX_VMUX_CABLE] = "Cable TV",
  990. [CX231XX_VMUX_DVB] = "DVB",
  991. [CX231XX_VMUX_DEBUG] = "for debug only",
  992. };
  993. static int vidioc_enum_input(struct file *file, void *priv,
  994. struct v4l2_input *i)
  995. {
  996. struct cx231xx_fh *fh = priv;
  997. struct cx231xx *dev = fh->dev;
  998. unsigned int n;
  999. n = i->index;
  1000. if (n >= MAX_CX231XX_INPUT)
  1001. return -EINVAL;
  1002. if (0 == INPUT(n)->type)
  1003. return -EINVAL;
  1004. i->index = n;
  1005. i->type = V4L2_INPUT_TYPE_CAMERA;
  1006. strcpy(i->name, iname[INPUT(n)->type]);
  1007. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1008. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  1009. i->type = V4L2_INPUT_TYPE_TUNER;
  1010. i->std = dev->vdev->tvnorms;
  1011. return 0;
  1012. }
  1013. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1014. {
  1015. struct cx231xx_fh *fh = priv;
  1016. struct cx231xx *dev = fh->dev;
  1017. *i = dev->video_input;
  1018. return 0;
  1019. }
  1020. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1021. {
  1022. struct cx231xx_fh *fh = priv;
  1023. struct cx231xx *dev = fh->dev;
  1024. int rc;
  1025. if (i == 10) {
  1026. dev->USE_ISO = 0;
  1027. cx231xx_info("trans-mode=BULK. USE_ISO = %d\n", dev->USE_ISO);
  1028. return 0;
  1029. }
  1030. if (i == 11) {
  1031. dev->USE_ISO = 1;
  1032. cx231xx_info("trans-mode=ISOC. USE_ISO = %d\n", dev->USE_ISO);
  1033. return 0;
  1034. }
  1035. dev->mode_tv = 0;
  1036. rc = check_dev(dev);
  1037. if (rc < 0)
  1038. return rc;
  1039. if (i >= MAX_CX231XX_INPUT)
  1040. return -EINVAL;
  1041. if (0 == INPUT(i)->type)
  1042. return -EINVAL;
  1043. mutex_lock(&dev->lock);
  1044. video_mux(dev, i);
  1045. mutex_unlock(&dev->lock);
  1046. return 0;
  1047. }
  1048. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1049. {
  1050. struct cx231xx_fh *fh = priv;
  1051. struct cx231xx *dev = fh->dev;
  1052. switch (a->index) {
  1053. case CX231XX_AMUX_VIDEO:
  1054. strcpy(a->name, "Television");
  1055. break;
  1056. case CX231XX_AMUX_LINE_IN:
  1057. strcpy(a->name, "Line In");
  1058. break;
  1059. default:
  1060. return -EINVAL;
  1061. }
  1062. a->index = dev->ctl_ainput;
  1063. a->capability = V4L2_AUDCAP_STEREO;
  1064. return 0;
  1065. }
  1066. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1067. {
  1068. struct cx231xx_fh *fh = priv;
  1069. struct cx231xx *dev = fh->dev;
  1070. int status = 0;
  1071. /* Doesn't allow manual routing */
  1072. if (a->index != dev->ctl_ainput)
  1073. return -EINVAL;
  1074. dev->ctl_ainput = INPUT(a->index)->amux;
  1075. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  1076. return status;
  1077. }
  1078. static int vidioc_queryctrl(struct file *file, void *priv,
  1079. struct v4l2_queryctrl *qc)
  1080. {
  1081. struct cx231xx_fh *fh = priv;
  1082. struct cx231xx *dev = fh->dev;
  1083. int id = qc->id;
  1084. int i;
  1085. int rc;
  1086. rc = check_dev(dev);
  1087. if (rc < 0)
  1088. return rc;
  1089. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  1090. if (unlikely(qc->id == 0))
  1091. return -EINVAL;
  1092. memset(qc, 0, sizeof(*qc));
  1093. qc->id = id;
  1094. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  1095. return -EINVAL;
  1096. for (i = 0; i < CX231XX_CTLS; i++)
  1097. if (cx231xx_ctls[i].v.id == qc->id)
  1098. break;
  1099. if (i == CX231XX_CTLS) {
  1100. *qc = no_ctl;
  1101. return 0;
  1102. }
  1103. *qc = cx231xx_ctls[i].v;
  1104. mutex_lock(&dev->lock);
  1105. call_all(dev, core, queryctrl, qc);
  1106. mutex_unlock(&dev->lock);
  1107. if (qc->type)
  1108. return 0;
  1109. else
  1110. return -EINVAL;
  1111. }
  1112. static int vidioc_g_ctrl(struct file *file, void *priv,
  1113. struct v4l2_control *ctrl)
  1114. {
  1115. struct cx231xx_fh *fh = priv;
  1116. struct cx231xx *dev = fh->dev;
  1117. int rc;
  1118. rc = check_dev(dev);
  1119. if (rc < 0)
  1120. return rc;
  1121. mutex_lock(&dev->lock);
  1122. call_all(dev, core, g_ctrl, ctrl);
  1123. mutex_unlock(&dev->lock);
  1124. return rc;
  1125. }
  1126. static int vidioc_s_ctrl(struct file *file, void *priv,
  1127. struct v4l2_control *ctrl)
  1128. {
  1129. struct cx231xx_fh *fh = priv;
  1130. struct cx231xx *dev = fh->dev;
  1131. int rc;
  1132. rc = check_dev(dev);
  1133. if (rc < 0)
  1134. return rc;
  1135. mutex_lock(&dev->lock);
  1136. call_all(dev, core, s_ctrl, ctrl);
  1137. mutex_unlock(&dev->lock);
  1138. return rc;
  1139. }
  1140. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1141. {
  1142. struct cx231xx_fh *fh = priv;
  1143. struct cx231xx *dev = fh->dev;
  1144. int rc;
  1145. rc = check_dev(dev);
  1146. if (rc < 0)
  1147. return rc;
  1148. if (0 != t->index)
  1149. return -EINVAL;
  1150. strcpy(t->name, "Tuner");
  1151. t->type = V4L2_TUNER_ANALOG_TV;
  1152. t->capability = V4L2_TUNER_CAP_NORM;
  1153. t->rangehigh = 0xffffffffUL;
  1154. t->signal = 0xffff; /* LOCKED */
  1155. return 0;
  1156. }
  1157. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1158. {
  1159. struct cx231xx_fh *fh = priv;
  1160. struct cx231xx *dev = fh->dev;
  1161. int rc;
  1162. rc = check_dev(dev);
  1163. if (rc < 0)
  1164. return rc;
  1165. if (0 != t->index)
  1166. return -EINVAL;
  1167. #if 0
  1168. mutex_lock(&dev->lock);
  1169. call_all(dev, tuner, s_tuner, t);
  1170. mutex_unlock(&dev->lock);
  1171. #endif
  1172. return 0;
  1173. }
  1174. static int vidioc_g_frequency(struct file *file, void *priv,
  1175. struct v4l2_frequency *f)
  1176. {
  1177. struct cx231xx_fh *fh = priv;
  1178. struct cx231xx *dev = fh->dev;
  1179. mutex_lock(&dev->lock);
  1180. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1181. f->frequency = dev->ctl_freq;
  1182. call_all(dev, tuner, g_frequency, f);
  1183. mutex_unlock(&dev->lock);
  1184. return 0;
  1185. }
  1186. static int vidioc_s_frequency(struct file *file, void *priv,
  1187. struct v4l2_frequency *f)
  1188. {
  1189. struct cx231xx_fh *fh = priv;
  1190. struct cx231xx *dev = fh->dev;
  1191. int rc;
  1192. u32 if_frequency = 5400000;
  1193. cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  1194. f->frequency, f->type);
  1195. /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
  1196. rc = check_dev(dev);
  1197. if (rc < 0)
  1198. return rc;
  1199. if (0 != f->tuner)
  1200. return -EINVAL;
  1201. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1202. return -EINVAL;
  1203. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1204. return -EINVAL;
  1205. /* set pre channel change settings in DIF first */
  1206. rc = cx231xx_tuner_pre_channel_change(dev);
  1207. mutex_lock(&dev->lock);
  1208. dev->ctl_freq = f->frequency;
  1209. call_all(dev, tuner, s_frequency, f);
  1210. mutex_unlock(&dev->lock);
  1211. /* set post channel change settings in DIF first */
  1212. rc = cx231xx_tuner_post_channel_change(dev);
  1213. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  1214. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  1215. if_frequency = 5400000; /*5.4MHz */
  1216. else if (dev->norm & V4L2_STD_B)
  1217. if_frequency = 6000000; /*6.0MHz */
  1218. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  1219. if_frequency = 6900000; /*6.9MHz */
  1220. else if (dev->norm & V4L2_STD_GH)
  1221. if_frequency = 7100000; /*7.1MHz */
  1222. else if (dev->norm & V4L2_STD_PAL_I)
  1223. if_frequency = 7250000; /*7.25MHz */
  1224. else if (dev->norm & V4L2_STD_SECAM_L)
  1225. if_frequency = 6900000; /*6.9MHz */
  1226. else if (dev->norm & V4L2_STD_SECAM_LC)
  1227. if_frequency = 1250000; /*1.25MHz */
  1228. cx231xx_info("if_frequency is set to %d\n", if_frequency);
  1229. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  1230. update_HH_register_after_set_DIF(dev);
  1231. }
  1232. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1233. return rc;
  1234. }
  1235. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1236. /*
  1237. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1238. chip=<chip>[,min=<addr>,max=<addr>]
  1239. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1240. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1241. chip=<chip>,reg=<addr>,val=<val>
  1242. set the register [VIDIOC_DBG_S_REGISTER]
  1243. if type == host, then <chip> is the hosts chip ID (default 0)
  1244. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1245. if type == i2caddr, then <chip> is the 7-bit I2C address
  1246. */
  1247. static int vidioc_g_register(struct file *file, void *priv,
  1248. struct v4l2_dbg_register *reg)
  1249. {
  1250. struct cx231xx_fh *fh = priv;
  1251. struct cx231xx *dev = fh->dev;
  1252. int ret = 0;
  1253. u8 value[4] = { 0, 0, 0, 0 };
  1254. u32 data = 0;
  1255. switch (reg->match.type) {
  1256. case V4L2_CHIP_MATCH_HOST:
  1257. switch (reg->match.addr) {
  1258. case 0: /* Cx231xx - internal registers */
  1259. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1260. (u16)reg->reg, value, 4);
  1261. reg->val = value[0] | value[1] << 8 |
  1262. value[2] << 16 | value[3] << 24;
  1263. break;
  1264. case 1: /* AFE - read byte */
  1265. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1266. (u16)reg->reg, 2, &data, 1);
  1267. reg->val = le32_to_cpu(data & 0xff);
  1268. break;
  1269. case 14: /* AFE - read dword */
  1270. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1271. (u16)reg->reg, 2, &data, 4);
  1272. reg->val = le32_to_cpu(data);
  1273. break;
  1274. case 2: /* Video Block - read byte */
  1275. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1276. (u16)reg->reg, 2, &data, 1);
  1277. reg->val = le32_to_cpu(data & 0xff);
  1278. break;
  1279. case 24: /* Video Block - read dword */
  1280. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1281. (u16)reg->reg, 2, &data, 4);
  1282. reg->val = le32_to_cpu(data);
  1283. break;
  1284. case 3: /* I2S block - read byte */
  1285. ret = cx231xx_read_i2c_data(dev,
  1286. I2S_BLK_DEVICE_ADDRESS,
  1287. (u16)reg->reg, 1,
  1288. &data, 1);
  1289. reg->val = le32_to_cpu(data & 0xff);
  1290. break;
  1291. case 34: /* I2S Block - read dword */
  1292. ret =
  1293. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1294. (u16)reg->reg, 1, &data, 4);
  1295. reg->val = le32_to_cpu(data);
  1296. break;
  1297. }
  1298. return ret < 0 ? ret : 0;
  1299. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1300. call_all(dev, core, g_register, reg);
  1301. return 0;
  1302. case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
  1303. switch (reg->match.addr) {
  1304. case 0: /* Cx231xx - internal registers */
  1305. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1306. (u16)reg->reg, value, 4);
  1307. reg->val = value[0] | value[1] << 8 |
  1308. value[2] << 16 | value[3] << 24;
  1309. break;
  1310. case 0x600:/* AFE - read byte */
  1311. ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
  1312. (u16)reg->reg, 2,
  1313. &data, 1 , 0);
  1314. reg->val = le32_to_cpu(data & 0xff);
  1315. break;
  1316. case 0x880:/* Video Block - read byte */
  1317. if (reg->reg < 0x0b) {
  1318. ret = cx231xx_read_i2c_master(dev,
  1319. VID_BLK_I2C_ADDRESS,
  1320. (u16)reg->reg, 2,
  1321. &data, 1 , 0);
  1322. reg->val = le32_to_cpu(data & 0xff);
  1323. } else {
  1324. ret = cx231xx_read_i2c_master(dev,
  1325. VID_BLK_I2C_ADDRESS,
  1326. (u16)reg->reg, 2,
  1327. &data, 4 , 0);
  1328. reg->val = le32_to_cpu(data);
  1329. }
  1330. break;
  1331. case 0x980:
  1332. ret = cx231xx_read_i2c_master(dev,
  1333. I2S_BLK_DEVICE_ADDRESS,
  1334. (u16)reg->reg, 1,
  1335. &data, 1 , 0);
  1336. reg->val = le32_to_cpu(data & 0xff);
  1337. break;
  1338. case 0x400:
  1339. ret =
  1340. cx231xx_read_i2c_master(dev, 0x40,
  1341. (u16)reg->reg, 1,
  1342. &data, 1 , 0);
  1343. reg->val = le32_to_cpu(data & 0xff);
  1344. break;
  1345. case 0xc01:
  1346. ret =
  1347. cx231xx_read_i2c_master(dev, 0xc0,
  1348. (u16)reg->reg, 2,
  1349. &data, 38, 1);
  1350. reg->val = le32_to_cpu(data);
  1351. break;
  1352. case 0x022:
  1353. ret =
  1354. cx231xx_read_i2c_master(dev, 0x02,
  1355. (u16)reg->reg, 1,
  1356. &data, 1, 2);
  1357. reg->val = le32_to_cpu(data & 0xff);
  1358. break;
  1359. case 0x322:
  1360. ret = cx231xx_read_i2c_master(dev,
  1361. 0x32,
  1362. (u16)reg->reg, 1,
  1363. &data, 4 , 2);
  1364. reg->val = le32_to_cpu(data);
  1365. break;
  1366. case 0x342:
  1367. ret = cx231xx_read_i2c_master(dev,
  1368. 0x34,
  1369. (u16)reg->reg, 1,
  1370. &data, 4 , 2);
  1371. reg->val = le32_to_cpu(data);
  1372. break;
  1373. default:
  1374. cx231xx_info("no match device address!!\n");
  1375. break;
  1376. }
  1377. return ret < 0 ? ret : 0;
  1378. /*return -EINVAL;*/
  1379. default:
  1380. if (!v4l2_chip_match_host(&reg->match))
  1381. return -EINVAL;
  1382. }
  1383. mutex_lock(&dev->lock);
  1384. call_all(dev, core, g_register, reg);
  1385. mutex_unlock(&dev->lock);
  1386. return ret;
  1387. }
  1388. static int vidioc_s_register(struct file *file, void *priv,
  1389. struct v4l2_dbg_register *reg)
  1390. {
  1391. struct cx231xx_fh *fh = priv;
  1392. struct cx231xx *dev = fh->dev;
  1393. int ret = 0;
  1394. __le64 buf;
  1395. u32 value;
  1396. u8 data[4] = { 0, 0, 0, 0 };
  1397. buf = cpu_to_le64(reg->val);
  1398. switch (reg->match.type) {
  1399. case V4L2_CHIP_MATCH_HOST:
  1400. {
  1401. value = (u32) buf & 0xffffffff;
  1402. switch (reg->match.addr) {
  1403. case 0: /* cx231xx internal registers */
  1404. data[0] = (u8) value;
  1405. data[1] = (u8) (value >> 8);
  1406. data[2] = (u8) (value >> 16);
  1407. data[3] = (u8) (value >> 24);
  1408. ret = cx231xx_write_ctrl_reg(dev,
  1409. VRT_SET_REGISTER,
  1410. (u16)reg->reg, data,
  1411. 4);
  1412. break;
  1413. case 1: /* AFE - read byte */
  1414. ret = cx231xx_write_i2c_data(dev,
  1415. AFE_DEVICE_ADDRESS,
  1416. (u16)reg->reg, 2,
  1417. value, 1);
  1418. break;
  1419. case 14: /* AFE - read dword */
  1420. ret = cx231xx_write_i2c_data(dev,
  1421. AFE_DEVICE_ADDRESS,
  1422. (u16)reg->reg, 2,
  1423. value, 4);
  1424. break;
  1425. case 2: /* Video Block - read byte */
  1426. ret =
  1427. cx231xx_write_i2c_data(dev,
  1428. VID_BLK_I2C_ADDRESS,
  1429. (u16)reg->reg, 2,
  1430. value, 1);
  1431. break;
  1432. case 24: /* Video Block - read dword */
  1433. ret =
  1434. cx231xx_write_i2c_data(dev,
  1435. VID_BLK_I2C_ADDRESS,
  1436. (u16)reg->reg, 2,
  1437. value, 4);
  1438. break;
  1439. case 3: /* I2S block - read byte */
  1440. ret =
  1441. cx231xx_write_i2c_data(dev,
  1442. I2S_BLK_DEVICE_ADDRESS,
  1443. (u16)reg->reg, 1,
  1444. value, 1);
  1445. break;
  1446. case 34: /* I2S block - read dword */
  1447. ret =
  1448. cx231xx_write_i2c_data(dev,
  1449. I2S_BLK_DEVICE_ADDRESS,
  1450. (u16)reg->reg, 1,
  1451. value, 4);
  1452. break;
  1453. }
  1454. }
  1455. return ret < 0 ? ret : 0;
  1456. case V4L2_CHIP_MATCH_I2C_ADDR:
  1457. {
  1458. value = (u32) buf & 0xffffffff;
  1459. switch (reg->match.addr) {
  1460. case 0:/*cx231xx internal registers*/
  1461. data[0] = (u8) value;
  1462. data[1] = (u8) (value >> 8);
  1463. data[2] = (u8) (value >> 16);
  1464. data[3] = (u8) (value >> 24);
  1465. ret = cx231xx_write_ctrl_reg(dev,
  1466. VRT_SET_REGISTER,
  1467. (u16)reg->reg, data,
  1468. 4);
  1469. break;
  1470. case 0x600:/* AFE - read byte */
  1471. ret = cx231xx_write_i2c_master(dev,
  1472. AFE_DEVICE_ADDRESS,
  1473. (u16)reg->reg, 2,
  1474. value, 1 , 0);
  1475. break;
  1476. case 0x880:/* Video Block - read byte */
  1477. if (reg->reg < 0x0b)
  1478. cx231xx_write_i2c_master(dev,
  1479. VID_BLK_I2C_ADDRESS,
  1480. (u16)reg->reg, 2,
  1481. value, 1, 0);
  1482. else
  1483. cx231xx_write_i2c_master(dev,
  1484. VID_BLK_I2C_ADDRESS,
  1485. (u16)reg->reg, 2,
  1486. value, 4, 0);
  1487. break;
  1488. case 0x980:
  1489. ret =
  1490. cx231xx_write_i2c_master(dev,
  1491. I2S_BLK_DEVICE_ADDRESS,
  1492. (u16)reg->reg, 1,
  1493. value, 1, 0);
  1494. break;
  1495. case 0x400:
  1496. ret =
  1497. cx231xx_write_i2c_master(dev,
  1498. 0x40,
  1499. (u16)reg->reg, 1,
  1500. value, 1, 0);
  1501. break;
  1502. case 0xc01:
  1503. ret =
  1504. cx231xx_write_i2c_master(dev,
  1505. 0xc0,
  1506. (u16)reg->reg, 1,
  1507. value, 1, 1);
  1508. break;
  1509. case 0x022:
  1510. ret =
  1511. cx231xx_write_i2c_master(dev,
  1512. 0x02,
  1513. (u16)reg->reg, 1,
  1514. value, 1, 2);
  1515. case 0x322:
  1516. ret =
  1517. cx231xx_write_i2c_master(dev,
  1518. 0x32,
  1519. (u16)reg->reg, 1,
  1520. value, 4, 2);
  1521. break;
  1522. case 0x342:
  1523. ret =
  1524. cx231xx_write_i2c_master(dev,
  1525. 0x34,
  1526. (u16)reg->reg, 1,
  1527. value, 4, 2);
  1528. break;
  1529. default:
  1530. cx231xx_info("no match device address, "
  1531. "the value is %x\n", reg->match.addr);
  1532. break;
  1533. }
  1534. }
  1535. default:
  1536. break;
  1537. }
  1538. mutex_lock(&dev->lock);
  1539. call_all(dev, core, s_register, reg);
  1540. mutex_unlock(&dev->lock);
  1541. return ret;
  1542. }
  1543. #endif
  1544. static int vidioc_cropcap(struct file *file, void *priv,
  1545. struct v4l2_cropcap *cc)
  1546. {
  1547. struct cx231xx_fh *fh = priv;
  1548. struct cx231xx *dev = fh->dev;
  1549. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1550. return -EINVAL;
  1551. cc->bounds.left = 0;
  1552. cc->bounds.top = 0;
  1553. cc->bounds.width = dev->width;
  1554. cc->bounds.height = dev->height;
  1555. cc->defrect = cc->bounds;
  1556. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1557. cc->pixelaspect.denominator = 59;
  1558. return 0;
  1559. }
  1560. static int vidioc_streamon(struct file *file, void *priv,
  1561. enum v4l2_buf_type type)
  1562. {
  1563. struct cx231xx_fh *fh = priv;
  1564. struct cx231xx *dev = fh->dev;
  1565. int rc;
  1566. rc = check_dev(dev);
  1567. if (rc < 0)
  1568. return rc;
  1569. mutex_lock(&dev->lock);
  1570. rc = res_get(fh);
  1571. if (likely(rc >= 0))
  1572. rc = videobuf_streamon(&fh->vb_vidq);
  1573. call_all(dev, video, s_stream, 1);
  1574. mutex_unlock(&dev->lock);
  1575. return rc;
  1576. }
  1577. static int vidioc_streamoff(struct file *file, void *priv,
  1578. enum v4l2_buf_type type)
  1579. {
  1580. struct cx231xx_fh *fh = priv;
  1581. struct cx231xx *dev = fh->dev;
  1582. int rc;
  1583. rc = check_dev(dev);
  1584. if (rc < 0)
  1585. return rc;
  1586. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1587. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1588. return -EINVAL;
  1589. if (type != fh->type)
  1590. return -EINVAL;
  1591. mutex_lock(&dev->lock);
  1592. cx25840_call(dev, video, s_stream, 0);
  1593. videobuf_streamoff(&fh->vb_vidq);
  1594. res_free(fh);
  1595. mutex_unlock(&dev->lock);
  1596. return 0;
  1597. }
  1598. static int vidioc_querycap(struct file *file, void *priv,
  1599. struct v4l2_capability *cap)
  1600. {
  1601. struct cx231xx_fh *fh = priv;
  1602. struct cx231xx *dev = fh->dev;
  1603. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1604. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1605. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1606. cap->version = CX231XX_VERSION_CODE;
  1607. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1608. #if 0
  1609. V4L2_CAP_SLICED_VBI_CAPTURE |
  1610. #endif
  1611. V4L2_CAP_VIDEO_CAPTURE |
  1612. V4L2_CAP_AUDIO |
  1613. V4L2_CAP_READWRITE |
  1614. V4L2_CAP_STREAMING;
  1615. if (dev->tuner_type != TUNER_ABSENT)
  1616. cap->capabilities |= V4L2_CAP_TUNER;
  1617. return 0;
  1618. }
  1619. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1620. struct v4l2_fmtdesc *f)
  1621. {
  1622. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1623. return -EINVAL;
  1624. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1625. f->pixelformat = format[f->index].fourcc;
  1626. return 0;
  1627. }
  1628. /* Sliced VBI ioctls */
  1629. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1630. struct v4l2_format *f)
  1631. {
  1632. struct cx231xx_fh *fh = priv;
  1633. struct cx231xx *dev = fh->dev;
  1634. int rc;
  1635. rc = check_dev(dev);
  1636. if (rc < 0)
  1637. return rc;
  1638. mutex_lock(&dev->lock);
  1639. f->fmt.sliced.service_set = 0;
  1640. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1641. if (f->fmt.sliced.service_set == 0)
  1642. rc = -EINVAL;
  1643. mutex_unlock(&dev->lock);
  1644. return rc;
  1645. }
  1646. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1647. struct v4l2_format *f)
  1648. {
  1649. struct cx231xx_fh *fh = priv;
  1650. struct cx231xx *dev = fh->dev;
  1651. int rc;
  1652. rc = check_dev(dev);
  1653. if (rc < 0)
  1654. return rc;
  1655. mutex_lock(&dev->lock);
  1656. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1657. mutex_unlock(&dev->lock);
  1658. if (f->fmt.sliced.service_set == 0)
  1659. return -EINVAL;
  1660. return 0;
  1661. }
  1662. /* RAW VBI ioctls */
  1663. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1664. struct v4l2_format *f)
  1665. {
  1666. struct cx231xx_fh *fh = priv;
  1667. struct cx231xx *dev = fh->dev;
  1668. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1669. 35468950 : 28636363;
  1670. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1671. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1672. f->fmt.vbi.offset = 64 * 4;
  1673. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1674. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1675. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1676. PAL_VBI_LINES : NTSC_VBI_LINES;
  1677. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1678. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1679. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1680. return 0;
  1681. }
  1682. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1683. struct v4l2_format *f)
  1684. {
  1685. struct cx231xx_fh *fh = priv;
  1686. struct cx231xx *dev = fh->dev;
  1687. if (dev->vbi_stream_on && !fh->stream_on) {
  1688. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1689. return -EBUSY;
  1690. }
  1691. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1692. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1693. 35468950 : 28636363;
  1694. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1695. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1696. f->fmt.vbi.offset = 244;
  1697. f->fmt.vbi.flags = 0;
  1698. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1699. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1700. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1701. PAL_VBI_LINES : NTSC_VBI_LINES;
  1702. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1703. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1704. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1705. return 0;
  1706. }
  1707. static int vidioc_reqbufs(struct file *file, void *priv,
  1708. struct v4l2_requestbuffers *rb)
  1709. {
  1710. struct cx231xx_fh *fh = priv;
  1711. struct cx231xx *dev = fh->dev;
  1712. int rc;
  1713. rc = check_dev(dev);
  1714. if (rc < 0)
  1715. return rc;
  1716. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1717. }
  1718. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1719. {
  1720. struct cx231xx_fh *fh = priv;
  1721. struct cx231xx *dev = fh->dev;
  1722. int rc;
  1723. rc = check_dev(dev);
  1724. if (rc < 0)
  1725. return rc;
  1726. return videobuf_querybuf(&fh->vb_vidq, b);
  1727. }
  1728. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1729. {
  1730. struct cx231xx_fh *fh = priv;
  1731. struct cx231xx *dev = fh->dev;
  1732. int rc;
  1733. rc = check_dev(dev);
  1734. if (rc < 0)
  1735. return rc;
  1736. return videobuf_qbuf(&fh->vb_vidq, b);
  1737. }
  1738. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1739. {
  1740. struct cx231xx_fh *fh = priv;
  1741. struct cx231xx *dev = fh->dev;
  1742. int rc;
  1743. rc = check_dev(dev);
  1744. if (rc < 0)
  1745. return rc;
  1746. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1747. }
  1748. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1749. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1750. {
  1751. struct cx231xx_fh *fh = priv;
  1752. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1753. }
  1754. #endif
  1755. /* ----------------------------------------------------------- */
  1756. /* RADIO ESPECIFIC IOCTLS */
  1757. /* ----------------------------------------------------------- */
  1758. static int radio_querycap(struct file *file, void *priv,
  1759. struct v4l2_capability *cap)
  1760. {
  1761. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1762. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1763. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1764. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1765. cap->version = CX231XX_VERSION_CODE;
  1766. cap->capabilities = V4L2_CAP_TUNER;
  1767. return 0;
  1768. }
  1769. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1770. {
  1771. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1772. if (unlikely(t->index > 0))
  1773. return -EINVAL;
  1774. strcpy(t->name, "Radio");
  1775. t->type = V4L2_TUNER_RADIO;
  1776. mutex_lock(&dev->lock);
  1777. call_all(dev, tuner, s_tuner, t);
  1778. mutex_unlock(&dev->lock);
  1779. return 0;
  1780. }
  1781. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1782. {
  1783. if (i->index != 0)
  1784. return -EINVAL;
  1785. strcpy(i->name, "Radio");
  1786. i->type = V4L2_INPUT_TYPE_TUNER;
  1787. return 0;
  1788. }
  1789. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1790. {
  1791. if (unlikely(a->index))
  1792. return -EINVAL;
  1793. strcpy(a->name, "Radio");
  1794. return 0;
  1795. }
  1796. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1797. {
  1798. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1799. if (0 != t->index)
  1800. return -EINVAL;
  1801. mutex_lock(&dev->lock);
  1802. call_all(dev, tuner, s_tuner, t);
  1803. mutex_unlock(&dev->lock);
  1804. return 0;
  1805. }
  1806. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1807. {
  1808. return 0;
  1809. }
  1810. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1811. {
  1812. return 0;
  1813. }
  1814. static int radio_queryctrl(struct file *file, void *priv,
  1815. struct v4l2_queryctrl *c)
  1816. {
  1817. int i;
  1818. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1819. return -EINVAL;
  1820. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1821. for (i = 0; i < CX231XX_CTLS; i++) {
  1822. if (cx231xx_ctls[i].v.id == c->id)
  1823. break;
  1824. }
  1825. if (i == CX231XX_CTLS)
  1826. return -EINVAL;
  1827. *c = cx231xx_ctls[i].v;
  1828. } else
  1829. *c = no_ctl;
  1830. return 0;
  1831. }
  1832. /*
  1833. * cx231xx_v4l2_open()
  1834. * inits the device and starts isoc transfer
  1835. */
  1836. static int cx231xx_v4l2_open(struct file *filp)
  1837. {
  1838. int errCode = 0, radio = 0;
  1839. struct video_device *vdev = video_devdata(filp);
  1840. struct cx231xx *dev = video_drvdata(filp);
  1841. struct cx231xx_fh *fh;
  1842. enum v4l2_buf_type fh_type = 0;
  1843. switch (vdev->vfl_type) {
  1844. case VFL_TYPE_GRABBER:
  1845. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1846. break;
  1847. case VFL_TYPE_VBI:
  1848. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1849. break;
  1850. case VFL_TYPE_RADIO:
  1851. radio = 1;
  1852. break;
  1853. }
  1854. mutex_lock(&dev->lock);
  1855. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1856. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1857. dev->users);
  1858. #if 0
  1859. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1860. if (errCode < 0) {
  1861. cx231xx_errdev
  1862. ("Device locked on digital mode. Can't open analog\n");
  1863. mutex_unlock(&dev->lock);
  1864. return -EBUSY;
  1865. }
  1866. #endif
  1867. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1868. if (!fh) {
  1869. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1870. mutex_unlock(&dev->lock);
  1871. return -ENOMEM;
  1872. }
  1873. fh->dev = dev;
  1874. fh->radio = radio;
  1875. fh->type = fh_type;
  1876. filp->private_data = fh;
  1877. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1878. dev->width = norm_maxw(dev);
  1879. dev->height = norm_maxh(dev);
  1880. dev->hscale = 0;
  1881. dev->vscale = 0;
  1882. /* Power up in Analog TV mode */
  1883. if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER)
  1884. cx231xx_set_power_mode(dev,
  1885. POLARIS_AVMODE_ENXTERNAL_AV);
  1886. else
  1887. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1888. #if 0
  1889. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1890. #endif
  1891. cx231xx_resolution_set(dev);
  1892. /* set video alternate setting */
  1893. cx231xx_set_video_alternate(dev);
  1894. /* Needed, since GPIO might have disabled power of
  1895. some i2c device */
  1896. cx231xx_config_i2c(dev);
  1897. /* device needs to be initialized before isoc transfer */
  1898. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1899. }
  1900. if (fh->radio) {
  1901. cx231xx_videodbg("video_open: setting radio device\n");
  1902. /* cx231xx_start_radio(dev); */
  1903. call_all(dev, tuner, s_radio);
  1904. }
  1905. dev->users++;
  1906. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1907. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1908. NULL, &dev->video_mode.slock,
  1909. fh->type, V4L2_FIELD_INTERLACED,
  1910. sizeof(struct cx231xx_buffer),
  1911. fh, NULL);
  1912. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1913. /* Set the required alternate setting VBI interface works in
  1914. Bulk mode only */
  1915. if (dev->model != CX231XX_BOARD_CNXT_VIDEO_GRABBER)
  1916. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1917. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1918. NULL, &dev->vbi_mode.slock,
  1919. fh->type, V4L2_FIELD_SEQ_TB,
  1920. sizeof(struct cx231xx_buffer),
  1921. fh, NULL);
  1922. }
  1923. mutex_unlock(&dev->lock);
  1924. return errCode;
  1925. }
  1926. /*
  1927. * cx231xx_realease_resources()
  1928. * unregisters the v4l2,i2c and usb devices
  1929. * called when the device gets disconected or at module unload
  1930. */
  1931. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1932. {
  1933. /*FIXME: I2C IR should be disconnected */
  1934. if (dev->radio_dev) {
  1935. if (video_is_registered(dev->radio_dev))
  1936. video_unregister_device(dev->radio_dev);
  1937. else
  1938. video_device_release(dev->radio_dev);
  1939. dev->radio_dev = NULL;
  1940. }
  1941. if (dev->vbi_dev) {
  1942. cx231xx_info("V4L2 device %s deregistered\n",
  1943. video_device_node_name(dev->vbi_dev));
  1944. if (video_is_registered(dev->vbi_dev))
  1945. video_unregister_device(dev->vbi_dev);
  1946. else
  1947. video_device_release(dev->vbi_dev);
  1948. dev->vbi_dev = NULL;
  1949. }
  1950. if (dev->vdev) {
  1951. cx231xx_info("V4L2 device %s deregistered\n",
  1952. video_device_node_name(dev->vdev));
  1953. if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER)
  1954. cx231xx_417_unregister(dev);
  1955. if (video_is_registered(dev->vdev))
  1956. video_unregister_device(dev->vdev);
  1957. else
  1958. video_device_release(dev->vdev);
  1959. dev->vdev = NULL;
  1960. }
  1961. }
  1962. /*
  1963. * cx231xx_v4l2_close()
  1964. * stops streaming and deallocates all resources allocated by the v4l2
  1965. * calls and ioctls
  1966. */
  1967. static int cx231xx_v4l2_close(struct file *filp)
  1968. {
  1969. struct cx231xx_fh *fh = filp->private_data;
  1970. struct cx231xx *dev = fh->dev;
  1971. cx231xx_videodbg("users=%d\n", dev->users);
  1972. mutex_lock(&dev->lock);
  1973. cx231xx_videodbg("users=%d\n", dev->users);
  1974. if (res_check(fh))
  1975. res_free(fh);
  1976. /*To workaround error number=-71 on EP0 for VideoGrabber,
  1977. need exclude following.*/
  1978. if (dev->model != CX231XX_BOARD_CNXT_VIDEO_GRABBER)
  1979. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1980. videobuf_stop(&fh->vb_vidq);
  1981. videobuf_mmap_free(&fh->vb_vidq);
  1982. /* the device is already disconnect,
  1983. free the remaining resources */
  1984. if (dev->state & DEV_DISCONNECTED) {
  1985. if (atomic_read(&dev->devlist_count) > 0) {
  1986. cx231xx_release_resources(dev);
  1987. mutex_unlock(&dev->lock);
  1988. kfree(dev);
  1989. dev = NULL;
  1990. return 0;
  1991. }
  1992. mutex_unlock(&dev->lock);
  1993. return 0;
  1994. }
  1995. /* do this before setting alternate! */
  1996. cx231xx_uninit_vbi_isoc(dev);
  1997. /* set alternate 0 */
  1998. if (!dev->vbi_or_sliced_cc_mode)
  1999. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  2000. else
  2001. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  2002. kfree(fh);
  2003. dev->users--;
  2004. wake_up_interruptible_nr(&dev->open, 1);
  2005. mutex_unlock(&dev->lock);
  2006. return 0;
  2007. }
  2008. if (dev->users == 1) {
  2009. videobuf_stop(&fh->vb_vidq);
  2010. videobuf_mmap_free(&fh->vb_vidq);
  2011. /* the device is already disconnect,
  2012. free the remaining resources */
  2013. if (dev->state & DEV_DISCONNECTED) {
  2014. cx231xx_release_resources(dev);
  2015. mutex_unlock(&dev->lock);
  2016. kfree(dev);
  2017. dev = NULL;
  2018. return 0;
  2019. }
  2020. /* Save some power by putting tuner to sleep */
  2021. call_all(dev, core, s_power, 0);
  2022. /* do this before setting alternate! */
  2023. if (dev->USE_ISO)
  2024. cx231xx_uninit_isoc(dev);
  2025. else
  2026. cx231xx_uninit_bulk(dev);
  2027. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  2028. /* set alternate 0 */
  2029. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  2030. }
  2031. kfree(fh);
  2032. dev->users--;
  2033. wake_up_interruptible_nr(&dev->open, 1);
  2034. mutex_unlock(&dev->lock);
  2035. return 0;
  2036. }
  2037. /*
  2038. * cx231xx_v4l2_read()
  2039. * will allocate buffers when called for the first time
  2040. */
  2041. static ssize_t
  2042. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  2043. loff_t *pos)
  2044. {
  2045. struct cx231xx_fh *fh = filp->private_data;
  2046. struct cx231xx *dev = fh->dev;
  2047. int rc;
  2048. rc = check_dev(dev);
  2049. if (rc < 0)
  2050. return rc;
  2051. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  2052. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  2053. mutex_lock(&dev->lock);
  2054. rc = res_get(fh);
  2055. mutex_unlock(&dev->lock);
  2056. if (unlikely(rc < 0))
  2057. return rc;
  2058. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  2059. filp->f_flags & O_NONBLOCK);
  2060. }
  2061. return 0;
  2062. }
  2063. /*
  2064. * cx231xx_v4l2_poll()
  2065. * will allocate buffers when called for the first time
  2066. */
  2067. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  2068. {
  2069. struct cx231xx_fh *fh = filp->private_data;
  2070. struct cx231xx *dev = fh->dev;
  2071. int rc;
  2072. rc = check_dev(dev);
  2073. if (rc < 0)
  2074. return rc;
  2075. mutex_lock(&dev->lock);
  2076. rc = res_get(fh);
  2077. mutex_unlock(&dev->lock);
  2078. if (unlikely(rc < 0))
  2079. return POLLERR;
  2080. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  2081. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  2082. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  2083. else
  2084. return POLLERR;
  2085. }
  2086. /*
  2087. * cx231xx_v4l2_mmap()
  2088. */
  2089. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  2090. {
  2091. struct cx231xx_fh *fh = filp->private_data;
  2092. struct cx231xx *dev = fh->dev;
  2093. int rc;
  2094. rc = check_dev(dev);
  2095. if (rc < 0)
  2096. return rc;
  2097. mutex_lock(&dev->lock);
  2098. rc = res_get(fh);
  2099. mutex_unlock(&dev->lock);
  2100. if (unlikely(rc < 0))
  2101. return rc;
  2102. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  2103. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  2104. (unsigned long)vma->vm_start,
  2105. (unsigned long)vma->vm_end -
  2106. (unsigned long)vma->vm_start, rc);
  2107. return rc;
  2108. }
  2109. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  2110. .owner = THIS_MODULE,
  2111. .open = cx231xx_v4l2_open,
  2112. .release = cx231xx_v4l2_close,
  2113. .read = cx231xx_v4l2_read,
  2114. .poll = cx231xx_v4l2_poll,
  2115. .mmap = cx231xx_v4l2_mmap,
  2116. .ioctl = video_ioctl2,
  2117. };
  2118. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  2119. .vidioc_querycap = vidioc_querycap,
  2120. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2121. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2122. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2123. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2124. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2125. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2126. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2127. .vidioc_g_audio = vidioc_g_audio,
  2128. .vidioc_s_audio = vidioc_s_audio,
  2129. .vidioc_cropcap = vidioc_cropcap,
  2130. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2131. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2132. .vidioc_reqbufs = vidioc_reqbufs,
  2133. .vidioc_querybuf = vidioc_querybuf,
  2134. .vidioc_qbuf = vidioc_qbuf,
  2135. .vidioc_dqbuf = vidioc_dqbuf,
  2136. .vidioc_s_std = vidioc_s_std,
  2137. .vidioc_g_std = vidioc_g_std,
  2138. .vidioc_enum_input = vidioc_enum_input,
  2139. .vidioc_g_input = vidioc_g_input,
  2140. .vidioc_s_input = vidioc_s_input,
  2141. .vidioc_queryctrl = vidioc_queryctrl,
  2142. .vidioc_g_ctrl = vidioc_g_ctrl,
  2143. .vidioc_s_ctrl = vidioc_s_ctrl,
  2144. .vidioc_streamon = vidioc_streamon,
  2145. .vidioc_streamoff = vidioc_streamoff,
  2146. .vidioc_g_tuner = vidioc_g_tuner,
  2147. .vidioc_s_tuner = vidioc_s_tuner,
  2148. .vidioc_g_frequency = vidioc_g_frequency,
  2149. .vidioc_s_frequency = vidioc_s_frequency,
  2150. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2151. .vidioc_g_register = vidioc_g_register,
  2152. .vidioc_s_register = vidioc_s_register,
  2153. #endif
  2154. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2155. .vidiocgmbuf = vidiocgmbuf,
  2156. #endif
  2157. };
  2158. static struct video_device cx231xx_vbi_template;
  2159. static const struct video_device cx231xx_video_template = {
  2160. .fops = &cx231xx_v4l_fops,
  2161. .release = video_device_release,
  2162. .ioctl_ops = &video_ioctl_ops,
  2163. .tvnorms = V4L2_STD_ALL,
  2164. .current_norm = V4L2_STD_PAL,
  2165. };
  2166. static const struct v4l2_file_operations radio_fops = {
  2167. .owner = THIS_MODULE,
  2168. .open = cx231xx_v4l2_open,
  2169. .release = cx231xx_v4l2_close,
  2170. .ioctl = video_ioctl2,
  2171. };
  2172. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2173. .vidioc_querycap = radio_querycap,
  2174. .vidioc_g_tuner = radio_g_tuner,
  2175. .vidioc_enum_input = radio_enum_input,
  2176. .vidioc_g_audio = radio_g_audio,
  2177. .vidioc_s_tuner = radio_s_tuner,
  2178. .vidioc_s_audio = radio_s_audio,
  2179. .vidioc_s_input = radio_s_input,
  2180. .vidioc_queryctrl = radio_queryctrl,
  2181. .vidioc_g_ctrl = vidioc_g_ctrl,
  2182. .vidioc_s_ctrl = vidioc_s_ctrl,
  2183. .vidioc_g_frequency = vidioc_g_frequency,
  2184. .vidioc_s_frequency = vidioc_s_frequency,
  2185. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2186. .vidioc_g_register = vidioc_g_register,
  2187. .vidioc_s_register = vidioc_s_register,
  2188. #endif
  2189. };
  2190. static struct video_device cx231xx_radio_template = {
  2191. .name = "cx231xx-radio",
  2192. .fops = &radio_fops,
  2193. .ioctl_ops = &radio_ioctl_ops,
  2194. };
  2195. /******************************** usb interface ******************************/
  2196. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  2197. const struct video_device
  2198. *template, const char *type_name)
  2199. {
  2200. struct video_device *vfd;
  2201. vfd = video_device_alloc();
  2202. if (NULL == vfd)
  2203. return NULL;
  2204. *vfd = *template;
  2205. vfd->v4l2_dev = &dev->v4l2_dev;
  2206. vfd->release = video_device_release;
  2207. vfd->debug = video_debug;
  2208. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  2209. video_set_drvdata(vfd, dev);
  2210. return vfd;
  2211. }
  2212. int cx231xx_register_analog_devices(struct cx231xx *dev)
  2213. {
  2214. int ret;
  2215. cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
  2216. dev->name,
  2217. (CX231XX_VERSION_CODE >> 16) & 0xff,
  2218. (CX231XX_VERSION_CODE >> 8) & 0xff,
  2219. CX231XX_VERSION_CODE & 0xff);
  2220. /* set default norm */
  2221. /*dev->norm = cx231xx_video_template.current_norm; */
  2222. dev->width = norm_maxw(dev);
  2223. dev->height = norm_maxh(dev);
  2224. dev->interlaced = 0;
  2225. dev->hscale = 0;
  2226. dev->vscale = 0;
  2227. /* Analog specific initialization */
  2228. dev->format = &format[0];
  2229. /* video_mux(dev, dev->video_input); */
  2230. /* Audio defaults */
  2231. dev->mute = 1;
  2232. dev->volume = 0x1f;
  2233. /* enable vbi capturing */
  2234. /* write code here... */
  2235. /* allocate and fill video video_device struct */
  2236. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  2237. if (!dev->vdev) {
  2238. cx231xx_errdev("cannot allocate video_device.\n");
  2239. return -ENODEV;
  2240. }
  2241. /* register v4l2 video video_device */
  2242. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2243. video_nr[dev->devno]);
  2244. if (ret) {
  2245. cx231xx_errdev("unable to register video device (error=%i).\n",
  2246. ret);
  2247. return ret;
  2248. }
  2249. cx231xx_info("%s/0: registered device %s [v4l2]\n",
  2250. dev->name, video_device_node_name(dev->vdev));
  2251. /* Initialize VBI template */
  2252. memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
  2253. sizeof(cx231xx_vbi_template));
  2254. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  2255. /* Allocate and fill vbi video_device struct */
  2256. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  2257. /* register v4l2 vbi video_device */
  2258. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2259. vbi_nr[dev->devno]);
  2260. if (ret < 0) {
  2261. cx231xx_errdev("unable to register vbi device\n");
  2262. return ret;
  2263. }
  2264. cx231xx_info("%s/0: registered device %s\n",
  2265. dev->name, video_device_node_name(dev->vbi_dev));
  2266. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  2267. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  2268. "radio");
  2269. if (!dev->radio_dev) {
  2270. cx231xx_errdev("cannot allocate video_device.\n");
  2271. return -ENODEV;
  2272. }
  2273. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2274. radio_nr[dev->devno]);
  2275. if (ret < 0) {
  2276. cx231xx_errdev("can't register radio device\n");
  2277. return ret;
  2278. }
  2279. cx231xx_info("Registered radio device as %s\n",
  2280. video_device_node_name(dev->radio_dev));
  2281. }
  2282. cx231xx_info("V4L2 device registered as %s and %s\n",
  2283. video_device_node_name(dev->vdev),
  2284. video_device_node_name(dev->vbi_dev));
  2285. return 0;
  2286. }