usbvision-video.c 46 KB

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