gspca.c 50 KB

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