usbvision-video.c 50 KB

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