stv680.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
  3. *
  4. * Thanks to STMicroelectronics for information on the usb commands, and
  5. * to Steve Miller at STM for his help and encouragement while I was
  6. * writing this driver.
  7. *
  8. * This driver is based heavily on the
  9. * Endpoints (formerly known as AOX) se401 USB Camera Driver
  10. * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
  11. *
  12. * Still somewhat based on the Linux ov511 driver.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  21. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * History:
  29. * ver 0.1 October, 2001. Initial attempt.
  30. *
  31. * ver 0.2 November, 2001. Fixed asbility to resize, added brightness
  32. * function, made more stable (?)
  33. *
  34. * ver 0.21 Nov, 2001. Added gamma correction and white balance,
  35. * due to Alexander Schwartz. Still trying to
  36. * improve stablility. Moved stuff into stv680.h
  37. *
  38. * ver 0.22 Nov, 2001. Added sharpen function (by Michael Sweet,
  39. * mike@easysw.com) from GIMP, also used in pencam.
  40. * Simple, fast, good integer math routine.
  41. *
  42. * ver 0.23 Dec, 2001 (gkh)
  43. * Took out sharpen function, ran code through
  44. * Lindent, and did other minor tweaks to get
  45. * things to work properly with 2.5.1
  46. *
  47. * ver 0.24 Jan, 2002 (kjs)
  48. * Fixed the problem with webcam crashing after
  49. * two pictures. Changed the way pic is halved to
  50. * improve quality. Got rid of green line around
  51. * frame. Fix brightness reset when changing size
  52. * bug. Adjusted gamma filters slightly.
  53. *
  54. * ver 0.25 Jan, 2002 (kjs)
  55. * Fixed a bug in which the driver sometimes attempted
  56. * to set to a non-supported size. This allowed
  57. * gnomemeeting to work.
  58. * Fixed proc entry removal bug.
  59. */
  60. #include <linux/module.h>
  61. #include <linux/init.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/slab.h>
  64. #include <linux/pagemap.h>
  65. #include <linux/errno.h>
  66. #include <linux/videodev.h>
  67. #include <media/v4l2-common.h>
  68. #include <media/v4l2-ioctl.h>
  69. #include <linux/usb.h>
  70. #include <linux/mutex.h>
  71. #include "stv680.h"
  72. static int video_nr = -1;
  73. static int swapRGB; /* 0 = default for auto select */
  74. /* 0 = default to allow auto select; -1 = swap never, +1 = swap always */
  75. static int swapRGB_on;
  76. static unsigned int debug;
  77. #define PDEBUG(level, fmt, args...) \
  78. do { \
  79. if (debug >= level) \
  80. printk(KERN_INFO KBUILD_MODNAME " [%s:%d] \n" fmt, \
  81. __func__, __LINE__ , ## args); \
  82. } while (0)
  83. /*
  84. * Version Information
  85. */
  86. #define DRIVER_VERSION "v0.25"
  87. #define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
  88. #define DRIVER_DESC "STV0680 USB Camera Driver"
  89. MODULE_AUTHOR (DRIVER_AUTHOR);
  90. MODULE_DESCRIPTION (DRIVER_DESC);
  91. MODULE_LICENSE ("GPL");
  92. module_param(debug, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC (debug, "Debug enabled or not");
  94. module_param(swapRGB_on, int, 0);
  95. MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
  96. module_param(video_nr, int, 0);
  97. /********************************************************************
  98. *
  99. * Memory management
  100. *
  101. * This is a shameless copy from the USB-cpia driver (linux kernel
  102. * version 2.3.29 or so, I have no idea what this code actually does ;).
  103. * Actually it seems to be a copy of a shameless copy of the bttv-driver.
  104. * Or that is a copy of a shameless copy of ... (To the powers: is there
  105. * no generic kernel-function to do this sort of stuff?)
  106. *
  107. * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
  108. * there will be one, but apparentely not yet -jerdfelt
  109. *
  110. * So I copied it again for the ov511 driver -claudio
  111. *
  112. * Same for the se401 driver -Jeroen
  113. *
  114. * And the STV0680 driver - Kevin
  115. ********************************************************************/
  116. static void *rvmalloc (unsigned long size)
  117. {
  118. void *mem;
  119. unsigned long adr;
  120. size = PAGE_ALIGN(size);
  121. mem = vmalloc_32 (size);
  122. if (!mem)
  123. return NULL;
  124. memset (mem, 0, size); /* Clear the ram out, no junk to the user */
  125. adr = (unsigned long) mem;
  126. while (size > 0) {
  127. SetPageReserved(vmalloc_to_page((void *)adr));
  128. adr += PAGE_SIZE;
  129. size -= PAGE_SIZE;
  130. }
  131. return mem;
  132. }
  133. static void rvfree (void *mem, unsigned long size)
  134. {
  135. unsigned long adr;
  136. if (!mem)
  137. return;
  138. adr = (unsigned long) mem;
  139. while ((long) size > 0) {
  140. ClearPageReserved(vmalloc_to_page((void *)adr));
  141. adr += PAGE_SIZE;
  142. size -= PAGE_SIZE;
  143. }
  144. vfree (mem);
  145. }
  146. /*********************************************************************
  147. * pencam read/write functions
  148. ********************************************************************/
  149. static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
  150. {
  151. int ret = -1;
  152. switch (set) {
  153. case 0: /* 0xc1 */
  154. ret = usb_control_msg (stv680->udev,
  155. usb_rcvctrlpipe (stv680->udev, 0),
  156. req,
  157. (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  158. value, 0, buffer, size, PENCAM_TIMEOUT);
  159. break;
  160. case 1: /* 0x41 */
  161. ret = usb_control_msg (stv680->udev,
  162. usb_sndctrlpipe (stv680->udev, 0),
  163. req,
  164. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  165. value, 0, buffer, size, PENCAM_TIMEOUT);
  166. break;
  167. case 2: /* 0x80 */
  168. ret = usb_control_msg (stv680->udev,
  169. usb_rcvctrlpipe (stv680->udev, 0),
  170. req,
  171. (USB_DIR_IN | USB_RECIP_DEVICE),
  172. value, 0, buffer, size, PENCAM_TIMEOUT);
  173. break;
  174. case 3: /* 0x40 */
  175. ret = usb_control_msg (stv680->udev,
  176. usb_sndctrlpipe (stv680->udev, 0),
  177. req,
  178. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
  179. value, 0, buffer, size, PENCAM_TIMEOUT);
  180. break;
  181. }
  182. if ((ret < 0) && (req != 0x0a)) {
  183. PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
  184. }
  185. return ret;
  186. }
  187. static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
  188. {
  189. if (configuration != dev->udev->actconfig->desc.bConfigurationValue
  190. || usb_reset_configuration (dev->udev) < 0) {
  191. PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
  192. return -1;
  193. }
  194. if (usb_set_interface (dev->udev, interface, alternate) < 0) {
  195. PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. static int stv_stop_video (struct usb_stv *dev)
  201. {
  202. int i;
  203. unsigned char *buf;
  204. buf = kmalloc (40, GFP_KERNEL);
  205. if (buf == NULL) {
  206. PDEBUG (0, "STV(e): Out of (small buf) memory");
  207. return -1;
  208. }
  209. /* this is a high priority command; it stops all lower order commands */
  210. if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
  211. i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
  212. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
  213. } else {
  214. PDEBUG (1, "STV(i): Camera reset to idle mode.");
  215. }
  216. if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
  217. PDEBUG (1, "STV(e): Reset config during exit failed");
  218. /* get current mode */
  219. buf[0] = 0xf0;
  220. if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
  221. PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
  222. if (dev->origMode != buf[0]) {
  223. memset (buf, 0, 8);
  224. buf[0] = (unsigned char) dev->origMode;
  225. if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
  226. PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
  227. i = -1;
  228. }
  229. buf[0] = 0xf0;
  230. i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
  231. if ((i != 0x08) || (buf[0] != dev->origMode)) {
  232. PDEBUG (0, "STV(e): camera NOT set to original resolution.");
  233. i = -1;
  234. } else
  235. PDEBUG (0, "STV(i): Camera set to original resolution");
  236. }
  237. /* origMode */
  238. kfree(buf);
  239. return i;
  240. }
  241. static int stv_set_video_mode (struct usb_stv *dev)
  242. {
  243. int i, stop_video = 1;
  244. unsigned char *buf;
  245. buf = kmalloc (40, GFP_KERNEL);
  246. if (buf == NULL) {
  247. PDEBUG (0, "STV(e): Out of (small buf) memory");
  248. return -1;
  249. }
  250. if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
  251. kfree(buf);
  252. return i;
  253. }
  254. i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
  255. if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
  256. PDEBUG (1, "STV(e): Could not get descriptor 0100.");
  257. goto error;
  258. }
  259. /* set alternate interface 1 */
  260. if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
  261. goto error;
  262. if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
  263. goto error;
  264. PDEBUG (1, "STV(i): Setting video mode.");
  265. /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
  266. if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
  267. stop_video = 0;
  268. goto error;
  269. }
  270. goto exit;
  271. error:
  272. kfree(buf);
  273. if (stop_video == 1)
  274. stv_stop_video (dev);
  275. return -1;
  276. exit:
  277. kfree(buf);
  278. return 0;
  279. }
  280. static int stv_init (struct usb_stv *stv680)
  281. {
  282. int i = 0;
  283. unsigned char *buffer;
  284. unsigned long int bufsize;
  285. buffer = kzalloc (40, GFP_KERNEL);
  286. if (buffer == NULL) {
  287. PDEBUG (0, "STV(e): Out of (small buf) memory");
  288. return -1;
  289. }
  290. udelay (100);
  291. /* set config 1, interface 0, alternate 0 */
  292. if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
  293. kfree(buffer);
  294. PDEBUG (0, "STV(e): set config 1,0,0 failed");
  295. return -1;
  296. }
  297. /* ping camera to be sure STV0680 is present */
  298. if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
  299. goto error;
  300. if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
  301. PDEBUG (1, "STV(e): camera ping failed!!");
  302. goto error;
  303. }
  304. /* get camera descriptor */
  305. if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
  306. goto error;
  307. i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
  308. if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
  309. PDEBUG (1, "STV(e): Could not get descriptor 0200.");
  310. goto error;
  311. }
  312. if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
  313. goto error;
  314. if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
  315. goto error;
  316. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  317. goto error;
  318. stv680->SupportedModes = buffer[7];
  319. i = stv680->SupportedModes;
  320. stv680->CIF = 0;
  321. stv680->VGA = 0;
  322. stv680->QVGA = 0;
  323. if (i & 1)
  324. stv680->CIF = 1;
  325. if (i & 2)
  326. stv680->VGA = 1;
  327. if (i & 8)
  328. stv680->QVGA = 1;
  329. if (stv680->SupportedModes == 0) {
  330. PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
  331. i = -1;
  332. goto error;
  333. } else {
  334. if (stv680->CIF)
  335. PDEBUG (0, "STV(i): CIF is supported");
  336. if (stv680->QVGA)
  337. PDEBUG (0, "STV(i): QVGA is supported");
  338. }
  339. /* FW rev, ASIC rev, sensor ID */
  340. PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
  341. PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
  342. PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
  343. /* set alternate interface 1 */
  344. if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
  345. goto error;
  346. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  347. goto error;
  348. if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
  349. goto error;
  350. i = buffer[3];
  351. PDEBUG (0, "STV(i): Camera has %i pictures.", i);
  352. /* get current mode */
  353. if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
  354. goto error;
  355. stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
  356. /* This will attemp CIF mode, if supported. If not, set to QVGA */
  357. memset (buffer, 0, 8);
  358. if (stv680->CIF)
  359. buffer[0] = 0x00;
  360. else if (stv680->QVGA)
  361. buffer[0] = 0x03;
  362. if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
  363. PDEBUG (0, "STV(i): Set_Camera_Mode failed");
  364. i = -1;
  365. goto error;
  366. }
  367. buffer[0] = 0xf0;
  368. stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
  369. if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
  370. PDEBUG (0, "STV(e): Error setting camera video mode!");
  371. i = -1;
  372. goto error;
  373. } else {
  374. if (buffer[0] == 0) {
  375. stv680->VideoMode = 0x0000;
  376. PDEBUG (0, "STV(i): Video Mode set to CIF");
  377. }
  378. if (buffer[0] == 0x03) {
  379. stv680->VideoMode = 0x0300;
  380. PDEBUG (0, "STV(i): Video Mode set to QVGA");
  381. }
  382. }
  383. if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
  384. goto error;
  385. bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
  386. stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
  387. stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
  388. stv680->origGain = buffer[12];
  389. goto exit;
  390. error:
  391. i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
  392. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
  393. kfree(buffer);
  394. return -1;
  395. exit:
  396. kfree(buffer);
  397. /* video = 320x240, 352x288 */
  398. if (stv680->CIF == 1) {
  399. stv680->maxwidth = 352;
  400. stv680->maxheight = 288;
  401. stv680->vwidth = 352;
  402. stv680->vheight = 288;
  403. }
  404. if (stv680->QVGA == 1) {
  405. stv680->maxwidth = 320;
  406. stv680->maxheight = 240;
  407. stv680->vwidth = 320;
  408. stv680->vheight = 240;
  409. }
  410. stv680->rawbufsize = bufsize; /* must be ./. by 8 */
  411. stv680->maxframesize = bufsize * 3; /* RGB size */
  412. PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
  413. PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
  414. /* some default values */
  415. stv680->bulk_in_endpointAddr = 0x82;
  416. stv680->dropped = 0;
  417. stv680->error = 0;
  418. stv680->framecount = 0;
  419. stv680->readcount = 0;
  420. stv680->streaming = 0;
  421. /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
  422. stv680->brightness = 32767;
  423. stv680->chgbright = 0;
  424. stv680->whiteness = 0; /* only for greyscale */
  425. stv680->colour = 32767;
  426. stv680->contrast = 32767;
  427. stv680->hue = 32767;
  428. stv680->palette = STV_VIDEO_PALETTE;
  429. stv680->depth = 24; /* rgb24 bits */
  430. if ((swapRGB_on == 0) && (swapRGB == 0))
  431. PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
  432. else if ((swapRGB_on == 0) && (swapRGB == 1))
  433. PDEBUG (1, "STV(i): swapRGB is (auto) ON");
  434. else if (swapRGB_on == 1)
  435. PDEBUG (1, "STV(i): swapRGB is (forced) ON");
  436. else if (swapRGB_on == -1)
  437. PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
  438. if (stv_set_video_mode (stv680) < 0) {
  439. PDEBUG (0, "STV(e): Could not set video mode in stv_init");
  440. return -1;
  441. }
  442. return 0;
  443. }
  444. /***************** last of pencam routines *******************/
  445. /****************************************************************************
  446. * sysfs
  447. ***************************************************************************/
  448. #define stv680_file(name, variable, field) \
  449. static ssize_t show_##name(struct device *class_dev, \
  450. struct device_attribute *attr, char *buf) \
  451. { \
  452. struct video_device *vdev = to_video_device(class_dev); \
  453. struct usb_stv *stv = video_get_drvdata(vdev); \
  454. return sprintf(buf, field, stv->variable); \
  455. } \
  456. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  457. stv680_file(model, camera_name, "%s\n");
  458. stv680_file(in_use, user, "%d\n");
  459. stv680_file(streaming, streaming, "%d\n");
  460. stv680_file(palette, palette, "%i\n");
  461. stv680_file(frames_total, readcount, "%d\n");
  462. stv680_file(frames_read, framecount, "%d\n");
  463. stv680_file(packets_dropped, dropped, "%d\n");
  464. stv680_file(decoding_errors, error, "%d\n");
  465. static int stv680_create_sysfs_files(struct video_device *vdev)
  466. {
  467. int rc;
  468. rc = device_create_file(&vdev->dev, &dev_attr_model);
  469. if (rc) goto err;
  470. rc = device_create_file(&vdev->dev, &dev_attr_in_use);
  471. if (rc) goto err_model;
  472. rc = device_create_file(&vdev->dev, &dev_attr_streaming);
  473. if (rc) goto err_inuse;
  474. rc = device_create_file(&vdev->dev, &dev_attr_palette);
  475. if (rc) goto err_stream;
  476. rc = device_create_file(&vdev->dev, &dev_attr_frames_total);
  477. if (rc) goto err_pal;
  478. rc = device_create_file(&vdev->dev, &dev_attr_frames_read);
  479. if (rc) goto err_framtot;
  480. rc = device_create_file(&vdev->dev, &dev_attr_packets_dropped);
  481. if (rc) goto err_framread;
  482. rc = device_create_file(&vdev->dev, &dev_attr_decoding_errors);
  483. if (rc) goto err_dropped;
  484. return 0;
  485. err_dropped:
  486. device_remove_file(&vdev->dev, &dev_attr_packets_dropped);
  487. err_framread:
  488. device_remove_file(&vdev->dev, &dev_attr_frames_read);
  489. err_framtot:
  490. device_remove_file(&vdev->dev, &dev_attr_frames_total);
  491. err_pal:
  492. device_remove_file(&vdev->dev, &dev_attr_palette);
  493. err_stream:
  494. device_remove_file(&vdev->dev, &dev_attr_streaming);
  495. err_inuse:
  496. device_remove_file(&vdev->dev, &dev_attr_in_use);
  497. err_model:
  498. device_remove_file(&vdev->dev, &dev_attr_model);
  499. err:
  500. PDEBUG(0, "STV(e): Could not create sysfs files");
  501. return rc;
  502. }
  503. static void stv680_remove_sysfs_files(struct video_device *vdev)
  504. {
  505. device_remove_file(&vdev->dev, &dev_attr_model);
  506. device_remove_file(&vdev->dev, &dev_attr_in_use);
  507. device_remove_file(&vdev->dev, &dev_attr_streaming);
  508. device_remove_file(&vdev->dev, &dev_attr_palette);
  509. device_remove_file(&vdev->dev, &dev_attr_frames_total);
  510. device_remove_file(&vdev->dev, &dev_attr_frames_read);
  511. device_remove_file(&vdev->dev, &dev_attr_packets_dropped);
  512. device_remove_file(&vdev->dev, &dev_attr_decoding_errors);
  513. }
  514. /********************************************************************
  515. * Camera control
  516. *******************************************************************/
  517. static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
  518. {
  519. /* This sets values for v4l interface. max/min = 65535/0 */
  520. p->brightness = stv680->brightness;
  521. p->whiteness = stv680->whiteness; /* greyscale */
  522. p->colour = stv680->colour;
  523. p->contrast = stv680->contrast;
  524. p->hue = stv680->hue;
  525. p->palette = stv680->palette;
  526. p->depth = stv680->depth;
  527. return 0;
  528. }
  529. static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
  530. {
  531. /* See above stv680_get_pict */
  532. if (p->palette != STV_VIDEO_PALETTE) {
  533. PDEBUG (2, "STV(e): Palette set error in _set_pic");
  534. return 1;
  535. }
  536. if (stv680->brightness != p->brightness) {
  537. stv680->chgbright = 1;
  538. stv680->brightness = p->brightness;
  539. }
  540. stv680->whiteness = p->whiteness; /* greyscale */
  541. stv680->colour = p->colour;
  542. stv680->contrast = p->contrast;
  543. stv680->hue = p->hue;
  544. stv680->palette = p->palette;
  545. stv680->depth = p->depth;
  546. return 0;
  547. }
  548. static void stv680_video_irq (struct urb *urb)
  549. {
  550. struct usb_stv *stv680 = urb->context;
  551. int length = urb->actual_length;
  552. if (length < stv680->rawbufsize)
  553. PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
  554. /* ohoh... */
  555. if (!stv680->streaming)
  556. return;
  557. if (!stv680->udev) {
  558. PDEBUG (0, "STV(e): device vapourished in video_irq");
  559. return;
  560. }
  561. /* 0 sized packets happen if we are to fast, but sometimes the camera
  562. keeps sending them forever...
  563. */
  564. if (length && !urb->status) {
  565. stv680->nullpackets = 0;
  566. switch (stv680->scratch[stv680->scratch_next].state) {
  567. case BUFFER_READY:
  568. case BUFFER_BUSY:
  569. stv680->dropped++;
  570. break;
  571. case BUFFER_UNUSED:
  572. memcpy (stv680->scratch[stv680->scratch_next].data,
  573. (unsigned char *) urb->transfer_buffer, length);
  574. stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
  575. stv680->scratch[stv680->scratch_next].length = length;
  576. if (waitqueue_active (&stv680->wq)) {
  577. wake_up_interruptible (&stv680->wq);
  578. }
  579. stv680->scratch_overflow = 0;
  580. stv680->scratch_next++;
  581. if (stv680->scratch_next >= STV680_NUMSCRATCH)
  582. stv680->scratch_next = 0;
  583. break;
  584. } /* switch */
  585. } else {
  586. stv680->nullpackets++;
  587. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  588. if (waitqueue_active (&stv680->wq)) {
  589. wake_up_interruptible (&stv680->wq);
  590. }
  591. }
  592. } /* if - else */
  593. /* Resubmit urb for new data */
  594. urb->status = 0;
  595. urb->dev = stv680->udev;
  596. if (usb_submit_urb (urb, GFP_ATOMIC))
  597. PDEBUG (0, "STV(e): urb burned down in video irq");
  598. return;
  599. } /* _video_irq */
  600. static int stv680_start_stream (struct usb_stv *stv680)
  601. {
  602. struct urb *urb;
  603. int err = 0, i;
  604. stv680->streaming = 1;
  605. /* Do some memory allocation */
  606. for (i = 0; i < STV680_NUMFRAMES; i++) {
  607. stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
  608. stv680->frame[i].curpix = 0;
  609. }
  610. /* packet size = 4096 */
  611. for (i = 0; i < STV680_NUMSBUF; i++) {
  612. stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  613. if (stv680->sbuf[i].data == NULL) {
  614. PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
  615. goto nomem_err;
  616. }
  617. }
  618. stv680->scratch_next = 0;
  619. stv680->scratch_use = 0;
  620. stv680->scratch_overflow = 0;
  621. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  622. stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  623. if (stv680->scratch[i].data == NULL) {
  624. PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
  625. goto nomem_err;
  626. }
  627. stv680->scratch[i].state = BUFFER_UNUSED;
  628. }
  629. for (i = 0; i < STV680_NUMSBUF; i++) {
  630. urb = usb_alloc_urb (0, GFP_KERNEL);
  631. if (!urb)
  632. goto nomem_err;
  633. /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
  634. usb_fill_bulk_urb (urb, stv680->udev,
  635. usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
  636. stv680->sbuf[i].data, stv680->rawbufsize,
  637. stv680_video_irq, stv680);
  638. stv680->urb[i] = urb;
  639. err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
  640. if (err) {
  641. PDEBUG (0, "STV(e): urb burned down with err "
  642. "%d in start stream %d", err, i);
  643. goto nomem_err;
  644. }
  645. } /* i STV680_NUMSBUF */
  646. stv680->framecount = 0;
  647. return 0;
  648. nomem_err:
  649. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  650. kfree(stv680->scratch[i].data);
  651. stv680->scratch[i].data = NULL;
  652. }
  653. for (i = 0; i < STV680_NUMSBUF; i++) {
  654. usb_kill_urb(stv680->urb[i]);
  655. usb_free_urb(stv680->urb[i]);
  656. stv680->urb[i] = NULL;
  657. kfree(stv680->sbuf[i].data);
  658. stv680->sbuf[i].data = NULL;
  659. }
  660. return -ENOMEM;
  661. }
  662. static int stv680_stop_stream (struct usb_stv *stv680)
  663. {
  664. int i;
  665. if (!stv680->streaming || !stv680->udev)
  666. return 1;
  667. stv680->streaming = 0;
  668. for (i = 0; i < STV680_NUMSBUF; i++)
  669. if (stv680->urb[i]) {
  670. usb_kill_urb (stv680->urb[i]);
  671. usb_free_urb (stv680->urb[i]);
  672. stv680->urb[i] = NULL;
  673. kfree(stv680->sbuf[i].data);
  674. }
  675. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  676. kfree(stv680->scratch[i].data);
  677. stv680->scratch[i].data = NULL;
  678. }
  679. return 0;
  680. }
  681. static int stv680_set_size (struct usb_stv *stv680, int width, int height)
  682. {
  683. int wasstreaming = stv680->streaming;
  684. /* Check to see if we need to change */
  685. if ((stv680->vwidth == width) && (stv680->vheight == height))
  686. return 0;
  687. PDEBUG (1, "STV(i): size request for %i x %i", width, height);
  688. /* Check for a valid mode */
  689. if ((!width || !height) || ((width & 1) || (height & 1))) {
  690. PDEBUG (1, "STV(e): set_size error: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  691. return 1;
  692. }
  693. if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
  694. width = stv680->maxwidth / 2;
  695. height = stv680->maxheight / 2;
  696. } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
  697. width = 160;
  698. height = 120;
  699. } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
  700. width = 176;
  701. height = 144;
  702. } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
  703. width = 320;
  704. height = 240;
  705. } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
  706. width = 352;
  707. height = 288;
  708. } else {
  709. PDEBUG (1, "STV(e): request for non-supported size: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  710. return 1;
  711. }
  712. /* Stop a current stream and start it again at the new size */
  713. if (wasstreaming)
  714. stv680_stop_stream (stv680);
  715. stv680->vwidth = width;
  716. stv680->vheight = height;
  717. PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
  718. if (wasstreaming)
  719. stv680_start_stream (stv680);
  720. return 0;
  721. }
  722. /**********************************************************************
  723. * Video Decoding
  724. **********************************************************************/
  725. /******* routines from the pencam program; hey, they work! ********/
  726. /*
  727. * STV0680 Vision Camera Chipset Driver
  728. * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
  729. */
  730. #define RED 0
  731. #define GREEN 1
  732. #define BLUE 2
  733. #define AD(x, y, w) (((y)*(w)+(x))*3)
  734. static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
  735. {
  736. int x, y, i;
  737. int w = stv680->cwidth;
  738. int vw = stv680->cwidth, vh = stv680->cheight;
  739. unsigned int p = 0;
  740. int colour = 0, bayer = 0;
  741. unsigned char *raw = buffer->data;
  742. struct stv680_frame *frame = &stv680->frame[stv680->curframe];
  743. unsigned char *output = frame->data;
  744. unsigned char *temp = frame->data;
  745. int offset = buffer->offset;
  746. if (frame->curpix == 0) {
  747. if (frame->grabstate == FRAME_READY) {
  748. frame->grabstate = FRAME_GRABBING;
  749. }
  750. }
  751. if (offset != frame->curpix) { /* Regard frame as lost :( */
  752. frame->curpix = 0;
  753. stv680->error++;
  754. return;
  755. }
  756. if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
  757. vw = 320;
  758. vh = 240;
  759. }
  760. if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
  761. vw = 352;
  762. vh = 288;
  763. }
  764. memset (output, 0, 3 * vw * vh); /* clear output matrix. */
  765. for (y = 0; y < vh; y++) {
  766. for (x = 0; x < vw; x++) {
  767. if (x & 1)
  768. p = *(raw + y * w + (x >> 1));
  769. else
  770. p = *(raw + y * w + (x >> 1) + (w >> 1));
  771. if (y & 1)
  772. bayer = 2;
  773. else
  774. bayer = 0;
  775. if (x & 1)
  776. bayer++;
  777. switch (bayer) {
  778. case 0:
  779. case 3:
  780. colour = 1;
  781. break;
  782. case 1:
  783. colour = 0;
  784. break;
  785. case 2:
  786. colour = 2;
  787. break;
  788. }
  789. i = (y * vw + x) * 3;
  790. *(output + i + colour) = (unsigned char) p;
  791. } /* for x */
  792. } /* for y */
  793. /****** gamma correction plus hardcoded white balance */
  794. /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
  795. Correction values red[], green[], blue[], are generated by
  796. (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
  797. White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
  798. converted to unsigned char. Values are in stv680.h */
  799. for (y = 0; y < vh; y++) {
  800. for (x = 0; x < vw; x++) {
  801. i = (y * vw + x) * 3;
  802. *(output + i) = red[*(output + i)];
  803. *(output + i + 1) = green[*(output + i + 1)];
  804. *(output + i + 2) = blue[*(output + i + 2)];
  805. }
  806. }
  807. /****** bayer demosaic ******/
  808. for (y = 1; y < (vh - 1); y++) {
  809. for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
  810. if (y & 1)
  811. bayer = 0;
  812. else
  813. bayer = 2;
  814. if (!(x & 1))
  815. bayer++;
  816. switch (bayer) {
  817. case 0: /* green. blue lr, red tb */
  818. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
  819. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
  820. break;
  821. case 1: /* blue. green lrtb, red diagonals */
  822. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  823. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
  824. break;
  825. case 2: /* red. green lrtb, blue diagonals */
  826. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  827. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
  828. break;
  829. case 3: /* green. red lr, blue tb */
  830. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
  831. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
  832. break;
  833. } /* switch */
  834. } /* for x */
  835. } /* for y - end demosaic */
  836. /* fix top and bottom row, left and right side */
  837. i = vw * 3;
  838. memcpy (output, (output + i), i);
  839. memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
  840. for (y = 0; y < vh; y++) {
  841. i = y * vw * 3;
  842. memcpy ((output + i), (output + i + 3), 3);
  843. memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
  844. }
  845. /* process all raw data, then trim to size if necessary */
  846. if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
  847. i = 0;
  848. for (y = 0; y < vh; y++) {
  849. if (!(y & 1)) {
  850. for (x = 0; x < vw; x++) {
  851. p = (y * vw + x) * 3;
  852. if (!(x & 1)) {
  853. *(output + i) = *(output + p);
  854. *(output + i + 1) = *(output + p + 1);
  855. *(output + i + 2) = *(output + p + 2);
  856. i += 3;
  857. }
  858. } /* for x */
  859. }
  860. } /* for y */
  861. }
  862. /* reset to proper width */
  863. if ((stv680->vwidth == 160)) {
  864. vw = 160;
  865. vh = 120;
  866. }
  867. if ((stv680->vwidth == 176)) {
  868. vw = 176;
  869. vh = 144;
  870. }
  871. /* output is RGB; some programs want BGR */
  872. /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
  873. /* swapRGB_on=-1, never swap */
  874. if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
  875. for (y = 0; y < vh; y++) {
  876. for (x = 0; x < vw; x++) {
  877. i = (y * vw + x) * 3;
  878. *(temp) = *(output + i);
  879. *(output + i) = *(output + i + 2);
  880. *(output + i + 2) = *(temp);
  881. }
  882. }
  883. }
  884. /* brightness */
  885. if (stv680->chgbright == 1) {
  886. if (stv680->brightness >= 32767) {
  887. p = (stv680->brightness - 32767) / 256;
  888. for (x = 0; x < (vw * vh * 3); x++) {
  889. if ((*(output + x) + (unsigned char) p) > 255)
  890. *(output + x) = 255;
  891. else
  892. *(output + x) += (unsigned char) p;
  893. } /* for */
  894. } else {
  895. p = (32767 - stv680->brightness) / 256;
  896. for (x = 0; x < (vw * vh * 3); x++) {
  897. if ((unsigned char) p > *(output + x))
  898. *(output + x) = 0;
  899. else
  900. *(output + x) -= (unsigned char) p;
  901. } /* for */
  902. } /* else */
  903. }
  904. /* if */
  905. frame->curpix = 0;
  906. frame->curlinepix = 0;
  907. frame->grabstate = FRAME_DONE;
  908. stv680->framecount++;
  909. stv680->readcount++;
  910. if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
  911. stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
  912. }
  913. } /* bayer_unshuffle */
  914. /******* end routines from the pencam program *********/
  915. static int stv680_newframe (struct usb_stv *stv680, int framenr)
  916. {
  917. int errors = 0;
  918. while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
  919. if (!stv680->frame[framenr].curpix) {
  920. errors++;
  921. }
  922. wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
  923. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  924. stv680->nullpackets = 0;
  925. PDEBUG (2, "STV(i): too many null length packets, restarting capture");
  926. stv680_stop_stream (stv680);
  927. stv680_start_stream (stv680);
  928. } else {
  929. if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
  930. stv680->frame[framenr].grabstate = FRAME_ERROR;
  931. PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
  932. return -EIO;
  933. }
  934. stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
  935. bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
  936. stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
  937. stv680->scratch_use++;
  938. if (stv680->scratch_use >= STV680_NUMSCRATCH)
  939. stv680->scratch_use = 0;
  940. if (errors > STV680_MAX_ERRORS) {
  941. errors = 0;
  942. PDEBUG (2, "STV(i): too many errors, restarting capture");
  943. stv680_stop_stream (stv680);
  944. stv680_start_stream (stv680);
  945. }
  946. } /* else */
  947. } /* while */
  948. return 0;
  949. }
  950. /*********************************************************************
  951. * Video4Linux
  952. *********************************************************************/
  953. static int stv_open(struct file *file)
  954. {
  955. struct video_device *dev = video_devdata(file);
  956. struct usb_stv *stv680 = video_get_drvdata(dev);
  957. int err = 0;
  958. /* we are called with the BKL held */
  959. lock_kernel();
  960. stv680->user = 1;
  961. err = stv_init (stv680); /* main initialization routine for camera */
  962. if (err >= 0) {
  963. stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
  964. if (!stv680->fbuf) {
  965. PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
  966. err = -ENOMEM;
  967. }
  968. file->private_data = dev;
  969. }
  970. if (err)
  971. stv680->user = 0;
  972. unlock_kernel();
  973. return err;
  974. }
  975. static int stv_close(struct file *file)
  976. {
  977. struct video_device *dev = file->private_data;
  978. struct usb_stv *stv680 = video_get_drvdata(dev);
  979. int i;
  980. for (i = 0; i < STV680_NUMFRAMES; i++)
  981. stv680->frame[i].grabstate = FRAME_UNUSED;
  982. if (stv680->streaming)
  983. stv680_stop_stream (stv680);
  984. if ((i = stv_stop_video (stv680)) < 0)
  985. PDEBUG (1, "STV(e): stop_video failed in stv_close");
  986. rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
  987. stv680->user = 0;
  988. if (stv680->removed) {
  989. kfree(stv680);
  990. stv680 = NULL;
  991. PDEBUG (0, "STV(i): device unregistered");
  992. }
  993. file->private_data = NULL;
  994. return 0;
  995. }
  996. static long stv680_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  997. {
  998. struct video_device *vdev = file->private_data;
  999. struct usb_stv *stv680 = video_get_drvdata(vdev);
  1000. if (!stv680->udev)
  1001. return -EIO;
  1002. switch (cmd) {
  1003. case VIDIOCGCAP:{
  1004. struct video_capability *b = arg;
  1005. strcpy (b->name, stv680->camera_name);
  1006. b->type = VID_TYPE_CAPTURE;
  1007. b->channels = 1;
  1008. b->audios = 0;
  1009. b->maxwidth = stv680->maxwidth;
  1010. b->maxheight = stv680->maxheight;
  1011. b->minwidth = stv680->maxwidth / 2;
  1012. b->minheight = stv680->maxheight / 2;
  1013. return 0;
  1014. }
  1015. case VIDIOCGCHAN:{
  1016. struct video_channel *v = arg;
  1017. if (v->channel != 0)
  1018. return -EINVAL;
  1019. v->flags = 0;
  1020. v->tuners = 0;
  1021. v->type = VIDEO_TYPE_CAMERA;
  1022. strcpy (v->name, "STV Camera");
  1023. return 0;
  1024. }
  1025. case VIDIOCSCHAN:{
  1026. struct video_channel *v = arg;
  1027. if (v->channel != 0)
  1028. return -EINVAL;
  1029. return 0;
  1030. }
  1031. case VIDIOCGPICT:{
  1032. struct video_picture *p = arg;
  1033. stv680_get_pict (stv680, p);
  1034. return 0;
  1035. }
  1036. case VIDIOCSPICT:{
  1037. struct video_picture *p = arg;
  1038. if (stv680_set_pict (stv680, p))
  1039. return -EINVAL;
  1040. return 0;
  1041. }
  1042. case VIDIOCSWIN:{
  1043. struct video_window *vw = arg;
  1044. if (vw->flags)
  1045. return -EINVAL;
  1046. if (vw->clipcount)
  1047. return -EINVAL;
  1048. if (vw->width != stv680->vwidth) {
  1049. if (stv680_set_size (stv680, vw->width, vw->height)) {
  1050. PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
  1051. return -EINVAL;
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. case VIDIOCGWIN:{
  1057. struct video_window *vw = arg;
  1058. vw->x = 0; /* FIXME */
  1059. vw->y = 0;
  1060. vw->chromakey = 0;
  1061. vw->flags = 0;
  1062. vw->clipcount = 0;
  1063. vw->width = stv680->vwidth;
  1064. vw->height = stv680->vheight;
  1065. return 0;
  1066. }
  1067. case VIDIOCGMBUF:{
  1068. struct video_mbuf *vm = arg;
  1069. int i;
  1070. memset (vm, 0, sizeof (*vm));
  1071. vm->size = STV680_NUMFRAMES * stv680->maxframesize;
  1072. vm->frames = STV680_NUMFRAMES;
  1073. for (i = 0; i < STV680_NUMFRAMES; i++)
  1074. vm->offsets[i] = stv680->maxframesize * i;
  1075. return 0;
  1076. }
  1077. case VIDIOCMCAPTURE:{
  1078. struct video_mmap *vm = arg;
  1079. if (vm->format != STV_VIDEO_PALETTE) {
  1080. PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
  1081. vm->format, STV_VIDEO_PALETTE);
  1082. if ((vm->format == 3) && (swapRGB_on == 0)) {
  1083. PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
  1084. /* this may fix those apps (e.g., xawtv) that want BGR */
  1085. swapRGB = 1;
  1086. }
  1087. return -EINVAL;
  1088. }
  1089. if (vm->frame >= STV680_NUMFRAMES) {
  1090. PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
  1091. return -EINVAL;
  1092. }
  1093. if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
  1094. || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
  1095. PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
  1096. stv680->frame[vm->frame].grabstate);
  1097. return -EBUSY;
  1098. }
  1099. /* Is this according to the v4l spec??? */
  1100. if (stv680->vwidth != vm->width) {
  1101. if (stv680_set_size (stv680, vm->width, vm->height)) {
  1102. PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
  1103. return -EINVAL;
  1104. }
  1105. }
  1106. stv680->frame[vm->frame].grabstate = FRAME_READY;
  1107. if (!stv680->streaming)
  1108. stv680_start_stream (stv680);
  1109. return 0;
  1110. }
  1111. case VIDIOCSYNC:{
  1112. int *frame = arg;
  1113. int ret = 0;
  1114. if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
  1115. PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
  1116. return -EINVAL;
  1117. }
  1118. ret = stv680_newframe (stv680, *frame);
  1119. stv680->frame[*frame].grabstate = FRAME_UNUSED;
  1120. return ret;
  1121. }
  1122. case VIDIOCGFBUF:{
  1123. struct video_buffer *vb = arg;
  1124. memset (vb, 0, sizeof (*vb));
  1125. return 0;
  1126. }
  1127. case VIDIOCKEY:
  1128. return 0;
  1129. case VIDIOCCAPTURE:
  1130. {
  1131. PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
  1132. return -EINVAL;
  1133. }
  1134. case VIDIOCSFBUF:
  1135. case VIDIOCGTUNER:
  1136. case VIDIOCSTUNER:
  1137. case VIDIOCGFREQ:
  1138. case VIDIOCSFREQ:
  1139. case VIDIOCGAUDIO:
  1140. case VIDIOCSAUDIO:
  1141. return -EINVAL;
  1142. default:
  1143. return -ENOIOCTLCMD;
  1144. } /* end switch */
  1145. return 0;
  1146. }
  1147. static long stv680_ioctl(struct file *file,
  1148. unsigned int cmd, unsigned long arg)
  1149. {
  1150. return video_usercopy(file, cmd, arg, stv680_do_ioctl);
  1151. }
  1152. static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
  1153. {
  1154. struct video_device *dev = file->private_data;
  1155. struct usb_stv *stv680 = video_get_drvdata(dev);
  1156. unsigned long start = vma->vm_start;
  1157. unsigned long size = vma->vm_end-vma->vm_start;
  1158. unsigned long page, pos;
  1159. mutex_lock(&stv680->lock);
  1160. if (stv680->udev == NULL) {
  1161. mutex_unlock(&stv680->lock);
  1162. return -EIO;
  1163. }
  1164. if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
  1165. & ~(PAGE_SIZE - 1))) {
  1166. mutex_unlock(&stv680->lock);
  1167. return -EINVAL;
  1168. }
  1169. pos = (unsigned long) stv680->fbuf;
  1170. while (size > 0) {
  1171. page = vmalloc_to_pfn((void *)pos);
  1172. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1173. mutex_unlock(&stv680->lock);
  1174. return -EAGAIN;
  1175. }
  1176. start += PAGE_SIZE;
  1177. pos += PAGE_SIZE;
  1178. if (size > PAGE_SIZE)
  1179. size -= PAGE_SIZE;
  1180. else
  1181. size = 0;
  1182. }
  1183. mutex_unlock(&stv680->lock);
  1184. return 0;
  1185. }
  1186. static ssize_t stv680_read (struct file *file, char __user *buf,
  1187. size_t count, loff_t *ppos)
  1188. {
  1189. struct video_device *dev = file->private_data;
  1190. unsigned long int realcount = count;
  1191. int ret = 0;
  1192. struct usb_stv *stv680 = video_get_drvdata(dev);
  1193. unsigned long int i;
  1194. if (STV680_NUMFRAMES != 2) {
  1195. PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
  1196. return -1;
  1197. }
  1198. if (stv680->udev == NULL)
  1199. return -EIO;
  1200. if (realcount > (stv680->vwidth * stv680->vheight * 3))
  1201. realcount = stv680->vwidth * stv680->vheight * 3;
  1202. /* Shouldn't happen: */
  1203. if (stv680->frame[0].grabstate == FRAME_GRABBING) {
  1204. PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
  1205. return -EBUSY;
  1206. }
  1207. stv680->frame[0].grabstate = FRAME_READY;
  1208. stv680->frame[1].grabstate = FRAME_UNUSED;
  1209. stv680->curframe = 0;
  1210. if (!stv680->streaming)
  1211. stv680_start_stream (stv680);
  1212. if (!stv680->streaming) {
  1213. ret = stv680_newframe (stv680, 0); /* ret should = 0 */
  1214. }
  1215. ret = stv680_newframe (stv680, 0);
  1216. if (!ret) {
  1217. if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
  1218. PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
  1219. return -EFAULT;
  1220. }
  1221. } else {
  1222. realcount = ret;
  1223. }
  1224. stv680->frame[0].grabstate = FRAME_UNUSED;
  1225. return realcount;
  1226. } /* stv680_read */
  1227. static const struct v4l2_file_operations stv680_fops = {
  1228. .owner = THIS_MODULE,
  1229. .open = stv_open,
  1230. .release = stv_close,
  1231. .read = stv680_read,
  1232. .mmap = stv680_mmap,
  1233. .ioctl = stv680_ioctl,
  1234. };
  1235. static struct video_device stv680_template = {
  1236. .name = "STV0680 USB camera",
  1237. .fops = &stv680_fops,
  1238. .release = video_device_release,
  1239. .minor = -1,
  1240. };
  1241. static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
  1242. {
  1243. struct usb_device *dev = interface_to_usbdev(intf);
  1244. struct usb_host_interface *interface;
  1245. struct usb_stv *stv680 = NULL;
  1246. char *camera_name = NULL;
  1247. int retval = 0;
  1248. /* We don't handle multi-config cameras */
  1249. if (dev->descriptor.bNumConfigurations != 1) {
  1250. PDEBUG (0, "STV(e): Number of Configurations != 1");
  1251. return -ENODEV;
  1252. }
  1253. interface = &intf->altsetting[0];
  1254. /* Is it a STV680? */
  1255. if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
  1256. (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
  1257. camera_name = "STV0680";
  1258. PDEBUG (0, "STV(i): STV0680 camera found.");
  1259. } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
  1260. (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
  1261. camera_name = "Creative WebCam Go Mini";
  1262. PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
  1263. } else {
  1264. PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
  1265. PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
  1266. retval = -ENODEV;
  1267. goto error;
  1268. }
  1269. /* We found one */
  1270. if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
  1271. PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
  1272. retval = -ENOMEM;
  1273. goto error;
  1274. }
  1275. stv680->udev = dev;
  1276. stv680->camera_name = camera_name;
  1277. stv680->vdev = video_device_alloc();
  1278. if (!stv680->vdev) {
  1279. retval = -ENOMEM;
  1280. goto error;
  1281. }
  1282. memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
  1283. stv680->vdev->parent = &intf->dev;
  1284. video_set_drvdata(stv680->vdev, stv680);
  1285. memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
  1286. init_waitqueue_head (&stv680->wq);
  1287. mutex_init (&stv680->lock);
  1288. wmb ();
  1289. if (video_register_device(stv680->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1290. PDEBUG (0, "STV(e): video_register_device failed");
  1291. retval = -EIO;
  1292. goto error_vdev;
  1293. }
  1294. PDEBUG(0, "STV(i): registered new video device: video%d",
  1295. stv680->vdev->num);
  1296. usb_set_intfdata (intf, stv680);
  1297. retval = stv680_create_sysfs_files(stv680->vdev);
  1298. if (retval)
  1299. goto error_unreg;
  1300. return 0;
  1301. error_unreg:
  1302. video_unregister_device(stv680->vdev);
  1303. error_vdev:
  1304. video_device_release(stv680->vdev);
  1305. error:
  1306. kfree(stv680);
  1307. return retval;
  1308. }
  1309. static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
  1310. {
  1311. int i;
  1312. stv680->udev = NULL;
  1313. stv680->frame[0].grabstate = FRAME_ERROR;
  1314. stv680->frame[1].grabstate = FRAME_ERROR;
  1315. stv680->streaming = 0;
  1316. wake_up_interruptible (&stv680->wq);
  1317. for (i = 0; i < STV680_NUMSBUF; i++)
  1318. if (stv680->urb[i]) {
  1319. usb_kill_urb (stv680->urb[i]);
  1320. usb_free_urb (stv680->urb[i]);
  1321. stv680->urb[i] = NULL;
  1322. kfree(stv680->sbuf[i].data);
  1323. }
  1324. for (i = 0; i < STV680_NUMSCRATCH; i++)
  1325. kfree(stv680->scratch[i].data);
  1326. PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
  1327. /* Free the memory */
  1328. kfree(stv680);
  1329. }
  1330. static void stv680_disconnect (struct usb_interface *intf)
  1331. {
  1332. struct usb_stv *stv680 = usb_get_intfdata (intf);
  1333. usb_set_intfdata (intf, NULL);
  1334. if (stv680) {
  1335. /* We don't want people trying to open up the device */
  1336. if (stv680->vdev) {
  1337. stv680_remove_sysfs_files(stv680->vdev);
  1338. video_unregister_device(stv680->vdev);
  1339. stv680->vdev = NULL;
  1340. }
  1341. if (!stv680->user) {
  1342. usb_stv680_remove_disconnected (stv680);
  1343. } else {
  1344. stv680->removed = 1;
  1345. }
  1346. }
  1347. }
  1348. static struct usb_driver stv680_driver = {
  1349. .name = "stv680",
  1350. .probe = stv680_probe,
  1351. .disconnect = stv680_disconnect,
  1352. .id_table = device_table
  1353. };
  1354. /********************************************************************
  1355. * Module routines
  1356. ********************************************************************/
  1357. static int __init usb_stv680_init (void)
  1358. {
  1359. if (usb_register (&stv680_driver) < 0) {
  1360. PDEBUG (0, "STV(e): Could not setup STV0680 driver");
  1361. return -1;
  1362. }
  1363. PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
  1364. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1365. DRIVER_DESC "\n");
  1366. return 0;
  1367. }
  1368. static void __exit usb_stv680_exit (void)
  1369. {
  1370. usb_deregister (&stv680_driver);
  1371. PDEBUG (0, "STV(i): driver deregistered");
  1372. }
  1373. module_init (usb_stv680_init);
  1374. module_exit (usb_stv680_exit);