usbvision-video.c 47 KB

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