usbvision-video.c 48 KB

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