usbvision-video.c 48 KB

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