gspca.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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. v4l2_get_timestamp(&frame->v4l2_buf.timestamp);
  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. frsz = gspca_dev->pixfmt.sizeimage;
  447. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  448. frsz = PAGE_ALIGN(frsz);
  449. if (count >= GSPCA_MAX_FRAMES)
  450. count = GSPCA_MAX_FRAMES - 1;
  451. gspca_dev->frbuf = vmalloc_32(frsz * count);
  452. if (!gspca_dev->frbuf) {
  453. pr_err("frame alloc failed\n");
  454. return -ENOMEM;
  455. }
  456. gspca_dev->capt_file = file;
  457. gspca_dev->memory = memory;
  458. gspca_dev->frsz = frsz;
  459. gspca_dev->nframes = count;
  460. for (i = 0; i < count; i++) {
  461. frame = &gspca_dev->frame[i];
  462. frame->v4l2_buf.index = i;
  463. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  464. frame->v4l2_buf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  465. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  466. frame->v4l2_buf.length = frsz;
  467. frame->v4l2_buf.memory = memory;
  468. frame->v4l2_buf.sequence = 0;
  469. frame->data = gspca_dev->frbuf + i * frsz;
  470. frame->v4l2_buf.m.offset = i * frsz;
  471. }
  472. atomic_set(&gspca_dev->fr_q, 0);
  473. atomic_set(&gspca_dev->fr_i, 0);
  474. gspca_dev->fr_o = 0;
  475. return 0;
  476. }
  477. static void frame_free(struct gspca_dev *gspca_dev)
  478. {
  479. int i;
  480. PDEBUG(D_STREAM, "frame free");
  481. if (gspca_dev->frbuf != NULL) {
  482. vfree(gspca_dev->frbuf);
  483. gspca_dev->frbuf = NULL;
  484. for (i = 0; i < gspca_dev->nframes; i++)
  485. gspca_dev->frame[i].data = NULL;
  486. }
  487. gspca_dev->nframes = 0;
  488. gspca_dev->frsz = 0;
  489. gspca_dev->capt_file = NULL;
  490. gspca_dev->memory = GSPCA_MEMORY_NO;
  491. }
  492. static void destroy_urbs(struct gspca_dev *gspca_dev)
  493. {
  494. struct urb *urb;
  495. unsigned int i;
  496. PDEBUG(D_STREAM, "kill transfer");
  497. for (i = 0; i < MAX_NURBS; i++) {
  498. urb = gspca_dev->urb[i];
  499. if (urb == NULL)
  500. break;
  501. gspca_dev->urb[i] = NULL;
  502. usb_kill_urb(urb);
  503. usb_free_coherent(gspca_dev->dev,
  504. urb->transfer_buffer_length,
  505. urb->transfer_buffer,
  506. urb->transfer_dma);
  507. usb_free_urb(urb);
  508. }
  509. }
  510. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  511. {
  512. int ret;
  513. if (gspca_dev->alt == 0)
  514. return 0;
  515. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  516. if (ret < 0)
  517. pr_err("set alt 0 err %d\n", ret);
  518. return ret;
  519. }
  520. /* Note: both the queue and the usb locks should be held when calling this */
  521. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  522. {
  523. gspca_dev->streaming = 0;
  524. gspca_dev->usb_err = 0;
  525. if (gspca_dev->sd_desc->stopN)
  526. gspca_dev->sd_desc->stopN(gspca_dev);
  527. destroy_urbs(gspca_dev);
  528. gspca_input_destroy_urb(gspca_dev);
  529. gspca_set_alt0(gspca_dev);
  530. gspca_input_create_urb(gspca_dev);
  531. if (gspca_dev->sd_desc->stop0)
  532. gspca_dev->sd_desc->stop0(gspca_dev);
  533. PDEBUG(D_STREAM, "stream off OK");
  534. }
  535. /*
  536. * look for an input transfer endpoint in an alternate setting.
  537. *
  538. * If xfer_ep is invalid, return the first valid ep found, otherwise
  539. * look for exactly the ep with address equal to xfer_ep.
  540. */
  541. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  542. int xfer, int xfer_ep)
  543. {
  544. struct usb_host_endpoint *ep;
  545. int i, attr;
  546. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  547. ep = &alt->endpoint[i];
  548. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  549. if (attr == xfer
  550. && ep->desc.wMaxPacketSize != 0
  551. && usb_endpoint_dir_in(&ep->desc)
  552. && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
  553. return ep;
  554. }
  555. return NULL;
  556. }
  557. /* compute the minimum bandwidth for the current transfer */
  558. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  559. {
  560. u32 bandwidth;
  561. /* get the (max) image size */
  562. bandwidth = gspca_dev->pixfmt.sizeimage;
  563. /* if the image is compressed, estimate its mean size */
  564. if (!gspca_dev->cam.needs_full_bandwidth &&
  565. bandwidth < gspca_dev->pixfmt.width *
  566. gspca_dev->pixfmt.height)
  567. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  568. /* estimate the frame rate */
  569. if (gspca_dev->sd_desc->get_streamparm) {
  570. struct v4l2_streamparm parm;
  571. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  572. bandwidth *= parm.parm.capture.timeperframe.denominator;
  573. bandwidth /= parm.parm.capture.timeperframe.numerator;
  574. } else {
  575. /* don't hope more than 15 fps with USB 1.1 and
  576. * image resolution >= 640x480 */
  577. if (gspca_dev->pixfmt.width >= 640
  578. && gspca_dev->dev->speed == USB_SPEED_FULL)
  579. bandwidth *= 15; /* 15 fps */
  580. else
  581. bandwidth *= 30; /* 30 fps */
  582. }
  583. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  584. return bandwidth;
  585. }
  586. /* endpoint table */
  587. #define MAX_ALT 16
  588. struct ep_tb_s {
  589. u32 alt;
  590. u32 bandwidth;
  591. };
  592. /*
  593. * build the table of the endpoints
  594. * and compute the minimum bandwidth for the image transfer
  595. */
  596. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  597. struct usb_interface *intf,
  598. struct ep_tb_s *ep_tb)
  599. {
  600. struct usb_host_endpoint *ep;
  601. int i, j, nbalt, psize, found;
  602. u32 bandwidth, last_bw;
  603. nbalt = intf->num_altsetting;
  604. if (nbalt > MAX_ALT)
  605. nbalt = MAX_ALT; /* fixme: should warn */
  606. /* build the endpoint table */
  607. i = 0;
  608. last_bw = 0;
  609. for (;;) {
  610. ep_tb->bandwidth = 2000 * 2000 * 120;
  611. found = 0;
  612. for (j = 0; j < nbalt; j++) {
  613. ep = alt_xfer(&intf->altsetting[j],
  614. USB_ENDPOINT_XFER_ISOC,
  615. gspca_dev->xfer_ep);
  616. if (ep == NULL)
  617. continue;
  618. if (ep->desc.bInterval == 0) {
  619. pr_err("alt %d iso endp with 0 interval\n", j);
  620. continue;
  621. }
  622. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  623. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  624. bandwidth = psize * 1000;
  625. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  626. || gspca_dev->dev->speed >= USB_SPEED_SUPER)
  627. bandwidth *= 8;
  628. bandwidth /= 1 << (ep->desc.bInterval - 1);
  629. if (bandwidth <= last_bw)
  630. continue;
  631. if (bandwidth < ep_tb->bandwidth) {
  632. ep_tb->bandwidth = bandwidth;
  633. ep_tb->alt = j;
  634. found = 1;
  635. }
  636. }
  637. if (!found)
  638. break;
  639. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  640. ep_tb->alt, ep_tb->bandwidth);
  641. last_bw = ep_tb->bandwidth;
  642. i++;
  643. ep_tb++;
  644. }
  645. /*
  646. * If the camera:
  647. * has a usb audio class interface (a built in usb mic); and
  648. * is a usb 1 full speed device; and
  649. * uses the max full speed iso bandwidth; and
  650. * and has more than 1 alt setting
  651. * then skip the highest alt setting to spare bandwidth for the mic
  652. */
  653. if (gspca_dev->audio &&
  654. gspca_dev->dev->speed == USB_SPEED_FULL &&
  655. last_bw >= 1000000 &&
  656. i > 1) {
  657. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  658. i--;
  659. ep_tb--;
  660. }
  661. /* get the requested bandwidth and start at the highest atlsetting */
  662. bandwidth = which_bandwidth(gspca_dev);
  663. ep_tb--;
  664. while (i > 1) {
  665. ep_tb--;
  666. if (ep_tb->bandwidth < bandwidth)
  667. break;
  668. i--;
  669. }
  670. return i;
  671. }
  672. /*
  673. * create the URBs for image transfer
  674. */
  675. static int create_urbs(struct gspca_dev *gspca_dev,
  676. struct usb_host_endpoint *ep)
  677. {
  678. struct urb *urb;
  679. int n, nurbs, i, psize, npkt, bsize;
  680. /* calculate the packet size and the number of packets */
  681. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  682. if (!gspca_dev->cam.bulk) { /* isoc */
  683. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  684. if (gspca_dev->pkt_size == 0)
  685. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  686. else
  687. psize = gspca_dev->pkt_size;
  688. npkt = gspca_dev->cam.npkt;
  689. if (npkt == 0)
  690. npkt = 32; /* default value */
  691. bsize = psize * npkt;
  692. PDEBUG(D_STREAM,
  693. "isoc %d pkts size %d = bsize:%d",
  694. npkt, psize, bsize);
  695. nurbs = DEF_NURBS;
  696. } else { /* bulk */
  697. npkt = 0;
  698. bsize = gspca_dev->cam.bulk_size;
  699. if (bsize == 0)
  700. bsize = psize;
  701. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  702. if (gspca_dev->cam.bulk_nurbs != 0)
  703. nurbs = gspca_dev->cam.bulk_nurbs;
  704. else
  705. nurbs = 1;
  706. }
  707. for (n = 0; n < nurbs; n++) {
  708. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  709. if (!urb)
  710. return -ENOMEM;
  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. gspca_dev->xfer_ep);
  775. if (ep == NULL) {
  776. pr_err("bad altsetting %d\n", gspca_dev->alt);
  777. return -EIO;
  778. }
  779. ep_tb[0].alt = gspca_dev->alt;
  780. alt_idx = 1;
  781. } else {
  782. /* else, compute the minimum bandwidth
  783. * and build the endpoint table */
  784. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  785. if (alt_idx <= 0) {
  786. pr_err("no transfer endpoint found\n");
  787. return -EIO;
  788. }
  789. }
  790. /* set the highest alternate setting and
  791. * loop until urb submit succeeds */
  792. gspca_input_destroy_urb(gspca_dev);
  793. gspca_dev->alt = ep_tb[--alt_idx].alt;
  794. alt = -1;
  795. for (;;) {
  796. if (alt != gspca_dev->alt) {
  797. alt = gspca_dev->alt;
  798. if (intf->num_altsetting > 1) {
  799. ret = usb_set_interface(gspca_dev->dev,
  800. gspca_dev->iface,
  801. alt);
  802. if (ret < 0) {
  803. if (ret == -ENOSPC)
  804. goto retry; /*fixme: ugly*/
  805. pr_err("set alt %d err %d\n", alt, ret);
  806. goto out;
  807. }
  808. }
  809. }
  810. if (!gspca_dev->cam.no_urb_create) {
  811. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  812. ret = create_urbs(gspca_dev,
  813. alt_xfer(&intf->altsetting[alt], xfer,
  814. gspca_dev->xfer_ep));
  815. if (ret < 0) {
  816. destroy_urbs(gspca_dev);
  817. goto out;
  818. }
  819. }
  820. /* clear the bulk endpoint */
  821. if (gspca_dev->cam.bulk)
  822. usb_clear_halt(gspca_dev->dev,
  823. gspca_dev->urb[0]->pipe);
  824. /* start the cam */
  825. ret = gspca_dev->sd_desc->start(gspca_dev);
  826. if (ret < 0) {
  827. destroy_urbs(gspca_dev);
  828. goto out;
  829. }
  830. gspca_dev->streaming = 1;
  831. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  832. /* some bulk transfers are started by the subdriver */
  833. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  834. break;
  835. /* submit the URBs */
  836. for (n = 0; n < MAX_NURBS; n++) {
  837. urb = gspca_dev->urb[n];
  838. if (urb == NULL)
  839. break;
  840. ret = usb_submit_urb(urb, GFP_KERNEL);
  841. if (ret < 0)
  842. break;
  843. }
  844. if (ret >= 0)
  845. break; /* transfer is started */
  846. /* something when wrong
  847. * stop the webcam and free the transfer resources */
  848. gspca_stream_off(gspca_dev);
  849. if (ret != -ENOSPC) {
  850. pr_err("usb_submit_urb alt %d err %d\n",
  851. gspca_dev->alt, ret);
  852. goto out;
  853. }
  854. /* the bandwidth is not wide enough
  855. * negotiate or try a lower alternate setting */
  856. retry:
  857. PERR("alt %d - bandwidth not wide enough, trying again", alt);
  858. msleep(20); /* wait for kill complete */
  859. if (gspca_dev->sd_desc->isoc_nego) {
  860. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  861. if (ret < 0)
  862. goto out;
  863. } else {
  864. if (alt_idx <= 0) {
  865. pr_err("no transfer endpoint found\n");
  866. ret = -EIO;
  867. goto out;
  868. }
  869. gspca_dev->alt = ep_tb[--alt_idx].alt;
  870. }
  871. }
  872. out:
  873. gspca_input_create_urb(gspca_dev);
  874. return ret;
  875. }
  876. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  877. {
  878. int i;
  879. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  880. gspca_dev->curr_mode = i;
  881. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
  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 = 0; i < gspca_dev->cam.nmodes; i++) {
  890. if (width == gspca_dev->cam.cam_mode[i].width
  891. && height == gspca_dev->cam.cam_mode[i].height)
  892. return i;
  893. }
  894. return -EINVAL;
  895. }
  896. static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
  897. int width, int height)
  898. {
  899. int i;
  900. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  901. if (width >= gspca_dev->cam.cam_mode[i].width
  902. && height >= gspca_dev->cam.cam_mode[i].height)
  903. break;
  904. }
  905. return i;
  906. }
  907. /*
  908. * search a mode with the right pixel format
  909. */
  910. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  911. int mode,
  912. int pixfmt)
  913. {
  914. int modeU, modeD;
  915. modeU = modeD = mode;
  916. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  917. if (--modeD >= 0) {
  918. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  919. == pixfmt)
  920. return modeD;
  921. }
  922. if (++modeU < gspca_dev->cam.nmodes) {
  923. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  924. == pixfmt)
  925. return modeU;
  926. }
  927. }
  928. return -EINVAL;
  929. }
  930. #ifdef CONFIG_VIDEO_ADV_DEBUG
  931. static int vidioc_g_chip_info(struct file *file, void *priv,
  932. struct v4l2_dbg_chip_info *chip)
  933. {
  934. struct gspca_dev *gspca_dev = video_drvdata(file);
  935. gspca_dev->usb_err = 0;
  936. if (gspca_dev->sd_desc->get_chip_info)
  937. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  938. return chip->match.addr ? -EINVAL : 0;
  939. }
  940. static int vidioc_g_register(struct file *file, void *priv,
  941. struct v4l2_dbg_register *reg)
  942. {
  943. struct gspca_dev *gspca_dev = video_drvdata(file);
  944. gspca_dev->usb_err = 0;
  945. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  946. }
  947. static int vidioc_s_register(struct file *file, void *priv,
  948. const struct v4l2_dbg_register *reg)
  949. {
  950. struct gspca_dev *gspca_dev = video_drvdata(file);
  951. gspca_dev->usb_err = 0;
  952. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  953. }
  954. #endif
  955. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  956. struct v4l2_fmtdesc *fmtdesc)
  957. {
  958. struct gspca_dev *gspca_dev = video_drvdata(file);
  959. int i, j, index;
  960. __u32 fmt_tb[8];
  961. /* give an index to each format */
  962. index = 0;
  963. j = 0;
  964. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  965. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  966. j = 0;
  967. for (;;) {
  968. if (fmt_tb[j] == fmt_tb[index])
  969. break;
  970. j++;
  971. }
  972. if (j == index) {
  973. if (fmtdesc->index == index)
  974. break; /* new format */
  975. index++;
  976. if (index >= ARRAY_SIZE(fmt_tb))
  977. return -EINVAL;
  978. }
  979. }
  980. if (i < 0)
  981. return -EINVAL; /* no more format */
  982. fmtdesc->pixelformat = fmt_tb[index];
  983. if (gspca_dev->cam.cam_mode[i].sizeimage <
  984. gspca_dev->cam.cam_mode[i].width *
  985. gspca_dev->cam.cam_mode[i].height)
  986. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  987. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  988. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  989. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  990. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  991. fmtdesc->description[4] = '\0';
  992. return 0;
  993. }
  994. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  995. struct v4l2_format *fmt)
  996. {
  997. struct gspca_dev *gspca_dev = video_drvdata(file);
  998. fmt->fmt.pix = gspca_dev->pixfmt;
  999. /* some drivers use priv internally, zero it before giving it back to
  1000. the core */
  1001. fmt->fmt.pix.priv = 0;
  1002. return 0;
  1003. }
  1004. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1005. struct v4l2_format *fmt)
  1006. {
  1007. int w, h, mode, mode2;
  1008. w = fmt->fmt.pix.width;
  1009. h = fmt->fmt.pix.height;
  1010. PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
  1011. fmt->fmt.pix.pixelformat, w, h);
  1012. /* search the nearest mode for width and height */
  1013. mode = wxh_to_nearest_mode(gspca_dev, w, h);
  1014. /* OK if right palette */
  1015. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1016. != fmt->fmt.pix.pixelformat) {
  1017. /* else, search the closest mode with the same pixel format */
  1018. mode2 = gspca_get_mode(gspca_dev, mode,
  1019. fmt->fmt.pix.pixelformat);
  1020. if (mode2 >= 0)
  1021. mode = mode2;
  1022. }
  1023. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1024. if (gspca_dev->sd_desc->try_fmt) {
  1025. /* pass original resolution to subdriver try_fmt */
  1026. fmt->fmt.pix.width = w;
  1027. fmt->fmt.pix.height = h;
  1028. gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
  1029. }
  1030. /* some drivers use priv internally, zero it before giving it back to
  1031. the core */
  1032. fmt->fmt.pix.priv = 0;
  1033. return mode; /* used when s_fmt */
  1034. }
  1035. static int vidioc_try_fmt_vid_cap(struct file *file,
  1036. void *priv,
  1037. struct v4l2_format *fmt)
  1038. {
  1039. struct gspca_dev *gspca_dev = video_drvdata(file);
  1040. int ret;
  1041. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1042. if (ret < 0)
  1043. return ret;
  1044. return 0;
  1045. }
  1046. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1047. struct v4l2_format *fmt)
  1048. {
  1049. struct gspca_dev *gspca_dev = video_drvdata(file);
  1050. int ret;
  1051. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1052. return -ERESTARTSYS;
  1053. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1054. if (ret < 0)
  1055. goto out;
  1056. if (gspca_dev->nframes != 0
  1057. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1058. ret = -EINVAL;
  1059. goto out;
  1060. }
  1061. if (gspca_dev->streaming) {
  1062. ret = -EBUSY;
  1063. goto out;
  1064. }
  1065. gspca_dev->curr_mode = ret;
  1066. if (gspca_dev->sd_desc->try_fmt)
  1067. /* subdriver try_fmt can modify format parameters */
  1068. gspca_dev->pixfmt = fmt->fmt.pix;
  1069. else
  1070. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret];
  1071. ret = 0;
  1072. out:
  1073. mutex_unlock(&gspca_dev->queue_lock);
  1074. return ret;
  1075. }
  1076. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1077. struct v4l2_frmsizeenum *fsize)
  1078. {
  1079. struct gspca_dev *gspca_dev = video_drvdata(file);
  1080. int i;
  1081. __u32 index = 0;
  1082. if (gspca_dev->sd_desc->enum_framesizes)
  1083. return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
  1084. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1085. if (fsize->pixel_format !=
  1086. gspca_dev->cam.cam_mode[i].pixelformat)
  1087. continue;
  1088. if (fsize->index == index) {
  1089. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1090. fsize->discrete.width =
  1091. gspca_dev->cam.cam_mode[i].width;
  1092. fsize->discrete.height =
  1093. gspca_dev->cam.cam_mode[i].height;
  1094. return 0;
  1095. }
  1096. index++;
  1097. }
  1098. return -EINVAL;
  1099. }
  1100. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1101. struct v4l2_frmivalenum *fival)
  1102. {
  1103. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1104. int mode;
  1105. __u32 i;
  1106. mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1107. if (mode < 0)
  1108. return -EINVAL;
  1109. if (gspca_dev->cam.mode_framerates == NULL ||
  1110. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1111. return -EINVAL;
  1112. if (fival->pixel_format !=
  1113. gspca_dev->cam.cam_mode[mode].pixelformat)
  1114. return -EINVAL;
  1115. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1116. if (fival->index == i) {
  1117. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1118. fival->discrete.numerator = 1;
  1119. fival->discrete.denominator =
  1120. gspca_dev->cam.mode_framerates[mode].rates[i];
  1121. return 0;
  1122. }
  1123. }
  1124. return -EINVAL;
  1125. }
  1126. static void gspca_release(struct v4l2_device *v4l2_device)
  1127. {
  1128. struct gspca_dev *gspca_dev =
  1129. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1130. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1131. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1132. kfree(gspca_dev->usb_buf);
  1133. kfree(gspca_dev);
  1134. }
  1135. static int dev_open(struct file *file)
  1136. {
  1137. struct gspca_dev *gspca_dev = video_drvdata(file);
  1138. int ret;
  1139. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1140. /* protect the subdriver against rmmod */
  1141. if (!try_module_get(gspca_dev->module))
  1142. return -ENODEV;
  1143. ret = v4l2_fh_open(file);
  1144. if (ret)
  1145. module_put(gspca_dev->module);
  1146. return ret;
  1147. }
  1148. static int dev_close(struct file *file)
  1149. {
  1150. struct gspca_dev *gspca_dev = video_drvdata(file);
  1151. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1152. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1153. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1154. return -ERESTARTSYS;
  1155. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1156. mutex_unlock(&gspca_dev->usb_lock);
  1157. return -ERESTARTSYS;
  1158. }
  1159. /* if the file did the capture, free the streaming resources */
  1160. if (gspca_dev->capt_file == file) {
  1161. if (gspca_dev->streaming)
  1162. gspca_stream_off(gspca_dev);
  1163. frame_free(gspca_dev);
  1164. }
  1165. module_put(gspca_dev->module);
  1166. mutex_unlock(&gspca_dev->queue_lock);
  1167. mutex_unlock(&gspca_dev->usb_lock);
  1168. PDEBUG(D_STREAM, "close done");
  1169. return v4l2_fh_release(file);
  1170. }
  1171. static int vidioc_querycap(struct file *file, void *priv,
  1172. struct v4l2_capability *cap)
  1173. {
  1174. struct gspca_dev *gspca_dev = video_drvdata(file);
  1175. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1176. sizeof cap->driver);
  1177. if (gspca_dev->dev->product != NULL) {
  1178. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1179. sizeof cap->card);
  1180. } else {
  1181. snprintf((char *) cap->card, sizeof cap->card,
  1182. "USB Camera (%04x:%04x)",
  1183. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1184. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1185. }
  1186. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1187. sizeof(cap->bus_info));
  1188. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1189. | V4L2_CAP_STREAMING
  1190. | V4L2_CAP_READWRITE;
  1191. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1192. return 0;
  1193. }
  1194. static int vidioc_enum_input(struct file *file, void *priv,
  1195. struct v4l2_input *input)
  1196. {
  1197. struct gspca_dev *gspca_dev = video_drvdata(file);
  1198. if (input->index != 0)
  1199. return -EINVAL;
  1200. input->type = V4L2_INPUT_TYPE_CAMERA;
  1201. input->status = gspca_dev->cam.input_flags;
  1202. strlcpy(input->name, gspca_dev->sd_desc->name,
  1203. sizeof input->name);
  1204. return 0;
  1205. }
  1206. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1207. {
  1208. *i = 0;
  1209. return 0;
  1210. }
  1211. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1212. {
  1213. if (i > 0)
  1214. return -EINVAL;
  1215. return (0);
  1216. }
  1217. static int vidioc_reqbufs(struct file *file, void *priv,
  1218. struct v4l2_requestbuffers *rb)
  1219. {
  1220. struct gspca_dev *gspca_dev = video_drvdata(file);
  1221. int i, ret = 0, streaming;
  1222. i = rb->memory; /* (avoid compilation warning) */
  1223. switch (i) {
  1224. case GSPCA_MEMORY_READ: /* (internal call) */
  1225. case V4L2_MEMORY_MMAP:
  1226. case V4L2_MEMORY_USERPTR:
  1227. break;
  1228. default:
  1229. return -EINVAL;
  1230. }
  1231. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1232. return -ERESTARTSYS;
  1233. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1234. && gspca_dev->memory != GSPCA_MEMORY_READ
  1235. && gspca_dev->memory != rb->memory) {
  1236. ret = -EBUSY;
  1237. goto out;
  1238. }
  1239. /* only one file may do the capture */
  1240. if (gspca_dev->capt_file != NULL
  1241. && gspca_dev->capt_file != file) {
  1242. ret = -EBUSY;
  1243. goto out;
  1244. }
  1245. /* if allocated, the buffers must not be mapped */
  1246. for (i = 0; i < gspca_dev->nframes; i++) {
  1247. if (gspca_dev->frame[i].vma_use_count) {
  1248. ret = -EBUSY;
  1249. goto out;
  1250. }
  1251. }
  1252. /* stop streaming */
  1253. streaming = gspca_dev->streaming;
  1254. if (streaming) {
  1255. gspca_stream_off(gspca_dev);
  1256. /* Don't restart the stream when switching from read
  1257. * to mmap mode */
  1258. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1259. streaming = 0;
  1260. }
  1261. /* free the previous allocated buffers, if any */
  1262. if (gspca_dev->nframes != 0)
  1263. frame_free(gspca_dev);
  1264. if (rb->count == 0) /* unrequest */
  1265. goto out;
  1266. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1267. if (ret == 0) {
  1268. rb->count = gspca_dev->nframes;
  1269. if (streaming)
  1270. ret = gspca_init_transfer(gspca_dev);
  1271. }
  1272. out:
  1273. mutex_unlock(&gspca_dev->queue_lock);
  1274. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1275. return ret;
  1276. }
  1277. static int vidioc_querybuf(struct file *file, void *priv,
  1278. struct v4l2_buffer *v4l2_buf)
  1279. {
  1280. struct gspca_dev *gspca_dev = video_drvdata(file);
  1281. struct gspca_frame *frame;
  1282. if (v4l2_buf->index >= gspca_dev->nframes)
  1283. return -EINVAL;
  1284. frame = &gspca_dev->frame[v4l2_buf->index];
  1285. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1286. return 0;
  1287. }
  1288. static int vidioc_streamon(struct file *file, void *priv,
  1289. enum v4l2_buf_type buf_type)
  1290. {
  1291. struct gspca_dev *gspca_dev = video_drvdata(file);
  1292. int ret;
  1293. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1294. return -EINVAL;
  1295. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1296. return -ERESTARTSYS;
  1297. /* check the capture file */
  1298. if (gspca_dev->capt_file != file) {
  1299. ret = -EBUSY;
  1300. goto out;
  1301. }
  1302. if (gspca_dev->nframes == 0
  1303. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1304. ret = -EINVAL;
  1305. goto out;
  1306. }
  1307. if (!gspca_dev->streaming) {
  1308. ret = gspca_init_transfer(gspca_dev);
  1309. if (ret < 0)
  1310. goto out;
  1311. }
  1312. PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK",
  1313. gspca_dev->pixfmt.pixelformat,
  1314. gspca_dev->pixfmt.width, gspca_dev->pixfmt.height);
  1315. ret = 0;
  1316. out:
  1317. mutex_unlock(&gspca_dev->queue_lock);
  1318. return ret;
  1319. }
  1320. static int vidioc_streamoff(struct file *file, void *priv,
  1321. enum v4l2_buf_type buf_type)
  1322. {
  1323. struct gspca_dev *gspca_dev = video_drvdata(file);
  1324. int i, ret;
  1325. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1326. return -EINVAL;
  1327. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1328. return -ERESTARTSYS;
  1329. if (!gspca_dev->streaming) {
  1330. ret = 0;
  1331. goto out;
  1332. }
  1333. /* check the capture file */
  1334. if (gspca_dev->capt_file != file) {
  1335. ret = -EBUSY;
  1336. goto out;
  1337. }
  1338. /* stop streaming */
  1339. gspca_stream_off(gspca_dev);
  1340. /* In case another thread is waiting in dqbuf */
  1341. wake_up_interruptible(&gspca_dev->wq);
  1342. /* empty the transfer queues */
  1343. for (i = 0; i < gspca_dev->nframes; i++)
  1344. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1345. atomic_set(&gspca_dev->fr_q, 0);
  1346. atomic_set(&gspca_dev->fr_i, 0);
  1347. gspca_dev->fr_o = 0;
  1348. ret = 0;
  1349. out:
  1350. mutex_unlock(&gspca_dev->queue_lock);
  1351. return ret;
  1352. }
  1353. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1354. struct v4l2_jpegcompression *jpegcomp)
  1355. {
  1356. struct gspca_dev *gspca_dev = video_drvdata(file);
  1357. gspca_dev->usb_err = 0;
  1358. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1359. }
  1360. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1361. const struct v4l2_jpegcompression *jpegcomp)
  1362. {
  1363. struct gspca_dev *gspca_dev = video_drvdata(file);
  1364. gspca_dev->usb_err = 0;
  1365. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1366. }
  1367. static int vidioc_g_parm(struct file *filp, void *priv,
  1368. struct v4l2_streamparm *parm)
  1369. {
  1370. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1371. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1372. if (gspca_dev->sd_desc->get_streamparm) {
  1373. gspca_dev->usb_err = 0;
  1374. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1375. return gspca_dev->usb_err;
  1376. }
  1377. return 0;
  1378. }
  1379. static int vidioc_s_parm(struct file *filp, void *priv,
  1380. struct v4l2_streamparm *parm)
  1381. {
  1382. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1383. unsigned int n;
  1384. n = parm->parm.capture.readbuffers;
  1385. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1386. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1387. else
  1388. gspca_dev->nbufread = n;
  1389. if (gspca_dev->sd_desc->set_streamparm) {
  1390. gspca_dev->usb_err = 0;
  1391. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1392. return gspca_dev->usb_err;
  1393. }
  1394. return 0;
  1395. }
  1396. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1397. {
  1398. struct gspca_dev *gspca_dev = video_drvdata(file);
  1399. struct gspca_frame *frame;
  1400. struct page *page;
  1401. unsigned long addr, start, size;
  1402. int i, ret;
  1403. start = vma->vm_start;
  1404. size = vma->vm_end - vma->vm_start;
  1405. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1406. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1407. return -ERESTARTSYS;
  1408. if (gspca_dev->capt_file != file) {
  1409. ret = -EINVAL;
  1410. goto out;
  1411. }
  1412. frame = NULL;
  1413. for (i = 0; i < gspca_dev->nframes; ++i) {
  1414. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1415. PDEBUG(D_STREAM, "mmap bad memory type");
  1416. break;
  1417. }
  1418. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1419. == vma->vm_pgoff) {
  1420. frame = &gspca_dev->frame[i];
  1421. break;
  1422. }
  1423. }
  1424. if (frame == NULL) {
  1425. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1426. ret = -EINVAL;
  1427. goto out;
  1428. }
  1429. if (size != frame->v4l2_buf.length) {
  1430. PDEBUG(D_STREAM, "mmap bad size");
  1431. ret = -EINVAL;
  1432. goto out;
  1433. }
  1434. /*
  1435. * - VM_IO marks the area as being a mmaped region for I/O to a
  1436. * device. It also prevents the region from being core dumped.
  1437. */
  1438. vma->vm_flags |= VM_IO;
  1439. addr = (unsigned long) frame->data;
  1440. while (size > 0) {
  1441. page = vmalloc_to_page((void *) addr);
  1442. ret = vm_insert_page(vma, start, page);
  1443. if (ret < 0)
  1444. goto out;
  1445. start += PAGE_SIZE;
  1446. addr += PAGE_SIZE;
  1447. size -= PAGE_SIZE;
  1448. }
  1449. vma->vm_ops = &gspca_vm_ops;
  1450. vma->vm_private_data = frame;
  1451. gspca_vm_open(vma);
  1452. ret = 0;
  1453. out:
  1454. mutex_unlock(&gspca_dev->queue_lock);
  1455. return ret;
  1456. }
  1457. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1458. enum v4l2_memory memory)
  1459. {
  1460. if (!gspca_dev->present)
  1461. return -ENODEV;
  1462. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1463. !gspca_dev->streaming)
  1464. return -EINVAL;
  1465. /* check if a frame is ready */
  1466. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1467. }
  1468. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1469. enum v4l2_memory memory)
  1470. {
  1471. int ret;
  1472. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1473. return -ERESTARTSYS;
  1474. ret = frame_ready_nolock(gspca_dev, file, memory);
  1475. mutex_unlock(&gspca_dev->queue_lock);
  1476. return ret;
  1477. }
  1478. /*
  1479. * dequeue a video buffer
  1480. *
  1481. * If nonblock_ing is false, block until a buffer is available.
  1482. */
  1483. static int vidioc_dqbuf(struct file *file, void *priv,
  1484. struct v4l2_buffer *v4l2_buf)
  1485. {
  1486. struct gspca_dev *gspca_dev = video_drvdata(file);
  1487. struct gspca_frame *frame;
  1488. int i, j, ret;
  1489. PDEBUG(D_FRAM, "dqbuf");
  1490. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1491. return -ERESTARTSYS;
  1492. for (;;) {
  1493. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1494. if (ret < 0)
  1495. goto out;
  1496. if (ret > 0)
  1497. break;
  1498. mutex_unlock(&gspca_dev->queue_lock);
  1499. if (file->f_flags & O_NONBLOCK)
  1500. return -EAGAIN;
  1501. /* wait till a frame is ready */
  1502. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1503. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1504. msecs_to_jiffies(3000));
  1505. if (ret < 0)
  1506. return ret;
  1507. if (ret == 0)
  1508. return -EIO;
  1509. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1510. return -ERESTARTSYS;
  1511. }
  1512. i = gspca_dev->fr_o;
  1513. j = gspca_dev->fr_queue[i];
  1514. frame = &gspca_dev->frame[j];
  1515. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1516. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1517. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1518. PDEBUG(D_FRAM, "dqbuf %d", j);
  1519. ret = 0;
  1520. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1521. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1522. frame->data,
  1523. frame->v4l2_buf.bytesused)) {
  1524. PERR("dqbuf cp to user failed");
  1525. ret = -EFAULT;
  1526. }
  1527. }
  1528. out:
  1529. mutex_unlock(&gspca_dev->queue_lock);
  1530. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1531. mutex_lock(&gspca_dev->usb_lock);
  1532. gspca_dev->usb_err = 0;
  1533. if (gspca_dev->present)
  1534. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1535. mutex_unlock(&gspca_dev->usb_lock);
  1536. }
  1537. return ret;
  1538. }
  1539. /*
  1540. * queue a video buffer
  1541. *
  1542. * Attempting to queue a buffer that has already been
  1543. * queued will return -EINVAL.
  1544. */
  1545. static int vidioc_qbuf(struct file *file, void *priv,
  1546. struct v4l2_buffer *v4l2_buf)
  1547. {
  1548. struct gspca_dev *gspca_dev = video_drvdata(file);
  1549. struct gspca_frame *frame;
  1550. int i, index, ret;
  1551. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1552. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1553. return -ERESTARTSYS;
  1554. index = v4l2_buf->index;
  1555. if ((unsigned) index >= gspca_dev->nframes) {
  1556. PDEBUG(D_FRAM,
  1557. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1558. ret = -EINVAL;
  1559. goto out;
  1560. }
  1561. if (v4l2_buf->memory != gspca_dev->memory) {
  1562. PDEBUG(D_FRAM, "qbuf bad memory type");
  1563. ret = -EINVAL;
  1564. goto out;
  1565. }
  1566. frame = &gspca_dev->frame[index];
  1567. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1568. PDEBUG(D_FRAM, "qbuf bad state");
  1569. ret = -EINVAL;
  1570. goto out;
  1571. }
  1572. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1573. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1574. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1575. frame->v4l2_buf.length = v4l2_buf->length;
  1576. }
  1577. /* put the buffer in the 'queued' queue */
  1578. i = atomic_read(&gspca_dev->fr_q);
  1579. gspca_dev->fr_queue[i] = index;
  1580. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1581. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1582. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1583. ret = 0;
  1584. out:
  1585. mutex_unlock(&gspca_dev->queue_lock);
  1586. return ret;
  1587. }
  1588. /*
  1589. * allocate the resources for read()
  1590. */
  1591. static int read_alloc(struct gspca_dev *gspca_dev,
  1592. struct file *file)
  1593. {
  1594. struct v4l2_buffer v4l2_buf;
  1595. int i, ret;
  1596. PDEBUG(D_STREAM, "read alloc");
  1597. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1598. return -ERESTARTSYS;
  1599. if (gspca_dev->nframes == 0) {
  1600. struct v4l2_requestbuffers rb;
  1601. memset(&rb, 0, sizeof rb);
  1602. rb.count = gspca_dev->nbufread;
  1603. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1604. rb.memory = GSPCA_MEMORY_READ;
  1605. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1606. if (ret != 0) {
  1607. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1608. goto out;
  1609. }
  1610. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1611. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1612. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1613. for (i = 0; i < gspca_dev->nbufread; i++) {
  1614. v4l2_buf.index = i;
  1615. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1616. if (ret != 0) {
  1617. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1618. goto out;
  1619. }
  1620. }
  1621. }
  1622. /* start streaming */
  1623. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1624. if (ret != 0)
  1625. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1626. out:
  1627. mutex_unlock(&gspca_dev->usb_lock);
  1628. return ret;
  1629. }
  1630. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1631. {
  1632. struct gspca_dev *gspca_dev = video_drvdata(file);
  1633. unsigned long req_events = poll_requested_events(wait);
  1634. int ret = 0;
  1635. PDEBUG(D_FRAM, "poll");
  1636. if (req_events & POLLPRI)
  1637. ret |= v4l2_ctrl_poll(file, wait);
  1638. if (req_events & (POLLIN | POLLRDNORM)) {
  1639. /* if reqbufs is not done, the user would use read() */
  1640. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1641. if (read_alloc(gspca_dev, file) != 0) {
  1642. ret |= POLLERR;
  1643. goto out;
  1644. }
  1645. }
  1646. poll_wait(file, &gspca_dev->wq, wait);
  1647. /* check if an image has been received */
  1648. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1649. ret |= POLLERR;
  1650. goto out;
  1651. }
  1652. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1653. ret |= POLLIN | POLLRDNORM;
  1654. mutex_unlock(&gspca_dev->queue_lock);
  1655. }
  1656. out:
  1657. if (!gspca_dev->present)
  1658. ret |= POLLHUP;
  1659. return ret;
  1660. }
  1661. static ssize_t dev_read(struct file *file, char __user *data,
  1662. size_t count, loff_t *ppos)
  1663. {
  1664. struct gspca_dev *gspca_dev = video_drvdata(file);
  1665. struct gspca_frame *frame;
  1666. struct v4l2_buffer v4l2_buf;
  1667. struct timeval timestamp;
  1668. int n, ret, ret2;
  1669. PDEBUG(D_FRAM, "read (%zd)", count);
  1670. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1671. ret = read_alloc(gspca_dev, file);
  1672. if (ret != 0)
  1673. return ret;
  1674. }
  1675. /* get a frame */
  1676. v4l2_get_timestamp(&timestamp);
  1677. timestamp.tv_sec--;
  1678. n = 2;
  1679. for (;;) {
  1680. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1681. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1682. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1683. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1684. if (ret != 0) {
  1685. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1686. return ret;
  1687. }
  1688. /* if the process slept for more than 1 second,
  1689. * get a newer frame */
  1690. frame = &gspca_dev->frame[v4l2_buf.index];
  1691. if (--n < 0)
  1692. break; /* avoid infinite loop */
  1693. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1694. break;
  1695. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1696. if (ret != 0) {
  1697. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1698. return ret;
  1699. }
  1700. }
  1701. /* copy the frame */
  1702. if (count > frame->v4l2_buf.bytesused)
  1703. count = frame->v4l2_buf.bytesused;
  1704. ret = copy_to_user(data, frame->data, count);
  1705. if (ret != 0) {
  1706. PERR("read cp to user lack %d / %zd", ret, count);
  1707. ret = -EFAULT;
  1708. goto out;
  1709. }
  1710. ret = count;
  1711. out:
  1712. /* in each case, requeue the buffer */
  1713. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1714. if (ret2 != 0)
  1715. return ret2;
  1716. return ret;
  1717. }
  1718. static struct v4l2_file_operations dev_fops = {
  1719. .owner = THIS_MODULE,
  1720. .open = dev_open,
  1721. .release = dev_close,
  1722. .read = dev_read,
  1723. .mmap = dev_mmap,
  1724. .unlocked_ioctl = video_ioctl2,
  1725. .poll = dev_poll,
  1726. };
  1727. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1728. .vidioc_querycap = vidioc_querycap,
  1729. .vidioc_dqbuf = vidioc_dqbuf,
  1730. .vidioc_qbuf = vidioc_qbuf,
  1731. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1732. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1733. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1734. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1735. .vidioc_streamon = vidioc_streamon,
  1736. .vidioc_enum_input = vidioc_enum_input,
  1737. .vidioc_g_input = vidioc_g_input,
  1738. .vidioc_s_input = vidioc_s_input,
  1739. .vidioc_reqbufs = vidioc_reqbufs,
  1740. .vidioc_querybuf = vidioc_querybuf,
  1741. .vidioc_streamoff = vidioc_streamoff,
  1742. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1743. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1744. .vidioc_g_parm = vidioc_g_parm,
  1745. .vidioc_s_parm = vidioc_s_parm,
  1746. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1747. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1748. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1749. .vidioc_g_chip_info = vidioc_g_chip_info,
  1750. .vidioc_g_register = vidioc_g_register,
  1751. .vidioc_s_register = vidioc_s_register,
  1752. #endif
  1753. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1754. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1755. };
  1756. static const struct video_device gspca_template = {
  1757. .name = "gspca main driver",
  1758. .fops = &dev_fops,
  1759. .ioctl_ops = &dev_ioctl_ops,
  1760. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1761. };
  1762. /*
  1763. * probe and create a new gspca device
  1764. *
  1765. * This function must be called by the sub-driver when it is
  1766. * called for probing a new device.
  1767. */
  1768. int gspca_dev_probe2(struct usb_interface *intf,
  1769. const struct usb_device_id *id,
  1770. const struct sd_desc *sd_desc,
  1771. int dev_size,
  1772. struct module *module)
  1773. {
  1774. struct gspca_dev *gspca_dev;
  1775. struct usb_device *dev = interface_to_usbdev(intf);
  1776. int ret;
  1777. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1778. sd_desc->name, id->idVendor, id->idProduct);
  1779. /* create the device */
  1780. if (dev_size < sizeof *gspca_dev)
  1781. dev_size = sizeof *gspca_dev;
  1782. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1783. if (!gspca_dev) {
  1784. pr_err("couldn't kzalloc gspca struct\n");
  1785. return -ENOMEM;
  1786. }
  1787. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1788. if (!gspca_dev->usb_buf) {
  1789. pr_err("out of memory\n");
  1790. ret = -ENOMEM;
  1791. goto out;
  1792. }
  1793. gspca_dev->dev = dev;
  1794. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1795. gspca_dev->xfer_ep = -1;
  1796. /* check if any audio device */
  1797. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1798. int i;
  1799. struct usb_interface *intf2;
  1800. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1801. intf2 = dev->actconfig->interface[i];
  1802. if (intf2 != NULL
  1803. && intf2->altsetting != NULL
  1804. && intf2->altsetting->desc.bInterfaceClass ==
  1805. USB_CLASS_AUDIO) {
  1806. gspca_dev->audio = 1;
  1807. break;
  1808. }
  1809. }
  1810. }
  1811. gspca_dev->v4l2_dev.release = gspca_release;
  1812. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1813. if (ret)
  1814. goto out;
  1815. gspca_dev->sd_desc = sd_desc;
  1816. gspca_dev->nbufread = 2;
  1817. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1818. gspca_dev->vdev = gspca_template;
  1819. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1820. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1821. gspca_dev->module = module;
  1822. gspca_dev->present = 1;
  1823. mutex_init(&gspca_dev->usb_lock);
  1824. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1825. mutex_init(&gspca_dev->queue_lock);
  1826. init_waitqueue_head(&gspca_dev->wq);
  1827. /* configure the subdriver and initialize the USB device */
  1828. ret = sd_desc->config(gspca_dev, id);
  1829. if (ret < 0)
  1830. goto out;
  1831. ret = sd_desc->init(gspca_dev);
  1832. if (ret < 0)
  1833. goto out;
  1834. if (sd_desc->init_controls)
  1835. ret = sd_desc->init_controls(gspca_dev);
  1836. if (ret < 0)
  1837. goto out;
  1838. gspca_set_default_mode(gspca_dev);
  1839. ret = gspca_input_connect(gspca_dev);
  1840. if (ret)
  1841. goto out;
  1842. /*
  1843. * Don't take usb_lock for these ioctls. This improves latency if
  1844. * usb_lock is taken for a long time, e.g. when changing a control
  1845. * value, and a new frame is ready to be dequeued.
  1846. */
  1847. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1848. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1849. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1850. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1851. if (!gspca_dev->sd_desc->get_register)
  1852. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1853. if (!gspca_dev->sd_desc->set_register)
  1854. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1855. #endif
  1856. if (!gspca_dev->sd_desc->get_jcomp)
  1857. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1858. if (!gspca_dev->sd_desc->set_jcomp)
  1859. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1860. /* init video stuff */
  1861. ret = video_register_device(&gspca_dev->vdev,
  1862. VFL_TYPE_GRABBER,
  1863. -1);
  1864. if (ret < 0) {
  1865. pr_err("video_register_device err %d\n", ret);
  1866. goto out;
  1867. }
  1868. usb_set_intfdata(intf, gspca_dev);
  1869. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  1870. gspca_input_create_urb(gspca_dev);
  1871. return 0;
  1872. out:
  1873. #if IS_ENABLED(CONFIG_INPUT)
  1874. if (gspca_dev->input_dev)
  1875. input_unregister_device(gspca_dev->input_dev);
  1876. #endif
  1877. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1878. kfree(gspca_dev->usb_buf);
  1879. kfree(gspca_dev);
  1880. return ret;
  1881. }
  1882. EXPORT_SYMBOL(gspca_dev_probe2);
  1883. /* same function as the previous one, but check the interface */
  1884. int gspca_dev_probe(struct usb_interface *intf,
  1885. const struct usb_device_id *id,
  1886. const struct sd_desc *sd_desc,
  1887. int dev_size,
  1888. struct module *module)
  1889. {
  1890. struct usb_device *dev = interface_to_usbdev(intf);
  1891. /* we don't handle multi-config cameras */
  1892. if (dev->descriptor.bNumConfigurations != 1) {
  1893. pr_err("%04x:%04x too many config\n",
  1894. id->idVendor, id->idProduct);
  1895. return -ENODEV;
  1896. }
  1897. /* the USB video interface must be the first one */
  1898. if (dev->actconfig->desc.bNumInterfaces != 1
  1899. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1900. return -ENODEV;
  1901. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1902. }
  1903. EXPORT_SYMBOL(gspca_dev_probe);
  1904. /*
  1905. * USB disconnection
  1906. *
  1907. * This function must be called by the sub-driver
  1908. * when the device disconnects, after the specific resources are freed.
  1909. */
  1910. void gspca_disconnect(struct usb_interface *intf)
  1911. {
  1912. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1913. #if IS_ENABLED(CONFIG_INPUT)
  1914. struct input_dev *input_dev;
  1915. #endif
  1916. PDEBUG(D_PROBE, "%s disconnect",
  1917. video_device_node_name(&gspca_dev->vdev));
  1918. mutex_lock(&gspca_dev->usb_lock);
  1919. gspca_dev->present = 0;
  1920. destroy_urbs(gspca_dev);
  1921. #if IS_ENABLED(CONFIG_INPUT)
  1922. gspca_input_destroy_urb(gspca_dev);
  1923. input_dev = gspca_dev->input_dev;
  1924. if (input_dev) {
  1925. gspca_dev->input_dev = NULL;
  1926. input_unregister_device(input_dev);
  1927. }
  1928. #endif
  1929. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  1930. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  1931. gspca_dev->sd_desc->stop0(gspca_dev);
  1932. gspca_dev->streaming = 0;
  1933. gspca_dev->dev = NULL;
  1934. wake_up_interruptible(&gspca_dev->wq);
  1935. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1936. video_unregister_device(&gspca_dev->vdev);
  1937. mutex_unlock(&gspca_dev->usb_lock);
  1938. /* (this will call gspca_release() immediately or on last close) */
  1939. v4l2_device_put(&gspca_dev->v4l2_dev);
  1940. }
  1941. EXPORT_SYMBOL(gspca_disconnect);
  1942. #ifdef CONFIG_PM
  1943. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1944. {
  1945. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1946. gspca_input_destroy_urb(gspca_dev);
  1947. if (!gspca_dev->streaming)
  1948. return 0;
  1949. mutex_lock(&gspca_dev->usb_lock);
  1950. gspca_dev->frozen = 1; /* avoid urb error messages */
  1951. gspca_dev->usb_err = 0;
  1952. if (gspca_dev->sd_desc->stopN)
  1953. gspca_dev->sd_desc->stopN(gspca_dev);
  1954. destroy_urbs(gspca_dev);
  1955. gspca_set_alt0(gspca_dev);
  1956. if (gspca_dev->sd_desc->stop0)
  1957. gspca_dev->sd_desc->stop0(gspca_dev);
  1958. mutex_unlock(&gspca_dev->usb_lock);
  1959. return 0;
  1960. }
  1961. EXPORT_SYMBOL(gspca_suspend);
  1962. int gspca_resume(struct usb_interface *intf)
  1963. {
  1964. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1965. int streaming, ret = 0;
  1966. mutex_lock(&gspca_dev->usb_lock);
  1967. gspca_dev->frozen = 0;
  1968. gspca_dev->usb_err = 0;
  1969. gspca_dev->sd_desc->init(gspca_dev);
  1970. /*
  1971. * Most subdrivers send all ctrl values on sd_start and thus
  1972. * only write to the device registers on s_ctrl when streaming ->
  1973. * Clear streaming to avoid setting all ctrls twice.
  1974. */
  1975. streaming = gspca_dev->streaming;
  1976. gspca_dev->streaming = 0;
  1977. if (streaming)
  1978. ret = gspca_init_transfer(gspca_dev);
  1979. else
  1980. gspca_input_create_urb(gspca_dev);
  1981. mutex_unlock(&gspca_dev->usb_lock);
  1982. return ret;
  1983. }
  1984. EXPORT_SYMBOL(gspca_resume);
  1985. #endif
  1986. /* -- module insert / remove -- */
  1987. static int __init gspca_init(void)
  1988. {
  1989. pr_info("v" GSPCA_VERSION " registered\n");
  1990. return 0;
  1991. }
  1992. static void __exit gspca_exit(void)
  1993. {
  1994. }
  1995. module_init(gspca_init);
  1996. module_exit(gspca_exit);
  1997. module_param_named(debug, gspca_debug, int, 0644);
  1998. MODULE_PARM_DESC(debug,
  1999. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");