usbvision-video.c 48 KB

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