usbvision-video.c 50 KB

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