usbvision-video.c 53 KB

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