usbvision-video.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /*
  2. * USB USBVISION Video device driver 0.9.10
  3. *
  4. *
  5. *
  6. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  7. *
  8. * This module is part of usbvision driver project.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * Let's call the version 0.... until compression decoding is completely
  21. * implemented.
  22. *
  23. * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
  24. * It was based on USB CPiA driver written by Peter Pregler,
  25. * Scott J. Bertin and Johannes Erdfelt
  26. * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
  27. * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
  28. * Updates to driver completed by Dwaine P. Garden
  29. *
  30. *
  31. * TODO:
  32. * - use submit_urb for all setup packets
  33. * - Fix memory settings for nt1004. It is 4 times as big as the
  34. * nt1003 memory.
  35. * - Add audio on endpoint 3 for nt1004 chip.
  36. * Seems impossible, needs a codec interface. Which one?
  37. * - Clean up the driver.
  38. * - optimization for performance.
  39. * - Add Videotext capability (VBI). Working on it.....
  40. * - Check audio for other devices
  41. *
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/list.h>
  45. #include <linux/timer.h>
  46. #include <linux/slab.h>
  47. #include <linux/mm.h>
  48. #include <linux/highmem.h>
  49. #include <linux/vmalloc.h>
  50. #include <linux/module.h>
  51. #include <linux/init.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/io.h>
  54. #include <linux/videodev2.h>
  55. #include <linux/i2c.h>
  56. #include <media/i2c/saa7115.h>
  57. #include <media/v4l2-common.h>
  58. #include <media/v4l2-ioctl.h>
  59. #include <media/v4l2-event.h>
  60. #include <media/tuner.h>
  61. #include <linux/workqueue.h>
  62. #include "usbvision.h"
  63. #include "usbvision-cards.h"
  64. #define DRIVER_AUTHOR \
  65. "Joerg Heckenbach <joerg@heckenbach-aw.de>, " \
  66. "Dwaine Garden <DwaineGarden@rogers.com>"
  67. #define DRIVER_NAME "usbvision"
  68. #define DRIVER_ALIAS "USBVision"
  69. #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
  70. #define DRIVER_LICENSE "GPL"
  71. #define USBVISION_VERSION_STRING "0.9.11"
  72. #define ENABLE_HEXDUMP 0 /* Enable if you need it */
  73. #ifdef USBVISION_DEBUG
  74. #define PDEBUG(level, fmt, args...) { \
  75. if (video_debug & (level)) \
  76. printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  77. __func__, __LINE__ , ## args); \
  78. }
  79. #else
  80. #define PDEBUG(level, fmt, args...) do {} while (0)
  81. #endif
  82. #define DBG_IO (1 << 1)
  83. #define DBG_PROBE (1 << 2)
  84. #define DBG_MMAP (1 << 3)
  85. /* String operations */
  86. #define rmspace(str) while (*str == ' ') str++;
  87. #define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
  88. /* sequential number of usbvision device */
  89. static int usbvision_nr;
  90. static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
  91. { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
  92. { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
  93. { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
  94. { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
  95. { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
  96. { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
  97. { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
  98. { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
  99. };
  100. /* Function prototypes */
  101. static void usbvision_release(struct usb_usbvision *usbvision);
  102. /* Default initialization of device driver parameters */
  103. /* Set the default format for ISOC endpoint */
  104. static int isoc_mode = ISOC_MODE_COMPRESS;
  105. /* Set the default Debug Mode of the device driver */
  106. static int video_debug;
  107. /* Sequential Number of Video Device */
  108. static int video_nr = -1;
  109. /* Sequential Number of Radio Device */
  110. static int radio_nr = -1;
  111. /* Grab parameters for the device driver */
  112. /* Showing parameters under SYSFS */
  113. module_param(isoc_mode, int, 0444);
  114. module_param(video_debug, int, 0444);
  115. module_param(video_nr, int, 0444);
  116. module_param(radio_nr, int, 0444);
  117. MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
  118. MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
  119. MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
  120. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  121. /* Misc stuff */
  122. MODULE_AUTHOR(DRIVER_AUTHOR);
  123. MODULE_DESCRIPTION(DRIVER_DESC);
  124. MODULE_LICENSE(DRIVER_LICENSE);
  125. MODULE_VERSION(USBVISION_VERSION_STRING);
  126. MODULE_ALIAS(DRIVER_ALIAS);
  127. /*****************************************************************************/
  128. /* SYSFS Code - Copied from the stv680.c usb module. */
  129. /* Device information is located at /sys/class/video4linux/video0 */
  130. /* Device parameters information is located at /sys/module/usbvision */
  131. /* Device USB Information is located at */
  132. /* /sys/bus/usb/drivers/USBVision Video Grabber */
  133. /*****************************************************************************/
  134. #define YES_NO(x) ((x) ? "Yes" : "No")
  135. static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
  136. {
  137. struct video_device *vdev = to_video_device(cd);
  138. return video_get_drvdata(vdev);
  139. }
  140. static ssize_t show_version(struct device *cd,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
  144. }
  145. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  146. static ssize_t show_model(struct device *cd,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct video_device *vdev = to_video_device(cd);
  150. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  151. return sprintf(buf, "%s\n",
  152. usbvision_device_data[usbvision->dev_model].model_string);
  153. }
  154. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  155. static ssize_t show_hue(struct device *cd,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct video_device *vdev = to_video_device(cd);
  159. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  160. s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
  161. V4L2_CID_HUE));
  162. return sprintf(buf, "%d\n", val);
  163. }
  164. static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  165. static ssize_t show_contrast(struct device *cd,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct video_device *vdev = to_video_device(cd);
  169. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  170. s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
  171. V4L2_CID_CONTRAST));
  172. return sprintf(buf, "%d\n", val);
  173. }
  174. static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  175. static ssize_t show_brightness(struct device *cd,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct video_device *vdev = to_video_device(cd);
  179. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  180. s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
  181. V4L2_CID_BRIGHTNESS));
  182. return sprintf(buf, "%d\n", val);
  183. }
  184. static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  185. static ssize_t show_saturation(struct device *cd,
  186. struct device_attribute *attr, char *buf)
  187. {
  188. struct video_device *vdev = to_video_device(cd);
  189. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  190. s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
  191. V4L2_CID_SATURATION));
  192. return sprintf(buf, "%d\n", val);
  193. }
  194. static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  195. static ssize_t show_streaming(struct device *cd,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct video_device *vdev = to_video_device(cd);
  199. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  200. return sprintf(buf, "%s\n",
  201. YES_NO(usbvision->streaming == stream_on ? 1 : 0));
  202. }
  203. static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
  204. static ssize_t show_compression(struct device *cd,
  205. struct device_attribute *attr, char *buf)
  206. {
  207. struct video_device *vdev = to_video_device(cd);
  208. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  209. return sprintf(buf, "%s\n",
  210. YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
  211. }
  212. static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
  213. static ssize_t show_device_bridge(struct device *cd,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct video_device *vdev = to_video_device(cd);
  217. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  218. return sprintf(buf, "%d\n", usbvision->bridge_type);
  219. }
  220. static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
  221. static void usbvision_create_sysfs(struct video_device *vdev)
  222. {
  223. int res;
  224. if (!vdev)
  225. return;
  226. do {
  227. res = device_create_file(&vdev->dev, &dev_attr_version);
  228. if (res < 0)
  229. break;
  230. res = device_create_file(&vdev->dev, &dev_attr_model);
  231. if (res < 0)
  232. break;
  233. res = device_create_file(&vdev->dev, &dev_attr_hue);
  234. if (res < 0)
  235. break;
  236. res = device_create_file(&vdev->dev, &dev_attr_contrast);
  237. if (res < 0)
  238. break;
  239. res = device_create_file(&vdev->dev, &dev_attr_brightness);
  240. if (res < 0)
  241. break;
  242. res = device_create_file(&vdev->dev, &dev_attr_saturation);
  243. if (res < 0)
  244. break;
  245. res = device_create_file(&vdev->dev, &dev_attr_streaming);
  246. if (res < 0)
  247. break;
  248. res = device_create_file(&vdev->dev, &dev_attr_compression);
  249. if (res < 0)
  250. break;
  251. res = device_create_file(&vdev->dev, &dev_attr_bridge);
  252. if (res >= 0)
  253. return;
  254. } while (0);
  255. dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
  256. }
  257. static void usbvision_remove_sysfs(struct video_device *vdev)
  258. {
  259. if (vdev) {
  260. device_remove_file(&vdev->dev, &dev_attr_version);
  261. device_remove_file(&vdev->dev, &dev_attr_model);
  262. device_remove_file(&vdev->dev, &dev_attr_hue);
  263. device_remove_file(&vdev->dev, &dev_attr_contrast);
  264. device_remove_file(&vdev->dev, &dev_attr_brightness);
  265. device_remove_file(&vdev->dev, &dev_attr_saturation);
  266. device_remove_file(&vdev->dev, &dev_attr_streaming);
  267. device_remove_file(&vdev->dev, &dev_attr_compression);
  268. device_remove_file(&vdev->dev, &dev_attr_bridge);
  269. }
  270. }
  271. /*
  272. * usbvision_open()
  273. *
  274. * This is part of Video 4 Linux API. The driver can be opened by one
  275. * client only (checks internal counter 'usbvision->user'). The procedure
  276. * then allocates buffers needed for video processing.
  277. *
  278. */
  279. static int usbvision_v4l2_open(struct file *file)
  280. {
  281. struct usb_usbvision *usbvision = video_drvdata(file);
  282. int err_code = 0;
  283. PDEBUG(DBG_IO, "open");
  284. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  285. return -ERESTARTSYS;
  286. if (usbvision->user) {
  287. err_code = -EBUSY;
  288. } else {
  289. err_code = v4l2_fh_open(file);
  290. if (err_code)
  291. goto unlock;
  292. /* Allocate memory for the scratch ring buffer */
  293. err_code = usbvision_scratch_alloc(usbvision);
  294. if (isoc_mode == ISOC_MODE_COMPRESS) {
  295. /* Allocate intermediate decompression buffers
  296. only if needed */
  297. err_code = usbvision_decompress_alloc(usbvision);
  298. }
  299. if (err_code) {
  300. /* Deallocate all buffers if trouble */
  301. usbvision_scratch_free(usbvision);
  302. usbvision_decompress_free(usbvision);
  303. }
  304. }
  305. /* If so far no errors then we shall start the camera */
  306. if (!err_code) {
  307. /* Send init sequence only once, it's large! */
  308. if (!usbvision->initialized) {
  309. int setup_ok = 0;
  310. setup_ok = usbvision_setup(usbvision, isoc_mode);
  311. if (setup_ok)
  312. usbvision->initialized = 1;
  313. else
  314. err_code = -EBUSY;
  315. }
  316. if (!err_code) {
  317. usbvision_begin_streaming(usbvision);
  318. err_code = usbvision_init_isoc(usbvision);
  319. /* device must be initialized before isoc transfer */
  320. usbvision_muxsel(usbvision, 0);
  321. /* prepare queues */
  322. usbvision_empty_framequeues(usbvision);
  323. usbvision->user++;
  324. }
  325. }
  326. unlock:
  327. mutex_unlock(&usbvision->v4l2_lock);
  328. PDEBUG(DBG_IO, "success");
  329. return err_code;
  330. }
  331. /*
  332. * usbvision_v4l2_close()
  333. *
  334. * This is part of Video 4 Linux API. The procedure
  335. * stops streaming and deallocates all buffers that were earlier
  336. * allocated in usbvision_v4l2_open().
  337. *
  338. */
  339. static int usbvision_v4l2_close(struct file *file)
  340. {
  341. struct usb_usbvision *usbvision = video_drvdata(file);
  342. PDEBUG(DBG_IO, "close");
  343. mutex_lock(&usbvision->v4l2_lock);
  344. usbvision_audio_off(usbvision);
  345. usbvision_restart_isoc(usbvision);
  346. usbvision_stop_isoc(usbvision);
  347. usbvision_decompress_free(usbvision);
  348. usbvision_frames_free(usbvision);
  349. usbvision_empty_framequeues(usbvision);
  350. usbvision_scratch_free(usbvision);
  351. usbvision->user--;
  352. mutex_unlock(&usbvision->v4l2_lock);
  353. if (usbvision->remove_pending) {
  354. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  355. usbvision_release(usbvision);
  356. return 0;
  357. }
  358. PDEBUG(DBG_IO, "success");
  359. return v4l2_fh_release(file);
  360. }
  361. /*
  362. * usbvision_ioctl()
  363. *
  364. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  365. *
  366. */
  367. #ifdef CONFIG_VIDEO_ADV_DEBUG
  368. static int vidioc_g_register(struct file *file, void *priv,
  369. struct v4l2_dbg_register *reg)
  370. {
  371. struct usb_usbvision *usbvision = video_drvdata(file);
  372. int err_code;
  373. /* NT100x has a 8-bit register space */
  374. err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
  375. if (err_code < 0) {
  376. dev_err(&usbvision->vdev.dev,
  377. "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
  378. __func__, err_code);
  379. return err_code;
  380. }
  381. reg->val = err_code;
  382. reg->size = 1;
  383. return 0;
  384. }
  385. static int vidioc_s_register(struct file *file, void *priv,
  386. const struct v4l2_dbg_register *reg)
  387. {
  388. struct usb_usbvision *usbvision = video_drvdata(file);
  389. int err_code;
  390. /* NT100x has a 8-bit register space */
  391. err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
  392. if (err_code < 0) {
  393. dev_err(&usbvision->vdev.dev,
  394. "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
  395. __func__, err_code);
  396. return err_code;
  397. }
  398. return 0;
  399. }
  400. #endif
  401. static int vidioc_querycap(struct file *file, void *priv,
  402. struct v4l2_capability *vc)
  403. {
  404. struct usb_usbvision *usbvision = video_drvdata(file);
  405. struct video_device *vdev = video_devdata(file);
  406. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  407. strlcpy(vc->card,
  408. usbvision_device_data[usbvision->dev_model].model_string,
  409. sizeof(vc->card));
  410. usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
  411. vc->device_caps = usbvision->have_tuner ? V4L2_CAP_TUNER : 0;
  412. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  413. vc->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  414. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  415. else
  416. vc->device_caps |= V4L2_CAP_RADIO;
  417. vc->capabilities = vc->device_caps | V4L2_CAP_VIDEO_CAPTURE |
  418. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  419. if (usbvision_device_data[usbvision->dev_model].radio)
  420. vc->capabilities |= V4L2_CAP_RADIO;
  421. return 0;
  422. }
  423. static int vidioc_enum_input(struct file *file, void *priv,
  424. struct v4l2_input *vi)
  425. {
  426. struct usb_usbvision *usbvision = video_drvdata(file);
  427. int chan;
  428. if (vi->index >= usbvision->video_inputs)
  429. return -EINVAL;
  430. if (usbvision->have_tuner)
  431. chan = vi->index;
  432. else
  433. chan = vi->index + 1; /* skip Television string*/
  434. /* Determine the requested input characteristics
  435. specific for each usbvision card model */
  436. switch (chan) {
  437. case 0:
  438. if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
  439. strcpy(vi->name, "White Video Input");
  440. } else {
  441. strcpy(vi->name, "Television");
  442. vi->type = V4L2_INPUT_TYPE_TUNER;
  443. vi->tuner = chan;
  444. vi->std = USBVISION_NORMS;
  445. }
  446. break;
  447. case 1:
  448. vi->type = V4L2_INPUT_TYPE_CAMERA;
  449. if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
  450. strcpy(vi->name, "Green Video Input");
  451. else
  452. strcpy(vi->name, "Composite Video Input");
  453. vi->std = USBVISION_NORMS;
  454. break;
  455. case 2:
  456. vi->type = V4L2_INPUT_TYPE_CAMERA;
  457. if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
  458. strcpy(vi->name, "Yellow Video Input");
  459. else
  460. strcpy(vi->name, "S-Video Input");
  461. vi->std = USBVISION_NORMS;
  462. break;
  463. case 3:
  464. vi->type = V4L2_INPUT_TYPE_CAMERA;
  465. strcpy(vi->name, "Red Video Input");
  466. vi->std = USBVISION_NORMS;
  467. break;
  468. }
  469. return 0;
  470. }
  471. static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
  472. {
  473. struct usb_usbvision *usbvision = video_drvdata(file);
  474. *input = usbvision->ctl_input;
  475. return 0;
  476. }
  477. static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
  478. {
  479. struct usb_usbvision *usbvision = video_drvdata(file);
  480. if (input >= usbvision->video_inputs)
  481. return -EINVAL;
  482. usbvision_muxsel(usbvision, input);
  483. usbvision_set_input(usbvision);
  484. usbvision_set_output(usbvision,
  485. usbvision->curwidth,
  486. usbvision->curheight);
  487. return 0;
  488. }
  489. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  490. {
  491. struct usb_usbvision *usbvision = video_drvdata(file);
  492. usbvision->tvnorm_id = id;
  493. call_all(usbvision, video, s_std, usbvision->tvnorm_id);
  494. /* propagate the change to the decoder */
  495. usbvision_muxsel(usbvision, usbvision->ctl_input);
  496. return 0;
  497. }
  498. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  499. {
  500. struct usb_usbvision *usbvision = video_drvdata(file);
  501. *id = usbvision->tvnorm_id;
  502. return 0;
  503. }
  504. static int vidioc_g_tuner(struct file *file, void *priv,
  505. struct v4l2_tuner *vt)
  506. {
  507. struct usb_usbvision *usbvision = video_drvdata(file);
  508. if (vt->index) /* Only tuner 0 */
  509. return -EINVAL;
  510. if (vt->type == V4L2_TUNER_RADIO)
  511. strcpy(vt->name, "Radio");
  512. else
  513. strcpy(vt->name, "Television");
  514. /* Let clients fill in the remainder of this struct */
  515. call_all(usbvision, tuner, g_tuner, vt);
  516. return 0;
  517. }
  518. static int vidioc_s_tuner(struct file *file, void *priv,
  519. const struct v4l2_tuner *vt)
  520. {
  521. struct usb_usbvision *usbvision = video_drvdata(file);
  522. /* Only one tuner for now */
  523. if (vt->index)
  524. return -EINVAL;
  525. /* let clients handle this */
  526. call_all(usbvision, tuner, s_tuner, vt);
  527. return 0;
  528. }
  529. static int vidioc_g_frequency(struct file *file, void *priv,
  530. struct v4l2_frequency *freq)
  531. {
  532. struct usb_usbvision *usbvision = video_drvdata(file);
  533. /* Only one tuner */
  534. if (freq->tuner)
  535. return -EINVAL;
  536. if (freq->type == V4L2_TUNER_RADIO)
  537. freq->frequency = usbvision->radio_freq;
  538. else
  539. freq->frequency = usbvision->tv_freq;
  540. return 0;
  541. }
  542. static int vidioc_s_frequency(struct file *file, void *priv,
  543. const struct v4l2_frequency *freq)
  544. {
  545. struct usb_usbvision *usbvision = video_drvdata(file);
  546. struct v4l2_frequency new_freq = *freq;
  547. /* Only one tuner for now */
  548. if (freq->tuner)
  549. return -EINVAL;
  550. call_all(usbvision, tuner, s_frequency, freq);
  551. call_all(usbvision, tuner, g_frequency, &new_freq);
  552. if (freq->type == V4L2_TUNER_RADIO)
  553. usbvision->radio_freq = new_freq.frequency;
  554. else
  555. usbvision->tv_freq = new_freq.frequency;
  556. return 0;
  557. }
  558. static int vidioc_reqbufs(struct file *file,
  559. void *priv, struct v4l2_requestbuffers *vr)
  560. {
  561. struct usb_usbvision *usbvision = video_drvdata(file);
  562. int ret;
  563. RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
  564. /* Check input validity:
  565. the user must do a VIDEO CAPTURE and MMAP method. */
  566. if (vr->memory != V4L2_MEMORY_MMAP)
  567. return -EINVAL;
  568. if (usbvision->streaming == stream_on) {
  569. ret = usbvision_stream_interrupt(usbvision);
  570. if (ret)
  571. return ret;
  572. }
  573. usbvision_frames_free(usbvision);
  574. usbvision_empty_framequeues(usbvision);
  575. vr->count = usbvision_frames_alloc(usbvision, vr->count);
  576. usbvision->cur_frame = NULL;
  577. return 0;
  578. }
  579. static int vidioc_querybuf(struct file *file,
  580. void *priv, struct v4l2_buffer *vb)
  581. {
  582. struct usb_usbvision *usbvision = video_drvdata(file);
  583. struct usbvision_frame *frame;
  584. /* FIXME : must control
  585. that buffers are mapped (VIDIOC_REQBUFS has been called) */
  586. if (vb->index >= usbvision->num_frames)
  587. return -EINVAL;
  588. /* Updating the corresponding frame state */
  589. vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  590. frame = &usbvision->frame[vb->index];
  591. if (frame->grabstate >= frame_state_ready)
  592. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  593. if (frame->grabstate >= frame_state_done)
  594. vb->flags |= V4L2_BUF_FLAG_DONE;
  595. if (frame->grabstate == frame_state_unused)
  596. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  597. vb->memory = V4L2_MEMORY_MMAP;
  598. vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
  599. vb->memory = V4L2_MEMORY_MMAP;
  600. vb->field = V4L2_FIELD_NONE;
  601. vb->length = usbvision->curwidth *
  602. usbvision->curheight *
  603. usbvision->palette.bytes_per_pixel;
  604. vb->timestamp = usbvision->frame[vb->index].timestamp;
  605. vb->sequence = usbvision->frame[vb->index].sequence;
  606. return 0;
  607. }
  608. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
  609. {
  610. struct usb_usbvision *usbvision = video_drvdata(file);
  611. struct usbvision_frame *frame;
  612. unsigned long lock_flags;
  613. /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
  614. if (vb->index >= usbvision->num_frames)
  615. return -EINVAL;
  616. frame = &usbvision->frame[vb->index];
  617. if (frame->grabstate != frame_state_unused)
  618. return -EAGAIN;
  619. /* Mark it as ready and enqueue frame */
  620. frame->grabstate = frame_state_ready;
  621. frame->scanstate = scan_state_scanning;
  622. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  623. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  624. /* set v4l2_format index */
  625. frame->v4l2_format = usbvision->palette;
  626. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  627. list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
  628. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  629. return 0;
  630. }
  631. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
  632. {
  633. struct usb_usbvision *usbvision = video_drvdata(file);
  634. int ret;
  635. struct usbvision_frame *f;
  636. unsigned long lock_flags;
  637. if (list_empty(&(usbvision->outqueue))) {
  638. if (usbvision->streaming == stream_idle)
  639. return -EINVAL;
  640. ret = wait_event_interruptible
  641. (usbvision->wait_frame,
  642. !list_empty(&(usbvision->outqueue)));
  643. if (ret)
  644. return ret;
  645. }
  646. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  647. f = list_entry(usbvision->outqueue.next,
  648. struct usbvision_frame, frame);
  649. list_del(usbvision->outqueue.next);
  650. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  651. f->grabstate = frame_state_unused;
  652. vb->memory = V4L2_MEMORY_MMAP;
  653. vb->flags = V4L2_BUF_FLAG_MAPPED |
  654. V4L2_BUF_FLAG_QUEUED |
  655. V4L2_BUF_FLAG_DONE |
  656. V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  657. vb->index = f->index;
  658. vb->sequence = f->sequence;
  659. vb->timestamp = f->timestamp;
  660. vb->field = V4L2_FIELD_NONE;
  661. vb->bytesused = f->scanlength;
  662. return 0;
  663. }
  664. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  665. {
  666. struct usb_usbvision *usbvision = video_drvdata(file);
  667. usbvision->streaming = stream_on;
  668. call_all(usbvision, video, s_stream, 1);
  669. return 0;
  670. }
  671. static int vidioc_streamoff(struct file *file,
  672. void *priv, enum v4l2_buf_type type)
  673. {
  674. struct usb_usbvision *usbvision = video_drvdata(file);
  675. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  676. return -EINVAL;
  677. if (usbvision->streaming == stream_on) {
  678. usbvision_stream_interrupt(usbvision);
  679. /* Stop all video streamings */
  680. call_all(usbvision, video, s_stream, 0);
  681. }
  682. usbvision_empty_framequeues(usbvision);
  683. return 0;
  684. }
  685. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  686. struct v4l2_fmtdesc *vfd)
  687. {
  688. if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
  689. return -EINVAL;
  690. strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
  691. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  692. return 0;
  693. }
  694. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  695. struct v4l2_format *vf)
  696. {
  697. struct usb_usbvision *usbvision = video_drvdata(file);
  698. vf->fmt.pix.width = usbvision->curwidth;
  699. vf->fmt.pix.height = usbvision->curheight;
  700. vf->fmt.pix.pixelformat = usbvision->palette.format;
  701. vf->fmt.pix.bytesperline =
  702. usbvision->curwidth * usbvision->palette.bytes_per_pixel;
  703. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
  704. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  705. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  706. return 0;
  707. }
  708. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  709. struct v4l2_format *vf)
  710. {
  711. struct usb_usbvision *usbvision = video_drvdata(file);
  712. int format_idx;
  713. /* Find requested format in available ones */
  714. for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
  715. if (vf->fmt.pix.pixelformat ==
  716. usbvision_v4l2_format[format_idx].format) {
  717. usbvision->palette = usbvision_v4l2_format[format_idx];
  718. break;
  719. }
  720. }
  721. /* robustness */
  722. if (format_idx == USBVISION_SUPPORTED_PALETTES)
  723. return -EINVAL;
  724. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  725. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  726. vf->fmt.pix.bytesperline = vf->fmt.pix.width*
  727. usbvision->palette.bytes_per_pixel;
  728. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
  729. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  730. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  731. return 0;
  732. }
  733. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  734. struct v4l2_format *vf)
  735. {
  736. struct usb_usbvision *usbvision = video_drvdata(file);
  737. int ret;
  738. ret = vidioc_try_fmt_vid_cap(file, priv, vf);
  739. if (ret)
  740. return ret;
  741. /* stop io in case it is already in progress */
  742. if (usbvision->streaming == stream_on) {
  743. ret = usbvision_stream_interrupt(usbvision);
  744. if (ret)
  745. return ret;
  746. }
  747. usbvision_frames_free(usbvision);
  748. usbvision_empty_framequeues(usbvision);
  749. usbvision->cur_frame = NULL;
  750. /* by now we are committed to the new data... */
  751. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  752. return 0;
  753. }
  754. static ssize_t usbvision_read(struct file *file, char __user *buf,
  755. size_t count, loff_t *ppos)
  756. {
  757. struct usb_usbvision *usbvision = video_drvdata(file);
  758. int noblock = file->f_flags & O_NONBLOCK;
  759. unsigned long lock_flags;
  760. int ret, i;
  761. struct usbvision_frame *frame;
  762. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
  763. (unsigned long)count, noblock);
  764. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  765. return -EFAULT;
  766. /* This entry point is compatible with the mmap routines
  767. so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
  768. to get frames or call read on the device. */
  769. if (!usbvision->num_frames) {
  770. /* First, allocate some frames to work with
  771. if this has not been done with VIDIOC_REQBUF */
  772. usbvision_frames_free(usbvision);
  773. usbvision_empty_framequeues(usbvision);
  774. usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
  775. }
  776. if (usbvision->streaming != stream_on) {
  777. /* no stream is running, make it running ! */
  778. usbvision->streaming = stream_on;
  779. call_all(usbvision, video, s_stream, 1);
  780. }
  781. /* Then, enqueue as many frames as possible
  782. (like a user of VIDIOC_QBUF would do) */
  783. for (i = 0; i < usbvision->num_frames; i++) {
  784. frame = &usbvision->frame[i];
  785. if (frame->grabstate == frame_state_unused) {
  786. /* Mark it as ready and enqueue frame */
  787. frame->grabstate = frame_state_ready;
  788. frame->scanstate = scan_state_scanning;
  789. /* Accumulated in usbvision_parse_data() */
  790. frame->scanlength = 0;
  791. /* set v4l2_format index */
  792. frame->v4l2_format = usbvision->palette;
  793. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  794. list_add_tail(&frame->frame, &usbvision->inqueue);
  795. spin_unlock_irqrestore(&usbvision->queue_lock,
  796. lock_flags);
  797. }
  798. }
  799. /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
  800. if (list_empty(&(usbvision->outqueue))) {
  801. if (noblock)
  802. return -EAGAIN;
  803. ret = wait_event_interruptible
  804. (usbvision->wait_frame,
  805. !list_empty(&(usbvision->outqueue)));
  806. if (ret)
  807. return ret;
  808. }
  809. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  810. frame = list_entry(usbvision->outqueue.next,
  811. struct usbvision_frame, frame);
  812. list_del(usbvision->outqueue.next);
  813. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  814. /* An error returns an empty frame */
  815. if (frame->grabstate == frame_state_error) {
  816. frame->bytes_read = 0;
  817. return 0;
  818. }
  819. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
  820. __func__,
  821. frame->index, frame->bytes_read, frame->scanlength);
  822. /* copy bytes to user space; we allow for partials reads */
  823. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  824. count = frame->scanlength - frame->bytes_read;
  825. if (copy_to_user(buf, frame->data + frame->bytes_read, count))
  826. return -EFAULT;
  827. frame->bytes_read += count;
  828. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
  829. __func__,
  830. (unsigned long)count, frame->bytes_read);
  831. #if 1
  832. /*
  833. * FIXME:
  834. * For now, forget the frame if it has not been read in one shot.
  835. */
  836. frame->bytes_read = 0;
  837. /* Mark it as available to be used again. */
  838. frame->grabstate = frame_state_unused;
  839. #else
  840. if (frame->bytes_read >= frame->scanlength) {
  841. /* All data has been read */
  842. frame->bytes_read = 0;
  843. /* Mark it as available to be used again. */
  844. frame->grabstate = frame_state_unused;
  845. }
  846. #endif
  847. return count;
  848. }
  849. static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
  850. size_t count, loff_t *ppos)
  851. {
  852. struct usb_usbvision *usbvision = video_drvdata(file);
  853. int res;
  854. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  855. return -ERESTARTSYS;
  856. res = usbvision_read(file, buf, count, ppos);
  857. mutex_unlock(&usbvision->v4l2_lock);
  858. return res;
  859. }
  860. static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
  861. {
  862. unsigned long size = vma->vm_end - vma->vm_start,
  863. start = vma->vm_start;
  864. void *pos;
  865. u32 i;
  866. struct usb_usbvision *usbvision = video_drvdata(file);
  867. PDEBUG(DBG_MMAP, "mmap");
  868. if (!USBVISION_IS_OPERATIONAL(usbvision))
  869. return -EFAULT;
  870. if (!(vma->vm_flags & VM_WRITE) ||
  871. size != PAGE_ALIGN(usbvision->max_frame_size)) {
  872. return -EINVAL;
  873. }
  874. for (i = 0; i < usbvision->num_frames; i++) {
  875. if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
  876. vma->vm_pgoff)
  877. break;
  878. }
  879. if (i == usbvision->num_frames) {
  880. PDEBUG(DBG_MMAP,
  881. "mmap: user supplied mapping address is out of range");
  882. return -EINVAL;
  883. }
  884. /* VM_IO is eventually going to replace PageReserved altogether */
  885. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  886. pos = usbvision->frame[i].data;
  887. while (size > 0) {
  888. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  889. PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
  890. return -EAGAIN;
  891. }
  892. start += PAGE_SIZE;
  893. pos += PAGE_SIZE;
  894. size -= PAGE_SIZE;
  895. }
  896. return 0;
  897. }
  898. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  899. {
  900. struct usb_usbvision *usbvision = video_drvdata(file);
  901. int res;
  902. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  903. return -ERESTARTSYS;
  904. res = usbvision_mmap(file, vma);
  905. mutex_unlock(&usbvision->v4l2_lock);
  906. return res;
  907. }
  908. /*
  909. * Here comes the stuff for radio on usbvision based devices
  910. *
  911. */
  912. static int usbvision_radio_open(struct file *file)
  913. {
  914. struct usb_usbvision *usbvision = video_drvdata(file);
  915. int err_code = 0;
  916. PDEBUG(DBG_IO, "%s:", __func__);
  917. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  918. return -ERESTARTSYS;
  919. err_code = v4l2_fh_open(file);
  920. if (err_code)
  921. goto out;
  922. if (usbvision->user) {
  923. dev_err(&usbvision->rdev.dev,
  924. "%s: Someone tried to open an already opened USBVision Radio!\n",
  925. __func__);
  926. err_code = -EBUSY;
  927. } else {
  928. /* Alternate interface 1 is is the biggest frame size */
  929. err_code = usbvision_set_alternate(usbvision);
  930. if (err_code < 0) {
  931. usbvision->last_error = err_code;
  932. err_code = -EBUSY;
  933. goto out;
  934. }
  935. /* If so far no errors then we shall start the radio */
  936. usbvision->radio = 1;
  937. call_all(usbvision, tuner, s_radio);
  938. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  939. usbvision->user++;
  940. }
  941. out:
  942. mutex_unlock(&usbvision->v4l2_lock);
  943. return err_code;
  944. }
  945. static int usbvision_radio_close(struct file *file)
  946. {
  947. struct usb_usbvision *usbvision = video_drvdata(file);
  948. PDEBUG(DBG_IO, "");
  949. mutex_lock(&usbvision->v4l2_lock);
  950. /* Set packet size to 0 */
  951. usbvision->iface_alt = 0;
  952. usb_set_interface(usbvision->dev, usbvision->iface,
  953. usbvision->iface_alt);
  954. usbvision_audio_off(usbvision);
  955. usbvision->radio = 0;
  956. usbvision->user--;
  957. mutex_unlock(&usbvision->v4l2_lock);
  958. if (usbvision->remove_pending) {
  959. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  960. v4l2_fh_release(file);
  961. usbvision_release(usbvision);
  962. return 0;
  963. }
  964. PDEBUG(DBG_IO, "success");
  965. return v4l2_fh_release(file);
  966. }
  967. /* Video registration stuff */
  968. /* Video template */
  969. static const struct v4l2_file_operations usbvision_fops = {
  970. .owner = THIS_MODULE,
  971. .open = usbvision_v4l2_open,
  972. .release = usbvision_v4l2_close,
  973. .read = usbvision_v4l2_read,
  974. .mmap = usbvision_v4l2_mmap,
  975. .unlocked_ioctl = video_ioctl2,
  976. };
  977. static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
  978. .vidioc_querycap = vidioc_querycap,
  979. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  980. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  981. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  982. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  983. .vidioc_reqbufs = vidioc_reqbufs,
  984. .vidioc_querybuf = vidioc_querybuf,
  985. .vidioc_qbuf = vidioc_qbuf,
  986. .vidioc_dqbuf = vidioc_dqbuf,
  987. .vidioc_s_std = vidioc_s_std,
  988. .vidioc_g_std = vidioc_g_std,
  989. .vidioc_enum_input = vidioc_enum_input,
  990. .vidioc_g_input = vidioc_g_input,
  991. .vidioc_s_input = vidioc_s_input,
  992. .vidioc_streamon = vidioc_streamon,
  993. .vidioc_streamoff = vidioc_streamoff,
  994. .vidioc_g_tuner = vidioc_g_tuner,
  995. .vidioc_s_tuner = vidioc_s_tuner,
  996. .vidioc_g_frequency = vidioc_g_frequency,
  997. .vidioc_s_frequency = vidioc_s_frequency,
  998. .vidioc_log_status = v4l2_ctrl_log_status,
  999. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1000. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1001. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1002. .vidioc_g_register = vidioc_g_register,
  1003. .vidioc_s_register = vidioc_s_register,
  1004. #endif
  1005. };
  1006. static struct video_device usbvision_video_template = {
  1007. .fops = &usbvision_fops,
  1008. .ioctl_ops = &usbvision_ioctl_ops,
  1009. .name = "usbvision-video",
  1010. .release = video_device_release_empty,
  1011. .tvnorms = USBVISION_NORMS,
  1012. };
  1013. /* Radio template */
  1014. static const struct v4l2_file_operations usbvision_radio_fops = {
  1015. .owner = THIS_MODULE,
  1016. .open = usbvision_radio_open,
  1017. .release = usbvision_radio_close,
  1018. .poll = v4l2_ctrl_poll,
  1019. .unlocked_ioctl = video_ioctl2,
  1020. };
  1021. static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
  1022. .vidioc_querycap = vidioc_querycap,
  1023. .vidioc_g_tuner = vidioc_g_tuner,
  1024. .vidioc_s_tuner = vidioc_s_tuner,
  1025. .vidioc_g_frequency = vidioc_g_frequency,
  1026. .vidioc_s_frequency = vidioc_s_frequency,
  1027. .vidioc_log_status = v4l2_ctrl_log_status,
  1028. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1029. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1030. };
  1031. static struct video_device usbvision_radio_template = {
  1032. .fops = &usbvision_radio_fops,
  1033. .name = "usbvision-radio",
  1034. .release = video_device_release_empty,
  1035. .ioctl_ops = &usbvision_radio_ioctl_ops,
  1036. };
  1037. static void usbvision_vdev_init(struct usb_usbvision *usbvision,
  1038. struct video_device *vdev,
  1039. const struct video_device *vdev_template,
  1040. const char *name)
  1041. {
  1042. struct usb_device *usb_dev = usbvision->dev;
  1043. if (usb_dev == NULL) {
  1044. dev_err(&usbvision->dev->dev,
  1045. "%s: usbvision->dev is not set\n", __func__);
  1046. return;
  1047. }
  1048. *vdev = *vdev_template;
  1049. vdev->lock = &usbvision->v4l2_lock;
  1050. vdev->v4l2_dev = &usbvision->v4l2_dev;
  1051. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1052. video_set_drvdata(vdev, usbvision);
  1053. }
  1054. /* unregister video4linux devices */
  1055. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1056. {
  1057. /* Radio Device: */
  1058. if (video_is_registered(&usbvision->rdev)) {
  1059. PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
  1060. video_device_node_name(&usbvision->rdev));
  1061. video_unregister_device(&usbvision->rdev);
  1062. }
  1063. /* Video Device: */
  1064. if (video_is_registered(&usbvision->vdev)) {
  1065. PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
  1066. video_device_node_name(&usbvision->vdev));
  1067. video_unregister_device(&usbvision->vdev);
  1068. }
  1069. }
  1070. /* register video4linux devices */
  1071. static int usbvision_register_video(struct usb_usbvision *usbvision)
  1072. {
  1073. int res = -ENOMEM;
  1074. /* Video Device: */
  1075. usbvision_vdev_init(usbvision, &usbvision->vdev,
  1076. &usbvision_video_template, "USBVision Video");
  1077. if (!usbvision->have_tuner) {
  1078. v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
  1079. v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
  1080. v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
  1081. v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
  1082. }
  1083. if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
  1084. goto err_exit;
  1085. printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
  1086. usbvision->nr, video_device_node_name(&usbvision->vdev));
  1087. /* Radio Device: */
  1088. if (usbvision_device_data[usbvision->dev_model].radio) {
  1089. /* usbvision has radio */
  1090. usbvision_vdev_init(usbvision, &usbvision->rdev,
  1091. &usbvision_radio_template, "USBVision Radio");
  1092. if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
  1093. goto err_exit;
  1094. printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
  1095. usbvision->nr, video_device_node_name(&usbvision->rdev));
  1096. }
  1097. /* all done */
  1098. return 0;
  1099. err_exit:
  1100. dev_err(&usbvision->dev->dev,
  1101. "USBVision[%d]: video_register_device() failed\n",
  1102. usbvision->nr);
  1103. usbvision_unregister_video(usbvision);
  1104. return res;
  1105. }
  1106. /*
  1107. * usbvision_alloc()
  1108. *
  1109. * This code allocates the struct usb_usbvision.
  1110. * It is filled with default values.
  1111. *
  1112. * Returns NULL on error, a pointer to usb_usbvision else.
  1113. *
  1114. */
  1115. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
  1116. struct usb_interface *intf)
  1117. {
  1118. struct usb_usbvision *usbvision;
  1119. usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
  1120. if (usbvision == NULL)
  1121. return NULL;
  1122. usbvision->dev = dev;
  1123. if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
  1124. goto err_free;
  1125. if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
  1126. goto err_unreg;
  1127. usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
  1128. mutex_init(&usbvision->v4l2_lock);
  1129. /* prepare control urb for control messages during interrupts */
  1130. usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1131. if (usbvision->ctrl_urb == NULL)
  1132. goto err_unreg;
  1133. return usbvision;
  1134. err_unreg:
  1135. v4l2_ctrl_handler_free(&usbvision->hdl);
  1136. v4l2_device_unregister(&usbvision->v4l2_dev);
  1137. err_free:
  1138. kfree(usbvision);
  1139. return NULL;
  1140. }
  1141. /*
  1142. * usbvision_release()
  1143. *
  1144. * This code does final release of struct usb_usbvision. This happens
  1145. * after the device is disconnected -and- all clients closed their files.
  1146. *
  1147. */
  1148. static void usbvision_release(struct usb_usbvision *usbvision)
  1149. {
  1150. PDEBUG(DBG_PROBE, "");
  1151. usbvision->initialized = 0;
  1152. usbvision_remove_sysfs(&usbvision->vdev);
  1153. usbvision_unregister_video(usbvision);
  1154. kfree(usbvision->alt_max_pkt_size);
  1155. usb_free_urb(usbvision->ctrl_urb);
  1156. v4l2_ctrl_handler_free(&usbvision->hdl);
  1157. v4l2_device_unregister(&usbvision->v4l2_dev);
  1158. kfree(usbvision);
  1159. PDEBUG(DBG_PROBE, "success");
  1160. }
  1161. /*********************** usb interface **********************************/
  1162. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1163. {
  1164. int model;
  1165. if (usbvision == NULL)
  1166. return;
  1167. model = usbvision->dev_model;
  1168. usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
  1169. if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
  1170. usbvision->vin_reg2_preset =
  1171. usbvision_device_data[usbvision->dev_model].vin_reg2;
  1172. } else {
  1173. usbvision->vin_reg2_preset = 0;
  1174. }
  1175. usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
  1176. usbvision->video_inputs = usbvision_device_data[model].video_channels;
  1177. usbvision->ctl_input = 0;
  1178. usbvision->radio_freq = 87.5 * 16000;
  1179. usbvision->tv_freq = 400 * 16;
  1180. /* This should be here to make i2c clients to be able to register */
  1181. /* first switch off audio */
  1182. if (usbvision_device_data[model].audio_channels > 0)
  1183. usbvision_audio_off(usbvision);
  1184. /* and then power up the tuner */
  1185. usbvision_power_on(usbvision);
  1186. usbvision_i2c_register(usbvision);
  1187. }
  1188. /*
  1189. * usbvision_probe()
  1190. *
  1191. * This procedure queries device descriptor and accepts the interface
  1192. * if it looks like USBVISION video device
  1193. *
  1194. */
  1195. static int usbvision_probe(struct usb_interface *intf,
  1196. const struct usb_device_id *devid)
  1197. {
  1198. struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
  1199. struct usb_interface *uif;
  1200. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1201. const struct usb_host_interface *interface;
  1202. struct usb_usbvision *usbvision = NULL;
  1203. const struct usb_endpoint_descriptor *endpoint;
  1204. int model, i, ret;
  1205. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1206. dev->descriptor.idVendor,
  1207. dev->descriptor.idProduct, ifnum);
  1208. model = devid->driver_info;
  1209. if (model < 0 || model >= usbvision_device_data_size) {
  1210. PDEBUG(DBG_PROBE, "model out of bounds %d", model);
  1211. ret = -ENODEV;
  1212. goto err_usb;
  1213. }
  1214. printk(KERN_INFO "%s: %s found\n", __func__,
  1215. usbvision_device_data[model].model_string);
  1216. if (usbvision_device_data[model].interface >= 0)
  1217. interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
  1218. else if (ifnum < dev->actconfig->desc.bNumInterfaces)
  1219. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1220. else {
  1221. dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
  1222. ifnum, dev->actconfig->desc.bNumInterfaces - 1);
  1223. ret = -ENODEV;
  1224. goto err_usb;
  1225. }
  1226. if (interface->desc.bNumEndpoints < 2) {
  1227. dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
  1228. ifnum, interface->desc.bNumEndpoints);
  1229. ret = -ENODEV;
  1230. goto err_usb;
  1231. }
  1232. endpoint = &interface->endpoint[1].desc;
  1233. if (!usb_endpoint_xfer_isoc(endpoint)) {
  1234. dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
  1235. __func__, ifnum);
  1236. dev_err(&intf->dev, "%s: Endpoint attributes %d",
  1237. __func__, endpoint->bmAttributes);
  1238. ret = -ENODEV;
  1239. goto err_usb;
  1240. }
  1241. if (usb_endpoint_dir_out(endpoint)) {
  1242. dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
  1243. __func__, ifnum);
  1244. ret = -ENODEV;
  1245. goto err_usb;
  1246. }
  1247. usbvision = usbvision_alloc(dev, intf);
  1248. if (usbvision == NULL) {
  1249. dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
  1250. ret = -ENOMEM;
  1251. goto err_usb;
  1252. }
  1253. if (dev->descriptor.bNumConfigurations > 1)
  1254. usbvision->bridge_type = BRIDGE_NT1004;
  1255. else if (model == DAZZLE_DVC_90_REV_1_SECAM)
  1256. usbvision->bridge_type = BRIDGE_NT1005;
  1257. else
  1258. usbvision->bridge_type = BRIDGE_NT1003;
  1259. PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
  1260. /* compute alternate max packet sizes */
  1261. uif = dev->actconfig->interface[0];
  1262. usbvision->num_alt = uif->num_altsetting;
  1263. PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
  1264. usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
  1265. if (usbvision->alt_max_pkt_size == NULL) {
  1266. dev_err(&intf->dev, "usbvision: out of memory!\n");
  1267. ret = -ENOMEM;
  1268. goto err_pkt;
  1269. }
  1270. for (i = 0; i < usbvision->num_alt; i++) {
  1271. u16 tmp;
  1272. if (uif->altsetting[i].desc.bNumEndpoints < 2) {
  1273. ret = -ENODEV;
  1274. goto err_pkt;
  1275. }
  1276. tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1277. wMaxPacketSize);
  1278. usbvision->alt_max_pkt_size[i] =
  1279. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1280. PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
  1281. usbvision->alt_max_pkt_size[i]);
  1282. }
  1283. usbvision->nr = usbvision_nr++;
  1284. spin_lock_init(&usbvision->queue_lock);
  1285. init_waitqueue_head(&usbvision->wait_frame);
  1286. init_waitqueue_head(&usbvision->wait_stream);
  1287. usbvision->have_tuner = usbvision_device_data[model].tuner;
  1288. if (usbvision->have_tuner)
  1289. usbvision->tuner_type = usbvision_device_data[model].tuner_type;
  1290. usbvision->dev_model = model;
  1291. usbvision->remove_pending = 0;
  1292. usbvision->iface = ifnum;
  1293. usbvision->iface_alt = 0;
  1294. usbvision->video_endp = endpoint->bEndpointAddress;
  1295. usbvision->isoc_packet_size = 0;
  1296. usbvision->usb_bandwidth = 0;
  1297. usbvision->user = 0;
  1298. usbvision->streaming = stream_off;
  1299. usbvision_configure_video(usbvision);
  1300. usbvision_register_video(usbvision);
  1301. usbvision_create_sysfs(&usbvision->vdev);
  1302. PDEBUG(DBG_PROBE, "success");
  1303. return 0;
  1304. err_pkt:
  1305. usbvision_release(usbvision);
  1306. err_usb:
  1307. usb_put_dev(dev);
  1308. return ret;
  1309. }
  1310. /*
  1311. * usbvision_disconnect()
  1312. *
  1313. * This procedure stops all driver activity, deallocates interface-private
  1314. * structure (pointed by 'ptr') and after that driver should be removable
  1315. * with no ill consequences.
  1316. *
  1317. */
  1318. static void usbvision_disconnect(struct usb_interface *intf)
  1319. {
  1320. struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
  1321. PDEBUG(DBG_PROBE, "");
  1322. if (usbvision == NULL) {
  1323. pr_err("%s: usb_get_intfdata() failed\n", __func__);
  1324. return;
  1325. }
  1326. mutex_lock(&usbvision->v4l2_lock);
  1327. /* At this time we ask to cancel outstanding URBs */
  1328. usbvision_stop_isoc(usbvision);
  1329. v4l2_device_disconnect(&usbvision->v4l2_dev);
  1330. usbvision_i2c_unregister(usbvision);
  1331. usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
  1332. usb_put_dev(usbvision->dev);
  1333. usbvision->dev = NULL; /* USB device is no more */
  1334. mutex_unlock(&usbvision->v4l2_lock);
  1335. if (usbvision->user) {
  1336. printk(KERN_INFO "%s: In use, disconnect pending\n",
  1337. __func__);
  1338. wake_up_interruptible(&usbvision->wait_frame);
  1339. wake_up_interruptible(&usbvision->wait_stream);
  1340. } else {
  1341. usbvision_release(usbvision);
  1342. }
  1343. PDEBUG(DBG_PROBE, "success");
  1344. }
  1345. static struct usb_driver usbvision_driver = {
  1346. .name = "usbvision",
  1347. .id_table = usbvision_table,
  1348. .probe = usbvision_probe,
  1349. .disconnect = usbvision_disconnect,
  1350. };
  1351. /*
  1352. * usbvision_init()
  1353. *
  1354. * This code is run to initialize the driver.
  1355. *
  1356. */
  1357. static int __init usbvision_init(void)
  1358. {
  1359. int err_code;
  1360. PDEBUG(DBG_PROBE, "");
  1361. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1362. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1363. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1364. /* disable planar mode support unless compression enabled */
  1365. if (isoc_mode != ISOC_MODE_COMPRESS) {
  1366. /* FIXME : not the right way to set supported flag */
  1367. usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
  1368. usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
  1369. }
  1370. err_code = usb_register(&usbvision_driver);
  1371. if (err_code == 0) {
  1372. printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
  1373. PDEBUG(DBG_PROBE, "success");
  1374. }
  1375. return err_code;
  1376. }
  1377. static void __exit usbvision_exit(void)
  1378. {
  1379. PDEBUG(DBG_PROBE, "");
  1380. usb_deregister(&usbvision_driver);
  1381. PDEBUG(DBG_PROBE, "success");
  1382. }
  1383. module_init(usbvision_init);
  1384. module_exit(usbvision_exit);