usbvision-video.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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 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 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_dbg_register *reg)
  425. {
  426. struct usb_usbvision *usbvision = video_drvdata(file);
  427. int errCode;
  428. if (!v4l2_chip_match_host(&reg->match))
  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. reg->size = 1;
  439. return 0;
  440. }
  441. static int vidioc_s_register (struct file *file, void *priv,
  442. struct v4l2_dbg_register *reg)
  443. {
  444. struct usb_usbvision *usbvision = video_drvdata(file);
  445. int errCode;
  446. if (!v4l2_chip_match_host(&reg->match))
  447. return -EINVAL;
  448. /* NT100x has a 8-bit register space */
  449. errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
  450. if (errCode < 0) {
  451. err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
  452. __func__, errCode);
  453. return errCode;
  454. }
  455. return 0;
  456. }
  457. #endif
  458. static int vidioc_querycap (struct file *file, void *priv,
  459. struct v4l2_capability *vc)
  460. {
  461. struct usb_usbvision *usbvision = video_drvdata(file);
  462. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  463. strlcpy(vc->card,
  464. usbvision_device_data[usbvision->DevModel].ModelString,
  465. sizeof(vc->card));
  466. strlcpy(vc->bus_info, dev_name(&usbvision->dev->dev),
  467. sizeof(vc->bus_info));
  468. vc->version = USBVISION_DRIVER_VERSION;
  469. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  470. V4L2_CAP_AUDIO |
  471. V4L2_CAP_READWRITE |
  472. V4L2_CAP_STREAMING |
  473. (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  474. return 0;
  475. }
  476. static int vidioc_enum_input (struct file *file, void *priv,
  477. struct v4l2_input *vi)
  478. {
  479. struct usb_usbvision *usbvision = video_drvdata(file);
  480. int chan;
  481. if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
  482. return -EINVAL;
  483. if (usbvision->have_tuner) {
  484. chan = vi->index;
  485. } else {
  486. chan = vi->index + 1; /*skip Television string*/
  487. }
  488. /* Determine the requested input characteristics
  489. specific for each usbvision card model */
  490. switch(chan) {
  491. case 0:
  492. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  493. strcpy(vi->name, "White Video Input");
  494. } else {
  495. strcpy(vi->name, "Television");
  496. vi->type = V4L2_INPUT_TYPE_TUNER;
  497. vi->audioset = 1;
  498. vi->tuner = chan;
  499. vi->std = USBVISION_NORMS;
  500. }
  501. break;
  502. case 1:
  503. vi->type = V4L2_INPUT_TYPE_CAMERA;
  504. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  505. strcpy(vi->name, "Green Video Input");
  506. } else {
  507. strcpy(vi->name, "Composite Video Input");
  508. }
  509. vi->std = V4L2_STD_PAL;
  510. break;
  511. case 2:
  512. vi->type = V4L2_INPUT_TYPE_CAMERA;
  513. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  514. strcpy(vi->name, "Yellow Video Input");
  515. } else {
  516. strcpy(vi->name, "S-Video Input");
  517. }
  518. vi->std = V4L2_STD_PAL;
  519. break;
  520. case 3:
  521. vi->type = V4L2_INPUT_TYPE_CAMERA;
  522. strcpy(vi->name, "Red Video Input");
  523. vi->std = V4L2_STD_PAL;
  524. break;
  525. }
  526. return 0;
  527. }
  528. static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
  529. {
  530. struct usb_usbvision *usbvision = video_drvdata(file);
  531. *input = usbvision->ctl_input;
  532. return 0;
  533. }
  534. static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
  535. {
  536. struct usb_usbvision *usbvision = video_drvdata(file);
  537. if ((input >= usbvision->video_inputs) || (input < 0) )
  538. return -EINVAL;
  539. mutex_lock(&usbvision->lock);
  540. usbvision_muxsel(usbvision, input);
  541. usbvision_set_input(usbvision);
  542. usbvision_set_output(usbvision,
  543. usbvision->curwidth,
  544. usbvision->curheight);
  545. mutex_unlock(&usbvision->lock);
  546. return 0;
  547. }
  548. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
  549. {
  550. struct usb_usbvision *usbvision = video_drvdata(file);
  551. usbvision->tvnormId=*id;
  552. mutex_lock(&usbvision->lock);
  553. call_i2c_clients(usbvision, VIDIOC_S_STD,
  554. &usbvision->tvnormId);
  555. mutex_unlock(&usbvision->lock);
  556. /* propagate the change to the decoder */
  557. usbvision_muxsel(usbvision, usbvision->ctl_input);
  558. return 0;
  559. }
  560. static int vidioc_g_tuner (struct file *file, void *priv,
  561. struct v4l2_tuner *vt)
  562. {
  563. struct usb_usbvision *usbvision = video_drvdata(file);
  564. if (!usbvision->have_tuner || vt->index) // Only tuner 0
  565. return -EINVAL;
  566. if(usbvision->radio) {
  567. strcpy(vt->name, "Radio");
  568. vt->type = V4L2_TUNER_RADIO;
  569. } else {
  570. strcpy(vt->name, "Television");
  571. }
  572. /* Let clients fill in the remainder of this struct */
  573. call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
  574. return 0;
  575. }
  576. static int vidioc_s_tuner (struct file *file, void *priv,
  577. struct v4l2_tuner *vt)
  578. {
  579. struct usb_usbvision *usbvision = video_drvdata(file);
  580. // Only no or one tuner for now
  581. if (!usbvision->have_tuner || vt->index)
  582. return -EINVAL;
  583. /* let clients handle this */
  584. call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
  585. return 0;
  586. }
  587. static int vidioc_g_frequency (struct file *file, void *priv,
  588. struct v4l2_frequency *freq)
  589. {
  590. struct usb_usbvision *usbvision = video_drvdata(file);
  591. freq->tuner = 0; // Only one tuner
  592. if(usbvision->radio) {
  593. freq->type = V4L2_TUNER_RADIO;
  594. } else {
  595. freq->type = V4L2_TUNER_ANALOG_TV;
  596. }
  597. freq->frequency = usbvision->freq;
  598. return 0;
  599. }
  600. static int vidioc_s_frequency (struct file *file, void *priv,
  601. struct v4l2_frequency *freq)
  602. {
  603. struct usb_usbvision *usbvision = video_drvdata(file);
  604. // Only no or one tuner for now
  605. if (!usbvision->have_tuner || freq->tuner)
  606. return -EINVAL;
  607. usbvision->freq = freq->frequency;
  608. call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
  609. return 0;
  610. }
  611. static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  612. {
  613. struct usb_usbvision *usbvision = video_drvdata(file);
  614. memset(a,0,sizeof(*a));
  615. if(usbvision->radio) {
  616. strcpy(a->name,"Radio");
  617. } else {
  618. strcpy(a->name, "TV");
  619. }
  620. return 0;
  621. }
  622. static int vidioc_s_audio (struct file *file, void *fh,
  623. struct v4l2_audio *a)
  624. {
  625. if(a->index) {
  626. return -EINVAL;
  627. }
  628. return 0;
  629. }
  630. static int vidioc_queryctrl (struct file *file, void *priv,
  631. struct v4l2_queryctrl *ctrl)
  632. {
  633. struct usb_usbvision *usbvision = video_drvdata(file);
  634. int id=ctrl->id;
  635. memset(ctrl,0,sizeof(*ctrl));
  636. ctrl->id=id;
  637. call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
  638. if (!ctrl->type)
  639. return -EINVAL;
  640. return 0;
  641. }
  642. static int vidioc_g_ctrl (struct file *file, void *priv,
  643. struct v4l2_control *ctrl)
  644. {
  645. struct usb_usbvision *usbvision = video_drvdata(file);
  646. call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
  647. return 0;
  648. }
  649. static int vidioc_s_ctrl (struct file *file, void *priv,
  650. struct v4l2_control *ctrl)
  651. {
  652. struct usb_usbvision *usbvision = video_drvdata(file);
  653. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  654. return 0;
  655. }
  656. static int vidioc_reqbufs (struct file *file,
  657. void *priv, struct v4l2_requestbuffers *vr)
  658. {
  659. struct usb_usbvision *usbvision = video_drvdata(file);
  660. int ret;
  661. RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
  662. /* Check input validity:
  663. the user must do a VIDEO CAPTURE and MMAP method. */
  664. if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  665. (vr->memory != V4L2_MEMORY_MMAP))
  666. return -EINVAL;
  667. if(usbvision->streaming == Stream_On) {
  668. if ((ret = usbvision_stream_interrupt(usbvision)))
  669. return ret;
  670. }
  671. usbvision_frames_free(usbvision);
  672. usbvision_empty_framequeues(usbvision);
  673. vr->count = usbvision_frames_alloc(usbvision,vr->count);
  674. usbvision->curFrame = NULL;
  675. return 0;
  676. }
  677. static int vidioc_querybuf (struct file *file,
  678. void *priv, struct v4l2_buffer *vb)
  679. {
  680. struct usb_usbvision *usbvision = video_drvdata(file);
  681. struct usbvision_frame *frame;
  682. /* FIXME : must control
  683. that buffers are mapped (VIDIOC_REQBUFS has been called) */
  684. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  685. return -EINVAL;
  686. }
  687. if(vb->index>=usbvision->num_frames) {
  688. return -EINVAL;
  689. }
  690. /* Updating the corresponding frame state */
  691. vb->flags = 0;
  692. frame = &usbvision->frame[vb->index];
  693. if(frame->grabstate >= FrameState_Ready)
  694. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  695. if(frame->grabstate >= FrameState_Done)
  696. vb->flags |= V4L2_BUF_FLAG_DONE;
  697. if(frame->grabstate == FrameState_Unused)
  698. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  699. vb->memory = V4L2_MEMORY_MMAP;
  700. vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
  701. vb->memory = V4L2_MEMORY_MMAP;
  702. vb->field = V4L2_FIELD_NONE;
  703. vb->length = usbvision->curwidth*
  704. usbvision->curheight*
  705. usbvision->palette.bytes_per_pixel;
  706. vb->timestamp = usbvision->frame[vb->index].timestamp;
  707. vb->sequence = usbvision->frame[vb->index].sequence;
  708. return 0;
  709. }
  710. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
  711. {
  712. struct usb_usbvision *usbvision = video_drvdata(file);
  713. struct usbvision_frame *frame;
  714. unsigned long lock_flags;
  715. /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
  716. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  717. return -EINVAL;
  718. }
  719. if(vb->index>=usbvision->num_frames) {
  720. return -EINVAL;
  721. }
  722. frame = &usbvision->frame[vb->index];
  723. if (frame->grabstate != FrameState_Unused) {
  724. return -EAGAIN;
  725. }
  726. /* Mark it as ready and enqueue frame */
  727. frame->grabstate = FrameState_Ready;
  728. frame->scanstate = ScanState_Scanning;
  729. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  730. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  731. /* set v4l2_format index */
  732. frame->v4l2_format = usbvision->palette;
  733. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  734. list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
  735. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  736. return 0;
  737. }
  738. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
  739. {
  740. struct usb_usbvision *usbvision = video_drvdata(file);
  741. int ret;
  742. struct usbvision_frame *f;
  743. unsigned long lock_flags;
  744. if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  745. return -EINVAL;
  746. if (list_empty(&(usbvision->outqueue))) {
  747. if (usbvision->streaming == Stream_Idle)
  748. return -EINVAL;
  749. ret = wait_event_interruptible
  750. (usbvision->wait_frame,
  751. !list_empty(&(usbvision->outqueue)));
  752. if (ret)
  753. return ret;
  754. }
  755. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  756. f = list_entry(usbvision->outqueue.next,
  757. struct usbvision_frame, frame);
  758. list_del(usbvision->outqueue.next);
  759. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  760. f->grabstate = FrameState_Unused;
  761. vb->memory = V4L2_MEMORY_MMAP;
  762. vb->flags = V4L2_BUF_FLAG_MAPPED |
  763. V4L2_BUF_FLAG_QUEUED |
  764. V4L2_BUF_FLAG_DONE;
  765. vb->index = f->index;
  766. vb->sequence = f->sequence;
  767. vb->timestamp = f->timestamp;
  768. vb->field = V4L2_FIELD_NONE;
  769. vb->bytesused = f->scanlength;
  770. return 0;
  771. }
  772. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  773. {
  774. struct usb_usbvision *usbvision = video_drvdata(file);
  775. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  776. usbvision->streaming = Stream_On;
  777. call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
  778. return 0;
  779. }
  780. static int vidioc_streamoff(struct file *file,
  781. void *priv, enum v4l2_buf_type type)
  782. {
  783. struct usb_usbvision *usbvision = video_drvdata(file);
  784. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  785. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  786. return -EINVAL;
  787. if(usbvision->streaming == Stream_On) {
  788. usbvision_stream_interrupt(usbvision);
  789. /* Stop all video streamings */
  790. call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
  791. }
  792. usbvision_empty_framequeues(usbvision);
  793. return 0;
  794. }
  795. static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
  796. struct v4l2_fmtdesc *vfd)
  797. {
  798. if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
  799. return -EINVAL;
  800. }
  801. vfd->flags = 0;
  802. vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  803. strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
  804. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  805. memset(vfd->reserved, 0, sizeof(vfd->reserved));
  806. return 0;
  807. }
  808. static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
  809. struct v4l2_format *vf)
  810. {
  811. struct usb_usbvision *usbvision = video_drvdata(file);
  812. vf->fmt.pix.width = usbvision->curwidth;
  813. vf->fmt.pix.height = usbvision->curheight;
  814. vf->fmt.pix.pixelformat = usbvision->palette.format;
  815. vf->fmt.pix.bytesperline =
  816. usbvision->curwidth*usbvision->palette.bytes_per_pixel;
  817. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
  818. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  819. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  820. return 0;
  821. }
  822. static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
  823. struct v4l2_format *vf)
  824. {
  825. struct usb_usbvision *usbvision = video_drvdata(file);
  826. int formatIdx;
  827. /* Find requested format in available ones */
  828. for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
  829. if(vf->fmt.pix.pixelformat ==
  830. usbvision_v4l2_format[formatIdx].format) {
  831. usbvision->palette = usbvision_v4l2_format[formatIdx];
  832. break;
  833. }
  834. }
  835. /* robustness */
  836. if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
  837. return -EINVAL;
  838. }
  839. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  840. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  841. vf->fmt.pix.bytesperline = vf->fmt.pix.width*
  842. usbvision->palette.bytes_per_pixel;
  843. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
  844. return 0;
  845. }
  846. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  847. struct v4l2_format *vf)
  848. {
  849. struct usb_usbvision *usbvision = video_drvdata(file);
  850. int ret;
  851. if( 0 != (ret=vidioc_try_fmt_vid_cap (file, priv, vf)) ) {
  852. return ret;
  853. }
  854. /* stop io in case it is already in progress */
  855. if(usbvision->streaming == Stream_On) {
  856. if ((ret = usbvision_stream_interrupt(usbvision)))
  857. return ret;
  858. }
  859. usbvision_frames_free(usbvision);
  860. usbvision_empty_framequeues(usbvision);
  861. usbvision->curFrame = NULL;
  862. /* by now we are committed to the new data... */
  863. mutex_lock(&usbvision->lock);
  864. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  865. mutex_unlock(&usbvision->lock);
  866. return 0;
  867. }
  868. static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
  869. size_t count, loff_t *ppos)
  870. {
  871. struct usb_usbvision *usbvision = video_drvdata(file);
  872. int noblock = file->f_flags & O_NONBLOCK;
  873. unsigned long lock_flags;
  874. int ret,i;
  875. struct usbvision_frame *frame;
  876. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
  877. (unsigned long)count, noblock);
  878. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  879. return -EFAULT;
  880. /* This entry point is compatible with the mmap routines
  881. so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
  882. to get frames or call read on the device. */
  883. if(!usbvision->num_frames) {
  884. /* First, allocate some frames to work with
  885. if this has not been done with VIDIOC_REQBUF */
  886. usbvision_frames_free(usbvision);
  887. usbvision_empty_framequeues(usbvision);
  888. usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
  889. }
  890. if(usbvision->streaming != Stream_On) {
  891. /* no stream is running, make it running ! */
  892. usbvision->streaming = Stream_On;
  893. call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
  894. }
  895. /* Then, enqueue as many frames as possible
  896. (like a user of VIDIOC_QBUF would do) */
  897. for(i=0;i<usbvision->num_frames;i++) {
  898. frame = &usbvision->frame[i];
  899. if(frame->grabstate == FrameState_Unused) {
  900. /* Mark it as ready and enqueue frame */
  901. frame->grabstate = FrameState_Ready;
  902. frame->scanstate = ScanState_Scanning;
  903. /* Accumulated in usbvision_parse_data() */
  904. frame->scanlength = 0;
  905. /* set v4l2_format index */
  906. frame->v4l2_format = usbvision->palette;
  907. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  908. list_add_tail(&frame->frame, &usbvision->inqueue);
  909. spin_unlock_irqrestore(&usbvision->queue_lock,
  910. lock_flags);
  911. }
  912. }
  913. /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
  914. if (list_empty(&(usbvision->outqueue))) {
  915. if(noblock)
  916. return -EAGAIN;
  917. ret = wait_event_interruptible
  918. (usbvision->wait_frame,
  919. !list_empty(&(usbvision->outqueue)));
  920. if (ret)
  921. return ret;
  922. }
  923. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  924. frame = list_entry(usbvision->outqueue.next,
  925. struct usbvision_frame, frame);
  926. list_del(usbvision->outqueue.next);
  927. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  928. /* An error returns an empty frame */
  929. if (frame->grabstate == FrameState_Error) {
  930. frame->bytes_read = 0;
  931. return 0;
  932. }
  933. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
  934. __func__,
  935. frame->index, frame->bytes_read, frame->scanlength);
  936. /* copy bytes to user space; we allow for partials reads */
  937. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  938. count = frame->scanlength - frame->bytes_read;
  939. if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
  940. return -EFAULT;
  941. }
  942. frame->bytes_read += count;
  943. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
  944. __func__,
  945. (unsigned long)count, frame->bytes_read);
  946. /* For now, forget the frame if it has not been read in one shot. */
  947. /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
  948. frame->bytes_read = 0;
  949. /* Mark it as available to be used again. */
  950. frame->grabstate = FrameState_Unused;
  951. /* } */
  952. return count;
  953. }
  954. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  955. {
  956. unsigned long size = vma->vm_end - vma->vm_start,
  957. start = vma->vm_start;
  958. void *pos;
  959. u32 i;
  960. struct usb_usbvision *usbvision = video_drvdata(file);
  961. PDEBUG(DBG_MMAP, "mmap");
  962. mutex_lock(&usbvision->lock);
  963. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  964. mutex_unlock(&usbvision->lock);
  965. return -EFAULT;
  966. }
  967. if (!(vma->vm_flags & VM_WRITE) ||
  968. size != PAGE_ALIGN(usbvision->max_frame_size)) {
  969. mutex_unlock(&usbvision->lock);
  970. return -EINVAL;
  971. }
  972. for (i = 0; i < usbvision->num_frames; i++) {
  973. if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
  974. vma->vm_pgoff)
  975. break;
  976. }
  977. if (i == usbvision->num_frames) {
  978. PDEBUG(DBG_MMAP,
  979. "mmap: user supplied mapping address is out of range");
  980. mutex_unlock(&usbvision->lock);
  981. return -EINVAL;
  982. }
  983. /* VM_IO is eventually going to replace PageReserved altogether */
  984. vma->vm_flags |= VM_IO;
  985. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  986. pos = usbvision->frame[i].data;
  987. while (size > 0) {
  988. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  989. PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
  990. mutex_unlock(&usbvision->lock);
  991. return -EAGAIN;
  992. }
  993. start += PAGE_SIZE;
  994. pos += PAGE_SIZE;
  995. size -= PAGE_SIZE;
  996. }
  997. mutex_unlock(&usbvision->lock);
  998. return 0;
  999. }
  1000. /*
  1001. * Here comes the stuff for radio on usbvision based devices
  1002. *
  1003. */
  1004. static int usbvision_radio_open(struct file *file)
  1005. {
  1006. struct usb_usbvision *usbvision = video_drvdata(file);
  1007. int errCode = 0;
  1008. PDEBUG(DBG_IO, "%s:", __func__);
  1009. mutex_lock(&usbvision->lock);
  1010. if (usbvision->user) {
  1011. err("%s: Someone tried to open an already opened USBVision Radio!", __func__);
  1012. errCode = -EBUSY;
  1013. }
  1014. else {
  1015. if(PowerOnAtOpen) {
  1016. usbvision_reset_powerOffTimer(usbvision);
  1017. if (usbvision->power == 0) {
  1018. usbvision_power_on(usbvision);
  1019. usbvision_i2c_register(usbvision);
  1020. }
  1021. }
  1022. /* Alternate interface 1 is is the biggest frame size */
  1023. errCode = usbvision_set_alternate(usbvision);
  1024. if (errCode < 0) {
  1025. usbvision->last_error = errCode;
  1026. errCode = -EBUSY;
  1027. goto out;
  1028. }
  1029. // If so far no errors then we shall start the radio
  1030. usbvision->radio = 1;
  1031. call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
  1032. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  1033. usbvision->user++;
  1034. }
  1035. if (errCode) {
  1036. if (PowerOnAtOpen) {
  1037. usbvision_i2c_unregister(usbvision);
  1038. usbvision_power_off(usbvision);
  1039. usbvision->initialized = 0;
  1040. }
  1041. }
  1042. out:
  1043. mutex_unlock(&usbvision->lock);
  1044. return errCode;
  1045. }
  1046. static int usbvision_radio_close(struct file *file)
  1047. {
  1048. struct usb_usbvision *usbvision = video_drvdata(file);
  1049. int errCode = 0;
  1050. PDEBUG(DBG_IO, "");
  1051. mutex_lock(&usbvision->lock);
  1052. /* Set packet size to 0 */
  1053. usbvision->ifaceAlt=0;
  1054. errCode = usb_set_interface(usbvision->dev, usbvision->iface,
  1055. usbvision->ifaceAlt);
  1056. usbvision_audio_off(usbvision);
  1057. usbvision->radio=0;
  1058. usbvision->user--;
  1059. if (PowerOnAtOpen) {
  1060. usbvision_set_powerOffTimer(usbvision);
  1061. usbvision->initialized = 0;
  1062. }
  1063. mutex_unlock(&usbvision->lock);
  1064. if (usbvision->remove_pending) {
  1065. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  1066. usbvision_release(usbvision);
  1067. }
  1068. PDEBUG(DBG_IO, "success");
  1069. return errCode;
  1070. }
  1071. /*
  1072. * Here comes the stuff for vbi on usbvision based devices
  1073. *
  1074. */
  1075. static int usbvision_vbi_open(struct file *file)
  1076. {
  1077. /* TODO */
  1078. return -ENODEV;
  1079. }
  1080. static int usbvision_vbi_close(struct file *file)
  1081. {
  1082. /* TODO */
  1083. return -ENODEV;
  1084. }
  1085. static long usbvision_do_vbi_ioctl(struct file *file,
  1086. unsigned int cmd, void *arg)
  1087. {
  1088. /* TODO */
  1089. return -ENOIOCTLCMD;
  1090. }
  1091. static long usbvision_vbi_ioctl(struct file *file,
  1092. unsigned int cmd, unsigned long arg)
  1093. {
  1094. return video_usercopy(file, cmd, arg, usbvision_do_vbi_ioctl);
  1095. }
  1096. //
  1097. // Video registration stuff
  1098. //
  1099. // Video template
  1100. static const struct v4l2_file_operations usbvision_fops = {
  1101. .owner = THIS_MODULE,
  1102. .open = usbvision_v4l2_open,
  1103. .release = usbvision_v4l2_close,
  1104. .read = usbvision_v4l2_read,
  1105. .mmap = usbvision_v4l2_mmap,
  1106. .ioctl = video_ioctl2,
  1107. /* .poll = video_poll, */
  1108. };
  1109. static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
  1110. .vidioc_querycap = vidioc_querycap,
  1111. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1112. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1113. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1114. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1115. .vidioc_reqbufs = vidioc_reqbufs,
  1116. .vidioc_querybuf = vidioc_querybuf,
  1117. .vidioc_qbuf = vidioc_qbuf,
  1118. .vidioc_dqbuf = vidioc_dqbuf,
  1119. .vidioc_s_std = vidioc_s_std,
  1120. .vidioc_enum_input = vidioc_enum_input,
  1121. .vidioc_g_input = vidioc_g_input,
  1122. .vidioc_s_input = vidioc_s_input,
  1123. .vidioc_queryctrl = vidioc_queryctrl,
  1124. .vidioc_g_audio = vidioc_g_audio,
  1125. .vidioc_s_audio = vidioc_s_audio,
  1126. .vidioc_g_ctrl = vidioc_g_ctrl,
  1127. .vidioc_s_ctrl = vidioc_s_ctrl,
  1128. .vidioc_streamon = vidioc_streamon,
  1129. .vidioc_streamoff = vidioc_streamoff,
  1130. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1131. /* .vidiocgmbuf = vidiocgmbuf, */
  1132. #endif
  1133. .vidioc_g_tuner = vidioc_g_tuner,
  1134. .vidioc_s_tuner = vidioc_s_tuner,
  1135. .vidioc_g_frequency = vidioc_g_frequency,
  1136. .vidioc_s_frequency = vidioc_s_frequency,
  1137. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1138. .vidioc_g_register = vidioc_g_register,
  1139. .vidioc_s_register = vidioc_s_register,
  1140. #endif
  1141. };
  1142. static struct video_device usbvision_video_template = {
  1143. .fops = &usbvision_fops,
  1144. .ioctl_ops = &usbvision_ioctl_ops,
  1145. .name = "usbvision-video",
  1146. .release = video_device_release,
  1147. .minor = -1,
  1148. .tvnorms = USBVISION_NORMS,
  1149. .current_norm = V4L2_STD_PAL
  1150. };
  1151. // Radio template
  1152. static const struct v4l2_file_operations usbvision_radio_fops = {
  1153. .owner = THIS_MODULE,
  1154. .open = usbvision_radio_open,
  1155. .release = usbvision_radio_close,
  1156. .ioctl = video_ioctl2,
  1157. };
  1158. static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
  1159. .vidioc_querycap = vidioc_querycap,
  1160. .vidioc_enum_input = vidioc_enum_input,
  1161. .vidioc_g_input = vidioc_g_input,
  1162. .vidioc_s_input = vidioc_s_input,
  1163. .vidioc_queryctrl = vidioc_queryctrl,
  1164. .vidioc_g_audio = vidioc_g_audio,
  1165. .vidioc_s_audio = vidioc_s_audio,
  1166. .vidioc_g_ctrl = vidioc_g_ctrl,
  1167. .vidioc_s_ctrl = vidioc_s_ctrl,
  1168. .vidioc_g_tuner = vidioc_g_tuner,
  1169. .vidioc_s_tuner = vidioc_s_tuner,
  1170. .vidioc_g_frequency = vidioc_g_frequency,
  1171. .vidioc_s_frequency = vidioc_s_frequency,
  1172. };
  1173. static struct video_device usbvision_radio_template = {
  1174. .fops = &usbvision_radio_fops,
  1175. .name = "usbvision-radio",
  1176. .release = video_device_release,
  1177. .minor = -1,
  1178. .ioctl_ops = &usbvision_radio_ioctl_ops,
  1179. .tvnorms = USBVISION_NORMS,
  1180. .current_norm = V4L2_STD_PAL
  1181. };
  1182. // vbi template
  1183. static const struct v4l2_file_operations usbvision_vbi_fops = {
  1184. .owner = THIS_MODULE,
  1185. .open = usbvision_vbi_open,
  1186. .release = usbvision_vbi_close,
  1187. .ioctl = usbvision_vbi_ioctl,
  1188. };
  1189. static struct video_device usbvision_vbi_template=
  1190. {
  1191. .fops = &usbvision_vbi_fops,
  1192. .release = video_device_release,
  1193. .name = "usbvision-vbi",
  1194. .minor = -1,
  1195. };
  1196. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  1197. struct video_device *vdev_template,
  1198. char *name)
  1199. {
  1200. struct usb_device *usb_dev = usbvision->dev;
  1201. struct video_device *vdev;
  1202. if (usb_dev == NULL) {
  1203. err("%s: usbvision->dev is not set", __func__);
  1204. return NULL;
  1205. }
  1206. vdev = video_device_alloc();
  1207. if (NULL == vdev) {
  1208. return NULL;
  1209. }
  1210. *vdev = *vdev_template;
  1211. // vdev->minor = -1;
  1212. vdev->parent = &usb_dev->dev;
  1213. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1214. video_set_drvdata(vdev, usbvision);
  1215. return vdev;
  1216. }
  1217. // unregister video4linux devices
  1218. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1219. {
  1220. // vbi Device:
  1221. if (usbvision->vbi) {
  1222. PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
  1223. usbvision->vbi->num);
  1224. if (usbvision->vbi->minor != -1) {
  1225. video_unregister_device(usbvision->vbi);
  1226. } else {
  1227. video_device_release(usbvision->vbi);
  1228. }
  1229. usbvision->vbi = NULL;
  1230. }
  1231. // Radio Device:
  1232. if (usbvision->rdev) {
  1233. PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
  1234. usbvision->rdev->num);
  1235. if (usbvision->rdev->minor != -1) {
  1236. video_unregister_device(usbvision->rdev);
  1237. } else {
  1238. video_device_release(usbvision->rdev);
  1239. }
  1240. usbvision->rdev = NULL;
  1241. }
  1242. // Video Device:
  1243. if (usbvision->vdev) {
  1244. PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
  1245. usbvision->vdev->num);
  1246. if (usbvision->vdev->minor != -1) {
  1247. video_unregister_device(usbvision->vdev);
  1248. } else {
  1249. video_device_release(usbvision->vdev);
  1250. }
  1251. usbvision->vdev = NULL;
  1252. }
  1253. }
  1254. // register video4linux devices
  1255. static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
  1256. {
  1257. // Video Device:
  1258. usbvision->vdev = usbvision_vdev_init(usbvision,
  1259. &usbvision_video_template,
  1260. "USBVision Video");
  1261. if (usbvision->vdev == NULL) {
  1262. goto err_exit;
  1263. }
  1264. if (video_register_device(usbvision->vdev,
  1265. VFL_TYPE_GRABBER,
  1266. video_nr)<0) {
  1267. goto err_exit;
  1268. }
  1269. printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
  1270. usbvision->nr, usbvision->vdev->num);
  1271. // Radio Device:
  1272. if (usbvision_device_data[usbvision->DevModel].Radio) {
  1273. // usbvision has radio
  1274. usbvision->rdev = usbvision_vdev_init(usbvision,
  1275. &usbvision_radio_template,
  1276. "USBVision Radio");
  1277. if (usbvision->rdev == NULL) {
  1278. goto err_exit;
  1279. }
  1280. if (video_register_device(usbvision->rdev,
  1281. VFL_TYPE_RADIO,
  1282. radio_nr)<0) {
  1283. goto err_exit;
  1284. }
  1285. printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
  1286. usbvision->nr, usbvision->rdev->num);
  1287. }
  1288. // vbi Device:
  1289. if (usbvision_device_data[usbvision->DevModel].vbi) {
  1290. usbvision->vbi = usbvision_vdev_init(usbvision,
  1291. &usbvision_vbi_template,
  1292. "USBVision VBI");
  1293. if (usbvision->vdev == NULL) {
  1294. goto err_exit;
  1295. }
  1296. if (video_register_device(usbvision->vbi,
  1297. VFL_TYPE_VBI,
  1298. vbi_nr)<0) {
  1299. goto err_exit;
  1300. }
  1301. printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
  1302. usbvision->nr, usbvision->vbi->num);
  1303. }
  1304. // all done
  1305. return 0;
  1306. err_exit:
  1307. err("USBVision[%d]: video_register_device() failed", usbvision->nr);
  1308. usbvision_unregister_video(usbvision);
  1309. return -1;
  1310. }
  1311. /*
  1312. * usbvision_alloc()
  1313. *
  1314. * This code allocates the struct usb_usbvision.
  1315. * It is filled with default values.
  1316. *
  1317. * Returns NULL on error, a pointer to usb_usbvision else.
  1318. *
  1319. */
  1320. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
  1321. {
  1322. struct usb_usbvision *usbvision;
  1323. if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
  1324. NULL) {
  1325. goto err_exit;
  1326. }
  1327. usbvision->dev = dev;
  1328. mutex_init(&usbvision->lock); /* available */
  1329. // prepare control urb for control messages during interrupts
  1330. usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1331. if (usbvision->ctrlUrb == NULL) {
  1332. goto err_exit;
  1333. }
  1334. init_waitqueue_head(&usbvision->ctrlUrb_wq);
  1335. usbvision_init_powerOffTimer(usbvision);
  1336. return usbvision;
  1337. err_exit:
  1338. if (usbvision && usbvision->ctrlUrb) {
  1339. usb_free_urb(usbvision->ctrlUrb);
  1340. }
  1341. if (usbvision) {
  1342. kfree(usbvision);
  1343. }
  1344. return NULL;
  1345. }
  1346. /*
  1347. * usbvision_release()
  1348. *
  1349. * This code does final release of struct usb_usbvision. This happens
  1350. * after the device is disconnected -and- all clients closed their files.
  1351. *
  1352. */
  1353. static void usbvision_release(struct usb_usbvision *usbvision)
  1354. {
  1355. PDEBUG(DBG_PROBE, "");
  1356. mutex_lock(&usbvision->lock);
  1357. usbvision_reset_powerOffTimer(usbvision);
  1358. usbvision->initialized = 0;
  1359. mutex_unlock(&usbvision->lock);
  1360. usbvision_remove_sysfs(usbvision->vdev);
  1361. usbvision_unregister_video(usbvision);
  1362. if (usbvision->ctrlUrb) {
  1363. usb_free_urb(usbvision->ctrlUrb);
  1364. }
  1365. kfree(usbvision);
  1366. PDEBUG(DBG_PROBE, "success");
  1367. }
  1368. /*********************** usb interface **********************************/
  1369. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1370. {
  1371. int model;
  1372. if (usbvision == NULL)
  1373. return;
  1374. model = usbvision->DevModel;
  1375. usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
  1376. if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
  1377. usbvision->Vin_Reg2_Preset =
  1378. usbvision_device_data[usbvision->DevModel].Vin_Reg2;
  1379. } else {
  1380. usbvision->Vin_Reg2_Preset = 0;
  1381. }
  1382. usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
  1383. usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
  1384. usbvision->ctl_input = 0;
  1385. /* This should be here to make i2c clients to be able to register */
  1386. /* first switch off audio */
  1387. usbvision_audio_off(usbvision);
  1388. if (!PowerOnAtOpen) {
  1389. /* and then power up the noisy tuner */
  1390. usbvision_power_on(usbvision);
  1391. usbvision_i2c_register(usbvision);
  1392. }
  1393. }
  1394. /*
  1395. * usbvision_probe()
  1396. *
  1397. * This procedure queries device descriptor and accepts the interface
  1398. * if it looks like USBVISION video device
  1399. *
  1400. */
  1401. static int __devinit usbvision_probe(struct usb_interface *intf,
  1402. const struct usb_device_id *devid)
  1403. {
  1404. struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
  1405. struct usb_interface *uif;
  1406. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1407. const struct usb_host_interface *interface;
  1408. struct usb_usbvision *usbvision = NULL;
  1409. const struct usb_endpoint_descriptor *endpoint;
  1410. int model,i;
  1411. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1412. dev->descriptor.idVendor,
  1413. dev->descriptor.idProduct, ifnum);
  1414. model = devid->driver_info;
  1415. if ( (model<0) || (model>=usbvision_device_data_size) ) {
  1416. PDEBUG(DBG_PROBE, "model out of bounds %d",model);
  1417. return -ENODEV;
  1418. }
  1419. printk(KERN_INFO "%s: %s found\n", __func__,
  1420. usbvision_device_data[model].ModelString);
  1421. if (usbvision_device_data[model].Interface >= 0) {
  1422. interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
  1423. } else {
  1424. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1425. }
  1426. endpoint = &interface->endpoint[1].desc;
  1427. if (usb_endpoint_type(endpoint) !=
  1428. USB_ENDPOINT_XFER_ISOC) {
  1429. err("%s: interface %d. has non-ISO endpoint!",
  1430. __func__, ifnum);
  1431. err("%s: Endpoint attributes %d",
  1432. __func__, endpoint->bmAttributes);
  1433. return -ENODEV;
  1434. }
  1435. if (usb_endpoint_dir_out(endpoint)) {
  1436. err("%s: interface %d. has ISO OUT endpoint!",
  1437. __func__, ifnum);
  1438. return -ENODEV;
  1439. }
  1440. if ((usbvision = usbvision_alloc(dev)) == NULL) {
  1441. err("%s: couldn't allocate USBVision struct", __func__);
  1442. return -ENOMEM;
  1443. }
  1444. if (dev->descriptor.bNumConfigurations > 1) {
  1445. usbvision->bridgeType = BRIDGE_NT1004;
  1446. } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
  1447. usbvision->bridgeType = BRIDGE_NT1005;
  1448. } else {
  1449. usbvision->bridgeType = BRIDGE_NT1003;
  1450. }
  1451. PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
  1452. mutex_lock(&usbvision->lock);
  1453. /* compute alternate max packet sizes */
  1454. uif = dev->actconfig->interface[0];
  1455. usbvision->num_alt=uif->num_altsetting;
  1456. PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
  1457. usbvision->alt_max_pkt_size = kmalloc(32*
  1458. usbvision->num_alt,GFP_KERNEL);
  1459. if (usbvision->alt_max_pkt_size == NULL) {
  1460. err("usbvision: out of memory!\n");
  1461. mutex_unlock(&usbvision->lock);
  1462. return -ENOMEM;
  1463. }
  1464. for (i = 0; i < usbvision->num_alt ; i++) {
  1465. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1466. wMaxPacketSize);
  1467. usbvision->alt_max_pkt_size[i] =
  1468. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1469. PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
  1470. usbvision->alt_max_pkt_size[i]);
  1471. }
  1472. usbvision->nr = usbvision_nr++;
  1473. usbvision->have_tuner = usbvision_device_data[model].Tuner;
  1474. if (usbvision->have_tuner) {
  1475. usbvision->tuner_type = usbvision_device_data[model].TunerType;
  1476. }
  1477. usbvision->tuner_addr = ADDR_UNSET;
  1478. usbvision->DevModel = model;
  1479. usbvision->remove_pending = 0;
  1480. usbvision->iface = ifnum;
  1481. usbvision->ifaceAlt = 0;
  1482. usbvision->video_endp = endpoint->bEndpointAddress;
  1483. usbvision->isocPacketSize = 0;
  1484. usbvision->usb_bandwidth = 0;
  1485. usbvision->user = 0;
  1486. usbvision->streaming = Stream_Off;
  1487. usbvision_register_video(usbvision);
  1488. usbvision_configure_video(usbvision);
  1489. mutex_unlock(&usbvision->lock);
  1490. usb_set_intfdata (intf, usbvision);
  1491. usbvision_create_sysfs(usbvision->vdev);
  1492. PDEBUG(DBG_PROBE, "success");
  1493. return 0;
  1494. }
  1495. /*
  1496. * usbvision_disconnect()
  1497. *
  1498. * This procedure stops all driver activity, deallocates interface-private
  1499. * structure (pointed by 'ptr') and after that driver should be removable
  1500. * with no ill consequences.
  1501. *
  1502. */
  1503. static void __devexit usbvision_disconnect(struct usb_interface *intf)
  1504. {
  1505. struct usb_usbvision *usbvision = usb_get_intfdata(intf);
  1506. PDEBUG(DBG_PROBE, "");
  1507. if (usbvision == NULL) {
  1508. err("%s: usb_get_intfdata() failed", __func__);
  1509. return;
  1510. }
  1511. usb_set_intfdata (intf, NULL);
  1512. mutex_lock(&usbvision->lock);
  1513. // At this time we ask to cancel outstanding URBs
  1514. usbvision_stop_isoc(usbvision);
  1515. if (usbvision->power) {
  1516. usbvision_i2c_unregister(usbvision);
  1517. usbvision_power_off(usbvision);
  1518. }
  1519. usbvision->remove_pending = 1; // Now all ISO data will be ignored
  1520. usb_put_dev(usbvision->dev);
  1521. usbvision->dev = NULL; // USB device is no more
  1522. mutex_unlock(&usbvision->lock);
  1523. if (usbvision->user) {
  1524. printk(KERN_INFO "%s: In use, disconnect pending\n",
  1525. __func__);
  1526. wake_up_interruptible(&usbvision->wait_frame);
  1527. wake_up_interruptible(&usbvision->wait_stream);
  1528. } else {
  1529. usbvision_release(usbvision);
  1530. }
  1531. PDEBUG(DBG_PROBE, "success");
  1532. }
  1533. static struct usb_driver usbvision_driver = {
  1534. .name = "usbvision",
  1535. .id_table = usbvision_table,
  1536. .probe = usbvision_probe,
  1537. .disconnect = usbvision_disconnect
  1538. };
  1539. /*
  1540. * usbvision_init()
  1541. *
  1542. * This code is run to initialize the driver.
  1543. *
  1544. */
  1545. static int __init usbvision_init(void)
  1546. {
  1547. int errCode;
  1548. PDEBUG(DBG_PROBE, "");
  1549. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1550. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1551. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1552. /* disable planar mode support unless compression enabled */
  1553. if (isocMode != ISOC_MODE_COMPRESS ) {
  1554. // FIXME : not the right way to set supported flag
  1555. usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
  1556. usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
  1557. }
  1558. errCode = usb_register(&usbvision_driver);
  1559. if (errCode == 0) {
  1560. printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
  1561. PDEBUG(DBG_PROBE, "success");
  1562. }
  1563. return errCode;
  1564. }
  1565. static void __exit usbvision_exit(void)
  1566. {
  1567. PDEBUG(DBG_PROBE, "");
  1568. usb_deregister(&usbvision_driver);
  1569. PDEBUG(DBG_PROBE, "success");
  1570. }
  1571. module_init(usbvision_init);
  1572. module_exit(usbvision_exit);
  1573. /*
  1574. * Overrides for Emacs so that we follow Linus's tabbing style.
  1575. * ---------------------------------------------------------------------------
  1576. * Local variables:
  1577. * c-basic-offset: 8
  1578. * End:
  1579. */