gspca.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. /*
  2. * Main USB camera driver
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define MODULE_NAME "gspca"
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/io.h>
  30. #include <asm/page.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/jiffies.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include "gspca.h"
  35. /* global values */
  36. #define DEF_NURBS 2 /* default number of URBs */
  37. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  38. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  39. MODULE_LICENSE("GPL");
  40. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
  41. static int video_nr = -1;
  42. #ifdef GSPCA_DEBUG
  43. int gspca_debug = D_ERR | D_PROBE;
  44. EXPORT_SYMBOL(gspca_debug);
  45. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  46. {
  47. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  48. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  49. txt,
  50. pixfmt & 0xff,
  51. (pixfmt >> 8) & 0xff,
  52. (pixfmt >> 16) & 0xff,
  53. pixfmt >> 24,
  54. w, h);
  55. } else {
  56. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  57. txt,
  58. pixfmt,
  59. w, h);
  60. }
  61. }
  62. #else
  63. #define PDEBUG_MODE(txt, pixfmt, w, h)
  64. #endif
  65. /* specific memory types - !! should different from V4L2_MEMORY_xxx */
  66. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  67. #define GSPCA_MEMORY_READ 7
  68. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  69. /*
  70. * VMA operations.
  71. */
  72. static void gspca_vm_open(struct vm_area_struct *vma)
  73. {
  74. struct gspca_frame *frame = vma->vm_private_data;
  75. frame->vma_use_count++;
  76. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  77. }
  78. static void gspca_vm_close(struct vm_area_struct *vma)
  79. {
  80. struct gspca_frame *frame = vma->vm_private_data;
  81. if (--frame->vma_use_count <= 0)
  82. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  83. }
  84. static struct vm_operations_struct gspca_vm_ops = {
  85. .open = gspca_vm_open,
  86. .close = gspca_vm_close,
  87. };
  88. /* get the current input frame buffer */
  89. struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
  90. {
  91. struct gspca_frame *frame;
  92. int i;
  93. i = gspca_dev->fr_i;
  94. i = gspca_dev->fr_queue[i];
  95. frame = &gspca_dev->frame[i];
  96. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  97. != V4L2_BUF_FLAG_QUEUED)
  98. return NULL;
  99. return frame;
  100. }
  101. EXPORT_SYMBOL(gspca_get_i_frame);
  102. /*
  103. * fill a video frame from an URB and resubmit
  104. */
  105. static void fill_frame(struct gspca_dev *gspca_dev,
  106. struct urb *urb)
  107. {
  108. struct gspca_frame *frame;
  109. __u8 *data; /* address of data in the iso message */
  110. int i, len, st;
  111. cam_pkt_op pkt_scan;
  112. if (urb->status != 0) {
  113. #ifdef CONFIG_PM
  114. if (!gspca_dev->frozen)
  115. #endif
  116. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  117. return; /* disconnection ? */
  118. }
  119. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  120. for (i = 0; i < urb->number_of_packets; i++) {
  121. /* check the availability of the frame buffer */
  122. frame = gspca_get_i_frame(gspca_dev);
  123. if (!frame) {
  124. gspca_dev->last_packet_type = DISCARD_PACKET;
  125. break;
  126. }
  127. /* check the packet status and length */
  128. len = urb->iso_frame_desc[i].actual_length;
  129. if (len == 0)
  130. continue;
  131. st = urb->iso_frame_desc[i].status;
  132. if (st) {
  133. PDEBUG(D_ERR,
  134. "ISOC data error: [%d] len=%d, status=%d",
  135. i, len, st);
  136. gspca_dev->last_packet_type = DISCARD_PACKET;
  137. continue;
  138. }
  139. /* let the packet be analyzed by the subdriver */
  140. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  141. i, urb->iso_frame_desc[i].offset, len);
  142. data = (__u8 *) urb->transfer_buffer
  143. + urb->iso_frame_desc[i].offset;
  144. pkt_scan(gspca_dev, frame, data, len);
  145. }
  146. /* resubmit the URB */
  147. urb->status = 0;
  148. st = usb_submit_urb(urb, GFP_ATOMIC);
  149. if (st < 0)
  150. PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
  151. }
  152. /*
  153. * ISOC message interrupt from the USB device
  154. *
  155. * Analyse each packet and call the subdriver for copy to the frame buffer.
  156. */
  157. static void isoc_irq(struct urb *urb
  158. )
  159. {
  160. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  161. PDEBUG(D_PACK, "isoc irq");
  162. if (!gspca_dev->streaming)
  163. return;
  164. fill_frame(gspca_dev, urb);
  165. }
  166. /*
  167. * bulk message interrupt from the USB device
  168. */
  169. static void bulk_irq(struct urb *urb
  170. )
  171. {
  172. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  173. struct gspca_frame *frame;
  174. PDEBUG(D_PACK, "bulk irq");
  175. if (!gspca_dev->streaming)
  176. return;
  177. if (urb->status != 0 && urb->status != -ECONNRESET) {
  178. #ifdef CONFIG_PM
  179. if (!gspca_dev->frozen)
  180. #endif
  181. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  182. return; /* disconnection ? */
  183. }
  184. /* check the availability of the frame buffer */
  185. frame = gspca_get_i_frame(gspca_dev);
  186. if (!frame) {
  187. gspca_dev->last_packet_type = DISCARD_PACKET;
  188. } else {
  189. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  190. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  191. frame,
  192. urb->transfer_buffer,
  193. urb->actual_length);
  194. }
  195. }
  196. /*
  197. * add data to the current frame
  198. *
  199. * This function is called by the subdrivers at interrupt level.
  200. *
  201. * To build a frame, these ones must add
  202. * - one FIRST_PACKET
  203. * - 0 or many INTER_PACKETs
  204. * - one LAST_PACKET
  205. * DISCARD_PACKET invalidates the whole frame.
  206. * On LAST_PACKET, a new frame is returned.
  207. */
  208. struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
  209. int packet_type,
  210. struct gspca_frame *frame,
  211. const __u8 *data,
  212. int len)
  213. {
  214. int i, j;
  215. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  216. /* when start of a new frame, if the current frame buffer
  217. * is not queued, discard the whole frame */
  218. if (packet_type == FIRST_PACKET) {
  219. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  220. != V4L2_BUF_FLAG_QUEUED) {
  221. gspca_dev->last_packet_type = DISCARD_PACKET;
  222. return frame;
  223. }
  224. frame->data_end = frame->data;
  225. jiffies_to_timeval(get_jiffies_64(),
  226. &frame->v4l2_buf.timestamp);
  227. frame->v4l2_buf.sequence = ++gspca_dev->sequence;
  228. } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
  229. if (packet_type == LAST_PACKET)
  230. gspca_dev->last_packet_type = packet_type;
  231. return frame;
  232. }
  233. /* append the packet to the frame buffer */
  234. if (len > 0) {
  235. if (frame->data_end - frame->data + len
  236. > frame->v4l2_buf.length) {
  237. PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
  238. frame->data_end - frame->data + len,
  239. frame->v4l2_buf.length);
  240. packet_type = DISCARD_PACKET;
  241. } else {
  242. memcpy(frame->data_end, data, len);
  243. frame->data_end += len;
  244. }
  245. }
  246. gspca_dev->last_packet_type = packet_type;
  247. /* if last packet, wake the application and advance in the queue */
  248. if (packet_type == LAST_PACKET) {
  249. frame->v4l2_buf.bytesused = frame->data_end - frame->data;
  250. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  251. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
  252. atomic_inc(&gspca_dev->nevent);
  253. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  254. i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
  255. gspca_dev->fr_i = i;
  256. PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
  257. frame->v4l2_buf.bytesused,
  258. gspca_dev->fr_q,
  259. i,
  260. gspca_dev->fr_o);
  261. j = gspca_dev->fr_queue[i];
  262. frame = &gspca_dev->frame[j];
  263. }
  264. return frame;
  265. }
  266. EXPORT_SYMBOL(gspca_frame_add);
  267. static int gspca_is_compressed(__u32 format)
  268. {
  269. switch (format) {
  270. case V4L2_PIX_FMT_MJPEG:
  271. case V4L2_PIX_FMT_JPEG:
  272. case V4L2_PIX_FMT_SPCA561:
  273. case V4L2_PIX_FMT_PAC207:
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. static void *rvmalloc(unsigned long size)
  279. {
  280. void *mem;
  281. unsigned long adr;
  282. /* size = PAGE_ALIGN(size); (already done) */
  283. mem = vmalloc_32(size);
  284. if (mem != NULL) {
  285. adr = (unsigned long) mem;
  286. while ((long) size > 0) {
  287. SetPageReserved(vmalloc_to_page((void *) adr));
  288. adr += PAGE_SIZE;
  289. size -= PAGE_SIZE;
  290. }
  291. }
  292. return mem;
  293. }
  294. static void rvfree(void *mem, long size)
  295. {
  296. unsigned long adr;
  297. adr = (unsigned long) mem;
  298. while (size > 0) {
  299. ClearPageReserved(vmalloc_to_page((void *) adr));
  300. adr += PAGE_SIZE;
  301. size -= PAGE_SIZE;
  302. }
  303. vfree(mem);
  304. }
  305. static int frame_alloc(struct gspca_dev *gspca_dev,
  306. unsigned int count)
  307. {
  308. struct gspca_frame *frame;
  309. unsigned int frsz;
  310. int i;
  311. i = gspca_dev->curr_mode;
  312. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  313. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  314. frsz = PAGE_ALIGN(frsz);
  315. gspca_dev->frsz = frsz;
  316. if (count > GSPCA_MAX_FRAMES)
  317. count = GSPCA_MAX_FRAMES;
  318. gspca_dev->frbuf = rvmalloc(frsz * count);
  319. if (!gspca_dev->frbuf) {
  320. err("frame alloc failed");
  321. return -ENOMEM;
  322. }
  323. gspca_dev->nframes = count;
  324. for (i = 0; i < count; i++) {
  325. frame = &gspca_dev->frame[i];
  326. frame->v4l2_buf.index = i;
  327. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  328. frame->v4l2_buf.flags = 0;
  329. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  330. frame->v4l2_buf.length = frsz;
  331. frame->v4l2_buf.memory = gspca_dev->memory;
  332. frame->v4l2_buf.sequence = 0;
  333. frame->data = frame->data_end =
  334. gspca_dev->frbuf + i * frsz;
  335. frame->v4l2_buf.m.offset = i * frsz;
  336. }
  337. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  338. gspca_dev->last_packet_type = DISCARD_PACKET;
  339. gspca_dev->sequence = 0;
  340. atomic_set(&gspca_dev->nevent, 0);
  341. return 0;
  342. }
  343. static void frame_free(struct gspca_dev *gspca_dev)
  344. {
  345. int i;
  346. PDEBUG(D_STREAM, "frame free");
  347. if (gspca_dev->frbuf != NULL) {
  348. rvfree(gspca_dev->frbuf,
  349. gspca_dev->nframes * gspca_dev->frsz);
  350. gspca_dev->frbuf = NULL;
  351. for (i = 0; i < gspca_dev->nframes; i++)
  352. gspca_dev->frame[i].data = NULL;
  353. }
  354. gspca_dev->nframes = 0;
  355. }
  356. static void destroy_urbs(struct gspca_dev *gspca_dev)
  357. {
  358. struct urb *urb;
  359. unsigned int i;
  360. PDEBUG(D_STREAM, "kill transfer");
  361. for (i = 0; i < MAX_NURBS; ++i) {
  362. urb = gspca_dev->urb[i];
  363. if (urb == NULL)
  364. break;
  365. gspca_dev->urb[i] = NULL;
  366. usb_kill_urb(urb);
  367. if (urb->transfer_buffer != NULL)
  368. usb_buffer_free(gspca_dev->dev,
  369. urb->transfer_buffer_length,
  370. urb->transfer_buffer,
  371. urb->transfer_dma);
  372. usb_free_urb(urb);
  373. }
  374. }
  375. /*
  376. * search an input transfer endpoint in an alternate setting
  377. */
  378. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  379. __u8 epaddr,
  380. __u8 xfer)
  381. {
  382. struct usb_host_endpoint *ep;
  383. int i, attr;
  384. epaddr |= USB_DIR_IN;
  385. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  386. ep = &alt->endpoint[i];
  387. if (ep->desc.bEndpointAddress == epaddr) {
  388. attr = ep->desc.bmAttributes
  389. & USB_ENDPOINT_XFERTYPE_MASK;
  390. if (attr == xfer)
  391. return ep;
  392. break;
  393. }
  394. }
  395. return NULL;
  396. }
  397. /*
  398. * search an input (isoc or bulk) endpoint
  399. *
  400. * The endpoint is defined by the subdriver.
  401. * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
  402. * This routine may be called many times when the bandwidth is too small
  403. * (the bandwidth is checked on urb submit).
  404. */
  405. static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
  406. {
  407. struct usb_interface *intf;
  408. struct usb_host_endpoint *ep;
  409. int i, ret;
  410. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  411. ep = NULL;
  412. i = gspca_dev->alt; /* previous alt setting */
  413. while (--i > 0) { /* alt 0 is unusable */
  414. ep = alt_xfer(&intf->altsetting[i],
  415. gspca_dev->cam.epaddr,
  416. gspca_dev->bulk
  417. ? USB_ENDPOINT_XFER_BULK
  418. : USB_ENDPOINT_XFER_ISOC);
  419. if (ep)
  420. break;
  421. }
  422. if (ep == NULL) {
  423. err("no transfer endpoint found");
  424. return NULL;
  425. }
  426. PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
  427. i, ep->desc.bEndpointAddress);
  428. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
  429. if (ret < 0) {
  430. err("set interface err %d", ret);
  431. return NULL;
  432. }
  433. gspca_dev->alt = i; /* memorize the current alt setting */
  434. return ep;
  435. }
  436. /*
  437. * create the URBs for image transfer
  438. */
  439. static int create_urbs(struct gspca_dev *gspca_dev,
  440. struct usb_host_endpoint *ep)
  441. {
  442. struct urb *urb;
  443. int n, nurbs, i, psize, npkt, bsize;
  444. /* calculate the packet size and the number of packets */
  445. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  446. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  447. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  448. if (!gspca_dev->bulk) {
  449. npkt = ISO_MAX_SIZE / psize;
  450. if (npkt > ISO_MAX_PKT)
  451. npkt = ISO_MAX_PKT;
  452. bsize = psize * npkt;
  453. PDEBUG(D_STREAM,
  454. "isoc %d pkts size %d = bsize:%d",
  455. npkt, psize, bsize);
  456. nurbs = DEF_NURBS;
  457. } else {
  458. npkt = 0;
  459. bsize = psize;
  460. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  461. nurbs = 1;
  462. }
  463. gspca_dev->nurbs = nurbs;
  464. for (n = 0; n < nurbs; n++) {
  465. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  466. if (!urb) {
  467. err("usb_alloc_urb failed");
  468. destroy_urbs(gspca_dev);
  469. return -ENOMEM;
  470. }
  471. urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
  472. bsize,
  473. GFP_KERNEL,
  474. &urb->transfer_dma);
  475. if (urb->transfer_buffer == NULL) {
  476. usb_free_urb(urb);
  477. err("usb_buffer_urb failed");
  478. destroy_urbs(gspca_dev);
  479. return -ENOMEM;
  480. }
  481. gspca_dev->urb[n] = urb;
  482. urb->dev = gspca_dev->dev;
  483. urb->context = gspca_dev;
  484. urb->transfer_buffer_length = bsize;
  485. if (npkt != 0) { /* ISOC */
  486. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  487. ep->desc.bEndpointAddress);
  488. urb->transfer_flags = URB_ISO_ASAP;
  489. urb->interval = ep->desc.bInterval;
  490. urb->complete = isoc_irq;
  491. urb->number_of_packets = npkt;
  492. for (i = 0; i < npkt; i++) {
  493. urb->iso_frame_desc[i].length = psize;
  494. urb->iso_frame_desc[i].offset = psize * i;
  495. }
  496. } else { /* bulk */
  497. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  498. ep->desc.bEndpointAddress),
  499. urb->complete = bulk_irq;
  500. }
  501. }
  502. return 0;
  503. }
  504. /*
  505. * start the USB transfer
  506. */
  507. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  508. {
  509. struct usb_host_endpoint *ep;
  510. int n, ret;
  511. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  512. return -ERESTARTSYS;
  513. /* set the higher alternate setting and
  514. * loop until urb submit succeeds */
  515. gspca_dev->alt = gspca_dev->nbalt;
  516. for (;;) {
  517. PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
  518. ep = get_ep(gspca_dev);
  519. if (ep == NULL) {
  520. ret = -EIO;
  521. goto out;
  522. }
  523. ret = create_urbs(gspca_dev, ep);
  524. if (ret < 0)
  525. goto out;
  526. /* start the cam */
  527. ret = gspca_dev->sd_desc->start(gspca_dev);
  528. if (ret < 0) {
  529. destroy_urbs(gspca_dev);
  530. goto out;
  531. }
  532. gspca_dev->streaming = 1;
  533. atomic_set(&gspca_dev->nevent, 0);
  534. /* start the bulk transfer is done by the subdriver */
  535. if (gspca_dev->bulk)
  536. break;
  537. /* submit the URBs */
  538. for (n = 0; n < gspca_dev->nurbs; n++) {
  539. ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
  540. if (ret < 0) {
  541. PDEBUG(D_ERR|D_STREAM,
  542. "usb_submit_urb [%d] err %d", n, ret);
  543. gspca_dev->streaming = 0;
  544. destroy_urbs(gspca_dev);
  545. if (ret == -ENOSPC)
  546. break; /* try the previous alt */
  547. goto out;
  548. }
  549. }
  550. if (ret >= 0)
  551. break;
  552. }
  553. out:
  554. mutex_unlock(&gspca_dev->usb_lock);
  555. return ret;
  556. }
  557. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  558. {
  559. int ret;
  560. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  561. if (ret < 0)
  562. PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
  563. return ret;
  564. }
  565. /* Note both the queue and the usb lock should be hold when calling this */
  566. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  567. {
  568. gspca_dev->streaming = 0;
  569. atomic_set(&gspca_dev->nevent, 0);
  570. if (gspca_dev->present) {
  571. if (gspca_dev->sd_desc->stopN)
  572. gspca_dev->sd_desc->stopN(gspca_dev);
  573. destroy_urbs(gspca_dev);
  574. gspca_set_alt0(gspca_dev);
  575. if (gspca_dev->sd_desc->stop0)
  576. gspca_dev->sd_desc->stop0(gspca_dev);
  577. PDEBUG(D_STREAM, "stream off OK");
  578. }
  579. }
  580. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  581. {
  582. int i;
  583. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  584. gspca_dev->curr_mode = i;
  585. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  586. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  587. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  588. }
  589. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  590. int width, int height)
  591. {
  592. int i;
  593. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  594. if (width >= gspca_dev->cam.cam_mode[i].width
  595. && height >= gspca_dev->cam.cam_mode[i].height)
  596. break;
  597. }
  598. return i;
  599. }
  600. /*
  601. * search a mode with the right pixel format
  602. */
  603. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  604. int mode,
  605. int pixfmt)
  606. {
  607. int modeU, modeD;
  608. modeU = modeD = mode;
  609. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  610. if (--modeD >= 0) {
  611. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  612. == pixfmt)
  613. return modeD;
  614. }
  615. if (++modeU < gspca_dev->cam.nmodes) {
  616. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  617. == pixfmt)
  618. return modeU;
  619. }
  620. }
  621. return -EINVAL;
  622. }
  623. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  624. struct v4l2_fmtdesc *fmtdesc)
  625. {
  626. struct gspca_dev *gspca_dev = priv;
  627. int i, j, index;
  628. __u32 fmt_tb[8];
  629. /* give an index to each format */
  630. index = 0;
  631. j = 0;
  632. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  633. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  634. j = 0;
  635. for (;;) {
  636. if (fmt_tb[j] == fmt_tb[index])
  637. break;
  638. j++;
  639. }
  640. if (j == index) {
  641. if (fmtdesc->index == index)
  642. break; /* new format */
  643. index++;
  644. if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
  645. return -EINVAL;
  646. }
  647. }
  648. if (i < 0)
  649. return -EINVAL; /* no more format */
  650. fmtdesc->pixelformat = fmt_tb[index];
  651. if (gspca_is_compressed(fmt_tb[index]))
  652. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  653. fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  654. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  655. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  656. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  657. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  658. fmtdesc->description[4] = '\0';
  659. return 0;
  660. }
  661. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  662. struct v4l2_format *fmt)
  663. {
  664. struct gspca_dev *gspca_dev = priv;
  665. int mode;
  666. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  667. return -EINVAL;
  668. mode = gspca_dev->curr_mode;
  669. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  670. sizeof fmt->fmt.pix);
  671. return 0;
  672. }
  673. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  674. struct v4l2_format *fmt)
  675. {
  676. int w, h, mode, mode2;
  677. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  678. return -EINVAL;
  679. w = fmt->fmt.pix.width;
  680. h = fmt->fmt.pix.height;
  681. #ifdef GSPCA_DEBUG
  682. if (gspca_debug & D_CONF)
  683. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  684. #endif
  685. /* search the closest mode for width and height */
  686. mode = wxh_to_mode(gspca_dev, w, h);
  687. /* OK if right palette */
  688. if (gspca_dev->cam.cam_mode[mode].pixelformat
  689. != fmt->fmt.pix.pixelformat) {
  690. /* else, search the closest mode with the same pixel format */
  691. mode2 = gspca_get_mode(gspca_dev, mode,
  692. fmt->fmt.pix.pixelformat);
  693. if (mode2 >= 0)
  694. mode = mode2;
  695. /* else
  696. ; * no chance, return this mode */
  697. }
  698. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  699. sizeof fmt->fmt.pix);
  700. return mode; /* used when s_fmt */
  701. }
  702. static int vidioc_try_fmt_vid_cap(struct file *file,
  703. void *priv,
  704. struct v4l2_format *fmt)
  705. {
  706. struct gspca_dev *gspca_dev = priv;
  707. int ret;
  708. ret = try_fmt_vid_cap(gspca_dev, fmt);
  709. if (ret < 0)
  710. return ret;
  711. return 0;
  712. }
  713. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  714. struct v4l2_format *fmt)
  715. {
  716. struct gspca_dev *gspca_dev = priv;
  717. int ret;
  718. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  719. return -ERESTARTSYS;
  720. ret = try_fmt_vid_cap(gspca_dev, fmt);
  721. if (ret < 0)
  722. goto out;
  723. if (gspca_dev->nframes != 0
  724. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  725. ret = -EINVAL;
  726. goto out;
  727. }
  728. if (ret == gspca_dev->curr_mode) {
  729. ret = 0;
  730. goto out; /* same mode */
  731. }
  732. if (gspca_dev->streaming) {
  733. ret = -EBUSY;
  734. goto out;
  735. }
  736. gspca_dev->width = fmt->fmt.pix.width;
  737. gspca_dev->height = fmt->fmt.pix.height;
  738. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  739. gspca_dev->curr_mode = ret;
  740. ret = 0;
  741. out:
  742. mutex_unlock(&gspca_dev->queue_lock);
  743. return ret;
  744. }
  745. static int dev_open(struct inode *inode, struct file *file)
  746. {
  747. struct gspca_dev *gspca_dev;
  748. int ret;
  749. PDEBUG(D_STREAM, "%s open", current->comm);
  750. gspca_dev = (struct gspca_dev *) video_devdata(file);
  751. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  752. return -ERESTARTSYS;
  753. if (!gspca_dev->present) {
  754. ret = -ENODEV;
  755. goto out;
  756. }
  757. if (gspca_dev->users > 4) { /* (arbitrary value) */
  758. ret = -EBUSY;
  759. goto out;
  760. }
  761. gspca_dev->users++;
  762. file->private_data = gspca_dev;
  763. #ifdef GSPCA_DEBUG
  764. /* activate the v4l2 debug */
  765. if (gspca_debug & D_V4L2)
  766. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  767. | V4L2_DEBUG_IOCTL_ARG;
  768. else
  769. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  770. | V4L2_DEBUG_IOCTL_ARG);
  771. #endif
  772. ret = 0;
  773. out:
  774. mutex_unlock(&gspca_dev->queue_lock);
  775. if (ret != 0)
  776. PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
  777. else
  778. PDEBUG(D_STREAM, "open done");
  779. return ret;
  780. }
  781. static int dev_close(struct inode *inode, struct file *file)
  782. {
  783. struct gspca_dev *gspca_dev = file->private_data;
  784. PDEBUG(D_STREAM, "%s close", current->comm);
  785. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  786. return -ERESTARTSYS;
  787. gspca_dev->users--;
  788. /* if the file did the capture, free the streaming resources */
  789. if (gspca_dev->capt_file == file) {
  790. if (gspca_dev->streaming) {
  791. mutex_lock(&gspca_dev->usb_lock);
  792. gspca_stream_off(gspca_dev);
  793. mutex_unlock(&gspca_dev->usb_lock);
  794. }
  795. frame_free(gspca_dev);
  796. gspca_dev->capt_file = NULL;
  797. gspca_dev->memory = GSPCA_MEMORY_NO;
  798. }
  799. file->private_data = NULL;
  800. mutex_unlock(&gspca_dev->queue_lock);
  801. PDEBUG(D_STREAM, "close done");
  802. return 0;
  803. }
  804. static int vidioc_querycap(struct file *file, void *priv,
  805. struct v4l2_capability *cap)
  806. {
  807. struct gspca_dev *gspca_dev = priv;
  808. memset(cap, 0, sizeof *cap);
  809. strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
  810. /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
  811. if (gspca_dev->dev->product != NULL) {
  812. strncpy(cap->card, gspca_dev->dev->product,
  813. sizeof cap->card);
  814. } else {
  815. snprintf(cap->card, sizeof cap->card,
  816. "USB Camera (%04x:%04x)",
  817. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  818. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  819. }
  820. strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
  821. sizeof cap->bus_info);
  822. cap->version = DRIVER_VERSION_NUMBER;
  823. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  824. | V4L2_CAP_STREAMING
  825. | V4L2_CAP_READWRITE;
  826. return 0;
  827. }
  828. static int vidioc_queryctrl(struct file *file, void *priv,
  829. struct v4l2_queryctrl *q_ctrl)
  830. {
  831. struct gspca_dev *gspca_dev = priv;
  832. int i, ix;
  833. u32 id;
  834. ix = -1;
  835. id = q_ctrl->id;
  836. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  837. id &= V4L2_CTRL_ID_MASK;
  838. id++;
  839. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  840. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  841. continue;
  842. if (ix < 0) {
  843. ix = i;
  844. continue;
  845. }
  846. if (gspca_dev->sd_desc->ctrls[i].qctrl.id
  847. > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
  848. continue;
  849. ix = i;
  850. }
  851. }
  852. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  853. if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  854. ix = i;
  855. break;
  856. }
  857. }
  858. if (ix < 0)
  859. return -EINVAL;
  860. memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
  861. sizeof *q_ctrl);
  862. if (gspca_dev->ctrl_dis & (1 << ix))
  863. q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  864. return 0;
  865. }
  866. static int vidioc_s_ctrl(struct file *file, void *priv,
  867. struct v4l2_control *ctrl)
  868. {
  869. struct gspca_dev *gspca_dev = priv;
  870. const struct ctrl *ctrls;
  871. int i, ret;
  872. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  873. i < gspca_dev->sd_desc->nctrls;
  874. i++, ctrls++) {
  875. if (ctrl->id != ctrls->qctrl.id)
  876. continue;
  877. if (gspca_dev->ctrl_dis & (1 << i))
  878. return -EINVAL;
  879. if (ctrl->value < ctrls->qctrl.minimum
  880. || ctrl->value > ctrls->qctrl.maximum)
  881. return -ERANGE;
  882. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  883. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  884. return -ERESTARTSYS;
  885. ret = ctrls->set(gspca_dev, ctrl->value);
  886. mutex_unlock(&gspca_dev->usb_lock);
  887. return ret;
  888. }
  889. return -EINVAL;
  890. }
  891. static int vidioc_g_ctrl(struct file *file, void *priv,
  892. struct v4l2_control *ctrl)
  893. {
  894. struct gspca_dev *gspca_dev = priv;
  895. const struct ctrl *ctrls;
  896. int i, ret;
  897. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  898. i < gspca_dev->sd_desc->nctrls;
  899. i++, ctrls++) {
  900. if (ctrl->id != ctrls->qctrl.id)
  901. continue;
  902. if (gspca_dev->ctrl_dis & (1 << i))
  903. return -EINVAL;
  904. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  905. return -ERESTARTSYS;
  906. ret = ctrls->get(gspca_dev, &ctrl->value);
  907. mutex_unlock(&gspca_dev->usb_lock);
  908. return ret;
  909. }
  910. return -EINVAL;
  911. }
  912. static int vidioc_querymenu(struct file *file, void *priv,
  913. struct v4l2_querymenu *qmenu)
  914. {
  915. struct gspca_dev *gspca_dev = priv;
  916. if (!gspca_dev->sd_desc->querymenu)
  917. return -EINVAL;
  918. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  919. }
  920. static int vidioc_enum_input(struct file *file, void *priv,
  921. struct v4l2_input *input)
  922. {
  923. struct gspca_dev *gspca_dev = priv;
  924. if (input->index != 0)
  925. return -EINVAL;
  926. memset(input, 0, sizeof *input);
  927. input->type = V4L2_INPUT_TYPE_CAMERA;
  928. strncpy(input->name, gspca_dev->sd_desc->name,
  929. sizeof input->name);
  930. return 0;
  931. }
  932. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  933. {
  934. *i = 0;
  935. return 0;
  936. }
  937. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  938. {
  939. if (i > 0)
  940. return -EINVAL;
  941. return (0);
  942. }
  943. static int vidioc_reqbufs(struct file *file, void *priv,
  944. struct v4l2_requestbuffers *rb)
  945. {
  946. struct gspca_dev *gspca_dev = priv;
  947. int i, ret = 0;
  948. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  949. return -EINVAL;
  950. switch (rb->memory) {
  951. case GSPCA_MEMORY_READ: /* (internal call) */
  952. case V4L2_MEMORY_MMAP:
  953. case V4L2_MEMORY_USERPTR:
  954. break;
  955. default:
  956. return -EINVAL;
  957. }
  958. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  959. return -ERESTARTSYS;
  960. if (gspca_dev->memory != GSPCA_MEMORY_NO
  961. && gspca_dev->memory != rb->memory) {
  962. ret = -EBUSY;
  963. goto out;
  964. }
  965. /* only one file may do the capture */
  966. if (gspca_dev->capt_file != NULL
  967. && gspca_dev->capt_file != file) {
  968. ret = -EBUSY;
  969. goto out;
  970. }
  971. /* if allocated, the buffers must not be mapped */
  972. for (i = 0; i < gspca_dev->nframes; i++) {
  973. if (gspca_dev->frame[i].vma_use_count) {
  974. ret = -EBUSY;
  975. goto out;
  976. }
  977. }
  978. /* stop streaming */
  979. if (gspca_dev->streaming) {
  980. mutex_lock(&gspca_dev->usb_lock);
  981. gspca_stream_off(gspca_dev);
  982. mutex_unlock(&gspca_dev->usb_lock);
  983. }
  984. /* free the previous allocated buffers, if any */
  985. if (gspca_dev->nframes != 0) {
  986. frame_free(gspca_dev);
  987. gspca_dev->capt_file = NULL;
  988. }
  989. if (rb->count == 0) /* unrequest */
  990. goto out;
  991. gspca_dev->memory = rb->memory;
  992. ret = frame_alloc(gspca_dev, rb->count);
  993. if (ret == 0) {
  994. rb->count = gspca_dev->nframes;
  995. gspca_dev->capt_file = file;
  996. }
  997. out:
  998. mutex_unlock(&gspca_dev->queue_lock);
  999. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1000. return ret;
  1001. }
  1002. static int vidioc_querybuf(struct file *file, void *priv,
  1003. struct v4l2_buffer *v4l2_buf)
  1004. {
  1005. struct gspca_dev *gspca_dev = priv;
  1006. struct gspca_frame *frame;
  1007. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1008. || v4l2_buf->index < 0
  1009. || v4l2_buf->index >= gspca_dev->nframes)
  1010. return -EINVAL;
  1011. frame = &gspca_dev->frame[v4l2_buf->index];
  1012. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1013. return 0;
  1014. }
  1015. static int vidioc_streamon(struct file *file, void *priv,
  1016. enum v4l2_buf_type buf_type)
  1017. {
  1018. struct gspca_dev *gspca_dev = priv;
  1019. int ret;
  1020. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1021. return -EINVAL;
  1022. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1023. return -ERESTARTSYS;
  1024. if (!gspca_dev->present) {
  1025. ret = -ENODEV;
  1026. goto out;
  1027. }
  1028. if (gspca_dev->nframes == 0) {
  1029. ret = -EINVAL;
  1030. goto out;
  1031. }
  1032. if (!gspca_dev->streaming) {
  1033. ret = gspca_init_transfer(gspca_dev);
  1034. if (ret < 0)
  1035. goto out;
  1036. }
  1037. #ifdef GSPCA_DEBUG
  1038. if (gspca_debug & D_STREAM) {
  1039. PDEBUG_MODE("stream on OK",
  1040. gspca_dev->pixfmt,
  1041. gspca_dev->width,
  1042. gspca_dev->height);
  1043. }
  1044. #endif
  1045. ret = 0;
  1046. out:
  1047. mutex_unlock(&gspca_dev->queue_lock);
  1048. return ret;
  1049. }
  1050. static int vidioc_streamoff(struct file *file, void *priv,
  1051. enum v4l2_buf_type buf_type)
  1052. {
  1053. struct gspca_dev *gspca_dev = priv;
  1054. int i, ret;
  1055. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1056. return -EINVAL;
  1057. if (!gspca_dev->streaming)
  1058. return 0;
  1059. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1060. return -ERESTARTSYS;
  1061. /* stop streaming */
  1062. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  1063. ret = -ERESTARTSYS;
  1064. goto out;
  1065. }
  1066. gspca_stream_off(gspca_dev);
  1067. mutex_unlock(&gspca_dev->usb_lock);
  1068. /* empty the application queues */
  1069. for (i = 0; i < gspca_dev->nframes; i++)
  1070. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1071. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  1072. gspca_dev->last_packet_type = DISCARD_PACKET;
  1073. gspca_dev->sequence = 0;
  1074. atomic_set(&gspca_dev->nevent, 0);
  1075. ret = 0;
  1076. out:
  1077. mutex_unlock(&gspca_dev->queue_lock);
  1078. return ret;
  1079. }
  1080. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1081. struct v4l2_jpegcompression *jpegcomp)
  1082. {
  1083. struct gspca_dev *gspca_dev = priv;
  1084. int ret;
  1085. if (!gspca_dev->sd_desc->get_jcomp)
  1086. return -EINVAL;
  1087. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1088. return -ERESTARTSYS;
  1089. ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1090. mutex_unlock(&gspca_dev->usb_lock);
  1091. return ret;
  1092. }
  1093. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1094. struct v4l2_jpegcompression *jpegcomp)
  1095. {
  1096. struct gspca_dev *gspca_dev = priv;
  1097. int ret;
  1098. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1099. return -ERESTARTSYS;
  1100. if (!gspca_dev->sd_desc->set_jcomp)
  1101. return -EINVAL;
  1102. ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1103. mutex_unlock(&gspca_dev->usb_lock);
  1104. return ret;
  1105. }
  1106. static int vidioc_g_parm(struct file *filp, void *priv,
  1107. struct v4l2_streamparm *parm)
  1108. {
  1109. struct gspca_dev *gspca_dev = priv;
  1110. memset(parm, 0, sizeof *parm);
  1111. parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1112. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1113. return 0;
  1114. }
  1115. static int vidioc_s_parm(struct file *filp, void *priv,
  1116. struct v4l2_streamparm *parm)
  1117. {
  1118. struct gspca_dev *gspca_dev = priv;
  1119. int n;
  1120. n = parm->parm.capture.readbuffers;
  1121. if (n == 0 || n > GSPCA_MAX_FRAMES)
  1122. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1123. else
  1124. gspca_dev->nbufread = n;
  1125. return 0;
  1126. }
  1127. static int vidioc_s_std(struct file *filp, void *priv,
  1128. v4l2_std_id *parm)
  1129. {
  1130. return 0;
  1131. }
  1132. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1133. static int vidiocgmbuf(struct file *file, void *priv,
  1134. struct video_mbuf *mbuf)
  1135. {
  1136. struct gspca_dev *gspca_dev = file->private_data;
  1137. int i;
  1138. PDEBUG(D_STREAM, "cgmbuf");
  1139. if (gspca_dev->nframes == 0) {
  1140. int ret;
  1141. {
  1142. struct v4l2_format fmt;
  1143. memset(&fmt, 0, sizeof fmt);
  1144. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1145. i = gspca_dev->cam.nmodes - 1; /* highest mode */
  1146. fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
  1147. fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
  1148. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
  1149. ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
  1150. if (ret != 0)
  1151. return ret;
  1152. }
  1153. {
  1154. struct v4l2_requestbuffers rb;
  1155. memset(&rb, 0, sizeof rb);
  1156. rb.count = 4;
  1157. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1158. rb.memory = V4L2_MEMORY_MMAP;
  1159. ret = vidioc_reqbufs(file, priv, &rb);
  1160. if (ret != 0)
  1161. return ret;
  1162. }
  1163. }
  1164. mbuf->frames = gspca_dev->nframes;
  1165. mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
  1166. for (i = 0; i < mbuf->frames; i++)
  1167. mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
  1168. return 0;
  1169. }
  1170. #endif
  1171. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1172. {
  1173. struct gspca_dev *gspca_dev = file->private_data;
  1174. struct gspca_frame *frame;
  1175. struct page *page;
  1176. unsigned long addr, start, size;
  1177. int i, ret;
  1178. start = vma->vm_start;
  1179. size = vma->vm_end - vma->vm_start;
  1180. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1181. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1182. return -ERESTARTSYS;
  1183. if (!gspca_dev->present) {
  1184. ret = -ENODEV;
  1185. goto out;
  1186. }
  1187. if (gspca_dev->capt_file != file) {
  1188. ret = -EINVAL;
  1189. goto out;
  1190. }
  1191. frame = NULL;
  1192. for (i = 0; i < gspca_dev->nframes; ++i) {
  1193. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1194. PDEBUG(D_STREAM, "mmap bad memory type");
  1195. break;
  1196. }
  1197. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1198. == vma->vm_pgoff) {
  1199. frame = &gspca_dev->frame[i];
  1200. break;
  1201. }
  1202. }
  1203. if (frame == NULL) {
  1204. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1205. ret = -EINVAL;
  1206. goto out;
  1207. }
  1208. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1209. /* v4l1 maps all the buffers */
  1210. if (i != 0
  1211. || size != frame->v4l2_buf.length * gspca_dev->nframes)
  1212. #endif
  1213. if (size != frame->v4l2_buf.length) {
  1214. PDEBUG(D_STREAM, "mmap bad size");
  1215. ret = -EINVAL;
  1216. goto out;
  1217. }
  1218. /*
  1219. * - VM_IO marks the area as being a mmaped region for I/O to a
  1220. * device. It also prevents the region from being core dumped.
  1221. */
  1222. vma->vm_flags |= VM_IO;
  1223. addr = (unsigned long) frame->data;
  1224. while (size > 0) {
  1225. page = vmalloc_to_page((void *) addr);
  1226. ret = vm_insert_page(vma, start, page);
  1227. if (ret < 0)
  1228. goto out;
  1229. start += PAGE_SIZE;
  1230. addr += PAGE_SIZE;
  1231. size -= PAGE_SIZE;
  1232. }
  1233. vma->vm_ops = &gspca_vm_ops;
  1234. vma->vm_private_data = frame;
  1235. gspca_vm_open(vma);
  1236. ret = 0;
  1237. out:
  1238. mutex_unlock(&gspca_dev->queue_lock);
  1239. return ret;
  1240. }
  1241. /*
  1242. * wait for a video frame
  1243. *
  1244. * If a frame is ready, its index is returned.
  1245. */
  1246. static int frame_wait(struct gspca_dev *gspca_dev,
  1247. int nonblock_ing)
  1248. {
  1249. struct gspca_frame *frame;
  1250. int i, j, ret;
  1251. /* check if a frame is ready */
  1252. i = gspca_dev->fr_o;
  1253. j = gspca_dev->fr_queue[i];
  1254. frame = &gspca_dev->frame[j];
  1255. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
  1256. atomic_dec(&gspca_dev->nevent);
  1257. goto ok;
  1258. }
  1259. if (nonblock_ing) /* no frame yet */
  1260. return -EAGAIN;
  1261. /* wait till a frame is ready */
  1262. for (;;) {
  1263. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1264. atomic_read(&gspca_dev->nevent) > 0,
  1265. msecs_to_jiffies(3000));
  1266. if (ret <= 0) {
  1267. if (ret < 0)
  1268. return ret; /* interrupt */
  1269. return -EIO; /* timeout */
  1270. }
  1271. atomic_dec(&gspca_dev->nevent);
  1272. if (!gspca_dev->streaming || !gspca_dev->present)
  1273. return -EIO;
  1274. i = gspca_dev->fr_o;
  1275. j = gspca_dev->fr_queue[i];
  1276. frame = &gspca_dev->frame[j];
  1277. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1278. break;
  1279. }
  1280. ok:
  1281. gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
  1282. PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
  1283. gspca_dev->fr_q,
  1284. gspca_dev->fr_i,
  1285. gspca_dev->fr_o);
  1286. if (gspca_dev->sd_desc->dq_callback) {
  1287. mutex_lock(&gspca_dev->usb_lock);
  1288. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1289. mutex_unlock(&gspca_dev->usb_lock);
  1290. }
  1291. return j;
  1292. }
  1293. /*
  1294. * dequeue a video buffer
  1295. *
  1296. * If nonblock_ing is false, block until a buffer is available.
  1297. */
  1298. static int vidioc_dqbuf(struct file *file, void *priv,
  1299. struct v4l2_buffer *v4l2_buf)
  1300. {
  1301. struct gspca_dev *gspca_dev = priv;
  1302. struct gspca_frame *frame;
  1303. int i, ret;
  1304. PDEBUG(D_FRAM, "dqbuf");
  1305. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1306. return -EINVAL;
  1307. if (v4l2_buf->memory != gspca_dev->memory)
  1308. return -EINVAL;
  1309. /* if not streaming, be sure the application will not loop forever */
  1310. if (!(file->f_flags & O_NONBLOCK)
  1311. && !gspca_dev->streaming && gspca_dev->users == 1)
  1312. return -EINVAL;
  1313. /* only the capturing file may dequeue */
  1314. if (gspca_dev->capt_file != file)
  1315. return -EINVAL;
  1316. /* only one dequeue / read at a time */
  1317. if (mutex_lock_interruptible(&gspca_dev->read_lock))
  1318. return -ERESTARTSYS;
  1319. ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
  1320. if (ret < 0)
  1321. goto out;
  1322. i = ret; /* frame index */
  1323. frame = &gspca_dev->frame[i];
  1324. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1325. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1326. frame->data,
  1327. frame->v4l2_buf.bytesused)) {
  1328. PDEBUG(D_ERR|D_STREAM,
  1329. "dqbuf cp to user failed");
  1330. ret = -EFAULT;
  1331. goto out;
  1332. }
  1333. }
  1334. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1335. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1336. PDEBUG(D_FRAM, "dqbuf %d", i);
  1337. ret = 0;
  1338. out:
  1339. mutex_unlock(&gspca_dev->read_lock);
  1340. return ret;
  1341. }
  1342. /*
  1343. * queue a video buffer
  1344. *
  1345. * Attempting to queue a buffer that has already been
  1346. * queued will return -EINVAL.
  1347. */
  1348. static int vidioc_qbuf(struct file *file, void *priv,
  1349. struct v4l2_buffer *v4l2_buf)
  1350. {
  1351. struct gspca_dev *gspca_dev = priv;
  1352. struct gspca_frame *frame;
  1353. int i, index, ret;
  1354. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1355. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1356. return -EINVAL;
  1357. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1358. return -ERESTARTSYS;
  1359. index = v4l2_buf->index;
  1360. if ((unsigned) index >= gspca_dev->nframes) {
  1361. PDEBUG(D_FRAM,
  1362. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1363. ret = -EINVAL;
  1364. goto out;
  1365. }
  1366. if (v4l2_buf->memory != gspca_dev->memory) {
  1367. PDEBUG(D_FRAM, "qbuf bad memory type");
  1368. ret = -EINVAL;
  1369. goto out;
  1370. }
  1371. frame = &gspca_dev->frame[index];
  1372. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1373. PDEBUG(D_FRAM, "qbuf bad state");
  1374. ret = -EINVAL;
  1375. goto out;
  1376. }
  1377. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1378. /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
  1379. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1380. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1381. frame->v4l2_buf.length = v4l2_buf->length;
  1382. }
  1383. /* put the buffer in the 'queued' queue */
  1384. i = gspca_dev->fr_q;
  1385. gspca_dev->fr_queue[i] = index;
  1386. gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
  1387. PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
  1388. gspca_dev->fr_q,
  1389. gspca_dev->fr_i,
  1390. gspca_dev->fr_o);
  1391. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1392. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1393. ret = 0;
  1394. out:
  1395. mutex_unlock(&gspca_dev->queue_lock);
  1396. return ret;
  1397. }
  1398. /*
  1399. * allocate the resources for read()
  1400. */
  1401. static int read_alloc(struct gspca_dev *gspca_dev,
  1402. struct file *file)
  1403. {
  1404. struct v4l2_buffer v4l2_buf;
  1405. int i, ret;
  1406. PDEBUG(D_STREAM, "read alloc");
  1407. if (gspca_dev->nframes == 0) {
  1408. struct v4l2_requestbuffers rb;
  1409. memset(&rb, 0, sizeof rb);
  1410. rb.count = gspca_dev->nbufread;
  1411. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1412. rb.memory = GSPCA_MEMORY_READ;
  1413. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1414. if (ret != 0) {
  1415. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1416. return ret;
  1417. }
  1418. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1419. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1420. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1421. for (i = 0; i < gspca_dev->nbufread; i++) {
  1422. v4l2_buf.index = i;
  1423. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1424. if (ret != 0) {
  1425. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1426. return ret;
  1427. }
  1428. }
  1429. gspca_dev->memory = GSPCA_MEMORY_READ;
  1430. }
  1431. /* start streaming */
  1432. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1433. if (ret != 0)
  1434. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1435. return ret;
  1436. }
  1437. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1438. {
  1439. struct gspca_dev *gspca_dev = file->private_data;
  1440. int i, ret;
  1441. PDEBUG(D_FRAM, "poll");
  1442. poll_wait(file, &gspca_dev->wq, wait);
  1443. if (!gspca_dev->present)
  1444. return POLLERR;
  1445. /* if reqbufs is not done, the user would use read() */
  1446. if (gspca_dev->nframes == 0) {
  1447. if (gspca_dev->memory != GSPCA_MEMORY_NO)
  1448. return POLLERR; /* not the 1st time */
  1449. ret = read_alloc(gspca_dev, file);
  1450. if (ret != 0)
  1451. return POLLERR;
  1452. }
  1453. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
  1454. return POLLERR;
  1455. if (!gspca_dev->present) {
  1456. ret = POLLERR;
  1457. goto out;
  1458. }
  1459. /* check the next incoming buffer */
  1460. i = gspca_dev->fr_o;
  1461. i = gspca_dev->fr_queue[i];
  1462. if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1463. ret = POLLIN | POLLRDNORM; /* something to read */
  1464. else
  1465. ret = 0;
  1466. out:
  1467. mutex_unlock(&gspca_dev->queue_lock);
  1468. return ret;
  1469. }
  1470. static ssize_t dev_read(struct file *file, char __user *data,
  1471. size_t count, loff_t *ppos)
  1472. {
  1473. struct gspca_dev *gspca_dev = file->private_data;
  1474. struct gspca_frame *frame;
  1475. struct v4l2_buffer v4l2_buf;
  1476. struct timeval timestamp;
  1477. int n, ret, ret2;
  1478. PDEBUG(D_FRAM, "read (%zd)", count);
  1479. if (!gspca_dev->present)
  1480. return -ENODEV;
  1481. switch (gspca_dev->memory) {
  1482. case GSPCA_MEMORY_NO: /* first time */
  1483. ret = read_alloc(gspca_dev, file);
  1484. if (ret != 0)
  1485. return ret;
  1486. break;
  1487. case GSPCA_MEMORY_READ:
  1488. if (gspca_dev->capt_file == file)
  1489. break;
  1490. /* fall thru */
  1491. default:
  1492. return -EINVAL;
  1493. }
  1494. /* get a frame */
  1495. jiffies_to_timeval(get_jiffies_64(), &timestamp);
  1496. timestamp.tv_sec--;
  1497. n = 2;
  1498. for (;;) {
  1499. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1500. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1501. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1502. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1503. if (ret != 0) {
  1504. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1505. return ret;
  1506. }
  1507. /* if the process slept for more than 1 second,
  1508. * get anewer frame */
  1509. frame = &gspca_dev->frame[v4l2_buf.index];
  1510. if (--n < 0)
  1511. break; /* avoid infinite loop */
  1512. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1513. break;
  1514. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1515. if (ret != 0) {
  1516. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1517. return ret;
  1518. }
  1519. }
  1520. /* copy the frame */
  1521. if (count > frame->v4l2_buf.bytesused)
  1522. count = frame->v4l2_buf.bytesused;
  1523. ret = copy_to_user(data, frame->data, count);
  1524. if (ret != 0) {
  1525. PDEBUG(D_ERR|D_STREAM,
  1526. "read cp to user lack %d / %zd", ret, count);
  1527. ret = -EFAULT;
  1528. goto out;
  1529. }
  1530. ret = count;
  1531. out:
  1532. /* in each case, requeue the buffer */
  1533. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1534. if (ret2 != 0)
  1535. return ret2;
  1536. return ret;
  1537. }
  1538. static void dev_release(struct video_device *vfd)
  1539. {
  1540. /* nothing */
  1541. }
  1542. static struct file_operations dev_fops = {
  1543. .owner = THIS_MODULE,
  1544. .open = dev_open,
  1545. .release = dev_close,
  1546. .read = dev_read,
  1547. .mmap = dev_mmap,
  1548. .ioctl = video_ioctl2,
  1549. #ifdef CONFIG_COMPAT
  1550. .compat_ioctl = v4l_compat_ioctl32,
  1551. #endif
  1552. .llseek = no_llseek,
  1553. .poll = dev_poll,
  1554. };
  1555. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1556. .vidioc_querycap = vidioc_querycap,
  1557. .vidioc_dqbuf = vidioc_dqbuf,
  1558. .vidioc_qbuf = vidioc_qbuf,
  1559. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1560. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1561. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1562. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1563. .vidioc_streamon = vidioc_streamon,
  1564. .vidioc_queryctrl = vidioc_queryctrl,
  1565. .vidioc_g_ctrl = vidioc_g_ctrl,
  1566. .vidioc_s_ctrl = vidioc_s_ctrl,
  1567. .vidioc_querymenu = vidioc_querymenu,
  1568. .vidioc_enum_input = vidioc_enum_input,
  1569. .vidioc_g_input = vidioc_g_input,
  1570. .vidioc_s_input = vidioc_s_input,
  1571. .vidioc_reqbufs = vidioc_reqbufs,
  1572. .vidioc_querybuf = vidioc_querybuf,
  1573. .vidioc_streamoff = vidioc_streamoff,
  1574. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1575. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1576. .vidioc_g_parm = vidioc_g_parm,
  1577. .vidioc_s_parm = vidioc_s_parm,
  1578. .vidioc_s_std = vidioc_s_std,
  1579. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1580. .vidiocgmbuf = vidiocgmbuf,
  1581. #endif
  1582. };
  1583. static struct video_device gspca_template = {
  1584. .name = "gspca main driver",
  1585. .fops = &dev_fops,
  1586. .ioctl_ops = &dev_ioctl_ops,
  1587. .release = dev_release, /* mandatory */
  1588. .minor = -1,
  1589. };
  1590. /*
  1591. * probe and create a new gspca device
  1592. *
  1593. * This function must be called by the sub-driver when it is
  1594. * called for probing a new device.
  1595. */
  1596. int gspca_dev_probe(struct usb_interface *intf,
  1597. const struct usb_device_id *id,
  1598. const struct sd_desc *sd_desc,
  1599. int dev_size,
  1600. struct module *module)
  1601. {
  1602. struct usb_interface_descriptor *interface;
  1603. struct gspca_dev *gspca_dev;
  1604. struct usb_device *dev = interface_to_usbdev(intf);
  1605. int ret;
  1606. PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
  1607. /* we don't handle multi-config cameras */
  1608. if (dev->descriptor.bNumConfigurations != 1)
  1609. return -ENODEV;
  1610. interface = &intf->cur_altsetting->desc;
  1611. if (interface->bInterfaceNumber > 0)
  1612. return -ENODEV;
  1613. /* create the device */
  1614. if (dev_size < sizeof *gspca_dev)
  1615. dev_size = sizeof *gspca_dev;
  1616. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1617. if (gspca_dev == NULL) {
  1618. err("couldn't kzalloc gspca struct");
  1619. return -EIO;
  1620. }
  1621. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1622. if (!gspca_dev->usb_buf) {
  1623. err("out of memory");
  1624. ret = -EIO;
  1625. goto out;
  1626. }
  1627. gspca_dev->dev = dev;
  1628. gspca_dev->iface = interface->bInterfaceNumber;
  1629. gspca_dev->nbalt = intf->num_altsetting;
  1630. gspca_dev->sd_desc = sd_desc;
  1631. /* gspca_dev->users = 0; (done by kzalloc) */
  1632. gspca_dev->nbufread = 2;
  1633. /* configure the subdriver and initialize the USB device */
  1634. ret = gspca_dev->sd_desc->config(gspca_dev, id);
  1635. if (ret < 0)
  1636. goto out;
  1637. ret = gspca_dev->sd_desc->init(gspca_dev);
  1638. if (ret < 0)
  1639. goto out;
  1640. ret = gspca_set_alt0(gspca_dev);
  1641. if (ret < 0)
  1642. goto out;
  1643. gspca_set_default_mode(gspca_dev);
  1644. mutex_init(&gspca_dev->usb_lock);
  1645. mutex_init(&gspca_dev->read_lock);
  1646. mutex_init(&gspca_dev->queue_lock);
  1647. init_waitqueue_head(&gspca_dev->wq);
  1648. /* init video stuff */
  1649. memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
  1650. gspca_dev->vdev.parent = &dev->dev;
  1651. memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
  1652. gspca_dev->vdev.fops = &gspca_dev->fops;
  1653. gspca_dev->fops.owner = module; /* module protection */
  1654. gspca_dev->present = 1;
  1655. ret = video_register_device(&gspca_dev->vdev,
  1656. VFL_TYPE_GRABBER,
  1657. video_nr);
  1658. if (ret < 0) {
  1659. err("video_register_device err %d", ret);
  1660. goto out;
  1661. }
  1662. usb_set_intfdata(intf, gspca_dev);
  1663. PDEBUG(D_PROBE, "probe ok");
  1664. return 0;
  1665. out:
  1666. kfree(gspca_dev->usb_buf);
  1667. kfree(gspca_dev);
  1668. return ret;
  1669. }
  1670. EXPORT_SYMBOL(gspca_dev_probe);
  1671. /*
  1672. * USB disconnection
  1673. *
  1674. * This function must be called by the sub-driver
  1675. * when the device disconnects, after the specific resources are freed.
  1676. */
  1677. void gspca_disconnect(struct usb_interface *intf)
  1678. {
  1679. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1680. if (!gspca_dev)
  1681. return;
  1682. gspca_dev->present = 0;
  1683. mutex_lock(&gspca_dev->queue_lock);
  1684. mutex_lock(&gspca_dev->usb_lock);
  1685. gspca_dev->streaming = 0;
  1686. destroy_urbs(gspca_dev);
  1687. mutex_unlock(&gspca_dev->usb_lock);
  1688. mutex_unlock(&gspca_dev->queue_lock);
  1689. while (gspca_dev->users != 0) { /* wait until fully closed */
  1690. atomic_inc(&gspca_dev->nevent);
  1691. wake_up_interruptible(&gspca_dev->wq); /* wake processes */
  1692. schedule();
  1693. }
  1694. /* We don't want people trying to open up the device */
  1695. video_unregister_device(&gspca_dev->vdev);
  1696. /* Free the memory */
  1697. kfree(gspca_dev->usb_buf);
  1698. kfree(gspca_dev);
  1699. PDEBUG(D_PROBE, "disconnect complete");
  1700. }
  1701. EXPORT_SYMBOL(gspca_disconnect);
  1702. #ifdef CONFIG_PM
  1703. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1704. {
  1705. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1706. if (!gspca_dev->streaming)
  1707. return 0;
  1708. gspca_dev->frozen = 1; /* avoid urb error messages */
  1709. if (gspca_dev->sd_desc->stopN)
  1710. gspca_dev->sd_desc->stopN(gspca_dev);
  1711. destroy_urbs(gspca_dev);
  1712. gspca_set_alt0(gspca_dev);
  1713. if (gspca_dev->sd_desc->stop0)
  1714. gspca_dev->sd_desc->stop0(gspca_dev);
  1715. return 0;
  1716. }
  1717. EXPORT_SYMBOL(gspca_suspend);
  1718. int gspca_resume(struct usb_interface *intf)
  1719. {
  1720. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1721. gspca_dev->frozen = 0;
  1722. gspca_dev->sd_desc->init(gspca_dev);
  1723. if (gspca_dev->streaming)
  1724. return gspca_init_transfer(gspca_dev);
  1725. return 0;
  1726. }
  1727. EXPORT_SYMBOL(gspca_resume);
  1728. #endif
  1729. /* -- cam driver utility functions -- */
  1730. /* auto gain and exposure algorithm based on the knee algorithm described here:
  1731. http://ytse.tricolour.net/docs/LowLightOptimization.html
  1732. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  1733. where changed. */
  1734. int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
  1735. int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
  1736. {
  1737. int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
  1738. const struct ctrl *gain_ctrl = NULL;
  1739. const struct ctrl *exposure_ctrl = NULL;
  1740. const struct ctrl *autogain_ctrl = NULL;
  1741. int retval = 0;
  1742. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1743. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
  1744. gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1745. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
  1746. exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1747. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
  1748. autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1749. }
  1750. if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
  1751. PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
  1752. "on cam without (auto)gain/exposure");
  1753. return 0;
  1754. }
  1755. if (gain_ctrl->get(gspca_dev, &gain) ||
  1756. exposure_ctrl->get(gspca_dev, &exposure) ||
  1757. autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
  1758. return 0;
  1759. orig_gain = gain;
  1760. orig_exposure = exposure;
  1761. /* If we are of a multiple of deadzone, do multiple steps to reach the
  1762. desired lumination fast (with the risc of a slight overshoot) */
  1763. steps = abs(desired_avg_lum - avg_lum) / deadzone;
  1764. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
  1765. avg_lum, desired_avg_lum, steps);
  1766. for (i = 0; i < steps; i++) {
  1767. if (avg_lum > desired_avg_lum) {
  1768. if (gain > gain_knee)
  1769. gain--;
  1770. else if (exposure > exposure_knee)
  1771. exposure--;
  1772. else if (gain > gain_ctrl->qctrl.default_value)
  1773. gain--;
  1774. else if (exposure > exposure_ctrl->qctrl.minimum)
  1775. exposure--;
  1776. else if (gain > gain_ctrl->qctrl.minimum)
  1777. gain--;
  1778. else
  1779. break;
  1780. } else {
  1781. if (gain < gain_ctrl->qctrl.default_value)
  1782. gain++;
  1783. else if (exposure < exposure_knee)
  1784. exposure++;
  1785. else if (gain < gain_knee)
  1786. gain++;
  1787. else if (exposure < exposure_ctrl->qctrl.maximum)
  1788. exposure++;
  1789. else if (gain < gain_ctrl->qctrl.maximum)
  1790. gain++;
  1791. else
  1792. break;
  1793. }
  1794. }
  1795. if (gain != orig_gain) {
  1796. gain_ctrl->set(gspca_dev, gain);
  1797. retval = 1;
  1798. }
  1799. if (exposure != orig_exposure) {
  1800. exposure_ctrl->set(gspca_dev, exposure);
  1801. retval = 1;
  1802. }
  1803. return retval;
  1804. }
  1805. EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
  1806. /* -- module insert / remove -- */
  1807. static int __init gspca_init(void)
  1808. {
  1809. info("main v%d.%d.%d registered",
  1810. (DRIVER_VERSION_NUMBER >> 16) & 0xff,
  1811. (DRIVER_VERSION_NUMBER >> 8) & 0xff,
  1812. DRIVER_VERSION_NUMBER & 0xff);
  1813. return 0;
  1814. }
  1815. static void __exit gspca_exit(void)
  1816. {
  1817. info("main deregistered");
  1818. }
  1819. module_init(gspca_init);
  1820. module_exit(gspca_exit);
  1821. #ifdef GSPCA_DEBUG
  1822. module_param_named(debug, gspca_debug, int, 0644);
  1823. MODULE_PARM_DESC(debug,
  1824. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  1825. " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
  1826. " 0x0100: v4l2");
  1827. #endif