usbvision-video.c 57 KB

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