gspca.c 51 KB

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