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