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