gspca.c 56 KB

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