gspca.c 49 KB

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