usbvision-video.c 50 KB

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