usbvision-video.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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. .mmap = usbvision_v4l2_mmap,
  1180. .compat_ioctl = v4l_compat_ioctl32,
  1181. };
  1182. static struct video_device usbvision_video_template = {
  1183. .owner = THIS_MODULE,
  1184. .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
  1185. .hardware = VID_HARDWARE_USBVISION,
  1186. .fops = &usbvision_fops,
  1187. .name = "usbvision-video",
  1188. .release = video_device_release,
  1189. .minor = -1,
  1190. .vidioc_querycap = vidioc_querycap,
  1191. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1192. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1193. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1194. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1195. .vidioc_reqbufs = vidioc_reqbufs,
  1196. .vidioc_querybuf = vidioc_querybuf,
  1197. .vidioc_qbuf = vidioc_qbuf,
  1198. .vidioc_dqbuf = vidioc_dqbuf,
  1199. .vidioc_s_std = vidioc_s_std,
  1200. .vidioc_enum_input = vidioc_enum_input,
  1201. .vidioc_g_input = vidioc_g_input,
  1202. .vidioc_s_input = vidioc_s_input,
  1203. .vidioc_queryctrl = vidioc_queryctrl,
  1204. .vidioc_g_audio = vidioc_g_audio,
  1205. .vidioc_g_audio = vidioc_s_audio,
  1206. .vidioc_g_ctrl = vidioc_g_ctrl,
  1207. .vidioc_s_ctrl = vidioc_s_ctrl,
  1208. .vidioc_streamon = vidioc_streamon,
  1209. .vidioc_streamoff = vidioc_streamoff,
  1210. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1211. /* .vidiocgmbuf = vidiocgmbuf, */
  1212. #endif
  1213. .vidioc_g_tuner = vidioc_g_tuner,
  1214. .vidioc_s_tuner = vidioc_s_tuner,
  1215. .vidioc_g_frequency = vidioc_g_frequency,
  1216. .vidioc_s_frequency = vidioc_s_frequency,
  1217. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1218. .vidioc_g_register = vidioc_g_register,
  1219. .vidioc_s_register = vidioc_s_register,
  1220. #endif
  1221. .tvnorms = USBVISION_NORMS,
  1222. .current_norm = V4L2_STD_PAL
  1223. };
  1224. // Radio template
  1225. static const struct file_operations usbvision_radio_fops = {
  1226. .owner = THIS_MODULE,
  1227. .open = usbvision_radio_open,
  1228. .release = usbvision_radio_close,
  1229. .ioctl = video_ioctl2,
  1230. .llseek = no_llseek,
  1231. .compat_ioctl = v4l_compat_ioctl32,
  1232. };
  1233. static struct video_device usbvision_radio_template=
  1234. {
  1235. .owner = THIS_MODULE,
  1236. .type = VID_TYPE_TUNER,
  1237. .hardware = VID_HARDWARE_USBVISION,
  1238. .fops = &usbvision_radio_fops,
  1239. .name = "usbvision-radio",
  1240. .release = video_device_release,
  1241. .minor = -1,
  1242. .vidioc_querycap = vidioc_querycap,
  1243. .vidioc_enum_input = vidioc_enum_input,
  1244. .vidioc_g_input = vidioc_g_input,
  1245. .vidioc_s_input = vidioc_s_input,
  1246. .vidioc_queryctrl = vidioc_queryctrl,
  1247. .vidioc_g_audio = vidioc_g_audio,
  1248. .vidioc_g_audio = vidioc_s_audio,
  1249. .vidioc_g_ctrl = vidioc_g_ctrl,
  1250. .vidioc_s_ctrl = vidioc_s_ctrl,
  1251. .vidioc_g_tuner = vidioc_g_tuner,
  1252. .vidioc_s_tuner = vidioc_s_tuner,
  1253. .vidioc_g_frequency = vidioc_g_frequency,
  1254. .vidioc_s_frequency = vidioc_s_frequency,
  1255. .tvnorms = USBVISION_NORMS,
  1256. .current_norm = V4L2_STD_PAL
  1257. };
  1258. // vbi template
  1259. static const struct file_operations usbvision_vbi_fops = {
  1260. .owner = THIS_MODULE,
  1261. .open = usbvision_vbi_open,
  1262. .release = usbvision_vbi_close,
  1263. .ioctl = usbvision_vbi_ioctl,
  1264. .llseek = no_llseek,
  1265. .compat_ioctl = v4l_compat_ioctl32,
  1266. };
  1267. static struct video_device usbvision_vbi_template=
  1268. {
  1269. .owner = THIS_MODULE,
  1270. .type = VID_TYPE_TUNER,
  1271. .hardware = VID_HARDWARE_USBVISION,
  1272. .fops = &usbvision_vbi_fops,
  1273. .release = video_device_release,
  1274. .name = "usbvision-vbi",
  1275. .minor = -1,
  1276. };
  1277. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  1278. struct video_device *vdev_template,
  1279. char *name)
  1280. {
  1281. struct usb_device *usb_dev = usbvision->dev;
  1282. struct video_device *vdev;
  1283. if (usb_dev == NULL) {
  1284. err("%s: usbvision->dev is not set", __FUNCTION__);
  1285. return NULL;
  1286. }
  1287. vdev = video_device_alloc();
  1288. if (NULL == vdev) {
  1289. return NULL;
  1290. }
  1291. *vdev = *vdev_template;
  1292. // vdev->minor = -1;
  1293. vdev->dev = &usb_dev->dev;
  1294. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1295. video_set_drvdata(vdev, usbvision);
  1296. return vdev;
  1297. }
  1298. // unregister video4linux devices
  1299. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1300. {
  1301. // vbi Device:
  1302. if (usbvision->vbi) {
  1303. PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
  1304. usbvision->vbi->minor & 0x1f);
  1305. if (usbvision->vbi->minor != -1) {
  1306. video_unregister_device(usbvision->vbi);
  1307. } else {
  1308. video_device_release(usbvision->vbi);
  1309. }
  1310. usbvision->vbi = NULL;
  1311. }
  1312. // Radio Device:
  1313. if (usbvision->rdev) {
  1314. PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
  1315. usbvision->rdev->minor & 0x1f);
  1316. if (usbvision->rdev->minor != -1) {
  1317. video_unregister_device(usbvision->rdev);
  1318. } else {
  1319. video_device_release(usbvision->rdev);
  1320. }
  1321. usbvision->rdev = NULL;
  1322. }
  1323. // Video Device:
  1324. if (usbvision->vdev) {
  1325. PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
  1326. usbvision->vdev->minor & 0x1f);
  1327. if (usbvision->vdev->minor != -1) {
  1328. video_unregister_device(usbvision->vdev);
  1329. } else {
  1330. video_device_release(usbvision->vdev);
  1331. }
  1332. usbvision->vdev = NULL;
  1333. }
  1334. }
  1335. // register video4linux devices
  1336. static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
  1337. {
  1338. // Video Device:
  1339. usbvision->vdev = usbvision_vdev_init(usbvision,
  1340. &usbvision_video_template,
  1341. "USBVision Video");
  1342. if (usbvision->vdev == NULL) {
  1343. goto err_exit;
  1344. }
  1345. if (video_register_device(usbvision->vdev,
  1346. VFL_TYPE_GRABBER,
  1347. video_nr)<0) {
  1348. goto err_exit;
  1349. }
  1350. printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
  1351. usbvision->nr,usbvision->vdev->minor & 0x1f);
  1352. // Radio Device:
  1353. if (usbvision_device_data[usbvision->DevModel].Radio) {
  1354. // usbvision has radio
  1355. usbvision->rdev = usbvision_vdev_init(usbvision,
  1356. &usbvision_radio_template,
  1357. "USBVision Radio");
  1358. if (usbvision->rdev == NULL) {
  1359. goto err_exit;
  1360. }
  1361. if (video_register_device(usbvision->rdev,
  1362. VFL_TYPE_RADIO,
  1363. radio_nr)<0) {
  1364. goto err_exit;
  1365. }
  1366. printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
  1367. usbvision->nr, usbvision->rdev->minor & 0x1f);
  1368. }
  1369. // vbi Device:
  1370. if (usbvision_device_data[usbvision->DevModel].vbi) {
  1371. usbvision->vbi = usbvision_vdev_init(usbvision,
  1372. &usbvision_vbi_template,
  1373. "USBVision VBI");
  1374. if (usbvision->vdev == NULL) {
  1375. goto err_exit;
  1376. }
  1377. if (video_register_device(usbvision->vbi,
  1378. VFL_TYPE_VBI,
  1379. vbi_nr)<0) {
  1380. goto err_exit;
  1381. }
  1382. printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
  1383. usbvision->nr,usbvision->vbi->minor & 0x1f);
  1384. }
  1385. // all done
  1386. return 0;
  1387. err_exit:
  1388. err("USBVision[%d]: video_register_device() failed", usbvision->nr);
  1389. usbvision_unregister_video(usbvision);
  1390. return -1;
  1391. }
  1392. /*
  1393. * usbvision_alloc()
  1394. *
  1395. * This code allocates the struct usb_usbvision.
  1396. * It is filled with default values.
  1397. *
  1398. * Returns NULL on error, a pointer to usb_usbvision else.
  1399. *
  1400. */
  1401. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
  1402. {
  1403. struct usb_usbvision *usbvision;
  1404. if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
  1405. NULL) {
  1406. goto err_exit;
  1407. }
  1408. usbvision->dev = dev;
  1409. init_MUTEX(&usbvision->lock); /* to 1 == available */
  1410. // prepare control urb for control messages during interrupts
  1411. usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1412. if (usbvision->ctrlUrb == NULL) {
  1413. goto err_exit;
  1414. }
  1415. init_waitqueue_head(&usbvision->ctrlUrb_wq);
  1416. init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
  1417. usbvision_init_powerOffTimer(usbvision);
  1418. return usbvision;
  1419. err_exit:
  1420. if (usbvision && usbvision->ctrlUrb) {
  1421. usb_free_urb(usbvision->ctrlUrb);
  1422. }
  1423. if (usbvision) {
  1424. kfree(usbvision);
  1425. }
  1426. return NULL;
  1427. }
  1428. /*
  1429. * usbvision_release()
  1430. *
  1431. * This code does final release of struct usb_usbvision. This happens
  1432. * after the device is disconnected -and- all clients closed their files.
  1433. *
  1434. */
  1435. static void usbvision_release(struct usb_usbvision *usbvision)
  1436. {
  1437. PDEBUG(DBG_PROBE, "");
  1438. down(&usbvision->lock);
  1439. usbvision_reset_powerOffTimer(usbvision);
  1440. usbvision->initialized = 0;
  1441. up(&usbvision->lock);
  1442. usbvision_remove_sysfs(usbvision->vdev);
  1443. usbvision_unregister_video(usbvision);
  1444. if (usbvision->ctrlUrb) {
  1445. usb_free_urb(usbvision->ctrlUrb);
  1446. }
  1447. kfree(usbvision);
  1448. PDEBUG(DBG_PROBE, "success");
  1449. }
  1450. /*********************** usb interface **********************************/
  1451. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1452. {
  1453. int model;
  1454. if (usbvision == NULL)
  1455. return;
  1456. model = usbvision->DevModel;
  1457. usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
  1458. if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
  1459. usbvision->Vin_Reg2_Preset =
  1460. usbvision_device_data[usbvision->DevModel].Vin_Reg2;
  1461. } else {
  1462. usbvision->Vin_Reg2_Preset = 0;
  1463. }
  1464. usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
  1465. usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
  1466. usbvision->ctl_input = 0;
  1467. /* This should be here to make i2c clients to be able to register */
  1468. /* first switch off audio */
  1469. usbvision_audio_off(usbvision);
  1470. if (!PowerOnAtOpen) {
  1471. /* and then power up the noisy tuner */
  1472. usbvision_power_on(usbvision);
  1473. usbvision_i2c_register(usbvision);
  1474. }
  1475. }
  1476. /*
  1477. * usbvision_probe()
  1478. *
  1479. * This procedure queries device descriptor and accepts the interface
  1480. * if it looks like USBVISION video device
  1481. *
  1482. */
  1483. static int __devinit usbvision_probe(struct usb_interface *intf,
  1484. const struct usb_device_id *devid)
  1485. {
  1486. struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
  1487. struct usb_interface *uif;
  1488. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1489. const struct usb_host_interface *interface;
  1490. struct usb_usbvision *usbvision = NULL;
  1491. const struct usb_endpoint_descriptor *endpoint;
  1492. int model,i;
  1493. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1494. dev->descriptor.idVendor,
  1495. dev->descriptor.idProduct, ifnum);
  1496. model = devid->driver_info;
  1497. if ( (model<0) || (model>=usbvision_device_data_size) ) {
  1498. PDEBUG(DBG_PROBE, "model out of bounds %d",model);
  1499. return -ENODEV;
  1500. }
  1501. printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
  1502. usbvision_device_data[model].ModelString);
  1503. if (usbvision_device_data[model].Interface >= 0) {
  1504. interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
  1505. } else {
  1506. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1507. }
  1508. endpoint = &interface->endpoint[1].desc;
  1509. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1510. USB_ENDPOINT_XFER_ISOC) {
  1511. err("%s: interface %d. has non-ISO endpoint!",
  1512. __FUNCTION__, ifnum);
  1513. err("%s: Endpoint attributes %d",
  1514. __FUNCTION__, endpoint->bmAttributes);
  1515. return -ENODEV;
  1516. }
  1517. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1518. USB_DIR_OUT) {
  1519. err("%s: interface %d. has ISO OUT endpoint!",
  1520. __FUNCTION__, ifnum);
  1521. return -ENODEV;
  1522. }
  1523. if ((usbvision = usbvision_alloc(dev)) == NULL) {
  1524. err("%s: couldn't allocate USBVision struct", __FUNCTION__);
  1525. return -ENOMEM;
  1526. }
  1527. if (dev->descriptor.bNumConfigurations > 1) {
  1528. usbvision->bridgeType = BRIDGE_NT1004;
  1529. } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
  1530. usbvision->bridgeType = BRIDGE_NT1005;
  1531. } else {
  1532. usbvision->bridgeType = BRIDGE_NT1003;
  1533. }
  1534. PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
  1535. down(&usbvision->lock);
  1536. /* compute alternate max packet sizes */
  1537. uif = dev->actconfig->interface[0];
  1538. usbvision->num_alt=uif->num_altsetting;
  1539. PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
  1540. usbvision->alt_max_pkt_size = kmalloc(32*
  1541. usbvision->num_alt,GFP_KERNEL);
  1542. if (usbvision->alt_max_pkt_size == NULL) {
  1543. err("usbvision: out of memory!\n");
  1544. return -ENOMEM;
  1545. }
  1546. for (i = 0; i < usbvision->num_alt ; i++) {
  1547. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1548. wMaxPacketSize);
  1549. usbvision->alt_max_pkt_size[i] =
  1550. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1551. PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
  1552. usbvision->alt_max_pkt_size[i]);
  1553. }
  1554. usbvision->nr = usbvision_nr++;
  1555. usbvision->have_tuner = usbvision_device_data[model].Tuner;
  1556. if (usbvision->have_tuner) {
  1557. usbvision->tuner_type = usbvision_device_data[model].TunerType;
  1558. }
  1559. usbvision->tuner_addr = ADDR_UNSET;
  1560. usbvision->DevModel = model;
  1561. usbvision->remove_pending = 0;
  1562. usbvision->iface = ifnum;
  1563. usbvision->ifaceAlt = 0;
  1564. usbvision->video_endp = endpoint->bEndpointAddress;
  1565. usbvision->isocPacketSize = 0;
  1566. usbvision->usb_bandwidth = 0;
  1567. usbvision->user = 0;
  1568. usbvision->streaming = Stream_Off;
  1569. usbvision_register_video(usbvision);
  1570. usbvision_configure_video(usbvision);
  1571. up(&usbvision->lock);
  1572. usb_set_intfdata (intf, usbvision);
  1573. usbvision_create_sysfs(usbvision->vdev);
  1574. PDEBUG(DBG_PROBE, "success");
  1575. return 0;
  1576. }
  1577. /*
  1578. * usbvision_disconnect()
  1579. *
  1580. * This procedure stops all driver activity, deallocates interface-private
  1581. * structure (pointed by 'ptr') and after that driver should be removable
  1582. * with no ill consequences.
  1583. *
  1584. */
  1585. static void __devexit usbvision_disconnect(struct usb_interface *intf)
  1586. {
  1587. struct usb_usbvision *usbvision = usb_get_intfdata(intf);
  1588. PDEBUG(DBG_PROBE, "");
  1589. if (usbvision == NULL) {
  1590. err("%s: usb_get_intfdata() failed", __FUNCTION__);
  1591. return;
  1592. }
  1593. usb_set_intfdata (intf, NULL);
  1594. down(&usbvision->lock);
  1595. // At this time we ask to cancel outstanding URBs
  1596. usbvision_stop_isoc(usbvision);
  1597. if (usbvision->power) {
  1598. usbvision_i2c_unregister(usbvision);
  1599. usbvision_power_off(usbvision);
  1600. }
  1601. usbvision->remove_pending = 1; // Now all ISO data will be ignored
  1602. usb_put_dev(usbvision->dev);
  1603. usbvision->dev = NULL; // USB device is no more
  1604. up(&usbvision->lock);
  1605. if (usbvision->user) {
  1606. printk(KERN_INFO "%s: In use, disconnect pending\n",
  1607. __FUNCTION__);
  1608. wake_up_interruptible(&usbvision->wait_frame);
  1609. wake_up_interruptible(&usbvision->wait_stream);
  1610. } else {
  1611. usbvision_release(usbvision);
  1612. }
  1613. PDEBUG(DBG_PROBE, "success");
  1614. }
  1615. static struct usb_driver usbvision_driver = {
  1616. .name = "usbvision",
  1617. .id_table = usbvision_table,
  1618. .probe = usbvision_probe,
  1619. .disconnect = usbvision_disconnect
  1620. };
  1621. /*
  1622. * usbvision_init()
  1623. *
  1624. * This code is run to initialize the driver.
  1625. *
  1626. */
  1627. static int __init usbvision_init(void)
  1628. {
  1629. int errCode;
  1630. PDEBUG(DBG_PROBE, "");
  1631. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1632. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1633. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1634. /* disable planar mode support unless compression enabled */
  1635. if (isocMode != ISOC_MODE_COMPRESS ) {
  1636. // FIXME : not the right way to set supported flag
  1637. usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
  1638. usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
  1639. }
  1640. errCode = usb_register(&usbvision_driver);
  1641. if (errCode == 0) {
  1642. printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
  1643. PDEBUG(DBG_PROBE, "success");
  1644. }
  1645. return errCode;
  1646. }
  1647. static void __exit usbvision_exit(void)
  1648. {
  1649. PDEBUG(DBG_PROBE, "");
  1650. usb_deregister(&usbvision_driver);
  1651. PDEBUG(DBG_PROBE, "success");
  1652. }
  1653. module_init(usbvision_init);
  1654. module_exit(usbvision_exit);
  1655. /*
  1656. * Overrides for Emacs so that we follow Linus's tabbing style.
  1657. * ---------------------------------------------------------------------------
  1658. * Local variables:
  1659. * c-basic-offset: 8
  1660. * End:
  1661. */