stv680.c 43 KB

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