cx231xx-video.c 57 KB

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