usbvision-video.c 47 KB

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