usbvision-video.c 46 KB

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