cx231xx-video.c 57 KB

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