stv680.c 44 KB

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