vivid-core.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * vivid-core.c - A Virtual Video Test Driver, core initialization
  3. *
  4. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/font.h>
  27. #include <linux/mutex.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/videodev2.h>
  30. #include <linux/v4l2-dv-timings.h>
  31. #include <media/videobuf2-vmalloc.h>
  32. #include <media/v4l2-dv-timings.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-fh.h>
  35. #include <media/v4l2-event.h>
  36. #include "vivid-core.h"
  37. #include "vivid-vid-common.h"
  38. #include "vivid-vid-cap.h"
  39. #include "vivid-vid-out.h"
  40. #include "vivid-radio-common.h"
  41. #include "vivid-radio-rx.h"
  42. #include "vivid-radio-tx.h"
  43. #include "vivid-sdr-cap.h"
  44. #include "vivid-vbi-cap.h"
  45. #include "vivid-vbi-out.h"
  46. #include "vivid-osd.h"
  47. #include "vivid-ctrls.h"
  48. #define VIVID_MODULE_NAME "vivid"
  49. /* The maximum number of vivid devices */
  50. #define VIVID_MAX_DEVS CONFIG_VIDEO_VIVID_MAX_DEVS
  51. MODULE_DESCRIPTION("Virtual Video Test Driver");
  52. MODULE_AUTHOR("Hans Verkuil");
  53. MODULE_LICENSE("GPL");
  54. static unsigned n_devs = 1;
  55. module_param(n_devs, uint, 0444);
  56. MODULE_PARM_DESC(n_devs, " number of driver instances to create");
  57. static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  58. module_param_array(vid_cap_nr, int, NULL, 0444);
  59. MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
  60. static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  61. module_param_array(vid_out_nr, int, NULL, 0444);
  62. MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
  63. static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  64. module_param_array(vbi_cap_nr, int, NULL, 0444);
  65. MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
  66. static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  67. module_param_array(vbi_out_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
  69. static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  70. module_param_array(sdr_cap_nr, int, NULL, 0444);
  71. MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
  72. static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  73. module_param_array(radio_rx_nr, int, NULL, 0444);
  74. MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
  75. static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  76. module_param_array(radio_tx_nr, int, NULL, 0444);
  77. MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
  78. static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  79. module_param_array(ccs_cap_mode, int, NULL, 0444);
  80. MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
  81. "\t\t bit 0=crop, 1=compose, 2=scale,\n"
  82. "\t\t -1=user-controlled (default)");
  83. static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  84. module_param_array(ccs_out_mode, int, NULL, 0444);
  85. MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
  86. "\t\t bit 0=crop, 1=compose, 2=scale,\n"
  87. "\t\t -1=user-controlled (default)");
  88. static unsigned multiplanar[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 1 };
  89. module_param_array(multiplanar, uint, NULL, 0444);
  90. MODULE_PARM_DESC(multiplanar, " 1 (default) creates a single planar device, 2 creates a multiplanar device.");
  91. /* Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr + vbi-out + vid-out */
  92. static unsigned node_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0x1d3d };
  93. module_param_array(node_types, uint, NULL, 0444);
  94. MODULE_PARM_DESC(node_types, " node types, default is 0x1d3d. Bitmask with the following meaning:\n"
  95. "\t\t bit 0: Video Capture node\n"
  96. "\t\t bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
  97. "\t\t bit 4: Radio Receiver node\n"
  98. "\t\t bit 5: Software Defined Radio Receiver node\n"
  99. "\t\t bit 8: Video Output node\n"
  100. "\t\t bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
  101. "\t\t bit 12: Radio Transmitter node\n"
  102. "\t\t bit 16: Framebuffer for testing overlays");
  103. /* Default: 4 inputs */
  104. static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
  105. module_param_array(num_inputs, uint, NULL, 0444);
  106. MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
  107. /* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
  108. static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
  109. module_param_array(input_types, uint, NULL, 0444);
  110. MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
  111. "\t\t bits 0-1 == input 0, bits 31-30 == input 15.\n"
  112. "\t\t Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
  113. /* Default: 2 outputs */
  114. static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
  115. module_param_array(num_outputs, uint, NULL, 0444);
  116. MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
  117. /* Default: output 0 = SVID, 1 = HDMI */
  118. static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
  119. module_param_array(output_types, uint, NULL, 0444);
  120. MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
  121. "\t\t bit 0 == output 0, bit 15 == output 15.\n"
  122. "\t\t Type 0 == S-Video, 1 == HDMI");
  123. unsigned vivid_debug;
  124. module_param(vivid_debug, uint, 0644);
  125. MODULE_PARM_DESC(vivid_debug, " activates debug info");
  126. static bool no_error_inj;
  127. module_param(no_error_inj, bool, 0444);
  128. MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
  129. static struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
  130. const struct v4l2_rect vivid_min_rect = {
  131. 0, 0, MIN_WIDTH, MIN_HEIGHT
  132. };
  133. const struct v4l2_rect vivid_max_rect = {
  134. 0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
  135. };
  136. static const u8 vivid_hdmi_edid[256] = {
  137. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  138. 0x63, 0x3a, 0xaa, 0x55, 0x00, 0x00, 0x00, 0x00,
  139. 0x0a, 0x18, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
  140. 0x0e, 0x00, 0xb2, 0xa0, 0x57, 0x49, 0x9b, 0x26,
  141. 0x10, 0x48, 0x4f, 0x2f, 0xcf, 0x00, 0x31, 0x59,
  142. 0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
  143. 0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x02, 0x3a,
  144. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  145. 0x46, 0x00, 0x10, 0x09, 0x00, 0x00, 0x00, 0x1e,
  146. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
  147. 0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
  148. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 'v',
  149. '4', 'l', '2', '-', 'h', 'd', 'm', 'i',
  150. 0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x10,
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0,
  153. 0x02, 0x03, 0x1a, 0xc0, 0x48, 0xa2, 0x10, 0x04,
  154. 0x02, 0x01, 0x21, 0x14, 0x13, 0x23, 0x09, 0x07,
  155. 0x07, 0x65, 0x03, 0x0c, 0x00, 0x10, 0x00, 0xe2,
  156. 0x00, 0x2a, 0x01, 0x1d, 0x00, 0x80, 0x51, 0xd0,
  157. 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0x00, 0x00,
  158. 0x00, 0x00, 0x00, 0x1e, 0x8c, 0x0a, 0xd0, 0x8a,
  159. 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd7
  169. };
  170. static int vidioc_querycap(struct file *file, void *priv,
  171. struct v4l2_capability *cap)
  172. {
  173. struct vivid_dev *dev = video_drvdata(file);
  174. struct video_device *vdev = video_devdata(file);
  175. strcpy(cap->driver, "vivid");
  176. strcpy(cap->card, "vivid");
  177. snprintf(cap->bus_info, sizeof(cap->bus_info),
  178. "platform:%s", dev->v4l2_dev.name);
  179. if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_RX)
  180. cap->device_caps = dev->vid_cap_caps;
  181. if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_TX)
  182. cap->device_caps = dev->vid_out_caps;
  183. else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_RX)
  184. cap->device_caps = dev->vbi_cap_caps;
  185. else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_TX)
  186. cap->device_caps = dev->vbi_out_caps;
  187. else if (vdev->vfl_type == VFL_TYPE_SDR)
  188. cap->device_caps = dev->sdr_cap_caps;
  189. else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_RX)
  190. cap->device_caps = dev->radio_rx_caps;
  191. else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_TX)
  192. cap->device_caps = dev->radio_tx_caps;
  193. cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
  194. dev->vbi_cap_caps | dev->vbi_out_caps |
  195. dev->radio_rx_caps | dev->radio_tx_caps |
  196. dev->sdr_cap_caps | V4L2_CAP_DEVICE_CAPS;
  197. return 0;
  198. }
  199. static int vidioc_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
  200. {
  201. struct video_device *vdev = video_devdata(file);
  202. if (vdev->vfl_type == VFL_TYPE_RADIO)
  203. return vivid_radio_rx_s_hw_freq_seek(file, fh, a);
  204. return -ENOTTY;
  205. }
  206. static int vidioc_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
  207. {
  208. struct video_device *vdev = video_devdata(file);
  209. if (vdev->vfl_type == VFL_TYPE_RADIO)
  210. return vivid_radio_rx_enum_freq_bands(file, fh, band);
  211. if (vdev->vfl_type == VFL_TYPE_SDR)
  212. return vivid_sdr_enum_freq_bands(file, fh, band);
  213. return -ENOTTY;
  214. }
  215. static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  216. {
  217. struct video_device *vdev = video_devdata(file);
  218. if (vdev->vfl_type == VFL_TYPE_RADIO)
  219. return vivid_radio_rx_g_tuner(file, fh, vt);
  220. if (vdev->vfl_type == VFL_TYPE_SDR)
  221. return vivid_sdr_g_tuner(file, fh, vt);
  222. return vivid_video_g_tuner(file, fh, vt);
  223. }
  224. static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
  225. {
  226. struct video_device *vdev = video_devdata(file);
  227. if (vdev->vfl_type == VFL_TYPE_RADIO)
  228. return vivid_radio_rx_s_tuner(file, fh, vt);
  229. if (vdev->vfl_type == VFL_TYPE_SDR)
  230. return vivid_sdr_s_tuner(file, fh, vt);
  231. return vivid_video_s_tuner(file, fh, vt);
  232. }
  233. static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  234. {
  235. struct vivid_dev *dev = video_drvdata(file);
  236. struct video_device *vdev = video_devdata(file);
  237. if (vdev->vfl_type == VFL_TYPE_RADIO)
  238. return vivid_radio_g_frequency(file,
  239. vdev->vfl_dir == VFL_DIR_RX ?
  240. &dev->radio_rx_freq : &dev->radio_tx_freq, vf);
  241. if (vdev->vfl_type == VFL_TYPE_SDR)
  242. return vivid_sdr_g_frequency(file, fh, vf);
  243. return vivid_video_g_frequency(file, fh, vf);
  244. }
  245. static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
  246. {
  247. struct vivid_dev *dev = video_drvdata(file);
  248. struct video_device *vdev = video_devdata(file);
  249. if (vdev->vfl_type == VFL_TYPE_RADIO)
  250. return vivid_radio_s_frequency(file,
  251. vdev->vfl_dir == VFL_DIR_RX ?
  252. &dev->radio_rx_freq : &dev->radio_tx_freq, vf);
  253. if (vdev->vfl_type == VFL_TYPE_SDR)
  254. return vivid_sdr_s_frequency(file, fh, vf);
  255. return vivid_video_s_frequency(file, fh, vf);
  256. }
  257. static int vidioc_overlay(struct file *file, void *fh, unsigned i)
  258. {
  259. struct video_device *vdev = video_devdata(file);
  260. if (vdev->vfl_dir == VFL_DIR_RX)
  261. return vivid_vid_cap_overlay(file, fh, i);
  262. return vivid_vid_out_overlay(file, fh, i);
  263. }
  264. static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *a)
  265. {
  266. struct video_device *vdev = video_devdata(file);
  267. if (vdev->vfl_dir == VFL_DIR_RX)
  268. return vivid_vid_cap_g_fbuf(file, fh, a);
  269. return vivid_vid_out_g_fbuf(file, fh, a);
  270. }
  271. static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
  272. {
  273. struct video_device *vdev = video_devdata(file);
  274. if (vdev->vfl_dir == VFL_DIR_RX)
  275. return vivid_vid_cap_s_fbuf(file, fh, a);
  276. return vivid_vid_out_s_fbuf(file, fh, a);
  277. }
  278. static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
  279. {
  280. struct video_device *vdev = video_devdata(file);
  281. if (vdev->vfl_dir == VFL_DIR_RX)
  282. return vivid_vid_cap_s_std(file, fh, id);
  283. return vivid_vid_out_s_std(file, fh, id);
  284. }
  285. static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
  286. {
  287. struct video_device *vdev = video_devdata(file);
  288. if (vdev->vfl_dir == VFL_DIR_RX)
  289. return vivid_vid_cap_s_dv_timings(file, fh, timings);
  290. return vivid_vid_out_s_dv_timings(file, fh, timings);
  291. }
  292. static int vidioc_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cc)
  293. {
  294. struct video_device *vdev = video_devdata(file);
  295. if (vdev->vfl_dir == VFL_DIR_RX)
  296. return vivid_vid_cap_cropcap(file, fh, cc);
  297. return vivid_vid_out_cropcap(file, fh, cc);
  298. }
  299. static int vidioc_g_selection(struct file *file, void *fh,
  300. struct v4l2_selection *sel)
  301. {
  302. struct video_device *vdev = video_devdata(file);
  303. if (vdev->vfl_dir == VFL_DIR_RX)
  304. return vivid_vid_cap_g_selection(file, fh, sel);
  305. return vivid_vid_out_g_selection(file, fh, sel);
  306. }
  307. static int vidioc_s_selection(struct file *file, void *fh,
  308. struct v4l2_selection *sel)
  309. {
  310. struct video_device *vdev = video_devdata(file);
  311. if (vdev->vfl_dir == VFL_DIR_RX)
  312. return vivid_vid_cap_s_selection(file, fh, sel);
  313. return vivid_vid_out_s_selection(file, fh, sel);
  314. }
  315. static int vidioc_g_parm(struct file *file, void *fh,
  316. struct v4l2_streamparm *parm)
  317. {
  318. struct video_device *vdev = video_devdata(file);
  319. if (vdev->vfl_dir == VFL_DIR_RX)
  320. return vivid_vid_cap_g_parm(file, fh, parm);
  321. return vivid_vid_out_g_parm(file, fh, parm);
  322. }
  323. static int vidioc_s_parm(struct file *file, void *fh,
  324. struct v4l2_streamparm *parm)
  325. {
  326. struct video_device *vdev = video_devdata(file);
  327. if (vdev->vfl_dir == VFL_DIR_RX)
  328. return vivid_vid_cap_s_parm(file, fh, parm);
  329. return vivid_vid_out_g_parm(file, fh, parm);
  330. }
  331. static int vidioc_log_status(struct file *file, void *fh)
  332. {
  333. struct vivid_dev *dev = video_drvdata(file);
  334. struct video_device *vdev = video_devdata(file);
  335. v4l2_ctrl_log_status(file, fh);
  336. if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_GRABBER)
  337. tpg_log_status(&dev->tpg);
  338. return 0;
  339. }
  340. static ssize_t vivid_radio_read(struct file *file, char __user *buf,
  341. size_t size, loff_t *offset)
  342. {
  343. struct video_device *vdev = video_devdata(file);
  344. if (vdev->vfl_dir == VFL_DIR_TX)
  345. return -EINVAL;
  346. return vivid_radio_rx_read(file, buf, size, offset);
  347. }
  348. static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
  349. size_t size, loff_t *offset)
  350. {
  351. struct video_device *vdev = video_devdata(file);
  352. if (vdev->vfl_dir == VFL_DIR_RX)
  353. return -EINVAL;
  354. return vivid_radio_tx_write(file, buf, size, offset);
  355. }
  356. static unsigned int vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
  357. {
  358. struct video_device *vdev = video_devdata(file);
  359. if (vdev->vfl_dir == VFL_DIR_RX)
  360. return vivid_radio_rx_poll(file, wait);
  361. return vivid_radio_tx_poll(file, wait);
  362. }
  363. static bool vivid_is_in_use(struct video_device *vdev)
  364. {
  365. unsigned long flags;
  366. bool res;
  367. spin_lock_irqsave(&vdev->fh_lock, flags);
  368. res = !list_empty(&vdev->fh_list);
  369. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  370. return res;
  371. }
  372. static bool vivid_is_last_user(struct vivid_dev *dev)
  373. {
  374. unsigned uses = vivid_is_in_use(&dev->vid_cap_dev) +
  375. vivid_is_in_use(&dev->vid_out_dev) +
  376. vivid_is_in_use(&dev->vbi_cap_dev) +
  377. vivid_is_in_use(&dev->vbi_out_dev) +
  378. vivid_is_in_use(&dev->sdr_cap_dev) +
  379. vivid_is_in_use(&dev->radio_rx_dev) +
  380. vivid_is_in_use(&dev->radio_tx_dev);
  381. return uses == 1;
  382. }
  383. static int vivid_fop_release(struct file *file)
  384. {
  385. struct vivid_dev *dev = video_drvdata(file);
  386. struct video_device *vdev = video_devdata(file);
  387. mutex_lock(&dev->mutex);
  388. if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
  389. !video_is_registered(vdev) && vivid_is_last_user(dev)) {
  390. /*
  391. * I am the last user of this driver, and a disconnect
  392. * was forced (since this video_device is unregistered),
  393. * so re-register all video_device's again.
  394. */
  395. v4l2_info(&dev->v4l2_dev, "reconnect\n");
  396. set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
  397. set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
  398. set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
  399. set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
  400. set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
  401. set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
  402. set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
  403. }
  404. mutex_unlock(&dev->mutex);
  405. if (file->private_data == dev->overlay_cap_owner)
  406. dev->overlay_cap_owner = NULL;
  407. if (file->private_data == dev->radio_rx_rds_owner) {
  408. dev->radio_rx_rds_last_block = 0;
  409. dev->radio_rx_rds_owner = NULL;
  410. }
  411. if (file->private_data == dev->radio_tx_rds_owner) {
  412. dev->radio_tx_rds_last_block = 0;
  413. dev->radio_tx_rds_owner = NULL;
  414. }
  415. if (vdev->queue)
  416. return vb2_fop_release(file);
  417. return v4l2_fh_release(file);
  418. }
  419. static const struct v4l2_file_operations vivid_fops = {
  420. .owner = THIS_MODULE,
  421. .open = v4l2_fh_open,
  422. .release = vivid_fop_release,
  423. .read = vb2_fop_read,
  424. .write = vb2_fop_write,
  425. .poll = vb2_fop_poll,
  426. .unlocked_ioctl = video_ioctl2,
  427. .mmap = vb2_fop_mmap,
  428. };
  429. static const struct v4l2_file_operations vivid_radio_fops = {
  430. .owner = THIS_MODULE,
  431. .open = v4l2_fh_open,
  432. .release = vivid_fop_release,
  433. .read = vivid_radio_read,
  434. .write = vivid_radio_write,
  435. .poll = vivid_radio_poll,
  436. .unlocked_ioctl = video_ioctl2,
  437. };
  438. static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
  439. .vidioc_querycap = vidioc_querycap,
  440. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid,
  441. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  442. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  443. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  444. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_mplane,
  445. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap_mplane,
  446. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
  447. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_vid_cap_mplane,
  448. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid,
  449. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  450. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  451. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  452. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_mplane,
  453. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out_mplane,
  454. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
  455. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_vid_out_mplane,
  456. .vidioc_g_selection = vidioc_g_selection,
  457. .vidioc_s_selection = vidioc_s_selection,
  458. .vidioc_cropcap = vidioc_cropcap,
  459. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  460. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  461. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  462. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  463. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_fmt_sliced_vbi_cap,
  464. .vidioc_s_fmt_sliced_vbi_cap = vidioc_s_fmt_sliced_vbi_cap,
  465. .vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap,
  466. .vidioc_g_fmt_vbi_out = vidioc_g_fmt_vbi_out,
  467. .vidioc_try_fmt_vbi_out = vidioc_g_fmt_vbi_out,
  468. .vidioc_s_fmt_vbi_out = vidioc_s_fmt_vbi_out,
  469. .vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out,
  470. .vidioc_try_fmt_sliced_vbi_out = vidioc_try_fmt_sliced_vbi_out,
  471. .vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out,
  472. .vidioc_enum_fmt_sdr_cap = vidioc_enum_fmt_sdr_cap,
  473. .vidioc_g_fmt_sdr_cap = vidioc_g_fmt_sdr_cap,
  474. .vidioc_try_fmt_sdr_cap = vidioc_try_fmt_sdr_cap,
  475. .vidioc_s_fmt_sdr_cap = vidioc_s_fmt_sdr_cap,
  476. .vidioc_overlay = vidioc_overlay,
  477. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  478. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  479. .vidioc_g_parm = vidioc_g_parm,
  480. .vidioc_s_parm = vidioc_s_parm,
  481. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  482. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  483. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  484. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  485. .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_out_overlay,
  486. .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_out_overlay,
  487. .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_out_overlay,
  488. .vidioc_g_fbuf = vidioc_g_fbuf,
  489. .vidioc_s_fbuf = vidioc_s_fbuf,
  490. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  491. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  492. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  493. .vidioc_querybuf = vb2_ioctl_querybuf,
  494. .vidioc_qbuf = vb2_ioctl_qbuf,
  495. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  496. .vidioc_expbuf = vb2_ioctl_expbuf,
  497. .vidioc_streamon = vb2_ioctl_streamon,
  498. .vidioc_streamoff = vb2_ioctl_streamoff,
  499. .vidioc_enum_input = vidioc_enum_input,
  500. .vidioc_g_input = vidioc_g_input,
  501. .vidioc_s_input = vidioc_s_input,
  502. .vidioc_s_audio = vidioc_s_audio,
  503. .vidioc_g_audio = vidioc_g_audio,
  504. .vidioc_enumaudio = vidioc_enumaudio,
  505. .vidioc_s_frequency = vidioc_s_frequency,
  506. .vidioc_g_frequency = vidioc_g_frequency,
  507. .vidioc_s_tuner = vidioc_s_tuner,
  508. .vidioc_g_tuner = vidioc_g_tuner,
  509. .vidioc_s_modulator = vidioc_s_modulator,
  510. .vidioc_g_modulator = vidioc_g_modulator,
  511. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  512. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  513. .vidioc_enum_output = vidioc_enum_output,
  514. .vidioc_g_output = vidioc_g_output,
  515. .vidioc_s_output = vidioc_s_output,
  516. .vidioc_s_audout = vidioc_s_audout,
  517. .vidioc_g_audout = vidioc_g_audout,
  518. .vidioc_enumaudout = vidioc_enumaudout,
  519. .vidioc_querystd = vidioc_querystd,
  520. .vidioc_g_std = vidioc_g_std,
  521. .vidioc_s_std = vidioc_s_std,
  522. .vidioc_s_dv_timings = vidioc_s_dv_timings,
  523. .vidioc_g_dv_timings = vidioc_g_dv_timings,
  524. .vidioc_query_dv_timings = vidioc_query_dv_timings,
  525. .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
  526. .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
  527. .vidioc_g_edid = vidioc_g_edid,
  528. .vidioc_s_edid = vidioc_s_edid,
  529. .vidioc_log_status = vidioc_log_status,
  530. .vidioc_subscribe_event = vidioc_subscribe_event,
  531. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  532. };
  533. /* -----------------------------------------------------------------
  534. Initialization and module stuff
  535. ------------------------------------------------------------------*/
  536. static void vivid_dev_release(struct v4l2_device *v4l2_dev)
  537. {
  538. struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
  539. vivid_free_controls(dev);
  540. v4l2_device_unregister(&dev->v4l2_dev);
  541. vfree(dev->scaled_line);
  542. vfree(dev->blended_line);
  543. vfree(dev->edid);
  544. vfree(dev->bitmap_cap);
  545. vfree(dev->bitmap_out);
  546. tpg_free(&dev->tpg);
  547. kfree(dev->query_dv_timings_qmenu);
  548. kfree(dev);
  549. }
  550. static int vivid_create_instance(struct platform_device *pdev, int inst)
  551. {
  552. static const struct v4l2_dv_timings def_dv_timings =
  553. V4L2_DV_BT_CEA_1280X720P60;
  554. unsigned in_type_counter[4] = { 0, 0, 0, 0 };
  555. unsigned out_type_counter[4] = { 0, 0, 0, 0 };
  556. int ccs_cap = ccs_cap_mode[inst];
  557. int ccs_out = ccs_out_mode[inst];
  558. bool has_tuner;
  559. bool has_modulator;
  560. struct vivid_dev *dev;
  561. struct video_device *vfd;
  562. struct vb2_queue *q;
  563. unsigned node_type = node_types[inst];
  564. v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
  565. int ret;
  566. int i;
  567. /* allocate main vivid state structure */
  568. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  569. if (!dev)
  570. return -ENOMEM;
  571. dev->inst = inst;
  572. /* register v4l2_device */
  573. snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  574. "%s-%03d", VIVID_MODULE_NAME, inst);
  575. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  576. if (ret) {
  577. kfree(dev);
  578. return ret;
  579. }
  580. dev->v4l2_dev.release = vivid_dev_release;
  581. /* start detecting feature set */
  582. /* do we use single- or multi-planar? */
  583. dev->multiplanar = multiplanar[inst] > 1;
  584. v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
  585. dev->multiplanar ? "multi" : "single ");
  586. /* how many inputs do we have and of what type? */
  587. dev->num_inputs = num_inputs[inst];
  588. if (dev->num_inputs < 1)
  589. dev->num_inputs = 1;
  590. if (dev->num_inputs >= MAX_INPUTS)
  591. dev->num_inputs = MAX_INPUTS;
  592. for (i = 0; i < dev->num_inputs; i++) {
  593. dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
  594. dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
  595. }
  596. dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
  597. /* how many outputs do we have and of what type? */
  598. dev->num_outputs = num_outputs[inst];
  599. if (dev->num_outputs < 1)
  600. dev->num_outputs = 1;
  601. if (dev->num_outputs >= MAX_OUTPUTS)
  602. dev->num_outputs = MAX_OUTPUTS;
  603. for (i = 0; i < dev->num_outputs; i++) {
  604. dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
  605. dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
  606. }
  607. dev->has_audio_outputs = out_type_counter[SVID];
  608. /* do we create a video capture device? */
  609. dev->has_vid_cap = node_type & 0x0001;
  610. /* do we create a vbi capture device? */
  611. if (in_type_counter[TV] || in_type_counter[SVID]) {
  612. dev->has_raw_vbi_cap = node_type & 0x0004;
  613. dev->has_sliced_vbi_cap = node_type & 0x0008;
  614. dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
  615. }
  616. /* do we create a video output device? */
  617. dev->has_vid_out = node_type & 0x0100;
  618. /* do we create a vbi output device? */
  619. if (out_type_counter[SVID]) {
  620. dev->has_raw_vbi_out = node_type & 0x0400;
  621. dev->has_sliced_vbi_out = node_type & 0x0800;
  622. dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
  623. }
  624. /* do we create a radio receiver device? */
  625. dev->has_radio_rx = node_type & 0x0010;
  626. /* do we create a radio transmitter device? */
  627. dev->has_radio_tx = node_type & 0x1000;
  628. /* do we create a software defined radio capture device? */
  629. dev->has_sdr_cap = node_type & 0x0020;
  630. /* do we have a tuner? */
  631. has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
  632. dev->has_radio_rx || dev->has_sdr_cap;
  633. /* do we have a modulator? */
  634. has_modulator = dev->has_radio_tx;
  635. if (dev->has_vid_cap)
  636. /* do we have a framebuffer for overlay testing? */
  637. dev->has_fb = node_type & 0x10000;
  638. /* can we do crop/compose/scaling while capturing? */
  639. if (no_error_inj && ccs_cap == -1)
  640. ccs_cap = 7;
  641. /* if ccs_cap == -1, then the use can select it using controls */
  642. if (ccs_cap != -1) {
  643. dev->has_crop_cap = ccs_cap & 1;
  644. dev->has_compose_cap = ccs_cap & 2;
  645. dev->has_scaler_cap = ccs_cap & 4;
  646. v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
  647. dev->has_crop_cap ? 'Y' : 'N',
  648. dev->has_compose_cap ? 'Y' : 'N',
  649. dev->has_scaler_cap ? 'Y' : 'N');
  650. }
  651. /* can we do crop/compose/scaling with video output? */
  652. if (no_error_inj && ccs_out == -1)
  653. ccs_out = 7;
  654. /* if ccs_out == -1, then the use can select it using controls */
  655. if (ccs_out != -1) {
  656. dev->has_crop_out = ccs_out & 1;
  657. dev->has_compose_out = ccs_out & 2;
  658. dev->has_scaler_out = ccs_out & 4;
  659. v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
  660. dev->has_crop_out ? 'Y' : 'N',
  661. dev->has_compose_out ? 'Y' : 'N',
  662. dev->has_scaler_out ? 'Y' : 'N');
  663. }
  664. /* end detecting feature set */
  665. if (dev->has_vid_cap) {
  666. /* set up the capabilities of the video capture device */
  667. dev->vid_cap_caps = dev->multiplanar ?
  668. V4L2_CAP_VIDEO_CAPTURE_MPLANE :
  669. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY;
  670. dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  671. if (dev->has_audio_inputs)
  672. dev->vid_cap_caps |= V4L2_CAP_AUDIO;
  673. if (in_type_counter[TV])
  674. dev->vid_cap_caps |= V4L2_CAP_TUNER;
  675. }
  676. if (dev->has_vid_out) {
  677. /* set up the capabilities of the video output device */
  678. dev->vid_out_caps = dev->multiplanar ?
  679. V4L2_CAP_VIDEO_OUTPUT_MPLANE :
  680. V4L2_CAP_VIDEO_OUTPUT;
  681. if (dev->has_fb)
  682. dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
  683. dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  684. if (dev->has_audio_outputs)
  685. dev->vid_out_caps |= V4L2_CAP_AUDIO;
  686. }
  687. if (dev->has_vbi_cap) {
  688. /* set up the capabilities of the vbi capture device */
  689. dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
  690. (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
  691. dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  692. if (dev->has_audio_inputs)
  693. dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
  694. if (in_type_counter[TV])
  695. dev->vbi_cap_caps |= V4L2_CAP_TUNER;
  696. }
  697. if (dev->has_vbi_out) {
  698. /* set up the capabilities of the vbi output device */
  699. dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
  700. (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
  701. dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  702. if (dev->has_audio_outputs)
  703. dev->vbi_out_caps |= V4L2_CAP_AUDIO;
  704. }
  705. if (dev->has_sdr_cap) {
  706. /* set up the capabilities of the sdr capture device */
  707. dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
  708. dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  709. }
  710. /* set up the capabilities of the radio receiver device */
  711. if (dev->has_radio_rx)
  712. dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
  713. V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
  714. V4L2_CAP_READWRITE;
  715. /* set up the capabilities of the radio transmitter device */
  716. if (dev->has_radio_tx)
  717. dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
  718. V4L2_CAP_READWRITE;
  719. /* initialize the test pattern generator */
  720. tpg_init(&dev->tpg, 640, 360);
  721. if (tpg_alloc(&dev->tpg, MAX_ZOOM * MAX_WIDTH))
  722. goto free_dev;
  723. dev->scaled_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
  724. if (!dev->scaled_line)
  725. goto free_dev;
  726. dev->blended_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
  727. if (!dev->blended_line)
  728. goto free_dev;
  729. /* load the edid */
  730. dev->edid = vmalloc(256 * 128);
  731. if (!dev->edid)
  732. goto free_dev;
  733. /* create a string array containing the names of all the preset timings */
  734. while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
  735. dev->query_dv_timings_size++;
  736. dev->query_dv_timings_qmenu = kmalloc(dev->query_dv_timings_size *
  737. (sizeof(void *) + 32), GFP_KERNEL);
  738. if (dev->query_dv_timings_qmenu == NULL)
  739. goto free_dev;
  740. for (i = 0; i < dev->query_dv_timings_size; i++) {
  741. const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
  742. char *p = (char *)&dev->query_dv_timings_qmenu[dev->query_dv_timings_size];
  743. u32 htot, vtot;
  744. p += i * 32;
  745. dev->query_dv_timings_qmenu[i] = p;
  746. htot = V4L2_DV_BT_FRAME_WIDTH(bt);
  747. vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
  748. snprintf(p, 32, "%ux%u%s%u",
  749. bt->width, bt->height, bt->interlaced ? "i" : "p",
  750. (u32)bt->pixelclock / (htot * vtot));
  751. }
  752. /* disable invalid ioctls based on the feature set */
  753. if (!dev->has_audio_inputs) {
  754. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
  755. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
  756. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
  757. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
  758. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
  759. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
  760. }
  761. if (!dev->has_audio_outputs) {
  762. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
  763. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
  764. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
  765. v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
  766. v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
  767. v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
  768. }
  769. if (!in_type_counter[TV] && !in_type_counter[SVID]) {
  770. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
  771. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
  772. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
  773. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
  774. }
  775. if (!out_type_counter[SVID]) {
  776. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
  777. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
  778. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
  779. }
  780. if (!has_tuner && !has_modulator) {
  781. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
  782. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
  783. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
  784. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
  785. }
  786. if (!has_tuner) {
  787. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
  788. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
  789. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
  790. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
  791. }
  792. if (in_type_counter[HDMI] == 0) {
  793. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
  794. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
  795. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
  796. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
  797. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
  798. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
  799. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
  800. }
  801. if (out_type_counter[HDMI] == 0) {
  802. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
  803. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
  804. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
  805. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
  806. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
  807. }
  808. if (!dev->has_fb) {
  809. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
  810. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
  811. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
  812. }
  813. v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
  814. v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
  815. v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
  816. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
  817. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
  818. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
  819. v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
  820. v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
  821. v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
  822. /* configure internal data */
  823. dev->fmt_cap = &vivid_formats[0];
  824. dev->fmt_out = &vivid_formats[0];
  825. if (!dev->multiplanar)
  826. vivid_formats[0].data_offset[0] = 0;
  827. dev->webcam_size_idx = 1;
  828. dev->webcam_ival_idx = 3;
  829. tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
  830. dev->std_cap = V4L2_STD_PAL;
  831. dev->std_out = V4L2_STD_PAL;
  832. if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
  833. tvnorms_cap = V4L2_STD_ALL;
  834. if (dev->output_type[0] == SVID)
  835. tvnorms_out = V4L2_STD_ALL;
  836. dev->dv_timings_cap = def_dv_timings;
  837. dev->dv_timings_out = def_dv_timings;
  838. dev->tv_freq = 2804 /* 175.25 * 16 */;
  839. dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
  840. dev->tv_field_cap = V4L2_FIELD_INTERLACED;
  841. dev->tv_field_out = V4L2_FIELD_INTERLACED;
  842. dev->radio_rx_freq = 95000 * 16;
  843. dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
  844. if (dev->has_radio_tx) {
  845. dev->radio_tx_freq = 95500 * 16;
  846. dev->radio_rds_loop = false;
  847. }
  848. dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
  849. dev->sdr_adc_freq = 300000;
  850. dev->sdr_fm_freq = 50000000;
  851. dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
  852. dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
  853. dev->edid_max_blocks = dev->edid_blocks = 2;
  854. memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
  855. ktime_get_ts(&dev->radio_rds_init_ts);
  856. /* create all controls */
  857. ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
  858. in_type_counter[TV] || in_type_counter[SVID] ||
  859. out_type_counter[SVID],
  860. in_type_counter[HDMI] || out_type_counter[HDMI]);
  861. if (ret)
  862. goto unreg_dev;
  863. /*
  864. * update the capture and output formats to do a proper initial
  865. * configuration.
  866. */
  867. vivid_update_format_cap(dev, false);
  868. vivid_update_format_out(dev);
  869. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
  870. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
  871. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
  872. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
  873. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
  874. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
  875. v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
  876. /* initialize overlay */
  877. dev->fb_cap.fmt.width = dev->src_rect.width;
  878. dev->fb_cap.fmt.height = dev->src_rect.height;
  879. dev->fb_cap.fmt.pixelformat = dev->fmt_cap->fourcc;
  880. dev->fb_cap.fmt.bytesperline = dev->src_rect.width * tpg_g_twopixelsize(&dev->tpg, 0) / 2;
  881. dev->fb_cap.fmt.sizeimage = dev->src_rect.height * dev->fb_cap.fmt.bytesperline;
  882. /* initialize locks */
  883. spin_lock_init(&dev->slock);
  884. mutex_init(&dev->mutex);
  885. /* init dma queues */
  886. INIT_LIST_HEAD(&dev->vid_cap_active);
  887. INIT_LIST_HEAD(&dev->vid_out_active);
  888. INIT_LIST_HEAD(&dev->vbi_cap_active);
  889. INIT_LIST_HEAD(&dev->vbi_out_active);
  890. INIT_LIST_HEAD(&dev->sdr_cap_active);
  891. /* start creating the vb2 queues */
  892. if (dev->has_vid_cap) {
  893. /* initialize vid_cap queue */
  894. q = &dev->vb_vid_cap_q;
  895. q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  896. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  897. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  898. q->drv_priv = dev;
  899. q->buf_struct_size = sizeof(struct vivid_buffer);
  900. q->ops = &vivid_vid_cap_qops;
  901. q->mem_ops = &vb2_vmalloc_memops;
  902. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  903. q->min_buffers_needed = 2;
  904. q->lock = &dev->mutex;
  905. ret = vb2_queue_init(q);
  906. if (ret)
  907. goto unreg_dev;
  908. }
  909. if (dev->has_vid_out) {
  910. /* initialize vid_out queue */
  911. q = &dev->vb_vid_out_q;
  912. q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  913. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  914. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
  915. q->drv_priv = dev;
  916. q->buf_struct_size = sizeof(struct vivid_buffer);
  917. q->ops = &vivid_vid_out_qops;
  918. q->mem_ops = &vb2_vmalloc_memops;
  919. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  920. q->min_buffers_needed = 2;
  921. q->lock = &dev->mutex;
  922. ret = vb2_queue_init(q);
  923. if (ret)
  924. goto unreg_dev;
  925. }
  926. if (dev->has_vbi_cap) {
  927. /* initialize vbi_cap queue */
  928. q = &dev->vb_vbi_cap_q;
  929. q->type = dev->has_raw_vbi_cap ? V4L2_BUF_TYPE_VBI_CAPTURE :
  930. V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
  931. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  932. q->drv_priv = dev;
  933. q->buf_struct_size = sizeof(struct vivid_buffer);
  934. q->ops = &vivid_vbi_cap_qops;
  935. q->mem_ops = &vb2_vmalloc_memops;
  936. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  937. q->min_buffers_needed = 2;
  938. q->lock = &dev->mutex;
  939. ret = vb2_queue_init(q);
  940. if (ret)
  941. goto unreg_dev;
  942. }
  943. if (dev->has_vbi_out) {
  944. /* initialize vbi_out queue */
  945. q = &dev->vb_vbi_out_q;
  946. q->type = dev->has_raw_vbi_out ? V4L2_BUF_TYPE_VBI_OUTPUT :
  947. V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
  948. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
  949. q->drv_priv = dev;
  950. q->buf_struct_size = sizeof(struct vivid_buffer);
  951. q->ops = &vivid_vbi_out_qops;
  952. q->mem_ops = &vb2_vmalloc_memops;
  953. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  954. q->min_buffers_needed = 2;
  955. q->lock = &dev->mutex;
  956. ret = vb2_queue_init(q);
  957. if (ret)
  958. goto unreg_dev;
  959. }
  960. if (dev->has_sdr_cap) {
  961. /* initialize sdr_cap queue */
  962. q = &dev->vb_sdr_cap_q;
  963. q->type = V4L2_BUF_TYPE_SDR_CAPTURE;
  964. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  965. q->drv_priv = dev;
  966. q->buf_struct_size = sizeof(struct vivid_buffer);
  967. q->ops = &vivid_sdr_cap_qops;
  968. q->mem_ops = &vb2_vmalloc_memops;
  969. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  970. q->min_buffers_needed = 8;
  971. q->lock = &dev->mutex;
  972. ret = vb2_queue_init(q);
  973. if (ret)
  974. goto unreg_dev;
  975. }
  976. if (dev->has_fb) {
  977. /* Create framebuffer for testing capture/output overlay */
  978. ret = vivid_fb_init(dev);
  979. if (ret)
  980. goto unreg_dev;
  981. v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
  982. dev->fb_info.node);
  983. }
  984. /* finally start creating the device nodes */
  985. if (dev->has_vid_cap) {
  986. vfd = &dev->vid_cap_dev;
  987. strlcpy(vfd->name, "vivid-vid-cap", sizeof(vfd->name));
  988. vfd->fops = &vivid_fops;
  989. vfd->ioctl_ops = &vivid_ioctl_ops;
  990. vfd->release = video_device_release_empty;
  991. vfd->v4l2_dev = &dev->v4l2_dev;
  992. vfd->queue = &dev->vb_vid_cap_q;
  993. vfd->tvnorms = tvnorms_cap;
  994. /*
  995. * Provide a mutex to v4l2 core. It will be used to protect
  996. * all fops and v4l2 ioctls.
  997. */
  998. vfd->lock = &dev->mutex;
  999. video_set_drvdata(vfd, dev);
  1000. ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
  1001. if (ret < 0)
  1002. goto unreg_dev;
  1003. v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
  1004. video_device_node_name(vfd));
  1005. }
  1006. if (dev->has_vid_out) {
  1007. vfd = &dev->vid_out_dev;
  1008. strlcpy(vfd->name, "vivid-vid-out", sizeof(vfd->name));
  1009. vfd->vfl_dir = VFL_DIR_TX;
  1010. vfd->fops = &vivid_fops;
  1011. vfd->ioctl_ops = &vivid_ioctl_ops;
  1012. vfd->release = video_device_release_empty;
  1013. vfd->v4l2_dev = &dev->v4l2_dev;
  1014. vfd->queue = &dev->vb_vid_out_q;
  1015. vfd->tvnorms = tvnorms_out;
  1016. /*
  1017. * Provide a mutex to v4l2 core. It will be used to protect
  1018. * all fops and v4l2 ioctls.
  1019. */
  1020. vfd->lock = &dev->mutex;
  1021. video_set_drvdata(vfd, dev);
  1022. ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
  1023. if (ret < 0)
  1024. goto unreg_dev;
  1025. v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
  1026. video_device_node_name(vfd));
  1027. }
  1028. if (dev->has_vbi_cap) {
  1029. vfd = &dev->vbi_cap_dev;
  1030. strlcpy(vfd->name, "vivid-vbi-cap", sizeof(vfd->name));
  1031. vfd->fops = &vivid_fops;
  1032. vfd->ioctl_ops = &vivid_ioctl_ops;
  1033. vfd->release = video_device_release_empty;
  1034. vfd->v4l2_dev = &dev->v4l2_dev;
  1035. vfd->queue = &dev->vb_vbi_cap_q;
  1036. vfd->lock = &dev->mutex;
  1037. vfd->tvnorms = tvnorms_cap;
  1038. video_set_drvdata(vfd, dev);
  1039. ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
  1040. if (ret < 0)
  1041. goto unreg_dev;
  1042. v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
  1043. video_device_node_name(vfd),
  1044. (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
  1045. "raw and sliced" :
  1046. (dev->has_raw_vbi_cap ? "raw" : "sliced"));
  1047. }
  1048. if (dev->has_vbi_out) {
  1049. vfd = &dev->vbi_out_dev;
  1050. strlcpy(vfd->name, "vivid-vbi-out", sizeof(vfd->name));
  1051. vfd->vfl_dir = VFL_DIR_TX;
  1052. vfd->fops = &vivid_fops;
  1053. vfd->ioctl_ops = &vivid_ioctl_ops;
  1054. vfd->release = video_device_release_empty;
  1055. vfd->v4l2_dev = &dev->v4l2_dev;
  1056. vfd->queue = &dev->vb_vbi_out_q;
  1057. vfd->lock = &dev->mutex;
  1058. vfd->tvnorms = tvnorms_out;
  1059. video_set_drvdata(vfd, dev);
  1060. ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
  1061. if (ret < 0)
  1062. goto unreg_dev;
  1063. v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
  1064. video_device_node_name(vfd),
  1065. (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
  1066. "raw and sliced" :
  1067. (dev->has_raw_vbi_out ? "raw" : "sliced"));
  1068. }
  1069. if (dev->has_sdr_cap) {
  1070. vfd = &dev->sdr_cap_dev;
  1071. strlcpy(vfd->name, "vivid-sdr-cap", sizeof(vfd->name));
  1072. vfd->fops = &vivid_fops;
  1073. vfd->ioctl_ops = &vivid_ioctl_ops;
  1074. vfd->release = video_device_release_empty;
  1075. vfd->v4l2_dev = &dev->v4l2_dev;
  1076. vfd->queue = &dev->vb_sdr_cap_q;
  1077. vfd->lock = &dev->mutex;
  1078. video_set_drvdata(vfd, dev);
  1079. ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
  1080. if (ret < 0)
  1081. goto unreg_dev;
  1082. v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
  1083. video_device_node_name(vfd));
  1084. }
  1085. if (dev->has_radio_rx) {
  1086. vfd = &dev->radio_rx_dev;
  1087. strlcpy(vfd->name, "vivid-rad-rx", sizeof(vfd->name));
  1088. vfd->fops = &vivid_radio_fops;
  1089. vfd->ioctl_ops = &vivid_ioctl_ops;
  1090. vfd->release = video_device_release_empty;
  1091. vfd->v4l2_dev = &dev->v4l2_dev;
  1092. vfd->lock = &dev->mutex;
  1093. video_set_drvdata(vfd, dev);
  1094. ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
  1095. if (ret < 0)
  1096. goto unreg_dev;
  1097. v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
  1098. video_device_node_name(vfd));
  1099. }
  1100. if (dev->has_radio_tx) {
  1101. vfd = &dev->radio_tx_dev;
  1102. strlcpy(vfd->name, "vivid-rad-tx", sizeof(vfd->name));
  1103. vfd->vfl_dir = VFL_DIR_TX;
  1104. vfd->fops = &vivid_radio_fops;
  1105. vfd->ioctl_ops = &vivid_ioctl_ops;
  1106. vfd->release = video_device_release_empty;
  1107. vfd->v4l2_dev = &dev->v4l2_dev;
  1108. vfd->lock = &dev->mutex;
  1109. video_set_drvdata(vfd, dev);
  1110. ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
  1111. if (ret < 0)
  1112. goto unreg_dev;
  1113. v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
  1114. video_device_node_name(vfd));
  1115. }
  1116. /* Now that everything is fine, let's add it to device list */
  1117. vivid_devs[inst] = dev;
  1118. return 0;
  1119. unreg_dev:
  1120. video_unregister_device(&dev->radio_tx_dev);
  1121. video_unregister_device(&dev->radio_rx_dev);
  1122. video_unregister_device(&dev->sdr_cap_dev);
  1123. video_unregister_device(&dev->vbi_out_dev);
  1124. video_unregister_device(&dev->vbi_cap_dev);
  1125. video_unregister_device(&dev->vid_out_dev);
  1126. video_unregister_device(&dev->vid_cap_dev);
  1127. free_dev:
  1128. v4l2_device_put(&dev->v4l2_dev);
  1129. return ret;
  1130. }
  1131. /* This routine allocates from 1 to n_devs virtual drivers.
  1132. The real maximum number of virtual drivers will depend on how many drivers
  1133. will succeed. This is limited to the maximum number of devices that
  1134. videodev supports, which is equal to VIDEO_NUM_DEVICES.
  1135. */
  1136. static int vivid_probe(struct platform_device *pdev)
  1137. {
  1138. const struct font_desc *font = find_font("VGA8x16");
  1139. int ret = 0, i;
  1140. if (font == NULL) {
  1141. pr_err("vivid: could not find font\n");
  1142. return -ENODEV;
  1143. }
  1144. tpg_set_font(font->data);
  1145. n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
  1146. for (i = 0; i < n_devs; i++) {
  1147. ret = vivid_create_instance(pdev, i);
  1148. if (ret) {
  1149. /* If some instantiations succeeded, keep driver */
  1150. if (i)
  1151. ret = 0;
  1152. break;
  1153. }
  1154. }
  1155. if (ret < 0) {
  1156. pr_err("vivid: error %d while loading driver\n", ret);
  1157. return ret;
  1158. }
  1159. /* n_devs will reflect the actual number of allocated devices */
  1160. n_devs = i;
  1161. return ret;
  1162. }
  1163. static int vivid_remove(struct platform_device *pdev)
  1164. {
  1165. struct vivid_dev *dev;
  1166. unsigned i;
  1167. for (i = 0; i < n_devs; i++) {
  1168. dev = vivid_devs[i];
  1169. if (!dev)
  1170. continue;
  1171. if (dev->has_vid_cap) {
  1172. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1173. video_device_node_name(&dev->vid_cap_dev));
  1174. video_unregister_device(&dev->vid_cap_dev);
  1175. }
  1176. if (dev->has_vid_out) {
  1177. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1178. video_device_node_name(&dev->vid_out_dev));
  1179. video_unregister_device(&dev->vid_out_dev);
  1180. }
  1181. if (dev->has_vbi_cap) {
  1182. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1183. video_device_node_name(&dev->vbi_cap_dev));
  1184. video_unregister_device(&dev->vbi_cap_dev);
  1185. }
  1186. if (dev->has_vbi_out) {
  1187. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1188. video_device_node_name(&dev->vbi_out_dev));
  1189. video_unregister_device(&dev->vbi_out_dev);
  1190. }
  1191. if (dev->has_sdr_cap) {
  1192. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1193. video_device_node_name(&dev->sdr_cap_dev));
  1194. video_unregister_device(&dev->sdr_cap_dev);
  1195. }
  1196. if (dev->has_radio_rx) {
  1197. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1198. video_device_node_name(&dev->radio_rx_dev));
  1199. video_unregister_device(&dev->radio_rx_dev);
  1200. }
  1201. if (dev->has_radio_tx) {
  1202. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1203. video_device_node_name(&dev->radio_tx_dev));
  1204. video_unregister_device(&dev->radio_tx_dev);
  1205. }
  1206. if (dev->has_fb) {
  1207. v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
  1208. dev->fb_info.node);
  1209. unregister_framebuffer(&dev->fb_info);
  1210. vivid_fb_release_buffers(dev);
  1211. }
  1212. v4l2_device_put(&dev->v4l2_dev);
  1213. vivid_devs[i] = NULL;
  1214. }
  1215. return 0;
  1216. }
  1217. static void vivid_pdev_release(struct device *dev)
  1218. {
  1219. }
  1220. static struct platform_device vivid_pdev = {
  1221. .name = "vivid",
  1222. .dev.release = vivid_pdev_release,
  1223. };
  1224. static struct platform_driver vivid_pdrv = {
  1225. .probe = vivid_probe,
  1226. .remove = vivid_remove,
  1227. .driver = {
  1228. .name = "vivid",
  1229. },
  1230. };
  1231. static int __init vivid_init(void)
  1232. {
  1233. int ret;
  1234. ret = platform_device_register(&vivid_pdev);
  1235. if (ret)
  1236. return ret;
  1237. ret = platform_driver_register(&vivid_pdrv);
  1238. if (ret)
  1239. platform_device_unregister(&vivid_pdev);
  1240. return ret;
  1241. }
  1242. static void __exit vivid_exit(void)
  1243. {
  1244. platform_driver_unregister(&vivid_pdrv);
  1245. platform_device_unregister(&vivid_pdev);
  1246. }
  1247. module_init(vivid_init);
  1248. module_exit(vivid_exit);