cx231xx-video.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  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. dev->video_mode.isoc_ctl.buf = NULL;
  213. list_del(&buf->vb.queue);
  214. wake_up(&buf->vb.done);
  215. }
  216. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  217. {
  218. char *errmsg = "Unknown";
  219. switch (status) {
  220. case -ENOENT:
  221. errmsg = "unlinked synchronuously";
  222. break;
  223. case -ECONNRESET:
  224. errmsg = "unlinked asynchronuously";
  225. break;
  226. case -ENOSR:
  227. errmsg = "Buffer error (overrun)";
  228. break;
  229. case -EPIPE:
  230. errmsg = "Stalled (device not responding)";
  231. break;
  232. case -EOVERFLOW:
  233. errmsg = "Babble (bad cable?)";
  234. break;
  235. case -EPROTO:
  236. errmsg = "Bit-stuff error (bad cable?)";
  237. break;
  238. case -EILSEQ:
  239. errmsg = "CRC/Timeout (could be anything)";
  240. break;
  241. case -ETIME:
  242. errmsg = "Device does not respond";
  243. break;
  244. }
  245. if (packet < 0) {
  246. cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  247. } else {
  248. cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
  249. packet, status, errmsg);
  250. }
  251. }
  252. /*
  253. * video-buf generic routine to get the next available buffer
  254. */
  255. static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
  256. struct cx231xx_buffer **buf)
  257. {
  258. struct cx231xx_video_mode *vmode =
  259. container_of(dma_q, struct cx231xx_video_mode, vidq);
  260. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  261. char *outp;
  262. if (list_empty(&dma_q->active)) {
  263. cx231xx_isocdbg("No active queue to serve\n");
  264. dev->video_mode.isoc_ctl.buf = NULL;
  265. *buf = NULL;
  266. return;
  267. }
  268. /* Get the next buffer */
  269. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  270. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  271. outp = videobuf_to_vmalloc(&(*buf)->vb);
  272. memset(outp, 0, (*buf)->vb.size);
  273. dev->video_mode.isoc_ctl.buf = *buf;
  274. return;
  275. }
  276. /*
  277. * Controls the isoc copy of each urb packet
  278. */
  279. static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  280. {
  281. struct cx231xx_buffer *buf;
  282. struct cx231xx_dmaqueue *dma_q = urb->context;
  283. unsigned char *outp = NULL;
  284. int i, rc = 1;
  285. unsigned char *p_buffer;
  286. u32 bytes_parsed = 0, buffer_size = 0;
  287. u8 sav_eav = 0;
  288. if (!dev)
  289. return 0;
  290. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  291. return 0;
  292. if (urb->status < 0) {
  293. print_err_status(dev, -1, urb->status);
  294. if (urb->status == -ENOENT)
  295. return 0;
  296. }
  297. buf = dev->video_mode.isoc_ctl.buf;
  298. if (buf != NULL)
  299. outp = videobuf_to_vmalloc(&buf->vb);
  300. for (i = 0; i < urb->number_of_packets; i++) {
  301. int status = urb->iso_frame_desc[i].status;
  302. if (status < 0) {
  303. print_err_status(dev, i, status);
  304. if (urb->iso_frame_desc[i].status != -EPROTO)
  305. continue;
  306. }
  307. if (urb->iso_frame_desc[i].actual_length <= 0) {
  308. /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
  309. continue;
  310. }
  311. if (urb->iso_frame_desc[i].actual_length >
  312. dev->video_mode.max_pkt_size) {
  313. cx231xx_isocdbg("packet bigger than packet size");
  314. continue;
  315. }
  316. /* get buffer pointer and length */
  317. p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  318. buffer_size = urb->iso_frame_desc[i].actual_length;
  319. bytes_parsed = 0;
  320. if (dma_q->is_partial_line) {
  321. /* Handle the case of a partial line */
  322. sav_eav = dma_q->last_sav;
  323. } else {
  324. /* Check for a SAV/EAV overlapping
  325. the buffer boundary */
  326. sav_eav =
  327. cx231xx_find_boundary_SAV_EAV(p_buffer,
  328. dma_q->partial_buf,
  329. &bytes_parsed);
  330. }
  331. sav_eav &= 0xF0;
  332. /* Get the first line if we have some portion of an SAV/EAV from
  333. the last buffer or a partial line */
  334. if (sav_eav) {
  335. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  336. sav_eav, /* SAV/EAV */
  337. p_buffer + bytes_parsed, /* p_buffer */
  338. buffer_size - bytes_parsed);/* buf size */
  339. }
  340. /* Now parse data that is completely in this buffer */
  341. /* dma_q->is_partial_line = 0; */
  342. while (bytes_parsed < buffer_size) {
  343. u32 bytes_used = 0;
  344. sav_eav = cx231xx_find_next_SAV_EAV(
  345. p_buffer + bytes_parsed, /* p_buffer */
  346. buffer_size - bytes_parsed, /* buf size */
  347. &bytes_used);/* bytes used to get SAV/EAV */
  348. bytes_parsed += bytes_used;
  349. sav_eav &= 0xF0;
  350. if (sav_eav && (bytes_parsed < buffer_size)) {
  351. bytes_parsed += cx231xx_get_video_line(dev,
  352. dma_q, sav_eav, /* SAV/EAV */
  353. p_buffer + bytes_parsed,/* p_buffer */
  354. buffer_size - bytes_parsed);/*buf size*/
  355. }
  356. }
  357. /* Save the last four bytes of the buffer so we can check the
  358. buffer boundary condition next time */
  359. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  360. bytes_parsed = 0;
  361. }
  362. return rc;
  363. }
  364. u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
  365. u32 *p_bytes_used)
  366. {
  367. u32 bytes_used;
  368. u8 boundary_bytes[8];
  369. u8 sav_eav = 0;
  370. *p_bytes_used = 0;
  371. /* Create an array of the last 4 bytes of the last buffer and the first
  372. 4 bytes of the current buffer. */
  373. memcpy(boundary_bytes, partial_buf, 4);
  374. memcpy(boundary_bytes + 4, p_buffer, 4);
  375. /* Check for the SAV/EAV in the boundary buffer */
  376. sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
  377. &bytes_used);
  378. if (sav_eav) {
  379. /* found a boundary SAV/EAV. Updates the bytes used to reflect
  380. only those used in the new buffer */
  381. *p_bytes_used = bytes_used - 4;
  382. }
  383. return sav_eav;
  384. }
  385. u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
  386. {
  387. u32 i;
  388. u8 sav_eav = 0;
  389. /*
  390. * Don't search if the buffer size is less than 4. It causes a page
  391. * fault since buffer_size - 4 evaluates to a large number in that
  392. * case.
  393. */
  394. if (buffer_size < 4) {
  395. *p_bytes_used = buffer_size;
  396. return 0;
  397. }
  398. for (i = 0; i < (buffer_size - 3); i++) {
  399. if ((p_buffer[i] == 0xFF) &&
  400. (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
  401. *p_bytes_used = i + 4;
  402. sav_eav = p_buffer[i + 3];
  403. return sav_eav;
  404. }
  405. }
  406. *p_bytes_used = buffer_size;
  407. return 0;
  408. }
  409. u32 cx231xx_get_video_line(struct cx231xx *dev,
  410. struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
  411. u8 *p_buffer, u32 buffer_size)
  412. {
  413. u32 bytes_copied = 0;
  414. int current_field = -1;
  415. switch (sav_eav) {
  416. case SAV_ACTIVE_VIDEO_FIELD1:
  417. /* looking for skipped line which occurred in PAL 720x480 mode.
  418. In this case, there will be no active data contained
  419. between the SAV and EAV */
  420. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  421. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  422. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  423. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  424. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  425. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  426. return bytes_copied;
  427. current_field = 1;
  428. break;
  429. case SAV_ACTIVE_VIDEO_FIELD2:
  430. /* looking for skipped line which occurred in PAL 720x480 mode.
  431. In this case, there will be no active data contained between
  432. the SAV and EAV */
  433. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  434. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  435. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  436. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  437. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  438. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  439. return bytes_copied;
  440. current_field = 2;
  441. break;
  442. }
  443. dma_q->last_sav = sav_eav;
  444. bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
  445. buffer_size, current_field);
  446. return bytes_copied;
  447. }
  448. u32 cx231xx_copy_video_line(struct cx231xx *dev,
  449. struct cx231xx_dmaqueue *dma_q, u8 *p_line,
  450. u32 length, int field_number)
  451. {
  452. u32 bytes_to_copy;
  453. struct cx231xx_buffer *buf;
  454. u32 _line_size = dev->width * 2;
  455. if (dma_q->current_field != field_number)
  456. cx231xx_reset_video_buffer(dev, dma_q);
  457. /* get the buffer pointer */
  458. buf = dev->video_mode.isoc_ctl.buf;
  459. /* Remember the field number for next time */
  460. dma_q->current_field = field_number;
  461. bytes_to_copy = dma_q->bytes_left_in_line;
  462. if (bytes_to_copy > length)
  463. bytes_to_copy = length;
  464. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  465. dma_q->bytes_left_in_line -= bytes_to_copy;
  466. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
  467. 0 : 1;
  468. return 0;
  469. }
  470. dma_q->is_partial_line = 1;
  471. /* If we don't have a buffer, just return the number of bytes we would
  472. have copied if we had a buffer. */
  473. if (!buf) {
  474. dma_q->bytes_left_in_line -= bytes_to_copy;
  475. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
  476. ? 0 : 1;
  477. return bytes_to_copy;
  478. }
  479. /* copy the data to video buffer */
  480. cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
  481. dma_q->pos += bytes_to_copy;
  482. dma_q->bytes_left_in_line -= bytes_to_copy;
  483. if (dma_q->bytes_left_in_line == 0) {
  484. dma_q->bytes_left_in_line = _line_size;
  485. dma_q->lines_completed++;
  486. dma_q->is_partial_line = 0;
  487. if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
  488. buffer_filled(dev, dma_q, buf);
  489. dma_q->pos = 0;
  490. buf = NULL;
  491. dma_q->lines_completed = 0;
  492. }
  493. }
  494. return bytes_to_copy;
  495. }
  496. void cx231xx_reset_video_buffer(struct cx231xx *dev,
  497. struct cx231xx_dmaqueue *dma_q)
  498. {
  499. struct cx231xx_buffer *buf;
  500. /* handle the switch from field 1 to field 2 */
  501. if (dma_q->current_field == 1) {
  502. if (dma_q->lines_completed >= dma_q->lines_per_field)
  503. dma_q->field1_done = 1;
  504. else
  505. dma_q->field1_done = 0;
  506. }
  507. buf = dev->video_mode.isoc_ctl.buf;
  508. if (buf == NULL) {
  509. u8 *outp = NULL;
  510. /* first try to get the buffer */
  511. get_next_buf(dma_q, &buf);
  512. if (buf)
  513. outp = videobuf_to_vmalloc(&buf->vb);
  514. dma_q->pos = 0;
  515. dma_q->field1_done = 0;
  516. dma_q->current_field = -1;
  517. }
  518. /* reset the counters */
  519. dma_q->bytes_left_in_line = dev->width << 1;
  520. dma_q->lines_completed = 0;
  521. }
  522. int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  523. u8 *p_buffer, u32 bytes_to_copy)
  524. {
  525. u8 *p_out_buffer = NULL;
  526. u32 current_line_bytes_copied = 0;
  527. struct cx231xx_buffer *buf;
  528. u32 _line_size = dev->width << 1;
  529. void *startwrite;
  530. int offset, lencopy;
  531. buf = dev->video_mode.isoc_ctl.buf;
  532. if (buf == NULL)
  533. return -1;
  534. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  535. current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
  536. /* Offset field 2 one line from the top of the buffer */
  537. offset = (dma_q->current_field == 1) ? 0 : _line_size;
  538. /* Offset for field 2 */
  539. startwrite = p_out_buffer + offset;
  540. /* lines already completed in the current field */
  541. startwrite += (dma_q->lines_completed * _line_size * 2);
  542. /* bytes already completed in the current line */
  543. startwrite += current_line_bytes_copied;
  544. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  545. bytes_to_copy : dma_q->bytes_left_in_line;
  546. if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
  547. return 0;
  548. /* The below copies the UYVY data straight into video buffer */
  549. cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
  550. return 0;
  551. }
  552. void cx231xx_swab(u16 *from, u16 *to, u16 len)
  553. {
  554. u16 i;
  555. if (len <= 0)
  556. return;
  557. for (i = 0; i < len / 2; i++)
  558. to[i] = (from[i] << 8) | (from[i] >> 8);
  559. }
  560. u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
  561. {
  562. u8 buffer_complete = 0;
  563. /* Dual field stream */
  564. buffer_complete = ((dma_q->current_field == 2) &&
  565. (dma_q->lines_completed >= dma_q->lines_per_field) &&
  566. dma_q->field1_done);
  567. return buffer_complete;
  568. }
  569. /* ------------------------------------------------------------------
  570. Videobuf operations
  571. ------------------------------------------------------------------*/
  572. static int
  573. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  574. {
  575. struct cx231xx_fh *fh = vq->priv_data;
  576. struct cx231xx *dev = fh->dev;
  577. struct v4l2_frequency f;
  578. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
  579. if (0 == *count)
  580. *count = CX231XX_DEF_BUF;
  581. if (*count < CX231XX_MIN_BUF)
  582. *count = CX231XX_MIN_BUF;
  583. /* Ask tuner to go to analog mode */
  584. memset(&f, 0, sizeof(f));
  585. f.frequency = dev->ctl_freq;
  586. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  587. call_all(dev, tuner, s_frequency, &f);
  588. return 0;
  589. }
  590. /* This is called *without* dev->slock held; please keep it that way */
  591. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  592. {
  593. struct cx231xx_fh *fh = vq->priv_data;
  594. struct cx231xx *dev = fh->dev;
  595. unsigned long flags = 0;
  596. if (in_interrupt())
  597. BUG();
  598. /* We used to wait for the buffer to finish here, but this didn't work
  599. because, as we were keeping the state as VIDEOBUF_QUEUED,
  600. videobuf_queue_cancel marked it as finished for us.
  601. (Also, it could wedge forever if the hardware was misconfigured.)
  602. This should be safe; by the time we get here, the buffer isn't
  603. queued anymore. If we ever start marking the buffers as
  604. VIDEOBUF_ACTIVE, it won't be, though.
  605. */
  606. spin_lock_irqsave(&dev->video_mode.slock, flags);
  607. if (dev->video_mode.isoc_ctl.buf == buf)
  608. dev->video_mode.isoc_ctl.buf = NULL;
  609. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  610. videobuf_vmalloc_free(&buf->vb);
  611. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  612. }
  613. static int
  614. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  615. enum v4l2_field field)
  616. {
  617. struct cx231xx_fh *fh = vq->priv_data;
  618. struct cx231xx_buffer *buf =
  619. container_of(vb, struct cx231xx_buffer, vb);
  620. struct cx231xx *dev = fh->dev;
  621. int rc = 0, urb_init = 0;
  622. /* The only currently supported format is 16 bits/pixel */
  623. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  624. + 7) >> 3;
  625. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  626. return -EINVAL;
  627. buf->vb.width = dev->width;
  628. buf->vb.height = dev->height;
  629. buf->vb.field = field;
  630. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  631. rc = videobuf_iolock(vq, &buf->vb, NULL);
  632. if (rc < 0)
  633. goto fail;
  634. }
  635. if (!dev->video_mode.isoc_ctl.num_bufs)
  636. urb_init = 1;
  637. if (urb_init) {
  638. rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  639. CX231XX_NUM_BUFS,
  640. dev->video_mode.max_pkt_size,
  641. cx231xx_isoc_copy);
  642. if (rc < 0)
  643. goto fail;
  644. }
  645. buf->vb.state = VIDEOBUF_PREPARED;
  646. return 0;
  647. fail:
  648. free_buffer(vq, buf);
  649. return rc;
  650. }
  651. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  652. {
  653. struct cx231xx_buffer *buf =
  654. container_of(vb, struct cx231xx_buffer, vb);
  655. struct cx231xx_fh *fh = vq->priv_data;
  656. struct cx231xx *dev = fh->dev;
  657. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  658. buf->vb.state = VIDEOBUF_QUEUED;
  659. list_add_tail(&buf->vb.queue, &vidq->active);
  660. }
  661. static void buffer_release(struct videobuf_queue *vq,
  662. struct videobuf_buffer *vb)
  663. {
  664. struct cx231xx_buffer *buf =
  665. container_of(vb, struct cx231xx_buffer, vb);
  666. struct cx231xx_fh *fh = vq->priv_data;
  667. struct cx231xx *dev = (struct cx231xx *)fh->dev;
  668. cx231xx_isocdbg("cx231xx: called buffer_release\n");
  669. free_buffer(vq, buf);
  670. }
  671. static struct videobuf_queue_ops cx231xx_video_qops = {
  672. .buf_setup = buffer_setup,
  673. .buf_prepare = buffer_prepare,
  674. .buf_queue = buffer_queue,
  675. .buf_release = buffer_release,
  676. };
  677. /********************* v4l2 interface **************************************/
  678. void video_mux(struct cx231xx *dev, int index)
  679. {
  680. dev->video_input = index;
  681. dev->ctl_ainput = INPUT(index)->amux;
  682. cx231xx_set_video_input_mux(dev, index);
  683. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  684. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  685. cx231xx_info("video_mux : %d\n", index);
  686. /* do mode control overrides if required */
  687. cx231xx_do_mode_ctrl_overrides(dev);
  688. }
  689. /* Usage lock check functions */
  690. static int res_get(struct cx231xx_fh *fh)
  691. {
  692. struct cx231xx *dev = fh->dev;
  693. int rc = 0;
  694. /* This instance already has stream_on */
  695. if (fh->stream_on)
  696. return rc;
  697. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  698. if (dev->stream_on)
  699. return -EBUSY;
  700. dev->stream_on = 1;
  701. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  702. if (dev->vbi_stream_on)
  703. return -EBUSY;
  704. dev->vbi_stream_on = 1;
  705. } else
  706. return -EINVAL;
  707. fh->stream_on = 1;
  708. return rc;
  709. }
  710. static int res_check(struct cx231xx_fh *fh)
  711. {
  712. return fh->stream_on;
  713. }
  714. static void res_free(struct cx231xx_fh *fh)
  715. {
  716. struct cx231xx *dev = fh->dev;
  717. fh->stream_on = 0;
  718. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  719. dev->stream_on = 0;
  720. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  721. dev->vbi_stream_on = 0;
  722. }
  723. static int check_dev(struct cx231xx *dev)
  724. {
  725. if (dev->state & DEV_DISCONNECTED) {
  726. cx231xx_errdev("v4l2 ioctl: device not present\n");
  727. return -ENODEV;
  728. }
  729. if (dev->state & DEV_MISCONFIGURED) {
  730. cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
  731. "close and open it again\n");
  732. return -EIO;
  733. }
  734. return 0;
  735. }
  736. static void get_scale(struct cx231xx *dev,
  737. unsigned int width, unsigned int height,
  738. unsigned int *hscale, unsigned int *vscale)
  739. {
  740. unsigned int maxw = norm_maxw(dev);
  741. unsigned int maxh = norm_maxh(dev);
  742. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  743. if (*hscale >= 0x4000)
  744. *hscale = 0x3fff;
  745. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  746. if (*vscale >= 0x4000)
  747. *vscale = 0x3fff;
  748. }
  749. /* ------------------------------------------------------------------
  750. IOCTL vidioc handling
  751. ------------------------------------------------------------------*/
  752. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  753. struct v4l2_format *f)
  754. {
  755. struct cx231xx_fh *fh = priv;
  756. struct cx231xx *dev = fh->dev;
  757. mutex_lock(&dev->lock);
  758. f->fmt.pix.width = dev->width;
  759. f->fmt.pix.height = dev->height;
  760. f->fmt.pix.pixelformat = dev->format->fourcc;
  761. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  762. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  763. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  764. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  765. mutex_unlock(&dev->lock);
  766. return 0;
  767. }
  768. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  769. {
  770. unsigned int i;
  771. for (i = 0; i < ARRAY_SIZE(format); i++)
  772. if (format[i].fourcc == fourcc)
  773. return &format[i];
  774. return NULL;
  775. }
  776. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  777. struct v4l2_format *f)
  778. {
  779. struct cx231xx_fh *fh = priv;
  780. struct cx231xx *dev = fh->dev;
  781. unsigned int width = f->fmt.pix.width;
  782. unsigned int height = f->fmt.pix.height;
  783. unsigned int maxw = norm_maxw(dev);
  784. unsigned int maxh = norm_maxh(dev);
  785. unsigned int hscale, vscale;
  786. struct cx231xx_fmt *fmt;
  787. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  788. if (!fmt) {
  789. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  790. f->fmt.pix.pixelformat);
  791. return -EINVAL;
  792. }
  793. /* width must even because of the YUYV format
  794. height must be even because of interlacing */
  795. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  796. get_scale(dev, width, height, &hscale, &vscale);
  797. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  798. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  799. f->fmt.pix.width = width;
  800. f->fmt.pix.height = height;
  801. f->fmt.pix.pixelformat = fmt->fourcc;
  802. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  803. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  804. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  805. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  806. return 0;
  807. }
  808. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  809. struct v4l2_format *f)
  810. {
  811. struct cx231xx_fh *fh = priv;
  812. struct cx231xx *dev = fh->dev;
  813. int rc;
  814. struct cx231xx_fmt *fmt;
  815. struct v4l2_mbus_framefmt mbus_fmt;
  816. rc = check_dev(dev);
  817. if (rc < 0)
  818. return rc;
  819. mutex_lock(&dev->lock);
  820. vidioc_try_fmt_vid_cap(file, priv, f);
  821. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  822. if (!fmt) {
  823. rc = -EINVAL;
  824. goto out;
  825. }
  826. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  827. cx231xx_errdev("%s queue busy\n", __func__);
  828. rc = -EBUSY;
  829. goto out;
  830. }
  831. if (dev->stream_on && !fh->stream_on) {
  832. cx231xx_errdev("%s device in use by another fh\n", __func__);
  833. rc = -EBUSY;
  834. goto out;
  835. }
  836. /* set new image size */
  837. dev->width = f->fmt.pix.width;
  838. dev->height = f->fmt.pix.height;
  839. dev->format = fmt;
  840. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  841. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  842. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  843. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  844. /* Set the correct alternate setting for this resolution */
  845. cx231xx_resolution_set(dev);
  846. out:
  847. mutex_unlock(&dev->lock);
  848. return rc;
  849. }
  850. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
  851. {
  852. struct cx231xx_fh *fh = priv;
  853. struct cx231xx *dev = fh->dev;
  854. *id = dev->norm;
  855. return 0;
  856. }
  857. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  858. {
  859. struct cx231xx_fh *fh = priv;
  860. struct cx231xx *dev = fh->dev;
  861. struct v4l2_format f;
  862. int rc;
  863. rc = check_dev(dev);
  864. if (rc < 0)
  865. return rc;
  866. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  867. mutex_lock(&dev->lock);
  868. dev->norm = *norm;
  869. /* Adjusts width/height, if needed */
  870. f.fmt.pix.width = dev->width;
  871. f.fmt.pix.height = dev->height;
  872. vidioc_try_fmt_vid_cap(file, priv, &f);
  873. /* set new image size */
  874. dev->width = f.fmt.pix.width;
  875. dev->height = f.fmt.pix.height;
  876. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  877. call_all(dev, core, s_std, dev->norm);
  878. mutex_unlock(&dev->lock);
  879. cx231xx_resolution_set(dev);
  880. /* do mode control overrides */
  881. cx231xx_do_mode_ctrl_overrides(dev);
  882. return 0;
  883. }
  884. static const char *iname[] = {
  885. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  886. [CX231XX_VMUX_SVIDEO] = "S-Video",
  887. [CX231XX_VMUX_TELEVISION] = "Television",
  888. [CX231XX_VMUX_CABLE] = "Cable TV",
  889. [CX231XX_VMUX_DVB] = "DVB",
  890. [CX231XX_VMUX_DEBUG] = "for debug only",
  891. };
  892. static int vidioc_enum_input(struct file *file, void *priv,
  893. struct v4l2_input *i)
  894. {
  895. struct cx231xx_fh *fh = priv;
  896. struct cx231xx *dev = fh->dev;
  897. unsigned int n;
  898. n = i->index;
  899. if (n >= MAX_CX231XX_INPUT)
  900. return -EINVAL;
  901. if (0 == INPUT(n)->type)
  902. return -EINVAL;
  903. i->index = n;
  904. i->type = V4L2_INPUT_TYPE_CAMERA;
  905. strcpy(i->name, iname[INPUT(n)->type]);
  906. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  907. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  908. i->type = V4L2_INPUT_TYPE_TUNER;
  909. i->std = dev->vdev->tvnorms;
  910. return 0;
  911. }
  912. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  913. {
  914. struct cx231xx_fh *fh = priv;
  915. struct cx231xx *dev = fh->dev;
  916. *i = dev->video_input;
  917. return 0;
  918. }
  919. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  920. {
  921. struct cx231xx_fh *fh = priv;
  922. struct cx231xx *dev = fh->dev;
  923. int rc;
  924. rc = check_dev(dev);
  925. if (rc < 0)
  926. return rc;
  927. if (i >= MAX_CX231XX_INPUT)
  928. return -EINVAL;
  929. if (0 == INPUT(i)->type)
  930. return -EINVAL;
  931. mutex_lock(&dev->lock);
  932. video_mux(dev, i);
  933. mutex_unlock(&dev->lock);
  934. return 0;
  935. }
  936. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  937. {
  938. struct cx231xx_fh *fh = priv;
  939. struct cx231xx *dev = fh->dev;
  940. switch (a->index) {
  941. case CX231XX_AMUX_VIDEO:
  942. strcpy(a->name, "Television");
  943. break;
  944. case CX231XX_AMUX_LINE_IN:
  945. strcpy(a->name, "Line In");
  946. break;
  947. default:
  948. return -EINVAL;
  949. }
  950. a->index = dev->ctl_ainput;
  951. a->capability = V4L2_AUDCAP_STEREO;
  952. return 0;
  953. }
  954. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  955. {
  956. struct cx231xx_fh *fh = priv;
  957. struct cx231xx *dev = fh->dev;
  958. int status = 0;
  959. /* Doesn't allow manual routing */
  960. if (a->index != dev->ctl_ainput)
  961. return -EINVAL;
  962. dev->ctl_ainput = INPUT(a->index)->amux;
  963. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  964. return status;
  965. }
  966. static int vidioc_queryctrl(struct file *file, void *priv,
  967. struct v4l2_queryctrl *qc)
  968. {
  969. struct cx231xx_fh *fh = priv;
  970. struct cx231xx *dev = fh->dev;
  971. int id = qc->id;
  972. int i;
  973. int rc;
  974. rc = check_dev(dev);
  975. if (rc < 0)
  976. return rc;
  977. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  978. if (unlikely(qc->id == 0))
  979. return -EINVAL;
  980. memset(qc, 0, sizeof(*qc));
  981. qc->id = id;
  982. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  983. return -EINVAL;
  984. for (i = 0; i < CX231XX_CTLS; i++)
  985. if (cx231xx_ctls[i].v.id == qc->id)
  986. break;
  987. if (i == CX231XX_CTLS) {
  988. *qc = no_ctl;
  989. return 0;
  990. }
  991. *qc = cx231xx_ctls[i].v;
  992. mutex_lock(&dev->lock);
  993. call_all(dev, core, queryctrl, qc);
  994. mutex_unlock(&dev->lock);
  995. if (qc->type)
  996. return 0;
  997. else
  998. return -EINVAL;
  999. }
  1000. static int vidioc_g_ctrl(struct file *file, void *priv,
  1001. struct v4l2_control *ctrl)
  1002. {
  1003. struct cx231xx_fh *fh = priv;
  1004. struct cx231xx *dev = fh->dev;
  1005. int rc;
  1006. rc = check_dev(dev);
  1007. if (rc < 0)
  1008. return rc;
  1009. mutex_lock(&dev->lock);
  1010. call_all(dev, core, g_ctrl, ctrl);
  1011. mutex_unlock(&dev->lock);
  1012. return rc;
  1013. }
  1014. static int vidioc_s_ctrl(struct file *file, void *priv,
  1015. struct v4l2_control *ctrl)
  1016. {
  1017. struct cx231xx_fh *fh = priv;
  1018. struct cx231xx *dev = fh->dev;
  1019. int rc;
  1020. rc = check_dev(dev);
  1021. if (rc < 0)
  1022. return rc;
  1023. mutex_lock(&dev->lock);
  1024. call_all(dev, core, s_ctrl, ctrl);
  1025. mutex_unlock(&dev->lock);
  1026. return rc;
  1027. }
  1028. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1029. {
  1030. struct cx231xx_fh *fh = priv;
  1031. struct cx231xx *dev = fh->dev;
  1032. int rc;
  1033. rc = check_dev(dev);
  1034. if (rc < 0)
  1035. return rc;
  1036. if (0 != t->index)
  1037. return -EINVAL;
  1038. strcpy(t->name, "Tuner");
  1039. t->type = V4L2_TUNER_ANALOG_TV;
  1040. t->capability = V4L2_TUNER_CAP_NORM;
  1041. t->rangehigh = 0xffffffffUL;
  1042. t->signal = 0xffff; /* LOCKED */
  1043. return 0;
  1044. }
  1045. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1046. {
  1047. struct cx231xx_fh *fh = priv;
  1048. struct cx231xx *dev = fh->dev;
  1049. int rc;
  1050. rc = check_dev(dev);
  1051. if (rc < 0)
  1052. return rc;
  1053. if (0 != t->index)
  1054. return -EINVAL;
  1055. #if 0
  1056. mutex_lock(&dev->lock);
  1057. call_all(dev, tuner, s_tuner, t);
  1058. mutex_unlock(&dev->lock);
  1059. #endif
  1060. return 0;
  1061. }
  1062. static int vidioc_g_frequency(struct file *file, void *priv,
  1063. struct v4l2_frequency *f)
  1064. {
  1065. struct cx231xx_fh *fh = priv;
  1066. struct cx231xx *dev = fh->dev;
  1067. mutex_lock(&dev->lock);
  1068. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1069. f->frequency = dev->ctl_freq;
  1070. call_all(dev, tuner, g_frequency, f);
  1071. mutex_unlock(&dev->lock);
  1072. return 0;
  1073. }
  1074. static int vidioc_s_frequency(struct file *file, void *priv,
  1075. struct v4l2_frequency *f)
  1076. {
  1077. struct cx231xx_fh *fh = priv;
  1078. struct cx231xx *dev = fh->dev;
  1079. int rc;
  1080. rc = check_dev(dev);
  1081. if (rc < 0)
  1082. return rc;
  1083. if (0 != f->tuner)
  1084. return -EINVAL;
  1085. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1086. return -EINVAL;
  1087. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1088. return -EINVAL;
  1089. /* set pre channel change settings in DIF first */
  1090. rc = cx231xx_tuner_pre_channel_change(dev);
  1091. mutex_lock(&dev->lock);
  1092. dev->ctl_freq = f->frequency;
  1093. if (dev->tuner_type == TUNER_XC5000) {
  1094. if (dev->cx231xx_set_analog_freq != NULL)
  1095. dev->cx231xx_set_analog_freq(dev, f->frequency);
  1096. } else
  1097. call_all(dev, tuner, s_frequency, f);
  1098. mutex_unlock(&dev->lock);
  1099. /* set post channel change settings in DIF first */
  1100. rc = cx231xx_tuner_post_channel_change(dev);
  1101. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1102. return rc;
  1103. }
  1104. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1105. /*
  1106. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1107. chip=<chip>[,min=<addr>,max=<addr>]
  1108. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1109. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1110. chip=<chip>,reg=<addr>,val=<val>
  1111. set the register [VIDIOC_DBG_S_REGISTER]
  1112. if type == host, then <chip> is the hosts chip ID (default 0)
  1113. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1114. if type == i2caddr, then <chip> is the 7-bit I2C address
  1115. */
  1116. static int vidioc_g_register(struct file *file, void *priv,
  1117. struct v4l2_dbg_register *reg)
  1118. {
  1119. struct cx231xx_fh *fh = priv;
  1120. struct cx231xx *dev = fh->dev;
  1121. int ret = 0;
  1122. u8 value[4] = { 0, 0, 0, 0 };
  1123. u32 data = 0;
  1124. switch (reg->match.type) {
  1125. case V4L2_CHIP_MATCH_HOST:
  1126. switch (reg->match.addr) {
  1127. case 0: /* Cx231xx - internal registers */
  1128. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1129. (u16)reg->reg, value, 4);
  1130. reg->val = value[0] | value[1] << 8 |
  1131. value[2] << 16 | value[3] << 24;
  1132. break;
  1133. case 1: /* AFE - read byte */
  1134. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1135. (u16)reg->reg, 2, &data, 1);
  1136. reg->val = le32_to_cpu(data & 0xff);
  1137. break;
  1138. case 14: /* AFE - read dword */
  1139. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1140. (u16)reg->reg, 2, &data, 4);
  1141. reg->val = le32_to_cpu(data);
  1142. break;
  1143. case 2: /* Video Block - read byte */
  1144. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1145. (u16)reg->reg, 2, &data, 1);
  1146. reg->val = le32_to_cpu(data & 0xff);
  1147. break;
  1148. case 24: /* Video Block - read dword */
  1149. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1150. (u16)reg->reg, 2, &data, 4);
  1151. reg->val = le32_to_cpu(data);
  1152. break;
  1153. case 3: /* I2S block - read byte */
  1154. ret = cx231xx_read_i2c_data(dev,
  1155. I2S_BLK_DEVICE_ADDRESS,
  1156. (u16)reg->reg, 1,
  1157. &data, 1);
  1158. reg->val = le32_to_cpu(data & 0xff);
  1159. break;
  1160. case 34: /* I2S Block - read dword */
  1161. ret =
  1162. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1163. (u16)reg->reg, 1, &data, 4);
  1164. reg->val = le32_to_cpu(data);
  1165. break;
  1166. }
  1167. return ret < 0 ? ret : 0;
  1168. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1169. call_all(dev, core, g_register, reg);
  1170. return 0;
  1171. case V4L2_CHIP_MATCH_I2C_ADDR:
  1172. /* Not supported yet */
  1173. return -EINVAL;
  1174. default:
  1175. if (!v4l2_chip_match_host(&reg->match))
  1176. return -EINVAL;
  1177. }
  1178. mutex_lock(&dev->lock);
  1179. call_all(dev, core, g_register, reg);
  1180. mutex_unlock(&dev->lock);
  1181. return ret;
  1182. }
  1183. static int vidioc_s_register(struct file *file, void *priv,
  1184. struct v4l2_dbg_register *reg)
  1185. {
  1186. struct cx231xx_fh *fh = priv;
  1187. struct cx231xx *dev = fh->dev;
  1188. int ret = 0;
  1189. __le64 buf;
  1190. u32 value;
  1191. u8 data[4] = { 0, 0, 0, 0 };
  1192. buf = cpu_to_le64(reg->val);
  1193. switch (reg->match.type) {
  1194. case V4L2_CHIP_MATCH_HOST:
  1195. {
  1196. value = (u32) buf & 0xffffffff;
  1197. switch (reg->match.addr) {
  1198. case 0: /* cx231xx internal registers */
  1199. data[0] = (u8) value;
  1200. data[1] = (u8) (value >> 8);
  1201. data[2] = (u8) (value >> 16);
  1202. data[3] = (u8) (value >> 24);
  1203. ret = cx231xx_write_ctrl_reg(dev,
  1204. VRT_SET_REGISTER,
  1205. (u16)reg->reg, data,
  1206. 4);
  1207. break;
  1208. case 1: /* AFE - read byte */
  1209. ret = cx231xx_write_i2c_data(dev,
  1210. AFE_DEVICE_ADDRESS,
  1211. (u16)reg->reg, 2,
  1212. value, 1);
  1213. break;
  1214. case 14: /* AFE - read dword */
  1215. ret = cx231xx_write_i2c_data(dev,
  1216. AFE_DEVICE_ADDRESS,
  1217. (u16)reg->reg, 2,
  1218. value, 4);
  1219. break;
  1220. case 2: /* Video Block - read byte */
  1221. ret =
  1222. cx231xx_write_i2c_data(dev,
  1223. VID_BLK_I2C_ADDRESS,
  1224. (u16)reg->reg, 2,
  1225. value, 1);
  1226. break;
  1227. case 24: /* Video Block - read dword */
  1228. ret =
  1229. cx231xx_write_i2c_data(dev,
  1230. VID_BLK_I2C_ADDRESS,
  1231. (u16)reg->reg, 2,
  1232. value, 4);
  1233. break;
  1234. case 3: /* I2S block - read byte */
  1235. ret =
  1236. cx231xx_write_i2c_data(dev,
  1237. I2S_BLK_DEVICE_ADDRESS,
  1238. (u16)reg->reg, 1,
  1239. value, 1);
  1240. break;
  1241. case 34: /* I2S block - read dword */
  1242. ret =
  1243. cx231xx_write_i2c_data(dev,
  1244. I2S_BLK_DEVICE_ADDRESS,
  1245. (u16)reg->reg, 1,
  1246. value, 4);
  1247. break;
  1248. }
  1249. }
  1250. return ret < 0 ? ret : 0;
  1251. default:
  1252. break;
  1253. }
  1254. mutex_lock(&dev->lock);
  1255. call_all(dev, core, s_register, reg);
  1256. mutex_unlock(&dev->lock);
  1257. return ret;
  1258. }
  1259. #endif
  1260. static int vidioc_cropcap(struct file *file, void *priv,
  1261. struct v4l2_cropcap *cc)
  1262. {
  1263. struct cx231xx_fh *fh = priv;
  1264. struct cx231xx *dev = fh->dev;
  1265. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1266. return -EINVAL;
  1267. cc->bounds.left = 0;
  1268. cc->bounds.top = 0;
  1269. cc->bounds.width = dev->width;
  1270. cc->bounds.height = dev->height;
  1271. cc->defrect = cc->bounds;
  1272. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1273. cc->pixelaspect.denominator = 59;
  1274. return 0;
  1275. }
  1276. static int vidioc_streamon(struct file *file, void *priv,
  1277. enum v4l2_buf_type type)
  1278. {
  1279. struct cx231xx_fh *fh = priv;
  1280. struct cx231xx *dev = fh->dev;
  1281. int rc;
  1282. rc = check_dev(dev);
  1283. if (rc < 0)
  1284. return rc;
  1285. mutex_lock(&dev->lock);
  1286. rc = res_get(fh);
  1287. if (likely(rc >= 0))
  1288. rc = videobuf_streamon(&fh->vb_vidq);
  1289. call_all(dev, video, s_stream, 1);
  1290. mutex_unlock(&dev->lock);
  1291. return rc;
  1292. }
  1293. static int vidioc_streamoff(struct file *file, void *priv,
  1294. enum v4l2_buf_type type)
  1295. {
  1296. struct cx231xx_fh *fh = priv;
  1297. struct cx231xx *dev = fh->dev;
  1298. int rc;
  1299. rc = check_dev(dev);
  1300. if (rc < 0)
  1301. return rc;
  1302. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1303. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1304. return -EINVAL;
  1305. if (type != fh->type)
  1306. return -EINVAL;
  1307. mutex_lock(&dev->lock);
  1308. cx25840_call(dev, video, s_stream, 0);
  1309. videobuf_streamoff(&fh->vb_vidq);
  1310. res_free(fh);
  1311. mutex_unlock(&dev->lock);
  1312. return 0;
  1313. }
  1314. static int vidioc_querycap(struct file *file, void *priv,
  1315. struct v4l2_capability *cap)
  1316. {
  1317. struct cx231xx_fh *fh = priv;
  1318. struct cx231xx *dev = fh->dev;
  1319. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1320. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1321. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1322. cap->version = CX231XX_VERSION_CODE;
  1323. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1324. #if 0
  1325. V4L2_CAP_SLICED_VBI_CAPTURE |
  1326. #endif
  1327. V4L2_CAP_VIDEO_CAPTURE |
  1328. V4L2_CAP_AUDIO |
  1329. V4L2_CAP_READWRITE |
  1330. V4L2_CAP_STREAMING;
  1331. if (dev->tuner_type != TUNER_ABSENT)
  1332. cap->capabilities |= V4L2_CAP_TUNER;
  1333. return 0;
  1334. }
  1335. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1336. struct v4l2_fmtdesc *f)
  1337. {
  1338. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1339. return -EINVAL;
  1340. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1341. f->pixelformat = format[f->index].fourcc;
  1342. return 0;
  1343. }
  1344. /* Sliced VBI ioctls */
  1345. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1346. struct v4l2_format *f)
  1347. {
  1348. struct cx231xx_fh *fh = priv;
  1349. struct cx231xx *dev = fh->dev;
  1350. int rc;
  1351. rc = check_dev(dev);
  1352. if (rc < 0)
  1353. return rc;
  1354. mutex_lock(&dev->lock);
  1355. f->fmt.sliced.service_set = 0;
  1356. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1357. if (f->fmt.sliced.service_set == 0)
  1358. rc = -EINVAL;
  1359. mutex_unlock(&dev->lock);
  1360. return rc;
  1361. }
  1362. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1363. struct v4l2_format *f)
  1364. {
  1365. struct cx231xx_fh *fh = priv;
  1366. struct cx231xx *dev = fh->dev;
  1367. int rc;
  1368. rc = check_dev(dev);
  1369. if (rc < 0)
  1370. return rc;
  1371. mutex_lock(&dev->lock);
  1372. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1373. mutex_unlock(&dev->lock);
  1374. if (f->fmt.sliced.service_set == 0)
  1375. return -EINVAL;
  1376. return 0;
  1377. }
  1378. /* RAW VBI ioctls */
  1379. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1380. struct v4l2_format *f)
  1381. {
  1382. struct cx231xx_fh *fh = priv;
  1383. struct cx231xx *dev = fh->dev;
  1384. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1385. 35468950 : 28636363;
  1386. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1387. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1388. f->fmt.vbi.offset = 64 * 4;
  1389. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1390. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1391. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1392. PAL_VBI_LINES : NTSC_VBI_LINES;
  1393. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1394. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1395. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1396. return 0;
  1397. }
  1398. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1399. struct v4l2_format *f)
  1400. {
  1401. struct cx231xx_fh *fh = priv;
  1402. struct cx231xx *dev = fh->dev;
  1403. if (dev->vbi_stream_on && !fh->stream_on) {
  1404. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1405. return -EBUSY;
  1406. }
  1407. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1408. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1409. 35468950 : 28636363;
  1410. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1411. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1412. f->fmt.vbi.offset = 244;
  1413. f->fmt.vbi.flags = 0;
  1414. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1415. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1416. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1417. PAL_VBI_LINES : NTSC_VBI_LINES;
  1418. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1419. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1420. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1421. return 0;
  1422. }
  1423. static int vidioc_reqbufs(struct file *file, void *priv,
  1424. struct v4l2_requestbuffers *rb)
  1425. {
  1426. struct cx231xx_fh *fh = priv;
  1427. struct cx231xx *dev = fh->dev;
  1428. int rc;
  1429. rc = check_dev(dev);
  1430. if (rc < 0)
  1431. return rc;
  1432. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1433. }
  1434. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1435. {
  1436. struct cx231xx_fh *fh = priv;
  1437. struct cx231xx *dev = fh->dev;
  1438. int rc;
  1439. rc = check_dev(dev);
  1440. if (rc < 0)
  1441. return rc;
  1442. return videobuf_querybuf(&fh->vb_vidq, b);
  1443. }
  1444. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1445. {
  1446. struct cx231xx_fh *fh = priv;
  1447. struct cx231xx *dev = fh->dev;
  1448. int rc;
  1449. rc = check_dev(dev);
  1450. if (rc < 0)
  1451. return rc;
  1452. return videobuf_qbuf(&fh->vb_vidq, b);
  1453. }
  1454. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1455. {
  1456. struct cx231xx_fh *fh = priv;
  1457. struct cx231xx *dev = fh->dev;
  1458. int rc;
  1459. rc = check_dev(dev);
  1460. if (rc < 0)
  1461. return rc;
  1462. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1463. }
  1464. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1465. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1466. {
  1467. struct cx231xx_fh *fh = priv;
  1468. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1469. }
  1470. #endif
  1471. /* ----------------------------------------------------------- */
  1472. /* RADIO ESPECIFIC IOCTLS */
  1473. /* ----------------------------------------------------------- */
  1474. static int radio_querycap(struct file *file, void *priv,
  1475. struct v4l2_capability *cap)
  1476. {
  1477. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1478. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1479. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1480. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1481. cap->version = CX231XX_VERSION_CODE;
  1482. cap->capabilities = V4L2_CAP_TUNER;
  1483. return 0;
  1484. }
  1485. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1486. {
  1487. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1488. if (unlikely(t->index > 0))
  1489. return -EINVAL;
  1490. strcpy(t->name, "Radio");
  1491. t->type = V4L2_TUNER_RADIO;
  1492. mutex_lock(&dev->lock);
  1493. call_all(dev, tuner, s_tuner, t);
  1494. mutex_unlock(&dev->lock);
  1495. return 0;
  1496. }
  1497. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1498. {
  1499. if (i->index != 0)
  1500. return -EINVAL;
  1501. strcpy(i->name, "Radio");
  1502. i->type = V4L2_INPUT_TYPE_TUNER;
  1503. return 0;
  1504. }
  1505. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1506. {
  1507. if (unlikely(a->index))
  1508. return -EINVAL;
  1509. strcpy(a->name, "Radio");
  1510. return 0;
  1511. }
  1512. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1513. {
  1514. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1515. if (0 != t->index)
  1516. return -EINVAL;
  1517. mutex_lock(&dev->lock);
  1518. call_all(dev, tuner, s_tuner, t);
  1519. mutex_unlock(&dev->lock);
  1520. return 0;
  1521. }
  1522. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1523. {
  1524. return 0;
  1525. }
  1526. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1527. {
  1528. return 0;
  1529. }
  1530. static int radio_queryctrl(struct file *file, void *priv,
  1531. struct v4l2_queryctrl *c)
  1532. {
  1533. int i;
  1534. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1535. return -EINVAL;
  1536. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1537. for (i = 0; i < CX231XX_CTLS; i++) {
  1538. if (cx231xx_ctls[i].v.id == c->id)
  1539. break;
  1540. }
  1541. if (i == CX231XX_CTLS)
  1542. return -EINVAL;
  1543. *c = cx231xx_ctls[i].v;
  1544. } else
  1545. *c = no_ctl;
  1546. return 0;
  1547. }
  1548. /*
  1549. * cx231xx_v4l2_open()
  1550. * inits the device and starts isoc transfer
  1551. */
  1552. static int cx231xx_v4l2_open(struct file *filp)
  1553. {
  1554. int errCode = 0, radio = 0;
  1555. struct video_device *vdev = video_devdata(filp);
  1556. struct cx231xx *dev = video_drvdata(filp);
  1557. struct cx231xx_fh *fh;
  1558. enum v4l2_buf_type fh_type = 0;
  1559. switch (vdev->vfl_type) {
  1560. case VFL_TYPE_GRABBER:
  1561. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1562. break;
  1563. case VFL_TYPE_VBI:
  1564. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1565. break;
  1566. case VFL_TYPE_RADIO:
  1567. radio = 1;
  1568. break;
  1569. }
  1570. mutex_lock(&dev->lock);
  1571. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1572. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1573. dev->users);
  1574. #if 0
  1575. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1576. if (errCode < 0) {
  1577. cx231xx_errdev
  1578. ("Device locked on digital mode. Can't open analog\n");
  1579. mutex_unlock(&dev->lock);
  1580. return -EBUSY;
  1581. }
  1582. #endif
  1583. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1584. if (!fh) {
  1585. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1586. mutex_unlock(&dev->lock);
  1587. return -ENOMEM;
  1588. }
  1589. fh->dev = dev;
  1590. fh->radio = radio;
  1591. fh->type = fh_type;
  1592. filp->private_data = fh;
  1593. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1594. dev->width = norm_maxw(dev);
  1595. dev->height = norm_maxh(dev);
  1596. dev->hscale = 0;
  1597. dev->vscale = 0;
  1598. /* Power up in Analog TV mode */
  1599. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1600. #if 0
  1601. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1602. #endif
  1603. cx231xx_resolution_set(dev);
  1604. /* set video alternate setting */
  1605. cx231xx_set_video_alternate(dev);
  1606. /* Needed, since GPIO might have disabled power of
  1607. some i2c device */
  1608. cx231xx_config_i2c(dev);
  1609. /* device needs to be initialized before isoc transfer */
  1610. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1611. video_mux(dev, dev->video_input);
  1612. }
  1613. if (fh->radio) {
  1614. cx231xx_videodbg("video_open: setting radio device\n");
  1615. /* cx231xx_start_radio(dev); */
  1616. call_all(dev, tuner, s_radio);
  1617. }
  1618. dev->users++;
  1619. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1620. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1621. NULL, &dev->video_mode.slock,
  1622. fh->type, V4L2_FIELD_INTERLACED,
  1623. sizeof(struct cx231xx_buffer), fh);
  1624. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1625. /* Set the required alternate setting VBI interface works in
  1626. Bulk mode only */
  1627. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1628. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1629. NULL, &dev->vbi_mode.slock,
  1630. fh->type, V4L2_FIELD_SEQ_TB,
  1631. sizeof(struct cx231xx_buffer), fh);
  1632. }
  1633. mutex_unlock(&dev->lock);
  1634. return errCode;
  1635. }
  1636. /*
  1637. * cx231xx_realease_resources()
  1638. * unregisters the v4l2,i2c and usb devices
  1639. * called when the device gets disconected or at module unload
  1640. */
  1641. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1642. {
  1643. /*FIXME: I2C IR should be disconnected */
  1644. if (dev->radio_dev) {
  1645. if (video_is_registered(dev->radio_dev))
  1646. video_unregister_device(dev->radio_dev);
  1647. else
  1648. video_device_release(dev->radio_dev);
  1649. dev->radio_dev = NULL;
  1650. }
  1651. if (dev->vbi_dev) {
  1652. cx231xx_info("V4L2 device %s deregistered\n",
  1653. video_device_node_name(dev->vbi_dev));
  1654. if (video_is_registered(dev->vbi_dev))
  1655. video_unregister_device(dev->vbi_dev);
  1656. else
  1657. video_device_release(dev->vbi_dev);
  1658. dev->vbi_dev = NULL;
  1659. }
  1660. if (dev->vdev) {
  1661. cx231xx_info("V4L2 device %s deregistered\n",
  1662. video_device_node_name(dev->vdev));
  1663. if (video_is_registered(dev->vdev))
  1664. video_unregister_device(dev->vdev);
  1665. else
  1666. video_device_release(dev->vdev);
  1667. dev->vdev = NULL;
  1668. }
  1669. }
  1670. /*
  1671. * cx231xx_v4l2_close()
  1672. * stops streaming and deallocates all resources allocated by the v4l2
  1673. * calls and ioctls
  1674. */
  1675. static int cx231xx_v4l2_close(struct file *filp)
  1676. {
  1677. struct cx231xx_fh *fh = filp->private_data;
  1678. struct cx231xx *dev = fh->dev;
  1679. cx231xx_videodbg("users=%d\n", dev->users);
  1680. mutex_lock(&dev->lock);
  1681. if (res_check(fh))
  1682. res_free(fh);
  1683. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1684. videobuf_stop(&fh->vb_vidq);
  1685. videobuf_mmap_free(&fh->vb_vidq);
  1686. /* the device is already disconnect,
  1687. free the remaining resources */
  1688. if (dev->state & DEV_DISCONNECTED) {
  1689. cx231xx_release_resources(dev);
  1690. mutex_unlock(&dev->lock);
  1691. kfree(dev);
  1692. return 0;
  1693. }
  1694. /* do this before setting alternate! */
  1695. cx231xx_uninit_vbi_isoc(dev);
  1696. /* set alternate 0 */
  1697. if (!dev->vbi_or_sliced_cc_mode)
  1698. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1699. else
  1700. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1701. kfree(fh);
  1702. dev->users--;
  1703. wake_up_interruptible_nr(&dev->open, 1);
  1704. mutex_unlock(&dev->lock);
  1705. return 0;
  1706. }
  1707. if (dev->users == 1) {
  1708. videobuf_stop(&fh->vb_vidq);
  1709. videobuf_mmap_free(&fh->vb_vidq);
  1710. /* the device is already disconnect,
  1711. free the remaining resources */
  1712. if (dev->state & DEV_DISCONNECTED) {
  1713. cx231xx_release_resources(dev);
  1714. mutex_unlock(&dev->lock);
  1715. kfree(dev);
  1716. return 0;
  1717. }
  1718. /* Save some power by putting tuner to sleep */
  1719. call_all(dev, core, s_power, 0);
  1720. /* do this before setting alternate! */
  1721. cx231xx_uninit_isoc(dev);
  1722. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1723. /* set alternate 0 */
  1724. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1725. }
  1726. kfree(fh);
  1727. dev->users--;
  1728. wake_up_interruptible_nr(&dev->open, 1);
  1729. mutex_unlock(&dev->lock);
  1730. return 0;
  1731. }
  1732. /*
  1733. * cx231xx_v4l2_read()
  1734. * will allocate buffers when called for the first time
  1735. */
  1736. static ssize_t
  1737. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1738. loff_t *pos)
  1739. {
  1740. struct cx231xx_fh *fh = filp->private_data;
  1741. struct cx231xx *dev = fh->dev;
  1742. int rc;
  1743. rc = check_dev(dev);
  1744. if (rc < 0)
  1745. return rc;
  1746. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1747. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1748. mutex_lock(&dev->lock);
  1749. rc = res_get(fh);
  1750. mutex_unlock(&dev->lock);
  1751. if (unlikely(rc < 0))
  1752. return rc;
  1753. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1754. filp->f_flags & O_NONBLOCK);
  1755. }
  1756. return 0;
  1757. }
  1758. /*
  1759. * cx231xx_v4l2_poll()
  1760. * will allocate buffers when called for the first time
  1761. */
  1762. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
  1763. {
  1764. struct cx231xx_fh *fh = filp->private_data;
  1765. struct cx231xx *dev = fh->dev;
  1766. int rc;
  1767. rc = check_dev(dev);
  1768. if (rc < 0)
  1769. return rc;
  1770. mutex_lock(&dev->lock);
  1771. rc = res_get(fh);
  1772. mutex_unlock(&dev->lock);
  1773. if (unlikely(rc < 0))
  1774. return POLLERR;
  1775. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1776. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  1777. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1778. else
  1779. return POLLERR;
  1780. }
  1781. /*
  1782. * cx231xx_v4l2_mmap()
  1783. */
  1784. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1785. {
  1786. struct cx231xx_fh *fh = filp->private_data;
  1787. struct cx231xx *dev = fh->dev;
  1788. int rc;
  1789. rc = check_dev(dev);
  1790. if (rc < 0)
  1791. return rc;
  1792. mutex_lock(&dev->lock);
  1793. rc = res_get(fh);
  1794. mutex_unlock(&dev->lock);
  1795. if (unlikely(rc < 0))
  1796. return rc;
  1797. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1798. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1799. (unsigned long)vma->vm_start,
  1800. (unsigned long)vma->vm_end -
  1801. (unsigned long)vma->vm_start, rc);
  1802. return rc;
  1803. }
  1804. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1805. .owner = THIS_MODULE,
  1806. .open = cx231xx_v4l2_open,
  1807. .release = cx231xx_v4l2_close,
  1808. .read = cx231xx_v4l2_read,
  1809. .poll = cx231xx_v4l2_poll,
  1810. .mmap = cx231xx_v4l2_mmap,
  1811. .ioctl = video_ioctl2,
  1812. };
  1813. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1814. .vidioc_querycap = vidioc_querycap,
  1815. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1816. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1817. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1818. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1819. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1820. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1821. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1822. .vidioc_g_audio = vidioc_g_audio,
  1823. .vidioc_s_audio = vidioc_s_audio,
  1824. .vidioc_cropcap = vidioc_cropcap,
  1825. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1826. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1827. .vidioc_reqbufs = vidioc_reqbufs,
  1828. .vidioc_querybuf = vidioc_querybuf,
  1829. .vidioc_qbuf = vidioc_qbuf,
  1830. .vidioc_dqbuf = vidioc_dqbuf,
  1831. .vidioc_s_std = vidioc_s_std,
  1832. .vidioc_g_std = vidioc_g_std,
  1833. .vidioc_enum_input = vidioc_enum_input,
  1834. .vidioc_g_input = vidioc_g_input,
  1835. .vidioc_s_input = vidioc_s_input,
  1836. .vidioc_queryctrl = vidioc_queryctrl,
  1837. .vidioc_g_ctrl = vidioc_g_ctrl,
  1838. .vidioc_s_ctrl = vidioc_s_ctrl,
  1839. .vidioc_streamon = vidioc_streamon,
  1840. .vidioc_streamoff = vidioc_streamoff,
  1841. .vidioc_g_tuner = vidioc_g_tuner,
  1842. .vidioc_s_tuner = vidioc_s_tuner,
  1843. .vidioc_g_frequency = vidioc_g_frequency,
  1844. .vidioc_s_frequency = vidioc_s_frequency,
  1845. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1846. .vidioc_g_register = vidioc_g_register,
  1847. .vidioc_s_register = vidioc_s_register,
  1848. #endif
  1849. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1850. .vidiocgmbuf = vidiocgmbuf,
  1851. #endif
  1852. };
  1853. static struct video_device cx231xx_vbi_template;
  1854. static const struct video_device cx231xx_video_template = {
  1855. .fops = &cx231xx_v4l_fops,
  1856. .release = video_device_release,
  1857. .ioctl_ops = &video_ioctl_ops,
  1858. .tvnorms = V4L2_STD_ALL,
  1859. .current_norm = V4L2_STD_PAL,
  1860. };
  1861. static const struct v4l2_file_operations radio_fops = {
  1862. .owner = THIS_MODULE,
  1863. .open = cx231xx_v4l2_open,
  1864. .release = cx231xx_v4l2_close,
  1865. .ioctl = video_ioctl2,
  1866. };
  1867. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1868. .vidioc_querycap = radio_querycap,
  1869. .vidioc_g_tuner = radio_g_tuner,
  1870. .vidioc_enum_input = radio_enum_input,
  1871. .vidioc_g_audio = radio_g_audio,
  1872. .vidioc_s_tuner = radio_s_tuner,
  1873. .vidioc_s_audio = radio_s_audio,
  1874. .vidioc_s_input = radio_s_input,
  1875. .vidioc_queryctrl = radio_queryctrl,
  1876. .vidioc_g_ctrl = vidioc_g_ctrl,
  1877. .vidioc_s_ctrl = vidioc_s_ctrl,
  1878. .vidioc_g_frequency = vidioc_g_frequency,
  1879. .vidioc_s_frequency = vidioc_s_frequency,
  1880. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1881. .vidioc_g_register = vidioc_g_register,
  1882. .vidioc_s_register = vidioc_s_register,
  1883. #endif
  1884. };
  1885. static struct video_device cx231xx_radio_template = {
  1886. .name = "cx231xx-radio",
  1887. .fops = &radio_fops,
  1888. .ioctl_ops = &radio_ioctl_ops,
  1889. };
  1890. /******************************** usb interface ******************************/
  1891. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  1892. const struct video_device
  1893. *template, const char *type_name)
  1894. {
  1895. struct video_device *vfd;
  1896. vfd = video_device_alloc();
  1897. if (NULL == vfd)
  1898. return NULL;
  1899. *vfd = *template;
  1900. vfd->v4l2_dev = &dev->v4l2_dev;
  1901. vfd->release = video_device_release;
  1902. vfd->debug = video_debug;
  1903. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1904. video_set_drvdata(vfd, dev);
  1905. return vfd;
  1906. }
  1907. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1908. {
  1909. int ret;
  1910. cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
  1911. dev->name,
  1912. (CX231XX_VERSION_CODE >> 16) & 0xff,
  1913. (CX231XX_VERSION_CODE >> 8) & 0xff,
  1914. CX231XX_VERSION_CODE & 0xff);
  1915. /* set default norm */
  1916. /*dev->norm = cx231xx_video_template.current_norm; */
  1917. dev->width = norm_maxw(dev);
  1918. dev->height = norm_maxh(dev);
  1919. dev->interlaced = 0;
  1920. dev->hscale = 0;
  1921. dev->vscale = 0;
  1922. /* Analog specific initialization */
  1923. dev->format = &format[0];
  1924. /* video_mux(dev, dev->video_input); */
  1925. /* Audio defaults */
  1926. dev->mute = 1;
  1927. dev->volume = 0x1f;
  1928. /* enable vbi capturing */
  1929. /* write code here... */
  1930. /* allocate and fill video video_device struct */
  1931. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  1932. if (!dev->vdev) {
  1933. cx231xx_errdev("cannot allocate video_device.\n");
  1934. return -ENODEV;
  1935. }
  1936. /* register v4l2 video video_device */
  1937. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1938. video_nr[dev->devno]);
  1939. if (ret) {
  1940. cx231xx_errdev("unable to register video device (error=%i).\n",
  1941. ret);
  1942. return ret;
  1943. }
  1944. cx231xx_info("%s/0: registered device %s [v4l2]\n",
  1945. dev->name, video_device_node_name(dev->vdev));
  1946. /* Initialize VBI template */
  1947. memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
  1948. sizeof(cx231xx_vbi_template));
  1949. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  1950. /* Allocate and fill vbi video_device struct */
  1951. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  1952. /* register v4l2 vbi video_device */
  1953. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1954. vbi_nr[dev->devno]);
  1955. if (ret < 0) {
  1956. cx231xx_errdev("unable to register vbi device\n");
  1957. return ret;
  1958. }
  1959. cx231xx_info("%s/0: registered device %s\n",
  1960. dev->name, video_device_node_name(dev->vbi_dev));
  1961. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1962. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  1963. "radio");
  1964. if (!dev->radio_dev) {
  1965. cx231xx_errdev("cannot allocate video_device.\n");
  1966. return -ENODEV;
  1967. }
  1968. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1969. radio_nr[dev->devno]);
  1970. if (ret < 0) {
  1971. cx231xx_errdev("can't register radio device\n");
  1972. return ret;
  1973. }
  1974. cx231xx_info("Registered radio device as %s\n",
  1975. video_device_node_name(dev->radio_dev));
  1976. }
  1977. cx231xx_info("V4L2 device registered as %s and %s\n",
  1978. video_device_node_name(dev->vdev),
  1979. video_device_node_name(dev->vbi_dev));
  1980. return 0;
  1981. }