usbvision-video.c 50 KB

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