usbvision-video.c 47 KB

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