gspca.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  1. /*
  2. * Main USB camera driver
  3. *
  4. * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
  5. *
  6. * Camera button input handling by Márton Németh
  7. * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #define GSPCA_VERSION "2.14.0"
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/io.h>
  30. #include <asm/page.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/ktime.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-ctrls.h>
  35. #include <media/v4l2-fh.h>
  36. #include <media/v4l2-event.h>
  37. #include "gspca.h"
  38. #if IS_ENABLED(CONFIG_INPUT)
  39. #include <linux/input.h>
  40. #include <linux/usb/input.h>
  41. #endif
  42. /* global values */
  43. #define DEF_NURBS 3 /* default number of URBs */
  44. #if DEF_NURBS > MAX_NURBS
  45. #error "DEF_NURBS too big"
  46. #endif
  47. MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
  48. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  49. MODULE_LICENSE("GPL");
  50. MODULE_VERSION(GSPCA_VERSION);
  51. int gspca_debug;
  52. EXPORT_SYMBOL(gspca_debug);
  53. static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
  54. __u32 pixfmt, int w, int h)
  55. {
  56. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  57. PDEBUG(debug, "%s %c%c%c%c %dx%d",
  58. txt,
  59. pixfmt & 0xff,
  60. (pixfmt >> 8) & 0xff,
  61. (pixfmt >> 16) & 0xff,
  62. pixfmt >> 24,
  63. w, h);
  64. } else {
  65. PDEBUG(debug, "%s 0x%08x %dx%d",
  66. txt,
  67. pixfmt,
  68. w, h);
  69. }
  70. }
  71. /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
  72. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  73. #define GSPCA_MEMORY_READ 7
  74. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  75. /*
  76. * VMA operations.
  77. */
  78. static void gspca_vm_open(struct vm_area_struct *vma)
  79. {
  80. struct gspca_frame *frame = vma->vm_private_data;
  81. frame->vma_use_count++;
  82. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  83. }
  84. static void gspca_vm_close(struct vm_area_struct *vma)
  85. {
  86. struct gspca_frame *frame = vma->vm_private_data;
  87. if (--frame->vma_use_count <= 0)
  88. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  89. }
  90. static const struct vm_operations_struct gspca_vm_ops = {
  91. .open = gspca_vm_open,
  92. .close = gspca_vm_close,
  93. };
  94. /*
  95. * Input and interrupt endpoint handling functions
  96. */
  97. #if IS_ENABLED(CONFIG_INPUT)
  98. static void int_irq(struct urb *urb)
  99. {
  100. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  101. int ret;
  102. ret = urb->status;
  103. switch (ret) {
  104. case 0:
  105. if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
  106. urb->transfer_buffer, urb->actual_length) < 0) {
  107. PERR("Unknown packet received");
  108. }
  109. break;
  110. case -ENOENT:
  111. case -ECONNRESET:
  112. case -ENODEV:
  113. case -ESHUTDOWN:
  114. /* Stop is requested either by software or hardware is gone,
  115. * keep the ret value non-zero and don't resubmit later.
  116. */
  117. break;
  118. default:
  119. PERR("URB error %i, resubmitting", urb->status);
  120. urb->status = 0;
  121. ret = 0;
  122. }
  123. if (ret == 0) {
  124. ret = usb_submit_urb(urb, GFP_ATOMIC);
  125. if (ret < 0)
  126. pr_err("Resubmit URB failed with error %i\n", ret);
  127. }
  128. }
  129. static int gspca_input_connect(struct gspca_dev *dev)
  130. {
  131. struct input_dev *input_dev;
  132. int err = 0;
  133. dev->input_dev = NULL;
  134. if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
  135. input_dev = input_allocate_device();
  136. if (!input_dev)
  137. return -ENOMEM;
  138. usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
  139. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  140. input_dev->name = dev->sd_desc->name;
  141. input_dev->phys = dev->phys;
  142. usb_to_input_id(dev->dev, &input_dev->id);
  143. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  144. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  145. input_dev->dev.parent = &dev->dev->dev;
  146. err = input_register_device(input_dev);
  147. if (err) {
  148. pr_err("Input device registration failed with error %i\n",
  149. err);
  150. input_dev->dev.parent = NULL;
  151. input_free_device(input_dev);
  152. } else {
  153. dev->input_dev = input_dev;
  154. }
  155. }
  156. return err;
  157. }
  158. static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
  159. struct usb_endpoint_descriptor *ep)
  160. {
  161. unsigned int buffer_len;
  162. int interval;
  163. struct urb *urb;
  164. struct usb_device *dev;
  165. void *buffer = NULL;
  166. int ret = -EINVAL;
  167. buffer_len = le16_to_cpu(ep->wMaxPacketSize);
  168. interval = ep->bInterval;
  169. PDEBUG(D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u",
  170. ep->bEndpointAddress, buffer_len, interval);
  171. dev = gspca_dev->dev;
  172. urb = usb_alloc_urb(0, GFP_KERNEL);
  173. if (!urb) {
  174. ret = -ENOMEM;
  175. goto error;
  176. }
  177. buffer = usb_alloc_coherent(dev, buffer_len,
  178. GFP_KERNEL, &urb->transfer_dma);
  179. if (!buffer) {
  180. ret = -ENOMEM;
  181. goto error_buffer;
  182. }
  183. usb_fill_int_urb(urb, dev,
  184. usb_rcvintpipe(dev, ep->bEndpointAddress),
  185. buffer, buffer_len,
  186. int_irq, (void *)gspca_dev, interval);
  187. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  188. ret = usb_submit_urb(urb, GFP_KERNEL);
  189. if (ret < 0) {
  190. PERR("submit int URB failed with error %i", ret);
  191. goto error_submit;
  192. }
  193. gspca_dev->int_urb = urb;
  194. return ret;
  195. error_submit:
  196. usb_free_coherent(dev,
  197. urb->transfer_buffer_length,
  198. urb->transfer_buffer,
  199. urb->transfer_dma);
  200. error_buffer:
  201. usb_free_urb(urb);
  202. error:
  203. return ret;
  204. }
  205. static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  206. {
  207. struct usb_interface *intf;
  208. struct usb_host_interface *intf_desc;
  209. struct usb_endpoint_descriptor *ep;
  210. int i;
  211. if (gspca_dev->sd_desc->int_pkt_scan) {
  212. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  213. intf_desc = intf->cur_altsetting;
  214. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  215. ep = &intf_desc->endpoint[i].desc;
  216. if (usb_endpoint_dir_in(ep) &&
  217. usb_endpoint_xfer_int(ep)) {
  218. alloc_and_submit_int_urb(gspca_dev, ep);
  219. break;
  220. }
  221. }
  222. }
  223. }
  224. static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  225. {
  226. struct urb *urb;
  227. urb = gspca_dev->int_urb;
  228. if (urb) {
  229. gspca_dev->int_urb = NULL;
  230. usb_kill_urb(urb);
  231. usb_free_coherent(gspca_dev->dev,
  232. urb->transfer_buffer_length,
  233. urb->transfer_buffer,
  234. urb->transfer_dma);
  235. usb_free_urb(urb);
  236. }
  237. }
  238. #else
  239. static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  240. {
  241. }
  242. static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  243. {
  244. }
  245. static inline int gspca_input_connect(struct gspca_dev *dev)
  246. {
  247. return 0;
  248. }
  249. #endif
  250. /*
  251. * fill a video frame from an URB and resubmit
  252. */
  253. static void fill_frame(struct gspca_dev *gspca_dev,
  254. struct urb *urb)
  255. {
  256. u8 *data; /* address of data in the iso message */
  257. int i, len, st;
  258. cam_pkt_op pkt_scan;
  259. if (urb->status != 0) {
  260. if (urb->status == -ESHUTDOWN)
  261. return; /* disconnection */
  262. #ifdef CONFIG_PM
  263. if (gspca_dev->frozen)
  264. return;
  265. #endif
  266. PERR("urb status: %d", urb->status);
  267. urb->status = 0;
  268. goto resubmit;
  269. }
  270. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  271. for (i = 0; i < urb->number_of_packets; i++) {
  272. len = urb->iso_frame_desc[i].actual_length;
  273. /* check the packet status and length */
  274. st = urb->iso_frame_desc[i].status;
  275. if (st) {
  276. pr_err("ISOC data error: [%d] len=%d, status=%d\n",
  277. i, len, st);
  278. gspca_dev->last_packet_type = DISCARD_PACKET;
  279. continue;
  280. }
  281. if (len == 0) {
  282. if (gspca_dev->empty_packet == 0)
  283. gspca_dev->empty_packet = 1;
  284. continue;
  285. }
  286. /* let the packet be analyzed by the subdriver */
  287. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  288. i, urb->iso_frame_desc[i].offset, len);
  289. data = (u8 *) urb->transfer_buffer
  290. + urb->iso_frame_desc[i].offset;
  291. pkt_scan(gspca_dev, data, len);
  292. }
  293. resubmit:
  294. /* resubmit the URB */
  295. st = usb_submit_urb(urb, GFP_ATOMIC);
  296. if (st < 0)
  297. pr_err("usb_submit_urb() ret %d\n", st);
  298. }
  299. /*
  300. * ISOC message interrupt from the USB device
  301. *
  302. * Analyse each packet and call the subdriver for copy to the frame buffer.
  303. */
  304. static void isoc_irq(struct urb *urb)
  305. {
  306. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  307. PDEBUG(D_PACK, "isoc irq");
  308. if (!gspca_dev->streaming)
  309. return;
  310. fill_frame(gspca_dev, urb);
  311. }
  312. /*
  313. * bulk message interrupt from the USB device
  314. */
  315. static void bulk_irq(struct urb *urb)
  316. {
  317. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  318. int st;
  319. PDEBUG(D_PACK, "bulk irq");
  320. if (!gspca_dev->streaming)
  321. return;
  322. switch (urb->status) {
  323. case 0:
  324. break;
  325. case -ESHUTDOWN:
  326. return; /* disconnection */
  327. default:
  328. #ifdef CONFIG_PM
  329. if (gspca_dev->frozen)
  330. return;
  331. #endif
  332. PERR("urb status: %d", urb->status);
  333. urb->status = 0;
  334. goto resubmit;
  335. }
  336. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  337. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  338. urb->transfer_buffer,
  339. urb->actual_length);
  340. resubmit:
  341. /* resubmit the URB */
  342. if (gspca_dev->cam.bulk_nurbs != 0) {
  343. st = usb_submit_urb(urb, GFP_ATOMIC);
  344. if (st < 0)
  345. pr_err("usb_submit_urb() ret %d\n", st);
  346. }
  347. }
  348. /*
  349. * add data to the current frame
  350. *
  351. * This function is called by the subdrivers at interrupt level.
  352. *
  353. * To build a frame, these ones must add
  354. * - one FIRST_PACKET
  355. * - 0 or many INTER_PACKETs
  356. * - one LAST_PACKET
  357. * DISCARD_PACKET invalidates the whole frame.
  358. */
  359. void gspca_frame_add(struct gspca_dev *gspca_dev,
  360. enum gspca_packet_type packet_type,
  361. const u8 *data,
  362. int len)
  363. {
  364. struct gspca_frame *frame;
  365. int i, j;
  366. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  367. if (packet_type == FIRST_PACKET) {
  368. i = atomic_read(&gspca_dev->fr_i);
  369. /* if there are no queued buffer, discard the whole frame */
  370. if (i == atomic_read(&gspca_dev->fr_q)) {
  371. gspca_dev->last_packet_type = DISCARD_PACKET;
  372. gspca_dev->sequence++;
  373. return;
  374. }
  375. j = gspca_dev->fr_queue[i];
  376. frame = &gspca_dev->frame[j];
  377. v4l2_get_timestamp(&frame->v4l2_buf.timestamp);
  378. frame->v4l2_buf.sequence = gspca_dev->sequence++;
  379. gspca_dev->image = frame->data;
  380. gspca_dev->image_len = 0;
  381. } else {
  382. switch (gspca_dev->last_packet_type) {
  383. case DISCARD_PACKET:
  384. if (packet_type == LAST_PACKET) {
  385. gspca_dev->last_packet_type = packet_type;
  386. gspca_dev->image = NULL;
  387. gspca_dev->image_len = 0;
  388. }
  389. return;
  390. case LAST_PACKET:
  391. return;
  392. }
  393. }
  394. /* append the packet to the frame buffer */
  395. if (len > 0) {
  396. if (gspca_dev->image_len + len > gspca_dev->frsz) {
  397. PERR("frame overflow %d > %d",
  398. gspca_dev->image_len + len,
  399. gspca_dev->frsz);
  400. packet_type = DISCARD_PACKET;
  401. } else {
  402. /* !! image is NULL only when last pkt is LAST or DISCARD
  403. if (gspca_dev->image == NULL) {
  404. pr_err("gspca_frame_add() image == NULL\n");
  405. return;
  406. }
  407. */
  408. memcpy(gspca_dev->image + gspca_dev->image_len,
  409. data, len);
  410. gspca_dev->image_len += len;
  411. }
  412. }
  413. gspca_dev->last_packet_type = packet_type;
  414. /* if last packet, invalidate packet concatenation until
  415. * next first packet, wake up the application and advance
  416. * in the queue */
  417. if (packet_type == LAST_PACKET) {
  418. i = atomic_read(&gspca_dev->fr_i);
  419. j = gspca_dev->fr_queue[i];
  420. frame = &gspca_dev->frame[j];
  421. frame->v4l2_buf.bytesused = gspca_dev->image_len;
  422. frame->v4l2_buf.flags = (frame->v4l2_buf.flags
  423. | V4L2_BUF_FLAG_DONE)
  424. & ~V4L2_BUF_FLAG_QUEUED;
  425. i = (i + 1) % GSPCA_MAX_FRAMES;
  426. atomic_set(&gspca_dev->fr_i, i);
  427. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  428. PDEBUG(D_FRAM, "frame complete len:%d",
  429. frame->v4l2_buf.bytesused);
  430. gspca_dev->image = NULL;
  431. gspca_dev->image_len = 0;
  432. }
  433. }
  434. EXPORT_SYMBOL(gspca_frame_add);
  435. static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
  436. enum v4l2_memory memory, unsigned int count)
  437. {
  438. struct gspca_frame *frame;
  439. unsigned int frsz;
  440. int i;
  441. frsz = gspca_dev->pixfmt.sizeimage;
  442. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  443. frsz = PAGE_ALIGN(frsz);
  444. if (count >= GSPCA_MAX_FRAMES)
  445. count = GSPCA_MAX_FRAMES - 1;
  446. gspca_dev->frbuf = vmalloc_32(frsz * count);
  447. if (!gspca_dev->frbuf) {
  448. pr_err("frame alloc failed\n");
  449. return -ENOMEM;
  450. }
  451. gspca_dev->capt_file = file;
  452. gspca_dev->memory = memory;
  453. gspca_dev->frsz = frsz;
  454. gspca_dev->nframes = count;
  455. for (i = 0; i < count; i++) {
  456. frame = &gspca_dev->frame[i];
  457. frame->v4l2_buf.index = i;
  458. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  459. frame->v4l2_buf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  460. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  461. frame->v4l2_buf.length = frsz;
  462. frame->v4l2_buf.memory = memory;
  463. frame->v4l2_buf.sequence = 0;
  464. frame->data = gspca_dev->frbuf + i * frsz;
  465. frame->v4l2_buf.m.offset = i * frsz;
  466. }
  467. atomic_set(&gspca_dev->fr_q, 0);
  468. atomic_set(&gspca_dev->fr_i, 0);
  469. gspca_dev->fr_o = 0;
  470. return 0;
  471. }
  472. static void frame_free(struct gspca_dev *gspca_dev)
  473. {
  474. int i;
  475. PDEBUG(D_STREAM, "frame free");
  476. if (gspca_dev->frbuf != NULL) {
  477. vfree(gspca_dev->frbuf);
  478. gspca_dev->frbuf = NULL;
  479. for (i = 0; i < gspca_dev->nframes; i++)
  480. gspca_dev->frame[i].data = NULL;
  481. }
  482. gspca_dev->nframes = 0;
  483. gspca_dev->frsz = 0;
  484. gspca_dev->capt_file = NULL;
  485. gspca_dev->memory = GSPCA_MEMORY_NO;
  486. }
  487. static void destroy_urbs(struct gspca_dev *gspca_dev)
  488. {
  489. struct urb *urb;
  490. unsigned int i;
  491. PDEBUG(D_STREAM, "kill transfer");
  492. for (i = 0; i < MAX_NURBS; i++) {
  493. urb = gspca_dev->urb[i];
  494. if (urb == NULL)
  495. break;
  496. gspca_dev->urb[i] = NULL;
  497. usb_kill_urb(urb);
  498. usb_free_coherent(gspca_dev->dev,
  499. urb->transfer_buffer_length,
  500. urb->transfer_buffer,
  501. urb->transfer_dma);
  502. usb_free_urb(urb);
  503. }
  504. }
  505. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  506. {
  507. int ret;
  508. if (gspca_dev->alt == 0)
  509. return 0;
  510. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  511. if (ret < 0)
  512. pr_err("set alt 0 err %d\n", ret);
  513. return ret;
  514. }
  515. /* Note: both the queue and the usb locks should be held when calling this */
  516. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  517. {
  518. gspca_dev->streaming = 0;
  519. gspca_dev->usb_err = 0;
  520. if (gspca_dev->sd_desc->stopN)
  521. gspca_dev->sd_desc->stopN(gspca_dev);
  522. destroy_urbs(gspca_dev);
  523. gspca_input_destroy_urb(gspca_dev);
  524. gspca_set_alt0(gspca_dev);
  525. gspca_input_create_urb(gspca_dev);
  526. if (gspca_dev->sd_desc->stop0)
  527. gspca_dev->sd_desc->stop0(gspca_dev);
  528. PDEBUG(D_STREAM, "stream off OK");
  529. }
  530. /*
  531. * look for an input transfer endpoint in an alternate setting.
  532. *
  533. * If xfer_ep is invalid, return the first valid ep found, otherwise
  534. * look for exactly the ep with address equal to xfer_ep.
  535. */
  536. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  537. int xfer, int xfer_ep)
  538. {
  539. struct usb_host_endpoint *ep;
  540. int i, attr;
  541. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  542. ep = &alt->endpoint[i];
  543. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  544. if (attr == xfer
  545. && ep->desc.wMaxPacketSize != 0
  546. && usb_endpoint_dir_in(&ep->desc)
  547. && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
  548. return ep;
  549. }
  550. return NULL;
  551. }
  552. /* compute the minimum bandwidth for the current transfer */
  553. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  554. {
  555. u32 bandwidth;
  556. /* get the (max) image size */
  557. bandwidth = gspca_dev->pixfmt.sizeimage;
  558. /* if the image is compressed, estimate its mean size */
  559. if (!gspca_dev->cam.needs_full_bandwidth &&
  560. bandwidth < gspca_dev->pixfmt.width *
  561. gspca_dev->pixfmt.height)
  562. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  563. /* estimate the frame rate */
  564. if (gspca_dev->sd_desc->get_streamparm) {
  565. struct v4l2_streamparm parm;
  566. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  567. bandwidth *= parm.parm.capture.timeperframe.denominator;
  568. bandwidth /= parm.parm.capture.timeperframe.numerator;
  569. } else {
  570. /* don't hope more than 15 fps with USB 1.1 and
  571. * image resolution >= 640x480 */
  572. if (gspca_dev->pixfmt.width >= 640
  573. && gspca_dev->dev->speed == USB_SPEED_FULL)
  574. bandwidth *= 15; /* 15 fps */
  575. else
  576. bandwidth *= 30; /* 30 fps */
  577. }
  578. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  579. return bandwidth;
  580. }
  581. /* endpoint table */
  582. #define MAX_ALT 16
  583. struct ep_tb_s {
  584. u32 alt;
  585. u32 bandwidth;
  586. };
  587. /*
  588. * build the table of the endpoints
  589. * and compute the minimum bandwidth for the image transfer
  590. */
  591. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  592. struct usb_interface *intf,
  593. struct ep_tb_s *ep_tb)
  594. {
  595. struct usb_host_endpoint *ep;
  596. int i, j, nbalt, psize, found;
  597. u32 bandwidth, last_bw;
  598. nbalt = intf->num_altsetting;
  599. if (nbalt > MAX_ALT)
  600. nbalt = MAX_ALT; /* fixme: should warn */
  601. /* build the endpoint table */
  602. i = 0;
  603. last_bw = 0;
  604. for (;;) {
  605. ep_tb->bandwidth = 2000 * 2000 * 120;
  606. found = 0;
  607. for (j = 0; j < nbalt; j++) {
  608. ep = alt_xfer(&intf->altsetting[j],
  609. USB_ENDPOINT_XFER_ISOC,
  610. gspca_dev->xfer_ep);
  611. if (ep == NULL)
  612. continue;
  613. if (ep->desc.bInterval == 0) {
  614. pr_err("alt %d iso endp with 0 interval\n", j);
  615. continue;
  616. }
  617. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  618. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  619. bandwidth = psize * 1000;
  620. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  621. || gspca_dev->dev->speed >= USB_SPEED_SUPER)
  622. bandwidth *= 8;
  623. bandwidth /= 1 << (ep->desc.bInterval - 1);
  624. if (bandwidth <= last_bw)
  625. continue;
  626. if (bandwidth < ep_tb->bandwidth) {
  627. ep_tb->bandwidth = bandwidth;
  628. ep_tb->alt = j;
  629. found = 1;
  630. }
  631. }
  632. if (!found)
  633. break;
  634. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  635. ep_tb->alt, ep_tb->bandwidth);
  636. last_bw = ep_tb->bandwidth;
  637. i++;
  638. ep_tb++;
  639. }
  640. /*
  641. * If the camera:
  642. * has a usb audio class interface (a built in usb mic); and
  643. * is a usb 1 full speed device; and
  644. * uses the max full speed iso bandwidth; and
  645. * and has more than 1 alt setting
  646. * then skip the highest alt setting to spare bandwidth for the mic
  647. */
  648. if (gspca_dev->audio &&
  649. gspca_dev->dev->speed == USB_SPEED_FULL &&
  650. last_bw >= 1000000 &&
  651. i > 1) {
  652. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  653. i--;
  654. ep_tb--;
  655. }
  656. /* get the requested bandwidth and start at the highest atlsetting */
  657. bandwidth = which_bandwidth(gspca_dev);
  658. ep_tb--;
  659. while (i > 1) {
  660. ep_tb--;
  661. if (ep_tb->bandwidth < bandwidth)
  662. break;
  663. i--;
  664. }
  665. return i;
  666. }
  667. /*
  668. * create the URBs for image transfer
  669. */
  670. static int create_urbs(struct gspca_dev *gspca_dev,
  671. struct usb_host_endpoint *ep)
  672. {
  673. struct urb *urb;
  674. int n, nurbs, i, psize, npkt, bsize;
  675. /* calculate the packet size and the number of packets */
  676. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  677. if (!gspca_dev->cam.bulk) { /* isoc */
  678. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  679. if (gspca_dev->pkt_size == 0)
  680. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  681. else
  682. psize = gspca_dev->pkt_size;
  683. npkt = gspca_dev->cam.npkt;
  684. if (npkt == 0)
  685. npkt = 32; /* default value */
  686. bsize = psize * npkt;
  687. PDEBUG(D_STREAM,
  688. "isoc %d pkts size %d = bsize:%d",
  689. npkt, psize, bsize);
  690. nurbs = DEF_NURBS;
  691. } else { /* bulk */
  692. npkt = 0;
  693. bsize = gspca_dev->cam.bulk_size;
  694. if (bsize == 0)
  695. bsize = psize;
  696. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  697. if (gspca_dev->cam.bulk_nurbs != 0)
  698. nurbs = gspca_dev->cam.bulk_nurbs;
  699. else
  700. nurbs = 1;
  701. }
  702. for (n = 0; n < nurbs; n++) {
  703. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  704. if (!urb)
  705. return -ENOMEM;
  706. gspca_dev->urb[n] = urb;
  707. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  708. bsize,
  709. GFP_KERNEL,
  710. &urb->transfer_dma);
  711. if (urb->transfer_buffer == NULL) {
  712. pr_err("usb_alloc_coherent failed\n");
  713. return -ENOMEM;
  714. }
  715. urb->dev = gspca_dev->dev;
  716. urb->context = gspca_dev;
  717. urb->transfer_buffer_length = bsize;
  718. if (npkt != 0) { /* ISOC */
  719. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  720. ep->desc.bEndpointAddress);
  721. urb->transfer_flags = URB_ISO_ASAP
  722. | URB_NO_TRANSFER_DMA_MAP;
  723. urb->interval = 1 << (ep->desc.bInterval - 1);
  724. urb->complete = isoc_irq;
  725. urb->number_of_packets = npkt;
  726. for (i = 0; i < npkt; i++) {
  727. urb->iso_frame_desc[i].length = psize;
  728. urb->iso_frame_desc[i].offset = psize * i;
  729. }
  730. } else { /* bulk */
  731. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  732. ep->desc.bEndpointAddress);
  733. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  734. urb->complete = bulk_irq;
  735. }
  736. }
  737. return 0;
  738. }
  739. /*
  740. * start the USB transfer
  741. */
  742. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  743. {
  744. struct usb_interface *intf;
  745. struct usb_host_endpoint *ep;
  746. struct urb *urb;
  747. struct ep_tb_s ep_tb[MAX_ALT];
  748. int n, ret, xfer, alt, alt_idx;
  749. /* reset the streaming variables */
  750. gspca_dev->image = NULL;
  751. gspca_dev->image_len = 0;
  752. gspca_dev->last_packet_type = DISCARD_PACKET;
  753. gspca_dev->sequence = 0;
  754. gspca_dev->usb_err = 0;
  755. /* do the specific subdriver stuff before endpoint selection */
  756. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  757. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  758. if (gspca_dev->sd_desc->isoc_init) {
  759. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  760. if (ret < 0)
  761. return ret;
  762. }
  763. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  764. : USB_ENDPOINT_XFER_ISOC;
  765. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  766. if (gspca_dev->alt != 0) {
  767. gspca_dev->alt--; /* (previous version compatibility) */
  768. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
  769. gspca_dev->xfer_ep);
  770. if (ep == NULL) {
  771. pr_err("bad altsetting %d\n", gspca_dev->alt);
  772. return -EIO;
  773. }
  774. ep_tb[0].alt = gspca_dev->alt;
  775. alt_idx = 1;
  776. } else {
  777. /* else, compute the minimum bandwidth
  778. * and build the endpoint table */
  779. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  780. if (alt_idx <= 0) {
  781. pr_err("no transfer endpoint found\n");
  782. return -EIO;
  783. }
  784. }
  785. /* set the highest alternate setting and
  786. * loop until urb submit succeeds */
  787. gspca_input_destroy_urb(gspca_dev);
  788. gspca_dev->alt = ep_tb[--alt_idx].alt;
  789. alt = -1;
  790. for (;;) {
  791. if (alt != gspca_dev->alt) {
  792. alt = gspca_dev->alt;
  793. if (intf->num_altsetting > 1) {
  794. ret = usb_set_interface(gspca_dev->dev,
  795. gspca_dev->iface,
  796. alt);
  797. if (ret < 0) {
  798. if (ret == -ENOSPC)
  799. goto retry; /*fixme: ugly*/
  800. pr_err("set alt %d err %d\n", alt, ret);
  801. goto out;
  802. }
  803. }
  804. }
  805. if (!gspca_dev->cam.no_urb_create) {
  806. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  807. ret = create_urbs(gspca_dev,
  808. alt_xfer(&intf->altsetting[alt], xfer,
  809. gspca_dev->xfer_ep));
  810. if (ret < 0) {
  811. destroy_urbs(gspca_dev);
  812. goto out;
  813. }
  814. }
  815. /* clear the bulk endpoint */
  816. if (gspca_dev->cam.bulk)
  817. usb_clear_halt(gspca_dev->dev,
  818. gspca_dev->urb[0]->pipe);
  819. /* start the cam */
  820. ret = gspca_dev->sd_desc->start(gspca_dev);
  821. if (ret < 0) {
  822. destroy_urbs(gspca_dev);
  823. goto out;
  824. }
  825. gspca_dev->streaming = 1;
  826. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  827. /* some bulk transfers are started by the subdriver */
  828. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  829. break;
  830. /* submit the URBs */
  831. for (n = 0; n < MAX_NURBS; n++) {
  832. urb = gspca_dev->urb[n];
  833. if (urb == NULL)
  834. break;
  835. ret = usb_submit_urb(urb, GFP_KERNEL);
  836. if (ret < 0)
  837. break;
  838. }
  839. if (ret >= 0)
  840. break; /* transfer is started */
  841. /* something when wrong
  842. * stop the webcam and free the transfer resources */
  843. gspca_stream_off(gspca_dev);
  844. if (ret != -ENOSPC) {
  845. pr_err("usb_submit_urb alt %d err %d\n",
  846. gspca_dev->alt, ret);
  847. goto out;
  848. }
  849. /* the bandwidth is not wide enough
  850. * negotiate or try a lower alternate setting */
  851. retry:
  852. PERR("alt %d - bandwidth not wide enough, trying again", alt);
  853. msleep(20); /* wait for kill complete */
  854. if (gspca_dev->sd_desc->isoc_nego) {
  855. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  856. if (ret < 0)
  857. goto out;
  858. } else {
  859. if (alt_idx <= 0) {
  860. pr_err("no transfer endpoint found\n");
  861. ret = -EIO;
  862. goto out;
  863. }
  864. gspca_dev->alt = ep_tb[--alt_idx].alt;
  865. }
  866. }
  867. out:
  868. gspca_input_create_urb(gspca_dev);
  869. return ret;
  870. }
  871. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  872. {
  873. int i;
  874. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  875. gspca_dev->curr_mode = i;
  876. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
  877. /* does nothing if ctrl_handler == NULL */
  878. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  879. }
  880. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  881. int width, int height)
  882. {
  883. int i;
  884. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  885. if (width == gspca_dev->cam.cam_mode[i].width
  886. && height == gspca_dev->cam.cam_mode[i].height)
  887. return i;
  888. }
  889. return -EINVAL;
  890. }
  891. static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
  892. int width, int height)
  893. {
  894. int i;
  895. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  896. if (width >= gspca_dev->cam.cam_mode[i].width
  897. && height >= gspca_dev->cam.cam_mode[i].height)
  898. break;
  899. }
  900. return i;
  901. }
  902. /*
  903. * search a mode with the right pixel format
  904. */
  905. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  906. int mode,
  907. int pixfmt)
  908. {
  909. int modeU, modeD;
  910. modeU = modeD = mode;
  911. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  912. if (--modeD >= 0) {
  913. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  914. == pixfmt)
  915. return modeD;
  916. }
  917. if (++modeU < gspca_dev->cam.nmodes) {
  918. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  919. == pixfmt)
  920. return modeU;
  921. }
  922. }
  923. return -EINVAL;
  924. }
  925. #ifdef CONFIG_VIDEO_ADV_DEBUG
  926. static int vidioc_g_chip_info(struct file *file, void *priv,
  927. struct v4l2_dbg_chip_info *chip)
  928. {
  929. struct gspca_dev *gspca_dev = video_drvdata(file);
  930. gspca_dev->usb_err = 0;
  931. if (gspca_dev->sd_desc->get_chip_info)
  932. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  933. return chip->match.addr ? -EINVAL : 0;
  934. }
  935. static int vidioc_g_register(struct file *file, void *priv,
  936. struct v4l2_dbg_register *reg)
  937. {
  938. struct gspca_dev *gspca_dev = video_drvdata(file);
  939. gspca_dev->usb_err = 0;
  940. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  941. }
  942. static int vidioc_s_register(struct file *file, void *priv,
  943. const struct v4l2_dbg_register *reg)
  944. {
  945. struct gspca_dev *gspca_dev = video_drvdata(file);
  946. gspca_dev->usb_err = 0;
  947. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  948. }
  949. #endif
  950. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  951. struct v4l2_fmtdesc *fmtdesc)
  952. {
  953. struct gspca_dev *gspca_dev = video_drvdata(file);
  954. int i, j, index;
  955. __u32 fmt_tb[8];
  956. /* give an index to each format */
  957. index = 0;
  958. j = 0;
  959. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  960. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  961. j = 0;
  962. for (;;) {
  963. if (fmt_tb[j] == fmt_tb[index])
  964. break;
  965. j++;
  966. }
  967. if (j == index) {
  968. if (fmtdesc->index == index)
  969. break; /* new format */
  970. index++;
  971. if (index >= ARRAY_SIZE(fmt_tb))
  972. return -EINVAL;
  973. }
  974. }
  975. if (i < 0)
  976. return -EINVAL; /* no more format */
  977. fmtdesc->pixelformat = fmt_tb[index];
  978. if (gspca_dev->cam.cam_mode[i].sizeimage <
  979. gspca_dev->cam.cam_mode[i].width *
  980. gspca_dev->cam.cam_mode[i].height)
  981. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  982. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  983. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  984. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  985. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  986. fmtdesc->description[4] = '\0';
  987. return 0;
  988. }
  989. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  990. struct v4l2_format *fmt)
  991. {
  992. struct gspca_dev *gspca_dev = video_drvdata(file);
  993. fmt->fmt.pix = gspca_dev->pixfmt;
  994. /* some drivers use priv internally, zero it before giving it back to
  995. the core */
  996. fmt->fmt.pix.priv = 0;
  997. return 0;
  998. }
  999. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1000. struct v4l2_format *fmt)
  1001. {
  1002. int w, h, mode, mode2;
  1003. w = fmt->fmt.pix.width;
  1004. h = fmt->fmt.pix.height;
  1005. PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
  1006. fmt->fmt.pix.pixelformat, w, h);
  1007. /* search the nearest mode for width and height */
  1008. mode = wxh_to_nearest_mode(gspca_dev, w, h);
  1009. /* OK if right palette */
  1010. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1011. != fmt->fmt.pix.pixelformat) {
  1012. /* else, search the closest mode with the same pixel format */
  1013. mode2 = gspca_get_mode(gspca_dev, mode,
  1014. fmt->fmt.pix.pixelformat);
  1015. if (mode2 >= 0)
  1016. mode = mode2;
  1017. }
  1018. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1019. if (gspca_dev->sd_desc->try_fmt) {
  1020. /* pass original resolution to subdriver try_fmt */
  1021. fmt->fmt.pix.width = w;
  1022. fmt->fmt.pix.height = h;
  1023. gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
  1024. }
  1025. /* some drivers use priv internally, zero it before giving it back to
  1026. the core */
  1027. fmt->fmt.pix.priv = 0;
  1028. return mode; /* used when s_fmt */
  1029. }
  1030. static int vidioc_try_fmt_vid_cap(struct file *file,
  1031. void *priv,
  1032. struct v4l2_format *fmt)
  1033. {
  1034. struct gspca_dev *gspca_dev = video_drvdata(file);
  1035. int ret;
  1036. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1037. if (ret < 0)
  1038. return ret;
  1039. return 0;
  1040. }
  1041. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1042. struct v4l2_format *fmt)
  1043. {
  1044. struct gspca_dev *gspca_dev = video_drvdata(file);
  1045. int ret;
  1046. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1047. return -ERESTARTSYS;
  1048. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1049. if (ret < 0)
  1050. goto out;
  1051. if (gspca_dev->nframes != 0
  1052. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1053. ret = -EINVAL;
  1054. goto out;
  1055. }
  1056. if (gspca_dev->streaming) {
  1057. ret = -EBUSY;
  1058. goto out;
  1059. }
  1060. gspca_dev->curr_mode = ret;
  1061. if (gspca_dev->sd_desc->try_fmt)
  1062. /* subdriver try_fmt can modify format parameters */
  1063. gspca_dev->pixfmt = fmt->fmt.pix;
  1064. else
  1065. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret];
  1066. ret = 0;
  1067. out:
  1068. mutex_unlock(&gspca_dev->queue_lock);
  1069. return ret;
  1070. }
  1071. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1072. struct v4l2_frmsizeenum *fsize)
  1073. {
  1074. struct gspca_dev *gspca_dev = video_drvdata(file);
  1075. int i;
  1076. __u32 index = 0;
  1077. if (gspca_dev->sd_desc->enum_framesizes)
  1078. return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
  1079. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1080. if (fsize->pixel_format !=
  1081. gspca_dev->cam.cam_mode[i].pixelformat)
  1082. continue;
  1083. if (fsize->index == index) {
  1084. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1085. fsize->discrete.width =
  1086. gspca_dev->cam.cam_mode[i].width;
  1087. fsize->discrete.height =
  1088. gspca_dev->cam.cam_mode[i].height;
  1089. return 0;
  1090. }
  1091. index++;
  1092. }
  1093. return -EINVAL;
  1094. }
  1095. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1096. struct v4l2_frmivalenum *fival)
  1097. {
  1098. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1099. int mode;
  1100. __u32 i;
  1101. mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1102. if (mode < 0)
  1103. return -EINVAL;
  1104. if (gspca_dev->cam.mode_framerates == NULL ||
  1105. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1106. return -EINVAL;
  1107. if (fival->pixel_format !=
  1108. gspca_dev->cam.cam_mode[mode].pixelformat)
  1109. return -EINVAL;
  1110. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1111. if (fival->index == i) {
  1112. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1113. fival->discrete.numerator = 1;
  1114. fival->discrete.denominator =
  1115. gspca_dev->cam.mode_framerates[mode].rates[i];
  1116. return 0;
  1117. }
  1118. }
  1119. return -EINVAL;
  1120. }
  1121. static void gspca_release(struct v4l2_device *v4l2_device)
  1122. {
  1123. struct gspca_dev *gspca_dev =
  1124. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1125. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1126. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1127. kfree(gspca_dev->usb_buf);
  1128. kfree(gspca_dev);
  1129. }
  1130. static int dev_open(struct file *file)
  1131. {
  1132. struct gspca_dev *gspca_dev = video_drvdata(file);
  1133. int ret;
  1134. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1135. /* protect the subdriver against rmmod */
  1136. if (!try_module_get(gspca_dev->module))
  1137. return -ENODEV;
  1138. ret = v4l2_fh_open(file);
  1139. if (ret)
  1140. module_put(gspca_dev->module);
  1141. return ret;
  1142. }
  1143. static int dev_close(struct file *file)
  1144. {
  1145. struct gspca_dev *gspca_dev = video_drvdata(file);
  1146. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1147. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1148. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1149. return -ERESTARTSYS;
  1150. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1151. mutex_unlock(&gspca_dev->usb_lock);
  1152. return -ERESTARTSYS;
  1153. }
  1154. /* if the file did the capture, free the streaming resources */
  1155. if (gspca_dev->capt_file == file) {
  1156. if (gspca_dev->streaming)
  1157. gspca_stream_off(gspca_dev);
  1158. frame_free(gspca_dev);
  1159. }
  1160. module_put(gspca_dev->module);
  1161. mutex_unlock(&gspca_dev->queue_lock);
  1162. mutex_unlock(&gspca_dev->usb_lock);
  1163. PDEBUG(D_STREAM, "close done");
  1164. return v4l2_fh_release(file);
  1165. }
  1166. static int vidioc_querycap(struct file *file, void *priv,
  1167. struct v4l2_capability *cap)
  1168. {
  1169. struct gspca_dev *gspca_dev = video_drvdata(file);
  1170. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1171. sizeof cap->driver);
  1172. if (gspca_dev->dev->product != NULL) {
  1173. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1174. sizeof cap->card);
  1175. } else {
  1176. snprintf((char *) cap->card, sizeof cap->card,
  1177. "USB Camera (%04x:%04x)",
  1178. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1179. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1180. }
  1181. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1182. sizeof(cap->bus_info));
  1183. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1184. | V4L2_CAP_STREAMING
  1185. | V4L2_CAP_READWRITE;
  1186. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1187. return 0;
  1188. }
  1189. static int vidioc_enum_input(struct file *file, void *priv,
  1190. struct v4l2_input *input)
  1191. {
  1192. struct gspca_dev *gspca_dev = video_drvdata(file);
  1193. if (input->index != 0)
  1194. return -EINVAL;
  1195. input->type = V4L2_INPUT_TYPE_CAMERA;
  1196. input->status = gspca_dev->cam.input_flags;
  1197. strlcpy(input->name, gspca_dev->sd_desc->name,
  1198. sizeof input->name);
  1199. return 0;
  1200. }
  1201. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1202. {
  1203. *i = 0;
  1204. return 0;
  1205. }
  1206. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1207. {
  1208. if (i > 0)
  1209. return -EINVAL;
  1210. return (0);
  1211. }
  1212. static int vidioc_reqbufs(struct file *file, void *priv,
  1213. struct v4l2_requestbuffers *rb)
  1214. {
  1215. struct gspca_dev *gspca_dev = video_drvdata(file);
  1216. int i, ret = 0, streaming;
  1217. i = rb->memory; /* (avoid compilation warning) */
  1218. switch (i) {
  1219. case GSPCA_MEMORY_READ: /* (internal call) */
  1220. case V4L2_MEMORY_MMAP:
  1221. case V4L2_MEMORY_USERPTR:
  1222. break;
  1223. default:
  1224. return -EINVAL;
  1225. }
  1226. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1227. return -ERESTARTSYS;
  1228. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1229. && gspca_dev->memory != GSPCA_MEMORY_READ
  1230. && gspca_dev->memory != rb->memory) {
  1231. ret = -EBUSY;
  1232. goto out;
  1233. }
  1234. /* only one file may do the capture */
  1235. if (gspca_dev->capt_file != NULL
  1236. && gspca_dev->capt_file != file) {
  1237. ret = -EBUSY;
  1238. goto out;
  1239. }
  1240. /* if allocated, the buffers must not be mapped */
  1241. for (i = 0; i < gspca_dev->nframes; i++) {
  1242. if (gspca_dev->frame[i].vma_use_count) {
  1243. ret = -EBUSY;
  1244. goto out;
  1245. }
  1246. }
  1247. /* stop streaming */
  1248. streaming = gspca_dev->streaming;
  1249. if (streaming) {
  1250. gspca_stream_off(gspca_dev);
  1251. /* Don't restart the stream when switching from read
  1252. * to mmap mode */
  1253. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1254. streaming = 0;
  1255. }
  1256. /* free the previous allocated buffers, if any */
  1257. if (gspca_dev->nframes != 0)
  1258. frame_free(gspca_dev);
  1259. if (rb->count == 0) /* unrequest */
  1260. goto out;
  1261. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1262. if (ret == 0) {
  1263. rb->count = gspca_dev->nframes;
  1264. if (streaming)
  1265. ret = gspca_init_transfer(gspca_dev);
  1266. }
  1267. out:
  1268. mutex_unlock(&gspca_dev->queue_lock);
  1269. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1270. return ret;
  1271. }
  1272. static int vidioc_querybuf(struct file *file, void *priv,
  1273. struct v4l2_buffer *v4l2_buf)
  1274. {
  1275. struct gspca_dev *gspca_dev = video_drvdata(file);
  1276. struct gspca_frame *frame;
  1277. if (v4l2_buf->index >= gspca_dev->nframes)
  1278. return -EINVAL;
  1279. frame = &gspca_dev->frame[v4l2_buf->index];
  1280. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1281. return 0;
  1282. }
  1283. static int vidioc_streamon(struct file *file, void *priv,
  1284. enum v4l2_buf_type buf_type)
  1285. {
  1286. struct gspca_dev *gspca_dev = video_drvdata(file);
  1287. int ret;
  1288. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1289. return -EINVAL;
  1290. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1291. return -ERESTARTSYS;
  1292. /* check the capture file */
  1293. if (gspca_dev->capt_file != file) {
  1294. ret = -EBUSY;
  1295. goto out;
  1296. }
  1297. if (gspca_dev->nframes == 0
  1298. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1299. ret = -EINVAL;
  1300. goto out;
  1301. }
  1302. if (!gspca_dev->streaming) {
  1303. ret = gspca_init_transfer(gspca_dev);
  1304. if (ret < 0)
  1305. goto out;
  1306. }
  1307. PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK",
  1308. gspca_dev->pixfmt.pixelformat,
  1309. gspca_dev->pixfmt.width, gspca_dev->pixfmt.height);
  1310. ret = 0;
  1311. out:
  1312. mutex_unlock(&gspca_dev->queue_lock);
  1313. return ret;
  1314. }
  1315. static int vidioc_streamoff(struct file *file, void *priv,
  1316. enum v4l2_buf_type buf_type)
  1317. {
  1318. struct gspca_dev *gspca_dev = video_drvdata(file);
  1319. int i, ret;
  1320. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1321. return -EINVAL;
  1322. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1323. return -ERESTARTSYS;
  1324. if (!gspca_dev->streaming) {
  1325. ret = 0;
  1326. goto out;
  1327. }
  1328. /* check the capture file */
  1329. if (gspca_dev->capt_file != file) {
  1330. ret = -EBUSY;
  1331. goto out;
  1332. }
  1333. /* stop streaming */
  1334. gspca_stream_off(gspca_dev);
  1335. /* In case another thread is waiting in dqbuf */
  1336. wake_up_interruptible(&gspca_dev->wq);
  1337. /* empty the transfer queues */
  1338. for (i = 0; i < gspca_dev->nframes; i++)
  1339. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1340. atomic_set(&gspca_dev->fr_q, 0);
  1341. atomic_set(&gspca_dev->fr_i, 0);
  1342. gspca_dev->fr_o = 0;
  1343. ret = 0;
  1344. out:
  1345. mutex_unlock(&gspca_dev->queue_lock);
  1346. return ret;
  1347. }
  1348. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1349. struct v4l2_jpegcompression *jpegcomp)
  1350. {
  1351. struct gspca_dev *gspca_dev = video_drvdata(file);
  1352. gspca_dev->usb_err = 0;
  1353. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1354. }
  1355. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1356. const struct v4l2_jpegcompression *jpegcomp)
  1357. {
  1358. struct gspca_dev *gspca_dev = video_drvdata(file);
  1359. gspca_dev->usb_err = 0;
  1360. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1361. }
  1362. static int vidioc_g_parm(struct file *filp, void *priv,
  1363. struct v4l2_streamparm *parm)
  1364. {
  1365. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1366. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1367. if (gspca_dev->sd_desc->get_streamparm) {
  1368. gspca_dev->usb_err = 0;
  1369. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1370. return gspca_dev->usb_err;
  1371. }
  1372. return 0;
  1373. }
  1374. static int vidioc_s_parm(struct file *filp, void *priv,
  1375. struct v4l2_streamparm *parm)
  1376. {
  1377. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1378. unsigned int n;
  1379. n = parm->parm.capture.readbuffers;
  1380. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1381. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1382. else
  1383. gspca_dev->nbufread = n;
  1384. if (gspca_dev->sd_desc->set_streamparm) {
  1385. gspca_dev->usb_err = 0;
  1386. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1387. return gspca_dev->usb_err;
  1388. }
  1389. return 0;
  1390. }
  1391. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1392. {
  1393. struct gspca_dev *gspca_dev = video_drvdata(file);
  1394. struct gspca_frame *frame;
  1395. struct page *page;
  1396. unsigned long addr, start, size;
  1397. int i, ret;
  1398. start = vma->vm_start;
  1399. size = vma->vm_end - vma->vm_start;
  1400. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1401. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1402. return -ERESTARTSYS;
  1403. if (gspca_dev->capt_file != file) {
  1404. ret = -EINVAL;
  1405. goto out;
  1406. }
  1407. frame = NULL;
  1408. for (i = 0; i < gspca_dev->nframes; ++i) {
  1409. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1410. PDEBUG(D_STREAM, "mmap bad memory type");
  1411. break;
  1412. }
  1413. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1414. == vma->vm_pgoff) {
  1415. frame = &gspca_dev->frame[i];
  1416. break;
  1417. }
  1418. }
  1419. if (frame == NULL) {
  1420. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1421. ret = -EINVAL;
  1422. goto out;
  1423. }
  1424. if (size != frame->v4l2_buf.length) {
  1425. PDEBUG(D_STREAM, "mmap bad size");
  1426. ret = -EINVAL;
  1427. goto out;
  1428. }
  1429. /*
  1430. * - VM_IO marks the area as being a mmaped region for I/O to a
  1431. * device. It also prevents the region from being core dumped.
  1432. */
  1433. vma->vm_flags |= VM_IO;
  1434. addr = (unsigned long) frame->data;
  1435. while (size > 0) {
  1436. page = vmalloc_to_page((void *) addr);
  1437. ret = vm_insert_page(vma, start, page);
  1438. if (ret < 0)
  1439. goto out;
  1440. start += PAGE_SIZE;
  1441. addr += PAGE_SIZE;
  1442. size -= PAGE_SIZE;
  1443. }
  1444. vma->vm_ops = &gspca_vm_ops;
  1445. vma->vm_private_data = frame;
  1446. gspca_vm_open(vma);
  1447. ret = 0;
  1448. out:
  1449. mutex_unlock(&gspca_dev->queue_lock);
  1450. return ret;
  1451. }
  1452. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1453. enum v4l2_memory memory)
  1454. {
  1455. if (!gspca_dev->present)
  1456. return -ENODEV;
  1457. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1458. !gspca_dev->streaming)
  1459. return -EINVAL;
  1460. /* check if a frame is ready */
  1461. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1462. }
  1463. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1464. enum v4l2_memory memory)
  1465. {
  1466. int ret;
  1467. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1468. return -ERESTARTSYS;
  1469. ret = frame_ready_nolock(gspca_dev, file, memory);
  1470. mutex_unlock(&gspca_dev->queue_lock);
  1471. return ret;
  1472. }
  1473. /*
  1474. * dequeue a video buffer
  1475. *
  1476. * If nonblock_ing is false, block until a buffer is available.
  1477. */
  1478. static int vidioc_dqbuf(struct file *file, void *priv,
  1479. struct v4l2_buffer *v4l2_buf)
  1480. {
  1481. struct gspca_dev *gspca_dev = video_drvdata(file);
  1482. struct gspca_frame *frame;
  1483. int i, j, ret;
  1484. PDEBUG(D_FRAM, "dqbuf");
  1485. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1486. return -ERESTARTSYS;
  1487. for (;;) {
  1488. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1489. if (ret < 0)
  1490. goto out;
  1491. if (ret > 0)
  1492. break;
  1493. mutex_unlock(&gspca_dev->queue_lock);
  1494. if (file->f_flags & O_NONBLOCK)
  1495. return -EAGAIN;
  1496. /* wait till a frame is ready */
  1497. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1498. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1499. msecs_to_jiffies(3000));
  1500. if (ret < 0)
  1501. return ret;
  1502. if (ret == 0)
  1503. return -EIO;
  1504. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1505. return -ERESTARTSYS;
  1506. }
  1507. i = gspca_dev->fr_o;
  1508. j = gspca_dev->fr_queue[i];
  1509. frame = &gspca_dev->frame[j];
  1510. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1511. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1512. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1513. PDEBUG(D_FRAM, "dqbuf %d", j);
  1514. ret = 0;
  1515. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1516. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1517. frame->data,
  1518. frame->v4l2_buf.bytesused)) {
  1519. PERR("dqbuf cp to user failed");
  1520. ret = -EFAULT;
  1521. }
  1522. }
  1523. out:
  1524. mutex_unlock(&gspca_dev->queue_lock);
  1525. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1526. mutex_lock(&gspca_dev->usb_lock);
  1527. gspca_dev->usb_err = 0;
  1528. if (gspca_dev->present)
  1529. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1530. mutex_unlock(&gspca_dev->usb_lock);
  1531. }
  1532. return ret;
  1533. }
  1534. /*
  1535. * queue a video buffer
  1536. *
  1537. * Attempting to queue a buffer that has already been
  1538. * queued will return -EINVAL.
  1539. */
  1540. static int vidioc_qbuf(struct file *file, void *priv,
  1541. struct v4l2_buffer *v4l2_buf)
  1542. {
  1543. struct gspca_dev *gspca_dev = video_drvdata(file);
  1544. struct gspca_frame *frame;
  1545. int i, index, ret;
  1546. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1547. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1548. return -ERESTARTSYS;
  1549. index = v4l2_buf->index;
  1550. if ((unsigned) index >= gspca_dev->nframes) {
  1551. PDEBUG(D_FRAM,
  1552. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1553. ret = -EINVAL;
  1554. goto out;
  1555. }
  1556. if (v4l2_buf->memory != gspca_dev->memory) {
  1557. PDEBUG(D_FRAM, "qbuf bad memory type");
  1558. ret = -EINVAL;
  1559. goto out;
  1560. }
  1561. frame = &gspca_dev->frame[index];
  1562. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1563. PDEBUG(D_FRAM, "qbuf bad state");
  1564. ret = -EINVAL;
  1565. goto out;
  1566. }
  1567. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1568. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1569. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1570. frame->v4l2_buf.length = v4l2_buf->length;
  1571. }
  1572. /* put the buffer in the 'queued' queue */
  1573. i = atomic_read(&gspca_dev->fr_q);
  1574. gspca_dev->fr_queue[i] = index;
  1575. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1576. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1577. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1578. ret = 0;
  1579. out:
  1580. mutex_unlock(&gspca_dev->queue_lock);
  1581. return ret;
  1582. }
  1583. /*
  1584. * allocate the resources for read()
  1585. */
  1586. static int read_alloc(struct gspca_dev *gspca_dev,
  1587. struct file *file)
  1588. {
  1589. struct v4l2_buffer v4l2_buf;
  1590. int i, ret;
  1591. PDEBUG(D_STREAM, "read alloc");
  1592. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1593. return -ERESTARTSYS;
  1594. if (gspca_dev->nframes == 0) {
  1595. struct v4l2_requestbuffers rb;
  1596. memset(&rb, 0, sizeof rb);
  1597. rb.count = gspca_dev->nbufread;
  1598. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1599. rb.memory = GSPCA_MEMORY_READ;
  1600. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1601. if (ret != 0) {
  1602. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1603. goto out;
  1604. }
  1605. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1606. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1607. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1608. for (i = 0; i < gspca_dev->nbufread; i++) {
  1609. v4l2_buf.index = i;
  1610. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1611. if (ret != 0) {
  1612. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1613. goto out;
  1614. }
  1615. }
  1616. }
  1617. /* start streaming */
  1618. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1619. if (ret != 0)
  1620. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1621. out:
  1622. mutex_unlock(&gspca_dev->usb_lock);
  1623. return ret;
  1624. }
  1625. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1626. {
  1627. struct gspca_dev *gspca_dev = video_drvdata(file);
  1628. unsigned long req_events = poll_requested_events(wait);
  1629. int ret = 0;
  1630. PDEBUG(D_FRAM, "poll");
  1631. if (req_events & POLLPRI)
  1632. ret |= v4l2_ctrl_poll(file, wait);
  1633. if (req_events & (POLLIN | POLLRDNORM)) {
  1634. /* if reqbufs is not done, the user would use read() */
  1635. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1636. if (read_alloc(gspca_dev, file) != 0) {
  1637. ret |= POLLERR;
  1638. goto out;
  1639. }
  1640. }
  1641. poll_wait(file, &gspca_dev->wq, wait);
  1642. /* check if an image has been received */
  1643. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1644. ret |= POLLERR;
  1645. goto out;
  1646. }
  1647. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1648. ret |= POLLIN | POLLRDNORM;
  1649. mutex_unlock(&gspca_dev->queue_lock);
  1650. }
  1651. out:
  1652. if (!gspca_dev->present)
  1653. ret |= POLLHUP;
  1654. return ret;
  1655. }
  1656. static ssize_t dev_read(struct file *file, char __user *data,
  1657. size_t count, loff_t *ppos)
  1658. {
  1659. struct gspca_dev *gspca_dev = video_drvdata(file);
  1660. struct gspca_frame *frame;
  1661. struct v4l2_buffer v4l2_buf;
  1662. struct timeval timestamp;
  1663. int n, ret, ret2;
  1664. PDEBUG(D_FRAM, "read (%zd)", count);
  1665. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1666. ret = read_alloc(gspca_dev, file);
  1667. if (ret != 0)
  1668. return ret;
  1669. }
  1670. /* get a frame */
  1671. v4l2_get_timestamp(&timestamp);
  1672. timestamp.tv_sec--;
  1673. n = 2;
  1674. for (;;) {
  1675. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1676. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1677. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1678. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1679. if (ret != 0) {
  1680. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1681. return ret;
  1682. }
  1683. /* if the process slept for more than 1 second,
  1684. * get a newer frame */
  1685. frame = &gspca_dev->frame[v4l2_buf.index];
  1686. if (--n < 0)
  1687. break; /* avoid infinite loop */
  1688. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1689. break;
  1690. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1691. if (ret != 0) {
  1692. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1693. return ret;
  1694. }
  1695. }
  1696. /* copy the frame */
  1697. if (count > frame->v4l2_buf.bytesused)
  1698. count = frame->v4l2_buf.bytesused;
  1699. ret = copy_to_user(data, frame->data, count);
  1700. if (ret != 0) {
  1701. PERR("read cp to user lack %d / %zd", ret, count);
  1702. ret = -EFAULT;
  1703. goto out;
  1704. }
  1705. ret = count;
  1706. out:
  1707. /* in each case, requeue the buffer */
  1708. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1709. if (ret2 != 0)
  1710. return ret2;
  1711. return ret;
  1712. }
  1713. static struct v4l2_file_operations dev_fops = {
  1714. .owner = THIS_MODULE,
  1715. .open = dev_open,
  1716. .release = dev_close,
  1717. .read = dev_read,
  1718. .mmap = dev_mmap,
  1719. .unlocked_ioctl = video_ioctl2,
  1720. .poll = dev_poll,
  1721. };
  1722. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1723. .vidioc_querycap = vidioc_querycap,
  1724. .vidioc_dqbuf = vidioc_dqbuf,
  1725. .vidioc_qbuf = vidioc_qbuf,
  1726. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1727. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1728. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1729. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1730. .vidioc_streamon = vidioc_streamon,
  1731. .vidioc_enum_input = vidioc_enum_input,
  1732. .vidioc_g_input = vidioc_g_input,
  1733. .vidioc_s_input = vidioc_s_input,
  1734. .vidioc_reqbufs = vidioc_reqbufs,
  1735. .vidioc_querybuf = vidioc_querybuf,
  1736. .vidioc_streamoff = vidioc_streamoff,
  1737. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1738. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1739. .vidioc_g_parm = vidioc_g_parm,
  1740. .vidioc_s_parm = vidioc_s_parm,
  1741. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1742. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1743. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1744. .vidioc_g_chip_info = vidioc_g_chip_info,
  1745. .vidioc_g_register = vidioc_g_register,
  1746. .vidioc_s_register = vidioc_s_register,
  1747. #endif
  1748. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1749. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1750. };
  1751. static const struct video_device gspca_template = {
  1752. .name = "gspca main driver",
  1753. .fops = &dev_fops,
  1754. .ioctl_ops = &dev_ioctl_ops,
  1755. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1756. };
  1757. /*
  1758. * probe and create a new gspca device
  1759. *
  1760. * This function must be called by the sub-driver when it is
  1761. * called for probing a new device.
  1762. */
  1763. int gspca_dev_probe2(struct usb_interface *intf,
  1764. const struct usb_device_id *id,
  1765. const struct sd_desc *sd_desc,
  1766. int dev_size,
  1767. struct module *module)
  1768. {
  1769. struct gspca_dev *gspca_dev;
  1770. struct usb_device *dev = interface_to_usbdev(intf);
  1771. int ret;
  1772. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1773. sd_desc->name, id->idVendor, id->idProduct);
  1774. /* create the device */
  1775. if (dev_size < sizeof *gspca_dev)
  1776. dev_size = sizeof *gspca_dev;
  1777. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1778. if (!gspca_dev) {
  1779. pr_err("couldn't kzalloc gspca struct\n");
  1780. return -ENOMEM;
  1781. }
  1782. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1783. if (!gspca_dev->usb_buf) {
  1784. pr_err("out of memory\n");
  1785. ret = -ENOMEM;
  1786. goto out;
  1787. }
  1788. gspca_dev->dev = dev;
  1789. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1790. gspca_dev->xfer_ep = -1;
  1791. /* check if any audio device */
  1792. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1793. int i;
  1794. struct usb_interface *intf2;
  1795. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1796. intf2 = dev->actconfig->interface[i];
  1797. if (intf2 != NULL
  1798. && intf2->altsetting != NULL
  1799. && intf2->altsetting->desc.bInterfaceClass ==
  1800. USB_CLASS_AUDIO) {
  1801. gspca_dev->audio = 1;
  1802. break;
  1803. }
  1804. }
  1805. }
  1806. gspca_dev->v4l2_dev.release = gspca_release;
  1807. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1808. if (ret)
  1809. goto out;
  1810. gspca_dev->sd_desc = sd_desc;
  1811. gspca_dev->nbufread = 2;
  1812. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1813. gspca_dev->vdev = gspca_template;
  1814. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1815. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1816. gspca_dev->module = module;
  1817. gspca_dev->present = 1;
  1818. mutex_init(&gspca_dev->usb_lock);
  1819. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1820. mutex_init(&gspca_dev->queue_lock);
  1821. init_waitqueue_head(&gspca_dev->wq);
  1822. /* configure the subdriver and initialize the USB device */
  1823. ret = sd_desc->config(gspca_dev, id);
  1824. if (ret < 0)
  1825. goto out;
  1826. ret = sd_desc->init(gspca_dev);
  1827. if (ret < 0)
  1828. goto out;
  1829. if (sd_desc->init_controls)
  1830. ret = sd_desc->init_controls(gspca_dev);
  1831. if (ret < 0)
  1832. goto out;
  1833. gspca_set_default_mode(gspca_dev);
  1834. ret = gspca_input_connect(gspca_dev);
  1835. if (ret)
  1836. goto out;
  1837. /*
  1838. * Don't take usb_lock for these ioctls. This improves latency if
  1839. * usb_lock is taken for a long time, e.g. when changing a control
  1840. * value, and a new frame is ready to be dequeued.
  1841. */
  1842. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1843. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1844. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1845. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1846. if (!gspca_dev->sd_desc->get_register)
  1847. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1848. if (!gspca_dev->sd_desc->set_register)
  1849. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1850. #endif
  1851. if (!gspca_dev->sd_desc->get_jcomp)
  1852. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1853. if (!gspca_dev->sd_desc->set_jcomp)
  1854. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1855. /* init video stuff */
  1856. ret = video_register_device(&gspca_dev->vdev,
  1857. VFL_TYPE_GRABBER,
  1858. -1);
  1859. if (ret < 0) {
  1860. pr_err("video_register_device err %d\n", ret);
  1861. goto out;
  1862. }
  1863. usb_set_intfdata(intf, gspca_dev);
  1864. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  1865. gspca_input_create_urb(gspca_dev);
  1866. return 0;
  1867. out:
  1868. #if IS_ENABLED(CONFIG_INPUT)
  1869. if (gspca_dev->input_dev)
  1870. input_unregister_device(gspca_dev->input_dev);
  1871. #endif
  1872. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1873. kfree(gspca_dev->usb_buf);
  1874. kfree(gspca_dev);
  1875. return ret;
  1876. }
  1877. EXPORT_SYMBOL(gspca_dev_probe2);
  1878. /* same function as the previous one, but check the interface */
  1879. int gspca_dev_probe(struct usb_interface *intf,
  1880. const struct usb_device_id *id,
  1881. const struct sd_desc *sd_desc,
  1882. int dev_size,
  1883. struct module *module)
  1884. {
  1885. struct usb_device *dev = interface_to_usbdev(intf);
  1886. /* we don't handle multi-config cameras */
  1887. if (dev->descriptor.bNumConfigurations != 1) {
  1888. pr_err("%04x:%04x too many config\n",
  1889. id->idVendor, id->idProduct);
  1890. return -ENODEV;
  1891. }
  1892. /* the USB video interface must be the first one */
  1893. if (dev->actconfig->desc.bNumInterfaces != 1
  1894. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1895. return -ENODEV;
  1896. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1897. }
  1898. EXPORT_SYMBOL(gspca_dev_probe);
  1899. /*
  1900. * USB disconnection
  1901. *
  1902. * This function must be called by the sub-driver
  1903. * when the device disconnects, after the specific resources are freed.
  1904. */
  1905. void gspca_disconnect(struct usb_interface *intf)
  1906. {
  1907. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1908. #if IS_ENABLED(CONFIG_INPUT)
  1909. struct input_dev *input_dev;
  1910. #endif
  1911. PDEBUG(D_PROBE, "%s disconnect",
  1912. video_device_node_name(&gspca_dev->vdev));
  1913. mutex_lock(&gspca_dev->usb_lock);
  1914. gspca_dev->present = 0;
  1915. destroy_urbs(gspca_dev);
  1916. #if IS_ENABLED(CONFIG_INPUT)
  1917. gspca_input_destroy_urb(gspca_dev);
  1918. input_dev = gspca_dev->input_dev;
  1919. if (input_dev) {
  1920. gspca_dev->input_dev = NULL;
  1921. input_unregister_device(input_dev);
  1922. }
  1923. #endif
  1924. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  1925. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  1926. gspca_dev->sd_desc->stop0(gspca_dev);
  1927. gspca_dev->streaming = 0;
  1928. gspca_dev->dev = NULL;
  1929. wake_up_interruptible(&gspca_dev->wq);
  1930. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1931. video_unregister_device(&gspca_dev->vdev);
  1932. mutex_unlock(&gspca_dev->usb_lock);
  1933. /* (this will call gspca_release() immediately or on last close) */
  1934. v4l2_device_put(&gspca_dev->v4l2_dev);
  1935. }
  1936. EXPORT_SYMBOL(gspca_disconnect);
  1937. #ifdef CONFIG_PM
  1938. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1939. {
  1940. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1941. gspca_input_destroy_urb(gspca_dev);
  1942. if (!gspca_dev->streaming)
  1943. return 0;
  1944. mutex_lock(&gspca_dev->usb_lock);
  1945. gspca_dev->frozen = 1; /* avoid urb error messages */
  1946. gspca_dev->usb_err = 0;
  1947. if (gspca_dev->sd_desc->stopN)
  1948. gspca_dev->sd_desc->stopN(gspca_dev);
  1949. destroy_urbs(gspca_dev);
  1950. gspca_set_alt0(gspca_dev);
  1951. if (gspca_dev->sd_desc->stop0)
  1952. gspca_dev->sd_desc->stop0(gspca_dev);
  1953. mutex_unlock(&gspca_dev->usb_lock);
  1954. return 0;
  1955. }
  1956. EXPORT_SYMBOL(gspca_suspend);
  1957. int gspca_resume(struct usb_interface *intf)
  1958. {
  1959. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1960. int streaming, ret = 0;
  1961. mutex_lock(&gspca_dev->usb_lock);
  1962. gspca_dev->frozen = 0;
  1963. gspca_dev->usb_err = 0;
  1964. gspca_dev->sd_desc->init(gspca_dev);
  1965. /*
  1966. * Most subdrivers send all ctrl values on sd_start and thus
  1967. * only write to the device registers on s_ctrl when streaming ->
  1968. * Clear streaming to avoid setting all ctrls twice.
  1969. */
  1970. streaming = gspca_dev->streaming;
  1971. gspca_dev->streaming = 0;
  1972. if (streaming)
  1973. ret = gspca_init_transfer(gspca_dev);
  1974. else
  1975. gspca_input_create_urb(gspca_dev);
  1976. mutex_unlock(&gspca_dev->usb_lock);
  1977. return ret;
  1978. }
  1979. EXPORT_SYMBOL(gspca_resume);
  1980. #endif
  1981. /* -- module insert / remove -- */
  1982. static int __init gspca_init(void)
  1983. {
  1984. pr_info("v" GSPCA_VERSION " registered\n");
  1985. return 0;
  1986. }
  1987. static void __exit gspca_exit(void)
  1988. {
  1989. }
  1990. module_init(gspca_init);
  1991. module_exit(gspca_exit);
  1992. module_param_named(debug, gspca_debug, int, 0644);
  1993. MODULE_PARM_DESC(debug,
  1994. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");