usbvision-video.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. }
  397. mutex_unlock(&usbvision->v4l2_lock);
  398. PDEBUG(DBG_IO, "success");
  399. return 0;
  400. }
  401. /*
  402. * usbvision_ioctl()
  403. *
  404. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  405. *
  406. */
  407. #ifdef CONFIG_VIDEO_ADV_DEBUG
  408. static int vidioc_g_register(struct file *file, void *priv,
  409. struct v4l2_dbg_register *reg)
  410. {
  411. struct usb_usbvision *usbvision = video_drvdata(file);
  412. int err_code;
  413. /* NT100x has a 8-bit register space */
  414. err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
  415. if (err_code < 0) {
  416. dev_err(&usbvision->vdev->dev,
  417. "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
  418. __func__, err_code);
  419. return err_code;
  420. }
  421. reg->val = err_code;
  422. reg->size = 1;
  423. return 0;
  424. }
  425. static int vidioc_s_register(struct file *file, void *priv,
  426. const struct v4l2_dbg_register *reg)
  427. {
  428. struct usb_usbvision *usbvision = video_drvdata(file);
  429. int err_code;
  430. /* NT100x has a 8-bit register space */
  431. err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
  432. if (err_code < 0) {
  433. dev_err(&usbvision->vdev->dev,
  434. "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
  435. __func__, err_code);
  436. return err_code;
  437. }
  438. return 0;
  439. }
  440. #endif
  441. static int vidioc_querycap(struct file *file, void *priv,
  442. struct v4l2_capability *vc)
  443. {
  444. struct usb_usbvision *usbvision = video_drvdata(file);
  445. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  446. strlcpy(vc->card,
  447. usbvision_device_data[usbvision->dev_model].model_string,
  448. sizeof(vc->card));
  449. usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
  450. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  451. V4L2_CAP_AUDIO |
  452. V4L2_CAP_READWRITE |
  453. V4L2_CAP_STREAMING |
  454. (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  455. return 0;
  456. }
  457. static int vidioc_enum_input(struct file *file, void *priv,
  458. struct v4l2_input *vi)
  459. {
  460. struct usb_usbvision *usbvision = video_drvdata(file);
  461. int chan;
  462. if (vi->index >= usbvision->video_inputs)
  463. return -EINVAL;
  464. if (usbvision->have_tuner)
  465. chan = vi->index;
  466. else
  467. chan = vi->index + 1; /* skip Television string*/
  468. /* Determine the requested input characteristics
  469. specific for each usbvision card model */
  470. switch (chan) {
  471. case 0:
  472. if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
  473. strcpy(vi->name, "White Video Input");
  474. } else {
  475. strcpy(vi->name, "Television");
  476. vi->type = V4L2_INPUT_TYPE_TUNER;
  477. vi->audioset = 1;
  478. vi->tuner = chan;
  479. vi->std = USBVISION_NORMS;
  480. }
  481. break;
  482. case 1:
  483. vi->type = V4L2_INPUT_TYPE_CAMERA;
  484. if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
  485. strcpy(vi->name, "Green Video Input");
  486. else
  487. strcpy(vi->name, "Composite Video Input");
  488. vi->std = V4L2_STD_PAL;
  489. break;
  490. case 2:
  491. vi->type = V4L2_INPUT_TYPE_CAMERA;
  492. if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
  493. strcpy(vi->name, "Yellow Video Input");
  494. else
  495. strcpy(vi->name, "S-Video Input");
  496. vi->std = V4L2_STD_PAL;
  497. break;
  498. case 3:
  499. vi->type = V4L2_INPUT_TYPE_CAMERA;
  500. strcpy(vi->name, "Red Video Input");
  501. vi->std = V4L2_STD_PAL;
  502. break;
  503. }
  504. return 0;
  505. }
  506. static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
  507. {
  508. struct usb_usbvision *usbvision = video_drvdata(file);
  509. *input = usbvision->ctl_input;
  510. return 0;
  511. }
  512. static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
  513. {
  514. struct usb_usbvision *usbvision = video_drvdata(file);
  515. if (input >= usbvision->video_inputs)
  516. return -EINVAL;
  517. usbvision_muxsel(usbvision, input);
  518. usbvision_set_input(usbvision);
  519. usbvision_set_output(usbvision,
  520. usbvision->curwidth,
  521. usbvision->curheight);
  522. return 0;
  523. }
  524. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  525. {
  526. struct usb_usbvision *usbvision = video_drvdata(file);
  527. usbvision->tvnorm_id = id;
  528. call_all(usbvision, core, s_std, usbvision->tvnorm_id);
  529. /* propagate the change to the decoder */
  530. usbvision_muxsel(usbvision, usbvision->ctl_input);
  531. return 0;
  532. }
  533. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  534. {
  535. struct usb_usbvision *usbvision = video_drvdata(file);
  536. *id = usbvision->tvnorm_id;
  537. return 0;
  538. }
  539. static int vidioc_g_tuner(struct file *file, void *priv,
  540. struct v4l2_tuner *vt)
  541. {
  542. struct usb_usbvision *usbvision = video_drvdata(file);
  543. if (!usbvision->have_tuner || vt->index) /* Only tuner 0 */
  544. return -EINVAL;
  545. if (usbvision->radio) {
  546. strcpy(vt->name, "Radio");
  547. vt->type = V4L2_TUNER_RADIO;
  548. } else {
  549. strcpy(vt->name, "Television");
  550. }
  551. /* Let clients fill in the remainder of this struct */
  552. call_all(usbvision, tuner, g_tuner, vt);
  553. return 0;
  554. }
  555. static int vidioc_s_tuner(struct file *file, void *priv,
  556. const struct v4l2_tuner *vt)
  557. {
  558. struct usb_usbvision *usbvision = video_drvdata(file);
  559. /* Only no or one tuner for now */
  560. if (!usbvision->have_tuner || vt->index)
  561. return -EINVAL;
  562. /* let clients handle this */
  563. call_all(usbvision, tuner, s_tuner, vt);
  564. return 0;
  565. }
  566. static int vidioc_g_frequency(struct file *file, void *priv,
  567. struct v4l2_frequency *freq)
  568. {
  569. struct usb_usbvision *usbvision = video_drvdata(file);
  570. freq->tuner = 0; /* Only one tuner */
  571. if (usbvision->radio)
  572. freq->type = V4L2_TUNER_RADIO;
  573. else
  574. freq->type = V4L2_TUNER_ANALOG_TV;
  575. freq->frequency = usbvision->freq;
  576. return 0;
  577. }
  578. static int vidioc_s_frequency(struct file *file, void *priv,
  579. const struct v4l2_frequency *freq)
  580. {
  581. struct usb_usbvision *usbvision = video_drvdata(file);
  582. /* Only no or one tuner for now */
  583. if (!usbvision->have_tuner || freq->tuner)
  584. return -EINVAL;
  585. usbvision->freq = freq->frequency;
  586. call_all(usbvision, tuner, s_frequency, freq);
  587. return 0;
  588. }
  589. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  590. {
  591. struct usb_usbvision *usbvision = video_drvdata(file);
  592. if (usbvision->radio)
  593. strcpy(a->name, "Radio");
  594. else
  595. strcpy(a->name, "TV");
  596. return 0;
  597. }
  598. static int vidioc_s_audio(struct file *file, void *fh,
  599. const struct v4l2_audio *a)
  600. {
  601. if (a->index)
  602. return -EINVAL;
  603. return 0;
  604. }
  605. static int vidioc_queryctrl(struct file *file, void *priv,
  606. struct v4l2_queryctrl *ctrl)
  607. {
  608. struct usb_usbvision *usbvision = video_drvdata(file);
  609. call_all(usbvision, core, queryctrl, ctrl);
  610. if (!ctrl->type)
  611. return -EINVAL;
  612. return 0;
  613. }
  614. static int vidioc_g_ctrl(struct file *file, void *priv,
  615. struct v4l2_control *ctrl)
  616. {
  617. struct usb_usbvision *usbvision = video_drvdata(file);
  618. call_all(usbvision, core, g_ctrl, ctrl);
  619. return 0;
  620. }
  621. static int vidioc_s_ctrl(struct file *file, void *priv,
  622. struct v4l2_control *ctrl)
  623. {
  624. struct usb_usbvision *usbvision = video_drvdata(file);
  625. call_all(usbvision, core, s_ctrl, ctrl);
  626. return 0;
  627. }
  628. static int vidioc_reqbufs(struct file *file,
  629. void *priv, struct v4l2_requestbuffers *vr)
  630. {
  631. struct usb_usbvision *usbvision = video_drvdata(file);
  632. int ret;
  633. RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
  634. /* Check input validity:
  635. the user must do a VIDEO CAPTURE and MMAP method. */
  636. if (vr->memory != V4L2_MEMORY_MMAP)
  637. return -EINVAL;
  638. if (usbvision->streaming == stream_on) {
  639. ret = usbvision_stream_interrupt(usbvision);
  640. if (ret)
  641. return ret;
  642. }
  643. usbvision_frames_free(usbvision);
  644. usbvision_empty_framequeues(usbvision);
  645. vr->count = usbvision_frames_alloc(usbvision, vr->count);
  646. usbvision->cur_frame = NULL;
  647. return 0;
  648. }
  649. static int vidioc_querybuf(struct file *file,
  650. void *priv, struct v4l2_buffer *vb)
  651. {
  652. struct usb_usbvision *usbvision = video_drvdata(file);
  653. struct usbvision_frame *frame;
  654. /* FIXME : must control
  655. that buffers are mapped (VIDIOC_REQBUFS has been called) */
  656. if (vb->index >= usbvision->num_frames)
  657. return -EINVAL;
  658. /* Updating the corresponding frame state */
  659. vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  660. frame = &usbvision->frame[vb->index];
  661. if (frame->grabstate >= frame_state_ready)
  662. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  663. if (frame->grabstate >= frame_state_done)
  664. vb->flags |= V4L2_BUF_FLAG_DONE;
  665. if (frame->grabstate == frame_state_unused)
  666. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  667. vb->memory = V4L2_MEMORY_MMAP;
  668. vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
  669. vb->memory = V4L2_MEMORY_MMAP;
  670. vb->field = V4L2_FIELD_NONE;
  671. vb->length = usbvision->curwidth *
  672. usbvision->curheight *
  673. usbvision->palette.bytes_per_pixel;
  674. vb->timestamp = usbvision->frame[vb->index].timestamp;
  675. vb->sequence = usbvision->frame[vb->index].sequence;
  676. return 0;
  677. }
  678. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
  679. {
  680. struct usb_usbvision *usbvision = video_drvdata(file);
  681. struct usbvision_frame *frame;
  682. unsigned long lock_flags;
  683. /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
  684. if (vb->index >= usbvision->num_frames)
  685. return -EINVAL;
  686. frame = &usbvision->frame[vb->index];
  687. if (frame->grabstate != frame_state_unused)
  688. return -EAGAIN;
  689. /* Mark it as ready and enqueue frame */
  690. frame->grabstate = frame_state_ready;
  691. frame->scanstate = scan_state_scanning;
  692. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  693. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  694. /* set v4l2_format index */
  695. frame->v4l2_format = usbvision->palette;
  696. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  697. list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
  698. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  699. return 0;
  700. }
  701. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
  702. {
  703. struct usb_usbvision *usbvision = video_drvdata(file);
  704. int ret;
  705. struct usbvision_frame *f;
  706. unsigned long lock_flags;
  707. if (list_empty(&(usbvision->outqueue))) {
  708. if (usbvision->streaming == stream_idle)
  709. return -EINVAL;
  710. ret = wait_event_interruptible
  711. (usbvision->wait_frame,
  712. !list_empty(&(usbvision->outqueue)));
  713. if (ret)
  714. return ret;
  715. }
  716. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  717. f = list_entry(usbvision->outqueue.next,
  718. struct usbvision_frame, frame);
  719. list_del(usbvision->outqueue.next);
  720. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  721. f->grabstate = frame_state_unused;
  722. vb->memory = V4L2_MEMORY_MMAP;
  723. vb->flags = V4L2_BUF_FLAG_MAPPED |
  724. V4L2_BUF_FLAG_QUEUED |
  725. V4L2_BUF_FLAG_DONE |
  726. V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  727. vb->index = f->index;
  728. vb->sequence = f->sequence;
  729. vb->timestamp = f->timestamp;
  730. vb->field = V4L2_FIELD_NONE;
  731. vb->bytesused = f->scanlength;
  732. return 0;
  733. }
  734. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  735. {
  736. struct usb_usbvision *usbvision = video_drvdata(file);
  737. usbvision->streaming = stream_on;
  738. call_all(usbvision, video, s_stream, 1);
  739. return 0;
  740. }
  741. static int vidioc_streamoff(struct file *file,
  742. void *priv, enum v4l2_buf_type type)
  743. {
  744. struct usb_usbvision *usbvision = video_drvdata(file);
  745. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  746. return -EINVAL;
  747. if (usbvision->streaming == stream_on) {
  748. usbvision_stream_interrupt(usbvision);
  749. /* Stop all video streamings */
  750. call_all(usbvision, video, s_stream, 0);
  751. }
  752. usbvision_empty_framequeues(usbvision);
  753. return 0;
  754. }
  755. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  756. struct v4l2_fmtdesc *vfd)
  757. {
  758. if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
  759. return -EINVAL;
  760. strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
  761. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  762. return 0;
  763. }
  764. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  765. struct v4l2_format *vf)
  766. {
  767. struct usb_usbvision *usbvision = video_drvdata(file);
  768. vf->fmt.pix.width = usbvision->curwidth;
  769. vf->fmt.pix.height = usbvision->curheight;
  770. vf->fmt.pix.pixelformat = usbvision->palette.format;
  771. vf->fmt.pix.bytesperline =
  772. usbvision->curwidth * usbvision->palette.bytes_per_pixel;
  773. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
  774. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  775. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  776. return 0;
  777. }
  778. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  779. struct v4l2_format *vf)
  780. {
  781. struct usb_usbvision *usbvision = video_drvdata(file);
  782. int format_idx;
  783. /* Find requested format in available ones */
  784. for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
  785. if (vf->fmt.pix.pixelformat ==
  786. usbvision_v4l2_format[format_idx].format) {
  787. usbvision->palette = usbvision_v4l2_format[format_idx];
  788. break;
  789. }
  790. }
  791. /* robustness */
  792. if (format_idx == USBVISION_SUPPORTED_PALETTES)
  793. return -EINVAL;
  794. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  795. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  796. vf->fmt.pix.bytesperline = vf->fmt.pix.width*
  797. usbvision->palette.bytes_per_pixel;
  798. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
  799. return 0;
  800. }
  801. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  802. struct v4l2_format *vf)
  803. {
  804. struct usb_usbvision *usbvision = video_drvdata(file);
  805. int ret;
  806. ret = vidioc_try_fmt_vid_cap(file, priv, vf);
  807. if (ret)
  808. return ret;
  809. /* stop io in case it is already in progress */
  810. if (usbvision->streaming == stream_on) {
  811. ret = usbvision_stream_interrupt(usbvision);
  812. if (ret)
  813. return ret;
  814. }
  815. usbvision_frames_free(usbvision);
  816. usbvision_empty_framequeues(usbvision);
  817. usbvision->cur_frame = NULL;
  818. /* by now we are committed to the new data... */
  819. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  820. return 0;
  821. }
  822. static ssize_t usbvision_read(struct file *file, char __user *buf,
  823. size_t count, loff_t *ppos)
  824. {
  825. struct usb_usbvision *usbvision = video_drvdata(file);
  826. int noblock = file->f_flags & O_NONBLOCK;
  827. unsigned long lock_flags;
  828. int ret, i;
  829. struct usbvision_frame *frame;
  830. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
  831. (unsigned long)count, noblock);
  832. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  833. return -EFAULT;
  834. /* This entry point is compatible with the mmap routines
  835. so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
  836. to get frames or call read on the device. */
  837. if (!usbvision->num_frames) {
  838. /* First, allocate some frames to work with
  839. if this has not been done with VIDIOC_REQBUF */
  840. usbvision_frames_free(usbvision);
  841. usbvision_empty_framequeues(usbvision);
  842. usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
  843. }
  844. if (usbvision->streaming != stream_on) {
  845. /* no stream is running, make it running ! */
  846. usbvision->streaming = stream_on;
  847. call_all(usbvision, video, s_stream, 1);
  848. }
  849. /* Then, enqueue as many frames as possible
  850. (like a user of VIDIOC_QBUF would do) */
  851. for (i = 0; i < usbvision->num_frames; i++) {
  852. frame = &usbvision->frame[i];
  853. if (frame->grabstate == frame_state_unused) {
  854. /* Mark it as ready and enqueue frame */
  855. frame->grabstate = frame_state_ready;
  856. frame->scanstate = scan_state_scanning;
  857. /* Accumulated in usbvision_parse_data() */
  858. frame->scanlength = 0;
  859. /* set v4l2_format index */
  860. frame->v4l2_format = usbvision->palette;
  861. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  862. list_add_tail(&frame->frame, &usbvision->inqueue);
  863. spin_unlock_irqrestore(&usbvision->queue_lock,
  864. lock_flags);
  865. }
  866. }
  867. /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
  868. if (list_empty(&(usbvision->outqueue))) {
  869. if (noblock)
  870. return -EAGAIN;
  871. ret = wait_event_interruptible
  872. (usbvision->wait_frame,
  873. !list_empty(&(usbvision->outqueue)));
  874. if (ret)
  875. return ret;
  876. }
  877. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  878. frame = list_entry(usbvision->outqueue.next,
  879. struct usbvision_frame, frame);
  880. list_del(usbvision->outqueue.next);
  881. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  882. /* An error returns an empty frame */
  883. if (frame->grabstate == frame_state_error) {
  884. frame->bytes_read = 0;
  885. return 0;
  886. }
  887. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
  888. __func__,
  889. frame->index, frame->bytes_read, frame->scanlength);
  890. /* copy bytes to user space; we allow for partials reads */
  891. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  892. count = frame->scanlength - frame->bytes_read;
  893. if (copy_to_user(buf, frame->data + frame->bytes_read, count))
  894. return -EFAULT;
  895. frame->bytes_read += count;
  896. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
  897. __func__,
  898. (unsigned long)count, frame->bytes_read);
  899. /* For now, forget the frame if it has not been read in one shot. */
  900. /* if (frame->bytes_read >= frame->scanlength) {*/ /* All data has been read */
  901. frame->bytes_read = 0;
  902. /* Mark it as available to be used again. */
  903. frame->grabstate = frame_state_unused;
  904. /* } */
  905. return count;
  906. }
  907. static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
  908. size_t count, loff_t *ppos)
  909. {
  910. struct usb_usbvision *usbvision = video_drvdata(file);
  911. int res;
  912. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  913. return -ERESTARTSYS;
  914. res = usbvision_read(file, buf, count, ppos);
  915. mutex_unlock(&usbvision->v4l2_lock);
  916. return res;
  917. }
  918. static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
  919. {
  920. unsigned long size = vma->vm_end - vma->vm_start,
  921. start = vma->vm_start;
  922. void *pos;
  923. u32 i;
  924. struct usb_usbvision *usbvision = video_drvdata(file);
  925. PDEBUG(DBG_MMAP, "mmap");
  926. if (!USBVISION_IS_OPERATIONAL(usbvision))
  927. return -EFAULT;
  928. if (!(vma->vm_flags & VM_WRITE) ||
  929. size != PAGE_ALIGN(usbvision->max_frame_size)) {
  930. return -EINVAL;
  931. }
  932. for (i = 0; i < usbvision->num_frames; i++) {
  933. if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
  934. vma->vm_pgoff)
  935. break;
  936. }
  937. if (i == usbvision->num_frames) {
  938. PDEBUG(DBG_MMAP,
  939. "mmap: user supplied mapping address is out of range");
  940. return -EINVAL;
  941. }
  942. /* VM_IO is eventually going to replace PageReserved altogether */
  943. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  944. pos = usbvision->frame[i].data;
  945. while (size > 0) {
  946. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  947. PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
  948. return -EAGAIN;
  949. }
  950. start += PAGE_SIZE;
  951. pos += PAGE_SIZE;
  952. size -= PAGE_SIZE;
  953. }
  954. return 0;
  955. }
  956. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  957. {
  958. struct usb_usbvision *usbvision = video_drvdata(file);
  959. int res;
  960. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  961. return -ERESTARTSYS;
  962. res = usbvision_mmap(file, vma);
  963. mutex_unlock(&usbvision->v4l2_lock);
  964. return res;
  965. }
  966. /*
  967. * Here comes the stuff for radio on usbvision based devices
  968. *
  969. */
  970. static int usbvision_radio_open(struct file *file)
  971. {
  972. struct usb_usbvision *usbvision = video_drvdata(file);
  973. int err_code = 0;
  974. PDEBUG(DBG_IO, "%s:", __func__);
  975. if (mutex_lock_interruptible(&usbvision->v4l2_lock))
  976. return -ERESTARTSYS;
  977. if (usbvision->user) {
  978. dev_err(&usbvision->rdev->dev,
  979. "%s: Someone tried to open an already opened USBVision Radio!\n",
  980. __func__);
  981. err_code = -EBUSY;
  982. } else {
  983. if (power_on_at_open) {
  984. usbvision_reset_power_off_timer(usbvision);
  985. if (usbvision->power == 0) {
  986. usbvision_power_on(usbvision);
  987. usbvision_i2c_register(usbvision);
  988. }
  989. }
  990. /* Alternate interface 1 is is the biggest frame size */
  991. err_code = usbvision_set_alternate(usbvision);
  992. if (err_code < 0) {
  993. usbvision->last_error = err_code;
  994. err_code = -EBUSY;
  995. goto out;
  996. }
  997. /* If so far no errors then we shall start the radio */
  998. usbvision->radio = 1;
  999. call_all(usbvision, tuner, s_radio);
  1000. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  1001. usbvision->user++;
  1002. }
  1003. if (err_code) {
  1004. if (power_on_at_open) {
  1005. usbvision_i2c_unregister(usbvision);
  1006. usbvision_power_off(usbvision);
  1007. usbvision->initialized = 0;
  1008. }
  1009. }
  1010. out:
  1011. mutex_unlock(&usbvision->v4l2_lock);
  1012. return err_code;
  1013. }
  1014. static int usbvision_radio_close(struct file *file)
  1015. {
  1016. struct usb_usbvision *usbvision = video_drvdata(file);
  1017. int err_code = 0;
  1018. PDEBUG(DBG_IO, "");
  1019. mutex_lock(&usbvision->v4l2_lock);
  1020. /* Set packet size to 0 */
  1021. usbvision->iface_alt = 0;
  1022. err_code = usb_set_interface(usbvision->dev, usbvision->iface,
  1023. usbvision->iface_alt);
  1024. usbvision_audio_off(usbvision);
  1025. usbvision->radio = 0;
  1026. usbvision->user--;
  1027. if (power_on_at_open) {
  1028. usbvision_set_power_off_timer(usbvision);
  1029. usbvision->initialized = 0;
  1030. }
  1031. if (usbvision->remove_pending) {
  1032. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  1033. usbvision_release(usbvision);
  1034. }
  1035. mutex_unlock(&usbvision->v4l2_lock);
  1036. PDEBUG(DBG_IO, "success");
  1037. return err_code;
  1038. }
  1039. /* Video registration stuff */
  1040. /* Video template */
  1041. static const struct v4l2_file_operations usbvision_fops = {
  1042. .owner = THIS_MODULE,
  1043. .open = usbvision_v4l2_open,
  1044. .release = usbvision_v4l2_close,
  1045. .read = usbvision_v4l2_read,
  1046. .mmap = usbvision_v4l2_mmap,
  1047. .unlocked_ioctl = video_ioctl2,
  1048. /* .poll = video_poll, */
  1049. };
  1050. static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
  1051. .vidioc_querycap = vidioc_querycap,
  1052. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1053. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1054. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1055. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1056. .vidioc_reqbufs = vidioc_reqbufs,
  1057. .vidioc_querybuf = vidioc_querybuf,
  1058. .vidioc_qbuf = vidioc_qbuf,
  1059. .vidioc_dqbuf = vidioc_dqbuf,
  1060. .vidioc_s_std = vidioc_s_std,
  1061. .vidioc_g_std = vidioc_g_std,
  1062. .vidioc_enum_input = vidioc_enum_input,
  1063. .vidioc_g_input = vidioc_g_input,
  1064. .vidioc_s_input = vidioc_s_input,
  1065. .vidioc_queryctrl = vidioc_queryctrl,
  1066. .vidioc_g_audio = vidioc_g_audio,
  1067. .vidioc_s_audio = vidioc_s_audio,
  1068. .vidioc_g_ctrl = vidioc_g_ctrl,
  1069. .vidioc_s_ctrl = vidioc_s_ctrl,
  1070. .vidioc_streamon = vidioc_streamon,
  1071. .vidioc_streamoff = vidioc_streamoff,
  1072. .vidioc_g_tuner = vidioc_g_tuner,
  1073. .vidioc_s_tuner = vidioc_s_tuner,
  1074. .vidioc_g_frequency = vidioc_g_frequency,
  1075. .vidioc_s_frequency = vidioc_s_frequency,
  1076. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1077. .vidioc_g_register = vidioc_g_register,
  1078. .vidioc_s_register = vidioc_s_register,
  1079. #endif
  1080. };
  1081. static struct video_device usbvision_video_template = {
  1082. .fops = &usbvision_fops,
  1083. .ioctl_ops = &usbvision_ioctl_ops,
  1084. .name = "usbvision-video",
  1085. .release = video_device_release,
  1086. .tvnorms = USBVISION_NORMS,
  1087. };
  1088. /* Radio template */
  1089. static const struct v4l2_file_operations usbvision_radio_fops = {
  1090. .owner = THIS_MODULE,
  1091. .open = usbvision_radio_open,
  1092. .release = usbvision_radio_close,
  1093. .unlocked_ioctl = video_ioctl2,
  1094. };
  1095. static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
  1096. .vidioc_querycap = vidioc_querycap,
  1097. .vidioc_enum_input = vidioc_enum_input,
  1098. .vidioc_g_input = vidioc_g_input,
  1099. .vidioc_s_input = vidioc_s_input,
  1100. .vidioc_queryctrl = vidioc_queryctrl,
  1101. .vidioc_g_audio = vidioc_g_audio,
  1102. .vidioc_s_audio = vidioc_s_audio,
  1103. .vidioc_g_ctrl = vidioc_g_ctrl,
  1104. .vidioc_s_ctrl = vidioc_s_ctrl,
  1105. .vidioc_g_tuner = vidioc_g_tuner,
  1106. .vidioc_s_tuner = vidioc_s_tuner,
  1107. .vidioc_g_frequency = vidioc_g_frequency,
  1108. .vidioc_s_frequency = vidioc_s_frequency,
  1109. };
  1110. static struct video_device usbvision_radio_template = {
  1111. .fops = &usbvision_radio_fops,
  1112. .name = "usbvision-radio",
  1113. .release = video_device_release,
  1114. .ioctl_ops = &usbvision_radio_ioctl_ops,
  1115. };
  1116. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  1117. struct video_device *vdev_template,
  1118. char *name)
  1119. {
  1120. struct usb_device *usb_dev = usbvision->dev;
  1121. struct video_device *vdev;
  1122. if (usb_dev == NULL) {
  1123. dev_err(&usbvision->dev->dev,
  1124. "%s: usbvision->dev is not set\n", __func__);
  1125. return NULL;
  1126. }
  1127. vdev = video_device_alloc();
  1128. if (NULL == vdev)
  1129. return NULL;
  1130. *vdev = *vdev_template;
  1131. vdev->lock = &usbvision->v4l2_lock;
  1132. vdev->v4l2_dev = &usbvision->v4l2_dev;
  1133. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1134. video_set_drvdata(vdev, usbvision);
  1135. return vdev;
  1136. }
  1137. /* unregister video4linux devices */
  1138. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1139. {
  1140. /* Radio Device: */
  1141. if (usbvision->rdev) {
  1142. PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
  1143. video_device_node_name(usbvision->rdev));
  1144. if (video_is_registered(usbvision->rdev))
  1145. video_unregister_device(usbvision->rdev);
  1146. else
  1147. video_device_release(usbvision->rdev);
  1148. usbvision->rdev = NULL;
  1149. }
  1150. /* Video Device: */
  1151. if (usbvision->vdev) {
  1152. PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
  1153. video_device_node_name(usbvision->vdev));
  1154. if (video_is_registered(usbvision->vdev))
  1155. video_unregister_device(usbvision->vdev);
  1156. else
  1157. video_device_release(usbvision->vdev);
  1158. usbvision->vdev = NULL;
  1159. }
  1160. }
  1161. /* register video4linux devices */
  1162. static int usbvision_register_video(struct usb_usbvision *usbvision)
  1163. {
  1164. /* Video Device: */
  1165. usbvision->vdev = usbvision_vdev_init(usbvision,
  1166. &usbvision_video_template,
  1167. "USBVision Video");
  1168. if (usbvision->vdev == NULL)
  1169. goto err_exit;
  1170. if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
  1171. goto err_exit;
  1172. printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
  1173. usbvision->nr, video_device_node_name(usbvision->vdev));
  1174. /* Radio Device: */
  1175. if (usbvision_device_data[usbvision->dev_model].radio) {
  1176. /* usbvision has radio */
  1177. usbvision->rdev = usbvision_vdev_init(usbvision,
  1178. &usbvision_radio_template,
  1179. "USBVision Radio");
  1180. if (usbvision->rdev == NULL)
  1181. goto err_exit;
  1182. if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
  1183. goto err_exit;
  1184. printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
  1185. usbvision->nr, video_device_node_name(usbvision->rdev));
  1186. }
  1187. /* all done */
  1188. return 0;
  1189. err_exit:
  1190. dev_err(&usbvision->dev->dev,
  1191. "USBVision[%d]: video_register_device() failed\n",
  1192. usbvision->nr);
  1193. usbvision_unregister_video(usbvision);
  1194. return -1;
  1195. }
  1196. /*
  1197. * usbvision_alloc()
  1198. *
  1199. * This code allocates the struct usb_usbvision.
  1200. * It is filled with default values.
  1201. *
  1202. * Returns NULL on error, a pointer to usb_usbvision else.
  1203. *
  1204. */
  1205. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
  1206. struct usb_interface *intf)
  1207. {
  1208. struct usb_usbvision *usbvision;
  1209. usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
  1210. if (usbvision == NULL)
  1211. return NULL;
  1212. usbvision->dev = dev;
  1213. if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
  1214. goto err_free;
  1215. mutex_init(&usbvision->v4l2_lock);
  1216. /* prepare control urb for control messages during interrupts */
  1217. usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1218. if (usbvision->ctrl_urb == NULL)
  1219. goto err_unreg;
  1220. init_waitqueue_head(&usbvision->ctrl_urb_wq);
  1221. usbvision_init_power_off_timer(usbvision);
  1222. return usbvision;
  1223. err_unreg:
  1224. v4l2_device_unregister(&usbvision->v4l2_dev);
  1225. err_free:
  1226. kfree(usbvision);
  1227. return NULL;
  1228. }
  1229. /*
  1230. * usbvision_release()
  1231. *
  1232. * This code does final release of struct usb_usbvision. This happens
  1233. * after the device is disconnected -and- all clients closed their files.
  1234. *
  1235. */
  1236. static void usbvision_release(struct usb_usbvision *usbvision)
  1237. {
  1238. PDEBUG(DBG_PROBE, "");
  1239. usbvision_reset_power_off_timer(usbvision);
  1240. usbvision->initialized = 0;
  1241. usbvision_remove_sysfs(usbvision->vdev);
  1242. usbvision_unregister_video(usbvision);
  1243. kfree(usbvision->alt_max_pkt_size);
  1244. usb_free_urb(usbvision->ctrl_urb);
  1245. v4l2_device_unregister(&usbvision->v4l2_dev);
  1246. kfree(usbvision);
  1247. PDEBUG(DBG_PROBE, "success");
  1248. }
  1249. /*********************** usb interface **********************************/
  1250. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1251. {
  1252. int model;
  1253. if (usbvision == NULL)
  1254. return;
  1255. model = usbvision->dev_model;
  1256. usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
  1257. if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
  1258. usbvision->vin_reg2_preset =
  1259. usbvision_device_data[usbvision->dev_model].vin_reg2;
  1260. } else {
  1261. usbvision->vin_reg2_preset = 0;
  1262. }
  1263. usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
  1264. usbvision->video_inputs = usbvision_device_data[model].video_channels;
  1265. usbvision->ctl_input = 0;
  1266. /* This should be here to make i2c clients to be able to register */
  1267. /* first switch off audio */
  1268. if (usbvision_device_data[model].audio_channels > 0)
  1269. usbvision_audio_off(usbvision);
  1270. if (!power_on_at_open) {
  1271. /* and then power up the noisy tuner */
  1272. usbvision_power_on(usbvision);
  1273. usbvision_i2c_register(usbvision);
  1274. }
  1275. }
  1276. /*
  1277. * usbvision_probe()
  1278. *
  1279. * This procedure queries device descriptor and accepts the interface
  1280. * if it looks like USBVISION video device
  1281. *
  1282. */
  1283. static int usbvision_probe(struct usb_interface *intf,
  1284. const struct usb_device_id *devid)
  1285. {
  1286. struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
  1287. struct usb_interface *uif;
  1288. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1289. const struct usb_host_interface *interface;
  1290. struct usb_usbvision *usbvision = NULL;
  1291. const struct usb_endpoint_descriptor *endpoint;
  1292. int model, i;
  1293. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1294. dev->descriptor.idVendor,
  1295. dev->descriptor.idProduct, ifnum);
  1296. model = devid->driver_info;
  1297. if (model < 0 || model >= usbvision_device_data_size) {
  1298. PDEBUG(DBG_PROBE, "model out of bounds %d", model);
  1299. return -ENODEV;
  1300. }
  1301. printk(KERN_INFO "%s: %s found\n", __func__,
  1302. usbvision_device_data[model].model_string);
  1303. if (usbvision_device_data[model].interface >= 0)
  1304. interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
  1305. else
  1306. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1307. endpoint = &interface->endpoint[1].desc;
  1308. if (!usb_endpoint_xfer_isoc(endpoint)) {
  1309. dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
  1310. __func__, ifnum);
  1311. dev_err(&intf->dev, "%s: Endpoint attributes %d",
  1312. __func__, endpoint->bmAttributes);
  1313. return -ENODEV;
  1314. }
  1315. if (usb_endpoint_dir_out(endpoint)) {
  1316. dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
  1317. __func__, ifnum);
  1318. return -ENODEV;
  1319. }
  1320. usbvision = usbvision_alloc(dev, intf);
  1321. if (usbvision == NULL) {
  1322. dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
  1323. return -ENOMEM;
  1324. }
  1325. if (dev->descriptor.bNumConfigurations > 1)
  1326. usbvision->bridge_type = BRIDGE_NT1004;
  1327. else if (model == DAZZLE_DVC_90_REV_1_SECAM)
  1328. usbvision->bridge_type = BRIDGE_NT1005;
  1329. else
  1330. usbvision->bridge_type = BRIDGE_NT1003;
  1331. PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
  1332. /* compute alternate max packet sizes */
  1333. uif = dev->actconfig->interface[0];
  1334. usbvision->num_alt = uif->num_altsetting;
  1335. PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
  1336. usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
  1337. if (usbvision->alt_max_pkt_size == NULL) {
  1338. dev_err(&intf->dev, "usbvision: out of memory!\n");
  1339. usbvision_release(usbvision);
  1340. return -ENOMEM;
  1341. }
  1342. for (i = 0; i < usbvision->num_alt; i++) {
  1343. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1344. wMaxPacketSize);
  1345. usbvision->alt_max_pkt_size[i] =
  1346. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1347. PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
  1348. usbvision->alt_max_pkt_size[i]);
  1349. }
  1350. usbvision->nr = usbvision_nr++;
  1351. usbvision->have_tuner = usbvision_device_data[model].tuner;
  1352. if (usbvision->have_tuner)
  1353. usbvision->tuner_type = usbvision_device_data[model].tuner_type;
  1354. usbvision->dev_model = model;
  1355. usbvision->remove_pending = 0;
  1356. usbvision->iface = ifnum;
  1357. usbvision->iface_alt = 0;
  1358. usbvision->video_endp = endpoint->bEndpointAddress;
  1359. usbvision->isoc_packet_size = 0;
  1360. usbvision->usb_bandwidth = 0;
  1361. usbvision->user = 0;
  1362. usbvision->streaming = stream_off;
  1363. usbvision_configure_video(usbvision);
  1364. usbvision_register_video(usbvision);
  1365. usbvision_create_sysfs(usbvision->vdev);
  1366. PDEBUG(DBG_PROBE, "success");
  1367. return 0;
  1368. }
  1369. /*
  1370. * usbvision_disconnect()
  1371. *
  1372. * This procedure stops all driver activity, deallocates interface-private
  1373. * structure (pointed by 'ptr') and after that driver should be removable
  1374. * with no ill consequences.
  1375. *
  1376. */
  1377. static void usbvision_disconnect(struct usb_interface *intf)
  1378. {
  1379. struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
  1380. PDEBUG(DBG_PROBE, "");
  1381. if (usbvision == NULL) {
  1382. pr_err("%s: usb_get_intfdata() failed\n", __func__);
  1383. return;
  1384. }
  1385. mutex_lock(&usbvision->v4l2_lock);
  1386. /* At this time we ask to cancel outstanding URBs */
  1387. usbvision_stop_isoc(usbvision);
  1388. v4l2_device_disconnect(&usbvision->v4l2_dev);
  1389. if (usbvision->power) {
  1390. usbvision_i2c_unregister(usbvision);
  1391. usbvision_power_off(usbvision);
  1392. }
  1393. usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
  1394. usb_put_dev(usbvision->dev);
  1395. usbvision->dev = NULL; /* USB device is no more */
  1396. mutex_unlock(&usbvision->v4l2_lock);
  1397. if (usbvision->user) {
  1398. printk(KERN_INFO "%s: In use, disconnect pending\n",
  1399. __func__);
  1400. wake_up_interruptible(&usbvision->wait_frame);
  1401. wake_up_interruptible(&usbvision->wait_stream);
  1402. } else {
  1403. usbvision_release(usbvision);
  1404. }
  1405. PDEBUG(DBG_PROBE, "success");
  1406. }
  1407. static struct usb_driver usbvision_driver = {
  1408. .name = "usbvision",
  1409. .id_table = usbvision_table,
  1410. .probe = usbvision_probe,
  1411. .disconnect = usbvision_disconnect,
  1412. };
  1413. /*
  1414. * usbvision_init()
  1415. *
  1416. * This code is run to initialize the driver.
  1417. *
  1418. */
  1419. static int __init usbvision_init(void)
  1420. {
  1421. int err_code;
  1422. PDEBUG(DBG_PROBE, "");
  1423. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1424. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1425. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1426. /* disable planar mode support unless compression enabled */
  1427. if (isoc_mode != ISOC_MODE_COMPRESS) {
  1428. /* FIXME : not the right way to set supported flag */
  1429. usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
  1430. usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
  1431. }
  1432. err_code = usb_register(&usbvision_driver);
  1433. if (err_code == 0) {
  1434. printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
  1435. PDEBUG(DBG_PROBE, "success");
  1436. }
  1437. return err_code;
  1438. }
  1439. static void __exit usbvision_exit(void)
  1440. {
  1441. PDEBUG(DBG_PROBE, "");
  1442. usb_deregister(&usbvision_driver);
  1443. PDEBUG(DBG_PROBE, "success");
  1444. }
  1445. module_init(usbvision_init);
  1446. module_exit(usbvision_exit);
  1447. /*
  1448. * Overrides for Emacs so that we follow Linus's tabbing style.
  1449. * ---------------------------------------------------------------------------
  1450. * Local variables:
  1451. * c-basic-offset: 8
  1452. * End:
  1453. */