pvrusb2-v4l2.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/version.h>
  24. #include "pvrusb2-context.h"
  25. #include "pvrusb2-hdw.h"
  26. #include "pvrusb2.h"
  27. #include "pvrusb2-debug.h"
  28. #include "pvrusb2-v4l2.h"
  29. #include "pvrusb2-ioread.h"
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/v4l2-common.h>
  33. struct pvr2_v4l2_dev;
  34. struct pvr2_v4l2_fh;
  35. struct pvr2_v4l2;
  36. struct pvr2_v4l2_dev {
  37. struct video_device devbase; /* MUST be first! */
  38. struct pvr2_v4l2 *v4lp;
  39. struct pvr2_context_stream *stream;
  40. /* Information about this device: */
  41. enum pvr2_config config; /* Expected stream format */
  42. int v4l_type; /* V4L defined type for this device node */
  43. enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
  44. };
  45. struct pvr2_v4l2_fh {
  46. struct pvr2_channel channel;
  47. struct pvr2_v4l2_dev *dev_info;
  48. enum v4l2_priority prio;
  49. struct pvr2_ioread *rhp;
  50. struct file *file;
  51. struct pvr2_v4l2 *vhead;
  52. struct pvr2_v4l2_fh *vnext;
  53. struct pvr2_v4l2_fh *vprev;
  54. wait_queue_head_t wait_data;
  55. int fw_mode_flag;
  56. };
  57. struct pvr2_v4l2 {
  58. struct pvr2_channel channel;
  59. struct pvr2_v4l2_fh *vfirst;
  60. struct pvr2_v4l2_fh *vlast;
  61. struct v4l2_prio_state prio;
  62. /* streams - Note that these must be separately, individually,
  63. * allocated pointers. This is because the v4l core is going to
  64. * manage their deletion - separately, individually... */
  65. struct pvr2_v4l2_dev *dev_video;
  66. struct pvr2_v4l2_dev *dev_radio;
  67. };
  68. static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  69. module_param_array(video_nr, int, NULL, 0444);
  70. MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
  71. static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  72. module_param_array(radio_nr, int, NULL, 0444);
  73. MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
  74. static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  75. module_param_array(vbi_nr, int, NULL, 0444);
  76. MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
  77. static struct v4l2_capability pvr_capability ={
  78. .driver = "pvrusb2",
  79. .card = "Hauppauge WinTV pvr-usb2",
  80. .bus_info = "usb",
  81. .version = KERNEL_VERSION(0,8,0),
  82. .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
  83. V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
  84. V4L2_CAP_READWRITE),
  85. .reserved = {0,0,0,0}
  86. };
  87. static struct v4l2_tuner pvr_v4l2_tuners[]= {
  88. {
  89. .index = 0,
  90. .name = "TV Tuner",
  91. .type = V4L2_TUNER_ANALOG_TV,
  92. .capability = (V4L2_TUNER_CAP_NORM |
  93. V4L2_TUNER_CAP_STEREO |
  94. V4L2_TUNER_CAP_LANG1 |
  95. V4L2_TUNER_CAP_LANG2),
  96. .rangelow = 0,
  97. .rangehigh = 0,
  98. .rxsubchans = V4L2_TUNER_SUB_STEREO,
  99. .audmode = V4L2_TUNER_MODE_STEREO,
  100. .signal = 0,
  101. .afc = 0,
  102. .reserved = {0,0,0,0}
  103. }
  104. };
  105. static struct v4l2_fmtdesc pvr_fmtdesc [] = {
  106. {
  107. .index = 0,
  108. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  109. .flags = V4L2_FMT_FLAG_COMPRESSED,
  110. .description = "MPEG1/2",
  111. // This should really be V4L2_PIX_FMT_MPEG, but xawtv
  112. // breaks when I do that.
  113. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  114. .reserved = { 0, 0, 0, 0 }
  115. }
  116. };
  117. #define PVR_FORMAT_PIX 0
  118. #define PVR_FORMAT_VBI 1
  119. static struct v4l2_format pvr_format [] = {
  120. [PVR_FORMAT_PIX] = {
  121. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  122. .fmt = {
  123. .pix = {
  124. .width = 720,
  125. .height = 576,
  126. // This should really be V4L2_PIX_FMT_MPEG,
  127. // but xawtv breaks when I do that.
  128. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  129. .field = V4L2_FIELD_INTERLACED,
  130. .bytesperline = 0, // doesn't make sense
  131. // here
  132. //FIXME : Don't know what to put here...
  133. .sizeimage = (32*1024),
  134. .colorspace = 0, // doesn't make sense here
  135. .priv = 0
  136. }
  137. }
  138. },
  139. [PVR_FORMAT_VBI] = {
  140. .type = V4L2_BUF_TYPE_VBI_CAPTURE,
  141. .fmt = {
  142. .vbi = {
  143. .sampling_rate = 27000000,
  144. .offset = 248,
  145. .samples_per_line = 1443,
  146. .sample_format = V4L2_PIX_FMT_GREY,
  147. .start = { 0, 0 },
  148. .count = { 0, 0 },
  149. .flags = 0,
  150. .reserved = { 0, 0 }
  151. }
  152. }
  153. }
  154. };
  155. static const char *get_v4l_name(int v4l_type)
  156. {
  157. switch (v4l_type) {
  158. case VFL_TYPE_GRABBER: return "video";
  159. case VFL_TYPE_RADIO: return "radio";
  160. case VFL_TYPE_VBI: return "vbi";
  161. default: return "?";
  162. }
  163. }
  164. /*
  165. * pvr_ioctl()
  166. *
  167. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  168. *
  169. */
  170. static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
  171. unsigned int cmd, void *arg)
  172. {
  173. struct pvr2_v4l2_fh *fh = file->private_data;
  174. struct pvr2_v4l2 *vp = fh->vhead;
  175. struct pvr2_v4l2_dev *dev_info = fh->dev_info;
  176. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  177. int ret = -EINVAL;
  178. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  179. v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
  180. }
  181. if (!pvr2_hdw_dev_ok(hdw)) {
  182. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  183. "ioctl failed - bad or no context");
  184. return -EFAULT;
  185. }
  186. /* check priority */
  187. switch (cmd) {
  188. case VIDIOC_S_CTRL:
  189. case VIDIOC_S_STD:
  190. case VIDIOC_S_INPUT:
  191. case VIDIOC_S_TUNER:
  192. case VIDIOC_S_FREQUENCY:
  193. ret = v4l2_prio_check(&vp->prio, &fh->prio);
  194. if (ret)
  195. return ret;
  196. }
  197. switch (cmd) {
  198. case VIDIOC_QUERYCAP:
  199. {
  200. struct v4l2_capability *cap = arg;
  201. memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
  202. ret = 0;
  203. break;
  204. }
  205. case VIDIOC_G_PRIORITY:
  206. {
  207. enum v4l2_priority *p = arg;
  208. *p = v4l2_prio_max(&vp->prio);
  209. ret = 0;
  210. break;
  211. }
  212. case VIDIOC_S_PRIORITY:
  213. {
  214. enum v4l2_priority *prio = arg;
  215. ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
  216. break;
  217. }
  218. case VIDIOC_ENUMSTD:
  219. {
  220. struct v4l2_standard *vs = (struct v4l2_standard *)arg;
  221. int idx = vs->index;
  222. ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
  223. break;
  224. }
  225. case VIDIOC_G_STD:
  226. {
  227. int val = 0;
  228. ret = pvr2_ctrl_get_value(
  229. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
  230. *(v4l2_std_id *)arg = val;
  231. break;
  232. }
  233. case VIDIOC_S_STD:
  234. {
  235. ret = pvr2_ctrl_set_value(
  236. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
  237. *(v4l2_std_id *)arg);
  238. break;
  239. }
  240. case VIDIOC_ENUMINPUT:
  241. {
  242. struct pvr2_ctrl *cptr;
  243. struct v4l2_input *vi = (struct v4l2_input *)arg;
  244. struct v4l2_input tmp;
  245. unsigned int cnt;
  246. cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  247. memset(&tmp,0,sizeof(tmp));
  248. tmp.index = vi->index;
  249. ret = 0;
  250. switch (vi->index) {
  251. case PVR2_CVAL_INPUT_TV:
  252. case PVR2_CVAL_INPUT_RADIO:
  253. tmp.type = V4L2_INPUT_TYPE_TUNER;
  254. break;
  255. case PVR2_CVAL_INPUT_SVIDEO:
  256. case PVR2_CVAL_INPUT_COMPOSITE:
  257. tmp.type = V4L2_INPUT_TYPE_CAMERA;
  258. break;
  259. default:
  260. ret = -EINVAL;
  261. break;
  262. }
  263. if (ret < 0) break;
  264. cnt = 0;
  265. pvr2_ctrl_get_valname(cptr,vi->index,
  266. tmp.name,sizeof(tmp.name)-1,&cnt);
  267. tmp.name[cnt] = 0;
  268. /* Don't bother with audioset, since this driver currently
  269. always switches the audio whenever the video is
  270. switched. */
  271. /* Handling std is a tougher problem. It doesn't make
  272. sense in cases where a device might be multi-standard.
  273. We could just copy out the current value for the
  274. standard, but it can change over time. For now just
  275. leave it zero. */
  276. memcpy(vi, &tmp, sizeof(tmp));
  277. ret = 0;
  278. break;
  279. }
  280. case VIDIOC_G_INPUT:
  281. {
  282. struct pvr2_ctrl *cptr;
  283. struct v4l2_input *vi = (struct v4l2_input *)arg;
  284. int val;
  285. cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  286. val = 0;
  287. ret = pvr2_ctrl_get_value(cptr,&val);
  288. vi->index = val;
  289. break;
  290. }
  291. case VIDIOC_S_INPUT:
  292. {
  293. struct v4l2_input *vi = (struct v4l2_input *)arg;
  294. ret = pvr2_ctrl_set_value(
  295. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
  296. vi->index);
  297. break;
  298. }
  299. case VIDIOC_ENUMAUDIO:
  300. {
  301. ret = -EINVAL;
  302. break;
  303. }
  304. case VIDIOC_G_AUDIO:
  305. {
  306. ret = -EINVAL;
  307. break;
  308. }
  309. case VIDIOC_S_AUDIO:
  310. {
  311. ret = -EINVAL;
  312. break;
  313. }
  314. case VIDIOC_G_TUNER:
  315. {
  316. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  317. unsigned int status_mask;
  318. int val;
  319. if (vt->index !=0) break;
  320. status_mask = pvr2_hdw_get_signal_status(hdw);
  321. memcpy(vt, &pvr_v4l2_tuners[vt->index],
  322. sizeof(struct v4l2_tuner));
  323. vt->signal = 0;
  324. if (status_mask & PVR2_SIGNAL_OK) {
  325. if (status_mask & PVR2_SIGNAL_STEREO) {
  326. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  327. } else {
  328. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  329. }
  330. if (status_mask & PVR2_SIGNAL_SAP) {
  331. vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
  332. V4L2_TUNER_SUB_LANG2);
  333. }
  334. vt->signal = 65535;
  335. }
  336. val = 0;
  337. ret = pvr2_ctrl_get_value(
  338. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
  339. &val);
  340. vt->audmode = val;
  341. break;
  342. }
  343. case VIDIOC_S_TUNER:
  344. {
  345. struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
  346. if (vt->index != 0)
  347. break;
  348. ret = pvr2_ctrl_set_value(
  349. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
  350. vt->audmode);
  351. }
  352. case VIDIOC_S_FREQUENCY:
  353. {
  354. const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
  355. unsigned long fv;
  356. fv = vf->frequency;
  357. if (vf->type == V4L2_TUNER_RADIO) {
  358. fv = (fv * 125) / 2;
  359. } else {
  360. fv = fv * 62500;
  361. }
  362. ret = pvr2_ctrl_set_value(
  363. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
  364. break;
  365. }
  366. case VIDIOC_G_FREQUENCY:
  367. {
  368. struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
  369. int val = 0;
  370. int cur_input = PVR2_CVAL_INPUT_TV;
  371. ret = pvr2_ctrl_get_value(
  372. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
  373. &val);
  374. if (ret != 0) break;
  375. pvr2_ctrl_get_value(
  376. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
  377. &cur_input);
  378. if (cur_input == PVR2_CVAL_INPUT_RADIO) {
  379. val = (val * 2) / 125;
  380. vf->frequency = val;
  381. vf->type = V4L2_TUNER_RADIO;
  382. } else {
  383. val /= 62500;
  384. vf->frequency = val;
  385. vf->type = V4L2_TUNER_ANALOG_TV;
  386. }
  387. break;
  388. }
  389. case VIDIOC_ENUM_FMT:
  390. {
  391. struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
  392. /* Only one format is supported : mpeg.*/
  393. if (fd->index != 0)
  394. break;
  395. memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
  396. ret = 0;
  397. break;
  398. }
  399. case VIDIOC_G_FMT:
  400. {
  401. struct v4l2_format *vf = (struct v4l2_format *)arg;
  402. int val;
  403. switch(vf->type) {
  404. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  405. memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
  406. sizeof(struct v4l2_format));
  407. val = 0;
  408. pvr2_ctrl_get_value(
  409. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
  410. &val);
  411. vf->fmt.pix.width = val;
  412. val = 0;
  413. pvr2_ctrl_get_value(
  414. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
  415. &val);
  416. vf->fmt.pix.height = val;
  417. ret = 0;
  418. break;
  419. case V4L2_BUF_TYPE_VBI_CAPTURE:
  420. // ????? Still need to figure out to do VBI correctly
  421. ret = -EINVAL;
  422. break;
  423. default:
  424. ret = -EINVAL;
  425. break;
  426. }
  427. break;
  428. }
  429. case VIDIOC_TRY_FMT:
  430. case VIDIOC_S_FMT:
  431. {
  432. struct v4l2_format *vf = (struct v4l2_format *)arg;
  433. ret = 0;
  434. switch(vf->type) {
  435. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  436. int lmin,lmax;
  437. struct pvr2_ctrl *hcp,*vcp;
  438. int h = vf->fmt.pix.height;
  439. int w = vf->fmt.pix.width;
  440. hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
  441. vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
  442. lmin = pvr2_ctrl_get_min(hcp);
  443. lmax = pvr2_ctrl_get_max(hcp);
  444. if (w < lmin) {
  445. w = lmin;
  446. } else if (w > lmax) {
  447. w = lmax;
  448. }
  449. lmin = pvr2_ctrl_get_min(vcp);
  450. lmax = pvr2_ctrl_get_max(vcp);
  451. if (h < lmin) {
  452. h = lmin;
  453. } else if (h > lmax) {
  454. h = lmax;
  455. }
  456. memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
  457. sizeof(struct v4l2_format));
  458. vf->fmt.pix.width = w;
  459. vf->fmt.pix.height = h;
  460. if (cmd == VIDIOC_S_FMT) {
  461. pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
  462. pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
  463. }
  464. } break;
  465. case V4L2_BUF_TYPE_VBI_CAPTURE:
  466. // ????? Still need to figure out to do VBI correctly
  467. ret = -EINVAL;
  468. break;
  469. default:
  470. ret = -EINVAL;
  471. break;
  472. }
  473. break;
  474. }
  475. case VIDIOC_STREAMON:
  476. {
  477. if (!fh->dev_info->stream) {
  478. /* No stream defined for this node. This means
  479. that we're not currently allowed to stream from
  480. this node. */
  481. ret = -EPERM;
  482. break;
  483. }
  484. ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
  485. if (ret < 0) return ret;
  486. ret = pvr2_hdw_set_streaming(hdw,!0);
  487. break;
  488. }
  489. case VIDIOC_STREAMOFF:
  490. {
  491. if (!fh->dev_info->stream) {
  492. /* No stream defined for this node. This means
  493. that we're not currently allowed to stream from
  494. this node. */
  495. ret = -EPERM;
  496. break;
  497. }
  498. ret = pvr2_hdw_set_streaming(hdw,0);
  499. break;
  500. }
  501. case VIDIOC_QUERYCTRL:
  502. {
  503. struct pvr2_ctrl *cptr;
  504. struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
  505. ret = 0;
  506. if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  507. cptr = pvr2_hdw_get_ctrl_nextv4l(
  508. hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
  509. if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
  510. } else {
  511. cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
  512. }
  513. if (!cptr) {
  514. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  515. "QUERYCTRL id=0x%x not implemented here",
  516. vc->id);
  517. ret = -EINVAL;
  518. break;
  519. }
  520. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  521. "QUERYCTRL id=0x%x mapping name=%s (%s)",
  522. vc->id,pvr2_ctrl_get_name(cptr),
  523. pvr2_ctrl_get_desc(cptr));
  524. strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
  525. vc->flags = pvr2_ctrl_get_v4lflags(cptr);
  526. vc->default_value = pvr2_ctrl_get_def(cptr);
  527. switch (pvr2_ctrl_get_type(cptr)) {
  528. case pvr2_ctl_enum:
  529. vc->type = V4L2_CTRL_TYPE_MENU;
  530. vc->minimum = 0;
  531. vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
  532. vc->step = 1;
  533. break;
  534. case pvr2_ctl_bool:
  535. vc->type = V4L2_CTRL_TYPE_BOOLEAN;
  536. vc->minimum = 0;
  537. vc->maximum = 1;
  538. vc->step = 1;
  539. break;
  540. case pvr2_ctl_int:
  541. vc->type = V4L2_CTRL_TYPE_INTEGER;
  542. vc->minimum = pvr2_ctrl_get_min(cptr);
  543. vc->maximum = pvr2_ctrl_get_max(cptr);
  544. vc->step = 1;
  545. break;
  546. default:
  547. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  548. "QUERYCTRL id=0x%x name=%s not mappable",
  549. vc->id,pvr2_ctrl_get_name(cptr));
  550. ret = -EINVAL;
  551. break;
  552. }
  553. break;
  554. }
  555. case VIDIOC_QUERYMENU:
  556. {
  557. struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
  558. unsigned int cnt = 0;
  559. ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
  560. vm->index,
  561. vm->name,sizeof(vm->name)-1,
  562. &cnt);
  563. vm->name[cnt] = 0;
  564. break;
  565. }
  566. case VIDIOC_G_CTRL:
  567. {
  568. struct v4l2_control *vc = (struct v4l2_control *)arg;
  569. int val = 0;
  570. ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
  571. &val);
  572. vc->value = val;
  573. break;
  574. }
  575. case VIDIOC_S_CTRL:
  576. {
  577. struct v4l2_control *vc = (struct v4l2_control *)arg;
  578. ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
  579. vc->value);
  580. break;
  581. }
  582. case VIDIOC_G_EXT_CTRLS:
  583. {
  584. struct v4l2_ext_controls *ctls =
  585. (struct v4l2_ext_controls *)arg;
  586. struct v4l2_ext_control *ctrl;
  587. unsigned int idx;
  588. int val;
  589. for (idx = 0; idx < ctls->count; idx++) {
  590. ctrl = ctls->controls + idx;
  591. ret = pvr2_ctrl_get_value(
  592. pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
  593. if (ret) {
  594. ctls->error_idx = idx;
  595. break;
  596. }
  597. /* Ensure that if read as a 64 bit value, the user
  598. will still get a hopefully sane value */
  599. ctrl->value64 = 0;
  600. ctrl->value = val;
  601. }
  602. break;
  603. }
  604. case VIDIOC_S_EXT_CTRLS:
  605. {
  606. struct v4l2_ext_controls *ctls =
  607. (struct v4l2_ext_controls *)arg;
  608. struct v4l2_ext_control *ctrl;
  609. unsigned int idx;
  610. for (idx = 0; idx < ctls->count; idx++) {
  611. ctrl = ctls->controls + idx;
  612. ret = pvr2_ctrl_set_value(
  613. pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
  614. ctrl->value);
  615. if (ret) {
  616. ctls->error_idx = idx;
  617. break;
  618. }
  619. }
  620. break;
  621. }
  622. case VIDIOC_TRY_EXT_CTRLS:
  623. {
  624. struct v4l2_ext_controls *ctls =
  625. (struct v4l2_ext_controls *)arg;
  626. struct v4l2_ext_control *ctrl;
  627. struct pvr2_ctrl *pctl;
  628. unsigned int idx;
  629. /* For the moment just validate that the requested control
  630. actually exists. */
  631. for (idx = 0; idx < ctls->count; idx++) {
  632. ctrl = ctls->controls + idx;
  633. pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
  634. if (!pctl) {
  635. ret = -EINVAL;
  636. ctls->error_idx = idx;
  637. break;
  638. }
  639. }
  640. break;
  641. }
  642. case VIDIOC_LOG_STATUS:
  643. {
  644. pvr2_hdw_trigger_module_log(hdw);
  645. ret = 0;
  646. break;
  647. }
  648. #ifdef CONFIG_VIDEO_ADV_DEBUG
  649. case VIDIOC_INT_G_REGISTER:
  650. case VIDIOC_INT_S_REGISTER:
  651. {
  652. u32 val;
  653. struct v4l2_register *req = (struct v4l2_register *)arg;
  654. if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
  655. ret = pvr2_hdw_register_access(
  656. hdw,req->i2c_id,req->reg,
  657. cmd == VIDIOC_INT_S_REGISTER,&val);
  658. if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
  659. break;
  660. }
  661. #endif
  662. default :
  663. ret = v4l_compat_translate_ioctl(inode,file,cmd,
  664. arg,pvr2_v4l2_do_ioctl);
  665. }
  666. pvr2_hdw_commit_ctl(hdw);
  667. if (ret < 0) {
  668. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  669. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  670. "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
  671. } else {
  672. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  673. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  674. "pvr2_v4l2_do_ioctl failure, ret=%d"
  675. " command was:",ret);
  676. v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
  677. cmd);
  678. }
  679. }
  680. } else {
  681. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  682. "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
  683. ret,ret);
  684. }
  685. return ret;
  686. }
  687. static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
  688. {
  689. int minor_id = dip->devbase.minor;
  690. struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
  691. enum pvr2_config cfg = dip->config;
  692. int v4l_type = dip->v4l_type;
  693. pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
  694. /* Paranoia */
  695. dip->v4lp = NULL;
  696. dip->stream = NULL;
  697. /* Actual deallocation happens later when all internal references
  698. are gone. */
  699. video_unregister_device(&dip->devbase);
  700. printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
  701. get_v4l_name(v4l_type),minor_id & 0x1f,
  702. pvr2_config_get_name(cfg));
  703. }
  704. static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
  705. {
  706. if (vp->dev_video) {
  707. pvr2_v4l2_dev_destroy(vp->dev_video);
  708. vp->dev_video = 0;
  709. }
  710. if (vp->dev_radio) {
  711. pvr2_v4l2_dev_destroy(vp->dev_radio);
  712. vp->dev_radio = 0;
  713. }
  714. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
  715. pvr2_channel_done(&vp->channel);
  716. kfree(vp);
  717. }
  718. static void pvr2_video_device_release(struct video_device *vdev)
  719. {
  720. struct pvr2_v4l2_dev *dev;
  721. dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
  722. kfree(dev);
  723. }
  724. static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
  725. {
  726. struct pvr2_v4l2 *vp;
  727. vp = container_of(chp,struct pvr2_v4l2,channel);
  728. if (!vp->channel.mc_head->disconnect_flag) return;
  729. if (vp->vfirst) return;
  730. pvr2_v4l2_destroy_no_lock(vp);
  731. }
  732. static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
  733. unsigned int cmd, unsigned long arg)
  734. {
  735. /* Temporary hack : use ivtv api until a v4l2 one is available. */
  736. #define IVTV_IOC_G_CODEC 0xFFEE7703
  737. #define IVTV_IOC_S_CODEC 0xFFEE7704
  738. if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
  739. return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
  740. }
  741. static int pvr2_v4l2_release(struct inode *inode, struct file *file)
  742. {
  743. struct pvr2_v4l2_fh *fhp = file->private_data;
  744. struct pvr2_v4l2 *vp = fhp->vhead;
  745. struct pvr2_context *mp = fhp->vhead->channel.mc_head;
  746. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
  747. if (fhp->rhp) {
  748. struct pvr2_stream *sp;
  749. struct pvr2_hdw *hdw;
  750. hdw = fhp->channel.mc_head->hdw;
  751. pvr2_hdw_set_streaming(hdw,0);
  752. sp = pvr2_ioread_get_stream(fhp->rhp);
  753. if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
  754. pvr2_ioread_destroy(fhp->rhp);
  755. fhp->rhp = NULL;
  756. }
  757. v4l2_prio_close(&vp->prio, &fhp->prio);
  758. file->private_data = NULL;
  759. pvr2_context_enter(mp); do {
  760. if (fhp->vnext) {
  761. fhp->vnext->vprev = fhp->vprev;
  762. } else {
  763. vp->vlast = fhp->vprev;
  764. }
  765. if (fhp->vprev) {
  766. fhp->vprev->vnext = fhp->vnext;
  767. } else {
  768. vp->vfirst = fhp->vnext;
  769. }
  770. fhp->vnext = NULL;
  771. fhp->vprev = NULL;
  772. fhp->vhead = NULL;
  773. pvr2_channel_done(&fhp->channel);
  774. pvr2_trace(PVR2_TRACE_STRUCT,
  775. "Destroying pvr_v4l2_fh id=%p",fhp);
  776. kfree(fhp);
  777. if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
  778. pvr2_v4l2_destroy_no_lock(vp);
  779. }
  780. } while (0); pvr2_context_exit(mp);
  781. return 0;
  782. }
  783. static int pvr2_v4l2_open(struct inode *inode, struct file *file)
  784. {
  785. struct pvr2_v4l2_dev *dip; /* Our own context pointer */
  786. struct pvr2_v4l2_fh *fhp;
  787. struct pvr2_v4l2 *vp;
  788. struct pvr2_hdw *hdw;
  789. dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
  790. vp = dip->v4lp;
  791. hdw = vp->channel.hdw;
  792. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
  793. if (!pvr2_hdw_dev_ok(hdw)) {
  794. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
  795. "pvr2_v4l2_open: hardware not ready");
  796. return -EIO;
  797. }
  798. fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
  799. if (!fhp) {
  800. return -ENOMEM;
  801. }
  802. memset(fhp,0,sizeof(*fhp));
  803. init_waitqueue_head(&fhp->wait_data);
  804. fhp->dev_info = dip;
  805. pvr2_context_enter(vp->channel.mc_head); do {
  806. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
  807. pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
  808. /* Opening the /dev/radioX device implies a mode switch.
  809. So execute that here. Note that you can get the
  810. IDENTICAL effect merely by opening the normal video
  811. device and setting the input appropriately. */
  812. if (dip->v4l_type == VFL_TYPE_RADIO) {
  813. pvr2_ctrl_set_value(
  814. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
  815. PVR2_CVAL_INPUT_RADIO);
  816. }
  817. fhp->vnext = NULL;
  818. fhp->vprev = vp->vlast;
  819. if (vp->vlast) {
  820. vp->vlast->vnext = fhp;
  821. } else {
  822. vp->vfirst = fhp;
  823. }
  824. vp->vlast = fhp;
  825. fhp->vhead = vp;
  826. } while (0); pvr2_context_exit(vp->channel.mc_head);
  827. fhp->file = file;
  828. file->private_data = fhp;
  829. v4l2_prio_open(&vp->prio,&fhp->prio);
  830. fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
  831. return 0;
  832. }
  833. static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
  834. {
  835. wake_up(&fhp->wait_data);
  836. }
  837. static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
  838. {
  839. int ret;
  840. struct pvr2_stream *sp;
  841. struct pvr2_hdw *hdw;
  842. if (fh->rhp) return 0;
  843. if (!fh->dev_info->stream) {
  844. /* No stream defined for this node. This means that we're
  845. not currently allowed to stream from this node. */
  846. return -EPERM;
  847. }
  848. /* First read() attempt. Try to claim the stream and start
  849. it... */
  850. if ((ret = pvr2_channel_claim_stream(&fh->channel,
  851. fh->dev_info->stream)) != 0) {
  852. /* Someone else must already have it */
  853. return ret;
  854. }
  855. fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
  856. if (!fh->rhp) {
  857. pvr2_channel_claim_stream(&fh->channel,NULL);
  858. return -ENOMEM;
  859. }
  860. hdw = fh->channel.mc_head->hdw;
  861. sp = fh->dev_info->stream->stream;
  862. pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
  863. pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
  864. pvr2_hdw_set_streaming(hdw,!0);
  865. ret = pvr2_ioread_set_enabled(fh->rhp,!0);
  866. return ret;
  867. }
  868. static ssize_t pvr2_v4l2_read(struct file *file,
  869. char __user *buff, size_t count, loff_t *ppos)
  870. {
  871. struct pvr2_v4l2_fh *fh = file->private_data;
  872. int ret;
  873. if (fh->fw_mode_flag) {
  874. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  875. char *tbuf;
  876. int c1,c2;
  877. int tcnt = 0;
  878. unsigned int offs = *ppos;
  879. tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
  880. if (!tbuf) return -ENOMEM;
  881. while (count) {
  882. c1 = count;
  883. if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
  884. c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
  885. if (c2 < 0) {
  886. tcnt = c2;
  887. break;
  888. }
  889. if (!c2) break;
  890. if (copy_to_user(buff,tbuf,c2)) {
  891. tcnt = -EFAULT;
  892. break;
  893. }
  894. offs += c2;
  895. tcnt += c2;
  896. buff += c2;
  897. count -= c2;
  898. *ppos += c2;
  899. }
  900. kfree(tbuf);
  901. return tcnt;
  902. }
  903. if (!fh->rhp) {
  904. ret = pvr2_v4l2_iosetup(fh);
  905. if (ret) {
  906. return ret;
  907. }
  908. }
  909. for (;;) {
  910. ret = pvr2_ioread_read(fh->rhp,buff,count);
  911. if (ret >= 0) break;
  912. if (ret != -EAGAIN) break;
  913. if (file->f_flags & O_NONBLOCK) break;
  914. /* Doing blocking I/O. Wait here. */
  915. ret = wait_event_interruptible(
  916. fh->wait_data,
  917. pvr2_ioread_avail(fh->rhp) >= 0);
  918. if (ret < 0) break;
  919. }
  920. return ret;
  921. }
  922. static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
  923. {
  924. unsigned int mask = 0;
  925. struct pvr2_v4l2_fh *fh = file->private_data;
  926. int ret;
  927. if (fh->fw_mode_flag) {
  928. mask |= POLLIN | POLLRDNORM;
  929. return mask;
  930. }
  931. if (!fh->rhp) {
  932. ret = pvr2_v4l2_iosetup(fh);
  933. if (ret) return POLLERR;
  934. }
  935. poll_wait(file,&fh->wait_data,wait);
  936. if (pvr2_ioread_avail(fh->rhp) >= 0) {
  937. mask |= POLLIN | POLLRDNORM;
  938. }
  939. return mask;
  940. }
  941. static const struct file_operations vdev_fops = {
  942. .owner = THIS_MODULE,
  943. .open = pvr2_v4l2_open,
  944. .release = pvr2_v4l2_release,
  945. .read = pvr2_v4l2_read,
  946. .ioctl = pvr2_v4l2_ioctl,
  947. .llseek = no_llseek,
  948. .poll = pvr2_v4l2_poll,
  949. };
  950. #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
  951. static struct video_device vdev_template = {
  952. .owner = THIS_MODULE,
  953. .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
  954. .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
  955. | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
  956. | V4L2_CAP_READWRITE),
  957. .hardware = VID_HARDWARE_PVRUSB2,
  958. .fops = &vdev_fops,
  959. };
  960. static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
  961. struct pvr2_v4l2 *vp,
  962. int v4l_type)
  963. {
  964. int mindevnum;
  965. int unit_number;
  966. int *nr_ptr = 0;
  967. dip->v4lp = vp;
  968. dip->v4l_type = v4l_type;
  969. switch (v4l_type) {
  970. case VFL_TYPE_GRABBER:
  971. dip->stream = &vp->channel.mc_head->video_stream;
  972. dip->config = pvr2_config_mpeg;
  973. dip->minor_type = pvr2_v4l_type_video;
  974. nr_ptr = video_nr;
  975. break;
  976. case VFL_TYPE_VBI:
  977. dip->config = pvr2_config_vbi;
  978. dip->minor_type = pvr2_v4l_type_vbi;
  979. nr_ptr = vbi_nr;
  980. break;
  981. case VFL_TYPE_RADIO:
  982. dip->config = pvr2_config_pcm;
  983. dip->minor_type = pvr2_v4l_type_radio;
  984. nr_ptr = radio_nr;
  985. break;
  986. default:
  987. /* Bail out (this should be impossible) */
  988. err("Failed to set up pvrusb2 v4l dev"
  989. " due to unrecognized config");
  990. return;
  991. }
  992. /* radio device doesn 't need its own stream */
  993. if (!dip->stream && dip->v4l_type == VFL_TYPE_GRABBER) {
  994. err("Failed to set up pvrusb2 v4l dev"
  995. " due to missing stream instance");
  996. return;
  997. }
  998. memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
  999. dip->devbase.release = pvr2_video_device_release;
  1000. mindevnum = -1;
  1001. unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
  1002. if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
  1003. mindevnum = nr_ptr[unit_number];
  1004. }
  1005. if ((video_register_device(&dip->devbase,
  1006. dip->v4l_type, mindevnum) < 0) &&
  1007. (video_register_device(&dip->devbase,
  1008. dip->v4l_type, -1) < 0)) {
  1009. err("Failed to register pvrusb2 v4l device");
  1010. }
  1011. printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
  1012. get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
  1013. pvr2_config_get_name(dip->config));
  1014. pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
  1015. dip->minor_type,dip->devbase.minor);
  1016. }
  1017. struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
  1018. {
  1019. struct pvr2_v4l2 *vp;
  1020. vp = kmalloc(sizeof(*vp),GFP_KERNEL);
  1021. if (!vp) return vp;
  1022. memset(vp,0,sizeof(*vp));
  1023. vp->dev_video = kmalloc(sizeof(*vp->dev_video),GFP_KERNEL);
  1024. vp->dev_radio = kmalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
  1025. if (!(vp->dev_video && vp->dev_radio)) {
  1026. if (vp->dev_video) kfree(vp->dev_video);
  1027. if (vp->dev_radio) kfree(vp->dev_radio);
  1028. kfree(vp);
  1029. return NULL;
  1030. }
  1031. memset(vp->dev_video,0,sizeof(*vp->dev_video));
  1032. memset(vp->dev_radio,0,sizeof(*vp->dev_radio));
  1033. pvr2_channel_init(&vp->channel,mnp);
  1034. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
  1035. vp->channel.check_func = pvr2_v4l2_internal_check;
  1036. /* register streams */
  1037. pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
  1038. pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
  1039. return vp;
  1040. }
  1041. /*
  1042. Stuff for Emacs to see, in order to encourage consistent editing style:
  1043. *** Local Variables: ***
  1044. *** mode: c ***
  1045. *** fill-column: 75 ***
  1046. *** tab-width: 8 ***
  1047. *** c-basic-offset: 8 ***
  1048. *** End: ***
  1049. */