cx231xx-video.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. cx231xx-video.c - driver for Conexant Cx23100/101/102
  4. USB video capture devices
  5. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  6. Based on em28xx driver
  7. Based on cx23885 driver
  8. Based on cx88 driver
  9. */
  10. #include "cx231xx.h"
  11. #include <linux/init.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/bitmap.h>
  16. #include <linux/i2c.h>
  17. #include <linux/mm.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <media/v4l2-common.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/drv-intf/msp3400.h>
  24. #include <media/tuner.h>
  25. #include <media/dvb_frontend.h>
  26. #include "cx231xx-vbi.h"
  27. #define CX231XX_VERSION "0.0.3"
  28. #define DRIVER_AUTHOR "Srinivasa Deevi <[email protected]>"
  29. #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
  30. #define cx231xx_videodbg(fmt, arg...) do {\
  31. if (video_debug) \
  32. printk(KERN_INFO "%s %s :"fmt, \
  33. dev->name, __func__ , ##arg); } while (0)
  34. static unsigned int isoc_debug;
  35. module_param(isoc_debug, int, 0644);
  36. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  37. #define cx231xx_isocdbg(fmt, arg...) \
  38. do {\
  39. if (isoc_debug) { \
  40. printk(KERN_INFO "%s %s :"fmt, \
  41. dev->name, __func__ , ##arg); \
  42. } \
  43. } while (0)
  44. MODULE_AUTHOR(DRIVER_AUTHOR);
  45. MODULE_DESCRIPTION(DRIVER_DESC);
  46. MODULE_LICENSE("GPL");
  47. MODULE_VERSION(CX231XX_VERSION);
  48. static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
  49. static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
  50. static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
  51. static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
  52. module_param_array(card, int, NULL, 0444);
  53. module_param_array(video_nr, int, NULL, 0444);
  54. module_param_array(vbi_nr, int, NULL, 0444);
  55. module_param_array(radio_nr, int, NULL, 0444);
  56. MODULE_PARM_DESC(card, "card type");
  57. MODULE_PARM_DESC(video_nr, "video device numbers");
  58. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  59. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  60. static unsigned int video_debug;
  61. module_param(video_debug, int, 0644);
  62. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  63. /* supported video standards */
  64. static struct cx231xx_fmt format[] = {
  65. {
  66. .fourcc = V4L2_PIX_FMT_YUYV,
  67. .depth = 16,
  68. .reg = 0,
  69. },
  70. };
  71. static int cx231xx_enable_analog_tuner(struct cx231xx *dev)
  72. {
  73. #ifdef CONFIG_MEDIA_CONTROLLER
  74. struct media_device *mdev = dev->media_dev;
  75. struct media_entity *entity, *decoder = NULL, *source;
  76. struct media_link *link, *found_link = NULL;
  77. int ret, active_links = 0;
  78. if (!mdev)
  79. return 0;
  80. /*
  81. * This will find the tuner that is connected into the decoder.
  82. * Technically, this is not 100% correct, as the device may be
  83. * using an analog input instead of the tuner. However, as we can't
  84. * do DVB streaming while the DMA engine is being used for V4L2,
  85. * this should be enough for the actual needs.
  86. */
  87. media_device_for_each_entity(entity, mdev) {
  88. if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
  89. decoder = entity;
  90. break;
  91. }
  92. }
  93. if (!decoder)
  94. return 0;
  95. list_for_each_entry(link, &decoder->links, list) {
  96. if (link->sink->entity == decoder) {
  97. found_link = link;
  98. if (link->flags & MEDIA_LNK_FL_ENABLED)
  99. active_links++;
  100. break;
  101. }
  102. }
  103. if (active_links == 1 || !found_link)
  104. return 0;
  105. source = found_link->source->entity;
  106. list_for_each_entry(link, &source->links, list) {
  107. struct media_entity *sink;
  108. int flags = 0;
  109. sink = link->sink->entity;
  110. if (sink == entity)
  111. flags = MEDIA_LNK_FL_ENABLED;
  112. ret = media_entity_setup_link(link, flags);
  113. if (ret) {
  114. dev_err(dev->dev,
  115. "Couldn't change link %s->%s to %s. Error %d\n",
  116. source->name, sink->name,
  117. flags ? "enabled" : "disabled",
  118. ret);
  119. return ret;
  120. } else
  121. dev_dbg(dev->dev,
  122. "link %s->%s was %s\n",
  123. source->name, sink->name,
  124. flags ? "ENABLED" : "disabled");
  125. }
  126. #endif
  127. return 0;
  128. }
  129. /* ------------------------------------------------------------------
  130. Video buffer and parser functions
  131. ------------------------------------------------------------------*/
  132. /*
  133. * Announces that a buffer were filled and request the next
  134. */
  135. static inline void buffer_filled(struct cx231xx *dev,
  136. struct cx231xx_dmaqueue *dma_q,
  137. struct cx231xx_buffer *buf)
  138. {
  139. /* Advice that buffer was filled */
  140. cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.vb2_buf.index);
  141. buf->vb.sequence = dma_q->sequence++;
  142. buf->vb.field = V4L2_FIELD_INTERLACED;
  143. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  144. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, dev->size);
  145. if (dev->USE_ISO)
  146. dev->video_mode.isoc_ctl.buf = NULL;
  147. else
  148. dev->video_mode.bulk_ctl.buf = NULL;
  149. list_del(&buf->list);
  150. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  151. }
  152. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  153. {
  154. char *errmsg = "Unknown";
  155. switch (status) {
  156. case -ENOENT:
  157. errmsg = "unlinked synchronously";
  158. break;
  159. case -ECONNRESET:
  160. errmsg = "unlinked asynchronously";
  161. break;
  162. case -ENOSR:
  163. errmsg = "Buffer error (overrun)";
  164. break;
  165. case -EPIPE:
  166. errmsg = "Stalled (device not responding)";
  167. break;
  168. case -EOVERFLOW:
  169. errmsg = "Babble (bad cable?)";
  170. break;
  171. case -EPROTO:
  172. errmsg = "Bit-stuff error (bad cable?)";
  173. break;
  174. case -EILSEQ:
  175. errmsg = "CRC/Timeout (could be anything)";
  176. break;
  177. case -ETIME:
  178. errmsg = "Device does not respond";
  179. break;
  180. }
  181. if (packet < 0) {
  182. cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  183. } else {
  184. cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
  185. packet, status, errmsg);
  186. }
  187. }
  188. /*
  189. * generic routine to get the next available buffer
  190. */
  191. static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
  192. struct cx231xx_buffer **buf)
  193. {
  194. struct cx231xx_video_mode *vmode =
  195. container_of(dma_q, struct cx231xx_video_mode, vidq);
  196. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  197. char *outp;
  198. if (list_empty(&dma_q->active)) {
  199. cx231xx_isocdbg("No active queue to serve\n");
  200. if (dev->USE_ISO)
  201. dev->video_mode.isoc_ctl.buf = NULL;
  202. else
  203. dev->video_mode.bulk_ctl.buf = NULL;
  204. *buf = NULL;
  205. return;
  206. }
  207. /* Get the next buffer */
  208. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, list);
  209. /* Cleans up buffer - Useful for testing for frame/URB loss */
  210. outp = vb2_plane_vaddr(&(*buf)->vb.vb2_buf, 0);
  211. memset(outp, 0, dev->size);
  212. if (dev->USE_ISO)
  213. dev->video_mode.isoc_ctl.buf = *buf;
  214. else
  215. dev->video_mode.bulk_ctl.buf = *buf;
  216. return;
  217. }
  218. /*
  219. * Controls the isoc copy of each urb packet
  220. */
  221. static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  222. {
  223. struct cx231xx_dmaqueue *dma_q = urb->context;
  224. int i;
  225. unsigned char *p_buffer;
  226. u32 bytes_parsed = 0, buffer_size = 0;
  227. u8 sav_eav = 0;
  228. if (!dev)
  229. return 0;
  230. if (dev->state & DEV_DISCONNECTED)
  231. return 0;
  232. if (urb->status < 0) {
  233. print_err_status(dev, -1, urb->status);
  234. if (urb->status == -ENOENT)
  235. return 0;
  236. }
  237. for (i = 0; i < urb->number_of_packets; i++) {
  238. int status = urb->iso_frame_desc[i].status;
  239. if (status < 0) {
  240. print_err_status(dev, i, status);
  241. if (urb->iso_frame_desc[i].status != -EPROTO)
  242. continue;
  243. }
  244. if (urb->iso_frame_desc[i].actual_length <= 0) {
  245. /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
  246. continue;
  247. }
  248. if (urb->iso_frame_desc[i].actual_length >
  249. dev->video_mode.max_pkt_size) {
  250. cx231xx_isocdbg("packet bigger than packet size");
  251. continue;
  252. }
  253. /* get buffer pointer and length */
  254. p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  255. buffer_size = urb->iso_frame_desc[i].actual_length;
  256. bytes_parsed = 0;
  257. if (dma_q->is_partial_line) {
  258. /* Handle the case of a partial line */
  259. sav_eav = dma_q->last_sav;
  260. } else {
  261. /* Check for a SAV/EAV overlapping
  262. the buffer boundary */
  263. sav_eav =
  264. cx231xx_find_boundary_SAV_EAV(p_buffer,
  265. dma_q->partial_buf,
  266. &bytes_parsed);
  267. }
  268. sav_eav &= 0xF0;
  269. /* Get the first line if we have some portion of an SAV/EAV from
  270. the last buffer or a partial line */
  271. if (sav_eav) {
  272. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  273. sav_eav, /* SAV/EAV */
  274. p_buffer + bytes_parsed, /* p_buffer */
  275. buffer_size - bytes_parsed);/* buf size */
  276. }
  277. /* Now parse data that is completely in this buffer */
  278. /* dma_q->is_partial_line = 0; */
  279. while (bytes_parsed < buffer_size) {
  280. u32 bytes_used = 0;
  281. sav_eav = cx231xx_find_next_SAV_EAV(
  282. p_buffer + bytes_parsed, /* p_buffer */
  283. buffer_size - bytes_parsed, /* buf size */
  284. &bytes_used);/* bytes used to get SAV/EAV */
  285. bytes_parsed += bytes_used;
  286. sav_eav &= 0xF0;
  287. if (sav_eav && (bytes_parsed < buffer_size)) {
  288. bytes_parsed += cx231xx_get_video_line(dev,
  289. dma_q, sav_eav, /* SAV/EAV */
  290. p_buffer + bytes_parsed,/* p_buffer */
  291. buffer_size - bytes_parsed);/*buf size*/
  292. }
  293. }
  294. /* Save the last four bytes of the buffer so we can check the
  295. buffer boundary condition next time */
  296. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  297. bytes_parsed = 0;
  298. }
  299. return 1;
  300. }
  301. static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
  302. {
  303. struct cx231xx_dmaqueue *dma_q = urb->context;
  304. unsigned char *p_buffer;
  305. u32 bytes_parsed = 0, buffer_size = 0;
  306. u8 sav_eav = 0;
  307. if (!dev)
  308. return 0;
  309. if (dev->state & DEV_DISCONNECTED)
  310. return 0;
  311. if (urb->status < 0) {
  312. print_err_status(dev, -1, urb->status);
  313. if (urb->status == -ENOENT)
  314. return 0;
  315. }
  316. if (1) {
  317. /* get buffer pointer and length */
  318. p_buffer = urb->transfer_buffer;
  319. buffer_size = urb->actual_length;
  320. bytes_parsed = 0;
  321. if (dma_q->is_partial_line) {
  322. /* Handle the case of a partial line */
  323. sav_eav = dma_q->last_sav;
  324. } else {
  325. /* Check for a SAV/EAV overlapping
  326. the buffer boundary */
  327. sav_eav =
  328. cx231xx_find_boundary_SAV_EAV(p_buffer,
  329. dma_q->partial_buf,
  330. &bytes_parsed);
  331. }
  332. sav_eav &= 0xF0;
  333. /* Get the first line if we have some portion of an SAV/EAV from
  334. the last buffer or a partial line */
  335. if (sav_eav) {
  336. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  337. sav_eav, /* SAV/EAV */
  338. p_buffer + bytes_parsed, /* p_buffer */
  339. buffer_size - bytes_parsed);/* buf size */
  340. }
  341. /* Now parse data that is completely in this buffer */
  342. /* dma_q->is_partial_line = 0; */
  343. while (bytes_parsed < buffer_size) {
  344. u32 bytes_used = 0;
  345. sav_eav = cx231xx_find_next_SAV_EAV(
  346. p_buffer + bytes_parsed, /* p_buffer */
  347. buffer_size - bytes_parsed, /* buf size */
  348. &bytes_used);/* bytes used to get SAV/EAV */
  349. bytes_parsed += bytes_used;
  350. sav_eav &= 0xF0;
  351. if (sav_eav && (bytes_parsed < buffer_size)) {
  352. bytes_parsed += cx231xx_get_video_line(dev,
  353. dma_q, sav_eav, /* SAV/EAV */
  354. p_buffer + bytes_parsed,/* p_buffer */
  355. buffer_size - bytes_parsed);/*buf size*/
  356. }
  357. }
  358. /* Save the last four bytes of the buffer so we can check the
  359. buffer boundary condition next time */
  360. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  361. bytes_parsed = 0;
  362. }
  363. return 1;
  364. }
  365. u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
  366. u32 *p_bytes_used)
  367. {
  368. u32 bytes_used;
  369. u8 boundary_bytes[8];
  370. u8 sav_eav = 0;
  371. *p_bytes_used = 0;
  372. /* Create an array of the last 4 bytes of the last buffer and the first
  373. 4 bytes of the current buffer. */
  374. memcpy(boundary_bytes, partial_buf, 4);
  375. memcpy(boundary_bytes + 4, p_buffer, 4);
  376. /* Check for the SAV/EAV in the boundary buffer */
  377. sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
  378. &bytes_used);
  379. if (sav_eav) {
  380. /* found a boundary SAV/EAV. Updates the bytes used to reflect
  381. only those used in the new buffer */
  382. *p_bytes_used = bytes_used - 4;
  383. }
  384. return sav_eav;
  385. }
  386. u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
  387. {
  388. u32 i;
  389. u8 sav_eav = 0;
  390. /*
  391. * Don't search if the buffer size is less than 4. It causes a page
  392. * fault since buffer_size - 4 evaluates to a large number in that
  393. * case.
  394. */
  395. if (buffer_size < 4) {
  396. *p_bytes_used = buffer_size;
  397. return 0;
  398. }
  399. for (i = 0; i < (buffer_size - 3); i++) {
  400. if ((p_buffer[i] == 0xFF) &&
  401. (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
  402. *p_bytes_used = i + 4;
  403. sav_eav = p_buffer[i + 3];
  404. return sav_eav;
  405. }
  406. }
  407. *p_bytes_used = buffer_size;
  408. return 0;
  409. }
  410. u32 cx231xx_get_video_line(struct cx231xx *dev,
  411. struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
  412. u8 *p_buffer, u32 buffer_size)
  413. {
  414. u32 bytes_copied = 0;
  415. int current_field = -1;
  416. switch (sav_eav) {
  417. case SAV_ACTIVE_VIDEO_FIELD1:
  418. /* looking for skipped line which occurred in PAL 720x480 mode.
  419. In this case, there will be no active data contained
  420. between the SAV and EAV */
  421. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  422. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  423. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  424. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  425. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  426. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  427. return bytes_copied;
  428. current_field = 1;
  429. break;
  430. case SAV_ACTIVE_VIDEO_FIELD2:
  431. /* looking for skipped line which occurred in PAL 720x480 mode.
  432. In this case, there will be no active data contained between
  433. the SAV and EAV */
  434. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  435. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  436. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  437. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  438. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  439. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  440. return bytes_copied;
  441. current_field = 2;
  442. break;
  443. }
  444. dma_q->last_sav = sav_eav;
  445. bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
  446. buffer_size, current_field);
  447. return bytes_copied;
  448. }
  449. u32 cx231xx_copy_video_line(struct cx231xx *dev,
  450. struct cx231xx_dmaqueue *dma_q, u8 *p_line,
  451. u32 length, int field_number)
  452. {
  453. u32 bytes_to_copy;
  454. struct cx231xx_buffer *buf;
  455. u32 _line_size = dev->width * 2;
  456. if (dma_q->current_field != field_number)
  457. cx231xx_reset_video_buffer(dev, dma_q);
  458. /* get the buffer pointer */
  459. if (dev->USE_ISO)
  460. buf = dev->video_mode.isoc_ctl.buf;
  461. else
  462. buf = dev->video_mode.bulk_ctl.buf;
  463. /* Remember the field number for next time */
  464. dma_q->current_field = field_number;
  465. bytes_to_copy = dma_q->bytes_left_in_line;
  466. if (bytes_to_copy > length)
  467. bytes_to_copy = length;
  468. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  469. dma_q->bytes_left_in_line -= bytes_to_copy;
  470. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
  471. 0 : 1;
  472. return 0;
  473. }
  474. dma_q->is_partial_line = 1;
  475. /* If we don't have a buffer, just return the number of bytes we would
  476. have copied if we had a buffer. */
  477. if (!buf) {
  478. dma_q->bytes_left_in_line -= bytes_to_copy;
  479. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
  480. ? 0 : 1;
  481. return bytes_to_copy;
  482. }
  483. /* copy the data to video buffer */
  484. cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
  485. dma_q->pos += bytes_to_copy;
  486. dma_q->bytes_left_in_line -= bytes_to_copy;
  487. if (dma_q->bytes_left_in_line == 0) {
  488. dma_q->bytes_left_in_line = _line_size;
  489. dma_q->lines_completed++;
  490. dma_q->is_partial_line = 0;
  491. if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
  492. buffer_filled(dev, dma_q, buf);
  493. dma_q->pos = 0;
  494. buf = NULL;
  495. dma_q->lines_completed = 0;
  496. }
  497. }
  498. return bytes_to_copy;
  499. }
  500. void cx231xx_reset_video_buffer(struct cx231xx *dev,
  501. struct cx231xx_dmaqueue *dma_q)
  502. {
  503. struct cx231xx_buffer *buf;
  504. /* handle the switch from field 1 to field 2 */
  505. if (dma_q->current_field == 1) {
  506. if (dma_q->lines_completed >= dma_q->lines_per_field)
  507. dma_q->field1_done = 1;
  508. else
  509. dma_q->field1_done = 0;
  510. }
  511. if (dev->USE_ISO)
  512. buf = dev->video_mode.isoc_ctl.buf;
  513. else
  514. buf = dev->video_mode.bulk_ctl.buf;
  515. if (buf == NULL) {
  516. /* first try to get the buffer */
  517. get_next_buf(dma_q, &buf);
  518. dma_q->pos = 0;
  519. dma_q->field1_done = 0;
  520. dma_q->current_field = -1;
  521. }
  522. /* reset the counters */
  523. dma_q->bytes_left_in_line = dev->width << 1;
  524. dma_q->lines_completed = 0;
  525. }
  526. int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  527. u8 *p_buffer, u32 bytes_to_copy)
  528. {
  529. u8 *p_out_buffer = NULL;
  530. u32 current_line_bytes_copied = 0;
  531. struct cx231xx_buffer *buf;
  532. u32 _line_size = dev->width << 1;
  533. void *startwrite;
  534. int offset, lencopy;
  535. if (dev->USE_ISO)
  536. buf = dev->video_mode.isoc_ctl.buf;
  537. else
  538. buf = dev->video_mode.bulk_ctl.buf;
  539. if (buf == NULL)
  540. return -1;
  541. p_out_buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  542. current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
  543. /* Offset field 2 one line from the top of the buffer */
  544. offset = (dma_q->current_field == 1) ? 0 : _line_size;
  545. /* Offset for field 2 */
  546. startwrite = p_out_buffer + offset;
  547. /* lines already completed in the current field */
  548. startwrite += (dma_q->lines_completed * _line_size * 2);
  549. /* bytes already completed in the current line */
  550. startwrite += current_line_bytes_copied;
  551. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  552. bytes_to_copy : dma_q->bytes_left_in_line;
  553. if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + dev->size))
  554. return 0;
  555. /* The below copies the UYVY data straight into video buffer */
  556. cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
  557. return 0;
  558. }
  559. void cx231xx_swab(u16 *from, u16 *to, u16 len)
  560. {
  561. u16 i;
  562. if (len <= 0)
  563. return;
  564. for (i = 0; i < len / 2; i++)
  565. to[i] = (from[i] << 8) | (from[i] >> 8);
  566. }
  567. u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
  568. {
  569. u8 buffer_complete = 0;
  570. /* Dual field stream */
  571. buffer_complete = ((dma_q->current_field == 2) &&
  572. (dma_q->lines_completed >= dma_q->lines_per_field) &&
  573. dma_q->field1_done);
  574. return buffer_complete;
  575. }
  576. /* ------------------------------------------------------------------
  577. Videobuf operations
  578. ------------------------------------------------------------------*/
  579. static int queue_setup(struct vb2_queue *vq,
  580. unsigned int *nbuffers, unsigned int *nplanes,
  581. unsigned int sizes[], struct device *alloc_devs[])
  582. {
  583. struct cx231xx *dev = vb2_get_drv_priv(vq);
  584. dev->size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
  585. if (vq->num_buffers + *nbuffers < CX231XX_MIN_BUF)
  586. *nbuffers = CX231XX_MIN_BUF - vq->num_buffers;
  587. if (*nplanes)
  588. return sizes[0] < dev->size ? -EINVAL : 0;
  589. *nplanes = 1;
  590. sizes[0] = dev->size;
  591. return 0;
  592. }
  593. static void buffer_queue(struct vb2_buffer *vb)
  594. {
  595. struct cx231xx_buffer *buf =
  596. container_of(vb, struct cx231xx_buffer, vb.vb2_buf);
  597. struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  598. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  599. unsigned long flags;
  600. spin_lock_irqsave(&dev->video_mode.slock, flags);
  601. list_add_tail(&buf->list, &vidq->active);
  602. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  603. }
  604. static void return_all_buffers(struct cx231xx *dev,
  605. enum vb2_buffer_state state)
  606. {
  607. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  608. struct cx231xx_buffer *buf, *node;
  609. unsigned long flags;
  610. spin_lock_irqsave(&dev->video_mode.slock, flags);
  611. if (dev->USE_ISO)
  612. dev->video_mode.isoc_ctl.buf = NULL;
  613. else
  614. dev->video_mode.bulk_ctl.buf = NULL;
  615. list_for_each_entry_safe(buf, node, &vidq->active, list) {
  616. list_del(&buf->list);
  617. vb2_buffer_done(&buf->vb.vb2_buf, state);
  618. }
  619. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  620. }
  621. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  622. {
  623. struct cx231xx *dev = vb2_get_drv_priv(vq);
  624. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  625. int ret = 0;
  626. vidq->sequence = 0;
  627. dev->mode_tv = 0;
  628. cx231xx_enable_analog_tuner(dev);
  629. if (dev->USE_ISO)
  630. ret = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  631. CX231XX_NUM_BUFS,
  632. dev->video_mode.max_pkt_size,
  633. cx231xx_isoc_copy);
  634. else
  635. ret = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
  636. CX231XX_NUM_BUFS,
  637. dev->video_mode.max_pkt_size,
  638. cx231xx_bulk_copy);
  639. if (ret)
  640. return_all_buffers(dev, VB2_BUF_STATE_QUEUED);
  641. call_all(dev, video, s_stream, 1);
  642. return ret;
  643. }
  644. static void stop_streaming(struct vb2_queue *vq)
  645. {
  646. struct cx231xx *dev = vb2_get_drv_priv(vq);
  647. call_all(dev, video, s_stream, 0);
  648. return_all_buffers(dev, VB2_BUF_STATE_ERROR);
  649. }
  650. static struct vb2_ops cx231xx_video_qops = {
  651. .queue_setup = queue_setup,
  652. .buf_queue = buffer_queue,
  653. .start_streaming = start_streaming,
  654. .stop_streaming = stop_streaming,
  655. .wait_prepare = vb2_ops_wait_prepare,
  656. .wait_finish = vb2_ops_wait_finish,
  657. };
  658. /********************* v4l2 interface **************************************/
  659. void video_mux(struct cx231xx *dev, int index)
  660. {
  661. dev->video_input = index;
  662. dev->ctl_ainput = INPUT(index)->amux;
  663. cx231xx_set_video_input_mux(dev, index);
  664. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  665. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  666. dev_dbg(dev->dev, "video_mux : %d\n", index);
  667. /* do mode control overrides if required */
  668. cx231xx_do_mode_ctrl_overrides(dev);
  669. }
  670. /* ------------------------------------------------------------------
  671. IOCTL vidioc handling
  672. ------------------------------------------------------------------*/
  673. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  674. struct v4l2_format *f)
  675. {
  676. struct cx231xx *dev = video_drvdata(file);
  677. f->fmt.pix.width = dev->width;
  678. f->fmt.pix.height = dev->height;
  679. f->fmt.pix.pixelformat = dev->format->fourcc;
  680. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  681. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  682. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  683. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  684. return 0;
  685. }
  686. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  687. {
  688. unsigned int i;
  689. for (i = 0; i < ARRAY_SIZE(format); i++)
  690. if (format[i].fourcc == fourcc)
  691. return &format[i];
  692. return NULL;
  693. }
  694. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  695. struct v4l2_format *f)
  696. {
  697. struct cx231xx *dev = video_drvdata(file);
  698. unsigned int width = f->fmt.pix.width;
  699. unsigned int height = f->fmt.pix.height;
  700. unsigned int maxw = norm_maxw(dev);
  701. unsigned int maxh = norm_maxh(dev);
  702. struct cx231xx_fmt *fmt;
  703. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  704. if (!fmt) {
  705. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  706. f->fmt.pix.pixelformat);
  707. return -EINVAL;
  708. }
  709. /* width must even because of the YUYV format
  710. height must be even because of interlacing */
  711. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  712. f->fmt.pix.width = width;
  713. f->fmt.pix.height = height;
  714. f->fmt.pix.pixelformat = fmt->fourcc;
  715. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  716. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  717. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  718. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  719. return 0;
  720. }
  721. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  722. struct v4l2_format *f)
  723. {
  724. struct cx231xx *dev = video_drvdata(file);
  725. struct v4l2_subdev_format format = {
  726. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  727. };
  728. int rc;
  729. rc = vidioc_try_fmt_vid_cap(file, priv, f);
  730. if (rc)
  731. return rc;
  732. if (vb2_is_busy(&dev->vidq)) {
  733. dev_err(dev->dev, "%s: queue busy\n", __func__);
  734. return -EBUSY;
  735. }
  736. /* set new image size */
  737. dev->width = f->fmt.pix.width;
  738. dev->height = f->fmt.pix.height;
  739. dev->format = format_by_fourcc(f->fmt.pix.pixelformat);
  740. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  741. call_all(dev, pad, set_fmt, NULL, &format);
  742. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  743. return rc;
  744. }
  745. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  746. {
  747. struct cx231xx *dev = video_drvdata(file);
  748. *id = dev->norm;
  749. return 0;
  750. }
  751. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  752. {
  753. struct cx231xx *dev = video_drvdata(file);
  754. struct v4l2_subdev_format format = {
  755. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  756. };
  757. if (dev->norm == norm)
  758. return 0;
  759. if (vb2_is_busy(&dev->vidq))
  760. return -EBUSY;
  761. dev->norm = norm;
  762. /* Adjusts width/height, if needed */
  763. dev->width = 720;
  764. dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
  765. call_all(dev, video, s_std, dev->norm);
  766. /* We need to reset basic properties in the decoder related to
  767. resolution (since a standard change effects things like the number
  768. of lines in VACT, etc) */
  769. format.format.code = MEDIA_BUS_FMT_FIXED;
  770. format.format.width = dev->width;
  771. format.format.height = dev->height;
  772. call_all(dev, pad, set_fmt, NULL, &format);
  773. /* do mode control overrides */
  774. cx231xx_do_mode_ctrl_overrides(dev);
  775. return 0;
  776. }
  777. static const char *iname[] = {
  778. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  779. [CX231XX_VMUX_SVIDEO] = "S-Video",
  780. [CX231XX_VMUX_TELEVISION] = "Television",
  781. [CX231XX_VMUX_CABLE] = "Cable TV",
  782. [CX231XX_VMUX_DVB] = "DVB",
  783. };
  784. void cx231xx_v4l2_create_entities(struct cx231xx *dev)
  785. {
  786. #if defined(CONFIG_MEDIA_CONTROLLER)
  787. int ret, i;
  788. /* Create entities for each input connector */
  789. for (i = 0; i < MAX_CX231XX_INPUT; i++) {
  790. struct media_entity *ent = &dev->input_ent[i];
  791. if (!INPUT(i)->type)
  792. break;
  793. ent->name = iname[INPUT(i)->type];
  794. ent->flags = MEDIA_ENT_FL_CONNECTOR;
  795. dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
  796. switch (INPUT(i)->type) {
  797. case CX231XX_VMUX_COMPOSITE1:
  798. ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
  799. break;
  800. case CX231XX_VMUX_SVIDEO:
  801. ent->function = MEDIA_ENT_F_CONN_SVIDEO;
  802. break;
  803. case CX231XX_VMUX_TELEVISION:
  804. case CX231XX_VMUX_CABLE:
  805. case CX231XX_VMUX_DVB:
  806. /* The DVB core will handle it */
  807. if (dev->tuner_type == TUNER_ABSENT)
  808. continue;
  809. fallthrough;
  810. default: /* just to shut up a gcc warning */
  811. ent->function = MEDIA_ENT_F_CONN_RF;
  812. break;
  813. }
  814. ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
  815. if (ret < 0)
  816. pr_err("failed to initialize input pad[%d]!\n", i);
  817. ret = media_device_register_entity(dev->media_dev, ent);
  818. if (ret < 0)
  819. pr_err("failed to register input entity %d!\n", i);
  820. }
  821. #endif
  822. }
  823. int cx231xx_enum_input(struct file *file, void *priv,
  824. struct v4l2_input *i)
  825. {
  826. struct cx231xx *dev = video_drvdata(file);
  827. u32 gen_stat;
  828. unsigned int n;
  829. int ret;
  830. n = i->index;
  831. if (n >= MAX_CX231XX_INPUT)
  832. return -EINVAL;
  833. if (0 == INPUT(n)->type)
  834. return -EINVAL;
  835. i->index = n;
  836. i->type = V4L2_INPUT_TYPE_CAMERA;
  837. strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
  838. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  839. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  840. i->type = V4L2_INPUT_TYPE_TUNER;
  841. i->std = dev->vdev.tvnorms;
  842. /* If they are asking about the active input, read signal status */
  843. if (n == dev->video_input) {
  844. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  845. GEN_STAT, 2, &gen_stat, 4);
  846. if (ret > 0) {
  847. if ((gen_stat & FLD_VPRES) == 0x00)
  848. i->status |= V4L2_IN_ST_NO_SIGNAL;
  849. if ((gen_stat & FLD_HLOCK) == 0x00)
  850. i->status |= V4L2_IN_ST_NO_H_LOCK;
  851. }
  852. }
  853. return 0;
  854. }
  855. int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
  856. {
  857. struct cx231xx *dev = video_drvdata(file);
  858. *i = dev->video_input;
  859. return 0;
  860. }
  861. int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
  862. {
  863. struct cx231xx *dev = video_drvdata(file);
  864. dev->mode_tv = 0;
  865. if (i >= MAX_CX231XX_INPUT)
  866. return -EINVAL;
  867. if (0 == INPUT(i)->type)
  868. return -EINVAL;
  869. video_mux(dev, i);
  870. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  871. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  872. /* There's a tuner, so reset the standard and put it on the
  873. last known frequency (since it was probably powered down
  874. until now */
  875. call_all(dev, video, s_std, dev->norm);
  876. }
  877. return 0;
  878. }
  879. int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  880. {
  881. struct cx231xx *dev = video_drvdata(file);
  882. if (0 != t->index)
  883. return -EINVAL;
  884. strscpy(t->name, "Tuner", sizeof(t->name));
  885. t->type = V4L2_TUNER_ANALOG_TV;
  886. t->capability = V4L2_TUNER_CAP_NORM;
  887. t->rangehigh = 0xffffffffUL;
  888. t->signal = 0xffff; /* LOCKED */
  889. call_all(dev, tuner, g_tuner, t);
  890. return 0;
  891. }
  892. int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  893. {
  894. if (0 != t->index)
  895. return -EINVAL;
  896. return 0;
  897. }
  898. int cx231xx_g_frequency(struct file *file, void *priv,
  899. struct v4l2_frequency *f)
  900. {
  901. struct cx231xx *dev = video_drvdata(file);
  902. if (f->tuner)
  903. return -EINVAL;
  904. f->frequency = dev->ctl_freq;
  905. return 0;
  906. }
  907. int cx231xx_s_frequency(struct file *file, void *priv,
  908. const struct v4l2_frequency *f)
  909. {
  910. struct cx231xx *dev = video_drvdata(file);
  911. struct v4l2_frequency new_freq = *f;
  912. int rc, need_if_freq = 0;
  913. u32 if_frequency = 5400000;
  914. dev_dbg(dev->dev,
  915. "Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  916. f->frequency, f->type);
  917. if (0 != f->tuner)
  918. return -EINVAL;
  919. /* set pre channel change settings in DIF first */
  920. rc = cx231xx_tuner_pre_channel_change(dev);
  921. switch (dev->model) { /* i2c device tuners */
  922. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
  923. case CX231XX_BOARD_HAUPPAUGE_935C:
  924. case CX231XX_BOARD_HAUPPAUGE_955Q:
  925. case CX231XX_BOARD_HAUPPAUGE_975:
  926. case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
  927. if (dev->cx231xx_set_analog_freq)
  928. dev->cx231xx_set_analog_freq(dev, f->frequency);
  929. dev->ctl_freq = f->frequency;
  930. need_if_freq = 1;
  931. break;
  932. default:
  933. call_all(dev, tuner, s_frequency, f);
  934. call_all(dev, tuner, g_frequency, &new_freq);
  935. dev->ctl_freq = new_freq.frequency;
  936. break;
  937. }
  938. pr_debug("%s() %u : %u\n", __func__, f->frequency, dev->ctl_freq);
  939. /* set post channel change settings in DIF first */
  940. rc = cx231xx_tuner_post_channel_change(dev);
  941. if (need_if_freq || dev->tuner_type == TUNER_NXP_TDA18271) {
  942. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  943. if_frequency = 5400000; /*5.4MHz */
  944. else if (dev->norm & V4L2_STD_B)
  945. if_frequency = 6000000; /*6.0MHz */
  946. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  947. if_frequency = 6900000; /*6.9MHz */
  948. else if (dev->norm & V4L2_STD_GH)
  949. if_frequency = 7100000; /*7.1MHz */
  950. else if (dev->norm & V4L2_STD_PAL_I)
  951. if_frequency = 7250000; /*7.25MHz */
  952. else if (dev->norm & V4L2_STD_SECAM_L)
  953. if_frequency = 6900000; /*6.9MHz */
  954. else if (dev->norm & V4L2_STD_SECAM_LC)
  955. if_frequency = 1250000; /*1.25MHz */
  956. dev_dbg(dev->dev,
  957. "if_frequency is set to %d\n", if_frequency);
  958. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  959. update_HH_register_after_set_DIF(dev);
  960. }
  961. dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency);
  962. return rc;
  963. }
  964. #ifdef CONFIG_VIDEO_ADV_DEBUG
  965. int cx231xx_g_chip_info(struct file *file, void *fh,
  966. struct v4l2_dbg_chip_info *chip)
  967. {
  968. switch (chip->match.addr) {
  969. case 0: /* Cx231xx - internal registers */
  970. return 0;
  971. case 1: /* AFE - read byte */
  972. strscpy(chip->name, "AFE (byte)", sizeof(chip->name));
  973. return 0;
  974. case 2: /* Video Block - read byte */
  975. strscpy(chip->name, "Video (byte)", sizeof(chip->name));
  976. return 0;
  977. case 3: /* I2S block - read byte */
  978. strscpy(chip->name, "I2S (byte)", sizeof(chip->name));
  979. return 0;
  980. case 4: /* AFE - read dword */
  981. strscpy(chip->name, "AFE (dword)", sizeof(chip->name));
  982. return 0;
  983. case 5: /* Video Block - read dword */
  984. strscpy(chip->name, "Video (dword)", sizeof(chip->name));
  985. return 0;
  986. case 6: /* I2S Block - read dword */
  987. strscpy(chip->name, "I2S (dword)", sizeof(chip->name));
  988. return 0;
  989. }
  990. return -EINVAL;
  991. }
  992. int cx231xx_g_register(struct file *file, void *priv,
  993. struct v4l2_dbg_register *reg)
  994. {
  995. struct cx231xx *dev = video_drvdata(file);
  996. int ret;
  997. u8 value[4] = { 0, 0, 0, 0 };
  998. u32 data = 0;
  999. switch (reg->match.addr) {
  1000. case 0: /* Cx231xx - internal registers */
  1001. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1002. (u16)reg->reg, value, 4);
  1003. reg->val = value[0] | value[1] << 8 |
  1004. value[2] << 16 | (u32)value[3] << 24;
  1005. reg->size = 4;
  1006. break;
  1007. case 1: /* AFE - read byte */
  1008. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1009. (u16)reg->reg, 2, &data, 1);
  1010. reg->val = data;
  1011. reg->size = 1;
  1012. break;
  1013. case 2: /* Video Block - read byte */
  1014. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1015. (u16)reg->reg, 2, &data, 1);
  1016. reg->val = data;
  1017. reg->size = 1;
  1018. break;
  1019. case 3: /* I2S block - read byte */
  1020. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1021. (u16)reg->reg, 1, &data, 1);
  1022. reg->val = data;
  1023. reg->size = 1;
  1024. break;
  1025. case 4: /* AFE - read dword */
  1026. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1027. (u16)reg->reg, 2, &data, 4);
  1028. reg->val = data;
  1029. reg->size = 4;
  1030. break;
  1031. case 5: /* Video Block - read dword */
  1032. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1033. (u16)reg->reg, 2, &data, 4);
  1034. reg->val = data;
  1035. reg->size = 4;
  1036. break;
  1037. case 6: /* I2S Block - read dword */
  1038. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1039. (u16)reg->reg, 1, &data, 4);
  1040. reg->val = data;
  1041. reg->size = 4;
  1042. break;
  1043. default:
  1044. return -EINVAL;
  1045. }
  1046. return ret < 0 ? ret : 0;
  1047. }
  1048. int cx231xx_s_register(struct file *file, void *priv,
  1049. const struct v4l2_dbg_register *reg)
  1050. {
  1051. struct cx231xx *dev = video_drvdata(file);
  1052. int ret;
  1053. u8 data[4] = { 0, 0, 0, 0 };
  1054. switch (reg->match.addr) {
  1055. case 0: /* cx231xx internal registers */
  1056. data[0] = (u8) reg->val;
  1057. data[1] = (u8) (reg->val >> 8);
  1058. data[2] = (u8) (reg->val >> 16);
  1059. data[3] = (u8) (reg->val >> 24);
  1060. ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1061. (u16)reg->reg, data, 4);
  1062. break;
  1063. case 1: /* AFE - write byte */
  1064. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1065. (u16)reg->reg, 2, reg->val, 1);
  1066. break;
  1067. case 2: /* Video Block - write byte */
  1068. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1069. (u16)reg->reg, 2, reg->val, 1);
  1070. break;
  1071. case 3: /* I2S block - write byte */
  1072. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1073. (u16)reg->reg, 1, reg->val, 1);
  1074. break;
  1075. case 4: /* AFE - write dword */
  1076. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1077. (u16)reg->reg, 2, reg->val, 4);
  1078. break;
  1079. case 5: /* Video Block - write dword */
  1080. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1081. (u16)reg->reg, 2, reg->val, 4);
  1082. break;
  1083. case 6: /* I2S block - write dword */
  1084. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1085. (u16)reg->reg, 1, reg->val, 4);
  1086. break;
  1087. default:
  1088. return -EINVAL;
  1089. }
  1090. return ret < 0 ? ret : 0;
  1091. }
  1092. #endif
  1093. static int vidioc_g_pixelaspect(struct file *file, void *priv,
  1094. int type, struct v4l2_fract *f)
  1095. {
  1096. struct cx231xx *dev = video_drvdata(file);
  1097. bool is_50hz = dev->norm & V4L2_STD_625_50;
  1098. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1099. return -EINVAL;
  1100. f->numerator = is_50hz ? 54 : 11;
  1101. f->denominator = is_50hz ? 59 : 10;
  1102. return 0;
  1103. }
  1104. static int vidioc_g_selection(struct file *file, void *priv,
  1105. struct v4l2_selection *s)
  1106. {
  1107. struct cx231xx *dev = video_drvdata(file);
  1108. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1109. return -EINVAL;
  1110. switch (s->target) {
  1111. case V4L2_SEL_TGT_CROP_BOUNDS:
  1112. case V4L2_SEL_TGT_CROP_DEFAULT:
  1113. s->r.left = 0;
  1114. s->r.top = 0;
  1115. s->r.width = dev->width;
  1116. s->r.height = dev->height;
  1117. break;
  1118. default:
  1119. return -EINVAL;
  1120. }
  1121. return 0;
  1122. }
  1123. int cx231xx_querycap(struct file *file, void *priv,
  1124. struct v4l2_capability *cap)
  1125. {
  1126. struct cx231xx *dev = video_drvdata(file);
  1127. strscpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1128. strscpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1129. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1130. cap->capabilities = V4L2_CAP_READWRITE |
  1131. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
  1132. V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  1133. if (video_is_registered(&dev->radio_dev))
  1134. cap->capabilities |= V4L2_CAP_RADIO;
  1135. switch (dev->model) {
  1136. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
  1137. case CX231XX_BOARD_HAUPPAUGE_935C:
  1138. case CX231XX_BOARD_HAUPPAUGE_955Q:
  1139. case CX231XX_BOARD_HAUPPAUGE_975:
  1140. case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
  1141. cap->capabilities |= V4L2_CAP_TUNER;
  1142. break;
  1143. default:
  1144. if (dev->tuner_type != TUNER_ABSENT)
  1145. cap->capabilities |= V4L2_CAP_TUNER;
  1146. break;
  1147. }
  1148. return 0;
  1149. }
  1150. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1151. struct v4l2_fmtdesc *f)
  1152. {
  1153. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1154. return -EINVAL;
  1155. f->pixelformat = format[f->index].fourcc;
  1156. return 0;
  1157. }
  1158. /* RAW VBI ioctls */
  1159. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1160. struct v4l2_format *f)
  1161. {
  1162. struct cx231xx *dev = video_drvdata(file);
  1163. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1164. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1165. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1166. f->fmt.vbi.offset = 0;
  1167. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1168. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1169. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1170. PAL_VBI_LINES : NTSC_VBI_LINES;
  1171. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1172. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1173. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1174. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1175. return 0;
  1176. }
  1177. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1178. struct v4l2_format *f)
  1179. {
  1180. struct cx231xx *dev = video_drvdata(file);
  1181. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1182. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1183. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1184. f->fmt.vbi.offset = 0;
  1185. f->fmt.vbi.flags = 0;
  1186. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1187. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1188. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1189. PAL_VBI_LINES : NTSC_VBI_LINES;
  1190. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1191. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1192. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1193. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1194. return 0;
  1195. }
  1196. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1197. struct v4l2_format *f)
  1198. {
  1199. return vidioc_try_fmt_vbi_cap(file, priv, f);
  1200. }
  1201. /* ----------------------------------------------------------- */
  1202. /* RADIO ESPECIFIC IOCTLS */
  1203. /* ----------------------------------------------------------- */
  1204. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1205. {
  1206. struct cx231xx *dev = video_drvdata(file);
  1207. if (t->index)
  1208. return -EINVAL;
  1209. strscpy(t->name, "Radio", sizeof(t->name));
  1210. call_all(dev, tuner, g_tuner, t);
  1211. return 0;
  1212. }
  1213. static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  1214. {
  1215. struct cx231xx *dev = video_drvdata(file);
  1216. if (t->index)
  1217. return -EINVAL;
  1218. call_all(dev, tuner, s_tuner, t);
  1219. return 0;
  1220. }
  1221. /*
  1222. * cx231xx_v4l2_open()
  1223. * inits the device and starts isoc transfer
  1224. */
  1225. static int cx231xx_v4l2_open(struct file *filp)
  1226. {
  1227. struct video_device *vdev = video_devdata(filp);
  1228. struct cx231xx *dev = video_drvdata(filp);
  1229. int ret;
  1230. if (mutex_lock_interruptible(&dev->lock))
  1231. return -ERESTARTSYS;
  1232. ret = v4l2_fh_open(filp);
  1233. if (ret) {
  1234. mutex_unlock(&dev->lock);
  1235. return ret;
  1236. }
  1237. if (dev->users++ == 0) {
  1238. /* Power up in Analog TV mode */
  1239. if (dev->board.external_av)
  1240. cx231xx_set_power_mode(dev,
  1241. POLARIS_AVMODE_ENXTERNAL_AV);
  1242. else
  1243. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1244. /* set video alternate setting */
  1245. cx231xx_set_video_alternate(dev);
  1246. /* Needed, since GPIO might have disabled power of
  1247. some i2c device */
  1248. cx231xx_config_i2c(dev);
  1249. /* device needs to be initialized before isoc transfer */
  1250. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1251. }
  1252. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1253. cx231xx_videodbg("video_open: setting radio device\n");
  1254. /* cx231xx_start_radio(dev); */
  1255. call_all(dev, tuner, s_radio);
  1256. }
  1257. if (vdev->vfl_type == VFL_TYPE_VBI) {
  1258. /* Set the required alternate setting VBI interface works in
  1259. Bulk mode only */
  1260. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1261. }
  1262. mutex_unlock(&dev->lock);
  1263. return 0;
  1264. }
  1265. /*
  1266. * cx231xx_realease_resources()
  1267. * unregisters the v4l2,i2c and usb devices
  1268. * called when the device gets disconnected or at module unload
  1269. */
  1270. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1271. {
  1272. /*FIXME: I2C IR should be disconnected */
  1273. if (video_is_registered(&dev->radio_dev))
  1274. video_unregister_device(&dev->radio_dev);
  1275. if (video_is_registered(&dev->vbi_dev)) {
  1276. dev_info(dev->dev, "V4L2 device %s deregistered\n",
  1277. video_device_node_name(&dev->vbi_dev));
  1278. video_unregister_device(&dev->vbi_dev);
  1279. }
  1280. if (video_is_registered(&dev->vdev)) {
  1281. dev_info(dev->dev, "V4L2 device %s deregistered\n",
  1282. video_device_node_name(&dev->vdev));
  1283. if (dev->board.has_417)
  1284. cx231xx_417_unregister(dev);
  1285. video_unregister_device(&dev->vdev);
  1286. }
  1287. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1288. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1289. }
  1290. /*
  1291. * cx231xx_close()
  1292. * stops streaming and deallocates all resources allocated by the v4l2
  1293. * calls and ioctls
  1294. */
  1295. static int cx231xx_close(struct file *filp)
  1296. {
  1297. struct cx231xx *dev = video_drvdata(filp);
  1298. struct video_device *vdev = video_devdata(filp);
  1299. _vb2_fop_release(filp, NULL);
  1300. if (--dev->users == 0) {
  1301. /* Save some power by putting tuner to sleep */
  1302. call_all(dev, tuner, standby);
  1303. /* do this before setting alternate! */
  1304. if (dev->USE_ISO)
  1305. cx231xx_uninit_isoc(dev);
  1306. else
  1307. cx231xx_uninit_bulk(dev);
  1308. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1309. }
  1310. /*
  1311. * To workaround error number=-71 on EP0 for VideoGrabber,
  1312. * need exclude following.
  1313. * FIXME: It is probably safe to remove most of these, as we're
  1314. * now avoiding the alternate setting for INDEX_VANC
  1315. */
  1316. if (!dev->board.no_alt_vanc && vdev->vfl_type == VFL_TYPE_VBI) {
  1317. /* do this before setting alternate! */
  1318. cx231xx_uninit_vbi_isoc(dev);
  1319. /* set alternate 0 */
  1320. if (!dev->vbi_or_sliced_cc_mode)
  1321. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1322. else
  1323. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1324. wake_up_interruptible_nr(&dev->open, 1);
  1325. return 0;
  1326. }
  1327. if (dev->users == 0) {
  1328. /* set alternate 0 */
  1329. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1330. }
  1331. wake_up_interruptible(&dev->open);
  1332. return 0;
  1333. }
  1334. static int cx231xx_v4l2_close(struct file *filp)
  1335. {
  1336. struct cx231xx *dev = video_drvdata(filp);
  1337. int rc;
  1338. mutex_lock(&dev->lock);
  1339. rc = cx231xx_close(filp);
  1340. mutex_unlock(&dev->lock);
  1341. return rc;
  1342. }
  1343. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1344. .owner = THIS_MODULE,
  1345. .open = cx231xx_v4l2_open,
  1346. .release = cx231xx_v4l2_close,
  1347. .read = vb2_fop_read,
  1348. .poll = vb2_fop_poll,
  1349. .mmap = vb2_fop_mmap,
  1350. .unlocked_ioctl = video_ioctl2,
  1351. };
  1352. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1353. .vidioc_querycap = cx231xx_querycap,
  1354. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1355. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1356. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1357. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1358. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1359. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1360. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1361. .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
  1362. .vidioc_g_selection = vidioc_g_selection,
  1363. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1364. .vidioc_querybuf = vb2_ioctl_querybuf,
  1365. .vidioc_qbuf = vb2_ioctl_qbuf,
  1366. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1367. .vidioc_s_std = vidioc_s_std,
  1368. .vidioc_g_std = vidioc_g_std,
  1369. .vidioc_enum_input = cx231xx_enum_input,
  1370. .vidioc_g_input = cx231xx_g_input,
  1371. .vidioc_s_input = cx231xx_s_input,
  1372. .vidioc_streamon = vb2_ioctl_streamon,
  1373. .vidioc_streamoff = vb2_ioctl_streamoff,
  1374. .vidioc_g_tuner = cx231xx_g_tuner,
  1375. .vidioc_s_tuner = cx231xx_s_tuner,
  1376. .vidioc_g_frequency = cx231xx_g_frequency,
  1377. .vidioc_s_frequency = cx231xx_s_frequency,
  1378. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1379. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1380. .vidioc_g_register = cx231xx_g_register,
  1381. .vidioc_s_register = cx231xx_s_register,
  1382. #endif
  1383. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1384. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1385. };
  1386. static struct video_device cx231xx_vbi_template;
  1387. static const struct video_device cx231xx_video_template = {
  1388. .fops = &cx231xx_v4l_fops,
  1389. .release = video_device_release_empty,
  1390. .ioctl_ops = &video_ioctl_ops,
  1391. .tvnorms = V4L2_STD_ALL,
  1392. };
  1393. static const struct v4l2_file_operations radio_fops = {
  1394. .owner = THIS_MODULE,
  1395. .open = cx231xx_v4l2_open,
  1396. .release = cx231xx_v4l2_close,
  1397. .poll = v4l2_ctrl_poll,
  1398. .unlocked_ioctl = video_ioctl2,
  1399. };
  1400. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1401. .vidioc_querycap = cx231xx_querycap,
  1402. .vidioc_g_tuner = radio_g_tuner,
  1403. .vidioc_s_tuner = radio_s_tuner,
  1404. .vidioc_g_frequency = cx231xx_g_frequency,
  1405. .vidioc_s_frequency = cx231xx_s_frequency,
  1406. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1407. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1408. .vidioc_g_register = cx231xx_g_register,
  1409. .vidioc_s_register = cx231xx_s_register,
  1410. #endif
  1411. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1412. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1413. };
  1414. static struct video_device cx231xx_radio_template = {
  1415. .name = "cx231xx-radio",
  1416. .fops = &radio_fops,
  1417. .ioctl_ops = &radio_ioctl_ops,
  1418. };
  1419. /******************************** usb interface ******************************/
  1420. static void cx231xx_vdev_init(struct cx231xx *dev,
  1421. struct video_device *vfd,
  1422. const struct video_device *template,
  1423. const char *type_name)
  1424. {
  1425. *vfd = *template;
  1426. vfd->v4l2_dev = &dev->v4l2_dev;
  1427. vfd->release = video_device_release_empty;
  1428. vfd->lock = &dev->lock;
  1429. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1430. video_set_drvdata(vfd, dev);
  1431. if (dev->tuner_type == TUNER_ABSENT) {
  1432. switch (dev->model) {
  1433. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
  1434. case CX231XX_BOARD_HAUPPAUGE_935C:
  1435. case CX231XX_BOARD_HAUPPAUGE_955Q:
  1436. case CX231XX_BOARD_HAUPPAUGE_975:
  1437. case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
  1438. break;
  1439. default:
  1440. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  1441. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  1442. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  1443. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1449. {
  1450. struct vb2_queue *q;
  1451. int ret;
  1452. dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
  1453. /* set default norm */
  1454. dev->norm = V4L2_STD_PAL;
  1455. dev->width = norm_maxw(dev);
  1456. dev->height = norm_maxh(dev);
  1457. dev->interlaced = 0;
  1458. /* Analog specific initialization */
  1459. dev->format = &format[0];
  1460. /* Set the initial input */
  1461. video_mux(dev, dev->video_input);
  1462. call_all(dev, video, s_std, dev->norm);
  1463. v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
  1464. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
  1465. if (dev->sd_cx25840) {
  1466. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1467. dev->sd_cx25840->ctrl_handler, NULL, true);
  1468. v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
  1469. dev->sd_cx25840->ctrl_handler,
  1470. v4l2_ctrl_radio_filter, true);
  1471. }
  1472. if (dev->ctrl_handler.error)
  1473. return dev->ctrl_handler.error;
  1474. if (dev->radio_ctrl_handler.error)
  1475. return dev->radio_ctrl_handler.error;
  1476. /* enable vbi capturing */
  1477. /* write code here... */
  1478. /* allocate and fill video video_device struct */
  1479. cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video");
  1480. #if defined(CONFIG_MEDIA_CONTROLLER)
  1481. dev->video_pad.flags = MEDIA_PAD_FL_SINK;
  1482. ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
  1483. if (ret < 0)
  1484. dev_err(dev->dev, "failed to initialize video media entity!\n");
  1485. #endif
  1486. dev->vdev.ctrl_handler = &dev->ctrl_handler;
  1487. q = &dev->vidq;
  1488. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1489. q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
  1490. q->drv_priv = dev;
  1491. q->buf_struct_size = sizeof(struct cx231xx_buffer);
  1492. q->ops = &cx231xx_video_qops;
  1493. q->mem_ops = &vb2_vmalloc_memops;
  1494. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1495. q->min_buffers_needed = 1;
  1496. q->lock = &dev->lock;
  1497. ret = vb2_queue_init(q);
  1498. if (ret)
  1499. return ret;
  1500. dev->vdev.queue = q;
  1501. dev->vdev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
  1502. V4L2_CAP_VIDEO_CAPTURE;
  1503. switch (dev->model) { /* i2c device tuners */
  1504. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
  1505. case CX231XX_BOARD_HAUPPAUGE_935C:
  1506. case CX231XX_BOARD_HAUPPAUGE_955Q:
  1507. case CX231XX_BOARD_HAUPPAUGE_975:
  1508. case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
  1509. dev->vdev.device_caps |= V4L2_CAP_TUNER;
  1510. break;
  1511. default:
  1512. if (dev->tuner_type != TUNER_ABSENT)
  1513. dev->vdev.device_caps |= V4L2_CAP_TUNER;
  1514. break;
  1515. }
  1516. /* register v4l2 video video_device */
  1517. ret = video_register_device(&dev->vdev, VFL_TYPE_VIDEO,
  1518. video_nr[dev->devno]);
  1519. if (ret) {
  1520. dev_err(dev->dev,
  1521. "unable to register video device (error=%i).\n",
  1522. ret);
  1523. return ret;
  1524. }
  1525. dev_info(dev->dev, "Registered video device %s [v4l2]\n",
  1526. video_device_node_name(&dev->vdev));
  1527. /* Initialize VBI template */
  1528. cx231xx_vbi_template = cx231xx_video_template;
  1529. strscpy(cx231xx_vbi_template.name, "cx231xx-vbi",
  1530. sizeof(cx231xx_vbi_template.name));
  1531. /* Allocate and fill vbi video_device struct */
  1532. cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi");
  1533. #if defined(CONFIG_MEDIA_CONTROLLER)
  1534. dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
  1535. ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
  1536. if (ret < 0)
  1537. dev_err(dev->dev, "failed to initialize vbi media entity!\n");
  1538. #endif
  1539. dev->vbi_dev.ctrl_handler = &dev->ctrl_handler;
  1540. q = &dev->vbiq;
  1541. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1542. q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
  1543. q->drv_priv = dev;
  1544. q->buf_struct_size = sizeof(struct cx231xx_buffer);
  1545. q->ops = &cx231xx_vbi_qops;
  1546. q->mem_ops = &vb2_vmalloc_memops;
  1547. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1548. q->min_buffers_needed = 1;
  1549. q->lock = &dev->lock;
  1550. ret = vb2_queue_init(q);
  1551. if (ret)
  1552. return ret;
  1553. dev->vbi_dev.queue = q;
  1554. dev->vbi_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
  1555. V4L2_CAP_VBI_CAPTURE;
  1556. switch (dev->model) { /* i2c device tuners */
  1557. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
  1558. case CX231XX_BOARD_HAUPPAUGE_935C:
  1559. case CX231XX_BOARD_HAUPPAUGE_955Q:
  1560. case CX231XX_BOARD_HAUPPAUGE_975:
  1561. case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
  1562. dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
  1563. break;
  1564. default:
  1565. if (dev->tuner_type != TUNER_ABSENT)
  1566. dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
  1567. }
  1568. /* register v4l2 vbi video_device */
  1569. ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
  1570. vbi_nr[dev->devno]);
  1571. if (ret < 0) {
  1572. dev_err(dev->dev, "unable to register vbi device\n");
  1573. return ret;
  1574. }
  1575. dev_info(dev->dev, "Registered VBI device %s\n",
  1576. video_device_node_name(&dev->vbi_dev));
  1577. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1578. cx231xx_vdev_init(dev, &dev->radio_dev,
  1579. &cx231xx_radio_template, "radio");
  1580. dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
  1581. dev->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  1582. ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1583. radio_nr[dev->devno]);
  1584. if (ret < 0) {
  1585. dev_err(dev->dev,
  1586. "can't register radio device\n");
  1587. return ret;
  1588. }
  1589. dev_info(dev->dev, "Registered radio device as %s\n",
  1590. video_device_node_name(&dev->radio_dev));
  1591. }
  1592. return 0;
  1593. }